blob: fc513eb2b289c0f687723a913d60db542b44b4eb [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Alex Smithd894fc62015-03-18 16:16:36 +00002/*
3 * Ingenic JZ4780 DMA controller
4 *
5 * Copyright (c) 2015 Imagination Technologies
6 * Author: Alex Smith <alex@alex-smith.me.uk>
Alex Smithd894fc62015-03-18 16:16:36 +00007 */
8
9#include <linux/clk.h>
10#include <linux/dmapool.h>
11#include <linux/init.h>
12#include <linux/interrupt.h>
13#include <linux/module.h>
14#include <linux/of.h>
Paul Cercueil6147b032018-08-29 23:32:45 +020015#include <linux/of_device.h>
Alex Smithd894fc62015-03-18 16:16:36 +000016#include <linux/of_dma.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19
20#include "dmaengine.h"
21#include "virt-dma.h"
22
Alex Smithd894fc62015-03-18 16:16:36 +000023/* Global registers. */
Paul Cercueil33633582018-08-29 23:32:46 +020024#define JZ_DMA_REG_DMAC 0x00
25#define JZ_DMA_REG_DIRQP 0x04
26#define JZ_DMA_REG_DDR 0x08
27#define JZ_DMA_REG_DDRS 0x0c
Paul Cercueil29870eb2018-08-29 23:32:49 +020028#define JZ_DMA_REG_DCKE 0x10
29#define JZ_DMA_REG_DCKES 0x14
30#define JZ_DMA_REG_DCKEC 0x18
Paul Cercueil33633582018-08-29 23:32:46 +020031#define JZ_DMA_REG_DMACP 0x1c
32#define JZ_DMA_REG_DSIRQP 0x20
33#define JZ_DMA_REG_DSIRQM 0x24
34#define JZ_DMA_REG_DCIRQP 0x28
35#define JZ_DMA_REG_DCIRQM 0x2c
Alex Smithd894fc62015-03-18 16:16:36 +000036
37/* Per-channel registers. */
38#define JZ_DMA_REG_CHAN(n) (n * 0x20)
Paul Cercueil33633582018-08-29 23:32:46 +020039#define JZ_DMA_REG_DSA 0x00
40#define JZ_DMA_REG_DTA 0x04
41#define JZ_DMA_REG_DTC 0x08
42#define JZ_DMA_REG_DRT 0x0c
43#define JZ_DMA_REG_DCS 0x10
44#define JZ_DMA_REG_DCM 0x14
45#define JZ_DMA_REG_DDA 0x18
46#define JZ_DMA_REG_DSD 0x1c
Alex Smithd894fc62015-03-18 16:16:36 +000047
48#define JZ_DMA_DMAC_DMAE BIT(0)
49#define JZ_DMA_DMAC_AR BIT(2)
50#define JZ_DMA_DMAC_HLT BIT(3)
Paul Cercueil17a8e302018-08-29 23:32:52 +020051#define JZ_DMA_DMAC_FAIC BIT(27)
Alex Smithd894fc62015-03-18 16:16:36 +000052#define JZ_DMA_DMAC_FMSC BIT(31)
53
54#define JZ_DMA_DRT_AUTO 0x8
55
56#define JZ_DMA_DCS_CTE BIT(0)
57#define JZ_DMA_DCS_HLT BIT(2)
58#define JZ_DMA_DCS_TT BIT(3)
59#define JZ_DMA_DCS_AR BIT(4)
60#define JZ_DMA_DCS_DES8 BIT(30)
61
62#define JZ_DMA_DCM_LINK BIT(0)
63#define JZ_DMA_DCM_TIE BIT(1)
64#define JZ_DMA_DCM_STDE BIT(2)
65#define JZ_DMA_DCM_TSZ_SHIFT 8
66#define JZ_DMA_DCM_TSZ_MASK (0x7 << JZ_DMA_DCM_TSZ_SHIFT)
67#define JZ_DMA_DCM_DP_SHIFT 12
68#define JZ_DMA_DCM_SP_SHIFT 14
69#define JZ_DMA_DCM_DAI BIT(22)
70#define JZ_DMA_DCM_SAI BIT(23)
71
72#define JZ_DMA_SIZE_4_BYTE 0x0
73#define JZ_DMA_SIZE_1_BYTE 0x1
74#define JZ_DMA_SIZE_2_BYTE 0x2
75#define JZ_DMA_SIZE_16_BYTE 0x3
76#define JZ_DMA_SIZE_32_BYTE 0x4
77#define JZ_DMA_SIZE_64_BYTE 0x5
78#define JZ_DMA_SIZE_128_BYTE 0x6
79
80#define JZ_DMA_WIDTH_32_BIT 0x0
81#define JZ_DMA_WIDTH_8_BIT 0x1
82#define JZ_DMA_WIDTH_16_BIT 0x2
83
84#define JZ_DMA_BUSWIDTHS (BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
85 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
86 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES))
87
Paul Cercueil33633582018-08-29 23:32:46 +020088#define JZ4780_DMA_CTRL_OFFSET 0x1000
89
Paul Cercueil29870eb2018-08-29 23:32:49 +020090/* macros for use with jz4780_dma_soc_data.flags */
91#define JZ_SOC_DATA_ALLOW_LEGACY_DT BIT(0)
92#define JZ_SOC_DATA_PROGRAMMABLE_DMA BIT(1)
93#define JZ_SOC_DATA_PER_CHAN_PM BIT(2)
Paul Cercueilae9156b2018-08-29 23:32:51 +020094#define JZ_SOC_DATA_NO_DCKES_DCKEC BIT(3)
Paul Cercueilf4c255f2019-07-14 17:55:04 -040095#define JZ_SOC_DATA_BREAK_LINKS BIT(4)
Paul Cercueil29870eb2018-08-29 23:32:49 +020096
Alex Smithd894fc62015-03-18 16:16:36 +000097/**
98 * struct jz4780_dma_hwdesc - descriptor structure read by the DMA controller.
99 * @dcm: value for the DCM (channel command) register
100 * @dsa: source address
101 * @dta: target address
102 * @dtc: transfer count (number of blocks of the transfer size specified in DCM
103 * to transfer) in the low 24 bits, offset of the next descriptor from the
104 * descriptor base address in the upper 8 bits.
Alex Smithd894fc62015-03-18 16:16:36 +0000105 */
106struct jz4780_dma_hwdesc {
Paul Cercueilc8c0cda2021-12-06 17:42:58 +0000107 u32 dcm;
108 u32 dsa;
109 u32 dta;
110 u32 dtc;
Alex Smithd894fc62015-03-18 16:16:36 +0000111};
112
113/* Size of allocations for hardware descriptor blocks. */
114#define JZ_DMA_DESC_BLOCK_SIZE PAGE_SIZE
115#define JZ_DMA_MAX_DESC \
116 (JZ_DMA_DESC_BLOCK_SIZE / sizeof(struct jz4780_dma_hwdesc))
117
118struct jz4780_dma_desc {
119 struct virt_dma_desc vdesc;
120
121 struct jz4780_dma_hwdesc *desc;
122 dma_addr_t desc_phys;
123 unsigned int count;
124 enum dma_transaction_type type;
Paul Cercueil76a09662021-12-06 17:42:59 +0000125 u32 transfer_type;
Paul Cercueilc8c0cda2021-12-06 17:42:58 +0000126 u32 status;
Alex Smithd894fc62015-03-18 16:16:36 +0000127};
128
129struct jz4780_dma_chan {
130 struct virt_dma_chan vchan;
131 unsigned int id;
132 struct dma_pool *desc_pool;
133
Paul Cercueil76a09662021-12-06 17:42:59 +0000134 u32 transfer_type_tx, transfer_type_rx;
Paul Cercueilc8c0cda2021-12-06 17:42:58 +0000135 u32 transfer_shift;
Alex Smithd894fc62015-03-18 16:16:36 +0000136 struct dma_slave_config config;
137
138 struct jz4780_dma_desc *desc;
139 unsigned int curr_hwdesc;
140};
141
Paul Cercueil6147b032018-08-29 23:32:45 +0200142struct jz4780_dma_soc_data {
143 unsigned int nb_channels;
Paul Cercueil29870eb2018-08-29 23:32:49 +0200144 unsigned int transfer_ord_max;
145 unsigned long flags;
Paul Cercueil6147b032018-08-29 23:32:45 +0200146};
147
Alex Smithd894fc62015-03-18 16:16:36 +0000148struct jz4780_dma_dev {
149 struct dma_device dma_device;
Paul Cercueil33633582018-08-29 23:32:46 +0200150 void __iomem *chn_base;
151 void __iomem *ctrl_base;
Alex Smithd894fc62015-03-18 16:16:36 +0000152 struct clk *clk;
153 unsigned int irq;
Paul Cercueil6147b032018-08-29 23:32:45 +0200154 const struct jz4780_dma_soc_data *soc_data;
Alex Smithd894fc62015-03-18 16:16:36 +0000155
Paul Cercueilc8c0cda2021-12-06 17:42:58 +0000156 u32 chan_reserved;
Paul Cercueil6147b032018-08-29 23:32:45 +0200157 struct jz4780_dma_chan chan[];
Alex Smithd894fc62015-03-18 16:16:36 +0000158};
159
Alex Smith026fd402015-07-24 17:24:24 +0100160struct jz4780_dma_filter_data {
Paul Cercueil76a09662021-12-06 17:42:59 +0000161 u32 transfer_type_tx, transfer_type_rx;
Alex Smithd894fc62015-03-18 16:16:36 +0000162 int channel;
163};
164
165static inline struct jz4780_dma_chan *to_jz4780_dma_chan(struct dma_chan *chan)
166{
167 return container_of(chan, struct jz4780_dma_chan, vchan.chan);
168}
169
170static inline struct jz4780_dma_desc *to_jz4780_dma_desc(
171 struct virt_dma_desc *vdesc)
172{
173 return container_of(vdesc, struct jz4780_dma_desc, vdesc);
174}
175
176static inline struct jz4780_dma_dev *jz4780_dma_chan_parent(
177 struct jz4780_dma_chan *jzchan)
178{
179 return container_of(jzchan->vchan.chan.device, struct jz4780_dma_dev,
180 dma_device);
181}
182
Paul Cercueilc8c0cda2021-12-06 17:42:58 +0000183static inline u32 jz4780_dma_chn_readl(struct jz4780_dma_dev *jzdma,
Paul Cercueil33633582018-08-29 23:32:46 +0200184 unsigned int chn, unsigned int reg)
Alex Smithd894fc62015-03-18 16:16:36 +0000185{
Paul Cercueil33633582018-08-29 23:32:46 +0200186 return readl(jzdma->chn_base + reg + JZ_DMA_REG_CHAN(chn));
Alex Smithd894fc62015-03-18 16:16:36 +0000187}
188
Paul Cercueil33633582018-08-29 23:32:46 +0200189static inline void jz4780_dma_chn_writel(struct jz4780_dma_dev *jzdma,
Paul Cercueilc8c0cda2021-12-06 17:42:58 +0000190 unsigned int chn, unsigned int reg, u32 val)
Paul Cercueil33633582018-08-29 23:32:46 +0200191{
192 writel(val, jzdma->chn_base + reg + JZ_DMA_REG_CHAN(chn));
193}
194
Paul Cercueilc8c0cda2021-12-06 17:42:58 +0000195static inline u32 jz4780_dma_ctrl_readl(struct jz4780_dma_dev *jzdma,
Paul Cercueil33633582018-08-29 23:32:46 +0200196 unsigned int reg)
197{
198 return readl(jzdma->ctrl_base + reg);
199}
200
201static inline void jz4780_dma_ctrl_writel(struct jz4780_dma_dev *jzdma,
Paul Cercueilc8c0cda2021-12-06 17:42:58 +0000202 unsigned int reg, u32 val)
Alex Smithd894fc62015-03-18 16:16:36 +0000203{
Paul Cercueil33633582018-08-29 23:32:46 +0200204 writel(val, jzdma->ctrl_base + reg);
Alex Smithd894fc62015-03-18 16:16:36 +0000205}
206
Paul Cercueil29870eb2018-08-29 23:32:49 +0200207static inline void jz4780_dma_chan_enable(struct jz4780_dma_dev *jzdma,
208 unsigned int chn)
209{
Paul Cercueilae9156b2018-08-29 23:32:51 +0200210 if (jzdma->soc_data->flags & JZ_SOC_DATA_PER_CHAN_PM) {
211 unsigned int reg;
212
213 if (jzdma->soc_data->flags & JZ_SOC_DATA_NO_DCKES_DCKEC)
214 reg = JZ_DMA_REG_DCKE;
215 else
216 reg = JZ_DMA_REG_DCKES;
217
218 jz4780_dma_ctrl_writel(jzdma, reg, BIT(chn));
219 }
Paul Cercueil29870eb2018-08-29 23:32:49 +0200220}
221
222static inline void jz4780_dma_chan_disable(struct jz4780_dma_dev *jzdma,
223 unsigned int chn)
224{
Paul Cercueilae9156b2018-08-29 23:32:51 +0200225 if ((jzdma->soc_data->flags & JZ_SOC_DATA_PER_CHAN_PM) &&
226 !(jzdma->soc_data->flags & JZ_SOC_DATA_NO_DCKES_DCKEC))
Paul Cercueil29870eb2018-08-29 23:32:49 +0200227 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DCKEC, BIT(chn));
Alex Smithd894fc62015-03-18 16:16:36 +0000228}
229
Paul Cercueil76a09662021-12-06 17:42:59 +0000230static struct jz4780_dma_desc *
231jz4780_dma_desc_alloc(struct jz4780_dma_chan *jzchan, unsigned int count,
232 enum dma_transaction_type type,
233 enum dma_transfer_direction direction)
Alex Smithd894fc62015-03-18 16:16:36 +0000234{
235 struct jz4780_dma_desc *desc;
236
237 if (count > JZ_DMA_MAX_DESC)
238 return NULL;
239
240 desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
241 if (!desc)
242 return NULL;
243
244 desc->desc = dma_pool_alloc(jzchan->desc_pool, GFP_NOWAIT,
245 &desc->desc_phys);
246 if (!desc->desc) {
247 kfree(desc);
248 return NULL;
249 }
250
251 desc->count = count;
252 desc->type = type;
Paul Cercueil76a09662021-12-06 17:42:59 +0000253
254 if (direction == DMA_DEV_TO_MEM)
255 desc->transfer_type = jzchan->transfer_type_rx;
256 else
257 desc->transfer_type = jzchan->transfer_type_tx;
258
Alex Smithd894fc62015-03-18 16:16:36 +0000259 return desc;
260}
261
262static void jz4780_dma_desc_free(struct virt_dma_desc *vdesc)
263{
264 struct jz4780_dma_desc *desc = to_jz4780_dma_desc(vdesc);
265 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(vdesc->tx.chan);
266
267 dma_pool_free(jzchan->desc_pool, desc->desc, desc->desc_phys);
268 kfree(desc);
269}
270
Paul Cercueilc8c0cda2021-12-06 17:42:58 +0000271static u32 jz4780_dma_transfer_size(struct jz4780_dma_chan *jzchan,
272 unsigned long val, u32 *shift)
Alex Smithd894fc62015-03-18 16:16:36 +0000273{
Paul Cercueil29870eb2018-08-29 23:32:49 +0200274 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
Alex Smithdc578f32015-07-24 17:24:21 +0100275 int ord = ffs(val) - 1;
Alex Smithd894fc62015-03-18 16:16:36 +0000276
Alex Smithdc578f32015-07-24 17:24:21 +0100277 /*
278 * 8 byte transfer sizes unsupported so fall back on 4. If it's larger
279 * than the maximum, just limit it. It is perfectly safe to fall back
280 * in this way since we won't exceed the maximum burst size supported
281 * by the device, the only effect is reduced efficiency. This is better
282 * than refusing to perform the request at all.
283 */
284 if (ord == 3)
285 ord = 2;
Paul Cercueil29870eb2018-08-29 23:32:49 +0200286 else if (ord > jzdma->soc_data->transfer_ord_max)
287 ord = jzdma->soc_data->transfer_ord_max;
Alex Smithdc578f32015-07-24 17:24:21 +0100288
289 *shift = ord;
290
291 switch (ord) {
Alex Smithd894fc62015-03-18 16:16:36 +0000292 case 0:
293 return JZ_DMA_SIZE_1_BYTE;
294 case 1:
295 return JZ_DMA_SIZE_2_BYTE;
296 case 2:
297 return JZ_DMA_SIZE_4_BYTE;
298 case 4:
299 return JZ_DMA_SIZE_16_BYTE;
300 case 5:
301 return JZ_DMA_SIZE_32_BYTE;
302 case 6:
303 return JZ_DMA_SIZE_64_BYTE;
Alex Smithd894fc62015-03-18 16:16:36 +0000304 default:
Alex Smithdc578f32015-07-24 17:24:21 +0100305 return JZ_DMA_SIZE_128_BYTE;
Alex Smithd894fc62015-03-18 16:16:36 +0000306 }
307}
308
Alex Smith839896e2015-07-24 17:24:22 +0100309static int jz4780_dma_setup_hwdesc(struct jz4780_dma_chan *jzchan,
Alex Smithd894fc62015-03-18 16:16:36 +0000310 struct jz4780_dma_hwdesc *desc, dma_addr_t addr, size_t len,
311 enum dma_transfer_direction direction)
312{
313 struct dma_slave_config *config = &jzchan->config;
Paul Cercueilc8c0cda2021-12-06 17:42:58 +0000314 u32 width, maxburst, tsz;
Alex Smithd894fc62015-03-18 16:16:36 +0000315
316 if (direction == DMA_MEM_TO_DEV) {
317 desc->dcm = JZ_DMA_DCM_SAI;
318 desc->dsa = addr;
319 desc->dta = config->dst_addr;
Alex Smithd894fc62015-03-18 16:16:36 +0000320
321 width = config->dst_addr_width;
322 maxburst = config->dst_maxburst;
323 } else {
324 desc->dcm = JZ_DMA_DCM_DAI;
325 desc->dsa = config->src_addr;
326 desc->dta = addr;
Alex Smithd894fc62015-03-18 16:16:36 +0000327
328 width = config->src_addr_width;
329 maxburst = config->src_maxburst;
330 }
331
332 /*
333 * This calculates the maximum transfer size that can be used with the
334 * given address, length, width and maximum burst size. The address
335 * must be aligned to the transfer size, the total length must be
336 * divisible by the transfer size, and we must not use more than the
337 * maximum burst specified by the user.
338 */
Paul Cercueil29870eb2018-08-29 23:32:49 +0200339 tsz = jz4780_dma_transfer_size(jzchan, addr | len | (width * maxburst),
Alex Smithdc578f32015-07-24 17:24:21 +0100340 &jzchan->transfer_shift);
Alex Smithd894fc62015-03-18 16:16:36 +0000341
342 switch (width) {
343 case DMA_SLAVE_BUSWIDTH_1_BYTE:
344 case DMA_SLAVE_BUSWIDTH_2_BYTES:
345 break;
346 case DMA_SLAVE_BUSWIDTH_4_BYTES:
347 width = JZ_DMA_WIDTH_32_BIT;
348 break;
349 default:
350 return -EINVAL;
351 }
352
353 desc->dcm |= tsz << JZ_DMA_DCM_TSZ_SHIFT;
354 desc->dcm |= width << JZ_DMA_DCM_SP_SHIFT;
355 desc->dcm |= width << JZ_DMA_DCM_DP_SHIFT;
356
Alex Smithdc578f32015-07-24 17:24:21 +0100357 desc->dtc = len >> jzchan->transfer_shift;
Alex Smith839896e2015-07-24 17:24:22 +0100358 return 0;
Alex Smithd894fc62015-03-18 16:16:36 +0000359}
360
361static struct dma_async_tx_descriptor *jz4780_dma_prep_slave_sg(
362 struct dma_chan *chan, struct scatterlist *sgl, unsigned int sg_len,
Alex Smith46fa51682015-07-24 17:24:20 +0100363 enum dma_transfer_direction direction, unsigned long flags,
364 void *context)
Alex Smithd894fc62015-03-18 16:16:36 +0000365{
366 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
Paul Cercueilf4c255f2019-07-14 17:55:04 -0400367 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
Alex Smithd894fc62015-03-18 16:16:36 +0000368 struct jz4780_dma_desc *desc;
369 unsigned int i;
370 int err;
371
Paul Cercueil76a09662021-12-06 17:42:59 +0000372 desc = jz4780_dma_desc_alloc(jzchan, sg_len, DMA_SLAVE, direction);
Alex Smithd894fc62015-03-18 16:16:36 +0000373 if (!desc)
374 return NULL;
375
376 for (i = 0; i < sg_len; i++) {
377 err = jz4780_dma_setup_hwdesc(jzchan, &desc->desc[i],
Alex Smith839896e2015-07-24 17:24:22 +0100378 sg_dma_address(&sgl[i]),
379 sg_dma_len(&sgl[i]),
380 direction);
Colin Ian Kingfc878ef2016-09-29 18:45:05 +0100381 if (err < 0) {
382 jz4780_dma_desc_free(&jzchan->desc->vdesc);
Alex Smith839896e2015-07-24 17:24:22 +0100383 return NULL;
Colin Ian Kingfc878ef2016-09-29 18:45:05 +0100384 }
Alex Smithd894fc62015-03-18 16:16:36 +0000385
386 desc->desc[i].dcm |= JZ_DMA_DCM_TIE;
387
Paul Cercueilf4c255f2019-07-14 17:55:04 -0400388 if (i != (sg_len - 1) &&
389 !(jzdma->soc_data->flags & JZ_SOC_DATA_BREAK_LINKS)) {
Alex Smithd894fc62015-03-18 16:16:36 +0000390 /* Automatically proceeed to the next descriptor. */
391 desc->desc[i].dcm |= JZ_DMA_DCM_LINK;
392
393 /*
394 * The upper 8 bits of the DTC field in the descriptor
395 * must be set to (offset from descriptor base of next
396 * descriptor >> 4).
397 */
398 desc->desc[i].dtc |=
399 (((i + 1) * sizeof(*desc->desc)) >> 4) << 24;
400 }
401 }
402
403 return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
404}
405
406static struct dma_async_tx_descriptor *jz4780_dma_prep_dma_cyclic(
407 struct dma_chan *chan, dma_addr_t buf_addr, size_t buf_len,
408 size_t period_len, enum dma_transfer_direction direction,
409 unsigned long flags)
410{
411 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
412 struct jz4780_dma_desc *desc;
413 unsigned int periods, i;
414 int err;
415
416 if (buf_len % period_len)
417 return NULL;
418
419 periods = buf_len / period_len;
420
Paul Cercueil76a09662021-12-06 17:42:59 +0000421 desc = jz4780_dma_desc_alloc(jzchan, periods, DMA_CYCLIC, direction);
Alex Smithd894fc62015-03-18 16:16:36 +0000422 if (!desc)
423 return NULL;
424
425 for (i = 0; i < periods; i++) {
426 err = jz4780_dma_setup_hwdesc(jzchan, &desc->desc[i], buf_addr,
Alex Smith839896e2015-07-24 17:24:22 +0100427 period_len, direction);
Colin Ian Kingfc878ef2016-09-29 18:45:05 +0100428 if (err < 0) {
429 jz4780_dma_desc_free(&jzchan->desc->vdesc);
Alex Smith839896e2015-07-24 17:24:22 +0100430 return NULL;
Colin Ian Kingfc878ef2016-09-29 18:45:05 +0100431 }
Alex Smithd894fc62015-03-18 16:16:36 +0000432
433 buf_addr += period_len;
434
435 /*
436 * Set the link bit to indicate that the controller should
437 * automatically proceed to the next descriptor. In
438 * jz4780_dma_begin(), this will be cleared if we need to issue
439 * an interrupt after each period.
440 */
441 desc->desc[i].dcm |= JZ_DMA_DCM_TIE | JZ_DMA_DCM_LINK;
442
443 /*
444 * The upper 8 bits of the DTC field in the descriptor must be
445 * set to (offset from descriptor base of next descriptor >> 4).
446 * If this is the last descriptor, link it back to the first,
447 * i.e. leave offset set to 0, otherwise point to the next one.
448 */
449 if (i != (periods - 1)) {
450 desc->desc[i].dtc |=
451 (((i + 1) * sizeof(*desc->desc)) >> 4) << 24;
452 }
453 }
454
455 return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
456}
457
Vinod Koul4f5db8c2016-09-02 15:27:55 +0530458static struct dma_async_tx_descriptor *jz4780_dma_prep_dma_memcpy(
Alex Smithd894fc62015-03-18 16:16:36 +0000459 struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
460 size_t len, unsigned long flags)
461{
462 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
463 struct jz4780_dma_desc *desc;
Paul Cercueilc8c0cda2021-12-06 17:42:58 +0000464 u32 tsz;
Alex Smithd894fc62015-03-18 16:16:36 +0000465
Paul Cercueil76a09662021-12-06 17:42:59 +0000466 desc = jz4780_dma_desc_alloc(jzchan, 1, DMA_MEMCPY, 0);
Alex Smithd894fc62015-03-18 16:16:36 +0000467 if (!desc)
468 return NULL;
469
Paul Cercueil29870eb2018-08-29 23:32:49 +0200470 tsz = jz4780_dma_transfer_size(jzchan, dest | src | len,
Alex Smithdc578f32015-07-24 17:24:21 +0100471 &jzchan->transfer_shift);
Alex Smithd894fc62015-03-18 16:16:36 +0000472
Paul Cercueil76a09662021-12-06 17:42:59 +0000473 desc->transfer_type = JZ_DMA_DRT_AUTO;
Paul Cercueil5eed7d82018-08-29 23:32:47 +0200474
Alex Smithd894fc62015-03-18 16:16:36 +0000475 desc->desc[0].dsa = src;
476 desc->desc[0].dta = dest;
Alex Smithd894fc62015-03-18 16:16:36 +0000477 desc->desc[0].dcm = JZ_DMA_DCM_TIE | JZ_DMA_DCM_SAI | JZ_DMA_DCM_DAI |
478 tsz << JZ_DMA_DCM_TSZ_SHIFT |
479 JZ_DMA_WIDTH_32_BIT << JZ_DMA_DCM_SP_SHIFT |
480 JZ_DMA_WIDTH_32_BIT << JZ_DMA_DCM_DP_SHIFT;
Alex Smith839896e2015-07-24 17:24:22 +0100481 desc->desc[0].dtc = len >> jzchan->transfer_shift;
Alex Smithd894fc62015-03-18 16:16:36 +0000482
483 return vchan_tx_prep(&jzchan->vchan, &desc->vdesc, flags);
484}
485
486static void jz4780_dma_begin(struct jz4780_dma_chan *jzchan)
487{
488 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
489 struct virt_dma_desc *vdesc;
490 unsigned int i;
491 dma_addr_t desc_phys;
492
493 if (!jzchan->desc) {
494 vdesc = vchan_next_desc(&jzchan->vchan);
495 if (!vdesc)
496 return;
497
498 list_del(&vdesc->node);
499
500 jzchan->desc = to_jz4780_dma_desc(vdesc);
501 jzchan->curr_hwdesc = 0;
502
503 if (jzchan->desc->type == DMA_CYCLIC && vdesc->tx.callback) {
504 /*
505 * The DMA controller doesn't support triggering an
506 * interrupt after processing each descriptor, only
507 * after processing an entire terminated list of
508 * descriptors. For a cyclic DMA setup the list of
509 * descriptors is not terminated so we can never get an
510 * interrupt.
511 *
512 * If the user requested a callback for a cyclic DMA
513 * setup then we workaround this hardware limitation
514 * here by degrading to a set of unlinked descriptors
515 * which we will submit in sequence in response to the
516 * completion of processing the previous descriptor.
517 */
518 for (i = 0; i < jzchan->desc->count; i++)
519 jzchan->desc->desc[i].dcm &= ~JZ_DMA_DCM_LINK;
520 }
521 } else {
522 /*
523 * There is an existing transfer, therefore this must be one
524 * for which we unlinked the descriptors above. Advance to the
525 * next one in the list.
526 */
527 jzchan->curr_hwdesc =
528 (jzchan->curr_hwdesc + 1) % jzchan->desc->count;
529 }
530
Paul Cercueil29870eb2018-08-29 23:32:49 +0200531 /* Enable the channel's clock. */
532 jz4780_dma_chan_enable(jzdma, jzchan->id);
533
Paul Cercueil5eed7d82018-08-29 23:32:47 +0200534 /* Use 4-word descriptors. */
535 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS, 0);
536
537 /* Set transfer type. */
538 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DRT,
Paul Cercueil76a09662021-12-06 17:42:59 +0000539 jzchan->desc->transfer_type);
Alex Smithd894fc62015-03-18 16:16:36 +0000540
Daniel Silsby9e4e3a42018-08-29 23:32:55 +0200541 /*
542 * Set the transfer count. This is redundant for a descriptor-driven
543 * transfer. However, there can be a delay between the transfer start
544 * time and when DTCn reg contains the new transfer count. Setting
545 * it explicitly ensures residue is computed correctly at all times.
546 */
547 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DTC,
548 jzchan->desc->desc[jzchan->curr_hwdesc].dtc);
Alex Smithd894fc62015-03-18 16:16:36 +0000549
550 /* Write descriptor address and initiate descriptor fetch. */
551 desc_phys = jzchan->desc->desc_phys +
552 (jzchan->curr_hwdesc * sizeof(*jzchan->desc->desc));
Paul Cercueil33633582018-08-29 23:32:46 +0200553 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DDA, desc_phys);
554 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DDRS, BIT(jzchan->id));
Alex Smithd894fc62015-03-18 16:16:36 +0000555
556 /* Enable the channel. */
Paul Cercueil33633582018-08-29 23:32:46 +0200557 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS,
Paul Cercueil5eed7d82018-08-29 23:32:47 +0200558 JZ_DMA_DCS_CTE);
Alex Smithd894fc62015-03-18 16:16:36 +0000559}
560
561static void jz4780_dma_issue_pending(struct dma_chan *chan)
562{
563 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
564 unsigned long flags;
565
566 spin_lock_irqsave(&jzchan->vchan.lock, flags);
567
568 if (vchan_issue_pending(&jzchan->vchan) && !jzchan->desc)
569 jz4780_dma_begin(jzchan);
570
571 spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
572}
573
Alex Smith46fa51682015-07-24 17:24:20 +0100574static int jz4780_dma_terminate_all(struct dma_chan *chan)
Alex Smithd894fc62015-03-18 16:16:36 +0000575{
Alex Smith46fa51682015-07-24 17:24:20 +0100576 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
Alex Smithd894fc62015-03-18 16:16:36 +0000577 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
578 unsigned long flags;
579 LIST_HEAD(head);
580
581 spin_lock_irqsave(&jzchan->vchan.lock, flags);
582
583 /* Clear the DMA status and stop the transfer. */
Paul Cercueil33633582018-08-29 23:32:46 +0200584 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS, 0);
Alex Smithd894fc62015-03-18 16:16:36 +0000585 if (jzchan->desc) {
Peter Ujfalusif0dd52c2017-11-14 16:32:08 +0200586 vchan_terminate_vdesc(&jzchan->desc->vdesc);
Alex Smithd894fc62015-03-18 16:16:36 +0000587 jzchan->desc = NULL;
588 }
589
Paul Cercueil29870eb2018-08-29 23:32:49 +0200590 jz4780_dma_chan_disable(jzdma, jzchan->id);
591
Alex Smithd894fc62015-03-18 16:16:36 +0000592 vchan_get_all_descriptors(&jzchan->vchan, &head);
593
594 spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
595
596 vchan_dma_desc_free_list(&jzchan->vchan, &head);
597 return 0;
598}
599
Peter Ujfalusif0dd52c2017-11-14 16:32:08 +0200600static void jz4780_dma_synchronize(struct dma_chan *chan)
601{
602 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
Paul Cercueil29870eb2018-08-29 23:32:49 +0200603 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
Peter Ujfalusif0dd52c2017-11-14 16:32:08 +0200604
605 vchan_synchronize(&jzchan->vchan);
Paul Cercueil29870eb2018-08-29 23:32:49 +0200606 jz4780_dma_chan_disable(jzdma, jzchan->id);
Peter Ujfalusif0dd52c2017-11-14 16:32:08 +0200607}
608
Alex Smith46fa51682015-07-24 17:24:20 +0100609static int jz4780_dma_config(struct dma_chan *chan,
610 struct dma_slave_config *config)
Alex Smithd894fc62015-03-18 16:16:36 +0000611{
Alex Smith46fa51682015-07-24 17:24:20 +0100612 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
613
Alex Smithd894fc62015-03-18 16:16:36 +0000614 if ((config->src_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES)
615 || (config->dst_addr_width == DMA_SLAVE_BUSWIDTH_8_BYTES))
616 return -EINVAL;
617
618 /* Copy the reset of the slave configuration, it is used later. */
619 memcpy(&jzchan->config, config, sizeof(jzchan->config));
620
621 return 0;
622}
623
624static size_t jz4780_dma_desc_residue(struct jz4780_dma_chan *jzchan,
625 struct jz4780_dma_desc *desc, unsigned int next_sg)
626{
627 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
Daniel Silsbyf3c045d2018-08-29 23:32:54 +0200628 unsigned int count = 0;
Alex Smithd894fc62015-03-18 16:16:36 +0000629 unsigned int i;
630
Alex Smithd894fc62015-03-18 16:16:36 +0000631 for (i = next_sg; i < desc->count; i++)
Daniel Silsbyf3c045d2018-08-29 23:32:54 +0200632 count += desc->desc[i].dtc & GENMASK(23, 0);
Alex Smithd894fc62015-03-18 16:16:36 +0000633
Daniel Silsbyf3c045d2018-08-29 23:32:54 +0200634 if (next_sg != 0)
635 count += jz4780_dma_chn_readl(jzdma, jzchan->id,
Paul Cercueil33633582018-08-29 23:32:46 +0200636 JZ_DMA_REG_DTC);
Alex Smithd894fc62015-03-18 16:16:36 +0000637
Daniel Silsbyf3c045d2018-08-29 23:32:54 +0200638 return count << jzchan->transfer_shift;
Alex Smithd894fc62015-03-18 16:16:36 +0000639}
640
641static enum dma_status jz4780_dma_tx_status(struct dma_chan *chan,
642 dma_cookie_t cookie, struct dma_tx_state *txstate)
643{
644 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
645 struct virt_dma_desc *vdesc;
646 enum dma_status status;
647 unsigned long flags;
Daniel Silsby1f0b0f22018-08-29 23:32:57 +0200648 unsigned long residue = 0;
Alex Smithd894fc62015-03-18 16:16:36 +0000649
Paul Cercueilbaf6fd92020-10-04 16:03:07 +0200650 spin_lock_irqsave(&jzchan->vchan.lock, flags);
651
Alex Smithd894fc62015-03-18 16:16:36 +0000652 status = dma_cookie_status(chan, cookie, txstate);
653 if ((status == DMA_COMPLETE) || (txstate == NULL))
Paul Cercueilbaf6fd92020-10-04 16:03:07 +0200654 goto out_unlock_irqrestore;
Alex Smithd894fc62015-03-18 16:16:36 +0000655
656 vdesc = vchan_find_desc(&jzchan->vchan, cookie);
657 if (vdesc) {
658 /* On the issued list, so hasn't been processed yet */
Daniel Silsby1f0b0f22018-08-29 23:32:57 +0200659 residue = jz4780_dma_desc_residue(jzchan,
Alex Smithd894fc62015-03-18 16:16:36 +0000660 to_jz4780_dma_desc(vdesc), 0);
661 } else if (cookie == jzchan->desc->vdesc.tx.cookie) {
Daniel Silsby1f0b0f22018-08-29 23:32:57 +0200662 residue = jz4780_dma_desc_residue(jzchan, jzchan->desc,
Daniel Silsby83ef4fb2018-08-29 23:32:56 +0200663 jzchan->curr_hwdesc + 1);
Daniel Silsby1f0b0f22018-08-29 23:32:57 +0200664 }
665 dma_set_residue(txstate, residue);
Alex Smithd894fc62015-03-18 16:16:36 +0000666
667 if (vdesc && jzchan->desc && vdesc == &jzchan->desc->vdesc
Alex Smith839896e2015-07-24 17:24:22 +0100668 && jzchan->desc->status & (JZ_DMA_DCS_AR | JZ_DMA_DCS_HLT))
669 status = DMA_ERROR;
Alex Smithd894fc62015-03-18 16:16:36 +0000670
Paul Cercueilbaf6fd92020-10-04 16:03:07 +0200671out_unlock_irqrestore:
Alex Smithd894fc62015-03-18 16:16:36 +0000672 spin_unlock_irqrestore(&jzchan->vchan.lock, flags);
673 return status;
674}
675
Paul Cercueil4e4106f2019-05-04 23:37:57 +0200676static bool jz4780_dma_chan_irq(struct jz4780_dma_dev *jzdma,
677 struct jz4780_dma_chan *jzchan)
Alex Smithd894fc62015-03-18 16:16:36 +0000678{
Paul Cercueilf4c255f2019-07-14 17:55:04 -0400679 const unsigned int soc_flags = jzdma->soc_data->flags;
680 struct jz4780_dma_desc *desc = jzchan->desc;
Paul Cercueilc8c0cda2021-12-06 17:42:58 +0000681 u32 dcs;
Paul Cercueil4e4106f2019-05-04 23:37:57 +0200682 bool ack = true;
Alex Smithd894fc62015-03-18 16:16:36 +0000683
684 spin_lock(&jzchan->vchan.lock);
685
Paul Cercueil33633582018-08-29 23:32:46 +0200686 dcs = jz4780_dma_chn_readl(jzdma, jzchan->id, JZ_DMA_REG_DCS);
687 jz4780_dma_chn_writel(jzdma, jzchan->id, JZ_DMA_REG_DCS, 0);
Alex Smithd894fc62015-03-18 16:16:36 +0000688
689 if (dcs & JZ_DMA_DCS_AR) {
690 dev_warn(&jzchan->vchan.chan.dev->device,
691 "address error (DCS=0x%x)\n", dcs);
692 }
693
694 if (dcs & JZ_DMA_DCS_HLT) {
695 dev_warn(&jzchan->vchan.chan.dev->device,
696 "channel halt (DCS=0x%x)\n", dcs);
697 }
698
699 if (jzchan->desc) {
700 jzchan->desc->status = dcs;
701
702 if ((dcs & (JZ_DMA_DCS_AR | JZ_DMA_DCS_HLT)) == 0) {
703 if (jzchan->desc->type == DMA_CYCLIC) {
704 vchan_cyclic_callback(&jzchan->desc->vdesc);
Paul Cercueil4e4106f2019-05-04 23:37:57 +0200705
706 jz4780_dma_begin(jzchan);
707 } else if (dcs & JZ_DMA_DCS_TT) {
Paul Cercueilf4c255f2019-07-14 17:55:04 -0400708 if (!(soc_flags & JZ_SOC_DATA_BREAK_LINKS) ||
709 (jzchan->curr_hwdesc + 1 == desc->count)) {
710 vchan_cookie_complete(&desc->vdesc);
711 jzchan->desc = NULL;
712 }
Alex Smithd894fc62015-03-18 16:16:36 +0000713
Paul Cercueil4e4106f2019-05-04 23:37:57 +0200714 jz4780_dma_begin(jzchan);
715 } else {
716 /* False positive - continue the transfer */
717 ack = false;
718 jz4780_dma_chn_writel(jzdma, jzchan->id,
719 JZ_DMA_REG_DCS,
720 JZ_DMA_DCS_CTE);
721 }
Alex Smithd894fc62015-03-18 16:16:36 +0000722 }
723 } else {
724 dev_err(&jzchan->vchan.chan.dev->device,
725 "channel IRQ with no active transfer\n");
726 }
727
728 spin_unlock(&jzchan->vchan.lock);
Paul Cercueil4e4106f2019-05-04 23:37:57 +0200729
730 return ack;
Alex Smithd894fc62015-03-18 16:16:36 +0000731}
732
733static irqreturn_t jz4780_dma_irq_handler(int irq, void *data)
734{
735 struct jz4780_dma_dev *jzdma = data;
Paul Cercueil4e4106f2019-05-04 23:37:57 +0200736 unsigned int nb_channels = jzdma->soc_data->nb_channels;
Dan Carpenter4c89cc72019-06-24 16:49:40 +0300737 unsigned long pending;
Paul Cercueilc8c0cda2021-12-06 17:42:58 +0000738 u32 dmac;
Alex Smithd894fc62015-03-18 16:16:36 +0000739 int i;
740
Paul Cercueil33633582018-08-29 23:32:46 +0200741 pending = jz4780_dma_ctrl_readl(jzdma, JZ_DMA_REG_DIRQP);
Alex Smithd894fc62015-03-18 16:16:36 +0000742
Dan Carpenter4c89cc72019-06-24 16:49:40 +0300743 for_each_set_bit(i, &pending, nb_channels) {
Paul Cercueil4e4106f2019-05-04 23:37:57 +0200744 if (jz4780_dma_chan_irq(jzdma, &jzdma->chan[i]))
745 pending &= ~BIT(i);
Alex Smithd894fc62015-03-18 16:16:36 +0000746 }
747
748 /* Clear halt and address error status of all channels. */
Paul Cercueil33633582018-08-29 23:32:46 +0200749 dmac = jz4780_dma_ctrl_readl(jzdma, JZ_DMA_REG_DMAC);
Alex Smithd894fc62015-03-18 16:16:36 +0000750 dmac &= ~(JZ_DMA_DMAC_HLT | JZ_DMA_DMAC_AR);
Paul Cercueil33633582018-08-29 23:32:46 +0200751 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DMAC, dmac);
Alex Smithd894fc62015-03-18 16:16:36 +0000752
753 /* Clear interrupt pending status. */
Paul Cercueil4e4106f2019-05-04 23:37:57 +0200754 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DIRQP, pending);
Alex Smithd894fc62015-03-18 16:16:36 +0000755
756 return IRQ_HANDLED;
757}
758
759static int jz4780_dma_alloc_chan_resources(struct dma_chan *chan)
760{
761 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
762
763 jzchan->desc_pool = dma_pool_create(dev_name(&chan->dev->device),
764 chan->device->dev,
765 JZ_DMA_DESC_BLOCK_SIZE,
766 PAGE_SIZE, 0);
767 if (!jzchan->desc_pool) {
768 dev_err(&chan->dev->device,
769 "failed to allocate descriptor pool\n");
770 return -ENOMEM;
771 }
772
773 return 0;
774}
775
776static void jz4780_dma_free_chan_resources(struct dma_chan *chan)
777{
778 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
779
780 vchan_free_chan_resources(&jzchan->vchan);
781 dma_pool_destroy(jzchan->desc_pool);
782 jzchan->desc_pool = NULL;
783}
784
785static bool jz4780_dma_filter_fn(struct dma_chan *chan, void *param)
786{
787 struct jz4780_dma_chan *jzchan = to_jz4780_dma_chan(chan);
788 struct jz4780_dma_dev *jzdma = jz4780_dma_chan_parent(jzchan);
Alex Smith026fd402015-07-24 17:24:24 +0100789 struct jz4780_dma_filter_data *data = param;
790
Alex Smithd894fc62015-03-18 16:16:36 +0000791
792 if (data->channel > -1) {
793 if (data->channel != jzchan->id)
794 return false;
795 } else if (jzdma->chan_reserved & BIT(jzchan->id)) {
796 return false;
797 }
798
Paul Cercueil76a09662021-12-06 17:42:59 +0000799 jzchan->transfer_type_tx = data->transfer_type_tx;
800 jzchan->transfer_type_rx = data->transfer_type_rx;
Alex Smithd894fc62015-03-18 16:16:36 +0000801
802 return true;
803}
804
805static struct dma_chan *jz4780_of_dma_xlate(struct of_phandle_args *dma_spec,
806 struct of_dma *ofdma)
807{
808 struct jz4780_dma_dev *jzdma = ofdma->of_dma_data;
809 dma_cap_mask_t mask = jzdma->dma_device.cap_mask;
Alex Smith026fd402015-07-24 17:24:24 +0100810 struct jz4780_dma_filter_data data;
Alex Smithd894fc62015-03-18 16:16:36 +0000811
Paul Cercueil76a09662021-12-06 17:42:59 +0000812 if (dma_spec->args_count == 2) {
813 data.transfer_type_tx = dma_spec->args[0];
814 data.transfer_type_rx = dma_spec->args[0];
815 data.channel = dma_spec->args[1];
816 } else if (dma_spec->args_count == 3) {
817 data.transfer_type_tx = dma_spec->args[0];
818 data.transfer_type_rx = dma_spec->args[1];
819 data.channel = dma_spec->args[2];
820 } else {
Alex Smithd894fc62015-03-18 16:16:36 +0000821 return NULL;
Paul Cercueil76a09662021-12-06 17:42:59 +0000822 }
Alex Smithd894fc62015-03-18 16:16:36 +0000823
824 if (data.channel > -1) {
Paul Cercueil6147b032018-08-29 23:32:45 +0200825 if (data.channel >= jzdma->soc_data->nb_channels) {
Alex Smithd894fc62015-03-18 16:16:36 +0000826 dev_err(jzdma->dma_device.dev,
827 "device requested non-existent channel %u\n",
828 data.channel);
829 return NULL;
830 }
831
832 /* Can only select a channel marked as reserved. */
833 if (!(jzdma->chan_reserved & BIT(data.channel))) {
834 dev_err(jzdma->dma_device.dev,
835 "device requested unreserved channel %u\n",
836 data.channel);
837 return NULL;
838 }
Alex Smithd894fc62015-03-18 16:16:36 +0000839
Paul Cercueil76a09662021-12-06 17:42:59 +0000840 jzdma->chan[data.channel].transfer_type_tx = data.transfer_type_tx;
841 jzdma->chan[data.channel].transfer_type_rx = data.transfer_type_rx;
Alex Smithd3273e12015-07-24 17:24:23 +0100842
843 return dma_get_slave_channel(
844 &jzdma->chan[data.channel].vchan.chan);
845 } else {
Baolin Wangc88ba7b2019-05-20 19:32:17 +0800846 return __dma_request_channel(&mask, jz4780_dma_filter_fn, &data,
847 ofdma->of_node);
Alex Smithd3273e12015-07-24 17:24:23 +0100848 }
Alex Smithd894fc62015-03-18 16:16:36 +0000849}
850
851static int jz4780_dma_probe(struct platform_device *pdev)
852{
853 struct device *dev = &pdev->dev;
Paul Cercueil6147b032018-08-29 23:32:45 +0200854 const struct jz4780_dma_soc_data *soc_data;
Alex Smithd894fc62015-03-18 16:16:36 +0000855 struct jz4780_dma_dev *jzdma;
856 struct jz4780_dma_chan *jzchan;
857 struct dma_device *dd;
858 struct resource *res;
859 int i, ret;
860
Paul Cercueil54f919a2018-08-29 23:32:44 +0200861 if (!dev->of_node) {
862 dev_err(dev, "This driver must be probed from devicetree\n");
863 return -EINVAL;
864 }
865
Paul Cercueil6147b032018-08-29 23:32:45 +0200866 soc_data = device_get_match_data(dev);
867 if (!soc_data)
868 return -EINVAL;
869
Gustavo A. R. Silvaed414d52018-12-24 00:52:17 -0600870 jzdma = devm_kzalloc(dev, struct_size(jzdma, chan,
871 soc_data->nb_channels), GFP_KERNEL);
Alex Smithd894fc62015-03-18 16:16:36 +0000872 if (!jzdma)
873 return -ENOMEM;
874
Paul Cercueil6147b032018-08-29 23:32:45 +0200875 jzdma->soc_data = soc_data;
Alex Smithd894fc62015-03-18 16:16:36 +0000876 platform_set_drvdata(pdev, jzdma);
877
Markus Elfring1148ac62019-09-22 11:18:27 +0200878 jzdma->chn_base = devm_platform_ioremap_resource(pdev, 0);
Paul Cercueil33633582018-08-29 23:32:46 +0200879 if (IS_ERR(jzdma->chn_base))
880 return PTR_ERR(jzdma->chn_base);
881
882 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
883 if (res) {
884 jzdma->ctrl_base = devm_ioremap_resource(dev, res);
885 if (IS_ERR(jzdma->ctrl_base))
886 return PTR_ERR(jzdma->ctrl_base);
Paul Cercueil29870eb2018-08-29 23:32:49 +0200887 } else if (soc_data->flags & JZ_SOC_DATA_ALLOW_LEGACY_DT) {
Paul Cercueil33633582018-08-29 23:32:46 +0200888 /*
889 * On JZ4780, if the second memory resource was not supplied,
890 * assume we're using an old devicetree, and calculate the
891 * offset to the control registers.
892 */
893 jzdma->ctrl_base = jzdma->chn_base + JZ4780_DMA_CTRL_OFFSET;
Paul Cercueil29870eb2018-08-29 23:32:49 +0200894 } else {
895 dev_err(dev, "failed to get I/O memory\n");
896 return -EINVAL;
Paul Cercueil33633582018-08-29 23:32:46 +0200897 }
Alex Smithd894fc62015-03-18 16:16:36 +0000898
Alex Smithd894fc62015-03-18 16:16:36 +0000899 jzdma->clk = devm_clk_get(dev, NULL);
900 if (IS_ERR(jzdma->clk)) {
901 dev_err(dev, "failed to get clock\n");
Alex Smithd509a832015-07-24 17:24:26 +0100902 ret = PTR_ERR(jzdma->clk);
Madhuparna Bhowmik6d6018f2020-08-21 09:14:23 +0530903 return ret;
Alex Smithd894fc62015-03-18 16:16:36 +0000904 }
905
906 clk_prepare_enable(jzdma->clk);
907
908 /* Property is optional, if it doesn't exist the value will remain 0. */
909 of_property_read_u32_index(dev->of_node, "ingenic,reserved-channels",
910 0, &jzdma->chan_reserved);
911
912 dd = &jzdma->dma_device;
913
914 dma_cap_set(DMA_MEMCPY, dd->cap_mask);
915 dma_cap_set(DMA_SLAVE, dd->cap_mask);
916 dma_cap_set(DMA_CYCLIC, dd->cap_mask);
917
918 dd->dev = dev;
Maxime Ripard77a68e52015-07-20 10:41:32 +0200919 dd->copy_align = DMAENGINE_ALIGN_4_BYTES;
Alex Smithd894fc62015-03-18 16:16:36 +0000920 dd->device_alloc_chan_resources = jz4780_dma_alloc_chan_resources;
921 dd->device_free_chan_resources = jz4780_dma_free_chan_resources;
922 dd->device_prep_slave_sg = jz4780_dma_prep_slave_sg;
923 dd->device_prep_dma_cyclic = jz4780_dma_prep_dma_cyclic;
924 dd->device_prep_dma_memcpy = jz4780_dma_prep_dma_memcpy;
Alex Smith46fa51682015-07-24 17:24:20 +0100925 dd->device_config = jz4780_dma_config;
Alex Smithd894fc62015-03-18 16:16:36 +0000926 dd->device_terminate_all = jz4780_dma_terminate_all;
Peter Ujfalusif0dd52c2017-11-14 16:32:08 +0200927 dd->device_synchronize = jz4780_dma_synchronize;
Alex Smithd894fc62015-03-18 16:16:36 +0000928 dd->device_tx_status = jz4780_dma_tx_status;
929 dd->device_issue_pending = jz4780_dma_issue_pending;
930 dd->src_addr_widths = JZ_DMA_BUSWIDTHS;
931 dd->dst_addr_widths = JZ_DMA_BUSWIDTHS;
932 dd->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
933 dd->residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Artur Rojekd59f7032021-08-29 21:58:05 +0200934 dd->max_sg_burst = JZ_DMA_MAX_DESC;
Alex Smithd894fc62015-03-18 16:16:36 +0000935
Alex Smithd894fc62015-03-18 16:16:36 +0000936 /*
937 * Enable DMA controller, mark all channels as not programmable.
938 * Also set the FMSC bit - it increases MSC performance, so it makes
939 * little sense not to enable it.
940 */
Paul Cercueil17a8e302018-08-29 23:32:52 +0200941 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DMAC, JZ_DMA_DMAC_DMAE |
942 JZ_DMA_DMAC_FAIC | JZ_DMA_DMAC_FMSC);
Paul Cercueil29870eb2018-08-29 23:32:49 +0200943
944 if (soc_data->flags & JZ_SOC_DATA_PROGRAMMABLE_DMA)
945 jz4780_dma_ctrl_writel(jzdma, JZ_DMA_REG_DMACP, 0);
Alex Smithd894fc62015-03-18 16:16:36 +0000946
947 INIT_LIST_HEAD(&dd->channels);
948
Paul Cercueil6147b032018-08-29 23:32:45 +0200949 for (i = 0; i < soc_data->nb_channels; i++) {
Alex Smithd894fc62015-03-18 16:16:36 +0000950 jzchan = &jzdma->chan[i];
951 jzchan->id = i;
952
953 vchan_init(&jzchan->vchan, dd);
954 jzchan->vchan.desc_free = jz4780_dma_desc_free;
955 }
956
Paul Cercueilb72cbb12021-12-06 17:42:56 +0000957 /*
958 * On JZ4760, chan0 won't enable properly the first time.
959 * Enabling then disabling chan1 will magically make chan0 work
960 * correctly.
961 */
962 jz4780_dma_chan_enable(jzdma, 1);
963 jz4780_dma_chan_disable(jzdma, 1);
964
Madhuparna Bhowmik6d6018f2020-08-21 09:14:23 +0530965 ret = platform_get_irq(pdev, 0);
966 if (ret < 0)
967 goto err_disable_clk;
968
969 jzdma->irq = ret;
970
971 ret = request_irq(jzdma->irq, jz4780_dma_irq_handler, 0, dev_name(dev),
972 jzdma);
973 if (ret) {
974 dev_err(dev, "failed to request IRQ %u!\n", jzdma->irq);
975 goto err_disable_clk;
976 }
977
Huang Shijie0f5a5e52018-08-06 16:52:28 +0800978 ret = dmaenginem_async_device_register(dd);
Alex Smithd894fc62015-03-18 16:16:36 +0000979 if (ret) {
980 dev_err(dev, "failed to register device\n");
Madhuparna Bhowmik6d6018f2020-08-21 09:14:23 +0530981 goto err_free_irq;
Alex Smithd894fc62015-03-18 16:16:36 +0000982 }
983
984 /* Register with OF DMA helpers. */
985 ret = of_dma_controller_register(dev->of_node, jz4780_of_dma_xlate,
986 jzdma);
987 if (ret) {
988 dev_err(dev, "failed to register OF DMA controller\n");
Madhuparna Bhowmik6d6018f2020-08-21 09:14:23 +0530989 goto err_free_irq;
Alex Smithd894fc62015-03-18 16:16:36 +0000990 }
991
992 dev_info(dev, "JZ4780 DMA controller initialised\n");
993 return 0;
994
Alex Smithd509a832015-07-24 17:24:26 +0100995err_free_irq:
996 free_irq(jzdma->irq, jzdma);
Madhuparna Bhowmik6d6018f2020-08-21 09:14:23 +0530997
998err_disable_clk:
999 clk_disable_unprepare(jzdma->clk);
Alex Smithd894fc62015-03-18 16:16:36 +00001000 return ret;
1001}
1002
1003static int jz4780_dma_remove(struct platform_device *pdev)
1004{
1005 struct jz4780_dma_dev *jzdma = platform_get_drvdata(pdev);
Alex Smithae9c02b2015-07-24 17:24:27 +01001006 int i;
Alex Smithd894fc62015-03-18 16:16:36 +00001007
1008 of_dma_controller_free(pdev->dev.of_node);
Alex Smithae9c02b2015-07-24 17:24:27 +01001009
Chuhong Yuan9568fed2019-11-05 00:16:22 +08001010 clk_disable_unprepare(jzdma->clk);
Alex Smithd509a832015-07-24 17:24:26 +01001011 free_irq(jzdma->irq, jzdma);
Alex Smithae9c02b2015-07-24 17:24:27 +01001012
Paul Cercueil6147b032018-08-29 23:32:45 +02001013 for (i = 0; i < jzdma->soc_data->nb_channels; i++)
Alex Smithae9c02b2015-07-24 17:24:27 +01001014 tasklet_kill(&jzdma->chan[i].vchan.task);
1015
Alex Smithd894fc62015-03-18 16:16:36 +00001016 return 0;
1017}
1018
Paul Cercueilffaaa8c2018-08-29 23:32:50 +02001019static const struct jz4780_dma_soc_data jz4740_dma_soc_data = {
1020 .nb_channels = 6,
1021 .transfer_ord_max = 5,
Paul Cercueilf4c255f2019-07-14 17:55:04 -04001022 .flags = JZ_SOC_DATA_BREAK_LINKS,
Paul Cercueilffaaa8c2018-08-29 23:32:50 +02001023};
1024
Paul Cercueilae9156b2018-08-29 23:32:51 +02001025static const struct jz4780_dma_soc_data jz4725b_dma_soc_data = {
1026 .nb_channels = 6,
1027 .transfer_ord_max = 5,
Paul Cercueila40c94b2019-12-10 17:55:45 +01001028 .flags = JZ_SOC_DATA_PER_CHAN_PM | JZ_SOC_DATA_NO_DCKES_DCKEC |
1029 JZ_SOC_DATA_BREAK_LINKS,
Paul Cercueilae9156b2018-08-29 23:32:51 +02001030};
1031
Paul Cercueild2852a32021-01-20 10:53:22 +00001032static const struct jz4780_dma_soc_data jz4760_dma_soc_data = {
1033 .nb_channels = 5,
1034 .transfer_ord_max = 6,
1035 .flags = JZ_SOC_DATA_PER_CHAN_PM | JZ_SOC_DATA_NO_DCKES_DCKEC,
1036};
1037
Paul Cercueil3d70fcc2021-12-06 17:42:57 +00001038static const struct jz4780_dma_soc_data jz4760_mdma_soc_data = {
1039 .nb_channels = 2,
1040 .transfer_ord_max = 6,
1041 .flags = JZ_SOC_DATA_PER_CHAN_PM | JZ_SOC_DATA_NO_DCKES_DCKEC,
1042};
1043
1044static const struct jz4780_dma_soc_data jz4760_bdma_soc_data = {
1045 .nb_channels = 3,
1046 .transfer_ord_max = 6,
1047 .flags = JZ_SOC_DATA_PER_CHAN_PM | JZ_SOC_DATA_NO_DCKES_DCKEC,
1048};
1049
Paul Cercueild2852a32021-01-20 10:53:22 +00001050static const struct jz4780_dma_soc_data jz4760b_dma_soc_data = {
1051 .nb_channels = 5,
1052 .transfer_ord_max = 6,
1053 .flags = JZ_SOC_DATA_PER_CHAN_PM,
1054};
1055
Paul Cercueil3d70fcc2021-12-06 17:42:57 +00001056static const struct jz4780_dma_soc_data jz4760b_mdma_soc_data = {
1057 .nb_channels = 2,
1058 .transfer_ord_max = 6,
1059 .flags = JZ_SOC_DATA_PER_CHAN_PM,
1060};
1061
1062static const struct jz4780_dma_soc_data jz4760b_bdma_soc_data = {
1063 .nb_channels = 3,
1064 .transfer_ord_max = 6,
1065 .flags = JZ_SOC_DATA_PER_CHAN_PM,
1066};
1067
Paul Cercueil29870eb2018-08-29 23:32:49 +02001068static const struct jz4780_dma_soc_data jz4770_dma_soc_data = {
1069 .nb_channels = 6,
1070 .transfer_ord_max = 6,
1071 .flags = JZ_SOC_DATA_PER_CHAN_PM,
1072};
1073
Paul Cercueil6147b032018-08-29 23:32:45 +02001074static const struct jz4780_dma_soc_data jz4780_dma_soc_data = {
1075 .nb_channels = 32,
Paul Cercueil29870eb2018-08-29 23:32:49 +02001076 .transfer_ord_max = 7,
1077 .flags = JZ_SOC_DATA_ALLOW_LEGACY_DT | JZ_SOC_DATA_PROGRAMMABLE_DMA,
Paul Cercueil6147b032018-08-29 23:32:45 +02001078};
1079
Zhou Yanjiefee175e2019-10-25 01:21:10 +08001080static const struct jz4780_dma_soc_data x1000_dma_soc_data = {
1081 .nb_channels = 8,
1082 .transfer_ord_max = 7,
1083 .flags = JZ_SOC_DATA_PROGRAMMABLE_DMA,
1084};
1085
周琰杰 (Zhou Yanjie)20f5a652019-12-17 21:59:00 +08001086static const struct jz4780_dma_soc_data x1830_dma_soc_data = {
1087 .nb_channels = 32,
1088 .transfer_ord_max = 7,
1089 .flags = JZ_SOC_DATA_PROGRAMMABLE_DMA,
1090};
1091
Alex Smithd894fc62015-03-18 16:16:36 +00001092static const struct of_device_id jz4780_dma_dt_match[] = {
Paul Cercueilffaaa8c2018-08-29 23:32:50 +02001093 { .compatible = "ingenic,jz4740-dma", .data = &jz4740_dma_soc_data },
Paul Cercueilae9156b2018-08-29 23:32:51 +02001094 { .compatible = "ingenic,jz4725b-dma", .data = &jz4725b_dma_soc_data },
Paul Cercueild2852a32021-01-20 10:53:22 +00001095 { .compatible = "ingenic,jz4760-dma", .data = &jz4760_dma_soc_data },
Paul Cercueil3d70fcc2021-12-06 17:42:57 +00001096 { .compatible = "ingenic,jz4760-mdma", .data = &jz4760_mdma_soc_data },
1097 { .compatible = "ingenic,jz4760-bdma", .data = &jz4760_bdma_soc_data },
Paul Cercueild2852a32021-01-20 10:53:22 +00001098 { .compatible = "ingenic,jz4760b-dma", .data = &jz4760b_dma_soc_data },
Paul Cercueil3d70fcc2021-12-06 17:42:57 +00001099 { .compatible = "ingenic,jz4760b-mdma", .data = &jz4760b_mdma_soc_data },
1100 { .compatible = "ingenic,jz4760b-bdma", .data = &jz4760b_bdma_soc_data },
Paul Cercueil29870eb2018-08-29 23:32:49 +02001101 { .compatible = "ingenic,jz4770-dma", .data = &jz4770_dma_soc_data },
Paul Cercueil6147b032018-08-29 23:32:45 +02001102 { .compatible = "ingenic,jz4780-dma", .data = &jz4780_dma_soc_data },
Zhou Yanjiefee175e2019-10-25 01:21:10 +08001103 { .compatible = "ingenic,x1000-dma", .data = &x1000_dma_soc_data },
周琰杰 (Zhou Yanjie)20f5a652019-12-17 21:59:00 +08001104 { .compatible = "ingenic,x1830-dma", .data = &x1830_dma_soc_data },
Alex Smithd894fc62015-03-18 16:16:36 +00001105 {},
1106};
1107MODULE_DEVICE_TABLE(of, jz4780_dma_dt_match);
1108
1109static struct platform_driver jz4780_dma_driver = {
1110 .probe = jz4780_dma_probe,
1111 .remove = jz4780_dma_remove,
1112 .driver = {
1113 .name = "jz4780-dma",
Krzysztof Kozlowski255c2cc2020-11-20 17:22:58 +01001114 .of_match_table = jz4780_dma_dt_match,
Alex Smithd894fc62015-03-18 16:16:36 +00001115 },
1116};
1117
1118static int __init jz4780_dma_init(void)
1119{
1120 return platform_driver_register(&jz4780_dma_driver);
1121}
1122subsys_initcall(jz4780_dma_init);
1123
1124static void __exit jz4780_dma_exit(void)
1125{
1126 platform_driver_unregister(&jz4780_dma_driver);
1127}
1128module_exit(jz4780_dma_exit);
1129
1130MODULE_AUTHOR("Alex Smith <alex@alex-smith.me.uk>");
1131MODULE_DESCRIPTION("Ingenic JZ4780 DMA controller driver");
1132MODULE_LICENSE("GPL");