blob: c00eb1244fcf631007a71f95d11c6b5b1a6952da [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);
John Stultz36387a22016-08-29 10:30:51 -0700226 WARN_ON_ONCE(p->ds_done);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800227 p->ds_done = p->ds_run;
John Stultz36387a22016-08-29 10:30:51 -0700228 p->ds_run = NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800229 spin_unlock_irqrestore(&c->vc.lock, flags);
230 }
Andy Greena7e08fa2016-08-29 10:30:52 -0700231 if (c && (tc2 & BIT(i))) {
232 spin_lock_irqsave(&c->vc.lock, flags);
233 if (p->ds_run != NULL)
234 vchan_cyclic_callback(&p->ds_run->vd);
235 spin_unlock_irqrestore(&c->vc.lock, flags);
236 }
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800237 irq_chan |= BIT(i);
238 }
239 if (unlikely((err1 & BIT(i)) || (err2 & BIT(i))))
240 dev_warn(d->slave.dev, "DMA ERR\n");
241 }
242
243 writel_relaxed(irq_chan, d->base + INT_TC1_RAW);
Andy Greena7e08fa2016-08-29 10:30:52 -0700244 writel_relaxed(irq_chan, d->base + INT_TC2_RAW);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800245 writel_relaxed(err1, d->base + INT_ERR1_RAW);
246 writel_relaxed(err2, d->base + INT_ERR2_RAW);
247
Andy Green0173c892016-08-29 10:30:49 -0700248 if (irq_chan)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800249 tasklet_schedule(&d->task);
Andy Green0173c892016-08-29 10:30:49 -0700250
251 if (irq_chan || err1 || err2)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800252 return IRQ_HANDLED;
Andy Green0173c892016-08-29 10:30:49 -0700253
254 return IRQ_NONE;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800255}
256
257static int k3_dma_start_txd(struct k3_dma_chan *c)
258{
259 struct k3_dma_dev *d = to_k3_dma(c->vc.chan.device);
260 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
261
262 if (!c->phy)
263 return -EAGAIN;
264
265 if (BIT(c->phy->idx) & k3_dma_get_chan_stat(d))
266 return -EAGAIN;
267
268 if (vd) {
269 struct k3_dma_desc_sw *ds =
270 container_of(vd, struct k3_dma_desc_sw, vd);
271 /*
272 * fetch and remove request from vc->desc_issued
273 * so vc->desc_issued only contains desc pending
274 */
275 list_del(&ds->vd.node);
John Stultz36387a22016-08-29 10:30:51 -0700276
277 WARN_ON_ONCE(c->phy->ds_run);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800278 c->phy->ds_run = ds;
Antonio Borneo626c4e82017-08-01 22:09:25 +0200279 c->phy->ds_done = NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800280 /* start dma */
281 k3_dma_set_desc(c->phy, &ds->desc_hw[0]);
282 return 0;
283 }
Antonio Borneo626c4e82017-08-01 22:09:25 +0200284 c->phy->ds_run = NULL;
285 c->phy->ds_done = NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800286 return -EAGAIN;
287}
288
289static void k3_dma_tasklet(unsigned long arg)
290{
291 struct k3_dma_dev *d = (struct k3_dma_dev *)arg;
292 struct k3_dma_phy *p;
293 struct k3_dma_chan *c, *cn;
294 unsigned pch, pch_alloc = 0;
295
296 /* check new dma request of running channel in vc->desc_issued */
297 list_for_each_entry_safe(c, cn, &d->slave.channels, vc.chan.device_node) {
298 spin_lock_irq(&c->vc.lock);
299 p = c->phy;
300 if (p && p->ds_done) {
301 if (k3_dma_start_txd(c)) {
302 /* No current txd associated with this channel */
303 dev_dbg(d->slave.dev, "pchan %u: free\n", p->idx);
304 /* Mark this channel free */
305 c->phy = NULL;
306 p->vchan = NULL;
307 }
308 }
309 spin_unlock_irq(&c->vc.lock);
310 }
311
312 /* check new channel request in d->chan_pending */
313 spin_lock_irq(&d->lock);
314 for (pch = 0; pch < d->dma_channels; pch++) {
315 p = &d->phy[pch];
316
317 if (p->vchan == NULL && !list_empty(&d->chan_pending)) {
318 c = list_first_entry(&d->chan_pending,
319 struct k3_dma_chan, node);
320 /* remove from d->chan_pending */
321 list_del_init(&c->node);
322 pch_alloc |= 1 << pch;
323 /* Mark this channel allocated */
324 p->vchan = c;
325 c->phy = p;
326 dev_dbg(d->slave.dev, "pchan %u: alloc vchan %p\n", pch, &c->vc);
327 }
328 }
329 spin_unlock_irq(&d->lock);
330
331 for (pch = 0; pch < d->dma_channels; pch++) {
332 if (pch_alloc & (1 << pch)) {
333 p = &d->phy[pch];
334 c = p->vchan;
335 if (c) {
336 spin_lock_irq(&c->vc.lock);
337 k3_dma_start_txd(c);
338 spin_unlock_irq(&c->vc.lock);
339 }
340 }
341 }
342}
343
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800344static void k3_dma_free_chan_resources(struct dma_chan *chan)
345{
346 struct k3_dma_chan *c = to_k3_chan(chan);
347 struct k3_dma_dev *d = to_k3_dma(chan->device);
348 unsigned long flags;
349
350 spin_lock_irqsave(&d->lock, flags);
351 list_del_init(&c->node);
352 spin_unlock_irqrestore(&d->lock, flags);
353
354 vchan_free_chan_resources(&c->vc);
355 c->ccfg = 0;
356}
357
358static enum dma_status k3_dma_tx_status(struct dma_chan *chan,
359 dma_cookie_t cookie, struct dma_tx_state *state)
360{
361 struct k3_dma_chan *c = to_k3_chan(chan);
362 struct k3_dma_dev *d = to_k3_dma(chan->device);
363 struct k3_dma_phy *p;
364 struct virt_dma_desc *vd;
365 unsigned long flags;
366 enum dma_status ret;
367 size_t bytes = 0;
368
369 ret = dma_cookie_status(&c->vc.chan, cookie, state);
Vinod Koulbd2c3482013-10-16 20:50:09 +0530370 if (ret == DMA_COMPLETE)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800371 return ret;
372
373 spin_lock_irqsave(&c->vc.lock, flags);
374 p = c->phy;
375 ret = c->status;
376
377 /*
378 * If the cookie is on our issue queue, then the residue is
379 * its total size.
380 */
381 vd = vchan_find_desc(&c->vc, cookie);
Andy Greena7e08fa2016-08-29 10:30:52 -0700382 if (vd && !c->cyclic) {
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800383 bytes = container_of(vd, struct k3_dma_desc_sw, vd)->size;
384 } else if ((!p) || (!p->ds_run)) {
385 bytes = 0;
386 } else {
387 struct k3_dma_desc_sw *ds = p->ds_run;
388 u32 clli = 0, index = 0;
389
390 bytes = k3_dma_get_curr_cnt(d, p);
391 clli = k3_dma_get_curr_lli(p);
Andy Greena7e08fa2016-08-29 10:30:52 -0700392 index = ((clli - ds->desc_hw_lli) /
393 sizeof(struct k3_desc_hw)) + 1;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800394 for (; index < ds->desc_num; index++) {
395 bytes += ds->desc_hw[index].count;
396 /* end of lli */
397 if (!ds->desc_hw[index].lli)
398 break;
399 }
400 }
401 spin_unlock_irqrestore(&c->vc.lock, flags);
402 dma_set_residue(state, bytes);
403 return ret;
404}
405
406static void k3_dma_issue_pending(struct dma_chan *chan)
407{
408 struct k3_dma_chan *c = to_k3_chan(chan);
409 struct k3_dma_dev *d = to_k3_dma(chan->device);
410 unsigned long flags;
411
412 spin_lock_irqsave(&c->vc.lock, flags);
413 /* add request to vc->desc_issued */
414 if (vchan_issue_pending(&c->vc)) {
415 spin_lock(&d->lock);
416 if (!c->phy) {
417 if (list_empty(&c->node)) {
418 /* if new channel, add chan_pending */
419 list_add_tail(&c->node, &d->chan_pending);
420 /* check in tasklet */
421 tasklet_schedule(&d->task);
422 dev_dbg(d->slave.dev, "vchan %p: issued\n", &c->vc);
423 }
424 }
425 spin_unlock(&d->lock);
426 } else
427 dev_dbg(d->slave.dev, "vchan %p: nothing to issue\n", &c->vc);
428 spin_unlock_irqrestore(&c->vc.lock, flags);
429}
430
431static void k3_dma_fill_desc(struct k3_dma_desc_sw *ds, dma_addr_t dst,
432 dma_addr_t src, size_t len, u32 num, u32 ccfg)
433{
Andy Greena7e08fa2016-08-29 10:30:52 -0700434 if (num != ds->desc_num - 1)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800435 ds->desc_hw[num].lli = ds->desc_hw_lli + (num + 1) *
436 sizeof(struct k3_desc_hw);
Andy Greena7e08fa2016-08-29 10:30:52 -0700437
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800438 ds->desc_hw[num].lli |= CX_LLI_CHAIN_EN;
439 ds->desc_hw[num].count = len;
440 ds->desc_hw[num].saddr = src;
441 ds->desc_hw[num].daddr = dst;
442 ds->desc_hw[num].config = ccfg;
443}
444
John Stultzb77f2622016-08-29 10:30:50 -0700445static struct k3_dma_desc_sw *k3_dma_alloc_desc_resource(int num,
446 struct dma_chan *chan)
447{
448 struct k3_dma_chan *c = to_k3_chan(chan);
449 struct k3_dma_desc_sw *ds;
450 struct k3_dma_dev *d = to_k3_dma(chan->device);
451 int lli_limit = LLI_BLOCK_SIZE / sizeof(struct k3_desc_hw);
452
453 if (num > lli_limit) {
454 dev_dbg(chan->device->dev, "vch %p: sg num %d exceed max %d\n",
455 &c->vc, num, lli_limit);
456 return NULL;
457 }
458
459 ds = kzalloc(sizeof(*ds), GFP_NOWAIT);
460 if (!ds)
461 return NULL;
462
Vinod Koul646b3b52016-12-07 09:36:22 +0530463 ds->desc_hw = dma_pool_zalloc(d->pool, GFP_NOWAIT, &ds->desc_hw_lli);
John Stultzb77f2622016-08-29 10:30:50 -0700464 if (!ds->desc_hw) {
465 dev_dbg(chan->device->dev, "vch %p: dma alloc fail\n", &c->vc);
466 kfree(ds);
467 return NULL;
468 }
John Stultzb77f2622016-08-29 10:30:50 -0700469 ds->desc_num = num;
470 return ds;
471}
472
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800473static struct dma_async_tx_descriptor *k3_dma_prep_memcpy(
474 struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
475 size_t len, unsigned long flags)
476{
477 struct k3_dma_chan *c = to_k3_chan(chan);
478 struct k3_dma_desc_sw *ds;
479 size_t copy = 0;
480 int num = 0;
481
482 if (!len)
483 return NULL;
484
485 num = DIV_ROUND_UP(len, DMA_MAX_SIZE);
John Stultzb77f2622016-08-29 10:30:50 -0700486
487 ds = k3_dma_alloc_desc_resource(num, chan);
Peter Griffinaef94fe2016-06-07 18:38:41 +0100488 if (!ds)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800489 return NULL;
Peter Griffinaef94fe2016-06-07 18:38:41 +0100490
Andy Greena7e08fa2016-08-29 10:30:52 -0700491 c->cyclic = 0;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800492 ds->size = len;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800493 num = 0;
494
495 if (!c->ccfg) {
Maxime Riparddb084252014-11-17 14:42:20 +0100496 /* default is memtomem, without calling device_config */
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800497 c->ccfg = CX_CFG_SRCINCR | CX_CFG_DSTINCR | CX_CFG_EN;
498 c->ccfg |= (0xf << 20) | (0xf << 24); /* burst = 16 */
499 c->ccfg |= (0x3 << 12) | (0x3 << 16); /* width = 64 bit */
500 }
501
502 do {
503 copy = min_t(size_t, len, DMA_MAX_SIZE);
504 k3_dma_fill_desc(ds, dst, src, copy, num++, c->ccfg);
505
506 if (c->dir == DMA_MEM_TO_DEV) {
507 src += copy;
508 } else if (c->dir == DMA_DEV_TO_MEM) {
509 dst += copy;
510 } else {
511 src += copy;
512 dst += copy;
513 }
514 len -= copy;
515 } while (len);
516
517 ds->desc_hw[num-1].lli = 0; /* end of link */
518 return vchan_tx_prep(&c->vc, &ds->vd, flags);
519}
520
521static struct dma_async_tx_descriptor *k3_dma_prep_slave_sg(
522 struct dma_chan *chan, struct scatterlist *sgl, unsigned int sglen,
523 enum dma_transfer_direction dir, unsigned long flags, void *context)
524{
525 struct k3_dma_chan *c = to_k3_chan(chan);
526 struct k3_dma_desc_sw *ds;
527 size_t len, avail, total = 0;
528 struct scatterlist *sg;
529 dma_addr_t addr, src = 0, dst = 0;
530 int num = sglen, i;
531
Zhangfei Gaoc61177c2014-01-14 11:37:43 +0800532 if (sgl == NULL)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800533 return NULL;
534
Andy Greena7e08fa2016-08-29 10:30:52 -0700535 c->cyclic = 0;
536
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800537 for_each_sg(sgl, sg, sglen, i) {
538 avail = sg_dma_len(sg);
539 if (avail > DMA_MAX_SIZE)
540 num += DIV_ROUND_UP(avail, DMA_MAX_SIZE) - 1;
541 }
542
John Stultzb77f2622016-08-29 10:30:50 -0700543 ds = k3_dma_alloc_desc_resource(num, chan);
Peter Griffinaef94fe2016-06-07 18:38:41 +0100544 if (!ds)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800545 return NULL;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800546 num = 0;
547
548 for_each_sg(sgl, sg, sglen, i) {
549 addr = sg_dma_address(sg);
550 avail = sg_dma_len(sg);
551 total += avail;
552
553 do {
554 len = min_t(size_t, avail, DMA_MAX_SIZE);
555
556 if (dir == DMA_MEM_TO_DEV) {
557 src = addr;
558 dst = c->dev_addr;
559 } else if (dir == DMA_DEV_TO_MEM) {
560 src = c->dev_addr;
561 dst = addr;
562 }
563
564 k3_dma_fill_desc(ds, dst, src, len, num++, c->ccfg);
565
566 addr += len;
567 avail -= len;
568 } while (avail);
569 }
570
571 ds->desc_hw[num-1].lli = 0; /* end of link */
572 ds->size = total;
573 return vchan_tx_prep(&c->vc, &ds->vd, flags);
574}
575
Andy Greena7e08fa2016-08-29 10:30:52 -0700576static struct dma_async_tx_descriptor *
577k3_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t buf_addr,
578 size_t buf_len, size_t period_len,
579 enum dma_transfer_direction dir,
580 unsigned long flags)
581{
582 struct k3_dma_chan *c = to_k3_chan(chan);
583 struct k3_dma_desc_sw *ds;
584 size_t len, avail, total = 0;
585 dma_addr_t addr, src = 0, dst = 0;
586 int num = 1, since = 0;
587 size_t modulo = DMA_CYCLIC_MAX_PERIOD;
588 u32 en_tc2 = 0;
589
Arnd Bergmann5f03c392016-09-06 15:17:49 +0200590 dev_dbg(chan->device->dev, "%s: buf %pad, dst %pad, buf len %zu, period_len = %zu, dir %d\n",
591 __func__, &buf_addr, &to_k3_chan(chan)->dev_addr,
592 buf_len, period_len, (int)dir);
Andy Greena7e08fa2016-08-29 10:30:52 -0700593
594 avail = buf_len;
595 if (avail > modulo)
596 num += DIV_ROUND_UP(avail, modulo) - 1;
597
598 ds = k3_dma_alloc_desc_resource(num, chan);
599 if (!ds)
600 return NULL;
601
602 c->cyclic = 1;
603 addr = buf_addr;
604 avail = buf_len;
605 total = avail;
606 num = 0;
607
608 if (period_len < modulo)
609 modulo = period_len;
610
611 do {
612 len = min_t(size_t, avail, modulo);
613
614 if (dir == DMA_MEM_TO_DEV) {
615 src = addr;
616 dst = c->dev_addr;
617 } else if (dir == DMA_DEV_TO_MEM) {
618 src = c->dev_addr;
619 dst = addr;
620 }
621 since += len;
622 if (since >= period_len) {
623 /* descriptor asks for TC2 interrupt on completion */
624 en_tc2 = CX_CFG_NODEIRQ;
625 since -= period_len;
626 } else
627 en_tc2 = 0;
628
629 k3_dma_fill_desc(ds, dst, src, len, num++, c->ccfg | en_tc2);
630
631 addr += len;
632 avail -= len;
633 } while (avail);
634
635 /* "Cyclic" == end of link points back to start of link */
636 ds->desc_hw[num - 1].lli |= ds->desc_hw_lli;
637
638 ds->size = total;
639
640 return vchan_tx_prep(&c->vc, &ds->vd, flags);
641}
642
Maxime Riparddb084252014-11-17 14:42:20 +0100643static int k3_dma_config(struct dma_chan *chan,
644 struct dma_slave_config *cfg)
645{
646 struct k3_dma_chan *c = to_k3_chan(chan);
647 u32 maxburst = 0, val = 0;
648 enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
649
650 if (cfg == NULL)
651 return -EINVAL;
652 c->dir = cfg->direction;
653 if (c->dir == DMA_DEV_TO_MEM) {
654 c->ccfg = CX_CFG_DSTINCR;
655 c->dev_addr = cfg->src_addr;
656 maxburst = cfg->src_maxburst;
657 width = cfg->src_addr_width;
658 } else if (c->dir == DMA_MEM_TO_DEV) {
659 c->ccfg = CX_CFG_SRCINCR;
660 c->dev_addr = cfg->dst_addr;
661 maxburst = cfg->dst_maxburst;
662 width = cfg->dst_addr_width;
663 }
664 switch (width) {
665 case DMA_SLAVE_BUSWIDTH_1_BYTE:
666 case DMA_SLAVE_BUSWIDTH_2_BYTES:
667 case DMA_SLAVE_BUSWIDTH_4_BYTES:
668 case DMA_SLAVE_BUSWIDTH_8_BYTES:
669 val = __ffs(width);
670 break;
671 default:
672 val = 3;
673 break;
674 }
675 c->ccfg |= (val << 12) | (val << 16);
676
677 if ((maxburst == 0) || (maxburst > 16))
Andy Green6c28a902016-08-29 10:30:47 -0700678 val = 15;
Maxime Riparddb084252014-11-17 14:42:20 +0100679 else
680 val = maxburst - 1;
681 c->ccfg |= (val << 20) | (val << 24);
682 c->ccfg |= CX_CFG_MEM2PER | CX_CFG_EN;
683
684 /* specific request line */
685 c->ccfg |= c->vc.chan.chan_id << 4;
686
687 return 0;
688}
689
John Stultz36387a22016-08-29 10:30:51 -0700690static void k3_dma_free_desc(struct virt_dma_desc *vd)
691{
692 struct k3_dma_desc_sw *ds =
693 container_of(vd, struct k3_dma_desc_sw, vd);
694 struct k3_dma_dev *d = to_k3_dma(vd->tx.chan->device);
695
696 dma_pool_free(d->pool, ds->desc_hw, ds->desc_hw_lli);
697 kfree(ds);
698}
699
Maxime Riparddb084252014-11-17 14:42:20 +0100700static int k3_dma_terminate_all(struct dma_chan *chan)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800701{
702 struct k3_dma_chan *c = to_k3_chan(chan);
703 struct k3_dma_dev *d = to_k3_dma(chan->device);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800704 struct k3_dma_phy *p = c->phy;
705 unsigned long flags;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800706 LIST_HEAD(head);
707
Maxime Riparddb084252014-11-17 14:42:20 +0100708 dev_dbg(d->slave.dev, "vchan %p: terminate all\n", &c->vc);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800709
Maxime Riparddb084252014-11-17 14:42:20 +0100710 /* Prevent this channel being scheduled */
711 spin_lock(&d->lock);
712 list_del_init(&c->node);
713 spin_unlock(&d->lock);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800714
Maxime Riparddb084252014-11-17 14:42:20 +0100715 /* Clear the tx descriptor lists */
716 spin_lock_irqsave(&c->vc.lock, flags);
717 vchan_get_all_descriptors(&c->vc, &head);
718 if (p) {
719 /* vchan is assigned to a pchan - stop the channel */
720 k3_dma_terminate_chan(p, d);
721 c->phy = NULL;
722 p->vchan = NULL;
John Stultz36387a22016-08-29 10:30:51 -0700723 if (p->ds_run) {
724 k3_dma_free_desc(&p->ds_run->vd);
725 p->ds_run = NULL;
726 }
727 if (p->ds_done) {
728 k3_dma_free_desc(&p->ds_done->vd);
729 p->ds_done = NULL;
730 }
731
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800732 }
Maxime Riparddb084252014-11-17 14:42:20 +0100733 spin_unlock_irqrestore(&c->vc.lock, flags);
734 vchan_dma_desc_free_list(&c->vc, &head);
735
736 return 0;
737}
738
Krzysztof Kozlowskia1a9bec2014-12-29 14:01:30 +0100739static int k3_dma_transfer_pause(struct dma_chan *chan)
Maxime Riparddb084252014-11-17 14:42:20 +0100740{
741 struct k3_dma_chan *c = to_k3_chan(chan);
742 struct k3_dma_dev *d = to_k3_dma(chan->device);
743 struct k3_dma_phy *p = c->phy;
744
745 dev_dbg(d->slave.dev, "vchan %p: pause\n", &c->vc);
746 if (c->status == DMA_IN_PROGRESS) {
747 c->status = DMA_PAUSED;
748 if (p) {
749 k3_dma_pause_dma(p, false);
750 } else {
751 spin_lock(&d->lock);
752 list_del_init(&c->node);
753 spin_unlock(&d->lock);
754 }
755 }
756
757 return 0;
758}
759
Krzysztof Kozlowskia1a9bec2014-12-29 14:01:30 +0100760static int k3_dma_transfer_resume(struct dma_chan *chan)
Maxime Riparddb084252014-11-17 14:42:20 +0100761{
762 struct k3_dma_chan *c = to_k3_chan(chan);
763 struct k3_dma_dev *d = to_k3_dma(chan->device);
764 struct k3_dma_phy *p = c->phy;
765 unsigned long flags;
766
767 dev_dbg(d->slave.dev, "vchan %p: resume\n", &c->vc);
768 spin_lock_irqsave(&c->vc.lock, flags);
769 if (c->status == DMA_PAUSED) {
770 c->status = DMA_IN_PROGRESS;
771 if (p) {
772 k3_dma_pause_dma(p, true);
773 } else if (!list_empty(&c->vc.desc_issued)) {
774 spin_lock(&d->lock);
775 list_add_tail(&c->node, &d->chan_pending);
776 spin_unlock(&d->lock);
777 }
778 }
779 spin_unlock_irqrestore(&c->vc.lock, flags);
780
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800781 return 0;
782}
783
Fabian Frederick57c03422015-03-16 20:17:14 +0100784static const struct of_device_id k3_pdma_dt_ids[] = {
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800785 { .compatible = "hisilicon,k3-dma-1.0", },
786 {}
787};
788MODULE_DEVICE_TABLE(of, k3_pdma_dt_ids);
789
790static struct dma_chan *k3_of_dma_simple_xlate(struct of_phandle_args *dma_spec,
791 struct of_dma *ofdma)
792{
793 struct k3_dma_dev *d = ofdma->of_dma_data;
794 unsigned int request = dma_spec->args[0];
795
796 if (request > d->dma_requests)
797 return NULL;
798
799 return dma_get_slave_channel(&(d->chans[request].vc.chan));
800}
801
802static int k3_dma_probe(struct platform_device *op)
803{
804 struct k3_dma_dev *d;
805 const struct of_device_id *of_id;
806 struct resource *iores;
807 int i, ret, irq = 0;
808
809 iores = platform_get_resource(op, IORESOURCE_MEM, 0);
810 if (!iores)
811 return -EINVAL;
812
813 d = devm_kzalloc(&op->dev, sizeof(*d), GFP_KERNEL);
814 if (!d)
815 return -ENOMEM;
816
Jingoo Hana576b7f2013-09-02 10:25:13 +0900817 d->base = devm_ioremap_resource(&op->dev, iores);
818 if (IS_ERR(d->base))
819 return PTR_ERR(d->base);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800820
821 of_id = of_match_device(k3_pdma_dt_ids, &op->dev);
822 if (of_id) {
823 of_property_read_u32((&op->dev)->of_node,
824 "dma-channels", &d->dma_channels);
825 of_property_read_u32((&op->dev)->of_node,
826 "dma-requests", &d->dma_requests);
827 }
828
829 d->clk = devm_clk_get(&op->dev, NULL);
830 if (IS_ERR(d->clk)) {
831 dev_err(&op->dev, "no dma clk\n");
832 return PTR_ERR(d->clk);
833 }
834
835 irq = platform_get_irq(op, 0);
836 ret = devm_request_irq(&op->dev, irq,
Michael Opdenacker174b5372013-10-13 07:10:51 +0200837 k3_dma_int_handler, 0, DRIVER_NAME, d);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800838 if (ret)
839 return ret;
840
Vinod Koul486b10a2016-07-03 00:02:29 +0530841 d->irq = irq;
842
John Stultzb77f2622016-08-29 10:30:50 -0700843 /* A DMA memory pool for LLIs, align on 32-byte boundary */
844 d->pool = dmam_pool_create(DRIVER_NAME, &op->dev,
845 LLI_BLOCK_SIZE, 32, 0);
846 if (!d->pool)
847 return -ENOMEM;
848
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800849 /* init phy channel */
850 d->phy = devm_kzalloc(&op->dev,
851 d->dma_channels * sizeof(struct k3_dma_phy), GFP_KERNEL);
852 if (d->phy == NULL)
853 return -ENOMEM;
854
855 for (i = 0; i < d->dma_channels; i++) {
856 struct k3_dma_phy *p = &d->phy[i];
857
858 p->idx = i;
859 p->base = d->base + i * 0x40;
860 }
861
862 INIT_LIST_HEAD(&d->slave.channels);
863 dma_cap_set(DMA_SLAVE, d->slave.cap_mask);
864 dma_cap_set(DMA_MEMCPY, d->slave.cap_mask);
Andy Greena7e08fa2016-08-29 10:30:52 -0700865 dma_cap_set(DMA_CYCLIC, d->slave.cap_mask);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800866 d->slave.dev = &op->dev;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800867 d->slave.device_free_chan_resources = k3_dma_free_chan_resources;
868 d->slave.device_tx_status = k3_dma_tx_status;
869 d->slave.device_prep_dma_memcpy = k3_dma_prep_memcpy;
870 d->slave.device_prep_slave_sg = k3_dma_prep_slave_sg;
Andy Greena7e08fa2016-08-29 10:30:52 -0700871 d->slave.device_prep_dma_cyclic = k3_dma_prep_dma_cyclic;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800872 d->slave.device_issue_pending = k3_dma_issue_pending;
Maxime Riparddb084252014-11-17 14:42:20 +0100873 d->slave.device_config = k3_dma_config;
Krzysztof Kozlowskia1a9bec2014-12-29 14:01:30 +0100874 d->slave.device_pause = k3_dma_transfer_pause;
875 d->slave.device_resume = k3_dma_transfer_resume;
Maxime Riparddb084252014-11-17 14:42:20 +0100876 d->slave.device_terminate_all = k3_dma_terminate_all;
Maxime Ripard77a68e52015-07-20 10:41:32 +0200877 d->slave.copy_align = DMAENGINE_ALIGN_8_BYTES;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800878
879 /* init virtual channel */
880 d->chans = devm_kzalloc(&op->dev,
881 d->dma_requests * sizeof(struct k3_dma_chan), GFP_KERNEL);
882 if (d->chans == NULL)
883 return -ENOMEM;
884
885 for (i = 0; i < d->dma_requests; i++) {
886 struct k3_dma_chan *c = &d->chans[i];
887
888 c->status = DMA_IN_PROGRESS;
889 INIT_LIST_HEAD(&c->node);
890 c->vc.desc_free = k3_dma_free_desc;
891 vchan_init(&c->vc, &d->slave);
892 }
893
894 /* Enable clock before accessing registers */
895 ret = clk_prepare_enable(d->clk);
896 if (ret < 0) {
897 dev_err(&op->dev, "clk_prepare_enable failed: %d\n", ret);
898 return ret;
899 }
900
901 k3_dma_enable_dma(d, true);
902
903 ret = dma_async_device_register(&d->slave);
904 if (ret)
Wei Yongjun89b90c02016-07-19 11:29:41 +0000905 goto dma_async_register_fail;
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800906
907 ret = of_dma_controller_register((&op->dev)->of_node,
908 k3_of_dma_simple_xlate, d);
909 if (ret)
910 goto of_dma_register_fail;
911
912 spin_lock_init(&d->lock);
913 INIT_LIST_HEAD(&d->chan_pending);
914 tasklet_init(&d->task, k3_dma_tasklet, (unsigned long)d);
915 platform_set_drvdata(op, d);
916 dev_info(&op->dev, "initialized\n");
917
918 return 0;
919
920of_dma_register_fail:
921 dma_async_device_unregister(&d->slave);
Wei Yongjun89b90c02016-07-19 11:29:41 +0000922dma_async_register_fail:
923 clk_disable_unprepare(d->clk);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800924 return ret;
925}
926
927static int k3_dma_remove(struct platform_device *op)
928{
929 struct k3_dma_chan *c, *cn;
930 struct k3_dma_dev *d = platform_get_drvdata(op);
931
932 dma_async_device_unregister(&d->slave);
933 of_dma_controller_free((&op->dev)->of_node);
934
Vinod Koul486b10a2016-07-03 00:02:29 +0530935 devm_free_irq(&op->dev, d->irq, d);
936
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800937 list_for_each_entry_safe(c, cn, &d->slave.channels, vc.chan.device_node) {
938 list_del(&c->vc.chan.device_node);
939 tasklet_kill(&c->vc.task);
940 }
941 tasklet_kill(&d->task);
942 clk_disable_unprepare(d->clk);
943 return 0;
944}
945
Jingoo Hanaf2d3132014-10-27 21:36:26 +0900946#ifdef CONFIG_PM_SLEEP
Arnd Bergmann10b3e222015-01-13 14:23:13 +0100947static int k3_dma_suspend_dev(struct device *dev)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800948{
949 struct k3_dma_dev *d = dev_get_drvdata(dev);
950 u32 stat = 0;
951
952 stat = k3_dma_get_chan_stat(d);
953 if (stat) {
954 dev_warn(d->slave.dev,
955 "chan %d is running fail to suspend\n", stat);
956 return -1;
957 }
958 k3_dma_enable_dma(d, false);
959 clk_disable_unprepare(d->clk);
960 return 0;
961}
962
Arnd Bergmann10b3e222015-01-13 14:23:13 +0100963static int k3_dma_resume_dev(struct device *dev)
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800964{
965 struct k3_dma_dev *d = dev_get_drvdata(dev);
966 int ret = 0;
967
968 ret = clk_prepare_enable(d->clk);
969 if (ret < 0) {
970 dev_err(d->slave.dev, "clk_prepare_enable failed: %d\n", ret);
971 return ret;
972 }
973 k3_dma_enable_dma(d, true);
974 return 0;
975}
Jingoo Hanaf2d3132014-10-27 21:36:26 +0900976#endif
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800977
Arnd Bergmann10b3e222015-01-13 14:23:13 +0100978static SIMPLE_DEV_PM_OPS(k3_dma_pmops, k3_dma_suspend_dev, k3_dma_resume_dev);
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800979
980static struct platform_driver k3_pdma_driver = {
981 .driver = {
982 .name = DRIVER_NAME,
Zhangfei Gao8e6152b2013-08-27 10:20:10 +0800983 .pm = &k3_dma_pmops,
984 .of_match_table = k3_pdma_dt_ids,
985 },
986 .probe = k3_dma_probe,
987 .remove = k3_dma_remove,
988};
989
990module_platform_driver(k3_pdma_driver);
991
992MODULE_DESCRIPTION("Hisilicon k3 DMA Driver");
993MODULE_ALIAS("platform:k3dma");
994MODULE_LICENSE("GPL v2");