blob: e5d97bf9264afa017356ccd066fd8f6220719eb4 [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; \
39 \
40 (DWC_CTLL_DST_MSIZE(0) \
41 | DWC_CTLL_SRC_MSIZE(0) \
42 | DWC_CTLL_LLP_D_EN \
43 | DWC_CTLL_LLP_S_EN \
44 | DWC_CTLL_DMS(dms) \
45 | DWC_CTLL_SMS(sms)); \
46 })
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070047
48/*
49 * This is configuration-dependent and usually a funny size like 4095.
50 * Let's round it down to the nearest power of two.
51 *
52 * Note that this is a transfer count, i.e. if we transfer 32-bit
53 * words, we can do 8192 bytes per descriptor.
54 *
55 * This parameter is also system-specific.
56 */
57#define DWC_MAX_COUNT 2048U
58
59/*
60 * Number of descriptors to allocate for each channel. This should be
61 * made configurable somehow; preferably, the clients (at least the
62 * ones using slave transfers) should be able to give us a hint.
63 */
64#define NR_DESCS_PER_CHANNEL 64
65
66/*----------------------------------------------------------------------*/
67
68/*
69 * Because we're not relying on writeback from the controller (it may not
70 * even be configured into the core!) we don't need to use dma_pool. These
71 * descriptors -- and associated data -- are cacheable. We do need to make
72 * sure their dcache entries are written back before handing them off to
73 * the controller, though.
74 */
75
Dan Williams41d5e592009-01-06 11:38:21 -070076static struct device *chan2dev(struct dma_chan *chan)
77{
78 return &chan->dev->device;
79}
80static struct device *chan2parent(struct dma_chan *chan)
81{
82 return chan->dev->device.parent;
83}
84
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070085static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
86{
87 return list_entry(dwc->active_list.next, struct dw_desc, desc_node);
88}
89
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070090static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
91{
92 struct dw_desc *desc, *_desc;
93 struct dw_desc *ret = NULL;
94 unsigned int i = 0;
95
96 spin_lock_bh(&dwc->lock);
97 list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
98 if (async_tx_test_ack(&desc->txd)) {
99 list_del(&desc->desc_node);
100 ret = desc;
101 break;
102 }
Dan Williams41d5e592009-01-06 11:38:21 -0700103 dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700104 i++;
105 }
106 spin_unlock_bh(&dwc->lock);
107
Dan Williams41d5e592009-01-06 11:38:21 -0700108 dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700109
110 return ret;
111}
112
113static void dwc_sync_desc_for_cpu(struct dw_dma_chan *dwc, struct dw_desc *desc)
114{
115 struct dw_desc *child;
116
Dan Williamse0bd0f82009-09-08 17:53:02 -0700117 list_for_each_entry(child, &desc->tx_list, desc_node)
Dan Williams41d5e592009-01-06 11:38:21 -0700118 dma_sync_single_for_cpu(chan2parent(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700119 child->txd.phys, sizeof(child->lli),
120 DMA_TO_DEVICE);
Dan Williams41d5e592009-01-06 11:38:21 -0700121 dma_sync_single_for_cpu(chan2parent(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700122 desc->txd.phys, sizeof(desc->lli),
123 DMA_TO_DEVICE);
124}
125
126/*
127 * Move a descriptor, including any children, to the free list.
128 * `desc' must not be on any lists.
129 */
130static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
131{
132 if (desc) {
133 struct dw_desc *child;
134
135 dwc_sync_desc_for_cpu(dwc, desc);
136
137 spin_lock_bh(&dwc->lock);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700138 list_for_each_entry(child, &desc->tx_list, desc_node)
Dan Williams41d5e592009-01-06 11:38:21 -0700139 dev_vdbg(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700140 "moving child desc %p to freelist\n",
141 child);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700142 list_splice_init(&desc->tx_list, &dwc->free_list);
Dan Williams41d5e592009-01-06 11:38:21 -0700143 dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700144 list_add(&desc->desc_node, &dwc->free_list);
145 spin_unlock_bh(&dwc->lock);
146 }
147}
148
149/* Called with dwc->lock held and bh disabled */
150static dma_cookie_t
151dwc_assign_cookie(struct dw_dma_chan *dwc, struct dw_desc *desc)
152{
153 dma_cookie_t cookie = dwc->chan.cookie;
154
155 if (++cookie < 0)
156 cookie = 1;
157
158 dwc->chan.cookie = cookie;
159 desc->txd.cookie = cookie;
160
161 return cookie;
162}
163
164/*----------------------------------------------------------------------*/
165
166/* Called with dwc->lock held and bh disabled */
167static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
168{
169 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
170
171 /* ASSERT: channel is idle */
172 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700173 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700174 "BUG: Attempted to start non-idle channel\n");
Dan Williams41d5e592009-01-06 11:38:21 -0700175 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700176 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
177 channel_readl(dwc, SAR),
178 channel_readl(dwc, DAR),
179 channel_readl(dwc, LLP),
180 channel_readl(dwc, CTL_HI),
181 channel_readl(dwc, CTL_LO));
182
183 /* The tasklet will hopefully advance the queue... */
184 return;
185 }
186
187 channel_writel(dwc, LLP, first->txd.phys);
188 channel_writel(dwc, CTL_LO,
189 DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
190 channel_writel(dwc, CTL_HI, 0);
191 channel_set_bit(dw, CH_EN, dwc->mask);
192}
193
194/*----------------------------------------------------------------------*/
195
196static void
197dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc)
198{
199 dma_async_tx_callback callback;
200 void *param;
201 struct dma_async_tx_descriptor *txd = &desc->txd;
202
Dan Williams41d5e592009-01-06 11:38:21 -0700203 dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700204
205 dwc->completed = txd->cookie;
206 callback = txd->callback;
207 param = txd->callback_param;
208
209 dwc_sync_desc_for_cpu(dwc, desc);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700210 list_splice_init(&desc->tx_list, &dwc->free_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700211 list_move(&desc->desc_node, &dwc->free_list);
212
Atsushi Nemoto657a77f2009-09-08 17:53:05 -0700213 if (!dwc->chan.private) {
214 struct device *parent = chan2parent(&dwc->chan);
215 if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
216 if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE)
217 dma_unmap_single(parent, desc->lli.dar,
218 desc->len, DMA_FROM_DEVICE);
219 else
220 dma_unmap_page(parent, desc->lli.dar,
221 desc->len, DMA_FROM_DEVICE);
222 }
223 if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
224 if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE)
225 dma_unmap_single(parent, desc->lli.sar,
226 desc->len, DMA_TO_DEVICE);
227 else
228 dma_unmap_page(parent, desc->lli.sar,
229 desc->len, DMA_TO_DEVICE);
230 }
231 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700232
233 /*
234 * The API requires that no submissions are done from a
235 * callback, so we don't need to drop the lock here
236 */
237 if (callback)
238 callback(param);
239}
240
241static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
242{
243 struct dw_desc *desc, *_desc;
244 LIST_HEAD(list);
245
246 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700247 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700248 "BUG: XFER bit set, but channel not idle!\n");
249
250 /* Try to continue after resetting the channel... */
251 channel_clear_bit(dw, CH_EN, dwc->mask);
252 while (dma_readl(dw, CH_EN) & dwc->mask)
253 cpu_relax();
254 }
255
256 /*
257 * Submit queued descriptors ASAP, i.e. before we go through
258 * the completed ones.
259 */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700260 list_splice_init(&dwc->active_list, &list);
Viresh Kumarf336e422011-03-03 15:47:16 +0530261 if (!list_empty(&dwc->queue)) {
262 list_move(dwc->queue.next, &dwc->active_list);
263 dwc_dostart(dwc, dwc_first_active(dwc));
264 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700265
266 list_for_each_entry_safe(desc, _desc, &list, desc_node)
267 dwc_descriptor_complete(dwc, desc);
268}
269
270static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
271{
272 dma_addr_t llp;
273 struct dw_desc *desc, *_desc;
274 struct dw_desc *child;
275 u32 status_xfer;
276
277 /*
278 * Clear block interrupt flag before scanning so that we don't
279 * miss any, and read LLP before RAW_XFER to ensure it is
280 * valid if we decide to scan the list.
281 */
282 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
283 llp = channel_readl(dwc, LLP);
284 status_xfer = dma_readl(dw, RAW.XFER);
285
286 if (status_xfer & dwc->mask) {
287 /* Everything we've submitted is done */
288 dma_writel(dw, CLEAR.XFER, dwc->mask);
289 dwc_complete_all(dw, dwc);
290 return;
291 }
292
Jamie Iles087809f2011-01-21 14:11:52 +0000293 if (list_empty(&dwc->active_list))
294 return;
295
Dan Williams41d5e592009-01-06 11:38:21 -0700296 dev_vdbg(chan2dev(&dwc->chan), "scan_descriptors: llp=0x%x\n", llp);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700297
298 list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
299 if (desc->lli.llp == llp)
300 /* This one is currently in progress */
301 return;
302
Dan Williamse0bd0f82009-09-08 17:53:02 -0700303 list_for_each_entry(child, &desc->tx_list, desc_node)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700304 if (child->lli.llp == llp)
305 /* Currently in progress */
306 return;
307
308 /*
309 * No descriptors so far seem to be in progress, i.e.
310 * this one must be done.
311 */
312 dwc_descriptor_complete(dwc, desc);
313 }
314
Dan Williams41d5e592009-01-06 11:38:21 -0700315 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700316 "BUG: All descriptors done, but channel not idle!\n");
317
318 /* Try to continue after resetting the channel... */
319 channel_clear_bit(dw, CH_EN, dwc->mask);
320 while (dma_readl(dw, CH_EN) & dwc->mask)
321 cpu_relax();
322
323 if (!list_empty(&dwc->queue)) {
Viresh Kumarf336e422011-03-03 15:47:16 +0530324 list_move(dwc->queue.next, &dwc->active_list);
325 dwc_dostart(dwc, dwc_first_active(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700326 }
327}
328
329static void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
330{
Dan Williams41d5e592009-01-06 11:38:21 -0700331 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700332 " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
333 lli->sar, lli->dar, lli->llp,
334 lli->ctlhi, lli->ctllo);
335}
336
337static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
338{
339 struct dw_desc *bad_desc;
340 struct dw_desc *child;
341
342 dwc_scan_descriptors(dw, dwc);
343
344 /*
345 * The descriptor currently at the head of the active list is
346 * borked. Since we don't have any way to report errors, we'll
347 * just have to scream loudly and try to carry on.
348 */
349 bad_desc = dwc_first_active(dwc);
350 list_del_init(&bad_desc->desc_node);
Viresh Kumarf336e422011-03-03 15:47:16 +0530351 list_move(dwc->queue.next, dwc->active_list.prev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700352
353 /* Clear the error flag and try to restart the controller */
354 dma_writel(dw, CLEAR.ERROR, dwc->mask);
355 if (!list_empty(&dwc->active_list))
356 dwc_dostart(dwc, dwc_first_active(dwc));
357
358 /*
359 * KERN_CRITICAL may seem harsh, but since this only happens
360 * when someone submits a bad physical address in a
361 * descriptor, we should consider ourselves lucky that the
362 * controller flagged an error instead of scribbling over
363 * random memory locations.
364 */
Dan Williams41d5e592009-01-06 11:38:21 -0700365 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700366 "Bad descriptor submitted for DMA!\n");
Dan Williams41d5e592009-01-06 11:38:21 -0700367 dev_printk(KERN_CRIT, chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700368 " cookie: %d\n", bad_desc->txd.cookie);
369 dwc_dump_lli(dwc, &bad_desc->lli);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700370 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700371 dwc_dump_lli(dwc, &child->lli);
372
373 /* Pretend the descriptor completed successfully */
374 dwc_descriptor_complete(dwc, bad_desc);
375}
376
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200377/* --------------------- Cyclic DMA API extensions -------------------- */
378
379inline dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
380{
381 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
382 return channel_readl(dwc, SAR);
383}
384EXPORT_SYMBOL(dw_dma_get_src_addr);
385
386inline dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
387{
388 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
389 return channel_readl(dwc, DAR);
390}
391EXPORT_SYMBOL(dw_dma_get_dst_addr);
392
393/* called with dwc->lock held and all DMAC interrupts disabled */
394static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
395 u32 status_block, u32 status_err, u32 status_xfer)
396{
397 if (status_block & dwc->mask) {
398 void (*callback)(void *param);
399 void *callback_param;
400
401 dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
402 channel_readl(dwc, LLP));
403 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
404
405 callback = dwc->cdesc->period_callback;
406 callback_param = dwc->cdesc->period_callback_param;
407 if (callback) {
408 spin_unlock(&dwc->lock);
409 callback(callback_param);
410 spin_lock(&dwc->lock);
411 }
412 }
413
414 /*
415 * Error and transfer complete are highly unlikely, and will most
416 * likely be due to a configuration error by the user.
417 */
418 if (unlikely(status_err & dwc->mask) ||
419 unlikely(status_xfer & dwc->mask)) {
420 int i;
421
422 dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s "
423 "interrupt, stopping DMA transfer\n",
424 status_xfer ? "xfer" : "error");
425 dev_err(chan2dev(&dwc->chan),
426 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
427 channel_readl(dwc, SAR),
428 channel_readl(dwc, DAR),
429 channel_readl(dwc, LLP),
430 channel_readl(dwc, CTL_HI),
431 channel_readl(dwc, CTL_LO));
432
433 channel_clear_bit(dw, CH_EN, dwc->mask);
434 while (dma_readl(dw, CH_EN) & dwc->mask)
435 cpu_relax();
436
437 /* make sure DMA does not restart by loading a new list */
438 channel_writel(dwc, LLP, 0);
439 channel_writel(dwc, CTL_LO, 0);
440 channel_writel(dwc, CTL_HI, 0);
441
442 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
443 dma_writel(dw, CLEAR.ERROR, dwc->mask);
444 dma_writel(dw, CLEAR.XFER, dwc->mask);
445
446 for (i = 0; i < dwc->cdesc->periods; i++)
447 dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
448 }
449}
450
451/* ------------------------------------------------------------------------- */
452
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700453static void dw_dma_tasklet(unsigned long data)
454{
455 struct dw_dma *dw = (struct dw_dma *)data;
456 struct dw_dma_chan *dwc;
457 u32 status_block;
458 u32 status_xfer;
459 u32 status_err;
460 int i;
461
462 status_block = dma_readl(dw, RAW.BLOCK);
Haavard Skinnemoen7fe7b2f2008-10-03 15:23:46 -0700463 status_xfer = dma_readl(dw, RAW.XFER);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700464 status_err = dma_readl(dw, RAW.ERROR);
465
466 dev_vdbg(dw->dma.dev, "tasklet: status_block=%x status_err=%x\n",
467 status_block, status_err);
468
469 for (i = 0; i < dw->dma.chancnt; i++) {
470 dwc = &dw->chan[i];
471 spin_lock(&dwc->lock);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200472 if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
473 dwc_handle_cyclic(dw, dwc, status_block, status_err,
474 status_xfer);
475 else if (status_err & (1 << i))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700476 dwc_handle_error(dw, dwc);
477 else if ((status_block | status_xfer) & (1 << i))
478 dwc_scan_descriptors(dw, dwc);
479 spin_unlock(&dwc->lock);
480 }
481
482 /*
483 * Re-enable interrupts. Block Complete interrupts are only
484 * enabled if the INT_EN bit in the descriptor is set. This
485 * will trigger a scan before the whole list is done.
486 */
487 channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
488 channel_set_bit(dw, MASK.BLOCK, dw->all_chan_mask);
489 channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
490}
491
492static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
493{
494 struct dw_dma *dw = dev_id;
495 u32 status;
496
497 dev_vdbg(dw->dma.dev, "interrupt: status=0x%x\n",
498 dma_readl(dw, STATUS_INT));
499
500 /*
501 * Just disable the interrupts. We'll turn them back on in the
502 * softirq handler.
503 */
504 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
505 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
506 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
507
508 status = dma_readl(dw, STATUS_INT);
509 if (status) {
510 dev_err(dw->dma.dev,
511 "BUG: Unexpected interrupts pending: 0x%x\n",
512 status);
513
514 /* Try to recover */
515 channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
516 channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1);
517 channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
518 channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
519 channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
520 }
521
522 tasklet_schedule(&dw->tasklet);
523
524 return IRQ_HANDLED;
525}
526
527/*----------------------------------------------------------------------*/
528
529static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
530{
531 struct dw_desc *desc = txd_to_dw_desc(tx);
532 struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
533 dma_cookie_t cookie;
534
535 spin_lock_bh(&dwc->lock);
536 cookie = dwc_assign_cookie(dwc, desc);
537
538 /*
539 * REVISIT: We should attempt to chain as many descriptors as
540 * possible, perhaps even appending to those already submitted
541 * for DMA. But this is hard to do in a race-free manner.
542 */
543 if (list_empty(&dwc->active_list)) {
Dan Williams41d5e592009-01-06 11:38:21 -0700544 dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700545 desc->txd.cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700546 list_add_tail(&desc->desc_node, &dwc->active_list);
Viresh Kumarf336e422011-03-03 15:47:16 +0530547 dwc_dostart(dwc, dwc_first_active(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700548 } else {
Dan Williams41d5e592009-01-06 11:38:21 -0700549 dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700550 desc->txd.cookie);
551
552 list_add_tail(&desc->desc_node, &dwc->queue);
553 }
554
555 spin_unlock_bh(&dwc->lock);
556
557 return cookie;
558}
559
560static struct dma_async_tx_descriptor *
561dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
562 size_t len, unsigned long flags)
563{
564 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
565 struct dw_desc *desc;
566 struct dw_desc *first;
567 struct dw_desc *prev;
568 size_t xfer_count;
569 size_t offset;
570 unsigned int src_width;
571 unsigned int dst_width;
572 u32 ctllo;
573
Dan Williams41d5e592009-01-06 11:38:21 -0700574 dev_vdbg(chan2dev(chan), "prep_dma_memcpy d0x%x s0x%x l0x%zx f0x%lx\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700575 dest, src, len, flags);
576
577 if (unlikely(!len)) {
Dan Williams41d5e592009-01-06 11:38:21 -0700578 dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700579 return NULL;
580 }
581
582 /*
583 * We can be a lot more clever here, but this should take care
584 * of the most common optimization.
585 */
Viresh Kumara0227452011-03-03 15:47:18 +0530586 if (!((src | dest | len) & 7))
587 src_width = dst_width = 3;
588 else if (!((src | dest | len) & 3))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700589 src_width = dst_width = 2;
590 else if (!((src | dest | len) & 1))
591 src_width = dst_width = 1;
592 else
593 src_width = dst_width = 0;
594
Jamie Ilesf301c062011-01-21 14:11:53 +0000595 ctllo = DWC_DEFAULT_CTLLO(chan->private)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700596 | DWC_CTLL_DST_WIDTH(dst_width)
597 | DWC_CTLL_SRC_WIDTH(src_width)
598 | DWC_CTLL_DST_INC
599 | DWC_CTLL_SRC_INC
600 | DWC_CTLL_FC_M2M;
601 prev = first = NULL;
602
603 for (offset = 0; offset < len; offset += xfer_count << src_width) {
604 xfer_count = min_t(size_t, (len - offset) >> src_width,
605 DWC_MAX_COUNT);
606
607 desc = dwc_desc_get(dwc);
608 if (!desc)
609 goto err_desc_get;
610
611 desc->lli.sar = src + offset;
612 desc->lli.dar = dest + offset;
613 desc->lli.ctllo = ctllo;
614 desc->lli.ctlhi = xfer_count;
615
616 if (!first) {
617 first = desc;
618 } else {
619 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700620 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700621 prev->txd.phys, sizeof(prev->lli),
622 DMA_TO_DEVICE);
623 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700624 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700625 }
626 prev = desc;
627 }
628
629
630 if (flags & DMA_PREP_INTERRUPT)
631 /* Trigger interrupt after last block */
632 prev->lli.ctllo |= DWC_CTLL_INT_EN;
633
634 prev->lli.llp = 0;
Dan Williams41d5e592009-01-06 11:38:21 -0700635 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700636 prev->txd.phys, sizeof(prev->lli),
637 DMA_TO_DEVICE);
638
639 first->txd.flags = flags;
640 first->len = len;
641
642 return &first->txd;
643
644err_desc_get:
645 dwc_desc_put(dwc, first);
646 return NULL;
647}
648
649static struct dma_async_tx_descriptor *
650dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
651 unsigned int sg_len, enum dma_data_direction direction,
652 unsigned long flags)
653{
654 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Dan Williams287d8592009-02-18 14:48:26 -0800655 struct dw_dma_slave *dws = chan->private;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700656 struct dw_desc *prev;
657 struct dw_desc *first;
658 u32 ctllo;
659 dma_addr_t reg;
660 unsigned int reg_width;
661 unsigned int mem_width;
662 unsigned int i;
663 struct scatterlist *sg;
664 size_t total_len = 0;
665
Dan Williams41d5e592009-01-06 11:38:21 -0700666 dev_vdbg(chan2dev(chan), "prep_dma_slave\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700667
668 if (unlikely(!dws || !sg_len))
669 return NULL;
670
Dan Williams74465b42009-01-06 11:38:16 -0700671 reg_width = dws->reg_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700672 prev = first = NULL;
673
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700674 switch (direction) {
675 case DMA_TO_DEVICE:
Jamie Ilesf301c062011-01-21 14:11:53 +0000676 ctllo = (DWC_DEFAULT_CTLLO(chan->private)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700677 | DWC_CTLL_DST_WIDTH(reg_width)
678 | DWC_CTLL_DST_FIX
679 | DWC_CTLL_SRC_INC
680 | DWC_CTLL_FC_M2P);
Dan Williams74465b42009-01-06 11:38:16 -0700681 reg = dws->tx_reg;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700682 for_each_sg(sgl, sg, sg_len, i) {
683 struct dw_desc *desc;
684 u32 len;
685 u32 mem;
686
687 desc = dwc_desc_get(dwc);
688 if (!desc) {
Dan Williams41d5e592009-01-06 11:38:21 -0700689 dev_err(chan2dev(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700690 "not enough descriptors available\n");
691 goto err_desc_get;
692 }
693
694 mem = sg_phys(sg);
695 len = sg_dma_len(sg);
696 mem_width = 2;
697 if (unlikely(mem & 3 || len & 3))
698 mem_width = 0;
699
700 desc->lli.sar = mem;
701 desc->lli.dar = reg;
702 desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
703 desc->lli.ctlhi = len >> mem_width;
704
705 if (!first) {
706 first = desc;
707 } else {
708 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700709 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700710 prev->txd.phys,
711 sizeof(prev->lli),
712 DMA_TO_DEVICE);
713 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700714 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700715 }
716 prev = desc;
717 total_len += len;
718 }
719 break;
720 case DMA_FROM_DEVICE:
Jamie Ilesf301c062011-01-21 14:11:53 +0000721 ctllo = (DWC_DEFAULT_CTLLO(chan->private)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700722 | DWC_CTLL_SRC_WIDTH(reg_width)
723 | DWC_CTLL_DST_INC
724 | DWC_CTLL_SRC_FIX
725 | DWC_CTLL_FC_P2M);
726
Dan Williams74465b42009-01-06 11:38:16 -0700727 reg = dws->rx_reg;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700728 for_each_sg(sgl, sg, sg_len, i) {
729 struct dw_desc *desc;
730 u32 len;
731 u32 mem;
732
733 desc = dwc_desc_get(dwc);
734 if (!desc) {
Dan Williams41d5e592009-01-06 11:38:21 -0700735 dev_err(chan2dev(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700736 "not enough descriptors available\n");
737 goto err_desc_get;
738 }
739
740 mem = sg_phys(sg);
741 len = sg_dma_len(sg);
742 mem_width = 2;
743 if (unlikely(mem & 3 || len & 3))
744 mem_width = 0;
745
746 desc->lli.sar = reg;
747 desc->lli.dar = mem;
748 desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
749 desc->lli.ctlhi = len >> reg_width;
750
751 if (!first) {
752 first = desc;
753 } else {
754 prev->lli.llp = desc->txd.phys;
Dan Williams41d5e592009-01-06 11:38:21 -0700755 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700756 prev->txd.phys,
757 sizeof(prev->lli),
758 DMA_TO_DEVICE);
759 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700760 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700761 }
762 prev = desc;
763 total_len += len;
764 }
765 break;
766 default:
767 return NULL;
768 }
769
770 if (flags & DMA_PREP_INTERRUPT)
771 /* Trigger interrupt after last block */
772 prev->lli.ctllo |= DWC_CTLL_INT_EN;
773
774 prev->lli.llp = 0;
Dan Williams41d5e592009-01-06 11:38:21 -0700775 dma_sync_single_for_device(chan2parent(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700776 prev->txd.phys, sizeof(prev->lli),
777 DMA_TO_DEVICE);
778
779 first->len = total_len;
780
781 return &first->txd;
782
783err_desc_get:
784 dwc_desc_put(dwc, first);
785 return NULL;
786}
787
Linus Walleij05827632010-05-17 16:30:42 -0700788static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
789 unsigned long arg)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700790{
791 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
792 struct dw_dma *dw = to_dw_dma(chan->device);
793 struct dw_desc *desc, *_desc;
794 LIST_HEAD(list);
795
Linus Walleijc3635c72010-03-26 16:44:01 -0700796 /* Only supports DMA_TERMINATE_ALL */
797 if (cmd != DMA_TERMINATE_ALL)
798 return -ENXIO;
799
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700800 /*
801 * This is only called when something went wrong elsewhere, so
802 * we don't really care about the data. Just disable the
803 * channel. We still have to poll the channel enable bit due
804 * to AHB/HSB limitations.
805 */
806 spin_lock_bh(&dwc->lock);
807
808 channel_clear_bit(dw, CH_EN, dwc->mask);
809
810 while (dma_readl(dw, CH_EN) & dwc->mask)
811 cpu_relax();
812
813 /* active_list entries will end up before queued entries */
814 list_splice_init(&dwc->queue, &list);
815 list_splice_init(&dwc->active_list, &list);
816
817 spin_unlock_bh(&dwc->lock);
818
819 /* Flush all pending and queued descriptors */
820 list_for_each_entry_safe(desc, _desc, &list, desc_node)
821 dwc_descriptor_complete(dwc, desc);
Linus Walleijc3635c72010-03-26 16:44:01 -0700822
823 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700824}
825
826static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -0700827dwc_tx_status(struct dma_chan *chan,
828 dma_cookie_t cookie,
829 struct dma_tx_state *txstate)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700830{
831 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
832 dma_cookie_t last_used;
833 dma_cookie_t last_complete;
834 int ret;
835
836 last_complete = dwc->completed;
837 last_used = chan->cookie;
838
839 ret = dma_async_is_complete(cookie, last_complete, last_used);
840 if (ret != DMA_SUCCESS) {
Viresh Kumar569432e2011-03-03 15:47:17 +0530841 spin_lock_bh(&dwc->lock);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700842 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
Viresh Kumar569432e2011-03-03 15:47:17 +0530843 spin_unlock_bh(&dwc->lock);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700844
845 last_complete = dwc->completed;
846 last_used = chan->cookie;
847
848 ret = dma_async_is_complete(cookie, last_complete, last_used);
849 }
850
Dan Williamsbca34692010-03-26 16:52:10 -0700851 dma_set_tx_state(txstate, last_complete, last_used, 0);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700852
853 return ret;
854}
855
856static void dwc_issue_pending(struct dma_chan *chan)
857{
858 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
859
860 spin_lock_bh(&dwc->lock);
861 if (!list_empty(&dwc->queue))
862 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
863 spin_unlock_bh(&dwc->lock);
864}
865
Dan Williamsaa1e6f12009-01-06 11:38:17 -0700866static int dwc_alloc_chan_resources(struct dma_chan *chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700867{
868 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
869 struct dw_dma *dw = to_dw_dma(chan->device);
870 struct dw_desc *desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700871 struct dw_dma_slave *dws;
872 int i;
873 u32 cfghi;
874 u32 cfglo;
875
Dan Williams41d5e592009-01-06 11:38:21 -0700876 dev_vdbg(chan2dev(chan), "alloc_chan_resources\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700877
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700878 /* ASSERT: channel is idle */
879 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700880 dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700881 return -EIO;
882 }
883
884 dwc->completed = chan->cookie = 1;
885
886 cfghi = DWC_CFGH_FIFO_MODE;
887 cfglo = 0;
888
Dan Williams287d8592009-02-18 14:48:26 -0800889 dws = chan->private;
Dan Williams74465b42009-01-06 11:38:16 -0700890 if (dws) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700891 /*
892 * We need controller-specific data to set up slave
893 * transfers.
894 */
Dan Williams74465b42009-01-06 11:38:16 -0700895 BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700896
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700897 cfghi = dws->cfg_hi;
898 cfglo = dws->cfg_lo;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700899 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700900 channel_writel(dwc, CFG_LO, cfglo);
901 channel_writel(dwc, CFG_HI, cfghi);
902
903 /*
904 * NOTE: some controllers may have additional features that we
905 * need to initialize here, like "scatter-gather" (which
906 * doesn't mean what you think it means), and status writeback.
907 */
908
909 spin_lock_bh(&dwc->lock);
910 i = dwc->descs_allocated;
911 while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
912 spin_unlock_bh(&dwc->lock);
913
914 desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL);
915 if (!desc) {
Dan Williams41d5e592009-01-06 11:38:21 -0700916 dev_info(chan2dev(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700917 "only allocated %d descriptors\n", i);
918 spin_lock_bh(&dwc->lock);
919 break;
920 }
921
Dan Williamse0bd0f82009-09-08 17:53:02 -0700922 INIT_LIST_HEAD(&desc->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700923 dma_async_tx_descriptor_init(&desc->txd, chan);
924 desc->txd.tx_submit = dwc_tx_submit;
925 desc->txd.flags = DMA_CTRL_ACK;
Dan Williams41d5e592009-01-06 11:38:21 -0700926 desc->txd.phys = dma_map_single(chan2parent(chan), &desc->lli,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700927 sizeof(desc->lli), DMA_TO_DEVICE);
928 dwc_desc_put(dwc, desc);
929
930 spin_lock_bh(&dwc->lock);
931 i = ++dwc->descs_allocated;
932 }
933
934 /* Enable interrupts */
935 channel_set_bit(dw, MASK.XFER, dwc->mask);
936 channel_set_bit(dw, MASK.BLOCK, dwc->mask);
937 channel_set_bit(dw, MASK.ERROR, dwc->mask);
938
939 spin_unlock_bh(&dwc->lock);
940
Dan Williams41d5e592009-01-06 11:38:21 -0700941 dev_dbg(chan2dev(chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700942 "alloc_chan_resources allocated %d descriptors\n", i);
943
944 return i;
945}
946
947static void dwc_free_chan_resources(struct dma_chan *chan)
948{
949 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
950 struct dw_dma *dw = to_dw_dma(chan->device);
951 struct dw_desc *desc, *_desc;
952 LIST_HEAD(list);
953
Dan Williams41d5e592009-01-06 11:38:21 -0700954 dev_dbg(chan2dev(chan), "free_chan_resources (descs allocated=%u)\n",
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700955 dwc->descs_allocated);
956
957 /* ASSERT: channel is idle */
958 BUG_ON(!list_empty(&dwc->active_list));
959 BUG_ON(!list_empty(&dwc->queue));
960 BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
961
962 spin_lock_bh(&dwc->lock);
963 list_splice_init(&dwc->free_list, &list);
964 dwc->descs_allocated = 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700965
966 /* Disable interrupts */
967 channel_clear_bit(dw, MASK.XFER, dwc->mask);
968 channel_clear_bit(dw, MASK.BLOCK, dwc->mask);
969 channel_clear_bit(dw, MASK.ERROR, dwc->mask);
970
971 spin_unlock_bh(&dwc->lock);
972
973 list_for_each_entry_safe(desc, _desc, &list, desc_node) {
Dan Williams41d5e592009-01-06 11:38:21 -0700974 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
975 dma_unmap_single(chan2parent(chan), desc->txd.phys,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700976 sizeof(desc->lli), DMA_TO_DEVICE);
977 kfree(desc);
978 }
979
Dan Williams41d5e592009-01-06 11:38:21 -0700980 dev_vdbg(chan2dev(chan), "free_chan_resources done\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700981}
982
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200983/* --------------------- Cyclic DMA API extensions -------------------- */
984
985/**
986 * dw_dma_cyclic_start - start the cyclic DMA transfer
987 * @chan: the DMA channel to start
988 *
989 * Must be called with soft interrupts disabled. Returns zero on success or
990 * -errno on failure.
991 */
992int dw_dma_cyclic_start(struct dma_chan *chan)
993{
994 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
995 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
996
997 if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
998 dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
999 return -ENODEV;
1000 }
1001
1002 spin_lock(&dwc->lock);
1003
1004 /* assert channel is idle */
1005 if (dma_readl(dw, CH_EN) & dwc->mask) {
1006 dev_err(chan2dev(&dwc->chan),
1007 "BUG: Attempted to start non-idle channel\n");
1008 dev_err(chan2dev(&dwc->chan),
1009 " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n",
1010 channel_readl(dwc, SAR),
1011 channel_readl(dwc, DAR),
1012 channel_readl(dwc, LLP),
1013 channel_readl(dwc, CTL_HI),
1014 channel_readl(dwc, CTL_LO));
1015 spin_unlock(&dwc->lock);
1016 return -EBUSY;
1017 }
1018
1019 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
1020 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1021 dma_writel(dw, CLEAR.XFER, dwc->mask);
1022
1023 /* setup DMAC channel registers */
1024 channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
1025 channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
1026 channel_writel(dwc, CTL_HI, 0);
1027
1028 channel_set_bit(dw, CH_EN, dwc->mask);
1029
1030 spin_unlock(&dwc->lock);
1031
1032 return 0;
1033}
1034EXPORT_SYMBOL(dw_dma_cyclic_start);
1035
1036/**
1037 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1038 * @chan: the DMA channel to stop
1039 *
1040 * Must be called with soft interrupts disabled.
1041 */
1042void dw_dma_cyclic_stop(struct dma_chan *chan)
1043{
1044 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1045 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1046
1047 spin_lock(&dwc->lock);
1048
1049 channel_clear_bit(dw, CH_EN, dwc->mask);
1050 while (dma_readl(dw, CH_EN) & dwc->mask)
1051 cpu_relax();
1052
1053 spin_unlock(&dwc->lock);
1054}
1055EXPORT_SYMBOL(dw_dma_cyclic_stop);
1056
1057/**
1058 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1059 * @chan: the DMA channel to prepare
1060 * @buf_addr: physical DMA address where the buffer starts
1061 * @buf_len: total number of bytes for the entire buffer
1062 * @period_len: number of bytes for each period
1063 * @direction: transfer direction, to or from device
1064 *
1065 * Must be called before trying to start the transfer. Returns a valid struct
1066 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1067 */
1068struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
1069 dma_addr_t buf_addr, size_t buf_len, size_t period_len,
1070 enum dma_data_direction direction)
1071{
1072 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1073 struct dw_cyclic_desc *cdesc;
1074 struct dw_cyclic_desc *retval = NULL;
1075 struct dw_desc *desc;
1076 struct dw_desc *last = NULL;
1077 struct dw_dma_slave *dws = chan->private;
1078 unsigned long was_cyclic;
1079 unsigned int reg_width;
1080 unsigned int periods;
1081 unsigned int i;
1082
1083 spin_lock_bh(&dwc->lock);
1084 if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
1085 spin_unlock_bh(&dwc->lock);
1086 dev_dbg(chan2dev(&dwc->chan),
1087 "queue and/or active list are not empty\n");
1088 return ERR_PTR(-EBUSY);
1089 }
1090
1091 was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1092 spin_unlock_bh(&dwc->lock);
1093 if (was_cyclic) {
1094 dev_dbg(chan2dev(&dwc->chan),
1095 "channel already prepared for cyclic DMA\n");
1096 return ERR_PTR(-EBUSY);
1097 }
1098
1099 retval = ERR_PTR(-EINVAL);
1100 reg_width = dws->reg_width;
1101 periods = buf_len / period_len;
1102
1103 /* Check for too big/unaligned periods and unaligned DMA buffer. */
1104 if (period_len > (DWC_MAX_COUNT << reg_width))
1105 goto out_err;
1106 if (unlikely(period_len & ((1 << reg_width) - 1)))
1107 goto out_err;
1108 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
1109 goto out_err;
1110 if (unlikely(!(direction & (DMA_TO_DEVICE | DMA_FROM_DEVICE))))
1111 goto out_err;
1112
1113 retval = ERR_PTR(-ENOMEM);
1114
1115 if (periods > NR_DESCS_PER_CHANNEL)
1116 goto out_err;
1117
1118 cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
1119 if (!cdesc)
1120 goto out_err;
1121
1122 cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
1123 if (!cdesc->desc)
1124 goto out_err_alloc;
1125
1126 for (i = 0; i < periods; i++) {
1127 desc = dwc_desc_get(dwc);
1128 if (!desc)
1129 goto out_err_desc_get;
1130
1131 switch (direction) {
1132 case DMA_TO_DEVICE:
1133 desc->lli.dar = dws->tx_reg;
1134 desc->lli.sar = buf_addr + (period_len * i);
Jamie Ilesf301c062011-01-21 14:11:53 +00001135 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001136 | DWC_CTLL_DST_WIDTH(reg_width)
1137 | DWC_CTLL_SRC_WIDTH(reg_width)
1138 | DWC_CTLL_DST_FIX
1139 | DWC_CTLL_SRC_INC
1140 | DWC_CTLL_FC_M2P
1141 | DWC_CTLL_INT_EN);
1142 break;
1143 case DMA_FROM_DEVICE:
1144 desc->lli.dar = buf_addr + (period_len * i);
1145 desc->lli.sar = dws->rx_reg;
Jamie Ilesf301c062011-01-21 14:11:53 +00001146 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001147 | DWC_CTLL_SRC_WIDTH(reg_width)
1148 | DWC_CTLL_DST_WIDTH(reg_width)
1149 | DWC_CTLL_DST_INC
1150 | DWC_CTLL_SRC_FIX
1151 | DWC_CTLL_FC_P2M
1152 | DWC_CTLL_INT_EN);
1153 break;
1154 default:
1155 break;
1156 }
1157
1158 desc->lli.ctlhi = (period_len >> reg_width);
1159 cdesc->desc[i] = desc;
1160
1161 if (last) {
1162 last->lli.llp = desc->txd.phys;
1163 dma_sync_single_for_device(chan2parent(chan),
1164 last->txd.phys, sizeof(last->lli),
1165 DMA_TO_DEVICE);
1166 }
1167
1168 last = desc;
1169 }
1170
1171 /* lets make a cyclic list */
1172 last->lli.llp = cdesc->desc[0]->txd.phys;
1173 dma_sync_single_for_device(chan2parent(chan), last->txd.phys,
1174 sizeof(last->lli), DMA_TO_DEVICE);
1175
1176 dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%08x len %zu "
1177 "period %zu periods %d\n", buf_addr, buf_len,
1178 period_len, periods);
1179
1180 cdesc->periods = periods;
1181 dwc->cdesc = cdesc;
1182
1183 return cdesc;
1184
1185out_err_desc_get:
1186 while (i--)
1187 dwc_desc_put(dwc, cdesc->desc[i]);
1188out_err_alloc:
1189 kfree(cdesc);
1190out_err:
1191 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1192 return (struct dw_cyclic_desc *)retval;
1193}
1194EXPORT_SYMBOL(dw_dma_cyclic_prep);
1195
1196/**
1197 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1198 * @chan: the DMA channel to free
1199 */
1200void dw_dma_cyclic_free(struct dma_chan *chan)
1201{
1202 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1203 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1204 struct dw_cyclic_desc *cdesc = dwc->cdesc;
1205 int i;
1206
1207 dev_dbg(chan2dev(&dwc->chan), "cyclic free\n");
1208
1209 if (!cdesc)
1210 return;
1211
1212 spin_lock_bh(&dwc->lock);
1213
1214 channel_clear_bit(dw, CH_EN, dwc->mask);
1215 while (dma_readl(dw, CH_EN) & dwc->mask)
1216 cpu_relax();
1217
1218 dma_writel(dw, CLEAR.BLOCK, dwc->mask);
1219 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1220 dma_writel(dw, CLEAR.XFER, dwc->mask);
1221
1222 spin_unlock_bh(&dwc->lock);
1223
1224 for (i = 0; i < cdesc->periods; i++)
1225 dwc_desc_put(dwc, cdesc->desc[i]);
1226
1227 kfree(cdesc->desc);
1228 kfree(cdesc);
1229
1230 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1231}
1232EXPORT_SYMBOL(dw_dma_cyclic_free);
1233
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001234/*----------------------------------------------------------------------*/
1235
1236static void dw_dma_off(struct dw_dma *dw)
1237{
1238 dma_writel(dw, CFG, 0);
1239
1240 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
1241 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
1242 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1243 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1244 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1245
1246 while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
1247 cpu_relax();
1248}
1249
1250static int __init dw_probe(struct platform_device *pdev)
1251{
1252 struct dw_dma_platform_data *pdata;
1253 struct resource *io;
1254 struct dw_dma *dw;
1255 size_t size;
1256 int irq;
1257 int err;
1258 int i;
1259
1260 pdata = pdev->dev.platform_data;
1261 if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS)
1262 return -EINVAL;
1263
1264 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1265 if (!io)
1266 return -EINVAL;
1267
1268 irq = platform_get_irq(pdev, 0);
1269 if (irq < 0)
1270 return irq;
1271
1272 size = sizeof(struct dw_dma);
1273 size += pdata->nr_channels * sizeof(struct dw_dma_chan);
1274 dw = kzalloc(size, GFP_KERNEL);
1275 if (!dw)
1276 return -ENOMEM;
1277
1278 if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) {
1279 err = -EBUSY;
1280 goto err_kfree;
1281 }
1282
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001283 dw->regs = ioremap(io->start, DW_REGLEN);
1284 if (!dw->regs) {
1285 err = -ENOMEM;
1286 goto err_release_r;
1287 }
1288
1289 dw->clk = clk_get(&pdev->dev, "hclk");
1290 if (IS_ERR(dw->clk)) {
1291 err = PTR_ERR(dw->clk);
1292 goto err_clk;
1293 }
1294 clk_enable(dw->clk);
1295
1296 /* force dma off, just in case */
1297 dw_dma_off(dw);
1298
1299 err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw);
1300 if (err)
1301 goto err_irq;
1302
1303 platform_set_drvdata(pdev, dw);
1304
1305 tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
1306
1307 dw->all_chan_mask = (1 << pdata->nr_channels) - 1;
1308
1309 INIT_LIST_HEAD(&dw->dma.channels);
1310 for (i = 0; i < pdata->nr_channels; i++, dw->dma.chancnt++) {
1311 struct dw_dma_chan *dwc = &dw->chan[i];
1312
1313 dwc->chan.device = &dw->dma;
1314 dwc->chan.cookie = dwc->completed = 1;
1315 dwc->chan.chan_id = i;
1316 list_add_tail(&dwc->chan.device_node, &dw->dma.channels);
1317
1318 dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
1319 spin_lock_init(&dwc->lock);
1320 dwc->mask = 1 << i;
1321
1322 INIT_LIST_HEAD(&dwc->active_list);
1323 INIT_LIST_HEAD(&dwc->queue);
1324 INIT_LIST_HEAD(&dwc->free_list);
1325
1326 channel_clear_bit(dw, CH_EN, dwc->mask);
1327 }
1328
1329 /* Clear/disable all interrupts on all channels. */
1330 dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
1331 dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
1332 dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
1333 dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
1334 dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
1335
1336 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
1337 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
1338 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1339 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1340 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1341
1342 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
1343 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
Jamie Iles95ea7592011-01-21 14:11:54 +00001344 if (pdata->is_private)
1345 dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001346 dw->dma.dev = &pdev->dev;
1347 dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
1348 dw->dma.device_free_chan_resources = dwc_free_chan_resources;
1349
1350 dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
1351
1352 dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
Linus Walleijc3635c72010-03-26 16:44:01 -07001353 dw->dma.device_control = dwc_control;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001354
Linus Walleij07934482010-03-26 16:50:49 -07001355 dw->dma.device_tx_status = dwc_tx_status;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001356 dw->dma.device_issue_pending = dwc_issue_pending;
1357
1358 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1359
1360 printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n",
Kay Sieversdfbc9012009-03-24 16:38:22 -07001361 dev_name(&pdev->dev), dw->dma.chancnt);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001362
1363 dma_async_device_register(&dw->dma);
1364
1365 return 0;
1366
1367err_irq:
1368 clk_disable(dw->clk);
1369 clk_put(dw->clk);
1370err_clk:
1371 iounmap(dw->regs);
1372 dw->regs = NULL;
1373err_release_r:
1374 release_resource(io);
1375err_kfree:
1376 kfree(dw);
1377 return err;
1378}
1379
1380static int __exit dw_remove(struct platform_device *pdev)
1381{
1382 struct dw_dma *dw = platform_get_drvdata(pdev);
1383 struct dw_dma_chan *dwc, *_dwc;
1384 struct resource *io;
1385
1386 dw_dma_off(dw);
1387 dma_async_device_unregister(&dw->dma);
1388
1389 free_irq(platform_get_irq(pdev, 0), dw);
1390 tasklet_kill(&dw->tasklet);
1391
1392 list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
1393 chan.device_node) {
1394 list_del(&dwc->chan.device_node);
1395 channel_clear_bit(dw, CH_EN, dwc->mask);
1396 }
1397
1398 clk_disable(dw->clk);
1399 clk_put(dw->clk);
1400
1401 iounmap(dw->regs);
1402 dw->regs = NULL;
1403
1404 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1405 release_mem_region(io->start, DW_REGLEN);
1406
1407 kfree(dw);
1408
1409 return 0;
1410}
1411
1412static void dw_shutdown(struct platform_device *pdev)
1413{
1414 struct dw_dma *dw = platform_get_drvdata(pdev);
1415
1416 dw_dma_off(platform_get_drvdata(pdev));
1417 clk_disable(dw->clk);
1418}
1419
Magnus Damm4a256b52009-07-08 13:22:18 +02001420static int dw_suspend_noirq(struct device *dev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001421{
Magnus Damm4a256b52009-07-08 13:22:18 +02001422 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001423 struct dw_dma *dw = platform_get_drvdata(pdev);
1424
1425 dw_dma_off(platform_get_drvdata(pdev));
1426 clk_disable(dw->clk);
1427 return 0;
1428}
1429
Magnus Damm4a256b52009-07-08 13:22:18 +02001430static int dw_resume_noirq(struct device *dev)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001431{
Magnus Damm4a256b52009-07-08 13:22:18 +02001432 struct platform_device *pdev = to_platform_device(dev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001433 struct dw_dma *dw = platform_get_drvdata(pdev);
1434
1435 clk_enable(dw->clk);
1436 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1437 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001438}
1439
Alexey Dobriyan47145212009-12-14 18:00:08 -08001440static const struct dev_pm_ops dw_dev_pm_ops = {
Magnus Damm4a256b52009-07-08 13:22:18 +02001441 .suspend_noirq = dw_suspend_noirq,
1442 .resume_noirq = dw_resume_noirq,
1443};
1444
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001445static struct platform_driver dw_driver = {
1446 .remove = __exit_p(dw_remove),
1447 .shutdown = dw_shutdown,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001448 .driver = {
1449 .name = "dw_dmac",
Magnus Damm4a256b52009-07-08 13:22:18 +02001450 .pm = &dw_dev_pm_ops,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001451 },
1452};
1453
1454static int __init dw_init(void)
1455{
1456 return platform_driver_probe(&dw_driver, dw_probe);
1457}
Viresh Kumarcb689a72011-03-03 15:47:15 +05301458subsys_initcall(dw_init);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001459
1460static void __exit dw_exit(void)
1461{
1462 platform_driver_unregister(&dw_driver);
1463}
1464module_exit(dw_exit);
1465
1466MODULE_LICENSE("GPL v2");
1467MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver");
1468MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");