blob: 7f158ef5672d79efba7beaf5fe98adafa4f73be0 [file] [log] [blame]
Baolin Wang9b3b8172017-10-24 13:47:50 +08001/*
2 * Copyright (C) 2017 Spreadtrum Communications Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0
5 */
6
7#include <linux/clk.h>
8#include <linux/dma-mapping.h>
Eric Longab42ddb2018-04-19 10:00:48 +08009#include <linux/dma/sprd-dma.h>
Baolin Wang9b3b8172017-10-24 13:47:50 +080010#include <linux/errno.h>
11#include <linux/init.h>
12#include <linux/interrupt.h>
13#include <linux/io.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/of_dma.h>
18#include <linux/of_device.h>
19#include <linux/pm_runtime.h>
20#include <linux/slab.h>
21
22#include "virt-dma.h"
23
24#define SPRD_DMA_CHN_REG_OFFSET 0x1000
25#define SPRD_DMA_CHN_REG_LENGTH 0x40
26#define SPRD_DMA_MEMCPY_MIN_SIZE 64
27
28/* DMA global registers definition */
29#define SPRD_DMA_GLB_PAUSE 0x0
30#define SPRD_DMA_GLB_FRAG_WAIT 0x4
31#define SPRD_DMA_GLB_REQ_PEND0_EN 0x8
32#define SPRD_DMA_GLB_REQ_PEND1_EN 0xc
33#define SPRD_DMA_GLB_INT_RAW_STS 0x10
34#define SPRD_DMA_GLB_INT_MSK_STS 0x14
35#define SPRD_DMA_GLB_REQ_STS 0x18
36#define SPRD_DMA_GLB_CHN_EN_STS 0x1c
37#define SPRD_DMA_GLB_DEBUG_STS 0x20
38#define SPRD_DMA_GLB_ARB_SEL_STS 0x24
Eric Long770399d2018-11-06 13:01:36 +080039#define SPRD_DMA_GLB_2STAGE_GRP1 0x28
40#define SPRD_DMA_GLB_2STAGE_GRP2 0x2c
Baolin Wang9b3b8172017-10-24 13:47:50 +080041#define SPRD_DMA_GLB_REQ_UID(uid) (0x4 * ((uid) - 1))
42#define SPRD_DMA_GLB_REQ_UID_OFFSET 0x2000
43
44/* DMA channel registers definition */
45#define SPRD_DMA_CHN_PAUSE 0x0
46#define SPRD_DMA_CHN_REQ 0x4
47#define SPRD_DMA_CHN_CFG 0x8
48#define SPRD_DMA_CHN_INTC 0xc
49#define SPRD_DMA_CHN_SRC_ADDR 0x10
50#define SPRD_DMA_CHN_DES_ADDR 0x14
51#define SPRD_DMA_CHN_FRG_LEN 0x18
52#define SPRD_DMA_CHN_BLK_LEN 0x1c
53#define SPRD_DMA_CHN_TRSC_LEN 0x20
54#define SPRD_DMA_CHN_TRSF_STEP 0x24
55#define SPRD_DMA_CHN_WARP_PTR 0x28
56#define SPRD_DMA_CHN_WARP_TO 0x2c
57#define SPRD_DMA_CHN_LLIST_PTR 0x30
58#define SPRD_DMA_CHN_FRAG_STEP 0x34
59#define SPRD_DMA_CHN_SRC_BLK_STEP 0x38
60#define SPRD_DMA_CHN_DES_BLK_STEP 0x3c
61
Eric Long770399d2018-11-06 13:01:36 +080062/* SPRD_DMA_GLB_2STAGE_GRP register definition */
63#define SPRD_DMA_GLB_2STAGE_EN BIT(24)
64#define SPRD_DMA_GLB_CHN_INT_MASK GENMASK(23, 20)
Baolin Wang9bb9fe02019-05-06 15:28:33 +080065#define SPRD_DMA_GLB_DEST_INT BIT(22)
66#define SPRD_DMA_GLB_SRC_INT BIT(20)
Eric Long770399d2018-11-06 13:01:36 +080067#define SPRD_DMA_GLB_LIST_DONE_TRG BIT(19)
68#define SPRD_DMA_GLB_TRANS_DONE_TRG BIT(18)
69#define SPRD_DMA_GLB_BLOCK_DONE_TRG BIT(17)
70#define SPRD_DMA_GLB_FRAG_DONE_TRG BIT(16)
71#define SPRD_DMA_GLB_TRG_OFFSET 16
72#define SPRD_DMA_GLB_DEST_CHN_MASK GENMASK(13, 8)
73#define SPRD_DMA_GLB_DEST_CHN_OFFSET 8
74#define SPRD_DMA_GLB_SRC_CHN_MASK GENMASK(5, 0)
75
Baolin Wang9b3b8172017-10-24 13:47:50 +080076/* SPRD_DMA_CHN_INTC register definition */
77#define SPRD_DMA_INT_MASK GENMASK(4, 0)
78#define SPRD_DMA_INT_CLR_OFFSET 24
79#define SPRD_DMA_FRAG_INT_EN BIT(0)
80#define SPRD_DMA_BLK_INT_EN BIT(1)
81#define SPRD_DMA_TRANS_INT_EN BIT(2)
82#define SPRD_DMA_LIST_INT_EN BIT(3)
83#define SPRD_DMA_CFG_ERR_INT_EN BIT(4)
84
85/* SPRD_DMA_CHN_CFG register definition */
86#define SPRD_DMA_CHN_EN BIT(0)
Eric Long4ac69542018-08-28 19:09:07 +080087#define SPRD_DMA_LINKLIST_EN BIT(4)
Baolin Wang9b3b8172017-10-24 13:47:50 +080088#define SPRD_DMA_WAIT_BDONE_OFFSET 24
89#define SPRD_DMA_DONOT_WAIT_BDONE 1
90
91/* SPRD_DMA_CHN_REQ register definition */
92#define SPRD_DMA_REQ_EN BIT(0)
93
94/* SPRD_DMA_CHN_PAUSE register definition */
95#define SPRD_DMA_PAUSE_EN BIT(0)
96#define SPRD_DMA_PAUSE_STS BIT(2)
97#define SPRD_DMA_PAUSE_CNT 0x2000
98
99/* DMA_CHN_WARP_* register definition */
100#define SPRD_DMA_HIGH_ADDR_MASK GENMASK(31, 28)
101#define SPRD_DMA_LOW_ADDR_MASK GENMASK(31, 0)
Eric Longa7e335d2019-10-23 14:31:32 +0800102#define SPRD_DMA_WRAP_ADDR_MASK GENMASK(27, 0)
Baolin Wang9b3b8172017-10-24 13:47:50 +0800103#define SPRD_DMA_HIGH_ADDR_OFFSET 4
104
105/* SPRD_DMA_CHN_INTC register definition */
106#define SPRD_DMA_FRAG_INT_STS BIT(16)
107#define SPRD_DMA_BLK_INT_STS BIT(17)
108#define SPRD_DMA_TRSC_INT_STS BIT(18)
109#define SPRD_DMA_LIST_INT_STS BIT(19)
110#define SPRD_DMA_CFGERR_INT_STS BIT(20)
111#define SPRD_DMA_CHN_INT_STS \
112 (SPRD_DMA_FRAG_INT_STS | SPRD_DMA_BLK_INT_STS | \
113 SPRD_DMA_TRSC_INT_STS | SPRD_DMA_LIST_INT_STS | \
114 SPRD_DMA_CFGERR_INT_STS)
115
116/* SPRD_DMA_CHN_FRG_LEN register definition */
117#define SPRD_DMA_SRC_DATAWIDTH_OFFSET 30
118#define SPRD_DMA_DES_DATAWIDTH_OFFSET 28
119#define SPRD_DMA_SWT_MODE_OFFSET 26
120#define SPRD_DMA_REQ_MODE_OFFSET 24
121#define SPRD_DMA_REQ_MODE_MASK GENMASK(1, 0)
Eric Longa7e335d2019-10-23 14:31:32 +0800122#define SPRD_DMA_WRAP_SEL_DEST BIT(23)
123#define SPRD_DMA_WRAP_EN BIT(22)
Baolin Wang9b3b8172017-10-24 13:47:50 +0800124#define SPRD_DMA_FIX_SEL_OFFSET 21
125#define SPRD_DMA_FIX_EN_OFFSET 20
Eric Long4ac69542018-08-28 19:09:07 +0800126#define SPRD_DMA_LLIST_END BIT(19)
Baolin Wang9b3b8172017-10-24 13:47:50 +0800127#define SPRD_DMA_FRG_LEN_MASK GENMASK(16, 0)
128
129/* SPRD_DMA_CHN_BLK_LEN register definition */
130#define SPRD_DMA_BLK_LEN_MASK GENMASK(16, 0)
131
132/* SPRD_DMA_CHN_TRSC_LEN register definition */
133#define SPRD_DMA_TRSC_LEN_MASK GENMASK(27, 0)
134
135/* SPRD_DMA_CHN_TRSF_STEP register definition */
136#define SPRD_DMA_DEST_TRSF_STEP_OFFSET 16
137#define SPRD_DMA_SRC_TRSF_STEP_OFFSET 0
138#define SPRD_DMA_TRSF_STEP_MASK GENMASK(15, 0)
139
Zhenfang Wang8b6bc5f2019-09-12 13:47:18 +0800140/* SPRD DMA_SRC_BLK_STEP register definition */
141#define SPRD_DMA_LLIST_HIGH_MASK GENMASK(31, 28)
142#define SPRD_DMA_LLIST_HIGH_SHIFT 28
143
Eric Long770399d2018-11-06 13:01:36 +0800144/* define DMA channel mode & trigger mode mask */
145#define SPRD_DMA_CHN_MODE_MASK GENMASK(7, 0)
146#define SPRD_DMA_TRG_MODE_MASK GENMASK(7, 0)
Baolin Wang9bb9fe02019-05-06 15:28:33 +0800147#define SPRD_DMA_INT_TYPE_MASK GENMASK(7, 0)
Eric Long770399d2018-11-06 13:01:36 +0800148
Eric Long6b1d2552018-04-19 10:00:46 +0800149/* define the DMA transfer step type */
150#define SPRD_DMA_NONE_STEP 0
151#define SPRD_DMA_BYTE_STEP 1
152#define SPRD_DMA_SHORT_STEP 2
153#define SPRD_DMA_WORD_STEP 4
154#define SPRD_DMA_DWORD_STEP 8
155
Baolin Wang9b3b8172017-10-24 13:47:50 +0800156#define SPRD_DMA_SOFTWARE_UID 0
157
Baolin Wangd7c33cf2018-04-19 10:00:47 +0800158/* dma data width values */
159enum sprd_dma_datawidth {
160 SPRD_DMA_DATAWIDTH_1_BYTE,
161 SPRD_DMA_DATAWIDTH_2_BYTES,
162 SPRD_DMA_DATAWIDTH_4_BYTES,
163 SPRD_DMA_DATAWIDTH_8_BYTES,
Baolin Wang9b3b8172017-10-24 13:47:50 +0800164};
165
166/* dma channel hardware configuration */
167struct sprd_dma_chn_hw {
168 u32 pause;
169 u32 req;
170 u32 cfg;
171 u32 intc;
172 u32 src_addr;
173 u32 des_addr;
174 u32 frg_len;
175 u32 blk_len;
176 u32 trsc_len;
177 u32 trsf_step;
178 u32 wrap_ptr;
179 u32 wrap_to;
180 u32 llist_ptr;
181 u32 frg_step;
182 u32 src_blk_step;
183 u32 des_blk_step;
184};
185
186/* dma request description */
187struct sprd_dma_desc {
188 struct virt_dma_desc vd;
189 struct sprd_dma_chn_hw chn_hw;
Eric Longd762ab32018-11-06 13:01:32 +0800190 enum dma_transfer_direction dir;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800191};
192
193/* dma channel description */
194struct sprd_dma_chn {
195 struct virt_dma_chan vc;
196 void __iomem *chn_base;
Eric Long4ac69542018-08-28 19:09:07 +0800197 struct sprd_dma_linklist linklist;
Eric Longca1b7d32018-05-23 17:31:11 +0800198 struct dma_slave_config slave_cfg;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800199 u32 chn_num;
200 u32 dev_id;
Eric Long770399d2018-11-06 13:01:36 +0800201 enum sprd_dma_chn_mode chn_mode;
202 enum sprd_dma_trg_mode trg_mode;
Baolin Wang9bb9fe02019-05-06 15:28:33 +0800203 enum sprd_dma_int_type int_type;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800204 struct sprd_dma_desc *cur_desc;
205};
206
207/* SPRD dma device */
208struct sprd_dma_dev {
209 struct dma_device dma_dev;
210 void __iomem *glb_base;
211 struct clk *clk;
212 struct clk *ashb_clk;
213 int irq;
214 u32 total_chns;
Gustavo A. R. Silvaa18cd9b2020-02-14 11:15:36 -0600215 struct sprd_dma_chn channels[];
Baolin Wang9b3b8172017-10-24 13:47:50 +0800216};
217
Baolin Wangec1ac302019-10-09 17:11:30 +0800218static void sprd_dma_free_desc(struct virt_dma_desc *vd);
Baolin Wang9b3b8172017-10-24 13:47:50 +0800219static bool sprd_dma_filter_fn(struct dma_chan *chan, void *param);
220static struct of_dma_filter_info sprd_dma_info = {
221 .filter_fn = sprd_dma_filter_fn,
222};
223
224static inline struct sprd_dma_chn *to_sprd_dma_chan(struct dma_chan *c)
225{
226 return container_of(c, struct sprd_dma_chn, vc.chan);
227}
228
229static inline struct sprd_dma_dev *to_sprd_dma_dev(struct dma_chan *c)
230{
231 struct sprd_dma_chn *schan = to_sprd_dma_chan(c);
232
233 return container_of(schan, struct sprd_dma_dev, channels[c->chan_id]);
234}
235
236static inline struct sprd_dma_desc *to_sprd_dma_desc(struct virt_dma_desc *vd)
237{
238 return container_of(vd, struct sprd_dma_desc, vd);
239}
240
Eric Long770399d2018-11-06 13:01:36 +0800241static void sprd_dma_glb_update(struct sprd_dma_dev *sdev, u32 reg,
242 u32 mask, u32 val)
243{
244 u32 orig = readl(sdev->glb_base + reg);
245 u32 tmp;
246
247 tmp = (orig & ~mask) | val;
248 writel(tmp, sdev->glb_base + reg);
249}
250
Baolin Wang9b3b8172017-10-24 13:47:50 +0800251static void sprd_dma_chn_update(struct sprd_dma_chn *schan, u32 reg,
252 u32 mask, u32 val)
253{
254 u32 orig = readl(schan->chn_base + reg);
255 u32 tmp;
256
257 tmp = (orig & ~mask) | val;
258 writel(tmp, schan->chn_base + reg);
259}
260
261static int sprd_dma_enable(struct sprd_dma_dev *sdev)
262{
263 int ret;
264
265 ret = clk_prepare_enable(sdev->clk);
266 if (ret)
267 return ret;
268
269 /*
270 * The ashb_clk is optional and only for AGCP DMA controller, so we
271 * need add one condition to check if the ashb_clk need enable.
272 */
273 if (!IS_ERR(sdev->ashb_clk))
274 ret = clk_prepare_enable(sdev->ashb_clk);
275
276 return ret;
277}
278
279static void sprd_dma_disable(struct sprd_dma_dev *sdev)
280{
281 clk_disable_unprepare(sdev->clk);
282
283 /*
284 * Need to check if we need disable the optional ashb_clk for AGCP DMA.
285 */
286 if (!IS_ERR(sdev->ashb_clk))
287 clk_disable_unprepare(sdev->ashb_clk);
288}
289
290static void sprd_dma_set_uid(struct sprd_dma_chn *schan)
291{
292 struct sprd_dma_dev *sdev = to_sprd_dma_dev(&schan->vc.chan);
293 u32 dev_id = schan->dev_id;
294
295 if (dev_id != SPRD_DMA_SOFTWARE_UID) {
296 u32 uid_offset = SPRD_DMA_GLB_REQ_UID_OFFSET +
297 SPRD_DMA_GLB_REQ_UID(dev_id);
298
299 writel(schan->chn_num + 1, sdev->glb_base + uid_offset);
300 }
301}
302
303static void sprd_dma_unset_uid(struct sprd_dma_chn *schan)
304{
305 struct sprd_dma_dev *sdev = to_sprd_dma_dev(&schan->vc.chan);
306 u32 dev_id = schan->dev_id;
307
308 if (dev_id != SPRD_DMA_SOFTWARE_UID) {
309 u32 uid_offset = SPRD_DMA_GLB_REQ_UID_OFFSET +
310 SPRD_DMA_GLB_REQ_UID(dev_id);
311
312 writel(0, sdev->glb_base + uid_offset);
313 }
314}
315
316static void sprd_dma_clear_int(struct sprd_dma_chn *schan)
317{
318 sprd_dma_chn_update(schan, SPRD_DMA_CHN_INTC,
319 SPRD_DMA_INT_MASK << SPRD_DMA_INT_CLR_OFFSET,
320 SPRD_DMA_INT_MASK << SPRD_DMA_INT_CLR_OFFSET);
321}
322
323static void sprd_dma_enable_chn(struct sprd_dma_chn *schan)
324{
325 sprd_dma_chn_update(schan, SPRD_DMA_CHN_CFG, SPRD_DMA_CHN_EN,
326 SPRD_DMA_CHN_EN);
327}
328
329static void sprd_dma_disable_chn(struct sprd_dma_chn *schan)
330{
331 sprd_dma_chn_update(schan, SPRD_DMA_CHN_CFG, SPRD_DMA_CHN_EN, 0);
332}
333
334static void sprd_dma_soft_request(struct sprd_dma_chn *schan)
335{
336 sprd_dma_chn_update(schan, SPRD_DMA_CHN_REQ, SPRD_DMA_REQ_EN,
337 SPRD_DMA_REQ_EN);
338}
339
340static void sprd_dma_pause_resume(struct sprd_dma_chn *schan, bool enable)
341{
342 struct sprd_dma_dev *sdev = to_sprd_dma_dev(&schan->vc.chan);
343 u32 pause, timeout = SPRD_DMA_PAUSE_CNT;
344
345 if (enable) {
346 sprd_dma_chn_update(schan, SPRD_DMA_CHN_PAUSE,
347 SPRD_DMA_PAUSE_EN, SPRD_DMA_PAUSE_EN);
348
349 do {
350 pause = readl(schan->chn_base + SPRD_DMA_CHN_PAUSE);
351 if (pause & SPRD_DMA_PAUSE_STS)
352 break;
353
354 cpu_relax();
355 } while (--timeout > 0);
356
357 if (!timeout)
358 dev_warn(sdev->dma_dev.dev,
359 "pause dma controller timeout\n");
360 } else {
361 sprd_dma_chn_update(schan, SPRD_DMA_CHN_PAUSE,
362 SPRD_DMA_PAUSE_EN, 0);
363 }
364}
365
366static void sprd_dma_stop_and_disable(struct sprd_dma_chn *schan)
367{
368 u32 cfg = readl(schan->chn_base + SPRD_DMA_CHN_CFG);
369
370 if (!(cfg & SPRD_DMA_CHN_EN))
371 return;
372
373 sprd_dma_pause_resume(schan, true);
374 sprd_dma_disable_chn(schan);
375}
376
Eric Longd762ab32018-11-06 13:01:32 +0800377static unsigned long sprd_dma_get_src_addr(struct sprd_dma_chn *schan)
378{
379 unsigned long addr, addr_high;
380
381 addr = readl(schan->chn_base + SPRD_DMA_CHN_SRC_ADDR);
382 addr_high = readl(schan->chn_base + SPRD_DMA_CHN_WARP_PTR) &
383 SPRD_DMA_HIGH_ADDR_MASK;
384
385 return addr | (addr_high << SPRD_DMA_HIGH_ADDR_OFFSET);
386}
387
Baolin Wang9b3b8172017-10-24 13:47:50 +0800388static unsigned long sprd_dma_get_dst_addr(struct sprd_dma_chn *schan)
389{
390 unsigned long addr, addr_high;
391
392 addr = readl(schan->chn_base + SPRD_DMA_CHN_DES_ADDR);
393 addr_high = readl(schan->chn_base + SPRD_DMA_CHN_WARP_TO) &
394 SPRD_DMA_HIGH_ADDR_MASK;
395
396 return addr | (addr_high << SPRD_DMA_HIGH_ADDR_OFFSET);
397}
398
399static enum sprd_dma_int_type sprd_dma_get_int_type(struct sprd_dma_chn *schan)
400{
401 struct sprd_dma_dev *sdev = to_sprd_dma_dev(&schan->vc.chan);
402 u32 intc_sts = readl(schan->chn_base + SPRD_DMA_CHN_INTC) &
403 SPRD_DMA_CHN_INT_STS;
404
405 switch (intc_sts) {
406 case SPRD_DMA_CFGERR_INT_STS:
407 return SPRD_DMA_CFGERR_INT;
408
409 case SPRD_DMA_LIST_INT_STS:
410 return SPRD_DMA_LIST_INT;
411
412 case SPRD_DMA_TRSC_INT_STS:
413 return SPRD_DMA_TRANS_INT;
414
415 case SPRD_DMA_BLK_INT_STS:
416 return SPRD_DMA_BLK_INT;
417
418 case SPRD_DMA_FRAG_INT_STS:
419 return SPRD_DMA_FRAG_INT;
420
421 default:
422 dev_warn(sdev->dma_dev.dev, "incorrect dma interrupt type\n");
423 return SPRD_DMA_NO_INT;
424 }
425}
426
427static enum sprd_dma_req_mode sprd_dma_get_req_type(struct sprd_dma_chn *schan)
428{
429 u32 frag_reg = readl(schan->chn_base + SPRD_DMA_CHN_FRG_LEN);
430
431 return (frag_reg >> SPRD_DMA_REQ_MODE_OFFSET) & SPRD_DMA_REQ_MODE_MASK;
432}
433
Eric Long770399d2018-11-06 13:01:36 +0800434static int sprd_dma_set_2stage_config(struct sprd_dma_chn *schan)
435{
436 struct sprd_dma_dev *sdev = to_sprd_dma_dev(&schan->vc.chan);
437 u32 val, chn = schan->chn_num + 1;
438
439 switch (schan->chn_mode) {
440 case SPRD_DMA_SRC_CHN0:
441 val = chn & SPRD_DMA_GLB_SRC_CHN_MASK;
442 val |= BIT(schan->trg_mode - 1) << SPRD_DMA_GLB_TRG_OFFSET;
443 val |= SPRD_DMA_GLB_2STAGE_EN;
Baolin Wang9bb9fe02019-05-06 15:28:33 +0800444 if (schan->int_type != SPRD_DMA_NO_INT)
445 val |= SPRD_DMA_GLB_SRC_INT;
446
Eric Long770399d2018-11-06 13:01:36 +0800447 sprd_dma_glb_update(sdev, SPRD_DMA_GLB_2STAGE_GRP1, val, val);
448 break;
449
450 case SPRD_DMA_SRC_CHN1:
451 val = chn & SPRD_DMA_GLB_SRC_CHN_MASK;
452 val |= BIT(schan->trg_mode - 1) << SPRD_DMA_GLB_TRG_OFFSET;
453 val |= SPRD_DMA_GLB_2STAGE_EN;
Baolin Wang9bb9fe02019-05-06 15:28:33 +0800454 if (schan->int_type != SPRD_DMA_NO_INT)
455 val |= SPRD_DMA_GLB_SRC_INT;
456
Eric Long770399d2018-11-06 13:01:36 +0800457 sprd_dma_glb_update(sdev, SPRD_DMA_GLB_2STAGE_GRP2, val, val);
458 break;
459
460 case SPRD_DMA_DST_CHN0:
461 val = (chn << SPRD_DMA_GLB_DEST_CHN_OFFSET) &
462 SPRD_DMA_GLB_DEST_CHN_MASK;
463 val |= SPRD_DMA_GLB_2STAGE_EN;
Baolin Wang9bb9fe02019-05-06 15:28:33 +0800464 if (schan->int_type != SPRD_DMA_NO_INT)
465 val |= SPRD_DMA_GLB_DEST_INT;
466
Eric Long770399d2018-11-06 13:01:36 +0800467 sprd_dma_glb_update(sdev, SPRD_DMA_GLB_2STAGE_GRP1, val, val);
468 break;
469
470 case SPRD_DMA_DST_CHN1:
471 val = (chn << SPRD_DMA_GLB_DEST_CHN_OFFSET) &
472 SPRD_DMA_GLB_DEST_CHN_MASK;
473 val |= SPRD_DMA_GLB_2STAGE_EN;
Baolin Wang9bb9fe02019-05-06 15:28:33 +0800474 if (schan->int_type != SPRD_DMA_NO_INT)
475 val |= SPRD_DMA_GLB_DEST_INT;
476
Eric Long770399d2018-11-06 13:01:36 +0800477 sprd_dma_glb_update(sdev, SPRD_DMA_GLB_2STAGE_GRP2, val, val);
478 break;
479
480 default:
481 dev_err(sdev->dma_dev.dev, "invalid channel mode setting %d\n",
482 schan->chn_mode);
483 return -EINVAL;
484 }
485
486 return 0;
487}
488
Zhenfang Wangd0f19a42020-03-12 21:26:04 +0800489static void sprd_dma_set_pending(struct sprd_dma_chn *schan, bool enable)
490{
491 struct sprd_dma_dev *sdev = to_sprd_dma_dev(&schan->vc.chan);
492 u32 reg, val, req_id;
493
494 if (schan->dev_id == SPRD_DMA_SOFTWARE_UID)
495 return;
496
497 /* The DMA request id always starts from 0. */
498 req_id = schan->dev_id - 1;
499
500 if (req_id < 32) {
501 reg = SPRD_DMA_GLB_REQ_PEND0_EN;
502 val = BIT(req_id);
503 } else {
504 reg = SPRD_DMA_GLB_REQ_PEND1_EN;
505 val = BIT(req_id - 32);
506 }
507
508 sprd_dma_glb_update(sdev, reg, val, enable ? val : 0);
509}
510
Baolin Wang9b3b8172017-10-24 13:47:50 +0800511static void sprd_dma_set_chn_config(struct sprd_dma_chn *schan,
512 struct sprd_dma_desc *sdesc)
513{
514 struct sprd_dma_chn_hw *cfg = &sdesc->chn_hw;
515
516 writel(cfg->pause, schan->chn_base + SPRD_DMA_CHN_PAUSE);
517 writel(cfg->cfg, schan->chn_base + SPRD_DMA_CHN_CFG);
518 writel(cfg->intc, schan->chn_base + SPRD_DMA_CHN_INTC);
519 writel(cfg->src_addr, schan->chn_base + SPRD_DMA_CHN_SRC_ADDR);
520 writel(cfg->des_addr, schan->chn_base + SPRD_DMA_CHN_DES_ADDR);
521 writel(cfg->frg_len, schan->chn_base + SPRD_DMA_CHN_FRG_LEN);
522 writel(cfg->blk_len, schan->chn_base + SPRD_DMA_CHN_BLK_LEN);
523 writel(cfg->trsc_len, schan->chn_base + SPRD_DMA_CHN_TRSC_LEN);
524 writel(cfg->trsf_step, schan->chn_base + SPRD_DMA_CHN_TRSF_STEP);
525 writel(cfg->wrap_ptr, schan->chn_base + SPRD_DMA_CHN_WARP_PTR);
526 writel(cfg->wrap_to, schan->chn_base + SPRD_DMA_CHN_WARP_TO);
527 writel(cfg->llist_ptr, schan->chn_base + SPRD_DMA_CHN_LLIST_PTR);
528 writel(cfg->frg_step, schan->chn_base + SPRD_DMA_CHN_FRAG_STEP);
529 writel(cfg->src_blk_step, schan->chn_base + SPRD_DMA_CHN_SRC_BLK_STEP);
530 writel(cfg->des_blk_step, schan->chn_base + SPRD_DMA_CHN_DES_BLK_STEP);
531 writel(cfg->req, schan->chn_base + SPRD_DMA_CHN_REQ);
532}
533
534static void sprd_dma_start(struct sprd_dma_chn *schan)
535{
536 struct virt_dma_desc *vd = vchan_next_desc(&schan->vc);
537
538 if (!vd)
539 return;
540
541 list_del(&vd->node);
542 schan->cur_desc = to_sprd_dma_desc(vd);
543
544 /*
Eric Long770399d2018-11-06 13:01:36 +0800545 * Set 2-stage configuration if the channel starts one 2-stage
546 * transfer.
547 */
548 if (schan->chn_mode && sprd_dma_set_2stage_config(schan))
549 return;
550
551 /*
Baolin Wang9b3b8172017-10-24 13:47:50 +0800552 * Copy the DMA configuration from DMA descriptor to this hardware
553 * channel.
554 */
555 sprd_dma_set_chn_config(schan, schan->cur_desc);
556 sprd_dma_set_uid(schan);
Zhenfang Wangd0f19a42020-03-12 21:26:04 +0800557 sprd_dma_set_pending(schan, true);
Baolin Wang9b3b8172017-10-24 13:47:50 +0800558 sprd_dma_enable_chn(schan);
559
Eric Long3d626a92019-05-06 15:28:30 +0800560 if (schan->dev_id == SPRD_DMA_SOFTWARE_UID &&
561 schan->chn_mode != SPRD_DMA_DST_CHN0 &&
562 schan->chn_mode != SPRD_DMA_DST_CHN1)
Baolin Wang9b3b8172017-10-24 13:47:50 +0800563 sprd_dma_soft_request(schan);
564}
565
566static void sprd_dma_stop(struct sprd_dma_chn *schan)
567{
568 sprd_dma_stop_and_disable(schan);
Zhenfang Wangd0f19a42020-03-12 21:26:04 +0800569 sprd_dma_set_pending(schan, false);
Baolin Wang9b3b8172017-10-24 13:47:50 +0800570 sprd_dma_unset_uid(schan);
571 sprd_dma_clear_int(schan);
Eric Long0e5d7b12018-11-06 13:01:34 +0800572 schan->cur_desc = NULL;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800573}
574
575static bool sprd_dma_check_trans_done(struct sprd_dma_desc *sdesc,
576 enum sprd_dma_int_type int_type,
577 enum sprd_dma_req_mode req_mode)
578{
579 if (int_type == SPRD_DMA_NO_INT)
580 return false;
581
582 if (int_type >= req_mode + 1)
583 return true;
584 else
585 return false;
586}
587
588static irqreturn_t dma_irq_handle(int irq, void *dev_id)
589{
590 struct sprd_dma_dev *sdev = (struct sprd_dma_dev *)dev_id;
591 u32 irq_status = readl(sdev->glb_base + SPRD_DMA_GLB_INT_MSK_STS);
592 struct sprd_dma_chn *schan;
593 struct sprd_dma_desc *sdesc;
594 enum sprd_dma_req_mode req_type;
595 enum sprd_dma_int_type int_type;
Eric Long97dbd6e2018-11-06 13:01:35 +0800596 bool trans_done = false, cyclic = false;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800597 u32 i;
598
599 while (irq_status) {
600 i = __ffs(irq_status);
601 irq_status &= (irq_status - 1);
602 schan = &sdev->channels[i];
603
604 spin_lock(&schan->vc.lock);
Baolin Wang58152b02019-05-06 15:28:29 +0800605
606 sdesc = schan->cur_desc;
607 if (!sdesc) {
608 spin_unlock(&schan->vc.lock);
609 return IRQ_HANDLED;
610 }
611
Baolin Wang9b3b8172017-10-24 13:47:50 +0800612 int_type = sprd_dma_get_int_type(schan);
613 req_type = sprd_dma_get_req_type(schan);
614 sprd_dma_clear_int(schan);
615
Eric Long97dbd6e2018-11-06 13:01:35 +0800616 /* cyclic mode schedule callback */
617 cyclic = schan->linklist.phy_addr ? true : false;
618 if (cyclic == true) {
619 vchan_cyclic_callback(&sdesc->vd);
620 } else {
621 /* Check if the dma request descriptor is done. */
622 trans_done = sprd_dma_check_trans_done(sdesc, int_type,
623 req_type);
624 if (trans_done == true) {
625 vchan_cookie_complete(&sdesc->vd);
626 schan->cur_desc = NULL;
627 sprd_dma_start(schan);
628 }
Baolin Wang9b3b8172017-10-24 13:47:50 +0800629 }
630 spin_unlock(&schan->vc.lock);
631 }
632
633 return IRQ_HANDLED;
634}
635
636static int sprd_dma_alloc_chan_resources(struct dma_chan *chan)
637{
Baolin Wangffb5be72019-02-21 13:34:41 +0800638 return pm_runtime_get_sync(chan->device->dev);
Baolin Wang9b3b8172017-10-24 13:47:50 +0800639}
640
641static void sprd_dma_free_chan_resources(struct dma_chan *chan)
642{
643 struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
Baolin Wangec1ac302019-10-09 17:11:30 +0800644 struct virt_dma_desc *cur_vd = NULL;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800645 unsigned long flags;
646
647 spin_lock_irqsave(&schan->vc.lock, flags);
Baolin Wangec1ac302019-10-09 17:11:30 +0800648 if (schan->cur_desc)
649 cur_vd = &schan->cur_desc->vd;
650
Baolin Wang9b3b8172017-10-24 13:47:50 +0800651 sprd_dma_stop(schan);
652 spin_unlock_irqrestore(&schan->vc.lock, flags);
653
Baolin Wangec1ac302019-10-09 17:11:30 +0800654 if (cur_vd)
655 sprd_dma_free_desc(cur_vd);
656
Baolin Wang9b3b8172017-10-24 13:47:50 +0800657 vchan_free_chan_resources(&schan->vc);
658 pm_runtime_put(chan->device->dev);
659}
660
661static enum dma_status sprd_dma_tx_status(struct dma_chan *chan,
662 dma_cookie_t cookie,
663 struct dma_tx_state *txstate)
664{
665 struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
666 struct virt_dma_desc *vd;
667 unsigned long flags;
668 enum dma_status ret;
669 u32 pos;
670
671 ret = dma_cookie_status(chan, cookie, txstate);
672 if (ret == DMA_COMPLETE || !txstate)
673 return ret;
674
675 spin_lock_irqsave(&schan->vc.lock, flags);
676 vd = vchan_find_desc(&schan->vc, cookie);
677 if (vd) {
678 struct sprd_dma_desc *sdesc = to_sprd_dma_desc(vd);
679 struct sprd_dma_chn_hw *hw = &sdesc->chn_hw;
680
681 if (hw->trsc_len > 0)
682 pos = hw->trsc_len;
683 else if (hw->blk_len > 0)
684 pos = hw->blk_len;
685 else if (hw->frg_len > 0)
686 pos = hw->frg_len;
687 else
688 pos = 0;
689 } else if (schan->cur_desc && schan->cur_desc->vd.tx.cookie == cookie) {
Baolin Wang16d0f852019-05-06 15:28:28 +0800690 struct sprd_dma_desc *sdesc = schan->cur_desc;
Eric Longd762ab32018-11-06 13:01:32 +0800691
692 if (sdesc->dir == DMA_DEV_TO_MEM)
693 pos = sprd_dma_get_dst_addr(schan);
694 else
695 pos = sprd_dma_get_src_addr(schan);
Baolin Wang9b3b8172017-10-24 13:47:50 +0800696 } else {
697 pos = 0;
698 }
699 spin_unlock_irqrestore(&schan->vc.lock, flags);
700
701 dma_set_residue(txstate, pos);
702 return ret;
703}
704
705static void sprd_dma_issue_pending(struct dma_chan *chan)
706{
707 struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
708 unsigned long flags;
709
710 spin_lock_irqsave(&schan->vc.lock, flags);
711 if (vchan_issue_pending(&schan->vc) && !schan->cur_desc)
712 sprd_dma_start(schan);
713 spin_unlock_irqrestore(&schan->vc.lock, flags);
714}
715
Eric Longca1b7d32018-05-23 17:31:11 +0800716static int sprd_dma_get_datawidth(enum dma_slave_buswidth buswidth)
717{
718 switch (buswidth) {
719 case DMA_SLAVE_BUSWIDTH_1_BYTE:
720 case DMA_SLAVE_BUSWIDTH_2_BYTES:
721 case DMA_SLAVE_BUSWIDTH_4_BYTES:
722 case DMA_SLAVE_BUSWIDTH_8_BYTES:
723 return ffs(buswidth) - 1;
724
725 default:
726 return -EINVAL;
727 }
728}
729
730static int sprd_dma_get_step(enum dma_slave_buswidth buswidth)
731{
732 switch (buswidth) {
733 case DMA_SLAVE_BUSWIDTH_1_BYTE:
734 case DMA_SLAVE_BUSWIDTH_2_BYTES:
735 case DMA_SLAVE_BUSWIDTH_4_BYTES:
736 case DMA_SLAVE_BUSWIDTH_8_BYTES:
737 return buswidth;
738
739 default:
740 return -EINVAL;
741 }
742}
743
744static int sprd_dma_fill_desc(struct dma_chan *chan,
Eric Long4ac69542018-08-28 19:09:07 +0800745 struct sprd_dma_chn_hw *hw,
746 unsigned int sglen, int sg_index,
Eric Longca1b7d32018-05-23 17:31:11 +0800747 dma_addr_t src, dma_addr_t dst, u32 len,
748 enum dma_transfer_direction dir,
749 unsigned long flags,
750 struct dma_slave_config *slave_cfg)
Baolin Wang9b3b8172017-10-24 13:47:50 +0800751{
752 struct sprd_dma_dev *sdev = to_sprd_dma_dev(chan);
Eric Longca1b7d32018-05-23 17:31:11 +0800753 struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
Eric Long770399d2018-11-06 13:01:36 +0800754 enum sprd_dma_chn_mode chn_mode = schan->chn_mode;
Eric Longca1b7d32018-05-23 17:31:11 +0800755 u32 req_mode = (flags >> SPRD_DMA_REQ_SHIFT) & SPRD_DMA_REQ_MODE_MASK;
756 u32 int_mode = flags & SPRD_DMA_INT_MASK;
757 int src_datawidth, dst_datawidth, src_step, dst_step;
758 u32 temp, fix_mode = 0, fix_en = 0;
Zhenfang Wang8b6bc5f2019-09-12 13:47:18 +0800759 phys_addr_t llist_ptr;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800760
Eric Longca1b7d32018-05-23 17:31:11 +0800761 if (dir == DMA_MEM_TO_DEV) {
762 src_step = sprd_dma_get_step(slave_cfg->src_addr_width);
763 if (src_step < 0) {
764 dev_err(sdev->dma_dev.dev, "invalid source step\n");
765 return src_step;
766 }
Eric Long770399d2018-11-06 13:01:36 +0800767
768 /*
769 * For 2-stage transfer, destination channel step can not be 0,
770 * since destination device is AON IRAM.
771 */
772 if (chn_mode == SPRD_DMA_DST_CHN0 ||
773 chn_mode == SPRD_DMA_DST_CHN1)
774 dst_step = src_step;
775 else
776 dst_step = SPRD_DMA_NONE_STEP;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800777 } else {
Eric Longca1b7d32018-05-23 17:31:11 +0800778 dst_step = sprd_dma_get_step(slave_cfg->dst_addr_width);
779 if (dst_step < 0) {
780 dev_err(sdev->dma_dev.dev, "invalid destination step\n");
781 return dst_step;
782 }
783 src_step = SPRD_DMA_NONE_STEP;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800784 }
785
Eric Longca1b7d32018-05-23 17:31:11 +0800786 src_datawidth = sprd_dma_get_datawidth(slave_cfg->src_addr_width);
787 if (src_datawidth < 0) {
788 dev_err(sdev->dma_dev.dev, "invalid source datawidth\n");
789 return src_datawidth;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800790 }
791
Eric Longca1b7d32018-05-23 17:31:11 +0800792 dst_datawidth = sprd_dma_get_datawidth(slave_cfg->dst_addr_width);
793 if (dst_datawidth < 0) {
794 dev_err(sdev->dma_dev.dev, "invalid destination datawidth\n");
795 return dst_datawidth;
796 }
797
Baolin Wang9b3b8172017-10-24 13:47:50 +0800798 hw->cfg = SPRD_DMA_DONOT_WAIT_BDONE << SPRD_DMA_WAIT_BDONE_OFFSET;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800799
Eric Longca1b7d32018-05-23 17:31:11 +0800800 /*
801 * wrap_ptr and wrap_to will save the high 4 bits source address and
802 * destination address.
803 */
804 hw->wrap_ptr = (src >> SPRD_DMA_HIGH_ADDR_OFFSET) & SPRD_DMA_HIGH_ADDR_MASK;
805 hw->wrap_to = (dst >> SPRD_DMA_HIGH_ADDR_OFFSET) & SPRD_DMA_HIGH_ADDR_MASK;
806 hw->src_addr = src & SPRD_DMA_LOW_ADDR_MASK;
807 hw->des_addr = dst & SPRD_DMA_LOW_ADDR_MASK;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800808
Eric Longca1b7d32018-05-23 17:31:11 +0800809 /*
810 * If the src step and dst step both are 0 or both are not 0, that means
811 * we can not enable the fix mode. If one is 0 and another one is not,
812 * we can enable the fix mode.
813 */
814 if ((src_step != 0 && dst_step != 0) || (src_step | dst_step) == 0) {
Baolin Wang9b3b8172017-10-24 13:47:50 +0800815 fix_en = 0;
816 } else {
817 fix_en = 1;
818 if (src_step)
819 fix_mode = 1;
820 else
821 fix_mode = 0;
822 }
823
Eric Longca1b7d32018-05-23 17:31:11 +0800824 hw->intc = int_mode | SPRD_DMA_CFG_ERR_INT_EN;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800825
Eric Longca1b7d32018-05-23 17:31:11 +0800826 temp = src_datawidth << SPRD_DMA_SRC_DATAWIDTH_OFFSET;
827 temp |= dst_datawidth << SPRD_DMA_DES_DATAWIDTH_OFFSET;
828 temp |= req_mode << SPRD_DMA_REQ_MODE_OFFSET;
829 temp |= fix_mode << SPRD_DMA_FIX_SEL_OFFSET;
830 temp |= fix_en << SPRD_DMA_FIX_EN_OFFSET;
Eric Longa7e335d2019-10-23 14:31:32 +0800831 temp |= schan->linklist.wrap_addr ?
832 SPRD_DMA_WRAP_EN | SPRD_DMA_WRAP_SEL_DEST : 0;
Eric Longca1b7d32018-05-23 17:31:11 +0800833 temp |= slave_cfg->src_maxburst & SPRD_DMA_FRG_LEN_MASK;
834 hw->frg_len = temp;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800835
Eric Long89d03b32019-05-06 15:28:31 +0800836 hw->blk_len = slave_cfg->src_maxburst & SPRD_DMA_BLK_LEN_MASK;
Eric Longca1b7d32018-05-23 17:31:11 +0800837 hw->trsc_len = len & SPRD_DMA_TRSC_LEN_MASK;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800838
Eric Longca1b7d32018-05-23 17:31:11 +0800839 temp = (dst_step & SPRD_DMA_TRSF_STEP_MASK) << SPRD_DMA_DEST_TRSF_STEP_OFFSET;
840 temp |= (src_step & SPRD_DMA_TRSF_STEP_MASK) << SPRD_DMA_SRC_TRSF_STEP_OFFSET;
841 hw->trsf_step = temp;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800842
Eric Long4ac69542018-08-28 19:09:07 +0800843 /* link-list configuration */
844 if (schan->linklist.phy_addr) {
Eric Long4ac69542018-08-28 19:09:07 +0800845 hw->cfg |= SPRD_DMA_LINKLIST_EN;
846
847 /* link-list index */
Eric Long13e89972018-11-06 13:01:33 +0800848 temp = sglen ? (sg_index + 1) % sglen : 0;
849
Eric Long4ac69542018-08-28 19:09:07 +0800850 /* Next link-list configuration's physical address offset */
851 temp = temp * sizeof(*hw) + SPRD_DMA_CHN_SRC_ADDR;
852 /*
853 * Set the link-list pointer point to next link-list
854 * configuration's physical address.
855 */
Zhenfang Wang8b6bc5f2019-09-12 13:47:18 +0800856 llist_ptr = schan->linklist.phy_addr + temp;
857 hw->llist_ptr = lower_32_bits(llist_ptr);
858 hw->src_blk_step = (upper_32_bits(llist_ptr) << SPRD_DMA_LLIST_HIGH_SHIFT) &
859 SPRD_DMA_LLIST_HIGH_MASK;
Eric Longa7e335d2019-10-23 14:31:32 +0800860
861 if (schan->linklist.wrap_addr) {
862 hw->wrap_ptr |= schan->linklist.wrap_addr &
863 SPRD_DMA_WRAP_ADDR_MASK;
864 hw->wrap_to |= dst & SPRD_DMA_WRAP_ADDR_MASK;
865 }
Eric Long4ac69542018-08-28 19:09:07 +0800866 } else {
867 hw->llist_ptr = 0;
Zhenfang Wang8b6bc5f2019-09-12 13:47:18 +0800868 hw->src_blk_step = 0;
Eric Long4ac69542018-08-28 19:09:07 +0800869 }
870
Baolin Wang9b3b8172017-10-24 13:47:50 +0800871 hw->frg_step = 0;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800872 hw->des_blk_step = 0;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800873 return 0;
874}
875
Eric Long4ac69542018-08-28 19:09:07 +0800876static int sprd_dma_fill_linklist_desc(struct dma_chan *chan,
877 unsigned int sglen, int sg_index,
878 dma_addr_t src, dma_addr_t dst, u32 len,
879 enum dma_transfer_direction dir,
880 unsigned long flags,
881 struct dma_slave_config *slave_cfg)
882{
883 struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
884 struct sprd_dma_chn_hw *hw;
885
886 if (!schan->linklist.virt_addr)
887 return -EINVAL;
888
889 hw = (struct sprd_dma_chn_hw *)(schan->linklist.virt_addr +
890 sg_index * sizeof(*hw));
891
892 return sprd_dma_fill_desc(chan, hw, sglen, sg_index, src, dst, len,
893 dir, flags, slave_cfg);
894}
895
Vinod Koul1ab8da12018-01-12 22:31:17 +0530896static struct dma_async_tx_descriptor *
Baolin Wang9b3b8172017-10-24 13:47:50 +0800897sprd_dma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src,
898 size_t len, unsigned long flags)
899{
900 struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
901 struct sprd_dma_desc *sdesc;
Eric Long32fa2012018-05-23 17:31:10 +0800902 struct sprd_dma_chn_hw *hw;
903 enum sprd_dma_datawidth datawidth;
904 u32 step, temp;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800905
906 sdesc = kzalloc(sizeof(*sdesc), GFP_NOWAIT);
907 if (!sdesc)
908 return NULL;
909
Eric Long32fa2012018-05-23 17:31:10 +0800910 hw = &sdesc->chn_hw;
911
912 hw->cfg = SPRD_DMA_DONOT_WAIT_BDONE << SPRD_DMA_WAIT_BDONE_OFFSET;
913 hw->intc = SPRD_DMA_TRANS_INT | SPRD_DMA_CFG_ERR_INT_EN;
914 hw->src_addr = src & SPRD_DMA_LOW_ADDR_MASK;
915 hw->des_addr = dest & SPRD_DMA_LOW_ADDR_MASK;
916 hw->wrap_ptr = (src >> SPRD_DMA_HIGH_ADDR_OFFSET) &
917 SPRD_DMA_HIGH_ADDR_MASK;
918 hw->wrap_to = (dest >> SPRD_DMA_HIGH_ADDR_OFFSET) &
919 SPRD_DMA_HIGH_ADDR_MASK;
920
921 if (IS_ALIGNED(len, 8)) {
922 datawidth = SPRD_DMA_DATAWIDTH_8_BYTES;
923 step = SPRD_DMA_DWORD_STEP;
924 } else if (IS_ALIGNED(len, 4)) {
925 datawidth = SPRD_DMA_DATAWIDTH_4_BYTES;
926 step = SPRD_DMA_WORD_STEP;
927 } else if (IS_ALIGNED(len, 2)) {
928 datawidth = SPRD_DMA_DATAWIDTH_2_BYTES;
929 step = SPRD_DMA_SHORT_STEP;
930 } else {
931 datawidth = SPRD_DMA_DATAWIDTH_1_BYTE;
932 step = SPRD_DMA_BYTE_STEP;
Baolin Wang9b3b8172017-10-24 13:47:50 +0800933 }
934
Eric Long32fa2012018-05-23 17:31:10 +0800935 temp = datawidth << SPRD_DMA_SRC_DATAWIDTH_OFFSET;
936 temp |= datawidth << SPRD_DMA_DES_DATAWIDTH_OFFSET;
937 temp |= SPRD_DMA_TRANS_REQ << SPRD_DMA_REQ_MODE_OFFSET;
938 temp |= len & SPRD_DMA_FRG_LEN_MASK;
939 hw->frg_len = temp;
940
941 hw->blk_len = len & SPRD_DMA_BLK_LEN_MASK;
942 hw->trsc_len = len & SPRD_DMA_TRSC_LEN_MASK;
943
944 temp = (step & SPRD_DMA_TRSF_STEP_MASK) << SPRD_DMA_DEST_TRSF_STEP_OFFSET;
945 temp |= (step & SPRD_DMA_TRSF_STEP_MASK) << SPRD_DMA_SRC_TRSF_STEP_OFFSET;
946 hw->trsf_step = temp;
947
Baolin Wang9b3b8172017-10-24 13:47:50 +0800948 return vchan_tx_prep(&schan->vc, &sdesc->vd, flags);
949}
950
Eric Longca1b7d32018-05-23 17:31:11 +0800951static struct dma_async_tx_descriptor *
952sprd_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
953 unsigned int sglen, enum dma_transfer_direction dir,
954 unsigned long flags, void *context)
955{
956 struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
957 struct dma_slave_config *slave_cfg = &schan->slave_cfg;
958 dma_addr_t src = 0, dst = 0;
Baolin Wang689379c2019-08-30 15:37:45 +0800959 dma_addr_t start_src = 0, start_dst = 0;
Eric Longca1b7d32018-05-23 17:31:11 +0800960 struct sprd_dma_desc *sdesc;
961 struct scatterlist *sg;
962 u32 len = 0;
963 int ret, i;
964
Eric Long4ac69542018-08-28 19:09:07 +0800965 if (!is_slave_direction(dir))
Eric Longca1b7d32018-05-23 17:31:11 +0800966 return NULL;
967
Eric Long4ac69542018-08-28 19:09:07 +0800968 if (context) {
969 struct sprd_dma_linklist *ll_cfg =
970 (struct sprd_dma_linklist *)context;
971
972 schan->linklist.phy_addr = ll_cfg->phy_addr;
973 schan->linklist.virt_addr = ll_cfg->virt_addr;
Eric Longa7e335d2019-10-23 14:31:32 +0800974 schan->linklist.wrap_addr = ll_cfg->wrap_addr;
Eric Long4ac69542018-08-28 19:09:07 +0800975 } else {
976 schan->linklist.phy_addr = 0;
977 schan->linklist.virt_addr = 0;
Eric Longa7e335d2019-10-23 14:31:32 +0800978 schan->linklist.wrap_addr = 0;
Eric Long4ac69542018-08-28 19:09:07 +0800979 }
980
Baolin Wang9bb9fe02019-05-06 15:28:33 +0800981 /*
982 * Set channel mode, interrupt mode and trigger mode for 2-stage
983 * transfer.
984 */
Eric Longc434e372019-05-06 15:28:32 +0800985 schan->chn_mode =
986 (flags >> SPRD_DMA_CHN_MODE_SHIFT) & SPRD_DMA_CHN_MODE_MASK;
987 schan->trg_mode =
988 (flags >> SPRD_DMA_TRG_MODE_SHIFT) & SPRD_DMA_TRG_MODE_MASK;
Baolin Wang9bb9fe02019-05-06 15:28:33 +0800989 schan->int_type = flags & SPRD_DMA_INT_TYPE_MASK;
Eric Longc434e372019-05-06 15:28:32 +0800990
Eric Longca1b7d32018-05-23 17:31:11 +0800991 sdesc = kzalloc(sizeof(*sdesc), GFP_NOWAIT);
992 if (!sdesc)
993 return NULL;
994
Eric Longd762ab32018-11-06 13:01:32 +0800995 sdesc->dir = dir;
996
Eric Longca1b7d32018-05-23 17:31:11 +0800997 for_each_sg(sgl, sg, sglen, i) {
998 len = sg_dma_len(sg);
999
1000 if (dir == DMA_MEM_TO_DEV) {
1001 src = sg_dma_address(sg);
1002 dst = slave_cfg->dst_addr;
1003 } else {
1004 src = slave_cfg->src_addr;
1005 dst = sg_dma_address(sg);
1006 }
Eric Long4ac69542018-08-28 19:09:07 +08001007
Baolin Wang689379c2019-08-30 15:37:45 +08001008 if (!i) {
1009 start_src = src;
1010 start_dst = dst;
1011 }
1012
Eric Long4ac69542018-08-28 19:09:07 +08001013 /*
1014 * The link-list mode needs at least 2 link-list
1015 * configurations. If there is only one sg, it doesn't
1016 * need to fill the link-list configuration.
1017 */
1018 if (sglen < 2)
1019 break;
1020
1021 ret = sprd_dma_fill_linklist_desc(chan, sglen, i, src, dst, len,
1022 dir, flags, slave_cfg);
1023 if (ret) {
1024 kfree(sdesc);
1025 return NULL;
1026 }
Eric Longca1b7d32018-05-23 17:31:11 +08001027 }
1028
Baolin Wang689379c2019-08-30 15:37:45 +08001029 ret = sprd_dma_fill_desc(chan, &sdesc->chn_hw, 0, 0, start_src,
1030 start_dst, len, dir, flags, slave_cfg);
Baolin Wang9b3b8172017-10-24 13:47:50 +08001031 if (ret) {
1032 kfree(sdesc);
1033 return NULL;
1034 }
1035
1036 return vchan_tx_prep(&schan->vc, &sdesc->vd, flags);
1037}
1038
Eric Longca1b7d32018-05-23 17:31:11 +08001039static int sprd_dma_slave_config(struct dma_chan *chan,
1040 struct dma_slave_config *config)
1041{
1042 struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
1043 struct dma_slave_config *slave_cfg = &schan->slave_cfg;
1044
Eric Longca1b7d32018-05-23 17:31:11 +08001045 memcpy(slave_cfg, config, sizeof(*config));
1046 return 0;
1047}
1048
Baolin Wang9b3b8172017-10-24 13:47:50 +08001049static int sprd_dma_pause(struct dma_chan *chan)
1050{
1051 struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
1052 unsigned long flags;
1053
1054 spin_lock_irqsave(&schan->vc.lock, flags);
1055 sprd_dma_pause_resume(schan, true);
1056 spin_unlock_irqrestore(&schan->vc.lock, flags);
1057
1058 return 0;
1059}
1060
1061static int sprd_dma_resume(struct dma_chan *chan)
1062{
1063 struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
1064 unsigned long flags;
1065
1066 spin_lock_irqsave(&schan->vc.lock, flags);
1067 sprd_dma_pause_resume(schan, false);
1068 spin_unlock_irqrestore(&schan->vc.lock, flags);
1069
1070 return 0;
1071}
1072
1073static int sprd_dma_terminate_all(struct dma_chan *chan)
1074{
1075 struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
Baolin Wangec1ac302019-10-09 17:11:30 +08001076 struct virt_dma_desc *cur_vd = NULL;
Baolin Wang9b3b8172017-10-24 13:47:50 +08001077 unsigned long flags;
1078 LIST_HEAD(head);
1079
1080 spin_lock_irqsave(&schan->vc.lock, flags);
Baolin Wangec1ac302019-10-09 17:11:30 +08001081 if (schan->cur_desc)
1082 cur_vd = &schan->cur_desc->vd;
1083
Baolin Wang9b3b8172017-10-24 13:47:50 +08001084 sprd_dma_stop(schan);
1085
1086 vchan_get_all_descriptors(&schan->vc, &head);
1087 spin_unlock_irqrestore(&schan->vc.lock, flags);
1088
Baolin Wangec1ac302019-10-09 17:11:30 +08001089 if (cur_vd)
1090 sprd_dma_free_desc(cur_vd);
1091
Baolin Wang9b3b8172017-10-24 13:47:50 +08001092 vchan_dma_desc_free_list(&schan->vc, &head);
1093 return 0;
1094}
1095
1096static void sprd_dma_free_desc(struct virt_dma_desc *vd)
1097{
1098 struct sprd_dma_desc *sdesc = to_sprd_dma_desc(vd);
1099
1100 kfree(sdesc);
1101}
1102
1103static bool sprd_dma_filter_fn(struct dma_chan *chan, void *param)
1104{
1105 struct sprd_dma_chn *schan = to_sprd_dma_chan(chan);
Baolin Wangffb5be72019-02-21 13:34:41 +08001106 u32 slave_id = *(u32 *)param;
Baolin Wang9b3b8172017-10-24 13:47:50 +08001107
Baolin Wangffb5be72019-02-21 13:34:41 +08001108 schan->dev_id = slave_id;
1109 return true;
Baolin Wang9b3b8172017-10-24 13:47:50 +08001110}
1111
1112static int sprd_dma_probe(struct platform_device *pdev)
1113{
1114 struct device_node *np = pdev->dev.of_node;
1115 struct sprd_dma_dev *sdev;
1116 struct sprd_dma_chn *dma_chn;
Baolin Wang9b3b8172017-10-24 13:47:50 +08001117 u32 chn_count;
1118 int ret, i;
1119
1120 ret = device_property_read_u32(&pdev->dev, "#dma-channels", &chn_count);
1121 if (ret) {
1122 dev_err(&pdev->dev, "get dma channels count failed\n");
1123 return ret;
1124 }
1125
Kees Cook0ed2dd02018-05-08 16:08:53 -07001126 sdev = devm_kzalloc(&pdev->dev,
1127 struct_size(sdev, channels, chn_count),
Baolin Wang9b3b8172017-10-24 13:47:50 +08001128 GFP_KERNEL);
1129 if (!sdev)
1130 return -ENOMEM;
1131
1132 sdev->clk = devm_clk_get(&pdev->dev, "enable");
1133 if (IS_ERR(sdev->clk)) {
1134 dev_err(&pdev->dev, "get enable clock failed\n");
1135 return PTR_ERR(sdev->clk);
1136 }
1137
1138 /* ashb clock is optional for AGCP DMA */
1139 sdev->ashb_clk = devm_clk_get(&pdev->dev, "ashb_eb");
1140 if (IS_ERR(sdev->ashb_clk))
1141 dev_warn(&pdev->dev, "no optional ashb eb clock\n");
1142
1143 /*
1144 * We have three DMA controllers: AP DMA, AON DMA and AGCP DMA. For AGCP
1145 * DMA controller, it can or do not request the irq, which will save
1146 * system power without resuming system by DMA interrupts if AGCP DMA
1147 * does not request the irq. Thus the DMA interrupts property should
1148 * be optional.
1149 */
1150 sdev->irq = platform_get_irq(pdev, 0);
1151 if (sdev->irq > 0) {
1152 ret = devm_request_irq(&pdev->dev, sdev->irq, dma_irq_handle,
1153 0, "sprd_dma", (void *)sdev);
1154 if (ret < 0) {
1155 dev_err(&pdev->dev, "request dma irq failed\n");
1156 return ret;
1157 }
1158 } else {
1159 dev_warn(&pdev->dev, "no interrupts for the dma controller\n");
1160 }
1161
Baolin Wangf228a4a2019-09-27 11:29:43 +08001162 sdev->glb_base = devm_platform_ioremap_resource(pdev, 0);
Dan Carpenterfd8d26a2018-05-16 11:48:07 +03001163 if (IS_ERR(sdev->glb_base))
1164 return PTR_ERR(sdev->glb_base);
Baolin Wang9b3b8172017-10-24 13:47:50 +08001165
1166 dma_cap_set(DMA_MEMCPY, sdev->dma_dev.cap_mask);
1167 sdev->total_chns = chn_count;
1168 sdev->dma_dev.chancnt = chn_count;
1169 INIT_LIST_HEAD(&sdev->dma_dev.channels);
1170 INIT_LIST_HEAD(&sdev->dma_dev.global_node);
1171 sdev->dma_dev.dev = &pdev->dev;
1172 sdev->dma_dev.device_alloc_chan_resources = sprd_dma_alloc_chan_resources;
1173 sdev->dma_dev.device_free_chan_resources = sprd_dma_free_chan_resources;
1174 sdev->dma_dev.device_tx_status = sprd_dma_tx_status;
1175 sdev->dma_dev.device_issue_pending = sprd_dma_issue_pending;
1176 sdev->dma_dev.device_prep_dma_memcpy = sprd_dma_prep_dma_memcpy;
Eric Longca1b7d32018-05-23 17:31:11 +08001177 sdev->dma_dev.device_prep_slave_sg = sprd_dma_prep_slave_sg;
1178 sdev->dma_dev.device_config = sprd_dma_slave_config;
Baolin Wang9b3b8172017-10-24 13:47:50 +08001179 sdev->dma_dev.device_pause = sprd_dma_pause;
1180 sdev->dma_dev.device_resume = sprd_dma_resume;
1181 sdev->dma_dev.device_terminate_all = sprd_dma_terminate_all;
1182
1183 for (i = 0; i < chn_count; i++) {
1184 dma_chn = &sdev->channels[i];
1185 dma_chn->chn_num = i;
1186 dma_chn->cur_desc = NULL;
1187 /* get each channel's registers base address. */
1188 dma_chn->chn_base = sdev->glb_base + SPRD_DMA_CHN_REG_OFFSET +
1189 SPRD_DMA_CHN_REG_LENGTH * i;
1190
1191 dma_chn->vc.desc_free = sprd_dma_free_desc;
1192 vchan_init(&dma_chn->vc, &sdev->dma_dev);
1193 }
1194
1195 platform_set_drvdata(pdev, sdev);
1196 ret = sprd_dma_enable(sdev);
1197 if (ret)
1198 return ret;
1199
1200 pm_runtime_set_active(&pdev->dev);
1201 pm_runtime_enable(&pdev->dev);
1202
1203 ret = pm_runtime_get_sync(&pdev->dev);
1204 if (ret < 0)
1205 goto err_rpm;
1206
1207 ret = dma_async_device_register(&sdev->dma_dev);
1208 if (ret < 0) {
1209 dev_err(&pdev->dev, "register dma device failed:%d\n", ret);
1210 goto err_register;
1211 }
1212
1213 sprd_dma_info.dma_cap = sdev->dma_dev.cap_mask;
1214 ret = of_dma_controller_register(np, of_dma_simple_xlate,
1215 &sprd_dma_info);
1216 if (ret)
1217 goto err_of_register;
1218
1219 pm_runtime_put(&pdev->dev);
1220 return 0;
1221
1222err_of_register:
1223 dma_async_device_unregister(&sdev->dma_dev);
1224err_register:
1225 pm_runtime_put_noidle(&pdev->dev);
1226 pm_runtime_disable(&pdev->dev);
1227err_rpm:
1228 sprd_dma_disable(sdev);
1229 return ret;
1230}
1231
1232static int sprd_dma_remove(struct platform_device *pdev)
1233{
1234 struct sprd_dma_dev *sdev = platform_get_drvdata(pdev);
1235 struct sprd_dma_chn *c, *cn;
1236 int ret;
1237
1238 ret = pm_runtime_get_sync(&pdev->dev);
1239 if (ret < 0)
1240 return ret;
1241
1242 /* explicitly free the irq */
1243 if (sdev->irq > 0)
1244 devm_free_irq(&pdev->dev, sdev->irq, sdev);
1245
1246 list_for_each_entry_safe(c, cn, &sdev->dma_dev.channels,
1247 vc.chan.device_node) {
1248 list_del(&c->vc.chan.device_node);
1249 tasklet_kill(&c->vc.task);
1250 }
1251
1252 of_dma_controller_free(pdev->dev.of_node);
1253 dma_async_device_unregister(&sdev->dma_dev);
1254 sprd_dma_disable(sdev);
1255
1256 pm_runtime_put_noidle(&pdev->dev);
1257 pm_runtime_disable(&pdev->dev);
1258 return 0;
1259}
1260
1261static const struct of_device_id sprd_dma_match[] = {
1262 { .compatible = "sprd,sc9860-dma", },
1263 {},
1264};
Zou Wei4faee8b2021-05-04 10:22:57 +08001265MODULE_DEVICE_TABLE(of, sprd_dma_match);
Baolin Wang9b3b8172017-10-24 13:47:50 +08001266
1267static int __maybe_unused sprd_dma_runtime_suspend(struct device *dev)
1268{
1269 struct sprd_dma_dev *sdev = dev_get_drvdata(dev);
1270
1271 sprd_dma_disable(sdev);
1272 return 0;
1273}
1274
1275static int __maybe_unused sprd_dma_runtime_resume(struct device *dev)
1276{
1277 struct sprd_dma_dev *sdev = dev_get_drvdata(dev);
1278 int ret;
1279
1280 ret = sprd_dma_enable(sdev);
1281 if (ret)
1282 dev_err(sdev->dma_dev.dev, "enable dma failed\n");
1283
1284 return ret;
1285}
1286
1287static const struct dev_pm_ops sprd_dma_pm_ops = {
1288 SET_RUNTIME_PM_OPS(sprd_dma_runtime_suspend,
1289 sprd_dma_runtime_resume,
1290 NULL)
1291};
1292
1293static struct platform_driver sprd_dma_driver = {
1294 .probe = sprd_dma_probe,
1295 .remove = sprd_dma_remove,
1296 .driver = {
1297 .name = "sprd-dma",
1298 .of_match_table = sprd_dma_match,
1299 .pm = &sprd_dma_pm_ops,
1300 },
1301};
1302module_platform_driver(sprd_dma_driver);
1303
1304MODULE_LICENSE("GPL v2");
1305MODULE_DESCRIPTION("DMA driver for Spreadtrum");
1306MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
Eric Long53197122018-11-06 13:01:37 +08001307MODULE_AUTHOR("Eric Long <eric.long@spreadtrum.com>");
Baolin Wang9b3b8172017-10-24 13:47:50 +08001308MODULE_ALIAS("platform:sprd-dma");