1 // SPDX-License-Identifier: GPL-2.0-only
3 * SA11x0 DMAengine support
5 * Copyright (C) 2012 Russell King
6 * Derived in part from arch/arm/mach-sa1100/dma.c,
7 * Copyright (C) 2000, 2001 by Nicolas Pitre
9 #include <linux/sched.h>
10 #include <linux/device.h>
11 #include <linux/dmaengine.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
24 #define DMA_MAX_SIZE 0x1fff
25 #define DMA_CHUNK_SIZE 0x1000
28 #define DMA_DCSR_S 0x04
29 #define DMA_DCSR_C 0x08
30 #define DMA_DCSR_R 0x0c
37 #define DCSR_RUN (1 << 0)
38 #define DCSR_IE (1 << 1)
39 #define DCSR_ERROR (1 << 2)
40 #define DCSR_DONEA (1 << 3)
41 #define DCSR_STRTA (1 << 4)
42 #define DCSR_DONEB (1 << 5)
43 #define DCSR_STRTB (1 << 6)
44 #define DCSR_BIU (1 << 7)
46 #define DDAR_RW (1 << 0) /* 0 = W, 1 = R */
47 #define DDAR_E (1 << 1) /* 0 = LE, 1 = BE */
48 #define DDAR_BS (1 << 2) /* 0 = BS4, 1 = BS8 */
49 #define DDAR_DW (1 << 3) /* 0 = 8b, 1 = 16b */
50 #define DDAR_Ser0UDCTr (0x0 << 4)
51 #define DDAR_Ser0UDCRc (0x1 << 4)
52 #define DDAR_Ser1SDLCTr (0x2 << 4)
53 #define DDAR_Ser1SDLCRc (0x3 << 4)
54 #define DDAR_Ser1UARTTr (0x4 << 4)
55 #define DDAR_Ser1UARTRc (0x5 << 4)
56 #define DDAR_Ser2ICPTr (0x6 << 4)
57 #define DDAR_Ser2ICPRc (0x7 << 4)
58 #define DDAR_Ser3UARTTr (0x8 << 4)
59 #define DDAR_Ser3UARTRc (0x9 << 4)
60 #define DDAR_Ser4MCP0Tr (0xa << 4)
61 #define DDAR_Ser4MCP0Rc (0xb << 4)
62 #define DDAR_Ser4MCP1Tr (0xc << 4)
63 #define DDAR_Ser4MCP1Rc (0xd << 4)
64 #define DDAR_Ser4SSPTr (0xe << 4)
65 #define DDAR_Ser4SSPRc (0xf << 4)
67 struct sa11x0_dma_sg {
72 struct sa11x0_dma_desc {
73 struct virt_dma_desc vd;
81 struct sa11x0_dma_sg sg[];
84 struct sa11x0_dma_phy;
86 struct sa11x0_dma_chan {
87 struct virt_dma_chan vc;
89 /* protected by c->vc.lock */
90 struct sa11x0_dma_phy *phy;
91 enum dma_status status;
93 /* protected by d->lock */
94 struct list_head node;
100 struct sa11x0_dma_phy {
102 struct sa11x0_dma_dev *dev;
105 struct sa11x0_dma_chan *vchan;
107 /* Protected by c->vc.lock */
109 struct sa11x0_dma_desc *txd_load;
111 struct sa11x0_dma_desc *txd_done;
117 struct sa11x0_dma_dev {
118 struct dma_device slave;
121 struct tasklet_struct task;
122 struct list_head chan_pending;
123 struct sa11x0_dma_phy phy[NR_PHY_CHAN];
126 static struct sa11x0_dma_chan *to_sa11x0_dma_chan(struct dma_chan *chan)
128 return container_of(chan, struct sa11x0_dma_chan, vc.chan);
131 static struct sa11x0_dma_dev *to_sa11x0_dma(struct dma_device *dmadev)
133 return container_of(dmadev, struct sa11x0_dma_dev, slave);
136 static struct sa11x0_dma_desc *sa11x0_dma_next_desc(struct sa11x0_dma_chan *c)
138 struct virt_dma_desc *vd = vchan_next_desc(&c->vc);
140 return vd ? container_of(vd, struct sa11x0_dma_desc, vd) : NULL;
143 static void sa11x0_dma_free_desc(struct virt_dma_desc *vd)
145 kfree(container_of(vd, struct sa11x0_dma_desc, vd));
148 static void sa11x0_dma_start_desc(struct sa11x0_dma_phy *p, struct sa11x0_dma_desc *txd)
150 list_del(&txd->vd.node);
154 dev_vdbg(p->dev->slave.dev, "pchan %u: txd %p[%x]: starting: DDAR:%x\n",
155 p->num, &txd->vd, txd->vd.tx.cookie, txd->ddar);
158 static void noinline sa11x0_dma_start_sg(struct sa11x0_dma_phy *p,
159 struct sa11x0_dma_chan *c)
161 struct sa11x0_dma_desc *txd = p->txd_load;
162 struct sa11x0_dma_sg *sg;
163 void __iomem *base = p->base;
170 dcsr = readl_relaxed(base + DMA_DCSR_R);
172 /* Don't try to load the next transfer if both buffers are started */
173 if ((dcsr & (DCSR_STRTA | DCSR_STRTB)) == (DCSR_STRTA | DCSR_STRTB))
176 if (p->sg_load == txd->sglen) {
178 struct sa11x0_dma_desc *txn = sa11x0_dma_next_desc(c);
181 * We have reached the end of the current descriptor.
182 * Peek at the next descriptor, and if compatible with
183 * the current, start processing it.
185 if (txn && txn->ddar == txd->ddar) {
187 sa11x0_dma_start_desc(p, txn);
193 /* Cyclic: reset back to beginning */
198 sg = &txd->sg[p->sg_load++];
200 /* Select buffer to load according to channel status */
201 if (((dcsr & (DCSR_BIU | DCSR_STRTB)) == (DCSR_BIU | DCSR_STRTB)) ||
202 ((dcsr & (DCSR_BIU | DCSR_STRTA)) == 0)) {
205 dcsr = DCSR_STRTA | DCSR_IE | DCSR_RUN;
209 dcsr = DCSR_STRTB | DCSR_IE | DCSR_RUN;
212 writel_relaxed(sg->addr, base + dbsx);
213 writel_relaxed(sg->len, base + dbtx);
214 writel(dcsr, base + DMA_DCSR_S);
216 dev_dbg(p->dev->slave.dev, "pchan %u: load: DCSR:%02x DBS%c:%08x DBT%c:%08x\n",
218 'A' + (dbsx == DMA_DBSB), sg->addr,
219 'A' + (dbtx == DMA_DBTB), sg->len);
222 static void noinline sa11x0_dma_complete(struct sa11x0_dma_phy *p,
223 struct sa11x0_dma_chan *c)
225 struct sa11x0_dma_desc *txd = p->txd_done;
227 if (++p->sg_done == txd->sglen) {
229 vchan_cookie_complete(&txd->vd);
232 p->txd_done = p->txd_load;
235 tasklet_schedule(&p->dev->task);
237 if ((p->sg_done % txd->period) == 0)
238 vchan_cyclic_callback(&txd->vd);
240 /* Cyclic: reset back to beginning */
245 sa11x0_dma_start_sg(p, c);
248 static irqreturn_t sa11x0_dma_irq(int irq, void *dev_id)
250 struct sa11x0_dma_phy *p = dev_id;
251 struct sa11x0_dma_dev *d = p->dev;
252 struct sa11x0_dma_chan *c;
255 dcsr = readl_relaxed(p->base + DMA_DCSR_R);
256 if (!(dcsr & (DCSR_ERROR | DCSR_DONEA | DCSR_DONEB)))
259 /* Clear reported status bits */
260 writel_relaxed(dcsr & (DCSR_ERROR | DCSR_DONEA | DCSR_DONEB),
261 p->base + DMA_DCSR_C);
263 dev_dbg(d->slave.dev, "pchan %u: irq: DCSR:%02x\n", p->num, dcsr);
265 if (dcsr & DCSR_ERROR) {
266 dev_err(d->slave.dev, "pchan %u: error. DCSR:%02x DDAR:%08x DBSA:%08x DBTA:%08x DBSB:%08x DBTB:%08x\n",
268 readl_relaxed(p->base + DMA_DDAR),
269 readl_relaxed(p->base + DMA_DBSA),
270 readl_relaxed(p->base + DMA_DBTA),
271 readl_relaxed(p->base + DMA_DBSB),
272 readl_relaxed(p->base + DMA_DBTB));
279 spin_lock_irqsave(&c->vc.lock, flags);
281 * Now that we're holding the lock, check that the vchan
282 * really is associated with this pchan before touching the
283 * hardware. This should always succeed, because we won't
284 * change p->vchan or c->phy while the channel is actively
288 if (dcsr & DCSR_DONEA)
289 sa11x0_dma_complete(p, c);
290 if (dcsr & DCSR_DONEB)
291 sa11x0_dma_complete(p, c);
293 spin_unlock_irqrestore(&c->vc.lock, flags);
299 static void sa11x0_dma_start_txd(struct sa11x0_dma_chan *c)
301 struct sa11x0_dma_desc *txd = sa11x0_dma_next_desc(c);
303 /* If the issued list is empty, we have no further txds to process */
305 struct sa11x0_dma_phy *p = c->phy;
307 sa11x0_dma_start_desc(p, txd);
311 /* The channel should not have any transfers started */
312 WARN_ON(readl_relaxed(p->base + DMA_DCSR_R) &
313 (DCSR_STRTA | DCSR_STRTB));
315 /* Clear the run and start bits before changing DDAR */
316 writel_relaxed(DCSR_RUN | DCSR_STRTA | DCSR_STRTB,
317 p->base + DMA_DCSR_C);
318 writel_relaxed(txd->ddar, p->base + DMA_DDAR);
320 /* Try to start both buffers */
321 sa11x0_dma_start_sg(p, c);
322 sa11x0_dma_start_sg(p, c);
326 static void sa11x0_dma_tasklet(struct tasklet_struct *t)
328 struct sa11x0_dma_dev *d = from_tasklet(d, t, task);
329 struct sa11x0_dma_phy *p;
330 struct sa11x0_dma_chan *c;
331 unsigned pch, pch_alloc = 0;
333 dev_dbg(d->slave.dev, "tasklet enter\n");
335 list_for_each_entry(c, &d->slave.channels, vc.chan.device_node) {
336 spin_lock_irq(&c->vc.lock);
338 if (p && !p->txd_done) {
339 sa11x0_dma_start_txd(c);
341 /* No current txd associated with this channel */
342 dev_dbg(d->slave.dev, "pchan %u: free\n", p->num);
344 /* Mark this channel free */
349 spin_unlock_irq(&c->vc.lock);
352 spin_lock_irq(&d->lock);
353 for (pch = 0; pch < NR_PHY_CHAN; pch++) {
356 if (p->vchan == NULL && !list_empty(&d->chan_pending)) {
357 c = list_first_entry(&d->chan_pending,
358 struct sa11x0_dma_chan, node);
359 list_del_init(&c->node);
361 pch_alloc |= 1 << pch;
363 /* Mark this channel allocated */
366 dev_dbg(d->slave.dev, "pchan %u: alloc vchan %p\n", pch, &c->vc);
369 spin_unlock_irq(&d->lock);
371 for (pch = 0; pch < NR_PHY_CHAN; pch++) {
372 if (pch_alloc & (1 << pch)) {
376 spin_lock_irq(&c->vc.lock);
379 sa11x0_dma_start_txd(c);
380 spin_unlock_irq(&c->vc.lock);
384 dev_dbg(d->slave.dev, "tasklet exit\n");
388 static void sa11x0_dma_free_chan_resources(struct dma_chan *chan)
390 struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
391 struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
394 spin_lock_irqsave(&d->lock, flags);
395 list_del_init(&c->node);
396 spin_unlock_irqrestore(&d->lock, flags);
398 vchan_free_chan_resources(&c->vc);
401 static dma_addr_t sa11x0_dma_pos(struct sa11x0_dma_phy *p)
406 dcsr = readl_relaxed(p->base + DMA_DCSR_R);
408 if ((dcsr & (DCSR_BIU | DCSR_STRTA)) == DCSR_STRTA ||
409 (dcsr & (DCSR_BIU | DCSR_STRTB)) == DCSR_BIU)
414 return readl_relaxed(p->base + reg);
417 static enum dma_status sa11x0_dma_tx_status(struct dma_chan *chan,
418 dma_cookie_t cookie, struct dma_tx_state *state)
420 struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
421 struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
422 struct sa11x0_dma_phy *p;
423 struct virt_dma_desc *vd;
427 ret = dma_cookie_status(&c->vc.chan, cookie, state);
428 if (ret == DMA_COMPLETE)
434 spin_lock_irqsave(&c->vc.lock, flags);
438 * If the cookie is on our issue queue, then the residue is
441 vd = vchan_find_desc(&c->vc, cookie);
443 state->residue = container_of(vd, struct sa11x0_dma_desc, vd)->size;
447 struct sa11x0_dma_desc *txd;
450 if (p->txd_done && p->txd_done->vd.tx.cookie == cookie)
452 else if (p->txd_load && p->txd_load->vd.tx.cookie == cookie)
459 dma_addr_t addr = sa11x0_dma_pos(p);
462 dev_vdbg(d->slave.dev, "tx_status: addr:%pad\n", &addr);
464 for (i = 0; i < txd->sglen; i++) {
465 dev_vdbg(d->slave.dev, "tx_status: [%u] %x+%x\n",
466 i, txd->sg[i].addr, txd->sg[i].len);
467 if (addr >= txd->sg[i].addr &&
468 addr < txd->sg[i].addr + txd->sg[i].len) {
471 len = txd->sg[i].len -
472 (addr - txd->sg[i].addr);
473 dev_vdbg(d->slave.dev, "tx_status: [%u] +%x\n",
480 for (; i < txd->sglen; i++) {
481 dev_vdbg(d->slave.dev, "tx_status: [%u] %x+%x ++\n",
482 i, txd->sg[i].addr, txd->sg[i].len);
483 bytes += txd->sg[i].len;
486 state->residue = bytes;
488 spin_unlock_irqrestore(&c->vc.lock, flags);
490 dev_vdbg(d->slave.dev, "tx_status: bytes 0x%x\n", state->residue);
496 * Move pending txds to the issued list, and re-init pending list.
497 * If not already pending, add this channel to the list of pending
498 * channels and trigger the tasklet to run.
500 static void sa11x0_dma_issue_pending(struct dma_chan *chan)
502 struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
503 struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
506 spin_lock_irqsave(&c->vc.lock, flags);
507 if (vchan_issue_pending(&c->vc)) {
510 if (list_empty(&c->node)) {
511 list_add_tail(&c->node, &d->chan_pending);
512 tasklet_schedule(&d->task);
513 dev_dbg(d->slave.dev, "vchan %p: issued\n", &c->vc);
515 spin_unlock(&d->lock);
518 dev_dbg(d->slave.dev, "vchan %p: nothing to issue\n", &c->vc);
519 spin_unlock_irqrestore(&c->vc.lock, flags);
522 static struct dma_async_tx_descriptor *sa11x0_dma_prep_slave_sg(
523 struct dma_chan *chan, struct scatterlist *sg, unsigned int sglen,
524 enum dma_transfer_direction dir, unsigned long flags, void *context)
526 struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
527 struct sa11x0_dma_desc *txd;
528 struct scatterlist *sgent;
529 unsigned i, j = sglen;
532 /* SA11x0 channels can only operate in their native direction */
533 if (dir != (c->ddar & DDAR_RW ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV)) {
534 dev_err(chan->device->dev, "vchan %p: bad DMA direction: DDAR:%08x dir:%u\n",
535 &c->vc, c->ddar, dir);
539 /* Do not allow zero-sized txds */
543 for_each_sg(sg, sgent, sglen, i) {
544 dma_addr_t addr = sg_dma_address(sgent);
545 unsigned int len = sg_dma_len(sgent);
547 if (len > DMA_MAX_SIZE)
548 j += DIV_ROUND_UP(len, DMA_MAX_SIZE & ~DMA_ALIGN) - 1;
549 if (addr & DMA_ALIGN) {
550 dev_dbg(chan->device->dev, "vchan %p: bad buffer alignment: %pad\n",
556 txd = kzalloc(struct_size(txd, sg, j), GFP_ATOMIC);
558 dev_dbg(chan->device->dev, "vchan %p: kzalloc failed\n", &c->vc);
563 for_each_sg(sg, sgent, sglen, i) {
564 dma_addr_t addr = sg_dma_address(sgent);
565 unsigned len = sg_dma_len(sgent);
573 * Check whether the transfer will fit. If not, try
574 * to split the transfer up such that we end up with
575 * equal chunks - but make sure that we preserve the
576 * alignment. This avoids small segments.
578 if (tlen > DMA_MAX_SIZE) {
579 unsigned mult = DIV_ROUND_UP(tlen,
580 DMA_MAX_SIZE & ~DMA_ALIGN);
582 tlen = (tlen / mult) & ~DMA_ALIGN;
585 txd->sg[j].addr = addr;
586 txd->sg[j].len = tlen;
598 dev_dbg(chan->device->dev, "vchan %p: txd %p: size %zu nr %u\n",
599 &c->vc, &txd->vd, txd->size, txd->sglen);
601 return vchan_tx_prep(&c->vc, &txd->vd, flags);
604 static struct dma_async_tx_descriptor *sa11x0_dma_prep_dma_cyclic(
605 struct dma_chan *chan, dma_addr_t addr, size_t size, size_t period,
606 enum dma_transfer_direction dir, unsigned long flags)
608 struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
609 struct sa11x0_dma_desc *txd;
610 unsigned i, j, k, sglen, sgperiod;
612 /* SA11x0 channels can only operate in their native direction */
613 if (dir != (c->ddar & DDAR_RW ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV)) {
614 dev_err(chan->device->dev, "vchan %p: bad DMA direction: DDAR:%08x dir:%u\n",
615 &c->vc, c->ddar, dir);
619 sgperiod = DIV_ROUND_UP(period, DMA_MAX_SIZE & ~DMA_ALIGN);
620 sglen = size * sgperiod / period;
622 /* Do not allow zero-sized txds */
626 txd = kzalloc(struct_size(txd, sg, sglen), GFP_ATOMIC);
628 dev_dbg(chan->device->dev, "vchan %p: kzalloc failed\n", &c->vc);
632 for (i = k = 0; i < size / period; i++) {
633 size_t tlen, len = period;
635 for (j = 0; j < sgperiod; j++, k++) {
638 if (tlen > DMA_MAX_SIZE) {
639 unsigned mult = DIV_ROUND_UP(tlen, DMA_MAX_SIZE & ~DMA_ALIGN);
640 tlen = (tlen / mult) & ~DMA_ALIGN;
643 txd->sg[k].addr = addr;
644 txd->sg[k].len = tlen;
658 txd->period = sgperiod;
660 return vchan_tx_prep(&c->vc, &txd->vd, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
663 static int sa11x0_dma_device_config(struct dma_chan *chan,
664 struct dma_slave_config *cfg)
666 struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
667 u32 ddar = c->ddar & ((0xf << 4) | DDAR_RW);
669 enum dma_slave_buswidth width;
672 if (ddar & DDAR_RW) {
673 addr = cfg->src_addr;
674 width = cfg->src_addr_width;
675 maxburst = cfg->src_maxburst;
677 addr = cfg->dst_addr;
678 width = cfg->dst_addr_width;
679 maxburst = cfg->dst_maxburst;
682 if ((width != DMA_SLAVE_BUSWIDTH_1_BYTE &&
683 width != DMA_SLAVE_BUSWIDTH_2_BYTES) ||
684 (maxburst != 4 && maxburst != 8))
687 if (width == DMA_SLAVE_BUSWIDTH_2_BYTES)
692 dev_dbg(c->vc.chan.device->dev, "vchan %p: dma_slave_config addr %pad width %u burst %u\n",
693 &c->vc, &addr, width, maxburst);
695 c->ddar = ddar | (addr & 0xf0000000) | (addr & 0x003ffffc) << 6;
700 static int sa11x0_dma_device_pause(struct dma_chan *chan)
702 struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
703 struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
704 struct sa11x0_dma_phy *p;
707 dev_dbg(d->slave.dev, "vchan %p: pause\n", &c->vc);
708 spin_lock_irqsave(&c->vc.lock, flags);
709 if (c->status == DMA_IN_PROGRESS) {
710 c->status = DMA_PAUSED;
714 writel(DCSR_RUN | DCSR_IE, p->base + DMA_DCSR_C);
717 list_del_init(&c->node);
718 spin_unlock(&d->lock);
721 spin_unlock_irqrestore(&c->vc.lock, flags);
726 static int sa11x0_dma_device_resume(struct dma_chan *chan)
728 struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
729 struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
730 struct sa11x0_dma_phy *p;
733 dev_dbg(d->slave.dev, "vchan %p: resume\n", &c->vc);
734 spin_lock_irqsave(&c->vc.lock, flags);
735 if (c->status == DMA_PAUSED) {
736 c->status = DMA_IN_PROGRESS;
740 writel(DCSR_RUN | DCSR_IE, p->base + DMA_DCSR_S);
741 } else if (!list_empty(&c->vc.desc_issued)) {
743 list_add_tail(&c->node, &d->chan_pending);
744 spin_unlock(&d->lock);
747 spin_unlock_irqrestore(&c->vc.lock, flags);
752 static int sa11x0_dma_device_terminate_all(struct dma_chan *chan)
754 struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
755 struct sa11x0_dma_dev *d = to_sa11x0_dma(chan->device);
756 struct sa11x0_dma_phy *p;
760 dev_dbg(d->slave.dev, "vchan %p: terminate all\n", &c->vc);
761 /* Clear the tx descriptor lists */
762 spin_lock_irqsave(&c->vc.lock, flags);
763 vchan_get_all_descriptors(&c->vc, &head);
767 dev_dbg(d->slave.dev, "pchan %u: terminating\n", p->num);
768 /* vchan is assigned to a pchan - stop the channel */
769 writel(DCSR_RUN | DCSR_IE |
770 DCSR_STRTA | DCSR_DONEA |
771 DCSR_STRTB | DCSR_DONEB,
772 p->base + DMA_DCSR_C);
775 if (p->txd_load != p->txd_done)
776 list_add_tail(&p->txd_load->vd.node, &head);
780 list_add_tail(&p->txd_done->vd.node, &head);
786 spin_unlock(&d->lock);
787 tasklet_schedule(&d->task);
789 spin_unlock_irqrestore(&c->vc.lock, flags);
790 vchan_dma_desc_free_list(&c->vc, &head);
795 struct sa11x0_dma_channel_desc {
800 #define CD(d1, d2) { .ddar = DDAR_##d1 | d2, .name = #d1 }
801 static const struct sa11x0_dma_channel_desc chan_desc[] = {
803 CD(Ser0UDCRc, DDAR_RW),
805 CD(Ser1SDLCRc, DDAR_RW),
807 CD(Ser1UARTRc, DDAR_RW),
809 CD(Ser2ICPRc, DDAR_RW),
811 CD(Ser3UARTRc, DDAR_RW),
813 CD(Ser4MCP0Rc, DDAR_RW),
815 CD(Ser4MCP1Rc, DDAR_RW),
817 CD(Ser4SSPRc, DDAR_RW),
820 static const struct dma_slave_map sa11x0_dma_map[] = {
821 { "sa11x0-ir", "tx", "Ser2ICPTr" },
822 { "sa11x0-ir", "rx", "Ser2ICPRc" },
823 { "sa11x0-ssp", "tx", "Ser4SSPTr" },
824 { "sa11x0-ssp", "rx", "Ser4SSPRc" },
827 static bool sa11x0_dma_filter_fn(struct dma_chan *chan, void *param)
829 struct sa11x0_dma_chan *c = to_sa11x0_dma_chan(chan);
830 const char *p = param;
832 return !strcmp(c->name, p);
835 static int sa11x0_dma_init_dmadev(struct dma_device *dmadev,
840 INIT_LIST_HEAD(&dmadev->channels);
842 dmadev->device_free_chan_resources = sa11x0_dma_free_chan_resources;
843 dmadev->device_config = sa11x0_dma_device_config;
844 dmadev->device_pause = sa11x0_dma_device_pause;
845 dmadev->device_resume = sa11x0_dma_device_resume;
846 dmadev->device_terminate_all = sa11x0_dma_device_terminate_all;
847 dmadev->device_tx_status = sa11x0_dma_tx_status;
848 dmadev->device_issue_pending = sa11x0_dma_issue_pending;
850 for (i = 0; i < ARRAY_SIZE(chan_desc); i++) {
851 struct sa11x0_dma_chan *c;
853 c = kzalloc(sizeof(*c), GFP_KERNEL);
855 dev_err(dev, "no memory for channel %u\n", i);
859 c->status = DMA_IN_PROGRESS;
860 c->ddar = chan_desc[i].ddar;
861 c->name = chan_desc[i].name;
862 INIT_LIST_HEAD(&c->node);
864 c->vc.desc_free = sa11x0_dma_free_desc;
865 vchan_init(&c->vc, dmadev);
868 return dma_async_device_register(dmadev);
871 static int sa11x0_dma_request_irq(struct platform_device *pdev, int nr,
874 int irq = platform_get_irq(pdev, nr);
879 return request_irq(irq, sa11x0_dma_irq, 0, dev_name(&pdev->dev), data);
882 static void sa11x0_dma_free_irq(struct platform_device *pdev, int nr,
885 int irq = platform_get_irq(pdev, nr);
890 static void sa11x0_dma_free_channels(struct dma_device *dmadev)
892 struct sa11x0_dma_chan *c, *cn;
894 list_for_each_entry_safe(c, cn, &dmadev->channels, vc.chan.device_node) {
895 list_del(&c->vc.chan.device_node);
896 tasklet_kill(&c->vc.task);
901 static int sa11x0_dma_probe(struct platform_device *pdev)
903 struct sa11x0_dma_dev *d;
904 struct resource *res;
908 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
912 d = kzalloc(sizeof(*d), GFP_KERNEL);
918 spin_lock_init(&d->lock);
919 INIT_LIST_HEAD(&d->chan_pending);
921 d->slave.filter.fn = sa11x0_dma_filter_fn;
922 d->slave.filter.mapcnt = ARRAY_SIZE(sa11x0_dma_map);
923 d->slave.filter.map = sa11x0_dma_map;
925 d->base = ioremap(res->start, resource_size(res));
931 tasklet_setup(&d->task, sa11x0_dma_tasklet);
933 for (i = 0; i < NR_PHY_CHAN; i++) {
934 struct sa11x0_dma_phy *p = &d->phy[i];
938 p->base = d->base + i * DMA_SIZE;
939 writel_relaxed(DCSR_RUN | DCSR_IE | DCSR_ERROR |
940 DCSR_DONEA | DCSR_STRTA | DCSR_DONEB | DCSR_STRTB,
941 p->base + DMA_DCSR_C);
942 writel_relaxed(0, p->base + DMA_DDAR);
944 ret = sa11x0_dma_request_irq(pdev, i, p);
948 sa11x0_dma_free_irq(pdev, i, &d->phy[i]);
954 dma_cap_set(DMA_SLAVE, d->slave.cap_mask);
955 dma_cap_set(DMA_CYCLIC, d->slave.cap_mask);
956 d->slave.device_prep_slave_sg = sa11x0_dma_prep_slave_sg;
957 d->slave.device_prep_dma_cyclic = sa11x0_dma_prep_dma_cyclic;
958 d->slave.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
959 d->slave.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
960 d->slave.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
961 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES);
962 d->slave.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
963 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES);
964 ret = sa11x0_dma_init_dmadev(&d->slave, &pdev->dev);
966 dev_warn(d->slave.dev, "failed to register slave async device: %d\n",
971 platform_set_drvdata(pdev, d);
975 sa11x0_dma_free_channels(&d->slave);
976 for (i = 0; i < NR_PHY_CHAN; i++)
977 sa11x0_dma_free_irq(pdev, i, &d->phy[i]);
979 tasklet_kill(&d->task);
987 static int sa11x0_dma_remove(struct platform_device *pdev)
989 struct sa11x0_dma_dev *d = platform_get_drvdata(pdev);
992 dma_async_device_unregister(&d->slave);
994 sa11x0_dma_free_channels(&d->slave);
995 for (pch = 0; pch < NR_PHY_CHAN; pch++)
996 sa11x0_dma_free_irq(pdev, pch, &d->phy[pch]);
997 tasklet_kill(&d->task);
1004 static int sa11x0_dma_suspend(struct device *dev)
1006 struct sa11x0_dma_dev *d = dev_get_drvdata(dev);
1009 for (pch = 0; pch < NR_PHY_CHAN; pch++) {
1010 struct sa11x0_dma_phy *p = &d->phy[pch];
1011 u32 dcsr, saved_dcsr;
1013 dcsr = saved_dcsr = readl_relaxed(p->base + DMA_DCSR_R);
1014 if (dcsr & DCSR_RUN) {
1015 writel(DCSR_RUN | DCSR_IE, p->base + DMA_DCSR_C);
1016 dcsr = readl_relaxed(p->base + DMA_DCSR_R);
1019 saved_dcsr &= DCSR_RUN | DCSR_IE;
1020 if (dcsr & DCSR_BIU) {
1021 p->dbs[0] = readl_relaxed(p->base + DMA_DBSB);
1022 p->dbt[0] = readl_relaxed(p->base + DMA_DBTB);
1023 p->dbs[1] = readl_relaxed(p->base + DMA_DBSA);
1024 p->dbt[1] = readl_relaxed(p->base + DMA_DBTA);
1025 saved_dcsr |= (dcsr & DCSR_STRTA ? DCSR_STRTB : 0) |
1026 (dcsr & DCSR_STRTB ? DCSR_STRTA : 0);
1028 p->dbs[0] = readl_relaxed(p->base + DMA_DBSA);
1029 p->dbt[0] = readl_relaxed(p->base + DMA_DBTA);
1030 p->dbs[1] = readl_relaxed(p->base + DMA_DBSB);
1031 p->dbt[1] = readl_relaxed(p->base + DMA_DBTB);
1032 saved_dcsr |= dcsr & (DCSR_STRTA | DCSR_STRTB);
1034 p->dcsr = saved_dcsr;
1036 writel(DCSR_STRTA | DCSR_STRTB, p->base + DMA_DCSR_C);
1042 static int sa11x0_dma_resume(struct device *dev)
1044 struct sa11x0_dma_dev *d = dev_get_drvdata(dev);
1047 for (pch = 0; pch < NR_PHY_CHAN; pch++) {
1048 struct sa11x0_dma_phy *p = &d->phy[pch];
1049 struct sa11x0_dma_desc *txd = NULL;
1050 u32 dcsr = readl_relaxed(p->base + DMA_DCSR_R);
1052 WARN_ON(dcsr & (DCSR_BIU | DCSR_STRTA | DCSR_STRTB | DCSR_RUN));
1056 else if (p->txd_load)
1062 writel_relaxed(txd->ddar, p->base + DMA_DDAR);
1064 writel_relaxed(p->dbs[0], p->base + DMA_DBSA);
1065 writel_relaxed(p->dbt[0], p->base + DMA_DBTA);
1066 writel_relaxed(p->dbs[1], p->base + DMA_DBSB);
1067 writel_relaxed(p->dbt[1], p->base + DMA_DBTB);
1068 writel_relaxed(p->dcsr, p->base + DMA_DCSR_S);
1074 static const struct dev_pm_ops sa11x0_dma_pm_ops = {
1075 .suspend_noirq = sa11x0_dma_suspend,
1076 .resume_noirq = sa11x0_dma_resume,
1077 .freeze_noirq = sa11x0_dma_suspend,
1078 .thaw_noirq = sa11x0_dma_resume,
1079 .poweroff_noirq = sa11x0_dma_suspend,
1080 .restore_noirq = sa11x0_dma_resume,
1083 static struct platform_driver sa11x0_dma_driver = {
1085 .name = "sa11x0-dma",
1086 .pm = &sa11x0_dma_pm_ops,
1088 .probe = sa11x0_dma_probe,
1089 .remove = sa11x0_dma_remove,
1092 static int __init sa11x0_dma_init(void)
1094 return platform_driver_register(&sa11x0_dma_driver);
1096 subsys_initcall(sa11x0_dma_init);
1098 static void __exit sa11x0_dma_exit(void)
1100 platform_driver_unregister(&sa11x0_dma_driver);
1102 module_exit(sa11x0_dma_exit);
1104 MODULE_AUTHOR("Russell King");
1105 MODULE_DESCRIPTION("SA-11x0 DMA driver");
1106 MODULE_LICENSE("GPL v2");
1107 MODULE_ALIAS("platform:sa11x0-dma");