blob: ac4fc0995a3e24c6be93cdb605654545b4263316 [file] [log] [blame]
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001/*
Heikki Krogerusb8014792012-10-18 17:34:08 +03002 * Core driver for the Synopsys DesignWare DMA Controller
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07003 *
4 * Copyright (C) 2007-2008 Atmel Corporation
Viresh Kumaraecb7b62011-05-24 14:04:09 +05305 * Copyright (C) 2010-2011 ST Microelectronics
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03006 * Copyright (C) 2013 Intel Corporation
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07007 *
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 */
Heikki Krogerusb8014792012-10-18 17:34:08 +030012
Viresh Kumar327e6972012-02-01 16:12:26 +053013#include <linux/bitops.h>
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070014#include <linux/delay.h>
15#include <linux/dmaengine.h>
16#include <linux/dma-mapping.h>
Andy Shevchenkof8122a82013-01-16 15:48:50 +020017#include <linux/dmapool.h>
Thierry Reding73312052013-01-21 11:09:00 +010018#include <linux/err.h>
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070019#include <linux/init.h>
20#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/mm.h>
23#include <linux/module.h>
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070024#include <linux/slab.h>
Andy Shevchenkobb32baf2014-11-05 18:34:48 +020025#include <linux/pm_runtime.h>
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070026
Andy Shevchenko61a76492013-06-05 15:26:44 +030027#include "../dmaengine.h"
Andy Shevchenko9cade1a2013-06-05 15:26:45 +030028#include "internal.h"
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070029
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 *
Andy Shevchenkodd5720b2014-02-12 11:16:17 +020036 * The driver has been tested with the Atmel AT32AP7000, which does not
37 * support descriptor writeback.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070038 */
39
Viresh Kumar327e6972012-02-01 16:12:26 +053040#define DWC_DEFAULT_CTLLO(_chan) ({ \
Viresh Kumar327e6972012-02-01 16:12:26 +053041 struct dw_dma_chan *_dwc = to_dw_dma_chan(_chan); \
42 struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \
Andy Shevchenko495aea42013-01-10 11:11:41 +020043 bool _is_slave = is_slave_direction(_dwc->direction); \
Andy Shevchenko495aea42013-01-10 11:11:41 +020044 u8 _smsize = _is_slave ? _sconfig->src_maxburst : \
Viresh Kumar327e6972012-02-01 16:12:26 +053045 DW_DMA_MSIZE_16; \
Andy Shevchenko495aea42013-01-10 11:11:41 +020046 u8 _dmsize = _is_slave ? _sconfig->dst_maxburst : \
Viresh Kumar327e6972012-02-01 16:12:26 +053047 DW_DMA_MSIZE_16; \
Jamie Ilesf301c062011-01-21 14:11:53 +000048 \
Viresh Kumar327e6972012-02-01 16:12:26 +053049 (DWC_CTLL_DST_MSIZE(_dmsize) \
50 | DWC_CTLL_SRC_MSIZE(_smsize) \
Jamie Ilesf301c062011-01-21 14:11:53 +000051 | DWC_CTLL_LLP_D_EN \
52 | DWC_CTLL_LLP_S_EN \
Arnd Bergmannf7760762013-03-26 16:53:57 +020053 | DWC_CTLL_DMS(_dwc->dst_master) \
54 | DWC_CTLL_SMS(_dwc->src_master)); \
Jamie Ilesf301c062011-01-21 14:11:53 +000055 })
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070056
57/*
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070058 * 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
Andy Shevchenko029a40e2015-01-02 16:17:24 +020064/* 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
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070071/*----------------------------------------------------------------------*/
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070072
Dan Williams41d5e592009-01-06 11:38:21 -070073static struct device *chan2dev(struct dma_chan *chan)
74{
75 return &chan->dev->device;
76}
Dan Williams41d5e592009-01-06 11:38:21 -070077
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070078static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
79{
Andy Shevchenkoe63a47a2012-10-18 17:34:12 +030080 return to_dw_desc(dwc->active_list.next);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070081}
82
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070083static 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;
Viresh Kumar69cea5a2011-04-15 16:03:35 +053088 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070089
Viresh Kumar69cea5a2011-04-15 16:03:35 +053090 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070091 list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
Andy Shevchenko2ab37272012-06-19 13:34:04 +030092 i++;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070093 if (async_tx_test_ack(&desc->txd)) {
94 list_del(&desc->desc_node);
95 ret = desc;
96 break;
97 }
Dan Williams41d5e592009-01-06 11:38:21 -070098 dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070099 }
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530100 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700101
Dan Williams41d5e592009-01-06 11:38:21 -0700102 dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700103
104 return ret;
105}
106
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700107/*
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{
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530113 unsigned long flags;
114
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700115 if (desc) {
116 struct dw_desc *child;
117
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530118 spin_lock_irqsave(&dwc->lock, flags);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700119 list_for_each_entry(child, &desc->tx_list, desc_node)
Dan Williams41d5e592009-01-06 11:38:21 -0700120 dev_vdbg(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700121 "moving child desc %p to freelist\n",
122 child);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700123 list_splice_init(&desc->tx_list, &dwc->free_list);
Dan Williams41d5e592009-01-06 11:38:21 -0700124 dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700125 list_add(&desc->desc_node, &dwc->free_list);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530126 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700127 }
128}
129
Viresh Kumar61e183f2011-11-17 16:01:29 +0530130static void dwc_initialize(struct dw_dma_chan *dwc)
131{
132 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
133 struct dw_dma_slave *dws = dwc->chan.private;
134 u32 cfghi = DWC_CFGH_FIFO_MODE;
135 u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority);
136
137 if (dwc->initialized == true)
138 return;
139
Arnd Bergmannf7760762013-03-26 16:53:57 +0200140 if (dws) {
Viresh Kumar61e183f2011-11-17 16:01:29 +0530141 /*
142 * We need controller-specific data to set up slave
143 * transfers.
144 */
145 BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
146
Andy Shevchenko7e1e2f22014-08-19 20:29:14 +0300147 cfghi |= DWC_CFGH_DST_PER(dws->dst_id);
148 cfghi |= DWC_CFGH_SRC_PER(dws->src_id);
Andy Shevchenko8fccc5b2012-09-03 13:46:19 +0300149 } else {
Andy Shevchenko89500522014-08-19 20:29:15 +0300150 cfghi |= DWC_CFGH_DST_PER(dwc->dst_id);
151 cfghi |= DWC_CFGH_SRC_PER(dwc->src_id);
Viresh Kumar61e183f2011-11-17 16:01:29 +0530152 }
153
154 channel_writel(dwc, CFG_LO, cfglo);
155 channel_writel(dwc, CFG_HI, cfghi);
156
157 /* Enable interrupts */
158 channel_set_bit(dw, MASK.XFER, dwc->mask);
Viresh Kumar61e183f2011-11-17 16:01:29 +0530159 channel_set_bit(dw, MASK.ERROR, dwc->mask);
160
161 dwc->initialized = true;
162}
163
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700164/*----------------------------------------------------------------------*/
165
Andy Shevchenko4c2d56c2012-06-19 13:34:08 +0300166static inline unsigned int dwc_fast_fls(unsigned long long v)
167{
168 /*
169 * We can be a lot more clever here, but this should take care
170 * of the most common optimization.
171 */
172 if (!(v & 7))
173 return 3;
174 else if (!(v & 3))
175 return 2;
176 else if (!(v & 1))
177 return 1;
178 return 0;
179}
180
Andy Shevchenkof52b36d2012-09-21 15:05:44 +0300181static inline void dwc_dump_chan_regs(struct dw_dma_chan *dwc)
Andy Shevchenko1d455432012-06-19 13:34:03 +0300182{
183 dev_err(chan2dev(&dwc->chan),
184 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
185 channel_readl(dwc, SAR),
186 channel_readl(dwc, DAR),
187 channel_readl(dwc, LLP),
188 channel_readl(dwc, CTL_HI),
189 channel_readl(dwc, CTL_LO));
190}
191
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300192static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc)
193{
194 channel_clear_bit(dw, CH_EN, dwc->mask);
195 while (dma_readl(dw, CH_EN) & dwc->mask)
196 cpu_relax();
197}
198
Andy Shevchenko1d455432012-06-19 13:34:03 +0300199/*----------------------------------------------------------------------*/
200
Andy Shevchenkofed25742012-09-21 15:05:49 +0300201/* Perform single block transfer */
202static inline void dwc_do_single_block(struct dw_dma_chan *dwc,
203 struct dw_desc *desc)
204{
205 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
206 u32 ctllo;
207
Andy Shevchenko1d566f12014-01-13 14:04:48 +0200208 /*
209 * Software emulation of LLP mode relies on interrupts to continue
210 * multi block transfer.
211 */
Andy Shevchenkofed25742012-09-21 15:05:49 +0300212 ctllo = desc->lli.ctllo | DWC_CTLL_INT_EN;
213
214 channel_writel(dwc, SAR, desc->lli.sar);
215 channel_writel(dwc, DAR, desc->lli.dar);
216 channel_writel(dwc, CTL_LO, ctllo);
217 channel_writel(dwc, CTL_HI, desc->lli.ctlhi);
218 channel_set_bit(dw, CH_EN, dwc->mask);
Andy Shevchenkof5c6a7d2013-01-09 10:17:13 +0200219
220 /* Move pointer to next descriptor */
221 dwc->tx_node_active = dwc->tx_node_active->next;
Andy Shevchenkofed25742012-09-21 15:05:49 +0300222}
223
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700224/* Called with dwc->lock held and bh disabled */
225static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
226{
227 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Andy Shevchenkofed25742012-09-21 15:05:49 +0300228 unsigned long was_soft_llp;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700229
230 /* ASSERT: channel is idle */
231 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700232 dev_err(chan2dev(&dwc->chan),
Jarkko Nikula550da642015-03-10 11:37:23 +0200233 "%s: BUG: Attempted to start non-idle channel\n",
234 __func__);
Andy Shevchenko1d455432012-06-19 13:34:03 +0300235 dwc_dump_chan_regs(dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700236
237 /* The tasklet will hopefully advance the queue... */
238 return;
239 }
240
Andy Shevchenkofed25742012-09-21 15:05:49 +0300241 if (dwc->nollp) {
242 was_soft_llp = test_and_set_bit(DW_DMA_IS_SOFT_LLP,
243 &dwc->flags);
244 if (was_soft_llp) {
245 dev_err(chan2dev(&dwc->chan),
Andy Shevchenkofc61f6b2014-01-13 14:04:49 +0200246 "BUG: Attempted to start new LLP transfer inside ongoing one\n");
Andy Shevchenkofed25742012-09-21 15:05:49 +0300247 return;
248 }
249
250 dwc_initialize(dwc);
251
Andy Shevchenko4702d522013-01-25 11:48:03 +0200252 dwc->residue = first->total_len;
Andy Shevchenkof5c6a7d2013-01-09 10:17:13 +0200253 dwc->tx_node_active = &first->tx_list;
Andy Shevchenkofed25742012-09-21 15:05:49 +0300254
Andy Shevchenkofdf475f2013-01-25 11:48:00 +0200255 /* Submit first block */
Andy Shevchenkofed25742012-09-21 15:05:49 +0300256 dwc_do_single_block(dwc, first);
257
258 return;
259 }
260
Viresh Kumar61e183f2011-11-17 16:01:29 +0530261 dwc_initialize(dwc);
262
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700263 channel_writel(dwc, LLP, first->txd.phys);
264 channel_writel(dwc, CTL_LO,
265 DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
266 channel_writel(dwc, CTL_HI, 0);
267 channel_set_bit(dw, CH_EN, dwc->mask);
268}
269
Andy Shevchenkoe7637c62014-06-18 12:15:36 +0300270static void dwc_dostart_first_queued(struct dw_dma_chan *dwc)
271{
Andy Shevchenkocba15612014-06-18 12:15:37 +0300272 struct dw_desc *desc;
273
Andy Shevchenkoe7637c62014-06-18 12:15:36 +0300274 if (list_empty(&dwc->queue))
275 return;
276
277 list_move(dwc->queue.next, &dwc->active_list);
Andy Shevchenkocba15612014-06-18 12:15:37 +0300278 desc = dwc_first_active(dwc);
279 dev_vdbg(chan2dev(&dwc->chan), "%s: started %u\n", __func__, desc->txd.cookie);
280 dwc_dostart(dwc, desc);
Andy Shevchenkoe7637c62014-06-18 12:15:36 +0300281}
282
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700283/*----------------------------------------------------------------------*/
284
285static void
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530286dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
287 bool callback_required)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700288{
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530289 dma_async_tx_callback callback = NULL;
290 void *param = NULL;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700291 struct dma_async_tx_descriptor *txd = &desc->txd;
Viresh Kumare5180762011-03-03 15:47:20 +0530292 struct dw_desc *child;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530293 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700294
Dan Williams41d5e592009-01-06 11:38:21 -0700295 dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700296
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530297 spin_lock_irqsave(&dwc->lock, flags);
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +0000298 dma_cookie_complete(txd);
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530299 if (callback_required) {
300 callback = txd->callback;
301 param = txd->callback_param;
302 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700303
Viresh Kumare5180762011-03-03 15:47:20 +0530304 /* async_tx_ack */
305 list_for_each_entry(child, &desc->tx_list, desc_node)
306 async_tx_ack(&child->txd);
307 async_tx_ack(&desc->txd);
308
Dan Williamse0bd0f82009-09-08 17:53:02 -0700309 list_splice_init(&desc->tx_list, &dwc->free_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700310 list_move(&desc->desc_node, &dwc->free_list);
311
Dan Williamsd38a8c62013-10-18 19:35:23 +0200312 dma_descriptor_unmap(txd);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530313 spin_unlock_irqrestore(&dwc->lock, flags);
314
Andy Shevchenko21e93c12013-01-09 10:17:12 +0200315 if (callback)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700316 callback(param);
317}
318
319static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
320{
321 struct dw_desc *desc, *_desc;
322 LIST_HEAD(list);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530323 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700324
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530325 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700326 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700327 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700328 "BUG: XFER bit set, but channel not idle!\n");
329
330 /* Try to continue after resetting the channel... */
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300331 dwc_chan_disable(dw, dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700332 }
333
334 /*
335 * Submit queued descriptors ASAP, i.e. before we go through
336 * the completed ones.
337 */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700338 list_splice_init(&dwc->active_list, &list);
Andy Shevchenkoe7637c62014-06-18 12:15:36 +0300339 dwc_dostart_first_queued(dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700340
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530341 spin_unlock_irqrestore(&dwc->lock, flags);
342
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700343 list_for_each_entry_safe(desc, _desc, &list, desc_node)
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530344 dwc_descriptor_complete(dwc, desc, true);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700345}
346
Andy Shevchenko4702d522013-01-25 11:48:03 +0200347/* Returns how many bytes were already received from source */
348static inline u32 dwc_get_sent(struct dw_dma_chan *dwc)
349{
350 u32 ctlhi = channel_readl(dwc, CTL_HI);
351 u32 ctllo = channel_readl(dwc, CTL_LO);
352
353 return (ctlhi & DWC_CTLH_BLOCK_TS_MASK) * (1 << (ctllo >> 4 & 7));
354}
355
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700356static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
357{
358 dma_addr_t llp;
359 struct dw_desc *desc, *_desc;
360 struct dw_desc *child;
361 u32 status_xfer;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530362 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700363
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530364 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700365 llp = channel_readl(dwc, LLP);
366 status_xfer = dma_readl(dw, RAW.XFER);
367
368 if (status_xfer & dwc->mask) {
369 /* Everything we've submitted is done */
370 dma_writel(dw, CLEAR.XFER, dwc->mask);
Andy Shevchenko77bcc4972013-01-18 14:14:15 +0200371
372 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
Andy Shevchenkofdf475f2013-01-25 11:48:00 +0200373 struct list_head *head, *active = dwc->tx_node_active;
374
375 /*
376 * We are inside first active descriptor.
377 * Otherwise something is really wrong.
378 */
379 desc = dwc_first_active(dwc);
380
381 head = &desc->tx_list;
382 if (active != head) {
Andy Shevchenko4702d522013-01-25 11:48:03 +0200383 /* Update desc to reflect last sent one */
384 if (active != head->next)
385 desc = to_dw_desc(active->prev);
386
387 dwc->residue -= desc->len;
388
Andy Shevchenkofdf475f2013-01-25 11:48:00 +0200389 child = to_dw_desc(active);
Andy Shevchenko77bcc4972013-01-18 14:14:15 +0200390
391 /* Submit next block */
Andy Shevchenkofdf475f2013-01-25 11:48:00 +0200392 dwc_do_single_block(dwc, child);
Andy Shevchenko77bcc4972013-01-18 14:14:15 +0200393
Andy Shevchenkofdf475f2013-01-25 11:48:00 +0200394 spin_unlock_irqrestore(&dwc->lock, flags);
Andy Shevchenko77bcc4972013-01-18 14:14:15 +0200395 return;
396 }
Andy Shevchenkofdf475f2013-01-25 11:48:00 +0200397
Andy Shevchenko77bcc4972013-01-18 14:14:15 +0200398 /* We are done here */
399 clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
400 }
Andy Shevchenko4702d522013-01-25 11:48:03 +0200401
402 dwc->residue = 0;
403
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530404 spin_unlock_irqrestore(&dwc->lock, flags);
405
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700406 dwc_complete_all(dw, dwc);
407 return;
408 }
409
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530410 if (list_empty(&dwc->active_list)) {
Andy Shevchenko4702d522013-01-25 11:48:03 +0200411 dwc->residue = 0;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530412 spin_unlock_irqrestore(&dwc->lock, flags);
Jamie Iles087809f2011-01-21 14:11:52 +0000413 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530414 }
Jamie Iles087809f2011-01-21 14:11:52 +0000415
Andy Shevchenko77bcc4972013-01-18 14:14:15 +0200416 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
417 dev_vdbg(chan2dev(&dwc->chan), "%s: soft LLP mode\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700418 spin_unlock_irqrestore(&dwc->lock, flags);
Dan Williams41d5e592009-01-06 11:38:21 -0700419 return;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700420 }
421
Andy Shevchenko5a87f0e2014-01-13 14:04:50 +0200422 dev_vdbg(chan2dev(&dwc->chan), "%s: llp=%pad\n", __func__, &llp);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700423
424 list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
Andy Shevchenko75c61222013-03-26 16:53:54 +0200425 /* Initial residue value */
Andy Shevchenko4702d522013-01-25 11:48:03 +0200426 dwc->residue = desc->total_len;
427
Andy Shevchenko75c61222013-03-26 16:53:54 +0200428 /* Check first descriptors addr */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530429 if (desc->txd.phys == llp) {
430 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700431 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530432 }
Viresh Kumar84adccf2011-03-24 11:32:15 +0530433
Andy Shevchenko75c61222013-03-26 16:53:54 +0200434 /* Check first descriptors llp */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530435 if (desc->lli.llp == llp) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700436 /* This one is currently in progress */
Andy Shevchenko4702d522013-01-25 11:48:03 +0200437 dwc->residue -= dwc_get_sent(dwc);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530438 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700439 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530440 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700441
Andy Shevchenko4702d522013-01-25 11:48:03 +0200442 dwc->residue -= desc->len;
443 list_for_each_entry(child, &desc->tx_list, desc_node) {
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530444 if (child->lli.llp == llp) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700445 /* Currently in progress */
Andy Shevchenko4702d522013-01-25 11:48:03 +0200446 dwc->residue -= dwc_get_sent(dwc);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530447 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700448 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530449 }
Andy Shevchenko4702d522013-01-25 11:48:03 +0200450 dwc->residue -= child->len;
451 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700452
453 /*
454 * No descriptors so far seem to be in progress, i.e.
455 * this one must be done.
456 */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530457 spin_unlock_irqrestore(&dwc->lock, flags);
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530458 dwc_descriptor_complete(dwc, desc, true);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530459 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700460 }
461
Dan Williams41d5e592009-01-06 11:38:21 -0700462 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700463 "BUG: All descriptors done, but channel not idle!\n");
464
465 /* Try to continue after resetting the channel... */
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300466 dwc_chan_disable(dw, dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700467
Andy Shevchenkoe7637c62014-06-18 12:15:36 +0300468 dwc_dostart_first_queued(dwc);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530469 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700470}
471
Andy Shevchenko93aad1b2012-07-13 11:09:32 +0300472static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700473{
Andy Shevchenko21d43f42012-10-18 17:34:09 +0300474 dev_crit(chan2dev(&dwc->chan), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
475 lli->sar, lli->dar, lli->llp, lli->ctlhi, lli->ctllo);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700476}
477
478static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
479{
480 struct dw_desc *bad_desc;
481 struct dw_desc *child;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530482 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700483
484 dwc_scan_descriptors(dw, dwc);
485
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530486 spin_lock_irqsave(&dwc->lock, flags);
487
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700488 /*
489 * The descriptor currently at the head of the active list is
490 * borked. Since we don't have any way to report errors, we'll
491 * just have to scream loudly and try to carry on.
492 */
493 bad_desc = dwc_first_active(dwc);
494 list_del_init(&bad_desc->desc_node);
Viresh Kumarf336e422011-03-03 15:47:16 +0530495 list_move(dwc->queue.next, dwc->active_list.prev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700496
497 /* Clear the error flag and try to restart the controller */
498 dma_writel(dw, CLEAR.ERROR, dwc->mask);
499 if (!list_empty(&dwc->active_list))
500 dwc_dostart(dwc, dwc_first_active(dwc));
501
502 /*
Andy Shevchenkoba84bd72012-10-18 17:34:11 +0300503 * WARN may seem harsh, but since this only happens
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700504 * when someone submits a bad physical address in a
505 * descriptor, we should consider ourselves lucky that the
506 * controller flagged an error instead of scribbling over
507 * random memory locations.
508 */
Andy Shevchenkoba84bd72012-10-18 17:34:11 +0300509 dev_WARN(chan2dev(&dwc->chan), "Bad descriptor submitted for DMA!\n"
510 " cookie: %d\n", bad_desc->txd.cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700511 dwc_dump_lli(dwc, &bad_desc->lli);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700512 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700513 dwc_dump_lli(dwc, &child->lli);
514
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530515 spin_unlock_irqrestore(&dwc->lock, flags);
516
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700517 /* Pretend the descriptor completed successfully */
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530518 dwc_descriptor_complete(dwc, bad_desc, true);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700519}
520
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200521/* --------------------- Cyclic DMA API extensions -------------------- */
522
Denis Efremov8004cbb2013-05-09 13:19:40 +0400523dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200524{
525 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
526 return channel_readl(dwc, SAR);
527}
528EXPORT_SYMBOL(dw_dma_get_src_addr);
529
Denis Efremov8004cbb2013-05-09 13:19:40 +0400530dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200531{
532 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
533 return channel_readl(dwc, DAR);
534}
535EXPORT_SYMBOL(dw_dma_get_dst_addr);
536
Andy Shevchenko75c61222013-03-26 16:53:54 +0200537/* Called with dwc->lock held and all DMAC interrupts disabled */
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200538static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530539 u32 status_err, u32 status_xfer)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200540{
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530541 unsigned long flags;
542
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530543 if (dwc->mask) {
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200544 void (*callback)(void *param);
545 void *callback_param;
546
547 dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
548 channel_readl(dwc, LLP));
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200549
550 callback = dwc->cdesc->period_callback;
551 callback_param = dwc->cdesc->period_callback_param;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530552
553 if (callback)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200554 callback(callback_param);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200555 }
556
557 /*
558 * Error and transfer complete are highly unlikely, and will most
559 * likely be due to a configuration error by the user.
560 */
561 if (unlikely(status_err & dwc->mask) ||
562 unlikely(status_xfer & dwc->mask)) {
563 int i;
564
Andy Shevchenkofc61f6b2014-01-13 14:04:49 +0200565 dev_err(chan2dev(&dwc->chan),
566 "cyclic DMA unexpected %s interrupt, stopping DMA transfer\n",
567 status_xfer ? "xfer" : "error");
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530568
569 spin_lock_irqsave(&dwc->lock, flags);
570
Andy Shevchenko1d455432012-06-19 13:34:03 +0300571 dwc_dump_chan_regs(dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200572
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300573 dwc_chan_disable(dw, dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200574
Andy Shevchenko75c61222013-03-26 16:53:54 +0200575 /* Make sure DMA does not restart by loading a new list */
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200576 channel_writel(dwc, LLP, 0);
577 channel_writel(dwc, CTL_LO, 0);
578 channel_writel(dwc, CTL_HI, 0);
579
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200580 dma_writel(dw, CLEAR.ERROR, dwc->mask);
581 dma_writel(dw, CLEAR.XFER, dwc->mask);
582
583 for (i = 0; i < dwc->cdesc->periods; i++)
584 dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530585
586 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200587 }
588}
589
590/* ------------------------------------------------------------------------- */
591
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700592static void dw_dma_tasklet(unsigned long data)
593{
594 struct dw_dma *dw = (struct dw_dma *)data;
595 struct dw_dma_chan *dwc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700596 u32 status_xfer;
597 u32 status_err;
598 int i;
599
Haavard Skinnemoen7fe7b2f2008-10-03 15:23:46 -0700600 status_xfer = dma_readl(dw, RAW.XFER);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700601 status_err = dma_readl(dw, RAW.ERROR);
602
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300603 dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700604
605 for (i = 0; i < dw->dma.chancnt; i++) {
606 dwc = &dw->chan[i];
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200607 if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530608 dwc_handle_cyclic(dw, dwc, status_err, status_xfer);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200609 else if (status_err & (1 << i))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700610 dwc_handle_error(dw, dwc);
Andy Shevchenko77bcc4972013-01-18 14:14:15 +0200611 else if (status_xfer & (1 << i))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700612 dwc_scan_descriptors(dw, dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700613 }
614
615 /*
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530616 * Re-enable interrupts.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700617 */
618 channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700619 channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
620}
621
622static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
623{
624 struct dw_dma *dw = dev_id;
Andy Shevchenko3783cef2013-07-15 15:04:39 +0300625 u32 status = dma_readl(dw, STATUS_INT);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700626
Andy Shevchenko3783cef2013-07-15 15:04:39 +0300627 dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__, status);
628
629 /* Check if we have any interrupt from the DMAC */
630 if (!status)
631 return IRQ_NONE;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700632
633 /*
634 * Just disable the interrupts. We'll turn them back on in the
635 * softirq handler.
636 */
637 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700638 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);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700648 channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
649 channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
650 channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
651 }
652
653 tasklet_schedule(&dw->tasklet);
654
655 return IRQ_HANDLED;
656}
657
658/*----------------------------------------------------------------------*/
659
660static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
661{
662 struct dw_desc *desc = txd_to_dw_desc(tx);
663 struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
664 dma_cookie_t cookie;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530665 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700666
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530667 spin_lock_irqsave(&dwc->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000668 cookie = dma_cookie_assign(tx);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700669
670 /*
671 * REVISIT: We should attempt to chain as many descriptors as
672 * possible, perhaps even appending to those already submitted
673 * for DMA. But this is hard to do in a race-free manner.
674 */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700675
Andy Shevchenkodd8ecfca2014-06-18 12:15:38 +0300676 dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n", __func__, desc->txd.cookie);
677 list_add_tail(&desc->desc_node, &dwc->queue);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700678
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530679 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700680
681 return cookie;
682}
683
684static struct dma_async_tx_descriptor *
685dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
686 size_t len, unsigned long flags)
687{
688 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Arnd Bergmannf7760762013-03-26 16:53:57 +0200689 struct dw_dma *dw = to_dw_dma(chan->device);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700690 struct dw_desc *desc;
691 struct dw_desc *first;
692 struct dw_desc *prev;
693 size_t xfer_count;
694 size_t offset;
695 unsigned int src_width;
696 unsigned int dst_width;
Andy Shevchenko3d4f8602012-10-01 13:06:25 +0300697 unsigned int data_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700698 u32 ctllo;
699
Andy Shevchenko2f45d612012-06-19 13:34:02 +0300700 dev_vdbg(chan2dev(chan),
Andy Shevchenko5a87f0e2014-01-13 14:04:50 +0200701 "%s: d%pad s%pad l0x%zx f0x%lx\n", __func__,
702 &dest, &src, len, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700703
704 if (unlikely(!len)) {
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300705 dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700706 return NULL;
707 }
708
Andy Shevchenko0fdb5672013-01-10 10:53:03 +0200709 dwc->direction = DMA_MEM_TO_MEM;
710
Arnd Bergmannf7760762013-03-26 16:53:57 +0200711 data_width = min_t(unsigned int, dw->data_width[dwc->src_master],
712 dw->data_width[dwc->dst_master]);
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300713
Andy Shevchenko3d4f8602012-10-01 13:06:25 +0300714 src_width = dst_width = min_t(unsigned int, data_width,
715 dwc_fast_fls(src | dest | len));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700716
Viresh Kumar327e6972012-02-01 16:12:26 +0530717 ctllo = DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700718 | 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,
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +0300727 dwc->block_size);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700728
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;
Andy Shevchenko176dcec2013-01-25 11:48:02 +0200737 desc->len = xfer_count << src_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700738
739 if (!first) {
740 first = desc;
741 } else {
742 prev->lli.llp = desc->txd.phys;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700743 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700744 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700745 }
746 prev = desc;
747 }
748
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700749 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;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700754 first->txd.flags = flags;
Andy Shevchenko30d38a32013-01-25 11:48:01 +0200755 first->total_len = len;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700756
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,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530766 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500767 unsigned long flags, void *context)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700768{
769 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Arnd Bergmannf7760762013-03-26 16:53:57 +0200770 struct dw_dma *dw = to_dw_dma(chan->device);
Viresh Kumar327e6972012-02-01 16:12:26 +0530771 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700772 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;
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300778 unsigned int data_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700779 unsigned int i;
780 struct scatterlist *sg;
781 size_t total_len = 0;
782
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300783 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700784
Andy Shevchenko495aea42013-01-10 11:11:41 +0200785 if (unlikely(!is_slave_direction(direction) || !sg_len))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700786 return NULL;
787
Andy Shevchenko0fdb5672013-01-10 10:53:03 +0200788 dwc->direction = direction;
789
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700790 prev = first = NULL;
791
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700792 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530793 case DMA_MEM_TO_DEV:
Viresh Kumar327e6972012-02-01 16:12:26 +0530794 reg_width = __fls(sconfig->dst_addr_width);
795 reg = sconfig->dst_addr;
796 ctllo = (DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700797 | DWC_CTLL_DST_WIDTH(reg_width)
798 | DWC_CTLL_DST_FIX
Viresh Kumar327e6972012-02-01 16:12:26 +0530799 | 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
Arnd Bergmannf7760762013-03-26 16:53:57 +0200804 data_width = dw->data_width[dwc->src_master];
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300805
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700806 for_each_sg(sgl, sg, sg_len, i) {
807 struct dw_desc *desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530808 u32 len, dlen, mem;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700809
Lars-Peter Clausencbb796c2012-04-25 20:50:51 +0200810 mem = sg_dma_address(sg);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700811 len = sg_dma_len(sg);
Viresh Kumar6bc711f2012-02-01 16:12:25 +0530812
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300813 mem_width = min_t(unsigned int,
814 data_width, dwc_fast_fls(mem | len));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700815
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530816slave_sg_todev_fill_desc:
817 desc = dwc_desc_get(dwc);
818 if (!desc) {
819 dev_err(chan2dev(chan),
820 "not enough descriptors available\n");
821 goto err_desc_get;
822 }
823
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700824 desc->lli.sar = mem;
825 desc->lli.dar = reg;
826 desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +0300827 if ((len >> mem_width) > dwc->block_size) {
828 dlen = dwc->block_size << mem_width;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530829 mem += dlen;
830 len -= dlen;
831 } else {
832 dlen = len;
833 len = 0;
834 }
835
836 desc->lli.ctlhi = dlen >> mem_width;
Andy Shevchenko176dcec2013-01-25 11:48:02 +0200837 desc->len = dlen;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700838
839 if (!first) {
840 first = desc;
841 } else {
842 prev->lli.llp = desc->txd.phys;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700843 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700844 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700845 }
846 prev = desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530847 total_len += dlen;
848
849 if (len)
850 goto slave_sg_todev_fill_desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700851 }
852 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530853 case DMA_DEV_TO_MEM:
Viresh Kumar327e6972012-02-01 16:12:26 +0530854 reg_width = __fls(sconfig->src_addr_width);
855 reg = sconfig->src_addr;
856 ctllo = (DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700857 | DWC_CTLL_SRC_WIDTH(reg_width)
858 | DWC_CTLL_DST_INC
Viresh Kumar327e6972012-02-01 16:12:26 +0530859 | DWC_CTLL_SRC_FIX);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700860
Viresh Kumar327e6972012-02-01 16:12:26 +0530861 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
862 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
863
Arnd Bergmannf7760762013-03-26 16:53:57 +0200864 data_width = dw->data_width[dwc->dst_master];
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300865
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700866 for_each_sg(sgl, sg, sg_len, i) {
867 struct dw_desc *desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530868 u32 len, dlen, mem;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700869
Lars-Peter Clausencbb796c2012-04-25 20:50:51 +0200870 mem = sg_dma_address(sg);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700871 len = sg_dma_len(sg);
Viresh Kumar6bc711f2012-02-01 16:12:25 +0530872
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300873 mem_width = min_t(unsigned int,
874 data_width, dwc_fast_fls(mem | len));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700875
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530876slave_sg_fromdev_fill_desc:
877 desc = dwc_desc_get(dwc);
878 if (!desc) {
879 dev_err(chan2dev(chan),
880 "not enough descriptors available\n");
881 goto err_desc_get;
882 }
883
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700884 desc->lli.sar = reg;
885 desc->lli.dar = mem;
886 desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +0300887 if ((len >> reg_width) > dwc->block_size) {
888 dlen = dwc->block_size << reg_width;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530889 mem += dlen;
890 len -= dlen;
891 } else {
892 dlen = len;
893 len = 0;
894 }
895 desc->lli.ctlhi = dlen >> reg_width;
Andy Shevchenko176dcec2013-01-25 11:48:02 +0200896 desc->len = dlen;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700897
898 if (!first) {
899 first = desc;
900 } else {
901 prev->lli.llp = desc->txd.phys;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700902 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700903 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700904 }
905 prev = desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530906 total_len += dlen;
907
908 if (len)
909 goto slave_sg_fromdev_fill_desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700910 }
911 break;
912 default:
913 return NULL;
914 }
915
916 if (flags & DMA_PREP_INTERRUPT)
917 /* Trigger interrupt after last block */
918 prev->lli.ctllo |= DWC_CTLL_INT_EN;
919
920 prev->lli.llp = 0;
Andy Shevchenko30d38a32013-01-25 11:48:01 +0200921 first->total_len = total_len;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700922
923 return &first->txd;
924
925err_desc_get:
926 dwc_desc_put(dwc, first);
927 return NULL;
928}
929
Andy Shevchenko4d130de2014-08-19 20:29:16 +0300930bool dw_dma_filter(struct dma_chan *chan, void *param)
931{
932 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
933 struct dw_dma_slave *dws = param;
934
935 if (!dws || dws->dma_dev != chan->device->dev)
936 return false;
937
938 /* We have to copy data since dws can be temporary storage */
939
940 dwc->src_id = dws->src_id;
941 dwc->dst_id = dws->dst_id;
942
943 dwc->src_master = dws->src_master;
944 dwc->dst_master = dws->dst_master;
945
946 return true;
947}
948EXPORT_SYMBOL_GPL(dw_dma_filter);
949
Viresh Kumar327e6972012-02-01 16:12:26 +0530950/*
951 * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
952 * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
953 *
954 * NOTE: burst size 2 is not supported by controller.
955 *
956 * This can be done by finding least significant bit set: n & (n - 1)
957 */
958static inline void convert_burst(u32 *maxburst)
959{
960 if (*maxburst > 1)
961 *maxburst = fls(*maxburst) - 2;
962 else
963 *maxburst = 0;
964}
965
Maxime Riparda4b0d342014-11-17 14:42:12 +0100966static int dwc_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
Viresh Kumar327e6972012-02-01 16:12:26 +0530967{
968 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
969
Andy Shevchenko495aea42013-01-10 11:11:41 +0200970 /* Check if chan will be configured for slave transfers */
971 if (!is_slave_direction(sconfig->direction))
Viresh Kumar327e6972012-02-01 16:12:26 +0530972 return -EINVAL;
973
974 memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
Andy Shevchenko0fdb5672013-01-10 10:53:03 +0200975 dwc->direction = sconfig->direction;
Viresh Kumar327e6972012-02-01 16:12:26 +0530976
977 convert_burst(&dwc->dma_sconfig.src_maxburst);
978 convert_burst(&dwc->dma_sconfig.dst_maxburst);
979
980 return 0;
981}
982
Maxime Riparda4b0d342014-11-17 14:42:12 +0100983static int dwc_pause(struct dma_chan *chan)
Andy Shevchenko21fe3c52013-01-09 10:17:14 +0200984{
Maxime Riparda4b0d342014-11-17 14:42:12 +0100985 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
986 unsigned long flags;
987 unsigned int count = 20; /* timeout iterations */
988 u32 cfglo;
Andy Shevchenko21fe3c52013-01-09 10:17:14 +0200989
Maxime Riparda4b0d342014-11-17 14:42:12 +0100990 spin_lock_irqsave(&dwc->lock, flags);
991
992 cfglo = channel_readl(dwc, CFG_LO);
Andy Shevchenko21fe3c52013-01-09 10:17:14 +0200993 channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
Andy Shevchenko123b69a2013-03-21 11:49:17 +0200994 while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY) && count--)
995 udelay(2);
Andy Shevchenko21fe3c52013-01-09 10:17:14 +0200996
997 dwc->paused = true;
Maxime Riparda4b0d342014-11-17 14:42:12 +0100998
999 spin_unlock_irqrestore(&dwc->lock, flags);
1000
1001 return 0;
Andy Shevchenko21fe3c52013-01-09 10:17:14 +02001002}
1003
1004static inline void dwc_chan_resume(struct dw_dma_chan *dwc)
1005{
1006 u32 cfglo = channel_readl(dwc, CFG_LO);
1007
1008 channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
1009
1010 dwc->paused = false;
1011}
1012
Maxime Riparda4b0d342014-11-17 14:42:12 +01001013static int dwc_resume(struct dma_chan *chan)
1014{
1015 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1016 unsigned long flags;
1017
1018 if (!dwc->paused)
1019 return 0;
1020
1021 spin_lock_irqsave(&dwc->lock, flags);
1022
1023 dwc_chan_resume(dwc);
1024
1025 spin_unlock_irqrestore(&dwc->lock, flags);
1026
1027 return 0;
1028}
1029
1030static int dwc_terminate_all(struct dma_chan *chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001031{
1032 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1033 struct dw_dma *dw = to_dw_dma(chan->device);
1034 struct dw_desc *desc, *_desc;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301035 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001036 LIST_HEAD(list);
1037
Maxime Riparda4b0d342014-11-17 14:42:12 +01001038 spin_lock_irqsave(&dwc->lock, flags);
Linus Walleija7c57cf2011-04-19 08:31:32 +08001039
Maxime Riparda4b0d342014-11-17 14:42:12 +01001040 clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
Linus Walleija7c57cf2011-04-19 08:31:32 +08001041
Maxime Riparda4b0d342014-11-17 14:42:12 +01001042 dwc_chan_disable(dw, dwc);
Linus Walleija7c57cf2011-04-19 08:31:32 +08001043
Maxime Riparda4b0d342014-11-17 14:42:12 +01001044 dwc_chan_resume(dwc);
Linus Walleija7c57cf2011-04-19 08:31:32 +08001045
Maxime Riparda4b0d342014-11-17 14:42:12 +01001046 /* active_list entries will end up before queued entries */
1047 list_splice_init(&dwc->queue, &list);
1048 list_splice_init(&dwc->active_list, &list);
Linus Walleija7c57cf2011-04-19 08:31:32 +08001049
Maxime Riparda4b0d342014-11-17 14:42:12 +01001050 spin_unlock_irqrestore(&dwc->lock, flags);
Linus Walleija7c57cf2011-04-19 08:31:32 +08001051
Maxime Riparda4b0d342014-11-17 14:42:12 +01001052 /* Flush all pending and queued descriptors */
1053 list_for_each_entry_safe(desc, _desc, &list, desc_node)
1054 dwc_descriptor_complete(dwc, desc, false);
Linus Walleijc3635c72010-03-26 16:44:01 -07001055
Linus Walleijc3635c72010-03-26 16:44:01 -07001056 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001057}
1058
Andy Shevchenko4702d522013-01-25 11:48:03 +02001059static inline u32 dwc_get_residue(struct dw_dma_chan *dwc)
1060{
1061 unsigned long flags;
1062 u32 residue;
1063
1064 spin_lock_irqsave(&dwc->lock, flags);
1065
1066 residue = dwc->residue;
1067 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags) && residue)
1068 residue -= dwc_get_sent(dwc);
1069
1070 spin_unlock_irqrestore(&dwc->lock, flags);
1071 return residue;
1072}
1073
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001074static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -07001075dwc_tx_status(struct dma_chan *chan,
1076 dma_cookie_t cookie,
1077 struct dma_tx_state *txstate)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001078{
1079 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001080 enum dma_status ret;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001081
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001082 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul2c404102013-10-16 13:41:15 +05301083 if (ret == DMA_COMPLETE)
Andy Shevchenko12381dc2013-07-15 15:04:40 +03001084 return ret;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001085
Andy Shevchenko12381dc2013-07-15 15:04:40 +03001086 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001087
Andy Shevchenko12381dc2013-07-15 15:04:40 +03001088 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul2c404102013-10-16 13:41:15 +05301089 if (ret != DMA_COMPLETE)
Andy Shevchenko4702d522013-01-25 11:48:03 +02001090 dma_set_residue(txstate, dwc_get_residue(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001091
Andy Shevchenkoeffd5cf2013-07-15 15:04:41 +03001092 if (dwc->paused && ret == DMA_IN_PROGRESS)
Linus Walleija7c57cf2011-04-19 08:31:32 +08001093 return DMA_PAUSED;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001094
1095 return ret;
1096}
1097
1098static void dwc_issue_pending(struct dma_chan *chan)
1099{
1100 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Andy Shevchenkodd8ecfca2014-06-18 12:15:38 +03001101 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001102
Andy Shevchenkodd8ecfca2014-06-18 12:15:38 +03001103 spin_lock_irqsave(&dwc->lock, flags);
1104 if (list_empty(&dwc->active_list))
1105 dwc_dostart_first_queued(dwc);
1106 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001107}
1108
Andy Shevchenko99d9bf42014-09-23 17:18:14 +03001109/*----------------------------------------------------------------------*/
1110
1111static void dw_dma_off(struct dw_dma *dw)
1112{
1113 int i;
1114
1115 dma_writel(dw, CFG, 0);
1116
1117 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
1118 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1119 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1120 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1121
1122 while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
1123 cpu_relax();
1124
1125 for (i = 0; i < dw->dma.chancnt; i++)
1126 dw->chan[i].initialized = false;
1127}
1128
1129static void dw_dma_on(struct dw_dma *dw)
1130{
1131 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1132}
1133
Dan Williamsaa1e6f12009-01-06 11:38:17 -07001134static int dwc_alloc_chan_resources(struct dma_chan *chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001135{
1136 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1137 struct dw_dma *dw = to_dw_dma(chan->device);
1138 struct dw_desc *desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001139 int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301140 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001141
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001142 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001143
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001144 /* ASSERT: channel is idle */
1145 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -07001146 dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001147 return -EIO;
1148 }
1149
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001150 dma_cookie_init(chan);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001151
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001152 /*
1153 * NOTE: some controllers may have additional features that we
1154 * need to initialize here, like "scatter-gather" (which
1155 * doesn't mean what you think it means), and status writeback.
1156 */
1157
Andy Shevchenko99d9bf42014-09-23 17:18:14 +03001158 /* Enable controller here if needed */
1159 if (!dw->in_use)
1160 dw_dma_on(dw);
1161 dw->in_use |= dwc->mask;
1162
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301163 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001164 i = dwc->descs_allocated;
1165 while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001166 dma_addr_t phys;
1167
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301168 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001169
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001170 desc = dma_pool_alloc(dw->desc_pool, GFP_ATOMIC, &phys);
Andy Shevchenkocbd65312013-01-09 10:17:11 +02001171 if (!desc)
1172 goto err_desc_alloc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001173
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001174 memset(desc, 0, sizeof(struct dw_desc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001175
Dan Williamse0bd0f82009-09-08 17:53:02 -07001176 INIT_LIST_HEAD(&desc->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001177 dma_async_tx_descriptor_init(&desc->txd, chan);
1178 desc->txd.tx_submit = dwc_tx_submit;
1179 desc->txd.flags = DMA_CTRL_ACK;
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001180 desc->txd.phys = phys;
Andy Shevchenkocbd65312013-01-09 10:17:11 +02001181
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001182 dwc_desc_put(dwc, desc);
1183
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301184 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001185 i = ++dwc->descs_allocated;
1186 }
1187
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301188 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001189
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001190 dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001191
1192 return i;
Andy Shevchenkocbd65312013-01-09 10:17:11 +02001193
1194err_desc_alloc:
Andy Shevchenkocbd65312013-01-09 10:17:11 +02001195 dev_info(chan2dev(chan), "only allocated %d descriptors\n", i);
1196
1197 return i;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001198}
1199
1200static void dwc_free_chan_resources(struct dma_chan *chan)
1201{
1202 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1203 struct dw_dma *dw = to_dw_dma(chan->device);
1204 struct dw_desc *desc, *_desc;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301205 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001206 LIST_HEAD(list);
1207
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001208 dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001209 dwc->descs_allocated);
1210
1211 /* ASSERT: channel is idle */
1212 BUG_ON(!list_empty(&dwc->active_list));
1213 BUG_ON(!list_empty(&dwc->queue));
1214 BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
1215
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301216 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001217 list_splice_init(&dwc->free_list, &list);
1218 dwc->descs_allocated = 0;
Viresh Kumar61e183f2011-11-17 16:01:29 +05301219 dwc->initialized = false;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001220
1221 /* Disable interrupts */
1222 channel_clear_bit(dw, MASK.XFER, dwc->mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001223 channel_clear_bit(dw, MASK.ERROR, dwc->mask);
1224
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301225 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001226
Andy Shevchenko99d9bf42014-09-23 17:18:14 +03001227 /* Disable controller in case it was a last user */
1228 dw->in_use &= ~dwc->mask;
1229 if (!dw->in_use)
1230 dw_dma_off(dw);
1231
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001232 list_for_each_entry_safe(desc, _desc, &list, desc_node) {
Dan Williams41d5e592009-01-06 11:38:21 -07001233 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001234 dma_pool_free(dw->desc_pool, desc, desc->txd.phys);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001235 }
1236
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001237 dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001238}
1239
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001240/* --------------------- Cyclic DMA API extensions -------------------- */
1241
1242/**
1243 * dw_dma_cyclic_start - start the cyclic DMA transfer
1244 * @chan: the DMA channel to start
1245 *
1246 * Must be called with soft interrupts disabled. Returns zero on success or
1247 * -errno on failure.
1248 */
1249int dw_dma_cyclic_start(struct dma_chan *chan)
1250{
1251 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1252 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301253 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001254
1255 if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
1256 dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
1257 return -ENODEV;
1258 }
1259
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301260 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001261
Andy Shevchenko75c61222013-03-26 16:53:54 +02001262 /* Assert channel is idle */
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001263 if (dma_readl(dw, CH_EN) & dwc->mask) {
1264 dev_err(chan2dev(&dwc->chan),
Jarkko Nikula550da642015-03-10 11:37:23 +02001265 "%s: BUG: Attempted to start non-idle channel\n",
1266 __func__);
Andy Shevchenko1d455432012-06-19 13:34:03 +03001267 dwc_dump_chan_regs(dwc);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301268 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001269 return -EBUSY;
1270 }
1271
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001272 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1273 dma_writel(dw, CLEAR.XFER, dwc->mask);
1274
Andy Shevchenko75c61222013-03-26 16:53:54 +02001275 /* Setup DMAC channel registers */
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001276 channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
1277 channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
1278 channel_writel(dwc, CTL_HI, 0);
1279
1280 channel_set_bit(dw, CH_EN, dwc->mask);
1281
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301282 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001283
1284 return 0;
1285}
1286EXPORT_SYMBOL(dw_dma_cyclic_start);
1287
1288/**
1289 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1290 * @chan: the DMA channel to stop
1291 *
1292 * Must be called with soft interrupts disabled.
1293 */
1294void dw_dma_cyclic_stop(struct dma_chan *chan)
1295{
1296 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1297 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301298 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001299
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301300 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001301
Andy Shevchenko3f9362072012-06-19 13:46:32 +03001302 dwc_chan_disable(dw, dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001303
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301304 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001305}
1306EXPORT_SYMBOL(dw_dma_cyclic_stop);
1307
1308/**
1309 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1310 * @chan: the DMA channel to prepare
1311 * @buf_addr: physical DMA address where the buffer starts
1312 * @buf_len: total number of bytes for the entire buffer
1313 * @period_len: number of bytes for each period
1314 * @direction: transfer direction, to or from device
1315 *
1316 * Must be called before trying to start the transfer. Returns a valid struct
1317 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1318 */
1319struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
1320 dma_addr_t buf_addr, size_t buf_len, size_t period_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05301321 enum dma_transfer_direction direction)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001322{
1323 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Viresh Kumar327e6972012-02-01 16:12:26 +05301324 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001325 struct dw_cyclic_desc *cdesc;
1326 struct dw_cyclic_desc *retval = NULL;
1327 struct dw_desc *desc;
1328 struct dw_desc *last = NULL;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001329 unsigned long was_cyclic;
1330 unsigned int reg_width;
1331 unsigned int periods;
1332 unsigned int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301333 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001334
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301335 spin_lock_irqsave(&dwc->lock, flags);
Andy Shevchenkofed25742012-09-21 15:05:49 +03001336 if (dwc->nollp) {
1337 spin_unlock_irqrestore(&dwc->lock, flags);
1338 dev_dbg(chan2dev(&dwc->chan),
1339 "channel doesn't support LLP transfers\n");
1340 return ERR_PTR(-EINVAL);
1341 }
1342
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001343 if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301344 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001345 dev_dbg(chan2dev(&dwc->chan),
1346 "queue and/or active list are not empty\n");
1347 return ERR_PTR(-EBUSY);
1348 }
1349
1350 was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301351 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001352 if (was_cyclic) {
1353 dev_dbg(chan2dev(&dwc->chan),
1354 "channel already prepared for cyclic DMA\n");
1355 return ERR_PTR(-EBUSY);
1356 }
1357
1358 retval = ERR_PTR(-EINVAL);
Viresh Kumar327e6972012-02-01 16:12:26 +05301359
Andy Shevchenkof44b92f2013-01-10 10:52:58 +02001360 if (unlikely(!is_slave_direction(direction)))
1361 goto out_err;
1362
Andy Shevchenko0fdb5672013-01-10 10:53:03 +02001363 dwc->direction = direction;
1364
Viresh Kumar327e6972012-02-01 16:12:26 +05301365 if (direction == DMA_MEM_TO_DEV)
1366 reg_width = __ffs(sconfig->dst_addr_width);
1367 else
1368 reg_width = __ffs(sconfig->src_addr_width);
1369
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001370 periods = buf_len / period_len;
1371
1372 /* Check for too big/unaligned periods and unaligned DMA buffer. */
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001373 if (period_len > (dwc->block_size << reg_width))
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001374 goto out_err;
1375 if (unlikely(period_len & ((1 << reg_width) - 1)))
1376 goto out_err;
1377 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
1378 goto out_err;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001379
1380 retval = ERR_PTR(-ENOMEM);
1381
1382 if (periods > NR_DESCS_PER_CHANNEL)
1383 goto out_err;
1384
1385 cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
1386 if (!cdesc)
1387 goto out_err;
1388
1389 cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
1390 if (!cdesc->desc)
1391 goto out_err_alloc;
1392
1393 for (i = 0; i < periods; i++) {
1394 desc = dwc_desc_get(dwc);
1395 if (!desc)
1396 goto out_err_desc_get;
1397
1398 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +05301399 case DMA_MEM_TO_DEV:
Viresh Kumar327e6972012-02-01 16:12:26 +05301400 desc->lli.dar = sconfig->dst_addr;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001401 desc->lli.sar = buf_addr + (period_len * i);
Viresh Kumar327e6972012-02-01 16:12:26 +05301402 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001403 | DWC_CTLL_DST_WIDTH(reg_width)
1404 | DWC_CTLL_SRC_WIDTH(reg_width)
1405 | DWC_CTLL_DST_FIX
1406 | DWC_CTLL_SRC_INC
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001407 | DWC_CTLL_INT_EN);
Viresh Kumar327e6972012-02-01 16:12:26 +05301408
1409 desc->lli.ctllo |= sconfig->device_fc ?
1410 DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
1411 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
1412
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001413 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +05301414 case DMA_DEV_TO_MEM:
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001415 desc->lli.dar = buf_addr + (period_len * i);
Viresh Kumar327e6972012-02-01 16:12:26 +05301416 desc->lli.sar = sconfig->src_addr;
1417 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001418 | DWC_CTLL_SRC_WIDTH(reg_width)
1419 | DWC_CTLL_DST_WIDTH(reg_width)
1420 | DWC_CTLL_DST_INC
1421 | DWC_CTLL_SRC_FIX
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001422 | DWC_CTLL_INT_EN);
Viresh Kumar327e6972012-02-01 16:12:26 +05301423
1424 desc->lli.ctllo |= sconfig->device_fc ?
1425 DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
1426 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
1427
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001428 break;
1429 default:
1430 break;
1431 }
1432
1433 desc->lli.ctlhi = (period_len >> reg_width);
1434 cdesc->desc[i] = desc;
1435
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001436 if (last)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001437 last->lli.llp = desc->txd.phys;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001438
1439 last = desc;
1440 }
1441
Andy Shevchenko75c61222013-03-26 16:53:54 +02001442 /* Let's make a cyclic list */
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001443 last->lli.llp = cdesc->desc[0]->txd.phys;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001444
Andy Shevchenko5a87f0e2014-01-13 14:04:50 +02001445 dev_dbg(chan2dev(&dwc->chan),
1446 "cyclic prepared buf %pad len %zu period %zu periods %d\n",
1447 &buf_addr, buf_len, period_len, periods);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001448
1449 cdesc->periods = periods;
1450 dwc->cdesc = cdesc;
1451
1452 return cdesc;
1453
1454out_err_desc_get:
1455 while (i--)
1456 dwc_desc_put(dwc, cdesc->desc[i]);
1457out_err_alloc:
1458 kfree(cdesc);
1459out_err:
1460 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1461 return (struct dw_cyclic_desc *)retval;
1462}
1463EXPORT_SYMBOL(dw_dma_cyclic_prep);
1464
1465/**
1466 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1467 * @chan: the DMA channel to free
1468 */
1469void dw_dma_cyclic_free(struct dma_chan *chan)
1470{
1471 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1472 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1473 struct dw_cyclic_desc *cdesc = dwc->cdesc;
1474 int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301475 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001476
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001477 dev_dbg(chan2dev(&dwc->chan), "%s\n", __func__);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001478
1479 if (!cdesc)
1480 return;
1481
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301482 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001483
Andy Shevchenko3f9362072012-06-19 13:46:32 +03001484 dwc_chan_disable(dw, dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001485
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001486 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1487 dma_writel(dw, CLEAR.XFER, dwc->mask);
1488
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301489 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001490
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
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001501/*----------------------------------------------------------------------*/
1502
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001503int dw_dma_probe(struct dw_dma_chip *chip, struct dw_dma_platform_data *pdata)
Viresh Kumara9ddb572012-10-16 09:49:17 +05301504{
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001505 struct dw_dma *dw;
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001506 bool autocfg;
1507 unsigned int dw_params;
1508 unsigned int nr_channels;
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001509 unsigned int max_blk_size = 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001510 int err;
1511 int i;
1512
Andy Shevchenko000871c2014-03-05 15:48:12 +02001513 dw = devm_kzalloc(chip->dev, sizeof(*dw), GFP_KERNEL);
1514 if (!dw)
1515 return -ENOMEM;
1516
1517 dw->regs = chip->regs;
1518 chip->dw = dw;
1519
Andy Shevchenkobb32baf2014-11-05 18:34:48 +02001520 pm_runtime_get_sync(chip->dev);
1521
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001522 dw_params = dma_read_byaddr(chip->regs, DW_PARAMS);
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001523 autocfg = dw_params >> DW_PARAMS_EN & 0x1;
1524
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001525 dev_dbg(chip->dev, "DW_PARAMS: 0x%08x\n", dw_params);
Andy Shevchenko123de542013-01-09 10:17:01 +02001526
1527 if (!pdata && autocfg) {
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001528 pdata = devm_kzalloc(chip->dev, sizeof(*pdata), GFP_KERNEL);
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001529 if (!pdata) {
1530 err = -ENOMEM;
1531 goto err_pdata;
1532 }
Andy Shevchenko123de542013-01-09 10:17:01 +02001533
1534 /* Fill platform data with the default values */
1535 pdata->is_private = true;
1536 pdata->chan_allocation_order = CHAN_ALLOCATION_ASCENDING;
1537 pdata->chan_priority = CHAN_PRIORITY_ASCENDING;
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001538 } else if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS) {
1539 err = -EINVAL;
1540 goto err_pdata;
1541 }
Andy Shevchenko123de542013-01-09 10:17:01 +02001542
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001543 if (autocfg)
1544 nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 0x7) + 1;
1545 else
1546 nr_channels = pdata->nr_channels;
1547
Andy Shevchenko000871c2014-03-05 15:48:12 +02001548 dw->chan = devm_kcalloc(chip->dev, nr_channels, sizeof(*dw->chan),
1549 GFP_KERNEL);
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001550 if (!dw->chan) {
1551 err = -ENOMEM;
1552 goto err_pdata;
1553 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001554
Andy Shevchenko75c61222013-03-26 16:53:54 +02001555 /* Get hardware configuration parameters */
Andy Shevchenkoa0982002012-09-21 15:05:48 +03001556 if (autocfg) {
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001557 max_blk_size = dma_readl(dw, MAX_BLK_SIZE);
1558
Andy Shevchenkoa0982002012-09-21 15:05:48 +03001559 dw->nr_masters = (dw_params >> DW_PARAMS_NR_MASTER & 3) + 1;
1560 for (i = 0; i < dw->nr_masters; i++) {
1561 dw->data_width[i] =
1562 (dw_params >> DW_PARAMS_DATA_WIDTH(i) & 3) + 2;
1563 }
1564 } else {
1565 dw->nr_masters = pdata->nr_masters;
Andy Shevchenkod8ded502015-01-13 19:08:14 +02001566 for (i = 0; i < dw->nr_masters; i++)
1567 dw->data_width[i] = pdata->data_width[i];
Andy Shevchenkoa0982002012-09-21 15:05:48 +03001568 }
1569
Andy Shevchenko11f932e2012-06-19 13:34:06 +03001570 /* Calculate all channel mask before DMA setup */
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001571 dw->all_chan_mask = (1 << nr_channels) - 1;
Andy Shevchenko11f932e2012-06-19 13:34:06 +03001572
Andy Shevchenko75c61222013-03-26 16:53:54 +02001573 /* Force dma off, just in case */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001574 dw_dma_off(dw);
1575
Andy Shevchenko75c61222013-03-26 16:53:54 +02001576 /* Disable BLOCK interrupts as well */
Andy Shevchenko236b1062012-06-19 13:34:07 +03001577 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
1578
Andy Shevchenko75c61222013-03-26 16:53:54 +02001579 /* Create a pool of consistent memory blocks for hardware descriptors */
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001580 dw->desc_pool = dmam_pool_create("dw_dmac_desc_pool", chip->dev,
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001581 sizeof(struct dw_desc), 4, 0);
1582 if (!dw->desc_pool) {
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001583 dev_err(chip->dev, "No memory for descriptors dma pool\n");
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001584 err = -ENOMEM;
1585 goto err_pdata;
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001586 }
1587
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001588 tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
1589
Andy Shevchenko97977f72014-05-07 10:56:24 +03001590 err = request_irq(chip->irq, dw_dma_interrupt, IRQF_SHARED,
1591 "dw_dmac", dw);
1592 if (err)
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001593 goto err_pdata;
Andy Shevchenko97977f72014-05-07 10:56:24 +03001594
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001595 INIT_LIST_HEAD(&dw->dma.channels);
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001596 for (i = 0; i < nr_channels; i++) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001597 struct dw_dma_chan *dwc = &dw->chan[i];
Andy Shevchenkofed25742012-09-21 15:05:49 +03001598 int r = nr_channels - i - 1;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001599
1600 dwc->chan.device = &dw->dma;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001601 dma_cookie_init(&dwc->chan);
Viresh Kumarb0c31302011-03-03 15:47:21 +05301602 if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
1603 list_add_tail(&dwc->chan.device_node,
1604 &dw->dma.channels);
1605 else
1606 list_add(&dwc->chan.device_node, &dw->dma.channels);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001607
Viresh Kumar93317e82011-03-03 15:47:22 +05301608 /* 7 is highest priority & 0 is lowest. */
1609 if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
Andy Shevchenkofed25742012-09-21 15:05:49 +03001610 dwc->priority = r;
Viresh Kumar93317e82011-03-03 15:47:22 +05301611 else
1612 dwc->priority = i;
1613
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001614 dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
1615 spin_lock_init(&dwc->lock);
1616 dwc->mask = 1 << i;
1617
1618 INIT_LIST_HEAD(&dwc->active_list);
1619 INIT_LIST_HEAD(&dwc->queue);
1620 INIT_LIST_HEAD(&dwc->free_list);
1621
1622 channel_clear_bit(dw, CH_EN, dwc->mask);
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001623
Andy Shevchenko0fdb5672013-01-10 10:53:03 +02001624 dwc->direction = DMA_TRANS_NONE;
Andy Shevchenkoa0982002012-09-21 15:05:48 +03001625
Andy Shevchenko75c61222013-03-26 16:53:54 +02001626 /* Hardware configuration */
Andy Shevchenkofed25742012-09-21 15:05:49 +03001627 if (autocfg) {
1628 unsigned int dwc_params;
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001629 void __iomem *addr = chip->regs + r * sizeof(u32);
Andy Shevchenkofed25742012-09-21 15:05:49 +03001630
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001631 dwc_params = dma_read_byaddr(addr, DWC_PARAMS);
Andy Shevchenkofed25742012-09-21 15:05:49 +03001632
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001633 dev_dbg(chip->dev, "DWC_PARAMS[%d]: 0x%08x\n", i,
1634 dwc_params);
Andy Shevchenko985a6c72013-01-18 17:10:59 +02001635
Andy Shevchenko1d566f12014-01-13 14:04:48 +02001636 /*
1637 * Decode maximum block size for given channel. The
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001638 * stored 4 bit value represents blocks from 0x00 for 3
Andy Shevchenko1d566f12014-01-13 14:04:48 +02001639 * up to 0x0a for 4095.
1640 */
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001641 dwc->block_size =
1642 (4 << ((max_blk_size >> 4 * i) & 0xf)) - 1;
Andy Shevchenkofed25742012-09-21 15:05:49 +03001643 dwc->nollp =
1644 (dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0;
1645 } else {
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001646 dwc->block_size = pdata->block_size;
Andy Shevchenkofed25742012-09-21 15:05:49 +03001647
1648 /* Check if channel supports multi block transfer */
1649 channel_writel(dwc, LLP, 0xfffffffc);
1650 dwc->nollp =
1651 (channel_readl(dwc, LLP) & 0xfffffffc) == 0;
1652 channel_writel(dwc, LLP, 0);
1653 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001654 }
1655
Andy Shevchenko11f932e2012-06-19 13:34:06 +03001656 /* Clear all interrupts on all channels. */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001657 dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
Andy Shevchenko236b1062012-06-19 13:34:07 +03001658 dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001659 dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
1660 dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
1661 dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
1662
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001663 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
1664 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
Jamie Iles95ea7592011-01-21 14:11:54 +00001665 if (pdata->is_private)
1666 dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001667 dw->dma.dev = chip->dev;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001668 dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
1669 dw->dma.device_free_chan_resources = dwc_free_chan_resources;
1670
1671 dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001672 dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
Andy Shevchenko029a40e2015-01-02 16:17:24 +02001673
Maxime Riparda4b0d342014-11-17 14:42:12 +01001674 dw->dma.device_config = dwc_config;
1675 dw->dma.device_pause = dwc_pause;
1676 dw->dma.device_resume = dwc_resume;
1677 dw->dma.device_terminate_all = dwc_terminate_all;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001678
Linus Walleij07934482010-03-26 16:50:49 -07001679 dw->dma.device_tx_status = dwc_tx_status;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001680 dw->dma.device_issue_pending = dwc_issue_pending;
1681
Andy Shevchenko029a40e2015-01-02 16:17:24 +02001682 /* DMA capabilities */
1683 dw->dma.src_addr_widths = DW_DMA_BUSWIDTHS;
1684 dw->dma.dst_addr_widths = DW_DMA_BUSWIDTHS;
1685 dw->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
1686 BIT(DMA_MEM_TO_MEM);
1687 dw->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1688
Andy Shevchenko12229342014-05-08 12:01:50 +03001689 err = dma_async_device_register(&dw->dma);
1690 if (err)
1691 goto err_dma_register;
1692
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001693 dev_info(chip->dev, "DesignWare DMA Controller, %d channels\n",
Andy Shevchenko21d43f42012-10-18 17:34:09 +03001694 nr_channels);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001695
Andy Shevchenkobb32baf2014-11-05 18:34:48 +02001696 pm_runtime_put_sync_suspend(chip->dev);
1697
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001698 return 0;
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001699
Andy Shevchenko12229342014-05-08 12:01:50 +03001700err_dma_register:
1701 free_irq(chip->irq, dw);
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001702err_pdata:
Andy Shevchenkobb32baf2014-11-05 18:34:48 +02001703 pm_runtime_put_sync_suspend(chip->dev);
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001704 return err;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001705}
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001706EXPORT_SYMBOL_GPL(dw_dma_probe);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001707
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001708int dw_dma_remove(struct dw_dma_chip *chip)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001709{
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001710 struct dw_dma *dw = chip->dw;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001711 struct dw_dma_chan *dwc, *_dwc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001712
Andy Shevchenkobb32baf2014-11-05 18:34:48 +02001713 pm_runtime_get_sync(chip->dev);
1714
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001715 dw_dma_off(dw);
1716 dma_async_device_unregister(&dw->dma);
1717
Andy Shevchenko97977f72014-05-07 10:56:24 +03001718 free_irq(chip->irq, dw);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001719 tasklet_kill(&dw->tasklet);
1720
1721 list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
1722 chan.device_node) {
1723 list_del(&dwc->chan.device_node);
1724 channel_clear_bit(dw, CH_EN, dwc->mask);
1725 }
1726
Andy Shevchenkobb32baf2014-11-05 18:34:48 +02001727 pm_runtime_put_sync_suspend(chip->dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001728 return 0;
1729}
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001730EXPORT_SYMBOL_GPL(dw_dma_remove);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001731
Andy Shevchenko2540f742014-09-23 17:18:13 +03001732int dw_dma_disable(struct dw_dma_chip *chip)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001733{
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001734 struct dw_dma *dw = chip->dw;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001735
Andy Shevchenko6168d562012-10-18 17:34:10 +03001736 dw_dma_off(dw);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001737 return 0;
1738}
Andy Shevchenko2540f742014-09-23 17:18:13 +03001739EXPORT_SYMBOL_GPL(dw_dma_disable);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001740
Andy Shevchenko2540f742014-09-23 17:18:13 +03001741int dw_dma_enable(struct dw_dma_chip *chip)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001742{
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001743 struct dw_dma *dw = chip->dw;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001744
Andy Shevchenko7a83c042014-09-23 17:18:12 +03001745 dw_dma_on(dw);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001746 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001747}
Andy Shevchenko2540f742014-09-23 17:18:13 +03001748EXPORT_SYMBOL_GPL(dw_dma_enable);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001749
1750MODULE_LICENSE("GPL v2");
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001751MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller core driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001752MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Viresh Kumar10d89352012-06-20 12:53:02 -07001753MODULE_AUTHOR("Viresh Kumar <viresh.linux@gmail.com>");