blob: 8108fa119d3987afd312b1fe401a4930341a3483 [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);
John Stultz36387a22016-08-29 10:30:51 -0700215 WARN_ON_ONCE(p->ds_done);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800216 p->ds_done = p->ds_run;
John Stultz36387a22016-08-29 10:30:51 -0700217 p->ds_run = NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800218 spin_unlock_irqrestore(&c->vc.lock, flags);
219 }
220 irq_chan |= BIT(i);
221 }
222 if (unlikely((err1 & BIT(i)) || (err2 & BIT(i))))
223 dev_warn(d->slave.dev, "DMA ERR\n");
224 }
225
226 writel_relaxed(irq_chan, d->base + INT_TC1_RAW);
227 writel_relaxed(err1, d->base + INT_ERR1_RAW);
228 writel_relaxed(err2, d->base + INT_ERR2_RAW);
229
Andy Green0173c892016-08-29 10:30:49 -0700230 if (irq_chan)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800231 tasklet_schedule(&d->task);
Andy Green0173c892016-08-29 10:30:49 -0700232
233 if (irq_chan || err1 || err2)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800234 return IRQ_HANDLED;
Andy Green0173c892016-08-29 10:30:49 -0700235
236 return IRQ_NONE;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800237}
238
239static int k3_dma_start_txd(struct k3_dma_chan *c)
240{
241 struct k3_dma_dev *d = to_k3_dma(c->vc.chan.device);
242 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
243
244 if (!c->phy)
245 return -EAGAIN;
246
247 if (BIT(c->phy->idx) & k3_dma_get_chan_stat(d))
248 return -EAGAIN;
249
250 if (vd) {
251 struct k3_dma_desc_sw *ds =
252 container_of(vd, struct k3_dma_desc_sw, vd);
253 /*
254 * fetch and remove request from vc->desc_issued
255 * so vc->desc_issued only contains desc pending
256 */
257 list_del(&ds->vd.node);
John Stultz36387a22016-08-29 10:30:51 -0700258
259 WARN_ON_ONCE(c->phy->ds_run);
260 WARN_ON_ONCE(c->phy->ds_done);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800261 c->phy->ds_run = ds;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800262 /* start dma */
263 k3_dma_set_desc(c->phy, &ds->desc_hw[0]);
264 return 0;
265 }
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800266 return -EAGAIN;
267}
268
269static void k3_dma_tasklet(unsigned long arg)
270{
271 struct k3_dma_dev *d = (struct k3_dma_dev *)arg;
272 struct k3_dma_phy *p;
273 struct k3_dma_chan *c, *cn;
274 unsigned pch, pch_alloc = 0;
275
276 /* check new dma request of running channel in vc->desc_issued */
277 list_for_each_entry_safe(c, cn, &d->slave.channels, vc.chan.device_node) {
278 spin_lock_irq(&c->vc.lock);
279 p = c->phy;
280 if (p && p->ds_done) {
281 if (k3_dma_start_txd(c)) {
282 /* No current txd associated with this channel */
283 dev_dbg(d->slave.dev, "pchan %u: free\n", p->idx);
284 /* Mark this channel free */
285 c->phy = NULL;
286 p->vchan = NULL;
287 }
288 }
289 spin_unlock_irq(&c->vc.lock);
290 }
291
292 /* check new channel request in d->chan_pending */
293 spin_lock_irq(&d->lock);
294 for (pch = 0; pch < d->dma_channels; pch++) {
295 p = &d->phy[pch];
296
297 if (p->vchan == NULL && !list_empty(&d->chan_pending)) {
298 c = list_first_entry(&d->chan_pending,
299 struct k3_dma_chan, node);
300 /* remove from d->chan_pending */
301 list_del_init(&c->node);
302 pch_alloc |= 1 << pch;
303 /* Mark this channel allocated */
304 p->vchan = c;
305 c->phy = p;
306 dev_dbg(d->slave.dev, "pchan %u: alloc vchan %p\n", pch, &c->vc);
307 }
308 }
309 spin_unlock_irq(&d->lock);
310
311 for (pch = 0; pch < d->dma_channels; pch++) {
312 if (pch_alloc & (1 << pch)) {
313 p = &d->phy[pch];
314 c = p->vchan;
315 if (c) {
316 spin_lock_irq(&c->vc.lock);
317 k3_dma_start_txd(c);
318 spin_unlock_irq(&c->vc.lock);
319 }
320 }
321 }
322}
323
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800324static void k3_dma_free_chan_resources(struct dma_chan *chan)
325{
326 struct k3_dma_chan *c = to_k3_chan(chan);
327 struct k3_dma_dev *d = to_k3_dma(chan->device);
328 unsigned long flags;
329
330 spin_lock_irqsave(&d->lock, flags);
331 list_del_init(&c->node);
332 spin_unlock_irqrestore(&d->lock, flags);
333
334 vchan_free_chan_resources(&c->vc);
335 c->ccfg = 0;
336}
337
338static enum dma_status k3_dma_tx_status(struct dma_chan *chan,
339 dma_cookie_t cookie, struct dma_tx_state *state)
340{
341 struct k3_dma_chan *c = to_k3_chan(chan);
342 struct k3_dma_dev *d = to_k3_dma(chan->device);
343 struct k3_dma_phy *p;
344 struct virt_dma_desc *vd;
345 unsigned long flags;
346 enum dma_status ret;
347 size_t bytes = 0;
348
349 ret = dma_cookie_status(&c->vc.chan, cookie, state);
Vinod Koulbd2c3482013-10-16 20:50:09 +0530350 if (ret == DMA_COMPLETE)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800351 return ret;
352
353 spin_lock_irqsave(&c->vc.lock, flags);
354 p = c->phy;
355 ret = c->status;
356
357 /*
358 * If the cookie is on our issue queue, then the residue is
359 * its total size.
360 */
361 vd = vchan_find_desc(&c->vc, cookie);
362 if (vd) {
363 bytes = container_of(vd, struct k3_dma_desc_sw, vd)->size;
364 } else if ((!p) || (!p->ds_run)) {
365 bytes = 0;
366 } else {
367 struct k3_dma_desc_sw *ds = p->ds_run;
368 u32 clli = 0, index = 0;
369
370 bytes = k3_dma_get_curr_cnt(d, p);
371 clli = k3_dma_get_curr_lli(p);
372 index = (clli - ds->desc_hw_lli) / sizeof(struct k3_desc_hw);
373 for (; index < ds->desc_num; index++) {
374 bytes += ds->desc_hw[index].count;
375 /* end of lli */
376 if (!ds->desc_hw[index].lli)
377 break;
378 }
379 }
380 spin_unlock_irqrestore(&c->vc.lock, flags);
381 dma_set_residue(state, bytes);
382 return ret;
383}
384
385static void k3_dma_issue_pending(struct dma_chan *chan)
386{
387 struct k3_dma_chan *c = to_k3_chan(chan);
388 struct k3_dma_dev *d = to_k3_dma(chan->device);
389 unsigned long flags;
390
391 spin_lock_irqsave(&c->vc.lock, flags);
392 /* add request to vc->desc_issued */
393 if (vchan_issue_pending(&c->vc)) {
394 spin_lock(&d->lock);
395 if (!c->phy) {
396 if (list_empty(&c->node)) {
397 /* if new channel, add chan_pending */
398 list_add_tail(&c->node, &d->chan_pending);
399 /* check in tasklet */
400 tasklet_schedule(&d->task);
401 dev_dbg(d->slave.dev, "vchan %p: issued\n", &c->vc);
402 }
403 }
404 spin_unlock(&d->lock);
405 } else
406 dev_dbg(d->slave.dev, "vchan %p: nothing to issue\n", &c->vc);
407 spin_unlock_irqrestore(&c->vc.lock, flags);
408}
409
410static void k3_dma_fill_desc(struct k3_dma_desc_sw *ds, dma_addr_t dst,
411 dma_addr_t src, size_t len, u32 num, u32 ccfg)
412{
413 if ((num + 1) < ds->desc_num)
414 ds->desc_hw[num].lli = ds->desc_hw_lli + (num + 1) *
415 sizeof(struct k3_desc_hw);
416 ds->desc_hw[num].lli |= CX_LLI_CHAIN_EN;
417 ds->desc_hw[num].count = len;
418 ds->desc_hw[num].saddr = src;
419 ds->desc_hw[num].daddr = dst;
420 ds->desc_hw[num].config = ccfg;
421}
422
John Stultzb77f2622016-08-29 10:30:50 -0700423static struct k3_dma_desc_sw *k3_dma_alloc_desc_resource(int num,
424 struct dma_chan *chan)
425{
426 struct k3_dma_chan *c = to_k3_chan(chan);
427 struct k3_dma_desc_sw *ds;
428 struct k3_dma_dev *d = to_k3_dma(chan->device);
429 int lli_limit = LLI_BLOCK_SIZE / sizeof(struct k3_desc_hw);
430
431 if (num > lli_limit) {
432 dev_dbg(chan->device->dev, "vch %p: sg num %d exceed max %d\n",
433 &c->vc, num, lli_limit);
434 return NULL;
435 }
436
437 ds = kzalloc(sizeof(*ds), GFP_NOWAIT);
438 if (!ds)
439 return NULL;
440
441 ds->desc_hw = dma_pool_alloc(d->pool, GFP_NOWAIT, &ds->desc_hw_lli);
442 if (!ds->desc_hw) {
443 dev_dbg(chan->device->dev, "vch %p: dma alloc fail\n", &c->vc);
444 kfree(ds);
445 return NULL;
446 }
447 memset(ds->desc_hw, 0, sizeof(struct k3_desc_hw) * num);
448 ds->desc_num = num;
449 return ds;
450}
451
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800452static struct dma_async_tx_descriptor *k3_dma_prep_memcpy(
453 struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
454 size_t len, unsigned long flags)
455{
456 struct k3_dma_chan *c = to_k3_chan(chan);
457 struct k3_dma_desc_sw *ds;
458 size_t copy = 0;
459 int num = 0;
460
461 if (!len)
462 return NULL;
463
464 num = DIV_ROUND_UP(len, DMA_MAX_SIZE);
John Stultzb77f2622016-08-29 10:30:50 -0700465
466 ds = k3_dma_alloc_desc_resource(num, chan);
Peter Griffinaef94fe2016-06-07 18:38:41 +0100467 if (!ds)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800468 return NULL;
Peter Griffinaef94fe2016-06-07 18:38:41 +0100469
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800470 ds->size = len;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800471 num = 0;
472
473 if (!c->ccfg) {
Maxime Riparddb084252014-11-17 14:42:20 +0100474 /* default is memtomem, without calling device_config */
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800475 c->ccfg = CX_CFG_SRCINCR | CX_CFG_DSTINCR | CX_CFG_EN;
476 c->ccfg |= (0xf << 20) | (0xf << 24); /* burst = 16 */
477 c->ccfg |= (0x3 << 12) | (0x3 << 16); /* width = 64 bit */
478 }
479
480 do {
481 copy = min_t(size_t, len, DMA_MAX_SIZE);
482 k3_dma_fill_desc(ds, dst, src, copy, num++, c->ccfg);
483
484 if (c->dir == DMA_MEM_TO_DEV) {
485 src += copy;
486 } else if (c->dir == DMA_DEV_TO_MEM) {
487 dst += copy;
488 } else {
489 src += copy;
490 dst += copy;
491 }
492 len -= copy;
493 } while (len);
494
495 ds->desc_hw[num-1].lli = 0; /* end of link */
496 return vchan_tx_prep(&c->vc, &ds->vd, flags);
497}
498
499static struct dma_async_tx_descriptor *k3_dma_prep_slave_sg(
500 struct dma_chan *chan, struct scatterlist *sgl, unsigned int sglen,
501 enum dma_transfer_direction dir, unsigned long flags, void *context)
502{
503 struct k3_dma_chan *c = to_k3_chan(chan);
504 struct k3_dma_desc_sw *ds;
505 size_t len, avail, total = 0;
506 struct scatterlist *sg;
507 dma_addr_t addr, src = 0, dst = 0;
508 int num = sglen, i;
509
Zhangfei Gaoc61177c2014-01-14 11:37:43 +0800510 if (sgl == NULL)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800511 return NULL;
512
513 for_each_sg(sgl, sg, sglen, i) {
514 avail = sg_dma_len(sg);
515 if (avail > DMA_MAX_SIZE)
516 num += DIV_ROUND_UP(avail, DMA_MAX_SIZE) - 1;
517 }
518
John Stultzb77f2622016-08-29 10:30:50 -0700519 ds = k3_dma_alloc_desc_resource(num, chan);
Peter Griffinaef94fe2016-06-07 18:38:41 +0100520 if (!ds)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800521 return NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800522 num = 0;
523
524 for_each_sg(sgl, sg, sglen, i) {
525 addr = sg_dma_address(sg);
526 avail = sg_dma_len(sg);
527 total += avail;
528
529 do {
530 len = min_t(size_t, avail, DMA_MAX_SIZE);
531
532 if (dir == DMA_MEM_TO_DEV) {
533 src = addr;
534 dst = c->dev_addr;
535 } else if (dir == DMA_DEV_TO_MEM) {
536 src = c->dev_addr;
537 dst = addr;
538 }
539
540 k3_dma_fill_desc(ds, dst, src, len, num++, c->ccfg);
541
542 addr += len;
543 avail -= len;
544 } while (avail);
545 }
546
547 ds->desc_hw[num-1].lli = 0; /* end of link */
548 ds->size = total;
549 return vchan_tx_prep(&c->vc, &ds->vd, flags);
550}
551
Maxime Riparddb084252014-11-17 14:42:20 +0100552static int k3_dma_config(struct dma_chan *chan,
553 struct dma_slave_config *cfg)
554{
555 struct k3_dma_chan *c = to_k3_chan(chan);
556 u32 maxburst = 0, val = 0;
557 enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
558
559 if (cfg == NULL)
560 return -EINVAL;
561 c->dir = cfg->direction;
562 if (c->dir == DMA_DEV_TO_MEM) {
563 c->ccfg = CX_CFG_DSTINCR;
564 c->dev_addr = cfg->src_addr;
565 maxburst = cfg->src_maxburst;
566 width = cfg->src_addr_width;
567 } else if (c->dir == DMA_MEM_TO_DEV) {
568 c->ccfg = CX_CFG_SRCINCR;
569 c->dev_addr = cfg->dst_addr;
570 maxburst = cfg->dst_maxburst;
571 width = cfg->dst_addr_width;
572 }
573 switch (width) {
574 case DMA_SLAVE_BUSWIDTH_1_BYTE:
575 case DMA_SLAVE_BUSWIDTH_2_BYTES:
576 case DMA_SLAVE_BUSWIDTH_4_BYTES:
577 case DMA_SLAVE_BUSWIDTH_8_BYTES:
578 val = __ffs(width);
579 break;
580 default:
581 val = 3;
582 break;
583 }
584 c->ccfg |= (val << 12) | (val << 16);
585
586 if ((maxburst == 0) || (maxburst > 16))
Andy Green6c28a902016-08-29 10:30:47 -0700587 val = 15;
Maxime Riparddb084252014-11-17 14:42:20 +0100588 else
589 val = maxburst - 1;
590 c->ccfg |= (val << 20) | (val << 24);
591 c->ccfg |= CX_CFG_MEM2PER | CX_CFG_EN;
592
593 /* specific request line */
594 c->ccfg |= c->vc.chan.chan_id << 4;
595
596 return 0;
597}
598
John Stultz36387a22016-08-29 10:30:51 -0700599static void k3_dma_free_desc(struct virt_dma_desc *vd)
600{
601 struct k3_dma_desc_sw *ds =
602 container_of(vd, struct k3_dma_desc_sw, vd);
603 struct k3_dma_dev *d = to_k3_dma(vd->tx.chan->device);
604
605 dma_pool_free(d->pool, ds->desc_hw, ds->desc_hw_lli);
606 kfree(ds);
607}
608
Maxime Riparddb084252014-11-17 14:42:20 +0100609static int k3_dma_terminate_all(struct dma_chan *chan)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800610{
611 struct k3_dma_chan *c = to_k3_chan(chan);
612 struct k3_dma_dev *d = to_k3_dma(chan->device);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800613 struct k3_dma_phy *p = c->phy;
614 unsigned long flags;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800615 LIST_HEAD(head);
616
Maxime Riparddb084252014-11-17 14:42:20 +0100617 dev_dbg(d->slave.dev, "vchan %p: terminate all\n", &c->vc);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800618
Maxime Riparddb084252014-11-17 14:42:20 +0100619 /* Prevent this channel being scheduled */
620 spin_lock(&d->lock);
621 list_del_init(&c->node);
622 spin_unlock(&d->lock);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800623
Maxime Riparddb084252014-11-17 14:42:20 +0100624 /* Clear the tx descriptor lists */
625 spin_lock_irqsave(&c->vc.lock, flags);
626 vchan_get_all_descriptors(&c->vc, &head);
627 if (p) {
628 /* vchan is assigned to a pchan - stop the channel */
629 k3_dma_terminate_chan(p, d);
630 c->phy = NULL;
631 p->vchan = NULL;
John Stultz36387a22016-08-29 10:30:51 -0700632 if (p->ds_run) {
633 k3_dma_free_desc(&p->ds_run->vd);
634 p->ds_run = NULL;
635 }
636 if (p->ds_done) {
637 k3_dma_free_desc(&p->ds_done->vd);
638 p->ds_done = NULL;
639 }
640
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800641 }
Maxime Riparddb084252014-11-17 14:42:20 +0100642 spin_unlock_irqrestore(&c->vc.lock, flags);
643 vchan_dma_desc_free_list(&c->vc, &head);
644
645 return 0;
646}
647
Krzysztof Kozlowskia1a9bec2014-12-29 14:01:30 +0100648static int k3_dma_transfer_pause(struct dma_chan *chan)
Maxime Riparddb084252014-11-17 14:42:20 +0100649{
650 struct k3_dma_chan *c = to_k3_chan(chan);
651 struct k3_dma_dev *d = to_k3_dma(chan->device);
652 struct k3_dma_phy *p = c->phy;
653
654 dev_dbg(d->slave.dev, "vchan %p: pause\n", &c->vc);
655 if (c->status == DMA_IN_PROGRESS) {
656 c->status = DMA_PAUSED;
657 if (p) {
658 k3_dma_pause_dma(p, false);
659 } else {
660 spin_lock(&d->lock);
661 list_del_init(&c->node);
662 spin_unlock(&d->lock);
663 }
664 }
665
666 return 0;
667}
668
Krzysztof Kozlowskia1a9bec2014-12-29 14:01:30 +0100669static int k3_dma_transfer_resume(struct dma_chan *chan)
Maxime Riparddb084252014-11-17 14:42:20 +0100670{
671 struct k3_dma_chan *c = to_k3_chan(chan);
672 struct k3_dma_dev *d = to_k3_dma(chan->device);
673 struct k3_dma_phy *p = c->phy;
674 unsigned long flags;
675
676 dev_dbg(d->slave.dev, "vchan %p: resume\n", &c->vc);
677 spin_lock_irqsave(&c->vc.lock, flags);
678 if (c->status == DMA_PAUSED) {
679 c->status = DMA_IN_PROGRESS;
680 if (p) {
681 k3_dma_pause_dma(p, true);
682 } else if (!list_empty(&c->vc.desc_issued)) {
683 spin_lock(&d->lock);
684 list_add_tail(&c->node, &d->chan_pending);
685 spin_unlock(&d->lock);
686 }
687 }
688 spin_unlock_irqrestore(&c->vc.lock, flags);
689
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800690 return 0;
691}
692
Fabian Frederick57c03422015-03-16 20:17:14 +0100693static const struct of_device_id k3_pdma_dt_ids[] = {
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800694 { .compatible = "hisilicon,k3-dma-1.0", },
695 {}
696};
697MODULE_DEVICE_TABLE(of, k3_pdma_dt_ids);
698
699static struct dma_chan *k3_of_dma_simple_xlate(struct of_phandle_args *dma_spec,
700 struct of_dma *ofdma)
701{
702 struct k3_dma_dev *d = ofdma->of_dma_data;
703 unsigned int request = dma_spec->args[0];
704
705 if (request > d->dma_requests)
706 return NULL;
707
708 return dma_get_slave_channel(&(d->chans[request].vc.chan));
709}
710
711static int k3_dma_probe(struct platform_device *op)
712{
713 struct k3_dma_dev *d;
714 const struct of_device_id *of_id;
715 struct resource *iores;
716 int i, ret, irq = 0;
717
718 iores = platform_get_resource(op, IORESOURCE_MEM, 0);
719 if (!iores)
720 return -EINVAL;
721
722 d = devm_kzalloc(&op->dev, sizeof(*d), GFP_KERNEL);
723 if (!d)
724 return -ENOMEM;
725
Jingoo Hana576b7f2013-09-02 10:25:13 +0900726 d->base = devm_ioremap_resource(&op->dev, iores);
727 if (IS_ERR(d->base))
728 return PTR_ERR(d->base);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800729
730 of_id = of_match_device(k3_pdma_dt_ids, &op->dev);
731 if (of_id) {
732 of_property_read_u32((&op->dev)->of_node,
733 "dma-channels", &d->dma_channels);
734 of_property_read_u32((&op->dev)->of_node,
735 "dma-requests", &d->dma_requests);
736 }
737
738 d->clk = devm_clk_get(&op->dev, NULL);
739 if (IS_ERR(d->clk)) {
740 dev_err(&op->dev, "no dma clk\n");
741 return PTR_ERR(d->clk);
742 }
743
744 irq = platform_get_irq(op, 0);
745 ret = devm_request_irq(&op->dev, irq,
Michael Opdenacker174b5372013-10-13 07:10:51 +0200746 k3_dma_int_handler, 0, DRIVER_NAME, d);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800747 if (ret)
748 return ret;
749
Vinod Koul486b10a2016-07-03 00:02:29 +0530750 d->irq = irq;
751
John Stultzb77f2622016-08-29 10:30:50 -0700752 /* A DMA memory pool for LLIs, align on 32-byte boundary */
753 d->pool = dmam_pool_create(DRIVER_NAME, &op->dev,
754 LLI_BLOCK_SIZE, 32, 0);
755 if (!d->pool)
756 return -ENOMEM;
757
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800758 /* init phy channel */
759 d->phy = devm_kzalloc(&op->dev,
760 d->dma_channels * sizeof(struct k3_dma_phy), GFP_KERNEL);
761 if (d->phy == NULL)
762 return -ENOMEM;
763
764 for (i = 0; i < d->dma_channels; i++) {
765 struct k3_dma_phy *p = &d->phy[i];
766
767 p->idx = i;
768 p->base = d->base + i * 0x40;
769 }
770
771 INIT_LIST_HEAD(&d->slave.channels);
772 dma_cap_set(DMA_SLAVE, d->slave.cap_mask);
773 dma_cap_set(DMA_MEMCPY, d->slave.cap_mask);
774 d->slave.dev = &op->dev;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800775 d->slave.device_free_chan_resources = k3_dma_free_chan_resources;
776 d->slave.device_tx_status = k3_dma_tx_status;
777 d->slave.device_prep_dma_memcpy = k3_dma_prep_memcpy;
778 d->slave.device_prep_slave_sg = k3_dma_prep_slave_sg;
779 d->slave.device_issue_pending = k3_dma_issue_pending;
Maxime Riparddb084252014-11-17 14:42:20 +0100780 d->slave.device_config = k3_dma_config;
Krzysztof Kozlowskia1a9bec2014-12-29 14:01:30 +0100781 d->slave.device_pause = k3_dma_transfer_pause;
782 d->slave.device_resume = k3_dma_transfer_resume;
Maxime Riparddb084252014-11-17 14:42:20 +0100783 d->slave.device_terminate_all = k3_dma_terminate_all;
Maxime Ripard77a68e52015-07-20 10:41:32 +0200784 d->slave.copy_align = DMAENGINE_ALIGN_8_BYTES;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800785
786 /* init virtual channel */
787 d->chans = devm_kzalloc(&op->dev,
788 d->dma_requests * sizeof(struct k3_dma_chan), GFP_KERNEL);
789 if (d->chans == NULL)
790 return -ENOMEM;
791
792 for (i = 0; i < d->dma_requests; i++) {
793 struct k3_dma_chan *c = &d->chans[i];
794
795 c->status = DMA_IN_PROGRESS;
796 INIT_LIST_HEAD(&c->node);
797 c->vc.desc_free = k3_dma_free_desc;
798 vchan_init(&c->vc, &d->slave);
799 }
800
801 /* Enable clock before accessing registers */
802 ret = clk_prepare_enable(d->clk);
803 if (ret < 0) {
804 dev_err(&op->dev, "clk_prepare_enable failed: %d\n", ret);
805 return ret;
806 }
807
808 k3_dma_enable_dma(d, true);
809
810 ret = dma_async_device_register(&d->slave);
811 if (ret)
Wei Yongjun89b90c02016-07-19 11:29:41 +0000812 goto dma_async_register_fail;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800813
814 ret = of_dma_controller_register((&op->dev)->of_node,
815 k3_of_dma_simple_xlate, d);
816 if (ret)
817 goto of_dma_register_fail;
818
819 spin_lock_init(&d->lock);
820 INIT_LIST_HEAD(&d->chan_pending);
821 tasklet_init(&d->task, k3_dma_tasklet, (unsigned long)d);
822 platform_set_drvdata(op, d);
823 dev_info(&op->dev, "initialized\n");
824
825 return 0;
826
827of_dma_register_fail:
828 dma_async_device_unregister(&d->slave);
Wei Yongjun89b90c02016-07-19 11:29:41 +0000829dma_async_register_fail:
830 clk_disable_unprepare(d->clk);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800831 return ret;
832}
833
834static int k3_dma_remove(struct platform_device *op)
835{
836 struct k3_dma_chan *c, *cn;
837 struct k3_dma_dev *d = platform_get_drvdata(op);
838
839 dma_async_device_unregister(&d->slave);
840 of_dma_controller_free((&op->dev)->of_node);
841
Vinod Koul486b10a2016-07-03 00:02:29 +0530842 devm_free_irq(&op->dev, d->irq, d);
843
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800844 list_for_each_entry_safe(c, cn, &d->slave.channels, vc.chan.device_node) {
845 list_del(&c->vc.chan.device_node);
846 tasklet_kill(&c->vc.task);
847 }
848 tasklet_kill(&d->task);
849 clk_disable_unprepare(d->clk);
850 return 0;
851}
852
Jingoo Hanaf2d3132014-10-27 21:36:26 +0900853#ifdef CONFIG_PM_SLEEP
Arnd Bergmann10b3e222015-01-13 14:23:13 +0100854static int k3_dma_suspend_dev(struct device *dev)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800855{
856 struct k3_dma_dev *d = dev_get_drvdata(dev);
857 u32 stat = 0;
858
859 stat = k3_dma_get_chan_stat(d);
860 if (stat) {
861 dev_warn(d->slave.dev,
862 "chan %d is running fail to suspend\n", stat);
863 return -1;
864 }
865 k3_dma_enable_dma(d, false);
866 clk_disable_unprepare(d->clk);
867 return 0;
868}
869
Arnd Bergmann10b3e222015-01-13 14:23:13 +0100870static int k3_dma_resume_dev(struct device *dev)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800871{
872 struct k3_dma_dev *d = dev_get_drvdata(dev);
873 int ret = 0;
874
875 ret = clk_prepare_enable(d->clk);
876 if (ret < 0) {
877 dev_err(d->slave.dev, "clk_prepare_enable failed: %d\n", ret);
878 return ret;
879 }
880 k3_dma_enable_dma(d, true);
881 return 0;
882}
Jingoo Hanaf2d3132014-10-27 21:36:26 +0900883#endif
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800884
Arnd Bergmann10b3e222015-01-13 14:23:13 +0100885static SIMPLE_DEV_PM_OPS(k3_dma_pmops, k3_dma_suspend_dev, k3_dma_resume_dev);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800886
887static struct platform_driver k3_pdma_driver = {
888 .driver = {
889 .name = DRIVER_NAME,
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800890 .pm = &k3_dma_pmops,
891 .of_match_table = k3_pdma_dt_ids,
892 },
893 .probe = k3_dma_probe,
894 .remove = k3_dma_remove,
895};
896
897module_platform_driver(k3_pdma_driver);
898
899MODULE_DESCRIPTION("Hisilicon k3 DMA Driver");
900MODULE_ALIAS("platform:k3dma");
901MODULE_LICENSE("GPL v2");