2 * linux/drivers/video/offb.c -- Open Firmware based frame buffer device
4 * Copyright (C) 1997 Geert Uytterhoeven
6 * This driver is partly based on the PowerMac console driver:
8 * Copyright (C) 1996 Paul Mackerras
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
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
20 #include <linux/vmalloc.h>
21 #include <linux/delay.h>
23 #include <linux/of_address.h>
24 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/ioport.h>
28 #include <linux/pci.h>
32 #include <asm/pci-bridge.h>
36 #include <asm/bootx.h>
41 /* Supported palette hacks */
44 cmap_simple, /* ATI Mach64 */
45 cmap_r128, /* ATI Rage128 */
46 cmap_M3A, /* ATI Rage Mobility M3 Head A */
47 cmap_M3B, /* ATI Rage Mobility M3 Head B */
48 cmap_radeon, /* ATI Radeon */
49 cmap_gxt2000, /* IBM GXT2000 */
50 cmap_avivo, /* ATI R5xx */
51 cmap_qemu, /* qemu vga */
55 volatile void __iomem *cmap_adr;
56 volatile void __iomem *cmap_data;
61 struct offb_par default_par;
64 extern boot_infos_t *boot_infos;
67 /* Definitions used by the Avivo palette hack */
68 #define AVIVO_DC_LUT_RW_SELECT 0x6480
69 #define AVIVO_DC_LUT_RW_MODE 0x6484
70 #define AVIVO_DC_LUT_RW_INDEX 0x6488
71 #define AVIVO_DC_LUT_SEQ_COLOR 0x648c
72 #define AVIVO_DC_LUT_PWL_DATA 0x6490
73 #define AVIVO_DC_LUT_30_COLOR 0x6494
74 #define AVIVO_DC_LUT_READ_PIPE_SELECT 0x6498
75 #define AVIVO_DC_LUT_WRITE_EN_MASK 0x649c
76 #define AVIVO_DC_LUT_AUTOFILL 0x64a0
78 #define AVIVO_DC_LUTA_CONTROL 0x64c0
79 #define AVIVO_DC_LUTA_BLACK_OFFSET_BLUE 0x64c4
80 #define AVIVO_DC_LUTA_BLACK_OFFSET_GREEN 0x64c8
81 #define AVIVO_DC_LUTA_BLACK_OFFSET_RED 0x64cc
82 #define AVIVO_DC_LUTA_WHITE_OFFSET_BLUE 0x64d0
83 #define AVIVO_DC_LUTA_WHITE_OFFSET_GREEN 0x64d4
84 #define AVIVO_DC_LUTA_WHITE_OFFSET_RED 0x64d8
86 #define AVIVO_DC_LUTB_CONTROL 0x6cc0
87 #define AVIVO_DC_LUTB_BLACK_OFFSET_BLUE 0x6cc4
88 #define AVIVO_DC_LUTB_BLACK_OFFSET_GREEN 0x6cc8
89 #define AVIVO_DC_LUTB_BLACK_OFFSET_RED 0x6ccc
90 #define AVIVO_DC_LUTB_WHITE_OFFSET_BLUE 0x6cd0
91 #define AVIVO_DC_LUTB_WHITE_OFFSET_GREEN 0x6cd4
92 #define AVIVO_DC_LUTB_WHITE_OFFSET_RED 0x6cd8
95 * Set a single color register. The values supplied are already
96 * rounded down to the hardware's capabilities (according to the
97 * entries in the var structure). Return != 0 for invalid regno.
100 static int offb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
101 u_int transp, struct fb_info *info)
103 struct offb_par *par = (struct offb_par *) info->par;
105 u32 *pal = info->pseudo_palette;
107 depth = info->var.bits_per_pixel;
109 depth = (info->var.green.length == 5) ? 15 : 16;
112 (depth == 16 && regno > 63) ||
113 (depth == 15 && regno > 31))
119 pal[regno] = (regno << 10) | (regno << 5) | regno;
122 pal[regno] = (regno << 11) | (regno << 5) | regno;
125 pal[regno] = (regno << 16) | (regno << 8) | regno;
128 i = (regno << 8) | regno;
129 pal[regno] = (i << 16) | i;
141 switch (par->cmap_type) {
143 writeb(regno, par->cmap_adr);
144 writeb(red, par->cmap_data);
145 writeb(green, par->cmap_data);
146 writeb(blue, par->cmap_data);
149 /* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
150 out_le32(par->cmap_adr + 0x58,
151 in_le32(par->cmap_adr + 0x58) & ~0x20);
153 /* Set palette index & data */
154 out_8(par->cmap_adr + 0xb0, regno);
155 out_le32(par->cmap_adr + 0xb4,
156 (red << 16 | green << 8 | blue));
159 /* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
160 out_le32(par->cmap_adr + 0x58,
161 in_le32(par->cmap_adr + 0x58) | 0x20);
162 /* Set palette index & data */
163 out_8(par->cmap_adr + 0xb0, regno);
164 out_le32(par->cmap_adr + 0xb4, (red << 16 | green << 8 | blue));
167 /* Set palette index & data (could be smarter) */
168 out_8(par->cmap_adr + 0xb0, regno);
169 out_le32(par->cmap_adr + 0xb4, (red << 16 | green << 8 | blue));
172 out_le32(((unsigned __iomem *) par->cmap_adr) + regno,
173 (red << 16 | green << 8 | blue));
176 /* Write to both LUTs for now */
177 writel(1, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
178 writeb(regno, par->cmap_adr + AVIVO_DC_LUT_RW_INDEX);
179 writel(((red) << 22) | ((green) << 12) | ((blue) << 2),
180 par->cmap_adr + AVIVO_DC_LUT_30_COLOR);
181 writel(0, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
182 writeb(regno, par->cmap_adr + AVIVO_DC_LUT_RW_INDEX);
183 writel(((red) << 22) | ((green) << 12) | ((blue) << 2),
184 par->cmap_adr + AVIVO_DC_LUT_30_COLOR);
195 static int offb_blank(int blank, struct fb_info *info)
197 struct offb_par *par = (struct offb_par *) info->par;
207 par->blanked = blank;
210 for (i = 0; i < 256; i++) {
211 switch (par->cmap_type) {
213 writeb(i, par->cmap_adr);
214 for (j = 0; j < 3; j++)
215 writeb(0, par->cmap_data);
218 /* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
219 out_le32(par->cmap_adr + 0x58,
220 in_le32(par->cmap_adr + 0x58) & ~0x20);
222 /* Set palette index & data */
223 out_8(par->cmap_adr + 0xb0, i);
224 out_le32(par->cmap_adr + 0xb4, 0);
227 /* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
228 out_le32(par->cmap_adr + 0x58,
229 in_le32(par->cmap_adr + 0x58) | 0x20);
230 /* Set palette index & data */
231 out_8(par->cmap_adr + 0xb0, i);
232 out_le32(par->cmap_adr + 0xb4, 0);
235 out_8(par->cmap_adr + 0xb0, i);
236 out_le32(par->cmap_adr + 0xb4, 0);
239 out_le32(((unsigned __iomem *) par->cmap_adr) + i,
243 writel(1, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
244 writeb(i, par->cmap_adr + AVIVO_DC_LUT_RW_INDEX);
245 writel(0, par->cmap_adr + AVIVO_DC_LUT_30_COLOR);
246 writel(0, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
247 writeb(i, par->cmap_adr + AVIVO_DC_LUT_RW_INDEX);
248 writel(0, par->cmap_adr + AVIVO_DC_LUT_30_COLOR);
252 fb_set_cmap(&info->cmap, info);
256 static int offb_set_par(struct fb_info *info)
258 struct offb_par *par = (struct offb_par *) info->par;
260 /* On avivo, initialize palette control */
261 if (par->cmap_type == cmap_avivo) {
262 writel(0, par->cmap_adr + AVIVO_DC_LUTA_CONTROL);
263 writel(0, par->cmap_adr + AVIVO_DC_LUTA_BLACK_OFFSET_BLUE);
264 writel(0, par->cmap_adr + AVIVO_DC_LUTA_BLACK_OFFSET_GREEN);
265 writel(0, par->cmap_adr + AVIVO_DC_LUTA_BLACK_OFFSET_RED);
266 writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTA_WHITE_OFFSET_BLUE);
267 writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTA_WHITE_OFFSET_GREEN);
268 writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTA_WHITE_OFFSET_RED);
269 writel(0, par->cmap_adr + AVIVO_DC_LUTB_CONTROL);
270 writel(0, par->cmap_adr + AVIVO_DC_LUTB_BLACK_OFFSET_BLUE);
271 writel(0, par->cmap_adr + AVIVO_DC_LUTB_BLACK_OFFSET_GREEN);
272 writel(0, par->cmap_adr + AVIVO_DC_LUTB_BLACK_OFFSET_RED);
273 writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTB_WHITE_OFFSET_BLUE);
274 writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTB_WHITE_OFFSET_GREEN);
275 writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTB_WHITE_OFFSET_RED);
276 writel(1, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
277 writel(0, par->cmap_adr + AVIVO_DC_LUT_RW_MODE);
278 writel(0x0000003f, par->cmap_adr + AVIVO_DC_LUT_WRITE_EN_MASK);
279 writel(0, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
280 writel(0, par->cmap_adr + AVIVO_DC_LUT_RW_MODE);
281 writel(0x0000003f, par->cmap_adr + AVIVO_DC_LUT_WRITE_EN_MASK);
286 static void offb_destroy(struct fb_info *info)
288 if (info->screen_base)
289 iounmap(info->screen_base);
290 release_mem_region(info->apertures->ranges[0].base, info->apertures->ranges[0].size);
291 framebuffer_release(info);
294 static struct fb_ops offb_ops = {
295 .owner = THIS_MODULE,
296 .fb_destroy = offb_destroy,
297 .fb_setcolreg = offb_setcolreg,
298 .fb_set_par = offb_set_par,
299 .fb_blank = offb_blank,
300 .fb_fillrect = cfb_fillrect,
301 .fb_copyarea = cfb_copyarea,
302 .fb_imageblit = cfb_imageblit,
305 static void __iomem *offb_map_reg(struct device_node *np, int index,
306 unsigned long offset, unsigned long size)
312 addrp = of_get_pci_address(np, index, &asize, &flags);
314 addrp = of_get_address(np, index, &asize, &flags);
317 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
319 if ((offset + size) > asize)
321 taddr = of_translate_address(np, addrp);
322 if (taddr == OF_BAD_ADDR)
324 return ioremap(taddr + offset, size);
327 static void offb_init_palette_hacks(struct fb_info *info, struct device_node *dp,
328 const char *name, unsigned long address)
330 struct offb_par *par = (struct offb_par *) info->par;
332 if (dp && !strncmp(name, "ATY,Rage128", 11)) {
333 par->cmap_adr = offb_map_reg(dp, 2, 0, 0x1fff);
335 par->cmap_type = cmap_r128;
336 } else if (dp && (!strncmp(name, "ATY,RageM3pA", 12)
337 || !strncmp(name, "ATY,RageM3p12A", 14))) {
338 par->cmap_adr = offb_map_reg(dp, 2, 0, 0x1fff);
340 par->cmap_type = cmap_M3A;
341 } else if (dp && !strncmp(name, "ATY,RageM3pB", 12)) {
342 par->cmap_adr = offb_map_reg(dp, 2, 0, 0x1fff);
344 par->cmap_type = cmap_M3B;
345 } else if (dp && !strncmp(name, "ATY,Rage6", 9)) {
346 par->cmap_adr = offb_map_reg(dp, 1, 0, 0x1fff);
348 par->cmap_type = cmap_radeon;
349 } else if (!strncmp(name, "ATY,", 4)) {
350 unsigned long base = address & 0xff000000UL;
352 ioremap(base + 0x7ff000, 0x1000) + 0xcc0;
353 par->cmap_data = par->cmap_adr + 1;
354 par->cmap_type = cmap_simple;
355 } else if (dp && (of_device_is_compatible(dp, "pci1014,b7") ||
356 of_device_is_compatible(dp, "pci1014,21c"))) {
357 par->cmap_adr = offb_map_reg(dp, 0, 0x6000, 0x1000);
359 par->cmap_type = cmap_gxt2000;
360 } else if (dp && !strncmp(name, "vga,Display-", 12)) {
361 /* Look for AVIVO initialized by SLOF */
362 struct device_node *pciparent = of_get_parent(dp);
363 const u32 *vid, *did;
364 vid = of_get_property(pciparent, "vendor-id", NULL);
365 did = of_get_property(pciparent, "device-id", NULL);
366 /* This will match most R5xx */
367 if (vid && did && *vid == 0x1002 &&
368 ((*did >= 0x7100 && *did < 0x7800) ||
370 par->cmap_adr = offb_map_reg(pciparent, 2, 0, 0x10000);
372 par->cmap_type = cmap_avivo;
374 of_node_put(pciparent);
375 } else if (dp && of_device_is_compatible(dp, "qemu,std-vga")) {
376 const u32 io_of_addr[3] = { 0x01000000, 0x0, 0x0 };
377 u64 io_addr = of_translate_address(dp, io_of_addr);
378 if (io_addr != OF_BAD_ADDR) {
379 par->cmap_adr = ioremap(io_addr + 0x3c8, 2);
381 par->cmap_type = cmap_simple;
382 par->cmap_data = par->cmap_adr + 1;
386 info->fix.visual = (par->cmap_type != cmap_unknown) ?
387 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_STATIC_PSEUDOCOLOR;
390 static void __init offb_init_fb(const char *name, const char *full_name,
391 int width, int height, int depth,
392 int pitch, unsigned long address,
393 int foreign_endian, struct device_node *dp)
395 unsigned long res_size = pitch * height;
396 struct offb_par *par = &default_par;
397 unsigned long res_start = address;
398 struct fb_fix_screeninfo *fix;
399 struct fb_var_screeninfo *var;
400 struct fb_info *info;
402 if (!request_mem_region(res_start, res_size, "offb"))
406 "Using unsupported %dx%d %s at %lx, depth=%d, pitch=%d\n",
407 width, height, name, address, depth, pitch);
408 if (depth != 8 && depth != 15 && depth != 16 && depth != 32) {
409 printk(KERN_ERR "%s: can't use depth = %d\n", full_name,
411 release_mem_region(res_start, res_size);
415 info = framebuffer_alloc(sizeof(u32) * 16, NULL);
418 release_mem_region(res_start, res_size);
426 strcpy(fix->id, "OFfb ");
427 strncat(fix->id, name, sizeof(fix->id) - sizeof("OFfb "));
428 fix->id[sizeof(fix->id) - 1] = '\0';
430 var->xres = var->xres_virtual = width;
431 var->yres = var->yres_virtual = height;
432 fix->line_length = pitch;
434 fix->smem_start = address;
435 fix->smem_len = pitch * height;
436 fix->type = FB_TYPE_PACKED_PIXELS;
439 par->cmap_type = cmap_unknown;
441 offb_init_palette_hacks(info, dp, name, address);
443 fix->visual = FB_VISUAL_TRUECOLOR;
445 var->xoffset = var->yoffset = 0;
448 var->bits_per_pixel = 8;
451 var->green.offset = 0;
452 var->green.length = 8;
453 var->blue.offset = 0;
454 var->blue.length = 8;
455 var->transp.offset = 0;
456 var->transp.length = 0;
458 case 15: /* RGB 555 */
459 var->bits_per_pixel = 16;
460 var->red.offset = 10;
462 var->green.offset = 5;
463 var->green.length = 5;
464 var->blue.offset = 0;
465 var->blue.length = 5;
466 var->transp.offset = 0;
467 var->transp.length = 0;
469 case 16: /* RGB 565 */
470 var->bits_per_pixel = 16;
471 var->red.offset = 11;
473 var->green.offset = 5;
474 var->green.length = 6;
475 var->blue.offset = 0;
476 var->blue.length = 5;
477 var->transp.offset = 0;
478 var->transp.length = 0;
480 case 32: /* RGB 888 */
481 var->bits_per_pixel = 32;
482 var->red.offset = 16;
484 var->green.offset = 8;
485 var->green.length = 8;
486 var->blue.offset = 0;
487 var->blue.length = 8;
488 var->transp.offset = 24;
489 var->transp.length = 8;
492 var->red.msb_right = var->green.msb_right = var->blue.msb_right =
493 var->transp.msb_right = 0;
497 var->height = var->width = -1;
498 var->pixclock = 10000;
499 var->left_margin = var->right_margin = 16;
500 var->upper_margin = var->lower_margin = 16;
501 var->hsync_len = var->vsync_len = 8;
503 var->vmode = FB_VMODE_NONINTERLACED;
505 /* set offb aperture size for generic probing */
506 info->apertures = alloc_apertures(1);
507 if (!info->apertures)
509 info->apertures->ranges[0].base = address;
510 info->apertures->ranges[0].size = fix->smem_len;
512 info->fbops = &offb_ops;
513 info->screen_base = ioremap(address, fix->smem_len);
514 info->pseudo_palette = (void *) (info + 1);
515 info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE | foreign_endian;
517 fb_alloc_cmap(&info->cmap, 256, 0);
519 if (register_framebuffer(info) < 0)
522 printk(KERN_INFO "fb%d: Open Firmware frame buffer device on %s\n",
523 info->node, full_name);
527 iounmap(info->screen_base);
529 iounmap(par->cmap_adr);
530 par->cmap_adr = NULL;
531 framebuffer_release(info);
532 release_mem_region(res_start, res_size);
536 static void __init offb_init_nodriver(struct device_node *dp, int no_real_node)
539 int i, width = 640, height = 480, depth = 8, pitch = 640;
540 unsigned int flags, rsize, addr_prop = 0;
541 unsigned long max_size = 0;
542 u64 rstart, address = OF_BAD_ADDR;
543 const u32 *pp, *addrp, *up;
545 int foreign_endian = 0;
548 if (of_get_property(dp, "little-endian", NULL))
549 foreign_endian = FBINFO_FOREIGN_ENDIAN;
551 if (of_get_property(dp, "big-endian", NULL))
552 foreign_endian = FBINFO_FOREIGN_ENDIAN;
555 pp = of_get_property(dp, "linux,bootx-depth", &len);
557 pp = of_get_property(dp, "depth", &len);
558 if (pp && len == sizeof(u32))
561 pp = of_get_property(dp, "linux,bootx-width", &len);
563 pp = of_get_property(dp, "width", &len);
564 if (pp && len == sizeof(u32))
567 pp = of_get_property(dp, "linux,bootx-height", &len);
569 pp = of_get_property(dp, "height", &len);
570 if (pp && len == sizeof(u32))
573 pp = of_get_property(dp, "linux,bootx-linebytes", &len);
575 pp = of_get_property(dp, "linebytes", &len);
576 if (pp && len == sizeof(u32) && (*pp != 0xffffffffu))
579 pitch = width * ((depth + 7) / 8);
581 rsize = (unsigned long)pitch * (unsigned long)height;
583 /* Ok, now we try to figure out the address of the framebuffer.
585 * Unfortunately, Open Firmware doesn't provide a standard way to do
586 * so. All we can do is a dodgy heuristic that happens to work in
587 * practice. On most machines, the "address" property contains what
588 * we need, though not on Matrox cards found in IBM machines. What I've
589 * found that appears to give good results is to go through the PCI
590 * ranges and pick one that is both big enough and if possible encloses
591 * the "address" property. If none match, we pick the biggest
593 up = of_get_property(dp, "linux,bootx-addr", &len);
595 up = of_get_property(dp, "address", &len);
596 if (up && len == sizeof(u32))
599 /* Hack for when BootX is passing us */
603 for (i = 0; (addrp = of_get_address(dp, i, &asize, &flags))
607 if (!(flags & IORESOURCE_MEM))
611 rstart = of_translate_address(dp, addrp);
612 if (rstart == OF_BAD_ADDR)
614 if (addr_prop && (rstart <= addr_prop) &&
615 ((rstart + asize) >= (addr_prop + rsize)))
621 if (rsize > max_size) {
623 address = OF_BAD_ADDR;
626 if (address == OF_BAD_ADDR)
630 if (address == OF_BAD_ADDR && addr_prop)
631 address = (u64)addr_prop;
632 if (address != OF_BAD_ADDR) {
633 /* kludge for valkyrie */
634 if (strcmp(dp->name, "valkyrie") == 0)
636 offb_init_fb(no_real_node ? "bootx" : dp->name,
637 no_real_node ? "display" : dp->full_name,
638 width, height, depth, pitch, address,
639 foreign_endian, no_real_node ? NULL : dp);
643 static int __init offb_init(void)
645 struct device_node *dp = NULL, *boot_disp = NULL;
647 if (fb_get_options("offb", NULL))
650 /* Check if we have a MacOS display without a node spec */
651 if (of_get_property(of_chosen, "linux,bootx-noscreen", NULL) != NULL) {
652 /* The old code tried to work out which node was the MacOS
653 * display based on the address. I'm dropping that since the
654 * lack of a node spec only happens with old BootX versions
655 * (users can update) and with this code, they'll still get
656 * a display (just not the palette hacks).
658 offb_init_nodriver(of_chosen, 1);
661 for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
662 if (of_get_property(dp, "linux,opened", NULL) &&
663 of_get_property(dp, "linux,boot-display", NULL)) {
665 offb_init_nodriver(dp, 0);
668 for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
669 if (of_get_property(dp, "linux,opened", NULL) &&
671 offb_init_nodriver(dp, 0);
678 module_init(offb_init);
679 MODULE_LICENSE("GPL");