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