blob: 1e984e18c1266b74fc01c247fad36f3e132ad587 [file] [log] [blame]
Russell King7bedaa52012-04-13 12:10:24 +01001/*
2 * OMAP DMAengine support
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
Russell Kingfa3ad862013-11-02 17:07:09 +00008#include <linux/delay.h>
Russell King7bedaa52012-04-13 12:10:24 +01009#include <linux/dmaengine.h>
10#include <linux/dma-mapping.h>
11#include <linux/err.h>
12#include <linux/init.h>
13#include <linux/interrupt.h>
14#include <linux/list.h>
15#include <linux/module.h>
16#include <linux/omap-dma.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19#include <linux/spinlock.h>
Jon Hunter8d306622013-02-26 12:27:24 -060020#include <linux/of_dma.h>
21#include <linux/of_device.h>
Russell King7bedaa52012-04-13 12:10:24 +010022
23#include "virt-dma.h"
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070024
Peter Ujfalusi341ce712015-04-09 12:35:50 +030025#define OMAP_SDMA_REQUESTS 127
26#define OMAP_SDMA_CHANNELS 32
27
Russell King7bedaa52012-04-13 12:10:24 +010028struct omap_dmadev {
29 struct dma_device ddev;
30 spinlock_t lock;
Russell King596c4712013-12-10 11:08:01 +000031 void __iomem *base;
32 const struct omap_dma_reg *reg_map;
Russell King1b416c42013-11-02 13:00:03 +000033 struct omap_system_dma_plat_info *plat;
Russell King6ddeb6d2013-12-10 19:05:50 +000034 bool legacy;
Peter Ujfaluside506082015-04-09 12:35:51 +030035 unsigned dma_requests;
Russell King6ddeb6d2013-12-10 19:05:50 +000036 spinlock_t irq_lock;
37 uint32_t irq_enable_mask;
Peter Ujfalusi341ce712015-04-09 12:35:50 +030038 struct omap_chan *lch_map[OMAP_SDMA_CHANNELS];
Russell King7bedaa52012-04-13 12:10:24 +010039};
40
41struct omap_chan {
42 struct virt_dma_chan vc;
Russell King596c4712013-12-10 11:08:01 +000043 void __iomem *channel_base;
44 const struct omap_dma_reg *reg_map;
Russell Kingaa4c5b92014-01-14 23:58:10 +000045 uint32_t ccr;
Russell King7bedaa52012-04-13 12:10:24 +010046
47 struct dma_slave_config cfg;
48 unsigned dma_sig;
Russell King3a774ea2012-06-21 10:40:15 +010049 bool cyclic;
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +030050 bool paused;
Peter Ujfalusi689d3c52016-04-05 15:20:20 +030051 bool running;
Russell King7bedaa52012-04-13 12:10:24 +010052
53 int dma_ch;
54 struct omap_desc *desc;
55 unsigned sgidx;
56};
57
58struct omap_sg {
59 dma_addr_t addr;
60 uint32_t en; /* number of elements (24-bit) */
61 uint32_t fn; /* number of frames (16-bit) */
62};
63
64struct omap_desc {
65 struct virt_dma_desc vd;
66 enum dma_transfer_direction dir;
67 dma_addr_t dev_addr;
68
Russell King7c836bc2012-06-18 16:45:19 +010069 int16_t fi; /* for OMAP_DMA_SYNC_PACKET */
Russell King90438262013-11-02 19:57:06 +000070 uint8_t es; /* CSDP_DATA_TYPE_xxx */
Russell King3ed4d182013-11-02 19:16:09 +000071 uint32_t ccr; /* CCR value */
Russell King965aeb4d2013-11-06 17:12:30 +000072 uint16_t clnk_ctrl; /* CLNK_CTRL value */
Russell Kingfa3ad862013-11-02 17:07:09 +000073 uint16_t cicr; /* CICR value */
Russell King2f0d13b2013-11-02 18:51:53 +000074 uint32_t csdp; /* CSDP value */
Russell King7bedaa52012-04-13 12:10:24 +010075
76 unsigned sglen;
77 struct omap_sg sg[0];
78};
79
Russell King90438262013-11-02 19:57:06 +000080enum {
81 CCR_FS = BIT(5),
82 CCR_READ_PRIORITY = BIT(6),
83 CCR_ENABLE = BIT(7),
84 CCR_AUTO_INIT = BIT(8), /* OMAP1 only */
85 CCR_REPEAT = BIT(9), /* OMAP1 only */
86 CCR_OMAP31_DISABLE = BIT(10), /* OMAP1 only */
87 CCR_SUSPEND_SENSITIVE = BIT(8), /* OMAP2+ only */
88 CCR_RD_ACTIVE = BIT(9), /* OMAP2+ only */
89 CCR_WR_ACTIVE = BIT(10), /* OMAP2+ only */
90 CCR_SRC_AMODE_CONSTANT = 0 << 12,
91 CCR_SRC_AMODE_POSTINC = 1 << 12,
92 CCR_SRC_AMODE_SGLIDX = 2 << 12,
93 CCR_SRC_AMODE_DBLIDX = 3 << 12,
94 CCR_DST_AMODE_CONSTANT = 0 << 14,
95 CCR_DST_AMODE_POSTINC = 1 << 14,
96 CCR_DST_AMODE_SGLIDX = 2 << 14,
97 CCR_DST_AMODE_DBLIDX = 3 << 14,
98 CCR_CONSTANT_FILL = BIT(16),
99 CCR_TRANSPARENT_COPY = BIT(17),
100 CCR_BS = BIT(18),
101 CCR_SUPERVISOR = BIT(22),
102 CCR_PREFETCH = BIT(23),
103 CCR_TRIGGER_SRC = BIT(24),
104 CCR_BUFFERING_DISABLE = BIT(25),
105 CCR_WRITE_PRIORITY = BIT(26),
106 CCR_SYNC_ELEMENT = 0,
107 CCR_SYNC_FRAME = CCR_FS,
108 CCR_SYNC_BLOCK = CCR_BS,
109 CCR_SYNC_PACKET = CCR_BS | CCR_FS,
110
111 CSDP_DATA_TYPE_8 = 0,
112 CSDP_DATA_TYPE_16 = 1,
113 CSDP_DATA_TYPE_32 = 2,
114 CSDP_SRC_PORT_EMIFF = 0 << 2, /* OMAP1 only */
115 CSDP_SRC_PORT_EMIFS = 1 << 2, /* OMAP1 only */
116 CSDP_SRC_PORT_OCP_T1 = 2 << 2, /* OMAP1 only */
117 CSDP_SRC_PORT_TIPB = 3 << 2, /* OMAP1 only */
118 CSDP_SRC_PORT_OCP_T2 = 4 << 2, /* OMAP1 only */
119 CSDP_SRC_PORT_MPUI = 5 << 2, /* OMAP1 only */
120 CSDP_SRC_PACKED = BIT(6),
121 CSDP_SRC_BURST_1 = 0 << 7,
122 CSDP_SRC_BURST_16 = 1 << 7,
123 CSDP_SRC_BURST_32 = 2 << 7,
124 CSDP_SRC_BURST_64 = 3 << 7,
125 CSDP_DST_PORT_EMIFF = 0 << 9, /* OMAP1 only */
126 CSDP_DST_PORT_EMIFS = 1 << 9, /* OMAP1 only */
127 CSDP_DST_PORT_OCP_T1 = 2 << 9, /* OMAP1 only */
128 CSDP_DST_PORT_TIPB = 3 << 9, /* OMAP1 only */
129 CSDP_DST_PORT_OCP_T2 = 4 << 9, /* OMAP1 only */
130 CSDP_DST_PORT_MPUI = 5 << 9, /* OMAP1 only */
131 CSDP_DST_PACKED = BIT(13),
132 CSDP_DST_BURST_1 = 0 << 14,
133 CSDP_DST_BURST_16 = 1 << 14,
134 CSDP_DST_BURST_32 = 2 << 14,
135 CSDP_DST_BURST_64 = 3 << 14,
136
137 CICR_TOUT_IE = BIT(0), /* OMAP1 only */
138 CICR_DROP_IE = BIT(1),
139 CICR_HALF_IE = BIT(2),
140 CICR_FRAME_IE = BIT(3),
141 CICR_LAST_IE = BIT(4),
142 CICR_BLOCK_IE = BIT(5),
143 CICR_PKT_IE = BIT(7), /* OMAP2+ only */
144 CICR_TRANS_ERR_IE = BIT(8), /* OMAP2+ only */
145 CICR_SUPERVISOR_ERR_IE = BIT(10), /* OMAP2+ only */
146 CICR_MISALIGNED_ERR_IE = BIT(11), /* OMAP2+ only */
147 CICR_DRAIN_IE = BIT(12), /* OMAP2+ only */
148 CICR_SUPER_BLOCK_IE = BIT(14), /* OMAP2+ only */
149
150 CLNK_CTRL_ENABLE_LNK = BIT(15),
151};
152
Russell King7bedaa52012-04-13 12:10:24 +0100153static const unsigned es_bytes[] = {
Russell King90438262013-11-02 19:57:06 +0000154 [CSDP_DATA_TYPE_8] = 1,
155 [CSDP_DATA_TYPE_16] = 2,
156 [CSDP_DATA_TYPE_32] = 4,
Russell King7bedaa52012-04-13 12:10:24 +0100157};
158
Jon Hunter8d306622013-02-26 12:27:24 -0600159static struct of_dma_filter_info omap_dma_info = {
160 .filter_fn = omap_dma_filter_fn,
161};
162
Russell King7bedaa52012-04-13 12:10:24 +0100163static inline struct omap_dmadev *to_omap_dma_dev(struct dma_device *d)
164{
165 return container_of(d, struct omap_dmadev, ddev);
166}
167
168static inline struct omap_chan *to_omap_dma_chan(struct dma_chan *c)
169{
170 return container_of(c, struct omap_chan, vc.chan);
171}
172
173static inline struct omap_desc *to_omap_dma_desc(struct dma_async_tx_descriptor *t)
174{
175 return container_of(t, struct omap_desc, vd.tx);
176}
177
178static void omap_dma_desc_free(struct virt_dma_desc *vd)
179{
180 kfree(container_of(vd, struct omap_desc, vd));
181}
182
Russell King596c4712013-12-10 11:08:01 +0000183static void omap_dma_write(uint32_t val, unsigned type, void __iomem *addr)
184{
185 switch (type) {
186 case OMAP_DMA_REG_16BIT:
187 writew_relaxed(val, addr);
188 break;
189 case OMAP_DMA_REG_2X16BIT:
190 writew_relaxed(val, addr);
191 writew_relaxed(val >> 16, addr + 2);
192 break;
193 case OMAP_DMA_REG_32BIT:
194 writel_relaxed(val, addr);
195 break;
196 default:
197 WARN_ON(1);
198 }
199}
200
201static unsigned omap_dma_read(unsigned type, void __iomem *addr)
202{
203 unsigned val;
204
205 switch (type) {
206 case OMAP_DMA_REG_16BIT:
207 val = readw_relaxed(addr);
208 break;
209 case OMAP_DMA_REG_2X16BIT:
210 val = readw_relaxed(addr);
211 val |= readw_relaxed(addr + 2) << 16;
212 break;
213 case OMAP_DMA_REG_32BIT:
214 val = readl_relaxed(addr);
215 break;
216 default:
217 WARN_ON(1);
218 val = 0;
219 }
220
221 return val;
222}
223
Russell Kingc5ed98b2013-11-06 17:33:09 +0000224static void omap_dma_glbl_write(struct omap_dmadev *od, unsigned reg, unsigned val)
225{
Russell King596c4712013-12-10 11:08:01 +0000226 const struct omap_dma_reg *r = od->reg_map + reg;
227
228 WARN_ON(r->stride);
229
230 omap_dma_write(val, r->type, od->base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000231}
232
233static unsigned omap_dma_glbl_read(struct omap_dmadev *od, unsigned reg)
234{
Russell King596c4712013-12-10 11:08:01 +0000235 const struct omap_dma_reg *r = od->reg_map + reg;
236
237 WARN_ON(r->stride);
238
239 return omap_dma_read(r->type, od->base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000240}
241
242static void omap_dma_chan_write(struct omap_chan *c, unsigned reg, unsigned val)
243{
Russell King596c4712013-12-10 11:08:01 +0000244 const struct omap_dma_reg *r = c->reg_map + reg;
245
246 omap_dma_write(val, r->type, c->channel_base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000247}
248
249static unsigned omap_dma_chan_read(struct omap_chan *c, unsigned reg)
250{
Russell King596c4712013-12-10 11:08:01 +0000251 const struct omap_dma_reg *r = c->reg_map + reg;
252
253 return omap_dma_read(r->type, c->channel_base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000254}
255
Russell King470b23f2013-11-02 21:23:06 +0000256static void omap_dma_clear_csr(struct omap_chan *c)
257{
258 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000259 omap_dma_chan_read(c, CSR);
Russell King470b23f2013-11-02 21:23:06 +0000260 else
Russell Kingc5ed98b2013-11-06 17:33:09 +0000261 omap_dma_chan_write(c, CSR, ~0);
Russell King470b23f2013-11-02 21:23:06 +0000262}
263
Russell King6ddeb6d2013-12-10 19:05:50 +0000264static unsigned omap_dma_get_csr(struct omap_chan *c)
265{
266 unsigned val = omap_dma_chan_read(c, CSR);
267
268 if (!dma_omap1())
269 omap_dma_chan_write(c, CSR, val);
270
271 return val;
272}
273
Russell King596c4712013-12-10 11:08:01 +0000274static void omap_dma_assign(struct omap_dmadev *od, struct omap_chan *c,
275 unsigned lch)
276{
277 c->channel_base = od->base + od->plat->channel_stride * lch;
Russell King6ddeb6d2013-12-10 19:05:50 +0000278
279 od->lch_map[lch] = c;
Russell King596c4712013-12-10 11:08:01 +0000280}
281
Russell Kingfa3ad862013-11-02 17:07:09 +0000282static void omap_dma_start(struct omap_chan *c, struct omap_desc *d)
283{
284 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
Russell Kingfa3ad862013-11-02 17:07:09 +0000285
286 if (__dma_omap15xx(od->plat->dma_attr))
Russell Kingc5ed98b2013-11-06 17:33:09 +0000287 omap_dma_chan_write(c, CPC, 0);
Russell Kingfa3ad862013-11-02 17:07:09 +0000288 else
Russell Kingc5ed98b2013-11-06 17:33:09 +0000289 omap_dma_chan_write(c, CDAC, 0);
Russell Kingfa3ad862013-11-02 17:07:09 +0000290
Russell King470b23f2013-11-02 21:23:06 +0000291 omap_dma_clear_csr(c);
Russell Kingfa3ad862013-11-02 17:07:09 +0000292
293 /* Enable interrupts */
Russell Kingc5ed98b2013-11-06 17:33:09 +0000294 omap_dma_chan_write(c, CICR, d->cicr);
Russell Kingfa3ad862013-11-02 17:07:09 +0000295
Russell King45da7b02013-11-06 17:18:42 +0000296 /* Enable channel */
Russell Kingc5ed98b2013-11-06 17:33:09 +0000297 omap_dma_chan_write(c, CCR, d->ccr | CCR_ENABLE);
Peter Ujfalusi689d3c52016-04-05 15:20:20 +0300298
299 c->running = true;
Russell Kingfa3ad862013-11-02 17:07:09 +0000300}
301
302static void omap_dma_stop(struct omap_chan *c)
303{
304 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
305 uint32_t val;
306
307 /* disable irq */
Russell Kingc5ed98b2013-11-06 17:33:09 +0000308 omap_dma_chan_write(c, CICR, 0);
Russell Kingfa3ad862013-11-02 17:07:09 +0000309
Russell King470b23f2013-11-02 21:23:06 +0000310 omap_dma_clear_csr(c);
Russell Kingfa3ad862013-11-02 17:07:09 +0000311
Russell Kingc5ed98b2013-11-06 17:33:09 +0000312 val = omap_dma_chan_read(c, CCR);
Russell King90438262013-11-02 19:57:06 +0000313 if (od->plat->errata & DMA_ERRATA_i541 && val & CCR_TRIGGER_SRC) {
Russell Kingfa3ad862013-11-02 17:07:09 +0000314 uint32_t sysconfig;
315 unsigned i;
316
Russell Kingc5ed98b2013-11-06 17:33:09 +0000317 sysconfig = omap_dma_glbl_read(od, OCP_SYSCONFIG);
Russell Kingfa3ad862013-11-02 17:07:09 +0000318 val = sysconfig & ~DMA_SYSCONFIG_MIDLEMODE_MASK;
319 val |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000320 omap_dma_glbl_write(od, OCP_SYSCONFIG, val);
Russell Kingfa3ad862013-11-02 17:07:09 +0000321
Russell Kingc5ed98b2013-11-06 17:33:09 +0000322 val = omap_dma_chan_read(c, CCR);
Russell King90438262013-11-02 19:57:06 +0000323 val &= ~CCR_ENABLE;
Russell Kingc5ed98b2013-11-06 17:33:09 +0000324 omap_dma_chan_write(c, CCR, val);
Russell Kingfa3ad862013-11-02 17:07:09 +0000325
326 /* Wait for sDMA FIFO to drain */
327 for (i = 0; ; i++) {
Russell Kingc5ed98b2013-11-06 17:33:09 +0000328 val = omap_dma_chan_read(c, CCR);
Russell King90438262013-11-02 19:57:06 +0000329 if (!(val & (CCR_RD_ACTIVE | CCR_WR_ACTIVE)))
Russell Kingfa3ad862013-11-02 17:07:09 +0000330 break;
331
332 if (i > 100)
333 break;
334
335 udelay(5);
336 }
337
Russell King90438262013-11-02 19:57:06 +0000338 if (val & (CCR_RD_ACTIVE | CCR_WR_ACTIVE))
Russell Kingfa3ad862013-11-02 17:07:09 +0000339 dev_err(c->vc.chan.device->dev,
340 "DMA drain did not complete on lch %d\n",
341 c->dma_ch);
342
Russell Kingc5ed98b2013-11-06 17:33:09 +0000343 omap_dma_glbl_write(od, OCP_SYSCONFIG, sysconfig);
Russell Kingfa3ad862013-11-02 17:07:09 +0000344 } else {
Russell King90438262013-11-02 19:57:06 +0000345 val &= ~CCR_ENABLE;
Russell Kingc5ed98b2013-11-06 17:33:09 +0000346 omap_dma_chan_write(c, CCR, val);
Russell Kingfa3ad862013-11-02 17:07:09 +0000347 }
348
349 mb();
350
351 if (!__dma_omap15xx(od->plat->dma_attr) && c->cyclic) {
Russell Kingc5ed98b2013-11-06 17:33:09 +0000352 val = omap_dma_chan_read(c, CLNK_CTRL);
Russell Kingfa3ad862013-11-02 17:07:09 +0000353
354 if (dma_omap1())
355 val |= 1 << 14; /* set the STOP_LNK bit */
356 else
Russell King90438262013-11-02 19:57:06 +0000357 val &= ~CLNK_CTRL_ENABLE_LNK;
Russell Kingfa3ad862013-11-02 17:07:09 +0000358
Russell Kingc5ed98b2013-11-06 17:33:09 +0000359 omap_dma_chan_write(c, CLNK_CTRL, val);
Russell Kingfa3ad862013-11-02 17:07:09 +0000360 }
Peter Ujfalusi689d3c52016-04-05 15:20:20 +0300361
362 c->running = false;
Russell Kingfa3ad862013-11-02 17:07:09 +0000363}
364
Russell King7bedaa52012-04-13 12:10:24 +0100365static void omap_dma_start_sg(struct omap_chan *c, struct omap_desc *d,
366 unsigned idx)
367{
368 struct omap_sg *sg = d->sg + idx;
Russell King893e63e2013-11-03 11:17:11 +0000369 unsigned cxsa, cxei, cxfi;
Russell King7bedaa52012-04-13 12:10:24 +0100370
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +0300371 if (d->dir == DMA_DEV_TO_MEM || d->dir == DMA_MEM_TO_MEM) {
Russell King893e63e2013-11-03 11:17:11 +0000372 cxsa = CDSA;
373 cxei = CDEI;
374 cxfi = CDFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000375 } else {
Russell King893e63e2013-11-03 11:17:11 +0000376 cxsa = CSSA;
377 cxei = CSEI;
378 cxfi = CSFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000379 }
380
Russell Kingc5ed98b2013-11-06 17:33:09 +0000381 omap_dma_chan_write(c, cxsa, sg->addr);
382 omap_dma_chan_write(c, cxei, 0);
383 omap_dma_chan_write(c, cxfi, 0);
384 omap_dma_chan_write(c, CEN, sg->en);
385 omap_dma_chan_write(c, CFN, sg->fn);
Russell King7bedaa52012-04-13 12:10:24 +0100386
Russell Kingfa3ad862013-11-02 17:07:09 +0000387 omap_dma_start(c, d);
Russell King7bedaa52012-04-13 12:10:24 +0100388}
389
390static void omap_dma_start_desc(struct omap_chan *c)
391{
392 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
393 struct omap_desc *d;
Russell King893e63e2013-11-03 11:17:11 +0000394 unsigned cxsa, cxei, cxfi;
Russell King7bedaa52012-04-13 12:10:24 +0100395
396 if (!vd) {
397 c->desc = NULL;
398 return;
399 }
400
401 list_del(&vd->node);
402
403 c->desc = d = to_omap_dma_desc(&vd->tx);
404 c->sgidx = 0;
405
Russell King59871902013-11-06 17:15:16 +0000406 /*
407 * This provides the necessary barrier to ensure data held in
408 * DMA coherent memory is visible to the DMA engine prior to
409 * the transfer starting.
410 */
411 mb();
412
Russell Kingc5ed98b2013-11-06 17:33:09 +0000413 omap_dma_chan_write(c, CCR, d->ccr);
Russell King3ed4d182013-11-02 19:16:09 +0000414 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000415 omap_dma_chan_write(c, CCR2, d->ccr >> 16);
Russell Kingb9e97822013-11-02 13:26:57 +0000416
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +0300417 if (d->dir == DMA_DEV_TO_MEM || d->dir == DMA_MEM_TO_MEM) {
Russell King893e63e2013-11-03 11:17:11 +0000418 cxsa = CSSA;
419 cxei = CSEI;
420 cxfi = CSFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000421 } else {
Russell King893e63e2013-11-03 11:17:11 +0000422 cxsa = CDSA;
423 cxei = CDEI;
424 cxfi = CDFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000425 }
Russell King7bedaa52012-04-13 12:10:24 +0100426
Russell Kingc5ed98b2013-11-06 17:33:09 +0000427 omap_dma_chan_write(c, cxsa, d->dev_addr);
428 omap_dma_chan_write(c, cxei, 0);
429 omap_dma_chan_write(c, cxfi, d->fi);
430 omap_dma_chan_write(c, CSDP, d->csdp);
431 omap_dma_chan_write(c, CLNK_CTRL, d->clnk_ctrl);
Russell King913a2d02013-11-02 14:41:42 +0000432
Russell King7bedaa52012-04-13 12:10:24 +0100433 omap_dma_start_sg(c, d, 0);
434}
435
436static void omap_dma_callback(int ch, u16 status, void *data)
437{
438 struct omap_chan *c = data;
439 struct omap_desc *d;
440 unsigned long flags;
441
442 spin_lock_irqsave(&c->vc.lock, flags);
443 d = c->desc;
444 if (d) {
Russell King3a774ea2012-06-21 10:40:15 +0100445 if (!c->cyclic) {
446 if (++c->sgidx < d->sglen) {
447 omap_dma_start_sg(c, d, c->sgidx);
448 } else {
449 omap_dma_start_desc(c);
450 vchan_cookie_complete(&d->vd);
451 }
Russell King7bedaa52012-04-13 12:10:24 +0100452 } else {
Russell King3a774ea2012-06-21 10:40:15 +0100453 vchan_cyclic_callback(&d->vd);
Russell King7bedaa52012-04-13 12:10:24 +0100454 }
455 }
456 spin_unlock_irqrestore(&c->vc.lock, flags);
457}
458
Russell King6ddeb6d2013-12-10 19:05:50 +0000459static irqreturn_t omap_dma_irq(int irq, void *devid)
460{
461 struct omap_dmadev *od = devid;
462 unsigned status, channel;
463
464 spin_lock(&od->irq_lock);
465
466 status = omap_dma_glbl_read(od, IRQSTATUS_L1);
467 status &= od->irq_enable_mask;
468 if (status == 0) {
469 spin_unlock(&od->irq_lock);
470 return IRQ_NONE;
471 }
472
473 while ((channel = ffs(status)) != 0) {
474 unsigned mask, csr;
475 struct omap_chan *c;
476
477 channel -= 1;
478 mask = BIT(channel);
479 status &= ~mask;
480
481 c = od->lch_map[channel];
482 if (c == NULL) {
483 /* This should never happen */
484 dev_err(od->ddev.dev, "invalid channel %u\n", channel);
485 continue;
486 }
487
488 csr = omap_dma_get_csr(c);
489 omap_dma_glbl_write(od, IRQSTATUS_L1, mask);
490
491 omap_dma_callback(channel, csr, c);
492 }
493
494 spin_unlock(&od->irq_lock);
495
496 return IRQ_HANDLED;
497}
498
Russell King7bedaa52012-04-13 12:10:24 +0100499static int omap_dma_alloc_chan_resources(struct dma_chan *chan)
500{
Russell King596c4712013-12-10 11:08:01 +0000501 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King7bedaa52012-04-13 12:10:24 +0100502 struct omap_chan *c = to_omap_dma_chan(chan);
Russell King596c4712013-12-10 11:08:01 +0000503 int ret;
Russell King7bedaa52012-04-13 12:10:24 +0100504
Russell King6ddeb6d2013-12-10 19:05:50 +0000505 if (od->legacy) {
506 ret = omap_request_dma(c->dma_sig, "DMA engine",
507 omap_dma_callback, c, &c->dma_ch);
508 } else {
509 ret = omap_request_dma(c->dma_sig, "DMA engine", NULL, NULL,
510 &c->dma_ch);
511 }
Russell King7bedaa52012-04-13 12:10:24 +0100512
Russell King6ddeb6d2013-12-10 19:05:50 +0000513 dev_dbg(od->ddev.dev, "allocating channel %u for %u\n",
514 c->dma_ch, c->dma_sig);
Russell King596c4712013-12-10 11:08:01 +0000515
Russell King6ddeb6d2013-12-10 19:05:50 +0000516 if (ret >= 0) {
Russell King596c4712013-12-10 11:08:01 +0000517 omap_dma_assign(od, c, c->dma_ch);
518
Russell King6ddeb6d2013-12-10 19:05:50 +0000519 if (!od->legacy) {
520 unsigned val;
521
522 spin_lock_irq(&od->irq_lock);
523 val = BIT(c->dma_ch);
524 omap_dma_glbl_write(od, IRQSTATUS_L1, val);
525 od->irq_enable_mask |= val;
526 omap_dma_glbl_write(od, IRQENABLE_L1, od->irq_enable_mask);
527
528 val = omap_dma_glbl_read(od, IRQENABLE_L0);
529 val &= ~BIT(c->dma_ch);
530 omap_dma_glbl_write(od, IRQENABLE_L0, val);
531 spin_unlock_irq(&od->irq_lock);
532 }
533 }
534
Russell Kingaa4c5b92014-01-14 23:58:10 +0000535 if (dma_omap1()) {
536 if (__dma_omap16xx(od->plat->dma_attr)) {
537 c->ccr = CCR_OMAP31_DISABLE;
538 /* Duplicate what plat-omap/dma.c does */
539 c->ccr |= c->dma_ch + 1;
540 } else {
541 c->ccr = c->dma_sig & 0x1f;
542 }
543 } else {
544 c->ccr = c->dma_sig & 0x1f;
545 c->ccr |= (c->dma_sig & ~0x1f) << 14;
546 }
547 if (od->plat->errata & DMA_ERRATA_IFRAME_BUFFERING)
548 c->ccr |= CCR_BUFFERING_DISABLE;
549
Russell King596c4712013-12-10 11:08:01 +0000550 return ret;
Russell King7bedaa52012-04-13 12:10:24 +0100551}
552
553static void omap_dma_free_chan_resources(struct dma_chan *chan)
554{
Russell King6ddeb6d2013-12-10 19:05:50 +0000555 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King7bedaa52012-04-13 12:10:24 +0100556 struct omap_chan *c = to_omap_dma_chan(chan);
557
Russell King6ddeb6d2013-12-10 19:05:50 +0000558 if (!od->legacy) {
559 spin_lock_irq(&od->irq_lock);
560 od->irq_enable_mask &= ~BIT(c->dma_ch);
561 omap_dma_glbl_write(od, IRQENABLE_L1, od->irq_enable_mask);
562 spin_unlock_irq(&od->irq_lock);
563 }
564
Russell King596c4712013-12-10 11:08:01 +0000565 c->channel_base = NULL;
Russell King6ddeb6d2013-12-10 19:05:50 +0000566 od->lch_map[c->dma_ch] = NULL;
Russell King7bedaa52012-04-13 12:10:24 +0100567 vchan_free_chan_resources(&c->vc);
568 omap_free_dma(c->dma_ch);
569
Russell King6ddeb6d2013-12-10 19:05:50 +0000570 dev_dbg(od->ddev.dev, "freeing channel for %u\n", c->dma_sig);
Peter Ujfalusieea531e2015-04-09 12:35:52 +0300571 c->dma_sig = 0;
Russell King7bedaa52012-04-13 12:10:24 +0100572}
573
Russell King3850e222012-06-21 10:37:35 +0100574static size_t omap_dma_sg_size(struct omap_sg *sg)
575{
576 return sg->en * sg->fn;
577}
578
579static size_t omap_dma_desc_size(struct omap_desc *d)
580{
581 unsigned i;
582 size_t size;
583
584 for (size = i = 0; i < d->sglen; i++)
585 size += omap_dma_sg_size(&d->sg[i]);
586
587 return size * es_bytes[d->es];
588}
589
590static size_t omap_dma_desc_size_pos(struct omap_desc *d, dma_addr_t addr)
591{
592 unsigned i;
593 size_t size, es_size = es_bytes[d->es];
594
595 for (size = i = 0; i < d->sglen; i++) {
596 size_t this_size = omap_dma_sg_size(&d->sg[i]) * es_size;
597
598 if (size)
599 size += this_size;
600 else if (addr >= d->sg[i].addr &&
601 addr < d->sg[i].addr + this_size)
602 size += d->sg[i].addr + this_size - addr;
603 }
604 return size;
605}
606
Russell Kingb07fd622013-11-06 19:26:45 +0000607/*
608 * OMAP 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
609 * read before the DMA controller finished disabling the channel.
610 */
611static uint32_t omap_dma_chan_read_3_3(struct omap_chan *c, unsigned reg)
612{
613 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
614 uint32_t val;
615
616 val = omap_dma_chan_read(c, reg);
617 if (val == 0 && od->plat->errata & DMA_ERRATA_3_3)
618 val = omap_dma_chan_read(c, reg);
619
620 return val;
621}
622
Russell King3997cab2013-11-02 18:04:17 +0000623static dma_addr_t omap_dma_get_src_pos(struct omap_chan *c)
624{
625 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
Russell Kingb07fd622013-11-06 19:26:45 +0000626 dma_addr_t addr, cdac;
Russell King3997cab2013-11-02 18:04:17 +0000627
Russell Kingb07fd622013-11-06 19:26:45 +0000628 if (__dma_omap15xx(od->plat->dma_attr)) {
Russell Kingc5ed98b2013-11-06 17:33:09 +0000629 addr = omap_dma_chan_read(c, CPC);
Russell Kingb07fd622013-11-06 19:26:45 +0000630 } else {
631 addr = omap_dma_chan_read_3_3(c, CSAC);
632 cdac = omap_dma_chan_read_3_3(c, CDAC);
Russell King3997cab2013-11-02 18:04:17 +0000633
Russell King3997cab2013-11-02 18:04:17 +0000634 /*
635 * CDAC == 0 indicates that the DMA transfer on the channel has
636 * not been started (no data has been transferred so far).
637 * Return the programmed source start address in this case.
638 */
Russell Kingb07fd622013-11-06 19:26:45 +0000639 if (cdac == 0)
Russell Kingc5ed98b2013-11-06 17:33:09 +0000640 addr = omap_dma_chan_read(c, CSSA);
Russell King3997cab2013-11-02 18:04:17 +0000641 }
642
643 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000644 addr |= omap_dma_chan_read(c, CSSA) & 0xffff0000;
Russell King3997cab2013-11-02 18:04:17 +0000645
646 return addr;
647}
648
649static dma_addr_t omap_dma_get_dst_pos(struct omap_chan *c)
650{
651 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
652 dma_addr_t addr;
653
Russell Kingb07fd622013-11-06 19:26:45 +0000654 if (__dma_omap15xx(od->plat->dma_attr)) {
Russell Kingc5ed98b2013-11-06 17:33:09 +0000655 addr = omap_dma_chan_read(c, CPC);
Russell Kingb07fd622013-11-06 19:26:45 +0000656 } else {
657 addr = omap_dma_chan_read_3_3(c, CDAC);
Russell King3997cab2013-11-02 18:04:17 +0000658
Russell King3997cab2013-11-02 18:04:17 +0000659 /*
Russell Kingb07fd622013-11-06 19:26:45 +0000660 * CDAC == 0 indicates that the DMA transfer on the channel
661 * has not been started (no data has been transferred so
662 * far). Return the programmed destination start address in
663 * this case.
Russell King3997cab2013-11-02 18:04:17 +0000664 */
665 if (addr == 0)
Russell Kingc5ed98b2013-11-06 17:33:09 +0000666 addr = omap_dma_chan_read(c, CDSA);
Russell King3997cab2013-11-02 18:04:17 +0000667 }
668
669 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000670 addr |= omap_dma_chan_read(c, CDSA) & 0xffff0000;
Russell King3997cab2013-11-02 18:04:17 +0000671
672 return addr;
673}
674
Russell King7bedaa52012-04-13 12:10:24 +0100675static enum dma_status omap_dma_tx_status(struct dma_chan *chan,
676 dma_cookie_t cookie, struct dma_tx_state *txstate)
677{
Russell King3850e222012-06-21 10:37:35 +0100678 struct omap_chan *c = to_omap_dma_chan(chan);
679 struct virt_dma_desc *vd;
680 enum dma_status ret;
681 unsigned long flags;
682
683 ret = dma_cookie_status(chan, cookie, txstate);
Peter Ujfalusi689d3c52016-04-05 15:20:20 +0300684
685 if (!c->paused && c->running) {
686 uint32_t ccr = omap_dma_chan_read(c, CCR);
687 /*
688 * The channel is no longer active, set the return value
689 * accordingly
690 */
691 if (!(ccr & CCR_ENABLE))
692 ret = DMA_COMPLETE;
693 }
694
Vinod Koul7cce5082013-10-16 20:51:54 +0530695 if (ret == DMA_COMPLETE || !txstate)
Russell King3850e222012-06-21 10:37:35 +0100696 return ret;
697
698 spin_lock_irqsave(&c->vc.lock, flags);
699 vd = vchan_find_desc(&c->vc, cookie);
700 if (vd) {
701 txstate->residue = omap_dma_desc_size(to_omap_dma_desc(&vd->tx));
702 } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
703 struct omap_desc *d = c->desc;
704 dma_addr_t pos;
705
706 if (d->dir == DMA_MEM_TO_DEV)
Russell King3997cab2013-11-02 18:04:17 +0000707 pos = omap_dma_get_src_pos(c);
Peter Ujfalusiadf850b2015-11-11 12:37:55 +0200708 else if (d->dir == DMA_DEV_TO_MEM || d->dir == DMA_MEM_TO_MEM)
Russell King3997cab2013-11-02 18:04:17 +0000709 pos = omap_dma_get_dst_pos(c);
Russell King3850e222012-06-21 10:37:35 +0100710 else
711 pos = 0;
712
713 txstate->residue = omap_dma_desc_size_pos(d, pos);
714 } else {
715 txstate->residue = 0;
716 }
717 spin_unlock_irqrestore(&c->vc.lock, flags);
718
719 return ret;
Russell King7bedaa52012-04-13 12:10:24 +0100720}
721
722static void omap_dma_issue_pending(struct dma_chan *chan)
723{
724 struct omap_chan *c = to_omap_dma_chan(chan);
725 unsigned long flags;
726
727 spin_lock_irqsave(&c->vc.lock, flags);
Peter Ujfalusi1c1d25f2015-11-11 12:37:57 +0200728 if (vchan_issue_pending(&c->vc) && !c->desc)
729 omap_dma_start_desc(c);
Russell King7bedaa52012-04-13 12:10:24 +0100730 spin_unlock_irqrestore(&c->vc.lock, flags);
731}
732
733static struct dma_async_tx_descriptor *omap_dma_prep_slave_sg(
734 struct dma_chan *chan, struct scatterlist *sgl, unsigned sglen,
735 enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
736{
Russell King49ae0b22013-11-02 21:09:18 +0000737 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King7bedaa52012-04-13 12:10:24 +0100738 struct omap_chan *c = to_omap_dma_chan(chan);
739 enum dma_slave_buswidth dev_width;
740 struct scatterlist *sgent;
741 struct omap_desc *d;
742 dma_addr_t dev_addr;
Peter Ujfalusie8a5e792015-11-11 12:37:56 +0200743 unsigned i, es, en, frame_bytes;
Russell King7bedaa52012-04-13 12:10:24 +0100744 u32 burst;
745
746 if (dir == DMA_DEV_TO_MEM) {
747 dev_addr = c->cfg.src_addr;
748 dev_width = c->cfg.src_addr_width;
749 burst = c->cfg.src_maxburst;
Russell King7bedaa52012-04-13 12:10:24 +0100750 } else if (dir == DMA_MEM_TO_DEV) {
751 dev_addr = c->cfg.dst_addr;
752 dev_width = c->cfg.dst_addr_width;
753 burst = c->cfg.dst_maxburst;
Russell King7bedaa52012-04-13 12:10:24 +0100754 } else {
755 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
756 return NULL;
757 }
758
759 /* Bus width translates to the element size (ES) */
760 switch (dev_width) {
761 case DMA_SLAVE_BUSWIDTH_1_BYTE:
Russell King90438262013-11-02 19:57:06 +0000762 es = CSDP_DATA_TYPE_8;
Russell King7bedaa52012-04-13 12:10:24 +0100763 break;
764 case DMA_SLAVE_BUSWIDTH_2_BYTES:
Russell King90438262013-11-02 19:57:06 +0000765 es = CSDP_DATA_TYPE_16;
Russell King7bedaa52012-04-13 12:10:24 +0100766 break;
767 case DMA_SLAVE_BUSWIDTH_4_BYTES:
Russell King90438262013-11-02 19:57:06 +0000768 es = CSDP_DATA_TYPE_32;
Russell King7bedaa52012-04-13 12:10:24 +0100769 break;
770 default: /* not reached */
771 return NULL;
772 }
773
774 /* Now allocate and setup the descriptor. */
775 d = kzalloc(sizeof(*d) + sglen * sizeof(d->sg[0]), GFP_ATOMIC);
776 if (!d)
777 return NULL;
778
779 d->dir = dir;
780 d->dev_addr = dev_addr;
781 d->es = es;
Russell King3ed4d182013-11-02 19:16:09 +0000782
Russell Kingaa4c5b92014-01-14 23:58:10 +0000783 d->ccr = c->ccr | CCR_SYNC_FRAME;
Russell King3ed4d182013-11-02 19:16:09 +0000784 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000785 d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_CONSTANT;
Russell King3ed4d182013-11-02 19:16:09 +0000786 else
Russell King90438262013-11-02 19:57:06 +0000787 d->ccr |= CCR_DST_AMODE_CONSTANT | CCR_SRC_AMODE_POSTINC;
Russell King3ed4d182013-11-02 19:16:09 +0000788
Russell King90438262013-11-02 19:57:06 +0000789 d->cicr = CICR_DROP_IE | CICR_BLOCK_IE;
Russell King2f0d13b2013-11-02 18:51:53 +0000790 d->csdp = es;
Russell Kingfa3ad862013-11-02 17:07:09 +0000791
Russell King2f0d13b2013-11-02 18:51:53 +0000792 if (dma_omap1()) {
Russell King90438262013-11-02 19:57:06 +0000793 d->cicr |= CICR_TOUT_IE;
Russell King2f0d13b2013-11-02 18:51:53 +0000794
795 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000796 d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_TIPB;
Russell King2f0d13b2013-11-02 18:51:53 +0000797 else
Russell King90438262013-11-02 19:57:06 +0000798 d->csdp |= CSDP_DST_PORT_TIPB | CSDP_SRC_PORT_EMIFF;
Russell King2f0d13b2013-11-02 18:51:53 +0000799 } else {
Russell King3ed4d182013-11-02 19:16:09 +0000800 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000801 d->ccr |= CCR_TRIGGER_SRC;
Russell King3ed4d182013-11-02 19:16:09 +0000802
Russell King90438262013-11-02 19:57:06 +0000803 d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
Russell King2f0d13b2013-11-02 18:51:53 +0000804 }
Russell King965aeb4d2013-11-06 17:12:30 +0000805 if (od->plat->errata & DMA_ERRATA_PARALLEL_CHANNELS)
806 d->clnk_ctrl = c->dma_ch;
Russell King7bedaa52012-04-13 12:10:24 +0100807
808 /*
809 * Build our scatterlist entries: each contains the address,
810 * the number of elements (EN) in each frame, and the number of
811 * frames (FN). Number of bytes for this entry = ES * EN * FN.
812 *
813 * Burst size translates to number of elements with frame sync.
814 * Note: DMA engine defines burst to be the number of dev-width
815 * transfers.
816 */
817 en = burst;
818 frame_bytes = es_bytes[es] * en;
819 for_each_sg(sgl, sgent, sglen, i) {
Peter Ujfalusie8a5e792015-11-11 12:37:56 +0200820 d->sg[i].addr = sg_dma_address(sgent);
821 d->sg[i].en = en;
822 d->sg[i].fn = sg_dma_len(sgent) / frame_bytes;
Russell King7bedaa52012-04-13 12:10:24 +0100823 }
824
Peter Ujfalusie8a5e792015-11-11 12:37:56 +0200825 d->sglen = sglen;
Russell King7bedaa52012-04-13 12:10:24 +0100826
827 return vchan_tx_prep(&c->vc, &d->vd, tx_flags);
828}
829
Russell King3a774ea2012-06-21 10:40:15 +0100830static struct dma_async_tx_descriptor *omap_dma_prep_dma_cyclic(
831 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +0200832 size_t period_len, enum dma_transfer_direction dir, unsigned long flags)
Russell King3a774ea2012-06-21 10:40:15 +0100833{
Russell Kingfa3ad862013-11-02 17:07:09 +0000834 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King3a774ea2012-06-21 10:40:15 +0100835 struct omap_chan *c = to_omap_dma_chan(chan);
836 enum dma_slave_buswidth dev_width;
837 struct omap_desc *d;
838 dma_addr_t dev_addr;
Russell King3ed4d182013-11-02 19:16:09 +0000839 unsigned es;
Russell King3a774ea2012-06-21 10:40:15 +0100840 u32 burst;
841
842 if (dir == DMA_DEV_TO_MEM) {
843 dev_addr = c->cfg.src_addr;
844 dev_width = c->cfg.src_addr_width;
845 burst = c->cfg.src_maxburst;
Russell King3a774ea2012-06-21 10:40:15 +0100846 } else if (dir == DMA_MEM_TO_DEV) {
847 dev_addr = c->cfg.dst_addr;
848 dev_width = c->cfg.dst_addr_width;
849 burst = c->cfg.dst_maxburst;
Russell King3a774ea2012-06-21 10:40:15 +0100850 } else {
851 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
852 return NULL;
853 }
854
855 /* Bus width translates to the element size (ES) */
856 switch (dev_width) {
857 case DMA_SLAVE_BUSWIDTH_1_BYTE:
Russell King90438262013-11-02 19:57:06 +0000858 es = CSDP_DATA_TYPE_8;
Russell King3a774ea2012-06-21 10:40:15 +0100859 break;
860 case DMA_SLAVE_BUSWIDTH_2_BYTES:
Russell King90438262013-11-02 19:57:06 +0000861 es = CSDP_DATA_TYPE_16;
Russell King3a774ea2012-06-21 10:40:15 +0100862 break;
863 case DMA_SLAVE_BUSWIDTH_4_BYTES:
Russell King90438262013-11-02 19:57:06 +0000864 es = CSDP_DATA_TYPE_32;
Russell King3a774ea2012-06-21 10:40:15 +0100865 break;
866 default: /* not reached */
867 return NULL;
868 }
869
870 /* Now allocate and setup the descriptor. */
871 d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC);
872 if (!d)
873 return NULL;
874
875 d->dir = dir;
876 d->dev_addr = dev_addr;
877 d->fi = burst;
878 d->es = es;
Russell King3a774ea2012-06-21 10:40:15 +0100879 d->sg[0].addr = buf_addr;
880 d->sg[0].en = period_len / es_bytes[es];
881 d->sg[0].fn = buf_len / period_len;
882 d->sglen = 1;
Russell King3ed4d182013-11-02 19:16:09 +0000883
Russell Kingaa4c5b92014-01-14 23:58:10 +0000884 d->ccr = c->ccr;
Russell King3ed4d182013-11-02 19:16:09 +0000885 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000886 d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_CONSTANT;
Russell King3ed4d182013-11-02 19:16:09 +0000887 else
Russell King90438262013-11-02 19:57:06 +0000888 d->ccr |= CCR_DST_AMODE_CONSTANT | CCR_SRC_AMODE_POSTINC;
Russell King3ed4d182013-11-02 19:16:09 +0000889
Russell King90438262013-11-02 19:57:06 +0000890 d->cicr = CICR_DROP_IE;
Russell Kingfa3ad862013-11-02 17:07:09 +0000891 if (flags & DMA_PREP_INTERRUPT)
Russell King90438262013-11-02 19:57:06 +0000892 d->cicr |= CICR_FRAME_IE;
Russell Kingfa3ad862013-11-02 17:07:09 +0000893
Russell King2f0d13b2013-11-02 18:51:53 +0000894 d->csdp = es;
895
896 if (dma_omap1()) {
Russell King90438262013-11-02 19:57:06 +0000897 d->cicr |= CICR_TOUT_IE;
Russell King2f0d13b2013-11-02 18:51:53 +0000898
899 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000900 d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_MPUI;
Russell King2f0d13b2013-11-02 18:51:53 +0000901 else
Russell King90438262013-11-02 19:57:06 +0000902 d->csdp |= CSDP_DST_PORT_MPUI | CSDP_SRC_PORT_EMIFF;
Russell King2f0d13b2013-11-02 18:51:53 +0000903 } else {
Russell King3ed4d182013-11-02 19:16:09 +0000904 if (burst)
Russell King90438262013-11-02 19:57:06 +0000905 d->ccr |= CCR_SYNC_PACKET;
906 else
907 d->ccr |= CCR_SYNC_ELEMENT;
Russell King3ed4d182013-11-02 19:16:09 +0000908
Misael Lopez Cruz47fac242015-09-14 15:31:05 +0300909 if (dir == DMA_DEV_TO_MEM) {
Russell King90438262013-11-02 19:57:06 +0000910 d->ccr |= CCR_TRIGGER_SRC;
Misael Lopez Cruz47fac242015-09-14 15:31:05 +0300911 d->csdp |= CSDP_DST_PACKED;
912 } else {
913 d->csdp |= CSDP_SRC_PACKED;
914 }
Russell King3ed4d182013-11-02 19:16:09 +0000915
Russell King90438262013-11-02 19:57:06 +0000916 d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
Russell King3a774ea2012-06-21 10:40:15 +0100917
Russell King90438262013-11-02 19:57:06 +0000918 d->csdp |= CSDP_DST_BURST_64 | CSDP_SRC_BURST_64;
Russell King2f0d13b2013-11-02 18:51:53 +0000919 }
920
Russell King965aeb4d2013-11-06 17:12:30 +0000921 if (__dma_omap15xx(od->plat->dma_attr))
922 d->ccr |= CCR_AUTO_INIT | CCR_REPEAT;
923 else
924 d->clnk_ctrl = c->dma_ch | CLNK_CTRL_ENABLE_LNK;
925
Russell King3ed4d182013-11-02 19:16:09 +0000926 c->cyclic = true;
Russell King3a774ea2012-06-21 10:40:15 +0100927
Peter Ujfalusi2dde5b92012-09-14 15:05:48 +0300928 return vchan_tx_prep(&c->vc, &d->vd, flags);
Russell King3a774ea2012-06-21 10:40:15 +0100929}
930
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +0300931static struct dma_async_tx_descriptor *omap_dma_prep_dma_memcpy(
932 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
933 size_t len, unsigned long tx_flags)
934{
935 struct omap_chan *c = to_omap_dma_chan(chan);
936 struct omap_desc *d;
937 uint8_t data_type;
938
939 d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC);
940 if (!d)
941 return NULL;
942
943 data_type = __ffs((src | dest | len));
944 if (data_type > CSDP_DATA_TYPE_32)
945 data_type = CSDP_DATA_TYPE_32;
946
947 d->dir = DMA_MEM_TO_MEM;
948 d->dev_addr = src;
949 d->fi = 0;
950 d->es = data_type;
951 d->sg[0].en = len / BIT(data_type);
952 d->sg[0].fn = 1;
953 d->sg[0].addr = dest;
954 d->sglen = 1;
955 d->ccr = c->ccr;
956 d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_POSTINC;
957
Peter Ujfalusib96c0332016-04-05 15:20:21 +0300958 d->cicr = CICR_DROP_IE | CICR_FRAME_IE;
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +0300959
960 d->csdp = data_type;
961
962 if (dma_omap1()) {
963 d->cicr |= CICR_TOUT_IE;
964 d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_EMIFF;
965 } else {
966 d->csdp |= CSDP_DST_PACKED | CSDP_SRC_PACKED;
967 d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
968 d->csdp |= CSDP_DST_BURST_64 | CSDP_SRC_BURST_64;
969 }
970
971 return vchan_tx_prep(&c->vc, &d->vd, tx_flags);
972}
973
Maxime Ripard78ea4fe2014-11-17 14:42:28 +0100974static int omap_dma_slave_config(struct dma_chan *chan, struct dma_slave_config *cfg)
Russell King7bedaa52012-04-13 12:10:24 +0100975{
Maxime Ripard78ea4fe2014-11-17 14:42:28 +0100976 struct omap_chan *c = to_omap_dma_chan(chan);
977
Russell King7bedaa52012-04-13 12:10:24 +0100978 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
979 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
980 return -EINVAL;
981
982 memcpy(&c->cfg, cfg, sizeof(c->cfg));
983
984 return 0;
985}
986
Maxime Ripard78ea4fe2014-11-17 14:42:28 +0100987static int omap_dma_terminate_all(struct dma_chan *chan)
Russell King7bedaa52012-04-13 12:10:24 +0100988{
Maxime Ripard78ea4fe2014-11-17 14:42:28 +0100989 struct omap_chan *c = to_omap_dma_chan(chan);
Russell King7bedaa52012-04-13 12:10:24 +0100990 unsigned long flags;
991 LIST_HEAD(head);
992
993 spin_lock_irqsave(&c->vc.lock, flags);
994
Russell King7bedaa52012-04-13 12:10:24 +0100995 /*
996 * Stop DMA activity: we assume the callback will not be called
Russell Kingfa3ad862013-11-02 17:07:09 +0000997 * after omap_dma_stop() returns (even if it does, it will see
Russell King7bedaa52012-04-13 12:10:24 +0100998 * c->desc is NULL and exit.)
999 */
1000 if (c->desc) {
Peter Ujfalusi02d88b72015-03-27 13:35:52 +02001001 omap_dma_desc_free(&c->desc->vd);
Russell King7bedaa52012-04-13 12:10:24 +01001002 c->desc = NULL;
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001003 /* Avoid stopping the dma twice */
1004 if (!c->paused)
Russell Kingfa3ad862013-11-02 17:07:09 +00001005 omap_dma_stop(c);
Russell King7bedaa52012-04-13 12:10:24 +01001006 }
1007
Russell King3a774ea2012-06-21 10:40:15 +01001008 if (c->cyclic) {
1009 c->cyclic = false;
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001010 c->paused = false;
Russell King3a774ea2012-06-21 10:40:15 +01001011 }
1012
Russell King7bedaa52012-04-13 12:10:24 +01001013 vchan_get_all_descriptors(&c->vc, &head);
1014 spin_unlock_irqrestore(&c->vc.lock, flags);
1015 vchan_dma_desc_free_list(&c->vc, &head);
1016
1017 return 0;
1018}
1019
Peter Ujfalusi9bef6d82016-02-11 11:08:34 +02001020static void omap_dma_synchronize(struct dma_chan *chan)
1021{
1022 struct omap_chan *c = to_omap_dma_chan(chan);
1023
1024 vchan_synchronize(&c->vc);
1025}
1026
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001027static int omap_dma_pause(struct dma_chan *chan)
Russell King7bedaa52012-04-13 12:10:24 +01001028{
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001029 struct omap_chan *c = to_omap_dma_chan(chan);
1030
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001031 /* Pause/Resume only allowed with cyclic mode */
1032 if (!c->cyclic)
1033 return -EINVAL;
1034
1035 if (!c->paused) {
Russell Kingfa3ad862013-11-02 17:07:09 +00001036 omap_dma_stop(c);
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001037 c->paused = true;
1038 }
1039
1040 return 0;
Russell King7bedaa52012-04-13 12:10:24 +01001041}
1042
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001043static int omap_dma_resume(struct dma_chan *chan)
Russell King7bedaa52012-04-13 12:10:24 +01001044{
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001045 struct omap_chan *c = to_omap_dma_chan(chan);
1046
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001047 /* Pause/Resume only allowed with cyclic mode */
1048 if (!c->cyclic)
1049 return -EINVAL;
1050
1051 if (c->paused) {
Peter Ujfalusib3d09da2014-09-16 22:45:56 +03001052 mb();
1053
Peter Ujfalusibfb60742014-09-16 22:45:57 +03001054 /* Restore channel link register */
1055 omap_dma_chan_write(c, CLNK_CTRL, c->desc->clnk_ctrl);
1056
Russell Kingfa3ad862013-11-02 17:07:09 +00001057 omap_dma_start(c, c->desc);
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001058 c->paused = false;
1059 }
1060
1061 return 0;
Russell King7bedaa52012-04-13 12:10:24 +01001062}
1063
Peter Ujfalusieea531e2015-04-09 12:35:52 +03001064static int omap_dma_chan_init(struct omap_dmadev *od)
Russell King7bedaa52012-04-13 12:10:24 +01001065{
1066 struct omap_chan *c;
1067
1068 c = kzalloc(sizeof(*c), GFP_KERNEL);
1069 if (!c)
1070 return -ENOMEM;
1071
Russell King596c4712013-12-10 11:08:01 +00001072 c->reg_map = od->reg_map;
Russell King7bedaa52012-04-13 12:10:24 +01001073 c->vc.desc_free = omap_dma_desc_free;
1074 vchan_init(&c->vc, &od->ddev);
Russell King7bedaa52012-04-13 12:10:24 +01001075
Russell King7bedaa52012-04-13 12:10:24 +01001076 return 0;
1077}
1078
1079static void omap_dma_free(struct omap_dmadev *od)
1080{
Russell King7bedaa52012-04-13 12:10:24 +01001081 while (!list_empty(&od->ddev.channels)) {
1082 struct omap_chan *c = list_first_entry(&od->ddev.channels,
1083 struct omap_chan, vc.chan.device_node);
1084
1085 list_del(&c->vc.chan.device_node);
1086 tasklet_kill(&c->vc.task);
1087 kfree(c);
1088 }
Russell King7bedaa52012-04-13 12:10:24 +01001089}
1090
Peter Ujfalusi80b0e0a2014-03-29 19:03:30 +05301091#define OMAP_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
1092 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
1093 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
1094
Russell King7bedaa52012-04-13 12:10:24 +01001095static int omap_dma_probe(struct platform_device *pdev)
1096{
1097 struct omap_dmadev *od;
Russell King596c4712013-12-10 11:08:01 +00001098 struct resource *res;
Russell King6ddeb6d2013-12-10 19:05:50 +00001099 int rc, i, irq;
Russell King7bedaa52012-04-13 12:10:24 +01001100
Russell King104fce72013-11-02 12:58:29 +00001101 od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
Russell King7bedaa52012-04-13 12:10:24 +01001102 if (!od)
1103 return -ENOMEM;
1104
Russell King596c4712013-12-10 11:08:01 +00001105 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1106 od->base = devm_ioremap_resource(&pdev->dev, res);
1107 if (IS_ERR(od->base))
1108 return PTR_ERR(od->base);
1109
Russell King1b416c42013-11-02 13:00:03 +00001110 od->plat = omap_get_plat_info();
1111 if (!od->plat)
1112 return -EPROBE_DEFER;
1113
Russell King596c4712013-12-10 11:08:01 +00001114 od->reg_map = od->plat->reg_map;
1115
Russell King7bedaa52012-04-13 12:10:24 +01001116 dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
Russell King3a774ea2012-06-21 10:40:15 +01001117 dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +03001118 dma_cap_set(DMA_MEMCPY, od->ddev.cap_mask);
Russell King7bedaa52012-04-13 12:10:24 +01001119 od->ddev.device_alloc_chan_resources = omap_dma_alloc_chan_resources;
1120 od->ddev.device_free_chan_resources = omap_dma_free_chan_resources;
1121 od->ddev.device_tx_status = omap_dma_tx_status;
1122 od->ddev.device_issue_pending = omap_dma_issue_pending;
1123 od->ddev.device_prep_slave_sg = omap_dma_prep_slave_sg;
Russell King3a774ea2012-06-21 10:40:15 +01001124 od->ddev.device_prep_dma_cyclic = omap_dma_prep_dma_cyclic;
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +03001125 od->ddev.device_prep_dma_memcpy = omap_dma_prep_dma_memcpy;
Vinod Koul6c04cd42014-12-07 23:12:31 +05301126 od->ddev.device_config = omap_dma_slave_config;
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001127 od->ddev.device_pause = omap_dma_pause;
1128 od->ddev.device_resume = omap_dma_resume;
1129 od->ddev.device_terminate_all = omap_dma_terminate_all;
Peter Ujfalusi9bef6d82016-02-11 11:08:34 +02001130 od->ddev.device_synchronize = omap_dma_synchronize;
Maxime Ripard7d15b872014-11-17 14:42:49 +01001131 od->ddev.src_addr_widths = OMAP_DMA_BUSWIDTHS;
1132 od->ddev.dst_addr_widths = OMAP_DMA_BUSWIDTHS;
1133 od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1134 od->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Russell King7bedaa52012-04-13 12:10:24 +01001135 od->ddev.dev = &pdev->dev;
1136 INIT_LIST_HEAD(&od->ddev.channels);
Russell King7bedaa52012-04-13 12:10:24 +01001137 spin_lock_init(&od->lock);
Russell King6ddeb6d2013-12-10 19:05:50 +00001138 spin_lock_init(&od->irq_lock);
Russell King7bedaa52012-04-13 12:10:24 +01001139
Peter Ujfaluside506082015-04-09 12:35:51 +03001140 od->dma_requests = OMAP_SDMA_REQUESTS;
1141 if (pdev->dev.of_node && of_property_read_u32(pdev->dev.of_node,
1142 "dma-requests",
1143 &od->dma_requests)) {
1144 dev_info(&pdev->dev,
1145 "Missing dma-requests property, using %u.\n",
1146 OMAP_SDMA_REQUESTS);
1147 }
1148
Peter Ujfalusi8a322222015-04-09 12:35:53 +03001149 for (i = 0; i < OMAP_SDMA_CHANNELS; i++) {
Peter Ujfalusieea531e2015-04-09 12:35:52 +03001150 rc = omap_dma_chan_init(od);
Russell King7bedaa52012-04-13 12:10:24 +01001151 if (rc) {
1152 omap_dma_free(od);
1153 return rc;
1154 }
1155 }
1156
Russell King6ddeb6d2013-12-10 19:05:50 +00001157 irq = platform_get_irq(pdev, 1);
1158 if (irq <= 0) {
1159 dev_info(&pdev->dev, "failed to get L1 IRQ: %d\n", irq);
1160 od->legacy = true;
1161 } else {
1162 /* Disable all interrupts */
1163 od->irq_enable_mask = 0;
1164 omap_dma_glbl_write(od, IRQENABLE_L1, 0);
1165
1166 rc = devm_request_irq(&pdev->dev, irq, omap_dma_irq,
1167 IRQF_SHARED, "omap-dma-engine", od);
1168 if (rc)
1169 return rc;
1170 }
1171
Peter Ujfalusi020c62a2015-12-14 22:47:42 +02001172 od->ddev.filter.map = od->plat->slave_map;
1173 od->ddev.filter.mapcnt = od->plat->slavecnt;
1174 od->ddev.filter.fn = omap_dma_filter_fn;
1175
Russell King7bedaa52012-04-13 12:10:24 +01001176 rc = dma_async_device_register(&od->ddev);
1177 if (rc) {
1178 pr_warn("OMAP-DMA: failed to register slave DMA engine device: %d\n",
1179 rc);
1180 omap_dma_free(od);
Jon Hunter8d306622013-02-26 12:27:24 -06001181 return rc;
1182 }
1183
1184 platform_set_drvdata(pdev, od);
1185
1186 if (pdev->dev.of_node) {
1187 omap_dma_info.dma_cap = od->ddev.cap_mask;
1188
1189 /* Device-tree DMA controller registration */
1190 rc = of_dma_controller_register(pdev->dev.of_node,
1191 of_dma_simple_xlate, &omap_dma_info);
1192 if (rc) {
1193 pr_warn("OMAP-DMA: failed to register DMA controller\n");
1194 dma_async_device_unregister(&od->ddev);
1195 omap_dma_free(od);
1196 }
Russell King7bedaa52012-04-13 12:10:24 +01001197 }
1198
1199 dev_info(&pdev->dev, "OMAP DMA engine driver\n");
1200
1201 return rc;
1202}
1203
1204static int omap_dma_remove(struct platform_device *pdev)
1205{
1206 struct omap_dmadev *od = platform_get_drvdata(pdev);
1207
Jon Hunter8d306622013-02-26 12:27:24 -06001208 if (pdev->dev.of_node)
1209 of_dma_controller_free(pdev->dev.of_node);
1210
Russell King7bedaa52012-04-13 12:10:24 +01001211 dma_async_device_unregister(&od->ddev);
Russell King6ddeb6d2013-12-10 19:05:50 +00001212
1213 if (!od->legacy) {
1214 /* Disable all interrupts */
1215 omap_dma_glbl_write(od, IRQENABLE_L0, 0);
1216 }
1217
Russell King7bedaa52012-04-13 12:10:24 +01001218 omap_dma_free(od);
1219
1220 return 0;
1221}
1222
Jon Hunter8d306622013-02-26 12:27:24 -06001223static const struct of_device_id omap_dma_match[] = {
1224 { .compatible = "ti,omap2420-sdma", },
1225 { .compatible = "ti,omap2430-sdma", },
1226 { .compatible = "ti,omap3430-sdma", },
1227 { .compatible = "ti,omap3630-sdma", },
1228 { .compatible = "ti,omap4430-sdma", },
1229 {},
1230};
1231MODULE_DEVICE_TABLE(of, omap_dma_match);
1232
Russell King7bedaa52012-04-13 12:10:24 +01001233static struct platform_driver omap_dma_driver = {
1234 .probe = omap_dma_probe,
1235 .remove = omap_dma_remove,
1236 .driver = {
1237 .name = "omap-dma-engine",
Jon Hunter8d306622013-02-26 12:27:24 -06001238 .of_match_table = of_match_ptr(omap_dma_match),
Russell King7bedaa52012-04-13 12:10:24 +01001239 },
1240};
1241
1242bool omap_dma_filter_fn(struct dma_chan *chan, void *param)
1243{
1244 if (chan->device->dev->driver == &omap_dma_driver.driver) {
Peter Ujfalusieea531e2015-04-09 12:35:52 +03001245 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King7bedaa52012-04-13 12:10:24 +01001246 struct omap_chan *c = to_omap_dma_chan(chan);
1247 unsigned req = *(unsigned *)param;
1248
Peter Ujfalusieea531e2015-04-09 12:35:52 +03001249 if (req <= od->dma_requests) {
1250 c->dma_sig = req;
1251 return true;
1252 }
Russell King7bedaa52012-04-13 12:10:24 +01001253 }
1254 return false;
1255}
1256EXPORT_SYMBOL_GPL(omap_dma_filter_fn);
1257
Russell King7bedaa52012-04-13 12:10:24 +01001258static int omap_dma_init(void)
1259{
Tony Lindgrenbe1f9482013-01-11 11:24:19 -08001260 return platform_driver_register(&omap_dma_driver);
Russell King7bedaa52012-04-13 12:10:24 +01001261}
1262subsys_initcall(omap_dma_init);
1263
1264static void __exit omap_dma_exit(void)
1265{
Russell King7bedaa52012-04-13 12:10:24 +01001266 platform_driver_unregister(&omap_dma_driver);
1267}
1268module_exit(omap_dma_exit);
1269
1270MODULE_AUTHOR("Russell King");
1271MODULE_LICENSE("GPL");