Merge branch 'for-linus' of git://git390.osdl.marist.edu/pub/scm/linux-2.6
[pandora-kernel.git] / drivers / video / hpfb.c
1 /*
2  *      HP300 Topcat framebuffer support (derived from macfb of all things)
3  *      Phil Blundell <philb@gnu.org> 1998
4  *      DIO-II, colour map and Catseye support by
5  *      Kars de Jong <jongk@linux-m68k.org>, May 2004.
6  */
7
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/errno.h>
12 #include <linux/string.h>
13 #include <linux/mm.h>
14 #include <linux/slab.h>
15 #include <linux/delay.h>
16 #include <linux/init.h>
17 #include <linux/fb.h>
18 #include <linux/dio.h>
19
20 #include <asm/io.h>
21 #include <asm/uaccess.h>
22
23 static struct fb_info fb_info = {
24         .fix = {
25                 .id             = "HP300 ",
26                 .type           = FB_TYPE_PACKED_PIXELS,
27                 .visual         = FB_VISUAL_PSEUDOCOLOR,
28                 .accel          = FB_ACCEL_NONE,
29         }
30 };
31
32 static unsigned long fb_regs;
33 static unsigned char fb_bitmask;
34
35 #define TC_NBLANK       0x4080
36 #define TC_WEN          0x4088
37 #define TC_REN          0x408c
38 #define TC_FBEN         0x4090
39 #define TC_PRR          0x40ea
40
41 /* These defines match the X window system */
42 #define RR_CLEAR        0x0
43 #define RR_COPY         0x3
44 #define RR_NOOP         0x5
45 #define RR_XOR          0x6
46 #define RR_INVERT       0xa
47 #define RR_COPYINVERTED 0xc
48 #define RR_SET          0xf
49
50 /* blitter regs */
51 #define BUSY            0x4044
52 #define WMRR            0x40ef
53 #define SOURCE_X        0x40f2
54 #define SOURCE_Y        0x40f6
55 #define DEST_X          0x40fa
56 #define DEST_Y          0x40fe
57 #define WHEIGHT         0x4106
58 #define WWIDTH          0x4102
59 #define WMOVE           0x409c
60
61 static struct fb_var_screeninfo hpfb_defined = {
62         .red            = {
63                 .length = 8,
64         },
65         .green          = {
66                 .length = 8,
67         },
68         .blue           = {
69                 .length = 8,
70         },
71         .activate       = FB_ACTIVATE_NOW,
72         .height         = -1,
73         .width          = -1,
74         .vmode          = FB_VMODE_NONINTERLACED,
75 };
76
77 static int hpfb_setcolreg(unsigned regno, unsigned red, unsigned green,
78                           unsigned blue, unsigned transp,
79                           struct fb_info *info)
80 {
81         /* use MSBs */
82         unsigned char _red  =red>>8;
83         unsigned char _green=green>>8;
84         unsigned char _blue =blue>>8;
85         unsigned char _regno=regno;
86
87         /*
88          *  Set a single color register. The values supplied are
89          *  already rounded down to the hardware's capabilities
90          *  (according to the entries in the `var' structure). Return
91          *  != 0 for invalid regno.
92          */
93
94         if (regno >= info->cmap.len)
95                 return 1;
96         
97         while (in_be16(fb_regs + 0x6002) & 0x4) udelay(1);
98
99         out_be16(fb_regs + 0x60ba, 0xff);
100
101         out_be16(fb_regs + 0x60b2, _red);
102         out_be16(fb_regs + 0x60b4, _green);
103         out_be16(fb_regs + 0x60b6, _blue);
104         out_be16(fb_regs + 0x60b8, ~_regno);
105         out_be16(fb_regs + 0x60f0, 0xff);
106
107         udelay(100);
108
109         while (in_be16(fb_regs + 0x6002) & 0x4) udelay(1);
110         out_be16(fb_regs + 0x60b2, 0);
111         out_be16(fb_regs + 0x60b4, 0);
112         out_be16(fb_regs + 0x60b6, 0);
113         out_be16(fb_regs + 0x60b8, 0);
114
115         return 0;
116 }
117
118 /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
119
120 static int hpfb_blank(int blank, struct fb_info *info)
121 {
122         out_8(fb_regs + TC_NBLANK, (blank ? 0x00 : fb_bitmask));
123
124         return 0;
125 }
126
127 static void topcat_blit(int x0, int y0, int x1, int y1, int w, int h, int rr)
128 {
129         if (rr >= 0) {
130                 while (in_8(fb_regs + BUSY) & fb_bitmask)
131                         ;
132         }
133         out_8(fb_regs + TC_FBEN, fb_bitmask);
134         if (rr >= 0) {
135                 out_8(fb_regs + TC_WEN, fb_bitmask);
136                 out_8(fb_regs + WMRR, rr);
137         }
138         out_be16(fb_regs + SOURCE_X, x0);
139         out_be16(fb_regs + SOURCE_Y, y0);
140         out_be16(fb_regs + DEST_X, x1);
141         out_be16(fb_regs + DEST_Y, y1);
142         out_be16(fb_regs + WWIDTH, w);
143         out_be16(fb_regs + WHEIGHT, h);
144         out_8(fb_regs + WMOVE, fb_bitmask);
145 }
146
147 static void hpfb_copyarea(struct fb_info *info, const struct fb_copyarea *area) 
148 {
149         topcat_blit(area->sx, area->sy, area->dx, area->dy, area->width, area->height, RR_COPY);
150 }
151
152 static void hpfb_fillrect(struct fb_info *p, const struct fb_fillrect *region)
153 {
154         u8 clr;
155
156         clr = region->color & 0xff;
157
158         while (in_8(fb_regs + BUSY) & fb_bitmask)
159                 ;
160
161         /* Foreground */
162         out_8(fb_regs + TC_WEN, fb_bitmask & clr);
163         out_8(fb_regs + WMRR, (region->rop == ROP_COPY ? RR_SET : RR_INVERT));
164
165         /* Background */
166         out_8(fb_regs + TC_WEN, fb_bitmask & ~clr);
167         out_8(fb_regs + WMRR, (region->rop == ROP_COPY ? RR_CLEAR : RR_NOOP));
168
169         topcat_blit(region->dx, region->dy, region->dx, region->dy, region->width, region->height, -1);
170 }
171
172 static int hpfb_sync(struct fb_info *info)
173 {
174         /*
175          * Since we also access the framebuffer directly, we have to wait
176          * until the block mover is finished
177          */
178         while (in_8(fb_regs + BUSY) & fb_bitmask)
179                 ;
180
181         out_8(fb_regs + TC_WEN, fb_bitmask);
182         out_8(fb_regs + TC_PRR, RR_COPY);
183         out_8(fb_regs + TC_FBEN, fb_bitmask);
184
185         return 0;
186 }
187
188 static struct fb_ops hpfb_ops = {
189         .owner          = THIS_MODULE,
190         .fb_setcolreg   = hpfb_setcolreg,
191         .fb_blank       = hpfb_blank,
192         .fb_fillrect    = hpfb_fillrect,
193         .fb_copyarea    = hpfb_copyarea,
194         .fb_imageblit   = cfb_imageblit,
195         .fb_sync        = hpfb_sync,
196 };
197
198 /* Common to all HP framebuffers */
199 #define HPFB_FBWMSB     0x05    /* Frame buffer width           */
200 #define HPFB_FBWLSB     0x07
201 #define HPFB_FBHMSB     0x09    /* Frame buffer height          */
202 #define HPFB_FBHLSB     0x0b
203 #define HPFB_DWMSB      0x0d    /* Display width                */
204 #define HPFB_DWLSB      0x0f
205 #define HPFB_DHMSB      0x11    /* Display height               */
206 #define HPFB_DHLSB      0x13
207 #define HPFB_NUMPLANES  0x5b    /* Number of colour planes      */
208 #define HPFB_FBOMSB     0x5d    /* Frame buffer offset          */
209 #define HPFB_FBOLSB     0x5f
210
211 static int __init hpfb_init_one(unsigned long phys_base, unsigned long virt_base)
212 {
213         unsigned long fboff, fb_width, fb_height, fb_start;
214
215         fb_regs = virt_base;
216         fboff = (in_8(fb_regs + HPFB_FBOMSB) << 8) | in_8(fb_regs + HPFB_FBOLSB);
217
218         fb_info.fix.smem_start = (in_8(fb_regs + fboff) << 16);
219
220         if (phys_base >= DIOII_BASE) {
221                 fb_info.fix.smem_start += phys_base;
222         }
223
224         if (DIO_SECID(fb_regs) != DIO_ID2_TOPCAT) {
225                 /* This is the magic incantation the HP X server uses to make Catseye boards work. */
226                 while (in_be16(fb_regs+0x4800) & 1)
227                         ;
228                 out_be16(fb_regs+0x4800, 0);    /* Catseye status */
229                 out_be16(fb_regs+0x4510, 0);    /* VB */
230                 out_be16(fb_regs+0x4512, 0);    /* TCNTRL */
231                 out_be16(fb_regs+0x4514, 0);    /* ACNTRL */
232                 out_be16(fb_regs+0x4516, 0);    /* PNCNTRL */
233                 out_be16(fb_regs+0x4206, 0x90); /* RUG Command/Status */
234                 out_be16(fb_regs+0x60a2, 0);    /* Overlay Mask */
235                 out_be16(fb_regs+0x60bc, 0);    /* Ram Select */
236         }
237
238         /*
239          *      Fill in the available video resolution
240          */
241         fb_width = (in_8(fb_regs + HPFB_FBWMSB) << 8) | in_8(fb_regs + HPFB_FBWLSB);
242         fb_info.fix.line_length = fb_width;
243         fb_height = (in_8(fb_regs + HPFB_FBHMSB) << 8) | in_8(fb_regs + HPFB_FBHLSB);
244         fb_info.fix.smem_len = fb_width * fb_height;
245         fb_start = (unsigned long)ioremap_writethrough(fb_info.fix.smem_start,
246                                                        fb_info.fix.smem_len);
247         hpfb_defined.xres = (in_8(fb_regs + HPFB_DWMSB) << 8) | in_8(fb_regs + HPFB_DWLSB);
248         hpfb_defined.yres = (in_8(fb_regs + HPFB_DHMSB) << 8) | in_8(fb_regs + HPFB_DHLSB);
249         hpfb_defined.xres_virtual = hpfb_defined.xres;
250         hpfb_defined.yres_virtual = hpfb_defined.yres;
251         hpfb_defined.bits_per_pixel = in_8(fb_regs + HPFB_NUMPLANES);
252
253         printk(KERN_INFO "hpfb: framebuffer at 0x%lx, mapped to 0x%lx, size %dk\n",
254                fb_info.fix.smem_start, fb_start, fb_info.fix.smem_len/1024);
255         printk(KERN_INFO "hpfb: mode is %dx%dx%d, linelength=%d\n",
256                hpfb_defined.xres, hpfb_defined.yres, hpfb_defined.bits_per_pixel, fb_info.fix.line_length);
257
258         /*
259          *      Give the hardware a bit of a prod and work out how many bits per
260          *      pixel are supported.
261          */
262         out_8(fb_regs + TC_WEN, 0xff);
263         out_8(fb_regs + TC_PRR, RR_COPY);
264         out_8(fb_regs + TC_FBEN, 0xff);
265         out_8(fb_start, 0xff);
266         fb_bitmask = in_8(fb_start);
267         out_8(fb_start, 0);
268
269         /*
270          *      Enable reading/writing of all the planes.
271          */
272         out_8(fb_regs + TC_WEN, fb_bitmask);
273         out_8(fb_regs + TC_PRR, RR_COPY);
274         out_8(fb_regs + TC_REN, fb_bitmask);
275         out_8(fb_regs + TC_FBEN, fb_bitmask);
276
277         /*
278          *      Clear the screen.
279          */
280         topcat_blit(0, 0, 0, 0, fb_width, fb_height, RR_CLEAR);
281
282         /*
283          *      Let there be consoles..
284          */
285         if (DIO_SECID(fb_regs) == DIO_ID2_TOPCAT)
286                 strcat(fb_info.fix.id, "Topcat");
287         else
288                 strcat(fb_info.fix.id, "Catseye");
289         fb_info.fbops = &hpfb_ops;
290         fb_info.flags = FBINFO_DEFAULT;
291         fb_info.var   = hpfb_defined;
292         fb_info.screen_base = (char *)fb_start;
293
294         fb_alloc_cmap(&fb_info.cmap, 1 << hpfb_defined.bits_per_pixel, 0);
295
296         if (register_framebuffer(&fb_info) < 0) {
297                 fb_dealloc_cmap(&fb_info.cmap);
298                 iounmap(fb_info.screen_base);
299                 fb_info.screen_base = NULL;
300                 return 1;
301         }
302
303         printk(KERN_INFO "fb%d: %s frame buffer device\n",
304                fb_info.node, fb_info.fix.id);
305
306         return 0;
307 }
308
309 /* 
310  * Check that the secondary ID indicates that we have some hope of working with this
311  * framebuffer.  The catseye boards are pretty much like topcats and we can muddle through.
312  */
313
314 #define topcat_sid_ok(x)  (((x) == DIO_ID2_LRCATSEYE) || ((x) == DIO_ID2_HRCCATSEYE)    \
315                            || ((x) == DIO_ID2_HRMCATSEYE) || ((x) == DIO_ID2_TOPCAT))
316
317 /* 
318  * Initialise the framebuffer
319  */
320 static int __devinit hpfb_dio_probe(struct dio_dev * d, const struct dio_device_id * ent)
321 {
322         unsigned long paddr, vaddr;
323
324         paddr = d->resource.start;
325         if (!request_mem_region(d->resource.start, d->resource.end - d->resource.start, d->name))
326                 return -EBUSY;
327
328         if (d->scode >= DIOII_SCBASE) {
329                 vaddr = (unsigned long)ioremap(paddr, d->resource.end - d->resource.start);
330         } else {
331                 vaddr = paddr + DIO_VIRADDRBASE;
332         }
333         printk(KERN_INFO "Topcat found at DIO select code %d "
334                "(secondary id %02x)\n", d->scode, (d->id >> 8) & 0xff);
335         if (hpfb_init_one(paddr, vaddr)) {
336                 if (d->scode >= DIOII_SCBASE)
337                         iounmap((void *)vaddr);
338                 return -ENOMEM;
339         }
340         return 0;
341 }
342
343 static void __devexit hpfb_remove_one(struct dio_dev *d)
344 {
345         unregister_framebuffer(&fb_info);
346         if (d->scode >= DIOII_SCBASE)
347                 iounmap((void *)fb_regs);
348         release_mem_region(d->resource.start, d->resource.end - d->resource.start);
349 }
350
351 static struct dio_device_id hpfb_dio_tbl[] = {
352     { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_LRCATSEYE) },
353     { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_HRCCATSEYE) },
354     { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_HRMCATSEYE) },
355     { DIO_ENCODE_ID(DIO_ID_FBUFFER, DIO_ID2_TOPCAT) },
356     { 0 }
357 };
358
359 static struct dio_driver hpfb_driver = {
360     .name      = "hpfb",
361     .id_table  = hpfb_dio_tbl,
362     .probe     = hpfb_dio_probe,
363     .remove    = __devexit_p(hpfb_remove_one),
364 };
365
366 int __init hpfb_init(void)
367 {
368         unsigned int sid;
369         mm_segment_t fs;
370         unsigned char i;
371         int err;
372
373         /* Topcats can be on the internal IO bus or real DIO devices.
374          * The internal variant sits at 0x560000; it has primary
375          * and secondary ID registers just like the DIO version.
376          * So we merge the two detection routines.
377          *
378          * Perhaps this #define should be in a global header file:
379          * I believe it's common to all internal fbs, not just topcat.
380          */
381 #define INTFBVADDR 0xf0560000
382 #define INTFBPADDR 0x560000
383
384         if (!MACH_IS_HP300)
385                 return -ENXIO;
386
387         if (fb_get_options("hpfb", NULL))
388                 return -ENODEV;
389
390         err = dio_register_driver(&hpfb_driver);
391         if (err)
392                 return err;
393
394         fs = get_fs();
395         set_fs(KERNEL_DS);
396         err = get_user(i, (unsigned char *)INTFBVADDR + DIO_IDOFF);
397         set_fs(fs);
398
399         if (!err && (i == DIO_ID_FBUFFER) && topcat_sid_ok(sid = DIO_SECID(INTFBVADDR))) {
400                 if (!request_mem_region(INTFBPADDR, DIO_DEVSIZE, "Internal Topcat"))
401                         return -EBUSY;
402                 printk(KERN_INFO "Internal Topcat found (secondary id %02x)\n", sid);
403                 if (hpfb_init_one(INTFBPADDR, INTFBVADDR)) {
404                         return -ENOMEM;
405                 }
406         }
407         return 0;
408 }
409
410 void __exit hpfb_cleanup_module(void)
411 {
412         dio_unregister_driver(&hpfb_driver);
413 }
414
415 module_init(hpfb_init);
416 module_exit(hpfb_cleanup_module);
417
418 MODULE_LICENSE("GPL");