xilinxfb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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_start;
  109. unsigned int dcr_len;
  110. void *fb_virt; /* virt. address of the frame buffer */
  111. dma_addr_t fb_phys; /* phys. address of the frame buffer */
  112. int fb_alloced; /* Flag, was the fb memory alloced? */
  113. u8 flags; /* features of the driver */
  114. u32 reg_ctrl_default;
  115. u32 pseudo_palette[PALETTE_ENTRIES_NO];
  116. /* Fake palette of 16 colors */
  117. };
  118. #define to_xilinxfb_drvdata(_info) \
  119. container_of(_info, struct xilinxfb_drvdata, info)
  120. /*
  121. * The XPS TFT Controller can be accessed through PLB or DCR interface.
  122. * To perform the read/write on the registers we need to check on
  123. * which bus its connected and call the appropriate write API.
  124. */
  125. static void xilinx_fb_out_be32(struct xilinxfb_drvdata *drvdata, u32 offset,
  126. u32 val)
  127. {
  128. if (drvdata->flags & PLB_ACCESS_FLAG)
  129. out_be32(drvdata->regs + (offset << 2), val);
  130. else
  131. dcr_write(drvdata->dcr_host, offset, val);
  132. }
  133. static int
  134. xilinx_fb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue,
  135. unsigned transp, struct fb_info *fbi)
  136. {
  137. u32 *palette = fbi->pseudo_palette;
  138. if (regno >= PALETTE_ENTRIES_NO)
  139. return -EINVAL;
  140. if (fbi->var.grayscale) {
  141. /* Convert color to grayscale.
  142. * grayscale = 0.30*R + 0.59*G + 0.11*B */
  143. red = green = blue =
  144. (red * 77 + green * 151 + blue * 28 + 127) >> 8;
  145. }
  146. /* fbi->fix.visual is always FB_VISUAL_TRUECOLOR */
  147. /* We only handle 8 bits of each color. */
  148. red >>= 8;
  149. green >>= 8;
  150. blue >>= 8;
  151. palette[regno] = (red << RED_SHIFT) | (green << GREEN_SHIFT) |
  152. (blue << BLUE_SHIFT);
  153. return 0;
  154. }
  155. static int
  156. xilinx_fb_blank(int blank_mode, struct fb_info *fbi)
  157. {
  158. struct xilinxfb_drvdata *drvdata = to_xilinxfb_drvdata(fbi);
  159. switch (blank_mode) {
  160. case FB_BLANK_UNBLANK:
  161. /* turn on panel */
  162. xilinx_fb_out_be32(drvdata, REG_CTRL, drvdata->reg_ctrl_default);
  163. break;
  164. case FB_BLANK_NORMAL:
  165. case FB_BLANK_VSYNC_SUSPEND:
  166. case FB_BLANK_HSYNC_SUSPEND:
  167. case FB_BLANK_POWERDOWN:
  168. /* turn off panel */
  169. xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
  170. default:
  171. break;
  172. }
  173. return 0; /* success */
  174. }
  175. static struct fb_ops xilinxfb_ops =
  176. {
  177. .owner = THIS_MODULE,
  178. .fb_setcolreg = xilinx_fb_setcolreg,
  179. .fb_blank = xilinx_fb_blank,
  180. .fb_fillrect = cfb_fillrect,
  181. .fb_copyarea = cfb_copyarea,
  182. .fb_imageblit = cfb_imageblit,
  183. };
  184. /* ---------------------------------------------------------------------
  185. * Bus independent setup/teardown
  186. */
  187. static int xilinxfb_assign(struct device *dev,
  188. struct xilinxfb_drvdata *drvdata,
  189. unsigned long physaddr,
  190. struct xilinxfb_platform_data *pdata)
  191. {
  192. int rc;
  193. int fbsize = pdata->xvirt * pdata->yvirt * BYTES_PER_PIXEL;
  194. if (drvdata->flags & PLB_ACCESS_FLAG) {
  195. /*
  196. * Map the control registers in if the controller
  197. * is on direct PLB interface.
  198. */
  199. if (!request_mem_region(physaddr, 8, DRIVER_NAME)) {
  200. dev_err(dev, "Couldn't lock memory region at 0x%08lX\n",
  201. physaddr);
  202. rc = -ENODEV;
  203. goto err_region;
  204. }
  205. drvdata->regs_phys = physaddr;
  206. drvdata->regs = ioremap(physaddr, 8);
  207. if (!drvdata->regs) {
  208. dev_err(dev, "Couldn't lock memory region at 0x%08lX\n",
  209. physaddr);
  210. rc = -ENODEV;
  211. goto err_map;
  212. }
  213. }
  214. /* Allocate the framebuffer memory */
  215. if (pdata->fb_phys) {
  216. drvdata->fb_phys = pdata->fb_phys;
  217. drvdata->fb_virt = ioremap(pdata->fb_phys, fbsize);
  218. } else {
  219. drvdata->fb_alloced = 1;
  220. drvdata->fb_virt = dma_alloc_coherent(dev, PAGE_ALIGN(fbsize),
  221. &drvdata->fb_phys, GFP_KERNEL);
  222. }
  223. if (!drvdata->fb_virt) {
  224. dev_err(dev, "Could not allocate frame buffer memory\n");
  225. rc = -ENOMEM;
  226. if (drvdata->flags & PLB_ACCESS_FLAG)
  227. goto err_fbmem;
  228. else
  229. goto err_region;
  230. }
  231. /* Clear (turn to black) the framebuffer */
  232. memset_io((void __iomem *)drvdata->fb_virt, 0, fbsize);
  233. /* Tell the hardware where the frame buffer is */
  234. xilinx_fb_out_be32(drvdata, REG_FB_ADDR, drvdata->fb_phys);
  235. /* Turn on the display */
  236. drvdata->reg_ctrl_default = REG_CTRL_ENABLE;
  237. if (pdata->rotate_screen)
  238. drvdata->reg_ctrl_default |= REG_CTRL_ROTATE;
  239. xilinx_fb_out_be32(drvdata, REG_CTRL,
  240. drvdata->reg_ctrl_default);
  241. /* Fill struct fb_info */
  242. drvdata->info.device = dev;
  243. drvdata->info.screen_base = (void __iomem *)drvdata->fb_virt;
  244. drvdata->info.fbops = &xilinxfb_ops;
  245. drvdata->info.fix = xilinx_fb_fix;
  246. drvdata->info.fix.smem_start = drvdata->fb_phys;
  247. drvdata->info.fix.smem_len = fbsize;
  248. drvdata->info.fix.line_length = pdata->xvirt * BYTES_PER_PIXEL;
  249. drvdata->info.pseudo_palette = drvdata->pseudo_palette;
  250. drvdata->info.flags = FBINFO_DEFAULT;
  251. drvdata->info.var = xilinx_fb_var;
  252. drvdata->info.var.height = pdata->screen_height_mm;
  253. drvdata->info.var.width = pdata->screen_width_mm;
  254. drvdata->info.var.xres = pdata->xres;
  255. drvdata->info.var.yres = pdata->yres;
  256. drvdata->info.var.xres_virtual = pdata->xvirt;
  257. drvdata->info.var.yres_virtual = pdata->yvirt;
  258. /* Allocate a colour map */
  259. rc = fb_alloc_cmap(&drvdata->info.cmap, PALETTE_ENTRIES_NO, 0);
  260. if (rc) {
  261. dev_err(dev, "Fail to allocate colormap (%d entries)\n",
  262. PALETTE_ENTRIES_NO);
  263. goto err_cmap;
  264. }
  265. /* Register new frame buffer */
  266. rc = register_framebuffer(&drvdata->info);
  267. if (rc) {
  268. dev_err(dev, "Could not register frame buffer\n");
  269. goto err_regfb;
  270. }
  271. if (drvdata->flags & PLB_ACCESS_FLAG) {
  272. /* Put a banner in the log (for DEBUG) */
  273. dev_dbg(dev, "regs: phys=%lx, virt=%p\n", physaddr,
  274. drvdata->regs);
  275. }
  276. /* Put a banner in the log (for DEBUG) */
  277. dev_dbg(dev, "fb: phys=%p, virt=%p, size=%x\n",
  278. (void *)drvdata->fb_phys, drvdata->fb_virt, fbsize);
  279. return 0; /* success */
  280. err_regfb:
  281. fb_dealloc_cmap(&drvdata->info.cmap);
  282. err_cmap:
  283. if (drvdata->fb_alloced)
  284. dma_free_coherent(dev, PAGE_ALIGN(fbsize), drvdata->fb_virt,
  285. drvdata->fb_phys);
  286. else
  287. iounmap(drvdata->fb_virt);
  288. /* Turn off the display */
  289. xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
  290. err_fbmem:
  291. if (drvdata->flags & PLB_ACCESS_FLAG)
  292. iounmap(drvdata->regs);
  293. err_map:
  294. if (drvdata->flags & PLB_ACCESS_FLAG)
  295. release_mem_region(physaddr, 8);
  296. err_region:
  297. kfree(drvdata);
  298. dev_set_drvdata(dev, NULL);
  299. return rc;
  300. }
  301. static int xilinxfb_release(struct device *dev)
  302. {
  303. struct xilinxfb_drvdata *drvdata = dev_get_drvdata(dev);
  304. #if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
  305. xilinx_fb_blank(VESA_POWERDOWN, &drvdata->info);
  306. #endif
  307. unregister_framebuffer(&drvdata->info);
  308. fb_dealloc_cmap(&drvdata->info.cmap);
  309. if (drvdata->fb_alloced)
  310. dma_free_coherent(dev, PAGE_ALIGN(drvdata->info.fix.smem_len),
  311. drvdata->fb_virt, drvdata->fb_phys);
  312. else
  313. iounmap(drvdata->fb_virt);
  314. /* Turn off the display */
  315. xilinx_fb_out_be32(drvdata, REG_CTRL, 0);
  316. /* Release the resources, as allocated based on interface */
  317. if (drvdata->flags & PLB_ACCESS_FLAG) {
  318. iounmap(drvdata->regs);
  319. release_mem_region(drvdata->regs_phys, 8);
  320. } else
  321. dcr_unmap(drvdata->dcr_host, drvdata->dcr_len);
  322. kfree(drvdata);
  323. dev_set_drvdata(dev, NULL);
  324. return 0;
  325. }
  326. /* ---------------------------------------------------------------------
  327. * OF bus binding
  328. */
  329. static int __devinit
  330. xilinxfb_of_probe(struct of_device *op, const struct of_device_id *match)
  331. {
  332. const u32 *prop;
  333. u32 *p;
  334. u32 tft_access;
  335. struct xilinxfb_platform_data pdata;
  336. struct resource res;
  337. int size, rc;
  338. int start = 0, len = 0;
  339. dcr_host_t dcr_host;
  340. struct xilinxfb_drvdata *drvdata;
  341. /* Copy with the default pdata (not a ptr reference!) */
  342. pdata = xilinx_fb_default_pdata;
  343. dev_dbg(&op->dev, "xilinxfb_of_probe(%p, %p)\n", op, match);
  344. /*
  345. * To check whether the core is connected directly to DCR or PLB
  346. * interface and initialize the tft_access accordingly.
  347. */
  348. p = (u32 *)of_get_property(op->node, "xlnx,dcr-splb-slave-if", NULL);
  349. if (p)
  350. tft_access = *p;
  351. else
  352. tft_access = 0; /* For backward compatibility */
  353. /*
  354. * Fill the resource structure if its direct PLB interface
  355. * otherwise fill the dcr_host structure.
  356. */
  357. if (tft_access) {
  358. rc = of_address_to_resource(op->node, 0, &res);
  359. if (rc) {
  360. dev_err(&op->dev, "invalid address\n");
  361. return -ENODEV;
  362. }
  363. } else {
  364. start = dcr_resource_start(op->node, 0);
  365. len = dcr_resource_len(op->node, 0);
  366. dcr_host = dcr_map(op->node, start, len);
  367. if (!DCR_MAP_OK(dcr_host)) {
  368. dev_err(&op->dev, "invalid address\n");
  369. return -ENODEV;
  370. }
  371. }
  372. prop = of_get_property(op->node, "phys-size", &size);
  373. if ((prop) && (size >= sizeof(u32)*2)) {
  374. pdata.screen_width_mm = prop[0];
  375. pdata.screen_height_mm = prop[1];
  376. }
  377. prop = of_get_property(op->node, "resolution", &size);
  378. if ((prop) && (size >= sizeof(u32)*2)) {
  379. pdata.xres = prop[0];
  380. pdata.yres = prop[1];
  381. }
  382. prop = of_get_property(op->node, "virtual-resolution", &size);
  383. if ((prop) && (size >= sizeof(u32)*2)) {
  384. pdata.xvirt = prop[0];
  385. pdata.yvirt = prop[1];
  386. }
  387. if (of_find_property(op->node, "rotate-display", NULL))
  388. pdata.rotate_screen = 1;
  389. /* Allocate the driver data region */
  390. drvdata = kzalloc(sizeof(*drvdata), GFP_KERNEL);
  391. if (!drvdata) {
  392. dev_err(&op->dev, "Couldn't allocate device private record\n");
  393. return -ENOMEM;
  394. }
  395. dev_set_drvdata(&op->dev, drvdata);
  396. if (tft_access)
  397. drvdata->flags |= PLB_ACCESS_FLAG;
  398. /* Arguments are passed based on the interface */
  399. if (drvdata->flags & PLB_ACCESS_FLAG) {
  400. return xilinxfb_assign(&op->dev, drvdata, res.start, &pdata);
  401. } else {
  402. drvdata->dcr_start = start;
  403. drvdata->dcr_len = len;
  404. drvdata->dcr_host = dcr_host;
  405. return xilinxfb_assign(&op->dev, drvdata, 0, &pdata);
  406. }
  407. }
  408. static int __devexit xilinxfb_of_remove(struct of_device *op)
  409. {
  410. return xilinxfb_release(&op->dev);
  411. }
  412. /* Match table for of_platform binding */
  413. static struct of_device_id xilinxfb_of_match[] __devinitdata = {
  414. { .compatible = "xlnx,xps-tft-1.00.a", },
  415. { .compatible = "xlnx,plb-tft-cntlr-ref-1.00.a", },
  416. { .compatible = "xlnx,plb-dvi-cntlr-ref-1.00.c", },
  417. {},
  418. };
  419. MODULE_DEVICE_TABLE(of, xilinxfb_of_match);
  420. static struct of_platform_driver xilinxfb_of_driver = {
  421. .owner = THIS_MODULE,
  422. .name = DRIVER_NAME,
  423. .match_table = xilinxfb_of_match,
  424. .probe = xilinxfb_of_probe,
  425. .remove = __devexit_p(xilinxfb_of_remove),
  426. .driver = {
  427. .name = DRIVER_NAME,
  428. },
  429. };
  430. /* ---------------------------------------------------------------------
  431. * Module setup and teardown
  432. */
  433. static int __init
  434. xilinxfb_init(void)
  435. {
  436. return of_register_platform_driver(&xilinxfb_of_driver);
  437. }
  438. static void __exit
  439. xilinxfb_cleanup(void)
  440. {
  441. of_unregister_platform_driver(&xilinxfb_of_driver);
  442. }
  443. module_init(xilinxfb_init);
  444. module_exit(xilinxfb_cleanup);
  445. MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
  446. MODULE_DESCRIPTION("Xilinx TFT frame buffer driver");
  447. MODULE_LICENSE("GPL");