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