blob: 9d96c956c25f6c03340da4acc2de5b40fd5d89f5 [file] [log] [blame]
Zhangfei Gao8e6152b2013-08-27 10:20:10 +08001/*
2 * Copyright (c) 2013 Linaro Ltd.
3 * Copyright (c) 2013 Hisilicon Limited.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9#include <linux/sched.h>
10#include <linux/device.h>
John Stultzb77f2622016-08-29 10:30:50 -070011#include <linux/dma-mapping.h>
12#include <linux/dmapool.h>
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080013#include <linux/dmaengine.h>
14#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/platform_device.h>
19#include <linux/slab.h>
20#include <linux/spinlock.h>
21#include <linux/of_device.h>
22#include <linux/of.h>
23#include <linux/clk.h>
24#include <linux/of_dma.h>
25
26#include "virt-dma.h"
27
28#define DRIVER_NAME "k3-dma"
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080029#define DMA_MAX_SIZE 0x1ffc
John Stultzb77f2622016-08-29 10:30:50 -070030#define LLI_BLOCK_SIZE (4 * PAGE_SIZE)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080031
32#define INT_STAT 0x00
33#define INT_TC1 0x04
34#define INT_ERR1 0x0c
35#define INT_ERR2 0x10
36#define INT_TC1_MASK 0x18
37#define INT_ERR1_MASK 0x20
38#define INT_ERR2_MASK 0x24
39#define INT_TC1_RAW 0x600
Andy Greenaceaaa12016-08-29 10:30:48 -070040#define INT_ERR1_RAW 0x610
41#define INT_ERR2_RAW 0x618
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080042#define CH_PRI 0x688
43#define CH_STAT 0x690
44#define CX_CUR_CNT 0x704
45#define CX_LLI 0x800
46#define CX_CNT 0x810
47#define CX_SRC 0x814
48#define CX_DST 0x818
49#define CX_CFG 0x81c
50#define AXI_CFG 0x820
51#define AXI_CFG_DEFAULT 0x201201
52
53#define CX_LLI_CHAIN_EN 0x2
54#define CX_CFG_EN 0x1
55#define CX_CFG_MEM2PER (0x1 << 2)
56#define CX_CFG_PER2MEM (0x2 << 2)
57#define CX_CFG_SRCINCR (0x1 << 31)
58#define CX_CFG_DSTINCR (0x1 << 30)
59
60struct k3_desc_hw {
61 u32 lli;
62 u32 reserved[3];
63 u32 count;
64 u32 saddr;
65 u32 daddr;
66 u32 config;
67} __aligned(32);
68
69struct k3_dma_desc_sw {
70 struct virt_dma_desc vd;
71 dma_addr_t desc_hw_lli;
72 size_t desc_num;
73 size_t size;
John Stultzb77f2622016-08-29 10:30:50 -070074 struct k3_desc_hw *desc_hw;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080075};
76
77struct k3_dma_phy;
78
79struct k3_dma_chan {
80 u32 ccfg;
81 struct virt_dma_chan vc;
82 struct k3_dma_phy *phy;
83 struct list_head node;
84 enum dma_transfer_direction dir;
85 dma_addr_t dev_addr;
86 enum dma_status status;
87};
88
89struct k3_dma_phy {
90 u32 idx;
91 void __iomem *base;
92 struct k3_dma_chan *vchan;
93 struct k3_dma_desc_sw *ds_run;
94 struct k3_dma_desc_sw *ds_done;
95};
96
97struct k3_dma_dev {
98 struct dma_device slave;
99 void __iomem *base;
100 struct tasklet_struct task;
101 spinlock_t lock;
102 struct list_head chan_pending;
103 struct k3_dma_phy *phy;
104 struct k3_dma_chan *chans;
105 struct clk *clk;
John Stultzb77f2622016-08-29 10:30:50 -0700106 struct dma_pool *pool;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800107 u32 dma_channels;
108 u32 dma_requests;
Vinod Koul486b10a2016-07-03 00:02:29 +0530109 unsigned int irq;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800110};
111
112#define to_k3_dma(dmadev) container_of(dmadev, struct k3_dma_dev, slave)
113
114static struct k3_dma_chan *to_k3_chan(struct dma_chan *chan)
115{
116 return container_of(chan, struct k3_dma_chan, vc.chan);
117}
118
119static void k3_dma_pause_dma(struct k3_dma_phy *phy, bool on)
120{
121 u32 val = 0;
122
123 if (on) {
124 val = readl_relaxed(phy->base + CX_CFG);
125 val |= CX_CFG_EN;
126 writel_relaxed(val, phy->base + CX_CFG);
127 } else {
128 val = readl_relaxed(phy->base + CX_CFG);
129 val &= ~CX_CFG_EN;
130 writel_relaxed(val, phy->base + CX_CFG);
131 }
132}
133
134static void k3_dma_terminate_chan(struct k3_dma_phy *phy, struct k3_dma_dev *d)
135{
136 u32 val = 0;
137
138 k3_dma_pause_dma(phy, false);
139
140 val = 0x1 << phy->idx;
141 writel_relaxed(val, d->base + INT_TC1_RAW);
142 writel_relaxed(val, d->base + INT_ERR1_RAW);
143 writel_relaxed(val, d->base + INT_ERR2_RAW);
144}
145
146static void k3_dma_set_desc(struct k3_dma_phy *phy, struct k3_desc_hw *hw)
147{
148 writel_relaxed(hw->lli, phy->base + CX_LLI);
149 writel_relaxed(hw->count, phy->base + CX_CNT);
150 writel_relaxed(hw->saddr, phy->base + CX_SRC);
151 writel_relaxed(hw->daddr, phy->base + CX_DST);
152 writel_relaxed(AXI_CFG_DEFAULT, phy->base + AXI_CFG);
153 writel_relaxed(hw->config, phy->base + CX_CFG);
154}
155
156static u32 k3_dma_get_curr_cnt(struct k3_dma_dev *d, struct k3_dma_phy *phy)
157{
158 u32 cnt = 0;
159
160 cnt = readl_relaxed(d->base + CX_CUR_CNT + phy->idx * 0x10);
161 cnt &= 0xffff;
162 return cnt;
163}
164
165static u32 k3_dma_get_curr_lli(struct k3_dma_phy *phy)
166{
167 return readl_relaxed(phy->base + CX_LLI);
168}
169
170static u32 k3_dma_get_chan_stat(struct k3_dma_dev *d)
171{
172 return readl_relaxed(d->base + CH_STAT);
173}
174
175static void k3_dma_enable_dma(struct k3_dma_dev *d, bool on)
176{
177 if (on) {
178 /* set same priority */
179 writel_relaxed(0x0, d->base + CH_PRI);
180
181 /* unmask irq */
182 writel_relaxed(0xffff, d->base + INT_TC1_MASK);
183 writel_relaxed(0xffff, d->base + INT_ERR1_MASK);
184 writel_relaxed(0xffff, d->base + INT_ERR2_MASK);
185 } else {
186 /* mask irq */
187 writel_relaxed(0x0, d->base + INT_TC1_MASK);
188 writel_relaxed(0x0, d->base + INT_ERR1_MASK);
189 writel_relaxed(0x0, d->base + INT_ERR2_MASK);
190 }
191}
192
193static irqreturn_t k3_dma_int_handler(int irq, void *dev_id)
194{
195 struct k3_dma_dev *d = (struct k3_dma_dev *)dev_id;
196 struct k3_dma_phy *p;
197 struct k3_dma_chan *c;
198 u32 stat = readl_relaxed(d->base + INT_STAT);
199 u32 tc1 = readl_relaxed(d->base + INT_TC1);
200 u32 err1 = readl_relaxed(d->base + INT_ERR1);
201 u32 err2 = readl_relaxed(d->base + INT_ERR2);
202 u32 i, irq_chan = 0;
203
204 while (stat) {
205 i = __ffs(stat);
206 stat &= (stat - 1);
207 if (likely(tc1 & BIT(i))) {
208 p = &d->phy[i];
209 c = p->vchan;
210 if (c) {
211 unsigned long flags;
212
213 spin_lock_irqsave(&c->vc.lock, flags);
214 vchan_cookie_complete(&p->ds_run->vd);
215 p->ds_done = p->ds_run;
216 spin_unlock_irqrestore(&c->vc.lock, flags);
217 }
218 irq_chan |= BIT(i);
219 }
220 if (unlikely((err1 & BIT(i)) || (err2 & BIT(i))))
221 dev_warn(d->slave.dev, "DMA ERR\n");
222 }
223
224 writel_relaxed(irq_chan, d->base + INT_TC1_RAW);
225 writel_relaxed(err1, d->base + INT_ERR1_RAW);
226 writel_relaxed(err2, d->base + INT_ERR2_RAW);
227
Andy Green0173c892016-08-29 10:30:49 -0700228 if (irq_chan)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800229 tasklet_schedule(&d->task);
Andy Green0173c892016-08-29 10:30:49 -0700230
231 if (irq_chan || err1 || err2)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800232 return IRQ_HANDLED;
Andy Green0173c892016-08-29 10:30:49 -0700233
234 return IRQ_NONE;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800235}
236
237static int k3_dma_start_txd(struct k3_dma_chan *c)
238{
239 struct k3_dma_dev *d = to_k3_dma(c->vc.chan.device);
240 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
241
242 if (!c->phy)
243 return -EAGAIN;
244
245 if (BIT(c->phy->idx) & k3_dma_get_chan_stat(d))
246 return -EAGAIN;
247
248 if (vd) {
249 struct k3_dma_desc_sw *ds =
250 container_of(vd, struct k3_dma_desc_sw, vd);
251 /*
252 * fetch and remove request from vc->desc_issued
253 * so vc->desc_issued only contains desc pending
254 */
255 list_del(&ds->vd.node);
256 c->phy->ds_run = ds;
257 c->phy->ds_done = NULL;
258 /* start dma */
259 k3_dma_set_desc(c->phy, &ds->desc_hw[0]);
260 return 0;
261 }
262 c->phy->ds_done = NULL;
263 c->phy->ds_run = NULL;
264 return -EAGAIN;
265}
266
267static void k3_dma_tasklet(unsigned long arg)
268{
269 struct k3_dma_dev *d = (struct k3_dma_dev *)arg;
270 struct k3_dma_phy *p;
271 struct k3_dma_chan *c, *cn;
272 unsigned pch, pch_alloc = 0;
273
274 /* check new dma request of running channel in vc->desc_issued */
275 list_for_each_entry_safe(c, cn, &d->slave.channels, vc.chan.device_node) {
276 spin_lock_irq(&c->vc.lock);
277 p = c->phy;
278 if (p && p->ds_done) {
279 if (k3_dma_start_txd(c)) {
280 /* No current txd associated with this channel */
281 dev_dbg(d->slave.dev, "pchan %u: free\n", p->idx);
282 /* Mark this channel free */
283 c->phy = NULL;
284 p->vchan = NULL;
285 }
286 }
287 spin_unlock_irq(&c->vc.lock);
288 }
289
290 /* check new channel request in d->chan_pending */
291 spin_lock_irq(&d->lock);
292 for (pch = 0; pch < d->dma_channels; pch++) {
293 p = &d->phy[pch];
294
295 if (p->vchan == NULL && !list_empty(&d->chan_pending)) {
296 c = list_first_entry(&d->chan_pending,
297 struct k3_dma_chan, node);
298 /* remove from d->chan_pending */
299 list_del_init(&c->node);
300 pch_alloc |= 1 << pch;
301 /* Mark this channel allocated */
302 p->vchan = c;
303 c->phy = p;
304 dev_dbg(d->slave.dev, "pchan %u: alloc vchan %p\n", pch, &c->vc);
305 }
306 }
307 spin_unlock_irq(&d->lock);
308
309 for (pch = 0; pch < d->dma_channels; pch++) {
310 if (pch_alloc & (1 << pch)) {
311 p = &d->phy[pch];
312 c = p->vchan;
313 if (c) {
314 spin_lock_irq(&c->vc.lock);
315 k3_dma_start_txd(c);
316 spin_unlock_irq(&c->vc.lock);
317 }
318 }
319 }
320}
321
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800322static void k3_dma_free_chan_resources(struct dma_chan *chan)
323{
324 struct k3_dma_chan *c = to_k3_chan(chan);
325 struct k3_dma_dev *d = to_k3_dma(chan->device);
326 unsigned long flags;
327
328 spin_lock_irqsave(&d->lock, flags);
329 list_del_init(&c->node);
330 spin_unlock_irqrestore(&d->lock, flags);
331
332 vchan_free_chan_resources(&c->vc);
333 c->ccfg = 0;
334}
335
336static enum dma_status k3_dma_tx_status(struct dma_chan *chan,
337 dma_cookie_t cookie, struct dma_tx_state *state)
338{
339 struct k3_dma_chan *c = to_k3_chan(chan);
340 struct k3_dma_dev *d = to_k3_dma(chan->device);
341 struct k3_dma_phy *p;
342 struct virt_dma_desc *vd;
343 unsigned long flags;
344 enum dma_status ret;
345 size_t bytes = 0;
346
347 ret = dma_cookie_status(&c->vc.chan, cookie, state);
Vinod Koulbd2c3482013-10-16 20:50:09 +0530348 if (ret == DMA_COMPLETE)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800349 return ret;
350
351 spin_lock_irqsave(&c->vc.lock, flags);
352 p = c->phy;
353 ret = c->status;
354
355 /*
356 * If the cookie is on our issue queue, then the residue is
357 * its total size.
358 */
359 vd = vchan_find_desc(&c->vc, cookie);
360 if (vd) {
361 bytes = container_of(vd, struct k3_dma_desc_sw, vd)->size;
362 } else if ((!p) || (!p->ds_run)) {
363 bytes = 0;
364 } else {
365 struct k3_dma_desc_sw *ds = p->ds_run;
366 u32 clli = 0, index = 0;
367
368 bytes = k3_dma_get_curr_cnt(d, p);
369 clli = k3_dma_get_curr_lli(p);
370 index = (clli - ds->desc_hw_lli) / sizeof(struct k3_desc_hw);
371 for (; index < ds->desc_num; index++) {
372 bytes += ds->desc_hw[index].count;
373 /* end of lli */
374 if (!ds->desc_hw[index].lli)
375 break;
376 }
377 }
378 spin_unlock_irqrestore(&c->vc.lock, flags);
379 dma_set_residue(state, bytes);
380 return ret;
381}
382
383static void k3_dma_issue_pending(struct dma_chan *chan)
384{
385 struct k3_dma_chan *c = to_k3_chan(chan);
386 struct k3_dma_dev *d = to_k3_dma(chan->device);
387 unsigned long flags;
388
389 spin_lock_irqsave(&c->vc.lock, flags);
390 /* add request to vc->desc_issued */
391 if (vchan_issue_pending(&c->vc)) {
392 spin_lock(&d->lock);
393 if (!c->phy) {
394 if (list_empty(&c->node)) {
395 /* if new channel, add chan_pending */
396 list_add_tail(&c->node, &d->chan_pending);
397 /* check in tasklet */
398 tasklet_schedule(&d->task);
399 dev_dbg(d->slave.dev, "vchan %p: issued\n", &c->vc);
400 }
401 }
402 spin_unlock(&d->lock);
403 } else
404 dev_dbg(d->slave.dev, "vchan %p: nothing to issue\n", &c->vc);
405 spin_unlock_irqrestore(&c->vc.lock, flags);
406}
407
408static void k3_dma_fill_desc(struct k3_dma_desc_sw *ds, dma_addr_t dst,
409 dma_addr_t src, size_t len, u32 num, u32 ccfg)
410{
411 if ((num + 1) < ds->desc_num)
412 ds->desc_hw[num].lli = ds->desc_hw_lli + (num + 1) *
413 sizeof(struct k3_desc_hw);
414 ds->desc_hw[num].lli |= CX_LLI_CHAIN_EN;
415 ds->desc_hw[num].count = len;
416 ds->desc_hw[num].saddr = src;
417 ds->desc_hw[num].daddr = dst;
418 ds->desc_hw[num].config = ccfg;
419}
420
John Stultzb77f2622016-08-29 10:30:50 -0700421static struct k3_dma_desc_sw *k3_dma_alloc_desc_resource(int num,
422 struct dma_chan *chan)
423{
424 struct k3_dma_chan *c = to_k3_chan(chan);
425 struct k3_dma_desc_sw *ds;
426 struct k3_dma_dev *d = to_k3_dma(chan->device);
427 int lli_limit = LLI_BLOCK_SIZE / sizeof(struct k3_desc_hw);
428
429 if (num > lli_limit) {
430 dev_dbg(chan->device->dev, "vch %p: sg num %d exceed max %d\n",
431 &c->vc, num, lli_limit);
432 return NULL;
433 }
434
435 ds = kzalloc(sizeof(*ds), GFP_NOWAIT);
436 if (!ds)
437 return NULL;
438
439 ds->desc_hw = dma_pool_alloc(d->pool, GFP_NOWAIT, &ds->desc_hw_lli);
440 if (!ds->desc_hw) {
441 dev_dbg(chan->device->dev, "vch %p: dma alloc fail\n", &c->vc);
442 kfree(ds);
443 return NULL;
444 }
445 memset(ds->desc_hw, 0, sizeof(struct k3_desc_hw) * num);
446 ds->desc_num = num;
447 return ds;
448}
449
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800450static struct dma_async_tx_descriptor *k3_dma_prep_memcpy(
451 struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
452 size_t len, unsigned long flags)
453{
454 struct k3_dma_chan *c = to_k3_chan(chan);
455 struct k3_dma_desc_sw *ds;
456 size_t copy = 0;
457 int num = 0;
458
459 if (!len)
460 return NULL;
461
462 num = DIV_ROUND_UP(len, DMA_MAX_SIZE);
John Stultzb77f2622016-08-29 10:30:50 -0700463
464 ds = k3_dma_alloc_desc_resource(num, chan);
Peter Griffinaef94fe2016-06-07 18:38:41 +0100465 if (!ds)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800466 return NULL;
Peter Griffinaef94fe2016-06-07 18:38:41 +0100467
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800468 ds->size = len;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800469 num = 0;
470
471 if (!c->ccfg) {
Maxime Riparddb084252014-11-17 14:42:20 +0100472 /* default is memtomem, without calling device_config */
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800473 c->ccfg = CX_CFG_SRCINCR | CX_CFG_DSTINCR | CX_CFG_EN;
474 c->ccfg |= (0xf << 20) | (0xf << 24); /* burst = 16 */
475 c->ccfg |= (0x3 << 12) | (0x3 << 16); /* width = 64 bit */
476 }
477
478 do {
479 copy = min_t(size_t, len, DMA_MAX_SIZE);
480 k3_dma_fill_desc(ds, dst, src, copy, num++, c->ccfg);
481
482 if (c->dir == DMA_MEM_TO_DEV) {
483 src += copy;
484 } else if (c->dir == DMA_DEV_TO_MEM) {
485 dst += copy;
486 } else {
487 src += copy;
488 dst += copy;
489 }
490 len -= copy;
491 } while (len);
492
493 ds->desc_hw[num-1].lli = 0; /* end of link */
494 return vchan_tx_prep(&c->vc, &ds->vd, flags);
495}
496
497static struct dma_async_tx_descriptor *k3_dma_prep_slave_sg(
498 struct dma_chan *chan, struct scatterlist *sgl, unsigned int sglen,
499 enum dma_transfer_direction dir, unsigned long flags, void *context)
500{
501 struct k3_dma_chan *c = to_k3_chan(chan);
502 struct k3_dma_desc_sw *ds;
503 size_t len, avail, total = 0;
504 struct scatterlist *sg;
505 dma_addr_t addr, src = 0, dst = 0;
506 int num = sglen, i;
507
Zhangfei Gaoc61177c2014-01-14 11:37:43 +0800508 if (sgl == NULL)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800509 return NULL;
510
511 for_each_sg(sgl, sg, sglen, i) {
512 avail = sg_dma_len(sg);
513 if (avail > DMA_MAX_SIZE)
514 num += DIV_ROUND_UP(avail, DMA_MAX_SIZE) - 1;
515 }
516
John Stultzb77f2622016-08-29 10:30:50 -0700517 ds = k3_dma_alloc_desc_resource(num, chan);
Peter Griffinaef94fe2016-06-07 18:38:41 +0100518 if (!ds)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800519 return NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800520 num = 0;
521
522 for_each_sg(sgl, sg, sglen, i) {
523 addr = sg_dma_address(sg);
524 avail = sg_dma_len(sg);
525 total += avail;
526
527 do {
528 len = min_t(size_t, avail, DMA_MAX_SIZE);
529
530 if (dir == DMA_MEM_TO_DEV) {
531 src = addr;
532 dst = c->dev_addr;
533 } else if (dir == DMA_DEV_TO_MEM) {
534 src = c->dev_addr;
535 dst = addr;
536 }
537
538 k3_dma_fill_desc(ds, dst, src, len, num++, c->ccfg);
539
540 addr += len;
541 avail -= len;
542 } while (avail);
543 }
544
545 ds->desc_hw[num-1].lli = 0; /* end of link */
546 ds->size = total;
547 return vchan_tx_prep(&c->vc, &ds->vd, flags);
548}
549
Maxime Riparddb084252014-11-17 14:42:20 +0100550static int k3_dma_config(struct dma_chan *chan,
551 struct dma_slave_config *cfg)
552{
553 struct k3_dma_chan *c = to_k3_chan(chan);
554 u32 maxburst = 0, val = 0;
555 enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
556
557 if (cfg == NULL)
558 return -EINVAL;
559 c->dir = cfg->direction;
560 if (c->dir == DMA_DEV_TO_MEM) {
561 c->ccfg = CX_CFG_DSTINCR;
562 c->dev_addr = cfg->src_addr;
563 maxburst = cfg->src_maxburst;
564 width = cfg->src_addr_width;
565 } else if (c->dir == DMA_MEM_TO_DEV) {
566 c->ccfg = CX_CFG_SRCINCR;
567 c->dev_addr = cfg->dst_addr;
568 maxburst = cfg->dst_maxburst;
569 width = cfg->dst_addr_width;
570 }
571 switch (width) {
572 case DMA_SLAVE_BUSWIDTH_1_BYTE:
573 case DMA_SLAVE_BUSWIDTH_2_BYTES:
574 case DMA_SLAVE_BUSWIDTH_4_BYTES:
575 case DMA_SLAVE_BUSWIDTH_8_BYTES:
576 val = __ffs(width);
577 break;
578 default:
579 val = 3;
580 break;
581 }
582 c->ccfg |= (val << 12) | (val << 16);
583
584 if ((maxburst == 0) || (maxburst > 16))
Andy Green6c28a902016-08-29 10:30:47 -0700585 val = 15;
Maxime Riparddb084252014-11-17 14:42:20 +0100586 else
587 val = maxburst - 1;
588 c->ccfg |= (val << 20) | (val << 24);
589 c->ccfg |= CX_CFG_MEM2PER | CX_CFG_EN;
590
591 /* specific request line */
592 c->ccfg |= c->vc.chan.chan_id << 4;
593
594 return 0;
595}
596
597static int k3_dma_terminate_all(struct dma_chan *chan)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800598{
599 struct k3_dma_chan *c = to_k3_chan(chan);
600 struct k3_dma_dev *d = to_k3_dma(chan->device);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800601 struct k3_dma_phy *p = c->phy;
602 unsigned long flags;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800603 LIST_HEAD(head);
604
Maxime Riparddb084252014-11-17 14:42:20 +0100605 dev_dbg(d->slave.dev, "vchan %p: terminate all\n", &c->vc);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800606
Maxime Riparddb084252014-11-17 14:42:20 +0100607 /* Prevent this channel being scheduled */
608 spin_lock(&d->lock);
609 list_del_init(&c->node);
610 spin_unlock(&d->lock);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800611
Maxime Riparddb084252014-11-17 14:42:20 +0100612 /* Clear the tx descriptor lists */
613 spin_lock_irqsave(&c->vc.lock, flags);
614 vchan_get_all_descriptors(&c->vc, &head);
615 if (p) {
616 /* vchan is assigned to a pchan - stop the channel */
617 k3_dma_terminate_chan(p, d);
618 c->phy = NULL;
619 p->vchan = NULL;
620 p->ds_run = p->ds_done = NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800621 }
Maxime Riparddb084252014-11-17 14:42:20 +0100622 spin_unlock_irqrestore(&c->vc.lock, flags);
623 vchan_dma_desc_free_list(&c->vc, &head);
624
625 return 0;
626}
627
Krzysztof Kozlowskia1a9bec2014-12-29 14:01:30 +0100628static int k3_dma_transfer_pause(struct dma_chan *chan)
Maxime Riparddb084252014-11-17 14:42:20 +0100629{
630 struct k3_dma_chan *c = to_k3_chan(chan);
631 struct k3_dma_dev *d = to_k3_dma(chan->device);
632 struct k3_dma_phy *p = c->phy;
633
634 dev_dbg(d->slave.dev, "vchan %p: pause\n", &c->vc);
635 if (c->status == DMA_IN_PROGRESS) {
636 c->status = DMA_PAUSED;
637 if (p) {
638 k3_dma_pause_dma(p, false);
639 } else {
640 spin_lock(&d->lock);
641 list_del_init(&c->node);
642 spin_unlock(&d->lock);
643 }
644 }
645
646 return 0;
647}
648
Krzysztof Kozlowskia1a9bec2014-12-29 14:01:30 +0100649static int k3_dma_transfer_resume(struct dma_chan *chan)
Maxime Riparddb084252014-11-17 14:42:20 +0100650{
651 struct k3_dma_chan *c = to_k3_chan(chan);
652 struct k3_dma_dev *d = to_k3_dma(chan->device);
653 struct k3_dma_phy *p = c->phy;
654 unsigned long flags;
655
656 dev_dbg(d->slave.dev, "vchan %p: resume\n", &c->vc);
657 spin_lock_irqsave(&c->vc.lock, flags);
658 if (c->status == DMA_PAUSED) {
659 c->status = DMA_IN_PROGRESS;
660 if (p) {
661 k3_dma_pause_dma(p, true);
662 } else if (!list_empty(&c->vc.desc_issued)) {
663 spin_lock(&d->lock);
664 list_add_tail(&c->node, &d->chan_pending);
665 spin_unlock(&d->lock);
666 }
667 }
668 spin_unlock_irqrestore(&c->vc.lock, flags);
669
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800670 return 0;
671}
672
673static void k3_dma_free_desc(struct virt_dma_desc *vd)
674{
675 struct k3_dma_desc_sw *ds =
676 container_of(vd, struct k3_dma_desc_sw, vd);
John Stultzb77f2622016-08-29 10:30:50 -0700677 struct k3_dma_dev *d = to_k3_dma(vd->tx.chan->device);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800678
John Stultzb77f2622016-08-29 10:30:50 -0700679 dma_pool_free(d->pool, ds->desc_hw, ds->desc_hw_lli);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800680 kfree(ds);
681}
682
Fabian Frederick57c03422015-03-16 20:17:14 +0100683static const struct of_device_id k3_pdma_dt_ids[] = {
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800684 { .compatible = "hisilicon,k3-dma-1.0", },
685 {}
686};
687MODULE_DEVICE_TABLE(of, k3_pdma_dt_ids);
688
689static struct dma_chan *k3_of_dma_simple_xlate(struct of_phandle_args *dma_spec,
690 struct of_dma *ofdma)
691{
692 struct k3_dma_dev *d = ofdma->of_dma_data;
693 unsigned int request = dma_spec->args[0];
694
695 if (request > d->dma_requests)
696 return NULL;
697
698 return dma_get_slave_channel(&(d->chans[request].vc.chan));
699}
700
701static int k3_dma_probe(struct platform_device *op)
702{
703 struct k3_dma_dev *d;
704 const struct of_device_id *of_id;
705 struct resource *iores;
706 int i, ret, irq = 0;
707
708 iores = platform_get_resource(op, IORESOURCE_MEM, 0);
709 if (!iores)
710 return -EINVAL;
711
712 d = devm_kzalloc(&op->dev, sizeof(*d), GFP_KERNEL);
713 if (!d)
714 return -ENOMEM;
715
Jingoo Hana576b7f2013-09-02 10:25:13 +0900716 d->base = devm_ioremap_resource(&op->dev, iores);
717 if (IS_ERR(d->base))
718 return PTR_ERR(d->base);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800719
720 of_id = of_match_device(k3_pdma_dt_ids, &op->dev);
721 if (of_id) {
722 of_property_read_u32((&op->dev)->of_node,
723 "dma-channels", &d->dma_channels);
724 of_property_read_u32((&op->dev)->of_node,
725 "dma-requests", &d->dma_requests);
726 }
727
728 d->clk = devm_clk_get(&op->dev, NULL);
729 if (IS_ERR(d->clk)) {
730 dev_err(&op->dev, "no dma clk\n");
731 return PTR_ERR(d->clk);
732 }
733
734 irq = platform_get_irq(op, 0);
735 ret = devm_request_irq(&op->dev, irq,
Michael Opdenacker174b5372013-10-13 07:10:51 +0200736 k3_dma_int_handler, 0, DRIVER_NAME, d);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800737 if (ret)
738 return ret;
739
Vinod Koul486b10a2016-07-03 00:02:29 +0530740 d->irq = irq;
741
John Stultzb77f2622016-08-29 10:30:50 -0700742 /* A DMA memory pool for LLIs, align on 32-byte boundary */
743 d->pool = dmam_pool_create(DRIVER_NAME, &op->dev,
744 LLI_BLOCK_SIZE, 32, 0);
745 if (!d->pool)
746 return -ENOMEM;
747
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800748 /* init phy channel */
749 d->phy = devm_kzalloc(&op->dev,
750 d->dma_channels * sizeof(struct k3_dma_phy), GFP_KERNEL);
751 if (d->phy == NULL)
752 return -ENOMEM;
753
754 for (i = 0; i < d->dma_channels; i++) {
755 struct k3_dma_phy *p = &d->phy[i];
756
757 p->idx = i;
758 p->base = d->base + i * 0x40;
759 }
760
761 INIT_LIST_HEAD(&d->slave.channels);
762 dma_cap_set(DMA_SLAVE, d->slave.cap_mask);
763 dma_cap_set(DMA_MEMCPY, d->slave.cap_mask);
764 d->slave.dev = &op->dev;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800765 d->slave.device_free_chan_resources = k3_dma_free_chan_resources;
766 d->slave.device_tx_status = k3_dma_tx_status;
767 d->slave.device_prep_dma_memcpy = k3_dma_prep_memcpy;
768 d->slave.device_prep_slave_sg = k3_dma_prep_slave_sg;
769 d->slave.device_issue_pending = k3_dma_issue_pending;
Maxime Riparddb084252014-11-17 14:42:20 +0100770 d->slave.device_config = k3_dma_config;
Krzysztof Kozlowskia1a9bec2014-12-29 14:01:30 +0100771 d->slave.device_pause = k3_dma_transfer_pause;
772 d->slave.device_resume = k3_dma_transfer_resume;
Maxime Riparddb084252014-11-17 14:42:20 +0100773 d->slave.device_terminate_all = k3_dma_terminate_all;
Maxime Ripard77a68e52015-07-20 10:41:32 +0200774 d->slave.copy_align = DMAENGINE_ALIGN_8_BYTES;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800775
776 /* init virtual channel */
777 d->chans = devm_kzalloc(&op->dev,
778 d->dma_requests * sizeof(struct k3_dma_chan), GFP_KERNEL);
779 if (d->chans == NULL)
780 return -ENOMEM;
781
782 for (i = 0; i < d->dma_requests; i++) {
783 struct k3_dma_chan *c = &d->chans[i];
784
785 c->status = DMA_IN_PROGRESS;
786 INIT_LIST_HEAD(&c->node);
787 c->vc.desc_free = k3_dma_free_desc;
788 vchan_init(&c->vc, &d->slave);
789 }
790
791 /* Enable clock before accessing registers */
792 ret = clk_prepare_enable(d->clk);
793 if (ret < 0) {
794 dev_err(&op->dev, "clk_prepare_enable failed: %d\n", ret);
795 return ret;
796 }
797
798 k3_dma_enable_dma(d, true);
799
800 ret = dma_async_device_register(&d->slave);
801 if (ret)
Wei Yongjun89b90c02016-07-19 11:29:41 +0000802 goto dma_async_register_fail;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800803
804 ret = of_dma_controller_register((&op->dev)->of_node,
805 k3_of_dma_simple_xlate, d);
806 if (ret)
807 goto of_dma_register_fail;
808
809 spin_lock_init(&d->lock);
810 INIT_LIST_HEAD(&d->chan_pending);
811 tasklet_init(&d->task, k3_dma_tasklet, (unsigned long)d);
812 platform_set_drvdata(op, d);
813 dev_info(&op->dev, "initialized\n");
814
815 return 0;
816
817of_dma_register_fail:
818 dma_async_device_unregister(&d->slave);
Wei Yongjun89b90c02016-07-19 11:29:41 +0000819dma_async_register_fail:
820 clk_disable_unprepare(d->clk);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800821 return ret;
822}
823
824static int k3_dma_remove(struct platform_device *op)
825{
826 struct k3_dma_chan *c, *cn;
827 struct k3_dma_dev *d = platform_get_drvdata(op);
828
829 dma_async_device_unregister(&d->slave);
830 of_dma_controller_free((&op->dev)->of_node);
831
Vinod Koul486b10a2016-07-03 00:02:29 +0530832 devm_free_irq(&op->dev, d->irq, d);
833
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800834 list_for_each_entry_safe(c, cn, &d->slave.channels, vc.chan.device_node) {
835 list_del(&c->vc.chan.device_node);
836 tasklet_kill(&c->vc.task);
837 }
838 tasklet_kill(&d->task);
839 clk_disable_unprepare(d->clk);
840 return 0;
841}
842
Jingoo Hanaf2d3132014-10-27 21:36:26 +0900843#ifdef CONFIG_PM_SLEEP
Arnd Bergmann10b3e222015-01-13 14:23:13 +0100844static int k3_dma_suspend_dev(struct device *dev)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800845{
846 struct k3_dma_dev *d = dev_get_drvdata(dev);
847 u32 stat = 0;
848
849 stat = k3_dma_get_chan_stat(d);
850 if (stat) {
851 dev_warn(d->slave.dev,
852 "chan %d is running fail to suspend\n", stat);
853 return -1;
854 }
855 k3_dma_enable_dma(d, false);
856 clk_disable_unprepare(d->clk);
857 return 0;
858}
859
Arnd Bergmann10b3e222015-01-13 14:23:13 +0100860static int k3_dma_resume_dev(struct device *dev)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800861{
862 struct k3_dma_dev *d = dev_get_drvdata(dev);
863 int ret = 0;
864
865 ret = clk_prepare_enable(d->clk);
866 if (ret < 0) {
867 dev_err(d->slave.dev, "clk_prepare_enable failed: %d\n", ret);
868 return ret;
869 }
870 k3_dma_enable_dma(d, true);
871 return 0;
872}
Jingoo Hanaf2d3132014-10-27 21:36:26 +0900873#endif
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800874
Arnd Bergmann10b3e222015-01-13 14:23:13 +0100875static SIMPLE_DEV_PM_OPS(k3_dma_pmops, k3_dma_suspend_dev, k3_dma_resume_dev);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800876
877static struct platform_driver k3_pdma_driver = {
878 .driver = {
879 .name = DRIVER_NAME,
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800880 .pm = &k3_dma_pmops,
881 .of_match_table = k3_pdma_dt_ids,
882 },
883 .probe = k3_dma_probe,
884 .remove = k3_dma_remove,
885};
886
887module_platform_driver(k3_pdma_driver);
888
889MODULE_DESCRIPTION("Hisilicon k3 DMA Driver");
890MODULE_ALIAS("platform:k3dma");
891MODULE_LICENSE("GPL v2");