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/module.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/string.h>
19 #include <linux/mm.h>
20 #include <linux/vmalloc.h>
21 #include <linux/delay.h>
22 #include <linux/of.h>
23 #include <linux/of_address.h>
24 #include <linux/interrupt.h>
25 #include <linux/fb.h>
26 #include <linux/init.h>
27 #include <linux/ioport.h>
28 #include <linux/pci.h>
29 #include <linux/platform_device.h>
30 #include <asm/io.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_simple,		/* 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 	cmap_avivo,		/* ATI R5xx */
48 	cmap_qemu,		/* qemu vga */
49 };
50 
51 struct offb_par {
52 	volatile void __iomem *cmap_adr;
53 	volatile void __iomem *cmap_data;
54 	int cmap_type;
55 	int blanked;
56 };
57 
58 struct offb_par default_par;
59 
60 #ifdef CONFIG_PPC32
61 extern boot_infos_t *boot_infos;
62 #endif
63 
64 /* Definitions used by the Avivo palette hack */
65 #define AVIVO_DC_LUT_RW_SELECT                  0x6480
66 #define AVIVO_DC_LUT_RW_MODE                    0x6484
67 #define AVIVO_DC_LUT_RW_INDEX                   0x6488
68 #define AVIVO_DC_LUT_SEQ_COLOR                  0x648c
69 #define AVIVO_DC_LUT_PWL_DATA                   0x6490
70 #define AVIVO_DC_LUT_30_COLOR                   0x6494
71 #define AVIVO_DC_LUT_READ_PIPE_SELECT           0x6498
72 #define AVIVO_DC_LUT_WRITE_EN_MASK              0x649c
73 #define AVIVO_DC_LUT_AUTOFILL                   0x64a0
74 
75 #define AVIVO_DC_LUTA_CONTROL                   0x64c0
76 #define AVIVO_DC_LUTA_BLACK_OFFSET_BLUE         0x64c4
77 #define AVIVO_DC_LUTA_BLACK_OFFSET_GREEN        0x64c8
78 #define AVIVO_DC_LUTA_BLACK_OFFSET_RED          0x64cc
79 #define AVIVO_DC_LUTA_WHITE_OFFSET_BLUE         0x64d0
80 #define AVIVO_DC_LUTA_WHITE_OFFSET_GREEN        0x64d4
81 #define AVIVO_DC_LUTA_WHITE_OFFSET_RED          0x64d8
82 
83 #define AVIVO_DC_LUTB_CONTROL                   0x6cc0
84 #define AVIVO_DC_LUTB_BLACK_OFFSET_BLUE         0x6cc4
85 #define AVIVO_DC_LUTB_BLACK_OFFSET_GREEN        0x6cc8
86 #define AVIVO_DC_LUTB_BLACK_OFFSET_RED          0x6ccc
87 #define AVIVO_DC_LUTB_WHITE_OFFSET_BLUE         0x6cd0
88 #define AVIVO_DC_LUTB_WHITE_OFFSET_GREEN        0x6cd4
89 #define AVIVO_DC_LUTB_WHITE_OFFSET_RED          0x6cd8
90 
91     /*
92      *  Set a single color register. The values supplied are already
93      *  rounded down to the hardware's capabilities (according to the
94      *  entries in the var structure). Return != 0 for invalid regno.
95      */
96 
offb_setcolreg(u_int regno,u_int red,u_int green,u_int blue,u_int transp,struct fb_info * info)97 static int offb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
98 			  u_int transp, struct fb_info *info)
99 {
100 	struct offb_par *par = (struct offb_par *) info->par;
101 
102 	if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
103 		u32 *pal = info->pseudo_palette;
104 		u32 cr = red >> (16 - info->var.red.length);
105 		u32 cg = green >> (16 - info->var.green.length);
106 		u32 cb = blue >> (16 - info->var.blue.length);
107 		u32 value;
108 
109 		if (regno >= 16)
110 			return -EINVAL;
111 
112 		value = (cr << info->var.red.offset) |
113 			(cg << info->var.green.offset) |
114 			(cb << info->var.blue.offset);
115 		if (info->var.transp.length > 0) {
116 			u32 mask = (1 << info->var.transp.length) - 1;
117 			mask <<= info->var.transp.offset;
118 			value |= mask;
119 		}
120 		pal[regno] = value;
121 		return 0;
122 	}
123 
124 	if (regno > 255)
125 		return -EINVAL;
126 
127 	red >>= 8;
128 	green >>= 8;
129 	blue >>= 8;
130 
131 	if (!par->cmap_adr)
132 		return 0;
133 
134 	switch (par->cmap_type) {
135 	case cmap_simple:
136 		writeb(regno, par->cmap_adr);
137 		writeb(red, par->cmap_data);
138 		writeb(green, par->cmap_data);
139 		writeb(blue, par->cmap_data);
140 		break;
141 	case cmap_M3A:
142 		/* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
143 		out_le32(par->cmap_adr + 0x58,
144 			 in_le32(par->cmap_adr + 0x58) & ~0x20);
145 		fallthrough;
146 	case cmap_r128:
147 		/* Set palette index & data */
148 		out_8(par->cmap_adr + 0xb0, regno);
149 		out_le32(par->cmap_adr + 0xb4,
150 			 (red << 16 | green << 8 | blue));
151 		break;
152 	case cmap_M3B:
153 		/* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
154 		out_le32(par->cmap_adr + 0x58,
155 			 in_le32(par->cmap_adr + 0x58) | 0x20);
156 		/* Set palette index & data */
157 		out_8(par->cmap_adr + 0xb0, regno);
158 		out_le32(par->cmap_adr + 0xb4, (red << 16 | green << 8 | blue));
159 		break;
160 	case cmap_radeon:
161 		/* Set palette index & data (could be smarter) */
162 		out_8(par->cmap_adr + 0xb0, regno);
163 		out_le32(par->cmap_adr + 0xb4, (red << 16 | green << 8 | blue));
164 		break;
165 	case cmap_gxt2000:
166 		out_le32(((unsigned __iomem *) par->cmap_adr) + regno,
167 			 (red << 16 | green << 8 | blue));
168 		break;
169 	case cmap_avivo:
170 		/* Write to both LUTs for now */
171 		writel(1, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
172 		writeb(regno, par->cmap_adr + AVIVO_DC_LUT_RW_INDEX);
173 		writel(((red) << 22) | ((green) << 12) | ((blue) << 2),
174 		       par->cmap_adr + AVIVO_DC_LUT_30_COLOR);
175 		writel(0, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
176 		writeb(regno, par->cmap_adr + AVIVO_DC_LUT_RW_INDEX);
177 		writel(((red) << 22) | ((green) << 12) | ((blue) << 2),
178 		       par->cmap_adr + AVIVO_DC_LUT_30_COLOR);
179 		break;
180 	}
181 
182 	return 0;
183 }
184 
185     /*
186      *  Blank the display.
187      */
188 
offb_blank(int blank,struct fb_info * info)189 static int offb_blank(int blank, struct fb_info *info)
190 {
191 	struct offb_par *par = (struct offb_par *) info->par;
192 	int i, j;
193 
194 	if (!par->cmap_adr)
195 		return 0;
196 
197 	if (!par->blanked)
198 		if (!blank)
199 			return 0;
200 
201 	par->blanked = blank;
202 
203 	if (blank)
204 		for (i = 0; i < 256; i++) {
205 			switch (par->cmap_type) {
206 			case cmap_simple:
207 				writeb(i, par->cmap_adr);
208 				for (j = 0; j < 3; j++)
209 					writeb(0, par->cmap_data);
210 				break;
211 			case cmap_M3A:
212 				/* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
213 				out_le32(par->cmap_adr + 0x58,
214 					 in_le32(par->cmap_adr + 0x58) & ~0x20);
215 				fallthrough;
216 			case cmap_r128:
217 				/* Set palette index & data */
218 				out_8(par->cmap_adr + 0xb0, i);
219 				out_le32(par->cmap_adr + 0xb4, 0);
220 				break;
221 			case cmap_M3B:
222 				/* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
223 				out_le32(par->cmap_adr + 0x58,
224 					 in_le32(par->cmap_adr + 0x58) | 0x20);
225 				/* Set palette index & data */
226 				out_8(par->cmap_adr + 0xb0, i);
227 				out_le32(par->cmap_adr + 0xb4, 0);
228 				break;
229 			case cmap_radeon:
230 				out_8(par->cmap_adr + 0xb0, i);
231 				out_le32(par->cmap_adr + 0xb4, 0);
232 				break;
233 			case cmap_gxt2000:
234 				out_le32(((unsigned __iomem *) par->cmap_adr) + i,
235 					 0);
236 				break;
237 			case cmap_avivo:
238 				writel(1, 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 				writel(0, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
242 				writeb(i, par->cmap_adr + AVIVO_DC_LUT_RW_INDEX);
243 				writel(0, par->cmap_adr + AVIVO_DC_LUT_30_COLOR);
244 				break;
245 			}
246 	} else
247 		fb_set_cmap(&info->cmap, info);
248 	return 0;
249 }
250 
offb_set_par(struct fb_info * info)251 static int offb_set_par(struct fb_info *info)
252 {
253 	struct offb_par *par = (struct offb_par *) info->par;
254 
255 	/* On avivo, initialize palette control */
256 	if (par->cmap_type == cmap_avivo) {
257 		writel(0, par->cmap_adr + AVIVO_DC_LUTA_CONTROL);
258 		writel(0, par->cmap_adr + AVIVO_DC_LUTA_BLACK_OFFSET_BLUE);
259 		writel(0, par->cmap_adr + AVIVO_DC_LUTA_BLACK_OFFSET_GREEN);
260 		writel(0, par->cmap_adr + AVIVO_DC_LUTA_BLACK_OFFSET_RED);
261 		writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTA_WHITE_OFFSET_BLUE);
262 		writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTA_WHITE_OFFSET_GREEN);
263 		writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTA_WHITE_OFFSET_RED);
264 		writel(0, par->cmap_adr + AVIVO_DC_LUTB_CONTROL);
265 		writel(0, par->cmap_adr + AVIVO_DC_LUTB_BLACK_OFFSET_BLUE);
266 		writel(0, par->cmap_adr + AVIVO_DC_LUTB_BLACK_OFFSET_GREEN);
267 		writel(0, par->cmap_adr + AVIVO_DC_LUTB_BLACK_OFFSET_RED);
268 		writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTB_WHITE_OFFSET_BLUE);
269 		writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTB_WHITE_OFFSET_GREEN);
270 		writel(0x0000ffff, par->cmap_adr + AVIVO_DC_LUTB_WHITE_OFFSET_RED);
271 		writel(1, 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 		writel(0, par->cmap_adr + AVIVO_DC_LUT_RW_SELECT);
275 		writel(0, par->cmap_adr + AVIVO_DC_LUT_RW_MODE);
276 		writel(0x0000003f, par->cmap_adr + AVIVO_DC_LUT_WRITE_EN_MASK);
277 	}
278 	return 0;
279 }
280 
offb_destroy(struct fb_info * info)281 static void offb_destroy(struct fb_info *info)
282 {
283 	if (info->screen_base)
284 		iounmap(info->screen_base);
285 	release_mem_region(info->apertures->ranges[0].base, info->apertures->ranges[0].size);
286 	fb_dealloc_cmap(&info->cmap);
287 	framebuffer_release(info);
288 }
289 
290 static const struct fb_ops offb_ops = {
291 	.owner		= THIS_MODULE,
292 	.fb_destroy	= offb_destroy,
293 	.fb_setcolreg	= offb_setcolreg,
294 	.fb_set_par	= offb_set_par,
295 	.fb_blank	= offb_blank,
296 	.fb_fillrect	= cfb_fillrect,
297 	.fb_copyarea	= cfb_copyarea,
298 	.fb_imageblit	= cfb_imageblit,
299 };
300 
offb_map_reg(struct device_node * np,int index,unsigned long offset,unsigned long size)301 static void __iomem *offb_map_reg(struct device_node *np, int index,
302 				  unsigned long offset, unsigned long size)
303 {
304 	const __be32 *addrp;
305 	u64 asize, taddr;
306 	unsigned int flags;
307 
308 	addrp = of_get_pci_address(np, index, &asize, &flags);
309 	if (addrp == NULL)
310 		addrp = of_get_address(np, index, &asize, &flags);
311 	if (addrp == NULL)
312 		return NULL;
313 	if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
314 		return NULL;
315 	if ((offset + size) > asize)
316 		return NULL;
317 	taddr = of_translate_address(np, addrp);
318 	if (taddr == OF_BAD_ADDR)
319 		return NULL;
320 	return ioremap(taddr + offset, size);
321 }
322 
offb_init_palette_hacks(struct fb_info * info,struct device_node * dp,unsigned long address)323 static void offb_init_palette_hacks(struct fb_info *info, struct device_node *dp,
324 				    unsigned long address)
325 {
326 	struct offb_par *par = (struct offb_par *) info->par;
327 
328 	if (of_node_name_prefix(dp, "ATY,Rage128")) {
329 		par->cmap_adr = offb_map_reg(dp, 2, 0, 0x1fff);
330 		if (par->cmap_adr)
331 			par->cmap_type = cmap_r128;
332 	} else if (of_node_name_prefix(dp, "ATY,RageM3pA") ||
333 		   of_node_name_prefix(dp, "ATY,RageM3p12A")) {
334 		par->cmap_adr = offb_map_reg(dp, 2, 0, 0x1fff);
335 		if (par->cmap_adr)
336 			par->cmap_type = cmap_M3A;
337 	} else if (of_node_name_prefix(dp, "ATY,RageM3pB")) {
338 		par->cmap_adr = offb_map_reg(dp, 2, 0, 0x1fff);
339 		if (par->cmap_adr)
340 			par->cmap_type = cmap_M3B;
341 	} else if (of_node_name_prefix(dp, "ATY,Rage6")) {
342 		par->cmap_adr = offb_map_reg(dp, 1, 0, 0x1fff);
343 		if (par->cmap_adr)
344 			par->cmap_type = cmap_radeon;
345 	} else if (of_node_name_prefix(dp, "ATY,")) {
346 		unsigned long base = address & 0xff000000UL;
347 		par->cmap_adr =
348 			ioremap(base + 0x7ff000, 0x1000) + 0xcc0;
349 		par->cmap_data = par->cmap_adr + 1;
350 		par->cmap_type = cmap_simple;
351 	} else if (dp && (of_device_is_compatible(dp, "pci1014,b7") ||
352 			  of_device_is_compatible(dp, "pci1014,21c"))) {
353 		par->cmap_adr = offb_map_reg(dp, 0, 0x6000, 0x1000);
354 		if (par->cmap_adr)
355 			par->cmap_type = cmap_gxt2000;
356 	} else if (of_node_name_prefix(dp, "vga,Display-")) {
357 		/* Look for AVIVO initialized by SLOF */
358 		struct device_node *pciparent = of_get_parent(dp);
359 		const u32 *vid, *did;
360 		vid = of_get_property(pciparent, "vendor-id", NULL);
361 		did = of_get_property(pciparent, "device-id", NULL);
362 		/* This will match most R5xx */
363 		if (vid && did && *vid == 0x1002 &&
364 		    ((*did >= 0x7100 && *did < 0x7800) ||
365 		     (*did >= 0x9400))) {
366 			par->cmap_adr = offb_map_reg(pciparent, 2, 0, 0x10000);
367 			if (par->cmap_adr)
368 				par->cmap_type = cmap_avivo;
369 		}
370 		of_node_put(pciparent);
371 	} else if (dp && of_device_is_compatible(dp, "qemu,std-vga")) {
372 #ifdef __BIG_ENDIAN
373 		const __be32 io_of_addr[3] = { 0x01000000, 0x0, 0x0 };
374 #else
375 		const __be32 io_of_addr[3] = { 0x00000001, 0x0, 0x0 };
376 #endif
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);
380 			if (par->cmap_adr) {
381 				par->cmap_type = cmap_simple;
382 				par->cmap_data = par->cmap_adr + 1;
383 			}
384 		}
385 	}
386 	info->fix.visual = (par->cmap_type != cmap_unknown) ?
387 		FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_STATIC_PSEUDOCOLOR;
388 }
389 
offb_init_fb(struct platform_device * parent,const char * name,int width,int height,int depth,int pitch,unsigned long address,int foreign_endian,struct device_node * dp)390 static void offb_init_fb(struct platform_device *parent, const char *name,
391 			 int width, int height, int depth,
392 			 int pitch, unsigned long address,
393 			 int foreign_endian, struct device_node *dp)
394 {
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;
401 
402 	if (!request_mem_region(res_start, res_size, "offb"))
403 		return;
404 
405 	printk(KERN_INFO
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 "%pOF: can't use depth = %d\n", dp, depth);
410 		release_mem_region(res_start, res_size);
411 		return;
412 	}
413 
414 	info = framebuffer_alloc(sizeof(u32) * 16, &parent->dev);
415 
416 	if (!info) {
417 		release_mem_region(res_start, res_size);
418 		return;
419 	}
420 	platform_set_drvdata(parent, info);
421 
422 	fix = &info->fix;
423 	var = &info->var;
424 	info->par = par;
425 
426 	if (name) {
427 		strcpy(fix->id, "OFfb ");
428 		strncat(fix->id, name, sizeof(fix->id) - sizeof("OFfb "));
429 		fix->id[sizeof(fix->id) - 1] = '\0';
430 	} else
431 		snprintf(fix->id, sizeof(fix->id), "OFfb %pOFn", dp);
432 
433 
434 	var->xres = var->xres_virtual = width;
435 	var->yres = var->yres_virtual = height;
436 	fix->line_length = pitch;
437 
438 	fix->smem_start = address;
439 	fix->smem_len = pitch * height;
440 	fix->type = FB_TYPE_PACKED_PIXELS;
441 	fix->type_aux = 0;
442 
443 	par->cmap_type = cmap_unknown;
444 	if (depth == 8)
445 		offb_init_palette_hacks(info, dp, address);
446 	else
447 		fix->visual = FB_VISUAL_TRUECOLOR;
448 
449 	var->xoffset = var->yoffset = 0;
450 	switch (depth) {
451 	case 8:
452 		var->bits_per_pixel = 8;
453 		var->red.offset = 0;
454 		var->red.length = 8;
455 		var->green.offset = 0;
456 		var->green.length = 8;
457 		var->blue.offset = 0;
458 		var->blue.length = 8;
459 		var->transp.offset = 0;
460 		var->transp.length = 0;
461 		break;
462 	case 15:		/* RGB 555 */
463 		var->bits_per_pixel = 16;
464 		var->red.offset = 10;
465 		var->red.length = 5;
466 		var->green.offset = 5;
467 		var->green.length = 5;
468 		var->blue.offset = 0;
469 		var->blue.length = 5;
470 		var->transp.offset = 0;
471 		var->transp.length = 0;
472 		break;
473 	case 16:		/* RGB 565 */
474 		var->bits_per_pixel = 16;
475 		var->red.offset = 11;
476 		var->red.length = 5;
477 		var->green.offset = 5;
478 		var->green.length = 6;
479 		var->blue.offset = 0;
480 		var->blue.length = 5;
481 		var->transp.offset = 0;
482 		var->transp.length = 0;
483 		break;
484 	case 32:		/* RGB 888 */
485 		var->bits_per_pixel = 32;
486 		var->red.offset = 16;
487 		var->red.length = 8;
488 		var->green.offset = 8;
489 		var->green.length = 8;
490 		var->blue.offset = 0;
491 		var->blue.length = 8;
492 		var->transp.offset = 24;
493 		var->transp.length = 8;
494 		break;
495 	}
496 	var->red.msb_right = var->green.msb_right = var->blue.msb_right =
497 	    var->transp.msb_right = 0;
498 	var->grayscale = 0;
499 	var->nonstd = 0;
500 	var->activate = 0;
501 	var->height = var->width = -1;
502 	var->pixclock = 10000;
503 	var->left_margin = var->right_margin = 16;
504 	var->upper_margin = var->lower_margin = 16;
505 	var->hsync_len = var->vsync_len = 8;
506 	var->sync = 0;
507 	var->vmode = FB_VMODE_NONINTERLACED;
508 
509 	/* set offb aperture size for generic probing */
510 	info->apertures = alloc_apertures(1);
511 	if (!info->apertures)
512 		goto out_aper;
513 	info->apertures->ranges[0].base = address;
514 	info->apertures->ranges[0].size = fix->smem_len;
515 
516 	info->fbops = &offb_ops;
517 	info->screen_base = ioremap(address, fix->smem_len);
518 	info->pseudo_palette = (void *) (info + 1);
519 	info->flags = FBINFO_DEFAULT | FBINFO_MISC_FIRMWARE | foreign_endian;
520 
521 	fb_alloc_cmap(&info->cmap, 256, 0);
522 
523 	if (register_framebuffer(info) < 0)
524 		goto out_err;
525 
526 	fb_info(info, "Open Firmware frame buffer device on %pOF\n", dp);
527 	return;
528 
529 out_err:
530 	fb_dealloc_cmap(&info->cmap);
531 	iounmap(info->screen_base);
532 out_aper:
533 	iounmap(par->cmap_adr);
534 	par->cmap_adr = NULL;
535 	framebuffer_release(info);
536 	release_mem_region(res_start, res_size);
537 }
538 
539 
offb_init_nodriver(struct platform_device * parent,struct device_node * dp,int no_real_node)540 static void offb_init_nodriver(struct platform_device *parent, struct device_node *dp,
541 			       int no_real_node)
542 {
543 	unsigned int len;
544 	int i, width = 640, height = 480, depth = 8, pitch = 640;
545 	unsigned int flags, rsize, addr_prop = 0;
546 	unsigned long max_size = 0;
547 	u64 rstart, address = OF_BAD_ADDR;
548 	const __be32 *pp, *addrp, *up;
549 	u64 asize;
550 	int foreign_endian = 0;
551 
552 #ifdef __BIG_ENDIAN
553 	if (of_get_property(dp, "little-endian", NULL))
554 		foreign_endian = FBINFO_FOREIGN_ENDIAN;
555 #else
556 	if (of_get_property(dp, "big-endian", NULL))
557 		foreign_endian = FBINFO_FOREIGN_ENDIAN;
558 #endif
559 
560 	pp = of_get_property(dp, "linux,bootx-depth", &len);
561 	if (pp == NULL)
562 		pp = of_get_property(dp, "depth", &len);
563 	if (pp && len == sizeof(u32))
564 		depth = be32_to_cpup(pp);
565 
566 	pp = of_get_property(dp, "linux,bootx-width", &len);
567 	if (pp == NULL)
568 		pp = of_get_property(dp, "width", &len);
569 	if (pp && len == sizeof(u32))
570 		width = be32_to_cpup(pp);
571 
572 	pp = of_get_property(dp, "linux,bootx-height", &len);
573 	if (pp == NULL)
574 		pp = of_get_property(dp, "height", &len);
575 	if (pp && len == sizeof(u32))
576 		height = be32_to_cpup(pp);
577 
578 	pp = of_get_property(dp, "linux,bootx-linebytes", &len);
579 	if (pp == NULL)
580 		pp = of_get_property(dp, "linebytes", &len);
581 	if (pp && len == sizeof(u32) && (*pp != 0xffffffffu))
582 		pitch = be32_to_cpup(pp);
583 	else
584 		pitch = width * ((depth + 7) / 8);
585 
586 	rsize = (unsigned long)pitch * (unsigned long)height;
587 
588 	/* Ok, now we try to figure out the address of the framebuffer.
589 	 *
590 	 * Unfortunately, Open Firmware doesn't provide a standard way to do
591 	 * so. All we can do is a dodgy heuristic that happens to work in
592 	 * practice. On most machines, the "address" property contains what
593 	 * we need, though not on Matrox cards found in IBM machines. What I've
594 	 * found that appears to give good results is to go through the PCI
595 	 * ranges and pick one that is both big enough and if possible encloses
596 	 * the "address" property. If none match, we pick the biggest
597 	 */
598 	up = of_get_property(dp, "linux,bootx-addr", &len);
599 	if (up == NULL)
600 		up = of_get_property(dp, "address", &len);
601 	if (up && len == sizeof(u32))
602 		addr_prop = *up;
603 
604 	/* Hack for when BootX is passing us */
605 	if (no_real_node)
606 		goto skip_addr;
607 
608 	for (i = 0; (addrp = of_get_address(dp, i, &asize, &flags))
609 		     != NULL; i++) {
610 		int match_addrp = 0;
611 
612 		if (!(flags & IORESOURCE_MEM))
613 			continue;
614 		if (asize < rsize)
615 			continue;
616 		rstart = of_translate_address(dp, addrp);
617 		if (rstart == OF_BAD_ADDR)
618 			continue;
619 		if (addr_prop && (rstart <= addr_prop) &&
620 		    ((rstart + asize) >= (addr_prop + rsize)))
621 			match_addrp = 1;
622 		if (match_addrp) {
623 			address = addr_prop;
624 			break;
625 		}
626 		if (rsize > max_size) {
627 			max_size = rsize;
628 			address = OF_BAD_ADDR;
629  		}
630 
631 		if (address == OF_BAD_ADDR)
632 			address = rstart;
633 	}
634  skip_addr:
635 	if (address == OF_BAD_ADDR && addr_prop)
636 		address = (u64)addr_prop;
637 	if (address != OF_BAD_ADDR) {
638 #ifdef CONFIG_PCI
639 		const __be32 *vidp, *didp;
640 		u32 vid, did;
641 		struct pci_dev *pdev;
642 
643 		vidp = of_get_property(dp, "vendor-id", NULL);
644 		didp = of_get_property(dp, "device-id", NULL);
645 		if (vidp && didp) {
646 			vid = be32_to_cpup(vidp);
647 			did = be32_to_cpup(didp);
648 			pdev = pci_get_device(vid, did, NULL);
649 			if (!pdev || pci_enable_device(pdev))
650 				return;
651 		}
652 #endif
653 		/* kludge for valkyrie */
654 		if (of_node_name_eq(dp, "valkyrie"))
655 			address += 0x1000;
656 		offb_init_fb(parent, no_real_node ? "bootx" : NULL,
657 			     width, height, depth, pitch, address,
658 			     foreign_endian, no_real_node ? NULL : dp);
659 	}
660 }
661 
offb_remove(struct platform_device * pdev)662 static int offb_remove(struct platform_device *pdev)
663 {
664 	struct fb_info *info = platform_get_drvdata(pdev);
665 
666 	if (info)
667 		unregister_framebuffer(info);
668 
669 	return 0;
670 }
671 
offb_probe_bootx_noscreen(struct platform_device * pdev)672 static int offb_probe_bootx_noscreen(struct platform_device *pdev)
673 {
674 	offb_init_nodriver(pdev, of_chosen, 1);
675 
676 	return 0;
677 }
678 
679 static struct platform_driver offb_driver_bootx_noscreen = {
680 	.driver = {
681 		.name = "bootx-noscreen",
682 	},
683 	.probe = offb_probe_bootx_noscreen,
684 	.remove = offb_remove,
685 };
686 
offb_probe_display(struct platform_device * pdev)687 static int offb_probe_display(struct platform_device *pdev)
688 {
689 	offb_init_nodriver(pdev, pdev->dev.of_node, 0);
690 
691 	return 0;
692 }
693 
694 static const struct of_device_id offb_of_match_display[] = {
695 	{ .compatible = "display", },
696 	{ },
697 };
698 MODULE_DEVICE_TABLE(of, offb_of_match_display);
699 
700 static struct platform_driver offb_driver_display = {
701 	.driver = {
702 		.name = "of-display",
703 		.of_match_table = offb_of_match_display,
704 	},
705 	.probe = offb_probe_display,
706 	.remove = offb_remove,
707 };
708 
offb_init(void)709 static int __init offb_init(void)
710 {
711 	if (fb_get_options("offb", NULL))
712 		return -ENODEV;
713 
714 	platform_driver_register(&offb_driver_bootx_noscreen);
715 	platform_driver_register(&offb_driver_display);
716 
717 	return 0;
718 }
719 module_init(offb_init);
720 
offb_exit(void)721 static void __exit offb_exit(void)
722 {
723 	platform_driver_unregister(&offb_driver_display);
724 	platform_driver_unregister(&offb_driver_bootx_noscreen);
725 }
726 module_exit(offb_exit);
727 
728 MODULE_LICENSE("GPL");
729