offb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. 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. int i, depth;
  86. u32 *pal = info->pseudo_palette;
  87. depth = info->var.bits_per_pixel;
  88. if (depth == 16)
  89. depth = (info->var.green.length == 5) ? 15 : 16;
  90. if (regno > 255 ||
  91. (depth == 16 && regno > 63) ||
  92. (depth == 15 && regno > 31))
  93. return 1;
  94. if (regno < 16) {
  95. switch (depth) {
  96. case 15:
  97. pal[regno] = (regno << 10) | (regno << 5) | regno;
  98. break;
  99. case 16:
  100. pal[regno] = (regno << 11) | (regno << 5) | regno;
  101. break;
  102. case 24:
  103. pal[regno] = (regno << 16) | (regno << 8) | regno;
  104. break;
  105. case 32:
  106. i = (regno << 8) | regno;
  107. pal[regno] = (i << 16) | i;
  108. break;
  109. }
  110. }
  111. red >>= 8;
  112. green >>= 8;
  113. blue >>= 8;
  114. if (!par->cmap_adr)
  115. return 0;
  116. switch (par->cmap_type) {
  117. case cmap_m64:
  118. writeb(regno, par->cmap_adr);
  119. writeb(red, par->cmap_data);
  120. writeb(green, par->cmap_data);
  121. writeb(blue, par->cmap_data);
  122. break;
  123. case cmap_M3A:
  124. /* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
  125. out_le32(par->cmap_adr + 0x58,
  126. in_le32(par->cmap_adr + 0x58) & ~0x20);
  127. case cmap_r128:
  128. /* Set palette index & data */
  129. out_8(par->cmap_adr + 0xb0, regno);
  130. out_le32(par->cmap_adr + 0xb4,
  131. (red << 16 | green << 8 | blue));
  132. break;
  133. case cmap_M3B:
  134. /* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
  135. out_le32(par->cmap_adr + 0x58,
  136. in_le32(par->cmap_adr + 0x58) | 0x20);
  137. /* Set palette index & data */
  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_radeon:
  142. /* Set palette index & data (could be smarter) */
  143. out_8(par->cmap_adr + 0xb0, regno);
  144. out_le32(par->cmap_adr + 0xb4, (red << 16 | green << 8 | blue));
  145. break;
  146. case cmap_gxt2000:
  147. out_le32((unsigned __iomem *) par->cmap_adr + regno,
  148. (red << 16 | green << 8 | blue));
  149. break;
  150. }
  151. return 0;
  152. }
  153. /*
  154. * Blank the display.
  155. */
  156. static int offb_blank(int blank, struct fb_info *info)
  157. {
  158. struct offb_par *par = (struct offb_par *) info->par;
  159. int i, j;
  160. if (!par->cmap_adr)
  161. return 0;
  162. if (!par->blanked)
  163. if (!blank)
  164. return 0;
  165. par->blanked = blank;
  166. if (blank)
  167. for (i = 0; i < 256; i++) {
  168. switch (par->cmap_type) {
  169. case cmap_m64:
  170. writeb(i, par->cmap_adr);
  171. for (j = 0; j < 3; j++)
  172. writeb(0, par->cmap_data);
  173. break;
  174. case cmap_M3A:
  175. /* Clear PALETTE_ACCESS_CNTL in DAC_CNTL */
  176. out_le32(par->cmap_adr + 0x58,
  177. in_le32(par->cmap_adr + 0x58) & ~0x20);
  178. case cmap_r128:
  179. /* Set palette index & data */
  180. out_8(par->cmap_adr + 0xb0, i);
  181. out_le32(par->cmap_adr + 0xb4, 0);
  182. break;
  183. case cmap_M3B:
  184. /* Set PALETTE_ACCESS_CNTL in DAC_CNTL */
  185. out_le32(par->cmap_adr + 0x58,
  186. in_le32(par->cmap_adr + 0x58) | 0x20);
  187. /* Set palette index & data */
  188. out_8(par->cmap_adr + 0xb0, i);
  189. out_le32(par->cmap_adr + 0xb4, 0);
  190. break;
  191. case cmap_radeon:
  192. out_8(par->cmap_adr + 0xb0, i);
  193. out_le32(par->cmap_adr + 0xb4, 0);
  194. break;
  195. case cmap_gxt2000:
  196. out_le32((unsigned __iomem *) par->cmap_adr + i,
  197. 0);
  198. break;
  199. }
  200. } else
  201. fb_set_cmap(&info->cmap, info);
  202. return 0;
  203. }
  204. /*
  205. * Initialisation
  206. */
  207. int __init offb_init(void)
  208. {
  209. struct device_node *dp = NULL, *boot_disp = NULL;
  210. if (fb_get_options("offb", NULL))
  211. return -ENODEV;
  212. for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
  213. if (get_property(dp, "linux,opened", NULL) &&
  214. get_property(dp, "linux,boot-display", NULL)) {
  215. boot_disp = dp;
  216. offb_init_nodriver(dp);
  217. }
  218. }
  219. for (dp = NULL; (dp = of_find_node_by_type(dp, "display"));) {
  220. if (get_property(dp, "linux,opened", NULL) &&
  221. dp != boot_disp)
  222. offb_init_nodriver(dp);
  223. }
  224. return 0;
  225. }
  226. static void __init offb_init_nodriver(struct device_node *dp)
  227. {
  228. unsigned int len;
  229. int i, width = 640, height = 480, depth = 8, pitch = 640;
  230. unsigned int flags, rsize, addr_prop = 0;
  231. unsigned long max_size = 0;
  232. u64 rstart, address = OF_BAD_ADDR;
  233. u32 *pp, *addrp, *up;
  234. u64 asize;
  235. pp = (u32 *)get_property(dp, "linux,bootx-depth", &len);
  236. if (pp == NULL)
  237. pp = (u32 *)get_property(dp, "depth", &len);
  238. if (pp && len == sizeof(u32))
  239. depth = *pp;
  240. pp = (u32 *)get_property(dp, "linux,bootx-width", &len);
  241. if (pp == NULL)
  242. pp = (u32 *)get_property(dp, "width", &len);
  243. if (pp && len == sizeof(u32))
  244. width = *pp;
  245. pp = (u32 *)get_property(dp, "linux,bootx-height", &len);
  246. if (pp == NULL)
  247. pp = (u32 *)get_property(dp, "height", &len);
  248. if (pp && len == sizeof(u32))
  249. height = *pp;
  250. pp = (u32 *)get_property(dp, "linux,bootx-linebytes", &len);
  251. if (pp == NULL)
  252. pp = (u32 *)get_property(dp, "linebytes", &len);
  253. if (pp && len == sizeof(u32))
  254. pitch = *pp;
  255. else
  256. pitch = width * ((depth + 7) / 8);
  257. rsize = (unsigned long)pitch * (unsigned long)height;
  258. /* Ok, now we try to figure out the address of the framebuffer.
  259. *
  260. * Unfortunately, Open Firmware doesn't provide a standard way to do
  261. * so. All we can do is a dodgy heuristic that happens to work in
  262. * practice. On most machines, the "address" property contains what
  263. * we need, though not on Matrox cards found in IBM machines. What I've
  264. * found that appears to give good results is to go through the PCI
  265. * ranges and pick one that is both big enough and if possible encloses
  266. * the "address" property. If none match, we pick the biggest
  267. */
  268. up = (u32 *)get_property(dp, "linux,bootx-addr", &len);
  269. if (up == NULL)
  270. up = (u32 *)get_property(dp, "address", &len);
  271. if (up && len == sizeof(u32))
  272. addr_prop = *up;
  273. for (i = 0; (addrp = of_get_address(dp, i, &asize, &flags))
  274. != NULL; i++) {
  275. int match_addrp = 0;
  276. if (!(flags & IORESOURCE_MEM))
  277. continue;
  278. if (asize < rsize)
  279. continue;
  280. rstart = of_translate_address(dp, addrp);
  281. if (rstart == OF_BAD_ADDR)
  282. continue;
  283. if (addr_prop && (rstart <= addr_prop) &&
  284. ((rstart + asize) >= (addr_prop + rsize)))
  285. match_addrp = 1;
  286. if (match_addrp) {
  287. address = addr_prop;
  288. break;
  289. }
  290. if (rsize > max_size) {
  291. max_size = rsize;
  292. address = OF_BAD_ADDR;
  293. }
  294. if (address == OF_BAD_ADDR)
  295. address = rstart;
  296. }
  297. if (address == OF_BAD_ADDR && addr_prop)
  298. address = (u64)addr_prop;
  299. if (address != OF_BAD_ADDR) {
  300. /* kludge for valkyrie */
  301. if (strcmp(dp->name, "valkyrie") == 0)
  302. address += 0x1000;
  303. offb_init_fb(dp->name, dp->full_name, width, height, depth,
  304. pitch, address, dp);
  305. }
  306. }
  307. static void __init offb_init_fb(const char *name, const char *full_name,
  308. int width, int height, int depth,
  309. int pitch, unsigned long address,
  310. struct device_node *dp)
  311. {
  312. unsigned long res_size = pitch * height * (depth + 7) / 8;
  313. struct offb_par *par = &default_par;
  314. unsigned long res_start = address;
  315. struct fb_fix_screeninfo *fix;
  316. struct fb_var_screeninfo *var;
  317. struct fb_info *info;
  318. int size;
  319. if (!request_mem_region(res_start, res_size, "offb"))
  320. return;
  321. printk(KERN_INFO
  322. "Using unsupported %dx%d %s at %lx, depth=%d, pitch=%d\n",
  323. width, height, name, address, depth, pitch);
  324. if (depth != 8 && depth != 15 && depth != 16 && depth != 32) {
  325. printk(KERN_ERR "%s: can't use depth = %d\n", full_name,
  326. depth);
  327. release_mem_region(res_start, res_size);
  328. return;
  329. }
  330. size = sizeof(struct fb_info) + sizeof(u32) * 17;
  331. info = kmalloc(size, GFP_ATOMIC);
  332. if (info == 0) {
  333. release_mem_region(res_start, res_size);
  334. return;
  335. }
  336. memset(info, 0, size);
  337. fix = &info->fix;
  338. var = &info->var;
  339. strcpy(fix->id, "OFfb ");
  340. strncat(fix->id, name, sizeof(fix->id) - sizeof("OFfb "));
  341. fix->id[sizeof(fix->id) - 1] = '\0';
  342. var->xres = var->xres_virtual = width;
  343. var->yres = var->yres_virtual = height;
  344. fix->line_length = pitch;
  345. fix->smem_start = address;
  346. fix->smem_len = pitch * height;
  347. fix->type = FB_TYPE_PACKED_PIXELS;
  348. fix->type_aux = 0;
  349. par->cmap_type = cmap_unknown;
  350. if (depth == 8) {
  351. /* Palette hacks disabled for now */
  352. #if 0
  353. if (dp && !strncmp(name, "ATY,Rage128", 11)) {
  354. unsigned long regbase = dp->addrs[2].address;
  355. par->cmap_adr = ioremap(regbase, 0x1FFF);
  356. par->cmap_type = cmap_r128;
  357. } else if (dp && (!strncmp(name, "ATY,RageM3pA", 12)
  358. || !strncmp(name, "ATY,RageM3p12A", 14))) {
  359. unsigned long regbase =
  360. dp->parent->addrs[2].address;
  361. par->cmap_adr = ioremap(regbase, 0x1FFF);
  362. par->cmap_type = cmap_M3A;
  363. } else if (dp && !strncmp(name, "ATY,RageM3pB", 12)) {
  364. unsigned long regbase =
  365. dp->parent->addrs[2].address;
  366. par->cmap_adr = ioremap(regbase, 0x1FFF);
  367. par->cmap_type = cmap_M3B;
  368. } else if (dp && !strncmp(name, "ATY,Rage6", 9)) {
  369. unsigned long regbase = dp->addrs[1].address;
  370. par->cmap_adr = ioremap(regbase, 0x1FFF);
  371. par->cmap_type = cmap_radeon;
  372. } else if (!strncmp(name, "ATY,", 4)) {
  373. unsigned long base = address & 0xff000000UL;
  374. par->cmap_adr =
  375. ioremap(base + 0x7ff000, 0x1000) + 0xcc0;
  376. par->cmap_data = par->cmap_adr + 1;
  377. par->cmap_type = cmap_m64;
  378. } else if (device_is_compatible(dp, "pci1014,b7")) {
  379. unsigned long regbase = dp->addrs[0].address;
  380. par->cmap_adr = ioremap(regbase + 0x6000, 0x1000);
  381. par->cmap_type = cmap_gxt2000;
  382. }
  383. #endif
  384. fix->visual = par->cmap_adr ? FB_VISUAL_PSEUDOCOLOR
  385. : FB_VISUAL_STATIC_PSEUDOCOLOR;
  386. } else
  387. fix->visual = /* par->cmap_adr ? FB_VISUAL_DIRECTCOLOR
  388. : */ FB_VISUAL_TRUECOLOR;
  389. var->xoffset = var->yoffset = 0;
  390. switch (depth) {
  391. case 8:
  392. var->bits_per_pixel = 8;
  393. var->red.offset = 0;
  394. var->red.length = 8;
  395. var->green.offset = 0;
  396. var->green.length = 8;
  397. var->blue.offset = 0;
  398. var->blue.length = 8;
  399. var->transp.offset = 0;
  400. var->transp.length = 0;
  401. break;
  402. case 15: /* RGB 555 */
  403. var->bits_per_pixel = 16;
  404. var->red.offset = 10;
  405. var->red.length = 5;
  406. var->green.offset = 5;
  407. var->green.length = 5;
  408. var->blue.offset = 0;
  409. var->blue.length = 5;
  410. var->transp.offset = 0;
  411. var->transp.length = 0;
  412. break;
  413. case 16: /* RGB 565 */
  414. var->bits_per_pixel = 16;
  415. var->red.offset = 11;
  416. var->red.length = 5;
  417. var->green.offset = 5;
  418. var->green.length = 6;
  419. var->blue.offset = 0;
  420. var->blue.length = 5;
  421. var->transp.offset = 0;
  422. var->transp.length = 0;
  423. break;
  424. case 32: /* RGB 888 */
  425. var->bits_per_pixel = 32;
  426. var->red.offset = 16;
  427. var->red.length = 8;
  428. var->green.offset = 8;
  429. var->green.length = 8;
  430. var->blue.offset = 0;
  431. var->blue.length = 8;
  432. var->transp.offset = 24;
  433. var->transp.length = 8;
  434. break;
  435. }
  436. var->red.msb_right = var->green.msb_right = var->blue.msb_right =
  437. var->transp.msb_right = 0;
  438. var->grayscale = 0;
  439. var->nonstd = 0;
  440. var->activate = 0;
  441. var->height = var->width = -1;
  442. var->pixclock = 10000;
  443. var->left_margin = var->right_margin = 16;
  444. var->upper_margin = var->lower_margin = 16;
  445. var->hsync_len = var->vsync_len = 8;
  446. var->sync = 0;
  447. var->vmode = FB_VMODE_NONINTERLACED;
  448. info->fbops = &offb_ops;
  449. info->screen_base = ioremap(address, fix->smem_len);
  450. info->par = par;
  451. info->pseudo_palette = (void *) (info + 1);
  452. info->flags = FBINFO_DEFAULT;
  453. fb_alloc_cmap(&info->cmap, 256, 0);
  454. if (register_framebuffer(info) < 0) {
  455. kfree(info);
  456. release_mem_region(res_start, res_size);
  457. return;
  458. }
  459. printk(KERN_INFO "fb%d: Open Firmware frame buffer device on %s\n",
  460. info->node, full_name);
  461. }
  462. module_init(offb_init);
  463. MODULE_LICENSE("GPL");