blob: 180f5235acc237698d7ed94e4fe1eae89e1cfbff [file] [log] [blame]
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001/*
2 * drivers/dma/imx-dma.c
3 *
4 * This file contains a driver for the Freescale i.MX DMA engine
5 * found on i.MX1/21/27
6 *
7 * Copyright 2010 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
Javier Martin9e15db72012-03-02 09:28:47 +01008 * Copyright 2012 Javier Martin, Vista Silicon <javier.martin@vista-silicon.com>
Sascha Hauer1f1846c2010-10-06 10:25:55 +02009 *
10 * The code contained herein is licensed under the GNU General Public
11 * License. You may obtain a copy of the GNU General Public License
12 * Version 2 or later at the following locations:
13 *
14 * http://www.opensource.org/licenses/gpl-license.html
15 * http://www.gnu.org/copyleft/gpl.html
16 */
17#include <linux/init.h>
18#include <linux/types.h>
19#include <linux/mm.h>
20#include <linux/interrupt.h>
21#include <linux/spinlock.h>
22#include <linux/device.h>
23#include <linux/dma-mapping.h>
24#include <linux/slab.h>
25#include <linux/platform_device.h>
Javier Martin6bd08122012-03-22 14:54:01 +010026#include <linux/clk.h>
Sascha Hauer1f1846c2010-10-06 10:25:55 +020027#include <linux/dmaengine.h>
Paul Gortmaker5c45ad72011-07-31 16:14:17 -040028#include <linux/module.h>
Sascha Hauer1f1846c2010-10-06 10:25:55 +020029
30#include <asm/irq.h>
Javier Martin6bd08122012-03-22 14:54:01 +010031#include <mach/dma.h>
Sascha Hauer1f1846c2010-10-06 10:25:55 +020032#include <mach/hardware.h>
33
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000034#include "dmaengine.h"
Javier Martin9e15db72012-03-02 09:28:47 +010035#define IMXDMA_MAX_CHAN_DESCRIPTORS 16
Javier Martin6bd08122012-03-22 14:54:01 +010036#define IMX_DMA_CHANNELS 16
37
Javier Martinf606ab82012-03-22 14:54:14 +010038#define IMX_DMA_2D_SLOTS 2
39#define IMX_DMA_2D_SLOT_A 0
40#define IMX_DMA_2D_SLOT_B 1
41
Javier Martin6bd08122012-03-22 14:54:01 +010042#define IMX_DMA_LENGTH_LOOP ((unsigned int)-1)
43#define IMX_DMA_MEMSIZE_32 (0 << 4)
44#define IMX_DMA_MEMSIZE_8 (1 << 4)
45#define IMX_DMA_MEMSIZE_16 (2 << 4)
46#define IMX_DMA_TYPE_LINEAR (0 << 10)
47#define IMX_DMA_TYPE_2D (1 << 10)
48#define IMX_DMA_TYPE_FIFO (2 << 10)
49
50#define IMX_DMA_ERR_BURST (1 << 0)
51#define IMX_DMA_ERR_REQUEST (1 << 1)
52#define IMX_DMA_ERR_TRANSFER (1 << 2)
53#define IMX_DMA_ERR_BUFFER (1 << 3)
54#define IMX_DMA_ERR_TIMEOUT (1 << 4)
55
56#define DMA_DCR 0x00 /* Control Register */
57#define DMA_DISR 0x04 /* Interrupt status Register */
58#define DMA_DIMR 0x08 /* Interrupt mask Register */
59#define DMA_DBTOSR 0x0c /* Burst timeout status Register */
60#define DMA_DRTOSR 0x10 /* Request timeout Register */
61#define DMA_DSESR 0x14 /* Transfer Error Status Register */
62#define DMA_DBOSR 0x18 /* Buffer overflow status Register */
63#define DMA_DBTOCR 0x1c /* Burst timeout control Register */
64#define DMA_WSRA 0x40 /* W-Size Register A */
65#define DMA_XSRA 0x44 /* X-Size Register A */
66#define DMA_YSRA 0x48 /* Y-Size Register A */
67#define DMA_WSRB 0x4c /* W-Size Register B */
68#define DMA_XSRB 0x50 /* X-Size Register B */
69#define DMA_YSRB 0x54 /* Y-Size Register B */
70#define DMA_SAR(x) (0x80 + ((x) << 6)) /* Source Address Registers */
71#define DMA_DAR(x) (0x84 + ((x) << 6)) /* Destination Address Registers */
72#define DMA_CNTR(x) (0x88 + ((x) << 6)) /* Count Registers */
73#define DMA_CCR(x) (0x8c + ((x) << 6)) /* Control Registers */
74#define DMA_RSSR(x) (0x90 + ((x) << 6)) /* Request source select Registers */
75#define DMA_BLR(x) (0x94 + ((x) << 6)) /* Burst length Registers */
76#define DMA_RTOR(x) (0x98 + ((x) << 6)) /* Request timeout Registers */
77#define DMA_BUCR(x) (0x98 + ((x) << 6)) /* Bus Utilization Registers */
78#define DMA_CCNR(x) (0x9C + ((x) << 6)) /* Channel counter Registers */
79
80#define DCR_DRST (1<<1)
81#define DCR_DEN (1<<0)
82#define DBTOCR_EN (1<<15)
83#define DBTOCR_CNT(x) ((x) & 0x7fff)
84#define CNTR_CNT(x) ((x) & 0xffffff)
85#define CCR_ACRPT (1<<14)
86#define CCR_DMOD_LINEAR (0x0 << 12)
87#define CCR_DMOD_2D (0x1 << 12)
88#define CCR_DMOD_FIFO (0x2 << 12)
89#define CCR_DMOD_EOBFIFO (0x3 << 12)
90#define CCR_SMOD_LINEAR (0x0 << 10)
91#define CCR_SMOD_2D (0x1 << 10)
92#define CCR_SMOD_FIFO (0x2 << 10)
93#define CCR_SMOD_EOBFIFO (0x3 << 10)
94#define CCR_MDIR_DEC (1<<9)
95#define CCR_MSEL_B (1<<8)
96#define CCR_DSIZ_32 (0x0 << 6)
97#define CCR_DSIZ_8 (0x1 << 6)
98#define CCR_DSIZ_16 (0x2 << 6)
99#define CCR_SSIZ_32 (0x0 << 4)
100#define CCR_SSIZ_8 (0x1 << 4)
101#define CCR_SSIZ_16 (0x2 << 4)
102#define CCR_REN (1<<3)
103#define CCR_RPT (1<<2)
104#define CCR_FRC (1<<1)
105#define CCR_CEN (1<<0)
106#define RTOR_EN (1<<15)
107#define RTOR_CLK (1<<14)
108#define RTOR_PSC (1<<13)
Javier Martin9e15db72012-03-02 09:28:47 +0100109
110enum imxdma_prep_type {
111 IMXDMA_DESC_MEMCPY,
112 IMXDMA_DESC_INTERLEAVED,
113 IMXDMA_DESC_SLAVE_SG,
114 IMXDMA_DESC_CYCLIC,
115};
116
Javier Martinf606ab82012-03-22 14:54:14 +0100117struct imx_dma_2d_config {
118 u16 xsr;
119 u16 ysr;
120 u16 wsr;
121 int count;
122};
123
Javier Martin9e15db72012-03-02 09:28:47 +0100124struct imxdma_desc {
125 struct list_head node;
126 struct dma_async_tx_descriptor desc;
127 enum dma_status status;
128 dma_addr_t src;
129 dma_addr_t dest;
130 size_t len;
Javier Martin2efc3442012-03-22 14:54:03 +0100131 enum dma_transfer_direction direction;
Javier Martin9e15db72012-03-02 09:28:47 +0100132 enum imxdma_prep_type type;
133 /* For memcpy and interleaved */
134 unsigned int config_port;
135 unsigned int config_mem;
136 /* For interleaved transfers */
137 unsigned int x;
138 unsigned int y;
139 unsigned int w;
140 /* For slave sg and cyclic */
141 struct scatterlist *sg;
142 unsigned int sgcount;
143};
144
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200145struct imxdma_channel {
Javier Martin2d9c2fc2012-03-22 14:54:10 +0100146 int hw_chaining;
147 struct timer_list watchdog;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200148 struct imxdma_engine *imxdma;
149 unsigned int channel;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200150
Javier Martin9e15db72012-03-02 09:28:47 +0100151 struct tasklet_struct dma_tasklet;
152 struct list_head ld_free;
153 struct list_head ld_queue;
154 struct list_head ld_active;
155 int descs_allocated;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200156 enum dma_slave_buswidth word_size;
157 dma_addr_t per_address;
158 u32 watermark_level;
159 struct dma_chan chan;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200160 struct dma_async_tx_descriptor desc;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200161 enum dma_status status;
162 int dma_request;
163 struct scatterlist *sg_list;
Javier Martin359291a2012-03-22 14:54:06 +0100164 u32 ccr_from_device;
165 u32 ccr_to_device;
Javier Martinf606ab82012-03-22 14:54:14 +0100166 bool enabled_2d;
167 int slot_2d;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200168};
169
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200170struct imxdma_engine {
171 struct device *dev;
Sascha Hauer1e070a62011-01-12 13:14:37 +0100172 struct device_dma_parameters dma_parms;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200173 struct dma_device dma_device;
Javier Martincd5cf9d2012-03-22 14:54:12 +0100174 void __iomem *base;
Fabio Estevam449daa02012-07-03 15:33:29 -0300175 struct clk *dma_clk;
Javier Martinf606ab82012-03-22 14:54:14 +0100176 spinlock_t lock;
177 struct imx_dma_2d_config slots_2d[IMX_DMA_2D_SLOTS];
Javier Martin6bd08122012-03-22 14:54:01 +0100178 struct imxdma_channel channel[IMX_DMA_CHANNELS];
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200179};
180
181static struct imxdma_channel *to_imxdma_chan(struct dma_chan *chan)
182{
183 return container_of(chan, struct imxdma_channel, chan);
184}
185
Javier Martin9e15db72012-03-02 09:28:47 +0100186static inline bool imxdma_chan_is_doing_cyclic(struct imxdma_channel *imxdmac)
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200187{
Javier Martin9e15db72012-03-02 09:28:47 +0100188 struct imxdma_desc *desc;
189
190 if (!list_empty(&imxdmac->ld_active)) {
191 desc = list_first_entry(&imxdmac->ld_active, struct imxdma_desc,
192 node);
193 if (desc->type == IMXDMA_DESC_CYCLIC)
194 return true;
195 }
196 return false;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200197}
198
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200199
Javier Martincd5cf9d2012-03-22 14:54:12 +0100200
201static void imx_dmav1_writel(struct imxdma_engine *imxdma, unsigned val,
202 unsigned offset)
Javier Martin6bd08122012-03-22 14:54:01 +0100203{
Javier Martincd5cf9d2012-03-22 14:54:12 +0100204 __raw_writel(val, imxdma->base + offset);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200205}
206
Javier Martincd5cf9d2012-03-22 14:54:12 +0100207static unsigned imx_dmav1_readl(struct imxdma_engine *imxdma, unsigned offset)
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200208{
Javier Martincd5cf9d2012-03-22 14:54:12 +0100209 return __raw_readl(imxdma->base + offset);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200210}
211
Javier Martin2d9c2fc2012-03-22 14:54:10 +0100212static int imxdma_hw_chain(struct imxdma_channel *imxdmac)
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200213{
Javier Martin6bd08122012-03-22 14:54:01 +0100214 if (cpu_is_mx27())
Javier Martin2d9c2fc2012-03-22 14:54:10 +0100215 return imxdmac->hw_chaining;
Javier Martin6bd08122012-03-22 14:54:01 +0100216 else
217 return 0;
218}
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200219
Javier Martin6bd08122012-03-22 14:54:01 +0100220/*
221 * imxdma_sg_next - prepare next chunk for scatter-gather DMA emulation
222 */
Javier Martina6cbb2d2012-03-22 14:54:11 +0100223static inline int imxdma_sg_next(struct imxdma_desc *d)
Javier Martin6bd08122012-03-22 14:54:01 +0100224{
Javier Martin2efc3442012-03-22 14:54:03 +0100225 struct imxdma_channel *imxdmac = to_imxdma_chan(d->desc.chan);
Javier Martincd5cf9d2012-03-22 14:54:12 +0100226 struct imxdma_engine *imxdma = imxdmac->imxdma;
Javier Martina6cbb2d2012-03-22 14:54:11 +0100227 struct scatterlist *sg = d->sg;
Javier Martin6bd08122012-03-22 14:54:01 +0100228 unsigned long now;
229
Javier Martin6b0e2f52012-03-22 14:54:09 +0100230 now = min(d->len, sg->length);
231 if (d->len != IMX_DMA_LENGTH_LOOP)
232 d->len -= now;
Javier Martin6bd08122012-03-22 14:54:01 +0100233
Javier Martin2efc3442012-03-22 14:54:03 +0100234 if (d->direction == DMA_DEV_TO_MEM)
Javier Martincd5cf9d2012-03-22 14:54:12 +0100235 imx_dmav1_writel(imxdma, sg->dma_address,
236 DMA_DAR(imxdmac->channel));
Javier Martin6bd08122012-03-22 14:54:01 +0100237 else
Javier Martincd5cf9d2012-03-22 14:54:12 +0100238 imx_dmav1_writel(imxdma, sg->dma_address,
239 DMA_SAR(imxdmac->channel));
Javier Martin6bd08122012-03-22 14:54:01 +0100240
Javier Martincd5cf9d2012-03-22 14:54:12 +0100241 imx_dmav1_writel(imxdma, now, DMA_CNTR(imxdmac->channel));
Javier Martin6bd08122012-03-22 14:54:01 +0100242
Javier Martinf9b283a2012-03-22 14:54:13 +0100243 dev_dbg(imxdma->dev, " %s channel: %d dst 0x%08x, src 0x%08x, "
244 "size 0x%08x\n", __func__, imxdmac->channel,
Javier Martincd5cf9d2012-03-22 14:54:12 +0100245 imx_dmav1_readl(imxdma, DMA_DAR(imxdmac->channel)),
246 imx_dmav1_readl(imxdma, DMA_SAR(imxdmac->channel)),
247 imx_dmav1_readl(imxdma, DMA_CNTR(imxdmac->channel)));
Javier Martin6bd08122012-03-22 14:54:01 +0100248
249 return now;
250}
251
Javier Martin2efc3442012-03-22 14:54:03 +0100252static void imxdma_enable_hw(struct imxdma_desc *d)
Javier Martin6bd08122012-03-22 14:54:01 +0100253{
Javier Martin2efc3442012-03-22 14:54:03 +0100254 struct imxdma_channel *imxdmac = to_imxdma_chan(d->desc.chan);
Javier Martincd5cf9d2012-03-22 14:54:12 +0100255 struct imxdma_engine *imxdma = imxdmac->imxdma;
Javier Martin6bd08122012-03-22 14:54:01 +0100256 int channel = imxdmac->channel;
257 unsigned long flags;
258
Javier Martinf9b283a2012-03-22 14:54:13 +0100259 dev_dbg(imxdma->dev, "%s channel %d\n", __func__, channel);
Javier Martin6bd08122012-03-22 14:54:01 +0100260
Javier Martin6bd08122012-03-22 14:54:01 +0100261 local_irq_save(flags);
262
Javier Martincd5cf9d2012-03-22 14:54:12 +0100263 imx_dmav1_writel(imxdma, 1 << channel, DMA_DISR);
264 imx_dmav1_writel(imxdma, imx_dmav1_readl(imxdma, DMA_DIMR) &
265 ~(1 << channel), DMA_DIMR);
266 imx_dmav1_writel(imxdma, imx_dmav1_readl(imxdma, DMA_CCR(channel)) |
267 CCR_CEN | CCR_ACRPT, DMA_CCR(channel));
Javier Martin6bd08122012-03-22 14:54:01 +0100268
269 if ((cpu_is_mx21() || cpu_is_mx27()) &&
Javier Martin2d9c2fc2012-03-22 14:54:10 +0100270 d->sg && imxdma_hw_chain(imxdmac)) {
Javier Martin833bc032012-03-22 14:54:07 +0100271 d->sg = sg_next(d->sg);
272 if (d->sg) {
Javier Martin6bd08122012-03-22 14:54:01 +0100273 u32 tmp;
Javier Martina6cbb2d2012-03-22 14:54:11 +0100274 imxdma_sg_next(d);
Javier Martincd5cf9d2012-03-22 14:54:12 +0100275 tmp = imx_dmav1_readl(imxdma, DMA_CCR(channel));
276 imx_dmav1_writel(imxdma, tmp | CCR_RPT | CCR_ACRPT,
277 DMA_CCR(channel));
Javier Martin6bd08122012-03-22 14:54:01 +0100278 }
279 }
Javier Martin6bd08122012-03-22 14:54:01 +0100280
281 local_irq_restore(flags);
282}
283
284static void imxdma_disable_hw(struct imxdma_channel *imxdmac)
285{
Javier Martincd5cf9d2012-03-22 14:54:12 +0100286 struct imxdma_engine *imxdma = imxdmac->imxdma;
Javier Martin6bd08122012-03-22 14:54:01 +0100287 int channel = imxdmac->channel;
288 unsigned long flags;
289
Javier Martinf9b283a2012-03-22 14:54:13 +0100290 dev_dbg(imxdma->dev, "%s channel %d\n", __func__, channel);
Javier Martin6bd08122012-03-22 14:54:01 +0100291
Javier Martin2d9c2fc2012-03-22 14:54:10 +0100292 if (imxdma_hw_chain(imxdmac))
293 del_timer(&imxdmac->watchdog);
Javier Martin6bd08122012-03-22 14:54:01 +0100294
295 local_irq_save(flags);
Javier Martincd5cf9d2012-03-22 14:54:12 +0100296 imx_dmav1_writel(imxdma, imx_dmav1_readl(imxdma, DMA_DIMR) |
297 (1 << channel), DMA_DIMR);
298 imx_dmav1_writel(imxdma, imx_dmav1_readl(imxdma, DMA_CCR(channel)) &
299 ~CCR_CEN, DMA_CCR(channel));
300 imx_dmav1_writel(imxdma, 1 << channel, DMA_DISR);
Javier Martin6bd08122012-03-22 14:54:01 +0100301 local_irq_restore(flags);
302}
303
Javier Martin6bd08122012-03-22 14:54:01 +0100304static void imxdma_watchdog(unsigned long data)
305{
306 struct imxdma_channel *imxdmac = (struct imxdma_channel *)data;
Javier Martincd5cf9d2012-03-22 14:54:12 +0100307 struct imxdma_engine *imxdma = imxdmac->imxdma;
Javier Martin6bd08122012-03-22 14:54:01 +0100308 int channel = imxdmac->channel;
309
Javier Martincd5cf9d2012-03-22 14:54:12 +0100310 imx_dmav1_writel(imxdma, 0, DMA_CCR(channel));
Javier Martin6bd08122012-03-22 14:54:01 +0100311
312 /* Tasklet watchdog error handler */
313 tasklet_schedule(&imxdmac->dma_tasklet);
Javier Martinf9b283a2012-03-22 14:54:13 +0100314 dev_dbg(imxdma->dev, "channel %d: watchdog timeout!\n",
315 imxdmac->channel);
Javier Martin6bd08122012-03-22 14:54:01 +0100316}
317
318static irqreturn_t imxdma_err_handler(int irq, void *dev_id)
319{
320 struct imxdma_engine *imxdma = dev_id;
Javier Martin6bd08122012-03-22 14:54:01 +0100321 unsigned int err_mask;
322 int i, disr;
323 int errcode;
324
Javier Martincd5cf9d2012-03-22 14:54:12 +0100325 disr = imx_dmav1_readl(imxdma, DMA_DISR);
Javier Martin6bd08122012-03-22 14:54:01 +0100326
Javier Martincd5cf9d2012-03-22 14:54:12 +0100327 err_mask = imx_dmav1_readl(imxdma, DMA_DBTOSR) |
328 imx_dmav1_readl(imxdma, DMA_DRTOSR) |
329 imx_dmav1_readl(imxdma, DMA_DSESR) |
330 imx_dmav1_readl(imxdma, DMA_DBOSR);
Javier Martin6bd08122012-03-22 14:54:01 +0100331
332 if (!err_mask)
333 return IRQ_HANDLED;
334
Javier Martincd5cf9d2012-03-22 14:54:12 +0100335 imx_dmav1_writel(imxdma, disr & err_mask, DMA_DISR);
Javier Martin6bd08122012-03-22 14:54:01 +0100336
337 for (i = 0; i < IMX_DMA_CHANNELS; i++) {
338 if (!(err_mask & (1 << i)))
339 continue;
Javier Martin6bd08122012-03-22 14:54:01 +0100340 errcode = 0;
341
Javier Martincd5cf9d2012-03-22 14:54:12 +0100342 if (imx_dmav1_readl(imxdma, DMA_DBTOSR) & (1 << i)) {
343 imx_dmav1_writel(imxdma, 1 << i, DMA_DBTOSR);
Javier Martin6bd08122012-03-22 14:54:01 +0100344 errcode |= IMX_DMA_ERR_BURST;
345 }
Javier Martincd5cf9d2012-03-22 14:54:12 +0100346 if (imx_dmav1_readl(imxdma, DMA_DRTOSR) & (1 << i)) {
347 imx_dmav1_writel(imxdma, 1 << i, DMA_DRTOSR);
Javier Martin6bd08122012-03-22 14:54:01 +0100348 errcode |= IMX_DMA_ERR_REQUEST;
349 }
Javier Martincd5cf9d2012-03-22 14:54:12 +0100350 if (imx_dmav1_readl(imxdma, DMA_DSESR) & (1 << i)) {
351 imx_dmav1_writel(imxdma, 1 << i, DMA_DSESR);
Javier Martin6bd08122012-03-22 14:54:01 +0100352 errcode |= IMX_DMA_ERR_TRANSFER;
353 }
Javier Martincd5cf9d2012-03-22 14:54:12 +0100354 if (imx_dmav1_readl(imxdma, DMA_DBOSR) & (1 << i)) {
355 imx_dmav1_writel(imxdma, 1 << i, DMA_DBOSR);
Javier Martin6bd08122012-03-22 14:54:01 +0100356 errcode |= IMX_DMA_ERR_BUFFER;
357 }
358 /* Tasklet error handler */
359 tasklet_schedule(&imxdma->channel[i].dma_tasklet);
360
361 printk(KERN_WARNING
362 "DMA timeout on channel %d -%s%s%s%s\n", i,
363 errcode & IMX_DMA_ERR_BURST ? " burst" : "",
364 errcode & IMX_DMA_ERR_REQUEST ? " request" : "",
365 errcode & IMX_DMA_ERR_TRANSFER ? " transfer" : "",
366 errcode & IMX_DMA_ERR_BUFFER ? " buffer" : "");
367 }
368 return IRQ_HANDLED;
369}
370
371static void dma_irq_handle_channel(struct imxdma_channel *imxdmac)
372{
Javier Martincd5cf9d2012-03-22 14:54:12 +0100373 struct imxdma_engine *imxdma = imxdmac->imxdma;
Javier Martin6bd08122012-03-22 14:54:01 +0100374 int chno = imxdmac->channel;
Javier Martin2efc3442012-03-22 14:54:03 +0100375 struct imxdma_desc *desc;
Javier Martin6bd08122012-03-22 14:54:01 +0100376
Javier Martinf606ab82012-03-22 14:54:14 +0100377 spin_lock(&imxdma->lock);
Javier Martin833bc032012-03-22 14:54:07 +0100378 if (list_empty(&imxdmac->ld_active)) {
Javier Martinf606ab82012-03-22 14:54:14 +0100379 spin_unlock(&imxdma->lock);
Javier Martin833bc032012-03-22 14:54:07 +0100380 goto out;
381 }
382
383 desc = list_first_entry(&imxdmac->ld_active,
384 struct imxdma_desc,
385 node);
Javier Martinf606ab82012-03-22 14:54:14 +0100386 spin_unlock(&imxdma->lock);
Javier Martin833bc032012-03-22 14:54:07 +0100387
388 if (desc->sg) {
Javier Martin6bd08122012-03-22 14:54:01 +0100389 u32 tmp;
Javier Martin833bc032012-03-22 14:54:07 +0100390 desc->sg = sg_next(desc->sg);
Javier Martin6bd08122012-03-22 14:54:01 +0100391
Javier Martin833bc032012-03-22 14:54:07 +0100392 if (desc->sg) {
Javier Martina6cbb2d2012-03-22 14:54:11 +0100393 imxdma_sg_next(desc);
Javier Martin6bd08122012-03-22 14:54:01 +0100394
Javier Martincd5cf9d2012-03-22 14:54:12 +0100395 tmp = imx_dmav1_readl(imxdma, DMA_CCR(chno));
Javier Martin6bd08122012-03-22 14:54:01 +0100396
Javier Martin2d9c2fc2012-03-22 14:54:10 +0100397 if (imxdma_hw_chain(imxdmac)) {
Javier Martin6bd08122012-03-22 14:54:01 +0100398 /* FIXME: The timeout should probably be
399 * configurable
400 */
Javier Martin2d9c2fc2012-03-22 14:54:10 +0100401 mod_timer(&imxdmac->watchdog,
Javier Martin6bd08122012-03-22 14:54:01 +0100402 jiffies + msecs_to_jiffies(500));
403
404 tmp |= CCR_CEN | CCR_RPT | CCR_ACRPT;
Javier Martincd5cf9d2012-03-22 14:54:12 +0100405 imx_dmav1_writel(imxdma, tmp, DMA_CCR(chno));
Javier Martin6bd08122012-03-22 14:54:01 +0100406 } else {
Javier Martincd5cf9d2012-03-22 14:54:12 +0100407 imx_dmav1_writel(imxdma, tmp & ~CCR_CEN,
408 DMA_CCR(chno));
Javier Martin6bd08122012-03-22 14:54:01 +0100409 tmp |= CCR_CEN;
410 }
411
Javier Martincd5cf9d2012-03-22 14:54:12 +0100412 imx_dmav1_writel(imxdma, tmp, DMA_CCR(chno));
Javier Martin6bd08122012-03-22 14:54:01 +0100413
414 if (imxdma_chan_is_doing_cyclic(imxdmac))
415 /* Tasklet progression */
416 tasklet_schedule(&imxdmac->dma_tasklet);
417
418 return;
419 }
420
Javier Martin2d9c2fc2012-03-22 14:54:10 +0100421 if (imxdma_hw_chain(imxdmac)) {
422 del_timer(&imxdmac->watchdog);
Javier Martin6bd08122012-03-22 14:54:01 +0100423 return;
424 }
425 }
426
Javier Martin2efc3442012-03-22 14:54:03 +0100427out:
Javier Martincd5cf9d2012-03-22 14:54:12 +0100428 imx_dmav1_writel(imxdma, 0, DMA_CCR(chno));
Javier Martin6bd08122012-03-22 14:54:01 +0100429 /* Tasklet irq */
Javier Martin9e15db72012-03-02 09:28:47 +0100430 tasklet_schedule(&imxdmac->dma_tasklet);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200431}
432
Javier Martin6bd08122012-03-22 14:54:01 +0100433static irqreturn_t dma_irq_handler(int irq, void *dev_id)
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200434{
Javier Martin6bd08122012-03-22 14:54:01 +0100435 struct imxdma_engine *imxdma = dev_id;
Javier Martin6bd08122012-03-22 14:54:01 +0100436 int i, disr;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200437
Javier Martin6bd08122012-03-22 14:54:01 +0100438 if (cpu_is_mx21() || cpu_is_mx27())
439 imxdma_err_handler(irq, dev_id);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200440
Javier Martincd5cf9d2012-03-22 14:54:12 +0100441 disr = imx_dmav1_readl(imxdma, DMA_DISR);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200442
Javier Martinf9b283a2012-03-22 14:54:13 +0100443 dev_dbg(imxdma->dev, "%s called, disr=0x%08x\n", __func__, disr);
Javier Martin6bd08122012-03-22 14:54:01 +0100444
Javier Martincd5cf9d2012-03-22 14:54:12 +0100445 imx_dmav1_writel(imxdma, disr, DMA_DISR);
Javier Martin6bd08122012-03-22 14:54:01 +0100446 for (i = 0; i < IMX_DMA_CHANNELS; i++) {
Javier Martin2d9c2fc2012-03-22 14:54:10 +0100447 if (disr & (1 << i))
Javier Martin6bd08122012-03-22 14:54:01 +0100448 dma_irq_handle_channel(&imxdma->channel[i]);
Javier Martin6bd08122012-03-22 14:54:01 +0100449 }
450
451 return IRQ_HANDLED;
Javier Martin9e15db72012-03-02 09:28:47 +0100452}
453
454static int imxdma_xfer_desc(struct imxdma_desc *d)
455{
456 struct imxdma_channel *imxdmac = to_imxdma_chan(d->desc.chan);
Javier Martin3b4b6df2012-03-22 14:54:04 +0100457 struct imxdma_engine *imxdma = imxdmac->imxdma;
Javier Martinf606ab82012-03-22 14:54:14 +0100458 unsigned long flags;
459 int slot = -1;
460 int i;
Javier Martin9e15db72012-03-02 09:28:47 +0100461
462 /* Configure and enable */
463 switch (d->type) {
Javier Martinf606ab82012-03-22 14:54:14 +0100464 case IMXDMA_DESC_INTERLEAVED:
465 /* Try to get a free 2D slot */
466 spin_lock_irqsave(&imxdma->lock, flags);
467 for (i = 0; i < IMX_DMA_2D_SLOTS; i++) {
468 if ((imxdma->slots_2d[i].count > 0) &&
469 ((imxdma->slots_2d[i].xsr != d->x) ||
470 (imxdma->slots_2d[i].ysr != d->y) ||
471 (imxdma->slots_2d[i].wsr != d->w)))
472 continue;
473 slot = i;
474 break;
475 }
Wei Yongjun7e8cec32012-10-21 19:58:30 +0800476 if (slot < 0) {
477 spin_unlock_irqrestore(&imxdma->lock, flags);
Javier Martinf606ab82012-03-22 14:54:14 +0100478 return -EBUSY;
Wei Yongjun7e8cec32012-10-21 19:58:30 +0800479 }
Javier Martinf606ab82012-03-22 14:54:14 +0100480
481 imxdma->slots_2d[slot].xsr = d->x;
482 imxdma->slots_2d[slot].ysr = d->y;
483 imxdma->slots_2d[slot].wsr = d->w;
484 imxdma->slots_2d[slot].count++;
485
486 imxdmac->slot_2d = slot;
487 imxdmac->enabled_2d = true;
488 spin_unlock_irqrestore(&imxdma->lock, flags);
489
490 if (slot == IMX_DMA_2D_SLOT_A) {
491 d->config_mem &= ~CCR_MSEL_B;
492 d->config_port &= ~CCR_MSEL_B;
493 imx_dmav1_writel(imxdma, d->x, DMA_XSRA);
494 imx_dmav1_writel(imxdma, d->y, DMA_YSRA);
495 imx_dmav1_writel(imxdma, d->w, DMA_WSRA);
496 } else {
497 d->config_mem |= CCR_MSEL_B;
498 d->config_port |= CCR_MSEL_B;
499 imx_dmav1_writel(imxdma, d->x, DMA_XSRB);
500 imx_dmav1_writel(imxdma, d->y, DMA_YSRB);
501 imx_dmav1_writel(imxdma, d->w, DMA_WSRB);
502 }
503 /*
504 * We fall-through here intentionally, since a 2D transfer is
505 * similar to MEMCPY just adding the 2D slot configuration.
506 */
Javier Martin9e15db72012-03-02 09:28:47 +0100507 case IMXDMA_DESC_MEMCPY:
Javier Martincd5cf9d2012-03-22 14:54:12 +0100508 imx_dmav1_writel(imxdma, d->src, DMA_SAR(imxdmac->channel));
509 imx_dmav1_writel(imxdma, d->dest, DMA_DAR(imxdmac->channel));
510 imx_dmav1_writel(imxdma, d->config_mem | (d->config_port << 2),
Javier Martin3b4b6df2012-03-22 14:54:04 +0100511 DMA_CCR(imxdmac->channel));
512
Javier Martincd5cf9d2012-03-22 14:54:12 +0100513 imx_dmav1_writel(imxdma, d->len, DMA_CNTR(imxdmac->channel));
Javier Martin3b4b6df2012-03-22 14:54:04 +0100514
515 dev_dbg(imxdma->dev, "%s channel: %d dest=0x%08x src=0x%08x "
516 "dma_length=%d\n", __func__, imxdmac->channel,
517 d->dest, d->src, d->len);
518
519 break;
Javier Martin6bd08122012-03-22 14:54:01 +0100520 /* Cyclic transfer is the same as slave_sg with special sg configuration. */
Javier Martin9e15db72012-03-02 09:28:47 +0100521 case IMXDMA_DESC_CYCLIC:
Javier Martin9e15db72012-03-02 09:28:47 +0100522 case IMXDMA_DESC_SLAVE_SG:
Javier Martin359291a2012-03-22 14:54:06 +0100523 if (d->direction == DMA_DEV_TO_MEM) {
Javier Martincd5cf9d2012-03-22 14:54:12 +0100524 imx_dmav1_writel(imxdma, imxdmac->per_address,
Javier Martin359291a2012-03-22 14:54:06 +0100525 DMA_SAR(imxdmac->channel));
Javier Martincd5cf9d2012-03-22 14:54:12 +0100526 imx_dmav1_writel(imxdma, imxdmac->ccr_from_device,
Javier Martin359291a2012-03-22 14:54:06 +0100527 DMA_CCR(imxdmac->channel));
528
529 dev_dbg(imxdma->dev, "%s channel: %d sg=%p sgcount=%d "
530 "total length=%d dev_addr=0x%08x (dev2mem)\n",
531 __func__, imxdmac->channel, d->sg, d->sgcount,
532 d->len, imxdmac->per_address);
533 } else if (d->direction == DMA_MEM_TO_DEV) {
Javier Martincd5cf9d2012-03-22 14:54:12 +0100534 imx_dmav1_writel(imxdma, imxdmac->per_address,
Javier Martin359291a2012-03-22 14:54:06 +0100535 DMA_DAR(imxdmac->channel));
Javier Martincd5cf9d2012-03-22 14:54:12 +0100536 imx_dmav1_writel(imxdma, imxdmac->ccr_to_device,
Javier Martin359291a2012-03-22 14:54:06 +0100537 DMA_CCR(imxdmac->channel));
538
539 dev_dbg(imxdma->dev, "%s channel: %d sg=%p sgcount=%d "
540 "total length=%d dev_addr=0x%08x (mem2dev)\n",
541 __func__, imxdmac->channel, d->sg, d->sgcount,
542 d->len, imxdmac->per_address);
543 } else {
544 dev_err(imxdma->dev, "%s channel: %d bad dma mode\n",
545 __func__, imxdmac->channel);
546 return -EINVAL;
547 }
548
Javier Martina6cbb2d2012-03-22 14:54:11 +0100549 imxdma_sg_next(d);
Javier Martin359291a2012-03-22 14:54:06 +0100550
Javier Martin9e15db72012-03-02 09:28:47 +0100551 break;
552 default:
553 return -EINVAL;
554 }
Javier Martin2efc3442012-03-22 14:54:03 +0100555 imxdma_enable_hw(d);
Javier Martin9e15db72012-03-02 09:28:47 +0100556 return 0;
557}
558
559static void imxdma_tasklet(unsigned long data)
560{
561 struct imxdma_channel *imxdmac = (void *)data;
562 struct imxdma_engine *imxdma = imxdmac->imxdma;
563 struct imxdma_desc *desc;
564
Javier Martinf606ab82012-03-22 14:54:14 +0100565 spin_lock(&imxdma->lock);
Javier Martin9e15db72012-03-02 09:28:47 +0100566
567 if (list_empty(&imxdmac->ld_active)) {
568 /* Someone might have called terminate all */
569 goto out;
570 }
571 desc = list_first_entry(&imxdmac->ld_active, struct imxdma_desc, node);
572
573 if (desc->desc.callback)
574 desc->desc.callback(desc->desc.callback_param);
575
Vinod Koul60f29512012-04-20 15:28:07 +0530576 /* If we are dealing with a cyclic descriptor keep it on ld_active
577 * and dont mark the descripor as complete.
578 * Only in non-cyclic cases it would be marked as complete
579 */
Javier Martin9e15db72012-03-02 09:28:47 +0100580 if (imxdma_chan_is_doing_cyclic(imxdmac))
581 goto out;
Vinod Koul60f29512012-04-20 15:28:07 +0530582 else
583 dma_cookie_complete(&desc->desc);
Javier Martin9e15db72012-03-02 09:28:47 +0100584
Javier Martinf606ab82012-03-22 14:54:14 +0100585 /* Free 2D slot if it was an interleaved transfer */
586 if (imxdmac->enabled_2d) {
587 imxdma->slots_2d[imxdmac->slot_2d].count--;
588 imxdmac->enabled_2d = false;
589 }
590
Javier Martin9e15db72012-03-02 09:28:47 +0100591 list_move_tail(imxdmac->ld_active.next, &imxdmac->ld_free);
592
593 if (!list_empty(&imxdmac->ld_queue)) {
594 desc = list_first_entry(&imxdmac->ld_queue, struct imxdma_desc,
595 node);
596 list_move_tail(imxdmac->ld_queue.next, &imxdmac->ld_active);
597 if (imxdma_xfer_desc(desc) < 0)
598 dev_warn(imxdma->dev, "%s: channel: %d couldn't xfer desc\n",
599 __func__, imxdmac->channel);
600 }
601out:
Javier Martinf606ab82012-03-22 14:54:14 +0100602 spin_unlock(&imxdma->lock);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200603}
604
605static int imxdma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
606 unsigned long arg)
607{
608 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
609 struct dma_slave_config *dmaengine_cfg = (void *)arg;
Javier Martincd5cf9d2012-03-22 14:54:12 +0100610 struct imxdma_engine *imxdma = imxdmac->imxdma;
Javier Martin9e15db72012-03-02 09:28:47 +0100611 unsigned long flags;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200612 unsigned int mode = 0;
613
614 switch (cmd) {
615 case DMA_TERMINATE_ALL:
Javier Martin6bd08122012-03-22 14:54:01 +0100616 imxdma_disable_hw(imxdmac);
Javier Martin9e15db72012-03-02 09:28:47 +0100617
Javier Martinf606ab82012-03-22 14:54:14 +0100618 spin_lock_irqsave(&imxdma->lock, flags);
Javier Martin9e15db72012-03-02 09:28:47 +0100619 list_splice_tail_init(&imxdmac->ld_active, &imxdmac->ld_free);
620 list_splice_tail_init(&imxdmac->ld_queue, &imxdmac->ld_free);
Javier Martinf606ab82012-03-22 14:54:14 +0100621 spin_unlock_irqrestore(&imxdma->lock, flags);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200622 return 0;
623 case DMA_SLAVE_CONFIG:
Vinod Kouldb8196d2011-10-13 22:34:23 +0530624 if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200625 imxdmac->per_address = dmaengine_cfg->src_addr;
626 imxdmac->watermark_level = dmaengine_cfg->src_maxburst;
627 imxdmac->word_size = dmaengine_cfg->src_addr_width;
628 } else {
629 imxdmac->per_address = dmaengine_cfg->dst_addr;
630 imxdmac->watermark_level = dmaengine_cfg->dst_maxburst;
631 imxdmac->word_size = dmaengine_cfg->dst_addr_width;
632 }
633
634 switch (imxdmac->word_size) {
635 case DMA_SLAVE_BUSWIDTH_1_BYTE:
636 mode = IMX_DMA_MEMSIZE_8;
637 break;
638 case DMA_SLAVE_BUSWIDTH_2_BYTES:
639 mode = IMX_DMA_MEMSIZE_16;
640 break;
641 default:
642 case DMA_SLAVE_BUSWIDTH_4_BYTES:
643 mode = IMX_DMA_MEMSIZE_32;
644 break;
645 }
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200646
Javier Martin2d9c2fc2012-03-22 14:54:10 +0100647 imxdmac->hw_chaining = 1;
648 if (!imxdma_hw_chain(imxdmac))
Javier Martinbdc0c752012-03-22 14:54:05 +0100649 return -EINVAL;
Javier Martin359291a2012-03-22 14:54:06 +0100650 imxdmac->ccr_from_device = (mode | IMX_DMA_TYPE_FIFO) |
Javier Martinbdc0c752012-03-22 14:54:05 +0100651 ((IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR) << 2) |
652 CCR_REN;
Javier Martin359291a2012-03-22 14:54:06 +0100653 imxdmac->ccr_to_device =
Javier Martinbdc0c752012-03-22 14:54:05 +0100654 (IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR) |
655 ((mode | IMX_DMA_TYPE_FIFO) << 2) | CCR_REN;
Javier Martincd5cf9d2012-03-22 14:54:12 +0100656 imx_dmav1_writel(imxdma, imxdmac->dma_request,
Javier Martinbdc0c752012-03-22 14:54:05 +0100657 DMA_RSSR(imxdmac->channel));
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200658
Javier Martin6bd08122012-03-22 14:54:01 +0100659 /* Set burst length */
Javier Martincd5cf9d2012-03-22 14:54:12 +0100660 imx_dmav1_writel(imxdma, imxdmac->watermark_level *
661 imxdmac->word_size, DMA_BLR(imxdmac->channel));
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200662
663 return 0;
664 default:
665 return -ENOSYS;
666 }
667
668 return -EINVAL;
669}
670
671static enum dma_status imxdma_tx_status(struct dma_chan *chan,
672 dma_cookie_t cookie,
673 struct dma_tx_state *txstate)
674{
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000675 return dma_cookie_status(chan, cookie, txstate);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200676}
677
678static dma_cookie_t imxdma_tx_submit(struct dma_async_tx_descriptor *tx)
679{
680 struct imxdma_channel *imxdmac = to_imxdma_chan(tx->chan);
Javier Martinf606ab82012-03-22 14:54:14 +0100681 struct imxdma_engine *imxdma = imxdmac->imxdma;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200682 dma_cookie_t cookie;
Javier Martin9e15db72012-03-02 09:28:47 +0100683 unsigned long flags;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200684
Javier Martinf606ab82012-03-22 14:54:14 +0100685 spin_lock_irqsave(&imxdma->lock, flags);
Javier Martin660cd0d2012-03-22 14:54:15 +0100686 list_move_tail(imxdmac->ld_free.next, &imxdmac->ld_queue);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000687 cookie = dma_cookie_assign(tx);
Javier Martinf606ab82012-03-22 14:54:14 +0100688 spin_unlock_irqrestore(&imxdma->lock, flags);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200689
690 return cookie;
691}
692
693static int imxdma_alloc_chan_resources(struct dma_chan *chan)
694{
695 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
696 struct imx_dma_data *data = chan->private;
697
Javier Martin6c05f092012-02-28 17:08:17 +0100698 if (data != NULL)
699 imxdmac->dma_request = data->dma_request;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200700
Javier Martin9e15db72012-03-02 09:28:47 +0100701 while (imxdmac->descs_allocated < IMXDMA_MAX_CHAN_DESCRIPTORS) {
702 struct imxdma_desc *desc;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200703
Javier Martin9e15db72012-03-02 09:28:47 +0100704 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
705 if (!desc)
706 break;
707 __memzero(&desc->desc, sizeof(struct dma_async_tx_descriptor));
708 dma_async_tx_descriptor_init(&desc->desc, chan);
709 desc->desc.tx_submit = imxdma_tx_submit;
710 /* txd.flags will be overwritten in prep funcs */
711 desc->desc.flags = DMA_CTRL_ACK;
712 desc->status = DMA_SUCCESS;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200713
Javier Martin9e15db72012-03-02 09:28:47 +0100714 list_add_tail(&desc->node, &imxdmac->ld_free);
715 imxdmac->descs_allocated++;
716 }
717
718 if (!imxdmac->descs_allocated)
719 return -ENOMEM;
720
721 return imxdmac->descs_allocated;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200722}
723
724static void imxdma_free_chan_resources(struct dma_chan *chan)
725{
726 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
Javier Martinf606ab82012-03-22 14:54:14 +0100727 struct imxdma_engine *imxdma = imxdmac->imxdma;
Javier Martin9e15db72012-03-02 09:28:47 +0100728 struct imxdma_desc *desc, *_desc;
729 unsigned long flags;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200730
Javier Martinf606ab82012-03-22 14:54:14 +0100731 spin_lock_irqsave(&imxdma->lock, flags);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200732
Javier Martin6bd08122012-03-22 14:54:01 +0100733 imxdma_disable_hw(imxdmac);
Javier Martin9e15db72012-03-02 09:28:47 +0100734 list_splice_tail_init(&imxdmac->ld_active, &imxdmac->ld_free);
735 list_splice_tail_init(&imxdmac->ld_queue, &imxdmac->ld_free);
736
Javier Martinf606ab82012-03-22 14:54:14 +0100737 spin_unlock_irqrestore(&imxdma->lock, flags);
Javier Martin9e15db72012-03-02 09:28:47 +0100738
739 list_for_each_entry_safe(desc, _desc, &imxdmac->ld_free, node) {
740 kfree(desc);
741 imxdmac->descs_allocated--;
742 }
743 INIT_LIST_HEAD(&imxdmac->ld_free);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200744
745 if (imxdmac->sg_list) {
746 kfree(imxdmac->sg_list);
747 imxdmac->sg_list = NULL;
748 }
749}
750
751static struct dma_async_tx_descriptor *imxdma_prep_slave_sg(
752 struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530753 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500754 unsigned long flags, void *context)
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200755{
756 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
757 struct scatterlist *sg;
Javier Martin9e15db72012-03-02 09:28:47 +0100758 int i, dma_length = 0;
759 struct imxdma_desc *desc;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200760
Javier Martin9e15db72012-03-02 09:28:47 +0100761 if (list_empty(&imxdmac->ld_free) ||
762 imxdma_chan_is_doing_cyclic(imxdmac))
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200763 return NULL;
764
Javier Martin9e15db72012-03-02 09:28:47 +0100765 desc = list_first_entry(&imxdmac->ld_free, struct imxdma_desc, node);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200766
767 for_each_sg(sgl, sg, sg_len, i) {
768 dma_length += sg->length;
769 }
770
Sascha Hauerd07102a2011-01-12 14:13:23 +0100771 switch (imxdmac->word_size) {
772 case DMA_SLAVE_BUSWIDTH_4_BYTES:
773 if (sgl->length & 3 || sgl->dma_address & 3)
774 return NULL;
775 break;
776 case DMA_SLAVE_BUSWIDTH_2_BYTES:
777 if (sgl->length & 1 || sgl->dma_address & 1)
778 return NULL;
779 break;
780 case DMA_SLAVE_BUSWIDTH_1_BYTE:
781 break;
782 default:
783 return NULL;
784 }
785
Javier Martin9e15db72012-03-02 09:28:47 +0100786 desc->type = IMXDMA_DESC_SLAVE_SG;
787 desc->sg = sgl;
788 desc->sgcount = sg_len;
789 desc->len = dma_length;
Javier Martin2efc3442012-03-22 14:54:03 +0100790 desc->direction = direction;
Javier Martin9e15db72012-03-02 09:28:47 +0100791 if (direction == DMA_DEV_TO_MEM) {
Javier Martin9e15db72012-03-02 09:28:47 +0100792 desc->src = imxdmac->per_address;
793 } else {
Javier Martin9e15db72012-03-02 09:28:47 +0100794 desc->dest = imxdmac->per_address;
795 }
796 desc->desc.callback = NULL;
797 desc->desc.callback_param = NULL;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200798
Javier Martin9e15db72012-03-02 09:28:47 +0100799 return &desc->desc;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200800}
801
802static struct dma_async_tx_descriptor *imxdma_prep_dma_cyclic(
803 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500804 size_t period_len, enum dma_transfer_direction direction,
805 void *context)
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200806{
807 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
808 struct imxdma_engine *imxdma = imxdmac->imxdma;
Javier Martin9e15db72012-03-02 09:28:47 +0100809 struct imxdma_desc *desc;
810 int i;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200811 unsigned int periods = buf_len / period_len;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200812
813 dev_dbg(imxdma->dev, "%s channel: %d buf_len=%d period_len=%d\n",
814 __func__, imxdmac->channel, buf_len, period_len);
815
Javier Martin9e15db72012-03-02 09:28:47 +0100816 if (list_empty(&imxdmac->ld_free) ||
817 imxdma_chan_is_doing_cyclic(imxdmac))
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200818 return NULL;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200819
Javier Martin9e15db72012-03-02 09:28:47 +0100820 desc = list_first_entry(&imxdmac->ld_free, struct imxdma_desc, node);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200821
822 if (imxdmac->sg_list)
823 kfree(imxdmac->sg_list);
824
825 imxdmac->sg_list = kcalloc(periods + 1,
826 sizeof(struct scatterlist), GFP_KERNEL);
827 if (!imxdmac->sg_list)
828 return NULL;
829
830 sg_init_table(imxdmac->sg_list, periods);
831
832 for (i = 0; i < periods; i++) {
833 imxdmac->sg_list[i].page_link = 0;
834 imxdmac->sg_list[i].offset = 0;
835 imxdmac->sg_list[i].dma_address = dma_addr;
836 imxdmac->sg_list[i].length = period_len;
837 dma_addr += period_len;
838 }
839
840 /* close the loop */
841 imxdmac->sg_list[periods].offset = 0;
842 imxdmac->sg_list[periods].length = 0;
843 imxdmac->sg_list[periods].page_link =
844 ((unsigned long)imxdmac->sg_list | 0x01) & ~0x02;
845
Javier Martin9e15db72012-03-02 09:28:47 +0100846 desc->type = IMXDMA_DESC_CYCLIC;
847 desc->sg = imxdmac->sg_list;
848 desc->sgcount = periods;
849 desc->len = IMX_DMA_LENGTH_LOOP;
Javier Martin2efc3442012-03-22 14:54:03 +0100850 desc->direction = direction;
Javier Martin9e15db72012-03-02 09:28:47 +0100851 if (direction == DMA_DEV_TO_MEM) {
Javier Martin9e15db72012-03-02 09:28:47 +0100852 desc->src = imxdmac->per_address;
853 } else {
Javier Martin9e15db72012-03-02 09:28:47 +0100854 desc->dest = imxdmac->per_address;
855 }
856 desc->desc.callback = NULL;
857 desc->desc.callback_param = NULL;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200858
Javier Martin9e15db72012-03-02 09:28:47 +0100859 return &desc->desc;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200860}
861
Javier Martin6c05f092012-02-28 17:08:17 +0100862static struct dma_async_tx_descriptor *imxdma_prep_dma_memcpy(
863 struct dma_chan *chan, dma_addr_t dest,
864 dma_addr_t src, size_t len, unsigned long flags)
865{
866 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
867 struct imxdma_engine *imxdma = imxdmac->imxdma;
Javier Martin9e15db72012-03-02 09:28:47 +0100868 struct imxdma_desc *desc;
Javier Martin6c05f092012-02-28 17:08:17 +0100869
870 dev_dbg(imxdma->dev, "%s channel: %d src=0x%x dst=0x%x len=%d\n",
871 __func__, imxdmac->channel, src, dest, len);
872
Javier Martin9e15db72012-03-02 09:28:47 +0100873 if (list_empty(&imxdmac->ld_free) ||
874 imxdma_chan_is_doing_cyclic(imxdmac))
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200875 return NULL;
876
Javier Martin9e15db72012-03-02 09:28:47 +0100877 desc = list_first_entry(&imxdmac->ld_free, struct imxdma_desc, node);
Javier Martin6c05f092012-02-28 17:08:17 +0100878
Javier Martin9e15db72012-03-02 09:28:47 +0100879 desc->type = IMXDMA_DESC_MEMCPY;
880 desc->src = src;
881 desc->dest = dest;
882 desc->len = len;
Javier Martin2efc3442012-03-22 14:54:03 +0100883 desc->direction = DMA_MEM_TO_MEM;
Javier Martin9e15db72012-03-02 09:28:47 +0100884 desc->config_port = IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR;
885 desc->config_mem = IMX_DMA_MEMSIZE_32 | IMX_DMA_TYPE_LINEAR;
886 desc->desc.callback = NULL;
887 desc->desc.callback_param = NULL;
888
889 return &desc->desc;
Javier Martin6c05f092012-02-28 17:08:17 +0100890}
891
Javier Martinf606ab82012-03-22 14:54:14 +0100892static struct dma_async_tx_descriptor *imxdma_prep_dma_interleaved(
893 struct dma_chan *chan, struct dma_interleaved_template *xt,
894 unsigned long flags)
895{
896 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
897 struct imxdma_engine *imxdma = imxdmac->imxdma;
898 struct imxdma_desc *desc;
899
900 dev_dbg(imxdma->dev, "%s channel: %d src_start=0x%x dst_start=0x%x\n"
901 " src_sgl=%s dst_sgl=%s numf=%d frame_size=%d\n", __func__,
902 imxdmac->channel, xt->src_start, xt->dst_start,
903 xt->src_sgl ? "true" : "false", xt->dst_sgl ? "true" : "false",
904 xt->numf, xt->frame_size);
905
906 if (list_empty(&imxdmac->ld_free) ||
907 imxdma_chan_is_doing_cyclic(imxdmac))
908 return NULL;
909
910 if (xt->frame_size != 1 || xt->numf <= 0 || xt->dir != DMA_MEM_TO_MEM)
911 return NULL;
912
913 desc = list_first_entry(&imxdmac->ld_free, struct imxdma_desc, node);
914
915 desc->type = IMXDMA_DESC_INTERLEAVED;
916 desc->src = xt->src_start;
917 desc->dest = xt->dst_start;
918 desc->x = xt->sgl[0].size;
919 desc->y = xt->numf;
920 desc->w = xt->sgl[0].icg + desc->x;
921 desc->len = desc->x * desc->y;
922 desc->direction = DMA_MEM_TO_MEM;
923 desc->config_port = IMX_DMA_MEMSIZE_32;
924 desc->config_mem = IMX_DMA_MEMSIZE_32;
925 if (xt->src_sgl)
926 desc->config_mem |= IMX_DMA_TYPE_2D;
927 if (xt->dst_sgl)
928 desc->config_port |= IMX_DMA_TYPE_2D;
929 desc->desc.callback = NULL;
930 desc->desc.callback_param = NULL;
931
932 return &desc->desc;
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200933}
934
935static void imxdma_issue_pending(struct dma_chan *chan)
936{
Sascha Hauer5b316872012-01-09 10:32:49 +0100937 struct imxdma_channel *imxdmac = to_imxdma_chan(chan);
Javier Martin9e15db72012-03-02 09:28:47 +0100938 struct imxdma_engine *imxdma = imxdmac->imxdma;
939 struct imxdma_desc *desc;
940 unsigned long flags;
Sascha Hauer5b316872012-01-09 10:32:49 +0100941
Javier Martinf606ab82012-03-22 14:54:14 +0100942 spin_lock_irqsave(&imxdma->lock, flags);
Javier Martin9e15db72012-03-02 09:28:47 +0100943 if (list_empty(&imxdmac->ld_active) &&
944 !list_empty(&imxdmac->ld_queue)) {
945 desc = list_first_entry(&imxdmac->ld_queue,
946 struct imxdma_desc, node);
947
948 if (imxdma_xfer_desc(desc) < 0) {
949 dev_warn(imxdma->dev,
950 "%s: channel: %d couldn't issue DMA xfer\n",
951 __func__, imxdmac->channel);
952 } else {
953 list_move_tail(imxdmac->ld_queue.next,
954 &imxdmac->ld_active);
955 }
956 }
Javier Martinf606ab82012-03-22 14:54:14 +0100957 spin_unlock_irqrestore(&imxdma->lock, flags);
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200958}
959
960static int __init imxdma_probe(struct platform_device *pdev)
Javier Martin6bd08122012-03-22 14:54:01 +0100961 {
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200962 struct imxdma_engine *imxdma;
963 int ret, i;
964
Javier Martin6bd08122012-03-22 14:54:01 +0100965
Sascha Hauer1f1846c2010-10-06 10:25:55 +0200966 imxdma = kzalloc(sizeof(*imxdma), GFP_KERNEL);
967 if (!imxdma)
968 return -ENOMEM;
969
Javier Martincd5cf9d2012-03-22 14:54:12 +0100970 if (cpu_is_mx1()) {
971 imxdma->base = MX1_IO_ADDRESS(MX1_DMA_BASE_ADDR);
972 } else if (cpu_is_mx21()) {
973 imxdma->base = MX21_IO_ADDRESS(MX21_DMA_BASE_ADDR);
974 } else if (cpu_is_mx27()) {
975 imxdma->base = MX27_IO_ADDRESS(MX27_DMA_BASE_ADDR);
976 } else {
977 kfree(imxdma);
978 return 0;
979 }
980
Fabio Estevam449daa02012-07-03 15:33:29 -0300981 imxdma->dma_clk = clk_get(NULL, "dma");
982 if (IS_ERR(imxdma->dma_clk))
983 return PTR_ERR(imxdma->dma_clk);
984 clk_enable(imxdma->dma_clk);
Javier Martin6bd08122012-03-22 14:54:01 +0100985
986 /* reset DMA module */
Javier Martincd5cf9d2012-03-22 14:54:12 +0100987 imx_dmav1_writel(imxdma, DCR_DRST, DMA_DCR);
Javier Martin6bd08122012-03-22 14:54:01 +0100988
989 if (cpu_is_mx1()) {
990 ret = request_irq(MX1_DMA_INT, dma_irq_handler, 0, "DMA", imxdma);
991 if (ret) {
Javier Martinf9b283a2012-03-22 14:54:13 +0100992 dev_warn(imxdma->dev, "Can't register IRQ for DMA\n");
Fabio Estevam449daa02012-07-03 15:33:29 -0300993 kfree(imxdma);
994 return ret;
Javier Martin6bd08122012-03-22 14:54:01 +0100995 }
996
997 ret = request_irq(MX1_DMA_ERR, imxdma_err_handler, 0, "DMA", imxdma);
998 if (ret) {
Javier Martinf9b283a2012-03-22 14:54:13 +0100999 dev_warn(imxdma->dev, "Can't register ERRIRQ for DMA\n");
Javier Martin6bd08122012-03-22 14:54:01 +01001000 free_irq(MX1_DMA_INT, NULL);
Fabio Estevam449daa02012-07-03 15:33:29 -03001001 kfree(imxdma);
1002 return ret;
Javier Martin6bd08122012-03-22 14:54:01 +01001003 }
1004 }
1005
1006 /* enable DMA module */
Javier Martincd5cf9d2012-03-22 14:54:12 +01001007 imx_dmav1_writel(imxdma, DCR_DEN, DMA_DCR);
Javier Martin6bd08122012-03-22 14:54:01 +01001008
1009 /* clear all interrupts */
Javier Martincd5cf9d2012-03-22 14:54:12 +01001010 imx_dmav1_writel(imxdma, (1 << IMX_DMA_CHANNELS) - 1, DMA_DISR);
Javier Martin6bd08122012-03-22 14:54:01 +01001011
1012 /* disable interrupts */
Javier Martincd5cf9d2012-03-22 14:54:12 +01001013 imx_dmav1_writel(imxdma, (1 << IMX_DMA_CHANNELS) - 1, DMA_DIMR);
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001014
1015 INIT_LIST_HEAD(&imxdma->dma_device.channels);
1016
Sascha Hauerf8a356f2011-01-31 11:35:59 +01001017 dma_cap_set(DMA_SLAVE, imxdma->dma_device.cap_mask);
1018 dma_cap_set(DMA_CYCLIC, imxdma->dma_device.cap_mask);
Javier Martin6c05f092012-02-28 17:08:17 +01001019 dma_cap_set(DMA_MEMCPY, imxdma->dma_device.cap_mask);
Javier Martinf606ab82012-03-22 14:54:14 +01001020 dma_cap_set(DMA_INTERLEAVE, imxdma->dma_device.cap_mask);
1021
1022 /* Initialize 2D global parameters */
1023 for (i = 0; i < IMX_DMA_2D_SLOTS; i++)
1024 imxdma->slots_2d[i].count = 0;
1025
1026 spin_lock_init(&imxdma->lock);
Sascha Hauerf8a356f2011-01-31 11:35:59 +01001027
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001028 /* Initialize channel parameters */
Javier Martin6bd08122012-03-22 14:54:01 +01001029 for (i = 0; i < IMX_DMA_CHANNELS; i++) {
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001030 struct imxdma_channel *imxdmac = &imxdma->channel[i];
1031
Javier Martin6bd08122012-03-22 14:54:01 +01001032 if (cpu_is_mx21() || cpu_is_mx27()) {
1033 ret = request_irq(MX2x_INT_DMACH0 + i,
1034 dma_irq_handler, 0, "DMA", imxdma);
1035 if (ret) {
Javier Martinf9b283a2012-03-22 14:54:13 +01001036 dev_warn(imxdma->dev, "Can't register IRQ %d "
1037 "for DMA channel %d\n",
1038 MX2x_INT_DMACH0 + i, i);
Javier Martin6bd08122012-03-22 14:54:01 +01001039 goto err_init;
1040 }
Javier Martin2d9c2fc2012-03-22 14:54:10 +01001041 init_timer(&imxdmac->watchdog);
1042 imxdmac->watchdog.function = &imxdma_watchdog;
1043 imxdmac->watchdog.data = (unsigned long)imxdmac;
Sascha Hauer8267f162010-10-20 08:37:19 +02001044 }
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001045
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001046 imxdmac->imxdma = imxdma;
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001047
Javier Martin9e15db72012-03-02 09:28:47 +01001048 INIT_LIST_HEAD(&imxdmac->ld_queue);
1049 INIT_LIST_HEAD(&imxdmac->ld_free);
1050 INIT_LIST_HEAD(&imxdmac->ld_active);
1051
1052 tasklet_init(&imxdmac->dma_tasklet, imxdma_tasklet,
1053 (unsigned long)imxdmac);
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001054 imxdmac->chan.device = &imxdma->dma_device;
Russell King - ARM Linux8ac69542012-03-06 22:36:27 +00001055 dma_cookie_init(&imxdmac->chan);
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001056 imxdmac->channel = i;
1057
1058 /* Add the channel to the DMAC list */
Javier Martin9e15db72012-03-02 09:28:47 +01001059 list_add_tail(&imxdmac->chan.device_node,
1060 &imxdma->dma_device.channels);
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001061 }
1062
1063 imxdma->dev = &pdev->dev;
1064 imxdma->dma_device.dev = &pdev->dev;
1065
1066 imxdma->dma_device.device_alloc_chan_resources = imxdma_alloc_chan_resources;
1067 imxdma->dma_device.device_free_chan_resources = imxdma_free_chan_resources;
1068 imxdma->dma_device.device_tx_status = imxdma_tx_status;
1069 imxdma->dma_device.device_prep_slave_sg = imxdma_prep_slave_sg;
1070 imxdma->dma_device.device_prep_dma_cyclic = imxdma_prep_dma_cyclic;
Javier Martin6c05f092012-02-28 17:08:17 +01001071 imxdma->dma_device.device_prep_dma_memcpy = imxdma_prep_dma_memcpy;
Javier Martinf606ab82012-03-22 14:54:14 +01001072 imxdma->dma_device.device_prep_interleaved_dma = imxdma_prep_dma_interleaved;
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001073 imxdma->dma_device.device_control = imxdma_control;
1074 imxdma->dma_device.device_issue_pending = imxdma_issue_pending;
1075
1076 platform_set_drvdata(pdev, imxdma);
1077
Javier Martin6c05f092012-02-28 17:08:17 +01001078 imxdma->dma_device.copy_align = 2; /* 2^2 = 4 bytes alignment */
Sascha Hauer1e070a62011-01-12 13:14:37 +01001079 imxdma->dma_device.dev->dma_parms = &imxdma->dma_parms;
1080 dma_set_max_seg_size(imxdma->dma_device.dev, 0xffffff);
1081
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001082 ret = dma_async_device_register(&imxdma->dma_device);
1083 if (ret) {
1084 dev_err(&pdev->dev, "unable to register\n");
1085 goto err_init;
1086 }
1087
1088 return 0;
1089
1090err_init:
Javier Martin6bd08122012-03-22 14:54:01 +01001091
1092 if (cpu_is_mx21() || cpu_is_mx27()) {
1093 while (--i >= 0)
1094 free_irq(MX2x_INT_DMACH0 + i, NULL);
1095 } else if cpu_is_mx1() {
1096 free_irq(MX1_DMA_INT, NULL);
1097 free_irq(MX1_DMA_ERR, NULL);
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001098 }
Fabio Estevam449daa02012-07-03 15:33:29 -03001099
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001100 kfree(imxdma);
1101 return ret;
1102}
1103
1104static int __exit imxdma_remove(struct platform_device *pdev)
1105{
1106 struct imxdma_engine *imxdma = platform_get_drvdata(pdev);
1107 int i;
1108
1109 dma_async_device_unregister(&imxdma->dma_device);
1110
Javier Martin6bd08122012-03-22 14:54:01 +01001111 if (cpu_is_mx21() || cpu_is_mx27()) {
1112 for (i = 0; i < IMX_DMA_CHANNELS; i++)
1113 free_irq(MX2x_INT_DMACH0 + i, NULL);
1114 } else if cpu_is_mx1() {
1115 free_irq(MX1_DMA_INT, NULL);
1116 free_irq(MX1_DMA_ERR, NULL);
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001117 }
1118
Fabio Estevam449daa02012-07-03 15:33:29 -03001119 kfree(imxdma);
Sascha Hauer1f1846c2010-10-06 10:25:55 +02001120
1121 return 0;
1122}
1123
1124static struct platform_driver imxdma_driver = {
1125 .driver = {
1126 .name = "imx-dma",
1127 },
1128 .remove = __exit_p(imxdma_remove),
1129};
1130
1131static int __init imxdma_module_init(void)
1132{
1133 return platform_driver_probe(&imxdma_driver, imxdma_probe);
1134}
1135subsys_initcall(imxdma_module_init);
1136
1137MODULE_AUTHOR("Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>");
1138MODULE_DESCRIPTION("i.MX dma driver");
1139MODULE_LICENSE("GPL");