intelfb: fix mtrr_reg signedness
[safe/jmp/linux-2.6] / drivers / video / 68328fb.c
1 /*
2  *  linux/drivers/video/68328fb.c -- Low level implementation of the
3  *                                   mc68x328 LCD frame buffer device
4  *
5  *      Copyright (C) 2003 Georges Menie
6  *
7  *  This driver assumes an already configured controller (e.g. from config.c)
8  *  Keep the code clean of board specific initialization.
9  *
10  *  This code has not been tested with colors, colormap management functions
11  *  are minimal (no colormap data written to the 68328 registers...)
12  *
13  *  initial version of this driver:
14  *    Copyright (C) 1998,1999 Kenneth Albanowski <kjahds@kjahds.com>,
15  *                            The Silver Hammer Group, Ltd.
16  *
17  *  this version is based on :
18  *
19  *  linux/drivers/video/vfb.c -- Virtual frame buffer device
20  *
21  *      Copyright (C) 2002 James Simmons
22  *
23  *      Copyright (C) 1997 Geert Uytterhoeven
24  *
25  *  This file is subject to the terms and conditions of the GNU General Public
26  *  License. See the file COPYING in the main directory of this archive for
27  *  more details.
28  */
29
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/errno.h>
33 #include <linux/string.h>
34 #include <linux/mm.h>
35 #include <linux/tty.h>
36 #include <linux/slab.h>
37 #include <linux/vmalloc.h>
38 #include <linux/delay.h>
39 #include <linux/interrupt.h>
40 #include <asm/uaccess.h>
41 #include <linux/fb.h>
42 #include <linux/init.h>
43
44 #if defined(CONFIG_M68VZ328)
45 #include <asm/MC68VZ328.h>
46 #elif defined(CONFIG_M68EZ328)
47 #include <asm/MC68EZ328.h>
48 #elif defined(CONFIG_M68328)
49 #include <asm/MC68328.h>
50 #else
51 #error wrong architecture for the MC68x328 frame buffer device
52 #endif
53
54 #if defined(CONFIG_FB_68328_INVERT)
55 #define MC68X328FB_MONO_VISUAL FB_VISUAL_MONO01
56 #else
57 #define MC68X328FB_MONO_VISUAL FB_VISUAL_MONO10
58 #endif
59
60 static u_long videomemory;
61 static u_long videomemorysize;
62
63 static struct fb_info fb_info;
64 static u32 mc68x328fb_pseudo_palette[17];
65
66 static struct fb_var_screeninfo mc68x328fb_default __initdata = {
67         .red =          { 0, 8, 0 },
68         .green =        { 0, 8, 0 },
69         .blue =         { 0, 8, 0 },
70         .activate =     FB_ACTIVATE_TEST,
71         .height =       -1,
72         .width =        -1,
73         .pixclock =     20000,
74         .left_margin =  64,
75         .right_margin = 64,
76         .upper_margin = 32,
77         .lower_margin = 32,
78         .hsync_len =    64,
79         .vsync_len =    2,
80         .vmode =        FB_VMODE_NONINTERLACED,
81 };
82
83 static struct fb_fix_screeninfo mc68x328fb_fix __initdata = {
84         .id =           "68328fb",
85         .type =         FB_TYPE_PACKED_PIXELS,
86         .xpanstep =     1,
87         .ypanstep =     1,
88         .ywrapstep =    1,
89         .accel =        FB_ACCEL_NONE,
90 };
91
92     /*
93      *  Interface used by the world
94      */
95 int mc68x328fb_init(void);
96 int mc68x328fb_setup(char *);
97
98 static int mc68x328fb_check_var(struct fb_var_screeninfo *var,
99                          struct fb_info *info);
100 static int mc68x328fb_set_par(struct fb_info *info);
101 static int mc68x328fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
102                          u_int transp, struct fb_info *info);
103 static int mc68x328fb_pan_display(struct fb_var_screeninfo *var,
104                            struct fb_info *info);
105 static int mc68x328fb_mmap(struct fb_info *info, struct vm_area_struct *vma);
106
107 static struct fb_ops mc68x328fb_ops = {
108         .fb_check_var   = mc68x328fb_check_var,
109         .fb_set_par     = mc68x328fb_set_par,
110         .fb_setcolreg   = mc68x328fb_setcolreg,
111         .fb_pan_display = mc68x328fb_pan_display,
112         .fb_fillrect    = cfb_fillrect,
113         .fb_copyarea    = cfb_copyarea,
114         .fb_imageblit   = cfb_imageblit,
115         .fb_mmap        = mc68x328fb_mmap,
116 };
117
118     /*
119      *  Internal routines
120      */
121
122 static u_long get_line_length(int xres_virtual, int bpp)
123 {
124         u_long length;
125
126         length = xres_virtual * bpp;
127         length = (length + 31) & ~31;
128         length >>= 3;
129         return (length);
130 }
131
132     /*
133      *  Setting the video mode has been split into two parts.
134      *  First part, xxxfb_check_var, must not write anything
135      *  to hardware, it should only verify and adjust var.
136      *  This means it doesn't alter par but it does use hardware
137      *  data from it to check this var. 
138      */
139
140 static int mc68x328fb_check_var(struct fb_var_screeninfo *var,
141                          struct fb_info *info)
142 {
143         u_long line_length;
144
145         /*
146          *  FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
147          *  as FB_VMODE_SMOOTH_XPAN is only used internally
148          */
149
150         if (var->vmode & FB_VMODE_CONUPDATE) {
151                 var->vmode |= FB_VMODE_YWRAP;
152                 var->xoffset = info->var.xoffset;
153                 var->yoffset = info->var.yoffset;
154         }
155
156         /*
157          *  Some very basic checks
158          */
159         if (!var->xres)
160                 var->xres = 1;
161         if (!var->yres)
162                 var->yres = 1;
163         if (var->xres > var->xres_virtual)
164                 var->xres_virtual = var->xres;
165         if (var->yres > var->yres_virtual)
166                 var->yres_virtual = var->yres;
167         if (var->bits_per_pixel <= 1)
168                 var->bits_per_pixel = 1;
169         else if (var->bits_per_pixel <= 8)
170                 var->bits_per_pixel = 8;
171         else if (var->bits_per_pixel <= 16)
172                 var->bits_per_pixel = 16;
173         else if (var->bits_per_pixel <= 24)
174                 var->bits_per_pixel = 24;
175         else if (var->bits_per_pixel <= 32)
176                 var->bits_per_pixel = 32;
177         else
178                 return -EINVAL;
179
180         if (var->xres_virtual < var->xoffset + var->xres)
181                 var->xres_virtual = var->xoffset + var->xres;
182         if (var->yres_virtual < var->yoffset + var->yres)
183                 var->yres_virtual = var->yoffset + var->yres;
184
185         /*
186          *  Memory limit
187          */
188         line_length =
189             get_line_length(var->xres_virtual, var->bits_per_pixel);
190         if (line_length * var->yres_virtual > videomemorysize)
191                 return -ENOMEM;
192
193         /*
194          * Now that we checked it we alter var. The reason being is that the video
195          * mode passed in might not work but slight changes to it might make it 
196          * work. This way we let the user know what is acceptable.
197          */
198         switch (var->bits_per_pixel) {
199         case 1:
200                 var->red.offset = 0;
201                 var->red.length = 1;
202                 var->green.offset = 0;
203                 var->green.length = 1;
204                 var->blue.offset = 0;
205                 var->blue.length = 1;
206                 var->transp.offset = 0;
207                 var->transp.length = 0;
208                 break;
209         case 8:
210                 var->red.offset = 0;
211                 var->red.length = 8;
212                 var->green.offset = 0;
213                 var->green.length = 8;
214                 var->blue.offset = 0;
215                 var->blue.length = 8;
216                 var->transp.offset = 0;
217                 var->transp.length = 0;
218                 break;
219         case 16:                /* RGBA 5551 */
220                 if (var->transp.length) {
221                         var->red.offset = 0;
222                         var->red.length = 5;
223                         var->green.offset = 5;
224                         var->green.length = 5;
225                         var->blue.offset = 10;
226                         var->blue.length = 5;
227                         var->transp.offset = 15;
228                         var->transp.length = 1;
229                 } else {        /* RGB 565 */
230                         var->red.offset = 0;
231                         var->red.length = 5;
232                         var->green.offset = 5;
233                         var->green.length = 6;
234                         var->blue.offset = 11;
235                         var->blue.length = 5;
236                         var->transp.offset = 0;
237                         var->transp.length = 0;
238                 }
239                 break;
240         case 24:                /* RGB 888 */
241                 var->red.offset = 0;
242                 var->red.length = 8;
243                 var->green.offset = 8;
244                 var->green.length = 8;
245                 var->blue.offset = 16;
246                 var->blue.length = 8;
247                 var->transp.offset = 0;
248                 var->transp.length = 0;
249                 break;
250         case 32:                /* RGBA 8888 */
251                 var->red.offset = 0;
252                 var->red.length = 8;
253                 var->green.offset = 8;
254                 var->green.length = 8;
255                 var->blue.offset = 16;
256                 var->blue.length = 8;
257                 var->transp.offset = 24;
258                 var->transp.length = 8;
259                 break;
260         }
261         var->red.msb_right = 0;
262         var->green.msb_right = 0;
263         var->blue.msb_right = 0;
264         var->transp.msb_right = 0;
265
266         return 0;
267 }
268
269 /* This routine actually sets the video mode. It's in here where we
270  * the hardware state info->par and fix which can be affected by the 
271  * change in par. For this driver it doesn't do much. 
272  */
273 static int mc68x328fb_set_par(struct fb_info *info)
274 {
275         info->fix.line_length = get_line_length(info->var.xres_virtual,
276                                                 info->var.bits_per_pixel);
277         return 0;
278 }
279
280     /*
281      *  Set a single color register. The values supplied are already
282      *  rounded down to the hardware's capabilities (according to the
283      *  entries in the var structure). Return != 0 for invalid regno.
284      */
285
286 static int mc68x328fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
287                          u_int transp, struct fb_info *info)
288 {
289         if (regno >= 256)       /* no. of hw registers */
290                 return 1;
291         /*
292          * Program hardware... do anything you want with transp
293          */
294
295         /* grayscale works only partially under directcolor */
296         if (info->var.grayscale) {
297                 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
298                 red = green = blue =
299                     (red * 77 + green * 151 + blue * 28) >> 8;
300         }
301
302         /* Directcolor:
303          *   var->{color}.offset contains start of bitfield
304          *   var->{color}.length contains length of bitfield
305          *   {hardwarespecific} contains width of RAMDAC
306          *   cmap[X] is programmed to (X << red.offset) | (X << green.offset) | (X << blue.offset)
307          *   RAMDAC[X] is programmed to (red, green, blue)
308          * 
309          * Pseudocolor:
310          *    uses offset = 0 && length = RAMDAC register width.
311          *    var->{color}.offset is 0
312          *    var->{color}.length contains widht of DAC
313          *    cmap is not used
314          *    RAMDAC[X] is programmed to (red, green, blue)
315          * Truecolor:
316          *    does not use DAC. Usually 3 are present.
317          *    var->{color}.offset contains start of bitfield
318          *    var->{color}.length contains length of bitfield
319          *    cmap is programmed to (red << red.offset) | (green << green.offset) |
320          *                      (blue << blue.offset) | (transp << transp.offset)
321          *    RAMDAC does not exist
322          */
323 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
324         switch (info->fix.visual) {
325         case FB_VISUAL_TRUECOLOR:
326         case FB_VISUAL_PSEUDOCOLOR:
327                 red = CNVT_TOHW(red, info->var.red.length);
328                 green = CNVT_TOHW(green, info->var.green.length);
329                 blue = CNVT_TOHW(blue, info->var.blue.length);
330                 transp = CNVT_TOHW(transp, info->var.transp.length);
331                 break;
332         case FB_VISUAL_DIRECTCOLOR:
333                 red = CNVT_TOHW(red, 8);        /* expect 8 bit DAC */
334                 green = CNVT_TOHW(green, 8);
335                 blue = CNVT_TOHW(blue, 8);
336                 /* hey, there is bug in transp handling... */
337                 transp = CNVT_TOHW(transp, 8);
338                 break;
339         }
340 #undef CNVT_TOHW
341         /* Truecolor has hardware independent palette */
342         if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
343                 u32 v;
344
345                 if (regno >= 16)
346                         return 1;
347
348                 v = (red << info->var.red.offset) |
349                     (green << info->var.green.offset) |
350                     (blue << info->var.blue.offset) |
351                     (transp << info->var.transp.offset);
352                 switch (info->var.bits_per_pixel) {
353                 case 8:
354                         break;
355                 case 16:
356                         ((u32 *) (info->pseudo_palette))[regno] = v;
357                         break;
358                 case 24:
359                 case 32:
360                         ((u32 *) (info->pseudo_palette))[regno] = v;
361                         break;
362                 }
363                 return 0;
364         }
365         return 0;
366 }
367
368     /*
369      *  Pan or Wrap the Display
370      *
371      *  This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
372      */
373
374 static int mc68x328fb_pan_display(struct fb_var_screeninfo *var,
375                            struct fb_info *info)
376 {
377         if (var->vmode & FB_VMODE_YWRAP) {
378                 if (var->yoffset < 0
379                     || var->yoffset >= info->var.yres_virtual
380                     || var->xoffset)
381                         return -EINVAL;
382         } else {
383                 if (var->xoffset + var->xres > info->var.xres_virtual ||
384                     var->yoffset + var->yres > info->var.yres_virtual)
385                         return -EINVAL;
386         }
387         info->var.xoffset = var->xoffset;
388         info->var.yoffset = var->yoffset;
389         if (var->vmode & FB_VMODE_YWRAP)
390                 info->var.vmode |= FB_VMODE_YWRAP;
391         else
392                 info->var.vmode &= ~FB_VMODE_YWRAP;
393         return 0;
394 }
395
396     /*
397      *  Most drivers don't need their own mmap function 
398      */
399
400 static int mc68x328fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
401 {
402 #ifndef MMU
403         /* this is uClinux (no MMU) specific code */
404
405         vma->vm_flags |= VM_RESERVED;
406         vma->vm_start = videomemory;
407
408         return 0;
409 #else
410         return -EINVAL;
411 #endif
412 }
413
414 int __init mc68x328fb_setup(char *options)
415 {
416 #if 0
417         char *this_opt;
418 #endif
419
420         if (!options || !*options)
421                 return 1;
422 #if 0
423         while ((this_opt = strsep(&options, ",")) != NULL) {
424                 if (!*this_opt)
425                         continue;
426                 if (!strncmp(this_opt, "disable", 7))
427                         mc68x328fb_enable = 0;
428         }
429 #endif
430         return 1;
431 }
432
433     /*
434      *  Initialisation
435      */
436
437 int __init mc68x328fb_init(void)
438 {
439 #ifndef MODULE
440         char *option = NULL;
441
442         if (fb_get_options("68328fb", &option))
443                 return -ENODEV;
444         mc68x328fb_setup(option);
445 #endif
446         /*
447          *  initialize the default mode from the LCD controller registers
448          */
449         mc68x328fb_default.xres = LXMAX;
450         mc68x328fb_default.yres = LYMAX+1;
451         mc68x328fb_default.xres_virtual = mc68x328fb_default.xres;
452         mc68x328fb_default.yres_virtual = mc68x328fb_default.yres;
453         mc68x328fb_default.bits_per_pixel = 1 + (LPICF & 0x01);
454         videomemory = LSSA;
455         videomemorysize = (mc68x328fb_default.xres_virtual+7) / 8 *
456                 mc68x328fb_default.yres_virtual * mc68x328fb_default.bits_per_pixel;
457
458         fb_info.screen_base = (void *)videomemory;
459         fb_info.fbops = &mc68x328fb_ops;
460         fb_info.var = mc68x328fb_default;
461         fb_info.fix = mc68x328fb_fix;
462         fb_info.fix.smem_start = videomemory;
463         fb_info.fix.smem_len = videomemorysize;
464         fb_info.fix.line_length =
465                 get_line_length(mc68x328fb_default.xres_virtual, mc68x328fb_default.bits_per_pixel);
466         fb_info.fix.visual = (mc68x328fb_default.bits_per_pixel) == 1 ?
467                 MC68X328FB_MONO_VISUAL : FB_VISUAL_PSEUDOCOLOR;
468         if (fb_info.var.bits_per_pixel == 1) {
469                 fb_info.var.red.length = fb_info.var.green.length = fb_info.var.blue.length = 1;
470                 fb_info.var.red.offset = fb_info.var.green.offset = fb_info.var.blue.offset = 0;
471         }
472         fb_info.pseudo_palette = &mc68x328fb_pseudo_palette;
473         fb_info.flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
474
475         fb_alloc_cmap(&fb_info.cmap, 256, 0);
476
477         if (register_framebuffer(&fb_info) < 0) {
478                 return -EINVAL;
479         }
480
481         printk(KERN_INFO
482                 "fb%d: %s frame buffer device\n", fb_info.node, fb_info.fix.id);
483         printk(KERN_INFO
484                 "fb%d: %dx%dx%d at 0x%08lx\n", fb_info.node,
485                 mc68x328fb_default.xres_virtual, mc68x328fb_default.yres_virtual,
486                 1 << mc68x328fb_default.bits_per_pixel, videomemory);
487
488         return 0;
489 }
490
491 module_init(mc68x328fb_init);
492
493 #ifdef MODULE
494
495 static void __exit mc68x328fb_cleanup(void)
496 {
497         unregister_framebuffer(&fb_info);
498 }
499
500 module_exit(mc68x328fb_cleanup);
501
502 MODULE_LICENSE("GPL");
503 #endif                          /* MODULE */