Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SuperH Mobile MERAM Driver for SuperH Mobile LCDC Driver |
| 3 | * |
| 4 | * Copyright (c) 2011 Damian Hobson-Garcia <dhobsong@igel.co.jp> |
| 5 | * Takanari Hayama <taki@igel.co.jp> |
| 6 | * |
| 7 | * This file is subject to the terms and conditions of the GNU General Public |
| 8 | * License. See the file "COPYING" in the main directory of this archive |
| 9 | * for more details. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/device.h> |
Damian Hobson-Garcia | 1767377 | 2011-07-04 08:06:11 +0200 | [diff] [blame] | 15 | #include <linux/pm_runtime.h> |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 16 | #include <linux/io.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/platform_device.h> |
Laurent Pinchart | 8a20974 | 2011-07-13 12:13:47 +0200 | [diff] [blame] | 19 | #include <video/sh_mobile_meram.h> |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 20 | |
| 21 | /* meram registers */ |
Laurent Pinchart | f0a260f | 2011-07-13 12:13:47 +0200 | [diff] [blame] | 22 | #define MEVCR1 0x4 |
| 23 | #define MEVCR1_RST (1 << 31) |
| 24 | #define MEVCR1_WD (1 << 30) |
| 25 | #define MEVCR1_AMD1 (1 << 29) |
| 26 | #define MEVCR1_AMD0 (1 << 28) |
| 27 | #define MEQSEL1 0x40 |
| 28 | #define MEQSEL2 0x44 |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 29 | |
Laurent Pinchart | f0a260f | 2011-07-13 12:13:47 +0200 | [diff] [blame] | 30 | #define MExxCTL 0x400 |
| 31 | #define MExxCTL_BV (1 << 31) |
| 32 | #define MExxCTL_BSZ_SHIFT 28 |
| 33 | #define MExxCTL_MSAR_MASK (0x7ff << MExxCTL_MSAR_SHIFT) |
| 34 | #define MExxCTL_MSAR_SHIFT 16 |
| 35 | #define MExxCTL_NXT_MASK (0x1f << MExxCTL_NXT_SHIFT) |
| 36 | #define MExxCTL_NXT_SHIFT 11 |
| 37 | #define MExxCTL_WD1 (1 << 10) |
| 38 | #define MExxCTL_WD0 (1 << 9) |
| 39 | #define MExxCTL_WS (1 << 8) |
| 40 | #define MExxCTL_CB (1 << 7) |
| 41 | #define MExxCTL_WBF (1 << 6) |
| 42 | #define MExxCTL_WF (1 << 5) |
| 43 | #define MExxCTL_RF (1 << 4) |
| 44 | #define MExxCTL_CM (1 << 3) |
| 45 | #define MExxCTL_MD_READ (1 << 0) |
| 46 | #define MExxCTL_MD_WRITE (2 << 0) |
| 47 | #define MExxCTL_MD_ICB_WB (3 << 0) |
| 48 | #define MExxCTL_MD_ICB (4 << 0) |
| 49 | #define MExxCTL_MD_FB (7 << 0) |
| 50 | #define MExxCTL_MD_MASK (7 << 0) |
| 51 | #define MExxBSIZE 0x404 |
| 52 | #define MExxBSIZE_RCNT_SHIFT 28 |
| 53 | #define MExxBSIZE_YSZM1_SHIFT 16 |
| 54 | #define MExxBSIZE_XSZM1_SHIFT 0 |
| 55 | #define MExxMNCF 0x408 |
| 56 | #define MExxMNCF_KWBNM_SHIFT 28 |
| 57 | #define MExxMNCF_KRBNM_SHIFT 24 |
| 58 | #define MExxMNCF_BNM_SHIFT 16 |
| 59 | #define MExxMNCF_XBV (1 << 15) |
| 60 | #define MExxMNCF_CPL_YCBCR444 (1 << 12) |
| 61 | #define MExxMNCF_CPL_YCBCR420 (2 << 12) |
| 62 | #define MExxMNCF_CPL_YCBCR422 (3 << 12) |
| 63 | #define MExxMNCF_CPL_MSK (3 << 12) |
| 64 | #define MExxMNCF_BL (1 << 2) |
| 65 | #define MExxMNCF_LNM_SHIFT 0 |
| 66 | #define MExxSARA 0x410 |
| 67 | #define MExxSARB 0x414 |
| 68 | #define MExxSBSIZE 0x418 |
| 69 | #define MExxSBSIZE_HDV (1 << 31) |
| 70 | #define MExxSBSIZE_HSZ16 (0 << 28) |
| 71 | #define MExxSBSIZE_HSZ32 (1 << 28) |
| 72 | #define MExxSBSIZE_HSZ64 (2 << 28) |
| 73 | #define MExxSBSIZE_HSZ128 (3 << 28) |
| 74 | #define MExxSBSIZE_SBSIZZ_SHIFT 0 |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 75 | |
Laurent Pinchart | f0a260f | 2011-07-13 12:13:47 +0200 | [diff] [blame] | 76 | #define MERAM_MExxCTL_VAL(next, addr) \ |
| 77 | ((((next) << MExxCTL_NXT_SHIFT) & MExxCTL_NXT_MASK) | \ |
| 78 | (((addr) << MExxCTL_MSAR_SHIFT) & MExxCTL_MSAR_MASK)) |
| 79 | #define MERAM_MExxBSIZE_VAL(rcnt, yszm1, xszm1) \ |
| 80 | (((rcnt) << MExxBSIZE_RCNT_SHIFT) | \ |
| 81 | ((yszm1) << MExxBSIZE_YSZM1_SHIFT) | \ |
| 82 | ((xszm1) << MExxBSIZE_XSZM1_SHIFT)) |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 83 | |
Laurent Pinchart | 8a20974 | 2011-07-13 12:13:47 +0200 | [diff] [blame] | 84 | #define SH_MOBILE_MERAM_ICB_NUM 32 |
| 85 | |
Damian Hobson-Garcia | 0b3bb77 | 2011-06-22 07:49:51 +0200 | [diff] [blame] | 86 | static unsigned long common_regs[] = { |
| 87 | MEVCR1, |
| 88 | MEQSEL1, |
| 89 | MEQSEL2, |
| 90 | }; |
| 91 | #define CMN_REGS_SIZE ARRAY_SIZE(common_regs) |
| 92 | |
| 93 | static unsigned long icb_regs[] = { |
| 94 | MExxCTL, |
| 95 | MExxBSIZE, |
| 96 | MExxMNCF, |
| 97 | MExxSARA, |
| 98 | MExxSARB, |
| 99 | MExxSBSIZE, |
| 100 | }; |
| 101 | #define ICB_REGS_SIZE ARRAY_SIZE(icb_regs) |
| 102 | |
Damian Hobson-Garcia | 0aa492b | 2011-06-22 07:49:50 +0200 | [diff] [blame] | 103 | struct sh_mobile_meram_priv { |
| 104 | void __iomem *base; |
| 105 | struct mutex lock; |
| 106 | unsigned long used_icb; |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 107 | unsigned int used_meram_cache_regions; |
Damian Hobson-Garcia | 0aa492b | 2011-06-22 07:49:50 +0200 | [diff] [blame] | 108 | unsigned long used_meram_cache[SH_MOBILE_MERAM_ICB_NUM]; |
Damian Hobson-Garcia | 0b3bb77 | 2011-06-22 07:49:51 +0200 | [diff] [blame] | 109 | unsigned long cmn_saved_regs[CMN_REGS_SIZE]; |
| 110 | unsigned long icb_saved_regs[ICB_REGS_SIZE * SH_MOBILE_MERAM_ICB_NUM]; |
Damian Hobson-Garcia | 0aa492b | 2011-06-22 07:49:50 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 113 | /* settings */ |
| 114 | #define MERAM_SEC_LINE 15 |
| 115 | #define MERAM_LINE_WIDTH 2048 |
| 116 | |
| 117 | /* |
| 118 | * MERAM/ICB access functions |
| 119 | */ |
| 120 | |
Laurent Pinchart | f0a260f | 2011-07-13 12:13:47 +0200 | [diff] [blame] | 121 | #define MERAM_ICB_OFFSET(base, idx, off) ((base) + (off) + (idx) * 0x20) |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 122 | |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 123 | static inline void meram_write_icb(void __iomem *base, unsigned int idx, |
| 124 | unsigned int off, unsigned long val) |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 125 | { |
| 126 | iowrite32(val, MERAM_ICB_OFFSET(base, idx, off)); |
| 127 | } |
| 128 | |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 129 | static inline unsigned long meram_read_icb(void __iomem *base, unsigned int idx, |
| 130 | unsigned int off) |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 131 | { |
| 132 | return ioread32(MERAM_ICB_OFFSET(base, idx, off)); |
| 133 | } |
| 134 | |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 135 | static inline void meram_write_reg(void __iomem *base, unsigned int off, |
| 136 | unsigned long val) |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 137 | { |
| 138 | iowrite32(val, base + off); |
| 139 | } |
| 140 | |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 141 | static inline unsigned long meram_read_reg(void __iomem *base, unsigned int off) |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 142 | { |
| 143 | return ioread32(base + off); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * register ICB |
| 148 | */ |
| 149 | |
| 150 | #define MERAM_CACHE_START(p) ((p) >> 16) |
| 151 | #define MERAM_CACHE_END(p) ((p) & 0xffff) |
| 152 | #define MERAM_CACHE_SET(o, s) ((((o) & 0xffff) << 16) | \ |
| 153 | (((o) + (s) - 1) & 0xffff)) |
| 154 | |
| 155 | /* |
| 156 | * check if there's no overlaps in MERAM allocation. |
| 157 | */ |
| 158 | |
| 159 | static inline int meram_check_overlap(struct sh_mobile_meram_priv *priv, |
Laurent Pinchart | d272f42 | 2011-09-19 11:40:31 +0200 | [diff] [blame] | 160 | struct sh_mobile_meram_icb_cfg *new) |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 161 | { |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 162 | unsigned int used_start, used_end, meram_start, meram_end; |
| 163 | unsigned int i; |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 164 | |
| 165 | /* valid ICB? */ |
| 166 | if (new->marker_icb & ~0x1f || new->cache_icb & ~0x1f) |
| 167 | return 1; |
| 168 | |
| 169 | if (test_bit(new->marker_icb, &priv->used_icb) || |
| 170 | test_bit(new->cache_icb, &priv->used_icb)) |
| 171 | return 1; |
| 172 | |
| 173 | for (i = 0; i < priv->used_meram_cache_regions; i++) { |
| 174 | used_start = MERAM_CACHE_START(priv->used_meram_cache[i]); |
| 175 | used_end = MERAM_CACHE_END(priv->used_meram_cache[i]); |
| 176 | meram_start = new->meram_offset; |
| 177 | meram_end = new->meram_offset + new->meram_size; |
| 178 | |
| 179 | if ((meram_start >= used_start && meram_start < used_end) || |
| 180 | (meram_end > used_start && meram_end < used_end)) |
| 181 | return 1; |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * mark the specified ICB as used |
| 189 | */ |
| 190 | |
| 191 | static inline void meram_mark(struct sh_mobile_meram_priv *priv, |
Laurent Pinchart | d272f42 | 2011-09-19 11:40:31 +0200 | [diff] [blame] | 192 | struct sh_mobile_meram_icb_cfg *new) |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 193 | { |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 194 | unsigned int n; |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 195 | |
| 196 | if (new->marker_icb < 0 || new->cache_icb < 0) |
| 197 | return; |
| 198 | |
| 199 | __set_bit(new->marker_icb, &priv->used_icb); |
| 200 | __set_bit(new->cache_icb, &priv->used_icb); |
| 201 | |
| 202 | n = priv->used_meram_cache_regions; |
| 203 | |
| 204 | priv->used_meram_cache[n] = MERAM_CACHE_SET(new->meram_offset, |
| 205 | new->meram_size); |
| 206 | |
| 207 | priv->used_meram_cache_regions++; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * unmark the specified ICB as used |
| 212 | */ |
| 213 | |
| 214 | static inline void meram_unmark(struct sh_mobile_meram_priv *priv, |
Laurent Pinchart | d272f42 | 2011-09-19 11:40:31 +0200 | [diff] [blame] | 215 | struct sh_mobile_meram_icb_cfg *icb) |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 216 | { |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 217 | unsigned long pattern; |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 218 | unsigned int i; |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 219 | |
| 220 | if (icb->marker_icb < 0 || icb->cache_icb < 0) |
| 221 | return; |
| 222 | |
| 223 | __clear_bit(icb->marker_icb, &priv->used_icb); |
| 224 | __clear_bit(icb->cache_icb, &priv->used_icb); |
| 225 | |
| 226 | pattern = MERAM_CACHE_SET(icb->meram_offset, icb->meram_size); |
| 227 | for (i = 0; i < priv->used_meram_cache_regions; i++) { |
| 228 | if (priv->used_meram_cache[i] == pattern) { |
| 229 | while (i < priv->used_meram_cache_regions - 1) { |
| 230 | priv->used_meram_cache[i] = |
| 231 | priv->used_meram_cache[i + 1] ; |
| 232 | i++; |
| 233 | } |
| 234 | priv->used_meram_cache[i] = 0; |
| 235 | priv->used_meram_cache_regions--; |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
Damian | 3fedd2a | 2011-05-18 11:10:08 +0000 | [diff] [blame] | 241 | /* |
| 242 | * is this a YCbCr(NV12, NV16 or NV24) colorspace |
| 243 | */ |
| 244 | static inline int is_nvcolor(int cspace) |
| 245 | { |
| 246 | if (cspace == SH_MOBILE_MERAM_PF_NV || |
| 247 | cspace == SH_MOBILE_MERAM_PF_NV24) |
| 248 | return 1; |
| 249 | return 0; |
| 250 | } |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 251 | |
| 252 | /* |
| 253 | * set the next address to fetch |
| 254 | */ |
| 255 | static inline void meram_set_next_addr(struct sh_mobile_meram_priv *priv, |
| 256 | struct sh_mobile_meram_cfg *cfg, |
| 257 | unsigned long base_addr_y, |
| 258 | unsigned long base_addr_c) |
| 259 | { |
| 260 | unsigned long target; |
| 261 | |
| 262 | target = (cfg->current_reg) ? MExxSARA : MExxSARB; |
| 263 | cfg->current_reg ^= 1; |
| 264 | |
| 265 | /* set the next address to fetch */ |
| 266 | meram_write_icb(priv->base, cfg->icb[0].cache_icb, target, |
| 267 | base_addr_y); |
| 268 | meram_write_icb(priv->base, cfg->icb[0].marker_icb, target, |
| 269 | base_addr_y + cfg->icb[0].cache_unit); |
| 270 | |
Damian | 3fedd2a | 2011-05-18 11:10:08 +0000 | [diff] [blame] | 271 | if (is_nvcolor(cfg->pixelformat)) { |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 272 | meram_write_icb(priv->base, cfg->icb[1].cache_icb, target, |
| 273 | base_addr_c); |
| 274 | meram_write_icb(priv->base, cfg->icb[1].marker_icb, target, |
| 275 | base_addr_c + cfg->icb[1].cache_unit); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * get the next ICB address |
| 281 | */ |
| 282 | static inline void meram_get_next_icb_addr(struct sh_mobile_meram_info *pdata, |
| 283 | struct sh_mobile_meram_cfg *cfg, |
| 284 | unsigned long *icb_addr_y, |
| 285 | unsigned long *icb_addr_c) |
| 286 | { |
| 287 | unsigned long icb_offset; |
| 288 | |
| 289 | if (pdata->addr_mode == SH_MOBILE_MERAM_MODE0) |
| 290 | icb_offset = 0x80000000 | (cfg->current_reg << 29); |
| 291 | else |
| 292 | icb_offset = 0xc0000000 | (cfg->current_reg << 23); |
| 293 | |
| 294 | *icb_addr_y = icb_offset | (cfg->icb[0].marker_icb << 24); |
Damian Hobson-Garcia | 06c8a6a | 2011-06-22 07:46:25 +0000 | [diff] [blame] | 295 | if (is_nvcolor(cfg->pixelformat)) |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 296 | *icb_addr_c = icb_offset | (cfg->icb[1].marker_icb << 24); |
| 297 | } |
| 298 | |
| 299 | #define MERAM_CALC_BYTECOUNT(x, y) \ |
| 300 | (((x) * (y) + (MERAM_LINE_WIDTH - 1)) & ~(MERAM_LINE_WIDTH - 1)) |
| 301 | |
| 302 | /* |
| 303 | * initialize MERAM |
| 304 | */ |
| 305 | |
| 306 | static int meram_init(struct sh_mobile_meram_priv *priv, |
Laurent Pinchart | d272f42 | 2011-09-19 11:40:31 +0200 | [diff] [blame] | 307 | struct sh_mobile_meram_icb_cfg *icb, |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 308 | unsigned int xres, unsigned int yres, |
| 309 | unsigned int *out_pitch) |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 310 | { |
| 311 | unsigned long total_byte_count = MERAM_CALC_BYTECOUNT(xres, yres); |
| 312 | unsigned long bnm; |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 313 | unsigned int lcdc_pitch; |
| 314 | unsigned int xpitch; |
| 315 | unsigned int line_cnt; |
| 316 | unsigned int save_lines; |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 317 | |
| 318 | /* adjust pitch to 1024, 2048, 4096 or 8192 */ |
| 319 | lcdc_pitch = (xres - 1) | 1023; |
| 320 | lcdc_pitch = lcdc_pitch | (lcdc_pitch >> 1); |
| 321 | lcdc_pitch = lcdc_pitch | (lcdc_pitch >> 2); |
| 322 | lcdc_pitch += 1; |
| 323 | |
| 324 | /* derive settings */ |
| 325 | if (lcdc_pitch == 8192 && yres >= 1024) { |
| 326 | lcdc_pitch = xpitch = MERAM_LINE_WIDTH; |
| 327 | line_cnt = total_byte_count >> 11; |
| 328 | *out_pitch = xres; |
| 329 | save_lines = (icb->meram_size / 16 / MERAM_SEC_LINE); |
| 330 | save_lines *= MERAM_SEC_LINE; |
| 331 | } else { |
| 332 | xpitch = xres; |
| 333 | line_cnt = yres; |
| 334 | *out_pitch = lcdc_pitch; |
| 335 | save_lines = icb->meram_size / (lcdc_pitch >> 10) / 2; |
| 336 | save_lines &= 0xff; |
| 337 | } |
| 338 | bnm = (save_lines - 1) << 16; |
| 339 | |
| 340 | /* TODO: we better to check if we have enough MERAM buffer size */ |
| 341 | |
| 342 | /* set up ICB */ |
| 343 | meram_write_icb(priv->base, icb->cache_icb, MExxBSIZE, |
| 344 | MERAM_MExxBSIZE_VAL(0x0, line_cnt - 1, xpitch - 1)); |
| 345 | meram_write_icb(priv->base, icb->marker_icb, MExxBSIZE, |
| 346 | MERAM_MExxBSIZE_VAL(0xf, line_cnt - 1, xpitch - 1)); |
| 347 | |
| 348 | meram_write_icb(priv->base, icb->cache_icb, MExxMNCF, bnm); |
| 349 | meram_write_icb(priv->base, icb->marker_icb, MExxMNCF, bnm); |
| 350 | |
| 351 | meram_write_icb(priv->base, icb->cache_icb, MExxSBSIZE, xpitch); |
| 352 | meram_write_icb(priv->base, icb->marker_icb, MExxSBSIZE, xpitch); |
| 353 | |
| 354 | /* save a cache unit size */ |
| 355 | icb->cache_unit = xres * save_lines; |
| 356 | |
| 357 | /* |
| 358 | * Set MERAM for framebuffer |
| 359 | * |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 360 | * we also chain the cache_icb and the marker_icb. |
| 361 | * we also split the allocated MERAM buffer between two ICBs. |
| 362 | */ |
| 363 | meram_write_icb(priv->base, icb->cache_icb, MExxCTL, |
Laurent Pinchart | f0a260f | 2011-07-13 12:13:47 +0200 | [diff] [blame] | 364 | MERAM_MExxCTL_VAL(icb->marker_icb, icb->meram_offset) | |
| 365 | MExxCTL_WD1 | MExxCTL_WD0 | MExxCTL_WS | MExxCTL_CM | |
| 366 | MExxCTL_MD_FB); |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 367 | meram_write_icb(priv->base, icb->marker_icb, MExxCTL, |
Laurent Pinchart | f0a260f | 2011-07-13 12:13:47 +0200 | [diff] [blame] | 368 | MERAM_MExxCTL_VAL(icb->cache_icb, icb->meram_offset + |
| 369 | icb->meram_size / 2) | |
| 370 | MExxCTL_WD1 | MExxCTL_WD0 | MExxCTL_WS | MExxCTL_CM | |
| 371 | MExxCTL_MD_FB); |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static void meram_deinit(struct sh_mobile_meram_priv *priv, |
Laurent Pinchart | d272f42 | 2011-09-19 11:40:31 +0200 | [diff] [blame] | 377 | struct sh_mobile_meram_icb_cfg *icb) |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 378 | { |
| 379 | /* disable ICB */ |
Laurent Pinchart | da6cf51 | 2011-08-31 13:00:52 +0200 | [diff] [blame] | 380 | meram_write_icb(priv->base, icb->cache_icb, MExxCTL, |
| 381 | MExxCTL_WBF | MExxCTL_WF | MExxCTL_RF); |
| 382 | meram_write_icb(priv->base, icb->marker_icb, MExxCTL, |
| 383 | MExxCTL_WBF | MExxCTL_WF | MExxCTL_RF); |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 384 | icb->cache_unit = 0; |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * register the ICB |
| 389 | */ |
| 390 | |
| 391 | static int sh_mobile_meram_register(struct sh_mobile_meram_info *pdata, |
| 392 | struct sh_mobile_meram_cfg *cfg, |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 393 | unsigned int xres, unsigned int yres, |
| 394 | unsigned int pixelformat, |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 395 | unsigned long base_addr_y, |
| 396 | unsigned long base_addr_c, |
| 397 | unsigned long *icb_addr_y, |
| 398 | unsigned long *icb_addr_c, |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 399 | unsigned int *pitch) |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 400 | { |
| 401 | struct platform_device *pdev; |
| 402 | struct sh_mobile_meram_priv *priv; |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 403 | unsigned int out_pitch; |
| 404 | unsigned int n; |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 405 | int error = 0; |
| 406 | |
| 407 | if (!pdata || !pdata->priv || !pdata->pdev || !cfg) |
| 408 | return -EINVAL; |
| 409 | |
| 410 | if (pixelformat != SH_MOBILE_MERAM_PF_NV && |
Damian | 3fedd2a | 2011-05-18 11:10:08 +0000 | [diff] [blame] | 411 | pixelformat != SH_MOBILE_MERAM_PF_NV24 && |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 412 | pixelformat != SH_MOBILE_MERAM_PF_RGB) |
| 413 | return -EINVAL; |
| 414 | |
| 415 | priv = pdata->priv; |
| 416 | pdev = pdata->pdev; |
| 417 | |
| 418 | dev_dbg(&pdev->dev, "registering %dx%d (%s) (y=%08lx, c=%08lx)", |
| 419 | xres, yres, (!pixelformat) ? "yuv" : "rgb", |
| 420 | base_addr_y, base_addr_c); |
| 421 | |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 422 | /* we can't handle wider than 8192px */ |
| 423 | if (xres > 8192) { |
| 424 | dev_err(&pdev->dev, "width exceeding the limit (> 8192)."); |
Laurent Pinchart | 7963e21 | 2011-07-13 12:13:47 +0200 | [diff] [blame] | 425 | return -EINVAL; |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /* do we have at least one ICB config? */ |
| 429 | if (cfg->icb[0].marker_icb < 0 || cfg->icb[0].cache_icb < 0) { |
| 430 | dev_err(&pdev->dev, "at least one ICB is required."); |
Laurent Pinchart | 7963e21 | 2011-07-13 12:13:47 +0200 | [diff] [blame] | 431 | return -EINVAL; |
| 432 | } |
| 433 | |
| 434 | mutex_lock(&priv->lock); |
| 435 | |
| 436 | if (priv->used_meram_cache_regions + 2 > SH_MOBILE_MERAM_ICB_NUM) { |
| 437 | dev_err(&pdev->dev, "no more ICB available."); |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 438 | error = -EINVAL; |
| 439 | goto err; |
| 440 | } |
| 441 | |
| 442 | /* make sure that there's no overlaps */ |
| 443 | if (meram_check_overlap(priv, &cfg->icb[0])) { |
| 444 | dev_err(&pdev->dev, "conflicting config detected."); |
| 445 | error = -EINVAL; |
| 446 | goto err; |
| 447 | } |
| 448 | n = 1; |
| 449 | |
| 450 | /* do the same if we have the second ICB set */ |
| 451 | if (cfg->icb[1].marker_icb >= 0 && cfg->icb[1].cache_icb >= 0) { |
| 452 | if (meram_check_overlap(priv, &cfg->icb[1])) { |
| 453 | dev_err(&pdev->dev, "conflicting config detected."); |
| 454 | error = -EINVAL; |
| 455 | goto err; |
| 456 | } |
| 457 | n = 2; |
| 458 | } |
| 459 | |
Damian | 3fedd2a | 2011-05-18 11:10:08 +0000 | [diff] [blame] | 460 | if (is_nvcolor(pixelformat) && n != 2) { |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 461 | dev_err(&pdev->dev, "requires two ICB sets for planar Y/C."); |
| 462 | error = -EINVAL; |
| 463 | goto err; |
| 464 | } |
| 465 | |
| 466 | /* we now register the ICB */ |
| 467 | cfg->pixelformat = pixelformat; |
| 468 | meram_mark(priv, &cfg->icb[0]); |
Damian | 3fedd2a | 2011-05-18 11:10:08 +0000 | [diff] [blame] | 469 | if (is_nvcolor(pixelformat)) |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 470 | meram_mark(priv, &cfg->icb[1]); |
| 471 | |
| 472 | /* initialize MERAM */ |
| 473 | meram_init(priv, &cfg->icb[0], xres, yres, &out_pitch); |
| 474 | *pitch = out_pitch; |
| 475 | if (pixelformat == SH_MOBILE_MERAM_PF_NV) |
| 476 | meram_init(priv, &cfg->icb[1], xres, (yres + 1) / 2, |
| 477 | &out_pitch); |
Damian | 3fedd2a | 2011-05-18 11:10:08 +0000 | [diff] [blame] | 478 | else if (pixelformat == SH_MOBILE_MERAM_PF_NV24) |
| 479 | meram_init(priv, &cfg->icb[1], 2 * xres, (yres + 1) / 2, |
| 480 | &out_pitch); |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 481 | |
| 482 | cfg->current_reg = 1; |
| 483 | meram_set_next_addr(priv, cfg, base_addr_y, base_addr_c); |
| 484 | meram_get_next_icb_addr(pdata, cfg, icb_addr_y, icb_addr_c); |
| 485 | |
| 486 | dev_dbg(&pdev->dev, "registered - can access via y=%08lx, c=%08lx", |
| 487 | *icb_addr_y, *icb_addr_c); |
| 488 | |
| 489 | err: |
| 490 | mutex_unlock(&priv->lock); |
| 491 | return error; |
| 492 | } |
| 493 | |
| 494 | static int sh_mobile_meram_unregister(struct sh_mobile_meram_info *pdata, |
| 495 | struct sh_mobile_meram_cfg *cfg) |
| 496 | { |
| 497 | struct sh_mobile_meram_priv *priv; |
| 498 | |
| 499 | if (!pdata || !pdata->priv || !cfg) |
| 500 | return -EINVAL; |
| 501 | |
| 502 | priv = pdata->priv; |
| 503 | |
| 504 | mutex_lock(&priv->lock); |
| 505 | |
| 506 | /* deinit & unmark */ |
Damian | 3fedd2a | 2011-05-18 11:10:08 +0000 | [diff] [blame] | 507 | if (is_nvcolor(cfg->pixelformat)) { |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 508 | meram_deinit(priv, &cfg->icb[1]); |
| 509 | meram_unmark(priv, &cfg->icb[1]); |
| 510 | } |
| 511 | meram_deinit(priv, &cfg->icb[0]); |
| 512 | meram_unmark(priv, &cfg->icb[0]); |
| 513 | |
| 514 | mutex_unlock(&priv->lock); |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | static int sh_mobile_meram_update(struct sh_mobile_meram_info *pdata, |
| 520 | struct sh_mobile_meram_cfg *cfg, |
| 521 | unsigned long base_addr_y, |
| 522 | unsigned long base_addr_c, |
| 523 | unsigned long *icb_addr_y, |
| 524 | unsigned long *icb_addr_c) |
| 525 | { |
| 526 | struct sh_mobile_meram_priv *priv; |
| 527 | |
| 528 | if (!pdata || !pdata->priv || !cfg) |
| 529 | return -EINVAL; |
| 530 | |
| 531 | priv = pdata->priv; |
| 532 | |
| 533 | mutex_lock(&priv->lock); |
| 534 | |
| 535 | meram_set_next_addr(priv, cfg, base_addr_y, base_addr_c); |
| 536 | meram_get_next_icb_addr(pdata, cfg, icb_addr_y, icb_addr_c); |
| 537 | |
| 538 | mutex_unlock(&priv->lock); |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
Damian Hobson-Garcia | 0b3bb77 | 2011-06-22 07:49:51 +0200 | [diff] [blame] | 543 | static int sh_mobile_meram_runtime_suspend(struct device *dev) |
| 544 | { |
| 545 | struct platform_device *pdev = to_platform_device(dev); |
| 546 | struct sh_mobile_meram_priv *priv = platform_get_drvdata(pdev); |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 547 | unsigned int i, j; |
Damian Hobson-Garcia | 0b3bb77 | 2011-06-22 07:49:51 +0200 | [diff] [blame] | 548 | |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 549 | for (i = 0; i < CMN_REGS_SIZE; i++) |
| 550 | priv->cmn_saved_regs[i] = meram_read_reg(priv->base, |
| 551 | common_regs[i]); |
Damian Hobson-Garcia | 0b3bb77 | 2011-06-22 07:49:51 +0200 | [diff] [blame] | 552 | |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 553 | for (i = 0; i < 32; i++) { |
| 554 | if (!test_bit(i, &priv->used_icb)) |
Damian Hobson-Garcia | 0b3bb77 | 2011-06-22 07:49:51 +0200 | [diff] [blame] | 555 | continue; |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 556 | for (j = 0; j < ICB_REGS_SIZE; j++) { |
| 557 | priv->icb_saved_regs[i * ICB_REGS_SIZE + j] = |
| 558 | meram_read_icb(priv->base, i, icb_regs[j]); |
Damian Hobson-Garcia | 0b3bb77 | 2011-06-22 07:49:51 +0200 | [diff] [blame] | 559 | /* Reset ICB on resume */ |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 560 | if (icb_regs[j] == MExxCTL) |
| 561 | priv->icb_saved_regs[i * ICB_REGS_SIZE + j] |= |
Laurent Pinchart | f0a260f | 2011-07-13 12:13:47 +0200 | [diff] [blame] | 562 | MExxCTL_WBF | MExxCTL_WF | MExxCTL_RF; |
Damian Hobson-Garcia | 0b3bb77 | 2011-06-22 07:49:51 +0200 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static int sh_mobile_meram_runtime_resume(struct device *dev) |
| 569 | { |
| 570 | struct platform_device *pdev = to_platform_device(dev); |
| 571 | struct sh_mobile_meram_priv *priv = platform_get_drvdata(pdev); |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 572 | unsigned int i, j; |
Damian Hobson-Garcia | 0b3bb77 | 2011-06-22 07:49:51 +0200 | [diff] [blame] | 573 | |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 574 | for (i = 0; i < 32; i++) { |
| 575 | if (!test_bit(i, &priv->used_icb)) |
Damian Hobson-Garcia | 0b3bb77 | 2011-06-22 07:49:51 +0200 | [diff] [blame] | 576 | continue; |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 577 | for (j = 0; j < ICB_REGS_SIZE; j++) { |
| 578 | meram_write_icb(priv->base, i, icb_regs[j], |
| 579 | priv->icb_saved_regs[i * ICB_REGS_SIZE + j]); |
Damian Hobson-Garcia | 0b3bb77 | 2011-06-22 07:49:51 +0200 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
Laurent Pinchart | 0543283 | 2011-09-19 11:40:31 +0200 | [diff] [blame^] | 583 | for (i = 0; i < CMN_REGS_SIZE; i++) |
| 584 | meram_write_reg(priv->base, common_regs[i], |
| 585 | priv->cmn_saved_regs[i]); |
Damian Hobson-Garcia | 0b3bb77 | 2011-06-22 07:49:51 +0200 | [diff] [blame] | 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | static const struct dev_pm_ops sh_mobile_meram_dev_pm_ops = { |
| 590 | .runtime_suspend = sh_mobile_meram_runtime_suspend, |
| 591 | .runtime_resume = sh_mobile_meram_runtime_resume, |
| 592 | }; |
| 593 | |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 594 | static struct sh_mobile_meram_ops sh_mobile_meram_ops = { |
| 595 | .module = THIS_MODULE, |
| 596 | .meram_register = sh_mobile_meram_register, |
| 597 | .meram_unregister = sh_mobile_meram_unregister, |
| 598 | .meram_update = sh_mobile_meram_update, |
| 599 | }; |
| 600 | |
| 601 | /* |
| 602 | * initialize MERAM |
| 603 | */ |
| 604 | |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 605 | static int __devinit sh_mobile_meram_probe(struct platform_device *pdev) |
| 606 | { |
| 607 | struct sh_mobile_meram_priv *priv; |
| 608 | struct sh_mobile_meram_info *pdata = pdev->dev.platform_data; |
Laurent Pinchart | e1d1144 | 2011-09-19 11:40:31 +0200 | [diff] [blame] | 609 | struct resource *regs; |
| 610 | struct resource *meram; |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 611 | int error; |
| 612 | |
| 613 | if (!pdata) { |
| 614 | dev_err(&pdev->dev, "no platform data defined\n"); |
| 615 | return -EINVAL; |
| 616 | } |
| 617 | |
Laurent Pinchart | e1d1144 | 2011-09-19 11:40:31 +0200 | [diff] [blame] | 618 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 619 | meram = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 620 | if (regs == NULL || meram == NULL) { |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 621 | dev_err(&pdev->dev, "cannot get platform resources\n"); |
| 622 | return -ENOENT; |
| 623 | } |
| 624 | |
| 625 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 626 | if (!priv) { |
| 627 | dev_err(&pdev->dev, "cannot allocate device data\n"); |
| 628 | return -ENOMEM; |
| 629 | } |
| 630 | |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 631 | /* initialize private data */ |
| 632 | mutex_init(&priv->lock); |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 633 | pdata->ops = &sh_mobile_meram_ops; |
| 634 | pdata->priv = priv; |
| 635 | pdata->pdev = pdev; |
| 636 | |
Laurent Pinchart | e1d1144 | 2011-09-19 11:40:31 +0200 | [diff] [blame] | 637 | if (!request_mem_region(regs->start, resource_size(regs), pdev->name)) { |
| 638 | dev_err(&pdev->dev, "MERAM registers region already claimed\n"); |
| 639 | error = -EBUSY; |
| 640 | goto err_req_regs; |
| 641 | } |
| 642 | |
| 643 | if (!request_mem_region(meram->start, resource_size(meram), |
| 644 | pdev->name)) { |
| 645 | dev_err(&pdev->dev, "MERAM memory region already claimed\n"); |
| 646 | error = -EBUSY; |
| 647 | goto err_req_meram; |
| 648 | } |
| 649 | |
| 650 | priv->base = ioremap_nocache(regs->start, resource_size(regs)); |
| 651 | if (!priv->base) { |
| 652 | dev_err(&pdev->dev, "ioremap failed\n"); |
| 653 | error = -EFAULT; |
| 654 | goto err_ioremap; |
| 655 | } |
| 656 | |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 657 | /* initialize ICB addressing mode */ |
| 658 | if (pdata->addr_mode == SH_MOBILE_MERAM_MODE1) |
Laurent Pinchart | f0a260f | 2011-07-13 12:13:47 +0200 | [diff] [blame] | 659 | meram_write_reg(priv->base, MEVCR1, MEVCR1_AMD1); |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 660 | |
Laurent Pinchart | e1d1144 | 2011-09-19 11:40:31 +0200 | [diff] [blame] | 661 | platform_set_drvdata(pdev, priv); |
Damian Hobson-Garcia | 1767377 | 2011-07-04 08:06:11 +0200 | [diff] [blame] | 662 | pm_runtime_enable(&pdev->dev); |
| 663 | |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 664 | dev_info(&pdev->dev, "sh_mobile_meram initialized."); |
| 665 | |
| 666 | return 0; |
| 667 | |
Laurent Pinchart | e1d1144 | 2011-09-19 11:40:31 +0200 | [diff] [blame] | 668 | err_ioremap: |
| 669 | release_mem_region(meram->start, resource_size(meram)); |
| 670 | err_req_meram: |
| 671 | release_mem_region(regs->start, resource_size(regs)); |
| 672 | err_req_regs: |
| 673 | mutex_destroy(&priv->lock); |
| 674 | kfree(priv); |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 675 | |
| 676 | return error; |
| 677 | } |
| 678 | |
| 679 | |
| 680 | static int sh_mobile_meram_remove(struct platform_device *pdev) |
| 681 | { |
| 682 | struct sh_mobile_meram_priv *priv = platform_get_drvdata(pdev); |
Laurent Pinchart | e1d1144 | 2011-09-19 11:40:31 +0200 | [diff] [blame] | 683 | struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 684 | struct resource *meram = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 685 | |
Damian Hobson-Garcia | 1767377 | 2011-07-04 08:06:11 +0200 | [diff] [blame] | 686 | pm_runtime_disable(&pdev->dev); |
| 687 | |
Laurent Pinchart | e1d1144 | 2011-09-19 11:40:31 +0200 | [diff] [blame] | 688 | iounmap(priv->base); |
| 689 | release_mem_region(meram->start, resource_size(meram)); |
| 690 | release_mem_region(regs->start, resource_size(regs)); |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 691 | |
| 692 | mutex_destroy(&priv->lock); |
| 693 | |
| 694 | kfree(priv); |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static struct platform_driver sh_mobile_meram_driver = { |
| 700 | .driver = { |
| 701 | .name = "sh_mobile_meram", |
| 702 | .owner = THIS_MODULE, |
Damian Hobson-Garcia | 0b3bb77 | 2011-06-22 07:49:51 +0200 | [diff] [blame] | 703 | .pm = &sh_mobile_meram_dev_pm_ops, |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 704 | }, |
| 705 | .probe = sh_mobile_meram_probe, |
| 706 | .remove = sh_mobile_meram_remove, |
| 707 | }; |
| 708 | |
Axel Lin | 4277f2c | 2011-11-26 10:25:54 +0800 | [diff] [blame] | 709 | module_platform_driver(sh_mobile_meram_driver); |
Damian | 7caa434 | 2011-05-18 11:10:07 +0000 | [diff] [blame] | 710 | |
| 711 | MODULE_DESCRIPTION("SuperH Mobile MERAM driver"); |
| 712 | MODULE_AUTHOR("Damian Hobson-Garcia / Takanari Hayama"); |
| 713 | MODULE_LICENSE("GPL v2"); |