blob: 244722170410fa774f1a1f5fd42898a26fe0cd9d [file] [log] [blame]
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001/*
Heikki Krogerusb8014792012-10-18 17:34:08 +03002 * Core driver for the Synopsys DesignWare DMA Controller
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07003 *
4 * Copyright (C) 2007-2008 Atmel Corporation
Viresh Kumaraecb7b62011-05-24 14:04:09 +05305 * Copyright (C) 2010-2011 ST Microelectronics
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03006 * Copyright (C) 2013 Intel Corporation
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
Heikki Krogerusb8014792012-10-18 17:34:08 +030012
Viresh Kumar327e6972012-02-01 16:12:26 +053013#include <linux/bitops.h>
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070014#include <linux/delay.h>
15#include <linux/dmaengine.h>
16#include <linux/dma-mapping.h>
Andy Shevchenkof8122a82013-01-16 15:48:50 +020017#include <linux/dmapool.h>
Thierry Reding73312052013-01-21 11:09:00 +010018#include <linux/err.h>
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070019#include <linux/init.h>
20#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/mm.h>
23#include <linux/module.h>
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070024#include <linux/slab.h>
25
Andy Shevchenko61a76492013-06-05 15:26:44 +030026#include "../dmaengine.h"
Andy Shevchenko9cade1a2013-06-05 15:26:45 +030027#include "internal.h"
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070028
29/*
30 * This supports the Synopsys "DesignWare AHB Central DMA Controller",
31 * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all
32 * of which use ARM any more). See the "Databook" from Synopsys for
33 * information beyond what licensees probably provide.
34 *
Andy Shevchenkodd5720b2014-02-12 11:16:17 +020035 * The driver has been tested with the Atmel AT32AP7000, which does not
36 * support descriptor writeback.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070037 */
38
Viresh Kumar327e6972012-02-01 16:12:26 +053039#define DWC_DEFAULT_CTLLO(_chan) ({ \
Viresh Kumar327e6972012-02-01 16:12:26 +053040 struct dw_dma_chan *_dwc = to_dw_dma_chan(_chan); \
41 struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \
Andy Shevchenko495aea42013-01-10 11:11:41 +020042 bool _is_slave = is_slave_direction(_dwc->direction); \
Andy Shevchenko495aea42013-01-10 11:11:41 +020043 u8 _smsize = _is_slave ? _sconfig->src_maxburst : \
Viresh Kumar327e6972012-02-01 16:12:26 +053044 DW_DMA_MSIZE_16; \
Andy Shevchenko495aea42013-01-10 11:11:41 +020045 u8 _dmsize = _is_slave ? _sconfig->dst_maxburst : \
Viresh Kumar327e6972012-02-01 16:12:26 +053046 DW_DMA_MSIZE_16; \
Jamie Ilesf301c062011-01-21 14:11:53 +000047 \
Viresh Kumar327e6972012-02-01 16:12:26 +053048 (DWC_CTLL_DST_MSIZE(_dmsize) \
49 | DWC_CTLL_SRC_MSIZE(_smsize) \
Jamie Ilesf301c062011-01-21 14:11:53 +000050 | DWC_CTLL_LLP_D_EN \
51 | DWC_CTLL_LLP_S_EN \
Arnd Bergmannf7760762013-03-26 16:53:57 +020052 | DWC_CTLL_DMS(_dwc->dst_master) \
53 | DWC_CTLL_SMS(_dwc->src_master)); \
Jamie Ilesf301c062011-01-21 14:11:53 +000054 })
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070055
56/*
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070057 * Number of descriptors to allocate for each channel. This should be
58 * made configurable somehow; preferably, the clients (at least the
59 * ones using slave transfers) should be able to give us a hint.
60 */
61#define NR_DESCS_PER_CHANNEL 64
62
63/*----------------------------------------------------------------------*/
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070064
Dan Williams41d5e592009-01-06 11:38:21 -070065static struct device *chan2dev(struct dma_chan *chan)
66{
67 return &chan->dev->device;
68}
Dan Williams41d5e592009-01-06 11:38:21 -070069
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070070static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc)
71{
Andy Shevchenkoe63a47a2012-10-18 17:34:12 +030072 return to_dw_desc(dwc->active_list.next);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070073}
74
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070075static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc)
76{
77 struct dw_desc *desc, *_desc;
78 struct dw_desc *ret = NULL;
79 unsigned int i = 0;
Viresh Kumar69cea5a2011-04-15 16:03:35 +053080 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070081
Viresh Kumar69cea5a2011-04-15 16:03:35 +053082 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070083 list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) {
Andy Shevchenko2ab37272012-06-19 13:34:04 +030084 i++;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070085 if (async_tx_test_ack(&desc->txd)) {
86 list_del(&desc->desc_node);
87 ret = desc;
88 break;
89 }
Dan Williams41d5e592009-01-06 11:38:21 -070090 dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070091 }
Viresh Kumar69cea5a2011-04-15 16:03:35 +053092 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070093
Dan Williams41d5e592009-01-06 11:38:21 -070094 dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070095
96 return ret;
97}
98
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -070099/*
100 * Move a descriptor, including any children, to the free list.
101 * `desc' must not be on any lists.
102 */
103static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc)
104{
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530105 unsigned long flags;
106
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700107 if (desc) {
108 struct dw_desc *child;
109
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530110 spin_lock_irqsave(&dwc->lock, flags);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700111 list_for_each_entry(child, &desc->tx_list, desc_node)
Dan Williams41d5e592009-01-06 11:38:21 -0700112 dev_vdbg(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700113 "moving child desc %p to freelist\n",
114 child);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700115 list_splice_init(&desc->tx_list, &dwc->free_list);
Dan Williams41d5e592009-01-06 11:38:21 -0700116 dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700117 list_add(&desc->desc_node, &dwc->free_list);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530118 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700119 }
120}
121
Viresh Kumar61e183f2011-11-17 16:01:29 +0530122static void dwc_initialize(struct dw_dma_chan *dwc)
123{
124 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
125 struct dw_dma_slave *dws = dwc->chan.private;
126 u32 cfghi = DWC_CFGH_FIFO_MODE;
127 u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority);
128
129 if (dwc->initialized == true)
130 return;
131
Arnd Bergmannf7760762013-03-26 16:53:57 +0200132 if (dws) {
Viresh Kumar61e183f2011-11-17 16:01:29 +0530133 /*
134 * We need controller-specific data to set up slave
135 * transfers.
136 */
137 BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev);
138
Andy Shevchenko7e1e2f22014-08-19 20:29:14 +0300139 cfghi |= DWC_CFGH_DST_PER(dws->dst_id);
140 cfghi |= DWC_CFGH_SRC_PER(dws->src_id);
Andy Shevchenko8fccc5b2012-09-03 13:46:19 +0300141 } else {
Andy Shevchenko89500522014-08-19 20:29:15 +0300142 cfghi |= DWC_CFGH_DST_PER(dwc->dst_id);
143 cfghi |= DWC_CFGH_SRC_PER(dwc->src_id);
Viresh Kumar61e183f2011-11-17 16:01:29 +0530144 }
145
146 channel_writel(dwc, CFG_LO, cfglo);
147 channel_writel(dwc, CFG_HI, cfghi);
148
149 /* Enable interrupts */
150 channel_set_bit(dw, MASK.XFER, dwc->mask);
Viresh Kumar61e183f2011-11-17 16:01:29 +0530151 channel_set_bit(dw, MASK.ERROR, dwc->mask);
152
153 dwc->initialized = true;
154}
155
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700156/*----------------------------------------------------------------------*/
157
Andy Shevchenko4c2d56c2012-06-19 13:34:08 +0300158static inline unsigned int dwc_fast_fls(unsigned long long v)
159{
160 /*
161 * We can be a lot more clever here, but this should take care
162 * of the most common optimization.
163 */
164 if (!(v & 7))
165 return 3;
166 else if (!(v & 3))
167 return 2;
168 else if (!(v & 1))
169 return 1;
170 return 0;
171}
172
Andy Shevchenkof52b36d2012-09-21 15:05:44 +0300173static inline void dwc_dump_chan_regs(struct dw_dma_chan *dwc)
Andy Shevchenko1d455432012-06-19 13:34:03 +0300174{
175 dev_err(chan2dev(&dwc->chan),
176 " 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
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300184static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc)
185{
186 channel_clear_bit(dw, CH_EN, dwc->mask);
187 while (dma_readl(dw, CH_EN) & dwc->mask)
188 cpu_relax();
189}
190
Andy Shevchenko1d455432012-06-19 13:34:03 +0300191/*----------------------------------------------------------------------*/
192
Andy Shevchenkofed25742012-09-21 15:05:49 +0300193/* Perform single block transfer */
194static inline void dwc_do_single_block(struct dw_dma_chan *dwc,
195 struct dw_desc *desc)
196{
197 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
198 u32 ctllo;
199
Andy Shevchenko1d566f12014-01-13 14:04:48 +0200200 /*
201 * Software emulation of LLP mode relies on interrupts to continue
202 * multi block transfer.
203 */
Andy Shevchenkofed25742012-09-21 15:05:49 +0300204 ctllo = desc->lli.ctllo | DWC_CTLL_INT_EN;
205
206 channel_writel(dwc, SAR, desc->lli.sar);
207 channel_writel(dwc, DAR, desc->lli.dar);
208 channel_writel(dwc, CTL_LO, ctllo);
209 channel_writel(dwc, CTL_HI, desc->lli.ctlhi);
210 channel_set_bit(dw, CH_EN, dwc->mask);
Andy Shevchenkof5c6a7d2013-01-09 10:17:13 +0200211
212 /* Move pointer to next descriptor */
213 dwc->tx_node_active = dwc->tx_node_active->next;
Andy Shevchenkofed25742012-09-21 15:05:49 +0300214}
215
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700216/* Called with dwc->lock held and bh disabled */
217static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first)
218{
219 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Andy Shevchenkofed25742012-09-21 15:05:49 +0300220 unsigned long was_soft_llp;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700221
222 /* ASSERT: channel is idle */
223 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700224 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700225 "BUG: Attempted to start non-idle channel\n");
Andy Shevchenko1d455432012-06-19 13:34:03 +0300226 dwc_dump_chan_regs(dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700227
228 /* The tasklet will hopefully advance the queue... */
229 return;
230 }
231
Andy Shevchenkofed25742012-09-21 15:05:49 +0300232 if (dwc->nollp) {
233 was_soft_llp = test_and_set_bit(DW_DMA_IS_SOFT_LLP,
234 &dwc->flags);
235 if (was_soft_llp) {
236 dev_err(chan2dev(&dwc->chan),
Andy Shevchenkofc61f6b2014-01-13 14:04:49 +0200237 "BUG: Attempted to start new LLP transfer inside ongoing one\n");
Andy Shevchenkofed25742012-09-21 15:05:49 +0300238 return;
239 }
240
241 dwc_initialize(dwc);
242
Andy Shevchenko4702d522013-01-25 11:48:03 +0200243 dwc->residue = first->total_len;
Andy Shevchenkof5c6a7d2013-01-09 10:17:13 +0200244 dwc->tx_node_active = &first->tx_list;
Andy Shevchenkofed25742012-09-21 15:05:49 +0300245
Andy Shevchenkofdf475f2013-01-25 11:48:00 +0200246 /* Submit first block */
Andy Shevchenkofed25742012-09-21 15:05:49 +0300247 dwc_do_single_block(dwc, first);
248
249 return;
250 }
251
Viresh Kumar61e183f2011-11-17 16:01:29 +0530252 dwc_initialize(dwc);
253
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700254 channel_writel(dwc, LLP, first->txd.phys);
255 channel_writel(dwc, CTL_LO,
256 DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
257 channel_writel(dwc, CTL_HI, 0);
258 channel_set_bit(dw, CH_EN, dwc->mask);
259}
260
Andy Shevchenkoe7637c62014-06-18 12:15:36 +0300261static void dwc_dostart_first_queued(struct dw_dma_chan *dwc)
262{
Andy Shevchenkocba15612014-06-18 12:15:37 +0300263 struct dw_desc *desc;
264
Andy Shevchenkoe7637c62014-06-18 12:15:36 +0300265 if (list_empty(&dwc->queue))
266 return;
267
268 list_move(dwc->queue.next, &dwc->active_list);
Andy Shevchenkocba15612014-06-18 12:15:37 +0300269 desc = dwc_first_active(dwc);
270 dev_vdbg(chan2dev(&dwc->chan), "%s: started %u\n", __func__, desc->txd.cookie);
271 dwc_dostart(dwc, desc);
Andy Shevchenkoe7637c62014-06-18 12:15:36 +0300272}
273
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700274/*----------------------------------------------------------------------*/
275
276static void
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530277dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc,
278 bool callback_required)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700279{
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530280 dma_async_tx_callback callback = NULL;
281 void *param = NULL;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700282 struct dma_async_tx_descriptor *txd = &desc->txd;
Viresh Kumare5180762011-03-03 15:47:20 +0530283 struct dw_desc *child;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530284 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700285
Dan Williams41d5e592009-01-06 11:38:21 -0700286 dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700287
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530288 spin_lock_irqsave(&dwc->lock, flags);
Russell King - ARM Linuxf7fbce02012-03-06 22:35:07 +0000289 dma_cookie_complete(txd);
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530290 if (callback_required) {
291 callback = txd->callback;
292 param = txd->callback_param;
293 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700294
Viresh Kumare5180762011-03-03 15:47:20 +0530295 /* async_tx_ack */
296 list_for_each_entry(child, &desc->tx_list, desc_node)
297 async_tx_ack(&child->txd);
298 async_tx_ack(&desc->txd);
299
Dan Williamse0bd0f82009-09-08 17:53:02 -0700300 list_splice_init(&desc->tx_list, &dwc->free_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700301 list_move(&desc->desc_node, &dwc->free_list);
302
Dan Williamsd38a8c62013-10-18 19:35:23 +0200303 dma_descriptor_unmap(txd);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530304 spin_unlock_irqrestore(&dwc->lock, flags);
305
Andy Shevchenko21e93c12013-01-09 10:17:12 +0200306 if (callback)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700307 callback(param);
308}
309
310static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc)
311{
312 struct dw_desc *desc, *_desc;
313 LIST_HEAD(list);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530314 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700315
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530316 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700317 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -0700318 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700319 "BUG: XFER bit set, but channel not idle!\n");
320
321 /* Try to continue after resetting the channel... */
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300322 dwc_chan_disable(dw, dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700323 }
324
325 /*
326 * Submit queued descriptors ASAP, i.e. before we go through
327 * the completed ones.
328 */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700329 list_splice_init(&dwc->active_list, &list);
Andy Shevchenkoe7637c62014-06-18 12:15:36 +0300330 dwc_dostart_first_queued(dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700331
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530332 spin_unlock_irqrestore(&dwc->lock, flags);
333
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700334 list_for_each_entry_safe(desc, _desc, &list, desc_node)
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530335 dwc_descriptor_complete(dwc, desc, true);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700336}
337
Andy Shevchenko4702d522013-01-25 11:48:03 +0200338/* Returns how many bytes were already received from source */
339static inline u32 dwc_get_sent(struct dw_dma_chan *dwc)
340{
341 u32 ctlhi = channel_readl(dwc, CTL_HI);
342 u32 ctllo = channel_readl(dwc, CTL_LO);
343
344 return (ctlhi & DWC_CTLH_BLOCK_TS_MASK) * (1 << (ctllo >> 4 & 7));
345}
346
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700347static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc)
348{
349 dma_addr_t llp;
350 struct dw_desc *desc, *_desc;
351 struct dw_desc *child;
352 u32 status_xfer;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530353 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700354
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530355 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700356 llp = channel_readl(dwc, LLP);
357 status_xfer = dma_readl(dw, RAW.XFER);
358
359 if (status_xfer & dwc->mask) {
360 /* Everything we've submitted is done */
361 dma_writel(dw, CLEAR.XFER, dwc->mask);
Andy Shevchenko77bcc4972013-01-18 14:14:15 +0200362
363 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
Andy Shevchenkofdf475f2013-01-25 11:48:00 +0200364 struct list_head *head, *active = dwc->tx_node_active;
365
366 /*
367 * We are inside first active descriptor.
368 * Otherwise something is really wrong.
369 */
370 desc = dwc_first_active(dwc);
371
372 head = &desc->tx_list;
373 if (active != head) {
Andy Shevchenko4702d522013-01-25 11:48:03 +0200374 /* Update desc to reflect last sent one */
375 if (active != head->next)
376 desc = to_dw_desc(active->prev);
377
378 dwc->residue -= desc->len;
379
Andy Shevchenkofdf475f2013-01-25 11:48:00 +0200380 child = to_dw_desc(active);
Andy Shevchenko77bcc4972013-01-18 14:14:15 +0200381
382 /* Submit next block */
Andy Shevchenkofdf475f2013-01-25 11:48:00 +0200383 dwc_do_single_block(dwc, child);
Andy Shevchenko77bcc4972013-01-18 14:14:15 +0200384
Andy Shevchenkofdf475f2013-01-25 11:48:00 +0200385 spin_unlock_irqrestore(&dwc->lock, flags);
Andy Shevchenko77bcc4972013-01-18 14:14:15 +0200386 return;
387 }
Andy Shevchenkofdf475f2013-01-25 11:48:00 +0200388
Andy Shevchenko77bcc4972013-01-18 14:14:15 +0200389 /* We are done here */
390 clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
391 }
Andy Shevchenko4702d522013-01-25 11:48:03 +0200392
393 dwc->residue = 0;
394
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530395 spin_unlock_irqrestore(&dwc->lock, flags);
396
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700397 dwc_complete_all(dw, dwc);
398 return;
399 }
400
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530401 if (list_empty(&dwc->active_list)) {
Andy Shevchenko4702d522013-01-25 11:48:03 +0200402 dwc->residue = 0;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530403 spin_unlock_irqrestore(&dwc->lock, flags);
Jamie Iles087809f2011-01-21 14:11:52 +0000404 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530405 }
Jamie Iles087809f2011-01-21 14:11:52 +0000406
Andy Shevchenko77bcc4972013-01-18 14:14:15 +0200407 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) {
408 dev_vdbg(chan2dev(&dwc->chan), "%s: soft LLP mode\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700409 spin_unlock_irqrestore(&dwc->lock, flags);
Dan Williams41d5e592009-01-06 11:38:21 -0700410 return;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700411 }
412
Andy Shevchenko5a87f0e2014-01-13 14:04:50 +0200413 dev_vdbg(chan2dev(&dwc->chan), "%s: llp=%pad\n", __func__, &llp);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700414
415 list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) {
Andy Shevchenko75c61222013-03-26 16:53:54 +0200416 /* Initial residue value */
Andy Shevchenko4702d522013-01-25 11:48:03 +0200417 dwc->residue = desc->total_len;
418
Andy Shevchenko75c61222013-03-26 16:53:54 +0200419 /* Check first descriptors addr */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530420 if (desc->txd.phys == llp) {
421 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700422 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530423 }
Viresh Kumar84adccf2011-03-24 11:32:15 +0530424
Andy Shevchenko75c61222013-03-26 16:53:54 +0200425 /* Check first descriptors llp */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530426 if (desc->lli.llp == llp) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700427 /* This one is currently in progress */
Andy Shevchenko4702d522013-01-25 11:48:03 +0200428 dwc->residue -= dwc_get_sent(dwc);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530429 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700430 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530431 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700432
Andy Shevchenko4702d522013-01-25 11:48:03 +0200433 dwc->residue -= desc->len;
434 list_for_each_entry(child, &desc->tx_list, desc_node) {
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530435 if (child->lli.llp == llp) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700436 /* Currently in progress */
Andy Shevchenko4702d522013-01-25 11:48:03 +0200437 dwc->residue -= dwc_get_sent(dwc);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530438 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700439 return;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530440 }
Andy Shevchenko4702d522013-01-25 11:48:03 +0200441 dwc->residue -= child->len;
442 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700443
444 /*
445 * No descriptors so far seem to be in progress, i.e.
446 * this one must be done.
447 */
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530448 spin_unlock_irqrestore(&dwc->lock, flags);
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530449 dwc_descriptor_complete(dwc, desc, true);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530450 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700451 }
452
Dan Williams41d5e592009-01-06 11:38:21 -0700453 dev_err(chan2dev(&dwc->chan),
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700454 "BUG: All descriptors done, but channel not idle!\n");
455
456 /* Try to continue after resetting the channel... */
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300457 dwc_chan_disable(dw, dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700458
Andy Shevchenkoe7637c62014-06-18 12:15:36 +0300459 dwc_dostart_first_queued(dwc);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530460 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700461}
462
Andy Shevchenko93aad1b2012-07-13 11:09:32 +0300463static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700464{
Andy Shevchenko21d43f42012-10-18 17:34:09 +0300465 dev_crit(chan2dev(&dwc->chan), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n",
466 lli->sar, lli->dar, lli->llp, lli->ctlhi, lli->ctllo);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700467}
468
469static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc)
470{
471 struct dw_desc *bad_desc;
472 struct dw_desc *child;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530473 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700474
475 dwc_scan_descriptors(dw, dwc);
476
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530477 spin_lock_irqsave(&dwc->lock, flags);
478
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700479 /*
480 * The descriptor currently at the head of the active list is
481 * borked. Since we don't have any way to report errors, we'll
482 * just have to scream loudly and try to carry on.
483 */
484 bad_desc = dwc_first_active(dwc);
485 list_del_init(&bad_desc->desc_node);
Viresh Kumarf336e422011-03-03 15:47:16 +0530486 list_move(dwc->queue.next, dwc->active_list.prev);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700487
488 /* Clear the error flag and try to restart the controller */
489 dma_writel(dw, CLEAR.ERROR, dwc->mask);
490 if (!list_empty(&dwc->active_list))
491 dwc_dostart(dwc, dwc_first_active(dwc));
492
493 /*
Andy Shevchenkoba84bd72012-10-18 17:34:11 +0300494 * WARN may seem harsh, but since this only happens
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700495 * when someone submits a bad physical address in a
496 * descriptor, we should consider ourselves lucky that the
497 * controller flagged an error instead of scribbling over
498 * random memory locations.
499 */
Andy Shevchenkoba84bd72012-10-18 17:34:11 +0300500 dev_WARN(chan2dev(&dwc->chan), "Bad descriptor submitted for DMA!\n"
501 " cookie: %d\n", bad_desc->txd.cookie);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700502 dwc_dump_lli(dwc, &bad_desc->lli);
Dan Williamse0bd0f82009-09-08 17:53:02 -0700503 list_for_each_entry(child, &bad_desc->tx_list, desc_node)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700504 dwc_dump_lli(dwc, &child->lli);
505
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530506 spin_unlock_irqrestore(&dwc->lock, flags);
507
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700508 /* Pretend the descriptor completed successfully */
Viresh Kumar5fedefb2011-04-15 16:03:35 +0530509 dwc_descriptor_complete(dwc, bad_desc, true);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700510}
511
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200512/* --------------------- Cyclic DMA API extensions -------------------- */
513
Denis Efremov8004cbb2013-05-09 13:19:40 +0400514dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200515{
516 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
517 return channel_readl(dwc, SAR);
518}
519EXPORT_SYMBOL(dw_dma_get_src_addr);
520
Denis Efremov8004cbb2013-05-09 13:19:40 +0400521dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200522{
523 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
524 return channel_readl(dwc, DAR);
525}
526EXPORT_SYMBOL(dw_dma_get_dst_addr);
527
Andy Shevchenko75c61222013-03-26 16:53:54 +0200528/* Called with dwc->lock held and all DMAC interrupts disabled */
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200529static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc,
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530530 u32 status_err, u32 status_xfer)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200531{
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530532 unsigned long flags;
533
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530534 if (dwc->mask) {
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200535 void (*callback)(void *param);
536 void *callback_param;
537
538 dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n",
539 channel_readl(dwc, LLP));
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200540
541 callback = dwc->cdesc->period_callback;
542 callback_param = dwc->cdesc->period_callback_param;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530543
544 if (callback)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200545 callback(callback_param);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200546 }
547
548 /*
549 * Error and transfer complete are highly unlikely, and will most
550 * likely be due to a configuration error by the user.
551 */
552 if (unlikely(status_err & dwc->mask) ||
553 unlikely(status_xfer & dwc->mask)) {
554 int i;
555
Andy Shevchenkofc61f6b2014-01-13 14:04:49 +0200556 dev_err(chan2dev(&dwc->chan),
557 "cyclic DMA unexpected %s interrupt, stopping DMA transfer\n",
558 status_xfer ? "xfer" : "error");
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530559
560 spin_lock_irqsave(&dwc->lock, flags);
561
Andy Shevchenko1d455432012-06-19 13:34:03 +0300562 dwc_dump_chan_regs(dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200563
Andy Shevchenko3f9362072012-06-19 13:46:32 +0300564 dwc_chan_disable(dw, dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200565
Andy Shevchenko75c61222013-03-26 16:53:54 +0200566 /* Make sure DMA does not restart by loading a new list */
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200567 channel_writel(dwc, LLP, 0);
568 channel_writel(dwc, CTL_LO, 0);
569 channel_writel(dwc, CTL_HI, 0);
570
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200571 dma_writel(dw, CLEAR.ERROR, dwc->mask);
572 dma_writel(dw, CLEAR.XFER, dwc->mask);
573
574 for (i = 0; i < dwc->cdesc->periods; i++)
575 dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli);
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530576
577 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200578 }
579}
580
581/* ------------------------------------------------------------------------- */
582
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700583static void dw_dma_tasklet(unsigned long data)
584{
585 struct dw_dma *dw = (struct dw_dma *)data;
586 struct dw_dma_chan *dwc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700587 u32 status_xfer;
588 u32 status_err;
589 int i;
590
Haavard Skinnemoen7fe7b2f2008-10-03 15:23:46 -0700591 status_xfer = dma_readl(dw, RAW.XFER);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700592 status_err = dma_readl(dw, RAW.ERROR);
593
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300594 dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700595
596 for (i = 0; i < dw->dma.chancnt; i++) {
597 dwc = &dw->chan[i];
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200598 if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags))
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530599 dwc_handle_cyclic(dw, dwc, status_err, status_xfer);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +0200600 else if (status_err & (1 << i))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700601 dwc_handle_error(dw, dwc);
Andy Shevchenko77bcc4972013-01-18 14:14:15 +0200602 else if (status_xfer & (1 << i))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700603 dwc_scan_descriptors(dw, dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700604 }
605
606 /*
Viresh Kumarff7b05f2012-02-01 16:12:23 +0530607 * Re-enable interrupts.
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700608 */
609 channel_set_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700610 channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask);
611}
612
613static irqreturn_t dw_dma_interrupt(int irq, void *dev_id)
614{
615 struct dw_dma *dw = dev_id;
Andy Shevchenko3783cef2013-07-15 15:04:39 +0300616 u32 status = dma_readl(dw, STATUS_INT);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700617
Andy Shevchenko3783cef2013-07-15 15:04:39 +0300618 dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__, status);
619
620 /* Check if we have any interrupt from the DMAC */
621 if (!status)
622 return IRQ_NONE;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700623
624 /*
625 * Just disable the interrupts. We'll turn them back on in the
626 * softirq handler.
627 */
628 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700629 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
630
631 status = dma_readl(dw, STATUS_INT);
632 if (status) {
633 dev_err(dw->dma.dev,
634 "BUG: Unexpected interrupts pending: 0x%x\n",
635 status);
636
637 /* Try to recover */
638 channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700639 channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1);
640 channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1);
641 channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1);
642 }
643
644 tasklet_schedule(&dw->tasklet);
645
646 return IRQ_HANDLED;
647}
648
649/*----------------------------------------------------------------------*/
650
651static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx)
652{
653 struct dw_desc *desc = txd_to_dw_desc(tx);
654 struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan);
655 dma_cookie_t cookie;
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530656 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700657
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530658 spin_lock_irqsave(&dwc->lock, flags);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000659 cookie = dma_cookie_assign(tx);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700660
661 /*
662 * REVISIT: We should attempt to chain as many descriptors as
663 * possible, perhaps even appending to those already submitted
664 * for DMA. But this is hard to do in a race-free manner.
665 */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700666
Andy Shevchenkodd8ecfca2014-06-18 12:15:38 +0300667 dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n", __func__, desc->txd.cookie);
668 list_add_tail(&desc->desc_node, &dwc->queue);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700669
Viresh Kumar69cea5a2011-04-15 16:03:35 +0530670 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700671
672 return cookie;
673}
674
675static struct dma_async_tx_descriptor *
676dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
677 size_t len, unsigned long flags)
678{
679 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Arnd Bergmannf7760762013-03-26 16:53:57 +0200680 struct dw_dma *dw = to_dw_dma(chan->device);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700681 struct dw_desc *desc;
682 struct dw_desc *first;
683 struct dw_desc *prev;
684 size_t xfer_count;
685 size_t offset;
686 unsigned int src_width;
687 unsigned int dst_width;
Andy Shevchenko3d4f8602012-10-01 13:06:25 +0300688 unsigned int data_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700689 u32 ctllo;
690
Andy Shevchenko2f45d612012-06-19 13:34:02 +0300691 dev_vdbg(chan2dev(chan),
Andy Shevchenko5a87f0e2014-01-13 14:04:50 +0200692 "%s: d%pad s%pad l0x%zx f0x%lx\n", __func__,
693 &dest, &src, len, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700694
695 if (unlikely(!len)) {
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300696 dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700697 return NULL;
698 }
699
Andy Shevchenko0fdb5672013-01-10 10:53:03 +0200700 dwc->direction = DMA_MEM_TO_MEM;
701
Arnd Bergmannf7760762013-03-26 16:53:57 +0200702 data_width = min_t(unsigned int, dw->data_width[dwc->src_master],
703 dw->data_width[dwc->dst_master]);
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300704
Andy Shevchenko3d4f8602012-10-01 13:06:25 +0300705 src_width = dst_width = min_t(unsigned int, data_width,
706 dwc_fast_fls(src | dest | len));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700707
Viresh Kumar327e6972012-02-01 16:12:26 +0530708 ctllo = DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700709 | DWC_CTLL_DST_WIDTH(dst_width)
710 | DWC_CTLL_SRC_WIDTH(src_width)
711 | DWC_CTLL_DST_INC
712 | DWC_CTLL_SRC_INC
713 | DWC_CTLL_FC_M2M;
714 prev = first = NULL;
715
716 for (offset = 0; offset < len; offset += xfer_count << src_width) {
717 xfer_count = min_t(size_t, (len - offset) >> src_width,
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +0300718 dwc->block_size);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700719
720 desc = dwc_desc_get(dwc);
721 if (!desc)
722 goto err_desc_get;
723
724 desc->lli.sar = src + offset;
725 desc->lli.dar = dest + offset;
726 desc->lli.ctllo = ctllo;
727 desc->lli.ctlhi = xfer_count;
Andy Shevchenko176dcec2013-01-25 11:48:02 +0200728 desc->len = xfer_count << src_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700729
730 if (!first) {
731 first = desc;
732 } else {
733 prev->lli.llp = desc->txd.phys;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700734 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700735 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700736 }
737 prev = desc;
738 }
739
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700740 if (flags & DMA_PREP_INTERRUPT)
741 /* Trigger interrupt after last block */
742 prev->lli.ctllo |= DWC_CTLL_INT_EN;
743
744 prev->lli.llp = 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700745 first->txd.flags = flags;
Andy Shevchenko30d38a32013-01-25 11:48:01 +0200746 first->total_len = len;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700747
748 return &first->txd;
749
750err_desc_get:
751 dwc_desc_put(dwc, first);
752 return NULL;
753}
754
755static struct dma_async_tx_descriptor *
756dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
Vinod Kouldb8196d2011-10-13 22:34:23 +0530757 unsigned int sg_len, enum dma_transfer_direction direction,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500758 unsigned long flags, void *context)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700759{
760 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Arnd Bergmannf7760762013-03-26 16:53:57 +0200761 struct dw_dma *dw = to_dw_dma(chan->device);
Viresh Kumar327e6972012-02-01 16:12:26 +0530762 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700763 struct dw_desc *prev;
764 struct dw_desc *first;
765 u32 ctllo;
766 dma_addr_t reg;
767 unsigned int reg_width;
768 unsigned int mem_width;
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300769 unsigned int data_width;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700770 unsigned int i;
771 struct scatterlist *sg;
772 size_t total_len = 0;
773
Andy Shevchenko2e4c3642012-06-19 13:34:05 +0300774 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700775
Andy Shevchenko495aea42013-01-10 11:11:41 +0200776 if (unlikely(!is_slave_direction(direction) || !sg_len))
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700777 return NULL;
778
Andy Shevchenko0fdb5672013-01-10 10:53:03 +0200779 dwc->direction = direction;
780
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700781 prev = first = NULL;
782
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700783 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +0530784 case DMA_MEM_TO_DEV:
Viresh Kumar327e6972012-02-01 16:12:26 +0530785 reg_width = __fls(sconfig->dst_addr_width);
786 reg = sconfig->dst_addr;
787 ctllo = (DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700788 | DWC_CTLL_DST_WIDTH(reg_width)
789 | DWC_CTLL_DST_FIX
Viresh Kumar327e6972012-02-01 16:12:26 +0530790 | DWC_CTLL_SRC_INC);
791
792 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
793 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
794
Arnd Bergmannf7760762013-03-26 16:53:57 +0200795 data_width = dw->data_width[dwc->src_master];
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300796
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700797 for_each_sg(sgl, sg, sg_len, i) {
798 struct dw_desc *desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530799 u32 len, dlen, mem;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700800
Lars-Peter Clausencbb796c2012-04-25 20:50:51 +0200801 mem = sg_dma_address(sg);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700802 len = sg_dma_len(sg);
Viresh Kumar6bc711f2012-02-01 16:12:25 +0530803
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300804 mem_width = min_t(unsigned int,
805 data_width, dwc_fast_fls(mem | len));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700806
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530807slave_sg_todev_fill_desc:
808 desc = dwc_desc_get(dwc);
809 if (!desc) {
810 dev_err(chan2dev(chan),
811 "not enough descriptors available\n");
812 goto err_desc_get;
813 }
814
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700815 desc->lli.sar = mem;
816 desc->lli.dar = reg;
817 desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width);
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +0300818 if ((len >> mem_width) > dwc->block_size) {
819 dlen = dwc->block_size << mem_width;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530820 mem += dlen;
821 len -= dlen;
822 } else {
823 dlen = len;
824 len = 0;
825 }
826
827 desc->lli.ctlhi = dlen >> mem_width;
Andy Shevchenko176dcec2013-01-25 11:48:02 +0200828 desc->len = dlen;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700829
830 if (!first) {
831 first = desc;
832 } else {
833 prev->lli.llp = desc->txd.phys;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700834 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700835 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700836 }
837 prev = desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530838 total_len += dlen;
839
840 if (len)
841 goto slave_sg_todev_fill_desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700842 }
843 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530844 case DMA_DEV_TO_MEM:
Viresh Kumar327e6972012-02-01 16:12:26 +0530845 reg_width = __fls(sconfig->src_addr_width);
846 reg = sconfig->src_addr;
847 ctllo = (DWC_DEFAULT_CTLLO(chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700848 | DWC_CTLL_SRC_WIDTH(reg_width)
849 | DWC_CTLL_DST_INC
Viresh Kumar327e6972012-02-01 16:12:26 +0530850 | DWC_CTLL_SRC_FIX);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700851
Viresh Kumar327e6972012-02-01 16:12:26 +0530852 ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
853 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
854
Arnd Bergmannf7760762013-03-26 16:53:57 +0200855 data_width = dw->data_width[dwc->dst_master];
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300856
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700857 for_each_sg(sgl, sg, sg_len, i) {
858 struct dw_desc *desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530859 u32 len, dlen, mem;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700860
Lars-Peter Clausencbb796c2012-04-25 20:50:51 +0200861 mem = sg_dma_address(sg);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700862 len = sg_dma_len(sg);
Viresh Kumar6bc711f2012-02-01 16:12:25 +0530863
Andy Shevchenkoa0982002012-09-21 15:05:48 +0300864 mem_width = min_t(unsigned int,
865 data_width, dwc_fast_fls(mem | len));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700866
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530867slave_sg_fromdev_fill_desc:
868 desc = dwc_desc_get(dwc);
869 if (!desc) {
870 dev_err(chan2dev(chan),
871 "not enough descriptors available\n");
872 goto err_desc_get;
873 }
874
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700875 desc->lli.sar = reg;
876 desc->lli.dar = mem;
877 desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width);
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +0300878 if ((len >> reg_width) > dwc->block_size) {
879 dlen = dwc->block_size << reg_width;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530880 mem += dlen;
881 len -= dlen;
882 } else {
883 dlen = len;
884 len = 0;
885 }
886 desc->lli.ctlhi = dlen >> reg_width;
Andy Shevchenko176dcec2013-01-25 11:48:02 +0200887 desc->len = dlen;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700888
889 if (!first) {
890 first = desc;
891 } else {
892 prev->lli.llp = desc->txd.phys;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700893 list_add_tail(&desc->desc_node,
Dan Williamse0bd0f82009-09-08 17:53:02 -0700894 &first->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700895 }
896 prev = desc;
Viresh Kumar69dc14b2011-04-18 14:54:56 +0530897 total_len += dlen;
898
899 if (len)
900 goto slave_sg_fromdev_fill_desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700901 }
902 break;
903 default:
904 return NULL;
905 }
906
907 if (flags & DMA_PREP_INTERRUPT)
908 /* Trigger interrupt after last block */
909 prev->lli.ctllo |= DWC_CTLL_INT_EN;
910
911 prev->lli.llp = 0;
Andy Shevchenko30d38a32013-01-25 11:48:01 +0200912 first->total_len = total_len;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700913
914 return &first->txd;
915
916err_desc_get:
917 dwc_desc_put(dwc, first);
918 return NULL;
919}
920
Andy Shevchenko4d130de2014-08-19 20:29:16 +0300921bool dw_dma_filter(struct dma_chan *chan, void *param)
922{
923 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
924 struct dw_dma_slave *dws = param;
925
926 if (!dws || dws->dma_dev != chan->device->dev)
927 return false;
928
929 /* We have to copy data since dws can be temporary storage */
930
931 dwc->src_id = dws->src_id;
932 dwc->dst_id = dws->dst_id;
933
934 dwc->src_master = dws->src_master;
935 dwc->dst_master = dws->dst_master;
936
937 return true;
938}
939EXPORT_SYMBOL_GPL(dw_dma_filter);
940
Viresh Kumar327e6972012-02-01 16:12:26 +0530941/*
942 * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
943 * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
944 *
945 * NOTE: burst size 2 is not supported by controller.
946 *
947 * This can be done by finding least significant bit set: n & (n - 1)
948 */
949static inline void convert_burst(u32 *maxburst)
950{
951 if (*maxburst > 1)
952 *maxburst = fls(*maxburst) - 2;
953 else
954 *maxburst = 0;
955}
956
957static int
958set_runtime_config(struct dma_chan *chan, struct dma_slave_config *sconfig)
959{
960 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
961
Andy Shevchenko495aea42013-01-10 11:11:41 +0200962 /* Check if chan will be configured for slave transfers */
963 if (!is_slave_direction(sconfig->direction))
Viresh Kumar327e6972012-02-01 16:12:26 +0530964 return -EINVAL;
965
966 memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig));
Andy Shevchenko0fdb5672013-01-10 10:53:03 +0200967 dwc->direction = sconfig->direction;
Viresh Kumar327e6972012-02-01 16:12:26 +0530968
969 convert_burst(&dwc->dma_sconfig.src_maxburst);
970 convert_burst(&dwc->dma_sconfig.dst_maxburst);
971
972 return 0;
973}
974
Andy Shevchenko21fe3c52013-01-09 10:17:14 +0200975static inline void dwc_chan_pause(struct dw_dma_chan *dwc)
976{
977 u32 cfglo = channel_readl(dwc, CFG_LO);
Andy Shevchenko123b69a2013-03-21 11:49:17 +0200978 unsigned int count = 20; /* timeout iterations */
Andy Shevchenko21fe3c52013-01-09 10:17:14 +0200979
980 channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
Andy Shevchenko123b69a2013-03-21 11:49:17 +0200981 while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY) && count--)
982 udelay(2);
Andy Shevchenko21fe3c52013-01-09 10:17:14 +0200983
984 dwc->paused = true;
985}
986
987static inline void dwc_chan_resume(struct dw_dma_chan *dwc)
988{
989 u32 cfglo = channel_readl(dwc, CFG_LO);
990
991 channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
992
993 dwc->paused = false;
994}
995
Linus Walleij05827632010-05-17 16:30:42 -0700996static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd,
997 unsigned long arg)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -0700998{
999 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1000 struct dw_dma *dw = to_dw_dma(chan->device);
1001 struct dw_desc *desc, *_desc;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301002 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001003 LIST_HEAD(list);
1004
Linus Walleija7c57cf2011-04-19 08:31:32 +08001005 if (cmd == DMA_PAUSE) {
1006 spin_lock_irqsave(&dwc->lock, flags);
1007
Andy Shevchenko21fe3c52013-01-09 10:17:14 +02001008 dwc_chan_pause(dwc);
Linus Walleija7c57cf2011-04-19 08:31:32 +08001009
Linus Walleija7c57cf2011-04-19 08:31:32 +08001010 spin_unlock_irqrestore(&dwc->lock, flags);
1011 } else if (cmd == DMA_RESUME) {
1012 if (!dwc->paused)
1013 return 0;
1014
1015 spin_lock_irqsave(&dwc->lock, flags);
1016
Andy Shevchenko21fe3c52013-01-09 10:17:14 +02001017 dwc_chan_resume(dwc);
Linus Walleija7c57cf2011-04-19 08:31:32 +08001018
1019 spin_unlock_irqrestore(&dwc->lock, flags);
1020 } else if (cmd == DMA_TERMINATE_ALL) {
1021 spin_lock_irqsave(&dwc->lock, flags);
1022
Andy Shevchenkofed25742012-09-21 15:05:49 +03001023 clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags);
1024
Andy Shevchenko3f9362072012-06-19 13:46:32 +03001025 dwc_chan_disable(dw, dwc);
Linus Walleija7c57cf2011-04-19 08:31:32 +08001026
Heikki Krogerusa5dbff12013-01-10 10:53:06 +02001027 dwc_chan_resume(dwc);
Linus Walleija7c57cf2011-04-19 08:31:32 +08001028
1029 /* active_list entries will end up before queued entries */
1030 list_splice_init(&dwc->queue, &list);
1031 list_splice_init(&dwc->active_list, &list);
1032
1033 spin_unlock_irqrestore(&dwc->lock, flags);
1034
1035 /* Flush all pending and queued descriptors */
1036 list_for_each_entry_safe(desc, _desc, &list, desc_node)
1037 dwc_descriptor_complete(dwc, desc, false);
Viresh Kumar327e6972012-02-01 16:12:26 +05301038 } else if (cmd == DMA_SLAVE_CONFIG) {
1039 return set_runtime_config(chan, (struct dma_slave_config *)arg);
1040 } else {
Linus Walleijc3635c72010-03-26 16:44:01 -07001041 return -ENXIO;
Viresh Kumar327e6972012-02-01 16:12:26 +05301042 }
Linus Walleijc3635c72010-03-26 16:44:01 -07001043
Linus Walleijc3635c72010-03-26 16:44:01 -07001044 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001045}
1046
Andy Shevchenko4702d522013-01-25 11:48:03 +02001047static inline u32 dwc_get_residue(struct dw_dma_chan *dwc)
1048{
1049 unsigned long flags;
1050 u32 residue;
1051
1052 spin_lock_irqsave(&dwc->lock, flags);
1053
1054 residue = dwc->residue;
1055 if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags) && residue)
1056 residue -= dwc_get_sent(dwc);
1057
1058 spin_unlock_irqrestore(&dwc->lock, flags);
1059 return residue;
1060}
1061
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001062static enum dma_status
Linus Walleij07934482010-03-26 16:50:49 -07001063dwc_tx_status(struct dma_chan *chan,
1064 dma_cookie_t cookie,
1065 struct dma_tx_state *txstate)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001066{
1067 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001068 enum dma_status ret;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001069
Russell King - ARM Linux96a2af42012-03-06 22:35:27 +00001070 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul2c404102013-10-16 13:41:15 +05301071 if (ret == DMA_COMPLETE)
Andy Shevchenko12381dc2013-07-15 15:04:40 +03001072 return ret;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001073
Andy Shevchenko12381dc2013-07-15 15:04:40 +03001074 dwc_scan_descriptors(to_dw_dma(chan->device), dwc);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001075
Andy Shevchenko12381dc2013-07-15 15:04:40 +03001076 ret = dma_cookie_status(chan, cookie, txstate);
Vinod Koul2c404102013-10-16 13:41:15 +05301077 if (ret != DMA_COMPLETE)
Andy Shevchenko4702d522013-01-25 11:48:03 +02001078 dma_set_residue(txstate, dwc_get_residue(dwc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001079
Andy Shevchenkoeffd5cf2013-07-15 15:04:41 +03001080 if (dwc->paused && ret == DMA_IN_PROGRESS)
Linus Walleija7c57cf2011-04-19 08:31:32 +08001081 return DMA_PAUSED;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001082
1083 return ret;
1084}
1085
1086static void dwc_issue_pending(struct dma_chan *chan)
1087{
1088 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Andy Shevchenkodd8ecfca2014-06-18 12:15:38 +03001089 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001090
Andy Shevchenkodd8ecfca2014-06-18 12:15:38 +03001091 spin_lock_irqsave(&dwc->lock, flags);
1092 if (list_empty(&dwc->active_list))
1093 dwc_dostart_first_queued(dwc);
1094 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001095}
1096
Andy Shevchenko99d9bf42014-09-23 17:18:14 +03001097/*----------------------------------------------------------------------*/
1098
1099static void dw_dma_off(struct dw_dma *dw)
1100{
1101 int i;
1102
1103 dma_writel(dw, CFG, 0);
1104
1105 channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask);
1106 channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask);
1107 channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask);
1108 channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask);
1109
1110 while (dma_readl(dw, CFG) & DW_CFG_DMA_EN)
1111 cpu_relax();
1112
1113 for (i = 0; i < dw->dma.chancnt; i++)
1114 dw->chan[i].initialized = false;
1115}
1116
1117static void dw_dma_on(struct dw_dma *dw)
1118{
1119 dma_writel(dw, CFG, DW_CFG_DMA_EN);
1120}
1121
Dan Williamsaa1e6f12009-01-06 11:38:17 -07001122static int dwc_alloc_chan_resources(struct dma_chan *chan)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001123{
1124 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1125 struct dw_dma *dw = to_dw_dma(chan->device);
1126 struct dw_desc *desc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001127 int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301128 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001129
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001130 dev_vdbg(chan2dev(chan), "%s\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001131
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001132 /* ASSERT: channel is idle */
1133 if (dma_readl(dw, CH_EN) & dwc->mask) {
Dan Williams41d5e592009-01-06 11:38:21 -07001134 dev_dbg(chan2dev(chan), "DMA channel not idle?\n");
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001135 return -EIO;
1136 }
1137
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001138 dma_cookie_init(chan);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001139
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001140 /*
1141 * NOTE: some controllers may have additional features that we
1142 * need to initialize here, like "scatter-gather" (which
1143 * doesn't mean what you think it means), and status writeback.
1144 */
1145
Andy Shevchenko99d9bf42014-09-23 17:18:14 +03001146 /* Enable controller here if needed */
1147 if (!dw->in_use)
1148 dw_dma_on(dw);
1149 dw->in_use |= dwc->mask;
1150
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301151 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001152 i = dwc->descs_allocated;
1153 while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) {
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001154 dma_addr_t phys;
1155
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301156 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001157
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001158 desc = dma_pool_alloc(dw->desc_pool, GFP_ATOMIC, &phys);
Andy Shevchenkocbd65312013-01-09 10:17:11 +02001159 if (!desc)
1160 goto err_desc_alloc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001161
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001162 memset(desc, 0, sizeof(struct dw_desc));
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001163
Dan Williamse0bd0f82009-09-08 17:53:02 -07001164 INIT_LIST_HEAD(&desc->tx_list);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001165 dma_async_tx_descriptor_init(&desc->txd, chan);
1166 desc->txd.tx_submit = dwc_tx_submit;
1167 desc->txd.flags = DMA_CTRL_ACK;
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001168 desc->txd.phys = phys;
Andy Shevchenkocbd65312013-01-09 10:17:11 +02001169
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001170 dwc_desc_put(dwc, desc);
1171
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301172 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001173 i = ++dwc->descs_allocated;
1174 }
1175
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301176 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001177
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001178 dev_dbg(chan2dev(chan), "%s: allocated %d descriptors\n", __func__, i);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001179
1180 return i;
Andy Shevchenkocbd65312013-01-09 10:17:11 +02001181
1182err_desc_alloc:
Andy Shevchenkocbd65312013-01-09 10:17:11 +02001183 dev_info(chan2dev(chan), "only allocated %d descriptors\n", i);
1184
1185 return i;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001186}
1187
1188static void dwc_free_chan_resources(struct dma_chan *chan)
1189{
1190 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1191 struct dw_dma *dw = to_dw_dma(chan->device);
1192 struct dw_desc *desc, *_desc;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301193 unsigned long flags;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001194 LIST_HEAD(list);
1195
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001196 dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__,
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001197 dwc->descs_allocated);
1198
1199 /* ASSERT: channel is idle */
1200 BUG_ON(!list_empty(&dwc->active_list));
1201 BUG_ON(!list_empty(&dwc->queue));
1202 BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask);
1203
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301204 spin_lock_irqsave(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001205 list_splice_init(&dwc->free_list, &list);
1206 dwc->descs_allocated = 0;
Viresh Kumar61e183f2011-11-17 16:01:29 +05301207 dwc->initialized = false;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001208
1209 /* Disable interrupts */
1210 channel_clear_bit(dw, MASK.XFER, dwc->mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001211 channel_clear_bit(dw, MASK.ERROR, dwc->mask);
1212
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301213 spin_unlock_irqrestore(&dwc->lock, flags);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001214
Andy Shevchenko99d9bf42014-09-23 17:18:14 +03001215 /* Disable controller in case it was a last user */
1216 dw->in_use &= ~dwc->mask;
1217 if (!dw->in_use)
1218 dw_dma_off(dw);
1219
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001220 list_for_each_entry_safe(desc, _desc, &list, desc_node) {
Dan Williams41d5e592009-01-06 11:38:21 -07001221 dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc);
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001222 dma_pool_free(dw->desc_pool, desc, desc->txd.phys);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001223 }
1224
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001225 dev_vdbg(chan2dev(chan), "%s: done\n", __func__);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001226}
1227
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001228/* --------------------- Cyclic DMA API extensions -------------------- */
1229
1230/**
1231 * dw_dma_cyclic_start - start the cyclic DMA transfer
1232 * @chan: the DMA channel to start
1233 *
1234 * Must be called with soft interrupts disabled. Returns zero on success or
1235 * -errno on failure.
1236 */
1237int dw_dma_cyclic_start(struct dma_chan *chan)
1238{
1239 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1240 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301241 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001242
1243 if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) {
1244 dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n");
1245 return -ENODEV;
1246 }
1247
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301248 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001249
Andy Shevchenko75c61222013-03-26 16:53:54 +02001250 /* Assert channel is idle */
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001251 if (dma_readl(dw, CH_EN) & dwc->mask) {
1252 dev_err(chan2dev(&dwc->chan),
1253 "BUG: Attempted to start non-idle channel\n");
Andy Shevchenko1d455432012-06-19 13:34:03 +03001254 dwc_dump_chan_regs(dwc);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301255 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001256 return -EBUSY;
1257 }
1258
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001259 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1260 dma_writel(dw, CLEAR.XFER, dwc->mask);
1261
Andy Shevchenko75c61222013-03-26 16:53:54 +02001262 /* Setup DMAC channel registers */
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001263 channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys);
1264 channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN);
1265 channel_writel(dwc, CTL_HI, 0);
1266
1267 channel_set_bit(dw, CH_EN, dwc->mask);
1268
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301269 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001270
1271 return 0;
1272}
1273EXPORT_SYMBOL(dw_dma_cyclic_start);
1274
1275/**
1276 * dw_dma_cyclic_stop - stop the cyclic DMA transfer
1277 * @chan: the DMA channel to stop
1278 *
1279 * Must be called with soft interrupts disabled.
1280 */
1281void dw_dma_cyclic_stop(struct dma_chan *chan)
1282{
1283 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1284 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301285 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001286
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301287 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001288
Andy Shevchenko3f9362072012-06-19 13:46:32 +03001289 dwc_chan_disable(dw, dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001290
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301291 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001292}
1293EXPORT_SYMBOL(dw_dma_cyclic_stop);
1294
1295/**
1296 * dw_dma_cyclic_prep - prepare the cyclic DMA transfer
1297 * @chan: the DMA channel to prepare
1298 * @buf_addr: physical DMA address where the buffer starts
1299 * @buf_len: total number of bytes for the entire buffer
1300 * @period_len: number of bytes for each period
1301 * @direction: transfer direction, to or from device
1302 *
1303 * Must be called before trying to start the transfer. Returns a valid struct
1304 * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful.
1305 */
1306struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan,
1307 dma_addr_t buf_addr, size_t buf_len, size_t period_len,
Vinod Kouldb8196d2011-10-13 22:34:23 +05301308 enum dma_transfer_direction direction)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001309{
1310 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
Viresh Kumar327e6972012-02-01 16:12:26 +05301311 struct dma_slave_config *sconfig = &dwc->dma_sconfig;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001312 struct dw_cyclic_desc *cdesc;
1313 struct dw_cyclic_desc *retval = NULL;
1314 struct dw_desc *desc;
1315 struct dw_desc *last = NULL;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001316 unsigned long was_cyclic;
1317 unsigned int reg_width;
1318 unsigned int periods;
1319 unsigned int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301320 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001321
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301322 spin_lock_irqsave(&dwc->lock, flags);
Andy Shevchenkofed25742012-09-21 15:05:49 +03001323 if (dwc->nollp) {
1324 spin_unlock_irqrestore(&dwc->lock, flags);
1325 dev_dbg(chan2dev(&dwc->chan),
1326 "channel doesn't support LLP transfers\n");
1327 return ERR_PTR(-EINVAL);
1328 }
1329
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001330 if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) {
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301331 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001332 dev_dbg(chan2dev(&dwc->chan),
1333 "queue and/or active list are not empty\n");
1334 return ERR_PTR(-EBUSY);
1335 }
1336
1337 was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301338 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001339 if (was_cyclic) {
1340 dev_dbg(chan2dev(&dwc->chan),
1341 "channel already prepared for cyclic DMA\n");
1342 return ERR_PTR(-EBUSY);
1343 }
1344
1345 retval = ERR_PTR(-EINVAL);
Viresh Kumar327e6972012-02-01 16:12:26 +05301346
Andy Shevchenkof44b92f2013-01-10 10:52:58 +02001347 if (unlikely(!is_slave_direction(direction)))
1348 goto out_err;
1349
Andy Shevchenko0fdb5672013-01-10 10:53:03 +02001350 dwc->direction = direction;
1351
Viresh Kumar327e6972012-02-01 16:12:26 +05301352 if (direction == DMA_MEM_TO_DEV)
1353 reg_width = __ffs(sconfig->dst_addr_width);
1354 else
1355 reg_width = __ffs(sconfig->src_addr_width);
1356
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001357 periods = buf_len / period_len;
1358
1359 /* Check for too big/unaligned periods and unaligned DMA buffer. */
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001360 if (period_len > (dwc->block_size << reg_width))
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001361 goto out_err;
1362 if (unlikely(period_len & ((1 << reg_width) - 1)))
1363 goto out_err;
1364 if (unlikely(buf_addr & ((1 << reg_width) - 1)))
1365 goto out_err;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001366
1367 retval = ERR_PTR(-ENOMEM);
1368
1369 if (periods > NR_DESCS_PER_CHANNEL)
1370 goto out_err;
1371
1372 cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL);
1373 if (!cdesc)
1374 goto out_err;
1375
1376 cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL);
1377 if (!cdesc->desc)
1378 goto out_err_alloc;
1379
1380 for (i = 0; i < periods; i++) {
1381 desc = dwc_desc_get(dwc);
1382 if (!desc)
1383 goto out_err_desc_get;
1384
1385 switch (direction) {
Vinod Kouldb8196d2011-10-13 22:34:23 +05301386 case DMA_MEM_TO_DEV:
Viresh Kumar327e6972012-02-01 16:12:26 +05301387 desc->lli.dar = sconfig->dst_addr;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001388 desc->lli.sar = buf_addr + (period_len * i);
Viresh Kumar327e6972012-02-01 16:12:26 +05301389 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001390 | DWC_CTLL_DST_WIDTH(reg_width)
1391 | DWC_CTLL_SRC_WIDTH(reg_width)
1392 | DWC_CTLL_DST_FIX
1393 | DWC_CTLL_SRC_INC
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001394 | DWC_CTLL_INT_EN);
Viresh Kumar327e6972012-02-01 16:12:26 +05301395
1396 desc->lli.ctllo |= sconfig->device_fc ?
1397 DWC_CTLL_FC(DW_DMA_FC_P_M2P) :
1398 DWC_CTLL_FC(DW_DMA_FC_D_M2P);
1399
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001400 break;
Vinod Kouldb8196d2011-10-13 22:34:23 +05301401 case DMA_DEV_TO_MEM:
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001402 desc->lli.dar = buf_addr + (period_len * i);
Viresh Kumar327e6972012-02-01 16:12:26 +05301403 desc->lli.sar = sconfig->src_addr;
1404 desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001405 | DWC_CTLL_SRC_WIDTH(reg_width)
1406 | DWC_CTLL_DST_WIDTH(reg_width)
1407 | DWC_CTLL_DST_INC
1408 | DWC_CTLL_SRC_FIX
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001409 | DWC_CTLL_INT_EN);
Viresh Kumar327e6972012-02-01 16:12:26 +05301410
1411 desc->lli.ctllo |= sconfig->device_fc ?
1412 DWC_CTLL_FC(DW_DMA_FC_P_P2M) :
1413 DWC_CTLL_FC(DW_DMA_FC_D_P2M);
1414
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001415 break;
1416 default:
1417 break;
1418 }
1419
1420 desc->lli.ctlhi = (period_len >> reg_width);
1421 cdesc->desc[i] = desc;
1422
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001423 if (last)
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001424 last->lli.llp = desc->txd.phys;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001425
1426 last = desc;
1427 }
1428
Andy Shevchenko75c61222013-03-26 16:53:54 +02001429 /* Let's make a cyclic list */
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001430 last->lli.llp = cdesc->desc[0]->txd.phys;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001431
Andy Shevchenko5a87f0e2014-01-13 14:04:50 +02001432 dev_dbg(chan2dev(&dwc->chan),
1433 "cyclic prepared buf %pad len %zu period %zu periods %d\n",
1434 &buf_addr, buf_len, period_len, periods);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001435
1436 cdesc->periods = periods;
1437 dwc->cdesc = cdesc;
1438
1439 return cdesc;
1440
1441out_err_desc_get:
1442 while (i--)
1443 dwc_desc_put(dwc, cdesc->desc[i]);
1444out_err_alloc:
1445 kfree(cdesc);
1446out_err:
1447 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1448 return (struct dw_cyclic_desc *)retval;
1449}
1450EXPORT_SYMBOL(dw_dma_cyclic_prep);
1451
1452/**
1453 * dw_dma_cyclic_free - free a prepared cyclic DMA transfer
1454 * @chan: the DMA channel to free
1455 */
1456void dw_dma_cyclic_free(struct dma_chan *chan)
1457{
1458 struct dw_dma_chan *dwc = to_dw_dma_chan(chan);
1459 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
1460 struct dw_cyclic_desc *cdesc = dwc->cdesc;
1461 int i;
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301462 unsigned long flags;
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001463
Andy Shevchenko2e4c3642012-06-19 13:34:05 +03001464 dev_dbg(chan2dev(&dwc->chan), "%s\n", __func__);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001465
1466 if (!cdesc)
1467 return;
1468
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301469 spin_lock_irqsave(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001470
Andy Shevchenko3f9362072012-06-19 13:46:32 +03001471 dwc_chan_disable(dw, dwc);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001472
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001473 dma_writel(dw, CLEAR.ERROR, dwc->mask);
1474 dma_writel(dw, CLEAR.XFER, dwc->mask);
1475
Viresh Kumar69cea5a2011-04-15 16:03:35 +05301476 spin_unlock_irqrestore(&dwc->lock, flags);
Hans-Christian Egtvedtd9de4512009-04-01 15:47:02 +02001477
1478 for (i = 0; i < cdesc->periods; i++)
1479 dwc_desc_put(dwc, cdesc->desc[i]);
1480
1481 kfree(cdesc->desc);
1482 kfree(cdesc);
1483
1484 clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags);
1485}
1486EXPORT_SYMBOL(dw_dma_cyclic_free);
1487
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001488/*----------------------------------------------------------------------*/
1489
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001490int dw_dma_probe(struct dw_dma_chip *chip, struct dw_dma_platform_data *pdata)
Viresh Kumara9ddb572012-10-16 09:49:17 +05301491{
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001492 struct dw_dma *dw;
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001493 bool autocfg;
1494 unsigned int dw_params;
1495 unsigned int nr_channels;
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001496 unsigned int max_blk_size = 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001497 int err;
1498 int i;
1499
Andy Shevchenko000871c2014-03-05 15:48:12 +02001500 dw = devm_kzalloc(chip->dev, sizeof(*dw), GFP_KERNEL);
1501 if (!dw)
1502 return -ENOMEM;
1503
1504 dw->regs = chip->regs;
1505 chip->dw = dw;
1506
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001507 dw_params = dma_read_byaddr(chip->regs, DW_PARAMS);
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001508 autocfg = dw_params >> DW_PARAMS_EN & 0x1;
1509
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001510 dev_dbg(chip->dev, "DW_PARAMS: 0x%08x\n", dw_params);
Andy Shevchenko123de542013-01-09 10:17:01 +02001511
1512 if (!pdata && autocfg) {
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001513 pdata = devm_kzalloc(chip->dev, sizeof(*pdata), GFP_KERNEL);
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001514 if (!pdata) {
1515 err = -ENOMEM;
1516 goto err_pdata;
1517 }
Andy Shevchenko123de542013-01-09 10:17:01 +02001518
1519 /* Fill platform data with the default values */
1520 pdata->is_private = true;
1521 pdata->chan_allocation_order = CHAN_ALLOCATION_ASCENDING;
1522 pdata->chan_priority = CHAN_PRIORITY_ASCENDING;
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001523 } else if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS) {
1524 err = -EINVAL;
1525 goto err_pdata;
1526 }
Andy Shevchenko123de542013-01-09 10:17:01 +02001527
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001528 if (autocfg)
1529 nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 0x7) + 1;
1530 else
1531 nr_channels = pdata->nr_channels;
1532
Andy Shevchenko000871c2014-03-05 15:48:12 +02001533 dw->chan = devm_kcalloc(chip->dev, nr_channels, sizeof(*dw->chan),
1534 GFP_KERNEL);
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001535 if (!dw->chan) {
1536 err = -ENOMEM;
1537 goto err_pdata;
1538 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001539
Andy Shevchenko75c61222013-03-26 16:53:54 +02001540 /* Get hardware configuration parameters */
Andy Shevchenkoa0982002012-09-21 15:05:48 +03001541 if (autocfg) {
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001542 max_blk_size = dma_readl(dw, MAX_BLK_SIZE);
1543
Andy Shevchenkoa0982002012-09-21 15:05:48 +03001544 dw->nr_masters = (dw_params >> DW_PARAMS_NR_MASTER & 3) + 1;
1545 for (i = 0; i < dw->nr_masters; i++) {
1546 dw->data_width[i] =
1547 (dw_params >> DW_PARAMS_DATA_WIDTH(i) & 3) + 2;
1548 }
1549 } else {
1550 dw->nr_masters = pdata->nr_masters;
1551 memcpy(dw->data_width, pdata->data_width, 4);
1552 }
1553
Andy Shevchenko11f932e2012-06-19 13:34:06 +03001554 /* Calculate all channel mask before DMA setup */
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001555 dw->all_chan_mask = (1 << nr_channels) - 1;
Andy Shevchenko11f932e2012-06-19 13:34:06 +03001556
Andy Shevchenko75c61222013-03-26 16:53:54 +02001557 /* Force dma off, just in case */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001558 dw_dma_off(dw);
1559
Andy Shevchenko75c61222013-03-26 16:53:54 +02001560 /* Disable BLOCK interrupts as well */
Andy Shevchenko236b1062012-06-19 13:34:07 +03001561 channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask);
1562
Andy Shevchenko75c61222013-03-26 16:53:54 +02001563 /* Create a pool of consistent memory blocks for hardware descriptors */
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001564 dw->desc_pool = dmam_pool_create("dw_dmac_desc_pool", chip->dev,
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001565 sizeof(struct dw_desc), 4, 0);
1566 if (!dw->desc_pool) {
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001567 dev_err(chip->dev, "No memory for descriptors dma pool\n");
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001568 err = -ENOMEM;
1569 goto err_pdata;
Andy Shevchenkof8122a82013-01-16 15:48:50 +02001570 }
1571
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001572 tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw);
1573
Andy Shevchenko97977f72014-05-07 10:56:24 +03001574 err = request_irq(chip->irq, dw_dma_interrupt, IRQF_SHARED,
1575 "dw_dmac", dw);
1576 if (err)
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001577 goto err_pdata;
Andy Shevchenko97977f72014-05-07 10:56:24 +03001578
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001579 INIT_LIST_HEAD(&dw->dma.channels);
Andy Shevchenko482c67e2012-09-21 15:05:46 +03001580 for (i = 0; i < nr_channels; i++) {
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001581 struct dw_dma_chan *dwc = &dw->chan[i];
Andy Shevchenkofed25742012-09-21 15:05:49 +03001582 int r = nr_channels - i - 1;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001583
1584 dwc->chan.device = &dw->dma;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +00001585 dma_cookie_init(&dwc->chan);
Viresh Kumarb0c31302011-03-03 15:47:21 +05301586 if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING)
1587 list_add_tail(&dwc->chan.device_node,
1588 &dw->dma.channels);
1589 else
1590 list_add(&dwc->chan.device_node, &dw->dma.channels);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001591
Viresh Kumar93317e82011-03-03 15:47:22 +05301592 /* 7 is highest priority & 0 is lowest. */
1593 if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING)
Andy Shevchenkofed25742012-09-21 15:05:49 +03001594 dwc->priority = r;
Viresh Kumar93317e82011-03-03 15:47:22 +05301595 else
1596 dwc->priority = i;
1597
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001598 dwc->ch_regs = &__dw_regs(dw)->CHAN[i];
1599 spin_lock_init(&dwc->lock);
1600 dwc->mask = 1 << i;
1601
1602 INIT_LIST_HEAD(&dwc->active_list);
1603 INIT_LIST_HEAD(&dwc->queue);
1604 INIT_LIST_HEAD(&dwc->free_list);
1605
1606 channel_clear_bit(dw, CH_EN, dwc->mask);
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001607
Andy Shevchenko0fdb5672013-01-10 10:53:03 +02001608 dwc->direction = DMA_TRANS_NONE;
Andy Shevchenkoa0982002012-09-21 15:05:48 +03001609
Andy Shevchenko75c61222013-03-26 16:53:54 +02001610 /* Hardware configuration */
Andy Shevchenkofed25742012-09-21 15:05:49 +03001611 if (autocfg) {
1612 unsigned int dwc_params;
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001613 void __iomem *addr = chip->regs + r * sizeof(u32);
Andy Shevchenkofed25742012-09-21 15:05:49 +03001614
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001615 dwc_params = dma_read_byaddr(addr, DWC_PARAMS);
Andy Shevchenkofed25742012-09-21 15:05:49 +03001616
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001617 dev_dbg(chip->dev, "DWC_PARAMS[%d]: 0x%08x\n", i,
1618 dwc_params);
Andy Shevchenko985a6c72013-01-18 17:10:59 +02001619
Andy Shevchenko1d566f12014-01-13 14:04:48 +02001620 /*
1621 * Decode maximum block size for given channel. The
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001622 * stored 4 bit value represents blocks from 0x00 for 3
Andy Shevchenko1d566f12014-01-13 14:04:48 +02001623 * up to 0x0a for 4095.
1624 */
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001625 dwc->block_size =
1626 (4 << ((max_blk_size >> 4 * i) & 0xf)) - 1;
Andy Shevchenkofed25742012-09-21 15:05:49 +03001627 dwc->nollp =
1628 (dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0;
1629 } else {
Andy Shevchenko4a63a8b2012-09-21 15:05:47 +03001630 dwc->block_size = pdata->block_size;
Andy Shevchenkofed25742012-09-21 15:05:49 +03001631
1632 /* Check if channel supports multi block transfer */
1633 channel_writel(dwc, LLP, 0xfffffffc);
1634 dwc->nollp =
1635 (channel_readl(dwc, LLP) & 0xfffffffc) == 0;
1636 channel_writel(dwc, LLP, 0);
1637 }
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001638 }
1639
Andy Shevchenko11f932e2012-06-19 13:34:06 +03001640 /* Clear all interrupts on all channels. */
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001641 dma_writel(dw, CLEAR.XFER, dw->all_chan_mask);
Andy Shevchenko236b1062012-06-19 13:34:07 +03001642 dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001643 dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask);
1644 dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask);
1645 dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask);
1646
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001647 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
1648 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
Jamie Iles95ea7592011-01-21 14:11:54 +00001649 if (pdata->is_private)
1650 dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask);
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001651 dw->dma.dev = chip->dev;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001652 dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources;
1653 dw->dma.device_free_chan_resources = dwc_free_chan_resources;
1654
1655 dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy;
1656
1657 dw->dma.device_prep_slave_sg = dwc_prep_slave_sg;
Linus Walleijc3635c72010-03-26 16:44:01 -07001658 dw->dma.device_control = dwc_control;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001659
Linus Walleij07934482010-03-26 16:50:49 -07001660 dw->dma.device_tx_status = dwc_tx_status;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001661 dw->dma.device_issue_pending = dwc_issue_pending;
1662
Andy Shevchenko12229342014-05-08 12:01:50 +03001663 err = dma_async_device_register(&dw->dma);
1664 if (err)
1665 goto err_dma_register;
1666
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001667 dev_info(chip->dev, "DesignWare DMA Controller, %d channels\n",
Andy Shevchenko21d43f42012-10-18 17:34:09 +03001668 nr_channels);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001669
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001670 return 0;
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001671
Andy Shevchenko12229342014-05-08 12:01:50 +03001672err_dma_register:
1673 free_irq(chip->irq, dw);
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001674err_pdata:
Andy Shevchenko8be4f522014-05-08 12:01:49 +03001675 return err;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001676}
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001677EXPORT_SYMBOL_GPL(dw_dma_probe);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001678
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001679int dw_dma_remove(struct dw_dma_chip *chip)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001680{
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001681 struct dw_dma *dw = chip->dw;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001682 struct dw_dma_chan *dwc, *_dwc;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001683
1684 dw_dma_off(dw);
1685 dma_async_device_unregister(&dw->dma);
1686
Andy Shevchenko97977f72014-05-07 10:56:24 +03001687 free_irq(chip->irq, dw);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001688 tasklet_kill(&dw->tasklet);
1689
1690 list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels,
1691 chan.device_node) {
1692 list_del(&dwc->chan.device_node);
1693 channel_clear_bit(dw, CH_EN, dwc->mask);
1694 }
1695
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001696 return 0;
1697}
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001698EXPORT_SYMBOL_GPL(dw_dma_remove);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001699
Andy Shevchenko2540f742014-09-23 17:18:13 +03001700int dw_dma_disable(struct dw_dma_chip *chip)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001701{
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001702 struct dw_dma *dw = chip->dw;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001703
Andy Shevchenko6168d562012-10-18 17:34:10 +03001704 dw_dma_off(dw);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001705 return 0;
1706}
Andy Shevchenko2540f742014-09-23 17:18:13 +03001707EXPORT_SYMBOL_GPL(dw_dma_disable);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001708
Andy Shevchenko2540f742014-09-23 17:18:13 +03001709int dw_dma_enable(struct dw_dma_chip *chip)
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001710{
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001711 struct dw_dma *dw = chip->dw;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001712
Andy Shevchenko7a83c042014-09-23 17:18:12 +03001713 dw_dma_on(dw);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001714 return 0;
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001715}
Andy Shevchenko2540f742014-09-23 17:18:13 +03001716EXPORT_SYMBOL_GPL(dw_dma_enable);
Haavard Skinnemoen3bfb1d22008-07-08 11:59:42 -07001717
1718MODULE_LICENSE("GPL v2");
Andy Shevchenko9cade1a2013-06-05 15:26:45 +03001719MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller core driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001720MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Viresh Kumar10d89352012-06-20 12:53:02 -07001721MODULE_AUTHOR("Viresh Kumar <viresh.linux@gmail.com>");