offb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. struct resource r;
  202. if (of_address_to_resource(np, index, &r))
  203. return 0;
  204. if ((r.start + offset + size) > r.end)
  205. return 0;
  206. return ioremap(r.start + offset, size);
  207. }
  208. static void __init offb_init_fb(const char *name, const char *full_name,
  209. int width, int height, int depth,
  210. int pitch, unsigned long address,
  211. struct device_node *dp)
  212. {
  213. unsigned long res_size = pitch * height * (depth + 7) / 8;
  214. struct offb_par *par = &default_par;
  215. unsigned long res_start = address;
  216. struct fb_fix_screeninfo *fix;
  217. struct fb_var_screeninfo *var;
  218. struct fb_info *info;
  219. int size;
  220. if (!request_mem_region(res_start, res_size, "offb"))
  221. return;
  222. printk(KERN_INFO
  223. "Using unsupported %dx%d %s at %lx, depth=%d, pitch=%d\n",
  224. width, height, name, address, depth, pitch);
  225. if (depth != 8 && depth != 15 && depth != 16 && depth != 32) {
  226. printk(KERN_ERR "%s: can't use depth = %d\n", full_name,
  227. depth);
  228. release_mem_region(res_start, res_size);
  229. return;
  230. }
  231. size = sizeof(struct fb_info) + sizeof(u32) * 17;
  232. info = kmalloc(size, GFP_ATOMIC);
  233. if (info == 0) {
  234. release_mem_region(res_start, res_size);
  235. return;
  236. }
  237. memset(info, 0, size);
  238. fix = &info->fix;
  239. var = &info->var;
  240. strcpy(fix->id, "OFfb ");
  241. strncat(fix->id, name, sizeof(fix->id) - sizeof("OFfb "));
  242. fix->id[sizeof(fix->id) - 1] = '\0';
  243. var->xres = var->xres_virtual = width;
  244. var->yres = var->yres_virtual = height;
  245. fix->line_length = pitch;
  246. fix->smem_start = address;
  247. fix->smem_len = pitch * height;
  248. fix->type = FB_TYPE_PACKED_PIXELS;
  249. fix->type_aux = 0;
  250. par->cmap_type = cmap_unknown;
  251. if (depth == 8) {
  252. /* Palette hacks disabled for now */
  253. if (dp && !strncmp(name, "ATY,Rage128", 11)) {
  254. par->cmap_adr = offb_map_reg(dp, 2, 0, 0x1fff);
  255. if (par->cmap_adr)
  256. par->cmap_type = cmap_r128;
  257. } else if (dp && (!strncmp(name, "ATY,RageM3pA", 12)
  258. || !strncmp(name, "ATY,RageM3p12A", 14))) {
  259. par->cmap_adr = offb_map_reg(dp, 2, 0, 0x1fff);
  260. if (par->cmap_adr)
  261. par->cmap_type = cmap_M3A;
  262. } else if (dp && !strncmp(name, "ATY,RageM3pB", 12)) {
  263. par->cmap_adr = offb_map_reg(dp, 2, 0, 0x1fff);
  264. if (par->cmap_adr)
  265. par->cmap_type = cmap_M3B;
  266. } else if (dp && !strncmp(name, "ATY,Rage6", 9)) {
  267. par->cmap_adr = offb_map_reg(dp, 1, 0, 0x1fff);
  268. if (par->cmap_adr)
  269. par->cmap_type = cmap_radeon;
  270. } else if (!strncmp(name, "ATY,", 4)) {
  271. unsigned long base = address & 0xff000000UL;
  272. par->cmap_adr =
  273. ioremap(base + 0x7ff000, 0x1000) + 0xcc0;
  274. par->cmap_data = par->cmap_adr + 1;
  275. par->cmap_type = cmap_m64;
  276. } else if (dp && device_is_compatible(dp, "pci1014,b7")) {
  277. par->cmap_adr = offb_map_reg(dp, 0, 0x6000, 0x1000);
  278. if (par->cmap_adr)
  279. par->cmap_type = cmap_gxt2000;
  280. }
  281. fix->visual = (par->cmap_type != cmap_unknown) ?
  282. FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_STATIC_PSEUDOCOLOR;
  283. } else
  284. fix->visual = FB_VISUAL_TRUECOLOR;
  285. var->xoffset = var->yoffset = 0;
  286. switch (depth) {
  287. case 8:
  288. var->bits_per_pixel = 8;
  289. var->red.offset = 0;
  290. var->red.length = 8;
  291. var->green.offset = 0;
  292. var->green.length = 8;
  293. var->blue.offset = 0;
  294. var->blue.length = 8;
  295. var->transp.offset = 0;
  296. var->transp.length = 0;
  297. break;
  298. case 15: /* RGB 555 */
  299. var->bits_per_pixel = 16;
  300. var->red.offset = 10;
  301. var->red.length = 5;
  302. var->green.offset = 5;
  303. var->green.length = 5;
  304. var->blue.offset = 0;
  305. var->blue.length = 5;
  306. var->transp.offset = 0;
  307. var->transp.length = 0;
  308. break;
  309. case 16: /* RGB 565 */
  310. var->bits_per_pixel = 16;
  311. var->red.offset = 11;
  312. var->red.length = 5;
  313. var->green.offset = 5;
  314. var->green.length = 6;
  315. var->blue.offset = 0;
  316. var->blue.length = 5;
  317. var->transp.offset = 0;
  318. var->transp.length = 0;
  319. break;
  320. case 32: /* RGB 888 */
  321. var->bits_per_pixel = 32;
  322. var->red.offset = 16;
  323. var->red.length = 8;
  324. var->green.offset = 8;
  325. var->green.length = 8;
  326. var->blue.offset = 0;
  327. var->blue.length = 8;
  328. var->transp.offset = 24;
  329. var->transp.length = 8;
  330. break;
  331. }
  332. var->red.msb_right = var->green.msb_right = var->blue.msb_right =
  333. var->transp.msb_right = 0;
  334. var->grayscale = 0;
  335. var->nonstd = 0;
  336. var->activate = 0;
  337. var->height = var->width = -1;
  338. var->pixclock = 10000;
  339. var->left_margin = var->right_margin = 16;
  340. var->upper_margin = var->lower_margin = 16;
  341. var->hsync_len = var->vsync_len = 8;
  342. var->sync = 0;
  343. var->vmode = FB_VMODE_NONINTERLACED;
  344. info->fbops = &offb_ops;
  345. info->screen_base = ioremap(address, fix->smem_len);
  346. info->par = par;
  347. info->pseudo_palette = (void *) (info + 1);
  348. info->flags = FBINFO_DEFAULT;
  349. fb_alloc_cmap(&info->cmap, 256, 0);
  350. if (register_framebuffer(info) < 0) {
  351. kfree(info);
  352. release_mem_region(res_start, res_size);
  353. return;
  354. }
  355. printk(KERN_INFO "fb%d: Open Firmware frame buffer device on %s\n",
  356. info->node, full_name);
  357. }
  358. static void __init offb_init_nodriver(struct device_node *dp, int no_real_node)
  359. {
  360. unsigned int len;
  361. int i, width = 640, height = 480, depth = 8, pitch = 640;
  362. unsigned int flags, rsize, addr_prop = 0;
  363. unsigned long max_size = 0;
  364. u64 rstart, address = OF_BAD_ADDR;
  365. const u32 *pp, *addrp, *up;
  366. u64 asize;
  367. pp = get_property(dp, "linux,bootx-depth", &len);
  368. if (pp == NULL)
  369. pp = get_property(dp, "depth", &len);
  370. if (pp && len == sizeof(u32))
  371. depth = *pp;
  372. pp = get_property(dp, "linux,bootx-width", &len);
  373. if (pp == NULL)
  374. pp = get_property(dp, "width", &len);
  375. if (pp && len == sizeof(u32))
  376. width = *pp;
  377. pp = get_property(dp, "linux,bootx-height", &len);
  378. if (pp == NULL)
  379. pp = get_property(dp, "height", &len);
  380. if (pp && len == sizeof(u32))
  381. height = *pp;
  382. pp = get_property(dp, "linux,bootx-linebytes", &len);
  383. if (pp == NULL)
  384. pp = get_property(dp, "linebytes", &len);
  385. if (pp && len == sizeof(u32))
  386. pitch = *pp;
  387. else
  388. pitch = width * ((depth + 7) / 8);
  389. rsize = (unsigned long)pitch * (unsigned long)height;
  390. /* Ok, now we try to figure out the address of the framebuffer.
  391. *
  392. * Unfortunately, Open Firmware doesn't provide a standard way to do
  393. * so. All we can do is a dodgy heuristic that happens to work in
  394. * practice. On most machines, the "address" property contains what
  395. * we need, though not on Matrox cards found in IBM machines. What I've
  396. * found that appears to give good results is to go through the PCI
  397. * ranges and pick one that is both big enough and if possible encloses
  398. * the "address" property. If none match, we pick the biggest
  399. */
  400. up = get_property(dp, "linux,bootx-addr", &len);
  401. if (up == NULL)
  402. up = get_property(dp, "address", &len);
  403. if (up && len == sizeof(u32))
  404. addr_prop = *up;
  405. /* Hack for when BootX is passing us */
  406. if (no_real_node)
  407. goto skip_addr;
  408. for (i = 0; (addrp = of_get_address(dp, i, &asize, &flags))
  409. != NULL; i++) {
  410. int match_addrp = 0;
  411. if (!(flags & IORESOURCE_MEM))
  412. continue;
  413. if (asize < rsize)
  414. continue;
  415. rstart = of_translate_address(dp, addrp);
  416. if (rstart == OF_BAD_ADDR)
  417. continue;
  418. if (addr_prop && (rstart <= addr_prop) &&
  419. ((rstart + asize) >= (addr_prop + rsize)))
  420. match_addrp = 1;
  421. if (match_addrp) {
  422. address = addr_prop;
  423. break;
  424. }
  425. if (rsize > max_size) {
  426. max_size = rsize;
  427. address = OF_BAD_ADDR;
  428. }
  429. if (address == OF_BAD_ADDR)
  430. address = rstart;
  431. }
  432. skip_addr:
  433. if (address == OF_BAD_ADDR && addr_prop)
  434. address = (u64)addr_prop;
  435. if (address != OF_BAD_ADDR) {
  436. /* kludge for valkyrie */
  437. if (strcmp(dp->name, "valkyrie") == 0)
  438. address += 0x1000;
  439. offb_init_fb(no_real_node ? "bootx" : dp->name,
  440. no_real_node ? "display" : dp->full_name,
  441. width, height, depth, pitch, address,
  442. no_real_node ? dp : NULL);
  443. }
  444. }
  445. static int __init offb_init(void)
  446. {
  447. struct device_node *dp = NULL, *boot_disp = NULL;
  448. if (fb_get_options("offb", NULL))
  449. return -ENODEV;
  450. /* Check if we have a MacOS display without a node spec */
  451. if (get_property(of_chosen, "linux,bootx-noscreen", NULL) != NULL) {
  452. /* The old code tried to work out which node was the MacOS
  453. * display based on the address. I'm dropping that since the
  454. * lack of a node spec only happens with old BootX versions
  455. * (users can update) and with this code, they'll still get
  456. * a display (just not the palette hacks).
  457. */
  458. offb_init_nodriver(of_chosen, 1);
  459. }
  460. for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
  461. if (get_property(dp, "linux,opened", NULL) &&
  462. get_property(dp, "linux,boot-display", NULL)) {
  463. boot_disp = dp;
  464. offb_init_nodriver(dp, 0);
  465. }
  466. }
  467. for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
  468. if (get_property(dp, "linux,opened", NULL) &&
  469. dp != boot_disp)
  470. offb_init_nodriver(dp, 0);
  471. }
  472. return 0;
  473. }
  474. module_init(offb_init);
  475. MODULE_LICENSE("GPL");