xilinxfb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * Xilinx TFT frame buffer driver
  3. *
  4. * Author: MontaVista Software, Inc.
  5. * source@mvista.com
  6. *
  7. * 2002-2007 (c) MontaVista Software, Inc.
  8. * 2007 (c) Secret Lab Technologies, Ltd.
  9. * 2009 (c) Xilinx Inc.
  10. *
  11. * This file is licensed under the terms of the GNU General Public License
  12. * version 2. This program is licensed "as is" without any warranty of any
  13. * kind, whether express or implied.
  14. */
  15. /*
  16. * This driver was based on au1100fb.c by MontaVista rewritten for 2.6
  17. * by Embedded Alley Solutions <source@embeddedalley.com>, which in turn
  18. * was based on skeletonfb.c, Skeleton for a frame buffer device by
  19. * Geert Uytterhoeven.
  20. */
  21. #include <linux/device.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/version.h>
  25. #include <linux/errno.h>
  26. #include <linux/string.h>
  27. #include <linux/mm.h>
  28. #include <linux/fb.h>
  29. #include <linux/init.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/of_device.h>
  32. #include <linux/of_platform.h>
  33. #include <linux/io.h>
  34. #include <linux/xilinxfb.h>
  35. #include <asm/dcr.h>
  36. #define DRIVER_NAME "xilinxfb"
  37. /*
  38. * Xilinx calls it "PLB TFT LCD Controller" though it can also be used for
  39. * the VGA port on the Xilinx ML40x board. This is a hardware display
  40. * controller for a 640x480 resolution TFT or VGA screen.
  41. *
  42. * The interface to the framebuffer is nice and simple. There are two
  43. * control registers. The first tells the LCD interface where in memory
  44. * the frame buffer is (only the 11 most significant bits are used, so
  45. * don't start thinking about scrolling). The second allows the LCD to
  46. * be turned on or off as well as rotated 180 degrees.
  47. *
  48. * In case of direct PLB access the second control register will be at
  49. * an offset of 4 as compared to the DCR access where the offset is 1
  50. * i.e. REG_CTRL. So this is taken care in the function
  51. * xilinx_fb_out_be32 where it left shifts the offset 2 times in case of
  52. * direct PLB access.
  53. */
  54. #define NUM_REGS 2
  55. #define REG_FB_ADDR 0
  56. #define REG_CTRL 1
  57. #define REG_CTRL_ENABLE 0x0001
  58. #define REG_CTRL_ROTATE 0x0002
  59. /*
  60. * The hardware only handles a single mode: 640x480 24 bit true
  61. * color. Each pixel gets a word (32 bits) of memory. Within each word,
  62. * the 8 most significant bits are ignored, the next 8 bits are the red
  63. * level, the next 8 bits are the green level and the 8 least
  64. * significant bits are the blue level. Each row of the LCD uses 1024
  65. * words, but only the first 640 pixels are displayed with the other 384
  66. * words being ignored. There are 480 rows.
  67. */
  68. #define BYTES_PER_PIXEL 4
  69. #define BITS_PER_PIXEL (BYTES_PER_PIXEL * 8)
  70. #define RED_SHIFT 16
  71. #define GREEN_SHIFT 8
  72. #define BLUE_SHIFT 0
  73. #define PALETTE_ENTRIES_NO 16 /* passed to fb_alloc_cmap() */
  74. /*
  75. * Default xilinxfb configuration
  76. */
  77. static struct xilinxfb_platform_data xilinx_fb_default_pdata = {
  78. .xres = 640,
  79. .yres = 480,
  80. .xvirt = 1024,
  81. .yvirt = 480,
  82. };
  83. /*
  84. * Here are the default fb_fix_screeninfo and fb_var_screeninfo structures
  85. */
  86. static struct fb_fix_screeninfo xilinx_fb_fix = {
  87. .id = "Xilinx",
  88. .type = FB_TYPE_PACKED_PIXELS,
  89. .visual = FB_VISUAL_TRUECOLOR,
  90. .accel = FB_ACCEL_NONE
  91. };
  92. static struct fb_var_screeninfo xilinx_fb_var = {
  93. .bits_per_pixel = BITS_PER_PIXEL,
  94. .red = { RED_SHIFT, 8, 0 },
  95. .green = { GREEN_SHIFT, 8, 0 },
  96. .blue = { BLUE_SHIFT, 8, 0 },
  97. .transp = { 0, 0, 0 },
  98. .activate = FB_ACTIVATE_NOW
  99. };
  100. #define PLB_ACCESS_FLAG 0x1 /* 1 = PLB, 0 = DCR */
  101. struct xilinxfb_drvdata {
  102. struct fb_info info; /* FB driver info record */
  103. phys_addr_t regs_phys; /* phys. address of the control
  104. registers */
  105. void __iomem *regs; /* virt. address of the control
  106. registers */
  107. dcr_host_t dcr_host;
  108. unsigned int dcr_len;
  109. void *fb_virt; /* virt. address of the frame buffer */
  110. dma_addr_t fb_phys; /* phys. address of the frame buffer */
  111. int fb_alloced; /* Flag, was the fb memory alloced? */
  112. u8 flags; /* features of the driver */
  113. u32 reg_ctrl_default;
  114. u32 pseudo_palette[PALETTE_ENTRIES_NO];
  115. /* Fake palette of 16 colors */
  116. };
  117. #define to_xilinxfb_drvdata(_info) \
  118. container_of(_info, struct xilinxfb_drvdata, info)
  119. /*
  120. * The XPS TFT Controller can be accessed through PLB or DCR interface.
  121. * To perform the read/write on the registers we need to check on
  122. * which bus its connected and call the appropriate write API.
  123. */
  124. static void xilinx_fb_out_be32(struct xilinxfb_drvdata *drvdata, u32 offset,
  125. u32 val)
  126. {
  127. if (drvdata->flags & PLB_ACCESS_FLAG)
  128. out_be32(drvdata->regs + (offset << 2), val);
  129. else
  130. dcr_write(drvdata->dcr_host, offset, val);
  131. }
  132. static int
  133. xilinx_fb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue,
  134. unsigned transp, struct fb_info *fbi)
  135. {
  136. u32 *palette = fbi->pseudo_palette;
  137. if (regno >= PALETTE_ENTRIES_NO)
  138. return -EINVAL;
  139. if (fbi->var.grayscale) {
  140. /* Convert color to grayscale.
  141. * grayscale = 0.30*R + 0.59*G + 0.11*B */
  142. red = green = blue =
  143. (red * 77 + green * 151 + blue * 28 + 127) >> 8;
  144. }
  145. /* fbi->fix.visual is always FB_VISUAL_TRUECOLOR */
  146. /* We only handle 8 bits of each color. */
  147. red >>= 8;
  148. green >>= 8;
  149. blue >>= 8;
  150. palette[regno] = (red << RED_SHIFT) | (green << GREEN_SHIFT) |
  151. (blue << BLUE_SHIFT);
  152. return 0;
  153. }
  154. static int
  155. xilinx_fb_blank(int blank_mode, struct fb_info *fbi)
  156. {
  157. struct xilinxfb_drvdata *drvdata = to_xilinxfb_drvdata(fbi);
  158. switch (blank_mode) {
  159. case FB_BLANK_UNBLANK:
  160. /* turn on panel */
  161. xilinx_fb_out_be32(drvdata, REG_CTRL, drvdata->reg_ctrl_default);
  162. break;
  163. case FB_BLANK_NORMAL:
  164. case FB_BLANK_VSYNC_SUSPEND:
  165. case FB_BLANK_HSYNC_SUSPEND:
  166. case FB_BLANK_POWERDOWN:
  167. /* turn off panel */
  168. xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
  169. default:
  170. break;
  171. }
  172. return 0; /* success */
  173. }
  174. static struct fb_ops xilinxfb_ops =
  175. {
  176. .owner = THIS_MODULE,
  177. .fb_setcolreg = xilinx_fb_setcolreg,
  178. .fb_blank = xilinx_fb_blank,
  179. .fb_fillrect = cfb_fillrect,
  180. .fb_copyarea = cfb_copyarea,
  181. .fb_imageblit = cfb_imageblit,
  182. };
  183. /* ---------------------------------------------------------------------
  184. * Bus independent setup/teardown
  185. */
  186. static int xilinxfb_assign(struct device *dev,
  187. struct xilinxfb_drvdata *drvdata,
  188. unsigned long physaddr,
  189. struct xilinxfb_platform_data *pdata)
  190. {
  191. int rc;
  192. int fbsize = pdata->xvirt * pdata->yvirt * BYTES_PER_PIXEL;
  193. if (drvdata->flags & PLB_ACCESS_FLAG) {
  194. /*
  195. * Map the control registers in if the controller
  196. * is on direct PLB interface.
  197. */
  198. if (!request_mem_region(physaddr, 8, DRIVER_NAME)) {
  199. dev_err(dev, "Couldn't lock memory region at 0x%08lX\n",
  200. physaddr);
  201. rc = -ENODEV;
  202. goto err_region;
  203. }
  204. drvdata->regs_phys = physaddr;
  205. drvdata->regs = ioremap(physaddr, 8);
  206. if (!drvdata->regs) {
  207. dev_err(dev, "Couldn't lock memory region at 0x%08lX\n",
  208. physaddr);
  209. rc = -ENODEV;
  210. goto err_map;
  211. }
  212. }
  213. /* Allocate the framebuffer memory */
  214. if (pdata->fb_phys) {
  215. drvdata->fb_phys = pdata->fb_phys;
  216. drvdata->fb_virt = ioremap(pdata->fb_phys, fbsize);
  217. } else {
  218. drvdata->fb_alloced = 1;
  219. drvdata->fb_virt = dma_alloc_coherent(dev, PAGE_ALIGN(fbsize),
  220. &drvdata->fb_phys, GFP_KERNEL);
  221. }
  222. if (!drvdata->fb_virt) {
  223. dev_err(dev, "Could not allocate frame buffer memory\n");
  224. rc = -ENOMEM;
  225. if (drvdata->flags & PLB_ACCESS_FLAG)
  226. goto err_fbmem;
  227. else
  228. goto err_region;
  229. }
  230. /* Clear (turn to black) the framebuffer */
  231. memset_io((void __iomem *)drvdata->fb_virt, 0, fbsize);
  232. /* Tell the hardware where the frame buffer is */
  233. xilinx_fb_out_be32(drvdata, REG_FB_ADDR, drvdata->fb_phys);
  234. /* Turn on the display */
  235. drvdata->reg_ctrl_default = REG_CTRL_ENABLE;
  236. if (pdata->rotate_screen)
  237. drvdata->reg_ctrl_default |= REG_CTRL_ROTATE;
  238. xilinx_fb_out_be32(drvdata, REG_CTRL,
  239. drvdata->reg_ctrl_default);
  240. /* Fill struct fb_info */
  241. drvdata->info.device = dev;
  242. drvdata->info.screen_base = (void __iomem *)drvdata->fb_virt;
  243. drvdata->info.fbops = &xilinxfb_ops;
  244. drvdata->info.fix = xilinx_fb_fix;
  245. drvdata->info.fix.smem_start = drvdata->fb_phys;
  246. drvdata->info.fix.smem_len = fbsize;
  247. drvdata->info.fix.line_length = pdata->xvirt * BYTES_PER_PIXEL;
  248. drvdata->info.pseudo_palette = drvdata->pseudo_palette;
  249. drvdata->info.flags = FBINFO_DEFAULT;
  250. drvdata->info.var = xilinx_fb_var;
  251. drvdata->info.var.height = pdata->screen_height_mm;
  252. drvdata->info.var.width = pdata->screen_width_mm;
  253. drvdata->info.var.xres = pdata->xres;
  254. drvdata->info.var.yres = pdata->yres;
  255. drvdata->info.var.xres_virtual = pdata->xvirt;
  256. drvdata->info.var.yres_virtual = pdata->yvirt;
  257. /* Allocate a colour map */
  258. rc = fb_alloc_cmap(&drvdata->info.cmap, PALETTE_ENTRIES_NO, 0);
  259. if (rc) {
  260. dev_err(dev, "Fail to allocate colormap (%d entries)\n",
  261. PALETTE_ENTRIES_NO);
  262. goto err_cmap;
  263. }
  264. /* Register new frame buffer */
  265. rc = register_framebuffer(&drvdata->info);
  266. if (rc) {
  267. dev_err(dev, "Could not register frame buffer\n");
  268. goto err_regfb;
  269. }
  270. if (drvdata->flags & PLB_ACCESS_FLAG) {
  271. /* Put a banner in the log (for DEBUG) */
  272. dev_dbg(dev, "regs: phys=%lx, virt=%p\n", physaddr,
  273. drvdata->regs);
  274. }
  275. /* Put a banner in the log (for DEBUG) */
  276. dev_dbg(dev, "fb: phys=%llx, virt=%p, size=%x\n",
  277. (unsigned long long)drvdata->fb_phys, drvdata->fb_virt, fbsize);
  278. return 0; /* success */
  279. err_regfb:
  280. fb_dealloc_cmap(&drvdata->info.cmap);
  281. err_cmap:
  282. if (drvdata->fb_alloced)
  283. dma_free_coherent(dev, PAGE_ALIGN(fbsize), drvdata->fb_virt,
  284. drvdata->fb_phys);
  285. else
  286. iounmap(drvdata->fb_virt);
  287. /* Turn off the display */
  288. xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
  289. err_fbmem:
  290. if (drvdata->flags & PLB_ACCESS_FLAG)
  291. iounmap(drvdata->regs);
  292. err_map:
  293. if (drvdata->flags & PLB_ACCESS_FLAG)
  294. release_mem_region(physaddr, 8);
  295. err_region:
  296. kfree(drvdata);
  297. dev_set_drvdata(dev, NULL);
  298. return rc;
  299. }
  300. static int xilinxfb_release(struct device *dev)
  301. {
  302. struct xilinxfb_drvdata *drvdata = dev_get_drvdata(dev);
  303. #if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
  304. xilinx_fb_blank(VESA_POWERDOWN, &drvdata->info);
  305. #endif
  306. unregister_framebuffer(&drvdata->info);
  307. fb_dealloc_cmap(&drvdata->info.cmap);
  308. if (drvdata->fb_alloced)
  309. dma_free_coherent(dev, PAGE_ALIGN(drvdata->info.fix.smem_len),
  310. drvdata->fb_virt, drvdata->fb_phys);
  311. else
  312. iounmap(drvdata->fb_virt);
  313. /* Turn off the display */
  314. xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
  315. /* Release the resources, as allocated based on interface */
  316. if (drvdata->flags & PLB_ACCESS_FLAG) {
  317. iounmap(drvdata->regs);
  318. release_mem_region(drvdata->regs_phys, 8);
  319. } else
  320. dcr_unmap(drvdata->dcr_host, drvdata->dcr_len);
  321. kfree(drvdata);
  322. dev_set_drvdata(dev, NULL);
  323. return 0;
  324. }
  325. /* ---------------------------------------------------------------------
  326. * OF bus binding
  327. */
  328. static int __devinit
  329. xilinxfb_of_probe(struct of_device *op, const struct of_device_id *match)
  330. {
  331. const u32 *prop;
  332. u32 *p;
  333. u32 tft_access;
  334. struct xilinxfb_platform_data pdata;
  335. struct resource res;
  336. int size, rc, start;
  337. struct xilinxfb_drvdata *drvdata;
  338. /* Copy with the default pdata (not a ptr reference!) */
  339. pdata = xilinx_fb_default_pdata;
  340. dev_dbg(&op->dev, "xilinxfb_of_probe(%p, %p)\n", op, match);
  341. /* Allocate the driver data region */
  342. drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
  343. if (!drvdata) {
  344. dev_err(&op->dev, "Couldn't allocate device private record\n");
  345. return -ENOMEM;
  346. }
  347. /*
  348. * To check whether the core is connected directly to DCR or PLB
  349. * interface and initialize the tft_access accordingly.
  350. */
  351. p = (u32 *)of_get_property(op->node, "xlnx,dcr-splb-slave-if", NULL);
  352. tft_access = p ? *p : 0;
  353. /*
  354. * Fill the resource structure if its direct PLB interface
  355. * otherwise fill the dcr_host structure.
  356. */
  357. if (tft_access) {
  358. drvdata->flags |= PLB_ACCESS_FLAG;
  359. rc = of_address_to_resource(op->node, 0, &res);
  360. if (rc) {
  361. dev_err(&op->dev, "invalid address\n");
  362. goto err;
  363. }
  364. } else {
  365. res.start = 0;
  366. start = dcr_resource_start(op->node, 0);
  367. drvdata->dcr_len = dcr_resource_len(op->node, 0);
  368. drvdata->dcr_host = dcr_map(op->node, start, drvdata->dcr_len);
  369. if (!DCR_MAP_OK(drvdata->dcr_host)) {
  370. dev_err(&op->dev, "invalid DCR address\n");
  371. goto err;
  372. }
  373. }
  374. prop = of_get_property(op->node, "phys-size", &size);
  375. if ((prop) && (size >= sizeof(u32)*2)) {
  376. pdata.screen_width_mm = prop[0];
  377. pdata.screen_height_mm = prop[1];
  378. }
  379. prop = of_get_property(op->node, "resolution", &size);
  380. if ((prop) && (size >= sizeof(u32)*2)) {
  381. pdata.xres = prop[0];
  382. pdata.yres = prop[1];
  383. }
  384. prop = of_get_property(op->node, "virtual-resolution", &size);
  385. if ((prop) && (size >= sizeof(u32)*2)) {
  386. pdata.xvirt = prop[0];
  387. pdata.yvirt = prop[1];
  388. }
  389. if (of_find_property(op->node, "rotate-display", NULL))
  390. pdata.rotate_screen = 1;
  391. dev_set_drvdata(&op->dev, drvdata);
  392. return xilinxfb_assign(&op->dev, drvdata, res.start, &pdata);
  393. err:
  394. kfree(drvdata);
  395. return -ENODEV;
  396. }
  397. static int __devexit xilinxfb_of_remove(struct of_device *op)
  398. {
  399. return xilinxfb_release(&op->dev);
  400. }
  401. /* Match table for of_platform binding */
  402. static struct of_device_id xilinxfb_of_match[] __devinitdata = {
  403. { .compatible = "xlnx,xps-tft-1.00.a", },
  404. { .compatible = "xlnx,plb-tft-cntlr-ref-1.00.a", },
  405. { .compatible = "xlnx,plb-dvi-cntlr-ref-1.00.c", },
  406. {},
  407. };
  408. MODULE_DEVICE_TABLE(of, xilinxfb_of_match);
  409. static struct of_platform_driver xilinxfb_of_driver = {
  410. .owner = THIS_MODULE,
  411. .name = DRIVER_NAME,
  412. .match_table = xilinxfb_of_match,
  413. .probe = xilinxfb_of_probe,
  414. .remove = __devexit_p(xilinxfb_of_remove),
  415. .driver = {
  416. .name = DRIVER_NAME,
  417. },
  418. };
  419. /* ---------------------------------------------------------------------
  420. * Module setup and teardown
  421. */
  422. static int __init
  423. xilinxfb_init(void)
  424. {
  425. return of_register_platform_driver(&xilinxfb_of_driver);
  426. }
  427. static void __exit
  428. xilinxfb_cleanup(void)
  429. {
  430. of_unregister_platform_driver(&xilinxfb_of_driver);
  431. }
  432. module_init(xilinxfb_init);
  433. module_exit(xilinxfb_cleanup);
  434. MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
  435. MODULE_DESCRIPTION("Xilinx TFT frame buffer driver");
  436. MODULE_LICENSE("GPL");