dmaengine: k3dma: fix sparse warnings
[linux-2.6-block.git] / drivers / dma / dw / core.c
CommitLineData
3bfb1d20 1/*
b801479b 2 * Core driver for the Synopsys DesignWare DMA Controller
3bfb1d20
HS
3 *
4 * Copyright (C) 2007-2008 Atmel Corporation
aecb7b64 5 * Copyright (C) 2010-2011 ST Microelectronics
9cade1a4 6 * Copyright (C) 2013 Intel Corporation
3bfb1d20
HS
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
b801479b 12
327e6970 13#include <linux/bitops.h>
3bfb1d20
HS
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/dmaengine.h>
17#include <linux/dma-mapping.h>
f8122a82 18#include <linux/dmapool.h>
7331205a 19#include <linux/err.h>
3bfb1d20
HS
20#include <linux/init.h>
21#include <linux/interrupt.h>
22#include <linux/io.h>
23#include <linux/mm.h>
24#include <linux/module.h>
3bfb1d20
HS
25#include <linux/slab.h>
26
61a76496 27#include "../dmaengine.h"
9cade1a4 28#include "internal.h"
3bfb1d20
HS
29
30/*
31 * This supports the Synopsys "DesignWare AHB Central DMA Controller",
32 * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
33 * of which use ARM any more). See the "Databook" from Synopsys for
34 * information beyond what licensees probably provide.
35 *
36 * The driver has currently been tested only with the Atmel AT32AP7000,
37 * which does not support descriptor writeback.
38 */
39
78f3c9d2
AS
40static inline bool is_request_line_unset(struct dw_dma_chan *dwc)
41{
42 return dwc->request_line == (typeof(dwc->request_line))~0;
43}
44
f776076b 45static inline void dwc_set_masters(struct dw_dma_chan *dwc)
5be10f34 46{
f776076b
AB
47 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
48 struct dw_dma_slave *dws = dwc->chan.private;
49 unsigned char mmax = dw->nr_masters - 1;
5be10f34 50
78f3c9d2
AS
51 if (!is_request_line_unset(dwc))
52 return;
53
54 dwc->src_master = min_t(unsigned char, mmax, dwc_get_sms(dws));
55 dwc->dst_master = min_t(unsigned char, mmax, dwc_get_dms(dws));
5be10f34
AS
56}
57
327e6970 58#define DWC_DEFAULT_CTLLO(_chan) ({ \
327e6970
VK
59 struct dw_dma_chan *_dwc = to_dw_dma_chan(_chan); \
60 struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \
495aea4b 61 bool _is_slave = is_slave_direction(_dwc->direction); \
495aea4b 62 u8 _smsize = _is_slave ? _sconfig->src_maxburst : \
327e6970 63 DW_DMA_MSIZE_16; \
495aea4b 64 u8 _dmsize = _is_slave ? _sconfig->dst_maxburst : \
327e6970 65 DW_DMA_MSIZE_16; \
f301c062 66 \
327e6970
VK
67 (DWC_CTLL_DST_MSIZE(_dmsize) \
68 | DWC_CTLL_SRC_MSIZE(_smsize) \
f301c062
JI
69 | DWC_CTLL_LLP_D_EN \
70 | DWC_CTLL_LLP_S_EN \
f776076b
AB
71 | DWC_CTLL_DMS(_dwc->dst_master) \
72 | DWC_CTLL_SMS(_dwc->src_master)); \
f301c062 73 })
3bfb1d20 74
3bfb1d20
HS
75/*
76 * Number of descriptors to allocate for each channel. This should be
77 * made configurable somehow; preferably, the clients (at least the
78 * ones using slave transfers) should be able to give us a hint.
79 */
80#define NR_DESCS_PER_CHANNEL 64
81
82/*----------------------------------------------------------------------*/
3bfb1d20 83
41d5e59c
DW
84static struct device *chan2dev(struct dma_chan *chan)
85{
86 return &chan->dev->device;
87}
41d5e59c 88
3bfb1d20
HS
89static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
90{
e63a47a3 91 return to_dw_desc(dwc->active_list.next);
3bfb1d20
HS
92}
93
3bfb1d20
HS
94static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
95{
96 struct dw_desc *desc, *_desc;
97 struct dw_desc *ret = NULL;
98 unsigned int i = 0;
69cea5a0 99 unsigned long flags;
3bfb1d20 100
69cea5a0 101 spin_lock_irqsave(&dwc->lock, flags);
3bfb1d20 102 list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
2ab37276 103 i++;
3bfb1d20
HS
104 if (async_tx_test_ack(&desc->txd)) {
105 list_del(&desc->desc_node);
106 ret = desc;
107 break;
108 }
41d5e59c 109 dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
3bfb1d20 110 }
69cea5a0 111 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20 112
41d5e59c 113 dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
3bfb1d20
HS
114
115 return ret;
116}
117
3bfb1d20
HS
118/*
119 * Move a descriptor, including any children, to the free list.
120 * `desc' must not be on any lists.
121 */
122static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
123{
69cea5a0
VK
124 unsigned long flags;
125
3bfb1d20
HS
126 if (desc) {
127 struct dw_desc *child;
128
69cea5a0 129 spin_lock_irqsave(&dwc->lock, flags);
e0bd0f8c 130 list_for_each_entry(child, &desc->tx_list, desc_node)
41d5e59c 131 dev_vdbg(chan2dev(&dwc->chan),
3bfb1d20
HS
132 "moving child desc %p to freelist\n",
133 child);
e0bd0f8c 134 list_splice_init(&desc->tx_list, &dwc->free_list);
41d5e59c 135 dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
3bfb1d20 136 list_add(&desc->desc_node, &dwc->free_list);
69cea5a0 137 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20
HS
138 }
139}
140
61e183f8
VK
141static void dwc_initialize(struct dw_dma_chan *dwc)
142{
143 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
144 struct dw_dma_slave *dws = dwc->chan.private;
145 u32 cfghi = DWC_CFGH_FIFO_MODE;
146 u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority);
147
148 if (dwc->initialized == true)
149 return;
150
f776076b 151 if (dws) {
61e183f8
VK
152 /*
153 * We need controller-specific data to set up slave
154 * transfers.
155 */
156 BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
157
158 cfghi = dws->cfg_hi;
159 cfglo |= dws->cfg_lo & ~DWC_CFGL_CH_PRIOR_MASK;
8fccc5bf 160 } else {
0fdb567f 161 if (dwc->direction == DMA_MEM_TO_DEV)
f776076b 162 cfghi = DWC_CFGH_DST_PER(dwc->request_line);
0fdb567f 163 else if (dwc->direction == DMA_DEV_TO_MEM)
f776076b 164 cfghi = DWC_CFGH_SRC_PER(dwc->request_line);
61e183f8
VK
165 }
166
167 channel_writel(dwc, CFG_LO, cfglo);
168 channel_writel(dwc, CFG_HI, cfghi);
169
170 /* Enable interrupts */
171 channel_set_bit(dw, MASK.XFER, dwc->mask);
61e183f8
VK
172 channel_set_bit(dw, MASK.ERROR, dwc->mask);
173
174 dwc->initialized = true;
175}
176
3bfb1d20
HS
177/*----------------------------------------------------------------------*/
178
4c2d56c5
AS
179static inline unsigned int dwc_fast_fls(unsigned long long v)
180{
181 /*
182 * We can be a lot more clever here, but this should take care
183 * of the most common optimization.
184 */
185 if (!(v & 7))
186 return 3;
187 else if (!(v & 3))
188 return 2;
189 else if (!(v & 1))
190 return 1;
191 return 0;
192}
193
f52b36d2 194static inline void dwc_dump_chan_regs(struct dw_dma_chan *dwc)
1d455437
AS
195{
196 dev_err(chan2dev(&dwc->chan),
197 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
198 channel_readl(dwc, SAR),
199 channel_readl(dwc, DAR),
200 channel_readl(dwc, LLP),
201 channel_readl(dwc, CTL_HI),
202 channel_readl(dwc, CTL_LO));
203}
204
3f936207
AS
205static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc)
206{
207 channel_clear_bit(dw, CH_EN, dwc->mask);
208 while (dma_readl(dw, CH_EN) & dwc->mask)
209 cpu_relax();
210}
211
1d455437
AS
212/*----------------------------------------------------------------------*/
213
fed2574b
AS
214/* Perform single block transfer */
215static inline void dwc_do_single_block(struct dw_dma_chan *dwc,
216 struct dw_desc *desc)
217{
218 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
219 u32 ctllo;
220
221 /* Software emulation of LLP mode relies on interrupts to continue
222 * multi block transfer. */
223 ctllo = desc->lli.ctllo | DWC_CTLL_INT_EN;
224
225 channel_writel(dwc, SAR, desc->lli.sar);
226 channel_writel(dwc, DAR, desc->lli.dar);
227 channel_writel(dwc, CTL_LO, ctllo);
228 channel_writel(dwc, CTL_HI, desc->lli.ctlhi);
229 channel_set_bit(dw, CH_EN, dwc->mask);
f5c6a7df
AS
230
231 /* Move pointer to next descriptor */
232 dwc->tx_node_active = dwc->tx_node_active->next;
fed2574b
AS
233}
234
3bfb1d20
HS
235/* Called with dwc->lock held and bh disabled */
236static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
237{
238 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
fed2574b 239 unsigned long was_soft_llp;
3bfb1d20
HS
240
241 /* ASSERT: channel is idle */
242 if (dma_readl(dw, CH_EN) & dwc->mask) {
41d5e59c 243 dev_err(chan2dev(&dwc->chan),
3bfb1d20 244 "BUG: Attempted to start non-idle channel\n");
1d455437 245 dwc_dump_chan_regs(dwc);
3bfb1d20
HS
246
247 /* The tasklet will hopefully advance the queue... */
248 return;
249 }
250
fed2574b
AS
251 if (dwc->nollp) {
252 was_soft_llp = test_and_set_bit(DW_DMA_IS_SOFT_LLP,
253 &dwc->flags);
254 if (was_soft_llp) {
255 dev_err(chan2dev(&dwc->chan),
256 "BUG: Attempted to start new LLP transfer "
257 "inside ongoing one\n");
258 return;
259 }
260
261 dwc_initialize(dwc);
262
4702d524 263 dwc->residue = first->total_len;
f5c6a7df 264 dwc->tx_node_active = &first->tx_list;
fed2574b 265
fdf475fa 266 /* Submit first block */
fed2574b
AS
267 dwc_do_single_block(dwc, first);
268
269 return;
270 }
271
61e183f8
VK
272 dwc_initialize(dwc);
273
3bfb1d20
HS
274 channel_writel(dwc, LLP, first->txd.phys);
275 channel_writel(dwc, CTL_LO,
276 DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
277 channel_writel(dwc, CTL_HI, 0);
278 channel_set_bit(dw, CH_EN, dwc->mask);
279}
280
281/*----------------------------------------------------------------------*/
282
283static void
5fedefb8
VK
284dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
285 bool callback_required)
3bfb1d20 286{
5fedefb8
VK
287 dma_async_tx_callback callback = NULL;
288 void *param = NULL;
3bfb1d20 289 struct dma_async_tx_descriptor *txd = &desc->txd;
e518076e 290 struct dw_desc *child;
69cea5a0 291 unsigned long flags;
3bfb1d20 292
41d5e59c 293 dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
3bfb1d20 294
69cea5a0 295 spin_lock_irqsave(&dwc->lock, flags);
f7fbce07 296 dma_cookie_complete(txd);
5fedefb8
VK
297 if (callback_required) {
298 callback = txd->callback;
299 param = txd->callback_param;
300 }
3bfb1d20 301
e518076e
VK
302 /* async_tx_ack */
303 list_for_each_entry(child, &desc->tx_list, desc_node)
304 async_tx_ack(&child->txd);
305 async_tx_ack(&desc->txd);
306
e0bd0f8c 307 list_splice_init(&desc->tx_list, &dwc->free_list);
3bfb1d20
HS
308 list_move(&desc->desc_node, &dwc->free_list);
309
d38a8c62 310 dma_descriptor_unmap(txd);
69cea5a0
VK
311 spin_unlock_irqrestore(&dwc->lock, flags);
312
21e93c1e 313 if (callback)
3bfb1d20
HS
314 callback(param);
315}
316
317static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
318{
319 struct dw_desc *desc, *_desc;
320 LIST_HEAD(list);
69cea5a0 321 unsigned long flags;
3bfb1d20 322
69cea5a0 323 spin_lock_irqsave(&dwc->lock, flags);
3bfb1d20 324 if (dma_readl(dw, CH_EN) & dwc->mask) {
41d5e59c 325 dev_err(chan2dev(&dwc->chan),
3bfb1d20
HS
326 "BUG: XFER bit set, but channel not idle!\n");
327
328 /* Try to continue after resetting the channel... */
3f936207 329 dwc_chan_disable(dw, dwc);
3bfb1d20
HS
330 }
331
332 /*
333 * Submit queued descriptors ASAP, i.e. before we go through
334 * the completed ones.
335 */
3bfb1d20 336 list_splice_init(&dwc->active_list, &list);
f336e42f
VK
337 if (!list_empty(&dwc->queue)) {
338 list_move(dwc->queue.next, &dwc->active_list);
339 dwc_dostart(dwc, dwc_first_active(dwc));
340 }
3bfb1d20 341
69cea5a0
VK
342 spin_unlock_irqrestore(&dwc->lock, flags);
343
3bfb1d20 344 list_for_each_entry_safe(desc, _desc, &list, desc_node)
5fedefb8 345 dwc_descriptor_complete(dwc, desc, true);
3bfb1d20
HS
346}
347
4702d524
AS
348/* Returns how many bytes were already received from source */
349static inline u32 dwc_get_sent(struct dw_dma_chan *dwc)
350{
351 u32 ctlhi = channel_readl(dwc, CTL_HI);
352 u32 ctllo = channel_readl(dwc, CTL_LO);
353
354 return (ctlhi & DWC_CTLH_BLOCK_TS_MASK) * (1 << (ctllo >> 4 & 7));
355}
356
3bfb1d20
HS
357static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
358{
359 dma_addr_t llp;
360 struct dw_desc *desc, *_desc;
361 struct dw_desc *child;
362 u32 status_xfer;
69cea5a0 363 unsigned long flags;
3bfb1d20 364
69cea5a0 365 spin_lock_irqsave(&dwc->lock, flags);
3bfb1d20
HS
366 llp = channel_readl(dwc, LLP);
367 status_xfer = dma_readl(dw, RAW.XFER);
368
369 if (status_xfer & dwc->mask) {
370 /* Everything we've submitted is done */
371 dma_writel(dw, CLEAR.XFER, dwc->mask);
77bcc497
AS
372
373 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
fdf475fa
AS
374 struct list_head *head, *active = dwc->tx_node_active;
375
376 /*
377 * We are inside first active descriptor.
378 * Otherwise something is really wrong.
379 */
380 desc = dwc_first_active(dwc);
381
382 head = &desc->tx_list;
383 if (active != head) {
4702d524
AS
384 /* Update desc to reflect last sent one */
385 if (active != head->next)
386 desc = to_dw_desc(active->prev);
387
388 dwc->residue -= desc->len;
389
fdf475fa 390 child = to_dw_desc(active);
77bcc497
AS
391
392 /* Submit next block */
fdf475fa 393 dwc_do_single_block(dwc, child);
77bcc497 394
fdf475fa 395 spin_unlock_irqrestore(&dwc->lock, flags);
77bcc497
AS
396 return;
397 }
fdf475fa 398
77bcc497
AS
399 /* We are done here */
400 clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
401 }
4702d524
AS
402
403 dwc->residue = 0;
404
69cea5a0
VK
405 spin_unlock_irqrestore(&dwc->lock, flags);
406
3bfb1d20
HS
407 dwc_complete_all(dw, dwc);
408 return;
409 }
410
69cea5a0 411 if (list_empty(&dwc->active_list)) {
4702d524 412 dwc->residue = 0;
69cea5a0 413 spin_unlock_irqrestore(&dwc->lock, flags);
087809fc 414 return;
69cea5a0 415 }
087809fc 416
77bcc497
AS
417 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
418 dev_vdbg(chan2dev(&dwc->chan), "%s: soft LLP mode\n", __func__);
69cea5a0 419 spin_unlock_irqrestore(&dwc->lock, flags);
087809fc 420 return;
69cea5a0 421 }
087809fc 422
2e4c364e 423 dev_vdbg(chan2dev(&dwc->chan), "%s: llp=0x%llx\n", __func__,
2f45d613 424 (unsigned long long)llp);
3bfb1d20
HS
425
426 list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
75c61225 427 /* Initial residue value */
4702d524
AS
428 dwc->residue = desc->total_len;
429
75c61225 430 /* Check first descriptors addr */
69cea5a0
VK
431 if (desc->txd.phys == llp) {
432 spin_unlock_irqrestore(&dwc->lock, flags);
84adccfb 433 return;
69cea5a0 434 }
84adccfb 435
75c61225 436 /* Check first descriptors llp */
69cea5a0 437 if (desc->lli.llp == llp) {
3bfb1d20 438 /* This one is currently in progress */
4702d524 439 dwc->residue -= dwc_get_sent(dwc);
69cea5a0 440 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20 441 return;
69cea5a0 442 }
3bfb1d20 443
4702d524
AS
444 dwc->residue -= desc->len;
445 list_for_each_entry(child, &desc->tx_list, desc_node) {
69cea5a0 446 if (child->lli.llp == llp) {
3bfb1d20 447 /* Currently in progress */
4702d524 448 dwc->residue -= dwc_get_sent(dwc);
69cea5a0 449 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20 450 return;
69cea5a0 451 }
4702d524
AS
452 dwc->residue -= child->len;
453 }
3bfb1d20
HS
454
455 /*
456 * No descriptors so far seem to be in progress, i.e.
457 * this one must be done.
458 */
69cea5a0 459 spin_unlock_irqrestore(&dwc->lock, flags);
5fedefb8 460 dwc_descriptor_complete(dwc, desc, true);
69cea5a0 461 spin_lock_irqsave(&dwc->lock, flags);
3bfb1d20
HS
462 }
463
41d5e59c 464 dev_err(chan2dev(&dwc->chan),
3bfb1d20
HS
465 "BUG: All descriptors done, but channel not idle!\n");
466
467 /* Try to continue after resetting the channel... */
3f936207 468 dwc_chan_disable(dw, dwc);
3bfb1d20
HS
469
470 if (!list_empty(&dwc->queue)) {
f336e42f
VK
471 list_move(dwc->queue.next, &dwc->active_list);
472 dwc_dostart(dwc, dwc_first_active(dwc));
3bfb1d20 473 }
69cea5a0 474 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20
HS
475}
476
93aad1bc 477static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
3bfb1d20 478{
21d43f49
AS
479 dev_crit(chan2dev(&dwc->chan), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
480 lli->sar, lli->dar, lli->llp, lli->ctlhi, lli->ctllo);
3bfb1d20
HS
481}
482
483static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
484{
485 struct dw_desc *bad_desc;
486 struct dw_desc *child;
69cea5a0 487 unsigned long flags;
3bfb1d20
HS
488
489 dwc_scan_descriptors(dw, dwc);
490
69cea5a0
VK
491 spin_lock_irqsave(&dwc->lock, flags);
492
3bfb1d20
HS
493 /*
494 * The descriptor currently at the head of the active list is
495 * borked. Since we don't have any way to report errors, we'll
496 * just have to scream loudly and try to carry on.
497 */
498 bad_desc = dwc_first_active(dwc);
499 list_del_init(&bad_desc->desc_node);
f336e42f 500 list_move(dwc->queue.next, dwc->active_list.prev);
3bfb1d20
HS
501
502 /* Clear the error flag and try to restart the controller */
503 dma_writel(dw, CLEAR.ERROR, dwc->mask);
504 if (!list_empty(&dwc->active_list))
505 dwc_dostart(dwc, dwc_first_active(dwc));
506
507 /*
ba84bd71 508 * WARN may seem harsh, but since this only happens
3bfb1d20
HS
509 * when someone submits a bad physical address in a
510 * descriptor, we should consider ourselves lucky that the
511 * controller flagged an error instead of scribbling over
512 * random memory locations.
513 */
ba84bd71
AS
514 dev_WARN(chan2dev(&dwc->chan), "Bad descriptor submitted for DMA!\n"
515 " cookie: %d\n", bad_desc->txd.cookie);
3bfb1d20 516 dwc_dump_lli(dwc, &bad_desc->lli);
e0bd0f8c 517 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
3bfb1d20
HS
518 dwc_dump_lli(dwc, &child->lli);
519
69cea5a0
VK
520 spin_unlock_irqrestore(&dwc->lock, flags);
521
3bfb1d20 522 /* Pretend the descriptor completed successfully */
5fedefb8 523 dwc_descriptor_complete(dwc, bad_desc, true);
3bfb1d20
HS
524}
525
d9de4519
HCE
526/* --------------------- Cyclic DMA API extensions -------------------- */
527
8004cbb4 528dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
d9de4519
HCE
529{
530 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
531 return channel_readl(dwc, SAR);
532}
533EXPORT_SYMBOL(dw_dma_get_src_addr);
534
8004cbb4 535dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
d9de4519
HCE
536{
537 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
538 return channel_readl(dwc, DAR);
539}
540EXPORT_SYMBOL(dw_dma_get_dst_addr);
541
75c61225 542/* Called with dwc->lock held and all DMAC interrupts disabled */
d9de4519 543static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
ff7b05f2 544 u32 status_err, u32 status_xfer)
d9de4519 545{
69cea5a0
VK
546 unsigned long flags;
547
ff7b05f2 548 if (dwc->mask) {
d9de4519
HCE
549 void (*callback)(void *param);
550 void *callback_param;
551
552 dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
553 channel_readl(dwc, LLP));
d9de4519
HCE
554
555 callback = dwc->cdesc->period_callback;
556 callback_param = dwc->cdesc->period_callback_param;
69cea5a0
VK
557
558 if (callback)
d9de4519 559 callback(callback_param);
d9de4519
HCE
560 }
561
562 /*
563 * Error and transfer complete are highly unlikely, and will most
564 * likely be due to a configuration error by the user.
565 */
566 if (unlikely(status_err & dwc->mask) ||
567 unlikely(status_xfer & dwc->mask)) {
568 int i;
569
570 dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s "
571 "interrupt, stopping DMA transfer\n",
572 status_xfer ? "xfer" : "error");
69cea5a0
VK
573
574 spin_lock_irqsave(&dwc->lock, flags);
575
1d455437 576 dwc_dump_chan_regs(dwc);
d9de4519 577
3f936207 578 dwc_chan_disable(dw, dwc);
d9de4519 579
75c61225 580 /* Make sure DMA does not restart by loading a new list */
d9de4519
HCE
581 channel_writel(dwc, LLP, 0);
582 channel_writel(dwc, CTL_LO, 0);
583 channel_writel(dwc, CTL_HI, 0);
584
d9de4519
HCE
585 dma_writel(dw, CLEAR.ERROR, dwc->mask);
586 dma_writel(dw, CLEAR.XFER, dwc->mask);
587
588 for (i = 0; i < dwc->cdesc->periods; i++)
589 dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
69cea5a0
VK
590
591 spin_unlock_irqrestore(&dwc->lock, flags);
d9de4519
HCE
592 }
593}
594
595/* ------------------------------------------------------------------------- */
596
3bfb1d20
HS
597static void dw_dma_tasklet(unsigned long data)
598{
599 struct dw_dma *dw = (struct dw_dma *)data;
600 struct dw_dma_chan *dwc;
3bfb1d20
HS
601 u32 status_xfer;
602 u32 status_err;
603 int i;
604
7fe7b2f4 605 status_xfer = dma_readl(dw, RAW.XFER);
3bfb1d20
HS
606 status_err = dma_readl(dw, RAW.ERROR);
607
2e4c364e 608 dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err);
3bfb1d20
HS
609
610 for (i = 0; i < dw->dma.chancnt; i++) {
611 dwc = &dw->chan[i];
d9de4519 612 if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
ff7b05f2 613 dwc_handle_cyclic(dw, dwc, status_err, status_xfer);
d9de4519 614 else if (status_err & (1 << i))
3bfb1d20 615 dwc_handle_error(dw, dwc);
77bcc497 616 else if (status_xfer & (1 << i))
3bfb1d20 617 dwc_scan_descriptors(dw, dwc);
3bfb1d20
HS
618 }
619
620 /*
ff7b05f2 621 * Re-enable interrupts.
3bfb1d20
HS
622 */
623 channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
3bfb1d20
HS
624 channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
625}
626
627static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
628{
629 struct dw_dma *dw = dev_id;
3783cef8 630 u32 status = dma_readl(dw, STATUS_INT);
3bfb1d20 631
3783cef8
AS
632 dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__, status);
633
634 /* Check if we have any interrupt from the DMAC */
635 if (!status)
636 return IRQ_NONE;
3bfb1d20
HS
637
638 /*
639 * Just disable the interrupts. We'll turn them back on in the
640 * softirq handler.
641 */
642 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
3bfb1d20
HS
643 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
644
645 status = dma_readl(dw, STATUS_INT);
646 if (status) {
647 dev_err(dw->dma.dev,
648 "BUG: Unexpected interrupts pending: 0x%x\n",
649 status);
650
651 /* Try to recover */
652 channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
3bfb1d20
HS
653 channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
654 channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
655 channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
656 }
657
658 tasklet_schedule(&dw->tasklet);
659
660 return IRQ_HANDLED;
661}
662
663/*----------------------------------------------------------------------*/
664
665static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
666{
667 struct dw_desc *desc = txd_to_dw_desc(tx);
668 struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
669 dma_cookie_t cookie;
69cea5a0 670 unsigned long flags;
3bfb1d20 671
69cea5a0 672 spin_lock_irqsave(&dwc->lock, flags);
884485e1 673 cookie = dma_cookie_assign(tx);
3bfb1d20
HS
674
675 /*
676 * REVISIT: We should attempt to chain as many descriptors as
677 * possible, perhaps even appending to those already submitted
678 * for DMA. But this is hard to do in a race-free manner.
679 */
680 if (list_empty(&dwc->active_list)) {
2e4c364e 681 dev_vdbg(chan2dev(tx->chan), "%s: started %u\n", __func__,
3bfb1d20 682 desc->txd.cookie);
3bfb1d20 683 list_add_tail(&desc->desc_node, &dwc->active_list);
f336e42f 684 dwc_dostart(dwc, dwc_first_active(dwc));
3bfb1d20 685 } else {
2e4c364e 686 dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n", __func__,
3bfb1d20
HS
687 desc->txd.cookie);
688
689 list_add_tail(&desc->desc_node, &dwc->queue);
690 }
691
69cea5a0 692 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20
HS
693
694 return cookie;
695}
696
697static struct dma_async_tx_descriptor *
698dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
699 size_t len, unsigned long flags)
700{
701 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
f776076b 702 struct dw_dma *dw = to_dw_dma(chan->device);
3bfb1d20
HS
703 struct dw_desc *desc;
704 struct dw_desc *first;
705 struct dw_desc *prev;
706 size_t xfer_count;
707 size_t offset;
708 unsigned int src_width;
709 unsigned int dst_width;
3d4f8605 710 unsigned int data_width;
3bfb1d20
HS
711 u32 ctllo;
712
2f45d613 713 dev_vdbg(chan2dev(chan),
2e4c364e 714 "%s: d0x%llx s0x%llx l0x%zx f0x%lx\n", __func__,
2f45d613
AS
715 (unsigned long long)dest, (unsigned long long)src,
716 len, flags);
3bfb1d20
HS
717
718 if (unlikely(!len)) {
2e4c364e 719 dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
3bfb1d20
HS
720 return NULL;
721 }
722
0fdb567f
AS
723 dwc->direction = DMA_MEM_TO_MEM;
724
f776076b
AB
725 data_width = min_t(unsigned int, dw->data_width[dwc->src_master],
726 dw->data_width[dwc->dst_master]);
a0982004 727
3d4f8605
AS
728 src_width = dst_width = min_t(unsigned int, data_width,
729 dwc_fast_fls(src | dest | len));
3bfb1d20 730
327e6970 731 ctllo = DWC_DEFAULT_CTLLO(chan)
3bfb1d20
HS
732 | DWC_CTLL_DST_WIDTH(dst_width)
733 | DWC_CTLL_SRC_WIDTH(src_width)
734 | DWC_CTLL_DST_INC
735 | DWC_CTLL_SRC_INC
736 | DWC_CTLL_FC_M2M;
737 prev = first = NULL;
738
739 for (offset = 0; offset < len; offset += xfer_count << src_width) {
740 xfer_count = min_t(size_t, (len - offset) >> src_width,
4a63a8b3 741 dwc->block_size);
3bfb1d20
HS
742
743 desc = dwc_desc_get(dwc);
744 if (!desc)
745 goto err_desc_get;
746
747 desc->lli.sar = src + offset;
748 desc->lli.dar = dest + offset;
749 desc->lli.ctllo = ctllo;
750 desc->lli.ctlhi = xfer_count;
176dcec5 751 desc->len = xfer_count << src_width;
3bfb1d20
HS
752
753 if (!first) {
754 first = desc;
755 } else {
756 prev->lli.llp = desc->txd.phys;
3bfb1d20 757 list_add_tail(&desc->desc_node,
e0bd0f8c 758 &first->tx_list);
3bfb1d20
HS
759 }
760 prev = desc;
761 }
762
3bfb1d20
HS
763 if (flags & DMA_PREP_INTERRUPT)
764 /* Trigger interrupt after last block */
765 prev->lli.ctllo |= DWC_CTLL_INT_EN;
766
767 prev->lli.llp = 0;
3bfb1d20 768 first->txd.flags = flags;
30d38a32 769 first->total_len = len;
3bfb1d20
HS
770
771 return &first->txd;
772
773err_desc_get:
774 dwc_desc_put(dwc, first);
775 return NULL;
776}
777
778static struct dma_async_tx_descriptor *
779dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
db8196df 780 unsigned int sg_len, enum dma_transfer_direction direction,
185ecb5f 781 unsigned long flags, void *context)
3bfb1d20
HS
782{
783 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
f776076b 784 struct dw_dma *dw = to_dw_dma(chan->device);
327e6970 785 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
3bfb1d20
HS
786 struct dw_desc *prev;
787 struct dw_desc *first;
788 u32 ctllo;
789 dma_addr_t reg;
790 unsigned int reg_width;
791 unsigned int mem_width;
a0982004 792 unsigned int data_width;
3bfb1d20
HS
793 unsigned int i;
794 struct scatterlist *sg;
795 size_t total_len = 0;
796
2e4c364e 797 dev_vdbg(chan2dev(chan), "%s\n", __func__);
3bfb1d20 798
495aea4b 799 if (unlikely(!is_slave_direction(direction) || !sg_len))
3bfb1d20
HS
800 return NULL;
801
0fdb567f
AS
802 dwc->direction = direction;
803
3bfb1d20
HS
804 prev = first = NULL;
805
3bfb1d20 806 switch (direction) {
db8196df 807 case DMA_MEM_TO_DEV:
327e6970
VK
808 reg_width = __fls(sconfig->dst_addr_width);
809 reg = sconfig->dst_addr;
810 ctllo = (DWC_DEFAULT_CTLLO(chan)
3bfb1d20
HS
811 | DWC_CTLL_DST_WIDTH(reg_width)
812 | DWC_CTLL_DST_FIX
327e6970
VK
813 | DWC_CTLL_SRC_INC);
814
815 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
816 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
817
f776076b 818 data_width = dw->data_width[dwc->src_master];
a0982004 819
3bfb1d20
HS
820 for_each_sg(sgl, sg, sg_len, i) {
821 struct dw_desc *desc;
69dc14b5 822 u32 len, dlen, mem;
3bfb1d20 823
cbb796cc 824 mem = sg_dma_address(sg);
69dc14b5 825 len = sg_dma_len(sg);
6bc711f6 826
a0982004
AS
827 mem_width = min_t(unsigned int,
828 data_width, dwc_fast_fls(mem | len));
3bfb1d20 829
69dc14b5 830slave_sg_todev_fill_desc:
3bfb1d20
HS
831 desc = dwc_desc_get(dwc);
832 if (!desc) {
41d5e59c 833 dev_err(chan2dev(chan),
3bfb1d20
HS
834 "not enough descriptors available\n");
835 goto err_desc_get;
836 }
837
3bfb1d20
HS
838 desc->lli.sar = mem;
839 desc->lli.dar = reg;
840 desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
4a63a8b3
AS
841 if ((len >> mem_width) > dwc->block_size) {
842 dlen = dwc->block_size << mem_width;
69dc14b5
VK
843 mem += dlen;
844 len -= dlen;
845 } else {
846 dlen = len;
847 len = 0;
848 }
849
850 desc->lli.ctlhi = dlen >> mem_width;
176dcec5 851 desc->len = dlen;
3bfb1d20
HS
852
853 if (!first) {
854 first = desc;
855 } else {
856 prev->lli.llp = desc->txd.phys;
3bfb1d20 857 list_add_tail(&desc->desc_node,
e0bd0f8c 858 &first->tx_list);
3bfb1d20
HS
859 }
860 prev = desc;
69dc14b5
VK
861 total_len += dlen;
862
863 if (len)
864 goto slave_sg_todev_fill_desc;
3bfb1d20
HS
865 }
866 break;
db8196df 867 case DMA_DEV_TO_MEM:
327e6970
VK
868 reg_width = __fls(sconfig->src_addr_width);
869 reg = sconfig->src_addr;
870 ctllo = (DWC_DEFAULT_CTLLO(chan)
3bfb1d20
HS
871 | DWC_CTLL_SRC_WIDTH(reg_width)
872 | DWC_CTLL_DST_INC
327e6970
VK
873 | DWC_CTLL_SRC_FIX);
874
875 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
876 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
3bfb1d20 877
f776076b 878 data_width = dw->data_width[dwc->dst_master];
a0982004 879
3bfb1d20
HS
880 for_each_sg(sgl, sg, sg_len, i) {
881 struct dw_desc *desc;
69dc14b5 882 u32 len, dlen, mem;
3bfb1d20 883
cbb796cc 884 mem = sg_dma_address(sg);
3bfb1d20 885 len = sg_dma_len(sg);
6bc711f6 886
a0982004
AS
887 mem_width = min_t(unsigned int,
888 data_width, dwc_fast_fls(mem | len));
3bfb1d20 889
69dc14b5
VK
890slave_sg_fromdev_fill_desc:
891 desc = dwc_desc_get(dwc);
892 if (!desc) {
893 dev_err(chan2dev(chan),
894 "not enough descriptors available\n");
895 goto err_desc_get;
896 }
897
3bfb1d20
HS
898 desc->lli.sar = reg;
899 desc->lli.dar = mem;
900 desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
4a63a8b3
AS
901 if ((len >> reg_width) > dwc->block_size) {
902 dlen = dwc->block_size << reg_width;
69dc14b5
VK
903 mem += dlen;
904 len -= dlen;
905 } else {
906 dlen = len;
907 len = 0;
908 }
909 desc->lli.ctlhi = dlen >> reg_width;
176dcec5 910 desc->len = dlen;
3bfb1d20
HS
911
912 if (!first) {
913 first = desc;
914 } else {
915 prev->lli.llp = desc->txd.phys;
3bfb1d20 916 list_add_tail(&desc->desc_node,
e0bd0f8c 917 &first->tx_list);
3bfb1d20
HS
918 }
919 prev = desc;
69dc14b5
VK
920 total_len += dlen;
921
922 if (len)
923 goto slave_sg_fromdev_fill_desc;
3bfb1d20
HS
924 }
925 break;
926 default:
927 return NULL;
928 }
929
930 if (flags & DMA_PREP_INTERRUPT)
931 /* Trigger interrupt after last block */
932 prev->lli.ctllo |= DWC_CTLL_INT_EN;
933
934 prev->lli.llp = 0;
30d38a32 935 first->total_len = total_len;
3bfb1d20
HS
936
937 return &first->txd;
938
939err_desc_get:
940 dwc_desc_put(dwc, first);
941 return NULL;
942}
943
327e6970
VK
944/*
945 * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
946 * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
947 *
948 * NOTE: burst size 2 is not supported by controller.
949 *
950 * This can be done by finding least significant bit set: n & (n - 1)
951 */
952static inline void convert_burst(u32 *maxburst)
953{
954 if (*maxburst > 1)
955 *maxburst = fls(*maxburst) - 2;
956 else
957 *maxburst = 0;
958}
959
960static int
961set_runtime_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
962{
963 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
964
495aea4b
AS
965 /* Check if chan will be configured for slave transfers */
966 if (!is_slave_direction(sconfig->direction))
327e6970
VK
967 return -EINVAL;
968
969 memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
0fdb567f 970 dwc->direction = sconfig->direction;
327e6970 971
f776076b 972 /* Take the request line from slave_id member */
78f3c9d2 973 if (is_request_line_unset(dwc))
f776076b
AB
974 dwc->request_line = sconfig->slave_id;
975
327e6970
VK
976 convert_burst(&dwc->dma_sconfig.src_maxburst);
977 convert_burst(&dwc->dma_sconfig.dst_maxburst);
978
979 return 0;
980}
981
21fe3c52
AS
982static inline void dwc_chan_pause(struct dw_dma_chan *dwc)
983{
984 u32 cfglo = channel_readl(dwc, CFG_LO);
123b69ab 985 unsigned int count = 20; /* timeout iterations */
21fe3c52
AS
986
987 channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
123b69ab
AS
988 while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY) && count--)
989 udelay(2);
21fe3c52
AS
990
991 dwc->paused = true;
992}
993
994static inline void dwc_chan_resume(struct dw_dma_chan *dwc)
995{
996 u32 cfglo = channel_readl(dwc, CFG_LO);
997
998 channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
999
1000 dwc->paused = false;
1001}
1002
05827630
LW
1003static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1004 unsigned long arg)
3bfb1d20
HS
1005{
1006 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1007 struct dw_dma *dw = to_dw_dma(chan->device);
1008 struct dw_desc *desc, *_desc;
69cea5a0 1009 unsigned long flags;
3bfb1d20
HS
1010 LIST_HEAD(list);
1011
a7c57cf7
LW
1012 if (cmd == DMA_PAUSE) {
1013 spin_lock_irqsave(&dwc->lock, flags);
c3635c78 1014
21fe3c52 1015 dwc_chan_pause(dwc);
3bfb1d20 1016
a7c57cf7
LW
1017 spin_unlock_irqrestore(&dwc->lock, flags);
1018 } else if (cmd == DMA_RESUME) {
1019 if (!dwc->paused)
1020 return 0;
3bfb1d20 1021
a7c57cf7 1022 spin_lock_irqsave(&dwc->lock, flags);
3bfb1d20 1023
21fe3c52 1024 dwc_chan_resume(dwc);
3bfb1d20 1025
a7c57cf7
LW
1026 spin_unlock_irqrestore(&dwc->lock, flags);
1027 } else if (cmd == DMA_TERMINATE_ALL) {
1028 spin_lock_irqsave(&dwc->lock, flags);
3bfb1d20 1029
fed2574b
AS
1030 clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
1031
3f936207 1032 dwc_chan_disable(dw, dwc);
a7c57cf7 1033
a5dbff11 1034 dwc_chan_resume(dwc);
a7c57cf7
LW
1035
1036 /* active_list entries will end up before queued entries */
1037 list_splice_init(&dwc->queue, &list);
1038 list_splice_init(&dwc->active_list, &list);
1039
1040 spin_unlock_irqrestore(&dwc->lock, flags);
1041
1042 /* Flush all pending and queued descriptors */
1043 list_for_each_entry_safe(desc, _desc, &list, desc_node)
1044 dwc_descriptor_complete(dwc, desc, false);
327e6970
VK
1045 } else if (cmd == DMA_SLAVE_CONFIG) {
1046 return set_runtime_config(chan, (struct dma_slave_config *)arg);
1047 } else {
a7c57cf7 1048 return -ENXIO;
327e6970 1049 }
c3635c78
LW
1050
1051 return 0;
3bfb1d20
HS
1052}
1053
4702d524
AS
1054static inline u32 dwc_get_residue(struct dw_dma_chan *dwc)
1055{
1056 unsigned long flags;
1057 u32 residue;
1058
1059 spin_lock_irqsave(&dwc->lock, flags);
1060
1061 residue = dwc->residue;
1062 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags) && residue)
1063 residue -= dwc_get_sent(dwc);
1064
1065 spin_unlock_irqrestore(&dwc->lock, flags);
1066 return residue;
1067}
1068
3bfb1d20 1069static enum dma_status
07934481
LW
1070dwc_tx_status(struct dma_chan *chan,
1071 dma_cookie_t cookie,
1072 struct dma_tx_state *txstate)
3bfb1d20
HS
1073{
1074 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
96a2af41 1075 enum dma_status ret;
3bfb1d20 1076
96a2af41 1077 ret = dma_cookie_status(chan, cookie, txstate);
2c40410b 1078 if (ret == DMA_COMPLETE)
12381dc0 1079 return ret;
3bfb1d20 1080
12381dc0 1081 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
3bfb1d20 1082
12381dc0 1083 ret = dma_cookie_status(chan, cookie, txstate);
2c40410b 1084 if (ret != DMA_COMPLETE)
4702d524 1085 dma_set_residue(txstate, dwc_get_residue(dwc));
3bfb1d20 1086
effd5cf6 1087 if (dwc->paused && ret == DMA_IN_PROGRESS)
a7c57cf7 1088 return DMA_PAUSED;
3bfb1d20
HS
1089
1090 return ret;
1091}
1092
1093static void dwc_issue_pending(struct dma_chan *chan)
1094{
1095 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1096
3bfb1d20
HS
1097 if (!list_empty(&dwc->queue))
1098 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
3bfb1d20
HS
1099}
1100
aa1e6f1a 1101static int dwc_alloc_chan_resources(struct dma_chan *chan)
3bfb1d20
HS
1102{
1103 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1104 struct dw_dma *dw = to_dw_dma(chan->device);
1105 struct dw_desc *desc;
3bfb1d20 1106 int i;
69cea5a0 1107 unsigned long flags;
3bfb1d20 1108
2e4c364e 1109 dev_vdbg(chan2dev(chan), "%s\n", __func__);
3bfb1d20 1110
3bfb1d20
HS
1111 /* ASSERT: channel is idle */
1112 if (dma_readl(dw, CH_EN) & dwc->mask) {
41d5e59c 1113 dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
3bfb1d20
HS
1114 return -EIO;
1115 }
1116
d3ee98cd 1117 dma_cookie_init(chan);
3bfb1d20 1118
3bfb1d20
HS
1119 /*
1120 * NOTE: some controllers may have additional features that we
1121 * need to initialize here, like "scatter-gather" (which
1122 * doesn't mean what you think it means), and status writeback.
1123 */
1124
f776076b
AB
1125 dwc_set_masters(dwc);
1126
69cea5a0 1127 spin_lock_irqsave(&dwc->lock, flags);
3bfb1d20
HS
1128 i = dwc->descs_allocated;
1129 while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
f8122a82
AS
1130 dma_addr_t phys;
1131
69cea5a0 1132 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20 1133
f8122a82 1134 desc = dma_pool_alloc(dw->desc_pool, GFP_ATOMIC, &phys);
cbd65312
AS
1135 if (!desc)
1136 goto err_desc_alloc;
3bfb1d20 1137
f8122a82 1138 memset(desc, 0, sizeof(struct dw_desc));
3bfb1d20 1139
e0bd0f8c 1140 INIT_LIST_HEAD(&desc->tx_list);
3bfb1d20
HS
1141 dma_async_tx_descriptor_init(&desc->txd, chan);
1142 desc->txd.tx_submit = dwc_tx_submit;
1143 desc->txd.flags = DMA_CTRL_ACK;
f8122a82 1144 desc->txd.phys = phys;
cbd65312 1145
3bfb1d20
HS
1146 dwc_desc_put(dwc, desc);
1147
69cea5a0 1148 spin_lock_irqsave(&dwc->lock, flags);
3bfb1d20
HS
1149 i = ++dwc->descs_allocated;
1150 }
1151
69cea5a0 1152 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20 1153
2e4c364e 1154 dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
3bfb1d20 1155
cbd65312
AS
1156 return i;
1157
1158err_desc_alloc:
cbd65312
AS
1159 dev_info(chan2dev(chan), "only allocated %d descriptors\n", i);
1160
3bfb1d20
HS
1161 return i;
1162}
1163
1164static void dwc_free_chan_resources(struct dma_chan *chan)
1165{
1166 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1167 struct dw_dma *dw = to_dw_dma(chan->device);
1168 struct dw_desc *desc, *_desc;
69cea5a0 1169 unsigned long flags;
3bfb1d20
HS
1170 LIST_HEAD(list);
1171
2e4c364e 1172 dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
3bfb1d20
HS
1173 dwc->descs_allocated);
1174
1175 /* ASSERT: channel is idle */
1176 BUG_ON(!list_empty(&dwc->active_list));
1177 BUG_ON(!list_empty(&dwc->queue));
1178 BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
1179
69cea5a0 1180 spin_lock_irqsave(&dwc->lock, flags);
3bfb1d20
HS
1181 list_splice_init(&dwc->free_list, &list);
1182 dwc->descs_allocated = 0;
61e183f8 1183 dwc->initialized = false;
f776076b 1184 dwc->request_line = ~0;
3bfb1d20
HS
1185
1186 /* Disable interrupts */
1187 channel_clear_bit(dw, MASK.XFER, dwc->mask);
3bfb1d20
HS
1188 channel_clear_bit(dw, MASK.ERROR, dwc->mask);
1189
69cea5a0 1190 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20
HS
1191
1192 list_for_each_entry_safe(desc, _desc, &list, desc_node) {
41d5e59c 1193 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
f8122a82 1194 dma_pool_free(dw->desc_pool, desc, desc->txd.phys);
3bfb1d20
HS
1195 }
1196
2e4c364e 1197 dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
3bfb1d20
HS
1198}
1199
d9de4519
HCE
1200/* --------------------- Cyclic DMA API extensions -------------------- */
1201
1202/**
1203 * dw_dma_cyclic_start - start the cyclic DMA transfer
1204 * @chan: the DMA channel to start
1205 *
1206 * Must be called with soft interrupts disabled. Returns zero on success or
1207 * -errno on failure.
1208 */
1209int dw_dma_cyclic_start(struct dma_chan *chan)
1210{
1211 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1212 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
69cea5a0 1213 unsigned long flags;
d9de4519
HCE
1214
1215 if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
1216 dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
1217 return -ENODEV;
1218 }
1219
69cea5a0 1220 spin_lock_irqsave(&dwc->lock, flags);
d9de4519 1221
75c61225 1222 /* Assert channel is idle */
d9de4519
HCE
1223 if (dma_readl(dw, CH_EN) & dwc->mask) {
1224 dev_err(chan2dev(&dwc->chan),
1225 "BUG: Attempted to start non-idle channel\n");
1d455437 1226 dwc_dump_chan_regs(dwc);
69cea5a0 1227 spin_unlock_irqrestore(&dwc->lock, flags);
d9de4519
HCE
1228 return -EBUSY;
1229 }
1230
d9de4519
HCE
1231 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1232 dma_writel(dw, CLEAR.XFER, dwc->mask);
1233
75c61225 1234 /* Setup DMAC channel registers */
d9de4519
HCE
1235 channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
1236 channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
1237 channel_writel(dwc, CTL_HI, 0);
1238
1239 channel_set_bit(dw, CH_EN, dwc->mask);
1240
69cea5a0 1241 spin_unlock_irqrestore(&dwc->lock, flags);
d9de4519
HCE
1242
1243 return 0;
1244}
1245EXPORT_SYMBOL(dw_dma_cyclic_start);
1246
1247/**
1248 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1249 * @chan: the DMA channel to stop
1250 *
1251 * Must be called with soft interrupts disabled.
1252 */
1253void dw_dma_cyclic_stop(struct dma_chan *chan)
1254{
1255 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1256 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
69cea5a0 1257 unsigned long flags;
d9de4519 1258
69cea5a0 1259 spin_lock_irqsave(&dwc->lock, flags);
d9de4519 1260
3f936207 1261 dwc_chan_disable(dw, dwc);
d9de4519 1262
69cea5a0 1263 spin_unlock_irqrestore(&dwc->lock, flags);
d9de4519
HCE
1264}
1265EXPORT_SYMBOL(dw_dma_cyclic_stop);
1266
1267/**
1268 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1269 * @chan: the DMA channel to prepare
1270 * @buf_addr: physical DMA address where the buffer starts
1271 * @buf_len: total number of bytes for the entire buffer
1272 * @period_len: number of bytes for each period
1273 * @direction: transfer direction, to or from device
1274 *
1275 * Must be called before trying to start the transfer. Returns a valid struct
1276 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1277 */
1278struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
1279 dma_addr_t buf_addr, size_t buf_len, size_t period_len,
db8196df 1280 enum dma_transfer_direction direction)
d9de4519
HCE
1281{
1282 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
327e6970 1283 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
d9de4519
HCE
1284 struct dw_cyclic_desc *cdesc;
1285 struct dw_cyclic_desc *retval = NULL;
1286 struct dw_desc *desc;
1287 struct dw_desc *last = NULL;
d9de4519
HCE
1288 unsigned long was_cyclic;
1289 unsigned int reg_width;
1290 unsigned int periods;
1291 unsigned int i;
69cea5a0 1292 unsigned long flags;
d9de4519 1293
69cea5a0 1294 spin_lock_irqsave(&dwc->lock, flags);
fed2574b
AS
1295 if (dwc->nollp) {
1296 spin_unlock_irqrestore(&dwc->lock, flags);
1297 dev_dbg(chan2dev(&dwc->chan),
1298 "channel doesn't support LLP transfers\n");
1299 return ERR_PTR(-EINVAL);
1300 }
1301
d9de4519 1302 if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
69cea5a0 1303 spin_unlock_irqrestore(&dwc->lock, flags);
d9de4519
HCE
1304 dev_dbg(chan2dev(&dwc->chan),
1305 "queue and/or active list are not empty\n");
1306 return ERR_PTR(-EBUSY);
1307 }
1308
1309 was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
69cea5a0 1310 spin_unlock_irqrestore(&dwc->lock, flags);
d9de4519
HCE
1311 if (was_cyclic) {
1312 dev_dbg(chan2dev(&dwc->chan),
1313 "channel already prepared for cyclic DMA\n");
1314 return ERR_PTR(-EBUSY);
1315 }
1316
1317 retval = ERR_PTR(-EINVAL);
327e6970 1318
f44b92f4
AS
1319 if (unlikely(!is_slave_direction(direction)))
1320 goto out_err;
1321
0fdb567f
AS
1322 dwc->direction = direction;
1323
327e6970
VK
1324 if (direction == DMA_MEM_TO_DEV)
1325 reg_width = __ffs(sconfig->dst_addr_width);
1326 else
1327 reg_width = __ffs(sconfig->src_addr_width);
1328
d9de4519
HCE
1329 periods = buf_len / period_len;
1330
1331 /* Check for too big/unaligned periods and unaligned DMA buffer. */
4a63a8b3 1332 if (period_len > (dwc->block_size << reg_width))
d9de4519
HCE
1333 goto out_err;
1334 if (unlikely(period_len & ((1 << reg_width) - 1)))
1335 goto out_err;
1336 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
1337 goto out_err;
d9de4519
HCE
1338
1339 retval = ERR_PTR(-ENOMEM);
1340
1341 if (periods > NR_DESCS_PER_CHANNEL)
1342 goto out_err;
1343
1344 cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
1345 if (!cdesc)
1346 goto out_err;
1347
1348 cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
1349 if (!cdesc->desc)
1350 goto out_err_alloc;
1351
1352 for (i = 0; i < periods; i++) {
1353 desc = dwc_desc_get(dwc);
1354 if (!desc)
1355 goto out_err_desc_get;
1356
1357 switch (direction) {
db8196df 1358 case DMA_MEM_TO_DEV:
327e6970 1359 desc->lli.dar = sconfig->dst_addr;
d9de4519 1360 desc->lli.sar = buf_addr + (period_len * i);
327e6970 1361 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
d9de4519
HCE
1362 | DWC_CTLL_DST_WIDTH(reg_width)
1363 | DWC_CTLL_SRC_WIDTH(reg_width)
1364 | DWC_CTLL_DST_FIX
1365 | DWC_CTLL_SRC_INC
d9de4519 1366 | DWC_CTLL_INT_EN);
327e6970
VK
1367
1368 desc->lli.ctllo |= sconfig->device_fc ?
1369 DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
1370 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
1371
d9de4519 1372 break;
db8196df 1373 case DMA_DEV_TO_MEM:
d9de4519 1374 desc->lli.dar = buf_addr + (period_len * i);
327e6970
VK
1375 desc->lli.sar = sconfig->src_addr;
1376 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
d9de4519
HCE
1377 | DWC_CTLL_SRC_WIDTH(reg_width)
1378 | DWC_CTLL_DST_WIDTH(reg_width)
1379 | DWC_CTLL_DST_INC
1380 | DWC_CTLL_SRC_FIX
d9de4519 1381 | DWC_CTLL_INT_EN);
327e6970
VK
1382
1383 desc->lli.ctllo |= sconfig->device_fc ?
1384 DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
1385 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
1386
d9de4519
HCE
1387 break;
1388 default:
1389 break;
1390 }
1391
1392 desc->lli.ctlhi = (period_len >> reg_width);
1393 cdesc->desc[i] = desc;
1394
f8122a82 1395 if (last)
d9de4519 1396 last->lli.llp = desc->txd.phys;
d9de4519
HCE
1397
1398 last = desc;
1399 }
1400
75c61225 1401 /* Let's make a cyclic list */
d9de4519 1402 last->lli.llp = cdesc->desc[0]->txd.phys;
d9de4519 1403
2f45d613
AS
1404 dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%llx len %zu "
1405 "period %zu periods %d\n", (unsigned long long)buf_addr,
1406 buf_len, period_len, periods);
d9de4519
HCE
1407
1408 cdesc->periods = periods;
1409 dwc->cdesc = cdesc;
1410
1411 return cdesc;
1412
1413out_err_desc_get:
1414 while (i--)
1415 dwc_desc_put(dwc, cdesc->desc[i]);
1416out_err_alloc:
1417 kfree(cdesc);
1418out_err:
1419 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1420 return (struct dw_cyclic_desc *)retval;
1421}
1422EXPORT_SYMBOL(dw_dma_cyclic_prep);
1423
1424/**
1425 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1426 * @chan: the DMA channel to free
1427 */
1428void dw_dma_cyclic_free(struct dma_chan *chan)
1429{
1430 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1431 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1432 struct dw_cyclic_desc *cdesc = dwc->cdesc;
1433 int i;
69cea5a0 1434 unsigned long flags;
d9de4519 1435
2e4c364e 1436 dev_dbg(chan2dev(&dwc->chan), "%s\n", __func__);
d9de4519
HCE
1437
1438 if (!cdesc)
1439 return;
1440
69cea5a0 1441 spin_lock_irqsave(&dwc->lock, flags);
d9de4519 1442
3f936207 1443 dwc_chan_disable(dw, dwc);
d9de4519 1444
d9de4519
HCE
1445 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1446 dma_writel(dw, CLEAR.XFER, dwc->mask);
1447
69cea5a0 1448 spin_unlock_irqrestore(&dwc->lock, flags);
d9de4519
HCE
1449
1450 for (i = 0; i < cdesc->periods; i++)
1451 dwc_desc_put(dwc, cdesc->desc[i]);
1452
1453 kfree(cdesc->desc);
1454 kfree(cdesc);
1455
1456 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1457}
1458EXPORT_SYMBOL(dw_dma_cyclic_free);
1459
3bfb1d20
HS
1460/*----------------------------------------------------------------------*/
1461
1462static void dw_dma_off(struct dw_dma *dw)
1463{
61e183f8
VK
1464 int i;
1465
3bfb1d20
HS
1466 dma_writel(dw, CFG, 0);
1467
1468 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
3bfb1d20
HS
1469 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1470 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1471 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1472
1473 while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
1474 cpu_relax();
61e183f8
VK
1475
1476 for (i = 0; i < dw->dma.chancnt; i++)
1477 dw->chan[i].initialized = false;
3bfb1d20
HS
1478}
1479
9cade1a4 1480int dw_dma_probe(struct dw_dma_chip *chip, struct dw_dma_platform_data *pdata)
a9ddb575 1481{
3bfb1d20
HS
1482 struct dw_dma *dw;
1483 size_t size;
482c67ea
AS
1484 bool autocfg;
1485 unsigned int dw_params;
1486 unsigned int nr_channels;
4a63a8b3 1487 unsigned int max_blk_size = 0;
3bfb1d20
HS
1488 int err;
1489 int i;
1490
9cade1a4 1491 dw_params = dma_read_byaddr(chip->regs, DW_PARAMS);
482c67ea
AS
1492 autocfg = dw_params >> DW_PARAMS_EN & 0x1;
1493
9cade1a4 1494 dev_dbg(chip->dev, "DW_PARAMS: 0x%08x\n", dw_params);
123de543
AS
1495
1496 if (!pdata && autocfg) {
9cade1a4 1497 pdata = devm_kzalloc(chip->dev, sizeof(*pdata), GFP_KERNEL);
123de543
AS
1498 if (!pdata)
1499 return -ENOMEM;
1500
1501 /* Fill platform data with the default values */
1502 pdata->is_private = true;
1503 pdata->chan_allocation_order = CHAN_ALLOCATION_ASCENDING;
1504 pdata->chan_priority = CHAN_PRIORITY_ASCENDING;
1505 } else if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
1506 return -EINVAL;
1507
482c67ea
AS
1508 if (autocfg)
1509 nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 0x7) + 1;
1510 else
1511 nr_channels = pdata->nr_channels;
1512
1513 size = sizeof(struct dw_dma) + nr_channels * sizeof(struct dw_dma_chan);
9cade1a4 1514 dw = devm_kzalloc(chip->dev, size, GFP_KERNEL);
3bfb1d20
HS
1515 if (!dw)
1516 return -ENOMEM;
1517
9cade1a4 1518 dw->clk = devm_clk_get(chip->dev, "hclk");
dbde5c29
AS
1519 if (IS_ERR(dw->clk))
1520 return PTR_ERR(dw->clk);
3075528d 1521 clk_prepare_enable(dw->clk);
3bfb1d20 1522
9cade1a4
AS
1523 dw->regs = chip->regs;
1524 chip->dw = dw;
482c67ea 1525
75c61225 1526 /* Get hardware configuration parameters */
a0982004 1527 if (autocfg) {
4a63a8b3
AS
1528 max_blk_size = dma_readl(dw, MAX_BLK_SIZE);
1529
a0982004
AS
1530 dw->nr_masters = (dw_params >> DW_PARAMS_NR_MASTER & 3) + 1;
1531 for (i = 0; i < dw->nr_masters; i++) {
1532 dw->data_width[i] =
1533 (dw_params >> DW_PARAMS_DATA_WIDTH(i) & 3) + 2;
1534 }
1535 } else {
1536 dw->nr_masters = pdata->nr_masters;
1537 memcpy(dw->data_width, pdata->data_width, 4);
1538 }
1539
11f932ec 1540 /* Calculate all channel mask before DMA setup */
482c67ea 1541 dw->all_chan_mask = (1 << nr_channels) - 1;
11f932ec 1542
75c61225 1543 /* Force dma off, just in case */
3bfb1d20
HS
1544 dw_dma_off(dw);
1545
75c61225 1546 /* Disable BLOCK interrupts as well */
236b106f
AS
1547 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
1548
3783cef8
AS
1549 err = devm_request_irq(chip->dev, chip->irq, dw_dma_interrupt,
1550 IRQF_SHARED, "dw_dmac", dw);
3bfb1d20 1551 if (err)
dbde5c29 1552 return err;
3bfb1d20 1553
75c61225 1554 /* Create a pool of consistent memory blocks for hardware descriptors */
9cade1a4 1555 dw->desc_pool = dmam_pool_create("dw_dmac_desc_pool", chip->dev,
f8122a82
AS
1556 sizeof(struct dw_desc), 4, 0);
1557 if (!dw->desc_pool) {
9cade1a4 1558 dev_err(chip->dev, "No memory for descriptors dma pool\n");
f8122a82
AS
1559 return -ENOMEM;
1560 }
1561
3bfb1d20
HS
1562 tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
1563
3bfb1d20 1564 INIT_LIST_HEAD(&dw->dma.channels);
482c67ea 1565 for (i = 0; i < nr_channels; i++) {
3bfb1d20 1566 struct dw_dma_chan *dwc = &dw->chan[i];
fed2574b 1567 int r = nr_channels - i - 1;
3bfb1d20
HS
1568
1569 dwc->chan.device = &dw->dma;
d3ee98cd 1570 dma_cookie_init(&dwc->chan);
b0c3130d
VK
1571 if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
1572 list_add_tail(&dwc->chan.device_node,
1573 &dw->dma.channels);
1574 else
1575 list_add(&dwc->chan.device_node, &dw->dma.channels);
3bfb1d20 1576
93317e8e
VK
1577 /* 7 is highest priority & 0 is lowest. */
1578 if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
fed2574b 1579 dwc->priority = r;
93317e8e
VK
1580 else
1581 dwc->priority = i;
1582
3bfb1d20
HS
1583 dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
1584 spin_lock_init(&dwc->lock);
1585 dwc->mask = 1 << i;
1586
1587 INIT_LIST_HEAD(&dwc->active_list);
1588 INIT_LIST_HEAD(&dwc->queue);
1589 INIT_LIST_HEAD(&dwc->free_list);
1590
1591 channel_clear_bit(dw, CH_EN, dwc->mask);
4a63a8b3 1592
0fdb567f 1593 dwc->direction = DMA_TRANS_NONE;
f776076b 1594 dwc->request_line = ~0;
a0982004 1595
75c61225 1596 /* Hardware configuration */
fed2574b
AS
1597 if (autocfg) {
1598 unsigned int dwc_params;
9cade1a4 1599 void __iomem *addr = chip->regs + r * sizeof(u32);
fed2574b 1600
9cade1a4 1601 dwc_params = dma_read_byaddr(addr, DWC_PARAMS);
fed2574b 1602
9cade1a4
AS
1603 dev_dbg(chip->dev, "DWC_PARAMS[%d]: 0x%08x\n", i,
1604 dwc_params);
985a6c7d 1605
4a63a8b3
AS
1606 /* Decode maximum block size for given channel. The
1607 * stored 4 bit value represents blocks from 0x00 for 3
1608 * up to 0x0a for 4095. */
1609 dwc->block_size =
1610 (4 << ((max_blk_size >> 4 * i) & 0xf)) - 1;
fed2574b
AS
1611 dwc->nollp =
1612 (dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0;
1613 } else {
4a63a8b3 1614 dwc->block_size = pdata->block_size;
fed2574b
AS
1615
1616 /* Check if channel supports multi block transfer */
1617 channel_writel(dwc, LLP, 0xfffffffc);
1618 dwc->nollp =
1619 (channel_readl(dwc, LLP) & 0xfffffffc) == 0;
1620 channel_writel(dwc, LLP, 0);
1621 }
3bfb1d20
HS
1622 }
1623
11f932ec 1624 /* Clear all interrupts on all channels. */
3bfb1d20 1625 dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
236b106f 1626 dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
3bfb1d20
HS
1627 dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
1628 dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
1629 dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
1630
3bfb1d20
HS
1631 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
1632 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
95ea759e
JI
1633 if (pdata->is_private)
1634 dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
9cade1a4 1635 dw->dma.dev = chip->dev;
3bfb1d20
HS
1636 dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
1637 dw->dma.device_free_chan_resources = dwc_free_chan_resources;
1638
1639 dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
1640
1641 dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
c3635c78 1642 dw->dma.device_control = dwc_control;
3bfb1d20 1643
07934481 1644 dw->dma.device_tx_status = dwc_tx_status;
3bfb1d20
HS
1645 dw->dma.device_issue_pending = dwc_issue_pending;
1646
1647 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1648
9cade1a4 1649 dev_info(chip->dev, "DesignWare DMA Controller, %d channels\n",
21d43f49 1650 nr_channels);
3bfb1d20
HS
1651
1652 dma_async_device_register(&dw->dma);
1653
1654 return 0;
3bfb1d20 1655}
9cade1a4 1656EXPORT_SYMBOL_GPL(dw_dma_probe);
3bfb1d20 1657
9cade1a4 1658int dw_dma_remove(struct dw_dma_chip *chip)
3bfb1d20 1659{
9cade1a4 1660 struct dw_dma *dw = chip->dw;
3bfb1d20 1661 struct dw_dma_chan *dwc, *_dwc;
3bfb1d20
HS
1662
1663 dw_dma_off(dw);
1664 dma_async_device_unregister(&dw->dma);
1665
3bfb1d20
HS
1666 tasklet_kill(&dw->tasklet);
1667
1668 list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
1669 chan.device_node) {
1670 list_del(&dwc->chan.device_node);
1671 channel_clear_bit(dw, CH_EN, dwc->mask);
1672 }
1673
3bfb1d20
HS
1674 return 0;
1675}
9cade1a4 1676EXPORT_SYMBOL_GPL(dw_dma_remove);
3bfb1d20 1677
9cade1a4 1678void dw_dma_shutdown(struct dw_dma_chip *chip)
3bfb1d20 1679{
9cade1a4 1680 struct dw_dma *dw = chip->dw;
3bfb1d20 1681
6168d567 1682 dw_dma_off(dw);
3075528d 1683 clk_disable_unprepare(dw->clk);
3bfb1d20 1684}
9cade1a4 1685EXPORT_SYMBOL_GPL(dw_dma_shutdown);
3bfb1d20 1686
9cade1a4
AS
1687#ifdef CONFIG_PM_SLEEP
1688
1689int dw_dma_suspend(struct dw_dma_chip *chip)
3bfb1d20 1690{
9cade1a4 1691 struct dw_dma *dw = chip->dw;
3bfb1d20 1692
6168d567 1693 dw_dma_off(dw);
3075528d 1694 clk_disable_unprepare(dw->clk);
61e183f8 1695
3bfb1d20
HS
1696 return 0;
1697}
9cade1a4 1698EXPORT_SYMBOL_GPL(dw_dma_suspend);
3bfb1d20 1699
9cade1a4 1700int dw_dma_resume(struct dw_dma_chip *chip)
3bfb1d20 1701{
9cade1a4 1702 struct dw_dma *dw = chip->dw;
3bfb1d20 1703
3075528d 1704 clk_prepare_enable(dw->clk);
3bfb1d20 1705 dma_writel(dw, CFG, DW_CFG_DMA_EN);
b801479b 1706
3bfb1d20 1707 return 0;
3bfb1d20 1708}
9cade1a4 1709EXPORT_SYMBOL_GPL(dw_dma_resume);
3bfb1d20 1710
9cade1a4 1711#endif /* CONFIG_PM_SLEEP */
3bfb1d20
HS
1712
1713MODULE_LICENSE("GPL v2");
9cade1a4 1714MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller core driver");
e05503ef 1715MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
10d8935f 1716MODULE_AUTHOR("Viresh Kumar <viresh.linux@gmail.com>");