sunxvr2500.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 __devinit 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 __devinit 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 __devinit 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. goto err_release_pci;
  154. err = s3d_set_fbinfo(sp);
  155. if (err)
  156. goto err_unmap_fb;
  157. pci_set_drvdata(pdev, info);
  158. printk("s3d: Found device at %s\n", pci_name(pdev));
  159. err = register_framebuffer(info);
  160. if (err < 0) {
  161. printk(KERN_ERR "s3d: Could not register framebuffer %s\n",
  162. pci_name(pdev));
  163. goto err_unmap_fb;
  164. }
  165. return 0;
  166. err_unmap_fb:
  167. iounmap(sp->fb_base);
  168. err_release_pci:
  169. pci_release_region(pdev, 1);
  170. err_release_fb:
  171. framebuffer_release(info);
  172. err_disable:
  173. pci_disable_device(pdev);
  174. err_out:
  175. return err;
  176. }
  177. static void __devexit s3d_pci_unregister(struct pci_dev *pdev)
  178. {
  179. struct fb_info *info = pci_get_drvdata(pdev);
  180. struct s3d_info *sp = info->par;
  181. unregister_framebuffer(info);
  182. iounmap(sp->fb_base);
  183. pci_release_region(pdev, 1);
  184. framebuffer_release(info);
  185. pci_disable_device(pdev);
  186. }
  187. static struct pci_device_id s3d_pci_table[] = {
  188. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002c), },
  189. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002d), },
  190. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002e), },
  191. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002f), },
  192. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0030), },
  193. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0031), },
  194. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0032), },
  195. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0033), },
  196. { 0, }
  197. };
  198. static struct pci_driver s3d_driver = {
  199. .name = "s3d",
  200. .id_table = s3d_pci_table,
  201. .probe = s3d_pci_register,
  202. .remove = __devexit_p(s3d_pci_unregister),
  203. };
  204. static int __init s3d_init(void)
  205. {
  206. if (fb_get_options("s3d", NULL))
  207. return -ENODEV;
  208. return pci_register_driver(&s3d_driver);
  209. }
  210. static void __exit s3d_exit(void)
  211. {
  212. pci_unregister_driver(&s3d_driver);
  213. }
  214. module_init(s3d_init);
  215. module_exit(s3d_exit);
  216. MODULE_DESCRIPTION("framebuffer driver for Sun XVR-2500 graphics");
  217. MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
  218. MODULE_VERSION("1.0");
  219. MODULE_LICENSE("GPL");