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