gxfb_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Geode GX framebuffer driver.
  3. *
  4. * Copyright (C) 2006 Arcom Control Systems Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. *
  12. * This driver assumes that the BIOS has created a virtual PCI device header
  13. * for the video device. The PCI header is assumed to contain the following
  14. * BARs:
  15. *
  16. * BAR0 - framebuffer memory
  17. * BAR1 - graphics processor registers
  18. * BAR2 - display controller registers
  19. * BAR3 - video processor and flat panel control registers.
  20. *
  21. * 16 MiB of framebuffer memory is assumed to be available.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/errno.h>
  26. #include <linux/string.h>
  27. #include <linux/mm.h>
  28. #include <linux/tty.h>
  29. #include <linux/slab.h>
  30. #include <linux/delay.h>
  31. #include <linux/fb.h>
  32. #include <linux/init.h>
  33. #include <linux/pci.h>
  34. #include "geodefb.h"
  35. #include "display_gx.h"
  36. #include "video_gx.h"
  37. static char mode_option[32] = "640x480-16@60";
  38. /* Modes relevant to the GX (taken from modedb.c) */
  39. static const struct fb_videomode __initdata gx_modedb[] = {
  40. /* 640x480-60 VESA */
  41. { NULL, 60, 640, 480, 39682, 48, 16, 33, 10, 96, 2,
  42. 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  43. /* 640x480-75 VESA */
  44. { NULL, 75, 640, 480, 31746, 120, 16, 16, 01, 64, 3,
  45. 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  46. /* 640x480-85 VESA */
  47. { NULL, 85, 640, 480, 27777, 80, 56, 25, 01, 56, 3,
  48. 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  49. /* 800x600-60 VESA */
  50. { NULL, 60, 800, 600, 25000, 88, 40, 23, 01, 128, 4,
  51. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  52. FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  53. /* 800x600-75 VESA */
  54. { NULL, 75, 800, 600, 20202, 160, 16, 21, 01, 80, 3,
  55. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  56. FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  57. /* 800x600-85 VESA */
  58. { NULL, 85, 800, 600, 17761, 152, 32, 27, 01, 64, 3,
  59. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  60. FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  61. /* 1024x768-60 VESA */
  62. { NULL, 60, 1024, 768, 15384, 160, 24, 29, 3, 136, 6,
  63. 0, FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  64. /* 1024x768-75 VESA */
  65. { NULL, 75, 1024, 768, 12690, 176, 16, 28, 1, 96, 3,
  66. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  67. FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  68. /* 1024x768-85 VESA */
  69. { NULL, 85, 1024, 768, 10582, 208, 48, 36, 1, 96, 3,
  70. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  71. FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  72. /* 1280x960-60 VESA */
  73. { NULL, 60, 1280, 960, 9259, 312, 96, 36, 1, 112, 3,
  74. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  75. FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  76. /* 1280x960-85 VESA */
  77. { NULL, 85, 1280, 960, 6734, 224, 64, 47, 1, 160, 3,
  78. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  79. FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  80. /* 1280x1024-60 VESA */
  81. { NULL, 60, 1280, 1024, 9259, 248, 48, 38, 1, 112, 3,
  82. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  83. FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  84. /* 1280x1024-75 VESA */
  85. { NULL, 75, 1280, 1024, 7407, 248, 16, 38, 1, 144, 3,
  86. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  87. FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  88. /* 1280x1024-85 VESA */
  89. { NULL, 85, 1280, 1024, 6349, 224, 64, 44, 1, 160, 3,
  90. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  91. FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  92. /* 1600x1200-60 VESA */
  93. { NULL, 60, 1600, 1200, 6172, 304, 64, 46, 1, 192, 3,
  94. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  95. FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  96. /* 1600x1200-75 VESA */
  97. { NULL, 75, 1600, 1200, 4938, 304, 64, 46, 1, 192, 3,
  98. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  99. FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  100. /* 1600x1200-85 VESA */
  101. { NULL, 85, 1600, 1200, 4357, 304, 64, 46, 1, 192, 3,
  102. FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT,
  103. FB_VMODE_NONINTERLACED, FB_MODE_IS_VESA },
  104. };
  105. static int gxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  106. {
  107. if (var->xres > 1600 || var->yres > 1200)
  108. return -EINVAL;
  109. if ((var->xres > 1280 || var->yres > 1024) && var->bits_per_pixel > 16)
  110. return -EINVAL;
  111. if (var->bits_per_pixel == 32) {
  112. var->red.offset = 16; var->red.length = 8;
  113. var->green.offset = 8; var->green.length = 8;
  114. var->blue.offset = 0; var->blue.length = 8;
  115. } else if (var->bits_per_pixel == 16) {
  116. var->red.offset = 11; var->red.length = 5;
  117. var->green.offset = 5; var->green.length = 6;
  118. var->blue.offset = 0; var->blue.length = 5;
  119. } else if (var->bits_per_pixel == 8) {
  120. var->red.offset = 0; var->red.length = 8;
  121. var->green.offset = 0; var->green.length = 8;
  122. var->blue.offset = 0; var->blue.length = 8;
  123. } else
  124. return -EINVAL;
  125. var->transp.offset = 0; var->transp.length = 0;
  126. /* Enough video memory? */
  127. if (gx_line_delta(var->xres, var->bits_per_pixel) * var->yres > info->fix.smem_len)
  128. return -EINVAL;
  129. /* FIXME: Check timing parameters here? */
  130. return 0;
  131. }
  132. static int gxfb_set_par(struct fb_info *info)
  133. {
  134. struct geodefb_par *par = info->par;
  135. if (info->var.bits_per_pixel > 8) {
  136. info->fix.visual = FB_VISUAL_TRUECOLOR;
  137. fb_dealloc_cmap(&info->cmap);
  138. } else {
  139. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  140. fb_alloc_cmap(&info->cmap, 1<<info->var.bits_per_pixel, 0);
  141. }
  142. info->fix.line_length = gx_line_delta(info->var.xres, info->var.bits_per_pixel);
  143. par->dc_ops->set_mode(info);
  144. return 0;
  145. }
  146. static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
  147. {
  148. chan &= 0xffff;
  149. chan >>= 16 - bf->length;
  150. return chan << bf->offset;
  151. }
  152. static int gxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  153. unsigned blue, unsigned transp,
  154. struct fb_info *info)
  155. {
  156. struct geodefb_par *par = info->par;
  157. if (info->var.grayscale) {
  158. /* grayscale = 0.30*R + 0.59*G + 0.11*B */
  159. red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
  160. }
  161. /* Truecolor has hardware independent palette */
  162. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  163. u32 *pal = info->pseudo_palette;
  164. u32 v;
  165. if (regno >= 16)
  166. return -EINVAL;
  167. v = chan_to_field(red, &info->var.red);
  168. v |= chan_to_field(green, &info->var.green);
  169. v |= chan_to_field(blue, &info->var.blue);
  170. pal[regno] = v;
  171. } else {
  172. if (regno >= 256)
  173. return -EINVAL;
  174. par->dc_ops->set_palette_reg(info, regno, red, green, blue);
  175. }
  176. return 0;
  177. }
  178. static int gxfb_blank(int blank_mode, struct fb_info *info)
  179. {
  180. struct geodefb_par *par = info->par;
  181. return par->vid_ops->blank_display(info, blank_mode);
  182. }
  183. static int __init gxfb_map_video_memory(struct fb_info *info, struct pci_dev *dev)
  184. {
  185. struct geodefb_par *par = info->par;
  186. int fb_len;
  187. int ret;
  188. ret = pci_enable_device(dev);
  189. if (ret < 0)
  190. return ret;
  191. ret = pci_request_region(dev, 3, "gxfb (video processor)");
  192. if (ret < 0)
  193. return ret;
  194. par->vid_regs = ioremap(pci_resource_start(dev, 3),
  195. pci_resource_len(dev, 3));
  196. if (!par->vid_regs)
  197. return -ENOMEM;
  198. ret = pci_request_region(dev, 2, "gxfb (display controller)");
  199. if (ret < 0)
  200. return ret;
  201. par->dc_regs = ioremap(pci_resource_start(dev, 2), pci_resource_len(dev, 2));
  202. if (!par->dc_regs)
  203. return -ENOMEM;
  204. ret = pci_request_region(dev, 0, "gxfb (framebuffer)");
  205. if (ret < 0)
  206. return ret;
  207. if ((fb_len = gx_frame_buffer_size()) < 0)
  208. return -ENOMEM;
  209. info->fix.smem_start = pci_resource_start(dev, 0);
  210. info->fix.smem_len = fb_len;
  211. info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
  212. if (!info->screen_base)
  213. return -ENOMEM;
  214. dev_info(&dev->dev, "%d Kibyte of video memory at 0x%lx\n",
  215. info->fix.smem_len / 1024, info->fix.smem_start);
  216. return 0;
  217. }
  218. static struct fb_ops gxfb_ops = {
  219. .owner = THIS_MODULE,
  220. .fb_check_var = gxfb_check_var,
  221. .fb_set_par = gxfb_set_par,
  222. .fb_setcolreg = gxfb_setcolreg,
  223. .fb_blank = gxfb_blank,
  224. /* No HW acceleration for now. */
  225. .fb_fillrect = cfb_fillrect,
  226. .fb_copyarea = cfb_copyarea,
  227. .fb_imageblit = cfb_imageblit,
  228. };
  229. static struct fb_info * __init gxfb_init_fbinfo(struct device *dev)
  230. {
  231. struct geodefb_par *par;
  232. struct fb_info *info;
  233. /* Alloc enough space for the pseudo palette. */
  234. info = framebuffer_alloc(sizeof(struct geodefb_par) + sizeof(u32) * 16, dev);
  235. if (!info)
  236. return NULL;
  237. par = info->par;
  238. strcpy(info->fix.id, "Geode GX");
  239. info->fix.type = FB_TYPE_PACKED_PIXELS;
  240. info->fix.type_aux = 0;
  241. info->fix.xpanstep = 0;
  242. info->fix.ypanstep = 0;
  243. info->fix.ywrapstep = 0;
  244. info->fix.accel = FB_ACCEL_NONE;
  245. info->var.nonstd = 0;
  246. info->var.activate = FB_ACTIVATE_NOW;
  247. info->var.height = -1;
  248. info->var.width = -1;
  249. info->var.accel_flags = 0;
  250. info->var.vmode = FB_VMODE_NONINTERLACED;
  251. info->fbops = &gxfb_ops;
  252. info->flags = FBINFO_DEFAULT;
  253. info->node = -1;
  254. info->pseudo_palette = (void *)par + sizeof(struct geodefb_par);
  255. info->var.grayscale = 0;
  256. return info;
  257. }
  258. static int __init gxfb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  259. {
  260. struct geodefb_par *par;
  261. struct fb_info *info;
  262. int ret;
  263. info = gxfb_init_fbinfo(&pdev->dev);
  264. if (!info)
  265. return -ENOMEM;
  266. par = info->par;
  267. /* GX display controller and GX video device. */
  268. par->dc_ops = &gx_dc_ops;
  269. par->vid_ops = &gx_vid_ops;
  270. if ((ret = gxfb_map_video_memory(info, pdev)) < 0) {
  271. dev_err(&pdev->dev, "failed to map frame buffer or controller registers\n");
  272. goto err;
  273. }
  274. ret = fb_find_mode(&info->var, info, mode_option,
  275. gx_modedb, ARRAY_SIZE(gx_modedb), NULL, 16);
  276. if (ret == 0 || ret == 4) {
  277. dev_err(&pdev->dev, "could not find valid video mode\n");
  278. ret = -EINVAL;
  279. goto err;
  280. }
  281. /* Clear the frame buffer of garbage. */
  282. memset_io(info->screen_base, 0, info->fix.smem_len);
  283. gxfb_check_var(&info->var, info);
  284. gxfb_set_par(info);
  285. if (register_framebuffer(info) < 0) {
  286. ret = -EINVAL;
  287. goto err;
  288. }
  289. pci_set_drvdata(pdev, info);
  290. printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node, info->fix.id);
  291. return 0;
  292. err:
  293. if (info->screen_base) {
  294. iounmap(info->screen_base);
  295. pci_release_region(pdev, 0);
  296. }
  297. if (par->vid_regs) {
  298. iounmap(par->vid_regs);
  299. pci_release_region(pdev, 3);
  300. }
  301. if (par->dc_regs) {
  302. iounmap(par->dc_regs);
  303. pci_release_region(pdev, 2);
  304. }
  305. pci_disable_device(pdev);
  306. if (info)
  307. framebuffer_release(info);
  308. return ret;
  309. }
  310. static void gxfb_remove(struct pci_dev *pdev)
  311. {
  312. struct fb_info *info = pci_get_drvdata(pdev);
  313. struct geodefb_par *par = info->par;
  314. unregister_framebuffer(info);
  315. iounmap((void __iomem *)info->screen_base);
  316. pci_release_region(pdev, 0);
  317. iounmap(par->vid_regs);
  318. pci_release_region(pdev, 3);
  319. iounmap(par->dc_regs);
  320. pci_release_region(pdev, 2);
  321. pci_disable_device(pdev);
  322. pci_set_drvdata(pdev, NULL);
  323. framebuffer_release(info);
  324. }
  325. static struct pci_device_id gxfb_id_table[] = {
  326. { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_CS5535_VIDEO,
  327. PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
  328. 0xff0000, 0 },
  329. { 0, }
  330. };
  331. MODULE_DEVICE_TABLE(pci, gxfb_id_table);
  332. static struct pci_driver gxfb_driver = {
  333. .name = "gxfb",
  334. .id_table = gxfb_id_table,
  335. .probe = gxfb_probe,
  336. .remove = gxfb_remove,
  337. };
  338. static int __init gxfb_init(void)
  339. {
  340. #ifndef MODULE
  341. if (fb_get_options("gxfb", NULL))
  342. return -ENODEV;
  343. #endif
  344. return pci_register_driver(&gxfb_driver);
  345. }
  346. static void __exit gxfb_cleanup(void)
  347. {
  348. pci_unregister_driver(&gxfb_driver);
  349. }
  350. module_init(gxfb_init);
  351. module_exit(gxfb_cleanup);
  352. module_param_string(mode, mode_option, sizeof(mode_option), 0444);
  353. MODULE_PARM_DESC(mode, "video mode (<x>x<y>[-<bpp>][@<refr>])");
  354. MODULE_DESCRIPTION("Framebuffer driver for the AMD Geode GX");
  355. MODULE_LICENSE("GPL");