blob: fb60a8f0cc94c8103d8dc141b25c5eecec5db39c [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/errno.h>
18#include <linux/string.h>
19#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/vmalloc.h>
21#include <linux/delay.h>
Grant Likely22ae7822010-07-29 11:49:01 -060022#include <linux/of.h>
23#include <linux/of_address.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/fb.h>
26#include <linux/init.h>
27#include <linux/ioport.h>
Paul Mackerrasf365cfd2005-11-18 16:41:49 +110028#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#ifdef CONFIG_PPC32
32#include <asm/bootx.h>
33#endif
34
35#include "macmodes.h"
36
37/* Supported palette hacks */
38enum {
39 cmap_unknown,
Benjamin Herrenschmidt9b961ed2011-12-14 13:58:29 +000040 cmap_simple, /* ATI Mach64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 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 */
Benjamin Herrenschmidt57a20d82008-07-17 15:05:22 +100046 cmap_avivo, /* ATI R5xx */
Benjamin Herrenschmidt9b961ed2011-12-14 13:58:29 +000047 cmap_qemu, /* qemu vga */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
50struct offb_par {
51 volatile void __iomem *cmap_adr;
52 volatile void __iomem *cmap_data;
53 int cmap_type;
54 int blanked;
55};
56
57struct offb_par default_par;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#ifdef CONFIG_PPC32
60extern boot_infos_t *boot_infos;
61#endif
62
Benjamin Herrenschmidt57a20d82008-07-17 15:05:22 +100063/* Definitions used by the Avivo palette hack */
64#define AVIVO_DC_LUT_RW_SELECT 0x6480
65#define AVIVO_DC_LUT_RW_MODE 0x6484
66#define AVIVO_DC_LUT_RW_INDEX 0x6488
67#define AVIVO_DC_LUT_SEQ_COLOR 0x648c
68#define AVIVO_DC_LUT_PWL_DATA 0x6490
69#define AVIVO_DC_LUT_30_COLOR 0x6494
70#define AVIVO_DC_LUT_READ_PIPE_SELECT 0x6498
71#define AVIVO_DC_LUT_WRITE_EN_MASK 0x649c
72#define AVIVO_DC_LUT_AUTOFILL 0x64a0
73
74#define AVIVO_DC_LUTA_CONTROL 0x64c0
75#define AVIVO_DC_LUTA_BLACK_OFFSET_BLUE 0x64c4
76#define AVIVO_DC_LUTA_BLACK_OFFSET_GREEN 0x64c8
77#define AVIVO_DC_LUTA_BLACK_OFFSET_RED 0x64cc
78#define AVIVO_DC_LUTA_WHITE_OFFSET_BLUE 0x64d0
79#define AVIVO_DC_LUTA_WHITE_OFFSET_GREEN 0x64d4
80#define AVIVO_DC_LUTA_WHITE_OFFSET_RED 0x64d8
81
82#define AVIVO_DC_LUTB_CONTROL 0x6cc0
83#define AVIVO_DC_LUTB_BLACK_OFFSET_BLUE 0x6cc4
84#define AVIVO_DC_LUTB_BLACK_OFFSET_GREEN 0x6cc8
85#define AVIVO_DC_LUTB_BLACK_OFFSET_RED 0x6ccc
86#define AVIVO_DC_LUTB_WHITE_OFFSET_BLUE 0x6cd0
87#define AVIVO_DC_LUTB_WHITE_OFFSET_GREEN 0x6cd4
88#define AVIVO_DC_LUTB_WHITE_OFFSET_RED 0x6cd8
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
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
Benjamin Herrenschmidt1bb0b7d2011-12-28 00:10:16 +0000101 if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
102 u32 *pal = info->pseudo_palette;
103 u32 cr = red >> (16 - info->var.red.length);
104 u32 cg = green >> (16 - info->var.green.length);
105 u32 cb = blue >> (16 - info->var.blue.length);
106 u32 value;
Benjamin Herrenschmidtab134462006-07-03 17:19:48 +1000107
Benjamin Herrenschmidt1bb0b7d2011-12-28 00:10:16 +0000108 if (regno >= 16)
109 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Benjamin Herrenschmidt1bb0b7d2011-12-28 00:10:16 +0000111 value = (cr << info->var.red.offset) |
112 (cg << info->var.green.offset) |
113 (cb << info->var.blue.offset);
114 if (info->var.transp.length > 0) {
115 u32 mask = (1 << info->var.transp.length) - 1;
116 mask <<= info->var.transp.offset;
117 value |= mask;
Benjamin Herrenschmidtab134462006-07-03 17:19:48 +1000118 }
Benjamin Herrenschmidt68986c92014-06-16 19:40:20 +1000119 pal[regno] = value;
Benjamin Herrenschmidt1bb0b7d2011-12-28 00:10:16 +0000120 return 0;
Benjamin Herrenschmidtab134462006-07-03 17:19:48 +1000121 }
122
Benjamin Herrenschmidt1bb0b7d2011-12-28 00:10:16 +0000123 if (regno > 255)
124 return -EINVAL;
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 red >>= 8;
127 green >>= 8;
128 blue >>= 8;
129
Benjamin Herrenschmidtab134462006-07-03 17:19:48 +1000130 if (!par->cmap_adr)
131 return 0;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 switch (par->cmap_type) {
Benjamin Herrenschmidt9b961ed2011-12-14 13:58:29 +0000134 case cmap_simple:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 writeb(regno, par->cmap_adr);
136 writeb(red, par->cmap_data);
137 writeb(green, par->cmap_data);
138 writeb(blue, par->cmap_data);
139 break;
140 case cmap_M3A:
141 /* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
142 out_le32(par->cmap_adr + 0x58,
143 in_le32(par->cmap_adr + 0x58) & ~0x20);
144 case cmap_r128:
145 /* Set palette index & data */
146 out_8(par->cmap_adr + 0xb0, regno);
147 out_le32(par->cmap_adr + 0xb4,
148 (red << 16 | green << 8 | blue));
149 break;
150 case cmap_M3B:
151 /* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
152 out_le32(par->cmap_adr + 0x58,
153 in_le32(par->cmap_adr + 0x58) | 0x20);
154 /* Set palette index & data */
155 out_8(par->cmap_adr + 0xb0, regno);
156 out_le32(par->cmap_adr + 0xb4, (red << 16 | green << 8 | blue));
157 break;
158 case cmap_radeon:
159 /* Set palette index & data (could be smarter) */
160 out_8(par->cmap_adr + 0xb0, regno);
161 out_le32(par->cmap_adr + 0xb4, (red << 16 | green << 8 | blue));
162 break;
163 case cmap_gxt2000:
Benjamin Herrenschmidt441cbd82006-10-26 15:38:10 +1000164 out_le32(((unsigned __iomem *) par->cmap_adr) + regno,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 (red << 16 | green << 8 | blue));
166 break;
Benjamin Herrenschmidt57a20d82008-07-17 15:05:22 +1000167 case cmap_avivo:
168 /* Write to both LUTs for now */
169 writel(1, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
170 writeb(regno, par->cmap_adr + AVIVO_DC_LUT_RW_INDEX);
171 writel(((red) << 22) | ((green) << 12) | ((blue) << 2),
172 par->cmap_adr + AVIVO_DC_LUT_30_COLOR);
173 writel(0, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
174 writeb(regno, par->cmap_adr + AVIVO_DC_LUT_RW_INDEX);
175 writel(((red) << 22) | ((green) << 12) | ((blue) << 2),
176 par->cmap_adr + AVIVO_DC_LUT_30_COLOR);
177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return 0;
181}
182
183 /*
184 * Blank the display.
185 */
186
187static int offb_blank(int blank, struct fb_info *info)
188{
189 struct offb_par *par = (struct offb_par *) info->par;
190 int i, j;
191
192 if (!par->cmap_adr)
193 return 0;
194
195 if (!par->blanked)
196 if (!blank)
197 return 0;
198
199 par->blanked = blank;
200
201 if (blank)
202 for (i = 0; i < 256; i++) {
203 switch (par->cmap_type) {
Benjamin Herrenschmidt9b961ed2011-12-14 13:58:29 +0000204 case cmap_simple:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 writeb(i, par->cmap_adr);
206 for (j = 0; j < 3; j++)
207 writeb(0, par->cmap_data);
208 break;
209 case cmap_M3A:
210 /* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
211 out_le32(par->cmap_adr + 0x58,
212 in_le32(par->cmap_adr + 0x58) & ~0x20);
213 case cmap_r128:
214 /* Set palette index & data */
215 out_8(par->cmap_adr + 0xb0, i);
216 out_le32(par->cmap_adr + 0xb4, 0);
217 break;
218 case cmap_M3B:
219 /* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
220 out_le32(par->cmap_adr + 0x58,
221 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);
225 break;
226 case cmap_radeon:
227 out_8(par->cmap_adr + 0xb0, i);
228 out_le32(par->cmap_adr + 0xb4, 0);
229 break;
230 case cmap_gxt2000:
Benjamin Herrenschmidt441cbd82006-10-26 15:38:10 +1000231 out_le32(((unsigned __iomem *) par->cmap_adr) + i,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 0);
233 break;
Benjamin Herrenschmidt57a20d82008-07-17 15:05:22 +1000234 case cmap_avivo:
235 writel(1, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
236 writeb(i, par->cmap_adr + AVIVO_DC_LUT_RW_INDEX);
237 writel(0, par->cmap_adr + AVIVO_DC_LUT_30_COLOR);
238 writel(0, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
239 writeb(i, par->cmap_adr + AVIVO_DC_LUT_RW_INDEX);
240 writel(0, par->cmap_adr + AVIVO_DC_LUT_30_COLOR);
241 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243 } else
244 fb_set_cmap(&info->cmap, info);
245 return 0;
246}
247
Benjamin Herrenschmidt57a20d82008-07-17 15:05:22 +1000248static int offb_set_par(struct fb_info *info)
249{
250 struct offb_par *par = (struct offb_par *) info->par;
251
252 /* On avivo, initialize palette control */
253 if (par->cmap_type == cmap_avivo) {
254 writel(0, par->cmap_adr + AVIVO_DC_LUTA_CONTROL);
255 writel(0, par->cmap_adr + AVIVO_DC_LUTA_BLACK_OFFSET_BLUE);
256 writel(0, par->cmap_adr + AVIVO_DC_LUTA_BLACK_OFFSET_GREEN);
257 writel(0, par->cmap_adr + AVIVO_DC_LUTA_BLACK_OFFSET_RED);
258 writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTA_WHITE_OFFSET_BLUE);
259 writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTA_WHITE_OFFSET_GREEN);
260 writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTA_WHITE_OFFSET_RED);
261 writel(0, par->cmap_adr + AVIVO_DC_LUTB_CONTROL);
262 writel(0, par->cmap_adr + AVIVO_DC_LUTB_BLACK_OFFSET_BLUE);
263 writel(0, par->cmap_adr + AVIVO_DC_LUTB_BLACK_OFFSET_GREEN);
264 writel(0, par->cmap_adr + AVIVO_DC_LUTB_BLACK_OFFSET_RED);
265 writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTB_WHITE_OFFSET_BLUE);
266 writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTB_WHITE_OFFSET_GREEN);
267 writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTB_WHITE_OFFSET_RED);
268 writel(1, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
269 writel(0, par->cmap_adr + AVIVO_DC_LUT_RW_MODE);
270 writel(0x0000003f, par->cmap_adr + AVIVO_DC_LUT_WRITE_EN_MASK);
271 writel(0, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
272 writel(0, par->cmap_adr + AVIVO_DC_LUT_RW_MODE);
273 writel(0x0000003f, par->cmap_adr + AVIVO_DC_LUT_WRITE_EN_MASK);
274 }
275 return 0;
276}
277
Dave Airlieceae8cb2009-12-06 20:01:26 +0000278static void offb_destroy(struct fb_info *info)
279{
280 if (info->screen_base)
281 iounmap(info->screen_base);
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200282 release_mem_region(info->apertures->ranges[0].base, info->apertures->ranges[0].size);
Dave Airlieceae8cb2009-12-06 20:01:26 +0000283 framebuffer_release(info);
284}
285
Benjamin Herrenschmidt57a20d82008-07-17 15:05:22 +1000286static struct fb_ops offb_ops = {
287 .owner = THIS_MODULE,
Dave Airlieceae8cb2009-12-06 20:01:26 +0000288 .fb_destroy = offb_destroy,
Benjamin Herrenschmidt57a20d82008-07-17 15:05:22 +1000289 .fb_setcolreg = offb_setcolreg,
290 .fb_set_par = offb_set_par,
291 .fb_blank = offb_blank,
292 .fb_fillrect = cfb_fillrect,
293 .fb_copyarea = cfb_copyarea,
294 .fb_imageblit = cfb_imageblit,
295};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000297static void __iomem *offb_map_reg(struct device_node *np, int index,
298 unsigned long offset, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Cedric Le Goater212c0cb2013-12-04 17:49:51 +0100300 const __be32 *addrp;
Benjamin Herrenschmidt441cbd82006-10-26 15:38:10 +1000301 u64 asize, taddr;
302 unsigned int flags;
Benjamin Herrenschmidtcc5d0182005-12-13 18:01:21 +1100303
Benjamin Herrenschmidt441cbd82006-10-26 15:38:10 +1000304 addrp = of_get_pci_address(np, index, &asize, &flags);
305 if (addrp == NULL)
306 addrp = of_get_address(np, index, &asize, &flags);
307 if (addrp == NULL)
308 return NULL;
309 if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
310 return NULL;
311 if ((offset + size) > asize)
312 return NULL;
313 taddr = of_translate_address(np, addrp);
314 if (taddr == OF_BAD_ADDR)
315 return NULL;
316 return ioremap(taddr + offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Benjamin Herrenschmidt57a20d82008-07-17 15:05:22 +1000319static void offb_init_palette_hacks(struct fb_info *info, struct device_node *dp,
320 const char *name, unsigned long address)
321{
322 struct offb_par *par = (struct offb_par *) info->par;
323
324 if (dp && !strncmp(name, "ATY,Rage128", 11)) {
325 par->cmap_adr = offb_map_reg(dp, 2, 0, 0x1fff);
326 if (par->cmap_adr)
327 par->cmap_type = cmap_r128;
328 } else if (dp && (!strncmp(name, "ATY,RageM3pA", 12)
329 || !strncmp(name, "ATY,RageM3p12A", 14))) {
330 par->cmap_adr = offb_map_reg(dp, 2, 0, 0x1fff);
331 if (par->cmap_adr)
332 par->cmap_type = cmap_M3A;
333 } else if (dp && !strncmp(name, "ATY,RageM3pB", 12)) {
334 par->cmap_adr = offb_map_reg(dp, 2, 0, 0x1fff);
335 if (par->cmap_adr)
336 par->cmap_type = cmap_M3B;
337 } else if (dp && !strncmp(name, "ATY,Rage6", 9)) {
338 par->cmap_adr = offb_map_reg(dp, 1, 0, 0x1fff);
339 if (par->cmap_adr)
340 par->cmap_type = cmap_radeon;
341 } else if (!strncmp(name, "ATY,", 4)) {
342 unsigned long base = address & 0xff000000UL;
343 par->cmap_adr =
344 ioremap(base + 0x7ff000, 0x1000) + 0xcc0;
345 par->cmap_data = par->cmap_adr + 1;
Benjamin Herrenschmidt9b961ed2011-12-14 13:58:29 +0000346 par->cmap_type = cmap_simple;
Benjamin Herrenschmidt57a20d82008-07-17 15:05:22 +1000347 } else if (dp && (of_device_is_compatible(dp, "pci1014,b7") ||
348 of_device_is_compatible(dp, "pci1014,21c"))) {
349 par->cmap_adr = offb_map_reg(dp, 0, 0x6000, 0x1000);
350 if (par->cmap_adr)
351 par->cmap_type = cmap_gxt2000;
352 } else if (dp && !strncmp(name, "vga,Display-", 12)) {
353 /* Look for AVIVO initialized by SLOF */
354 struct device_node *pciparent = of_get_parent(dp);
355 const u32 *vid, *did;
356 vid = of_get_property(pciparent, "vendor-id", NULL);
357 did = of_get_property(pciparent, "device-id", NULL);
358 /* This will match most R5xx */
359 if (vid && did && *vid == 0x1002 &&
360 ((*did >= 0x7100 && *did < 0x7800) ||
361 (*did >= 0x9400))) {
362 par->cmap_adr = offb_map_reg(pciparent, 2, 0, 0x10000);
363 if (par->cmap_adr)
364 par->cmap_type = cmap_avivo;
365 }
366 of_node_put(pciparent);
Benjamin Herrenschmidt9b961ed2011-12-14 13:58:29 +0000367 } else if (dp && of_device_is_compatible(dp, "qemu,std-vga")) {
Cedric Le Goater212c0cb2013-12-04 17:49:51 +0100368#ifdef __BIG_ENDIAN
369 const __be32 io_of_addr[3] = { 0x01000000, 0x0, 0x0 };
370#else
371 const __be32 io_of_addr[3] = { 0x00000001, 0x0, 0x0 };
372#endif
Benjamin Herrenschmidt9b961ed2011-12-14 13:58:29 +0000373 u64 io_addr = of_translate_address(dp, io_of_addr);
374 if (io_addr != OF_BAD_ADDR) {
375 par->cmap_adr = ioremap(io_addr + 0x3c8, 2);
376 if (par->cmap_adr) {
377 par->cmap_type = cmap_simple;
378 par->cmap_data = par->cmap_adr + 1;
379 }
380 }
Benjamin Herrenschmidt57a20d82008-07-17 15:05:22 +1000381 }
382 info->fix.visual = (par->cmap_type != cmap_unknown) ?
383 FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_STATIC_PSEUDOCOLOR;
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static void __init offb_init_fb(const char *name, const char *full_name,
387 int width, int height, int depth,
388 int pitch, unsigned long address,
Anton Vorontsov7f29b872008-04-28 02:14:50 -0700389 int foreign_endian, struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Benjamin Herrenschmidtc055fe072012-01-03 12:09:15 +1100391 unsigned long res_size = pitch * height;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 struct offb_par *par = &default_par;
393 unsigned long res_start = address;
394 struct fb_fix_screeninfo *fix;
395 struct fb_var_screeninfo *var;
396 struct fb_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
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);
Benjamin Herrenschmidtab134462006-07-03 17:19:48 +1000404 if (depth != 8 && depth != 15 && depth != 16 && depth != 32) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 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
Krzysztof Helt41138192009-06-16 15:34:23 -0700411 info = framebuffer_alloc(sizeof(u32) * 16, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 if (info == 0) {
414 release_mem_region(res_start, res_size);
415 return;
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 fix = &info->fix;
419 var = &info->var;
Benjamin Herrenschmidt57a20d82008-07-17 15:05:22 +1000420 info->par = par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 strcpy(fix->id, "OFfb ");
423 strncat(fix->id, name, sizeof(fix->id) - sizeof("OFfb "));
424 fix->id[sizeof(fix->id) - 1] = '\0';
425
426 var->xres = var->xres_virtual = width;
427 var->yres = var->yres_virtual = height;
428 fix->line_length = pitch;
429
430 fix->smem_start = address;
431 fix->smem_len = pitch * height;
432 fix->type = FB_TYPE_PACKED_PIXELS;
433 fix->type_aux = 0;
434
435 par->cmap_type = cmap_unknown;
Benjamin Herrenschmidt57a20d82008-07-17 15:05:22 +1000436 if (depth == 8)
437 offb_init_palette_hacks(info, dp, name, address);
438 else
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000439 fix->visual = FB_VISUAL_TRUECOLOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 var->xoffset = var->yoffset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 switch (depth) {
443 case 8:
444 var->bits_per_pixel = 8;
445 var->red.offset = 0;
446 var->red.length = 8;
447 var->green.offset = 0;
448 var->green.length = 8;
449 var->blue.offset = 0;
450 var->blue.length = 8;
451 var->transp.offset = 0;
452 var->transp.length = 0;
453 break;
Benjamin Herrenschmidtab134462006-07-03 17:19:48 +1000454 case 15: /* RGB 555 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 var->bits_per_pixel = 16;
456 var->red.offset = 10;
457 var->red.length = 5;
458 var->green.offset = 5;
459 var->green.length = 5;
460 var->blue.offset = 0;
461 var->blue.length = 5;
462 var->transp.offset = 0;
463 var->transp.length = 0;
464 break;
Benjamin Herrenschmidtab134462006-07-03 17:19:48 +1000465 case 16: /* RGB 565 */
466 var->bits_per_pixel = 16;
467 var->red.offset = 11;
468 var->red.length = 5;
469 var->green.offset = 5;
470 var->green.length = 6;
471 var->blue.offset = 0;
472 var->blue.length = 5;
473 var->transp.offset = 0;
474 var->transp.length = 0;
475 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 case 32: /* RGB 888 */
477 var->bits_per_pixel = 32;
478 var->red.offset = 16;
479 var->red.length = 8;
480 var->green.offset = 8;
481 var->green.length = 8;
482 var->blue.offset = 0;
483 var->blue.length = 8;
484 var->transp.offset = 24;
485 var->transp.length = 8;
486 break;
487 }
488 var->red.msb_right = var->green.msb_right = var->blue.msb_right =
489 var->transp.msb_right = 0;
490 var->grayscale = 0;
491 var->nonstd = 0;
492 var->activate = 0;
493 var->height = var->width = -1;
494 var->pixclock = 10000;
495 var->left_margin = var->right_margin = 16;
496 var->upper_margin = var->lower_margin = 16;
497 var->hsync_len = var->vsync_len = 8;
498 var->sync = 0;
499 var->vmode = FB_VMODE_NONINTERLACED;
500
Dave Airlieceae8cb2009-12-06 20:01:26 +0000501 /* set offb aperture size for generic probing */
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200502 info->apertures = alloc_apertures(1);
503 if (!info->apertures)
504 goto out_aper;
505 info->apertures->ranges[0].base = address;
506 info->apertures->ranges[0].size = fix->smem_len;
Dave Airlieceae8cb2009-12-06 20:01:26 +0000507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 info->fbops = &offb_ops;
509 info->screen_base = ioremap(address, fix->smem_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 info->pseudo_palette = (void *) (info + 1);
Dave Airlieceae8cb2009-12-06 20:01:26 +0000511 info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE | foreign_endian;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 fb_alloc_cmap(&info->cmap, 256, 0);
514
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200515 if (register_framebuffer(info) < 0)
516 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Joe Perches31b67802013-09-19 18:35:55 -0700518 fb_info(info, "Open Firmware frame buffer device on %s\n", full_name);
Marcin Slusarz1471ca92010-05-16 17:27:03 +0200519 return;
520
521out_err:
522 iounmap(info->screen_base);
523out_aper:
524 iounmap(par->cmap_adr);
525 par->cmap_adr = NULL;
526 framebuffer_release(info);
527 release_mem_region(res_start, res_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000530
531static void __init offb_init_nodriver(struct device_node *dp, int no_real_node)
532{
533 unsigned int len;
534 int i, width = 640, height = 480, depth = 8, pitch = 640;
535 unsigned int flags, rsize, addr_prop = 0;
536 unsigned long max_size = 0;
537 u64 rstart, address = OF_BAD_ADDR;
Cedric Le Goater212c0cb2013-12-04 17:49:51 +0100538 const __be32 *pp, *addrp, *up;
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000539 u64 asize;
Anton Vorontsov7f29b872008-04-28 02:14:50 -0700540 int foreign_endian = 0;
541
542#ifdef __BIG_ENDIAN
543 if (of_get_property(dp, "little-endian", NULL))
544 foreign_endian = FBINFO_FOREIGN_ENDIAN;
545#else
546 if (of_get_property(dp, "big-endian", NULL))
547 foreign_endian = FBINFO_FOREIGN_ENDIAN;
548#endif
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000549
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000550 pp = of_get_property(dp, "linux,bootx-depth", &len);
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000551 if (pp == NULL)
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000552 pp = of_get_property(dp, "depth", &len);
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000553 if (pp && len == sizeof(u32))
Cedric Le Goater212c0cb2013-12-04 17:49:51 +0100554 depth = be32_to_cpup(pp);
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000555
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000556 pp = of_get_property(dp, "linux,bootx-width", &len);
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000557 if (pp == NULL)
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000558 pp = of_get_property(dp, "width", &len);
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000559 if (pp && len == sizeof(u32))
Cedric Le Goater212c0cb2013-12-04 17:49:51 +0100560 width = be32_to_cpup(pp);
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000561
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000562 pp = of_get_property(dp, "linux,bootx-height", &len);
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000563 if (pp == NULL)
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000564 pp = of_get_property(dp, "height", &len);
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000565 if (pp && len == sizeof(u32))
Cedric Le Goater212c0cb2013-12-04 17:49:51 +0100566 height = be32_to_cpup(pp);
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000567
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000568 pp = of_get_property(dp, "linux,bootx-linebytes", &len);
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000569 if (pp == NULL)
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000570 pp = of_get_property(dp, "linebytes", &len);
Benjamin Herrenschmidt441cbd82006-10-26 15:38:10 +1000571 if (pp && len == sizeof(u32) && (*pp != 0xffffffffu))
Cedric Le Goater212c0cb2013-12-04 17:49:51 +0100572 pitch = be32_to_cpup(pp);
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000573 else
574 pitch = width * ((depth + 7) / 8);
575
576 rsize = (unsigned long)pitch * (unsigned long)height;
577
578 /* Ok, now we try to figure out the address of the framebuffer.
579 *
580 * Unfortunately, Open Firmware doesn't provide a standard way to do
581 * so. All we can do is a dodgy heuristic that happens to work in
582 * practice. On most machines, the "address" property contains what
583 * we need, though not on Matrox cards found in IBM machines. What I've
584 * found that appears to give good results is to go through the PCI
585 * ranges and pick one that is both big enough and if possible encloses
586 * the "address" property. If none match, we pick the biggest
587 */
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000588 up = of_get_property(dp, "linux,bootx-addr", &len);
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000589 if (up == NULL)
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000590 up = of_get_property(dp, "address", &len);
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000591 if (up && len == sizeof(u32))
592 addr_prop = *up;
593
594 /* Hack for when BootX is passing us */
595 if (no_real_node)
596 goto skip_addr;
597
598 for (i = 0; (addrp = of_get_address(dp, i, &asize, &flags))
599 != NULL; i++) {
600 int match_addrp = 0;
601
602 if (!(flags & IORESOURCE_MEM))
603 continue;
604 if (asize < rsize)
605 continue;
606 rstart = of_translate_address(dp, addrp);
607 if (rstart == OF_BAD_ADDR)
608 continue;
609 if (addr_prop && (rstart <= addr_prop) &&
610 ((rstart + asize) >= (addr_prop + rsize)))
611 match_addrp = 1;
612 if (match_addrp) {
613 address = addr_prop;
614 break;
615 }
616 if (rsize > max_size) {
617 max_size = rsize;
618 address = OF_BAD_ADDR;
619 }
620
621 if (address == OF_BAD_ADDR)
622 address = rstart;
623 }
624 skip_addr:
625 if (address == OF_BAD_ADDR && addr_prop)
626 address = (u64)addr_prop;
627 if (address != OF_BAD_ADDR) {
628 /* kludge for valkyrie */
629 if (strcmp(dp->name, "valkyrie") == 0)
630 address += 0x1000;
631 offb_init_fb(no_real_node ? "bootx" : dp->name,
632 no_real_node ? "display" : dp->full_name,
633 width, height, depth, pitch, address,
Anton Vorontsov7f29b872008-04-28 02:14:50 -0700634 foreign_endian, no_real_node ? NULL : dp);
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000635 }
636}
637
638static int __init offb_init(void)
639{
640 struct device_node *dp = NULL, *boot_disp = NULL;
641
642 if (fb_get_options("offb", NULL))
643 return -ENODEV;
644
645 /* Check if we have a MacOS display without a node spec */
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000646 if (of_get_property(of_chosen, "linux,bootx-noscreen", NULL) != NULL) {
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000647 /* The old code tried to work out which node was the MacOS
648 * display based on the address. I'm dropping that since the
649 * lack of a node spec only happens with old BootX versions
650 * (users can update) and with this code, they'll still get
651 * a display (just not the palette hacks).
652 */
653 offb_init_nodriver(of_chosen, 1);
654 }
655
656 for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000657 if (of_get_property(dp, "linux,opened", NULL) &&
658 of_get_property(dp, "linux,boot-display", NULL)) {
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000659 boot_disp = dp;
660 offb_init_nodriver(dp, 0);
661 }
662 }
663 for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000664 if (of_get_property(dp, "linux,opened", NULL) &&
Benjamin Herrenschmidt73ea6952006-07-04 17:07:18 +1000665 dp != boot_disp)
666 offb_init_nodriver(dp, 0);
667 }
668
669 return 0;
670}
671
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673module_init(offb_init);
674MODULE_LICENSE("GPL");