xilinxfb.c 14 KB

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