blob: 4afc2c18d451444c0d8cd679c57649f36f449422 [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;
31 struct tasklet_struct task;
32 struct list_head pending;
Russell King596c4712013-12-10 11:08:01 +000033 void __iomem *base;
34 const struct omap_dma_reg *reg_map;
Russell King1b416c42013-11-02 13:00:03 +000035 struct omap_system_dma_plat_info *plat;
Russell King6ddeb6d2013-12-10 19:05:50 +000036 bool legacy;
Peter Ujfaluside506082015-04-09 12:35:51 +030037 unsigned dma_requests;
Russell King6ddeb6d2013-12-10 19:05:50 +000038 spinlock_t irq_lock;
39 uint32_t irq_enable_mask;
Peter Ujfalusi341ce712015-04-09 12:35:50 +030040 struct omap_chan *lch_map[OMAP_SDMA_CHANNELS];
Russell King7bedaa52012-04-13 12:10:24 +010041};
42
43struct omap_chan {
44 struct virt_dma_chan vc;
45 struct list_head node;
Russell King596c4712013-12-10 11:08:01 +000046 void __iomem *channel_base;
47 const struct omap_dma_reg *reg_map;
Russell Kingaa4c5b92014-01-14 23:58:10 +000048 uint32_t ccr;
Russell King7bedaa52012-04-13 12:10:24 +010049
50 struct dma_slave_config cfg;
51 unsigned dma_sig;
Russell King3a774ea2012-06-21 10:40:15 +010052 bool cyclic;
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +030053 bool paused;
Russell King7bedaa52012-04-13 12:10:24 +010054
55 int dma_ch;
56 struct omap_desc *desc;
57 unsigned sgidx;
58};
59
60struct omap_sg {
61 dma_addr_t addr;
62 uint32_t en; /* number of elements (24-bit) */
63 uint32_t fn; /* number of frames (16-bit) */
64};
65
66struct omap_desc {
67 struct virt_dma_desc vd;
68 enum dma_transfer_direction dir;
69 dma_addr_t dev_addr;
70
Russell King7c836bc2012-06-18 16:45:19 +010071 int16_t fi; /* for OMAP_DMA_SYNC_PACKET */
Russell King90438262013-11-02 19:57:06 +000072 uint8_t es; /* CSDP_DATA_TYPE_xxx */
Russell King3ed4d182013-11-02 19:16:09 +000073 uint32_t ccr; /* CCR value */
Russell King965aeb4d2013-11-06 17:12:30 +000074 uint16_t clnk_ctrl; /* CLNK_CTRL value */
Russell Kingfa3ad862013-11-02 17:07:09 +000075 uint16_t cicr; /* CICR value */
Russell King2f0d13b2013-11-02 18:51:53 +000076 uint32_t csdp; /* CSDP value */
Russell King7bedaa52012-04-13 12:10:24 +010077
78 unsigned sglen;
79 struct omap_sg sg[0];
80};
81
Russell King90438262013-11-02 19:57:06 +000082enum {
83 CCR_FS = BIT(5),
84 CCR_READ_PRIORITY = BIT(6),
85 CCR_ENABLE = BIT(7),
86 CCR_AUTO_INIT = BIT(8), /* OMAP1 only */
87 CCR_REPEAT = BIT(9), /* OMAP1 only */
88 CCR_OMAP31_DISABLE = BIT(10), /* OMAP1 only */
89 CCR_SUSPEND_SENSITIVE = BIT(8), /* OMAP2+ only */
90 CCR_RD_ACTIVE = BIT(9), /* OMAP2+ only */
91 CCR_WR_ACTIVE = BIT(10), /* OMAP2+ only */
92 CCR_SRC_AMODE_CONSTANT = 0 << 12,
93 CCR_SRC_AMODE_POSTINC = 1 << 12,
94 CCR_SRC_AMODE_SGLIDX = 2 << 12,
95 CCR_SRC_AMODE_DBLIDX = 3 << 12,
96 CCR_DST_AMODE_CONSTANT = 0 << 14,
97 CCR_DST_AMODE_POSTINC = 1 << 14,
98 CCR_DST_AMODE_SGLIDX = 2 << 14,
99 CCR_DST_AMODE_DBLIDX = 3 << 14,
100 CCR_CONSTANT_FILL = BIT(16),
101 CCR_TRANSPARENT_COPY = BIT(17),
102 CCR_BS = BIT(18),
103 CCR_SUPERVISOR = BIT(22),
104 CCR_PREFETCH = BIT(23),
105 CCR_TRIGGER_SRC = BIT(24),
106 CCR_BUFFERING_DISABLE = BIT(25),
107 CCR_WRITE_PRIORITY = BIT(26),
108 CCR_SYNC_ELEMENT = 0,
109 CCR_SYNC_FRAME = CCR_FS,
110 CCR_SYNC_BLOCK = CCR_BS,
111 CCR_SYNC_PACKET = CCR_BS | CCR_FS,
112
113 CSDP_DATA_TYPE_8 = 0,
114 CSDP_DATA_TYPE_16 = 1,
115 CSDP_DATA_TYPE_32 = 2,
116 CSDP_SRC_PORT_EMIFF = 0 << 2, /* OMAP1 only */
117 CSDP_SRC_PORT_EMIFS = 1 << 2, /* OMAP1 only */
118 CSDP_SRC_PORT_OCP_T1 = 2 << 2, /* OMAP1 only */
119 CSDP_SRC_PORT_TIPB = 3 << 2, /* OMAP1 only */
120 CSDP_SRC_PORT_OCP_T2 = 4 << 2, /* OMAP1 only */
121 CSDP_SRC_PORT_MPUI = 5 << 2, /* OMAP1 only */
122 CSDP_SRC_PACKED = BIT(6),
123 CSDP_SRC_BURST_1 = 0 << 7,
124 CSDP_SRC_BURST_16 = 1 << 7,
125 CSDP_SRC_BURST_32 = 2 << 7,
126 CSDP_SRC_BURST_64 = 3 << 7,
127 CSDP_DST_PORT_EMIFF = 0 << 9, /* OMAP1 only */
128 CSDP_DST_PORT_EMIFS = 1 << 9, /* OMAP1 only */
129 CSDP_DST_PORT_OCP_T1 = 2 << 9, /* OMAP1 only */
130 CSDP_DST_PORT_TIPB = 3 << 9, /* OMAP1 only */
131 CSDP_DST_PORT_OCP_T2 = 4 << 9, /* OMAP1 only */
132 CSDP_DST_PORT_MPUI = 5 << 9, /* OMAP1 only */
133 CSDP_DST_PACKED = BIT(13),
134 CSDP_DST_BURST_1 = 0 << 14,
135 CSDP_DST_BURST_16 = 1 << 14,
136 CSDP_DST_BURST_32 = 2 << 14,
137 CSDP_DST_BURST_64 = 3 << 14,
138
139 CICR_TOUT_IE = BIT(0), /* OMAP1 only */
140 CICR_DROP_IE = BIT(1),
141 CICR_HALF_IE = BIT(2),
142 CICR_FRAME_IE = BIT(3),
143 CICR_LAST_IE = BIT(4),
144 CICR_BLOCK_IE = BIT(5),
145 CICR_PKT_IE = BIT(7), /* OMAP2+ only */
146 CICR_TRANS_ERR_IE = BIT(8), /* OMAP2+ only */
147 CICR_SUPERVISOR_ERR_IE = BIT(10), /* OMAP2+ only */
148 CICR_MISALIGNED_ERR_IE = BIT(11), /* OMAP2+ only */
149 CICR_DRAIN_IE = BIT(12), /* OMAP2+ only */
150 CICR_SUPER_BLOCK_IE = BIT(14), /* OMAP2+ only */
151
152 CLNK_CTRL_ENABLE_LNK = BIT(15),
153};
154
Russell King7bedaa52012-04-13 12:10:24 +0100155static const unsigned es_bytes[] = {
Russell King90438262013-11-02 19:57:06 +0000156 [CSDP_DATA_TYPE_8] = 1,
157 [CSDP_DATA_TYPE_16] = 2,
158 [CSDP_DATA_TYPE_32] = 4,
Russell King7bedaa52012-04-13 12:10:24 +0100159};
160
Jon Hunter8d306622013-02-26 12:27:24 -0600161static struct of_dma_filter_info omap_dma_info = {
162 .filter_fn = omap_dma_filter_fn,
163};
164
Russell King7bedaa52012-04-13 12:10:24 +0100165static inline struct omap_dmadev *to_omap_dma_dev(struct dma_device *d)
166{
167 return container_of(d, struct omap_dmadev, ddev);
168}
169
170static inline struct omap_chan *to_omap_dma_chan(struct dma_chan *c)
171{
172 return container_of(c, struct omap_chan, vc.chan);
173}
174
175static inline struct omap_desc *to_omap_dma_desc(struct dma_async_tx_descriptor *t)
176{
177 return container_of(t, struct omap_desc, vd.tx);
178}
179
180static void omap_dma_desc_free(struct virt_dma_desc *vd)
181{
182 kfree(container_of(vd, struct omap_desc, vd));
183}
184
Russell King596c4712013-12-10 11:08:01 +0000185static void omap_dma_write(uint32_t val, unsigned type, void __iomem *addr)
186{
187 switch (type) {
188 case OMAP_DMA_REG_16BIT:
189 writew_relaxed(val, addr);
190 break;
191 case OMAP_DMA_REG_2X16BIT:
192 writew_relaxed(val, addr);
193 writew_relaxed(val >> 16, addr + 2);
194 break;
195 case OMAP_DMA_REG_32BIT:
196 writel_relaxed(val, addr);
197 break;
198 default:
199 WARN_ON(1);
200 }
201}
202
203static unsigned omap_dma_read(unsigned type, void __iomem *addr)
204{
205 unsigned val;
206
207 switch (type) {
208 case OMAP_DMA_REG_16BIT:
209 val = readw_relaxed(addr);
210 break;
211 case OMAP_DMA_REG_2X16BIT:
212 val = readw_relaxed(addr);
213 val |= readw_relaxed(addr + 2) << 16;
214 break;
215 case OMAP_DMA_REG_32BIT:
216 val = readl_relaxed(addr);
217 break;
218 default:
219 WARN_ON(1);
220 val = 0;
221 }
222
223 return val;
224}
225
Russell Kingc5ed98b2013-11-06 17:33:09 +0000226static void omap_dma_glbl_write(struct omap_dmadev *od, unsigned reg, unsigned val)
227{
Russell King596c4712013-12-10 11:08:01 +0000228 const struct omap_dma_reg *r = od->reg_map + reg;
229
230 WARN_ON(r->stride);
231
232 omap_dma_write(val, r->type, od->base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000233}
234
235static unsigned omap_dma_glbl_read(struct omap_dmadev *od, unsigned reg)
236{
Russell King596c4712013-12-10 11:08:01 +0000237 const struct omap_dma_reg *r = od->reg_map + reg;
238
239 WARN_ON(r->stride);
240
241 return omap_dma_read(r->type, od->base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000242}
243
244static void omap_dma_chan_write(struct omap_chan *c, unsigned reg, unsigned val)
245{
Russell King596c4712013-12-10 11:08:01 +0000246 const struct omap_dma_reg *r = c->reg_map + reg;
247
248 omap_dma_write(val, r->type, c->channel_base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000249}
250
251static unsigned omap_dma_chan_read(struct omap_chan *c, unsigned reg)
252{
Russell King596c4712013-12-10 11:08:01 +0000253 const struct omap_dma_reg *r = c->reg_map + reg;
254
255 return omap_dma_read(r->type, c->channel_base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000256}
257
Russell King470b23f2013-11-02 21:23:06 +0000258static void omap_dma_clear_csr(struct omap_chan *c)
259{
260 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000261 omap_dma_chan_read(c, CSR);
Russell King470b23f2013-11-02 21:23:06 +0000262 else
Russell Kingc5ed98b2013-11-06 17:33:09 +0000263 omap_dma_chan_write(c, CSR, ~0);
Russell King470b23f2013-11-02 21:23:06 +0000264}
265
Russell King6ddeb6d2013-12-10 19:05:50 +0000266static unsigned omap_dma_get_csr(struct omap_chan *c)
267{
268 unsigned val = omap_dma_chan_read(c, CSR);
269
270 if (!dma_omap1())
271 omap_dma_chan_write(c, CSR, val);
272
273 return val;
274}
275
Russell King596c4712013-12-10 11:08:01 +0000276static void omap_dma_assign(struct omap_dmadev *od, struct omap_chan *c,
277 unsigned lch)
278{
279 c->channel_base = od->base + od->plat->channel_stride * lch;
Russell King6ddeb6d2013-12-10 19:05:50 +0000280
281 od->lch_map[lch] = c;
Russell King596c4712013-12-10 11:08:01 +0000282}
283
Russell Kingfa3ad862013-11-02 17:07:09 +0000284static void omap_dma_start(struct omap_chan *c, struct omap_desc *d)
285{
286 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
Russell Kingfa3ad862013-11-02 17:07:09 +0000287
288 if (__dma_omap15xx(od->plat->dma_attr))
Russell Kingc5ed98b2013-11-06 17:33:09 +0000289 omap_dma_chan_write(c, CPC, 0);
Russell Kingfa3ad862013-11-02 17:07:09 +0000290 else
Russell Kingc5ed98b2013-11-06 17:33:09 +0000291 omap_dma_chan_write(c, CDAC, 0);
Russell Kingfa3ad862013-11-02 17:07:09 +0000292
Russell King470b23f2013-11-02 21:23:06 +0000293 omap_dma_clear_csr(c);
Russell Kingfa3ad862013-11-02 17:07:09 +0000294
295 /* Enable interrupts */
Russell Kingc5ed98b2013-11-06 17:33:09 +0000296 omap_dma_chan_write(c, CICR, d->cicr);
Russell Kingfa3ad862013-11-02 17:07:09 +0000297
Russell King45da7b02013-11-06 17:18:42 +0000298 /* Enable channel */
Russell Kingc5ed98b2013-11-06 17:33:09 +0000299 omap_dma_chan_write(c, CCR, d->ccr | CCR_ENABLE);
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 }
361}
362
Russell King7bedaa52012-04-13 12:10:24 +0100363static void omap_dma_start_sg(struct omap_chan *c, struct omap_desc *d,
364 unsigned idx)
365{
366 struct omap_sg *sg = d->sg + idx;
Russell King893e63e2013-11-03 11:17:11 +0000367 unsigned cxsa, cxei, cxfi;
Russell King7bedaa52012-04-13 12:10:24 +0100368
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +0300369 if (d->dir == DMA_DEV_TO_MEM || d->dir == DMA_MEM_TO_MEM) {
Russell King893e63e2013-11-03 11:17:11 +0000370 cxsa = CDSA;
371 cxei = CDEI;
372 cxfi = CDFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000373 } else {
Russell King893e63e2013-11-03 11:17:11 +0000374 cxsa = CSSA;
375 cxei = CSEI;
376 cxfi = CSFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000377 }
378
Russell Kingc5ed98b2013-11-06 17:33:09 +0000379 omap_dma_chan_write(c, cxsa, sg->addr);
380 omap_dma_chan_write(c, cxei, 0);
381 omap_dma_chan_write(c, cxfi, 0);
382 omap_dma_chan_write(c, CEN, sg->en);
383 omap_dma_chan_write(c, CFN, sg->fn);
Russell King7bedaa52012-04-13 12:10:24 +0100384
Russell Kingfa3ad862013-11-02 17:07:09 +0000385 omap_dma_start(c, d);
Russell King7bedaa52012-04-13 12:10:24 +0100386}
387
388static void omap_dma_start_desc(struct omap_chan *c)
389{
390 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
391 struct omap_desc *d;
Russell King893e63e2013-11-03 11:17:11 +0000392 unsigned cxsa, cxei, cxfi;
Russell King7bedaa52012-04-13 12:10:24 +0100393
394 if (!vd) {
395 c->desc = NULL;
396 return;
397 }
398
399 list_del(&vd->node);
400
401 c->desc = d = to_omap_dma_desc(&vd->tx);
402 c->sgidx = 0;
403
Russell King59871902013-11-06 17:15:16 +0000404 /*
405 * This provides the necessary barrier to ensure data held in
406 * DMA coherent memory is visible to the DMA engine prior to
407 * the transfer starting.
408 */
409 mb();
410
Russell Kingc5ed98b2013-11-06 17:33:09 +0000411 omap_dma_chan_write(c, CCR, d->ccr);
Russell King3ed4d182013-11-02 19:16:09 +0000412 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000413 omap_dma_chan_write(c, CCR2, d->ccr >> 16);
Russell Kingb9e97822013-11-02 13:26:57 +0000414
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +0300415 if (d->dir == DMA_DEV_TO_MEM || d->dir == DMA_MEM_TO_MEM) {
Russell King893e63e2013-11-03 11:17:11 +0000416 cxsa = CSSA;
417 cxei = CSEI;
418 cxfi = CSFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000419 } else {
Russell King893e63e2013-11-03 11:17:11 +0000420 cxsa = CDSA;
421 cxei = CDEI;
422 cxfi = CDFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000423 }
Russell King7bedaa52012-04-13 12:10:24 +0100424
Russell Kingc5ed98b2013-11-06 17:33:09 +0000425 omap_dma_chan_write(c, cxsa, d->dev_addr);
426 omap_dma_chan_write(c, cxei, 0);
427 omap_dma_chan_write(c, cxfi, d->fi);
428 omap_dma_chan_write(c, CSDP, d->csdp);
429 omap_dma_chan_write(c, CLNK_CTRL, d->clnk_ctrl);
Russell King913a2d02013-11-02 14:41:42 +0000430
Russell King7bedaa52012-04-13 12:10:24 +0100431 omap_dma_start_sg(c, d, 0);
432}
433
434static void omap_dma_callback(int ch, u16 status, void *data)
435{
436 struct omap_chan *c = data;
437 struct omap_desc *d;
438 unsigned long flags;
439
440 spin_lock_irqsave(&c->vc.lock, flags);
441 d = c->desc;
442 if (d) {
Russell King3a774ea2012-06-21 10:40:15 +0100443 if (!c->cyclic) {
444 if (++c->sgidx < d->sglen) {
445 omap_dma_start_sg(c, d, c->sgidx);
446 } else {
447 omap_dma_start_desc(c);
448 vchan_cookie_complete(&d->vd);
449 }
Russell King7bedaa52012-04-13 12:10:24 +0100450 } else {
Russell King3a774ea2012-06-21 10:40:15 +0100451 vchan_cyclic_callback(&d->vd);
Russell King7bedaa52012-04-13 12:10:24 +0100452 }
453 }
454 spin_unlock_irqrestore(&c->vc.lock, flags);
455}
456
457/*
458 * This callback schedules all pending channels. We could be more
459 * clever here by postponing allocation of the real DMA channels to
460 * this point, and freeing them when our virtual channel becomes idle.
461 *
462 * We would then need to deal with 'all channels in-use'
463 */
464static void omap_dma_sched(unsigned long data)
465{
466 struct omap_dmadev *d = (struct omap_dmadev *)data;
467 LIST_HEAD(head);
468
469 spin_lock_irq(&d->lock);
470 list_splice_tail_init(&d->pending, &head);
471 spin_unlock_irq(&d->lock);
472
473 while (!list_empty(&head)) {
474 struct omap_chan *c = list_first_entry(&head,
475 struct omap_chan, node);
476
477 spin_lock_irq(&c->vc.lock);
478 list_del_init(&c->node);
479 omap_dma_start_desc(c);
480 spin_unlock_irq(&c->vc.lock);
481 }
482}
483
Russell King6ddeb6d2013-12-10 19:05:50 +0000484static irqreturn_t omap_dma_irq(int irq, void *devid)
485{
486 struct omap_dmadev *od = devid;
487 unsigned status, channel;
488
489 spin_lock(&od->irq_lock);
490
491 status = omap_dma_glbl_read(od, IRQSTATUS_L1);
492 status &= od->irq_enable_mask;
493 if (status == 0) {
494 spin_unlock(&od->irq_lock);
495 return IRQ_NONE;
496 }
497
498 while ((channel = ffs(status)) != 0) {
499 unsigned mask, csr;
500 struct omap_chan *c;
501
502 channel -= 1;
503 mask = BIT(channel);
504 status &= ~mask;
505
506 c = od->lch_map[channel];
507 if (c == NULL) {
508 /* This should never happen */
509 dev_err(od->ddev.dev, "invalid channel %u\n", channel);
510 continue;
511 }
512
513 csr = omap_dma_get_csr(c);
514 omap_dma_glbl_write(od, IRQSTATUS_L1, mask);
515
516 omap_dma_callback(channel, csr, c);
517 }
518
519 spin_unlock(&od->irq_lock);
520
521 return IRQ_HANDLED;
522}
523
Russell King7bedaa52012-04-13 12:10:24 +0100524static int omap_dma_alloc_chan_resources(struct dma_chan *chan)
525{
Russell King596c4712013-12-10 11:08:01 +0000526 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King7bedaa52012-04-13 12:10:24 +0100527 struct omap_chan *c = to_omap_dma_chan(chan);
Russell King596c4712013-12-10 11:08:01 +0000528 int ret;
Russell King7bedaa52012-04-13 12:10:24 +0100529
Russell King6ddeb6d2013-12-10 19:05:50 +0000530 if (od->legacy) {
531 ret = omap_request_dma(c->dma_sig, "DMA engine",
532 omap_dma_callback, c, &c->dma_ch);
533 } else {
534 ret = omap_request_dma(c->dma_sig, "DMA engine", NULL, NULL,
535 &c->dma_ch);
536 }
Russell King7bedaa52012-04-13 12:10:24 +0100537
Russell King6ddeb6d2013-12-10 19:05:50 +0000538 dev_dbg(od->ddev.dev, "allocating channel %u for %u\n",
539 c->dma_ch, c->dma_sig);
Russell King596c4712013-12-10 11:08:01 +0000540
Russell King6ddeb6d2013-12-10 19:05:50 +0000541 if (ret >= 0) {
Russell King596c4712013-12-10 11:08:01 +0000542 omap_dma_assign(od, c, c->dma_ch);
543
Russell King6ddeb6d2013-12-10 19:05:50 +0000544 if (!od->legacy) {
545 unsigned val;
546
547 spin_lock_irq(&od->irq_lock);
548 val = BIT(c->dma_ch);
549 omap_dma_glbl_write(od, IRQSTATUS_L1, val);
550 od->irq_enable_mask |= val;
551 omap_dma_glbl_write(od, IRQENABLE_L1, od->irq_enable_mask);
552
553 val = omap_dma_glbl_read(od, IRQENABLE_L0);
554 val &= ~BIT(c->dma_ch);
555 omap_dma_glbl_write(od, IRQENABLE_L0, val);
556 spin_unlock_irq(&od->irq_lock);
557 }
558 }
559
Russell Kingaa4c5b92014-01-14 23:58:10 +0000560 if (dma_omap1()) {
561 if (__dma_omap16xx(od->plat->dma_attr)) {
562 c->ccr = CCR_OMAP31_DISABLE;
563 /* Duplicate what plat-omap/dma.c does */
564 c->ccr |= c->dma_ch + 1;
565 } else {
566 c->ccr = c->dma_sig & 0x1f;
567 }
568 } else {
569 c->ccr = c->dma_sig & 0x1f;
570 c->ccr |= (c->dma_sig & ~0x1f) << 14;
571 }
572 if (od->plat->errata & DMA_ERRATA_IFRAME_BUFFERING)
573 c->ccr |= CCR_BUFFERING_DISABLE;
574
Russell King596c4712013-12-10 11:08:01 +0000575 return ret;
Russell King7bedaa52012-04-13 12:10:24 +0100576}
577
578static void omap_dma_free_chan_resources(struct dma_chan *chan)
579{
Russell King6ddeb6d2013-12-10 19:05:50 +0000580 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King7bedaa52012-04-13 12:10:24 +0100581 struct omap_chan *c = to_omap_dma_chan(chan);
582
Russell King6ddeb6d2013-12-10 19:05:50 +0000583 if (!od->legacy) {
584 spin_lock_irq(&od->irq_lock);
585 od->irq_enable_mask &= ~BIT(c->dma_ch);
586 omap_dma_glbl_write(od, IRQENABLE_L1, od->irq_enable_mask);
587 spin_unlock_irq(&od->irq_lock);
588 }
589
Russell King596c4712013-12-10 11:08:01 +0000590 c->channel_base = NULL;
Russell King6ddeb6d2013-12-10 19:05:50 +0000591 od->lch_map[c->dma_ch] = NULL;
Russell King7bedaa52012-04-13 12:10:24 +0100592 vchan_free_chan_resources(&c->vc);
593 omap_free_dma(c->dma_ch);
594
Russell King6ddeb6d2013-12-10 19:05:50 +0000595 dev_dbg(od->ddev.dev, "freeing channel for %u\n", c->dma_sig);
Peter Ujfalusieea531e2015-04-09 12:35:52 +0300596 c->dma_sig = 0;
Russell King7bedaa52012-04-13 12:10:24 +0100597}
598
Russell King3850e222012-06-21 10:37:35 +0100599static size_t omap_dma_sg_size(struct omap_sg *sg)
600{
601 return sg->en * sg->fn;
602}
603
604static size_t omap_dma_desc_size(struct omap_desc *d)
605{
606 unsigned i;
607 size_t size;
608
609 for (size = i = 0; i < d->sglen; i++)
610 size += omap_dma_sg_size(&d->sg[i]);
611
612 return size * es_bytes[d->es];
613}
614
615static size_t omap_dma_desc_size_pos(struct omap_desc *d, dma_addr_t addr)
616{
617 unsigned i;
618 size_t size, es_size = es_bytes[d->es];
619
620 for (size = i = 0; i < d->sglen; i++) {
621 size_t this_size = omap_dma_sg_size(&d->sg[i]) * es_size;
622
623 if (size)
624 size += this_size;
625 else if (addr >= d->sg[i].addr &&
626 addr < d->sg[i].addr + this_size)
627 size += d->sg[i].addr + this_size - addr;
628 }
629 return size;
630}
631
Russell Kingb07fd622013-11-06 19:26:45 +0000632/*
633 * OMAP 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
634 * read before the DMA controller finished disabling the channel.
635 */
636static uint32_t omap_dma_chan_read_3_3(struct omap_chan *c, unsigned reg)
637{
638 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
639 uint32_t val;
640
641 val = omap_dma_chan_read(c, reg);
642 if (val == 0 && od->plat->errata & DMA_ERRATA_3_3)
643 val = omap_dma_chan_read(c, reg);
644
645 return val;
646}
647
Russell King3997cab2013-11-02 18:04:17 +0000648static dma_addr_t omap_dma_get_src_pos(struct omap_chan *c)
649{
650 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
Russell Kingb07fd622013-11-06 19:26:45 +0000651 dma_addr_t addr, cdac;
Russell King3997cab2013-11-02 18:04:17 +0000652
Russell Kingb07fd622013-11-06 19:26:45 +0000653 if (__dma_omap15xx(od->plat->dma_attr)) {
Russell Kingc5ed98b2013-11-06 17:33:09 +0000654 addr = omap_dma_chan_read(c, CPC);
Russell Kingb07fd622013-11-06 19:26:45 +0000655 } else {
656 addr = omap_dma_chan_read_3_3(c, CSAC);
657 cdac = omap_dma_chan_read_3_3(c, CDAC);
Russell King3997cab2013-11-02 18:04:17 +0000658
Russell King3997cab2013-11-02 18:04:17 +0000659 /*
660 * CDAC == 0 indicates that the DMA transfer on the channel has
661 * not been started (no data has been transferred so far).
662 * Return the programmed source start address in this case.
663 */
Russell Kingb07fd622013-11-06 19:26:45 +0000664 if (cdac == 0)
Russell Kingc5ed98b2013-11-06 17:33:09 +0000665 addr = omap_dma_chan_read(c, CSSA);
Russell King3997cab2013-11-02 18:04:17 +0000666 }
667
668 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000669 addr |= omap_dma_chan_read(c, CSSA) & 0xffff0000;
Russell King3997cab2013-11-02 18:04:17 +0000670
671 return addr;
672}
673
674static dma_addr_t omap_dma_get_dst_pos(struct omap_chan *c)
675{
676 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
677 dma_addr_t addr;
678
Russell Kingb07fd622013-11-06 19:26:45 +0000679 if (__dma_omap15xx(od->plat->dma_attr)) {
Russell Kingc5ed98b2013-11-06 17:33:09 +0000680 addr = omap_dma_chan_read(c, CPC);
Russell Kingb07fd622013-11-06 19:26:45 +0000681 } else {
682 addr = omap_dma_chan_read_3_3(c, CDAC);
Russell King3997cab2013-11-02 18:04:17 +0000683
Russell King3997cab2013-11-02 18:04:17 +0000684 /*
Russell Kingb07fd622013-11-06 19:26:45 +0000685 * CDAC == 0 indicates that the DMA transfer on the channel
686 * has not been started (no data has been transferred so
687 * far). Return the programmed destination start address in
688 * this case.
Russell King3997cab2013-11-02 18:04:17 +0000689 */
690 if (addr == 0)
Russell Kingc5ed98b2013-11-06 17:33:09 +0000691 addr = omap_dma_chan_read(c, CDSA);
Russell King3997cab2013-11-02 18:04:17 +0000692 }
693
694 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000695 addr |= omap_dma_chan_read(c, CDSA) & 0xffff0000;
Russell King3997cab2013-11-02 18:04:17 +0000696
697 return addr;
698}
699
Russell King7bedaa52012-04-13 12:10:24 +0100700static enum dma_status omap_dma_tx_status(struct dma_chan *chan,
701 dma_cookie_t cookie, struct dma_tx_state *txstate)
702{
Russell King3850e222012-06-21 10:37:35 +0100703 struct omap_chan *c = to_omap_dma_chan(chan);
704 struct virt_dma_desc *vd;
705 enum dma_status ret;
706 unsigned long flags;
707
708 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul7cce5082013-10-16 20:51:54 +0530709 if (ret == DMA_COMPLETE || !txstate)
Russell King3850e222012-06-21 10:37:35 +0100710 return ret;
711
712 spin_lock_irqsave(&c->vc.lock, flags);
713 vd = vchan_find_desc(&c->vc, cookie);
714 if (vd) {
715 txstate->residue = omap_dma_desc_size(to_omap_dma_desc(&vd->tx));
716 } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
717 struct omap_desc *d = c->desc;
718 dma_addr_t pos;
719
720 if (d->dir == DMA_MEM_TO_DEV)
Russell King3997cab2013-11-02 18:04:17 +0000721 pos = omap_dma_get_src_pos(c);
Peter Ujfalusiadf850b2015-11-11 12:37:55 +0200722 else if (d->dir == DMA_DEV_TO_MEM || d->dir == DMA_MEM_TO_MEM)
Russell King3997cab2013-11-02 18:04:17 +0000723 pos = omap_dma_get_dst_pos(c);
Russell King3850e222012-06-21 10:37:35 +0100724 else
725 pos = 0;
726
727 txstate->residue = omap_dma_desc_size_pos(d, pos);
728 } else {
729 txstate->residue = 0;
730 }
731 spin_unlock_irqrestore(&c->vc.lock, flags);
732
733 return ret;
Russell King7bedaa52012-04-13 12:10:24 +0100734}
735
736static void omap_dma_issue_pending(struct dma_chan *chan)
737{
738 struct omap_chan *c = to_omap_dma_chan(chan);
739 unsigned long flags;
740
741 spin_lock_irqsave(&c->vc.lock, flags);
742 if (vchan_issue_pending(&c->vc) && !c->desc) {
Peter Ujfalusi76502462013-04-09 16:33:06 +0200743 /*
744 * c->cyclic is used only by audio and in this case the DMA need
745 * to be started without delay.
746 */
747 if (!c->cyclic) {
748 struct omap_dmadev *d = to_omap_dma_dev(chan->device);
749 spin_lock(&d->lock);
750 if (list_empty(&c->node))
751 list_add_tail(&c->node, &d->pending);
752 spin_unlock(&d->lock);
753 tasklet_schedule(&d->task);
754 } else {
755 omap_dma_start_desc(c);
756 }
Russell King7bedaa52012-04-13 12:10:24 +0100757 }
758 spin_unlock_irqrestore(&c->vc.lock, flags);
759}
760
761static struct dma_async_tx_descriptor *omap_dma_prep_slave_sg(
762 struct dma_chan *chan, struct scatterlist *sgl, unsigned sglen,
763 enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
764{
Russell King49ae0b22013-11-02 21:09:18 +0000765 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King7bedaa52012-04-13 12:10:24 +0100766 struct omap_chan *c = to_omap_dma_chan(chan);
767 enum dma_slave_buswidth dev_width;
768 struct scatterlist *sgent;
769 struct omap_desc *d;
770 dma_addr_t dev_addr;
Peter Ujfalusie8a5e792015-11-11 12:37:56 +0200771 unsigned i, es, en, frame_bytes;
Russell King7bedaa52012-04-13 12:10:24 +0100772 u32 burst;
773
774 if (dir == DMA_DEV_TO_MEM) {
775 dev_addr = c->cfg.src_addr;
776 dev_width = c->cfg.src_addr_width;
777 burst = c->cfg.src_maxburst;
Russell King7bedaa52012-04-13 12:10:24 +0100778 } else if (dir == DMA_MEM_TO_DEV) {
779 dev_addr = c->cfg.dst_addr;
780 dev_width = c->cfg.dst_addr_width;
781 burst = c->cfg.dst_maxburst;
Russell King7bedaa52012-04-13 12:10:24 +0100782 } else {
783 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
784 return NULL;
785 }
786
787 /* Bus width translates to the element size (ES) */
788 switch (dev_width) {
789 case DMA_SLAVE_BUSWIDTH_1_BYTE:
Russell King90438262013-11-02 19:57:06 +0000790 es = CSDP_DATA_TYPE_8;
Russell King7bedaa52012-04-13 12:10:24 +0100791 break;
792 case DMA_SLAVE_BUSWIDTH_2_BYTES:
Russell King90438262013-11-02 19:57:06 +0000793 es = CSDP_DATA_TYPE_16;
Russell King7bedaa52012-04-13 12:10:24 +0100794 break;
795 case DMA_SLAVE_BUSWIDTH_4_BYTES:
Russell King90438262013-11-02 19:57:06 +0000796 es = CSDP_DATA_TYPE_32;
Russell King7bedaa52012-04-13 12:10:24 +0100797 break;
798 default: /* not reached */
799 return NULL;
800 }
801
802 /* Now allocate and setup the descriptor. */
803 d = kzalloc(sizeof(*d) + sglen * sizeof(d->sg[0]), GFP_ATOMIC);
804 if (!d)
805 return NULL;
806
807 d->dir = dir;
808 d->dev_addr = dev_addr;
809 d->es = es;
Russell King3ed4d182013-11-02 19:16:09 +0000810
Russell Kingaa4c5b92014-01-14 23:58:10 +0000811 d->ccr = c->ccr | CCR_SYNC_FRAME;
Russell King3ed4d182013-11-02 19:16:09 +0000812 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000813 d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_CONSTANT;
Russell King3ed4d182013-11-02 19:16:09 +0000814 else
Russell King90438262013-11-02 19:57:06 +0000815 d->ccr |= CCR_DST_AMODE_CONSTANT | CCR_SRC_AMODE_POSTINC;
Russell King3ed4d182013-11-02 19:16:09 +0000816
Russell King90438262013-11-02 19:57:06 +0000817 d->cicr = CICR_DROP_IE | CICR_BLOCK_IE;
Russell King2f0d13b2013-11-02 18:51:53 +0000818 d->csdp = es;
Russell Kingfa3ad862013-11-02 17:07:09 +0000819
Russell King2f0d13b2013-11-02 18:51:53 +0000820 if (dma_omap1()) {
Russell King90438262013-11-02 19:57:06 +0000821 d->cicr |= CICR_TOUT_IE;
Russell King2f0d13b2013-11-02 18:51:53 +0000822
823 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000824 d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_TIPB;
Russell King2f0d13b2013-11-02 18:51:53 +0000825 else
Russell King90438262013-11-02 19:57:06 +0000826 d->csdp |= CSDP_DST_PORT_TIPB | CSDP_SRC_PORT_EMIFF;
Russell King2f0d13b2013-11-02 18:51:53 +0000827 } else {
Russell King3ed4d182013-11-02 19:16:09 +0000828 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000829 d->ccr |= CCR_TRIGGER_SRC;
Russell King3ed4d182013-11-02 19:16:09 +0000830
Russell King90438262013-11-02 19:57:06 +0000831 d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
Russell King2f0d13b2013-11-02 18:51:53 +0000832 }
Russell King965aeb4d2013-11-06 17:12:30 +0000833 if (od->plat->errata & DMA_ERRATA_PARALLEL_CHANNELS)
834 d->clnk_ctrl = c->dma_ch;
Russell King7bedaa52012-04-13 12:10:24 +0100835
836 /*
837 * Build our scatterlist entries: each contains the address,
838 * the number of elements (EN) in each frame, and the number of
839 * frames (FN). Number of bytes for this entry = ES * EN * FN.
840 *
841 * Burst size translates to number of elements with frame sync.
842 * Note: DMA engine defines burst to be the number of dev-width
843 * transfers.
844 */
845 en = burst;
846 frame_bytes = es_bytes[es] * en;
847 for_each_sg(sgl, sgent, sglen, i) {
Peter Ujfalusie8a5e792015-11-11 12:37:56 +0200848 d->sg[i].addr = sg_dma_address(sgent);
849 d->sg[i].en = en;
850 d->sg[i].fn = sg_dma_len(sgent) / frame_bytes;
Russell King7bedaa52012-04-13 12:10:24 +0100851 }
852
Peter Ujfalusie8a5e792015-11-11 12:37:56 +0200853 d->sglen = sglen;
Russell King7bedaa52012-04-13 12:10:24 +0100854
855 return vchan_tx_prep(&c->vc, &d->vd, tx_flags);
856}
857
Russell King3a774ea2012-06-21 10:40:15 +0100858static struct dma_async_tx_descriptor *omap_dma_prep_dma_cyclic(
859 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +0200860 size_t period_len, enum dma_transfer_direction dir, unsigned long flags)
Russell King3a774ea2012-06-21 10:40:15 +0100861{
Russell Kingfa3ad862013-11-02 17:07:09 +0000862 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King3a774ea2012-06-21 10:40:15 +0100863 struct omap_chan *c = to_omap_dma_chan(chan);
864 enum dma_slave_buswidth dev_width;
865 struct omap_desc *d;
866 dma_addr_t dev_addr;
Russell King3ed4d182013-11-02 19:16:09 +0000867 unsigned es;
Russell King3a774ea2012-06-21 10:40:15 +0100868 u32 burst;
869
870 if (dir == DMA_DEV_TO_MEM) {
871 dev_addr = c->cfg.src_addr;
872 dev_width = c->cfg.src_addr_width;
873 burst = c->cfg.src_maxburst;
Russell King3a774ea2012-06-21 10:40:15 +0100874 } else if (dir == DMA_MEM_TO_DEV) {
875 dev_addr = c->cfg.dst_addr;
876 dev_width = c->cfg.dst_addr_width;
877 burst = c->cfg.dst_maxburst;
Russell King3a774ea2012-06-21 10:40:15 +0100878 } else {
879 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
880 return NULL;
881 }
882
883 /* Bus width translates to the element size (ES) */
884 switch (dev_width) {
885 case DMA_SLAVE_BUSWIDTH_1_BYTE:
Russell King90438262013-11-02 19:57:06 +0000886 es = CSDP_DATA_TYPE_8;
Russell King3a774ea2012-06-21 10:40:15 +0100887 break;
888 case DMA_SLAVE_BUSWIDTH_2_BYTES:
Russell King90438262013-11-02 19:57:06 +0000889 es = CSDP_DATA_TYPE_16;
Russell King3a774ea2012-06-21 10:40:15 +0100890 break;
891 case DMA_SLAVE_BUSWIDTH_4_BYTES:
Russell King90438262013-11-02 19:57:06 +0000892 es = CSDP_DATA_TYPE_32;
Russell King3a774ea2012-06-21 10:40:15 +0100893 break;
894 default: /* not reached */
895 return NULL;
896 }
897
898 /* Now allocate and setup the descriptor. */
899 d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC);
900 if (!d)
901 return NULL;
902
903 d->dir = dir;
904 d->dev_addr = dev_addr;
905 d->fi = burst;
906 d->es = es;
Russell King3a774ea2012-06-21 10:40:15 +0100907 d->sg[0].addr = buf_addr;
908 d->sg[0].en = period_len / es_bytes[es];
909 d->sg[0].fn = buf_len / period_len;
910 d->sglen = 1;
Russell King3ed4d182013-11-02 19:16:09 +0000911
Russell Kingaa4c5b92014-01-14 23:58:10 +0000912 d->ccr = c->ccr;
Russell King3ed4d182013-11-02 19:16:09 +0000913 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000914 d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_CONSTANT;
Russell King3ed4d182013-11-02 19:16:09 +0000915 else
Russell King90438262013-11-02 19:57:06 +0000916 d->ccr |= CCR_DST_AMODE_CONSTANT | CCR_SRC_AMODE_POSTINC;
Russell King3ed4d182013-11-02 19:16:09 +0000917
Russell King90438262013-11-02 19:57:06 +0000918 d->cicr = CICR_DROP_IE;
Russell Kingfa3ad862013-11-02 17:07:09 +0000919 if (flags & DMA_PREP_INTERRUPT)
Russell King90438262013-11-02 19:57:06 +0000920 d->cicr |= CICR_FRAME_IE;
Russell Kingfa3ad862013-11-02 17:07:09 +0000921
Russell King2f0d13b2013-11-02 18:51:53 +0000922 d->csdp = es;
923
924 if (dma_omap1()) {
Russell King90438262013-11-02 19:57:06 +0000925 d->cicr |= CICR_TOUT_IE;
Russell King2f0d13b2013-11-02 18:51:53 +0000926
927 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000928 d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_MPUI;
Russell King2f0d13b2013-11-02 18:51:53 +0000929 else
Russell King90438262013-11-02 19:57:06 +0000930 d->csdp |= CSDP_DST_PORT_MPUI | CSDP_SRC_PORT_EMIFF;
Russell King2f0d13b2013-11-02 18:51:53 +0000931 } else {
Russell King3ed4d182013-11-02 19:16:09 +0000932 if (burst)
Russell King90438262013-11-02 19:57:06 +0000933 d->ccr |= CCR_SYNC_PACKET;
934 else
935 d->ccr |= CCR_SYNC_ELEMENT;
Russell King3ed4d182013-11-02 19:16:09 +0000936
Misael Lopez Cruz47fac242015-09-14 15:31:05 +0300937 if (dir == DMA_DEV_TO_MEM) {
Russell King90438262013-11-02 19:57:06 +0000938 d->ccr |= CCR_TRIGGER_SRC;
Misael Lopez Cruz47fac242015-09-14 15:31:05 +0300939 d->csdp |= CSDP_DST_PACKED;
940 } else {
941 d->csdp |= CSDP_SRC_PACKED;
942 }
Russell King3ed4d182013-11-02 19:16:09 +0000943
Russell King90438262013-11-02 19:57:06 +0000944 d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
Russell King3a774ea2012-06-21 10:40:15 +0100945
Russell King90438262013-11-02 19:57:06 +0000946 d->csdp |= CSDP_DST_BURST_64 | CSDP_SRC_BURST_64;
Russell King2f0d13b2013-11-02 18:51:53 +0000947 }
948
Russell King965aeb4d2013-11-06 17:12:30 +0000949 if (__dma_omap15xx(od->plat->dma_attr))
950 d->ccr |= CCR_AUTO_INIT | CCR_REPEAT;
951 else
952 d->clnk_ctrl = c->dma_ch | CLNK_CTRL_ENABLE_LNK;
953
Russell King3ed4d182013-11-02 19:16:09 +0000954 c->cyclic = true;
Russell King3a774ea2012-06-21 10:40:15 +0100955
Peter Ujfalusi2dde5b92012-09-14 15:05:48 +0300956 return vchan_tx_prep(&c->vc, &d->vd, flags);
Russell King3a774ea2012-06-21 10:40:15 +0100957}
958
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +0300959static struct dma_async_tx_descriptor *omap_dma_prep_dma_memcpy(
960 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
961 size_t len, unsigned long tx_flags)
962{
963 struct omap_chan *c = to_omap_dma_chan(chan);
964 struct omap_desc *d;
965 uint8_t data_type;
966
967 d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC);
968 if (!d)
969 return NULL;
970
971 data_type = __ffs((src | dest | len));
972 if (data_type > CSDP_DATA_TYPE_32)
973 data_type = CSDP_DATA_TYPE_32;
974
975 d->dir = DMA_MEM_TO_MEM;
976 d->dev_addr = src;
977 d->fi = 0;
978 d->es = data_type;
979 d->sg[0].en = len / BIT(data_type);
980 d->sg[0].fn = 1;
981 d->sg[0].addr = dest;
982 d->sglen = 1;
983 d->ccr = c->ccr;
984 d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_POSTINC;
985
986 d->cicr = CICR_DROP_IE;
987 if (tx_flags & DMA_PREP_INTERRUPT)
988 d->cicr |= CICR_FRAME_IE;
989
990 d->csdp = data_type;
991
992 if (dma_omap1()) {
993 d->cicr |= CICR_TOUT_IE;
994 d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_EMIFF;
995 } else {
996 d->csdp |= CSDP_DST_PACKED | CSDP_SRC_PACKED;
997 d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
998 d->csdp |= CSDP_DST_BURST_64 | CSDP_SRC_BURST_64;
999 }
1000
1001 return vchan_tx_prep(&c->vc, &d->vd, tx_flags);
1002}
1003
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001004static int omap_dma_slave_config(struct dma_chan *chan, struct dma_slave_config *cfg)
Russell King7bedaa52012-04-13 12:10:24 +01001005{
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001006 struct omap_chan *c = to_omap_dma_chan(chan);
1007
Russell King7bedaa52012-04-13 12:10:24 +01001008 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
1009 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
1010 return -EINVAL;
1011
1012 memcpy(&c->cfg, cfg, sizeof(c->cfg));
1013
1014 return 0;
1015}
1016
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001017static int omap_dma_terminate_all(struct dma_chan *chan)
Russell King7bedaa52012-04-13 12:10:24 +01001018{
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001019 struct omap_chan *c = to_omap_dma_chan(chan);
Russell King7bedaa52012-04-13 12:10:24 +01001020 struct omap_dmadev *d = to_omap_dma_dev(c->vc.chan.device);
1021 unsigned long flags;
1022 LIST_HEAD(head);
1023
1024 spin_lock_irqsave(&c->vc.lock, flags);
1025
1026 /* Prevent this channel being scheduled */
1027 spin_lock(&d->lock);
1028 list_del_init(&c->node);
1029 spin_unlock(&d->lock);
1030
1031 /*
1032 * Stop DMA activity: we assume the callback will not be called
Russell Kingfa3ad862013-11-02 17:07:09 +00001033 * after omap_dma_stop() returns (even if it does, it will see
Russell King7bedaa52012-04-13 12:10:24 +01001034 * c->desc is NULL and exit.)
1035 */
1036 if (c->desc) {
Peter Ujfalusi02d88b72015-03-27 13:35:52 +02001037 omap_dma_desc_free(&c->desc->vd);
Russell King7bedaa52012-04-13 12:10:24 +01001038 c->desc = NULL;
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001039 /* Avoid stopping the dma twice */
1040 if (!c->paused)
Russell Kingfa3ad862013-11-02 17:07:09 +00001041 omap_dma_stop(c);
Russell King7bedaa52012-04-13 12:10:24 +01001042 }
1043
Russell King3a774ea2012-06-21 10:40:15 +01001044 if (c->cyclic) {
1045 c->cyclic = false;
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001046 c->paused = false;
Russell King3a774ea2012-06-21 10:40:15 +01001047 }
1048
Russell King7bedaa52012-04-13 12:10:24 +01001049 vchan_get_all_descriptors(&c->vc, &head);
1050 spin_unlock_irqrestore(&c->vc.lock, flags);
1051 vchan_dma_desc_free_list(&c->vc, &head);
1052
1053 return 0;
1054}
1055
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001056static int omap_dma_pause(struct dma_chan *chan)
Russell King7bedaa52012-04-13 12:10:24 +01001057{
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001058 struct omap_chan *c = to_omap_dma_chan(chan);
1059
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001060 /* Pause/Resume only allowed with cyclic mode */
1061 if (!c->cyclic)
1062 return -EINVAL;
1063
1064 if (!c->paused) {
Russell Kingfa3ad862013-11-02 17:07:09 +00001065 omap_dma_stop(c);
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001066 c->paused = true;
1067 }
1068
1069 return 0;
Russell King7bedaa52012-04-13 12:10:24 +01001070}
1071
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001072static int omap_dma_resume(struct dma_chan *chan)
Russell King7bedaa52012-04-13 12:10:24 +01001073{
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001074 struct omap_chan *c = to_omap_dma_chan(chan);
1075
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001076 /* Pause/Resume only allowed with cyclic mode */
1077 if (!c->cyclic)
1078 return -EINVAL;
1079
1080 if (c->paused) {
Peter Ujfalusib3d09da2014-09-16 22:45:56 +03001081 mb();
1082
Peter Ujfalusibfb60742014-09-16 22:45:57 +03001083 /* Restore channel link register */
1084 omap_dma_chan_write(c, CLNK_CTRL, c->desc->clnk_ctrl);
1085
Russell Kingfa3ad862013-11-02 17:07:09 +00001086 omap_dma_start(c, c->desc);
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001087 c->paused = false;
1088 }
1089
1090 return 0;
Russell King7bedaa52012-04-13 12:10:24 +01001091}
1092
Peter Ujfalusieea531e2015-04-09 12:35:52 +03001093static int omap_dma_chan_init(struct omap_dmadev *od)
Russell King7bedaa52012-04-13 12:10:24 +01001094{
1095 struct omap_chan *c;
1096
1097 c = kzalloc(sizeof(*c), GFP_KERNEL);
1098 if (!c)
1099 return -ENOMEM;
1100
Russell King596c4712013-12-10 11:08:01 +00001101 c->reg_map = od->reg_map;
Russell King7bedaa52012-04-13 12:10:24 +01001102 c->vc.desc_free = omap_dma_desc_free;
1103 vchan_init(&c->vc, &od->ddev);
1104 INIT_LIST_HEAD(&c->node);
1105
Russell King7bedaa52012-04-13 12:10:24 +01001106 return 0;
1107}
1108
1109static void omap_dma_free(struct omap_dmadev *od)
1110{
1111 tasklet_kill(&od->task);
1112 while (!list_empty(&od->ddev.channels)) {
1113 struct omap_chan *c = list_first_entry(&od->ddev.channels,
1114 struct omap_chan, vc.chan.device_node);
1115
1116 list_del(&c->vc.chan.device_node);
1117 tasklet_kill(&c->vc.task);
1118 kfree(c);
1119 }
Russell King7bedaa52012-04-13 12:10:24 +01001120}
1121
Peter Ujfalusi80b0e0a2014-03-29 19:03:30 +05301122#define OMAP_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
1123 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
1124 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
1125
Russell King7bedaa52012-04-13 12:10:24 +01001126static int omap_dma_probe(struct platform_device *pdev)
1127{
1128 struct omap_dmadev *od;
Russell King596c4712013-12-10 11:08:01 +00001129 struct resource *res;
Russell King6ddeb6d2013-12-10 19:05:50 +00001130 int rc, i, irq;
Russell King7bedaa52012-04-13 12:10:24 +01001131
Russell King104fce72013-11-02 12:58:29 +00001132 od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
Russell King7bedaa52012-04-13 12:10:24 +01001133 if (!od)
1134 return -ENOMEM;
1135
Russell King596c4712013-12-10 11:08:01 +00001136 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1137 od->base = devm_ioremap_resource(&pdev->dev, res);
1138 if (IS_ERR(od->base))
1139 return PTR_ERR(od->base);
1140
Russell King1b416c42013-11-02 13:00:03 +00001141 od->plat = omap_get_plat_info();
1142 if (!od->plat)
1143 return -EPROBE_DEFER;
1144
Russell King596c4712013-12-10 11:08:01 +00001145 od->reg_map = od->plat->reg_map;
1146
Russell King7bedaa52012-04-13 12:10:24 +01001147 dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
Russell King3a774ea2012-06-21 10:40:15 +01001148 dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +03001149 dma_cap_set(DMA_MEMCPY, od->ddev.cap_mask);
Russell King7bedaa52012-04-13 12:10:24 +01001150 od->ddev.device_alloc_chan_resources = omap_dma_alloc_chan_resources;
1151 od->ddev.device_free_chan_resources = omap_dma_free_chan_resources;
1152 od->ddev.device_tx_status = omap_dma_tx_status;
1153 od->ddev.device_issue_pending = omap_dma_issue_pending;
1154 od->ddev.device_prep_slave_sg = omap_dma_prep_slave_sg;
Russell King3a774ea2012-06-21 10:40:15 +01001155 od->ddev.device_prep_dma_cyclic = omap_dma_prep_dma_cyclic;
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +03001156 od->ddev.device_prep_dma_memcpy = omap_dma_prep_dma_memcpy;
Vinod Koul6c04cd42014-12-07 23:12:31 +05301157 od->ddev.device_config = omap_dma_slave_config;
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001158 od->ddev.device_pause = omap_dma_pause;
1159 od->ddev.device_resume = omap_dma_resume;
1160 od->ddev.device_terminate_all = omap_dma_terminate_all;
Maxime Ripard7d15b872014-11-17 14:42:49 +01001161 od->ddev.src_addr_widths = OMAP_DMA_BUSWIDTHS;
1162 od->ddev.dst_addr_widths = OMAP_DMA_BUSWIDTHS;
1163 od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1164 od->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Russell King7bedaa52012-04-13 12:10:24 +01001165 od->ddev.dev = &pdev->dev;
1166 INIT_LIST_HEAD(&od->ddev.channels);
1167 INIT_LIST_HEAD(&od->pending);
1168 spin_lock_init(&od->lock);
Russell King6ddeb6d2013-12-10 19:05:50 +00001169 spin_lock_init(&od->irq_lock);
Russell King7bedaa52012-04-13 12:10:24 +01001170
1171 tasklet_init(&od->task, omap_dma_sched, (unsigned long)od);
1172
Peter Ujfaluside506082015-04-09 12:35:51 +03001173 od->dma_requests = OMAP_SDMA_REQUESTS;
1174 if (pdev->dev.of_node && of_property_read_u32(pdev->dev.of_node,
1175 "dma-requests",
1176 &od->dma_requests)) {
1177 dev_info(&pdev->dev,
1178 "Missing dma-requests property, using %u.\n",
1179 OMAP_SDMA_REQUESTS);
1180 }
1181
Peter Ujfalusi8a322222015-04-09 12:35:53 +03001182 for (i = 0; i < OMAP_SDMA_CHANNELS; i++) {
Peter Ujfalusieea531e2015-04-09 12:35:52 +03001183 rc = omap_dma_chan_init(od);
Russell King7bedaa52012-04-13 12:10:24 +01001184 if (rc) {
1185 omap_dma_free(od);
1186 return rc;
1187 }
1188 }
1189
Russell King6ddeb6d2013-12-10 19:05:50 +00001190 irq = platform_get_irq(pdev, 1);
1191 if (irq <= 0) {
1192 dev_info(&pdev->dev, "failed to get L1 IRQ: %d\n", irq);
1193 od->legacy = true;
1194 } else {
1195 /* Disable all interrupts */
1196 od->irq_enable_mask = 0;
1197 omap_dma_glbl_write(od, IRQENABLE_L1, 0);
1198
1199 rc = devm_request_irq(&pdev->dev, irq, omap_dma_irq,
1200 IRQF_SHARED, "omap-dma-engine", od);
1201 if (rc)
1202 return rc;
1203 }
1204
Russell King7bedaa52012-04-13 12:10:24 +01001205 rc = dma_async_device_register(&od->ddev);
1206 if (rc) {
1207 pr_warn("OMAP-DMA: failed to register slave DMA engine device: %d\n",
1208 rc);
1209 omap_dma_free(od);
Jon Hunter8d306622013-02-26 12:27:24 -06001210 return rc;
1211 }
1212
1213 platform_set_drvdata(pdev, od);
1214
1215 if (pdev->dev.of_node) {
1216 omap_dma_info.dma_cap = od->ddev.cap_mask;
1217
1218 /* Device-tree DMA controller registration */
1219 rc = of_dma_controller_register(pdev->dev.of_node,
1220 of_dma_simple_xlate, &omap_dma_info);
1221 if (rc) {
1222 pr_warn("OMAP-DMA: failed to register DMA controller\n");
1223 dma_async_device_unregister(&od->ddev);
1224 omap_dma_free(od);
1225 }
Russell King7bedaa52012-04-13 12:10:24 +01001226 }
1227
1228 dev_info(&pdev->dev, "OMAP DMA engine driver\n");
1229
1230 return rc;
1231}
1232
1233static int omap_dma_remove(struct platform_device *pdev)
1234{
1235 struct omap_dmadev *od = platform_get_drvdata(pdev);
1236
Jon Hunter8d306622013-02-26 12:27:24 -06001237 if (pdev->dev.of_node)
1238 of_dma_controller_free(pdev->dev.of_node);
1239
Russell King7bedaa52012-04-13 12:10:24 +01001240 dma_async_device_unregister(&od->ddev);
Russell King6ddeb6d2013-12-10 19:05:50 +00001241
1242 if (!od->legacy) {
1243 /* Disable all interrupts */
1244 omap_dma_glbl_write(od, IRQENABLE_L0, 0);
1245 }
1246
Russell King7bedaa52012-04-13 12:10:24 +01001247 omap_dma_free(od);
1248
1249 return 0;
1250}
1251
Jon Hunter8d306622013-02-26 12:27:24 -06001252static const struct of_device_id omap_dma_match[] = {
1253 { .compatible = "ti,omap2420-sdma", },
1254 { .compatible = "ti,omap2430-sdma", },
1255 { .compatible = "ti,omap3430-sdma", },
1256 { .compatible = "ti,omap3630-sdma", },
1257 { .compatible = "ti,omap4430-sdma", },
1258 {},
1259};
1260MODULE_DEVICE_TABLE(of, omap_dma_match);
1261
Russell King7bedaa52012-04-13 12:10:24 +01001262static struct platform_driver omap_dma_driver = {
1263 .probe = omap_dma_probe,
1264 .remove = omap_dma_remove,
1265 .driver = {
1266 .name = "omap-dma-engine",
Jon Hunter8d306622013-02-26 12:27:24 -06001267 .of_match_table = of_match_ptr(omap_dma_match),
Russell King7bedaa52012-04-13 12:10:24 +01001268 },
1269};
1270
1271bool omap_dma_filter_fn(struct dma_chan *chan, void *param)
1272{
1273 if (chan->device->dev->driver == &omap_dma_driver.driver) {
Peter Ujfalusieea531e2015-04-09 12:35:52 +03001274 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King7bedaa52012-04-13 12:10:24 +01001275 struct omap_chan *c = to_omap_dma_chan(chan);
1276 unsigned req = *(unsigned *)param;
1277
Peter Ujfalusieea531e2015-04-09 12:35:52 +03001278 if (req <= od->dma_requests) {
1279 c->dma_sig = req;
1280 return true;
1281 }
Russell King7bedaa52012-04-13 12:10:24 +01001282 }
1283 return false;
1284}
1285EXPORT_SYMBOL_GPL(omap_dma_filter_fn);
1286
Russell King7bedaa52012-04-13 12:10:24 +01001287static int omap_dma_init(void)
1288{
Tony Lindgrenbe1f9482013-01-11 11:24:19 -08001289 return platform_driver_register(&omap_dma_driver);
Russell King7bedaa52012-04-13 12:10:24 +01001290}
1291subsys_initcall(omap_dma_init);
1292
1293static void __exit omap_dma_exit(void)
1294{
Russell King7bedaa52012-04-13 12:10:24 +01001295 platform_driver_unregister(&omap_dma_driver);
1296}
1297module_exit(omap_dma_exit);
1298
1299MODULE_AUTHOR("Russell King");
1300MODULE_LICENSE("GPL");