blob: 85ed5f20e027a9a077483506b12f014ecc7a93ed [file] [log] [blame]
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001/*
2 * Driver for the Synopsys DesignWare DMA Controller (aka DMACA on
3 * AVR32 systems.)
4 *
5 * Copyright (C) 2007-2008 Atmel Corporation
Viresh Kumaraecb7b62011-05-24 14:04:09 +05306 * Copyright (C) 2010-2011 ST Microelectronics
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 */
Viresh Kumar327e6972012-02-01 16:12:26 +053012#include <linux/bitops.h>
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070013#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>
Viresh Kumard3f797d2012-04-20 20:15:34 +053020#include <linux/of.h>
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070021#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"
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000027#include "dmaengine.h"
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070028
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
Viresh Kumar327e6972012-02-01 16:12:26 +053039#define DWC_DEFAULT_CTLLO(_chan) ({ \
40 struct dw_dma_slave *__slave = (_chan->private); \
41 struct dw_dma_chan *_dwc = to_dw_dma_chan(_chan); \
42 struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \
43 int _dms = __slave ? __slave->dst_master : 0; \
44 int _sms = __slave ? __slave->src_master : 1; \
45 u8 _smsize = __slave ? _sconfig->src_maxburst : \
46 DW_DMA_MSIZE_16; \
47 u8 _dmsize = __slave ? _sconfig->dst_maxburst : \
48 DW_DMA_MSIZE_16; \
Jamie Ilesf301c062011-01-21 14:11:53 +000049 \
Viresh Kumar327e6972012-02-01 16:12:26 +053050 (DWC_CTLL_DST_MSIZE(_dmsize) \
51 | DWC_CTLL_SRC_MSIZE(_smsize) \
Jamie Ilesf301c062011-01-21 14:11:53 +000052 | DWC_CTLL_LLP_D_EN \
53 | DWC_CTLL_LLP_S_EN \
Viresh Kumar327e6972012-02-01 16:12:26 +053054 | DWC_CTLL_DMS(_dms) \
55 | DWC_CTLL_SMS(_sms)); \
Jamie Ilesf301c062011-01-21 14:11:53 +000056 })
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070057
58/*
59 * This is configuration-dependent and usually a funny size like 4095.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070060 *
61 * Note that this is a transfer count, i.e. if we transfer 32-bit
Viresh Kumar418e7402011-03-04 15:42:50 +053062 * words, we can do 16380 bytes per descriptor.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070063 *
64 * This parameter is also system-specific.
65 */
Viresh Kumar418e7402011-03-04 15:42:50 +053066#define DWC_MAX_COUNT 4095U
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070067
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
Dan Williams41d5e592009-01-06 11:38:21 -070085static 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
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070094static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
95{
96 return list_entry(dwc->active_list.next, struct dw_desc, desc_node);
97}
98
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070099static 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;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530104 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700105
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530106 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700107 list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
Andy Shevchenko2ab37272012-06-19 13:34:04 +0300108 i++;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700109 if (async_tx_test_ack(&desc->txd)) {
110 list_del(&desc->desc_node);
111 ret = desc;
112 break;
113 }
Dan Williams41d5e592009-01-06 11:38:21 -0700114 dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700115 }
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530116 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700117
Dan Williams41d5e592009-01-06 11:38:21 -0700118 dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700119
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
Dan Williamse0bd0f82009-09-08 17:53:02 -0700127 list_for_each_entry(child, &desc->tx_list, desc_node)
Dan Williams41d5e592009-01-06 11:38:21 -0700128 dma_sync_single_for_cpu(chan2parent(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700129 child->txd.phys, sizeof(child->lli),
130 DMA_TO_DEVICE);
Dan Williams41d5e592009-01-06 11:38:21 -0700131 dma_sync_single_for_cpu(chan2parent(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700132 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{
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530142 unsigned long flags;
143
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700144 if (desc) {
145 struct dw_desc *child;
146
147 dwc_sync_desc_for_cpu(dwc, desc);
148
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530149 spin_lock_irqsave(&dwc->lock, flags);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700150 list_for_each_entry(child, &desc->tx_list, desc_node)
Dan Williams41d5e592009-01-06 11:38:21 -0700151 dev_vdbg(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700152 "moving child desc %p to freelist\n",
153 child);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700154 list_splice_init(&desc->tx_list, &dwc->free_list);
Dan Williams41d5e592009-01-06 11:38:21 -0700155 dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700156 list_add(&desc->desc_node, &dwc->free_list);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530157 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700158 }
159}
160
Viresh Kumar61e183f2011-11-17 16:01:29 +0530161static 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;
180 }
181
182 channel_writel(dwc, CFG_LO, cfglo);
183 channel_writel(dwc, CFG_HI, cfghi);
184
185 /* Enable interrupts */
186 channel_set_bit(dw, MASK.XFER, dwc->mask);
Viresh Kumar61e183f2011-11-17 16:01:29 +0530187 channel_set_bit(dw, MASK.ERROR, dwc->mask);
188
189 dwc->initialized = true;
190}
191
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700192/*----------------------------------------------------------------------*/
193
Andy Shevchenko4c2d56c2012-06-19 13:34:08 +0300194static inline unsigned int dwc_fast_fls(unsigned long long v)
195{
196 /*
197 * We can be a lot more clever here, but this should take care
198 * of the most common optimization.
199 */
200 if (!(v & 7))
201 return 3;
202 else if (!(v & 3))
203 return 2;
204 else if (!(v & 1))
205 return 1;
206 return 0;
207}
208
Andy Shevchenko1d455432012-06-19 13:34:03 +0300209static void dwc_dump_chan_regs(struct dw_dma_chan *dwc)
210{
211 dev_err(chan2dev(&dwc->chan),
212 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
213 channel_readl(dwc, SAR),
214 channel_readl(dwc, DAR),
215 channel_readl(dwc, LLP),
216 channel_readl(dwc, CTL_HI),
217 channel_readl(dwc, CTL_LO));
218}
219
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300220
221static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc)
222{
223 channel_clear_bit(dw, CH_EN, dwc->mask);
224 while (dma_readl(dw, CH_EN) & dwc->mask)
225 cpu_relax();
226}
227
Andy Shevchenko1d455432012-06-19 13:34:03 +0300228/*----------------------------------------------------------------------*/
229
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700230/* Called with dwc->lock held and bh disabled */
231static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
232{
233 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
234
235 /* ASSERT: channel is idle */
236 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700237 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700238 "BUG: Attempted to start non-idle channel\n");
Andy Shevchenko1d455432012-06-19 13:34:03 +0300239 dwc_dump_chan_regs(dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700240
241 /* The tasklet will hopefully advance the queue... */
242 return;
243 }
244
Viresh Kumar61e183f2011-11-17 16:01:29 +0530245 dwc_initialize(dwc);
246
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700247 channel_writel(dwc, LLP, first->txd.phys);
248 channel_writel(dwc, CTL_LO,
249 DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
250 channel_writel(dwc, CTL_HI, 0);
251 channel_set_bit(dw, CH_EN, dwc->mask);
252}
253
254/*----------------------------------------------------------------------*/
255
256static void
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530257dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
258 bool callback_required)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700259{
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530260 dma_async_tx_callback callback = NULL;
261 void *param = NULL;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700262 struct dma_async_tx_descriptor *txd = &desc->txd;
Viresh Kumare5180762011-03-03 15:47:20 +0530263 struct dw_desc *child;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530264 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700265
Dan Williams41d5e592009-01-06 11:38:21 -0700266 dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700267
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530268 spin_lock_irqsave(&dwc->lock, flags);
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +0000269 dma_cookie_complete(txd);
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530270 if (callback_required) {
271 callback = txd->callback;
272 param = txd->callback_param;
273 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700274
275 dwc_sync_desc_for_cpu(dwc, desc);
Viresh Kumare5180762011-03-03 15:47:20 +0530276
277 /* async_tx_ack */
278 list_for_each_entry(child, &desc->tx_list, desc_node)
279 async_tx_ack(&child->txd);
280 async_tx_ack(&desc->txd);
281
Dan Williamse0bd0f82009-09-08 17:53:02 -0700282 list_splice_init(&desc->tx_list, &dwc->free_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700283 list_move(&desc->desc_node, &dwc->free_list);
284
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700285 if (!dwc->chan.private) {
286 struct device *parent = chan2parent(&dwc->chan);
287 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
288 if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
289 dma_unmap_single(parent, desc->lli.dar,
290 desc->len, DMA_FROM_DEVICE);
291 else
292 dma_unmap_page(parent, desc->lli.dar,
293 desc->len, DMA_FROM_DEVICE);
294 }
295 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
296 if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
297 dma_unmap_single(parent, desc->lli.sar,
298 desc->len, DMA_TO_DEVICE);
299 else
300 dma_unmap_page(parent, desc->lli.sar,
301 desc->len, DMA_TO_DEVICE);
302 }
303 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700304
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530305 spin_unlock_irqrestore(&dwc->lock, flags);
306
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530307 if (callback_required && callback)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700308 callback(param);
309}
310
311static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
312{
313 struct dw_desc *desc, *_desc;
314 LIST_HEAD(list);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530315 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700316
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530317 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700318 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700319 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700320 "BUG: XFER bit set, but channel not idle!\n");
321
322 /* Try to continue after resetting the channel... */
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300323 dwc_chan_disable(dw, dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700324 }
325
326 /*
327 * Submit queued descriptors ASAP, i.e. before we go through
328 * the completed ones.
329 */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700330 list_splice_init(&dwc->active_list, &list);
Viresh Kumarf336e422011-03-03 15:47:16 +0530331 if (!list_empty(&dwc->queue)) {
332 list_move(dwc->queue.next, &dwc->active_list);
333 dwc_dostart(dwc, dwc_first_active(dwc));
334 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700335
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530336 spin_unlock_irqrestore(&dwc->lock, flags);
337
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700338 list_for_each_entry_safe(desc, _desc, &list, desc_node)
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530339 dwc_descriptor_complete(dwc, desc, true);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700340}
341
342static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
343{
344 dma_addr_t llp;
345 struct dw_desc *desc, *_desc;
346 struct dw_desc *child;
347 u32 status_xfer;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530348 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700349
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530350 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700351 llp = channel_readl(dwc, LLP);
352 status_xfer = dma_readl(dw, RAW.XFER);
353
354 if (status_xfer & dwc->mask) {
355 /* Everything we've submitted is done */
356 dma_writel(dw, CLEAR.XFER, dwc->mask);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530357 spin_unlock_irqrestore(&dwc->lock, flags);
358
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700359 dwc_complete_all(dw, dwc);
360 return;
361 }
362
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530363 if (list_empty(&dwc->active_list)) {
364 spin_unlock_irqrestore(&dwc->lock, flags);
Jamie Iles087809f2011-01-21 14:11:52 +0000365 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530366 }
Jamie Iles087809f2011-01-21 14:11:52 +0000367
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300368 dev_vdbg(chan2dev(&dwc->chan), "%s: llp=0x%llx\n", __func__,
Andy Shevchenko2f45d612012-06-19 13:34:02 +0300369 (unsigned long long)llp);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700370
371 list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
Viresh Kumar84adccf2011-03-24 11:32:15 +0530372 /* check first descriptors addr */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530373 if (desc->txd.phys == llp) {
374 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700375 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530376 }
Viresh Kumar84adccf2011-03-24 11:32:15 +0530377
378 /* check first descriptors llp */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530379 if (desc->lli.llp == llp) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700380 /* This one is currently in progress */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530381 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700382 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530383 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700384
Dan Williamse0bd0f82009-09-08 17:53:02 -0700385 list_for_each_entry(child, &desc->tx_list, desc_node)
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530386 if (child->lli.llp == llp) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700387 /* Currently in progress */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530388 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700389 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530390 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700391
392 /*
393 * No descriptors so far seem to be in progress, i.e.
394 * this one must be done.
395 */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530396 spin_unlock_irqrestore(&dwc->lock, flags);
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530397 dwc_descriptor_complete(dwc, desc, true);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530398 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700399 }
400
Dan Williams41d5e592009-01-06 11:38:21 -0700401 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700402 "BUG: All descriptors done, but channel not idle!\n");
403
404 /* Try to continue after resetting the channel... */
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300405 dwc_chan_disable(dw, dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700406
407 if (!list_empty(&dwc->queue)) {
Viresh Kumarf336e422011-03-03 15:47:16 +0530408 list_move(dwc->queue.next, &dwc->active_list);
409 dwc_dostart(dwc, dwc_first_active(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700410 }
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530411 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700412}
413
414static void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
415{
Dan Williams41d5e592009-01-06 11:38:21 -0700416 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Andy Shevchenko2f45d612012-06-19 13:34:02 +0300417 " desc: s0x%llx d0x%llx l0x%llx c0x%x:%x\n",
418 (unsigned long long)lli->sar,
419 (unsigned long long)lli->dar,
420 (unsigned long long)lli->llp,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700421 lli->ctlhi, lli->ctllo);
422}
423
424static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
425{
426 struct dw_desc *bad_desc;
427 struct dw_desc *child;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530428 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700429
430 dwc_scan_descriptors(dw, dwc);
431
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530432 spin_lock_irqsave(&dwc->lock, flags);
433
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700434 /*
435 * The descriptor currently at the head of the active list is
436 * borked. Since we don't have any way to report errors, we'll
437 * just have to scream loudly and try to carry on.
438 */
439 bad_desc = dwc_first_active(dwc);
440 list_del_init(&bad_desc->desc_node);
Viresh Kumarf336e422011-03-03 15:47:16 +0530441 list_move(dwc->queue.next, dwc->active_list.prev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700442
443 /* Clear the error flag and try to restart the controller */
444 dma_writel(dw, CLEAR.ERROR, dwc->mask);
445 if (!list_empty(&dwc->active_list))
446 dwc_dostart(dwc, dwc_first_active(dwc));
447
448 /*
449 * KERN_CRITICAL may seem harsh, but since this only happens
450 * when someone submits a bad physical address in a
451 * descriptor, we should consider ourselves lucky that the
452 * controller flagged an error instead of scribbling over
453 * random memory locations.
454 */
Dan Williams41d5e592009-01-06 11:38:21 -0700455 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700456 "Bad descriptor submitted for DMA!\n");
Dan Williams41d5e592009-01-06 11:38:21 -0700457 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700458 " cookie: %d\n", bad_desc->txd.cookie);
459 dwc_dump_lli(dwc, &bad_desc->lli);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700460 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700461 dwc_dump_lli(dwc, &child->lli);
462
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530463 spin_unlock_irqrestore(&dwc->lock, flags);
464
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700465 /* Pretend the descriptor completed successfully */
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530466 dwc_descriptor_complete(dwc, bad_desc, true);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700467}
468
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200469/* --------------------- Cyclic DMA API extensions -------------------- */
470
471inline dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
472{
473 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
474 return channel_readl(dwc, SAR);
475}
476EXPORT_SYMBOL(dw_dma_get_src_addr);
477
478inline dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
479{
480 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
481 return channel_readl(dwc, DAR);
482}
483EXPORT_SYMBOL(dw_dma_get_dst_addr);
484
485/* called with dwc->lock held and all DMAC interrupts disabled */
486static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530487 u32 status_err, u32 status_xfer)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200488{
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530489 unsigned long flags;
490
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530491 if (dwc->mask) {
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200492 void (*callback)(void *param);
493 void *callback_param;
494
495 dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
496 channel_readl(dwc, LLP));
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200497
498 callback = dwc->cdesc->period_callback;
499 callback_param = dwc->cdesc->period_callback_param;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530500
501 if (callback)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200502 callback(callback_param);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200503 }
504
505 /*
506 * Error and transfer complete are highly unlikely, and will most
507 * likely be due to a configuration error by the user.
508 */
509 if (unlikely(status_err & dwc->mask) ||
510 unlikely(status_xfer & dwc->mask)) {
511 int i;
512
513 dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s "
514 "interrupt, stopping DMA transfer\n",
515 status_xfer ? "xfer" : "error");
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530516
517 spin_lock_irqsave(&dwc->lock, flags);
518
Andy Shevchenko1d455432012-06-19 13:34:03 +0300519 dwc_dump_chan_regs(dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200520
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300521 dwc_chan_disable(dw, dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200522
523 /* make sure DMA does not restart by loading a new list */
524 channel_writel(dwc, LLP, 0);
525 channel_writel(dwc, CTL_LO, 0);
526 channel_writel(dwc, CTL_HI, 0);
527
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200528 dma_writel(dw, CLEAR.ERROR, dwc->mask);
529 dma_writel(dw, CLEAR.XFER, dwc->mask);
530
531 for (i = 0; i < dwc->cdesc->periods; i++)
532 dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530533
534 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200535 }
536}
537
538/* ------------------------------------------------------------------------- */
539
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700540static void dw_dma_tasklet(unsigned long data)
541{
542 struct dw_dma *dw = (struct dw_dma *)data;
543 struct dw_dma_chan *dwc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700544 u32 status_xfer;
545 u32 status_err;
546 int i;
547
Haavard Skinnemoen7fe7b2f2008-10-03 15:23:46 -0700548 status_xfer = dma_readl(dw, RAW.XFER);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700549 status_err = dma_readl(dw, RAW.ERROR);
550
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300551 dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700552
553 for (i = 0; i < dw->dma.chancnt; i++) {
554 dwc = &dw->chan[i];
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200555 if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530556 dwc_handle_cyclic(dw, dwc, status_err, status_xfer);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200557 else if (status_err & (1 << i))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700558 dwc_handle_error(dw, dwc);
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530559 else if (status_xfer & (1 << i))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700560 dwc_scan_descriptors(dw, dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700561 }
562
563 /*
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530564 * Re-enable interrupts.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700565 */
566 channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700567 channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
568}
569
570static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
571{
572 struct dw_dma *dw = dev_id;
573 u32 status;
574
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300575 dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700576 dma_readl(dw, STATUS_INT));
577
578 /*
579 * Just disable the interrupts. We'll turn them back on in the
580 * softirq handler.
581 */
582 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700583 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
584
585 status = dma_readl(dw, STATUS_INT);
586 if (status) {
587 dev_err(dw->dma.dev,
588 "BUG: Unexpected interrupts pending: 0x%x\n",
589 status);
590
591 /* Try to recover */
592 channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700593 channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
594 channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
595 channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
596 }
597
598 tasklet_schedule(&dw->tasklet);
599
600 return IRQ_HANDLED;
601}
602
603/*----------------------------------------------------------------------*/
604
605static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
606{
607 struct dw_desc *desc = txd_to_dw_desc(tx);
608 struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
609 dma_cookie_t cookie;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530610 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700611
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530612 spin_lock_irqsave(&dwc->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000613 cookie = dma_cookie_assign(tx);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700614
615 /*
616 * REVISIT: We should attempt to chain as many descriptors as
617 * possible, perhaps even appending to those already submitted
618 * for DMA. But this is hard to do in a race-free manner.
619 */
620 if (list_empty(&dwc->active_list)) {
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300621 dev_vdbg(chan2dev(tx->chan), "%s: started %u\n", __func__,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700622 desc->txd.cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700623 list_add_tail(&desc->desc_node, &dwc->active_list);
Viresh Kumarf336e422011-03-03 15:47:16 +0530624 dwc_dostart(dwc, dwc_first_active(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700625 } else {
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300626 dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n", __func__,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700627 desc->txd.cookie);
628
629 list_add_tail(&desc->desc_node, &dwc->queue);
630 }
631
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530632 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700633
634 return cookie;
635}
636
637static struct dma_async_tx_descriptor *
638dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
639 size_t len, unsigned long flags)
640{
641 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
642 struct dw_desc *desc;
643 struct dw_desc *first;
644 struct dw_desc *prev;
645 size_t xfer_count;
646 size_t offset;
647 unsigned int src_width;
648 unsigned int dst_width;
649 u32 ctllo;
650
Andy Shevchenko2f45d612012-06-19 13:34:02 +0300651 dev_vdbg(chan2dev(chan),
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300652 "%s: d0x%llx s0x%llx l0x%zx f0x%lx\n", __func__,
Andy Shevchenko2f45d612012-06-19 13:34:02 +0300653 (unsigned long long)dest, (unsigned long long)src,
654 len, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700655
656 if (unlikely(!len)) {
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300657 dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700658 return NULL;
659 }
660
Andy Shevchenko4c2d56c2012-06-19 13:34:08 +0300661 src_width = dst_width = dwc_fast_fls(src | dest | len);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700662
Viresh Kumar327e6972012-02-01 16:12:26 +0530663 ctllo = DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700664 | DWC_CTLL_DST_WIDTH(dst_width)
665 | DWC_CTLL_SRC_WIDTH(src_width)
666 | DWC_CTLL_DST_INC
667 | DWC_CTLL_SRC_INC
668 | DWC_CTLL_FC_M2M;
669 prev = first = NULL;
670
671 for (offset = 0; offset < len; offset += xfer_count << src_width) {
672 xfer_count = min_t(size_t, (len - offset) >> src_width,
673 DWC_MAX_COUNT);
674
675 desc = dwc_desc_get(dwc);
676 if (!desc)
677 goto err_desc_get;
678
679 desc->lli.sar = src + offset;
680 desc->lli.dar = dest + offset;
681 desc->lli.ctllo = ctllo;
682 desc->lli.ctlhi = xfer_count;
683
684 if (!first) {
685 first = desc;
686 } else {
687 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700688 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700689 prev->txd.phys, sizeof(prev->lli),
690 DMA_TO_DEVICE);
691 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700692 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700693 }
694 prev = desc;
695 }
696
697
698 if (flags & DMA_PREP_INTERRUPT)
699 /* Trigger interrupt after last block */
700 prev->lli.ctllo |= DWC_CTLL_INT_EN;
701
702 prev->lli.llp = 0;
Dan Williams41d5e592009-01-06 11:38:21 -0700703 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700704 prev->txd.phys, sizeof(prev->lli),
705 DMA_TO_DEVICE);
706
707 first->txd.flags = flags;
708 first->len = len;
709
710 return &first->txd;
711
712err_desc_get:
713 dwc_desc_put(dwc, first);
714 return NULL;
715}
716
717static struct dma_async_tx_descriptor *
718dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530719 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500720 unsigned long flags, void *context)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700721{
722 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Dan Williams287d8592009-02-18 14:48:26 -0800723 struct dw_dma_slave *dws = chan->private;
Viresh Kumar327e6972012-02-01 16:12:26 +0530724 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700725 struct dw_desc *prev;
726 struct dw_desc *first;
727 u32 ctllo;
728 dma_addr_t reg;
729 unsigned int reg_width;
730 unsigned int mem_width;
731 unsigned int i;
732 struct scatterlist *sg;
733 size_t total_len = 0;
734
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300735 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700736
737 if (unlikely(!dws || !sg_len))
738 return NULL;
739
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700740 prev = first = NULL;
741
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700742 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530743 case DMA_MEM_TO_DEV:
Viresh Kumar327e6972012-02-01 16:12:26 +0530744 reg_width = __fls(sconfig->dst_addr_width);
745 reg = sconfig->dst_addr;
746 ctllo = (DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700747 | DWC_CTLL_DST_WIDTH(reg_width)
748 | DWC_CTLL_DST_FIX
Viresh Kumar327e6972012-02-01 16:12:26 +0530749 | DWC_CTLL_SRC_INC);
750
751 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
752 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
753
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700754 for_each_sg(sgl, sg, sg_len, i) {
755 struct dw_desc *desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530756 u32 len, dlen, mem;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700757
Lars-Peter Clausencbb796c2012-04-25 20:50:51 +0200758 mem = sg_dma_address(sg);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700759 len = sg_dma_len(sg);
Viresh Kumar6bc711f2012-02-01 16:12:25 +0530760
Andy Shevchenko4c2d56c2012-06-19 13:34:08 +0300761 mem_width = dwc_fast_fls(mem | len);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700762
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530763slave_sg_todev_fill_desc:
764 desc = dwc_desc_get(dwc);
765 if (!desc) {
766 dev_err(chan2dev(chan),
767 "not enough descriptors available\n");
768 goto err_desc_get;
769 }
770
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700771 desc->lli.sar = mem;
772 desc->lli.dar = reg;
773 desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530774 if ((len >> mem_width) > DWC_MAX_COUNT) {
775 dlen = DWC_MAX_COUNT << mem_width;
776 mem += dlen;
777 len -= dlen;
778 } else {
779 dlen = len;
780 len = 0;
781 }
782
783 desc->lli.ctlhi = dlen >> mem_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700784
785 if (!first) {
786 first = desc;
787 } else {
788 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700789 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700790 prev->txd.phys,
791 sizeof(prev->lli),
792 DMA_TO_DEVICE);
793 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700794 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700795 }
796 prev = desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530797 total_len += dlen;
798
799 if (len)
800 goto slave_sg_todev_fill_desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700801 }
802 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530803 case DMA_DEV_TO_MEM:
Viresh Kumar327e6972012-02-01 16:12:26 +0530804 reg_width = __fls(sconfig->src_addr_width);
805 reg = sconfig->src_addr;
806 ctllo = (DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700807 | DWC_CTLL_SRC_WIDTH(reg_width)
808 | DWC_CTLL_DST_INC
Viresh Kumar327e6972012-02-01 16:12:26 +0530809 | DWC_CTLL_SRC_FIX);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700810
Viresh Kumar327e6972012-02-01 16:12:26 +0530811 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
812 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
813
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700814 for_each_sg(sgl, sg, sg_len, i) {
815 struct dw_desc *desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530816 u32 len, dlen, mem;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700817
Lars-Peter Clausencbb796c2012-04-25 20:50:51 +0200818 mem = sg_dma_address(sg);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700819 len = sg_dma_len(sg);
Viresh Kumar6bc711f2012-02-01 16:12:25 +0530820
Andy Shevchenko4c2d56c2012-06-19 13:34:08 +0300821 mem_width = dwc_fast_fls(mem | len);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700822
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530823slave_sg_fromdev_fill_desc:
824 desc = dwc_desc_get(dwc);
825 if (!desc) {
826 dev_err(chan2dev(chan),
827 "not enough descriptors available\n");
828 goto err_desc_get;
829 }
830
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700831 desc->lli.sar = reg;
832 desc->lli.dar = mem;
833 desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530834 if ((len >> reg_width) > DWC_MAX_COUNT) {
835 dlen = DWC_MAX_COUNT << reg_width;
836 mem += dlen;
837 len -= dlen;
838 } else {
839 dlen = len;
840 len = 0;
841 }
842 desc->lli.ctlhi = dlen >> reg_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700843
844 if (!first) {
845 first = desc;
846 } else {
847 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700848 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700849 prev->txd.phys,
850 sizeof(prev->lli),
851 DMA_TO_DEVICE);
852 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700853 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700854 }
855 prev = desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530856 total_len += dlen;
857
858 if (len)
859 goto slave_sg_fromdev_fill_desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700860 }
861 break;
862 default:
863 return NULL;
864 }
865
866 if (flags & DMA_PREP_INTERRUPT)
867 /* Trigger interrupt after last block */
868 prev->lli.ctllo |= DWC_CTLL_INT_EN;
869
870 prev->lli.llp = 0;
Dan Williams41d5e592009-01-06 11:38:21 -0700871 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700872 prev->txd.phys, sizeof(prev->lli),
873 DMA_TO_DEVICE);
874
875 first->len = total_len;
876
877 return &first->txd;
878
879err_desc_get:
880 dwc_desc_put(dwc, first);
881 return NULL;
882}
883
Viresh Kumar327e6972012-02-01 16:12:26 +0530884/*
885 * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
886 * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
887 *
888 * NOTE: burst size 2 is not supported by controller.
889 *
890 * This can be done by finding least significant bit set: n & (n - 1)
891 */
892static inline void convert_burst(u32 *maxburst)
893{
894 if (*maxburst > 1)
895 *maxburst = fls(*maxburst) - 2;
896 else
897 *maxburst = 0;
898}
899
900static int
901set_runtime_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
902{
903 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
904
905 /* Check if it is chan is configured for slave transfers */
906 if (!chan->private)
907 return -EINVAL;
908
909 memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
910
911 convert_burst(&dwc->dma_sconfig.src_maxburst);
912 convert_burst(&dwc->dma_sconfig.dst_maxburst);
913
914 return 0;
915}
916
Linus Walleij05827632010-05-17 16:30:42 -0700917static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
918 unsigned long arg)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700919{
920 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
921 struct dw_dma *dw = to_dw_dma(chan->device);
922 struct dw_desc *desc, *_desc;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530923 unsigned long flags;
Linus Walleija7c57cf2011-04-19 08:31:32 +0800924 u32 cfglo;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700925 LIST_HEAD(list);
926
Linus Walleija7c57cf2011-04-19 08:31:32 +0800927 if (cmd == DMA_PAUSE) {
928 spin_lock_irqsave(&dwc->lock, flags);
929
930 cfglo = channel_readl(dwc, CFG_LO);
931 channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
932 while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY))
933 cpu_relax();
934
935 dwc->paused = true;
936 spin_unlock_irqrestore(&dwc->lock, flags);
937 } else if (cmd == DMA_RESUME) {
938 if (!dwc->paused)
939 return 0;
940
941 spin_lock_irqsave(&dwc->lock, flags);
942
943 cfglo = channel_readl(dwc, CFG_LO);
944 channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
945 dwc->paused = false;
946
947 spin_unlock_irqrestore(&dwc->lock, flags);
948 } else if (cmd == DMA_TERMINATE_ALL) {
949 spin_lock_irqsave(&dwc->lock, flags);
950
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300951 dwc_chan_disable(dw, dwc);
Linus Walleija7c57cf2011-04-19 08:31:32 +0800952
953 dwc->paused = false;
954
955 /* active_list entries will end up before queued entries */
956 list_splice_init(&dwc->queue, &list);
957 list_splice_init(&dwc->active_list, &list);
958
959 spin_unlock_irqrestore(&dwc->lock, flags);
960
961 /* Flush all pending and queued descriptors */
962 list_for_each_entry_safe(desc, _desc, &list, desc_node)
963 dwc_descriptor_complete(dwc, desc, false);
Viresh Kumar327e6972012-02-01 16:12:26 +0530964 } else if (cmd == DMA_SLAVE_CONFIG) {
965 return set_runtime_config(chan, (struct dma_slave_config *)arg);
966 } else {
Linus Walleijc3635c72010-03-26 16:44:01 -0700967 return -ENXIO;
Viresh Kumar327e6972012-02-01 16:12:26 +0530968 }
Linus Walleijc3635c72010-03-26 16:44:01 -0700969
Linus Walleijc3635c72010-03-26 16:44:01 -0700970 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700971}
972
973static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -0700974dwc_tx_status(struct dma_chan *chan,
975 dma_cookie_t cookie,
976 struct dma_tx_state *txstate)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700977{
978 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000979 enum dma_status ret;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700980
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000981 ret = dma_cookie_status(chan, cookie, txstate);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700982 if (ret != DMA_SUCCESS) {
983 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
984
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000985 ret = dma_cookie_status(chan, cookie, txstate);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700986 }
987
Viresh Kumarabf53902011-04-15 16:03:35 +0530988 if (ret != DMA_SUCCESS)
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000989 dma_set_residue(txstate, dwc_first_active(dwc)->len);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700990
Linus Walleija7c57cf2011-04-19 08:31:32 +0800991 if (dwc->paused)
992 return DMA_PAUSED;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700993
994 return ret;
995}
996
997static void dwc_issue_pending(struct dma_chan *chan)
998{
999 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1000
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001001 if (!list_empty(&dwc->queue))
1002 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001003}
1004
Dan Williamsaa1e6f12009-01-06 11:38:17 -07001005static int dwc_alloc_chan_resources(struct dma_chan *chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001006{
1007 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1008 struct dw_dma *dw = to_dw_dma(chan->device);
1009 struct dw_desc *desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001010 int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301011 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001012
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001013 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001014
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001015 /* ASSERT: channel is idle */
1016 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -07001017 dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001018 return -EIO;
1019 }
1020
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001021 dma_cookie_init(chan);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001022
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001023 /*
1024 * NOTE: some controllers may have additional features that we
1025 * need to initialize here, like "scatter-gather" (which
1026 * doesn't mean what you think it means), and status writeback.
1027 */
1028
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301029 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001030 i = dwc->descs_allocated;
1031 while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301032 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001033
1034 desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL);
1035 if (!desc) {
Dan Williams41d5e592009-01-06 11:38:21 -07001036 dev_info(chan2dev(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001037 "only allocated %d descriptors\n", i);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301038 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001039 break;
1040 }
1041
Dan Williamse0bd0f82009-09-08 17:53:02 -07001042 INIT_LIST_HEAD(&desc->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001043 dma_async_tx_descriptor_init(&desc->txd, chan);
1044 desc->txd.tx_submit = dwc_tx_submit;
1045 desc->txd.flags = DMA_CTRL_ACK;
Dan Williams41d5e592009-01-06 11:38:21 -07001046 desc->txd.phys = dma_map_single(chan2parent(chan), &desc->lli,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001047 sizeof(desc->lli), DMA_TO_DEVICE);
1048 dwc_desc_put(dwc, desc);
1049
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301050 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001051 i = ++dwc->descs_allocated;
1052 }
1053
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301054 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001055
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001056 dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001057
1058 return i;
1059}
1060
1061static void dwc_free_chan_resources(struct dma_chan *chan)
1062{
1063 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1064 struct dw_dma *dw = to_dw_dma(chan->device);
1065 struct dw_desc *desc, *_desc;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301066 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001067 LIST_HEAD(list);
1068
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001069 dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001070 dwc->descs_allocated);
1071
1072 /* ASSERT: channel is idle */
1073 BUG_ON(!list_empty(&dwc->active_list));
1074 BUG_ON(!list_empty(&dwc->queue));
1075 BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
1076
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301077 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001078 list_splice_init(&dwc->free_list, &list);
1079 dwc->descs_allocated = 0;
Viresh Kumar61e183f2011-11-17 16:01:29 +05301080 dwc->initialized = false;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001081
1082 /* Disable interrupts */
1083 channel_clear_bit(dw, MASK.XFER, dwc->mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001084 channel_clear_bit(dw, MASK.ERROR, dwc->mask);
1085
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301086 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001087
1088 list_for_each_entry_safe(desc, _desc, &list, desc_node) {
Dan Williams41d5e592009-01-06 11:38:21 -07001089 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1090 dma_unmap_single(chan2parent(chan), desc->txd.phys,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001091 sizeof(desc->lli), DMA_TO_DEVICE);
1092 kfree(desc);
1093 }
1094
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001095 dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001096}
1097
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001098/* --------------------- Cyclic DMA API extensions -------------------- */
1099
1100/**
1101 * dw_dma_cyclic_start - start the cyclic DMA transfer
1102 * @chan: the DMA channel to start
1103 *
1104 * Must be called with soft interrupts disabled. Returns zero on success or
1105 * -errno on failure.
1106 */
1107int dw_dma_cyclic_start(struct dma_chan *chan)
1108{
1109 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1110 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301111 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001112
1113 if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
1114 dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
1115 return -ENODEV;
1116 }
1117
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301118 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001119
1120 /* assert channel is idle */
1121 if (dma_readl(dw, CH_EN) & dwc->mask) {
1122 dev_err(chan2dev(&dwc->chan),
1123 "BUG: Attempted to start non-idle channel\n");
Andy Shevchenko1d455432012-06-19 13:34:03 +03001124 dwc_dump_chan_regs(dwc);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301125 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001126 return -EBUSY;
1127 }
1128
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001129 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1130 dma_writel(dw, CLEAR.XFER, dwc->mask);
1131
1132 /* setup DMAC channel registers */
1133 channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
1134 channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
1135 channel_writel(dwc, CTL_HI, 0);
1136
1137 channel_set_bit(dw, CH_EN, dwc->mask);
1138
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301139 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001140
1141 return 0;
1142}
1143EXPORT_SYMBOL(dw_dma_cyclic_start);
1144
1145/**
1146 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1147 * @chan: the DMA channel to stop
1148 *
1149 * Must be called with soft interrupts disabled.
1150 */
1151void dw_dma_cyclic_stop(struct dma_chan *chan)
1152{
1153 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1154 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301155 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001156
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301157 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001158
Andy Shevchenko3f9362072012-06-19 13:46:32 +03001159 dwc_chan_disable(dw, dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001160
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301161 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001162}
1163EXPORT_SYMBOL(dw_dma_cyclic_stop);
1164
1165/**
1166 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1167 * @chan: the DMA channel to prepare
1168 * @buf_addr: physical DMA address where the buffer starts
1169 * @buf_len: total number of bytes for the entire buffer
1170 * @period_len: number of bytes for each period
1171 * @direction: transfer direction, to or from device
1172 *
1173 * Must be called before trying to start the transfer. Returns a valid struct
1174 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1175 */
1176struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
1177 dma_addr_t buf_addr, size_t buf_len, size_t period_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05301178 enum dma_transfer_direction direction)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001179{
1180 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Viresh Kumar327e6972012-02-01 16:12:26 +05301181 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001182 struct dw_cyclic_desc *cdesc;
1183 struct dw_cyclic_desc *retval = NULL;
1184 struct dw_desc *desc;
1185 struct dw_desc *last = NULL;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001186 unsigned long was_cyclic;
1187 unsigned int reg_width;
1188 unsigned int periods;
1189 unsigned int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301190 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001191
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301192 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001193 if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301194 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001195 dev_dbg(chan2dev(&dwc->chan),
1196 "queue and/or active list are not empty\n");
1197 return ERR_PTR(-EBUSY);
1198 }
1199
1200 was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301201 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001202 if (was_cyclic) {
1203 dev_dbg(chan2dev(&dwc->chan),
1204 "channel already prepared for cyclic DMA\n");
1205 return ERR_PTR(-EBUSY);
1206 }
1207
1208 retval = ERR_PTR(-EINVAL);
Viresh Kumar327e6972012-02-01 16:12:26 +05301209
1210 if (direction == DMA_MEM_TO_DEV)
1211 reg_width = __ffs(sconfig->dst_addr_width);
1212 else
1213 reg_width = __ffs(sconfig->src_addr_width);
1214
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001215 periods = buf_len / period_len;
1216
1217 /* Check for too big/unaligned periods and unaligned DMA buffer. */
1218 if (period_len > (DWC_MAX_COUNT << reg_width))
1219 goto out_err;
1220 if (unlikely(period_len & ((1 << reg_width) - 1)))
1221 goto out_err;
1222 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
1223 goto out_err;
Vinod Kouldb8196d2011-10-13 22:34:23 +05301224 if (unlikely(!(direction & (DMA_MEM_TO_DEV | DMA_DEV_TO_MEM))))
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001225 goto out_err;
1226
1227 retval = ERR_PTR(-ENOMEM);
1228
1229 if (periods > NR_DESCS_PER_CHANNEL)
1230 goto out_err;
1231
1232 cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
1233 if (!cdesc)
1234 goto out_err;
1235
1236 cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
1237 if (!cdesc->desc)
1238 goto out_err_alloc;
1239
1240 for (i = 0; i < periods; i++) {
1241 desc = dwc_desc_get(dwc);
1242 if (!desc)
1243 goto out_err_desc_get;
1244
1245 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +05301246 case DMA_MEM_TO_DEV:
Viresh Kumar327e6972012-02-01 16:12:26 +05301247 desc->lli.dar = sconfig->dst_addr;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001248 desc->lli.sar = buf_addr + (period_len * i);
Viresh Kumar327e6972012-02-01 16:12:26 +05301249 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001250 | DWC_CTLL_DST_WIDTH(reg_width)
1251 | DWC_CTLL_SRC_WIDTH(reg_width)
1252 | DWC_CTLL_DST_FIX
1253 | DWC_CTLL_SRC_INC
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001254 | DWC_CTLL_INT_EN);
Viresh Kumar327e6972012-02-01 16:12:26 +05301255
1256 desc->lli.ctllo |= sconfig->device_fc ?
1257 DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
1258 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
1259
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001260 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +05301261 case DMA_DEV_TO_MEM:
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001262 desc->lli.dar = buf_addr + (period_len * i);
Viresh Kumar327e6972012-02-01 16:12:26 +05301263 desc->lli.sar = sconfig->src_addr;
1264 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001265 | DWC_CTLL_SRC_WIDTH(reg_width)
1266 | DWC_CTLL_DST_WIDTH(reg_width)
1267 | DWC_CTLL_DST_INC
1268 | DWC_CTLL_SRC_FIX
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001269 | DWC_CTLL_INT_EN);
Viresh Kumar327e6972012-02-01 16:12:26 +05301270
1271 desc->lli.ctllo |= sconfig->device_fc ?
1272 DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
1273 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
1274
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001275 break;
1276 default:
1277 break;
1278 }
1279
1280 desc->lli.ctlhi = (period_len >> reg_width);
1281 cdesc->desc[i] = desc;
1282
1283 if (last) {
1284 last->lli.llp = desc->txd.phys;
1285 dma_sync_single_for_device(chan2parent(chan),
1286 last->txd.phys, sizeof(last->lli),
1287 DMA_TO_DEVICE);
1288 }
1289
1290 last = desc;
1291 }
1292
1293 /* lets make a cyclic list */
1294 last->lli.llp = cdesc->desc[0]->txd.phys;
1295 dma_sync_single_for_device(chan2parent(chan), last->txd.phys,
1296 sizeof(last->lli), DMA_TO_DEVICE);
1297
Andy Shevchenko2f45d612012-06-19 13:34:02 +03001298 dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%llx len %zu "
1299 "period %zu periods %d\n", (unsigned long long)buf_addr,
1300 buf_len, period_len, periods);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001301
1302 cdesc->periods = periods;
1303 dwc->cdesc = cdesc;
1304
1305 return cdesc;
1306
1307out_err_desc_get:
1308 while (i--)
1309 dwc_desc_put(dwc, cdesc->desc[i]);
1310out_err_alloc:
1311 kfree(cdesc);
1312out_err:
1313 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1314 return (struct dw_cyclic_desc *)retval;
1315}
1316EXPORT_SYMBOL(dw_dma_cyclic_prep);
1317
1318/**
1319 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1320 * @chan: the DMA channel to free
1321 */
1322void dw_dma_cyclic_free(struct dma_chan *chan)
1323{
1324 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1325 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1326 struct dw_cyclic_desc *cdesc = dwc->cdesc;
1327 int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301328 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001329
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001330 dev_dbg(chan2dev(&dwc->chan), "%s\n", __func__);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001331
1332 if (!cdesc)
1333 return;
1334
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301335 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001336
Andy Shevchenko3f9362072012-06-19 13:46:32 +03001337 dwc_chan_disable(dw, dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001338
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001339 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1340 dma_writel(dw, CLEAR.XFER, dwc->mask);
1341
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301342 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001343
1344 for (i = 0; i < cdesc->periods; i++)
1345 dwc_desc_put(dwc, cdesc->desc[i]);
1346
1347 kfree(cdesc->desc);
1348 kfree(cdesc);
1349
1350 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1351}
1352EXPORT_SYMBOL(dw_dma_cyclic_free);
1353
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001354/*----------------------------------------------------------------------*/
1355
1356static void dw_dma_off(struct dw_dma *dw)
1357{
Viresh Kumar61e183f2011-11-17 16:01:29 +05301358 int i;
1359
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001360 dma_writel(dw, CFG, 0);
1361
1362 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001363 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1364 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1365 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1366
1367 while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
1368 cpu_relax();
Viresh Kumar61e183f2011-11-17 16:01:29 +05301369
1370 for (i = 0; i < dw->dma.chancnt; i++)
1371 dw->chan[i].initialized = false;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001372}
1373
Andy Shevchenko0272e932012-06-19 13:34:09 +03001374static int __devinit dw_probe(struct platform_device *pdev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001375{
1376 struct dw_dma_platform_data *pdata;
1377 struct resource *io;
1378 struct dw_dma *dw;
1379 size_t size;
1380 int irq;
1381 int err;
1382 int i;
1383
Viresh Kumar6c618c92012-02-01 16:12:22 +05301384 pdata = dev_get_platdata(&pdev->dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001385 if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
1386 return -EINVAL;
1387
1388 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1389 if (!io)
1390 return -EINVAL;
1391
1392 irq = platform_get_irq(pdev, 0);
1393 if (irq < 0)
1394 return irq;
1395
1396 size = sizeof(struct dw_dma);
1397 size += pdata->nr_channels * sizeof(struct dw_dma_chan);
1398 dw = kzalloc(size, GFP_KERNEL);
1399 if (!dw)
1400 return -ENOMEM;
1401
1402 if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) {
1403 err = -EBUSY;
1404 goto err_kfree;
1405 }
1406
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001407 dw->regs = ioremap(io->start, DW_REGLEN);
1408 if (!dw->regs) {
1409 err = -ENOMEM;
1410 goto err_release_r;
1411 }
1412
1413 dw->clk = clk_get(&pdev->dev, "hclk");
1414 if (IS_ERR(dw->clk)) {
1415 err = PTR_ERR(dw->clk);
1416 goto err_clk;
1417 }
Viresh Kumar30755282012-04-17 17:10:07 +05301418 clk_prepare_enable(dw->clk);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001419
Andy Shevchenko11f932e2012-06-19 13:34:06 +03001420 /* Calculate all channel mask before DMA setup */
1421 dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
1422
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001423 /* force dma off, just in case */
1424 dw_dma_off(dw);
1425
Andy Shevchenko236b1062012-06-19 13:34:07 +03001426 /* disable BLOCK interrupts as well */
1427 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
1428
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001429 err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw);
1430 if (err)
1431 goto err_irq;
1432
1433 platform_set_drvdata(pdev, dw);
1434
1435 tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
1436
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001437 INIT_LIST_HEAD(&dw->dma.channels);
Barry Song463894702011-09-15 03:06:30 -07001438 for (i = 0; i < pdata->nr_channels; i++) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001439 struct dw_dma_chan *dwc = &dw->chan[i];
1440
1441 dwc->chan.device = &dw->dma;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001442 dma_cookie_init(&dwc->chan);
Viresh Kumarb0c31302011-03-03 15:47:21 +05301443 if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
1444 list_add_tail(&dwc->chan.device_node,
1445 &dw->dma.channels);
1446 else
1447 list_add(&dwc->chan.device_node, &dw->dma.channels);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001448
Viresh Kumar93317e82011-03-03 15:47:22 +05301449 /* 7 is highest priority & 0 is lowest. */
1450 if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
Viresh Kumare8d9f872012-02-01 16:12:21 +05301451 dwc->priority = pdata->nr_channels - i - 1;
Viresh Kumar93317e82011-03-03 15:47:22 +05301452 else
1453 dwc->priority = i;
1454
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001455 dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
1456 spin_lock_init(&dwc->lock);
1457 dwc->mask = 1 << i;
1458
1459 INIT_LIST_HEAD(&dwc->active_list);
1460 INIT_LIST_HEAD(&dwc->queue);
1461 INIT_LIST_HEAD(&dwc->free_list);
1462
1463 channel_clear_bit(dw, CH_EN, dwc->mask);
1464 }
1465
Andy Shevchenko11f932e2012-06-19 13:34:06 +03001466 /* Clear all interrupts on all channels. */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001467 dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
Andy Shevchenko236b1062012-06-19 13:34:07 +03001468 dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001469 dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
1470 dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
1471 dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
1472
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001473 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
1474 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
Jamie Iles95ea7592011-01-21 14:11:54 +00001475 if (pdata->is_private)
1476 dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001477 dw->dma.dev = &pdev->dev;
1478 dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
1479 dw->dma.device_free_chan_resources = dwc_free_chan_resources;
1480
1481 dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
1482
1483 dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
Linus Walleijc3635c72010-03-26 16:44:01 -07001484 dw->dma.device_control = dwc_control;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001485
Linus Walleij07934482010-03-26 16:50:49 -07001486 dw->dma.device_tx_status = dwc_tx_status;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001487 dw->dma.device_issue_pending = dwc_issue_pending;
1488
1489 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1490
1491 printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n",
Barry Song463894702011-09-15 03:06:30 -07001492 dev_name(&pdev->dev), pdata->nr_channels);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001493
1494 dma_async_device_register(&dw->dma);
1495
1496 return 0;
1497
1498err_irq:
Viresh Kumar30755282012-04-17 17:10:07 +05301499 clk_disable_unprepare(dw->clk);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001500 clk_put(dw->clk);
1501err_clk:
1502 iounmap(dw->regs);
1503 dw->regs = NULL;
1504err_release_r:
1505 release_resource(io);
1506err_kfree:
1507 kfree(dw);
1508 return err;
1509}
1510
Andy Shevchenko0272e932012-06-19 13:34:09 +03001511static int __devexit dw_remove(struct platform_device *pdev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001512{
1513 struct dw_dma *dw = platform_get_drvdata(pdev);
1514 struct dw_dma_chan *dwc, *_dwc;
1515 struct resource *io;
1516
1517 dw_dma_off(dw);
1518 dma_async_device_unregister(&dw->dma);
1519
1520 free_irq(platform_get_irq(pdev, 0), dw);
1521 tasklet_kill(&dw->tasklet);
1522
1523 list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
1524 chan.device_node) {
1525 list_del(&dwc->chan.device_node);
1526 channel_clear_bit(dw, CH_EN, dwc->mask);
1527 }
1528
Viresh Kumar30755282012-04-17 17:10:07 +05301529 clk_disable_unprepare(dw->clk);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001530 clk_put(dw->clk);
1531
1532 iounmap(dw->regs);
1533 dw->regs = NULL;
1534
1535 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1536 release_mem_region(io->start, DW_REGLEN);
1537
1538 kfree(dw);
1539
1540 return 0;
1541}
1542
1543static void dw_shutdown(struct platform_device *pdev)
1544{
1545 struct dw_dma *dw = platform_get_drvdata(pdev);
1546
1547 dw_dma_off(platform_get_drvdata(pdev));
Viresh Kumar30755282012-04-17 17:10:07 +05301548 clk_disable_unprepare(dw->clk);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001549}
1550
Magnus Damm4a256b52009-07-08 13:22:18 +02001551static int dw_suspend_noirq(struct device *dev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001552{
Magnus Damm4a256b52009-07-08 13:22:18 +02001553 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001554 struct dw_dma *dw = platform_get_drvdata(pdev);
1555
1556 dw_dma_off(platform_get_drvdata(pdev));
Viresh Kumar30755282012-04-17 17:10:07 +05301557 clk_disable_unprepare(dw->clk);
Viresh Kumar61e183f2011-11-17 16:01:29 +05301558
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001559 return 0;
1560}
1561
Magnus Damm4a256b52009-07-08 13:22:18 +02001562static int dw_resume_noirq(struct device *dev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001563{
Magnus Damm4a256b52009-07-08 13:22:18 +02001564 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001565 struct dw_dma *dw = platform_get_drvdata(pdev);
1566
Viresh Kumar30755282012-04-17 17:10:07 +05301567 clk_prepare_enable(dw->clk);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001568 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1569 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001570}
1571
Alexey Dobriyan47145212009-12-14 18:00:08 -08001572static const struct dev_pm_ops dw_dev_pm_ops = {
Magnus Damm4a256b52009-07-08 13:22:18 +02001573 .suspend_noirq = dw_suspend_noirq,
1574 .resume_noirq = dw_resume_noirq,
Rajeev KUMAR7414a1b2012-02-01 16:12:17 +05301575 .freeze_noirq = dw_suspend_noirq,
1576 .thaw_noirq = dw_resume_noirq,
1577 .restore_noirq = dw_resume_noirq,
1578 .poweroff_noirq = dw_suspend_noirq,
Magnus Damm4a256b52009-07-08 13:22:18 +02001579};
1580
Viresh Kumard3f797d2012-04-20 20:15:34 +05301581#ifdef CONFIG_OF
1582static const struct of_device_id dw_dma_id_table[] = {
1583 { .compatible = "snps,dma-spear1340" },
1584 {}
1585};
1586MODULE_DEVICE_TABLE(of, dw_dma_id_table);
1587#endif
1588
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001589static struct platform_driver dw_driver = {
Andy Shevchenko0272e932012-06-19 13:34:09 +03001590 .remove = __devexit_p(dw_remove),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001591 .shutdown = dw_shutdown,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001592 .driver = {
1593 .name = "dw_dmac",
Magnus Damm4a256b52009-07-08 13:22:18 +02001594 .pm = &dw_dev_pm_ops,
Viresh Kumard3f797d2012-04-20 20:15:34 +05301595 .of_match_table = of_match_ptr(dw_dma_id_table),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001596 },
1597};
1598
1599static int __init dw_init(void)
1600{
1601 return platform_driver_probe(&dw_driver, dw_probe);
1602}
Viresh Kumarcb689a72011-03-03 15:47:15 +05301603subsys_initcall(dw_init);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001604
1605static void __exit dw_exit(void)
1606{
1607 platform_driver_unregister(&dw_driver);
1608}
1609module_exit(dw_exit);
1610
1611MODULE_LICENSE("GPL v2");
1612MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001613MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Viresh Kumaraecb7b62011-05-24 14:04:09 +05301614MODULE_AUTHOR("Viresh Kumar <viresh.kumar@st.com>");