xilinxfb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Xilinx TFT frame buffer driver
  3. *
  4. * Author: MontaVista Software, Inc.
  5. * [email protected]
  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 <[email protected]>, 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. break;
  212. default:
  213. break;
  214. }
  215. return 0; /* success */
  216. }
  217. static const struct fb_ops xilinxfb_ops = {
  218. .owner = THIS_MODULE,
  219. .fb_setcolreg = xilinx_fb_setcolreg,
  220. .fb_blank = xilinx_fb_blank,
  221. .fb_fillrect = cfb_fillrect,
  222. .fb_copyarea = cfb_copyarea,
  223. .fb_imageblit = cfb_imageblit,
  224. };
  225. /* ---------------------------------------------------------------------
  226. * Bus independent setup/teardown
  227. */
  228. static int xilinxfb_assign(struct platform_device *pdev,
  229. struct xilinxfb_drvdata *drvdata,
  230. struct xilinxfb_platform_data *pdata)
  231. {
  232. int rc;
  233. struct device *dev = &pdev->dev;
  234. int fbsize = pdata->xvirt * pdata->yvirt * BYTES_PER_PIXEL;
  235. if (drvdata->flags & BUS_ACCESS_FLAG) {
  236. struct resource *res;
  237. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  238. drvdata->regs = devm_ioremap_resource(&pdev->dev, res);
  239. if (IS_ERR(drvdata->regs))
  240. return PTR_ERR(drvdata->regs);
  241. drvdata->regs_phys = res->start;
  242. }
  243. /* Allocate the framebuffer memory */
  244. if (pdata->fb_phys) {
  245. drvdata->fb_phys = pdata->fb_phys;
  246. drvdata->fb_virt = ioremap(pdata->fb_phys, fbsize);
  247. } else {
  248. drvdata->fb_alloced = 1;
  249. drvdata->fb_virt = dma_alloc_coherent(dev, PAGE_ALIGN(fbsize),
  250. &drvdata->fb_phys,
  251. GFP_KERNEL);
  252. }
  253. if (!drvdata->fb_virt) {
  254. dev_err(dev, "Could not allocate frame buffer memory\n");
  255. return -ENOMEM;
  256. }
  257. /* Clear (turn to black) the framebuffer */
  258. memset_io((void __iomem *)drvdata->fb_virt, 0, fbsize);
  259. /* Tell the hardware where the frame buffer is */
  260. xilinx_fb_out32(drvdata, REG_FB_ADDR, drvdata->fb_phys);
  261. rc = xilinx_fb_in32(drvdata, REG_FB_ADDR);
  262. /* Endianness detection */
  263. if (rc != drvdata->fb_phys) {
  264. drvdata->flags |= LITTLE_ENDIAN_ACCESS;
  265. xilinx_fb_out32(drvdata, REG_FB_ADDR, drvdata->fb_phys);
  266. }
  267. /* Turn on the display */
  268. drvdata->reg_ctrl_default = REG_CTRL_ENABLE;
  269. if (pdata->rotate_screen)
  270. drvdata->reg_ctrl_default |= REG_CTRL_ROTATE;
  271. xilinx_fb_out32(drvdata, REG_CTRL, drvdata->reg_ctrl_default);
  272. /* Fill struct fb_info */
  273. drvdata->info.device = dev;
  274. drvdata->info.screen_base = (void __iomem *)drvdata->fb_virt;
  275. drvdata->info.fbops = &xilinxfb_ops;
  276. drvdata->info.fix = xilinx_fb_fix;
  277. drvdata->info.fix.smem_start = drvdata->fb_phys;
  278. drvdata->info.fix.smem_len = fbsize;
  279. drvdata->info.fix.line_length = pdata->xvirt * BYTES_PER_PIXEL;
  280. drvdata->info.pseudo_palette = drvdata->pseudo_palette;
  281. drvdata->info.flags = FBINFO_DEFAULT;
  282. drvdata->info.var = xilinx_fb_var;
  283. drvdata->info.var.height = pdata->screen_height_mm;
  284. drvdata->info.var.width = pdata->screen_width_mm;
  285. drvdata->info.var.xres = pdata->xres;
  286. drvdata->info.var.yres = pdata->yres;
  287. drvdata->info.var.xres_virtual = pdata->xvirt;
  288. drvdata->info.var.yres_virtual = pdata->yvirt;
  289. /* Allocate a colour map */
  290. rc = fb_alloc_cmap(&drvdata->info.cmap, PALETTE_ENTRIES_NO, 0);
  291. if (rc) {
  292. dev_err(dev, "Fail to allocate colormap (%d entries)\n",
  293. PALETTE_ENTRIES_NO);
  294. goto err_cmap;
  295. }
  296. /* Register new frame buffer */
  297. rc = register_framebuffer(&drvdata->info);
  298. if (rc) {
  299. dev_err(dev, "Could not register frame buffer\n");
  300. goto err_regfb;
  301. }
  302. if (drvdata->flags & BUS_ACCESS_FLAG) {
  303. /* Put a banner in the log (for DEBUG) */
  304. dev_dbg(dev, "regs: phys=%pa, virt=%p\n",
  305. &drvdata->regs_phys, drvdata->regs);
  306. }
  307. /* Put a banner in the log (for DEBUG) */
  308. dev_dbg(dev, "fb: phys=%llx, virt=%p, size=%x\n",
  309. (unsigned long long)drvdata->fb_phys, drvdata->fb_virt, fbsize);
  310. return 0; /* success */
  311. err_regfb:
  312. fb_dealloc_cmap(&drvdata->info.cmap);
  313. err_cmap:
  314. if (drvdata->fb_alloced)
  315. dma_free_coherent(dev, PAGE_ALIGN(fbsize), drvdata->fb_virt,
  316. drvdata->fb_phys);
  317. else
  318. iounmap(drvdata->fb_virt);
  319. /* Turn off the display */
  320. xilinx_fb_out32(drvdata, REG_CTRL, 0);
  321. return rc;
  322. }
  323. static void xilinxfb_release(struct device *dev)
  324. {
  325. struct xilinxfb_drvdata *drvdata = dev_get_drvdata(dev);
  326. #if !defined(CONFIG_FRAMEBUFFER_CONSOLE) && defined(CONFIG_LOGO)
  327. xilinx_fb_blank(VESA_POWERDOWN, &drvdata->info);
  328. #endif
  329. unregister_framebuffer(&drvdata->info);
  330. fb_dealloc_cmap(&drvdata->info.cmap);
  331. if (drvdata->fb_alloced)
  332. dma_free_coherent(dev, PAGE_ALIGN(drvdata->info.fix.smem_len),
  333. drvdata->fb_virt, drvdata->fb_phys);
  334. else
  335. iounmap(drvdata->fb_virt);
  336. /* Turn off the display */
  337. xilinx_fb_out32(drvdata, REG_CTRL, 0);
  338. #ifdef CONFIG_PPC_DCR
  339. /* Release the resources, as allocated based on interface */
  340. if (!(drvdata->flags & BUS_ACCESS_FLAG))
  341. dcr_unmap(drvdata->dcr_host, drvdata->dcr_len);
  342. #endif
  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. platform_set_drvdata(pdev, drvdata);
  402. return xilinxfb_assign(pdev, drvdata, &pdata);
  403. }
  404. static int xilinxfb_of_remove(struct platform_device *op)
  405. {
  406. xilinxfb_release(&op->dev);
  407. return 0;
  408. }
  409. /* Match table for of_platform binding */
  410. static const struct of_device_id xilinxfb_of_match[] = {
  411. { .compatible = "xlnx,xps-tft-1.00.a", },
  412. { .compatible = "xlnx,xps-tft-2.00.a", },
  413. { .compatible = "xlnx,xps-tft-2.01.a", },
  414. { .compatible = "xlnx,plb-tft-cntlr-ref-1.00.a", },
  415. { .compatible = "xlnx,plb-dvi-cntlr-ref-1.00.c", },
  416. {},
  417. };
  418. MODULE_DEVICE_TABLE(of, xilinxfb_of_match);
  419. static struct platform_driver xilinxfb_of_driver = {
  420. .probe = xilinxfb_of_probe,
  421. .remove = xilinxfb_of_remove,
  422. .driver = {
  423. .name = DRIVER_NAME,
  424. .of_match_table = xilinxfb_of_match,
  425. },
  426. };
  427. module_platform_driver(xilinxfb_of_driver);
  428. MODULE_AUTHOR("MontaVista Software, Inc. <[email protected]>");
  429. MODULE_DESCRIPTION("Xilinx TFT frame buffer driver");
  430. MODULE_LICENSE("GPL");