gxfb_core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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/console.h>
  32. #include <linux/init.h>
  33. #include <linux/pci.h>
  34. #include <asm/geode.h>
  35. #include "gxfb.h"
  36. static char *mode_option;
  37. static int vram;
  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. if (info->var.bits_per_pixel > 8) {
  135. info->fix.visual = FB_VISUAL_TRUECOLOR;
  136. fb_dealloc_cmap(&info->cmap);
  137. } else {
  138. info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
  139. fb_alloc_cmap(&info->cmap, 1<<info->var.bits_per_pixel, 0);
  140. }
  141. info->fix.line_length = gx_line_delta(info->var.xres, info->var.bits_per_pixel);
  142. gx_set_mode(info);
  143. return 0;
  144. }
  145. static inline u_int chan_to_field(u_int chan, struct fb_bitfield *bf)
  146. {
  147. chan &= 0xffff;
  148. chan >>= 16 - bf->length;
  149. return chan << bf->offset;
  150. }
  151. static int gxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  152. unsigned blue, unsigned transp,
  153. struct fb_info *info)
  154. {
  155. if (info->var.grayscale) {
  156. /* grayscale = 0.30*R + 0.59*G + 0.11*B */
  157. red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
  158. }
  159. /* Truecolor has hardware independent palette */
  160. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  161. u32 *pal = info->pseudo_palette;
  162. u32 v;
  163. if (regno >= 16)
  164. return -EINVAL;
  165. v = chan_to_field(red, &info->var.red);
  166. v |= chan_to_field(green, &info->var.green);
  167. v |= chan_to_field(blue, &info->var.blue);
  168. pal[regno] = v;
  169. } else {
  170. if (regno >= 256)
  171. return -EINVAL;
  172. gx_set_hw_palette_reg(info, regno, red, green, blue);
  173. }
  174. return 0;
  175. }
  176. static int gxfb_blank(int blank_mode, struct fb_info *info)
  177. {
  178. return gx_blank_display(info, blank_mode);
  179. }
  180. static int __init gxfb_map_video_memory(struct fb_info *info, struct pci_dev *dev)
  181. {
  182. struct gxfb_par *par = info->par;
  183. int ret;
  184. ret = pci_enable_device(dev);
  185. if (ret < 0)
  186. return ret;
  187. ret = pci_request_region(dev, 3, "gxfb (video processor)");
  188. if (ret < 0)
  189. return ret;
  190. par->vid_regs = ioremap(pci_resource_start(dev, 3),
  191. pci_resource_len(dev, 3));
  192. if (!par->vid_regs)
  193. return -ENOMEM;
  194. ret = pci_request_region(dev, 2, "gxfb (display controller)");
  195. if (ret < 0)
  196. return ret;
  197. par->dc_regs = ioremap(pci_resource_start(dev, 2), pci_resource_len(dev, 2));
  198. if (!par->dc_regs)
  199. return -ENOMEM;
  200. ret = pci_request_region(dev, 1, "gxfb (graphics processor)");
  201. if (ret < 0)
  202. return ret;
  203. par->gp_regs = ioremap(pci_resource_start(dev, 1),
  204. pci_resource_len(dev, 1));
  205. if (!par->gp_regs)
  206. return -ENOMEM;
  207. ret = pci_request_region(dev, 0, "gxfb (framebuffer)");
  208. if (ret < 0)
  209. return ret;
  210. info->fix.smem_start = pci_resource_start(dev, 0);
  211. info->fix.smem_len = vram ? vram : gx_frame_buffer_size();
  212. info->screen_base = ioremap(info->fix.smem_start, info->fix.smem_len);
  213. if (!info->screen_base)
  214. return -ENOMEM;
  215. /* Set the 16MiB aligned base address of the graphics memory region
  216. * in the display controller */
  217. write_dc(par, DC_GLIU0_MEM_OFFSET, info->fix.smem_start & 0xFF000000);
  218. dev_info(&dev->dev, "%d KiB 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 gxfb_par *par;
  236. struct fb_info *info;
  237. /* Alloc enough space for the pseudo palette. */
  238. info = framebuffer_alloc(sizeof(struct gxfb_par) + sizeof(u32) * 16,
  239. dev);
  240. if (!info)
  241. return NULL;
  242. par = info->par;
  243. strcpy(info->fix.id, "Geode GX");
  244. info->fix.type = FB_TYPE_PACKED_PIXELS;
  245. info->fix.type_aux = 0;
  246. info->fix.xpanstep = 0;
  247. info->fix.ypanstep = 0;
  248. info->fix.ywrapstep = 0;
  249. info->fix.accel = FB_ACCEL_NONE;
  250. info->var.nonstd = 0;
  251. info->var.activate = FB_ACTIVATE_NOW;
  252. info->var.height = -1;
  253. info->var.width = -1;
  254. info->var.accel_flags = 0;
  255. info->var.vmode = FB_VMODE_NONINTERLACED;
  256. info->fbops = &gxfb_ops;
  257. info->flags = FBINFO_DEFAULT;
  258. info->node = -1;
  259. info->pseudo_palette = (void *)par + sizeof(struct gxfb_par);
  260. info->var.grayscale = 0;
  261. return info;
  262. }
  263. #ifdef CONFIG_PM
  264. static int gxfb_suspend(struct pci_dev *pdev, pm_message_t state)
  265. {
  266. struct fb_info *info = pci_get_drvdata(pdev);
  267. if (state.event == PM_EVENT_SUSPEND) {
  268. acquire_console_sem();
  269. gx_powerdown(info);
  270. fb_set_suspend(info, 1);
  271. release_console_sem();
  272. }
  273. /* there's no point in setting PCI states; we emulate PCI, so
  274. * we don't end up getting power savings anyways */
  275. return 0;
  276. }
  277. static int gxfb_resume(struct pci_dev *pdev)
  278. {
  279. struct fb_info *info = pci_get_drvdata(pdev);
  280. int ret;
  281. acquire_console_sem();
  282. ret = gx_powerup(info);
  283. if (ret) {
  284. printk(KERN_ERR "gxfb: power up failed!\n");
  285. return ret;
  286. }
  287. fb_set_suspend(info, 0);
  288. release_console_sem();
  289. return 0;
  290. }
  291. #endif
  292. static int __init gxfb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  293. {
  294. struct gxfb_par *par;
  295. struct fb_info *info;
  296. int ret;
  297. unsigned long val;
  298. info = gxfb_init_fbinfo(&pdev->dev);
  299. if (!info)
  300. return -ENOMEM;
  301. par = info->par;
  302. if ((ret = gxfb_map_video_memory(info, pdev)) < 0) {
  303. dev_err(&pdev->dev, "failed to map frame buffer or controller registers\n");
  304. goto err;
  305. }
  306. /* Figure out if this is a TFT or CRT part */
  307. rdmsrl(MSR_GX_GLD_MSR_CONFIG, val);
  308. if ((val & MSR_GX_GLD_MSR_CONFIG_FP) == MSR_GX_GLD_MSR_CONFIG_FP)
  309. par->enable_crt = 0;
  310. else
  311. par->enable_crt = 1;
  312. ret = fb_find_mode(&info->var, info, mode_option,
  313. gx_modedb, ARRAY_SIZE(gx_modedb), NULL, 16);
  314. if (ret == 0 || ret == 4) {
  315. dev_err(&pdev->dev, "could not find valid video mode\n");
  316. ret = -EINVAL;
  317. goto err;
  318. }
  319. /* Clear the frame buffer of garbage. */
  320. memset_io(info->screen_base, 0, info->fix.smem_len);
  321. gxfb_check_var(&info->var, info);
  322. gxfb_set_par(info);
  323. if (register_framebuffer(info) < 0) {
  324. ret = -EINVAL;
  325. goto err;
  326. }
  327. pci_set_drvdata(pdev, info);
  328. printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node, info->fix.id);
  329. return 0;
  330. err:
  331. if (info->screen_base) {
  332. iounmap(info->screen_base);
  333. pci_release_region(pdev, 0);
  334. }
  335. if (par->vid_regs) {
  336. iounmap(par->vid_regs);
  337. pci_release_region(pdev, 3);
  338. }
  339. if (par->dc_regs) {
  340. iounmap(par->dc_regs);
  341. pci_release_region(pdev, 2);
  342. }
  343. if (par->gp_regs) {
  344. iounmap(par->gp_regs);
  345. pci_release_region(pdev, 1);
  346. }
  347. if (info)
  348. framebuffer_release(info);
  349. return ret;
  350. }
  351. static void gxfb_remove(struct pci_dev *pdev)
  352. {
  353. struct fb_info *info = pci_get_drvdata(pdev);
  354. struct gxfb_par *par = info->par;
  355. unregister_framebuffer(info);
  356. iounmap((void __iomem *)info->screen_base);
  357. pci_release_region(pdev, 0);
  358. iounmap(par->vid_regs);
  359. pci_release_region(pdev, 3);
  360. iounmap(par->dc_regs);
  361. pci_release_region(pdev, 2);
  362. iounmap(par->gp_regs);
  363. pci_release_region(pdev, 1);
  364. pci_set_drvdata(pdev, NULL);
  365. framebuffer_release(info);
  366. }
  367. static struct pci_device_id gxfb_id_table[] = {
  368. { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_GX_VIDEO) },
  369. { 0, }
  370. };
  371. MODULE_DEVICE_TABLE(pci, gxfb_id_table);
  372. static struct pci_driver gxfb_driver = {
  373. .name = "gxfb",
  374. .id_table = gxfb_id_table,
  375. .probe = gxfb_probe,
  376. .remove = gxfb_remove,
  377. #ifdef CONFIG_PM
  378. .suspend = gxfb_suspend,
  379. .resume = gxfb_resume,
  380. #endif
  381. };
  382. #ifndef MODULE
  383. static int __init gxfb_setup(char *options)
  384. {
  385. char *opt;
  386. if (!options || !*options)
  387. return 0;
  388. while ((opt = strsep(&options, ",")) != NULL) {
  389. if (!*opt)
  390. continue;
  391. mode_option = opt;
  392. }
  393. return 0;
  394. }
  395. #endif
  396. static int __init gxfb_init(void)
  397. {
  398. #ifndef MODULE
  399. char *option = NULL;
  400. if (fb_get_options("gxfb", &option))
  401. return -ENODEV;
  402. gxfb_setup(option);
  403. #endif
  404. return pci_register_driver(&gxfb_driver);
  405. }
  406. static void __exit gxfb_cleanup(void)
  407. {
  408. pci_unregister_driver(&gxfb_driver);
  409. }
  410. module_init(gxfb_init);
  411. module_exit(gxfb_cleanup);
  412. module_param(mode_option, charp, 0);
  413. MODULE_PARM_DESC(mode_option, "video mode (<x>x<y>[-<bpp>][@<refr>])");
  414. module_param(vram, int, 0);
  415. MODULE_PARM_DESC(vram, "video memory size");
  416. MODULE_DESCRIPTION("Framebuffer driver for the AMD Geode GX");
  417. MODULE_LICENSE("GPL");