offb.c 14 KB

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