xilinxfb.c 14 KB

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