sunxvr500.c 11 KB

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