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