sunxvr500.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* sunxvr500.c: Sun 3DLABS XVR-500 Expert3D driver for sparc64 systems
  2. *
  3. * Copyright (C) 2007 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/slab.h>
  8. #include <linux/fb.h>
  9. #include <linux/pci.h>
  10. #include <linux/init.h>
  11. #include <asm/io.h>
  12. #include <asm/prom.h>
  13. #include <asm/of_device.h>
  14. /* XXX This device has a 'dev-comm' property which aparently is
  15. * XXX a pointer into the openfirmware's address space which is
  16. * XXX a shared area the kernel driver can use to keep OBP
  17. * XXX informed about the current resolution setting. The idea
  18. * XXX is that the kernel can change resolutions, and as long
  19. * XXX as the values in the 'dev-comm' area are accurate then
  20. * XXX OBP can still render text properly to the console.
  21. * XXX
  22. * XXX I'm still working out the layout of this and whether there
  23. * XXX are any signatures we need to look for etc.
  24. */
  25. struct e3d_info {
  26. struct fb_info *info;
  27. struct pci_dev *pdev;
  28. spinlock_t lock;
  29. char __iomem *fb_base;
  30. unsigned long fb_base_phys;
  31. unsigned long fb8_buf_diff;
  32. unsigned long regs_base_phys;
  33. void __iomem *ramdac;
  34. struct device_node *of_node;
  35. unsigned int width;
  36. unsigned int height;
  37. unsigned int depth;
  38. unsigned int fb_size;
  39. u32 fb_base_reg;
  40. u32 fb8_0_off;
  41. u32 fb8_1_off;
  42. u32 pseudo_palette[16];
  43. };
  44. static int __devinit e3d_get_props(struct e3d_info *ep)
  45. {
  46. ep->width = of_getintprop_default(ep->of_node, "width", 0);
  47. ep->height = of_getintprop_default(ep->of_node, "height", 0);
  48. ep->depth = of_getintprop_default(ep->of_node, "depth", 8);
  49. if (!ep->width || !ep->height) {
  50. printk(KERN_ERR "e3d: Critical properties missing for %s\n",
  51. pci_name(ep->pdev));
  52. return -EINVAL;
  53. }
  54. return 0;
  55. }
  56. /* My XVR-500 comes up, at 1280x768 and a FB base register value of
  57. * 0x04000000, the following video layout register values:
  58. *
  59. * RAMDAC_VID_WH 0x03ff04ff
  60. * RAMDAC_VID_CFG 0x1a0b0088
  61. * RAMDAC_VID_32FB_0 0x04000000
  62. * RAMDAC_VID_32FB_1 0x04800000
  63. * RAMDAC_VID_8FB_0 0x05000000
  64. * RAMDAC_VID_8FB_1 0x05200000
  65. * RAMDAC_VID_XXXFB 0x05400000
  66. * RAMDAC_VID_YYYFB 0x05c00000
  67. * RAMDAC_VID_ZZZFB 0x05e00000
  68. */
  69. /* Video layout registers */
  70. #define RAMDAC_VID_WH 0x00000070UL /* (height-1)<<16 | (width-1) */
  71. #define RAMDAC_VID_CFG 0x00000074UL /* 0x1a000088|(linesz_log2<<16) */
  72. #define RAMDAC_VID_32FB_0 0x00000078UL /* PCI base 32bpp FB buffer 0 */
  73. #define RAMDAC_VID_32FB_1 0x0000007cUL /* PCI base 32bpp FB buffer 1 */
  74. #define RAMDAC_VID_8FB_0 0x00000080UL /* PCI base 8bpp FB buffer 0 */
  75. #define RAMDAC_VID_8FB_1 0x00000084UL /* PCI base 8bpp FB buffer 1 */
  76. #define RAMDAC_VID_XXXFB 0x00000088UL /* PCI base of XXX FB */
  77. #define RAMDAC_VID_YYYFB 0x0000008cUL /* PCI base of YYY FB */
  78. #define RAMDAC_VID_ZZZFB 0x00000090UL /* PCI base of ZZZ FB */
  79. /* CLUT registers */
  80. #define RAMDAC_INDEX 0x000000bcUL
  81. #define RAMDAC_DATA 0x000000c0UL
  82. static void e3d_clut_write(struct e3d_info *ep, int index, u32 val)
  83. {
  84. void __iomem *ramdac = ep->ramdac;
  85. unsigned long flags;
  86. spin_lock_irqsave(&ep->lock, flags);
  87. writel(index, ramdac + RAMDAC_INDEX);
  88. writel(val, ramdac + RAMDAC_DATA);
  89. spin_unlock_irqrestore(&ep->lock, flags);
  90. }
  91. static int e3d_setcolreg(unsigned regno,
  92. unsigned red, unsigned green, unsigned blue,
  93. unsigned transp, struct fb_info *info)
  94. {
  95. struct e3d_info *ep = info->par;
  96. u32 red_8, green_8, blue_8;
  97. u32 red_10, green_10, blue_10;
  98. u32 value;
  99. if (regno >= 256)
  100. return 1;
  101. red_8 = red >> 8;
  102. green_8 = green >> 8;
  103. blue_8 = blue >> 8;
  104. value = (blue_8 << 24) | (green_8 << 16) | (red_8 << 8);
  105. if (info->fix.visual == FB_VISUAL_TRUECOLOR && regno < 16)
  106. ((u32 *)info->pseudo_palette)[regno] = value;
  107. red_10 = red >> 6;
  108. green_10 = green >> 6;
  109. blue_10 = blue >> 6;
  110. value = (blue_10 << 20) | (green_10 << 10) | (red_10 << 0);
  111. e3d_clut_write(ep, regno, value);
  112. return 0;
  113. }
  114. /* XXX This is a bit of a hack. I can't figure out exactly how the
  115. * XXX two 8bpp areas of the framebuffer work. I imagine there is
  116. * XXX a WID attribute somewhere else in the framebuffer which tells
  117. * XXX the ramdac which of the two 8bpp framebuffer regions to take
  118. * XXX the pixel from. So, for now, render into both regions to make
  119. * XXX sure the pixel shows up.
  120. */
  121. static void e3d_imageblit(struct fb_info *info, const struct fb_image *image)
  122. {
  123. struct e3d_info *ep = info->par;
  124. unsigned long flags;
  125. spin_lock_irqsave(&ep->lock, flags);
  126. cfb_imageblit(info, image);
  127. info->screen_base += ep->fb8_buf_diff;
  128. cfb_imageblit(info, image);
  129. info->screen_base -= ep->fb8_buf_diff;
  130. spin_unlock_irqrestore(&ep->lock, flags);
  131. }
  132. static void e3d_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  133. {
  134. struct e3d_info *ep = info->par;
  135. unsigned long flags;
  136. spin_lock_irqsave(&ep->lock, flags);
  137. cfb_fillrect(info, rect);
  138. info->screen_base += ep->fb8_buf_diff;
  139. cfb_fillrect(info, rect);
  140. info->screen_base -= ep->fb8_buf_diff;
  141. spin_unlock_irqrestore(&ep->lock, flags);
  142. }
  143. static void e3d_copyarea(struct fb_info *info, const struct fb_copyarea *area)
  144. {
  145. struct e3d_info *ep = info->par;
  146. unsigned long flags;
  147. spin_lock_irqsave(&ep->lock, flags);
  148. cfb_copyarea(info, area);
  149. info->screen_base += ep->fb8_buf_diff;
  150. cfb_copyarea(info, area);
  151. info->screen_base -= ep->fb8_buf_diff;
  152. spin_unlock_irqrestore(&ep->lock, flags);
  153. }
  154. static struct fb_ops e3d_ops = {
  155. .owner = THIS_MODULE,
  156. .fb_setcolreg = e3d_setcolreg,
  157. .fb_fillrect = e3d_fillrect,
  158. .fb_copyarea = e3d_copyarea,
  159. .fb_imageblit = e3d_imageblit,
  160. };
  161. static int __devinit e3d_set_fbinfo(struct e3d_info *ep)
  162. {
  163. struct fb_info *info = ep->info;
  164. struct fb_var_screeninfo *var = &info->var;
  165. info->flags = FBINFO_DEFAULT;
  166. info->fbops = &e3d_ops;
  167. info->screen_base = ep->fb_base;
  168. info->screen_size = ep->fb_size;
  169. info->pseudo_palette = ep->pseudo_palette;
  170. /* Fill fix common fields */
  171. strlcpy(info->fix.id, "e3d", sizeof(info->fix.id));
  172. info->fix.smem_start = ep->fb_base_phys;
  173. info->fix.smem_len = ep->fb_size;
  174. info->fix.type = FB_TYPE_PACKED_PIXELS;
  175. if (ep->depth == 32 || ep->depth == 24)
  176. info->fix.visual = FB_VISUAL_TRUECOLOR;
  177. else
  178. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  179. var->xres = ep->width;
  180. var->yres = ep->height;
  181. var->xres_virtual = var->xres;
  182. var->yres_virtual = var->yres;
  183. var->bits_per_pixel = ep->depth;
  184. var->red.offset = 8;
  185. var->red.length = 8;
  186. var->green.offset = 16;
  187. var->green.length = 8;
  188. var->blue.offset = 24;
  189. var->blue.length = 8;
  190. var->transp.offset = 0;
  191. var->transp.length = 0;
  192. if (fb_alloc_cmap(&info->cmap, 256, 0)) {
  193. printk(KERN_ERR "e3d: Cannot allocate color map.\n");
  194. return -ENOMEM;
  195. }
  196. return 0;
  197. }
  198. static int __devinit e3d_pci_register(struct pci_dev *pdev,
  199. const struct pci_device_id *ent)
  200. {
  201. struct fb_info *info;
  202. struct e3d_info *ep;
  203. unsigned int line_length;
  204. int err;
  205. err = pci_enable_device(pdev);
  206. if (err < 0) {
  207. printk(KERN_ERR "e3d: Cannot enable PCI device %s\n",
  208. pci_name(pdev));
  209. goto err_out;
  210. }
  211. info = framebuffer_alloc(sizeof(struct e3d_info), &pdev->dev);
  212. if (!info) {
  213. printk(KERN_ERR "e3d: Cannot allocate fb_info\n");
  214. err = -ENOMEM;
  215. goto err_disable;
  216. }
  217. ep = info->par;
  218. ep->info = info;
  219. ep->pdev = pdev;
  220. spin_lock_init(&ep->lock);
  221. ep->of_node = pci_device_to_OF_node(pdev);
  222. if (!ep->of_node) {
  223. printk(KERN_ERR "e3d: Cannot find OF node of %s\n",
  224. pci_name(pdev));
  225. err = -ENODEV;
  226. goto err_release_fb;
  227. }
  228. /* Read the PCI base register of the frame buffer, which we
  229. * need in order to interpret the RAMDAC_VID_*FB* values in
  230. * the ramdac correctly.
  231. */
  232. pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0,
  233. &ep->fb_base_reg);
  234. ep->fb_base_reg &= PCI_BASE_ADDRESS_MEM_MASK;
  235. ep->regs_base_phys = pci_resource_start (pdev, 1);
  236. err = pci_request_region(pdev, 1, "e3d regs");
  237. if (err < 0) {
  238. printk("e3d: Cannot request region 1 for %s\n",
  239. pci_name(pdev));
  240. goto err_release_fb;
  241. }
  242. ep->ramdac = ioremap(ep->regs_base_phys + 0x8000, 0x1000);
  243. if (!ep->ramdac)
  244. goto err_release_pci1;
  245. ep->fb8_0_off = readl(ep->ramdac + RAMDAC_VID_8FB_0);
  246. ep->fb8_0_off -= ep->fb_base_reg;
  247. ep->fb8_1_off = readl(ep->ramdac + RAMDAC_VID_8FB_1);
  248. ep->fb8_1_off -= ep->fb_base_reg;
  249. ep->fb8_buf_diff = ep->fb8_1_off - ep->fb8_0_off;
  250. ep->fb_base_phys = pci_resource_start (pdev, 0);
  251. ep->fb_base_phys += ep->fb8_0_off;
  252. err = pci_request_region(pdev, 0, "e3d framebuffer");
  253. if (err < 0) {
  254. printk("e3d: Cannot request region 0 for %s\n",
  255. pci_name(pdev));
  256. goto err_unmap_ramdac;
  257. }
  258. err = e3d_get_props(ep);
  259. if (err)
  260. goto err_release_pci0;
  261. line_length = (readl(ep->ramdac + RAMDAC_VID_CFG) >> 16) & 0xff;
  262. line_length = 1 << line_length;
  263. switch (ep->depth) {
  264. case 8:
  265. info->fix.line_length = line_length;
  266. break;
  267. case 16:
  268. info->fix.line_length = line_length * 2;
  269. break;
  270. case 24:
  271. info->fix.line_length = line_length * 3;
  272. break;
  273. case 32:
  274. info->fix.line_length = line_length * 4;
  275. break;
  276. }
  277. ep->fb_size = info->fix.line_length * ep->height;
  278. ep->fb_base = ioremap(ep->fb_base_phys, ep->fb_size);
  279. if (!ep->fb_base)
  280. goto err_release_pci0;
  281. err = e3d_set_fbinfo(ep);
  282. if (err)
  283. goto err_unmap_fb;
  284. pci_set_drvdata(pdev, info);
  285. printk("e3d: Found device at %s\n", pci_name(pdev));
  286. err = register_framebuffer(info);
  287. if (err < 0) {
  288. printk(KERN_ERR "e3d: Could not register framebuffer %s\n",
  289. pci_name(pdev));
  290. goto err_unmap_fb;
  291. }
  292. return 0;
  293. err_unmap_fb:
  294. iounmap(ep->fb_base);
  295. err_release_pci0:
  296. pci_release_region(pdev, 0);
  297. err_unmap_ramdac:
  298. iounmap(ep->ramdac);
  299. err_release_pci1:
  300. pci_release_region(pdev, 1);
  301. err_release_fb:
  302. framebuffer_release(info);
  303. err_disable:
  304. pci_disable_device(pdev);
  305. err_out:
  306. return err;
  307. }
  308. static void __devexit e3d_pci_unregister(struct pci_dev *pdev)
  309. {
  310. struct fb_info *info = pci_get_drvdata(pdev);
  311. struct e3d_info *ep = info->par;
  312. unregister_framebuffer(info);
  313. iounmap(ep->ramdac);
  314. iounmap(ep->fb_base);
  315. pci_release_region(pdev, 0);
  316. pci_release_region(pdev, 1);
  317. framebuffer_release(info);
  318. pci_disable_device(pdev);
  319. }
  320. static struct pci_device_id e3d_pci_table[] = {
  321. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x7a0), },
  322. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x7a2), },
  323. { .vendor = PCI_VENDOR_ID_3DLABS,
  324. .device = PCI_ANY_ID,
  325. .subvendor = PCI_VENDOR_ID_3DLABS,
  326. .subdevice = 0x0108,
  327. },
  328. { .vendor = PCI_VENDOR_ID_3DLABS,
  329. .device = PCI_ANY_ID,
  330. .subvendor = PCI_VENDOR_ID_3DLABS,
  331. .subdevice = 0x0140,
  332. },
  333. { .vendor = PCI_VENDOR_ID_3DLABS,
  334. .device = PCI_ANY_ID,
  335. .subvendor = PCI_VENDOR_ID_3DLABS,
  336. .subdevice = 0x1024,
  337. },
  338. { 0, }
  339. };
  340. static struct pci_driver e3d_driver = {
  341. .name = "e3d",
  342. .id_table = e3d_pci_table,
  343. .probe = e3d_pci_register,
  344. .remove = __devexit_p(e3d_pci_unregister),
  345. };
  346. static int __init e3d_init(void)
  347. {
  348. if (fb_get_options("e3d", NULL))
  349. return -ENODEV;
  350. return pci_register_driver(&e3d_driver);
  351. }
  352. static void __exit e3d_exit(void)
  353. {
  354. pci_unregister_driver(&e3d_driver);
  355. }
  356. module_init(e3d_init);
  357. module_exit(e3d_exit);
  358. MODULE_DESCRIPTION("framebuffer driver for Sun XVR-500 graphics");
  359. MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
  360. MODULE_VERSION("1.0");
  361. MODULE_LICENSE("GPL");