blob: 0a525c450f24faf2f1cb3a8f4c67f981f5222225 [file] [log] [blame]
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001/*
2 * Copyright (C) 2008
3 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
4 *
5 * Copyright (C) 2005-2007 Freescale Semiconductor, Inc. All Rights Reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/init.h>
13#include <linux/platform_device.h>
14#include <linux/err.h>
15#include <linux/spinlock.h>
16#include <linux/delay.h>
17#include <linux/list.h>
18#include <linux/clk.h>
19#include <linux/vmalloc.h>
20#include <linux/string.h>
21#include <linux/interrupt.h>
22#include <linux/io.h>
23
24#include <mach/ipu.h>
25
26#include "ipu_intern.h"
27
28#define FS_VF_IN_VALID 0x00000002
29#define FS_ENC_IN_VALID 0x00000001
30
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -070031static int ipu_disable_channel(struct idmac *idmac, struct idmac_channel *ichan,
32 bool wait_for_stop);
33
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -070034/*
35 * There can be only one, we could allocate it dynamically, but then we'd have
36 * to add an extra parameter to some functions, and use something as ugly as
37 * struct ipu *ipu = to_ipu(to_idmac(ichan->dma_chan.device));
38 * in the ISR
39 */
40static struct ipu ipu_data;
41
42#define to_ipu(id) container_of(id, struct ipu, idmac)
43
44static u32 __idmac_read_icreg(struct ipu *ipu, unsigned long reg)
45{
46 return __raw_readl(ipu->reg_ic + reg);
47}
48
49#define idmac_read_icreg(ipu, reg) __idmac_read_icreg(ipu, reg - IC_CONF)
50
51static void __idmac_write_icreg(struct ipu *ipu, u32 value, unsigned long reg)
52{
53 __raw_writel(value, ipu->reg_ic + reg);
54}
55
56#define idmac_write_icreg(ipu, v, reg) __idmac_write_icreg(ipu, v, reg - IC_CONF)
57
58static u32 idmac_read_ipureg(struct ipu *ipu, unsigned long reg)
59{
60 return __raw_readl(ipu->reg_ipu + reg);
61}
62
63static void idmac_write_ipureg(struct ipu *ipu, u32 value, unsigned long reg)
64{
65 __raw_writel(value, ipu->reg_ipu + reg);
66}
67
68/*****************************************************************************
69 * IPU / IC common functions
70 */
71static void dump_idmac_reg(struct ipu *ipu)
72{
73 dev_dbg(ipu->dev, "IDMAC_CONF 0x%x, IC_CONF 0x%x, IDMAC_CHA_EN 0x%x, "
74 "IDMAC_CHA_PRI 0x%x, IDMAC_CHA_BUSY 0x%x\n",
75 idmac_read_icreg(ipu, IDMAC_CONF),
76 idmac_read_icreg(ipu, IC_CONF),
77 idmac_read_icreg(ipu, IDMAC_CHA_EN),
78 idmac_read_icreg(ipu, IDMAC_CHA_PRI),
79 idmac_read_icreg(ipu, IDMAC_CHA_BUSY));
80 dev_dbg(ipu->dev, "BUF0_RDY 0x%x, BUF1_RDY 0x%x, CUR_BUF 0x%x, "
81 "DB_MODE 0x%x, TASKS_STAT 0x%x\n",
82 idmac_read_ipureg(ipu, IPU_CHA_BUF0_RDY),
83 idmac_read_ipureg(ipu, IPU_CHA_BUF1_RDY),
84 idmac_read_ipureg(ipu, IPU_CHA_CUR_BUF),
85 idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL),
86 idmac_read_ipureg(ipu, IPU_TASKS_STAT));
87}
88
89static uint32_t bytes_per_pixel(enum pixel_fmt fmt)
90{
91 switch (fmt) {
92 case IPU_PIX_FMT_GENERIC: /* generic data */
93 case IPU_PIX_FMT_RGB332:
94 case IPU_PIX_FMT_YUV420P:
95 case IPU_PIX_FMT_YUV422P:
96 default:
97 return 1;
98 case IPU_PIX_FMT_RGB565:
99 case IPU_PIX_FMT_YUYV:
100 case IPU_PIX_FMT_UYVY:
101 return 2;
102 case IPU_PIX_FMT_BGR24:
103 case IPU_PIX_FMT_RGB24:
104 return 3;
105 case IPU_PIX_FMT_GENERIC_32: /* generic data */
106 case IPU_PIX_FMT_BGR32:
107 case IPU_PIX_FMT_RGB32:
108 case IPU_PIX_FMT_ABGR32:
109 return 4;
110 }
111}
112
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700113/* Enable direct write to memory by the Camera Sensor Interface */
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700114static void ipu_ic_enable_task(struct ipu *ipu, enum ipu_channel channel)
115{
116 uint32_t ic_conf, mask;
117
118 switch (channel) {
119 case IDMAC_IC_0:
120 mask = IC_CONF_PRPENC_EN;
121 break;
122 case IDMAC_IC_7:
123 mask = IC_CONF_RWS_EN | IC_CONF_PRPENC_EN;
124 break;
125 default:
126 return;
127 }
128 ic_conf = idmac_read_icreg(ipu, IC_CONF) | mask;
129 idmac_write_icreg(ipu, ic_conf, IC_CONF);
130}
131
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700132/* Called under spin_lock_irqsave(&ipu_data.lock) */
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700133static void ipu_ic_disable_task(struct ipu *ipu, enum ipu_channel channel)
134{
135 uint32_t ic_conf, mask;
136
137 switch (channel) {
138 case IDMAC_IC_0:
139 mask = IC_CONF_PRPENC_EN;
140 break;
141 case IDMAC_IC_7:
142 mask = IC_CONF_RWS_EN | IC_CONF_PRPENC_EN;
143 break;
144 default:
145 return;
146 }
147 ic_conf = idmac_read_icreg(ipu, IC_CONF) & ~mask;
148 idmac_write_icreg(ipu, ic_conf, IC_CONF);
149}
150
151static uint32_t ipu_channel_status(struct ipu *ipu, enum ipu_channel channel)
152{
153 uint32_t stat = TASK_STAT_IDLE;
154 uint32_t task_stat_reg = idmac_read_ipureg(ipu, IPU_TASKS_STAT);
155
156 switch (channel) {
157 case IDMAC_IC_7:
158 stat = (task_stat_reg & TSTAT_CSI2MEM_MASK) >>
159 TSTAT_CSI2MEM_OFFSET;
160 break;
161 case IDMAC_IC_0:
162 case IDMAC_SDC_0:
163 case IDMAC_SDC_1:
164 default:
165 break;
166 }
167 return stat;
168}
169
170struct chan_param_mem_planar {
171 /* Word 0 */
172 u32 xv:10;
173 u32 yv:10;
174 u32 xb:12;
175
176 u32 yb:12;
177 u32 res1:2;
178 u32 nsb:1;
179 u32 lnpb:6;
180 u32 ubo_l:11;
181
182 u32 ubo_h:15;
183 u32 vbo_l:17;
184
185 u32 vbo_h:9;
186 u32 res2:3;
187 u32 fw:12;
188 u32 fh_l:8;
189
190 u32 fh_h:4;
191 u32 res3:28;
192
193 /* Word 1 */
194 u32 eba0;
195
196 u32 eba1;
197
198 u32 bpp:3;
199 u32 sl:14;
200 u32 pfs:3;
201 u32 bam:3;
202 u32 res4:2;
203 u32 npb:6;
204 u32 res5:1;
205
206 u32 sat:2;
207 u32 res6:30;
208} __attribute__ ((packed));
209
210struct chan_param_mem_interleaved {
211 /* Word 0 */
212 u32 xv:10;
213 u32 yv:10;
214 u32 xb:12;
215
216 u32 yb:12;
217 u32 sce:1;
218 u32 res1:1;
219 u32 nsb:1;
220 u32 lnpb:6;
221 u32 sx:10;
222 u32 sy_l:1;
223
224 u32 sy_h:9;
225 u32 ns:10;
226 u32 sm:10;
227 u32 sdx_l:3;
228
229 u32 sdx_h:2;
230 u32 sdy:5;
231 u32 sdrx:1;
232 u32 sdry:1;
233 u32 sdr1:1;
234 u32 res2:2;
235 u32 fw:12;
236 u32 fh_l:8;
237
238 u32 fh_h:4;
239 u32 res3:28;
240
241 /* Word 1 */
242 u32 eba0;
243
244 u32 eba1;
245
246 u32 bpp:3;
247 u32 sl:14;
248 u32 pfs:3;
249 u32 bam:3;
250 u32 res4:2;
251 u32 npb:6;
252 u32 res5:1;
253
254 u32 sat:2;
255 u32 scc:1;
256 u32 ofs0:5;
257 u32 ofs1:5;
258 u32 ofs2:5;
259 u32 ofs3:5;
260 u32 wid0:3;
261 u32 wid1:3;
262 u32 wid2:3;
263
264 u32 wid3:3;
265 u32 dec_sel:1;
266 u32 res6:28;
267} __attribute__ ((packed));
268
269union chan_param_mem {
270 struct chan_param_mem_planar pp;
271 struct chan_param_mem_interleaved ip;
272};
273
274static void ipu_ch_param_set_plane_offset(union chan_param_mem *params,
275 u32 u_offset, u32 v_offset)
276{
277 params->pp.ubo_l = u_offset & 0x7ff;
278 params->pp.ubo_h = u_offset >> 11;
279 params->pp.vbo_l = v_offset & 0x1ffff;
280 params->pp.vbo_h = v_offset >> 17;
281}
282
283static void ipu_ch_param_set_size(union chan_param_mem *params,
284 uint32_t pixel_fmt, uint16_t width,
285 uint16_t height, uint16_t stride)
286{
287 u32 u_offset;
288 u32 v_offset;
289
290 params->pp.fw = width - 1;
291 params->pp.fh_l = height - 1;
292 params->pp.fh_h = (height - 1) >> 8;
293 params->pp.sl = stride - 1;
294
295 switch (pixel_fmt) {
296 case IPU_PIX_FMT_GENERIC:
297 /*Represents 8-bit Generic data */
298 params->pp.bpp = 3;
299 params->pp.pfs = 7;
300 params->pp.npb = 31;
301 params->pp.sat = 2; /* SAT = use 32-bit access */
302 break;
303 case IPU_PIX_FMT_GENERIC_32:
304 /*Represents 32-bit Generic data */
305 params->pp.bpp = 0;
306 params->pp.pfs = 7;
307 params->pp.npb = 7;
308 params->pp.sat = 2; /* SAT = use 32-bit access */
309 break;
310 case IPU_PIX_FMT_RGB565:
311 params->ip.bpp = 2;
312 params->ip.pfs = 4;
313 params->ip.npb = 7;
314 params->ip.sat = 2; /* SAT = 32-bit access */
315 params->ip.ofs0 = 0; /* Red bit offset */
316 params->ip.ofs1 = 5; /* Green bit offset */
317 params->ip.ofs2 = 11; /* Blue bit offset */
318 params->ip.ofs3 = 16; /* Alpha bit offset */
319 params->ip.wid0 = 4; /* Red bit width - 1 */
320 params->ip.wid1 = 5; /* Green bit width - 1 */
321 params->ip.wid2 = 4; /* Blue bit width - 1 */
322 break;
323 case IPU_PIX_FMT_BGR24:
324 params->ip.bpp = 1; /* 24 BPP & RGB PFS */
325 params->ip.pfs = 4;
326 params->ip.npb = 7;
327 params->ip.sat = 2; /* SAT = 32-bit access */
328 params->ip.ofs0 = 0; /* Red bit offset */
329 params->ip.ofs1 = 8; /* Green bit offset */
330 params->ip.ofs2 = 16; /* Blue bit offset */
331 params->ip.ofs3 = 24; /* Alpha bit offset */
332 params->ip.wid0 = 7; /* Red bit width - 1 */
333 params->ip.wid1 = 7; /* Green bit width - 1 */
334 params->ip.wid2 = 7; /* Blue bit width - 1 */
335 break;
336 case IPU_PIX_FMT_RGB24:
337 params->ip.bpp = 1; /* 24 BPP & RGB PFS */
338 params->ip.pfs = 4;
339 params->ip.npb = 7;
340 params->ip.sat = 2; /* SAT = 32-bit access */
341 params->ip.ofs0 = 16; /* Red bit offset */
342 params->ip.ofs1 = 8; /* Green bit offset */
343 params->ip.ofs2 = 0; /* Blue bit offset */
344 params->ip.ofs3 = 24; /* Alpha bit offset */
345 params->ip.wid0 = 7; /* Red bit width - 1 */
346 params->ip.wid1 = 7; /* Green bit width - 1 */
347 params->ip.wid2 = 7; /* Blue bit width - 1 */
348 break;
349 case IPU_PIX_FMT_BGRA32:
350 case IPU_PIX_FMT_BGR32:
351 params->ip.bpp = 0;
352 params->ip.pfs = 4;
353 params->ip.npb = 7;
354 params->ip.sat = 2; /* SAT = 32-bit access */
355 params->ip.ofs0 = 8; /* Red bit offset */
356 params->ip.ofs1 = 16; /* Green bit offset */
357 params->ip.ofs2 = 24; /* Blue bit offset */
358 params->ip.ofs3 = 0; /* Alpha bit offset */
359 params->ip.wid0 = 7; /* Red bit width - 1 */
360 params->ip.wid1 = 7; /* Green bit width - 1 */
361 params->ip.wid2 = 7; /* Blue bit width - 1 */
362 params->ip.wid3 = 7; /* Alpha bit width - 1 */
363 break;
364 case IPU_PIX_FMT_RGBA32:
365 case IPU_PIX_FMT_RGB32:
366 params->ip.bpp = 0;
367 params->ip.pfs = 4;
368 params->ip.npb = 7;
369 params->ip.sat = 2; /* SAT = 32-bit access */
370 params->ip.ofs0 = 24; /* Red bit offset */
371 params->ip.ofs1 = 16; /* Green bit offset */
372 params->ip.ofs2 = 8; /* Blue bit offset */
373 params->ip.ofs3 = 0; /* Alpha bit offset */
374 params->ip.wid0 = 7; /* Red bit width - 1 */
375 params->ip.wid1 = 7; /* Green bit width - 1 */
376 params->ip.wid2 = 7; /* Blue bit width - 1 */
377 params->ip.wid3 = 7; /* Alpha bit width - 1 */
378 break;
379 case IPU_PIX_FMT_ABGR32:
380 params->ip.bpp = 0;
381 params->ip.pfs = 4;
382 params->ip.npb = 7;
383 params->ip.sat = 2; /* SAT = 32-bit access */
384 params->ip.ofs0 = 8; /* Red bit offset */
385 params->ip.ofs1 = 16; /* Green bit offset */
386 params->ip.ofs2 = 24; /* Blue bit offset */
387 params->ip.ofs3 = 0; /* Alpha bit offset */
388 params->ip.wid0 = 7; /* Red bit width - 1 */
389 params->ip.wid1 = 7; /* Green bit width - 1 */
390 params->ip.wid2 = 7; /* Blue bit width - 1 */
391 params->ip.wid3 = 7; /* Alpha bit width - 1 */
392 break;
393 case IPU_PIX_FMT_UYVY:
394 params->ip.bpp = 2;
395 params->ip.pfs = 6;
396 params->ip.npb = 7;
397 params->ip.sat = 2; /* SAT = 32-bit access */
398 break;
399 case IPU_PIX_FMT_YUV420P2:
400 case IPU_PIX_FMT_YUV420P:
401 params->ip.bpp = 3;
402 params->ip.pfs = 3;
403 params->ip.npb = 7;
404 params->ip.sat = 2; /* SAT = 32-bit access */
405 u_offset = stride * height;
406 v_offset = u_offset + u_offset / 4;
407 ipu_ch_param_set_plane_offset(params, u_offset, v_offset);
408 break;
409 case IPU_PIX_FMT_YVU422P:
410 params->ip.bpp = 3;
411 params->ip.pfs = 2;
412 params->ip.npb = 7;
413 params->ip.sat = 2; /* SAT = 32-bit access */
414 v_offset = stride * height;
415 u_offset = v_offset + v_offset / 2;
416 ipu_ch_param_set_plane_offset(params, u_offset, v_offset);
417 break;
418 case IPU_PIX_FMT_YUV422P:
419 params->ip.bpp = 3;
420 params->ip.pfs = 2;
421 params->ip.npb = 7;
422 params->ip.sat = 2; /* SAT = 32-bit access */
423 u_offset = stride * height;
424 v_offset = u_offset + u_offset / 2;
425 ipu_ch_param_set_plane_offset(params, u_offset, v_offset);
426 break;
427 default:
428 dev_err(ipu_data.dev,
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700429 "mx3 ipu: unimplemented pixel format %d\n", pixel_fmt);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700430 break;
431 }
432
433 params->pp.nsb = 1;
434}
435
436static void ipu_ch_param_set_burst_size(union chan_param_mem *params,
437 uint16_t burst_pixels)
438{
439 params->pp.npb = burst_pixels - 1;
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700440}
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700441
442static void ipu_ch_param_set_buffer(union chan_param_mem *params,
443 dma_addr_t buf0, dma_addr_t buf1)
444{
445 params->pp.eba0 = buf0;
446 params->pp.eba1 = buf1;
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700447}
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700448
449static void ipu_ch_param_set_rotation(union chan_param_mem *params,
450 enum ipu_rotate_mode rotate)
451{
452 params->pp.bam = rotate;
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700453}
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700454
455static void ipu_write_param_mem(uint32_t addr, uint32_t *data,
456 uint32_t num_words)
457{
458 for (; num_words > 0; num_words--) {
459 dev_dbg(ipu_data.dev,
460 "write param mem - addr = 0x%08X, data = 0x%08X\n",
461 addr, *data);
462 idmac_write_ipureg(&ipu_data, addr, IPU_IMA_ADDR);
463 idmac_write_ipureg(&ipu_data, *data++, IPU_IMA_DATA);
464 addr++;
465 if ((addr & 0x7) == 5) {
466 addr &= ~0x7; /* set to word 0 */
467 addr += 8; /* increment to next row */
468 }
469 }
470}
471
472static int calc_resize_coeffs(uint32_t in_size, uint32_t out_size,
473 uint32_t *resize_coeff,
474 uint32_t *downsize_coeff)
475{
476 uint32_t temp_size;
477 uint32_t temp_downsize;
478
479 *resize_coeff = 1 << 13;
480 *downsize_coeff = 1 << 13;
481
482 /* Cannot downsize more than 8:1 */
483 if (out_size << 3 < in_size)
484 return -EINVAL;
485
486 /* compute downsizing coefficient */
487 temp_downsize = 0;
488 temp_size = in_size;
489 while (temp_size >= out_size * 2 && temp_downsize < 2) {
490 temp_size >>= 1;
491 temp_downsize++;
492 }
493 *downsize_coeff = temp_downsize;
494
495 /*
496 * compute resizing coefficient using the following formula:
497 * resize_coeff = M*(SI -1)/(SO - 1)
498 * where M = 2^13, SI - input size, SO - output size
499 */
500 *resize_coeff = (8192L * (temp_size - 1)) / (out_size - 1);
501 if (*resize_coeff >= 16384L) {
502 dev_err(ipu_data.dev, "Warning! Overflow on resize coeff.\n");
503 *resize_coeff = 0x3FFF;
504 }
505
506 dev_dbg(ipu_data.dev, "resizing from %u -> %u pixels, "
507 "downsize=%u, resize=%u.%lu (reg=%u)\n", in_size, out_size,
508 *downsize_coeff, *resize_coeff >= 8192L ? 1 : 0,
509 ((*resize_coeff & 0x1FFF) * 10000L) / 8192L, *resize_coeff);
510
511 return 0;
512}
513
514static enum ipu_color_space format_to_colorspace(enum pixel_fmt fmt)
515{
516 switch (fmt) {
517 case IPU_PIX_FMT_RGB565:
518 case IPU_PIX_FMT_BGR24:
519 case IPU_PIX_FMT_RGB24:
520 case IPU_PIX_FMT_BGR32:
521 case IPU_PIX_FMT_RGB32:
522 return IPU_COLORSPACE_RGB;
523 default:
524 return IPU_COLORSPACE_YCBCR;
525 }
526}
527
528static int ipu_ic_init_prpenc(struct ipu *ipu,
529 union ipu_channel_param *params, bool src_is_csi)
530{
531 uint32_t reg, ic_conf;
532 uint32_t downsize_coeff, resize_coeff;
533 enum ipu_color_space in_fmt, out_fmt;
534
535 /* Setup vertical resizing */
536 calc_resize_coeffs(params->video.in_height,
537 params->video.out_height,
538 &resize_coeff, &downsize_coeff);
539 reg = (downsize_coeff << 30) | (resize_coeff << 16);
540
541 /* Setup horizontal resizing */
542 calc_resize_coeffs(params->video.in_width,
543 params->video.out_width,
544 &resize_coeff, &downsize_coeff);
545 reg |= (downsize_coeff << 14) | resize_coeff;
546
547 /* Setup color space conversion */
548 in_fmt = format_to_colorspace(params->video.in_pixel_fmt);
549 out_fmt = format_to_colorspace(params->video.out_pixel_fmt);
550
551 /*
552 * Colourspace conversion unsupported yet - see _init_csc() in
553 * Freescale sources
554 */
555 if (in_fmt != out_fmt) {
556 dev_err(ipu->dev, "Colourspace conversion unsupported!\n");
557 return -EOPNOTSUPP;
558 }
559
560 idmac_write_icreg(ipu, reg, IC_PRP_ENC_RSC);
561
562 ic_conf = idmac_read_icreg(ipu, IC_CONF);
563
564 if (src_is_csi)
565 ic_conf &= ~IC_CONF_RWS_EN;
566 else
567 ic_conf |= IC_CONF_RWS_EN;
568
569 idmac_write_icreg(ipu, ic_conf, IC_CONF);
570
571 return 0;
572}
573
574static uint32_t dma_param_addr(uint32_t dma_ch)
575{
576 /* Channel Parameter Memory */
577 return 0x10000 | (dma_ch << 4);
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700578}
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700579
580static void ipu_channel_set_priority(struct ipu *ipu, enum ipu_channel channel,
581 bool prio)
582{
583 u32 reg = idmac_read_icreg(ipu, IDMAC_CHA_PRI);
584
585 if (prio)
586 reg |= 1UL << channel;
587 else
588 reg &= ~(1UL << channel);
589
590 idmac_write_icreg(ipu, reg, IDMAC_CHA_PRI);
591
592 dump_idmac_reg(ipu);
593}
594
595static uint32_t ipu_channel_conf_mask(enum ipu_channel channel)
596{
597 uint32_t mask;
598
599 switch (channel) {
600 case IDMAC_IC_0:
601 case IDMAC_IC_7:
602 mask = IPU_CONF_CSI_EN | IPU_CONF_IC_EN;
603 break;
604 case IDMAC_SDC_0:
605 case IDMAC_SDC_1:
606 mask = IPU_CONF_SDC_EN | IPU_CONF_DI_EN;
607 break;
608 default:
609 mask = 0;
610 break;
611 }
612
613 return mask;
614}
615
616/**
617 * ipu_enable_channel() - enable an IPU channel.
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700618 * @idmac: IPU DMAC context.
619 * @ichan: IDMAC channel.
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700620 * @return: 0 on success or negative error code on failure.
621 */
622static int ipu_enable_channel(struct idmac *idmac, struct idmac_channel *ichan)
623{
624 struct ipu *ipu = to_ipu(idmac);
625 enum ipu_channel channel = ichan->dma_chan.chan_id;
626 uint32_t reg;
627 unsigned long flags;
628
629 spin_lock_irqsave(&ipu->lock, flags);
630
631 /* Reset to buffer 0 */
632 idmac_write_ipureg(ipu, 1UL << channel, IPU_CHA_CUR_BUF);
633 ichan->active_buffer = 0;
634 ichan->status = IPU_CHANNEL_ENABLED;
635
636 switch (channel) {
637 case IDMAC_SDC_0:
638 case IDMAC_SDC_1:
639 case IDMAC_IC_7:
640 ipu_channel_set_priority(ipu, channel, true);
641 default:
642 break;
643 }
644
645 reg = idmac_read_icreg(ipu, IDMAC_CHA_EN);
646
647 idmac_write_icreg(ipu, reg | (1UL << channel), IDMAC_CHA_EN);
648
649 ipu_ic_enable_task(ipu, channel);
650
651 spin_unlock_irqrestore(&ipu->lock, flags);
652 return 0;
653}
654
655/**
656 * ipu_init_channel_buffer() - initialize a buffer for logical IPU channel.
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700657 * @ichan: IDMAC channel.
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700658 * @pixel_fmt: pixel format of buffer. Pixel format is a FOURCC ASCII code.
659 * @width: width of buffer in pixels.
660 * @height: height of buffer in pixels.
661 * @stride: stride length of buffer in pixels.
662 * @rot_mode: rotation mode of buffer. A rotation setting other than
663 * IPU_ROTATE_VERT_FLIP should only be used for input buffers of
664 * rotation channels.
665 * @phyaddr_0: buffer 0 physical address.
666 * @phyaddr_1: buffer 1 physical address. Setting this to a value other than
667 * NULL enables double buffering mode.
668 * @return: 0 on success or negative error code on failure.
669 */
670static int ipu_init_channel_buffer(struct idmac_channel *ichan,
671 enum pixel_fmt pixel_fmt,
672 uint16_t width, uint16_t height,
673 uint32_t stride,
674 enum ipu_rotate_mode rot_mode,
675 dma_addr_t phyaddr_0, dma_addr_t phyaddr_1)
676{
677 enum ipu_channel channel = ichan->dma_chan.chan_id;
678 struct idmac *idmac = to_idmac(ichan->dma_chan.device);
679 struct ipu *ipu = to_ipu(idmac);
680 union chan_param_mem params = {};
681 unsigned long flags;
682 uint32_t reg;
683 uint32_t stride_bytes;
684
685 stride_bytes = stride * bytes_per_pixel(pixel_fmt);
686
687 if (stride_bytes % 4) {
688 dev_err(ipu->dev,
689 "Stride length must be 32-bit aligned, stride = %d, bytes = %d\n",
690 stride, stride_bytes);
691 return -EINVAL;
692 }
693
694 /* IC channel's stride must be a multiple of 8 pixels */
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700695 if ((channel <= IDMAC_IC_13) && (stride % 8)) {
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700696 dev_err(ipu->dev, "Stride must be 8 pixel multiple\n");
697 return -EINVAL;
698 }
699
700 /* Build parameter memory data for DMA channel */
701 ipu_ch_param_set_size(&params, pixel_fmt, width, height, stride_bytes);
702 ipu_ch_param_set_buffer(&params, phyaddr_0, phyaddr_1);
703 ipu_ch_param_set_rotation(&params, rot_mode);
704 /* Some channels (rotation) have restriction on burst length */
705 switch (channel) {
706 case IDMAC_IC_7: /* Hangs with burst 8, 16, other values
707 invalid - Table 44-30 */
708/*
709 ipu_ch_param_set_burst_size(&params, 8);
710 */
711 break;
712 case IDMAC_SDC_0:
713 case IDMAC_SDC_1:
714 /* In original code only IPU_PIX_FMT_RGB565 was setting burst */
715 ipu_ch_param_set_burst_size(&params, 16);
716 break;
717 case IDMAC_IC_0:
718 default:
719 break;
720 }
721
722 spin_lock_irqsave(&ipu->lock, flags);
723
724 ipu_write_param_mem(dma_param_addr(channel), (uint32_t *)&params, 10);
725
726 reg = idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL);
727
728 if (phyaddr_1)
729 reg |= 1UL << channel;
730 else
731 reg &= ~(1UL << channel);
732
733 idmac_write_ipureg(ipu, reg, IPU_CHA_DB_MODE_SEL);
734
735 ichan->status = IPU_CHANNEL_READY;
736
Luotao Fuc74ef1f2009-02-26 12:29:20 +0100737 spin_unlock_irqrestore(&ipu->lock, flags);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700738
739 return 0;
740}
741
742/**
743 * ipu_select_buffer() - mark a channel's buffer as ready.
744 * @channel: channel ID.
745 * @buffer_n: buffer number to mark ready.
746 */
747static void ipu_select_buffer(enum ipu_channel channel, int buffer_n)
748{
749 /* No locking - this is a write-one-to-set register, cleared by IPU */
750 if (buffer_n == 0)
751 /* Mark buffer 0 as ready. */
752 idmac_write_ipureg(&ipu_data, 1UL << channel, IPU_CHA_BUF0_RDY);
753 else
754 /* Mark buffer 1 as ready. */
755 idmac_write_ipureg(&ipu_data, 1UL << channel, IPU_CHA_BUF1_RDY);
756}
757
758/**
759 * ipu_update_channel_buffer() - update physical address of a channel buffer.
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700760 * @ichan: IDMAC channel.
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700761 * @buffer_n: buffer number to update.
762 * 0 or 1 are the only valid values.
763 * @phyaddr: buffer physical address.
764 * @return: Returns 0 on success or negative error code on failure. This
765 * function will fail if the buffer is set to ready.
766 */
767/* Called under spin_lock(_irqsave)(&ichan->lock) */
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700768static int ipu_update_channel_buffer(struct idmac_channel *ichan,
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700769 int buffer_n, dma_addr_t phyaddr)
770{
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700771 enum ipu_channel channel = ichan->dma_chan.chan_id;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700772 uint32_t reg;
773 unsigned long flags;
774
775 spin_lock_irqsave(&ipu_data.lock, flags);
776
777 if (buffer_n == 0) {
778 reg = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF0_RDY);
779 if (reg & (1UL << channel)) {
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700780 ipu_ic_disable_task(&ipu_data, channel);
781 ichan->status = IPU_CHANNEL_READY;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700782 }
783
784 /* 44.3.3.1.9 - Row Number 1 (WORD1, offset 0) */
785 idmac_write_ipureg(&ipu_data, dma_param_addr(channel) +
786 0x0008UL, IPU_IMA_ADDR);
787 idmac_write_ipureg(&ipu_data, phyaddr, IPU_IMA_DATA);
788 } else {
789 reg = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF1_RDY);
790 if (reg & (1UL << channel)) {
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700791 ipu_ic_disable_task(&ipu_data, channel);
792 ichan->status = IPU_CHANNEL_READY;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700793 }
794
795 /* Check if double-buffering is already enabled */
796 reg = idmac_read_ipureg(&ipu_data, IPU_CHA_DB_MODE_SEL);
797
798 if (!(reg & (1UL << channel)))
799 idmac_write_ipureg(&ipu_data, reg | (1UL << channel),
800 IPU_CHA_DB_MODE_SEL);
801
802 /* 44.3.3.1.9 - Row Number 1 (WORD1, offset 1) */
803 idmac_write_ipureg(&ipu_data, dma_param_addr(channel) +
804 0x0009UL, IPU_IMA_ADDR);
805 idmac_write_ipureg(&ipu_data, phyaddr, IPU_IMA_DATA);
806 }
807
808 spin_unlock_irqrestore(&ipu_data.lock, flags);
809
810 return 0;
811}
812
813/* Called under spin_lock_irqsave(&ichan->lock) */
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700814static int ipu_submit_buffer(struct idmac_channel *ichan,
815 struct idmac_tx_desc *desc, struct scatterlist *sg, int buf_idx)
816{
817 unsigned int chan_id = ichan->dma_chan.chan_id;
818 struct device *dev = &ichan->dma_chan.dev->device;
819 int ret;
820
821 if (async_tx_test_ack(&desc->txd))
822 return -EINTR;
823
824 /*
825 * On first invocation this shouldn't be necessary, the call to
826 * ipu_init_channel_buffer() above will set addresses for us, so we
827 * could make it conditional on status >= IPU_CHANNEL_ENABLED, but
828 * doing it again shouldn't hurt either.
829 */
830 ret = ipu_update_channel_buffer(ichan, buf_idx,
831 sg_dma_address(sg));
832
833 if (ret < 0) {
834 dev_err(dev, "Updating sg %p on channel 0x%x buffer %d failed!\n",
835 sg, chan_id, buf_idx);
836 return ret;
837 }
838
839 ipu_select_buffer(chan_id, buf_idx);
840 dev_dbg(dev, "Updated sg %p on channel 0x%x buffer %d\n",
841 sg, chan_id, buf_idx);
842
843 return 0;
844}
845
846/* Called under spin_lock_irqsave(&ichan->lock) */
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700847static int ipu_submit_channel_buffers(struct idmac_channel *ichan,
848 struct idmac_tx_desc *desc)
849{
850 struct scatterlist *sg;
851 int i, ret = 0;
852
853 for (i = 0, sg = desc->sg; i < 2 && sg; i++) {
854 if (!ichan->sg[i]) {
855 ichan->sg[i] = sg;
856
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700857 ret = ipu_submit_buffer(ichan, desc, sg, i);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700858 if (ret < 0)
859 return ret;
860
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700861 sg = sg_next(sg);
862 }
863 }
864
865 return ret;
866}
867
868static dma_cookie_t idmac_tx_submit(struct dma_async_tx_descriptor *tx)
869{
870 struct idmac_tx_desc *desc = to_tx_desc(tx);
871 struct idmac_channel *ichan = to_idmac_chan(tx->chan);
872 struct idmac *idmac = to_idmac(tx->chan->device);
873 struct ipu *ipu = to_ipu(idmac);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700874 struct device *dev = &ichan->dma_chan.dev->device;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700875 dma_cookie_t cookie;
876 unsigned long flags;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700877 int ret;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700878
879 /* Sanity check */
880 if (!list_empty(&desc->list)) {
881 /* The descriptor doesn't belong to client */
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700882 dev_err(dev, "Descriptor %p not prepared!\n", tx);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700883 return -EBUSY;
884 }
885
886 mutex_lock(&ichan->chan_mutex);
887
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700888 async_tx_clear_ack(tx);
889
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700890 if (ichan->status < IPU_CHANNEL_READY) {
891 struct idmac_video_param *video = &ichan->params.video;
892 /*
893 * Initial buffer assignment - the first two sg-entries from
894 * the descriptor will end up in the IDMAC buffers
895 */
896 dma_addr_t dma_1 = sg_is_last(desc->sg) ? 0 :
897 sg_dma_address(&desc->sg[1]);
898
899 WARN_ON(ichan->sg[0] || ichan->sg[1]);
900
901 cookie = ipu_init_channel_buffer(ichan,
902 video->out_pixel_fmt,
903 video->out_width,
904 video->out_height,
905 video->out_stride,
906 IPU_ROTATE_NONE,
907 sg_dma_address(&desc->sg[0]),
908 dma_1);
909 if (cookie < 0)
910 goto out;
911 }
912
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700913 dev_dbg(dev, "Submitting sg %p\n", &desc->sg[0]);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700914
915 cookie = ichan->dma_chan.cookie;
916
917 if (++cookie < 0)
918 cookie = 1;
919
920 /* from dmaengine.h: "last cookie value returned to client" */
921 ichan->dma_chan.cookie = cookie;
922 tx->cookie = cookie;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700923
924 /* ipu->lock can be taken under ichan->lock, but not v.v. */
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700925 spin_lock_irqsave(&ichan->lock, flags);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700926
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700927 list_add_tail(&desc->list, &ichan->queue);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700928 /* submit_buffers() atomically verifies and fills empty sg slots */
929 ret = ipu_submit_channel_buffers(ichan, desc);
930
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700931 spin_unlock_irqrestore(&ichan->lock, flags);
932
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700933 if (ret < 0) {
934 cookie = ret;
935 goto dequeue;
936 }
937
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700938 if (ichan->status < IPU_CHANNEL_ENABLED) {
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700939 ret = ipu_enable_channel(idmac, ichan);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700940 if (ret < 0) {
941 cookie = ret;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700942 goto dequeue;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700943 }
944 }
945
946 dump_idmac_reg(ipu);
947
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700948dequeue:
949 if (cookie < 0) {
950 spin_lock_irqsave(&ichan->lock, flags);
951 list_del_init(&desc->list);
952 spin_unlock_irqrestore(&ichan->lock, flags);
953 tx->cookie = cookie;
954 ichan->dma_chan.cookie = cookie;
955 }
956
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700957out:
958 mutex_unlock(&ichan->chan_mutex);
959
960 return cookie;
961}
962
963/* Called with ichan->chan_mutex held */
964static int idmac_desc_alloc(struct idmac_channel *ichan, int n)
965{
966 struct idmac_tx_desc *desc = vmalloc(n * sizeof(struct idmac_tx_desc));
967 struct idmac *idmac = to_idmac(ichan->dma_chan.device);
968
969 if (!desc)
970 return -ENOMEM;
971
972 /* No interrupts, just disable the tasklet for a moment */
973 tasklet_disable(&to_ipu(idmac)->tasklet);
974
975 ichan->n_tx_desc = n;
976 ichan->desc = desc;
977 INIT_LIST_HEAD(&ichan->queue);
978 INIT_LIST_HEAD(&ichan->free_list);
979
980 while (n--) {
981 struct dma_async_tx_descriptor *txd = &desc->txd;
982
983 memset(txd, 0, sizeof(*txd));
984 dma_async_tx_descriptor_init(txd, &ichan->dma_chan);
985 txd->tx_submit = idmac_tx_submit;
986 txd->chan = &ichan->dma_chan;
987 INIT_LIST_HEAD(&txd->tx_list);
988
989 list_add(&desc->list, &ichan->free_list);
990
991 desc++;
992 }
993
994 tasklet_enable(&to_ipu(idmac)->tasklet);
995
996 return 0;
997}
998
999/**
1000 * ipu_init_channel() - initialize an IPU channel.
1001 * @idmac: IPU DMAC context.
1002 * @ichan: pointer to the channel object.
1003 * @return 0 on success or negative error code on failure.
1004 */
1005static int ipu_init_channel(struct idmac *idmac, struct idmac_channel *ichan)
1006{
1007 union ipu_channel_param *params = &ichan->params;
1008 uint32_t ipu_conf;
1009 enum ipu_channel channel = ichan->dma_chan.chan_id;
1010 unsigned long flags;
1011 uint32_t reg;
1012 struct ipu *ipu = to_ipu(idmac);
1013 int ret = 0, n_desc = 0;
1014
1015 dev_dbg(ipu->dev, "init channel = %d\n", channel);
1016
1017 if (channel != IDMAC_SDC_0 && channel != IDMAC_SDC_1 &&
1018 channel != IDMAC_IC_7)
1019 return -EINVAL;
1020
1021 spin_lock_irqsave(&ipu->lock, flags);
1022
1023 switch (channel) {
1024 case IDMAC_IC_7:
1025 n_desc = 16;
1026 reg = idmac_read_icreg(ipu, IC_CONF);
1027 idmac_write_icreg(ipu, reg & ~IC_CONF_CSI_MEM_WR_EN, IC_CONF);
1028 break;
1029 case IDMAC_IC_0:
1030 n_desc = 16;
1031 reg = idmac_read_ipureg(ipu, IPU_FS_PROC_FLOW);
1032 idmac_write_ipureg(ipu, reg & ~FS_ENC_IN_VALID, IPU_FS_PROC_FLOW);
1033 ret = ipu_ic_init_prpenc(ipu, params, true);
1034 break;
1035 case IDMAC_SDC_0:
1036 case IDMAC_SDC_1:
1037 n_desc = 4;
1038 default:
1039 break;
1040 }
1041
1042 ipu->channel_init_mask |= 1L << channel;
1043
1044 /* Enable IPU sub module */
1045 ipu_conf = idmac_read_ipureg(ipu, IPU_CONF) |
1046 ipu_channel_conf_mask(channel);
1047 idmac_write_ipureg(ipu, ipu_conf, IPU_CONF);
1048
1049 spin_unlock_irqrestore(&ipu->lock, flags);
1050
1051 if (n_desc && !ichan->desc)
1052 ret = idmac_desc_alloc(ichan, n_desc);
1053
1054 dump_idmac_reg(ipu);
1055
1056 return ret;
1057}
1058
1059/**
1060 * ipu_uninit_channel() - uninitialize an IPU channel.
1061 * @idmac: IPU DMAC context.
1062 * @ichan: pointer to the channel object.
1063 */
1064static void ipu_uninit_channel(struct idmac *idmac, struct idmac_channel *ichan)
1065{
1066 enum ipu_channel channel = ichan->dma_chan.chan_id;
1067 unsigned long flags;
1068 uint32_t reg;
1069 unsigned long chan_mask = 1UL << channel;
1070 uint32_t ipu_conf;
1071 struct ipu *ipu = to_ipu(idmac);
1072
1073 spin_lock_irqsave(&ipu->lock, flags);
1074
1075 if (!(ipu->channel_init_mask & chan_mask)) {
1076 dev_err(ipu->dev, "Channel already uninitialized %d\n",
1077 channel);
1078 spin_unlock_irqrestore(&ipu->lock, flags);
1079 return;
1080 }
1081
1082 /* Reset the double buffer */
1083 reg = idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL);
1084 idmac_write_ipureg(ipu, reg & ~chan_mask, IPU_CHA_DB_MODE_SEL);
1085
1086 ichan->sec_chan_en = false;
1087
1088 switch (channel) {
1089 case IDMAC_IC_7:
1090 reg = idmac_read_icreg(ipu, IC_CONF);
1091 idmac_write_icreg(ipu, reg & ~(IC_CONF_RWS_EN | IC_CONF_PRPENC_EN),
1092 IC_CONF);
1093 break;
1094 case IDMAC_IC_0:
1095 reg = idmac_read_icreg(ipu, IC_CONF);
1096 idmac_write_icreg(ipu, reg & ~(IC_CONF_PRPENC_EN | IC_CONF_PRPENC_CSC1),
1097 IC_CONF);
1098 break;
1099 case IDMAC_SDC_0:
1100 case IDMAC_SDC_1:
1101 default:
1102 break;
1103 }
1104
1105 ipu->channel_init_mask &= ~(1L << channel);
1106
1107 ipu_conf = idmac_read_ipureg(ipu, IPU_CONF) &
1108 ~ipu_channel_conf_mask(channel);
1109 idmac_write_ipureg(ipu, ipu_conf, IPU_CONF);
1110
1111 spin_unlock_irqrestore(&ipu->lock, flags);
1112
1113 ichan->n_tx_desc = 0;
1114 vfree(ichan->desc);
1115 ichan->desc = NULL;
1116}
1117
1118/**
1119 * ipu_disable_channel() - disable an IPU channel.
1120 * @idmac: IPU DMAC context.
1121 * @ichan: channel object pointer.
1122 * @wait_for_stop: flag to set whether to wait for channel end of frame or
1123 * return immediately.
1124 * @return: 0 on success or negative error code on failure.
1125 */
1126static int ipu_disable_channel(struct idmac *idmac, struct idmac_channel *ichan,
1127 bool wait_for_stop)
1128{
1129 enum ipu_channel channel = ichan->dma_chan.chan_id;
1130 struct ipu *ipu = to_ipu(idmac);
1131 uint32_t reg;
1132 unsigned long flags;
1133 unsigned long chan_mask = 1UL << channel;
1134 unsigned int timeout;
1135
1136 if (wait_for_stop && channel != IDMAC_SDC_1 && channel != IDMAC_SDC_0) {
1137 timeout = 40;
1138 /* This waiting always fails. Related to spurious irq problem */
1139 while ((idmac_read_icreg(ipu, IDMAC_CHA_BUSY) & chan_mask) ||
1140 (ipu_channel_status(ipu, channel) == TASK_STAT_ACTIVE)) {
1141 timeout--;
1142 msleep(10);
1143
1144 if (!timeout) {
1145 dev_dbg(ipu->dev,
1146 "Warning: timeout waiting for channel %u to "
1147 "stop: buf0_rdy = 0x%08X, buf1_rdy = 0x%08X, "
1148 "busy = 0x%08X, tstat = 0x%08X\n", channel,
1149 idmac_read_ipureg(ipu, IPU_CHA_BUF0_RDY),
1150 idmac_read_ipureg(ipu, IPU_CHA_BUF1_RDY),
1151 idmac_read_icreg(ipu, IDMAC_CHA_BUSY),
1152 idmac_read_ipureg(ipu, IPU_TASKS_STAT));
1153 break;
1154 }
1155 }
1156 dev_dbg(ipu->dev, "timeout = %d * 10ms\n", 40 - timeout);
1157 }
1158 /* SDC BG and FG must be disabled before DMA is disabled */
1159 if (wait_for_stop && (channel == IDMAC_SDC_0 ||
1160 channel == IDMAC_SDC_1)) {
1161 for (timeout = 5;
1162 timeout && !ipu_irq_status(ichan->eof_irq); timeout--)
1163 msleep(5);
1164 }
1165
1166 spin_lock_irqsave(&ipu->lock, flags);
1167
1168 /* Disable IC task */
1169 ipu_ic_disable_task(ipu, channel);
1170
1171 /* Disable DMA channel(s) */
1172 reg = idmac_read_icreg(ipu, IDMAC_CHA_EN);
1173 idmac_write_icreg(ipu, reg & ~chan_mask, IDMAC_CHA_EN);
1174
1175 /*
1176 * Problem (observed with channel DMAIC_7): after enabling the channel
1177 * and initialising buffers, there comes an interrupt with current still
1178 * pointing at buffer 0, whereas it should use buffer 0 first and only
1179 * generate an interrupt when it is done, then current should already
1180 * point to buffer 1. This spurious interrupt also comes on channel
1181 * DMASDC_0. With DMAIC_7 normally, is we just leave the ISR after the
1182 * first interrupt, there comes the second with current correctly
1183 * pointing to buffer 1 this time. But sometimes this second interrupt
1184 * doesn't come and the channel hangs. Clearing BUFx_RDY when disabling
1185 * the channel seems to prevent the channel from hanging, but it doesn't
1186 * prevent the spurious interrupt. This might also be unsafe. Think
1187 * about the IDMAC controller trying to switch to a buffer, when we
1188 * clear the ready bit, and re-enable it a moment later.
1189 */
1190 reg = idmac_read_ipureg(ipu, IPU_CHA_BUF0_RDY);
1191 idmac_write_ipureg(ipu, 0, IPU_CHA_BUF0_RDY);
1192 idmac_write_ipureg(ipu, reg & ~(1UL << channel), IPU_CHA_BUF0_RDY);
1193
1194 reg = idmac_read_ipureg(ipu, IPU_CHA_BUF1_RDY);
1195 idmac_write_ipureg(ipu, 0, IPU_CHA_BUF1_RDY);
1196 idmac_write_ipureg(ipu, reg & ~(1UL << channel), IPU_CHA_BUF1_RDY);
1197
1198 spin_unlock_irqrestore(&ipu->lock, flags);
1199
1200 return 0;
1201}
1202
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001203static struct scatterlist *idmac_sg_next(struct idmac_channel *ichan,
1204 struct idmac_tx_desc **desc, struct scatterlist *sg)
1205{
1206 struct scatterlist *sgnew = sg ? sg_next(sg) : NULL;
1207
1208 if (sgnew)
1209 /* next sg-element in this list */
1210 return sgnew;
1211
1212 if ((*desc)->list.next == &ichan->queue)
1213 /* No more descriptors on the queue */
1214 return NULL;
1215
1216 /* Fetch next descriptor */
1217 *desc = list_entry((*desc)->list.next, struct idmac_tx_desc, list);
1218 return (*desc)->sg;
1219}
1220
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001221/*
1222 * We have several possibilities here:
1223 * current BUF next BUF
1224 *
1225 * not last sg next not last sg
1226 * not last sg next last sg
1227 * last sg first sg from next descriptor
1228 * last sg NULL
1229 *
1230 * Besides, the descriptor queue might be empty or not. We process all these
1231 * cases carefully.
1232 */
1233static irqreturn_t idmac_interrupt(int irq, void *dev_id)
1234{
1235 struct idmac_channel *ichan = dev_id;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001236 struct device *dev = &ichan->dma_chan.dev->device;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001237 unsigned int chan_id = ichan->dma_chan.chan_id;
1238 struct scatterlist **sg, *sgnext, *sgnew = NULL;
1239 /* Next transfer descriptor */
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001240 struct idmac_tx_desc *desc, *descnew;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001241 dma_async_tx_callback callback;
1242 void *callback_param;
1243 bool done = false;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001244 u32 ready0, ready1, curbuf, err;
1245 unsigned long flags;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001246
1247 /* IDMAC has cleared the respective BUFx_RDY bit, we manage the buffer */
1248
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001249 dev_dbg(dev, "IDMAC irq %d, buf %d\n", irq, ichan->active_buffer);
1250
1251 spin_lock_irqsave(&ipu_data.lock, flags);
1252
1253 ready0 = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF0_RDY);
1254 ready1 = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF1_RDY);
1255 curbuf = idmac_read_ipureg(&ipu_data, IPU_CHA_CUR_BUF);
1256 err = idmac_read_ipureg(&ipu_data, IPU_INT_STAT_4);
1257
1258 if (err & (1 << chan_id)) {
1259 idmac_write_ipureg(&ipu_data, 1 << chan_id, IPU_INT_STAT_4);
1260 spin_unlock_irqrestore(&ipu_data.lock, flags);
1261 /*
1262 * Doing this
1263 * ichan->sg[0] = ichan->sg[1] = NULL;
1264 * you can force channel re-enable on the next tx_submit(), but
1265 * this is dirty - think about descriptors with multiple
1266 * sg elements.
1267 */
1268 dev_warn(dev, "NFB4EOF on channel %d, ready %x, %x, cur %x\n",
1269 chan_id, ready0, ready1, curbuf);
1270 return IRQ_HANDLED;
1271 }
1272 spin_unlock_irqrestore(&ipu_data.lock, flags);
1273
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001274 /* Other interrupts do not interfere with this channel */
1275 spin_lock(&ichan->lock);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001276 if (unlikely(chan_id != IDMAC_SDC_0 && chan_id != IDMAC_SDC_1 &&
1277 ((curbuf >> chan_id) & 1) == ichan->active_buffer)) {
1278 int i = 100;
1279
1280 /* This doesn't help. See comment in ipu_disable_channel() */
1281 while (--i) {
1282 curbuf = idmac_read_ipureg(&ipu_data, IPU_CHA_CUR_BUF);
1283 if (((curbuf >> chan_id) & 1) != ichan->active_buffer)
1284 break;
1285 cpu_relax();
1286 }
1287
1288 if (!i) {
1289 spin_unlock(&ichan->lock);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001290 dev_dbg(dev,
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001291 "IRQ on active buffer on channel %x, active "
1292 "%d, ready %x, %x, current %x!\n", chan_id,
1293 ichan->active_buffer, ready0, ready1, curbuf);
1294 return IRQ_NONE;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001295 } else
1296 dev_dbg(dev,
1297 "Buffer deactivated on channel %x, active "
1298 "%d, ready %x, %x, current %x, rest %d!\n", chan_id,
1299 ichan->active_buffer, ready0, ready1, curbuf, i);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001300 }
1301
1302 if (unlikely((ichan->active_buffer && (ready1 >> chan_id) & 1) ||
1303 (!ichan->active_buffer && (ready0 >> chan_id) & 1)
1304 )) {
1305 spin_unlock(&ichan->lock);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001306 dev_dbg(dev,
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001307 "IRQ with active buffer still ready on channel %x, "
1308 "active %d, ready %x, %x!\n", chan_id,
1309 ichan->active_buffer, ready0, ready1);
1310 return IRQ_NONE;
1311 }
1312
1313 if (unlikely(list_empty(&ichan->queue))) {
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001314 ichan->sg[ichan->active_buffer] = NULL;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001315 spin_unlock(&ichan->lock);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001316 dev_err(dev,
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001317 "IRQ without queued buffers on channel %x, active %d, "
1318 "ready %x, %x!\n", chan_id,
1319 ichan->active_buffer, ready0, ready1);
1320 return IRQ_NONE;
1321 }
1322
1323 /*
1324 * active_buffer is a software flag, it shows which buffer we are
1325 * currently expecting back from the hardware, IDMAC should be
1326 * processing the other buffer already
1327 */
1328 sg = &ichan->sg[ichan->active_buffer];
1329 sgnext = ichan->sg[!ichan->active_buffer];
1330
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001331 if (!*sg) {
1332 spin_unlock(&ichan->lock);
1333 return IRQ_HANDLED;
1334 }
1335
1336 desc = list_entry(ichan->queue.next, struct idmac_tx_desc, list);
1337 descnew = desc;
1338
1339 dev_dbg(dev, "IDMAC irq %d, dma 0x%08x, next dma 0x%08x, current %d, curbuf 0x%08x\n",
1340 irq, sg_dma_address(*sg), sgnext ? sg_dma_address(sgnext) : 0, ichan->active_buffer, curbuf);
1341
1342 /* Find the descriptor of sgnext */
1343 sgnew = idmac_sg_next(ichan, &descnew, *sg);
1344 if (sgnext != sgnew)
1345 dev_err(dev, "Submitted buffer %p, next buffer %p\n", sgnext, sgnew);
1346
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001347 /*
1348 * if sgnext == NULL sg must be the last element in a scatterlist and
1349 * queue must be empty
1350 */
1351 if (unlikely(!sgnext)) {
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001352 if (!WARN_ON(sg_next(*sg)))
1353 dev_dbg(dev, "Underrun on channel %x\n", chan_id);
1354 ichan->sg[!ichan->active_buffer] = sgnew;
1355
1356 if (unlikely(sgnew)) {
1357 ipu_submit_buffer(ichan, descnew, sgnew, !ichan->active_buffer);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001358 } else {
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001359 spin_lock_irqsave(&ipu_data.lock, flags);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001360 ipu_ic_disable_task(&ipu_data, chan_id);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001361 spin_unlock_irqrestore(&ipu_data.lock, flags);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001362 ichan->status = IPU_CHANNEL_READY;
1363 /* Continue to check for complete descriptor */
1364 }
1365 }
1366
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001367 /* Calculate and submit the next sg element */
1368 sgnew = idmac_sg_next(ichan, &descnew, sgnew);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001369
1370 if (unlikely(!sg_next(*sg)) || !sgnext) {
1371 /*
1372 * Last element in scatterlist done, remove from the queue,
1373 * _init for debugging
1374 */
1375 list_del_init(&desc->list);
1376 done = true;
1377 }
1378
1379 *sg = sgnew;
1380
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001381 if (likely(sgnew) &&
1382 ipu_submit_buffer(ichan, descnew, sgnew, ichan->active_buffer) < 0) {
1383 callback = desc->txd.callback;
1384 callback_param = desc->txd.callback_param;
1385 spin_unlock(&ichan->lock);
1386 callback(callback_param);
1387 spin_lock(&ichan->lock);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001388 }
1389
1390 /* Flip the active buffer - even if update above failed */
1391 ichan->active_buffer = !ichan->active_buffer;
1392 if (done)
1393 ichan->completed = desc->txd.cookie;
1394
1395 callback = desc->txd.callback;
1396 callback_param = desc->txd.callback_param;
1397
1398 spin_unlock(&ichan->lock);
1399
1400 if (done && (desc->txd.flags & DMA_PREP_INTERRUPT) && callback)
1401 callback(callback_param);
1402
1403 return IRQ_HANDLED;
1404}
1405
1406static void ipu_gc_tasklet(unsigned long arg)
1407{
1408 struct ipu *ipu = (struct ipu *)arg;
1409 int i;
1410
1411 for (i = 0; i < IPU_CHANNELS_NUM; i++) {
1412 struct idmac_channel *ichan = ipu->channel + i;
1413 struct idmac_tx_desc *desc;
1414 unsigned long flags;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001415 struct scatterlist *sg;
1416 int j, k;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001417
1418 for (j = 0; j < ichan->n_tx_desc; j++) {
1419 desc = ichan->desc + j;
1420 spin_lock_irqsave(&ichan->lock, flags);
1421 if (async_tx_test_ack(&desc->txd)) {
1422 list_move(&desc->list, &ichan->free_list);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001423 for_each_sg(desc->sg, sg, desc->sg_len, k) {
1424 if (ichan->sg[0] == sg)
1425 ichan->sg[0] = NULL;
1426 else if (ichan->sg[1] == sg)
1427 ichan->sg[1] = NULL;
1428 }
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001429 async_tx_clear_ack(&desc->txd);
1430 }
1431 spin_unlock_irqrestore(&ichan->lock, flags);
1432 }
1433 }
1434}
1435
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -07001436/* Allocate and initialise a transfer descriptor. */
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001437static struct dma_async_tx_descriptor *idmac_prep_slave_sg(struct dma_chan *chan,
1438 struct scatterlist *sgl, unsigned int sg_len,
1439 enum dma_data_direction direction, unsigned long tx_flags)
1440{
1441 struct idmac_channel *ichan = to_idmac_chan(chan);
1442 struct idmac_tx_desc *desc = NULL;
1443 struct dma_async_tx_descriptor *txd = NULL;
1444 unsigned long flags;
1445
1446 /* We only can handle these three channels so far */
1447 if (ichan->dma_chan.chan_id != IDMAC_SDC_0 && ichan->dma_chan.chan_id != IDMAC_SDC_1 &&
1448 ichan->dma_chan.chan_id != IDMAC_IC_7)
1449 return NULL;
1450
1451 if (direction != DMA_FROM_DEVICE && direction != DMA_TO_DEVICE) {
1452 dev_err(chan->device->dev, "Invalid DMA direction %d!\n", direction);
1453 return NULL;
1454 }
1455
1456 mutex_lock(&ichan->chan_mutex);
1457
1458 spin_lock_irqsave(&ichan->lock, flags);
1459 if (!list_empty(&ichan->free_list)) {
1460 desc = list_entry(ichan->free_list.next,
1461 struct idmac_tx_desc, list);
1462
1463 list_del_init(&desc->list);
1464
1465 desc->sg_len = sg_len;
1466 desc->sg = sgl;
1467 txd = &desc->txd;
1468 txd->flags = tx_flags;
1469 }
1470 spin_unlock_irqrestore(&ichan->lock, flags);
1471
1472 mutex_unlock(&ichan->chan_mutex);
1473
1474 tasklet_schedule(&to_ipu(to_idmac(chan->device))->tasklet);
1475
1476 return txd;
1477}
1478
1479/* Re-select the current buffer and re-activate the channel */
1480static void idmac_issue_pending(struct dma_chan *chan)
1481{
1482 struct idmac_channel *ichan = to_idmac_chan(chan);
1483 struct idmac *idmac = to_idmac(chan->device);
1484 struct ipu *ipu = to_ipu(idmac);
1485 unsigned long flags;
1486
1487 /* This is not always needed, but doesn't hurt either */
1488 spin_lock_irqsave(&ipu->lock, flags);
1489 ipu_select_buffer(ichan->dma_chan.chan_id, ichan->active_buffer);
1490 spin_unlock_irqrestore(&ipu->lock, flags);
1491
1492 /*
1493 * Might need to perform some parts of initialisation from
1494 * ipu_enable_channel(), but not all, we do not want to reset to buffer
1495 * 0, don't need to set priority again either, but re-enabling the task
1496 * and the channel might be a good idea.
1497 */
1498}
1499
1500static void __idmac_terminate_all(struct dma_chan *chan)
1501{
1502 struct idmac_channel *ichan = to_idmac_chan(chan);
1503 struct idmac *idmac = to_idmac(chan->device);
1504 unsigned long flags;
1505 int i;
1506
1507 ipu_disable_channel(idmac, ichan,
1508 ichan->status >= IPU_CHANNEL_ENABLED);
1509
1510 tasklet_disable(&to_ipu(idmac)->tasklet);
1511
1512 /* ichan->queue is modified in ISR, have to spinlock */
1513 spin_lock_irqsave(&ichan->lock, flags);
1514 list_splice_init(&ichan->queue, &ichan->free_list);
1515
1516 if (ichan->desc)
1517 for (i = 0; i < ichan->n_tx_desc; i++) {
1518 struct idmac_tx_desc *desc = ichan->desc + i;
1519 if (list_empty(&desc->list))
1520 /* Descriptor was prepared, but not submitted */
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -07001521 list_add(&desc->list, &ichan->free_list);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001522
1523 async_tx_clear_ack(&desc->txd);
1524 }
1525
1526 ichan->sg[0] = NULL;
1527 ichan->sg[1] = NULL;
1528 spin_unlock_irqrestore(&ichan->lock, flags);
1529
1530 tasklet_enable(&to_ipu(idmac)->tasklet);
1531
1532 ichan->status = IPU_CHANNEL_INITIALIZED;
1533}
1534
1535static void idmac_terminate_all(struct dma_chan *chan)
1536{
1537 struct idmac_channel *ichan = to_idmac_chan(chan);
1538
1539 mutex_lock(&ichan->chan_mutex);
1540
1541 __idmac_terminate_all(chan);
1542
1543 mutex_unlock(&ichan->chan_mutex);
1544}
1545
1546static int idmac_alloc_chan_resources(struct dma_chan *chan)
1547{
1548 struct idmac_channel *ichan = to_idmac_chan(chan);
1549 struct idmac *idmac = to_idmac(chan->device);
1550 int ret;
1551
1552 /* dmaengine.c now guarantees to only offer free channels */
1553 BUG_ON(chan->client_count > 1);
1554 WARN_ON(ichan->status != IPU_CHANNEL_FREE);
1555
1556 chan->cookie = 1;
1557 ichan->completed = -ENXIO;
1558
1559 ret = ipu_irq_map(ichan->dma_chan.chan_id);
1560 if (ret < 0)
1561 goto eimap;
1562
1563 ichan->eof_irq = ret;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001564
1565 /*
1566 * Important to first disable the channel, because maybe someone
1567 * used it before us, e.g., the bootloader
1568 */
1569 ipu_disable_channel(idmac, ichan, true);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001570
1571 ret = ipu_init_channel(idmac, ichan);
1572 if (ret < 0)
1573 goto eichan;
1574
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001575 ret = request_irq(ichan->eof_irq, idmac_interrupt, 0,
1576 ichan->eof_name, ichan);
1577 if (ret < 0)
1578 goto erirq;
1579
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001580 ichan->status = IPU_CHANNEL_INITIALIZED;
1581
1582 dev_dbg(&ichan->dma_chan.dev->device, "Found channel 0x%x, irq %d\n",
1583 ichan->dma_chan.chan_id, ichan->eof_irq);
1584
1585 return ret;
1586
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001587erirq:
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001588 ipu_uninit_channel(idmac, ichan);
1589eichan:
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001590 ipu_irq_unmap(ichan->dma_chan.chan_id);
1591eimap:
1592 return ret;
1593}
1594
1595static void idmac_free_chan_resources(struct dma_chan *chan)
1596{
1597 struct idmac_channel *ichan = to_idmac_chan(chan);
1598 struct idmac *idmac = to_idmac(chan->device);
1599
1600 mutex_lock(&ichan->chan_mutex);
1601
1602 __idmac_terminate_all(chan);
1603
1604 if (ichan->status > IPU_CHANNEL_FREE) {
1605 free_irq(ichan->eof_irq, ichan);
1606 ipu_irq_unmap(ichan->dma_chan.chan_id);
1607 }
1608
1609 ichan->status = IPU_CHANNEL_FREE;
1610
1611 ipu_uninit_channel(idmac, ichan);
1612
1613 mutex_unlock(&ichan->chan_mutex);
1614
1615 tasklet_schedule(&to_ipu(idmac)->tasklet);
1616}
1617
1618static enum dma_status idmac_is_tx_complete(struct dma_chan *chan,
1619 dma_cookie_t cookie, dma_cookie_t *done, dma_cookie_t *used)
1620{
1621 struct idmac_channel *ichan = to_idmac_chan(chan);
1622
1623 if (done)
1624 *done = ichan->completed;
1625 if (used)
1626 *used = chan->cookie;
1627 if (cookie != chan->cookie)
1628 return DMA_ERROR;
1629 return DMA_SUCCESS;
1630}
1631
1632static int __init ipu_idmac_init(struct ipu *ipu)
1633{
1634 struct idmac *idmac = &ipu->idmac;
1635 struct dma_device *dma = &idmac->dma;
1636 int i;
1637
1638 dma_cap_set(DMA_SLAVE, dma->cap_mask);
1639 dma_cap_set(DMA_PRIVATE, dma->cap_mask);
1640
1641 /* Compulsory common fields */
1642 dma->dev = ipu->dev;
1643 dma->device_alloc_chan_resources = idmac_alloc_chan_resources;
1644 dma->device_free_chan_resources = idmac_free_chan_resources;
1645 dma->device_is_tx_complete = idmac_is_tx_complete;
1646 dma->device_issue_pending = idmac_issue_pending;
1647
1648 /* Compulsory for DMA_SLAVE fields */
1649 dma->device_prep_slave_sg = idmac_prep_slave_sg;
1650 dma->device_terminate_all = idmac_terminate_all;
1651
1652 INIT_LIST_HEAD(&dma->channels);
1653 for (i = 0; i < IPU_CHANNELS_NUM; i++) {
1654 struct idmac_channel *ichan = ipu->channel + i;
1655 struct dma_chan *dma_chan = &ichan->dma_chan;
1656
1657 spin_lock_init(&ichan->lock);
1658 mutex_init(&ichan->chan_mutex);
1659
1660 ichan->status = IPU_CHANNEL_FREE;
1661 ichan->sec_chan_en = false;
1662 ichan->completed = -ENXIO;
1663 snprintf(ichan->eof_name, sizeof(ichan->eof_name), "IDMAC EOF %d", i);
1664
1665 dma_chan->device = &idmac->dma;
1666 dma_chan->cookie = 1;
1667 dma_chan->chan_id = i;
1668 list_add_tail(&ichan->dma_chan.device_node, &dma->channels);
1669 }
1670
1671 idmac_write_icreg(ipu, 0x00000070, IDMAC_CONF);
1672
1673 return dma_async_device_register(&idmac->dma);
1674}
1675
Guennadi Liakhovetski234f2df2009-03-25 09:13:24 -07001676static void __exit ipu_idmac_exit(struct ipu *ipu)
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001677{
1678 int i;
1679 struct idmac *idmac = &ipu->idmac;
1680
1681 for (i = 0; i < IPU_CHANNELS_NUM; i++) {
1682 struct idmac_channel *ichan = ipu->channel + i;
1683
1684 idmac_terminate_all(&ichan->dma_chan);
1685 idmac_prep_slave_sg(&ichan->dma_chan, NULL, 0, DMA_NONE, 0);
1686 }
1687
1688 dma_async_device_unregister(&idmac->dma);
1689}
1690
1691/*****************************************************************************
1692 * IPU common probe / remove
1693 */
1694
Guennadi Liakhovetski234f2df2009-03-25 09:13:24 -07001695static int __init ipu_probe(struct platform_device *pdev)
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001696{
1697 struct ipu_platform_data *pdata = pdev->dev.platform_data;
1698 struct resource *mem_ipu, *mem_ic;
1699 int ret;
1700
1701 spin_lock_init(&ipu_data.lock);
1702
1703 mem_ipu = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1704 mem_ic = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1705 if (!pdata || !mem_ipu || !mem_ic)
1706 return -EINVAL;
1707
1708 ipu_data.dev = &pdev->dev;
1709
1710 platform_set_drvdata(pdev, &ipu_data);
1711
1712 ret = platform_get_irq(pdev, 0);
1713 if (ret < 0)
1714 goto err_noirq;
1715
1716 ipu_data.irq_fn = ret;
1717 ret = platform_get_irq(pdev, 1);
1718 if (ret < 0)
1719 goto err_noirq;
1720
1721 ipu_data.irq_err = ret;
1722 ipu_data.irq_base = pdata->irq_base;
1723
1724 dev_dbg(&pdev->dev, "fn irq %u, err irq %u, irq-base %u\n",
1725 ipu_data.irq_fn, ipu_data.irq_err, ipu_data.irq_base);
1726
1727 /* Remap IPU common registers */
1728 ipu_data.reg_ipu = ioremap(mem_ipu->start,
1729 mem_ipu->end - mem_ipu->start + 1);
1730 if (!ipu_data.reg_ipu) {
1731 ret = -ENOMEM;
1732 goto err_ioremap_ipu;
1733 }
1734
1735 /* Remap Image Converter and Image DMA Controller registers */
1736 ipu_data.reg_ic = ioremap(mem_ic->start,
1737 mem_ic->end - mem_ic->start + 1);
1738 if (!ipu_data.reg_ic) {
1739 ret = -ENOMEM;
1740 goto err_ioremap_ic;
1741 }
1742
1743 /* Get IPU clock */
1744 ipu_data.ipu_clk = clk_get(&pdev->dev, "ipu_clk");
1745 if (IS_ERR(ipu_data.ipu_clk)) {
1746 ret = PTR_ERR(ipu_data.ipu_clk);
1747 goto err_clk_get;
1748 }
1749
1750 /* Make sure IPU HSP clock is running */
1751 clk_enable(ipu_data.ipu_clk);
1752
1753 /* Disable all interrupts */
1754 idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_1);
1755 idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_2);
1756 idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_3);
1757 idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_4);
1758 idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_5);
1759
1760 dev_dbg(&pdev->dev, "%s @ 0x%08lx, fn irq %u, err irq %u\n", pdev->name,
1761 (unsigned long)mem_ipu->start, ipu_data.irq_fn, ipu_data.irq_err);
1762
1763 ret = ipu_irq_attach_irq(&ipu_data, pdev);
1764 if (ret < 0)
1765 goto err_attach_irq;
1766
1767 /* Initialize DMA engine */
1768 ret = ipu_idmac_init(&ipu_data);
1769 if (ret < 0)
1770 goto err_idmac_init;
1771
1772 tasklet_init(&ipu_data.tasklet, ipu_gc_tasklet, (unsigned long)&ipu_data);
1773
1774 ipu_data.dev = &pdev->dev;
1775
1776 dev_dbg(ipu_data.dev, "IPU initialized\n");
1777
1778 return 0;
1779
1780err_idmac_init:
1781err_attach_irq:
1782 ipu_irq_detach_irq(&ipu_data, pdev);
1783 clk_disable(ipu_data.ipu_clk);
1784 clk_put(ipu_data.ipu_clk);
1785err_clk_get:
1786 iounmap(ipu_data.reg_ic);
1787err_ioremap_ic:
1788 iounmap(ipu_data.reg_ipu);
1789err_ioremap_ipu:
1790err_noirq:
1791 dev_err(&pdev->dev, "Failed to probe IPU: %d\n", ret);
1792 return ret;
1793}
1794
Guennadi Liakhovetski234f2df2009-03-25 09:13:24 -07001795static int __exit ipu_remove(struct platform_device *pdev)
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001796{
1797 struct ipu *ipu = platform_get_drvdata(pdev);
1798
1799 ipu_idmac_exit(ipu);
1800 ipu_irq_detach_irq(ipu, pdev);
1801 clk_disable(ipu->ipu_clk);
1802 clk_put(ipu->ipu_clk);
1803 iounmap(ipu->reg_ic);
1804 iounmap(ipu->reg_ipu);
1805 tasklet_kill(&ipu->tasklet);
1806 platform_set_drvdata(pdev, NULL);
1807
1808 return 0;
1809}
1810
1811/*
1812 * We need two MEM resources - with IPU-common and Image Converter registers,
1813 * including PF_CONF and IDMAC_* registers, and two IRQs - function and error
1814 */
1815static struct platform_driver ipu_platform_driver = {
1816 .driver = {
1817 .name = "ipu-core",
1818 .owner = THIS_MODULE,
1819 },
Guennadi Liakhovetski234f2df2009-03-25 09:13:24 -07001820 .remove = __exit_p(ipu_remove),
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001821};
1822
1823static int __init ipu_init(void)
1824{
1825 return platform_driver_probe(&ipu_platform_driver, ipu_probe);
1826}
1827subsys_initcall(ipu_init);
1828
1829MODULE_DESCRIPTION("IPU core driver");
1830MODULE_LICENSE("GPL v2");
1831MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
1832MODULE_ALIAS("platform:ipu-core");