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