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/boot/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/slab.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 #endif /* CONFIG_MTRR */
30 #include <video/fbcon.h>
31 #include <video/fbcon-cfb8.h>
32 #include <video/fbcon-cfb16.h>
33 #include <video/fbcon-cfb24.h>
34 #include <video/fbcon-cfb32.h>
35 #include <video/fbcon-mac.h>
37 #define dac_reg (0x3c8)
38 #define dac_val (0x3c9)
40 /* --------------------------------------------------------------------- */
47 unsigned long video_base; /* physical addr */
49 char *video_vbase; /* mapped */
53 static int video_width;
54 static int video_height;
55 static int video_height_virtual;
56 static int video_type = FB_TYPE_PACKED_PIXELS;
57 static int video_visual;
58 static int video_linelength;
59 static int video_cmap_len;
61 /* --------------------------------------------------------------------- */
63 static struct fb_var_screeninfo vesafb_defined = {
64 0,0,0,0, /* W,H, W, H (virtual) load xres,xres_virtual*/
65 0,0, /* virtual -> visible no offset */
66 8, /* depth -> load bits_per_pixel */
71 {0,0,0}, /* transparency */
72 0, /* standard pixel format */
77 0L,0L,0, /* No sync info */
78 FB_VMODE_NONINTERLACED,
82 static struct display disp;
83 static struct fb_info fb_info;
84 static struct { u_short blue, green, red, pad; } palette[256];
86 #ifdef FBCON_HAS_CFB16
89 #ifdef FBCON_HAS_CFB24
92 #ifdef FBCON_HAS_CFB32
97 static int inverse = 0;
99 static int currcon = 0;
101 static int pmi_setpal = 0; /* pmi for palette changes ??? */
102 static int ypan = 0; /* 0..nothing, 1..ypan, 2..ywrap */
103 static unsigned short *pmi_base = 0;
104 static void (*pmi_start)(void);
105 static void (*pmi_pal)(void);
107 static struct display_switch vesafb_sw;
109 /* --------------------------------------------------------------------- */
111 static int vesafb_pan_display(struct fb_var_screeninfo *var, int con,
112 struct fb_info *info)
114 #if defined(__i386__) || defined(__x86_64__)
121 if (var->yoffset > var->yres_virtual)
123 if ((ypan==1) && var->yoffset+var->yres > var->yres_virtual)
126 offset = (var->yoffset * video_linelength + var->xoffset) / 4;
128 __asm__ __volatile__(
130 : /* no return value */
131 : "a" (0x4f07), /* EAX */
133 "c" (offset), /* ECX */
134 "d" (offset >> 16), /* EDX */
135 "D" (&pmi_start)); /* EDI */
140 static int vesafb_update_var(int con, struct fb_info *info)
142 if (con == currcon && ypan) {
143 struct fb_var_screeninfo *var = &fb_display[currcon].var;
144 return vesafb_pan_display(var,con,info);
149 static int vesafb_get_fix(struct fb_fix_screeninfo *fix, int con,
150 struct fb_info *info)
152 memset(fix, 0, sizeof(struct fb_fix_screeninfo));
153 strcpy(fix->id,"VESA VGA");
155 fix->smem_start=video_base;
156 fix->smem_len=video_size;
157 fix->type = video_type;
158 fix->visual = video_visual;
160 fix->ypanstep = ypan ? 1 : 0;
161 fix->ywrapstep = (ypan>1) ? 1 : 0;
162 fix->line_length=video_linelength;
166 static int vesafb_get_var(struct fb_var_screeninfo *var, int con,
167 struct fb_info *info)
170 memcpy(var, &vesafb_defined, sizeof(struct fb_var_screeninfo));
172 *var=fb_display[con].var;
176 static void vesafb_set_disp(int con)
178 struct fb_fix_screeninfo fix;
179 struct display *display;
180 struct display_switch *sw;
183 display = &fb_display[con];
185 display = &disp; /* used during initialization */
187 vesafb_get_fix(&fix, con, 0);
189 memset(display, 0, sizeof(struct display));
190 display->screen_base = video_vbase;
191 display->visual = fix.visual;
192 display->type = fix.type;
193 display->type_aux = fix.type_aux;
194 display->ypanstep = fix.ypanstep;
195 display->ywrapstep = fix.ywrapstep;
196 display->line_length = fix.line_length;
197 display->next_line = fix.line_length;
198 display->can_soft_blank = 0;
199 display->inverse = inverse;
200 vesafb_get_var(&display->var, -1, &fb_info);
203 #ifdef FBCON_HAS_CFB8
208 #ifdef FBCON_HAS_CFB16
212 display->dispsw_data = fbcon_cmap.cfb16;
215 #ifdef FBCON_HAS_CFB24
218 display->dispsw_data = fbcon_cmap.cfb24;
221 #ifdef FBCON_HAS_CFB32
224 display->dispsw_data = fbcon_cmap.cfb32;
236 memcpy(&vesafb_sw, sw, sizeof(*sw));
237 display->dispsw = &vesafb_sw;
239 display->scrollmode = SCROLL_YREDRAW;
240 vesafb_sw.bmove = fbcon_redraw_bmove;
244 static int vesafb_set_var(struct fb_var_screeninfo *var, int con,
245 struct fb_info *info)
247 static int first = 1;
249 if (var->xres != vesafb_defined.xres ||
250 var->yres != vesafb_defined.yres ||
251 var->xres_virtual != vesafb_defined.xres_virtual ||
252 var->yres_virtual > video_height_virtual ||
253 var->yres_virtual < video_height ||
255 var->bits_per_pixel != vesafb_defined.bits_per_pixel ||
258 printk(KERN_ERR "Vesafb does not support changing the video mode\n");
264 if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_TEST)
268 if (vesafb_defined.yres_virtual != var->yres_virtual) {
269 vesafb_defined.yres_virtual = var->yres_virtual;
271 fb_display[con].var = vesafb_defined;
272 info->changevar(con);
276 if (var->yoffset != vesafb_defined.yoffset)
277 return vesafb_pan_display(var,con,info);
286 static int vesa_getcolreg(unsigned regno, unsigned *red, unsigned *green,
287 unsigned *blue, unsigned *transp,
288 struct fb_info *fb_info)
291 * Read a single color register and split it into colors/transparent.
292 * Return != 0 for invalid regno.
295 if (regno >= video_cmap_len)
298 *red = palette[regno].red;
299 *green = palette[regno].green;
300 *blue = palette[regno].blue;
305 #ifdef FBCON_HAS_CFB8
307 static void vesa_setpalette(int regno, unsigned red, unsigned green, unsigned blue)
310 struct { u_char blue, green, red, pad; } entry;
312 #if defined(__i386__) || defined(__x86_64__)
314 entry.red = red >> 10;
315 entry.green = green >> 10;
316 entry.blue = blue >> 10;
319 __asm__ __volatile__(
321 : /* no return value */
322 : "a" (0x4f09), /* EAX */
325 "d" (regno), /* EDX */
326 "D" (&entry), /* EDI */
327 "S" (&pmi_pal)); /* ESI */
329 #endif /* __i386__ || __x86_64__ */
331 /* without protected mode interface, try VGA registers... */
332 outb_p(regno, dac_reg);
333 outb_p(red >> 10, dac_val);
334 outb_p(green >> 10, dac_val);
335 outb_p(blue >> 10, dac_val);
343 static int vesa_setcolreg(unsigned regno, unsigned red, unsigned green,
344 unsigned blue, unsigned transp,
345 struct fb_info *fb_info)
348 * Set a single color register. The values supplied are
349 * already rounded down to the hardware's capabilities
350 * (according to the entries in the `var' structure). Return
351 * != 0 for invalid regno.
354 if (regno >= video_cmap_len)
357 palette[regno].red = red;
358 palette[regno].green = green;
359 palette[regno].blue = blue;
362 #ifdef FBCON_HAS_CFB8
364 vesa_setpalette(regno,red,green,blue);
367 #ifdef FBCON_HAS_CFB16
370 if (vesafb_defined.red.offset == 10) {
372 fbcon_cmap.cfb16[regno] =
373 ((red & 0xf800) >> 1) |
374 ((green & 0xf800) >> 6) |
375 ((blue & 0xf800) >> 11);
378 fbcon_cmap.cfb16[regno] =
380 ((green & 0xfc00) >> 5) |
381 ((blue & 0xf800) >> 11);
385 #ifdef FBCON_HAS_CFB24
390 fbcon_cmap.cfb24[regno] =
391 (red << vesafb_defined.red.offset) |
392 (green << vesafb_defined.green.offset) |
393 (blue << vesafb_defined.blue.offset);
396 #ifdef FBCON_HAS_CFB32
401 fbcon_cmap.cfb32[regno] =
402 (red << vesafb_defined.red.offset) |
403 (green << vesafb_defined.green.offset) |
404 (blue << vesafb_defined.blue.offset);
411 static void do_install_cmap(int con, struct fb_info *info)
415 if (fb_display[con].cmap.len)
416 fb_set_cmap(&fb_display[con].cmap, 1, vesa_setcolreg, info);
418 fb_set_cmap(fb_default_cmap(video_cmap_len), 1, vesa_setcolreg,
422 static int vesafb_get_cmap(struct fb_cmap *cmap, int kspc, int con,
423 struct fb_info *info)
425 if (con == currcon) /* current console? */
426 return fb_get_cmap(cmap, kspc, vesa_getcolreg, info);
427 else if (fb_display[con].cmap.len) /* non default colormap? */
428 fb_copy_cmap(&fb_display[con].cmap, cmap, kspc ? 0 : 2);
430 fb_copy_cmap(fb_default_cmap(video_cmap_len),
435 static int vesafb_set_cmap(struct fb_cmap *cmap, int kspc, int con,
436 struct fb_info *info)
440 if (!fb_display[con].cmap.len) { /* no colormap allocated? */
441 err = fb_alloc_cmap(&fb_display[con].cmap,video_cmap_len,0);
445 if (con == currcon) /* current console? */
446 return fb_set_cmap(cmap, kspc, vesa_setcolreg, info);
448 fb_copy_cmap(cmap, &fb_display[con].cmap, kspc ? 0 : 1);
452 static struct fb_ops vesafb_ops = {
454 fb_get_fix: vesafb_get_fix,
455 fb_get_var: vesafb_get_var,
456 fb_set_var: vesafb_set_var,
457 fb_get_cmap: vesafb_get_cmap,
458 fb_set_cmap: vesafb_set_cmap,
459 fb_pan_display: vesafb_pan_display,
462 int __init vesafb_setup(char *options)
466 fb_info.fontname[0] = '\0';
468 if (!options || !*options)
471 while ((this_opt = strsep(&options, ",")) != NULL) {
472 if (!*this_opt) continue;
474 if (! strcmp(this_opt, "inverse"))
476 else if (! strcmp(this_opt, "redraw"))
478 #if defined(__i386__) || defined(__x86_64__)
479 else if (! strcmp(this_opt, "ypan"))
481 else if (! strcmp(this_opt, "ywrap"))
483 else if (! strcmp(this_opt, "vgapal"))
485 else if (! strcmp(this_opt, "pmipal"))
487 #endif /* __i386__ || __x64-64__ */
489 else if (! strcmp(this_opt, "mtrr"))
492 else if (!strncmp(this_opt, "font:", 5))
493 strcpy(fb_info.fontname, this_opt+5);
498 static int vesafb_switch(int con, struct fb_info *info)
500 /* Do we have to save the colormap? */
501 if (fb_display[currcon].cmap.len)
502 fb_get_cmap(&fb_display[currcon].cmap, 1, vesa_getcolreg,
506 /* Install new colormap */
507 do_install_cmap(con, info);
508 vesafb_update_var(con,info);
512 /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
514 static void vesafb_blank(int blank, struct fb_info *info)
519 int __init vesafb_init(void)
523 if (screen_info.orig_video_isVGA != VIDEO_TYPE_VLFB)
526 video_base = screen_info.lfb_base;
527 video_bpp = screen_info.lfb_depth;
530 video_width = screen_info.lfb_width;
531 video_height = screen_info.lfb_height;
532 video_linelength = screen_info.lfb_linelength;
533 video_size = screen_info.lfb_size * 65536;
534 video_visual = (video_bpp == 8) ?
535 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
538 screen_info.vesapm_seg = 0;
541 if (!request_mem_region(video_base, video_size, "vesafb")) {
543 "vesafb: abort, cannot reserve video memory at 0x%lx\n",
545 /* We cannot make this fatal. Sometimes this comes from magic
546 spaces our resource handlers simply don't know about */
549 video_vbase = ioremap(video_base, video_size);
551 release_mem_region(video_base, video_size);
553 "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
554 video_size, video_base);
558 printk(KERN_INFO "vesafb: framebuffer at 0x%lx, mapped to 0x%p, size %dk\n",
559 video_base, video_vbase, video_size/1024);
560 printk(KERN_INFO "vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
561 video_width, video_height, video_bpp, video_linelength, screen_info.pages);
563 vesafb_defined.xres=video_width;
564 vesafb_defined.yres=video_height;
565 vesafb_defined.xres_virtual=video_width;
566 vesafb_defined.yres_virtual=video_size / video_linelength;
567 vesafb_defined.bits_per_pixel=video_bpp;
569 #if defined(__i386__) || defined(__x86_64__)
570 if (screen_info.vesapm_seg) {
571 printk(KERN_INFO "vesafb: protected mode interface info at %04x:%04x\n",
572 screen_info.vesapm_seg,screen_info.vesapm_off);
575 if (screen_info.vesapm_seg < 0xc000)
576 ypan = pmi_setpal = 0; /* not available or some DOS TSR ... */
578 if (ypan || pmi_setpal) {
579 pmi_base = (unsigned short*)bus_to_virt(((unsigned long)screen_info.vesapm_seg << 4) + screen_info.vesapm_off);
580 pmi_start = (void*)((char*)pmi_base + pmi_base[1]);
581 pmi_pal = (void*)((char*)pmi_base + pmi_base[2]);
582 printk(KERN_INFO "vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start,pmi_pal);
584 printk(KERN_INFO "vesafb: pmi: ports = ");
585 for (i = pmi_base[3]/2; pmi_base[i] != 0xffff; i++)
586 printk("%x ",pmi_base[i]);
588 if (pmi_base[i] != 0xffff) {
590 * memory areas not supported (yet?)
592 * Rules are: we have to set up a descriptor for the requested
593 * memory area and pass it in the ES register to the BIOS function.
595 printk(KERN_INFO "vesafb: can't handle memory requests, pmi disabled\n");
596 ypan = pmi_setpal = 0;
601 if (ypan && vesafb_defined.yres_virtual > video_height) {
602 printk(KERN_INFO "vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
603 (ypan > 1) ? "ywrap" : "ypan",vesafb_defined.yres_virtual);
605 #endif /* __i386__ || __x86-64__ */
607 printk(KERN_INFO "vesafb: scrolling: redraw\n");
608 vesafb_defined.yres_virtual = video_height;
611 video_height_virtual = vesafb_defined.yres_virtual;
613 /* some dummy values for timing to make fbset happy */
614 vesafb_defined.pixclock = 10000000 / video_width * 1000 / video_height;
615 vesafb_defined.left_margin = (video_width / 8) & 0xf8;
616 vesafb_defined.right_margin = 32;
617 vesafb_defined.upper_margin = 16;
618 vesafb_defined.lower_margin = 4;
619 vesafb_defined.hsync_len = (video_width / 8) & 0xf8;
620 vesafb_defined.vsync_len = 4;
623 vesafb_defined.red.offset = screen_info.red_pos;
624 vesafb_defined.red.length = screen_info.red_size;
625 vesafb_defined.green.offset = screen_info.green_pos;
626 vesafb_defined.green.length = screen_info.green_size;
627 vesafb_defined.blue.offset = screen_info.blue_pos;
628 vesafb_defined.blue.length = screen_info.blue_size;
629 vesafb_defined.transp.offset = screen_info.rsvd_pos;
630 vesafb_defined.transp.length = screen_info.rsvd_size;
631 printk(KERN_INFO "vesafb: directcolor: "
632 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
633 screen_info.rsvd_size,
634 screen_info.red_size,
635 screen_info.green_size,
636 screen_info.blue_size,
637 screen_info.rsvd_pos,
639 screen_info.green_pos,
640 screen_info.blue_pos);
643 vesafb_defined.red.length = 6;
644 vesafb_defined.green.length = 6;
645 vesafb_defined.blue.length = 6;
646 for(i = 0; i < 16; i++) {
648 palette[i].red = default_red[j];
649 palette[i].green = default_grn[j];
650 palette[i].blue = default_blu[j];
652 video_cmap_len = 256;
655 /* request failure does not faze us, as vgacon probably has this
656 * region already (FIXME) */
657 request_region(0x3c0, 32, "vesafb");
661 int temp_size = video_size;
662 /* Find the largest power-of-two */
663 while (temp_size & (temp_size - 1))
664 temp_size &= (temp_size - 1);
666 /* Try and find a power of two to add */
667 while (temp_size && mtrr_add(video_base, temp_size, MTRR_TYPE_WRCOMB, 1)==-EINVAL) {
671 #endif /* CONFIG_MTRR */
673 strcpy(fb_info.modename, "VESA VGA");
674 fb_info.changevar = NULL;
676 fb_info.fbops = &vesafb_ops;
678 fb_info.switch_con=&vesafb_switch;
679 fb_info.updatevar=&vesafb_update_var;
680 fb_info.blank=&vesafb_blank;
681 fb_info.flags=FBINFO_FLAG_DEFAULT;
684 if (register_framebuffer(&fb_info)<0)
687 printk(KERN_INFO "fb%d: %s frame buffer device\n",
688 GET_FB_IDX(fb_info.node), fb_info.modename);
693 * Overrides for Emacs so that we follow Linus's tabbing style.
694 * ---------------------------------------------------------------------------
700 MODULE_LICENSE("GPL");