offb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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/slab.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/delay.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/fb.h>
  24. #include <linux/init.h>
  25. #include <linux/ioport.h>
  26. #include <linux/pci.h>
  27. #include <asm/io.h>
  28. #include <asm/prom.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_m64, /* 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. };
  46. struct offb_par {
  47. volatile void __iomem *cmap_adr;
  48. volatile void __iomem *cmap_data;
  49. int cmap_type;
  50. int blanked;
  51. };
  52. struct offb_par default_par;
  53. /*
  54. * Interface used by the world
  55. */
  56. static int offb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  57. u_int transp, struct fb_info *info);
  58. static int offb_blank(int blank, struct fb_info *info);
  59. #ifdef CONFIG_PPC32
  60. extern boot_infos_t *boot_infos;
  61. #endif
  62. static struct fb_ops offb_ops = {
  63. .owner = THIS_MODULE,
  64. .fb_setcolreg = offb_setcolreg,
  65. .fb_blank = offb_blank,
  66. .fb_fillrect = cfb_fillrect,
  67. .fb_copyarea = cfb_copyarea,
  68. .fb_imageblit = cfb_imageblit,
  69. };
  70. /*
  71. * Set a single color register. The values supplied are already
  72. * rounded down to the hardware's capabilities (according to the
  73. * entries in the var structure). Return != 0 for invalid regno.
  74. */
  75. static int offb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  76. u_int transp, struct fb_info *info)
  77. {
  78. struct offb_par *par = (struct offb_par *) info->par;
  79. int i, depth;
  80. u32 *pal = info->pseudo_palette;
  81. depth = info->var.bits_per_pixel;
  82. if (depth == 16)
  83. depth = (info->var.green.length == 5) ? 15 : 16;
  84. if (regno > 255 ||
  85. (depth == 16 && regno > 63) ||
  86. (depth == 15 && regno > 31))
  87. return 1;
  88. if (regno < 16) {
  89. switch (depth) {
  90. case 15:
  91. pal[regno] = (regno << 10) | (regno << 5) | regno;
  92. break;
  93. case 16:
  94. pal[regno] = (regno << 11) | (regno << 5) | regno;
  95. break;
  96. case 24:
  97. pal[regno] = (regno << 16) | (regno << 8) | regno;
  98. break;
  99. case 32:
  100. i = (regno << 8) | regno;
  101. pal[regno] = (i << 16) | i;
  102. break;
  103. }
  104. }
  105. red >>= 8;
  106. green >>= 8;
  107. blue >>= 8;
  108. if (!par->cmap_adr)
  109. return 0;
  110. switch (par->cmap_type) {
  111. case cmap_m64:
  112. writeb(regno, par->cmap_adr);
  113. writeb(red, par->cmap_data);
  114. writeb(green, par->cmap_data);
  115. writeb(blue, par->cmap_data);
  116. break;
  117. case cmap_M3A:
  118. /* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
  119. out_le32(par->cmap_adr + 0x58,
  120. in_le32(par->cmap_adr + 0x58) & ~0x20);
  121. case cmap_r128:
  122. /* Set palette index & data */
  123. out_8(par->cmap_adr + 0xb0, regno);
  124. out_le32(par->cmap_adr + 0xb4,
  125. (red << 16 | green << 8 | blue));
  126. break;
  127. case cmap_M3B:
  128. /* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
  129. out_le32(par->cmap_adr + 0x58,
  130. in_le32(par->cmap_adr + 0x58) | 0x20);
  131. /* Set palette index & data */
  132. out_8(par->cmap_adr + 0xb0, regno);
  133. out_le32(par->cmap_adr + 0xb4, (red << 16 | green << 8 | blue));
  134. break;
  135. case cmap_radeon:
  136. /* Set palette index & data (could be smarter) */
  137. out_8(par->cmap_adr + 0xb0, regno);
  138. out_le32(par->cmap_adr + 0xb4, (red << 16 | green << 8 | blue));
  139. break;
  140. case cmap_gxt2000:
  141. out_le32(((unsigned __iomem *) par->cmap_adr) + regno,
  142. (red << 16 | green << 8 | blue));
  143. break;
  144. }
  145. return 0;
  146. }
  147. /*
  148. * Blank the display.
  149. */
  150. static int offb_blank(int blank, struct fb_info *info)
  151. {
  152. struct offb_par *par = (struct offb_par *) info->par;
  153. int i, j;
  154. if (!par->cmap_adr)
  155. return 0;
  156. if (!par->blanked)
  157. if (!blank)
  158. return 0;
  159. par->blanked = blank;
  160. if (blank)
  161. for (i = 0; i < 256; i++) {
  162. switch (par->cmap_type) {
  163. case cmap_m64:
  164. writeb(i, par->cmap_adr);
  165. for (j = 0; j < 3; j++)
  166. writeb(0, par->cmap_data);
  167. break;
  168. case cmap_M3A:
  169. /* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
  170. out_le32(par->cmap_adr + 0x58,
  171. in_le32(par->cmap_adr + 0x58) & ~0x20);
  172. case cmap_r128:
  173. /* Set palette index & data */
  174. out_8(par->cmap_adr + 0xb0, i);
  175. out_le32(par->cmap_adr + 0xb4, 0);
  176. break;
  177. case cmap_M3B:
  178. /* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
  179. out_le32(par->cmap_adr + 0x58,
  180. in_le32(par->cmap_adr + 0x58) | 0x20);
  181. /* Set palette index & data */
  182. out_8(par->cmap_adr + 0xb0, i);
  183. out_le32(par->cmap_adr + 0xb4, 0);
  184. break;
  185. case cmap_radeon:
  186. out_8(par->cmap_adr + 0xb0, i);
  187. out_le32(par->cmap_adr + 0xb4, 0);
  188. break;
  189. case cmap_gxt2000:
  190. out_le32(((unsigned __iomem *) par->cmap_adr) + i,
  191. 0);
  192. break;
  193. }
  194. } else
  195. fb_set_cmap(&info->cmap, info);
  196. return 0;
  197. }
  198. static void __iomem *offb_map_reg(struct device_node *np, int index,
  199. unsigned long offset, unsigned long size)
  200. {
  201. const u32 *addrp;
  202. u64 asize, taddr;
  203. unsigned int flags;
  204. addrp = of_get_pci_address(np, index, &asize, &flags);
  205. if (addrp == NULL)
  206. addrp = of_get_address(np, index, &asize, &flags);
  207. if (addrp == NULL)
  208. return NULL;
  209. if ((flags & (IORESOURCE_IO | IORESOURCE_MEM)) == 0)
  210. return NULL;
  211. if ((offset + size) > asize)
  212. return NULL;
  213. taddr = of_translate_address(np, addrp);
  214. if (taddr == OF_BAD_ADDR)
  215. return NULL;
  216. return ioremap(taddr + offset, size);
  217. }
  218. static void __init offb_init_fb(const char *name, const char *full_name,
  219. int width, int height, int depth,
  220. int pitch, unsigned long address,
  221. struct device_node *dp)
  222. {
  223. unsigned long res_size = pitch * height * (depth + 7) / 8;
  224. struct offb_par *par = &default_par;
  225. unsigned long res_start = address;
  226. struct fb_fix_screeninfo *fix;
  227. struct fb_var_screeninfo *var;
  228. struct fb_info *info;
  229. int size;
  230. if (!request_mem_region(res_start, res_size, "offb"))
  231. return;
  232. printk(KERN_INFO
  233. "Using unsupported %dx%d %s at %lx, depth=%d, pitch=%d\n",
  234. width, height, name, address, depth, pitch);
  235. if (depth != 8 && depth != 15 && depth != 16 && depth != 32) {
  236. printk(KERN_ERR "%s: can't use depth = %d\n", full_name,
  237. depth);
  238. release_mem_region(res_start, res_size);
  239. return;
  240. }
  241. size = sizeof(struct fb_info) + sizeof(u32) * 17;
  242. info = kmalloc(size, GFP_ATOMIC);
  243. if (info == 0) {
  244. release_mem_region(res_start, res_size);
  245. return;
  246. }
  247. memset(info, 0, size);
  248. fix = &info->fix;
  249. var = &info->var;
  250. strcpy(fix->id, "OFfb ");
  251. strncat(fix->id, name, sizeof(fix->id) - sizeof("OFfb "));
  252. fix->id[sizeof(fix->id) - 1] = '\0';
  253. var->xres = var->xres_virtual = width;
  254. var->yres = var->yres_virtual = height;
  255. fix->line_length = pitch;
  256. fix->smem_start = address;
  257. fix->smem_len = pitch * height;
  258. fix->type = FB_TYPE_PACKED_PIXELS;
  259. fix->type_aux = 0;
  260. par->cmap_type = cmap_unknown;
  261. if (depth == 8) {
  262. if (dp && !strncmp(name, "ATY,Rage128", 11)) {
  263. par->cmap_adr = offb_map_reg(dp, 2, 0, 0x1fff);
  264. if (par->cmap_adr)
  265. par->cmap_type = cmap_r128;
  266. } else if (dp && (!strncmp(name, "ATY,RageM3pA", 12)
  267. || !strncmp(name, "ATY,RageM3p12A", 14))) {
  268. par->cmap_adr = offb_map_reg(dp, 2, 0, 0x1fff);
  269. if (par->cmap_adr)
  270. par->cmap_type = cmap_M3A;
  271. } else if (dp && !strncmp(name, "ATY,RageM3pB", 12)) {
  272. par->cmap_adr = offb_map_reg(dp, 2, 0, 0x1fff);
  273. if (par->cmap_adr)
  274. par->cmap_type = cmap_M3B;
  275. } else if (dp && !strncmp(name, "ATY,Rage6", 9)) {
  276. par->cmap_adr = offb_map_reg(dp, 1, 0, 0x1fff);
  277. if (par->cmap_adr)
  278. par->cmap_type = cmap_radeon;
  279. } else if (!strncmp(name, "ATY,", 4)) {
  280. unsigned long base = address & 0xff000000UL;
  281. par->cmap_adr =
  282. ioremap(base + 0x7ff000, 0x1000) + 0xcc0;
  283. par->cmap_data = par->cmap_adr + 1;
  284. par->cmap_type = cmap_m64;
  285. } else if (dp && (device_is_compatible(dp, "pci1014,b7") ||
  286. device_is_compatible(dp, "pci1014,21c"))) {
  287. par->cmap_adr = offb_map_reg(dp, 0, 0x6000, 0x1000);
  288. if (par->cmap_adr)
  289. par->cmap_type = cmap_gxt2000;
  290. }
  291. fix->visual = (par->cmap_type != cmap_unknown) ?
  292. FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_STATIC_PSEUDOCOLOR;
  293. } else
  294. fix->visual = FB_VISUAL_TRUECOLOR;
  295. var->xoffset = var->yoffset = 0;
  296. switch (depth) {
  297. case 8:
  298. var->bits_per_pixel = 8;
  299. var->red.offset = 0;
  300. var->red.length = 8;
  301. var->green.offset = 0;
  302. var->green.length = 8;
  303. var->blue.offset = 0;
  304. var->blue.length = 8;
  305. var->transp.offset = 0;
  306. var->transp.length = 0;
  307. break;
  308. case 15: /* RGB 555 */
  309. var->bits_per_pixel = 16;
  310. var->red.offset = 10;
  311. var->red.length = 5;
  312. var->green.offset = 5;
  313. var->green.length = 5;
  314. var->blue.offset = 0;
  315. var->blue.length = 5;
  316. var->transp.offset = 0;
  317. var->transp.length = 0;
  318. break;
  319. case 16: /* RGB 565 */
  320. var->bits_per_pixel = 16;
  321. var->red.offset = 11;
  322. var->red.length = 5;
  323. var->green.offset = 5;
  324. var->green.length = 6;
  325. var->blue.offset = 0;
  326. var->blue.length = 5;
  327. var->transp.offset = 0;
  328. var->transp.length = 0;
  329. break;
  330. case 32: /* RGB 888 */
  331. var->bits_per_pixel = 32;
  332. var->red.offset = 16;
  333. var->red.length = 8;
  334. var->green.offset = 8;
  335. var->green.length = 8;
  336. var->blue.offset = 0;
  337. var->blue.length = 8;
  338. var->transp.offset = 24;
  339. var->transp.length = 8;
  340. break;
  341. }
  342. var->red.msb_right = var->green.msb_right = var->blue.msb_right =
  343. var->transp.msb_right = 0;
  344. var->grayscale = 0;
  345. var->nonstd = 0;
  346. var->activate = 0;
  347. var->height = var->width = -1;
  348. var->pixclock = 10000;
  349. var->left_margin = var->right_margin = 16;
  350. var->upper_margin = var->lower_margin = 16;
  351. var->hsync_len = var->vsync_len = 8;
  352. var->sync = 0;
  353. var->vmode = FB_VMODE_NONINTERLACED;
  354. info->fbops = &offb_ops;
  355. info->screen_base = ioremap(address, fix->smem_len);
  356. info->par = par;
  357. info->pseudo_palette = (void *) (info + 1);
  358. info->flags = FBINFO_DEFAULT;
  359. fb_alloc_cmap(&info->cmap, 256, 0);
  360. if (register_framebuffer(info) < 0) {
  361. iounmap(par->cmap_adr);
  362. par->cmap_adr = NULL;
  363. iounmap(info->screen_base);
  364. kfree(info);
  365. release_mem_region(res_start, res_size);
  366. return;
  367. }
  368. printk(KERN_INFO "fb%d: Open Firmware frame buffer device on %s\n",
  369. info->node, full_name);
  370. }
  371. static void __init offb_init_nodriver(struct device_node *dp, int no_real_node)
  372. {
  373. unsigned int len;
  374. int i, width = 640, height = 480, depth = 8, pitch = 640;
  375. unsigned int flags, rsize, addr_prop = 0;
  376. unsigned long max_size = 0;
  377. u64 rstart, address = OF_BAD_ADDR;
  378. const u32 *pp, *addrp, *up;
  379. u64 asize;
  380. pp = get_property(dp, "linux,bootx-depth", &len);
  381. if (pp == NULL)
  382. pp = get_property(dp, "depth", &len);
  383. if (pp && len == sizeof(u32))
  384. depth = *pp;
  385. pp = get_property(dp, "linux,bootx-width", &len);
  386. if (pp == NULL)
  387. pp = get_property(dp, "width", &len);
  388. if (pp && len == sizeof(u32))
  389. width = *pp;
  390. pp = get_property(dp, "linux,bootx-height", &len);
  391. if (pp == NULL)
  392. pp = get_property(dp, "height", &len);
  393. if (pp && len == sizeof(u32))
  394. height = *pp;
  395. pp = get_property(dp, "linux,bootx-linebytes", &len);
  396. if (pp == NULL)
  397. pp = get_property(dp, "linebytes", &len);
  398. if (pp && len == sizeof(u32) && (*pp != 0xffffffffu))
  399. pitch = *pp;
  400. else
  401. pitch = width * ((depth + 7) / 8);
  402. rsize = (unsigned long)pitch * (unsigned long)height;
  403. /* Ok, now we try to figure out the address of the framebuffer.
  404. *
  405. * Unfortunately, Open Firmware doesn't provide a standard way to do
  406. * so. All we can do is a dodgy heuristic that happens to work in
  407. * practice. On most machines, the "address" property contains what
  408. * we need, though not on Matrox cards found in IBM machines. What I've
  409. * found that appears to give good results is to go through the PCI
  410. * ranges and pick one that is both big enough and if possible encloses
  411. * the "address" property. If none match, we pick the biggest
  412. */
  413. up = get_property(dp, "linux,bootx-addr", &len);
  414. if (up == NULL)
  415. up = get_property(dp, "address", &len);
  416. if (up && len == sizeof(u32))
  417. addr_prop = *up;
  418. /* Hack for when BootX is passing us */
  419. if (no_real_node)
  420. goto skip_addr;
  421. for (i = 0; (addrp = of_get_address(dp, i, &asize, &flags))
  422. != NULL; i++) {
  423. int match_addrp = 0;
  424. if (!(flags & IORESOURCE_MEM))
  425. continue;
  426. if (asize < rsize)
  427. continue;
  428. rstart = of_translate_address(dp, addrp);
  429. if (rstart == OF_BAD_ADDR)
  430. continue;
  431. if (addr_prop && (rstart <= addr_prop) &&
  432. ((rstart + asize) >= (addr_prop + rsize)))
  433. match_addrp = 1;
  434. if (match_addrp) {
  435. address = addr_prop;
  436. break;
  437. }
  438. if (rsize > max_size) {
  439. max_size = rsize;
  440. address = OF_BAD_ADDR;
  441. }
  442. if (address == OF_BAD_ADDR)
  443. address = rstart;
  444. }
  445. skip_addr:
  446. if (address == OF_BAD_ADDR && addr_prop)
  447. address = (u64)addr_prop;
  448. if (address != OF_BAD_ADDR) {
  449. /* kludge for valkyrie */
  450. if (strcmp(dp->name, "valkyrie") == 0)
  451. address += 0x1000;
  452. offb_init_fb(no_real_node ? "bootx" : dp->name,
  453. no_real_node ? "display" : dp->full_name,
  454. width, height, depth, pitch, address,
  455. no_real_node ? NULL : dp);
  456. }
  457. }
  458. static int __init offb_init(void)
  459. {
  460. struct device_node *dp = NULL, *boot_disp = NULL;
  461. if (fb_get_options("offb", NULL))
  462. return -ENODEV;
  463. /* Check if we have a MacOS display without a node spec */
  464. if (get_property(of_chosen, "linux,bootx-noscreen", NULL) != NULL) {
  465. /* The old code tried to work out which node was the MacOS
  466. * display based on the address. I'm dropping that since the
  467. * lack of a node spec only happens with old BootX versions
  468. * (users can update) and with this code, they'll still get
  469. * a display (just not the palette hacks).
  470. */
  471. offb_init_nodriver(of_chosen, 1);
  472. }
  473. for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
  474. if (get_property(dp, "linux,opened", NULL) &&
  475. get_property(dp, "linux,boot-display", NULL)) {
  476. boot_disp = dp;
  477. offb_init_nodriver(dp, 0);
  478. }
  479. }
  480. for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
  481. if (get_property(dp, "linux,opened", NULL) &&
  482. dp != boot_disp)
  483. offb_init_nodriver(dp, 0);
  484. }
  485. return 0;
  486. }
  487. module_init(offb_init);
  488. MODULE_LICENSE("GPL");