blob: ba74e10aa0c27d25e6a965bcf1a3ecad67cb3943 [file] [log] [blame]
Angelo Dureghello9d831522018-08-19 19:27:13 +02001// SPDX-License-Identifier: GPL-2.0+
2//
3// Copyright (c) 2013-2014 Freescale Semiconductor, Inc
4// Copyright (c) 2017 Sysam, Angelo Dureghello <angelo@sysam.it>
5
6#include <linux/dmapool.h>
7#include <linux/module.h>
8#include <linux/slab.h>
Laurentiu Tudor0fa89f92019-01-18 12:06:23 +02009#include <linux/dma-mapping.h>
Angelo Dureghello9d831522018-08-19 19:27:13 +020010
11#include "fsl-edma-common.h"
12
13#define EDMA_CR 0x00
14#define EDMA_ES 0x04
15#define EDMA_ERQ 0x0C
16#define EDMA_EEI 0x14
17#define EDMA_SERQ 0x1B
18#define EDMA_CERQ 0x1A
19#define EDMA_SEEI 0x19
20#define EDMA_CEEI 0x18
21#define EDMA_CINT 0x1F
22#define EDMA_CERR 0x1E
23#define EDMA_SSRT 0x1D
24#define EDMA_CDNE 0x1C
25#define EDMA_INTR 0x24
26#define EDMA_ERR 0x2C
27
28#define EDMA64_ERQH 0x08
29#define EDMA64_EEIH 0x10
30#define EDMA64_SERQ 0x18
31#define EDMA64_CERQ 0x19
32#define EDMA64_SEEI 0x1a
33#define EDMA64_CEEI 0x1b
34#define EDMA64_CINT 0x1c
35#define EDMA64_CERR 0x1d
36#define EDMA64_SSRT 0x1e
37#define EDMA64_CDNE 0x1f
38#define EDMA64_INTH 0x20
39#define EDMA64_INTL 0x24
40#define EDMA64_ERRH 0x28
41#define EDMA64_ERRL 0x2c
42
43#define EDMA_TCD 0x1000
44
45static void fsl_edma_enable_request(struct fsl_edma_chan *fsl_chan)
46{
Angelo Dureghello377eaf32018-08-19 19:27:14 +020047 struct edma_regs *regs = &fsl_chan->edma->regs;
Angelo Dureghello9d831522018-08-19 19:27:13 +020048 u32 ch = fsl_chan->vchan.chan.chan_id;
49
Robin Gongaf802722019-06-25 17:43:19 +080050 if (fsl_chan->edma->drvdata->version == v1) {
Angelo Dureghelloe7a3ff92018-08-19 19:27:16 +020051 edma_writeb(fsl_chan->edma, EDMA_SEEI_SEEI(ch), regs->seei);
52 edma_writeb(fsl_chan->edma, ch, regs->serq);
53 } else {
54 /* ColdFire is big endian, and accesses natively
55 * big endian I/O peripherals
56 */
57 iowrite8(EDMA_SEEI_SEEI(ch), regs->seei);
58 iowrite8(ch, regs->serq);
59 }
Angelo Dureghello9d831522018-08-19 19:27:13 +020060}
61
62void fsl_edma_disable_request(struct fsl_edma_chan *fsl_chan)
63{
Angelo Dureghello377eaf32018-08-19 19:27:14 +020064 struct edma_regs *regs = &fsl_chan->edma->regs;
Angelo Dureghello9d831522018-08-19 19:27:13 +020065 u32 ch = fsl_chan->vchan.chan.chan_id;
66
Robin Gongaf802722019-06-25 17:43:19 +080067 if (fsl_chan->edma->drvdata->version == v1) {
Angelo Dureghelloe7a3ff92018-08-19 19:27:16 +020068 edma_writeb(fsl_chan->edma, ch, regs->cerq);
69 edma_writeb(fsl_chan->edma, EDMA_CEEI_CEEI(ch), regs->ceei);
70 } else {
71 /* ColdFire is big endian, and accesses natively
72 * big endian I/O peripherals
73 */
74 iowrite8(ch, regs->cerq);
75 iowrite8(EDMA_CEEI_CEEI(ch), regs->ceei);
76 }
Angelo Dureghello9d831522018-08-19 19:27:13 +020077}
78EXPORT_SYMBOL_GPL(fsl_edma_disable_request);
79
Robin Gong78690bf2019-06-25 17:43:20 +080080static void mux_configure8(struct fsl_edma_chan *fsl_chan, void __iomem *addr,
81 u32 off, u32 slot, bool enable)
82{
83 u8 val8;
84
85 if (enable)
86 val8 = EDMAMUX_CHCFG_ENBL | slot;
87 else
88 val8 = EDMAMUX_CHCFG_DIS;
89
90 iowrite8(val8, addr + off);
91}
92
Angelo Dureghello9d831522018-08-19 19:27:13 +020093void fsl_edma_chan_mux(struct fsl_edma_chan *fsl_chan,
94 unsigned int slot, bool enable)
95{
96 u32 ch = fsl_chan->vchan.chan.chan_id;
97 void __iomem *muxaddr;
98 unsigned int chans_per_mux, ch_off;
Robin Gongaf802722019-06-25 17:43:19 +080099 u32 dmamux_nr = fsl_chan->edma->drvdata->dmamuxs;
Angelo Dureghello9d831522018-08-19 19:27:13 +0200100
Robin Gongaf802722019-06-25 17:43:19 +0800101 chans_per_mux = fsl_chan->edma->n_chans / dmamux_nr;
Angelo Dureghello9d831522018-08-19 19:27:13 +0200102 ch_off = fsl_chan->vchan.chan.chan_id % chans_per_mux;
103 muxaddr = fsl_chan->edma->muxbase[ch / chans_per_mux];
104 slot = EDMAMUX_CHCFG_SOURCE(slot);
105
Robin Gong78690bf2019-06-25 17:43:20 +0800106 mux_configure8(fsl_chan, muxaddr, ch_off, slot, enable);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200107}
108EXPORT_SYMBOL_GPL(fsl_edma_chan_mux);
109
110static unsigned int fsl_edma_get_tcd_attr(enum dma_slave_buswidth addr_width)
111{
112 switch (addr_width) {
113 case 1:
114 return EDMA_TCD_ATTR_SSIZE_8BIT | EDMA_TCD_ATTR_DSIZE_8BIT;
115 case 2:
116 return EDMA_TCD_ATTR_SSIZE_16BIT | EDMA_TCD_ATTR_DSIZE_16BIT;
117 case 4:
118 return EDMA_TCD_ATTR_SSIZE_32BIT | EDMA_TCD_ATTR_DSIZE_32BIT;
119 case 8:
120 return EDMA_TCD_ATTR_SSIZE_64BIT | EDMA_TCD_ATTR_DSIZE_64BIT;
121 default:
122 return EDMA_TCD_ATTR_SSIZE_32BIT | EDMA_TCD_ATTR_DSIZE_32BIT;
123 }
124}
125
126void fsl_edma_free_desc(struct virt_dma_desc *vdesc)
127{
128 struct fsl_edma_desc *fsl_desc;
129 int i;
130
131 fsl_desc = to_fsl_edma_desc(vdesc);
132 for (i = 0; i < fsl_desc->n_tcds; i++)
133 dma_pool_free(fsl_desc->echan->tcd_pool, fsl_desc->tcd[i].vtcd,
134 fsl_desc->tcd[i].ptcd);
135 kfree(fsl_desc);
136}
137EXPORT_SYMBOL_GPL(fsl_edma_free_desc);
138
139int fsl_edma_terminate_all(struct dma_chan *chan)
140{
141 struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
142 unsigned long flags;
143 LIST_HEAD(head);
144
145 spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
146 fsl_edma_disable_request(fsl_chan);
147 fsl_chan->edesc = NULL;
148 fsl_chan->idle = true;
149 vchan_get_all_descriptors(&fsl_chan->vchan, &head);
150 spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
151 vchan_dma_desc_free_list(&fsl_chan->vchan, &head);
152 return 0;
153}
154EXPORT_SYMBOL_GPL(fsl_edma_terminate_all);
155
156int fsl_edma_pause(struct dma_chan *chan)
157{
158 struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
159 unsigned long flags;
160
161 spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
162 if (fsl_chan->edesc) {
163 fsl_edma_disable_request(fsl_chan);
164 fsl_chan->status = DMA_PAUSED;
165 fsl_chan->idle = true;
166 }
167 spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
168 return 0;
169}
170EXPORT_SYMBOL_GPL(fsl_edma_pause);
171
172int fsl_edma_resume(struct dma_chan *chan)
173{
174 struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
175 unsigned long flags;
176
177 spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
178 if (fsl_chan->edesc) {
179 fsl_edma_enable_request(fsl_chan);
180 fsl_chan->status = DMA_IN_PROGRESS;
181 fsl_chan->idle = false;
182 }
183 spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
184 return 0;
185}
186EXPORT_SYMBOL_GPL(fsl_edma_resume);
187
Laurentiu Tudor0fa89f92019-01-18 12:06:23 +0200188static void fsl_edma_unprep_slave_dma(struct fsl_edma_chan *fsl_chan)
189{
190 if (fsl_chan->dma_dir != DMA_NONE)
191 dma_unmap_resource(fsl_chan->vchan.chan.device->dev,
192 fsl_chan->dma_dev_addr,
193 fsl_chan->dma_dev_size,
194 fsl_chan->dma_dir, 0);
195 fsl_chan->dma_dir = DMA_NONE;
196}
197
198static bool fsl_edma_prep_slave_dma(struct fsl_edma_chan *fsl_chan,
199 enum dma_transfer_direction dir)
200{
201 struct device *dev = fsl_chan->vchan.chan.device->dev;
202 enum dma_data_direction dma_dir;
203 phys_addr_t addr = 0;
204 u32 size = 0;
205
206 switch (dir) {
207 case DMA_MEM_TO_DEV:
208 dma_dir = DMA_FROM_DEVICE;
209 addr = fsl_chan->cfg.dst_addr;
210 size = fsl_chan->cfg.dst_maxburst;
211 break;
212 case DMA_DEV_TO_MEM:
213 dma_dir = DMA_TO_DEVICE;
214 addr = fsl_chan->cfg.src_addr;
215 size = fsl_chan->cfg.src_maxburst;
216 break;
217 default:
218 dma_dir = DMA_NONE;
219 break;
220 }
221
222 /* Already mapped for this config? */
223 if (fsl_chan->dma_dir == dma_dir)
224 return true;
225
226 fsl_edma_unprep_slave_dma(fsl_chan);
227
228 fsl_chan->dma_dev_addr = dma_map_resource(dev, addr, size, dma_dir, 0);
229 if (dma_mapping_error(dev, fsl_chan->dma_dev_addr))
230 return false;
231 fsl_chan->dma_dev_size = size;
232 fsl_chan->dma_dir = dma_dir;
233
234 return true;
235}
236
Angelo Dureghello9d831522018-08-19 19:27:13 +0200237int fsl_edma_slave_config(struct dma_chan *chan,
238 struct dma_slave_config *cfg)
239{
240 struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
241
Vinod Koul0e819e352018-10-07 19:42:56 +0530242 memcpy(&fsl_chan->cfg, cfg, sizeof(*cfg));
Laurentiu Tudor0fa89f92019-01-18 12:06:23 +0200243 fsl_edma_unprep_slave_dma(fsl_chan);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200244
245 return 0;
246}
247EXPORT_SYMBOL_GPL(fsl_edma_slave_config);
248
249static size_t fsl_edma_desc_residue(struct fsl_edma_chan *fsl_chan,
250 struct virt_dma_desc *vdesc, bool in_progress)
251{
252 struct fsl_edma_desc *edesc = fsl_chan->edesc;
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200253 struct edma_regs *regs = &fsl_chan->edma->regs;
Angelo Dureghello9d831522018-08-19 19:27:13 +0200254 u32 ch = fsl_chan->vchan.chan.chan_id;
Vinod Koul0e819e352018-10-07 19:42:56 +0530255 enum dma_transfer_direction dir = edesc->dirn;
Angelo Dureghello9d831522018-08-19 19:27:13 +0200256 dma_addr_t cur_addr, dma_addr;
257 size_t len, size;
258 int i;
259
260 /* calculate the total size in this desc */
261 for (len = i = 0; i < fsl_chan->edesc->n_tcds; i++)
262 len += le32_to_cpu(edesc->tcd[i].vtcd->nbytes)
263 * le16_to_cpu(edesc->tcd[i].vtcd->biter);
264
265 if (!in_progress)
266 return len;
267
268 if (dir == DMA_MEM_TO_DEV)
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200269 cur_addr = edma_readl(fsl_chan->edma, &regs->tcd[ch].saddr);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200270 else
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200271 cur_addr = edma_readl(fsl_chan->edma, &regs->tcd[ch].daddr);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200272
273 /* figure out the finished and calculate the residue */
274 for (i = 0; i < fsl_chan->edesc->n_tcds; i++) {
275 size = le32_to_cpu(edesc->tcd[i].vtcd->nbytes)
276 * le16_to_cpu(edesc->tcd[i].vtcd->biter);
277 if (dir == DMA_MEM_TO_DEV)
278 dma_addr = le32_to_cpu(edesc->tcd[i].vtcd->saddr);
279 else
280 dma_addr = le32_to_cpu(edesc->tcd[i].vtcd->daddr);
281
282 len -= size;
283 if (cur_addr >= dma_addr && cur_addr < dma_addr + size) {
284 len += dma_addr + size - cur_addr;
285 break;
286 }
287 }
288
289 return len;
290}
291
292enum dma_status fsl_edma_tx_status(struct dma_chan *chan,
293 dma_cookie_t cookie, struct dma_tx_state *txstate)
294{
295 struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
296 struct virt_dma_desc *vdesc;
297 enum dma_status status;
298 unsigned long flags;
299
300 status = dma_cookie_status(chan, cookie, txstate);
301 if (status == DMA_COMPLETE)
302 return status;
303
304 if (!txstate)
305 return fsl_chan->status;
306
307 spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
308 vdesc = vchan_find_desc(&fsl_chan->vchan, cookie);
309 if (fsl_chan->edesc && cookie == fsl_chan->edesc->vdesc.tx.cookie)
310 txstate->residue =
311 fsl_edma_desc_residue(fsl_chan, vdesc, true);
312 else if (vdesc)
313 txstate->residue =
314 fsl_edma_desc_residue(fsl_chan, vdesc, false);
315 else
316 txstate->residue = 0;
317
318 spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
319
320 return fsl_chan->status;
321}
322EXPORT_SYMBOL_GPL(fsl_edma_tx_status);
323
324static void fsl_edma_set_tcd_regs(struct fsl_edma_chan *fsl_chan,
325 struct fsl_edma_hw_tcd *tcd)
326{
327 struct fsl_edma_engine *edma = fsl_chan->edma;
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200328 struct edma_regs *regs = &fsl_chan->edma->regs;
Angelo Dureghello9d831522018-08-19 19:27:13 +0200329 u32 ch = fsl_chan->vchan.chan.chan_id;
330
331 /*
332 * TCD parameters are stored in struct fsl_edma_hw_tcd in little
333 * endian format. However, we need to load the TCD registers in
334 * big- or little-endian obeying the eDMA engine model endian.
335 */
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200336 edma_writew(edma, 0, &regs->tcd[ch].csr);
337 edma_writel(edma, le32_to_cpu(tcd->saddr), &regs->tcd[ch].saddr);
338 edma_writel(edma, le32_to_cpu(tcd->daddr), &regs->tcd[ch].daddr);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200339
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200340 edma_writew(edma, le16_to_cpu(tcd->attr), &regs->tcd[ch].attr);
341 edma_writew(edma, le16_to_cpu(tcd->soff), &regs->tcd[ch].soff);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200342
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200343 edma_writel(edma, le32_to_cpu(tcd->nbytes), &regs->tcd[ch].nbytes);
344 edma_writel(edma, le32_to_cpu(tcd->slast), &regs->tcd[ch].slast);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200345
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200346 edma_writew(edma, le16_to_cpu(tcd->citer), &regs->tcd[ch].citer);
347 edma_writew(edma, le16_to_cpu(tcd->biter), &regs->tcd[ch].biter);
348 edma_writew(edma, le16_to_cpu(tcd->doff), &regs->tcd[ch].doff);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200349
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200350 edma_writel(edma, le32_to_cpu(tcd->dlast_sga),
351 &regs->tcd[ch].dlast_sga);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200352
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200353 edma_writew(edma, le16_to_cpu(tcd->csr), &regs->tcd[ch].csr);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200354}
355
356static inline
357void fsl_edma_fill_tcd(struct fsl_edma_hw_tcd *tcd, u32 src, u32 dst,
358 u16 attr, u16 soff, u32 nbytes, u32 slast, u16 citer,
359 u16 biter, u16 doff, u32 dlast_sga, bool major_int,
360 bool disable_req, bool enable_sg)
361{
362 u16 csr = 0;
363
364 /*
365 * eDMA hardware SGs require the TCDs to be stored in little
366 * endian format irrespective of the register endian model.
367 * So we put the value in little endian in memory, waiting
368 * for fsl_edma_set_tcd_regs doing the swap.
369 */
370 tcd->saddr = cpu_to_le32(src);
371 tcd->daddr = cpu_to_le32(dst);
372
373 tcd->attr = cpu_to_le16(attr);
374
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200375 tcd->soff = cpu_to_le16(soff);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200376
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200377 tcd->nbytes = cpu_to_le32(nbytes);
378 tcd->slast = cpu_to_le32(slast);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200379
380 tcd->citer = cpu_to_le16(EDMA_TCD_CITER_CITER(citer));
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200381 tcd->doff = cpu_to_le16(doff);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200382
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200383 tcd->dlast_sga = cpu_to_le32(dlast_sga);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200384
385 tcd->biter = cpu_to_le16(EDMA_TCD_BITER_BITER(biter));
386 if (major_int)
387 csr |= EDMA_TCD_CSR_INT_MAJOR;
388
389 if (disable_req)
390 csr |= EDMA_TCD_CSR_D_REQ;
391
392 if (enable_sg)
393 csr |= EDMA_TCD_CSR_E_SG;
394
395 tcd->csr = cpu_to_le16(csr);
396}
397
398static struct fsl_edma_desc *fsl_edma_alloc_desc(struct fsl_edma_chan *fsl_chan,
399 int sg_len)
400{
401 struct fsl_edma_desc *fsl_desc;
402 int i;
403
Gustavo A. R. Silvade1fa4f2019-01-04 15:25:45 -0600404 fsl_desc = kzalloc(struct_size(fsl_desc, tcd, sg_len), GFP_NOWAIT);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200405 if (!fsl_desc)
406 return NULL;
407
408 fsl_desc->echan = fsl_chan;
409 fsl_desc->n_tcds = sg_len;
410 for (i = 0; i < sg_len; i++) {
411 fsl_desc->tcd[i].vtcd = dma_pool_alloc(fsl_chan->tcd_pool,
412 GFP_NOWAIT, &fsl_desc->tcd[i].ptcd);
413 if (!fsl_desc->tcd[i].vtcd)
414 goto err;
415 }
416 return fsl_desc;
417
418err:
419 while (--i >= 0)
420 dma_pool_free(fsl_chan->tcd_pool, fsl_desc->tcd[i].vtcd,
421 fsl_desc->tcd[i].ptcd);
422 kfree(fsl_desc);
423 return NULL;
424}
425
426struct dma_async_tx_descriptor *fsl_edma_prep_dma_cyclic(
427 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
428 size_t period_len, enum dma_transfer_direction direction,
429 unsigned long flags)
430{
431 struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
432 struct fsl_edma_desc *fsl_desc;
433 dma_addr_t dma_buf_next;
434 int sg_len, i;
435 u32 src_addr, dst_addr, last_sg, nbytes;
436 u16 soff, doff, iter;
437
Vinod Koul0e819e352018-10-07 19:42:56 +0530438 if (!is_slave_direction(direction))
Angelo Dureghello9d831522018-08-19 19:27:13 +0200439 return NULL;
440
Laurentiu Tudor0fa89f92019-01-18 12:06:23 +0200441 if (!fsl_edma_prep_slave_dma(fsl_chan, direction))
442 return NULL;
443
Angelo Dureghello9d831522018-08-19 19:27:13 +0200444 sg_len = buf_len / period_len;
445 fsl_desc = fsl_edma_alloc_desc(fsl_chan, sg_len);
446 if (!fsl_desc)
447 return NULL;
448 fsl_desc->iscyclic = true;
Vinod Koul0e819e352018-10-07 19:42:56 +0530449 fsl_desc->dirn = direction;
Angelo Dureghello9d831522018-08-19 19:27:13 +0200450
451 dma_buf_next = dma_addr;
Vinod Koul0e819e352018-10-07 19:42:56 +0530452 if (direction == DMA_MEM_TO_DEV) {
453 fsl_chan->attr =
454 fsl_edma_get_tcd_attr(fsl_chan->cfg.dst_addr_width);
455 nbytes = fsl_chan->cfg.dst_addr_width *
456 fsl_chan->cfg.dst_maxburst;
457 } else {
458 fsl_chan->attr =
459 fsl_edma_get_tcd_attr(fsl_chan->cfg.src_addr_width);
460 nbytes = fsl_chan->cfg.src_addr_width *
461 fsl_chan->cfg.src_maxburst;
462 }
463
Angelo Dureghello9d831522018-08-19 19:27:13 +0200464 iter = period_len / nbytes;
465
466 for (i = 0; i < sg_len; i++) {
467 if (dma_buf_next >= dma_addr + buf_len)
468 dma_buf_next = dma_addr;
469
470 /* get next sg's physical address */
471 last_sg = fsl_desc->tcd[(i + 1) % sg_len].ptcd;
472
Vinod Koul0e819e352018-10-07 19:42:56 +0530473 if (direction == DMA_MEM_TO_DEV) {
Angelo Dureghello9d831522018-08-19 19:27:13 +0200474 src_addr = dma_buf_next;
Laurentiu Tudor0fa89f92019-01-18 12:06:23 +0200475 dst_addr = fsl_chan->dma_dev_addr;
Vinod Koul0e819e352018-10-07 19:42:56 +0530476 soff = fsl_chan->cfg.dst_addr_width;
Angelo Dureghello9d831522018-08-19 19:27:13 +0200477 doff = 0;
478 } else {
Laurentiu Tudor0fa89f92019-01-18 12:06:23 +0200479 src_addr = fsl_chan->dma_dev_addr;
Angelo Dureghello9d831522018-08-19 19:27:13 +0200480 dst_addr = dma_buf_next;
481 soff = 0;
Vinod Koul0e819e352018-10-07 19:42:56 +0530482 doff = fsl_chan->cfg.src_addr_width;
Angelo Dureghello9d831522018-08-19 19:27:13 +0200483 }
484
485 fsl_edma_fill_tcd(fsl_desc->tcd[i].vtcd, src_addr, dst_addr,
Vinod Koul0e819e352018-10-07 19:42:56 +0530486 fsl_chan->attr, soff, nbytes, 0, iter,
Angelo Dureghello9d831522018-08-19 19:27:13 +0200487 iter, doff, last_sg, true, false, true);
488 dma_buf_next += period_len;
489 }
490
491 return vchan_tx_prep(&fsl_chan->vchan, &fsl_desc->vdesc, flags);
492}
493EXPORT_SYMBOL_GPL(fsl_edma_prep_dma_cyclic);
494
495struct dma_async_tx_descriptor *fsl_edma_prep_slave_sg(
496 struct dma_chan *chan, struct scatterlist *sgl,
497 unsigned int sg_len, enum dma_transfer_direction direction,
498 unsigned long flags, void *context)
499{
500 struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
501 struct fsl_edma_desc *fsl_desc;
502 struct scatterlist *sg;
503 u32 src_addr, dst_addr, last_sg, nbytes;
504 u16 soff, doff, iter;
505 int i;
506
Vinod Koul0e819e352018-10-07 19:42:56 +0530507 if (!is_slave_direction(direction))
Angelo Dureghello9d831522018-08-19 19:27:13 +0200508 return NULL;
509
Laurentiu Tudor0fa89f92019-01-18 12:06:23 +0200510 if (!fsl_edma_prep_slave_dma(fsl_chan, direction))
511 return NULL;
512
Angelo Dureghello9d831522018-08-19 19:27:13 +0200513 fsl_desc = fsl_edma_alloc_desc(fsl_chan, sg_len);
514 if (!fsl_desc)
515 return NULL;
516 fsl_desc->iscyclic = false;
Vinod Koul0e819e352018-10-07 19:42:56 +0530517 fsl_desc->dirn = direction;
Angelo Dureghello9d831522018-08-19 19:27:13 +0200518
Vinod Koul0e819e352018-10-07 19:42:56 +0530519 if (direction == DMA_MEM_TO_DEV) {
520 fsl_chan->attr =
521 fsl_edma_get_tcd_attr(fsl_chan->cfg.dst_addr_width);
522 nbytes = fsl_chan->cfg.dst_addr_width *
523 fsl_chan->cfg.dst_maxburst;
524 } else {
525 fsl_chan->attr =
526 fsl_edma_get_tcd_attr(fsl_chan->cfg.src_addr_width);
527 nbytes = fsl_chan->cfg.src_addr_width *
528 fsl_chan->cfg.src_maxburst;
529 }
530
Angelo Dureghello9d831522018-08-19 19:27:13 +0200531 for_each_sg(sgl, sg, sg_len, i) {
532 /* get next sg's physical address */
533 last_sg = fsl_desc->tcd[(i + 1) % sg_len].ptcd;
534
Vinod Koul0e819e352018-10-07 19:42:56 +0530535 if (direction == DMA_MEM_TO_DEV) {
Angelo Dureghello9d831522018-08-19 19:27:13 +0200536 src_addr = sg_dma_address(sg);
Laurentiu Tudor0fa89f92019-01-18 12:06:23 +0200537 dst_addr = fsl_chan->dma_dev_addr;
Vinod Koul0e819e352018-10-07 19:42:56 +0530538 soff = fsl_chan->cfg.dst_addr_width;
Angelo Dureghello9d831522018-08-19 19:27:13 +0200539 doff = 0;
540 } else {
Laurentiu Tudor0fa89f92019-01-18 12:06:23 +0200541 src_addr = fsl_chan->dma_dev_addr;
Angelo Dureghello9d831522018-08-19 19:27:13 +0200542 dst_addr = sg_dma_address(sg);
543 soff = 0;
Vinod Koul0e819e352018-10-07 19:42:56 +0530544 doff = fsl_chan->cfg.src_addr_width;
Angelo Dureghello9d831522018-08-19 19:27:13 +0200545 }
546
547 iter = sg_dma_len(sg) / nbytes;
548 if (i < sg_len - 1) {
549 last_sg = fsl_desc->tcd[(i + 1)].ptcd;
550 fsl_edma_fill_tcd(fsl_desc->tcd[i].vtcd, src_addr,
Vinod Koul0e819e352018-10-07 19:42:56 +0530551 dst_addr, fsl_chan->attr, soff,
Angelo Dureghello9d831522018-08-19 19:27:13 +0200552 nbytes, 0, iter, iter, doff, last_sg,
553 false, false, true);
554 } else {
555 last_sg = 0;
556 fsl_edma_fill_tcd(fsl_desc->tcd[i].vtcd, src_addr,
Vinod Koul0e819e352018-10-07 19:42:56 +0530557 dst_addr, fsl_chan->attr, soff,
Angelo Dureghello9d831522018-08-19 19:27:13 +0200558 nbytes, 0, iter, iter, doff, last_sg,
559 true, true, false);
560 }
561 }
562
563 return vchan_tx_prep(&fsl_chan->vchan, &fsl_desc->vdesc, flags);
564}
565EXPORT_SYMBOL_GPL(fsl_edma_prep_slave_sg);
566
567void fsl_edma_xfer_desc(struct fsl_edma_chan *fsl_chan)
568{
569 struct virt_dma_desc *vdesc;
570
571 vdesc = vchan_next_desc(&fsl_chan->vchan);
572 if (!vdesc)
573 return;
574 fsl_chan->edesc = to_fsl_edma_desc(vdesc);
575 fsl_edma_set_tcd_regs(fsl_chan, fsl_chan->edesc->tcd[0].vtcd);
576 fsl_edma_enable_request(fsl_chan);
577 fsl_chan->status = DMA_IN_PROGRESS;
578 fsl_chan->idle = false;
579}
580EXPORT_SYMBOL_GPL(fsl_edma_xfer_desc);
581
582void fsl_edma_issue_pending(struct dma_chan *chan)
583{
584 struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
585 unsigned long flags;
586
587 spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
588
589 if (unlikely(fsl_chan->pm_state != RUNNING)) {
590 spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
591 /* cannot submit due to suspend */
592 return;
593 }
594
595 if (vchan_issue_pending(&fsl_chan->vchan) && !fsl_chan->edesc)
596 fsl_edma_xfer_desc(fsl_chan);
597
598 spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
599}
600EXPORT_SYMBOL_GPL(fsl_edma_issue_pending);
601
602int fsl_edma_alloc_chan_resources(struct dma_chan *chan)
603{
604 struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
605
606 fsl_chan->tcd_pool = dma_pool_create("tcd_pool", chan->device->dev,
607 sizeof(struct fsl_edma_hw_tcd),
608 32, 0);
609 return 0;
610}
611EXPORT_SYMBOL_GPL(fsl_edma_alloc_chan_resources);
612
613void fsl_edma_free_chan_resources(struct dma_chan *chan)
614{
615 struct fsl_edma_chan *fsl_chan = to_fsl_edma_chan(chan);
616 unsigned long flags;
617 LIST_HEAD(head);
618
619 spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
620 fsl_edma_disable_request(fsl_chan);
621 fsl_edma_chan_mux(fsl_chan, 0, false);
622 fsl_chan->edesc = NULL;
623 vchan_get_all_descriptors(&fsl_chan->vchan, &head);
Laurentiu Tudor0fa89f92019-01-18 12:06:23 +0200624 fsl_edma_unprep_slave_dma(fsl_chan);
Angelo Dureghello9d831522018-08-19 19:27:13 +0200625 spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags);
626
627 vchan_dma_desc_free_list(&fsl_chan->vchan, &head);
628 dma_pool_destroy(fsl_chan->tcd_pool);
629 fsl_chan->tcd_pool = NULL;
630}
631EXPORT_SYMBOL_GPL(fsl_edma_free_chan_resources);
632
633void fsl_edma_cleanup_vchan(struct dma_device *dmadev)
634{
635 struct fsl_edma_chan *chan, *_chan;
636
637 list_for_each_entry_safe(chan, _chan,
638 &dmadev->channels, vchan.chan.device_node) {
639 list_del(&chan->vchan.chan.device_node);
640 tasklet_kill(&chan->vchan.task);
641 }
642}
643EXPORT_SYMBOL_GPL(fsl_edma_cleanup_vchan);
644
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200645/*
646 * On the 32 channels Vybrid/mpc577x edma version (here called "v1"),
647 * register offsets are different compared to ColdFire mcf5441x 64 channels
648 * edma (here called "v2").
649 *
650 * This function sets up register offsets as per proper declared version
651 * so must be called in xxx_edma_probe() just after setting the
652 * edma "version" and "membase" appropriately.
653 */
654void fsl_edma_setup_regs(struct fsl_edma_engine *edma)
655{
656 edma->regs.cr = edma->membase + EDMA_CR;
657 edma->regs.es = edma->membase + EDMA_ES;
658 edma->regs.erql = edma->membase + EDMA_ERQ;
659 edma->regs.eeil = edma->membase + EDMA_EEI;
660
Robin Gongaf802722019-06-25 17:43:19 +0800661 edma->regs.serq = edma->membase + ((edma->drvdata->version == v1) ?
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200662 EDMA_SERQ : EDMA64_SERQ);
Robin Gongaf802722019-06-25 17:43:19 +0800663 edma->regs.cerq = edma->membase + ((edma->drvdata->version == v1) ?
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200664 EDMA_CERQ : EDMA64_CERQ);
Robin Gongaf802722019-06-25 17:43:19 +0800665 edma->regs.seei = edma->membase + ((edma->drvdata->version == v1) ?
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200666 EDMA_SEEI : EDMA64_SEEI);
Robin Gongaf802722019-06-25 17:43:19 +0800667 edma->regs.ceei = edma->membase + ((edma->drvdata->version == v1) ?
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200668 EDMA_CEEI : EDMA64_CEEI);
Robin Gongaf802722019-06-25 17:43:19 +0800669 edma->regs.cint = edma->membase + ((edma->drvdata->version == v1) ?
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200670 EDMA_CINT : EDMA64_CINT);
Robin Gongaf802722019-06-25 17:43:19 +0800671 edma->regs.cerr = edma->membase + ((edma->drvdata->version == v1) ?
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200672 EDMA_CERR : EDMA64_CERR);
Robin Gongaf802722019-06-25 17:43:19 +0800673 edma->regs.ssrt = edma->membase + ((edma->drvdata->version == v1) ?
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200674 EDMA_SSRT : EDMA64_SSRT);
Robin Gongaf802722019-06-25 17:43:19 +0800675 edma->regs.cdne = edma->membase + ((edma->drvdata->version == v1) ?
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200676 EDMA_CDNE : EDMA64_CDNE);
Robin Gongaf802722019-06-25 17:43:19 +0800677 edma->regs.intl = edma->membase + ((edma->drvdata->version == v1) ?
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200678 EDMA_INTR : EDMA64_INTL);
Robin Gongaf802722019-06-25 17:43:19 +0800679 edma->regs.errl = edma->membase + ((edma->drvdata->version == v1) ?
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200680 EDMA_ERR : EDMA64_ERRL);
681
Robin Gongaf802722019-06-25 17:43:19 +0800682 if (edma->drvdata->version == v2) {
Angelo Dureghello377eaf32018-08-19 19:27:14 +0200683 edma->regs.erqh = edma->membase + EDMA64_ERQH;
684 edma->regs.eeih = edma->membase + EDMA64_EEIH;
685 edma->regs.errh = edma->membase + EDMA64_ERRH;
686 edma->regs.inth = edma->membase + EDMA64_INTH;
687 }
688
689 edma->regs.tcd = edma->membase + EDMA_TCD;
690}
691EXPORT_SYMBOL_GPL(fsl_edma_setup_regs);
692
Angelo Dureghello9d831522018-08-19 19:27:13 +0200693MODULE_LICENSE("GPL v2");