Commit | Line | Data |
---|---|---|
dc78baa2 NF |
1 | /* |
2 | * Driver for the Atmel AHB DMA Controller (aka HDMA or DMAC on AT91 systems) | |
3 | * | |
4 | * Copyright (C) 2008 Atmel Corporation | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * | |
9102d871 NF |
12 | * This supports the Atmel AHB DMA Controller found in several Atmel SoCs. |
13 | * The only Atmel DMA Controller that is not covered by this driver is the one | |
14 | * found on AT91SAM9263. | |
dc78baa2 NF |
15 | */ |
16 | ||
62971b29 | 17 | #include <dt-bindings/dma/at91.h> |
dc78baa2 NF |
18 | #include <linux/clk.h> |
19 | #include <linux/dmaengine.h> | |
20 | #include <linux/dma-mapping.h> | |
21 | #include <linux/dmapool.h> | |
22 | #include <linux/interrupt.h> | |
23 | #include <linux/module.h> | |
24 | #include <linux/platform_device.h> | |
5a0e3ad6 | 25 | #include <linux/slab.h> |
c5115953 NF |
26 | #include <linux/of.h> |
27 | #include <linux/of_device.h> | |
bbe89c8e | 28 | #include <linux/of_dma.h> |
dc78baa2 NF |
29 | |
30 | #include "at_hdmac_regs.h" | |
d2ebfb33 | 31 | #include "dmaengine.h" |
dc78baa2 NF |
32 | |
33 | /* | |
34 | * Glossary | |
35 | * -------- | |
36 | * | |
37 | * at_hdmac : Name of the ATmel AHB DMA Controller | |
38 | * at_dma_ / atdma : ATmel DMA controller entity related | |
39 | * atc_ / atchan : ATmel DMA Channel entity related | |
40 | */ | |
41 | ||
42 | #define ATC_DEFAULT_CFG (ATC_FIFOCFG_HALFFIFO) | |
ae14d4b5 NF |
43 | #define ATC_DEFAULT_CTRLB (ATC_SIF(AT_DMA_MEM_IF) \ |
44 | |ATC_DIF(AT_DMA_MEM_IF)) | |
dc78baa2 NF |
45 | |
46 | /* | |
47 | * Initial number of descriptors to allocate for each channel. This could | |
48 | * be increased during dma usage. | |
49 | */ | |
50 | static unsigned int init_nr_desc_per_channel = 64; | |
51 | module_param(init_nr_desc_per_channel, uint, 0644); | |
52 | MODULE_PARM_DESC(init_nr_desc_per_channel, | |
53 | "initial descriptors per channel (default: 64)"); | |
54 | ||
55 | ||
56 | /* prototypes */ | |
57 | static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx); | |
d48de6f1 | 58 | static void atc_issue_pending(struct dma_chan *chan); |
dc78baa2 NF |
59 | |
60 | ||
61 | /*----------------------------------------------------------------------*/ | |
62 | ||
63 | static struct at_desc *atc_first_active(struct at_dma_chan *atchan) | |
64 | { | |
65 | return list_first_entry(&atchan->active_list, | |
66 | struct at_desc, desc_node); | |
67 | } | |
68 | ||
69 | static struct at_desc *atc_first_queued(struct at_dma_chan *atchan) | |
70 | { | |
71 | return list_first_entry(&atchan->queue, | |
72 | struct at_desc, desc_node); | |
73 | } | |
74 | ||
75 | /** | |
421f91d2 | 76 | * atc_alloc_descriptor - allocate and return an initialized descriptor |
dc78baa2 NF |
77 | * @chan: the channel to allocate descriptors for |
78 | * @gfp_flags: GFP allocation flags | |
79 | * | |
80 | * Note: The ack-bit is positioned in the descriptor flag at creation time | |
81 | * to make initial allocation more convenient. This bit will be cleared | |
82 | * and control will be given to client at usage time (during | |
83 | * preparation functions). | |
84 | */ | |
85 | static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan, | |
86 | gfp_t gfp_flags) | |
87 | { | |
88 | struct at_desc *desc = NULL; | |
89 | struct at_dma *atdma = to_at_dma(chan->device); | |
90 | dma_addr_t phys; | |
91 | ||
92 | desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys); | |
93 | if (desc) { | |
94 | memset(desc, 0, sizeof(struct at_desc)); | |
285a3c71 | 95 | INIT_LIST_HEAD(&desc->tx_list); |
dc78baa2 NF |
96 | dma_async_tx_descriptor_init(&desc->txd, chan); |
97 | /* txd.flags will be overwritten in prep functions */ | |
98 | desc->txd.flags = DMA_CTRL_ACK; | |
99 | desc->txd.tx_submit = atc_tx_submit; | |
100 | desc->txd.phys = phys; | |
101 | } | |
102 | ||
103 | return desc; | |
104 | } | |
105 | ||
106 | /** | |
af901ca1 | 107 | * atc_desc_get - get an unused descriptor from free_list |
dc78baa2 NF |
108 | * @atchan: channel we want a new descriptor for |
109 | */ | |
110 | static struct at_desc *atc_desc_get(struct at_dma_chan *atchan) | |
111 | { | |
112 | struct at_desc *desc, *_desc; | |
113 | struct at_desc *ret = NULL; | |
d8cb04b0 | 114 | unsigned long flags; |
dc78baa2 NF |
115 | unsigned int i = 0; |
116 | LIST_HEAD(tmp_list); | |
117 | ||
d8cb04b0 | 118 | spin_lock_irqsave(&atchan->lock, flags); |
dc78baa2 NF |
119 | list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) { |
120 | i++; | |
121 | if (async_tx_test_ack(&desc->txd)) { | |
122 | list_del(&desc->desc_node); | |
123 | ret = desc; | |
124 | break; | |
125 | } | |
126 | dev_dbg(chan2dev(&atchan->chan_common), | |
127 | "desc %p not ACKed\n", desc); | |
128 | } | |
d8cb04b0 | 129 | spin_unlock_irqrestore(&atchan->lock, flags); |
dc78baa2 NF |
130 | dev_vdbg(chan2dev(&atchan->chan_common), |
131 | "scanned %u descriptors on freelist\n", i); | |
132 | ||
133 | /* no more descriptor available in initial pool: create one more */ | |
134 | if (!ret) { | |
135 | ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC); | |
136 | if (ret) { | |
d8cb04b0 | 137 | spin_lock_irqsave(&atchan->lock, flags); |
dc78baa2 | 138 | atchan->descs_allocated++; |
d8cb04b0 | 139 | spin_unlock_irqrestore(&atchan->lock, flags); |
dc78baa2 NF |
140 | } else { |
141 | dev_err(chan2dev(&atchan->chan_common), | |
142 | "not enough descriptors available\n"); | |
143 | } | |
144 | } | |
145 | ||
146 | return ret; | |
147 | } | |
148 | ||
149 | /** | |
150 | * atc_desc_put - move a descriptor, including any children, to the free list | |
151 | * @atchan: channel we work on | |
152 | * @desc: descriptor, at the head of a chain, to move to free list | |
153 | */ | |
154 | static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc) | |
155 | { | |
156 | if (desc) { | |
157 | struct at_desc *child; | |
d8cb04b0 | 158 | unsigned long flags; |
dc78baa2 | 159 | |
d8cb04b0 | 160 | spin_lock_irqsave(&atchan->lock, flags); |
285a3c71 | 161 | list_for_each_entry(child, &desc->tx_list, desc_node) |
dc78baa2 NF |
162 | dev_vdbg(chan2dev(&atchan->chan_common), |
163 | "moving child desc %p to freelist\n", | |
164 | child); | |
285a3c71 | 165 | list_splice_init(&desc->tx_list, &atchan->free_list); |
dc78baa2 NF |
166 | dev_vdbg(chan2dev(&atchan->chan_common), |
167 | "moving desc %p to freelist\n", desc); | |
168 | list_add(&desc->desc_node, &atchan->free_list); | |
d8cb04b0 | 169 | spin_unlock_irqrestore(&atchan->lock, flags); |
dc78baa2 NF |
170 | } |
171 | } | |
172 | ||
53830cc7 | 173 | /** |
d73111c6 MI |
174 | * atc_desc_chain - build chain adding a descriptor |
175 | * @first: address of first descriptor of the chain | |
176 | * @prev: address of previous descriptor of the chain | |
53830cc7 NF |
177 | * @desc: descriptor to queue |
178 | * | |
179 | * Called from prep_* functions | |
180 | */ | |
181 | static void atc_desc_chain(struct at_desc **first, struct at_desc **prev, | |
182 | struct at_desc *desc) | |
183 | { | |
184 | if (!(*first)) { | |
185 | *first = desc; | |
186 | } else { | |
187 | /* inform the HW lli about chaining */ | |
188 | (*prev)->lli.dscr = desc->txd.phys; | |
189 | /* insert the link descriptor to the LD ring */ | |
190 | list_add_tail(&desc->desc_node, | |
191 | &(*first)->tx_list); | |
192 | } | |
193 | *prev = desc; | |
194 | } | |
195 | ||
dc78baa2 NF |
196 | /** |
197 | * atc_dostart - starts the DMA engine for real | |
198 | * @atchan: the channel we want to start | |
199 | * @first: first descriptor in the list we want to begin with | |
200 | * | |
201 | * Called with atchan->lock held and bh disabled | |
202 | */ | |
203 | static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first) | |
204 | { | |
205 | struct at_dma *atdma = to_at_dma(atchan->chan_common.device); | |
206 | ||
207 | /* ASSERT: channel is idle */ | |
208 | if (atc_chan_is_enabled(atchan)) { | |
209 | dev_err(chan2dev(&atchan->chan_common), | |
210 | "BUG: Attempted to start non-idle channel\n"); | |
211 | dev_err(chan2dev(&atchan->chan_common), | |
212 | " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n", | |
213 | channel_readl(atchan, SADDR), | |
214 | channel_readl(atchan, DADDR), | |
215 | channel_readl(atchan, CTRLA), | |
216 | channel_readl(atchan, CTRLB), | |
217 | channel_readl(atchan, DSCR)); | |
218 | ||
219 | /* The tasklet will hopefully advance the queue... */ | |
220 | return; | |
221 | } | |
222 | ||
223 | vdbg_dump_regs(atchan); | |
224 | ||
dc78baa2 NF |
225 | channel_writel(atchan, SADDR, 0); |
226 | channel_writel(atchan, DADDR, 0); | |
227 | channel_writel(atchan, CTRLA, 0); | |
228 | channel_writel(atchan, CTRLB, 0); | |
229 | channel_writel(atchan, DSCR, first->txd.phys); | |
230 | dma_writel(atdma, CHER, atchan->mask); | |
231 | ||
232 | vdbg_dump_regs(atchan); | |
233 | } | |
234 | ||
d48de6f1 ES |
235 | /* |
236 | * atc_get_current_descriptors - | |
237 | * locate the descriptor which equal to physical address in DSCR | |
238 | * @atchan: the channel we want to start | |
239 | * @dscr_addr: physical descriptor address in DSCR | |
240 | */ | |
241 | static struct at_desc *atc_get_current_descriptors(struct at_dma_chan *atchan, | |
242 | u32 dscr_addr) | |
243 | { | |
244 | struct at_desc *desc, *_desc, *child, *desc_cur = NULL; | |
245 | ||
246 | list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) { | |
247 | if (desc->lli.dscr == dscr_addr) { | |
248 | desc_cur = desc; | |
249 | break; | |
250 | } | |
251 | ||
252 | list_for_each_entry(child, &desc->tx_list, desc_node) { | |
253 | if (child->lli.dscr == dscr_addr) { | |
254 | desc_cur = child; | |
255 | break; | |
256 | } | |
257 | } | |
258 | } | |
259 | ||
260 | return desc_cur; | |
261 | } | |
262 | ||
263 | /* | |
264 | * atc_get_bytes_left - | |
265 | * Get the number of bytes residue in dma buffer, | |
266 | * @chan: the channel we want to start | |
267 | */ | |
268 | static int atc_get_bytes_left(struct dma_chan *chan) | |
269 | { | |
270 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
271 | struct at_dma *atdma = to_at_dma(chan->device); | |
272 | int chan_id = atchan->chan_common.chan_id; | |
273 | struct at_desc *desc_first = atc_first_active(atchan); | |
274 | struct at_desc *desc_cur; | |
275 | int ret = 0, count = 0; | |
276 | ||
277 | /* | |
278 | * Initialize necessary values in the first time. | |
279 | * remain_desc record remain desc length. | |
280 | */ | |
281 | if (atchan->remain_desc == 0) | |
282 | /* First descriptor embedds the transaction length */ | |
283 | atchan->remain_desc = desc_first->len; | |
284 | ||
285 | /* | |
286 | * This happens when current descriptor transfer complete. | |
287 | * The residual buffer size should reduce current descriptor length. | |
288 | */ | |
289 | if (unlikely(test_bit(ATC_IS_BTC, &atchan->status))) { | |
290 | clear_bit(ATC_IS_BTC, &atchan->status); | |
291 | desc_cur = atc_get_current_descriptors(atchan, | |
292 | channel_readl(atchan, DSCR)); | |
293 | if (!desc_cur) { | |
294 | ret = -EINVAL; | |
295 | goto out; | |
296 | } | |
6758ddaf AB |
297 | |
298 | count = (desc_cur->lli.ctrla & ATC_BTSIZE_MAX) | |
299 | << desc_first->tx_width; | |
300 | if (atchan->remain_desc < count) { | |
d48de6f1 ES |
301 | ret = -EINVAL; |
302 | goto out; | |
c3dbc60c | 303 | } |
6758ddaf AB |
304 | |
305 | atchan->remain_desc -= count; | |
306 | ret = atchan->remain_desc; | |
d48de6f1 ES |
307 | } else { |
308 | /* | |
309 | * Get residual bytes when current | |
310 | * descriptor transfer in progress. | |
311 | */ | |
312 | count = (channel_readl(atchan, CTRLA) & ATC_BTSIZE_MAX) | |
313 | << (desc_first->tx_width); | |
314 | ret = atchan->remain_desc - count; | |
315 | } | |
316 | /* | |
317 | * Check fifo empty. | |
318 | */ | |
319 | if (!(dma_readl(atdma, CHSR) & AT_DMA_EMPT(chan_id))) | |
320 | atc_issue_pending(chan); | |
321 | ||
322 | out: | |
323 | return ret; | |
324 | } | |
325 | ||
dc78baa2 NF |
326 | /** |
327 | * atc_chain_complete - finish work for one transaction chain | |
328 | * @atchan: channel we work on | |
329 | * @desc: descriptor at the head of the chain we want do complete | |
330 | * | |
331 | * Called with atchan->lock held and bh disabled */ | |
332 | static void | |
333 | atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc) | |
334 | { | |
dc78baa2 NF |
335 | struct dma_async_tx_descriptor *txd = &desc->txd; |
336 | ||
337 | dev_vdbg(chan2dev(&atchan->chan_common), | |
338 | "descriptor %u complete\n", txd->cookie); | |
339 | ||
d4116052 VK |
340 | /* mark the descriptor as complete for non cyclic cases only */ |
341 | if (!atc_chan_is_cyclic(atchan)) | |
342 | dma_cookie_complete(txd); | |
dc78baa2 NF |
343 | |
344 | /* move children to free_list */ | |
285a3c71 | 345 | list_splice_init(&desc->tx_list, &atchan->free_list); |
dc78baa2 NF |
346 | /* move myself to free_list */ |
347 | list_move(&desc->desc_node, &atchan->free_list); | |
348 | ||
d38a8c62 | 349 | dma_descriptor_unmap(txd); |
53830cc7 NF |
350 | /* for cyclic transfers, |
351 | * no need to replay callback function while stopping */ | |
3c477482 | 352 | if (!atc_chan_is_cyclic(atchan)) { |
53830cc7 NF |
353 | dma_async_tx_callback callback = txd->callback; |
354 | void *param = txd->callback_param; | |
355 | ||
356 | /* | |
357 | * The API requires that no submissions are done from a | |
358 | * callback, so we don't need to drop the lock here | |
359 | */ | |
360 | if (callback) | |
361 | callback(param); | |
362 | } | |
dc78baa2 NF |
363 | |
364 | dma_run_dependencies(txd); | |
365 | } | |
366 | ||
367 | /** | |
368 | * atc_complete_all - finish work for all transactions | |
369 | * @atchan: channel to complete transactions for | |
370 | * | |
371 | * Eventually submit queued descriptors if any | |
372 | * | |
373 | * Assume channel is idle while calling this function | |
374 | * Called with atchan->lock held and bh disabled | |
375 | */ | |
376 | static void atc_complete_all(struct at_dma_chan *atchan) | |
377 | { | |
378 | struct at_desc *desc, *_desc; | |
379 | LIST_HEAD(list); | |
380 | ||
381 | dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n"); | |
382 | ||
dc78baa2 NF |
383 | /* |
384 | * Submit queued descriptors ASAP, i.e. before we go through | |
385 | * the completed ones. | |
386 | */ | |
387 | if (!list_empty(&atchan->queue)) | |
388 | atc_dostart(atchan, atc_first_queued(atchan)); | |
389 | /* empty active_list now it is completed */ | |
390 | list_splice_init(&atchan->active_list, &list); | |
391 | /* empty queue list by moving descriptors (if any) to active_list */ | |
392 | list_splice_init(&atchan->queue, &atchan->active_list); | |
393 | ||
394 | list_for_each_entry_safe(desc, _desc, &list, desc_node) | |
395 | atc_chain_complete(atchan, desc); | |
396 | } | |
397 | ||
dc78baa2 NF |
398 | /** |
399 | * atc_advance_work - at the end of a transaction, move forward | |
400 | * @atchan: channel where the transaction ended | |
401 | * | |
402 | * Called with atchan->lock held and bh disabled | |
403 | */ | |
404 | static void atc_advance_work(struct at_dma_chan *atchan) | |
405 | { | |
406 | dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n"); | |
407 | ||
d202f051 LD |
408 | if (atc_chan_is_enabled(atchan)) |
409 | return; | |
410 | ||
dc78baa2 NF |
411 | if (list_empty(&atchan->active_list) || |
412 | list_is_singular(&atchan->active_list)) { | |
413 | atc_complete_all(atchan); | |
414 | } else { | |
415 | atc_chain_complete(atchan, atc_first_active(atchan)); | |
416 | /* advance work */ | |
417 | atc_dostart(atchan, atc_first_active(atchan)); | |
418 | } | |
419 | } | |
420 | ||
421 | ||
422 | /** | |
423 | * atc_handle_error - handle errors reported by DMA controller | |
424 | * @atchan: channel where error occurs | |
425 | * | |
426 | * Called with atchan->lock held and bh disabled | |
427 | */ | |
428 | static void atc_handle_error(struct at_dma_chan *atchan) | |
429 | { | |
430 | struct at_desc *bad_desc; | |
431 | struct at_desc *child; | |
432 | ||
433 | /* | |
434 | * The descriptor currently at the head of the active list is | |
435 | * broked. Since we don't have any way to report errors, we'll | |
436 | * just have to scream loudly and try to carry on. | |
437 | */ | |
438 | bad_desc = atc_first_active(atchan); | |
439 | list_del_init(&bad_desc->desc_node); | |
440 | ||
441 | /* As we are stopped, take advantage to push queued descriptors | |
442 | * in active_list */ | |
443 | list_splice_init(&atchan->queue, atchan->active_list.prev); | |
444 | ||
445 | /* Try to restart the controller */ | |
446 | if (!list_empty(&atchan->active_list)) | |
447 | atc_dostart(atchan, atc_first_active(atchan)); | |
448 | ||
449 | /* | |
450 | * KERN_CRITICAL may seem harsh, but since this only happens | |
451 | * when someone submits a bad physical address in a | |
452 | * descriptor, we should consider ourselves lucky that the | |
453 | * controller flagged an error instead of scribbling over | |
454 | * random memory locations. | |
455 | */ | |
456 | dev_crit(chan2dev(&atchan->chan_common), | |
457 | "Bad descriptor submitted for DMA!\n"); | |
458 | dev_crit(chan2dev(&atchan->chan_common), | |
459 | " cookie: %d\n", bad_desc->txd.cookie); | |
460 | atc_dump_lli(atchan, &bad_desc->lli); | |
285a3c71 | 461 | list_for_each_entry(child, &bad_desc->tx_list, desc_node) |
dc78baa2 NF |
462 | atc_dump_lli(atchan, &child->lli); |
463 | ||
464 | /* Pretend the descriptor completed successfully */ | |
465 | atc_chain_complete(atchan, bad_desc); | |
466 | } | |
467 | ||
53830cc7 NF |
468 | /** |
469 | * atc_handle_cyclic - at the end of a period, run callback function | |
470 | * @atchan: channel used for cyclic operations | |
471 | * | |
472 | * Called with atchan->lock held and bh disabled | |
473 | */ | |
474 | static void atc_handle_cyclic(struct at_dma_chan *atchan) | |
475 | { | |
476 | struct at_desc *first = atc_first_active(atchan); | |
477 | struct dma_async_tx_descriptor *txd = &first->txd; | |
478 | dma_async_tx_callback callback = txd->callback; | |
479 | void *param = txd->callback_param; | |
480 | ||
481 | dev_vdbg(chan2dev(&atchan->chan_common), | |
482 | "new cyclic period llp 0x%08x\n", | |
483 | channel_readl(atchan, DSCR)); | |
484 | ||
485 | if (callback) | |
486 | callback(param); | |
487 | } | |
dc78baa2 NF |
488 | |
489 | /*-- IRQ & Tasklet ---------------------------------------------------*/ | |
490 | ||
491 | static void atc_tasklet(unsigned long data) | |
492 | { | |
493 | struct at_dma_chan *atchan = (struct at_dma_chan *)data; | |
d8cb04b0 | 494 | unsigned long flags; |
dc78baa2 | 495 | |
d8cb04b0 | 496 | spin_lock_irqsave(&atchan->lock, flags); |
53830cc7 | 497 | if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status)) |
dc78baa2 | 498 | atc_handle_error(atchan); |
3c477482 | 499 | else if (atc_chan_is_cyclic(atchan)) |
53830cc7 | 500 | atc_handle_cyclic(atchan); |
dc78baa2 NF |
501 | else |
502 | atc_advance_work(atchan); | |
503 | ||
d8cb04b0 | 504 | spin_unlock_irqrestore(&atchan->lock, flags); |
dc78baa2 NF |
505 | } |
506 | ||
507 | static irqreturn_t at_dma_interrupt(int irq, void *dev_id) | |
508 | { | |
509 | struct at_dma *atdma = (struct at_dma *)dev_id; | |
510 | struct at_dma_chan *atchan; | |
511 | int i; | |
512 | u32 status, pending, imr; | |
513 | int ret = IRQ_NONE; | |
514 | ||
515 | do { | |
516 | imr = dma_readl(atdma, EBCIMR); | |
517 | status = dma_readl(atdma, EBCISR); | |
518 | pending = status & imr; | |
519 | ||
520 | if (!pending) | |
521 | break; | |
522 | ||
523 | dev_vdbg(atdma->dma_common.dev, | |
524 | "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n", | |
525 | status, imr, pending); | |
526 | ||
527 | for (i = 0; i < atdma->dma_common.chancnt; i++) { | |
528 | atchan = &atdma->chan[i]; | |
9b3aa589 | 529 | if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) { |
dc78baa2 NF |
530 | if (pending & AT_DMA_ERR(i)) { |
531 | /* Disable channel on AHB error */ | |
23b5e3ad NF |
532 | dma_writel(atdma, CHDR, |
533 | AT_DMA_RES(i) | atchan->mask); | |
dc78baa2 | 534 | /* Give information to tasklet */ |
53830cc7 | 535 | set_bit(ATC_IS_ERROR, &atchan->status); |
dc78baa2 | 536 | } |
d48de6f1 ES |
537 | if (pending & AT_DMA_BTC(i)) |
538 | set_bit(ATC_IS_BTC, &atchan->status); | |
dc78baa2 NF |
539 | tasklet_schedule(&atchan->tasklet); |
540 | ret = IRQ_HANDLED; | |
541 | } | |
542 | } | |
543 | ||
544 | } while (pending); | |
545 | ||
546 | return ret; | |
547 | } | |
548 | ||
549 | ||
550 | /*-- DMA Engine API --------------------------------------------------*/ | |
551 | ||
552 | /** | |
553 | * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine | |
554 | * @desc: descriptor at the head of the transaction chain | |
555 | * | |
556 | * Queue chain if DMA engine is working already | |
557 | * | |
558 | * Cookie increment and adding to active_list or queue must be atomic | |
559 | */ | |
560 | static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx) | |
561 | { | |
562 | struct at_desc *desc = txd_to_at_desc(tx); | |
563 | struct at_dma_chan *atchan = to_at_dma_chan(tx->chan); | |
564 | dma_cookie_t cookie; | |
d8cb04b0 | 565 | unsigned long flags; |
dc78baa2 | 566 | |
d8cb04b0 | 567 | spin_lock_irqsave(&atchan->lock, flags); |
884485e1 | 568 | cookie = dma_cookie_assign(tx); |
dc78baa2 NF |
569 | |
570 | if (list_empty(&atchan->active_list)) { | |
571 | dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n", | |
572 | desc->txd.cookie); | |
573 | atc_dostart(atchan, desc); | |
574 | list_add_tail(&desc->desc_node, &atchan->active_list); | |
575 | } else { | |
576 | dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n", | |
577 | desc->txd.cookie); | |
578 | list_add_tail(&desc->desc_node, &atchan->queue); | |
579 | } | |
580 | ||
d8cb04b0 | 581 | spin_unlock_irqrestore(&atchan->lock, flags); |
dc78baa2 NF |
582 | |
583 | return cookie; | |
584 | } | |
585 | ||
586 | /** | |
587 | * atc_prep_dma_memcpy - prepare a memcpy operation | |
588 | * @chan: the channel to prepare operation on | |
589 | * @dest: operation virtual destination address | |
590 | * @src: operation virtual source address | |
591 | * @len: operation length | |
592 | * @flags: tx descriptor status flags | |
593 | */ | |
594 | static struct dma_async_tx_descriptor * | |
595 | atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, | |
596 | size_t len, unsigned long flags) | |
597 | { | |
598 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
599 | struct at_desc *desc = NULL; | |
600 | struct at_desc *first = NULL; | |
601 | struct at_desc *prev = NULL; | |
602 | size_t xfer_count; | |
603 | size_t offset; | |
604 | unsigned int src_width; | |
605 | unsigned int dst_width; | |
606 | u32 ctrla; | |
607 | u32 ctrlb; | |
608 | ||
609 | dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n", | |
610 | dest, src, len, flags); | |
611 | ||
612 | if (unlikely(!len)) { | |
613 | dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n"); | |
614 | return NULL; | |
615 | } | |
616 | ||
9b3aa589 | 617 | ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN |
dc78baa2 NF |
618 | | ATC_SRC_ADDR_MODE_INCR |
619 | | ATC_DST_ADDR_MODE_INCR | |
620 | | ATC_FC_MEM2MEM; | |
621 | ||
622 | /* | |
623 | * We can be a lot more clever here, but this should take care | |
624 | * of the most common optimization. | |
625 | */ | |
626 | if (!((src | dest | len) & 3)) { | |
b409ebfb | 627 | ctrla = ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD; |
dc78baa2 NF |
628 | src_width = dst_width = 2; |
629 | } else if (!((src | dest | len) & 1)) { | |
b409ebfb | 630 | ctrla = ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD; |
dc78baa2 NF |
631 | src_width = dst_width = 1; |
632 | } else { | |
b409ebfb | 633 | ctrla = ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE; |
dc78baa2 NF |
634 | src_width = dst_width = 0; |
635 | } | |
636 | ||
637 | for (offset = 0; offset < len; offset += xfer_count << src_width) { | |
638 | xfer_count = min_t(size_t, (len - offset) >> src_width, | |
639 | ATC_BTSIZE_MAX); | |
640 | ||
641 | desc = atc_desc_get(atchan); | |
642 | if (!desc) | |
643 | goto err_desc_get; | |
644 | ||
645 | desc->lli.saddr = src + offset; | |
646 | desc->lli.daddr = dest + offset; | |
647 | desc->lli.ctrla = ctrla | xfer_count; | |
648 | desc->lli.ctrlb = ctrlb; | |
649 | ||
650 | desc->txd.cookie = 0; | |
dc78baa2 | 651 | |
e257e156 | 652 | atc_desc_chain(&first, &prev, desc); |
dc78baa2 NF |
653 | } |
654 | ||
655 | /* First descriptor of the chain embedds additional information */ | |
656 | first->txd.cookie = -EBUSY; | |
657 | first->len = len; | |
d088c33b | 658 | first->tx_width = src_width; |
dc78baa2 NF |
659 | |
660 | /* set end-of-link to the last link descriptor of list*/ | |
661 | set_desc_eol(desc); | |
662 | ||
568f7f0c | 663 | first->txd.flags = flags; /* client is in control of this ack */ |
dc78baa2 NF |
664 | |
665 | return &first->txd; | |
666 | ||
667 | err_desc_get: | |
668 | atc_desc_put(atchan, first); | |
669 | return NULL; | |
670 | } | |
671 | ||
808347f6 NF |
672 | |
673 | /** | |
674 | * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction | |
675 | * @chan: DMA channel | |
676 | * @sgl: scatterlist to transfer to/from | |
677 | * @sg_len: number of entries in @scatterlist | |
678 | * @direction: DMA direction | |
679 | * @flags: tx descriptor status flags | |
185ecb5f | 680 | * @context: transaction context (ignored) |
808347f6 NF |
681 | */ |
682 | static struct dma_async_tx_descriptor * | |
683 | atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, | |
db8196df | 684 | unsigned int sg_len, enum dma_transfer_direction direction, |
185ecb5f | 685 | unsigned long flags, void *context) |
808347f6 NF |
686 | { |
687 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
688 | struct at_dma_slave *atslave = chan->private; | |
beeaa103 | 689 | struct dma_slave_config *sconfig = &atchan->dma_sconfig; |
808347f6 NF |
690 | struct at_desc *first = NULL; |
691 | struct at_desc *prev = NULL; | |
692 | u32 ctrla; | |
693 | u32 ctrlb; | |
694 | dma_addr_t reg; | |
695 | unsigned int reg_width; | |
696 | unsigned int mem_width; | |
697 | unsigned int i; | |
698 | struct scatterlist *sg; | |
699 | size_t total_len = 0; | |
700 | ||
cc52a10a NF |
701 | dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n", |
702 | sg_len, | |
db8196df | 703 | direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE", |
808347f6 NF |
704 | flags); |
705 | ||
706 | if (unlikely(!atslave || !sg_len)) { | |
c618a9be | 707 | dev_dbg(chan2dev(chan), "prep_slave_sg: sg length is zero!\n"); |
808347f6 NF |
708 | return NULL; |
709 | } | |
710 | ||
1dd1ea8e NF |
711 | ctrla = ATC_SCSIZE(sconfig->src_maxburst) |
712 | | ATC_DCSIZE(sconfig->dst_maxburst); | |
ae14d4b5 | 713 | ctrlb = ATC_IEN; |
808347f6 NF |
714 | |
715 | switch (direction) { | |
db8196df | 716 | case DMA_MEM_TO_DEV: |
beeaa103 | 717 | reg_width = convert_buswidth(sconfig->dst_addr_width); |
808347f6 NF |
718 | ctrla |= ATC_DST_WIDTH(reg_width); |
719 | ctrlb |= ATC_DST_ADDR_MODE_FIXED | |
720 | | ATC_SRC_ADDR_MODE_INCR | |
ae14d4b5 | 721 | | ATC_FC_MEM2PER |
bbe89c8e | 722 | | ATC_SIF(atchan->mem_if) | ATC_DIF(atchan->per_if); |
beeaa103 | 723 | reg = sconfig->dst_addr; |
808347f6 NF |
724 | for_each_sg(sgl, sg, sg_len, i) { |
725 | struct at_desc *desc; | |
726 | u32 len; | |
727 | u32 mem; | |
728 | ||
729 | desc = atc_desc_get(atchan); | |
730 | if (!desc) | |
731 | goto err_desc_get; | |
732 | ||
0f70e8ce | 733 | mem = sg_dma_address(sg); |
808347f6 | 734 | len = sg_dma_len(sg); |
c4567976 NF |
735 | if (unlikely(!len)) { |
736 | dev_dbg(chan2dev(chan), | |
737 | "prep_slave_sg: sg(%d) data length is zero\n", i); | |
738 | goto err; | |
739 | } | |
808347f6 NF |
740 | mem_width = 2; |
741 | if (unlikely(mem & 3 || len & 3)) | |
742 | mem_width = 0; | |
743 | ||
744 | desc->lli.saddr = mem; | |
745 | desc->lli.daddr = reg; | |
746 | desc->lli.ctrla = ctrla | |
747 | | ATC_SRC_WIDTH(mem_width) | |
748 | | len >> mem_width; | |
749 | desc->lli.ctrlb = ctrlb; | |
750 | ||
e257e156 | 751 | atc_desc_chain(&first, &prev, desc); |
808347f6 NF |
752 | total_len += len; |
753 | } | |
754 | break; | |
db8196df | 755 | case DMA_DEV_TO_MEM: |
beeaa103 | 756 | reg_width = convert_buswidth(sconfig->src_addr_width); |
808347f6 NF |
757 | ctrla |= ATC_SRC_WIDTH(reg_width); |
758 | ctrlb |= ATC_DST_ADDR_MODE_INCR | |
759 | | ATC_SRC_ADDR_MODE_FIXED | |
ae14d4b5 | 760 | | ATC_FC_PER2MEM |
bbe89c8e | 761 | | ATC_SIF(atchan->per_if) | ATC_DIF(atchan->mem_if); |
808347f6 | 762 | |
beeaa103 | 763 | reg = sconfig->src_addr; |
808347f6 NF |
764 | for_each_sg(sgl, sg, sg_len, i) { |
765 | struct at_desc *desc; | |
766 | u32 len; | |
767 | u32 mem; | |
768 | ||
769 | desc = atc_desc_get(atchan); | |
770 | if (!desc) | |
771 | goto err_desc_get; | |
772 | ||
0f70e8ce | 773 | mem = sg_dma_address(sg); |
808347f6 | 774 | len = sg_dma_len(sg); |
c4567976 NF |
775 | if (unlikely(!len)) { |
776 | dev_dbg(chan2dev(chan), | |
777 | "prep_slave_sg: sg(%d) data length is zero\n", i); | |
778 | goto err; | |
779 | } | |
808347f6 NF |
780 | mem_width = 2; |
781 | if (unlikely(mem & 3 || len & 3)) | |
782 | mem_width = 0; | |
783 | ||
784 | desc->lli.saddr = reg; | |
785 | desc->lli.daddr = mem; | |
786 | desc->lli.ctrla = ctrla | |
787 | | ATC_DST_WIDTH(mem_width) | |
59a609d9 | 788 | | len >> reg_width; |
808347f6 NF |
789 | desc->lli.ctrlb = ctrlb; |
790 | ||
e257e156 | 791 | atc_desc_chain(&first, &prev, desc); |
808347f6 NF |
792 | total_len += len; |
793 | } | |
794 | break; | |
795 | default: | |
796 | return NULL; | |
797 | } | |
798 | ||
799 | /* set end-of-link to the last link descriptor of list*/ | |
800 | set_desc_eol(prev); | |
801 | ||
802 | /* First descriptor of the chain embedds additional information */ | |
803 | first->txd.cookie = -EBUSY; | |
804 | first->len = total_len; | |
d088c33b | 805 | first->tx_width = reg_width; |
808347f6 | 806 | |
568f7f0c NF |
807 | /* first link descriptor of list is responsible of flags */ |
808 | first->txd.flags = flags; /* client is in control of this ack */ | |
808347f6 NF |
809 | |
810 | return &first->txd; | |
811 | ||
812 | err_desc_get: | |
813 | dev_err(chan2dev(chan), "not enough descriptors available\n"); | |
c4567976 | 814 | err: |
808347f6 NF |
815 | atc_desc_put(atchan, first); |
816 | return NULL; | |
817 | } | |
818 | ||
53830cc7 NF |
819 | /** |
820 | * atc_dma_cyclic_check_values | |
821 | * Check for too big/unaligned periods and unaligned DMA buffer | |
822 | */ | |
823 | static int | |
824 | atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr, | |
0e7264cc | 825 | size_t period_len) |
53830cc7 NF |
826 | { |
827 | if (period_len > (ATC_BTSIZE_MAX << reg_width)) | |
828 | goto err_out; | |
829 | if (unlikely(period_len & ((1 << reg_width) - 1))) | |
830 | goto err_out; | |
831 | if (unlikely(buf_addr & ((1 << reg_width) - 1))) | |
832 | goto err_out; | |
53830cc7 NF |
833 | |
834 | return 0; | |
835 | ||
836 | err_out: | |
837 | return -EINVAL; | |
838 | } | |
839 | ||
840 | /** | |
d73111c6 | 841 | * atc_dma_cyclic_fill_desc - Fill one period descriptor |
53830cc7 NF |
842 | */ |
843 | static int | |
beeaa103 | 844 | atc_dma_cyclic_fill_desc(struct dma_chan *chan, struct at_desc *desc, |
53830cc7 | 845 | unsigned int period_index, dma_addr_t buf_addr, |
beeaa103 NF |
846 | unsigned int reg_width, size_t period_len, |
847 | enum dma_transfer_direction direction) | |
53830cc7 | 848 | { |
beeaa103 | 849 | struct at_dma_chan *atchan = to_at_dma_chan(chan); |
beeaa103 NF |
850 | struct dma_slave_config *sconfig = &atchan->dma_sconfig; |
851 | u32 ctrla; | |
53830cc7 NF |
852 | |
853 | /* prepare common CRTLA value */ | |
1dd1ea8e NF |
854 | ctrla = ATC_SCSIZE(sconfig->src_maxburst) |
855 | | ATC_DCSIZE(sconfig->dst_maxburst) | |
53830cc7 NF |
856 | | ATC_DST_WIDTH(reg_width) |
857 | | ATC_SRC_WIDTH(reg_width) | |
858 | | period_len >> reg_width; | |
859 | ||
860 | switch (direction) { | |
db8196df | 861 | case DMA_MEM_TO_DEV: |
53830cc7 | 862 | desc->lli.saddr = buf_addr + (period_len * period_index); |
beeaa103 | 863 | desc->lli.daddr = sconfig->dst_addr; |
53830cc7 | 864 | desc->lli.ctrla = ctrla; |
ae14d4b5 | 865 | desc->lli.ctrlb = ATC_DST_ADDR_MODE_FIXED |
53830cc7 | 866 | | ATC_SRC_ADDR_MODE_INCR |
ae14d4b5 | 867 | | ATC_FC_MEM2PER |
bbe89c8e LD |
868 | | ATC_SIF(atchan->mem_if) |
869 | | ATC_DIF(atchan->per_if); | |
53830cc7 NF |
870 | break; |
871 | ||
db8196df | 872 | case DMA_DEV_TO_MEM: |
beeaa103 | 873 | desc->lli.saddr = sconfig->src_addr; |
53830cc7 NF |
874 | desc->lli.daddr = buf_addr + (period_len * period_index); |
875 | desc->lli.ctrla = ctrla; | |
ae14d4b5 | 876 | desc->lli.ctrlb = ATC_DST_ADDR_MODE_INCR |
53830cc7 | 877 | | ATC_SRC_ADDR_MODE_FIXED |
ae14d4b5 | 878 | | ATC_FC_PER2MEM |
bbe89c8e LD |
879 | | ATC_SIF(atchan->per_if) |
880 | | ATC_DIF(atchan->mem_if); | |
53830cc7 NF |
881 | break; |
882 | ||
883 | default: | |
884 | return -EINVAL; | |
885 | } | |
886 | ||
887 | return 0; | |
888 | } | |
889 | ||
890 | /** | |
891 | * atc_prep_dma_cyclic - prepare the cyclic DMA transfer | |
892 | * @chan: the DMA channel to prepare | |
893 | * @buf_addr: physical DMA address where the buffer starts | |
894 | * @buf_len: total number of bytes for the entire buffer | |
895 | * @period_len: number of bytes for each period | |
896 | * @direction: transfer direction, to or from device | |
ec8b5e48 | 897 | * @flags: tx descriptor status flags |
53830cc7 NF |
898 | */ |
899 | static struct dma_async_tx_descriptor * | |
900 | atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len, | |
185ecb5f | 901 | size_t period_len, enum dma_transfer_direction direction, |
31c1e5a1 | 902 | unsigned long flags) |
53830cc7 NF |
903 | { |
904 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
905 | struct at_dma_slave *atslave = chan->private; | |
beeaa103 | 906 | struct dma_slave_config *sconfig = &atchan->dma_sconfig; |
53830cc7 NF |
907 | struct at_desc *first = NULL; |
908 | struct at_desc *prev = NULL; | |
909 | unsigned long was_cyclic; | |
beeaa103 | 910 | unsigned int reg_width; |
53830cc7 NF |
911 | unsigned int periods = buf_len / period_len; |
912 | unsigned int i; | |
913 | ||
914 | dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n", | |
db8196df | 915 | direction == DMA_MEM_TO_DEV ? "TO DEVICE" : "FROM DEVICE", |
53830cc7 NF |
916 | buf_addr, |
917 | periods, buf_len, period_len); | |
918 | ||
919 | if (unlikely(!atslave || !buf_len || !period_len)) { | |
920 | dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n"); | |
921 | return NULL; | |
922 | } | |
923 | ||
924 | was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status); | |
925 | if (was_cyclic) { | |
926 | dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n"); | |
927 | return NULL; | |
928 | } | |
929 | ||
0e7264cc AS |
930 | if (unlikely(!is_slave_direction(direction))) |
931 | goto err_out; | |
932 | ||
beeaa103 NF |
933 | if (sconfig->direction == DMA_MEM_TO_DEV) |
934 | reg_width = convert_buswidth(sconfig->dst_addr_width); | |
935 | else | |
936 | reg_width = convert_buswidth(sconfig->src_addr_width); | |
937 | ||
53830cc7 | 938 | /* Check for too big/unaligned periods and unaligned DMA buffer */ |
0e7264cc | 939 | if (atc_dma_cyclic_check_values(reg_width, buf_addr, period_len)) |
53830cc7 NF |
940 | goto err_out; |
941 | ||
942 | /* build cyclic linked list */ | |
943 | for (i = 0; i < periods; i++) { | |
944 | struct at_desc *desc; | |
945 | ||
946 | desc = atc_desc_get(atchan); | |
947 | if (!desc) | |
948 | goto err_desc_get; | |
949 | ||
beeaa103 NF |
950 | if (atc_dma_cyclic_fill_desc(chan, desc, i, buf_addr, |
951 | reg_width, period_len, direction)) | |
53830cc7 NF |
952 | goto err_desc_get; |
953 | ||
954 | atc_desc_chain(&first, &prev, desc); | |
955 | } | |
956 | ||
957 | /* lets make a cyclic list */ | |
958 | prev->lli.dscr = first->txd.phys; | |
959 | ||
960 | /* First descriptor of the chain embedds additional information */ | |
961 | first->txd.cookie = -EBUSY; | |
962 | first->len = buf_len; | |
d088c33b | 963 | first->tx_width = reg_width; |
53830cc7 NF |
964 | |
965 | return &first->txd; | |
966 | ||
967 | err_desc_get: | |
968 | dev_err(chan2dev(chan), "not enough descriptors available\n"); | |
969 | atc_desc_put(atchan, first); | |
970 | err_out: | |
971 | clear_bit(ATC_IS_CYCLIC, &atchan->status); | |
972 | return NULL; | |
973 | } | |
974 | ||
beeaa103 NF |
975 | static int set_runtime_config(struct dma_chan *chan, |
976 | struct dma_slave_config *sconfig) | |
977 | { | |
978 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
979 | ||
980 | /* Check if it is chan is configured for slave transfers */ | |
981 | if (!chan->private) | |
982 | return -EINVAL; | |
983 | ||
984 | memcpy(&atchan->dma_sconfig, sconfig, sizeof(*sconfig)); | |
985 | ||
986 | convert_burst(&atchan->dma_sconfig.src_maxburst); | |
987 | convert_burst(&atchan->dma_sconfig.dst_maxburst); | |
988 | ||
989 | return 0; | |
990 | } | |
991 | ||
53830cc7 | 992 | |
05827630 LW |
993 | static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, |
994 | unsigned long arg) | |
808347f6 NF |
995 | { |
996 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
997 | struct at_dma *atdma = to_at_dma(chan->device); | |
23b5e3ad | 998 | int chan_id = atchan->chan_common.chan_id; |
d8cb04b0 | 999 | unsigned long flags; |
23b5e3ad | 1000 | |
808347f6 NF |
1001 | LIST_HEAD(list); |
1002 | ||
23b5e3ad | 1003 | dev_vdbg(chan2dev(chan), "atc_control (%d)\n", cmd); |
c3635c78 | 1004 | |
23b5e3ad | 1005 | if (cmd == DMA_PAUSE) { |
d8cb04b0 | 1006 | spin_lock_irqsave(&atchan->lock, flags); |
808347f6 | 1007 | |
23b5e3ad | 1008 | dma_writel(atdma, CHER, AT_DMA_SUSP(chan_id)); |
23b5e3ad | 1009 | set_bit(ATC_IS_PAUSED, &atchan->status); |
808347f6 | 1010 | |
d8cb04b0 | 1011 | spin_unlock_irqrestore(&atchan->lock, flags); |
23b5e3ad | 1012 | } else if (cmd == DMA_RESUME) { |
3c477482 | 1013 | if (!atc_chan_is_paused(atchan)) |
23b5e3ad | 1014 | return 0; |
808347f6 | 1015 | |
d8cb04b0 | 1016 | spin_lock_irqsave(&atchan->lock, flags); |
808347f6 | 1017 | |
23b5e3ad NF |
1018 | dma_writel(atdma, CHDR, AT_DMA_RES(chan_id)); |
1019 | clear_bit(ATC_IS_PAUSED, &atchan->status); | |
c3635c78 | 1020 | |
d8cb04b0 | 1021 | spin_unlock_irqrestore(&atchan->lock, flags); |
23b5e3ad NF |
1022 | } else if (cmd == DMA_TERMINATE_ALL) { |
1023 | struct at_desc *desc, *_desc; | |
1024 | /* | |
1025 | * This is only called when something went wrong elsewhere, so | |
1026 | * we don't really care about the data. Just disable the | |
1027 | * channel. We still have to poll the channel enable bit due | |
1028 | * to AHB/HSB limitations. | |
1029 | */ | |
d8cb04b0 | 1030 | spin_lock_irqsave(&atchan->lock, flags); |
23b5e3ad NF |
1031 | |
1032 | /* disabling channel: must also remove suspend state */ | |
1033 | dma_writel(atdma, CHDR, AT_DMA_RES(chan_id) | atchan->mask); | |
1034 | ||
1035 | /* confirm that this channel is disabled */ | |
1036 | while (dma_readl(atdma, CHSR) & atchan->mask) | |
1037 | cpu_relax(); | |
1038 | ||
1039 | /* active_list entries will end up before queued entries */ | |
1040 | list_splice_init(&atchan->queue, &list); | |
1041 | list_splice_init(&atchan->active_list, &list); | |
1042 | ||
1043 | /* Flush all pending and queued descriptors */ | |
1044 | list_for_each_entry_safe(desc, _desc, &list, desc_node) | |
1045 | atc_chain_complete(atchan, desc); | |
1046 | ||
1047 | clear_bit(ATC_IS_PAUSED, &atchan->status); | |
1048 | /* if channel dedicated to cyclic operations, free it */ | |
1049 | clear_bit(ATC_IS_CYCLIC, &atchan->status); | |
1050 | ||
d8cb04b0 | 1051 | spin_unlock_irqrestore(&atchan->lock, flags); |
beeaa103 NF |
1052 | } else if (cmd == DMA_SLAVE_CONFIG) { |
1053 | return set_runtime_config(chan, (struct dma_slave_config *)arg); | |
23b5e3ad NF |
1054 | } else { |
1055 | return -ENXIO; | |
1056 | } | |
b0ebeb9c | 1057 | |
c3635c78 | 1058 | return 0; |
808347f6 NF |
1059 | } |
1060 | ||
dc78baa2 | 1061 | /** |
07934481 | 1062 | * atc_tx_status - poll for transaction completion |
dc78baa2 NF |
1063 | * @chan: DMA channel |
1064 | * @cookie: transaction identifier to check status of | |
07934481 | 1065 | * @txstate: if not %NULL updated with transaction state |
dc78baa2 | 1066 | * |
07934481 | 1067 | * If @txstate is passed in, upon return it reflect the driver |
dc78baa2 NF |
1068 | * internal state and can be used with dma_async_is_complete() to check |
1069 | * the status of multiple cookies without re-checking hardware state. | |
1070 | */ | |
1071 | static enum dma_status | |
07934481 | 1072 | atc_tx_status(struct dma_chan *chan, |
dc78baa2 | 1073 | dma_cookie_t cookie, |
07934481 | 1074 | struct dma_tx_state *txstate) |
dc78baa2 NF |
1075 | { |
1076 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
d8cb04b0 | 1077 | unsigned long flags; |
dc78baa2 | 1078 | enum dma_status ret; |
d48de6f1 | 1079 | int bytes = 0; |
dc78baa2 | 1080 | |
96a2af41 | 1081 | ret = dma_cookie_status(chan, cookie, txstate); |
6d203d1e | 1082 | if (ret == DMA_COMPLETE) |
d48de6f1 ES |
1083 | return ret; |
1084 | /* | |
1085 | * There's no point calculating the residue if there's | |
1086 | * no txstate to store the value. | |
1087 | */ | |
1088 | if (!txstate) | |
1089 | return DMA_ERROR; | |
dc78baa2 | 1090 | |
d48de6f1 | 1091 | spin_lock_irqsave(&atchan->lock, flags); |
dc78baa2 | 1092 | |
d48de6f1 ES |
1093 | /* Get number of bytes left in the active transactions */ |
1094 | bytes = atc_get_bytes_left(chan); | |
96a2af41 | 1095 | |
d8cb04b0 | 1096 | spin_unlock_irqrestore(&atchan->lock, flags); |
dc78baa2 | 1097 | |
d48de6f1 ES |
1098 | if (unlikely(bytes < 0)) { |
1099 | dev_vdbg(chan2dev(chan), "get residual bytes error\n"); | |
1100 | return DMA_ERROR; | |
c3dbc60c | 1101 | } else { |
d48de6f1 | 1102 | dma_set_residue(txstate, bytes); |
c3dbc60c | 1103 | } |
23b5e3ad | 1104 | |
d48de6f1 ES |
1105 | dev_vdbg(chan2dev(chan), "tx_status %d: cookie = %d residue = %d\n", |
1106 | ret, cookie, bytes); | |
dc78baa2 NF |
1107 | |
1108 | return ret; | |
1109 | } | |
1110 | ||
1111 | /** | |
1112 | * atc_issue_pending - try to finish work | |
1113 | * @chan: target DMA channel | |
1114 | */ | |
1115 | static void atc_issue_pending(struct dma_chan *chan) | |
1116 | { | |
1117 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
d8cb04b0 | 1118 | unsigned long flags; |
dc78baa2 NF |
1119 | |
1120 | dev_vdbg(chan2dev(chan), "issue_pending\n"); | |
1121 | ||
53830cc7 | 1122 | /* Not needed for cyclic transfers */ |
3c477482 | 1123 | if (atc_chan_is_cyclic(atchan)) |
53830cc7 NF |
1124 | return; |
1125 | ||
d8cb04b0 | 1126 | spin_lock_irqsave(&atchan->lock, flags); |
d202f051 | 1127 | atc_advance_work(atchan); |
d8cb04b0 | 1128 | spin_unlock_irqrestore(&atchan->lock, flags); |
dc78baa2 NF |
1129 | } |
1130 | ||
1131 | /** | |
1132 | * atc_alloc_chan_resources - allocate resources for DMA channel | |
1133 | * @chan: allocate descriptor resources for this channel | |
1134 | * @client: current client requesting the channel be ready for requests | |
1135 | * | |
1136 | * return - the number of allocated descriptors | |
1137 | */ | |
1138 | static int atc_alloc_chan_resources(struct dma_chan *chan) | |
1139 | { | |
1140 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
1141 | struct at_dma *atdma = to_at_dma(chan->device); | |
1142 | struct at_desc *desc; | |
808347f6 | 1143 | struct at_dma_slave *atslave; |
d8cb04b0 | 1144 | unsigned long flags; |
dc78baa2 | 1145 | int i; |
808347f6 | 1146 | u32 cfg; |
dc78baa2 NF |
1147 | LIST_HEAD(tmp_list); |
1148 | ||
1149 | dev_vdbg(chan2dev(chan), "alloc_chan_resources\n"); | |
1150 | ||
1151 | /* ASSERT: channel is idle */ | |
1152 | if (atc_chan_is_enabled(atchan)) { | |
1153 | dev_dbg(chan2dev(chan), "DMA channel not idle ?\n"); | |
1154 | return -EIO; | |
1155 | } | |
1156 | ||
808347f6 NF |
1157 | cfg = ATC_DEFAULT_CFG; |
1158 | ||
1159 | atslave = chan->private; | |
1160 | if (atslave) { | |
1161 | /* | |
1162 | * We need controller-specific data to set up slave | |
1163 | * transfers. | |
1164 | */ | |
1165 | BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev); | |
1166 | ||
ea7e7906 | 1167 | /* if cfg configuration specified take it instead of default */ |
808347f6 NF |
1168 | if (atslave->cfg) |
1169 | cfg = atslave->cfg; | |
1170 | } | |
1171 | ||
1172 | /* have we already been set up? | |
1173 | * reconfigure channel but no need to reallocate descriptors */ | |
dc78baa2 NF |
1174 | if (!list_empty(&atchan->free_list)) |
1175 | return atchan->descs_allocated; | |
1176 | ||
1177 | /* Allocate initial pool of descriptors */ | |
1178 | for (i = 0; i < init_nr_desc_per_channel; i++) { | |
1179 | desc = atc_alloc_descriptor(chan, GFP_KERNEL); | |
1180 | if (!desc) { | |
1181 | dev_err(atdma->dma_common.dev, | |
1182 | "Only %d initial descriptors\n", i); | |
1183 | break; | |
1184 | } | |
1185 | list_add_tail(&desc->desc_node, &tmp_list); | |
1186 | } | |
1187 | ||
d8cb04b0 | 1188 | spin_lock_irqsave(&atchan->lock, flags); |
dc78baa2 | 1189 | atchan->descs_allocated = i; |
d48de6f1 | 1190 | atchan->remain_desc = 0; |
dc78baa2 | 1191 | list_splice(&tmp_list, &atchan->free_list); |
d3ee98cd | 1192 | dma_cookie_init(chan); |
d8cb04b0 | 1193 | spin_unlock_irqrestore(&atchan->lock, flags); |
dc78baa2 NF |
1194 | |
1195 | /* channel parameters */ | |
808347f6 | 1196 | channel_writel(atchan, CFG, cfg); |
dc78baa2 NF |
1197 | |
1198 | dev_dbg(chan2dev(chan), | |
1199 | "alloc_chan_resources: allocated %d descriptors\n", | |
1200 | atchan->descs_allocated); | |
1201 | ||
1202 | return atchan->descs_allocated; | |
1203 | } | |
1204 | ||
1205 | /** | |
1206 | * atc_free_chan_resources - free all channel resources | |
1207 | * @chan: DMA channel | |
1208 | */ | |
1209 | static void atc_free_chan_resources(struct dma_chan *chan) | |
1210 | { | |
1211 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
1212 | struct at_dma *atdma = to_at_dma(chan->device); | |
1213 | struct at_desc *desc, *_desc; | |
1214 | LIST_HEAD(list); | |
1215 | ||
1216 | dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n", | |
1217 | atchan->descs_allocated); | |
1218 | ||
1219 | /* ASSERT: channel is idle */ | |
1220 | BUG_ON(!list_empty(&atchan->active_list)); | |
1221 | BUG_ON(!list_empty(&atchan->queue)); | |
1222 | BUG_ON(atc_chan_is_enabled(atchan)); | |
1223 | ||
1224 | list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) { | |
1225 | dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc); | |
1226 | list_del(&desc->desc_node); | |
1227 | /* free link descriptor */ | |
1228 | dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys); | |
1229 | } | |
1230 | list_splice_init(&atchan->free_list, &list); | |
1231 | atchan->descs_allocated = 0; | |
53830cc7 | 1232 | atchan->status = 0; |
d48de6f1 | 1233 | atchan->remain_desc = 0; |
dc78baa2 NF |
1234 | |
1235 | dev_vdbg(chan2dev(chan), "free_chan_resources: done\n"); | |
1236 | } | |
1237 | ||
bbe89c8e LD |
1238 | #ifdef CONFIG_OF |
1239 | static bool at_dma_filter(struct dma_chan *chan, void *slave) | |
1240 | { | |
1241 | struct at_dma_slave *atslave = slave; | |
1242 | ||
1243 | if (atslave->dma_dev == chan->device->dev) { | |
1244 | chan->private = atslave; | |
1245 | return true; | |
1246 | } else { | |
1247 | return false; | |
1248 | } | |
1249 | } | |
1250 | ||
1251 | static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec, | |
1252 | struct of_dma *of_dma) | |
1253 | { | |
1254 | struct dma_chan *chan; | |
1255 | struct at_dma_chan *atchan; | |
1256 | struct at_dma_slave *atslave; | |
1257 | dma_cap_mask_t mask; | |
1258 | unsigned int per_id; | |
1259 | struct platform_device *dmac_pdev; | |
1260 | ||
1261 | if (dma_spec->args_count != 2) | |
1262 | return NULL; | |
1263 | ||
1264 | dmac_pdev = of_find_device_by_node(dma_spec->np); | |
1265 | ||
1266 | dma_cap_zero(mask); | |
1267 | dma_cap_set(DMA_SLAVE, mask); | |
1268 | ||
1269 | atslave = devm_kzalloc(&dmac_pdev->dev, sizeof(*atslave), GFP_KERNEL); | |
1270 | if (!atslave) | |
1271 | return NULL; | |
62971b29 LD |
1272 | |
1273 | atslave->cfg = ATC_DST_H2SEL_HW | ATC_SRC_H2SEL_HW; | |
bbe89c8e LD |
1274 | /* |
1275 | * We can fill both SRC_PER and DST_PER, one of these fields will be | |
1276 | * ignored depending on DMA transfer direction. | |
1277 | */ | |
62971b29 LD |
1278 | per_id = dma_spec->args[1] & AT91_DMA_CFG_PER_ID_MASK; |
1279 | atslave->cfg |= ATC_DST_PER_MSB(per_id) | ATC_DST_PER(per_id) | |
6c22770f | 1280 | | ATC_SRC_PER_MSB(per_id) | ATC_SRC_PER(per_id); |
62971b29 LD |
1281 | /* |
1282 | * We have to translate the value we get from the device tree since | |
1283 | * the half FIFO configuration value had to be 0 to keep backward | |
1284 | * compatibility. | |
1285 | */ | |
1286 | switch (dma_spec->args[1] & AT91_DMA_CFG_FIFOCFG_MASK) { | |
1287 | case AT91_DMA_CFG_FIFOCFG_ALAP: | |
1288 | atslave->cfg |= ATC_FIFOCFG_LARGESTBURST; | |
1289 | break; | |
1290 | case AT91_DMA_CFG_FIFOCFG_ASAP: | |
1291 | atslave->cfg |= ATC_FIFOCFG_ENOUGHSPACE; | |
1292 | break; | |
1293 | case AT91_DMA_CFG_FIFOCFG_HALF: | |
1294 | default: | |
1295 | atslave->cfg |= ATC_FIFOCFG_HALFFIFO; | |
1296 | } | |
bbe89c8e LD |
1297 | atslave->dma_dev = &dmac_pdev->dev; |
1298 | ||
1299 | chan = dma_request_channel(mask, at_dma_filter, atslave); | |
1300 | if (!chan) | |
1301 | return NULL; | |
1302 | ||
1303 | atchan = to_at_dma_chan(chan); | |
1304 | atchan->per_if = dma_spec->args[0] & 0xff; | |
1305 | atchan->mem_if = (dma_spec->args[0] >> 16) & 0xff; | |
1306 | ||
1307 | return chan; | |
1308 | } | |
1309 | #else | |
1310 | static struct dma_chan *at_dma_xlate(struct of_phandle_args *dma_spec, | |
1311 | struct of_dma *of_dma) | |
1312 | { | |
1313 | return NULL; | |
1314 | } | |
1315 | #endif | |
dc78baa2 NF |
1316 | |
1317 | /*-- Module Management -----------------------------------------------*/ | |
1318 | ||
02f88be9 NF |
1319 | /* cap_mask is a multi-u32 bitfield, fill it with proper C code. */ |
1320 | static struct at_dma_platform_data at91sam9rl_config = { | |
1321 | .nr_channels = 2, | |
1322 | }; | |
1323 | static struct at_dma_platform_data at91sam9g45_config = { | |
1324 | .nr_channels = 8, | |
1325 | }; | |
1326 | ||
c5115953 NF |
1327 | #if defined(CONFIG_OF) |
1328 | static const struct of_device_id atmel_dma_dt_ids[] = { | |
1329 | { | |
1330 | .compatible = "atmel,at91sam9rl-dma", | |
02f88be9 | 1331 | .data = &at91sam9rl_config, |
c5115953 NF |
1332 | }, { |
1333 | .compatible = "atmel,at91sam9g45-dma", | |
02f88be9 | 1334 | .data = &at91sam9g45_config, |
dcc81734 NF |
1335 | }, { |
1336 | /* sentinel */ | |
1337 | } | |
c5115953 NF |
1338 | }; |
1339 | ||
1340 | MODULE_DEVICE_TABLE(of, atmel_dma_dt_ids); | |
1341 | #endif | |
1342 | ||
0ab88a01 | 1343 | static const struct platform_device_id atdma_devtypes[] = { |
67348450 NF |
1344 | { |
1345 | .name = "at91sam9rl_dma", | |
02f88be9 | 1346 | .driver_data = (unsigned long) &at91sam9rl_config, |
67348450 NF |
1347 | }, { |
1348 | .name = "at91sam9g45_dma", | |
02f88be9 | 1349 | .driver_data = (unsigned long) &at91sam9g45_config, |
67348450 NF |
1350 | }, { |
1351 | /* sentinel */ | |
1352 | } | |
1353 | }; | |
1354 | ||
7fd63ccd | 1355 | static inline const struct at_dma_platform_data * __init at_dma_get_driver_data( |
02f88be9 | 1356 | struct platform_device *pdev) |
c5115953 NF |
1357 | { |
1358 | if (pdev->dev.of_node) { | |
1359 | const struct of_device_id *match; | |
1360 | match = of_match_node(atmel_dma_dt_ids, pdev->dev.of_node); | |
1361 | if (match == NULL) | |
02f88be9 NF |
1362 | return NULL; |
1363 | return match->data; | |
c5115953 | 1364 | } |
02f88be9 NF |
1365 | return (struct at_dma_platform_data *) |
1366 | platform_get_device_id(pdev)->driver_data; | |
c5115953 NF |
1367 | } |
1368 | ||
dc78baa2 NF |
1369 | /** |
1370 | * at_dma_off - disable DMA controller | |
1371 | * @atdma: the Atmel HDAMC device | |
1372 | */ | |
1373 | static void at_dma_off(struct at_dma *atdma) | |
1374 | { | |
1375 | dma_writel(atdma, EN, 0); | |
1376 | ||
1377 | /* disable all interrupts */ | |
1378 | dma_writel(atdma, EBCIDR, -1L); | |
1379 | ||
1380 | /* confirm that all channels are disabled */ | |
1381 | while (dma_readl(atdma, CHSR) & atdma->all_chan_mask) | |
1382 | cpu_relax(); | |
1383 | } | |
1384 | ||
1385 | static int __init at_dma_probe(struct platform_device *pdev) | |
1386 | { | |
dc78baa2 NF |
1387 | struct resource *io; |
1388 | struct at_dma *atdma; | |
1389 | size_t size; | |
1390 | int irq; | |
1391 | int err; | |
1392 | int i; | |
7fd63ccd | 1393 | const struct at_dma_platform_data *plat_dat; |
67348450 | 1394 | |
02f88be9 NF |
1395 | /* setup platform data for each SoC */ |
1396 | dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask); | |
1397 | dma_cap_set(DMA_MEMCPY, at91sam9g45_config.cap_mask); | |
1398 | dma_cap_set(DMA_SLAVE, at91sam9g45_config.cap_mask); | |
67348450 NF |
1399 | |
1400 | /* get DMA parameters from controller type */ | |
02f88be9 NF |
1401 | plat_dat = at_dma_get_driver_data(pdev); |
1402 | if (!plat_dat) | |
1403 | return -ENODEV; | |
dc78baa2 NF |
1404 | |
1405 | io = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
1406 | if (!io) | |
1407 | return -EINVAL; | |
1408 | ||
1409 | irq = platform_get_irq(pdev, 0); | |
1410 | if (irq < 0) | |
1411 | return irq; | |
1412 | ||
1413 | size = sizeof(struct at_dma); | |
02f88be9 | 1414 | size += plat_dat->nr_channels * sizeof(struct at_dma_chan); |
dc78baa2 NF |
1415 | atdma = kzalloc(size, GFP_KERNEL); |
1416 | if (!atdma) | |
1417 | return -ENOMEM; | |
1418 | ||
67348450 | 1419 | /* discover transaction capabilities */ |
02f88be9 NF |
1420 | atdma->dma_common.cap_mask = plat_dat->cap_mask; |
1421 | atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1; | |
dc78baa2 | 1422 | |
114df7d6 | 1423 | size = resource_size(io); |
dc78baa2 NF |
1424 | if (!request_mem_region(io->start, size, pdev->dev.driver->name)) { |
1425 | err = -EBUSY; | |
1426 | goto err_kfree; | |
1427 | } | |
1428 | ||
1429 | atdma->regs = ioremap(io->start, size); | |
1430 | if (!atdma->regs) { | |
1431 | err = -ENOMEM; | |
1432 | goto err_release_r; | |
1433 | } | |
1434 | ||
1435 | atdma->clk = clk_get(&pdev->dev, "dma_clk"); | |
1436 | if (IS_ERR(atdma->clk)) { | |
1437 | err = PTR_ERR(atdma->clk); | |
1438 | goto err_clk; | |
1439 | } | |
f784d9c9 BB |
1440 | err = clk_prepare_enable(atdma->clk); |
1441 | if (err) | |
1442 | goto err_clk_prepare; | |
dc78baa2 NF |
1443 | |
1444 | /* force dma off, just in case */ | |
1445 | at_dma_off(atdma); | |
1446 | ||
1447 | err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma); | |
1448 | if (err) | |
1449 | goto err_irq; | |
1450 | ||
1451 | platform_set_drvdata(pdev, atdma); | |
1452 | ||
1453 | /* create a pool of consistent memory blocks for hardware descriptors */ | |
1454 | atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool", | |
1455 | &pdev->dev, sizeof(struct at_desc), | |
1456 | 4 /* word alignment */, 0); | |
1457 | if (!atdma->dma_desc_pool) { | |
1458 | dev_err(&pdev->dev, "No memory for descriptors dma pool\n"); | |
1459 | err = -ENOMEM; | |
1460 | goto err_pool_create; | |
1461 | } | |
1462 | ||
1463 | /* clear any pending interrupt */ | |
1464 | while (dma_readl(atdma, EBCISR)) | |
1465 | cpu_relax(); | |
1466 | ||
1467 | /* initialize channels related values */ | |
1468 | INIT_LIST_HEAD(&atdma->dma_common.channels); | |
02f88be9 | 1469 | for (i = 0; i < plat_dat->nr_channels; i++) { |
dc78baa2 NF |
1470 | struct at_dma_chan *atchan = &atdma->chan[i]; |
1471 | ||
bbe89c8e LD |
1472 | atchan->mem_if = AT_DMA_MEM_IF; |
1473 | atchan->per_if = AT_DMA_PER_IF; | |
dc78baa2 | 1474 | atchan->chan_common.device = &atdma->dma_common; |
d3ee98cd | 1475 | dma_cookie_init(&atchan->chan_common); |
dc78baa2 NF |
1476 | list_add_tail(&atchan->chan_common.device_node, |
1477 | &atdma->dma_common.channels); | |
1478 | ||
1479 | atchan->ch_regs = atdma->regs + ch_regs(i); | |
1480 | spin_lock_init(&atchan->lock); | |
1481 | atchan->mask = 1 << i; | |
1482 | ||
1483 | INIT_LIST_HEAD(&atchan->active_list); | |
1484 | INIT_LIST_HEAD(&atchan->queue); | |
1485 | INIT_LIST_HEAD(&atchan->free_list); | |
1486 | ||
1487 | tasklet_init(&atchan->tasklet, atc_tasklet, | |
1488 | (unsigned long)atchan); | |
bda3a47c | 1489 | atc_enable_chan_irq(atdma, i); |
dc78baa2 NF |
1490 | } |
1491 | ||
1492 | /* set base routines */ | |
1493 | atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources; | |
1494 | atdma->dma_common.device_free_chan_resources = atc_free_chan_resources; | |
07934481 | 1495 | atdma->dma_common.device_tx_status = atc_tx_status; |
dc78baa2 NF |
1496 | atdma->dma_common.device_issue_pending = atc_issue_pending; |
1497 | atdma->dma_common.dev = &pdev->dev; | |
1498 | ||
1499 | /* set prep routines based on capability */ | |
1500 | if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask)) | |
1501 | atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy; | |
1502 | ||
d7db8080 | 1503 | if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) { |
808347f6 | 1504 | atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg; |
d7db8080 NF |
1505 | /* controller can do slave DMA: can trigger cyclic transfers */ |
1506 | dma_cap_set(DMA_CYCLIC, atdma->dma_common.cap_mask); | |
53830cc7 | 1507 | atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic; |
c3635c78 | 1508 | atdma->dma_common.device_control = atc_control; |
d7db8080 | 1509 | } |
808347f6 | 1510 | |
dc78baa2 NF |
1511 | dma_writel(atdma, EN, AT_DMA_ENABLE); |
1512 | ||
1513 | dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n", | |
1514 | dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "", | |
1515 | dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "", | |
02f88be9 | 1516 | plat_dat->nr_channels); |
dc78baa2 NF |
1517 | |
1518 | dma_async_device_register(&atdma->dma_common); | |
1519 | ||
bbe89c8e LD |
1520 | /* |
1521 | * Do not return an error if the dmac node is not present in order to | |
1522 | * not break the existing way of requesting channel with | |
1523 | * dma_request_channel(). | |
1524 | */ | |
1525 | if (pdev->dev.of_node) { | |
1526 | err = of_dma_controller_register(pdev->dev.of_node, | |
1527 | at_dma_xlate, atdma); | |
1528 | if (err) { | |
1529 | dev_err(&pdev->dev, "could not register of_dma_controller\n"); | |
1530 | goto err_of_dma_controller_register; | |
1531 | } | |
1532 | } | |
1533 | ||
dc78baa2 NF |
1534 | return 0; |
1535 | ||
bbe89c8e LD |
1536 | err_of_dma_controller_register: |
1537 | dma_async_device_unregister(&atdma->dma_common); | |
1538 | dma_pool_destroy(atdma->dma_desc_pool); | |
dc78baa2 | 1539 | err_pool_create: |
dc78baa2 NF |
1540 | free_irq(platform_get_irq(pdev, 0), atdma); |
1541 | err_irq: | |
f784d9c9 BB |
1542 | clk_disable_unprepare(atdma->clk); |
1543 | err_clk_prepare: | |
dc78baa2 NF |
1544 | clk_put(atdma->clk); |
1545 | err_clk: | |
1546 | iounmap(atdma->regs); | |
1547 | atdma->regs = NULL; | |
1548 | err_release_r: | |
1549 | release_mem_region(io->start, size); | |
1550 | err_kfree: | |
1551 | kfree(atdma); | |
1552 | return err; | |
1553 | } | |
1554 | ||
1d1bbd30 | 1555 | static int at_dma_remove(struct platform_device *pdev) |
dc78baa2 NF |
1556 | { |
1557 | struct at_dma *atdma = platform_get_drvdata(pdev); | |
1558 | struct dma_chan *chan, *_chan; | |
1559 | struct resource *io; | |
1560 | ||
1561 | at_dma_off(atdma); | |
1562 | dma_async_device_unregister(&atdma->dma_common); | |
1563 | ||
1564 | dma_pool_destroy(atdma->dma_desc_pool); | |
dc78baa2 NF |
1565 | free_irq(platform_get_irq(pdev, 0), atdma); |
1566 | ||
1567 | list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels, | |
1568 | device_node) { | |
1569 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
1570 | ||
1571 | /* Disable interrupts */ | |
bda3a47c | 1572 | atc_disable_chan_irq(atdma, chan->chan_id); |
dc78baa2 NF |
1573 | |
1574 | tasklet_kill(&atchan->tasklet); | |
1575 | list_del(&chan->device_node); | |
1576 | } | |
1577 | ||
f784d9c9 | 1578 | clk_disable_unprepare(atdma->clk); |
dc78baa2 NF |
1579 | clk_put(atdma->clk); |
1580 | ||
1581 | iounmap(atdma->regs); | |
1582 | atdma->regs = NULL; | |
1583 | ||
1584 | io = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
114df7d6 | 1585 | release_mem_region(io->start, resource_size(io)); |
dc78baa2 NF |
1586 | |
1587 | kfree(atdma); | |
1588 | ||
1589 | return 0; | |
1590 | } | |
1591 | ||
1592 | static void at_dma_shutdown(struct platform_device *pdev) | |
1593 | { | |
1594 | struct at_dma *atdma = platform_get_drvdata(pdev); | |
1595 | ||
1596 | at_dma_off(platform_get_drvdata(pdev)); | |
f784d9c9 | 1597 | clk_disable_unprepare(atdma->clk); |
dc78baa2 NF |
1598 | } |
1599 | ||
c0ba5947 NF |
1600 | static int at_dma_prepare(struct device *dev) |
1601 | { | |
1602 | struct platform_device *pdev = to_platform_device(dev); | |
1603 | struct at_dma *atdma = platform_get_drvdata(pdev); | |
1604 | struct dma_chan *chan, *_chan; | |
1605 | ||
1606 | list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels, | |
1607 | device_node) { | |
1608 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
1609 | /* wait for transaction completion (except in cyclic case) */ | |
3c477482 | 1610 | if (atc_chan_is_enabled(atchan) && !atc_chan_is_cyclic(atchan)) |
c0ba5947 NF |
1611 | return -EAGAIN; |
1612 | } | |
1613 | return 0; | |
1614 | } | |
1615 | ||
1616 | static void atc_suspend_cyclic(struct at_dma_chan *atchan) | |
1617 | { | |
1618 | struct dma_chan *chan = &atchan->chan_common; | |
1619 | ||
1620 | /* Channel should be paused by user | |
1621 | * do it anyway even if it is not done already */ | |
3c477482 | 1622 | if (!atc_chan_is_paused(atchan)) { |
c0ba5947 NF |
1623 | dev_warn(chan2dev(chan), |
1624 | "cyclic channel not paused, should be done by channel user\n"); | |
1625 | atc_control(chan, DMA_PAUSE, 0); | |
1626 | } | |
1627 | ||
1628 | /* now preserve additional data for cyclic operations */ | |
1629 | /* next descriptor address in the cyclic list */ | |
1630 | atchan->save_dscr = channel_readl(atchan, DSCR); | |
1631 | ||
1632 | vdbg_dump_regs(atchan); | |
1633 | } | |
1634 | ||
33f82d14 | 1635 | static int at_dma_suspend_noirq(struct device *dev) |
dc78baa2 | 1636 | { |
33f82d14 DW |
1637 | struct platform_device *pdev = to_platform_device(dev); |
1638 | struct at_dma *atdma = platform_get_drvdata(pdev); | |
c0ba5947 | 1639 | struct dma_chan *chan, *_chan; |
dc78baa2 | 1640 | |
c0ba5947 NF |
1641 | /* preserve data */ |
1642 | list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels, | |
1643 | device_node) { | |
1644 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
1645 | ||
3c477482 | 1646 | if (atc_chan_is_cyclic(atchan)) |
c0ba5947 NF |
1647 | atc_suspend_cyclic(atchan); |
1648 | atchan->save_cfg = channel_readl(atchan, CFG); | |
1649 | } | |
1650 | atdma->save_imr = dma_readl(atdma, EBCIMR); | |
1651 | ||
1652 | /* disable DMA controller */ | |
1653 | at_dma_off(atdma); | |
f784d9c9 | 1654 | clk_disable_unprepare(atdma->clk); |
dc78baa2 NF |
1655 | return 0; |
1656 | } | |
1657 | ||
c0ba5947 NF |
1658 | static void atc_resume_cyclic(struct at_dma_chan *atchan) |
1659 | { | |
1660 | struct at_dma *atdma = to_at_dma(atchan->chan_common.device); | |
1661 | ||
1662 | /* restore channel status for cyclic descriptors list: | |
1663 | * next descriptor in the cyclic list at the time of suspend */ | |
1664 | channel_writel(atchan, SADDR, 0); | |
1665 | channel_writel(atchan, DADDR, 0); | |
1666 | channel_writel(atchan, CTRLA, 0); | |
1667 | channel_writel(atchan, CTRLB, 0); | |
1668 | channel_writel(atchan, DSCR, atchan->save_dscr); | |
1669 | dma_writel(atdma, CHER, atchan->mask); | |
1670 | ||
1671 | /* channel pause status should be removed by channel user | |
1672 | * We cannot take the initiative to do it here */ | |
1673 | ||
1674 | vdbg_dump_regs(atchan); | |
1675 | } | |
1676 | ||
33f82d14 | 1677 | static int at_dma_resume_noirq(struct device *dev) |
dc78baa2 | 1678 | { |
33f82d14 DW |
1679 | struct platform_device *pdev = to_platform_device(dev); |
1680 | struct at_dma *atdma = platform_get_drvdata(pdev); | |
c0ba5947 | 1681 | struct dma_chan *chan, *_chan; |
dc78baa2 | 1682 | |
c0ba5947 | 1683 | /* bring back DMA controller */ |
f784d9c9 | 1684 | clk_prepare_enable(atdma->clk); |
dc78baa2 | 1685 | dma_writel(atdma, EN, AT_DMA_ENABLE); |
c0ba5947 NF |
1686 | |
1687 | /* clear any pending interrupt */ | |
1688 | while (dma_readl(atdma, EBCISR)) | |
1689 | cpu_relax(); | |
1690 | ||
1691 | /* restore saved data */ | |
1692 | dma_writel(atdma, EBCIER, atdma->save_imr); | |
1693 | list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels, | |
1694 | device_node) { | |
1695 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
1696 | ||
1697 | channel_writel(atchan, CFG, atchan->save_cfg); | |
3c477482 | 1698 | if (atc_chan_is_cyclic(atchan)) |
c0ba5947 NF |
1699 | atc_resume_cyclic(atchan); |
1700 | } | |
dc78baa2 | 1701 | return 0; |
dc78baa2 NF |
1702 | } |
1703 | ||
47145210 | 1704 | static const struct dev_pm_ops at_dma_dev_pm_ops = { |
c0ba5947 | 1705 | .prepare = at_dma_prepare, |
33f82d14 DW |
1706 | .suspend_noirq = at_dma_suspend_noirq, |
1707 | .resume_noirq = at_dma_resume_noirq, | |
1708 | }; | |
1709 | ||
dc78baa2 | 1710 | static struct platform_driver at_dma_driver = { |
1d1bbd30 | 1711 | .remove = at_dma_remove, |
dc78baa2 | 1712 | .shutdown = at_dma_shutdown, |
67348450 | 1713 | .id_table = atdma_devtypes, |
dc78baa2 NF |
1714 | .driver = { |
1715 | .name = "at_hdmac", | |
33f82d14 | 1716 | .pm = &at_dma_dev_pm_ops, |
c5115953 | 1717 | .of_match_table = of_match_ptr(atmel_dma_dt_ids), |
dc78baa2 NF |
1718 | }, |
1719 | }; | |
1720 | ||
1721 | static int __init at_dma_init(void) | |
1722 | { | |
1723 | return platform_driver_probe(&at_dma_driver, at_dma_probe); | |
1724 | } | |
93d0bec2 | 1725 | subsys_initcall(at_dma_init); |
dc78baa2 NF |
1726 | |
1727 | static void __exit at_dma_exit(void) | |
1728 | { | |
1729 | platform_driver_unregister(&at_dma_driver); | |
1730 | } | |
1731 | module_exit(at_dma_exit); | |
1732 | ||
1733 | MODULE_DESCRIPTION("Atmel AHB DMA Controller driver"); | |
1734 | MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>"); | |
1735 | MODULE_LICENSE("GPL"); | |
1736 | MODULE_ALIAS("platform:at_hdmac"); |