vserver 1.9.3
[linux-2.6.git] / drivers / video / offb.c
1 /*
2  *  linux/drivers/video/offb.c -- Open Firmware based frame buffer device
3  *
4  *      Copyright (C) 1997 Geert Uytterhoeven
5  *
6  *  This driver is partly based on the PowerMac console driver:
7  *
8  *      Copyright (C) 1996 Paul Mackerras
9  *
10  *  This file is subject to the terms and conditions of the GNU General Public
11  *  License. See the file COPYING in the main directory of this archive for
12  *  more details.
13  */
14
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/string.h>
20 #include <linux/mm.h>
21 #include <linux/tty.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/fb.h>
27 #include <linux/init.h>
28 #include <linux/ioport.h>
29 #include <asm/io.h>
30 #include <asm/prom.h>
31
32 #ifdef CONFIG_PPC32
33 #include <asm/bootx.h>
34 #endif
35
36 #include "macmodes.h"
37
38 /* Supported palette hacks */
39 enum {
40         cmap_unknown,
41         cmap_m64,               /* ATI Mach64 */
42         cmap_r128,              /* ATI Rage128 */
43         cmap_M3A,               /* ATI Rage Mobility M3 Head A */
44         cmap_M3B,               /* ATI Rage Mobility M3 Head B */
45         cmap_radeon,            /* ATI Radeon */
46         cmap_gxt2000,           /* IBM GXT2000 */
47 };
48
49 struct offb_par {
50         volatile unsigned char *cmap_adr;
51         volatile unsigned char *cmap_data;
52         int cmap_type;
53         int blanked;
54 };
55
56 struct offb_par default_par;
57
58 #ifdef __powerpc__
59 #define mach_eieio()    eieio()
60 #else
61 #define mach_eieio()    do {} while (0)
62 #endif
63
64     /*
65      *  Interface used by the world
66      */
67
68 int offb_init(void);
69
70 static int offb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
71                           u_int transp, struct fb_info *info);
72 static int offb_blank(int blank, struct fb_info *info);
73
74 #ifdef CONFIG_PPC32
75 extern boot_infos_t *boot_infos;
76 #endif
77
78 static void offb_init_nodriver(struct device_node *);
79 static void offb_init_fb(const char *name, const char *full_name,
80                          int width, int height, int depth, int pitch,
81                          unsigned long address, struct device_node *dp);
82
83 static struct fb_ops offb_ops = {
84         .owner          = THIS_MODULE,
85         .fb_setcolreg   = offb_setcolreg,
86         .fb_blank       = offb_blank,
87         .fb_fillrect    = cfb_fillrect,
88         .fb_copyarea    = cfb_copyarea,
89         .fb_imageblit   = cfb_imageblit,
90         .fb_cursor      = soft_cursor,
91 };
92
93     /*
94      *  Set a single color register. The values supplied are already
95      *  rounded down to the hardware's capabilities (according to the
96      *  entries in the var structure). Return != 0 for invalid regno.
97      */
98
99 static int offb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
100                           u_int transp, struct fb_info *info)
101 {
102         struct offb_par *par = (struct offb_par *) info->par;
103
104         if (!par->cmap_adr || regno > 255)
105                 return 1;
106
107         red >>= 8;
108         green >>= 8;
109         blue >>= 8;
110
111         switch (par->cmap_type) {
112         case cmap_m64:
113                 *par->cmap_adr = regno;
114                 mach_eieio();
115                 *par->cmap_data = red;
116                 mach_eieio();
117                 *par->cmap_data = green;
118                 mach_eieio();
119                 *par->cmap_data = blue;
120                 mach_eieio();
121                 break;
122         case cmap_M3A:
123                 /* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
124                 out_le32((unsigned *) (par->cmap_adr + 0x58),
125                          in_le32((unsigned *) (par->cmap_adr +
126                                                0x58)) & ~0x20);
127         case cmap_r128:
128                 /* Set palette index & data */
129                 out_8(par->cmap_adr + 0xb0, regno);
130                 out_le32((unsigned *) (par->cmap_adr + 0xb4),
131                          (red << 16 | green << 8 | blue));
132                 break;
133         case cmap_M3B:
134                 /* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
135                 out_le32((unsigned *) (par->cmap_adr + 0x58),
136                          in_le32((unsigned *) (par->cmap_adr +
137                                                0x58)) | 0x20);
138                 /* Set palette index & data */
139                 out_8(par->cmap_adr + 0xb0, regno);
140                 out_le32((unsigned *) (par->cmap_adr + 0xb4),
141                          (red << 16 | green << 8 | blue));
142                 break;
143         case cmap_radeon:
144                 /* Set palette index & data (could be smarter) */
145                 out_8(par->cmap_adr + 0xb0, regno);
146                 out_le32((unsigned *) (par->cmap_adr + 0xb4),
147                          (red << 16 | green << 8 | blue));
148                 break;
149         case cmap_gxt2000:
150                 out_le32((unsigned *) par->cmap_adr + regno,
151                          (red << 16 | green << 8 | blue));
152                 break;
153         }
154
155         if (regno < 16)
156                 switch (info->var.bits_per_pixel) {
157                 case 16:
158                         ((u16 *) (info->pseudo_palette))[regno] =
159                             (regno << 10) | (regno << 5) | regno;
160                         break;
161                 case 32:
162                         {
163                                 int i = (regno << 8) | regno;
164                                 ((u32 *) (info->pseudo_palette))[regno] =
165                                     (i << 16) | i;
166                                 break;
167                         }
168                 }
169         return 0;
170 }
171
172     /*
173      *  Blank the display.
174      */
175
176 static int offb_blank(int blank, struct fb_info *info)
177 {
178         struct offb_par *par = (struct offb_par *) info->par;
179         int i, j;
180
181         if (!par->cmap_adr)
182                 return 0;
183
184         if (!par->blanked)
185                 if (!blank)
186                         return 0;
187
188         par->blanked = blank;
189
190         if (blank)
191                 for (i = 0; i < 256; i++) {
192                         switch (par->cmap_type) {
193                         case cmap_m64:
194                                 *par->cmap_adr = i;
195                                 mach_eieio();
196                                 for (j = 0; j < 3; j++) {
197                                         *par->cmap_data = 0;
198                                         mach_eieio();
199                                 }
200                                 break;
201                         case cmap_M3A:
202                                 /* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
203                                 out_le32((unsigned *) (par->cmap_adr +
204                                                        0x58),
205                                          in_le32((unsigned *) (par->
206                                                                cmap_adr +
207                                                                0x58)) &
208                                          ~0x20);
209                         case cmap_r128:
210                                 /* Set palette index & data */
211                                 out_8(par->cmap_adr + 0xb0, i);
212                                 out_le32((unsigned *) (par->cmap_adr +
213                                                        0xb4), 0);
214                                 break;
215                         case cmap_M3B:
216                                 /* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
217                                 out_le32((unsigned *) (par->cmap_adr +
218                                                        0x58),
219                                          in_le32((unsigned *) (par->
220                                                                cmap_adr +
221                                                                0x58)) |
222                                          0x20);
223                                 /* Set palette index & data */
224                                 out_8(par->cmap_adr + 0xb0, i);
225                                 out_le32((unsigned *) (par->cmap_adr +
226                                                        0xb4), 0);
227                                 break;
228                         case cmap_radeon:
229                                 out_8(par->cmap_adr + 0xb0, i);
230                                 out_le32((unsigned *) (par->cmap_adr +
231                                                        0xb4), 0);
232                                 break;
233                         case cmap_gxt2000:
234                                 out_le32((unsigned *) par->cmap_adr + i,
235                                          0);
236                                 break;
237                         }
238         } else
239                 fb_set_cmap(&info->cmap, info);
240         return 0;
241 }
242
243     /*
244      *  Initialisation
245      */
246
247 int __init offb_init(void)
248 {
249         struct device_node *dp = NULL, *boot_disp = NULL;
250 #if defined(CONFIG_BOOTX_TEXT) && defined(CONFIG_PPC32)
251         struct device_node *macos_display = NULL;
252 #endif
253         if (fb_get_options("offb", NULL))
254                 return -ENODEV;
255
256 #if defined(CONFIG_BOOTX_TEXT) && defined(CONFIG_PPC32)
257         /* If we're booted from BootX... */
258         if (boot_infos != 0) {
259                 unsigned long addr =
260                     (unsigned long) boot_infos->dispDeviceBase;
261                 /* find the device node corresponding to the macos display */
262                 while ((dp = of_find_node_by_type(dp, "display"))) {
263                         int i;
264                         /*
265                          * Grrr...  It looks like the MacOS ATI driver
266                          * munges the assigned-addresses property (but
267                          * the AAPL,address value is OK).
268                          */
269                         if (strncmp(dp->name, "ATY,", 4) == 0
270                             && dp->n_addrs == 1) {
271                                 unsigned int *ap =
272                                     (unsigned int *) get_property(dp,
273                                                                   "AAPL,address",
274                                                                   NULL);
275                                 if (ap != NULL) {
276                                         dp->addrs[0].address = *ap;
277                                         dp->addrs[0].size = 0x01000000;
278                                 }
279                         }
280
281                         /*
282                          * The LTPro on the Lombard powerbook has no addresses
283                          * on the display nodes, they are on their parent.
284                          */
285                         if (dp->n_addrs == 0
286                             && device_is_compatible(dp, "ATY,264LTPro")) {
287                                 int na;
288                                 unsigned int *ap = (unsigned int *)
289                                     get_property(dp, "AAPL,address", &na);
290                                 if (ap != 0)
291                                         for (na /= sizeof(unsigned int);
292                                              na > 0; --na, ++ap)
293                                                 if (*ap <= addr
294                                                     && addr <
295                                                     *ap + 0x1000000)
296                                                         goto foundit;
297                         }
298
299                         /*
300                          * See if the display address is in one of the address
301                          * ranges for this display.
302                          */
303                         for (i = 0; i < dp->n_addrs; ++i) {
304                                 if (dp->addrs[i].address <= addr
305                                     && addr <
306                                     dp->addrs[i].address +
307                                     dp->addrs[i].size)
308                                         break;
309                         }
310                         if (i < dp->n_addrs) {
311                               foundit:
312                                 printk(KERN_INFO "MacOS display is %s\n",
313                                        dp->full_name);
314                                 macos_display = dp;
315                                 break;
316                         }
317                 }
318
319                 /* initialize it */
320                 offb_init_fb(macos_display ? macos_display->
321                              name : "MacOS display",
322                              macos_display ? macos_display->
323                              full_name : "MacOS display",
324                              boot_infos->dispDeviceRect[2],
325                              boot_infos->dispDeviceRect[3],
326                              boot_infos->dispDeviceDepth,
327                              boot_infos->dispDeviceRowBytes, addr, NULL);
328         }
329 #endif /* defined(CONFIG_BOOTX_TEXT) && defined(CONFIG_PPC32) */
330
331         for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
332                 if (get_property(dp, "linux,opened", NULL) &&
333                     get_property(dp, "linux,boot-display", NULL)) {
334                         boot_disp = dp;
335                         offb_init_nodriver(dp);
336                 }
337         }
338         for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
339                 if (get_property(dp, "linux,opened", NULL) &&
340                     dp != boot_disp)
341                         offb_init_nodriver(dp);
342         }
343
344         return 0;
345 }
346
347
348 static void __init offb_init_nodriver(struct device_node *dp)
349 {
350         int *pp, i;
351         unsigned int len;
352         int width = 640, height = 480, depth = 8, pitch;
353         unsigned *up, address;
354
355         if ((pp = (int *) get_property(dp, "depth", &len)) != NULL
356             && len == sizeof(int))
357                 depth = *pp;
358         if ((pp = (int *) get_property(dp, "width", &len)) != NULL
359             && len == sizeof(int))
360                 width = *pp;
361         if ((pp = (int *) get_property(dp, "height", &len)) != NULL
362             && len == sizeof(int))
363                 height = *pp;
364         if ((pp = (int *) get_property(dp, "linebytes", &len)) != NULL
365             && len == sizeof(int)) {
366                 pitch = *pp;
367                 if (pitch == 1)
368                         pitch = 0x1000;
369         } else
370                 pitch = width;
371         if ((up = (unsigned *) get_property(dp, "address", &len)) != NULL
372             && len == sizeof(unsigned))
373                 address = (u_long) * up;
374         else {
375                 for (i = 0; i < dp->n_addrs; ++i)
376                         if (dp->addrs[i].size >=
377                             pitch * height * depth / 8)
378                                 break;
379                 if (i >= dp->n_addrs) {
380                         printk(KERN_ERR
381                                "no framebuffer address found for %s\n",
382                                dp->full_name);
383                         return;
384                 }
385
386                 address = (u_long) dp->addrs[i].address;
387
388                 /* kludge for valkyrie */
389                 if (strcmp(dp->name, "valkyrie") == 0)
390                         address += 0x1000;
391         }
392         offb_init_fb(dp->name, dp->full_name, width, height, depth,
393                      pitch, address, dp);
394
395 }
396
397 static void __init offb_init_fb(const char *name, const char *full_name,
398                                 int width, int height, int depth,
399                                 int pitch, unsigned long address,
400                                 struct device_node *dp)
401 {
402         unsigned long res_size = pitch * height * depth / 8;
403         struct offb_par *par = &default_par;
404         unsigned long res_start = address;
405         struct fb_fix_screeninfo *fix;
406         struct fb_var_screeninfo *var;
407         struct fb_info *info;
408         int size;
409
410         if (!request_mem_region(res_start, res_size, "offb"))
411                 return;
412
413         printk(KERN_INFO
414                "Using unsupported %dx%d %s at %lx, depth=%d, pitch=%d\n",
415                width, height, name, address, depth, pitch);
416         if (depth != 8 && depth != 16 && depth != 32) {
417                 printk(KERN_ERR "%s: can't use depth = %d\n", full_name,
418                        depth);
419                 release_mem_region(res_start, res_size);
420                 return;
421         }
422
423         size = sizeof(struct fb_info) + sizeof(u32) * 17;
424
425         info = kmalloc(size, GFP_ATOMIC);
426         
427         if (info == 0) {
428                 release_mem_region(res_start, res_size);
429                 return;
430         }
431         memset(info, 0, size);
432
433         fix = &info->fix;
434         var = &info->var;
435
436         strcpy(fix->id, "OFfb ");
437         strncat(fix->id, name, sizeof(fix->id) - sizeof("OFfb "));
438         fix->id[sizeof(fix->id) - 1] = '\0';
439
440         var->xres = var->xres_virtual = width;
441         var->yres = var->yres_virtual = height;
442         fix->line_length = pitch;
443
444         fix->smem_start = address;
445         fix->smem_len = pitch * height;
446         fix->type = FB_TYPE_PACKED_PIXELS;
447         fix->type_aux = 0;
448
449         par->cmap_type = cmap_unknown;
450         if (depth == 8) {
451                 /* XXX kludge for ati */
452                 if (dp && !strncmp(name, "ATY,Rage128", 11)) {
453                         unsigned long regbase = dp->addrs[2].address;
454                         par->cmap_adr = ioremap(regbase, 0x1FFF);
455                         par->cmap_type = cmap_r128;
456                 } else if (dp && (!strncmp(name, "ATY,RageM3pA", 12)
457                                   || !strncmp(name, "ATY,RageM3p12A", 14))) {
458                         unsigned long regbase =
459                             dp->parent->addrs[2].address;
460                         par->cmap_adr = ioremap(regbase, 0x1FFF);
461                         par->cmap_type = cmap_M3A;
462                 } else if (dp && !strncmp(name, "ATY,RageM3pB", 12)) {
463                         unsigned long regbase =
464                             dp->parent->addrs[2].address;
465                         par->cmap_adr = ioremap(regbase, 0x1FFF);
466                         par->cmap_type = cmap_M3B;
467                 } else if (dp && !strncmp(name, "ATY,Rage6", 9)) {
468                         unsigned long regbase = dp->addrs[1].address;
469                         par->cmap_adr = ioremap(regbase, 0x1FFF);
470                         par->cmap_type = cmap_radeon;
471                 } else if (!strncmp(name, "ATY,", 4)) {
472                         unsigned long base = address & 0xff000000UL;
473                         par->cmap_adr =
474                             ioremap(base + 0x7ff000, 0x1000) + 0xcc0;
475                         par->cmap_data = par->cmap_adr + 1;
476                         par->cmap_type = cmap_m64;
477                 } else if (device_is_compatible(dp, "pci1014,b7")) {
478                         unsigned long regbase = dp->addrs[0].address;
479                         par->cmap_adr = ioremap(regbase + 0x6000, 0x1000);
480                         par->cmap_type = cmap_gxt2000;
481                 }
482                 fix->visual = par->cmap_adr ? FB_VISUAL_PSEUDOCOLOR
483                     : FB_VISUAL_STATIC_PSEUDOCOLOR;
484         } else
485                 fix->visual =   /* par->cmap_adr ? FB_VISUAL_DIRECTCOLOR
486                                    : */ FB_VISUAL_TRUECOLOR;
487
488         var->xoffset = var->yoffset = 0;
489         var->bits_per_pixel = depth;
490         switch (depth) {
491         case 8:
492                 var->bits_per_pixel = 8;
493                 var->red.offset = 0;
494                 var->red.length = 8;
495                 var->green.offset = 0;
496                 var->green.length = 8;
497                 var->blue.offset = 0;
498                 var->blue.length = 8;
499                 var->transp.offset = 0;
500                 var->transp.length = 0;
501                 break;
502         case 16:                /* RGB 555 */
503                 var->bits_per_pixel = 16;
504                 var->red.offset = 10;
505                 var->red.length = 5;
506                 var->green.offset = 5;
507                 var->green.length = 5;
508                 var->blue.offset = 0;
509                 var->blue.length = 5;
510                 var->transp.offset = 0;
511                 var->transp.length = 0;
512                 break;
513         case 32:                /* RGB 888 */
514                 var->bits_per_pixel = 32;
515                 var->red.offset = 16;
516                 var->red.length = 8;
517                 var->green.offset = 8;
518                 var->green.length = 8;
519                 var->blue.offset = 0;
520                 var->blue.length = 8;
521                 var->transp.offset = 24;
522                 var->transp.length = 8;
523                 break;
524         }
525         var->red.msb_right = var->green.msb_right = var->blue.msb_right =
526             var->transp.msb_right = 0;
527         var->grayscale = 0;
528         var->nonstd = 0;
529         var->activate = 0;
530         var->height = var->width = -1;
531         var->pixclock = 10000;
532         var->left_margin = var->right_margin = 16;
533         var->upper_margin = var->lower_margin = 16;
534         var->hsync_len = var->vsync_len = 8;
535         var->sync = 0;
536         var->vmode = FB_VMODE_NONINTERLACED;
537
538         info->fbops = &offb_ops;
539         info->screen_base = ioremap(address, fix->smem_len);
540         info->par = par;
541         info->pseudo_palette = (void *) (info + 1);
542         info->flags = FBINFO_DEFAULT;
543
544         fb_alloc_cmap(&info->cmap, 256, 0);
545
546         if (register_framebuffer(info) < 0) {
547                 kfree(info);
548                 release_mem_region(res_start, res_size);
549                 return;
550         }
551
552         printk(KERN_INFO "fb%d: Open Firmware frame buffer device on %s\n",
553                info->node, full_name);
554 }
555
556 module_init(offb_init);
557 MODULE_LICENSE("GPL");