3 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
5 * Copyright (C) 2005-2007 Freescale Semiconductor, Inc. All Rights Reserved.
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.
12 #include <linux/dma-mapping.h>
13 #include <linux/init.h>
14 #include <linux/platform_device.h>
15 #include <linux/err.h>
16 #include <linux/spinlock.h>
17 #include <linux/delay.h>
18 #include <linux/list.h>
19 #include <linux/clk.h>
20 #include <linux/vmalloc.h>
21 #include <linux/string.h>
22 #include <linux/interrupt.h>
24 #include <linux/module.h>
28 #include "../dmaengine.h"
29 #include "ipu_intern.h"
31 #define FS_VF_IN_VALID 0x00000002
32 #define FS_ENC_IN_VALID 0x00000001
34 static int ipu_disable_channel(struct idmac
*idmac
, struct idmac_channel
*ichan
,
38 * There can be only one, we could allocate it dynamically, but then we'd have
39 * to add an extra parameter to some functions, and use something as ugly as
40 * struct ipu *ipu = to_ipu(to_idmac(ichan->dma_chan.device));
43 static struct ipu ipu_data
;
45 #define to_ipu(id) container_of(id, struct ipu, idmac)
47 static u32
__idmac_read_icreg(struct ipu
*ipu
, unsigned long reg
)
49 return __raw_readl(ipu
->reg_ic
+ reg
);
52 #define idmac_read_icreg(ipu, reg) __idmac_read_icreg(ipu, reg - IC_CONF)
54 static void __idmac_write_icreg(struct ipu
*ipu
, u32 value
, unsigned long reg
)
56 __raw_writel(value
, ipu
->reg_ic
+ reg
);
59 #define idmac_write_icreg(ipu, v, reg) __idmac_write_icreg(ipu, v, reg - IC_CONF)
61 static u32
idmac_read_ipureg(struct ipu
*ipu
, unsigned long reg
)
63 return __raw_readl(ipu
->reg_ipu
+ reg
);
66 static void idmac_write_ipureg(struct ipu
*ipu
, u32 value
, unsigned long reg
)
68 __raw_writel(value
, ipu
->reg_ipu
+ reg
);
71 /*****************************************************************************
72 * IPU / IC common functions
74 static void dump_idmac_reg(struct ipu
*ipu
)
76 dev_dbg(ipu
->dev
, "IDMAC_CONF 0x%x, IC_CONF 0x%x, IDMAC_CHA_EN 0x%x, "
77 "IDMAC_CHA_PRI 0x%x, IDMAC_CHA_BUSY 0x%x\n",
78 idmac_read_icreg(ipu
, IDMAC_CONF
),
79 idmac_read_icreg(ipu
, IC_CONF
),
80 idmac_read_icreg(ipu
, IDMAC_CHA_EN
),
81 idmac_read_icreg(ipu
, IDMAC_CHA_PRI
),
82 idmac_read_icreg(ipu
, IDMAC_CHA_BUSY
));
83 dev_dbg(ipu
->dev
, "BUF0_RDY 0x%x, BUF1_RDY 0x%x, CUR_BUF 0x%x, "
84 "DB_MODE 0x%x, TASKS_STAT 0x%x\n",
85 idmac_read_ipureg(ipu
, IPU_CHA_BUF0_RDY
),
86 idmac_read_ipureg(ipu
, IPU_CHA_BUF1_RDY
),
87 idmac_read_ipureg(ipu
, IPU_CHA_CUR_BUF
),
88 idmac_read_ipureg(ipu
, IPU_CHA_DB_MODE_SEL
),
89 idmac_read_ipureg(ipu
, IPU_TASKS_STAT
));
92 static uint32_t bytes_per_pixel(enum pixel_fmt fmt
)
95 case IPU_PIX_FMT_GENERIC
: /* generic data */
96 case IPU_PIX_FMT_RGB332
:
97 case IPU_PIX_FMT_YUV420P
:
98 case IPU_PIX_FMT_YUV422P
:
101 case IPU_PIX_FMT_RGB565
:
102 case IPU_PIX_FMT_YUYV
:
103 case IPU_PIX_FMT_UYVY
:
105 case IPU_PIX_FMT_BGR24
:
106 case IPU_PIX_FMT_RGB24
:
108 case IPU_PIX_FMT_GENERIC_32
: /* generic data */
109 case IPU_PIX_FMT_BGR32
:
110 case IPU_PIX_FMT_RGB32
:
111 case IPU_PIX_FMT_ABGR32
:
116 /* Enable direct write to memory by the Camera Sensor Interface */
117 static void ipu_ic_enable_task(struct ipu
*ipu
, enum ipu_channel channel
)
119 uint32_t ic_conf
, mask
;
123 mask
= IC_CONF_PRPENC_EN
;
126 mask
= IC_CONF_RWS_EN
| IC_CONF_PRPENC_EN
;
131 ic_conf
= idmac_read_icreg(ipu
, IC_CONF
) | mask
;
132 idmac_write_icreg(ipu
, ic_conf
, IC_CONF
);
135 /* Called under spin_lock_irqsave(&ipu_data.lock) */
136 static void ipu_ic_disable_task(struct ipu
*ipu
, enum ipu_channel channel
)
138 uint32_t ic_conf
, mask
;
142 mask
= IC_CONF_PRPENC_EN
;
145 mask
= IC_CONF_RWS_EN
| IC_CONF_PRPENC_EN
;
150 ic_conf
= idmac_read_icreg(ipu
, IC_CONF
) & ~mask
;
151 idmac_write_icreg(ipu
, ic_conf
, IC_CONF
);
154 static uint32_t ipu_channel_status(struct ipu
*ipu
, enum ipu_channel channel
)
156 uint32_t stat
= TASK_STAT_IDLE
;
157 uint32_t task_stat_reg
= idmac_read_ipureg(ipu
, IPU_TASKS_STAT
);
161 stat
= (task_stat_reg
& TSTAT_CSI2MEM_MASK
) >>
162 TSTAT_CSI2MEM_OFFSET
;
173 struct chan_param_mem_planar
{
211 } __attribute__ ((packed
));
213 struct chan_param_mem_interleaved
{
270 } __attribute__ ((packed
));
272 union chan_param_mem
{
273 struct chan_param_mem_planar pp
;
274 struct chan_param_mem_interleaved ip
;
277 static void ipu_ch_param_set_plane_offset(union chan_param_mem
*params
,
278 u32 u_offset
, u32 v_offset
)
280 params
->pp
.ubo_l
= u_offset
& 0x7ff;
281 params
->pp
.ubo_h
= u_offset
>> 11;
282 params
->pp
.vbo_l
= v_offset
& 0x1ffff;
283 params
->pp
.vbo_h
= v_offset
>> 17;
286 static void ipu_ch_param_set_size(union chan_param_mem
*params
,
287 uint32_t pixel_fmt
, uint16_t width
,
288 uint16_t height
, uint16_t stride
)
293 params
->pp
.fw
= width
- 1;
294 params
->pp
.fh_l
= height
- 1;
295 params
->pp
.fh_h
= (height
- 1) >> 8;
296 params
->pp
.sl
= stride
- 1;
299 case IPU_PIX_FMT_GENERIC
:
300 /*Represents 8-bit Generic data */
304 params
->pp
.sat
= 2; /* SAT = use 32-bit access */
306 case IPU_PIX_FMT_GENERIC_32
:
307 /*Represents 32-bit Generic data */
311 params
->pp
.sat
= 2; /* SAT = use 32-bit access */
313 case IPU_PIX_FMT_RGB565
:
317 params
->ip
.sat
= 2; /* SAT = 32-bit access */
318 params
->ip
.ofs0
= 0; /* Red bit offset */
319 params
->ip
.ofs1
= 5; /* Green bit offset */
320 params
->ip
.ofs2
= 11; /* Blue bit offset */
321 params
->ip
.ofs3
= 16; /* Alpha bit offset */
322 params
->ip
.wid0
= 4; /* Red bit width - 1 */
323 params
->ip
.wid1
= 5; /* Green bit width - 1 */
324 params
->ip
.wid2
= 4; /* Blue bit width - 1 */
326 case IPU_PIX_FMT_BGR24
:
327 params
->ip
.bpp
= 1; /* 24 BPP & RGB PFS */
330 params
->ip
.sat
= 2; /* SAT = 32-bit access */
331 params
->ip
.ofs0
= 0; /* Red bit offset */
332 params
->ip
.ofs1
= 8; /* Green bit offset */
333 params
->ip
.ofs2
= 16; /* Blue bit offset */
334 params
->ip
.ofs3
= 24; /* Alpha bit offset */
335 params
->ip
.wid0
= 7; /* Red bit width - 1 */
336 params
->ip
.wid1
= 7; /* Green bit width - 1 */
337 params
->ip
.wid2
= 7; /* Blue bit width - 1 */
339 case IPU_PIX_FMT_RGB24
:
340 params
->ip
.bpp
= 1; /* 24 BPP & RGB PFS */
343 params
->ip
.sat
= 2; /* SAT = 32-bit access */
344 params
->ip
.ofs0
= 16; /* Red bit offset */
345 params
->ip
.ofs1
= 8; /* Green bit offset */
346 params
->ip
.ofs2
= 0; /* Blue bit offset */
347 params
->ip
.ofs3
= 24; /* Alpha bit offset */
348 params
->ip
.wid0
= 7; /* Red bit width - 1 */
349 params
->ip
.wid1
= 7; /* Green bit width - 1 */
350 params
->ip
.wid2
= 7; /* Blue bit width - 1 */
352 case IPU_PIX_FMT_BGRA32
:
353 case IPU_PIX_FMT_BGR32
:
354 case IPU_PIX_FMT_ABGR32
:
358 params
->ip
.sat
= 2; /* SAT = 32-bit access */
359 params
->ip
.ofs0
= 8; /* Red bit offset */
360 params
->ip
.ofs1
= 16; /* Green bit offset */
361 params
->ip
.ofs2
= 24; /* Blue bit offset */
362 params
->ip
.ofs3
= 0; /* Alpha bit offset */
363 params
->ip
.wid0
= 7; /* Red bit width - 1 */
364 params
->ip
.wid1
= 7; /* Green bit width - 1 */
365 params
->ip
.wid2
= 7; /* Blue bit width - 1 */
366 params
->ip
.wid3
= 7; /* Alpha bit width - 1 */
368 case IPU_PIX_FMT_RGBA32
:
369 case IPU_PIX_FMT_RGB32
:
373 params
->ip
.sat
= 2; /* SAT = 32-bit access */
374 params
->ip
.ofs0
= 24; /* Red bit offset */
375 params
->ip
.ofs1
= 16; /* Green bit offset */
376 params
->ip
.ofs2
= 8; /* Blue bit offset */
377 params
->ip
.ofs3
= 0; /* Alpha bit offset */
378 params
->ip
.wid0
= 7; /* Red bit width - 1 */
379 params
->ip
.wid1
= 7; /* Green bit width - 1 */
380 params
->ip
.wid2
= 7; /* Blue bit width - 1 */
381 params
->ip
.wid3
= 7; /* Alpha bit width - 1 */
383 case IPU_PIX_FMT_UYVY
:
387 params
->ip
.sat
= 2; /* SAT = 32-bit access */
389 case IPU_PIX_FMT_YUV420P2
:
390 case IPU_PIX_FMT_YUV420P
:
394 params
->ip
.sat
= 2; /* SAT = 32-bit access */
395 u_offset
= stride
* height
;
396 v_offset
= u_offset
+ u_offset
/ 4;
397 ipu_ch_param_set_plane_offset(params
, u_offset
, v_offset
);
399 case IPU_PIX_FMT_YVU422P
:
403 params
->ip
.sat
= 2; /* SAT = 32-bit access */
404 v_offset
= stride
* height
;
405 u_offset
= v_offset
+ v_offset
/ 2;
406 ipu_ch_param_set_plane_offset(params
, u_offset
, v_offset
);
408 case IPU_PIX_FMT_YUV422P
:
412 params
->ip
.sat
= 2; /* SAT = 32-bit access */
413 u_offset
= stride
* height
;
414 v_offset
= u_offset
+ u_offset
/ 2;
415 ipu_ch_param_set_plane_offset(params
, u_offset
, v_offset
);
418 dev_err(ipu_data
.dev
,
419 "mx3 ipu: unimplemented pixel format %d\n", pixel_fmt
);
426 static void ipu_ch_param_set_buffer(union chan_param_mem
*params
,
427 dma_addr_t buf0
, dma_addr_t buf1
)
429 params
->pp
.eba0
= buf0
;
430 params
->pp
.eba1
= buf1
;
433 static void ipu_ch_param_set_rotation(union chan_param_mem
*params
,
434 enum ipu_rotate_mode rotate
)
436 params
->pp
.bam
= rotate
;
439 static void ipu_write_param_mem(uint32_t addr
, uint32_t *data
,
442 for (; num_words
> 0; num_words
--) {
443 dev_dbg(ipu_data
.dev
,
444 "write param mem - addr = 0x%08X, data = 0x%08X\n",
446 idmac_write_ipureg(&ipu_data
, addr
, IPU_IMA_ADDR
);
447 idmac_write_ipureg(&ipu_data
, *data
++, IPU_IMA_DATA
);
449 if ((addr
& 0x7) == 5) {
450 addr
&= ~0x7; /* set to word 0 */
451 addr
+= 8; /* increment to next row */
456 static int calc_resize_coeffs(uint32_t in_size
, uint32_t out_size
,
457 uint32_t *resize_coeff
,
458 uint32_t *downsize_coeff
)
461 uint32_t temp_downsize
;
463 *resize_coeff
= 1 << 13;
464 *downsize_coeff
= 1 << 13;
466 /* Cannot downsize more than 8:1 */
467 if (out_size
<< 3 < in_size
)
470 /* compute downsizing coefficient */
473 while (temp_size
>= out_size
* 2 && temp_downsize
< 2) {
477 *downsize_coeff
= temp_downsize
;
480 * compute resizing coefficient using the following formula:
481 * resize_coeff = M*(SI -1)/(SO - 1)
482 * where M = 2^13, SI - input size, SO - output size
484 *resize_coeff
= (8192L * (temp_size
- 1)) / (out_size
- 1);
485 if (*resize_coeff
>= 16384L) {
486 dev_err(ipu_data
.dev
, "Warning! Overflow on resize coeff.\n");
487 *resize_coeff
= 0x3FFF;
490 dev_dbg(ipu_data
.dev
, "resizing from %u -> %u pixels, "
491 "downsize=%u, resize=%u.%lu (reg=%u)\n", in_size
, out_size
,
492 *downsize_coeff
, *resize_coeff
>= 8192L ? 1 : 0,
493 ((*resize_coeff
& 0x1FFF) * 10000L) / 8192L, *resize_coeff
);
498 static enum ipu_color_space
format_to_colorspace(enum pixel_fmt fmt
)
501 case IPU_PIX_FMT_RGB565
:
502 case IPU_PIX_FMT_BGR24
:
503 case IPU_PIX_FMT_RGB24
:
504 case IPU_PIX_FMT_BGR32
:
505 case IPU_PIX_FMT_RGB32
:
506 return IPU_COLORSPACE_RGB
;
508 return IPU_COLORSPACE_YCBCR
;
512 static int ipu_ic_init_prpenc(struct ipu
*ipu
,
513 union ipu_channel_param
*params
, bool src_is_csi
)
515 uint32_t reg
, ic_conf
;
516 uint32_t downsize_coeff
, resize_coeff
;
517 enum ipu_color_space in_fmt
, out_fmt
;
519 /* Setup vertical resizing */
520 calc_resize_coeffs(params
->video
.in_height
,
521 params
->video
.out_height
,
522 &resize_coeff
, &downsize_coeff
);
523 reg
= (downsize_coeff
<< 30) | (resize_coeff
<< 16);
525 /* Setup horizontal resizing */
526 calc_resize_coeffs(params
->video
.in_width
,
527 params
->video
.out_width
,
528 &resize_coeff
, &downsize_coeff
);
529 reg
|= (downsize_coeff
<< 14) | resize_coeff
;
531 /* Setup color space conversion */
532 in_fmt
= format_to_colorspace(params
->video
.in_pixel_fmt
);
533 out_fmt
= format_to_colorspace(params
->video
.out_pixel_fmt
);
536 * Colourspace conversion unsupported yet - see _init_csc() in
539 if (in_fmt
!= out_fmt
) {
540 dev_err(ipu
->dev
, "Colourspace conversion unsupported!\n");
544 idmac_write_icreg(ipu
, reg
, IC_PRP_ENC_RSC
);
546 ic_conf
= idmac_read_icreg(ipu
, IC_CONF
);
549 ic_conf
&= ~IC_CONF_RWS_EN
;
551 ic_conf
|= IC_CONF_RWS_EN
;
553 idmac_write_icreg(ipu
, ic_conf
, IC_CONF
);
558 static uint32_t dma_param_addr(uint32_t dma_ch
)
560 /* Channel Parameter Memory */
561 return 0x10000 | (dma_ch
<< 4);
564 static void ipu_channel_set_priority(struct ipu
*ipu
, enum ipu_channel channel
,
567 u32 reg
= idmac_read_icreg(ipu
, IDMAC_CHA_PRI
);
570 reg
|= 1UL << channel
;
572 reg
&= ~(1UL << channel
);
574 idmac_write_icreg(ipu
, reg
, IDMAC_CHA_PRI
);
579 static uint32_t ipu_channel_conf_mask(enum ipu_channel channel
)
586 mask
= IPU_CONF_CSI_EN
| IPU_CONF_IC_EN
;
590 mask
= IPU_CONF_SDC_EN
| IPU_CONF_DI_EN
;
601 * ipu_enable_channel() - enable an IPU channel.
602 * @idmac: IPU DMAC context.
603 * @ichan: IDMAC channel.
604 * @return: 0 on success or negative error code on failure.
606 static int ipu_enable_channel(struct idmac
*idmac
, struct idmac_channel
*ichan
)
608 struct ipu
*ipu
= to_ipu(idmac
);
609 enum ipu_channel channel
= ichan
->dma_chan
.chan_id
;
613 spin_lock_irqsave(&ipu
->lock
, flags
);
615 /* Reset to buffer 0 */
616 idmac_write_ipureg(ipu
, 1UL << channel
, IPU_CHA_CUR_BUF
);
617 ichan
->active_buffer
= 0;
618 ichan
->status
= IPU_CHANNEL_ENABLED
;
624 ipu_channel_set_priority(ipu
, channel
, true);
629 reg
= idmac_read_icreg(ipu
, IDMAC_CHA_EN
);
631 idmac_write_icreg(ipu
, reg
| (1UL << channel
), IDMAC_CHA_EN
);
633 ipu_ic_enable_task(ipu
, channel
);
635 spin_unlock_irqrestore(&ipu
->lock
, flags
);
640 * ipu_init_channel_buffer() - initialize a buffer for logical IPU channel.
641 * @ichan: IDMAC channel.
642 * @pixel_fmt: pixel format of buffer. Pixel format is a FOURCC ASCII code.
643 * @width: width of buffer in pixels.
644 * @height: height of buffer in pixels.
645 * @stride: stride length of buffer in pixels.
646 * @rot_mode: rotation mode of buffer. A rotation setting other than
647 * IPU_ROTATE_VERT_FLIP should only be used for input buffers of
649 * @phyaddr_0: buffer 0 physical address.
650 * @phyaddr_1: buffer 1 physical address. Setting this to a value other than
651 * NULL enables double buffering mode.
652 * @return: 0 on success or negative error code on failure.
654 static int ipu_init_channel_buffer(struct idmac_channel
*ichan
,
655 enum pixel_fmt pixel_fmt
,
656 uint16_t width
, uint16_t height
,
658 enum ipu_rotate_mode rot_mode
,
659 dma_addr_t phyaddr_0
, dma_addr_t phyaddr_1
)
661 enum ipu_channel channel
= ichan
->dma_chan
.chan_id
;
662 struct idmac
*idmac
= to_idmac(ichan
->dma_chan
.device
);
663 struct ipu
*ipu
= to_ipu(idmac
);
664 union chan_param_mem params
= {};
667 uint32_t stride_bytes
;
669 stride_bytes
= stride
* bytes_per_pixel(pixel_fmt
);
671 if (stride_bytes
% 4) {
673 "Stride length must be 32-bit aligned, stride = %d, bytes = %d\n",
674 stride
, stride_bytes
);
678 /* IC channel's stride must be a multiple of 8 pixels */
679 if ((channel
<= IDMAC_IC_13
) && (stride
% 8)) {
680 dev_err(ipu
->dev
, "Stride must be 8 pixel multiple\n");
684 /* Build parameter memory data for DMA channel */
685 ipu_ch_param_set_size(¶ms
, pixel_fmt
, width
, height
, stride_bytes
);
686 ipu_ch_param_set_buffer(¶ms
, phyaddr_0
, phyaddr_1
);
687 ipu_ch_param_set_rotation(¶ms
, rot_mode
);
689 spin_lock_irqsave(&ipu
->lock
, flags
);
691 ipu_write_param_mem(dma_param_addr(channel
), (uint32_t *)¶ms
, 10);
693 reg
= idmac_read_ipureg(ipu
, IPU_CHA_DB_MODE_SEL
);
696 reg
|= 1UL << channel
;
698 reg
&= ~(1UL << channel
);
700 idmac_write_ipureg(ipu
, reg
, IPU_CHA_DB_MODE_SEL
);
702 ichan
->status
= IPU_CHANNEL_READY
;
704 spin_unlock_irqrestore(&ipu
->lock
, flags
);
710 * ipu_select_buffer() - mark a channel's buffer as ready.
711 * @channel: channel ID.
712 * @buffer_n: buffer number to mark ready.
714 static void ipu_select_buffer(enum ipu_channel channel
, int buffer_n
)
716 /* No locking - this is a write-one-to-set register, cleared by IPU */
718 /* Mark buffer 0 as ready. */
719 idmac_write_ipureg(&ipu_data
, 1UL << channel
, IPU_CHA_BUF0_RDY
);
721 /* Mark buffer 1 as ready. */
722 idmac_write_ipureg(&ipu_data
, 1UL << channel
, IPU_CHA_BUF1_RDY
);
726 * ipu_update_channel_buffer() - update physical address of a channel buffer.
727 * @ichan: IDMAC channel.
728 * @buffer_n: buffer number to update.
729 * 0 or 1 are the only valid values.
730 * @phyaddr: buffer physical address.
732 /* Called under spin_lock(_irqsave)(&ichan->lock) */
733 static void ipu_update_channel_buffer(struct idmac_channel
*ichan
,
734 int buffer_n
, dma_addr_t phyaddr
)
736 enum ipu_channel channel
= ichan
->dma_chan
.chan_id
;
740 spin_lock_irqsave(&ipu_data
.lock
, flags
);
743 reg
= idmac_read_ipureg(&ipu_data
, IPU_CHA_BUF0_RDY
);
744 if (reg
& (1UL << channel
)) {
745 ipu_ic_disable_task(&ipu_data
, channel
);
746 ichan
->status
= IPU_CHANNEL_READY
;
749 /* 44.3.3.1.9 - Row Number 1 (WORD1, offset 0) */
750 idmac_write_ipureg(&ipu_data
, dma_param_addr(channel
) +
751 0x0008UL
, IPU_IMA_ADDR
);
752 idmac_write_ipureg(&ipu_data
, phyaddr
, IPU_IMA_DATA
);
754 reg
= idmac_read_ipureg(&ipu_data
, IPU_CHA_BUF1_RDY
);
755 if (reg
& (1UL << channel
)) {
756 ipu_ic_disable_task(&ipu_data
, channel
);
757 ichan
->status
= IPU_CHANNEL_READY
;
760 /* Check if double-buffering is already enabled */
761 reg
= idmac_read_ipureg(&ipu_data
, IPU_CHA_DB_MODE_SEL
);
763 if (!(reg
& (1UL << channel
)))
764 idmac_write_ipureg(&ipu_data
, reg
| (1UL << channel
),
765 IPU_CHA_DB_MODE_SEL
);
767 /* 44.3.3.1.9 - Row Number 1 (WORD1, offset 1) */
768 idmac_write_ipureg(&ipu_data
, dma_param_addr(channel
) +
769 0x0009UL
, IPU_IMA_ADDR
);
770 idmac_write_ipureg(&ipu_data
, phyaddr
, IPU_IMA_DATA
);
773 spin_unlock_irqrestore(&ipu_data
.lock
, flags
);
776 /* Called under spin_lock_irqsave(&ichan->lock) */
777 static int ipu_submit_buffer(struct idmac_channel
*ichan
,
778 struct idmac_tx_desc
*desc
, struct scatterlist
*sg
, int buf_idx
)
780 unsigned int chan_id
= ichan
->dma_chan
.chan_id
;
781 struct device
*dev
= &ichan
->dma_chan
.dev
->device
;
783 if (async_tx_test_ack(&desc
->txd
))
787 * On first invocation this shouldn't be necessary, the call to
788 * ipu_init_channel_buffer() above will set addresses for us, so we
789 * could make it conditional on status >= IPU_CHANNEL_ENABLED, but
790 * doing it again shouldn't hurt either.
792 ipu_update_channel_buffer(ichan
, buf_idx
, sg_dma_address(sg
));
794 ipu_select_buffer(chan_id
, buf_idx
);
795 dev_dbg(dev
, "Updated sg %p on channel 0x%x buffer %d\n",
796 sg
, chan_id
, buf_idx
);
801 /* Called under spin_lock_irqsave(&ichan->lock) */
802 static int ipu_submit_channel_buffers(struct idmac_channel
*ichan
,
803 struct idmac_tx_desc
*desc
)
805 struct scatterlist
*sg
;
808 for (i
= 0, sg
= desc
->sg
; i
< 2 && sg
; i
++) {
812 ret
= ipu_submit_buffer(ichan
, desc
, sg
, i
);
823 static dma_cookie_t
idmac_tx_submit(struct dma_async_tx_descriptor
*tx
)
825 struct idmac_tx_desc
*desc
= to_tx_desc(tx
);
826 struct idmac_channel
*ichan
= to_idmac_chan(tx
->chan
);
827 struct idmac
*idmac
= to_idmac(tx
->chan
->device
);
828 struct ipu
*ipu
= to_ipu(idmac
);
829 struct device
*dev
= &ichan
->dma_chan
.dev
->device
;
835 if (!list_empty(&desc
->list
)) {
836 /* The descriptor doesn't belong to client */
837 dev_err(dev
, "Descriptor %p not prepared!\n", tx
);
841 mutex_lock(&ichan
->chan_mutex
);
843 async_tx_clear_ack(tx
);
845 if (ichan
->status
< IPU_CHANNEL_READY
) {
846 struct idmac_video_param
*video
= &ichan
->params
.video
;
848 * Initial buffer assignment - the first two sg-entries from
849 * the descriptor will end up in the IDMAC buffers
851 dma_addr_t dma_1
= sg_is_last(desc
->sg
) ? 0 :
852 sg_dma_address(&desc
->sg
[1]);
854 WARN_ON(ichan
->sg
[0] || ichan
->sg
[1]);
856 cookie
= ipu_init_channel_buffer(ichan
,
857 video
->out_pixel_fmt
,
862 sg_dma_address(&desc
->sg
[0]),
868 dev_dbg(dev
, "Submitting sg %p\n", &desc
->sg
[0]);
870 cookie
= dma_cookie_assign(tx
);
872 /* ipu->lock can be taken under ichan->lock, but not v.v. */
873 spin_lock_irqsave(&ichan
->lock
, flags
);
875 list_add_tail(&desc
->list
, &ichan
->queue
);
876 /* submit_buffers() atomically verifies and fills empty sg slots */
877 ret
= ipu_submit_channel_buffers(ichan
, desc
);
879 spin_unlock_irqrestore(&ichan
->lock
, flags
);
886 if (ichan
->status
< IPU_CHANNEL_ENABLED
) {
887 ret
= ipu_enable_channel(idmac
, ichan
);
898 spin_lock_irqsave(&ichan
->lock
, flags
);
899 list_del_init(&desc
->list
);
900 spin_unlock_irqrestore(&ichan
->lock
, flags
);
902 ichan
->dma_chan
.cookie
= cookie
;
906 mutex_unlock(&ichan
->chan_mutex
);
911 /* Called with ichan->chan_mutex held */
912 static int idmac_desc_alloc(struct idmac_channel
*ichan
, int n
)
914 struct idmac_tx_desc
*desc
= vmalloc(n
* sizeof(struct idmac_tx_desc
));
915 struct idmac
*idmac
= to_idmac(ichan
->dma_chan
.device
);
920 /* No interrupts, just disable the tasklet for a moment */
921 tasklet_disable(&to_ipu(idmac
)->tasklet
);
923 ichan
->n_tx_desc
= n
;
925 INIT_LIST_HEAD(&ichan
->queue
);
926 INIT_LIST_HEAD(&ichan
->free_list
);
929 struct dma_async_tx_descriptor
*txd
= &desc
->txd
;
931 memset(txd
, 0, sizeof(*txd
));
932 dma_async_tx_descriptor_init(txd
, &ichan
->dma_chan
);
933 txd
->tx_submit
= idmac_tx_submit
;
935 list_add(&desc
->list
, &ichan
->free_list
);
940 tasklet_enable(&to_ipu(idmac
)->tasklet
);
946 * ipu_init_channel() - initialize an IPU channel.
947 * @idmac: IPU DMAC context.
948 * @ichan: pointer to the channel object.
949 * @return 0 on success or negative error code on failure.
951 static int ipu_init_channel(struct idmac
*idmac
, struct idmac_channel
*ichan
)
953 union ipu_channel_param
*params
= &ichan
->params
;
955 enum ipu_channel channel
= ichan
->dma_chan
.chan_id
;
958 struct ipu
*ipu
= to_ipu(idmac
);
959 int ret
= 0, n_desc
= 0;
961 dev_dbg(ipu
->dev
, "init channel = %d\n", channel
);
963 if (channel
!= IDMAC_SDC_0
&& channel
!= IDMAC_SDC_1
&&
964 channel
!= IDMAC_IC_7
)
967 spin_lock_irqsave(&ipu
->lock
, flags
);
972 reg
= idmac_read_icreg(ipu
, IC_CONF
);
973 idmac_write_icreg(ipu
, reg
& ~IC_CONF_CSI_MEM_WR_EN
, IC_CONF
);
977 reg
= idmac_read_ipureg(ipu
, IPU_FS_PROC_FLOW
);
978 idmac_write_ipureg(ipu
, reg
& ~FS_ENC_IN_VALID
, IPU_FS_PROC_FLOW
);
979 ret
= ipu_ic_init_prpenc(ipu
, params
, true);
988 ipu
->channel_init_mask
|= 1L << channel
;
990 /* Enable IPU sub module */
991 ipu_conf
= idmac_read_ipureg(ipu
, IPU_CONF
) |
992 ipu_channel_conf_mask(channel
);
993 idmac_write_ipureg(ipu
, ipu_conf
, IPU_CONF
);
995 spin_unlock_irqrestore(&ipu
->lock
, flags
);
997 if (n_desc
&& !ichan
->desc
)
998 ret
= idmac_desc_alloc(ichan
, n_desc
);
1000 dump_idmac_reg(ipu
);
1006 * ipu_uninit_channel() - uninitialize an IPU channel.
1007 * @idmac: IPU DMAC context.
1008 * @ichan: pointer to the channel object.
1010 static void ipu_uninit_channel(struct idmac
*idmac
, struct idmac_channel
*ichan
)
1012 enum ipu_channel channel
= ichan
->dma_chan
.chan_id
;
1013 unsigned long flags
;
1015 unsigned long chan_mask
= 1UL << channel
;
1017 struct ipu
*ipu
= to_ipu(idmac
);
1019 spin_lock_irqsave(&ipu
->lock
, flags
);
1021 if (!(ipu
->channel_init_mask
& chan_mask
)) {
1022 dev_err(ipu
->dev
, "Channel already uninitialized %d\n",
1024 spin_unlock_irqrestore(&ipu
->lock
, flags
);
1028 /* Reset the double buffer */
1029 reg
= idmac_read_ipureg(ipu
, IPU_CHA_DB_MODE_SEL
);
1030 idmac_write_ipureg(ipu
, reg
& ~chan_mask
, IPU_CHA_DB_MODE_SEL
);
1032 ichan
->sec_chan_en
= false;
1036 reg
= idmac_read_icreg(ipu
, IC_CONF
);
1037 idmac_write_icreg(ipu
, reg
& ~(IC_CONF_RWS_EN
| IC_CONF_PRPENC_EN
),
1041 reg
= idmac_read_icreg(ipu
, IC_CONF
);
1042 idmac_write_icreg(ipu
, reg
& ~(IC_CONF_PRPENC_EN
| IC_CONF_PRPENC_CSC1
),
1051 ipu
->channel_init_mask
&= ~(1L << channel
);
1053 ipu_conf
= idmac_read_ipureg(ipu
, IPU_CONF
) &
1054 ~ipu_channel_conf_mask(channel
);
1055 idmac_write_ipureg(ipu
, ipu_conf
, IPU_CONF
);
1057 spin_unlock_irqrestore(&ipu
->lock
, flags
);
1059 ichan
->n_tx_desc
= 0;
1065 * ipu_disable_channel() - disable an IPU channel.
1066 * @idmac: IPU DMAC context.
1067 * @ichan: channel object pointer.
1068 * @wait_for_stop: flag to set whether to wait for channel end of frame or
1069 * return immediately.
1070 * @return: 0 on success or negative error code on failure.
1072 static int ipu_disable_channel(struct idmac
*idmac
, struct idmac_channel
*ichan
,
1075 enum ipu_channel channel
= ichan
->dma_chan
.chan_id
;
1076 struct ipu
*ipu
= to_ipu(idmac
);
1078 unsigned long flags
;
1079 unsigned long chan_mask
= 1UL << channel
;
1080 unsigned int timeout
;
1082 if (wait_for_stop
&& channel
!= IDMAC_SDC_1
&& channel
!= IDMAC_SDC_0
) {
1084 /* This waiting always fails. Related to spurious irq problem */
1085 while ((idmac_read_icreg(ipu
, IDMAC_CHA_BUSY
) & chan_mask
) ||
1086 (ipu_channel_status(ipu
, channel
) == TASK_STAT_ACTIVE
)) {
1092 "Warning: timeout waiting for channel %u to "
1093 "stop: buf0_rdy = 0x%08X, buf1_rdy = 0x%08X, "
1094 "busy = 0x%08X, tstat = 0x%08X\n", channel
,
1095 idmac_read_ipureg(ipu
, IPU_CHA_BUF0_RDY
),
1096 idmac_read_ipureg(ipu
, IPU_CHA_BUF1_RDY
),
1097 idmac_read_icreg(ipu
, IDMAC_CHA_BUSY
),
1098 idmac_read_ipureg(ipu
, IPU_TASKS_STAT
));
1102 dev_dbg(ipu
->dev
, "timeout = %d * 10ms\n", 40 - timeout
);
1104 /* SDC BG and FG must be disabled before DMA is disabled */
1105 if (wait_for_stop
&& (channel
== IDMAC_SDC_0
||
1106 channel
== IDMAC_SDC_1
)) {
1108 timeout
&& !ipu_irq_status(ichan
->eof_irq
); timeout
--)
1112 spin_lock_irqsave(&ipu
->lock
, flags
);
1114 /* Disable IC task */
1115 ipu_ic_disable_task(ipu
, channel
);
1117 /* Disable DMA channel(s) */
1118 reg
= idmac_read_icreg(ipu
, IDMAC_CHA_EN
);
1119 idmac_write_icreg(ipu
, reg
& ~chan_mask
, IDMAC_CHA_EN
);
1121 spin_unlock_irqrestore(&ipu
->lock
, flags
);
1126 static struct scatterlist
*idmac_sg_next(struct idmac_channel
*ichan
,
1127 struct idmac_tx_desc
**desc
, struct scatterlist
*sg
)
1129 struct scatterlist
*sgnew
= sg
? sg_next(sg
) : NULL
;
1132 /* next sg-element in this list */
1135 if ((*desc
)->list
.next
== &ichan
->queue
)
1136 /* No more descriptors on the queue */
1139 /* Fetch next descriptor */
1140 *desc
= list_entry((*desc
)->list
.next
, struct idmac_tx_desc
, list
);
1145 * We have several possibilities here:
1146 * current BUF next BUF
1148 * not last sg next not last sg
1149 * not last sg next last sg
1150 * last sg first sg from next descriptor
1153 * Besides, the descriptor queue might be empty or not. We process all these
1156 static irqreturn_t
idmac_interrupt(int irq
, void *dev_id
)
1158 struct idmac_channel
*ichan
= dev_id
;
1159 struct device
*dev
= &ichan
->dma_chan
.dev
->device
;
1160 unsigned int chan_id
= ichan
->dma_chan
.chan_id
;
1161 struct scatterlist
**sg
, *sgnext
, *sgnew
= NULL
;
1162 /* Next transfer descriptor */
1163 struct idmac_tx_desc
*desc
, *descnew
;
1164 dma_async_tx_callback callback
;
1165 void *callback_param
;
1167 u32 ready0
, ready1
, curbuf
, err
;
1168 unsigned long flags
;
1170 /* IDMAC has cleared the respective BUFx_RDY bit, we manage the buffer */
1172 dev_dbg(dev
, "IDMAC irq %d, buf %d\n", irq
, ichan
->active_buffer
);
1174 spin_lock_irqsave(&ipu_data
.lock
, flags
);
1176 ready0
= idmac_read_ipureg(&ipu_data
, IPU_CHA_BUF0_RDY
);
1177 ready1
= idmac_read_ipureg(&ipu_data
, IPU_CHA_BUF1_RDY
);
1178 curbuf
= idmac_read_ipureg(&ipu_data
, IPU_CHA_CUR_BUF
);
1179 err
= idmac_read_ipureg(&ipu_data
, IPU_INT_STAT_4
);
1181 if (err
& (1 << chan_id
)) {
1182 idmac_write_ipureg(&ipu_data
, 1 << chan_id
, IPU_INT_STAT_4
);
1183 spin_unlock_irqrestore(&ipu_data
.lock
, flags
);
1186 * ichan->sg[0] = ichan->sg[1] = NULL;
1187 * you can force channel re-enable on the next tx_submit(), but
1188 * this is dirty - think about descriptors with multiple
1191 dev_warn(dev
, "NFB4EOF on channel %d, ready %x, %x, cur %x\n",
1192 chan_id
, ready0
, ready1
, curbuf
);
1195 spin_unlock_irqrestore(&ipu_data
.lock
, flags
);
1197 /* Other interrupts do not interfere with this channel */
1198 spin_lock(&ichan
->lock
);
1199 if (unlikely((ichan
->active_buffer
&& (ready1
>> chan_id
) & 1) ||
1200 (!ichan
->active_buffer
&& (ready0
>> chan_id
) & 1)
1202 spin_unlock(&ichan
->lock
);
1204 "IRQ with active buffer still ready on channel %x, "
1205 "active %d, ready %x, %x!\n", chan_id
,
1206 ichan
->active_buffer
, ready0
, ready1
);
1210 if (unlikely(list_empty(&ichan
->queue
))) {
1211 ichan
->sg
[ichan
->active_buffer
] = NULL
;
1212 spin_unlock(&ichan
->lock
);
1214 "IRQ without queued buffers on channel %x, active %d, "
1215 "ready %x, %x!\n", chan_id
,
1216 ichan
->active_buffer
, ready0
, ready1
);
1221 * active_buffer is a software flag, it shows which buffer we are
1222 * currently expecting back from the hardware, IDMAC should be
1223 * processing the other buffer already
1225 sg
= &ichan
->sg
[ichan
->active_buffer
];
1226 sgnext
= ichan
->sg
[!ichan
->active_buffer
];
1229 spin_unlock(&ichan
->lock
);
1233 desc
= list_entry(ichan
->queue
.next
, struct idmac_tx_desc
, list
);
1236 dev_dbg(dev
, "IDMAC irq %d, dma 0x%08x, next dma 0x%08x, current %d, curbuf 0x%08x\n",
1237 irq
, sg_dma_address(*sg
), sgnext
? sg_dma_address(sgnext
) : 0, ichan
->active_buffer
, curbuf
);
1239 /* Find the descriptor of sgnext */
1240 sgnew
= idmac_sg_next(ichan
, &descnew
, *sg
);
1241 if (sgnext
!= sgnew
)
1242 dev_err(dev
, "Submitted buffer %p, next buffer %p\n", sgnext
, sgnew
);
1245 * if sgnext == NULL sg must be the last element in a scatterlist and
1246 * queue must be empty
1248 if (unlikely(!sgnext
)) {
1249 if (!WARN_ON(sg_next(*sg
)))
1250 dev_dbg(dev
, "Underrun on channel %x\n", chan_id
);
1251 ichan
->sg
[!ichan
->active_buffer
] = sgnew
;
1253 if (unlikely(sgnew
)) {
1254 ipu_submit_buffer(ichan
, descnew
, sgnew
, !ichan
->active_buffer
);
1256 spin_lock_irqsave(&ipu_data
.lock
, flags
);
1257 ipu_ic_disable_task(&ipu_data
, chan_id
);
1258 spin_unlock_irqrestore(&ipu_data
.lock
, flags
);
1259 ichan
->status
= IPU_CHANNEL_READY
;
1260 /* Continue to check for complete descriptor */
1264 /* Calculate and submit the next sg element */
1265 sgnew
= idmac_sg_next(ichan
, &descnew
, sgnew
);
1267 if (unlikely(!sg_next(*sg
)) || !sgnext
) {
1269 * Last element in scatterlist done, remove from the queue,
1270 * _init for debugging
1272 list_del_init(&desc
->list
);
1278 if (likely(sgnew
) &&
1279 ipu_submit_buffer(ichan
, descnew
, sgnew
, ichan
->active_buffer
) < 0) {
1280 callback
= descnew
->txd
.callback
;
1281 callback_param
= descnew
->txd
.callback_param
;
1282 list_del_init(&descnew
->list
);
1283 spin_unlock(&ichan
->lock
);
1285 callback(callback_param
);
1286 spin_lock(&ichan
->lock
);
1289 /* Flip the active buffer - even if update above failed */
1290 ichan
->active_buffer
= !ichan
->active_buffer
;
1292 dma_cookie_complete(&desc
->txd
);
1294 callback
= desc
->txd
.callback
;
1295 callback_param
= desc
->txd
.callback_param
;
1297 spin_unlock(&ichan
->lock
);
1299 if (done
&& (desc
->txd
.flags
& DMA_PREP_INTERRUPT
) && callback
)
1300 callback(callback_param
);
1305 static void ipu_gc_tasklet(unsigned long arg
)
1307 struct ipu
*ipu
= (struct ipu
*)arg
;
1310 for (i
= 0; i
< IPU_CHANNELS_NUM
; i
++) {
1311 struct idmac_channel
*ichan
= ipu
->channel
+ i
;
1312 struct idmac_tx_desc
*desc
;
1313 unsigned long flags
;
1314 struct scatterlist
*sg
;
1317 for (j
= 0; j
< ichan
->n_tx_desc
; j
++) {
1318 desc
= ichan
->desc
+ j
;
1319 spin_lock_irqsave(&ichan
->lock
, flags
);
1320 if (async_tx_test_ack(&desc
->txd
)) {
1321 list_move(&desc
->list
, &ichan
->free_list
);
1322 for_each_sg(desc
->sg
, sg
, desc
->sg_len
, k
) {
1323 if (ichan
->sg
[0] == sg
)
1324 ichan
->sg
[0] = NULL
;
1325 else if (ichan
->sg
[1] == sg
)
1326 ichan
->sg
[1] = NULL
;
1328 async_tx_clear_ack(&desc
->txd
);
1330 spin_unlock_irqrestore(&ichan
->lock
, flags
);
1335 /* Allocate and initialise a transfer descriptor. */
1336 static struct dma_async_tx_descriptor
*idmac_prep_slave_sg(struct dma_chan
*chan
,
1337 struct scatterlist
*sgl
, unsigned int sg_len
,
1338 enum dma_transfer_direction direction
, unsigned long tx_flags
,
1341 struct idmac_channel
*ichan
= to_idmac_chan(chan
);
1342 struct idmac_tx_desc
*desc
= NULL
;
1343 struct dma_async_tx_descriptor
*txd
= NULL
;
1344 unsigned long flags
;
1346 /* We only can handle these three channels so far */
1347 if (chan
->chan_id
!= IDMAC_SDC_0
&& chan
->chan_id
!= IDMAC_SDC_1
&&
1348 chan
->chan_id
!= IDMAC_IC_7
)
1351 if (direction
!= DMA_DEV_TO_MEM
&& direction
!= DMA_MEM_TO_DEV
) {
1352 dev_err(chan
->device
->dev
, "Invalid DMA direction %d!\n", direction
);
1356 mutex_lock(&ichan
->chan_mutex
);
1358 spin_lock_irqsave(&ichan
->lock
, flags
);
1359 if (!list_empty(&ichan
->free_list
)) {
1360 desc
= list_entry(ichan
->free_list
.next
,
1361 struct idmac_tx_desc
, list
);
1363 list_del_init(&desc
->list
);
1365 desc
->sg_len
= sg_len
;
1368 txd
->flags
= tx_flags
;
1370 spin_unlock_irqrestore(&ichan
->lock
, flags
);
1372 mutex_unlock(&ichan
->chan_mutex
);
1374 tasklet_schedule(&to_ipu(to_idmac(chan
->device
))->tasklet
);
1379 /* Re-select the current buffer and re-activate the channel */
1380 static void idmac_issue_pending(struct dma_chan
*chan
)
1382 struct idmac_channel
*ichan
= to_idmac_chan(chan
);
1383 struct idmac
*idmac
= to_idmac(chan
->device
);
1384 struct ipu
*ipu
= to_ipu(idmac
);
1385 unsigned long flags
;
1387 /* This is not always needed, but doesn't hurt either */
1388 spin_lock_irqsave(&ipu
->lock
, flags
);
1389 ipu_select_buffer(chan
->chan_id
, ichan
->active_buffer
);
1390 spin_unlock_irqrestore(&ipu
->lock
, flags
);
1393 * Might need to perform some parts of initialisation from
1394 * ipu_enable_channel(), but not all, we do not want to reset to buffer
1395 * 0, don't need to set priority again either, but re-enabling the task
1396 * and the channel might be a good idea.
1400 static int __idmac_control(struct dma_chan
*chan
, enum dma_ctrl_cmd cmd
,
1403 struct idmac_channel
*ichan
= to_idmac_chan(chan
);
1404 struct idmac
*idmac
= to_idmac(chan
->device
);
1405 struct ipu
*ipu
= to_ipu(idmac
);
1406 struct list_head
*list
, *tmp
;
1407 unsigned long flags
;
1412 spin_lock_irqsave(&ipu
->lock
, flags
);
1413 ipu_ic_disable_task(ipu
, chan
->chan_id
);
1415 /* Return all descriptors into "prepared" state */
1416 list_for_each_safe(list
, tmp
, &ichan
->queue
)
1417 list_del_init(list
);
1419 ichan
->sg
[0] = NULL
;
1420 ichan
->sg
[1] = NULL
;
1422 spin_unlock_irqrestore(&ipu
->lock
, flags
);
1424 ichan
->status
= IPU_CHANNEL_INITIALIZED
;
1426 case DMA_TERMINATE_ALL
:
1427 ipu_disable_channel(idmac
, ichan
,
1428 ichan
->status
>= IPU_CHANNEL_ENABLED
);
1430 tasklet_disable(&ipu
->tasklet
);
1432 /* ichan->queue is modified in ISR, have to spinlock */
1433 spin_lock_irqsave(&ichan
->lock
, flags
);
1434 list_splice_init(&ichan
->queue
, &ichan
->free_list
);
1437 for (i
= 0; i
< ichan
->n_tx_desc
; i
++) {
1438 struct idmac_tx_desc
*desc
= ichan
->desc
+ i
;
1439 if (list_empty(&desc
->list
))
1440 /* Descriptor was prepared, but not submitted */
1441 list_add(&desc
->list
, &ichan
->free_list
);
1443 async_tx_clear_ack(&desc
->txd
);
1446 ichan
->sg
[0] = NULL
;
1447 ichan
->sg
[1] = NULL
;
1448 spin_unlock_irqrestore(&ichan
->lock
, flags
);
1450 tasklet_enable(&ipu
->tasklet
);
1452 ichan
->status
= IPU_CHANNEL_INITIALIZED
;
1461 static int idmac_control(struct dma_chan
*chan
, enum dma_ctrl_cmd cmd
,
1464 struct idmac_channel
*ichan
= to_idmac_chan(chan
);
1467 mutex_lock(&ichan
->chan_mutex
);
1469 ret
= __idmac_control(chan
, cmd
, arg
);
1471 mutex_unlock(&ichan
->chan_mutex
);
1477 static irqreturn_t
ic_sof_irq(int irq
, void *dev_id
)
1479 struct idmac_channel
*ichan
= dev_id
;
1480 printk(KERN_DEBUG
"Got SOF IRQ %d on Channel %d\n",
1481 irq
, ichan
->dma_chan
.chan_id
);
1482 disable_irq_nosync(irq
);
1486 static irqreturn_t
ic_eof_irq(int irq
, void *dev_id
)
1488 struct idmac_channel
*ichan
= dev_id
;
1489 printk(KERN_DEBUG
"Got EOF IRQ %d on Channel %d\n",
1490 irq
, ichan
->dma_chan
.chan_id
);
1491 disable_irq_nosync(irq
);
1495 static int ic_sof
= -EINVAL
, ic_eof
= -EINVAL
;
1498 static int idmac_alloc_chan_resources(struct dma_chan
*chan
)
1500 struct idmac_channel
*ichan
= to_idmac_chan(chan
);
1501 struct idmac
*idmac
= to_idmac(chan
->device
);
1504 /* dmaengine.c now guarantees to only offer free channels */
1505 BUG_ON(chan
->client_count
> 1);
1506 WARN_ON(ichan
->status
!= IPU_CHANNEL_FREE
);
1508 dma_cookie_init(chan
);
1510 ret
= ipu_irq_map(chan
->chan_id
);
1514 ichan
->eof_irq
= ret
;
1517 * Important to first disable the channel, because maybe someone
1518 * used it before us, e.g., the bootloader
1520 ipu_disable_channel(idmac
, ichan
, true);
1522 ret
= ipu_init_channel(idmac
, ichan
);
1526 ret
= request_irq(ichan
->eof_irq
, idmac_interrupt
, 0,
1527 ichan
->eof_name
, ichan
);
1532 if (chan
->chan_id
== IDMAC_IC_7
) {
1533 ic_sof
= ipu_irq_map(69);
1535 request_irq(ic_sof
, ic_sof_irq
, 0, "IC SOF", ichan
);
1536 ic_eof
= ipu_irq_map(70);
1538 request_irq(ic_eof
, ic_eof_irq
, 0, "IC EOF", ichan
);
1542 ichan
->status
= IPU_CHANNEL_INITIALIZED
;
1544 dev_dbg(&chan
->dev
->device
, "Found channel 0x%x, irq %d\n",
1545 chan
->chan_id
, ichan
->eof_irq
);
1550 ipu_uninit_channel(idmac
, ichan
);
1552 ipu_irq_unmap(chan
->chan_id
);
1557 static void idmac_free_chan_resources(struct dma_chan
*chan
)
1559 struct idmac_channel
*ichan
= to_idmac_chan(chan
);
1560 struct idmac
*idmac
= to_idmac(chan
->device
);
1562 mutex_lock(&ichan
->chan_mutex
);
1564 __idmac_control(chan
, DMA_TERMINATE_ALL
, 0);
1566 if (ichan
->status
> IPU_CHANNEL_FREE
) {
1568 if (chan
->chan_id
== IDMAC_IC_7
) {
1570 free_irq(ic_sof
, ichan
);
1575 free_irq(ic_eof
, ichan
);
1581 free_irq(ichan
->eof_irq
, ichan
);
1582 ipu_irq_unmap(chan
->chan_id
);
1585 ichan
->status
= IPU_CHANNEL_FREE
;
1587 ipu_uninit_channel(idmac
, ichan
);
1589 mutex_unlock(&ichan
->chan_mutex
);
1591 tasklet_schedule(&to_ipu(idmac
)->tasklet
);
1594 static enum dma_status
idmac_tx_status(struct dma_chan
*chan
,
1595 dma_cookie_t cookie
, struct dma_tx_state
*txstate
)
1597 dma_set_tx_state(txstate
, chan
->completed_cookie
, chan
->cookie
, 0);
1598 if (cookie
!= chan
->cookie
)
1603 static int __init
ipu_idmac_init(struct ipu
*ipu
)
1605 struct idmac
*idmac
= &ipu
->idmac
;
1606 struct dma_device
*dma
= &idmac
->dma
;
1609 dma_cap_set(DMA_SLAVE
, dma
->cap_mask
);
1610 dma_cap_set(DMA_PRIVATE
, dma
->cap_mask
);
1612 /* Compulsory common fields */
1613 dma
->dev
= ipu
->dev
;
1614 dma
->device_alloc_chan_resources
= idmac_alloc_chan_resources
;
1615 dma
->device_free_chan_resources
= idmac_free_chan_resources
;
1616 dma
->device_tx_status
= idmac_tx_status
;
1617 dma
->device_issue_pending
= idmac_issue_pending
;
1619 /* Compulsory for DMA_SLAVE fields */
1620 dma
->device_prep_slave_sg
= idmac_prep_slave_sg
;
1621 dma
->device_control
= idmac_control
;
1623 INIT_LIST_HEAD(&dma
->channels
);
1624 for (i
= 0; i
< IPU_CHANNELS_NUM
; i
++) {
1625 struct idmac_channel
*ichan
= ipu
->channel
+ i
;
1626 struct dma_chan
*dma_chan
= &ichan
->dma_chan
;
1628 spin_lock_init(&ichan
->lock
);
1629 mutex_init(&ichan
->chan_mutex
);
1631 ichan
->status
= IPU_CHANNEL_FREE
;
1632 ichan
->sec_chan_en
= false;
1633 snprintf(ichan
->eof_name
, sizeof(ichan
->eof_name
), "IDMAC EOF %d", i
);
1635 dma_chan
->device
= &idmac
->dma
;
1636 dma_cookie_init(dma_chan
);
1637 dma_chan
->chan_id
= i
;
1638 list_add_tail(&dma_chan
->device_node
, &dma
->channels
);
1641 idmac_write_icreg(ipu
, 0x00000070, IDMAC_CONF
);
1643 return dma_async_device_register(&idmac
->dma
);
1646 static void __exit
ipu_idmac_exit(struct ipu
*ipu
)
1649 struct idmac
*idmac
= &ipu
->idmac
;
1651 for (i
= 0; i
< IPU_CHANNELS_NUM
; i
++) {
1652 struct idmac_channel
*ichan
= ipu
->channel
+ i
;
1654 idmac_control(&ichan
->dma_chan
, DMA_TERMINATE_ALL
, 0);
1657 dma_async_device_unregister(&idmac
->dma
);
1660 /*****************************************************************************
1661 * IPU common probe / remove
1664 static int __init
ipu_probe(struct platform_device
*pdev
)
1666 struct resource
*mem_ipu
, *mem_ic
;
1669 spin_lock_init(&ipu_data
.lock
);
1671 mem_ipu
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1672 mem_ic
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1673 if (!mem_ipu
|| !mem_ic
)
1676 ipu_data
.dev
= &pdev
->dev
;
1678 platform_set_drvdata(pdev
, &ipu_data
);
1680 ret
= platform_get_irq(pdev
, 0);
1684 ipu_data
.irq_fn
= ret
;
1685 ret
= platform_get_irq(pdev
, 1);
1689 ipu_data
.irq_err
= ret
;
1691 dev_dbg(&pdev
->dev
, "fn irq %u, err irq %u\n",
1692 ipu_data
.irq_fn
, ipu_data
.irq_err
);
1694 /* Remap IPU common registers */
1695 ipu_data
.reg_ipu
= ioremap(mem_ipu
->start
, resource_size(mem_ipu
));
1696 if (!ipu_data
.reg_ipu
) {
1698 goto err_ioremap_ipu
;
1701 /* Remap Image Converter and Image DMA Controller registers */
1702 ipu_data
.reg_ic
= ioremap(mem_ic
->start
, resource_size(mem_ic
));
1703 if (!ipu_data
.reg_ic
) {
1705 goto err_ioremap_ic
;
1709 ipu_data
.ipu_clk
= clk_get(&pdev
->dev
, NULL
);
1710 if (IS_ERR(ipu_data
.ipu_clk
)) {
1711 ret
= PTR_ERR(ipu_data
.ipu_clk
);
1715 /* Make sure IPU HSP clock is running */
1716 clk_prepare_enable(ipu_data
.ipu_clk
);
1718 /* Disable all interrupts */
1719 idmac_write_ipureg(&ipu_data
, 0, IPU_INT_CTRL_1
);
1720 idmac_write_ipureg(&ipu_data
, 0, IPU_INT_CTRL_2
);
1721 idmac_write_ipureg(&ipu_data
, 0, IPU_INT_CTRL_3
);
1722 idmac_write_ipureg(&ipu_data
, 0, IPU_INT_CTRL_4
);
1723 idmac_write_ipureg(&ipu_data
, 0, IPU_INT_CTRL_5
);
1725 dev_dbg(&pdev
->dev
, "%s @ 0x%08lx, fn irq %u, err irq %u\n", pdev
->name
,
1726 (unsigned long)mem_ipu
->start
, ipu_data
.irq_fn
, ipu_data
.irq_err
);
1728 ret
= ipu_irq_attach_irq(&ipu_data
, pdev
);
1730 goto err_attach_irq
;
1732 /* Initialize DMA engine */
1733 ret
= ipu_idmac_init(&ipu_data
);
1735 goto err_idmac_init
;
1737 tasklet_init(&ipu_data
.tasklet
, ipu_gc_tasklet
, (unsigned long)&ipu_data
);
1739 ipu_data
.dev
= &pdev
->dev
;
1741 dev_dbg(ipu_data
.dev
, "IPU initialized\n");
1747 ipu_irq_detach_irq(&ipu_data
, pdev
);
1748 clk_disable_unprepare(ipu_data
.ipu_clk
);
1749 clk_put(ipu_data
.ipu_clk
);
1751 iounmap(ipu_data
.reg_ic
);
1753 iounmap(ipu_data
.reg_ipu
);
1756 dev_err(&pdev
->dev
, "Failed to probe IPU: %d\n", ret
);
1760 static int __exit
ipu_remove(struct platform_device
*pdev
)
1762 struct ipu
*ipu
= platform_get_drvdata(pdev
);
1764 ipu_idmac_exit(ipu
);
1765 ipu_irq_detach_irq(ipu
, pdev
);
1766 clk_disable_unprepare(ipu
->ipu_clk
);
1767 clk_put(ipu
->ipu_clk
);
1768 iounmap(ipu
->reg_ic
);
1769 iounmap(ipu
->reg_ipu
);
1770 tasklet_kill(&ipu
->tasklet
);
1771 platform_set_drvdata(pdev
, NULL
);
1777 * We need two MEM resources - with IPU-common and Image Converter registers,
1778 * including PF_CONF and IDMAC_* registers, and two IRQs - function and error
1780 static struct platform_driver ipu_platform_driver
= {
1783 .owner
= THIS_MODULE
,
1785 .remove
= __exit_p(ipu_remove
),
1788 static int __init
ipu_init(void)
1790 return platform_driver_probe(&ipu_platform_driver
, ipu_probe
);
1792 subsys_initcall(ipu_init
);
1794 MODULE_DESCRIPTION("IPU core driver");
1795 MODULE_LICENSE("GPL v2");
1796 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
1797 MODULE_ALIAS("platform:ipu-core");