blob: ec98e718de700f61ba5f619e1dc3beb39c3d477a [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
Russell King7bedaa52012-04-13 12:10:24 +010025struct omap_dmadev {
26 struct dma_device ddev;
27 spinlock_t lock;
28 struct tasklet_struct task;
29 struct list_head pending;
Russell King596c4712013-12-10 11:08:01 +000030 void __iomem *base;
31 const struct omap_dma_reg *reg_map;
Russell King1b416c42013-11-02 13:00:03 +000032 struct omap_system_dma_plat_info *plat;
Russell King6ddeb6d2013-12-10 19:05:50 +000033 bool legacy;
34 spinlock_t irq_lock;
35 uint32_t irq_enable_mask;
36 struct omap_chan *lch_map[32];
Russell King7bedaa52012-04-13 12:10:24 +010037};
38
39struct omap_chan {
40 struct virt_dma_chan vc;
41 struct list_head node;
Russell King596c4712013-12-10 11:08:01 +000042 void __iomem *channel_base;
43 const struct omap_dma_reg *reg_map;
Russell King7bedaa52012-04-13 12:10:24 +010044
45 struct dma_slave_config cfg;
46 unsigned dma_sig;
Russell King3a774ea2012-06-21 10:40:15 +010047 bool cyclic;
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +030048 bool paused;
Russell King7bedaa52012-04-13 12:10:24 +010049
50 int dma_ch;
51 struct omap_desc *desc;
52 unsigned sgidx;
53};
54
55struct omap_sg {
56 dma_addr_t addr;
57 uint32_t en; /* number of elements (24-bit) */
58 uint32_t fn; /* number of frames (16-bit) */
59};
60
61struct omap_desc {
62 struct virt_dma_desc vd;
63 enum dma_transfer_direction dir;
64 dma_addr_t dev_addr;
65
Russell King7c836bc2012-06-18 16:45:19 +010066 int16_t fi; /* for OMAP_DMA_SYNC_PACKET */
Russell King90438262013-11-02 19:57:06 +000067 uint8_t es; /* CSDP_DATA_TYPE_xxx */
Russell King3ed4d182013-11-02 19:16:09 +000068 uint32_t ccr; /* CCR value */
Russell King965aeb4d2013-11-06 17:12:30 +000069 uint16_t clnk_ctrl; /* CLNK_CTRL value */
Russell Kingfa3ad862013-11-02 17:07:09 +000070 uint16_t cicr; /* CICR value */
Russell King2f0d13b2013-11-02 18:51:53 +000071 uint32_t csdp; /* CSDP value */
Russell King7bedaa52012-04-13 12:10:24 +010072
73 unsigned sglen;
74 struct omap_sg sg[0];
75};
76
Russell King90438262013-11-02 19:57:06 +000077enum {
78 CCR_FS = BIT(5),
79 CCR_READ_PRIORITY = BIT(6),
80 CCR_ENABLE = BIT(7),
81 CCR_AUTO_INIT = BIT(8), /* OMAP1 only */
82 CCR_REPEAT = BIT(9), /* OMAP1 only */
83 CCR_OMAP31_DISABLE = BIT(10), /* OMAP1 only */
84 CCR_SUSPEND_SENSITIVE = BIT(8), /* OMAP2+ only */
85 CCR_RD_ACTIVE = BIT(9), /* OMAP2+ only */
86 CCR_WR_ACTIVE = BIT(10), /* OMAP2+ only */
87 CCR_SRC_AMODE_CONSTANT = 0 << 12,
88 CCR_SRC_AMODE_POSTINC = 1 << 12,
89 CCR_SRC_AMODE_SGLIDX = 2 << 12,
90 CCR_SRC_AMODE_DBLIDX = 3 << 12,
91 CCR_DST_AMODE_CONSTANT = 0 << 14,
92 CCR_DST_AMODE_POSTINC = 1 << 14,
93 CCR_DST_AMODE_SGLIDX = 2 << 14,
94 CCR_DST_AMODE_DBLIDX = 3 << 14,
95 CCR_CONSTANT_FILL = BIT(16),
96 CCR_TRANSPARENT_COPY = BIT(17),
97 CCR_BS = BIT(18),
98 CCR_SUPERVISOR = BIT(22),
99 CCR_PREFETCH = BIT(23),
100 CCR_TRIGGER_SRC = BIT(24),
101 CCR_BUFFERING_DISABLE = BIT(25),
102 CCR_WRITE_PRIORITY = BIT(26),
103 CCR_SYNC_ELEMENT = 0,
104 CCR_SYNC_FRAME = CCR_FS,
105 CCR_SYNC_BLOCK = CCR_BS,
106 CCR_SYNC_PACKET = CCR_BS | CCR_FS,
107
108 CSDP_DATA_TYPE_8 = 0,
109 CSDP_DATA_TYPE_16 = 1,
110 CSDP_DATA_TYPE_32 = 2,
111 CSDP_SRC_PORT_EMIFF = 0 << 2, /* OMAP1 only */
112 CSDP_SRC_PORT_EMIFS = 1 << 2, /* OMAP1 only */
113 CSDP_SRC_PORT_OCP_T1 = 2 << 2, /* OMAP1 only */
114 CSDP_SRC_PORT_TIPB = 3 << 2, /* OMAP1 only */
115 CSDP_SRC_PORT_OCP_T2 = 4 << 2, /* OMAP1 only */
116 CSDP_SRC_PORT_MPUI = 5 << 2, /* OMAP1 only */
117 CSDP_SRC_PACKED = BIT(6),
118 CSDP_SRC_BURST_1 = 0 << 7,
119 CSDP_SRC_BURST_16 = 1 << 7,
120 CSDP_SRC_BURST_32 = 2 << 7,
121 CSDP_SRC_BURST_64 = 3 << 7,
122 CSDP_DST_PORT_EMIFF = 0 << 9, /* OMAP1 only */
123 CSDP_DST_PORT_EMIFS = 1 << 9, /* OMAP1 only */
124 CSDP_DST_PORT_OCP_T1 = 2 << 9, /* OMAP1 only */
125 CSDP_DST_PORT_TIPB = 3 << 9, /* OMAP1 only */
126 CSDP_DST_PORT_OCP_T2 = 4 << 9, /* OMAP1 only */
127 CSDP_DST_PORT_MPUI = 5 << 9, /* OMAP1 only */
128 CSDP_DST_PACKED = BIT(13),
129 CSDP_DST_BURST_1 = 0 << 14,
130 CSDP_DST_BURST_16 = 1 << 14,
131 CSDP_DST_BURST_32 = 2 << 14,
132 CSDP_DST_BURST_64 = 3 << 14,
133
134 CICR_TOUT_IE = BIT(0), /* OMAP1 only */
135 CICR_DROP_IE = BIT(1),
136 CICR_HALF_IE = BIT(2),
137 CICR_FRAME_IE = BIT(3),
138 CICR_LAST_IE = BIT(4),
139 CICR_BLOCK_IE = BIT(5),
140 CICR_PKT_IE = BIT(7), /* OMAP2+ only */
141 CICR_TRANS_ERR_IE = BIT(8), /* OMAP2+ only */
142 CICR_SUPERVISOR_ERR_IE = BIT(10), /* OMAP2+ only */
143 CICR_MISALIGNED_ERR_IE = BIT(11), /* OMAP2+ only */
144 CICR_DRAIN_IE = BIT(12), /* OMAP2+ only */
145 CICR_SUPER_BLOCK_IE = BIT(14), /* OMAP2+ only */
146
147 CLNK_CTRL_ENABLE_LNK = BIT(15),
148};
149
Russell King7bedaa52012-04-13 12:10:24 +0100150static const unsigned es_bytes[] = {
Russell King90438262013-11-02 19:57:06 +0000151 [CSDP_DATA_TYPE_8] = 1,
152 [CSDP_DATA_TYPE_16] = 2,
153 [CSDP_DATA_TYPE_32] = 4,
Russell King7bedaa52012-04-13 12:10:24 +0100154};
155
Jon Hunter8d306622013-02-26 12:27:24 -0600156static struct of_dma_filter_info omap_dma_info = {
157 .filter_fn = omap_dma_filter_fn,
158};
159
Russell King7bedaa52012-04-13 12:10:24 +0100160static inline struct omap_dmadev *to_omap_dma_dev(struct dma_device *d)
161{
162 return container_of(d, struct omap_dmadev, ddev);
163}
164
165static inline struct omap_chan *to_omap_dma_chan(struct dma_chan *c)
166{
167 return container_of(c, struct omap_chan, vc.chan);
168}
169
170static inline struct omap_desc *to_omap_dma_desc(struct dma_async_tx_descriptor *t)
171{
172 return container_of(t, struct omap_desc, vd.tx);
173}
174
175static void omap_dma_desc_free(struct virt_dma_desc *vd)
176{
177 kfree(container_of(vd, struct omap_desc, vd));
178}
179
Russell King596c4712013-12-10 11:08:01 +0000180static void omap_dma_write(uint32_t val, unsigned type, void __iomem *addr)
181{
182 switch (type) {
183 case OMAP_DMA_REG_16BIT:
184 writew_relaxed(val, addr);
185 break;
186 case OMAP_DMA_REG_2X16BIT:
187 writew_relaxed(val, addr);
188 writew_relaxed(val >> 16, addr + 2);
189 break;
190 case OMAP_DMA_REG_32BIT:
191 writel_relaxed(val, addr);
192 break;
193 default:
194 WARN_ON(1);
195 }
196}
197
198static unsigned omap_dma_read(unsigned type, void __iomem *addr)
199{
200 unsigned val;
201
202 switch (type) {
203 case OMAP_DMA_REG_16BIT:
204 val = readw_relaxed(addr);
205 break;
206 case OMAP_DMA_REG_2X16BIT:
207 val = readw_relaxed(addr);
208 val |= readw_relaxed(addr + 2) << 16;
209 break;
210 case OMAP_DMA_REG_32BIT:
211 val = readl_relaxed(addr);
212 break;
213 default:
214 WARN_ON(1);
215 val = 0;
216 }
217
218 return val;
219}
220
Russell Kingc5ed98b2013-11-06 17:33:09 +0000221static void omap_dma_glbl_write(struct omap_dmadev *od, unsigned reg, unsigned val)
222{
Russell King596c4712013-12-10 11:08:01 +0000223 const struct omap_dma_reg *r = od->reg_map + reg;
224
225 WARN_ON(r->stride);
226
227 omap_dma_write(val, r->type, od->base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000228}
229
230static unsigned omap_dma_glbl_read(struct omap_dmadev *od, unsigned reg)
231{
Russell King596c4712013-12-10 11:08:01 +0000232 const struct omap_dma_reg *r = od->reg_map + reg;
233
234 WARN_ON(r->stride);
235
236 return omap_dma_read(r->type, od->base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000237}
238
239static void omap_dma_chan_write(struct omap_chan *c, unsigned reg, unsigned val)
240{
Russell King596c4712013-12-10 11:08:01 +0000241 const struct omap_dma_reg *r = c->reg_map + reg;
242
243 omap_dma_write(val, r->type, c->channel_base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000244}
245
246static unsigned omap_dma_chan_read(struct omap_chan *c, unsigned reg)
247{
Russell King596c4712013-12-10 11:08:01 +0000248 const struct omap_dma_reg *r = c->reg_map + reg;
249
250 return omap_dma_read(r->type, c->channel_base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000251}
252
Russell King470b23f2013-11-02 21:23:06 +0000253static void omap_dma_clear_csr(struct omap_chan *c)
254{
255 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000256 omap_dma_chan_read(c, CSR);
Russell King470b23f2013-11-02 21:23:06 +0000257 else
Russell Kingc5ed98b2013-11-06 17:33:09 +0000258 omap_dma_chan_write(c, CSR, ~0);
Russell King470b23f2013-11-02 21:23:06 +0000259}
260
Russell King6ddeb6d2013-12-10 19:05:50 +0000261static unsigned omap_dma_get_csr(struct omap_chan *c)
262{
263 unsigned val = omap_dma_chan_read(c, CSR);
264
265 if (!dma_omap1())
266 omap_dma_chan_write(c, CSR, val);
267
268 return val;
269}
270
Russell King596c4712013-12-10 11:08:01 +0000271static void omap_dma_assign(struct omap_dmadev *od, struct omap_chan *c,
272 unsigned lch)
273{
274 c->channel_base = od->base + od->plat->channel_stride * lch;
Russell King6ddeb6d2013-12-10 19:05:50 +0000275
276 od->lch_map[lch] = c;
Russell King596c4712013-12-10 11:08:01 +0000277}
278
Russell Kingfa3ad862013-11-02 17:07:09 +0000279static void omap_dma_start(struct omap_chan *c, struct omap_desc *d)
280{
281 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
Russell Kingfa3ad862013-11-02 17:07:09 +0000282
283 if (__dma_omap15xx(od->plat->dma_attr))
Russell Kingc5ed98b2013-11-06 17:33:09 +0000284 omap_dma_chan_write(c, CPC, 0);
Russell Kingfa3ad862013-11-02 17:07:09 +0000285 else
Russell Kingc5ed98b2013-11-06 17:33:09 +0000286 omap_dma_chan_write(c, CDAC, 0);
Russell Kingfa3ad862013-11-02 17:07:09 +0000287
Russell King470b23f2013-11-02 21:23:06 +0000288 omap_dma_clear_csr(c);
Russell Kingfa3ad862013-11-02 17:07:09 +0000289
290 /* Enable interrupts */
Russell Kingc5ed98b2013-11-06 17:33:09 +0000291 omap_dma_chan_write(c, CICR, d->cicr);
Russell Kingfa3ad862013-11-02 17:07:09 +0000292
Russell King45da7b02013-11-06 17:18:42 +0000293 /* Enable channel */
Russell Kingc5ed98b2013-11-06 17:33:09 +0000294 omap_dma_chan_write(c, CCR, d->ccr | CCR_ENABLE);
Russell Kingfa3ad862013-11-02 17:07:09 +0000295}
296
297static void omap_dma_stop(struct omap_chan *c)
298{
299 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
300 uint32_t val;
301
302 /* disable irq */
Russell Kingc5ed98b2013-11-06 17:33:09 +0000303 omap_dma_chan_write(c, CICR, 0);
Russell Kingfa3ad862013-11-02 17:07:09 +0000304
Russell King470b23f2013-11-02 21:23:06 +0000305 omap_dma_clear_csr(c);
Russell Kingfa3ad862013-11-02 17:07:09 +0000306
Russell Kingc5ed98b2013-11-06 17:33:09 +0000307 val = omap_dma_chan_read(c, CCR);
Russell King90438262013-11-02 19:57:06 +0000308 if (od->plat->errata & DMA_ERRATA_i541 && val & CCR_TRIGGER_SRC) {
Russell Kingfa3ad862013-11-02 17:07:09 +0000309 uint32_t sysconfig;
310 unsigned i;
311
Russell Kingc5ed98b2013-11-06 17:33:09 +0000312 sysconfig = omap_dma_glbl_read(od, OCP_SYSCONFIG);
Russell Kingfa3ad862013-11-02 17:07:09 +0000313 val = sysconfig & ~DMA_SYSCONFIG_MIDLEMODE_MASK;
314 val |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000315 omap_dma_glbl_write(od, OCP_SYSCONFIG, val);
Russell Kingfa3ad862013-11-02 17:07:09 +0000316
Russell Kingc5ed98b2013-11-06 17:33:09 +0000317 val = omap_dma_chan_read(c, CCR);
Russell King90438262013-11-02 19:57:06 +0000318 val &= ~CCR_ENABLE;
Russell Kingc5ed98b2013-11-06 17:33:09 +0000319 omap_dma_chan_write(c, CCR, val);
Russell Kingfa3ad862013-11-02 17:07:09 +0000320
321 /* Wait for sDMA FIFO to drain */
322 for (i = 0; ; i++) {
Russell Kingc5ed98b2013-11-06 17:33:09 +0000323 val = omap_dma_chan_read(c, CCR);
Russell King90438262013-11-02 19:57:06 +0000324 if (!(val & (CCR_RD_ACTIVE | CCR_WR_ACTIVE)))
Russell Kingfa3ad862013-11-02 17:07:09 +0000325 break;
326
327 if (i > 100)
328 break;
329
330 udelay(5);
331 }
332
Russell King90438262013-11-02 19:57:06 +0000333 if (val & (CCR_RD_ACTIVE | CCR_WR_ACTIVE))
Russell Kingfa3ad862013-11-02 17:07:09 +0000334 dev_err(c->vc.chan.device->dev,
335 "DMA drain did not complete on lch %d\n",
336 c->dma_ch);
337
Russell Kingc5ed98b2013-11-06 17:33:09 +0000338 omap_dma_glbl_write(od, OCP_SYSCONFIG, sysconfig);
Russell Kingfa3ad862013-11-02 17:07:09 +0000339 } else {
Russell King90438262013-11-02 19:57:06 +0000340 val &= ~CCR_ENABLE;
Russell Kingc5ed98b2013-11-06 17:33:09 +0000341 omap_dma_chan_write(c, CCR, val);
Russell Kingfa3ad862013-11-02 17:07:09 +0000342 }
343
344 mb();
345
346 if (!__dma_omap15xx(od->plat->dma_attr) && c->cyclic) {
Russell Kingc5ed98b2013-11-06 17:33:09 +0000347 val = omap_dma_chan_read(c, CLNK_CTRL);
Russell Kingfa3ad862013-11-02 17:07:09 +0000348
349 if (dma_omap1())
350 val |= 1 << 14; /* set the STOP_LNK bit */
351 else
Russell King90438262013-11-02 19:57:06 +0000352 val &= ~CLNK_CTRL_ENABLE_LNK;
Russell Kingfa3ad862013-11-02 17:07:09 +0000353
Russell Kingc5ed98b2013-11-06 17:33:09 +0000354 omap_dma_chan_write(c, CLNK_CTRL, val);
Russell Kingfa3ad862013-11-02 17:07:09 +0000355 }
356}
357
Russell King7bedaa52012-04-13 12:10:24 +0100358static void omap_dma_start_sg(struct omap_chan *c, struct omap_desc *d,
359 unsigned idx)
360{
361 struct omap_sg *sg = d->sg + idx;
Russell King893e63e2013-11-03 11:17:11 +0000362 unsigned cxsa, cxei, cxfi;
Russell King7bedaa52012-04-13 12:10:24 +0100363
Russell Kingb9e97822013-11-02 13:26:57 +0000364 if (d->dir == DMA_DEV_TO_MEM) {
Russell King893e63e2013-11-03 11:17:11 +0000365 cxsa = CDSA;
366 cxei = CDEI;
367 cxfi = CDFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000368 } else {
Russell King893e63e2013-11-03 11:17:11 +0000369 cxsa = CSSA;
370 cxei = CSEI;
371 cxfi = CSFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000372 }
373
Russell Kingc5ed98b2013-11-06 17:33:09 +0000374 omap_dma_chan_write(c, cxsa, sg->addr);
375 omap_dma_chan_write(c, cxei, 0);
376 omap_dma_chan_write(c, cxfi, 0);
377 omap_dma_chan_write(c, CEN, sg->en);
378 omap_dma_chan_write(c, CFN, sg->fn);
Russell King7bedaa52012-04-13 12:10:24 +0100379
Russell Kingfa3ad862013-11-02 17:07:09 +0000380 omap_dma_start(c, d);
Russell King7bedaa52012-04-13 12:10:24 +0100381}
382
383static void omap_dma_start_desc(struct omap_chan *c)
384{
385 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
386 struct omap_desc *d;
Russell King893e63e2013-11-03 11:17:11 +0000387 unsigned cxsa, cxei, cxfi;
Russell King7bedaa52012-04-13 12:10:24 +0100388
389 if (!vd) {
390 c->desc = NULL;
391 return;
392 }
393
394 list_del(&vd->node);
395
396 c->desc = d = to_omap_dma_desc(&vd->tx);
397 c->sgidx = 0;
398
Russell King59871902013-11-06 17:15:16 +0000399 /*
400 * This provides the necessary barrier to ensure data held in
401 * DMA coherent memory is visible to the DMA engine prior to
402 * the transfer starting.
403 */
404 mb();
405
Russell Kingc5ed98b2013-11-06 17:33:09 +0000406 omap_dma_chan_write(c, CCR, d->ccr);
Russell King3ed4d182013-11-02 19:16:09 +0000407 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000408 omap_dma_chan_write(c, CCR2, d->ccr >> 16);
Russell Kingb9e97822013-11-02 13:26:57 +0000409
Russell King3ed4d182013-11-02 19:16:09 +0000410 if (d->dir == DMA_DEV_TO_MEM) {
Russell King893e63e2013-11-03 11:17:11 +0000411 cxsa = CSSA;
412 cxei = CSEI;
413 cxfi = CSFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000414 } else {
Russell King893e63e2013-11-03 11:17:11 +0000415 cxsa = CDSA;
416 cxei = CDEI;
417 cxfi = CDFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000418 }
Russell King7bedaa52012-04-13 12:10:24 +0100419
Russell Kingc5ed98b2013-11-06 17:33:09 +0000420 omap_dma_chan_write(c, cxsa, d->dev_addr);
421 omap_dma_chan_write(c, cxei, 0);
422 omap_dma_chan_write(c, cxfi, d->fi);
423 omap_dma_chan_write(c, CSDP, d->csdp);
424 omap_dma_chan_write(c, CLNK_CTRL, d->clnk_ctrl);
Russell King913a2d02013-11-02 14:41:42 +0000425
Russell King7bedaa52012-04-13 12:10:24 +0100426 omap_dma_start_sg(c, d, 0);
427}
428
429static void omap_dma_callback(int ch, u16 status, void *data)
430{
431 struct omap_chan *c = data;
432 struct omap_desc *d;
433 unsigned long flags;
434
435 spin_lock_irqsave(&c->vc.lock, flags);
436 d = c->desc;
437 if (d) {
Russell King3a774ea2012-06-21 10:40:15 +0100438 if (!c->cyclic) {
439 if (++c->sgidx < d->sglen) {
440 omap_dma_start_sg(c, d, c->sgidx);
441 } else {
442 omap_dma_start_desc(c);
443 vchan_cookie_complete(&d->vd);
444 }
Russell King7bedaa52012-04-13 12:10:24 +0100445 } else {
Russell King3a774ea2012-06-21 10:40:15 +0100446 vchan_cyclic_callback(&d->vd);
Russell King7bedaa52012-04-13 12:10:24 +0100447 }
448 }
449 spin_unlock_irqrestore(&c->vc.lock, flags);
450}
451
452/*
453 * This callback schedules all pending channels. We could be more
454 * clever here by postponing allocation of the real DMA channels to
455 * this point, and freeing them when our virtual channel becomes idle.
456 *
457 * We would then need to deal with 'all channels in-use'
458 */
459static void omap_dma_sched(unsigned long data)
460{
461 struct omap_dmadev *d = (struct omap_dmadev *)data;
462 LIST_HEAD(head);
463
464 spin_lock_irq(&d->lock);
465 list_splice_tail_init(&d->pending, &head);
466 spin_unlock_irq(&d->lock);
467
468 while (!list_empty(&head)) {
469 struct omap_chan *c = list_first_entry(&head,
470 struct omap_chan, node);
471
472 spin_lock_irq(&c->vc.lock);
473 list_del_init(&c->node);
474 omap_dma_start_desc(c);
475 spin_unlock_irq(&c->vc.lock);
476 }
477}
478
Russell King6ddeb6d2013-12-10 19:05:50 +0000479static irqreturn_t omap_dma_irq(int irq, void *devid)
480{
481 struct omap_dmadev *od = devid;
482 unsigned status, channel;
483
484 spin_lock(&od->irq_lock);
485
486 status = omap_dma_glbl_read(od, IRQSTATUS_L1);
487 status &= od->irq_enable_mask;
488 if (status == 0) {
489 spin_unlock(&od->irq_lock);
490 return IRQ_NONE;
491 }
492
493 while ((channel = ffs(status)) != 0) {
494 unsigned mask, csr;
495 struct omap_chan *c;
496
497 channel -= 1;
498 mask = BIT(channel);
499 status &= ~mask;
500
501 c = od->lch_map[channel];
502 if (c == NULL) {
503 /* This should never happen */
504 dev_err(od->ddev.dev, "invalid channel %u\n", channel);
505 continue;
506 }
507
508 csr = omap_dma_get_csr(c);
509 omap_dma_glbl_write(od, IRQSTATUS_L1, mask);
510
511 omap_dma_callback(channel, csr, c);
512 }
513
514 spin_unlock(&od->irq_lock);
515
516 return IRQ_HANDLED;
517}
518
Russell King7bedaa52012-04-13 12:10:24 +0100519static int omap_dma_alloc_chan_resources(struct dma_chan *chan)
520{
Russell King596c4712013-12-10 11:08:01 +0000521 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King7bedaa52012-04-13 12:10:24 +0100522 struct omap_chan *c = to_omap_dma_chan(chan);
Russell King596c4712013-12-10 11:08:01 +0000523 int ret;
Russell King7bedaa52012-04-13 12:10:24 +0100524
Russell King6ddeb6d2013-12-10 19:05:50 +0000525 if (od->legacy) {
526 ret = omap_request_dma(c->dma_sig, "DMA engine",
527 omap_dma_callback, c, &c->dma_ch);
528 } else {
529 ret = omap_request_dma(c->dma_sig, "DMA engine", NULL, NULL,
530 &c->dma_ch);
531 }
Russell King7bedaa52012-04-13 12:10:24 +0100532
Russell King6ddeb6d2013-12-10 19:05:50 +0000533 dev_dbg(od->ddev.dev, "allocating channel %u for %u\n",
534 c->dma_ch, c->dma_sig);
Russell King596c4712013-12-10 11:08:01 +0000535
Russell King6ddeb6d2013-12-10 19:05:50 +0000536 if (ret >= 0) {
Russell King596c4712013-12-10 11:08:01 +0000537 omap_dma_assign(od, c, c->dma_ch);
538
Russell King6ddeb6d2013-12-10 19:05:50 +0000539 if (!od->legacy) {
540 unsigned val;
541
542 spin_lock_irq(&od->irq_lock);
543 val = BIT(c->dma_ch);
544 omap_dma_glbl_write(od, IRQSTATUS_L1, val);
545 od->irq_enable_mask |= val;
546 omap_dma_glbl_write(od, IRQENABLE_L1, od->irq_enable_mask);
547
548 val = omap_dma_glbl_read(od, IRQENABLE_L0);
549 val &= ~BIT(c->dma_ch);
550 omap_dma_glbl_write(od, IRQENABLE_L0, val);
551 spin_unlock_irq(&od->irq_lock);
552 }
553 }
554
Russell King596c4712013-12-10 11:08:01 +0000555 return ret;
Russell King7bedaa52012-04-13 12:10:24 +0100556}
557
558static void omap_dma_free_chan_resources(struct dma_chan *chan)
559{
Russell King6ddeb6d2013-12-10 19:05:50 +0000560 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King7bedaa52012-04-13 12:10:24 +0100561 struct omap_chan *c = to_omap_dma_chan(chan);
562
Russell King6ddeb6d2013-12-10 19:05:50 +0000563 if (!od->legacy) {
564 spin_lock_irq(&od->irq_lock);
565 od->irq_enable_mask &= ~BIT(c->dma_ch);
566 omap_dma_glbl_write(od, IRQENABLE_L1, od->irq_enable_mask);
567 spin_unlock_irq(&od->irq_lock);
568 }
569
Russell King596c4712013-12-10 11:08:01 +0000570 c->channel_base = NULL;
Russell King6ddeb6d2013-12-10 19:05:50 +0000571 od->lch_map[c->dma_ch] = NULL;
Russell King7bedaa52012-04-13 12:10:24 +0100572 vchan_free_chan_resources(&c->vc);
573 omap_free_dma(c->dma_ch);
574
Russell King6ddeb6d2013-12-10 19:05:50 +0000575 dev_dbg(od->ddev.dev, "freeing channel for %u\n", c->dma_sig);
Russell King7bedaa52012-04-13 12:10:24 +0100576}
577
Russell King3850e222012-06-21 10:37:35 +0100578static size_t omap_dma_sg_size(struct omap_sg *sg)
579{
580 return sg->en * sg->fn;
581}
582
583static size_t omap_dma_desc_size(struct omap_desc *d)
584{
585 unsigned i;
586 size_t size;
587
588 for (size = i = 0; i < d->sglen; i++)
589 size += omap_dma_sg_size(&d->sg[i]);
590
591 return size * es_bytes[d->es];
592}
593
594static size_t omap_dma_desc_size_pos(struct omap_desc *d, dma_addr_t addr)
595{
596 unsigned i;
597 size_t size, es_size = es_bytes[d->es];
598
599 for (size = i = 0; i < d->sglen; i++) {
600 size_t this_size = omap_dma_sg_size(&d->sg[i]) * es_size;
601
602 if (size)
603 size += this_size;
604 else if (addr >= d->sg[i].addr &&
605 addr < d->sg[i].addr + this_size)
606 size += d->sg[i].addr + this_size - addr;
607 }
608 return size;
609}
610
Russell Kingb07fd622013-11-06 19:26:45 +0000611/*
612 * OMAP 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
613 * read before the DMA controller finished disabling the channel.
614 */
615static uint32_t omap_dma_chan_read_3_3(struct omap_chan *c, unsigned reg)
616{
617 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
618 uint32_t val;
619
620 val = omap_dma_chan_read(c, reg);
621 if (val == 0 && od->plat->errata & DMA_ERRATA_3_3)
622 val = omap_dma_chan_read(c, reg);
623
624 return val;
625}
626
Russell King3997cab2013-11-02 18:04:17 +0000627static dma_addr_t omap_dma_get_src_pos(struct omap_chan *c)
628{
629 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
Russell Kingb07fd622013-11-06 19:26:45 +0000630 dma_addr_t addr, cdac;
Russell King3997cab2013-11-02 18:04:17 +0000631
Russell Kingb07fd622013-11-06 19:26:45 +0000632 if (__dma_omap15xx(od->plat->dma_attr)) {
Russell Kingc5ed98b2013-11-06 17:33:09 +0000633 addr = omap_dma_chan_read(c, CPC);
Russell Kingb07fd622013-11-06 19:26:45 +0000634 } else {
635 addr = omap_dma_chan_read_3_3(c, CSAC);
636 cdac = omap_dma_chan_read_3_3(c, CDAC);
Russell King3997cab2013-11-02 18:04:17 +0000637
Russell King3997cab2013-11-02 18:04:17 +0000638 /*
639 * CDAC == 0 indicates that the DMA transfer on the channel has
640 * not been started (no data has been transferred so far).
641 * Return the programmed source start address in this case.
642 */
Russell Kingb07fd622013-11-06 19:26:45 +0000643 if (cdac == 0)
Russell Kingc5ed98b2013-11-06 17:33:09 +0000644 addr = omap_dma_chan_read(c, CSSA);
Russell King3997cab2013-11-02 18:04:17 +0000645 }
646
647 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000648 addr |= omap_dma_chan_read(c, CSSA) & 0xffff0000;
Russell King3997cab2013-11-02 18:04:17 +0000649
650 return addr;
651}
652
653static dma_addr_t omap_dma_get_dst_pos(struct omap_chan *c)
654{
655 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
656 dma_addr_t addr;
657
Russell Kingb07fd622013-11-06 19:26:45 +0000658 if (__dma_omap15xx(od->plat->dma_attr)) {
Russell Kingc5ed98b2013-11-06 17:33:09 +0000659 addr = omap_dma_chan_read(c, CPC);
Russell Kingb07fd622013-11-06 19:26:45 +0000660 } else {
661 addr = omap_dma_chan_read_3_3(c, CDAC);
Russell King3997cab2013-11-02 18:04:17 +0000662
Russell King3997cab2013-11-02 18:04:17 +0000663 /*
Russell Kingb07fd622013-11-06 19:26:45 +0000664 * CDAC == 0 indicates that the DMA transfer on the channel
665 * has not been started (no data has been transferred so
666 * far). Return the programmed destination start address in
667 * this case.
Russell King3997cab2013-11-02 18:04:17 +0000668 */
669 if (addr == 0)
Russell Kingc5ed98b2013-11-06 17:33:09 +0000670 addr = omap_dma_chan_read(c, CDSA);
Russell King3997cab2013-11-02 18:04:17 +0000671 }
672
673 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000674 addr |= omap_dma_chan_read(c, CDSA) & 0xffff0000;
Russell King3997cab2013-11-02 18:04:17 +0000675
676 return addr;
677}
678
Russell King7bedaa52012-04-13 12:10:24 +0100679static enum dma_status omap_dma_tx_status(struct dma_chan *chan,
680 dma_cookie_t cookie, struct dma_tx_state *txstate)
681{
Russell King3850e222012-06-21 10:37:35 +0100682 struct omap_chan *c = to_omap_dma_chan(chan);
683 struct virt_dma_desc *vd;
684 enum dma_status ret;
685 unsigned long flags;
686
687 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul7cce5082013-10-16 20:51:54 +0530688 if (ret == DMA_COMPLETE || !txstate)
Russell King3850e222012-06-21 10:37:35 +0100689 return ret;
690
691 spin_lock_irqsave(&c->vc.lock, flags);
692 vd = vchan_find_desc(&c->vc, cookie);
693 if (vd) {
694 txstate->residue = omap_dma_desc_size(to_omap_dma_desc(&vd->tx));
695 } else if (c->desc && c->desc->vd.tx.cookie == cookie) {
696 struct omap_desc *d = c->desc;
697 dma_addr_t pos;
698
699 if (d->dir == DMA_MEM_TO_DEV)
Russell King3997cab2013-11-02 18:04:17 +0000700 pos = omap_dma_get_src_pos(c);
Russell King3850e222012-06-21 10:37:35 +0100701 else if (d->dir == DMA_DEV_TO_MEM)
Russell King3997cab2013-11-02 18:04:17 +0000702 pos = omap_dma_get_dst_pos(c);
Russell King3850e222012-06-21 10:37:35 +0100703 else
704 pos = 0;
705
706 txstate->residue = omap_dma_desc_size_pos(d, pos);
707 } else {
708 txstate->residue = 0;
709 }
710 spin_unlock_irqrestore(&c->vc.lock, flags);
711
712 return ret;
Russell King7bedaa52012-04-13 12:10:24 +0100713}
714
715static void omap_dma_issue_pending(struct dma_chan *chan)
716{
717 struct omap_chan *c = to_omap_dma_chan(chan);
718 unsigned long flags;
719
720 spin_lock_irqsave(&c->vc.lock, flags);
721 if (vchan_issue_pending(&c->vc) && !c->desc) {
Peter Ujfalusi76502462013-04-09 16:33:06 +0200722 /*
723 * c->cyclic is used only by audio and in this case the DMA need
724 * to be started without delay.
725 */
726 if (!c->cyclic) {
727 struct omap_dmadev *d = to_omap_dma_dev(chan->device);
728 spin_lock(&d->lock);
729 if (list_empty(&c->node))
730 list_add_tail(&c->node, &d->pending);
731 spin_unlock(&d->lock);
732 tasklet_schedule(&d->task);
733 } else {
734 omap_dma_start_desc(c);
735 }
Russell King7bedaa52012-04-13 12:10:24 +0100736 }
737 spin_unlock_irqrestore(&c->vc.lock, flags);
738}
739
740static struct dma_async_tx_descriptor *omap_dma_prep_slave_sg(
741 struct dma_chan *chan, struct scatterlist *sgl, unsigned sglen,
742 enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
743{
Russell King49ae0b22013-11-02 21:09:18 +0000744 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King7bedaa52012-04-13 12:10:24 +0100745 struct omap_chan *c = to_omap_dma_chan(chan);
746 enum dma_slave_buswidth dev_width;
747 struct scatterlist *sgent;
748 struct omap_desc *d;
749 dma_addr_t dev_addr;
Russell King3ed4d182013-11-02 19:16:09 +0000750 unsigned i, j = 0, es, en, frame_bytes;
Russell King7bedaa52012-04-13 12:10:24 +0100751 u32 burst;
752
753 if (dir == DMA_DEV_TO_MEM) {
754 dev_addr = c->cfg.src_addr;
755 dev_width = c->cfg.src_addr_width;
756 burst = c->cfg.src_maxburst;
Russell King7bedaa52012-04-13 12:10:24 +0100757 } else if (dir == DMA_MEM_TO_DEV) {
758 dev_addr = c->cfg.dst_addr;
759 dev_width = c->cfg.dst_addr_width;
760 burst = c->cfg.dst_maxburst;
Russell King7bedaa52012-04-13 12:10:24 +0100761 } else {
762 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
763 return NULL;
764 }
765
766 /* Bus width translates to the element size (ES) */
767 switch (dev_width) {
768 case DMA_SLAVE_BUSWIDTH_1_BYTE:
Russell King90438262013-11-02 19:57:06 +0000769 es = CSDP_DATA_TYPE_8;
Russell King7bedaa52012-04-13 12:10:24 +0100770 break;
771 case DMA_SLAVE_BUSWIDTH_2_BYTES:
Russell King90438262013-11-02 19:57:06 +0000772 es = CSDP_DATA_TYPE_16;
Russell King7bedaa52012-04-13 12:10:24 +0100773 break;
774 case DMA_SLAVE_BUSWIDTH_4_BYTES:
Russell King90438262013-11-02 19:57:06 +0000775 es = CSDP_DATA_TYPE_32;
Russell King7bedaa52012-04-13 12:10:24 +0100776 break;
777 default: /* not reached */
778 return NULL;
779 }
780
781 /* Now allocate and setup the descriptor. */
782 d = kzalloc(sizeof(*d) + sglen * sizeof(d->sg[0]), GFP_ATOMIC);
783 if (!d)
784 return NULL;
785
786 d->dir = dir;
787 d->dev_addr = dev_addr;
788 d->es = es;
Russell King3ed4d182013-11-02 19:16:09 +0000789
Russell King90438262013-11-02 19:57:06 +0000790 d->ccr = CCR_SYNC_FRAME;
Russell King3ed4d182013-11-02 19:16:09 +0000791 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000792 d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_CONSTANT;
Russell King3ed4d182013-11-02 19:16:09 +0000793 else
Russell King90438262013-11-02 19:57:06 +0000794 d->ccr |= CCR_DST_AMODE_CONSTANT | CCR_SRC_AMODE_POSTINC;
Russell King3ed4d182013-11-02 19:16:09 +0000795
Russell King90438262013-11-02 19:57:06 +0000796 d->cicr = CICR_DROP_IE | CICR_BLOCK_IE;
Russell King2f0d13b2013-11-02 18:51:53 +0000797 d->csdp = es;
Russell Kingfa3ad862013-11-02 17:07:09 +0000798
Russell King2f0d13b2013-11-02 18:51:53 +0000799 if (dma_omap1()) {
Russell King3ed4d182013-11-02 19:16:09 +0000800 if (__dma_omap16xx(od->plat->dma_attr)) {
Russell King90438262013-11-02 19:57:06 +0000801 d->ccr |= CCR_OMAP31_DISABLE;
Russell King3ed4d182013-11-02 19:16:09 +0000802 /* Duplicate what plat-omap/dma.c does */
803 d->ccr |= c->dma_ch + 1;
804 } else {
805 d->ccr |= c->dma_sig & 0x1f;
806 }
807
Russell King90438262013-11-02 19:57:06 +0000808 d->cicr |= CICR_TOUT_IE;
Russell King2f0d13b2013-11-02 18:51:53 +0000809
810 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000811 d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_TIPB;
Russell King2f0d13b2013-11-02 18:51:53 +0000812 else
Russell King90438262013-11-02 19:57:06 +0000813 d->csdp |= CSDP_DST_PORT_TIPB | CSDP_SRC_PORT_EMIFF;
Russell King2f0d13b2013-11-02 18:51:53 +0000814 } else {
Russell King3ed4d182013-11-02 19:16:09 +0000815 d->ccr |= (c->dma_sig & ~0x1f) << 14;
816 d->ccr |= c->dma_sig & 0x1f;
Russell King3ed4d182013-11-02 19:16:09 +0000817
818 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000819 d->ccr |= CCR_TRIGGER_SRC;
Russell King3ed4d182013-11-02 19:16:09 +0000820
Russell King90438262013-11-02 19:57:06 +0000821 d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
Russell King2f0d13b2013-11-02 18:51:53 +0000822 }
Russell King49ae0b22013-11-02 21:09:18 +0000823 if (od->plat->errata & DMA_ERRATA_IFRAME_BUFFERING)
824 d->ccr |= CCR_BUFFERING_DISABLE;
Russell King965aeb4d2013-11-06 17:12:30 +0000825 if (od->plat->errata & DMA_ERRATA_PARALLEL_CHANNELS)
826 d->clnk_ctrl = c->dma_ch;
Russell King7bedaa52012-04-13 12:10:24 +0100827
828 /*
829 * Build our scatterlist entries: each contains the address,
830 * the number of elements (EN) in each frame, and the number of
831 * frames (FN). Number of bytes for this entry = ES * EN * FN.
832 *
833 * Burst size translates to number of elements with frame sync.
834 * Note: DMA engine defines burst to be the number of dev-width
835 * transfers.
836 */
837 en = burst;
838 frame_bytes = es_bytes[es] * en;
839 for_each_sg(sgl, sgent, sglen, i) {
840 d->sg[j].addr = sg_dma_address(sgent);
841 d->sg[j].en = en;
842 d->sg[j].fn = sg_dma_len(sgent) / frame_bytes;
843 j++;
844 }
845
846 d->sglen = j;
847
848 return vchan_tx_prep(&c->vc, &d->vd, tx_flags);
849}
850
Russell King3a774ea2012-06-21 10:40:15 +0100851static struct dma_async_tx_descriptor *omap_dma_prep_dma_cyclic(
852 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
Peter Ujfalusiec8b5e42012-09-14 15:05:47 +0300853 size_t period_len, enum dma_transfer_direction dir, unsigned long flags,
854 void *context)
Russell King3a774ea2012-06-21 10:40:15 +0100855{
Russell Kingfa3ad862013-11-02 17:07:09 +0000856 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King3a774ea2012-06-21 10:40:15 +0100857 struct omap_chan *c = to_omap_dma_chan(chan);
858 enum dma_slave_buswidth dev_width;
859 struct omap_desc *d;
860 dma_addr_t dev_addr;
Russell King3ed4d182013-11-02 19:16:09 +0000861 unsigned es;
Russell King3a774ea2012-06-21 10:40:15 +0100862 u32 burst;
863
864 if (dir == DMA_DEV_TO_MEM) {
865 dev_addr = c->cfg.src_addr;
866 dev_width = c->cfg.src_addr_width;
867 burst = c->cfg.src_maxburst;
Russell King3a774ea2012-06-21 10:40:15 +0100868 } else if (dir == DMA_MEM_TO_DEV) {
869 dev_addr = c->cfg.dst_addr;
870 dev_width = c->cfg.dst_addr_width;
871 burst = c->cfg.dst_maxburst;
Russell King3a774ea2012-06-21 10:40:15 +0100872 } else {
873 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
874 return NULL;
875 }
876
877 /* Bus width translates to the element size (ES) */
878 switch (dev_width) {
879 case DMA_SLAVE_BUSWIDTH_1_BYTE:
Russell King90438262013-11-02 19:57:06 +0000880 es = CSDP_DATA_TYPE_8;
Russell King3a774ea2012-06-21 10:40:15 +0100881 break;
882 case DMA_SLAVE_BUSWIDTH_2_BYTES:
Russell King90438262013-11-02 19:57:06 +0000883 es = CSDP_DATA_TYPE_16;
Russell King3a774ea2012-06-21 10:40:15 +0100884 break;
885 case DMA_SLAVE_BUSWIDTH_4_BYTES:
Russell King90438262013-11-02 19:57:06 +0000886 es = CSDP_DATA_TYPE_32;
Russell King3a774ea2012-06-21 10:40:15 +0100887 break;
888 default: /* not reached */
889 return NULL;
890 }
891
892 /* Now allocate and setup the descriptor. */
893 d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC);
894 if (!d)
895 return NULL;
896
897 d->dir = dir;
898 d->dev_addr = dev_addr;
899 d->fi = burst;
900 d->es = es;
Russell King3a774ea2012-06-21 10:40:15 +0100901 d->sg[0].addr = buf_addr;
902 d->sg[0].en = period_len / es_bytes[es];
903 d->sg[0].fn = buf_len / period_len;
904 d->sglen = 1;
Russell King3ed4d182013-11-02 19:16:09 +0000905
906 d->ccr = 0;
Russell King3ed4d182013-11-02 19:16:09 +0000907 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000908 d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_CONSTANT;
Russell King3ed4d182013-11-02 19:16:09 +0000909 else
Russell King90438262013-11-02 19:57:06 +0000910 d->ccr |= CCR_DST_AMODE_CONSTANT | CCR_SRC_AMODE_POSTINC;
Russell King3ed4d182013-11-02 19:16:09 +0000911
Russell King90438262013-11-02 19:57:06 +0000912 d->cicr = CICR_DROP_IE;
Russell Kingfa3ad862013-11-02 17:07:09 +0000913 if (flags & DMA_PREP_INTERRUPT)
Russell King90438262013-11-02 19:57:06 +0000914 d->cicr |= CICR_FRAME_IE;
Russell Kingfa3ad862013-11-02 17:07:09 +0000915
Russell King2f0d13b2013-11-02 18:51:53 +0000916 d->csdp = es;
917
918 if (dma_omap1()) {
Russell King3ed4d182013-11-02 19:16:09 +0000919 if (__dma_omap16xx(od->plat->dma_attr)) {
Russell King90438262013-11-02 19:57:06 +0000920 d->ccr |= CCR_OMAP31_DISABLE;
Russell King3ed4d182013-11-02 19:16:09 +0000921 /* Duplicate what plat-omap/dma.c does */
922 d->ccr |= c->dma_ch + 1;
923 } else {
924 d->ccr |= c->dma_sig & 0x1f;
925 }
926
Russell King90438262013-11-02 19:57:06 +0000927 d->cicr |= CICR_TOUT_IE;
Russell King2f0d13b2013-11-02 18:51:53 +0000928
929 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000930 d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_MPUI;
Russell King2f0d13b2013-11-02 18:51:53 +0000931 else
Russell King90438262013-11-02 19:57:06 +0000932 d->csdp |= CSDP_DST_PORT_MPUI | CSDP_SRC_PORT_EMIFF;
Russell King2f0d13b2013-11-02 18:51:53 +0000933 } else {
Russell King3ed4d182013-11-02 19:16:09 +0000934 d->ccr |= (c->dma_sig & ~0x1f) << 14;
935 d->ccr |= c->dma_sig & 0x1f;
936
937 if (burst)
Russell King90438262013-11-02 19:57:06 +0000938 d->ccr |= CCR_SYNC_PACKET;
939 else
940 d->ccr |= CCR_SYNC_ELEMENT;
Russell King3ed4d182013-11-02 19:16:09 +0000941
942 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000943 d->ccr |= CCR_TRIGGER_SRC;
Russell King3ed4d182013-11-02 19:16:09 +0000944
Russell King90438262013-11-02 19:57:06 +0000945 d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
Russell King3a774ea2012-06-21 10:40:15 +0100946
Russell King90438262013-11-02 19:57:06 +0000947 d->csdp |= CSDP_DST_BURST_64 | CSDP_SRC_BURST_64;
Russell King2f0d13b2013-11-02 18:51:53 +0000948 }
Russell King49ae0b22013-11-02 21:09:18 +0000949 if (od->plat->errata & DMA_ERRATA_IFRAME_BUFFERING)
950 d->ccr |= CCR_BUFFERING_DISABLE;
Russell King2f0d13b2013-11-02 18:51:53 +0000951
Russell King965aeb4d2013-11-06 17:12:30 +0000952 if (__dma_omap15xx(od->plat->dma_attr))
953 d->ccr |= CCR_AUTO_INIT | CCR_REPEAT;
954 else
955 d->clnk_ctrl = c->dma_ch | CLNK_CTRL_ENABLE_LNK;
956
Russell King3ed4d182013-11-02 19:16:09 +0000957 c->cyclic = true;
Russell King3a774ea2012-06-21 10:40:15 +0100958
Peter Ujfalusi2dde5b92012-09-14 15:05:48 +0300959 return vchan_tx_prep(&c->vc, &d->vd, flags);
Russell King3a774ea2012-06-21 10:40:15 +0100960}
961
Russell King7bedaa52012-04-13 12:10:24 +0100962static int omap_dma_slave_config(struct omap_chan *c, struct dma_slave_config *cfg)
963{
964 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
965 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
966 return -EINVAL;
967
968 memcpy(&c->cfg, cfg, sizeof(c->cfg));
969
970 return 0;
971}
972
973static int omap_dma_terminate_all(struct omap_chan *c)
974{
975 struct omap_dmadev *d = to_omap_dma_dev(c->vc.chan.device);
976 unsigned long flags;
977 LIST_HEAD(head);
978
979 spin_lock_irqsave(&c->vc.lock, flags);
980
981 /* Prevent this channel being scheduled */
982 spin_lock(&d->lock);
983 list_del_init(&c->node);
984 spin_unlock(&d->lock);
985
986 /*
987 * Stop DMA activity: we assume the callback will not be called
Russell Kingfa3ad862013-11-02 17:07:09 +0000988 * after omap_dma_stop() returns (even if it does, it will see
Russell King7bedaa52012-04-13 12:10:24 +0100989 * c->desc is NULL and exit.)
990 */
991 if (c->desc) {
992 c->desc = NULL;
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +0300993 /* Avoid stopping the dma twice */
994 if (!c->paused)
Russell Kingfa3ad862013-11-02 17:07:09 +0000995 omap_dma_stop(c);
Russell King7bedaa52012-04-13 12:10:24 +0100996 }
997
Russell King3a774ea2012-06-21 10:40:15 +0100998 if (c->cyclic) {
999 c->cyclic = false;
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001000 c->paused = false;
Russell King3a774ea2012-06-21 10:40:15 +01001001 }
1002
Russell King7bedaa52012-04-13 12:10:24 +01001003 vchan_get_all_descriptors(&c->vc, &head);
1004 spin_unlock_irqrestore(&c->vc.lock, flags);
1005 vchan_dma_desc_free_list(&c->vc, &head);
1006
1007 return 0;
1008}
1009
1010static int omap_dma_pause(struct omap_chan *c)
1011{
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001012 /* Pause/Resume only allowed with cyclic mode */
1013 if (!c->cyclic)
1014 return -EINVAL;
1015
1016 if (!c->paused) {
Russell Kingfa3ad862013-11-02 17:07:09 +00001017 omap_dma_stop(c);
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001018 c->paused = true;
1019 }
1020
1021 return 0;
Russell King7bedaa52012-04-13 12:10:24 +01001022}
1023
1024static int omap_dma_resume(struct omap_chan *c)
1025{
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001026 /* Pause/Resume only allowed with cyclic mode */
1027 if (!c->cyclic)
1028 return -EINVAL;
1029
1030 if (c->paused) {
Russell Kingfa3ad862013-11-02 17:07:09 +00001031 omap_dma_start(c, c->desc);
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001032 c->paused = false;
1033 }
1034
1035 return 0;
Russell King7bedaa52012-04-13 12:10:24 +01001036}
1037
1038static int omap_dma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1039 unsigned long arg)
1040{
1041 struct omap_chan *c = to_omap_dma_chan(chan);
1042 int ret;
1043
1044 switch (cmd) {
1045 case DMA_SLAVE_CONFIG:
1046 ret = omap_dma_slave_config(c, (struct dma_slave_config *)arg);
1047 break;
1048
1049 case DMA_TERMINATE_ALL:
1050 ret = omap_dma_terminate_all(c);
1051 break;
1052
1053 case DMA_PAUSE:
1054 ret = omap_dma_pause(c);
1055 break;
1056
1057 case DMA_RESUME:
1058 ret = omap_dma_resume(c);
1059 break;
1060
1061 default:
1062 ret = -ENXIO;
1063 break;
1064 }
1065
1066 return ret;
1067}
1068
1069static int omap_dma_chan_init(struct omap_dmadev *od, int dma_sig)
1070{
1071 struct omap_chan *c;
1072
1073 c = kzalloc(sizeof(*c), GFP_KERNEL);
1074 if (!c)
1075 return -ENOMEM;
1076
Russell King596c4712013-12-10 11:08:01 +00001077 c->reg_map = od->reg_map;
Russell King7bedaa52012-04-13 12:10:24 +01001078 c->dma_sig = dma_sig;
1079 c->vc.desc_free = omap_dma_desc_free;
1080 vchan_init(&c->vc, &od->ddev);
1081 INIT_LIST_HEAD(&c->node);
1082
1083 od->ddev.chancnt++;
1084
1085 return 0;
1086}
1087
1088static void omap_dma_free(struct omap_dmadev *od)
1089{
1090 tasklet_kill(&od->task);
1091 while (!list_empty(&od->ddev.channels)) {
1092 struct omap_chan *c = list_first_entry(&od->ddev.channels,
1093 struct omap_chan, vc.chan.device_node);
1094
1095 list_del(&c->vc.chan.device_node);
1096 tasklet_kill(&c->vc.task);
1097 kfree(c);
1098 }
Russell King7bedaa52012-04-13 12:10:24 +01001099}
1100
1101static int omap_dma_probe(struct platform_device *pdev)
1102{
1103 struct omap_dmadev *od;
Russell King596c4712013-12-10 11:08:01 +00001104 struct resource *res;
Russell King6ddeb6d2013-12-10 19:05:50 +00001105 int rc, i, irq;
Russell King7bedaa52012-04-13 12:10:24 +01001106
Russell King104fce72013-11-02 12:58:29 +00001107 od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
Russell King7bedaa52012-04-13 12:10:24 +01001108 if (!od)
1109 return -ENOMEM;
1110
Russell King596c4712013-12-10 11:08:01 +00001111 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1112 od->base = devm_ioremap_resource(&pdev->dev, res);
1113 if (IS_ERR(od->base))
1114 return PTR_ERR(od->base);
1115
Russell King1b416c42013-11-02 13:00:03 +00001116 od->plat = omap_get_plat_info();
1117 if (!od->plat)
1118 return -EPROBE_DEFER;
1119
Russell King596c4712013-12-10 11:08:01 +00001120 od->reg_map = od->plat->reg_map;
1121
Russell King7bedaa52012-04-13 12:10:24 +01001122 dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
Russell King3a774ea2012-06-21 10:40:15 +01001123 dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
Russell King7bedaa52012-04-13 12:10:24 +01001124 od->ddev.device_alloc_chan_resources = omap_dma_alloc_chan_resources;
1125 od->ddev.device_free_chan_resources = omap_dma_free_chan_resources;
1126 od->ddev.device_tx_status = omap_dma_tx_status;
1127 od->ddev.device_issue_pending = omap_dma_issue_pending;
1128 od->ddev.device_prep_slave_sg = omap_dma_prep_slave_sg;
Russell King3a774ea2012-06-21 10:40:15 +01001129 od->ddev.device_prep_dma_cyclic = omap_dma_prep_dma_cyclic;
Russell King7bedaa52012-04-13 12:10:24 +01001130 od->ddev.device_control = omap_dma_control;
1131 od->ddev.dev = &pdev->dev;
1132 INIT_LIST_HEAD(&od->ddev.channels);
1133 INIT_LIST_HEAD(&od->pending);
1134 spin_lock_init(&od->lock);
Russell King6ddeb6d2013-12-10 19:05:50 +00001135 spin_lock_init(&od->irq_lock);
Russell King7bedaa52012-04-13 12:10:24 +01001136
1137 tasklet_init(&od->task, omap_dma_sched, (unsigned long)od);
1138
1139 for (i = 0; i < 127; i++) {
1140 rc = omap_dma_chan_init(od, i);
1141 if (rc) {
1142 omap_dma_free(od);
1143 return rc;
1144 }
1145 }
1146
Russell King6ddeb6d2013-12-10 19:05:50 +00001147 irq = platform_get_irq(pdev, 1);
1148 if (irq <= 0) {
1149 dev_info(&pdev->dev, "failed to get L1 IRQ: %d\n", irq);
1150 od->legacy = true;
1151 } else {
1152 /* Disable all interrupts */
1153 od->irq_enable_mask = 0;
1154 omap_dma_glbl_write(od, IRQENABLE_L1, 0);
1155
1156 rc = devm_request_irq(&pdev->dev, irq, omap_dma_irq,
1157 IRQF_SHARED, "omap-dma-engine", od);
1158 if (rc)
1159 return rc;
1160 }
1161
Russell King7bedaa52012-04-13 12:10:24 +01001162 rc = dma_async_device_register(&od->ddev);
1163 if (rc) {
1164 pr_warn("OMAP-DMA: failed to register slave DMA engine device: %d\n",
1165 rc);
1166 omap_dma_free(od);
Jon Hunter8d306622013-02-26 12:27:24 -06001167 return rc;
1168 }
1169
1170 platform_set_drvdata(pdev, od);
1171
1172 if (pdev->dev.of_node) {
1173 omap_dma_info.dma_cap = od->ddev.cap_mask;
1174
1175 /* Device-tree DMA controller registration */
1176 rc = of_dma_controller_register(pdev->dev.of_node,
1177 of_dma_simple_xlate, &omap_dma_info);
1178 if (rc) {
1179 pr_warn("OMAP-DMA: failed to register DMA controller\n");
1180 dma_async_device_unregister(&od->ddev);
1181 omap_dma_free(od);
1182 }
Russell King7bedaa52012-04-13 12:10:24 +01001183 }
1184
1185 dev_info(&pdev->dev, "OMAP DMA engine driver\n");
1186
1187 return rc;
1188}
1189
1190static int omap_dma_remove(struct platform_device *pdev)
1191{
1192 struct omap_dmadev *od = platform_get_drvdata(pdev);
1193
Jon Hunter8d306622013-02-26 12:27:24 -06001194 if (pdev->dev.of_node)
1195 of_dma_controller_free(pdev->dev.of_node);
1196
Russell King7bedaa52012-04-13 12:10:24 +01001197 dma_async_device_unregister(&od->ddev);
Russell King6ddeb6d2013-12-10 19:05:50 +00001198
1199 if (!od->legacy) {
1200 /* Disable all interrupts */
1201 omap_dma_glbl_write(od, IRQENABLE_L0, 0);
1202 }
1203
Russell King7bedaa52012-04-13 12:10:24 +01001204 omap_dma_free(od);
1205
1206 return 0;
1207}
1208
Jon Hunter8d306622013-02-26 12:27:24 -06001209static const struct of_device_id omap_dma_match[] = {
1210 { .compatible = "ti,omap2420-sdma", },
1211 { .compatible = "ti,omap2430-sdma", },
1212 { .compatible = "ti,omap3430-sdma", },
1213 { .compatible = "ti,omap3630-sdma", },
1214 { .compatible = "ti,omap4430-sdma", },
1215 {},
1216};
1217MODULE_DEVICE_TABLE(of, omap_dma_match);
1218
Russell King7bedaa52012-04-13 12:10:24 +01001219static struct platform_driver omap_dma_driver = {
1220 .probe = omap_dma_probe,
1221 .remove = omap_dma_remove,
1222 .driver = {
1223 .name = "omap-dma-engine",
1224 .owner = THIS_MODULE,
Jon Hunter8d306622013-02-26 12:27:24 -06001225 .of_match_table = of_match_ptr(omap_dma_match),
Russell King7bedaa52012-04-13 12:10:24 +01001226 },
1227};
1228
1229bool omap_dma_filter_fn(struct dma_chan *chan, void *param)
1230{
1231 if (chan->device->dev->driver == &omap_dma_driver.driver) {
1232 struct omap_chan *c = to_omap_dma_chan(chan);
1233 unsigned req = *(unsigned *)param;
1234
1235 return req == c->dma_sig;
1236 }
1237 return false;
1238}
1239EXPORT_SYMBOL_GPL(omap_dma_filter_fn);
1240
Russell King7bedaa52012-04-13 12:10:24 +01001241static int omap_dma_init(void)
1242{
Tony Lindgrenbe1f9482013-01-11 11:24:19 -08001243 return platform_driver_register(&omap_dma_driver);
Russell King7bedaa52012-04-13 12:10:24 +01001244}
1245subsys_initcall(omap_dma_init);
1246
1247static void __exit omap_dma_exit(void)
1248{
Russell King7bedaa52012-04-13 12:10:24 +01001249 platform_driver_unregister(&omap_dma_driver);
1250}
1251module_exit(omap_dma_exit);
1252
1253MODULE_AUTHOR("Russell King");
1254MODULE_LICENSE("GPL");