blob: 23da592594fa24dee9c147feb31c5733ba3e375b [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Russell King7bedaa52012-04-13 12:10:24 +01002/*
3 * OMAP DMAengine support
Russell King7bedaa52012-04-13 12:10:24 +01004 */
Russell Kingfa3ad862013-11-02 17:07:09 +00005#include <linux/delay.h>
Russell King7bedaa52012-04-13 12:10:24 +01006#include <linux/dmaengine.h>
7#include <linux/dma-mapping.h>
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +03008#include <linux/dmapool.h>
Russell King7bedaa52012-04-13 12:10:24 +01009#include <linux/err.h>
10#include <linux/init.h>
11#include <linux/interrupt.h>
12#include <linux/list.h>
13#include <linux/module.h>
14#include <linux/omap-dma.h>
15#include <linux/platform_device.h>
16#include <linux/slab.h>
17#include <linux/spinlock.h>
Jon Hunter8d306622013-02-26 12:27:24 -060018#include <linux/of_dma.h>
19#include <linux/of_device.h>
Russell King7bedaa52012-04-13 12:10:24 +010020
Peter Ujfalusid88b1392018-04-25 11:45:03 +030021#include "../virt-dma.h"
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070022
Peter Ujfalusi341ce712015-04-09 12:35:50 +030023#define OMAP_SDMA_REQUESTS 127
24#define OMAP_SDMA_CHANNELS 32
25
Russell King7bedaa52012-04-13 12:10:24 +010026struct omap_dmadev {
27 struct dma_device ddev;
28 spinlock_t lock;
Russell King596c4712013-12-10 11:08:01 +000029 void __iomem *base;
30 const struct omap_dma_reg *reg_map;
Russell King1b416c42013-11-02 13:00:03 +000031 struct omap_system_dma_plat_info *plat;
Russell King6ddeb6d2013-12-10 19:05:50 +000032 bool legacy;
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +030033 bool ll123_supported;
34 struct dma_pool *desc_pool;
Peter Ujfaluside506082015-04-09 12:35:51 +030035 unsigned dma_requests;
Russell King6ddeb6d2013-12-10 19:05:50 +000036 spinlock_t irq_lock;
37 uint32_t irq_enable_mask;
Peter Ujfalusi2d1a9a92016-07-20 11:50:29 +030038 struct omap_chan **lch_map;
Russell King7bedaa52012-04-13 12:10:24 +010039};
40
41struct omap_chan {
42 struct virt_dma_chan vc;
Russell King596c4712013-12-10 11:08:01 +000043 void __iomem *channel_base;
44 const struct omap_dma_reg *reg_map;
Russell Kingaa4c5b92014-01-14 23:58:10 +000045 uint32_t ccr;
Russell King7bedaa52012-04-13 12:10:24 +010046
47 struct dma_slave_config cfg;
48 unsigned dma_sig;
Russell King3a774ea2012-06-21 10:40:15 +010049 bool cyclic;
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +030050 bool paused;
Peter Ujfalusi689d3c52016-04-05 15:20:20 +030051 bool running;
Russell King7bedaa52012-04-13 12:10:24 +010052
53 int dma_ch;
54 struct omap_desc *desc;
55 unsigned sgidx;
56};
57
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +030058#define DESC_NXT_SV_REFRESH (0x1 << 24)
59#define DESC_NXT_SV_REUSE (0x2 << 24)
60#define DESC_NXT_DV_REFRESH (0x1 << 26)
61#define DESC_NXT_DV_REUSE (0x2 << 26)
62#define DESC_NTYPE_TYPE2 (0x2 << 29)
63
64/* Type 2 descriptor with Source or Destination address update */
65struct omap_type2_desc {
66 uint32_t next_desc;
67 uint32_t en;
68 uint32_t addr; /* src or dst */
69 uint16_t fn;
70 uint16_t cicr;
Peter Ujfalusid4c77c02016-09-13 10:58:43 +030071 int16_t cdei;
72 int16_t csei;
73 int32_t cdfi;
74 int32_t csfi;
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +030075} __packed;
76
Russell King7bedaa52012-04-13 12:10:24 +010077struct omap_sg {
78 dma_addr_t addr;
79 uint32_t en; /* number of elements (24-bit) */
80 uint32_t fn; /* number of frames (16-bit) */
Peter Ujfalusiad524652016-07-12 14:21:14 +030081 int32_t fi; /* for double indexing */
82 int16_t ei; /* for double indexing */
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +030083
84 /* Linked list */
85 struct omap_type2_desc *t2_desc;
86 dma_addr_t t2_desc_paddr;
Russell King7bedaa52012-04-13 12:10:24 +010087};
88
89struct omap_desc {
90 struct virt_dma_desc vd;
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +030091 bool using_ll;
Russell King7bedaa52012-04-13 12:10:24 +010092 enum dma_transfer_direction dir;
93 dma_addr_t dev_addr;
Peter Ujfalusi4689d352019-07-16 11:24:59 +030094 bool polled;
Russell King7bedaa52012-04-13 12:10:24 +010095
Peter Ujfalusiad524652016-07-12 14:21:14 +030096 int32_t fi; /* for OMAP_DMA_SYNC_PACKET / double indexing */
97 int16_t ei; /* for double indexing */
Russell King90438262013-11-02 19:57:06 +000098 uint8_t es; /* CSDP_DATA_TYPE_xxx */
Russell King3ed4d182013-11-02 19:16:09 +000099 uint32_t ccr; /* CCR value */
Russell King965aeb4d2013-11-06 17:12:30 +0000100 uint16_t clnk_ctrl; /* CLNK_CTRL value */
Russell Kingfa3ad862013-11-02 17:07:09 +0000101 uint16_t cicr; /* CICR value */
Russell King2f0d13b2013-11-02 18:51:53 +0000102 uint32_t csdp; /* CSDP value */
Russell King7bedaa52012-04-13 12:10:24 +0100103
104 unsigned sglen;
105 struct omap_sg sg[0];
106};
107
Russell King90438262013-11-02 19:57:06 +0000108enum {
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +0300109 CAPS_0_SUPPORT_LL123 = BIT(20), /* Linked List type1/2/3 */
110 CAPS_0_SUPPORT_LL4 = BIT(21), /* Linked List type4 */
111
Russell King90438262013-11-02 19:57:06 +0000112 CCR_FS = BIT(5),
113 CCR_READ_PRIORITY = BIT(6),
114 CCR_ENABLE = BIT(7),
115 CCR_AUTO_INIT = BIT(8), /* OMAP1 only */
116 CCR_REPEAT = BIT(9), /* OMAP1 only */
117 CCR_OMAP31_DISABLE = BIT(10), /* OMAP1 only */
118 CCR_SUSPEND_SENSITIVE = BIT(8), /* OMAP2+ only */
119 CCR_RD_ACTIVE = BIT(9), /* OMAP2+ only */
120 CCR_WR_ACTIVE = BIT(10), /* OMAP2+ only */
121 CCR_SRC_AMODE_CONSTANT = 0 << 12,
122 CCR_SRC_AMODE_POSTINC = 1 << 12,
123 CCR_SRC_AMODE_SGLIDX = 2 << 12,
124 CCR_SRC_AMODE_DBLIDX = 3 << 12,
125 CCR_DST_AMODE_CONSTANT = 0 << 14,
126 CCR_DST_AMODE_POSTINC = 1 << 14,
127 CCR_DST_AMODE_SGLIDX = 2 << 14,
128 CCR_DST_AMODE_DBLIDX = 3 << 14,
129 CCR_CONSTANT_FILL = BIT(16),
130 CCR_TRANSPARENT_COPY = BIT(17),
131 CCR_BS = BIT(18),
132 CCR_SUPERVISOR = BIT(22),
133 CCR_PREFETCH = BIT(23),
134 CCR_TRIGGER_SRC = BIT(24),
135 CCR_BUFFERING_DISABLE = BIT(25),
136 CCR_WRITE_PRIORITY = BIT(26),
137 CCR_SYNC_ELEMENT = 0,
138 CCR_SYNC_FRAME = CCR_FS,
139 CCR_SYNC_BLOCK = CCR_BS,
140 CCR_SYNC_PACKET = CCR_BS | CCR_FS,
141
142 CSDP_DATA_TYPE_8 = 0,
143 CSDP_DATA_TYPE_16 = 1,
144 CSDP_DATA_TYPE_32 = 2,
145 CSDP_SRC_PORT_EMIFF = 0 << 2, /* OMAP1 only */
146 CSDP_SRC_PORT_EMIFS = 1 << 2, /* OMAP1 only */
147 CSDP_SRC_PORT_OCP_T1 = 2 << 2, /* OMAP1 only */
148 CSDP_SRC_PORT_TIPB = 3 << 2, /* OMAP1 only */
149 CSDP_SRC_PORT_OCP_T2 = 4 << 2, /* OMAP1 only */
150 CSDP_SRC_PORT_MPUI = 5 << 2, /* OMAP1 only */
151 CSDP_SRC_PACKED = BIT(6),
152 CSDP_SRC_BURST_1 = 0 << 7,
153 CSDP_SRC_BURST_16 = 1 << 7,
154 CSDP_SRC_BURST_32 = 2 << 7,
155 CSDP_SRC_BURST_64 = 3 << 7,
156 CSDP_DST_PORT_EMIFF = 0 << 9, /* OMAP1 only */
157 CSDP_DST_PORT_EMIFS = 1 << 9, /* OMAP1 only */
158 CSDP_DST_PORT_OCP_T1 = 2 << 9, /* OMAP1 only */
159 CSDP_DST_PORT_TIPB = 3 << 9, /* OMAP1 only */
160 CSDP_DST_PORT_OCP_T2 = 4 << 9, /* OMAP1 only */
161 CSDP_DST_PORT_MPUI = 5 << 9, /* OMAP1 only */
162 CSDP_DST_PACKED = BIT(13),
163 CSDP_DST_BURST_1 = 0 << 14,
164 CSDP_DST_BURST_16 = 1 << 14,
165 CSDP_DST_BURST_32 = 2 << 14,
166 CSDP_DST_BURST_64 = 3 << 14,
Peter Ujfalusi201ac482016-11-29 16:23:42 +0200167 CSDP_WRITE_NON_POSTED = 0 << 16,
168 CSDP_WRITE_POSTED = 1 << 16,
169 CSDP_WRITE_LAST_NON_POSTED = 2 << 16,
Russell King90438262013-11-02 19:57:06 +0000170
171 CICR_TOUT_IE = BIT(0), /* OMAP1 only */
172 CICR_DROP_IE = BIT(1),
173 CICR_HALF_IE = BIT(2),
174 CICR_FRAME_IE = BIT(3),
175 CICR_LAST_IE = BIT(4),
176 CICR_BLOCK_IE = BIT(5),
177 CICR_PKT_IE = BIT(7), /* OMAP2+ only */
178 CICR_TRANS_ERR_IE = BIT(8), /* OMAP2+ only */
179 CICR_SUPERVISOR_ERR_IE = BIT(10), /* OMAP2+ only */
180 CICR_MISALIGNED_ERR_IE = BIT(11), /* OMAP2+ only */
181 CICR_DRAIN_IE = BIT(12), /* OMAP2+ only */
182 CICR_SUPER_BLOCK_IE = BIT(14), /* OMAP2+ only */
183
184 CLNK_CTRL_ENABLE_LNK = BIT(15),
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +0300185
186 CDP_DST_VALID_INC = 0 << 0,
187 CDP_DST_VALID_RELOAD = 1 << 0,
188 CDP_DST_VALID_REUSE = 2 << 0,
189 CDP_SRC_VALID_INC = 0 << 2,
190 CDP_SRC_VALID_RELOAD = 1 << 2,
191 CDP_SRC_VALID_REUSE = 2 << 2,
192 CDP_NTYPE_TYPE1 = 1 << 4,
193 CDP_NTYPE_TYPE2 = 2 << 4,
194 CDP_NTYPE_TYPE3 = 3 << 4,
195 CDP_TMODE_NORMAL = 0 << 8,
196 CDP_TMODE_LLIST = 1 << 8,
197 CDP_FAST = BIT(10),
Russell King90438262013-11-02 19:57:06 +0000198};
199
Russell King7bedaa52012-04-13 12:10:24 +0100200static const unsigned es_bytes[] = {
Russell King90438262013-11-02 19:57:06 +0000201 [CSDP_DATA_TYPE_8] = 1,
202 [CSDP_DATA_TYPE_16] = 2,
203 [CSDP_DATA_TYPE_32] = 4,
Russell King7bedaa52012-04-13 12:10:24 +0100204};
205
Arnd Bergmann9c71b9e2019-07-22 10:16:44 +0200206static bool omap_dma_filter_fn(struct dma_chan *chan, void *param);
Jon Hunter8d306622013-02-26 12:27:24 -0600207static struct of_dma_filter_info omap_dma_info = {
208 .filter_fn = omap_dma_filter_fn,
209};
210
Russell King7bedaa52012-04-13 12:10:24 +0100211static inline struct omap_dmadev *to_omap_dma_dev(struct dma_device *d)
212{
213 return container_of(d, struct omap_dmadev, ddev);
214}
215
216static inline struct omap_chan *to_omap_dma_chan(struct dma_chan *c)
217{
218 return container_of(c, struct omap_chan, vc.chan);
219}
220
221static inline struct omap_desc *to_omap_dma_desc(struct dma_async_tx_descriptor *t)
222{
223 return container_of(t, struct omap_desc, vd.tx);
224}
225
226static void omap_dma_desc_free(struct virt_dma_desc *vd)
227{
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +0300228 struct omap_desc *d = to_omap_dma_desc(&vd->tx);
229
230 if (d->using_ll) {
231 struct omap_dmadev *od = to_omap_dma_dev(vd->tx.chan->device);
232 int i;
233
234 for (i = 0; i < d->sglen; i++) {
235 if (d->sg[i].t2_desc)
236 dma_pool_free(od->desc_pool, d->sg[i].t2_desc,
237 d->sg[i].t2_desc_paddr);
238 }
239 }
240
241 kfree(d);
242}
243
244static void omap_dma_fill_type2_desc(struct omap_desc *d, int idx,
245 enum dma_transfer_direction dir, bool last)
246{
247 struct omap_sg *sg = &d->sg[idx];
248 struct omap_type2_desc *t2_desc = sg->t2_desc;
249
250 if (idx)
251 d->sg[idx - 1].t2_desc->next_desc = sg->t2_desc_paddr;
252 if (last)
253 t2_desc->next_desc = 0xfffffffc;
254
255 t2_desc->en = sg->en;
256 t2_desc->addr = sg->addr;
257 t2_desc->fn = sg->fn & 0xffff;
258 t2_desc->cicr = d->cicr;
259 if (!last)
260 t2_desc->cicr &= ~CICR_BLOCK_IE;
261
262 switch (dir) {
263 case DMA_DEV_TO_MEM:
264 t2_desc->cdei = sg->ei;
265 t2_desc->csei = d->ei;
266 t2_desc->cdfi = sg->fi;
267 t2_desc->csfi = d->fi;
268
269 t2_desc->en |= DESC_NXT_DV_REFRESH;
270 t2_desc->en |= DESC_NXT_SV_REUSE;
271 break;
272 case DMA_MEM_TO_DEV:
273 t2_desc->cdei = d->ei;
274 t2_desc->csei = sg->ei;
275 t2_desc->cdfi = d->fi;
276 t2_desc->csfi = sg->fi;
277
278 t2_desc->en |= DESC_NXT_SV_REFRESH;
279 t2_desc->en |= DESC_NXT_DV_REUSE;
280 break;
281 default:
282 return;
283 }
284
285 t2_desc->en |= DESC_NTYPE_TYPE2;
Russell King7bedaa52012-04-13 12:10:24 +0100286}
287
Russell King596c4712013-12-10 11:08:01 +0000288static void omap_dma_write(uint32_t val, unsigned type, void __iomem *addr)
289{
290 switch (type) {
291 case OMAP_DMA_REG_16BIT:
292 writew_relaxed(val, addr);
293 break;
294 case OMAP_DMA_REG_2X16BIT:
295 writew_relaxed(val, addr);
296 writew_relaxed(val >> 16, addr + 2);
297 break;
298 case OMAP_DMA_REG_32BIT:
299 writel_relaxed(val, addr);
300 break;
301 default:
302 WARN_ON(1);
303 }
304}
305
306static unsigned omap_dma_read(unsigned type, void __iomem *addr)
307{
308 unsigned val;
309
310 switch (type) {
311 case OMAP_DMA_REG_16BIT:
312 val = readw_relaxed(addr);
313 break;
314 case OMAP_DMA_REG_2X16BIT:
315 val = readw_relaxed(addr);
316 val |= readw_relaxed(addr + 2) << 16;
317 break;
318 case OMAP_DMA_REG_32BIT:
319 val = readl_relaxed(addr);
320 break;
321 default:
322 WARN_ON(1);
323 val = 0;
324 }
325
326 return val;
327}
328
Russell Kingc5ed98b2013-11-06 17:33:09 +0000329static void omap_dma_glbl_write(struct omap_dmadev *od, unsigned reg, unsigned val)
330{
Russell King596c4712013-12-10 11:08:01 +0000331 const struct omap_dma_reg *r = od->reg_map + reg;
332
333 WARN_ON(r->stride);
334
335 omap_dma_write(val, r->type, od->base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000336}
337
338static unsigned omap_dma_glbl_read(struct omap_dmadev *od, unsigned reg)
339{
Russell King596c4712013-12-10 11:08:01 +0000340 const struct omap_dma_reg *r = od->reg_map + reg;
341
342 WARN_ON(r->stride);
343
344 return omap_dma_read(r->type, od->base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000345}
346
347static void omap_dma_chan_write(struct omap_chan *c, unsigned reg, unsigned val)
348{
Russell King596c4712013-12-10 11:08:01 +0000349 const struct omap_dma_reg *r = c->reg_map + reg;
350
351 omap_dma_write(val, r->type, c->channel_base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000352}
353
354static unsigned omap_dma_chan_read(struct omap_chan *c, unsigned reg)
355{
Russell King596c4712013-12-10 11:08:01 +0000356 const struct omap_dma_reg *r = c->reg_map + reg;
357
358 return omap_dma_read(r->type, c->channel_base + r->offset);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000359}
360
Russell King470b23f2013-11-02 21:23:06 +0000361static void omap_dma_clear_csr(struct omap_chan *c)
362{
363 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000364 omap_dma_chan_read(c, CSR);
Russell King470b23f2013-11-02 21:23:06 +0000365 else
Russell Kingc5ed98b2013-11-06 17:33:09 +0000366 omap_dma_chan_write(c, CSR, ~0);
Russell King470b23f2013-11-02 21:23:06 +0000367}
368
Russell King6ddeb6d2013-12-10 19:05:50 +0000369static unsigned omap_dma_get_csr(struct omap_chan *c)
370{
371 unsigned val = omap_dma_chan_read(c, CSR);
372
373 if (!dma_omap1())
374 omap_dma_chan_write(c, CSR, val);
375
376 return val;
377}
378
Russell King596c4712013-12-10 11:08:01 +0000379static void omap_dma_assign(struct omap_dmadev *od, struct omap_chan *c,
380 unsigned lch)
381{
382 c->channel_base = od->base + od->plat->channel_stride * lch;
Russell King6ddeb6d2013-12-10 19:05:50 +0000383
384 od->lch_map[lch] = c;
Russell King596c4712013-12-10 11:08:01 +0000385}
386
Russell Kingfa3ad862013-11-02 17:07:09 +0000387static void omap_dma_start(struct omap_chan *c, struct omap_desc *d)
388{
389 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +0300390 uint16_t cicr = d->cicr;
Russell Kingfa3ad862013-11-02 17:07:09 +0000391
392 if (__dma_omap15xx(od->plat->dma_attr))
Russell Kingc5ed98b2013-11-06 17:33:09 +0000393 omap_dma_chan_write(c, CPC, 0);
Russell Kingfa3ad862013-11-02 17:07:09 +0000394 else
Russell Kingc5ed98b2013-11-06 17:33:09 +0000395 omap_dma_chan_write(c, CDAC, 0);
Russell Kingfa3ad862013-11-02 17:07:09 +0000396
Russell King470b23f2013-11-02 21:23:06 +0000397 omap_dma_clear_csr(c);
Russell Kingfa3ad862013-11-02 17:07:09 +0000398
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +0300399 if (d->using_ll) {
400 uint32_t cdp = CDP_TMODE_LLIST | CDP_NTYPE_TYPE2 | CDP_FAST;
401
402 if (d->dir == DMA_DEV_TO_MEM)
403 cdp |= (CDP_DST_VALID_RELOAD | CDP_SRC_VALID_REUSE);
404 else
405 cdp |= (CDP_DST_VALID_REUSE | CDP_SRC_VALID_RELOAD);
406 omap_dma_chan_write(c, CDP, cdp);
407
408 omap_dma_chan_write(c, CNDP, d->sg[0].t2_desc_paddr);
409 omap_dma_chan_write(c, CCDN, 0);
410 omap_dma_chan_write(c, CCFN, 0xffff);
411 omap_dma_chan_write(c, CCEN, 0xffffff);
412
413 cicr &= ~CICR_BLOCK_IE;
414 } else if (od->ll123_supported) {
415 omap_dma_chan_write(c, CDP, 0);
416 }
417
Russell Kingfa3ad862013-11-02 17:07:09 +0000418 /* Enable interrupts */
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +0300419 omap_dma_chan_write(c, CICR, cicr);
Russell Kingfa3ad862013-11-02 17:07:09 +0000420
Russell King45da7b02013-11-06 17:18:42 +0000421 /* Enable channel */
Russell Kingc5ed98b2013-11-06 17:33:09 +0000422 omap_dma_chan_write(c, CCR, d->ccr | CCR_ENABLE);
Peter Ujfalusi689d3c52016-04-05 15:20:20 +0300423
424 c->running = true;
Russell Kingfa3ad862013-11-02 17:07:09 +0000425}
426
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +0530427static void omap_dma_drain_chan(struct omap_chan *c)
428{
429 int i;
430 u32 val;
431
432 /* Wait for sDMA FIFO to drain */
433 for (i = 0; ; i++) {
434 val = omap_dma_chan_read(c, CCR);
435 if (!(val & (CCR_RD_ACTIVE | CCR_WR_ACTIVE)))
436 break;
437
438 if (i > 100)
439 break;
440
441 udelay(5);
442 }
443
444 if (val & (CCR_RD_ACTIVE | CCR_WR_ACTIVE))
445 dev_err(c->vc.chan.device->dev,
446 "DMA drain did not complete on lch %d\n",
447 c->dma_ch);
448}
449
450static int omap_dma_stop(struct omap_chan *c)
Russell Kingfa3ad862013-11-02 17:07:09 +0000451{
452 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
453 uint32_t val;
454
455 /* disable irq */
Russell Kingc5ed98b2013-11-06 17:33:09 +0000456 omap_dma_chan_write(c, CICR, 0);
Russell Kingfa3ad862013-11-02 17:07:09 +0000457
Russell King470b23f2013-11-02 21:23:06 +0000458 omap_dma_clear_csr(c);
Russell Kingfa3ad862013-11-02 17:07:09 +0000459
Russell Kingc5ed98b2013-11-06 17:33:09 +0000460 val = omap_dma_chan_read(c, CCR);
Russell King90438262013-11-02 19:57:06 +0000461 if (od->plat->errata & DMA_ERRATA_i541 && val & CCR_TRIGGER_SRC) {
Russell Kingfa3ad862013-11-02 17:07:09 +0000462 uint32_t sysconfig;
Russell Kingfa3ad862013-11-02 17:07:09 +0000463
Russell Kingc5ed98b2013-11-06 17:33:09 +0000464 sysconfig = omap_dma_glbl_read(od, OCP_SYSCONFIG);
Russell Kingfa3ad862013-11-02 17:07:09 +0000465 val = sysconfig & ~DMA_SYSCONFIG_MIDLEMODE_MASK;
466 val |= DMA_SYSCONFIG_MIDLEMODE(DMA_IDLEMODE_NO_IDLE);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000467 omap_dma_glbl_write(od, OCP_SYSCONFIG, val);
Russell Kingfa3ad862013-11-02 17:07:09 +0000468
Russell Kingc5ed98b2013-11-06 17:33:09 +0000469 val = omap_dma_chan_read(c, CCR);
Russell King90438262013-11-02 19:57:06 +0000470 val &= ~CCR_ENABLE;
Russell Kingc5ed98b2013-11-06 17:33:09 +0000471 omap_dma_chan_write(c, CCR, val);
Russell Kingfa3ad862013-11-02 17:07:09 +0000472
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +0530473 if (!(c->ccr & CCR_BUFFERING_DISABLE))
474 omap_dma_drain_chan(c);
Russell Kingfa3ad862013-11-02 17:07:09 +0000475
Russell Kingc5ed98b2013-11-06 17:33:09 +0000476 omap_dma_glbl_write(od, OCP_SYSCONFIG, sysconfig);
Russell Kingfa3ad862013-11-02 17:07:09 +0000477 } else {
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +0530478 if (!(val & CCR_ENABLE))
479 return -EINVAL;
480
Russell King90438262013-11-02 19:57:06 +0000481 val &= ~CCR_ENABLE;
Russell Kingc5ed98b2013-11-06 17:33:09 +0000482 omap_dma_chan_write(c, CCR, val);
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +0530483
484 if (!(c->ccr & CCR_BUFFERING_DISABLE))
485 omap_dma_drain_chan(c);
Russell Kingfa3ad862013-11-02 17:07:09 +0000486 }
487
488 mb();
489
490 if (!__dma_omap15xx(od->plat->dma_attr) && c->cyclic) {
Russell Kingc5ed98b2013-11-06 17:33:09 +0000491 val = omap_dma_chan_read(c, CLNK_CTRL);
Russell Kingfa3ad862013-11-02 17:07:09 +0000492
493 if (dma_omap1())
494 val |= 1 << 14; /* set the STOP_LNK bit */
495 else
Russell King90438262013-11-02 19:57:06 +0000496 val &= ~CLNK_CTRL_ENABLE_LNK;
Russell Kingfa3ad862013-11-02 17:07:09 +0000497
Russell Kingc5ed98b2013-11-06 17:33:09 +0000498 omap_dma_chan_write(c, CLNK_CTRL, val);
Russell Kingfa3ad862013-11-02 17:07:09 +0000499 }
Peter Ujfalusi689d3c52016-04-05 15:20:20 +0300500 c->running = false;
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +0530501 return 0;
Russell Kingfa3ad862013-11-02 17:07:09 +0000502}
503
Peter Ujfalusia5dc3fc2016-07-20 11:50:27 +0300504static void omap_dma_start_sg(struct omap_chan *c, struct omap_desc *d)
Russell King7bedaa52012-04-13 12:10:24 +0100505{
Peter Ujfalusia5dc3fc2016-07-20 11:50:27 +0300506 struct omap_sg *sg = d->sg + c->sgidx;
Russell King893e63e2013-11-03 11:17:11 +0000507 unsigned cxsa, cxei, cxfi;
Russell King7bedaa52012-04-13 12:10:24 +0100508
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +0300509 if (d->dir == DMA_DEV_TO_MEM || d->dir == DMA_MEM_TO_MEM) {
Russell King893e63e2013-11-03 11:17:11 +0000510 cxsa = CDSA;
511 cxei = CDEI;
512 cxfi = CDFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000513 } else {
Russell King893e63e2013-11-03 11:17:11 +0000514 cxsa = CSSA;
515 cxei = CSEI;
516 cxfi = CSFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000517 }
518
Russell Kingc5ed98b2013-11-06 17:33:09 +0000519 omap_dma_chan_write(c, cxsa, sg->addr);
Peter Ujfalusiad524652016-07-12 14:21:14 +0300520 omap_dma_chan_write(c, cxei, sg->ei);
521 omap_dma_chan_write(c, cxfi, sg->fi);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000522 omap_dma_chan_write(c, CEN, sg->en);
523 omap_dma_chan_write(c, CFN, sg->fn);
Russell King7bedaa52012-04-13 12:10:24 +0100524
Russell Kingfa3ad862013-11-02 17:07:09 +0000525 omap_dma_start(c, d);
Peter Ujfalusia5dc3fc2016-07-20 11:50:27 +0300526 c->sgidx++;
Russell King7bedaa52012-04-13 12:10:24 +0100527}
528
529static void omap_dma_start_desc(struct omap_chan *c)
530{
531 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
532 struct omap_desc *d;
Russell King893e63e2013-11-03 11:17:11 +0000533 unsigned cxsa, cxei, cxfi;
Russell King7bedaa52012-04-13 12:10:24 +0100534
535 if (!vd) {
536 c->desc = NULL;
537 return;
538 }
539
540 list_del(&vd->node);
541
542 c->desc = d = to_omap_dma_desc(&vd->tx);
543 c->sgidx = 0;
544
Russell King59871902013-11-06 17:15:16 +0000545 /*
546 * This provides the necessary barrier to ensure data held in
547 * DMA coherent memory is visible to the DMA engine prior to
548 * the transfer starting.
549 */
550 mb();
551
Russell Kingc5ed98b2013-11-06 17:33:09 +0000552 omap_dma_chan_write(c, CCR, d->ccr);
Russell King3ed4d182013-11-02 19:16:09 +0000553 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000554 omap_dma_chan_write(c, CCR2, d->ccr >> 16);
Russell Kingb9e97822013-11-02 13:26:57 +0000555
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +0300556 if (d->dir == DMA_DEV_TO_MEM || d->dir == DMA_MEM_TO_MEM) {
Russell King893e63e2013-11-03 11:17:11 +0000557 cxsa = CSSA;
558 cxei = CSEI;
559 cxfi = CSFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000560 } else {
Russell King893e63e2013-11-03 11:17:11 +0000561 cxsa = CDSA;
562 cxei = CDEI;
563 cxfi = CDFI;
Russell Kingb9e97822013-11-02 13:26:57 +0000564 }
Russell King7bedaa52012-04-13 12:10:24 +0100565
Russell Kingc5ed98b2013-11-06 17:33:09 +0000566 omap_dma_chan_write(c, cxsa, d->dev_addr);
Peter Ujfalusiad524652016-07-12 14:21:14 +0300567 omap_dma_chan_write(c, cxei, d->ei);
Russell Kingc5ed98b2013-11-06 17:33:09 +0000568 omap_dma_chan_write(c, cxfi, d->fi);
569 omap_dma_chan_write(c, CSDP, d->csdp);
570 omap_dma_chan_write(c, CLNK_CTRL, d->clnk_ctrl);
Russell King913a2d02013-11-02 14:41:42 +0000571
Peter Ujfalusia5dc3fc2016-07-20 11:50:27 +0300572 omap_dma_start_sg(c, d);
Russell King7bedaa52012-04-13 12:10:24 +0100573}
574
575static void omap_dma_callback(int ch, u16 status, void *data)
576{
577 struct omap_chan *c = data;
578 struct omap_desc *d;
579 unsigned long flags;
580
581 spin_lock_irqsave(&c->vc.lock, flags);
582 d = c->desc;
583 if (d) {
Peter Ujfalusib57ebe02016-07-20 11:50:28 +0300584 if (c->cyclic) {
Russell King3a774ea2012-06-21 10:40:15 +0100585 vchan_cyclic_callback(&d->vd);
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +0300586 } else if (d->using_ll || c->sgidx == d->sglen) {
Peter Ujfalusib57ebe02016-07-20 11:50:28 +0300587 omap_dma_start_desc(c);
588 vchan_cookie_complete(&d->vd);
589 } else {
590 omap_dma_start_sg(c, d);
Russell King7bedaa52012-04-13 12:10:24 +0100591 }
592 }
593 spin_unlock_irqrestore(&c->vc.lock, flags);
594}
595
Russell King6ddeb6d2013-12-10 19:05:50 +0000596static irqreturn_t omap_dma_irq(int irq, void *devid)
597{
598 struct omap_dmadev *od = devid;
599 unsigned status, channel;
600
601 spin_lock(&od->irq_lock);
602
603 status = omap_dma_glbl_read(od, IRQSTATUS_L1);
604 status &= od->irq_enable_mask;
605 if (status == 0) {
606 spin_unlock(&od->irq_lock);
607 return IRQ_NONE;
608 }
609
610 while ((channel = ffs(status)) != 0) {
611 unsigned mask, csr;
612 struct omap_chan *c;
613
614 channel -= 1;
615 mask = BIT(channel);
616 status &= ~mask;
617
618 c = od->lch_map[channel];
619 if (c == NULL) {
620 /* This should never happen */
621 dev_err(od->ddev.dev, "invalid channel %u\n", channel);
622 continue;
623 }
624
625 csr = omap_dma_get_csr(c);
626 omap_dma_glbl_write(od, IRQSTATUS_L1, mask);
627
628 omap_dma_callback(channel, csr, c);
629 }
630
631 spin_unlock(&od->irq_lock);
632
633 return IRQ_HANDLED;
634}
635
Russell King7bedaa52012-04-13 12:10:24 +0100636static int omap_dma_alloc_chan_resources(struct dma_chan *chan)
637{
Russell King596c4712013-12-10 11:08:01 +0000638 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King7bedaa52012-04-13 12:10:24 +0100639 struct omap_chan *c = to_omap_dma_chan(chan);
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +0300640 struct device *dev = od->ddev.dev;
Russell King596c4712013-12-10 11:08:01 +0000641 int ret;
Russell King7bedaa52012-04-13 12:10:24 +0100642
Russell King6ddeb6d2013-12-10 19:05:50 +0000643 if (od->legacy) {
644 ret = omap_request_dma(c->dma_sig, "DMA engine",
645 omap_dma_callback, c, &c->dma_ch);
646 } else {
647 ret = omap_request_dma(c->dma_sig, "DMA engine", NULL, NULL,
648 &c->dma_ch);
649 }
Russell King7bedaa52012-04-13 12:10:24 +0100650
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +0300651 dev_dbg(dev, "allocating channel %u for %u\n", c->dma_ch, c->dma_sig);
Russell King596c4712013-12-10 11:08:01 +0000652
Russell King6ddeb6d2013-12-10 19:05:50 +0000653 if (ret >= 0) {
Russell King596c4712013-12-10 11:08:01 +0000654 omap_dma_assign(od, c, c->dma_ch);
655
Russell King6ddeb6d2013-12-10 19:05:50 +0000656 if (!od->legacy) {
657 unsigned val;
658
659 spin_lock_irq(&od->irq_lock);
660 val = BIT(c->dma_ch);
661 omap_dma_glbl_write(od, IRQSTATUS_L1, val);
662 od->irq_enable_mask |= val;
663 omap_dma_glbl_write(od, IRQENABLE_L1, od->irq_enable_mask);
664
665 val = omap_dma_glbl_read(od, IRQENABLE_L0);
666 val &= ~BIT(c->dma_ch);
667 omap_dma_glbl_write(od, IRQENABLE_L0, val);
668 spin_unlock_irq(&od->irq_lock);
669 }
670 }
671
Russell Kingaa4c5b92014-01-14 23:58:10 +0000672 if (dma_omap1()) {
673 if (__dma_omap16xx(od->plat->dma_attr)) {
674 c->ccr = CCR_OMAP31_DISABLE;
675 /* Duplicate what plat-omap/dma.c does */
676 c->ccr |= c->dma_ch + 1;
677 } else {
678 c->ccr = c->dma_sig & 0x1f;
679 }
680 } else {
681 c->ccr = c->dma_sig & 0x1f;
682 c->ccr |= (c->dma_sig & ~0x1f) << 14;
683 }
684 if (od->plat->errata & DMA_ERRATA_IFRAME_BUFFERING)
685 c->ccr |= CCR_BUFFERING_DISABLE;
686
Russell King596c4712013-12-10 11:08:01 +0000687 return ret;
Russell King7bedaa52012-04-13 12:10:24 +0100688}
689
690static void omap_dma_free_chan_resources(struct dma_chan *chan)
691{
Russell King6ddeb6d2013-12-10 19:05:50 +0000692 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King7bedaa52012-04-13 12:10:24 +0100693 struct omap_chan *c = to_omap_dma_chan(chan);
694
Russell King6ddeb6d2013-12-10 19:05:50 +0000695 if (!od->legacy) {
696 spin_lock_irq(&od->irq_lock);
697 od->irq_enable_mask &= ~BIT(c->dma_ch);
698 omap_dma_glbl_write(od, IRQENABLE_L1, od->irq_enable_mask);
699 spin_unlock_irq(&od->irq_lock);
700 }
701
Russell King596c4712013-12-10 11:08:01 +0000702 c->channel_base = NULL;
Russell King6ddeb6d2013-12-10 19:05:50 +0000703 od->lch_map[c->dma_ch] = NULL;
Russell King7bedaa52012-04-13 12:10:24 +0100704 vchan_free_chan_resources(&c->vc);
705 omap_free_dma(c->dma_ch);
706
Peter Ujfalusi3c9b8332016-07-20 11:50:30 +0300707 dev_dbg(od->ddev.dev, "freeing channel %u used for %u\n", c->dma_ch,
708 c->dma_sig);
Peter Ujfalusieea531e2015-04-09 12:35:52 +0300709 c->dma_sig = 0;
Russell King7bedaa52012-04-13 12:10:24 +0100710}
711
Russell King3850e222012-06-21 10:37:35 +0100712static size_t omap_dma_sg_size(struct omap_sg *sg)
713{
714 return sg->en * sg->fn;
715}
716
717static size_t omap_dma_desc_size(struct omap_desc *d)
718{
719 unsigned i;
720 size_t size;
721
722 for (size = i = 0; i < d->sglen; i++)
723 size += omap_dma_sg_size(&d->sg[i]);
724
725 return size * es_bytes[d->es];
726}
727
728static size_t omap_dma_desc_size_pos(struct omap_desc *d, dma_addr_t addr)
729{
730 unsigned i;
731 size_t size, es_size = es_bytes[d->es];
732
733 for (size = i = 0; i < d->sglen; i++) {
734 size_t this_size = omap_dma_sg_size(&d->sg[i]) * es_size;
735
736 if (size)
737 size += this_size;
738 else if (addr >= d->sg[i].addr &&
739 addr < d->sg[i].addr + this_size)
740 size += d->sg[i].addr + this_size - addr;
741 }
742 return size;
743}
744
Russell Kingb07fd622013-11-06 19:26:45 +0000745/*
746 * OMAP 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
747 * read before the DMA controller finished disabling the channel.
748 */
749static uint32_t omap_dma_chan_read_3_3(struct omap_chan *c, unsigned reg)
750{
751 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
752 uint32_t val;
753
754 val = omap_dma_chan_read(c, reg);
755 if (val == 0 && od->plat->errata & DMA_ERRATA_3_3)
756 val = omap_dma_chan_read(c, reg);
757
758 return val;
759}
760
Russell King3997cab2013-11-02 18:04:17 +0000761static dma_addr_t omap_dma_get_src_pos(struct omap_chan *c)
762{
763 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
Russell Kingb07fd622013-11-06 19:26:45 +0000764 dma_addr_t addr, cdac;
Russell King3997cab2013-11-02 18:04:17 +0000765
Russell Kingb07fd622013-11-06 19:26:45 +0000766 if (__dma_omap15xx(od->plat->dma_attr)) {
Russell Kingc5ed98b2013-11-06 17:33:09 +0000767 addr = omap_dma_chan_read(c, CPC);
Russell Kingb07fd622013-11-06 19:26:45 +0000768 } else {
769 addr = omap_dma_chan_read_3_3(c, CSAC);
770 cdac = omap_dma_chan_read_3_3(c, CDAC);
Russell King3997cab2013-11-02 18:04:17 +0000771
Russell King3997cab2013-11-02 18:04:17 +0000772 /*
773 * CDAC == 0 indicates that the DMA transfer on the channel has
774 * not been started (no data has been transferred so far).
775 * Return the programmed source start address in this case.
776 */
Russell Kingb07fd622013-11-06 19:26:45 +0000777 if (cdac == 0)
Russell Kingc5ed98b2013-11-06 17:33:09 +0000778 addr = omap_dma_chan_read(c, CSSA);
Russell King3997cab2013-11-02 18:04:17 +0000779 }
780
781 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000782 addr |= omap_dma_chan_read(c, CSSA) & 0xffff0000;
Russell King3997cab2013-11-02 18:04:17 +0000783
784 return addr;
785}
786
787static dma_addr_t omap_dma_get_dst_pos(struct omap_chan *c)
788{
789 struct omap_dmadev *od = to_omap_dma_dev(c->vc.chan.device);
790 dma_addr_t addr;
791
Russell Kingb07fd622013-11-06 19:26:45 +0000792 if (__dma_omap15xx(od->plat->dma_attr)) {
Russell Kingc5ed98b2013-11-06 17:33:09 +0000793 addr = omap_dma_chan_read(c, CPC);
Russell Kingb07fd622013-11-06 19:26:45 +0000794 } else {
795 addr = omap_dma_chan_read_3_3(c, CDAC);
Russell King3997cab2013-11-02 18:04:17 +0000796
Russell King3997cab2013-11-02 18:04:17 +0000797 /*
Russell Kingb07fd622013-11-06 19:26:45 +0000798 * CDAC == 0 indicates that the DMA transfer on the channel
799 * has not been started (no data has been transferred so
800 * far). Return the programmed destination start address in
801 * this case.
Russell King3997cab2013-11-02 18:04:17 +0000802 */
803 if (addr == 0)
Russell Kingc5ed98b2013-11-06 17:33:09 +0000804 addr = omap_dma_chan_read(c, CDSA);
Russell King3997cab2013-11-02 18:04:17 +0000805 }
806
807 if (dma_omap1())
Russell Kingc5ed98b2013-11-06 17:33:09 +0000808 addr |= omap_dma_chan_read(c, CDSA) & 0xffff0000;
Russell King3997cab2013-11-02 18:04:17 +0000809
810 return addr;
811}
812
Russell King7bedaa52012-04-13 12:10:24 +0100813static enum dma_status omap_dma_tx_status(struct dma_chan *chan,
814 dma_cookie_t cookie, struct dma_tx_state *txstate)
815{
Russell King3850e222012-06-21 10:37:35 +0100816 struct omap_chan *c = to_omap_dma_chan(chan);
Russell King3850e222012-06-21 10:37:35 +0100817 enum dma_status ret;
818 unsigned long flags;
Peter Ujfalusi4689d352019-07-16 11:24:59 +0300819 struct omap_desc *d = NULL;
Russell King3850e222012-06-21 10:37:35 +0100820
821 ret = dma_cookie_status(chan, cookie, txstate);
Peter Ujfalusi4689d352019-07-16 11:24:59 +0300822 if (ret == DMA_COMPLETE)
Russell King3850e222012-06-21 10:37:35 +0100823 return ret;
824
825 spin_lock_irqsave(&c->vc.lock, flags);
Peter Ujfalusi4689d352019-07-16 11:24:59 +0300826 if (c->desc && c->desc->vd.tx.cookie == cookie)
827 d = c->desc;
Peter Ujfalusiaac86702019-07-16 11:24:58 +0300828
Peter Ujfalusi4689d352019-07-16 11:24:59 +0300829 if (!txstate)
830 goto out;
831
832 if (d) {
Russell King3850e222012-06-21 10:37:35 +0100833 dma_addr_t pos;
834
835 if (d->dir == DMA_MEM_TO_DEV)
Russell King3997cab2013-11-02 18:04:17 +0000836 pos = omap_dma_get_src_pos(c);
Peter Ujfalusiadf850b2015-11-11 12:37:55 +0200837 else if (d->dir == DMA_DEV_TO_MEM || d->dir == DMA_MEM_TO_MEM)
Russell King3997cab2013-11-02 18:04:17 +0000838 pos = omap_dma_get_dst_pos(c);
Russell King3850e222012-06-21 10:37:35 +0100839 else
840 pos = 0;
841
842 txstate->residue = omap_dma_desc_size_pos(d, pos);
843 } else {
Peter Ujfalusi7a09c092019-07-30 16:20:15 +0300844 struct virt_dma_desc *vd = vchan_find_desc(&c->vc, cookie);
845
846 if (vd)
847 txstate->residue = omap_dma_desc_size(
848 to_omap_dma_desc(&vd->tx));
849 else
850 txstate->residue = 0;
Russell King3850e222012-06-21 10:37:35 +0100851 }
Peter Ujfalusiaac86702019-07-16 11:24:58 +0300852
Peter Ujfalusi4689d352019-07-16 11:24:59 +0300853out:
854 if (ret == DMA_IN_PROGRESS && c->paused) {
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +0530855 ret = DMA_PAUSED;
Peter Ujfalusi4689d352019-07-16 11:24:59 +0300856 } else if (d && d->polled && c->running) {
857 uint32_t ccr = omap_dma_chan_read(c, CCR);
858 /*
859 * The channel is no longer active, set the return value
860 * accordingly and mark it as completed
861 */
862 if (!(ccr & CCR_ENABLE)) {
863 struct omap_desc *d = c->desc;
864 ret = DMA_COMPLETE;
865 omap_dma_start_desc(c);
866 vchan_cookie_complete(&d->vd);
867 }
868 }
Peter Ujfalusiaac86702019-07-16 11:24:58 +0300869
Russell King3850e222012-06-21 10:37:35 +0100870 spin_unlock_irqrestore(&c->vc.lock, flags);
871
872 return ret;
Russell King7bedaa52012-04-13 12:10:24 +0100873}
874
875static void omap_dma_issue_pending(struct dma_chan *chan)
876{
877 struct omap_chan *c = to_omap_dma_chan(chan);
878 unsigned long flags;
879
880 spin_lock_irqsave(&c->vc.lock, flags);
Peter Ujfalusi1c1d25f2015-11-11 12:37:57 +0200881 if (vchan_issue_pending(&c->vc) && !c->desc)
882 omap_dma_start_desc(c);
Russell King7bedaa52012-04-13 12:10:24 +0100883 spin_unlock_irqrestore(&c->vc.lock, flags);
884}
885
886static struct dma_async_tx_descriptor *omap_dma_prep_slave_sg(
887 struct dma_chan *chan, struct scatterlist *sgl, unsigned sglen,
888 enum dma_transfer_direction dir, unsigned long tx_flags, void *context)
889{
Russell King49ae0b22013-11-02 21:09:18 +0000890 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King7bedaa52012-04-13 12:10:24 +0100891 struct omap_chan *c = to_omap_dma_chan(chan);
892 enum dma_slave_buswidth dev_width;
893 struct scatterlist *sgent;
894 struct omap_desc *d;
895 dma_addr_t dev_addr;
Peter Ujfalusie8a5e792015-11-11 12:37:56 +0200896 unsigned i, es, en, frame_bytes;
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +0300897 bool ll_failed = false;
Russell King7bedaa52012-04-13 12:10:24 +0100898 u32 burst;
Peter Ujfalusi201ac482016-11-29 16:23:42 +0200899 u32 port_window, port_window_bytes;
Russell King7bedaa52012-04-13 12:10:24 +0100900
901 if (dir == DMA_DEV_TO_MEM) {
902 dev_addr = c->cfg.src_addr;
903 dev_width = c->cfg.src_addr_width;
904 burst = c->cfg.src_maxburst;
Peter Ujfalusi201ac482016-11-29 16:23:42 +0200905 port_window = c->cfg.src_port_window_size;
Russell King7bedaa52012-04-13 12:10:24 +0100906 } else if (dir == DMA_MEM_TO_DEV) {
907 dev_addr = c->cfg.dst_addr;
908 dev_width = c->cfg.dst_addr_width;
909 burst = c->cfg.dst_maxburst;
Peter Ujfalusi201ac482016-11-29 16:23:42 +0200910 port_window = c->cfg.dst_port_window_size;
Russell King7bedaa52012-04-13 12:10:24 +0100911 } else {
912 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
913 return NULL;
914 }
915
916 /* Bus width translates to the element size (ES) */
917 switch (dev_width) {
918 case DMA_SLAVE_BUSWIDTH_1_BYTE:
Russell King90438262013-11-02 19:57:06 +0000919 es = CSDP_DATA_TYPE_8;
Russell King7bedaa52012-04-13 12:10:24 +0100920 break;
921 case DMA_SLAVE_BUSWIDTH_2_BYTES:
Russell King90438262013-11-02 19:57:06 +0000922 es = CSDP_DATA_TYPE_16;
Russell King7bedaa52012-04-13 12:10:24 +0100923 break;
924 case DMA_SLAVE_BUSWIDTH_4_BYTES:
Russell King90438262013-11-02 19:57:06 +0000925 es = CSDP_DATA_TYPE_32;
Russell King7bedaa52012-04-13 12:10:24 +0100926 break;
927 default: /* not reached */
928 return NULL;
929 }
930
931 /* Now allocate and setup the descriptor. */
Kees Cookacafe7e2018-05-08 13:45:50 -0700932 d = kzalloc(struct_size(d, sg, sglen), GFP_ATOMIC);
Russell King7bedaa52012-04-13 12:10:24 +0100933 if (!d)
934 return NULL;
935
936 d->dir = dir;
937 d->dev_addr = dev_addr;
938 d->es = es;
Russell King3ed4d182013-11-02 19:16:09 +0000939
Peter Ujfalusi9816c092017-06-16 10:40:55 -0500940 /* When the port_window is used, one frame must cover the window */
941 if (port_window) {
942 burst = port_window;
943 port_window_bytes = port_window * es_bytes[es];
944
945 d->ei = 1;
946 /*
947 * One frame covers the port_window and by configure
948 * the source frame index to be -1 * (port_window - 1)
949 * we instruct the sDMA that after a frame is processed
950 * it should move back to the start of the window.
951 */
952 d->fi = -(port_window_bytes - 1);
953 }
954
Russell Kingaa4c5b92014-01-14 23:58:10 +0000955 d->ccr = c->ccr | CCR_SYNC_FRAME;
Misael Lopez Cruze7b2acf2016-09-16 13:53:15 +0300956 if (dir == DMA_DEV_TO_MEM) {
Misael Lopez Cruze7b2acf2016-09-16 13:53:15 +0300957 d->csdp = CSDP_DST_BURST_64 | CSDP_DST_PACKED;
Peter Ujfalusi201ac482016-11-29 16:23:42 +0200958
959 d->ccr |= CCR_DST_AMODE_POSTINC;
960 if (port_window) {
961 d->ccr |= CCR_SRC_AMODE_DBLIDX;
Peter Ujfalusi527a2752017-01-09 16:50:52 +0200962
963 if (port_window_bytes >= 64)
964 d->csdp |= CSDP_SRC_BURST_64;
965 else if (port_window_bytes >= 32)
966 d->csdp |= CSDP_SRC_BURST_32;
967 else if (port_window_bytes >= 16)
968 d->csdp |= CSDP_SRC_BURST_16;
969
970 } else {
971 d->ccr |= CCR_SRC_AMODE_CONSTANT;
972 }
973 } else {
974 d->csdp = CSDP_SRC_BURST_64 | CSDP_SRC_PACKED;
975
976 d->ccr |= CCR_SRC_AMODE_POSTINC;
977 if (port_window) {
978 d->ccr |= CCR_DST_AMODE_DBLIDX;
Peter Ujfalusi201ac482016-11-29 16:23:42 +0200979
980 if (port_window_bytes >= 64)
Peter Ujfalusi527a2752017-01-09 16:50:52 +0200981 d->csdp |= CSDP_DST_BURST_64;
Peter Ujfalusi201ac482016-11-29 16:23:42 +0200982 else if (port_window_bytes >= 32)
Peter Ujfalusi527a2752017-01-09 16:50:52 +0200983 d->csdp |= CSDP_DST_BURST_32;
Peter Ujfalusi201ac482016-11-29 16:23:42 +0200984 else if (port_window_bytes >= 16)
Peter Ujfalusi527a2752017-01-09 16:50:52 +0200985 d->csdp |= CSDP_DST_BURST_16;
Peter Ujfalusi201ac482016-11-29 16:23:42 +0200986 } else {
987 d->ccr |= CCR_DST_AMODE_CONSTANT;
988 }
Misael Lopez Cruze7b2acf2016-09-16 13:53:15 +0300989 }
Russell King3ed4d182013-11-02 19:16:09 +0000990
Russell King90438262013-11-02 19:57:06 +0000991 d->cicr = CICR_DROP_IE | CICR_BLOCK_IE;
Misael Lopez Cruze7b2acf2016-09-16 13:53:15 +0300992 d->csdp |= es;
Russell Kingfa3ad862013-11-02 17:07:09 +0000993
Russell King2f0d13b2013-11-02 18:51:53 +0000994 if (dma_omap1()) {
Russell King90438262013-11-02 19:57:06 +0000995 d->cicr |= CICR_TOUT_IE;
Russell King2f0d13b2013-11-02 18:51:53 +0000996
997 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +0000998 d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_TIPB;
Russell King2f0d13b2013-11-02 18:51:53 +0000999 else
Russell King90438262013-11-02 19:57:06 +00001000 d->csdp |= CSDP_DST_PORT_TIPB | CSDP_SRC_PORT_EMIFF;
Russell King2f0d13b2013-11-02 18:51:53 +00001001 } else {
Russell King3ed4d182013-11-02 19:16:09 +00001002 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +00001003 d->ccr |= CCR_TRIGGER_SRC;
Russell King3ed4d182013-11-02 19:16:09 +00001004
Russell King90438262013-11-02 19:57:06 +00001005 d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
Peter Ujfalusi201ac482016-11-29 16:23:42 +02001006
1007 if (port_window)
1008 d->csdp |= CSDP_WRITE_LAST_NON_POSTED;
Russell King2f0d13b2013-11-02 18:51:53 +00001009 }
Russell King965aeb4d2013-11-06 17:12:30 +00001010 if (od->plat->errata & DMA_ERRATA_PARALLEL_CHANNELS)
1011 d->clnk_ctrl = c->dma_ch;
Russell King7bedaa52012-04-13 12:10:24 +01001012
1013 /*
1014 * Build our scatterlist entries: each contains the address,
1015 * the number of elements (EN) in each frame, and the number of
1016 * frames (FN). Number of bytes for this entry = ES * EN * FN.
1017 *
1018 * Burst size translates to number of elements with frame sync.
1019 * Note: DMA engine defines burst to be the number of dev-width
1020 * transfers.
1021 */
1022 en = burst;
1023 frame_bytes = es_bytes[es] * en;
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +03001024
1025 if (sglen >= 2)
1026 d->using_ll = od->ll123_supported;
1027
Russell King7bedaa52012-04-13 12:10:24 +01001028 for_each_sg(sgl, sgent, sglen, i) {
Peter Ujfalusicb7958d2016-07-20 11:50:31 +03001029 struct omap_sg *osg = &d->sg[i];
1030
1031 osg->addr = sg_dma_address(sgent);
1032 osg->en = en;
1033 osg->fn = sg_dma_len(sgent) / frame_bytes;
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +03001034
1035 if (d->using_ll) {
1036 osg->t2_desc = dma_pool_alloc(od->desc_pool, GFP_ATOMIC,
1037 &osg->t2_desc_paddr);
1038 if (!osg->t2_desc) {
1039 dev_err(chan->device->dev,
1040 "t2_desc[%d] allocation failed\n", i);
1041 ll_failed = true;
1042 d->using_ll = false;
1043 continue;
1044 }
1045
1046 omap_dma_fill_type2_desc(d, i, dir, (i == sglen - 1));
1047 }
Russell King7bedaa52012-04-13 12:10:24 +01001048 }
1049
Peter Ujfalusie8a5e792015-11-11 12:37:56 +02001050 d->sglen = sglen;
Russell King7bedaa52012-04-13 12:10:24 +01001051
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +03001052 /* Release the dma_pool entries if one allocation failed */
1053 if (ll_failed) {
1054 for (i = 0; i < d->sglen; i++) {
1055 struct omap_sg *osg = &d->sg[i];
1056
1057 if (osg->t2_desc) {
1058 dma_pool_free(od->desc_pool, osg->t2_desc,
1059 osg->t2_desc_paddr);
1060 osg->t2_desc = NULL;
1061 }
1062 }
1063 }
1064
Russell King7bedaa52012-04-13 12:10:24 +01001065 return vchan_tx_prep(&c->vc, &d->vd, tx_flags);
1066}
1067
Russell King3a774ea2012-06-21 10:40:15 +01001068static struct dma_async_tx_descriptor *omap_dma_prep_dma_cyclic(
1069 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
Laurent Pinchart31c1e5a2014-08-01 12:20:10 +02001070 size_t period_len, enum dma_transfer_direction dir, unsigned long flags)
Russell King3a774ea2012-06-21 10:40:15 +01001071{
Russell Kingfa3ad862013-11-02 17:07:09 +00001072 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King3a774ea2012-06-21 10:40:15 +01001073 struct omap_chan *c = to_omap_dma_chan(chan);
1074 enum dma_slave_buswidth dev_width;
1075 struct omap_desc *d;
1076 dma_addr_t dev_addr;
Russell King3ed4d182013-11-02 19:16:09 +00001077 unsigned es;
Russell King3a774ea2012-06-21 10:40:15 +01001078 u32 burst;
1079
1080 if (dir == DMA_DEV_TO_MEM) {
1081 dev_addr = c->cfg.src_addr;
1082 dev_width = c->cfg.src_addr_width;
1083 burst = c->cfg.src_maxburst;
Russell King3a774ea2012-06-21 10:40:15 +01001084 } else if (dir == DMA_MEM_TO_DEV) {
1085 dev_addr = c->cfg.dst_addr;
1086 dev_width = c->cfg.dst_addr_width;
1087 burst = c->cfg.dst_maxburst;
Russell King3a774ea2012-06-21 10:40:15 +01001088 } else {
1089 dev_err(chan->device->dev, "%s: bad direction?\n", __func__);
1090 return NULL;
1091 }
1092
1093 /* Bus width translates to the element size (ES) */
1094 switch (dev_width) {
1095 case DMA_SLAVE_BUSWIDTH_1_BYTE:
Russell King90438262013-11-02 19:57:06 +00001096 es = CSDP_DATA_TYPE_8;
Russell King3a774ea2012-06-21 10:40:15 +01001097 break;
1098 case DMA_SLAVE_BUSWIDTH_2_BYTES:
Russell King90438262013-11-02 19:57:06 +00001099 es = CSDP_DATA_TYPE_16;
Russell King3a774ea2012-06-21 10:40:15 +01001100 break;
1101 case DMA_SLAVE_BUSWIDTH_4_BYTES:
Russell King90438262013-11-02 19:57:06 +00001102 es = CSDP_DATA_TYPE_32;
Russell King3a774ea2012-06-21 10:40:15 +01001103 break;
1104 default: /* not reached */
1105 return NULL;
1106 }
1107
1108 /* Now allocate and setup the descriptor. */
1109 d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC);
1110 if (!d)
1111 return NULL;
1112
1113 d->dir = dir;
1114 d->dev_addr = dev_addr;
1115 d->fi = burst;
1116 d->es = es;
Russell King3a774ea2012-06-21 10:40:15 +01001117 d->sg[0].addr = buf_addr;
1118 d->sg[0].en = period_len / es_bytes[es];
1119 d->sg[0].fn = buf_len / period_len;
1120 d->sglen = 1;
Russell King3ed4d182013-11-02 19:16:09 +00001121
Russell Kingaa4c5b92014-01-14 23:58:10 +00001122 d->ccr = c->ccr;
Russell King3ed4d182013-11-02 19:16:09 +00001123 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +00001124 d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_CONSTANT;
Russell King3ed4d182013-11-02 19:16:09 +00001125 else
Russell King90438262013-11-02 19:57:06 +00001126 d->ccr |= CCR_DST_AMODE_CONSTANT | CCR_SRC_AMODE_POSTINC;
Russell King3ed4d182013-11-02 19:16:09 +00001127
Russell King90438262013-11-02 19:57:06 +00001128 d->cicr = CICR_DROP_IE;
Russell Kingfa3ad862013-11-02 17:07:09 +00001129 if (flags & DMA_PREP_INTERRUPT)
Russell King90438262013-11-02 19:57:06 +00001130 d->cicr |= CICR_FRAME_IE;
Russell Kingfa3ad862013-11-02 17:07:09 +00001131
Russell King2f0d13b2013-11-02 18:51:53 +00001132 d->csdp = es;
1133
1134 if (dma_omap1()) {
Russell King90438262013-11-02 19:57:06 +00001135 d->cicr |= CICR_TOUT_IE;
Russell King2f0d13b2013-11-02 18:51:53 +00001136
1137 if (dir == DMA_DEV_TO_MEM)
Russell King90438262013-11-02 19:57:06 +00001138 d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_MPUI;
Russell King2f0d13b2013-11-02 18:51:53 +00001139 else
Russell King90438262013-11-02 19:57:06 +00001140 d->csdp |= CSDP_DST_PORT_MPUI | CSDP_SRC_PORT_EMIFF;
Russell King2f0d13b2013-11-02 18:51:53 +00001141 } else {
Russell King3ed4d182013-11-02 19:16:09 +00001142 if (burst)
Russell King90438262013-11-02 19:57:06 +00001143 d->ccr |= CCR_SYNC_PACKET;
1144 else
1145 d->ccr |= CCR_SYNC_ELEMENT;
Russell King3ed4d182013-11-02 19:16:09 +00001146
Misael Lopez Cruz47fac242015-09-14 15:31:05 +03001147 if (dir == DMA_DEV_TO_MEM) {
Russell King90438262013-11-02 19:57:06 +00001148 d->ccr |= CCR_TRIGGER_SRC;
Misael Lopez Cruz47fac242015-09-14 15:31:05 +03001149 d->csdp |= CSDP_DST_PACKED;
1150 } else {
1151 d->csdp |= CSDP_SRC_PACKED;
1152 }
Russell King3ed4d182013-11-02 19:16:09 +00001153
Russell King90438262013-11-02 19:57:06 +00001154 d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
Russell King3a774ea2012-06-21 10:40:15 +01001155
Russell King90438262013-11-02 19:57:06 +00001156 d->csdp |= CSDP_DST_BURST_64 | CSDP_SRC_BURST_64;
Russell King2f0d13b2013-11-02 18:51:53 +00001157 }
1158
Russell King965aeb4d2013-11-06 17:12:30 +00001159 if (__dma_omap15xx(od->plat->dma_attr))
1160 d->ccr |= CCR_AUTO_INIT | CCR_REPEAT;
1161 else
1162 d->clnk_ctrl = c->dma_ch | CLNK_CTRL_ENABLE_LNK;
1163
Russell King3ed4d182013-11-02 19:16:09 +00001164 c->cyclic = true;
Russell King3a774ea2012-06-21 10:40:15 +01001165
Peter Ujfalusi2dde5b92012-09-14 15:05:48 +03001166 return vchan_tx_prep(&c->vc, &d->vd, flags);
Russell King3a774ea2012-06-21 10:40:15 +01001167}
1168
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +03001169static struct dma_async_tx_descriptor *omap_dma_prep_dma_memcpy(
1170 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
1171 size_t len, unsigned long tx_flags)
1172{
1173 struct omap_chan *c = to_omap_dma_chan(chan);
1174 struct omap_desc *d;
1175 uint8_t data_type;
1176
1177 d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC);
1178 if (!d)
1179 return NULL;
1180
1181 data_type = __ffs((src | dest | len));
1182 if (data_type > CSDP_DATA_TYPE_32)
1183 data_type = CSDP_DATA_TYPE_32;
1184
1185 d->dir = DMA_MEM_TO_MEM;
1186 d->dev_addr = src;
1187 d->fi = 0;
1188 d->es = data_type;
1189 d->sg[0].en = len / BIT(data_type);
1190 d->sg[0].fn = 1;
1191 d->sg[0].addr = dest;
1192 d->sglen = 1;
1193 d->ccr = c->ccr;
1194 d->ccr |= CCR_DST_AMODE_POSTINC | CCR_SRC_AMODE_POSTINC;
1195
Peter Ujfalusi4689d352019-07-16 11:24:59 +03001196 if (tx_flags & DMA_PREP_INTERRUPT)
1197 d->cicr |= CICR_FRAME_IE;
1198 else
1199 d->polled = true;
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +03001200
1201 d->csdp = data_type;
1202
1203 if (dma_omap1()) {
1204 d->cicr |= CICR_TOUT_IE;
1205 d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_EMIFF;
1206 } else {
1207 d->csdp |= CSDP_DST_PACKED | CSDP_SRC_PACKED;
1208 d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
1209 d->csdp |= CSDP_DST_BURST_64 | CSDP_SRC_BURST_64;
1210 }
1211
1212 return vchan_tx_prep(&c->vc, &d->vd, tx_flags);
1213}
1214
Peter Ujfalusiad524652016-07-12 14:21:14 +03001215static struct dma_async_tx_descriptor *omap_dma_prep_dma_interleaved(
1216 struct dma_chan *chan, struct dma_interleaved_template *xt,
1217 unsigned long flags)
1218{
1219 struct omap_chan *c = to_omap_dma_chan(chan);
1220 struct omap_desc *d;
1221 struct omap_sg *sg;
1222 uint8_t data_type;
1223 size_t src_icg, dst_icg;
1224
1225 /* Slave mode is not supported */
1226 if (is_slave_direction(xt->dir))
1227 return NULL;
1228
1229 if (xt->frame_size != 1 || xt->numf == 0)
1230 return NULL;
1231
1232 d = kzalloc(sizeof(*d) + sizeof(d->sg[0]), GFP_ATOMIC);
1233 if (!d)
1234 return NULL;
1235
1236 data_type = __ffs((xt->src_start | xt->dst_start | xt->sgl[0].size));
1237 if (data_type > CSDP_DATA_TYPE_32)
1238 data_type = CSDP_DATA_TYPE_32;
1239
1240 sg = &d->sg[0];
1241 d->dir = DMA_MEM_TO_MEM;
1242 d->dev_addr = xt->src_start;
1243 d->es = data_type;
1244 sg->en = xt->sgl[0].size / BIT(data_type);
1245 sg->fn = xt->numf;
1246 sg->addr = xt->dst_start;
1247 d->sglen = 1;
1248 d->ccr = c->ccr;
1249
1250 src_icg = dmaengine_get_src_icg(xt, &xt->sgl[0]);
1251 dst_icg = dmaengine_get_dst_icg(xt, &xt->sgl[0]);
1252 if (src_icg) {
1253 d->ccr |= CCR_SRC_AMODE_DBLIDX;
1254 d->ei = 1;
1255 d->fi = src_icg;
1256 } else if (xt->src_inc) {
1257 d->ccr |= CCR_SRC_AMODE_POSTINC;
1258 d->fi = 0;
1259 } else {
1260 dev_err(chan->device->dev,
1261 "%s: SRC constant addressing is not supported\n",
1262 __func__);
1263 kfree(d);
1264 return NULL;
1265 }
1266
1267 if (dst_icg) {
1268 d->ccr |= CCR_DST_AMODE_DBLIDX;
1269 sg->ei = 1;
1270 sg->fi = dst_icg;
1271 } else if (xt->dst_inc) {
1272 d->ccr |= CCR_DST_AMODE_POSTINC;
1273 sg->fi = 0;
1274 } else {
1275 dev_err(chan->device->dev,
1276 "%s: DST constant addressing is not supported\n",
1277 __func__);
1278 kfree(d);
1279 return NULL;
1280 }
1281
1282 d->cicr = CICR_DROP_IE | CICR_FRAME_IE;
1283
1284 d->csdp = data_type;
1285
1286 if (dma_omap1()) {
1287 d->cicr |= CICR_TOUT_IE;
1288 d->csdp |= CSDP_DST_PORT_EMIFF | CSDP_SRC_PORT_EMIFF;
1289 } else {
1290 d->csdp |= CSDP_DST_PACKED | CSDP_SRC_PACKED;
1291 d->cicr |= CICR_MISALIGNED_ERR_IE | CICR_TRANS_ERR_IE;
1292 d->csdp |= CSDP_DST_BURST_64 | CSDP_SRC_BURST_64;
1293 }
1294
1295 return vchan_tx_prep(&c->vc, &d->vd, flags);
1296}
1297
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001298static int omap_dma_slave_config(struct dma_chan *chan, struct dma_slave_config *cfg)
Russell King7bedaa52012-04-13 12:10:24 +01001299{
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001300 struct omap_chan *c = to_omap_dma_chan(chan);
1301
Russell King7bedaa52012-04-13 12:10:24 +01001302 if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES ||
1303 cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
1304 return -EINVAL;
1305
Peter Ujfalusi05ec62a2017-10-03 11:35:38 +03001306 if (cfg->src_maxburst > chan->device->max_burst ||
1307 cfg->dst_maxburst > chan->device->max_burst)
1308 return -EINVAL;
1309
Russell King7bedaa52012-04-13 12:10:24 +01001310 memcpy(&c->cfg, cfg, sizeof(c->cfg));
1311
1312 return 0;
1313}
1314
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001315static int omap_dma_terminate_all(struct dma_chan *chan)
Russell King7bedaa52012-04-13 12:10:24 +01001316{
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001317 struct omap_chan *c = to_omap_dma_chan(chan);
Russell King7bedaa52012-04-13 12:10:24 +01001318 unsigned long flags;
1319 LIST_HEAD(head);
1320
1321 spin_lock_irqsave(&c->vc.lock, flags);
1322
Russell King7bedaa52012-04-13 12:10:24 +01001323 /*
1324 * Stop DMA activity: we assume the callback will not be called
Russell Kingfa3ad862013-11-02 17:07:09 +00001325 * after omap_dma_stop() returns (even if it does, it will see
Russell King7bedaa52012-04-13 12:10:24 +01001326 * c->desc is NULL and exit.)
1327 */
1328 if (c->desc) {
Peter Ujfalusib1faf0f2017-11-14 16:32:05 +02001329 vchan_terminate_vdesc(&c->desc->vd);
Russell King7bedaa52012-04-13 12:10:24 +01001330 c->desc = NULL;
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001331 /* Avoid stopping the dma twice */
1332 if (!c->paused)
Russell Kingfa3ad862013-11-02 17:07:09 +00001333 omap_dma_stop(c);
Russell King7bedaa52012-04-13 12:10:24 +01001334 }
1335
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +05301336 c->cyclic = false;
1337 c->paused = false;
Russell King3a774ea2012-06-21 10:40:15 +01001338
Russell King7bedaa52012-04-13 12:10:24 +01001339 vchan_get_all_descriptors(&c->vc, &head);
1340 spin_unlock_irqrestore(&c->vc.lock, flags);
1341 vchan_dma_desc_free_list(&c->vc, &head);
1342
1343 return 0;
1344}
1345
Peter Ujfalusi9bef6d82016-02-11 11:08:34 +02001346static void omap_dma_synchronize(struct dma_chan *chan)
1347{
1348 struct omap_chan *c = to_omap_dma_chan(chan);
1349
1350 vchan_synchronize(&c->vc);
1351}
1352
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001353static int omap_dma_pause(struct dma_chan *chan)
Russell King7bedaa52012-04-13 12:10:24 +01001354{
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001355 struct omap_chan *c = to_omap_dma_chan(chan);
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +05301356 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
1357 unsigned long flags;
1358 int ret = -EINVAL;
Colin Ian King0741b812016-10-19 12:05:53 +01001359 bool can_pause = false;
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001360
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +05301361 spin_lock_irqsave(&od->irq_lock, flags);
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001362
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +05301363 if (!c->desc)
1364 goto out;
1365
1366 if (c->cyclic)
1367 can_pause = true;
1368
1369 /*
1370 * We do not allow DMA_MEM_TO_DEV transfers to be paused.
1371 * From the AM572x TRM, 16.1.4.18 Disabling a Channel During Transfer:
1372 * "When a channel is disabled during a transfer, the channel undergoes
1373 * an abort, unless it is hardware-source-synchronized …".
1374 * A source-synchronised channel is one where the fetching of data is
1375 * under control of the device. In other words, a device-to-memory
1376 * transfer. So, a destination-synchronised channel (which would be a
1377 * memory-to-device transfer) undergoes an abort if the the CCR_ENABLE
1378 * bit is cleared.
1379 * From 16.1.4.20.4.6.2 Abort: "If an abort trigger occurs, the channel
1380 * aborts immediately after completion of current read/write
1381 * transactions and then the FIFO is cleaned up." The term "cleaned up"
1382 * is not defined. TI recommends to check that RD_ACTIVE and WR_ACTIVE
1383 * are both clear _before_ disabling the channel, otherwise data loss
1384 * will occur.
1385 * The problem is that if the channel is active, then device activity
1386 * can result in DMA activity starting between reading those as both
1387 * clear and the write to DMA_CCR to clear the enable bit hitting the
1388 * hardware. If the DMA hardware can't drain the data in its FIFO to the
1389 * destination, then data loss "might" occur (say if we write to an UART
1390 * and the UART is not accepting any further data).
1391 */
1392 else if (c->desc->dir == DMA_DEV_TO_MEM)
1393 can_pause = true;
1394
1395 if (can_pause && !c->paused) {
1396 ret = omap_dma_stop(c);
1397 if (!ret)
1398 c->paused = true;
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001399 }
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +05301400out:
1401 spin_unlock_irqrestore(&od->irq_lock, flags);
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001402
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +05301403 return ret;
Russell King7bedaa52012-04-13 12:10:24 +01001404}
1405
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001406static int omap_dma_resume(struct dma_chan *chan)
Russell King7bedaa52012-04-13 12:10:24 +01001407{
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001408 struct omap_chan *c = to_omap_dma_chan(chan);
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +05301409 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
1410 unsigned long flags;
1411 int ret = -EINVAL;
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001412
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +05301413 spin_lock_irqsave(&od->irq_lock, flags);
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001414
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +05301415 if (c->paused && c->desc) {
Peter Ujfalusib3d09da2014-09-16 22:45:56 +03001416 mb();
1417
Peter Ujfalusibfb60742014-09-16 22:45:57 +03001418 /* Restore channel link register */
1419 omap_dma_chan_write(c, CLNK_CTRL, c->desc->clnk_ctrl);
1420
Russell Kingfa3ad862013-11-02 17:07:09 +00001421 omap_dma_start(c, c->desc);
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001422 c->paused = false;
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +05301423 ret = 0;
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001424 }
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +05301425 spin_unlock_irqrestore(&od->irq_lock, flags);
Peter Ujfalusi2dcdf572012-09-14 15:05:45 +03001426
Sebastian Andrzej Siewior99340752016-10-14 10:30:47 +05301427 return ret;
Russell King7bedaa52012-04-13 12:10:24 +01001428}
1429
Peter Ujfalusieea531e2015-04-09 12:35:52 +03001430static int omap_dma_chan_init(struct omap_dmadev *od)
Russell King7bedaa52012-04-13 12:10:24 +01001431{
1432 struct omap_chan *c;
1433
1434 c = kzalloc(sizeof(*c), GFP_KERNEL);
1435 if (!c)
1436 return -ENOMEM;
1437
Russell King596c4712013-12-10 11:08:01 +00001438 c->reg_map = od->reg_map;
Russell King7bedaa52012-04-13 12:10:24 +01001439 c->vc.desc_free = omap_dma_desc_free;
1440 vchan_init(&c->vc, &od->ddev);
Russell King7bedaa52012-04-13 12:10:24 +01001441
Russell King7bedaa52012-04-13 12:10:24 +01001442 return 0;
1443}
1444
1445static void omap_dma_free(struct omap_dmadev *od)
1446{
Russell King7bedaa52012-04-13 12:10:24 +01001447 while (!list_empty(&od->ddev.channels)) {
1448 struct omap_chan *c = list_first_entry(&od->ddev.channels,
1449 struct omap_chan, vc.chan.device_node);
1450
1451 list_del(&c->vc.chan.device_node);
1452 tasklet_kill(&c->vc.task);
1453 kfree(c);
1454 }
Russell King7bedaa52012-04-13 12:10:24 +01001455}
1456
Peter Ujfalusi80b0e0a2014-03-29 19:03:30 +05301457#define OMAP_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
1458 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
1459 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
1460
Russell King7bedaa52012-04-13 12:10:24 +01001461static int omap_dma_probe(struct platform_device *pdev)
1462{
1463 struct omap_dmadev *od;
Russell King596c4712013-12-10 11:08:01 +00001464 struct resource *res;
Russell King6ddeb6d2013-12-10 19:05:50 +00001465 int rc, i, irq;
Peter Ujfalusi836c3ce2017-01-02 12:07:37 +02001466 u32 lch_count;
Russell King7bedaa52012-04-13 12:10:24 +01001467
Russell King104fce72013-11-02 12:58:29 +00001468 od = devm_kzalloc(&pdev->dev, sizeof(*od), GFP_KERNEL);
Russell King7bedaa52012-04-13 12:10:24 +01001469 if (!od)
1470 return -ENOMEM;
1471
Russell King596c4712013-12-10 11:08:01 +00001472 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1473 od->base = devm_ioremap_resource(&pdev->dev, res);
1474 if (IS_ERR(od->base))
1475 return PTR_ERR(od->base);
1476
Russell King1b416c42013-11-02 13:00:03 +00001477 od->plat = omap_get_plat_info();
1478 if (!od->plat)
1479 return -EPROBE_DEFER;
1480
Russell King596c4712013-12-10 11:08:01 +00001481 od->reg_map = od->plat->reg_map;
1482
Russell King7bedaa52012-04-13 12:10:24 +01001483 dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
Russell King3a774ea2012-06-21 10:40:15 +01001484 dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +03001485 dma_cap_set(DMA_MEMCPY, od->ddev.cap_mask);
Peter Ujfalusiad524652016-07-12 14:21:14 +03001486 dma_cap_set(DMA_INTERLEAVE, od->ddev.cap_mask);
Russell King7bedaa52012-04-13 12:10:24 +01001487 od->ddev.device_alloc_chan_resources = omap_dma_alloc_chan_resources;
1488 od->ddev.device_free_chan_resources = omap_dma_free_chan_resources;
1489 od->ddev.device_tx_status = omap_dma_tx_status;
1490 od->ddev.device_issue_pending = omap_dma_issue_pending;
1491 od->ddev.device_prep_slave_sg = omap_dma_prep_slave_sg;
Russell King3a774ea2012-06-21 10:40:15 +01001492 od->ddev.device_prep_dma_cyclic = omap_dma_prep_dma_cyclic;
Peter Ujfalusi4ce98c02015-04-22 10:34:29 +03001493 od->ddev.device_prep_dma_memcpy = omap_dma_prep_dma_memcpy;
Peter Ujfalusiad524652016-07-12 14:21:14 +03001494 od->ddev.device_prep_interleaved_dma = omap_dma_prep_dma_interleaved;
Vinod Koul6c04cd42014-12-07 23:12:31 +05301495 od->ddev.device_config = omap_dma_slave_config;
Maxime Ripard78ea4fe2014-11-17 14:42:28 +01001496 od->ddev.device_pause = omap_dma_pause;
1497 od->ddev.device_resume = omap_dma_resume;
1498 od->ddev.device_terminate_all = omap_dma_terminate_all;
Peter Ujfalusi9bef6d82016-02-11 11:08:34 +02001499 od->ddev.device_synchronize = omap_dma_synchronize;
Maxime Ripard7d15b872014-11-17 14:42:49 +01001500 od->ddev.src_addr_widths = OMAP_DMA_BUSWIDTHS;
1501 od->ddev.dst_addr_widths = OMAP_DMA_BUSWIDTHS;
1502 od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
Janusz Krzysztofikc9bd0942018-06-05 18:59:57 +02001503 if (__dma_omap15xx(od->plat->dma_attr))
1504 od->ddev.residue_granularity =
1505 DMA_RESIDUE_GRANULARITY_DESCRIPTOR;
1506 else
1507 od->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Peter Ujfalusi05ec62a2017-10-03 11:35:38 +03001508 od->ddev.max_burst = SZ_16M - 1; /* CCEN: 24bit unsigned */
Russell King7bedaa52012-04-13 12:10:24 +01001509 od->ddev.dev = &pdev->dev;
1510 INIT_LIST_HEAD(&od->ddev.channels);
Russell King7bedaa52012-04-13 12:10:24 +01001511 spin_lock_init(&od->lock);
Russell King6ddeb6d2013-12-10 19:05:50 +00001512 spin_lock_init(&od->irq_lock);
Russell King7bedaa52012-04-13 12:10:24 +01001513
Peter Ujfalusi836c3ce2017-01-02 12:07:37 +02001514 /* Number of DMA requests */
1515 od->dma_requests = OMAP_SDMA_REQUESTS;
1516 if (pdev->dev.of_node && of_property_read_u32(pdev->dev.of_node,
1517 "dma-requests",
1518 &od->dma_requests)) {
Peter Ujfaluside506082015-04-09 12:35:51 +03001519 dev_info(&pdev->dev,
1520 "Missing dma-requests property, using %u.\n",
1521 OMAP_SDMA_REQUESTS);
1522 }
1523
Peter Ujfalusi836c3ce2017-01-02 12:07:37 +02001524 /* Number of available logical channels */
1525 if (!pdev->dev.of_node) {
1526 lch_count = od->plat->dma_attr->lch_count;
1527 if (unlikely(!lch_count))
1528 lch_count = OMAP_SDMA_CHANNELS;
1529 } else if (of_property_read_u32(pdev->dev.of_node, "dma-channels",
1530 &lch_count)) {
1531 dev_info(&pdev->dev,
1532 "Missing dma-channels property, using %u.\n",
1533 OMAP_SDMA_CHANNELS);
1534 lch_count = OMAP_SDMA_CHANNELS;
1535 }
1536
1537 od->lch_map = devm_kcalloc(&pdev->dev, lch_count, sizeof(*od->lch_map),
1538 GFP_KERNEL);
Peter Ujfalusi2d1a9a92016-07-20 11:50:29 +03001539 if (!od->lch_map)
1540 return -ENOMEM;
1541
1542 for (i = 0; i < od->dma_requests; i++) {
Peter Ujfalusieea531e2015-04-09 12:35:52 +03001543 rc = omap_dma_chan_init(od);
Russell King7bedaa52012-04-13 12:10:24 +01001544 if (rc) {
1545 omap_dma_free(od);
1546 return rc;
1547 }
1548 }
1549
Russell King6ddeb6d2013-12-10 19:05:50 +00001550 irq = platform_get_irq(pdev, 1);
1551 if (irq <= 0) {
1552 dev_info(&pdev->dev, "failed to get L1 IRQ: %d\n", irq);
1553 od->legacy = true;
1554 } else {
1555 /* Disable all interrupts */
1556 od->irq_enable_mask = 0;
1557 omap_dma_glbl_write(od, IRQENABLE_L1, 0);
1558
1559 rc = devm_request_irq(&pdev->dev, irq, omap_dma_irq,
1560 IRQF_SHARED, "omap-dma-engine", od);
1561 if (rc)
1562 return rc;
1563 }
1564
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +03001565 if (omap_dma_glbl_read(od, CAPS_0) & CAPS_0_SUPPORT_LL123)
1566 od->ll123_supported = true;
1567
Peter Ujfalusi020c62a2015-12-14 22:47:42 +02001568 od->ddev.filter.map = od->plat->slave_map;
1569 od->ddev.filter.mapcnt = od->plat->slavecnt;
1570 od->ddev.filter.fn = omap_dma_filter_fn;
1571
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +03001572 if (od->ll123_supported) {
1573 od->desc_pool = dma_pool_create(dev_name(&pdev->dev),
1574 &pdev->dev,
1575 sizeof(struct omap_type2_desc),
1576 4, 0);
1577 if (!od->desc_pool) {
1578 dev_err(&pdev->dev,
1579 "unable to allocate descriptor pool\n");
1580 od->ll123_supported = false;
1581 }
1582 }
1583
Russell King7bedaa52012-04-13 12:10:24 +01001584 rc = dma_async_device_register(&od->ddev);
1585 if (rc) {
1586 pr_warn("OMAP-DMA: failed to register slave DMA engine device: %d\n",
1587 rc);
1588 omap_dma_free(od);
Jon Hunter8d306622013-02-26 12:27:24 -06001589 return rc;
1590 }
1591
1592 platform_set_drvdata(pdev, od);
1593
1594 if (pdev->dev.of_node) {
1595 omap_dma_info.dma_cap = od->ddev.cap_mask;
1596
1597 /* Device-tree DMA controller registration */
1598 rc = of_dma_controller_register(pdev->dev.of_node,
1599 of_dma_simple_xlate, &omap_dma_info);
1600 if (rc) {
1601 pr_warn("OMAP-DMA: failed to register DMA controller\n");
1602 dma_async_device_unregister(&od->ddev);
1603 omap_dma_free(od);
1604 }
Russell King7bedaa52012-04-13 12:10:24 +01001605 }
1606
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +03001607 dev_info(&pdev->dev, "OMAP DMA engine driver%s\n",
1608 od->ll123_supported ? " (LinkedList1/2/3 supported)" : "");
Russell King7bedaa52012-04-13 12:10:24 +01001609
1610 return rc;
1611}
1612
1613static int omap_dma_remove(struct platform_device *pdev)
1614{
1615 struct omap_dmadev *od = platform_get_drvdata(pdev);
Vinod Koul898dbbf2016-07-05 09:58:33 +05301616 int irq;
Russell King7bedaa52012-04-13 12:10:24 +01001617
Jon Hunter8d306622013-02-26 12:27:24 -06001618 if (pdev->dev.of_node)
1619 of_dma_controller_free(pdev->dev.of_node);
1620
Vinod Koul898dbbf2016-07-05 09:58:33 +05301621 irq = platform_get_irq(pdev, 1);
1622 devm_free_irq(&pdev->dev, irq, od);
1623
Russell King7bedaa52012-04-13 12:10:24 +01001624 dma_async_device_unregister(&od->ddev);
Russell King6ddeb6d2013-12-10 19:05:50 +00001625
1626 if (!od->legacy) {
1627 /* Disable all interrupts */
1628 omap_dma_glbl_write(od, IRQENABLE_L0, 0);
1629 }
1630
Peter Ujfalusi1c2e8e62016-07-20 11:50:32 +03001631 if (od->ll123_supported)
1632 dma_pool_destroy(od->desc_pool);
1633
Russell King7bedaa52012-04-13 12:10:24 +01001634 omap_dma_free(od);
1635
1636 return 0;
1637}
1638
Jon Hunter8d306622013-02-26 12:27:24 -06001639static const struct of_device_id omap_dma_match[] = {
1640 { .compatible = "ti,omap2420-sdma", },
1641 { .compatible = "ti,omap2430-sdma", },
1642 { .compatible = "ti,omap3430-sdma", },
1643 { .compatible = "ti,omap3630-sdma", },
1644 { .compatible = "ti,omap4430-sdma", },
1645 {},
1646};
1647MODULE_DEVICE_TABLE(of, omap_dma_match);
1648
Russell King7bedaa52012-04-13 12:10:24 +01001649static struct platform_driver omap_dma_driver = {
1650 .probe = omap_dma_probe,
1651 .remove = omap_dma_remove,
1652 .driver = {
1653 .name = "omap-dma-engine",
Jon Hunter8d306622013-02-26 12:27:24 -06001654 .of_match_table = of_match_ptr(omap_dma_match),
Russell King7bedaa52012-04-13 12:10:24 +01001655 },
1656};
1657
Arnd Bergmann9c71b9e2019-07-22 10:16:44 +02001658static bool omap_dma_filter_fn(struct dma_chan *chan, void *param)
Russell King7bedaa52012-04-13 12:10:24 +01001659{
1660 if (chan->device->dev->driver == &omap_dma_driver.driver) {
Peter Ujfalusieea531e2015-04-09 12:35:52 +03001661 struct omap_dmadev *od = to_omap_dma_dev(chan->device);
Russell King7bedaa52012-04-13 12:10:24 +01001662 struct omap_chan *c = to_omap_dma_chan(chan);
1663 unsigned req = *(unsigned *)param;
1664
Peter Ujfalusieea531e2015-04-09 12:35:52 +03001665 if (req <= od->dma_requests) {
1666 c->dma_sig = req;
1667 return true;
1668 }
Russell King7bedaa52012-04-13 12:10:24 +01001669 }
1670 return false;
1671}
Russell King7bedaa52012-04-13 12:10:24 +01001672
Russell King7bedaa52012-04-13 12:10:24 +01001673static int omap_dma_init(void)
1674{
Tony Lindgrenbe1f9482013-01-11 11:24:19 -08001675 return platform_driver_register(&omap_dma_driver);
Russell King7bedaa52012-04-13 12:10:24 +01001676}
1677subsys_initcall(omap_dma_init);
1678
1679static void __exit omap_dma_exit(void)
1680{
Russell King7bedaa52012-04-13 12:10:24 +01001681 platform_driver_unregister(&omap_dma_driver);
1682}
1683module_exit(omap_dma_exit);
1684
1685MODULE_AUTHOR("Russell King");
1686MODULE_LICENSE("GPL");