xref: /openbmc/linux/drivers/dma/at_hdmac.c (revision baa7eb025ab14f3cba2e35c0a8648f9c9f01d24f)
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>
25 #include <linux/slab.h>
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 /**
72  * atc_alloc_descriptor - allocate and return an initialized descriptor
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));
91 		INIT_LIST_HEAD(&desc->tx_list);
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 /**
103  * atc_desc_get - get an unused descriptor from free_list
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);
155 		list_for_each_entry(child, &desc->tx_list, desc_node)
156 			dev_vdbg(chan2dev(&atchan->chan_common),
157 					"moving child desc %p to freelist\n",
158 					child);
159 		list_splice_init(&desc->tx_list, &atchan->free_list);
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 
167 /**
168  * atc_assign_cookie - compute and assign new cookie
169  * @atchan: channel we work on
170  * @desc: descriptor to asign cookie for
171  *
172  * Called with atchan->lock held and bh disabled
173  */
174 static dma_cookie_t
175 atc_assign_cookie(struct at_dma_chan *atchan, struct at_desc *desc)
176 {
177 	dma_cookie_t cookie = atchan->chan_common.cookie;
178 
179 	if (++cookie < 0)
180 		cookie = 1;
181 
182 	atchan->chan_common.cookie = cookie;
183 	desc->txd.cookie = cookie;
184 
185 	return cookie;
186 }
187 
188 /**
189  * atc_dostart - starts the DMA engine for real
190  * @atchan: the channel we want to start
191  * @first: first descriptor in the list we want to begin with
192  *
193  * Called with atchan->lock held and bh disabled
194  */
195 static void atc_dostart(struct at_dma_chan *atchan, struct at_desc *first)
196 {
197 	struct at_dma	*atdma = to_at_dma(atchan->chan_common.device);
198 
199 	/* ASSERT:  channel is idle */
200 	if (atc_chan_is_enabled(atchan)) {
201 		dev_err(chan2dev(&atchan->chan_common),
202 			"BUG: Attempted to start non-idle channel\n");
203 		dev_err(chan2dev(&atchan->chan_common),
204 			"  channel: s0x%x d0x%x ctrl0x%x:0x%x l0x%x\n",
205 			channel_readl(atchan, SADDR),
206 			channel_readl(atchan, DADDR),
207 			channel_readl(atchan, CTRLA),
208 			channel_readl(atchan, CTRLB),
209 			channel_readl(atchan, DSCR));
210 
211 		/* The tasklet will hopefully advance the queue... */
212 		return;
213 	}
214 
215 	vdbg_dump_regs(atchan);
216 
217 	/* clear any pending interrupt */
218 	while (dma_readl(atdma, EBCISR))
219 		cpu_relax();
220 
221 	channel_writel(atchan, SADDR, 0);
222 	channel_writel(atchan, DADDR, 0);
223 	channel_writel(atchan, CTRLA, 0);
224 	channel_writel(atchan, CTRLB, 0);
225 	channel_writel(atchan, DSCR, first->txd.phys);
226 	dma_writel(atdma, CHER, atchan->mask);
227 
228 	vdbg_dump_regs(atchan);
229 }
230 
231 /**
232  * atc_chain_complete - finish work for one transaction chain
233  * @atchan: channel we work on
234  * @desc: descriptor at the head of the chain we want do complete
235  *
236  * Called with atchan->lock held and bh disabled */
237 static void
238 atc_chain_complete(struct at_dma_chan *atchan, struct at_desc *desc)
239 {
240 	dma_async_tx_callback		callback;
241 	void				*param;
242 	struct dma_async_tx_descriptor	*txd = &desc->txd;
243 
244 	dev_vdbg(chan2dev(&atchan->chan_common),
245 		"descriptor %u complete\n", txd->cookie);
246 
247 	atchan->completed_cookie = txd->cookie;
248 	callback = txd->callback;
249 	param = txd->callback_param;
250 
251 	/* move children to free_list */
252 	list_splice_init(&desc->tx_list, &atchan->free_list);
253 	/* move myself to free_list */
254 	list_move(&desc->desc_node, &atchan->free_list);
255 
256 	/* unmap dma addresses */
257 	if (!atchan->chan_common.private) {
258 		struct device *parent = chan2parent(&atchan->chan_common);
259 		if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
260 			if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
261 				dma_unmap_single(parent,
262 						desc->lli.daddr,
263 						desc->len, DMA_FROM_DEVICE);
264 			else
265 				dma_unmap_page(parent,
266 						desc->lli.daddr,
267 						desc->len, DMA_FROM_DEVICE);
268 		}
269 		if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
270 			if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
271 				dma_unmap_single(parent,
272 						desc->lli.saddr,
273 						desc->len, DMA_TO_DEVICE);
274 			else
275 				dma_unmap_page(parent,
276 						desc->lli.saddr,
277 						desc->len, DMA_TO_DEVICE);
278 		}
279 	}
280 
281 	/*
282 	 * The API requires that no submissions are done from a
283 	 * callback, so we don't need to drop the lock here
284 	 */
285 	if (callback)
286 		callback(param);
287 
288 	dma_run_dependencies(txd);
289 }
290 
291 /**
292  * atc_complete_all - finish work for all transactions
293  * @atchan: channel to complete transactions for
294  *
295  * Eventually submit queued descriptors if any
296  *
297  * Assume channel is idle while calling this function
298  * Called with atchan->lock held and bh disabled
299  */
300 static void atc_complete_all(struct at_dma_chan *atchan)
301 {
302 	struct at_desc *desc, *_desc;
303 	LIST_HEAD(list);
304 
305 	dev_vdbg(chan2dev(&atchan->chan_common), "complete all\n");
306 
307 	BUG_ON(atc_chan_is_enabled(atchan));
308 
309 	/*
310 	 * Submit queued descriptors ASAP, i.e. before we go through
311 	 * the completed ones.
312 	 */
313 	if (!list_empty(&atchan->queue))
314 		atc_dostart(atchan, atc_first_queued(atchan));
315 	/* empty active_list now it is completed */
316 	list_splice_init(&atchan->active_list, &list);
317 	/* empty queue list by moving descriptors (if any) to active_list */
318 	list_splice_init(&atchan->queue, &atchan->active_list);
319 
320 	list_for_each_entry_safe(desc, _desc, &list, desc_node)
321 		atc_chain_complete(atchan, desc);
322 }
323 
324 /**
325  * atc_cleanup_descriptors - cleanup up finished descriptors in active_list
326  * @atchan: channel to be cleaned up
327  *
328  * Called with atchan->lock held and bh disabled
329  */
330 static void atc_cleanup_descriptors(struct at_dma_chan *atchan)
331 {
332 	struct at_desc	*desc, *_desc;
333 	struct at_desc	*child;
334 
335 	dev_vdbg(chan2dev(&atchan->chan_common), "cleanup descriptors\n");
336 
337 	list_for_each_entry_safe(desc, _desc, &atchan->active_list, desc_node) {
338 		if (!(desc->lli.ctrla & ATC_DONE))
339 			/* This one is currently in progress */
340 			return;
341 
342 		list_for_each_entry(child, &desc->tx_list, desc_node)
343 			if (!(child->lli.ctrla & ATC_DONE))
344 				/* Currently in progress */
345 				return;
346 
347 		/*
348 		 * No descriptors so far seem to be in progress, i.e.
349 		 * this chain must be done.
350 		 */
351 		atc_chain_complete(atchan, desc);
352 	}
353 }
354 
355 /**
356  * atc_advance_work - at the end of a transaction, move forward
357  * @atchan: channel where the transaction ended
358  *
359  * Called with atchan->lock held and bh disabled
360  */
361 static void atc_advance_work(struct at_dma_chan *atchan)
362 {
363 	dev_vdbg(chan2dev(&atchan->chan_common), "advance_work\n");
364 
365 	if (list_empty(&atchan->active_list) ||
366 	    list_is_singular(&atchan->active_list)) {
367 		atc_complete_all(atchan);
368 	} else {
369 		atc_chain_complete(atchan, atc_first_active(atchan));
370 		/* advance work */
371 		atc_dostart(atchan, atc_first_active(atchan));
372 	}
373 }
374 
375 
376 /**
377  * atc_handle_error - handle errors reported by DMA controller
378  * @atchan: channel where error occurs
379  *
380  * Called with atchan->lock held and bh disabled
381  */
382 static void atc_handle_error(struct at_dma_chan *atchan)
383 {
384 	struct at_desc *bad_desc;
385 	struct at_desc *child;
386 
387 	/*
388 	 * The descriptor currently at the head of the active list is
389 	 * broked. Since we don't have any way to report errors, we'll
390 	 * just have to scream loudly and try to carry on.
391 	 */
392 	bad_desc = atc_first_active(atchan);
393 	list_del_init(&bad_desc->desc_node);
394 
395 	/* As we are stopped, take advantage to push queued descriptors
396 	 * in active_list */
397 	list_splice_init(&atchan->queue, atchan->active_list.prev);
398 
399 	/* Try to restart the controller */
400 	if (!list_empty(&atchan->active_list))
401 		atc_dostart(atchan, atc_first_active(atchan));
402 
403 	/*
404 	 * KERN_CRITICAL may seem harsh, but since this only happens
405 	 * when someone submits a bad physical address in a
406 	 * descriptor, we should consider ourselves lucky that the
407 	 * controller flagged an error instead of scribbling over
408 	 * random memory locations.
409 	 */
410 	dev_crit(chan2dev(&atchan->chan_common),
411 			"Bad descriptor submitted for DMA!\n");
412 	dev_crit(chan2dev(&atchan->chan_common),
413 			"  cookie: %d\n", bad_desc->txd.cookie);
414 	atc_dump_lli(atchan, &bad_desc->lli);
415 	list_for_each_entry(child, &bad_desc->tx_list, desc_node)
416 		atc_dump_lli(atchan, &child->lli);
417 
418 	/* Pretend the descriptor completed successfully */
419 	atc_chain_complete(atchan, bad_desc);
420 }
421 
422 
423 /*--  IRQ & Tasklet  ---------------------------------------------------*/
424 
425 static void atc_tasklet(unsigned long data)
426 {
427 	struct at_dma_chan *atchan = (struct at_dma_chan *)data;
428 
429 	/* Channel cannot be enabled here */
430 	if (atc_chan_is_enabled(atchan)) {
431 		dev_err(chan2dev(&atchan->chan_common),
432 			"BUG: channel enabled in tasklet\n");
433 		return;
434 	}
435 
436 	spin_lock(&atchan->lock);
437 	if (test_and_clear_bit(0, &atchan->error_status))
438 		atc_handle_error(atchan);
439 	else
440 		atc_advance_work(atchan);
441 
442 	spin_unlock(&atchan->lock);
443 }
444 
445 static irqreturn_t at_dma_interrupt(int irq, void *dev_id)
446 {
447 	struct at_dma		*atdma = (struct at_dma *)dev_id;
448 	struct at_dma_chan	*atchan;
449 	int			i;
450 	u32			status, pending, imr;
451 	int			ret = IRQ_NONE;
452 
453 	do {
454 		imr = dma_readl(atdma, EBCIMR);
455 		status = dma_readl(atdma, EBCISR);
456 		pending = status & imr;
457 
458 		if (!pending)
459 			break;
460 
461 		dev_vdbg(atdma->dma_common.dev,
462 			"interrupt: status = 0x%08x, 0x%08x, 0x%08x\n",
463 			 status, imr, pending);
464 
465 		for (i = 0; i < atdma->dma_common.chancnt; i++) {
466 			atchan = &atdma->chan[i];
467 			if (pending & (AT_DMA_CBTC(i) | AT_DMA_ERR(i))) {
468 				if (pending & AT_DMA_ERR(i)) {
469 					/* Disable channel on AHB error */
470 					dma_writel(atdma, CHDR, atchan->mask);
471 					/* Give information to tasklet */
472 					set_bit(0, &atchan->error_status);
473 				}
474 				tasklet_schedule(&atchan->tasklet);
475 				ret = IRQ_HANDLED;
476 			}
477 		}
478 
479 	} while (pending);
480 
481 	return ret;
482 }
483 
484 
485 /*--  DMA Engine API  --------------------------------------------------*/
486 
487 /**
488  * atc_tx_submit - set the prepared descriptor(s) to be executed by the engine
489  * @desc: descriptor at the head of the transaction chain
490  *
491  * Queue chain if DMA engine is working already
492  *
493  * Cookie increment and adding to active_list or queue must be atomic
494  */
495 static dma_cookie_t atc_tx_submit(struct dma_async_tx_descriptor *tx)
496 {
497 	struct at_desc		*desc = txd_to_at_desc(tx);
498 	struct at_dma_chan	*atchan = to_at_dma_chan(tx->chan);
499 	dma_cookie_t		cookie;
500 
501 	spin_lock_bh(&atchan->lock);
502 	cookie = atc_assign_cookie(atchan, desc);
503 
504 	if (list_empty(&atchan->active_list)) {
505 		dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
506 				desc->txd.cookie);
507 		atc_dostart(atchan, desc);
508 		list_add_tail(&desc->desc_node, &atchan->active_list);
509 	} else {
510 		dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
511 				desc->txd.cookie);
512 		list_add_tail(&desc->desc_node, &atchan->queue);
513 	}
514 
515 	spin_unlock_bh(&atchan->lock);
516 
517 	return cookie;
518 }
519 
520 /**
521  * atc_prep_dma_memcpy - prepare a memcpy operation
522  * @chan: the channel to prepare operation on
523  * @dest: operation virtual destination address
524  * @src: operation virtual source address
525  * @len: operation length
526  * @flags: tx descriptor status flags
527  */
528 static struct dma_async_tx_descriptor *
529 atc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
530 		size_t len, unsigned long flags)
531 {
532 	struct at_dma_chan	*atchan = to_at_dma_chan(chan);
533 	struct at_desc		*desc = NULL;
534 	struct at_desc		*first = NULL;
535 	struct at_desc		*prev = NULL;
536 	size_t			xfer_count;
537 	size_t			offset;
538 	unsigned int		src_width;
539 	unsigned int		dst_width;
540 	u32			ctrla;
541 	u32			ctrlb;
542 
543 	dev_vdbg(chan2dev(chan), "prep_dma_memcpy: d0x%x s0x%x l0x%zx f0x%lx\n",
544 			dest, src, len, flags);
545 
546 	if (unlikely(!len)) {
547 		dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
548 		return NULL;
549 	}
550 
551 	ctrla =   ATC_DEFAULT_CTRLA;
552 	ctrlb =   ATC_DEFAULT_CTRLB
553 		| ATC_SRC_ADDR_MODE_INCR
554 		| ATC_DST_ADDR_MODE_INCR
555 		| ATC_FC_MEM2MEM;
556 
557 	/*
558 	 * We can be a lot more clever here, but this should take care
559 	 * of the most common optimization.
560 	 */
561 	if (!((src | dest  | len) & 3)) {
562 		ctrla |= ATC_SRC_WIDTH_WORD | ATC_DST_WIDTH_WORD;
563 		src_width = dst_width = 2;
564 	} else if (!((src | dest | len) & 1)) {
565 		ctrla |= ATC_SRC_WIDTH_HALFWORD | ATC_DST_WIDTH_HALFWORD;
566 		src_width = dst_width = 1;
567 	} else {
568 		ctrla |= ATC_SRC_WIDTH_BYTE | ATC_DST_WIDTH_BYTE;
569 		src_width = dst_width = 0;
570 	}
571 
572 	for (offset = 0; offset < len; offset += xfer_count << src_width) {
573 		xfer_count = min_t(size_t, (len - offset) >> src_width,
574 				ATC_BTSIZE_MAX);
575 
576 		desc = atc_desc_get(atchan);
577 		if (!desc)
578 			goto err_desc_get;
579 
580 		desc->lli.saddr = src + offset;
581 		desc->lli.daddr = dest + offset;
582 		desc->lli.ctrla = ctrla | xfer_count;
583 		desc->lli.ctrlb = ctrlb;
584 
585 		desc->txd.cookie = 0;
586 		async_tx_ack(&desc->txd);
587 
588 		if (!first) {
589 			first = desc;
590 		} else {
591 			/* inform the HW lli about chaining */
592 			prev->lli.dscr = desc->txd.phys;
593 			/* insert the link descriptor to the LD ring */
594 			list_add_tail(&desc->desc_node,
595 					&first->tx_list);
596 		}
597 		prev = desc;
598 	}
599 
600 	/* First descriptor of the chain embedds additional information */
601 	first->txd.cookie = -EBUSY;
602 	first->len = len;
603 
604 	/* set end-of-link to the last link descriptor of list*/
605 	set_desc_eol(desc);
606 
607 	desc->txd.flags = flags; /* client is in control of this ack */
608 
609 	return &first->txd;
610 
611 err_desc_get:
612 	atc_desc_put(atchan, first);
613 	return NULL;
614 }
615 
616 
617 /**
618  * atc_prep_slave_sg - prepare descriptors for a DMA_SLAVE transaction
619  * @chan: DMA channel
620  * @sgl: scatterlist to transfer to/from
621  * @sg_len: number of entries in @scatterlist
622  * @direction: DMA direction
623  * @flags: tx descriptor status flags
624  */
625 static struct dma_async_tx_descriptor *
626 atc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
627 		unsigned int sg_len, enum dma_data_direction direction,
628 		unsigned long flags)
629 {
630 	struct at_dma_chan	*atchan = to_at_dma_chan(chan);
631 	struct at_dma_slave	*atslave = chan->private;
632 	struct at_desc		*first = NULL;
633 	struct at_desc		*prev = NULL;
634 	u32			ctrla;
635 	u32			ctrlb;
636 	dma_addr_t		reg;
637 	unsigned int		reg_width;
638 	unsigned int		mem_width;
639 	unsigned int		i;
640 	struct scatterlist	*sg;
641 	size_t			total_len = 0;
642 
643 	dev_vdbg(chan2dev(chan), "prep_slave_sg: %s f0x%lx\n",
644 			direction == DMA_TO_DEVICE ? "TO DEVICE" : "FROM DEVICE",
645 			flags);
646 
647 	if (unlikely(!atslave || !sg_len)) {
648 		dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
649 		return NULL;
650 	}
651 
652 	reg_width = atslave->reg_width;
653 
654 	ctrla = ATC_DEFAULT_CTRLA | atslave->ctrla;
655 	ctrlb = ATC_DEFAULT_CTRLB | ATC_IEN;
656 
657 	switch (direction) {
658 	case DMA_TO_DEVICE:
659 		ctrla |=  ATC_DST_WIDTH(reg_width);
660 		ctrlb |=  ATC_DST_ADDR_MODE_FIXED
661 			| ATC_SRC_ADDR_MODE_INCR
662 			| ATC_FC_MEM2PER;
663 		reg = atslave->tx_reg;
664 		for_each_sg(sgl, sg, sg_len, i) {
665 			struct at_desc	*desc;
666 			u32		len;
667 			u32		mem;
668 
669 			desc = atc_desc_get(atchan);
670 			if (!desc)
671 				goto err_desc_get;
672 
673 			mem = sg_phys(sg);
674 			len = sg_dma_len(sg);
675 			mem_width = 2;
676 			if (unlikely(mem & 3 || len & 3))
677 				mem_width = 0;
678 
679 			desc->lli.saddr = mem;
680 			desc->lli.daddr = reg;
681 			desc->lli.ctrla = ctrla
682 					| ATC_SRC_WIDTH(mem_width)
683 					| len >> mem_width;
684 			desc->lli.ctrlb = ctrlb;
685 
686 			if (!first) {
687 				first = desc;
688 			} else {
689 				/* inform the HW lli about chaining */
690 				prev->lli.dscr = desc->txd.phys;
691 				/* insert the link descriptor to the LD ring */
692 				list_add_tail(&desc->desc_node,
693 						&first->tx_list);
694 			}
695 			prev = desc;
696 			total_len += len;
697 		}
698 		break;
699 	case DMA_FROM_DEVICE:
700 		ctrla |=  ATC_SRC_WIDTH(reg_width);
701 		ctrlb |=  ATC_DST_ADDR_MODE_INCR
702 			| ATC_SRC_ADDR_MODE_FIXED
703 			| ATC_FC_PER2MEM;
704 
705 		reg = atslave->rx_reg;
706 		for_each_sg(sgl, sg, sg_len, i) {
707 			struct at_desc	*desc;
708 			u32		len;
709 			u32		mem;
710 
711 			desc = atc_desc_get(atchan);
712 			if (!desc)
713 				goto err_desc_get;
714 
715 			mem = sg_phys(sg);
716 			len = sg_dma_len(sg);
717 			mem_width = 2;
718 			if (unlikely(mem & 3 || len & 3))
719 				mem_width = 0;
720 
721 			desc->lli.saddr = reg;
722 			desc->lli.daddr = mem;
723 			desc->lli.ctrla = ctrla
724 					| ATC_DST_WIDTH(mem_width)
725 					| len >> reg_width;
726 			desc->lli.ctrlb = ctrlb;
727 
728 			if (!first) {
729 				first = desc;
730 			} else {
731 				/* inform the HW lli about chaining */
732 				prev->lli.dscr = desc->txd.phys;
733 				/* insert the link descriptor to the LD ring */
734 				list_add_tail(&desc->desc_node,
735 						&first->tx_list);
736 			}
737 			prev = desc;
738 			total_len += len;
739 		}
740 		break;
741 	default:
742 		return NULL;
743 	}
744 
745 	/* set end-of-link to the last link descriptor of list*/
746 	set_desc_eol(prev);
747 
748 	/* First descriptor of the chain embedds additional information */
749 	first->txd.cookie = -EBUSY;
750 	first->len = total_len;
751 
752 	/* last link descriptor of list is responsible of flags */
753 	prev->txd.flags = flags; /* client is in control of this ack */
754 
755 	return &first->txd;
756 
757 err_desc_get:
758 	dev_err(chan2dev(chan), "not enough descriptors available\n");
759 	atc_desc_put(atchan, first);
760 	return NULL;
761 }
762 
763 static int atc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
764 		       unsigned long arg)
765 {
766 	struct at_dma_chan	*atchan = to_at_dma_chan(chan);
767 	struct at_dma		*atdma = to_at_dma(chan->device);
768 	struct at_desc		*desc, *_desc;
769 	LIST_HEAD(list);
770 
771 	/* Only supports DMA_TERMINATE_ALL */
772 	if (cmd != DMA_TERMINATE_ALL)
773 		return -ENXIO;
774 
775 	/*
776 	 * This is only called when something went wrong elsewhere, so
777 	 * we don't really care about the data. Just disable the
778 	 * channel. We still have to poll the channel enable bit due
779 	 * to AHB/HSB limitations.
780 	 */
781 	spin_lock_bh(&atchan->lock);
782 
783 	dma_writel(atdma, CHDR, atchan->mask);
784 
785 	/* confirm that this channel is disabled */
786 	while (dma_readl(atdma, CHSR) & atchan->mask)
787 		cpu_relax();
788 
789 	/* active_list entries will end up before queued entries */
790 	list_splice_init(&atchan->queue, &list);
791 	list_splice_init(&atchan->active_list, &list);
792 
793 	/* Flush all pending and queued descriptors */
794 	list_for_each_entry_safe(desc, _desc, &list, desc_node)
795 		atc_chain_complete(atchan, desc);
796 
797 	spin_unlock_bh(&atchan->lock);
798 
799 	return 0;
800 }
801 
802 /**
803  * atc_tx_status - poll for transaction completion
804  * @chan: DMA channel
805  * @cookie: transaction identifier to check status of
806  * @txstate: if not %NULL updated with transaction state
807  *
808  * If @txstate is passed in, upon return it reflect the driver
809  * internal state and can be used with dma_async_is_complete() to check
810  * the status of multiple cookies without re-checking hardware state.
811  */
812 static enum dma_status
813 atc_tx_status(struct dma_chan *chan,
814 		dma_cookie_t cookie,
815 		struct dma_tx_state *txstate)
816 {
817 	struct at_dma_chan	*atchan = to_at_dma_chan(chan);
818 	dma_cookie_t		last_used;
819 	dma_cookie_t		last_complete;
820 	enum dma_status		ret;
821 
822 	spin_lock_bh(&atchan->lock);
823 
824 	last_complete = atchan->completed_cookie;
825 	last_used = chan->cookie;
826 
827 	ret = dma_async_is_complete(cookie, last_complete, last_used);
828 	if (ret != DMA_SUCCESS) {
829 		atc_cleanup_descriptors(atchan);
830 
831 		last_complete = atchan->completed_cookie;
832 		last_used = chan->cookie;
833 
834 		ret = dma_async_is_complete(cookie, last_complete, last_used);
835 	}
836 
837 	spin_unlock_bh(&atchan->lock);
838 
839 	dma_set_tx_state(txstate, last_complete, last_used, 0);
840 	dev_vdbg(chan2dev(chan), "tx_status: %d (d%d, u%d)\n",
841 		 cookie, last_complete ? last_complete : 0,
842 		 last_used ? last_used : 0);
843 
844 	return ret;
845 }
846 
847 /**
848  * atc_issue_pending - try to finish work
849  * @chan: target DMA channel
850  */
851 static void atc_issue_pending(struct dma_chan *chan)
852 {
853 	struct at_dma_chan	*atchan = to_at_dma_chan(chan);
854 
855 	dev_vdbg(chan2dev(chan), "issue_pending\n");
856 
857 	if (!atc_chan_is_enabled(atchan)) {
858 		spin_lock_bh(&atchan->lock);
859 		atc_advance_work(atchan);
860 		spin_unlock_bh(&atchan->lock);
861 	}
862 }
863 
864 /**
865  * atc_alloc_chan_resources - allocate resources for DMA channel
866  * @chan: allocate descriptor resources for this channel
867  * @client: current client requesting the channel be ready for requests
868  *
869  * return - the number of allocated descriptors
870  */
871 static int atc_alloc_chan_resources(struct dma_chan *chan)
872 {
873 	struct at_dma_chan	*atchan = to_at_dma_chan(chan);
874 	struct at_dma		*atdma = to_at_dma(chan->device);
875 	struct at_desc		*desc;
876 	struct at_dma_slave	*atslave;
877 	int			i;
878 	u32			cfg;
879 	LIST_HEAD(tmp_list);
880 
881 	dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
882 
883 	/* ASSERT:  channel is idle */
884 	if (atc_chan_is_enabled(atchan)) {
885 		dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
886 		return -EIO;
887 	}
888 
889 	cfg = ATC_DEFAULT_CFG;
890 
891 	atslave = chan->private;
892 	if (atslave) {
893 		/*
894 		 * We need controller-specific data to set up slave
895 		 * transfers.
896 		 */
897 		BUG_ON(!atslave->dma_dev || atslave->dma_dev != atdma->dma_common.dev);
898 
899 		/* if cfg configuration specified take it instad of default */
900 		if (atslave->cfg)
901 			cfg = atslave->cfg;
902 	}
903 
904 	/* have we already been set up?
905 	 * reconfigure channel but no need to reallocate descriptors */
906 	if (!list_empty(&atchan->free_list))
907 		return atchan->descs_allocated;
908 
909 	/* Allocate initial pool of descriptors */
910 	for (i = 0; i < init_nr_desc_per_channel; i++) {
911 		desc = atc_alloc_descriptor(chan, GFP_KERNEL);
912 		if (!desc) {
913 			dev_err(atdma->dma_common.dev,
914 				"Only %d initial descriptors\n", i);
915 			break;
916 		}
917 		list_add_tail(&desc->desc_node, &tmp_list);
918 	}
919 
920 	spin_lock_bh(&atchan->lock);
921 	atchan->descs_allocated = i;
922 	list_splice(&tmp_list, &atchan->free_list);
923 	atchan->completed_cookie = chan->cookie = 1;
924 	spin_unlock_bh(&atchan->lock);
925 
926 	/* channel parameters */
927 	channel_writel(atchan, CFG, cfg);
928 
929 	dev_dbg(chan2dev(chan),
930 		"alloc_chan_resources: allocated %d descriptors\n",
931 		atchan->descs_allocated);
932 
933 	return atchan->descs_allocated;
934 }
935 
936 /**
937  * atc_free_chan_resources - free all channel resources
938  * @chan: DMA channel
939  */
940 static void atc_free_chan_resources(struct dma_chan *chan)
941 {
942 	struct at_dma_chan	*atchan = to_at_dma_chan(chan);
943 	struct at_dma		*atdma = to_at_dma(chan->device);
944 	struct at_desc		*desc, *_desc;
945 	LIST_HEAD(list);
946 
947 	dev_dbg(chan2dev(chan), "free_chan_resources: (descs allocated=%u)\n",
948 		atchan->descs_allocated);
949 
950 	/* ASSERT:  channel is idle */
951 	BUG_ON(!list_empty(&atchan->active_list));
952 	BUG_ON(!list_empty(&atchan->queue));
953 	BUG_ON(atc_chan_is_enabled(atchan));
954 
955 	list_for_each_entry_safe(desc, _desc, &atchan->free_list, desc_node) {
956 		dev_vdbg(chan2dev(chan), "  freeing descriptor %p\n", desc);
957 		list_del(&desc->desc_node);
958 		/* free link descriptor */
959 		dma_pool_free(atdma->dma_desc_pool, desc, desc->txd.phys);
960 	}
961 	list_splice_init(&atchan->free_list, &list);
962 	atchan->descs_allocated = 0;
963 
964 	dev_vdbg(chan2dev(chan), "free_chan_resources: done\n");
965 }
966 
967 
968 /*--  Module Management  -----------------------------------------------*/
969 
970 /**
971  * at_dma_off - disable DMA controller
972  * @atdma: the Atmel HDAMC device
973  */
974 static void at_dma_off(struct at_dma *atdma)
975 {
976 	dma_writel(atdma, EN, 0);
977 
978 	/* disable all interrupts */
979 	dma_writel(atdma, EBCIDR, -1L);
980 
981 	/* confirm that all channels are disabled */
982 	while (dma_readl(atdma, CHSR) & atdma->all_chan_mask)
983 		cpu_relax();
984 }
985 
986 static int __init at_dma_probe(struct platform_device *pdev)
987 {
988 	struct at_dma_platform_data *pdata;
989 	struct resource		*io;
990 	struct at_dma		*atdma;
991 	size_t			size;
992 	int			irq;
993 	int			err;
994 	int			i;
995 
996 	/* get DMA Controller parameters from platform */
997 	pdata = pdev->dev.platform_data;
998 	if (!pdata || pdata->nr_channels > AT_DMA_MAX_NR_CHANNELS)
999 		return -EINVAL;
1000 
1001 	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1002 	if (!io)
1003 		return -EINVAL;
1004 
1005 	irq = platform_get_irq(pdev, 0);
1006 	if (irq < 0)
1007 		return irq;
1008 
1009 	size = sizeof(struct at_dma);
1010 	size += pdata->nr_channels * sizeof(struct at_dma_chan);
1011 	atdma = kzalloc(size, GFP_KERNEL);
1012 	if (!atdma)
1013 		return -ENOMEM;
1014 
1015 	/* discover transaction capabilites from the platform data */
1016 	atdma->dma_common.cap_mask = pdata->cap_mask;
1017 	atdma->all_chan_mask = (1 << pdata->nr_channels) - 1;
1018 
1019 	size = io->end - io->start + 1;
1020 	if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1021 		err = -EBUSY;
1022 		goto err_kfree;
1023 	}
1024 
1025 	atdma->regs = ioremap(io->start, size);
1026 	if (!atdma->regs) {
1027 		err = -ENOMEM;
1028 		goto err_release_r;
1029 	}
1030 
1031 	atdma->clk = clk_get(&pdev->dev, "dma_clk");
1032 	if (IS_ERR(atdma->clk)) {
1033 		err = PTR_ERR(atdma->clk);
1034 		goto err_clk;
1035 	}
1036 	clk_enable(atdma->clk);
1037 
1038 	/* force dma off, just in case */
1039 	at_dma_off(atdma);
1040 
1041 	err = request_irq(irq, at_dma_interrupt, 0, "at_hdmac", atdma);
1042 	if (err)
1043 		goto err_irq;
1044 
1045 	platform_set_drvdata(pdev, atdma);
1046 
1047 	/* create a pool of consistent memory blocks for hardware descriptors */
1048 	atdma->dma_desc_pool = dma_pool_create("at_hdmac_desc_pool",
1049 			&pdev->dev, sizeof(struct at_desc),
1050 			4 /* word alignment */, 0);
1051 	if (!atdma->dma_desc_pool) {
1052 		dev_err(&pdev->dev, "No memory for descriptors dma pool\n");
1053 		err = -ENOMEM;
1054 		goto err_pool_create;
1055 	}
1056 
1057 	/* clear any pending interrupt */
1058 	while (dma_readl(atdma, EBCISR))
1059 		cpu_relax();
1060 
1061 	/* initialize channels related values */
1062 	INIT_LIST_HEAD(&atdma->dma_common.channels);
1063 	for (i = 0; i < pdata->nr_channels; i++, atdma->dma_common.chancnt++) {
1064 		struct at_dma_chan	*atchan = &atdma->chan[i];
1065 
1066 		atchan->chan_common.device = &atdma->dma_common;
1067 		atchan->chan_common.cookie = atchan->completed_cookie = 1;
1068 		atchan->chan_common.chan_id = i;
1069 		list_add_tail(&atchan->chan_common.device_node,
1070 				&atdma->dma_common.channels);
1071 
1072 		atchan->ch_regs = atdma->regs + ch_regs(i);
1073 		spin_lock_init(&atchan->lock);
1074 		atchan->mask = 1 << i;
1075 
1076 		INIT_LIST_HEAD(&atchan->active_list);
1077 		INIT_LIST_HEAD(&atchan->queue);
1078 		INIT_LIST_HEAD(&atchan->free_list);
1079 
1080 		tasklet_init(&atchan->tasklet, atc_tasklet,
1081 				(unsigned long)atchan);
1082 		atc_enable_irq(atchan);
1083 	}
1084 
1085 	/* set base routines */
1086 	atdma->dma_common.device_alloc_chan_resources = atc_alloc_chan_resources;
1087 	atdma->dma_common.device_free_chan_resources = atc_free_chan_resources;
1088 	atdma->dma_common.device_tx_status = atc_tx_status;
1089 	atdma->dma_common.device_issue_pending = atc_issue_pending;
1090 	atdma->dma_common.dev = &pdev->dev;
1091 
1092 	/* set prep routines based on capability */
1093 	if (dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask))
1094 		atdma->dma_common.device_prep_dma_memcpy = atc_prep_dma_memcpy;
1095 
1096 	if (dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)) {
1097 		atdma->dma_common.device_prep_slave_sg = atc_prep_slave_sg;
1098 		atdma->dma_common.device_control = atc_control;
1099 	}
1100 
1101 	dma_writel(atdma, EN, AT_DMA_ENABLE);
1102 
1103 	dev_info(&pdev->dev, "Atmel AHB DMA Controller ( %s%s), %d channels\n",
1104 	  dma_has_cap(DMA_MEMCPY, atdma->dma_common.cap_mask) ? "cpy " : "",
1105 	  dma_has_cap(DMA_SLAVE, atdma->dma_common.cap_mask)  ? "slave " : "",
1106 	  atdma->dma_common.chancnt);
1107 
1108 	dma_async_device_register(&atdma->dma_common);
1109 
1110 	return 0;
1111 
1112 err_pool_create:
1113 	platform_set_drvdata(pdev, NULL);
1114 	free_irq(platform_get_irq(pdev, 0), atdma);
1115 err_irq:
1116 	clk_disable(atdma->clk);
1117 	clk_put(atdma->clk);
1118 err_clk:
1119 	iounmap(atdma->regs);
1120 	atdma->regs = NULL;
1121 err_release_r:
1122 	release_mem_region(io->start, size);
1123 err_kfree:
1124 	kfree(atdma);
1125 	return err;
1126 }
1127 
1128 static int __exit at_dma_remove(struct platform_device *pdev)
1129 {
1130 	struct at_dma		*atdma = platform_get_drvdata(pdev);
1131 	struct dma_chan		*chan, *_chan;
1132 	struct resource		*io;
1133 
1134 	at_dma_off(atdma);
1135 	dma_async_device_unregister(&atdma->dma_common);
1136 
1137 	dma_pool_destroy(atdma->dma_desc_pool);
1138 	platform_set_drvdata(pdev, NULL);
1139 	free_irq(platform_get_irq(pdev, 0), atdma);
1140 
1141 	list_for_each_entry_safe(chan, _chan, &atdma->dma_common.channels,
1142 			device_node) {
1143 		struct at_dma_chan	*atchan = to_at_dma_chan(chan);
1144 
1145 		/* Disable interrupts */
1146 		atc_disable_irq(atchan);
1147 		tasklet_disable(&atchan->tasklet);
1148 
1149 		tasklet_kill(&atchan->tasklet);
1150 		list_del(&chan->device_node);
1151 	}
1152 
1153 	clk_disable(atdma->clk);
1154 	clk_put(atdma->clk);
1155 
1156 	iounmap(atdma->regs);
1157 	atdma->regs = NULL;
1158 
1159 	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1160 	release_mem_region(io->start, io->end - io->start + 1);
1161 
1162 	kfree(atdma);
1163 
1164 	return 0;
1165 }
1166 
1167 static void at_dma_shutdown(struct platform_device *pdev)
1168 {
1169 	struct at_dma	*atdma = platform_get_drvdata(pdev);
1170 
1171 	at_dma_off(platform_get_drvdata(pdev));
1172 	clk_disable(atdma->clk);
1173 }
1174 
1175 static int at_dma_suspend_noirq(struct device *dev)
1176 {
1177 	struct platform_device *pdev = to_platform_device(dev);
1178 	struct at_dma *atdma = platform_get_drvdata(pdev);
1179 
1180 	at_dma_off(platform_get_drvdata(pdev));
1181 	clk_disable(atdma->clk);
1182 	return 0;
1183 }
1184 
1185 static int at_dma_resume_noirq(struct device *dev)
1186 {
1187 	struct platform_device *pdev = to_platform_device(dev);
1188 	struct at_dma *atdma = platform_get_drvdata(pdev);
1189 
1190 	clk_enable(atdma->clk);
1191 	dma_writel(atdma, EN, AT_DMA_ENABLE);
1192 	return 0;
1193 }
1194 
1195 static const struct dev_pm_ops at_dma_dev_pm_ops = {
1196 	.suspend_noirq = at_dma_suspend_noirq,
1197 	.resume_noirq = at_dma_resume_noirq,
1198 };
1199 
1200 static struct platform_driver at_dma_driver = {
1201 	.remove		= __exit_p(at_dma_remove),
1202 	.shutdown	= at_dma_shutdown,
1203 	.driver = {
1204 		.name	= "at_hdmac",
1205 		.pm	= &at_dma_dev_pm_ops,
1206 	},
1207 };
1208 
1209 static int __init at_dma_init(void)
1210 {
1211 	return platform_driver_probe(&at_dma_driver, at_dma_probe);
1212 }
1213 module_init(at_dma_init);
1214 
1215 static void __exit at_dma_exit(void)
1216 {
1217 	platform_driver_unregister(&at_dma_driver);
1218 }
1219 module_exit(at_dma_exit);
1220 
1221 MODULE_DESCRIPTION("Atmel AHB DMA Controller driver");
1222 MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
1223 MODULE_LICENSE("GPL");
1224 MODULE_ALIAS("platform:at_hdmac");
1225