blob: fe8ba364b3e85941a139a00eda947a6c40657618 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
43enum {
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
53struct offb_par {
54 volatile void __iomem *cmap_adr;
55 volatile void __iomem *cmap_data;
56 int cmap_type;
57 int blanked;
58};
59
60struct offb_par default_par;
61
62 /*
63 * Interface used by the world
64 */
65
66int offb_init(void);
67
68static int offb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
69 u_int transp, struct fb_info *info);
70static int offb_blank(int blank, struct fb_info *info);
71
72#ifdef CONFIG_PPC32
73extern boot_infos_t *boot_infos;
74#endif
75
76static void offb_init_nodriver(struct device_node *);
77static 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
81static 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
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
96static 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
165static 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
222int __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
323static 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;
Benjamin Herrenschmidtb341e322005-11-17 13:34:57 +1100328 unsigned int rsize, *up;
329 unsigned long address = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
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;
Benjamin Herrenschmidtb341e322005-11-17 13:34:57 +1100347
348 rsize = (unsigned long)pitch * (unsigned long)height *
349 (unsigned long)(depth / 8);
350
351 /* Try to match device to a PCI device in order to get a properly
352 * translated address rather then trying to decode the open firmware
353 * stuff in various incorrect ways
354 */
355#ifdef CONFIG_PCI
356 /* First try to locate the PCI device if any */
357 {
358 struct pci_dev *pdev = NULL;
359
360 for_each_pci_dev(pdev) {
361 if (dp == pci_device_to_OF_node(pdev))
362 break;
363 }
364 if (pdev) {
365 for (i = 0; i < 6 && address == 0; i++) {
366 if ((pci_resource_flags(pdev, i) &
367 IORESOURCE_MEM) &&
368 (pci_resource_len(pdev, i) >= rsize))
369 address = pci_resource_start(pdev, i);
370 }
371 pci_dev_put(pdev);
372 }
373 }
374#endif /* CONFIG_PCI */
375
376 if (address == 0 &&
377 (up = (unsigned *) get_property(dp, "address", &len)) != NULL &&
378 len == sizeof(unsigned))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 address = (u_long) * up;
Benjamin Herrenschmidtb341e322005-11-17 13:34:57 +1100380 if (address == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 for (i = 0; i < dp->n_addrs; ++i)
382 if (dp->addrs[i].size >=
383 pitch * height * depth / 8)
384 break;
385 if (i >= dp->n_addrs) {
386 printk(KERN_ERR
387 "no framebuffer address found for %s\n",
388 dp->full_name);
389 return;
390 }
391
392 address = (u_long) dp->addrs[i].address;
393
394#ifdef CONFIG_PPC64
Paul Mackerras16353172005-09-06 13:17:54 +1000395 address += ((struct pci_dn *)dp->data)->phb->pci_mem_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396#endif
397
398 /* kludge for valkyrie */
399 if (strcmp(dp->name, "valkyrie") == 0)
400 address += 0x1000;
401 }
402 offb_init_fb(dp->name, dp->full_name, width, height, depth,
403 pitch, address, dp);
404
405}
406
407static void __init offb_init_fb(const char *name, const char *full_name,
408 int width, int height, int depth,
409 int pitch, unsigned long address,
410 struct device_node *dp)
411{
412 unsigned long res_size = pitch * height * depth / 8;
413 struct offb_par *par = &default_par;
414 unsigned long res_start = address;
415 struct fb_fix_screeninfo *fix;
416 struct fb_var_screeninfo *var;
417 struct fb_info *info;
418 int size;
419
420 if (!request_mem_region(res_start, res_size, "offb"))
421 return;
422
423 printk(KERN_INFO
424 "Using unsupported %dx%d %s at %lx, depth=%d, pitch=%d\n",
425 width, height, name, address, depth, pitch);
426 if (depth != 8 && depth != 16 && depth != 32) {
427 printk(KERN_ERR "%s: can't use depth = %d\n", full_name,
428 depth);
429 release_mem_region(res_start, res_size);
430 return;
431 }
432
433 size = sizeof(struct fb_info) + sizeof(u32) * 17;
434
435 info = kmalloc(size, GFP_ATOMIC);
436
437 if (info == 0) {
438 release_mem_region(res_start, res_size);
439 return;
440 }
441 memset(info, 0, size);
442
443 fix = &info->fix;
444 var = &info->var;
445
446 strcpy(fix->id, "OFfb ");
447 strncat(fix->id, name, sizeof(fix->id) - sizeof("OFfb "));
448 fix->id[sizeof(fix->id) - 1] = '\0';
449
450 var->xres = var->xres_virtual = width;
451 var->yres = var->yres_virtual = height;
452 fix->line_length = pitch;
453
454 fix->smem_start = address;
455 fix->smem_len = pitch * height;
456 fix->type = FB_TYPE_PACKED_PIXELS;
457 fix->type_aux = 0;
458
459 par->cmap_type = cmap_unknown;
460 if (depth == 8) {
461 /* XXX kludge for ati */
462 if (dp && !strncmp(name, "ATY,Rage128", 11)) {
463 unsigned long regbase = dp->addrs[2].address;
464 par->cmap_adr = ioremap(regbase, 0x1FFF);
465 par->cmap_type = cmap_r128;
466 } else if (dp && (!strncmp(name, "ATY,RageM3pA", 12)
467 || !strncmp(name, "ATY,RageM3p12A", 14))) {
468 unsigned long regbase =
469 dp->parent->addrs[2].address;
470 par->cmap_adr = ioremap(regbase, 0x1FFF);
471 par->cmap_type = cmap_M3A;
472 } else if (dp && !strncmp(name, "ATY,RageM3pB", 12)) {
473 unsigned long regbase =
474 dp->parent->addrs[2].address;
475 par->cmap_adr = ioremap(regbase, 0x1FFF);
476 par->cmap_type = cmap_M3B;
477 } else if (dp && !strncmp(name, "ATY,Rage6", 9)) {
478 unsigned long regbase = dp->addrs[1].address;
479 par->cmap_adr = ioremap(regbase, 0x1FFF);
480 par->cmap_type = cmap_radeon;
481 } else if (!strncmp(name, "ATY,", 4)) {
482 unsigned long base = address & 0xff000000UL;
483 par->cmap_adr =
484 ioremap(base + 0x7ff000, 0x1000) + 0xcc0;
485 par->cmap_data = par->cmap_adr + 1;
486 par->cmap_type = cmap_m64;
487 } else if (device_is_compatible(dp, "pci1014,b7")) {
488 unsigned long regbase = dp->addrs[0].address;
489 par->cmap_adr = ioremap(regbase + 0x6000, 0x1000);
490 par->cmap_type = cmap_gxt2000;
491 }
492 fix->visual = par->cmap_adr ? FB_VISUAL_PSEUDOCOLOR
493 : FB_VISUAL_STATIC_PSEUDOCOLOR;
494 } else
495 fix->visual = /* par->cmap_adr ? FB_VISUAL_DIRECTCOLOR
496 : */ FB_VISUAL_TRUECOLOR;
497
498 var->xoffset = var->yoffset = 0;
499 var->bits_per_pixel = depth;
500 switch (depth) {
501 case 8:
502 var->bits_per_pixel = 8;
503 var->red.offset = 0;
504 var->red.length = 8;
505 var->green.offset = 0;
506 var->green.length = 8;
507 var->blue.offset = 0;
508 var->blue.length = 8;
509 var->transp.offset = 0;
510 var->transp.length = 0;
511 break;
512 case 16: /* RGB 555 */
513 var->bits_per_pixel = 16;
514 var->red.offset = 10;
515 var->red.length = 5;
516 var->green.offset = 5;
517 var->green.length = 5;
518 var->blue.offset = 0;
519 var->blue.length = 5;
520 var->transp.offset = 0;
521 var->transp.length = 0;
522 break;
523 case 32: /* RGB 888 */
524 var->bits_per_pixel = 32;
525 var->red.offset = 16;
526 var->red.length = 8;
527 var->green.offset = 8;
528 var->green.length = 8;
529 var->blue.offset = 0;
530 var->blue.length = 8;
531 var->transp.offset = 24;
532 var->transp.length = 8;
533 break;
534 }
535 var->red.msb_right = var->green.msb_right = var->blue.msb_right =
536 var->transp.msb_right = 0;
537 var->grayscale = 0;
538 var->nonstd = 0;
539 var->activate = 0;
540 var->height = var->width = -1;
541 var->pixclock = 10000;
542 var->left_margin = var->right_margin = 16;
543 var->upper_margin = var->lower_margin = 16;
544 var->hsync_len = var->vsync_len = 8;
545 var->sync = 0;
546 var->vmode = FB_VMODE_NONINTERLACED;
547
548 info->fbops = &offb_ops;
549 info->screen_base = ioremap(address, fix->smem_len);
550 info->par = par;
551 info->pseudo_palette = (void *) (info + 1);
552 info->flags = FBINFO_DEFAULT;
553
554 fb_alloc_cmap(&info->cmap, 256, 0);
555
556 if (register_framebuffer(info) < 0) {
557 kfree(info);
558 release_mem_region(res_start, res_size);
559 return;
560 }
561
562 printk(KERN_INFO "fb%d: Open Firmware frame buffer device on %s\n",
563 info->node, full_name);
564}
565
566module_init(offb_init);
567MODULE_LICENSE("GPL");