]>
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 | * | |
12 | * This supports the Atmel AHB DMA Controller, | |
13 | * | |
14 | * The driver has currently been tested with the Atmel AT91SAM9RL | |
15 | * and AT91SAM9G45 series. | |
16 | */ | |
17 | ||
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> |
dc78baa2 NF |
26 | |
27 | #include "at_hdmac_regs.h" | |
28 | ||
29 | /* | |
30 | * Glossary | |
31 | * -------- | |
32 | * | |
33 | * at_hdmac : Name of the ATmel AHB DMA Controller | |
34 | * at_dma_ / atdma : ATmel DMA controller entity related | |
35 | * atc_ / atchan : ATmel DMA Channel entity related | |
36 | */ | |
37 | ||
38 | #define ATC_DEFAULT_CFG (ATC_FIFOCFG_HALFFIFO) | |
39 | #define ATC_DEFAULT_CTRLA (0) | |
40 | #define ATC_DEFAULT_CTRLB (ATC_SIF(0) \ | |
41 | |ATC_DIF(1)) | |
42 | ||
43 | /* | |
44 | * Initial number of descriptors to allocate for each channel. This could | |
45 | * be increased during dma usage. | |
46 | */ | |
47 | static unsigned int init_nr_desc_per_channel = 64; | |
48 | module_param(init_nr_desc_per_channel, uint, 0644); | |
49 | MODULE_PARM_DESC(init_nr_desc_per_channel, | |
50 | "initial descriptors per channel (default: 64)"); | |
51 | ||
52 | ||
53 | /* prototypes */ | |
54 | static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx); | |
55 | ||
56 | ||
57 | /*----------------------------------------------------------------------*/ | |
58 | ||
59 | static struct at_desc *atc_first_active(struct at_dma_chan *atchan) | |
60 | { | |
61 | return list_first_entry(&atchan->active_list, | |
62 | struct at_desc, desc_node); | |
63 | } | |
64 | ||
65 | static struct at_desc *atc_first_queued(struct at_dma_chan *atchan) | |
66 | { | |
67 | return list_first_entry(&atchan->queue, | |
68 | struct at_desc, desc_node); | |
69 | } | |
70 | ||
71 | /** | |
421f91d2 | 72 | * atc_alloc_descriptor - allocate and return an initialized descriptor |
dc78baa2 NF |
73 | * @chan: the channel to allocate descriptors for |
74 | * @gfp_flags: GFP allocation flags | |
75 | * | |
76 | * Note: The ack-bit is positioned in the descriptor flag at creation time | |
77 | * to make initial allocation more convenient. This bit will be cleared | |
78 | * and control will be given to client at usage time (during | |
79 | * preparation functions). | |
80 | */ | |
81 | static struct at_desc *atc_alloc_descriptor(struct dma_chan *chan, | |
82 | gfp_t gfp_flags) | |
83 | { | |
84 | struct at_desc *desc = NULL; | |
85 | struct at_dma *atdma = to_at_dma(chan->device); | |
86 | dma_addr_t phys; | |
87 | ||
88 | desc = dma_pool_alloc(atdma->dma_desc_pool, gfp_flags, &phys); | |
89 | if (desc) { | |
90 | memset(desc, 0, sizeof(struct at_desc)); | |
285a3c71 | 91 | INIT_LIST_HEAD(&desc->tx_list); |
dc78baa2 NF |
92 | dma_async_tx_descriptor_init(&desc->txd, chan); |
93 | /* txd.flags will be overwritten in prep functions */ | |
94 | desc->txd.flags = DMA_CTRL_ACK; | |
95 | desc->txd.tx_submit = atc_tx_submit; | |
96 | desc->txd.phys = phys; | |
97 | } | |
98 | ||
99 | return desc; | |
100 | } | |
101 | ||
102 | /** | |
af901ca1 | 103 | * atc_desc_get - get an unused descriptor from free_list |
dc78baa2 NF |
104 | * @atchan: channel we want a new descriptor for |
105 | */ | |
106 | static struct at_desc *atc_desc_get(struct at_dma_chan *atchan) | |
107 | { | |
108 | struct at_desc *desc, *_desc; | |
109 | struct at_desc *ret = NULL; | |
110 | unsigned int i = 0; | |
111 | LIST_HEAD(tmp_list); | |
112 | ||
113 | spin_lock_bh(&atchan->lock); | |
114 | list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) { | |
115 | i++; | |
116 | if (async_tx_test_ack(&desc->txd)) { | |
117 | list_del(&desc->desc_node); | |
118 | ret = desc; | |
119 | break; | |
120 | } | |
121 | dev_dbg(chan2dev(&atchan->chan_common), | |
122 | "desc %p not ACKed\n", desc); | |
123 | } | |
124 | spin_unlock_bh(&atchan->lock); | |
125 | dev_vdbg(chan2dev(&atchan->chan_common), | |
126 | "scanned %u descriptors on freelist\n", i); | |
127 | ||
128 | /* no more descriptor available in initial pool: create one more */ | |
129 | if (!ret) { | |
130 | ret = atc_alloc_descriptor(&atchan->chan_common, GFP_ATOMIC); | |
131 | if (ret) { | |
132 | spin_lock_bh(&atchan->lock); | |
133 | atchan->descs_allocated++; | |
134 | spin_unlock_bh(&atchan->lock); | |
135 | } else { | |
136 | dev_err(chan2dev(&atchan->chan_common), | |
137 | "not enough descriptors available\n"); | |
138 | } | |
139 | } | |
140 | ||
141 | return ret; | |
142 | } | |
143 | ||
144 | /** | |
145 | * atc_desc_put - move a descriptor, including any children, to the free list | |
146 | * @atchan: channel we work on | |
147 | * @desc: descriptor, at the head of a chain, to move to free list | |
148 | */ | |
149 | static void atc_desc_put(struct at_dma_chan *atchan, struct at_desc *desc) | |
150 | { | |
151 | if (desc) { | |
152 | struct at_desc *child; | |
153 | ||
154 | spin_lock_bh(&atchan->lock); | |
285a3c71 | 155 | list_for_each_entry(child, &desc->tx_list, desc_node) |
dc78baa2 NF |
156 | dev_vdbg(chan2dev(&atchan->chan_common), |
157 | "moving child desc %p to freelist\n", | |
158 | child); | |
285a3c71 | 159 | list_splice_init(&desc->tx_list, &atchan->free_list); |
dc78baa2 NF |
160 | dev_vdbg(chan2dev(&atchan->chan_common), |
161 | "moving desc %p to freelist\n", desc); | |
162 | list_add(&desc->desc_node, &atchan->free_list); | |
163 | spin_unlock_bh(&atchan->lock); | |
164 | } | |
165 | } | |
166 | ||
53830cc7 NF |
167 | /** |
168 | * atc_desc_chain - build chain adding a descripor | |
169 | * @first: address of first descripor of the chain | |
170 | * @prev: address of previous descripor of the chain | |
171 | * @desc: descriptor to queue | |
172 | * | |
173 | * Called from prep_* functions | |
174 | */ | |
175 | static void atc_desc_chain(struct at_desc **first, struct at_desc **prev, | |
176 | struct at_desc *desc) | |
177 | { | |
178 | if (!(*first)) { | |
179 | *first = desc; | |
180 | } else { | |
181 | /* inform the HW lli about chaining */ | |
182 | (*prev)->lli.dscr = desc->txd.phys; | |
183 | /* insert the link descriptor to the LD ring */ | |
184 | list_add_tail(&desc->desc_node, | |
185 | &(*first)->tx_list); | |
186 | } | |
187 | *prev = desc; | |
188 | } | |
189 | ||
dc78baa2 NF |
190 | /** |
191 | * atc_assign_cookie - compute and assign new cookie | |
192 | * @atchan: channel we work on | |
193 | * @desc: descriptor to asign cookie for | |
194 | * | |
195 | * Called with atchan->lock held and bh disabled | |
196 | */ | |
197 | static dma_cookie_t | |
198 | atc_assign_cookie(struct at_dma_chan *atchan, struct at_desc *desc) | |
199 | { | |
200 | dma_cookie_t cookie = atchan->chan_common.cookie; | |
201 | ||
202 | if (++cookie < 0) | |
203 | cookie = 1; | |
204 | ||
205 | atchan->chan_common.cookie = cookie; | |
206 | desc->txd.cookie = cookie; | |
207 | ||
208 | return cookie; | |
209 | } | |
210 | ||
211 | /** | |
212 | * atc_dostart - starts the DMA engine for real | |
213 | * @atchan: the channel we want to start | |
214 | * @first: first descriptor in the list we want to begin with | |
215 | * | |
216 | * Called with atchan->lock held and bh disabled | |
217 | */ | |
218 | static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first) | |
219 | { | |
220 | struct at_dma *atdma = to_at_dma(atchan->chan_common.device); | |
221 | ||
222 | /* ASSERT: channel is idle */ | |
223 | if (atc_chan_is_enabled(atchan)) { | |
224 | dev_err(chan2dev(&atchan->chan_common), | |
225 | "BUG: Attempted to start non-idle channel\n"); | |
226 | dev_err(chan2dev(&atchan->chan_common), | |
227 | " channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n", | |
228 | channel_readl(atchan, SADDR), | |
229 | channel_readl(atchan, DADDR), | |
230 | channel_readl(atchan, CTRLA), | |
231 | channel_readl(atchan, CTRLB), | |
232 | channel_readl(atchan, DSCR)); | |
233 | ||
234 | /* The tasklet will hopefully advance the queue... */ | |
235 | return; | |
236 | } | |
237 | ||
238 | vdbg_dump_regs(atchan); | |
239 | ||
240 | /* clear any pending interrupt */ | |
241 | while (dma_readl(atdma, EBCISR)) | |
242 | cpu_relax(); | |
243 | ||
244 | channel_writel(atchan, SADDR, 0); | |
245 | channel_writel(atchan, DADDR, 0); | |
246 | channel_writel(atchan, CTRLA, 0); | |
247 | channel_writel(atchan, CTRLB, 0); | |
248 | channel_writel(atchan, DSCR, first->txd.phys); | |
249 | dma_writel(atdma, CHER, atchan->mask); | |
250 | ||
251 | vdbg_dump_regs(atchan); | |
252 | } | |
253 | ||
254 | /** | |
255 | * atc_chain_complete - finish work for one transaction chain | |
256 | * @atchan: channel we work on | |
257 | * @desc: descriptor at the head of the chain we want do complete | |
258 | * | |
259 | * Called with atchan->lock held and bh disabled */ | |
260 | static void | |
261 | atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc) | |
262 | { | |
dc78baa2 NF |
263 | struct dma_async_tx_descriptor *txd = &desc->txd; |
264 | ||
265 | dev_vdbg(chan2dev(&atchan->chan_common), | |
266 | "descriptor %u complete\n", txd->cookie); | |
267 | ||
268 | atchan->completed_cookie = txd->cookie; | |
dc78baa2 NF |
269 | |
270 | /* move children to free_list */ | |
285a3c71 | 271 | list_splice_init(&desc->tx_list, &atchan->free_list); |
dc78baa2 NF |
272 | /* move myself to free_list */ |
273 | list_move(&desc->desc_node, &atchan->free_list); | |
274 | ||
ebcf9b80 | 275 | /* unmap dma addresses (not on slave channels) */ |
657a77fa AN |
276 | if (!atchan->chan_common.private) { |
277 | struct device *parent = chan2parent(&atchan->chan_common); | |
278 | if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) { | |
279 | if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE) | |
280 | dma_unmap_single(parent, | |
281 | desc->lli.daddr, | |
282 | desc->len, DMA_FROM_DEVICE); | |
283 | else | |
284 | dma_unmap_page(parent, | |
285 | desc->lli.daddr, | |
286 | desc->len, DMA_FROM_DEVICE); | |
287 | } | |
288 | if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) { | |
289 | if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE) | |
290 | dma_unmap_single(parent, | |
291 | desc->lli.saddr, | |
292 | desc->len, DMA_TO_DEVICE); | |
293 | else | |
294 | dma_unmap_page(parent, | |
295 | desc->lli.saddr, | |
296 | desc->len, DMA_TO_DEVICE); | |
297 | } | |
dc78baa2 NF |
298 | } |
299 | ||
53830cc7 NF |
300 | /* for cyclic transfers, |
301 | * no need to replay callback function while stopping */ | |
302 | if (!test_bit(ATC_IS_CYCLIC, &atchan->status)) { | |
303 | dma_async_tx_callback callback = txd->callback; | |
304 | void *param = txd->callback_param; | |
305 | ||
306 | /* | |
307 | * The API requires that no submissions are done from a | |
308 | * callback, so we don't need to drop the lock here | |
309 | */ | |
310 | if (callback) | |
311 | callback(param); | |
312 | } | |
dc78baa2 NF |
313 | |
314 | dma_run_dependencies(txd); | |
315 | } | |
316 | ||
317 | /** | |
318 | * atc_complete_all - finish work for all transactions | |
319 | * @atchan: channel to complete transactions for | |
320 | * | |
321 | * Eventually submit queued descriptors if any | |
322 | * | |
323 | * Assume channel is idle while calling this function | |
324 | * Called with atchan->lock held and bh disabled | |
325 | */ | |
326 | static void atc_complete_all(struct at_dma_chan *atchan) | |
327 | { | |
328 | struct at_desc *desc, *_desc; | |
329 | LIST_HEAD(list); | |
330 | ||
331 | dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n"); | |
332 | ||
333 | BUG_ON(atc_chan_is_enabled(atchan)); | |
334 | ||
335 | /* | |
336 | * Submit queued descriptors ASAP, i.e. before we go through | |
337 | * the completed ones. | |
338 | */ | |
339 | if (!list_empty(&atchan->queue)) | |
340 | atc_dostart(atchan, atc_first_queued(atchan)); | |
341 | /* empty active_list now it is completed */ | |
342 | list_splice_init(&atchan->active_list, &list); | |
343 | /* empty queue list by moving descriptors (if any) to active_list */ | |
344 | list_splice_init(&atchan->queue, &atchan->active_list); | |
345 | ||
346 | list_for_each_entry_safe(desc, _desc, &list, desc_node) | |
347 | atc_chain_complete(atchan, desc); | |
348 | } | |
349 | ||
350 | /** | |
351 | * atc_cleanup_descriptors - cleanup up finished descriptors in active_list | |
352 | * @atchan: channel to be cleaned up | |
353 | * | |
354 | * Called with atchan->lock held and bh disabled | |
355 | */ | |
356 | static void atc_cleanup_descriptors(struct at_dma_chan *atchan) | |
357 | { | |
358 | struct at_desc *desc, *_desc; | |
359 | struct at_desc *child; | |
360 | ||
361 | dev_vdbg(chan2dev(&atchan->chan_common), "cleanup descriptors\n"); | |
362 | ||
363 | list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) { | |
364 | if (!(desc->lli.ctrla & ATC_DONE)) | |
365 | /* This one is currently in progress */ | |
366 | return; | |
367 | ||
285a3c71 | 368 | list_for_each_entry(child, &desc->tx_list, desc_node) |
dc78baa2 NF |
369 | if (!(child->lli.ctrla & ATC_DONE)) |
370 | /* Currently in progress */ | |
371 | return; | |
372 | ||
373 | /* | |
374 | * No descriptors so far seem to be in progress, i.e. | |
375 | * this chain must be done. | |
376 | */ | |
377 | atc_chain_complete(atchan, desc); | |
378 | } | |
379 | } | |
380 | ||
381 | /** | |
382 | * atc_advance_work - at the end of a transaction, move forward | |
383 | * @atchan: channel where the transaction ended | |
384 | * | |
385 | * Called with atchan->lock held and bh disabled | |
386 | */ | |
387 | static void atc_advance_work(struct at_dma_chan *atchan) | |
388 | { | |
389 | dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n"); | |
390 | ||
391 | if (list_empty(&atchan->active_list) || | |
392 | list_is_singular(&atchan->active_list)) { | |
393 | atc_complete_all(atchan); | |
394 | } else { | |
395 | atc_chain_complete(atchan, atc_first_active(atchan)); | |
396 | /* advance work */ | |
397 | atc_dostart(atchan, atc_first_active(atchan)); | |
398 | } | |
399 | } | |
400 | ||
401 | ||
402 | /** | |
403 | * atc_handle_error - handle errors reported by DMA controller | |
404 | * @atchan: channel where error occurs | |
405 | * | |
406 | * Called with atchan->lock held and bh disabled | |
407 | */ | |
408 | static void atc_handle_error(struct at_dma_chan *atchan) | |
409 | { | |
410 | struct at_desc *bad_desc; | |
411 | struct at_desc *child; | |
412 | ||
413 | /* | |
414 | * The descriptor currently at the head of the active list is | |
415 | * broked. Since we don't have any way to report errors, we'll | |
416 | * just have to scream loudly and try to carry on. | |
417 | */ | |
418 | bad_desc = atc_first_active(atchan); | |
419 | list_del_init(&bad_desc->desc_node); | |
420 | ||
421 | /* As we are stopped, take advantage to push queued descriptors | |
422 | * in active_list */ | |
423 | list_splice_init(&atchan->queue, atchan->active_list.prev); | |
424 | ||
425 | /* Try to restart the controller */ | |
426 | if (!list_empty(&atchan->active_list)) | |
427 | atc_dostart(atchan, atc_first_active(atchan)); | |
428 | ||
429 | /* | |
430 | * KERN_CRITICAL may seem harsh, but since this only happens | |
431 | * when someone submits a bad physical address in a | |
432 | * descriptor, we should consider ourselves lucky that the | |
433 | * controller flagged an error instead of scribbling over | |
434 | * random memory locations. | |
435 | */ | |
436 | dev_crit(chan2dev(&atchan->chan_common), | |
437 | "Bad descriptor submitted for DMA!\n"); | |
438 | dev_crit(chan2dev(&atchan->chan_common), | |
439 | " cookie: %d\n", bad_desc->txd.cookie); | |
440 | atc_dump_lli(atchan, &bad_desc->lli); | |
285a3c71 | 441 | list_for_each_entry(child, &bad_desc->tx_list, desc_node) |
dc78baa2 NF |
442 | atc_dump_lli(atchan, &child->lli); |
443 | ||
444 | /* Pretend the descriptor completed successfully */ | |
445 | atc_chain_complete(atchan, bad_desc); | |
446 | } | |
447 | ||
53830cc7 NF |
448 | /** |
449 | * atc_handle_cyclic - at the end of a period, run callback function | |
450 | * @atchan: channel used for cyclic operations | |
451 | * | |
452 | * Called with atchan->lock held and bh disabled | |
453 | */ | |
454 | static void atc_handle_cyclic(struct at_dma_chan *atchan) | |
455 | { | |
456 | struct at_desc *first = atc_first_active(atchan); | |
457 | struct dma_async_tx_descriptor *txd = &first->txd; | |
458 | dma_async_tx_callback callback = txd->callback; | |
459 | void *param = txd->callback_param; | |
460 | ||
461 | dev_vdbg(chan2dev(&atchan->chan_common), | |
462 | "new cyclic period llp 0x%08x\n", | |
463 | channel_readl(atchan, DSCR)); | |
464 | ||
465 | if (callback) | |
466 | callback(param); | |
467 | } | |
dc78baa2 NF |
468 | |
469 | /*-- IRQ & Tasklet ---------------------------------------------------*/ | |
470 | ||
471 | static void atc_tasklet(unsigned long data) | |
472 | { | |
473 | struct at_dma_chan *atchan = (struct at_dma_chan *)data; | |
474 | ||
dc78baa2 | 475 | spin_lock(&atchan->lock); |
53830cc7 | 476 | if (test_and_clear_bit(ATC_IS_ERROR, &atchan->status)) |
dc78baa2 | 477 | atc_handle_error(atchan); |
53830cc7 NF |
478 | else if (test_bit(ATC_IS_CYCLIC, &atchan->status)) |
479 | atc_handle_cyclic(atchan); | |
dc78baa2 NF |
480 | else |
481 | atc_advance_work(atchan); | |
482 | ||
483 | spin_unlock(&atchan->lock); | |
484 | } | |
485 | ||
486 | static irqreturn_t at_dma_interrupt(int irq, void *dev_id) | |
487 | { | |
488 | struct at_dma *atdma = (struct at_dma *)dev_id; | |
489 | struct at_dma_chan *atchan; | |
490 | int i; | |
491 | u32 status, pending, imr; | |
492 | int ret = IRQ_NONE; | |
493 | ||
494 | do { | |
495 | imr = dma_readl(atdma, EBCIMR); | |
496 | status = dma_readl(atdma, EBCISR); | |
497 | pending = status & imr; | |
498 | ||
499 | if (!pending) | |
500 | break; | |
501 | ||
502 | dev_vdbg(atdma->dma_common.dev, | |
503 | "interrupt: status = 0x%08x, 0x%08x, 0x%08x\n", | |
504 | status, imr, pending); | |
505 | ||
506 | for (i = 0; i < atdma->dma_common.chancnt; i++) { | |
507 | atchan = &atdma->chan[i]; | |
9b3aa589 | 508 | if (pending & (AT_DMA_BTC(i) | AT_DMA_ERR(i))) { |
dc78baa2 NF |
509 | if (pending & AT_DMA_ERR(i)) { |
510 | /* Disable channel on AHB error */ | |
511 | dma_writel(atdma, CHDR, atchan->mask); | |
512 | /* Give information to tasklet */ | |
53830cc7 | 513 | set_bit(ATC_IS_ERROR, &atchan->status); |
dc78baa2 NF |
514 | } |
515 | tasklet_schedule(&atchan->tasklet); | |
516 | ret = IRQ_HANDLED; | |
517 | } | |
518 | } | |
519 | ||
520 | } while (pending); | |
521 | ||
522 | return ret; | |
523 | } | |
524 | ||
525 | ||
526 | /*-- DMA Engine API --------------------------------------------------*/ | |
527 | ||
528 | /** | |
529 | * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine | |
530 | * @desc: descriptor at the head of the transaction chain | |
531 | * | |
532 | * Queue chain if DMA engine is working already | |
533 | * | |
534 | * Cookie increment and adding to active_list or queue must be atomic | |
535 | */ | |
536 | static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx) | |
537 | { | |
538 | struct at_desc *desc = txd_to_at_desc(tx); | |
539 | struct at_dma_chan *atchan = to_at_dma_chan(tx->chan); | |
540 | dma_cookie_t cookie; | |
541 | ||
542 | spin_lock_bh(&atchan->lock); | |
543 | cookie = atc_assign_cookie(atchan, desc); | |
544 | ||
545 | if (list_empty(&atchan->active_list)) { | |
546 | dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n", | |
547 | desc->txd.cookie); | |
548 | atc_dostart(atchan, desc); | |
549 | list_add_tail(&desc->desc_node, &atchan->active_list); | |
550 | } else { | |
551 | dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n", | |
552 | desc->txd.cookie); | |
553 | list_add_tail(&desc->desc_node, &atchan->queue); | |
554 | } | |
555 | ||
556 | spin_unlock_bh(&atchan->lock); | |
557 | ||
558 | return cookie; | |
559 | } | |
560 | ||
561 | /** | |
562 | * atc_prep_dma_memcpy - prepare a memcpy operation | |
563 | * @chan: the channel to prepare operation on | |
564 | * @dest: operation virtual destination address | |
565 | * @src: operation virtual source address | |
566 | * @len: operation length | |
567 | * @flags: tx descriptor status flags | |
568 | */ | |
569 | static struct dma_async_tx_descriptor * | |
570 | atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, | |
571 | size_t len, unsigned long flags) | |
572 | { | |
573 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
574 | struct at_desc *desc = NULL; | |
575 | struct at_desc *first = NULL; | |
576 | struct at_desc *prev = NULL; | |
577 | size_t xfer_count; | |
578 | size_t offset; | |
579 | unsigned int src_width; | |
580 | unsigned int dst_width; | |
581 | u32 ctrla; | |
582 | u32 ctrlb; | |
583 | ||
584 | dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n", | |
585 | dest, src, len, flags); | |
586 | ||
587 | if (unlikely(!len)) { | |
588 | dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n"); | |
589 | return NULL; | |
590 | } | |
591 | ||
592 | ctrla = ATC_DEFAULT_CTRLA; | |
9b3aa589 | 593 | ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN |
dc78baa2 NF |
594 | | ATC_SRC_ADDR_MODE_INCR |
595 | | ATC_DST_ADDR_MODE_INCR | |
596 | | ATC_FC_MEM2MEM; | |
597 | ||
598 | /* | |
599 | * We can be a lot more clever here, but this should take care | |
600 | * of the most common optimization. | |
601 | */ | |
602 | if (!((src | dest | len) & 3)) { | |
603 | ctrla |= ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD; | |
604 | src_width = dst_width = 2; | |
605 | } else if (!((src | dest | len) & 1)) { | |
606 | ctrla |= ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD; | |
607 | src_width = dst_width = 1; | |
608 | } else { | |
609 | ctrla |= ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE; | |
610 | src_width = dst_width = 0; | |
611 | } | |
612 | ||
613 | for (offset = 0; offset < len; offset += xfer_count << src_width) { | |
614 | xfer_count = min_t(size_t, (len - offset) >> src_width, | |
615 | ATC_BTSIZE_MAX); | |
616 | ||
617 | desc = atc_desc_get(atchan); | |
618 | if (!desc) | |
619 | goto err_desc_get; | |
620 | ||
621 | desc->lli.saddr = src + offset; | |
622 | desc->lli.daddr = dest + offset; | |
623 | desc->lli.ctrla = ctrla | xfer_count; | |
624 | desc->lli.ctrlb = ctrlb; | |
625 | ||
626 | desc->txd.cookie = 0; | |
dc78baa2 NF |
627 | |
628 | if (!first) { | |
629 | first = desc; | |
630 | } else { | |
631 | /* inform the HW lli about chaining */ | |
632 | prev->lli.dscr = desc->txd.phys; | |
633 | /* insert the link descriptor to the LD ring */ | |
634 | list_add_tail(&desc->desc_node, | |
285a3c71 | 635 | &first->tx_list); |
dc78baa2 NF |
636 | } |
637 | prev = desc; | |
638 | } | |
639 | ||
640 | /* First descriptor of the chain embedds additional information */ | |
641 | first->txd.cookie = -EBUSY; | |
642 | first->len = len; | |
643 | ||
644 | /* set end-of-link to the last link descriptor of list*/ | |
645 | set_desc_eol(desc); | |
646 | ||
568f7f0c | 647 | first->txd.flags = flags; /* client is in control of this ack */ |
dc78baa2 NF |
648 | |
649 | return &first->txd; | |
650 | ||
651 | err_desc_get: | |
652 | atc_desc_put(atchan, first); | |
653 | return NULL; | |
654 | } | |
655 | ||
808347f6 NF |
656 | |
657 | /** | |
658 | * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction | |
659 | * @chan: DMA channel | |
660 | * @sgl: scatterlist to transfer to/from | |
661 | * @sg_len: number of entries in @scatterlist | |
662 | * @direction: DMA direction | |
663 | * @flags: tx descriptor status flags | |
664 | */ | |
665 | static struct dma_async_tx_descriptor * | |
666 | atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, | |
667 | unsigned int sg_len, enum dma_data_direction direction, | |
668 | unsigned long flags) | |
669 | { | |
670 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
671 | struct at_dma_slave *atslave = chan->private; | |
672 | struct at_desc *first = NULL; | |
673 | struct at_desc *prev = NULL; | |
674 | u32 ctrla; | |
675 | u32 ctrlb; | |
676 | dma_addr_t reg; | |
677 | unsigned int reg_width; | |
678 | unsigned int mem_width; | |
679 | unsigned int i; | |
680 | struct scatterlist *sg; | |
681 | size_t total_len = 0; | |
682 | ||
cc52a10a NF |
683 | dev_vdbg(chan2dev(chan), "prep_slave_sg (%d): %s f0x%lx\n", |
684 | sg_len, | |
808347f6 NF |
685 | direction == DMA_TO_DEVICE ? "TO DEVICE" : "FROM DEVICE", |
686 | flags); | |
687 | ||
688 | if (unlikely(!atslave || !sg_len)) { | |
689 | dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n"); | |
690 | return NULL; | |
691 | } | |
692 | ||
693 | reg_width = atslave->reg_width; | |
694 | ||
808347f6 NF |
695 | ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla; |
696 | ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN; | |
697 | ||
698 | switch (direction) { | |
699 | case DMA_TO_DEVICE: | |
700 | ctrla |= ATC_DST_WIDTH(reg_width); | |
701 | ctrlb |= ATC_DST_ADDR_MODE_FIXED | |
702 | | ATC_SRC_ADDR_MODE_INCR | |
703 | | ATC_FC_MEM2PER; | |
704 | reg = atslave->tx_reg; | |
705 | for_each_sg(sgl, sg, sg_len, i) { | |
706 | struct at_desc *desc; | |
707 | u32 len; | |
708 | u32 mem; | |
709 | ||
710 | desc = atc_desc_get(atchan); | |
711 | if (!desc) | |
712 | goto err_desc_get; | |
713 | ||
0f70e8ce | 714 | mem = sg_dma_address(sg); |
808347f6 NF |
715 | len = sg_dma_len(sg); |
716 | mem_width = 2; | |
717 | if (unlikely(mem & 3 || len & 3)) | |
718 | mem_width = 0; | |
719 | ||
720 | desc->lli.saddr = mem; | |
721 | desc->lli.daddr = reg; | |
722 | desc->lli.ctrla = ctrla | |
723 | | ATC_SRC_WIDTH(mem_width) | |
724 | | len >> mem_width; | |
725 | desc->lli.ctrlb = ctrlb; | |
726 | ||
727 | if (!first) { | |
728 | first = desc; | |
729 | } else { | |
730 | /* inform the HW lli about chaining */ | |
731 | prev->lli.dscr = desc->txd.phys; | |
732 | /* insert the link descriptor to the LD ring */ | |
733 | list_add_tail(&desc->desc_node, | |
285a3c71 | 734 | &first->tx_list); |
808347f6 NF |
735 | } |
736 | prev = desc; | |
737 | total_len += len; | |
738 | } | |
739 | break; | |
740 | case DMA_FROM_DEVICE: | |
741 | ctrla |= ATC_SRC_WIDTH(reg_width); | |
742 | ctrlb |= ATC_DST_ADDR_MODE_INCR | |
743 | | ATC_SRC_ADDR_MODE_FIXED | |
744 | | ATC_FC_PER2MEM; | |
745 | ||
746 | reg = atslave->rx_reg; | |
747 | for_each_sg(sgl, sg, sg_len, i) { | |
748 | struct at_desc *desc; | |
749 | u32 len; | |
750 | u32 mem; | |
751 | ||
752 | desc = atc_desc_get(atchan); | |
753 | if (!desc) | |
754 | goto err_desc_get; | |
755 | ||
0f70e8ce | 756 | mem = sg_dma_address(sg); |
808347f6 NF |
757 | len = sg_dma_len(sg); |
758 | mem_width = 2; | |
759 | if (unlikely(mem & 3 || len & 3)) | |
760 | mem_width = 0; | |
761 | ||
762 | desc->lli.saddr = reg; | |
763 | desc->lli.daddr = mem; | |
764 | desc->lli.ctrla = ctrla | |
765 | | ATC_DST_WIDTH(mem_width) | |
59a609d9 | 766 | | len >> reg_width; |
808347f6 NF |
767 | desc->lli.ctrlb = ctrlb; |
768 | ||
769 | if (!first) { | |
770 | first = desc; | |
771 | } else { | |
772 | /* inform the HW lli about chaining */ | |
773 | prev->lli.dscr = desc->txd.phys; | |
774 | /* insert the link descriptor to the LD ring */ | |
775 | list_add_tail(&desc->desc_node, | |
285a3c71 | 776 | &first->tx_list); |
808347f6 NF |
777 | } |
778 | prev = desc; | |
779 | total_len += len; | |
780 | } | |
781 | break; | |
782 | default: | |
783 | return NULL; | |
784 | } | |
785 | ||
786 | /* set end-of-link to the last link descriptor of list*/ | |
787 | set_desc_eol(prev); | |
788 | ||
789 | /* First descriptor of the chain embedds additional information */ | |
790 | first->txd.cookie = -EBUSY; | |
791 | first->len = total_len; | |
792 | ||
568f7f0c NF |
793 | /* first link descriptor of list is responsible of flags */ |
794 | first->txd.flags = flags; /* client is in control of this ack */ | |
808347f6 NF |
795 | |
796 | return &first->txd; | |
797 | ||
798 | err_desc_get: | |
799 | dev_err(chan2dev(chan), "not enough descriptors available\n"); | |
800 | atc_desc_put(atchan, first); | |
801 | return NULL; | |
802 | } | |
803 | ||
53830cc7 NF |
804 | /** |
805 | * atc_dma_cyclic_check_values | |
806 | * Check for too big/unaligned periods and unaligned DMA buffer | |
807 | */ | |
808 | static int | |
809 | atc_dma_cyclic_check_values(unsigned int reg_width, dma_addr_t buf_addr, | |
810 | size_t period_len, enum dma_data_direction direction) | |
811 | { | |
812 | if (period_len > (ATC_BTSIZE_MAX << reg_width)) | |
813 | goto err_out; | |
814 | if (unlikely(period_len & ((1 << reg_width) - 1))) | |
815 | goto err_out; | |
816 | if (unlikely(buf_addr & ((1 << reg_width) - 1))) | |
817 | goto err_out; | |
818 | if (unlikely(!(direction & (DMA_TO_DEVICE | DMA_FROM_DEVICE)))) | |
819 | goto err_out; | |
820 | ||
821 | return 0; | |
822 | ||
823 | err_out: | |
824 | return -EINVAL; | |
825 | } | |
826 | ||
827 | /** | |
828 | * atc_dma_cyclic_fill_desc - Fill one period decriptor | |
829 | */ | |
830 | static int | |
831 | atc_dma_cyclic_fill_desc(struct at_dma_slave *atslave, struct at_desc *desc, | |
832 | unsigned int period_index, dma_addr_t buf_addr, | |
833 | size_t period_len, enum dma_data_direction direction) | |
834 | { | |
835 | u32 ctrla; | |
836 | unsigned int reg_width = atslave->reg_width; | |
837 | ||
838 | /* prepare common CRTLA value */ | |
839 | ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla | |
840 | | ATC_DST_WIDTH(reg_width) | |
841 | | ATC_SRC_WIDTH(reg_width) | |
842 | | period_len >> reg_width; | |
843 | ||
844 | switch (direction) { | |
845 | case DMA_TO_DEVICE: | |
846 | desc->lli.saddr = buf_addr + (period_len * period_index); | |
847 | desc->lli.daddr = atslave->tx_reg; | |
848 | desc->lli.ctrla = ctrla; | |
849 | desc->lli.ctrlb = ATC_DEFAULT_CTRLB | |
850 | | ATC_DST_ADDR_MODE_FIXED | |
851 | | ATC_SRC_ADDR_MODE_INCR | |
852 | | ATC_FC_MEM2PER; | |
853 | break; | |
854 | ||
855 | case DMA_FROM_DEVICE: | |
856 | desc->lli.saddr = atslave->rx_reg; | |
857 | desc->lli.daddr = buf_addr + (period_len * period_index); | |
858 | desc->lli.ctrla = ctrla; | |
859 | desc->lli.ctrlb = ATC_DEFAULT_CTRLB | |
860 | | ATC_DST_ADDR_MODE_INCR | |
861 | | ATC_SRC_ADDR_MODE_FIXED | |
862 | | ATC_FC_PER2MEM; | |
863 | break; | |
864 | ||
865 | default: | |
866 | return -EINVAL; | |
867 | } | |
868 | ||
869 | return 0; | |
870 | } | |
871 | ||
872 | /** | |
873 | * atc_prep_dma_cyclic - prepare the cyclic DMA transfer | |
874 | * @chan: the DMA channel to prepare | |
875 | * @buf_addr: physical DMA address where the buffer starts | |
876 | * @buf_len: total number of bytes for the entire buffer | |
877 | * @period_len: number of bytes for each period | |
878 | * @direction: transfer direction, to or from device | |
879 | */ | |
880 | static struct dma_async_tx_descriptor * | |
881 | atc_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len, | |
882 | size_t period_len, enum dma_data_direction direction) | |
883 | { | |
884 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
885 | struct at_dma_slave *atslave = chan->private; | |
886 | struct at_desc *first = NULL; | |
887 | struct at_desc *prev = NULL; | |
888 | unsigned long was_cyclic; | |
889 | unsigned int periods = buf_len / period_len; | |
890 | unsigned int i; | |
891 | ||
892 | dev_vdbg(chan2dev(chan), "prep_dma_cyclic: %s buf@0x%08x - %d (%d/%d)\n", | |
893 | direction == DMA_TO_DEVICE ? "TO DEVICE" : "FROM DEVICE", | |
894 | buf_addr, | |
895 | periods, buf_len, period_len); | |
896 | ||
897 | if (unlikely(!atslave || !buf_len || !period_len)) { | |
898 | dev_dbg(chan2dev(chan), "prep_dma_cyclic: length is zero!\n"); | |
899 | return NULL; | |
900 | } | |
901 | ||
902 | was_cyclic = test_and_set_bit(ATC_IS_CYCLIC, &atchan->status); | |
903 | if (was_cyclic) { | |
904 | dev_dbg(chan2dev(chan), "prep_dma_cyclic: channel in use!\n"); | |
905 | return NULL; | |
906 | } | |
907 | ||
908 | /* Check for too big/unaligned periods and unaligned DMA buffer */ | |
909 | if (atc_dma_cyclic_check_values(atslave->reg_width, buf_addr, | |
910 | period_len, direction)) | |
911 | goto err_out; | |
912 | ||
913 | /* build cyclic linked list */ | |
914 | for (i = 0; i < periods; i++) { | |
915 | struct at_desc *desc; | |
916 | ||
917 | desc = atc_desc_get(atchan); | |
918 | if (!desc) | |
919 | goto err_desc_get; | |
920 | ||
921 | if (atc_dma_cyclic_fill_desc(atslave, desc, i, buf_addr, | |
922 | period_len, direction)) | |
923 | goto err_desc_get; | |
924 | ||
925 | atc_desc_chain(&first, &prev, desc); | |
926 | } | |
927 | ||
928 | /* lets make a cyclic list */ | |
929 | prev->lli.dscr = first->txd.phys; | |
930 | ||
931 | /* First descriptor of the chain embedds additional information */ | |
932 | first->txd.cookie = -EBUSY; | |
933 | first->len = buf_len; | |
934 | ||
935 | return &first->txd; | |
936 | ||
937 | err_desc_get: | |
938 | dev_err(chan2dev(chan), "not enough descriptors available\n"); | |
939 | atc_desc_put(atchan, first); | |
940 | err_out: | |
941 | clear_bit(ATC_IS_CYCLIC, &atchan->status); | |
942 | return NULL; | |
943 | } | |
944 | ||
945 | ||
05827630 LW |
946 | static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, |
947 | unsigned long arg) | |
808347f6 NF |
948 | { |
949 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
950 | struct at_dma *atdma = to_at_dma(chan->device); | |
951 | struct at_desc *desc, *_desc; | |
952 | LIST_HEAD(list); | |
953 | ||
c3635c78 LW |
954 | /* Only supports DMA_TERMINATE_ALL */ |
955 | if (cmd != DMA_TERMINATE_ALL) | |
956 | return -ENXIO; | |
957 | ||
808347f6 NF |
958 | /* |
959 | * This is only called when something went wrong elsewhere, so | |
960 | * we don't really care about the data. Just disable the | |
961 | * channel. We still have to poll the channel enable bit due | |
962 | * to AHB/HSB limitations. | |
963 | */ | |
964 | spin_lock_bh(&atchan->lock); | |
965 | ||
966 | dma_writel(atdma, CHDR, atchan->mask); | |
967 | ||
968 | /* confirm that this channel is disabled */ | |
969 | while (dma_readl(atdma, CHSR) & atchan->mask) | |
970 | cpu_relax(); | |
971 | ||
972 | /* active_list entries will end up before queued entries */ | |
973 | list_splice_init(&atchan->queue, &list); | |
974 | list_splice_init(&atchan->active_list, &list); | |
975 | ||
808347f6 NF |
976 | /* Flush all pending and queued descriptors */ |
977 | list_for_each_entry_safe(desc, _desc, &list, desc_node) | |
978 | atc_chain_complete(atchan, desc); | |
c3635c78 | 979 | |
53830cc7 NF |
980 | /* if channel dedicated to cyclic operations, free it */ |
981 | clear_bit(ATC_IS_CYCLIC, &atchan->status); | |
982 | ||
b0ebeb9c YW |
983 | spin_unlock_bh(&atchan->lock); |
984 | ||
c3635c78 | 985 | return 0; |
808347f6 NF |
986 | } |
987 | ||
dc78baa2 | 988 | /** |
07934481 | 989 | * atc_tx_status - poll for transaction completion |
dc78baa2 NF |
990 | * @chan: DMA channel |
991 | * @cookie: transaction identifier to check status of | |
07934481 | 992 | * @txstate: if not %NULL updated with transaction state |
dc78baa2 | 993 | * |
07934481 | 994 | * If @txstate is passed in, upon return it reflect the driver |
dc78baa2 NF |
995 | * internal state and can be used with dma_async_is_complete() to check |
996 | * the status of multiple cookies without re-checking hardware state. | |
997 | */ | |
998 | static enum dma_status | |
07934481 | 999 | atc_tx_status(struct dma_chan *chan, |
dc78baa2 | 1000 | dma_cookie_t cookie, |
07934481 | 1001 | struct dma_tx_state *txstate) |
dc78baa2 NF |
1002 | { |
1003 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
1004 | dma_cookie_t last_used; | |
1005 | dma_cookie_t last_complete; | |
1006 | enum dma_status ret; | |
1007 | ||
4297a462 | 1008 | spin_lock_bh(&atchan->lock); |
dc78baa2 NF |
1009 | |
1010 | last_complete = atchan->completed_cookie; | |
1011 | last_used = chan->cookie; | |
1012 | ||
1013 | ret = dma_async_is_complete(cookie, last_complete, last_used); | |
1014 | if (ret != DMA_SUCCESS) { | |
1015 | atc_cleanup_descriptors(atchan); | |
1016 | ||
1017 | last_complete = atchan->completed_cookie; | |
1018 | last_used = chan->cookie; | |
1019 | ||
1020 | ret = dma_async_is_complete(cookie, last_complete, last_used); | |
1021 | } | |
1022 | ||
4297a462 | 1023 | spin_unlock_bh(&atchan->lock); |
dc78baa2 | 1024 | |
bca34692 | 1025 | dma_set_tx_state(txstate, last_complete, last_used, 0); |
07934481 LW |
1026 | dev_vdbg(chan2dev(chan), "tx_status: %d (d%d, u%d)\n", |
1027 | cookie, last_complete ? last_complete : 0, | |
1028 | last_used ? last_used : 0); | |
dc78baa2 NF |
1029 | |
1030 | return ret; | |
1031 | } | |
1032 | ||
1033 | /** | |
1034 | * atc_issue_pending - try to finish work | |
1035 | * @chan: target DMA channel | |
1036 | */ | |
1037 | static void atc_issue_pending(struct dma_chan *chan) | |
1038 | { | |
1039 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
1040 | ||
1041 | dev_vdbg(chan2dev(chan), "issue_pending\n"); | |
1042 | ||
53830cc7 NF |
1043 | /* Not needed for cyclic transfers */ |
1044 | if (test_bit(ATC_IS_CYCLIC, &atchan->status)) | |
1045 | return; | |
1046 | ||
dda36f98 | 1047 | spin_lock_bh(&atchan->lock); |
dc78baa2 | 1048 | if (!atc_chan_is_enabled(atchan)) { |
dc78baa2 | 1049 | atc_advance_work(atchan); |
dc78baa2 | 1050 | } |
dda36f98 | 1051 | spin_unlock_bh(&atchan->lock); |
dc78baa2 NF |
1052 | } |
1053 | ||
1054 | /** | |
1055 | * atc_alloc_chan_resources - allocate resources for DMA channel | |
1056 | * @chan: allocate descriptor resources for this channel | |
1057 | * @client: current client requesting the channel be ready for requests | |
1058 | * | |
1059 | * return - the number of allocated descriptors | |
1060 | */ | |
1061 | static int atc_alloc_chan_resources(struct dma_chan *chan) | |
1062 | { | |
1063 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
1064 | struct at_dma *atdma = to_at_dma(chan->device); | |
1065 | struct at_desc *desc; | |
808347f6 | 1066 | struct at_dma_slave *atslave; |
dc78baa2 | 1067 | int i; |
808347f6 | 1068 | u32 cfg; |
dc78baa2 NF |
1069 | LIST_HEAD(tmp_list); |
1070 | ||
1071 | dev_vdbg(chan2dev(chan), "alloc_chan_resources\n"); | |
1072 | ||
1073 | /* ASSERT: channel is idle */ | |
1074 | if (atc_chan_is_enabled(atchan)) { | |
1075 | dev_dbg(chan2dev(chan), "DMA channel not idle ?\n"); | |
1076 | return -EIO; | |
1077 | } | |
1078 | ||
808347f6 NF |
1079 | cfg = ATC_DEFAULT_CFG; |
1080 | ||
1081 | atslave = chan->private; | |
1082 | if (atslave) { | |
1083 | /* | |
1084 | * We need controller-specific data to set up slave | |
1085 | * transfers. | |
1086 | */ | |
1087 | BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev); | |
1088 | ||
1089 | /* if cfg configuration specified take it instad of default */ | |
1090 | if (atslave->cfg) | |
1091 | cfg = atslave->cfg; | |
1092 | } | |
1093 | ||
1094 | /* have we already been set up? | |
1095 | * reconfigure channel but no need to reallocate descriptors */ | |
dc78baa2 NF |
1096 | if (!list_empty(&atchan->free_list)) |
1097 | return atchan->descs_allocated; | |
1098 | ||
1099 | /* Allocate initial pool of descriptors */ | |
1100 | for (i = 0; i < init_nr_desc_per_channel; i++) { | |
1101 | desc = atc_alloc_descriptor(chan, GFP_KERNEL); | |
1102 | if (!desc) { | |
1103 | dev_err(atdma->dma_common.dev, | |
1104 | "Only %d initial descriptors\n", i); | |
1105 | break; | |
1106 | } | |
1107 | list_add_tail(&desc->desc_node, &tmp_list); | |
1108 | } | |
1109 | ||
1110 | spin_lock_bh(&atchan->lock); | |
1111 | atchan->descs_allocated = i; | |
1112 | list_splice(&tmp_list, &atchan->free_list); | |
1113 | atchan->completed_cookie = chan->cookie = 1; | |
1114 | spin_unlock_bh(&atchan->lock); | |
1115 | ||
1116 | /* channel parameters */ | |
808347f6 | 1117 | channel_writel(atchan, CFG, cfg); |
dc78baa2 NF |
1118 | |
1119 | dev_dbg(chan2dev(chan), | |
1120 | "alloc_chan_resources: allocated %d descriptors\n", | |
1121 | atchan->descs_allocated); | |
1122 | ||
1123 | return atchan->descs_allocated; | |
1124 | } | |
1125 | ||
1126 | /** | |
1127 | * atc_free_chan_resources - free all channel resources | |
1128 | * @chan: DMA channel | |
1129 | */ | |
1130 | static void atc_free_chan_resources(struct dma_chan *chan) | |
1131 | { | |
1132 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
1133 | struct at_dma *atdma = to_at_dma(chan->device); | |
1134 | struct at_desc *desc, *_desc; | |
1135 | LIST_HEAD(list); | |
1136 | ||
1137 | dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n", | |
1138 | atchan->descs_allocated); | |
1139 | ||
1140 | /* ASSERT: channel is idle */ | |
1141 | BUG_ON(!list_empty(&atchan->active_list)); | |
1142 | BUG_ON(!list_empty(&atchan->queue)); | |
1143 | BUG_ON(atc_chan_is_enabled(atchan)); | |
1144 | ||
1145 | list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) { | |
1146 | dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc); | |
1147 | list_del(&desc->desc_node); | |
1148 | /* free link descriptor */ | |
1149 | dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys); | |
1150 | } | |
1151 | list_splice_init(&atchan->free_list, &list); | |
1152 | atchan->descs_allocated = 0; | |
53830cc7 | 1153 | atchan->status = 0; |
dc78baa2 NF |
1154 | |
1155 | dev_vdbg(chan2dev(chan), "free_chan_resources: done\n"); | |
1156 | } | |
1157 | ||
1158 | ||
1159 | /*-- Module Management -----------------------------------------------*/ | |
1160 | ||
1161 | /** | |
1162 | * at_dma_off - disable DMA controller | |
1163 | * @atdma: the Atmel HDAMC device | |
1164 | */ | |
1165 | static void at_dma_off(struct at_dma *atdma) | |
1166 | { | |
1167 | dma_writel(atdma, EN, 0); | |
1168 | ||
1169 | /* disable all interrupts */ | |
1170 | dma_writel(atdma, EBCIDR, -1L); | |
1171 | ||
1172 | /* confirm that all channels are disabled */ | |
1173 | while (dma_readl(atdma, CHSR) & atdma->all_chan_mask) | |
1174 | cpu_relax(); | |
1175 | } | |
1176 | ||
1177 | static int __init at_dma_probe(struct platform_device *pdev) | |
1178 | { | |
1179 | struct at_dma_platform_data *pdata; | |
1180 | struct resource *io; | |
1181 | struct at_dma *atdma; | |
1182 | size_t size; | |
1183 | int irq; | |
1184 | int err; | |
1185 | int i; | |
1186 | ||
1187 | /* get DMA Controller parameters from platform */ | |
1188 | pdata = pdev->dev.platform_data; | |
1189 | if (!pdata || pdata->nr_channels > AT_DMA_MAX_NR_CHANNELS) | |
1190 | return -EINVAL; | |
1191 | ||
1192 | io = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
1193 | if (!io) | |
1194 | return -EINVAL; | |
1195 | ||
1196 | irq = platform_get_irq(pdev, 0); | |
1197 | if (irq < 0) | |
1198 | return irq; | |
1199 | ||
1200 | size = sizeof(struct at_dma); | |
1201 | size += pdata->nr_channels * sizeof(struct at_dma_chan); | |
1202 | atdma = kzalloc(size, GFP_KERNEL); | |
1203 | if (!atdma) | |
1204 | return -ENOMEM; | |
1205 | ||
1206 | /* discover transaction capabilites from the platform data */ | |
1207 | atdma->dma_common.cap_mask = pdata->cap_mask; | |
1208 | atdma->all_chan_mask = (1 << pdata->nr_channels) - 1; | |
1209 | ||
1210 | size = io->end - io->start + 1; | |
1211 | if (!request_mem_region(io->start, size, pdev->dev.driver->name)) { | |
1212 | err = -EBUSY; | |
1213 | goto err_kfree; | |
1214 | } | |
1215 | ||
1216 | atdma->regs = ioremap(io->start, size); | |
1217 | if (!atdma->regs) { | |
1218 | err = -ENOMEM; | |
1219 | goto err_release_r; | |
1220 | } | |
1221 | ||
1222 | atdma->clk = clk_get(&pdev->dev, "dma_clk"); | |
1223 | if (IS_ERR(atdma->clk)) { | |
1224 | err = PTR_ERR(atdma->clk); | |
1225 | goto err_clk; | |
1226 | } | |
1227 | clk_enable(atdma->clk); | |
1228 | ||
1229 | /* force dma off, just in case */ | |
1230 | at_dma_off(atdma); | |
1231 | ||
1232 | err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma); | |
1233 | if (err) | |
1234 | goto err_irq; | |
1235 | ||
1236 | platform_set_drvdata(pdev, atdma); | |
1237 | ||
1238 | /* create a pool of consistent memory blocks for hardware descriptors */ | |
1239 | atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool", | |
1240 | &pdev->dev, sizeof(struct at_desc), | |
1241 | 4 /* word alignment */, 0); | |
1242 | if (!atdma->dma_desc_pool) { | |
1243 | dev_err(&pdev->dev, "No memory for descriptors dma pool\n"); | |
1244 | err = -ENOMEM; | |
1245 | goto err_pool_create; | |
1246 | } | |
1247 | ||
1248 | /* clear any pending interrupt */ | |
1249 | while (dma_readl(atdma, EBCISR)) | |
1250 | cpu_relax(); | |
1251 | ||
1252 | /* initialize channels related values */ | |
1253 | INIT_LIST_HEAD(&atdma->dma_common.channels); | |
1254 | for (i = 0; i < pdata->nr_channels; i++, atdma->dma_common.chancnt++) { | |
1255 | struct at_dma_chan *atchan = &atdma->chan[i]; | |
1256 | ||
1257 | atchan->chan_common.device = &atdma->dma_common; | |
1258 | atchan->chan_common.cookie = atchan->completed_cookie = 1; | |
1259 | atchan->chan_common.chan_id = i; | |
1260 | list_add_tail(&atchan->chan_common.device_node, | |
1261 | &atdma->dma_common.channels); | |
1262 | ||
1263 | atchan->ch_regs = atdma->regs + ch_regs(i); | |
1264 | spin_lock_init(&atchan->lock); | |
1265 | atchan->mask = 1 << i; | |
1266 | ||
1267 | INIT_LIST_HEAD(&atchan->active_list); | |
1268 | INIT_LIST_HEAD(&atchan->queue); | |
1269 | INIT_LIST_HEAD(&atchan->free_list); | |
1270 | ||
1271 | tasklet_init(&atchan->tasklet, atc_tasklet, | |
1272 | (unsigned long)atchan); | |
1273 | atc_enable_irq(atchan); | |
1274 | } | |
1275 | ||
1276 | /* set base routines */ | |
1277 | atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources; | |
1278 | atdma->dma_common.device_free_chan_resources = atc_free_chan_resources; | |
07934481 | 1279 | atdma->dma_common.device_tx_status = atc_tx_status; |
dc78baa2 NF |
1280 | atdma->dma_common.device_issue_pending = atc_issue_pending; |
1281 | atdma->dma_common.dev = &pdev->dev; | |
1282 | ||
1283 | /* set prep routines based on capability */ | |
1284 | if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask)) | |
1285 | atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy; | |
1286 | ||
53830cc7 | 1287 | if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) |
808347f6 | 1288 | atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg; |
53830cc7 NF |
1289 | |
1290 | if (dma_has_cap(DMA_CYCLIC, atdma->dma_common.cap_mask)) | |
1291 | atdma->dma_common.device_prep_dma_cyclic = atc_prep_dma_cyclic; | |
1292 | ||
1293 | if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) || | |
1294 | dma_has_cap(DMA_CYCLIC, atdma->dma_common.cap_mask)) | |
c3635c78 | 1295 | atdma->dma_common.device_control = atc_control; |
808347f6 | 1296 | |
dc78baa2 NF |
1297 | dma_writel(atdma, EN, AT_DMA_ENABLE); |
1298 | ||
1299 | dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n", | |
1300 | dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "", | |
1301 | dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask) ? "slave " : "", | |
1302 | atdma->dma_common.chancnt); | |
1303 | ||
1304 | dma_async_device_register(&atdma->dma_common); | |
1305 | ||
1306 | return 0; | |
1307 | ||
1308 | err_pool_create: | |
1309 | platform_set_drvdata(pdev, NULL); | |
1310 | free_irq(platform_get_irq(pdev, 0), atdma); | |
1311 | err_irq: | |
1312 | clk_disable(atdma->clk); | |
1313 | clk_put(atdma->clk); | |
1314 | err_clk: | |
1315 | iounmap(atdma->regs); | |
1316 | atdma->regs = NULL; | |
1317 | err_release_r: | |
1318 | release_mem_region(io->start, size); | |
1319 | err_kfree: | |
1320 | kfree(atdma); | |
1321 | return err; | |
1322 | } | |
1323 | ||
1324 | static int __exit at_dma_remove(struct platform_device *pdev) | |
1325 | { | |
1326 | struct at_dma *atdma = platform_get_drvdata(pdev); | |
1327 | struct dma_chan *chan, *_chan; | |
1328 | struct resource *io; | |
1329 | ||
1330 | at_dma_off(atdma); | |
1331 | dma_async_device_unregister(&atdma->dma_common); | |
1332 | ||
1333 | dma_pool_destroy(atdma->dma_desc_pool); | |
1334 | platform_set_drvdata(pdev, NULL); | |
1335 | free_irq(platform_get_irq(pdev, 0), atdma); | |
1336 | ||
1337 | list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels, | |
1338 | device_node) { | |
1339 | struct at_dma_chan *atchan = to_at_dma_chan(chan); | |
1340 | ||
1341 | /* Disable interrupts */ | |
1342 | atc_disable_irq(atchan); | |
1343 | tasklet_disable(&atchan->tasklet); | |
1344 | ||
1345 | tasklet_kill(&atchan->tasklet); | |
1346 | list_del(&chan->device_node); | |
1347 | } | |
1348 | ||
1349 | clk_disable(atdma->clk); | |
1350 | clk_put(atdma->clk); | |
1351 | ||
1352 | iounmap(atdma->regs); | |
1353 | atdma->regs = NULL; | |
1354 | ||
1355 | io = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
1356 | release_mem_region(io->start, io->end - io->start + 1); | |
1357 | ||
1358 | kfree(atdma); | |
1359 | ||
1360 | return 0; | |
1361 | } | |
1362 | ||
1363 | static void at_dma_shutdown(struct platform_device *pdev) | |
1364 | { | |
1365 | struct at_dma *atdma = platform_get_drvdata(pdev); | |
1366 | ||
1367 | at_dma_off(platform_get_drvdata(pdev)); | |
1368 | clk_disable(atdma->clk); | |
1369 | } | |
1370 | ||
33f82d14 | 1371 | static int at_dma_suspend_noirq(struct device *dev) |
dc78baa2 | 1372 | { |
33f82d14 DW |
1373 | struct platform_device *pdev = to_platform_device(dev); |
1374 | struct at_dma *atdma = platform_get_drvdata(pdev); | |
dc78baa2 NF |
1375 | |
1376 | at_dma_off(platform_get_drvdata(pdev)); | |
1377 | clk_disable(atdma->clk); | |
1378 | return 0; | |
1379 | } | |
1380 | ||
33f82d14 | 1381 | static int at_dma_resume_noirq(struct device *dev) |
dc78baa2 | 1382 | { |
33f82d14 DW |
1383 | struct platform_device *pdev = to_platform_device(dev); |
1384 | struct at_dma *atdma = platform_get_drvdata(pdev); | |
dc78baa2 NF |
1385 | |
1386 | clk_enable(atdma->clk); | |
1387 | dma_writel(atdma, EN, AT_DMA_ENABLE); | |
1388 | return 0; | |
dc78baa2 NF |
1389 | } |
1390 | ||
47145210 | 1391 | static const struct dev_pm_ops at_dma_dev_pm_ops = { |
33f82d14 DW |
1392 | .suspend_noirq = at_dma_suspend_noirq, |
1393 | .resume_noirq = at_dma_resume_noirq, | |
1394 | }; | |
1395 | ||
dc78baa2 NF |
1396 | static struct platform_driver at_dma_driver = { |
1397 | .remove = __exit_p(at_dma_remove), | |
1398 | .shutdown = at_dma_shutdown, | |
dc78baa2 NF |
1399 | .driver = { |
1400 | .name = "at_hdmac", | |
33f82d14 | 1401 | .pm = &at_dma_dev_pm_ops, |
dc78baa2 NF |
1402 | }, |
1403 | }; | |
1404 | ||
1405 | static int __init at_dma_init(void) | |
1406 | { | |
1407 | return platform_driver_probe(&at_dma_driver, at_dma_probe); | |
1408 | } | |
93d0bec2 | 1409 | subsys_initcall(at_dma_init); |
dc78baa2 NF |
1410 | |
1411 | static void __exit at_dma_exit(void) | |
1412 | { | |
1413 | platform_driver_unregister(&at_dma_driver); | |
1414 | } | |
1415 | module_exit(at_dma_exit); | |
1416 | ||
1417 | MODULE_DESCRIPTION("Atmel AHB DMA Controller driver"); | |
1418 | MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>"); | |
1419 | MODULE_LICENSE("GPL"); | |
1420 | MODULE_ALIAS("platform:at_hdmac"); |