UHCI: workaround for Asus motherboard
[linux-2.6] / drivers / video / hitfb.c
1 /*
2  * linux/drivers/video/hitfb.c -- Hitachi LCD frame buffer device
3  *
4  * (C) 1999 Mihai Spatar
5  * (C) 2000 YAEGASHI Takeshi
6  * (C) 2003, 2004 Paul Mundt
7  * (C) 2003, 2004, 2006 Andriy Skulysh
8  *
9  *  This file is subject to the terms and conditions of the GNU General Public
10  *  License. See the file COPYING in the main directory of this archive for
11  *  more details.
12  */
13
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
19 #include <linux/mm.h>
20 #include <linux/slab.h>
21 #include <linux/delay.h>
22 #include <linux/init.h>
23 #include <linux/platform_device.h>
24 #include <linux/fb.h>
25
26 #include <asm/machvec.h>
27 #include <asm/uaccess.h>
28 #include <asm/pgtable.h>
29 #include <asm/io.h>
30 #include <asm/hd64461.h>
31 #include <asm/cpu/dac.h>
32 #include <asm/hp6xx/hp6xx.h>
33
34 #define WIDTH 640
35
36 static struct fb_var_screeninfo hitfb_var __initdata = {
37         .activate       = FB_ACTIVATE_NOW,
38         .height         = -1,
39         .width          = -1,
40         .vmode          = FB_VMODE_NONINTERLACED,
41 };
42
43 static struct fb_fix_screeninfo hitfb_fix __initdata = {
44         .id             = "Hitachi HD64461",
45         .type           = FB_TYPE_PACKED_PIXELS,
46         .accel          = FB_ACCEL_NONE,
47 };
48
49 static u32 pseudo_palette[16];
50 static struct fb_info fb_info;
51
52 static inline void hitfb_accel_wait(void)
53 {
54         while (fb_readw(HD64461_GRCFGR) & HD64461_GRCFGR_ACCSTATUS) ;
55 }
56
57 static inline void hitfb_accel_start(int truecolor)
58 {
59         if (truecolor) {
60                 fb_writew(6, HD64461_GRCFGR);
61         } else {
62                 fb_writew(7, HD64461_GRCFGR);
63         }
64 }
65
66 static inline void hitfb_accel_set_dest(int truecolor, u16 dx, u16 dy,
67                                         u16 width, u16 height)
68 {
69         u32 saddr = WIDTH * dy + dx;
70         if (truecolor)
71                 saddr <<= 1;
72
73         fb_writew(width-1, HD64461_BBTDWR);
74         fb_writew(height-1, HD64461_BBTDHR);
75
76         fb_writew(saddr & 0xffff, HD64461_BBTDSARL);
77         fb_writew(saddr >> 16, HD64461_BBTDSARH);
78
79 }
80
81 static inline void hitfb_accel_bitblt(int truecolor, u16 sx, u16 sy, u16 dx,
82                                       u16 dy, u16 width, u16 height, u16 rop,
83                                       u32 mask_addr)
84 {
85         u32 saddr, daddr;
86         u32 maddr = 0;
87
88         height--;
89         width--;
90         fb_writew(rop, HD64461_BBTROPR);
91         if ((sy < dy) || ((sy == dy) && (sx <= dx))) {
92                 saddr = WIDTH * (sy + height) + sx + width;
93                 daddr = WIDTH * (dy + height) + dx + width;
94                 if (mask_addr) {
95                         if (truecolor)
96                                 maddr = ((width >> 3) + 1) * (height + 1) - 1;
97                         else
98                                 maddr =
99                                     (((width >> 4) + 1) * (height + 1) - 1) * 2;
100
101                         fb_writew((1 << 5) | 1, HD64461_BBTMDR);
102                 } else
103                         fb_writew(1, HD64461_BBTMDR);
104         } else {
105                 saddr = WIDTH * sy + sx;
106                 daddr = WIDTH * dy + dx;
107                 if (mask_addr) {
108                         fb_writew((1 << 5), HD64461_BBTMDR);
109                 } else {
110                         fb_writew(0, HD64461_BBTMDR);
111                 }
112         }
113         if (truecolor) {
114                 saddr <<= 1;
115                 daddr <<= 1;
116         }
117         fb_writew(width, HD64461_BBTDWR);
118         fb_writew(height, HD64461_BBTDHR);
119         fb_writew(saddr & 0xffff, HD64461_BBTSSARL);
120         fb_writew(saddr >> 16, HD64461_BBTSSARH);
121         fb_writew(daddr & 0xffff, HD64461_BBTDSARL);
122         fb_writew(daddr >> 16, HD64461_BBTDSARH);
123         if (mask_addr) {
124                 maddr += mask_addr;
125                 fb_writew(maddr & 0xffff, HD64461_BBTMARL);
126                 fb_writew(maddr >> 16, HD64461_BBTMARH);
127         }
128         hitfb_accel_start(truecolor);
129 }
130
131 static void hitfb_fillrect(struct fb_info *p, const struct fb_fillrect *rect)
132 {
133         if (rect->rop != ROP_COPY)
134                 cfb_fillrect(p, rect);
135         else {
136                 hitfb_accel_wait();
137                 fb_writew(0x00f0, HD64461_BBTROPR);
138                 fb_writew(16, HD64461_BBTMDR);
139
140                 if (p->var.bits_per_pixel == 16) {
141                         fb_writew(((u32 *) (p->pseudo_palette))[rect->color],
142                                   HD64461_GRSCR);
143                         hitfb_accel_set_dest(1, rect->dx, rect->dy, rect->width,
144                                              rect->height);
145                         hitfb_accel_start(1);
146                 } else {
147                         fb_writew(rect->color, HD64461_GRSCR);
148                         hitfb_accel_set_dest(0, rect->dx, rect->dy, rect->width,
149                                              rect->height);
150                         hitfb_accel_start(0);
151                 }
152         }
153 }
154
155 static void hitfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
156 {
157         hitfb_accel_wait();
158         hitfb_accel_bitblt(p->var.bits_per_pixel == 16, area->sx, area->sy,
159                            area->dx, area->dy, area->width, area->height,
160                            0x00cc, 0);
161 }
162
163 static int hitfb_pan_display(struct fb_var_screeninfo *var,
164                              struct fb_info *info)
165 {
166         int xoffset = var->xoffset;
167         int yoffset = var->yoffset;
168
169         if (xoffset != 0)
170                 return -EINVAL;
171
172         fb_writew((yoffset*info->fix.line_length)>>10, HD64461_LCDCBAR);
173
174         return 0;
175 }
176
177 int hitfb_blank(int blank_mode, struct fb_info *info)
178 {
179         unsigned short v;
180
181         if (blank_mode) {
182                 v = fb_readw(HD64461_LDR1);
183                 v &= ~HD64461_LDR1_DON;
184                 fb_writew(v, HD64461_LDR1);
185
186                 v = fb_readw(HD64461_LCDCCR);
187                 v |= HD64461_LCDCCR_MOFF;
188                 fb_writew(v, HD64461_LCDCCR);
189
190                 v = fb_readw(HD64461_STBCR);
191                 v |= HD64461_STBCR_SLCDST;
192                 fb_writew(v, HD64461_STBCR);
193         } else {
194                 v = fb_readw(HD64461_STBCR);
195                 v &= ~HD64461_STBCR_SLCDST;
196                 fb_writew(v, HD64461_STBCR);
197
198                 v = fb_readw(HD64461_LCDCCR);
199                 v &= ~(HD64461_LCDCCR_MOFF | HD64461_LCDCCR_STREQ);
200                 fb_writew(v, HD64461_LCDCCR);
201
202                 do {
203                     v = fb_readw(HD64461_LCDCCR);
204                 } while(v&HD64461_LCDCCR_STBACK);
205
206                 v = fb_readw(HD64461_LDR1);
207                 v |= HD64461_LDR1_DON;
208                 fb_writew(v, HD64461_LDR1);
209         }
210         return 0;
211 }
212
213 static int hitfb_setcolreg(unsigned regno, unsigned red, unsigned green,
214                            unsigned blue, unsigned transp, struct fb_info *info)
215 {
216         if (regno >= 256)
217                 return 1;
218
219         switch (info->var.bits_per_pixel) {
220         case 8:
221                 fb_writew(regno << 8, HD64461_CPTWAR);
222                 fb_writew(red >> 10, HD64461_CPTWDR);
223                 fb_writew(green >> 10, HD64461_CPTWDR);
224                 fb_writew(blue >> 10, HD64461_CPTWDR);
225                 break;
226         case 16:
227                 if (regno >= 16)
228                         return 1;
229                 ((u32 *) (info->pseudo_palette))[regno] =
230                     ((red & 0xf800)) |
231                     ((green & 0xfc00) >> 5) | ((blue & 0xf800) >> 11);
232                 break;
233         }
234         return 0;
235 }
236
237 static int hitfb_sync(struct fb_info *info)
238 {
239         hitfb_accel_wait();
240
241         return 0;
242 }
243
244 static int hitfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
245 {
246         int maxy;
247
248         var->xres = info->var.xres;
249         var->xres_virtual = info->var.xres;
250         var->yres = info->var.yres;
251
252         if ((var->bits_per_pixel != 8) && (var->bits_per_pixel != 16))
253                 var->bits_per_pixel = info->var.bits_per_pixel;
254
255         if (var->yres_virtual < var->yres)
256                 var->yres_virtual = var->yres;
257
258         maxy = info->fix.smem_len / var->xres;
259
260         if (var->bits_per_pixel == 16)
261                 maxy /= 2;
262
263         if (var->yres_virtual > maxy)
264                 var->yres_virtual = maxy;
265
266         var->xoffset = 0;
267         var->yoffset = 0;
268
269         switch (var->bits_per_pixel) {
270         case 8:
271                 var->red.offset = 0;
272                 var->red.length = 8;
273                 var->green.offset = 0;
274                 var->green.length = 8;
275                 var->blue.offset = 0;
276                 var->blue.length = 8;
277                 var->transp.offset = 0;
278                 var->transp.length = 0;
279                 break;
280         case 16:                /* RGB 565 */
281                 var->red.offset = 11;
282                 var->red.length = 5;
283                 var->green.offset = 5;
284                 var->green.length = 6;
285                 var->blue.offset = 0;
286                 var->blue.length = 5;
287                 var->transp.offset = 0;
288                 var->transp.length = 0;
289                 break;
290         }
291
292         return 0;
293 }
294
295 static int hitfb_set_par(struct fb_info *info)
296 {
297         unsigned short ldr3;
298
299         switch (info->var.bits_per_pixel) {
300         case 8:
301                 info->fix.line_length = info->var.xres;
302                 info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
303                 info->fix.ypanstep = 16;
304                 break;
305         case 16:
306                 info->fix.line_length = info->var.xres*2;
307                 info->fix.visual = FB_VISUAL_TRUECOLOR;
308                 info->fix.ypanstep = 8;
309                 break;
310         }
311
312         fb_writew(info->fix.line_length, HD64461_LCDCLOR);
313         ldr3 = fb_readw(HD64461_LDR3);
314         ldr3 &= ~15;
315         ldr3 |= (info->var.bits_per_pixel == 8) ? 4 : 8;
316         fb_writew(ldr3, HD64461_LDR3);
317         return 0;
318 }
319
320 static struct fb_ops hitfb_ops = {
321         .owner          = THIS_MODULE,
322         .fb_check_var   = hitfb_check_var,
323         .fb_set_par             = hitfb_set_par,
324         .fb_setcolreg   = hitfb_setcolreg,
325         .fb_blank       = hitfb_blank,
326         .fb_sync        = hitfb_sync,
327         .fb_pan_display = hitfb_pan_display,
328         .fb_fillrect    = hitfb_fillrect,
329         .fb_copyarea    = hitfb_copyarea,
330         .fb_imageblit   = cfb_imageblit,
331 };
332
333 static int __init hitfb_probe(struct platform_device *dev)
334 {
335         unsigned short lcdclor, ldr3, ldvndr;
336
337         if (fb_get_options("hitfb", NULL))
338                 return -ENODEV;
339
340         hitfb_fix.mmio_start = CONFIG_HD64461_IOBASE+0x1000;
341         hitfb_fix.mmio_len = 0x1000;
342         hitfb_fix.smem_start = CONFIG_HD64461_IOBASE + 0x02000000;
343         hitfb_fix.smem_len = 512 * 1024;
344
345         lcdclor = fb_readw(HD64461_LCDCLOR);
346         ldvndr = fb_readw(HD64461_LDVNDR);
347         ldr3 = fb_readw(HD64461_LDR3);
348
349         switch (ldr3 & 15) {
350         default:
351         case 4:
352                 hitfb_var.bits_per_pixel = 8;
353                 hitfb_var.xres = lcdclor;
354                 break;
355         case 8:
356                 hitfb_var.bits_per_pixel = 16;
357                 hitfb_var.xres = lcdclor / 2;
358                 break;
359         }
360         hitfb_fix.line_length = lcdclor;
361         hitfb_fix.visual = (hitfb_var.bits_per_pixel == 8) ?
362             FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
363         hitfb_var.yres = ldvndr + 1;
364         hitfb_var.xres_virtual = hitfb_var.xres;
365         hitfb_var.yres_virtual = hitfb_fix.smem_len / lcdclor;
366         switch (hitfb_var.bits_per_pixel) {
367         case 8:
368                 hitfb_var.red.offset = 0;
369                 hitfb_var.red.length = 8;
370                 hitfb_var.green.offset = 0;
371                 hitfb_var.green.length = 8;
372                 hitfb_var.blue.offset = 0;
373                 hitfb_var.blue.length = 8;
374                 hitfb_var.transp.offset = 0;
375                 hitfb_var.transp.length = 0;
376                 break;
377         case 16:                /* RGB 565 */
378                 hitfb_var.red.offset = 11;
379                 hitfb_var.red.length = 5;
380                 hitfb_var.green.offset = 5;
381                 hitfb_var.green.length = 6;
382                 hitfb_var.blue.offset = 0;
383                 hitfb_var.blue.length = 5;
384                 hitfb_var.transp.offset = 0;
385                 hitfb_var.transp.length = 0;
386                 break;
387         }
388
389         fb_info.fbops = &hitfb_ops;
390         fb_info.var = hitfb_var;
391         fb_info.fix = hitfb_fix;
392         fb_info.pseudo_palette = pseudo_palette;
393         fb_info.flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN |
394                 FBINFO_HWACCEL_FILLRECT | FBINFO_HWACCEL_COPYAREA;
395
396         fb_info.screen_base = (void *)hitfb_fix.smem_start;
397
398         fb_alloc_cmap(&fb_info.cmap, 256, 0);
399
400         if (register_framebuffer(&fb_info) < 0)
401                 return -EINVAL;
402
403         printk(KERN_INFO "fb%d: %s frame buffer device\n",
404                fb_info.node, fb_info.fix.id);
405         return 0;
406 }
407
408 static int __devexit hitfb_remove(struct platform_device *dev)
409 {
410         return unregister_framebuffer(&fb_info);
411 }
412
413 #ifdef CONFIG_PM
414 static int hitfb_suspend(struct platform_device *dev, pm_message_t state)
415 {
416         u16 v;
417
418         hitfb_blank(1,0);
419         v = fb_readw(HD64461_STBCR);
420         v |= HD64461_STBCR_SLCKE_IST;
421         fb_writew(v, HD64461_STBCR);
422
423         return 0;
424 }
425
426 static int hitfb_resume(struct platform_device *dev)
427 {
428         u16 v;
429
430         v = fb_readw(HD64461_STBCR);
431         v &= ~HD64461_STBCR_SLCKE_OST;
432         msleep(100);
433         v = fb_readw(HD64461_STBCR);
434         v &= ~HD64461_STBCR_SLCKE_IST;
435         fb_writew(v, HD64461_STBCR);
436         hitfb_blank(0,0);
437
438         return 0;
439 }
440 #endif
441
442 static struct platform_driver hitfb_driver = {
443         .probe          = hitfb_probe,
444         .remove         = __devexit_p(hitfb_remove),
445 #ifdef CONFIG_PM
446         .suspend        = hitfb_suspend,
447         .resume         = hitfb_resume,
448 #endif
449         .driver         = {
450                 .name   = "hitfb",
451         },
452 };
453
454 static struct platform_device hitfb_device = {
455         .name   = "hitfb",
456         .id     = -1,
457 };
458
459 static int __init hitfb_init(void)
460 {
461         int ret;
462
463         ret = platform_driver_register(&hitfb_driver);
464         if (!ret) {
465                 ret = platform_device_register(&hitfb_device);
466                 if (ret)
467                         platform_driver_unregister(&hitfb_driver);
468         }
469         return ret;
470 }
471
472
473 static void __exit hitfb_exit(void)
474 {
475         platform_device_unregister(&hitfb_device);
476         platform_driver_unregister(&hitfb_driver);
477 }
478
479 module_init(hitfb_init);
480 module_exit(hitfb_exit);
481
482 MODULE_LICENSE("GPL");