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