xref: /linux/drivers/dma/txx9dmac.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for the TXx9 SoC DMA Controller
4  *
5  * Copyright (C) 2009 Atsushi Nemoto
6  */
7 #include <linux/dma-mapping.h>
8 #include <linux/init.h>
9 #include <linux/interrupt.h>
10 #include <linux/io.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/scatterlist.h>
15 
16 #include "dmaengine.h"
17 #include "txx9dmac.h"
18 
19 static struct txx9dmac_chan *to_txx9dmac_chan(struct dma_chan *chan)
20 {
21 	return container_of(chan, struct txx9dmac_chan, chan);
22 }
23 
24 static struct txx9dmac_cregs __iomem *__dma_regs(const struct txx9dmac_chan *dc)
25 {
26 	return dc->ch_regs;
27 }
28 
29 static struct txx9dmac_cregs32 __iomem *__dma_regs32(
30 	const struct txx9dmac_chan *dc)
31 {
32 	return dc->ch_regs;
33 }
34 
35 #define channel64_readq(dc, name) \
36 	__raw_readq(&(__dma_regs(dc)->name))
37 #define channel64_writeq(dc, name, val) \
38 	__raw_writeq((val), &(__dma_regs(dc)->name))
39 #define channel64_readl(dc, name) \
40 	__raw_readl(&(__dma_regs(dc)->name))
41 #define channel64_writel(dc, name, val) \
42 	__raw_writel((val), &(__dma_regs(dc)->name))
43 
44 #define channel32_readl(dc, name) \
45 	__raw_readl(&(__dma_regs32(dc)->name))
46 #define channel32_writel(dc, name, val) \
47 	__raw_writel((val), &(__dma_regs32(dc)->name))
48 
49 #define channel_readq(dc, name) channel64_readq(dc, name)
50 #define channel_writeq(dc, name, val) channel64_writeq(dc, name, val)
51 #define channel_readl(dc, name) \
52 	(is_dmac64(dc) ? \
53 	 channel64_readl(dc, name) : channel32_readl(dc, name))
54 #define channel_writel(dc, name, val) \
55 	(is_dmac64(dc) ? \
56 	 channel64_writel(dc, name, val) : channel32_writel(dc, name, val))
57 
58 static dma_addr_t channel64_read_CHAR(const struct txx9dmac_chan *dc)
59 {
60 	if (sizeof(__dma_regs(dc)->CHAR) == sizeof(u64))
61 		return channel64_readq(dc, CHAR);
62 	else
63 		return channel64_readl(dc, CHAR);
64 }
65 
66 static void channel64_write_CHAR(const struct txx9dmac_chan *dc, dma_addr_t val)
67 {
68 	if (sizeof(__dma_regs(dc)->CHAR) == sizeof(u64))
69 		channel64_writeq(dc, CHAR, val);
70 	else
71 		channel64_writel(dc, CHAR, val);
72 }
73 
74 static void channel64_clear_CHAR(const struct txx9dmac_chan *dc)
75 {
76 #if defined(CONFIG_32BIT) && !defined(CONFIG_PHYS_ADDR_T_64BIT)
77 	channel64_writel(dc, CHAR, 0);
78 	channel64_writel(dc, __pad_CHAR, 0);
79 #else
80 	channel64_writeq(dc, CHAR, 0);
81 #endif
82 }
83 
84 static dma_addr_t channel_read_CHAR(const struct txx9dmac_chan *dc)
85 {
86 	if (is_dmac64(dc))
87 		return channel64_read_CHAR(dc);
88 	else
89 		return channel32_readl(dc, CHAR);
90 }
91 
92 static void channel_write_CHAR(const struct txx9dmac_chan *dc, dma_addr_t val)
93 {
94 	if (is_dmac64(dc))
95 		channel64_write_CHAR(dc, val);
96 	else
97 		channel32_writel(dc, CHAR, val);
98 }
99 
100 static struct txx9dmac_regs __iomem *__txx9dmac_regs(
101 	const struct txx9dmac_dev *ddev)
102 {
103 	return ddev->regs;
104 }
105 
106 static struct txx9dmac_regs32 __iomem *__txx9dmac_regs32(
107 	const struct txx9dmac_dev *ddev)
108 {
109 	return ddev->regs;
110 }
111 
112 #define dma64_readl(ddev, name) \
113 	__raw_readl(&(__txx9dmac_regs(ddev)->name))
114 #define dma64_writel(ddev, name, val) \
115 	__raw_writel((val), &(__txx9dmac_regs(ddev)->name))
116 
117 #define dma32_readl(ddev, name) \
118 	__raw_readl(&(__txx9dmac_regs32(ddev)->name))
119 #define dma32_writel(ddev, name, val) \
120 	__raw_writel((val), &(__txx9dmac_regs32(ddev)->name))
121 
122 #define dma_readl(ddev, name) \
123 	(__is_dmac64(ddev) ? \
124 	dma64_readl(ddev, name) : dma32_readl(ddev, name))
125 #define dma_writel(ddev, name, val) \
126 	(__is_dmac64(ddev) ? \
127 	dma64_writel(ddev, name, val) : dma32_writel(ddev, name, val))
128 static struct device *chan2parent(struct dma_chan *chan)
129 {
130 	return dmaengine_chan_dev(chan)->parent;
131 }
132 
133 static struct txx9dmac_desc *
134 txd_to_txx9dmac_desc(struct dma_async_tx_descriptor *txd)
135 {
136 	return container_of(txd, struct txx9dmac_desc, txd);
137 }
138 
139 static dma_addr_t desc_read_CHAR(const struct txx9dmac_chan *dc,
140 				 const struct txx9dmac_desc *desc)
141 {
142 	return is_dmac64(dc) ? desc->hwdesc.CHAR : desc->hwdesc32.CHAR;
143 }
144 
145 static void desc_write_CHAR(const struct txx9dmac_chan *dc,
146 			    struct txx9dmac_desc *desc, dma_addr_t val)
147 {
148 	if (is_dmac64(dc))
149 		desc->hwdesc.CHAR = val;
150 	else
151 		desc->hwdesc32.CHAR = val;
152 }
153 
154 #define TXX9_DMA_MAX_COUNT	0x04000000
155 
156 #define TXX9_DMA_INITIAL_DESC_COUNT	64
157 
158 static struct txx9dmac_desc *txx9dmac_first_active(struct txx9dmac_chan *dc)
159 {
160 	return list_entry(dc->active_list.next,
161 			  struct txx9dmac_desc, desc_node);
162 }
163 
164 static struct txx9dmac_desc *txx9dmac_last_active(struct txx9dmac_chan *dc)
165 {
166 	return list_entry(dc->active_list.prev,
167 			  struct txx9dmac_desc, desc_node);
168 }
169 
170 static struct txx9dmac_desc *txx9dmac_first_queued(struct txx9dmac_chan *dc)
171 {
172 	return list_entry(dc->queue.next, struct txx9dmac_desc, desc_node);
173 }
174 
175 static struct txx9dmac_desc *txx9dmac_last_child(struct txx9dmac_desc *desc)
176 {
177 	if (!list_empty(&desc->tx_list))
178 		desc = list_entry(desc->tx_list.prev, typeof(*desc), desc_node);
179 	return desc;
180 }
181 
182 static dma_cookie_t txx9dmac_tx_submit(struct dma_async_tx_descriptor *tx);
183 
184 static struct txx9dmac_desc *txx9dmac_desc_alloc(struct txx9dmac_chan *dc,
185 						 gfp_t flags)
186 {
187 	struct txx9dmac_dev *ddev = dc->ddev;
188 	struct txx9dmac_desc *desc;
189 
190 	desc = kzalloc_obj(*desc, flags);
191 	if (!desc)
192 		return NULL;
193 	INIT_LIST_HEAD(&desc->tx_list);
194 	dma_async_tx_descriptor_init(&desc->txd, &dc->chan);
195 	desc->txd.tx_submit = txx9dmac_tx_submit;
196 	/* txd.flags will be overwritten in prep funcs */
197 	desc->txd.flags = DMA_CTRL_ACK;
198 	desc->txd.phys = dma_map_single(chan2parent(&dc->chan), &desc->hwdesc,
199 					ddev->descsize, DMA_TO_DEVICE);
200 	return desc;
201 }
202 
203 static struct txx9dmac_desc *txx9dmac_desc_get(struct txx9dmac_chan *dc)
204 {
205 	struct txx9dmac_desc *desc, *_desc;
206 	struct txx9dmac_desc *ret = NULL;
207 	unsigned int i = 0;
208 
209 	spin_lock_bh(&dc->lock);
210 	list_for_each_entry_safe(desc, _desc, &dc->free_list, desc_node) {
211 		if (async_tx_test_ack(&desc->txd)) {
212 			list_del(&desc->desc_node);
213 			ret = desc;
214 			break;
215 		}
216 		dev_dbg(dmaengine_chan_dev(&dc->chan), "desc %p not ACKed\n", desc);
217 		i++;
218 	}
219 	spin_unlock_bh(&dc->lock);
220 
221 	dev_vdbg(dmaengine_chan_dev(&dc->chan), "scanned %u descriptors on freelist\n",
222 		 i);
223 	if (!ret) {
224 		ret = txx9dmac_desc_alloc(dc, GFP_ATOMIC);
225 		if (ret) {
226 			spin_lock_bh(&dc->lock);
227 			dc->descs_allocated++;
228 			spin_unlock_bh(&dc->lock);
229 		} else
230 			dev_err(dmaengine_chan_dev(&dc->chan),
231 				"not enough descriptors available\n");
232 	}
233 	return ret;
234 }
235 
236 static void txx9dmac_sync_desc_for_cpu(struct txx9dmac_chan *dc,
237 				       struct txx9dmac_desc *desc)
238 {
239 	struct txx9dmac_dev *ddev = dc->ddev;
240 	struct txx9dmac_desc *child;
241 
242 	list_for_each_entry(child, &desc->tx_list, desc_node)
243 		dma_sync_single_for_cpu(chan2parent(&dc->chan),
244 				child->txd.phys, ddev->descsize,
245 				DMA_TO_DEVICE);
246 	dma_sync_single_for_cpu(chan2parent(&dc->chan),
247 			desc->txd.phys, ddev->descsize,
248 			DMA_TO_DEVICE);
249 }
250 
251 /*
252  * Move a descriptor, including any children, to the free list.
253  * `desc' must not be on any lists.
254  */
255 static void txx9dmac_desc_put(struct txx9dmac_chan *dc,
256 			      struct txx9dmac_desc *desc)
257 {
258 	if (desc) {
259 		struct txx9dmac_desc *child;
260 
261 		txx9dmac_sync_desc_for_cpu(dc, desc);
262 
263 		spin_lock_bh(&dc->lock);
264 		list_for_each_entry(child, &desc->tx_list, desc_node)
265 			dev_vdbg(dmaengine_chan_dev(&dc->chan),
266 				 "moving child desc %p to freelist\n",
267 				 child);
268 		list_splice_init(&desc->tx_list, &dc->free_list);
269 		dev_vdbg(dmaengine_chan_dev(&dc->chan), "moving desc %p to freelist\n",
270 			 desc);
271 		list_add(&desc->desc_node, &dc->free_list);
272 		spin_unlock_bh(&dc->lock);
273 	}
274 }
275 
276 /*----------------------------------------------------------------------*/
277 
278 static void txx9dmac_dump_regs(struct txx9dmac_chan *dc)
279 {
280 	if (is_dmac64(dc))
281 		dev_err(dmaengine_chan_dev(&dc->chan),
282 			"  CHAR: %#llx SAR: %#llx DAR: %#llx CNTR: %#x"
283 			" SAIR: %#x DAIR: %#x CCR: %#x CSR: %#x\n",
284 			(u64)channel64_read_CHAR(dc),
285 			channel64_readq(dc, SAR),
286 			channel64_readq(dc, DAR),
287 			channel64_readl(dc, CNTR),
288 			channel64_readl(dc, SAIR),
289 			channel64_readl(dc, DAIR),
290 			channel64_readl(dc, CCR),
291 			channel64_readl(dc, CSR));
292 	else
293 		dev_err(dmaengine_chan_dev(&dc->chan),
294 			"  CHAR: %#x SAR: %#x DAR: %#x CNTR: %#x"
295 			" SAIR: %#x DAIR: %#x CCR: %#x CSR: %#x\n",
296 			channel32_readl(dc, CHAR),
297 			channel32_readl(dc, SAR),
298 			channel32_readl(dc, DAR),
299 			channel32_readl(dc, CNTR),
300 			channel32_readl(dc, SAIR),
301 			channel32_readl(dc, DAIR),
302 			channel32_readl(dc, CCR),
303 			channel32_readl(dc, CSR));
304 }
305 
306 static void txx9dmac_reset_chan(struct txx9dmac_chan *dc)
307 {
308 	channel_writel(dc, CCR, TXX9_DMA_CCR_CHRST);
309 	if (is_dmac64(dc)) {
310 		channel64_clear_CHAR(dc);
311 		channel_writeq(dc, SAR, 0);
312 		channel_writeq(dc, DAR, 0);
313 	} else {
314 		channel_writel(dc, CHAR, 0);
315 		channel_writel(dc, SAR, 0);
316 		channel_writel(dc, DAR, 0);
317 	}
318 	channel_writel(dc, CNTR, 0);
319 	channel_writel(dc, SAIR, 0);
320 	channel_writel(dc, DAIR, 0);
321 	channel_writel(dc, CCR, 0);
322 }
323 
324 /* Called with dc->lock held and bh disabled */
325 static void txx9dmac_dostart(struct txx9dmac_chan *dc,
326 			     struct txx9dmac_desc *first)
327 {
328 	struct txx9dmac_slave *ds = dc->chan.private;
329 	u32 sai, dai;
330 
331 	dev_vdbg(dmaengine_chan_dev(&dc->chan), "dostart %u %p\n",
332 		 first->txd.cookie, first);
333 	/* ASSERT:  channel is idle */
334 	if (channel_readl(dc, CSR) & TXX9_DMA_CSR_XFACT) {
335 		dev_err(dmaengine_chan_dev(&dc->chan),
336 			"BUG: Attempted to start non-idle channel\n");
337 		txx9dmac_dump_regs(dc);
338 		/* The tasklet will hopefully advance the queue... */
339 		return;
340 	}
341 
342 	if (is_dmac64(dc)) {
343 		channel64_writel(dc, CNTR, 0);
344 		channel64_writel(dc, CSR, 0xffffffff);
345 		if (ds) {
346 			if (ds->tx_reg) {
347 				sai = ds->reg_width;
348 				dai = 0;
349 			} else {
350 				sai = 0;
351 				dai = ds->reg_width;
352 			}
353 		} else {
354 			sai = 8;
355 			dai = 8;
356 		}
357 		channel64_writel(dc, SAIR, sai);
358 		channel64_writel(dc, DAIR, dai);
359 		/* All 64-bit DMAC supports SMPCHN */
360 		channel64_writel(dc, CCR, dc->ccr);
361 		/* Writing a non zero value to CHAR will assert XFACT */
362 		channel64_write_CHAR(dc, first->txd.phys);
363 	} else {
364 		channel32_writel(dc, CNTR, 0);
365 		channel32_writel(dc, CSR, 0xffffffff);
366 		if (ds) {
367 			if (ds->tx_reg) {
368 				sai = ds->reg_width;
369 				dai = 0;
370 			} else {
371 				sai = 0;
372 				dai = ds->reg_width;
373 			}
374 		} else {
375 			sai = 4;
376 			dai = 4;
377 		}
378 		channel32_writel(dc, SAIR, sai);
379 		channel32_writel(dc, DAIR, dai);
380 		if (txx9_dma_have_SMPCHN()) {
381 			channel32_writel(dc, CCR, dc->ccr);
382 			/* Writing a non zero value to CHAR will assert XFACT */
383 			channel32_writel(dc, CHAR, first->txd.phys);
384 		} else {
385 			channel32_writel(dc, CHAR, first->txd.phys);
386 			channel32_writel(dc, CCR, dc->ccr);
387 		}
388 	}
389 }
390 
391 /*----------------------------------------------------------------------*/
392 
393 static void
394 txx9dmac_descriptor_complete(struct txx9dmac_chan *dc,
395 			     struct txx9dmac_desc *desc)
396 {
397 	struct dmaengine_desc_callback cb;
398 	struct dma_async_tx_descriptor *txd = &desc->txd;
399 
400 	dev_vdbg(dmaengine_chan_dev(&dc->chan), "descriptor %u %p complete\n",
401 		 txd->cookie, desc);
402 
403 	dma_cookie_complete(txd);
404 	dmaengine_desc_get_callback(txd, &cb);
405 
406 	txx9dmac_sync_desc_for_cpu(dc, desc);
407 	list_splice_init(&desc->tx_list, &dc->free_list);
408 	list_move(&desc->desc_node, &dc->free_list);
409 
410 	dma_descriptor_unmap(txd);
411 	/*
412 	 * The API requires that no submissions are done from a
413 	 * callback, so we don't need to drop the lock here
414 	 */
415 	dmaengine_desc_callback_invoke(&cb, NULL);
416 	dma_run_dependencies(txd);
417 }
418 
419 static void txx9dmac_dequeue(struct txx9dmac_chan *dc, struct list_head *list)
420 {
421 	struct txx9dmac_dev *ddev = dc->ddev;
422 	struct txx9dmac_desc *desc;
423 	struct txx9dmac_desc *prev = NULL;
424 
425 	BUG_ON(!list_empty(list));
426 	do {
427 		desc = txx9dmac_first_queued(dc);
428 		if (prev) {
429 			desc_write_CHAR(dc, prev, desc->txd.phys);
430 			dma_sync_single_for_device(chan2parent(&dc->chan),
431 				prev->txd.phys, ddev->descsize,
432 				DMA_TO_DEVICE);
433 		}
434 		prev = txx9dmac_last_child(desc);
435 		list_move_tail(&desc->desc_node, list);
436 		/* Make chain-completion interrupt happen */
437 		if ((desc->txd.flags & DMA_PREP_INTERRUPT) &&
438 		    !txx9dmac_chan_INTENT(dc))
439 			break;
440 	} while (!list_empty(&dc->queue));
441 }
442 
443 static void txx9dmac_complete_all(struct txx9dmac_chan *dc)
444 {
445 	struct txx9dmac_desc *desc, *_desc;
446 	LIST_HEAD(list);
447 
448 	/*
449 	 * Submit queued descriptors ASAP, i.e. before we go through
450 	 * the completed ones.
451 	 */
452 	list_splice_init(&dc->active_list, &list);
453 	if (!list_empty(&dc->queue)) {
454 		txx9dmac_dequeue(dc, &dc->active_list);
455 		txx9dmac_dostart(dc, txx9dmac_first_active(dc));
456 	}
457 
458 	list_for_each_entry_safe(desc, _desc, &list, desc_node)
459 		txx9dmac_descriptor_complete(dc, desc);
460 }
461 
462 static void txx9dmac_dump_desc(struct txx9dmac_chan *dc,
463 			       struct txx9dmac_hwdesc *desc)
464 {
465 	if (is_dmac64(dc)) {
466 #ifdef TXX9_DMA_USE_SIMPLE_CHAIN
467 		dev_crit(dmaengine_chan_dev(&dc->chan),
468 			 "  desc: ch%#llx s%#llx d%#llx c%#x\n",
469 			 (u64)desc->CHAR, desc->SAR, desc->DAR, desc->CNTR);
470 #else
471 		dev_crit(dmaengine_chan_dev(&dc->chan),
472 			 "  desc: ch%#llx s%#llx d%#llx c%#x"
473 			 " si%#x di%#x cc%#x cs%#x\n",
474 			 (u64)desc->CHAR, desc->SAR, desc->DAR, desc->CNTR,
475 			 desc->SAIR, desc->DAIR, desc->CCR, desc->CSR);
476 #endif
477 	} else {
478 		struct txx9dmac_hwdesc32 *d = (struct txx9dmac_hwdesc32 *)desc;
479 #ifdef TXX9_DMA_USE_SIMPLE_CHAIN
480 		dev_crit(dmaengine_chan_dev(&dc->chan),
481 			 "  desc: ch%#x s%#x d%#x c%#x\n",
482 			 d->CHAR, d->SAR, d->DAR, d->CNTR);
483 #else
484 		dev_crit(dmaengine_chan_dev(&dc->chan),
485 			 "  desc: ch%#x s%#x d%#x c%#x"
486 			 " si%#x di%#x cc%#x cs%#x\n",
487 			 d->CHAR, d->SAR, d->DAR, d->CNTR,
488 			 d->SAIR, d->DAIR, d->CCR, d->CSR);
489 #endif
490 	}
491 }
492 
493 static void txx9dmac_handle_error(struct txx9dmac_chan *dc, u32 csr)
494 {
495 	struct txx9dmac_desc *bad_desc;
496 	struct txx9dmac_desc *child;
497 	u32 errors;
498 
499 	/*
500 	 * The descriptor currently at the head of the active list is
501 	 * borked. Since we don't have any way to report errors, we'll
502 	 * just have to scream loudly and try to carry on.
503 	 */
504 	dev_crit(dmaengine_chan_dev(&dc->chan), "Abnormal Chain Completion\n");
505 	txx9dmac_dump_regs(dc);
506 
507 	bad_desc = txx9dmac_first_active(dc);
508 	list_del_init(&bad_desc->desc_node);
509 
510 	/* Clear all error flags and try to restart the controller */
511 	errors = csr & (TXX9_DMA_CSR_ABCHC |
512 			TXX9_DMA_CSR_CFERR | TXX9_DMA_CSR_CHERR |
513 			TXX9_DMA_CSR_DESERR | TXX9_DMA_CSR_SORERR);
514 	channel_writel(dc, CSR, errors);
515 
516 	if (list_empty(&dc->active_list) && !list_empty(&dc->queue))
517 		txx9dmac_dequeue(dc, &dc->active_list);
518 	if (!list_empty(&dc->active_list))
519 		txx9dmac_dostart(dc, txx9dmac_first_active(dc));
520 
521 	dev_crit(dmaengine_chan_dev(&dc->chan),
522 		 "Bad descriptor submitted for DMA! (cookie: %d)\n",
523 		 bad_desc->txd.cookie);
524 	txx9dmac_dump_desc(dc, &bad_desc->hwdesc);
525 	list_for_each_entry(child, &bad_desc->tx_list, desc_node)
526 		txx9dmac_dump_desc(dc, &child->hwdesc);
527 	/* Pretend the descriptor completed successfully */
528 	txx9dmac_descriptor_complete(dc, bad_desc);
529 }
530 
531 static void txx9dmac_scan_descriptors(struct txx9dmac_chan *dc)
532 {
533 	dma_addr_t chain;
534 	struct txx9dmac_desc *desc, *_desc;
535 	struct txx9dmac_desc *child;
536 	u32 csr;
537 
538 	if (is_dmac64(dc)) {
539 		chain = channel64_read_CHAR(dc);
540 		csr = channel64_readl(dc, CSR);
541 		channel64_writel(dc, CSR, csr);
542 	} else {
543 		chain = channel32_readl(dc, CHAR);
544 		csr = channel32_readl(dc, CSR);
545 		channel32_writel(dc, CSR, csr);
546 	}
547 	/* For dynamic chain, we should look at XFACT instead of NCHNC */
548 	if (!(csr & (TXX9_DMA_CSR_XFACT | TXX9_DMA_CSR_ABCHC))) {
549 		/* Everything we've submitted is done */
550 		txx9dmac_complete_all(dc);
551 		return;
552 	}
553 	if (!(csr & TXX9_DMA_CSR_CHNEN))
554 		chain = 0;	/* last descriptor of this chain */
555 
556 	dev_vdbg(dmaengine_chan_dev(&dc->chan), "scan_descriptors: char=%#llx\n",
557 		 (u64)chain);
558 
559 	list_for_each_entry_safe(desc, _desc, &dc->active_list, desc_node) {
560 		if (desc_read_CHAR(dc, desc) == chain) {
561 			/* This one is currently in progress */
562 			if (csr & TXX9_DMA_CSR_ABCHC)
563 				goto scan_done;
564 			return;
565 		}
566 
567 		list_for_each_entry(child, &desc->tx_list, desc_node)
568 			if (desc_read_CHAR(dc, child) == chain) {
569 				/* Currently in progress */
570 				if (csr & TXX9_DMA_CSR_ABCHC)
571 					goto scan_done;
572 				return;
573 			}
574 
575 		/*
576 		 * No descriptors so far seem to be in progress, i.e.
577 		 * this one must be done.
578 		 */
579 		txx9dmac_descriptor_complete(dc, desc);
580 	}
581 scan_done:
582 	if (csr & TXX9_DMA_CSR_ABCHC) {
583 		txx9dmac_handle_error(dc, csr);
584 		return;
585 	}
586 
587 	dev_err(dmaengine_chan_dev(&dc->chan),
588 		"BUG: All descriptors done, but channel not idle!\n");
589 
590 	/* Try to continue after resetting the channel... */
591 	txx9dmac_reset_chan(dc);
592 
593 	if (!list_empty(&dc->queue)) {
594 		txx9dmac_dequeue(dc, &dc->active_list);
595 		txx9dmac_dostart(dc, txx9dmac_first_active(dc));
596 	}
597 }
598 
599 static void txx9dmac_chan_tasklet(struct tasklet_struct *t)
600 {
601 	int irq;
602 	u32 csr;
603 	struct txx9dmac_chan *dc;
604 
605 	dc = from_tasklet(dc, t, tasklet);
606 	csr = channel_readl(dc, CSR);
607 	dev_vdbg(dmaengine_chan_dev(&dc->chan), "tasklet: status=%x\n", csr);
608 
609 	spin_lock(&dc->lock);
610 	if (csr & (TXX9_DMA_CSR_ABCHC | TXX9_DMA_CSR_NCHNC |
611 		   TXX9_DMA_CSR_NTRNFC))
612 		txx9dmac_scan_descriptors(dc);
613 	spin_unlock(&dc->lock);
614 	irq = dc->irq;
615 
616 	enable_irq(irq);
617 }
618 
619 static irqreturn_t txx9dmac_chan_interrupt(int irq, void *dev_id)
620 {
621 	struct txx9dmac_chan *dc = dev_id;
622 
623 	dev_vdbg(dmaengine_chan_dev(&dc->chan), "interrupt: status=%#x\n",
624 			channel_readl(dc, CSR));
625 
626 	tasklet_schedule(&dc->tasklet);
627 	/*
628 	 * Just disable the interrupts. We'll turn them back on in the
629 	 * softirq handler.
630 	 */
631 	disable_irq_nosync(irq);
632 
633 	return IRQ_HANDLED;
634 }
635 
636 static void txx9dmac_tasklet(struct tasklet_struct *t)
637 {
638 	int irq;
639 	u32 csr;
640 	struct txx9dmac_chan *dc;
641 
642 	struct txx9dmac_dev *ddev = from_tasklet(ddev, t, tasklet);
643 	u32 mcr;
644 	int i;
645 
646 	mcr = dma_readl(ddev, MCR);
647 	dev_vdbg(ddev->chan[0]->dma.dev, "tasklet: mcr=%x\n", mcr);
648 	for (i = 0; i < TXX9_DMA_MAX_NR_CHANNELS; i++) {
649 		if ((mcr >> (24 + i)) & 0x11) {
650 			dc = ddev->chan[i];
651 			csr = channel_readl(dc, CSR);
652 			dev_vdbg(dmaengine_chan_dev(&dc->chan), "tasklet: status=%x\n",
653 				 csr);
654 			spin_lock(&dc->lock);
655 			if (csr & (TXX9_DMA_CSR_ABCHC | TXX9_DMA_CSR_NCHNC |
656 				   TXX9_DMA_CSR_NTRNFC))
657 				txx9dmac_scan_descriptors(dc);
658 			spin_unlock(&dc->lock);
659 		}
660 	}
661 	irq = ddev->irq;
662 
663 	enable_irq(irq);
664 }
665 
666 static irqreturn_t txx9dmac_interrupt(int irq, void *dev_id)
667 {
668 	struct txx9dmac_dev *ddev = dev_id;
669 
670 	dev_vdbg(ddev->chan[0]->dma.dev, "interrupt: status=%#x\n",
671 			dma_readl(ddev, MCR));
672 
673 	tasklet_schedule(&ddev->tasklet);
674 	/*
675 	 * Just disable the interrupts. We'll turn them back on in the
676 	 * softirq handler.
677 	 */
678 	disable_irq_nosync(irq);
679 
680 	return IRQ_HANDLED;
681 }
682 
683 /*----------------------------------------------------------------------*/
684 
685 static dma_cookie_t txx9dmac_tx_submit(struct dma_async_tx_descriptor *tx)
686 {
687 	struct txx9dmac_desc *desc = txd_to_txx9dmac_desc(tx);
688 	struct txx9dmac_chan *dc = to_txx9dmac_chan(tx->chan);
689 	dma_cookie_t cookie;
690 
691 	spin_lock_bh(&dc->lock);
692 	cookie = dma_cookie_assign(tx);
693 
694 	dev_vdbg(dmaengine_chan_dev(tx->chan), "tx_submit: queued %u %p\n",
695 		 desc->txd.cookie, desc);
696 
697 	list_add_tail(&desc->desc_node, &dc->queue);
698 	spin_unlock_bh(&dc->lock);
699 
700 	return cookie;
701 }
702 
703 static struct dma_async_tx_descriptor *
704 txx9dmac_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
705 		size_t len, unsigned long flags)
706 {
707 	struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
708 	struct txx9dmac_dev *ddev = dc->ddev;
709 	struct txx9dmac_desc *desc;
710 	struct txx9dmac_desc *first;
711 	struct txx9dmac_desc *prev;
712 	size_t xfer_count;
713 	size_t offset;
714 
715 	dev_vdbg(dmaengine_chan_dev(chan), "prep_dma_memcpy d%#llx s%#llx l%#zx f%#lx\n",
716 		 (u64)dest, (u64)src, len, flags);
717 
718 	if (unlikely(!len)) {
719 		dev_dbg(dmaengine_chan_dev(chan), "prep_dma_memcpy: length is zero!\n");
720 		return NULL;
721 	}
722 
723 	prev = first = NULL;
724 
725 	for (offset = 0; offset < len; offset += xfer_count) {
726 		xfer_count = min_t(size_t, len - offset, TXX9_DMA_MAX_COUNT);
727 		/*
728 		 * Workaround for ERT-TX49H2-033, ERT-TX49H3-020,
729 		 * ERT-TX49H4-016 (slightly conservative)
730 		 */
731 		if (__is_dmac64(ddev)) {
732 			if (xfer_count > 0x100 &&
733 			    (xfer_count & 0xff) >= 0xfa &&
734 			    (xfer_count & 0xff) <= 0xff)
735 				xfer_count -= 0x20;
736 		} else {
737 			if (xfer_count > 0x80 &&
738 			    (xfer_count & 0x7f) >= 0x7e &&
739 			    (xfer_count & 0x7f) <= 0x7f)
740 				xfer_count -= 0x20;
741 		}
742 
743 		desc = txx9dmac_desc_get(dc);
744 		if (!desc) {
745 			txx9dmac_desc_put(dc, first);
746 			return NULL;
747 		}
748 
749 		if (__is_dmac64(ddev)) {
750 			desc->hwdesc.SAR = src + offset;
751 			desc->hwdesc.DAR = dest + offset;
752 			desc->hwdesc.CNTR = xfer_count;
753 			txx9dmac_desc_set_nosimple(ddev, desc, 8, 8,
754 					dc->ccr | TXX9_DMA_CCR_XFACT);
755 		} else {
756 			desc->hwdesc32.SAR = src + offset;
757 			desc->hwdesc32.DAR = dest + offset;
758 			desc->hwdesc32.CNTR = xfer_count;
759 			txx9dmac_desc_set_nosimple(ddev, desc, 4, 4,
760 					dc->ccr | TXX9_DMA_CCR_XFACT);
761 		}
762 
763 		/*
764 		 * The descriptors on tx_list are not reachable from
765 		 * the dc->queue list or dc->active_list after a
766 		 * submit.  If we put all descriptors on active_list,
767 		 * calling of callback on the completion will be more
768 		 * complex.
769 		 */
770 		if (!first) {
771 			first = desc;
772 		} else {
773 			desc_write_CHAR(dc, prev, desc->txd.phys);
774 			dma_sync_single_for_device(chan2parent(&dc->chan),
775 					prev->txd.phys, ddev->descsize,
776 					DMA_TO_DEVICE);
777 			list_add_tail(&desc->desc_node, &first->tx_list);
778 		}
779 		prev = desc;
780 	}
781 
782 	/* Trigger interrupt after last block */
783 	if (flags & DMA_PREP_INTERRUPT)
784 		txx9dmac_desc_set_INTENT(ddev, prev);
785 
786 	desc_write_CHAR(dc, prev, 0);
787 	dma_sync_single_for_device(chan2parent(&dc->chan),
788 			prev->txd.phys, ddev->descsize,
789 			DMA_TO_DEVICE);
790 
791 	first->txd.flags = flags;
792 	first->len = len;
793 
794 	return &first->txd;
795 }
796 
797 static struct dma_async_tx_descriptor *
798 txx9dmac_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
799 		unsigned int sg_len, enum dma_transfer_direction direction,
800 		unsigned long flags, void *context)
801 {
802 	struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
803 	struct txx9dmac_dev *ddev = dc->ddev;
804 	struct txx9dmac_slave *ds = chan->private;
805 	struct txx9dmac_desc *prev;
806 	struct txx9dmac_desc *first;
807 	unsigned int i;
808 	struct scatterlist *sg;
809 
810 	dev_vdbg(dmaengine_chan_dev(chan), "prep_dma_slave\n");
811 
812 	BUG_ON(!ds || !ds->reg_width);
813 	if (ds->tx_reg)
814 		BUG_ON(direction != DMA_MEM_TO_DEV);
815 	else
816 		BUG_ON(direction != DMA_DEV_TO_MEM);
817 	if (unlikely(!sg_len))
818 		return NULL;
819 
820 	prev = first = NULL;
821 
822 	for_each_sg(sgl, sg, sg_len, i) {
823 		struct txx9dmac_desc *desc;
824 		dma_addr_t mem;
825 		u32 sai, dai;
826 
827 		desc = txx9dmac_desc_get(dc);
828 		if (!desc) {
829 			txx9dmac_desc_put(dc, first);
830 			return NULL;
831 		}
832 
833 		mem = sg_dma_address(sg);
834 
835 		if (__is_dmac64(ddev)) {
836 			if (direction == DMA_MEM_TO_DEV) {
837 				desc->hwdesc.SAR = mem;
838 				desc->hwdesc.DAR = ds->tx_reg;
839 			} else {
840 				desc->hwdesc.SAR = ds->rx_reg;
841 				desc->hwdesc.DAR = mem;
842 			}
843 			desc->hwdesc.CNTR = sg_dma_len(sg);
844 		} else {
845 			if (direction == DMA_MEM_TO_DEV) {
846 				desc->hwdesc32.SAR = mem;
847 				desc->hwdesc32.DAR = ds->tx_reg;
848 			} else {
849 				desc->hwdesc32.SAR = ds->rx_reg;
850 				desc->hwdesc32.DAR = mem;
851 			}
852 			desc->hwdesc32.CNTR = sg_dma_len(sg);
853 		}
854 		if (direction == DMA_MEM_TO_DEV) {
855 			sai = ds->reg_width;
856 			dai = 0;
857 		} else {
858 			sai = 0;
859 			dai = ds->reg_width;
860 		}
861 		txx9dmac_desc_set_nosimple(ddev, desc, sai, dai,
862 					dc->ccr | TXX9_DMA_CCR_XFACT);
863 
864 		if (!first) {
865 			first = desc;
866 		} else {
867 			desc_write_CHAR(dc, prev, desc->txd.phys);
868 			dma_sync_single_for_device(chan2parent(&dc->chan),
869 					prev->txd.phys,
870 					ddev->descsize,
871 					DMA_TO_DEVICE);
872 			list_add_tail(&desc->desc_node, &first->tx_list);
873 		}
874 		prev = desc;
875 	}
876 
877 	/* Trigger interrupt after last block */
878 	if (flags & DMA_PREP_INTERRUPT)
879 		txx9dmac_desc_set_INTENT(ddev, prev);
880 
881 	desc_write_CHAR(dc, prev, 0);
882 	dma_sync_single_for_device(chan2parent(&dc->chan),
883 			prev->txd.phys, ddev->descsize,
884 			DMA_TO_DEVICE);
885 
886 	first->txd.flags = flags;
887 	first->len = 0;
888 
889 	return &first->txd;
890 }
891 
892 static int txx9dmac_terminate_all(struct dma_chan *chan)
893 {
894 	struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
895 	struct txx9dmac_desc *desc, *_desc;
896 	LIST_HEAD(list);
897 
898 	dev_vdbg(dmaengine_chan_dev(chan), "terminate_all\n");
899 	spin_lock_bh(&dc->lock);
900 
901 	txx9dmac_reset_chan(dc);
902 
903 	/* active_list entries will end up before queued entries */
904 	list_splice_init(&dc->queue, &list);
905 	list_splice_init(&dc->active_list, &list);
906 
907 	spin_unlock_bh(&dc->lock);
908 
909 	/* Flush all pending and queued descriptors */
910 	list_for_each_entry_safe(desc, _desc, &list, desc_node)
911 		txx9dmac_descriptor_complete(dc, desc);
912 
913 	return 0;
914 }
915 
916 static enum dma_status
917 txx9dmac_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
918 		   struct dma_tx_state *txstate)
919 {
920 	struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
921 	enum dma_status ret;
922 
923 	ret = dma_cookie_status(chan, cookie, txstate);
924 	if (ret == DMA_COMPLETE)
925 		return DMA_COMPLETE;
926 
927 	spin_lock_bh(&dc->lock);
928 	txx9dmac_scan_descriptors(dc);
929 	spin_unlock_bh(&dc->lock);
930 
931 	return dma_cookie_status(chan, cookie, txstate);
932 }
933 
934 static void txx9dmac_chain_dynamic(struct txx9dmac_chan *dc,
935 				   struct txx9dmac_desc *prev)
936 {
937 	struct txx9dmac_dev *ddev = dc->ddev;
938 	struct txx9dmac_desc *desc;
939 	LIST_HEAD(list);
940 
941 	prev = txx9dmac_last_child(prev);
942 	txx9dmac_dequeue(dc, &list);
943 	desc = list_entry(list.next, struct txx9dmac_desc, desc_node);
944 	desc_write_CHAR(dc, prev, desc->txd.phys);
945 	dma_sync_single_for_device(chan2parent(&dc->chan),
946 				   prev->txd.phys, ddev->descsize,
947 				   DMA_TO_DEVICE);
948 	if (!(channel_readl(dc, CSR) & TXX9_DMA_CSR_CHNEN) &&
949 	    channel_read_CHAR(dc) == prev->txd.phys)
950 		/* Restart chain DMA */
951 		channel_write_CHAR(dc, desc->txd.phys);
952 	list_splice_tail(&list, &dc->active_list);
953 }
954 
955 static void txx9dmac_issue_pending(struct dma_chan *chan)
956 {
957 	struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
958 
959 	spin_lock_bh(&dc->lock);
960 
961 	if (!list_empty(&dc->active_list))
962 		txx9dmac_scan_descriptors(dc);
963 	if (!list_empty(&dc->queue)) {
964 		if (list_empty(&dc->active_list)) {
965 			txx9dmac_dequeue(dc, &dc->active_list);
966 			txx9dmac_dostart(dc, txx9dmac_first_active(dc));
967 		} else if (txx9_dma_have_SMPCHN()) {
968 			struct txx9dmac_desc *prev = txx9dmac_last_active(dc);
969 
970 			if (!(prev->txd.flags & DMA_PREP_INTERRUPT) ||
971 			    txx9dmac_chan_INTENT(dc))
972 				txx9dmac_chain_dynamic(dc, prev);
973 		}
974 	}
975 
976 	spin_unlock_bh(&dc->lock);
977 }
978 
979 static int txx9dmac_alloc_chan_resources(struct dma_chan *chan)
980 {
981 	struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
982 	struct txx9dmac_slave *ds = chan->private;
983 	struct txx9dmac_desc *desc;
984 	int i;
985 
986 	dev_vdbg(dmaengine_chan_dev(chan), "alloc_chan_resources\n");
987 
988 	/* ASSERT:  channel is idle */
989 	if (channel_readl(dc, CSR) & TXX9_DMA_CSR_XFACT) {
990 		dev_dbg(dmaengine_chan_dev(chan), "DMA channel not idle?\n");
991 		return -EIO;
992 	}
993 
994 	dma_cookie_init(chan);
995 
996 	dc->ccr = TXX9_DMA_CCR_IMMCHN | TXX9_DMA_CCR_INTENE | CCR_LE;
997 	txx9dmac_chan_set_SMPCHN(dc);
998 	if (!txx9_dma_have_SMPCHN() || (dc->ccr & TXX9_DMA_CCR_SMPCHN))
999 		dc->ccr |= TXX9_DMA_CCR_INTENC;
1000 	if (chan->device->device_prep_dma_memcpy) {
1001 		if (ds)
1002 			return -EINVAL;
1003 		dc->ccr |= TXX9_DMA_CCR_XFSZ_X8;
1004 	} else {
1005 		if (!ds ||
1006 		    (ds->tx_reg && ds->rx_reg) || (!ds->tx_reg && !ds->rx_reg))
1007 			return -EINVAL;
1008 		dc->ccr |= TXX9_DMA_CCR_EXTRQ |
1009 			TXX9_DMA_CCR_XFSZ(__ffs(ds->reg_width));
1010 		txx9dmac_chan_set_INTENT(dc);
1011 	}
1012 
1013 	spin_lock_bh(&dc->lock);
1014 	i = dc->descs_allocated;
1015 	while (dc->descs_allocated < TXX9_DMA_INITIAL_DESC_COUNT) {
1016 		spin_unlock_bh(&dc->lock);
1017 
1018 		desc = txx9dmac_desc_alloc(dc, GFP_KERNEL);
1019 		if (!desc) {
1020 			dev_info(dmaengine_chan_dev(chan),
1021 				"only allocated %d descriptors\n", i);
1022 			spin_lock_bh(&dc->lock);
1023 			break;
1024 		}
1025 		txx9dmac_desc_put(dc, desc);
1026 
1027 		spin_lock_bh(&dc->lock);
1028 		i = ++dc->descs_allocated;
1029 	}
1030 	spin_unlock_bh(&dc->lock);
1031 
1032 	dev_dbg(dmaengine_chan_dev(chan),
1033 		"alloc_chan_resources allocated %d descriptors\n", i);
1034 
1035 	return i;
1036 }
1037 
1038 static void txx9dmac_free_chan_resources(struct dma_chan *chan)
1039 {
1040 	struct txx9dmac_chan *dc = to_txx9dmac_chan(chan);
1041 	struct txx9dmac_dev *ddev = dc->ddev;
1042 	struct txx9dmac_desc *desc, *_desc;
1043 	LIST_HEAD(list);
1044 
1045 	dev_dbg(dmaengine_chan_dev(chan), "free_chan_resources (descs allocated=%u)\n",
1046 			dc->descs_allocated);
1047 
1048 	/* ASSERT:  channel is idle */
1049 	BUG_ON(!list_empty(&dc->active_list));
1050 	BUG_ON(!list_empty(&dc->queue));
1051 	BUG_ON(channel_readl(dc, CSR) & TXX9_DMA_CSR_XFACT);
1052 
1053 	spin_lock_bh(&dc->lock);
1054 	list_splice_init(&dc->free_list, &list);
1055 	dc->descs_allocated = 0;
1056 	spin_unlock_bh(&dc->lock);
1057 
1058 	list_for_each_entry_safe(desc, _desc, &list, desc_node) {
1059 		dev_vdbg(dmaengine_chan_dev(chan), "  freeing descriptor %p\n", desc);
1060 		dma_unmap_single(chan2parent(chan), desc->txd.phys,
1061 				 ddev->descsize, DMA_TO_DEVICE);
1062 		kfree(desc);
1063 	}
1064 
1065 	dev_vdbg(dmaengine_chan_dev(chan), "free_chan_resources done\n");
1066 }
1067 
1068 /*----------------------------------------------------------------------*/
1069 
1070 static void txx9dmac_off(struct txx9dmac_dev *ddev)
1071 {
1072 	dma_writel(ddev, MCR, 0);
1073 }
1074 
1075 static int __init txx9dmac_chan_probe(struct platform_device *pdev)
1076 {
1077 	struct txx9dmac_chan_platform_data *cpdata =
1078 			dev_get_platdata(&pdev->dev);
1079 	struct platform_device *dmac_dev = cpdata->dmac_dev;
1080 	struct txx9dmac_platform_data *pdata = dev_get_platdata(&dmac_dev->dev);
1081 	struct txx9dmac_chan *dc;
1082 	int err;
1083 	int ch = pdev->id % TXX9_DMA_MAX_NR_CHANNELS;
1084 	int irq;
1085 
1086 	dc = devm_kzalloc(&pdev->dev, sizeof(*dc), GFP_KERNEL);
1087 	if (!dc)
1088 		return -ENOMEM;
1089 
1090 	dc->dma.dev = &pdev->dev;
1091 	dc->dma.device_alloc_chan_resources = txx9dmac_alloc_chan_resources;
1092 	dc->dma.device_free_chan_resources = txx9dmac_free_chan_resources;
1093 	dc->dma.device_terminate_all = txx9dmac_terminate_all;
1094 	dc->dma.device_tx_status = txx9dmac_tx_status;
1095 	dc->dma.device_issue_pending = txx9dmac_issue_pending;
1096 	if (pdata && pdata->memcpy_chan == ch) {
1097 		dc->dma.device_prep_dma_memcpy = txx9dmac_prep_dma_memcpy;
1098 		dma_cap_set(DMA_MEMCPY, dc->dma.cap_mask);
1099 	} else {
1100 		dc->dma.device_prep_slave_sg = txx9dmac_prep_slave_sg;
1101 		dma_cap_set(DMA_SLAVE, dc->dma.cap_mask);
1102 		dma_cap_set(DMA_PRIVATE, dc->dma.cap_mask);
1103 	}
1104 
1105 	INIT_LIST_HEAD(&dc->dma.channels);
1106 	dc->ddev = platform_get_drvdata(dmac_dev);
1107 	if (dc->ddev->irq < 0) {
1108 		irq = platform_get_irq(pdev, 0);
1109 		if (irq < 0)
1110 			return irq;
1111 		tasklet_setup(&dc->tasklet, txx9dmac_chan_tasklet);
1112 		dc->irq = irq;
1113 		err = devm_request_irq(&pdev->dev, dc->irq,
1114 			txx9dmac_chan_interrupt, 0, dev_name(&pdev->dev), dc);
1115 		if (err)
1116 			return err;
1117 	} else
1118 		dc->irq = -1;
1119 	dc->ddev->chan[ch] = dc;
1120 	dc->chan.device = &dc->dma;
1121 	list_add_tail(&dc->chan.device_node, &dc->chan.device->channels);
1122 	dma_cookie_init(&dc->chan);
1123 
1124 	if (is_dmac64(dc))
1125 		dc->ch_regs = &__txx9dmac_regs(dc->ddev)->CHAN[ch];
1126 	else
1127 		dc->ch_regs = &__txx9dmac_regs32(dc->ddev)->CHAN[ch];
1128 	spin_lock_init(&dc->lock);
1129 
1130 	INIT_LIST_HEAD(&dc->active_list);
1131 	INIT_LIST_HEAD(&dc->queue);
1132 	INIT_LIST_HEAD(&dc->free_list);
1133 
1134 	txx9dmac_reset_chan(dc);
1135 
1136 	platform_set_drvdata(pdev, dc);
1137 
1138 	err = dma_async_device_register(&dc->dma);
1139 	if (err)
1140 		return err;
1141 	dev_dbg(&pdev->dev, "TXx9 DMA Channel (dma%d%s%s)\n",
1142 		dc->dma.dev_id,
1143 		dma_has_cap(DMA_MEMCPY, dc->dma.cap_mask) ? " memcpy" : "",
1144 		dma_has_cap(DMA_SLAVE, dc->dma.cap_mask) ? " slave" : "");
1145 
1146 	return 0;
1147 }
1148 
1149 static void txx9dmac_chan_remove(struct platform_device *pdev)
1150 {
1151 	struct txx9dmac_chan *dc = platform_get_drvdata(pdev);
1152 
1153 
1154 	dma_async_device_unregister(&dc->dma);
1155 	if (dc->irq >= 0) {
1156 		devm_free_irq(&pdev->dev, dc->irq, dc);
1157 		tasklet_kill(&dc->tasklet);
1158 	}
1159 	dc->ddev->chan[pdev->id % TXX9_DMA_MAX_NR_CHANNELS] = NULL;
1160 }
1161 
1162 static int __init txx9dmac_probe(struct platform_device *pdev)
1163 {
1164 	struct txx9dmac_platform_data *pdata = dev_get_platdata(&pdev->dev);
1165 	struct txx9dmac_dev *ddev;
1166 	void __iomem *regs;
1167 	u32 mcr;
1168 	int err;
1169 
1170 	regs = devm_platform_ioremap_resource(pdev, 0);
1171 	if (IS_ERR(regs))
1172 		return PTR_ERR(regs);
1173 
1174 	ddev = devm_kzalloc(&pdev->dev, sizeof(*ddev), GFP_KERNEL);
1175 	if (!ddev)
1176 		return -ENOMEM;
1177 
1178 	ddev->regs = regs;
1179 	ddev->have_64bit_regs = pdata->have_64bit_regs;
1180 	if (__is_dmac64(ddev))
1181 		ddev->descsize = sizeof(struct txx9dmac_hwdesc);
1182 	else
1183 		ddev->descsize = sizeof(struct txx9dmac_hwdesc32);
1184 
1185 	/* force dma off, just in case */
1186 	txx9dmac_off(ddev);
1187 
1188 	ddev->irq = platform_get_irq(pdev, 0);
1189 	if (ddev->irq >= 0) {
1190 		tasklet_setup(&ddev->tasklet, txx9dmac_tasklet);
1191 		err = devm_request_irq(&pdev->dev, ddev->irq,
1192 			txx9dmac_interrupt, 0, dev_name(&pdev->dev), ddev);
1193 		if (err)
1194 			return err;
1195 	}
1196 
1197 	mcr = TXX9_DMA_MCR_MSTEN | MCR_LE;
1198 	if (pdata && pdata->memcpy_chan >= 0)
1199 		mcr |= TXX9_DMA_MCR_FIFUM(pdata->memcpy_chan);
1200 	dma_writel(ddev, MCR, mcr);
1201 
1202 	platform_set_drvdata(pdev, ddev);
1203 	return 0;
1204 }
1205 
1206 static void txx9dmac_remove(struct platform_device *pdev)
1207 {
1208 	struct txx9dmac_dev *ddev = platform_get_drvdata(pdev);
1209 
1210 	txx9dmac_off(ddev);
1211 	if (ddev->irq >= 0) {
1212 		devm_free_irq(&pdev->dev, ddev->irq, ddev);
1213 		tasklet_kill(&ddev->tasklet);
1214 	}
1215 }
1216 
1217 static void txx9dmac_shutdown(struct platform_device *pdev)
1218 {
1219 	struct txx9dmac_dev *ddev = platform_get_drvdata(pdev);
1220 
1221 	txx9dmac_off(ddev);
1222 }
1223 
1224 static int txx9dmac_suspend_noirq(struct device *dev)
1225 {
1226 	struct txx9dmac_dev *ddev = dev_get_drvdata(dev);
1227 
1228 	txx9dmac_off(ddev);
1229 	return 0;
1230 }
1231 
1232 static int txx9dmac_resume_noirq(struct device *dev)
1233 {
1234 	struct txx9dmac_dev *ddev = dev_get_drvdata(dev);
1235 	struct txx9dmac_platform_data *pdata = dev_get_platdata(dev);
1236 	u32 mcr;
1237 
1238 	mcr = TXX9_DMA_MCR_MSTEN | MCR_LE;
1239 	if (pdata && pdata->memcpy_chan >= 0)
1240 		mcr |= TXX9_DMA_MCR_FIFUM(pdata->memcpy_chan);
1241 	dma_writel(ddev, MCR, mcr);
1242 	return 0;
1243 
1244 }
1245 
1246 static const struct dev_pm_ops txx9dmac_dev_pm_ops = {
1247 	.suspend_noirq = txx9dmac_suspend_noirq,
1248 	.resume_noirq = txx9dmac_resume_noirq,
1249 };
1250 
1251 static struct platform_driver txx9dmac_chan_driver = {
1252 	.remove		= txx9dmac_chan_remove,
1253 	.driver = {
1254 		.name	= "txx9dmac-chan",
1255 	},
1256 };
1257 
1258 static struct platform_driver txx9dmac_driver = {
1259 	.remove		= txx9dmac_remove,
1260 	.shutdown	= txx9dmac_shutdown,
1261 	.driver = {
1262 		.name	= "txx9dmac",
1263 		.pm	= &txx9dmac_dev_pm_ops,
1264 	},
1265 };
1266 
1267 static int __init txx9dmac_init(void)
1268 {
1269 	int rc;
1270 
1271 	rc = platform_driver_probe(&txx9dmac_driver, txx9dmac_probe);
1272 	if (!rc) {
1273 		rc = platform_driver_probe(&txx9dmac_chan_driver,
1274 					   txx9dmac_chan_probe);
1275 		if (rc)
1276 			platform_driver_unregister(&txx9dmac_driver);
1277 	}
1278 	return rc;
1279 }
1280 module_init(txx9dmac_init);
1281 
1282 static void __exit txx9dmac_exit(void)
1283 {
1284 	platform_driver_unregister(&txx9dmac_chan_driver);
1285 	platform_driver_unregister(&txx9dmac_driver);
1286 }
1287 module_exit(txx9dmac_exit);
1288 
1289 MODULE_LICENSE("GPL");
1290 MODULE_DESCRIPTION("TXx9 DMA Controller driver");
1291 MODULE_AUTHOR("Atsushi Nemoto <anemo@mba.ocn.ne.jp>");
1292 MODULE_ALIAS("platform:txx9dmac");
1293 MODULE_ALIAS("platform:txx9dmac-chan");
1294