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