gxfb_core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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/slab.h>
  29. #include <linux/delay.h>
  30. #include <linux/fb.h>
  31. #include <linux/init.h>
  32. #include <linux/pci.h>
  33. #include <asm/geode.h>
  34. #include "geodefb.h"
  35. #include "display_gx.h"
  36. #include "video_gx.h"
  37. static char *mode_option;
  38. /* Modes relevant to the GX (taken from modedb.c) */
  39. static const struct fb_videomode gx_modedb[] __initdata = {
  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. /* Set the 16MB aligned base address of the graphics memory region
  215. * in the display controller */
  216. writel(info->fix.smem_start & 0xFF000000,
  217. par->dc_regs + DC_GLIU0_MEM_OFFSET);
  218. dev_info(&dev->dev, "%d Kibyte of video memory at 0x%lx\n",
  219. info->fix.smem_len / 1024, info->fix.smem_start);
  220. return 0;
  221. }
  222. static struct fb_ops gxfb_ops = {
  223. .owner = THIS_MODULE,
  224. .fb_check_var = gxfb_check_var,
  225. .fb_set_par = gxfb_set_par,
  226. .fb_setcolreg = gxfb_setcolreg,
  227. .fb_blank = gxfb_blank,
  228. /* No HW acceleration for now. */
  229. .fb_fillrect = cfb_fillrect,
  230. .fb_copyarea = cfb_copyarea,
  231. .fb_imageblit = cfb_imageblit,
  232. };
  233. static struct fb_info * __init gxfb_init_fbinfo(struct device *dev)
  234. {
  235. struct geodefb_par *par;
  236. struct fb_info *info;
  237. /* Alloc enough space for the pseudo palette. */
  238. info = framebuffer_alloc(sizeof(struct geodefb_par) + sizeof(u32) * 16, dev);
  239. if (!info)
  240. return NULL;
  241. par = info->par;
  242. strcpy(info->fix.id, "Geode GX");
  243. info->fix.type = FB_TYPE_PACKED_PIXELS;
  244. info->fix.type_aux = 0;
  245. info->fix.xpanstep = 0;
  246. info->fix.ypanstep = 0;
  247. info->fix.ywrapstep = 0;
  248. info->fix.accel = FB_ACCEL_NONE;
  249. info->var.nonstd = 0;
  250. info->var.activate = FB_ACTIVATE_NOW;
  251. info->var.height = -1;
  252. info->var.width = -1;
  253. info->var.accel_flags = 0;
  254. info->var.vmode = FB_VMODE_NONINTERLACED;
  255. info->fbops = &gxfb_ops;
  256. info->flags = FBINFO_DEFAULT;
  257. info->node = -1;
  258. info->pseudo_palette = (void *)par + sizeof(struct geodefb_par);
  259. info->var.grayscale = 0;
  260. return info;
  261. }
  262. static int __init gxfb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  263. {
  264. struct geodefb_par *par;
  265. struct fb_info *info;
  266. int ret;
  267. unsigned long val;
  268. info = gxfb_init_fbinfo(&pdev->dev);
  269. if (!info)
  270. return -ENOMEM;
  271. par = info->par;
  272. /* GX display controller and GX video device. */
  273. par->dc_ops = &gx_dc_ops;
  274. par->vid_ops = &gx_vid_ops;
  275. if ((ret = gxfb_map_video_memory(info, pdev)) < 0) {
  276. dev_err(&pdev->dev, "failed to map frame buffer or controller registers\n");
  277. goto err;
  278. }
  279. /* Figure out if this is a TFT or CRT part */
  280. rdmsrl(MSR_GX_GLD_MSR_CONFIG, val);
  281. if ((val & GLD_MSR_CONFIG_DM_FP) == GLD_MSR_CONFIG_DM_FP)
  282. par->enable_crt = 0;
  283. else
  284. par->enable_crt = 1;
  285. ret = fb_find_mode(&info->var, info, mode_option,
  286. gx_modedb, ARRAY_SIZE(gx_modedb), NULL, 16);
  287. if (ret == 0 || ret == 4) {
  288. dev_err(&pdev->dev, "could not find valid video mode\n");
  289. ret = -EINVAL;
  290. goto err;
  291. }
  292. /* Clear the frame buffer of garbage. */
  293. memset_io(info->screen_base, 0, info->fix.smem_len);
  294. gxfb_check_var(&info->var, info);
  295. gxfb_set_par(info);
  296. if (register_framebuffer(info) < 0) {
  297. ret = -EINVAL;
  298. goto err;
  299. }
  300. pci_set_drvdata(pdev, info);
  301. printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node, info->fix.id);
  302. return 0;
  303. err:
  304. if (info->screen_base) {
  305. iounmap(info->screen_base);
  306. pci_release_region(pdev, 0);
  307. }
  308. if (par->vid_regs) {
  309. iounmap(par->vid_regs);
  310. pci_release_region(pdev, 3);
  311. }
  312. if (par->dc_regs) {
  313. iounmap(par->dc_regs);
  314. pci_release_region(pdev, 2);
  315. }
  316. if (info)
  317. framebuffer_release(info);
  318. return ret;
  319. }
  320. static void gxfb_remove(struct pci_dev *pdev)
  321. {
  322. struct fb_info *info = pci_get_drvdata(pdev);
  323. struct geodefb_par *par = info->par;
  324. unregister_framebuffer(info);
  325. iounmap((void __iomem *)info->screen_base);
  326. pci_release_region(pdev, 0);
  327. iounmap(par->vid_regs);
  328. pci_release_region(pdev, 3);
  329. iounmap(par->dc_regs);
  330. pci_release_region(pdev, 2);
  331. pci_set_drvdata(pdev, NULL);
  332. framebuffer_release(info);
  333. }
  334. static struct pci_device_id gxfb_id_table[] = {
  335. { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_GX_VIDEO,
  336. PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
  337. 0xff0000, 0 },
  338. { 0, }
  339. };
  340. MODULE_DEVICE_TABLE(pci, gxfb_id_table);
  341. static struct pci_driver gxfb_driver = {
  342. .name = "gxfb",
  343. .id_table = gxfb_id_table,
  344. .probe = gxfb_probe,
  345. .remove = gxfb_remove,
  346. };
  347. #ifndef MODULE
  348. static int __init gxfb_setup(char *options)
  349. {
  350. char *opt;
  351. if (!options || !*options)
  352. return 0;
  353. while ((opt = strsep(&options, ",")) != NULL) {
  354. if (!*opt)
  355. continue;
  356. mode_option = opt;
  357. }
  358. return 0;
  359. }
  360. #endif
  361. static int __init gxfb_init(void)
  362. {
  363. #ifndef MODULE
  364. char *option = NULL;
  365. if (fb_get_options("gxfb", &option))
  366. return -ENODEV;
  367. gxfb_setup(option);
  368. #endif
  369. return pci_register_driver(&gxfb_driver);
  370. }
  371. static void __exit gxfb_cleanup(void)
  372. {
  373. pci_unregister_driver(&gxfb_driver);
  374. }
  375. module_init(gxfb_init);
  376. module_exit(gxfb_cleanup);
  377. module_param(mode_option, charp, 0);
  378. MODULE_PARM_DESC(mode_option, "video mode (<x>x<y>[-<bpp>][@<refr>])");
  379. MODULE_DESCRIPTION("Framebuffer driver for the AMD Geode GX");
  380. MODULE_LICENSE("GPL");