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