xilinxfb.c 14 KB

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