dmaengine: dw: rename masters to reflect actual topology
[deliverable/linux.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 \
c422025c
AS
53 | DWC_CTLL_DMS(_dwc->p_master) \
54 | DWC_CTLL_SMS(_dwc->m_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);
61e183f8
VK
133 u32 cfghi = DWC_CFGH_FIFO_MODE;
134 u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority);
135
136 if (dwc->initialized == true)
137 return;
138
3fe6409c
AS
139 cfghi |= DWC_CFGH_DST_PER(dwc->dst_id);
140 cfghi |= DWC_CFGH_SRC_PER(dwc->src_id);
61e183f8
VK
141
142 channel_writel(dwc, CFG_LO, cfglo);
143 channel_writel(dwc, CFG_HI, cfghi);
144
145 /* Enable interrupts */
146 channel_set_bit(dw, MASK.XFER, dwc->mask);
61e183f8
VK
147 channel_set_bit(dw, MASK.ERROR, dwc->mask);
148
149 dwc->initialized = true;
150}
151
3bfb1d20
HS
152/*----------------------------------------------------------------------*/
153
39416677 154static inline unsigned int dwc_fast_ffs(unsigned long long v)
4c2d56c5
AS
155{
156 /*
157 * We can be a lot more clever here, but this should take care
158 * of the most common optimization.
159 */
160 if (!(v & 7))
161 return 3;
162 else if (!(v & 3))
163 return 2;
164 else if (!(v & 1))
165 return 1;
166 return 0;
167}
168
f52b36d2 169static inline void dwc_dump_chan_regs(struct dw_dma_chan *dwc)
1d455437
AS
170{
171 dev_err(chan2dev(&dwc->chan),
172 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
173 channel_readl(dwc, SAR),
174 channel_readl(dwc, DAR),
175 channel_readl(dwc, LLP),
176 channel_readl(dwc, CTL_HI),
177 channel_readl(dwc, CTL_LO));
178}
179
3f936207
AS
180static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc)
181{
182 channel_clear_bit(dw, CH_EN, dwc->mask);
183 while (dma_readl(dw, CH_EN) & dwc->mask)
184 cpu_relax();
185}
186
1d455437
AS
187/*----------------------------------------------------------------------*/
188
fed2574b
AS
189/* Perform single block transfer */
190static inline void dwc_do_single_block(struct dw_dma_chan *dwc,
191 struct dw_desc *desc)
192{
193 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
194 u32 ctllo;
195
1d566f11
AS
196 /*
197 * Software emulation of LLP mode relies on interrupts to continue
198 * multi block transfer.
199 */
fed2574b
AS
200 ctllo = desc->lli.ctllo | DWC_CTLL_INT_EN;
201
202 channel_writel(dwc, SAR, desc->lli.sar);
203 channel_writel(dwc, DAR, desc->lli.dar);
204 channel_writel(dwc, CTL_LO, ctllo);
205 channel_writel(dwc, CTL_HI, desc->lli.ctlhi);
206 channel_set_bit(dw, CH_EN, dwc->mask);
f5c6a7df
AS
207
208 /* Move pointer to next descriptor */
209 dwc->tx_node_active = dwc->tx_node_active->next;
fed2574b
AS
210}
211
3bfb1d20
HS
212/* Called with dwc->lock held and bh disabled */
213static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
214{
215 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
fed2574b 216 unsigned long was_soft_llp;
3bfb1d20
HS
217
218 /* ASSERT: channel is idle */
219 if (dma_readl(dw, CH_EN) & dwc->mask) {
41d5e59c 220 dev_err(chan2dev(&dwc->chan),
550da64b
JN
221 "%s: BUG: Attempted to start non-idle channel\n",
222 __func__);
1d455437 223 dwc_dump_chan_regs(dwc);
3bfb1d20
HS
224
225 /* The tasklet will hopefully advance the queue... */
226 return;
227 }
228
fed2574b
AS
229 if (dwc->nollp) {
230 was_soft_llp = test_and_set_bit(DW_DMA_IS_SOFT_LLP,
231 &dwc->flags);
232 if (was_soft_llp) {
233 dev_err(chan2dev(&dwc->chan),
fc61f6b4 234 "BUG: Attempted to start new LLP transfer inside ongoing one\n");
fed2574b
AS
235 return;
236 }
237
238 dwc_initialize(dwc);
239
4702d524 240 dwc->residue = first->total_len;
f5c6a7df 241 dwc->tx_node_active = &first->tx_list;
fed2574b 242
fdf475fa 243 /* Submit first block */
fed2574b
AS
244 dwc_do_single_block(dwc, first);
245
246 return;
247 }
248
61e183f8
VK
249 dwc_initialize(dwc);
250
3bfb1d20
HS
251 channel_writel(dwc, LLP, first->txd.phys);
252 channel_writel(dwc, CTL_LO,
253 DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
254 channel_writel(dwc, CTL_HI, 0);
255 channel_set_bit(dw, CH_EN, dwc->mask);
256}
257
e7637c6c
AS
258static void dwc_dostart_first_queued(struct dw_dma_chan *dwc)
259{
cba15617
AS
260 struct dw_desc *desc;
261
e7637c6c
AS
262 if (list_empty(&dwc->queue))
263 return;
264
265 list_move(dwc->queue.next, &dwc->active_list);
cba15617
AS
266 desc = dwc_first_active(dwc);
267 dev_vdbg(chan2dev(&dwc->chan), "%s: started %u\n", __func__, desc->txd.cookie);
268 dwc_dostart(dwc, desc);
e7637c6c
AS
269}
270
3bfb1d20
HS
271/*----------------------------------------------------------------------*/
272
273static void
5fedefb8
VK
274dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
275 bool callback_required)
3bfb1d20 276{
5fedefb8
VK
277 dma_async_tx_callback callback = NULL;
278 void *param = NULL;
3bfb1d20 279 struct dma_async_tx_descriptor *txd = &desc->txd;
e518076e 280 struct dw_desc *child;
69cea5a0 281 unsigned long flags;
3bfb1d20 282
41d5e59c 283 dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
3bfb1d20 284
69cea5a0 285 spin_lock_irqsave(&dwc->lock, flags);
f7fbce07 286 dma_cookie_complete(txd);
5fedefb8
VK
287 if (callback_required) {
288 callback = txd->callback;
289 param = txd->callback_param;
290 }
3bfb1d20 291
e518076e
VK
292 /* async_tx_ack */
293 list_for_each_entry(child, &desc->tx_list, desc_node)
294 async_tx_ack(&child->txd);
295 async_tx_ack(&desc->txd);
296
e0bd0f8c 297 list_splice_init(&desc->tx_list, &dwc->free_list);
3bfb1d20
HS
298 list_move(&desc->desc_node, &dwc->free_list);
299
d38a8c62 300 dma_descriptor_unmap(txd);
69cea5a0
VK
301 spin_unlock_irqrestore(&dwc->lock, flags);
302
21e93c1e 303 if (callback)
3bfb1d20
HS
304 callback(param);
305}
306
307static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
308{
309 struct dw_desc *desc, *_desc;
310 LIST_HEAD(list);
69cea5a0 311 unsigned long flags;
3bfb1d20 312
69cea5a0 313 spin_lock_irqsave(&dwc->lock, flags);
3bfb1d20 314 if (dma_readl(dw, CH_EN) & dwc->mask) {
41d5e59c 315 dev_err(chan2dev(&dwc->chan),
3bfb1d20
HS
316 "BUG: XFER bit set, but channel not idle!\n");
317
318 /* Try to continue after resetting the channel... */
3f936207 319 dwc_chan_disable(dw, dwc);
3bfb1d20
HS
320 }
321
322 /*
323 * Submit queued descriptors ASAP, i.e. before we go through
324 * the completed ones.
325 */
3bfb1d20 326 list_splice_init(&dwc->active_list, &list);
e7637c6c 327 dwc_dostart_first_queued(dwc);
3bfb1d20 328
69cea5a0
VK
329 spin_unlock_irqrestore(&dwc->lock, flags);
330
3bfb1d20 331 list_for_each_entry_safe(desc, _desc, &list, desc_node)
5fedefb8 332 dwc_descriptor_complete(dwc, desc, true);
3bfb1d20
HS
333}
334
4702d524
AS
335/* Returns how many bytes were already received from source */
336static inline u32 dwc_get_sent(struct dw_dma_chan *dwc)
337{
338 u32 ctlhi = channel_readl(dwc, CTL_HI);
339 u32 ctllo = channel_readl(dwc, CTL_LO);
340
341 return (ctlhi & DWC_CTLH_BLOCK_TS_MASK) * (1 << (ctllo >> 4 & 7));
342}
343
3bfb1d20
HS
344static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
345{
346 dma_addr_t llp;
347 struct dw_desc *desc, *_desc;
348 struct dw_desc *child;
349 u32 status_xfer;
69cea5a0 350 unsigned long flags;
3bfb1d20 351
69cea5a0 352 spin_lock_irqsave(&dwc->lock, flags);
3bfb1d20
HS
353 llp = channel_readl(dwc, LLP);
354 status_xfer = dma_readl(dw, RAW.XFER);
355
356 if (status_xfer & dwc->mask) {
357 /* Everything we've submitted is done */
358 dma_writel(dw, CLEAR.XFER, dwc->mask);
77bcc497
AS
359
360 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
fdf475fa
AS
361 struct list_head *head, *active = dwc->tx_node_active;
362
363 /*
364 * We are inside first active descriptor.
365 * Otherwise something is really wrong.
366 */
367 desc = dwc_first_active(dwc);
368
369 head = &desc->tx_list;
370 if (active != head) {
4702d524
AS
371 /* Update desc to reflect last sent one */
372 if (active != head->next)
373 desc = to_dw_desc(active->prev);
374
375 dwc->residue -= desc->len;
376
fdf475fa 377 child = to_dw_desc(active);
77bcc497
AS
378
379 /* Submit next block */
fdf475fa 380 dwc_do_single_block(dwc, child);
77bcc497 381
fdf475fa 382 spin_unlock_irqrestore(&dwc->lock, flags);
77bcc497
AS
383 return;
384 }
fdf475fa 385
77bcc497
AS
386 /* We are done here */
387 clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
388 }
4702d524
AS
389
390 dwc->residue = 0;
391
69cea5a0
VK
392 spin_unlock_irqrestore(&dwc->lock, flags);
393
3bfb1d20
HS
394 dwc_complete_all(dw, dwc);
395 return;
396 }
397
69cea5a0 398 if (list_empty(&dwc->active_list)) {
4702d524 399 dwc->residue = 0;
69cea5a0 400 spin_unlock_irqrestore(&dwc->lock, flags);
087809fc 401 return;
69cea5a0 402 }
087809fc 403
77bcc497
AS
404 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
405 dev_vdbg(chan2dev(&dwc->chan), "%s: soft LLP mode\n", __func__);
69cea5a0 406 spin_unlock_irqrestore(&dwc->lock, flags);
087809fc 407 return;
69cea5a0 408 }
087809fc 409
5a87f0e6 410 dev_vdbg(chan2dev(&dwc->chan), "%s: llp=%pad\n", __func__, &llp);
3bfb1d20
HS
411
412 list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
75c61225 413 /* Initial residue value */
4702d524
AS
414 dwc->residue = desc->total_len;
415
75c61225 416 /* Check first descriptors addr */
69cea5a0
VK
417 if (desc->txd.phys == llp) {
418 spin_unlock_irqrestore(&dwc->lock, flags);
84adccfb 419 return;
69cea5a0 420 }
84adccfb 421
75c61225 422 /* Check first descriptors llp */
69cea5a0 423 if (desc->lli.llp == llp) {
3bfb1d20 424 /* This one is currently in progress */
4702d524 425 dwc->residue -= dwc_get_sent(dwc);
69cea5a0 426 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20 427 return;
69cea5a0 428 }
3bfb1d20 429
4702d524
AS
430 dwc->residue -= desc->len;
431 list_for_each_entry(child, &desc->tx_list, desc_node) {
69cea5a0 432 if (child->lli.llp == llp) {
3bfb1d20 433 /* Currently in progress */
4702d524 434 dwc->residue -= dwc_get_sent(dwc);
69cea5a0 435 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20 436 return;
69cea5a0 437 }
4702d524
AS
438 dwc->residue -= child->len;
439 }
3bfb1d20
HS
440
441 /*
442 * No descriptors so far seem to be in progress, i.e.
443 * this one must be done.
444 */
69cea5a0 445 spin_unlock_irqrestore(&dwc->lock, flags);
5fedefb8 446 dwc_descriptor_complete(dwc, desc, true);
69cea5a0 447 spin_lock_irqsave(&dwc->lock, flags);
3bfb1d20
HS
448 }
449
41d5e59c 450 dev_err(chan2dev(&dwc->chan),
3bfb1d20
HS
451 "BUG: All descriptors done, but channel not idle!\n");
452
453 /* Try to continue after resetting the channel... */
3f936207 454 dwc_chan_disable(dw, dwc);
3bfb1d20 455
e7637c6c 456 dwc_dostart_first_queued(dwc);
69cea5a0 457 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20
HS
458}
459
93aad1bc 460static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
3bfb1d20 461{
21d43f49
AS
462 dev_crit(chan2dev(&dwc->chan), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
463 lli->sar, lli->dar, lli->llp, lli->ctlhi, lli->ctllo);
3bfb1d20
HS
464}
465
466static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
467{
468 struct dw_desc *bad_desc;
469 struct dw_desc *child;
69cea5a0 470 unsigned long flags;
3bfb1d20
HS
471
472 dwc_scan_descriptors(dw, dwc);
473
69cea5a0
VK
474 spin_lock_irqsave(&dwc->lock, flags);
475
3bfb1d20
HS
476 /*
477 * The descriptor currently at the head of the active list is
478 * borked. Since we don't have any way to report errors, we'll
479 * just have to scream loudly and try to carry on.
480 */
481 bad_desc = dwc_first_active(dwc);
482 list_del_init(&bad_desc->desc_node);
f336e42f 483 list_move(dwc->queue.next, dwc->active_list.prev);
3bfb1d20
HS
484
485 /* Clear the error flag and try to restart the controller */
486 dma_writel(dw, CLEAR.ERROR, dwc->mask);
487 if (!list_empty(&dwc->active_list))
488 dwc_dostart(dwc, dwc_first_active(dwc));
489
490 /*
ba84bd71 491 * WARN may seem harsh, but since this only happens
3bfb1d20
HS
492 * when someone submits a bad physical address in a
493 * descriptor, we should consider ourselves lucky that the
494 * controller flagged an error instead of scribbling over
495 * random memory locations.
496 */
ba84bd71
AS
497 dev_WARN(chan2dev(&dwc->chan), "Bad descriptor submitted for DMA!\n"
498 " cookie: %d\n", bad_desc->txd.cookie);
3bfb1d20 499 dwc_dump_lli(dwc, &bad_desc->lli);
e0bd0f8c 500 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
3bfb1d20
HS
501 dwc_dump_lli(dwc, &child->lli);
502
69cea5a0
VK
503 spin_unlock_irqrestore(&dwc->lock, flags);
504
3bfb1d20 505 /* Pretend the descriptor completed successfully */
5fedefb8 506 dwc_descriptor_complete(dwc, bad_desc, true);
3bfb1d20
HS
507}
508
d9de4519
HCE
509/* --------------------- Cyclic DMA API extensions -------------------- */
510
8004cbb4 511dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
d9de4519
HCE
512{
513 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
514 return channel_readl(dwc, SAR);
515}
516EXPORT_SYMBOL(dw_dma_get_src_addr);
517
8004cbb4 518dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
d9de4519
HCE
519{
520 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
521 return channel_readl(dwc, DAR);
522}
523EXPORT_SYMBOL(dw_dma_get_dst_addr);
524
75c61225 525/* Called with dwc->lock held and all DMAC interrupts disabled */
d9de4519 526static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
2895b2ca 527 u32 status_block, u32 status_err, u32 status_xfer)
d9de4519 528{
69cea5a0
VK
529 unsigned long flags;
530
2895b2ca 531 if (status_block & dwc->mask) {
d9de4519
HCE
532 void (*callback)(void *param);
533 void *callback_param;
534
535 dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
536 channel_readl(dwc, LLP));
2895b2ca 537 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
d9de4519
HCE
538
539 callback = dwc->cdesc->period_callback;
540 callback_param = dwc->cdesc->period_callback_param;
69cea5a0
VK
541
542 if (callback)
d9de4519 543 callback(callback_param);
d9de4519
HCE
544 }
545
546 /*
547 * Error and transfer complete are highly unlikely, and will most
548 * likely be due to a configuration error by the user.
549 */
550 if (unlikely(status_err & dwc->mask) ||
551 unlikely(status_xfer & dwc->mask)) {
552 int i;
553
fc61f6b4
AS
554 dev_err(chan2dev(&dwc->chan),
555 "cyclic DMA unexpected %s interrupt, stopping DMA transfer\n",
556 status_xfer ? "xfer" : "error");
69cea5a0
VK
557
558 spin_lock_irqsave(&dwc->lock, flags);
559
1d455437 560 dwc_dump_chan_regs(dwc);
d9de4519 561
3f936207 562 dwc_chan_disable(dw, dwc);
d9de4519 563
75c61225 564 /* Make sure DMA does not restart by loading a new list */
d9de4519
HCE
565 channel_writel(dwc, LLP, 0);
566 channel_writel(dwc, CTL_LO, 0);
567 channel_writel(dwc, CTL_HI, 0);
568
2895b2ca 569 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
d9de4519
HCE
570 dma_writel(dw, CLEAR.ERROR, dwc->mask);
571 dma_writel(dw, CLEAR.XFER, dwc->mask);
572
573 for (i = 0; i < dwc->cdesc->periods; i++)
574 dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
69cea5a0
VK
575
576 spin_unlock_irqrestore(&dwc->lock, flags);
d9de4519 577 }
ee1cdcda
AS
578
579 /* Re-enable interrupts */
580 channel_set_bit(dw, MASK.BLOCK, dwc->mask);
d9de4519
HCE
581}
582
583/* ------------------------------------------------------------------------- */
584
3bfb1d20
HS
585static void dw_dma_tasklet(unsigned long data)
586{
587 struct dw_dma *dw = (struct dw_dma *)data;
588 struct dw_dma_chan *dwc;
2895b2ca 589 u32 status_block;
3bfb1d20
HS
590 u32 status_xfer;
591 u32 status_err;
592 int i;
593
2895b2ca 594 status_block = dma_readl(dw, RAW.BLOCK);
7fe7b2f4 595 status_xfer = dma_readl(dw, RAW.XFER);
3bfb1d20
HS
596 status_err = dma_readl(dw, RAW.ERROR);
597
2e4c364e 598 dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err);
3bfb1d20
HS
599
600 for (i = 0; i < dw->dma.chancnt; i++) {
601 dwc = &dw->chan[i];
d9de4519 602 if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
2895b2ca
MR
603 dwc_handle_cyclic(dw, dwc, status_block, status_err,
604 status_xfer);
d9de4519 605 else if (status_err & (1 << i))
3bfb1d20 606 dwc_handle_error(dw, dwc);
77bcc497 607 else if (status_xfer & (1 << i))
3bfb1d20 608 dwc_scan_descriptors(dw, dwc);
3bfb1d20
HS
609 }
610
ee1cdcda 611 /* Re-enable interrupts */
3bfb1d20 612 channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
3bfb1d20
HS
613 channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
614}
615
616static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
617{
618 struct dw_dma *dw = dev_id;
02a21b79 619 u32 status;
3bfb1d20 620
02a21b79
AS
621 /* Check if we have any interrupt from the DMAC which is not in use */
622 if (!dw->in_use)
623 return IRQ_NONE;
624
625 status = dma_readl(dw, STATUS_INT);
3783cef8
AS
626 dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__, status);
627
628 /* Check if we have any interrupt from the DMAC */
02a21b79 629 if (!status)
3783cef8 630 return IRQ_NONE;
3bfb1d20
HS
631
632 /*
633 * Just disable the interrupts. We'll turn them back on in the
634 * softirq handler.
635 */
636 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
2895b2ca 637 channel_clear_bit(dw, MASK.BLOCK, 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);
2895b2ca 648 channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
3bfb1d20
HS
649 channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
650 channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
651 channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
652 }
653
654 tasklet_schedule(&dw->tasklet);
655
656 return IRQ_HANDLED;
657}
658
659/*----------------------------------------------------------------------*/
660
661static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
662{
663 struct dw_desc *desc = txd_to_dw_desc(tx);
664 struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
665 dma_cookie_t cookie;
69cea5a0 666 unsigned long flags;
3bfb1d20 667
69cea5a0 668 spin_lock_irqsave(&dwc->lock, flags);
884485e1 669 cookie = dma_cookie_assign(tx);
3bfb1d20
HS
670
671 /*
672 * REVISIT: We should attempt to chain as many descriptors as
673 * possible, perhaps even appending to those already submitted
674 * for DMA. But this is hard to do in a race-free manner.
675 */
3bfb1d20 676
dd8ecfca
AS
677 dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n", __func__, desc->txd.cookie);
678 list_add_tail(&desc->desc_node, &dwc->queue);
3bfb1d20 679
69cea5a0 680 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20
HS
681
682 return cookie;
683}
684
685static struct dma_async_tx_descriptor *
686dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
687 size_t len, unsigned long flags)
688{
689 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
f776076b 690 struct dw_dma *dw = to_dw_dma(chan->device);
3bfb1d20
HS
691 struct dw_desc *desc;
692 struct dw_desc *first;
693 struct dw_desc *prev;
694 size_t xfer_count;
695 size_t offset;
696 unsigned int src_width;
697 unsigned int dst_width;
3d4f8605 698 unsigned int data_width;
3bfb1d20
HS
699 u32 ctllo;
700
2f45d613 701 dev_vdbg(chan2dev(chan),
5a87f0e6
AS
702 "%s: d%pad s%pad l0x%zx f0x%lx\n", __func__,
703 &dest, &src, len, flags);
3bfb1d20
HS
704
705 if (unlikely(!len)) {
2e4c364e 706 dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
3bfb1d20
HS
707 return NULL;
708 }
709
0fdb567f
AS
710 dwc->direction = DMA_MEM_TO_MEM;
711
c422025c 712 data_width = dw->data_width[dwc->m_master];
a0982004 713
3d4f8605 714 src_width = dst_width = min_t(unsigned int, data_width,
39416677 715 dwc_fast_ffs(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:
39416677 794 reg_width = __ffs(sconfig->dst_addr_width);
327e6970
VK
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
c422025c 804 data_width = dw->data_width[dwc->m_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 813 mem_width = min_t(unsigned int,
39416677 814 data_width, dwc_fast_ffs(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:
39416677 851 reg_width = __ffs(sconfig->src_addr_width);
327e6970
VK
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
c422025c 861 data_width = dw->data_width[dwc->m_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 870 mem_width = min_t(unsigned int,
39416677 871 data_width, dwc_fast_ffs(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
3fe6409c 931 if (dws->dma_dev != chan->device->dev)
4d130de2
AS
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
c422025c
AS
939 dwc->m_master = dws->m_master;
940 dwc->p_master = dws->p_master;
4d130de2
AS
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);
2895b2ca 1114 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
99d9bf4e
AS
1115 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1116 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1117 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1118
1119 while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
1120 cpu_relax();
1121
1122 for (i = 0; i < dw->dma.chancnt; i++)
1123 dw->chan[i].initialized = false;
1124}
1125
1126static void dw_dma_on(struct dw_dma *dw)
1127{
1128 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1129}
1130
aa1e6f1a 1131static int dwc_alloc_chan_resources(struct dma_chan *chan)
3bfb1d20
HS
1132{
1133 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1134 struct dw_dma *dw = to_dw_dma(chan->device);
1135 struct dw_desc *desc;
3bfb1d20 1136 int i;
69cea5a0 1137 unsigned long flags;
3bfb1d20 1138
2e4c364e 1139 dev_vdbg(chan2dev(chan), "%s\n", __func__);
3bfb1d20 1140
3bfb1d20
HS
1141 /* ASSERT: channel is idle */
1142 if (dma_readl(dw, CH_EN) & dwc->mask) {
41d5e59c 1143 dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
3bfb1d20
HS
1144 return -EIO;
1145 }
1146
d3ee98cd 1147 dma_cookie_init(chan);
3bfb1d20 1148
3bfb1d20
HS
1149 /*
1150 * NOTE: some controllers may have additional features that we
1151 * need to initialize here, like "scatter-gather" (which
1152 * doesn't mean what you think it means), and status writeback.
1153 */
1154
3fe6409c
AS
1155 /*
1156 * We need controller-specific data to set up slave transfers.
1157 */
1158 if (chan->private && !dw_dma_filter(chan, chan->private)) {
1159 dev_warn(chan2dev(chan), "Wrong controller-specific data\n");
1160 return -EINVAL;
1161 }
1162
99d9bf4e
AS
1163 /* Enable controller here if needed */
1164 if (!dw->in_use)
1165 dw_dma_on(dw);
1166 dw->in_use |= dwc->mask;
1167
69cea5a0 1168 spin_lock_irqsave(&dwc->lock, flags);
3bfb1d20
HS
1169 i = dwc->descs_allocated;
1170 while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
f8122a82
AS
1171 dma_addr_t phys;
1172
69cea5a0 1173 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20 1174
f8122a82 1175 desc = dma_pool_alloc(dw->desc_pool, GFP_ATOMIC, &phys);
cbd65312
AS
1176 if (!desc)
1177 goto err_desc_alloc;
3bfb1d20 1178
f8122a82 1179 memset(desc, 0, sizeof(struct dw_desc));
3bfb1d20 1180
e0bd0f8c 1181 INIT_LIST_HEAD(&desc->tx_list);
3bfb1d20
HS
1182 dma_async_tx_descriptor_init(&desc->txd, chan);
1183 desc->txd.tx_submit = dwc_tx_submit;
1184 desc->txd.flags = DMA_CTRL_ACK;
f8122a82 1185 desc->txd.phys = phys;
cbd65312 1186
3bfb1d20
HS
1187 dwc_desc_put(dwc, desc);
1188
69cea5a0 1189 spin_lock_irqsave(&dwc->lock, flags);
3bfb1d20
HS
1190 i = ++dwc->descs_allocated;
1191 }
1192
69cea5a0 1193 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20 1194
2e4c364e 1195 dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
3bfb1d20 1196
cbd65312
AS
1197 return i;
1198
1199err_desc_alloc:
cbd65312
AS
1200 dev_info(chan2dev(chan), "only allocated %d descriptors\n", i);
1201
3bfb1d20
HS
1202 return i;
1203}
1204
1205static void dwc_free_chan_resources(struct dma_chan *chan)
1206{
1207 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1208 struct dw_dma *dw = to_dw_dma(chan->device);
1209 struct dw_desc *desc, *_desc;
69cea5a0 1210 unsigned long flags;
3bfb1d20
HS
1211 LIST_HEAD(list);
1212
2e4c364e 1213 dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
3bfb1d20
HS
1214 dwc->descs_allocated);
1215
1216 /* ASSERT: channel is idle */
1217 BUG_ON(!list_empty(&dwc->active_list));
1218 BUG_ON(!list_empty(&dwc->queue));
1219 BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
1220
69cea5a0 1221 spin_lock_irqsave(&dwc->lock, flags);
3bfb1d20
HS
1222 list_splice_init(&dwc->free_list, &list);
1223 dwc->descs_allocated = 0;
3fe6409c
AS
1224
1225 /* Clear custom channel configuration */
1226 dwc->src_id = 0;
1227 dwc->dst_id = 0;
1228
c422025c
AS
1229 dwc->m_master = 0;
1230 dwc->p_master = 0;
3fe6409c 1231
61e183f8 1232 dwc->initialized = false;
3bfb1d20
HS
1233
1234 /* Disable interrupts */
1235 channel_clear_bit(dw, MASK.XFER, dwc->mask);
2895b2ca 1236 channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
3bfb1d20
HS
1237 channel_clear_bit(dw, MASK.ERROR, dwc->mask);
1238
69cea5a0 1239 spin_unlock_irqrestore(&dwc->lock, flags);
3bfb1d20 1240
99d9bf4e
AS
1241 /* Disable controller in case it was a last user */
1242 dw->in_use &= ~dwc->mask;
1243 if (!dw->in_use)
1244 dw_dma_off(dw);
1245
3bfb1d20 1246 list_for_each_entry_safe(desc, _desc, &list, desc_node) {
41d5e59c 1247 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
f8122a82 1248 dma_pool_free(dw->desc_pool, desc, desc->txd.phys);
3bfb1d20
HS
1249 }
1250
2e4c364e 1251 dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
3bfb1d20
HS
1252}
1253
d9de4519
HCE
1254/* --------------------- Cyclic DMA API extensions -------------------- */
1255
1256/**
1257 * dw_dma_cyclic_start - start the cyclic DMA transfer
1258 * @chan: the DMA channel to start
1259 *
1260 * Must be called with soft interrupts disabled. Returns zero on success or
1261 * -errno on failure.
1262 */
1263int dw_dma_cyclic_start(struct dma_chan *chan)
1264{
1265 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
ee1cdcda 1266 struct dw_dma *dw = to_dw_dma(chan->device);
69cea5a0 1267 unsigned long flags;
d9de4519
HCE
1268
1269 if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
1270 dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
1271 return -ENODEV;
1272 }
1273
69cea5a0 1274 spin_lock_irqsave(&dwc->lock, flags);
ee1cdcda
AS
1275
1276 /* Enable interrupts to perform cyclic transfer */
1277 channel_set_bit(dw, MASK.BLOCK, dwc->mask);
1278
df3bb8a0 1279 dwc_dostart(dwc, dwc->cdesc->desc[0]);
ee1cdcda 1280
69cea5a0 1281 spin_unlock_irqrestore(&dwc->lock, flags);
d9de4519
HCE
1282
1283 return 0;
1284}
1285EXPORT_SYMBOL(dw_dma_cyclic_start);
1286
1287/**
1288 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1289 * @chan: the DMA channel to stop
1290 *
1291 * Must be called with soft interrupts disabled.
1292 */
1293void dw_dma_cyclic_stop(struct dma_chan *chan)
1294{
1295 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1296 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
69cea5a0 1297 unsigned long flags;
d9de4519 1298
69cea5a0 1299 spin_lock_irqsave(&dwc->lock, flags);
d9de4519 1300
3f936207 1301 dwc_chan_disable(dw, dwc);
d9de4519 1302
69cea5a0 1303 spin_unlock_irqrestore(&dwc->lock, flags);
d9de4519
HCE
1304}
1305EXPORT_SYMBOL(dw_dma_cyclic_stop);
1306
1307/**
1308 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1309 * @chan: the DMA channel to prepare
1310 * @buf_addr: physical DMA address where the buffer starts
1311 * @buf_len: total number of bytes for the entire buffer
1312 * @period_len: number of bytes for each period
1313 * @direction: transfer direction, to or from device
1314 *
1315 * Must be called before trying to start the transfer. Returns a valid struct
1316 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1317 */
1318struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
1319 dma_addr_t buf_addr, size_t buf_len, size_t period_len,
db8196df 1320 enum dma_transfer_direction direction)
d9de4519
HCE
1321{
1322 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
327e6970 1323 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
d9de4519
HCE
1324 struct dw_cyclic_desc *cdesc;
1325 struct dw_cyclic_desc *retval = NULL;
1326 struct dw_desc *desc;
1327 struct dw_desc *last = NULL;
d9de4519
HCE
1328 unsigned long was_cyclic;
1329 unsigned int reg_width;
1330 unsigned int periods;
1331 unsigned int i;
69cea5a0 1332 unsigned long flags;
d9de4519 1333
69cea5a0 1334 spin_lock_irqsave(&dwc->lock, flags);
fed2574b
AS
1335 if (dwc->nollp) {
1336 spin_unlock_irqrestore(&dwc->lock, flags);
1337 dev_dbg(chan2dev(&dwc->chan),
1338 "channel doesn't support LLP transfers\n");
1339 return ERR_PTR(-EINVAL);
1340 }
1341
d9de4519 1342 if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
69cea5a0 1343 spin_unlock_irqrestore(&dwc->lock, flags);
d9de4519
HCE
1344 dev_dbg(chan2dev(&dwc->chan),
1345 "queue and/or active list are not empty\n");
1346 return ERR_PTR(-EBUSY);
1347 }
1348
1349 was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
69cea5a0 1350 spin_unlock_irqrestore(&dwc->lock, flags);
d9de4519
HCE
1351 if (was_cyclic) {
1352 dev_dbg(chan2dev(&dwc->chan),
1353 "channel already prepared for cyclic DMA\n");
1354 return ERR_PTR(-EBUSY);
1355 }
1356
1357 retval = ERR_PTR(-EINVAL);
327e6970 1358
f44b92f4
AS
1359 if (unlikely(!is_slave_direction(direction)))
1360 goto out_err;
1361
0fdb567f
AS
1362 dwc->direction = direction;
1363
327e6970
VK
1364 if (direction == DMA_MEM_TO_DEV)
1365 reg_width = __ffs(sconfig->dst_addr_width);
1366 else
1367 reg_width = __ffs(sconfig->src_addr_width);
1368
d9de4519
HCE
1369 periods = buf_len / period_len;
1370
1371 /* Check for too big/unaligned periods and unaligned DMA buffer. */
4a63a8b3 1372 if (period_len > (dwc->block_size << reg_width))
d9de4519
HCE
1373 goto out_err;
1374 if (unlikely(period_len & ((1 << reg_width) - 1)))
1375 goto out_err;
1376 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
1377 goto out_err;
d9de4519
HCE
1378
1379 retval = ERR_PTR(-ENOMEM);
1380
1381 if (periods > NR_DESCS_PER_CHANNEL)
1382 goto out_err;
1383
1384 cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
1385 if (!cdesc)
1386 goto out_err;
1387
1388 cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
1389 if (!cdesc->desc)
1390 goto out_err_alloc;
1391
1392 for (i = 0; i < periods; i++) {
1393 desc = dwc_desc_get(dwc);
1394 if (!desc)
1395 goto out_err_desc_get;
1396
1397 switch (direction) {
db8196df 1398 case DMA_MEM_TO_DEV:
327e6970 1399 desc->lli.dar = sconfig->dst_addr;
d9de4519 1400 desc->lli.sar = buf_addr + (period_len * i);
327e6970 1401 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
d9de4519
HCE
1402 | DWC_CTLL_DST_WIDTH(reg_width)
1403 | DWC_CTLL_SRC_WIDTH(reg_width)
1404 | DWC_CTLL_DST_FIX
1405 | DWC_CTLL_SRC_INC
d9de4519 1406 | DWC_CTLL_INT_EN);
327e6970
VK
1407
1408 desc->lli.ctllo |= sconfig->device_fc ?
1409 DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
1410 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
1411
d9de4519 1412 break;
db8196df 1413 case DMA_DEV_TO_MEM:
d9de4519 1414 desc->lli.dar = buf_addr + (period_len * i);
327e6970
VK
1415 desc->lli.sar = sconfig->src_addr;
1416 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
d9de4519
HCE
1417 | DWC_CTLL_SRC_WIDTH(reg_width)
1418 | DWC_CTLL_DST_WIDTH(reg_width)
1419 | DWC_CTLL_DST_INC
1420 | DWC_CTLL_SRC_FIX
d9de4519 1421 | DWC_CTLL_INT_EN);
327e6970
VK
1422
1423 desc->lli.ctllo |= sconfig->device_fc ?
1424 DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
1425 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
1426
d9de4519
HCE
1427 break;
1428 default:
1429 break;
1430 }
1431
1432 desc->lli.ctlhi = (period_len >> reg_width);
1433 cdesc->desc[i] = desc;
1434
f8122a82 1435 if (last)
d9de4519 1436 last->lli.llp = desc->txd.phys;
d9de4519
HCE
1437
1438 last = desc;
1439 }
1440
75c61225 1441 /* Let's make a cyclic list */
d9de4519 1442 last->lli.llp = cdesc->desc[0]->txd.phys;
d9de4519 1443
5a87f0e6
AS
1444 dev_dbg(chan2dev(&dwc->chan),
1445 "cyclic prepared buf %pad len %zu period %zu periods %d\n",
1446 &buf_addr, buf_len, period_len, periods);
d9de4519
HCE
1447
1448 cdesc->periods = periods;
1449 dwc->cdesc = cdesc;
1450
1451 return cdesc;
1452
1453out_err_desc_get:
1454 while (i--)
1455 dwc_desc_put(dwc, cdesc->desc[i]);
1456out_err_alloc:
1457 kfree(cdesc);
1458out_err:
1459 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1460 return (struct dw_cyclic_desc *)retval;
1461}
1462EXPORT_SYMBOL(dw_dma_cyclic_prep);
1463
1464/**
1465 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1466 * @chan: the DMA channel to free
1467 */
1468void dw_dma_cyclic_free(struct dma_chan *chan)
1469{
1470 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1471 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1472 struct dw_cyclic_desc *cdesc = dwc->cdesc;
1473 int i;
69cea5a0 1474 unsigned long flags;
d9de4519 1475
2e4c364e 1476 dev_dbg(chan2dev(&dwc->chan), "%s\n", __func__);
d9de4519
HCE
1477
1478 if (!cdesc)
1479 return;
1480
69cea5a0 1481 spin_lock_irqsave(&dwc->lock, flags);
d9de4519 1482
3f936207 1483 dwc_chan_disable(dw, dwc);
d9de4519 1484
2895b2ca 1485 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
d9de4519
HCE
1486 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1487 dma_writel(dw, CLEAR.XFER, dwc->mask);
1488
69cea5a0 1489 spin_unlock_irqrestore(&dwc->lock, flags);
d9de4519
HCE
1490
1491 for (i = 0; i < cdesc->periods; i++)
1492 dwc_desc_put(dwc, cdesc->desc[i]);
1493
1494 kfree(cdesc->desc);
1495 kfree(cdesc);
1496
1497 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1498}
1499EXPORT_SYMBOL(dw_dma_cyclic_free);
1500
3bfb1d20
HS
1501/*----------------------------------------------------------------------*/
1502
9cade1a4 1503int dw_dma_probe(struct dw_dma_chip *chip, struct dw_dma_platform_data *pdata)
a9ddb575 1504{
3bfb1d20 1505 struct dw_dma *dw;
30cb2639 1506 bool autocfg = false;
482c67ea 1507 unsigned int dw_params;
4a63a8b3 1508 unsigned int max_blk_size = 0;
3bfb1d20
HS
1509 int err;
1510 int i;
1511
000871ce
AS
1512 dw = devm_kzalloc(chip->dev, sizeof(*dw), GFP_KERNEL);
1513 if (!dw)
1514 return -ENOMEM;
1515
1516 dw->regs = chip->regs;
1517 chip->dw = dw;
1518
bb32baf7
AS
1519 pm_runtime_get_sync(chip->dev);
1520
30cb2639
AS
1521 if (!pdata) {
1522 dw_params = dma_read_byaddr(chip->regs, DW_PARAMS);
1523 dev_dbg(chip->dev, "DW_PARAMS: 0x%08x\n", dw_params);
482c67ea 1524
30cb2639
AS
1525 autocfg = dw_params >> DW_PARAMS_EN & 1;
1526 if (!autocfg) {
1527 err = -EINVAL;
1528 goto err_pdata;
1529 }
123de543 1530
9cade1a4 1531 pdata = devm_kzalloc(chip->dev, sizeof(*pdata), GFP_KERNEL);
8be4f523
AS
1532 if (!pdata) {
1533 err = -ENOMEM;
1534 goto err_pdata;
1535 }
123de543 1536
30cb2639
AS
1537 /* Get hardware configuration parameters */
1538 pdata->nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 7) + 1;
1539 pdata->nr_masters = (dw_params >> DW_PARAMS_NR_MASTER & 3) + 1;
1540 for (i = 0; i < pdata->nr_masters; i++) {
1541 pdata->data_width[i] =
1542 (dw_params >> DW_PARAMS_DATA_WIDTH(i) & 3) + 2;
1543 }
1544 max_blk_size = dma_readl(dw, MAX_BLK_SIZE);
1545
123de543
AS
1546 /* Fill platform data with the default values */
1547 pdata->is_private = true;
df5c7386 1548 pdata->is_memcpy = true;
123de543
AS
1549 pdata->chan_allocation_order = CHAN_ALLOCATION_ASCENDING;
1550 pdata->chan_priority = CHAN_PRIORITY_ASCENDING;
30cb2639 1551 } else if (pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS) {
8be4f523
AS
1552 err = -EINVAL;
1553 goto err_pdata;
1554 }
123de543 1555
30cb2639 1556 dw->chan = devm_kcalloc(chip->dev, pdata->nr_channels, sizeof(*dw->chan),
000871ce 1557 GFP_KERNEL);
8be4f523
AS
1558 if (!dw->chan) {
1559 err = -ENOMEM;
1560 goto err_pdata;
1561 }
3bfb1d20 1562
75c61225 1563 /* Get hardware configuration parameters */
30cb2639
AS
1564 dw->nr_masters = pdata->nr_masters;
1565 for (i = 0; i < dw->nr_masters; i++)
1566 dw->data_width[i] = pdata->data_width[i];
a0982004 1567
11f932ec 1568 /* Calculate all channel mask before DMA setup */
30cb2639 1569 dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
11f932ec 1570
75c61225 1571 /* Force dma off, just in case */
3bfb1d20
HS
1572 dw_dma_off(dw);
1573
75c61225 1574 /* Create a pool of consistent memory blocks for hardware descriptors */
9cade1a4 1575 dw->desc_pool = dmam_pool_create("dw_dmac_desc_pool", chip->dev,
f8122a82
AS
1576 sizeof(struct dw_desc), 4, 0);
1577 if (!dw->desc_pool) {
9cade1a4 1578 dev_err(chip->dev, "No memory for descriptors dma pool\n");
8be4f523
AS
1579 err = -ENOMEM;
1580 goto err_pdata;
f8122a82
AS
1581 }
1582
3bfb1d20
HS
1583 tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
1584
97977f75
AS
1585 err = request_irq(chip->irq, dw_dma_interrupt, IRQF_SHARED,
1586 "dw_dmac", dw);
1587 if (err)
8be4f523 1588 goto err_pdata;
97977f75 1589
3bfb1d20 1590 INIT_LIST_HEAD(&dw->dma.channels);
30cb2639 1591 for (i = 0; i < pdata->nr_channels; i++) {
3bfb1d20
HS
1592 struct dw_dma_chan *dwc = &dw->chan[i];
1593
1594 dwc->chan.device = &dw->dma;
d3ee98cd 1595 dma_cookie_init(&dwc->chan);
b0c3130d
VK
1596 if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
1597 list_add_tail(&dwc->chan.device_node,
1598 &dw->dma.channels);
1599 else
1600 list_add(&dwc->chan.device_node, &dw->dma.channels);
3bfb1d20 1601
93317e8e
VK
1602 /* 7 is highest priority & 0 is lowest. */
1603 if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
30cb2639 1604 dwc->priority = pdata->nr_channels - i - 1;
93317e8e
VK
1605 else
1606 dwc->priority = i;
1607
3bfb1d20
HS
1608 dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
1609 spin_lock_init(&dwc->lock);
1610 dwc->mask = 1 << i;
1611
1612 INIT_LIST_HEAD(&dwc->active_list);
1613 INIT_LIST_HEAD(&dwc->queue);
1614 INIT_LIST_HEAD(&dwc->free_list);
1615
1616 channel_clear_bit(dw, CH_EN, dwc->mask);
4a63a8b3 1617
0fdb567f 1618 dwc->direction = DMA_TRANS_NONE;
a0982004 1619
75c61225 1620 /* Hardware configuration */
fed2574b
AS
1621 if (autocfg) {
1622 unsigned int dwc_params;
6bea0f6d 1623 unsigned int r = DW_DMA_MAX_NR_CHANNELS - i - 1;
9cade1a4 1624 void __iomem *addr = chip->regs + r * sizeof(u32);
fed2574b 1625
9cade1a4 1626 dwc_params = dma_read_byaddr(addr, DWC_PARAMS);
fed2574b 1627
9cade1a4
AS
1628 dev_dbg(chip->dev, "DWC_PARAMS[%d]: 0x%08x\n", i,
1629 dwc_params);
985a6c7d 1630
1d566f11
AS
1631 /*
1632 * Decode maximum block size for given channel. The
4a63a8b3 1633 * stored 4 bit value represents blocks from 0x00 for 3
1d566f11
AS
1634 * up to 0x0a for 4095.
1635 */
4a63a8b3
AS
1636 dwc->block_size =
1637 (4 << ((max_blk_size >> 4 * i) & 0xf)) - 1;
fed2574b
AS
1638 dwc->nollp =
1639 (dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0;
1640 } else {
4a63a8b3 1641 dwc->block_size = pdata->block_size;
fed2574b
AS
1642
1643 /* Check if channel supports multi block transfer */
1644 channel_writel(dwc, LLP, 0xfffffffc);
1645 dwc->nollp =
1646 (channel_readl(dwc, LLP) & 0xfffffffc) == 0;
1647 channel_writel(dwc, LLP, 0);
1648 }
3bfb1d20
HS
1649 }
1650
11f932ec 1651 /* Clear all interrupts on all channels. */
3bfb1d20 1652 dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
236b106f 1653 dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
3bfb1d20
HS
1654 dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
1655 dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
1656 dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
1657
df5c7386 1658 /* Set capabilities */
3bfb1d20 1659 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
95ea759e
JI
1660 if (pdata->is_private)
1661 dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
df5c7386
AS
1662 if (pdata->is_memcpy)
1663 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
1664
9cade1a4 1665 dw->dma.dev = chip->dev;
3bfb1d20
HS
1666 dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
1667 dw->dma.device_free_chan_resources = dwc_free_chan_resources;
1668
1669 dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
3bfb1d20 1670 dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
029a40e9 1671
a4b0d348
MR
1672 dw->dma.device_config = dwc_config;
1673 dw->dma.device_pause = dwc_pause;
1674 dw->dma.device_resume = dwc_resume;
1675 dw->dma.device_terminate_all = dwc_terminate_all;
3bfb1d20 1676
07934481 1677 dw->dma.device_tx_status = dwc_tx_status;
3bfb1d20
HS
1678 dw->dma.device_issue_pending = dwc_issue_pending;
1679
029a40e9
AS
1680 /* DMA capabilities */
1681 dw->dma.src_addr_widths = DW_DMA_BUSWIDTHS;
1682 dw->dma.dst_addr_widths = DW_DMA_BUSWIDTHS;
1683 dw->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
1684 BIT(DMA_MEM_TO_MEM);
1685 dw->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1686
1222934e
AS
1687 err = dma_async_device_register(&dw->dma);
1688 if (err)
1689 goto err_dma_register;
1690
9cade1a4 1691 dev_info(chip->dev, "DesignWare DMA Controller, %d channels\n",
30cb2639 1692 pdata->nr_channels);
3bfb1d20 1693
bb32baf7
AS
1694 pm_runtime_put_sync_suspend(chip->dev);
1695
3bfb1d20 1696 return 0;
8be4f523 1697
1222934e
AS
1698err_dma_register:
1699 free_irq(chip->irq, dw);
8be4f523 1700err_pdata:
bb32baf7 1701 pm_runtime_put_sync_suspend(chip->dev);
8be4f523 1702 return err;
3bfb1d20 1703}
9cade1a4 1704EXPORT_SYMBOL_GPL(dw_dma_probe);
3bfb1d20 1705
9cade1a4 1706int dw_dma_remove(struct dw_dma_chip *chip)
3bfb1d20 1707{
9cade1a4 1708 struct dw_dma *dw = chip->dw;
3bfb1d20 1709 struct dw_dma_chan *dwc, *_dwc;
3bfb1d20 1710
bb32baf7
AS
1711 pm_runtime_get_sync(chip->dev);
1712
3bfb1d20
HS
1713 dw_dma_off(dw);
1714 dma_async_device_unregister(&dw->dma);
1715
97977f75 1716 free_irq(chip->irq, dw);
3bfb1d20
HS
1717 tasklet_kill(&dw->tasklet);
1718
1719 list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
1720 chan.device_node) {
1721 list_del(&dwc->chan.device_node);
1722 channel_clear_bit(dw, CH_EN, dwc->mask);
1723 }
1724
bb32baf7 1725 pm_runtime_put_sync_suspend(chip->dev);
3bfb1d20
HS
1726 return 0;
1727}
9cade1a4 1728EXPORT_SYMBOL_GPL(dw_dma_remove);
3bfb1d20 1729
2540f74b 1730int dw_dma_disable(struct dw_dma_chip *chip)
3bfb1d20 1731{
9cade1a4 1732 struct dw_dma *dw = chip->dw;
3bfb1d20 1733
6168d567 1734 dw_dma_off(dw);
3bfb1d20
HS
1735 return 0;
1736}
2540f74b 1737EXPORT_SYMBOL_GPL(dw_dma_disable);
3bfb1d20 1738
2540f74b 1739int dw_dma_enable(struct dw_dma_chip *chip)
3bfb1d20 1740{
9cade1a4 1741 struct dw_dma *dw = chip->dw;
3bfb1d20 1742
7a83c045 1743 dw_dma_on(dw);
3bfb1d20 1744 return 0;
3bfb1d20 1745}
2540f74b 1746EXPORT_SYMBOL_GPL(dw_dma_enable);
3bfb1d20
HS
1747
1748MODULE_LICENSE("GPL v2");
9cade1a4 1749MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller core driver");
e05503ef 1750MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
da89947b 1751MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>");
This page took 0.475929 seconds and 5 git commands to generate.