blob: 82ba830bf95d51631776512ff0e12f124da8765f [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
Laurent Pinchart75543402011-09-19 11:40:31 +020012#include <linux/device.h>
Laurent Pinchart48110052011-12-12 16:36:13 +010013#include <linux/err.h>
Laurent Pinchart974d250b2011-09-19 11:40:31 +020014#include <linux/genalloc.h>
Laurent Pinchart75543402011-09-19 11:40:31 +020015#include <linux/io.h>
Damian7caa4342011-05-18 11:10:07 +000016#include <linux/kernel.h>
17#include <linux/module.h>
Damian7caa4342011-05-18 11:10:07 +000018#include <linux/platform_device.h>
Laurent Pinchart75543402011-09-19 11:40:31 +020019#include <linux/pm_runtime.h>
20#include <linux/slab.h>
21
Laurent Pinchart8a209742011-07-13 12:13:47 +020022#include <video/sh_mobile_meram.h>
Damian7caa4342011-05-18 11:10:07 +000023
Laurent Pinchart75543402011-09-19 11:40:31 +020024/* -----------------------------------------------------------------------------
25 * MERAM registers
26 */
27
Laurent Pinchartf0a260f2011-07-13 12:13:47 +020028#define MEVCR1 0x4
29#define MEVCR1_RST (1 << 31)
30#define MEVCR1_WD (1 << 30)
31#define MEVCR1_AMD1 (1 << 29)
32#define MEVCR1_AMD0 (1 << 28)
33#define MEQSEL1 0x40
34#define MEQSEL2 0x44
Damian7caa4342011-05-18 11:10:07 +000035
Laurent Pinchartf0a260f2011-07-13 12:13:47 +020036#define MExxCTL 0x400
37#define MExxCTL_BV (1 << 31)
38#define MExxCTL_BSZ_SHIFT 28
39#define MExxCTL_MSAR_MASK (0x7ff << MExxCTL_MSAR_SHIFT)
40#define MExxCTL_MSAR_SHIFT 16
41#define MExxCTL_NXT_MASK (0x1f << MExxCTL_NXT_SHIFT)
42#define MExxCTL_NXT_SHIFT 11
43#define MExxCTL_WD1 (1 << 10)
44#define MExxCTL_WD0 (1 << 9)
45#define MExxCTL_WS (1 << 8)
46#define MExxCTL_CB (1 << 7)
47#define MExxCTL_WBF (1 << 6)
48#define MExxCTL_WF (1 << 5)
49#define MExxCTL_RF (1 << 4)
50#define MExxCTL_CM (1 << 3)
51#define MExxCTL_MD_READ (1 << 0)
52#define MExxCTL_MD_WRITE (2 << 0)
53#define MExxCTL_MD_ICB_WB (3 << 0)
54#define MExxCTL_MD_ICB (4 << 0)
55#define MExxCTL_MD_FB (7 << 0)
56#define MExxCTL_MD_MASK (7 << 0)
57#define MExxBSIZE 0x404
58#define MExxBSIZE_RCNT_SHIFT 28
59#define MExxBSIZE_YSZM1_SHIFT 16
60#define MExxBSIZE_XSZM1_SHIFT 0
61#define MExxMNCF 0x408
62#define MExxMNCF_KWBNM_SHIFT 28
63#define MExxMNCF_KRBNM_SHIFT 24
64#define MExxMNCF_BNM_SHIFT 16
65#define MExxMNCF_XBV (1 << 15)
66#define MExxMNCF_CPL_YCBCR444 (1 << 12)
67#define MExxMNCF_CPL_YCBCR420 (2 << 12)
68#define MExxMNCF_CPL_YCBCR422 (3 << 12)
69#define MExxMNCF_CPL_MSK (3 << 12)
70#define MExxMNCF_BL (1 << 2)
71#define MExxMNCF_LNM_SHIFT 0
72#define MExxSARA 0x410
73#define MExxSARB 0x414
74#define MExxSBSIZE 0x418
75#define MExxSBSIZE_HDV (1 << 31)
76#define MExxSBSIZE_HSZ16 (0 << 28)
77#define MExxSBSIZE_HSZ32 (1 << 28)
78#define MExxSBSIZE_HSZ64 (2 << 28)
79#define MExxSBSIZE_HSZ128 (3 << 28)
80#define MExxSBSIZE_SBSIZZ_SHIFT 0
Damian7caa4342011-05-18 11:10:07 +000081
Laurent Pinchartf0a260f2011-07-13 12:13:47 +020082#define MERAM_MExxCTL_VAL(next, addr) \
83 ((((next) << MExxCTL_NXT_SHIFT) & MExxCTL_NXT_MASK) | \
84 (((addr) << MExxCTL_MSAR_SHIFT) & MExxCTL_MSAR_MASK))
85#define MERAM_MExxBSIZE_VAL(rcnt, yszm1, xszm1) \
86 (((rcnt) << MExxBSIZE_RCNT_SHIFT) | \
87 ((yszm1) << MExxBSIZE_YSZM1_SHIFT) | \
88 ((xszm1) << MExxBSIZE_XSZM1_SHIFT))
Damian7caa4342011-05-18 11:10:07 +000089
Laurent Pinchart762f7cc2011-09-19 11:40:31 +020090static const unsigned long common_regs[] = {
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +020091 MEVCR1,
92 MEQSEL1,
93 MEQSEL2,
94};
Laurent Pinchart75543402011-09-19 11:40:31 +020095#define MERAM_REGS_SIZE ARRAY_SIZE(common_regs)
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +020096
Laurent Pinchart762f7cc2011-09-19 11:40:31 +020097static const unsigned long icb_regs[] = {
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +020098 MExxCTL,
99 MExxBSIZE,
100 MExxMNCF,
101 MExxSARA,
102 MExxSARB,
103 MExxSBSIZE,
104};
105#define ICB_REGS_SIZE ARRAY_SIZE(icb_regs)
106
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200107/*
108 * sh_mobile_meram_icb - MERAM ICB information
109 * @regs: Registers cache
Laurent Pinchart48110052011-12-12 16:36:13 +0100110 * @index: ICB index
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200111 * @offset: MERAM block offset
Laurent Pinchart48110052011-12-12 16:36:13 +0100112 * @size: MERAM block size in KiB
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200113 * @cache_unit: Bytes to cache per ICB
114 * @pixelformat: Video pixel format of the data stored in the ICB
115 * @current_reg: Which of Start Address Register A (0) or B (1) is in use
116 */
117struct sh_mobile_meram_icb {
118 unsigned long regs[ICB_REGS_SIZE];
Laurent Pinchart48110052011-12-12 16:36:13 +0100119 unsigned int index;
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200120 unsigned long offset;
121 unsigned int size;
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200122
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200123 unsigned int cache_unit;
124 unsigned int pixelformat;
125 unsigned int current_reg;
126};
127
Laurent Pinchart75543402011-09-19 11:40:31 +0200128#define MERAM_ICB_NUM 32
129
Laurent Pinchart48110052011-12-12 16:36:13 +0100130struct sh_mobile_meram_fb_plane {
131 struct sh_mobile_meram_icb *marker;
132 struct sh_mobile_meram_icb *cache;
133};
134
135struct sh_mobile_meram_fb_cache {
136 unsigned int nplanes;
137 struct sh_mobile_meram_fb_plane planes[2];
138};
139
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200140/*
141 * sh_mobile_meram_priv - MERAM device
142 * @base: Registers base address
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200143 * @meram: MERAM physical address
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200144 * @regs: Registers cache
145 * @lock: Protects used_icb and icbs
146 * @used_icb: Bitmask of used ICBs
147 * @icbs: ICBs
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200148 * @pool: Allocation pool to manage the MERAM
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200149 */
Damian Hobson-Garcia0aa492b2011-06-22 07:49:50 +0200150struct sh_mobile_meram_priv {
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200151 void __iomem *base;
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200152 unsigned long meram;
Laurent Pinchart75543402011-09-19 11:40:31 +0200153 unsigned long regs[MERAM_REGS_SIZE];
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200154
155 struct mutex lock;
156 unsigned long used_icb;
Laurent Pinchart75543402011-09-19 11:40:31 +0200157 struct sh_mobile_meram_icb icbs[MERAM_ICB_NUM];
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200158
159 struct gen_pool *pool;
Damian Hobson-Garcia0aa492b2011-06-22 07:49:50 +0200160};
161
Damian7caa4342011-05-18 11:10:07 +0000162/* settings */
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200163#define MERAM_GRANULARITY 1024
Laurent Pinchart75543402011-09-19 11:40:31 +0200164#define MERAM_SEC_LINE 15
165#define MERAM_LINE_WIDTH 2048
Damian7caa4342011-05-18 11:10:07 +0000166
Laurent Pinchart75543402011-09-19 11:40:31 +0200167/* -----------------------------------------------------------------------------
168 * Registers access
Damian7caa4342011-05-18 11:10:07 +0000169 */
170
Laurent Pinchartf0a260f2011-07-13 12:13:47 +0200171#define MERAM_ICB_OFFSET(base, idx, off) ((base) + (off) + (idx) * 0x20)
Damian7caa4342011-05-18 11:10:07 +0000172
Laurent Pinchart05432832011-09-19 11:40:31 +0200173static inline void meram_write_icb(void __iomem *base, unsigned int idx,
174 unsigned int off, unsigned long val)
Damian7caa4342011-05-18 11:10:07 +0000175{
176 iowrite32(val, MERAM_ICB_OFFSET(base, idx, off));
177}
178
Laurent Pinchart05432832011-09-19 11:40:31 +0200179static inline unsigned long meram_read_icb(void __iomem *base, unsigned int idx,
180 unsigned int off)
Damian7caa4342011-05-18 11:10:07 +0000181{
182 return ioread32(MERAM_ICB_OFFSET(base, idx, off));
183}
184
Laurent Pinchart05432832011-09-19 11:40:31 +0200185static inline void meram_write_reg(void __iomem *base, unsigned int off,
186 unsigned long val)
Damian7caa4342011-05-18 11:10:07 +0000187{
188 iowrite32(val, base + off);
189}
190
Laurent Pinchart05432832011-09-19 11:40:31 +0200191static inline unsigned long meram_read_reg(void __iomem *base, unsigned int off)
Damian7caa4342011-05-18 11:10:07 +0000192{
193 return ioread32(base + off);
194}
195
Laurent Pinchart75543402011-09-19 11:40:31 +0200196/* -----------------------------------------------------------------------------
197 * Allocation
Damian7caa4342011-05-18 11:10:07 +0000198 */
199
Laurent Pinchart48110052011-12-12 16:36:13 +0100200/* Allocate ICBs and MERAM for a plane. */
201static int __meram_alloc(struct sh_mobile_meram_priv *priv,
202 struct sh_mobile_meram_fb_plane *plane,
203 size_t size)
Damian7caa4342011-05-18 11:10:07 +0000204{
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200205 unsigned long mem;
Laurent Pinchart48110052011-12-12 16:36:13 +0100206 unsigned long idx;
Damian7caa4342011-05-18 11:10:07 +0000207
Laurent Pinchart48110052011-12-12 16:36:13 +0100208 idx = find_first_zero_bit(&priv->used_icb, 28);
209 if (idx == 28)
210 return -ENOMEM;
211 plane->cache = &priv->icbs[idx];
212
213 idx = find_next_zero_bit(&priv->used_icb, 32, 28);
214 if (idx == 32)
215 return -ENOMEM;
216 plane->marker = &priv->icbs[idx];
217
218 mem = gen_pool_alloc(priv->pool, size * 1024);
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200219 if (mem == 0)
220 return -ENOMEM;
221
Laurent Pinchart48110052011-12-12 16:36:13 +0100222 __set_bit(plane->marker->index, &priv->used_icb);
223 __set_bit(plane->cache->index, &priv->used_icb);
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200224
Laurent Pinchart48110052011-12-12 16:36:13 +0100225 plane->marker->offset = mem - priv->meram;
226 plane->marker->size = size;
Damian7caa4342011-05-18 11:10:07 +0000227
228 return 0;
229}
230
Laurent Pinchart48110052011-12-12 16:36:13 +0100231/* Free ICBs and MERAM for a plane. */
232static void __meram_free(struct sh_mobile_meram_priv *priv,
233 struct sh_mobile_meram_fb_plane *plane)
Damian7caa4342011-05-18 11:10:07 +0000234{
Laurent Pinchart48110052011-12-12 16:36:13 +0100235 gen_pool_free(priv->pool, priv->meram + plane->marker->offset,
236 plane->marker->size * 1024);
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200237
Laurent Pinchart48110052011-12-12 16:36:13 +0100238 __clear_bit(plane->marker->index, &priv->used_icb);
239 __clear_bit(plane->cache->index, &priv->used_icb);
Damian7caa4342011-05-18 11:10:07 +0000240}
241
Laurent Pinchart75543402011-09-19 11:40:31 +0200242/* Is this a YCbCr(NV12, NV16 or NV24) colorspace? */
Laurent Pinchart762f7cc2011-09-19 11:40:31 +0200243static int is_nvcolor(int cspace)
Damian3fedd2a2011-05-18 11:10:08 +0000244{
245 if (cspace == SH_MOBILE_MERAM_PF_NV ||
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200246 cspace == SH_MOBILE_MERAM_PF_NV24)
Damian3fedd2a2011-05-18 11:10:08 +0000247 return 1;
248 return 0;
249}
Damian7caa4342011-05-18 11:10:07 +0000250
Laurent Pinchart48110052011-12-12 16:36:13 +0100251/* Allocate memory for the ICBs and mark them as used. */
252static struct sh_mobile_meram_fb_cache *
253meram_alloc(struct sh_mobile_meram_priv *priv,
254 const struct sh_mobile_meram_cfg *cfg,
255 int pixelformat)
256{
257 struct sh_mobile_meram_fb_cache *cache;
258 unsigned int nplanes = is_nvcolor(pixelformat) ? 2 : 1;
259 int ret;
260
261 if (cfg->icb[0].meram_size == 0)
262 return ERR_PTR(-EINVAL);
263
264 if (nplanes == 2 && cfg->icb[1].meram_size == 0)
265 return ERR_PTR(-EINVAL);
266
267 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
268 if (cache == NULL)
269 return ERR_PTR(-ENOMEM);
270
271 cache->nplanes = nplanes;
272
273 ret = __meram_alloc(priv, &cache->planes[0], cfg->icb[0].meram_size);
274 if (ret < 0)
275 goto error;
276
277 cache->planes[0].marker->current_reg = 1;
278 cache->planes[0].marker->pixelformat = pixelformat;
279
280 if (cache->nplanes == 1)
281 return cache;
282
283 ret = __meram_alloc(priv, &cache->planes[1], cfg->icb[1].meram_size);
284 if (ret < 0) {
285 __meram_free(priv, &cache->planes[0]);
286 goto error;
287 }
288
289 return cache;
290
291error:
292 kfree(cache);
293 return ERR_PTR(-ENOMEM);
294}
295
296/* Unmark the specified ICB as used. */
297static void meram_free(struct sh_mobile_meram_priv *priv,
298 struct sh_mobile_meram_fb_cache *cache)
299{
300 __meram_free(priv, &cache->planes[0]);
301 if (cache->nplanes == 2)
302 __meram_free(priv, &cache->planes[1]);
303
304 kfree(cache);
305}
306
Laurent Pinchart75543402011-09-19 11:40:31 +0200307/* Set the next address to fetch. */
Laurent Pinchart762f7cc2011-09-19 11:40:31 +0200308static void meram_set_next_addr(struct sh_mobile_meram_priv *priv,
Laurent Pinchart48110052011-12-12 16:36:13 +0100309 struct sh_mobile_meram_fb_cache *cache,
Laurent Pinchart762f7cc2011-09-19 11:40:31 +0200310 unsigned long base_addr_y,
311 unsigned long base_addr_c)
Damian7caa4342011-05-18 11:10:07 +0000312{
Laurent Pinchart48110052011-12-12 16:36:13 +0100313 struct sh_mobile_meram_icb *icb = cache->planes[0].marker;
Damian7caa4342011-05-18 11:10:07 +0000314 unsigned long target;
315
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200316 icb->current_reg ^= 1;
317 target = icb->current_reg ? MExxSARB : MExxSARA;
Damian7caa4342011-05-18 11:10:07 +0000318
319 /* set the next address to fetch */
Laurent Pinchart48110052011-12-12 16:36:13 +0100320 meram_write_icb(priv->base, cache->planes[0].cache->index, target,
Damian7caa4342011-05-18 11:10:07 +0000321 base_addr_y);
Laurent Pinchart48110052011-12-12 16:36:13 +0100322 meram_write_icb(priv->base, cache->planes[0].marker->index, target,
323 base_addr_y + cache->planes[0].marker->cache_unit);
Damian7caa4342011-05-18 11:10:07 +0000324
Laurent Pinchart48110052011-12-12 16:36:13 +0100325 if (cache->nplanes == 2) {
326 meram_write_icb(priv->base, cache->planes[1].cache->index,
327 target, base_addr_c);
328 meram_write_icb(priv->base, cache->planes[1].marker->index,
329 target, base_addr_c +
330 cache->planes[1].marker->cache_unit);
Damian7caa4342011-05-18 11:10:07 +0000331 }
332}
333
Laurent Pinchart75543402011-09-19 11:40:31 +0200334/* Get the next ICB address. */
Laurent Pinchart762f7cc2011-09-19 11:40:31 +0200335static void
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200336meram_get_next_icb_addr(struct sh_mobile_meram_info *pdata,
Laurent Pinchart48110052011-12-12 16:36:13 +0100337 struct sh_mobile_meram_fb_cache *cache,
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200338 unsigned long *icb_addr_y, unsigned long *icb_addr_c)
Damian7caa4342011-05-18 11:10:07 +0000339{
Laurent Pinchart48110052011-12-12 16:36:13 +0100340 struct sh_mobile_meram_icb *icb = cache->planes[0].marker;
Damian7caa4342011-05-18 11:10:07 +0000341 unsigned long icb_offset;
342
343 if (pdata->addr_mode == SH_MOBILE_MERAM_MODE0)
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200344 icb_offset = 0x80000000 | (icb->current_reg << 29);
Damian7caa4342011-05-18 11:10:07 +0000345 else
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200346 icb_offset = 0xc0000000 | (icb->current_reg << 23);
Damian7caa4342011-05-18 11:10:07 +0000347
Laurent Pinchart48110052011-12-12 16:36:13 +0100348 *icb_addr_y = icb_offset | (cache->planes[0].marker->index << 24);
349 if (cache->nplanes == 2)
350 *icb_addr_c = icb_offset
351 | (cache->planes[1].marker->index << 24);
Damian7caa4342011-05-18 11:10:07 +0000352}
353
354#define MERAM_CALC_BYTECOUNT(x, y) \
355 (((x) * (y) + (MERAM_LINE_WIDTH - 1)) & ~(MERAM_LINE_WIDTH - 1))
356
Laurent Pinchart75543402011-09-19 11:40:31 +0200357/* Initialize MERAM. */
Damian7caa4342011-05-18 11:10:07 +0000358static int meram_init(struct sh_mobile_meram_priv *priv,
Laurent Pinchart48110052011-12-12 16:36:13 +0100359 struct sh_mobile_meram_fb_plane *plane,
Laurent Pinchart05432832011-09-19 11:40:31 +0200360 unsigned int xres, unsigned int yres,
361 unsigned int *out_pitch)
Damian7caa4342011-05-18 11:10:07 +0000362{
Laurent Pinchart48110052011-12-12 16:36:13 +0100363 struct sh_mobile_meram_icb *marker = plane->marker;
Damian7caa4342011-05-18 11:10:07 +0000364 unsigned long total_byte_count = MERAM_CALC_BYTECOUNT(xres, yres);
365 unsigned long bnm;
Laurent Pinchart05432832011-09-19 11:40:31 +0200366 unsigned int lcdc_pitch;
367 unsigned int xpitch;
368 unsigned int line_cnt;
369 unsigned int save_lines;
Damian7caa4342011-05-18 11:10:07 +0000370
371 /* adjust pitch to 1024, 2048, 4096 or 8192 */
372 lcdc_pitch = (xres - 1) | 1023;
373 lcdc_pitch = lcdc_pitch | (lcdc_pitch >> 1);
374 lcdc_pitch = lcdc_pitch | (lcdc_pitch >> 2);
375 lcdc_pitch += 1;
376
377 /* derive settings */
378 if (lcdc_pitch == 8192 && yres >= 1024) {
379 lcdc_pitch = xpitch = MERAM_LINE_WIDTH;
380 line_cnt = total_byte_count >> 11;
381 *out_pitch = xres;
Laurent Pinchart48110052011-12-12 16:36:13 +0100382 save_lines = plane->marker->size / 16 / MERAM_SEC_LINE;
Damian7caa4342011-05-18 11:10:07 +0000383 save_lines *= MERAM_SEC_LINE;
384 } else {
385 xpitch = xres;
386 line_cnt = yres;
387 *out_pitch = lcdc_pitch;
Laurent Pinchart48110052011-12-12 16:36:13 +0100388 save_lines = plane->marker->size / (lcdc_pitch >> 10) / 2;
Damian7caa4342011-05-18 11:10:07 +0000389 save_lines &= 0xff;
390 }
391 bnm = (save_lines - 1) << 16;
392
393 /* TODO: we better to check if we have enough MERAM buffer size */
394
395 /* set up ICB */
Laurent Pinchart48110052011-12-12 16:36:13 +0100396 meram_write_icb(priv->base, plane->cache->index, MExxBSIZE,
Damian7caa4342011-05-18 11:10:07 +0000397 MERAM_MExxBSIZE_VAL(0x0, line_cnt - 1, xpitch - 1));
Laurent Pinchart48110052011-12-12 16:36:13 +0100398 meram_write_icb(priv->base, plane->marker->index, MExxBSIZE,
Damian7caa4342011-05-18 11:10:07 +0000399 MERAM_MExxBSIZE_VAL(0xf, line_cnt - 1, xpitch - 1));
400
Laurent Pinchart48110052011-12-12 16:36:13 +0100401 meram_write_icb(priv->base, plane->cache->index, MExxMNCF, bnm);
402 meram_write_icb(priv->base, plane->marker->index, MExxMNCF, bnm);
Damian7caa4342011-05-18 11:10:07 +0000403
Laurent Pinchart48110052011-12-12 16:36:13 +0100404 meram_write_icb(priv->base, plane->cache->index, MExxSBSIZE, xpitch);
405 meram_write_icb(priv->base, plane->marker->index, MExxSBSIZE, xpitch);
Damian7caa4342011-05-18 11:10:07 +0000406
407 /* save a cache unit size */
Laurent Pinchart48110052011-12-12 16:36:13 +0100408 plane->cache->cache_unit = xres * save_lines;
409 plane->marker->cache_unit = xres * save_lines;
Damian7caa4342011-05-18 11:10:07 +0000410
411 /*
412 * Set MERAM for framebuffer
413 *
Damian7caa4342011-05-18 11:10:07 +0000414 * we also chain the cache_icb and the marker_icb.
415 * we also split the allocated MERAM buffer between two ICBs.
416 */
Laurent Pinchart48110052011-12-12 16:36:13 +0100417 meram_write_icb(priv->base, plane->cache->index, MExxCTL,
418 MERAM_MExxCTL_VAL(plane->marker->index, marker->offset)
419 | MExxCTL_WD1 | MExxCTL_WD0 | MExxCTL_WS | MExxCTL_CM |
Laurent Pinchartf0a260f2011-07-13 12:13:47 +0200420 MExxCTL_MD_FB);
Laurent Pinchart48110052011-12-12 16:36:13 +0100421 meram_write_icb(priv->base, plane->marker->index, MExxCTL,
422 MERAM_MExxCTL_VAL(plane->cache->index, marker->offset +
423 plane->marker->size / 2) |
Laurent Pinchartf0a260f2011-07-13 12:13:47 +0200424 MExxCTL_WD1 | MExxCTL_WD0 | MExxCTL_WS | MExxCTL_CM |
425 MExxCTL_MD_FB);
Damian7caa4342011-05-18 11:10:07 +0000426
427 return 0;
428}
429
430static void meram_deinit(struct sh_mobile_meram_priv *priv,
Laurent Pinchart48110052011-12-12 16:36:13 +0100431 struct sh_mobile_meram_fb_plane *plane)
Damian7caa4342011-05-18 11:10:07 +0000432{
433 /* disable ICB */
Laurent Pinchart48110052011-12-12 16:36:13 +0100434 meram_write_icb(priv->base, plane->cache->index, MExxCTL,
Laurent Pinchartda6cf512011-08-31 13:00:52 +0200435 MExxCTL_WBF | MExxCTL_WF | MExxCTL_RF);
Laurent Pinchart48110052011-12-12 16:36:13 +0100436 meram_write_icb(priv->base, plane->marker->index, MExxCTL,
Laurent Pinchartda6cf512011-08-31 13:00:52 +0200437 MExxCTL_WBF | MExxCTL_WF | MExxCTL_RF);
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200438
Laurent Pinchart48110052011-12-12 16:36:13 +0100439 plane->cache->cache_unit = 0;
440 plane->marker->cache_unit = 0;
Damian7caa4342011-05-18 11:10:07 +0000441}
442
Laurent Pinchart75543402011-09-19 11:40:31 +0200443/* -----------------------------------------------------------------------------
444 * Registration/unregistration
Damian7caa4342011-05-18 11:10:07 +0000445 */
446
Laurent Pinchart48110052011-12-12 16:36:13 +0100447static void *sh_mobile_meram_register(struct sh_mobile_meram_info *pdata,
448 const struct sh_mobile_meram_cfg *cfg,
449 unsigned int xres, unsigned int yres,
450 unsigned int pixelformat,
Laurent Pinchart48110052011-12-12 16:36:13 +0100451 unsigned int *pitch)
Damian7caa4342011-05-18 11:10:07 +0000452{
Laurent Pinchart48110052011-12-12 16:36:13 +0100453 struct sh_mobile_meram_fb_cache *cache;
Laurent Pinchartcdf88b92011-11-22 00:56:58 +0100454 struct sh_mobile_meram_priv *priv = pdata->priv;
455 struct platform_device *pdev = pdata->pdev;
Laurent Pinchart05432832011-09-19 11:40:31 +0200456 unsigned int out_pitch;
Damian7caa4342011-05-18 11:10:07 +0000457
Damian7caa4342011-05-18 11:10:07 +0000458 if (pixelformat != SH_MOBILE_MERAM_PF_NV &&
Damian3fedd2a2011-05-18 11:10:08 +0000459 pixelformat != SH_MOBILE_MERAM_PF_NV24 &&
Damian7caa4342011-05-18 11:10:07 +0000460 pixelformat != SH_MOBILE_MERAM_PF_RGB)
Laurent Pinchart48110052011-12-12 16:36:13 +0100461 return ERR_PTR(-EINVAL);
Damian7caa4342011-05-18 11:10:07 +0000462
Laurent Pinchart97d16fe2011-11-22 00:56:58 +0100463 dev_dbg(&pdev->dev, "registering %dx%d (%s)", xres, yres,
464 !pixelformat ? "yuv" : "rgb");
Damian7caa4342011-05-18 11:10:07 +0000465
Damian7caa4342011-05-18 11:10:07 +0000466 /* we can't handle wider than 8192px */
467 if (xres > 8192) {
468 dev_err(&pdev->dev, "width exceeding the limit (> 8192).");
Laurent Pinchart48110052011-12-12 16:36:13 +0100469 return ERR_PTR(-EINVAL);
Laurent Pinchart7963e212011-07-13 12:13:47 +0200470 }
471
472 mutex_lock(&priv->lock);
473
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200474 /* We now register the ICBs and allocate the MERAM regions. */
Laurent Pinchart48110052011-12-12 16:36:13 +0100475 cache = meram_alloc(priv, cfg, pixelformat);
476 if (IS_ERR(cache)) {
477 dev_err(&pdev->dev, "MERAM allocation failed (%ld).",
478 PTR_ERR(cache));
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200479 goto err;
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200480 }
Damian7caa4342011-05-18 11:10:07 +0000481
482 /* initialize MERAM */
Laurent Pinchart48110052011-12-12 16:36:13 +0100483 meram_init(priv, &cache->planes[0], xres, yres, &out_pitch);
Damian7caa4342011-05-18 11:10:07 +0000484 *pitch = out_pitch;
485 if (pixelformat == SH_MOBILE_MERAM_PF_NV)
Laurent Pinchart48110052011-12-12 16:36:13 +0100486 meram_init(priv, &cache->planes[1], xres, (yres + 1) / 2,
Damian7caa4342011-05-18 11:10:07 +0000487 &out_pitch);
Damian3fedd2a2011-05-18 11:10:08 +0000488 else if (pixelformat == SH_MOBILE_MERAM_PF_NV24)
Laurent Pinchart48110052011-12-12 16:36:13 +0100489 meram_init(priv, &cache->planes[1], 2 * xres, (yres + 1) / 2,
Damian3fedd2a2011-05-18 11:10:08 +0000490 &out_pitch);
Damian7caa4342011-05-18 11:10:07 +0000491
Damian7caa4342011-05-18 11:10:07 +0000492err:
493 mutex_unlock(&priv->lock);
Laurent Pinchart48110052011-12-12 16:36:13 +0100494 return cache;
Damian7caa4342011-05-18 11:10:07 +0000495}
496
Laurent Pinchartcdf88b92011-11-22 00:56:58 +0100497static void
Laurent Pinchart48110052011-12-12 16:36:13 +0100498sh_mobile_meram_unregister(struct sh_mobile_meram_info *pdata, void *data)
Damian7caa4342011-05-18 11:10:07 +0000499{
Laurent Pinchart48110052011-12-12 16:36:13 +0100500 struct sh_mobile_meram_fb_cache *cache = data;
Laurent Pinchartcdf88b92011-11-22 00:56:58 +0100501 struct sh_mobile_meram_priv *priv = pdata->priv;
Damian7caa4342011-05-18 11:10:07 +0000502
503 mutex_lock(&priv->lock);
504
Laurent Pinchart48110052011-12-12 16:36:13 +0100505 /* deinit & free */
506 meram_deinit(priv, &cache->planes[0]);
507 if (cache->nplanes == 2)
508 meram_deinit(priv, &cache->planes[1]);
509
510 meram_free(priv, cache);
Damian7caa4342011-05-18 11:10:07 +0000511
512 mutex_unlock(&priv->lock);
Damian7caa4342011-05-18 11:10:07 +0000513}
514
Laurent Pinchartcdf88b92011-11-22 00:56:58 +0100515static void
Laurent Pinchart48110052011-12-12 16:36:13 +0100516sh_mobile_meram_update(struct sh_mobile_meram_info *pdata, void *data,
517 unsigned long base_addr_y, unsigned long base_addr_c,
518 unsigned long *icb_addr_y, unsigned long *icb_addr_c)
Damian7caa4342011-05-18 11:10:07 +0000519{
Laurent Pinchart48110052011-12-12 16:36:13 +0100520 struct sh_mobile_meram_fb_cache *cache = data;
Laurent Pinchartcdf88b92011-11-22 00:56:58 +0100521 struct sh_mobile_meram_priv *priv = pdata->priv;
Damian7caa4342011-05-18 11:10:07 +0000522
523 mutex_lock(&priv->lock);
524
Laurent Pinchart48110052011-12-12 16:36:13 +0100525 meram_set_next_addr(priv, cache, base_addr_y, base_addr_c);
526 meram_get_next_icb_addr(pdata, cache, icb_addr_y, icb_addr_c);
Damian7caa4342011-05-18 11:10:07 +0000527
528 mutex_unlock(&priv->lock);
Damian7caa4342011-05-18 11:10:07 +0000529}
530
Laurent Pinchart75543402011-09-19 11:40:31 +0200531static struct sh_mobile_meram_ops sh_mobile_meram_ops = {
532 .module = THIS_MODULE,
533 .meram_register = sh_mobile_meram_register,
534 .meram_unregister = sh_mobile_meram_unregister,
535 .meram_update = sh_mobile_meram_update,
536};
537
538/* -----------------------------------------------------------------------------
539 * Power management
540 */
541
Laurent Pinchartaf899562011-11-22 00:56:58 +0100542static int sh_mobile_meram_suspend(struct device *dev)
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200543{
544 struct platform_device *pdev = to_platform_device(dev);
545 struct sh_mobile_meram_priv *priv = platform_get_drvdata(pdev);
Laurent Pinchart05432832011-09-19 11:40:31 +0200546 unsigned int i, j;
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200547
Laurent Pinchart75543402011-09-19 11:40:31 +0200548 for (i = 0; i < MERAM_REGS_SIZE; i++)
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200549 priv->regs[i] = meram_read_reg(priv->base, common_regs[i]);
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200550
Laurent Pinchart05432832011-09-19 11:40:31 +0200551 for (i = 0; i < 32; i++) {
552 if (!test_bit(i, &priv->used_icb))
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200553 continue;
Laurent Pinchart05432832011-09-19 11:40:31 +0200554 for (j = 0; j < ICB_REGS_SIZE; j++) {
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200555 priv->icbs[i].regs[j] =
Laurent Pinchart05432832011-09-19 11:40:31 +0200556 meram_read_icb(priv->base, i, icb_regs[j]);
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200557 /* Reset ICB on resume */
Laurent Pinchart05432832011-09-19 11:40:31 +0200558 if (icb_regs[j] == MExxCTL)
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200559 priv->icbs[i].regs[j] |=
Laurent Pinchartf0a260f2011-07-13 12:13:47 +0200560 MExxCTL_WBF | MExxCTL_WF | MExxCTL_RF;
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200561 }
562 }
563 return 0;
564}
565
Laurent Pinchartaf899562011-11-22 00:56:58 +0100566static int sh_mobile_meram_resume(struct device *dev)
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200567{
568 struct platform_device *pdev = to_platform_device(dev);
569 struct sh_mobile_meram_priv *priv = platform_get_drvdata(pdev);
Laurent Pinchart05432832011-09-19 11:40:31 +0200570 unsigned int i, j;
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200571
Laurent Pinchart05432832011-09-19 11:40:31 +0200572 for (i = 0; i < 32; i++) {
573 if (!test_bit(i, &priv->used_icb))
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200574 continue;
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200575 for (j = 0; j < ICB_REGS_SIZE; j++)
Laurent Pinchart05432832011-09-19 11:40:31 +0200576 meram_write_icb(priv->base, i, icb_regs[j],
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200577 priv->icbs[i].regs[j]);
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200578 }
579
Laurent Pinchart75543402011-09-19 11:40:31 +0200580 for (i = 0; i < MERAM_REGS_SIZE; i++)
Laurent Pinchart2a618e02011-09-19 11:40:31 +0200581 meram_write_reg(priv->base, common_regs[i], priv->regs[i]);
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200582 return 0;
583}
584
Laurent Pinchartaf899562011-11-22 00:56:58 +0100585static UNIVERSAL_DEV_PM_OPS(sh_mobile_meram_dev_pm_ops,
586 sh_mobile_meram_suspend,
587 sh_mobile_meram_resume, NULL);
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200588
Laurent Pinchart75543402011-09-19 11:40:31 +0200589/* -----------------------------------------------------------------------------
590 * Probe/remove and driver init/exit
Damian7caa4342011-05-18 11:10:07 +0000591 */
592
Damian7caa4342011-05-18 11:10:07 +0000593static int __devinit sh_mobile_meram_probe(struct platform_device *pdev)
594{
595 struct sh_mobile_meram_priv *priv;
596 struct sh_mobile_meram_info *pdata = pdev->dev.platform_data;
Laurent Pincharte1d11442011-09-19 11:40:31 +0200597 struct resource *regs;
598 struct resource *meram;
Laurent Pinchart48110052011-12-12 16:36:13 +0100599 unsigned int i;
Damian7caa4342011-05-18 11:10:07 +0000600 int error;
601
602 if (!pdata) {
603 dev_err(&pdev->dev, "no platform data defined\n");
604 return -EINVAL;
605 }
606
Laurent Pincharte1d11442011-09-19 11:40:31 +0200607 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
608 meram = platform_get_resource(pdev, IORESOURCE_MEM, 1);
609 if (regs == NULL || meram == NULL) {
Damian7caa4342011-05-18 11:10:07 +0000610 dev_err(&pdev->dev, "cannot get platform resources\n");
611 return -ENOENT;
612 }
613
614 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
615 if (!priv) {
616 dev_err(&pdev->dev, "cannot allocate device data\n");
617 return -ENOMEM;
618 }
619
Laurent Pinchart48110052011-12-12 16:36:13 +0100620 /* Initialize private data. */
Damian7caa4342011-05-18 11:10:07 +0000621 mutex_init(&priv->lock);
Laurent Pinchart48110052011-12-12 16:36:13 +0100622 priv->used_icb = pdata->reserved_icbs;
623
624 for (i = 0; i < MERAM_ICB_NUM; ++i)
625 priv->icbs[i].index = i;
626
Damian7caa4342011-05-18 11:10:07 +0000627 pdata->ops = &sh_mobile_meram_ops;
628 pdata->priv = priv;
629 pdata->pdev = pdev;
630
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200631 /* Request memory regions and remap the registers. */
Laurent Pincharte1d11442011-09-19 11:40:31 +0200632 if (!request_mem_region(regs->start, resource_size(regs), pdev->name)) {
633 dev_err(&pdev->dev, "MERAM registers region already claimed\n");
634 error = -EBUSY;
635 goto err_req_regs;
636 }
637
638 if (!request_mem_region(meram->start, resource_size(meram),
639 pdev->name)) {
640 dev_err(&pdev->dev, "MERAM memory region already claimed\n");
641 error = -EBUSY;
642 goto err_req_meram;
643 }
644
645 priv->base = ioremap_nocache(regs->start, resource_size(regs));
646 if (!priv->base) {
647 dev_err(&pdev->dev, "ioremap failed\n");
648 error = -EFAULT;
649 goto err_ioremap;
650 }
651
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200652 priv->meram = meram->start;
653
654 /* Create and initialize the MERAM memory pool. */
655 priv->pool = gen_pool_create(ilog2(MERAM_GRANULARITY), -1);
656 if (priv->pool == NULL) {
657 error = -ENOMEM;
658 goto err_genpool;
659 }
660
661 error = gen_pool_add(priv->pool, meram->start, resource_size(meram),
662 -1);
663 if (error < 0)
664 goto err_genpool;
665
Damian7caa4342011-05-18 11:10:07 +0000666 /* initialize ICB addressing mode */
667 if (pdata->addr_mode == SH_MOBILE_MERAM_MODE1)
Laurent Pinchartf0a260f2011-07-13 12:13:47 +0200668 meram_write_reg(priv->base, MEVCR1, MEVCR1_AMD1);
Damian7caa4342011-05-18 11:10:07 +0000669
Laurent Pincharte1d11442011-09-19 11:40:31 +0200670 platform_set_drvdata(pdev, priv);
Damian Hobson-Garcia17673772011-07-04 08:06:11 +0200671 pm_runtime_enable(&pdev->dev);
672
Damian7caa4342011-05-18 11:10:07 +0000673 dev_info(&pdev->dev, "sh_mobile_meram initialized.");
674
675 return 0;
676
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200677err_genpool:
678 if (priv->pool)
679 gen_pool_destroy(priv->pool);
680 iounmap(priv->base);
Laurent Pincharte1d11442011-09-19 11:40:31 +0200681err_ioremap:
682 release_mem_region(meram->start, resource_size(meram));
683err_req_meram:
684 release_mem_region(regs->start, resource_size(regs));
685err_req_regs:
686 mutex_destroy(&priv->lock);
687 kfree(priv);
Damian7caa4342011-05-18 11:10:07 +0000688
689 return error;
690}
691
692
693static int sh_mobile_meram_remove(struct platform_device *pdev)
694{
695 struct sh_mobile_meram_priv *priv = platform_get_drvdata(pdev);
Laurent Pincharte1d11442011-09-19 11:40:31 +0200696 struct resource *regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
697 struct resource *meram = platform_get_resource(pdev, IORESOURCE_MEM, 1);
Damian7caa4342011-05-18 11:10:07 +0000698
Damian Hobson-Garcia17673772011-07-04 08:06:11 +0200699 pm_runtime_disable(&pdev->dev);
700
Laurent Pinchart974d250b2011-09-19 11:40:31 +0200701 gen_pool_destroy(priv->pool);
702
Laurent Pincharte1d11442011-09-19 11:40:31 +0200703 iounmap(priv->base);
704 release_mem_region(meram->start, resource_size(meram));
705 release_mem_region(regs->start, resource_size(regs));
Damian7caa4342011-05-18 11:10:07 +0000706
707 mutex_destroy(&priv->lock);
708
709 kfree(priv);
710
711 return 0;
712}
713
714static struct platform_driver sh_mobile_meram_driver = {
715 .driver = {
716 .name = "sh_mobile_meram",
717 .owner = THIS_MODULE,
Damian Hobson-Garcia0b3bb772011-06-22 07:49:51 +0200718 .pm = &sh_mobile_meram_dev_pm_ops,
Damian7caa4342011-05-18 11:10:07 +0000719 },
720 .probe = sh_mobile_meram_probe,
721 .remove = sh_mobile_meram_remove,
722};
723
Axel Lin4277f2c2011-11-26 10:25:54 +0800724module_platform_driver(sh_mobile_meram_driver);
Damian7caa4342011-05-18 11:10:07 +0000725
726MODULE_DESCRIPTION("SuperH Mobile MERAM driver");
727MODULE_AUTHOR("Damian Hobson-Garcia / Takanari Hayama");
728MODULE_LICENSE("GPL v2");