offb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. /* find the device node corresponding to the macos display */
  210. while ((dp = of_find_node_by_type(dp, "display"))) {
  211. int i;
  212. /*
  213. * Grrr... It looks like the MacOS ATI driver
  214. * munges the assigned-addresses property (but
  215. * the AAPL,address value is OK).
  216. */
  217. if (strncmp(dp->name, "ATY,", 4) == 0
  218. && dp->n_addrs == 1) {
  219. unsigned int *ap =
  220. (unsigned int *) get_property(dp,
  221. "AAPL,address",
  222. NULL);
  223. if (ap != NULL) {
  224. dp->addrs[0].address = *ap;
  225. dp->addrs[0].size = 0x01000000;
  226. }
  227. }
  228. /*
  229. * The LTPro on the Lombard powerbook has no addresses
  230. * on the display nodes, they are on their parent.
  231. */
  232. if (dp->n_addrs == 0
  233. && device_is_compatible(dp, "ATY,264LTPro")) {
  234. int na;
  235. unsigned int *ap = (unsigned int *)
  236. get_property(dp, "AAPL,address", &na);
  237. if (ap != 0)
  238. for (na /= sizeof(unsigned int);
  239. na > 0; --na, ++ap)
  240. if (*ap <= addr
  241. && addr <
  242. *ap + 0x1000000)
  243. goto foundit;
  244. }
  245. /*
  246. * See if the display address is in one of the address
  247. * ranges for this display.
  248. */
  249. for (i = 0; i < dp->n_addrs; ++i) {
  250. if (dp->addrs[i].address <= addr
  251. && addr <
  252. dp->addrs[i].address +
  253. dp->addrs[i].size)
  254. break;
  255. }
  256. if (i < dp->n_addrs) {
  257. foundit:
  258. printk(KERN_INFO "MacOS display is %s\n",
  259. dp->full_name);
  260. macos_display = dp;
  261. break;
  262. }
  263. }
  264. /* initialize it */
  265. offb_init_fb(macos_display ? macos_display->
  266. name : "MacOS display",
  267. macos_display ? macos_display->
  268. full_name : "MacOS display",
  269. boot_infos->dispDeviceRect[2],
  270. boot_infos->dispDeviceRect[3],
  271. boot_infos->dispDeviceDepth,
  272. boot_infos->dispDeviceRowBytes, addr, NULL);
  273. }
  274. #endif /* defined(CONFIG_BOOTX_TEXT) && defined(CONFIG_PPC32) */
  275. for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
  276. if (get_property(dp, "linux,opened", NULL) &&
  277. get_property(dp, "linux,boot-display", NULL)) {
  278. boot_disp = dp;
  279. offb_init_nodriver(dp);
  280. }
  281. }
  282. for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
  283. if (get_property(dp, "linux,opened", NULL) &&
  284. dp != boot_disp)
  285. offb_init_nodriver(dp);
  286. }
  287. return 0;
  288. }
  289. static void __init offb_init_nodriver(struct device_node *dp)
  290. {
  291. int *pp, i;
  292. unsigned int len;
  293. int width = 640, height = 480, depth = 8, pitch;
  294. unsigned int rsize, *up;
  295. unsigned long address = 0;
  296. if ((pp = (int *) get_property(dp, "depth", &len)) != NULL
  297. && len == sizeof(int))
  298. depth = *pp;
  299. if ((pp = (int *) get_property(dp, "width", &len)) != NULL
  300. && len == sizeof(int))
  301. width = *pp;
  302. if ((pp = (int *) get_property(dp, "height", &len)) != NULL
  303. && len == sizeof(int))
  304. height = *pp;
  305. if ((pp = (int *) get_property(dp, "linebytes", &len)) != NULL
  306. && len == sizeof(int)) {
  307. pitch = *pp;
  308. if (pitch == 1)
  309. pitch = 0x1000;
  310. } else
  311. pitch = width;
  312. rsize = (unsigned long)pitch * (unsigned long)height *
  313. (unsigned long)(depth / 8);
  314. /* Try to match device to a PCI device in order to get a properly
  315. * translated address rather then trying to decode the open firmware
  316. * stuff in various incorrect ways
  317. */
  318. #ifdef CONFIG_PCI
  319. /* First try to locate the PCI device if any */
  320. {
  321. struct pci_dev *pdev = NULL;
  322. for_each_pci_dev(pdev) {
  323. if (dp == pci_device_to_OF_node(pdev))
  324. break;
  325. }
  326. if (pdev) {
  327. for (i = 0; i < 6 && address == 0; i++) {
  328. if ((pci_resource_flags(pdev, i) &
  329. IORESOURCE_MEM) &&
  330. (pci_resource_len(pdev, i) >= rsize))
  331. address = pci_resource_start(pdev, i);
  332. }
  333. pci_dev_put(pdev);
  334. }
  335. }
  336. #endif /* CONFIG_PCI */
  337. if (address == 0 &&
  338. (up = (unsigned *) get_property(dp, "address", &len)) != NULL &&
  339. len == sizeof(unsigned))
  340. address = (u_long) * up;
  341. if (address == 0) {
  342. for (i = 0; i < dp->n_addrs; ++i)
  343. if (dp->addrs[i].size >=
  344. pitch * height * depth / 8)
  345. break;
  346. if (i >= dp->n_addrs) {
  347. printk(KERN_ERR
  348. "no framebuffer address found for %s\n",
  349. dp->full_name);
  350. return;
  351. }
  352. address = (u_long) dp->addrs[i].address;
  353. #ifdef CONFIG_PPC64
  354. address += ((struct pci_dn *)dp->data)->phb->pci_mem_offset;
  355. #endif
  356. /* kludge for valkyrie */
  357. if (strcmp(dp->name, "valkyrie") == 0)
  358. address += 0x1000;
  359. }
  360. offb_init_fb(dp->name, dp->full_name, width, height, depth,
  361. pitch, address, dp);
  362. }
  363. static void __init offb_init_fb(const char *name, const char *full_name,
  364. int width, int height, int depth,
  365. int pitch, unsigned long address,
  366. struct device_node *dp)
  367. {
  368. unsigned long res_size = pitch * height * depth / 8;
  369. struct offb_par *par = &default_par;
  370. unsigned long res_start = address;
  371. struct fb_fix_screeninfo *fix;
  372. struct fb_var_screeninfo *var;
  373. struct fb_info *info;
  374. int size;
  375. if (!request_mem_region(res_start, res_size, "offb"))
  376. return;
  377. printk(KERN_INFO
  378. "Using unsupported %dx%d %s at %lx, depth=%d, pitch=%d\n",
  379. width, height, name, address, depth, pitch);
  380. if (depth != 8 && depth != 16 && depth != 32) {
  381. printk(KERN_ERR "%s: can't use depth = %d\n", full_name,
  382. depth);
  383. release_mem_region(res_start, res_size);
  384. return;
  385. }
  386. size = sizeof(struct fb_info) + sizeof(u32) * 17;
  387. info = kmalloc(size, GFP_ATOMIC);
  388. if (info == 0) {
  389. release_mem_region(res_start, res_size);
  390. return;
  391. }
  392. memset(info, 0, size);
  393. fix = &info->fix;
  394. var = &info->var;
  395. strcpy(fix->id, "OFfb ");
  396. strncat(fix->id, name, sizeof(fix->id) - sizeof("OFfb "));
  397. fix->id[sizeof(fix->id) - 1] = '\0';
  398. var->xres = var->xres_virtual = width;
  399. var->yres = var->yres_virtual = height;
  400. fix->line_length = pitch;
  401. fix->smem_start = address;
  402. fix->smem_len = pitch * height;
  403. fix->type = FB_TYPE_PACKED_PIXELS;
  404. fix->type_aux = 0;
  405. par->cmap_type = cmap_unknown;
  406. if (depth == 8) {
  407. /* XXX kludge for ati */
  408. if (dp && !strncmp(name, "ATY,Rage128", 11)) {
  409. unsigned long regbase = dp->addrs[2].address;
  410. par->cmap_adr = ioremap(regbase, 0x1FFF);
  411. par->cmap_type = cmap_r128;
  412. } else if (dp && (!strncmp(name, "ATY,RageM3pA", 12)
  413. || !strncmp(name, "ATY,RageM3p12A", 14))) {
  414. unsigned long regbase =
  415. dp->parent->addrs[2].address;
  416. par->cmap_adr = ioremap(regbase, 0x1FFF);
  417. par->cmap_type = cmap_M3A;
  418. } else if (dp && !strncmp(name, "ATY,RageM3pB", 12)) {
  419. unsigned long regbase =
  420. dp->parent->addrs[2].address;
  421. par->cmap_adr = ioremap(regbase, 0x1FFF);
  422. par->cmap_type = cmap_M3B;
  423. } else if (dp && !strncmp(name, "ATY,Rage6", 9)) {
  424. unsigned long regbase = dp->addrs[1].address;
  425. par->cmap_adr = ioremap(regbase, 0x1FFF);
  426. par->cmap_type = cmap_radeon;
  427. } else if (!strncmp(name, "ATY,", 4)) {
  428. unsigned long base = address & 0xff000000UL;
  429. par->cmap_adr =
  430. ioremap(base + 0x7ff000, 0x1000) + 0xcc0;
  431. par->cmap_data = par->cmap_adr + 1;
  432. par->cmap_type = cmap_m64;
  433. } else if (device_is_compatible(dp, "pci1014,b7")) {
  434. unsigned long regbase = dp->addrs[0].address;
  435. par->cmap_adr = ioremap(regbase + 0x6000, 0x1000);
  436. par->cmap_type = cmap_gxt2000;
  437. }
  438. fix->visual = par->cmap_adr ? FB_VISUAL_PSEUDOCOLOR
  439. : FB_VISUAL_STATIC_PSEUDOCOLOR;
  440. } else
  441. fix->visual = /* par->cmap_adr ? FB_VISUAL_DIRECTCOLOR
  442. : */ FB_VISUAL_TRUECOLOR;
  443. var->xoffset = var->yoffset = 0;
  444. var->bits_per_pixel = depth;
  445. switch (depth) {
  446. case 8:
  447. var->bits_per_pixel = 8;
  448. var->red.offset = 0;
  449. var->red.length = 8;
  450. var->green.offset = 0;
  451. var->green.length = 8;
  452. var->blue.offset = 0;
  453. var->blue.length = 8;
  454. var->transp.offset = 0;
  455. var->transp.length = 0;
  456. break;
  457. case 16: /* RGB 555 */
  458. var->bits_per_pixel = 16;
  459. var->red.offset = 10;
  460. var->red.length = 5;
  461. var->green.offset = 5;
  462. var->green.length = 5;
  463. var->blue.offset = 0;
  464. var->blue.length = 5;
  465. var->transp.offset = 0;
  466. var->transp.length = 0;
  467. break;
  468. case 32: /* RGB 888 */
  469. var->bits_per_pixel = 32;
  470. var->red.offset = 16;
  471. var->red.length = 8;
  472. var->green.offset = 8;
  473. var->green.length = 8;
  474. var->blue.offset = 0;
  475. var->blue.length = 8;
  476. var->transp.offset = 24;
  477. var->transp.length = 8;
  478. break;
  479. }
  480. var->red.msb_right = var->green.msb_right = var->blue.msb_right =
  481. var->transp.msb_right = 0;
  482. var->grayscale = 0;
  483. var->nonstd = 0;
  484. var->activate = 0;
  485. var->height = var->width = -1;
  486. var->pixclock = 10000;
  487. var->left_margin = var->right_margin = 16;
  488. var->upper_margin = var->lower_margin = 16;
  489. var->hsync_len = var->vsync_len = 8;
  490. var->sync = 0;
  491. var->vmode = FB_VMODE_NONINTERLACED;
  492. info->fbops = &offb_ops;
  493. info->screen_base = ioremap(address, fix->smem_len);
  494. info->par = par;
  495. info->pseudo_palette = (void *) (info + 1);
  496. info->flags = FBINFO_DEFAULT;
  497. fb_alloc_cmap(&info->cmap, 256, 0);
  498. if (register_framebuffer(info) < 0) {
  499. kfree(info);
  500. release_mem_region(res_start, res_size);
  501. return;
  502. }
  503. printk(KERN_INFO "fb%d: Open Firmware frame buffer device on %s\n",
  504. info->node, full_name);
  505. }
  506. module_init(offb_init);
  507. MODULE_LICENSE("GPL");