vrfb.c 9.5 KB

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