sunxvr2500.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* sunxvr2500.c: Sun 3DLABS XVR-2500 et al. 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. struct s3d_info {
  14. struct fb_info *info;
  15. struct pci_dev *pdev;
  16. char __iomem *fb_base;
  17. unsigned long fb_base_phys;
  18. struct device_node *of_node;
  19. unsigned int width;
  20. unsigned int height;
  21. unsigned int depth;
  22. unsigned int fb_size;
  23. u32 pseudo_palette[16];
  24. };
  25. static int s3d_get_props(struct s3d_info *sp)
  26. {
  27. sp->width = of_getintprop_default(sp->of_node, "width", 0);
  28. sp->height = of_getintprop_default(sp->of_node, "height", 0);
  29. sp->depth = of_getintprop_default(sp->of_node, "depth", 8);
  30. if (!sp->width || !sp->height) {
  31. printk(KERN_ERR "s3d: Critical properties missing for %s\n",
  32. pci_name(sp->pdev));
  33. return -EINVAL;
  34. }
  35. return 0;
  36. }
  37. static int s3d_setcolreg(unsigned regno,
  38. unsigned red, unsigned green, unsigned blue,
  39. unsigned transp, struct fb_info *info)
  40. {
  41. u32 value;
  42. if (regno < 16) {
  43. red >>= 8;
  44. green >>= 8;
  45. blue >>= 8;
  46. value = (blue << 24) | (green << 16) | (red << 8);
  47. ((u32 *)info->pseudo_palette)[regno] = value;
  48. }
  49. return 0;
  50. }
  51. static const struct fb_ops s3d_ops = {
  52. .owner = THIS_MODULE,
  53. .fb_setcolreg = s3d_setcolreg,
  54. .fb_fillrect = cfb_fillrect,
  55. .fb_copyarea = cfb_copyarea,
  56. .fb_imageblit = cfb_imageblit,
  57. };
  58. static int s3d_set_fbinfo(struct s3d_info *sp)
  59. {
  60. struct fb_info *info = sp->info;
  61. struct fb_var_screeninfo *var = &info->var;
  62. info->flags = FBINFO_DEFAULT;
  63. info->fbops = &s3d_ops;
  64. info->screen_base = sp->fb_base;
  65. info->screen_size = sp->fb_size;
  66. info->pseudo_palette = sp->pseudo_palette;
  67. /* Fill fix common fields */
  68. strlcpy(info->fix.id, "s3d", sizeof(info->fix.id));
  69. info->fix.smem_start = sp->fb_base_phys;
  70. info->fix.smem_len = sp->fb_size;
  71. info->fix.type = FB_TYPE_PACKED_PIXELS;
  72. if (sp->depth == 32 || sp->depth == 24)
  73. info->fix.visual = FB_VISUAL_TRUECOLOR;
  74. else
  75. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  76. var->xres = sp->width;
  77. var->yres = sp->height;
  78. var->xres_virtual = var->xres;
  79. var->yres_virtual = var->yres;
  80. var->bits_per_pixel = sp->depth;
  81. var->red.offset = 8;
  82. var->red.length = 8;
  83. var->green.offset = 16;
  84. var->green.length = 8;
  85. var->blue.offset = 24;
  86. var->blue.length = 8;
  87. var->transp.offset = 0;
  88. var->transp.length = 0;
  89. if (fb_alloc_cmap(&info->cmap, 256, 0)) {
  90. printk(KERN_ERR "s3d: Cannot allocate color map.\n");
  91. return -ENOMEM;
  92. }
  93. return 0;
  94. }
  95. static int s3d_pci_register(struct pci_dev *pdev,
  96. const struct pci_device_id *ent)
  97. {
  98. struct fb_info *info;
  99. struct s3d_info *sp;
  100. int err;
  101. err = pci_enable_device(pdev);
  102. if (err < 0) {
  103. printk(KERN_ERR "s3d: Cannot enable PCI device %s\n",
  104. pci_name(pdev));
  105. goto err_out;
  106. }
  107. info = framebuffer_alloc(sizeof(struct s3d_info), &pdev->dev);
  108. if (!info) {
  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 const struct pci_device_id s3d_pci_table[] = {
  180. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002c), },
  181. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002d), },
  182. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002e), },
  183. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x002f), },
  184. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0030), },
  185. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0031), },
  186. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0032), },
  187. { PCI_DEVICE(PCI_VENDOR_ID_3DLABS, 0x0033), },
  188. { 0, }
  189. };
  190. static struct pci_driver s3d_driver = {
  191. .driver = {
  192. .suppress_bind_attrs = true,
  193. },
  194. .name = "s3d",
  195. .id_table = s3d_pci_table,
  196. .probe = s3d_pci_register,
  197. };
  198. static int __init s3d_init(void)
  199. {
  200. if (fb_get_options("s3d", NULL))
  201. return -ENODEV;
  202. return pci_register_driver(&s3d_driver);
  203. }
  204. device_initcall(s3d_init);