xilinxfb.c 14 KB

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