blob: b279e1920725f4315292a2611b21a90704de84bc [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>
20#include <linux/mm.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/slab.h>
24
25#include "dw_dmac_regs.h"
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000026#include "dmaengine.h"
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070027
28/*
29 * This supports the Synopsys "DesignWare AHB Central DMA Controller",
30 * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
31 * of which use ARM any more). See the "Databook" from Synopsys for
32 * information beyond what licensees probably provide.
33 *
34 * The driver has currently been tested only with the Atmel AT32AP7000,
35 * which does not support descriptor writeback.
36 */
37
Viresh Kumar327e6972012-02-01 16:12:26 +053038#define DWC_DEFAULT_CTLLO(_chan) ({ \
39 struct dw_dma_slave *__slave = (_chan->private); \
40 struct dw_dma_chan *_dwc = to_dw_dma_chan(_chan); \
41 struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \
42 int _dms = __slave ? __slave->dst_master : 0; \
43 int _sms = __slave ? __slave->src_master : 1; \
44 u8 _smsize = __slave ? _sconfig->src_maxburst : \
45 DW_DMA_MSIZE_16; \
46 u8 _dmsize = __slave ? _sconfig->dst_maxburst : \
47 DW_DMA_MSIZE_16; \
Jamie Ilesf301c062011-01-21 14:11:53 +000048 \
Viresh Kumar327e6972012-02-01 16:12:26 +053049 (DWC_CTLL_DST_MSIZE(_dmsize) \
50 | DWC_CTLL_SRC_MSIZE(_smsize) \
Jamie Ilesf301c062011-01-21 14:11:53 +000051 | DWC_CTLL_LLP_D_EN \
52 | DWC_CTLL_LLP_S_EN \
Viresh Kumar327e6972012-02-01 16:12:26 +053053 | DWC_CTLL_DMS(_dms) \
54 | DWC_CTLL_SMS(_sms)); \
Jamie Ilesf301c062011-01-21 14:11:53 +000055 })
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070056
57/*
58 * This is configuration-dependent and usually a funny size like 4095.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070059 *
60 * Note that this is a transfer count, i.e. if we transfer 32-bit
Viresh Kumar418e7402011-03-04 15:42:50 +053061 * words, we can do 16380 bytes per descriptor.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070062 *
63 * This parameter is also system-specific.
64 */
Viresh Kumar418e7402011-03-04 15:42:50 +053065#define DWC_MAX_COUNT 4095U
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070066
67/*
68 * Number of descriptors to allocate for each channel. This should be
69 * made configurable somehow; preferably, the clients (at least the
70 * ones using slave transfers) should be able to give us a hint.
71 */
72#define NR_DESCS_PER_CHANNEL 64
73
74/*----------------------------------------------------------------------*/
75
76/*
77 * Because we're not relying on writeback from the controller (it may not
78 * even be configured into the core!) we don't need to use dma_pool. These
79 * descriptors -- and associated data -- are cacheable. We do need to make
80 * sure their dcache entries are written back before handing them off to
81 * the controller, though.
82 */
83
Dan Williams41d5e592009-01-06 11:38:21 -070084static struct device *chan2dev(struct dma_chan *chan)
85{
86 return &chan->dev->device;
87}
88static struct device *chan2parent(struct dma_chan *chan)
89{
90 return chan->dev->device.parent;
91}
92
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070093static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
94{
95 return list_entry(dwc->active_list.next, struct dw_desc, desc_node);
96}
97
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070098static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
99{
100 struct dw_desc *desc, *_desc;
101 struct dw_desc *ret = NULL;
102 unsigned int i = 0;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530103 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700104
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530105 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700106 list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
107 if (async_tx_test_ack(&desc->txd)) {
108 list_del(&desc->desc_node);
109 ret = desc;
110 break;
111 }
Dan Williams41d5e592009-01-06 11:38:21 -0700112 dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700113 i++;
114 }
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530115 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700116
Dan Williams41d5e592009-01-06 11:38:21 -0700117 dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700118
119 return ret;
120}
121
122static void dwc_sync_desc_for_cpu(struct dw_dma_chan *dwc, struct dw_desc *desc)
123{
124 struct dw_desc *child;
125
Dan Williamse0bd0f82009-09-08 17:53:02 -0700126 list_for_each_entry(child, &desc->tx_list, desc_node)
Dan Williams41d5e592009-01-06 11:38:21 -0700127 dma_sync_single_for_cpu(chan2parent(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700128 child->txd.phys, sizeof(child->lli),
129 DMA_TO_DEVICE);
Dan Williams41d5e592009-01-06 11:38:21 -0700130 dma_sync_single_for_cpu(chan2parent(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700131 desc->txd.phys, sizeof(desc->lli),
132 DMA_TO_DEVICE);
133}
134
135/*
136 * Move a descriptor, including any children, to the free list.
137 * `desc' must not be on any lists.
138 */
139static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
140{
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530141 unsigned long flags;
142
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700143 if (desc) {
144 struct dw_desc *child;
145
146 dwc_sync_desc_for_cpu(dwc, desc);
147
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530148 spin_lock_irqsave(&dwc->lock, flags);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700149 list_for_each_entry(child, &desc->tx_list, desc_node)
Dan Williams41d5e592009-01-06 11:38:21 -0700150 dev_vdbg(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700151 "moving child desc %p to freelist\n",
152 child);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700153 list_splice_init(&desc->tx_list, &dwc->free_list);
Dan Williams41d5e592009-01-06 11:38:21 -0700154 dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700155 list_add(&desc->desc_node, &dwc->free_list);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530156 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700157 }
158}
159
160/* Called with dwc->lock held and bh disabled */
161static dma_cookie_t
162dwc_assign_cookie(struct dw_dma_chan *dwc, struct dw_desc *desc)
163{
164 dma_cookie_t cookie = dwc->chan.cookie;
165
166 if (++cookie < 0)
167 cookie = 1;
168
169 dwc->chan.cookie = cookie;
170 desc->txd.cookie = cookie;
171
172 return cookie;
173}
174
Viresh Kumar61e183f2011-11-17 16:01:29 +0530175static void dwc_initialize(struct dw_dma_chan *dwc)
176{
177 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
178 struct dw_dma_slave *dws = dwc->chan.private;
179 u32 cfghi = DWC_CFGH_FIFO_MODE;
180 u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority);
181
182 if (dwc->initialized == true)
183 return;
184
185 if (dws) {
186 /*
187 * We need controller-specific data to set up slave
188 * transfers.
189 */
190 BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
191
192 cfghi = dws->cfg_hi;
193 cfglo |= dws->cfg_lo & ~DWC_CFGL_CH_PRIOR_MASK;
194 }
195
196 channel_writel(dwc, CFG_LO, cfglo);
197 channel_writel(dwc, CFG_HI, cfghi);
198
199 /* Enable interrupts */
200 channel_set_bit(dw, MASK.XFER, dwc->mask);
Viresh Kumar61e183f2011-11-17 16:01:29 +0530201 channel_set_bit(dw, MASK.ERROR, dwc->mask);
202
203 dwc->initialized = true;
204}
205
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700206/*----------------------------------------------------------------------*/
207
208/* Called with dwc->lock held and bh disabled */
209static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
210{
211 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
212
213 /* ASSERT: channel is idle */
214 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700215 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700216 "BUG: Attempted to start non-idle channel\n");
Dan Williams41d5e592009-01-06 11:38:21 -0700217 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700218 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
219 channel_readl(dwc, SAR),
220 channel_readl(dwc, DAR),
221 channel_readl(dwc, LLP),
222 channel_readl(dwc, CTL_HI),
223 channel_readl(dwc, CTL_LO));
224
225 /* The tasklet will hopefully advance the queue... */
226 return;
227 }
228
Viresh Kumar61e183f2011-11-17 16:01:29 +0530229 dwc_initialize(dwc);
230
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700231 channel_writel(dwc, LLP, first->txd.phys);
232 channel_writel(dwc, CTL_LO,
233 DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
234 channel_writel(dwc, CTL_HI, 0);
235 channel_set_bit(dw, CH_EN, dwc->mask);
236}
237
238/*----------------------------------------------------------------------*/
239
240static void
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530241dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
242 bool callback_required)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700243{
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530244 dma_async_tx_callback callback = NULL;
245 void *param = NULL;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700246 struct dma_async_tx_descriptor *txd = &desc->txd;
Viresh Kumare5180762011-03-03 15:47:20 +0530247 struct dw_desc *child;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530248 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700249
Dan Williams41d5e592009-01-06 11:38:21 -0700250 dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700251
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530252 spin_lock_irqsave(&dwc->lock, flags);
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +0000253 dwc->chan.completed_cookie = txd->cookie;
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530254 if (callback_required) {
255 callback = txd->callback;
256 param = txd->callback_param;
257 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700258
259 dwc_sync_desc_for_cpu(dwc, desc);
Viresh Kumare5180762011-03-03 15:47:20 +0530260
261 /* async_tx_ack */
262 list_for_each_entry(child, &desc->tx_list, desc_node)
263 async_tx_ack(&child->txd);
264 async_tx_ack(&desc->txd);
265
Dan Williamse0bd0f82009-09-08 17:53:02 -0700266 list_splice_init(&desc->tx_list, &dwc->free_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700267 list_move(&desc->desc_node, &dwc->free_list);
268
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700269 if (!dwc->chan.private) {
270 struct device *parent = chan2parent(&dwc->chan);
271 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
272 if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
273 dma_unmap_single(parent, desc->lli.dar,
274 desc->len, DMA_FROM_DEVICE);
275 else
276 dma_unmap_page(parent, desc->lli.dar,
277 desc->len, DMA_FROM_DEVICE);
278 }
279 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
280 if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
281 dma_unmap_single(parent, desc->lli.sar,
282 desc->len, DMA_TO_DEVICE);
283 else
284 dma_unmap_page(parent, desc->lli.sar,
285 desc->len, DMA_TO_DEVICE);
286 }
287 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700288
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530289 spin_unlock_irqrestore(&dwc->lock, flags);
290
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530291 if (callback_required && callback)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700292 callback(param);
293}
294
295static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
296{
297 struct dw_desc *desc, *_desc;
298 LIST_HEAD(list);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530299 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700300
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530301 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700302 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700303 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700304 "BUG: XFER bit set, but channel not idle!\n");
305
306 /* Try to continue after resetting the channel... */
307 channel_clear_bit(dw, CH_EN, dwc->mask);
308 while (dma_readl(dw, CH_EN) & dwc->mask)
309 cpu_relax();
310 }
311
312 /*
313 * Submit queued descriptors ASAP, i.e. before we go through
314 * the completed ones.
315 */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700316 list_splice_init(&dwc->active_list, &list);
Viresh Kumarf336e422011-03-03 15:47:16 +0530317 if (!list_empty(&dwc->queue)) {
318 list_move(dwc->queue.next, &dwc->active_list);
319 dwc_dostart(dwc, dwc_first_active(dwc));
320 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700321
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530322 spin_unlock_irqrestore(&dwc->lock, flags);
323
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700324 list_for_each_entry_safe(desc, _desc, &list, desc_node)
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530325 dwc_descriptor_complete(dwc, desc, true);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700326}
327
328static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
329{
330 dma_addr_t llp;
331 struct dw_desc *desc, *_desc;
332 struct dw_desc *child;
333 u32 status_xfer;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530334 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700335
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530336 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700337 llp = channel_readl(dwc, LLP);
338 status_xfer = dma_readl(dw, RAW.XFER);
339
340 if (status_xfer & dwc->mask) {
341 /* Everything we've submitted is done */
342 dma_writel(dw, CLEAR.XFER, dwc->mask);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530343 spin_unlock_irqrestore(&dwc->lock, flags);
344
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700345 dwc_complete_all(dw, dwc);
346 return;
347 }
348
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530349 if (list_empty(&dwc->active_list)) {
350 spin_unlock_irqrestore(&dwc->lock, flags);
Jamie Iles087809f2011-01-21 14:11:52 +0000351 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530352 }
Jamie Iles087809f2011-01-21 14:11:52 +0000353
Dan Williams41d5e592009-01-06 11:38:21 -0700354 dev_vdbg(chan2dev(&dwc->chan), "scan_descriptors: llp=0x%x\n", llp);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700355
356 list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
Viresh Kumar84adccf2011-03-24 11:32:15 +0530357 /* check first descriptors addr */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530358 if (desc->txd.phys == llp) {
359 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700360 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530361 }
Viresh Kumar84adccf2011-03-24 11:32:15 +0530362
363 /* check first descriptors llp */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530364 if (desc->lli.llp == llp) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700365 /* This one is currently in progress */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530366 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700367 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530368 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700369
Dan Williamse0bd0f82009-09-08 17:53:02 -0700370 list_for_each_entry(child, &desc->tx_list, desc_node)
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530371 if (child->lli.llp == llp) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700372 /* Currently in progress */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530373 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700374 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530375 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700376
377 /*
378 * No descriptors so far seem to be in progress, i.e.
379 * this one must be done.
380 */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530381 spin_unlock_irqrestore(&dwc->lock, flags);
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530382 dwc_descriptor_complete(dwc, desc, true);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530383 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700384 }
385
Dan Williams41d5e592009-01-06 11:38:21 -0700386 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700387 "BUG: All descriptors done, but channel not idle!\n");
388
389 /* Try to continue after resetting the channel... */
390 channel_clear_bit(dw, CH_EN, dwc->mask);
391 while (dma_readl(dw, CH_EN) & dwc->mask)
392 cpu_relax();
393
394 if (!list_empty(&dwc->queue)) {
Viresh Kumarf336e422011-03-03 15:47:16 +0530395 list_move(dwc->queue.next, &dwc->active_list);
396 dwc_dostart(dwc, dwc_first_active(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700397 }
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530398 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700399}
400
401static void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
402{
Dan Williams41d5e592009-01-06 11:38:21 -0700403 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700404 " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
405 lli->sar, lli->dar, lli->llp,
406 lli->ctlhi, lli->ctllo);
407}
408
409static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
410{
411 struct dw_desc *bad_desc;
412 struct dw_desc *child;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530413 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700414
415 dwc_scan_descriptors(dw, dwc);
416
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530417 spin_lock_irqsave(&dwc->lock, flags);
418
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700419 /*
420 * The descriptor currently at the head of the active list is
421 * borked. Since we don't have any way to report errors, we'll
422 * just have to scream loudly and try to carry on.
423 */
424 bad_desc = dwc_first_active(dwc);
425 list_del_init(&bad_desc->desc_node);
Viresh Kumarf336e422011-03-03 15:47:16 +0530426 list_move(dwc->queue.next, dwc->active_list.prev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700427
428 /* Clear the error flag and try to restart the controller */
429 dma_writel(dw, CLEAR.ERROR, dwc->mask);
430 if (!list_empty(&dwc->active_list))
431 dwc_dostart(dwc, dwc_first_active(dwc));
432
433 /*
434 * KERN_CRITICAL may seem harsh, but since this only happens
435 * when someone submits a bad physical address in a
436 * descriptor, we should consider ourselves lucky that the
437 * controller flagged an error instead of scribbling over
438 * random memory locations.
439 */
Dan Williams41d5e592009-01-06 11:38:21 -0700440 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700441 "Bad descriptor submitted for DMA!\n");
Dan Williams41d5e592009-01-06 11:38:21 -0700442 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700443 " cookie: %d\n", bad_desc->txd.cookie);
444 dwc_dump_lli(dwc, &bad_desc->lli);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700445 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700446 dwc_dump_lli(dwc, &child->lli);
447
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530448 spin_unlock_irqrestore(&dwc->lock, flags);
449
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700450 /* Pretend the descriptor completed successfully */
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530451 dwc_descriptor_complete(dwc, bad_desc, true);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700452}
453
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200454/* --------------------- Cyclic DMA API extensions -------------------- */
455
456inline dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
457{
458 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
459 return channel_readl(dwc, SAR);
460}
461EXPORT_SYMBOL(dw_dma_get_src_addr);
462
463inline dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
464{
465 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
466 return channel_readl(dwc, DAR);
467}
468EXPORT_SYMBOL(dw_dma_get_dst_addr);
469
470/* called with dwc->lock held and all DMAC interrupts disabled */
471static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530472 u32 status_err, u32 status_xfer)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200473{
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530474 unsigned long flags;
475
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530476 if (dwc->mask) {
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200477 void (*callback)(void *param);
478 void *callback_param;
479
480 dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
481 channel_readl(dwc, LLP));
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200482
483 callback = dwc->cdesc->period_callback;
484 callback_param = dwc->cdesc->period_callback_param;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530485
486 if (callback)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200487 callback(callback_param);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200488 }
489
490 /*
491 * Error and transfer complete are highly unlikely, and will most
492 * likely be due to a configuration error by the user.
493 */
494 if (unlikely(status_err & dwc->mask) ||
495 unlikely(status_xfer & dwc->mask)) {
496 int i;
497
498 dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s "
499 "interrupt, stopping DMA transfer\n",
500 status_xfer ? "xfer" : "error");
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530501
502 spin_lock_irqsave(&dwc->lock, flags);
503
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200504 dev_err(chan2dev(&dwc->chan),
505 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
506 channel_readl(dwc, SAR),
507 channel_readl(dwc, DAR),
508 channel_readl(dwc, LLP),
509 channel_readl(dwc, CTL_HI),
510 channel_readl(dwc, CTL_LO));
511
512 channel_clear_bit(dw, CH_EN, dwc->mask);
513 while (dma_readl(dw, CH_EN) & dwc->mask)
514 cpu_relax();
515
516 /* make sure DMA does not restart by loading a new list */
517 channel_writel(dwc, LLP, 0);
518 channel_writel(dwc, CTL_LO, 0);
519 channel_writel(dwc, CTL_HI, 0);
520
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200521 dma_writel(dw, CLEAR.ERROR, dwc->mask);
522 dma_writel(dw, CLEAR.XFER, dwc->mask);
523
524 for (i = 0; i < dwc->cdesc->periods; i++)
525 dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530526
527 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200528 }
529}
530
531/* ------------------------------------------------------------------------- */
532
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700533static void dw_dma_tasklet(unsigned long data)
534{
535 struct dw_dma *dw = (struct dw_dma *)data;
536 struct dw_dma_chan *dwc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700537 u32 status_xfer;
538 u32 status_err;
539 int i;
540
Haavard Skinnemoen7fe7b2f2008-10-03 15:23:46 -0700541 status_xfer = dma_readl(dw, RAW.XFER);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700542 status_err = dma_readl(dw, RAW.ERROR);
543
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530544 dev_vdbg(dw->dma.dev, "tasklet: status_err=%x\n", status_err);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700545
546 for (i = 0; i < dw->dma.chancnt; i++) {
547 dwc = &dw->chan[i];
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200548 if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530549 dwc_handle_cyclic(dw, dwc, status_err, status_xfer);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200550 else if (status_err & (1 << i))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700551 dwc_handle_error(dw, dwc);
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530552 else if (status_xfer & (1 << i))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700553 dwc_scan_descriptors(dw, dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700554 }
555
556 /*
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530557 * Re-enable interrupts.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700558 */
559 channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700560 channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
561}
562
563static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
564{
565 struct dw_dma *dw = dev_id;
566 u32 status;
567
568 dev_vdbg(dw->dma.dev, "interrupt: status=0x%x\n",
569 dma_readl(dw, STATUS_INT));
570
571 /*
572 * Just disable the interrupts. We'll turn them back on in the
573 * softirq handler.
574 */
575 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700576 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
577
578 status = dma_readl(dw, STATUS_INT);
579 if (status) {
580 dev_err(dw->dma.dev,
581 "BUG: Unexpected interrupts pending: 0x%x\n",
582 status);
583
584 /* Try to recover */
585 channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700586 channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
587 channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
588 channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
589 }
590
591 tasklet_schedule(&dw->tasklet);
592
593 return IRQ_HANDLED;
594}
595
596/*----------------------------------------------------------------------*/
597
598static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
599{
600 struct dw_desc *desc = txd_to_dw_desc(tx);
601 struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
602 dma_cookie_t cookie;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530603 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700604
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530605 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700606 cookie = dwc_assign_cookie(dwc, desc);
607
608 /*
609 * REVISIT: We should attempt to chain as many descriptors as
610 * possible, perhaps even appending to those already submitted
611 * for DMA. But this is hard to do in a race-free manner.
612 */
613 if (list_empty(&dwc->active_list)) {
Dan Williams41d5e592009-01-06 11:38:21 -0700614 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700615 desc->txd.cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700616 list_add_tail(&desc->desc_node, &dwc->active_list);
Viresh Kumarf336e422011-03-03 15:47:16 +0530617 dwc_dostart(dwc, dwc_first_active(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700618 } else {
Dan Williams41d5e592009-01-06 11:38:21 -0700619 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700620 desc->txd.cookie);
621
622 list_add_tail(&desc->desc_node, &dwc->queue);
623 }
624
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530625 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700626
627 return cookie;
628}
629
630static struct dma_async_tx_descriptor *
631dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
632 size_t len, unsigned long flags)
633{
634 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
635 struct dw_desc *desc;
636 struct dw_desc *first;
637 struct dw_desc *prev;
638 size_t xfer_count;
639 size_t offset;
640 unsigned int src_width;
641 unsigned int dst_width;
642 u32 ctllo;
643
Dan Williams41d5e592009-01-06 11:38:21 -0700644 dev_vdbg(chan2dev(chan), "prep_dma_memcpy d0x%x s0x%x l0x%zx f0x%lx\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700645 dest, src, len, flags);
646
647 if (unlikely(!len)) {
Dan Williams41d5e592009-01-06 11:38:21 -0700648 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700649 return NULL;
650 }
651
652 /*
653 * We can be a lot more clever here, but this should take care
654 * of the most common optimization.
655 */
Viresh Kumara0227452011-03-03 15:47:18 +0530656 if (!((src | dest | len) & 7))
657 src_width = dst_width = 3;
658 else if (!((src | dest | len) & 3))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700659 src_width = dst_width = 2;
660 else if (!((src | dest | len) & 1))
661 src_width = dst_width = 1;
662 else
663 src_width = dst_width = 0;
664
Viresh Kumar327e6972012-02-01 16:12:26 +0530665 ctllo = DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700666 | DWC_CTLL_DST_WIDTH(dst_width)
667 | DWC_CTLL_SRC_WIDTH(src_width)
668 | DWC_CTLL_DST_INC
669 | DWC_CTLL_SRC_INC
670 | DWC_CTLL_FC_M2M;
671 prev = first = NULL;
672
673 for (offset = 0; offset < len; offset += xfer_count << src_width) {
674 xfer_count = min_t(size_t, (len - offset) >> src_width,
675 DWC_MAX_COUNT);
676
677 desc = dwc_desc_get(dwc);
678 if (!desc)
679 goto err_desc_get;
680
681 desc->lli.sar = src + offset;
682 desc->lli.dar = dest + offset;
683 desc->lli.ctllo = ctllo;
684 desc->lli.ctlhi = xfer_count;
685
686 if (!first) {
687 first = desc;
688 } else {
689 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700690 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700691 prev->txd.phys, sizeof(prev->lli),
692 DMA_TO_DEVICE);
693 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700694 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700695 }
696 prev = desc;
697 }
698
699
700 if (flags & DMA_PREP_INTERRUPT)
701 /* Trigger interrupt after last block */
702 prev->lli.ctllo |= DWC_CTLL_INT_EN;
703
704 prev->lli.llp = 0;
Dan Williams41d5e592009-01-06 11:38:21 -0700705 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700706 prev->txd.phys, sizeof(prev->lli),
707 DMA_TO_DEVICE);
708
709 first->txd.flags = flags;
710 first->len = len;
711
712 return &first->txd;
713
714err_desc_get:
715 dwc_desc_put(dwc, first);
716 return NULL;
717}
718
719static struct dma_async_tx_descriptor *
720dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530721 unsigned int sg_len, enum dma_transfer_direction direction,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700722 unsigned long flags)
723{
724 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Dan Williams287d8592009-02-18 14:48:26 -0800725 struct dw_dma_slave *dws = chan->private;
Viresh Kumar327e6972012-02-01 16:12:26 +0530726 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700727 struct dw_desc *prev;
728 struct dw_desc *first;
729 u32 ctllo;
730 dma_addr_t reg;
731 unsigned int reg_width;
732 unsigned int mem_width;
733 unsigned int i;
734 struct scatterlist *sg;
735 size_t total_len = 0;
736
Dan Williams41d5e592009-01-06 11:38:21 -0700737 dev_vdbg(chan2dev(chan), "prep_dma_slave\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700738
739 if (unlikely(!dws || !sg_len))
740 return NULL;
741
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700742 prev = first = NULL;
743
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700744 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530745 case DMA_MEM_TO_DEV:
Viresh Kumar327e6972012-02-01 16:12:26 +0530746 reg_width = __fls(sconfig->dst_addr_width);
747 reg = sconfig->dst_addr;
748 ctllo = (DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700749 | DWC_CTLL_DST_WIDTH(reg_width)
750 | DWC_CTLL_DST_FIX
Viresh Kumar327e6972012-02-01 16:12:26 +0530751 | DWC_CTLL_SRC_INC);
752
753 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
754 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
755
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700756 for_each_sg(sgl, sg, sg_len, i) {
757 struct dw_desc *desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530758 u32 len, dlen, mem;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700759
760 mem = sg_phys(sg);
761 len = sg_dma_len(sg);
Viresh Kumar6bc711f2012-02-01 16:12:25 +0530762
763 if (!((mem | len) & 7))
764 mem_width = 3;
765 else if (!((mem | len) & 3))
766 mem_width = 2;
767 else if (!((mem | len) & 1))
768 mem_width = 1;
769 else
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700770 mem_width = 0;
771
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530772slave_sg_todev_fill_desc:
773 desc = dwc_desc_get(dwc);
774 if (!desc) {
775 dev_err(chan2dev(chan),
776 "not enough descriptors available\n");
777 goto err_desc_get;
778 }
779
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700780 desc->lli.sar = mem;
781 desc->lli.dar = reg;
782 desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530783 if ((len >> mem_width) > DWC_MAX_COUNT) {
784 dlen = DWC_MAX_COUNT << mem_width;
785 mem += dlen;
786 len -= dlen;
787 } else {
788 dlen = len;
789 len = 0;
790 }
791
792 desc->lli.ctlhi = dlen >> mem_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700793
794 if (!first) {
795 first = desc;
796 } else {
797 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700798 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700799 prev->txd.phys,
800 sizeof(prev->lli),
801 DMA_TO_DEVICE);
802 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700803 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700804 }
805 prev = desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530806 total_len += dlen;
807
808 if (len)
809 goto slave_sg_todev_fill_desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700810 }
811 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530812 case DMA_DEV_TO_MEM:
Viresh Kumar327e6972012-02-01 16:12:26 +0530813 reg_width = __fls(sconfig->src_addr_width);
814 reg = sconfig->src_addr;
815 ctllo = (DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700816 | DWC_CTLL_SRC_WIDTH(reg_width)
817 | DWC_CTLL_DST_INC
Viresh Kumar327e6972012-02-01 16:12:26 +0530818 | DWC_CTLL_SRC_FIX);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700819
Viresh Kumar327e6972012-02-01 16:12:26 +0530820 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
821 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
822
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700823 for_each_sg(sgl, sg, sg_len, i) {
824 struct dw_desc *desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530825 u32 len, dlen, mem;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700826
827 mem = sg_phys(sg);
828 len = sg_dma_len(sg);
Viresh Kumar6bc711f2012-02-01 16:12:25 +0530829
830 if (!((mem | len) & 7))
831 mem_width = 3;
832 else if (!((mem | len) & 3))
833 mem_width = 2;
834 else if (!((mem | len) & 1))
835 mem_width = 1;
836 else
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700837 mem_width = 0;
838
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530839slave_sg_fromdev_fill_desc:
840 desc = dwc_desc_get(dwc);
841 if (!desc) {
842 dev_err(chan2dev(chan),
843 "not enough descriptors available\n");
844 goto err_desc_get;
845 }
846
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700847 desc->lli.sar = reg;
848 desc->lli.dar = mem;
849 desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530850 if ((len >> reg_width) > DWC_MAX_COUNT) {
851 dlen = DWC_MAX_COUNT << reg_width;
852 mem += dlen;
853 len -= dlen;
854 } else {
855 dlen = len;
856 len = 0;
857 }
858 desc->lli.ctlhi = dlen >> reg_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700859
860 if (!first) {
861 first = desc;
862 } else {
863 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700864 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700865 prev->txd.phys,
866 sizeof(prev->lli),
867 DMA_TO_DEVICE);
868 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700869 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700870 }
871 prev = desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530872 total_len += dlen;
873
874 if (len)
875 goto slave_sg_fromdev_fill_desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700876 }
877 break;
878 default:
879 return NULL;
880 }
881
882 if (flags & DMA_PREP_INTERRUPT)
883 /* Trigger interrupt after last block */
884 prev->lli.ctllo |= DWC_CTLL_INT_EN;
885
886 prev->lli.llp = 0;
Dan Williams41d5e592009-01-06 11:38:21 -0700887 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700888 prev->txd.phys, sizeof(prev->lli),
889 DMA_TO_DEVICE);
890
891 first->len = total_len;
892
893 return &first->txd;
894
895err_desc_get:
896 dwc_desc_put(dwc, first);
897 return NULL;
898}
899
Viresh Kumar327e6972012-02-01 16:12:26 +0530900/*
901 * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
902 * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
903 *
904 * NOTE: burst size 2 is not supported by controller.
905 *
906 * This can be done by finding least significant bit set: n & (n - 1)
907 */
908static inline void convert_burst(u32 *maxburst)
909{
910 if (*maxburst > 1)
911 *maxburst = fls(*maxburst) - 2;
912 else
913 *maxburst = 0;
914}
915
916static int
917set_runtime_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
918{
919 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
920
921 /* Check if it is chan is configured for slave transfers */
922 if (!chan->private)
923 return -EINVAL;
924
925 memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
926
927 convert_burst(&dwc->dma_sconfig.src_maxburst);
928 convert_burst(&dwc->dma_sconfig.dst_maxburst);
929
930 return 0;
931}
932
Linus Walleij05827632010-05-17 16:30:42 -0700933static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
934 unsigned long arg)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700935{
936 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
937 struct dw_dma *dw = to_dw_dma(chan->device);
938 struct dw_desc *desc, *_desc;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530939 unsigned long flags;
Linus Walleija7c57cf2011-04-19 08:31:32 +0800940 u32 cfglo;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700941 LIST_HEAD(list);
942
Linus Walleija7c57cf2011-04-19 08:31:32 +0800943 if (cmd == DMA_PAUSE) {
944 spin_lock_irqsave(&dwc->lock, flags);
945
946 cfglo = channel_readl(dwc, CFG_LO);
947 channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
948 while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY))
949 cpu_relax();
950
951 dwc->paused = true;
952 spin_unlock_irqrestore(&dwc->lock, flags);
953 } else if (cmd == DMA_RESUME) {
954 if (!dwc->paused)
955 return 0;
956
957 spin_lock_irqsave(&dwc->lock, flags);
958
959 cfglo = channel_readl(dwc, CFG_LO);
960 channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
961 dwc->paused = false;
962
963 spin_unlock_irqrestore(&dwc->lock, flags);
964 } else if (cmd == DMA_TERMINATE_ALL) {
965 spin_lock_irqsave(&dwc->lock, flags);
966
967 channel_clear_bit(dw, CH_EN, dwc->mask);
968 while (dma_readl(dw, CH_EN) & dwc->mask)
969 cpu_relax();
970
971 dwc->paused = false;
972
973 /* active_list entries will end up before queued entries */
974 list_splice_init(&dwc->queue, &list);
975 list_splice_init(&dwc->active_list, &list);
976
977 spin_unlock_irqrestore(&dwc->lock, flags);
978
979 /* Flush all pending and queued descriptors */
980 list_for_each_entry_safe(desc, _desc, &list, desc_node)
981 dwc_descriptor_complete(dwc, desc, false);
Viresh Kumar327e6972012-02-01 16:12:26 +0530982 } else if (cmd == DMA_SLAVE_CONFIG) {
983 return set_runtime_config(chan, (struct dma_slave_config *)arg);
984 } else {
Linus Walleijc3635c72010-03-26 16:44:01 -0700985 return -ENXIO;
Viresh Kumar327e6972012-02-01 16:12:26 +0530986 }
Linus Walleijc3635c72010-03-26 16:44:01 -0700987
Linus Walleijc3635c72010-03-26 16:44:01 -0700988 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700989}
990
991static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -0700992dwc_tx_status(struct dma_chan *chan,
993 dma_cookie_t cookie,
994 struct dma_tx_state *txstate)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700995{
996 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
997 dma_cookie_t last_used;
998 dma_cookie_t last_complete;
999 int ret;
1000
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +00001001 last_complete = chan->completed_cookie;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001002 last_used = chan->cookie;
1003
1004 ret = dma_async_is_complete(cookie, last_complete, last_used);
1005 if (ret != DMA_SUCCESS) {
1006 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
1007
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +00001008 last_complete = chan->completed_cookie;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001009 last_used = chan->cookie;
1010
1011 ret = dma_async_is_complete(cookie, last_complete, last_used);
1012 }
1013
Viresh Kumarabf53902011-04-15 16:03:35 +05301014 if (ret != DMA_SUCCESS)
1015 dma_set_tx_state(txstate, last_complete, last_used,
1016 dwc_first_active(dwc)->len);
1017 else
1018 dma_set_tx_state(txstate, last_complete, last_used, 0);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001019
Linus Walleija7c57cf2011-04-19 08:31:32 +08001020 if (dwc->paused)
1021 return DMA_PAUSED;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001022
1023 return ret;
1024}
1025
1026static void dwc_issue_pending(struct dma_chan *chan)
1027{
1028 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1029
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001030 if (!list_empty(&dwc->queue))
1031 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001032}
1033
Dan Williamsaa1e6f12009-01-06 11:38:17 -07001034static int dwc_alloc_chan_resources(struct dma_chan *chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001035{
1036 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1037 struct dw_dma *dw = to_dw_dma(chan->device);
1038 struct dw_desc *desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001039 int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301040 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001041
Dan Williams41d5e592009-01-06 11:38:21 -07001042 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001043
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001044 /* ASSERT: channel is idle */
1045 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -07001046 dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001047 return -EIO;
1048 }
1049
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +00001050 chan->completed_cookie = chan->cookie = 1;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001051
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001052 /*
1053 * NOTE: some controllers may have additional features that we
1054 * need to initialize here, like "scatter-gather" (which
1055 * doesn't mean what you think it means), and status writeback.
1056 */
1057
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301058 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001059 i = dwc->descs_allocated;
1060 while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301061 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001062
1063 desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL);
1064 if (!desc) {
Dan Williams41d5e592009-01-06 11:38:21 -07001065 dev_info(chan2dev(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001066 "only allocated %d descriptors\n", i);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301067 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001068 break;
1069 }
1070
Dan Williamse0bd0f82009-09-08 17:53:02 -07001071 INIT_LIST_HEAD(&desc->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001072 dma_async_tx_descriptor_init(&desc->txd, chan);
1073 desc->txd.tx_submit = dwc_tx_submit;
1074 desc->txd.flags = DMA_CTRL_ACK;
Dan Williams41d5e592009-01-06 11:38:21 -07001075 desc->txd.phys = dma_map_single(chan2parent(chan), &desc->lli,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001076 sizeof(desc->lli), DMA_TO_DEVICE);
1077 dwc_desc_put(dwc, desc);
1078
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301079 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001080 i = ++dwc->descs_allocated;
1081 }
1082
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301083 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001084
Dan Williams41d5e592009-01-06 11:38:21 -07001085 dev_dbg(chan2dev(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001086 "alloc_chan_resources allocated %d descriptors\n", i);
1087
1088 return i;
1089}
1090
1091static void dwc_free_chan_resources(struct dma_chan *chan)
1092{
1093 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1094 struct dw_dma *dw = to_dw_dma(chan->device);
1095 struct dw_desc *desc, *_desc;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301096 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001097 LIST_HEAD(list);
1098
Dan Williams41d5e592009-01-06 11:38:21 -07001099 dev_dbg(chan2dev(chan), "free_chan_resources (descs allocated=%u)\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001100 dwc->descs_allocated);
1101
1102 /* ASSERT: channel is idle */
1103 BUG_ON(!list_empty(&dwc->active_list));
1104 BUG_ON(!list_empty(&dwc->queue));
1105 BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
1106
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301107 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001108 list_splice_init(&dwc->free_list, &list);
1109 dwc->descs_allocated = 0;
Viresh Kumar61e183f2011-11-17 16:01:29 +05301110 dwc->initialized = false;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001111
1112 /* Disable interrupts */
1113 channel_clear_bit(dw, MASK.XFER, dwc->mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001114 channel_clear_bit(dw, MASK.ERROR, dwc->mask);
1115
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301116 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001117
1118 list_for_each_entry_safe(desc, _desc, &list, desc_node) {
Dan Williams41d5e592009-01-06 11:38:21 -07001119 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1120 dma_unmap_single(chan2parent(chan), desc->txd.phys,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001121 sizeof(desc->lli), DMA_TO_DEVICE);
1122 kfree(desc);
1123 }
1124
Dan Williams41d5e592009-01-06 11:38:21 -07001125 dev_vdbg(chan2dev(chan), "free_chan_resources done\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001126}
1127
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001128/* --------------------- Cyclic DMA API extensions -------------------- */
1129
1130/**
1131 * dw_dma_cyclic_start - start the cyclic DMA transfer
1132 * @chan: the DMA channel to start
1133 *
1134 * Must be called with soft interrupts disabled. Returns zero on success or
1135 * -errno on failure.
1136 */
1137int dw_dma_cyclic_start(struct dma_chan *chan)
1138{
1139 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1140 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301141 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001142
1143 if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
1144 dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
1145 return -ENODEV;
1146 }
1147
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301148 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001149
1150 /* assert channel is idle */
1151 if (dma_readl(dw, CH_EN) & dwc->mask) {
1152 dev_err(chan2dev(&dwc->chan),
1153 "BUG: Attempted to start non-idle channel\n");
1154 dev_err(chan2dev(&dwc->chan),
1155 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
1156 channel_readl(dwc, SAR),
1157 channel_readl(dwc, DAR),
1158 channel_readl(dwc, LLP),
1159 channel_readl(dwc, CTL_HI),
1160 channel_readl(dwc, CTL_LO));
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301161 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001162 return -EBUSY;
1163 }
1164
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001165 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1166 dma_writel(dw, CLEAR.XFER, dwc->mask);
1167
1168 /* setup DMAC channel registers */
1169 channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
1170 channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
1171 channel_writel(dwc, CTL_HI, 0);
1172
1173 channel_set_bit(dw, CH_EN, dwc->mask);
1174
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301175 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001176
1177 return 0;
1178}
1179EXPORT_SYMBOL(dw_dma_cyclic_start);
1180
1181/**
1182 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1183 * @chan: the DMA channel to stop
1184 *
1185 * Must be called with soft interrupts disabled.
1186 */
1187void dw_dma_cyclic_stop(struct dma_chan *chan)
1188{
1189 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1190 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301191 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001192
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301193 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001194
1195 channel_clear_bit(dw, CH_EN, dwc->mask);
1196 while (dma_readl(dw, CH_EN) & dwc->mask)
1197 cpu_relax();
1198
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301199 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001200}
1201EXPORT_SYMBOL(dw_dma_cyclic_stop);
1202
1203/**
1204 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1205 * @chan: the DMA channel to prepare
1206 * @buf_addr: physical DMA address where the buffer starts
1207 * @buf_len: total number of bytes for the entire buffer
1208 * @period_len: number of bytes for each period
1209 * @direction: transfer direction, to or from device
1210 *
1211 * Must be called before trying to start the transfer. Returns a valid struct
1212 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1213 */
1214struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
1215 dma_addr_t buf_addr, size_t buf_len, size_t period_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05301216 enum dma_transfer_direction direction)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001217{
1218 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Viresh Kumar327e6972012-02-01 16:12:26 +05301219 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001220 struct dw_cyclic_desc *cdesc;
1221 struct dw_cyclic_desc *retval = NULL;
1222 struct dw_desc *desc;
1223 struct dw_desc *last = NULL;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001224 unsigned long was_cyclic;
1225 unsigned int reg_width;
1226 unsigned int periods;
1227 unsigned int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301228 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001229
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301230 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001231 if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301232 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001233 dev_dbg(chan2dev(&dwc->chan),
1234 "queue and/or active list are not empty\n");
1235 return ERR_PTR(-EBUSY);
1236 }
1237
1238 was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301239 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001240 if (was_cyclic) {
1241 dev_dbg(chan2dev(&dwc->chan),
1242 "channel already prepared for cyclic DMA\n");
1243 return ERR_PTR(-EBUSY);
1244 }
1245
1246 retval = ERR_PTR(-EINVAL);
Viresh Kumar327e6972012-02-01 16:12:26 +05301247
1248 if (direction == DMA_MEM_TO_DEV)
1249 reg_width = __ffs(sconfig->dst_addr_width);
1250 else
1251 reg_width = __ffs(sconfig->src_addr_width);
1252
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001253 periods = buf_len / period_len;
1254
1255 /* Check for too big/unaligned periods and unaligned DMA buffer. */
1256 if (period_len > (DWC_MAX_COUNT << reg_width))
1257 goto out_err;
1258 if (unlikely(period_len & ((1 << reg_width) - 1)))
1259 goto out_err;
1260 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
1261 goto out_err;
Vinod Kouldb8196d2011-10-13 22:34:23 +05301262 if (unlikely(!(direction & (DMA_MEM_TO_DEV | DMA_DEV_TO_MEM))))
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001263 goto out_err;
1264
1265 retval = ERR_PTR(-ENOMEM);
1266
1267 if (periods > NR_DESCS_PER_CHANNEL)
1268 goto out_err;
1269
1270 cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
1271 if (!cdesc)
1272 goto out_err;
1273
1274 cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
1275 if (!cdesc->desc)
1276 goto out_err_alloc;
1277
1278 for (i = 0; i < periods; i++) {
1279 desc = dwc_desc_get(dwc);
1280 if (!desc)
1281 goto out_err_desc_get;
1282
1283 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +05301284 case DMA_MEM_TO_DEV:
Viresh Kumar327e6972012-02-01 16:12:26 +05301285 desc->lli.dar = sconfig->dst_addr;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001286 desc->lli.sar = buf_addr + (period_len * i);
Viresh Kumar327e6972012-02-01 16:12:26 +05301287 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001288 | DWC_CTLL_DST_WIDTH(reg_width)
1289 | DWC_CTLL_SRC_WIDTH(reg_width)
1290 | DWC_CTLL_DST_FIX
1291 | DWC_CTLL_SRC_INC
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001292 | DWC_CTLL_INT_EN);
Viresh Kumar327e6972012-02-01 16:12:26 +05301293
1294 desc->lli.ctllo |= sconfig->device_fc ?
1295 DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
1296 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
1297
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001298 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +05301299 case DMA_DEV_TO_MEM:
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001300 desc->lli.dar = buf_addr + (period_len * i);
Viresh Kumar327e6972012-02-01 16:12:26 +05301301 desc->lli.sar = sconfig->src_addr;
1302 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001303 | DWC_CTLL_SRC_WIDTH(reg_width)
1304 | DWC_CTLL_DST_WIDTH(reg_width)
1305 | DWC_CTLL_DST_INC
1306 | DWC_CTLL_SRC_FIX
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001307 | DWC_CTLL_INT_EN);
Viresh Kumar327e6972012-02-01 16:12:26 +05301308
1309 desc->lli.ctllo |= sconfig->device_fc ?
1310 DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
1311 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
1312
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001313 break;
1314 default:
1315 break;
1316 }
1317
1318 desc->lli.ctlhi = (period_len >> reg_width);
1319 cdesc->desc[i] = desc;
1320
1321 if (last) {
1322 last->lli.llp = desc->txd.phys;
1323 dma_sync_single_for_device(chan2parent(chan),
1324 last->txd.phys, sizeof(last->lli),
1325 DMA_TO_DEVICE);
1326 }
1327
1328 last = desc;
1329 }
1330
1331 /* lets make a cyclic list */
1332 last->lli.llp = cdesc->desc[0]->txd.phys;
1333 dma_sync_single_for_device(chan2parent(chan), last->txd.phys,
1334 sizeof(last->lli), DMA_TO_DEVICE);
1335
1336 dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%08x len %zu "
1337 "period %zu periods %d\n", buf_addr, buf_len,
1338 period_len, periods);
1339
1340 cdesc->periods = periods;
1341 dwc->cdesc = cdesc;
1342
1343 return cdesc;
1344
1345out_err_desc_get:
1346 while (i--)
1347 dwc_desc_put(dwc, cdesc->desc[i]);
1348out_err_alloc:
1349 kfree(cdesc);
1350out_err:
1351 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1352 return (struct dw_cyclic_desc *)retval;
1353}
1354EXPORT_SYMBOL(dw_dma_cyclic_prep);
1355
1356/**
1357 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1358 * @chan: the DMA channel to free
1359 */
1360void dw_dma_cyclic_free(struct dma_chan *chan)
1361{
1362 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1363 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1364 struct dw_cyclic_desc *cdesc = dwc->cdesc;
1365 int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301366 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001367
1368 dev_dbg(chan2dev(&dwc->chan), "cyclic free\n");
1369
1370 if (!cdesc)
1371 return;
1372
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301373 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001374
1375 channel_clear_bit(dw, CH_EN, dwc->mask);
1376 while (dma_readl(dw, CH_EN) & dwc->mask)
1377 cpu_relax();
1378
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001379 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1380 dma_writel(dw, CLEAR.XFER, dwc->mask);
1381
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301382 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001383
1384 for (i = 0; i < cdesc->periods; i++)
1385 dwc_desc_put(dwc, cdesc->desc[i]);
1386
1387 kfree(cdesc->desc);
1388 kfree(cdesc);
1389
1390 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1391}
1392EXPORT_SYMBOL(dw_dma_cyclic_free);
1393
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001394/*----------------------------------------------------------------------*/
1395
1396static void dw_dma_off(struct dw_dma *dw)
1397{
Viresh Kumar61e183f2011-11-17 16:01:29 +05301398 int i;
1399
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001400 dma_writel(dw, CFG, 0);
1401
1402 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001403 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1404 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1405 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1406
1407 while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
1408 cpu_relax();
Viresh Kumar61e183f2011-11-17 16:01:29 +05301409
1410 for (i = 0; i < dw->dma.chancnt; i++)
1411 dw->chan[i].initialized = false;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001412}
1413
1414static int __init dw_probe(struct platform_device *pdev)
1415{
1416 struct dw_dma_platform_data *pdata;
1417 struct resource *io;
1418 struct dw_dma *dw;
1419 size_t size;
1420 int irq;
1421 int err;
1422 int i;
1423
Viresh Kumar6c618c92012-02-01 16:12:22 +05301424 pdata = dev_get_platdata(&pdev->dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001425 if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
1426 return -EINVAL;
1427
1428 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1429 if (!io)
1430 return -EINVAL;
1431
1432 irq = platform_get_irq(pdev, 0);
1433 if (irq < 0)
1434 return irq;
1435
1436 size = sizeof(struct dw_dma);
1437 size += pdata->nr_channels * sizeof(struct dw_dma_chan);
1438 dw = kzalloc(size, GFP_KERNEL);
1439 if (!dw)
1440 return -ENOMEM;
1441
1442 if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) {
1443 err = -EBUSY;
1444 goto err_kfree;
1445 }
1446
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001447 dw->regs = ioremap(io->start, DW_REGLEN);
1448 if (!dw->regs) {
1449 err = -ENOMEM;
1450 goto err_release_r;
1451 }
1452
1453 dw->clk = clk_get(&pdev->dev, "hclk");
1454 if (IS_ERR(dw->clk)) {
1455 err = PTR_ERR(dw->clk);
1456 goto err_clk;
1457 }
1458 clk_enable(dw->clk);
1459
1460 /* force dma off, just in case */
1461 dw_dma_off(dw);
1462
1463 err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw);
1464 if (err)
1465 goto err_irq;
1466
1467 platform_set_drvdata(pdev, dw);
1468
1469 tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
1470
1471 dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
1472
1473 INIT_LIST_HEAD(&dw->dma.channels);
Barry Song463894702011-09-15 03:06:30 -07001474 for (i = 0; i < pdata->nr_channels; i++) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001475 struct dw_dma_chan *dwc = &dw->chan[i];
1476
1477 dwc->chan.device = &dw->dma;
Russell King - ARM Linux4d4e58d2012-03-06 22:34:06 +00001478 dwc->chan.cookie = dwc->chan.completed_cookie = 1;
Viresh Kumarb0c31302011-03-03 15:47:21 +05301479 if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
1480 list_add_tail(&dwc->chan.device_node,
1481 &dw->dma.channels);
1482 else
1483 list_add(&dwc->chan.device_node, &dw->dma.channels);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001484
Viresh Kumar93317e82011-03-03 15:47:22 +05301485 /* 7 is highest priority & 0 is lowest. */
1486 if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
Viresh Kumare8d9f872012-02-01 16:12:21 +05301487 dwc->priority = pdata->nr_channels - i - 1;
Viresh Kumar93317e82011-03-03 15:47:22 +05301488 else
1489 dwc->priority = i;
1490
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001491 dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
1492 spin_lock_init(&dwc->lock);
1493 dwc->mask = 1 << i;
1494
1495 INIT_LIST_HEAD(&dwc->active_list);
1496 INIT_LIST_HEAD(&dwc->queue);
1497 INIT_LIST_HEAD(&dwc->free_list);
1498
1499 channel_clear_bit(dw, CH_EN, dwc->mask);
1500 }
1501
1502 /* Clear/disable all interrupts on all channels. */
1503 dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001504 dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
1505 dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
1506 dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
1507
1508 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001509 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1510 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1511 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1512
1513 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
1514 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
Jamie Iles95ea7592011-01-21 14:11:54 +00001515 if (pdata->is_private)
1516 dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001517 dw->dma.dev = &pdev->dev;
1518 dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
1519 dw->dma.device_free_chan_resources = dwc_free_chan_resources;
1520
1521 dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
1522
1523 dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
Linus Walleijc3635c72010-03-26 16:44:01 -07001524 dw->dma.device_control = dwc_control;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001525
Linus Walleij07934482010-03-26 16:50:49 -07001526 dw->dma.device_tx_status = dwc_tx_status;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001527 dw->dma.device_issue_pending = dwc_issue_pending;
1528
1529 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1530
1531 printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n",
Barry Song463894702011-09-15 03:06:30 -07001532 dev_name(&pdev->dev), pdata->nr_channels);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001533
1534 dma_async_device_register(&dw->dma);
1535
1536 return 0;
1537
1538err_irq:
1539 clk_disable(dw->clk);
1540 clk_put(dw->clk);
1541err_clk:
1542 iounmap(dw->regs);
1543 dw->regs = NULL;
1544err_release_r:
1545 release_resource(io);
1546err_kfree:
1547 kfree(dw);
1548 return err;
1549}
1550
1551static int __exit dw_remove(struct platform_device *pdev)
1552{
1553 struct dw_dma *dw = platform_get_drvdata(pdev);
1554 struct dw_dma_chan *dwc, *_dwc;
1555 struct resource *io;
1556
1557 dw_dma_off(dw);
1558 dma_async_device_unregister(&dw->dma);
1559
1560 free_irq(platform_get_irq(pdev, 0), dw);
1561 tasklet_kill(&dw->tasklet);
1562
1563 list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
1564 chan.device_node) {
1565 list_del(&dwc->chan.device_node);
1566 channel_clear_bit(dw, CH_EN, dwc->mask);
1567 }
1568
1569 clk_disable(dw->clk);
1570 clk_put(dw->clk);
1571
1572 iounmap(dw->regs);
1573 dw->regs = NULL;
1574
1575 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1576 release_mem_region(io->start, DW_REGLEN);
1577
1578 kfree(dw);
1579
1580 return 0;
1581}
1582
1583static void dw_shutdown(struct platform_device *pdev)
1584{
1585 struct dw_dma *dw = platform_get_drvdata(pdev);
1586
1587 dw_dma_off(platform_get_drvdata(pdev));
1588 clk_disable(dw->clk);
1589}
1590
Magnus Damm4a256b52009-07-08 13:22:18 +02001591static int dw_suspend_noirq(struct device *dev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001592{
Magnus Damm4a256b52009-07-08 13:22:18 +02001593 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001594 struct dw_dma *dw = platform_get_drvdata(pdev);
1595
1596 dw_dma_off(platform_get_drvdata(pdev));
1597 clk_disable(dw->clk);
Viresh Kumar61e183f2011-11-17 16:01:29 +05301598
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001599 return 0;
1600}
1601
Magnus Damm4a256b52009-07-08 13:22:18 +02001602static int dw_resume_noirq(struct device *dev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001603{
Magnus Damm4a256b52009-07-08 13:22:18 +02001604 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001605 struct dw_dma *dw = platform_get_drvdata(pdev);
1606
1607 clk_enable(dw->clk);
1608 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1609 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001610}
1611
Alexey Dobriyan47145212009-12-14 18:00:08 -08001612static const struct dev_pm_ops dw_dev_pm_ops = {
Magnus Damm4a256b52009-07-08 13:22:18 +02001613 .suspend_noirq = dw_suspend_noirq,
1614 .resume_noirq = dw_resume_noirq,
Rajeev KUMAR7414a1b2012-02-01 16:12:17 +05301615 .freeze_noirq = dw_suspend_noirq,
1616 .thaw_noirq = dw_resume_noirq,
1617 .restore_noirq = dw_resume_noirq,
1618 .poweroff_noirq = dw_suspend_noirq,
Magnus Damm4a256b52009-07-08 13:22:18 +02001619};
1620
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001621static struct platform_driver dw_driver = {
1622 .remove = __exit_p(dw_remove),
1623 .shutdown = dw_shutdown,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001624 .driver = {
1625 .name = "dw_dmac",
Magnus Damm4a256b52009-07-08 13:22:18 +02001626 .pm = &dw_dev_pm_ops,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001627 },
1628};
1629
1630static int __init dw_init(void)
1631{
1632 return platform_driver_probe(&dw_driver, dw_probe);
1633}
Viresh Kumarcb689a72011-03-03 15:47:15 +05301634subsys_initcall(dw_init);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001635
1636static void __exit dw_exit(void)
1637{
1638 platform_driver_unregister(&dw_driver);
1639}
1640module_exit(dw_exit);
1641
1642MODULE_LICENSE("GPL v2");
1643MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001644MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Viresh Kumaraecb7b62011-05-24 14:04:09 +05301645MODULE_AUTHOR("Viresh Kumar <viresh.kumar@st.com>");