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