vrfb.c 9.4 KB

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