offb.c 20 KB

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