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