blob: 548f70096124b35453efd85503dd3c0d30c96a76 [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>
Laurent Pinchart8a209742011-07-13 12:13:47 +020019#include <video/sh_mobile_meram.h>
Damian7caa4342011-05-18 11:10:07 +000020
21/* meram registers */
Laurent Pinchartf0a260f2011-07-13 12:13:47 +020022#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
Damian7caa4342011-05-18 11:10:07 +000029
Laurent Pinchartf0a260f2011-07-13 12:13:47 +020030#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
Damian7caa4342011-05-18 11:10:07 +000075
Laurent Pinchartf0a260f2011-07-13 12:13:47 +020076#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))
Damian7caa4342011-05-18 11:10:07 +000083
Laurent Pinchart8a209742011-07-13 12:13:47 +020084#define SH_MOBILE_MERAM_ICB_NUM 32
85
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +020086static unsigned long common_regs[] = {
87 MEVCR1,
88 MEQSEL1,
89 MEQSEL2,
90};
91#define CMN_REGS_SIZE ARRAY_SIZE(common_regs)
92
93static 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-Garcia0aa492b2011-06-22 07:49:50 +0200103struct sh_mobile_meram_priv {
104 void __iomem *base;
105 struct mutex lock;
106 unsigned long used_icb;
Laurent Pinchart05432832011-09-19 11:40:31 +0200107 unsigned int used_meram_cache_regions;
Damian Hobson-Garcia0aa492b2011-06-22 07:49:50 +0200108 unsigned long used_meram_cache[SH_MOBILE_MERAM_ICB_NUM];
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200109 unsigned long cmn_saved_regs[CMN_REGS_SIZE];
110 unsigned long icb_saved_regs[ICB_REGS_SIZE * SH_MOBILE_MERAM_ICB_NUM];
Damian Hobson-Garcia0aa492b2011-06-22 07:49:50 +0200111};
112
Damian7caa4342011-05-18 11:10:07 +0000113/* settings */
114#define MERAM_SEC_LINE 15
115#define MERAM_LINE_WIDTH 2048
116
117/*
118 * MERAM/ICB access functions
119 */
120
Laurent Pinchartf0a260f2011-07-13 12:13:47 +0200121#define MERAM_ICB_OFFSET(base, idx, off) ((base) + (off) + (idx) * 0x20)
Damian7caa4342011-05-18 11:10:07 +0000122
Laurent Pinchart05432832011-09-19 11:40:31 +0200123static inline void meram_write_icb(void __iomem *base, unsigned int idx,
124 unsigned int off, unsigned long val)
Damian7caa4342011-05-18 11:10:07 +0000125{
126 iowrite32(val, MERAM_ICB_OFFSET(base, idx, off));
127}
128
Laurent Pinchart05432832011-09-19 11:40:31 +0200129static inline unsigned long meram_read_icb(void __iomem *base, unsigned int idx,
130 unsigned int off)
Damian7caa4342011-05-18 11:10:07 +0000131{
132 return ioread32(MERAM_ICB_OFFSET(base, idx, off));
133}
134
Laurent Pinchart05432832011-09-19 11:40:31 +0200135static inline void meram_write_reg(void __iomem *base, unsigned int off,
136 unsigned long val)
Damian7caa4342011-05-18 11:10:07 +0000137{
138 iowrite32(val, base + off);
139}
140
Laurent Pinchart05432832011-09-19 11:40:31 +0200141static inline unsigned long meram_read_reg(void __iomem *base, unsigned int off)
Damian7caa4342011-05-18 11:10:07 +0000142{
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
159static inline int meram_check_overlap(struct sh_mobile_meram_priv *priv,
Laurent Pinchartd272f422011-09-19 11:40:31 +0200160 struct sh_mobile_meram_icb_cfg *new)
Damian7caa4342011-05-18 11:10:07 +0000161{
Laurent Pinchart05432832011-09-19 11:40:31 +0200162 unsigned int used_start, used_end, meram_start, meram_end;
163 unsigned int i;
Damian7caa4342011-05-18 11:10:07 +0000164
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
191static inline void meram_mark(struct sh_mobile_meram_priv *priv,
Laurent Pinchartd272f422011-09-19 11:40:31 +0200192 struct sh_mobile_meram_icb_cfg *new)
Damian7caa4342011-05-18 11:10:07 +0000193{
Laurent Pinchart05432832011-09-19 11:40:31 +0200194 unsigned int n;
Damian7caa4342011-05-18 11:10:07 +0000195
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
214static inline void meram_unmark(struct sh_mobile_meram_priv *priv,
Laurent Pinchartd272f422011-09-19 11:40:31 +0200215 struct sh_mobile_meram_icb_cfg *icb)
Damian7caa4342011-05-18 11:10:07 +0000216{
Damian7caa4342011-05-18 11:10:07 +0000217 unsigned long pattern;
Laurent Pinchart05432832011-09-19 11:40:31 +0200218 unsigned int i;
Damian7caa4342011-05-18 11:10:07 +0000219
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
Damian3fedd2a2011-05-18 11:10:08 +0000241/*
242 * is this a YCbCr(NV12, NV16 or NV24) colorspace
243 */
244static 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}
Damian7caa4342011-05-18 11:10:07 +0000251
252/*
253 * set the next address to fetch
254 */
255static 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
Damian3fedd2a2011-05-18 11:10:08 +0000271 if (is_nvcolor(cfg->pixelformat)) {
Damian7caa4342011-05-18 11:10:07 +0000272 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 */
282static 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-Garcia06c8a6a2011-06-22 07:46:25 +0000295 if (is_nvcolor(cfg->pixelformat))
Damian7caa4342011-05-18 11:10:07 +0000296 *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
306static int meram_init(struct sh_mobile_meram_priv *priv,
Laurent Pinchartd272f422011-09-19 11:40:31 +0200307 struct sh_mobile_meram_icb_cfg *icb,
Laurent Pinchart05432832011-09-19 11:40:31 +0200308 unsigned int xres, unsigned int yres,
309 unsigned int *out_pitch)
Damian7caa4342011-05-18 11:10:07 +0000310{
311 unsigned long total_byte_count = MERAM_CALC_BYTECOUNT(xres, yres);
312 unsigned long bnm;
Laurent Pinchart05432832011-09-19 11:40:31 +0200313 unsigned int lcdc_pitch;
314 unsigned int xpitch;
315 unsigned int line_cnt;
316 unsigned int save_lines;
Damian7caa4342011-05-18 11:10:07 +0000317
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 *
Damian7caa4342011-05-18 11:10:07 +0000360 * 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 Pinchartf0a260f2011-07-13 12:13:47 +0200364 MERAM_MExxCTL_VAL(icb->marker_icb, icb->meram_offset) |
365 MExxCTL_WD1 | MExxCTL_WD0 | MExxCTL_WS | MExxCTL_CM |
366 MExxCTL_MD_FB);
Damian7caa4342011-05-18 11:10:07 +0000367 meram_write_icb(priv->base, icb->marker_icb, MExxCTL,
Laurent Pinchartf0a260f2011-07-13 12:13:47 +0200368 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);
Damian7caa4342011-05-18 11:10:07 +0000372
373 return 0;
374}
375
376static void meram_deinit(struct sh_mobile_meram_priv *priv,
Laurent Pinchartd272f422011-09-19 11:40:31 +0200377 struct sh_mobile_meram_icb_cfg *icb)
Damian7caa4342011-05-18 11:10:07 +0000378{
379 /* disable ICB */
Laurent Pinchartda6cf512011-08-31 13:00:52 +0200380 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);
Damian7caa4342011-05-18 11:10:07 +0000384 icb->cache_unit = 0;
385}
386
387/*
388 * register the ICB
389 */
390
391static int sh_mobile_meram_register(struct sh_mobile_meram_info *pdata,
392 struct sh_mobile_meram_cfg *cfg,
Laurent Pinchart05432832011-09-19 11:40:31 +0200393 unsigned int xres, unsigned int yres,
394 unsigned int pixelformat,
Damian7caa4342011-05-18 11:10:07 +0000395 unsigned long base_addr_y,
396 unsigned long base_addr_c,
397 unsigned long *icb_addr_y,
398 unsigned long *icb_addr_c,
Laurent Pinchart05432832011-09-19 11:40:31 +0200399 unsigned int *pitch)
Damian7caa4342011-05-18 11:10:07 +0000400{
401 struct platform_device *pdev;
402 struct sh_mobile_meram_priv *priv;
Laurent Pinchart05432832011-09-19 11:40:31 +0200403 unsigned int out_pitch;
404 unsigned int n;
Damian7caa4342011-05-18 11:10:07 +0000405 int error = 0;
406
407 if (!pdata || !pdata->priv || !pdata->pdev || !cfg)
408 return -EINVAL;
409
410 if (pixelformat != SH_MOBILE_MERAM_PF_NV &&
Damian3fedd2a2011-05-18 11:10:08 +0000411 pixelformat != SH_MOBILE_MERAM_PF_NV24 &&
Damian7caa4342011-05-18 11:10:07 +0000412 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
Damian7caa4342011-05-18 11:10:07 +0000422 /* we can't handle wider than 8192px */
423 if (xres > 8192) {
424 dev_err(&pdev->dev, "width exceeding the limit (> 8192).");
Laurent Pinchart7963e212011-07-13 12:13:47 +0200425 return -EINVAL;
Damian7caa4342011-05-18 11:10:07 +0000426 }
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 Pinchart7963e212011-07-13 12:13:47 +0200431 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.");
Damian7caa4342011-05-18 11:10:07 +0000438 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
Damian3fedd2a2011-05-18 11:10:08 +0000460 if (is_nvcolor(pixelformat) && n != 2) {
Damian7caa4342011-05-18 11:10:07 +0000461 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]);
Damian3fedd2a2011-05-18 11:10:08 +0000469 if (is_nvcolor(pixelformat))
Damian7caa4342011-05-18 11:10:07 +0000470 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);
Damian3fedd2a2011-05-18 11:10:08 +0000478 else if (pixelformat == SH_MOBILE_MERAM_PF_NV24)
479 meram_init(priv, &cfg->icb[1], 2 * xres, (yres + 1) / 2,
480 &out_pitch);
Damian7caa4342011-05-18 11:10:07 +0000481
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
489err:
490 mutex_unlock(&priv->lock);
491 return error;
492}
493
494static 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 */
Damian3fedd2a2011-05-18 11:10:08 +0000507 if (is_nvcolor(cfg->pixelformat)) {
Damian7caa4342011-05-18 11:10:07 +0000508 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
519static 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-Garcia0b3bb772011-06-22 07:49:51 +0200543static 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 Pinchart05432832011-09-19 11:40:31 +0200547 unsigned int i, j;
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200548
Laurent Pinchart05432832011-09-19 11:40:31 +0200549 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-Garcia0b3bb772011-06-22 07:49:51 +0200552
Laurent Pinchart05432832011-09-19 11:40:31 +0200553 for (i = 0; i < 32; i++) {
554 if (!test_bit(i, &priv->used_icb))
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200555 continue;
Laurent Pinchart05432832011-09-19 11:40:31 +0200556 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-Garcia0b3bb772011-06-22 07:49:51 +0200559 /* Reset ICB on resume */
Laurent Pinchart05432832011-09-19 11:40:31 +0200560 if (icb_regs[j] == MExxCTL)
561 priv->icb_saved_regs[i * ICB_REGS_SIZE + j] |=
Laurent Pinchartf0a260f2011-07-13 12:13:47 +0200562 MExxCTL_WBF | MExxCTL_WF | MExxCTL_RF;
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200563 }
564 }
565 return 0;
566}
567
568static 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 Pinchart05432832011-09-19 11:40:31 +0200572 unsigned int i, j;
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200573
Laurent Pinchart05432832011-09-19 11:40:31 +0200574 for (i = 0; i < 32; i++) {
575 if (!test_bit(i, &priv->used_icb))
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200576 continue;
Laurent Pinchart05432832011-09-19 11:40:31 +0200577 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-Garcia0b3bb772011-06-22 07:49:51 +0200580 }
581 }
582
Laurent Pinchart05432832011-09-19 11:40:31 +0200583 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-Garcia0b3bb772011-06-22 07:49:51 +0200586 return 0;
587}
588
589static 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
Damian7caa4342011-05-18 11:10:07 +0000594static 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
Damian7caa4342011-05-18 11:10:07 +0000605static 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 Pincharte1d11442011-09-19 11:40:31 +0200609 struct resource *regs;
610 struct resource *meram;
Damian7caa4342011-05-18 11:10:07 +0000611 int error;
612
613 if (!pdata) {
614 dev_err(&pdev->dev, "no platform data defined\n");
615 return -EINVAL;
616 }
617
Laurent Pincharte1d11442011-09-19 11:40:31 +0200618 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
619 meram = platform_get_resource(pdev, IORESOURCE_MEM, 1);
620 if (regs == NULL || meram == NULL) {
Damian7caa4342011-05-18 11:10:07 +0000621 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
Damian7caa4342011-05-18 11:10:07 +0000631 /* initialize private data */
632 mutex_init(&priv->lock);
Damian7caa4342011-05-18 11:10:07 +0000633 pdata->ops = &sh_mobile_meram_ops;
634 pdata->priv = priv;
635 pdata->pdev = pdev;
636
Laurent Pincharte1d11442011-09-19 11:40:31 +0200637 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
Damian7caa4342011-05-18 11:10:07 +0000657 /* initialize ICB addressing mode */
658 if (pdata->addr_mode == SH_MOBILE_MERAM_MODE1)
Laurent Pinchartf0a260f2011-07-13 12:13:47 +0200659 meram_write_reg(priv->base, MEVCR1, MEVCR1_AMD1);
Damian7caa4342011-05-18 11:10:07 +0000660
Laurent Pincharte1d11442011-09-19 11:40:31 +0200661 platform_set_drvdata(pdev, priv);
Damian Hobson-Garcia17673772011-07-04 08:06:11 +0200662 pm_runtime_enable(&pdev->dev);
663
Damian7caa4342011-05-18 11:10:07 +0000664 dev_info(&pdev->dev, "sh_mobile_meram initialized.");
665
666 return 0;
667
Laurent Pincharte1d11442011-09-19 11:40:31 +0200668err_ioremap:
669 release_mem_region(meram->start, resource_size(meram));
670err_req_meram:
671 release_mem_region(regs->start, resource_size(regs));
672err_req_regs:
673 mutex_destroy(&priv->lock);
674 kfree(priv);
Damian7caa4342011-05-18 11:10:07 +0000675
676 return error;
677}
678
679
680static int sh_mobile_meram_remove(struct platform_device *pdev)
681{
682 struct sh_mobile_meram_priv *priv = platform_get_drvdata(pdev);
Laurent Pincharte1d11442011-09-19 11:40:31 +0200683 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
684 struct resource *meram = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Damian7caa4342011-05-18 11:10:07 +0000685
Damian Hobson-Garcia17673772011-07-04 08:06:11 +0200686 pm_runtime_disable(&pdev->dev);
687
Laurent Pincharte1d11442011-09-19 11:40:31 +0200688 iounmap(priv->base);
689 release_mem_region(meram->start, resource_size(meram));
690 release_mem_region(regs->start, resource_size(regs));
Damian7caa4342011-05-18 11:10:07 +0000691
692 mutex_destroy(&priv->lock);
693
694 kfree(priv);
695
696 return 0;
697}
698
699static struct platform_driver sh_mobile_meram_driver = {
700 .driver = {
701 .name = "sh_mobile_meram",
702 .owner = THIS_MODULE,
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200703 .pm = &sh_mobile_meram_dev_pm_ops,
Damian7caa4342011-05-18 11:10:07 +0000704 },
705 .probe = sh_mobile_meram_probe,
706 .remove = sh_mobile_meram_remove,
707};
708
Axel Lin4277f2c2011-11-26 10:25:54 +0800709module_platform_driver(sh_mobile_meram_driver);
Damian7caa4342011-05-18 11:10:07 +0000710
711MODULE_DESCRIPTION("SuperH Mobile MERAM driver");
712MODULE_AUTHOR("Damian Hobson-Garcia / Takanari Hayama");
713MODULE_LICENSE("GPL v2");