blob: 0d55c8fc49513d242bfdd47a3d6c976b06399ed7 [file] [log] [blame]
Jun Niee3fa9842015-05-05 22:06:08 +08001/*
2 * Copyright 2015 Linaro.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8#include <linux/sched.h>
9#include <linux/device.h>
10#include <linux/dmaengine.h>
11#include <linux/dma-mapping.h>
12#include <linux/dmapool.h>
13#include <linux/init.h>
14#include <linux/interrupt.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/slab.h>
19#include <linux/spinlock.h>
20#include <linux/of_device.h>
21#include <linux/of.h>
22#include <linux/clk.h>
23#include <linux/of_dma.h>
24
25#include "virt-dma.h"
26
27#define DRIVER_NAME "zx-dma"
28#define DMA_ALIGN 4
29#define DMA_MAX_SIZE (0x10000 - PAGE_SIZE)
30#define LLI_BLOCK_SIZE (4 * PAGE_SIZE)
31
32#define REG_ZX_SRC_ADDR 0x00
33#define REG_ZX_DST_ADDR 0x04
34#define REG_ZX_TX_X_COUNT 0x08
35#define REG_ZX_TX_ZY_COUNT 0x0c
36#define REG_ZX_SRC_ZY_STEP 0x10
37#define REG_ZX_DST_ZY_STEP 0x14
38#define REG_ZX_LLI_ADDR 0x1c
39#define REG_ZX_CTRL 0x20
40#define REG_ZX_TC_IRQ 0x800
41#define REG_ZX_SRC_ERR_IRQ 0x804
42#define REG_ZX_DST_ERR_IRQ 0x808
43#define REG_ZX_CFG_ERR_IRQ 0x80c
44#define REG_ZX_TC_IRQ_RAW 0x810
45#define REG_ZX_SRC_ERR_IRQ_RAW 0x814
46#define REG_ZX_DST_ERR_IRQ_RAW 0x818
47#define REG_ZX_CFG_ERR_IRQ_RAW 0x81c
48#define REG_ZX_STATUS 0x820
49#define REG_ZX_DMA_GRP_PRIO 0x824
50#define REG_ZX_DMA_ARB 0x828
51
52#define ZX_FORCE_CLOSE BIT(31)
53#define ZX_DST_BURST_WIDTH(x) (((x) & 0x7) << 13)
54#define ZX_MAX_BURST_LEN 16
55#define ZX_SRC_BURST_LEN(x) (((x) & 0xf) << 9)
56#define ZX_SRC_BURST_WIDTH(x) (((x) & 0x7) << 6)
57#define ZX_IRQ_ENABLE_ALL (3 << 4)
58#define ZX_DST_FIFO_MODE BIT(3)
59#define ZX_SRC_FIFO_MODE BIT(2)
60#define ZX_SOFT_REQ BIT(1)
61#define ZX_CH_ENABLE BIT(0)
62
63#define ZX_DMA_BUSWIDTHS \
64 (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
65 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
66 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
67 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
68 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
69
70enum zx_dma_burst_width {
71 ZX_DMA_WIDTH_8BIT = 0,
72 ZX_DMA_WIDTH_16BIT = 1,
73 ZX_DMA_WIDTH_32BIT = 2,
74 ZX_DMA_WIDTH_64BIT = 3,
75};
76
77struct zx_desc_hw {
78 u32 saddr;
79 u32 daddr;
80 u32 src_x;
81 u32 src_zy;
82 u32 src_zy_step;
83 u32 dst_zy_step;
84 u32 reserved1;
85 u32 lli;
86 u32 ctr;
87 u32 reserved[7]; /* pack as hardware registers region size */
88} __aligned(32);
89
90struct zx_dma_desc_sw {
91 struct virt_dma_desc vd;
92 dma_addr_t desc_hw_lli;
93 size_t desc_num;
94 size_t size;
95 struct zx_desc_hw *desc_hw;
96};
97
98struct zx_dma_phy;
99
100struct zx_dma_chan {
101 struct dma_slave_config slave_cfg;
102 int id; /* Request phy chan id */
103 u32 ccfg;
Jun Nie2f2560e2015-07-21 11:01:06 +0800104 u32 cyclic;
Jun Niee3fa9842015-05-05 22:06:08 +0800105 struct virt_dma_chan vc;
106 struct zx_dma_phy *phy;
107 struct list_head node;
108 dma_addr_t dev_addr;
109 enum dma_status status;
110};
111
112struct zx_dma_phy {
113 u32 idx;
114 void __iomem *base;
115 struct zx_dma_chan *vchan;
116 struct zx_dma_desc_sw *ds_run;
117 struct zx_dma_desc_sw *ds_done;
118};
119
120struct zx_dma_dev {
121 struct dma_device slave;
122 void __iomem *base;
123 spinlock_t lock; /* lock for ch and phy */
124 struct list_head chan_pending;
125 struct zx_dma_phy *phy;
126 struct zx_dma_chan *chans;
127 struct clk *clk;
128 struct dma_pool *pool;
129 u32 dma_channels;
130 u32 dma_requests;
Vinod Koul9bde2822015-05-18 15:33:13 +0530131 int irq;
Jun Niee3fa9842015-05-05 22:06:08 +0800132};
133
134#define to_zx_dma(dmadev) container_of(dmadev, struct zx_dma_dev, slave)
135
136static struct zx_dma_chan *to_zx_chan(struct dma_chan *chan)
137{
138 return container_of(chan, struct zx_dma_chan, vc.chan);
139}
140
141static void zx_dma_terminate_chan(struct zx_dma_phy *phy, struct zx_dma_dev *d)
142{
143 u32 val = 0;
144
145 val = readl_relaxed(phy->base + REG_ZX_CTRL);
146 val &= ~ZX_CH_ENABLE;
147 writel_relaxed(val, phy->base + REG_ZX_CTRL);
148
149 val = 0x1 << phy->idx;
150 writel_relaxed(val, d->base + REG_ZX_TC_IRQ_RAW);
151 writel_relaxed(val, d->base + REG_ZX_SRC_ERR_IRQ_RAW);
152 writel_relaxed(val, d->base + REG_ZX_DST_ERR_IRQ_RAW);
153 writel_relaxed(val, d->base + REG_ZX_CFG_ERR_IRQ_RAW);
154}
155
156static void zx_dma_set_desc(struct zx_dma_phy *phy, struct zx_desc_hw *hw)
157{
158 writel_relaxed(hw->saddr, phy->base + REG_ZX_SRC_ADDR);
159 writel_relaxed(hw->daddr, phy->base + REG_ZX_DST_ADDR);
160 writel_relaxed(hw->src_x, phy->base + REG_ZX_TX_X_COUNT);
161 writel_relaxed(0, phy->base + REG_ZX_TX_ZY_COUNT);
162 writel_relaxed(0, phy->base + REG_ZX_SRC_ZY_STEP);
163 writel_relaxed(0, phy->base + REG_ZX_DST_ZY_STEP);
164 writel_relaxed(hw->lli, phy->base + REG_ZX_LLI_ADDR);
165 writel_relaxed(hw->ctr, phy->base + REG_ZX_CTRL);
166}
167
168static u32 zx_dma_get_curr_lli(struct zx_dma_phy *phy)
169{
170 return readl_relaxed(phy->base + REG_ZX_LLI_ADDR);
171}
172
173static u32 zx_dma_get_chan_stat(struct zx_dma_dev *d)
174{
175 return readl_relaxed(d->base + REG_ZX_STATUS);
176}
177
178static void zx_dma_init_state(struct zx_dma_dev *d)
179{
180 /* set same priority */
181 writel_relaxed(0x0, d->base + REG_ZX_DMA_ARB);
182 /* clear all irq */
183 writel_relaxed(0xffffffff, d->base + REG_ZX_TC_IRQ_RAW);
184 writel_relaxed(0xffffffff, d->base + REG_ZX_SRC_ERR_IRQ_RAW);
185 writel_relaxed(0xffffffff, d->base + REG_ZX_DST_ERR_IRQ_RAW);
186 writel_relaxed(0xffffffff, d->base + REG_ZX_CFG_ERR_IRQ_RAW);
187}
188
189static int zx_dma_start_txd(struct zx_dma_chan *c)
190{
191 struct zx_dma_dev *d = to_zx_dma(c->vc.chan.device);
192 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
193
194 if (!c->phy)
195 return -EAGAIN;
196
197 if (BIT(c->phy->idx) & zx_dma_get_chan_stat(d))
198 return -EAGAIN;
199
200 if (vd) {
201 struct zx_dma_desc_sw *ds =
202 container_of(vd, struct zx_dma_desc_sw, vd);
203 /*
204 * fetch and remove request from vc->desc_issued
205 * so vc->desc_issued only contains desc pending
206 */
207 list_del(&ds->vd.node);
208 c->phy->ds_run = ds;
209 c->phy->ds_done = NULL;
210 /* start dma */
211 zx_dma_set_desc(c->phy, ds->desc_hw);
212 return 0;
213 }
214 c->phy->ds_done = NULL;
215 c->phy->ds_run = NULL;
216 return -EAGAIN;
217}
218
219static void zx_dma_task(struct zx_dma_dev *d)
220{
221 struct zx_dma_phy *p;
222 struct zx_dma_chan *c, *cn;
223 unsigned pch, pch_alloc = 0;
224 unsigned long flags;
225
226 /* check new dma request of running channel in vc->desc_issued */
227 list_for_each_entry_safe(c, cn, &d->slave.channels,
228 vc.chan.device_node) {
229 spin_lock_irqsave(&c->vc.lock, flags);
230 p = c->phy;
231 if (p && p->ds_done && zx_dma_start_txd(c)) {
232 /* No current txd associated with this channel */
233 dev_dbg(d->slave.dev, "pchan %u: free\n", p->idx);
234 /* Mark this channel free */
235 c->phy = NULL;
236 p->vchan = NULL;
237 }
238 spin_unlock_irqrestore(&c->vc.lock, flags);
239 }
240
241 /* check new channel request in d->chan_pending */
242 spin_lock_irqsave(&d->lock, flags);
243 while (!list_empty(&d->chan_pending)) {
244 c = list_first_entry(&d->chan_pending,
245 struct zx_dma_chan, node);
246 p = &d->phy[c->id];
247 if (!p->vchan) {
248 /* remove from d->chan_pending */
249 list_del_init(&c->node);
250 pch_alloc |= 1 << c->id;
251 /* Mark this channel allocated */
252 p->vchan = c;
253 c->phy = p;
254 } else {
255 dev_dbg(d->slave.dev, "pchan %u: busy!\n", c->id);
256 }
257 }
258 spin_unlock_irqrestore(&d->lock, flags);
259
260 for (pch = 0; pch < d->dma_channels; pch++) {
261 if (pch_alloc & (1 << pch)) {
262 p = &d->phy[pch];
263 c = p->vchan;
264 if (c) {
265 spin_lock_irqsave(&c->vc.lock, flags);
266 zx_dma_start_txd(c);
267 spin_unlock_irqrestore(&c->vc.lock, flags);
268 }
269 }
270 }
271}
272
273static irqreturn_t zx_dma_int_handler(int irq, void *dev_id)
274{
275 struct zx_dma_dev *d = (struct zx_dma_dev *)dev_id;
276 struct zx_dma_phy *p;
277 struct zx_dma_chan *c;
278 u32 tc = readl_relaxed(d->base + REG_ZX_TC_IRQ);
279 u32 serr = readl_relaxed(d->base + REG_ZX_SRC_ERR_IRQ);
280 u32 derr = readl_relaxed(d->base + REG_ZX_DST_ERR_IRQ);
281 u32 cfg = readl_relaxed(d->base + REG_ZX_CFG_ERR_IRQ);
Jun Nie2f2560e2015-07-21 11:01:06 +0800282 u32 i, irq_chan = 0, task = 0;
Jun Niee3fa9842015-05-05 22:06:08 +0800283
284 while (tc) {
285 i = __ffs(tc);
286 tc &= ~BIT(i);
287 p = &d->phy[i];
288 c = p->vchan;
289 if (c) {
290 unsigned long flags;
291
292 spin_lock_irqsave(&c->vc.lock, flags);
Jun Nie2f2560e2015-07-21 11:01:06 +0800293 if (c->cyclic) {
294 vchan_cyclic_callback(&p->ds_run->vd);
295 } else {
296 vchan_cookie_complete(&p->ds_run->vd);
297 p->ds_done = p->ds_run;
298 task = 1;
299 }
Jun Niee3fa9842015-05-05 22:06:08 +0800300 spin_unlock_irqrestore(&c->vc.lock, flags);
Jun Nie2f2560e2015-07-21 11:01:06 +0800301 irq_chan |= BIT(i);
Jun Niee3fa9842015-05-05 22:06:08 +0800302 }
Jun Niee3fa9842015-05-05 22:06:08 +0800303 }
304
305 if (serr || derr || cfg)
306 dev_warn(d->slave.dev, "DMA ERR src 0x%x, dst 0x%x, cfg 0x%x\n",
307 serr, derr, cfg);
308
309 writel_relaxed(irq_chan, d->base + REG_ZX_TC_IRQ_RAW);
310 writel_relaxed(serr, d->base + REG_ZX_SRC_ERR_IRQ_RAW);
311 writel_relaxed(derr, d->base + REG_ZX_DST_ERR_IRQ_RAW);
312 writel_relaxed(cfg, d->base + REG_ZX_CFG_ERR_IRQ_RAW);
313
Jun Nie2f2560e2015-07-21 11:01:06 +0800314 if (task)
Jun Niee3fa9842015-05-05 22:06:08 +0800315 zx_dma_task(d);
Jun Nie2f2560e2015-07-21 11:01:06 +0800316 return IRQ_HANDLED;
Jun Niee3fa9842015-05-05 22:06:08 +0800317}
318
319static void zx_dma_free_chan_resources(struct dma_chan *chan)
320{
321 struct zx_dma_chan *c = to_zx_chan(chan);
322 struct zx_dma_dev *d = to_zx_dma(chan->device);
323 unsigned long flags;
324
325 spin_lock_irqsave(&d->lock, flags);
326 list_del_init(&c->node);
327 spin_unlock_irqrestore(&d->lock, flags);
328
329 vchan_free_chan_resources(&c->vc);
330 c->ccfg = 0;
331}
332
333static enum dma_status zx_dma_tx_status(struct dma_chan *chan,
334 dma_cookie_t cookie,
335 struct dma_tx_state *state)
336{
337 struct zx_dma_chan *c = to_zx_chan(chan);
338 struct zx_dma_phy *p;
339 struct virt_dma_desc *vd;
340 unsigned long flags;
341 enum dma_status ret;
342 size_t bytes = 0;
343
344 ret = dma_cookie_status(&c->vc.chan, cookie, state);
345 if (ret == DMA_COMPLETE || !state)
346 return ret;
347
348 spin_lock_irqsave(&c->vc.lock, flags);
349 p = c->phy;
350 ret = c->status;
351
352 /*
353 * If the cookie is on our issue queue, then the residue is
354 * its total size.
355 */
356 vd = vchan_find_desc(&c->vc, cookie);
357 if (vd) {
358 bytes = container_of(vd, struct zx_dma_desc_sw, vd)->size;
359 } else if ((!p) || (!p->ds_run)) {
360 bytes = 0;
361 } else {
362 struct zx_dma_desc_sw *ds = p->ds_run;
363 u32 clli = 0, index = 0;
364
365 bytes = 0;
366 clli = zx_dma_get_curr_lli(p);
367 index = (clli - ds->desc_hw_lli) / sizeof(struct zx_desc_hw);
368 for (; index < ds->desc_num; index++) {
369 bytes += ds->desc_hw[index].src_x;
370 /* end of lli */
371 if (!ds->desc_hw[index].lli)
372 break;
373 }
374 }
375 spin_unlock_irqrestore(&c->vc.lock, flags);
376 dma_set_residue(state, bytes);
377 return ret;
378}
379
380static void zx_dma_issue_pending(struct dma_chan *chan)
381{
382 struct zx_dma_chan *c = to_zx_chan(chan);
383 struct zx_dma_dev *d = to_zx_dma(chan->device);
384 unsigned long flags;
385 int issue = 0;
386
387 spin_lock_irqsave(&c->vc.lock, flags);
388 /* add request to vc->desc_issued */
389 if (vchan_issue_pending(&c->vc)) {
390 spin_lock(&d->lock);
391 if (!c->phy && list_empty(&c->node)) {
392 /* if new channel, add chan_pending */
393 list_add_tail(&c->node, &d->chan_pending);
394 issue = 1;
395 dev_dbg(d->slave.dev, "vchan %p: issued\n", &c->vc);
396 }
397 spin_unlock(&d->lock);
398 } else {
399 dev_dbg(d->slave.dev, "vchan %p: nothing to issue\n", &c->vc);
400 }
401 spin_unlock_irqrestore(&c->vc.lock, flags);
402
403 if (issue)
404 zx_dma_task(d);
405}
406
407static void zx_dma_fill_desc(struct zx_dma_desc_sw *ds, dma_addr_t dst,
408 dma_addr_t src, size_t len, u32 num, u32 ccfg)
409{
410 if ((num + 1) < ds->desc_num)
411 ds->desc_hw[num].lli = ds->desc_hw_lli + (num + 1) *
412 sizeof(struct zx_desc_hw);
413 ds->desc_hw[num].saddr = src;
414 ds->desc_hw[num].daddr = dst;
415 ds->desc_hw[num].src_x = len;
416 ds->desc_hw[num].ctr = ccfg;
417}
418
419static struct zx_dma_desc_sw *zx_alloc_desc_resource(int num,
420 struct dma_chan *chan)
421{
422 struct zx_dma_chan *c = to_zx_chan(chan);
423 struct zx_dma_desc_sw *ds;
424 struct zx_dma_dev *d = to_zx_dma(chan->device);
425 int lli_limit = LLI_BLOCK_SIZE / sizeof(struct zx_desc_hw);
426
427 if (num > lli_limit) {
428 dev_dbg(chan->device->dev, "vch %p: sg num %d exceed max %d\n",
429 &c->vc, num, lli_limit);
430 return NULL;
431 }
432
433 ds = kzalloc(sizeof(*ds), GFP_ATOMIC);
434 if (!ds)
435 return NULL;
436
437 ds->desc_hw = dma_pool_alloc(d->pool, GFP_NOWAIT, &ds->desc_hw_lli);
438 if (!ds->desc_hw) {
439 dev_dbg(chan->device->dev, "vch %p: dma alloc fail\n", &c->vc);
440 kfree(ds);
441 return NULL;
442 }
443 memset(ds->desc_hw, sizeof(struct zx_desc_hw) * num, 0);
444 ds->desc_num = num;
445 return ds;
446}
447
448static enum zx_dma_burst_width zx_dma_burst_width(enum dma_slave_buswidth width)
449{
450 switch (width) {
451 case DMA_SLAVE_BUSWIDTH_1_BYTE:
452 case DMA_SLAVE_BUSWIDTH_2_BYTES:
453 case DMA_SLAVE_BUSWIDTH_4_BYTES:
454 case DMA_SLAVE_BUSWIDTH_8_BYTES:
455 return ffs(width) - 1;
456 default:
457 return ZX_DMA_WIDTH_32BIT;
458 }
459}
460
461static int zx_pre_config(struct zx_dma_chan *c, enum dma_transfer_direction dir)
462{
463 struct dma_slave_config *cfg = &c->slave_cfg;
464 enum zx_dma_burst_width src_width;
465 enum zx_dma_burst_width dst_width;
466 u32 maxburst = 0;
467
468 switch (dir) {
469 case DMA_MEM_TO_MEM:
470 c->ccfg = ZX_CH_ENABLE | ZX_SOFT_REQ
471 | ZX_SRC_BURST_LEN(ZX_MAX_BURST_LEN - 1)
472 | ZX_SRC_BURST_WIDTH(ZX_DMA_WIDTH_32BIT)
473 | ZX_DST_BURST_WIDTH(ZX_DMA_WIDTH_32BIT);
474 break;
475 case DMA_MEM_TO_DEV:
476 c->dev_addr = cfg->dst_addr;
477 /* dst len is calculated from src width, len and dst width.
478 * We need make sure dst len not exceed MAX LEN.
479 */
480 dst_width = zx_dma_burst_width(cfg->dst_addr_width);
481 maxburst = cfg->dst_maxburst * cfg->dst_addr_width
482 / DMA_SLAVE_BUSWIDTH_8_BYTES;
483 maxburst = maxburst < ZX_MAX_BURST_LEN ?
484 maxburst : ZX_MAX_BURST_LEN;
485 c->ccfg = ZX_DST_FIFO_MODE | ZX_CH_ENABLE
486 | ZX_SRC_BURST_LEN(maxburst - 1)
487 | ZX_SRC_BURST_WIDTH(ZX_DMA_WIDTH_64BIT)
488 | ZX_DST_BURST_WIDTH(dst_width);
489 break;
490 case DMA_DEV_TO_MEM:
491 c->dev_addr = cfg->src_addr;
492 src_width = zx_dma_burst_width(cfg->src_addr_width);
493 maxburst = cfg->src_maxburst;
494 maxburst = maxburst < ZX_MAX_BURST_LEN ?
495 maxburst : ZX_MAX_BURST_LEN;
496 c->ccfg = ZX_SRC_FIFO_MODE | ZX_CH_ENABLE
497 | ZX_SRC_BURST_LEN(maxburst - 1)
498 | ZX_SRC_BURST_WIDTH(src_width)
499 | ZX_DST_BURST_WIDTH(ZX_DMA_WIDTH_64BIT);
500 break;
501 default:
502 return -EINVAL;
503 }
504 return 0;
505}
506
507static struct dma_async_tx_descriptor *zx_dma_prep_memcpy(
508 struct dma_chan *chan, dma_addr_t dst, dma_addr_t src,
509 size_t len, unsigned long flags)
510{
511 struct zx_dma_chan *c = to_zx_chan(chan);
512 struct zx_dma_desc_sw *ds;
513 size_t copy = 0;
514 int num = 0;
515
516 if (!len)
517 return NULL;
518
519 if (zx_pre_config(c, DMA_MEM_TO_MEM))
520 return NULL;
521
522 num = DIV_ROUND_UP(len, DMA_MAX_SIZE);
523
524 ds = zx_alloc_desc_resource(num, chan);
525 if (!ds)
526 return NULL;
527
528 ds->size = len;
529 num = 0;
530
531 do {
532 copy = min_t(size_t, len, DMA_MAX_SIZE);
533 zx_dma_fill_desc(ds, dst, src, copy, num++, c->ccfg);
534
535 src += copy;
536 dst += copy;
537 len -= copy;
538 } while (len);
539
Jun Nie2f2560e2015-07-21 11:01:06 +0800540 c->cyclic = 0;
Jun Niee3fa9842015-05-05 22:06:08 +0800541 ds->desc_hw[num - 1].lli = 0; /* end of link */
542 ds->desc_hw[num - 1].ctr |= ZX_IRQ_ENABLE_ALL;
543 return vchan_tx_prep(&c->vc, &ds->vd, flags);
544}
545
546static struct dma_async_tx_descriptor *zx_dma_prep_slave_sg(
547 struct dma_chan *chan, struct scatterlist *sgl, unsigned int sglen,
548 enum dma_transfer_direction dir, unsigned long flags, void *context)
549{
550 struct zx_dma_chan *c = to_zx_chan(chan);
551 struct zx_dma_desc_sw *ds;
552 size_t len, avail, total = 0;
553 struct scatterlist *sg;
554 dma_addr_t addr, src = 0, dst = 0;
555 int num = sglen, i;
556
557 if (!sgl)
558 return NULL;
559
560 if (zx_pre_config(c, dir))
561 return NULL;
562
563 for_each_sg(sgl, sg, sglen, i) {
564 avail = sg_dma_len(sg);
565 if (avail > DMA_MAX_SIZE)
566 num += DIV_ROUND_UP(avail, DMA_MAX_SIZE) - 1;
567 }
568
569 ds = zx_alloc_desc_resource(num, chan);
570 if (!ds)
571 return NULL;
572
Jun Nie2f2560e2015-07-21 11:01:06 +0800573 c->cyclic = 0;
Jun Niee3fa9842015-05-05 22:06:08 +0800574 num = 0;
575 for_each_sg(sgl, sg, sglen, i) {
576 addr = sg_dma_address(sg);
577 avail = sg_dma_len(sg);
578 total += avail;
579
580 do {
581 len = min_t(size_t, avail, DMA_MAX_SIZE);
582
583 if (dir == DMA_MEM_TO_DEV) {
584 src = addr;
585 dst = c->dev_addr;
586 } else if (dir == DMA_DEV_TO_MEM) {
587 src = c->dev_addr;
588 dst = addr;
589 }
590
591 zx_dma_fill_desc(ds, dst, src, len, num++, c->ccfg);
592
593 addr += len;
594 avail -= len;
595 } while (avail);
596 }
597
598 ds->desc_hw[num - 1].lli = 0; /* end of link */
599 ds->desc_hw[num - 1].ctr |= ZX_IRQ_ENABLE_ALL;
600 ds->size = total;
601 return vchan_tx_prep(&c->vc, &ds->vd, flags);
602}
603
Jun Nie2f2560e2015-07-21 11:01:06 +0800604static struct dma_async_tx_descriptor *zx_dma_prep_dma_cyclic(
605 struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
606 size_t period_len, enum dma_transfer_direction dir,
607 unsigned long flags)
608{
609 struct zx_dma_chan *c = to_zx_chan(chan);
610 struct zx_dma_desc_sw *ds;
611 dma_addr_t src = 0, dst = 0;
612 int num_periods = buf_len / period_len;
613 int buf = 0, num = 0;
614
615 if (period_len > DMA_MAX_SIZE) {
616 dev_err(chan->device->dev, "maximum period size exceeded\n");
617 return NULL;
618 }
619
620 if (zx_pre_config(c, dir))
621 return NULL;
622
623 ds = zx_alloc_desc_resource(num_periods, chan);
624 if (!ds)
625 return NULL;
626 c->cyclic = 1;
627
628 while (buf < buf_len) {
629 if (dir == DMA_MEM_TO_DEV) {
630 src = dma_addr;
631 dst = c->dev_addr;
632 } else if (dir == DMA_DEV_TO_MEM) {
633 src = c->dev_addr;
634 dst = dma_addr;
635 }
636 zx_dma_fill_desc(ds, dst, src, period_len, num++,
637 c->ccfg | ZX_IRQ_ENABLE_ALL);
638 dma_addr += period_len;
639 buf += period_len;
640 }
641
642 ds->desc_hw[num - 1].lli = ds->desc_hw_lli;
643 ds->size = buf_len;
644 return vchan_tx_prep(&c->vc, &ds->vd, flags);
645}
646
Jun Niee3fa9842015-05-05 22:06:08 +0800647static int zx_dma_config(struct dma_chan *chan,
648 struct dma_slave_config *cfg)
649{
650 struct zx_dma_chan *c = to_zx_chan(chan);
651
652 if (!cfg)
653 return -EINVAL;
654
655 memcpy(&c->slave_cfg, cfg, sizeof(*cfg));
656
657 return 0;
658}
659
660static int zx_dma_terminate_all(struct dma_chan *chan)
661{
662 struct zx_dma_chan *c = to_zx_chan(chan);
663 struct zx_dma_dev *d = to_zx_dma(chan->device);
664 struct zx_dma_phy *p = c->phy;
665 unsigned long flags;
666 LIST_HEAD(head);
667
668 dev_dbg(d->slave.dev, "vchan %p: terminate all\n", &c->vc);
669
670 /* Prevent this channel being scheduled */
671 spin_lock(&d->lock);
672 list_del_init(&c->node);
673 spin_unlock(&d->lock);
674
675 /* Clear the tx descriptor lists */
676 spin_lock_irqsave(&c->vc.lock, flags);
677 vchan_get_all_descriptors(&c->vc, &head);
678 if (p) {
679 /* vchan is assigned to a pchan - stop the channel */
680 zx_dma_terminate_chan(p, d);
681 c->phy = NULL;
682 p->vchan = NULL;
683 p->ds_run = NULL;
684 p->ds_done = NULL;
685 }
686 spin_unlock_irqrestore(&c->vc.lock, flags);
687 vchan_dma_desc_free_list(&c->vc, &head);
688
689 return 0;
690}
691
Jun Nie2f2560e2015-07-21 11:01:06 +0800692static int zx_dma_transfer_pause(struct dma_chan *chan)
693{
694 struct zx_dma_chan *c = to_zx_chan(chan);
695 u32 val = 0;
696
697 val = readl_relaxed(c->phy->base + REG_ZX_CTRL);
698 val &= ~ZX_CH_ENABLE;
699 writel_relaxed(val, c->phy->base + REG_ZX_CTRL);
700
701 return 0;
702}
703
704static int zx_dma_transfer_resume(struct dma_chan *chan)
705{
706 struct zx_dma_chan *c = to_zx_chan(chan);
707 u32 val = 0;
708
709 val = readl_relaxed(c->phy->base + REG_ZX_CTRL);
710 val |= ZX_CH_ENABLE;
711 writel_relaxed(val, c->phy->base + REG_ZX_CTRL);
712
713 return 0;
714}
715
Jun Niee3fa9842015-05-05 22:06:08 +0800716static void zx_dma_free_desc(struct virt_dma_desc *vd)
717{
718 struct zx_dma_desc_sw *ds =
719 container_of(vd, struct zx_dma_desc_sw, vd);
720 struct zx_dma_dev *d = to_zx_dma(vd->tx.chan->device);
721
722 dma_pool_free(d->pool, ds->desc_hw, ds->desc_hw_lli);
723 kfree(ds);
724}
725
726static const struct of_device_id zx6702_dma_dt_ids[] = {
727 { .compatible = "zte,zx296702-dma", },
728 {}
729};
730MODULE_DEVICE_TABLE(of, zx6702_dma_dt_ids);
731
732static struct dma_chan *zx_of_dma_simple_xlate(struct of_phandle_args *dma_spec,
733 struct of_dma *ofdma)
734{
735 struct zx_dma_dev *d = ofdma->of_dma_data;
736 unsigned int request = dma_spec->args[0];
737 struct dma_chan *chan;
738 struct zx_dma_chan *c;
739
740 if (request > d->dma_requests)
741 return NULL;
742
743 chan = dma_get_any_slave_channel(&d->slave);
744 if (!chan) {
745 dev_err(d->slave.dev, "get channel fail in %s.\n", __func__);
746 return NULL;
747 }
748 c = to_zx_chan(chan);
749 c->id = request;
750 dev_info(d->slave.dev, "zx_dma: pchan %u: alloc vchan %p\n",
751 c->id, &c->vc);
752 return chan;
753}
754
755static int zx_dma_probe(struct platform_device *op)
756{
757 struct zx_dma_dev *d;
758 struct resource *iores;
Vinod Koul9bde2822015-05-18 15:33:13 +0530759 int i, ret = 0;
Jun Niee3fa9842015-05-05 22:06:08 +0800760
761 iores = platform_get_resource(op, IORESOURCE_MEM, 0);
762 if (!iores)
763 return -EINVAL;
764
765 d = devm_kzalloc(&op->dev, sizeof(*d), GFP_KERNEL);
766 if (!d)
767 return -ENOMEM;
768
769 d->base = devm_ioremap_resource(&op->dev, iores);
770 if (IS_ERR(d->base))
771 return PTR_ERR(d->base);
772
773 of_property_read_u32((&op->dev)->of_node,
774 "dma-channels", &d->dma_channels);
775 of_property_read_u32((&op->dev)->of_node,
776 "dma-requests", &d->dma_requests);
777 if (!d->dma_requests || !d->dma_channels)
778 return -EINVAL;
779
780 d->clk = devm_clk_get(&op->dev, NULL);
781 if (IS_ERR(d->clk)) {
782 dev_err(&op->dev, "no dma clk\n");
783 return PTR_ERR(d->clk);
784 }
785
Vinod Koul9bde2822015-05-18 15:33:13 +0530786 d->irq = platform_get_irq(op, 0);
787 ret = devm_request_irq(&op->dev, d->irq, zx_dma_int_handler,
Jun Niee3fa9842015-05-05 22:06:08 +0800788 0, DRIVER_NAME, d);
789 if (ret)
790 return ret;
791
792 /* A DMA memory pool for LLIs, align on 32-byte boundary */
793 d->pool = dmam_pool_create(DRIVER_NAME, &op->dev,
794 LLI_BLOCK_SIZE, 32, 0);
795 if (!d->pool)
796 return -ENOMEM;
797
798 /* init phy channel */
799 d->phy = devm_kzalloc(&op->dev,
800 d->dma_channels * sizeof(struct zx_dma_phy), GFP_KERNEL);
801 if (!d->phy)
802 return -ENOMEM;
803
804 for (i = 0; i < d->dma_channels; i++) {
805 struct zx_dma_phy *p = &d->phy[i];
806
807 p->idx = i;
808 p->base = d->base + i * 0x40;
809 }
810
811 INIT_LIST_HEAD(&d->slave.channels);
812 dma_cap_set(DMA_SLAVE, d->slave.cap_mask);
813 dma_cap_set(DMA_MEMCPY, d->slave.cap_mask);
814 dma_cap_set(DMA_PRIVATE, d->slave.cap_mask);
815 d->slave.dev = &op->dev;
816 d->slave.device_free_chan_resources = zx_dma_free_chan_resources;
817 d->slave.device_tx_status = zx_dma_tx_status;
818 d->slave.device_prep_dma_memcpy = zx_dma_prep_memcpy;
819 d->slave.device_prep_slave_sg = zx_dma_prep_slave_sg;
Jun Nie2f2560e2015-07-21 11:01:06 +0800820 d->slave.device_prep_dma_cyclic = zx_dma_prep_dma_cyclic;
Jun Niee3fa9842015-05-05 22:06:08 +0800821 d->slave.device_issue_pending = zx_dma_issue_pending;
822 d->slave.device_config = zx_dma_config;
823 d->slave.device_terminate_all = zx_dma_terminate_all;
Jun Nie2f2560e2015-07-21 11:01:06 +0800824 d->slave.device_pause = zx_dma_transfer_pause;
825 d->slave.device_resume = zx_dma_transfer_resume;
Jun Niee3fa9842015-05-05 22:06:08 +0800826 d->slave.copy_align = DMA_ALIGN;
827 d->slave.src_addr_widths = ZX_DMA_BUSWIDTHS;
828 d->slave.dst_addr_widths = ZX_DMA_BUSWIDTHS;
829 d->slave.directions = BIT(DMA_MEM_TO_MEM) | BIT(DMA_MEM_TO_DEV)
830 | BIT(DMA_DEV_TO_MEM);
831 d->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
832
833 /* init virtual channel */
834 d->chans = devm_kzalloc(&op->dev,
835 d->dma_requests * sizeof(struct zx_dma_chan), GFP_KERNEL);
836 if (!d->chans)
837 return -ENOMEM;
838
839 for (i = 0; i < d->dma_requests; i++) {
840 struct zx_dma_chan *c = &d->chans[i];
841
842 c->status = DMA_IN_PROGRESS;
843 INIT_LIST_HEAD(&c->node);
844 c->vc.desc_free = zx_dma_free_desc;
845 vchan_init(&c->vc, &d->slave);
846 }
847
848 /* Enable clock before accessing registers */
849 ret = clk_prepare_enable(d->clk);
850 if (ret < 0) {
851 dev_err(&op->dev, "clk_prepare_enable failed: %d\n", ret);
852 goto zx_dma_out;
853 }
854
855 zx_dma_init_state(d);
856
857 spin_lock_init(&d->lock);
858 INIT_LIST_HEAD(&d->chan_pending);
859 platform_set_drvdata(op, d);
860
861 ret = dma_async_device_register(&d->slave);
862 if (ret)
863 goto clk_dis;
864
865 ret = of_dma_controller_register((&op->dev)->of_node,
866 zx_of_dma_simple_xlate, d);
867 if (ret)
868 goto of_dma_register_fail;
869
870 dev_info(&op->dev, "initialized\n");
871 return 0;
872
873of_dma_register_fail:
874 dma_async_device_unregister(&d->slave);
875clk_dis:
876 clk_disable_unprepare(d->clk);
877zx_dma_out:
878 return ret;
879}
880
881static int zx_dma_remove(struct platform_device *op)
882{
883 struct zx_dma_chan *c, *cn;
884 struct zx_dma_dev *d = platform_get_drvdata(op);
885
Vinod Koul9bde2822015-05-18 15:33:13 +0530886 /* explictly free the irq */
887 devm_free_irq(&op->dev, d->irq, d);
888
Jun Niee3fa9842015-05-05 22:06:08 +0800889 dma_async_device_unregister(&d->slave);
890 of_dma_controller_free((&op->dev)->of_node);
891
892 list_for_each_entry_safe(c, cn, &d->slave.channels,
893 vc.chan.device_node) {
894 list_del(&c->vc.chan.device_node);
895 }
896 clk_disable_unprepare(d->clk);
897 dmam_pool_destroy(d->pool);
898
899 return 0;
900}
901
902#ifdef CONFIG_PM_SLEEP
903static int zx_dma_suspend_dev(struct device *dev)
904{
905 struct zx_dma_dev *d = dev_get_drvdata(dev);
906 u32 stat = 0;
907
908 stat = zx_dma_get_chan_stat(d);
909 if (stat) {
910 dev_warn(d->slave.dev,
911 "chan %d is running fail to suspend\n", stat);
912 return -1;
913 }
914 clk_disable_unprepare(d->clk);
915 return 0;
916}
917
918static int zx_dma_resume_dev(struct device *dev)
919{
920 struct zx_dma_dev *d = dev_get_drvdata(dev);
921 int ret = 0;
922
923 ret = clk_prepare_enable(d->clk);
924 if (ret < 0) {
925 dev_err(d->slave.dev, "clk_prepare_enable failed: %d\n", ret);
926 return ret;
927 }
928 zx_dma_init_state(d);
929 return 0;
930}
931#endif
932
933static SIMPLE_DEV_PM_OPS(zx_dma_pmops, zx_dma_suspend_dev, zx_dma_resume_dev);
934
935static struct platform_driver zx_pdma_driver = {
936 .driver = {
937 .name = DRIVER_NAME,
938 .pm = &zx_dma_pmops,
939 .of_match_table = zx6702_dma_dt_ids,
940 },
941 .probe = zx_dma_probe,
942 .remove = zx_dma_remove,
943};
944
945module_platform_driver(zx_pdma_driver);
946
947MODULE_DESCRIPTION("ZTE ZX296702 DMA Driver");
948MODULE_AUTHOR("Jun Nie jun.nie@linaro.org");
949MODULE_LICENSE("GPL v2");