2 * framebuffer driver for VBE 2.0 compliant graphic boards
4 * switching to graphics mode happens at boot time (while
5 * running in real mode, see arch/i386/video.S).
7 * (c) 1998 Gerd Knorr <kraxel@goldbach.in-berlin.de>
11 #include <linux/module.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/string.h>
16 #include <linux/tty.h>
17 #include <linux/malloc.h>
18 #include <linux/delay.h>
20 #include <linux/console.h>
21 #include <linux/selection.h>
22 #include <linux/ioport.h>
23 #include <linux/init.h>
28 #include <video/fbcon.h>
29 #include <video/fbcon-cfb8.h>
30 #include <video/fbcon-cfb16.h>
31 #include <video/fbcon-cfb24.h>
32 #include <video/fbcon-cfb32.h>
33 #include <video/fbcon-mac.h>
35 #define dac_reg (0x3c8)
36 #define dac_val (0x3c9)
38 /* --------------------------------------------------------------------- */
45 unsigned long video_base
; /* physical addr */
47 char *video_vbase
; /* mapped */
51 static int video_width
;
52 static int video_height
;
53 static int video_height_virtual
;
54 static int video_type
= FB_TYPE_PACKED_PIXELS
;
55 static int video_visual
;
56 static int video_linelength
;
57 static int video_cmap_len
;
59 /* --------------------------------------------------------------------- */
61 static struct fb_var_screeninfo vesafb_defined
= {
62 0,0,0,0, /* W,H, W, H (virtual) load xres,xres_virtual*/
63 0,0, /* virtual -> visible no offset */
64 8, /* depth -> load bits_per_pixel */
69 {0,0,0}, /* transparency */
70 0, /* standard pixel format */
75 0L,0L,0, /* No sync info */
76 FB_VMODE_NONINTERLACED
,
80 static struct display disp
;
81 static struct fb_info fb_info
;
82 static struct { u_short blue
, green
, red
, pad
; } palette
[256];
84 #ifdef FBCON_HAS_CFB16
87 #ifdef FBCON_HAS_CFB24
90 #ifdef FBCON_HAS_CFB32
95 static int inverse
= 0;
97 static int currcon
= 0;
99 static int pmi_setpal
= 0; /* pmi for palette changes ??? */
100 static int ypan
= 0; /* 0..nothing, 1..ypan, 2..ywrap */
101 static unsigned short *pmi_base
= 0;
102 static void (*pmi_start
)(void);
103 static void (*pmi_pal
)(void);
105 static struct display_switch vesafb_sw
;
107 /* --------------------------------------------------------------------- */
109 static int vesafb_pan_display(struct fb_var_screeninfo
*var
, int con
,
110 struct fb_info
*info
)
118 if (var
->yoffset
> var
->yres_virtual
)
120 if ((ypan
==1) && var
->yoffset
+var
->yres
> var
->yres_virtual
)
123 offset
= (var
->yoffset
* video_linelength
+ var
->xoffset
) / 4;
125 __asm__
__volatile__(
127 : /* no return value */
128 : "a" (0x4f07), /* EAX */
130 "c" (offset
), /* ECX */
131 "d" (offset
>> 16), /* EDX */
132 "D" (&pmi_start
)); /* EDI */
136 static int vesafb_update_var(int con
, struct fb_info
*info
)
138 if (con
== currcon
&& ypan
) {
139 struct fb_var_screeninfo
*var
= &fb_display
[currcon
].var
;
140 return vesafb_pan_display(var
,con
,info
);
145 static int vesafb_get_fix(struct fb_fix_screeninfo
*fix
, int con
,
146 struct fb_info
*info
)
148 memset(fix
, 0, sizeof(struct fb_fix_screeninfo
));
149 strcpy(fix
->id
,"VESA VGA");
151 fix
->smem_start
=video_base
;
152 fix
->smem_len
=video_size
;
153 fix
->type
= video_type
;
154 fix
->visual
= video_visual
;
156 fix
->ypanstep
= ypan
? 1 : 0;
157 fix
->ywrapstep
= (ypan
>1) ? 1 : 0;
158 fix
->line_length
=video_linelength
;
162 static int vesafb_get_var(struct fb_var_screeninfo
*var
, int con
,
163 struct fb_info
*info
)
166 memcpy(var
, &vesafb_defined
, sizeof(struct fb_var_screeninfo
));
168 *var
=fb_display
[con
].var
;
172 static void vesafb_set_disp(int con
)
174 struct fb_fix_screeninfo fix
;
175 struct display
*display
;
176 struct display_switch
*sw
;
179 display
= &fb_display
[con
];
181 display
= &disp
; /* used during initialization */
183 vesafb_get_fix(&fix
, con
, 0);
185 memset(display
, 0, sizeof(struct display
));
186 display
->screen_base
= video_vbase
;
187 display
->visual
= fix
.visual
;
188 display
->type
= fix
.type
;
189 display
->type_aux
= fix
.type_aux
;
190 display
->ypanstep
= fix
.ypanstep
;
191 display
->ywrapstep
= fix
.ywrapstep
;
192 display
->line_length
= fix
.line_length
;
193 display
->next_line
= fix
.line_length
;
194 display
->can_soft_blank
= 0;
195 display
->inverse
= inverse
;
196 vesafb_get_var(&display
->var
, -1, &fb_info
);
199 #ifdef FBCON_HAS_CFB8
204 #ifdef FBCON_HAS_CFB16
208 display
->dispsw_data
= fbcon_cmap
.cfb16
;
211 #ifdef FBCON_HAS_CFB24
214 display
->dispsw_data
= fbcon_cmap
.cfb24
;
217 #ifdef FBCON_HAS_CFB32
220 display
->dispsw_data
= fbcon_cmap
.cfb32
;
232 memcpy(&vesafb_sw
, sw
, sizeof(*sw
));
233 display
->dispsw
= &vesafb_sw
;
235 display
->scrollmode
= SCROLL_YREDRAW
;
236 vesafb_sw
.bmove
= fbcon_redraw_bmove
;
240 static int vesafb_set_var(struct fb_var_screeninfo
*var
, int con
,
241 struct fb_info
*info
)
243 static int first
= 1;
245 if (var
->xres
!= vesafb_defined
.xres
||
246 var
->yres
!= vesafb_defined
.yres
||
247 var
->xres_virtual
!= vesafb_defined
.xres_virtual
||
248 var
->yres_virtual
> video_height_virtual
||
249 var
->yres_virtual
< video_height
||
251 var
->bits_per_pixel
!= vesafb_defined
.bits_per_pixel
||
254 printk(KERN_ERR
"Vesafb does not support changing the video mode\n");
260 if ((var
->activate
& FB_ACTIVATE_MASK
) == FB_ACTIVATE_TEST
)
264 if (vesafb_defined
.yres_virtual
!= var
->yres_virtual
) {
265 vesafb_defined
.yres_virtual
= var
->yres_virtual
;
267 fb_display
[con
].var
= vesafb_defined
;
268 info
->changevar(con
);
272 if (var
->yoffset
!= vesafb_defined
.yoffset
)
273 return vesafb_pan_display(var
,con
,info
);
282 static int vesa_getcolreg(unsigned regno
, unsigned *red
, unsigned *green
,
283 unsigned *blue
, unsigned *transp
,
284 struct fb_info
*fb_info
)
287 * Read a single color register and split it into colors/transparent.
288 * Return != 0 for invalid regno.
291 if (regno
>= video_cmap_len
)
294 *red
= palette
[regno
].red
;
295 *green
= palette
[regno
].green
;
296 *blue
= palette
[regno
].blue
;
301 #ifdef FBCON_HAS_CFB8
303 static void vesa_setpalette(int regno
, unsigned red
, unsigned green
, unsigned blue
)
305 struct { u_char blue
, green
, red
, pad
; } entry
;
308 entry
.red
= red
>> 10;
309 entry
.green
= green
>> 10;
310 entry
.blue
= blue
>> 10;
312 __asm__
__volatile__(
314 : /* no return value */
315 : "a" (0x4f09), /* EAX */
318 "d" (regno
), /* EDX */
319 "D" (&entry
), /* EDI */
320 "S" (&pmi_pal
)); /* ESI */
322 /* without protected mode interface, try VGA registers... */
323 outb_p(regno
, dac_reg
);
324 outb_p(red
>> 10, dac_val
);
325 outb_p(green
>> 10, dac_val
);
326 outb_p(blue
>> 10, dac_val
);
332 static int vesa_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
333 unsigned blue
, unsigned transp
,
334 struct fb_info
*fb_info
)
337 * Set a single color register. The values supplied are
338 * already rounded down to the hardware's capabilities
339 * (according to the entries in the `var' structure). Return
340 * != 0 for invalid regno.
343 if (regno
>= video_cmap_len
)
346 palette
[regno
].red
= red
;
347 palette
[regno
].green
= green
;
348 palette
[regno
].blue
= blue
;
351 #ifdef FBCON_HAS_CFB8
353 vesa_setpalette(regno
,red
,green
,blue
);
356 #ifdef FBCON_HAS_CFB16
359 if (vesafb_defined
.red
.offset
== 10) {
361 fbcon_cmap
.cfb16
[regno
] =
362 ((red
& 0xf800) >> 1) |
363 ((green
& 0xf800) >> 6) |
364 ((blue
& 0xf800) >> 11);
367 fbcon_cmap
.cfb16
[regno
] =
369 ((green
& 0xfc00) >> 5) |
370 ((blue
& 0xf800) >> 11);
374 #ifdef FBCON_HAS_CFB24
379 fbcon_cmap
.cfb24
[regno
] =
380 (red
<< vesafb_defined
.red
.offset
) |
381 (green
<< vesafb_defined
.green
.offset
) |
382 (blue
<< vesafb_defined
.blue
.offset
);
385 #ifdef FBCON_HAS_CFB32
390 fbcon_cmap
.cfb32
[regno
] =
391 (red
<< vesafb_defined
.red
.offset
) |
392 (green
<< vesafb_defined
.green
.offset
) |
393 (blue
<< vesafb_defined
.blue
.offset
);
400 static void do_install_cmap(int con
, struct fb_info
*info
)
404 if (fb_display
[con
].cmap
.len
)
405 fb_set_cmap(&fb_display
[con
].cmap
, 1, vesa_setcolreg
, info
);
407 fb_set_cmap(fb_default_cmap(video_cmap_len
), 1, vesa_setcolreg
,
411 static int vesafb_get_cmap(struct fb_cmap
*cmap
, int kspc
, int con
,
412 struct fb_info
*info
)
414 if (con
== currcon
) /* current console? */
415 return fb_get_cmap(cmap
, kspc
, vesa_getcolreg
, info
);
416 else if (fb_display
[con
].cmap
.len
) /* non default colormap? */
417 fb_copy_cmap(&fb_display
[con
].cmap
, cmap
, kspc
? 0 : 2);
419 fb_copy_cmap(fb_default_cmap(video_cmap_len
),
424 static int vesafb_set_cmap(struct fb_cmap
*cmap
, int kspc
, int con
,
425 struct fb_info
*info
)
429 if (!fb_display
[con
].cmap
.len
) { /* no colormap allocated? */
430 err
= fb_alloc_cmap(&fb_display
[con
].cmap
,video_cmap_len
,0);
434 if (con
== currcon
) /* current console? */
435 return fb_set_cmap(cmap
, kspc
, vesa_setcolreg
, info
);
437 fb_copy_cmap(cmap
, &fb_display
[con
].cmap
, kspc
? 0 : 1);
441 static struct fb_ops vesafb_ops
= {
443 fb_get_fix
: vesafb_get_fix
,
444 fb_get_var
: vesafb_get_var
,
445 fb_set_var
: vesafb_set_var
,
446 fb_get_cmap
: vesafb_get_cmap
,
447 fb_set_cmap
: vesafb_set_cmap
,
448 fb_pan_display
: vesafb_pan_display
,
451 int vesafb_setup(char *options
)
455 fb_info
.fontname
[0] = '\0';
457 if (!options
|| !*options
)
460 for(this_opt
=strtok(options
,","); this_opt
; this_opt
=strtok(NULL
,",")) {
461 if (!*this_opt
) continue;
463 if (! strcmp(this_opt
, "inverse"))
465 else if (! strcmp(this_opt
, "redraw"))
467 else if (! strcmp(this_opt
, "ypan"))
469 else if (! strcmp(this_opt
, "ywrap"))
471 else if (! strcmp(this_opt
, "vgapal"))
473 else if (! strcmp(this_opt
, "pmipal"))
475 else if (! strcmp(this_opt
, "mtrr"))
477 else if (!strncmp(this_opt
, "font:", 5))
478 strcpy(fb_info
.fontname
, this_opt
+5);
483 static int vesafb_switch(int con
, struct fb_info
*info
)
485 /* Do we have to save the colormap? */
486 if (fb_display
[currcon
].cmap
.len
)
487 fb_get_cmap(&fb_display
[currcon
].cmap
, 1, vesa_getcolreg
,
491 /* Install new colormap */
492 do_install_cmap(con
, info
);
493 vesafb_update_var(con
,info
);
497 /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
499 static void vesafb_blank(int blank
, struct fb_info
*info
)
504 int __init
vesafb_init(void)
508 if (screen_info
.orig_video_isVGA
!= VIDEO_TYPE_VLFB
)
511 video_base
= screen_info
.lfb_base
;
512 video_bpp
= screen_info
.lfb_depth
;
515 video_width
= screen_info
.lfb_width
;
516 video_height
= screen_info
.lfb_height
;
517 video_linelength
= screen_info
.lfb_linelength
;
518 video_size
= screen_info
.lfb_size
* 65536;
519 video_visual
= (video_bpp
== 8) ?
520 FB_VISUAL_PSEUDOCOLOR
: FB_VISUAL_TRUECOLOR
;
522 if (!request_mem_region(video_base
, video_size
, "vesafb")) {
524 "vesafb: abort, cannot reserve video memory at 0x%lx\n",
529 video_vbase
= ioremap(video_base
, video_size
);
531 release_mem_region(video_base
, video_size
);
533 "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
534 video_size
, video_base
);
538 printk(KERN_INFO
"vesafb: framebuffer at 0x%lx, mapped to 0x%p, size %dk\n",
539 video_base
, video_vbase
, video_size
/1024);
540 printk(KERN_INFO
"vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
541 video_width
, video_height
, video_bpp
, video_linelength
, screen_info
.pages
);
543 if (screen_info
.vesapm_seg
) {
544 printk(KERN_INFO
"vesafb: protected mode interface info at %04x:%04x\n",
545 screen_info
.vesapm_seg
,screen_info
.vesapm_off
);
548 if (screen_info
.vesapm_seg
< 0xc000)
549 ypan
= pmi_setpal
= 0; /* not available or some DOS TSR ... */
551 if (ypan
|| pmi_setpal
) {
552 pmi_base
= (unsigned short*)bus_to_virt(((unsigned long)screen_info
.vesapm_seg
<< 4) + screen_info
.vesapm_off
);
553 pmi_start
= (void*)((char*)pmi_base
+ pmi_base
[1]);
554 pmi_pal
= (void*)((char*)pmi_base
+ pmi_base
[2]);
555 printk(KERN_INFO
"vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start
,pmi_pal
);
557 printk(KERN_INFO
"vesafb: pmi: ports = ");
558 for (i
= pmi_base
[3]/2; pmi_base
[i
] != 0xffff; i
++)
559 printk("%x ",pmi_base
[i
]);
561 if (pmi_base
[i
] != 0xffff) {
563 * memory areas not supported (yet?)
565 * Rules are: we have to set up a descriptor for the requested
566 * memory area and pass it in the ES register to the BIOS function.
568 printk(KERN_INFO
"vesafb: can't handle memory requests, pmi disabled\n");
569 ypan
= pmi_setpal
= 0;
574 vesafb_defined
.xres
=video_width
;
575 vesafb_defined
.yres
=video_height
;
576 vesafb_defined
.xres_virtual
=video_width
;
577 vesafb_defined
.yres_virtual
=video_size
/ video_linelength
;
578 vesafb_defined
.bits_per_pixel
=video_bpp
;
580 if (ypan
&& vesafb_defined
.yres_virtual
> video_height
) {
581 printk(KERN_INFO
"vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
582 (ypan
> 1) ? "ywrap" : "ypan",vesafb_defined
.yres_virtual
);
584 printk(KERN_INFO
"vesafb: scrolling: redraw\n");
585 vesafb_defined
.yres_virtual
= video_height
;
588 video_height_virtual
= vesafb_defined
.yres_virtual
;
590 /* some dummy values for timing to make fbset happy */
591 vesafb_defined
.pixclock
= 10000000 / video_width
* 1000 / video_height
;
592 vesafb_defined
.left_margin
= (video_width
/ 8) & 0xf8;
593 vesafb_defined
.right_margin
= 32;
594 vesafb_defined
.upper_margin
= 16;
595 vesafb_defined
.lower_margin
= 4;
596 vesafb_defined
.hsync_len
= (video_width
/ 8) & 0xf8;
597 vesafb_defined
.vsync_len
= 4;
600 vesafb_defined
.red
.offset
= screen_info
.red_pos
;
601 vesafb_defined
.red
.length
= screen_info
.red_size
;
602 vesafb_defined
.green
.offset
= screen_info
.green_pos
;
603 vesafb_defined
.green
.length
= screen_info
.green_size
;
604 vesafb_defined
.blue
.offset
= screen_info
.blue_pos
;
605 vesafb_defined
.blue
.length
= screen_info
.blue_size
;
606 vesafb_defined
.transp
.offset
= screen_info
.rsvd_pos
;
607 vesafb_defined
.transp
.length
= screen_info
.rsvd_size
;
608 printk(KERN_INFO
"vesafb: directcolor: "
609 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
610 screen_info
.rsvd_size
,
611 screen_info
.red_size
,
612 screen_info
.green_size
,
613 screen_info
.blue_size
,
614 screen_info
.rsvd_pos
,
616 screen_info
.green_pos
,
617 screen_info
.blue_pos
);
620 vesafb_defined
.red
.length
= 6;
621 vesafb_defined
.green
.length
= 6;
622 vesafb_defined
.blue
.length
= 6;
623 for(i
= 0; i
< 16; i
++) {
625 palette
[i
].red
= default_red
[j
];
626 palette
[i
].green
= default_grn
[j
];
627 palette
[i
].blue
= default_blu
[j
];
629 video_cmap_len
= 256;
632 /* request failure does not faze us, as vgacon probably has this
633 * region already (FIXME) */
634 request_region(0x3c0, 32, "vesafb");
637 int temp_size
= video_size
;
638 while (mtrr_add(video_base
, temp_size
, MTRR_TYPE_WRCOMB
, 1)==-EINVAL
) {
643 strcpy(fb_info
.modename
, "VESA VGA");
644 fb_info
.changevar
= NULL
;
646 fb_info
.fbops
= &vesafb_ops
;
648 fb_info
.switch_con
=&vesafb_switch
;
649 fb_info
.updatevar
=&vesafb_update_var
;
650 fb_info
.blank
=&vesafb_blank
;
651 fb_info
.flags
=FBINFO_FLAG_DEFAULT
;
654 if (register_framebuffer(&fb_info
)<0)
657 printk(KERN_INFO
"fb%d: %s frame buffer device\n",
658 GET_FB_IDX(fb_info
.node
), fb_info
.modename
);
663 * Overrides for Emacs so that we follow Linus's tabbing style.
664 * ---------------------------------------------------------------------------