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