offb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 <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. int offb_init(void);
  58. static int offb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
  59. u_int transp, struct fb_info *info);
  60. static int offb_blank(int blank, struct fb_info *info);
  61. #ifdef CONFIG_PPC32
  62. extern boot_infos_t *boot_infos;
  63. #endif
  64. static void offb_init_nodriver(struct device_node *);
  65. static void offb_init_fb(const char *name, const char *full_name,
  66. int width, int height, int depth, int pitch,
  67. unsigned long address, struct device_node *dp);
  68. static struct fb_ops offb_ops = {
  69. .owner = THIS_MODULE,
  70. .fb_setcolreg = offb_setcolreg,
  71. .fb_blank = offb_blank,
  72. .fb_fillrect = cfb_fillrect,
  73. .fb_copyarea = cfb_copyarea,
  74. .fb_imageblit = cfb_imageblit,
  75. .fb_cursor = soft_cursor,
  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 *up;
  295. unsigned long address;
  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. if ((up = (unsigned *) get_property(dp, "address", &len)) != NULL
  313. && len == sizeof(unsigned))
  314. address = (u_long) * up;
  315. else {
  316. for (i = 0; i < dp->n_addrs; ++i)
  317. if (dp->addrs[i].size >=
  318. pitch * height * depth / 8)
  319. break;
  320. if (i >= dp->n_addrs) {
  321. printk(KERN_ERR
  322. "no framebuffer address found for %s\n",
  323. dp->full_name);
  324. return;
  325. }
  326. address = (u_long) dp->addrs[i].address;
  327. #ifdef CONFIG_PPC64
  328. address += ((struct pci_dn *)dp->data)->phb->pci_mem_offset;
  329. #endif
  330. /* kludge for valkyrie */
  331. if (strcmp(dp->name, "valkyrie") == 0)
  332. address += 0x1000;
  333. }
  334. offb_init_fb(dp->name, dp->full_name, width, height, depth,
  335. pitch, address, dp);
  336. }
  337. static void __init offb_init_fb(const char *name, const char *full_name,
  338. int width, int height, int depth,
  339. int pitch, unsigned long address,
  340. struct device_node *dp)
  341. {
  342. unsigned long res_size = pitch * height * depth / 8;
  343. struct offb_par *par = &default_par;
  344. unsigned long res_start = address;
  345. struct fb_fix_screeninfo *fix;
  346. struct fb_var_screeninfo *var;
  347. struct fb_info *info;
  348. int size;
  349. if (!request_mem_region(res_start, res_size, "offb"))
  350. return;
  351. printk(KERN_INFO
  352. "Using unsupported %dx%d %s at %lx, depth=%d, pitch=%d\n",
  353. width, height, name, address, depth, pitch);
  354. if (depth != 8 && depth != 16 && depth != 32) {
  355. printk(KERN_ERR "%s: can't use depth = %d\n", full_name,
  356. depth);
  357. release_mem_region(res_start, res_size);
  358. return;
  359. }
  360. size = sizeof(struct fb_info) + sizeof(u32) * 17;
  361. info = kmalloc(size, GFP_ATOMIC);
  362. if (info == 0) {
  363. release_mem_region(res_start, res_size);
  364. return;
  365. }
  366. memset(info, 0, size);
  367. fix = &info->fix;
  368. var = &info->var;
  369. strcpy(fix->id, "OFfb ");
  370. strncat(fix->id, name, sizeof(fix->id) - sizeof("OFfb "));
  371. fix->id[sizeof(fix->id) - 1] = '\0';
  372. var->xres = var->xres_virtual = width;
  373. var->yres = var->yres_virtual = height;
  374. fix->line_length = pitch;
  375. fix->smem_start = address;
  376. fix->smem_len = pitch * height;
  377. fix->type = FB_TYPE_PACKED_PIXELS;
  378. fix->type_aux = 0;
  379. par->cmap_type = cmap_unknown;
  380. if (depth == 8) {
  381. /* XXX kludge for ati */
  382. if (dp && !strncmp(name, "ATY,Rage128", 11)) {
  383. unsigned long regbase = dp->addrs[2].address;
  384. par->cmap_adr = ioremap(regbase, 0x1FFF);
  385. par->cmap_type = cmap_r128;
  386. } else if (dp && (!strncmp(name, "ATY,RageM3pA", 12)
  387. || !strncmp(name, "ATY,RageM3p12A", 14))) {
  388. unsigned long regbase =
  389. dp->parent->addrs[2].address;
  390. par->cmap_adr = ioremap(regbase, 0x1FFF);
  391. par->cmap_type = cmap_M3A;
  392. } else if (dp && !strncmp(name, "ATY,RageM3pB", 12)) {
  393. unsigned long regbase =
  394. dp->parent->addrs[2].address;
  395. par->cmap_adr = ioremap(regbase, 0x1FFF);
  396. par->cmap_type = cmap_M3B;
  397. } else if (dp && !strncmp(name, "ATY,Rage6", 9)) {
  398. unsigned long regbase = dp->addrs[1].address;
  399. par->cmap_adr = ioremap(regbase, 0x1FFF);
  400. par->cmap_type = cmap_radeon;
  401. } else if (!strncmp(name, "ATY,", 4)) {
  402. unsigned long base = address & 0xff000000UL;
  403. par->cmap_adr =
  404. ioremap(base + 0x7ff000, 0x1000) + 0xcc0;
  405. par->cmap_data = par->cmap_adr + 1;
  406. par->cmap_type = cmap_m64;
  407. } else if (device_is_compatible(dp, "pci1014,b7")) {
  408. unsigned long regbase = dp->addrs[0].address;
  409. par->cmap_adr = ioremap(regbase + 0x6000, 0x1000);
  410. par->cmap_type = cmap_gxt2000;
  411. }
  412. fix->visual = par->cmap_adr ? FB_VISUAL_PSEUDOCOLOR
  413. : FB_VISUAL_STATIC_PSEUDOCOLOR;
  414. } else
  415. fix->visual = /* par->cmap_adr ? FB_VISUAL_DIRECTCOLOR
  416. : */ FB_VISUAL_TRUECOLOR;
  417. var->xoffset = var->yoffset = 0;
  418. var->bits_per_pixel = depth;
  419. switch (depth) {
  420. case 8:
  421. var->bits_per_pixel = 8;
  422. var->red.offset = 0;
  423. var->red.length = 8;
  424. var->green.offset = 0;
  425. var->green.length = 8;
  426. var->blue.offset = 0;
  427. var->blue.length = 8;
  428. var->transp.offset = 0;
  429. var->transp.length = 0;
  430. break;
  431. case 16: /* RGB 555 */
  432. var->bits_per_pixel = 16;
  433. var->red.offset = 10;
  434. var->red.length = 5;
  435. var->green.offset = 5;
  436. var->green.length = 5;
  437. var->blue.offset = 0;
  438. var->blue.length = 5;
  439. var->transp.offset = 0;
  440. var->transp.length = 0;
  441. break;
  442. case 32: /* RGB 888 */
  443. var->bits_per_pixel = 32;
  444. var->red.offset = 16;
  445. var->red.length = 8;
  446. var->green.offset = 8;
  447. var->green.length = 8;
  448. var->blue.offset = 0;
  449. var->blue.length = 8;
  450. var->transp.offset = 24;
  451. var->transp.length = 8;
  452. break;
  453. }
  454. var->red.msb_right = var->green.msb_right = var->blue.msb_right =
  455. var->transp.msb_right = 0;
  456. var->grayscale = 0;
  457. var->nonstd = 0;
  458. var->activate = 0;
  459. var->height = var->width = -1;
  460. var->pixclock = 10000;
  461. var->left_margin = var->right_margin = 16;
  462. var->upper_margin = var->lower_margin = 16;
  463. var->hsync_len = var->vsync_len = 8;
  464. var->sync = 0;
  465. var->vmode = FB_VMODE_NONINTERLACED;
  466. info->fbops = &offb_ops;
  467. info->screen_base = ioremap(address, fix->smem_len);
  468. info->par = par;
  469. info->pseudo_palette = (void *) (info + 1);
  470. info->flags = FBINFO_DEFAULT;
  471. fb_alloc_cmap(&info->cmap, 256, 0);
  472. if (register_framebuffer(info) < 0) {
  473. kfree(info);
  474. release_mem_region(res_start, res_size);
  475. return;
  476. }
  477. printk(KERN_INFO "fb%d: Open Firmware frame buffer device on %s\n",
  478. info->node, full_name);
  479. }
  480. module_init(offb_init);
  481. MODULE_LICENSE("GPL");