ocfb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. * OpenCores VGA/LCD 2.0 core frame buffer driver
  3. *
  4. * Copyright (C) 2013 Stefan Kristiansson, stefan.kristiansson@saunalahti.fi
  5. *
  6. * This file is licensed under the terms of the GNU General Public License
  7. * version 2. This program is licensed "as is" without any warranty of any
  8. * kind, whether express or implied.
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/errno.h>
  13. #include <linux/fb.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/string.h>
  22. #include <linux/slab.h>
  23. /* OCFB register defines */
  24. #define OCFB_CTRL 0x000
  25. #define OCFB_STAT 0x004
  26. #define OCFB_HTIM 0x008
  27. #define OCFB_VTIM 0x00c
  28. #define OCFB_HVLEN 0x010
  29. #define OCFB_VBARA 0x014
  30. #define OCFB_PALETTE 0x800
  31. #define OCFB_CTRL_VEN 0x00000001 /* Video Enable */
  32. #define OCFB_CTRL_HIE 0x00000002 /* HSync Interrupt Enable */
  33. #define OCFB_CTRL_PC 0x00000800 /* 8-bit Pseudo Color Enable*/
  34. #define OCFB_CTRL_CD8 0x00000000 /* Color Depth 8 */
  35. #define OCFB_CTRL_CD16 0x00000200 /* Color Depth 16 */
  36. #define OCFB_CTRL_CD24 0x00000400 /* Color Depth 24 */
  37. #define OCFB_CTRL_CD32 0x00000600 /* Color Depth 32 */
  38. #define OCFB_CTRL_VBL1 0x00000000 /* Burst Length 1 */
  39. #define OCFB_CTRL_VBL2 0x00000080 /* Burst Length 2 */
  40. #define OCFB_CTRL_VBL4 0x00000100 /* Burst Length 4 */
  41. #define OCFB_CTRL_VBL8 0x00000180 /* Burst Length 8 */
  42. #define PALETTE_SIZE 256
  43. #define OCFB_NAME "OC VGA/LCD"
  44. static char *mode_option;
  45. static const struct fb_videomode default_mode = {
  46. /* 640x480 @ 60 Hz, 31.5 kHz hsync */
  47. NULL, 60, 640, 480, 39721, 40, 24, 32, 11, 96, 2,
  48. 0, FB_VMODE_NONINTERLACED
  49. };
  50. struct ocfb_dev {
  51. struct fb_info info;
  52. void __iomem *regs;
  53. /* flag indicating whether the regs are little endian accessed */
  54. int little_endian;
  55. /* Physical and virtual addresses of framebuffer */
  56. dma_addr_t fb_phys;
  57. void __iomem *fb_virt;
  58. u32 pseudo_palette[PALETTE_SIZE];
  59. };
  60. #ifndef MODULE
  61. static int __init ocfb_setup(char *options)
  62. {
  63. char *curr_opt;
  64. if (!options || !*options)
  65. return 0;
  66. while ((curr_opt = strsep(&options, ",")) != NULL) {
  67. if (!*curr_opt)
  68. continue;
  69. mode_option = curr_opt;
  70. }
  71. return 0;
  72. }
  73. #endif
  74. static inline u32 ocfb_readreg(struct ocfb_dev *fbdev, loff_t offset)
  75. {
  76. if (fbdev->little_endian)
  77. return ioread32(fbdev->regs + offset);
  78. else
  79. return ioread32be(fbdev->regs + offset);
  80. }
  81. static void ocfb_writereg(struct ocfb_dev *fbdev, loff_t offset, u32 data)
  82. {
  83. if (fbdev->little_endian)
  84. iowrite32(data, fbdev->regs + offset);
  85. else
  86. iowrite32be(data, fbdev->regs + offset);
  87. }
  88. static int ocfb_setupfb(struct ocfb_dev *fbdev)
  89. {
  90. unsigned long bpp_config;
  91. struct fb_var_screeninfo *var = &fbdev->info.var;
  92. struct device *dev = fbdev->info.device;
  93. u32 hlen;
  94. u32 vlen;
  95. /* Disable display */
  96. ocfb_writereg(fbdev, OCFB_CTRL, 0);
  97. /* Register framebuffer address */
  98. fbdev->little_endian = 0;
  99. ocfb_writereg(fbdev, OCFB_VBARA, fbdev->fb_phys);
  100. /* Detect endianess */
  101. if (ocfb_readreg(fbdev, OCFB_VBARA) != fbdev->fb_phys) {
  102. fbdev->little_endian = 1;
  103. ocfb_writereg(fbdev, OCFB_VBARA, fbdev->fb_phys);
  104. }
  105. /* Horizontal timings */
  106. ocfb_writereg(fbdev, OCFB_HTIM, (var->hsync_len - 1) << 24 |
  107. (var->right_margin - 1) << 16 | (var->xres - 1));
  108. /* Vertical timings */
  109. ocfb_writereg(fbdev, OCFB_VTIM, (var->vsync_len - 1) << 24 |
  110. (var->lower_margin - 1) << 16 | (var->yres - 1));
  111. /* Total length of frame */
  112. hlen = var->left_margin + var->right_margin + var->hsync_len +
  113. var->xres;
  114. vlen = var->upper_margin + var->lower_margin + var->vsync_len +
  115. var->yres;
  116. ocfb_writereg(fbdev, OCFB_HVLEN, (hlen - 1) << 16 | (vlen - 1));
  117. bpp_config = OCFB_CTRL_CD8;
  118. switch (var->bits_per_pixel) {
  119. case 8:
  120. if (!var->grayscale)
  121. bpp_config |= OCFB_CTRL_PC; /* enable palette */
  122. break;
  123. case 16:
  124. bpp_config |= OCFB_CTRL_CD16;
  125. break;
  126. case 24:
  127. bpp_config |= OCFB_CTRL_CD24;
  128. break;
  129. case 32:
  130. bpp_config |= OCFB_CTRL_CD32;
  131. break;
  132. default:
  133. dev_err(dev, "no bpp specified\n");
  134. break;
  135. }
  136. /* maximum (8) VBL (video memory burst length) */
  137. bpp_config |= OCFB_CTRL_VBL8;
  138. /* Enable output */
  139. ocfb_writereg(fbdev, OCFB_CTRL, (OCFB_CTRL_VEN | bpp_config));
  140. return 0;
  141. }
  142. static int ocfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  143. unsigned blue, unsigned transp,
  144. struct fb_info *info)
  145. {
  146. struct ocfb_dev *fbdev = (struct ocfb_dev *)info->par;
  147. u32 color;
  148. if (regno >= info->cmap.len) {
  149. dev_err(info->device, "regno >= cmap.len\n");
  150. return 1;
  151. }
  152. if (info->var.grayscale) {
  153. /* grayscale = 0.30*R + 0.59*G + 0.11*B */
  154. red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
  155. }
  156. red >>= (16 - info->var.red.length);
  157. green >>= (16 - info->var.green.length);
  158. blue >>= (16 - info->var.blue.length);
  159. transp >>= (16 - info->var.transp.length);
  160. if (info->var.bits_per_pixel == 8 && !info->var.grayscale) {
  161. regno <<= 2;
  162. color = (red << 16) | (green << 8) | blue;
  163. ocfb_writereg(fbdev, OCFB_PALETTE + regno, color);
  164. } else {
  165. ((u32 *)(info->pseudo_palette))[regno] =
  166. (red << info->var.red.offset) |
  167. (green << info->var.green.offset) |
  168. (blue << info->var.blue.offset) |
  169. (transp << info->var.transp.offset);
  170. }
  171. return 0;
  172. }
  173. static int ocfb_init_fix(struct ocfb_dev *fbdev)
  174. {
  175. struct fb_var_screeninfo *var = &fbdev->info.var;
  176. struct fb_fix_screeninfo *fix = &fbdev->info.fix;
  177. strcpy(fix->id, OCFB_NAME);
  178. fix->line_length = var->xres * var->bits_per_pixel/8;
  179. fix->smem_len = fix->line_length * var->yres;
  180. fix->type = FB_TYPE_PACKED_PIXELS;
  181. if (var->bits_per_pixel == 8 && !var->grayscale)
  182. fix->visual = FB_VISUAL_PSEUDOCOLOR;
  183. else
  184. fix->visual = FB_VISUAL_TRUECOLOR;
  185. return 0;
  186. }
  187. static int ocfb_init_var(struct ocfb_dev *fbdev)
  188. {
  189. struct fb_var_screeninfo *var = &fbdev->info.var;
  190. var->accel_flags = FB_ACCEL_NONE;
  191. var->activate = FB_ACTIVATE_NOW;
  192. var->xres_virtual = var->xres;
  193. var->yres_virtual = var->yres;
  194. switch (var->bits_per_pixel) {
  195. case 8:
  196. var->transp.offset = 0;
  197. var->transp.length = 0;
  198. var->red.offset = 0;
  199. var->red.length = 8;
  200. var->green.offset = 0;
  201. var->green.length = 8;
  202. var->blue.offset = 0;
  203. var->blue.length = 8;
  204. break;
  205. case 16:
  206. var->transp.offset = 0;
  207. var->transp.length = 0;
  208. var->red.offset = 11;
  209. var->red.length = 5;
  210. var->green.offset = 5;
  211. var->green.length = 6;
  212. var->blue.offset = 0;
  213. var->blue.length = 5;
  214. break;
  215. case 24:
  216. var->transp.offset = 0;
  217. var->transp.length = 0;
  218. var->red.offset = 16;
  219. var->red.length = 8;
  220. var->green.offset = 8;
  221. var->green.length = 8;
  222. var->blue.offset = 0;
  223. var->blue.length = 8;
  224. break;
  225. case 32:
  226. var->transp.offset = 24;
  227. var->transp.length = 8;
  228. var->red.offset = 16;
  229. var->red.length = 8;
  230. var->green.offset = 8;
  231. var->green.length = 8;
  232. var->blue.offset = 0;
  233. var->blue.length = 8;
  234. break;
  235. }
  236. return 0;
  237. }
  238. static struct fb_ops ocfb_ops = {
  239. .owner = THIS_MODULE,
  240. .fb_setcolreg = ocfb_setcolreg,
  241. .fb_fillrect = cfb_fillrect,
  242. .fb_copyarea = cfb_copyarea,
  243. .fb_imageblit = cfb_imageblit,
  244. };
  245. static int ocfb_probe(struct platform_device *pdev)
  246. {
  247. int ret = 0;
  248. struct ocfb_dev *fbdev;
  249. struct resource *res;
  250. int fbsize;
  251. fbdev = devm_kzalloc(&pdev->dev, sizeof(*fbdev), GFP_KERNEL);
  252. if (!fbdev)
  253. return -ENOMEM;
  254. platform_set_drvdata(pdev, fbdev);
  255. fbdev->info.fbops = &ocfb_ops;
  256. fbdev->info.device = &pdev->dev;
  257. fbdev->info.par = fbdev;
  258. /* Video mode setup */
  259. if (!fb_find_mode(&fbdev->info.var, &fbdev->info, mode_option,
  260. NULL, 0, &default_mode, 16)) {
  261. dev_err(&pdev->dev, "No valid video modes found\n");
  262. return -EINVAL;
  263. }
  264. ocfb_init_var(fbdev);
  265. ocfb_init_fix(fbdev);
  266. /* Request I/O resource */
  267. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  268. if (!res) {
  269. dev_err(&pdev->dev, "I/O resource request failed\n");
  270. return -ENXIO;
  271. }
  272. fbdev->regs = devm_ioremap_resource(&pdev->dev, res);
  273. if (IS_ERR(fbdev->regs))
  274. return PTR_ERR(fbdev->regs);
  275. /* Allocate framebuffer memory */
  276. fbsize = fbdev->info.fix.smem_len;
  277. fbdev->fb_virt = dma_alloc_coherent(&pdev->dev, PAGE_ALIGN(fbsize),
  278. &fbdev->fb_phys, GFP_KERNEL);
  279. if (!fbdev->fb_virt) {
  280. dev_err(&pdev->dev,
  281. "Frame buffer memory allocation failed\n");
  282. return -ENOMEM;
  283. }
  284. fbdev->info.fix.smem_start = fbdev->fb_phys;
  285. fbdev->info.screen_base = fbdev->fb_virt;
  286. fbdev->info.pseudo_palette = fbdev->pseudo_palette;
  287. /* Clear framebuffer */
  288. memset_io(fbdev->fb_virt, 0, fbsize);
  289. /* Setup and enable the framebuffer */
  290. ocfb_setupfb(fbdev);
  291. if (fbdev->little_endian)
  292. fbdev->info.flags |= FBINFO_FOREIGN_ENDIAN;
  293. /* Allocate color map */
  294. ret = fb_alloc_cmap(&fbdev->info.cmap, PALETTE_SIZE, 0);
  295. if (ret) {
  296. dev_err(&pdev->dev, "Color map allocation failed\n");
  297. goto err_dma_free;
  298. }
  299. /* Register framebuffer */
  300. ret = register_framebuffer(&fbdev->info);
  301. if (ret) {
  302. dev_err(&pdev->dev, "Framebuffer registration failed\n");
  303. goto err_dealloc_cmap;
  304. }
  305. return 0;
  306. err_dealloc_cmap:
  307. fb_dealloc_cmap(&fbdev->info.cmap);
  308. err_dma_free:
  309. dma_free_coherent(&pdev->dev, PAGE_ALIGN(fbsize), fbdev->fb_virt,
  310. fbdev->fb_phys);
  311. return ret;
  312. }
  313. static int ocfb_remove(struct platform_device *pdev)
  314. {
  315. struct ocfb_dev *fbdev = platform_get_drvdata(pdev);
  316. unregister_framebuffer(&fbdev->info);
  317. fb_dealloc_cmap(&fbdev->info.cmap);
  318. dma_free_coherent(&pdev->dev, PAGE_ALIGN(fbdev->info.fix.smem_len),
  319. fbdev->fb_virt, fbdev->fb_phys);
  320. /* Disable display */
  321. ocfb_writereg(fbdev, OCFB_CTRL, 0);
  322. platform_set_drvdata(pdev, NULL);
  323. return 0;
  324. }
  325. static struct of_device_id ocfb_match[] = {
  326. { .compatible = "opencores,ocfb", },
  327. {},
  328. };
  329. MODULE_DEVICE_TABLE(of, ocfb_match);
  330. static struct platform_driver ocfb_driver = {
  331. .probe = ocfb_probe,
  332. .remove = ocfb_remove,
  333. .driver = {
  334. .name = "ocfb_fb",
  335. .of_match_table = ocfb_match,
  336. }
  337. };
  338. /*
  339. * Init and exit routines
  340. */
  341. static int __init ocfb_init(void)
  342. {
  343. #ifndef MODULE
  344. char *option = NULL;
  345. if (fb_get_options("ocfb", &option))
  346. return -ENODEV;
  347. ocfb_setup(option);
  348. #endif
  349. return platform_driver_register(&ocfb_driver);
  350. }
  351. static void __exit ocfb_exit(void)
  352. {
  353. platform_driver_unregister(&ocfb_driver);
  354. }
  355. module_init(ocfb_init);
  356. module_exit(ocfb_exit);
  357. MODULE_AUTHOR("Stefan Kristiansson <stefan.kristiansson@saunalahti.fi>");
  358. MODULE_DESCRIPTION("OpenCores VGA/LCD 2.0 frame buffer driver");
  359. MODULE_LICENSE("GPL v2");
  360. module_param(mode_option, charp, 0);
  361. MODULE_PARM_DESC(mode_option, "Video mode ('<xres>x<yres>[-<bpp>][@refresh]')");