blob: eb75d4b4bafe1e846250eb7e594366894cbb4dcb [file] [log] [blame]
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +08001/*
2 * Copyright 2012 Marvell International Ltd.
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 */
Thierry Reding73312052013-01-21 11:09:00 +01008#include <linux/err.h>
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +08009#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/types.h>
12#include <linux/interrupt.h>
13#include <linux/dma-mapping.h>
14#include <linux/slab.h>
15#include <linux/dmaengine.h>
16#include <linux/platform_device.h>
17#include <linux/device.h>
18#include <linux/platform_data/mmp_dma.h>
19#include <linux/dmapool.h>
20#include <linux/of_device.h>
21#include <linux/of.h>
22
23#include "dmaengine.h"
24
25#define DCSR 0x0000
26#define DALGN 0x00a0
27#define DINT 0x00f0
28#define DDADR 0x0200
29#define DSADR 0x0204
30#define DTADR 0x0208
31#define DCMD 0x020c
32
33#define DCSR_RUN (1 << 31) /* Run Bit (read / write) */
34#define DCSR_NODESC (1 << 30) /* No-Descriptor Fetch (read / write) */
35#define DCSR_STOPIRQEN (1 << 29) /* Stop Interrupt Enable (read / write) */
36#define DCSR_REQPEND (1 << 8) /* Request Pending (read-only) */
37#define DCSR_STOPSTATE (1 << 3) /* Stop State (read-only) */
38#define DCSR_ENDINTR (1 << 2) /* End Interrupt (read / write) */
39#define DCSR_STARTINTR (1 << 1) /* Start Interrupt (read / write) */
40#define DCSR_BUSERR (1 << 0) /* Bus Error Interrupt (read / write) */
41
42#define DCSR_EORIRQEN (1 << 28) /* End of Receive Interrupt Enable (R/W) */
43#define DCSR_EORJMPEN (1 << 27) /* Jump to next descriptor on EOR */
44#define DCSR_EORSTOPEN (1 << 26) /* STOP on an EOR */
45#define DCSR_SETCMPST (1 << 25) /* Set Descriptor Compare Status */
46#define DCSR_CLRCMPST (1 << 24) /* Clear Descriptor Compare Status */
47#define DCSR_CMPST (1 << 10) /* The Descriptor Compare Status */
48#define DCSR_EORINTR (1 << 9) /* The end of Receive */
49
Daniel Mack8b298de2013-08-10 18:52:15 +020050#define DRCMR(n) ((((n) < 64) ? 0x0100 : 0x1100) + \
51 (((n) & 0x3f) << 2))
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +080052#define DRCMR_MAPVLD (1 << 7) /* Map Valid (read / write) */
53#define DRCMR_CHLNUM 0x1f /* mask for Channel Number (read / write) */
54
55#define DDADR_DESCADDR 0xfffffff0 /* Address of next descriptor (mask) */
56#define DDADR_STOP (1 << 0) /* Stop (read / write) */
57
58#define DCMD_INCSRCADDR (1 << 31) /* Source Address Increment Setting. */
59#define DCMD_INCTRGADDR (1 << 30) /* Target Address Increment Setting. */
60#define DCMD_FLOWSRC (1 << 29) /* Flow Control by the source. */
61#define DCMD_FLOWTRG (1 << 28) /* Flow Control by the target. */
62#define DCMD_STARTIRQEN (1 << 22) /* Start Interrupt Enable */
63#define DCMD_ENDIRQEN (1 << 21) /* End Interrupt Enable */
64#define DCMD_ENDIAN (1 << 18) /* Device Endian-ness. */
65#define DCMD_BURST8 (1 << 16) /* 8 byte burst */
66#define DCMD_BURST16 (2 << 16) /* 16 byte burst */
67#define DCMD_BURST32 (3 << 16) /* 32 byte burst */
68#define DCMD_WIDTH1 (1 << 14) /* 1 byte width */
69#define DCMD_WIDTH2 (2 << 14) /* 2 byte width (HalfWord) */
70#define DCMD_WIDTH4 (3 << 14) /* 4 byte width (Word) */
71#define DCMD_LENGTH 0x01fff /* length mask (max = 8K - 1) */
72
73#define PDMA_ALIGNMENT 3
Daniel Mack1ac0e842013-08-10 18:52:17 +020074#define PDMA_MAX_DESC_BYTES DCMD_LENGTH
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +080075
76struct mmp_pdma_desc_hw {
77 u32 ddadr; /* Points to the next descriptor + flags */
78 u32 dsadr; /* DSADR value for the current transfer */
79 u32 dtadr; /* DTADR value for the current transfer */
80 u32 dcmd; /* DCMD value for the current transfer */
81} __aligned(32);
82
83struct mmp_pdma_desc_sw {
84 struct mmp_pdma_desc_hw desc;
85 struct list_head node;
86 struct list_head tx_list;
87 struct dma_async_tx_descriptor async_tx;
88};
89
90struct mmp_pdma_phy;
91
92struct mmp_pdma_chan {
93 struct device *dev;
94 struct dma_chan chan;
95 struct dma_async_tx_descriptor desc;
96 struct mmp_pdma_phy *phy;
97 enum dma_transfer_direction dir;
98
99 /* channel's basic info */
100 struct tasklet_struct tasklet;
101 u32 dcmd;
102 u32 drcmr;
103 u32 dev_addr;
104
105 /* list for desc */
106 spinlock_t desc_lock; /* Descriptor list lock */
107 struct list_head chain_pending; /* Link descriptors queue for pending */
108 struct list_head chain_running; /* Link descriptors queue for running */
109 bool idle; /* channel statue machine */
110
111 struct dma_pool *desc_pool; /* Descriptors pool */
112};
113
114struct mmp_pdma_phy {
115 int idx;
116 void __iomem *base;
117 struct mmp_pdma_chan *vchan;
118};
119
120struct mmp_pdma_device {
121 int dma_channels;
122 void __iomem *base;
123 struct device *dev;
124 struct dma_device device;
125 struct mmp_pdma_phy *phy;
Xiang Wang027f28b2013-06-18 14:55:58 +0800126 spinlock_t phy_lock; /* protect alloc/free phy channels */
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800127};
128
129#define tx_to_mmp_pdma_desc(tx) container_of(tx, struct mmp_pdma_desc_sw, async_tx)
130#define to_mmp_pdma_desc(lh) container_of(lh, struct mmp_pdma_desc_sw, node)
131#define to_mmp_pdma_chan(dchan) container_of(dchan, struct mmp_pdma_chan, chan)
132#define to_mmp_pdma_dev(dmadev) container_of(dmadev, struct mmp_pdma_device, device)
133
134static void set_desc(struct mmp_pdma_phy *phy, dma_addr_t addr)
135{
136 u32 reg = (phy->idx << 4) + DDADR;
137
138 writel(addr, phy->base + reg);
139}
140
141static void enable_chan(struct mmp_pdma_phy *phy)
142{
143 u32 reg;
144
145 if (!phy->vchan)
146 return;
147
Daniel Mack8b298de2013-08-10 18:52:15 +0200148 reg = DRCMR(phy->vchan->drcmr);
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800149 writel(DRCMR_MAPVLD | phy->idx, phy->base + reg);
150
151 reg = (phy->idx << 2) + DCSR;
152 writel(readl(phy->base + reg) | DCSR_RUN,
153 phy->base + reg);
154}
155
156static void disable_chan(struct mmp_pdma_phy *phy)
157{
158 u32 reg;
159
160 if (phy) {
161 reg = (phy->idx << 2) + DCSR;
162 writel(readl(phy->base + reg) & ~DCSR_RUN,
163 phy->base + reg);
164 }
165}
166
167static int clear_chan_irq(struct mmp_pdma_phy *phy)
168{
169 u32 dcsr;
170 u32 dint = readl(phy->base + DINT);
171 u32 reg = (phy->idx << 2) + DCSR;
172
173 if (dint & BIT(phy->idx)) {
174 /* clear irq */
175 dcsr = readl(phy->base + reg);
176 writel(dcsr, phy->base + reg);
177 if ((dcsr & DCSR_BUSERR) && (phy->vchan))
178 dev_warn(phy->vchan->dev, "DCSR_BUSERR\n");
179 return 0;
180 }
181 return -EAGAIN;
182}
183
184static irqreturn_t mmp_pdma_chan_handler(int irq, void *dev_id)
185{
186 struct mmp_pdma_phy *phy = dev_id;
187
188 if (clear_chan_irq(phy) == 0) {
189 tasklet_schedule(&phy->vchan->tasklet);
190 return IRQ_HANDLED;
191 } else
192 return IRQ_NONE;
193}
194
195static irqreturn_t mmp_pdma_int_handler(int irq, void *dev_id)
196{
197 struct mmp_pdma_device *pdev = dev_id;
198 struct mmp_pdma_phy *phy;
199 u32 dint = readl(pdev->base + DINT);
200 int i, ret;
201 int irq_num = 0;
202
203 while (dint) {
204 i = __ffs(dint);
205 dint &= (dint - 1);
206 phy = &pdev->phy[i];
207 ret = mmp_pdma_chan_handler(irq, phy);
208 if (ret == IRQ_HANDLED)
209 irq_num++;
210 }
211
212 if (irq_num)
213 return IRQ_HANDLED;
214 else
215 return IRQ_NONE;
216}
217
218/* lookup free phy channel as descending priority */
219static struct mmp_pdma_phy *lookup_phy(struct mmp_pdma_chan *pchan)
220{
221 int prio, i;
222 struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
Daniel Mack638a5422013-08-10 18:52:16 +0200223 struct mmp_pdma_phy *phy, *found = NULL;
Xiang Wang027f28b2013-06-18 14:55:58 +0800224 unsigned long flags;
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800225
226 /*
227 * dma channel priorities
228 * ch 0 - 3, 16 - 19 <--> (0)
229 * ch 4 - 7, 20 - 23 <--> (1)
230 * ch 8 - 11, 24 - 27 <--> (2)
231 * ch 12 - 15, 28 - 31 <--> (3)
232 */
Xiang Wang027f28b2013-06-18 14:55:58 +0800233
234 spin_lock_irqsave(&pdev->phy_lock, flags);
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800235 for (prio = 0; prio <= (((pdev->dma_channels - 1) & 0xf) >> 2); prio++) {
236 for (i = 0; i < pdev->dma_channels; i++) {
237 if (prio != ((i & 0xf) >> 2))
238 continue;
239 phy = &pdev->phy[i];
240 if (!phy->vchan) {
241 phy->vchan = pchan;
Daniel Mack638a5422013-08-10 18:52:16 +0200242 found = phy;
243 goto out_unlock;
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800244 }
245 }
246 }
247
Daniel Mack638a5422013-08-10 18:52:16 +0200248out_unlock:
Xiang Wang027f28b2013-06-18 14:55:58 +0800249 spin_unlock_irqrestore(&pdev->phy_lock, flags);
Daniel Mack638a5422013-08-10 18:52:16 +0200250 return found;
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800251}
252
Xiang Wang027f28b2013-06-18 14:55:58 +0800253static void mmp_pdma_free_phy(struct mmp_pdma_chan *pchan)
254{
255 struct mmp_pdma_device *pdev = to_mmp_pdma_dev(pchan->chan.device);
256 unsigned long flags;
Xiang Wang26a2dfd2013-06-18 14:55:59 +0800257 u32 reg;
Xiang Wang027f28b2013-06-18 14:55:58 +0800258
259 if (!pchan->phy)
260 return;
261
Xiang Wang26a2dfd2013-06-18 14:55:59 +0800262 /* clear the channel mapping in DRCMR */
Daniel Mack8b298de2013-08-10 18:52:15 +0200263 reg = DRCMR(pchan->phy->vchan->drcmr);
Xiang Wang26a2dfd2013-06-18 14:55:59 +0800264 writel(0, pchan->phy->base + reg);
265
Xiang Wang027f28b2013-06-18 14:55:58 +0800266 spin_lock_irqsave(&pdev->phy_lock, flags);
267 pchan->phy->vchan = NULL;
268 pchan->phy = NULL;
269 spin_unlock_irqrestore(&pdev->phy_lock, flags);
270}
271
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800272/* desc->tx_list ==> pending list */
273static void append_pending_queue(struct mmp_pdma_chan *chan,
274 struct mmp_pdma_desc_sw *desc)
275{
276 struct mmp_pdma_desc_sw *tail =
277 to_mmp_pdma_desc(chan->chain_pending.prev);
278
279 if (list_empty(&chan->chain_pending))
280 goto out_splice;
281
282 /* one irq per queue, even appended */
283 tail->desc.ddadr = desc->async_tx.phys;
284 tail->desc.dcmd &= ~DCMD_ENDIRQEN;
285
286 /* softly link to pending list */
287out_splice:
288 list_splice_tail_init(&desc->tx_list, &chan->chain_pending);
289}
290
291/**
292 * start_pending_queue - transfer any pending transactions
293 * pending list ==> running list
294 */
295static void start_pending_queue(struct mmp_pdma_chan *chan)
296{
297 struct mmp_pdma_desc_sw *desc;
298
299 /* still in running, irq will start the pending list */
300 if (!chan->idle) {
301 dev_dbg(chan->dev, "DMA controller still busy\n");
302 return;
303 }
304
305 if (list_empty(&chan->chain_pending)) {
306 /* chance to re-fetch phy channel with higher prio */
Xiang Wang027f28b2013-06-18 14:55:58 +0800307 mmp_pdma_free_phy(chan);
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800308 dev_dbg(chan->dev, "no pending list\n");
309 return;
310 }
311
312 if (!chan->phy) {
313 chan->phy = lookup_phy(chan);
314 if (!chan->phy) {
315 dev_dbg(chan->dev, "no free dma channel\n");
316 return;
317 }
318 }
319
320 /*
321 * pending -> running
322 * reintilize pending list
323 */
324 desc = list_first_entry(&chan->chain_pending,
325 struct mmp_pdma_desc_sw, node);
326 list_splice_tail_init(&chan->chain_pending, &chan->chain_running);
327
328 /*
329 * Program the descriptor's address into the DMA controller,
330 * then start the DMA transaction
331 */
332 set_desc(chan->phy, desc->async_tx.phys);
333 enable_chan(chan->phy);
334 chan->idle = false;
335}
336
337
338/* desc->tx_list ==> pending list */
339static dma_cookie_t mmp_pdma_tx_submit(struct dma_async_tx_descriptor *tx)
340{
341 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(tx->chan);
342 struct mmp_pdma_desc_sw *desc = tx_to_mmp_pdma_desc(tx);
343 struct mmp_pdma_desc_sw *child;
344 unsigned long flags;
345 dma_cookie_t cookie = -EBUSY;
346
347 spin_lock_irqsave(&chan->desc_lock, flags);
348
349 list_for_each_entry(child, &desc->tx_list, node) {
350 cookie = dma_cookie_assign(&child->async_tx);
351 }
352
353 append_pending_queue(chan, desc);
354
355 spin_unlock_irqrestore(&chan->desc_lock, flags);
356
357 return cookie;
358}
359
Jingoo Han69c9f0a2013-08-06 19:35:13 +0900360static struct mmp_pdma_desc_sw *
361mmp_pdma_alloc_descriptor(struct mmp_pdma_chan *chan)
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800362{
363 struct mmp_pdma_desc_sw *desc;
364 dma_addr_t pdesc;
365
366 desc = dma_pool_alloc(chan->desc_pool, GFP_ATOMIC, &pdesc);
367 if (!desc) {
368 dev_err(chan->dev, "out of memory for link descriptor\n");
369 return NULL;
370 }
371
372 memset(desc, 0, sizeof(*desc));
373 INIT_LIST_HEAD(&desc->tx_list);
374 dma_async_tx_descriptor_init(&desc->async_tx, &chan->chan);
375 /* each desc has submit */
376 desc->async_tx.tx_submit = mmp_pdma_tx_submit;
377 desc->async_tx.phys = pdesc;
378
379 return desc;
380}
381
382/**
383 * mmp_pdma_alloc_chan_resources - Allocate resources for DMA channel.
384 *
385 * This function will create a dma pool for descriptor allocation.
386 * Request irq only when channel is requested
387 * Return - The number of allocated descriptors.
388 */
389
390static int mmp_pdma_alloc_chan_resources(struct dma_chan *dchan)
391{
392 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
393
394 if (chan->desc_pool)
395 return 1;
396
397 chan->desc_pool =
398 dma_pool_create(dev_name(&dchan->dev->device), chan->dev,
399 sizeof(struct mmp_pdma_desc_sw),
400 __alignof__(struct mmp_pdma_desc_sw), 0);
401 if (!chan->desc_pool) {
402 dev_err(chan->dev, "unable to allocate descriptor pool\n");
403 return -ENOMEM;
404 }
Xiang Wang027f28b2013-06-18 14:55:58 +0800405 mmp_pdma_free_phy(chan);
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800406 chan->idle = true;
407 chan->dev_addr = 0;
408 return 1;
409}
410
411static void mmp_pdma_free_desc_list(struct mmp_pdma_chan *chan,
412 struct list_head *list)
413{
414 struct mmp_pdma_desc_sw *desc, *_desc;
415
416 list_for_each_entry_safe(desc, _desc, list, node) {
417 list_del(&desc->node);
418 dma_pool_free(chan->desc_pool, desc, desc->async_tx.phys);
419 }
420}
421
422static void mmp_pdma_free_chan_resources(struct dma_chan *dchan)
423{
424 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
425 unsigned long flags;
426
427 spin_lock_irqsave(&chan->desc_lock, flags);
428 mmp_pdma_free_desc_list(chan, &chan->chain_pending);
429 mmp_pdma_free_desc_list(chan, &chan->chain_running);
430 spin_unlock_irqrestore(&chan->desc_lock, flags);
431
432 dma_pool_destroy(chan->desc_pool);
433 chan->desc_pool = NULL;
434 chan->idle = true;
435 chan->dev_addr = 0;
Xiang Wang027f28b2013-06-18 14:55:58 +0800436 mmp_pdma_free_phy(chan);
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800437 return;
438}
439
440static struct dma_async_tx_descriptor *
441mmp_pdma_prep_memcpy(struct dma_chan *dchan,
442 dma_addr_t dma_dst, dma_addr_t dma_src,
443 size_t len, unsigned long flags)
444{
445 struct mmp_pdma_chan *chan;
446 struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new;
447 size_t copy = 0;
448
449 if (!dchan)
450 return NULL;
451
452 if (!len)
453 return NULL;
454
455 chan = to_mmp_pdma_chan(dchan);
456
457 if (!chan->dir) {
458 chan->dir = DMA_MEM_TO_MEM;
459 chan->dcmd = DCMD_INCTRGADDR | DCMD_INCSRCADDR;
460 chan->dcmd |= DCMD_BURST32;
461 }
462
463 do {
464 /* Allocate the link descriptor from DMA pool */
465 new = mmp_pdma_alloc_descriptor(chan);
466 if (!new) {
467 dev_err(chan->dev, "no memory for desc\n");
468 goto fail;
469 }
470
471 copy = min_t(size_t, len, PDMA_MAX_DESC_BYTES);
472
473 new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & copy);
474 new->desc.dsadr = dma_src;
475 new->desc.dtadr = dma_dst;
476
477 if (!first)
478 first = new;
479 else
480 prev->desc.ddadr = new->async_tx.phys;
481
482 new->async_tx.cookie = 0;
483 async_tx_ack(&new->async_tx);
484
485 prev = new;
486 len -= copy;
487
488 if (chan->dir == DMA_MEM_TO_DEV) {
489 dma_src += copy;
490 } else if (chan->dir == DMA_DEV_TO_MEM) {
491 dma_dst += copy;
492 } else if (chan->dir == DMA_MEM_TO_MEM) {
493 dma_src += copy;
494 dma_dst += copy;
495 }
496
497 /* Insert the link descriptor to the LD ring */
498 list_add_tail(&new->node, &first->tx_list);
499 } while (len);
500
501 first->async_tx.flags = flags; /* client is in control of this ack */
502 first->async_tx.cookie = -EBUSY;
503
504 /* last desc and fire IRQ */
505 new->desc.ddadr = DDADR_STOP;
506 new->desc.dcmd |= DCMD_ENDIRQEN;
507
508 return &first->async_tx;
509
510fail:
511 if (first)
512 mmp_pdma_free_desc_list(chan, &first->tx_list);
513 return NULL;
514}
515
516static struct dma_async_tx_descriptor *
517mmp_pdma_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
518 unsigned int sg_len, enum dma_transfer_direction dir,
519 unsigned long flags, void *context)
520{
521 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
522 struct mmp_pdma_desc_sw *first = NULL, *prev = NULL, *new = NULL;
523 size_t len, avail;
524 struct scatterlist *sg;
525 dma_addr_t addr;
526 int i;
527
528 if ((sgl == NULL) || (sg_len == 0))
529 return NULL;
530
531 for_each_sg(sgl, sg, sg_len, i) {
532 addr = sg_dma_address(sg);
533 avail = sg_dma_len(sgl);
534
535 do {
536 len = min_t(size_t, avail, PDMA_MAX_DESC_BYTES);
537
538 /* allocate and populate the descriptor */
539 new = mmp_pdma_alloc_descriptor(chan);
540 if (!new) {
541 dev_err(chan->dev, "no memory for desc\n");
542 goto fail;
543 }
544
545 new->desc.dcmd = chan->dcmd | (DCMD_LENGTH & len);
546 if (dir == DMA_MEM_TO_DEV) {
547 new->desc.dsadr = addr;
548 new->desc.dtadr = chan->dev_addr;
549 } else {
550 new->desc.dsadr = chan->dev_addr;
551 new->desc.dtadr = addr;
552 }
553
554 if (!first)
555 first = new;
556 else
557 prev->desc.ddadr = new->async_tx.phys;
558
559 new->async_tx.cookie = 0;
560 async_tx_ack(&new->async_tx);
561 prev = new;
562
563 /* Insert the link descriptor to the LD ring */
564 list_add_tail(&new->node, &first->tx_list);
565
566 /* update metadata */
567 addr += len;
568 avail -= len;
569 } while (avail);
570 }
571
572 first->async_tx.cookie = -EBUSY;
573 first->async_tx.flags = flags;
574
575 /* last desc and fire IRQ */
576 new->desc.ddadr = DDADR_STOP;
577 new->desc.dcmd |= DCMD_ENDIRQEN;
578
579 return &first->async_tx;
580
581fail:
582 if (first)
583 mmp_pdma_free_desc_list(chan, &first->tx_list);
584 return NULL;
585}
586
587static int mmp_pdma_control(struct dma_chan *dchan, enum dma_ctrl_cmd cmd,
588 unsigned long arg)
589{
590 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
591 struct dma_slave_config *cfg = (void *)arg;
592 unsigned long flags;
593 int ret = 0;
594 u32 maxburst = 0, addr = 0;
595 enum dma_slave_buswidth width = DMA_SLAVE_BUSWIDTH_UNDEFINED;
596
597 if (!dchan)
598 return -EINVAL;
599
600 switch (cmd) {
601 case DMA_TERMINATE_ALL:
602 disable_chan(chan->phy);
Xiang Wang027f28b2013-06-18 14:55:58 +0800603 mmp_pdma_free_phy(chan);
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800604 spin_lock_irqsave(&chan->desc_lock, flags);
605 mmp_pdma_free_desc_list(chan, &chan->chain_pending);
606 mmp_pdma_free_desc_list(chan, &chan->chain_running);
607 spin_unlock_irqrestore(&chan->desc_lock, flags);
608 chan->idle = true;
609 break;
610 case DMA_SLAVE_CONFIG:
611 if (cfg->direction == DMA_DEV_TO_MEM) {
612 chan->dcmd = DCMD_INCTRGADDR | DCMD_FLOWSRC;
613 maxburst = cfg->src_maxburst;
614 width = cfg->src_addr_width;
615 addr = cfg->src_addr;
616 } else if (cfg->direction == DMA_MEM_TO_DEV) {
617 chan->dcmd = DCMD_INCSRCADDR | DCMD_FLOWTRG;
618 maxburst = cfg->dst_maxburst;
619 width = cfg->dst_addr_width;
620 addr = cfg->dst_addr;
621 }
622
623 if (width == DMA_SLAVE_BUSWIDTH_1_BYTE)
624 chan->dcmd |= DCMD_WIDTH1;
625 else if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
626 chan->dcmd |= DCMD_WIDTH2;
627 else if (width == DMA_SLAVE_BUSWIDTH_4_BYTES)
628 chan->dcmd |= DCMD_WIDTH4;
629
630 if (maxburst == 8)
631 chan->dcmd |= DCMD_BURST8;
632 else if (maxburst == 16)
633 chan->dcmd |= DCMD_BURST16;
634 else if (maxburst == 32)
635 chan->dcmd |= DCMD_BURST32;
636
Cong Dinged30933e2013-01-15 01:19:48 +0100637 chan->dir = cfg->direction;
638 chan->drcmr = cfg->slave_id;
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800639 chan->dev_addr = addr;
640 break;
641 default:
642 return -ENOSYS;
643 }
644
645 return ret;
646}
647
648static enum dma_status mmp_pdma_tx_status(struct dma_chan *dchan,
649 dma_cookie_t cookie, struct dma_tx_state *txstate)
650{
Andy Shevchenko4aa9fe02013-05-27 15:14:36 +0300651 return dma_cookie_status(dchan, cookie, txstate);
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800652}
653
654/**
655 * mmp_pdma_issue_pending - Issue the DMA start command
656 * pending list ==> running list
657 */
658static void mmp_pdma_issue_pending(struct dma_chan *dchan)
659{
660 struct mmp_pdma_chan *chan = to_mmp_pdma_chan(dchan);
661 unsigned long flags;
662
663 spin_lock_irqsave(&chan->desc_lock, flags);
664 start_pending_queue(chan);
665 spin_unlock_irqrestore(&chan->desc_lock, flags);
666}
667
668/*
669 * dma_do_tasklet
670 * Do call back
671 * Start pending list
672 */
673static void dma_do_tasklet(unsigned long data)
674{
675 struct mmp_pdma_chan *chan = (struct mmp_pdma_chan *)data;
676 struct mmp_pdma_desc_sw *desc, *_desc;
677 LIST_HEAD(chain_cleanup);
678 unsigned long flags;
679
680 /* submit pending list; callback for each desc; free desc */
681
682 spin_lock_irqsave(&chan->desc_lock, flags);
683
684 /* update the cookie if we have some descriptors to cleanup */
685 if (!list_empty(&chan->chain_running)) {
686 dma_cookie_t cookie;
687
688 desc = to_mmp_pdma_desc(chan->chain_running.prev);
689 cookie = desc->async_tx.cookie;
690 dma_cookie_complete(&desc->async_tx);
691
692 dev_dbg(chan->dev, "completed_cookie=%d\n", cookie);
693 }
694
695 /*
696 * move the descriptors to a temporary list so we can drop the lock
697 * during the entire cleanup operation
698 */
699 list_splice_tail_init(&chan->chain_running, &chain_cleanup);
700
701 /* the hardware is now idle and ready for more */
702 chan->idle = true;
703
704 /* Start any pending transactions automatically */
705 start_pending_queue(chan);
706 spin_unlock_irqrestore(&chan->desc_lock, flags);
707
708 /* Run the callback for each descriptor, in order */
709 list_for_each_entry_safe(desc, _desc, &chain_cleanup, node) {
710 struct dma_async_tx_descriptor *txd = &desc->async_tx;
711
712 /* Remove from the list of transactions */
713 list_del(&desc->node);
714 /* Run the link descriptor callback function */
715 if (txd->callback)
716 txd->callback(txd->callback_param);
717
718 dma_pool_free(chan->desc_pool, desc, txd->phys);
719 }
720}
721
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -0800722static int mmp_pdma_remove(struct platform_device *op)
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800723{
724 struct mmp_pdma_device *pdev = platform_get_drvdata(op);
725
726 dma_async_device_unregister(&pdev->device);
727 return 0;
728}
729
Bill Pemberton463a1f82012-11-19 13:22:55 -0500730static int mmp_pdma_chan_init(struct mmp_pdma_device *pdev,
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800731 int idx, int irq)
732{
733 struct mmp_pdma_phy *phy = &pdev->phy[idx];
734 struct mmp_pdma_chan *chan;
735 int ret;
736
737 chan = devm_kzalloc(pdev->dev,
738 sizeof(struct mmp_pdma_chan), GFP_KERNEL);
739 if (chan == NULL)
740 return -ENOMEM;
741
742 phy->idx = idx;
743 phy->base = pdev->base;
744
745 if (irq) {
746 ret = devm_request_irq(pdev->dev, irq,
747 mmp_pdma_chan_handler, IRQF_DISABLED, "pdma", phy);
748 if (ret) {
749 dev_err(pdev->dev, "channel request irq fail!\n");
750 return ret;
751 }
752 }
753
754 spin_lock_init(&chan->desc_lock);
755 chan->dev = pdev->dev;
756 chan->chan.device = &pdev->device;
757 tasklet_init(&chan->tasklet, dma_do_tasklet, (unsigned long)chan);
758 INIT_LIST_HEAD(&chan->chain_pending);
759 INIT_LIST_HEAD(&chan->chain_running);
760
761 /* register virt channel to dma engine */
762 list_add_tail(&chan->chan.device_node,
763 &pdev->device.channels);
764
765 return 0;
766}
767
768static struct of_device_id mmp_pdma_dt_ids[] = {
769 { .compatible = "marvell,pdma-1.0", },
770 {}
771};
772MODULE_DEVICE_TABLE(of, mmp_pdma_dt_ids);
773
Bill Pemberton463a1f82012-11-19 13:22:55 -0500774static int mmp_pdma_probe(struct platform_device *op)
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800775{
776 struct mmp_pdma_device *pdev;
777 const struct of_device_id *of_id;
778 struct mmp_dma_platdata *pdata = dev_get_platdata(&op->dev);
779 struct resource *iores;
780 int i, ret, irq = 0;
781 int dma_channels = 0, irq_num = 0;
782
783 pdev = devm_kzalloc(&op->dev, sizeof(*pdev), GFP_KERNEL);
784 if (!pdev)
785 return -ENOMEM;
786 pdev->dev = &op->dev;
787
Xiang Wang027f28b2013-06-18 14:55:58 +0800788 spin_lock_init(&pdev->phy_lock);
789
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800790 iores = platform_get_resource(op, IORESOURCE_MEM, 0);
791 if (!iores)
792 return -EINVAL;
793
Thierry Reding73312052013-01-21 11:09:00 +0100794 pdev->base = devm_ioremap_resource(pdev->dev, iores);
795 if (IS_ERR(pdev->base))
796 return PTR_ERR(pdev->base);
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800797
798 of_id = of_match_device(mmp_pdma_dt_ids, pdev->dev);
799 if (of_id)
800 of_property_read_u32(pdev->dev->of_node,
801 "#dma-channels", &dma_channels);
802 else if (pdata && pdata->dma_channels)
803 dma_channels = pdata->dma_channels;
804 else
805 dma_channels = 32; /* default 32 channel */
806 pdev->dma_channels = dma_channels;
807
808 for (i = 0; i < dma_channels; i++) {
809 if (platform_get_irq(op, i) > 0)
810 irq_num++;
811 }
812
813 pdev->phy = devm_kzalloc(pdev->dev,
814 dma_channels * sizeof(struct mmp_pdma_chan), GFP_KERNEL);
815 if (pdev->phy == NULL)
816 return -ENOMEM;
817
818 INIT_LIST_HEAD(&pdev->device.channels);
819
820 if (irq_num != dma_channels) {
821 /* all chan share one irq, demux inside */
822 irq = platform_get_irq(op, 0);
823 ret = devm_request_irq(pdev->dev, irq,
824 mmp_pdma_int_handler, IRQF_DISABLED, "pdma", pdev);
825 if (ret)
826 return ret;
827 }
828
829 for (i = 0; i < dma_channels; i++) {
830 irq = (irq_num != dma_channels) ? 0 : platform_get_irq(op, i);
831 ret = mmp_pdma_chan_init(pdev, i, irq);
832 if (ret)
833 return ret;
834 }
835
836 dma_cap_set(DMA_SLAVE, pdev->device.cap_mask);
837 dma_cap_set(DMA_MEMCPY, pdev->device.cap_mask);
838 dma_cap_set(DMA_SLAVE, pdev->device.cap_mask);
839 pdev->device.dev = &op->dev;
840 pdev->device.device_alloc_chan_resources = mmp_pdma_alloc_chan_resources;
841 pdev->device.device_free_chan_resources = mmp_pdma_free_chan_resources;
842 pdev->device.device_tx_status = mmp_pdma_tx_status;
843 pdev->device.device_prep_dma_memcpy = mmp_pdma_prep_memcpy;
844 pdev->device.device_prep_slave_sg = mmp_pdma_prep_slave_sg;
845 pdev->device.device_issue_pending = mmp_pdma_issue_pending;
846 pdev->device.device_control = mmp_pdma_control;
847 pdev->device.copy_align = PDMA_ALIGNMENT;
848
849 if (pdev->dev->coherent_dma_mask)
850 dma_set_mask(pdev->dev, pdev->dev->coherent_dma_mask);
851 else
852 dma_set_mask(pdev->dev, DMA_BIT_MASK(64));
853
854 ret = dma_async_device_register(&pdev->device);
855 if (ret) {
856 dev_err(pdev->device.dev, "unable to register\n");
857 return ret;
858 }
859
860 dev_info(pdev->device.dev, "initialized\n");
861 return 0;
862}
863
864static const struct platform_device_id mmp_pdma_id_table[] = {
865 { "mmp-pdma", },
866 { },
867};
868
869static struct platform_driver mmp_pdma_driver = {
870 .driver = {
871 .name = "mmp-pdma",
872 .owner = THIS_MODULE,
873 .of_match_table = mmp_pdma_dt_ids,
874 },
875 .id_table = mmp_pdma_id_table,
876 .probe = mmp_pdma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -0500877 .remove = mmp_pdma_remove,
Zhangfei Gaoc8acd6a2012-09-03 11:03:45 +0800878};
879
880module_platform_driver(mmp_pdma_driver);
881
882MODULE_DESCRIPTION("MARVELL MMP Periphera DMA Driver");
883MODULE_AUTHOR("Marvell International Ltd.");
884MODULE_LICENSE("GPL v2");