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/ioport.h>
21 #include <linux/init.h>
22 #include <linux/platform_device.h>
24 #include <video/vga.h>
28 #define dac_reg (0x3c8)
29 #define dac_val (0x3c9)
31 /* --------------------------------------------------------------------- */
33 static struct fb_var_screeninfo vesafb_defined __initdata = {
34 .activate = FB_ACTIVATE_NOW,
41 .vmode = FB_VMODE_NONINTERLACED,
44 static struct fb_fix_screeninfo vesafb_fix __initdata = {
46 .type = FB_TYPE_PACKED_PIXELS,
47 .accel = FB_ACCEL_NONE,
50 static int inverse = 0;
51 static int mtrr = 3; /* default to write-combining */
52 static int vram_remap __initdata = 0; /* Set amount of memory to be used */
53 static int vram_total __initdata = 0; /* Set total amount of memory */
54 static int pmi_setpal = 0; /* pmi for palette changes ??? */
55 static int ypan = 0; /* 0..nothing, 1..ypan, 2..ywrap */
56 static unsigned short *pmi_base = NULL;
57 static void (*pmi_start)(void);
58 static void (*pmi_pal)(void);
60 static int vga_compat;
62 /* --------------------------------------------------------------------- */
64 static int vesafb_pan_display(struct fb_var_screeninfo *var,
74 if (var->yoffset > var->yres_virtual)
76 if ((ypan==1) && var->yoffset+var->yres > var->yres_virtual)
79 offset = (var->yoffset * info->fix.line_length + var->xoffset) / 4;
83 : /* no return value */
84 : "a" (0x4f07), /* EAX */
86 "c" (offset), /* ECX */
87 "d" (offset >> 16), /* EDX */
88 "D" (&pmi_start)); /* EDI */
93 static int vesafb_blank(int blank, struct fb_info *info)
99 u8 seq = 0, crtc17 = 0;
101 if (blank == FB_BLANK_POWERDOWN) {
108 err = (blank == FB_BLANK_UNBLANK) ? 0 : -EINVAL;
111 vga_wseq(NULL, 0x00, 0x01);
112 seq |= vga_rseq(NULL, 0x01) & ~0x20;
113 vga_wseq(NULL, 0x00, seq);
115 crtc17 |= vga_rcrt(NULL, 0x17) & ~0x80;
117 vga_wcrt(NULL, 0x17, crtc17);
118 vga_wseq(NULL, 0x00, 0x03);
124 static void vesa_setpalette(int regno, unsigned red, unsigned green,
128 struct { u_char blue, green, red, pad; } entry;
129 int shift = 16 - depth;
132 entry.red = red >> shift;
133 entry.green = green >> shift;
134 entry.blue = blue >> shift;
136 __asm__ __volatile__(
138 : /* no return value */
139 : "a" (0x4f09), /* EAX */
142 "d" (regno), /* EDX */
143 "D" (&entry), /* EDI */
144 "S" (&pmi_pal)); /* ESI */
146 /* without protected mode interface, try VGA registers... */
147 outb_p(regno, dac_reg);
148 outb_p(red >> shift, dac_val);
149 outb_p(green >> shift, dac_val);
150 outb_p(blue >> shift, dac_val);
155 static int vesafb_setcolreg(unsigned regno, unsigned red, unsigned green,
156 unsigned blue, unsigned transp,
157 struct fb_info *info)
160 * Set a single color register. The values supplied are
161 * already rounded down to the hardware's capabilities
162 * (according to the entries in the `var' structure). Return
163 * != 0 for invalid regno.
166 if (regno >= info->cmap.len)
169 switch (info->var.bits_per_pixel) {
171 vesa_setpalette(regno,red,green,blue);
174 if (info->var.red.offset == 10) {
176 ((u32*) (info->pseudo_palette))[regno] =
177 ((red & 0xf800) >> 1) |
178 ((green & 0xf800) >> 6) |
179 ((blue & 0xf800) >> 11);
182 ((u32*) (info->pseudo_palette))[regno] =
184 ((green & 0xfc00) >> 5) |
185 ((blue & 0xf800) >> 11);
192 ((u32 *)(info->pseudo_palette))[regno] =
193 (red << info->var.red.offset) |
194 (green << info->var.green.offset) |
195 (blue << info->var.blue.offset);
201 ((u32 *)(info->pseudo_palette))[regno] =
202 (red << info->var.red.offset) |
203 (green << info->var.green.offset) |
204 (blue << info->var.blue.offset);
210 static struct fb_ops vesafb_ops = {
211 .owner = THIS_MODULE,
212 .fb_setcolreg = vesafb_setcolreg,
213 .fb_pan_display = vesafb_pan_display,
214 .fb_blank = vesafb_blank,
215 .fb_fillrect = cfb_fillrect,
216 .fb_copyarea = cfb_copyarea,
217 .fb_imageblit = cfb_imageblit,
218 .fb_cursor = soft_cursor,
221 static int __init vesafb_setup(char *options)
225 if (!options || !*options)
228 while ((this_opt = strsep(&options, ",")) != NULL) {
229 if (!*this_opt) continue;
231 if (! strcmp(this_opt, "inverse"))
233 else if (! strcmp(this_opt, "redraw"))
235 else if (! strcmp(this_opt, "ypan"))
237 else if (! strcmp(this_opt, "ywrap"))
239 else if (! strcmp(this_opt, "vgapal"))
241 else if (! strcmp(this_opt, "pmipal"))
243 else if (! strncmp(this_opt, "mtrr:", 5))
244 mtrr = simple_strtoul(this_opt+5, NULL, 0);
245 else if (! strcmp(this_opt, "nomtrr"))
247 else if (! strncmp(this_opt, "vtotal:", 7))
248 vram_total = simple_strtoul(this_opt+7, NULL, 0);
249 else if (! strncmp(this_opt, "vremap:", 7))
250 vram_remap = simple_strtoul(this_opt+7, NULL, 0);
255 static int __init vesafb_probe(struct device *device)
257 struct platform_device *dev = to_platform_device(device);
258 struct fb_info *info;
260 unsigned int size_vmode;
261 unsigned int size_remap;
262 unsigned int size_total;
264 if (screen_info.orig_video_isVGA != VIDEO_TYPE_VLFB)
267 vesafb_fix.smem_start = screen_info.lfb_base;
268 vesafb_defined.bits_per_pixel = screen_info.lfb_depth;
269 if (15 == vesafb_defined.bits_per_pixel)
270 vesafb_defined.bits_per_pixel = 16;
271 vesafb_defined.xres = screen_info.lfb_width;
272 vesafb_defined.yres = screen_info.lfb_height;
273 vesafb_fix.line_length = screen_info.lfb_linelength;
274 vesafb_fix.visual = (vesafb_defined.bits_per_pixel == 8) ?
275 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
277 /* size_vmode -- that is the amount of memory needed for the
278 * used video mode, i.e. the minimum amount of
280 size_vmode = vesafb_defined.yres * vesafb_fix.line_length;
282 /* size_total -- all video memory we have. Used for mtrr
283 * entries, ressource allocation and bounds
285 size_total = screen_info.lfb_size * 65536;
287 size_total = vram_total * 1024 * 1024;
288 if (size_total < size_vmode)
289 size_total = size_vmode;
291 /* size_remap -- the amount of video memory we are going to
292 * use for vesafb. With modern cards it is no
293 * option to simply use size_total as that
294 * wastes plenty of kernel address space. */
295 size_remap = size_vmode * 2;
297 size_remap = vram_remap * 1024 * 1024;
298 if (size_remap < size_vmode)
299 size_remap = size_vmode;
300 if (size_remap > size_total)
301 size_remap = size_total;
302 vesafb_fix.smem_len = size_remap;
305 screen_info.vesapm_seg = 0;
308 if (!request_mem_region(vesafb_fix.smem_start, size_total, "vesafb")) {
310 "vesafb: cannot reserve video memory at 0x%lx\n",
311 vesafb_fix.smem_start);
312 /* We cannot make this fatal. Sometimes this comes from magic
313 spaces our resource handlers simply don't know about */
316 info = framebuffer_alloc(sizeof(u32) * 256, &dev->dev);
318 release_mem_region(vesafb_fix.smem_start, size_total);
321 info->pseudo_palette = info->par;
324 info->screen_base = ioremap(vesafb_fix.smem_start, vesafb_fix.smem_len);
325 if (!info->screen_base) {
327 "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
328 vesafb_fix.smem_len, vesafb_fix.smem_start);
333 printk(KERN_INFO "vesafb: framebuffer at 0x%lx, mapped to 0x%p, "
334 "using %dk, total %dk\n",
335 vesafb_fix.smem_start, info->screen_base,
336 size_remap/1024, size_total/1024);
337 printk(KERN_INFO "vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
338 vesafb_defined.xres, vesafb_defined.yres, vesafb_defined.bits_per_pixel, vesafb_fix.line_length, screen_info.pages);
340 if (screen_info.vesapm_seg) {
341 printk(KERN_INFO "vesafb: protected mode interface info at %04x:%04x\n",
342 screen_info.vesapm_seg,screen_info.vesapm_off);
345 if (screen_info.vesapm_seg < 0xc000)
346 ypan = pmi_setpal = 0; /* not available or some DOS TSR ... */
348 if (ypan || pmi_setpal) {
349 pmi_base = (unsigned short*)phys_to_virt(((unsigned long)screen_info.vesapm_seg << 4) + screen_info.vesapm_off);
350 pmi_start = (void*)((char*)pmi_base + pmi_base[1]);
351 pmi_pal = (void*)((char*)pmi_base + pmi_base[2]);
352 printk(KERN_INFO "vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start,pmi_pal);
354 printk(KERN_INFO "vesafb: pmi: ports = ");
355 for (i = pmi_base[3]/2; pmi_base[i] != 0xffff; i++)
356 printk("%x ",pmi_base[i]);
358 if (pmi_base[i] != 0xffff) {
360 * memory areas not supported (yet?)
362 * Rules are: we have to set up a descriptor for the requested
363 * memory area and pass it in the ES register to the BIOS function.
365 printk(KERN_INFO "vesafb: can't handle memory requests, pmi disabled\n");
366 ypan = pmi_setpal = 0;
371 vesafb_defined.xres_virtual = vesafb_defined.xres;
372 vesafb_defined.yres_virtual = vesafb_fix.smem_len / vesafb_fix.line_length;
373 if (ypan && vesafb_defined.yres_virtual > vesafb_defined.yres) {
374 printk(KERN_INFO "vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
375 (ypan > 1) ? "ywrap" : "ypan",vesafb_defined.yres_virtual);
377 printk(KERN_INFO "vesafb: scrolling: redraw\n");
378 vesafb_defined.yres_virtual = vesafb_defined.yres;
382 /* some dummy values for timing to make fbset happy */
383 vesafb_defined.pixclock = 10000000 / vesafb_defined.xres * 1000 / vesafb_defined.yres;
384 vesafb_defined.left_margin = (vesafb_defined.xres / 8) & 0xf8;
385 vesafb_defined.hsync_len = (vesafb_defined.xres / 8) & 0xf8;
387 vesafb_defined.red.offset = screen_info.red_pos;
388 vesafb_defined.red.length = screen_info.red_size;
389 vesafb_defined.green.offset = screen_info.green_pos;
390 vesafb_defined.green.length = screen_info.green_size;
391 vesafb_defined.blue.offset = screen_info.blue_pos;
392 vesafb_defined.blue.length = screen_info.blue_size;
393 vesafb_defined.transp.offset = screen_info.rsvd_pos;
394 vesafb_defined.transp.length = screen_info.rsvd_size;
396 if (vesafb_defined.bits_per_pixel <= 8) {
397 depth = vesafb_defined.green.length;
398 vesafb_defined.red.length =
399 vesafb_defined.green.length =
400 vesafb_defined.blue.length =
401 vesafb_defined.bits_per_pixel;
404 printk(KERN_INFO "vesafb: %s: "
405 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
406 (vesafb_defined.bits_per_pixel > 8) ?
407 "Truecolor" : "Pseudocolor",
408 screen_info.rsvd_size,
409 screen_info.red_size,
410 screen_info.green_size,
411 screen_info.blue_size,
412 screen_info.rsvd_pos,
414 screen_info.green_pos,
415 screen_info.blue_pos);
417 vesafb_fix.ypanstep = ypan ? 1 : 0;
418 vesafb_fix.ywrapstep = (ypan>1) ? 1 : 0;
420 /* request failure does not faze us, as vgacon probably has this
421 * region already (FIXME) */
422 request_region(0x3c0, 32, "vesafb");
425 unsigned int temp_size = size_total;
426 unsigned int type = 0;
430 type = MTRR_TYPE_UNCACHABLE;
433 type = MTRR_TYPE_WRBACK;
436 type = MTRR_TYPE_WRCOMB;
439 type = MTRR_TYPE_WRTHROUGH;
449 /* Find the largest power-of-two */
450 while (temp_size & (temp_size - 1))
451 temp_size &= (temp_size - 1);
453 /* Try and find a power of two to add */
455 rc = mtrr_add(vesafb_fix.smem_start, temp_size,
458 } while (temp_size >= PAGE_SIZE && rc == -EINVAL);
462 info->fbops = &vesafb_ops;
463 info->var = vesafb_defined;
464 info->fix = vesafb_fix;
465 info->flags = FBINFO_FLAG_DEFAULT |
466 (ypan) ? FBINFO_HWACCEL_YPAN : 0;
468 vga_compat = (screen_info.capabilities & 2) ? 0 : 1;
469 printk("vesafb: Mode is %sVGA compatible\n",
470 (vga_compat) ? "" : "not ");
472 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
476 if (register_framebuffer(info)<0) {
478 fb_dealloc_cmap(&info->cmap);
481 printk(KERN_INFO "fb%d: %s frame buffer device\n",
482 info->node, info->fix.id);
485 framebuffer_release(info);
486 release_mem_region(vesafb_fix.smem_start, size_total);
490 static struct device_driver vesafb_driver = {
492 .bus = &platform_bus_type,
493 .probe = vesafb_probe,
496 static struct platform_device vesafb_device = {
500 static int __init vesafb_init(void)
505 /* ignore error return of fb_get_options */
506 fb_get_options("vesafb", &option);
507 vesafb_setup(option);
508 ret = driver_register(&vesafb_driver);
511 ret = platform_device_register(&vesafb_device);
513 driver_unregister(&vesafb_driver);
517 module_init(vesafb_init);
519 MODULE_LICENSE("GPL");