blob: 1c518f1cc49bf3d50fb71910b43bd917a361cbd5 [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:
Roel Kluin9ad7bd22010-01-20 01:25:56 +0100351 case IPU_PIX_FMT_ABGR32:
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700352 params->ip.bpp = 0;
353 params->ip.pfs = 4;
354 params->ip.npb = 7;
355 params->ip.sat = 2; /* SAT = 32-bit access */
356 params->ip.ofs0 = 8; /* Red bit offset */
357 params->ip.ofs1 = 16; /* Green bit offset */
358 params->ip.ofs2 = 24; /* Blue bit offset */
359 params->ip.ofs3 = 0; /* Alpha bit offset */
360 params->ip.wid0 = 7; /* Red bit width - 1 */
361 params->ip.wid1 = 7; /* Green bit width - 1 */
362 params->ip.wid2 = 7; /* Blue bit width - 1 */
363 params->ip.wid3 = 7; /* Alpha bit width - 1 */
364 break;
365 case IPU_PIX_FMT_RGBA32:
366 case IPU_PIX_FMT_RGB32:
367 params->ip.bpp = 0;
368 params->ip.pfs = 4;
369 params->ip.npb = 7;
370 params->ip.sat = 2; /* SAT = 32-bit access */
371 params->ip.ofs0 = 24; /* Red bit offset */
372 params->ip.ofs1 = 16; /* Green bit offset */
373 params->ip.ofs2 = 8; /* Blue bit offset */
374 params->ip.ofs3 = 0; /* Alpha bit offset */
375 params->ip.wid0 = 7; /* Red bit width - 1 */
376 params->ip.wid1 = 7; /* Green bit width - 1 */
377 params->ip.wid2 = 7; /* Blue bit width - 1 */
378 params->ip.wid3 = 7; /* Alpha bit width - 1 */
379 break;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700380 case IPU_PIX_FMT_UYVY:
381 params->ip.bpp = 2;
382 params->ip.pfs = 6;
383 params->ip.npb = 7;
384 params->ip.sat = 2; /* SAT = 32-bit access */
385 break;
386 case IPU_PIX_FMT_YUV420P2:
387 case IPU_PIX_FMT_YUV420P:
388 params->ip.bpp = 3;
389 params->ip.pfs = 3;
390 params->ip.npb = 7;
391 params->ip.sat = 2; /* SAT = 32-bit access */
392 u_offset = stride * height;
393 v_offset = u_offset + u_offset / 4;
394 ipu_ch_param_set_plane_offset(params, u_offset, v_offset);
395 break;
396 case IPU_PIX_FMT_YVU422P:
397 params->ip.bpp = 3;
398 params->ip.pfs = 2;
399 params->ip.npb = 7;
400 params->ip.sat = 2; /* SAT = 32-bit access */
401 v_offset = stride * height;
402 u_offset = v_offset + v_offset / 2;
403 ipu_ch_param_set_plane_offset(params, u_offset, v_offset);
404 break;
405 case IPU_PIX_FMT_YUV422P:
406 params->ip.bpp = 3;
407 params->ip.pfs = 2;
408 params->ip.npb = 7;
409 params->ip.sat = 2; /* SAT = 32-bit access */
410 u_offset = stride * height;
411 v_offset = u_offset + u_offset / 2;
412 ipu_ch_param_set_plane_offset(params, u_offset, v_offset);
413 break;
414 default:
415 dev_err(ipu_data.dev,
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700416 "mx3 ipu: unimplemented pixel format %d\n", pixel_fmt);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700417 break;
418 }
419
420 params->pp.nsb = 1;
421}
422
423static void ipu_ch_param_set_burst_size(union chan_param_mem *params,
424 uint16_t burst_pixels)
425{
426 params->pp.npb = burst_pixels - 1;
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700427}
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700428
429static void ipu_ch_param_set_buffer(union chan_param_mem *params,
430 dma_addr_t buf0, dma_addr_t buf1)
431{
432 params->pp.eba0 = buf0;
433 params->pp.eba1 = buf1;
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700434}
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700435
436static void ipu_ch_param_set_rotation(union chan_param_mem *params,
437 enum ipu_rotate_mode rotate)
438{
439 params->pp.bam = rotate;
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700440}
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700441
442static void ipu_write_param_mem(uint32_t addr, uint32_t *data,
443 uint32_t num_words)
444{
445 for (; num_words > 0; num_words--) {
446 dev_dbg(ipu_data.dev,
447 "write param mem - addr = 0x%08X, data = 0x%08X\n",
448 addr, *data);
449 idmac_write_ipureg(&ipu_data, addr, IPU_IMA_ADDR);
450 idmac_write_ipureg(&ipu_data, *data++, IPU_IMA_DATA);
451 addr++;
452 if ((addr & 0x7) == 5) {
453 addr &= ~0x7; /* set to word 0 */
454 addr += 8; /* increment to next row */
455 }
456 }
457}
458
459static int calc_resize_coeffs(uint32_t in_size, uint32_t out_size,
460 uint32_t *resize_coeff,
461 uint32_t *downsize_coeff)
462{
463 uint32_t temp_size;
464 uint32_t temp_downsize;
465
466 *resize_coeff = 1 << 13;
467 *downsize_coeff = 1 << 13;
468
469 /* Cannot downsize more than 8:1 */
470 if (out_size << 3 < in_size)
471 return -EINVAL;
472
473 /* compute downsizing coefficient */
474 temp_downsize = 0;
475 temp_size = in_size;
476 while (temp_size >= out_size * 2 && temp_downsize < 2) {
477 temp_size >>= 1;
478 temp_downsize++;
479 }
480 *downsize_coeff = temp_downsize;
481
482 /*
483 * compute resizing coefficient using the following formula:
484 * resize_coeff = M*(SI -1)/(SO - 1)
485 * where M = 2^13, SI - input size, SO - output size
486 */
487 *resize_coeff = (8192L * (temp_size - 1)) / (out_size - 1);
488 if (*resize_coeff >= 16384L) {
489 dev_err(ipu_data.dev, "Warning! Overflow on resize coeff.\n");
490 *resize_coeff = 0x3FFF;
491 }
492
493 dev_dbg(ipu_data.dev, "resizing from %u -> %u pixels, "
494 "downsize=%u, resize=%u.%lu (reg=%u)\n", in_size, out_size,
495 *downsize_coeff, *resize_coeff >= 8192L ? 1 : 0,
496 ((*resize_coeff & 0x1FFF) * 10000L) / 8192L, *resize_coeff);
497
498 return 0;
499}
500
501static enum ipu_color_space format_to_colorspace(enum pixel_fmt fmt)
502{
503 switch (fmt) {
504 case IPU_PIX_FMT_RGB565:
505 case IPU_PIX_FMT_BGR24:
506 case IPU_PIX_FMT_RGB24:
507 case IPU_PIX_FMT_BGR32:
508 case IPU_PIX_FMT_RGB32:
509 return IPU_COLORSPACE_RGB;
510 default:
511 return IPU_COLORSPACE_YCBCR;
512 }
513}
514
515static int ipu_ic_init_prpenc(struct ipu *ipu,
516 union ipu_channel_param *params, bool src_is_csi)
517{
518 uint32_t reg, ic_conf;
519 uint32_t downsize_coeff, resize_coeff;
520 enum ipu_color_space in_fmt, out_fmt;
521
522 /* Setup vertical resizing */
523 calc_resize_coeffs(params->video.in_height,
524 params->video.out_height,
525 &resize_coeff, &downsize_coeff);
526 reg = (downsize_coeff << 30) | (resize_coeff << 16);
527
528 /* Setup horizontal resizing */
529 calc_resize_coeffs(params->video.in_width,
530 params->video.out_width,
531 &resize_coeff, &downsize_coeff);
532 reg |= (downsize_coeff << 14) | resize_coeff;
533
534 /* Setup color space conversion */
535 in_fmt = format_to_colorspace(params->video.in_pixel_fmt);
536 out_fmt = format_to_colorspace(params->video.out_pixel_fmt);
537
538 /*
539 * Colourspace conversion unsupported yet - see _init_csc() in
540 * Freescale sources
541 */
542 if (in_fmt != out_fmt) {
543 dev_err(ipu->dev, "Colourspace conversion unsupported!\n");
544 return -EOPNOTSUPP;
545 }
546
547 idmac_write_icreg(ipu, reg, IC_PRP_ENC_RSC);
548
549 ic_conf = idmac_read_icreg(ipu, IC_CONF);
550
551 if (src_is_csi)
552 ic_conf &= ~IC_CONF_RWS_EN;
553 else
554 ic_conf |= IC_CONF_RWS_EN;
555
556 idmac_write_icreg(ipu, ic_conf, IC_CONF);
557
558 return 0;
559}
560
561static uint32_t dma_param_addr(uint32_t dma_ch)
562{
563 /* Channel Parameter Memory */
564 return 0x10000 | (dma_ch << 4);
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700565}
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700566
567static void ipu_channel_set_priority(struct ipu *ipu, enum ipu_channel channel,
568 bool prio)
569{
570 u32 reg = idmac_read_icreg(ipu, IDMAC_CHA_PRI);
571
572 if (prio)
573 reg |= 1UL << channel;
574 else
575 reg &= ~(1UL << channel);
576
577 idmac_write_icreg(ipu, reg, IDMAC_CHA_PRI);
578
579 dump_idmac_reg(ipu);
580}
581
582static uint32_t ipu_channel_conf_mask(enum ipu_channel channel)
583{
584 uint32_t mask;
585
586 switch (channel) {
587 case IDMAC_IC_0:
588 case IDMAC_IC_7:
589 mask = IPU_CONF_CSI_EN | IPU_CONF_IC_EN;
590 break;
591 case IDMAC_SDC_0:
592 case IDMAC_SDC_1:
593 mask = IPU_CONF_SDC_EN | IPU_CONF_DI_EN;
594 break;
595 default:
596 mask = 0;
597 break;
598 }
599
600 return mask;
601}
602
603/**
604 * ipu_enable_channel() - enable an IPU channel.
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700605 * @idmac: IPU DMAC context.
606 * @ichan: IDMAC channel.
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700607 * @return: 0 on success or negative error code on failure.
608 */
609static int ipu_enable_channel(struct idmac *idmac, struct idmac_channel *ichan)
610{
611 struct ipu *ipu = to_ipu(idmac);
612 enum ipu_channel channel = ichan->dma_chan.chan_id;
613 uint32_t reg;
614 unsigned long flags;
615
616 spin_lock_irqsave(&ipu->lock, flags);
617
618 /* Reset to buffer 0 */
619 idmac_write_ipureg(ipu, 1UL << channel, IPU_CHA_CUR_BUF);
620 ichan->active_buffer = 0;
621 ichan->status = IPU_CHANNEL_ENABLED;
622
623 switch (channel) {
624 case IDMAC_SDC_0:
625 case IDMAC_SDC_1:
626 case IDMAC_IC_7:
627 ipu_channel_set_priority(ipu, channel, true);
628 default:
629 break;
630 }
631
632 reg = idmac_read_icreg(ipu, IDMAC_CHA_EN);
633
634 idmac_write_icreg(ipu, reg | (1UL << channel), IDMAC_CHA_EN);
635
636 ipu_ic_enable_task(ipu, channel);
637
638 spin_unlock_irqrestore(&ipu->lock, flags);
639 return 0;
640}
641
642/**
643 * ipu_init_channel_buffer() - initialize a buffer for logical IPU channel.
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700644 * @ichan: IDMAC channel.
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700645 * @pixel_fmt: pixel format of buffer. Pixel format is a FOURCC ASCII code.
646 * @width: width of buffer in pixels.
647 * @height: height of buffer in pixels.
648 * @stride: stride length of buffer in pixels.
649 * @rot_mode: rotation mode of buffer. A rotation setting other than
650 * IPU_ROTATE_VERT_FLIP should only be used for input buffers of
651 * rotation channels.
652 * @phyaddr_0: buffer 0 physical address.
653 * @phyaddr_1: buffer 1 physical address. Setting this to a value other than
654 * NULL enables double buffering mode.
655 * @return: 0 on success or negative error code on failure.
656 */
657static int ipu_init_channel_buffer(struct idmac_channel *ichan,
658 enum pixel_fmt pixel_fmt,
659 uint16_t width, uint16_t height,
660 uint32_t stride,
661 enum ipu_rotate_mode rot_mode,
662 dma_addr_t phyaddr_0, dma_addr_t phyaddr_1)
663{
664 enum ipu_channel channel = ichan->dma_chan.chan_id;
665 struct idmac *idmac = to_idmac(ichan->dma_chan.device);
666 struct ipu *ipu = to_ipu(idmac);
667 union chan_param_mem params = {};
668 unsigned long flags;
669 uint32_t reg;
670 uint32_t stride_bytes;
671
672 stride_bytes = stride * bytes_per_pixel(pixel_fmt);
673
674 if (stride_bytes % 4) {
675 dev_err(ipu->dev,
676 "Stride length must be 32-bit aligned, stride = %d, bytes = %d\n",
677 stride, stride_bytes);
678 return -EINVAL;
679 }
680
681 /* IC channel's stride must be a multiple of 8 pixels */
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700682 if ((channel <= IDMAC_IC_13) && (stride % 8)) {
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700683 dev_err(ipu->dev, "Stride must be 8 pixel multiple\n");
684 return -EINVAL;
685 }
686
687 /* Build parameter memory data for DMA channel */
688 ipu_ch_param_set_size(&params, pixel_fmt, width, height, stride_bytes);
689 ipu_ch_param_set_buffer(&params, phyaddr_0, phyaddr_1);
690 ipu_ch_param_set_rotation(&params, rot_mode);
691 /* Some channels (rotation) have restriction on burst length */
692 switch (channel) {
693 case IDMAC_IC_7: /* Hangs with burst 8, 16, other values
694 invalid - Table 44-30 */
695/*
696 ipu_ch_param_set_burst_size(&params, 8);
697 */
698 break;
699 case IDMAC_SDC_0:
700 case IDMAC_SDC_1:
701 /* In original code only IPU_PIX_FMT_RGB565 was setting burst */
702 ipu_ch_param_set_burst_size(&params, 16);
703 break;
704 case IDMAC_IC_0:
705 default:
706 break;
707 }
708
709 spin_lock_irqsave(&ipu->lock, flags);
710
711 ipu_write_param_mem(dma_param_addr(channel), (uint32_t *)&params, 10);
712
713 reg = idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL);
714
715 if (phyaddr_1)
716 reg |= 1UL << channel;
717 else
718 reg &= ~(1UL << channel);
719
720 idmac_write_ipureg(ipu, reg, IPU_CHA_DB_MODE_SEL);
721
722 ichan->status = IPU_CHANNEL_READY;
723
Luotao Fuc74ef1f2009-02-26 12:29:20 +0100724 spin_unlock_irqrestore(&ipu->lock, flags);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700725
726 return 0;
727}
728
729/**
730 * ipu_select_buffer() - mark a channel's buffer as ready.
731 * @channel: channel ID.
732 * @buffer_n: buffer number to mark ready.
733 */
734static void ipu_select_buffer(enum ipu_channel channel, int buffer_n)
735{
736 /* No locking - this is a write-one-to-set register, cleared by IPU */
737 if (buffer_n == 0)
738 /* Mark buffer 0 as ready. */
739 idmac_write_ipureg(&ipu_data, 1UL << channel, IPU_CHA_BUF0_RDY);
740 else
741 /* Mark buffer 1 as ready. */
742 idmac_write_ipureg(&ipu_data, 1UL << channel, IPU_CHA_BUF1_RDY);
743}
744
745/**
746 * ipu_update_channel_buffer() - update physical address of a channel buffer.
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -0700747 * @ichan: IDMAC channel.
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700748 * @buffer_n: buffer number to update.
749 * 0 or 1 are the only valid values.
750 * @phyaddr: buffer physical address.
751 * @return: Returns 0 on success or negative error code on failure. This
752 * function will fail if the buffer is set to ready.
753 */
754/* Called under spin_lock(_irqsave)(&ichan->lock) */
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700755static int ipu_update_channel_buffer(struct idmac_channel *ichan,
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700756 int buffer_n, dma_addr_t phyaddr)
757{
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700758 enum ipu_channel channel = ichan->dma_chan.chan_id;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700759 uint32_t reg;
760 unsigned long flags;
761
762 spin_lock_irqsave(&ipu_data.lock, flags);
763
764 if (buffer_n == 0) {
765 reg = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF0_RDY);
766 if (reg & (1UL << channel)) {
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700767 ipu_ic_disable_task(&ipu_data, channel);
768 ichan->status = IPU_CHANNEL_READY;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700769 }
770
771 /* 44.3.3.1.9 - Row Number 1 (WORD1, offset 0) */
772 idmac_write_ipureg(&ipu_data, dma_param_addr(channel) +
773 0x0008UL, IPU_IMA_ADDR);
774 idmac_write_ipureg(&ipu_data, phyaddr, IPU_IMA_DATA);
775 } else {
776 reg = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF1_RDY);
777 if (reg & (1UL << channel)) {
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700778 ipu_ic_disable_task(&ipu_data, channel);
779 ichan->status = IPU_CHANNEL_READY;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700780 }
781
782 /* Check if double-buffering is already enabled */
783 reg = idmac_read_ipureg(&ipu_data, IPU_CHA_DB_MODE_SEL);
784
785 if (!(reg & (1UL << channel)))
786 idmac_write_ipureg(&ipu_data, reg | (1UL << channel),
787 IPU_CHA_DB_MODE_SEL);
788
789 /* 44.3.3.1.9 - Row Number 1 (WORD1, offset 1) */
790 idmac_write_ipureg(&ipu_data, dma_param_addr(channel) +
791 0x0009UL, IPU_IMA_ADDR);
792 idmac_write_ipureg(&ipu_data, phyaddr, IPU_IMA_DATA);
793 }
794
795 spin_unlock_irqrestore(&ipu_data.lock, flags);
796
797 return 0;
798}
799
800/* Called under spin_lock_irqsave(&ichan->lock) */
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700801static int ipu_submit_buffer(struct idmac_channel *ichan,
802 struct idmac_tx_desc *desc, struct scatterlist *sg, int buf_idx)
803{
804 unsigned int chan_id = ichan->dma_chan.chan_id;
805 struct device *dev = &ichan->dma_chan.dev->device;
806 int ret;
807
808 if (async_tx_test_ack(&desc->txd))
809 return -EINTR;
810
811 /*
812 * On first invocation this shouldn't be necessary, the call to
813 * ipu_init_channel_buffer() above will set addresses for us, so we
814 * could make it conditional on status >= IPU_CHANNEL_ENABLED, but
815 * doing it again shouldn't hurt either.
816 */
817 ret = ipu_update_channel_buffer(ichan, buf_idx,
818 sg_dma_address(sg));
819
820 if (ret < 0) {
821 dev_err(dev, "Updating sg %p on channel 0x%x buffer %d failed!\n",
822 sg, chan_id, buf_idx);
823 return ret;
824 }
825
826 ipu_select_buffer(chan_id, buf_idx);
827 dev_dbg(dev, "Updated sg %p on channel 0x%x buffer %d\n",
828 sg, chan_id, buf_idx);
829
830 return 0;
831}
832
833/* Called under spin_lock_irqsave(&ichan->lock) */
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700834static int ipu_submit_channel_buffers(struct idmac_channel *ichan,
835 struct idmac_tx_desc *desc)
836{
837 struct scatterlist *sg;
838 int i, ret = 0;
839
840 for (i = 0, sg = desc->sg; i < 2 && sg; i++) {
841 if (!ichan->sg[i]) {
842 ichan->sg[i] = sg;
843
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700844 ret = ipu_submit_buffer(ichan, desc, sg, i);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700845 if (ret < 0)
846 return ret;
847
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700848 sg = sg_next(sg);
849 }
850 }
851
852 return ret;
853}
854
855static dma_cookie_t idmac_tx_submit(struct dma_async_tx_descriptor *tx)
856{
857 struct idmac_tx_desc *desc = to_tx_desc(tx);
858 struct idmac_channel *ichan = to_idmac_chan(tx->chan);
859 struct idmac *idmac = to_idmac(tx->chan->device);
860 struct ipu *ipu = to_ipu(idmac);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700861 struct device *dev = &ichan->dma_chan.dev->device;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700862 dma_cookie_t cookie;
863 unsigned long flags;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700864 int ret;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700865
866 /* Sanity check */
867 if (!list_empty(&desc->list)) {
868 /* The descriptor doesn't belong to client */
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700869 dev_err(dev, "Descriptor %p not prepared!\n", tx);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700870 return -EBUSY;
871 }
872
873 mutex_lock(&ichan->chan_mutex);
874
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700875 async_tx_clear_ack(tx);
876
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700877 if (ichan->status < IPU_CHANNEL_READY) {
878 struct idmac_video_param *video = &ichan->params.video;
879 /*
880 * Initial buffer assignment - the first two sg-entries from
881 * the descriptor will end up in the IDMAC buffers
882 */
883 dma_addr_t dma_1 = sg_is_last(desc->sg) ? 0 :
884 sg_dma_address(&desc->sg[1]);
885
886 WARN_ON(ichan->sg[0] || ichan->sg[1]);
887
888 cookie = ipu_init_channel_buffer(ichan,
889 video->out_pixel_fmt,
890 video->out_width,
891 video->out_height,
892 video->out_stride,
893 IPU_ROTATE_NONE,
894 sg_dma_address(&desc->sg[0]),
895 dma_1);
896 if (cookie < 0)
897 goto out;
898 }
899
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700900 dev_dbg(dev, "Submitting sg %p\n", &desc->sg[0]);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700901
902 cookie = ichan->dma_chan.cookie;
903
904 if (++cookie < 0)
905 cookie = 1;
906
907 /* from dmaengine.h: "last cookie value returned to client" */
908 ichan->dma_chan.cookie = cookie;
909 tx->cookie = cookie;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700910
911 /* ipu->lock can be taken under ichan->lock, but not v.v. */
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700912 spin_lock_irqsave(&ichan->lock, flags);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700913
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700914 list_add_tail(&desc->list, &ichan->queue);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700915 /* submit_buffers() atomically verifies and fills empty sg slots */
916 ret = ipu_submit_channel_buffers(ichan, desc);
917
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700918 spin_unlock_irqrestore(&ichan->lock, flags);
919
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700920 if (ret < 0) {
921 cookie = ret;
922 goto dequeue;
923 }
924
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700925 if (ichan->status < IPU_CHANNEL_ENABLED) {
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700926 ret = ipu_enable_channel(idmac, ichan);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700927 if (ret < 0) {
928 cookie = ret;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700929 goto dequeue;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700930 }
931 }
932
933 dump_idmac_reg(ipu);
934
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -0700935dequeue:
936 if (cookie < 0) {
937 spin_lock_irqsave(&ichan->lock, flags);
938 list_del_init(&desc->list);
939 spin_unlock_irqrestore(&ichan->lock, flags);
940 tx->cookie = cookie;
941 ichan->dma_chan.cookie = cookie;
942 }
943
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700944out:
945 mutex_unlock(&ichan->chan_mutex);
946
947 return cookie;
948}
949
950/* Called with ichan->chan_mutex held */
951static int idmac_desc_alloc(struct idmac_channel *ichan, int n)
952{
953 struct idmac_tx_desc *desc = vmalloc(n * sizeof(struct idmac_tx_desc));
954 struct idmac *idmac = to_idmac(ichan->dma_chan.device);
955
956 if (!desc)
957 return -ENOMEM;
958
959 /* No interrupts, just disable the tasklet for a moment */
960 tasklet_disable(&to_ipu(idmac)->tasklet);
961
962 ichan->n_tx_desc = n;
963 ichan->desc = desc;
964 INIT_LIST_HEAD(&ichan->queue);
965 INIT_LIST_HEAD(&ichan->free_list);
966
967 while (n--) {
968 struct dma_async_tx_descriptor *txd = &desc->txd;
969
970 memset(txd, 0, sizeof(*txd));
971 dma_async_tx_descriptor_init(txd, &ichan->dma_chan);
972 txd->tx_submit = idmac_tx_submit;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -0700973
974 list_add(&desc->list, &ichan->free_list);
975
976 desc++;
977 }
978
979 tasklet_enable(&to_ipu(idmac)->tasklet);
980
981 return 0;
982}
983
984/**
985 * ipu_init_channel() - initialize an IPU channel.
986 * @idmac: IPU DMAC context.
987 * @ichan: pointer to the channel object.
988 * @return 0 on success or negative error code on failure.
989 */
990static int ipu_init_channel(struct idmac *idmac, struct idmac_channel *ichan)
991{
992 union ipu_channel_param *params = &ichan->params;
993 uint32_t ipu_conf;
994 enum ipu_channel channel = ichan->dma_chan.chan_id;
995 unsigned long flags;
996 uint32_t reg;
997 struct ipu *ipu = to_ipu(idmac);
998 int ret = 0, n_desc = 0;
999
1000 dev_dbg(ipu->dev, "init channel = %d\n", channel);
1001
1002 if (channel != IDMAC_SDC_0 && channel != IDMAC_SDC_1 &&
1003 channel != IDMAC_IC_7)
1004 return -EINVAL;
1005
1006 spin_lock_irqsave(&ipu->lock, flags);
1007
1008 switch (channel) {
1009 case IDMAC_IC_7:
1010 n_desc = 16;
1011 reg = idmac_read_icreg(ipu, IC_CONF);
1012 idmac_write_icreg(ipu, reg & ~IC_CONF_CSI_MEM_WR_EN, IC_CONF);
1013 break;
1014 case IDMAC_IC_0:
1015 n_desc = 16;
1016 reg = idmac_read_ipureg(ipu, IPU_FS_PROC_FLOW);
1017 idmac_write_ipureg(ipu, reg & ~FS_ENC_IN_VALID, IPU_FS_PROC_FLOW);
1018 ret = ipu_ic_init_prpenc(ipu, params, true);
1019 break;
1020 case IDMAC_SDC_0:
1021 case IDMAC_SDC_1:
1022 n_desc = 4;
1023 default:
1024 break;
1025 }
1026
1027 ipu->channel_init_mask |= 1L << channel;
1028
1029 /* Enable IPU sub module */
1030 ipu_conf = idmac_read_ipureg(ipu, IPU_CONF) |
1031 ipu_channel_conf_mask(channel);
1032 idmac_write_ipureg(ipu, ipu_conf, IPU_CONF);
1033
1034 spin_unlock_irqrestore(&ipu->lock, flags);
1035
1036 if (n_desc && !ichan->desc)
1037 ret = idmac_desc_alloc(ichan, n_desc);
1038
1039 dump_idmac_reg(ipu);
1040
1041 return ret;
1042}
1043
1044/**
1045 * ipu_uninit_channel() - uninitialize an IPU channel.
1046 * @idmac: IPU DMAC context.
1047 * @ichan: pointer to the channel object.
1048 */
1049static void ipu_uninit_channel(struct idmac *idmac, struct idmac_channel *ichan)
1050{
1051 enum ipu_channel channel = ichan->dma_chan.chan_id;
1052 unsigned long flags;
1053 uint32_t reg;
1054 unsigned long chan_mask = 1UL << channel;
1055 uint32_t ipu_conf;
1056 struct ipu *ipu = to_ipu(idmac);
1057
1058 spin_lock_irqsave(&ipu->lock, flags);
1059
1060 if (!(ipu->channel_init_mask & chan_mask)) {
1061 dev_err(ipu->dev, "Channel already uninitialized %d\n",
1062 channel);
1063 spin_unlock_irqrestore(&ipu->lock, flags);
1064 return;
1065 }
1066
1067 /* Reset the double buffer */
1068 reg = idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL);
1069 idmac_write_ipureg(ipu, reg & ~chan_mask, IPU_CHA_DB_MODE_SEL);
1070
1071 ichan->sec_chan_en = false;
1072
1073 switch (channel) {
1074 case IDMAC_IC_7:
1075 reg = idmac_read_icreg(ipu, IC_CONF);
1076 idmac_write_icreg(ipu, reg & ~(IC_CONF_RWS_EN | IC_CONF_PRPENC_EN),
1077 IC_CONF);
1078 break;
1079 case IDMAC_IC_0:
1080 reg = idmac_read_icreg(ipu, IC_CONF);
1081 idmac_write_icreg(ipu, reg & ~(IC_CONF_PRPENC_EN | IC_CONF_PRPENC_CSC1),
1082 IC_CONF);
1083 break;
1084 case IDMAC_SDC_0:
1085 case IDMAC_SDC_1:
1086 default:
1087 break;
1088 }
1089
1090 ipu->channel_init_mask &= ~(1L << channel);
1091
1092 ipu_conf = idmac_read_ipureg(ipu, IPU_CONF) &
1093 ~ipu_channel_conf_mask(channel);
1094 idmac_write_ipureg(ipu, ipu_conf, IPU_CONF);
1095
1096 spin_unlock_irqrestore(&ipu->lock, flags);
1097
1098 ichan->n_tx_desc = 0;
1099 vfree(ichan->desc);
1100 ichan->desc = NULL;
1101}
1102
1103/**
1104 * ipu_disable_channel() - disable an IPU channel.
1105 * @idmac: IPU DMAC context.
1106 * @ichan: channel object pointer.
1107 * @wait_for_stop: flag to set whether to wait for channel end of frame or
1108 * return immediately.
1109 * @return: 0 on success or negative error code on failure.
1110 */
1111static int ipu_disable_channel(struct idmac *idmac, struct idmac_channel *ichan,
1112 bool wait_for_stop)
1113{
1114 enum ipu_channel channel = ichan->dma_chan.chan_id;
1115 struct ipu *ipu = to_ipu(idmac);
1116 uint32_t reg;
1117 unsigned long flags;
1118 unsigned long chan_mask = 1UL << channel;
1119 unsigned int timeout;
1120
1121 if (wait_for_stop && channel != IDMAC_SDC_1 && channel != IDMAC_SDC_0) {
1122 timeout = 40;
1123 /* This waiting always fails. Related to spurious irq problem */
1124 while ((idmac_read_icreg(ipu, IDMAC_CHA_BUSY) & chan_mask) ||
1125 (ipu_channel_status(ipu, channel) == TASK_STAT_ACTIVE)) {
1126 timeout--;
1127 msleep(10);
1128
1129 if (!timeout) {
1130 dev_dbg(ipu->dev,
1131 "Warning: timeout waiting for channel %u to "
1132 "stop: buf0_rdy = 0x%08X, buf1_rdy = 0x%08X, "
1133 "busy = 0x%08X, tstat = 0x%08X\n", channel,
1134 idmac_read_ipureg(ipu, IPU_CHA_BUF0_RDY),
1135 idmac_read_ipureg(ipu, IPU_CHA_BUF1_RDY),
1136 idmac_read_icreg(ipu, IDMAC_CHA_BUSY),
1137 idmac_read_ipureg(ipu, IPU_TASKS_STAT));
1138 break;
1139 }
1140 }
1141 dev_dbg(ipu->dev, "timeout = %d * 10ms\n", 40 - timeout);
1142 }
1143 /* SDC BG and FG must be disabled before DMA is disabled */
1144 if (wait_for_stop && (channel == IDMAC_SDC_0 ||
1145 channel == IDMAC_SDC_1)) {
1146 for (timeout = 5;
1147 timeout && !ipu_irq_status(ichan->eof_irq); timeout--)
1148 msleep(5);
1149 }
1150
1151 spin_lock_irqsave(&ipu->lock, flags);
1152
1153 /* Disable IC task */
1154 ipu_ic_disable_task(ipu, channel);
1155
1156 /* Disable DMA channel(s) */
1157 reg = idmac_read_icreg(ipu, IDMAC_CHA_EN);
1158 idmac_write_icreg(ipu, reg & ~chan_mask, IDMAC_CHA_EN);
1159
1160 /*
1161 * Problem (observed with channel DMAIC_7): after enabling the channel
1162 * and initialising buffers, there comes an interrupt with current still
1163 * pointing at buffer 0, whereas it should use buffer 0 first and only
1164 * generate an interrupt when it is done, then current should already
1165 * point to buffer 1. This spurious interrupt also comes on channel
1166 * DMASDC_0. With DMAIC_7 normally, is we just leave the ISR after the
1167 * first interrupt, there comes the second with current correctly
1168 * pointing to buffer 1 this time. But sometimes this second interrupt
1169 * doesn't come and the channel hangs. Clearing BUFx_RDY when disabling
1170 * the channel seems to prevent the channel from hanging, but it doesn't
1171 * prevent the spurious interrupt. This might also be unsafe. Think
1172 * about the IDMAC controller trying to switch to a buffer, when we
1173 * clear the ready bit, and re-enable it a moment later.
1174 */
1175 reg = idmac_read_ipureg(ipu, IPU_CHA_BUF0_RDY);
1176 idmac_write_ipureg(ipu, 0, IPU_CHA_BUF0_RDY);
1177 idmac_write_ipureg(ipu, reg & ~(1UL << channel), IPU_CHA_BUF0_RDY);
1178
1179 reg = idmac_read_ipureg(ipu, IPU_CHA_BUF1_RDY);
1180 idmac_write_ipureg(ipu, 0, IPU_CHA_BUF1_RDY);
1181 idmac_write_ipureg(ipu, reg & ~(1UL << channel), IPU_CHA_BUF1_RDY);
1182
1183 spin_unlock_irqrestore(&ipu->lock, flags);
1184
1185 return 0;
1186}
1187
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001188static struct scatterlist *idmac_sg_next(struct idmac_channel *ichan,
1189 struct idmac_tx_desc **desc, struct scatterlist *sg)
1190{
1191 struct scatterlist *sgnew = sg ? sg_next(sg) : NULL;
1192
1193 if (sgnew)
1194 /* next sg-element in this list */
1195 return sgnew;
1196
1197 if ((*desc)->list.next == &ichan->queue)
1198 /* No more descriptors on the queue */
1199 return NULL;
1200
1201 /* Fetch next descriptor */
1202 *desc = list_entry((*desc)->list.next, struct idmac_tx_desc, list);
1203 return (*desc)->sg;
1204}
1205
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001206/*
1207 * We have several possibilities here:
1208 * current BUF next BUF
1209 *
1210 * not last sg next not last sg
1211 * not last sg next last sg
1212 * last sg first sg from next descriptor
1213 * last sg NULL
1214 *
1215 * Besides, the descriptor queue might be empty or not. We process all these
1216 * cases carefully.
1217 */
1218static irqreturn_t idmac_interrupt(int irq, void *dev_id)
1219{
1220 struct idmac_channel *ichan = dev_id;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001221 struct device *dev = &ichan->dma_chan.dev->device;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001222 unsigned int chan_id = ichan->dma_chan.chan_id;
1223 struct scatterlist **sg, *sgnext, *sgnew = NULL;
1224 /* Next transfer descriptor */
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001225 struct idmac_tx_desc *desc, *descnew;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001226 dma_async_tx_callback callback;
1227 void *callback_param;
1228 bool done = false;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001229 u32 ready0, ready1, curbuf, err;
1230 unsigned long flags;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001231
1232 /* IDMAC has cleared the respective BUFx_RDY bit, we manage the buffer */
1233
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001234 dev_dbg(dev, "IDMAC irq %d, buf %d\n", irq, ichan->active_buffer);
1235
1236 spin_lock_irqsave(&ipu_data.lock, flags);
1237
1238 ready0 = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF0_RDY);
1239 ready1 = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF1_RDY);
1240 curbuf = idmac_read_ipureg(&ipu_data, IPU_CHA_CUR_BUF);
1241 err = idmac_read_ipureg(&ipu_data, IPU_INT_STAT_4);
1242
1243 if (err & (1 << chan_id)) {
1244 idmac_write_ipureg(&ipu_data, 1 << chan_id, IPU_INT_STAT_4);
1245 spin_unlock_irqrestore(&ipu_data.lock, flags);
1246 /*
1247 * Doing this
1248 * ichan->sg[0] = ichan->sg[1] = NULL;
1249 * you can force channel re-enable on the next tx_submit(), but
1250 * this is dirty - think about descriptors with multiple
1251 * sg elements.
1252 */
1253 dev_warn(dev, "NFB4EOF on channel %d, ready %x, %x, cur %x\n",
1254 chan_id, ready0, ready1, curbuf);
1255 return IRQ_HANDLED;
1256 }
1257 spin_unlock_irqrestore(&ipu_data.lock, flags);
1258
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001259 /* Other interrupts do not interfere with this channel */
1260 spin_lock(&ichan->lock);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001261 if (unlikely(chan_id != IDMAC_SDC_0 && chan_id != IDMAC_SDC_1 &&
Guennadi Liakhovetskiad567ff2009-05-12 09:16:29 +02001262 ((curbuf >> chan_id) & 1) == ichan->active_buffer &&
1263 !list_is_last(ichan->queue.next, &ichan->queue))) {
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001264 int i = 100;
1265
1266 /* This doesn't help. See comment in ipu_disable_channel() */
1267 while (--i) {
1268 curbuf = idmac_read_ipureg(&ipu_data, IPU_CHA_CUR_BUF);
1269 if (((curbuf >> chan_id) & 1) != ichan->active_buffer)
1270 break;
1271 cpu_relax();
1272 }
1273
1274 if (!i) {
1275 spin_unlock(&ichan->lock);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001276 dev_dbg(dev,
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001277 "IRQ on active buffer on channel %x, active "
1278 "%d, ready %x, %x, current %x!\n", chan_id,
1279 ichan->active_buffer, ready0, ready1, curbuf);
1280 return IRQ_NONE;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001281 } else
1282 dev_dbg(dev,
1283 "Buffer deactivated on channel %x, active "
1284 "%d, ready %x, %x, current %x, rest %d!\n", chan_id,
1285 ichan->active_buffer, ready0, ready1, curbuf, i);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001286 }
1287
1288 if (unlikely((ichan->active_buffer && (ready1 >> chan_id) & 1) ||
1289 (!ichan->active_buffer && (ready0 >> chan_id) & 1)
1290 )) {
1291 spin_unlock(&ichan->lock);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001292 dev_dbg(dev,
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001293 "IRQ with active buffer still ready on channel %x, "
1294 "active %d, ready %x, %x!\n", chan_id,
1295 ichan->active_buffer, ready0, ready1);
1296 return IRQ_NONE;
1297 }
1298
1299 if (unlikely(list_empty(&ichan->queue))) {
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001300 ichan->sg[ichan->active_buffer] = NULL;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001301 spin_unlock(&ichan->lock);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001302 dev_err(dev,
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001303 "IRQ without queued buffers on channel %x, active %d, "
1304 "ready %x, %x!\n", chan_id,
1305 ichan->active_buffer, ready0, ready1);
1306 return IRQ_NONE;
1307 }
1308
1309 /*
1310 * active_buffer is a software flag, it shows which buffer we are
1311 * currently expecting back from the hardware, IDMAC should be
1312 * processing the other buffer already
1313 */
1314 sg = &ichan->sg[ichan->active_buffer];
1315 sgnext = ichan->sg[!ichan->active_buffer];
1316
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001317 if (!*sg) {
1318 spin_unlock(&ichan->lock);
1319 return IRQ_HANDLED;
1320 }
1321
1322 desc = list_entry(ichan->queue.next, struct idmac_tx_desc, list);
1323 descnew = desc;
1324
1325 dev_dbg(dev, "IDMAC irq %d, dma 0x%08x, next dma 0x%08x, current %d, curbuf 0x%08x\n",
1326 irq, sg_dma_address(*sg), sgnext ? sg_dma_address(sgnext) : 0, ichan->active_buffer, curbuf);
1327
1328 /* Find the descriptor of sgnext */
1329 sgnew = idmac_sg_next(ichan, &descnew, *sg);
1330 if (sgnext != sgnew)
1331 dev_err(dev, "Submitted buffer %p, next buffer %p\n", sgnext, sgnew);
1332
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001333 /*
1334 * if sgnext == NULL sg must be the last element in a scatterlist and
1335 * queue must be empty
1336 */
1337 if (unlikely(!sgnext)) {
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001338 if (!WARN_ON(sg_next(*sg)))
1339 dev_dbg(dev, "Underrun on channel %x\n", chan_id);
1340 ichan->sg[!ichan->active_buffer] = sgnew;
1341
1342 if (unlikely(sgnew)) {
1343 ipu_submit_buffer(ichan, descnew, sgnew, !ichan->active_buffer);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001344 } else {
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001345 spin_lock_irqsave(&ipu_data.lock, flags);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001346 ipu_ic_disable_task(&ipu_data, chan_id);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001347 spin_unlock_irqrestore(&ipu_data.lock, flags);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001348 ichan->status = IPU_CHANNEL_READY;
1349 /* Continue to check for complete descriptor */
1350 }
1351 }
1352
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001353 /* Calculate and submit the next sg element */
1354 sgnew = idmac_sg_next(ichan, &descnew, sgnew);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001355
1356 if (unlikely(!sg_next(*sg)) || !sgnext) {
1357 /*
1358 * Last element in scatterlist done, remove from the queue,
1359 * _init for debugging
1360 */
1361 list_del_init(&desc->list);
1362 done = true;
1363 }
1364
1365 *sg = sgnew;
1366
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001367 if (likely(sgnew) &&
1368 ipu_submit_buffer(ichan, descnew, sgnew, ichan->active_buffer) < 0) {
1369 callback = desc->txd.callback;
1370 callback_param = desc->txd.callback_param;
1371 spin_unlock(&ichan->lock);
1372 callback(callback_param);
1373 spin_lock(&ichan->lock);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001374 }
1375
1376 /* Flip the active buffer - even if update above failed */
1377 ichan->active_buffer = !ichan->active_buffer;
1378 if (done)
1379 ichan->completed = desc->txd.cookie;
1380
1381 callback = desc->txd.callback;
1382 callback_param = desc->txd.callback_param;
1383
1384 spin_unlock(&ichan->lock);
1385
1386 if (done && (desc->txd.flags & DMA_PREP_INTERRUPT) && callback)
1387 callback(callback_param);
1388
1389 return IRQ_HANDLED;
1390}
1391
1392static void ipu_gc_tasklet(unsigned long arg)
1393{
1394 struct ipu *ipu = (struct ipu *)arg;
1395 int i;
1396
1397 for (i = 0; i < IPU_CHANNELS_NUM; i++) {
1398 struct idmac_channel *ichan = ipu->channel + i;
1399 struct idmac_tx_desc *desc;
1400 unsigned long flags;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001401 struct scatterlist *sg;
1402 int j, k;
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001403
1404 for (j = 0; j < ichan->n_tx_desc; j++) {
1405 desc = ichan->desc + j;
1406 spin_lock_irqsave(&ichan->lock, flags);
1407 if (async_tx_test_ack(&desc->txd)) {
1408 list_move(&desc->list, &ichan->free_list);
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001409 for_each_sg(desc->sg, sg, desc->sg_len, k) {
1410 if (ichan->sg[0] == sg)
1411 ichan->sg[0] = NULL;
1412 else if (ichan->sg[1] == sg)
1413 ichan->sg[1] = NULL;
1414 }
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001415 async_tx_clear_ack(&desc->txd);
1416 }
1417 spin_unlock_irqrestore(&ichan->lock, flags);
1418 }
1419 }
1420}
1421
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -07001422/* Allocate and initialise a transfer descriptor. */
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001423static struct dma_async_tx_descriptor *idmac_prep_slave_sg(struct dma_chan *chan,
1424 struct scatterlist *sgl, unsigned int sg_len,
1425 enum dma_data_direction direction, unsigned long tx_flags)
1426{
1427 struct idmac_channel *ichan = to_idmac_chan(chan);
1428 struct idmac_tx_desc *desc = NULL;
1429 struct dma_async_tx_descriptor *txd = NULL;
1430 unsigned long flags;
1431
1432 /* We only can handle these three channels so far */
Guennadi Liakhovetski8c6db1bb2009-04-02 11:36:58 +02001433 if (chan->chan_id != IDMAC_SDC_0 && chan->chan_id != IDMAC_SDC_1 &&
1434 chan->chan_id != IDMAC_IC_7)
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001435 return NULL;
1436
1437 if (direction != DMA_FROM_DEVICE && direction != DMA_TO_DEVICE) {
1438 dev_err(chan->device->dev, "Invalid DMA direction %d!\n", direction);
1439 return NULL;
1440 }
1441
1442 mutex_lock(&ichan->chan_mutex);
1443
1444 spin_lock_irqsave(&ichan->lock, flags);
1445 if (!list_empty(&ichan->free_list)) {
1446 desc = list_entry(ichan->free_list.next,
1447 struct idmac_tx_desc, list);
1448
1449 list_del_init(&desc->list);
1450
1451 desc->sg_len = sg_len;
1452 desc->sg = sgl;
1453 txd = &desc->txd;
1454 txd->flags = tx_flags;
1455 }
1456 spin_unlock_irqrestore(&ichan->lock, flags);
1457
1458 mutex_unlock(&ichan->chan_mutex);
1459
1460 tasklet_schedule(&to_ipu(to_idmac(chan->device))->tasklet);
1461
1462 return txd;
1463}
1464
1465/* Re-select the current buffer and re-activate the channel */
1466static void idmac_issue_pending(struct dma_chan *chan)
1467{
1468 struct idmac_channel *ichan = to_idmac_chan(chan);
1469 struct idmac *idmac = to_idmac(chan->device);
1470 struct ipu *ipu = to_ipu(idmac);
1471 unsigned long flags;
1472
1473 /* This is not always needed, but doesn't hurt either */
1474 spin_lock_irqsave(&ipu->lock, flags);
Guennadi Liakhovetski8c6db1bb2009-04-02 11:36:58 +02001475 ipu_select_buffer(chan->chan_id, ichan->active_buffer);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001476 spin_unlock_irqrestore(&ipu->lock, flags);
1477
1478 /*
1479 * Might need to perform some parts of initialisation from
1480 * ipu_enable_channel(), but not all, we do not want to reset to buffer
1481 * 0, don't need to set priority again either, but re-enabling the task
1482 * and the channel might be a good idea.
1483 */
1484}
1485
1486static void __idmac_terminate_all(struct dma_chan *chan)
1487{
1488 struct idmac_channel *ichan = to_idmac_chan(chan);
1489 struct idmac *idmac = to_idmac(chan->device);
1490 unsigned long flags;
1491 int i;
1492
1493 ipu_disable_channel(idmac, ichan,
1494 ichan->status >= IPU_CHANNEL_ENABLED);
1495
1496 tasklet_disable(&to_ipu(idmac)->tasklet);
1497
1498 /* ichan->queue is modified in ISR, have to spinlock */
1499 spin_lock_irqsave(&ichan->lock, flags);
1500 list_splice_init(&ichan->queue, &ichan->free_list);
1501
1502 if (ichan->desc)
1503 for (i = 0; i < ichan->n_tx_desc; i++) {
1504 struct idmac_tx_desc *desc = ichan->desc + i;
1505 if (list_empty(&desc->list))
1506 /* Descriptor was prepared, but not submitted */
Guennadi Liakhovetski0149f7d2009-03-25 09:13:23 -07001507 list_add(&desc->list, &ichan->free_list);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001508
1509 async_tx_clear_ack(&desc->txd);
1510 }
1511
1512 ichan->sg[0] = NULL;
1513 ichan->sg[1] = NULL;
1514 spin_unlock_irqrestore(&ichan->lock, flags);
1515
1516 tasklet_enable(&to_ipu(idmac)->tasklet);
1517
1518 ichan->status = IPU_CHANNEL_INITIALIZED;
1519}
1520
1521static void idmac_terminate_all(struct dma_chan *chan)
1522{
1523 struct idmac_channel *ichan = to_idmac_chan(chan);
1524
1525 mutex_lock(&ichan->chan_mutex);
1526
1527 __idmac_terminate_all(chan);
1528
1529 mutex_unlock(&ichan->chan_mutex);
1530}
1531
Guennadi Liakhovetski8c6db1bb2009-04-02 11:36:58 +02001532#ifdef DEBUG
1533static irqreturn_t ic_sof_irq(int irq, void *dev_id)
1534{
1535 struct idmac_channel *ichan = dev_id;
1536 printk(KERN_DEBUG "Got SOF IRQ %d on Channel %d\n",
1537 irq, ichan->dma_chan.chan_id);
Ben Nizetteca50a512009-04-16 05:54:12 +10001538 disable_irq_nosync(irq);
Guennadi Liakhovetski8c6db1bb2009-04-02 11:36:58 +02001539 return IRQ_HANDLED;
1540}
1541
1542static irqreturn_t ic_eof_irq(int irq, void *dev_id)
1543{
1544 struct idmac_channel *ichan = dev_id;
1545 printk(KERN_DEBUG "Got EOF IRQ %d on Channel %d\n",
1546 irq, ichan->dma_chan.chan_id);
Ben Nizetteca50a512009-04-16 05:54:12 +10001547 disable_irq_nosync(irq);
Guennadi Liakhovetski8c6db1bb2009-04-02 11:36:58 +02001548 return IRQ_HANDLED;
1549}
1550
1551static int ic_sof = -EINVAL, ic_eof = -EINVAL;
1552#endif
1553
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001554static int idmac_alloc_chan_resources(struct dma_chan *chan)
1555{
1556 struct idmac_channel *ichan = to_idmac_chan(chan);
1557 struct idmac *idmac = to_idmac(chan->device);
1558 int ret;
1559
1560 /* dmaengine.c now guarantees to only offer free channels */
1561 BUG_ON(chan->client_count > 1);
1562 WARN_ON(ichan->status != IPU_CHANNEL_FREE);
1563
1564 chan->cookie = 1;
1565 ichan->completed = -ENXIO;
1566
Guennadi Liakhovetski8c6db1bb2009-04-02 11:36:58 +02001567 ret = ipu_irq_map(chan->chan_id);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001568 if (ret < 0)
1569 goto eimap;
1570
1571 ichan->eof_irq = ret;
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001572
1573 /*
1574 * Important to first disable the channel, because maybe someone
1575 * used it before us, e.g., the bootloader
1576 */
1577 ipu_disable_channel(idmac, ichan, true);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001578
1579 ret = ipu_init_channel(idmac, ichan);
1580 if (ret < 0)
1581 goto eichan;
1582
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001583 ret = request_irq(ichan->eof_irq, idmac_interrupt, 0,
1584 ichan->eof_name, ichan);
1585 if (ret < 0)
1586 goto erirq;
1587
Guennadi Liakhovetski8c6db1bb2009-04-02 11:36:58 +02001588#ifdef DEBUG
1589 if (chan->chan_id == IDMAC_IC_7) {
1590 ic_sof = ipu_irq_map(69);
1591 if (ic_sof > 0)
1592 request_irq(ic_sof, ic_sof_irq, 0, "IC SOF", ichan);
1593 ic_eof = ipu_irq_map(70);
1594 if (ic_eof > 0)
1595 request_irq(ic_eof, ic_eof_irq, 0, "IC EOF", ichan);
1596 }
1597#endif
1598
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001599 ichan->status = IPU_CHANNEL_INITIALIZED;
1600
Guennadi Liakhovetski8c6db1bb2009-04-02 11:36:58 +02001601 dev_dbg(&chan->dev->device, "Found channel 0x%x, irq %d\n",
1602 chan->chan_id, ichan->eof_irq);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001603
1604 return ret;
1605
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001606erirq:
Guennadi Liakhovetski8d47bae2009-03-25 09:13:24 -07001607 ipu_uninit_channel(idmac, ichan);
1608eichan:
Guennadi Liakhovetski8c6db1bb2009-04-02 11:36:58 +02001609 ipu_irq_unmap(chan->chan_id);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001610eimap:
1611 return ret;
1612}
1613
1614static void idmac_free_chan_resources(struct dma_chan *chan)
1615{
1616 struct idmac_channel *ichan = to_idmac_chan(chan);
1617 struct idmac *idmac = to_idmac(chan->device);
1618
1619 mutex_lock(&ichan->chan_mutex);
1620
1621 __idmac_terminate_all(chan);
1622
1623 if (ichan->status > IPU_CHANNEL_FREE) {
Guennadi Liakhovetski8c6db1bb2009-04-02 11:36:58 +02001624#ifdef DEBUG
1625 if (chan->chan_id == IDMAC_IC_7) {
1626 if (ic_sof > 0) {
1627 free_irq(ic_sof, ichan);
1628 ipu_irq_unmap(69);
1629 ic_sof = -EINVAL;
1630 }
1631 if (ic_eof > 0) {
1632 free_irq(ic_eof, ichan);
1633 ipu_irq_unmap(70);
1634 ic_eof = -EINVAL;
1635 }
1636 }
1637#endif
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001638 free_irq(ichan->eof_irq, ichan);
Guennadi Liakhovetski8c6db1bb2009-04-02 11:36:58 +02001639 ipu_irq_unmap(chan->chan_id);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001640 }
1641
1642 ichan->status = IPU_CHANNEL_FREE;
1643
1644 ipu_uninit_channel(idmac, ichan);
1645
1646 mutex_unlock(&ichan->chan_mutex);
1647
1648 tasklet_schedule(&to_ipu(idmac)->tasklet);
1649}
1650
1651static enum dma_status idmac_is_tx_complete(struct dma_chan *chan,
1652 dma_cookie_t cookie, dma_cookie_t *done, dma_cookie_t *used)
1653{
1654 struct idmac_channel *ichan = to_idmac_chan(chan);
1655
1656 if (done)
1657 *done = ichan->completed;
1658 if (used)
1659 *used = chan->cookie;
1660 if (cookie != chan->cookie)
1661 return DMA_ERROR;
1662 return DMA_SUCCESS;
1663}
1664
1665static int __init ipu_idmac_init(struct ipu *ipu)
1666{
1667 struct idmac *idmac = &ipu->idmac;
1668 struct dma_device *dma = &idmac->dma;
1669 int i;
1670
1671 dma_cap_set(DMA_SLAVE, dma->cap_mask);
1672 dma_cap_set(DMA_PRIVATE, dma->cap_mask);
1673
1674 /* Compulsory common fields */
1675 dma->dev = ipu->dev;
1676 dma->device_alloc_chan_resources = idmac_alloc_chan_resources;
1677 dma->device_free_chan_resources = idmac_free_chan_resources;
1678 dma->device_is_tx_complete = idmac_is_tx_complete;
1679 dma->device_issue_pending = idmac_issue_pending;
1680
1681 /* Compulsory for DMA_SLAVE fields */
1682 dma->device_prep_slave_sg = idmac_prep_slave_sg;
1683 dma->device_terminate_all = idmac_terminate_all;
1684
1685 INIT_LIST_HEAD(&dma->channels);
1686 for (i = 0; i < IPU_CHANNELS_NUM; i++) {
1687 struct idmac_channel *ichan = ipu->channel + i;
1688 struct dma_chan *dma_chan = &ichan->dma_chan;
1689
1690 spin_lock_init(&ichan->lock);
1691 mutex_init(&ichan->chan_mutex);
1692
1693 ichan->status = IPU_CHANNEL_FREE;
1694 ichan->sec_chan_en = false;
1695 ichan->completed = -ENXIO;
1696 snprintf(ichan->eof_name, sizeof(ichan->eof_name), "IDMAC EOF %d", i);
1697
1698 dma_chan->device = &idmac->dma;
1699 dma_chan->cookie = 1;
1700 dma_chan->chan_id = i;
Guennadi Liakhovetski8c6db1bb2009-04-02 11:36:58 +02001701 list_add_tail(&dma_chan->device_node, &dma->channels);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001702 }
1703
1704 idmac_write_icreg(ipu, 0x00000070, IDMAC_CONF);
1705
1706 return dma_async_device_register(&idmac->dma);
1707}
1708
Guennadi Liakhovetski234f2df2009-03-25 09:13:24 -07001709static void __exit ipu_idmac_exit(struct ipu *ipu)
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001710{
1711 int i;
1712 struct idmac *idmac = &ipu->idmac;
1713
1714 for (i = 0; i < IPU_CHANNELS_NUM; i++) {
1715 struct idmac_channel *ichan = ipu->channel + i;
1716
1717 idmac_terminate_all(&ichan->dma_chan);
1718 idmac_prep_slave_sg(&ichan->dma_chan, NULL, 0, DMA_NONE, 0);
1719 }
1720
1721 dma_async_device_unregister(&idmac->dma);
1722}
1723
1724/*****************************************************************************
1725 * IPU common probe / remove
1726 */
1727
Guennadi Liakhovetski234f2df2009-03-25 09:13:24 -07001728static int __init ipu_probe(struct platform_device *pdev)
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001729{
1730 struct ipu_platform_data *pdata = pdev->dev.platform_data;
1731 struct resource *mem_ipu, *mem_ic;
1732 int ret;
1733
1734 spin_lock_init(&ipu_data.lock);
1735
1736 mem_ipu = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1737 mem_ic = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1738 if (!pdata || !mem_ipu || !mem_ic)
1739 return -EINVAL;
1740
1741 ipu_data.dev = &pdev->dev;
1742
1743 platform_set_drvdata(pdev, &ipu_data);
1744
1745 ret = platform_get_irq(pdev, 0);
1746 if (ret < 0)
1747 goto err_noirq;
1748
1749 ipu_data.irq_fn = ret;
1750 ret = platform_get_irq(pdev, 1);
1751 if (ret < 0)
1752 goto err_noirq;
1753
1754 ipu_data.irq_err = ret;
1755 ipu_data.irq_base = pdata->irq_base;
1756
1757 dev_dbg(&pdev->dev, "fn irq %u, err irq %u, irq-base %u\n",
1758 ipu_data.irq_fn, ipu_data.irq_err, ipu_data.irq_base);
1759
1760 /* Remap IPU common registers */
1761 ipu_data.reg_ipu = ioremap(mem_ipu->start,
1762 mem_ipu->end - mem_ipu->start + 1);
1763 if (!ipu_data.reg_ipu) {
1764 ret = -ENOMEM;
1765 goto err_ioremap_ipu;
1766 }
1767
1768 /* Remap Image Converter and Image DMA Controller registers */
1769 ipu_data.reg_ic = ioremap(mem_ic->start,
1770 mem_ic->end - mem_ic->start + 1);
1771 if (!ipu_data.reg_ic) {
1772 ret = -ENOMEM;
1773 goto err_ioremap_ic;
1774 }
1775
1776 /* Get IPU clock */
Sascha Hauer9eb2eb82009-02-18 11:55:33 +01001777 ipu_data.ipu_clk = clk_get(&pdev->dev, NULL);
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001778 if (IS_ERR(ipu_data.ipu_clk)) {
1779 ret = PTR_ERR(ipu_data.ipu_clk);
1780 goto err_clk_get;
1781 }
1782
1783 /* Make sure IPU HSP clock is running */
1784 clk_enable(ipu_data.ipu_clk);
1785
1786 /* Disable all interrupts */
1787 idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_1);
1788 idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_2);
1789 idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_3);
1790 idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_4);
1791 idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_5);
1792
1793 dev_dbg(&pdev->dev, "%s @ 0x%08lx, fn irq %u, err irq %u\n", pdev->name,
1794 (unsigned long)mem_ipu->start, ipu_data.irq_fn, ipu_data.irq_err);
1795
1796 ret = ipu_irq_attach_irq(&ipu_data, pdev);
1797 if (ret < 0)
1798 goto err_attach_irq;
1799
1800 /* Initialize DMA engine */
1801 ret = ipu_idmac_init(&ipu_data);
1802 if (ret < 0)
1803 goto err_idmac_init;
1804
1805 tasklet_init(&ipu_data.tasklet, ipu_gc_tasklet, (unsigned long)&ipu_data);
1806
1807 ipu_data.dev = &pdev->dev;
1808
1809 dev_dbg(ipu_data.dev, "IPU initialized\n");
1810
1811 return 0;
1812
1813err_idmac_init:
1814err_attach_irq:
1815 ipu_irq_detach_irq(&ipu_data, pdev);
1816 clk_disable(ipu_data.ipu_clk);
1817 clk_put(ipu_data.ipu_clk);
1818err_clk_get:
1819 iounmap(ipu_data.reg_ic);
1820err_ioremap_ic:
1821 iounmap(ipu_data.reg_ipu);
1822err_ioremap_ipu:
1823err_noirq:
1824 dev_err(&pdev->dev, "Failed to probe IPU: %d\n", ret);
1825 return ret;
1826}
1827
Guennadi Liakhovetski234f2df2009-03-25 09:13:24 -07001828static int __exit ipu_remove(struct platform_device *pdev)
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001829{
1830 struct ipu *ipu = platform_get_drvdata(pdev);
1831
1832 ipu_idmac_exit(ipu);
1833 ipu_irq_detach_irq(ipu, pdev);
1834 clk_disable(ipu->ipu_clk);
1835 clk_put(ipu->ipu_clk);
1836 iounmap(ipu->reg_ic);
1837 iounmap(ipu->reg_ipu);
1838 tasklet_kill(&ipu->tasklet);
1839 platform_set_drvdata(pdev, NULL);
1840
1841 return 0;
1842}
1843
1844/*
1845 * We need two MEM resources - with IPU-common and Image Converter registers,
1846 * including PF_CONF and IDMAC_* registers, and two IRQs - function and error
1847 */
1848static struct platform_driver ipu_platform_driver = {
1849 .driver = {
1850 .name = "ipu-core",
1851 .owner = THIS_MODULE,
1852 },
Guennadi Liakhovetski234f2df2009-03-25 09:13:24 -07001853 .remove = __exit_p(ipu_remove),
Guennadi Liakhovetski5296b562009-01-19 15:36:21 -07001854};
1855
1856static int __init ipu_init(void)
1857{
1858 return platform_driver_probe(&ipu_platform_driver, ipu_probe);
1859}
1860subsys_initcall(ipu_init);
1861
1862MODULE_DESCRIPTION("IPU core driver");
1863MODULE_LICENSE("GPL v2");
1864MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
1865MODULE_ALIAS("platform:ipu-core");