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