xilinxfb.c 14 KB

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