blob: 41f03f37df2a742574b983af7826a601ce0fcca8 [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 Shevchenko1d455432012-06-19 13:34:03 +0300194static void dwc_dump_chan_regs(struct dw_dma_chan *dwc)
195{
196 dev_err(chan2dev(&dwc->chan),
197 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
198 channel_readl(dwc, SAR),
199 channel_readl(dwc, DAR),
200 channel_readl(dwc, LLP),
201 channel_readl(dwc, CTL_HI),
202 channel_readl(dwc, CTL_LO));
203}
204
205/*----------------------------------------------------------------------*/
206
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700207/* Called with dwc->lock held and bh disabled */
208static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
209{
210 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
211
212 /* ASSERT: channel is idle */
213 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700214 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700215 "BUG: Attempted to start non-idle channel\n");
Andy Shevchenko1d455432012-06-19 13:34:03 +0300216 dwc_dump_chan_regs(dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700217
218 /* The tasklet will hopefully advance the queue... */
219 return;
220 }
221
Viresh Kumar61e183f2011-11-17 16:01:29 +0530222 dwc_initialize(dwc);
223
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700224 channel_writel(dwc, LLP, first->txd.phys);
225 channel_writel(dwc, CTL_LO,
226 DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
227 channel_writel(dwc, CTL_HI, 0);
228 channel_set_bit(dw, CH_EN, dwc->mask);
229}
230
231/*----------------------------------------------------------------------*/
232
233static void
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530234dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
235 bool callback_required)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700236{
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530237 dma_async_tx_callback callback = NULL;
238 void *param = NULL;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700239 struct dma_async_tx_descriptor *txd = &desc->txd;
Viresh Kumare5180762011-03-03 15:47:20 +0530240 struct dw_desc *child;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530241 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700242
Dan Williams41d5e592009-01-06 11:38:21 -0700243 dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700244
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530245 spin_lock_irqsave(&dwc->lock, flags);
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +0000246 dma_cookie_complete(txd);
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530247 if (callback_required) {
248 callback = txd->callback;
249 param = txd->callback_param;
250 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700251
252 dwc_sync_desc_for_cpu(dwc, desc);
Viresh Kumare5180762011-03-03 15:47:20 +0530253
254 /* async_tx_ack */
255 list_for_each_entry(child, &desc->tx_list, desc_node)
256 async_tx_ack(&child->txd);
257 async_tx_ack(&desc->txd);
258
Dan Williamse0bd0f82009-09-08 17:53:02 -0700259 list_splice_init(&desc->tx_list, &dwc->free_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700260 list_move(&desc->desc_node, &dwc->free_list);
261
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700262 if (!dwc->chan.private) {
263 struct device *parent = chan2parent(&dwc->chan);
264 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
265 if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
266 dma_unmap_single(parent, desc->lli.dar,
267 desc->len, DMA_FROM_DEVICE);
268 else
269 dma_unmap_page(parent, desc->lli.dar,
270 desc->len, DMA_FROM_DEVICE);
271 }
272 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
273 if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
274 dma_unmap_single(parent, desc->lli.sar,
275 desc->len, DMA_TO_DEVICE);
276 else
277 dma_unmap_page(parent, desc->lli.sar,
278 desc->len, DMA_TO_DEVICE);
279 }
280 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700281
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530282 spin_unlock_irqrestore(&dwc->lock, flags);
283
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530284 if (callback_required && callback)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700285 callback(param);
286}
287
288static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
289{
290 struct dw_desc *desc, *_desc;
291 LIST_HEAD(list);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530292 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700293
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530294 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700295 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700296 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700297 "BUG: XFER bit set, but channel not idle!\n");
298
299 /* Try to continue after resetting the channel... */
300 channel_clear_bit(dw, CH_EN, dwc->mask);
301 while (dma_readl(dw, CH_EN) & dwc->mask)
302 cpu_relax();
303 }
304
305 /*
306 * Submit queued descriptors ASAP, i.e. before we go through
307 * the completed ones.
308 */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700309 list_splice_init(&dwc->active_list, &list);
Viresh Kumarf336e422011-03-03 15:47:16 +0530310 if (!list_empty(&dwc->queue)) {
311 list_move(dwc->queue.next, &dwc->active_list);
312 dwc_dostart(dwc, dwc_first_active(dwc));
313 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700314
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530315 spin_unlock_irqrestore(&dwc->lock, flags);
316
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700317 list_for_each_entry_safe(desc, _desc, &list, desc_node)
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530318 dwc_descriptor_complete(dwc, desc, true);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700319}
320
321static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
322{
323 dma_addr_t llp;
324 struct dw_desc *desc, *_desc;
325 struct dw_desc *child;
326 u32 status_xfer;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530327 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700328
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530329 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700330 llp = channel_readl(dwc, LLP);
331 status_xfer = dma_readl(dw, RAW.XFER);
332
333 if (status_xfer & dwc->mask) {
334 /* Everything we've submitted is done */
335 dma_writel(dw, CLEAR.XFER, dwc->mask);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530336 spin_unlock_irqrestore(&dwc->lock, flags);
337
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700338 dwc_complete_all(dw, dwc);
339 return;
340 }
341
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530342 if (list_empty(&dwc->active_list)) {
343 spin_unlock_irqrestore(&dwc->lock, flags);
Jamie Iles087809f2011-01-21 14:11:52 +0000344 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530345 }
Jamie Iles087809f2011-01-21 14:11:52 +0000346
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300347 dev_vdbg(chan2dev(&dwc->chan), "%s: llp=0x%llx\n", __func__,
Andy Shevchenko2f45d612012-06-19 13:34:02 +0300348 (unsigned long long)llp);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700349
350 list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
Viresh Kumar84adccf2011-03-24 11:32:15 +0530351 /* check first descriptors addr */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530352 if (desc->txd.phys == llp) {
353 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700354 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530355 }
Viresh Kumar84adccf2011-03-24 11:32:15 +0530356
357 /* check first descriptors llp */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530358 if (desc->lli.llp == llp) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700359 /* This one is currently in progress */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530360 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700361 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530362 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700363
Dan Williamse0bd0f82009-09-08 17:53:02 -0700364 list_for_each_entry(child, &desc->tx_list, desc_node)
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530365 if (child->lli.llp == llp) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700366 /* Currently in progress */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530367 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700368 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530369 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700370
371 /*
372 * No descriptors so far seem to be in progress, i.e.
373 * this one must be done.
374 */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530375 spin_unlock_irqrestore(&dwc->lock, flags);
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530376 dwc_descriptor_complete(dwc, desc, true);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530377 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700378 }
379
Dan Williams41d5e592009-01-06 11:38:21 -0700380 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700381 "BUG: All descriptors done, but channel not idle!\n");
382
383 /* Try to continue after resetting the channel... */
384 channel_clear_bit(dw, CH_EN, dwc->mask);
385 while (dma_readl(dw, CH_EN) & dwc->mask)
386 cpu_relax();
387
388 if (!list_empty(&dwc->queue)) {
Viresh Kumarf336e422011-03-03 15:47:16 +0530389 list_move(dwc->queue.next, &dwc->active_list);
390 dwc_dostart(dwc, dwc_first_active(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700391 }
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530392 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700393}
394
395static void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
396{
Dan Williams41d5e592009-01-06 11:38:21 -0700397 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Andy Shevchenko2f45d612012-06-19 13:34:02 +0300398 " desc: s0x%llx d0x%llx l0x%llx c0x%x:%x\n",
399 (unsigned long long)lli->sar,
400 (unsigned long long)lli->dar,
401 (unsigned long long)lli->llp,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700402 lli->ctlhi, lli->ctllo);
403}
404
405static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
406{
407 struct dw_desc *bad_desc;
408 struct dw_desc *child;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530409 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700410
411 dwc_scan_descriptors(dw, dwc);
412
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530413 spin_lock_irqsave(&dwc->lock, flags);
414
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700415 /*
416 * The descriptor currently at the head of the active list is
417 * borked. Since we don't have any way to report errors, we'll
418 * just have to scream loudly and try to carry on.
419 */
420 bad_desc = dwc_first_active(dwc);
421 list_del_init(&bad_desc->desc_node);
Viresh Kumarf336e422011-03-03 15:47:16 +0530422 list_move(dwc->queue.next, dwc->active_list.prev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700423
424 /* Clear the error flag and try to restart the controller */
425 dma_writel(dw, CLEAR.ERROR, dwc->mask);
426 if (!list_empty(&dwc->active_list))
427 dwc_dostart(dwc, dwc_first_active(dwc));
428
429 /*
430 * KERN_CRITICAL may seem harsh, but since this only happens
431 * when someone submits a bad physical address in a
432 * descriptor, we should consider ourselves lucky that the
433 * controller flagged an error instead of scribbling over
434 * random memory locations.
435 */
Dan Williams41d5e592009-01-06 11:38:21 -0700436 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700437 "Bad descriptor submitted for DMA!\n");
Dan Williams41d5e592009-01-06 11:38:21 -0700438 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700439 " cookie: %d\n", bad_desc->txd.cookie);
440 dwc_dump_lli(dwc, &bad_desc->lli);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700441 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700442 dwc_dump_lli(dwc, &child->lli);
443
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530444 spin_unlock_irqrestore(&dwc->lock, flags);
445
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700446 /* Pretend the descriptor completed successfully */
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530447 dwc_descriptor_complete(dwc, bad_desc, true);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700448}
449
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200450/* --------------------- Cyclic DMA API extensions -------------------- */
451
452inline dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
453{
454 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
455 return channel_readl(dwc, SAR);
456}
457EXPORT_SYMBOL(dw_dma_get_src_addr);
458
459inline dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
460{
461 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
462 return channel_readl(dwc, DAR);
463}
464EXPORT_SYMBOL(dw_dma_get_dst_addr);
465
466/* called with dwc->lock held and all DMAC interrupts disabled */
467static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530468 u32 status_err, u32 status_xfer)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200469{
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530470 unsigned long flags;
471
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530472 if (dwc->mask) {
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200473 void (*callback)(void *param);
474 void *callback_param;
475
476 dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
477 channel_readl(dwc, LLP));
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200478
479 callback = dwc->cdesc->period_callback;
480 callback_param = dwc->cdesc->period_callback_param;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530481
482 if (callback)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200483 callback(callback_param);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200484 }
485
486 /*
487 * Error and transfer complete are highly unlikely, and will most
488 * likely be due to a configuration error by the user.
489 */
490 if (unlikely(status_err & dwc->mask) ||
491 unlikely(status_xfer & dwc->mask)) {
492 int i;
493
494 dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s "
495 "interrupt, stopping DMA transfer\n",
496 status_xfer ? "xfer" : "error");
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530497
498 spin_lock_irqsave(&dwc->lock, flags);
499
Andy Shevchenko1d455432012-06-19 13:34:03 +0300500 dwc_dump_chan_regs(dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200501
502 channel_clear_bit(dw, CH_EN, dwc->mask);
503 while (dma_readl(dw, CH_EN) & dwc->mask)
504 cpu_relax();
505
506 /* make sure DMA does not restart by loading a new list */
507 channel_writel(dwc, LLP, 0);
508 channel_writel(dwc, CTL_LO, 0);
509 channel_writel(dwc, CTL_HI, 0);
510
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200511 dma_writel(dw, CLEAR.ERROR, dwc->mask);
512 dma_writel(dw, CLEAR.XFER, dwc->mask);
513
514 for (i = 0; i < dwc->cdesc->periods; i++)
515 dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530516
517 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200518 }
519}
520
521/* ------------------------------------------------------------------------- */
522
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700523static void dw_dma_tasklet(unsigned long data)
524{
525 struct dw_dma *dw = (struct dw_dma *)data;
526 struct dw_dma_chan *dwc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700527 u32 status_xfer;
528 u32 status_err;
529 int i;
530
Haavard Skinnemoen7fe7b2f2008-10-03 15:23:46 -0700531 status_xfer = dma_readl(dw, RAW.XFER);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700532 status_err = dma_readl(dw, RAW.ERROR);
533
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300534 dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700535
536 for (i = 0; i < dw->dma.chancnt; i++) {
537 dwc = &dw->chan[i];
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200538 if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530539 dwc_handle_cyclic(dw, dwc, status_err, status_xfer);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200540 else if (status_err & (1 << i))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700541 dwc_handle_error(dw, dwc);
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530542 else if (status_xfer & (1 << i))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700543 dwc_scan_descriptors(dw, dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700544 }
545
546 /*
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530547 * Re-enable interrupts.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700548 */
549 channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700550 channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
551}
552
553static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
554{
555 struct dw_dma *dw = dev_id;
556 u32 status;
557
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300558 dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700559 dma_readl(dw, STATUS_INT));
560
561 /*
562 * Just disable the interrupts. We'll turn them back on in the
563 * softirq handler.
564 */
565 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700566 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
567
568 status = dma_readl(dw, STATUS_INT);
569 if (status) {
570 dev_err(dw->dma.dev,
571 "BUG: Unexpected interrupts pending: 0x%x\n",
572 status);
573
574 /* Try to recover */
575 channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700576 channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
577 channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
578 channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
579 }
580
581 tasklet_schedule(&dw->tasklet);
582
583 return IRQ_HANDLED;
584}
585
586/*----------------------------------------------------------------------*/
587
588static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
589{
590 struct dw_desc *desc = txd_to_dw_desc(tx);
591 struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
592 dma_cookie_t cookie;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530593 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700594
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530595 spin_lock_irqsave(&dwc->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000596 cookie = dma_cookie_assign(tx);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700597
598 /*
599 * REVISIT: We should attempt to chain as many descriptors as
600 * possible, perhaps even appending to those already submitted
601 * for DMA. But this is hard to do in a race-free manner.
602 */
603 if (list_empty(&dwc->active_list)) {
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300604 dev_vdbg(chan2dev(tx->chan), "%s: started %u\n", __func__,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700605 desc->txd.cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700606 list_add_tail(&desc->desc_node, &dwc->active_list);
Viresh Kumarf336e422011-03-03 15:47:16 +0530607 dwc_dostart(dwc, dwc_first_active(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700608 } else {
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300609 dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n", __func__,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700610 desc->txd.cookie);
611
612 list_add_tail(&desc->desc_node, &dwc->queue);
613 }
614
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530615 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700616
617 return cookie;
618}
619
620static struct dma_async_tx_descriptor *
621dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
622 size_t len, unsigned long flags)
623{
624 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
625 struct dw_desc *desc;
626 struct dw_desc *first;
627 struct dw_desc *prev;
628 size_t xfer_count;
629 size_t offset;
630 unsigned int src_width;
631 unsigned int dst_width;
632 u32 ctllo;
633
Andy Shevchenko2f45d612012-06-19 13:34:02 +0300634 dev_vdbg(chan2dev(chan),
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300635 "%s: d0x%llx s0x%llx l0x%zx f0x%lx\n", __func__,
Andy Shevchenko2f45d612012-06-19 13:34:02 +0300636 (unsigned long long)dest, (unsigned long long)src,
637 len, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700638
639 if (unlikely(!len)) {
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300640 dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700641 return NULL;
642 }
643
644 /*
645 * We can be a lot more clever here, but this should take care
646 * of the most common optimization.
647 */
Viresh Kumara0227452011-03-03 15:47:18 +0530648 if (!((src | dest | len) & 7))
649 src_width = dst_width = 3;
650 else if (!((src | dest | len) & 3))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700651 src_width = dst_width = 2;
652 else if (!((src | dest | len) & 1))
653 src_width = dst_width = 1;
654 else
655 src_width = dst_width = 0;
656
Viresh Kumar327e6972012-02-01 16:12:26 +0530657 ctllo = DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700658 | DWC_CTLL_DST_WIDTH(dst_width)
659 | DWC_CTLL_SRC_WIDTH(src_width)
660 | DWC_CTLL_DST_INC
661 | DWC_CTLL_SRC_INC
662 | DWC_CTLL_FC_M2M;
663 prev = first = NULL;
664
665 for (offset = 0; offset < len; offset += xfer_count << src_width) {
666 xfer_count = min_t(size_t, (len - offset) >> src_width,
667 DWC_MAX_COUNT);
668
669 desc = dwc_desc_get(dwc);
670 if (!desc)
671 goto err_desc_get;
672
673 desc->lli.sar = src + offset;
674 desc->lli.dar = dest + offset;
675 desc->lli.ctllo = ctllo;
676 desc->lli.ctlhi = xfer_count;
677
678 if (!first) {
679 first = desc;
680 } else {
681 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700682 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700683 prev->txd.phys, sizeof(prev->lli),
684 DMA_TO_DEVICE);
685 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700686 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700687 }
688 prev = desc;
689 }
690
691
692 if (flags & DMA_PREP_INTERRUPT)
693 /* Trigger interrupt after last block */
694 prev->lli.ctllo |= DWC_CTLL_INT_EN;
695
696 prev->lli.llp = 0;
Dan Williams41d5e592009-01-06 11:38:21 -0700697 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700698 prev->txd.phys, sizeof(prev->lli),
699 DMA_TO_DEVICE);
700
701 first->txd.flags = flags;
702 first->len = len;
703
704 return &first->txd;
705
706err_desc_get:
707 dwc_desc_put(dwc, first);
708 return NULL;
709}
710
711static struct dma_async_tx_descriptor *
712dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530713 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500714 unsigned long flags, void *context)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700715{
716 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Dan Williams287d8592009-02-18 14:48:26 -0800717 struct dw_dma_slave *dws = chan->private;
Viresh Kumar327e6972012-02-01 16:12:26 +0530718 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700719 struct dw_desc *prev;
720 struct dw_desc *first;
721 u32 ctllo;
722 dma_addr_t reg;
723 unsigned int reg_width;
724 unsigned int mem_width;
725 unsigned int i;
726 struct scatterlist *sg;
727 size_t total_len = 0;
728
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300729 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700730
731 if (unlikely(!dws || !sg_len))
732 return NULL;
733
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700734 prev = first = NULL;
735
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700736 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530737 case DMA_MEM_TO_DEV:
Viresh Kumar327e6972012-02-01 16:12:26 +0530738 reg_width = __fls(sconfig->dst_addr_width);
739 reg = sconfig->dst_addr;
740 ctllo = (DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700741 | DWC_CTLL_DST_WIDTH(reg_width)
742 | DWC_CTLL_DST_FIX
Viresh Kumar327e6972012-02-01 16:12:26 +0530743 | DWC_CTLL_SRC_INC);
744
745 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
746 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
747
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700748 for_each_sg(sgl, sg, sg_len, i) {
749 struct dw_desc *desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530750 u32 len, dlen, mem;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700751
Lars-Peter Clausencbb796c2012-04-25 20:50:51 +0200752 mem = sg_dma_address(sg);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700753 len = sg_dma_len(sg);
Viresh Kumar6bc711f2012-02-01 16:12:25 +0530754
755 if (!((mem | len) & 7))
756 mem_width = 3;
757 else if (!((mem | len) & 3))
758 mem_width = 2;
759 else if (!((mem | len) & 1))
760 mem_width = 1;
761 else
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700762 mem_width = 0;
763
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530764slave_sg_todev_fill_desc:
765 desc = dwc_desc_get(dwc);
766 if (!desc) {
767 dev_err(chan2dev(chan),
768 "not enough descriptors available\n");
769 goto err_desc_get;
770 }
771
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700772 desc->lli.sar = mem;
773 desc->lli.dar = reg;
774 desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530775 if ((len >> mem_width) > DWC_MAX_COUNT) {
776 dlen = DWC_MAX_COUNT << mem_width;
777 mem += dlen;
778 len -= dlen;
779 } else {
780 dlen = len;
781 len = 0;
782 }
783
784 desc->lli.ctlhi = dlen >> mem_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700785
786 if (!first) {
787 first = desc;
788 } else {
789 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700790 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700791 prev->txd.phys,
792 sizeof(prev->lli),
793 DMA_TO_DEVICE);
794 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700795 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700796 }
797 prev = desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530798 total_len += dlen;
799
800 if (len)
801 goto slave_sg_todev_fill_desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700802 }
803 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530804 case DMA_DEV_TO_MEM:
Viresh Kumar327e6972012-02-01 16:12:26 +0530805 reg_width = __fls(sconfig->src_addr_width);
806 reg = sconfig->src_addr;
807 ctllo = (DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700808 | DWC_CTLL_SRC_WIDTH(reg_width)
809 | DWC_CTLL_DST_INC
Viresh Kumar327e6972012-02-01 16:12:26 +0530810 | DWC_CTLL_SRC_FIX);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700811
Viresh Kumar327e6972012-02-01 16:12:26 +0530812 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
813 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
814
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700815 for_each_sg(sgl, sg, sg_len, i) {
816 struct dw_desc *desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530817 u32 len, dlen, mem;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700818
Lars-Peter Clausencbb796c2012-04-25 20:50:51 +0200819 mem = sg_dma_address(sg);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700820 len = sg_dma_len(sg);
Viresh Kumar6bc711f2012-02-01 16:12:25 +0530821
822 if (!((mem | len) & 7))
823 mem_width = 3;
824 else if (!((mem | len) & 3))
825 mem_width = 2;
826 else if (!((mem | len) & 1))
827 mem_width = 1;
828 else
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700829 mem_width = 0;
830
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530831slave_sg_fromdev_fill_desc:
832 desc = dwc_desc_get(dwc);
833 if (!desc) {
834 dev_err(chan2dev(chan),
835 "not enough descriptors available\n");
836 goto err_desc_get;
837 }
838
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700839 desc->lli.sar = reg;
840 desc->lli.dar = mem;
841 desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530842 if ((len >> reg_width) > DWC_MAX_COUNT) {
843 dlen = DWC_MAX_COUNT << reg_width;
844 mem += dlen;
845 len -= dlen;
846 } else {
847 dlen = len;
848 len = 0;
849 }
850 desc->lli.ctlhi = dlen >> reg_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700851
852 if (!first) {
853 first = desc;
854 } else {
855 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700856 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700857 prev->txd.phys,
858 sizeof(prev->lli),
859 DMA_TO_DEVICE);
860 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700861 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700862 }
863 prev = desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530864 total_len += dlen;
865
866 if (len)
867 goto slave_sg_fromdev_fill_desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700868 }
869 break;
870 default:
871 return NULL;
872 }
873
874 if (flags & DMA_PREP_INTERRUPT)
875 /* Trigger interrupt after last block */
876 prev->lli.ctllo |= DWC_CTLL_INT_EN;
877
878 prev->lli.llp = 0;
Dan Williams41d5e592009-01-06 11:38:21 -0700879 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700880 prev->txd.phys, sizeof(prev->lli),
881 DMA_TO_DEVICE);
882
883 first->len = total_len;
884
885 return &first->txd;
886
887err_desc_get:
888 dwc_desc_put(dwc, first);
889 return NULL;
890}
891
Viresh Kumar327e6972012-02-01 16:12:26 +0530892/*
893 * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
894 * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
895 *
896 * NOTE: burst size 2 is not supported by controller.
897 *
898 * This can be done by finding least significant bit set: n & (n - 1)
899 */
900static inline void convert_burst(u32 *maxburst)
901{
902 if (*maxburst > 1)
903 *maxburst = fls(*maxburst) - 2;
904 else
905 *maxburst = 0;
906}
907
908static int
909set_runtime_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
910{
911 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
912
913 /* Check if it is chan is configured for slave transfers */
914 if (!chan->private)
915 return -EINVAL;
916
917 memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
918
919 convert_burst(&dwc->dma_sconfig.src_maxburst);
920 convert_burst(&dwc->dma_sconfig.dst_maxburst);
921
922 return 0;
923}
924
Linus Walleij05827632010-05-17 16:30:42 -0700925static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
926 unsigned long arg)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700927{
928 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
929 struct dw_dma *dw = to_dw_dma(chan->device);
930 struct dw_desc *desc, *_desc;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530931 unsigned long flags;
Linus Walleija7c57cf2011-04-19 08:31:32 +0800932 u32 cfglo;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700933 LIST_HEAD(list);
934
Linus Walleija7c57cf2011-04-19 08:31:32 +0800935 if (cmd == DMA_PAUSE) {
936 spin_lock_irqsave(&dwc->lock, flags);
937
938 cfglo = channel_readl(dwc, CFG_LO);
939 channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
940 while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY))
941 cpu_relax();
942
943 dwc->paused = true;
944 spin_unlock_irqrestore(&dwc->lock, flags);
945 } else if (cmd == DMA_RESUME) {
946 if (!dwc->paused)
947 return 0;
948
949 spin_lock_irqsave(&dwc->lock, flags);
950
951 cfglo = channel_readl(dwc, CFG_LO);
952 channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
953 dwc->paused = false;
954
955 spin_unlock_irqrestore(&dwc->lock, flags);
956 } else if (cmd == DMA_TERMINATE_ALL) {
957 spin_lock_irqsave(&dwc->lock, flags);
958
959 channel_clear_bit(dw, CH_EN, dwc->mask);
960 while (dma_readl(dw, CH_EN) & dwc->mask)
961 cpu_relax();
962
963 dwc->paused = false;
964
965 /* active_list entries will end up before queued entries */
966 list_splice_init(&dwc->queue, &list);
967 list_splice_init(&dwc->active_list, &list);
968
969 spin_unlock_irqrestore(&dwc->lock, flags);
970
971 /* Flush all pending and queued descriptors */
972 list_for_each_entry_safe(desc, _desc, &list, desc_node)
973 dwc_descriptor_complete(dwc, desc, false);
Viresh Kumar327e6972012-02-01 16:12:26 +0530974 } else if (cmd == DMA_SLAVE_CONFIG) {
975 return set_runtime_config(chan, (struct dma_slave_config *)arg);
976 } else {
Linus Walleijc3635c72010-03-26 16:44:01 -0700977 return -ENXIO;
Viresh Kumar327e6972012-02-01 16:12:26 +0530978 }
Linus Walleijc3635c72010-03-26 16:44:01 -0700979
Linus Walleijc3635c72010-03-26 16:44:01 -0700980 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700981}
982
983static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -0700984dwc_tx_status(struct dma_chan *chan,
985 dma_cookie_t cookie,
986 struct dma_tx_state *txstate)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700987{
988 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000989 enum dma_status ret;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700990
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000991 ret = dma_cookie_status(chan, cookie, txstate);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700992 if (ret != DMA_SUCCESS) {
993 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
994
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000995 ret = dma_cookie_status(chan, cookie, txstate);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700996 }
997
Viresh Kumarabf53902011-04-15 16:03:35 +0530998 if (ret != DMA_SUCCESS)
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +0000999 dma_set_residue(txstate, dwc_first_active(dwc)->len);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001000
Linus Walleija7c57cf2011-04-19 08:31:32 +08001001 if (dwc->paused)
1002 return DMA_PAUSED;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001003
1004 return ret;
1005}
1006
1007static void dwc_issue_pending(struct dma_chan *chan)
1008{
1009 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1010
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001011 if (!list_empty(&dwc->queue))
1012 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001013}
1014
Dan Williamsaa1e6f12009-01-06 11:38:17 -07001015static int dwc_alloc_chan_resources(struct dma_chan *chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001016{
1017 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1018 struct dw_dma *dw = to_dw_dma(chan->device);
1019 struct dw_desc *desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001020 int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301021 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001022
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001023 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001024
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001025 /* ASSERT: channel is idle */
1026 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -07001027 dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001028 return -EIO;
1029 }
1030
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001031 dma_cookie_init(chan);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001032
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001033 /*
1034 * NOTE: some controllers may have additional features that we
1035 * need to initialize here, like "scatter-gather" (which
1036 * doesn't mean what you think it means), and status writeback.
1037 */
1038
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301039 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001040 i = dwc->descs_allocated;
1041 while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301042 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001043
1044 desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL);
1045 if (!desc) {
Dan Williams41d5e592009-01-06 11:38:21 -07001046 dev_info(chan2dev(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001047 "only allocated %d descriptors\n", i);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301048 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001049 break;
1050 }
1051
Dan Williamse0bd0f82009-09-08 17:53:02 -07001052 INIT_LIST_HEAD(&desc->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001053 dma_async_tx_descriptor_init(&desc->txd, chan);
1054 desc->txd.tx_submit = dwc_tx_submit;
1055 desc->txd.flags = DMA_CTRL_ACK;
Dan Williams41d5e592009-01-06 11:38:21 -07001056 desc->txd.phys = dma_map_single(chan2parent(chan), &desc->lli,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001057 sizeof(desc->lli), DMA_TO_DEVICE);
1058 dwc_desc_put(dwc, desc);
1059
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301060 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001061 i = ++dwc->descs_allocated;
1062 }
1063
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301064 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001065
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001066 dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001067
1068 return i;
1069}
1070
1071static void dwc_free_chan_resources(struct dma_chan *chan)
1072{
1073 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1074 struct dw_dma *dw = to_dw_dma(chan->device);
1075 struct dw_desc *desc, *_desc;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301076 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001077 LIST_HEAD(list);
1078
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001079 dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001080 dwc->descs_allocated);
1081
1082 /* ASSERT: channel is idle */
1083 BUG_ON(!list_empty(&dwc->active_list));
1084 BUG_ON(!list_empty(&dwc->queue));
1085 BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
1086
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301087 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001088 list_splice_init(&dwc->free_list, &list);
1089 dwc->descs_allocated = 0;
Viresh Kumar61e183f2011-11-17 16:01:29 +05301090 dwc->initialized = false;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001091
1092 /* Disable interrupts */
1093 channel_clear_bit(dw, MASK.XFER, dwc->mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001094 channel_clear_bit(dw, MASK.ERROR, dwc->mask);
1095
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301096 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001097
1098 list_for_each_entry_safe(desc, _desc, &list, desc_node) {
Dan Williams41d5e592009-01-06 11:38:21 -07001099 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1100 dma_unmap_single(chan2parent(chan), desc->txd.phys,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001101 sizeof(desc->lli), DMA_TO_DEVICE);
1102 kfree(desc);
1103 }
1104
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001105 dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001106}
1107
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001108/* --------------------- Cyclic DMA API extensions -------------------- */
1109
1110/**
1111 * dw_dma_cyclic_start - start the cyclic DMA transfer
1112 * @chan: the DMA channel to start
1113 *
1114 * Must be called with soft interrupts disabled. Returns zero on success or
1115 * -errno on failure.
1116 */
1117int dw_dma_cyclic_start(struct dma_chan *chan)
1118{
1119 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1120 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301121 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001122
1123 if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
1124 dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
1125 return -ENODEV;
1126 }
1127
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301128 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001129
1130 /* assert channel is idle */
1131 if (dma_readl(dw, CH_EN) & dwc->mask) {
1132 dev_err(chan2dev(&dwc->chan),
1133 "BUG: Attempted to start non-idle channel\n");
Andy Shevchenko1d455432012-06-19 13:34:03 +03001134 dwc_dump_chan_regs(dwc);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301135 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001136 return -EBUSY;
1137 }
1138
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001139 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1140 dma_writel(dw, CLEAR.XFER, dwc->mask);
1141
1142 /* setup DMAC channel registers */
1143 channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
1144 channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
1145 channel_writel(dwc, CTL_HI, 0);
1146
1147 channel_set_bit(dw, CH_EN, dwc->mask);
1148
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301149 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001150
1151 return 0;
1152}
1153EXPORT_SYMBOL(dw_dma_cyclic_start);
1154
1155/**
1156 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1157 * @chan: the DMA channel to stop
1158 *
1159 * Must be called with soft interrupts disabled.
1160 */
1161void dw_dma_cyclic_stop(struct dma_chan *chan)
1162{
1163 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1164 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301165 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001166
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301167 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001168
1169 channel_clear_bit(dw, CH_EN, dwc->mask);
1170 while (dma_readl(dw, CH_EN) & dwc->mask)
1171 cpu_relax();
1172
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301173 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001174}
1175EXPORT_SYMBOL(dw_dma_cyclic_stop);
1176
1177/**
1178 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1179 * @chan: the DMA channel to prepare
1180 * @buf_addr: physical DMA address where the buffer starts
1181 * @buf_len: total number of bytes for the entire buffer
1182 * @period_len: number of bytes for each period
1183 * @direction: transfer direction, to or from device
1184 *
1185 * Must be called before trying to start the transfer. Returns a valid struct
1186 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1187 */
1188struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
1189 dma_addr_t buf_addr, size_t buf_len, size_t period_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05301190 enum dma_transfer_direction direction)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001191{
1192 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Viresh Kumar327e6972012-02-01 16:12:26 +05301193 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001194 struct dw_cyclic_desc *cdesc;
1195 struct dw_cyclic_desc *retval = NULL;
1196 struct dw_desc *desc;
1197 struct dw_desc *last = NULL;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001198 unsigned long was_cyclic;
1199 unsigned int reg_width;
1200 unsigned int periods;
1201 unsigned int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301202 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001203
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301204 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001205 if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301206 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001207 dev_dbg(chan2dev(&dwc->chan),
1208 "queue and/or active list are not empty\n");
1209 return ERR_PTR(-EBUSY);
1210 }
1211
1212 was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301213 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001214 if (was_cyclic) {
1215 dev_dbg(chan2dev(&dwc->chan),
1216 "channel already prepared for cyclic DMA\n");
1217 return ERR_PTR(-EBUSY);
1218 }
1219
1220 retval = ERR_PTR(-EINVAL);
Viresh Kumar327e6972012-02-01 16:12:26 +05301221
1222 if (direction == DMA_MEM_TO_DEV)
1223 reg_width = __ffs(sconfig->dst_addr_width);
1224 else
1225 reg_width = __ffs(sconfig->src_addr_width);
1226
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001227 periods = buf_len / period_len;
1228
1229 /* Check for too big/unaligned periods and unaligned DMA buffer. */
1230 if (period_len > (DWC_MAX_COUNT << reg_width))
1231 goto out_err;
1232 if (unlikely(period_len & ((1 << reg_width) - 1)))
1233 goto out_err;
1234 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
1235 goto out_err;
Vinod Kouldb8196d2011-10-13 22:34:23 +05301236 if (unlikely(!(direction & (DMA_MEM_TO_DEV | DMA_DEV_TO_MEM))))
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001237 goto out_err;
1238
1239 retval = ERR_PTR(-ENOMEM);
1240
1241 if (periods > NR_DESCS_PER_CHANNEL)
1242 goto out_err;
1243
1244 cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
1245 if (!cdesc)
1246 goto out_err;
1247
1248 cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
1249 if (!cdesc->desc)
1250 goto out_err_alloc;
1251
1252 for (i = 0; i < periods; i++) {
1253 desc = dwc_desc_get(dwc);
1254 if (!desc)
1255 goto out_err_desc_get;
1256
1257 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +05301258 case DMA_MEM_TO_DEV:
Viresh Kumar327e6972012-02-01 16:12:26 +05301259 desc->lli.dar = sconfig->dst_addr;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001260 desc->lli.sar = buf_addr + (period_len * i);
Viresh Kumar327e6972012-02-01 16:12:26 +05301261 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001262 | DWC_CTLL_DST_WIDTH(reg_width)
1263 | DWC_CTLL_SRC_WIDTH(reg_width)
1264 | DWC_CTLL_DST_FIX
1265 | DWC_CTLL_SRC_INC
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001266 | DWC_CTLL_INT_EN);
Viresh Kumar327e6972012-02-01 16:12:26 +05301267
1268 desc->lli.ctllo |= sconfig->device_fc ?
1269 DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
1270 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
1271
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001272 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +05301273 case DMA_DEV_TO_MEM:
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001274 desc->lli.dar = buf_addr + (period_len * i);
Viresh Kumar327e6972012-02-01 16:12:26 +05301275 desc->lli.sar = sconfig->src_addr;
1276 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001277 | DWC_CTLL_SRC_WIDTH(reg_width)
1278 | DWC_CTLL_DST_WIDTH(reg_width)
1279 | DWC_CTLL_DST_INC
1280 | DWC_CTLL_SRC_FIX
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001281 | DWC_CTLL_INT_EN);
Viresh Kumar327e6972012-02-01 16:12:26 +05301282
1283 desc->lli.ctllo |= sconfig->device_fc ?
1284 DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
1285 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
1286
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001287 break;
1288 default:
1289 break;
1290 }
1291
1292 desc->lli.ctlhi = (period_len >> reg_width);
1293 cdesc->desc[i] = desc;
1294
1295 if (last) {
1296 last->lli.llp = desc->txd.phys;
1297 dma_sync_single_for_device(chan2parent(chan),
1298 last->txd.phys, sizeof(last->lli),
1299 DMA_TO_DEVICE);
1300 }
1301
1302 last = desc;
1303 }
1304
1305 /* lets make a cyclic list */
1306 last->lli.llp = cdesc->desc[0]->txd.phys;
1307 dma_sync_single_for_device(chan2parent(chan), last->txd.phys,
1308 sizeof(last->lli), DMA_TO_DEVICE);
1309
Andy Shevchenko2f45d612012-06-19 13:34:02 +03001310 dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%llx len %zu "
1311 "period %zu periods %d\n", (unsigned long long)buf_addr,
1312 buf_len, period_len, periods);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001313
1314 cdesc->periods = periods;
1315 dwc->cdesc = cdesc;
1316
1317 return cdesc;
1318
1319out_err_desc_get:
1320 while (i--)
1321 dwc_desc_put(dwc, cdesc->desc[i]);
1322out_err_alloc:
1323 kfree(cdesc);
1324out_err:
1325 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1326 return (struct dw_cyclic_desc *)retval;
1327}
1328EXPORT_SYMBOL(dw_dma_cyclic_prep);
1329
1330/**
1331 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1332 * @chan: the DMA channel to free
1333 */
1334void dw_dma_cyclic_free(struct dma_chan *chan)
1335{
1336 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1337 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1338 struct dw_cyclic_desc *cdesc = dwc->cdesc;
1339 int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301340 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001341
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001342 dev_dbg(chan2dev(&dwc->chan), "%s\n", __func__);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001343
1344 if (!cdesc)
1345 return;
1346
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301347 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001348
1349 channel_clear_bit(dw, CH_EN, dwc->mask);
1350 while (dma_readl(dw, CH_EN) & dwc->mask)
1351 cpu_relax();
1352
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001353 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1354 dma_writel(dw, CLEAR.XFER, dwc->mask);
1355
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301356 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001357
1358 for (i = 0; i < cdesc->periods; i++)
1359 dwc_desc_put(dwc, cdesc->desc[i]);
1360
1361 kfree(cdesc->desc);
1362 kfree(cdesc);
1363
1364 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1365}
1366EXPORT_SYMBOL(dw_dma_cyclic_free);
1367
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001368/*----------------------------------------------------------------------*/
1369
1370static void dw_dma_off(struct dw_dma *dw)
1371{
Viresh Kumar61e183f2011-11-17 16:01:29 +05301372 int i;
1373
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001374 dma_writel(dw, CFG, 0);
1375
1376 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001377 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1378 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1379 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1380
1381 while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
1382 cpu_relax();
Viresh Kumar61e183f2011-11-17 16:01:29 +05301383
1384 for (i = 0; i < dw->dma.chancnt; i++)
1385 dw->chan[i].initialized = false;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001386}
1387
1388static int __init dw_probe(struct platform_device *pdev)
1389{
1390 struct dw_dma_platform_data *pdata;
1391 struct resource *io;
1392 struct dw_dma *dw;
1393 size_t size;
1394 int irq;
1395 int err;
1396 int i;
1397
Viresh Kumar6c618c92012-02-01 16:12:22 +05301398 pdata = dev_get_platdata(&pdev->dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001399 if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
1400 return -EINVAL;
1401
1402 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1403 if (!io)
1404 return -EINVAL;
1405
1406 irq = platform_get_irq(pdev, 0);
1407 if (irq < 0)
1408 return irq;
1409
1410 size = sizeof(struct dw_dma);
1411 size += pdata->nr_channels * sizeof(struct dw_dma_chan);
1412 dw = kzalloc(size, GFP_KERNEL);
1413 if (!dw)
1414 return -ENOMEM;
1415
1416 if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) {
1417 err = -EBUSY;
1418 goto err_kfree;
1419 }
1420
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001421 dw->regs = ioremap(io->start, DW_REGLEN);
1422 if (!dw->regs) {
1423 err = -ENOMEM;
1424 goto err_release_r;
1425 }
1426
1427 dw->clk = clk_get(&pdev->dev, "hclk");
1428 if (IS_ERR(dw->clk)) {
1429 err = PTR_ERR(dw->clk);
1430 goto err_clk;
1431 }
Viresh Kumar30755282012-04-17 17:10:07 +05301432 clk_prepare_enable(dw->clk);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001433
1434 /* force dma off, just in case */
1435 dw_dma_off(dw);
1436
1437 err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw);
1438 if (err)
1439 goto err_irq;
1440
1441 platform_set_drvdata(pdev, dw);
1442
1443 tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
1444
1445 dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
1446
1447 INIT_LIST_HEAD(&dw->dma.channels);
Barry Song463894702011-09-15 03:06:30 -07001448 for (i = 0; i < pdata->nr_channels; i++) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001449 struct dw_dma_chan *dwc = &dw->chan[i];
1450
1451 dwc->chan.device = &dw->dma;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001452 dma_cookie_init(&dwc->chan);
Viresh Kumarb0c31302011-03-03 15:47:21 +05301453 if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
1454 list_add_tail(&dwc->chan.device_node,
1455 &dw->dma.channels);
1456 else
1457 list_add(&dwc->chan.device_node, &dw->dma.channels);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001458
Viresh Kumar93317e82011-03-03 15:47:22 +05301459 /* 7 is highest priority & 0 is lowest. */
1460 if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
Viresh Kumare8d9f872012-02-01 16:12:21 +05301461 dwc->priority = pdata->nr_channels - i - 1;
Viresh Kumar93317e82011-03-03 15:47:22 +05301462 else
1463 dwc->priority = i;
1464
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001465 dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
1466 spin_lock_init(&dwc->lock);
1467 dwc->mask = 1 << i;
1468
1469 INIT_LIST_HEAD(&dwc->active_list);
1470 INIT_LIST_HEAD(&dwc->queue);
1471 INIT_LIST_HEAD(&dwc->free_list);
1472
1473 channel_clear_bit(dw, CH_EN, dwc->mask);
1474 }
1475
1476 /* Clear/disable all interrupts on all channels. */
1477 dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001478 dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
1479 dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
1480 dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
1481
1482 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001483 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1484 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1485 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1486
1487 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
1488 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
Jamie Iles95ea7592011-01-21 14:11:54 +00001489 if (pdata->is_private)
1490 dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001491 dw->dma.dev = &pdev->dev;
1492 dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
1493 dw->dma.device_free_chan_resources = dwc_free_chan_resources;
1494
1495 dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
1496
1497 dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
Linus Walleijc3635c72010-03-26 16:44:01 -07001498 dw->dma.device_control = dwc_control;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001499
Linus Walleij07934482010-03-26 16:50:49 -07001500 dw->dma.device_tx_status = dwc_tx_status;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001501 dw->dma.device_issue_pending = dwc_issue_pending;
1502
1503 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1504
1505 printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n",
Barry Song463894702011-09-15 03:06:30 -07001506 dev_name(&pdev->dev), pdata->nr_channels);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001507
1508 dma_async_device_register(&dw->dma);
1509
1510 return 0;
1511
1512err_irq:
Viresh Kumar30755282012-04-17 17:10:07 +05301513 clk_disable_unprepare(dw->clk);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001514 clk_put(dw->clk);
1515err_clk:
1516 iounmap(dw->regs);
1517 dw->regs = NULL;
1518err_release_r:
1519 release_resource(io);
1520err_kfree:
1521 kfree(dw);
1522 return err;
1523}
1524
1525static int __exit dw_remove(struct platform_device *pdev)
1526{
1527 struct dw_dma *dw = platform_get_drvdata(pdev);
1528 struct dw_dma_chan *dwc, *_dwc;
1529 struct resource *io;
1530
1531 dw_dma_off(dw);
1532 dma_async_device_unregister(&dw->dma);
1533
1534 free_irq(platform_get_irq(pdev, 0), dw);
1535 tasklet_kill(&dw->tasklet);
1536
1537 list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
1538 chan.device_node) {
1539 list_del(&dwc->chan.device_node);
1540 channel_clear_bit(dw, CH_EN, dwc->mask);
1541 }
1542
Viresh Kumar30755282012-04-17 17:10:07 +05301543 clk_disable_unprepare(dw->clk);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001544 clk_put(dw->clk);
1545
1546 iounmap(dw->regs);
1547 dw->regs = NULL;
1548
1549 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1550 release_mem_region(io->start, DW_REGLEN);
1551
1552 kfree(dw);
1553
1554 return 0;
1555}
1556
1557static void dw_shutdown(struct platform_device *pdev)
1558{
1559 struct dw_dma *dw = platform_get_drvdata(pdev);
1560
1561 dw_dma_off(platform_get_drvdata(pdev));
Viresh Kumar30755282012-04-17 17:10:07 +05301562 clk_disable_unprepare(dw->clk);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001563}
1564
Magnus Damm4a256b52009-07-08 13:22:18 +02001565static int dw_suspend_noirq(struct device *dev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001566{
Magnus Damm4a256b52009-07-08 13:22:18 +02001567 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001568 struct dw_dma *dw = platform_get_drvdata(pdev);
1569
1570 dw_dma_off(platform_get_drvdata(pdev));
Viresh Kumar30755282012-04-17 17:10:07 +05301571 clk_disable_unprepare(dw->clk);
Viresh Kumar61e183f2011-11-17 16:01:29 +05301572
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001573 return 0;
1574}
1575
Magnus Damm4a256b52009-07-08 13:22:18 +02001576static int dw_resume_noirq(struct device *dev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001577{
Magnus Damm4a256b52009-07-08 13:22:18 +02001578 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001579 struct dw_dma *dw = platform_get_drvdata(pdev);
1580
Viresh Kumar30755282012-04-17 17:10:07 +05301581 clk_prepare_enable(dw->clk);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001582 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1583 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001584}
1585
Alexey Dobriyan47145212009-12-14 18:00:08 -08001586static const struct dev_pm_ops dw_dev_pm_ops = {
Magnus Damm4a256b52009-07-08 13:22:18 +02001587 .suspend_noirq = dw_suspend_noirq,
1588 .resume_noirq = dw_resume_noirq,
Rajeev KUMAR7414a1b2012-02-01 16:12:17 +05301589 .freeze_noirq = dw_suspend_noirq,
1590 .thaw_noirq = dw_resume_noirq,
1591 .restore_noirq = dw_resume_noirq,
1592 .poweroff_noirq = dw_suspend_noirq,
Magnus Damm4a256b52009-07-08 13:22:18 +02001593};
1594
Viresh Kumard3f797d2012-04-20 20:15:34 +05301595#ifdef CONFIG_OF
1596static const struct of_device_id dw_dma_id_table[] = {
1597 { .compatible = "snps,dma-spear1340" },
1598 {}
1599};
1600MODULE_DEVICE_TABLE(of, dw_dma_id_table);
1601#endif
1602
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001603static struct platform_driver dw_driver = {
1604 .remove = __exit_p(dw_remove),
1605 .shutdown = dw_shutdown,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001606 .driver = {
1607 .name = "dw_dmac",
Magnus Damm4a256b52009-07-08 13:22:18 +02001608 .pm = &dw_dev_pm_ops,
Viresh Kumard3f797d2012-04-20 20:15:34 +05301609 .of_match_table = of_match_ptr(dw_dma_id_table),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001610 },
1611};
1612
1613static int __init dw_init(void)
1614{
1615 return platform_driver_probe(&dw_driver, dw_probe);
1616}
Viresh Kumarcb689a72011-03-03 15:47:15 +05301617subsys_initcall(dw_init);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001618
1619static void __exit dw_exit(void)
1620{
1621 platform_driver_unregister(&dw_driver);
1622}
1623module_exit(dw_exit);
1624
1625MODULE_LICENSE("GPL v2");
1626MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001627MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Viresh Kumaraecb7b62011-05-24 14:04:09 +05301628MODULE_AUTHOR("Viresh Kumar <viresh.kumar@st.com>");