1 // SPDX-License-Identifier: GPL-2.0
2 // (C) 2017-2018 Synopsys, Inc. (www.synopsys.com)
3
4 /*
5 * Synopsys DesignWare AXI DMA Controller driver.
6 *
7 * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
8 */
9
10 #include <linux/bitops.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/dmaengine.h>
14 #include <linux/dmapool.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/err.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/iopoll.h>
20 #include <linux/io-64-nonatomic-lo-hi.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/of_dma.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/property.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
30
31 #include "dw-axi-dmac.h"
32 #include "../dmaengine.h"
33 #include "../virt-dma.h"
34
35 /*
36 * The set of bus widths supported by the DMA controller. DW AXI DMAC supports
37 * master data bus width up to 512 bits (for both AXI master interfaces), but
38 * it depends on IP block configurarion.
39 */
40 #define AXI_DMA_BUSWIDTHS \
41 (DMA_SLAVE_BUSWIDTH_1_BYTE | \
42 DMA_SLAVE_BUSWIDTH_2_BYTES | \
43 DMA_SLAVE_BUSWIDTH_4_BYTES | \
44 DMA_SLAVE_BUSWIDTH_8_BYTES | \
45 DMA_SLAVE_BUSWIDTH_16_BYTES | \
46 DMA_SLAVE_BUSWIDTH_32_BYTES | \
47 DMA_SLAVE_BUSWIDTH_64_BYTES)
48
49 static inline void
axi_dma_iowrite32(struct axi_dma_chip * chip,u32 reg,u32 val)50 axi_dma_iowrite32(struct axi_dma_chip *chip, u32 reg, u32 val)
51 {
52 iowrite32(val, chip->regs + reg);
53 }
54
axi_dma_ioread32(struct axi_dma_chip * chip,u32 reg)55 static inline u32 axi_dma_ioread32(struct axi_dma_chip *chip, u32 reg)
56 {
57 return ioread32(chip->regs + reg);
58 }
59
60 static inline void
axi_chan_iowrite32(struct axi_dma_chan * chan,u32 reg,u32 val)61 axi_chan_iowrite32(struct axi_dma_chan *chan, u32 reg, u32 val)
62 {
63 iowrite32(val, chan->chan_regs + reg);
64 }
65
axi_chan_ioread32(struct axi_dma_chan * chan,u32 reg)66 static inline u32 axi_chan_ioread32(struct axi_dma_chan *chan, u32 reg)
67 {
68 return ioread32(chan->chan_regs + reg);
69 }
70
71 static inline void
axi_chan_iowrite64(struct axi_dma_chan * chan,u32 reg,u64 val)72 axi_chan_iowrite64(struct axi_dma_chan *chan, u32 reg, u64 val)
73 {
74 /*
75 * We split one 64 bit write for two 32 bit write as some HW doesn't
76 * support 64 bit access.
77 */
78 iowrite32(lower_32_bits(val), chan->chan_regs + reg);
79 iowrite32(upper_32_bits(val), chan->chan_regs + reg + 4);
80 }
81
axi_dma_disable(struct axi_dma_chip * chip)82 static inline void axi_dma_disable(struct axi_dma_chip *chip)
83 {
84 u32 val;
85
86 val = axi_dma_ioread32(chip, DMAC_CFG);
87 val &= ~DMAC_EN_MASK;
88 axi_dma_iowrite32(chip, DMAC_CFG, val);
89 }
90
axi_dma_enable(struct axi_dma_chip * chip)91 static inline void axi_dma_enable(struct axi_dma_chip *chip)
92 {
93 u32 val;
94
95 val = axi_dma_ioread32(chip, DMAC_CFG);
96 val |= DMAC_EN_MASK;
97 axi_dma_iowrite32(chip, DMAC_CFG, val);
98 }
99
axi_dma_irq_disable(struct axi_dma_chip * chip)100 static inline void axi_dma_irq_disable(struct axi_dma_chip *chip)
101 {
102 u32 val;
103
104 val = axi_dma_ioread32(chip, DMAC_CFG);
105 val &= ~INT_EN_MASK;
106 axi_dma_iowrite32(chip, DMAC_CFG, val);
107 }
108
axi_dma_irq_enable(struct axi_dma_chip * chip)109 static inline void axi_dma_irq_enable(struct axi_dma_chip *chip)
110 {
111 u32 val;
112
113 val = axi_dma_ioread32(chip, DMAC_CFG);
114 val |= INT_EN_MASK;
115 axi_dma_iowrite32(chip, DMAC_CFG, val);
116 }
117
axi_chan_irq_disable(struct axi_dma_chan * chan,u32 irq_mask)118 static inline void axi_chan_irq_disable(struct axi_dma_chan *chan, u32 irq_mask)
119 {
120 u32 val;
121
122 if (likely(irq_mask == DWAXIDMAC_IRQ_ALL)) {
123 axi_chan_iowrite32(chan, CH_INTSTATUS_ENA, DWAXIDMAC_IRQ_NONE);
124 } else {
125 val = axi_chan_ioread32(chan, CH_INTSTATUS_ENA);
126 val &= ~irq_mask;
127 axi_chan_iowrite32(chan, CH_INTSTATUS_ENA, val);
128 }
129 }
130
axi_chan_irq_set(struct axi_dma_chan * chan,u32 irq_mask)131 static inline void axi_chan_irq_set(struct axi_dma_chan *chan, u32 irq_mask)
132 {
133 axi_chan_iowrite32(chan, CH_INTSTATUS_ENA, irq_mask);
134 }
135
axi_chan_irq_sig_set(struct axi_dma_chan * chan,u32 irq_mask)136 static inline void axi_chan_irq_sig_set(struct axi_dma_chan *chan, u32 irq_mask)
137 {
138 axi_chan_iowrite32(chan, CH_INTSIGNAL_ENA, irq_mask);
139 }
140
axi_chan_irq_clear(struct axi_dma_chan * chan,u32 irq_mask)141 static inline void axi_chan_irq_clear(struct axi_dma_chan *chan, u32 irq_mask)
142 {
143 axi_chan_iowrite32(chan, CH_INTCLEAR, irq_mask);
144 }
145
axi_chan_irq_read(struct axi_dma_chan * chan)146 static inline u32 axi_chan_irq_read(struct axi_dma_chan *chan)
147 {
148 return axi_chan_ioread32(chan, CH_INTSTATUS);
149 }
150
axi_chan_disable(struct axi_dma_chan * chan)151 static inline void axi_chan_disable(struct axi_dma_chan *chan)
152 {
153 u32 val;
154
155 val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
156 val &= ~(BIT(chan->id) << DMAC_CHAN_EN_SHIFT);
157 val |= BIT(chan->id) << DMAC_CHAN_EN_WE_SHIFT;
158 axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
159 }
160
axi_chan_enable(struct axi_dma_chan * chan)161 static inline void axi_chan_enable(struct axi_dma_chan *chan)
162 {
163 u32 val;
164
165 val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
166 val |= BIT(chan->id) << DMAC_CHAN_EN_SHIFT |
167 BIT(chan->id) << DMAC_CHAN_EN_WE_SHIFT;
168 axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
169 }
170
axi_chan_is_hw_enable(struct axi_dma_chan * chan)171 static inline bool axi_chan_is_hw_enable(struct axi_dma_chan *chan)
172 {
173 u32 val;
174
175 val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
176
177 return !!(val & (BIT(chan->id) << DMAC_CHAN_EN_SHIFT));
178 }
179
axi_dma_hw_init(struct axi_dma_chip * chip)180 static void axi_dma_hw_init(struct axi_dma_chip *chip)
181 {
182 u32 i;
183
184 for (i = 0; i < chip->dw->hdata->nr_channels; i++) {
185 axi_chan_irq_disable(&chip->dw->chan[i], DWAXIDMAC_IRQ_ALL);
186 axi_chan_disable(&chip->dw->chan[i]);
187 }
188 }
189
axi_chan_get_xfer_width(struct axi_dma_chan * chan,dma_addr_t src,dma_addr_t dst,size_t len)190 static u32 axi_chan_get_xfer_width(struct axi_dma_chan *chan, dma_addr_t src,
191 dma_addr_t dst, size_t len)
192 {
193 u32 max_width = chan->chip->dw->hdata->m_data_width;
194
195 return __ffs(src | dst | len | BIT(max_width));
196 }
197
axi_chan_name(struct axi_dma_chan * chan)198 static inline const char *axi_chan_name(struct axi_dma_chan *chan)
199 {
200 return dma_chan_name(&chan->vc.chan);
201 }
202
axi_desc_alloc(u32 num)203 static struct axi_dma_desc *axi_desc_alloc(u32 num)
204 {
205 struct axi_dma_desc *desc;
206
207 desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
208 if (!desc)
209 return NULL;
210
211 desc->hw_desc = kcalloc(num, sizeof(*desc->hw_desc), GFP_NOWAIT);
212 if (!desc->hw_desc) {
213 kfree(desc);
214 return NULL;
215 }
216
217 return desc;
218 }
219
axi_desc_get(struct axi_dma_chan * chan,dma_addr_t * addr)220 static struct axi_dma_lli *axi_desc_get(struct axi_dma_chan *chan,
221 dma_addr_t *addr)
222 {
223 struct axi_dma_lli *lli;
224 dma_addr_t phys;
225
226 lli = dma_pool_zalloc(chan->desc_pool, GFP_NOWAIT, &phys);
227 if (unlikely(!lli)) {
228 dev_err(chan2dev(chan), "%s: not enough descriptors available\n",
229 axi_chan_name(chan));
230 return NULL;
231 }
232
233 atomic_inc(&chan->descs_allocated);
234 *addr = phys;
235
236 return lli;
237 }
238
axi_desc_put(struct axi_dma_desc * desc)239 static void axi_desc_put(struct axi_dma_desc *desc)
240 {
241 struct axi_dma_chan *chan = desc->chan;
242 int count = atomic_read(&chan->descs_allocated);
243 struct axi_dma_hw_desc *hw_desc;
244 int descs_put;
245
246 for (descs_put = 0; descs_put < count; descs_put++) {
247 hw_desc = &desc->hw_desc[descs_put];
248 dma_pool_free(chan->desc_pool, hw_desc->lli, hw_desc->llp);
249 }
250
251 kfree(desc->hw_desc);
252 kfree(desc);
253 atomic_sub(descs_put, &chan->descs_allocated);
254 dev_vdbg(chan2dev(chan), "%s: %d descs put, %d still allocated\n",
255 axi_chan_name(chan), descs_put,
256 atomic_read(&chan->descs_allocated));
257 }
258
vchan_desc_put(struct virt_dma_desc * vdesc)259 static void vchan_desc_put(struct virt_dma_desc *vdesc)
260 {
261 axi_desc_put(vd_to_axi_desc(vdesc));
262 }
263
264 static enum dma_status
dma_chan_tx_status(struct dma_chan * dchan,dma_cookie_t cookie,struct dma_tx_state * txstate)265 dma_chan_tx_status(struct dma_chan *dchan, dma_cookie_t cookie,
266 struct dma_tx_state *txstate)
267 {
268 struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
269 struct virt_dma_desc *vdesc;
270 enum dma_status status;
271 u32 completed_length;
272 unsigned long flags;
273 u32 completed_blocks;
274 size_t bytes = 0;
275 u32 length;
276 u32 len;
277
278 status = dma_cookie_status(dchan, cookie, txstate);
279 if (status == DMA_COMPLETE || !txstate)
280 return status;
281
282 spin_lock_irqsave(&chan->vc.lock, flags);
283
284 vdesc = vchan_find_desc(&chan->vc, cookie);
285 if (vdesc) {
286 length = vd_to_axi_desc(vdesc)->length;
287 completed_blocks = vd_to_axi_desc(vdesc)->completed_blocks;
288 len = vd_to_axi_desc(vdesc)->hw_desc[0].len;
289 completed_length = completed_blocks * len;
290 bytes = length - completed_length;
291 }
292
293 spin_unlock_irqrestore(&chan->vc.lock, flags);
294 dma_set_residue(txstate, bytes);
295
296 return status;
297 }
298
write_desc_llp(struct axi_dma_hw_desc * desc,dma_addr_t adr)299 static void write_desc_llp(struct axi_dma_hw_desc *desc, dma_addr_t adr)
300 {
301 desc->lli->llp = cpu_to_le64(adr);
302 }
303
write_chan_llp(struct axi_dma_chan * chan,dma_addr_t adr)304 static void write_chan_llp(struct axi_dma_chan *chan, dma_addr_t adr)
305 {
306 axi_chan_iowrite64(chan, CH_LLP, adr);
307 }
308
dw_axi_dma_set_byte_halfword(struct axi_dma_chan * chan,bool set)309 static void dw_axi_dma_set_byte_halfword(struct axi_dma_chan *chan, bool set)
310 {
311 u32 offset = DMAC_APB_BYTE_WR_CH_EN;
312 u32 reg_width, val;
313
314 if (!chan->chip->apb_regs) {
315 dev_dbg(chan->chip->dev, "apb_regs not initialized\n");
316 return;
317 }
318
319 reg_width = __ffs(chan->config.dst_addr_width);
320 if (reg_width == DWAXIDMAC_TRANS_WIDTH_16)
321 offset = DMAC_APB_HALFWORD_WR_CH_EN;
322
323 val = ioread32(chan->chip->apb_regs + offset);
324
325 if (set)
326 val |= BIT(chan->id);
327 else
328 val &= ~BIT(chan->id);
329
330 iowrite32(val, chan->chip->apb_regs + offset);
331 }
332 /* Called in chan locked context */
axi_chan_block_xfer_start(struct axi_dma_chan * chan,struct axi_dma_desc * first)333 static void axi_chan_block_xfer_start(struct axi_dma_chan *chan,
334 struct axi_dma_desc *first)
335 {
336 u32 priority = chan->chip->dw->hdata->priority[chan->id];
337 u32 reg, irq_mask;
338 u8 lms = 0; /* Select AXI0 master for LLI fetching */
339
340 if (unlikely(axi_chan_is_hw_enable(chan))) {
341 dev_err(chan2dev(chan), "%s is non-idle!\n",
342 axi_chan_name(chan));
343
344 return;
345 }
346
347 axi_dma_enable(chan->chip);
348
349 reg = (DWAXIDMAC_MBLK_TYPE_LL << CH_CFG_L_DST_MULTBLK_TYPE_POS |
350 DWAXIDMAC_MBLK_TYPE_LL << CH_CFG_L_SRC_MULTBLK_TYPE_POS);
351 axi_chan_iowrite32(chan, CH_CFG_L, reg);
352
353 reg = (DWAXIDMAC_TT_FC_MEM_TO_MEM_DMAC << CH_CFG_H_TT_FC_POS |
354 priority << CH_CFG_H_PRIORITY_POS |
355 DWAXIDMAC_HS_SEL_HW << CH_CFG_H_HS_SEL_DST_POS |
356 DWAXIDMAC_HS_SEL_HW << CH_CFG_H_HS_SEL_SRC_POS);
357 switch (chan->direction) {
358 case DMA_MEM_TO_DEV:
359 dw_axi_dma_set_byte_halfword(chan, true);
360 reg |= (chan->config.device_fc ?
361 DWAXIDMAC_TT_FC_MEM_TO_PER_DST :
362 DWAXIDMAC_TT_FC_MEM_TO_PER_DMAC)
363 << CH_CFG_H_TT_FC_POS;
364 if (chan->chip->apb_regs)
365 reg |= (chan->id << CH_CFG_H_DST_PER_POS);
366 break;
367 case DMA_DEV_TO_MEM:
368 reg |= (chan->config.device_fc ?
369 DWAXIDMAC_TT_FC_PER_TO_MEM_SRC :
370 DWAXIDMAC_TT_FC_PER_TO_MEM_DMAC)
371 << CH_CFG_H_TT_FC_POS;
372 if (chan->chip->apb_regs)
373 reg |= (chan->id << CH_CFG_H_SRC_PER_POS);
374 break;
375 default:
376 break;
377 }
378 axi_chan_iowrite32(chan, CH_CFG_H, reg);
379
380 write_chan_llp(chan, first->hw_desc[0].llp | lms);
381
382 irq_mask = DWAXIDMAC_IRQ_DMA_TRF | DWAXIDMAC_IRQ_ALL_ERR;
383 axi_chan_irq_sig_set(chan, irq_mask);
384
385 /* Generate 'suspend' status but don't generate interrupt */
386 irq_mask |= DWAXIDMAC_IRQ_SUSPENDED;
387 axi_chan_irq_set(chan, irq_mask);
388
389 axi_chan_enable(chan);
390 }
391
axi_chan_start_first_queued(struct axi_dma_chan * chan)392 static void axi_chan_start_first_queued(struct axi_dma_chan *chan)
393 {
394 struct axi_dma_desc *desc;
395 struct virt_dma_desc *vd;
396
397 vd = vchan_next_desc(&chan->vc);
398 if (!vd)
399 return;
400
401 desc = vd_to_axi_desc(vd);
402 dev_vdbg(chan2dev(chan), "%s: started %u\n", axi_chan_name(chan),
403 vd->tx.cookie);
404 axi_chan_block_xfer_start(chan, desc);
405 }
406
dma_chan_issue_pending(struct dma_chan * dchan)407 static void dma_chan_issue_pending(struct dma_chan *dchan)
408 {
409 struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
410 unsigned long flags;
411
412 spin_lock_irqsave(&chan->vc.lock, flags);
413 if (vchan_issue_pending(&chan->vc))
414 axi_chan_start_first_queued(chan);
415 spin_unlock_irqrestore(&chan->vc.lock, flags);
416 }
417
dw_axi_dma_synchronize(struct dma_chan * dchan)418 static void dw_axi_dma_synchronize(struct dma_chan *dchan)
419 {
420 struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
421
422 vchan_synchronize(&chan->vc);
423 }
424
dma_chan_alloc_chan_resources(struct dma_chan * dchan)425 static int dma_chan_alloc_chan_resources(struct dma_chan *dchan)
426 {
427 struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
428
429 /* ASSERT: channel is idle */
430 if (axi_chan_is_hw_enable(chan)) {
431 dev_err(chan2dev(chan), "%s is non-idle!\n",
432 axi_chan_name(chan));
433 return -EBUSY;
434 }
435
436 /* LLI address must be aligned to a 64-byte boundary */
437 chan->desc_pool = dma_pool_create(dev_name(chan2dev(chan)),
438 chan->chip->dev,
439 sizeof(struct axi_dma_lli),
440 64, 0);
441 if (!chan->desc_pool) {
442 dev_err(chan2dev(chan), "No memory for descriptors\n");
443 return -ENOMEM;
444 }
445 dev_vdbg(dchan2dev(dchan), "%s: allocating\n", axi_chan_name(chan));
446
447 pm_runtime_get(chan->chip->dev);
448
449 return 0;
450 }
451
dma_chan_free_chan_resources(struct dma_chan * dchan)452 static void dma_chan_free_chan_resources(struct dma_chan *dchan)
453 {
454 struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
455
456 /* ASSERT: channel is idle */
457 if (axi_chan_is_hw_enable(chan))
458 dev_err(dchan2dev(dchan), "%s is non-idle!\n",
459 axi_chan_name(chan));
460
461 axi_chan_disable(chan);
462 axi_chan_irq_disable(chan, DWAXIDMAC_IRQ_ALL);
463
464 vchan_free_chan_resources(&chan->vc);
465
466 dma_pool_destroy(chan->desc_pool);
467 chan->desc_pool = NULL;
468 dev_vdbg(dchan2dev(dchan),
469 "%s: free resources, descriptor still allocated: %u\n",
470 axi_chan_name(chan), atomic_read(&chan->descs_allocated));
471
472 pm_runtime_put(chan->chip->dev);
473 }
474
dw_axi_dma_set_hw_channel(struct axi_dma_chan * chan,bool set)475 static void dw_axi_dma_set_hw_channel(struct axi_dma_chan *chan, bool set)
476 {
477 struct axi_dma_chip *chip = chan->chip;
478 unsigned long reg_value, val;
479
480 if (!chip->apb_regs) {
481 dev_err(chip->dev, "apb_regs not initialized\n");
482 return;
483 }
484
485 /*
486 * An unused DMA channel has a default value of 0x3F.
487 * Lock the DMA channel by assign a handshake number to the channel.
488 * Unlock the DMA channel by assign 0x3F to the channel.
489 */
490 if (set)
491 val = chan->hw_handshake_num;
492 else
493 val = UNUSED_CHANNEL;
494
495 reg_value = lo_hi_readq(chip->apb_regs + DMAC_APB_HW_HS_SEL_0);
496
497 /* Channel is already allocated, set handshake as per channel ID */
498 /* 64 bit write should handle for 8 channels */
499
500 reg_value &= ~(DMA_APB_HS_SEL_MASK <<
501 (chan->id * DMA_APB_HS_SEL_BIT_SIZE));
502 reg_value |= (val << (chan->id * DMA_APB_HS_SEL_BIT_SIZE));
503 lo_hi_writeq(reg_value, chip->apb_regs + DMAC_APB_HW_HS_SEL_0);
504
505 return;
506 }
507
508 /*
509 * If DW_axi_dmac sees CHx_CTL.ShadowReg_Or_LLI_Last bit of the fetched LLI
510 * as 1, it understands that the current block is the final block in the
511 * transfer and completes the DMA transfer operation at the end of current
512 * block transfer.
513 */
set_desc_last(struct axi_dma_hw_desc * desc)514 static void set_desc_last(struct axi_dma_hw_desc *desc)
515 {
516 u32 val;
517
518 val = le32_to_cpu(desc->lli->ctl_hi);
519 val |= CH_CTL_H_LLI_LAST;
520 desc->lli->ctl_hi = cpu_to_le32(val);
521 }
522
write_desc_sar(struct axi_dma_hw_desc * desc,dma_addr_t adr)523 static void write_desc_sar(struct axi_dma_hw_desc *desc, dma_addr_t adr)
524 {
525 desc->lli->sar = cpu_to_le64(adr);
526 }
527
write_desc_dar(struct axi_dma_hw_desc * desc,dma_addr_t adr)528 static void write_desc_dar(struct axi_dma_hw_desc *desc, dma_addr_t adr)
529 {
530 desc->lli->dar = cpu_to_le64(adr);
531 }
532
set_desc_src_master(struct axi_dma_hw_desc * desc)533 static void set_desc_src_master(struct axi_dma_hw_desc *desc)
534 {
535 u32 val;
536
537 /* Select AXI0 for source master */
538 val = le32_to_cpu(desc->lli->ctl_lo);
539 val &= ~CH_CTL_L_SRC_MAST;
540 desc->lli->ctl_lo = cpu_to_le32(val);
541 }
542
set_desc_dest_master(struct axi_dma_hw_desc * hw_desc,struct axi_dma_desc * desc)543 static void set_desc_dest_master(struct axi_dma_hw_desc *hw_desc,
544 struct axi_dma_desc *desc)
545 {
546 u32 val;
547
548 /* Select AXI1 for source master if available */
549 val = le32_to_cpu(hw_desc->lli->ctl_lo);
550 if (desc->chan->chip->dw->hdata->nr_masters > 1)
551 val |= CH_CTL_L_DST_MAST;
552 else
553 val &= ~CH_CTL_L_DST_MAST;
554
555 hw_desc->lli->ctl_lo = cpu_to_le32(val);
556 }
557
dw_axi_dma_set_hw_desc(struct axi_dma_chan * chan,struct axi_dma_hw_desc * hw_desc,dma_addr_t mem_addr,size_t len)558 static int dw_axi_dma_set_hw_desc(struct axi_dma_chan *chan,
559 struct axi_dma_hw_desc *hw_desc,
560 dma_addr_t mem_addr, size_t len)
561 {
562 unsigned int data_width = BIT(chan->chip->dw->hdata->m_data_width);
563 unsigned int reg_width;
564 unsigned int mem_width;
565 dma_addr_t device_addr;
566 size_t axi_block_ts;
567 size_t block_ts;
568 u32 ctllo, ctlhi;
569 u32 burst_len;
570
571 axi_block_ts = chan->chip->dw->hdata->block_size[chan->id];
572
573 mem_width = __ffs(data_width | mem_addr | len);
574 if (mem_width > DWAXIDMAC_TRANS_WIDTH_32)
575 mem_width = DWAXIDMAC_TRANS_WIDTH_32;
576
577 if (!IS_ALIGNED(mem_addr, 4)) {
578 dev_err(chan->chip->dev, "invalid buffer alignment\n");
579 return -EINVAL;
580 }
581
582 switch (chan->direction) {
583 case DMA_MEM_TO_DEV:
584 reg_width = __ffs(chan->config.dst_addr_width);
585 device_addr = chan->config.dst_addr;
586 ctllo = reg_width << CH_CTL_L_DST_WIDTH_POS |
587 mem_width << CH_CTL_L_SRC_WIDTH_POS |
588 DWAXIDMAC_CH_CTL_L_NOINC << CH_CTL_L_DST_INC_POS |
589 DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_SRC_INC_POS;
590 block_ts = len >> mem_width;
591 break;
592 case DMA_DEV_TO_MEM:
593 reg_width = __ffs(chan->config.src_addr_width);
594 device_addr = chan->config.src_addr;
595 ctllo = reg_width << CH_CTL_L_SRC_WIDTH_POS |
596 mem_width << CH_CTL_L_DST_WIDTH_POS |
597 DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_DST_INC_POS |
598 DWAXIDMAC_CH_CTL_L_NOINC << CH_CTL_L_SRC_INC_POS;
599 block_ts = len >> reg_width;
600 break;
601 default:
602 return -EINVAL;
603 }
604
605 if (block_ts > axi_block_ts)
606 return -EINVAL;
607
608 hw_desc->lli = axi_desc_get(chan, &hw_desc->llp);
609 if (unlikely(!hw_desc->lli))
610 return -ENOMEM;
611
612 ctlhi = CH_CTL_H_LLI_VALID;
613
614 if (chan->chip->dw->hdata->restrict_axi_burst_len) {
615 burst_len = chan->chip->dw->hdata->axi_rw_burst_len;
616 ctlhi |= CH_CTL_H_ARLEN_EN | CH_CTL_H_AWLEN_EN |
617 burst_len << CH_CTL_H_ARLEN_POS |
618 burst_len << CH_CTL_H_AWLEN_POS;
619 }
620
621 hw_desc->lli->ctl_hi = cpu_to_le32(ctlhi);
622
623 if (chan->direction == DMA_MEM_TO_DEV) {
624 write_desc_sar(hw_desc, mem_addr);
625 write_desc_dar(hw_desc, device_addr);
626 } else {
627 write_desc_sar(hw_desc, device_addr);
628 write_desc_dar(hw_desc, mem_addr);
629 }
630
631 hw_desc->lli->block_ts_lo = cpu_to_le32(block_ts - 1);
632
633 ctllo |= DWAXIDMAC_BURST_TRANS_LEN_4 << CH_CTL_L_DST_MSIZE_POS |
634 DWAXIDMAC_BURST_TRANS_LEN_4 << CH_CTL_L_SRC_MSIZE_POS;
635 hw_desc->lli->ctl_lo = cpu_to_le32(ctllo);
636
637 set_desc_src_master(hw_desc);
638
639 hw_desc->len = len;
640 return 0;
641 }
642
calculate_block_len(struct axi_dma_chan * chan,dma_addr_t dma_addr,size_t buf_len,enum dma_transfer_direction direction)643 static size_t calculate_block_len(struct axi_dma_chan *chan,
644 dma_addr_t dma_addr, size_t buf_len,
645 enum dma_transfer_direction direction)
646 {
647 u32 data_width, reg_width, mem_width;
648 size_t axi_block_ts, block_len;
649
650 axi_block_ts = chan->chip->dw->hdata->block_size[chan->id];
651
652 switch (direction) {
653 case DMA_MEM_TO_DEV:
654 data_width = BIT(chan->chip->dw->hdata->m_data_width);
655 mem_width = __ffs(data_width | dma_addr | buf_len);
656 if (mem_width > DWAXIDMAC_TRANS_WIDTH_32)
657 mem_width = DWAXIDMAC_TRANS_WIDTH_32;
658
659 block_len = axi_block_ts << mem_width;
660 break;
661 case DMA_DEV_TO_MEM:
662 reg_width = __ffs(chan->config.src_addr_width);
663 block_len = axi_block_ts << reg_width;
664 break;
665 default:
666 block_len = 0;
667 }
668
669 return block_len;
670 }
671
672 static struct dma_async_tx_descriptor *
dw_axi_dma_chan_prep_cyclic(struct dma_chan * dchan,dma_addr_t dma_addr,size_t buf_len,size_t period_len,enum dma_transfer_direction direction,unsigned long flags)673 dw_axi_dma_chan_prep_cyclic(struct dma_chan *dchan, dma_addr_t dma_addr,
674 size_t buf_len, size_t period_len,
675 enum dma_transfer_direction direction,
676 unsigned long flags)
677 {
678 struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
679 struct axi_dma_hw_desc *hw_desc = NULL;
680 struct axi_dma_desc *desc = NULL;
681 dma_addr_t src_addr = dma_addr;
682 u32 num_periods, num_segments;
683 size_t axi_block_len;
684 u32 total_segments;
685 u32 segment_len;
686 unsigned int i;
687 int status;
688 u64 llp = 0;
689 u8 lms = 0; /* Select AXI0 master for LLI fetching */
690
691 num_periods = buf_len / period_len;
692
693 axi_block_len = calculate_block_len(chan, dma_addr, buf_len, direction);
694 if (axi_block_len == 0)
695 return NULL;
696
697 num_segments = DIV_ROUND_UP(period_len, axi_block_len);
698 segment_len = DIV_ROUND_UP(period_len, num_segments);
699
700 total_segments = num_periods * num_segments;
701
702 desc = axi_desc_alloc(total_segments);
703 if (unlikely(!desc))
704 goto err_desc_get;
705
706 chan->direction = direction;
707 desc->chan = chan;
708 chan->cyclic = true;
709 desc->length = 0;
710 desc->period_len = period_len;
711
712 for (i = 0; i < total_segments; i++) {
713 hw_desc = &desc->hw_desc[i];
714
715 status = dw_axi_dma_set_hw_desc(chan, hw_desc, src_addr,
716 segment_len);
717 if (status < 0)
718 goto err_desc_get;
719
720 desc->length += hw_desc->len;
721 /* Set end-of-link to the linked descriptor, so that cyclic
722 * callback function can be triggered during interrupt.
723 */
724 set_desc_last(hw_desc);
725
726 src_addr += segment_len;
727 }
728
729 llp = desc->hw_desc[0].llp;
730
731 /* Managed transfer list */
732 do {
733 hw_desc = &desc->hw_desc[--total_segments];
734 write_desc_llp(hw_desc, llp | lms);
735 llp = hw_desc->llp;
736 } while (total_segments);
737
738 dw_axi_dma_set_hw_channel(chan, true);
739
740 return vchan_tx_prep(&chan->vc, &desc->vd, flags);
741
742 err_desc_get:
743 if (desc)
744 axi_desc_put(desc);
745
746 return NULL;
747 }
748
749 static struct dma_async_tx_descriptor *
dw_axi_dma_chan_prep_slave_sg(struct dma_chan * dchan,struct scatterlist * sgl,unsigned int sg_len,enum dma_transfer_direction direction,unsigned long flags,void * context)750 dw_axi_dma_chan_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
751 unsigned int sg_len,
752 enum dma_transfer_direction direction,
753 unsigned long flags, void *context)
754 {
755 struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
756 struct axi_dma_hw_desc *hw_desc = NULL;
757 struct axi_dma_desc *desc = NULL;
758 u32 num_segments, segment_len;
759 unsigned int loop = 0;
760 struct scatterlist *sg;
761 size_t axi_block_len;
762 u32 len, num_sgs = 0;
763 unsigned int i;
764 dma_addr_t mem;
765 int status;
766 u64 llp = 0;
767 u8 lms = 0; /* Select AXI0 master for LLI fetching */
768
769 if (unlikely(!is_slave_direction(direction) || !sg_len))
770 return NULL;
771
772 mem = sg_dma_address(sgl);
773 len = sg_dma_len(sgl);
774
775 axi_block_len = calculate_block_len(chan, mem, len, direction);
776 if (axi_block_len == 0)
777 return NULL;
778
779 for_each_sg(sgl, sg, sg_len, i)
780 num_sgs += DIV_ROUND_UP(sg_dma_len(sg), axi_block_len);
781
782 desc = axi_desc_alloc(num_sgs);
783 if (unlikely(!desc))
784 goto err_desc_get;
785
786 desc->chan = chan;
787 desc->length = 0;
788 chan->direction = direction;
789
790 for_each_sg(sgl, sg, sg_len, i) {
791 mem = sg_dma_address(sg);
792 len = sg_dma_len(sg);
793 num_segments = DIV_ROUND_UP(sg_dma_len(sg), axi_block_len);
794 segment_len = DIV_ROUND_UP(sg_dma_len(sg), num_segments);
795
796 do {
797 hw_desc = &desc->hw_desc[loop++];
798 status = dw_axi_dma_set_hw_desc(chan, hw_desc, mem, segment_len);
799 if (status < 0)
800 goto err_desc_get;
801
802 desc->length += hw_desc->len;
803 len -= segment_len;
804 mem += segment_len;
805 } while (len >= segment_len);
806 }
807
808 /* Set end-of-link to the last link descriptor of list */
809 set_desc_last(&desc->hw_desc[num_sgs - 1]);
810
811 /* Managed transfer list */
812 do {
813 hw_desc = &desc->hw_desc[--num_sgs];
814 write_desc_llp(hw_desc, llp | lms);
815 llp = hw_desc->llp;
816 } while (num_sgs);
817
818 dw_axi_dma_set_hw_channel(chan, true);
819
820 return vchan_tx_prep(&chan->vc, &desc->vd, flags);
821
822 err_desc_get:
823 if (desc)
824 axi_desc_put(desc);
825
826 return NULL;
827 }
828
829 static struct dma_async_tx_descriptor *
dma_chan_prep_dma_memcpy(struct dma_chan * dchan,dma_addr_t dst_adr,dma_addr_t src_adr,size_t len,unsigned long flags)830 dma_chan_prep_dma_memcpy(struct dma_chan *dchan, dma_addr_t dst_adr,
831 dma_addr_t src_adr, size_t len, unsigned long flags)
832 {
833 struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
834 size_t block_ts, max_block_ts, xfer_len;
835 struct axi_dma_hw_desc *hw_desc = NULL;
836 struct axi_dma_desc *desc = NULL;
837 u32 xfer_width, reg, num;
838 u64 llp = 0;
839 u8 lms = 0; /* Select AXI0 master for LLI fetching */
840
841 dev_dbg(chan2dev(chan), "%s: memcpy: src: %pad dst: %pad length: %zd flags: %#lx",
842 axi_chan_name(chan), &src_adr, &dst_adr, len, flags);
843
844 max_block_ts = chan->chip->dw->hdata->block_size[chan->id];
845 xfer_width = axi_chan_get_xfer_width(chan, src_adr, dst_adr, len);
846 num = DIV_ROUND_UP(len, max_block_ts << xfer_width);
847 desc = axi_desc_alloc(num);
848 if (unlikely(!desc))
849 goto err_desc_get;
850
851 desc->chan = chan;
852 num = 0;
853 desc->length = 0;
854 while (len) {
855 xfer_len = len;
856
857 hw_desc = &desc->hw_desc[num];
858 /*
859 * Take care for the alignment.
860 * Actually source and destination widths can be different, but
861 * make them same to be simpler.
862 */
863 xfer_width = axi_chan_get_xfer_width(chan, src_adr, dst_adr, xfer_len);
864
865 /*
866 * block_ts indicates the total number of data of width
867 * to be transferred in a DMA block transfer.
868 * BLOCK_TS register should be set to block_ts - 1
869 */
870 block_ts = xfer_len >> xfer_width;
871 if (block_ts > max_block_ts) {
872 block_ts = max_block_ts;
873 xfer_len = max_block_ts << xfer_width;
874 }
875
876 hw_desc->lli = axi_desc_get(chan, &hw_desc->llp);
877 if (unlikely(!hw_desc->lli))
878 goto err_desc_get;
879
880 write_desc_sar(hw_desc, src_adr);
881 write_desc_dar(hw_desc, dst_adr);
882 hw_desc->lli->block_ts_lo = cpu_to_le32(block_ts - 1);
883
884 reg = CH_CTL_H_LLI_VALID;
885 if (chan->chip->dw->hdata->restrict_axi_burst_len) {
886 u32 burst_len = chan->chip->dw->hdata->axi_rw_burst_len;
887
888 reg |= (CH_CTL_H_ARLEN_EN |
889 burst_len << CH_CTL_H_ARLEN_POS |
890 CH_CTL_H_AWLEN_EN |
891 burst_len << CH_CTL_H_AWLEN_POS);
892 }
893 hw_desc->lli->ctl_hi = cpu_to_le32(reg);
894
895 reg = (DWAXIDMAC_BURST_TRANS_LEN_4 << CH_CTL_L_DST_MSIZE_POS |
896 DWAXIDMAC_BURST_TRANS_LEN_4 << CH_CTL_L_SRC_MSIZE_POS |
897 xfer_width << CH_CTL_L_DST_WIDTH_POS |
898 xfer_width << CH_CTL_L_SRC_WIDTH_POS |
899 DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_DST_INC_POS |
900 DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_SRC_INC_POS);
901 hw_desc->lli->ctl_lo = cpu_to_le32(reg);
902
903 set_desc_src_master(hw_desc);
904 set_desc_dest_master(hw_desc, desc);
905
906 hw_desc->len = xfer_len;
907 desc->length += hw_desc->len;
908 /* update the length and addresses for the next loop cycle */
909 len -= xfer_len;
910 dst_adr += xfer_len;
911 src_adr += xfer_len;
912 num++;
913 }
914
915 /* Set end-of-link to the last link descriptor of list */
916 set_desc_last(&desc->hw_desc[num - 1]);
917 /* Managed transfer list */
918 do {
919 hw_desc = &desc->hw_desc[--num];
920 write_desc_llp(hw_desc, llp | lms);
921 llp = hw_desc->llp;
922 } while (num);
923
924 return vchan_tx_prep(&chan->vc, &desc->vd, flags);
925
926 err_desc_get:
927 if (desc)
928 axi_desc_put(desc);
929 return NULL;
930 }
931
dw_axi_dma_chan_slave_config(struct dma_chan * dchan,struct dma_slave_config * config)932 static int dw_axi_dma_chan_slave_config(struct dma_chan *dchan,
933 struct dma_slave_config *config)
934 {
935 struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
936
937 memcpy(&chan->config, config, sizeof(*config));
938
939 return 0;
940 }
941
axi_chan_dump_lli(struct axi_dma_chan * chan,struct axi_dma_hw_desc * desc)942 static void axi_chan_dump_lli(struct axi_dma_chan *chan,
943 struct axi_dma_hw_desc *desc)
944 {
945 if (!desc->lli) {
946 dev_err(dchan2dev(&chan->vc.chan), "NULL LLI\n");
947 return;
948 }
949
950 dev_err(dchan2dev(&chan->vc.chan),
951 "SAR: 0x%llx DAR: 0x%llx LLP: 0x%llx BTS 0x%x CTL: 0x%x:%08x",
952 le64_to_cpu(desc->lli->sar),
953 le64_to_cpu(desc->lli->dar),
954 le64_to_cpu(desc->lli->llp),
955 le32_to_cpu(desc->lli->block_ts_lo),
956 le32_to_cpu(desc->lli->ctl_hi),
957 le32_to_cpu(desc->lli->ctl_lo));
958 }
959
axi_chan_list_dump_lli(struct axi_dma_chan * chan,struct axi_dma_desc * desc_head)960 static void axi_chan_list_dump_lli(struct axi_dma_chan *chan,
961 struct axi_dma_desc *desc_head)
962 {
963 int count = atomic_read(&chan->descs_allocated);
964 int i;
965
966 for (i = 0; i < count; i++)
967 axi_chan_dump_lli(chan, &desc_head->hw_desc[i]);
968 }
969
axi_chan_handle_err(struct axi_dma_chan * chan,u32 status)970 static noinline void axi_chan_handle_err(struct axi_dma_chan *chan, u32 status)
971 {
972 struct virt_dma_desc *vd;
973 unsigned long flags;
974
975 spin_lock_irqsave(&chan->vc.lock, flags);
976
977 axi_chan_disable(chan);
978
979 /* The bad descriptor currently is in the head of vc list */
980 vd = vchan_next_desc(&chan->vc);
981 if (!vd) {
982 dev_err(chan2dev(chan), "BUG: %s, IRQ with no descriptors\n",
983 axi_chan_name(chan));
984 goto out;
985 }
986 /* Remove the completed descriptor from issued list */
987 list_del(&vd->node);
988
989 /* WARN about bad descriptor */
990 dev_err(chan2dev(chan),
991 "Bad descriptor submitted for %s, cookie: %d, irq: 0x%08x\n",
992 axi_chan_name(chan), vd->tx.cookie, status);
993 axi_chan_list_dump_lli(chan, vd_to_axi_desc(vd));
994
995 vchan_cookie_complete(vd);
996
997 /* Try to restart the controller */
998 axi_chan_start_first_queued(chan);
999
1000 out:
1001 spin_unlock_irqrestore(&chan->vc.lock, flags);
1002 }
1003
axi_chan_block_xfer_complete(struct axi_dma_chan * chan)1004 static void axi_chan_block_xfer_complete(struct axi_dma_chan *chan)
1005 {
1006 int count = atomic_read(&chan->descs_allocated);
1007 struct axi_dma_hw_desc *hw_desc;
1008 struct axi_dma_desc *desc;
1009 struct virt_dma_desc *vd;
1010 unsigned long flags;
1011 u64 llp;
1012 int i;
1013
1014 spin_lock_irqsave(&chan->vc.lock, flags);
1015 if (unlikely(axi_chan_is_hw_enable(chan))) {
1016 dev_err(chan2dev(chan), "BUG: %s caught DWAXIDMAC_IRQ_DMA_TRF, but channel not idle!\n",
1017 axi_chan_name(chan));
1018 axi_chan_disable(chan);
1019 }
1020
1021 /* The completed descriptor currently is in the head of vc list */
1022 vd = vchan_next_desc(&chan->vc);
1023 if (!vd) {
1024 dev_err(chan2dev(chan), "BUG: %s, IRQ with no descriptors\n",
1025 axi_chan_name(chan));
1026 goto out;
1027 }
1028
1029 if (chan->cyclic) {
1030 desc = vd_to_axi_desc(vd);
1031 if (desc) {
1032 llp = lo_hi_readq(chan->chan_regs + CH_LLP);
1033 for (i = 0; i < count; i++) {
1034 hw_desc = &desc->hw_desc[i];
1035 if (hw_desc->llp == llp) {
1036 axi_chan_irq_clear(chan, hw_desc->lli->status_lo);
1037 hw_desc->lli->ctl_hi |= CH_CTL_H_LLI_VALID;
1038 desc->completed_blocks = i;
1039
1040 if (((hw_desc->len * (i + 1)) % desc->period_len) == 0)
1041 vchan_cyclic_callback(vd);
1042 break;
1043 }
1044 }
1045
1046 axi_chan_enable(chan);
1047 }
1048 } else {
1049 /* Remove the completed descriptor from issued list before completing */
1050 list_del(&vd->node);
1051 vchan_cookie_complete(vd);
1052
1053 /* Submit queued descriptors after processing the completed ones */
1054 axi_chan_start_first_queued(chan);
1055 }
1056
1057 out:
1058 spin_unlock_irqrestore(&chan->vc.lock, flags);
1059 }
1060
dw_axi_dma_interrupt(int irq,void * dev_id)1061 static irqreturn_t dw_axi_dma_interrupt(int irq, void *dev_id)
1062 {
1063 struct axi_dma_chip *chip = dev_id;
1064 struct dw_axi_dma *dw = chip->dw;
1065 struct axi_dma_chan *chan;
1066
1067 u32 status, i;
1068
1069 /* Disable DMAC inerrupts. We'll enable them after processing chanels */
1070 axi_dma_irq_disable(chip);
1071
1072 /* Poll, clear and process every chanel interrupt status */
1073 for (i = 0; i < dw->hdata->nr_channels; i++) {
1074 chan = &dw->chan[i];
1075 status = axi_chan_irq_read(chan);
1076 axi_chan_irq_clear(chan, status);
1077
1078 dev_vdbg(chip->dev, "%s %u IRQ status: 0x%08x\n",
1079 axi_chan_name(chan), i, status);
1080
1081 if (status & DWAXIDMAC_IRQ_ALL_ERR)
1082 axi_chan_handle_err(chan, status);
1083 else if (status & DWAXIDMAC_IRQ_DMA_TRF)
1084 axi_chan_block_xfer_complete(chan);
1085 }
1086
1087 /* Re-enable interrupts */
1088 axi_dma_irq_enable(chip);
1089
1090 return IRQ_HANDLED;
1091 }
1092
dma_chan_terminate_all(struct dma_chan * dchan)1093 static int dma_chan_terminate_all(struct dma_chan *dchan)
1094 {
1095 struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
1096 u32 chan_active = BIT(chan->id) << DMAC_CHAN_EN_SHIFT;
1097 unsigned long flags;
1098 u32 val;
1099 int ret;
1100 LIST_HEAD(head);
1101
1102 axi_chan_disable(chan);
1103
1104 ret = readl_poll_timeout_atomic(chan->chip->regs + DMAC_CHEN, val,
1105 !(val & chan_active), 1000, 10000);
1106 if (ret == -ETIMEDOUT)
1107 dev_warn(dchan2dev(dchan),
1108 "%s failed to stop\n", axi_chan_name(chan));
1109
1110 if (chan->direction != DMA_MEM_TO_MEM)
1111 dw_axi_dma_set_hw_channel(chan, false);
1112 if (chan->direction == DMA_MEM_TO_DEV)
1113 dw_axi_dma_set_byte_halfword(chan, false);
1114
1115 spin_lock_irqsave(&chan->vc.lock, flags);
1116
1117 vchan_get_all_descriptors(&chan->vc, &head);
1118
1119 chan->cyclic = false;
1120 spin_unlock_irqrestore(&chan->vc.lock, flags);
1121
1122 vchan_dma_desc_free_list(&chan->vc, &head);
1123
1124 dev_vdbg(dchan2dev(dchan), "terminated: %s\n", axi_chan_name(chan));
1125
1126 return 0;
1127 }
1128
dma_chan_pause(struct dma_chan * dchan)1129 static int dma_chan_pause(struct dma_chan *dchan)
1130 {
1131 struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
1132 unsigned long flags;
1133 unsigned int timeout = 20; /* timeout iterations */
1134 u32 val;
1135
1136 spin_lock_irqsave(&chan->vc.lock, flags);
1137
1138 val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
1139 val |= BIT(chan->id) << DMAC_CHAN_SUSP_SHIFT |
1140 BIT(chan->id) << DMAC_CHAN_SUSP_WE_SHIFT;
1141 axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
1142
1143 do {
1144 if (axi_chan_irq_read(chan) & DWAXIDMAC_IRQ_SUSPENDED)
1145 break;
1146
1147 udelay(2);
1148 } while (--timeout);
1149
1150 axi_chan_irq_clear(chan, DWAXIDMAC_IRQ_SUSPENDED);
1151
1152 chan->is_paused = true;
1153
1154 spin_unlock_irqrestore(&chan->vc.lock, flags);
1155
1156 return timeout ? 0 : -EAGAIN;
1157 }
1158
1159 /* Called in chan locked context */
axi_chan_resume(struct axi_dma_chan * chan)1160 static inline void axi_chan_resume(struct axi_dma_chan *chan)
1161 {
1162 u32 val;
1163
1164 val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
1165 val &= ~(BIT(chan->id) << DMAC_CHAN_SUSP_SHIFT);
1166 val |= (BIT(chan->id) << DMAC_CHAN_SUSP_WE_SHIFT);
1167 axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
1168
1169 chan->is_paused = false;
1170 }
1171
dma_chan_resume(struct dma_chan * dchan)1172 static int dma_chan_resume(struct dma_chan *dchan)
1173 {
1174 struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
1175 unsigned long flags;
1176
1177 spin_lock_irqsave(&chan->vc.lock, flags);
1178
1179 if (chan->is_paused)
1180 axi_chan_resume(chan);
1181
1182 spin_unlock_irqrestore(&chan->vc.lock, flags);
1183
1184 return 0;
1185 }
1186
axi_dma_suspend(struct axi_dma_chip * chip)1187 static int axi_dma_suspend(struct axi_dma_chip *chip)
1188 {
1189 axi_dma_irq_disable(chip);
1190 axi_dma_disable(chip);
1191
1192 clk_disable_unprepare(chip->core_clk);
1193 clk_disable_unprepare(chip->cfgr_clk);
1194
1195 return 0;
1196 }
1197
axi_dma_resume(struct axi_dma_chip * chip)1198 static int axi_dma_resume(struct axi_dma_chip *chip)
1199 {
1200 int ret;
1201
1202 ret = clk_prepare_enable(chip->cfgr_clk);
1203 if (ret < 0)
1204 return ret;
1205
1206 ret = clk_prepare_enable(chip->core_clk);
1207 if (ret < 0)
1208 return ret;
1209
1210 axi_dma_enable(chip);
1211 axi_dma_irq_enable(chip);
1212
1213 return 0;
1214 }
1215
axi_dma_runtime_suspend(struct device * dev)1216 static int __maybe_unused axi_dma_runtime_suspend(struct device *dev)
1217 {
1218 struct axi_dma_chip *chip = dev_get_drvdata(dev);
1219
1220 return axi_dma_suspend(chip);
1221 }
1222
axi_dma_runtime_resume(struct device * dev)1223 static int __maybe_unused axi_dma_runtime_resume(struct device *dev)
1224 {
1225 struct axi_dma_chip *chip = dev_get_drvdata(dev);
1226
1227 return axi_dma_resume(chip);
1228 }
1229
dw_axi_dma_of_xlate(struct of_phandle_args * dma_spec,struct of_dma * ofdma)1230 static struct dma_chan *dw_axi_dma_of_xlate(struct of_phandle_args *dma_spec,
1231 struct of_dma *ofdma)
1232 {
1233 struct dw_axi_dma *dw = ofdma->of_dma_data;
1234 struct axi_dma_chan *chan;
1235 struct dma_chan *dchan;
1236
1237 dchan = dma_get_any_slave_channel(&dw->dma);
1238 if (!dchan)
1239 return NULL;
1240
1241 chan = dchan_to_axi_dma_chan(dchan);
1242 chan->hw_handshake_num = dma_spec->args[0];
1243 return dchan;
1244 }
1245
parse_device_properties(struct axi_dma_chip * chip)1246 static int parse_device_properties(struct axi_dma_chip *chip)
1247 {
1248 struct device *dev = chip->dev;
1249 u32 tmp, carr[DMAC_MAX_CHANNELS];
1250 int ret;
1251
1252 ret = device_property_read_u32(dev, "dma-channels", &tmp);
1253 if (ret)
1254 return ret;
1255 if (tmp == 0 || tmp > DMAC_MAX_CHANNELS)
1256 return -EINVAL;
1257
1258 chip->dw->hdata->nr_channels = tmp;
1259
1260 ret = device_property_read_u32(dev, "snps,dma-masters", &tmp);
1261 if (ret)
1262 return ret;
1263 if (tmp == 0 || tmp > DMAC_MAX_MASTERS)
1264 return -EINVAL;
1265
1266 chip->dw->hdata->nr_masters = tmp;
1267
1268 ret = device_property_read_u32(dev, "snps,data-width", &tmp);
1269 if (ret)
1270 return ret;
1271 if (tmp > DWAXIDMAC_TRANS_WIDTH_MAX)
1272 return -EINVAL;
1273
1274 chip->dw->hdata->m_data_width = tmp;
1275
1276 ret = device_property_read_u32_array(dev, "snps,block-size", carr,
1277 chip->dw->hdata->nr_channels);
1278 if (ret)
1279 return ret;
1280 for (tmp = 0; tmp < chip->dw->hdata->nr_channels; tmp++) {
1281 if (carr[tmp] == 0 || carr[tmp] > DMAC_MAX_BLK_SIZE)
1282 return -EINVAL;
1283
1284 chip->dw->hdata->block_size[tmp] = carr[tmp];
1285 }
1286
1287 ret = device_property_read_u32_array(dev, "snps,priority", carr,
1288 chip->dw->hdata->nr_channels);
1289 if (ret)
1290 return ret;
1291 /* Priority value must be programmed within [0:nr_channels-1] range */
1292 for (tmp = 0; tmp < chip->dw->hdata->nr_channels; tmp++) {
1293 if (carr[tmp] >= chip->dw->hdata->nr_channels)
1294 return -EINVAL;
1295
1296 chip->dw->hdata->priority[tmp] = carr[tmp];
1297 }
1298
1299 /* axi-max-burst-len is optional property */
1300 ret = device_property_read_u32(dev, "snps,axi-max-burst-len", &tmp);
1301 if (!ret) {
1302 if (tmp > DWAXIDMAC_ARWLEN_MAX + 1)
1303 return -EINVAL;
1304 if (tmp < DWAXIDMAC_ARWLEN_MIN + 1)
1305 return -EINVAL;
1306
1307 chip->dw->hdata->restrict_axi_burst_len = true;
1308 chip->dw->hdata->axi_rw_burst_len = tmp;
1309 }
1310
1311 return 0;
1312 }
1313
dw_probe(struct platform_device * pdev)1314 static int dw_probe(struct platform_device *pdev)
1315 {
1316 struct device_node *node = pdev->dev.of_node;
1317 struct axi_dma_chip *chip;
1318 struct resource *mem;
1319 struct dw_axi_dma *dw;
1320 struct dw_axi_dma_hcfg *hdata;
1321 u32 i;
1322 int ret;
1323
1324 chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
1325 if (!chip)
1326 return -ENOMEM;
1327
1328 dw = devm_kzalloc(&pdev->dev, sizeof(*dw), GFP_KERNEL);
1329 if (!dw)
1330 return -ENOMEM;
1331
1332 hdata = devm_kzalloc(&pdev->dev, sizeof(*hdata), GFP_KERNEL);
1333 if (!hdata)
1334 return -ENOMEM;
1335
1336 chip->dw = dw;
1337 chip->dev = &pdev->dev;
1338 chip->dw->hdata = hdata;
1339
1340 chip->irq = platform_get_irq(pdev, 0);
1341 if (chip->irq < 0)
1342 return chip->irq;
1343
1344 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1345 chip->regs = devm_ioremap_resource(chip->dev, mem);
1346 if (IS_ERR(chip->regs))
1347 return PTR_ERR(chip->regs);
1348
1349 if (of_device_is_compatible(node, "intel,kmb-axi-dma")) {
1350 chip->apb_regs = devm_platform_ioremap_resource(pdev, 1);
1351 if (IS_ERR(chip->apb_regs))
1352 return PTR_ERR(chip->apb_regs);
1353 }
1354
1355 chip->core_clk = devm_clk_get(chip->dev, "core-clk");
1356 if (IS_ERR(chip->core_clk))
1357 return PTR_ERR(chip->core_clk);
1358
1359 chip->cfgr_clk = devm_clk_get(chip->dev, "cfgr-clk");
1360 if (IS_ERR(chip->cfgr_clk))
1361 return PTR_ERR(chip->cfgr_clk);
1362
1363 ret = parse_device_properties(chip);
1364 if (ret)
1365 return ret;
1366
1367 dw->chan = devm_kcalloc(chip->dev, hdata->nr_channels,
1368 sizeof(*dw->chan), GFP_KERNEL);
1369 if (!dw->chan)
1370 return -ENOMEM;
1371
1372 ret = devm_request_irq(chip->dev, chip->irq, dw_axi_dma_interrupt,
1373 IRQF_SHARED, KBUILD_MODNAME, chip);
1374 if (ret)
1375 return ret;
1376
1377 INIT_LIST_HEAD(&dw->dma.channels);
1378 for (i = 0; i < hdata->nr_channels; i++) {
1379 struct axi_dma_chan *chan = &dw->chan[i];
1380
1381 chan->chip = chip;
1382 chan->id = i;
1383 chan->chan_regs = chip->regs + COMMON_REG_LEN + i * CHAN_REG_LEN;
1384 atomic_set(&chan->descs_allocated, 0);
1385
1386 chan->vc.desc_free = vchan_desc_put;
1387 vchan_init(&chan->vc, &dw->dma);
1388 }
1389
1390 /* Set capabilities */
1391 dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
1392 dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
1393 dma_cap_set(DMA_CYCLIC, dw->dma.cap_mask);
1394
1395 /* DMA capabilities */
1396 dw->dma.chancnt = hdata->nr_channels;
1397 dw->dma.max_burst = hdata->axi_rw_burst_len;
1398 dw->dma.src_addr_widths = AXI_DMA_BUSWIDTHS;
1399 dw->dma.dst_addr_widths = AXI_DMA_BUSWIDTHS;
1400 dw->dma.directions = BIT(DMA_MEM_TO_MEM);
1401 dw->dma.directions |= BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
1402 dw->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
1403
1404 dw->dma.dev = chip->dev;
1405 dw->dma.device_tx_status = dma_chan_tx_status;
1406 dw->dma.device_issue_pending = dma_chan_issue_pending;
1407 dw->dma.device_terminate_all = dma_chan_terminate_all;
1408 dw->dma.device_pause = dma_chan_pause;
1409 dw->dma.device_resume = dma_chan_resume;
1410
1411 dw->dma.device_alloc_chan_resources = dma_chan_alloc_chan_resources;
1412 dw->dma.device_free_chan_resources = dma_chan_free_chan_resources;
1413
1414 dw->dma.device_prep_dma_memcpy = dma_chan_prep_dma_memcpy;
1415 dw->dma.device_synchronize = dw_axi_dma_synchronize;
1416 dw->dma.device_config = dw_axi_dma_chan_slave_config;
1417 dw->dma.device_prep_slave_sg = dw_axi_dma_chan_prep_slave_sg;
1418 dw->dma.device_prep_dma_cyclic = dw_axi_dma_chan_prep_cyclic;
1419
1420 /*
1421 * Synopsis DesignWare AxiDMA datasheet mentioned Maximum
1422 * supported blocks is 1024. Device register width is 4 bytes.
1423 * Therefore, set constraint to 1024 * 4.
1424 */
1425 dw->dma.dev->dma_parms = &dw->dma_parms;
1426 dma_set_max_seg_size(&pdev->dev, MAX_BLOCK_SIZE);
1427 platform_set_drvdata(pdev, chip);
1428
1429 pm_runtime_enable(chip->dev);
1430
1431 /*
1432 * We can't just call pm_runtime_get here instead of
1433 * pm_runtime_get_noresume + axi_dma_resume because we need
1434 * driver to work also without Runtime PM.
1435 */
1436 pm_runtime_get_noresume(chip->dev);
1437 ret = axi_dma_resume(chip);
1438 if (ret < 0)
1439 goto err_pm_disable;
1440
1441 axi_dma_hw_init(chip);
1442
1443 pm_runtime_put(chip->dev);
1444
1445 ret = dmaenginem_async_device_register(&dw->dma);
1446 if (ret)
1447 goto err_pm_disable;
1448
1449 /* Register with OF helpers for DMA lookups */
1450 ret = of_dma_controller_register(pdev->dev.of_node,
1451 dw_axi_dma_of_xlate, dw);
1452 if (ret < 0)
1453 dev_warn(&pdev->dev,
1454 "Failed to register OF DMA controller, fallback to MEM_TO_MEM mode\n");
1455
1456 dev_info(chip->dev, "DesignWare AXI DMA Controller, %d channels\n",
1457 dw->hdata->nr_channels);
1458
1459 return 0;
1460
1461 err_pm_disable:
1462 pm_runtime_disable(chip->dev);
1463
1464 return ret;
1465 }
1466
dw_remove(struct platform_device * pdev)1467 static int dw_remove(struct platform_device *pdev)
1468 {
1469 struct axi_dma_chip *chip = platform_get_drvdata(pdev);
1470 struct dw_axi_dma *dw = chip->dw;
1471 struct axi_dma_chan *chan, *_chan;
1472 u32 i;
1473
1474 /* Enable clk before accessing to registers */
1475 clk_prepare_enable(chip->cfgr_clk);
1476 clk_prepare_enable(chip->core_clk);
1477 axi_dma_irq_disable(chip);
1478 for (i = 0; i < dw->hdata->nr_channels; i++) {
1479 axi_chan_disable(&chip->dw->chan[i]);
1480 axi_chan_irq_disable(&chip->dw->chan[i], DWAXIDMAC_IRQ_ALL);
1481 }
1482 axi_dma_disable(chip);
1483
1484 pm_runtime_disable(chip->dev);
1485 axi_dma_suspend(chip);
1486
1487 devm_free_irq(chip->dev, chip->irq, chip);
1488
1489 of_dma_controller_free(chip->dev->of_node);
1490
1491 list_for_each_entry_safe(chan, _chan, &dw->dma.channels,
1492 vc.chan.device_node) {
1493 list_del(&chan->vc.chan.device_node);
1494 tasklet_kill(&chan->vc.task);
1495 }
1496
1497 return 0;
1498 }
1499
1500 static const struct dev_pm_ops dw_axi_dma_pm_ops = {
1501 SET_RUNTIME_PM_OPS(axi_dma_runtime_suspend, axi_dma_runtime_resume, NULL)
1502 };
1503
1504 static const struct of_device_id dw_dma_of_id_table[] = {
1505 { .compatible = "snps,axi-dma-1.01a" },
1506 { .compatible = "intel,kmb-axi-dma" },
1507 {}
1508 };
1509 MODULE_DEVICE_TABLE(of, dw_dma_of_id_table);
1510
1511 static struct platform_driver dw_driver = {
1512 .probe = dw_probe,
1513 .remove = dw_remove,
1514 .driver = {
1515 .name = KBUILD_MODNAME,
1516 .of_match_table = dw_dma_of_id_table,
1517 .pm = &dw_axi_dma_pm_ops,
1518 },
1519 };
1520 module_platform_driver(dw_driver);
1521
1522 MODULE_LICENSE("GPL v2");
1523 MODULE_DESCRIPTION("Synopsys DesignWare AXI DMA Controller platform driver");
1524 MODULE_AUTHOR("Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>");
1525