TTY: serial, move locking in uart_close
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / video / sh_mobile_meram.c
blobcc7d7329dc151af821153feb999998e42460267e
1 /*
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
9 * for more details.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/io.h>
16 #include <linux/slab.h>
17 #include <linux/platform_device.h>
19 #include "sh_mobile_meram.h"
21 /* meram registers */
22 #define MExxCTL 0x0
23 #define MExxBSIZE 0x4
24 #define MExxMNCF 0x8
25 #define MExxSARA 0x10
26 #define MExxSARB 0x14
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))
34 #define MEVCR1 0x4
35 #define MEACTS 0x10
36 #define MEQSEL1 0x40
37 #define MEQSEL2 0x44
39 /* settings */
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,
51 unsigned long val)
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,
62 unsigned long val)
64 iowrite32(val, base + off);
67 static inline unsigned long meram_read_reg(void __iomem *base, int off)
69 return ioread32(base + off);
73 * register ICB
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)
88 int i;
89 int used_start, used_end, meram_start, meram_end;
91 /* valid ICB? */
92 if (new->marker_icb & ~0x1f || new->cache_icb & ~0x1f)
93 return 1;
95 if (test_bit(new->marker_icb, &priv->used_icb) ||
96 test_bit(new->cache_icb, &priv->used_icb))
97 return 1;
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))
107 return 1;
110 return 0;
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)
120 int n;
122 if (new->marker_icb < 0 || new->cache_icb < 0)
123 return;
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,
131 new->meram_size);
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)
143 int i;
144 unsigned long pattern;
146 if (icb->marker_icb < 0 || icb->cache_icb < 0)
147 return;
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] ;
158 i++;
160 priv->used_meram_cache[i] = 0;
161 priv->used_meram_cache_regions--;
162 break;
168 * is this a YCbCr(NV12, NV16 or NV24) colorspace
170 static inline int is_nvcolor(int cspace)
172 if (cspace == SH_MOBILE_MERAM_PF_NV ||
173 cspace == SH_MOBILE_MERAM_PF_NV24)
174 return 1;
175 return 0;
179 * set the next address to fetch
181 static inline void meram_set_next_addr(struct sh_mobile_meram_priv *priv,
182 struct sh_mobile_meram_cfg *cfg,
183 unsigned long base_addr_y,
184 unsigned long base_addr_c)
186 unsigned long target;
188 target = (cfg->current_reg) ? MExxSARA : MExxSARB;
189 cfg->current_reg ^= 1;
191 /* set the next address to fetch */
192 meram_write_icb(priv->base, cfg->icb[0].cache_icb, target,
193 base_addr_y);
194 meram_write_icb(priv->base, cfg->icb[0].marker_icb, target,
195 base_addr_y + cfg->icb[0].cache_unit);
197 if (is_nvcolor(cfg->pixelformat)) {
198 meram_write_icb(priv->base, cfg->icb[1].cache_icb, target,
199 base_addr_c);
200 meram_write_icb(priv->base, cfg->icb[1].marker_icb, target,
201 base_addr_c + cfg->icb[1].cache_unit);
206 * get the next ICB address
208 static inline void meram_get_next_icb_addr(struct sh_mobile_meram_info *pdata,
209 struct sh_mobile_meram_cfg *cfg,
210 unsigned long *icb_addr_y,
211 unsigned long *icb_addr_c)
213 unsigned long icb_offset;
215 if (pdata->addr_mode == SH_MOBILE_MERAM_MODE0)
216 icb_offset = 0x80000000 | (cfg->current_reg << 29);
217 else
218 icb_offset = 0xc0000000 | (cfg->current_reg << 23);
220 *icb_addr_y = icb_offset | (cfg->icb[0].marker_icb << 24);
221 if (is_nvcolor(cfg->pixelformat))
222 *icb_addr_c = icb_offset | (cfg->icb[1].marker_icb << 24);
225 #define MERAM_CALC_BYTECOUNT(x, y) \
226 (((x) * (y) + (MERAM_LINE_WIDTH - 1)) & ~(MERAM_LINE_WIDTH - 1))
229 * initialize MERAM
232 static int meram_init(struct sh_mobile_meram_priv *priv,
233 struct sh_mobile_meram_icb *icb,
234 int xres, int yres, int *out_pitch)
236 unsigned long total_byte_count = MERAM_CALC_BYTECOUNT(xres, yres);
237 unsigned long bnm;
238 int lcdc_pitch, xpitch, line_cnt;
239 int save_lines;
241 /* adjust pitch to 1024, 2048, 4096 or 8192 */
242 lcdc_pitch = (xres - 1) | 1023;
243 lcdc_pitch = lcdc_pitch | (lcdc_pitch >> 1);
244 lcdc_pitch = lcdc_pitch | (lcdc_pitch >> 2);
245 lcdc_pitch += 1;
247 /* derive settings */
248 if (lcdc_pitch == 8192 && yres >= 1024) {
249 lcdc_pitch = xpitch = MERAM_LINE_WIDTH;
250 line_cnt = total_byte_count >> 11;
251 *out_pitch = xres;
252 save_lines = (icb->meram_size / 16 / MERAM_SEC_LINE);
253 save_lines *= MERAM_SEC_LINE;
254 } else {
255 xpitch = xres;
256 line_cnt = yres;
257 *out_pitch = lcdc_pitch;
258 save_lines = icb->meram_size / (lcdc_pitch >> 10) / 2;
259 save_lines &= 0xff;
261 bnm = (save_lines - 1) << 16;
263 /* TODO: we better to check if we have enough MERAM buffer size */
265 /* set up ICB */
266 meram_write_icb(priv->base, icb->cache_icb, MExxBSIZE,
267 MERAM_MExxBSIZE_VAL(0x0, line_cnt - 1, xpitch - 1));
268 meram_write_icb(priv->base, icb->marker_icb, MExxBSIZE,
269 MERAM_MExxBSIZE_VAL(0xf, line_cnt - 1, xpitch - 1));
271 meram_write_icb(priv->base, icb->cache_icb, MExxMNCF, bnm);
272 meram_write_icb(priv->base, icb->marker_icb, MExxMNCF, bnm);
274 meram_write_icb(priv->base, icb->cache_icb, MExxSBSIZE, xpitch);
275 meram_write_icb(priv->base, icb->marker_icb, MExxSBSIZE, xpitch);
277 /* save a cache unit size */
278 icb->cache_unit = xres * save_lines;
281 * Set MERAM for framebuffer
283 * 0x70f: WD = 0x3, WS=0x1, CM=0x1, MD=FB mode
284 * we also chain the cache_icb and the marker_icb.
285 * we also split the allocated MERAM buffer between two ICBs.
287 meram_write_icb(priv->base, icb->cache_icb, MExxCTL,
288 MERAM_MExxCTL_VAL(0x70f, icb->marker_icb,
289 icb->meram_offset));
290 meram_write_icb(priv->base, icb->marker_icb, MExxCTL,
291 MERAM_MExxCTL_VAL(0x70f, icb->cache_icb,
292 icb->meram_offset +
293 icb->meram_size / 2));
295 return 0;
298 static void meram_deinit(struct sh_mobile_meram_priv *priv,
299 struct sh_mobile_meram_icb *icb)
301 /* disable ICB */
302 meram_write_icb(priv->base, icb->cache_icb, MExxCTL, 0);
303 meram_write_icb(priv->base, icb->marker_icb, MExxCTL, 0);
304 icb->cache_unit = 0;
308 * register the ICB
311 static int sh_mobile_meram_register(struct sh_mobile_meram_info *pdata,
312 struct sh_mobile_meram_cfg *cfg,
313 int xres, int yres, int pixelformat,
314 unsigned long base_addr_y,
315 unsigned long base_addr_c,
316 unsigned long *icb_addr_y,
317 unsigned long *icb_addr_c,
318 int *pitch)
320 struct platform_device *pdev;
321 struct sh_mobile_meram_priv *priv;
322 int n, out_pitch;
323 int error = 0;
325 if (!pdata || !pdata->priv || !pdata->pdev || !cfg)
326 return -EINVAL;
328 if (pixelformat != SH_MOBILE_MERAM_PF_NV &&
329 pixelformat != SH_MOBILE_MERAM_PF_NV24 &&
330 pixelformat != SH_MOBILE_MERAM_PF_RGB)
331 return -EINVAL;
333 priv = pdata->priv;
334 pdev = pdata->pdev;
336 dev_dbg(&pdev->dev, "registering %dx%d (%s) (y=%08lx, c=%08lx)",
337 xres, yres, (!pixelformat) ? "yuv" : "rgb",
338 base_addr_y, base_addr_c);
340 mutex_lock(&priv->lock);
342 /* we can't handle wider than 8192px */
343 if (xres > 8192) {
344 dev_err(&pdev->dev, "width exceeding the limit (> 8192).");
345 error = -EINVAL;
346 goto err;
349 if (priv->used_meram_cache_regions + 2 > SH_MOBILE_MERAM_ICB_NUM) {
350 dev_err(&pdev->dev, "no more ICB available.");
351 error = -EINVAL;
352 goto err;
355 /* do we have at least one ICB config? */
356 if (cfg->icb[0].marker_icb < 0 || cfg->icb[0].cache_icb < 0) {
357 dev_err(&pdev->dev, "at least one ICB is required.");
358 error = -EINVAL;
359 goto err;
362 /* make sure that there's no overlaps */
363 if (meram_check_overlap(priv, &cfg->icb[0])) {
364 dev_err(&pdev->dev, "conflicting config detected.");
365 error = -EINVAL;
366 goto err;
368 n = 1;
370 /* do the same if we have the second ICB set */
371 if (cfg->icb[1].marker_icb >= 0 && cfg->icb[1].cache_icb >= 0) {
372 if (meram_check_overlap(priv, &cfg->icb[1])) {
373 dev_err(&pdev->dev, "conflicting config detected.");
374 error = -EINVAL;
375 goto err;
377 n = 2;
380 if (is_nvcolor(pixelformat) && n != 2) {
381 dev_err(&pdev->dev, "requires two ICB sets for planar Y/C.");
382 error = -EINVAL;
383 goto err;
386 /* we now register the ICB */
387 cfg->pixelformat = pixelformat;
388 meram_mark(priv, &cfg->icb[0]);
389 if (is_nvcolor(pixelformat))
390 meram_mark(priv, &cfg->icb[1]);
392 /* initialize MERAM */
393 meram_init(priv, &cfg->icb[0], xres, yres, &out_pitch);
394 *pitch = out_pitch;
395 if (pixelformat == SH_MOBILE_MERAM_PF_NV)
396 meram_init(priv, &cfg->icb[1], xres, (yres + 1) / 2,
397 &out_pitch);
398 else if (pixelformat == SH_MOBILE_MERAM_PF_NV24)
399 meram_init(priv, &cfg->icb[1], 2 * xres, (yres + 1) / 2,
400 &out_pitch);
402 cfg->current_reg = 1;
403 meram_set_next_addr(priv, cfg, base_addr_y, base_addr_c);
404 meram_get_next_icb_addr(pdata, cfg, icb_addr_y, icb_addr_c);
406 dev_dbg(&pdev->dev, "registered - can access via y=%08lx, c=%08lx",
407 *icb_addr_y, *icb_addr_c);
409 err:
410 mutex_unlock(&priv->lock);
411 return error;
414 static int sh_mobile_meram_unregister(struct sh_mobile_meram_info *pdata,
415 struct sh_mobile_meram_cfg *cfg)
417 struct sh_mobile_meram_priv *priv;
419 if (!pdata || !pdata->priv || !cfg)
420 return -EINVAL;
422 priv = pdata->priv;
424 mutex_lock(&priv->lock);
426 /* deinit & unmark */
427 if (is_nvcolor(cfg->pixelformat)) {
428 meram_deinit(priv, &cfg->icb[1]);
429 meram_unmark(priv, &cfg->icb[1]);
431 meram_deinit(priv, &cfg->icb[0]);
432 meram_unmark(priv, &cfg->icb[0]);
434 mutex_unlock(&priv->lock);
436 return 0;
439 static int sh_mobile_meram_update(struct sh_mobile_meram_info *pdata,
440 struct sh_mobile_meram_cfg *cfg,
441 unsigned long base_addr_y,
442 unsigned long base_addr_c,
443 unsigned long *icb_addr_y,
444 unsigned long *icb_addr_c)
446 struct sh_mobile_meram_priv *priv;
448 if (!pdata || !pdata->priv || !cfg)
449 return -EINVAL;
451 priv = pdata->priv;
453 mutex_lock(&priv->lock);
455 meram_set_next_addr(priv, cfg, base_addr_y, base_addr_c);
456 meram_get_next_icb_addr(pdata, cfg, icb_addr_y, icb_addr_c);
458 mutex_unlock(&priv->lock);
460 return 0;
463 static struct sh_mobile_meram_ops sh_mobile_meram_ops = {
464 .module = THIS_MODULE,
465 .meram_register = sh_mobile_meram_register,
466 .meram_unregister = sh_mobile_meram_unregister,
467 .meram_update = sh_mobile_meram_update,
471 * initialize MERAM
474 static int sh_mobile_meram_remove(struct platform_device *pdev);
476 static int __devinit sh_mobile_meram_probe(struct platform_device *pdev)
478 struct sh_mobile_meram_priv *priv;
479 struct sh_mobile_meram_info *pdata = pdev->dev.platform_data;
480 struct resource *res;
481 int error;
483 if (!pdata) {
484 dev_err(&pdev->dev, "no platform data defined\n");
485 return -EINVAL;
488 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
489 if (!res) {
490 dev_err(&pdev->dev, "cannot get platform resources\n");
491 return -ENOENT;
494 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
495 if (!priv) {
496 dev_err(&pdev->dev, "cannot allocate device data\n");
497 return -ENOMEM;
500 platform_set_drvdata(pdev, priv);
502 /* initialize private data */
503 mutex_init(&priv->lock);
504 priv->base = ioremap_nocache(res->start, resource_size(res));
505 if (!priv->base) {
506 dev_err(&pdev->dev, "ioremap failed\n");
507 error = -EFAULT;
508 goto err;
510 pdata->ops = &sh_mobile_meram_ops;
511 pdata->priv = priv;
512 pdata->pdev = pdev;
514 /* initialize ICB addressing mode */
515 if (pdata->addr_mode == SH_MOBILE_MERAM_MODE1)
516 meram_write_reg(priv->base, MEVCR1, 1 << 29);
518 dev_info(&pdev->dev, "sh_mobile_meram initialized.");
520 return 0;
522 err:
523 sh_mobile_meram_remove(pdev);
525 return error;
529 static int sh_mobile_meram_remove(struct platform_device *pdev)
531 struct sh_mobile_meram_priv *priv = platform_get_drvdata(pdev);
533 if (priv->base)
534 iounmap(priv->base);
536 mutex_destroy(&priv->lock);
538 kfree(priv);
540 return 0;
543 static struct platform_driver sh_mobile_meram_driver = {
544 .driver = {
545 .name = "sh_mobile_meram",
546 .owner = THIS_MODULE,
548 .probe = sh_mobile_meram_probe,
549 .remove = sh_mobile_meram_remove,
552 static int __init sh_mobile_meram_init(void)
554 return platform_driver_register(&sh_mobile_meram_driver);
557 static void __exit sh_mobile_meram_exit(void)
559 platform_driver_unregister(&sh_mobile_meram_driver);
562 module_init(sh_mobile_meram_init);
563 module_exit(sh_mobile_meram_exit);
565 MODULE_DESCRIPTION("SuperH Mobile MERAM driver");
566 MODULE_AUTHOR("Damian Hobson-Garcia / Takanari Hayama");
567 MODULE_LICENSE("GPL v2");