2 * SuperH Mobile MERAM Driver for SuperH Mobile LCDC Driver
4 * Copyright (c) 2011 Damian Hobson-Garcia <dhobsong@igel.co.jp>
5 * Takanari Hayama <taki@igel.co.jp>
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
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
16 #include <linux/slab.h>
17 #include <linux/platform_device.h>
19 #include "sh_mobile_meram.h"
27 #define MExxSBSIZE 0x18
29 #define MERAM_MExxCTL_VAL(ctl, next_icb, addr) \
30 ((ctl) | (((next_icb) & 0x1f) << 11) | (((addr) & 0x7ff) << 16))
31 #define MERAM_MExxBSIZE_VAL(a, b, c) \
32 (((a) << 28) | ((b) << 16) | (c))
40 #define MERAM_SEC_LINE 15
41 #define MERAM_LINE_WIDTH 2048
44 * MERAM/ICB access functions
47 #define MERAM_ICB_OFFSET(base, idx, off) \
48 ((base) + (0x400 + ((idx) * 0x20) + (off)))
50 static inline void meram_write_icb(void __iomem *base, int idx, int off,
53 iowrite32(val, MERAM_ICB_OFFSET(base, idx, off));
56 static inline unsigned long meram_read_icb(void __iomem *base, int idx, int off)
58 return ioread32(MERAM_ICB_OFFSET(base, idx, off));
61 static inline void meram_write_reg(void __iomem *base, int off,
64 iowrite32(val, base + off);
67 static inline unsigned long meram_read_reg(void __iomem *base, int off)
69 return ioread32(base + off);
76 #define MERAM_CACHE_START(p) ((p) >> 16)
77 #define MERAM_CACHE_END(p) ((p) & 0xffff)
78 #define MERAM_CACHE_SET(o, s) ((((o) & 0xffff) << 16) | \
79 (((o) + (s) - 1) & 0xffff))
82 * check if there's no overlaps in MERAM allocation.
85 static inline int meram_check_overlap(struct sh_mobile_meram_priv *priv,
86 struct sh_mobile_meram_icb *new)
89 int used_start, used_end, meram_start, meram_end;
92 if (new->marker_icb & ~0x1f || new->cache_icb & ~0x1f)
95 if (test_bit(new->marker_icb, &priv->used_icb) ||
96 test_bit(new->cache_icb, &priv->used_icb))
99 for (i = 0; i < priv->used_meram_cache_regions; i++) {
100 used_start = MERAM_CACHE_START(priv->used_meram_cache[i]);
101 used_end = MERAM_CACHE_END(priv->used_meram_cache[i]);
102 meram_start = new->meram_offset;
103 meram_end = new->meram_offset + new->meram_size;
105 if ((meram_start >= used_start && meram_start < used_end) ||
106 (meram_end > used_start && meram_end < used_end))
114 * mark the specified ICB as used
117 static inline void meram_mark(struct sh_mobile_meram_priv *priv,
118 struct sh_mobile_meram_icb *new)
122 if (new->marker_icb < 0 || new->cache_icb < 0)
125 __set_bit(new->marker_icb, &priv->used_icb);
126 __set_bit(new->cache_icb, &priv->used_icb);
128 n = priv->used_meram_cache_regions;
130 priv->used_meram_cache[n] = MERAM_CACHE_SET(new->meram_offset,
133 priv->used_meram_cache_regions++;
137 * unmark the specified ICB as used
140 static inline void meram_unmark(struct sh_mobile_meram_priv *priv,
141 struct sh_mobile_meram_icb *icb)
144 unsigned long pattern;
146 if (icb->marker_icb < 0 || icb->cache_icb < 0)
149 __clear_bit(icb->marker_icb, &priv->used_icb);
150 __clear_bit(icb->cache_icb, &priv->used_icb);
152 pattern = MERAM_CACHE_SET(icb->meram_offset, icb->meram_size);
153 for (i = 0; i < priv->used_meram_cache_regions; i++) {
154 if (priv->used_meram_cache[i] == pattern) {
155 while (i < priv->used_meram_cache_regions - 1) {
156 priv->used_meram_cache[i] =
157 priv->used_meram_cache[i + 1] ;
160 priv->used_meram_cache[i] = 0;
161 priv->used_meram_cache_regions--;
169 * set the next address to fetch
171 static inline void meram_set_next_addr(struct sh_mobile_meram_priv *priv,
172 struct sh_mobile_meram_cfg *cfg,
173 unsigned long base_addr_y,
174 unsigned long base_addr_c)
176 unsigned long target;
178 target = (cfg->current_reg) ? MExxSARA : MExxSARB;
179 cfg->current_reg ^= 1;
181 /* set the next address to fetch */
182 meram_write_icb(priv->base, cfg->icb[0].cache_icb, target,
184 meram_write_icb(priv->base, cfg->icb[0].marker_icb, target,
185 base_addr_y + cfg->icb[0].cache_unit);
187 if (cfg->pixelformat == SH_MOBILE_MERAM_PF_NV) {
188 meram_write_icb(priv->base, cfg->icb[1].cache_icb, target,
190 meram_write_icb(priv->base, cfg->icb[1].marker_icb, target,
191 base_addr_c + cfg->icb[1].cache_unit);
196 * get the next ICB address
198 static inline void meram_get_next_icb_addr(struct sh_mobile_meram_info *pdata,
199 struct sh_mobile_meram_cfg *cfg,
200 unsigned long *icb_addr_y,
201 unsigned long *icb_addr_c)
203 unsigned long icb_offset;
205 if (pdata->addr_mode == SH_MOBILE_MERAM_MODE0)
206 icb_offset = 0x80000000 | (cfg->current_reg << 29);
208 icb_offset = 0xc0000000 | (cfg->current_reg << 23);
210 *icb_addr_y = icb_offset | (cfg->icb[0].marker_icb << 24);
211 if ((*icb_addr_c) && cfg->pixelformat == SH_MOBILE_MERAM_PF_NV)
212 *icb_addr_c = icb_offset | (cfg->icb[1].marker_icb << 24);
215 #define MERAM_CALC_BYTECOUNT(x, y) \
216 (((x) * (y) + (MERAM_LINE_WIDTH - 1)) & ~(MERAM_LINE_WIDTH - 1))
222 static int meram_init(struct sh_mobile_meram_priv *priv,
223 struct sh_mobile_meram_icb *icb,
224 int xres, int yres, int *out_pitch)
226 unsigned long total_byte_count = MERAM_CALC_BYTECOUNT(xres, yres);
228 int lcdc_pitch, xpitch, line_cnt;
231 /* adjust pitch to 1024, 2048, 4096 or 8192 */
232 lcdc_pitch = (xres - 1) | 1023;
233 lcdc_pitch = lcdc_pitch | (lcdc_pitch >> 1);
234 lcdc_pitch = lcdc_pitch | (lcdc_pitch >> 2);
237 /* derive settings */
238 if (lcdc_pitch == 8192 && yres >= 1024) {
239 lcdc_pitch = xpitch = MERAM_LINE_WIDTH;
240 line_cnt = total_byte_count >> 11;
242 save_lines = (icb->meram_size / 16 / MERAM_SEC_LINE);
243 save_lines *= MERAM_SEC_LINE;
247 *out_pitch = lcdc_pitch;
248 save_lines = icb->meram_size / (lcdc_pitch >> 10) / 2;
251 bnm = (save_lines - 1) << 16;
253 /* TODO: we better to check if we have enough MERAM buffer size */
256 meram_write_icb(priv->base, icb->cache_icb, MExxBSIZE,
257 MERAM_MExxBSIZE_VAL(0x0, line_cnt - 1, xpitch - 1));
258 meram_write_icb(priv->base, icb->marker_icb, MExxBSIZE,
259 MERAM_MExxBSIZE_VAL(0xf, line_cnt - 1, xpitch - 1));
261 meram_write_icb(priv->base, icb->cache_icb, MExxMNCF, bnm);
262 meram_write_icb(priv->base, icb->marker_icb, MExxMNCF, bnm);
264 meram_write_icb(priv->base, icb->cache_icb, MExxSBSIZE, xpitch);
265 meram_write_icb(priv->base, icb->marker_icb, MExxSBSIZE, xpitch);
267 /* save a cache unit size */
268 icb->cache_unit = xres * save_lines;
271 * Set MERAM for framebuffer
273 * 0x70f: WD = 0x3, WS=0x1, CM=0x1, MD=FB mode
274 * we also chain the cache_icb and the marker_icb.
275 * we also split the allocated MERAM buffer between two ICBs.
277 meram_write_icb(priv->base, icb->cache_icb, MExxCTL,
278 MERAM_MExxCTL_VAL(0x70f, icb->marker_icb,
280 meram_write_icb(priv->base, icb->marker_icb, MExxCTL,
281 MERAM_MExxCTL_VAL(0x70f, icb->cache_icb,
283 icb->meram_size / 2));
288 static void meram_deinit(struct sh_mobile_meram_priv *priv,
289 struct sh_mobile_meram_icb *icb)
292 meram_write_icb(priv->base, icb->cache_icb, MExxCTL, 0);
293 meram_write_icb(priv->base, icb->marker_icb, MExxCTL, 0);
301 static int sh_mobile_meram_register(struct sh_mobile_meram_info *pdata,
302 struct sh_mobile_meram_cfg *cfg,
303 int xres, int yres, int pixelformat,
304 unsigned long base_addr_y,
305 unsigned long base_addr_c,
306 unsigned long *icb_addr_y,
307 unsigned long *icb_addr_c,
310 struct platform_device *pdev;
311 struct sh_mobile_meram_priv *priv;
315 if (!pdata || !pdata->priv || !pdata->pdev || !cfg)
318 if (pixelformat != SH_MOBILE_MERAM_PF_NV &&
319 pixelformat != SH_MOBILE_MERAM_PF_RGB)
325 dev_dbg(&pdev->dev, "registering %dx%d (%s) (y=%08lx, c=%08lx)",
326 xres, yres, (!pixelformat) ? "yuv" : "rgb",
327 base_addr_y, base_addr_c);
329 mutex_lock(&priv->lock);
331 /* we can't handle wider than 8192px */
333 dev_err(&pdev->dev, "width exceeding the limit (> 8192).");
338 if (priv->used_meram_cache_regions + 2 > SH_MOBILE_MERAM_ICB_NUM) {
339 dev_err(&pdev->dev, "no more ICB available.");
344 /* do we have at least one ICB config? */
345 if (cfg->icb[0].marker_icb < 0 || cfg->icb[0].cache_icb < 0) {
346 dev_err(&pdev->dev, "at least one ICB is required.");
351 /* make sure that there's no overlaps */
352 if (meram_check_overlap(priv, &cfg->icb[0])) {
353 dev_err(&pdev->dev, "conflicting config detected.");
359 /* do the same if we have the second ICB set */
360 if (cfg->icb[1].marker_icb >= 0 && cfg->icb[1].cache_icb >= 0) {
361 if (meram_check_overlap(priv, &cfg->icb[1])) {
362 dev_err(&pdev->dev, "conflicting config detected.");
369 if (pixelformat == SH_MOBILE_MERAM_PF_NV && n != 2) {
370 dev_err(&pdev->dev, "requires two ICB sets for planar Y/C.");
375 /* we now register the ICB */
376 cfg->pixelformat = pixelformat;
377 meram_mark(priv, &cfg->icb[0]);
378 if (pixelformat == SH_MOBILE_MERAM_PF_NV)
379 meram_mark(priv, &cfg->icb[1]);
381 /* initialize MERAM */
382 meram_init(priv, &cfg->icb[0], xres, yres, &out_pitch);
384 if (pixelformat == SH_MOBILE_MERAM_PF_NV)
385 meram_init(priv, &cfg->icb[1], xres, (yres + 1) / 2,
388 cfg->current_reg = 1;
389 meram_set_next_addr(priv, cfg, base_addr_y, base_addr_c);
390 meram_get_next_icb_addr(pdata, cfg, icb_addr_y, icb_addr_c);
392 dev_dbg(&pdev->dev, "registered - can access via y=%08lx, c=%08lx",
393 *icb_addr_y, *icb_addr_c);
396 mutex_unlock(&priv->lock);
400 static int sh_mobile_meram_unregister(struct sh_mobile_meram_info *pdata,
401 struct sh_mobile_meram_cfg *cfg)
403 struct sh_mobile_meram_priv *priv;
405 if (!pdata || !pdata->priv || !cfg)
410 mutex_lock(&priv->lock);
412 /* deinit & unmark */
413 if (cfg->pixelformat == SH_MOBILE_MERAM_PF_NV) {
414 meram_deinit(priv, &cfg->icb[1]);
415 meram_unmark(priv, &cfg->icb[1]);
417 meram_deinit(priv, &cfg->icb[0]);
418 meram_unmark(priv, &cfg->icb[0]);
420 mutex_unlock(&priv->lock);
425 static int sh_mobile_meram_update(struct sh_mobile_meram_info *pdata,
426 struct sh_mobile_meram_cfg *cfg,
427 unsigned long base_addr_y,
428 unsigned long base_addr_c,
429 unsigned long *icb_addr_y,
430 unsigned long *icb_addr_c)
432 struct sh_mobile_meram_priv *priv;
434 if (!pdata || !pdata->priv || !cfg)
439 mutex_lock(&priv->lock);
441 meram_set_next_addr(priv, cfg, base_addr_y, base_addr_c);
442 meram_get_next_icb_addr(pdata, cfg, icb_addr_y, icb_addr_c);
444 mutex_unlock(&priv->lock);
449 static struct sh_mobile_meram_ops sh_mobile_meram_ops = {
450 .module = THIS_MODULE,
451 .meram_register = sh_mobile_meram_register,
452 .meram_unregister = sh_mobile_meram_unregister,
453 .meram_update = sh_mobile_meram_update,
460 static int sh_mobile_meram_remove(struct platform_device *pdev);
462 static int __devinit sh_mobile_meram_probe(struct platform_device *pdev)
464 struct sh_mobile_meram_priv *priv;
465 struct sh_mobile_meram_info *pdata = pdev->dev.platform_data;
466 struct resource *res;
470 dev_err(&pdev->dev, "no platform data defined\n");
474 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
476 dev_err(&pdev->dev, "cannot get platform resources\n");
480 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
482 dev_err(&pdev->dev, "cannot allocate device data\n");
486 platform_set_drvdata(pdev, priv);
488 /* initialize private data */
489 mutex_init(&priv->lock);
490 priv->base = ioremap_nocache(res->start, resource_size(res));
492 dev_err(&pdev->dev, "ioremap failed\n");
496 pdata->ops = &sh_mobile_meram_ops;
500 /* initialize ICB addressing mode */
501 if (pdata->addr_mode == SH_MOBILE_MERAM_MODE1)
502 meram_write_reg(priv->base, MEVCR1, 1 << 29);
504 dev_info(&pdev->dev, "sh_mobile_meram initialized.");
509 sh_mobile_meram_remove(pdev);
515 static int sh_mobile_meram_remove(struct platform_device *pdev)
517 struct sh_mobile_meram_priv *priv = platform_get_drvdata(pdev);
522 mutex_destroy(&priv->lock);
529 static struct platform_driver sh_mobile_meram_driver = {
531 .name = "sh_mobile_meram",
532 .owner = THIS_MODULE,
534 .probe = sh_mobile_meram_probe,
535 .remove = sh_mobile_meram_remove,
538 static int __init sh_mobile_meram_init(void)
540 return platform_driver_register(&sh_mobile_meram_driver);
543 static void __exit sh_mobile_meram_exit(void)
545 platform_driver_unregister(&sh_mobile_meram_driver);
548 module_init(sh_mobile_meram_init);
549 module_exit(sh_mobile_meram_exit);
551 MODULE_DESCRIPTION("SuperH Mobile MERAM driver");
552 MODULE_AUTHOR("Damian Hobson-Garcia / Takanari Hayama");
553 MODULE_LICENSE("GPL v2");