blob: eec675bf4f9540128addb797de9305e8d8d33af7 [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
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/clk.h>
12#include <linux/delay.h>
13#include <linux/dmaengine.h>
14#include <linux/dma-mapping.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/io.h>
18#include <linux/mm.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/slab.h>
22
23#include "dw_dmac_regs.h"
24
25/*
26 * This supports the Synopsys "DesignWare AHB Central DMA Controller",
27 * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
28 * of which use ARM any more). See the "Databook" from Synopsys for
29 * information beyond what licensees probably provide.
30 *
31 * The driver has currently been tested only with the Atmel AT32AP7000,
32 * which does not support descriptor writeback.
33 */
34
Jamie Ilesf301c062011-01-21 14:11:53 +000035#define DWC_DEFAULT_CTLLO(private) ({ \
36 struct dw_dma_slave *__slave = (private); \
37 int dms = __slave ? __slave->dst_master : 0; \
38 int sms = __slave ? __slave->src_master : 1; \
Viresh Kumare51dc532011-03-03 15:47:25 +053039 u8 smsize = __slave ? __slave->src_msize : DW_DMA_MSIZE_16; \
40 u8 dmsize = __slave ? __slave->dst_msize : DW_DMA_MSIZE_16; \
Jamie Ilesf301c062011-01-21 14:11:53 +000041 \
Viresh KUMARee665092011-03-04 15:42:51 +053042 (DWC_CTLL_DST_MSIZE(dmsize) \
43 | DWC_CTLL_SRC_MSIZE(smsize) \
Jamie Ilesf301c062011-01-21 14:11:53 +000044 | DWC_CTLL_LLP_D_EN \
45 | DWC_CTLL_LLP_S_EN \
46 | DWC_CTLL_DMS(dms) \
47 | DWC_CTLL_SMS(sms)); \
48 })
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070049
50/*
51 * This is configuration-dependent and usually a funny size like 4095.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070052 *
53 * Note that this is a transfer count, i.e. if we transfer 32-bit
Viresh Kumar418e7402011-03-04 15:42:50 +053054 * words, we can do 16380 bytes per descriptor.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070055 *
56 * This parameter is also system-specific.
57 */
Viresh Kumar418e7402011-03-04 15:42:50 +053058#define DWC_MAX_COUNT 4095U
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070059
60/*
61 * Number of descriptors to allocate for each channel. This should be
62 * made configurable somehow; preferably, the clients (at least the
63 * ones using slave transfers) should be able to give us a hint.
64 */
65#define NR_DESCS_PER_CHANNEL 64
66
67/*----------------------------------------------------------------------*/
68
69/*
70 * Because we're not relying on writeback from the controller (it may not
71 * even be configured into the core!) we don't need to use dma_pool. These
72 * descriptors -- and associated data -- are cacheable. We do need to make
73 * sure their dcache entries are written back before handing them off to
74 * the controller, though.
75 */
76
Dan Williams41d5e592009-01-06 11:38:21 -070077static struct device *chan2dev(struct dma_chan *chan)
78{
79 return &chan->dev->device;
80}
81static struct device *chan2parent(struct dma_chan *chan)
82{
83 return chan->dev->device.parent;
84}
85
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070086static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
87{
88 return list_entry(dwc->active_list.next, struct dw_desc, desc_node);
89}
90
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070091static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
92{
93 struct dw_desc *desc, *_desc;
94 struct dw_desc *ret = NULL;
95 unsigned int i = 0;
Viresh Kumar69cea5a2011-04-15 16:03:35 +053096 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070097
Viresh Kumar69cea5a2011-04-15 16:03:35 +053098 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070099 list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
100 if (async_tx_test_ack(&desc->txd)) {
101 list_del(&desc->desc_node);
102 ret = desc;
103 break;
104 }
Dan Williams41d5e592009-01-06 11:38:21 -0700105 dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700106 i++;
107 }
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530108 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700109
Dan Williams41d5e592009-01-06 11:38:21 -0700110 dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700111
112 return ret;
113}
114
115static void dwc_sync_desc_for_cpu(struct dw_dma_chan *dwc, struct dw_desc *desc)
116{
117 struct dw_desc *child;
118
Dan Williamse0bd0f82009-09-08 17:53:02 -0700119 list_for_each_entry(child, &desc->tx_list, desc_node)
Dan Williams41d5e592009-01-06 11:38:21 -0700120 dma_sync_single_for_cpu(chan2parent(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700121 child->txd.phys, sizeof(child->lli),
122 DMA_TO_DEVICE);
Dan Williams41d5e592009-01-06 11:38:21 -0700123 dma_sync_single_for_cpu(chan2parent(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700124 desc->txd.phys, sizeof(desc->lli),
125 DMA_TO_DEVICE);
126}
127
128/*
129 * Move a descriptor, including any children, to the free list.
130 * `desc' must not be on any lists.
131 */
132static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
133{
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530134 unsigned long flags;
135
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700136 if (desc) {
137 struct dw_desc *child;
138
139 dwc_sync_desc_for_cpu(dwc, desc);
140
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530141 spin_lock_irqsave(&dwc->lock, flags);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700142 list_for_each_entry(child, &desc->tx_list, desc_node)
Dan Williams41d5e592009-01-06 11:38:21 -0700143 dev_vdbg(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700144 "moving child desc %p to freelist\n",
145 child);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700146 list_splice_init(&desc->tx_list, &dwc->free_list);
Dan Williams41d5e592009-01-06 11:38:21 -0700147 dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700148 list_add(&desc->desc_node, &dwc->free_list);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530149 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700150 }
151}
152
153/* Called with dwc->lock held and bh disabled */
154static dma_cookie_t
155dwc_assign_cookie(struct dw_dma_chan *dwc, struct dw_desc *desc)
156{
157 dma_cookie_t cookie = dwc->chan.cookie;
158
159 if (++cookie < 0)
160 cookie = 1;
161
162 dwc->chan.cookie = cookie;
163 desc->txd.cookie = cookie;
164
165 return cookie;
166}
167
168/*----------------------------------------------------------------------*/
169
170/* Called with dwc->lock held and bh disabled */
171static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
172{
173 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
174
175 /* ASSERT: channel is idle */
176 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700177 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700178 "BUG: Attempted to start non-idle channel\n");
Dan Williams41d5e592009-01-06 11:38:21 -0700179 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700180 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
181 channel_readl(dwc, SAR),
182 channel_readl(dwc, DAR),
183 channel_readl(dwc, LLP),
184 channel_readl(dwc, CTL_HI),
185 channel_readl(dwc, CTL_LO));
186
187 /* The tasklet will hopefully advance the queue... */
188 return;
189 }
190
191 channel_writel(dwc, LLP, first->txd.phys);
192 channel_writel(dwc, CTL_LO,
193 DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
194 channel_writel(dwc, CTL_HI, 0);
195 channel_set_bit(dw, CH_EN, dwc->mask);
196}
197
198/*----------------------------------------------------------------------*/
199
200static void
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530201dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
202 bool callback_required)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700203{
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530204 dma_async_tx_callback callback = NULL;
205 void *param = NULL;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700206 struct dma_async_tx_descriptor *txd = &desc->txd;
Viresh Kumare5180762011-03-03 15:47:20 +0530207 struct dw_desc *child;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530208 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700209
Dan Williams41d5e592009-01-06 11:38:21 -0700210 dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700211
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530212 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700213 dwc->completed = txd->cookie;
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530214 if (callback_required) {
215 callback = txd->callback;
216 param = txd->callback_param;
217 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700218
219 dwc_sync_desc_for_cpu(dwc, desc);
Viresh Kumare5180762011-03-03 15:47:20 +0530220
221 /* async_tx_ack */
222 list_for_each_entry(child, &desc->tx_list, desc_node)
223 async_tx_ack(&child->txd);
224 async_tx_ack(&desc->txd);
225
Dan Williamse0bd0f82009-09-08 17:53:02 -0700226 list_splice_init(&desc->tx_list, &dwc->free_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700227 list_move(&desc->desc_node, &dwc->free_list);
228
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700229 if (!dwc->chan.private) {
230 struct device *parent = chan2parent(&dwc->chan);
231 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
232 if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
233 dma_unmap_single(parent, desc->lli.dar,
234 desc->len, DMA_FROM_DEVICE);
235 else
236 dma_unmap_page(parent, desc->lli.dar,
237 desc->len, DMA_FROM_DEVICE);
238 }
239 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
240 if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
241 dma_unmap_single(parent, desc->lli.sar,
242 desc->len, DMA_TO_DEVICE);
243 else
244 dma_unmap_page(parent, desc->lli.sar,
245 desc->len, DMA_TO_DEVICE);
246 }
247 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700248
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530249 spin_unlock_irqrestore(&dwc->lock, flags);
250
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530251 if (callback_required && callback)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700252 callback(param);
253}
254
255static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
256{
257 struct dw_desc *desc, *_desc;
258 LIST_HEAD(list);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530259 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700260
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530261 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700262 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700263 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700264 "BUG: XFER bit set, but channel not idle!\n");
265
266 /* Try to continue after resetting the channel... */
267 channel_clear_bit(dw, CH_EN, dwc->mask);
268 while (dma_readl(dw, CH_EN) & dwc->mask)
269 cpu_relax();
270 }
271
272 /*
273 * Submit queued descriptors ASAP, i.e. before we go through
274 * the completed ones.
275 */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700276 list_splice_init(&dwc->active_list, &list);
Viresh Kumarf336e422011-03-03 15:47:16 +0530277 if (!list_empty(&dwc->queue)) {
278 list_move(dwc->queue.next, &dwc->active_list);
279 dwc_dostart(dwc, dwc_first_active(dwc));
280 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700281
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530282 spin_unlock_irqrestore(&dwc->lock, flags);
283
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700284 list_for_each_entry_safe(desc, _desc, &list, desc_node)
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530285 dwc_descriptor_complete(dwc, desc, true);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700286}
287
288static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
289{
290 dma_addr_t llp;
291 struct dw_desc *desc, *_desc;
292 struct dw_desc *child;
293 u32 status_xfer;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530294 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700295
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530296 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700297 /*
298 * Clear block interrupt flag before scanning so that we don't
299 * miss any, and read LLP before RAW_XFER to ensure it is
300 * valid if we decide to scan the list.
301 */
302 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
303 llp = channel_readl(dwc, LLP);
304 status_xfer = dma_readl(dw, RAW.XFER);
305
306 if (status_xfer & dwc->mask) {
307 /* Everything we've submitted is done */
308 dma_writel(dw, CLEAR.XFER, dwc->mask);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530309 spin_unlock_irqrestore(&dwc->lock, flags);
310
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700311 dwc_complete_all(dw, dwc);
312 return;
313 }
314
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530315 if (list_empty(&dwc->active_list)) {
316 spin_unlock_irqrestore(&dwc->lock, flags);
Jamie Iles087809f2011-01-21 14:11:52 +0000317 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530318 }
Jamie Iles087809f2011-01-21 14:11:52 +0000319
Dan Williams41d5e592009-01-06 11:38:21 -0700320 dev_vdbg(chan2dev(&dwc->chan), "scan_descriptors: llp=0x%x\n", llp);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700321
322 list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
Viresh Kumar84adccf2011-03-24 11:32:15 +0530323 /* check first descriptors addr */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530324 if (desc->txd.phys == llp) {
325 spin_unlock_irqrestore(&dwc->lock, flags);
Viresh Kumar84adccf2011-03-24 11:32:15 +0530326 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530327 }
Viresh Kumar84adccf2011-03-24 11:32:15 +0530328
329 /* check first descriptors llp */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530330 if (desc->lli.llp == llp) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700331 /* This one is currently in progress */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530332 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700333 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530334 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700335
Dan Williamse0bd0f82009-09-08 17:53:02 -0700336 list_for_each_entry(child, &desc->tx_list, desc_node)
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530337 if (child->lli.llp == llp) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700338 /* Currently in progress */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530339 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700340 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530341 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700342
343 /*
344 * No descriptors so far seem to be in progress, i.e.
345 * this one must be done.
346 */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530347 spin_unlock_irqrestore(&dwc->lock, flags);
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530348 dwc_descriptor_complete(dwc, desc, true);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530349 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700350 }
351
Dan Williams41d5e592009-01-06 11:38:21 -0700352 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700353 "BUG: All descriptors done, but channel not idle!\n");
354
355 /* Try to continue after resetting the channel... */
356 channel_clear_bit(dw, CH_EN, dwc->mask);
357 while (dma_readl(dw, CH_EN) & dwc->mask)
358 cpu_relax();
359
360 if (!list_empty(&dwc->queue)) {
Viresh Kumarf336e422011-03-03 15:47:16 +0530361 list_move(dwc->queue.next, &dwc->active_list);
362 dwc_dostart(dwc, dwc_first_active(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700363 }
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530364 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700365}
366
367static void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
368{
Dan Williams41d5e592009-01-06 11:38:21 -0700369 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700370 " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
371 lli->sar, lli->dar, lli->llp,
372 lli->ctlhi, lli->ctllo);
373}
374
375static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
376{
377 struct dw_desc *bad_desc;
378 struct dw_desc *child;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530379 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700380
381 dwc_scan_descriptors(dw, dwc);
382
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530383 spin_lock_irqsave(&dwc->lock, flags);
384
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700385 /*
386 * The descriptor currently at the head of the active list is
387 * borked. Since we don't have any way to report errors, we'll
388 * just have to scream loudly and try to carry on.
389 */
390 bad_desc = dwc_first_active(dwc);
391 list_del_init(&bad_desc->desc_node);
Viresh Kumarf336e422011-03-03 15:47:16 +0530392 list_move(dwc->queue.next, dwc->active_list.prev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700393
394 /* Clear the error flag and try to restart the controller */
395 dma_writel(dw, CLEAR.ERROR, dwc->mask);
396 if (!list_empty(&dwc->active_list))
397 dwc_dostart(dwc, dwc_first_active(dwc));
398
399 /*
400 * KERN_CRITICAL may seem harsh, but since this only happens
401 * when someone submits a bad physical address in a
402 * descriptor, we should consider ourselves lucky that the
403 * controller flagged an error instead of scribbling over
404 * random memory locations.
405 */
Dan Williams41d5e592009-01-06 11:38:21 -0700406 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700407 "Bad descriptor submitted for DMA!\n");
Dan Williams41d5e592009-01-06 11:38:21 -0700408 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700409 " cookie: %d\n", bad_desc->txd.cookie);
410 dwc_dump_lli(dwc, &bad_desc->lli);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700411 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700412 dwc_dump_lli(dwc, &child->lli);
413
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530414 spin_unlock_irqrestore(&dwc->lock, flags);
415
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700416 /* Pretend the descriptor completed successfully */
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530417 dwc_descriptor_complete(dwc, bad_desc, true);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700418}
419
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200420/* --------------------- Cyclic DMA API extensions -------------------- */
421
422inline dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
423{
424 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
425 return channel_readl(dwc, SAR);
426}
427EXPORT_SYMBOL(dw_dma_get_src_addr);
428
429inline dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
430{
431 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
432 return channel_readl(dwc, DAR);
433}
434EXPORT_SYMBOL(dw_dma_get_dst_addr);
435
436/* called with dwc->lock held and all DMAC interrupts disabled */
437static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
438 u32 status_block, u32 status_err, u32 status_xfer)
439{
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530440 unsigned long flags;
441
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200442 if (status_block & dwc->mask) {
443 void (*callback)(void *param);
444 void *callback_param;
445
446 dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
447 channel_readl(dwc, LLP));
448 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
449
450 callback = dwc->cdesc->period_callback;
451 callback_param = dwc->cdesc->period_callback_param;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530452
453 if (callback)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200454 callback(callback_param);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200455 }
456
457 /*
458 * Error and transfer complete are highly unlikely, and will most
459 * likely be due to a configuration error by the user.
460 */
461 if (unlikely(status_err & dwc->mask) ||
462 unlikely(status_xfer & dwc->mask)) {
463 int i;
464
465 dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s "
466 "interrupt, stopping DMA transfer\n",
467 status_xfer ? "xfer" : "error");
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530468
469 spin_lock_irqsave(&dwc->lock, flags);
470
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200471 dev_err(chan2dev(&dwc->chan),
472 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
473 channel_readl(dwc, SAR),
474 channel_readl(dwc, DAR),
475 channel_readl(dwc, LLP),
476 channel_readl(dwc, CTL_HI),
477 channel_readl(dwc, CTL_LO));
478
479 channel_clear_bit(dw, CH_EN, dwc->mask);
480 while (dma_readl(dw, CH_EN) & dwc->mask)
481 cpu_relax();
482
483 /* make sure DMA does not restart by loading a new list */
484 channel_writel(dwc, LLP, 0);
485 channel_writel(dwc, CTL_LO, 0);
486 channel_writel(dwc, CTL_HI, 0);
487
488 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
489 dma_writel(dw, CLEAR.ERROR, dwc->mask);
490 dma_writel(dw, CLEAR.XFER, dwc->mask);
491
492 for (i = 0; i < dwc->cdesc->periods; i++)
493 dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530494
495 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200496 }
497}
498
499/* ------------------------------------------------------------------------- */
500
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700501static void dw_dma_tasklet(unsigned long data)
502{
503 struct dw_dma *dw = (struct dw_dma *)data;
504 struct dw_dma_chan *dwc;
505 u32 status_block;
506 u32 status_xfer;
507 u32 status_err;
508 int i;
509
510 status_block = dma_readl(dw, RAW.BLOCK);
Haavard Skinnemoen7fe7b2f2008-10-03 15:23:46 -0700511 status_xfer = dma_readl(dw, RAW.XFER);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700512 status_err = dma_readl(dw, RAW.ERROR);
513
514 dev_vdbg(dw->dma.dev, "tasklet: status_block=%x status_err=%x\n",
515 status_block, status_err);
516
517 for (i = 0; i < dw->dma.chancnt; i++) {
518 dwc = &dw->chan[i];
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200519 if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
520 dwc_handle_cyclic(dw, dwc, status_block, status_err,
521 status_xfer);
522 else if (status_err & (1 << i))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700523 dwc_handle_error(dw, dwc);
524 else if ((status_block | status_xfer) & (1 << i))
525 dwc_scan_descriptors(dw, dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700526 }
527
528 /*
529 * Re-enable interrupts. Block Complete interrupts are only
530 * enabled if the INT_EN bit in the descriptor is set. This
531 * will trigger a scan before the whole list is done.
532 */
533 channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
534 channel_set_bit(dw, MASK.BLOCK, dw->all_chan_mask);
535 channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
536}
537
538static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
539{
540 struct dw_dma *dw = dev_id;
541 u32 status;
542
543 dev_vdbg(dw->dma.dev, "interrupt: status=0x%x\n",
544 dma_readl(dw, STATUS_INT));
545
546 /*
547 * Just disable the interrupts. We'll turn them back on in the
548 * softirq handler.
549 */
550 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
551 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
552 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
553
554 status = dma_readl(dw, STATUS_INT);
555 if (status) {
556 dev_err(dw->dma.dev,
557 "BUG: Unexpected interrupts pending: 0x%x\n",
558 status);
559
560 /* Try to recover */
561 channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
562 channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
563 channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
564 channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
565 channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
566 }
567
568 tasklet_schedule(&dw->tasklet);
569
570 return IRQ_HANDLED;
571}
572
573/*----------------------------------------------------------------------*/
574
575static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
576{
577 struct dw_desc *desc = txd_to_dw_desc(tx);
578 struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
579 dma_cookie_t cookie;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530580 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700581
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530582 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700583 cookie = dwc_assign_cookie(dwc, desc);
584
585 /*
586 * REVISIT: We should attempt to chain as many descriptors as
587 * possible, perhaps even appending to those already submitted
588 * for DMA. But this is hard to do in a race-free manner.
589 */
590 if (list_empty(&dwc->active_list)) {
Dan Williams41d5e592009-01-06 11:38:21 -0700591 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700592 desc->txd.cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700593 list_add_tail(&desc->desc_node, &dwc->active_list);
Viresh Kumarf336e422011-03-03 15:47:16 +0530594 dwc_dostart(dwc, dwc_first_active(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700595 } else {
Dan Williams41d5e592009-01-06 11:38:21 -0700596 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700597 desc->txd.cookie);
598
599 list_add_tail(&desc->desc_node, &dwc->queue);
600 }
601
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530602 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700603
604 return cookie;
605}
606
607static struct dma_async_tx_descriptor *
608dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
609 size_t len, unsigned long flags)
610{
611 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
612 struct dw_desc *desc;
613 struct dw_desc *first;
614 struct dw_desc *prev;
615 size_t xfer_count;
616 size_t offset;
617 unsigned int src_width;
618 unsigned int dst_width;
619 u32 ctllo;
620
Dan Williams41d5e592009-01-06 11:38:21 -0700621 dev_vdbg(chan2dev(chan), "prep_dma_memcpy d0x%x s0x%x l0x%zx f0x%lx\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700622 dest, src, len, flags);
623
624 if (unlikely(!len)) {
Dan Williams41d5e592009-01-06 11:38:21 -0700625 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700626 return NULL;
627 }
628
629 /*
630 * We can be a lot more clever here, but this should take care
631 * of the most common optimization.
632 */
Viresh Kumara0227452011-03-03 15:47:18 +0530633 if (!((src | dest | len) & 7))
634 src_width = dst_width = 3;
635 else if (!((src | dest | len) & 3))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700636 src_width = dst_width = 2;
637 else if (!((src | dest | len) & 1))
638 src_width = dst_width = 1;
639 else
640 src_width = dst_width = 0;
641
Jamie Ilesf301c062011-01-21 14:11:53 +0000642 ctllo = DWC_DEFAULT_CTLLO(chan->private)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700643 | DWC_CTLL_DST_WIDTH(dst_width)
644 | DWC_CTLL_SRC_WIDTH(src_width)
645 | DWC_CTLL_DST_INC
646 | DWC_CTLL_SRC_INC
647 | DWC_CTLL_FC_M2M;
648 prev = first = NULL;
649
650 for (offset = 0; offset < len; offset += xfer_count << src_width) {
651 xfer_count = min_t(size_t, (len - offset) >> src_width,
652 DWC_MAX_COUNT);
653
654 desc = dwc_desc_get(dwc);
655 if (!desc)
656 goto err_desc_get;
657
658 desc->lli.sar = src + offset;
659 desc->lli.dar = dest + offset;
660 desc->lli.ctllo = ctllo;
661 desc->lli.ctlhi = xfer_count;
662
663 if (!first) {
664 first = desc;
665 } else {
666 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700667 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700668 prev->txd.phys, sizeof(prev->lli),
669 DMA_TO_DEVICE);
670 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700671 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700672 }
673 prev = desc;
674 }
675
676
677 if (flags & DMA_PREP_INTERRUPT)
678 /* Trigger interrupt after last block */
679 prev->lli.ctllo |= DWC_CTLL_INT_EN;
680
681 prev->lli.llp = 0;
Dan Williams41d5e592009-01-06 11:38:21 -0700682 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700683 prev->txd.phys, sizeof(prev->lli),
684 DMA_TO_DEVICE);
685
686 first->txd.flags = flags;
687 first->len = len;
688
689 return &first->txd;
690
691err_desc_get:
692 dwc_desc_put(dwc, first);
693 return NULL;
694}
695
696static struct dma_async_tx_descriptor *
697dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
698 unsigned int sg_len, enum dma_data_direction direction,
699 unsigned long flags)
700{
701 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Dan Williams287d8592009-02-18 14:48:26 -0800702 struct dw_dma_slave *dws = chan->private;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700703 struct dw_desc *prev;
704 struct dw_desc *first;
705 u32 ctllo;
706 dma_addr_t reg;
707 unsigned int reg_width;
708 unsigned int mem_width;
709 unsigned int i;
710 struct scatterlist *sg;
711 size_t total_len = 0;
712
Dan Williams41d5e592009-01-06 11:38:21 -0700713 dev_vdbg(chan2dev(chan), "prep_dma_slave\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700714
715 if (unlikely(!dws || !sg_len))
716 return NULL;
717
Dan Williams74465b42009-01-06 11:38:16 -0700718 reg_width = dws->reg_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700719 prev = first = NULL;
720
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700721 switch (direction) {
722 case DMA_TO_DEVICE:
Jamie Ilesf301c062011-01-21 14:11:53 +0000723 ctllo = (DWC_DEFAULT_CTLLO(chan->private)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700724 | DWC_CTLL_DST_WIDTH(reg_width)
725 | DWC_CTLL_DST_FIX
726 | DWC_CTLL_SRC_INC
Viresh KUMARee665092011-03-04 15:42:51 +0530727 | DWC_CTLL_FC(dws->fc));
Dan Williams74465b42009-01-06 11:38:16 -0700728 reg = dws->tx_reg;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700729 for_each_sg(sgl, sg, sg_len, i) {
730 struct dw_desc *desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530731 u32 len, dlen, mem;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700732
733 mem = sg_phys(sg);
734 len = sg_dma_len(sg);
735 mem_width = 2;
736 if (unlikely(mem & 3 || len & 3))
737 mem_width = 0;
738
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530739slave_sg_todev_fill_desc:
740 desc = dwc_desc_get(dwc);
741 if (!desc) {
742 dev_err(chan2dev(chan),
743 "not enough descriptors available\n");
744 goto err_desc_get;
745 }
746
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700747 desc->lli.sar = mem;
748 desc->lli.dar = reg;
749 desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530750 if ((len >> mem_width) > DWC_MAX_COUNT) {
751 dlen = DWC_MAX_COUNT << mem_width;
752 mem += dlen;
753 len -= dlen;
754 } else {
755 dlen = len;
756 len = 0;
757 }
758
759 desc->lli.ctlhi = dlen >> mem_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700760
761 if (!first) {
762 first = desc;
763 } else {
764 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700765 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700766 prev->txd.phys,
767 sizeof(prev->lli),
768 DMA_TO_DEVICE);
769 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700770 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700771 }
772 prev = desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530773 total_len += dlen;
774
775 if (len)
776 goto slave_sg_todev_fill_desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700777 }
778 break;
779 case DMA_FROM_DEVICE:
Jamie Ilesf301c062011-01-21 14:11:53 +0000780 ctllo = (DWC_DEFAULT_CTLLO(chan->private)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700781 | DWC_CTLL_SRC_WIDTH(reg_width)
782 | DWC_CTLL_DST_INC
783 | DWC_CTLL_SRC_FIX
Viresh KUMARee665092011-03-04 15:42:51 +0530784 | DWC_CTLL_FC(dws->fc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700785
Dan Williams74465b42009-01-06 11:38:16 -0700786 reg = dws->rx_reg;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700787 for_each_sg(sgl, sg, sg_len, i) {
788 struct dw_desc *desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530789 u32 len, dlen, mem;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700790
791 mem = sg_phys(sg);
792 len = sg_dma_len(sg);
793 mem_width = 2;
794 if (unlikely(mem & 3 || len & 3))
795 mem_width = 0;
796
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530797slave_sg_fromdev_fill_desc:
798 desc = dwc_desc_get(dwc);
799 if (!desc) {
800 dev_err(chan2dev(chan),
801 "not enough descriptors available\n");
802 goto err_desc_get;
803 }
804
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700805 desc->lli.sar = reg;
806 desc->lli.dar = mem;
807 desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530808 if ((len >> reg_width) > DWC_MAX_COUNT) {
809 dlen = DWC_MAX_COUNT << reg_width;
810 mem += dlen;
811 len -= dlen;
812 } else {
813 dlen = len;
814 len = 0;
815 }
816 desc->lli.ctlhi = dlen >> reg_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700817
818 if (!first) {
819 first = desc;
820 } else {
821 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700822 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700823 prev->txd.phys,
824 sizeof(prev->lli),
825 DMA_TO_DEVICE);
826 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700827 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700828 }
829 prev = desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530830 total_len += dlen;
831
832 if (len)
833 goto slave_sg_fromdev_fill_desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700834 }
835 break;
836 default:
837 return NULL;
838 }
839
840 if (flags & DMA_PREP_INTERRUPT)
841 /* Trigger interrupt after last block */
842 prev->lli.ctllo |= DWC_CTLL_INT_EN;
843
844 prev->lli.llp = 0;
Dan Williams41d5e592009-01-06 11:38:21 -0700845 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700846 prev->txd.phys, sizeof(prev->lli),
847 DMA_TO_DEVICE);
848
849 first->len = total_len;
850
851 return &first->txd;
852
853err_desc_get:
854 dwc_desc_put(dwc, first);
855 return NULL;
856}
857
Linus Walleij05827632010-05-17 16:30:42 -0700858static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
859 unsigned long arg)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700860{
861 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
862 struct dw_dma *dw = to_dw_dma(chan->device);
863 struct dw_desc *desc, *_desc;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530864 unsigned long flags;
Linus Walleija7c57cf2011-04-19 08:31:32 +0800865 u32 cfglo;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700866 LIST_HEAD(list);
867
Linus Walleija7c57cf2011-04-19 08:31:32 +0800868 if (cmd == DMA_PAUSE) {
869 spin_lock_irqsave(&dwc->lock, flags);
870
871 cfglo = channel_readl(dwc, CFG_LO);
872 channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
873 while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY))
874 cpu_relax();
875
876 dwc->paused = true;
877 spin_unlock_irqrestore(&dwc->lock, flags);
878 } else if (cmd == DMA_RESUME) {
879 if (!dwc->paused)
880 return 0;
881
882 spin_lock_irqsave(&dwc->lock, flags);
883
884 cfglo = channel_readl(dwc, CFG_LO);
885 channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
886 dwc->paused = false;
887
888 spin_unlock_irqrestore(&dwc->lock, flags);
889 } else if (cmd == DMA_TERMINATE_ALL) {
890 spin_lock_irqsave(&dwc->lock, flags);
891
892 channel_clear_bit(dw, CH_EN, dwc->mask);
893 while (dma_readl(dw, CH_EN) & dwc->mask)
894 cpu_relax();
895
896 dwc->paused = false;
897
898 /* active_list entries will end up before queued entries */
899 list_splice_init(&dwc->queue, &list);
900 list_splice_init(&dwc->active_list, &list);
901
902 spin_unlock_irqrestore(&dwc->lock, flags);
903
904 /* Flush all pending and queued descriptors */
905 list_for_each_entry_safe(desc, _desc, &list, desc_node)
906 dwc_descriptor_complete(dwc, desc, false);
907 } else
Linus Walleijc3635c72010-03-26 16:44:01 -0700908 return -ENXIO;
909
Linus Walleijc3635c72010-03-26 16:44:01 -0700910 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700911}
912
913static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -0700914dwc_tx_status(struct dma_chan *chan,
915 dma_cookie_t cookie,
916 struct dma_tx_state *txstate)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700917{
918 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
919 dma_cookie_t last_used;
920 dma_cookie_t last_complete;
921 int ret;
922
923 last_complete = dwc->completed;
924 last_used = chan->cookie;
925
926 ret = dma_async_is_complete(cookie, last_complete, last_used);
927 if (ret != DMA_SUCCESS) {
928 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
929
930 last_complete = dwc->completed;
931 last_used = chan->cookie;
932
933 ret = dma_async_is_complete(cookie, last_complete, last_used);
934 }
935
Viresh Kumarabf53902011-04-15 16:03:35 +0530936 if (ret != DMA_SUCCESS)
937 dma_set_tx_state(txstate, last_complete, last_used,
938 dwc_first_active(dwc)->len);
939 else
940 dma_set_tx_state(txstate, last_complete, last_used, 0);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700941
Linus Walleija7c57cf2011-04-19 08:31:32 +0800942 if (dwc->paused)
943 return DMA_PAUSED;
944
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700945 return ret;
946}
947
948static void dwc_issue_pending(struct dma_chan *chan)
949{
950 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
951
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700952 if (!list_empty(&dwc->queue))
953 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700954}
955
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700956static int dwc_alloc_chan_resources(struct dma_chan *chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700957{
958 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
959 struct dw_dma *dw = to_dw_dma(chan->device);
960 struct dw_desc *desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700961 struct dw_dma_slave *dws;
962 int i;
963 u32 cfghi;
964 u32 cfglo;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530965 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700966
Dan Williams41d5e592009-01-06 11:38:21 -0700967 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700968
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700969 /* ASSERT: channel is idle */
970 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700971 dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700972 return -EIO;
973 }
974
975 dwc->completed = chan->cookie = 1;
976
977 cfghi = DWC_CFGH_FIFO_MODE;
978 cfglo = 0;
979
Dan Williams287d8592009-02-18 14:48:26 -0800980 dws = chan->private;
Dan Williams74465b42009-01-06 11:38:16 -0700981 if (dws) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700982 /*
983 * We need controller-specific data to set up slave
984 * transfers.
985 */
Dan Williams74465b42009-01-06 11:38:16 -0700986 BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700987
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700988 cfghi = dws->cfg_hi;
Viresh Kumar93317e82011-03-03 15:47:22 +0530989 cfglo = dws->cfg_lo & ~DWC_CFGL_CH_PRIOR_MASK;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700990 }
Viresh Kumar93317e82011-03-03 15:47:22 +0530991
992 cfglo |= DWC_CFGL_CH_PRIOR(dwc->priority);
993
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700994 channel_writel(dwc, CFG_LO, cfglo);
995 channel_writel(dwc, CFG_HI, cfghi);
996
997 /*
998 * NOTE: some controllers may have additional features that we
999 * need to initialize here, like "scatter-gather" (which
1000 * doesn't mean what you think it means), and status writeback.
1001 */
1002
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301003 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001004 i = dwc->descs_allocated;
1005 while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301006 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001007
1008 desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL);
1009 if (!desc) {
Dan Williams41d5e592009-01-06 11:38:21 -07001010 dev_info(chan2dev(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001011 "only allocated %d descriptors\n", i);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301012 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001013 break;
1014 }
1015
Dan Williamse0bd0f82009-09-08 17:53:02 -07001016 INIT_LIST_HEAD(&desc->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001017 dma_async_tx_descriptor_init(&desc->txd, chan);
1018 desc->txd.tx_submit = dwc_tx_submit;
1019 desc->txd.flags = DMA_CTRL_ACK;
Dan Williams41d5e592009-01-06 11:38:21 -07001020 desc->txd.phys = dma_map_single(chan2parent(chan), &desc->lli,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001021 sizeof(desc->lli), DMA_TO_DEVICE);
1022 dwc_desc_put(dwc, desc);
1023
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301024 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001025 i = ++dwc->descs_allocated;
1026 }
1027
1028 /* Enable interrupts */
1029 channel_set_bit(dw, MASK.XFER, dwc->mask);
1030 channel_set_bit(dw, MASK.BLOCK, dwc->mask);
1031 channel_set_bit(dw, MASK.ERROR, dwc->mask);
1032
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301033 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001034
Dan Williams41d5e592009-01-06 11:38:21 -07001035 dev_dbg(chan2dev(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001036 "alloc_chan_resources allocated %d descriptors\n", i);
1037
1038 return i;
1039}
1040
1041static void dwc_free_chan_resources(struct dma_chan *chan)
1042{
1043 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1044 struct dw_dma *dw = to_dw_dma(chan->device);
1045 struct dw_desc *desc, *_desc;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301046 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001047 LIST_HEAD(list);
1048
Dan Williams41d5e592009-01-06 11:38:21 -07001049 dev_dbg(chan2dev(chan), "free_chan_resources (descs allocated=%u)\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001050 dwc->descs_allocated);
1051
1052 /* ASSERT: channel is idle */
1053 BUG_ON(!list_empty(&dwc->active_list));
1054 BUG_ON(!list_empty(&dwc->queue));
1055 BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
1056
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301057 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001058 list_splice_init(&dwc->free_list, &list);
1059 dwc->descs_allocated = 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001060
1061 /* Disable interrupts */
1062 channel_clear_bit(dw, MASK.XFER, dwc->mask);
1063 channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
1064 channel_clear_bit(dw, MASK.ERROR, dwc->mask);
1065
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301066 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001067
1068 list_for_each_entry_safe(desc, _desc, &list, desc_node) {
Dan Williams41d5e592009-01-06 11:38:21 -07001069 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
1070 dma_unmap_single(chan2parent(chan), desc->txd.phys,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001071 sizeof(desc->lli), DMA_TO_DEVICE);
1072 kfree(desc);
1073 }
1074
Dan Williams41d5e592009-01-06 11:38:21 -07001075 dev_vdbg(chan2dev(chan), "free_chan_resources done\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001076}
1077
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001078/* --------------------- Cyclic DMA API extensions -------------------- */
1079
1080/**
1081 * dw_dma_cyclic_start - start the cyclic DMA transfer
1082 * @chan: the DMA channel to start
1083 *
1084 * Must be called with soft interrupts disabled. Returns zero on success or
1085 * -errno on failure.
1086 */
1087int dw_dma_cyclic_start(struct dma_chan *chan)
1088{
1089 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1090 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301091 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001092
1093 if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
1094 dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
1095 return -ENODEV;
1096 }
1097
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301098 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001099
1100 /* assert channel is idle */
1101 if (dma_readl(dw, CH_EN) & dwc->mask) {
1102 dev_err(chan2dev(&dwc->chan),
1103 "BUG: Attempted to start non-idle channel\n");
1104 dev_err(chan2dev(&dwc->chan),
1105 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
1106 channel_readl(dwc, SAR),
1107 channel_readl(dwc, DAR),
1108 channel_readl(dwc, LLP),
1109 channel_readl(dwc, CTL_HI),
1110 channel_readl(dwc, CTL_LO));
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301111 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001112 return -EBUSY;
1113 }
1114
1115 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
1116 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1117 dma_writel(dw, CLEAR.XFER, dwc->mask);
1118
1119 /* setup DMAC channel registers */
1120 channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
1121 channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
1122 channel_writel(dwc, CTL_HI, 0);
1123
1124 channel_set_bit(dw, CH_EN, dwc->mask);
1125
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301126 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001127
1128 return 0;
1129}
1130EXPORT_SYMBOL(dw_dma_cyclic_start);
1131
1132/**
1133 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1134 * @chan: the DMA channel to stop
1135 *
1136 * Must be called with soft interrupts disabled.
1137 */
1138void dw_dma_cyclic_stop(struct dma_chan *chan)
1139{
1140 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1141 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301142 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001143
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301144 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001145
1146 channel_clear_bit(dw, CH_EN, dwc->mask);
1147 while (dma_readl(dw, CH_EN) & dwc->mask)
1148 cpu_relax();
1149
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301150 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001151}
1152EXPORT_SYMBOL(dw_dma_cyclic_stop);
1153
1154/**
1155 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1156 * @chan: the DMA channel to prepare
1157 * @buf_addr: physical DMA address where the buffer starts
1158 * @buf_len: total number of bytes for the entire buffer
1159 * @period_len: number of bytes for each period
1160 * @direction: transfer direction, to or from device
1161 *
1162 * Must be called before trying to start the transfer. Returns a valid struct
1163 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1164 */
1165struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
1166 dma_addr_t buf_addr, size_t buf_len, size_t period_len,
1167 enum dma_data_direction direction)
1168{
1169 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1170 struct dw_cyclic_desc *cdesc;
1171 struct dw_cyclic_desc *retval = NULL;
1172 struct dw_desc *desc;
1173 struct dw_desc *last = NULL;
1174 struct dw_dma_slave *dws = chan->private;
1175 unsigned long was_cyclic;
1176 unsigned int reg_width;
1177 unsigned int periods;
1178 unsigned int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301179 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001180
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301181 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001182 if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301183 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001184 dev_dbg(chan2dev(&dwc->chan),
1185 "queue and/or active list are not empty\n");
1186 return ERR_PTR(-EBUSY);
1187 }
1188
1189 was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301190 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001191 if (was_cyclic) {
1192 dev_dbg(chan2dev(&dwc->chan),
1193 "channel already prepared for cyclic DMA\n");
1194 return ERR_PTR(-EBUSY);
1195 }
1196
1197 retval = ERR_PTR(-EINVAL);
1198 reg_width = dws->reg_width;
1199 periods = buf_len / period_len;
1200
1201 /* Check for too big/unaligned periods and unaligned DMA buffer. */
1202 if (period_len > (DWC_MAX_COUNT << reg_width))
1203 goto out_err;
1204 if (unlikely(period_len & ((1 << reg_width) - 1)))
1205 goto out_err;
1206 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
1207 goto out_err;
1208 if (unlikely(!(direction & (DMA_TO_DEVICE | DMA_FROM_DEVICE))))
1209 goto out_err;
1210
1211 retval = ERR_PTR(-ENOMEM);
1212
1213 if (periods > NR_DESCS_PER_CHANNEL)
1214 goto out_err;
1215
1216 cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
1217 if (!cdesc)
1218 goto out_err;
1219
1220 cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
1221 if (!cdesc->desc)
1222 goto out_err_alloc;
1223
1224 for (i = 0; i < periods; i++) {
1225 desc = dwc_desc_get(dwc);
1226 if (!desc)
1227 goto out_err_desc_get;
1228
1229 switch (direction) {
1230 case DMA_TO_DEVICE:
1231 desc->lli.dar = dws->tx_reg;
1232 desc->lli.sar = buf_addr + (period_len * i);
Jamie Ilesf301c062011-01-21 14:11:53 +00001233 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001234 | DWC_CTLL_DST_WIDTH(reg_width)
1235 | DWC_CTLL_SRC_WIDTH(reg_width)
1236 | DWC_CTLL_DST_FIX
1237 | DWC_CTLL_SRC_INC
Viresh KUMARee665092011-03-04 15:42:51 +05301238 | DWC_CTLL_FC(dws->fc)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001239 | DWC_CTLL_INT_EN);
1240 break;
1241 case DMA_FROM_DEVICE:
1242 desc->lli.dar = buf_addr + (period_len * i);
1243 desc->lli.sar = dws->rx_reg;
Jamie Ilesf301c062011-01-21 14:11:53 +00001244 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001245 | DWC_CTLL_SRC_WIDTH(reg_width)
1246 | DWC_CTLL_DST_WIDTH(reg_width)
1247 | DWC_CTLL_DST_INC
1248 | DWC_CTLL_SRC_FIX
Viresh KUMARee665092011-03-04 15:42:51 +05301249 | DWC_CTLL_FC(dws->fc)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001250 | DWC_CTLL_INT_EN);
1251 break;
1252 default:
1253 break;
1254 }
1255
1256 desc->lli.ctlhi = (period_len >> reg_width);
1257 cdesc->desc[i] = desc;
1258
1259 if (last) {
1260 last->lli.llp = desc->txd.phys;
1261 dma_sync_single_for_device(chan2parent(chan),
1262 last->txd.phys, sizeof(last->lli),
1263 DMA_TO_DEVICE);
1264 }
1265
1266 last = desc;
1267 }
1268
1269 /* lets make a cyclic list */
1270 last->lli.llp = cdesc->desc[0]->txd.phys;
1271 dma_sync_single_for_device(chan2parent(chan), last->txd.phys,
1272 sizeof(last->lli), DMA_TO_DEVICE);
1273
1274 dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%08x len %zu "
1275 "period %zu periods %d\n", buf_addr, buf_len,
1276 period_len, periods);
1277
1278 cdesc->periods = periods;
1279 dwc->cdesc = cdesc;
1280
1281 return cdesc;
1282
1283out_err_desc_get:
1284 while (i--)
1285 dwc_desc_put(dwc, cdesc->desc[i]);
1286out_err_alloc:
1287 kfree(cdesc);
1288out_err:
1289 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1290 return (struct dw_cyclic_desc *)retval;
1291}
1292EXPORT_SYMBOL(dw_dma_cyclic_prep);
1293
1294/**
1295 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1296 * @chan: the DMA channel to free
1297 */
1298void dw_dma_cyclic_free(struct dma_chan *chan)
1299{
1300 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1301 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1302 struct dw_cyclic_desc *cdesc = dwc->cdesc;
1303 int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301304 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001305
1306 dev_dbg(chan2dev(&dwc->chan), "cyclic free\n");
1307
1308 if (!cdesc)
1309 return;
1310
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301311 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001312
1313 channel_clear_bit(dw, CH_EN, dwc->mask);
1314 while (dma_readl(dw, CH_EN) & dwc->mask)
1315 cpu_relax();
1316
1317 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
1318 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1319 dma_writel(dw, CLEAR.XFER, dwc->mask);
1320
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301321 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001322
1323 for (i = 0; i < cdesc->periods; i++)
1324 dwc_desc_put(dwc, cdesc->desc[i]);
1325
1326 kfree(cdesc->desc);
1327 kfree(cdesc);
1328
1329 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1330}
1331EXPORT_SYMBOL(dw_dma_cyclic_free);
1332
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001333/*----------------------------------------------------------------------*/
1334
1335static void dw_dma_off(struct dw_dma *dw)
1336{
1337 dma_writel(dw, CFG, 0);
1338
1339 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
1340 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
1341 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1342 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1343 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1344
1345 while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
1346 cpu_relax();
1347}
1348
1349static int __init dw_probe(struct platform_device *pdev)
1350{
1351 struct dw_dma_platform_data *pdata;
1352 struct resource *io;
1353 struct dw_dma *dw;
1354 size_t size;
1355 int irq;
1356 int err;
1357 int i;
1358
1359 pdata = pdev->dev.platform_data;
1360 if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
1361 return -EINVAL;
1362
1363 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1364 if (!io)
1365 return -EINVAL;
1366
1367 irq = platform_get_irq(pdev, 0);
1368 if (irq < 0)
1369 return irq;
1370
1371 size = sizeof(struct dw_dma);
1372 size += pdata->nr_channels * sizeof(struct dw_dma_chan);
1373 dw = kzalloc(size, GFP_KERNEL);
1374 if (!dw)
1375 return -ENOMEM;
1376
1377 if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) {
1378 err = -EBUSY;
1379 goto err_kfree;
1380 }
1381
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001382 dw->regs = ioremap(io->start, DW_REGLEN);
1383 if (!dw->regs) {
1384 err = -ENOMEM;
1385 goto err_release_r;
1386 }
1387
1388 dw->clk = clk_get(&pdev->dev, "hclk");
1389 if (IS_ERR(dw->clk)) {
1390 err = PTR_ERR(dw->clk);
1391 goto err_clk;
1392 }
1393 clk_enable(dw->clk);
1394
1395 /* force dma off, just in case */
1396 dw_dma_off(dw);
1397
1398 err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw);
1399 if (err)
1400 goto err_irq;
1401
1402 platform_set_drvdata(pdev, dw);
1403
1404 tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
1405
1406 dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
1407
1408 INIT_LIST_HEAD(&dw->dma.channels);
1409 for (i = 0; i < pdata->nr_channels; i++, dw->dma.chancnt++) {
1410 struct dw_dma_chan *dwc = &dw->chan[i];
1411
1412 dwc->chan.device = &dw->dma;
1413 dwc->chan.cookie = dwc->completed = 1;
1414 dwc->chan.chan_id = i;
Viresh Kumarb0c31302011-03-03 15:47:21 +05301415 if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
1416 list_add_tail(&dwc->chan.device_node,
1417 &dw->dma.channels);
1418 else
1419 list_add(&dwc->chan.device_node, &dw->dma.channels);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001420
Viresh Kumar93317e82011-03-03 15:47:22 +05301421 /* 7 is highest priority & 0 is lowest. */
1422 if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
1423 dwc->priority = 7 - i;
1424 else
1425 dwc->priority = i;
1426
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001427 dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
1428 spin_lock_init(&dwc->lock);
1429 dwc->mask = 1 << i;
1430
1431 INIT_LIST_HEAD(&dwc->active_list);
1432 INIT_LIST_HEAD(&dwc->queue);
1433 INIT_LIST_HEAD(&dwc->free_list);
1434
1435 channel_clear_bit(dw, CH_EN, dwc->mask);
1436 }
1437
1438 /* Clear/disable all interrupts on all channels. */
1439 dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
1440 dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
1441 dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
1442 dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
1443 dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
1444
1445 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
1446 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
1447 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1448 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1449 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1450
1451 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
1452 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
Jamie Iles95ea7592011-01-21 14:11:54 +00001453 if (pdata->is_private)
1454 dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001455 dw->dma.dev = &pdev->dev;
1456 dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
1457 dw->dma.device_free_chan_resources = dwc_free_chan_resources;
1458
1459 dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
1460
1461 dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
Linus Walleijc3635c72010-03-26 16:44:01 -07001462 dw->dma.device_control = dwc_control;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001463
Linus Walleij07934482010-03-26 16:50:49 -07001464 dw->dma.device_tx_status = dwc_tx_status;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001465 dw->dma.device_issue_pending = dwc_issue_pending;
1466
1467 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1468
1469 printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n",
Kay Sieversdfbc9012009-03-24 16:38:22 -07001470 dev_name(&pdev->dev), dw->dma.chancnt);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001471
1472 dma_async_device_register(&dw->dma);
1473
1474 return 0;
1475
1476err_irq:
1477 clk_disable(dw->clk);
1478 clk_put(dw->clk);
1479err_clk:
1480 iounmap(dw->regs);
1481 dw->regs = NULL;
1482err_release_r:
1483 release_resource(io);
1484err_kfree:
1485 kfree(dw);
1486 return err;
1487}
1488
1489static int __exit dw_remove(struct platform_device *pdev)
1490{
1491 struct dw_dma *dw = platform_get_drvdata(pdev);
1492 struct dw_dma_chan *dwc, *_dwc;
1493 struct resource *io;
1494
1495 dw_dma_off(dw);
1496 dma_async_device_unregister(&dw->dma);
1497
1498 free_irq(platform_get_irq(pdev, 0), dw);
1499 tasklet_kill(&dw->tasklet);
1500
1501 list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
1502 chan.device_node) {
1503 list_del(&dwc->chan.device_node);
1504 channel_clear_bit(dw, CH_EN, dwc->mask);
1505 }
1506
1507 clk_disable(dw->clk);
1508 clk_put(dw->clk);
1509
1510 iounmap(dw->regs);
1511 dw->regs = NULL;
1512
1513 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1514 release_mem_region(io->start, DW_REGLEN);
1515
1516 kfree(dw);
1517
1518 return 0;
1519}
1520
1521static void dw_shutdown(struct platform_device *pdev)
1522{
1523 struct dw_dma *dw = platform_get_drvdata(pdev);
1524
1525 dw_dma_off(platform_get_drvdata(pdev));
1526 clk_disable(dw->clk);
1527}
1528
Magnus Damm4a256b52009-07-08 13:22:18 +02001529static int dw_suspend_noirq(struct device *dev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001530{
Magnus Damm4a256b52009-07-08 13:22:18 +02001531 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001532 struct dw_dma *dw = platform_get_drvdata(pdev);
1533
1534 dw_dma_off(platform_get_drvdata(pdev));
1535 clk_disable(dw->clk);
1536 return 0;
1537}
1538
Magnus Damm4a256b52009-07-08 13:22:18 +02001539static int dw_resume_noirq(struct device *dev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001540{
Magnus Damm4a256b52009-07-08 13:22:18 +02001541 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001542 struct dw_dma *dw = platform_get_drvdata(pdev);
1543
1544 clk_enable(dw->clk);
1545 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1546 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001547}
1548
Alexey Dobriyan47145212009-12-14 18:00:08 -08001549static const struct dev_pm_ops dw_dev_pm_ops = {
Magnus Damm4a256b52009-07-08 13:22:18 +02001550 .suspend_noirq = dw_suspend_noirq,
1551 .resume_noirq = dw_resume_noirq,
1552};
1553
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001554static struct platform_driver dw_driver = {
1555 .remove = __exit_p(dw_remove),
1556 .shutdown = dw_shutdown,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001557 .driver = {
1558 .name = "dw_dmac",
Magnus Damm4a256b52009-07-08 13:22:18 +02001559 .pm = &dw_dev_pm_ops,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001560 },
1561};
1562
1563static int __init dw_init(void)
1564{
1565 return platform_driver_probe(&dw_driver, dw_probe);
1566}
Viresh Kumarcb689a72011-03-03 15:47:15 +05301567subsys_initcall(dw_init);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001568
1569static void __exit dw_exit(void)
1570{
1571 platform_driver_unregister(&dw_driver);
1572}
1573module_exit(dw_exit);
1574
1575MODULE_LICENSE("GPL v2");
1576MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
1577MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");