offb.c 14 KB

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