8fc1278d7fbddaac58b88d694ff90ba1e3bb1091
[linux-3.10.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 #ifdef __i386__
23 #include <video/edid.h>
24 #endif
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      = 1;
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         switch (info->var.bits_per_pixel) {
138         case 8:
139                 vesa_setpalette(regno,red,green,blue);
140                 break;
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                 red   >>= 8;
158                 green >>= 8;
159                 blue  >>= 8;
160                 ((u32 *)(info->pseudo_palette))[regno] =
161                         (red   << info->var.red.offset)   |
162                         (green << info->var.green.offset) |
163                         (blue  << info->var.blue.offset);
164                 break;
165         case 32:
166                 red   >>= 8;
167                 green >>= 8;
168                 blue  >>= 8;
169                 ((u32 *)(info->pseudo_palette))[regno] =
170                         (red   << info->var.red.offset)   |
171                         (green << info->var.green.offset) |
172                         (blue  << info->var.blue.offset);
173                 break;
174     }
175     return 0;
176 }
177
178 static struct fb_ops vesafb_ops = {
179         .owner          = THIS_MODULE,
180         .fb_setcolreg   = vesafb_setcolreg,
181         .fb_pan_display = vesafb_pan_display,
182         .fb_fillrect    = cfb_fillrect,
183         .fb_copyarea    = cfb_copyarea,
184         .fb_imageblit   = cfb_imageblit,
185         .fb_cursor      = soft_cursor,
186 };
187
188 static int __init vesafb_setup(char *options)
189 {
190         char *this_opt;
191         
192         if (!options || !*options)
193                 return 0;
194         
195         while ((this_opt = strsep(&options, ",")) != NULL) {
196                 if (!*this_opt) continue;
197                 
198                 if (! strcmp(this_opt, "inverse"))
199                         inverse=1;
200                 else if (! strcmp(this_opt, "redraw"))
201                         ypan=0;
202                 else if (! strcmp(this_opt, "ypan"))
203                         ypan=1;
204                 else if (! strcmp(this_opt, "ywrap"))
205                         ypan=2;
206                 else if (! strcmp(this_opt, "vgapal"))
207                         pmi_setpal=0;
208                 else if (! strcmp(this_opt, "pmipal"))
209                         pmi_setpal=1;
210                 else if (! strcmp(this_opt, "mtrr"))
211                         mtrr=1;
212                 else if (! strcmp(this_opt, "nomtrr"))
213                         mtrr=0;
214                 else if (! strncmp(this_opt, "vtotal:", 7))
215                         vram_total = simple_strtoul(this_opt+7, NULL, 0);
216                 else if (! strncmp(this_opt, "vremap:", 7))
217                         vram_remap = simple_strtoul(this_opt+7, NULL, 0);
218         }
219         return 0;
220 }
221
222 static int __init vesafb_probe(struct device *device)
223 {
224         struct platform_device *dev = to_platform_device(device);
225         struct fb_info *info;
226         int i, err;
227         unsigned int size_vmode;
228         unsigned int size_remap;
229         unsigned int size_total;
230
231         if (screen_info.orig_video_isVGA != VIDEO_TYPE_VLFB)
232                 return -ENODEV;
233
234         vesafb_fix.smem_start = screen_info.lfb_base;
235         vesafb_defined.bits_per_pixel = screen_info.lfb_depth;
236         if (15 == vesafb_defined.bits_per_pixel)
237                 vesafb_defined.bits_per_pixel = 16;
238         vesafb_defined.xres = screen_info.lfb_width;
239         vesafb_defined.yres = screen_info.lfb_height;
240         vesafb_fix.line_length = screen_info.lfb_linelength;
241         vesafb_fix.visual   = (vesafb_defined.bits_per_pixel == 8) ?
242                 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
243
244         /*   size_vmode -- that is the amount of memory needed for the
245          *                 used video mode, i.e. the minimum amount of
246          *                 memory we need. */
247         size_vmode = vesafb_defined.yres * vesafb_fix.line_length;
248
249         /*   size_total -- all video memory we have. Used for mtrr
250          *                 entries, ressource allocation and bounds
251          *                 checking. */
252         size_total = screen_info.lfb_size * 65536;
253         if (vram_total)
254                 size_total = vram_total * 1024 * 1024;
255         if (size_total < size_vmode)
256                 size_total = size_vmode;
257
258         /*   size_remap -- the amount of video memory we are going to
259          *                 use for vesafb.  With modern cards it is no
260          *                 option to simply use size_total as that
261          *                 wastes plenty of kernel address space. */
262         size_remap  = size_vmode * 2;
263         if (vram_remap)
264                 size_remap = vram_remap * 1024 * 1024;
265         if (size_remap < size_vmode)
266                 size_remap = size_vmode;
267         if (size_remap > size_total)
268                 size_remap = size_total;
269         vesafb_fix.smem_len = size_remap;
270
271 #ifndef __i386__
272         screen_info.vesapm_seg = 0;
273 #endif
274
275         if (!request_mem_region(vesafb_fix.smem_start, size_total, "vesafb")) {
276                 printk(KERN_WARNING
277                        "vesafb: abort, cannot reserve video memory at 0x%lx\n",
278                         vesafb_fix.smem_start);
279                 /* We cannot make this fatal. Sometimes this comes from magic
280                    spaces our resource handlers simply don't know about */
281         }
282
283         info = framebuffer_alloc(sizeof(u32) * 256, &dev->dev);
284         if (!info) {
285                 release_mem_region(vesafb_fix.smem_start, vesafb_fix.smem_len);
286                 return -ENOMEM;
287         }
288         info->pseudo_palette = info->par;
289         info->par = NULL;
290
291         info->screen_base = ioremap(vesafb_fix.smem_start, vesafb_fix.smem_len);
292         if (!info->screen_base) {
293                 printk(KERN_ERR
294                        "vesafb: abort, cannot ioremap video memory 0x%x @ 0x%lx\n",
295                         vesafb_fix.smem_len, vesafb_fix.smem_start);
296                 err = -EIO;
297                 goto err;
298         }
299
300         printk(KERN_INFO "vesafb: framebuffer at 0x%lx, mapped to 0x%p, "
301                "using %dk, total %dk\n",
302                vesafb_fix.smem_start, info->screen_base,
303                size_remap/1024, size_total/1024);
304         printk(KERN_INFO "vesafb: mode is %dx%dx%d, linelength=%d, pages=%d\n",
305                vesafb_defined.xres, vesafb_defined.yres, vesafb_defined.bits_per_pixel, vesafb_fix.line_length, screen_info.pages);
306
307         if (screen_info.vesapm_seg) {
308                 printk(KERN_INFO "vesafb: protected mode interface info at %04x:%04x\n",
309                        screen_info.vesapm_seg,screen_info.vesapm_off);
310         }
311
312         if (screen_info.vesapm_seg < 0xc000)
313                 ypan = pmi_setpal = 0; /* not available or some DOS TSR ... */
314
315         if (ypan || pmi_setpal) {
316                 pmi_base  = (unsigned short*)phys_to_virt(((unsigned long)screen_info.vesapm_seg << 4) + screen_info.vesapm_off);
317                 pmi_start = (void*)((char*)pmi_base + pmi_base[1]);
318                 pmi_pal   = (void*)((char*)pmi_base + pmi_base[2]);
319                 printk(KERN_INFO "vesafb: pmi: set display start = %p, set palette = %p\n",pmi_start,pmi_pal);
320                 if (pmi_base[3]) {
321                         printk(KERN_INFO "vesafb: pmi: ports = ");
322                                 for (i = pmi_base[3]/2; pmi_base[i] != 0xffff; i++)
323                                         printk("%x ",pmi_base[i]);
324                         printk("\n");
325                         if (pmi_base[i] != 0xffff) {
326                                 /*
327                                  * memory areas not supported (yet?)
328                                  *
329                                  * Rules are: we have to set up a descriptor for the requested
330                                  * memory area and pass it in the ES register to the BIOS function.
331                                  */
332                                 printk(KERN_INFO "vesafb: can't handle memory requests, pmi disabled\n");
333                                 ypan = pmi_setpal = 0;
334                         }
335                 }
336         }
337
338         vesafb_defined.xres_virtual = vesafb_defined.xres;
339         vesafb_defined.yres_virtual = vesafb_fix.smem_len / vesafb_fix.line_length;
340         if (ypan && vesafb_defined.yres_virtual > vesafb_defined.yres) {
341                 printk(KERN_INFO "vesafb: scrolling: %s using protected mode interface, yres_virtual=%d\n",
342                        (ypan > 1) ? "ywrap" : "ypan",vesafb_defined.yres_virtual);
343         } else {
344                 printk(KERN_INFO "vesafb: scrolling: redraw\n");
345                 vesafb_defined.yres_virtual = vesafb_defined.yres;
346                 ypan = 0;
347         }
348
349         /* some dummy values for timing to make fbset happy */
350         vesafb_defined.pixclock     = 10000000 / vesafb_defined.xres * 1000 / vesafb_defined.yres;
351         vesafb_defined.left_margin  = (vesafb_defined.xres / 8) & 0xf8;
352         vesafb_defined.hsync_len    = (vesafb_defined.xres / 8) & 0xf8;
353         
354         vesafb_defined.red.offset    = screen_info.red_pos;
355         vesafb_defined.red.length    = screen_info.red_size;
356         vesafb_defined.green.offset  = screen_info.green_pos;
357         vesafb_defined.green.length  = screen_info.green_size;
358         vesafb_defined.blue.offset   = screen_info.blue_pos;
359         vesafb_defined.blue.length   = screen_info.blue_size;
360         vesafb_defined.transp.offset = screen_info.rsvd_pos;
361         vesafb_defined.transp.length = screen_info.rsvd_size;
362
363         if (vesafb_defined.bits_per_pixel <= 8) {
364                 depth = vesafb_defined.green.length;
365                 vesafb_defined.red.length =
366                 vesafb_defined.green.length =
367                 vesafb_defined.blue.length =
368                 vesafb_defined.bits_per_pixel;
369         }
370
371         printk(KERN_INFO "vesafb: %s: "
372                "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
373                (vesafb_defined.bits_per_pixel > 8) ?
374                "Truecolor" : "Pseudocolor",
375                screen_info.rsvd_size,
376                screen_info.red_size,
377                screen_info.green_size,
378                screen_info.blue_size,
379                screen_info.rsvd_pos,
380                screen_info.red_pos,
381                screen_info.green_pos,
382                screen_info.blue_pos);
383
384         vesafb_fix.ypanstep  = ypan     ? 1 : 0;
385         vesafb_fix.ywrapstep = (ypan>1) ? 1 : 0;
386
387         /* request failure does not faze us, as vgacon probably has this
388          * region already (FIXME) */
389         request_region(0x3c0, 32, "vesafb");
390
391         if (mtrr) {
392                 int temp_size = size_total;
393                 /* Find the largest power-of-two */
394                 while (temp_size & (temp_size - 1))
395                         temp_size &= (temp_size - 1);
396                         
397                 /* Try and find a power of two to add */
398                 while (temp_size && mtrr_add(vesafb_fix.smem_start, temp_size, MTRR_TYPE_WRCOMB, 1)==-EINVAL) {
399                         temp_size >>= 1;
400                 }
401         }
402         
403         info->fbops = &vesafb_ops;
404         info->var = vesafb_defined;
405         info->fix = vesafb_fix;
406         info->flags = FBINFO_FLAG_DEFAULT |
407                 (ypan) ? FBINFO_HWACCEL_YPAN : 0;
408
409         if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
410                 err = -ENOMEM;
411                 goto err;
412         }
413         if (register_framebuffer(info)<0) {
414                 err = -EINVAL;
415                 fb_dealloc_cmap(&info->cmap);
416                 goto err;
417         }
418         printk(KERN_INFO "fb%d: %s frame buffer device\n",
419                info->node, info->fix.id);
420         return 0;
421 err:
422         framebuffer_release(info);
423         release_mem_region(vesafb_fix.smem_start, size_total);
424         return err;
425 }
426
427 static struct device_driver vesafb_driver = {
428         .name   = "vesafb",
429         .bus    = &platform_bus_type,
430         .probe  = vesafb_probe,
431 };
432
433 static struct platform_device vesafb_device = {
434         .name   = "vesafb",
435 };
436
437 static int __init vesafb_init(void)
438 {
439         int ret;
440         char *option = NULL;
441
442         /* ignore error return of fb_get_options */
443         fb_get_options("vesafb", &option);
444         vesafb_setup(option);
445         ret = driver_register(&vesafb_driver);
446
447         if (!ret) {
448                 ret = platform_device_register(&vesafb_device);
449                 if (ret)
450                         driver_unregister(&vesafb_driver);
451         }
452         return ret;
453 }
454 module_init(vesafb_init);
455
456 MODULE_LICENSE("GPL");