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