Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[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 <linux/pci.h>
30 #include <asm/io.h>
31 #include <asm/prom.h>
32
33 #ifdef CONFIG_PPC64
34 #include <asm/pci-bridge.h>
35 #endif
36
37 #ifdef CONFIG_PPC32
38 #include <asm/bootx.h>
39 #endif
40
41 #include "macmodes.h"
42
43 /* Supported palette hacks */
44 enum {
45         cmap_unknown,
46         cmap_m64,               /* ATI Mach64 */
47         cmap_r128,              /* ATI Rage128 */
48         cmap_M3A,               /* ATI Rage Mobility M3 Head A */
49         cmap_M3B,               /* ATI Rage Mobility M3 Head B */
50         cmap_radeon,            /* ATI Radeon */
51         cmap_gxt2000,           /* IBM GXT2000 */
52 };
53
54 struct offb_par {
55         volatile void __iomem *cmap_adr;
56         volatile void __iomem *cmap_data;
57         int cmap_type;
58         int blanked;
59 };
60
61 struct offb_par default_par;
62
63     /*
64      *  Interface used by the world
65      */
66
67 int offb_init(void);
68
69 static int offb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
70                           u_int transp, struct fb_info *info);
71 static int offb_blank(int blank, struct fb_info *info);
72
73 #ifdef CONFIG_PPC32
74 extern boot_infos_t *boot_infos;
75 #endif
76
77 static void offb_init_nodriver(struct device_node *);
78 static void offb_init_fb(const char *name, const char *full_name,
79                          int width, int height, int depth, int pitch,
80                          unsigned long address, struct device_node *dp);
81
82 static struct fb_ops offb_ops = {
83         .owner          = THIS_MODULE,
84         .fb_setcolreg   = offb_setcolreg,
85         .fb_blank       = offb_blank,
86         .fb_fillrect    = cfb_fillrect,
87         .fb_copyarea    = cfb_copyarea,
88         .fb_imageblit   = cfb_imageblit,
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
227 #if defined(CONFIG_BOOTX_TEXT) && defined(CONFIG_PPC32)
228         struct device_node *macos_display = NULL;
229 #endif
230         if (fb_get_options("offb", NULL))
231                 return -ENODEV;
232
233 #if defined(CONFIG_BOOTX_TEXT) && defined(CONFIG_PPC32)
234         /* If we're booted from BootX... */
235         if (boot_infos != 0) {
236                 unsigned long addr =
237                     (unsigned long) boot_infos->dispDeviceBase;
238                 u32 *addrp;
239                 u64 daddr, dsize;
240                 unsigned int flags;
241
242                 /* find the device node corresponding to the macos display */
243                 while ((dp = of_find_node_by_type(dp, "display"))) {
244                         int i;
245
246                         /*
247                          * Look for an AAPL,address property first.
248                          */
249                         unsigned int na;
250                         unsigned int *ap =
251                                 (unsigned int *)get_property(dp, "AAPL,address",
252                                                              &na);
253                         if (ap != 0) {
254                                 for (na /= sizeof(unsigned int); na > 0;
255                                      --na, ++ap)
256                                         if (*ap <= addr &&
257                                             addr < *ap + 0x1000000) {
258                                                 macos_display = dp;
259                                                 goto foundit;
260                                         }
261                         }
262
263                         /*
264                          * See if the display address is in one of the address
265                          * ranges for this display.
266                          */
267                         i = 0;
268                         for (;;) {
269                                 addrp = of_get_address(dp, i++, &dsize, &flags);
270                                 if (addrp == NULL)
271                                         break;
272                                 if (!(flags & IORESOURCE_MEM))
273                                         continue;
274                                 daddr = of_translate_address(dp, addrp);
275                                 if (daddr == OF_BAD_ADDR)
276                                         continue;
277                                 if (daddr <= addr && addr < (daddr + dsize)) {
278                                         macos_display = dp;
279                                         goto foundit;
280                                 }
281                         }
282                 foundit:
283                         if (macos_display) {
284                                 printk(KERN_INFO "MacOS display is %s\n",
285                                        dp->full_name);
286                                 break;
287                         }
288                 }
289
290                 /* initialize it */
291                 offb_init_fb(macos_display ? macos_display->
292                              name : "MacOS display",
293                              macos_display ? macos_display->
294                              full_name : "MacOS display",
295                              boot_infos->dispDeviceRect[2],
296                              boot_infos->dispDeviceRect[3],
297                              boot_infos->dispDeviceDepth,
298                              boot_infos->dispDeviceRowBytes, addr, NULL);
299         }
300 #endif /* defined(CONFIG_BOOTX_TEXT) && defined(CONFIG_PPC32) */
301
302         for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
303                 if (get_property(dp, "linux,opened", NULL) &&
304                     get_property(dp, "linux,boot-display", NULL)) {
305                         boot_disp = dp;
306                         offb_init_nodriver(dp);
307                 }
308         }
309         for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
310                 if (get_property(dp, "linux,opened", NULL) &&
311                     dp != boot_disp)
312                         offb_init_nodriver(dp);
313         }
314
315         return 0;
316 }
317
318
319 static void __init offb_init_nodriver(struct device_node *dp)
320 {
321         int *pp, i;
322         unsigned int len;
323         int width = 640, height = 480, depth = 8, pitch;
324         unsigned  flags, rsize, *up, addr_prop = 0;
325         unsigned long max_size = 0;
326         u64 rstart, address = OF_BAD_ADDR;
327         u32 *addrp;
328         u64 asize;
329
330         if ((pp = (int *) get_property(dp, "depth", &len)) != NULL
331             && len == sizeof(int))
332                 depth = *pp;
333         if ((pp = (int *) get_property(dp, "width", &len)) != NULL
334             && len == sizeof(int))
335                 width = *pp;
336         if ((pp = (int *) get_property(dp, "height", &len)) != NULL
337             && len == sizeof(int))
338                 height = *pp;
339         if ((pp = (int *) get_property(dp, "linebytes", &len)) != NULL
340             && len == sizeof(int)) {
341                 pitch = *pp;
342                 if (pitch == 1)
343                         pitch = 0x1000;
344         } else
345                 pitch = width;
346
347         rsize = (unsigned long)pitch * (unsigned long)height *
348                 (unsigned long)(depth / 8);
349
350         /* Ok, now we try to figure out the address of the framebuffer.
351          *
352          * Unfortunately, Open Firmware doesn't provide a standard way to do
353          * so. All we can do is a dodgy heuristic that happens to work in
354          * practice. On most machines, the "address" property contains what
355          * we need, though not on Matrox cards found in IBM machines. What I've
356          * found that appears to give good results is to go through the PCI
357          * ranges and pick one that is both big enough and if possible encloses
358          * the "address" property. If none match, we pick the biggest
359          */
360         up = (unsigned int *) get_property(dp, "address", &len);
361         if (up && len == sizeof(unsigned int))
362                 addr_prop = *up;
363
364         for (i = 0; (addrp = of_get_address(dp, i, &asize, &flags))
365                      != NULL; i++) {
366                 int match_addrp = 0;
367
368                 if (!(flags & IORESOURCE_MEM))
369                         continue;
370                 if (asize < rsize)
371                         continue;
372                 rstart = of_translate_address(dp, addrp);
373                 if (rstart == OF_BAD_ADDR)
374                         continue;
375                 if (addr_prop && (rstart <= addr_prop) &&
376                     ((rstart + rsize) > addr_prop))
377                         match_addrp = 1;
378                 if (match_addrp) {
379                         address = addr_prop;
380                         break;
381                 }
382                 if (rsize > max_size) {
383                         max_size = rsize;
384                         address = OF_BAD_ADDR;
385                 }
386                 if (address == OF_BAD_ADDR)
387                         address = rstart;
388         }
389         if (address == OF_BAD_ADDR && addr_prop)
390                 address = (u_long)addr_prop;
391         if (address != OF_BAD_ADDR) {
392                 /* kludge for valkyrie */
393                 if (strcmp(dp->name, "valkyrie") == 0)
394                         address += 0x1000;
395                 offb_init_fb(dp->name, dp->full_name, width, height, depth,
396                              pitch, address, dp);
397         }
398 }
399
400 static void __init offb_init_fb(const char *name, const char *full_name,
401                                 int width, int height, int depth,
402                                 int pitch, unsigned long address,
403                                 struct device_node *dp)
404 {
405         unsigned long res_size = pitch * height * depth / 8;
406         struct offb_par *par = &default_par;
407         unsigned long res_start = address;
408         struct fb_fix_screeninfo *fix;
409         struct fb_var_screeninfo *var;
410         struct fb_info *info;
411         int size;
412
413         if (!request_mem_region(res_start, res_size, "offb"))
414                 return;
415
416         printk(KERN_INFO
417                "Using unsupported %dx%d %s at %lx, depth=%d, pitch=%d\n",
418                width, height, name, address, depth, pitch);
419         if (depth != 8 && depth != 16 && depth != 32) {
420                 printk(KERN_ERR "%s: can't use depth = %d\n", full_name,
421                        depth);
422                 release_mem_region(res_start, res_size);
423                 return;
424         }
425
426         size = sizeof(struct fb_info) + sizeof(u32) * 17;
427
428         info = kmalloc(size, GFP_ATOMIC);
429         
430         if (info == 0) {
431                 release_mem_region(res_start, res_size);
432                 return;
433         }
434         memset(info, 0, size);
435
436         fix = &info->fix;
437         var = &info->var;
438
439         strcpy(fix->id, "OFfb ");
440         strncat(fix->id, name, sizeof(fix->id) - sizeof("OFfb "));
441         fix->id[sizeof(fix->id) - 1] = '\0';
442
443         var->xres = var->xres_virtual = width;
444         var->yres = var->yres_virtual = height;
445         fix->line_length = pitch;
446
447         fix->smem_start = address;
448         fix->smem_len = pitch * height;
449         fix->type = FB_TYPE_PACKED_PIXELS;
450         fix->type_aux = 0;
451
452         par->cmap_type = cmap_unknown;
453         if (depth == 8) {
454
455                 /* Palette hacks disabled for now */
456 #if 0
457                 if (dp && !strncmp(name, "ATY,Rage128", 11)) {
458                         unsigned long regbase = dp->addrs[2].address;
459                         par->cmap_adr = ioremap(regbase, 0x1FFF);
460                         par->cmap_type = cmap_r128;
461                 } else if (dp && (!strncmp(name, "ATY,RageM3pA", 12)
462                                   || !strncmp(name, "ATY,RageM3p12A", 14))) {
463                         unsigned long regbase =
464                             dp->parent->addrs[2].address;
465                         par->cmap_adr = ioremap(regbase, 0x1FFF);
466                         par->cmap_type = cmap_M3A;
467                 } else if (dp && !strncmp(name, "ATY,RageM3pB", 12)) {
468                         unsigned long regbase =
469                             dp->parent->addrs[2].address;
470                         par->cmap_adr = ioremap(regbase, 0x1FFF);
471                         par->cmap_type = cmap_M3B;
472                 } else if (dp && !strncmp(name, "ATY,Rage6", 9)) {
473                         unsigned long regbase = dp->addrs[1].address;
474                         par->cmap_adr = ioremap(regbase, 0x1FFF);
475                         par->cmap_type = cmap_radeon;
476                 } else if (!strncmp(name, "ATY,", 4)) {
477                         unsigned long base = address & 0xff000000UL;
478                         par->cmap_adr =
479                             ioremap(base + 0x7ff000, 0x1000) + 0xcc0;
480                         par->cmap_data = par->cmap_adr + 1;
481                         par->cmap_type = cmap_m64;
482                 } else if (device_is_compatible(dp, "pci1014,b7")) {
483                         unsigned long regbase = dp->addrs[0].address;
484                         par->cmap_adr = ioremap(regbase + 0x6000, 0x1000);
485                         par->cmap_type = cmap_gxt2000;
486                 }
487 #endif
488                 fix->visual = par->cmap_adr ? FB_VISUAL_PSEUDOCOLOR
489                     : FB_VISUAL_STATIC_PSEUDOCOLOR;
490         } else
491                 fix->visual =   /* par->cmap_adr ? FB_VISUAL_DIRECTCOLOR
492                                    : */ FB_VISUAL_TRUECOLOR;
493
494         var->xoffset = var->yoffset = 0;
495         var->bits_per_pixel = depth;
496         switch (depth) {
497         case 8:
498                 var->bits_per_pixel = 8;
499                 var->red.offset = 0;
500                 var->red.length = 8;
501                 var->green.offset = 0;
502                 var->green.length = 8;
503                 var->blue.offset = 0;
504                 var->blue.length = 8;
505                 var->transp.offset = 0;
506                 var->transp.length = 0;
507                 break;
508         case 16:                /* RGB 555 */
509                 var->bits_per_pixel = 16;
510                 var->red.offset = 10;
511                 var->red.length = 5;
512                 var->green.offset = 5;
513                 var->green.length = 5;
514                 var->blue.offset = 0;
515                 var->blue.length = 5;
516                 var->transp.offset = 0;
517                 var->transp.length = 0;
518                 break;
519         case 32:                /* RGB 888 */
520                 var->bits_per_pixel = 32;
521                 var->red.offset = 16;
522                 var->red.length = 8;
523                 var->green.offset = 8;
524                 var->green.length = 8;
525                 var->blue.offset = 0;
526                 var->blue.length = 8;
527                 var->transp.offset = 24;
528                 var->transp.length = 8;
529                 break;
530         }
531         var->red.msb_right = var->green.msb_right = var->blue.msb_right =
532             var->transp.msb_right = 0;
533         var->grayscale = 0;
534         var->nonstd = 0;
535         var->activate = 0;
536         var->height = var->width = -1;
537         var->pixclock = 10000;
538         var->left_margin = var->right_margin = 16;
539         var->upper_margin = var->lower_margin = 16;
540         var->hsync_len = var->vsync_len = 8;
541         var->sync = 0;
542         var->vmode = FB_VMODE_NONINTERLACED;
543
544         info->fbops = &offb_ops;
545         info->screen_base = ioremap(address, fix->smem_len);
546         info->par = par;
547         info->pseudo_palette = (void *) (info + 1);
548         info->flags = FBINFO_DEFAULT;
549
550         fb_alloc_cmap(&info->cmap, 256, 0);
551
552         if (register_framebuffer(info) < 0) {
553                 kfree(info);
554                 release_mem_region(res_start, res_size);
555                 return;
556         }
557
558         printk(KERN_INFO "fb%d: Open Firmware frame buffer device on %s\n",
559                info->node, full_name);
560 }
561
562 module_init(offb_init);
563 MODULE_LICENSE("GPL");