blob: 2fbaa8484f8a3384402dcc9d081ba73a419ed936 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * framebuffer driver for VBE 2.0 compliant graphic boards
3 *
4 * switching to graphics mode happens at boot time (while
5 * running in real mode, see arch/i386/boot/video.S).
6 *
7 * (c) 1998 Gerd Knorr <kraxel@goldbach.in-berlin.de>
8 *
9 */
10
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/errno.h>
14#include <linux/string.h>
15#include <linux/mm.h>
16#include <linux/tty.h>
17#include <linux/slab.h>
18#include <linux/delay.h>
19#include <linux/fb.h>
20#include <linux/ioport.h>
21#include <linux/init.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010022#include <linux/platform_device.h>
23
Antonino A. Daplasd2d58382005-09-09 13:04:31 -070024#include <video/vga.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/io.h>
26#include <asm/mtrr.h>
27
28#define dac_reg (0x3c8)
29#define dac_val (0x3c9)
30
31/* --------------------------------------------------------------------- */
32
33static struct fb_var_screeninfo vesafb_defined __initdata = {
34 .activate = FB_ACTIVATE_NOW,
35 .height = -1,
36 .width = -1,
37 .right_margin = 32,
38 .upper_margin = 16,
39 .lower_margin = 4,
40 .vsync_len = 4,
41 .vmode = FB_VMODE_NONINTERLACED,
42};
43
44static struct fb_fix_screeninfo vesafb_fix __initdata = {
45 .id = "VESA VGA",
46 .type = FB_TYPE_PACKED_PIXELS,
47 .accel = FB_ACCEL_NONE,
48};
49
50static int inverse = 0;
Antonino A. Daplas8a0934f2005-11-07 01:00:49 -080051static int mtrr = 0; /* disable mtrr */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static int vram_remap __initdata = 0; /* Set amount of memory to be used */
53static int vram_total __initdata = 0; /* Set total amount of memory */
54static int pmi_setpal = 0; /* pmi for palette changes ??? */
55static int ypan = 0; /* 0..nothing, 1..ypan, 2..ywrap */
56static unsigned short *pmi_base = NULL;
57static void (*pmi_start)(void);
58static void (*pmi_pal)(void);
59static int depth;
Antonino A. Daplas89ec4c22006-04-10 22:55:48 -070060static int vga_compat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* --------------------------------------------------------------------- */
62
63static int vesafb_pan_display(struct fb_var_screeninfo *var,
64 struct fb_info *info)
65{
66#ifdef __i386__
67 int offset;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 offset = (var->yoffset * info->fix.line_length + var->xoffset) / 4;
70
71 __asm__ __volatile__(
72 "call *(%%edi)"
73 : /* no return value */
74 : "a" (0x4f07), /* EAX */
75 "b" (0), /* EBX */
76 "c" (offset), /* ECX */
77 "d" (offset >> 16), /* EDX */
78 "D" (&pmi_start)); /* EDI */
79#endif
80 return 0;
81}
82
83static void vesa_setpalette(int regno, unsigned red, unsigned green,
84 unsigned blue)
85{
Antonino A. Daplas89ec4c22006-04-10 22:55:48 -070086 int shift = 16 - depth;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#ifdef __i386__
89 struct { u_char blue, green, red, pad; } entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 if (pmi_setpal) {
92 entry.red = red >> shift;
93 entry.green = green >> shift;
94 entry.blue = blue >> shift;
95 entry.pad = 0;
96 __asm__ __volatile__(
97 "call *(%%esi)"
98 : /* no return value */
99 : "a" (0x4f09), /* EAX */
100 "b" (0), /* EBX */
101 "c" (1), /* ECX */
102 "d" (regno), /* EDX */
103 "D" (&entry), /* EDI */
104 "S" (&pmi_pal)); /* ESI */
Antonino A. Daplas89ec4c22006-04-10 22:55:48 -0700105 return;
106 }
107#endif
108
109/*
110 * without protected mode interface and if VGA compatible,
111 * try VGA registers...
112 */
113 if (vga_compat) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 outb_p(regno, dac_reg);
115 outb_p(red >> shift, dac_val);
116 outb_p(green >> shift, dac_val);
117 outb_p(blue >> shift, dac_val);
118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121static int vesafb_setcolreg(unsigned regno, unsigned red, unsigned green,
122 unsigned blue, unsigned transp,
123 struct fb_info *info)
124{
125 /*
126 * Set a single color register. The values supplied are
127 * already rounded down to the hardware's capabilities
128 * (according to the entries in the `var' structure). Return
129 * != 0 for invalid regno.
130 */
131
132 if (regno >= info->cmap.len)
133 return 1;
134
Antonino A. Daplaseba50852005-11-07 01:00:40 -0800135 if (info->var.bits_per_pixel == 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 vesa_setpalette(regno,red,green,blue);
Antonino A. Daplaseba50852005-11-07 01:00:40 -0800137 else if (regno < 16) {
138 switch (info->var.bits_per_pixel) {
139 case 16:
140 if (info->var.red.offset == 10) {
141 /* 1:5:5:5 */
142 ((u32*) (info->pseudo_palette))[regno] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 ((red & 0xf800) >> 1) |
144 ((green & 0xf800) >> 6) |
145 ((blue & 0xf800) >> 11);
Antonino A. Daplaseba50852005-11-07 01:00:40 -0800146 } else {
147 /* 0:5:6:5 */
148 ((u32*) (info->pseudo_palette))[regno] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 ((red & 0xf800) ) |
150 ((green & 0xfc00) >> 5) |
151 ((blue & 0xf800) >> 11);
Antonino A. Daplaseba50852005-11-07 01:00:40 -0800152 }
153 break;
154 case 24:
155 case 32:
156 red >>= 8;
157 green >>= 8;
158 blue >>= 8;
159 ((u32 *)(info->pseudo_palette))[regno] =
160 (red << info->var.red.offset) |
161 (green << info->var.green.offset) |
162 (blue << info->var.blue.offset);
163 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
Antonino A. Daplaseba50852005-11-07 01:00:40 -0800165 }
166
167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170static struct fb_ops vesafb_ops = {
171 .owner = THIS_MODULE,
172 .fb_setcolreg = vesafb_setcolreg,
173 .fb_pan_display = vesafb_pan_display,
174 .fb_fillrect = cfb_fillrect,
175 .fb_copyarea = cfb_copyarea,
176 .fb_imageblit = cfb_imageblit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
179static int __init vesafb_setup(char *options)
180{
181 char *this_opt;
182
183 if (!options || !*options)
184 return 0;
185
186 while ((this_opt = strsep(&options, ",")) != NULL) {
187 if (!*this_opt) continue;
188
189 if (! strcmp(this_opt, "inverse"))
190 inverse=1;
191 else if (! strcmp(this_opt, "redraw"))
192 ypan=0;
193 else if (! strcmp(this_opt, "ypan"))
194 ypan=1;
195 else if (! strcmp(this_opt, "ywrap"))
196 ypan=2;
197 else if (! strcmp(this_opt, "vgapal"))
198 pmi_setpal=0;
199 else if (! strcmp(this_opt, "pmipal"))
200 pmi_setpal=1;
Antonino A. Daplas80625942005-07-29 14:03:31 -0700201 else if (! strncmp(this_opt, "mtrr:", 5))
202 mtrr = simple_strtoul(this_opt+5, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 else if (! strcmp(this_opt, "nomtrr"))
204 mtrr=0;
205 else if (! strncmp(this_opt, "vtotal:", 7))
206 vram_total = simple_strtoul(this_opt+7, NULL, 0);
207 else if (! strncmp(this_opt, "vremap:", 7))
208 vram_remap = simple_strtoul(this_opt+7, NULL, 0);
209 }
210 return 0;
211}
212
Russell King3ae5eae2005-11-09 22:32:44 +0000213static int __init vesafb_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 struct fb_info *info;
216 int i, err;
217 unsigned int size_vmode;
218 unsigned int size_remap;
219 unsigned int size_total;
220
221 if (screen_info.orig_video_isVGA != VIDEO_TYPE_VLFB)
222 return -ENODEV;
223
Antonino A. Daplas89ec4c22006-04-10 22:55:48 -0700224 vga_compat = (screen_info.capabilities & 2) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 vesafb_fix.smem_start = screen_info.lfb_base;
226 vesafb_defined.bits_per_pixel = screen_info.lfb_depth;
227 if (15 == vesafb_defined.bits_per_pixel)
228 vesafb_defined.bits_per_pixel = 16;
229 vesafb_defined.xres = screen_info.lfb_width;
230 vesafb_defined.yres = screen_info.lfb_height;
231 vesafb_fix.line_length = screen_info.lfb_linelength;
232 vesafb_fix.visual = (vesafb_defined.bits_per_pixel == 8) ?
233 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
234
235 /* size_vmode -- that is the amount of memory needed for the
236 * used video mode, i.e. the minimum amount of
237 * memory we need. */
238 size_vmode = vesafb_defined.yres * vesafb_fix.line_length;
239
240 /* size_total -- all video memory we have. Used for mtrr
241 * entries, ressource allocation and bounds
242 * checking. */
243 size_total = screen_info.lfb_size * 65536;
244 if (vram_total)
245 size_total = vram_total * 1024 * 1024;
246 if (size_total < size_vmode)
247 size_total = size_vmode;
248
249 /* size_remap -- the amount of video memory we are going to
250 * use for vesafb. With modern cards it is no
251 * option to simply use size_total as that
252 * wastes plenty of kernel address space. */
253 size_remap = size_vmode * 2;
254 if (vram_remap)
255 size_remap = vram_remap * 1024 * 1024;
256 if (size_remap < size_vmode)
257 size_remap = size_vmode;
258 if (size_remap > size_total)
259 size_remap = size_total;
260 vesafb_fix.smem_len = size_remap;
261
262#ifndef __i386__
263 screen_info.vesapm_seg = 0;
264#endif
265
266 if (!request_mem_region(vesafb_fix.smem_start, size_total, "vesafb")) {
267 printk(KERN_WARNING
Gerd Knorr78c03712005-06-21 17:16:56 -0700268 "vesafb: cannot reserve video memory at 0x%lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 vesafb_fix.smem_start);
270 /* We cannot make this fatal. Sometimes this comes from magic
271 spaces our resource handlers simply don't know about */
272 }
273
274 info = framebuffer_alloc(sizeof(u32) * 256, &dev->dev);
275 if (!info) {
Gerd Knorr78c03712005-06-21 17:16:56 -0700276 release_mem_region(vesafb_fix.smem_start, size_total);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return -ENOMEM;
278 }
279 info->pseudo_palette = info->par;
280 info->par = NULL;
281
Gerd Knorr78c03712005-06-21 17:16:56 -0700282 info->screen_base = ioremap(vesafb_fix.smem_start, vesafb_fix.smem_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (!info->screen_base) {
284 printk(KERN_ERR
285 "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
286 vesafb_fix.smem_len, vesafb_fix.smem_start);
287 err = -EIO;
288 goto err;
289 }
290
291 printk(KERN_INFO "vesafb: framebuffer at 0x%lx, mapped to 0x%p, "
292 "using %dk, total %dk\n",
293 vesafb_fix.smem_start, info->screen_base,
294 size_remap/1024, size_total/1024);
295 printk(KERN_INFO "vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
296 vesafb_defined.xres, vesafb_defined.yres, vesafb_defined.bits_per_pixel, vesafb_fix.line_length, screen_info.pages);
297
298 if (screen_info.vesapm_seg) {
299 printk(KERN_INFO "vesafb: protected mode interface info at %04x:%04x\n",
300 screen_info.vesapm_seg,screen_info.vesapm_off);
301 }
302
303 if (screen_info.vesapm_seg < 0xc000)
304 ypan = pmi_setpal = 0; /* not available or some DOS TSR ... */
305
306 if (ypan || pmi_setpal) {
307 pmi_base = (unsigned short*)phys_to_virt(((unsigned long)screen_info.vesapm_seg << 4) + screen_info.vesapm_off);
308 pmi_start = (void*)((char*)pmi_base + pmi_base[1]);
309 pmi_pal = (void*)((char*)pmi_base + pmi_base[2]);
310 printk(KERN_INFO "vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start,pmi_pal);
311 if (pmi_base[3]) {
312 printk(KERN_INFO "vesafb: pmi: ports = ");
313 for (i = pmi_base[3]/2; pmi_base[i] != 0xffff; i++)
314 printk("%x ",pmi_base[i]);
315 printk("\n");
316 if (pmi_base[i] != 0xffff) {
317 /*
318 * memory areas not supported (yet?)
319 *
320 * Rules are: we have to set up a descriptor for the requested
321 * memory area and pass it in the ES register to the BIOS function.
322 */
323 printk(KERN_INFO "vesafb: can't handle memory requests, pmi disabled\n");
324 ypan = pmi_setpal = 0;
325 }
326 }
327 }
328
Antonino A. Daplas89ec4c22006-04-10 22:55:48 -0700329 if (vesafb_defined.bits_per_pixel == 8 && !pmi_setpal && !vga_compat) {
330 printk(KERN_WARNING "vesafb: hardware palette is unchangeable,\n"
331 " colors may be incorrect\n");
332 vesafb_fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
333 }
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 vesafb_defined.xres_virtual = vesafb_defined.xres;
336 vesafb_defined.yres_virtual = vesafb_fix.smem_len / vesafb_fix.line_length;
337 if (ypan && vesafb_defined.yres_virtual > vesafb_defined.yres) {
338 printk(KERN_INFO "vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
339 (ypan > 1) ? "ywrap" : "ypan",vesafb_defined.yres_virtual);
340 } else {
341 printk(KERN_INFO "vesafb: scrolling: redraw\n");
342 vesafb_defined.yres_virtual = vesafb_defined.yres;
343 ypan = 0;
344 }
345
346 /* some dummy values for timing to make fbset happy */
347 vesafb_defined.pixclock = 10000000 / vesafb_defined.xres * 1000 / vesafb_defined.yres;
348 vesafb_defined.left_margin = (vesafb_defined.xres / 8) & 0xf8;
349 vesafb_defined.hsync_len = (vesafb_defined.xres / 8) & 0xf8;
350
351 vesafb_defined.red.offset = screen_info.red_pos;
352 vesafb_defined.red.length = screen_info.red_size;
353 vesafb_defined.green.offset = screen_info.green_pos;
354 vesafb_defined.green.length = screen_info.green_size;
355 vesafb_defined.blue.offset = screen_info.blue_pos;
356 vesafb_defined.blue.length = screen_info.blue_size;
357 vesafb_defined.transp.offset = screen_info.rsvd_pos;
358 vesafb_defined.transp.length = screen_info.rsvd_size;
359
360 if (vesafb_defined.bits_per_pixel <= 8) {
361 depth = vesafb_defined.green.length;
362 vesafb_defined.red.length =
363 vesafb_defined.green.length =
364 vesafb_defined.blue.length =
365 vesafb_defined.bits_per_pixel;
366 }
367
368 printk(KERN_INFO "vesafb: %s: "
369 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
370 (vesafb_defined.bits_per_pixel > 8) ?
Antonino A. Daplas89ec4c22006-04-10 22:55:48 -0700371 "Truecolor" : (vga_compat || pmi_setpal) ?
372 "Pseudocolor" : "Static Pseudocolor",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 screen_info.rsvd_size,
374 screen_info.red_size,
375 screen_info.green_size,
376 screen_info.blue_size,
377 screen_info.rsvd_pos,
378 screen_info.red_pos,
379 screen_info.green_pos,
380 screen_info.blue_pos);
381
382 vesafb_fix.ypanstep = ypan ? 1 : 0;
383 vesafb_fix.ywrapstep = (ypan>1) ? 1 : 0;
384
385 /* request failure does not faze us, as vgacon probably has this
386 * region already (FIXME) */
387 request_region(0x3c0, 32, "vesafb");
388
Jan Beulichf5f4917c2005-11-13 16:08:12 -0800389#ifdef CONFIG_MTRR
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (mtrr) {
Gerd Knorr78c03712005-06-21 17:16:56 -0700391 unsigned int temp_size = size_total;
Antonino A. Daplas80625942005-07-29 14:03:31 -0700392 unsigned int type = 0;
Dave Jonesd7496cb2005-06-25 14:58:30 -0700393
Antonino A. Daplas80625942005-07-29 14:03:31 -0700394 switch (mtrr) {
395 case 1:
396 type = MTRR_TYPE_UNCACHABLE;
397 break;
398 case 2:
399 type = MTRR_TYPE_WRBACK;
400 break;
401 case 3:
402 type = MTRR_TYPE_WRCOMB;
403 break;
404 case 4:
405 type = MTRR_TYPE_WRTHROUGH;
406 break;
407 default:
408 type = 0;
409 break;
410 }
411
412 if (type) {
413 int rc;
414
415 /* Find the largest power-of-two */
416 while (temp_size & (temp_size - 1))
417 temp_size &= (temp_size - 1);
418
419 /* Try and find a power of two to add */
420 do {
421 rc = mtrr_add(vesafb_fix.smem_start, temp_size,
422 type, 1);
423 temp_size >>= 1;
424 } while (temp_size >= PAGE_SIZE && rc == -EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 }
Jan Beulichf5f4917c2005-11-13 16:08:12 -0800427#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 info->fbops = &vesafb_ops;
430 info->var = vesafb_defined;
431 info->fix = vesafb_fix;
432 info->flags = FBINFO_FLAG_DEFAULT |
433 (ypan) ? FBINFO_HWACCEL_YPAN : 0;
434
Antonino A. Daplase53f87a2006-01-09 20:53:18 -0800435 if (!ypan)
436 info->fbops->fb_pan_display = NULL;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
439 err = -ENOMEM;
440 goto err;
441 }
442 if (register_framebuffer(info)<0) {
443 err = -EINVAL;
444 fb_dealloc_cmap(&info->cmap);
445 goto err;
446 }
447 printk(KERN_INFO "fb%d: %s frame buffer device\n",
448 info->node, info->fix.id);
449 return 0;
450err:
451 framebuffer_release(info);
452 release_mem_region(vesafb_fix.smem_start, size_total);
453 return err;
454}
455
Russell King3ae5eae2005-11-09 22:32:44 +0000456static struct platform_driver vesafb_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 .probe = vesafb_probe,
Russell King3ae5eae2005-11-09 22:32:44 +0000458 .driver = {
459 .name = "vesafb",
460 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461};
462
Antonino A. Daplas103edf02006-06-26 00:26:33 -0700463static struct platform_device *vesafb_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465static int __init vesafb_init(void)
466{
467 int ret;
468 char *option = NULL;
469
470 /* ignore error return of fb_get_options */
471 fb_get_options("vesafb", &option);
472 vesafb_setup(option);
Russell King3ae5eae2005-11-09 22:32:44 +0000473 ret = platform_driver_register(&vesafb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 if (!ret) {
Antonino A. Daplas103edf02006-06-26 00:26:33 -0700476 vesafb_device = platform_device_alloc("vesafb", 0);
477
478 if (vesafb_device)
479 ret = platform_device_add(vesafb_device);
480 else
481 ret = -ENOMEM;
482
483 if (ret) {
484 platform_device_put(vesafb_device);
Russell King3ae5eae2005-11-09 22:32:44 +0000485 platform_driver_unregister(&vesafb_driver);
Antonino A. Daplas103edf02006-06-26 00:26:33 -0700486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
Antonino A. Daplas103edf02006-06-26 00:26:33 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return ret;
490}
491module_init(vesafb_init);
492
493MODULE_LICENSE("GPL");