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