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