blob: ecdaada951205954d114b597851f9692b47f8f41 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Zhangfei Gao8e6152b2013-08-27 10:20:10 +08002/*
Andy Greena7e08fa2016-08-29 10:30:52 -07003 * Copyright (c) 2013 - 2015 Linaro Ltd.
Hao Fang1b6216a2021-04-01 19:50:20 +08004 * Copyright (c) 2013 HiSilicon Limited.
Zhangfei Gao8e6152b2013-08-27 10:20:10 +08005 */
6#include <linux/sched.h>
7#include <linux/device.h>
John Stultzb77f2622016-08-29 10:30:50 -07008#include <linux/dma-mapping.h>
9#include <linux/dmapool.h>
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080010#include <linux/dmaengine.h>
11#include <linux/init.h>
12#include <linux/interrupt.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/slab.h>
17#include <linux/spinlock.h>
18#include <linux/of_device.h>
19#include <linux/of.h>
20#include <linux/clk.h>
21#include <linux/of_dma.h>
22
23#include "virt-dma.h"
24
25#define DRIVER_NAME "k3-dma"
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080026#define DMA_MAX_SIZE 0x1ffc
Andy Greena7e08fa2016-08-29 10:30:52 -070027#define DMA_CYCLIC_MAX_PERIOD 0x1000
John Stultzb77f2622016-08-29 10:30:50 -070028#define LLI_BLOCK_SIZE (4 * PAGE_SIZE)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080029
30#define INT_STAT 0x00
31#define INT_TC1 0x04
Andy Greena7e08fa2016-08-29 10:30:52 -070032#define INT_TC2 0x08
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080033#define INT_ERR1 0x0c
34#define INT_ERR2 0x10
35#define INT_TC1_MASK 0x18
Andy Greena7e08fa2016-08-29 10:30:52 -070036#define INT_TC2_MASK 0x1c
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080037#define INT_ERR1_MASK 0x20
38#define INT_ERR2_MASK 0x24
39#define INT_TC1_RAW 0x600
Andy Greena7e08fa2016-08-29 10:30:52 -070040#define INT_TC2_RAW 0x608
Andy Greenaceaaa12016-08-29 10:30:48 -070041#define INT_ERR1_RAW 0x610
42#define INT_ERR2_RAW 0x618
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080043#define CH_PRI 0x688
44#define CH_STAT 0x690
45#define CX_CUR_CNT 0x704
46#define CX_LLI 0x800
Andy Greena7e08fa2016-08-29 10:30:52 -070047#define CX_CNT1 0x80c
48#define CX_CNT0 0x810
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080049#define CX_SRC 0x814
50#define CX_DST 0x818
51#define CX_CFG 0x81c
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080052
53#define CX_LLI_CHAIN_EN 0x2
54#define CX_CFG_EN 0x1
Andy Greena7e08fa2016-08-29 10:30:52 -070055#define CX_CFG_NODEIRQ BIT(1)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080056#define CX_CFG_MEM2PER (0x1 << 2)
57#define CX_CFG_PER2MEM (0x2 << 2)
58#define CX_CFG_SRCINCR (0x1 << 31)
59#define CX_CFG_DSTINCR (0x1 << 30)
60
61struct k3_desc_hw {
62 u32 lli;
63 u32 reserved[3];
64 u32 count;
65 u32 saddr;
66 u32 daddr;
67 u32 config;
68} __aligned(32);
69
70struct k3_dma_desc_sw {
71 struct virt_dma_desc vd;
72 dma_addr_t desc_hw_lli;
73 size_t desc_num;
74 size_t size;
John Stultzb77f2622016-08-29 10:30:50 -070075 struct k3_desc_hw *desc_hw;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080076};
77
78struct k3_dma_phy;
79
80struct k3_dma_chan {
81 u32 ccfg;
82 struct virt_dma_chan vc;
83 struct k3_dma_phy *phy;
84 struct list_head node;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080085 dma_addr_t dev_addr;
86 enum dma_status status;
Andy Greena7e08fa2016-08-29 10:30:52 -070087 bool cyclic;
Vinod Koul2ae1a232018-07-19 22:22:26 +053088 struct dma_slave_config slave_config;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080089};
90
91struct k3_dma_phy {
92 u32 idx;
93 void __iomem *base;
94 struct k3_dma_chan *vchan;
95 struct k3_dma_desc_sw *ds_run;
96 struct k3_dma_desc_sw *ds_done;
97};
98
99struct k3_dma_dev {
100 struct dma_device slave;
101 void __iomem *base;
102 struct tasklet_struct task;
103 spinlock_t lock;
104 struct list_head chan_pending;
105 struct k3_dma_phy *phy;
106 struct k3_dma_chan *chans;
107 struct clk *clk;
John Stultzb77f2622016-08-29 10:30:50 -0700108 struct dma_pool *pool;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800109 u32 dma_channels;
110 u32 dma_requests;
Li Yuc4994a92019-01-24 12:24:20 -0800111 u32 dma_channel_mask;
Vinod Koul486b10a2016-07-03 00:02:29 +0530112 unsigned int irq;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800113};
114
Youlin Wangd4bdc392019-01-24 12:24:18 -0800115
116#define K3_FLAG_NOCLK BIT(1)
117
118struct k3dma_soc_data {
119 unsigned long flags;
120};
121
122
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800123#define to_k3_dma(dmadev) container_of(dmadev, struct k3_dma_dev, slave)
124
Vinod Koul2ae1a232018-07-19 22:22:26 +0530125static int k3_dma_config_write(struct dma_chan *chan,
126 enum dma_transfer_direction dir,
127 struct dma_slave_config *cfg);
128
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800129static struct k3_dma_chan *to_k3_chan(struct dma_chan *chan)
130{
131 return container_of(chan, struct k3_dma_chan, vc.chan);
132}
133
134static void k3_dma_pause_dma(struct k3_dma_phy *phy, bool on)
135{
136 u32 val = 0;
137
138 if (on) {
139 val = readl_relaxed(phy->base + CX_CFG);
140 val |= CX_CFG_EN;
141 writel_relaxed(val, phy->base + CX_CFG);
142 } else {
143 val = readl_relaxed(phy->base + CX_CFG);
144 val &= ~CX_CFG_EN;
145 writel_relaxed(val, phy->base + CX_CFG);
146 }
147}
148
149static void k3_dma_terminate_chan(struct k3_dma_phy *phy, struct k3_dma_dev *d)
150{
151 u32 val = 0;
152
153 k3_dma_pause_dma(phy, false);
154
155 val = 0x1 << phy->idx;
156 writel_relaxed(val, d->base + INT_TC1_RAW);
Andy Greena7e08fa2016-08-29 10:30:52 -0700157 writel_relaxed(val, d->base + INT_TC2_RAW);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800158 writel_relaxed(val, d->base + INT_ERR1_RAW);
159 writel_relaxed(val, d->base + INT_ERR2_RAW);
160}
161
162static void k3_dma_set_desc(struct k3_dma_phy *phy, struct k3_desc_hw *hw)
163{
164 writel_relaxed(hw->lli, phy->base + CX_LLI);
Andy Greena7e08fa2016-08-29 10:30:52 -0700165 writel_relaxed(hw->count, phy->base + CX_CNT0);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800166 writel_relaxed(hw->saddr, phy->base + CX_SRC);
167 writel_relaxed(hw->daddr, phy->base + CX_DST);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800168 writel_relaxed(hw->config, phy->base + CX_CFG);
169}
170
171static u32 k3_dma_get_curr_cnt(struct k3_dma_dev *d, struct k3_dma_phy *phy)
172{
173 u32 cnt = 0;
174
175 cnt = readl_relaxed(d->base + CX_CUR_CNT + phy->idx * 0x10);
176 cnt &= 0xffff;
177 return cnt;
178}
179
180static u32 k3_dma_get_curr_lli(struct k3_dma_phy *phy)
181{
182 return readl_relaxed(phy->base + CX_LLI);
183}
184
185static u32 k3_dma_get_chan_stat(struct k3_dma_dev *d)
186{
187 return readl_relaxed(d->base + CH_STAT);
188}
189
190static void k3_dma_enable_dma(struct k3_dma_dev *d, bool on)
191{
192 if (on) {
193 /* set same priority */
194 writel_relaxed(0x0, d->base + CH_PRI);
195
196 /* unmask irq */
197 writel_relaxed(0xffff, d->base + INT_TC1_MASK);
Andy Greena7e08fa2016-08-29 10:30:52 -0700198 writel_relaxed(0xffff, d->base + INT_TC2_MASK);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800199 writel_relaxed(0xffff, d->base + INT_ERR1_MASK);
200 writel_relaxed(0xffff, d->base + INT_ERR2_MASK);
201 } else {
202 /* mask irq */
203 writel_relaxed(0x0, d->base + INT_TC1_MASK);
Andy Greena7e08fa2016-08-29 10:30:52 -0700204 writel_relaxed(0x0, d->base + INT_TC2_MASK);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800205 writel_relaxed(0x0, d->base + INT_ERR1_MASK);
206 writel_relaxed(0x0, d->base + INT_ERR2_MASK);
207 }
208}
209
210static irqreturn_t k3_dma_int_handler(int irq, void *dev_id)
211{
212 struct k3_dma_dev *d = (struct k3_dma_dev *)dev_id;
213 struct k3_dma_phy *p;
214 struct k3_dma_chan *c;
215 u32 stat = readl_relaxed(d->base + INT_STAT);
216 u32 tc1 = readl_relaxed(d->base + INT_TC1);
Andy Greena7e08fa2016-08-29 10:30:52 -0700217 u32 tc2 = readl_relaxed(d->base + INT_TC2);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800218 u32 err1 = readl_relaxed(d->base + INT_ERR1);
219 u32 err2 = readl_relaxed(d->base + INT_ERR2);
220 u32 i, irq_chan = 0;
221
222 while (stat) {
223 i = __ffs(stat);
Andy Greena7e08fa2016-08-29 10:30:52 -0700224 stat &= ~BIT(i);
225 if (likely(tc1 & BIT(i)) || (tc2 & BIT(i))) {
Andy Greena7e08fa2016-08-29 10:30:52 -0700226
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800227 p = &d->phy[i];
228 c = p->vchan;
Andy Greena7e08fa2016-08-29 10:30:52 -0700229 if (c && (tc1 & BIT(i))) {
Barry Song1ff20652020-10-28 10:52:48 +1300230 spin_lock(&c->vc.lock);
John Stultz2f42e052019-12-18 19:09:06 +0000231 if (p->ds_run != NULL) {
232 vchan_cookie_complete(&p->ds_run->vd);
233 p->ds_done = p->ds_run;
234 p->ds_run = NULL;
235 }
Barry Song1ff20652020-10-28 10:52:48 +1300236 spin_unlock(&c->vc.lock);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800237 }
Andy Greena7e08fa2016-08-29 10:30:52 -0700238 if (c && (tc2 & BIT(i))) {
Barry Song1ff20652020-10-28 10:52:48 +1300239 spin_lock(&c->vc.lock);
Andy Greena7e08fa2016-08-29 10:30:52 -0700240 if (p->ds_run != NULL)
241 vchan_cyclic_callback(&p->ds_run->vd);
Barry Song1ff20652020-10-28 10:52:48 +1300242 spin_unlock(&c->vc.lock);
Andy Greena7e08fa2016-08-29 10:30:52 -0700243 }
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800244 irq_chan |= BIT(i);
245 }
246 if (unlikely((err1 & BIT(i)) || (err2 & BIT(i))))
247 dev_warn(d->slave.dev, "DMA ERR\n");
248 }
249
250 writel_relaxed(irq_chan, d->base + INT_TC1_RAW);
Andy Greena7e08fa2016-08-29 10:30:52 -0700251 writel_relaxed(irq_chan, d->base + INT_TC2_RAW);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800252 writel_relaxed(err1, d->base + INT_ERR1_RAW);
253 writel_relaxed(err2, d->base + INT_ERR2_RAW);
254
Andy Green0173c892016-08-29 10:30:49 -0700255 if (irq_chan)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800256 tasklet_schedule(&d->task);
Andy Green0173c892016-08-29 10:30:49 -0700257
258 if (irq_chan || err1 || err2)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800259 return IRQ_HANDLED;
Andy Green0173c892016-08-29 10:30:49 -0700260
261 return IRQ_NONE;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800262}
263
264static int k3_dma_start_txd(struct k3_dma_chan *c)
265{
266 struct k3_dma_dev *d = to_k3_dma(c->vc.chan.device);
267 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
268
269 if (!c->phy)
270 return -EAGAIN;
271
272 if (BIT(c->phy->idx) & k3_dma_get_chan_stat(d))
273 return -EAGAIN;
274
John Stultz2f42e052019-12-18 19:09:06 +0000275 /* Avoid losing track of ds_run if a transaction is in flight */
276 if (c->phy->ds_run)
277 return -EAGAIN;
278
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800279 if (vd) {
280 struct k3_dma_desc_sw *ds =
281 container_of(vd, struct k3_dma_desc_sw, vd);
282 /*
283 * fetch and remove request from vc->desc_issued
284 * so vc->desc_issued only contains desc pending
285 */
286 list_del(&ds->vd.node);
John Stultz36387a22016-08-29 10:30:51 -0700287
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800288 c->phy->ds_run = ds;
Antonio Borneo626c4e82017-08-01 22:09:25 +0200289 c->phy->ds_done = NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800290 /* start dma */
291 k3_dma_set_desc(c->phy, &ds->desc_hw[0]);
292 return 0;
293 }
Antonio Borneo626c4e82017-08-01 22:09:25 +0200294 c->phy->ds_run = NULL;
295 c->phy->ds_done = NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800296 return -EAGAIN;
297}
298
Allen Pais881bd142020-08-31 16:05:19 +0530299static void k3_dma_tasklet(struct tasklet_struct *t)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800300{
Allen Pais881bd142020-08-31 16:05:19 +0530301 struct k3_dma_dev *d = from_tasklet(d, t, task);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800302 struct k3_dma_phy *p;
303 struct k3_dma_chan *c, *cn;
304 unsigned pch, pch_alloc = 0;
305
306 /* check new dma request of running channel in vc->desc_issued */
307 list_for_each_entry_safe(c, cn, &d->slave.channels, vc.chan.device_node) {
308 spin_lock_irq(&c->vc.lock);
309 p = c->phy;
310 if (p && p->ds_done) {
311 if (k3_dma_start_txd(c)) {
312 /* No current txd associated with this channel */
313 dev_dbg(d->slave.dev, "pchan %u: free\n", p->idx);
314 /* Mark this channel free */
315 c->phy = NULL;
316 p->vchan = NULL;
317 }
318 }
319 spin_unlock_irq(&c->vc.lock);
320 }
321
322 /* check new channel request in d->chan_pending */
323 spin_lock_irq(&d->lock);
324 for (pch = 0; pch < d->dma_channels; pch++) {
Li Yuc4994a92019-01-24 12:24:20 -0800325 if (!(d->dma_channel_mask & (1 << pch)))
326 continue;
327
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800328 p = &d->phy[pch];
329
330 if (p->vchan == NULL && !list_empty(&d->chan_pending)) {
331 c = list_first_entry(&d->chan_pending,
332 struct k3_dma_chan, node);
333 /* remove from d->chan_pending */
334 list_del_init(&c->node);
335 pch_alloc |= 1 << pch;
336 /* Mark this channel allocated */
337 p->vchan = c;
338 c->phy = p;
339 dev_dbg(d->slave.dev, "pchan %u: alloc vchan %p\n", pch, &c->vc);
340 }
341 }
342 spin_unlock_irq(&d->lock);
343
344 for (pch = 0; pch < d->dma_channels; pch++) {
Li Yuc4994a92019-01-24 12:24:20 -0800345 if (!(d->dma_channel_mask & (1 << pch)))
346 continue;
347
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800348 if (pch_alloc & (1 << pch)) {
349 p = &d->phy[pch];
350 c = p->vchan;
351 if (c) {
352 spin_lock_irq(&c->vc.lock);
353 k3_dma_start_txd(c);
354 spin_unlock_irq(&c->vc.lock);
355 }
356 }
357 }
358}
359
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800360static void k3_dma_free_chan_resources(struct dma_chan *chan)
361{
362 struct k3_dma_chan *c = to_k3_chan(chan);
363 struct k3_dma_dev *d = to_k3_dma(chan->device);
364 unsigned long flags;
365
366 spin_lock_irqsave(&d->lock, flags);
367 list_del_init(&c->node);
368 spin_unlock_irqrestore(&d->lock, flags);
369
370 vchan_free_chan_resources(&c->vc);
371 c->ccfg = 0;
372}
373
374static enum dma_status k3_dma_tx_status(struct dma_chan *chan,
375 dma_cookie_t cookie, struct dma_tx_state *state)
376{
377 struct k3_dma_chan *c = to_k3_chan(chan);
378 struct k3_dma_dev *d = to_k3_dma(chan->device);
379 struct k3_dma_phy *p;
380 struct virt_dma_desc *vd;
381 unsigned long flags;
382 enum dma_status ret;
383 size_t bytes = 0;
384
385 ret = dma_cookie_status(&c->vc.chan, cookie, state);
Vinod Koulbd2c3482013-10-16 20:50:09 +0530386 if (ret == DMA_COMPLETE)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800387 return ret;
388
389 spin_lock_irqsave(&c->vc.lock, flags);
390 p = c->phy;
391 ret = c->status;
392
393 /*
394 * If the cookie is on our issue queue, then the residue is
395 * its total size.
396 */
397 vd = vchan_find_desc(&c->vc, cookie);
Andy Greena7e08fa2016-08-29 10:30:52 -0700398 if (vd && !c->cyclic) {
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800399 bytes = container_of(vd, struct k3_dma_desc_sw, vd)->size;
400 } else if ((!p) || (!p->ds_run)) {
401 bytes = 0;
402 } else {
403 struct k3_dma_desc_sw *ds = p->ds_run;
404 u32 clli = 0, index = 0;
405
406 bytes = k3_dma_get_curr_cnt(d, p);
407 clli = k3_dma_get_curr_lli(p);
Andy Greena7e08fa2016-08-29 10:30:52 -0700408 index = ((clli - ds->desc_hw_lli) /
409 sizeof(struct k3_desc_hw)) + 1;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800410 for (; index < ds->desc_num; index++) {
411 bytes += ds->desc_hw[index].count;
412 /* end of lli */
413 if (!ds->desc_hw[index].lli)
414 break;
415 }
416 }
417 spin_unlock_irqrestore(&c->vc.lock, flags);
418 dma_set_residue(state, bytes);
419 return ret;
420}
421
422static void k3_dma_issue_pending(struct dma_chan *chan)
423{
424 struct k3_dma_chan *c = to_k3_chan(chan);
425 struct k3_dma_dev *d = to_k3_dma(chan->device);
426 unsigned long flags;
427
428 spin_lock_irqsave(&c->vc.lock, flags);
429 /* add request to vc->desc_issued */
430 if (vchan_issue_pending(&c->vc)) {
431 spin_lock(&d->lock);
432 if (!c->phy) {
433 if (list_empty(&c->node)) {
434 /* if new channel, add chan_pending */
435 list_add_tail(&c->node, &d->chan_pending);
436 /* check in tasklet */
437 tasklet_schedule(&d->task);
438 dev_dbg(d->slave.dev, "vchan %p: issued\n", &c->vc);
439 }
440 }
441 spin_unlock(&d->lock);
442 } else
443 dev_dbg(d->slave.dev, "vchan %p: nothing to issue\n", &c->vc);
444 spin_unlock_irqrestore(&c->vc.lock, flags);
445}
446
447static void k3_dma_fill_desc(struct k3_dma_desc_sw *ds, dma_addr_t dst,
448 dma_addr_t src, size_t len, u32 num, u32 ccfg)
449{
Andy Greena7e08fa2016-08-29 10:30:52 -0700450 if (num != ds->desc_num - 1)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800451 ds->desc_hw[num].lli = ds->desc_hw_lli + (num + 1) *
452 sizeof(struct k3_desc_hw);
Andy Greena7e08fa2016-08-29 10:30:52 -0700453
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800454 ds->desc_hw[num].lli |= CX_LLI_CHAIN_EN;
455 ds->desc_hw[num].count = len;
456 ds->desc_hw[num].saddr = src;
457 ds->desc_hw[num].daddr = dst;
458 ds->desc_hw[num].config = ccfg;
459}
460
John Stultzb77f2622016-08-29 10:30:50 -0700461static struct k3_dma_desc_sw *k3_dma_alloc_desc_resource(int num,
462 struct dma_chan *chan)
463{
464 struct k3_dma_chan *c = to_k3_chan(chan);
465 struct k3_dma_desc_sw *ds;
466 struct k3_dma_dev *d = to_k3_dma(chan->device);
467 int lli_limit = LLI_BLOCK_SIZE / sizeof(struct k3_desc_hw);
468
469 if (num > lli_limit) {
470 dev_dbg(chan->device->dev, "vch %p: sg num %d exceed max %d\n",
471 &c->vc, num, lli_limit);
472 return NULL;
473 }
474
475 ds = kzalloc(sizeof(*ds), GFP_NOWAIT);
476 if (!ds)
477 return NULL;
478
Vinod Koul646b3b52016-12-07 09:36:22 +0530479 ds->desc_hw = dma_pool_zalloc(d->pool, GFP_NOWAIT, &ds->desc_hw_lli);
John Stultzb77f2622016-08-29 10:30:50 -0700480 if (!ds->desc_hw) {
481 dev_dbg(chan->device->dev, "vch %p: dma alloc fail\n", &c->vc);
482 kfree(ds);
483 return NULL;
484 }
John Stultzb77f2622016-08-29 10:30:50 -0700485 ds->desc_num = num;
486 return ds;
487}
488
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800489static struct dma_async_tx_descriptor *k3_dma_prep_memcpy(
490 struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
491 size_t len, unsigned long flags)
492{
493 struct k3_dma_chan *c = to_k3_chan(chan);
494 struct k3_dma_desc_sw *ds;
495 size_t copy = 0;
496 int num = 0;
497
498 if (!len)
499 return NULL;
500
501 num = DIV_ROUND_UP(len, DMA_MAX_SIZE);
John Stultzb77f2622016-08-29 10:30:50 -0700502
503 ds = k3_dma_alloc_desc_resource(num, chan);
Peter Griffinaef94fe2016-06-07 18:38:41 +0100504 if (!ds)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800505 return NULL;
Peter Griffinaef94fe2016-06-07 18:38:41 +0100506
Andy Greena7e08fa2016-08-29 10:30:52 -0700507 c->cyclic = 0;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800508 ds->size = len;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800509 num = 0;
510
511 if (!c->ccfg) {
Maxime Riparddb084252014-11-17 14:42:20 +0100512 /* default is memtomem, without calling device_config */
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800513 c->ccfg = CX_CFG_SRCINCR | CX_CFG_DSTINCR | CX_CFG_EN;
514 c->ccfg |= (0xf << 20) | (0xf << 24); /* burst = 16 */
515 c->ccfg |= (0x3 << 12) | (0x3 << 16); /* width = 64 bit */
516 }
517
518 do {
519 copy = min_t(size_t, len, DMA_MAX_SIZE);
520 k3_dma_fill_desc(ds, dst, src, copy, num++, c->ccfg);
521
Vinod Koulad7756e2018-10-05 06:32:12 +0530522 src += copy;
523 dst += copy;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800524 len -= copy;
525 } while (len);
526
527 ds->desc_hw[num-1].lli = 0; /* end of link */
528 return vchan_tx_prep(&c->vc, &ds->vd, flags);
529}
530
531static struct dma_async_tx_descriptor *k3_dma_prep_slave_sg(
532 struct dma_chan *chan, struct scatterlist *sgl, unsigned int sglen,
533 enum dma_transfer_direction dir, unsigned long flags, void *context)
534{
535 struct k3_dma_chan *c = to_k3_chan(chan);
536 struct k3_dma_desc_sw *ds;
537 size_t len, avail, total = 0;
538 struct scatterlist *sg;
539 dma_addr_t addr, src = 0, dst = 0;
540 int num = sglen, i;
541
Zhangfei Gaoc61177c2014-01-14 11:37:43 +0800542 if (sgl == NULL)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800543 return NULL;
544
Andy Greena7e08fa2016-08-29 10:30:52 -0700545 c->cyclic = 0;
546
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800547 for_each_sg(sgl, sg, sglen, i) {
548 avail = sg_dma_len(sg);
549 if (avail > DMA_MAX_SIZE)
550 num += DIV_ROUND_UP(avail, DMA_MAX_SIZE) - 1;
551 }
552
John Stultzb77f2622016-08-29 10:30:50 -0700553 ds = k3_dma_alloc_desc_resource(num, chan);
Peter Griffinaef94fe2016-06-07 18:38:41 +0100554 if (!ds)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800555 return NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800556 num = 0;
Vinod Koul2ae1a232018-07-19 22:22:26 +0530557 k3_dma_config_write(chan, dir, &c->slave_config);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800558
559 for_each_sg(sgl, sg, sglen, i) {
560 addr = sg_dma_address(sg);
561 avail = sg_dma_len(sg);
562 total += avail;
563
564 do {
565 len = min_t(size_t, avail, DMA_MAX_SIZE);
566
567 if (dir == DMA_MEM_TO_DEV) {
568 src = addr;
569 dst = c->dev_addr;
570 } else if (dir == DMA_DEV_TO_MEM) {
571 src = c->dev_addr;
572 dst = addr;
573 }
574
575 k3_dma_fill_desc(ds, dst, src, len, num++, c->ccfg);
576
577 addr += len;
578 avail -= len;
579 } while (avail);
580 }
581
582 ds->desc_hw[num-1].lli = 0; /* end of link */
583 ds->size = total;
584 return vchan_tx_prep(&c->vc, &ds->vd, flags);
585}
586
Andy Greena7e08fa2016-08-29 10:30:52 -0700587static struct dma_async_tx_descriptor *
588k3_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr,
589 size_t buf_len, size_t period_len,
590 enum dma_transfer_direction dir,
591 unsigned long flags)
592{
593 struct k3_dma_chan *c = to_k3_chan(chan);
594 struct k3_dma_desc_sw *ds;
595 size_t len, avail, total = 0;
596 dma_addr_t addr, src = 0, dst = 0;
597 int num = 1, since = 0;
598 size_t modulo = DMA_CYCLIC_MAX_PERIOD;
599 u32 en_tc2 = 0;
600
Arnd Bergmann5f03c392016-09-06 15:17:49 +0200601 dev_dbg(chan->device->dev, "%s: buf %pad, dst %pad, buf len %zu, period_len = %zu, dir %d\n",
602 __func__, &buf_addr, &to_k3_chan(chan)->dev_addr,
603 buf_len, period_len, (int)dir);
Andy Greena7e08fa2016-08-29 10:30:52 -0700604
605 avail = buf_len;
606 if (avail > modulo)
607 num += DIV_ROUND_UP(avail, modulo) - 1;
608
609 ds = k3_dma_alloc_desc_resource(num, chan);
610 if (!ds)
611 return NULL;
612
613 c->cyclic = 1;
614 addr = buf_addr;
615 avail = buf_len;
616 total = avail;
617 num = 0;
Vinod Koul2ae1a232018-07-19 22:22:26 +0530618 k3_dma_config_write(chan, dir, &c->slave_config);
Andy Greena7e08fa2016-08-29 10:30:52 -0700619
620 if (period_len < modulo)
621 modulo = period_len;
622
623 do {
624 len = min_t(size_t, avail, modulo);
625
626 if (dir == DMA_MEM_TO_DEV) {
627 src = addr;
628 dst = c->dev_addr;
629 } else if (dir == DMA_DEV_TO_MEM) {
630 src = c->dev_addr;
631 dst = addr;
632 }
633 since += len;
634 if (since >= period_len) {
635 /* descriptor asks for TC2 interrupt on completion */
636 en_tc2 = CX_CFG_NODEIRQ;
637 since -= period_len;
638 } else
639 en_tc2 = 0;
640
641 k3_dma_fill_desc(ds, dst, src, len, num++, c->ccfg | en_tc2);
642
643 addr += len;
644 avail -= len;
645 } while (avail);
646
647 /* "Cyclic" == end of link points back to start of link */
648 ds->desc_hw[num - 1].lli |= ds->desc_hw_lli;
649
650 ds->size = total;
651
652 return vchan_tx_prep(&c->vc, &ds->vd, flags);
653}
654
Maxime Riparddb084252014-11-17 14:42:20 +0100655static int k3_dma_config(struct dma_chan *chan,
656 struct dma_slave_config *cfg)
657{
658 struct k3_dma_chan *c = to_k3_chan(chan);
Vinod Koul2ae1a232018-07-19 22:22:26 +0530659
660 memcpy(&c->slave_config, cfg, sizeof(*cfg));
661
662 return 0;
663}
664
665static int k3_dma_config_write(struct dma_chan *chan,
666 enum dma_transfer_direction dir,
667 struct dma_slave_config *cfg)
668{
669 struct k3_dma_chan *c = to_k3_chan(chan);
Maxime Riparddb084252014-11-17 14:42:20 +0100670 u32 maxburst = 0, val = 0;
671 enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
672
Vinod Koul2ae1a232018-07-19 22:22:26 +0530673 if (dir == DMA_DEV_TO_MEM) {
Maxime Riparddb084252014-11-17 14:42:20 +0100674 c->ccfg = CX_CFG_DSTINCR;
675 c->dev_addr = cfg->src_addr;
676 maxburst = cfg->src_maxburst;
677 width = cfg->src_addr_width;
Vinod Koul2ae1a232018-07-19 22:22:26 +0530678 } else if (dir == DMA_MEM_TO_DEV) {
Maxime Riparddb084252014-11-17 14:42:20 +0100679 c->ccfg = CX_CFG_SRCINCR;
680 c->dev_addr = cfg->dst_addr;
681 maxburst = cfg->dst_maxburst;
682 width = cfg->dst_addr_width;
683 }
684 switch (width) {
685 case DMA_SLAVE_BUSWIDTH_1_BYTE:
686 case DMA_SLAVE_BUSWIDTH_2_BYTES:
687 case DMA_SLAVE_BUSWIDTH_4_BYTES:
688 case DMA_SLAVE_BUSWIDTH_8_BYTES:
689 val = __ffs(width);
690 break;
691 default:
692 val = 3;
693 break;
694 }
695 c->ccfg |= (val << 12) | (val << 16);
696
697 if ((maxburst == 0) || (maxburst > 16))
Andy Green6c28a902016-08-29 10:30:47 -0700698 val = 15;
Maxime Riparddb084252014-11-17 14:42:20 +0100699 else
700 val = maxburst - 1;
701 c->ccfg |= (val << 20) | (val << 24);
702 c->ccfg |= CX_CFG_MEM2PER | CX_CFG_EN;
703
704 /* specific request line */
705 c->ccfg |= c->vc.chan.chan_id << 4;
706
707 return 0;
708}
709
John Stultz36387a22016-08-29 10:30:51 -0700710static void k3_dma_free_desc(struct virt_dma_desc *vd)
711{
712 struct k3_dma_desc_sw *ds =
713 container_of(vd, struct k3_dma_desc_sw, vd);
714 struct k3_dma_dev *d = to_k3_dma(vd->tx.chan->device);
715
716 dma_pool_free(d->pool, ds->desc_hw, ds->desc_hw_lli);
717 kfree(ds);
718}
719
Maxime Riparddb084252014-11-17 14:42:20 +0100720static int k3_dma_terminate_all(struct dma_chan *chan)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800721{
722 struct k3_dma_chan *c = to_k3_chan(chan);
723 struct k3_dma_dev *d = to_k3_dma(chan->device);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800724 struct k3_dma_phy *p = c->phy;
725 unsigned long flags;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800726 LIST_HEAD(head);
727
Maxime Riparddb084252014-11-17 14:42:20 +0100728 dev_dbg(d->slave.dev, "vchan %p: terminate all\n", &c->vc);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800729
Maxime Riparddb084252014-11-17 14:42:20 +0100730 /* Prevent this channel being scheduled */
731 spin_lock(&d->lock);
732 list_del_init(&c->node);
733 spin_unlock(&d->lock);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800734
Maxime Riparddb084252014-11-17 14:42:20 +0100735 /* Clear the tx descriptor lists */
736 spin_lock_irqsave(&c->vc.lock, flags);
737 vchan_get_all_descriptors(&c->vc, &head);
738 if (p) {
739 /* vchan is assigned to a pchan - stop the channel */
740 k3_dma_terminate_chan(p, d);
741 c->phy = NULL;
742 p->vchan = NULL;
John Stultz36387a22016-08-29 10:30:51 -0700743 if (p->ds_run) {
Peter Ujfalusi3ee7e422017-11-14 16:32:11 +0200744 vchan_terminate_vdesc(&p->ds_run->vd);
John Stultz36387a22016-08-29 10:30:51 -0700745 p->ds_run = NULL;
746 }
Antonio Borneo132b4732017-08-01 22:09:26 +0200747 p->ds_done = NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800748 }
Maxime Riparddb084252014-11-17 14:42:20 +0100749 spin_unlock_irqrestore(&c->vc.lock, flags);
750 vchan_dma_desc_free_list(&c->vc, &head);
751
752 return 0;
753}
754
Peter Ujfalusi3ee7e422017-11-14 16:32:11 +0200755static void k3_dma_synchronize(struct dma_chan *chan)
756{
757 struct k3_dma_chan *c = to_k3_chan(chan);
758
759 vchan_synchronize(&c->vc);
760}
761
Krzysztof Kozlowskia1a9bec2014-12-29 14:01:30 +0100762static int k3_dma_transfer_pause(struct dma_chan *chan)
Maxime Riparddb084252014-11-17 14:42:20 +0100763{
764 struct k3_dma_chan *c = to_k3_chan(chan);
765 struct k3_dma_dev *d = to_k3_dma(chan->device);
766 struct k3_dma_phy *p = c->phy;
767
768 dev_dbg(d->slave.dev, "vchan %p: pause\n", &c->vc);
769 if (c->status == DMA_IN_PROGRESS) {
770 c->status = DMA_PAUSED;
771 if (p) {
772 k3_dma_pause_dma(p, false);
773 } else {
774 spin_lock(&d->lock);
775 list_del_init(&c->node);
776 spin_unlock(&d->lock);
777 }
778 }
779
780 return 0;
781}
782
Krzysztof Kozlowskia1a9bec2014-12-29 14:01:30 +0100783static int k3_dma_transfer_resume(struct dma_chan *chan)
Maxime Riparddb084252014-11-17 14:42:20 +0100784{
785 struct k3_dma_chan *c = to_k3_chan(chan);
786 struct k3_dma_dev *d = to_k3_dma(chan->device);
787 struct k3_dma_phy *p = c->phy;
788 unsigned long flags;
789
790 dev_dbg(d->slave.dev, "vchan %p: resume\n", &c->vc);
791 spin_lock_irqsave(&c->vc.lock, flags);
792 if (c->status == DMA_PAUSED) {
793 c->status = DMA_IN_PROGRESS;
794 if (p) {
795 k3_dma_pause_dma(p, true);
796 } else if (!list_empty(&c->vc.desc_issued)) {
797 spin_lock(&d->lock);
798 list_add_tail(&c->node, &d->chan_pending);
799 spin_unlock(&d->lock);
800 }
801 }
802 spin_unlock_irqrestore(&c->vc.lock, flags);
803
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800804 return 0;
805}
806
Youlin Wangd4bdc392019-01-24 12:24:18 -0800807static const struct k3dma_soc_data k3_v1_dma_data = {
808 .flags = 0,
809};
810
811static const struct k3dma_soc_data asp_v1_dma_data = {
812 .flags = K3_FLAG_NOCLK,
813};
814
Fabian Frederick57c03422015-03-16 20:17:14 +0100815static const struct of_device_id k3_pdma_dt_ids[] = {
Youlin Wangd4bdc392019-01-24 12:24:18 -0800816 { .compatible = "hisilicon,k3-dma-1.0",
817 .data = &k3_v1_dma_data
818 },
819 { .compatible = "hisilicon,hisi-pcm-asp-dma-1.0",
820 .data = &asp_v1_dma_data
821 },
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800822 {}
823};
824MODULE_DEVICE_TABLE(of, k3_pdma_dt_ids);
825
826static struct dma_chan *k3_of_dma_simple_xlate(struct of_phandle_args *dma_spec,
827 struct of_dma *ofdma)
828{
829 struct k3_dma_dev *d = ofdma->of_dma_data;
830 unsigned int request = dma_spec->args[0];
831
Dan Carpenterc4c2b762018-06-22 14:15:47 +0300832 if (request >= d->dma_requests)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800833 return NULL;
834
835 return dma_get_slave_channel(&(d->chans[request].vc.chan));
836}
837
838static int k3_dma_probe(struct platform_device *op)
839{
Youlin Wangd4bdc392019-01-24 12:24:18 -0800840 const struct k3dma_soc_data *soc_data;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800841 struct k3_dma_dev *d;
842 const struct of_device_id *of_id;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800843 int i, ret, irq = 0;
844
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800845 d = devm_kzalloc(&op->dev, sizeof(*d), GFP_KERNEL);
846 if (!d)
847 return -ENOMEM;
848
Youlin Wangd4bdc392019-01-24 12:24:18 -0800849 soc_data = device_get_match_data(&op->dev);
850 if (!soc_data)
851 return -EINVAL;
852
Markus Elfring3d4d6c22019-09-22 11:36:18 +0200853 d->base = devm_platform_ioremap_resource(op, 0);
Jingoo Hana576b7f2013-09-02 10:25:13 +0900854 if (IS_ERR(d->base))
855 return PTR_ERR(d->base);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800856
857 of_id = of_match_device(k3_pdma_dt_ids, &op->dev);
858 if (of_id) {
859 of_property_read_u32((&op->dev)->of_node,
860 "dma-channels", &d->dma_channels);
861 of_property_read_u32((&op->dev)->of_node,
862 "dma-requests", &d->dma_requests);
Li Yuc4994a92019-01-24 12:24:20 -0800863 ret = of_property_read_u32((&op->dev)->of_node,
864 "dma-channel-mask", &d->dma_channel_mask);
865 if (ret) {
866 dev_warn(&op->dev,
867 "dma-channel-mask doesn't exist, considering all as available.\n");
868 d->dma_channel_mask = (u32)~0UL;
869 }
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800870 }
871
Youlin Wangd4bdc392019-01-24 12:24:18 -0800872 if (!(soc_data->flags & K3_FLAG_NOCLK)) {
873 d->clk = devm_clk_get(&op->dev, NULL);
874 if (IS_ERR(d->clk)) {
875 dev_err(&op->dev, "no dma clk\n");
876 return PTR_ERR(d->clk);
877 }
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800878 }
879
880 irq = platform_get_irq(op, 0);
881 ret = devm_request_irq(&op->dev, irq,
Michael Opdenacker174b5372013-10-13 07:10:51 +0200882 k3_dma_int_handler, 0, DRIVER_NAME, d);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800883 if (ret)
884 return ret;
885
Vinod Koul486b10a2016-07-03 00:02:29 +0530886 d->irq = irq;
887
John Stultzb77f2622016-08-29 10:30:50 -0700888 /* A DMA memory pool for LLIs, align on 32-byte boundary */
889 d->pool = dmam_pool_create(DRIVER_NAME, &op->dev,
890 LLI_BLOCK_SIZE, 32, 0);
891 if (!d->pool)
892 return -ENOMEM;
893
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800894 /* init phy channel */
Kees Cooka86854d2018-06-12 14:07:58 -0700895 d->phy = devm_kcalloc(&op->dev,
896 d->dma_channels, sizeof(struct k3_dma_phy), GFP_KERNEL);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800897 if (d->phy == NULL)
898 return -ENOMEM;
899
900 for (i = 0; i < d->dma_channels; i++) {
Li Yuc4994a92019-01-24 12:24:20 -0800901 struct k3_dma_phy *p;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800902
Li Yuc4994a92019-01-24 12:24:20 -0800903 if (!(d->dma_channel_mask & BIT(i)))
904 continue;
905
906 p = &d->phy[i];
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800907 p->idx = i;
908 p->base = d->base + i * 0x40;
909 }
910
911 INIT_LIST_HEAD(&d->slave.channels);
912 dma_cap_set(DMA_SLAVE, d->slave.cap_mask);
913 dma_cap_set(DMA_MEMCPY, d->slave.cap_mask);
Andy Greena7e08fa2016-08-29 10:30:52 -0700914 dma_cap_set(DMA_CYCLIC, d->slave.cap_mask);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800915 d->slave.dev = &op->dev;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800916 d->slave.device_free_chan_resources = k3_dma_free_chan_resources;
917 d->slave.device_tx_status = k3_dma_tx_status;
918 d->slave.device_prep_dma_memcpy = k3_dma_prep_memcpy;
919 d->slave.device_prep_slave_sg = k3_dma_prep_slave_sg;
Andy Greena7e08fa2016-08-29 10:30:52 -0700920 d->slave.device_prep_dma_cyclic = k3_dma_prep_dma_cyclic;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800921 d->slave.device_issue_pending = k3_dma_issue_pending;
Maxime Riparddb084252014-11-17 14:42:20 +0100922 d->slave.device_config = k3_dma_config;
Krzysztof Kozlowskia1a9bec2014-12-29 14:01:30 +0100923 d->slave.device_pause = k3_dma_transfer_pause;
924 d->slave.device_resume = k3_dma_transfer_resume;
Maxime Riparddb084252014-11-17 14:42:20 +0100925 d->slave.device_terminate_all = k3_dma_terminate_all;
Peter Ujfalusi3ee7e422017-11-14 16:32:11 +0200926 d->slave.device_synchronize = k3_dma_synchronize;
Maxime Ripard77a68e52015-07-20 10:41:32 +0200927 d->slave.copy_align = DMAENGINE_ALIGN_8_BYTES;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800928
929 /* init virtual channel */
Kees Cooka86854d2018-06-12 14:07:58 -0700930 d->chans = devm_kcalloc(&op->dev,
931 d->dma_requests, sizeof(struct k3_dma_chan), GFP_KERNEL);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800932 if (d->chans == NULL)
933 return -ENOMEM;
934
935 for (i = 0; i < d->dma_requests; i++) {
936 struct k3_dma_chan *c = &d->chans[i];
937
938 c->status = DMA_IN_PROGRESS;
939 INIT_LIST_HEAD(&c->node);
940 c->vc.desc_free = k3_dma_free_desc;
941 vchan_init(&c->vc, &d->slave);
942 }
943
944 /* Enable clock before accessing registers */
945 ret = clk_prepare_enable(d->clk);
946 if (ret < 0) {
947 dev_err(&op->dev, "clk_prepare_enable failed: %d\n", ret);
948 return ret;
949 }
950
951 k3_dma_enable_dma(d, true);
952
953 ret = dma_async_device_register(&d->slave);
954 if (ret)
Wei Yongjun89b90c02016-07-19 11:29:41 +0000955 goto dma_async_register_fail;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800956
957 ret = of_dma_controller_register((&op->dev)->of_node,
958 k3_of_dma_simple_xlate, d);
959 if (ret)
960 goto of_dma_register_fail;
961
962 spin_lock_init(&d->lock);
963 INIT_LIST_HEAD(&d->chan_pending);
Allen Pais881bd142020-08-31 16:05:19 +0530964 tasklet_setup(&d->task, k3_dma_tasklet);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800965 platform_set_drvdata(op, d);
966 dev_info(&op->dev, "initialized\n");
967
968 return 0;
969
970of_dma_register_fail:
971 dma_async_device_unregister(&d->slave);
Wei Yongjun89b90c02016-07-19 11:29:41 +0000972dma_async_register_fail:
973 clk_disable_unprepare(d->clk);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800974 return ret;
975}
976
977static int k3_dma_remove(struct platform_device *op)
978{
979 struct k3_dma_chan *c, *cn;
980 struct k3_dma_dev *d = platform_get_drvdata(op);
981
982 dma_async_device_unregister(&d->slave);
983 of_dma_controller_free((&op->dev)->of_node);
984
Vinod Koul486b10a2016-07-03 00:02:29 +0530985 devm_free_irq(&op->dev, d->irq, d);
986
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800987 list_for_each_entry_safe(c, cn, &d->slave.channels, vc.chan.device_node) {
988 list_del(&c->vc.chan.device_node);
989 tasklet_kill(&c->vc.task);
990 }
991 tasklet_kill(&d->task);
992 clk_disable_unprepare(d->clk);
993 return 0;
994}
995
Jingoo Hanaf2d3132014-10-27 21:36:26 +0900996#ifdef CONFIG_PM_SLEEP
Arnd Bergmann10b3e222015-01-13 14:23:13 +0100997static int k3_dma_suspend_dev(struct device *dev)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800998{
999 struct k3_dma_dev *d = dev_get_drvdata(dev);
1000 u32 stat = 0;
1001
1002 stat = k3_dma_get_chan_stat(d);
1003 if (stat) {
1004 dev_warn(d->slave.dev,
1005 "chan %d is running fail to suspend\n", stat);
1006 return -1;
1007 }
1008 k3_dma_enable_dma(d, false);
1009 clk_disable_unprepare(d->clk);
1010 return 0;
1011}
1012
Arnd Bergmann10b3e222015-01-13 14:23:13 +01001013static int k3_dma_resume_dev(struct device *dev)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +08001014{
1015 struct k3_dma_dev *d = dev_get_drvdata(dev);
1016 int ret = 0;
1017
1018 ret = clk_prepare_enable(d->clk);
1019 if (ret < 0) {
1020 dev_err(d->slave.dev, "clk_prepare_enable failed: %d\n", ret);
1021 return ret;
1022 }
1023 k3_dma_enable_dma(d, true);
1024 return 0;
1025}
Jingoo Hanaf2d3132014-10-27 21:36:26 +09001026#endif
Zhangfei Gao8e6152b2013-08-27 10:20:10 +08001027
Arnd Bergmann10b3e222015-01-13 14:23:13 +01001028static SIMPLE_DEV_PM_OPS(k3_dma_pmops, k3_dma_suspend_dev, k3_dma_resume_dev);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +08001029
1030static struct platform_driver k3_pdma_driver = {
1031 .driver = {
1032 .name = DRIVER_NAME,
Zhangfei Gao8e6152b2013-08-27 10:20:10 +08001033 .pm = &k3_dma_pmops,
1034 .of_match_table = k3_pdma_dt_ids,
1035 },
1036 .probe = k3_dma_probe,
1037 .remove = k3_dma_remove,
1038};
1039
1040module_platform_driver(k3_pdma_driver);
1041
Hao Fang1b6216a2021-04-01 19:50:20 +08001042MODULE_DESCRIPTION("HiSilicon k3 DMA Driver");
Zhangfei Gao8e6152b2013-08-27 10:20:10 +08001043MODULE_ALIAS("platform:k3dma");
1044MODULE_LICENSE("GPL v2");