offb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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/config.h>
  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/tty.h>
  21. #include <linux/slab.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/delay.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 <asm/io.h>
  30. #include <asm/prom.h>
  31. #ifdef CONFIG_PPC64
  32. #include <asm/pci-bridge.h>
  33. #endif
  34. #ifdef CONFIG_PPC32
  35. #include <asm/bootx.h>
  36. #endif
  37. #include "macmodes.h"
  38. /* Supported palette hacks */
  39. enum {
  40. cmap_unknown,
  41. cmap_m64, /* 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. };
  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. /*
  56. * Interface used by the world
  57. */
  58. int offb_init(void);
  59. static int offb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  60. u_int transp, struct fb_info *info);
  61. static int offb_blank(int blank, struct fb_info *info);
  62. #ifdef CONFIG_PPC32
  63. extern boot_infos_t *boot_infos;
  64. #endif
  65. static void offb_init_nodriver(struct device_node *);
  66. static void offb_init_fb(const char *name, const char *full_name,
  67. int width, int height, int depth, int pitch,
  68. unsigned long address, struct device_node *dp);
  69. static struct fb_ops offb_ops = {
  70. .owner = THIS_MODULE,
  71. .fb_setcolreg = offb_setcolreg,
  72. .fb_blank = offb_blank,
  73. .fb_fillrect = cfb_fillrect,
  74. .fb_copyarea = cfb_copyarea,
  75. .fb_imageblit = cfb_imageblit,
  76. };
  77. /*
  78. * Set a single color register. The values supplied are already
  79. * rounded down to the hardware's capabilities (according to the
  80. * entries in the var structure). Return != 0 for invalid regno.
  81. */
  82. static int offb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  83. u_int transp, struct fb_info *info)
  84. {
  85. struct offb_par *par = (struct offb_par *) info->par;
  86. if (!par->cmap_adr || regno > 255)
  87. return 1;
  88. red >>= 8;
  89. green >>= 8;
  90. blue >>= 8;
  91. switch (par->cmap_type) {
  92. case cmap_m64:
  93. writeb(regno, par->cmap_adr);
  94. writeb(red, par->cmap_data);
  95. writeb(green, par->cmap_data);
  96. writeb(blue, par->cmap_data);
  97. break;
  98. case cmap_M3A:
  99. /* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
  100. out_le32(par->cmap_adr + 0x58,
  101. in_le32(par->cmap_adr + 0x58) & ~0x20);
  102. case cmap_r128:
  103. /* Set palette index & data */
  104. out_8(par->cmap_adr + 0xb0, regno);
  105. out_le32(par->cmap_adr + 0xb4,
  106. (red << 16 | green << 8 | blue));
  107. break;
  108. case cmap_M3B:
  109. /* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
  110. out_le32(par->cmap_adr + 0x58,
  111. in_le32(par->cmap_adr + 0x58) | 0x20);
  112. /* Set palette index & data */
  113. out_8(par->cmap_adr + 0xb0, regno);
  114. out_le32(par->cmap_adr + 0xb4, (red << 16 | green << 8 | blue));
  115. break;
  116. case cmap_radeon:
  117. /* Set palette index & data (could be smarter) */
  118. out_8(par->cmap_adr + 0xb0, regno);
  119. out_le32(par->cmap_adr + 0xb4, (red << 16 | green << 8 | blue));
  120. break;
  121. case cmap_gxt2000:
  122. out_le32((unsigned __iomem *) par->cmap_adr + regno,
  123. (red << 16 | green << 8 | blue));
  124. break;
  125. }
  126. if (regno < 16)
  127. switch (info->var.bits_per_pixel) {
  128. case 16:
  129. ((u16 *) (info->pseudo_palette))[regno] =
  130. (regno << 10) | (regno << 5) | regno;
  131. break;
  132. case 32:
  133. {
  134. int i = (regno << 8) | regno;
  135. ((u32 *) (info->pseudo_palette))[regno] =
  136. (i << 16) | i;
  137. break;
  138. }
  139. }
  140. return 0;
  141. }
  142. /*
  143. * Blank the display.
  144. */
  145. static int offb_blank(int blank, struct fb_info *info)
  146. {
  147. struct offb_par *par = (struct offb_par *) info->par;
  148. int i, j;
  149. if (!par->cmap_adr)
  150. return 0;
  151. if (!par->blanked)
  152. if (!blank)
  153. return 0;
  154. par->blanked = blank;
  155. if (blank)
  156. for (i = 0; i < 256; i++) {
  157. switch (par->cmap_type) {
  158. case cmap_m64:
  159. writeb(i, par->cmap_adr);
  160. for (j = 0; j < 3; j++)
  161. writeb(0, par->cmap_data);
  162. break;
  163. case cmap_M3A:
  164. /* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
  165. out_le32(par->cmap_adr + 0x58,
  166. in_le32(par->cmap_adr + 0x58) & ~0x20);
  167. case cmap_r128:
  168. /* Set palette index & data */
  169. out_8(par->cmap_adr + 0xb0, i);
  170. out_le32(par->cmap_adr + 0xb4, 0);
  171. break;
  172. case cmap_M3B:
  173. /* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
  174. out_le32(par->cmap_adr + 0x58,
  175. in_le32(par->cmap_adr + 0x58) | 0x20);
  176. /* Set palette index & data */
  177. out_8(par->cmap_adr + 0xb0, i);
  178. out_le32(par->cmap_adr + 0xb4, 0);
  179. break;
  180. case cmap_radeon:
  181. out_8(par->cmap_adr + 0xb0, i);
  182. out_le32(par->cmap_adr + 0xb4, 0);
  183. break;
  184. case cmap_gxt2000:
  185. out_le32((unsigned __iomem *) par->cmap_adr + i,
  186. 0);
  187. break;
  188. }
  189. } else
  190. fb_set_cmap(&info->cmap, info);
  191. return 0;
  192. }
  193. /*
  194. * Initialisation
  195. */
  196. int __init offb_init(void)
  197. {
  198. struct device_node *dp = NULL, *boot_disp = NULL;
  199. #if defined(CONFIG_BOOTX_TEXT) && defined(CONFIG_PPC32)
  200. struct device_node *macos_display = NULL;
  201. #endif
  202. if (fb_get_options("offb", NULL))
  203. return -ENODEV;
  204. #if defined(CONFIG_BOOTX_TEXT) && defined(CONFIG_PPC32)
  205. /* If we're booted from BootX... */
  206. if (boot_infos != 0) {
  207. unsigned long addr =
  208. (unsigned long) boot_infos->dispDeviceBase;
  209. u32 *addrp;
  210. u64 daddr, dsize;
  211. unsigned int flags;
  212. /* find the device node corresponding to the macos display */
  213. while ((dp = of_find_node_by_type(dp, "display"))) {
  214. int i;
  215. /*
  216. * Look for an AAPL,address property first.
  217. */
  218. unsigned int na;
  219. unsigned int *ap =
  220. (unsigned int *)get_property(dp, "AAPL,address",
  221. &na);
  222. if (ap != 0) {
  223. for (na /= sizeof(unsigned int); na > 0;
  224. --na, ++ap)
  225. if (*ap <= addr &&
  226. addr < *ap + 0x1000000) {
  227. macos_display = dp;
  228. goto foundit;
  229. }
  230. }
  231. /*
  232. * See if the display address is in one of the address
  233. * ranges for this display.
  234. */
  235. i = 0;
  236. for (;;) {
  237. addrp = of_get_address(dp, i++, &dsize, &flags);
  238. if (addrp == NULL)
  239. break;
  240. if (!(flags & IORESOURCE_MEM))
  241. continue;
  242. daddr = of_translate_address(dp, addrp);
  243. if (daddr == OF_BAD_ADDR)
  244. continue;
  245. if (daddr <= addr && addr < (daddr + dsize)) {
  246. macos_display = dp;
  247. goto foundit;
  248. }
  249. }
  250. foundit:
  251. if (macos_display) {
  252. printk(KERN_INFO "MacOS display is %s\n",
  253. dp->full_name);
  254. break;
  255. }
  256. }
  257. /* initialize it */
  258. offb_init_fb(macos_display ? macos_display->
  259. name : "MacOS display",
  260. macos_display ? macos_display->
  261. full_name : "MacOS display",
  262. boot_infos->dispDeviceRect[2],
  263. boot_infos->dispDeviceRect[3],
  264. boot_infos->dispDeviceDepth,
  265. boot_infos->dispDeviceRowBytes, addr, NULL);
  266. }
  267. #endif /* defined(CONFIG_BOOTX_TEXT) && defined(CONFIG_PPC32) */
  268. for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
  269. if (get_property(dp, "linux,opened", NULL) &&
  270. get_property(dp, "linux,boot-display", NULL)) {
  271. boot_disp = dp;
  272. offb_init_nodriver(dp);
  273. }
  274. }
  275. for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
  276. if (get_property(dp, "linux,opened", NULL) &&
  277. dp != boot_disp)
  278. offb_init_nodriver(dp);
  279. }
  280. return 0;
  281. }
  282. static void __init offb_init_nodriver(struct device_node *dp)
  283. {
  284. int *pp, i;
  285. unsigned int len;
  286. int width = 640, height = 480, depth = 8, pitch;
  287. unsigned int flags, rsize, *up;
  288. u64 address = OF_BAD_ADDR;
  289. u32 *addrp;
  290. u64 asize;
  291. if ((pp = (int *) get_property(dp, "depth", &len)) != NULL
  292. && len == sizeof(int))
  293. depth = *pp;
  294. if ((pp = (int *) get_property(dp, "width", &len)) != NULL
  295. && len == sizeof(int))
  296. width = *pp;
  297. if ((pp = (int *) get_property(dp, "height", &len)) != NULL
  298. && len == sizeof(int))
  299. height = *pp;
  300. if ((pp = (int *) get_property(dp, "linebytes", &len)) != NULL
  301. && len == sizeof(int)) {
  302. pitch = *pp;
  303. if (pitch == 1)
  304. pitch = 0x1000;
  305. } else
  306. pitch = width;
  307. rsize = (unsigned long)pitch * (unsigned long)height *
  308. (unsigned long)(depth / 8);
  309. /* Try to match device to a PCI device in order to get a properly
  310. * translated address rather then trying to decode the open firmware
  311. * stuff in various incorrect ways
  312. */
  313. #ifdef CONFIG_PCI
  314. /* First try to locate the PCI device if any */
  315. {
  316. struct pci_dev *pdev = NULL;
  317. for_each_pci_dev(pdev) {
  318. if (dp == pci_device_to_OF_node(pdev))
  319. break;
  320. }
  321. if (pdev) {
  322. for (i = 0; i < 6 && address == OF_BAD_ADDR; i++) {
  323. if ((pci_resource_flags(pdev, i) &
  324. IORESOURCE_MEM) &&
  325. (pci_resource_len(pdev, i) >= rsize))
  326. address = pci_resource_start(pdev, i);
  327. }
  328. pci_dev_put(pdev);
  329. }
  330. }
  331. #endif /* CONFIG_PCI */
  332. /* This one is dodgy, we may drop it ... */
  333. if (address == OF_BAD_ADDR &&
  334. (up = (unsigned *) get_property(dp, "address", &len)) != NULL &&
  335. len == sizeof(unsigned int))
  336. address = (u64) * up;
  337. if (address == OF_BAD_ADDR) {
  338. for (i = 0; (addrp = of_get_address(dp, i, &asize, &flags))
  339. != NULL; i++) {
  340. if (!(flags & IORESOURCE_MEM))
  341. continue;
  342. if (asize >= pitch * height * depth / 8)
  343. break;
  344. }
  345. if (addrp == NULL) {
  346. printk(KERN_ERR
  347. "no framebuffer address found for %s\n",
  348. dp->full_name);
  349. return;
  350. }
  351. address = of_translate_address(dp, addrp);
  352. if (address == OF_BAD_ADDR) {
  353. printk(KERN_ERR
  354. "can't translate framebuffer address for %s\n",
  355. dp->full_name);
  356. return;
  357. }
  358. /* kludge for valkyrie */
  359. if (strcmp(dp->name, "valkyrie") == 0)
  360. address += 0x1000;
  361. }
  362. offb_init_fb(dp->name, dp->full_name, width, height, depth,
  363. pitch, address, dp);
  364. }
  365. static void __init offb_init_fb(const char *name, const char *full_name,
  366. int width, int height, int depth,
  367. int pitch, unsigned long address,
  368. struct device_node *dp)
  369. {
  370. unsigned long res_size = pitch * height * depth / 8;
  371. struct offb_par *par = &default_par;
  372. unsigned long res_start = address;
  373. struct fb_fix_screeninfo *fix;
  374. struct fb_var_screeninfo *var;
  375. struct fb_info *info;
  376. int size;
  377. if (!request_mem_region(res_start, res_size, "offb"))
  378. return;
  379. printk(KERN_INFO
  380. "Using unsupported %dx%d %s at %lx, depth=%d, pitch=%d\n",
  381. width, height, name, address, depth, pitch);
  382. if (depth != 8 && depth != 16 && depth != 32) {
  383. printk(KERN_ERR "%s: can't use depth = %d\n", full_name,
  384. depth);
  385. release_mem_region(res_start, res_size);
  386. return;
  387. }
  388. size = sizeof(struct fb_info) + sizeof(u32) * 17;
  389. info = kmalloc(size, GFP_ATOMIC);
  390. if (info == 0) {
  391. release_mem_region(res_start, res_size);
  392. return;
  393. }
  394. memset(info, 0, size);
  395. fix = &info->fix;
  396. var = &info->var;
  397. strcpy(fix->id, "OFfb ");
  398. strncat(fix->id, name, sizeof(fix->id) - sizeof("OFfb "));
  399. fix->id[sizeof(fix->id) - 1] = '\0';
  400. var->xres = var->xres_virtual = width;
  401. var->yres = var->yres_virtual = height;
  402. fix->line_length = pitch;
  403. fix->smem_start = address;
  404. fix->smem_len = pitch * height;
  405. fix->type = FB_TYPE_PACKED_PIXELS;
  406. fix->type_aux = 0;
  407. par->cmap_type = cmap_unknown;
  408. if (depth == 8) {
  409. /* Palette hacks disabled for now */
  410. #if 0
  411. if (dp && !strncmp(name, "ATY,Rage128", 11)) {
  412. unsigned long regbase = dp->addrs[2].address;
  413. par->cmap_adr = ioremap(regbase, 0x1FFF);
  414. par->cmap_type = cmap_r128;
  415. } else if (dp && (!strncmp(name, "ATY,RageM3pA", 12)
  416. || !strncmp(name, "ATY,RageM3p12A", 14))) {
  417. unsigned long regbase =
  418. dp->parent->addrs[2].address;
  419. par->cmap_adr = ioremap(regbase, 0x1FFF);
  420. par->cmap_type = cmap_M3A;
  421. } else if (dp && !strncmp(name, "ATY,RageM3pB", 12)) {
  422. unsigned long regbase =
  423. dp->parent->addrs[2].address;
  424. par->cmap_adr = ioremap(regbase, 0x1FFF);
  425. par->cmap_type = cmap_M3B;
  426. } else if (dp && !strncmp(name, "ATY,Rage6", 9)) {
  427. unsigned long regbase = dp->addrs[1].address;
  428. par->cmap_adr = ioremap(regbase, 0x1FFF);
  429. par->cmap_type = cmap_radeon;
  430. } else if (!strncmp(name, "ATY,", 4)) {
  431. unsigned long base = address & 0xff000000UL;
  432. par->cmap_adr =
  433. ioremap(base + 0x7ff000, 0x1000) + 0xcc0;
  434. par->cmap_data = par->cmap_adr + 1;
  435. par->cmap_type = cmap_m64;
  436. } else if (device_is_compatible(dp, "pci1014,b7")) {
  437. unsigned long regbase = dp->addrs[0].address;
  438. par->cmap_adr = ioremap(regbase + 0x6000, 0x1000);
  439. par->cmap_type = cmap_gxt2000;
  440. }
  441. #endif
  442. fix->visual = par->cmap_adr ? FB_VISUAL_PSEUDOCOLOR
  443. : FB_VISUAL_STATIC_PSEUDOCOLOR;
  444. } else
  445. fix->visual = /* par->cmap_adr ? FB_VISUAL_DIRECTCOLOR
  446. : */ FB_VISUAL_TRUECOLOR;
  447. var->xoffset = var->yoffset = 0;
  448. var->bits_per_pixel = depth;
  449. switch (depth) {
  450. case 8:
  451. var->bits_per_pixel = 8;
  452. var->red.offset = 0;
  453. var->red.length = 8;
  454. var->green.offset = 0;
  455. var->green.length = 8;
  456. var->blue.offset = 0;
  457. var->blue.length = 8;
  458. var->transp.offset = 0;
  459. var->transp.length = 0;
  460. break;
  461. case 16: /* RGB 555 */
  462. var->bits_per_pixel = 16;
  463. var->red.offset = 10;
  464. var->red.length = 5;
  465. var->green.offset = 5;
  466. var->green.length = 5;
  467. var->blue.offset = 0;
  468. var->blue.length = 5;
  469. var->transp.offset = 0;
  470. var->transp.length = 0;
  471. break;
  472. case 32: /* RGB 888 */
  473. var->bits_per_pixel = 32;
  474. var->red.offset = 16;
  475. var->red.length = 8;
  476. var->green.offset = 8;
  477. var->green.length = 8;
  478. var->blue.offset = 0;
  479. var->blue.length = 8;
  480. var->transp.offset = 24;
  481. var->transp.length = 8;
  482. break;
  483. }
  484. var->red.msb_right = var->green.msb_right = var->blue.msb_right =
  485. var->transp.msb_right = 0;
  486. var->grayscale = 0;
  487. var->nonstd = 0;
  488. var->activate = 0;
  489. var->height = var->width = -1;
  490. var->pixclock = 10000;
  491. var->left_margin = var->right_margin = 16;
  492. var->upper_margin = var->lower_margin = 16;
  493. var->hsync_len = var->vsync_len = 8;
  494. var->sync = 0;
  495. var->vmode = FB_VMODE_NONINTERLACED;
  496. info->fbops = &offb_ops;
  497. info->screen_base = ioremap(address, fix->smem_len);
  498. info->par = par;
  499. info->pseudo_palette = (void *) (info + 1);
  500. info->flags = FBINFO_DEFAULT;
  501. fb_alloc_cmap(&info->cmap, 256, 0);
  502. if (register_framebuffer(info) < 0) {
  503. kfree(info);
  504. release_mem_region(res_start, res_size);
  505. return;
  506. }
  507. printk(KERN_INFO "fb%d: Open Firmware frame buffer device on %s\n",
  508. info->node, full_name);
  509. }
  510. module_init(offb_init);
  511. MODULE_LICENSE("GPL");