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/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>
26 #include "ipu_intern.h"
28 #define FS_VF_IN_VALID 0x00000002
29 #define FS_ENC_IN_VALID 0x00000001
31 static int ipu_disable_channel(struct idmac *idmac, struct idmac_channel *ichan,
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));
40 static struct ipu ipu_data;
42 #define to_ipu(id) container_of(id, struct ipu, idmac)
44 static u32 __idmac_read_icreg(struct ipu *ipu, unsigned long reg)
46 return __raw_readl(ipu->reg_ic + reg);
49 #define idmac_read_icreg(ipu, reg) __idmac_read_icreg(ipu, reg - IC_CONF)
51 static void __idmac_write_icreg(struct ipu *ipu, u32 value, unsigned long reg)
53 __raw_writel(value, ipu->reg_ic + reg);
56 #define idmac_write_icreg(ipu, v, reg) __idmac_write_icreg(ipu, v, reg - IC_CONF)
58 static u32 idmac_read_ipureg(struct ipu *ipu, unsigned long reg)
60 return __raw_readl(ipu->reg_ipu + reg);
63 static void idmac_write_ipureg(struct ipu *ipu, u32 value, unsigned long reg)
65 __raw_writel(value, ipu->reg_ipu + reg);
68 /*****************************************************************************
69 * IPU / IC common functions
71 static void dump_idmac_reg(struct ipu *ipu)
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));
89 static uint32_t bytes_per_pixel(enum pixel_fmt 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:
98 case IPU_PIX_FMT_RGB565:
99 case IPU_PIX_FMT_YUYV:
100 case IPU_PIX_FMT_UYVY:
102 case IPU_PIX_FMT_BGR24:
103 case IPU_PIX_FMT_RGB24:
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:
113 /* Enable direct write to memory by the Camera Sensor Interface */
114 static void ipu_ic_enable_task(struct ipu *ipu, enum ipu_channel channel)
116 uint32_t ic_conf, mask;
120 mask = IC_CONF_PRPENC_EN;
123 mask = IC_CONF_RWS_EN | IC_CONF_PRPENC_EN;
128 ic_conf = idmac_read_icreg(ipu, IC_CONF) | mask;
129 idmac_write_icreg(ipu, ic_conf, IC_CONF);
132 /* Called under spin_lock_irqsave(&ipu_data.lock) */
133 static void ipu_ic_disable_task(struct ipu *ipu, enum ipu_channel channel)
135 uint32_t ic_conf, mask;
139 mask = IC_CONF_PRPENC_EN;
142 mask = IC_CONF_RWS_EN | IC_CONF_PRPENC_EN;
147 ic_conf = idmac_read_icreg(ipu, IC_CONF) & ~mask;
148 idmac_write_icreg(ipu, ic_conf, IC_CONF);
151 static uint32_t ipu_channel_status(struct ipu *ipu, enum ipu_channel channel)
153 uint32_t stat = TASK_STAT_IDLE;
154 uint32_t task_stat_reg = idmac_read_ipureg(ipu, IPU_TASKS_STAT);
158 stat = (task_stat_reg & TSTAT_CSI2MEM_MASK) >>
159 TSTAT_CSI2MEM_OFFSET;
170 struct chan_param_mem_planar {
208 } __attribute__ ((packed));
210 struct chan_param_mem_interleaved {
267 } __attribute__ ((packed));
269 union chan_param_mem {
270 struct chan_param_mem_planar pp;
271 struct chan_param_mem_interleaved ip;
274 static void ipu_ch_param_set_plane_offset(union chan_param_mem *params,
275 u32 u_offset, u32 v_offset)
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;
283 static 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)
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;
296 case IPU_PIX_FMT_GENERIC:
297 /*Represents 8-bit Generic data */
301 params->pp.sat = 2; /* SAT = use 32-bit access */
303 case IPU_PIX_FMT_GENERIC_32:
304 /*Represents 32-bit Generic data */
308 params->pp.sat = 2; /* SAT = use 32-bit access */
310 case IPU_PIX_FMT_RGB565:
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 */
323 case IPU_PIX_FMT_BGR24:
324 params->ip.bpp = 1; /* 24 BPP & RGB PFS */
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 */
336 case IPU_PIX_FMT_RGB24:
337 params->ip.bpp = 1; /* 24 BPP & RGB PFS */
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 */
349 case IPU_PIX_FMT_BGRA32:
350 case IPU_PIX_FMT_BGR32:
351 case IPU_PIX_FMT_ABGR32:
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 */
365 case IPU_PIX_FMT_RGBA32:
366 case IPU_PIX_FMT_RGB32:
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 */
380 case IPU_PIX_FMT_UYVY:
384 params->ip.sat = 2; /* SAT = 32-bit access */
386 case IPU_PIX_FMT_YUV420P2:
387 case IPU_PIX_FMT_YUV420P:
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);
396 case IPU_PIX_FMT_YVU422P:
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);
405 case IPU_PIX_FMT_YUV422P:
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);
415 dev_err(ipu_data.dev,
416 "mx3 ipu: unimplemented pixel format %d\n", pixel_fmt);
423 static void ipu_ch_param_set_burst_size(union chan_param_mem *params,
424 uint16_t burst_pixels)
426 params->pp.npb = burst_pixels - 1;
429 static void ipu_ch_param_set_buffer(union chan_param_mem *params,
430 dma_addr_t buf0, dma_addr_t buf1)
432 params->pp.eba0 = buf0;
433 params->pp.eba1 = buf1;
436 static void ipu_ch_param_set_rotation(union chan_param_mem *params,
437 enum ipu_rotate_mode rotate)
439 params->pp.bam = rotate;
442 static void ipu_write_param_mem(uint32_t addr, uint32_t *data,
445 for (; num_words > 0; num_words--) {
446 dev_dbg(ipu_data.dev,
447 "write param mem - addr = 0x%08X, data = 0x%08X\n",
449 idmac_write_ipureg(&ipu_data, addr, IPU_IMA_ADDR);
450 idmac_write_ipureg(&ipu_data, *data++, IPU_IMA_DATA);
452 if ((addr & 0x7) == 5) {
453 addr &= ~0x7; /* set to word 0 */
454 addr += 8; /* increment to next row */
459 static int calc_resize_coeffs(uint32_t in_size, uint32_t out_size,
460 uint32_t *resize_coeff,
461 uint32_t *downsize_coeff)
464 uint32_t temp_downsize;
466 *resize_coeff = 1 << 13;
467 *downsize_coeff = 1 << 13;
469 /* Cannot downsize more than 8:1 */
470 if (out_size << 3 < in_size)
473 /* compute downsizing coefficient */
476 while (temp_size >= out_size * 2 && temp_downsize < 2) {
480 *downsize_coeff = temp_downsize;
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
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;
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);
501 static enum ipu_color_space format_to_colorspace(enum pixel_fmt 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;
511 return IPU_COLORSPACE_YCBCR;
515 static int ipu_ic_init_prpenc(struct ipu *ipu,
516 union ipu_channel_param *params, bool src_is_csi)
518 uint32_t reg, ic_conf;
519 uint32_t downsize_coeff, resize_coeff;
520 enum ipu_color_space in_fmt, out_fmt;
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);
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;
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);
539 * Colourspace conversion unsupported yet - see _init_csc() in
542 if (in_fmt != out_fmt) {
543 dev_err(ipu->dev, "Colourspace conversion unsupported!\n");
547 idmac_write_icreg(ipu, reg, IC_PRP_ENC_RSC);
549 ic_conf = idmac_read_icreg(ipu, IC_CONF);
552 ic_conf &= ~IC_CONF_RWS_EN;
554 ic_conf |= IC_CONF_RWS_EN;
556 idmac_write_icreg(ipu, ic_conf, IC_CONF);
561 static uint32_t dma_param_addr(uint32_t dma_ch)
563 /* Channel Parameter Memory */
564 return 0x10000 | (dma_ch << 4);
567 static void ipu_channel_set_priority(struct ipu *ipu, enum ipu_channel channel,
570 u32 reg = idmac_read_icreg(ipu, IDMAC_CHA_PRI);
573 reg |= 1UL << channel;
575 reg &= ~(1UL << channel);
577 idmac_write_icreg(ipu, reg, IDMAC_CHA_PRI);
582 static uint32_t ipu_channel_conf_mask(enum ipu_channel channel)
589 mask = IPU_CONF_CSI_EN | IPU_CONF_IC_EN;
593 mask = IPU_CONF_SDC_EN | IPU_CONF_DI_EN;
604 * ipu_enable_channel() - enable an IPU channel.
605 * @idmac: IPU DMAC context.
606 * @ichan: IDMAC channel.
607 * @return: 0 on success or negative error code on failure.
609 static int ipu_enable_channel(struct idmac *idmac, struct idmac_channel *ichan)
611 struct ipu *ipu = to_ipu(idmac);
612 enum ipu_channel channel = ichan->dma_chan.chan_id;
616 spin_lock_irqsave(&ipu->lock, flags);
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;
627 ipu_channel_set_priority(ipu, channel, true);
632 reg = idmac_read_icreg(ipu, IDMAC_CHA_EN);
634 idmac_write_icreg(ipu, reg | (1UL << channel), IDMAC_CHA_EN);
636 ipu_ic_enable_task(ipu, channel);
638 spin_unlock_irqrestore(&ipu->lock, flags);
643 * ipu_init_channel_buffer() - initialize a buffer for logical IPU channel.
644 * @ichan: IDMAC channel.
645 * @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
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.
657 static int ipu_init_channel_buffer(struct idmac_channel *ichan,
658 enum pixel_fmt pixel_fmt,
659 uint16_t width, uint16_t height,
661 enum ipu_rotate_mode rot_mode,
662 dma_addr_t phyaddr_0, dma_addr_t phyaddr_1)
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 = {};
670 uint32_t stride_bytes;
672 stride_bytes = stride * bytes_per_pixel(pixel_fmt);
674 if (stride_bytes % 4) {
676 "Stride length must be 32-bit aligned, stride = %d, bytes = %d\n",
677 stride, stride_bytes);
681 /* IC channel's stride must be a multiple of 8 pixels */
682 if ((channel <= IDMAC_IC_13) && (stride % 8)) {
683 dev_err(ipu->dev, "Stride must be 8 pixel multiple\n");
687 /* Build parameter memory data for DMA channel */
688 ipu_ch_param_set_size(¶ms, pixel_fmt, width, height, stride_bytes);
689 ipu_ch_param_set_buffer(¶ms, phyaddr_0, phyaddr_1);
690 ipu_ch_param_set_rotation(¶ms, rot_mode);
691 /* Some channels (rotation) have restriction on burst length */
693 case IDMAC_IC_7: /* Hangs with burst 8, 16, other values
694 invalid - Table 44-30 */
696 ipu_ch_param_set_burst_size(¶ms, 8);
701 /* In original code only IPU_PIX_FMT_RGB565 was setting burst */
702 ipu_ch_param_set_burst_size(¶ms, 16);
709 spin_lock_irqsave(&ipu->lock, flags);
711 ipu_write_param_mem(dma_param_addr(channel), (uint32_t *)¶ms, 10);
713 reg = idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL);
716 reg |= 1UL << channel;
718 reg &= ~(1UL << channel);
720 idmac_write_ipureg(ipu, reg, IPU_CHA_DB_MODE_SEL);
722 ichan->status = IPU_CHANNEL_READY;
724 spin_unlock_irqrestore(&ipu->lock, flags);
730 * ipu_select_buffer() - mark a channel's buffer as ready.
731 * @channel: channel ID.
732 * @buffer_n: buffer number to mark ready.
734 static void ipu_select_buffer(enum ipu_channel channel, int buffer_n)
736 /* No locking - this is a write-one-to-set register, cleared by IPU */
738 /* Mark buffer 0 as ready. */
739 idmac_write_ipureg(&ipu_data, 1UL << channel, IPU_CHA_BUF0_RDY);
741 /* Mark buffer 1 as ready. */
742 idmac_write_ipureg(&ipu_data, 1UL << channel, IPU_CHA_BUF1_RDY);
746 * ipu_update_channel_buffer() - update physical address of a channel buffer.
747 * @ichan: IDMAC channel.
748 * @buffer_n: buffer number to update.
749 * 0 or 1 are the only valid values.
750 * @phyaddr: buffer physical address.
752 /* Called under spin_lock(_irqsave)(&ichan->lock) */
753 static void ipu_update_channel_buffer(struct idmac_channel *ichan,
754 int buffer_n, dma_addr_t phyaddr)
756 enum ipu_channel channel = ichan->dma_chan.chan_id;
760 spin_lock_irqsave(&ipu_data.lock, flags);
763 reg = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF0_RDY);
764 if (reg & (1UL << channel)) {
765 ipu_ic_disable_task(&ipu_data, channel);
766 ichan->status = IPU_CHANNEL_READY;
769 /* 44.3.3.1.9 - Row Number 1 (WORD1, offset 0) */
770 idmac_write_ipureg(&ipu_data, dma_param_addr(channel) +
771 0x0008UL, IPU_IMA_ADDR);
772 idmac_write_ipureg(&ipu_data, phyaddr, IPU_IMA_DATA);
774 reg = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF1_RDY);
775 if (reg & (1UL << channel)) {
776 ipu_ic_disable_task(&ipu_data, channel);
777 ichan->status = IPU_CHANNEL_READY;
780 /* Check if double-buffering is already enabled */
781 reg = idmac_read_ipureg(&ipu_data, IPU_CHA_DB_MODE_SEL);
783 if (!(reg & (1UL << channel)))
784 idmac_write_ipureg(&ipu_data, reg | (1UL << channel),
785 IPU_CHA_DB_MODE_SEL);
787 /* 44.3.3.1.9 - Row Number 1 (WORD1, offset 1) */
788 idmac_write_ipureg(&ipu_data, dma_param_addr(channel) +
789 0x0009UL, IPU_IMA_ADDR);
790 idmac_write_ipureg(&ipu_data, phyaddr, IPU_IMA_DATA);
793 spin_unlock_irqrestore(&ipu_data.lock, flags);
796 /* Called under spin_lock_irqsave(&ichan->lock) */
797 static int ipu_submit_buffer(struct idmac_channel *ichan,
798 struct idmac_tx_desc *desc, struct scatterlist *sg, int buf_idx)
800 unsigned int chan_id = ichan->dma_chan.chan_id;
801 struct device *dev = &ichan->dma_chan.dev->device;
803 if (async_tx_test_ack(&desc->txd))
807 * On first invocation this shouldn't be necessary, the call to
808 * ipu_init_channel_buffer() above will set addresses for us, so we
809 * could make it conditional on status >= IPU_CHANNEL_ENABLED, but
810 * doing it again shouldn't hurt either.
812 ipu_update_channel_buffer(ichan, buf_idx, sg_dma_address(sg));
814 ipu_select_buffer(chan_id, buf_idx);
815 dev_dbg(dev, "Updated sg %p on channel 0x%x buffer %d\n",
816 sg, chan_id, buf_idx);
821 /* Called under spin_lock_irqsave(&ichan->lock) */
822 static int ipu_submit_channel_buffers(struct idmac_channel *ichan,
823 struct idmac_tx_desc *desc)
825 struct scatterlist *sg;
828 for (i = 0, sg = desc->sg; i < 2 && sg; i++) {
832 ret = ipu_submit_buffer(ichan, desc, sg, i);
843 static dma_cookie_t idmac_tx_submit(struct dma_async_tx_descriptor *tx)
845 struct idmac_tx_desc *desc = to_tx_desc(tx);
846 struct idmac_channel *ichan = to_idmac_chan(tx->chan);
847 struct idmac *idmac = to_idmac(tx->chan->device);
848 struct ipu *ipu = to_ipu(idmac);
849 struct device *dev = &ichan->dma_chan.dev->device;
855 if (!list_empty(&desc->list)) {
856 /* The descriptor doesn't belong to client */
857 dev_err(dev, "Descriptor %p not prepared!\n", tx);
861 mutex_lock(&ichan->chan_mutex);
863 async_tx_clear_ack(tx);
865 if (ichan->status < IPU_CHANNEL_READY) {
866 struct idmac_video_param *video = &ichan->params.video;
868 * Initial buffer assignment - the first two sg-entries from
869 * the descriptor will end up in the IDMAC buffers
871 dma_addr_t dma_1 = sg_is_last(desc->sg) ? 0 :
872 sg_dma_address(&desc->sg[1]);
874 WARN_ON(ichan->sg[0] || ichan->sg[1]);
876 cookie = ipu_init_channel_buffer(ichan,
877 video->out_pixel_fmt,
882 sg_dma_address(&desc->sg[0]),
888 dev_dbg(dev, "Submitting sg %p\n", &desc->sg[0]);
890 cookie = ichan->dma_chan.cookie;
895 /* from dmaengine.h: "last cookie value returned to client" */
896 ichan->dma_chan.cookie = cookie;
899 /* ipu->lock can be taken under ichan->lock, but not v.v. */
900 spin_lock_irqsave(&ichan->lock, flags);
902 list_add_tail(&desc->list, &ichan->queue);
903 /* submit_buffers() atomically verifies and fills empty sg slots */
904 ret = ipu_submit_channel_buffers(ichan, desc);
906 spin_unlock_irqrestore(&ichan->lock, flags);
913 if (ichan->status < IPU_CHANNEL_ENABLED) {
914 ret = ipu_enable_channel(idmac, ichan);
925 spin_lock_irqsave(&ichan->lock, flags);
926 list_del_init(&desc->list);
927 spin_unlock_irqrestore(&ichan->lock, flags);
929 ichan->dma_chan.cookie = cookie;
933 mutex_unlock(&ichan->chan_mutex);
938 /* Called with ichan->chan_mutex held */
939 static int idmac_desc_alloc(struct idmac_channel *ichan, int n)
941 struct idmac_tx_desc *desc = vmalloc(n * sizeof(struct idmac_tx_desc));
942 struct idmac *idmac = to_idmac(ichan->dma_chan.device);
947 /* No interrupts, just disable the tasklet for a moment */
948 tasklet_disable(&to_ipu(idmac)->tasklet);
950 ichan->n_tx_desc = n;
952 INIT_LIST_HEAD(&ichan->queue);
953 INIT_LIST_HEAD(&ichan->free_list);
956 struct dma_async_tx_descriptor *txd = &desc->txd;
958 memset(txd, 0, sizeof(*txd));
959 dma_async_tx_descriptor_init(txd, &ichan->dma_chan);
960 txd->tx_submit = idmac_tx_submit;
962 list_add(&desc->list, &ichan->free_list);
967 tasklet_enable(&to_ipu(idmac)->tasklet);
973 * ipu_init_channel() - initialize an IPU channel.
974 * @idmac: IPU DMAC context.
975 * @ichan: pointer to the channel object.
976 * @return 0 on success or negative error code on failure.
978 static int ipu_init_channel(struct idmac *idmac, struct idmac_channel *ichan)
980 union ipu_channel_param *params = &ichan->params;
982 enum ipu_channel channel = ichan->dma_chan.chan_id;
985 struct ipu *ipu = to_ipu(idmac);
986 int ret = 0, n_desc = 0;
988 dev_dbg(ipu->dev, "init channel = %d\n", channel);
990 if (channel != IDMAC_SDC_0 && channel != IDMAC_SDC_1 &&
991 channel != IDMAC_IC_7)
994 spin_lock_irqsave(&ipu->lock, flags);
999 reg = idmac_read_icreg(ipu, IC_CONF);
1000 idmac_write_icreg(ipu, reg & ~IC_CONF_CSI_MEM_WR_EN, IC_CONF);
1004 reg = idmac_read_ipureg(ipu, IPU_FS_PROC_FLOW);
1005 idmac_write_ipureg(ipu, reg & ~FS_ENC_IN_VALID, IPU_FS_PROC_FLOW);
1006 ret = ipu_ic_init_prpenc(ipu, params, true);
1015 ipu->channel_init_mask |= 1L << channel;
1017 /* Enable IPU sub module */
1018 ipu_conf = idmac_read_ipureg(ipu, IPU_CONF) |
1019 ipu_channel_conf_mask(channel);
1020 idmac_write_ipureg(ipu, ipu_conf, IPU_CONF);
1022 spin_unlock_irqrestore(&ipu->lock, flags);
1024 if (n_desc && !ichan->desc)
1025 ret = idmac_desc_alloc(ichan, n_desc);
1027 dump_idmac_reg(ipu);
1033 * ipu_uninit_channel() - uninitialize an IPU channel.
1034 * @idmac: IPU DMAC context.
1035 * @ichan: pointer to the channel object.
1037 static void ipu_uninit_channel(struct idmac *idmac, struct idmac_channel *ichan)
1039 enum ipu_channel channel = ichan->dma_chan.chan_id;
1040 unsigned long flags;
1042 unsigned long chan_mask = 1UL << channel;
1044 struct ipu *ipu = to_ipu(idmac);
1046 spin_lock_irqsave(&ipu->lock, flags);
1048 if (!(ipu->channel_init_mask & chan_mask)) {
1049 dev_err(ipu->dev, "Channel already uninitialized %d\n",
1051 spin_unlock_irqrestore(&ipu->lock, flags);
1055 /* Reset the double buffer */
1056 reg = idmac_read_ipureg(ipu, IPU_CHA_DB_MODE_SEL);
1057 idmac_write_ipureg(ipu, reg & ~chan_mask, IPU_CHA_DB_MODE_SEL);
1059 ichan->sec_chan_en = false;
1063 reg = idmac_read_icreg(ipu, IC_CONF);
1064 idmac_write_icreg(ipu, reg & ~(IC_CONF_RWS_EN | IC_CONF_PRPENC_EN),
1068 reg = idmac_read_icreg(ipu, IC_CONF);
1069 idmac_write_icreg(ipu, reg & ~(IC_CONF_PRPENC_EN | IC_CONF_PRPENC_CSC1),
1078 ipu->channel_init_mask &= ~(1L << channel);
1080 ipu_conf = idmac_read_ipureg(ipu, IPU_CONF) &
1081 ~ipu_channel_conf_mask(channel);
1082 idmac_write_ipureg(ipu, ipu_conf, IPU_CONF);
1084 spin_unlock_irqrestore(&ipu->lock, flags);
1086 ichan->n_tx_desc = 0;
1092 * ipu_disable_channel() - disable an IPU channel.
1093 * @idmac: IPU DMAC context.
1094 * @ichan: channel object pointer.
1095 * @wait_for_stop: flag to set whether to wait for channel end of frame or
1096 * return immediately.
1097 * @return: 0 on success or negative error code on failure.
1099 static int ipu_disable_channel(struct idmac *idmac, struct idmac_channel *ichan,
1102 enum ipu_channel channel = ichan->dma_chan.chan_id;
1103 struct ipu *ipu = to_ipu(idmac);
1105 unsigned long flags;
1106 unsigned long chan_mask = 1UL << channel;
1107 unsigned int timeout;
1109 if (wait_for_stop && channel != IDMAC_SDC_1 && channel != IDMAC_SDC_0) {
1111 /* This waiting always fails. Related to spurious irq problem */
1112 while ((idmac_read_icreg(ipu, IDMAC_CHA_BUSY) & chan_mask) ||
1113 (ipu_channel_status(ipu, channel) == TASK_STAT_ACTIVE)) {
1119 "Warning: timeout waiting for channel %u to "
1120 "stop: buf0_rdy = 0x%08X, buf1_rdy = 0x%08X, "
1121 "busy = 0x%08X, tstat = 0x%08X\n", channel,
1122 idmac_read_ipureg(ipu, IPU_CHA_BUF0_RDY),
1123 idmac_read_ipureg(ipu, IPU_CHA_BUF1_RDY),
1124 idmac_read_icreg(ipu, IDMAC_CHA_BUSY),
1125 idmac_read_ipureg(ipu, IPU_TASKS_STAT));
1129 dev_dbg(ipu->dev, "timeout = %d * 10ms\n", 40 - timeout);
1131 /* SDC BG and FG must be disabled before DMA is disabled */
1132 if (wait_for_stop && (channel == IDMAC_SDC_0 ||
1133 channel == IDMAC_SDC_1)) {
1135 timeout && !ipu_irq_status(ichan->eof_irq); timeout--)
1139 spin_lock_irqsave(&ipu->lock, flags);
1141 /* Disable IC task */
1142 ipu_ic_disable_task(ipu, channel);
1144 /* Disable DMA channel(s) */
1145 reg = idmac_read_icreg(ipu, IDMAC_CHA_EN);
1146 idmac_write_icreg(ipu, reg & ~chan_mask, IDMAC_CHA_EN);
1149 * Problem (observed with channel DMAIC_7): after enabling the channel
1150 * and initialising buffers, there comes an interrupt with current still
1151 * pointing at buffer 0, whereas it should use buffer 0 first and only
1152 * generate an interrupt when it is done, then current should already
1153 * point to buffer 1. This spurious interrupt also comes on channel
1154 * DMASDC_0. With DMAIC_7 normally, is we just leave the ISR after the
1155 * first interrupt, there comes the second with current correctly
1156 * pointing to buffer 1 this time. But sometimes this second interrupt
1157 * doesn't come and the channel hangs. Clearing BUFx_RDY when disabling
1158 * the channel seems to prevent the channel from hanging, but it doesn't
1159 * prevent the spurious interrupt. This might also be unsafe. Think
1160 * about the IDMAC controller trying to switch to a buffer, when we
1161 * clear the ready bit, and re-enable it a moment later.
1163 reg = idmac_read_ipureg(ipu, IPU_CHA_BUF0_RDY);
1164 idmac_write_ipureg(ipu, 0, IPU_CHA_BUF0_RDY);
1165 idmac_write_ipureg(ipu, reg & ~(1UL << channel), IPU_CHA_BUF0_RDY);
1167 reg = idmac_read_ipureg(ipu, IPU_CHA_BUF1_RDY);
1168 idmac_write_ipureg(ipu, 0, IPU_CHA_BUF1_RDY);
1169 idmac_write_ipureg(ipu, reg & ~(1UL << channel), IPU_CHA_BUF1_RDY);
1171 spin_unlock_irqrestore(&ipu->lock, flags);
1176 static struct scatterlist *idmac_sg_next(struct idmac_channel *ichan,
1177 struct idmac_tx_desc **desc, struct scatterlist *sg)
1179 struct scatterlist *sgnew = sg ? sg_next(sg) : NULL;
1182 /* next sg-element in this list */
1185 if ((*desc)->list.next == &ichan->queue)
1186 /* No more descriptors on the queue */
1189 /* Fetch next descriptor */
1190 *desc = list_entry((*desc)->list.next, struct idmac_tx_desc, list);
1195 * We have several possibilities here:
1196 * current BUF next BUF
1198 * not last sg next not last sg
1199 * not last sg next last sg
1200 * last sg first sg from next descriptor
1203 * Besides, the descriptor queue might be empty or not. We process all these
1206 static irqreturn_t idmac_interrupt(int irq, void *dev_id)
1208 struct idmac_channel *ichan = dev_id;
1209 struct device *dev = &ichan->dma_chan.dev->device;
1210 unsigned int chan_id = ichan->dma_chan.chan_id;
1211 struct scatterlist **sg, *sgnext, *sgnew = NULL;
1212 /* Next transfer descriptor */
1213 struct idmac_tx_desc *desc, *descnew;
1214 dma_async_tx_callback callback;
1215 void *callback_param;
1217 u32 ready0, ready1, curbuf, err;
1218 unsigned long flags;
1220 /* IDMAC has cleared the respective BUFx_RDY bit, we manage the buffer */
1222 dev_dbg(dev, "IDMAC irq %d, buf %d\n", irq, ichan->active_buffer);
1224 spin_lock_irqsave(&ipu_data.lock, flags);
1226 ready0 = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF0_RDY);
1227 ready1 = idmac_read_ipureg(&ipu_data, IPU_CHA_BUF1_RDY);
1228 curbuf = idmac_read_ipureg(&ipu_data, IPU_CHA_CUR_BUF);
1229 err = idmac_read_ipureg(&ipu_data, IPU_INT_STAT_4);
1231 if (err & (1 << chan_id)) {
1232 idmac_write_ipureg(&ipu_data, 1 << chan_id, IPU_INT_STAT_4);
1233 spin_unlock_irqrestore(&ipu_data.lock, flags);
1236 * ichan->sg[0] = ichan->sg[1] = NULL;
1237 * you can force channel re-enable on the next tx_submit(), but
1238 * this is dirty - think about descriptors with multiple
1241 dev_warn(dev, "NFB4EOF on channel %d, ready %x, %x, cur %x\n",
1242 chan_id, ready0, ready1, curbuf);
1245 spin_unlock_irqrestore(&ipu_data.lock, flags);
1247 /* Other interrupts do not interfere with this channel */
1248 spin_lock(&ichan->lock);
1249 if (unlikely(chan_id != IDMAC_SDC_0 && chan_id != IDMAC_SDC_1 &&
1250 ((curbuf >> chan_id) & 1) == ichan->active_buffer &&
1251 !list_is_last(ichan->queue.next, &ichan->queue))) {
1254 /* This doesn't help. See comment in ipu_disable_channel() */
1256 curbuf = idmac_read_ipureg(&ipu_data, IPU_CHA_CUR_BUF);
1257 if (((curbuf >> chan_id) & 1) != ichan->active_buffer)
1263 spin_unlock(&ichan->lock);
1265 "IRQ on active buffer on channel %x, active "
1266 "%d, ready %x, %x, current %x!\n", chan_id,
1267 ichan->active_buffer, ready0, ready1, curbuf);
1271 "Buffer deactivated on channel %x, active "
1272 "%d, ready %x, %x, current %x, rest %d!\n", chan_id,
1273 ichan->active_buffer, ready0, ready1, curbuf, i);
1276 if (unlikely((ichan->active_buffer && (ready1 >> chan_id) & 1) ||
1277 (!ichan->active_buffer && (ready0 >> chan_id) & 1)
1279 spin_unlock(&ichan->lock);
1281 "IRQ with active buffer still ready on channel %x, "
1282 "active %d, ready %x, %x!\n", chan_id,
1283 ichan->active_buffer, ready0, ready1);
1287 if (unlikely(list_empty(&ichan->queue))) {
1288 ichan->sg[ichan->active_buffer] = NULL;
1289 spin_unlock(&ichan->lock);
1291 "IRQ without queued buffers on channel %x, active %d, "
1292 "ready %x, %x!\n", chan_id,
1293 ichan->active_buffer, ready0, ready1);
1298 * active_buffer is a software flag, it shows which buffer we are
1299 * currently expecting back from the hardware, IDMAC should be
1300 * processing the other buffer already
1302 sg = &ichan->sg[ichan->active_buffer];
1303 sgnext = ichan->sg[!ichan->active_buffer];
1306 spin_unlock(&ichan->lock);
1310 desc = list_entry(ichan->queue.next, struct idmac_tx_desc, list);
1313 dev_dbg(dev, "IDMAC irq %d, dma 0x%08x, next dma 0x%08x, current %d, curbuf 0x%08x\n",
1314 irq, sg_dma_address(*sg), sgnext ? sg_dma_address(sgnext) : 0, ichan->active_buffer, curbuf);
1316 /* Find the descriptor of sgnext */
1317 sgnew = idmac_sg_next(ichan, &descnew, *sg);
1318 if (sgnext != sgnew)
1319 dev_err(dev, "Submitted buffer %p, next buffer %p\n", sgnext, sgnew);
1322 * if sgnext == NULL sg must be the last element in a scatterlist and
1323 * queue must be empty
1325 if (unlikely(!sgnext)) {
1326 if (!WARN_ON(sg_next(*sg)))
1327 dev_dbg(dev, "Underrun on channel %x\n", chan_id);
1328 ichan->sg[!ichan->active_buffer] = sgnew;
1330 if (unlikely(sgnew)) {
1331 ipu_submit_buffer(ichan, descnew, sgnew, !ichan->active_buffer);
1333 spin_lock_irqsave(&ipu_data.lock, flags);
1334 ipu_ic_disable_task(&ipu_data, chan_id);
1335 spin_unlock_irqrestore(&ipu_data.lock, flags);
1336 ichan->status = IPU_CHANNEL_READY;
1337 /* Continue to check for complete descriptor */
1341 /* Calculate and submit the next sg element */
1342 sgnew = idmac_sg_next(ichan, &descnew, sgnew);
1344 if (unlikely(!sg_next(*sg)) || !sgnext) {
1346 * Last element in scatterlist done, remove from the queue,
1347 * _init for debugging
1349 list_del_init(&desc->list);
1355 if (likely(sgnew) &&
1356 ipu_submit_buffer(ichan, descnew, sgnew, ichan->active_buffer) < 0) {
1357 callback = descnew->txd.callback;
1358 callback_param = descnew->txd.callback_param;
1359 spin_unlock(&ichan->lock);
1361 callback(callback_param);
1362 spin_lock(&ichan->lock);
1365 /* Flip the active buffer - even if update above failed */
1366 ichan->active_buffer = !ichan->active_buffer;
1368 ichan->completed = desc->txd.cookie;
1370 callback = desc->txd.callback;
1371 callback_param = desc->txd.callback_param;
1373 spin_unlock(&ichan->lock);
1375 if (done && (desc->txd.flags & DMA_PREP_INTERRUPT) && callback)
1376 callback(callback_param);
1381 static void ipu_gc_tasklet(unsigned long arg)
1383 struct ipu *ipu = (struct ipu *)arg;
1386 for (i = 0; i < IPU_CHANNELS_NUM; i++) {
1387 struct idmac_channel *ichan = ipu->channel + i;
1388 struct idmac_tx_desc *desc;
1389 unsigned long flags;
1390 struct scatterlist *sg;
1393 for (j = 0; j < ichan->n_tx_desc; j++) {
1394 desc = ichan->desc + j;
1395 spin_lock_irqsave(&ichan->lock, flags);
1396 if (async_tx_test_ack(&desc->txd)) {
1397 list_move(&desc->list, &ichan->free_list);
1398 for_each_sg(desc->sg, sg, desc->sg_len, k) {
1399 if (ichan->sg[0] == sg)
1400 ichan->sg[0] = NULL;
1401 else if (ichan->sg[1] == sg)
1402 ichan->sg[1] = NULL;
1404 async_tx_clear_ack(&desc->txd);
1406 spin_unlock_irqrestore(&ichan->lock, flags);
1411 /* Allocate and initialise a transfer descriptor. */
1412 static struct dma_async_tx_descriptor *idmac_prep_slave_sg(struct dma_chan *chan,
1413 struct scatterlist *sgl, unsigned int sg_len,
1414 enum dma_data_direction direction, unsigned long tx_flags)
1416 struct idmac_channel *ichan = to_idmac_chan(chan);
1417 struct idmac_tx_desc *desc = NULL;
1418 struct dma_async_tx_descriptor *txd = NULL;
1419 unsigned long flags;
1421 /* We only can handle these three channels so far */
1422 if (chan->chan_id != IDMAC_SDC_0 && chan->chan_id != IDMAC_SDC_1 &&
1423 chan->chan_id != IDMAC_IC_7)
1426 if (direction != DMA_FROM_DEVICE && direction != DMA_TO_DEVICE) {
1427 dev_err(chan->device->dev, "Invalid DMA direction %d!\n", direction);
1431 mutex_lock(&ichan->chan_mutex);
1433 spin_lock_irqsave(&ichan->lock, flags);
1434 if (!list_empty(&ichan->free_list)) {
1435 desc = list_entry(ichan->free_list.next,
1436 struct idmac_tx_desc, list);
1438 list_del_init(&desc->list);
1440 desc->sg_len = sg_len;
1443 txd->flags = tx_flags;
1445 spin_unlock_irqrestore(&ichan->lock, flags);
1447 mutex_unlock(&ichan->chan_mutex);
1449 tasklet_schedule(&to_ipu(to_idmac(chan->device))->tasklet);
1454 /* Re-select the current buffer and re-activate the channel */
1455 static void idmac_issue_pending(struct dma_chan *chan)
1457 struct idmac_channel *ichan = to_idmac_chan(chan);
1458 struct idmac *idmac = to_idmac(chan->device);
1459 struct ipu *ipu = to_ipu(idmac);
1460 unsigned long flags;
1462 /* This is not always needed, but doesn't hurt either */
1463 spin_lock_irqsave(&ipu->lock, flags);
1464 ipu_select_buffer(chan->chan_id, ichan->active_buffer);
1465 spin_unlock_irqrestore(&ipu->lock, flags);
1468 * Might need to perform some parts of initialisation from
1469 * ipu_enable_channel(), but not all, we do not want to reset to buffer
1470 * 0, don't need to set priority again either, but re-enabling the task
1471 * and the channel might be a good idea.
1475 static int __idmac_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1478 struct idmac_channel *ichan = to_idmac_chan(chan);
1479 struct idmac *idmac = to_idmac(chan->device);
1480 unsigned long flags;
1483 /* Only supports DMA_TERMINATE_ALL */
1484 if (cmd != DMA_TERMINATE_ALL)
1487 ipu_disable_channel(idmac, ichan,
1488 ichan->status >= IPU_CHANNEL_ENABLED);
1490 tasklet_disable(&to_ipu(idmac)->tasklet);
1492 /* ichan->queue is modified in ISR, have to spinlock */
1493 spin_lock_irqsave(&ichan->lock, flags);
1494 list_splice_init(&ichan->queue, &ichan->free_list);
1497 for (i = 0; i < ichan->n_tx_desc; i++) {
1498 struct idmac_tx_desc *desc = ichan->desc + i;
1499 if (list_empty(&desc->list))
1500 /* Descriptor was prepared, but not submitted */
1501 list_add(&desc->list, &ichan->free_list);
1503 async_tx_clear_ack(&desc->txd);
1506 ichan->sg[0] = NULL;
1507 ichan->sg[1] = NULL;
1508 spin_unlock_irqrestore(&ichan->lock, flags);
1510 tasklet_enable(&to_ipu(idmac)->tasklet);
1512 ichan->status = IPU_CHANNEL_INITIALIZED;
1517 static int idmac_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1520 struct idmac_channel *ichan = to_idmac_chan(chan);
1523 mutex_lock(&ichan->chan_mutex);
1525 ret = __idmac_control(chan, cmd, arg);
1527 mutex_unlock(&ichan->chan_mutex);
1533 static irqreturn_t ic_sof_irq(int irq, void *dev_id)
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);
1538 disable_irq_nosync(irq);
1542 static irqreturn_t ic_eof_irq(int irq, void *dev_id)
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);
1547 disable_irq_nosync(irq);
1551 static int ic_sof = -EINVAL, ic_eof = -EINVAL;
1554 static int idmac_alloc_chan_resources(struct dma_chan *chan)
1556 struct idmac_channel *ichan = to_idmac_chan(chan);
1557 struct idmac *idmac = to_idmac(chan->device);
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);
1565 ichan->completed = -ENXIO;
1567 ret = ipu_irq_map(chan->chan_id);
1571 ichan->eof_irq = ret;
1574 * Important to first disable the channel, because maybe someone
1575 * used it before us, e.g., the bootloader
1577 ipu_disable_channel(idmac, ichan, true);
1579 ret = ipu_init_channel(idmac, ichan);
1583 ret = request_irq(ichan->eof_irq, idmac_interrupt, 0,
1584 ichan->eof_name, ichan);
1589 if (chan->chan_id == IDMAC_IC_7) {
1590 ic_sof = ipu_irq_map(69);
1592 request_irq(ic_sof, ic_sof_irq, 0, "IC SOF", ichan);
1593 ic_eof = ipu_irq_map(70);
1595 request_irq(ic_eof, ic_eof_irq, 0, "IC EOF", ichan);
1599 ichan->status = IPU_CHANNEL_INITIALIZED;
1601 dev_dbg(&chan->dev->device, "Found channel 0x%x, irq %d\n",
1602 chan->chan_id, ichan->eof_irq);
1607 ipu_uninit_channel(idmac, ichan);
1609 ipu_irq_unmap(chan->chan_id);
1614 static void idmac_free_chan_resources(struct dma_chan *chan)
1616 struct idmac_channel *ichan = to_idmac_chan(chan);
1617 struct idmac *idmac = to_idmac(chan->device);
1619 mutex_lock(&ichan->chan_mutex);
1621 __idmac_control(chan, DMA_TERMINATE_ALL, 0);
1623 if (ichan->status > IPU_CHANNEL_FREE) {
1625 if (chan->chan_id == IDMAC_IC_7) {
1627 free_irq(ic_sof, ichan);
1632 free_irq(ic_eof, ichan);
1638 free_irq(ichan->eof_irq, ichan);
1639 ipu_irq_unmap(chan->chan_id);
1642 ichan->status = IPU_CHANNEL_FREE;
1644 ipu_uninit_channel(idmac, ichan);
1646 mutex_unlock(&ichan->chan_mutex);
1648 tasklet_schedule(&to_ipu(idmac)->tasklet);
1651 static enum dma_status idmac_tx_status(struct dma_chan *chan,
1652 dma_cookie_t cookie, struct dma_tx_state *txstate)
1654 struct idmac_channel *ichan = to_idmac_chan(chan);
1656 dma_set_tx_state(txstate, ichan->completed, chan->cookie, 0);
1657 if (cookie != chan->cookie)
1662 static int __init ipu_idmac_init(struct ipu *ipu)
1664 struct idmac *idmac = &ipu->idmac;
1665 struct dma_device *dma = &idmac->dma;
1668 dma_cap_set(DMA_SLAVE, dma->cap_mask);
1669 dma_cap_set(DMA_PRIVATE, dma->cap_mask);
1671 /* Compulsory common fields */
1672 dma->dev = ipu->dev;
1673 dma->device_alloc_chan_resources = idmac_alloc_chan_resources;
1674 dma->device_free_chan_resources = idmac_free_chan_resources;
1675 dma->device_tx_status = idmac_tx_status;
1676 dma->device_issue_pending = idmac_issue_pending;
1678 /* Compulsory for DMA_SLAVE fields */
1679 dma->device_prep_slave_sg = idmac_prep_slave_sg;
1680 dma->device_control = idmac_control;
1682 INIT_LIST_HEAD(&dma->channels);
1683 for (i = 0; i < IPU_CHANNELS_NUM; i++) {
1684 struct idmac_channel *ichan = ipu->channel + i;
1685 struct dma_chan *dma_chan = &ichan->dma_chan;
1687 spin_lock_init(&ichan->lock);
1688 mutex_init(&ichan->chan_mutex);
1690 ichan->status = IPU_CHANNEL_FREE;
1691 ichan->sec_chan_en = false;
1692 ichan->completed = -ENXIO;
1693 snprintf(ichan->eof_name, sizeof(ichan->eof_name), "IDMAC EOF %d", i);
1695 dma_chan->device = &idmac->dma;
1696 dma_chan->cookie = 1;
1697 dma_chan->chan_id = i;
1698 list_add_tail(&dma_chan->device_node, &dma->channels);
1701 idmac_write_icreg(ipu, 0x00000070, IDMAC_CONF);
1703 return dma_async_device_register(&idmac->dma);
1706 static void __exit ipu_idmac_exit(struct ipu *ipu)
1709 struct idmac *idmac = &ipu->idmac;
1711 for (i = 0; i < IPU_CHANNELS_NUM; i++) {
1712 struct idmac_channel *ichan = ipu->channel + i;
1714 idmac_control(&ichan->dma_chan, DMA_TERMINATE_ALL, 0);
1715 idmac_prep_slave_sg(&ichan->dma_chan, NULL, 0, DMA_NONE, 0);
1718 dma_async_device_unregister(&idmac->dma);
1721 /*****************************************************************************
1722 * IPU common probe / remove
1725 static int __init ipu_probe(struct platform_device *pdev)
1727 struct ipu_platform_data *pdata = pdev->dev.platform_data;
1728 struct resource *mem_ipu, *mem_ic;
1731 spin_lock_init(&ipu_data.lock);
1733 mem_ipu = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1734 mem_ic = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1735 if (!pdata || !mem_ipu || !mem_ic)
1738 ipu_data.dev = &pdev->dev;
1740 platform_set_drvdata(pdev, &ipu_data);
1742 ret = platform_get_irq(pdev, 0);
1746 ipu_data.irq_fn = ret;
1747 ret = platform_get_irq(pdev, 1);
1751 ipu_data.irq_err = ret;
1752 ipu_data.irq_base = pdata->irq_base;
1754 dev_dbg(&pdev->dev, "fn irq %u, err irq %u, irq-base %u\n",
1755 ipu_data.irq_fn, ipu_data.irq_err, ipu_data.irq_base);
1757 /* Remap IPU common registers */
1758 ipu_data.reg_ipu = ioremap(mem_ipu->start,
1759 mem_ipu->end - mem_ipu->start + 1);
1760 if (!ipu_data.reg_ipu) {
1762 goto err_ioremap_ipu;
1765 /* Remap Image Converter and Image DMA Controller registers */
1766 ipu_data.reg_ic = ioremap(mem_ic->start,
1767 mem_ic->end - mem_ic->start + 1);
1768 if (!ipu_data.reg_ic) {
1770 goto err_ioremap_ic;
1774 ipu_data.ipu_clk = clk_get(&pdev->dev, NULL);
1775 if (IS_ERR(ipu_data.ipu_clk)) {
1776 ret = PTR_ERR(ipu_data.ipu_clk);
1780 /* Make sure IPU HSP clock is running */
1781 clk_enable(ipu_data.ipu_clk);
1783 /* Disable all interrupts */
1784 idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_1);
1785 idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_2);
1786 idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_3);
1787 idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_4);
1788 idmac_write_ipureg(&ipu_data, 0, IPU_INT_CTRL_5);
1790 dev_dbg(&pdev->dev, "%s @ 0x%08lx, fn irq %u, err irq %u\n", pdev->name,
1791 (unsigned long)mem_ipu->start, ipu_data.irq_fn, ipu_data.irq_err);
1793 ret = ipu_irq_attach_irq(&ipu_data, pdev);
1795 goto err_attach_irq;
1797 /* Initialize DMA engine */
1798 ret = ipu_idmac_init(&ipu_data);
1800 goto err_idmac_init;
1802 tasklet_init(&ipu_data.tasklet, ipu_gc_tasklet, (unsigned long)&ipu_data);
1804 ipu_data.dev = &pdev->dev;
1806 dev_dbg(ipu_data.dev, "IPU initialized\n");
1812 ipu_irq_detach_irq(&ipu_data, pdev);
1813 clk_disable(ipu_data.ipu_clk);
1814 clk_put(ipu_data.ipu_clk);
1816 iounmap(ipu_data.reg_ic);
1818 iounmap(ipu_data.reg_ipu);
1821 dev_err(&pdev->dev, "Failed to probe IPU: %d\n", ret);
1825 static int __exit ipu_remove(struct platform_device *pdev)
1827 struct ipu *ipu = platform_get_drvdata(pdev);
1829 ipu_idmac_exit(ipu);
1830 ipu_irq_detach_irq(ipu, pdev);
1831 clk_disable(ipu->ipu_clk);
1832 clk_put(ipu->ipu_clk);
1833 iounmap(ipu->reg_ic);
1834 iounmap(ipu->reg_ipu);
1835 tasklet_kill(&ipu->tasklet);
1836 platform_set_drvdata(pdev, NULL);
1842 * We need two MEM resources - with IPU-common and Image Converter registers,
1843 * including PF_CONF and IDMAC_* registers, and two IRQs - function and error
1845 static struct platform_driver ipu_platform_driver = {
1848 .owner = THIS_MODULE,
1850 .remove = __exit_p(ipu_remove),
1853 static int __init ipu_init(void)
1855 return platform_driver_probe(&ipu_platform_driver, ipu_probe);
1857 subsys_initcall(ipu_init);
1859 MODULE_DESCRIPTION("IPU core driver");
1860 MODULE_LICENSE("GPL v2");
1861 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
1862 MODULE_ALIAS("platform:ipu-core");