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