sunxvr2500.c 5.5 KB

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