Merge git://git.infradead.org/iommu-2.6
[linux-2.6] / drivers / video / vesafb.c
1 /*
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/slab.h>
17 #include <linux/delay.h>
18 #include <linux/fb.h>
19 #include <linux/ioport.h>
20 #include <linux/init.h>
21 #include <linux/platform_device.h>
22 #include <linux/screen_info.h>
23
24 #include <video/vga.h>
25 #include <asm/io.h>
26 #include <asm/mtrr.h>
27
28 #define dac_reg (0x3c8)
29 #define dac_val (0x3c9)
30
31 /* --------------------------------------------------------------------- */
32
33 static 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
44 static struct fb_fix_screeninfo vesafb_fix __initdata = {
45         .id     = "VESA VGA",
46         .type   = FB_TYPE_PACKED_PIXELS,
47         .accel  = FB_ACCEL_NONE,
48 };
49
50 static int   inverse    __read_mostly;
51 static int   mtrr       __read_mostly;          /* disable mtrr */
52 static int   vram_remap __initdata;             /* Set amount of memory to be used */
53 static int   vram_total __initdata;             /* Set total amount of memory */
54 static int   pmi_setpal __read_mostly = 1;      /* pmi for palette changes ??? */
55 static int   ypan       __read_mostly;          /* 0..nothing, 1..ypan, 2..ywrap */
56 static void  (*pmi_start)(void) __read_mostly;
57 static void  (*pmi_pal)  (void) __read_mostly;
58 static int   depth      __read_mostly;
59 static int   vga_compat __read_mostly;
60 /* --------------------------------------------------------------------- */
61
62 static int vesafb_pan_display(struct fb_var_screeninfo *var,
63                               struct fb_info *info)
64 {
65 #ifdef __i386__
66         int offset;
67
68         offset = (var->yoffset * info->fix.line_length + var->xoffset) / 4;
69
70         __asm__ __volatile__(
71                 "call *(%%edi)"
72                 : /* no return value */
73                 : "a" (0x4f07),         /* EAX */
74                   "b" (0),              /* EBX */
75                   "c" (offset),         /* ECX */
76                   "d" (offset >> 16),   /* EDX */
77                   "D" (&pmi_start));    /* EDI */
78 #endif
79         return 0;
80 }
81
82 static int vesa_setpalette(int regno, unsigned red, unsigned green,
83                             unsigned blue)
84 {
85         int shift = 16 - depth;
86         int err = -EINVAL;
87
88 /*
89  * Try VGA registers first...
90  */
91         if (vga_compat) {
92                 outb_p(regno,       dac_reg);
93                 outb_p(red   >> shift, dac_val);
94                 outb_p(green >> shift, dac_val);
95                 outb_p(blue  >> shift, dac_val);
96                 err = 0;
97         }
98
99 #ifdef __i386__
100 /*
101  * Fallback to the PMI....
102  */
103         if (err && pmi_setpal) {
104                 struct { u_char blue, green, red, pad; } entry;
105
106                 entry.red   = red   >> shift;
107                 entry.green = green >> shift;
108                 entry.blue  = blue  >> shift;
109                 entry.pad   = 0;
110                 __asm__ __volatile__(
111                 "call *(%%esi)"
112                 : /* no return value */
113                 : "a" (0x4f09),         /* EAX */
114                   "b" (0),              /* EBX */
115                   "c" (1),              /* ECX */
116                   "d" (regno),          /* EDX */
117                   "D" (&entry),         /* EDI */
118                   "S" (&pmi_pal));      /* ESI */
119                 err = 0;
120         }
121 #endif
122
123         return err;
124 }
125
126 static int vesafb_setcolreg(unsigned regno, unsigned red, unsigned green,
127                             unsigned blue, unsigned transp,
128                             struct fb_info *info)
129 {
130         int err = 0;
131
132         /*
133          *  Set a single color register. The values supplied are
134          *  already rounded down to the hardware's capabilities
135          *  (according to the entries in the `var' structure). Return
136          *  != 0 for invalid regno.
137          */
138         
139         if (regno >= info->cmap.len)
140                 return 1;
141
142         if (info->var.bits_per_pixel == 8)
143                 err = vesa_setpalette(regno,red,green,blue);
144         else if (regno < 16) {
145                 switch (info->var.bits_per_pixel) {
146                 case 16:
147                         if (info->var.red.offset == 10) {
148                                 /* 1:5:5:5 */
149                                 ((u32*) (info->pseudo_palette))[regno] =
150                                         ((red   & 0xf800) >>  1) |
151                                         ((green & 0xf800) >>  6) |
152                                         ((blue  & 0xf800) >> 11);
153                         } else {
154                                 /* 0:5:6:5 */
155                                 ((u32*) (info->pseudo_palette))[regno] =
156                                         ((red   & 0xf800)      ) |
157                                         ((green & 0xfc00) >>  5) |
158                                         ((blue  & 0xf800) >> 11);
159                         }
160                         break;
161                 case 24:
162                 case 32:
163                         red   >>= 8;
164                         green >>= 8;
165                         blue  >>= 8;
166                         ((u32 *)(info->pseudo_palette))[regno] =
167                                 (red   << info->var.red.offset)   |
168                                 (green << info->var.green.offset) |
169                                 (blue  << info->var.blue.offset);
170                         break;
171                 }
172         }
173
174         return err;
175 }
176
177 static struct fb_ops vesafb_ops = {
178         .owner          = THIS_MODULE,
179         .fb_setcolreg   = vesafb_setcolreg,
180         .fb_pan_display = vesafb_pan_display,
181         .fb_fillrect    = cfb_fillrect,
182         .fb_copyarea    = cfb_copyarea,
183         .fb_imageblit   = cfb_imageblit,
184 };
185
186 static int __init vesafb_setup(char *options)
187 {
188         char *this_opt;
189         
190         if (!options || !*options)
191                 return 0;
192         
193         while ((this_opt = strsep(&options, ",")) != NULL) {
194                 if (!*this_opt) continue;
195                 
196                 if (! strcmp(this_opt, "inverse"))
197                         inverse=1;
198                 else if (! strcmp(this_opt, "redraw"))
199                         ypan=0;
200                 else if (! strcmp(this_opt, "ypan"))
201                         ypan=1;
202                 else if (! strcmp(this_opt, "ywrap"))
203                         ypan=2;
204                 else if (! strcmp(this_opt, "vgapal"))
205                         pmi_setpal=0;
206                 else if (! strcmp(this_opt, "pmipal"))
207                         pmi_setpal=1;
208                 else if (! strncmp(this_opt, "mtrr:", 5))
209                         mtrr = simple_strtoul(this_opt+5, NULL, 0);
210                 else if (! strcmp(this_opt, "nomtrr"))
211                         mtrr=0;
212                 else if (! strncmp(this_opt, "vtotal:", 7))
213                         vram_total = simple_strtoul(this_opt+7, NULL, 0);
214                 else if (! strncmp(this_opt, "vremap:", 7))
215                         vram_remap = simple_strtoul(this_opt+7, NULL, 0);
216         }
217         return 0;
218 }
219
220 static int __init vesafb_probe(struct platform_device *dev)
221 {
222         struct fb_info *info;
223         int i, err;
224         unsigned int size_vmode;
225         unsigned int size_remap;
226         unsigned int size_total;
227
228         if (screen_info.orig_video_isVGA != VIDEO_TYPE_VLFB)
229                 return -ENODEV;
230
231         vga_compat = (screen_info.capabilities & 2) ? 0 : 1;
232         vesafb_fix.smem_start = screen_info.lfb_base;
233         vesafb_defined.bits_per_pixel = screen_info.lfb_depth;
234         if (15 == vesafb_defined.bits_per_pixel)
235                 vesafb_defined.bits_per_pixel = 16;
236         vesafb_defined.xres = screen_info.lfb_width;
237         vesafb_defined.yres = screen_info.lfb_height;
238         vesafb_fix.line_length = screen_info.lfb_linelength;
239         vesafb_fix.visual   = (vesafb_defined.bits_per_pixel == 8) ?
240                 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
241
242         /*   size_vmode -- that is the amount of memory needed for the
243          *                 used video mode, i.e. the minimum amount of
244          *                 memory we need. */
245         size_vmode = vesafb_defined.yres * vesafb_fix.line_length;
246
247         /*   size_total -- all video memory we have. Used for mtrr
248          *                 entries, ressource allocation and bounds
249          *                 checking. */
250         size_total = screen_info.lfb_size * 65536;
251         if (vram_total)
252                 size_total = vram_total * 1024 * 1024;
253         if (size_total < size_vmode)
254                 size_total = size_vmode;
255
256         /*   size_remap -- the amount of video memory we are going to
257          *                 use for vesafb.  With modern cards it is no
258          *                 option to simply use size_total as that
259          *                 wastes plenty of kernel address space. */
260         size_remap  = size_vmode * 2;
261         if (vram_remap)
262                 size_remap = vram_remap * 1024 * 1024;
263         if (size_remap < size_vmode)
264                 size_remap = size_vmode;
265         if (size_remap > size_total)
266                 size_remap = size_total;
267         vesafb_fix.smem_len = size_remap;
268
269 #ifndef __i386__
270         screen_info.vesapm_seg = 0;
271 #endif
272
273         if (!request_mem_region(vesafb_fix.smem_start, size_total, "vesafb")) {
274                 printk(KERN_WARNING
275                        "vesafb: cannot reserve video memory at 0x%lx\n",
276                         vesafb_fix.smem_start);
277                 /* We cannot make this fatal. Sometimes this comes from magic
278                    spaces our resource handlers simply don't know about */
279         }
280
281         info = framebuffer_alloc(sizeof(u32) * 256, &dev->dev);
282         if (!info) {
283                 release_mem_region(vesafb_fix.smem_start, size_total);
284                 return -ENOMEM;
285         }
286         info->pseudo_palette = info->par;
287         info->par = NULL;
288
289         info->screen_base = ioremap(vesafb_fix.smem_start, vesafb_fix.smem_len);
290         if (!info->screen_base) {
291                 printk(KERN_ERR
292                        "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
293                         vesafb_fix.smem_len, vesafb_fix.smem_start);
294                 err = -EIO;
295                 goto err;
296         }
297
298         printk(KERN_INFO "vesafb: framebuffer at 0x%lx, mapped to 0x%p, "
299                "using %dk, total %dk\n",
300                vesafb_fix.smem_start, info->screen_base,
301                size_remap/1024, size_total/1024);
302         printk(KERN_INFO "vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
303                vesafb_defined.xres, vesafb_defined.yres, vesafb_defined.bits_per_pixel, vesafb_fix.line_length, screen_info.pages);
304
305         if (screen_info.vesapm_seg) {
306                 printk(KERN_INFO "vesafb: protected mode interface info at %04x:%04x\n",
307                        screen_info.vesapm_seg,screen_info.vesapm_off);
308         }
309
310         if (screen_info.vesapm_seg < 0xc000)
311                 ypan = pmi_setpal = 0; /* not available or some DOS TSR ... */
312
313         if (ypan || pmi_setpal) {
314                 unsigned short *pmi_base;
315                 pmi_base  = (unsigned short*)phys_to_virt(((unsigned long)screen_info.vesapm_seg << 4) + screen_info.vesapm_off);
316                 pmi_start = (void*)((char*)pmi_base + pmi_base[1]);
317                 pmi_pal   = (void*)((char*)pmi_base + pmi_base[2]);
318                 printk(KERN_INFO "vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start,pmi_pal);
319                 if (pmi_base[3]) {
320                         printk(KERN_INFO "vesafb: pmi: ports = ");
321                                 for (i = pmi_base[3]/2; pmi_base[i] != 0xffff; i++)
322                                         printk("%x ",pmi_base[i]);
323                         printk("\n");
324                         if (pmi_base[i] != 0xffff) {
325                                 /*
326                                  * memory areas not supported (yet?)
327                                  *
328                                  * Rules are: we have to set up a descriptor for the requested
329                                  * memory area and pass it in the ES register to the BIOS function.
330                                  */
331                                 printk(KERN_INFO "vesafb: can't handle memory requests, pmi disabled\n");
332                                 ypan = pmi_setpal = 0;
333                         }
334                 }
335         }
336
337         if (vesafb_defined.bits_per_pixel == 8 && !pmi_setpal && !vga_compat) {
338                 printk(KERN_WARNING "vesafb: hardware palette is unchangeable,\n"
339                                     "        colors may be incorrect\n");
340                 vesafb_fix.visual = FB_VISUAL_STATIC_PSEUDOCOLOR;
341         }
342
343         vesafb_defined.xres_virtual = vesafb_defined.xres;
344         vesafb_defined.yres_virtual = vesafb_fix.smem_len / vesafb_fix.line_length;
345         if (ypan && vesafb_defined.yres_virtual > vesafb_defined.yres) {
346                 printk(KERN_INFO "vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
347                        (ypan > 1) ? "ywrap" : "ypan",vesafb_defined.yres_virtual);
348         } else {
349                 printk(KERN_INFO "vesafb: scrolling: redraw\n");
350                 vesafb_defined.yres_virtual = vesafb_defined.yres;
351                 ypan = 0;
352         }
353
354         /* some dummy values for timing to make fbset happy */
355         vesafb_defined.pixclock     = 10000000 / vesafb_defined.xres * 1000 / vesafb_defined.yres;
356         vesafb_defined.left_margin  = (vesafb_defined.xres / 8) & 0xf8;
357         vesafb_defined.hsync_len    = (vesafb_defined.xres / 8) & 0xf8;
358         
359         vesafb_defined.red.offset    = screen_info.red_pos;
360         vesafb_defined.red.length    = screen_info.red_size;
361         vesafb_defined.green.offset  = screen_info.green_pos;
362         vesafb_defined.green.length  = screen_info.green_size;
363         vesafb_defined.blue.offset   = screen_info.blue_pos;
364         vesafb_defined.blue.length   = screen_info.blue_size;
365         vesafb_defined.transp.offset = screen_info.rsvd_pos;
366         vesafb_defined.transp.length = screen_info.rsvd_size;
367
368         if (vesafb_defined.bits_per_pixel <= 8) {
369                 depth = vesafb_defined.green.length;
370                 vesafb_defined.red.length =
371                 vesafb_defined.green.length =
372                 vesafb_defined.blue.length =
373                 vesafb_defined.bits_per_pixel;
374         }
375
376         printk(KERN_INFO "vesafb: %s: "
377                "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
378                (vesafb_defined.bits_per_pixel > 8) ?
379                "Truecolor" : (vga_compat || pmi_setpal) ?
380                "Pseudocolor" : "Static Pseudocolor",
381                screen_info.rsvd_size,
382                screen_info.red_size,
383                screen_info.green_size,
384                screen_info.blue_size,
385                screen_info.rsvd_pos,
386                screen_info.red_pos,
387                screen_info.green_pos,
388                screen_info.blue_pos);
389
390         vesafb_fix.ypanstep  = ypan     ? 1 : 0;
391         vesafb_fix.ywrapstep = (ypan>1) ? 1 : 0;
392
393         /* request failure does not faze us, as vgacon probably has this
394          * region already (FIXME) */
395         request_region(0x3c0, 32, "vesafb");
396
397 #ifdef CONFIG_MTRR
398         if (mtrr) {
399                 unsigned int temp_size = size_total;
400                 unsigned int type = 0;
401
402                 switch (mtrr) {
403                 case 1:
404                         type = MTRR_TYPE_UNCACHABLE;
405                         break;
406                 case 2:
407                         type = MTRR_TYPE_WRBACK;
408                         break;
409                 case 3:
410                         type = MTRR_TYPE_WRCOMB;
411                         break;
412                 case 4:
413                         type = MTRR_TYPE_WRTHROUGH;
414                         break;
415                 default:
416                         type = 0;
417                         break;
418                 }
419
420                 if (type) {
421                         int rc;
422
423                         /* Find the largest power-of-two */
424                         while (temp_size & (temp_size - 1))
425                                 temp_size &= (temp_size - 1);
426
427                         /* Try and find a power of two to add */
428                         do {
429                                 rc = mtrr_add(vesafb_fix.smem_start, temp_size,
430                                               type, 1);
431                                 temp_size >>= 1;
432                         } while (temp_size >= PAGE_SIZE && rc == -EINVAL);
433                 }
434         }
435 #endif
436         
437         info->fbops = &vesafb_ops;
438         info->var = vesafb_defined;
439         info->fix = vesafb_fix;
440         info->flags = FBINFO_FLAG_DEFAULT |
441                 (ypan ? FBINFO_HWACCEL_YPAN : 0);
442
443         if (!ypan)
444                 info->fbops->fb_pan_display = NULL;
445
446         if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
447                 err = -ENOMEM;
448                 goto err;
449         }
450         if (register_framebuffer(info)<0) {
451                 err = -EINVAL;
452                 fb_dealloc_cmap(&info->cmap);
453                 goto err;
454         }
455         printk(KERN_INFO "fb%d: %s frame buffer device\n",
456                info->node, info->fix.id);
457         return 0;
458 err:
459         if (info->screen_base)
460                 iounmap(info->screen_base);
461         framebuffer_release(info);
462         release_mem_region(vesafb_fix.smem_start, size_total);
463         return err;
464 }
465
466 static struct platform_driver vesafb_driver = {
467         .probe  = vesafb_probe,
468         .driver = {
469                 .name   = "vesafb",
470         },
471 };
472
473 static struct platform_device *vesafb_device;
474
475 static int __init vesafb_init(void)
476 {
477         int ret;
478         char *option = NULL;
479
480         /* ignore error return of fb_get_options */
481         fb_get_options("vesafb", &option);
482         vesafb_setup(option);
483         ret = platform_driver_register(&vesafb_driver);
484
485         if (!ret) {
486                 vesafb_device = platform_device_alloc("vesafb", 0);
487
488                 if (vesafb_device)
489                         ret = platform_device_add(vesafb_device);
490                 else
491                         ret = -ENOMEM;
492
493                 if (ret) {
494                         platform_device_put(vesafb_device);
495                         platform_driver_unregister(&vesafb_driver);
496                 }
497         }
498
499         return ret;
500 }
501 module_init(vesafb_init);
502
503 MODULE_LICENSE("GPL");