]>
Commit | Line | Data |
---|---|---|
c2dde5f8 MP |
1 | /* |
2 | * TI EDMA DMA engine driver | |
3 | * | |
4 | * Copyright 2012 Texas Instruments | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License as | |
8 | * published by the Free Software Foundation version 2. | |
9 | * | |
10 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any | |
11 | * kind, whether express or implied; without even the implied warranty | |
12 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | */ | |
15 | ||
16 | #include <linux/dmaengine.h> | |
17 | #include <linux/dma-mapping.h> | |
18 | #include <linux/err.h> | |
19 | #include <linux/init.h> | |
20 | #include <linux/interrupt.h> | |
21 | #include <linux/list.h> | |
22 | #include <linux/module.h> | |
23 | #include <linux/platform_device.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/spinlock.h> | |
26 | ||
3ad7a42d | 27 | #include <linux/platform_data/edma.h> |
c2dde5f8 MP |
28 | |
29 | #include "dmaengine.h" | |
30 | #include "virt-dma.h" | |
31 | ||
32 | /* | |
33 | * This will go away when the private EDMA API is folded | |
34 | * into this driver and the platform device(s) are | |
35 | * instantiated in the arch code. We can only get away | |
36 | * with this simplification because DA8XX may not be built | |
37 | * in the same kernel image with other DaVinci parts. This | |
38 | * avoids having to sprinkle dmaengine driver platform devices | |
39 | * and data throughout all the existing board files. | |
40 | */ | |
41 | #ifdef CONFIG_ARCH_DAVINCI_DA8XX | |
42 | #define EDMA_CTLRS 2 | |
43 | #define EDMA_CHANS 32 | |
44 | #else | |
45 | #define EDMA_CTLRS 1 | |
46 | #define EDMA_CHANS 64 | |
47 | #endif /* CONFIG_ARCH_DAVINCI_DA8XX */ | |
48 | ||
49 | /* Max of 16 segments per channel to conserve PaRAM slots */ | |
50 | #define MAX_NR_SG 16 | |
51 | #define EDMA_MAX_SLOTS MAX_NR_SG | |
52 | #define EDMA_DESCRIPTORS 16 | |
53 | ||
54 | struct edma_desc { | |
55 | struct virt_dma_desc vdesc; | |
56 | struct list_head node; | |
57 | int absync; | |
58 | int pset_nr; | |
53407062 | 59 | int processed; |
c2dde5f8 MP |
60 | struct edmacc_param pset[0]; |
61 | }; | |
62 | ||
63 | struct edma_cc; | |
64 | ||
65 | struct edma_chan { | |
66 | struct virt_dma_chan vchan; | |
67 | struct list_head node; | |
68 | struct edma_desc *edesc; | |
69 | struct edma_cc *ecc; | |
70 | int ch_num; | |
71 | bool alloced; | |
72 | int slot[EDMA_MAX_SLOTS]; | |
c5f47990 | 73 | int missed; |
661f7cb5 | 74 | struct dma_slave_config cfg; |
c2dde5f8 MP |
75 | }; |
76 | ||
77 | struct edma_cc { | |
78 | int ctlr; | |
79 | struct dma_device dma_slave; | |
80 | struct edma_chan slave_chans[EDMA_CHANS]; | |
81 | int num_slave_chans; | |
82 | int dummy_slot; | |
83 | }; | |
84 | ||
85 | static inline struct edma_cc *to_edma_cc(struct dma_device *d) | |
86 | { | |
87 | return container_of(d, struct edma_cc, dma_slave); | |
88 | } | |
89 | ||
90 | static inline struct edma_chan *to_edma_chan(struct dma_chan *c) | |
91 | { | |
92 | return container_of(c, struct edma_chan, vchan.chan); | |
93 | } | |
94 | ||
95 | static inline struct edma_desc | |
96 | *to_edma_desc(struct dma_async_tx_descriptor *tx) | |
97 | { | |
98 | return container_of(tx, struct edma_desc, vdesc.tx); | |
99 | } | |
100 | ||
101 | static void edma_desc_free(struct virt_dma_desc *vdesc) | |
102 | { | |
103 | kfree(container_of(vdesc, struct edma_desc, vdesc)); | |
104 | } | |
105 | ||
106 | /* Dispatch a queued descriptor to the controller (caller holds lock) */ | |
107 | static void edma_execute(struct edma_chan *echan) | |
108 | { | |
53407062 | 109 | struct virt_dma_desc *vdesc; |
c2dde5f8 | 110 | struct edma_desc *edesc; |
53407062 JF |
111 | struct device *dev = echan->vchan.chan.device->dev; |
112 | int i, j, left, nslots; | |
113 | ||
114 | /* If either we processed all psets or we're still not started */ | |
115 | if (!echan->edesc || | |
116 | echan->edesc->pset_nr == echan->edesc->processed) { | |
117 | /* Get next vdesc */ | |
118 | vdesc = vchan_next_desc(&echan->vchan); | |
119 | if (!vdesc) { | |
120 | echan->edesc = NULL; | |
121 | return; | |
122 | } | |
123 | list_del(&vdesc->node); | |
124 | echan->edesc = to_edma_desc(&vdesc->tx); | |
c2dde5f8 MP |
125 | } |
126 | ||
53407062 | 127 | edesc = echan->edesc; |
c2dde5f8 | 128 | |
53407062 JF |
129 | /* Find out how many left */ |
130 | left = edesc->pset_nr - edesc->processed; | |
131 | nslots = min(MAX_NR_SG, left); | |
c2dde5f8 MP |
132 | |
133 | /* Write descriptor PaRAM set(s) */ | |
53407062 JF |
134 | for (i = 0; i < nslots; i++) { |
135 | j = i + edesc->processed; | |
136 | edma_write_slot(echan->slot[i], &edesc->pset[j]); | |
c2dde5f8 MP |
137 | dev_dbg(echan->vchan.chan.device->dev, |
138 | "\n pset[%d]:\n" | |
139 | " chnum\t%d\n" | |
140 | " slot\t%d\n" | |
141 | " opt\t%08x\n" | |
142 | " src\t%08x\n" | |
143 | " dst\t%08x\n" | |
144 | " abcnt\t%08x\n" | |
145 | " ccnt\t%08x\n" | |
146 | " bidx\t%08x\n" | |
147 | " cidx\t%08x\n" | |
148 | " lkrld\t%08x\n", | |
53407062 JF |
149 | j, echan->ch_num, echan->slot[i], |
150 | edesc->pset[j].opt, | |
151 | edesc->pset[j].src, | |
152 | edesc->pset[j].dst, | |
153 | edesc->pset[j].a_b_cnt, | |
154 | edesc->pset[j].ccnt, | |
155 | edesc->pset[j].src_dst_bidx, | |
156 | edesc->pset[j].src_dst_cidx, | |
157 | edesc->pset[j].link_bcntrld); | |
c2dde5f8 | 158 | /* Link to the previous slot if not the last set */ |
53407062 | 159 | if (i != (nslots - 1)) |
c2dde5f8 MP |
160 | edma_link(echan->slot[i], echan->slot[i+1]); |
161 | /* Final pset links to the dummy pset */ | |
162 | else | |
163 | edma_link(echan->slot[i], echan->ecc->dummy_slot); | |
164 | } | |
165 | ||
53407062 JF |
166 | edesc->processed += nslots; |
167 | ||
168 | edma_resume(echan->ch_num); | |
169 | ||
170 | if (edesc->processed <= MAX_NR_SG) { | |
171 | dev_dbg(dev, "first transfer starting %d\n", echan->ch_num); | |
172 | edma_start(echan->ch_num); | |
173 | } | |
c5f47990 JF |
174 | |
175 | /* | |
176 | * This happens due to setup times between intermediate transfers | |
177 | * in long SG lists which have to be broken up into transfers of | |
178 | * MAX_NR_SG | |
179 | */ | |
180 | if (echan->missed) { | |
181 | dev_dbg(dev, "missed event in execute detected\n"); | |
182 | edma_clean_channel(echan->ch_num); | |
183 | edma_stop(echan->ch_num); | |
184 | edma_start(echan->ch_num); | |
185 | edma_trigger_channel(echan->ch_num); | |
186 | echan->missed = 0; | |
187 | } | |
c2dde5f8 MP |
188 | } |
189 | ||
190 | static int edma_terminate_all(struct edma_chan *echan) | |
191 | { | |
192 | unsigned long flags; | |
193 | LIST_HEAD(head); | |
194 | ||
195 | spin_lock_irqsave(&echan->vchan.lock, flags); | |
196 | ||
197 | /* | |
198 | * Stop DMA activity: we assume the callback will not be called | |
199 | * after edma_dma() returns (even if it does, it will see | |
200 | * echan->edesc is NULL and exit.) | |
201 | */ | |
202 | if (echan->edesc) { | |
203 | echan->edesc = NULL; | |
204 | edma_stop(echan->ch_num); | |
205 | } | |
206 | ||
207 | vchan_get_all_descriptors(&echan->vchan, &head); | |
208 | spin_unlock_irqrestore(&echan->vchan.lock, flags); | |
209 | vchan_dma_desc_free_list(&echan->vchan, &head); | |
210 | ||
211 | return 0; | |
212 | } | |
213 | ||
c2dde5f8 | 214 | static int edma_slave_config(struct edma_chan *echan, |
661f7cb5 | 215 | struct dma_slave_config *cfg) |
c2dde5f8 | 216 | { |
661f7cb5 MP |
217 | if (cfg->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES || |
218 | cfg->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES) | |
c2dde5f8 MP |
219 | return -EINVAL; |
220 | ||
661f7cb5 | 221 | memcpy(&echan->cfg, cfg, sizeof(echan->cfg)); |
c2dde5f8 MP |
222 | |
223 | return 0; | |
224 | } | |
225 | ||
226 | static int edma_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, | |
227 | unsigned long arg) | |
228 | { | |
229 | int ret = 0; | |
230 | struct dma_slave_config *config; | |
231 | struct edma_chan *echan = to_edma_chan(chan); | |
232 | ||
233 | switch (cmd) { | |
234 | case DMA_TERMINATE_ALL: | |
235 | edma_terminate_all(echan); | |
236 | break; | |
237 | case DMA_SLAVE_CONFIG: | |
238 | config = (struct dma_slave_config *)arg; | |
239 | ret = edma_slave_config(echan, config); | |
240 | break; | |
241 | default: | |
242 | ret = -ENOSYS; | |
243 | } | |
244 | ||
245 | return ret; | |
246 | } | |
247 | ||
248 | static struct dma_async_tx_descriptor *edma_prep_slave_sg( | |
249 | struct dma_chan *chan, struct scatterlist *sgl, | |
250 | unsigned int sg_len, enum dma_transfer_direction direction, | |
251 | unsigned long tx_flags, void *context) | |
252 | { | |
253 | struct edma_chan *echan = to_edma_chan(chan); | |
254 | struct device *dev = chan->device->dev; | |
255 | struct edma_desc *edesc; | |
661f7cb5 MP |
256 | dma_addr_t dev_addr; |
257 | enum dma_slave_buswidth dev_width; | |
258 | u32 burst; | |
c2dde5f8 | 259 | struct scatterlist *sg; |
c2dde5f8 MP |
260 | int acnt, bcnt, ccnt, src, dst, cidx; |
261 | int src_bidx, dst_bidx, src_cidx, dst_cidx; | |
6fbe24da | 262 | int i, nslots; |
c2dde5f8 MP |
263 | |
264 | if (unlikely(!echan || !sgl || !sg_len)) | |
265 | return NULL; | |
266 | ||
661f7cb5 MP |
267 | if (direction == DMA_DEV_TO_MEM) { |
268 | dev_addr = echan->cfg.src_addr; | |
269 | dev_width = echan->cfg.src_addr_width; | |
270 | burst = echan->cfg.src_maxburst; | |
271 | } else if (direction == DMA_MEM_TO_DEV) { | |
272 | dev_addr = echan->cfg.dst_addr; | |
273 | dev_width = echan->cfg.dst_addr_width; | |
274 | burst = echan->cfg.dst_maxburst; | |
275 | } else { | |
276 | dev_err(dev, "%s: bad direction?\n", __func__); | |
277 | return NULL; | |
278 | } | |
279 | ||
280 | if (dev_width == DMA_SLAVE_BUSWIDTH_UNDEFINED) { | |
c2dde5f8 MP |
281 | dev_err(dev, "Undefined slave buswidth\n"); |
282 | return NULL; | |
283 | } | |
284 | ||
285 | if (sg_len > MAX_NR_SG) { | |
286 | dev_err(dev, "Exceeded max SG segments %d > %d\n", | |
287 | sg_len, MAX_NR_SG); | |
288 | return NULL; | |
289 | } | |
290 | ||
291 | edesc = kzalloc(sizeof(*edesc) + sg_len * | |
292 | sizeof(edesc->pset[0]), GFP_ATOMIC); | |
293 | if (!edesc) { | |
294 | dev_dbg(dev, "Failed to allocate a descriptor\n"); | |
295 | return NULL; | |
296 | } | |
297 | ||
298 | edesc->pset_nr = sg_len; | |
299 | ||
6fbe24da JF |
300 | /* Allocate a PaRAM slot, if needed */ |
301 | nslots = min_t(unsigned, MAX_NR_SG, sg_len); | |
302 | ||
303 | for (i = 0; i < nslots; i++) { | |
c2dde5f8 MP |
304 | if (echan->slot[i] < 0) { |
305 | echan->slot[i] = | |
306 | edma_alloc_slot(EDMA_CTLR(echan->ch_num), | |
307 | EDMA_SLOT_ANY); | |
308 | if (echan->slot[i] < 0) { | |
309 | dev_err(dev, "Failed to allocate slot\n"); | |
310 | return NULL; | |
311 | } | |
312 | } | |
6fbe24da JF |
313 | } |
314 | ||
315 | /* Configure PaRAM sets for each SG */ | |
316 | for_each_sg(sgl, sg, sg_len, i) { | |
c2dde5f8 | 317 | |
661f7cb5 | 318 | acnt = dev_width; |
c2dde5f8 MP |
319 | |
320 | /* | |
321 | * If the maxburst is equal to the fifo width, use | |
322 | * A-synced transfers. This allows for large contiguous | |
323 | * buffer transfers using only one PaRAM set. | |
324 | */ | |
661f7cb5 | 325 | if (burst == 1) { |
c2dde5f8 MP |
326 | edesc->absync = false; |
327 | ccnt = sg_dma_len(sg) / acnt / (SZ_64K - 1); | |
328 | bcnt = sg_dma_len(sg) / acnt - ccnt * (SZ_64K - 1); | |
329 | if (bcnt) | |
330 | ccnt++; | |
331 | else | |
332 | bcnt = SZ_64K - 1; | |
333 | cidx = acnt; | |
334 | /* | |
335 | * If maxburst is greater than the fifo address_width, | |
336 | * use AB-synced transfers where A count is the fifo | |
337 | * address_width and B count is the maxburst. In this | |
338 | * case, we are limited to transfers of C count frames | |
339 | * of (address_width * maxburst) where C count is limited | |
340 | * to SZ_64K-1. This places an upper bound on the length | |
341 | * of an SG segment that can be handled. | |
342 | */ | |
343 | } else { | |
344 | edesc->absync = true; | |
661f7cb5 | 345 | bcnt = burst; |
c2dde5f8 MP |
346 | ccnt = sg_dma_len(sg) / (acnt * bcnt); |
347 | if (ccnt > (SZ_64K - 1)) { | |
348 | dev_err(dev, "Exceeded max SG segment size\n"); | |
349 | return NULL; | |
350 | } | |
351 | cidx = acnt * bcnt; | |
352 | } | |
353 | ||
354 | if (direction == DMA_MEM_TO_DEV) { | |
355 | src = sg_dma_address(sg); | |
661f7cb5 | 356 | dst = dev_addr; |
c2dde5f8 MP |
357 | src_bidx = acnt; |
358 | src_cidx = cidx; | |
359 | dst_bidx = 0; | |
360 | dst_cidx = 0; | |
361 | } else { | |
661f7cb5 | 362 | src = dev_addr; |
c2dde5f8 MP |
363 | dst = sg_dma_address(sg); |
364 | src_bidx = 0; | |
365 | src_cidx = 0; | |
366 | dst_bidx = acnt; | |
367 | dst_cidx = cidx; | |
368 | } | |
369 | ||
370 | edesc->pset[i].opt = EDMA_TCC(EDMA_CHAN_SLOT(echan->ch_num)); | |
371 | /* Configure A or AB synchronized transfers */ | |
372 | if (edesc->absync) | |
373 | edesc->pset[i].opt |= SYNCDIM; | |
6fbe24da JF |
374 | |
375 | /* If this is the last in a current SG set of transactions, | |
376 | enable interrupts so that next set is processed */ | |
377 | if (!((i+1) % MAX_NR_SG)) | |
378 | edesc->pset[i].opt |= TCINTEN; | |
379 | ||
c2dde5f8 MP |
380 | /* If this is the last set, enable completion interrupt flag */ |
381 | if (i == sg_len - 1) | |
382 | edesc->pset[i].opt |= TCINTEN; | |
383 | ||
384 | edesc->pset[i].src = src; | |
385 | edesc->pset[i].dst = dst; | |
386 | ||
387 | edesc->pset[i].src_dst_bidx = (dst_bidx << 16) | src_bidx; | |
388 | edesc->pset[i].src_dst_cidx = (dst_cidx << 16) | src_cidx; | |
389 | ||
390 | edesc->pset[i].a_b_cnt = bcnt << 16 | acnt; | |
391 | edesc->pset[i].ccnt = ccnt; | |
392 | edesc->pset[i].link_bcntrld = 0xffffffff; | |
393 | ||
394 | } | |
395 | ||
396 | return vchan_tx_prep(&echan->vchan, &edesc->vdesc, tx_flags); | |
397 | } | |
398 | ||
399 | static void edma_callback(unsigned ch_num, u16 ch_status, void *data) | |
400 | { | |
401 | struct edma_chan *echan = data; | |
402 | struct device *dev = echan->vchan.chan.device->dev; | |
403 | struct edma_desc *edesc; | |
404 | unsigned long flags; | |
c5f47990 | 405 | struct edmacc_param p; |
c2dde5f8 | 406 | |
53407062 JF |
407 | /* Pause the channel */ |
408 | edma_pause(echan->ch_num); | |
c2dde5f8 MP |
409 | |
410 | switch (ch_status) { | |
411 | case DMA_COMPLETE: | |
c2dde5f8 MP |
412 | spin_lock_irqsave(&echan->vchan.lock, flags); |
413 | ||
414 | edesc = echan->edesc; | |
415 | if (edesc) { | |
53407062 JF |
416 | if (edesc->processed == edesc->pset_nr) { |
417 | dev_dbg(dev, "Transfer complete, stopping channel %d\n", ch_num); | |
418 | edma_stop(echan->ch_num); | |
419 | vchan_cookie_complete(&edesc->vdesc); | |
420 | } else { | |
421 | dev_dbg(dev, "Intermediate transfer complete on channel %d\n", ch_num); | |
422 | } | |
423 | ||
c2dde5f8 | 424 | edma_execute(echan); |
c2dde5f8 MP |
425 | } |
426 | ||
427 | spin_unlock_irqrestore(&echan->vchan.lock, flags); | |
428 | ||
429 | break; | |
430 | case DMA_CC_ERROR: | |
c5f47990 JF |
431 | spin_lock_irqsave(&echan->vchan.lock, flags); |
432 | ||
433 | edma_read_slot(EDMA_CHAN_SLOT(echan->slot[0]), &p); | |
434 | ||
435 | /* | |
436 | * Issue later based on missed flag which will be sure | |
437 | * to happen as: | |
438 | * (1) we finished transmitting an intermediate slot and | |
439 | * edma_execute is coming up. | |
440 | * (2) or we finished current transfer and issue will | |
441 | * call edma_execute. | |
442 | * | |
443 | * Important note: issuing can be dangerous here and | |
444 | * lead to some nasty recursion when we are in a NULL | |
445 | * slot. So we avoid doing so and set the missed flag. | |
446 | */ | |
447 | if (p.a_b_cnt == 0 && p.ccnt == 0) { | |
448 | dev_dbg(dev, "Error occurred, looks like slot is null, just setting miss\n"); | |
449 | echan->missed = 1; | |
450 | } else { | |
451 | /* | |
452 | * The slot is already programmed but the event got | |
453 | * missed, so its safe to issue it here. | |
454 | */ | |
455 | dev_dbg(dev, "Error occurred but slot is non-null, TRIGGERING\n"); | |
456 | edma_clean_channel(echan->ch_num); | |
457 | edma_stop(echan->ch_num); | |
458 | edma_start(echan->ch_num); | |
459 | edma_trigger_channel(echan->ch_num); | |
460 | } | |
461 | ||
462 | spin_unlock_irqrestore(&echan->vchan.lock, flags); | |
463 | ||
c2dde5f8 MP |
464 | break; |
465 | default: | |
466 | break; | |
467 | } | |
468 | } | |
469 | ||
470 | /* Alloc channel resources */ | |
471 | static int edma_alloc_chan_resources(struct dma_chan *chan) | |
472 | { | |
473 | struct edma_chan *echan = to_edma_chan(chan); | |
474 | struct device *dev = chan->device->dev; | |
475 | int ret; | |
476 | int a_ch_num; | |
477 | LIST_HEAD(descs); | |
478 | ||
479 | a_ch_num = edma_alloc_channel(echan->ch_num, edma_callback, | |
480 | chan, EVENTQ_DEFAULT); | |
481 | ||
482 | if (a_ch_num < 0) { | |
483 | ret = -ENODEV; | |
484 | goto err_no_chan; | |
485 | } | |
486 | ||
487 | if (a_ch_num != echan->ch_num) { | |
488 | dev_err(dev, "failed to allocate requested channel %u:%u\n", | |
489 | EDMA_CTLR(echan->ch_num), | |
490 | EDMA_CHAN_SLOT(echan->ch_num)); | |
491 | ret = -ENODEV; | |
492 | goto err_wrong_chan; | |
493 | } | |
494 | ||
495 | echan->alloced = true; | |
496 | echan->slot[0] = echan->ch_num; | |
497 | ||
498 | dev_info(dev, "allocated channel for %u:%u\n", | |
499 | EDMA_CTLR(echan->ch_num), EDMA_CHAN_SLOT(echan->ch_num)); | |
500 | ||
501 | return 0; | |
502 | ||
503 | err_wrong_chan: | |
504 | edma_free_channel(a_ch_num); | |
505 | err_no_chan: | |
506 | return ret; | |
507 | } | |
508 | ||
509 | /* Free channel resources */ | |
510 | static void edma_free_chan_resources(struct dma_chan *chan) | |
511 | { | |
512 | struct edma_chan *echan = to_edma_chan(chan); | |
513 | struct device *dev = chan->device->dev; | |
514 | int i; | |
515 | ||
516 | /* Terminate transfers */ | |
517 | edma_stop(echan->ch_num); | |
518 | ||
519 | vchan_free_chan_resources(&echan->vchan); | |
520 | ||
521 | /* Free EDMA PaRAM slots */ | |
522 | for (i = 1; i < EDMA_MAX_SLOTS; i++) { | |
523 | if (echan->slot[i] >= 0) { | |
524 | edma_free_slot(echan->slot[i]); | |
525 | echan->slot[i] = -1; | |
526 | } | |
527 | } | |
528 | ||
529 | /* Free EDMA channel */ | |
530 | if (echan->alloced) { | |
531 | edma_free_channel(echan->ch_num); | |
532 | echan->alloced = false; | |
533 | } | |
534 | ||
535 | dev_info(dev, "freeing channel for %u\n", echan->ch_num); | |
536 | } | |
537 | ||
538 | /* Send pending descriptor to hardware */ | |
539 | static void edma_issue_pending(struct dma_chan *chan) | |
540 | { | |
541 | struct edma_chan *echan = to_edma_chan(chan); | |
542 | unsigned long flags; | |
543 | ||
544 | spin_lock_irqsave(&echan->vchan.lock, flags); | |
545 | if (vchan_issue_pending(&echan->vchan) && !echan->edesc) | |
546 | edma_execute(echan); | |
547 | spin_unlock_irqrestore(&echan->vchan.lock, flags); | |
548 | } | |
549 | ||
550 | static size_t edma_desc_size(struct edma_desc *edesc) | |
551 | { | |
552 | int i; | |
553 | size_t size; | |
554 | ||
555 | if (edesc->absync) | |
556 | for (size = i = 0; i < edesc->pset_nr; i++) | |
557 | size += (edesc->pset[i].a_b_cnt & 0xffff) * | |
558 | (edesc->pset[i].a_b_cnt >> 16) * | |
559 | edesc->pset[i].ccnt; | |
560 | else | |
561 | size = (edesc->pset[0].a_b_cnt & 0xffff) * | |
562 | (edesc->pset[0].a_b_cnt >> 16) + | |
563 | (edesc->pset[0].a_b_cnt & 0xffff) * | |
564 | (SZ_64K - 1) * edesc->pset[0].ccnt; | |
565 | ||
566 | return size; | |
567 | } | |
568 | ||
569 | /* Check request completion status */ | |
570 | static enum dma_status edma_tx_status(struct dma_chan *chan, | |
571 | dma_cookie_t cookie, | |
572 | struct dma_tx_state *txstate) | |
573 | { | |
574 | struct edma_chan *echan = to_edma_chan(chan); | |
575 | struct virt_dma_desc *vdesc; | |
576 | enum dma_status ret; | |
577 | unsigned long flags; | |
578 | ||
579 | ret = dma_cookie_status(chan, cookie, txstate); | |
580 | if (ret == DMA_SUCCESS || !txstate) | |
581 | return ret; | |
582 | ||
583 | spin_lock_irqsave(&echan->vchan.lock, flags); | |
584 | vdesc = vchan_find_desc(&echan->vchan, cookie); | |
585 | if (vdesc) { | |
586 | txstate->residue = edma_desc_size(to_edma_desc(&vdesc->tx)); | |
587 | } else if (echan->edesc && echan->edesc->vdesc.tx.cookie == cookie) { | |
588 | struct edma_desc *edesc = echan->edesc; | |
589 | txstate->residue = edma_desc_size(edesc); | |
c2dde5f8 MP |
590 | } |
591 | spin_unlock_irqrestore(&echan->vchan.lock, flags); | |
592 | ||
593 | return ret; | |
594 | } | |
595 | ||
596 | static void __init edma_chan_init(struct edma_cc *ecc, | |
597 | struct dma_device *dma, | |
598 | struct edma_chan *echans) | |
599 | { | |
600 | int i, j; | |
601 | ||
602 | for (i = 0; i < EDMA_CHANS; i++) { | |
603 | struct edma_chan *echan = &echans[i]; | |
604 | echan->ch_num = EDMA_CTLR_CHAN(ecc->ctlr, i); | |
605 | echan->ecc = ecc; | |
606 | echan->vchan.desc_free = edma_desc_free; | |
607 | ||
608 | vchan_init(&echan->vchan, dma); | |
609 | ||
610 | INIT_LIST_HEAD(&echan->node); | |
611 | for (j = 0; j < EDMA_MAX_SLOTS; j++) | |
612 | echan->slot[j] = -1; | |
613 | } | |
614 | } | |
615 | ||
616 | static void edma_dma_init(struct edma_cc *ecc, struct dma_device *dma, | |
617 | struct device *dev) | |
618 | { | |
619 | dma->device_prep_slave_sg = edma_prep_slave_sg; | |
620 | dma->device_alloc_chan_resources = edma_alloc_chan_resources; | |
621 | dma->device_free_chan_resources = edma_free_chan_resources; | |
622 | dma->device_issue_pending = edma_issue_pending; | |
623 | dma->device_tx_status = edma_tx_status; | |
624 | dma->device_control = edma_control; | |
625 | dma->dev = dev; | |
626 | ||
627 | INIT_LIST_HEAD(&dma->channels); | |
628 | } | |
629 | ||
463a1f8b | 630 | static int edma_probe(struct platform_device *pdev) |
c2dde5f8 MP |
631 | { |
632 | struct edma_cc *ecc; | |
633 | int ret; | |
634 | ||
635 | ecc = devm_kzalloc(&pdev->dev, sizeof(*ecc), GFP_KERNEL); | |
636 | if (!ecc) { | |
637 | dev_err(&pdev->dev, "Can't allocate controller\n"); | |
638 | return -ENOMEM; | |
639 | } | |
640 | ||
641 | ecc->ctlr = pdev->id; | |
642 | ecc->dummy_slot = edma_alloc_slot(ecc->ctlr, EDMA_SLOT_ANY); | |
643 | if (ecc->dummy_slot < 0) { | |
644 | dev_err(&pdev->dev, "Can't allocate PaRAM dummy slot\n"); | |
645 | return -EIO; | |
646 | } | |
647 | ||
648 | dma_cap_zero(ecc->dma_slave.cap_mask); | |
649 | dma_cap_set(DMA_SLAVE, ecc->dma_slave.cap_mask); | |
650 | ||
651 | edma_dma_init(ecc, &ecc->dma_slave, &pdev->dev); | |
652 | ||
653 | edma_chan_init(ecc, &ecc->dma_slave, ecc->slave_chans); | |
654 | ||
655 | ret = dma_async_device_register(&ecc->dma_slave); | |
656 | if (ret) | |
657 | goto err_reg1; | |
658 | ||
659 | platform_set_drvdata(pdev, ecc); | |
660 | ||
661 | dev_info(&pdev->dev, "TI EDMA DMA engine driver\n"); | |
662 | ||
663 | return 0; | |
664 | ||
665 | err_reg1: | |
666 | edma_free_slot(ecc->dummy_slot); | |
667 | return ret; | |
668 | } | |
669 | ||
4bf27b8b | 670 | static int edma_remove(struct platform_device *pdev) |
c2dde5f8 MP |
671 | { |
672 | struct device *dev = &pdev->dev; | |
673 | struct edma_cc *ecc = dev_get_drvdata(dev); | |
674 | ||
675 | dma_async_device_unregister(&ecc->dma_slave); | |
676 | edma_free_slot(ecc->dummy_slot); | |
677 | ||
678 | return 0; | |
679 | } | |
680 | ||
681 | static struct platform_driver edma_driver = { | |
682 | .probe = edma_probe, | |
a7d6e3ec | 683 | .remove = edma_remove, |
c2dde5f8 MP |
684 | .driver = { |
685 | .name = "edma-dma-engine", | |
686 | .owner = THIS_MODULE, | |
687 | }, | |
688 | }; | |
689 | ||
690 | bool edma_filter_fn(struct dma_chan *chan, void *param) | |
691 | { | |
692 | if (chan->device->dev->driver == &edma_driver.driver) { | |
693 | struct edma_chan *echan = to_edma_chan(chan); | |
694 | unsigned ch_req = *(unsigned *)param; | |
695 | return ch_req == echan->ch_num; | |
696 | } | |
697 | return false; | |
698 | } | |
699 | EXPORT_SYMBOL(edma_filter_fn); | |
700 | ||
701 | static struct platform_device *pdev0, *pdev1; | |
702 | ||
703 | static const struct platform_device_info edma_dev_info0 = { | |
704 | .name = "edma-dma-engine", | |
705 | .id = 0, | |
c2dde5f8 MP |
706 | }; |
707 | ||
708 | static const struct platform_device_info edma_dev_info1 = { | |
709 | .name = "edma-dma-engine", | |
710 | .id = 1, | |
c2dde5f8 MP |
711 | }; |
712 | ||
713 | static int edma_init(void) | |
714 | { | |
715 | int ret = platform_driver_register(&edma_driver); | |
716 | ||
717 | if (ret == 0) { | |
718 | pdev0 = platform_device_register_full(&edma_dev_info0); | |
719 | if (IS_ERR(pdev0)) { | |
720 | platform_driver_unregister(&edma_driver); | |
721 | ret = PTR_ERR(pdev0); | |
722 | goto out; | |
723 | } | |
373459ee AS |
724 | pdev0->dev.dma_mask = &pdev0->dev.coherent_dma_mask; |
725 | pdev0->dev.coherent_dma_mask = DMA_BIT_MASK(32); | |
c2dde5f8 MP |
726 | } |
727 | ||
728 | if (EDMA_CTLRS == 2) { | |
729 | pdev1 = platform_device_register_full(&edma_dev_info1); | |
730 | if (IS_ERR(pdev1)) { | |
731 | platform_driver_unregister(&edma_driver); | |
732 | platform_device_unregister(pdev0); | |
733 | ret = PTR_ERR(pdev1); | |
734 | } | |
373459ee AS |
735 | pdev1->dev.dma_mask = &pdev1->dev.coherent_dma_mask; |
736 | pdev1->dev.coherent_dma_mask = DMA_BIT_MASK(32); | |
c2dde5f8 MP |
737 | } |
738 | ||
739 | out: | |
740 | return ret; | |
741 | } | |
742 | subsys_initcall(edma_init); | |
743 | ||
744 | static void __exit edma_exit(void) | |
745 | { | |
746 | platform_device_unregister(pdev0); | |
747 | if (pdev1) | |
748 | platform_device_unregister(pdev1); | |
749 | platform_driver_unregister(&edma_driver); | |
750 | } | |
751 | module_exit(edma_exit); | |
752 | ||
753 | MODULE_AUTHOR("Matt Porter <mporter@ti.com>"); | |
754 | MODULE_DESCRIPTION("TI EDMA DMA engine driver"); | |
755 | MODULE_LICENSE("GPL v2"); |