vrfb.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * VRFB Rotation Engine
  3. *
  4. * Copyright (C) 2009 Nokia Corporation
  5. * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*#define DEBUG*/
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/ioport.h>
  24. #include <linux/io.h>
  25. #include <linux/bitops.h>
  26. #include <linux/mutex.h>
  27. #include <linux/platform_device.h>
  28. #include <video/omapvrfb.h>
  29. #ifdef DEBUG
  30. #define DBG(format, ...) pr_debug("VRFB: " format, ## __VA_ARGS__)
  31. #else
  32. #define DBG(format, ...)
  33. #endif
  34. #define SMS_ROT_CONTROL(context) (0x0 + 0x10 * context)
  35. #define SMS_ROT_SIZE(context) (0x4 + 0x10 * context)
  36. #define SMS_ROT_PHYSICAL_BA(context) (0x8 + 0x10 * context)
  37. #define SMS_ROT_VIRT_BASE(rot) (0x1000000 * (rot))
  38. #define OMAP_VRFB_SIZE (2048 * 2048 * 4)
  39. #define VRFB_PAGE_WIDTH_EXP 5 /* Assuming SDRAM pagesize= 1024 */
  40. #define VRFB_PAGE_HEIGHT_EXP 5 /* 1024 = 2^5 * 2^5 */
  41. #define VRFB_PAGE_WIDTH (1 << VRFB_PAGE_WIDTH_EXP)
  42. #define VRFB_PAGE_HEIGHT (1 << VRFB_PAGE_HEIGHT_EXP)
  43. #define SMS_IMAGEHEIGHT_OFFSET 16
  44. #define SMS_IMAGEWIDTH_OFFSET 0
  45. #define SMS_PH_OFFSET 8
  46. #define SMS_PW_OFFSET 4
  47. #define SMS_PS_OFFSET 0
  48. /* bitmap of reserved contexts */
  49. static unsigned long ctx_map;
  50. struct vrfb_ctx {
  51. u32 base;
  52. u32 physical_ba;
  53. u32 control;
  54. u32 size;
  55. };
  56. static DEFINE_MUTEX(ctx_lock);
  57. /*
  58. * Access to this happens from client drivers or the PM core after wake-up.
  59. * For the first case we require locking at the driver level, for the second
  60. * we don't need locking, since no drivers will run until after the wake-up
  61. * has finished.
  62. */
  63. static void __iomem *vrfb_base;
  64. static int num_ctxs;
  65. static struct vrfb_ctx *ctxs;
  66. static void omap2_sms_write_rot_control(u32 val, unsigned ctx)
  67. {
  68. __raw_writel(val, vrfb_base + SMS_ROT_CONTROL(ctx));
  69. }
  70. static void omap2_sms_write_rot_size(u32 val, unsigned ctx)
  71. {
  72. __raw_writel(val, vrfb_base + SMS_ROT_SIZE(ctx));
  73. }
  74. static void omap2_sms_write_rot_physical_ba(u32 val, unsigned ctx)
  75. {
  76. __raw_writel(val, vrfb_base + SMS_ROT_PHYSICAL_BA(ctx));
  77. }
  78. static inline void restore_hw_context(int ctx)
  79. {
  80. omap2_sms_write_rot_control(ctxs[ctx].control, ctx);
  81. omap2_sms_write_rot_size(ctxs[ctx].size, ctx);
  82. omap2_sms_write_rot_physical_ba(ctxs[ctx].physical_ba, ctx);
  83. }
  84. static u32 get_image_width_roundup(u16 width, u8 bytespp)
  85. {
  86. unsigned long stride = width * bytespp;
  87. unsigned long ceil_pages_per_stride = (stride / VRFB_PAGE_WIDTH) +
  88. (stride % VRFB_PAGE_WIDTH != 0);
  89. return ceil_pages_per_stride * VRFB_PAGE_WIDTH / bytespp;
  90. }
  91. /*
  92. * This the extra space needed in the VRFB physical area for VRFB to safely wrap
  93. * any memory accesses to the invisible part of the virtual view to the physical
  94. * area.
  95. */
  96. static inline u32 get_extra_physical_size(u16 image_width_roundup, u8 bytespp)
  97. {
  98. return (OMAP_VRFB_LINE_LEN - image_width_roundup) * VRFB_PAGE_HEIGHT *
  99. bytespp;
  100. }
  101. void omap_vrfb_restore_context(void)
  102. {
  103. int i;
  104. unsigned long map = ctx_map;
  105. for (i = ffs(map); i; i = ffs(map)) {
  106. /* i=1..32 */
  107. i--;
  108. map &= ~(1 << i);
  109. restore_hw_context(i);
  110. }
  111. }
  112. void omap_vrfb_adjust_size(u16 *width, u16 *height,
  113. u8 bytespp)
  114. {
  115. *width = ALIGN(*width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
  116. *height = ALIGN(*height, VRFB_PAGE_HEIGHT);
  117. }
  118. EXPORT_SYMBOL(omap_vrfb_adjust_size);
  119. u32 omap_vrfb_min_phys_size(u16 width, u16 height, u8 bytespp)
  120. {
  121. unsigned long image_width_roundup = get_image_width_roundup(width,
  122. bytespp);
  123. if (image_width_roundup > OMAP_VRFB_LINE_LEN)
  124. return 0;
  125. return (width * height * bytespp) + get_extra_physical_size(
  126. image_width_roundup, bytespp);
  127. }
  128. EXPORT_SYMBOL(omap_vrfb_min_phys_size);
  129. u16 omap_vrfb_max_height(u32 phys_size, u16 width, u8 bytespp)
  130. {
  131. unsigned long image_width_roundup = get_image_width_roundup(width,
  132. bytespp);
  133. unsigned long height;
  134. unsigned long extra;
  135. if (image_width_roundup > OMAP_VRFB_LINE_LEN)
  136. return 0;
  137. extra = get_extra_physical_size(image_width_roundup, bytespp);
  138. if (phys_size < extra)
  139. return 0;
  140. height = (phys_size - extra) / (width * bytespp);
  141. /* Virtual views provided by VRFB are limited to 2048x2048. */
  142. return min_t(unsigned long, height, 2048);
  143. }
  144. EXPORT_SYMBOL(omap_vrfb_max_height);
  145. void omap_vrfb_setup(struct vrfb *vrfb, unsigned long paddr,
  146. u16 width, u16 height,
  147. unsigned bytespp, bool yuv_mode)
  148. {
  149. unsigned pixel_size_exp;
  150. u16 vrfb_width;
  151. u16 vrfb_height;
  152. u8 ctx = vrfb->context;
  153. u32 size;
  154. u32 control;
  155. DBG("omapfb_set_vrfb(%d, %lx, %dx%d, %d, %d)\n", ctx, paddr,
  156. width, height, bytespp, yuv_mode);
  157. /* For YUV2 and UYVY modes VRFB needs to handle pixels a bit
  158. * differently. See TRM. */
  159. if (yuv_mode) {
  160. bytespp *= 2;
  161. width /= 2;
  162. }
  163. if (bytespp == 4)
  164. pixel_size_exp = 2;
  165. else if (bytespp == 2)
  166. pixel_size_exp = 1;
  167. else {
  168. BUG();
  169. return;
  170. }
  171. vrfb_width = ALIGN(width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
  172. vrfb_height = ALIGN(height, VRFB_PAGE_HEIGHT);
  173. DBG("vrfb w %u, h %u bytespp %d\n", vrfb_width, vrfb_height, bytespp);
  174. size = vrfb_width << SMS_IMAGEWIDTH_OFFSET;
  175. size |= vrfb_height << SMS_IMAGEHEIGHT_OFFSET;
  176. control = pixel_size_exp << SMS_PS_OFFSET;
  177. control |= VRFB_PAGE_WIDTH_EXP << SMS_PW_OFFSET;
  178. control |= VRFB_PAGE_HEIGHT_EXP << SMS_PH_OFFSET;
  179. ctxs[ctx].physical_ba = paddr;
  180. ctxs[ctx].size = size;
  181. ctxs[ctx].control = control;
  182. omap2_sms_write_rot_physical_ba(paddr, ctx);
  183. omap2_sms_write_rot_size(size, ctx);
  184. omap2_sms_write_rot_control(control, ctx);
  185. DBG("vrfb offset pixels %d, %d\n",
  186. vrfb_width - width, vrfb_height - height);
  187. vrfb->xres = width;
  188. vrfb->yres = height;
  189. vrfb->xoffset = vrfb_width - width;
  190. vrfb->yoffset = vrfb_height - height;
  191. vrfb->bytespp = bytespp;
  192. vrfb->yuv_mode = yuv_mode;
  193. }
  194. EXPORT_SYMBOL(omap_vrfb_setup);
  195. int omap_vrfb_map_angle(struct vrfb *vrfb, u16 height, u8 rot)
  196. {
  197. unsigned long size = height * OMAP_VRFB_LINE_LEN * vrfb->bytespp;
  198. vrfb->vaddr[rot] = ioremap_wc(vrfb->paddr[rot], size);
  199. if (!vrfb->vaddr[rot]) {
  200. printk(KERN_ERR "vrfb: ioremap failed\n");
  201. return -ENOMEM;
  202. }
  203. DBG("ioremapped vrfb area %d of size %lu into %p\n", rot, size,
  204. vrfb->vaddr[rot]);
  205. return 0;
  206. }
  207. EXPORT_SYMBOL(omap_vrfb_map_angle);
  208. void omap_vrfb_release_ctx(struct vrfb *vrfb)
  209. {
  210. int rot;
  211. int ctx = vrfb->context;
  212. if (ctx == 0xff)
  213. return;
  214. DBG("release ctx %d\n", ctx);
  215. mutex_lock(&ctx_lock);
  216. BUG_ON(!(ctx_map & (1 << ctx)));
  217. clear_bit(ctx, &ctx_map);
  218. for (rot = 0; rot < 4; ++rot) {
  219. if (vrfb->paddr[rot]) {
  220. release_mem_region(vrfb->paddr[rot], OMAP_VRFB_SIZE);
  221. vrfb->paddr[rot] = 0;
  222. }
  223. }
  224. vrfb->context = 0xff;
  225. mutex_unlock(&ctx_lock);
  226. }
  227. EXPORT_SYMBOL(omap_vrfb_release_ctx);
  228. int omap_vrfb_request_ctx(struct vrfb *vrfb)
  229. {
  230. int rot;
  231. u32 paddr;
  232. u8 ctx;
  233. int r;
  234. DBG("request ctx\n");
  235. mutex_lock(&ctx_lock);
  236. for (ctx = 0; ctx < num_ctxs; ++ctx)
  237. if ((ctx_map & (1 << ctx)) == 0)
  238. break;
  239. if (ctx == num_ctxs) {
  240. pr_err("vrfb: no free contexts\n");
  241. r = -EBUSY;
  242. goto out;
  243. }
  244. DBG("found free ctx %d\n", ctx);
  245. set_bit(ctx, &ctx_map);
  246. memset(vrfb, 0, sizeof(*vrfb));
  247. vrfb->context = ctx;
  248. for (rot = 0; rot < 4; ++rot) {
  249. paddr = ctxs[ctx].base + SMS_ROT_VIRT_BASE(rot);
  250. if (!request_mem_region(paddr, OMAP_VRFB_SIZE, "vrfb")) {
  251. pr_err("vrfb: failed to reserve VRFB "
  252. "area for ctx %d, rotation %d\n",
  253. ctx, rot * 90);
  254. omap_vrfb_release_ctx(vrfb);
  255. r = -ENOMEM;
  256. goto out;
  257. }
  258. vrfb->paddr[rot] = paddr;
  259. DBG("VRFB %d/%d: %lx\n", ctx, rot*90, vrfb->paddr[rot]);
  260. }
  261. r = 0;
  262. out:
  263. mutex_unlock(&ctx_lock);
  264. return r;
  265. }
  266. EXPORT_SYMBOL(omap_vrfb_request_ctx);
  267. static int __init vrfb_probe(struct platform_device *pdev)
  268. {
  269. struct resource *mem;
  270. int i;
  271. /* first resource is the register res, the rest are vrfb contexts */
  272. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  273. if (!mem) {
  274. dev_err(&pdev->dev, "can't get vrfb base address\n");
  275. return -EINVAL;
  276. }
  277. vrfb_base = devm_request_and_ioremap(&pdev->dev, mem);
  278. if (!vrfb_base) {
  279. dev_err(&pdev->dev, "can't ioremap vrfb memory\n");
  280. return -ENOMEM;
  281. }
  282. num_ctxs = pdev->num_resources - 1;
  283. ctxs = devm_kzalloc(&pdev->dev,
  284. sizeof(struct vrfb_ctx) * num_ctxs,
  285. GFP_KERNEL);
  286. if (!ctxs)
  287. return -ENOMEM;
  288. for (i = 0; i < num_ctxs; ++i) {
  289. mem = platform_get_resource(pdev, IORESOURCE_MEM, 1 + i);
  290. if (!mem) {
  291. dev_err(&pdev->dev, "can't get vrfb ctx %d address\n",
  292. i);
  293. return -EINVAL;
  294. }
  295. ctxs[i].base = mem->start;
  296. }
  297. return 0;
  298. }
  299. static struct platform_driver vrfb_driver = {
  300. .driver.name = "omapvrfb",
  301. };
  302. static int __init vrfb_init(void)
  303. {
  304. return platform_driver_probe(&vrfb_driver, &vrfb_probe);
  305. }
  306. static void __exit vrfb_exit(void)
  307. {
  308. platform_driver_unregister(&vrfb_driver);
  309. }
  310. module_init(vrfb_init);
  311. module_exit(vrfb_exit);
  312. MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
  313. MODULE_DESCRIPTION("OMAP VRFB");
  314. MODULE_LICENSE("GPL v2");