sunxvr500.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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/aperture.h>
  8. #include <linux/kernel.h>
  9. #include <linux/fb.h>
  10. #include <linux/pci.h>
  11. #include <linux/init.h>
  12. #include <linux/of.h>
  13. #include <asm/io.h>
  14. /* XXX This device has a 'dev-comm' property which apparently 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 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 const struct fb_ops e3d_ops = {
  155. .owner = THIS_MODULE,
  156. __FB_DEFAULT_IOMEM_OPS_RDWR,
  157. .fb_setcolreg = e3d_setcolreg,
  158. .fb_fillrect = e3d_fillrect,
  159. .fb_copyarea = e3d_copyarea,
  160. .fb_imageblit = e3d_imageblit,
  161. __FB_DEFAULT_IOMEM_OPS_MMAP,
  162. };
  163. static int e3d_set_fbinfo(struct e3d_info *ep)
  164. {
  165. struct fb_info *info = ep->info;
  166. struct fb_var_screeninfo *var = &info->var;
  167. info->fbops = &e3d_ops;
  168. info->screen_base = ep->fb_base;
  169. info->screen_size = ep->fb_size;
  170. info->pseudo_palette = ep->pseudo_palette;
  171. /* Fill fix common fields */
  172. strscpy(info->fix.id, "e3d", sizeof(info->fix.id));
  173. info->fix.smem_start = ep->fb_base_phys;
  174. info->fix.smem_len = ep->fb_size;
  175. info->fix.type = FB_TYPE_PACKED_PIXELS;
  176. if (ep->depth == 32 || ep->depth == 24)
  177. info->fix.visual = FB_VISUAL_TRUECOLOR;
  178. else
  179. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  180. var->xres = ep->width;
  181. var->yres = ep->height;
  182. var->xres_virtual = var->xres;
  183. var->yres_virtual = var->yres;
  184. var->bits_per_pixel = ep->depth;
  185. var->red.offset = 8;
  186. var->red.length = 8;
  187. var->green.offset = 16;
  188. var->green.length = 8;
  189. var->blue.offset = 24;
  190. var->blue.length = 8;
  191. var->transp.offset = 0;
  192. var->transp.length = 0;
  193. if (fb_alloc_cmap(&info->cmap, 256, 0)) {
  194. printk(KERN_ERR "e3d: Cannot allocate color map.\n");
  195. return -ENOMEM;
  196. }
  197. return 0;
  198. }
  199. static int e3d_pci_register(struct pci_dev *pdev,
  200. const struct pci_device_id *ent)
  201. {
  202. struct device_node *of_node;
  203. const char *device_type;
  204. struct fb_info *info;
  205. struct e3d_info *ep;
  206. unsigned int line_length;
  207. int err;
  208. err = aperture_remove_conflicting_pci_devices(pdev, "e3dfb");
  209. if (err)
  210. return err;
  211. of_node = pci_device_to_OF_node(pdev);
  212. if (!of_node) {
  213. printk(KERN_ERR "e3d: Cannot find OF node of %s\n",
  214. pci_name(pdev));
  215. return -ENODEV;
  216. }
  217. device_type = of_get_property(of_node, "device_type", NULL);
  218. if (!device_type) {
  219. printk(KERN_INFO "e3d: Ignoring secondary output device "
  220. "at %s\n", pci_name(pdev));
  221. return -ENODEV;
  222. }
  223. err = pci_enable_device(pdev);
  224. if (err < 0) {
  225. printk(KERN_ERR "e3d: Cannot enable PCI device %s\n",
  226. pci_name(pdev));
  227. goto err_out;
  228. }
  229. info = framebuffer_alloc(sizeof(struct e3d_info), &pdev->dev);
  230. if (!info) {
  231. err = -ENOMEM;
  232. goto err_disable;
  233. }
  234. ep = info->par;
  235. ep->info = info;
  236. ep->pdev = pdev;
  237. spin_lock_init(&ep->lock);
  238. ep->of_node = of_node;
  239. /* Read the PCI base register of the frame buffer, which we
  240. * need in order to interpret the RAMDAC_VID_*FB* values in
  241. * the ramdac correctly.
  242. */
  243. pci_read_config_dword(pdev, PCI_BASE_ADDRESS_0,
  244. &ep->fb_base_reg);
  245. ep->fb_base_reg &= PCI_BASE_ADDRESS_MEM_MASK;
  246. ep->regs_base_phys = pci_resource_start (pdev, 1);
  247. err = pci_request_region(pdev, 1, "e3d regs");
  248. if (err < 0) {
  249. printk("e3d: Cannot request region 1 for %s\n",
  250. pci_name(pdev));
  251. goto err_release_fb;
  252. }
  253. ep->ramdac = ioremap(ep->regs_base_phys + 0x8000, 0x1000);
  254. if (!ep->ramdac) {
  255. err = -ENOMEM;
  256. goto err_release_pci1;
  257. }
  258. ep->fb8_0_off = readl(ep->ramdac + RAMDAC_VID_8FB_0);
  259. ep->fb8_0_off -= ep->fb_base_reg;
  260. ep->fb8_1_off = readl(ep->ramdac + RAMDAC_VID_8FB_1);
  261. ep->fb8_1_off -= ep->fb_base_reg;
  262. ep->fb8_buf_diff = ep->fb8_1_off - ep->fb8_0_off;
  263. ep->fb_base_phys = pci_resource_start (pdev, 0);
  264. ep->fb_base_phys += ep->fb8_0_off;
  265. err = pci_request_region(pdev, 0, "e3d framebuffer");
  266. if (err < 0) {
  267. printk("e3d: Cannot request region 0 for %s\n",
  268. pci_name(pdev));
  269. goto err_unmap_ramdac;
  270. }
  271. err = e3d_get_props(ep);
  272. if (err)
  273. goto err_release_pci0;
  274. line_length = (readl(ep->ramdac + RAMDAC_VID_CFG) >> 16) & 0xff;
  275. line_length = 1 << line_length;
  276. switch (ep->depth) {
  277. case 8:
  278. info->fix.line_length = line_length;
  279. break;
  280. case 16:
  281. info->fix.line_length = line_length * 2;
  282. break;
  283. case 24:
  284. info->fix.line_length = line_length * 3;
  285. break;
  286. case 32:
  287. info->fix.line_length = line_length * 4;
  288. break;
  289. }
  290. ep->fb_size = info->fix.line_length * ep->height;
  291. ep->fb_base = ioremap(ep->fb_base_phys, ep->fb_size);
  292. if (!ep->fb_base) {
  293. err = -ENOMEM;
  294. goto err_release_pci0;
  295. }
  296. err = e3d_set_fbinfo(ep);
  297. if (err)
  298. goto err_unmap_fb;
  299. pci_set_drvdata(pdev, info);
  300. printk("e3d: Found device at %s\n", pci_name(pdev));
  301. err = register_framebuffer(info);
  302. if (err < 0) {
  303. printk(KERN_ERR "e3d: Could not register framebuffer %s\n",
  304. pci_name(pdev));
  305. goto err_free_cmap;
  306. }
  307. return 0;
  308. err_free_cmap:
  309. fb_dealloc_cmap(&info->cmap);
  310. err_unmap_fb:
  311. iounmap(ep->fb_base);
  312. err_release_pci0:
  313. pci_release_region(pdev, 0);
  314. err_unmap_ramdac:
  315. iounmap(ep->ramdac);
  316. err_release_pci1:
  317. pci_release_region(pdev, 1);
  318. err_release_fb:
  319. framebuffer_release(info);
  320. err_disable:
  321. pci_disable_device(pdev);
  322. err_out:
  323. return err;
  324. }
  325. static const struct pci_device_id e3d_pci_table[] = {
  326. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x7a0), },
  327. { PCI_DEVICE(0x1091, 0x7a0), },
  328. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x7a2), },
  329. { .vendor = PCI_VENDOR_ID_3DLABS,
  330. .device = PCI_ANY_ID,
  331. .subvendor = PCI_VENDOR_ID_3DLABS,
  332. .subdevice = 0x0108,
  333. },
  334. { .vendor = PCI_VENDOR_ID_3DLABS,
  335. .device = PCI_ANY_ID,
  336. .subvendor = PCI_VENDOR_ID_3DLABS,
  337. .subdevice = 0x0140,
  338. },
  339. { .vendor = PCI_VENDOR_ID_3DLABS,
  340. .device = PCI_ANY_ID,
  341. .subvendor = PCI_VENDOR_ID_3DLABS,
  342. .subdevice = 0x1024,
  343. },
  344. { 0, }
  345. };
  346. static struct pci_driver e3d_driver = {
  347. .driver = {
  348. .suppress_bind_attrs = true,
  349. },
  350. .name = "e3d",
  351. .id_table = e3d_pci_table,
  352. .probe = e3d_pci_register,
  353. };
  354. static int __init e3d_init(void)
  355. {
  356. if (fb_modesetting_disabled("e3d"))
  357. return -ENODEV;
  358. if (fb_get_options("e3d", NULL))
  359. return -ENODEV;
  360. return pci_register_driver(&e3d_driver);
  361. }
  362. device_initcall(e3d_init);