xilinxfb.c 14 KB

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