sunxvr500.c 11 KB

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