sunxvr2500.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* s3d.c: Sun 3DLABS XVR-2500 et al. 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. struct s3d_info {
  13. struct fb_info *info;
  14. struct pci_dev *pdev;
  15. char __iomem *fb_base;
  16. unsigned long fb_base_phys;
  17. struct device_node *of_node;
  18. unsigned int width;
  19. unsigned int height;
  20. unsigned int depth;
  21. unsigned int fb_size;
  22. u32 pseudo_palette[16];
  23. };
  24. static int s3d_get_props(struct s3d_info *sp)
  25. {
  26. sp->width = of_getintprop_default(sp->of_node, "width", 0);
  27. sp->height = of_getintprop_default(sp->of_node, "height", 0);
  28. sp->depth = of_getintprop_default(sp->of_node, "depth", 8);
  29. if (!sp->width || !sp->height) {
  30. printk(KERN_ERR "s3d: Critical properties missing for %s\n",
  31. pci_name(sp->pdev));
  32. return -EINVAL;
  33. }
  34. return 0;
  35. }
  36. static int s3d_setcolreg(unsigned regno,
  37. unsigned red, unsigned green, unsigned blue,
  38. unsigned transp, struct fb_info *info)
  39. {
  40. u32 value;
  41. if (regno < 16) {
  42. red >>= 8;
  43. green >>= 8;
  44. blue >>= 8;
  45. value = (blue << 24) | (green << 16) | (red << 8);
  46. ((u32 *)info->pseudo_palette)[regno] = value;
  47. }
  48. return 0;
  49. }
  50. static struct fb_ops s3d_ops = {
  51. .owner = THIS_MODULE,
  52. .fb_setcolreg = s3d_setcolreg,
  53. .fb_fillrect = cfb_fillrect,
  54. .fb_copyarea = cfb_copyarea,
  55. .fb_imageblit = cfb_imageblit,
  56. };
  57. static int s3d_set_fbinfo(struct s3d_info *sp)
  58. {
  59. struct fb_info *info = sp->info;
  60. struct fb_var_screeninfo *var = &info->var;
  61. info->flags = FBINFO_DEFAULT;
  62. info->fbops = &s3d_ops;
  63. info->screen_base = sp->fb_base;
  64. info->screen_size = sp->fb_size;
  65. info->pseudo_palette = sp->pseudo_palette;
  66. /* Fill fix common fields */
  67. strlcpy(info->fix.id, "s3d", sizeof(info->fix.id));
  68. info->fix.smem_start = sp->fb_base_phys;
  69. info->fix.smem_len = sp->fb_size;
  70. info->fix.type = FB_TYPE_PACKED_PIXELS;
  71. if (sp->depth == 32 || sp->depth == 24)
  72. info->fix.visual = FB_VISUAL_TRUECOLOR;
  73. else
  74. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  75. var->xres = sp->width;
  76. var->yres = sp->height;
  77. var->xres_virtual = var->xres;
  78. var->yres_virtual = var->yres;
  79. var->bits_per_pixel = sp->depth;
  80. var->red.offset = 8;
  81. var->red.length = 8;
  82. var->green.offset = 16;
  83. var->green.length = 8;
  84. var->blue.offset = 24;
  85. var->blue.length = 8;
  86. var->transp.offset = 0;
  87. var->transp.length = 0;
  88. if (fb_alloc_cmap(&info->cmap, 256, 0)) {
  89. printk(KERN_ERR "s3d: Cannot allocate color map.\n");
  90. return -ENOMEM;
  91. }
  92. return 0;
  93. }
  94. static int s3d_pci_register(struct pci_dev *pdev,
  95. const struct pci_device_id *ent)
  96. {
  97. struct fb_info *info;
  98. struct s3d_info *sp;
  99. int err;
  100. err = pci_enable_device(pdev);
  101. if (err < 0) {
  102. printk(KERN_ERR "s3d: Cannot enable PCI device %s\n",
  103. pci_name(pdev));
  104. goto err_out;
  105. }
  106. info = framebuffer_alloc(sizeof(struct s3d_info), &pdev->dev);
  107. if (!info) {
  108. printk(KERN_ERR "s3d: Cannot allocate fb_info\n");
  109. err = -ENOMEM;
  110. goto err_disable;
  111. }
  112. sp = info->par;
  113. sp->info = info;
  114. sp->pdev = pdev;
  115. sp->of_node = pci_device_to_OF_node(pdev);
  116. if (!sp->of_node) {
  117. printk(KERN_ERR "s3d: Cannot find OF node of %s\n",
  118. pci_name(pdev));
  119. err = -ENODEV;
  120. goto err_release_fb;
  121. }
  122. sp->fb_base_phys = pci_resource_start (pdev, 1);
  123. err = pci_request_region(pdev, 1, "s3d framebuffer");
  124. if (err < 0) {
  125. printk("s3d: Cannot request region 1 for %s\n",
  126. pci_name(pdev));
  127. goto err_release_fb;
  128. }
  129. err = s3d_get_props(sp);
  130. if (err)
  131. goto err_release_pci;
  132. /* XXX 'linebytes' is often wrong, it is equal to the width
  133. * XXX with depth of 32 on my XVR-2500 which is clearly not
  134. * XXX right. So we don't try to use it.
  135. */
  136. switch (sp->depth) {
  137. case 8:
  138. info->fix.line_length = sp->width;
  139. break;
  140. case 16:
  141. info->fix.line_length = sp->width * 2;
  142. break;
  143. case 24:
  144. info->fix.line_length = sp->width * 3;
  145. break;
  146. case 32:
  147. info->fix.line_length = sp->width * 4;
  148. break;
  149. }
  150. sp->fb_size = info->fix.line_length * sp->height;
  151. sp->fb_base = ioremap(sp->fb_base_phys, sp->fb_size);
  152. if (!sp->fb_base) {
  153. err = -ENOMEM;
  154. goto err_release_pci;
  155. }
  156. err = s3d_set_fbinfo(sp);
  157. if (err)
  158. goto err_unmap_fb;
  159. pci_set_drvdata(pdev, info);
  160. printk("s3d: Found device at %s\n", pci_name(pdev));
  161. err = register_framebuffer(info);
  162. if (err < 0) {
  163. printk(KERN_ERR "s3d: Could not register framebuffer %s\n",
  164. pci_name(pdev));
  165. goto err_unmap_fb;
  166. }
  167. return 0;
  168. err_unmap_fb:
  169. iounmap(sp->fb_base);
  170. err_release_pci:
  171. pci_release_region(pdev, 1);
  172. err_release_fb:
  173. framebuffer_release(info);
  174. err_disable:
  175. pci_disable_device(pdev);
  176. err_out:
  177. return err;
  178. }
  179. static void s3d_pci_unregister(struct pci_dev *pdev)
  180. {
  181. struct fb_info *info = pci_get_drvdata(pdev);
  182. struct s3d_info *sp = info->par;
  183. unregister_framebuffer(info);
  184. iounmap(sp->fb_base);
  185. pci_release_region(pdev, 1);
  186. framebuffer_release(info);
  187. pci_disable_device(pdev);
  188. }
  189. static struct pci_device_id s3d_pci_table[] = {
  190. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002c), },
  191. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002d), },
  192. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002e), },
  193. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002f), },
  194. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0030), },
  195. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0031), },
  196. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0032), },
  197. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0033), },
  198. { 0, }
  199. };
  200. static struct pci_driver s3d_driver = {
  201. .name = "s3d",
  202. .id_table = s3d_pci_table,
  203. .probe = s3d_pci_register,
  204. .remove = s3d_pci_unregister,
  205. };
  206. static int __init s3d_init(void)
  207. {
  208. if (fb_get_options("s3d", NULL))
  209. return -ENODEV;
  210. return pci_register_driver(&s3d_driver);
  211. }
  212. static void __exit s3d_exit(void)
  213. {
  214. pci_unregister_driver(&s3d_driver);
  215. }
  216. module_init(s3d_init);
  217. module_exit(s3d_exit);
  218. MODULE_DESCRIPTION("framebuffer driver for Sun XVR-2500 graphics");
  219. MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
  220. MODULE_VERSION("1.0");
  221. MODULE_LICENSE("GPL");