blob: 964dcb46755b54d00fc09bdb33ab6fdf0363bc59 [file] [log] [blame]
Zhangfei Gao8e6152b2013-08-27 10:20:10 +08001/*
Andy Greena7e08fa2016-08-29 10:30:52 -07002 * Copyright (c) 2013 - 2015 Linaro Ltd.
Zhangfei Gao8e6152b2013-08-27 10:20:10 +08003 * 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
Andy Greena7e08fa2016-08-29 10:30:52 -070030#define DMA_CYCLIC_MAX_PERIOD 0x1000
John Stultzb77f2622016-08-29 10:30:50 -070031#define LLI_BLOCK_SIZE (4 * PAGE_SIZE)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080032
33#define INT_STAT 0x00
34#define INT_TC1 0x04
Andy Greena7e08fa2016-08-29 10:30:52 -070035#define INT_TC2 0x08
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080036#define INT_ERR1 0x0c
37#define INT_ERR2 0x10
38#define INT_TC1_MASK 0x18
Andy Greena7e08fa2016-08-29 10:30:52 -070039#define INT_TC2_MASK 0x1c
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080040#define INT_ERR1_MASK 0x20
41#define INT_ERR2_MASK 0x24
42#define INT_TC1_RAW 0x600
Andy Greena7e08fa2016-08-29 10:30:52 -070043#define INT_TC2_RAW 0x608
Andy Greenaceaaa12016-08-29 10:30:48 -070044#define INT_ERR1_RAW 0x610
45#define INT_ERR2_RAW 0x618
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080046#define CH_PRI 0x688
47#define CH_STAT 0x690
48#define CX_CUR_CNT 0x704
49#define CX_LLI 0x800
Andy Greena7e08fa2016-08-29 10:30:52 -070050#define CX_CNT1 0x80c
51#define CX_CNT0 0x810
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080052#define CX_SRC 0x814
53#define CX_DST 0x818
54#define CX_CFG 0x81c
55#define AXI_CFG 0x820
56#define AXI_CFG_DEFAULT 0x201201
57
58#define CX_LLI_CHAIN_EN 0x2
59#define CX_CFG_EN 0x1
Andy Greena7e08fa2016-08-29 10:30:52 -070060#define CX_CFG_NODEIRQ BIT(1)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080061#define CX_CFG_MEM2PER (0x1 << 2)
62#define CX_CFG_PER2MEM (0x2 << 2)
63#define CX_CFG_SRCINCR (0x1 << 31)
64#define CX_CFG_DSTINCR (0x1 << 30)
65
66struct k3_desc_hw {
67 u32 lli;
68 u32 reserved[3];
69 u32 count;
70 u32 saddr;
71 u32 daddr;
72 u32 config;
73} __aligned(32);
74
75struct k3_dma_desc_sw {
76 struct virt_dma_desc vd;
77 dma_addr_t desc_hw_lli;
78 size_t desc_num;
79 size_t size;
John Stultzb77f2622016-08-29 10:30:50 -070080 struct k3_desc_hw *desc_hw;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080081};
82
83struct k3_dma_phy;
84
85struct k3_dma_chan {
86 u32 ccfg;
87 struct virt_dma_chan vc;
88 struct k3_dma_phy *phy;
89 struct list_head node;
90 enum dma_transfer_direction dir;
91 dma_addr_t dev_addr;
92 enum dma_status status;
Andy Greena7e08fa2016-08-29 10:30:52 -070093 bool cyclic;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +080094};
95
96struct k3_dma_phy {
97 u32 idx;
98 void __iomem *base;
99 struct k3_dma_chan *vchan;
100 struct k3_dma_desc_sw *ds_run;
101 struct k3_dma_desc_sw *ds_done;
102};
103
104struct k3_dma_dev {
105 struct dma_device slave;
106 void __iomem *base;
107 struct tasklet_struct task;
108 spinlock_t lock;
109 struct list_head chan_pending;
110 struct k3_dma_phy *phy;
111 struct k3_dma_chan *chans;
112 struct clk *clk;
John Stultzb77f2622016-08-29 10:30:50 -0700113 struct dma_pool *pool;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800114 u32 dma_channels;
115 u32 dma_requests;
Vinod Koul486b10a2016-07-03 00:02:29 +0530116 unsigned int irq;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800117};
118
119#define to_k3_dma(dmadev) container_of(dmadev, struct k3_dma_dev, slave)
120
121static struct k3_dma_chan *to_k3_chan(struct dma_chan *chan)
122{
123 return container_of(chan, struct k3_dma_chan, vc.chan);
124}
125
126static void k3_dma_pause_dma(struct k3_dma_phy *phy, bool on)
127{
128 u32 val = 0;
129
130 if (on) {
131 val = readl_relaxed(phy->base + CX_CFG);
132 val |= CX_CFG_EN;
133 writel_relaxed(val, phy->base + CX_CFG);
134 } else {
135 val = readl_relaxed(phy->base + CX_CFG);
136 val &= ~CX_CFG_EN;
137 writel_relaxed(val, phy->base + CX_CFG);
138 }
139}
140
141static void k3_dma_terminate_chan(struct k3_dma_phy *phy, struct k3_dma_dev *d)
142{
143 u32 val = 0;
144
145 k3_dma_pause_dma(phy, false);
146
147 val = 0x1 << phy->idx;
148 writel_relaxed(val, d->base + INT_TC1_RAW);
Andy Greena7e08fa2016-08-29 10:30:52 -0700149 writel_relaxed(val, d->base + INT_TC2_RAW);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800150 writel_relaxed(val, d->base + INT_ERR1_RAW);
151 writel_relaxed(val, d->base + INT_ERR2_RAW);
152}
153
154static void k3_dma_set_desc(struct k3_dma_phy *phy, struct k3_desc_hw *hw)
155{
156 writel_relaxed(hw->lli, phy->base + CX_LLI);
Andy Greena7e08fa2016-08-29 10:30:52 -0700157 writel_relaxed(hw->count, phy->base + CX_CNT0);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800158 writel_relaxed(hw->saddr, phy->base + CX_SRC);
159 writel_relaxed(hw->daddr, phy->base + CX_DST);
160 writel_relaxed(AXI_CFG_DEFAULT, phy->base + AXI_CFG);
161 writel_relaxed(hw->config, phy->base + CX_CFG);
162}
163
164static u32 k3_dma_get_curr_cnt(struct k3_dma_dev *d, struct k3_dma_phy *phy)
165{
166 u32 cnt = 0;
167
168 cnt = readl_relaxed(d->base + CX_CUR_CNT + phy->idx * 0x10);
169 cnt &= 0xffff;
170 return cnt;
171}
172
173static u32 k3_dma_get_curr_lli(struct k3_dma_phy *phy)
174{
175 return readl_relaxed(phy->base + CX_LLI);
176}
177
178static u32 k3_dma_get_chan_stat(struct k3_dma_dev *d)
179{
180 return readl_relaxed(d->base + CH_STAT);
181}
182
183static void k3_dma_enable_dma(struct k3_dma_dev *d, bool on)
184{
185 if (on) {
186 /* set same priority */
187 writel_relaxed(0x0, d->base + CH_PRI);
188
189 /* unmask irq */
190 writel_relaxed(0xffff, d->base + INT_TC1_MASK);
Andy Greena7e08fa2016-08-29 10:30:52 -0700191 writel_relaxed(0xffff, d->base + INT_TC2_MASK);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800192 writel_relaxed(0xffff, d->base + INT_ERR1_MASK);
193 writel_relaxed(0xffff, d->base + INT_ERR2_MASK);
194 } else {
195 /* mask irq */
196 writel_relaxed(0x0, d->base + INT_TC1_MASK);
Andy Greena7e08fa2016-08-29 10:30:52 -0700197 writel_relaxed(0x0, d->base + INT_TC2_MASK);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800198 writel_relaxed(0x0, d->base + INT_ERR1_MASK);
199 writel_relaxed(0x0, d->base + INT_ERR2_MASK);
200 }
201}
202
203static irqreturn_t k3_dma_int_handler(int irq, void *dev_id)
204{
205 struct k3_dma_dev *d = (struct k3_dma_dev *)dev_id;
206 struct k3_dma_phy *p;
207 struct k3_dma_chan *c;
208 u32 stat = readl_relaxed(d->base + INT_STAT);
209 u32 tc1 = readl_relaxed(d->base + INT_TC1);
Andy Greena7e08fa2016-08-29 10:30:52 -0700210 u32 tc2 = readl_relaxed(d->base + INT_TC2);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800211 u32 err1 = readl_relaxed(d->base + INT_ERR1);
212 u32 err2 = readl_relaxed(d->base + INT_ERR2);
213 u32 i, irq_chan = 0;
214
215 while (stat) {
216 i = __ffs(stat);
Andy Greena7e08fa2016-08-29 10:30:52 -0700217 stat &= ~BIT(i);
218 if (likely(tc1 & BIT(i)) || (tc2 & BIT(i))) {
219 unsigned long flags;
220
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800221 p = &d->phy[i];
222 c = p->vchan;
Andy Greena7e08fa2016-08-29 10:30:52 -0700223 if (c && (tc1 & BIT(i))) {
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800224 spin_lock_irqsave(&c->vc.lock, flags);
225 vchan_cookie_complete(&p->ds_run->vd);
226 p->ds_done = p->ds_run;
John Stultz36387a22016-08-29 10:30:51 -0700227 p->ds_run = NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800228 spin_unlock_irqrestore(&c->vc.lock, flags);
229 }
Andy Greena7e08fa2016-08-29 10:30:52 -0700230 if (c && (tc2 & BIT(i))) {
231 spin_lock_irqsave(&c->vc.lock, flags);
232 if (p->ds_run != NULL)
233 vchan_cyclic_callback(&p->ds_run->vd);
234 spin_unlock_irqrestore(&c->vc.lock, flags);
235 }
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800236 irq_chan |= BIT(i);
237 }
238 if (unlikely((err1 & BIT(i)) || (err2 & BIT(i))))
239 dev_warn(d->slave.dev, "DMA ERR\n");
240 }
241
242 writel_relaxed(irq_chan, d->base + INT_TC1_RAW);
Andy Greena7e08fa2016-08-29 10:30:52 -0700243 writel_relaxed(irq_chan, d->base + INT_TC2_RAW);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800244 writel_relaxed(err1, d->base + INT_ERR1_RAW);
245 writel_relaxed(err2, d->base + INT_ERR2_RAW);
246
Andy Green0173c892016-08-29 10:30:49 -0700247 if (irq_chan)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800248 tasklet_schedule(&d->task);
Andy Green0173c892016-08-29 10:30:49 -0700249
250 if (irq_chan || err1 || err2)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800251 return IRQ_HANDLED;
Andy Green0173c892016-08-29 10:30:49 -0700252
253 return IRQ_NONE;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800254}
255
256static int k3_dma_start_txd(struct k3_dma_chan *c)
257{
258 struct k3_dma_dev *d = to_k3_dma(c->vc.chan.device);
259 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
260
261 if (!c->phy)
262 return -EAGAIN;
263
264 if (BIT(c->phy->idx) & k3_dma_get_chan_stat(d))
265 return -EAGAIN;
266
267 if (vd) {
268 struct k3_dma_desc_sw *ds =
269 container_of(vd, struct k3_dma_desc_sw, vd);
270 /*
271 * fetch and remove request from vc->desc_issued
272 * so vc->desc_issued only contains desc pending
273 */
274 list_del(&ds->vd.node);
John Stultz36387a22016-08-29 10:30:51 -0700275
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800276 c->phy->ds_run = ds;
Antonio Borneo626c4e82017-08-01 22:09:25 +0200277 c->phy->ds_done = NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800278 /* start dma */
279 k3_dma_set_desc(c->phy, &ds->desc_hw[0]);
280 return 0;
281 }
Antonio Borneo626c4e82017-08-01 22:09:25 +0200282 c->phy->ds_run = NULL;
283 c->phy->ds_done = NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800284 return -EAGAIN;
285}
286
287static void k3_dma_tasklet(unsigned long arg)
288{
289 struct k3_dma_dev *d = (struct k3_dma_dev *)arg;
290 struct k3_dma_phy *p;
291 struct k3_dma_chan *c, *cn;
292 unsigned pch, pch_alloc = 0;
293
294 /* check new dma request of running channel in vc->desc_issued */
295 list_for_each_entry_safe(c, cn, &d->slave.channels, vc.chan.device_node) {
296 spin_lock_irq(&c->vc.lock);
297 p = c->phy;
298 if (p && p->ds_done) {
299 if (k3_dma_start_txd(c)) {
300 /* No current txd associated with this channel */
301 dev_dbg(d->slave.dev, "pchan %u: free\n", p->idx);
302 /* Mark this channel free */
303 c->phy = NULL;
304 p->vchan = NULL;
305 }
306 }
307 spin_unlock_irq(&c->vc.lock);
308 }
309
310 /* check new channel request in d->chan_pending */
311 spin_lock_irq(&d->lock);
312 for (pch = 0; pch < d->dma_channels; pch++) {
313 p = &d->phy[pch];
314
315 if (p->vchan == NULL && !list_empty(&d->chan_pending)) {
316 c = list_first_entry(&d->chan_pending,
317 struct k3_dma_chan, node);
318 /* remove from d->chan_pending */
319 list_del_init(&c->node);
320 pch_alloc |= 1 << pch;
321 /* Mark this channel allocated */
322 p->vchan = c;
323 c->phy = p;
324 dev_dbg(d->slave.dev, "pchan %u: alloc vchan %p\n", pch, &c->vc);
325 }
326 }
327 spin_unlock_irq(&d->lock);
328
329 for (pch = 0; pch < d->dma_channels; pch++) {
330 if (pch_alloc & (1 << pch)) {
331 p = &d->phy[pch];
332 c = p->vchan;
333 if (c) {
334 spin_lock_irq(&c->vc.lock);
335 k3_dma_start_txd(c);
336 spin_unlock_irq(&c->vc.lock);
337 }
338 }
339 }
340}
341
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800342static void k3_dma_free_chan_resources(struct dma_chan *chan)
343{
344 struct k3_dma_chan *c = to_k3_chan(chan);
345 struct k3_dma_dev *d = to_k3_dma(chan->device);
346 unsigned long flags;
347
348 spin_lock_irqsave(&d->lock, flags);
349 list_del_init(&c->node);
350 spin_unlock_irqrestore(&d->lock, flags);
351
352 vchan_free_chan_resources(&c->vc);
353 c->ccfg = 0;
354}
355
356static enum dma_status k3_dma_tx_status(struct dma_chan *chan,
357 dma_cookie_t cookie, struct dma_tx_state *state)
358{
359 struct k3_dma_chan *c = to_k3_chan(chan);
360 struct k3_dma_dev *d = to_k3_dma(chan->device);
361 struct k3_dma_phy *p;
362 struct virt_dma_desc *vd;
363 unsigned long flags;
364 enum dma_status ret;
365 size_t bytes = 0;
366
367 ret = dma_cookie_status(&c->vc.chan, cookie, state);
Vinod Koulbd2c3482013-10-16 20:50:09 +0530368 if (ret == DMA_COMPLETE)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800369 return ret;
370
371 spin_lock_irqsave(&c->vc.lock, flags);
372 p = c->phy;
373 ret = c->status;
374
375 /*
376 * If the cookie is on our issue queue, then the residue is
377 * its total size.
378 */
379 vd = vchan_find_desc(&c->vc, cookie);
Andy Greena7e08fa2016-08-29 10:30:52 -0700380 if (vd && !c->cyclic) {
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800381 bytes = container_of(vd, struct k3_dma_desc_sw, vd)->size;
382 } else if ((!p) || (!p->ds_run)) {
383 bytes = 0;
384 } else {
385 struct k3_dma_desc_sw *ds = p->ds_run;
386 u32 clli = 0, index = 0;
387
388 bytes = k3_dma_get_curr_cnt(d, p);
389 clli = k3_dma_get_curr_lli(p);
Andy Greena7e08fa2016-08-29 10:30:52 -0700390 index = ((clli - ds->desc_hw_lli) /
391 sizeof(struct k3_desc_hw)) + 1;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800392 for (; index < ds->desc_num; index++) {
393 bytes += ds->desc_hw[index].count;
394 /* end of lli */
395 if (!ds->desc_hw[index].lli)
396 break;
397 }
398 }
399 spin_unlock_irqrestore(&c->vc.lock, flags);
400 dma_set_residue(state, bytes);
401 return ret;
402}
403
404static void k3_dma_issue_pending(struct dma_chan *chan)
405{
406 struct k3_dma_chan *c = to_k3_chan(chan);
407 struct k3_dma_dev *d = to_k3_dma(chan->device);
408 unsigned long flags;
409
410 spin_lock_irqsave(&c->vc.lock, flags);
411 /* add request to vc->desc_issued */
412 if (vchan_issue_pending(&c->vc)) {
413 spin_lock(&d->lock);
414 if (!c->phy) {
415 if (list_empty(&c->node)) {
416 /* if new channel, add chan_pending */
417 list_add_tail(&c->node, &d->chan_pending);
418 /* check in tasklet */
419 tasklet_schedule(&d->task);
420 dev_dbg(d->slave.dev, "vchan %p: issued\n", &c->vc);
421 }
422 }
423 spin_unlock(&d->lock);
424 } else
425 dev_dbg(d->slave.dev, "vchan %p: nothing to issue\n", &c->vc);
426 spin_unlock_irqrestore(&c->vc.lock, flags);
427}
428
429static void k3_dma_fill_desc(struct k3_dma_desc_sw *ds, dma_addr_t dst,
430 dma_addr_t src, size_t len, u32 num, u32 ccfg)
431{
Andy Greena7e08fa2016-08-29 10:30:52 -0700432 if (num != ds->desc_num - 1)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800433 ds->desc_hw[num].lli = ds->desc_hw_lli + (num + 1) *
434 sizeof(struct k3_desc_hw);
Andy Greena7e08fa2016-08-29 10:30:52 -0700435
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800436 ds->desc_hw[num].lli |= CX_LLI_CHAIN_EN;
437 ds->desc_hw[num].count = len;
438 ds->desc_hw[num].saddr = src;
439 ds->desc_hw[num].daddr = dst;
440 ds->desc_hw[num].config = ccfg;
441}
442
John Stultzb77f2622016-08-29 10:30:50 -0700443static struct k3_dma_desc_sw *k3_dma_alloc_desc_resource(int num,
444 struct dma_chan *chan)
445{
446 struct k3_dma_chan *c = to_k3_chan(chan);
447 struct k3_dma_desc_sw *ds;
448 struct k3_dma_dev *d = to_k3_dma(chan->device);
449 int lli_limit = LLI_BLOCK_SIZE / sizeof(struct k3_desc_hw);
450
451 if (num > lli_limit) {
452 dev_dbg(chan->device->dev, "vch %p: sg num %d exceed max %d\n",
453 &c->vc, num, lli_limit);
454 return NULL;
455 }
456
457 ds = kzalloc(sizeof(*ds), GFP_NOWAIT);
458 if (!ds)
459 return NULL;
460
Vinod Koul646b3b52016-12-07 09:36:22 +0530461 ds->desc_hw = dma_pool_zalloc(d->pool, GFP_NOWAIT, &ds->desc_hw_lli);
John Stultzb77f2622016-08-29 10:30:50 -0700462 if (!ds->desc_hw) {
463 dev_dbg(chan->device->dev, "vch %p: dma alloc fail\n", &c->vc);
464 kfree(ds);
465 return NULL;
466 }
John Stultzb77f2622016-08-29 10:30:50 -0700467 ds->desc_num = num;
468 return ds;
469}
470
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800471static struct dma_async_tx_descriptor *k3_dma_prep_memcpy(
472 struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
473 size_t len, unsigned long flags)
474{
475 struct k3_dma_chan *c = to_k3_chan(chan);
476 struct k3_dma_desc_sw *ds;
477 size_t copy = 0;
478 int num = 0;
479
480 if (!len)
481 return NULL;
482
483 num = DIV_ROUND_UP(len, DMA_MAX_SIZE);
John Stultzb77f2622016-08-29 10:30:50 -0700484
485 ds = k3_dma_alloc_desc_resource(num, chan);
Peter Griffinaef94fe2016-06-07 18:38:41 +0100486 if (!ds)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800487 return NULL;
Peter Griffinaef94fe2016-06-07 18:38:41 +0100488
Andy Greena7e08fa2016-08-29 10:30:52 -0700489 c->cyclic = 0;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800490 ds->size = len;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800491 num = 0;
492
493 if (!c->ccfg) {
Maxime Riparddb084252014-11-17 14:42:20 +0100494 /* default is memtomem, without calling device_config */
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800495 c->ccfg = CX_CFG_SRCINCR | CX_CFG_DSTINCR | CX_CFG_EN;
496 c->ccfg |= (0xf << 20) | (0xf << 24); /* burst = 16 */
497 c->ccfg |= (0x3 << 12) | (0x3 << 16); /* width = 64 bit */
498 }
499
500 do {
501 copy = min_t(size_t, len, DMA_MAX_SIZE);
502 k3_dma_fill_desc(ds, dst, src, copy, num++, c->ccfg);
503
Vinod Koulad7756e2018-10-05 06:32:12 +0530504 src += copy;
505 dst += copy;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800506 len -= copy;
507 } while (len);
508
509 ds->desc_hw[num-1].lli = 0; /* end of link */
510 return vchan_tx_prep(&c->vc, &ds->vd, flags);
511}
512
513static struct dma_async_tx_descriptor *k3_dma_prep_slave_sg(
514 struct dma_chan *chan, struct scatterlist *sgl, unsigned int sglen,
515 enum dma_transfer_direction dir, unsigned long flags, void *context)
516{
517 struct k3_dma_chan *c = to_k3_chan(chan);
518 struct k3_dma_desc_sw *ds;
519 size_t len, avail, total = 0;
520 struct scatterlist *sg;
521 dma_addr_t addr, src = 0, dst = 0;
522 int num = sglen, i;
523
Zhangfei Gaoc61177c2014-01-14 11:37:43 +0800524 if (sgl == NULL)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800525 return NULL;
526
Andy Greena7e08fa2016-08-29 10:30:52 -0700527 c->cyclic = 0;
528
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800529 for_each_sg(sgl, sg, sglen, i) {
530 avail = sg_dma_len(sg);
531 if (avail > DMA_MAX_SIZE)
532 num += DIV_ROUND_UP(avail, DMA_MAX_SIZE) - 1;
533 }
534
John Stultzb77f2622016-08-29 10:30:50 -0700535 ds = k3_dma_alloc_desc_resource(num, chan);
Peter Griffinaef94fe2016-06-07 18:38:41 +0100536 if (!ds)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800537 return NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800538 num = 0;
539
540 for_each_sg(sgl, sg, sglen, i) {
541 addr = sg_dma_address(sg);
542 avail = sg_dma_len(sg);
543 total += avail;
544
545 do {
546 len = min_t(size_t, avail, DMA_MAX_SIZE);
547
548 if (dir == DMA_MEM_TO_DEV) {
549 src = addr;
550 dst = c->dev_addr;
551 } else if (dir == DMA_DEV_TO_MEM) {
552 src = c->dev_addr;
553 dst = addr;
554 }
555
556 k3_dma_fill_desc(ds, dst, src, len, num++, c->ccfg);
557
558 addr += len;
559 avail -= len;
560 } while (avail);
561 }
562
563 ds->desc_hw[num-1].lli = 0; /* end of link */
564 ds->size = total;
565 return vchan_tx_prep(&c->vc, &ds->vd, flags);
566}
567
Andy Greena7e08fa2016-08-29 10:30:52 -0700568static struct dma_async_tx_descriptor *
569k3_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr,
570 size_t buf_len, size_t period_len,
571 enum dma_transfer_direction dir,
572 unsigned long flags)
573{
574 struct k3_dma_chan *c = to_k3_chan(chan);
575 struct k3_dma_desc_sw *ds;
576 size_t len, avail, total = 0;
577 dma_addr_t addr, src = 0, dst = 0;
578 int num = 1, since = 0;
579 size_t modulo = DMA_CYCLIC_MAX_PERIOD;
580 u32 en_tc2 = 0;
581
Arnd Bergmann5f03c392016-09-06 15:17:49 +0200582 dev_dbg(chan->device->dev, "%s: buf %pad, dst %pad, buf len %zu, period_len = %zu, dir %d\n",
583 __func__, &buf_addr, &to_k3_chan(chan)->dev_addr,
584 buf_len, period_len, (int)dir);
Andy Greena7e08fa2016-08-29 10:30:52 -0700585
586 avail = buf_len;
587 if (avail > modulo)
588 num += DIV_ROUND_UP(avail, modulo) - 1;
589
590 ds = k3_dma_alloc_desc_resource(num, chan);
591 if (!ds)
592 return NULL;
593
594 c->cyclic = 1;
595 addr = buf_addr;
596 avail = buf_len;
597 total = avail;
598 num = 0;
599
600 if (period_len < modulo)
601 modulo = period_len;
602
603 do {
604 len = min_t(size_t, avail, modulo);
605
606 if (dir == DMA_MEM_TO_DEV) {
607 src = addr;
608 dst = c->dev_addr;
609 } else if (dir == DMA_DEV_TO_MEM) {
610 src = c->dev_addr;
611 dst = addr;
612 }
613 since += len;
614 if (since >= period_len) {
615 /* descriptor asks for TC2 interrupt on completion */
616 en_tc2 = CX_CFG_NODEIRQ;
617 since -= period_len;
618 } else
619 en_tc2 = 0;
620
621 k3_dma_fill_desc(ds, dst, src, len, num++, c->ccfg | en_tc2);
622
623 addr += len;
624 avail -= len;
625 } while (avail);
626
627 /* "Cyclic" == end of link points back to start of link */
628 ds->desc_hw[num - 1].lli |= ds->desc_hw_lli;
629
630 ds->size = total;
631
632 return vchan_tx_prep(&c->vc, &ds->vd, flags);
633}
634
Maxime Riparddb084252014-11-17 14:42:20 +0100635static int k3_dma_config(struct dma_chan *chan,
636 struct dma_slave_config *cfg)
637{
638 struct k3_dma_chan *c = to_k3_chan(chan);
639 u32 maxburst = 0, val = 0;
640 enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
641
642 if (cfg == NULL)
643 return -EINVAL;
644 c->dir = cfg->direction;
645 if (c->dir == DMA_DEV_TO_MEM) {
646 c->ccfg = CX_CFG_DSTINCR;
647 c->dev_addr = cfg->src_addr;
648 maxburst = cfg->src_maxburst;
649 width = cfg->src_addr_width;
650 } else if (c->dir == DMA_MEM_TO_DEV) {
651 c->ccfg = CX_CFG_SRCINCR;
652 c->dev_addr = cfg->dst_addr;
653 maxburst = cfg->dst_maxburst;
654 width = cfg->dst_addr_width;
655 }
656 switch (width) {
657 case DMA_SLAVE_BUSWIDTH_1_BYTE:
658 case DMA_SLAVE_BUSWIDTH_2_BYTES:
659 case DMA_SLAVE_BUSWIDTH_4_BYTES:
660 case DMA_SLAVE_BUSWIDTH_8_BYTES:
661 val = __ffs(width);
662 break;
663 default:
664 val = 3;
665 break;
666 }
667 c->ccfg |= (val << 12) | (val << 16);
668
669 if ((maxburst == 0) || (maxburst > 16))
Andy Green6c28a902016-08-29 10:30:47 -0700670 val = 15;
Maxime Riparddb084252014-11-17 14:42:20 +0100671 else
672 val = maxburst - 1;
673 c->ccfg |= (val << 20) | (val << 24);
674 c->ccfg |= CX_CFG_MEM2PER | CX_CFG_EN;
675
676 /* specific request line */
677 c->ccfg |= c->vc.chan.chan_id << 4;
678
679 return 0;
680}
681
John Stultz36387a22016-08-29 10:30:51 -0700682static void k3_dma_free_desc(struct virt_dma_desc *vd)
683{
684 struct k3_dma_desc_sw *ds =
685 container_of(vd, struct k3_dma_desc_sw, vd);
686 struct k3_dma_dev *d = to_k3_dma(vd->tx.chan->device);
687
688 dma_pool_free(d->pool, ds->desc_hw, ds->desc_hw_lli);
689 kfree(ds);
690}
691
Maxime Riparddb084252014-11-17 14:42:20 +0100692static int k3_dma_terminate_all(struct dma_chan *chan)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800693{
694 struct k3_dma_chan *c = to_k3_chan(chan);
695 struct k3_dma_dev *d = to_k3_dma(chan->device);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800696 struct k3_dma_phy *p = c->phy;
697 unsigned long flags;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800698 LIST_HEAD(head);
699
Maxime Riparddb084252014-11-17 14:42:20 +0100700 dev_dbg(d->slave.dev, "vchan %p: terminate all\n", &c->vc);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800701
Maxime Riparddb084252014-11-17 14:42:20 +0100702 /* Prevent this channel being scheduled */
703 spin_lock(&d->lock);
704 list_del_init(&c->node);
705 spin_unlock(&d->lock);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800706
Maxime Riparddb084252014-11-17 14:42:20 +0100707 /* Clear the tx descriptor lists */
708 spin_lock_irqsave(&c->vc.lock, flags);
709 vchan_get_all_descriptors(&c->vc, &head);
710 if (p) {
711 /* vchan is assigned to a pchan - stop the channel */
712 k3_dma_terminate_chan(p, d);
713 c->phy = NULL;
714 p->vchan = NULL;
John Stultz36387a22016-08-29 10:30:51 -0700715 if (p->ds_run) {
Peter Ujfalusi3ee7e422017-11-14 16:32:11 +0200716 vchan_terminate_vdesc(&p->ds_run->vd);
John Stultz36387a22016-08-29 10:30:51 -0700717 p->ds_run = NULL;
718 }
Antonio Borneo132b4732017-08-01 22:09:26 +0200719 p->ds_done = NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800720 }
Maxime Riparddb084252014-11-17 14:42:20 +0100721 spin_unlock_irqrestore(&c->vc.lock, flags);
722 vchan_dma_desc_free_list(&c->vc, &head);
723
724 return 0;
725}
726
Peter Ujfalusi3ee7e422017-11-14 16:32:11 +0200727static void k3_dma_synchronize(struct dma_chan *chan)
728{
729 struct k3_dma_chan *c = to_k3_chan(chan);
730
731 vchan_synchronize(&c->vc);
732}
733
Krzysztof Kozlowskia1a9bec2014-12-29 14:01:30 +0100734static int k3_dma_transfer_pause(struct dma_chan *chan)
Maxime Riparddb084252014-11-17 14:42:20 +0100735{
736 struct k3_dma_chan *c = to_k3_chan(chan);
737 struct k3_dma_dev *d = to_k3_dma(chan->device);
738 struct k3_dma_phy *p = c->phy;
739
740 dev_dbg(d->slave.dev, "vchan %p: pause\n", &c->vc);
741 if (c->status == DMA_IN_PROGRESS) {
742 c->status = DMA_PAUSED;
743 if (p) {
744 k3_dma_pause_dma(p, false);
745 } else {
746 spin_lock(&d->lock);
747 list_del_init(&c->node);
748 spin_unlock(&d->lock);
749 }
750 }
751
752 return 0;
753}
754
Krzysztof Kozlowskia1a9bec2014-12-29 14:01:30 +0100755static int k3_dma_transfer_resume(struct dma_chan *chan)
Maxime Riparddb084252014-11-17 14:42:20 +0100756{
757 struct k3_dma_chan *c = to_k3_chan(chan);
758 struct k3_dma_dev *d = to_k3_dma(chan->device);
759 struct k3_dma_phy *p = c->phy;
760 unsigned long flags;
761
762 dev_dbg(d->slave.dev, "vchan %p: resume\n", &c->vc);
763 spin_lock_irqsave(&c->vc.lock, flags);
764 if (c->status == DMA_PAUSED) {
765 c->status = DMA_IN_PROGRESS;
766 if (p) {
767 k3_dma_pause_dma(p, true);
768 } else if (!list_empty(&c->vc.desc_issued)) {
769 spin_lock(&d->lock);
770 list_add_tail(&c->node, &d->chan_pending);
771 spin_unlock(&d->lock);
772 }
773 }
774 spin_unlock_irqrestore(&c->vc.lock, flags);
775
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800776 return 0;
777}
778
Fabian Frederick57c03422015-03-16 20:17:14 +0100779static const struct of_device_id k3_pdma_dt_ids[] = {
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800780 { .compatible = "hisilicon,k3-dma-1.0", },
781 {}
782};
783MODULE_DEVICE_TABLE(of, k3_pdma_dt_ids);
784
785static struct dma_chan *k3_of_dma_simple_xlate(struct of_phandle_args *dma_spec,
786 struct of_dma *ofdma)
787{
788 struct k3_dma_dev *d = ofdma->of_dma_data;
789 unsigned int request = dma_spec->args[0];
790
Dan Carpenterc4c2b762018-06-22 14:15:47 +0300791 if (request >= d->dma_requests)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800792 return NULL;
793
794 return dma_get_slave_channel(&(d->chans[request].vc.chan));
795}
796
797static int k3_dma_probe(struct platform_device *op)
798{
799 struct k3_dma_dev *d;
800 const struct of_device_id *of_id;
801 struct resource *iores;
802 int i, ret, irq = 0;
803
804 iores = platform_get_resource(op, IORESOURCE_MEM, 0);
805 if (!iores)
806 return -EINVAL;
807
808 d = devm_kzalloc(&op->dev, sizeof(*d), GFP_KERNEL);
809 if (!d)
810 return -ENOMEM;
811
Jingoo Hana576b7f2013-09-02 10:25:13 +0900812 d->base = devm_ioremap_resource(&op->dev, iores);
813 if (IS_ERR(d->base))
814 return PTR_ERR(d->base);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800815
816 of_id = of_match_device(k3_pdma_dt_ids, &op->dev);
817 if (of_id) {
818 of_property_read_u32((&op->dev)->of_node,
819 "dma-channels", &d->dma_channels);
820 of_property_read_u32((&op->dev)->of_node,
821 "dma-requests", &d->dma_requests);
822 }
823
824 d->clk = devm_clk_get(&op->dev, NULL);
825 if (IS_ERR(d->clk)) {
826 dev_err(&op->dev, "no dma clk\n");
827 return PTR_ERR(d->clk);
828 }
829
830 irq = platform_get_irq(op, 0);
831 ret = devm_request_irq(&op->dev, irq,
Michael Opdenacker174b5372013-10-13 07:10:51 +0200832 k3_dma_int_handler, 0, DRIVER_NAME, d);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800833 if (ret)
834 return ret;
835
Vinod Koul486b10a2016-07-03 00:02:29 +0530836 d->irq = irq;
837
John Stultzb77f2622016-08-29 10:30:50 -0700838 /* A DMA memory pool for LLIs, align on 32-byte boundary */
839 d->pool = dmam_pool_create(DRIVER_NAME, &op->dev,
840 LLI_BLOCK_SIZE, 32, 0);
841 if (!d->pool)
842 return -ENOMEM;
843
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800844 /* init phy channel */
Kees Cooka86854d2018-06-12 14:07:58 -0700845 d->phy = devm_kcalloc(&op->dev,
846 d->dma_channels, sizeof(struct k3_dma_phy), GFP_KERNEL);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800847 if (d->phy == NULL)
848 return -ENOMEM;
849
850 for (i = 0; i < d->dma_channels; i++) {
851 struct k3_dma_phy *p = &d->phy[i];
852
853 p->idx = i;
854 p->base = d->base + i * 0x40;
855 }
856
857 INIT_LIST_HEAD(&d->slave.channels);
858 dma_cap_set(DMA_SLAVE, d->slave.cap_mask);
859 dma_cap_set(DMA_MEMCPY, d->slave.cap_mask);
Andy Greena7e08fa2016-08-29 10:30:52 -0700860 dma_cap_set(DMA_CYCLIC, d->slave.cap_mask);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800861 d->slave.dev = &op->dev;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800862 d->slave.device_free_chan_resources = k3_dma_free_chan_resources;
863 d->slave.device_tx_status = k3_dma_tx_status;
864 d->slave.device_prep_dma_memcpy = k3_dma_prep_memcpy;
865 d->slave.device_prep_slave_sg = k3_dma_prep_slave_sg;
Andy Greena7e08fa2016-08-29 10:30:52 -0700866 d->slave.device_prep_dma_cyclic = k3_dma_prep_dma_cyclic;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800867 d->slave.device_issue_pending = k3_dma_issue_pending;
Maxime Riparddb084252014-11-17 14:42:20 +0100868 d->slave.device_config = k3_dma_config;
Krzysztof Kozlowskia1a9bec2014-12-29 14:01:30 +0100869 d->slave.device_pause = k3_dma_transfer_pause;
870 d->slave.device_resume = k3_dma_transfer_resume;
Maxime Riparddb084252014-11-17 14:42:20 +0100871 d->slave.device_terminate_all = k3_dma_terminate_all;
Peter Ujfalusi3ee7e422017-11-14 16:32:11 +0200872 d->slave.device_synchronize = k3_dma_synchronize;
Maxime Ripard77a68e52015-07-20 10:41:32 +0200873 d->slave.copy_align = DMAENGINE_ALIGN_8_BYTES;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800874
875 /* init virtual channel */
Kees Cooka86854d2018-06-12 14:07:58 -0700876 d->chans = devm_kcalloc(&op->dev,
877 d->dma_requests, sizeof(struct k3_dma_chan), GFP_KERNEL);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800878 if (d->chans == NULL)
879 return -ENOMEM;
880
881 for (i = 0; i < d->dma_requests; i++) {
882 struct k3_dma_chan *c = &d->chans[i];
883
884 c->status = DMA_IN_PROGRESS;
885 INIT_LIST_HEAD(&c->node);
886 c->vc.desc_free = k3_dma_free_desc;
887 vchan_init(&c->vc, &d->slave);
888 }
889
890 /* Enable clock before accessing registers */
891 ret = clk_prepare_enable(d->clk);
892 if (ret < 0) {
893 dev_err(&op->dev, "clk_prepare_enable failed: %d\n", ret);
894 return ret;
895 }
896
897 k3_dma_enable_dma(d, true);
898
899 ret = dma_async_device_register(&d->slave);
900 if (ret)
Wei Yongjun89b90c02016-07-19 11:29:41 +0000901 goto dma_async_register_fail;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800902
903 ret = of_dma_controller_register((&op->dev)->of_node,
904 k3_of_dma_simple_xlate, d);
905 if (ret)
906 goto of_dma_register_fail;
907
908 spin_lock_init(&d->lock);
909 INIT_LIST_HEAD(&d->chan_pending);
910 tasklet_init(&d->task, k3_dma_tasklet, (unsigned long)d);
911 platform_set_drvdata(op, d);
912 dev_info(&op->dev, "initialized\n");
913
914 return 0;
915
916of_dma_register_fail:
917 dma_async_device_unregister(&d->slave);
Wei Yongjun89b90c02016-07-19 11:29:41 +0000918dma_async_register_fail:
919 clk_disable_unprepare(d->clk);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800920 return ret;
921}
922
923static int k3_dma_remove(struct platform_device *op)
924{
925 struct k3_dma_chan *c, *cn;
926 struct k3_dma_dev *d = platform_get_drvdata(op);
927
928 dma_async_device_unregister(&d->slave);
929 of_dma_controller_free((&op->dev)->of_node);
930
Vinod Koul486b10a2016-07-03 00:02:29 +0530931 devm_free_irq(&op->dev, d->irq, d);
932
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800933 list_for_each_entry_safe(c, cn, &d->slave.channels, vc.chan.device_node) {
934 list_del(&c->vc.chan.device_node);
935 tasklet_kill(&c->vc.task);
936 }
937 tasklet_kill(&d->task);
938 clk_disable_unprepare(d->clk);
939 return 0;
940}
941
Jingoo Hanaf2d3132014-10-27 21:36:26 +0900942#ifdef CONFIG_PM_SLEEP
Arnd Bergmann10b3e222015-01-13 14:23:13 +0100943static int k3_dma_suspend_dev(struct device *dev)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800944{
945 struct k3_dma_dev *d = dev_get_drvdata(dev);
946 u32 stat = 0;
947
948 stat = k3_dma_get_chan_stat(d);
949 if (stat) {
950 dev_warn(d->slave.dev,
951 "chan %d is running fail to suspend\n", stat);
952 return -1;
953 }
954 k3_dma_enable_dma(d, false);
955 clk_disable_unprepare(d->clk);
956 return 0;
957}
958
Arnd Bergmann10b3e222015-01-13 14:23:13 +0100959static int k3_dma_resume_dev(struct device *dev)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800960{
961 struct k3_dma_dev *d = dev_get_drvdata(dev);
962 int ret = 0;
963
964 ret = clk_prepare_enable(d->clk);
965 if (ret < 0) {
966 dev_err(d->slave.dev, "clk_prepare_enable failed: %d\n", ret);
967 return ret;
968 }
969 k3_dma_enable_dma(d, true);
970 return 0;
971}
Jingoo Hanaf2d3132014-10-27 21:36:26 +0900972#endif
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800973
Arnd Bergmann10b3e222015-01-13 14:23:13 +0100974static SIMPLE_DEV_PM_OPS(k3_dma_pmops, k3_dma_suspend_dev, k3_dma_resume_dev);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800975
976static struct platform_driver k3_pdma_driver = {
977 .driver = {
978 .name = DRIVER_NAME,
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800979 .pm = &k3_dma_pmops,
980 .of_match_table = k3_pdma_dt_ids,
981 },
982 .probe = k3_dma_probe,
983 .remove = k3_dma_remove,
984};
985
986module_platform_driver(k3_pdma_driver);
987
988MODULE_DESCRIPTION("Hisilicon k3 DMA Driver");
989MODULE_ALIAS("platform:k3dma");
990MODULE_LICENSE("GPL v2");