blob: c4b0eb3cde819977fbf2ac45cfb1b588ccc04f37 [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
Andy Shevchenkoa0982002012-09-21 15:05:48 +030039static inline unsigned int dwc_get_dms(struct dw_dma_slave *slave)
40{
41 return slave ? slave->dst_master : 0;
42}
43
44static inline unsigned int dwc_get_sms(struct dw_dma_slave *slave)
45{
46 return slave ? slave->src_master : 1;
47}
48
Viresh Kumar327e6972012-02-01 16:12:26 +053049#define DWC_DEFAULT_CTLLO(_chan) ({ \
50 struct dw_dma_slave *__slave = (_chan->private); \
51 struct dw_dma_chan *_dwc = to_dw_dma_chan(_chan); \
52 struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \
Andy Shevchenkoa0982002012-09-21 15:05:48 +030053 int _dms = dwc_get_dms(__slave); \
54 int _sms = dwc_get_sms(__slave); \
Viresh Kumar327e6972012-02-01 16:12:26 +053055 u8 _smsize = __slave ? _sconfig->src_maxburst : \
56 DW_DMA_MSIZE_16; \
57 u8 _dmsize = __slave ? _sconfig->dst_maxburst : \
58 DW_DMA_MSIZE_16; \
Jamie Ilesf301c062011-01-21 14:11:53 +000059 \
Viresh Kumar327e6972012-02-01 16:12:26 +053060 (DWC_CTLL_DST_MSIZE(_dmsize) \
61 | DWC_CTLL_SRC_MSIZE(_smsize) \
Jamie Ilesf301c062011-01-21 14:11:53 +000062 | DWC_CTLL_LLP_D_EN \
63 | DWC_CTLL_LLP_S_EN \
Viresh Kumar327e6972012-02-01 16:12:26 +053064 | DWC_CTLL_DMS(_dms) \
65 | DWC_CTLL_SMS(_sms)); \
Jamie Ilesf301c062011-01-21 14:11:53 +000066 })
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070067
68/*
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070069 * 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;
Andy Shevchenko8fccc5b2012-09-03 13:46:19 +0300180 } else {
181 if (dwc->dma_sconfig.direction == DMA_MEM_TO_DEV)
182 cfghi = DWC_CFGH_DST_PER(dwc->dma_sconfig.slave_id);
183 else if (dwc->dma_sconfig.direction == DMA_DEV_TO_MEM)
184 cfghi = DWC_CFGH_SRC_PER(dwc->dma_sconfig.slave_id);
Viresh Kumar61e183f2011-11-17 16:01:29 +0530185 }
186
187 channel_writel(dwc, CFG_LO, cfglo);
188 channel_writel(dwc, CFG_HI, cfghi);
189
190 /* Enable interrupts */
191 channel_set_bit(dw, MASK.XFER, dwc->mask);
Viresh Kumar61e183f2011-11-17 16:01:29 +0530192 channel_set_bit(dw, MASK.ERROR, dwc->mask);
193
194 dwc->initialized = true;
195}
196
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700197/*----------------------------------------------------------------------*/
198
Andy Shevchenko4c2d56c2012-06-19 13:34:08 +0300199static inline unsigned int dwc_fast_fls(unsigned long long v)
200{
201 /*
202 * We can be a lot more clever here, but this should take care
203 * of the most common optimization.
204 */
205 if (!(v & 7))
206 return 3;
207 else if (!(v & 3))
208 return 2;
209 else if (!(v & 1))
210 return 1;
211 return 0;
212}
213
Andy Shevchenkof52b36d2012-09-21 15:05:44 +0300214static inline void dwc_dump_chan_regs(struct dw_dma_chan *dwc)
Andy Shevchenko1d455432012-06-19 13:34:03 +0300215{
216 dev_err(chan2dev(&dwc->chan),
217 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
218 channel_readl(dwc, SAR),
219 channel_readl(dwc, DAR),
220 channel_readl(dwc, LLP),
221 channel_readl(dwc, CTL_HI),
222 channel_readl(dwc, CTL_LO));
223}
224
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300225
226static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc)
227{
228 channel_clear_bit(dw, CH_EN, dwc->mask);
229 while (dma_readl(dw, CH_EN) & dwc->mask)
230 cpu_relax();
231}
232
Andy Shevchenko1d455432012-06-19 13:34:03 +0300233/*----------------------------------------------------------------------*/
234
Andy Shevchenkofed25742012-09-21 15:05:49 +0300235/* Perform single block transfer */
236static inline void dwc_do_single_block(struct dw_dma_chan *dwc,
237 struct dw_desc *desc)
238{
239 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
240 u32 ctllo;
241
242 /* Software emulation of LLP mode relies on interrupts to continue
243 * multi block transfer. */
244 ctllo = desc->lli.ctllo | DWC_CTLL_INT_EN;
245
246 channel_writel(dwc, SAR, desc->lli.sar);
247 channel_writel(dwc, DAR, desc->lli.dar);
248 channel_writel(dwc, CTL_LO, ctllo);
249 channel_writel(dwc, CTL_HI, desc->lli.ctlhi);
250 channel_set_bit(dw, CH_EN, dwc->mask);
251}
252
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700253/* Called with dwc->lock held and bh disabled */
254static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
255{
256 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Andy Shevchenkofed25742012-09-21 15:05:49 +0300257 unsigned long was_soft_llp;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700258
259 /* ASSERT: channel is idle */
260 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700261 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700262 "BUG: Attempted to start non-idle channel\n");
Andy Shevchenko1d455432012-06-19 13:34:03 +0300263 dwc_dump_chan_regs(dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700264
265 /* The tasklet will hopefully advance the queue... */
266 return;
267 }
268
Andy Shevchenkofed25742012-09-21 15:05:49 +0300269 if (dwc->nollp) {
270 was_soft_llp = test_and_set_bit(DW_DMA_IS_SOFT_LLP,
271 &dwc->flags);
272 if (was_soft_llp) {
273 dev_err(chan2dev(&dwc->chan),
274 "BUG: Attempted to start new LLP transfer "
275 "inside ongoing one\n");
276 return;
277 }
278
279 dwc_initialize(dwc);
280
281 dwc->tx_list = &first->tx_list;
282 dwc->tx_node_active = first->tx_list.next;
283
284 dwc_do_single_block(dwc, first);
285
286 return;
287 }
288
Viresh Kumar61e183f2011-11-17 16:01:29 +0530289 dwc_initialize(dwc);
290
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700291 channel_writel(dwc, LLP, first->txd.phys);
292 channel_writel(dwc, CTL_LO,
293 DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
294 channel_writel(dwc, CTL_HI, 0);
295 channel_set_bit(dw, CH_EN, dwc->mask);
296}
297
298/*----------------------------------------------------------------------*/
299
300static void
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530301dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
302 bool callback_required)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700303{
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530304 dma_async_tx_callback callback = NULL;
305 void *param = NULL;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700306 struct dma_async_tx_descriptor *txd = &desc->txd;
Viresh Kumare5180762011-03-03 15:47:20 +0530307 struct dw_desc *child;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530308 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700309
Dan Williams41d5e592009-01-06 11:38:21 -0700310 dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700311
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530312 spin_lock_irqsave(&dwc->lock, flags);
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +0000313 dma_cookie_complete(txd);
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530314 if (callback_required) {
315 callback = txd->callback;
316 param = txd->callback_param;
317 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700318
319 dwc_sync_desc_for_cpu(dwc, desc);
Viresh Kumare5180762011-03-03 15:47:20 +0530320
321 /* async_tx_ack */
322 list_for_each_entry(child, &desc->tx_list, desc_node)
323 async_tx_ack(&child->txd);
324 async_tx_ack(&desc->txd);
325
Dan Williamse0bd0f82009-09-08 17:53:02 -0700326 list_splice_init(&desc->tx_list, &dwc->free_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700327 list_move(&desc->desc_node, &dwc->free_list);
328
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700329 if (!dwc->chan.private) {
330 struct device *parent = chan2parent(&dwc->chan);
331 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
332 if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
333 dma_unmap_single(parent, desc->lli.dar,
334 desc->len, DMA_FROM_DEVICE);
335 else
336 dma_unmap_page(parent, desc->lli.dar,
337 desc->len, DMA_FROM_DEVICE);
338 }
339 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
340 if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
341 dma_unmap_single(parent, desc->lli.sar,
342 desc->len, DMA_TO_DEVICE);
343 else
344 dma_unmap_page(parent, desc->lli.sar,
345 desc->len, DMA_TO_DEVICE);
346 }
347 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700348
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530349 spin_unlock_irqrestore(&dwc->lock, flags);
350
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530351 if (callback_required && callback)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700352 callback(param);
353}
354
355static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
356{
357 struct dw_desc *desc, *_desc;
358 LIST_HEAD(list);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530359 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700360
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530361 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700362 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700363 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700364 "BUG: XFER bit set, but channel not idle!\n");
365
366 /* Try to continue after resetting the channel... */
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300367 dwc_chan_disable(dw, dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700368 }
369
370 /*
371 * Submit queued descriptors ASAP, i.e. before we go through
372 * the completed ones.
373 */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700374 list_splice_init(&dwc->active_list, &list);
Viresh Kumarf336e422011-03-03 15:47:16 +0530375 if (!list_empty(&dwc->queue)) {
376 list_move(dwc->queue.next, &dwc->active_list);
377 dwc_dostart(dwc, dwc_first_active(dwc));
378 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700379
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530380 spin_unlock_irqrestore(&dwc->lock, flags);
381
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700382 list_for_each_entry_safe(desc, _desc, &list, desc_node)
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530383 dwc_descriptor_complete(dwc, desc, true);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700384}
385
386static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
387{
388 dma_addr_t llp;
389 struct dw_desc *desc, *_desc;
390 struct dw_desc *child;
391 u32 status_xfer;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530392 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700393
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530394 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700395 llp = channel_readl(dwc, LLP);
396 status_xfer = dma_readl(dw, RAW.XFER);
397
398 if (status_xfer & dwc->mask) {
399 /* Everything we've submitted is done */
400 dma_writel(dw, CLEAR.XFER, dwc->mask);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530401 spin_unlock_irqrestore(&dwc->lock, flags);
402
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700403 dwc_complete_all(dw, dwc);
404 return;
405 }
406
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530407 if (list_empty(&dwc->active_list)) {
408 spin_unlock_irqrestore(&dwc->lock, flags);
Jamie Iles087809f2011-01-21 14:11:52 +0000409 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530410 }
Jamie Iles087809f2011-01-21 14:11:52 +0000411
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300412 dev_vdbg(chan2dev(&dwc->chan), "%s: llp=0x%llx\n", __func__,
Andy Shevchenko2f45d612012-06-19 13:34:02 +0300413 (unsigned long long)llp);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700414
415 list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
Viresh Kumar84adccf2011-03-24 11:32:15 +0530416 /* check first descriptors addr */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530417 if (desc->txd.phys == llp) {
418 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700419 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530420 }
Viresh Kumar84adccf2011-03-24 11:32:15 +0530421
422 /* check first descriptors llp */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530423 if (desc->lli.llp == llp) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700424 /* This one is currently in progress */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530425 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700426 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530427 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700428
Dan Williamse0bd0f82009-09-08 17:53:02 -0700429 list_for_each_entry(child, &desc->tx_list, desc_node)
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530430 if (child->lli.llp == llp) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700431 /* Currently in progress */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530432 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700433 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530434 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700435
436 /*
437 * No descriptors so far seem to be in progress, i.e.
438 * this one must be done.
439 */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530440 spin_unlock_irqrestore(&dwc->lock, flags);
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530441 dwc_descriptor_complete(dwc, desc, true);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530442 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700443 }
444
Dan Williams41d5e592009-01-06 11:38:21 -0700445 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700446 "BUG: All descriptors done, but channel not idle!\n");
447
448 /* Try to continue after resetting the channel... */
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300449 dwc_chan_disable(dw, dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700450
451 if (!list_empty(&dwc->queue)) {
Viresh Kumarf336e422011-03-03 15:47:16 +0530452 list_move(dwc->queue.next, &dwc->active_list);
453 dwc_dostart(dwc, dwc_first_active(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700454 }
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530455 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700456}
457
Andy Shevchenko93aad1b2012-07-13 11:09:32 +0300458static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700459{
Dan Williams41d5e592009-01-06 11:38:21 -0700460 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700461 " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
Andy Shevchenkof8609c22012-07-13 11:09:33 +0300462 lli->sar, lli->dar, lli->llp, lli->ctlhi, lli->ctllo);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700463}
464
465static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
466{
467 struct dw_desc *bad_desc;
468 struct dw_desc *child;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530469 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700470
471 dwc_scan_descriptors(dw, dwc);
472
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530473 spin_lock_irqsave(&dwc->lock, flags);
474
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700475 /*
476 * The descriptor currently at the head of the active list is
477 * borked. Since we don't have any way to report errors, we'll
478 * just have to scream loudly and try to carry on.
479 */
480 bad_desc = dwc_first_active(dwc);
481 list_del_init(&bad_desc->desc_node);
Viresh Kumarf336e422011-03-03 15:47:16 +0530482 list_move(dwc->queue.next, dwc->active_list.prev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700483
484 /* Clear the error flag and try to restart the controller */
485 dma_writel(dw, CLEAR.ERROR, dwc->mask);
486 if (!list_empty(&dwc->active_list))
487 dwc_dostart(dwc, dwc_first_active(dwc));
488
489 /*
490 * KERN_CRITICAL may seem harsh, but since this only happens
491 * when someone submits a bad physical address in a
492 * descriptor, we should consider ourselves lucky that the
493 * controller flagged an error instead of scribbling over
494 * random memory locations.
495 */
Dan Williams41d5e592009-01-06 11:38:21 -0700496 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700497 "Bad descriptor submitted for DMA!\n");
Dan Williams41d5e592009-01-06 11:38:21 -0700498 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700499 " cookie: %d\n", bad_desc->txd.cookie);
500 dwc_dump_lli(dwc, &bad_desc->lli);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700501 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700502 dwc_dump_lli(dwc, &child->lli);
503
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530504 spin_unlock_irqrestore(&dwc->lock, flags);
505
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700506 /* Pretend the descriptor completed successfully */
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530507 dwc_descriptor_complete(dwc, bad_desc, true);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700508}
509
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200510/* --------------------- Cyclic DMA API extensions -------------------- */
511
512inline dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
513{
514 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
515 return channel_readl(dwc, SAR);
516}
517EXPORT_SYMBOL(dw_dma_get_src_addr);
518
519inline dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
520{
521 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
522 return channel_readl(dwc, DAR);
523}
524EXPORT_SYMBOL(dw_dma_get_dst_addr);
525
526/* called with dwc->lock held and all DMAC interrupts disabled */
527static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530528 u32 status_err, u32 status_xfer)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200529{
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530530 unsigned long flags;
531
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530532 if (dwc->mask) {
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200533 void (*callback)(void *param);
534 void *callback_param;
535
536 dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
537 channel_readl(dwc, LLP));
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200538
539 callback = dwc->cdesc->period_callback;
540 callback_param = dwc->cdesc->period_callback_param;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530541
542 if (callback)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200543 callback(callback_param);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200544 }
545
546 /*
547 * Error and transfer complete are highly unlikely, and will most
548 * likely be due to a configuration error by the user.
549 */
550 if (unlikely(status_err & dwc->mask) ||
551 unlikely(status_xfer & dwc->mask)) {
552 int i;
553
554 dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s "
555 "interrupt, stopping DMA transfer\n",
556 status_xfer ? "xfer" : "error");
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530557
558 spin_lock_irqsave(&dwc->lock, flags);
559
Andy Shevchenko1d455432012-06-19 13:34:03 +0300560 dwc_dump_chan_regs(dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200561
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300562 dwc_chan_disable(dw, dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200563
564 /* make sure DMA does not restart by loading a new list */
565 channel_writel(dwc, LLP, 0);
566 channel_writel(dwc, CTL_LO, 0);
567 channel_writel(dwc, CTL_HI, 0);
568
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200569 dma_writel(dw, CLEAR.ERROR, dwc->mask);
570 dma_writel(dw, CLEAR.XFER, dwc->mask);
571
572 for (i = 0; i < dwc->cdesc->periods; i++)
573 dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530574
575 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200576 }
577}
578
579/* ------------------------------------------------------------------------- */
580
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700581static void dw_dma_tasklet(unsigned long data)
582{
583 struct dw_dma *dw = (struct dw_dma *)data;
584 struct dw_dma_chan *dwc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700585 u32 status_xfer;
586 u32 status_err;
587 int i;
588
Haavard Skinnemoen7fe7b2f2008-10-03 15:23:46 -0700589 status_xfer = dma_readl(dw, RAW.XFER);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700590 status_err = dma_readl(dw, RAW.ERROR);
591
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300592 dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700593
594 for (i = 0; i < dw->dma.chancnt; i++) {
595 dwc = &dw->chan[i];
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200596 if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530597 dwc_handle_cyclic(dw, dwc, status_err, status_xfer);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200598 else if (status_err & (1 << i))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700599 dwc_handle_error(dw, dwc);
Andy Shevchenkofed25742012-09-21 15:05:49 +0300600 else if (status_xfer & (1 << i)) {
601 unsigned long flags;
602
603 spin_lock_irqsave(&dwc->lock, flags);
604 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
605 if (dwc->tx_node_active != dwc->tx_list) {
606 struct dw_desc *desc =
607 list_entry(dwc->tx_node_active,
608 struct dw_desc,
609 desc_node);
610
611 dma_writel(dw, CLEAR.XFER, dwc->mask);
612
613 /* move pointer to next descriptor */
614 dwc->tx_node_active =
615 dwc->tx_node_active->next;
616
617 dwc_do_single_block(dwc, desc);
618
619 spin_unlock_irqrestore(&dwc->lock, flags);
620 continue;
621 } else {
622 /* we are done here */
623 clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
624 }
625 }
626 spin_unlock_irqrestore(&dwc->lock, flags);
627
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700628 dwc_scan_descriptors(dw, dwc);
Andy Shevchenkofed25742012-09-21 15:05:49 +0300629 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700630 }
631
632 /*
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530633 * Re-enable interrupts.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700634 */
635 channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700636 channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
637}
638
639static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
640{
641 struct dw_dma *dw = dev_id;
642 u32 status;
643
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300644 dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700645 dma_readl(dw, STATUS_INT));
646
647 /*
648 * Just disable the interrupts. We'll turn them back on in the
649 * softirq handler.
650 */
651 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700652 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
653
654 status = dma_readl(dw, STATUS_INT);
655 if (status) {
656 dev_err(dw->dma.dev,
657 "BUG: Unexpected interrupts pending: 0x%x\n",
658 status);
659
660 /* Try to recover */
661 channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700662 channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
663 channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
664 channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
665 }
666
667 tasklet_schedule(&dw->tasklet);
668
669 return IRQ_HANDLED;
670}
671
672/*----------------------------------------------------------------------*/
673
674static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
675{
676 struct dw_desc *desc = txd_to_dw_desc(tx);
677 struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
678 dma_cookie_t cookie;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530679 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700680
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530681 spin_lock_irqsave(&dwc->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000682 cookie = dma_cookie_assign(tx);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700683
684 /*
685 * REVISIT: We should attempt to chain as many descriptors as
686 * possible, perhaps even appending to those already submitted
687 * for DMA. But this is hard to do in a race-free manner.
688 */
689 if (list_empty(&dwc->active_list)) {
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300690 dev_vdbg(chan2dev(tx->chan), "%s: started %u\n", __func__,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700691 desc->txd.cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700692 list_add_tail(&desc->desc_node, &dwc->active_list);
Viresh Kumarf336e422011-03-03 15:47:16 +0530693 dwc_dostart(dwc, dwc_first_active(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700694 } else {
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300695 dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n", __func__,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700696 desc->txd.cookie);
697
698 list_add_tail(&desc->desc_node, &dwc->queue);
699 }
700
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530701 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700702
703 return cookie;
704}
705
706static struct dma_async_tx_descriptor *
707dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
708 size_t len, unsigned long flags)
709{
710 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300711 struct dw_dma_slave *dws = chan->private;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700712 struct dw_desc *desc;
713 struct dw_desc *first;
714 struct dw_desc *prev;
715 size_t xfer_count;
716 size_t offset;
717 unsigned int src_width;
718 unsigned int dst_width;
Andy Shevchenko3d4f8602012-10-01 13:06:25 +0300719 unsigned int data_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700720 u32 ctllo;
721
Andy Shevchenko2f45d612012-06-19 13:34:02 +0300722 dev_vdbg(chan2dev(chan),
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300723 "%s: d0x%llx s0x%llx l0x%zx f0x%lx\n", __func__,
Andy Shevchenko2f45d612012-06-19 13:34:02 +0300724 (unsigned long long)dest, (unsigned long long)src,
725 len, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700726
727 if (unlikely(!len)) {
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300728 dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700729 return NULL;
730 }
731
Andy Shevchenko3d4f8602012-10-01 13:06:25 +0300732 data_width = min_t(unsigned int, dwc->dw->data_width[dwc_get_sms(dws)],
733 dwc->dw->data_width[dwc_get_dms(dws)]);
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300734
Andy Shevchenko3d4f8602012-10-01 13:06:25 +0300735 src_width = dst_width = min_t(unsigned int, data_width,
736 dwc_fast_fls(src | dest | len));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700737
Viresh Kumar327e6972012-02-01 16:12:26 +0530738 ctllo = DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700739 | DWC_CTLL_DST_WIDTH(dst_width)
740 | DWC_CTLL_SRC_WIDTH(src_width)
741 | DWC_CTLL_DST_INC
742 | DWC_CTLL_SRC_INC
743 | DWC_CTLL_FC_M2M;
744 prev = first = NULL;
745
746 for (offset = 0; offset < len; offset += xfer_count << src_width) {
747 xfer_count = min_t(size_t, (len - offset) >> src_width,
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +0300748 dwc->block_size);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700749
750 desc = dwc_desc_get(dwc);
751 if (!desc)
752 goto err_desc_get;
753
754 desc->lli.sar = src + offset;
755 desc->lli.dar = dest + offset;
756 desc->lli.ctllo = ctllo;
757 desc->lli.ctlhi = xfer_count;
758
759 if (!first) {
760 first = desc;
761 } else {
762 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700763 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700764 prev->txd.phys, sizeof(prev->lli),
765 DMA_TO_DEVICE);
766 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700767 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700768 }
769 prev = desc;
770 }
771
772
773 if (flags & DMA_PREP_INTERRUPT)
774 /* Trigger interrupt after last block */
775 prev->lli.ctllo |= DWC_CTLL_INT_EN;
776
777 prev->lli.llp = 0;
Dan Williams41d5e592009-01-06 11:38:21 -0700778 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700779 prev->txd.phys, sizeof(prev->lli),
780 DMA_TO_DEVICE);
781
782 first->txd.flags = flags;
783 first->len = len;
784
785 return &first->txd;
786
787err_desc_get:
788 dwc_desc_put(dwc, first);
789 return NULL;
790}
791
792static struct dma_async_tx_descriptor *
793dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530794 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500795 unsigned long flags, void *context)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700796{
797 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Dan Williams287d8592009-02-18 14:48:26 -0800798 struct dw_dma_slave *dws = chan->private;
Viresh Kumar327e6972012-02-01 16:12:26 +0530799 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700800 struct dw_desc *prev;
801 struct dw_desc *first;
802 u32 ctllo;
803 dma_addr_t reg;
804 unsigned int reg_width;
805 unsigned int mem_width;
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300806 unsigned int data_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700807 unsigned int i;
808 struct scatterlist *sg;
809 size_t total_len = 0;
810
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300811 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700812
813 if (unlikely(!dws || !sg_len))
814 return NULL;
815
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700816 prev = first = NULL;
817
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700818 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530819 case DMA_MEM_TO_DEV:
Viresh Kumar327e6972012-02-01 16:12:26 +0530820 reg_width = __fls(sconfig->dst_addr_width);
821 reg = sconfig->dst_addr;
822 ctllo = (DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700823 | DWC_CTLL_DST_WIDTH(reg_width)
824 | DWC_CTLL_DST_FIX
Viresh Kumar327e6972012-02-01 16:12:26 +0530825 | DWC_CTLL_SRC_INC);
826
827 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
828 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
829
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300830 data_width = dwc->dw->data_width[dwc_get_sms(dws)];
831
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700832 for_each_sg(sgl, sg, sg_len, i) {
833 struct dw_desc *desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530834 u32 len, dlen, mem;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700835
Lars-Peter Clausencbb796c2012-04-25 20:50:51 +0200836 mem = sg_dma_address(sg);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700837 len = sg_dma_len(sg);
Viresh Kumar6bc711f2012-02-01 16:12:25 +0530838
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300839 mem_width = min_t(unsigned int,
840 data_width, dwc_fast_fls(mem | len));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700841
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530842slave_sg_todev_fill_desc:
843 desc = dwc_desc_get(dwc);
844 if (!desc) {
845 dev_err(chan2dev(chan),
846 "not enough descriptors available\n");
847 goto err_desc_get;
848 }
849
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700850 desc->lli.sar = mem;
851 desc->lli.dar = reg;
852 desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +0300853 if ((len >> mem_width) > dwc->block_size) {
854 dlen = dwc->block_size << mem_width;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530855 mem += dlen;
856 len -= dlen;
857 } else {
858 dlen = len;
859 len = 0;
860 }
861
862 desc->lli.ctlhi = dlen >> mem_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700863
864 if (!first) {
865 first = desc;
866 } else {
867 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700868 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700869 prev->txd.phys,
870 sizeof(prev->lli),
871 DMA_TO_DEVICE);
872 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700873 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700874 }
875 prev = desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530876 total_len += dlen;
877
878 if (len)
879 goto slave_sg_todev_fill_desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700880 }
881 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530882 case DMA_DEV_TO_MEM:
Viresh Kumar327e6972012-02-01 16:12:26 +0530883 reg_width = __fls(sconfig->src_addr_width);
884 reg = sconfig->src_addr;
885 ctllo = (DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700886 | DWC_CTLL_SRC_WIDTH(reg_width)
887 | DWC_CTLL_DST_INC
Viresh Kumar327e6972012-02-01 16:12:26 +0530888 | DWC_CTLL_SRC_FIX);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700889
Viresh Kumar327e6972012-02-01 16:12:26 +0530890 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
891 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
892
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300893 data_width = dwc->dw->data_width[dwc_get_dms(dws)];
894
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700895 for_each_sg(sgl, sg, sg_len, i) {
896 struct dw_desc *desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530897 u32 len, dlen, mem;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700898
Lars-Peter Clausencbb796c2012-04-25 20:50:51 +0200899 mem = sg_dma_address(sg);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700900 len = sg_dma_len(sg);
Viresh Kumar6bc711f2012-02-01 16:12:25 +0530901
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300902 mem_width = min_t(unsigned int,
903 data_width, dwc_fast_fls(mem | len));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700904
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530905slave_sg_fromdev_fill_desc:
906 desc = dwc_desc_get(dwc);
907 if (!desc) {
908 dev_err(chan2dev(chan),
909 "not enough descriptors available\n");
910 goto err_desc_get;
911 }
912
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700913 desc->lli.sar = reg;
914 desc->lli.dar = mem;
915 desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +0300916 if ((len >> reg_width) > dwc->block_size) {
917 dlen = dwc->block_size << reg_width;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530918 mem += dlen;
919 len -= dlen;
920 } else {
921 dlen = len;
922 len = 0;
923 }
924 desc->lli.ctlhi = dlen >> reg_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700925
926 if (!first) {
927 first = desc;
928 } else {
929 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700930 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700931 prev->txd.phys,
932 sizeof(prev->lli),
933 DMA_TO_DEVICE);
934 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700935 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700936 }
937 prev = desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530938 total_len += dlen;
939
940 if (len)
941 goto slave_sg_fromdev_fill_desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700942 }
943 break;
944 default:
945 return NULL;
946 }
947
948 if (flags & DMA_PREP_INTERRUPT)
949 /* Trigger interrupt after last block */
950 prev->lli.ctllo |= DWC_CTLL_INT_EN;
951
952 prev->lli.llp = 0;
Dan Williams41d5e592009-01-06 11:38:21 -0700953 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700954 prev->txd.phys, sizeof(prev->lli),
955 DMA_TO_DEVICE);
956
957 first->len = total_len;
958
959 return &first->txd;
960
961err_desc_get:
962 dwc_desc_put(dwc, first);
963 return NULL;
964}
965
Viresh Kumar327e6972012-02-01 16:12:26 +0530966/*
967 * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
968 * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
969 *
970 * NOTE: burst size 2 is not supported by controller.
971 *
972 * This can be done by finding least significant bit set: n & (n - 1)
973 */
974static inline void convert_burst(u32 *maxburst)
975{
976 if (*maxburst > 1)
977 *maxburst = fls(*maxburst) - 2;
978 else
979 *maxburst = 0;
980}
981
982static int
983set_runtime_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
984{
985 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
986
987 /* Check if it is chan is configured for slave transfers */
988 if (!chan->private)
989 return -EINVAL;
990
991 memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
992
993 convert_burst(&dwc->dma_sconfig.src_maxburst);
994 convert_burst(&dwc->dma_sconfig.dst_maxburst);
995
996 return 0;
997}
998
Linus Walleij05827632010-05-17 16:30:42 -0700999static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
1000 unsigned long arg)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001001{
1002 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1003 struct dw_dma *dw = to_dw_dma(chan->device);
1004 struct dw_desc *desc, *_desc;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301005 unsigned long flags;
Linus Walleija7c57cf2011-04-19 08:31:32 +08001006 u32 cfglo;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001007 LIST_HEAD(list);
1008
Linus Walleija7c57cf2011-04-19 08:31:32 +08001009 if (cmd == DMA_PAUSE) {
1010 spin_lock_irqsave(&dwc->lock, flags);
1011
1012 cfglo = channel_readl(dwc, CFG_LO);
1013 channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
1014 while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY))
1015 cpu_relax();
1016
1017 dwc->paused = true;
1018 spin_unlock_irqrestore(&dwc->lock, flags);
1019 } else if (cmd == DMA_RESUME) {
1020 if (!dwc->paused)
1021 return 0;
1022
1023 spin_lock_irqsave(&dwc->lock, flags);
1024
1025 cfglo = channel_readl(dwc, CFG_LO);
1026 channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
1027 dwc->paused = false;
1028
1029 spin_unlock_irqrestore(&dwc->lock, flags);
1030 } else if (cmd == DMA_TERMINATE_ALL) {
1031 spin_lock_irqsave(&dwc->lock, flags);
1032
Andy Shevchenkofed25742012-09-21 15:05:49 +03001033 clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
1034
Andy Shevchenko3f9362072012-06-19 13:46:32 +03001035 dwc_chan_disable(dw, dwc);
Linus Walleija7c57cf2011-04-19 08:31:32 +08001036
1037 dwc->paused = false;
1038
1039 /* active_list entries will end up before queued entries */
1040 list_splice_init(&dwc->queue, &list);
1041 list_splice_init(&dwc->active_list, &list);
1042
1043 spin_unlock_irqrestore(&dwc->lock, flags);
1044
1045 /* Flush all pending and queued descriptors */
1046 list_for_each_entry_safe(desc, _desc, &list, desc_node)
1047 dwc_descriptor_complete(dwc, desc, false);
Viresh Kumar327e6972012-02-01 16:12:26 +05301048 } else if (cmd == DMA_SLAVE_CONFIG) {
1049 return set_runtime_config(chan, (struct dma_slave_config *)arg);
1050 } else {
Linus Walleijc3635c72010-03-26 16:44:01 -07001051 return -ENXIO;
Viresh Kumar327e6972012-02-01 16:12:26 +05301052 }
Linus Walleijc3635c72010-03-26 16:44:01 -07001053
Linus Walleijc3635c72010-03-26 16:44:01 -07001054 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001055}
1056
1057static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -07001058dwc_tx_status(struct dma_chan *chan,
1059 dma_cookie_t cookie,
1060 struct dma_tx_state *txstate)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001061{
1062 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001063 enum dma_status ret;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001064
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001065 ret = dma_cookie_status(chan, cookie, txstate);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001066 if (ret != DMA_SUCCESS) {
1067 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
1068
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001069 ret = dma_cookie_status(chan, cookie, txstate);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001070 }
1071
Viresh Kumarabf53902011-04-15 16:03:35 +05301072 if (ret != DMA_SUCCESS)
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001073 dma_set_residue(txstate, dwc_first_active(dwc)->len);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001074
Linus Walleija7c57cf2011-04-19 08:31:32 +08001075 if (dwc->paused)
1076 return DMA_PAUSED;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001077
1078 return ret;
1079}
1080
1081static void dwc_issue_pending(struct dma_chan *chan)
1082{
1083 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1084
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001085 if (!list_empty(&dwc->queue))
1086 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001087}
1088
Dan Williamsaa1e6f12009-01-06 11:38:17 -07001089static int dwc_alloc_chan_resources(struct dma_chan *chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001090{
1091 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1092 struct dw_dma *dw = to_dw_dma(chan->device);
1093 struct dw_desc *desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001094 int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301095 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001096
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001097 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001098
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001099 /* ASSERT: channel is idle */
1100 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -07001101 dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001102 return -EIO;
1103 }
1104
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001105 dma_cookie_init(chan);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001106
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001107 /*
1108 * NOTE: some controllers may have additional features that we
1109 * need to initialize here, like "scatter-gather" (which
1110 * doesn't mean what you think it means), and status writeback.
1111 */
1112
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301113 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001114 i = dwc->descs_allocated;
1115 while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301116 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001117
1118 desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL);
1119 if (!desc) {
Dan Williams41d5e592009-01-06 11:38:21 -07001120 dev_info(chan2dev(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001121 "only allocated %d descriptors\n", i);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301122 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001123 break;
1124 }
1125
Dan Williamse0bd0f82009-09-08 17:53:02 -07001126 INIT_LIST_HEAD(&desc->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001127 dma_async_tx_descriptor_init(&desc->txd, chan);
1128 desc->txd.tx_submit = dwc_tx_submit;
1129 desc->txd.flags = DMA_CTRL_ACK;
Dan Williams41d5e592009-01-06 11:38:21 -07001130 desc->txd.phys = dma_map_single(chan2parent(chan), &desc->lli,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001131 sizeof(desc->lli), DMA_TO_DEVICE);
1132 dwc_desc_put(dwc, desc);
1133
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301134 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001135 i = ++dwc->descs_allocated;
1136 }
1137
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301138 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001139
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001140 dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001141
1142 return i;
1143}
1144
1145static void dwc_free_chan_resources(struct dma_chan *chan)
1146{
1147 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1148 struct dw_dma *dw = to_dw_dma(chan->device);
1149 struct dw_desc *desc, *_desc;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301150 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001151 LIST_HEAD(list);
1152
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001153 dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001154 dwc->descs_allocated);
1155
1156 /* ASSERT: channel is idle */
1157 BUG_ON(!list_empty(&dwc->active_list));
1158 BUG_ON(!list_empty(&dwc->queue));
1159 BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
1160
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301161 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001162 list_splice_init(&dwc->free_list, &list);
1163 dwc->descs_allocated = 0;
Viresh Kumar61e183f2011-11-17 16:01:29 +05301164 dwc->initialized = false;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001165
1166 /* Disable interrupts */
1167 channel_clear_bit(dw, MASK.XFER, dwc->mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001168 channel_clear_bit(dw, MASK.ERROR, dwc->mask);
1169
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301170 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001171
1172 list_for_each_entry_safe(desc, _desc, &list, desc_node) {
Dan Williams41d5e592009-01-06 11:38:21 -07001173 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1174 dma_unmap_single(chan2parent(chan), desc->txd.phys,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001175 sizeof(desc->lli), DMA_TO_DEVICE);
1176 kfree(desc);
1177 }
1178
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001179 dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001180}
1181
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001182/* --------------------- Cyclic DMA API extensions -------------------- */
1183
1184/**
1185 * dw_dma_cyclic_start - start the cyclic DMA transfer
1186 * @chan: the DMA channel to start
1187 *
1188 * Must be called with soft interrupts disabled. Returns zero on success or
1189 * -errno on failure.
1190 */
1191int dw_dma_cyclic_start(struct dma_chan *chan)
1192{
1193 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1194 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301195 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001196
1197 if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
1198 dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
1199 return -ENODEV;
1200 }
1201
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301202 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001203
1204 /* assert channel is idle */
1205 if (dma_readl(dw, CH_EN) & dwc->mask) {
1206 dev_err(chan2dev(&dwc->chan),
1207 "BUG: Attempted to start non-idle channel\n");
Andy Shevchenko1d455432012-06-19 13:34:03 +03001208 dwc_dump_chan_regs(dwc);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301209 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001210 return -EBUSY;
1211 }
1212
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001213 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1214 dma_writel(dw, CLEAR.XFER, dwc->mask);
1215
1216 /* setup DMAC channel registers */
1217 channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
1218 channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
1219 channel_writel(dwc, CTL_HI, 0);
1220
1221 channel_set_bit(dw, CH_EN, dwc->mask);
1222
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301223 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001224
1225 return 0;
1226}
1227EXPORT_SYMBOL(dw_dma_cyclic_start);
1228
1229/**
1230 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1231 * @chan: the DMA channel to stop
1232 *
1233 * Must be called with soft interrupts disabled.
1234 */
1235void dw_dma_cyclic_stop(struct dma_chan *chan)
1236{
1237 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1238 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301239 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001240
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301241 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001242
Andy Shevchenko3f9362072012-06-19 13:46:32 +03001243 dwc_chan_disable(dw, dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001244
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301245 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001246}
1247EXPORT_SYMBOL(dw_dma_cyclic_stop);
1248
1249/**
1250 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1251 * @chan: the DMA channel to prepare
1252 * @buf_addr: physical DMA address where the buffer starts
1253 * @buf_len: total number of bytes for the entire buffer
1254 * @period_len: number of bytes for each period
1255 * @direction: transfer direction, to or from device
1256 *
1257 * Must be called before trying to start the transfer. Returns a valid struct
1258 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1259 */
1260struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
1261 dma_addr_t buf_addr, size_t buf_len, size_t period_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05301262 enum dma_transfer_direction direction)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001263{
1264 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Viresh Kumar327e6972012-02-01 16:12:26 +05301265 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001266 struct dw_cyclic_desc *cdesc;
1267 struct dw_cyclic_desc *retval = NULL;
1268 struct dw_desc *desc;
1269 struct dw_desc *last = NULL;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001270 unsigned long was_cyclic;
1271 unsigned int reg_width;
1272 unsigned int periods;
1273 unsigned int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301274 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001275
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301276 spin_lock_irqsave(&dwc->lock, flags);
Andy Shevchenkofed25742012-09-21 15:05:49 +03001277 if (dwc->nollp) {
1278 spin_unlock_irqrestore(&dwc->lock, flags);
1279 dev_dbg(chan2dev(&dwc->chan),
1280 "channel doesn't support LLP transfers\n");
1281 return ERR_PTR(-EINVAL);
1282 }
1283
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001284 if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301285 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001286 dev_dbg(chan2dev(&dwc->chan),
1287 "queue and/or active list are not empty\n");
1288 return ERR_PTR(-EBUSY);
1289 }
1290
1291 was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301292 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001293 if (was_cyclic) {
1294 dev_dbg(chan2dev(&dwc->chan),
1295 "channel already prepared for cyclic DMA\n");
1296 return ERR_PTR(-EBUSY);
1297 }
1298
1299 retval = ERR_PTR(-EINVAL);
Viresh Kumar327e6972012-02-01 16:12:26 +05301300
1301 if (direction == DMA_MEM_TO_DEV)
1302 reg_width = __ffs(sconfig->dst_addr_width);
1303 else
1304 reg_width = __ffs(sconfig->src_addr_width);
1305
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001306 periods = buf_len / period_len;
1307
1308 /* Check for too big/unaligned periods and unaligned DMA buffer. */
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001309 if (period_len > (dwc->block_size << reg_width))
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001310 goto out_err;
1311 if (unlikely(period_len & ((1 << reg_width) - 1)))
1312 goto out_err;
1313 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
1314 goto out_err;
Vinod Kouldb8196d2011-10-13 22:34:23 +05301315 if (unlikely(!(direction & (DMA_MEM_TO_DEV | DMA_DEV_TO_MEM))))
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001316 goto out_err;
1317
1318 retval = ERR_PTR(-ENOMEM);
1319
1320 if (periods > NR_DESCS_PER_CHANNEL)
1321 goto out_err;
1322
1323 cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
1324 if (!cdesc)
1325 goto out_err;
1326
1327 cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
1328 if (!cdesc->desc)
1329 goto out_err_alloc;
1330
1331 for (i = 0; i < periods; i++) {
1332 desc = dwc_desc_get(dwc);
1333 if (!desc)
1334 goto out_err_desc_get;
1335
1336 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +05301337 case DMA_MEM_TO_DEV:
Viresh Kumar327e6972012-02-01 16:12:26 +05301338 desc->lli.dar = sconfig->dst_addr;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001339 desc->lli.sar = buf_addr + (period_len * i);
Viresh Kumar327e6972012-02-01 16:12:26 +05301340 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001341 | DWC_CTLL_DST_WIDTH(reg_width)
1342 | DWC_CTLL_SRC_WIDTH(reg_width)
1343 | DWC_CTLL_DST_FIX
1344 | DWC_CTLL_SRC_INC
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001345 | DWC_CTLL_INT_EN);
Viresh Kumar327e6972012-02-01 16:12:26 +05301346
1347 desc->lli.ctllo |= sconfig->device_fc ?
1348 DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
1349 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
1350
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001351 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +05301352 case DMA_DEV_TO_MEM:
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001353 desc->lli.dar = buf_addr + (period_len * i);
Viresh Kumar327e6972012-02-01 16:12:26 +05301354 desc->lli.sar = sconfig->src_addr;
1355 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001356 | DWC_CTLL_SRC_WIDTH(reg_width)
1357 | DWC_CTLL_DST_WIDTH(reg_width)
1358 | DWC_CTLL_DST_INC
1359 | DWC_CTLL_SRC_FIX
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001360 | DWC_CTLL_INT_EN);
Viresh Kumar327e6972012-02-01 16:12:26 +05301361
1362 desc->lli.ctllo |= sconfig->device_fc ?
1363 DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
1364 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
1365
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001366 break;
1367 default:
1368 break;
1369 }
1370
1371 desc->lli.ctlhi = (period_len >> reg_width);
1372 cdesc->desc[i] = desc;
1373
1374 if (last) {
1375 last->lli.llp = desc->txd.phys;
1376 dma_sync_single_for_device(chan2parent(chan),
1377 last->txd.phys, sizeof(last->lli),
1378 DMA_TO_DEVICE);
1379 }
1380
1381 last = desc;
1382 }
1383
1384 /* lets make a cyclic list */
1385 last->lli.llp = cdesc->desc[0]->txd.phys;
1386 dma_sync_single_for_device(chan2parent(chan), last->txd.phys,
1387 sizeof(last->lli), DMA_TO_DEVICE);
1388
Andy Shevchenko2f45d612012-06-19 13:34:02 +03001389 dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%llx len %zu "
1390 "period %zu periods %d\n", (unsigned long long)buf_addr,
1391 buf_len, period_len, periods);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001392
1393 cdesc->periods = periods;
1394 dwc->cdesc = cdesc;
1395
1396 return cdesc;
1397
1398out_err_desc_get:
1399 while (i--)
1400 dwc_desc_put(dwc, cdesc->desc[i]);
1401out_err_alloc:
1402 kfree(cdesc);
1403out_err:
1404 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1405 return (struct dw_cyclic_desc *)retval;
1406}
1407EXPORT_SYMBOL(dw_dma_cyclic_prep);
1408
1409/**
1410 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1411 * @chan: the DMA channel to free
1412 */
1413void dw_dma_cyclic_free(struct dma_chan *chan)
1414{
1415 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1416 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1417 struct dw_cyclic_desc *cdesc = dwc->cdesc;
1418 int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301419 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001420
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001421 dev_dbg(chan2dev(&dwc->chan), "%s\n", __func__);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001422
1423 if (!cdesc)
1424 return;
1425
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301426 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001427
Andy Shevchenko3f9362072012-06-19 13:46:32 +03001428 dwc_chan_disable(dw, dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001429
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001430 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1431 dma_writel(dw, CLEAR.XFER, dwc->mask);
1432
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301433 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001434
1435 for (i = 0; i < cdesc->periods; i++)
1436 dwc_desc_put(dwc, cdesc->desc[i]);
1437
1438 kfree(cdesc->desc);
1439 kfree(cdesc);
1440
1441 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1442}
1443EXPORT_SYMBOL(dw_dma_cyclic_free);
1444
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001445/*----------------------------------------------------------------------*/
1446
1447static void dw_dma_off(struct dw_dma *dw)
1448{
Viresh Kumar61e183f2011-11-17 16:01:29 +05301449 int i;
1450
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001451 dma_writel(dw, CFG, 0);
1452
1453 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001454 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1455 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1456 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1457
1458 while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
1459 cpu_relax();
Viresh Kumar61e183f2011-11-17 16:01:29 +05301460
1461 for (i = 0; i < dw->dma.chancnt; i++)
1462 dw->chan[i].initialized = false;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001463}
1464
Andy Shevchenko0272e932012-06-19 13:34:09 +03001465static int __devinit dw_probe(struct platform_device *pdev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001466{
1467 struct dw_dma_platform_data *pdata;
1468 struct resource *io;
1469 struct dw_dma *dw;
1470 size_t size;
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001471 void __iomem *regs;
1472 bool autocfg;
1473 unsigned int dw_params;
1474 unsigned int nr_channels;
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001475 unsigned int max_blk_size = 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001476 int irq;
1477 int err;
1478 int i;
1479
Viresh Kumar6c618c92012-02-01 16:12:22 +05301480 pdata = dev_get_platdata(&pdev->dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001481 if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
1482 return -EINVAL;
1483
1484 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1485 if (!io)
1486 return -EINVAL;
1487
1488 irq = platform_get_irq(pdev, 0);
1489 if (irq < 0)
1490 return irq;
1491
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001492 regs = devm_request_and_ioremap(&pdev->dev, io);
1493 if (!regs)
1494 return -EBUSY;
1495
1496 dw_params = dma_read_byaddr(regs, DW_PARAMS);
1497 autocfg = dw_params >> DW_PARAMS_EN & 0x1;
1498
1499 if (autocfg)
1500 nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 0x7) + 1;
1501 else
1502 nr_channels = pdata->nr_channels;
1503
1504 size = sizeof(struct dw_dma) + nr_channels * sizeof(struct dw_dma_chan);
Andy Shevchenkodbde5c22012-07-24 11:00:55 +03001505 dw = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001506 if (!dw)
1507 return -ENOMEM;
1508
Andy Shevchenkodbde5c22012-07-24 11:00:55 +03001509 dw->clk = devm_clk_get(&pdev->dev, "hclk");
1510 if (IS_ERR(dw->clk))
1511 return PTR_ERR(dw->clk);
Viresh Kumar30755282012-04-17 17:10:07 +05301512 clk_prepare_enable(dw->clk);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001513
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001514 dw->regs = regs;
1515
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001516 /* get hardware configuration parameters */
Andy Shevchenkoa0982002012-09-21 15:05:48 +03001517 if (autocfg) {
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001518 max_blk_size = dma_readl(dw, MAX_BLK_SIZE);
1519
Andy Shevchenkoa0982002012-09-21 15:05:48 +03001520 dw->nr_masters = (dw_params >> DW_PARAMS_NR_MASTER & 3) + 1;
1521 for (i = 0; i < dw->nr_masters; i++) {
1522 dw->data_width[i] =
1523 (dw_params >> DW_PARAMS_DATA_WIDTH(i) & 3) + 2;
1524 }
1525 } else {
1526 dw->nr_masters = pdata->nr_masters;
1527 memcpy(dw->data_width, pdata->data_width, 4);
1528 }
1529
Andy Shevchenko11f932e2012-06-19 13:34:06 +03001530 /* Calculate all channel mask before DMA setup */
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001531 dw->all_chan_mask = (1 << nr_channels) - 1;
Andy Shevchenko11f932e2012-06-19 13:34:06 +03001532
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001533 /* force dma off, just in case */
1534 dw_dma_off(dw);
1535
Andy Shevchenko236b1062012-06-19 13:34:07 +03001536 /* disable BLOCK interrupts as well */
1537 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
1538
Andy Shevchenkodbde5c22012-07-24 11:00:55 +03001539 err = devm_request_irq(&pdev->dev, irq, dw_dma_interrupt, 0,
1540 "dw_dmac", dw);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001541 if (err)
Andy Shevchenkodbde5c22012-07-24 11:00:55 +03001542 return err;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001543
1544 platform_set_drvdata(pdev, dw);
1545
1546 tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
1547
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001548 INIT_LIST_HEAD(&dw->dma.channels);
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001549 for (i = 0; i < nr_channels; i++) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001550 struct dw_dma_chan *dwc = &dw->chan[i];
Andy Shevchenkofed25742012-09-21 15:05:49 +03001551 int r = nr_channels - i - 1;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001552
1553 dwc->chan.device = &dw->dma;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001554 dma_cookie_init(&dwc->chan);
Viresh Kumarb0c31302011-03-03 15:47:21 +05301555 if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
1556 list_add_tail(&dwc->chan.device_node,
1557 &dw->dma.channels);
1558 else
1559 list_add(&dwc->chan.device_node, &dw->dma.channels);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001560
Viresh Kumar93317e82011-03-03 15:47:22 +05301561 /* 7 is highest priority & 0 is lowest. */
1562 if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
Andy Shevchenkofed25742012-09-21 15:05:49 +03001563 dwc->priority = r;
Viresh Kumar93317e82011-03-03 15:47:22 +05301564 else
1565 dwc->priority = i;
1566
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001567 dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
1568 spin_lock_init(&dwc->lock);
1569 dwc->mask = 1 << i;
1570
1571 INIT_LIST_HEAD(&dwc->active_list);
1572 INIT_LIST_HEAD(&dwc->queue);
1573 INIT_LIST_HEAD(&dwc->free_list);
1574
1575 channel_clear_bit(dw, CH_EN, dwc->mask);
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001576
Andy Shevchenkoa0982002012-09-21 15:05:48 +03001577 dwc->dw = dw;
1578
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001579 /* hardware configuration */
Andy Shevchenkofed25742012-09-21 15:05:49 +03001580 if (autocfg) {
1581 unsigned int dwc_params;
1582
1583 dwc_params = dma_read_byaddr(regs + r * sizeof(u32),
1584 DWC_PARAMS);
1585
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001586 /* Decode maximum block size for given channel. The
1587 * stored 4 bit value represents blocks from 0x00 for 3
1588 * up to 0x0a for 4095. */
1589 dwc->block_size =
1590 (4 << ((max_blk_size >> 4 * i) & 0xf)) - 1;
Andy Shevchenkofed25742012-09-21 15:05:49 +03001591 dwc->nollp =
1592 (dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0;
1593 } else {
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001594 dwc->block_size = pdata->block_size;
Andy Shevchenkofed25742012-09-21 15:05:49 +03001595
1596 /* Check if channel supports multi block transfer */
1597 channel_writel(dwc, LLP, 0xfffffffc);
1598 dwc->nollp =
1599 (channel_readl(dwc, LLP) & 0xfffffffc) == 0;
1600 channel_writel(dwc, LLP, 0);
1601 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001602 }
1603
Andy Shevchenko11f932e2012-06-19 13:34:06 +03001604 /* Clear all interrupts on all channels. */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001605 dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
Andy Shevchenko236b1062012-06-19 13:34:07 +03001606 dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001607 dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
1608 dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
1609 dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
1610
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001611 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
1612 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
Jamie Iles95ea7592011-01-21 14:11:54 +00001613 if (pdata->is_private)
1614 dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001615 dw->dma.dev = &pdev->dev;
1616 dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
1617 dw->dma.device_free_chan_resources = dwc_free_chan_resources;
1618
1619 dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
1620
1621 dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
Linus Walleijc3635c72010-03-26 16:44:01 -07001622 dw->dma.device_control = dwc_control;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001623
Linus Walleij07934482010-03-26 16:50:49 -07001624 dw->dma.device_tx_status = dwc_tx_status;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001625 dw->dma.device_issue_pending = dwc_issue_pending;
1626
1627 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1628
1629 printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n",
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001630 dev_name(&pdev->dev), nr_channels);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001631
1632 dma_async_device_register(&dw->dma);
1633
1634 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001635}
1636
Andy Shevchenko0272e932012-06-19 13:34:09 +03001637static int __devexit dw_remove(struct platform_device *pdev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001638{
1639 struct dw_dma *dw = platform_get_drvdata(pdev);
1640 struct dw_dma_chan *dwc, *_dwc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001641
1642 dw_dma_off(dw);
1643 dma_async_device_unregister(&dw->dma);
1644
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001645 tasklet_kill(&dw->tasklet);
1646
1647 list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
1648 chan.device_node) {
1649 list_del(&dwc->chan.device_node);
1650 channel_clear_bit(dw, CH_EN, dwc->mask);
1651 }
1652
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001653 return 0;
1654}
1655
1656static void dw_shutdown(struct platform_device *pdev)
1657{
1658 struct dw_dma *dw = platform_get_drvdata(pdev);
1659
1660 dw_dma_off(platform_get_drvdata(pdev));
Viresh Kumar30755282012-04-17 17:10:07 +05301661 clk_disable_unprepare(dw->clk);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001662}
1663
Magnus Damm4a256b52009-07-08 13:22:18 +02001664static int dw_suspend_noirq(struct device *dev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001665{
Magnus Damm4a256b52009-07-08 13:22:18 +02001666 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001667 struct dw_dma *dw = platform_get_drvdata(pdev);
1668
1669 dw_dma_off(platform_get_drvdata(pdev));
Viresh Kumar30755282012-04-17 17:10:07 +05301670 clk_disable_unprepare(dw->clk);
Viresh Kumar61e183f2011-11-17 16:01:29 +05301671
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001672 return 0;
1673}
1674
Magnus Damm4a256b52009-07-08 13:22:18 +02001675static int dw_resume_noirq(struct device *dev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001676{
Magnus Damm4a256b52009-07-08 13:22:18 +02001677 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001678 struct dw_dma *dw = platform_get_drvdata(pdev);
1679
Viresh Kumar30755282012-04-17 17:10:07 +05301680 clk_prepare_enable(dw->clk);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001681 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1682 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001683}
1684
Alexey Dobriyan47145212009-12-14 18:00:08 -08001685static const struct dev_pm_ops dw_dev_pm_ops = {
Magnus Damm4a256b52009-07-08 13:22:18 +02001686 .suspend_noirq = dw_suspend_noirq,
1687 .resume_noirq = dw_resume_noirq,
Rajeev KUMAR7414a1b2012-02-01 16:12:17 +05301688 .freeze_noirq = dw_suspend_noirq,
1689 .thaw_noirq = dw_resume_noirq,
1690 .restore_noirq = dw_resume_noirq,
1691 .poweroff_noirq = dw_suspend_noirq,
Magnus Damm4a256b52009-07-08 13:22:18 +02001692};
1693
Viresh Kumard3f797d2012-04-20 20:15:34 +05301694#ifdef CONFIG_OF
1695static const struct of_device_id dw_dma_id_table[] = {
1696 { .compatible = "snps,dma-spear1340" },
1697 {}
1698};
1699MODULE_DEVICE_TABLE(of, dw_dma_id_table);
1700#endif
1701
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001702static struct platform_driver dw_driver = {
Andy Shevchenko0272e932012-06-19 13:34:09 +03001703 .remove = __devexit_p(dw_remove),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001704 .shutdown = dw_shutdown,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001705 .driver = {
1706 .name = "dw_dmac",
Magnus Damm4a256b52009-07-08 13:22:18 +02001707 .pm = &dw_dev_pm_ops,
Viresh Kumard3f797d2012-04-20 20:15:34 +05301708 .of_match_table = of_match_ptr(dw_dma_id_table),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001709 },
1710};
1711
1712static int __init dw_init(void)
1713{
1714 return platform_driver_probe(&dw_driver, dw_probe);
1715}
Viresh Kumarcb689a72011-03-03 15:47:15 +05301716subsys_initcall(dw_init);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001717
1718static void __exit dw_exit(void)
1719{
1720 platform_driver_unregister(&dw_driver);
1721}
1722module_exit(dw_exit);
1723
1724MODULE_LICENSE("GPL v2");
1725MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001726MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Viresh Kumar10d89352012-06-20 12:53:02 -07001727MODULE_AUTHOR("Viresh Kumar <viresh.linux@gmail.com>");