Commit | Line | Data |
---|---|---|
3bfb1d20 HS |
1 | /* |
2 | * Driver for the Synopsys DesignWare DMA Controller (aka DMACA on | |
3 | * AVR32 systems.) | |
4 | * | |
5 | * Copyright (C) 2007-2008 Atmel Corporation | |
aecb7b64 | 6 | * Copyright (C) 2010-2011 ST Microelectronics |
3bfb1d20 HS |
7 | * |
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 | */ | |
12 | #include <linux/clk.h> | |
13 | #include <linux/delay.h> | |
14 | #include <linux/dmaengine.h> | |
15 | #include <linux/dma-mapping.h> | |
16 | #include <linux/init.h> | |
17 | #include <linux/interrupt.h> | |
18 | #include <linux/io.h> | |
19 | #include <linux/mm.h> | |
20 | #include <linux/module.h> | |
21 | #include <linux/platform_device.h> | |
22 | #include <linux/slab.h> | |
23 | ||
24 | #include "dw_dmac_regs.h" | |
25 | ||
26 | /* | |
27 | * This supports the Synopsys "DesignWare AHB Central DMA Controller", | |
28 | * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all | |
29 | * of which use ARM any more). See the "Databook" from Synopsys for | |
30 | * information beyond what licensees probably provide. | |
31 | * | |
32 | * The driver has currently been tested only with the Atmel AT32AP7000, | |
33 | * which does not support descriptor writeback. | |
34 | */ | |
35 | ||
f301c062 JI |
36 | #define DWC_DEFAULT_CTLLO(private) ({ \ |
37 | struct dw_dma_slave *__slave = (private); \ | |
38 | int dms = __slave ? __slave->dst_master : 0; \ | |
39 | int sms = __slave ? __slave->src_master : 1; \ | |
e51dc53b VK |
40 | u8 smsize = __slave ? __slave->src_msize : DW_DMA_MSIZE_16; \ |
41 | u8 dmsize = __slave ? __slave->dst_msize : DW_DMA_MSIZE_16; \ | |
f301c062 | 42 | \ |
ee66509d VK |
43 | (DWC_CTLL_DST_MSIZE(dmsize) \ |
44 | | DWC_CTLL_SRC_MSIZE(smsize) \ | |
f301c062 JI |
45 | | DWC_CTLL_LLP_D_EN \ |
46 | | DWC_CTLL_LLP_S_EN \ | |
47 | | DWC_CTLL_DMS(dms) \ | |
48 | | DWC_CTLL_SMS(sms)); \ | |
49 | }) | |
3bfb1d20 HS |
50 | |
51 | /* | |
52 | * This is configuration-dependent and usually a funny size like 4095. | |
3bfb1d20 HS |
53 | * |
54 | * Note that this is a transfer count, i.e. if we transfer 32-bit | |
418e7407 | 55 | * words, we can do 16380 bytes per descriptor. |
3bfb1d20 HS |
56 | * |
57 | * This parameter is also system-specific. | |
58 | */ | |
418e7407 | 59 | #define DWC_MAX_COUNT 4095U |
3bfb1d20 HS |
60 | |
61 | /* | |
62 | * Number of descriptors to allocate for each channel. This should be | |
63 | * made configurable somehow; preferably, the clients (at least the | |
64 | * ones using slave transfers) should be able to give us a hint. | |
65 | */ | |
66 | #define NR_DESCS_PER_CHANNEL 64 | |
67 | ||
68 | /*----------------------------------------------------------------------*/ | |
69 | ||
70 | /* | |
71 | * Because we're not relying on writeback from the controller (it may not | |
72 | * even be configured into the core!) we don't need to use dma_pool. These | |
73 | * descriptors -- and associated data -- are cacheable. We do need to make | |
74 | * sure their dcache entries are written back before handing them off to | |
75 | * the controller, though. | |
76 | */ | |
77 | ||
41d5e59c DW |
78 | static struct device *chan2dev(struct dma_chan *chan) |
79 | { | |
80 | return &chan->dev->device; | |
81 | } | |
82 | static struct device *chan2parent(struct dma_chan *chan) | |
83 | { | |
84 | return chan->dev->device.parent; | |
85 | } | |
86 | ||
3bfb1d20 HS |
87 | static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc) |
88 | { | |
89 | return list_entry(dwc->active_list.next, struct dw_desc, desc_node); | |
90 | } | |
91 | ||
3bfb1d20 HS |
92 | static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc) |
93 | { | |
94 | struct dw_desc *desc, *_desc; | |
95 | struct dw_desc *ret = NULL; | |
96 | unsigned int i = 0; | |
69cea5a0 | 97 | unsigned long flags; |
3bfb1d20 | 98 | |
69cea5a0 | 99 | spin_lock_irqsave(&dwc->lock, flags); |
3bfb1d20 HS |
100 | list_for_each_entry_safe(desc, _desc, &dwc->free_list, desc_node) { |
101 | if (async_tx_test_ack(&desc->txd)) { | |
102 | list_del(&desc->desc_node); | |
103 | ret = desc; | |
104 | break; | |
105 | } | |
41d5e59c | 106 | dev_dbg(chan2dev(&dwc->chan), "desc %p not ACKed\n", desc); |
3bfb1d20 HS |
107 | i++; |
108 | } | |
69cea5a0 | 109 | spin_unlock_irqrestore(&dwc->lock, flags); |
3bfb1d20 | 110 | |
41d5e59c | 111 | dev_vdbg(chan2dev(&dwc->chan), "scanned %u descriptors on freelist\n", i); |
3bfb1d20 HS |
112 | |
113 | return ret; | |
114 | } | |
115 | ||
116 | static void dwc_sync_desc_for_cpu(struct dw_dma_chan *dwc, struct dw_desc *desc) | |
117 | { | |
118 | struct dw_desc *child; | |
119 | ||
e0bd0f8c | 120 | list_for_each_entry(child, &desc->tx_list, desc_node) |
41d5e59c | 121 | dma_sync_single_for_cpu(chan2parent(&dwc->chan), |
3bfb1d20 HS |
122 | child->txd.phys, sizeof(child->lli), |
123 | DMA_TO_DEVICE); | |
41d5e59c | 124 | dma_sync_single_for_cpu(chan2parent(&dwc->chan), |
3bfb1d20 HS |
125 | desc->txd.phys, sizeof(desc->lli), |
126 | DMA_TO_DEVICE); | |
127 | } | |
128 | ||
129 | /* | |
130 | * Move a descriptor, including any children, to the free list. | |
131 | * `desc' must not be on any lists. | |
132 | */ | |
133 | static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc) | |
134 | { | |
69cea5a0 VK |
135 | unsigned long flags; |
136 | ||
3bfb1d20 HS |
137 | if (desc) { |
138 | struct dw_desc *child; | |
139 | ||
140 | dwc_sync_desc_for_cpu(dwc, desc); | |
141 | ||
69cea5a0 | 142 | spin_lock_irqsave(&dwc->lock, flags); |
e0bd0f8c | 143 | list_for_each_entry(child, &desc->tx_list, desc_node) |
41d5e59c | 144 | dev_vdbg(chan2dev(&dwc->chan), |
3bfb1d20 HS |
145 | "moving child desc %p to freelist\n", |
146 | child); | |
e0bd0f8c | 147 | list_splice_init(&desc->tx_list, &dwc->free_list); |
41d5e59c | 148 | dev_vdbg(chan2dev(&dwc->chan), "moving desc %p to freelist\n", desc); |
3bfb1d20 | 149 | list_add(&desc->desc_node, &dwc->free_list); |
69cea5a0 | 150 | spin_unlock_irqrestore(&dwc->lock, flags); |
3bfb1d20 HS |
151 | } |
152 | } | |
153 | ||
154 | /* Called with dwc->lock held and bh disabled */ | |
155 | static dma_cookie_t | |
156 | dwc_assign_cookie(struct dw_dma_chan *dwc, struct dw_desc *desc) | |
157 | { | |
158 | dma_cookie_t cookie = dwc->chan.cookie; | |
159 | ||
160 | if (++cookie < 0) | |
161 | cookie = 1; | |
162 | ||
163 | dwc->chan.cookie = cookie; | |
164 | desc->txd.cookie = cookie; | |
165 | ||
166 | return cookie; | |
167 | } | |
168 | ||
61e183f8 VK |
169 | static void dwc_initialize(struct dw_dma_chan *dwc) |
170 | { | |
171 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); | |
172 | struct dw_dma_slave *dws = dwc->chan.private; | |
173 | u32 cfghi = DWC_CFGH_FIFO_MODE; | |
174 | u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority); | |
175 | ||
176 | if (dwc->initialized == true) | |
177 | return; | |
178 | ||
179 | if (dws) { | |
180 | /* | |
181 | * We need controller-specific data to set up slave | |
182 | * transfers. | |
183 | */ | |
184 | BUG_ON(!dws->dma_dev || dws->dma_dev != dw->dma.dev); | |
185 | ||
186 | cfghi = dws->cfg_hi; | |
187 | cfglo |= dws->cfg_lo & ~DWC_CFGL_CH_PRIOR_MASK; | |
188 | } | |
189 | ||
190 | channel_writel(dwc, CFG_LO, cfglo); | |
191 | channel_writel(dwc, CFG_HI, cfghi); | |
192 | ||
193 | /* Enable interrupts */ | |
194 | channel_set_bit(dw, MASK.XFER, dwc->mask); | |
61e183f8 VK |
195 | channel_set_bit(dw, MASK.ERROR, dwc->mask); |
196 | ||
197 | dwc->initialized = true; | |
198 | } | |
199 | ||
3bfb1d20 HS |
200 | /*----------------------------------------------------------------------*/ |
201 | ||
202 | /* Called with dwc->lock held and bh disabled */ | |
203 | static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first) | |
204 | { | |
205 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); | |
206 | ||
207 | /* ASSERT: channel is idle */ | |
208 | if (dma_readl(dw, CH_EN) & dwc->mask) { | |
41d5e59c | 209 | dev_err(chan2dev(&dwc->chan), |
3bfb1d20 | 210 | "BUG: Attempted to start non-idle channel\n"); |
41d5e59c | 211 | dev_err(chan2dev(&dwc->chan), |
3bfb1d20 HS |
212 | " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n", |
213 | channel_readl(dwc, SAR), | |
214 | channel_readl(dwc, DAR), | |
215 | channel_readl(dwc, LLP), | |
216 | channel_readl(dwc, CTL_HI), | |
217 | channel_readl(dwc, CTL_LO)); | |
218 | ||
219 | /* The tasklet will hopefully advance the queue... */ | |
220 | return; | |
221 | } | |
222 | ||
61e183f8 VK |
223 | dwc_initialize(dwc); |
224 | ||
3bfb1d20 HS |
225 | channel_writel(dwc, LLP, first->txd.phys); |
226 | channel_writel(dwc, CTL_LO, | |
227 | DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN); | |
228 | channel_writel(dwc, CTL_HI, 0); | |
229 | channel_set_bit(dw, CH_EN, dwc->mask); | |
230 | } | |
231 | ||
232 | /*----------------------------------------------------------------------*/ | |
233 | ||
234 | static void | |
5fedefb8 VK |
235 | dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc, |
236 | bool callback_required) | |
3bfb1d20 | 237 | { |
5fedefb8 VK |
238 | dma_async_tx_callback callback = NULL; |
239 | void *param = NULL; | |
3bfb1d20 | 240 | struct dma_async_tx_descriptor *txd = &desc->txd; |
e518076e | 241 | struct dw_desc *child; |
69cea5a0 | 242 | unsigned long flags; |
3bfb1d20 | 243 | |
41d5e59c | 244 | dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie); |
3bfb1d20 | 245 | |
69cea5a0 | 246 | spin_lock_irqsave(&dwc->lock, flags); |
3bfb1d20 | 247 | dwc->completed = txd->cookie; |
5fedefb8 VK |
248 | if (callback_required) { |
249 | callback = txd->callback; | |
250 | param = txd->callback_param; | |
251 | } | |
3bfb1d20 HS |
252 | |
253 | dwc_sync_desc_for_cpu(dwc, desc); | |
e518076e VK |
254 | |
255 | /* async_tx_ack */ | |
256 | list_for_each_entry(child, &desc->tx_list, desc_node) | |
257 | async_tx_ack(&child->txd); | |
258 | async_tx_ack(&desc->txd); | |
259 | ||
e0bd0f8c | 260 | list_splice_init(&desc->tx_list, &dwc->free_list); |
3bfb1d20 HS |
261 | list_move(&desc->desc_node, &dwc->free_list); |
262 | ||
657a77fa AN |
263 | if (!dwc->chan.private) { |
264 | struct device *parent = chan2parent(&dwc->chan); | |
265 | if (!(txd->flags & DMA_COMPL_SKIP_DEST_UNMAP)) { | |
266 | if (txd->flags & DMA_COMPL_DEST_UNMAP_SINGLE) | |
267 | dma_unmap_single(parent, desc->lli.dar, | |
268 | desc->len, DMA_FROM_DEVICE); | |
269 | else | |
270 | dma_unmap_page(parent, desc->lli.dar, | |
271 | desc->len, DMA_FROM_DEVICE); | |
272 | } | |
273 | if (!(txd->flags & DMA_COMPL_SKIP_SRC_UNMAP)) { | |
274 | if (txd->flags & DMA_COMPL_SRC_UNMAP_SINGLE) | |
275 | dma_unmap_single(parent, desc->lli.sar, | |
276 | desc->len, DMA_TO_DEVICE); | |
277 | else | |
278 | dma_unmap_page(parent, desc->lli.sar, | |
279 | desc->len, DMA_TO_DEVICE); | |
280 | } | |
281 | } | |
3bfb1d20 | 282 | |
69cea5a0 VK |
283 | spin_unlock_irqrestore(&dwc->lock, flags); |
284 | ||
5fedefb8 | 285 | if (callback_required && callback) |
3bfb1d20 HS |
286 | callback(param); |
287 | } | |
288 | ||
289 | static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc) | |
290 | { | |
291 | struct dw_desc *desc, *_desc; | |
292 | LIST_HEAD(list); | |
69cea5a0 | 293 | unsigned long flags; |
3bfb1d20 | 294 | |
69cea5a0 | 295 | spin_lock_irqsave(&dwc->lock, flags); |
3bfb1d20 | 296 | if (dma_readl(dw, CH_EN) & dwc->mask) { |
41d5e59c | 297 | dev_err(chan2dev(&dwc->chan), |
3bfb1d20 HS |
298 | "BUG: XFER bit set, but channel not idle!\n"); |
299 | ||
300 | /* Try to continue after resetting the channel... */ | |
301 | channel_clear_bit(dw, CH_EN, dwc->mask); | |
302 | while (dma_readl(dw, CH_EN) & dwc->mask) | |
303 | cpu_relax(); | |
304 | } | |
305 | ||
306 | /* | |
307 | * Submit queued descriptors ASAP, i.e. before we go through | |
308 | * the completed ones. | |
309 | */ | |
3bfb1d20 | 310 | list_splice_init(&dwc->active_list, &list); |
f336e42f VK |
311 | if (!list_empty(&dwc->queue)) { |
312 | list_move(dwc->queue.next, &dwc->active_list); | |
313 | dwc_dostart(dwc, dwc_first_active(dwc)); | |
314 | } | |
3bfb1d20 | 315 | |
69cea5a0 VK |
316 | spin_unlock_irqrestore(&dwc->lock, flags); |
317 | ||
3bfb1d20 | 318 | list_for_each_entry_safe(desc, _desc, &list, desc_node) |
5fedefb8 | 319 | dwc_descriptor_complete(dwc, desc, true); |
3bfb1d20 HS |
320 | } |
321 | ||
322 | static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc) | |
323 | { | |
324 | dma_addr_t llp; | |
325 | struct dw_desc *desc, *_desc; | |
326 | struct dw_desc *child; | |
327 | u32 status_xfer; | |
69cea5a0 | 328 | unsigned long flags; |
3bfb1d20 | 329 | |
69cea5a0 | 330 | spin_lock_irqsave(&dwc->lock, flags); |
3bfb1d20 HS |
331 | llp = channel_readl(dwc, LLP); |
332 | status_xfer = dma_readl(dw, RAW.XFER); | |
333 | ||
334 | if (status_xfer & dwc->mask) { | |
335 | /* Everything we've submitted is done */ | |
336 | dma_writel(dw, CLEAR.XFER, dwc->mask); | |
69cea5a0 VK |
337 | spin_unlock_irqrestore(&dwc->lock, flags); |
338 | ||
3bfb1d20 HS |
339 | dwc_complete_all(dw, dwc); |
340 | return; | |
341 | } | |
342 | ||
69cea5a0 VK |
343 | if (list_empty(&dwc->active_list)) { |
344 | spin_unlock_irqrestore(&dwc->lock, flags); | |
087809fc | 345 | return; |
69cea5a0 | 346 | } |
087809fc | 347 | |
41d5e59c | 348 | dev_vdbg(chan2dev(&dwc->chan), "scan_descriptors: llp=0x%x\n", llp); |
3bfb1d20 HS |
349 | |
350 | list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) { | |
84adccfb | 351 | /* check first descriptors addr */ |
69cea5a0 VK |
352 | if (desc->txd.phys == llp) { |
353 | spin_unlock_irqrestore(&dwc->lock, flags); | |
84adccfb | 354 | return; |
69cea5a0 | 355 | } |
84adccfb VK |
356 | |
357 | /* check first descriptors llp */ | |
69cea5a0 | 358 | if (desc->lli.llp == llp) { |
3bfb1d20 | 359 | /* This one is currently in progress */ |
69cea5a0 | 360 | spin_unlock_irqrestore(&dwc->lock, flags); |
3bfb1d20 | 361 | return; |
69cea5a0 | 362 | } |
3bfb1d20 | 363 | |
e0bd0f8c | 364 | list_for_each_entry(child, &desc->tx_list, desc_node) |
69cea5a0 | 365 | if (child->lli.llp == llp) { |
3bfb1d20 | 366 | /* Currently in progress */ |
69cea5a0 | 367 | spin_unlock_irqrestore(&dwc->lock, flags); |
3bfb1d20 | 368 | return; |
69cea5a0 | 369 | } |
3bfb1d20 HS |
370 | |
371 | /* | |
372 | * No descriptors so far seem to be in progress, i.e. | |
373 | * this one must be done. | |
374 | */ | |
69cea5a0 | 375 | spin_unlock_irqrestore(&dwc->lock, flags); |
5fedefb8 | 376 | dwc_descriptor_complete(dwc, desc, true); |
69cea5a0 | 377 | spin_lock_irqsave(&dwc->lock, flags); |
3bfb1d20 HS |
378 | } |
379 | ||
41d5e59c | 380 | dev_err(chan2dev(&dwc->chan), |
3bfb1d20 HS |
381 | "BUG: All descriptors done, but channel not idle!\n"); |
382 | ||
383 | /* Try to continue after resetting the channel... */ | |
384 | channel_clear_bit(dw, CH_EN, dwc->mask); | |
385 | while (dma_readl(dw, CH_EN) & dwc->mask) | |
386 | cpu_relax(); | |
387 | ||
388 | if (!list_empty(&dwc->queue)) { | |
f336e42f VK |
389 | list_move(dwc->queue.next, &dwc->active_list); |
390 | dwc_dostart(dwc, dwc_first_active(dwc)); | |
3bfb1d20 | 391 | } |
69cea5a0 | 392 | spin_unlock_irqrestore(&dwc->lock, flags); |
3bfb1d20 HS |
393 | } |
394 | ||
395 | static void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_lli *lli) | |
396 | { | |
41d5e59c | 397 | dev_printk(KERN_CRIT, chan2dev(&dwc->chan), |
3bfb1d20 HS |
398 | " desc: s0x%x d0x%x l0x%x c0x%x:%x\n", |
399 | lli->sar, lli->dar, lli->llp, | |
400 | lli->ctlhi, lli->ctllo); | |
401 | } | |
402 | ||
403 | static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc) | |
404 | { | |
405 | struct dw_desc *bad_desc; | |
406 | struct dw_desc *child; | |
69cea5a0 | 407 | unsigned long flags; |
3bfb1d20 HS |
408 | |
409 | dwc_scan_descriptors(dw, dwc); | |
410 | ||
69cea5a0 VK |
411 | spin_lock_irqsave(&dwc->lock, flags); |
412 | ||
3bfb1d20 HS |
413 | /* |
414 | * The descriptor currently at the head of the active list is | |
415 | * borked. Since we don't have any way to report errors, we'll | |
416 | * just have to scream loudly and try to carry on. | |
417 | */ | |
418 | bad_desc = dwc_first_active(dwc); | |
419 | list_del_init(&bad_desc->desc_node); | |
f336e42f | 420 | list_move(dwc->queue.next, dwc->active_list.prev); |
3bfb1d20 HS |
421 | |
422 | /* Clear the error flag and try to restart the controller */ | |
423 | dma_writel(dw, CLEAR.ERROR, dwc->mask); | |
424 | if (!list_empty(&dwc->active_list)) | |
425 | dwc_dostart(dwc, dwc_first_active(dwc)); | |
426 | ||
427 | /* | |
428 | * KERN_CRITICAL may seem harsh, but since this only happens | |
429 | * when someone submits a bad physical address in a | |
430 | * descriptor, we should consider ourselves lucky that the | |
431 | * controller flagged an error instead of scribbling over | |
432 | * random memory locations. | |
433 | */ | |
41d5e59c | 434 | dev_printk(KERN_CRIT, chan2dev(&dwc->chan), |
3bfb1d20 | 435 | "Bad descriptor submitted for DMA!\n"); |
41d5e59c | 436 | dev_printk(KERN_CRIT, chan2dev(&dwc->chan), |
3bfb1d20 HS |
437 | " cookie: %d\n", bad_desc->txd.cookie); |
438 | dwc_dump_lli(dwc, &bad_desc->lli); | |
e0bd0f8c | 439 | list_for_each_entry(child, &bad_desc->tx_list, desc_node) |
3bfb1d20 HS |
440 | dwc_dump_lli(dwc, &child->lli); |
441 | ||
69cea5a0 VK |
442 | spin_unlock_irqrestore(&dwc->lock, flags); |
443 | ||
3bfb1d20 | 444 | /* Pretend the descriptor completed successfully */ |
5fedefb8 | 445 | dwc_descriptor_complete(dwc, bad_desc, true); |
3bfb1d20 HS |
446 | } |
447 | ||
d9de4519 HCE |
448 | /* --------------------- Cyclic DMA API extensions -------------------- */ |
449 | ||
450 | inline dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan) | |
451 | { | |
452 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); | |
453 | return channel_readl(dwc, SAR); | |
454 | } | |
455 | EXPORT_SYMBOL(dw_dma_get_src_addr); | |
456 | ||
457 | inline dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan) | |
458 | { | |
459 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); | |
460 | return channel_readl(dwc, DAR); | |
461 | } | |
462 | EXPORT_SYMBOL(dw_dma_get_dst_addr); | |
463 | ||
464 | /* called with dwc->lock held and all DMAC interrupts disabled */ | |
465 | static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc, | |
ff7b05f2 | 466 | u32 status_err, u32 status_xfer) |
d9de4519 | 467 | { |
69cea5a0 VK |
468 | unsigned long flags; |
469 | ||
ff7b05f2 | 470 | if (dwc->mask) { |
d9de4519 HCE |
471 | void (*callback)(void *param); |
472 | void *callback_param; | |
473 | ||
474 | dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n", | |
475 | channel_readl(dwc, LLP)); | |
d9de4519 HCE |
476 | |
477 | callback = dwc->cdesc->period_callback; | |
478 | callback_param = dwc->cdesc->period_callback_param; | |
69cea5a0 VK |
479 | |
480 | if (callback) | |
d9de4519 | 481 | callback(callback_param); |
d9de4519 HCE |
482 | } |
483 | ||
484 | /* | |
485 | * Error and transfer complete are highly unlikely, and will most | |
486 | * likely be due to a configuration error by the user. | |
487 | */ | |
488 | if (unlikely(status_err & dwc->mask) || | |
489 | unlikely(status_xfer & dwc->mask)) { | |
490 | int i; | |
491 | ||
492 | dev_err(chan2dev(&dwc->chan), "cyclic DMA unexpected %s " | |
493 | "interrupt, stopping DMA transfer\n", | |
494 | status_xfer ? "xfer" : "error"); | |
69cea5a0 VK |
495 | |
496 | spin_lock_irqsave(&dwc->lock, flags); | |
497 | ||
d9de4519 HCE |
498 | dev_err(chan2dev(&dwc->chan), |
499 | " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n", | |
500 | channel_readl(dwc, SAR), | |
501 | channel_readl(dwc, DAR), | |
502 | channel_readl(dwc, LLP), | |
503 | channel_readl(dwc, CTL_HI), | |
504 | channel_readl(dwc, CTL_LO)); | |
505 | ||
506 | channel_clear_bit(dw, CH_EN, dwc->mask); | |
507 | while (dma_readl(dw, CH_EN) & dwc->mask) | |
508 | cpu_relax(); | |
509 | ||
510 | /* make sure DMA does not restart by loading a new list */ | |
511 | channel_writel(dwc, LLP, 0); | |
512 | channel_writel(dwc, CTL_LO, 0); | |
513 | channel_writel(dwc, CTL_HI, 0); | |
514 | ||
d9de4519 HCE |
515 | dma_writel(dw, CLEAR.ERROR, dwc->mask); |
516 | dma_writel(dw, CLEAR.XFER, dwc->mask); | |
517 | ||
518 | for (i = 0; i < dwc->cdesc->periods; i++) | |
519 | dwc_dump_lli(dwc, &dwc->cdesc->desc[i]->lli); | |
69cea5a0 VK |
520 | |
521 | spin_unlock_irqrestore(&dwc->lock, flags); | |
d9de4519 HCE |
522 | } |
523 | } | |
524 | ||
525 | /* ------------------------------------------------------------------------- */ | |
526 | ||
3bfb1d20 HS |
527 | static void dw_dma_tasklet(unsigned long data) |
528 | { | |
529 | struct dw_dma *dw = (struct dw_dma *)data; | |
530 | struct dw_dma_chan *dwc; | |
3bfb1d20 HS |
531 | u32 status_xfer; |
532 | u32 status_err; | |
533 | int i; | |
534 | ||
7fe7b2f4 | 535 | status_xfer = dma_readl(dw, RAW.XFER); |
3bfb1d20 HS |
536 | status_err = dma_readl(dw, RAW.ERROR); |
537 | ||
ff7b05f2 | 538 | dev_vdbg(dw->dma.dev, "tasklet: status_err=%x\n", status_err); |
3bfb1d20 HS |
539 | |
540 | for (i = 0; i < dw->dma.chancnt; i++) { | |
541 | dwc = &dw->chan[i]; | |
d9de4519 | 542 | if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) |
ff7b05f2 | 543 | dwc_handle_cyclic(dw, dwc, status_err, status_xfer); |
d9de4519 | 544 | else if (status_err & (1 << i)) |
3bfb1d20 | 545 | dwc_handle_error(dw, dwc); |
ff7b05f2 | 546 | else if (status_xfer & (1 << i)) |
3bfb1d20 | 547 | dwc_scan_descriptors(dw, dwc); |
3bfb1d20 HS |
548 | } |
549 | ||
550 | /* | |
ff7b05f2 | 551 | * Re-enable interrupts. |
3bfb1d20 HS |
552 | */ |
553 | channel_set_bit(dw, MASK.XFER, dw->all_chan_mask); | |
3bfb1d20 HS |
554 | channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask); |
555 | } | |
556 | ||
557 | static irqreturn_t dw_dma_interrupt(int irq, void *dev_id) | |
558 | { | |
559 | struct dw_dma *dw = dev_id; | |
560 | u32 status; | |
561 | ||
562 | dev_vdbg(dw->dma.dev, "interrupt: status=0x%x\n", | |
563 | dma_readl(dw, STATUS_INT)); | |
564 | ||
565 | /* | |
566 | * Just disable the interrupts. We'll turn them back on in the | |
567 | * softirq handler. | |
568 | */ | |
569 | channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask); | |
3bfb1d20 HS |
570 | channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask); |
571 | ||
572 | status = dma_readl(dw, STATUS_INT); | |
573 | if (status) { | |
574 | dev_err(dw->dma.dev, | |
575 | "BUG: Unexpected interrupts pending: 0x%x\n", | |
576 | status); | |
577 | ||
578 | /* Try to recover */ | |
579 | channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1); | |
3bfb1d20 HS |
580 | channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1); |
581 | channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1); | |
582 | channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1); | |
583 | } | |
584 | ||
585 | tasklet_schedule(&dw->tasklet); | |
586 | ||
587 | return IRQ_HANDLED; | |
588 | } | |
589 | ||
590 | /*----------------------------------------------------------------------*/ | |
591 | ||
592 | static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx) | |
593 | { | |
594 | struct dw_desc *desc = txd_to_dw_desc(tx); | |
595 | struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan); | |
596 | dma_cookie_t cookie; | |
69cea5a0 | 597 | unsigned long flags; |
3bfb1d20 | 598 | |
69cea5a0 | 599 | spin_lock_irqsave(&dwc->lock, flags); |
3bfb1d20 HS |
600 | cookie = dwc_assign_cookie(dwc, desc); |
601 | ||
602 | /* | |
603 | * REVISIT: We should attempt to chain as many descriptors as | |
604 | * possible, perhaps even appending to those already submitted | |
605 | * for DMA. But this is hard to do in a race-free manner. | |
606 | */ | |
607 | if (list_empty(&dwc->active_list)) { | |
41d5e59c | 608 | dev_vdbg(chan2dev(tx->chan), "tx_submit: started %u\n", |
3bfb1d20 | 609 | desc->txd.cookie); |
3bfb1d20 | 610 | list_add_tail(&desc->desc_node, &dwc->active_list); |
f336e42f | 611 | dwc_dostart(dwc, dwc_first_active(dwc)); |
3bfb1d20 | 612 | } else { |
41d5e59c | 613 | dev_vdbg(chan2dev(tx->chan), "tx_submit: queued %u\n", |
3bfb1d20 HS |
614 | desc->txd.cookie); |
615 | ||
616 | list_add_tail(&desc->desc_node, &dwc->queue); | |
617 | } | |
618 | ||
69cea5a0 | 619 | spin_unlock_irqrestore(&dwc->lock, flags); |
3bfb1d20 HS |
620 | |
621 | return cookie; | |
622 | } | |
623 | ||
624 | static struct dma_async_tx_descriptor * | |
625 | dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, | |
626 | size_t len, unsigned long flags) | |
627 | { | |
628 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); | |
629 | struct dw_desc *desc; | |
630 | struct dw_desc *first; | |
631 | struct dw_desc *prev; | |
632 | size_t xfer_count; | |
633 | size_t offset; | |
634 | unsigned int src_width; | |
635 | unsigned int dst_width; | |
636 | u32 ctllo; | |
637 | ||
41d5e59c | 638 | dev_vdbg(chan2dev(chan), "prep_dma_memcpy d0x%x s0x%x l0x%zx f0x%lx\n", |
3bfb1d20 HS |
639 | dest, src, len, flags); |
640 | ||
641 | if (unlikely(!len)) { | |
41d5e59c | 642 | dev_dbg(chan2dev(chan), "prep_dma_memcpy: length is zero!\n"); |
3bfb1d20 HS |
643 | return NULL; |
644 | } | |
645 | ||
646 | /* | |
647 | * We can be a lot more clever here, but this should take care | |
648 | * of the most common optimization. | |
649 | */ | |
a0227456 VK |
650 | if (!((src | dest | len) & 7)) |
651 | src_width = dst_width = 3; | |
652 | else if (!((src | dest | len) & 3)) | |
3bfb1d20 HS |
653 | src_width = dst_width = 2; |
654 | else if (!((src | dest | len) & 1)) | |
655 | src_width = dst_width = 1; | |
656 | else | |
657 | src_width = dst_width = 0; | |
658 | ||
f301c062 | 659 | ctllo = DWC_DEFAULT_CTLLO(chan->private) |
3bfb1d20 HS |
660 | | DWC_CTLL_DST_WIDTH(dst_width) |
661 | | DWC_CTLL_SRC_WIDTH(src_width) | |
662 | | DWC_CTLL_DST_INC | |
663 | | DWC_CTLL_SRC_INC | |
664 | | DWC_CTLL_FC_M2M; | |
665 | prev = first = NULL; | |
666 | ||
667 | for (offset = 0; offset < len; offset += xfer_count << src_width) { | |
668 | xfer_count = min_t(size_t, (len - offset) >> src_width, | |
669 | DWC_MAX_COUNT); | |
670 | ||
671 | desc = dwc_desc_get(dwc); | |
672 | if (!desc) | |
673 | goto err_desc_get; | |
674 | ||
675 | desc->lli.sar = src + offset; | |
676 | desc->lli.dar = dest + offset; | |
677 | desc->lli.ctllo = ctllo; | |
678 | desc->lli.ctlhi = xfer_count; | |
679 | ||
680 | if (!first) { | |
681 | first = desc; | |
682 | } else { | |
683 | prev->lli.llp = desc->txd.phys; | |
41d5e59c | 684 | dma_sync_single_for_device(chan2parent(chan), |
3bfb1d20 HS |
685 | prev->txd.phys, sizeof(prev->lli), |
686 | DMA_TO_DEVICE); | |
687 | list_add_tail(&desc->desc_node, | |
e0bd0f8c | 688 | &first->tx_list); |
3bfb1d20 HS |
689 | } |
690 | prev = desc; | |
691 | } | |
692 | ||
693 | ||
694 | if (flags & DMA_PREP_INTERRUPT) | |
695 | /* Trigger interrupt after last block */ | |
696 | prev->lli.ctllo |= DWC_CTLL_INT_EN; | |
697 | ||
698 | prev->lli.llp = 0; | |
41d5e59c | 699 | dma_sync_single_for_device(chan2parent(chan), |
3bfb1d20 HS |
700 | prev->txd.phys, sizeof(prev->lli), |
701 | DMA_TO_DEVICE); | |
702 | ||
703 | first->txd.flags = flags; | |
704 | first->len = len; | |
705 | ||
706 | return &first->txd; | |
707 | ||
708 | err_desc_get: | |
709 | dwc_desc_put(dwc, first); | |
710 | return NULL; | |
711 | } | |
712 | ||
713 | static struct dma_async_tx_descriptor * | |
714 | dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, | |
db8196df | 715 | unsigned int sg_len, enum dma_transfer_direction direction, |
3bfb1d20 HS |
716 | unsigned long flags) |
717 | { | |
718 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); | |
287d8592 | 719 | struct dw_dma_slave *dws = chan->private; |
3bfb1d20 HS |
720 | struct dw_desc *prev; |
721 | struct dw_desc *first; | |
722 | u32 ctllo; | |
723 | dma_addr_t reg; | |
724 | unsigned int reg_width; | |
725 | unsigned int mem_width; | |
726 | unsigned int i; | |
727 | struct scatterlist *sg; | |
728 | size_t total_len = 0; | |
729 | ||
41d5e59c | 730 | dev_vdbg(chan2dev(chan), "prep_dma_slave\n"); |
3bfb1d20 HS |
731 | |
732 | if (unlikely(!dws || !sg_len)) | |
733 | return NULL; | |
734 | ||
74465b4f | 735 | reg_width = dws->reg_width; |
3bfb1d20 HS |
736 | prev = first = NULL; |
737 | ||
3bfb1d20 | 738 | switch (direction) { |
db8196df | 739 | case DMA_MEM_TO_DEV: |
f301c062 | 740 | ctllo = (DWC_DEFAULT_CTLLO(chan->private) |
3bfb1d20 HS |
741 | | DWC_CTLL_DST_WIDTH(reg_width) |
742 | | DWC_CTLL_DST_FIX | |
743 | | DWC_CTLL_SRC_INC | |
ee66509d | 744 | | DWC_CTLL_FC(dws->fc)); |
74465b4f | 745 | reg = dws->tx_reg; |
3bfb1d20 HS |
746 | for_each_sg(sgl, sg, sg_len, i) { |
747 | struct dw_desc *desc; | |
69dc14b5 | 748 | u32 len, dlen, mem; |
3bfb1d20 | 749 | |
69dc14b5 VK |
750 | mem = sg_phys(sg); |
751 | len = sg_dma_len(sg); | |
752 | mem_width = 2; | |
753 | if (unlikely(mem & 3 || len & 3)) | |
754 | mem_width = 0; | |
3bfb1d20 | 755 | |
69dc14b5 | 756 | slave_sg_todev_fill_desc: |
3bfb1d20 HS |
757 | desc = dwc_desc_get(dwc); |
758 | if (!desc) { | |
41d5e59c | 759 | dev_err(chan2dev(chan), |
3bfb1d20 HS |
760 | "not enough descriptors available\n"); |
761 | goto err_desc_get; | |
762 | } | |
763 | ||
3bfb1d20 HS |
764 | desc->lli.sar = mem; |
765 | desc->lli.dar = reg; | |
766 | desc->lli.ctllo = ctllo | DWC_CTLL_SRC_WIDTH(mem_width); | |
69dc14b5 VK |
767 | if ((len >> mem_width) > DWC_MAX_COUNT) { |
768 | dlen = DWC_MAX_COUNT << mem_width; | |
769 | mem += dlen; | |
770 | len -= dlen; | |
771 | } else { | |
772 | dlen = len; | |
773 | len = 0; | |
774 | } | |
775 | ||
776 | desc->lli.ctlhi = dlen >> mem_width; | |
3bfb1d20 HS |
777 | |
778 | if (!first) { | |
779 | first = desc; | |
780 | } else { | |
781 | prev->lli.llp = desc->txd.phys; | |
41d5e59c | 782 | dma_sync_single_for_device(chan2parent(chan), |
3bfb1d20 HS |
783 | prev->txd.phys, |
784 | sizeof(prev->lli), | |
785 | DMA_TO_DEVICE); | |
786 | list_add_tail(&desc->desc_node, | |
e0bd0f8c | 787 | &first->tx_list); |
3bfb1d20 HS |
788 | } |
789 | prev = desc; | |
69dc14b5 VK |
790 | total_len += dlen; |
791 | ||
792 | if (len) | |
793 | goto slave_sg_todev_fill_desc; | |
3bfb1d20 HS |
794 | } |
795 | break; | |
db8196df | 796 | case DMA_DEV_TO_MEM: |
f301c062 | 797 | ctllo = (DWC_DEFAULT_CTLLO(chan->private) |
3bfb1d20 HS |
798 | | DWC_CTLL_SRC_WIDTH(reg_width) |
799 | | DWC_CTLL_DST_INC | |
800 | | DWC_CTLL_SRC_FIX | |
ee66509d | 801 | | DWC_CTLL_FC(dws->fc)); |
3bfb1d20 | 802 | |
74465b4f | 803 | reg = dws->rx_reg; |
3bfb1d20 HS |
804 | for_each_sg(sgl, sg, sg_len, i) { |
805 | struct dw_desc *desc; | |
69dc14b5 | 806 | u32 len, dlen, mem; |
3bfb1d20 HS |
807 | |
808 | mem = sg_phys(sg); | |
809 | len = sg_dma_len(sg); | |
810 | mem_width = 2; | |
811 | if (unlikely(mem & 3 || len & 3)) | |
812 | mem_width = 0; | |
813 | ||
69dc14b5 VK |
814 | slave_sg_fromdev_fill_desc: |
815 | desc = dwc_desc_get(dwc); | |
816 | if (!desc) { | |
817 | dev_err(chan2dev(chan), | |
818 | "not enough descriptors available\n"); | |
819 | goto err_desc_get; | |
820 | } | |
821 | ||
3bfb1d20 HS |
822 | desc->lli.sar = reg; |
823 | desc->lli.dar = mem; | |
824 | desc->lli.ctllo = ctllo | DWC_CTLL_DST_WIDTH(mem_width); | |
69dc14b5 VK |
825 | if ((len >> reg_width) > DWC_MAX_COUNT) { |
826 | dlen = DWC_MAX_COUNT << reg_width; | |
827 | mem += dlen; | |
828 | len -= dlen; | |
829 | } else { | |
830 | dlen = len; | |
831 | len = 0; | |
832 | } | |
833 | desc->lli.ctlhi = dlen >> reg_width; | |
3bfb1d20 HS |
834 | |
835 | if (!first) { | |
836 | first = desc; | |
837 | } else { | |
838 | prev->lli.llp = desc->txd.phys; | |
41d5e59c | 839 | dma_sync_single_for_device(chan2parent(chan), |
3bfb1d20 HS |
840 | prev->txd.phys, |
841 | sizeof(prev->lli), | |
842 | DMA_TO_DEVICE); | |
843 | list_add_tail(&desc->desc_node, | |
e0bd0f8c | 844 | &first->tx_list); |
3bfb1d20 HS |
845 | } |
846 | prev = desc; | |
69dc14b5 VK |
847 | total_len += dlen; |
848 | ||
849 | if (len) | |
850 | goto slave_sg_fromdev_fill_desc; | |
3bfb1d20 HS |
851 | } |
852 | break; | |
853 | default: | |
854 | return NULL; | |
855 | } | |
856 | ||
857 | if (flags & DMA_PREP_INTERRUPT) | |
858 | /* Trigger interrupt after last block */ | |
859 | prev->lli.ctllo |= DWC_CTLL_INT_EN; | |
860 | ||
861 | prev->lli.llp = 0; | |
41d5e59c | 862 | dma_sync_single_for_device(chan2parent(chan), |
3bfb1d20 HS |
863 | prev->txd.phys, sizeof(prev->lli), |
864 | DMA_TO_DEVICE); | |
865 | ||
866 | first->len = total_len; | |
867 | ||
868 | return &first->txd; | |
869 | ||
870 | err_desc_get: | |
871 | dwc_desc_put(dwc, first); | |
872 | return NULL; | |
873 | } | |
874 | ||
05827630 LW |
875 | static int dwc_control(struct dma_chan *chan, enum dma_ctrl_cmd cmd, |
876 | unsigned long arg) | |
3bfb1d20 HS |
877 | { |
878 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); | |
879 | struct dw_dma *dw = to_dw_dma(chan->device); | |
880 | struct dw_desc *desc, *_desc; | |
69cea5a0 | 881 | unsigned long flags; |
a7c57cf7 | 882 | u32 cfglo; |
3bfb1d20 HS |
883 | LIST_HEAD(list); |
884 | ||
a7c57cf7 LW |
885 | if (cmd == DMA_PAUSE) { |
886 | spin_lock_irqsave(&dwc->lock, flags); | |
c3635c78 | 887 | |
a7c57cf7 LW |
888 | cfglo = channel_readl(dwc, CFG_LO); |
889 | channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP); | |
890 | while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY)) | |
891 | cpu_relax(); | |
3bfb1d20 | 892 | |
a7c57cf7 LW |
893 | dwc->paused = true; |
894 | spin_unlock_irqrestore(&dwc->lock, flags); | |
895 | } else if (cmd == DMA_RESUME) { | |
896 | if (!dwc->paused) | |
897 | return 0; | |
3bfb1d20 | 898 | |
a7c57cf7 | 899 | spin_lock_irqsave(&dwc->lock, flags); |
3bfb1d20 | 900 | |
a7c57cf7 LW |
901 | cfglo = channel_readl(dwc, CFG_LO); |
902 | channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP); | |
903 | dwc->paused = false; | |
3bfb1d20 | 904 | |
a7c57cf7 LW |
905 | spin_unlock_irqrestore(&dwc->lock, flags); |
906 | } else if (cmd == DMA_TERMINATE_ALL) { | |
907 | spin_lock_irqsave(&dwc->lock, flags); | |
3bfb1d20 | 908 | |
a7c57cf7 LW |
909 | channel_clear_bit(dw, CH_EN, dwc->mask); |
910 | while (dma_readl(dw, CH_EN) & dwc->mask) | |
911 | cpu_relax(); | |
912 | ||
913 | dwc->paused = false; | |
914 | ||
915 | /* active_list entries will end up before queued entries */ | |
916 | list_splice_init(&dwc->queue, &list); | |
917 | list_splice_init(&dwc->active_list, &list); | |
918 | ||
919 | spin_unlock_irqrestore(&dwc->lock, flags); | |
920 | ||
921 | /* Flush all pending and queued descriptors */ | |
922 | list_for_each_entry_safe(desc, _desc, &list, desc_node) | |
923 | dwc_descriptor_complete(dwc, desc, false); | |
924 | } else | |
925 | return -ENXIO; | |
c3635c78 LW |
926 | |
927 | return 0; | |
3bfb1d20 HS |
928 | } |
929 | ||
930 | static enum dma_status | |
07934481 LW |
931 | dwc_tx_status(struct dma_chan *chan, |
932 | dma_cookie_t cookie, | |
933 | struct dma_tx_state *txstate) | |
3bfb1d20 HS |
934 | { |
935 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); | |
936 | dma_cookie_t last_used; | |
937 | dma_cookie_t last_complete; | |
938 | int ret; | |
939 | ||
940 | last_complete = dwc->completed; | |
941 | last_used = chan->cookie; | |
942 | ||
943 | ret = dma_async_is_complete(cookie, last_complete, last_used); | |
944 | if (ret != DMA_SUCCESS) { | |
945 | dwc_scan_descriptors(to_dw_dma(chan->device), dwc); | |
946 | ||
947 | last_complete = dwc->completed; | |
948 | last_used = chan->cookie; | |
949 | ||
950 | ret = dma_async_is_complete(cookie, last_complete, last_used); | |
951 | } | |
952 | ||
abf53902 VK |
953 | if (ret != DMA_SUCCESS) |
954 | dma_set_tx_state(txstate, last_complete, last_used, | |
955 | dwc_first_active(dwc)->len); | |
956 | else | |
957 | dma_set_tx_state(txstate, last_complete, last_used, 0); | |
3bfb1d20 | 958 | |
a7c57cf7 LW |
959 | if (dwc->paused) |
960 | return DMA_PAUSED; | |
3bfb1d20 HS |
961 | |
962 | return ret; | |
963 | } | |
964 | ||
965 | static void dwc_issue_pending(struct dma_chan *chan) | |
966 | { | |
967 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); | |
968 | ||
3bfb1d20 HS |
969 | if (!list_empty(&dwc->queue)) |
970 | dwc_scan_descriptors(to_dw_dma(chan->device), dwc); | |
3bfb1d20 HS |
971 | } |
972 | ||
aa1e6f1a | 973 | static int dwc_alloc_chan_resources(struct dma_chan *chan) |
3bfb1d20 HS |
974 | { |
975 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); | |
976 | struct dw_dma *dw = to_dw_dma(chan->device); | |
977 | struct dw_desc *desc; | |
3bfb1d20 | 978 | int i; |
69cea5a0 | 979 | unsigned long flags; |
3bfb1d20 | 980 | |
41d5e59c | 981 | dev_vdbg(chan2dev(chan), "alloc_chan_resources\n"); |
3bfb1d20 | 982 | |
3bfb1d20 HS |
983 | /* ASSERT: channel is idle */ |
984 | if (dma_readl(dw, CH_EN) & dwc->mask) { | |
41d5e59c | 985 | dev_dbg(chan2dev(chan), "DMA channel not idle?\n"); |
3bfb1d20 HS |
986 | return -EIO; |
987 | } | |
988 | ||
989 | dwc->completed = chan->cookie = 1; | |
990 | ||
3bfb1d20 HS |
991 | /* |
992 | * NOTE: some controllers may have additional features that we | |
993 | * need to initialize here, like "scatter-gather" (which | |
994 | * doesn't mean what you think it means), and status writeback. | |
995 | */ | |
996 | ||
69cea5a0 | 997 | spin_lock_irqsave(&dwc->lock, flags); |
3bfb1d20 HS |
998 | i = dwc->descs_allocated; |
999 | while (dwc->descs_allocated < NR_DESCS_PER_CHANNEL) { | |
69cea5a0 | 1000 | spin_unlock_irqrestore(&dwc->lock, flags); |
3bfb1d20 HS |
1001 | |
1002 | desc = kzalloc(sizeof(struct dw_desc), GFP_KERNEL); | |
1003 | if (!desc) { | |
41d5e59c | 1004 | dev_info(chan2dev(chan), |
3bfb1d20 | 1005 | "only allocated %d descriptors\n", i); |
69cea5a0 | 1006 | spin_lock_irqsave(&dwc->lock, flags); |
3bfb1d20 HS |
1007 | break; |
1008 | } | |
1009 | ||
e0bd0f8c | 1010 | INIT_LIST_HEAD(&desc->tx_list); |
3bfb1d20 HS |
1011 | dma_async_tx_descriptor_init(&desc->txd, chan); |
1012 | desc->txd.tx_submit = dwc_tx_submit; | |
1013 | desc->txd.flags = DMA_CTRL_ACK; | |
41d5e59c | 1014 | desc->txd.phys = dma_map_single(chan2parent(chan), &desc->lli, |
3bfb1d20 HS |
1015 | sizeof(desc->lli), DMA_TO_DEVICE); |
1016 | dwc_desc_put(dwc, desc); | |
1017 | ||
69cea5a0 | 1018 | spin_lock_irqsave(&dwc->lock, flags); |
3bfb1d20 HS |
1019 | i = ++dwc->descs_allocated; |
1020 | } | |
1021 | ||
69cea5a0 | 1022 | spin_unlock_irqrestore(&dwc->lock, flags); |
3bfb1d20 | 1023 | |
41d5e59c | 1024 | dev_dbg(chan2dev(chan), |
3bfb1d20 HS |
1025 | "alloc_chan_resources allocated %d descriptors\n", i); |
1026 | ||
1027 | return i; | |
1028 | } | |
1029 | ||
1030 | static void dwc_free_chan_resources(struct dma_chan *chan) | |
1031 | { | |
1032 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); | |
1033 | struct dw_dma *dw = to_dw_dma(chan->device); | |
1034 | struct dw_desc *desc, *_desc; | |
69cea5a0 | 1035 | unsigned long flags; |
3bfb1d20 HS |
1036 | LIST_HEAD(list); |
1037 | ||
41d5e59c | 1038 | dev_dbg(chan2dev(chan), "free_chan_resources (descs allocated=%u)\n", |
3bfb1d20 HS |
1039 | dwc->descs_allocated); |
1040 | ||
1041 | /* ASSERT: channel is idle */ | |
1042 | BUG_ON(!list_empty(&dwc->active_list)); | |
1043 | BUG_ON(!list_empty(&dwc->queue)); | |
1044 | BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask); | |
1045 | ||
69cea5a0 | 1046 | spin_lock_irqsave(&dwc->lock, flags); |
3bfb1d20 HS |
1047 | list_splice_init(&dwc->free_list, &list); |
1048 | dwc->descs_allocated = 0; | |
61e183f8 | 1049 | dwc->initialized = false; |
3bfb1d20 HS |
1050 | |
1051 | /* Disable interrupts */ | |
1052 | channel_clear_bit(dw, MASK.XFER, dwc->mask); | |
3bfb1d20 HS |
1053 | channel_clear_bit(dw, MASK.ERROR, dwc->mask); |
1054 | ||
69cea5a0 | 1055 | spin_unlock_irqrestore(&dwc->lock, flags); |
3bfb1d20 HS |
1056 | |
1057 | list_for_each_entry_safe(desc, _desc, &list, desc_node) { | |
41d5e59c DW |
1058 | dev_vdbg(chan2dev(chan), " freeing descriptor %p\n", desc); |
1059 | dma_unmap_single(chan2parent(chan), desc->txd.phys, | |
3bfb1d20 HS |
1060 | sizeof(desc->lli), DMA_TO_DEVICE); |
1061 | kfree(desc); | |
1062 | } | |
1063 | ||
41d5e59c | 1064 | dev_vdbg(chan2dev(chan), "free_chan_resources done\n"); |
3bfb1d20 HS |
1065 | } |
1066 | ||
d9de4519 HCE |
1067 | /* --------------------- Cyclic DMA API extensions -------------------- */ |
1068 | ||
1069 | /** | |
1070 | * dw_dma_cyclic_start - start the cyclic DMA transfer | |
1071 | * @chan: the DMA channel to start | |
1072 | * | |
1073 | * Must be called with soft interrupts disabled. Returns zero on success or | |
1074 | * -errno on failure. | |
1075 | */ | |
1076 | int dw_dma_cyclic_start(struct dma_chan *chan) | |
1077 | { | |
1078 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); | |
1079 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); | |
69cea5a0 | 1080 | unsigned long flags; |
d9de4519 HCE |
1081 | |
1082 | if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) { | |
1083 | dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n"); | |
1084 | return -ENODEV; | |
1085 | } | |
1086 | ||
69cea5a0 | 1087 | spin_lock_irqsave(&dwc->lock, flags); |
d9de4519 HCE |
1088 | |
1089 | /* assert channel is idle */ | |
1090 | if (dma_readl(dw, CH_EN) & dwc->mask) { | |
1091 | dev_err(chan2dev(&dwc->chan), | |
1092 | "BUG: Attempted to start non-idle channel\n"); | |
1093 | dev_err(chan2dev(&dwc->chan), | |
1094 | " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n", | |
1095 | channel_readl(dwc, SAR), | |
1096 | channel_readl(dwc, DAR), | |
1097 | channel_readl(dwc, LLP), | |
1098 | channel_readl(dwc, CTL_HI), | |
1099 | channel_readl(dwc, CTL_LO)); | |
69cea5a0 | 1100 | spin_unlock_irqrestore(&dwc->lock, flags); |
d9de4519 HCE |
1101 | return -EBUSY; |
1102 | } | |
1103 | ||
d9de4519 HCE |
1104 | dma_writel(dw, CLEAR.ERROR, dwc->mask); |
1105 | dma_writel(dw, CLEAR.XFER, dwc->mask); | |
1106 | ||
1107 | /* setup DMAC channel registers */ | |
1108 | channel_writel(dwc, LLP, dwc->cdesc->desc[0]->txd.phys); | |
1109 | channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN); | |
1110 | channel_writel(dwc, CTL_HI, 0); | |
1111 | ||
1112 | channel_set_bit(dw, CH_EN, dwc->mask); | |
1113 | ||
69cea5a0 | 1114 | spin_unlock_irqrestore(&dwc->lock, flags); |
d9de4519 HCE |
1115 | |
1116 | return 0; | |
1117 | } | |
1118 | EXPORT_SYMBOL(dw_dma_cyclic_start); | |
1119 | ||
1120 | /** | |
1121 | * dw_dma_cyclic_stop - stop the cyclic DMA transfer | |
1122 | * @chan: the DMA channel to stop | |
1123 | * | |
1124 | * Must be called with soft interrupts disabled. | |
1125 | */ | |
1126 | void dw_dma_cyclic_stop(struct dma_chan *chan) | |
1127 | { | |
1128 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); | |
1129 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); | |
69cea5a0 | 1130 | unsigned long flags; |
d9de4519 | 1131 | |
69cea5a0 | 1132 | spin_lock_irqsave(&dwc->lock, flags); |
d9de4519 HCE |
1133 | |
1134 | channel_clear_bit(dw, CH_EN, dwc->mask); | |
1135 | while (dma_readl(dw, CH_EN) & dwc->mask) | |
1136 | cpu_relax(); | |
1137 | ||
69cea5a0 | 1138 | spin_unlock_irqrestore(&dwc->lock, flags); |
d9de4519 HCE |
1139 | } |
1140 | EXPORT_SYMBOL(dw_dma_cyclic_stop); | |
1141 | ||
1142 | /** | |
1143 | * dw_dma_cyclic_prep - prepare the cyclic DMA transfer | |
1144 | * @chan: the DMA channel to prepare | |
1145 | * @buf_addr: physical DMA address where the buffer starts | |
1146 | * @buf_len: total number of bytes for the entire buffer | |
1147 | * @period_len: number of bytes for each period | |
1148 | * @direction: transfer direction, to or from device | |
1149 | * | |
1150 | * Must be called before trying to start the transfer. Returns a valid struct | |
1151 | * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful. | |
1152 | */ | |
1153 | struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan, | |
1154 | dma_addr_t buf_addr, size_t buf_len, size_t period_len, | |
db8196df | 1155 | enum dma_transfer_direction direction) |
d9de4519 HCE |
1156 | { |
1157 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); | |
1158 | struct dw_cyclic_desc *cdesc; | |
1159 | struct dw_cyclic_desc *retval = NULL; | |
1160 | struct dw_desc *desc; | |
1161 | struct dw_desc *last = NULL; | |
1162 | struct dw_dma_slave *dws = chan->private; | |
1163 | unsigned long was_cyclic; | |
1164 | unsigned int reg_width; | |
1165 | unsigned int periods; | |
1166 | unsigned int i; | |
69cea5a0 | 1167 | unsigned long flags; |
d9de4519 | 1168 | |
69cea5a0 | 1169 | spin_lock_irqsave(&dwc->lock, flags); |
d9de4519 | 1170 | if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) { |
69cea5a0 | 1171 | spin_unlock_irqrestore(&dwc->lock, flags); |
d9de4519 HCE |
1172 | dev_dbg(chan2dev(&dwc->chan), |
1173 | "queue and/or active list are not empty\n"); | |
1174 | return ERR_PTR(-EBUSY); | |
1175 | } | |
1176 | ||
1177 | was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags); | |
69cea5a0 | 1178 | spin_unlock_irqrestore(&dwc->lock, flags); |
d9de4519 HCE |
1179 | if (was_cyclic) { |
1180 | dev_dbg(chan2dev(&dwc->chan), | |
1181 | "channel already prepared for cyclic DMA\n"); | |
1182 | return ERR_PTR(-EBUSY); | |
1183 | } | |
1184 | ||
1185 | retval = ERR_PTR(-EINVAL); | |
1186 | reg_width = dws->reg_width; | |
1187 | periods = buf_len / period_len; | |
1188 | ||
1189 | /* Check for too big/unaligned periods and unaligned DMA buffer. */ | |
1190 | if (period_len > (DWC_MAX_COUNT << reg_width)) | |
1191 | goto out_err; | |
1192 | if (unlikely(period_len & ((1 << reg_width) - 1))) | |
1193 | goto out_err; | |
1194 | if (unlikely(buf_addr & ((1 << reg_width) - 1))) | |
1195 | goto out_err; | |
db8196df | 1196 | if (unlikely(!(direction & (DMA_MEM_TO_DEV | DMA_DEV_TO_MEM)))) |
d9de4519 HCE |
1197 | goto out_err; |
1198 | ||
1199 | retval = ERR_PTR(-ENOMEM); | |
1200 | ||
1201 | if (periods > NR_DESCS_PER_CHANNEL) | |
1202 | goto out_err; | |
1203 | ||
1204 | cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL); | |
1205 | if (!cdesc) | |
1206 | goto out_err; | |
1207 | ||
1208 | cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL); | |
1209 | if (!cdesc->desc) | |
1210 | goto out_err_alloc; | |
1211 | ||
1212 | for (i = 0; i < periods; i++) { | |
1213 | desc = dwc_desc_get(dwc); | |
1214 | if (!desc) | |
1215 | goto out_err_desc_get; | |
1216 | ||
1217 | switch (direction) { | |
db8196df | 1218 | case DMA_MEM_TO_DEV: |
d9de4519 HCE |
1219 | desc->lli.dar = dws->tx_reg; |
1220 | desc->lli.sar = buf_addr + (period_len * i); | |
f301c062 | 1221 | desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private) |
d9de4519 HCE |
1222 | | DWC_CTLL_DST_WIDTH(reg_width) |
1223 | | DWC_CTLL_SRC_WIDTH(reg_width) | |
1224 | | DWC_CTLL_DST_FIX | |
1225 | | DWC_CTLL_SRC_INC | |
ee66509d | 1226 | | DWC_CTLL_FC(dws->fc) |
d9de4519 HCE |
1227 | | DWC_CTLL_INT_EN); |
1228 | break; | |
db8196df | 1229 | case DMA_DEV_TO_MEM: |
d9de4519 HCE |
1230 | desc->lli.dar = buf_addr + (period_len * i); |
1231 | desc->lli.sar = dws->rx_reg; | |
f301c062 | 1232 | desc->lli.ctllo = (DWC_DEFAULT_CTLLO(chan->private) |
d9de4519 HCE |
1233 | | DWC_CTLL_SRC_WIDTH(reg_width) |
1234 | | DWC_CTLL_DST_WIDTH(reg_width) | |
1235 | | DWC_CTLL_DST_INC | |
1236 | | DWC_CTLL_SRC_FIX | |
ee66509d | 1237 | | DWC_CTLL_FC(dws->fc) |
d9de4519 HCE |
1238 | | DWC_CTLL_INT_EN); |
1239 | break; | |
1240 | default: | |
1241 | break; | |
1242 | } | |
1243 | ||
1244 | desc->lli.ctlhi = (period_len >> reg_width); | |
1245 | cdesc->desc[i] = desc; | |
1246 | ||
1247 | if (last) { | |
1248 | last->lli.llp = desc->txd.phys; | |
1249 | dma_sync_single_for_device(chan2parent(chan), | |
1250 | last->txd.phys, sizeof(last->lli), | |
1251 | DMA_TO_DEVICE); | |
1252 | } | |
1253 | ||
1254 | last = desc; | |
1255 | } | |
1256 | ||
1257 | /* lets make a cyclic list */ | |
1258 | last->lli.llp = cdesc->desc[0]->txd.phys; | |
1259 | dma_sync_single_for_device(chan2parent(chan), last->txd.phys, | |
1260 | sizeof(last->lli), DMA_TO_DEVICE); | |
1261 | ||
1262 | dev_dbg(chan2dev(&dwc->chan), "cyclic prepared buf 0x%08x len %zu " | |
1263 | "period %zu periods %d\n", buf_addr, buf_len, | |
1264 | period_len, periods); | |
1265 | ||
1266 | cdesc->periods = periods; | |
1267 | dwc->cdesc = cdesc; | |
1268 | ||
1269 | return cdesc; | |
1270 | ||
1271 | out_err_desc_get: | |
1272 | while (i--) | |
1273 | dwc_desc_put(dwc, cdesc->desc[i]); | |
1274 | out_err_alloc: | |
1275 | kfree(cdesc); | |
1276 | out_err: | |
1277 | clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags); | |
1278 | return (struct dw_cyclic_desc *)retval; | |
1279 | } | |
1280 | EXPORT_SYMBOL(dw_dma_cyclic_prep); | |
1281 | ||
1282 | /** | |
1283 | * dw_dma_cyclic_free - free a prepared cyclic DMA transfer | |
1284 | * @chan: the DMA channel to free | |
1285 | */ | |
1286 | void dw_dma_cyclic_free(struct dma_chan *chan) | |
1287 | { | |
1288 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); | |
1289 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); | |
1290 | struct dw_cyclic_desc *cdesc = dwc->cdesc; | |
1291 | int i; | |
69cea5a0 | 1292 | unsigned long flags; |
d9de4519 HCE |
1293 | |
1294 | dev_dbg(chan2dev(&dwc->chan), "cyclic free\n"); | |
1295 | ||
1296 | if (!cdesc) | |
1297 | return; | |
1298 | ||
69cea5a0 | 1299 | spin_lock_irqsave(&dwc->lock, flags); |
d9de4519 HCE |
1300 | |
1301 | channel_clear_bit(dw, CH_EN, dwc->mask); | |
1302 | while (dma_readl(dw, CH_EN) & dwc->mask) | |
1303 | cpu_relax(); | |
1304 | ||
d9de4519 HCE |
1305 | dma_writel(dw, CLEAR.ERROR, dwc->mask); |
1306 | dma_writel(dw, CLEAR.XFER, dwc->mask); | |
1307 | ||
69cea5a0 | 1308 | spin_unlock_irqrestore(&dwc->lock, flags); |
d9de4519 HCE |
1309 | |
1310 | for (i = 0; i < cdesc->periods; i++) | |
1311 | dwc_desc_put(dwc, cdesc->desc[i]); | |
1312 | ||
1313 | kfree(cdesc->desc); | |
1314 | kfree(cdesc); | |
1315 | ||
1316 | clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags); | |
1317 | } | |
1318 | EXPORT_SYMBOL(dw_dma_cyclic_free); | |
1319 | ||
3bfb1d20 HS |
1320 | /*----------------------------------------------------------------------*/ |
1321 | ||
1322 | static void dw_dma_off(struct dw_dma *dw) | |
1323 | { | |
61e183f8 VK |
1324 | int i; |
1325 | ||
3bfb1d20 HS |
1326 | dma_writel(dw, CFG, 0); |
1327 | ||
1328 | channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask); | |
3bfb1d20 HS |
1329 | channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask); |
1330 | channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask); | |
1331 | channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask); | |
1332 | ||
1333 | while (dma_readl(dw, CFG) & DW_CFG_DMA_EN) | |
1334 | cpu_relax(); | |
61e183f8 VK |
1335 | |
1336 | for (i = 0; i < dw->dma.chancnt; i++) | |
1337 | dw->chan[i].initialized = false; | |
3bfb1d20 HS |
1338 | } |
1339 | ||
1340 | static int __init dw_probe(struct platform_device *pdev) | |
1341 | { | |
1342 | struct dw_dma_platform_data *pdata; | |
1343 | struct resource *io; | |
1344 | struct dw_dma *dw; | |
1345 | size_t size; | |
1346 | int irq; | |
1347 | int err; | |
1348 | int i; | |
1349 | ||
6c618c9d | 1350 | pdata = dev_get_platdata(&pdev->dev); |
3bfb1d20 HS |
1351 | if (!pdata || pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS) |
1352 | return -EINVAL; | |
1353 | ||
1354 | io = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
1355 | if (!io) | |
1356 | return -EINVAL; | |
1357 | ||
1358 | irq = platform_get_irq(pdev, 0); | |
1359 | if (irq < 0) | |
1360 | return irq; | |
1361 | ||
1362 | size = sizeof(struct dw_dma); | |
1363 | size += pdata->nr_channels * sizeof(struct dw_dma_chan); | |
1364 | dw = kzalloc(size, GFP_KERNEL); | |
1365 | if (!dw) | |
1366 | return -ENOMEM; | |
1367 | ||
1368 | if (!request_mem_region(io->start, DW_REGLEN, pdev->dev.driver->name)) { | |
1369 | err = -EBUSY; | |
1370 | goto err_kfree; | |
1371 | } | |
1372 | ||
3bfb1d20 HS |
1373 | dw->regs = ioremap(io->start, DW_REGLEN); |
1374 | if (!dw->regs) { | |
1375 | err = -ENOMEM; | |
1376 | goto err_release_r; | |
1377 | } | |
1378 | ||
1379 | dw->clk = clk_get(&pdev->dev, "hclk"); | |
1380 | if (IS_ERR(dw->clk)) { | |
1381 | err = PTR_ERR(dw->clk); | |
1382 | goto err_clk; | |
1383 | } | |
1384 | clk_enable(dw->clk); | |
1385 | ||
1386 | /* force dma off, just in case */ | |
1387 | dw_dma_off(dw); | |
1388 | ||
1389 | err = request_irq(irq, dw_dma_interrupt, 0, "dw_dmac", dw); | |
1390 | if (err) | |
1391 | goto err_irq; | |
1392 | ||
1393 | platform_set_drvdata(pdev, dw); | |
1394 | ||
1395 | tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw); | |
1396 | ||
1397 | dw->all_chan_mask = (1 << pdata->nr_channels) - 1; | |
1398 | ||
1399 | INIT_LIST_HEAD(&dw->dma.channels); | |
46389470 | 1400 | for (i = 0; i < pdata->nr_channels; i++) { |
3bfb1d20 HS |
1401 | struct dw_dma_chan *dwc = &dw->chan[i]; |
1402 | ||
1403 | dwc->chan.device = &dw->dma; | |
1404 | dwc->chan.cookie = dwc->completed = 1; | |
b0c3130d VK |
1405 | if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING) |
1406 | list_add_tail(&dwc->chan.device_node, | |
1407 | &dw->dma.channels); | |
1408 | else | |
1409 | list_add(&dwc->chan.device_node, &dw->dma.channels); | |
3bfb1d20 | 1410 | |
93317e8e VK |
1411 | /* 7 is highest priority & 0 is lowest. */ |
1412 | if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING) | |
e8d9f875 | 1413 | dwc->priority = pdata->nr_channels - i - 1; |
93317e8e VK |
1414 | else |
1415 | dwc->priority = i; | |
1416 | ||
3bfb1d20 HS |
1417 | dwc->ch_regs = &__dw_regs(dw)->CHAN[i]; |
1418 | spin_lock_init(&dwc->lock); | |
1419 | dwc->mask = 1 << i; | |
1420 | ||
1421 | INIT_LIST_HEAD(&dwc->active_list); | |
1422 | INIT_LIST_HEAD(&dwc->queue); | |
1423 | INIT_LIST_HEAD(&dwc->free_list); | |
1424 | ||
1425 | channel_clear_bit(dw, CH_EN, dwc->mask); | |
1426 | } | |
1427 | ||
1428 | /* Clear/disable all interrupts on all channels. */ | |
1429 | dma_writel(dw, CLEAR.XFER, dw->all_chan_mask); | |
3bfb1d20 HS |
1430 | dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask); |
1431 | dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask); | |
1432 | dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask); | |
1433 | ||
1434 | channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask); | |
3bfb1d20 HS |
1435 | channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask); |
1436 | channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask); | |
1437 | channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask); | |
1438 | ||
1439 | dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask); | |
1440 | dma_cap_set(DMA_SLAVE, dw->dma.cap_mask); | |
95ea759e JI |
1441 | if (pdata->is_private) |
1442 | dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask); | |
3bfb1d20 HS |
1443 | dw->dma.dev = &pdev->dev; |
1444 | dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources; | |
1445 | dw->dma.device_free_chan_resources = dwc_free_chan_resources; | |
1446 | ||
1447 | dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy; | |
1448 | ||
1449 | dw->dma.device_prep_slave_sg = dwc_prep_slave_sg; | |
c3635c78 | 1450 | dw->dma.device_control = dwc_control; |
3bfb1d20 | 1451 | |
07934481 | 1452 | dw->dma.device_tx_status = dwc_tx_status; |
3bfb1d20 HS |
1453 | dw->dma.device_issue_pending = dwc_issue_pending; |
1454 | ||
1455 | dma_writel(dw, CFG, DW_CFG_DMA_EN); | |
1456 | ||
1457 | printk(KERN_INFO "%s: DesignWare DMA Controller, %d channels\n", | |
46389470 | 1458 | dev_name(&pdev->dev), pdata->nr_channels); |
3bfb1d20 HS |
1459 | |
1460 | dma_async_device_register(&dw->dma); | |
1461 | ||
1462 | return 0; | |
1463 | ||
1464 | err_irq: | |
1465 | clk_disable(dw->clk); | |
1466 | clk_put(dw->clk); | |
1467 | err_clk: | |
1468 | iounmap(dw->regs); | |
1469 | dw->regs = NULL; | |
1470 | err_release_r: | |
1471 | release_resource(io); | |
1472 | err_kfree: | |
1473 | kfree(dw); | |
1474 | return err; | |
1475 | } | |
1476 | ||
1477 | static int __exit dw_remove(struct platform_device *pdev) | |
1478 | { | |
1479 | struct dw_dma *dw = platform_get_drvdata(pdev); | |
1480 | struct dw_dma_chan *dwc, *_dwc; | |
1481 | struct resource *io; | |
1482 | ||
1483 | dw_dma_off(dw); | |
1484 | dma_async_device_unregister(&dw->dma); | |
1485 | ||
1486 | free_irq(platform_get_irq(pdev, 0), dw); | |
1487 | tasklet_kill(&dw->tasklet); | |
1488 | ||
1489 | list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels, | |
1490 | chan.device_node) { | |
1491 | list_del(&dwc->chan.device_node); | |
1492 | channel_clear_bit(dw, CH_EN, dwc->mask); | |
1493 | } | |
1494 | ||
1495 | clk_disable(dw->clk); | |
1496 | clk_put(dw->clk); | |
1497 | ||
1498 | iounmap(dw->regs); | |
1499 | dw->regs = NULL; | |
1500 | ||
1501 | io = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
1502 | release_mem_region(io->start, DW_REGLEN); | |
1503 | ||
1504 | kfree(dw); | |
1505 | ||
1506 | return 0; | |
1507 | } | |
1508 | ||
1509 | static void dw_shutdown(struct platform_device *pdev) | |
1510 | { | |
1511 | struct dw_dma *dw = platform_get_drvdata(pdev); | |
1512 | ||
1513 | dw_dma_off(platform_get_drvdata(pdev)); | |
1514 | clk_disable(dw->clk); | |
1515 | } | |
1516 | ||
4a256b5f | 1517 | static int dw_suspend_noirq(struct device *dev) |
3bfb1d20 | 1518 | { |
4a256b5f | 1519 | struct platform_device *pdev = to_platform_device(dev); |
3bfb1d20 HS |
1520 | struct dw_dma *dw = platform_get_drvdata(pdev); |
1521 | ||
1522 | dw_dma_off(platform_get_drvdata(pdev)); | |
1523 | clk_disable(dw->clk); | |
61e183f8 | 1524 | |
3bfb1d20 HS |
1525 | return 0; |
1526 | } | |
1527 | ||
4a256b5f | 1528 | static int dw_resume_noirq(struct device *dev) |
3bfb1d20 | 1529 | { |
4a256b5f | 1530 | struct platform_device *pdev = to_platform_device(dev); |
3bfb1d20 HS |
1531 | struct dw_dma *dw = platform_get_drvdata(pdev); |
1532 | ||
1533 | clk_enable(dw->clk); | |
1534 | dma_writel(dw, CFG, DW_CFG_DMA_EN); | |
1535 | return 0; | |
3bfb1d20 HS |
1536 | } |
1537 | ||
47145210 | 1538 | static const struct dev_pm_ops dw_dev_pm_ops = { |
4a256b5f MD |
1539 | .suspend_noirq = dw_suspend_noirq, |
1540 | .resume_noirq = dw_resume_noirq, | |
7414a1b8 RK |
1541 | .freeze_noirq = dw_suspend_noirq, |
1542 | .thaw_noirq = dw_resume_noirq, | |
1543 | .restore_noirq = dw_resume_noirq, | |
1544 | .poweroff_noirq = dw_suspend_noirq, | |
4a256b5f MD |
1545 | }; |
1546 | ||
3bfb1d20 HS |
1547 | static struct platform_driver dw_driver = { |
1548 | .remove = __exit_p(dw_remove), | |
1549 | .shutdown = dw_shutdown, | |
3bfb1d20 HS |
1550 | .driver = { |
1551 | .name = "dw_dmac", | |
4a256b5f | 1552 | .pm = &dw_dev_pm_ops, |
3bfb1d20 HS |
1553 | }, |
1554 | }; | |
1555 | ||
1556 | static int __init dw_init(void) | |
1557 | { | |
1558 | return platform_driver_probe(&dw_driver, dw_probe); | |
1559 | } | |
cb689a70 | 1560 | subsys_initcall(dw_init); |
3bfb1d20 HS |
1561 | |
1562 | static void __exit dw_exit(void) | |
1563 | { | |
1564 | platform_driver_unregister(&dw_driver); | |
1565 | } | |
1566 | module_exit(dw_exit); | |
1567 | ||
1568 | MODULE_LICENSE("GPL v2"); | |
1569 | MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller driver"); | |
e05503ef | 1570 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
aecb7b64 | 1571 | MODULE_AUTHOR("Viresh Kumar <viresh.kumar@st.com>"); |