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