2 * Copyright (C) 2013-2014 Renesas Electronics Europe Ltd.
3 * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
10 #include <linux/bitmap.h>
11 #include <linux/bitops.h>
12 #include <linux/clk.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/dmaengine.h>
15 #include <linux/err.h>
16 #include <linux/interrupt.h>
18 #include <linux/log2.h>
19 #include <linux/module.h>
21 #include <linux/of_device.h>
22 #include <linux/of_dma.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
26 #include <dt-bindings/dma/nbpfaxi.h>
28 #include "dmaengine.h"
30 #define NBPF_REG_CHAN_OFFSET 0
31 #define NBPF_REG_CHAN_SIZE 0x40
33 /* Channel Current Transaction Byte register */
34 #define NBPF_CHAN_CUR_TR_BYTE 0x20
36 /* Channel Status register */
37 #define NBPF_CHAN_STAT 0x24
38 #define NBPF_CHAN_STAT_EN 1
39 #define NBPF_CHAN_STAT_TACT 4
40 #define NBPF_CHAN_STAT_ERR 0x10
41 #define NBPF_CHAN_STAT_END 0x20
42 #define NBPF_CHAN_STAT_TC 0x40
43 #define NBPF_CHAN_STAT_DER 0x400
45 /* Channel Control register */
46 #define NBPF_CHAN_CTRL 0x28
47 #define NBPF_CHAN_CTRL_SETEN 1
48 #define NBPF_CHAN_CTRL_CLREN 2
49 #define NBPF_CHAN_CTRL_STG 4
50 #define NBPF_CHAN_CTRL_SWRST 8
51 #define NBPF_CHAN_CTRL_CLRRQ 0x10
52 #define NBPF_CHAN_CTRL_CLREND 0x20
53 #define NBPF_CHAN_CTRL_CLRTC 0x40
54 #define NBPF_CHAN_CTRL_SETSUS 0x100
55 #define NBPF_CHAN_CTRL_CLRSUS 0x200
57 /* Channel Configuration register */
58 #define NBPF_CHAN_CFG 0x2c
59 #define NBPF_CHAN_CFG_SEL 7 /* terminal SELect: 0..7 */
60 #define NBPF_CHAN_CFG_REQD 8 /* REQuest Direction: DMAREQ is 0: input, 1: output */
61 #define NBPF_CHAN_CFG_LOEN 0x10 /* LOw ENable: low DMA request line is: 0: inactive, 1: active */
62 #define NBPF_CHAN_CFG_HIEN 0x20 /* HIgh ENable: high DMA request line is: 0: inactive, 1: active */
63 #define NBPF_CHAN_CFG_LVL 0x40 /* LeVeL: DMA request line is sensed as 0: edge, 1: level */
64 #define NBPF_CHAN_CFG_AM 0x700 /* ACK Mode: 0: Pulse mode, 1: Level mode, b'1x: Bus Cycle */
65 #define NBPF_CHAN_CFG_SDS 0xf000 /* Source Data Size: 0: 8 bits,... , 7: 1024 bits */
66 #define NBPF_CHAN_CFG_DDS 0xf0000 /* Destination Data Size: as above */
67 #define NBPF_CHAN_CFG_SAD 0x100000 /* Source ADdress counting: 0: increment, 1: fixed */
68 #define NBPF_CHAN_CFG_DAD 0x200000 /* Destination ADdress counting: 0: increment, 1: fixed */
69 #define NBPF_CHAN_CFG_TM 0x400000 /* Transfer Mode: 0: single, 1: block TM */
70 #define NBPF_CHAN_CFG_DEM 0x1000000 /* DMAEND interrupt Mask */
71 #define NBPF_CHAN_CFG_TCM 0x2000000 /* DMATCO interrupt Mask */
72 #define NBPF_CHAN_CFG_SBE 0x8000000 /* Sweep Buffer Enable */
73 #define NBPF_CHAN_CFG_RSEL 0x10000000 /* RM: Register Set sELect */
74 #define NBPF_CHAN_CFG_RSW 0x20000000 /* RM: Register Select sWitch */
75 #define NBPF_CHAN_CFG_REN 0x40000000 /* RM: Register Set Enable */
76 #define NBPF_CHAN_CFG_DMS 0x80000000 /* 0: register mode (RM), 1: link mode (LM) */
78 #define NBPF_CHAN_NXLA 0x38
79 #define NBPF_CHAN_CRLA 0x3c
81 /* Link Header field */
82 #define NBPF_HEADER_LV 1
83 #define NBPF_HEADER_LE 2
84 #define NBPF_HEADER_WBD 4
85 #define NBPF_HEADER_DIM 8
87 #define NBPF_CTRL 0x300
88 #define NBPF_CTRL_PR 1 /* 0: fixed priority, 1: round robin */
89 #define NBPF_CTRL_LVINT 2 /* DMAEND and DMAERR signalling: 0: pulse, 1: level */
91 #define NBPF_DSTAT_ER 0x314
92 #define NBPF_DSTAT_END 0x318
94 #define NBPF_DMA_BUSWIDTHS \
95 (BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
96 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
97 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
98 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
99 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES))
107 * We've got 3 types of objects, used to describe DMA transfers:
108 * 1. high-level descriptor, containing a struct dma_async_tx_descriptor object
109 * in it, used to communicate with the user
110 * 2. hardware DMA link descriptors, that we pass to DMAC for DMA transfer
111 * queuing, these must be DMAable, using either the streaming DMA API or
112 * allocated from coherent memory - one per SG segment
113 * 3. one per SG segment descriptors, used to manage HW link descriptors from
114 * (2). They do not have to be DMAable. They can either be (a) allocated
115 * together with link descriptors as mixed (DMA / CPU) objects, or (b)
116 * separately. Even if allocated separately it would be best to link them
117 * to link descriptors once during channel resource allocation and always
118 * use them as a single object.
119 * Therefore for both cases (a) and (b) at run-time objects (2) and (3) shall be
120 * treated as a single SG segment descriptor.
123 struct nbpf_link_reg {
127 u32 transaction_size;
138 struct nbpf_link_desc {
139 struct nbpf_link_reg *hwdesc;
140 dma_addr_t hwdesc_dma_addr;
141 struct nbpf_desc *desc;
142 struct list_head node;
146 * struct nbpf_desc - DMA transfer descriptor
147 * @async_tx: dmaengine object
148 * @user_wait: waiting for a user ack
149 * @length: total transfer length
150 * @sg: list of hardware descriptors, represented by struct nbpf_link_desc
151 * @node: member in channel descriptor lists
154 struct dma_async_tx_descriptor async_tx;
157 struct nbpf_channel *chan;
159 struct list_head node;
162 /* Take a wild guess: allocate 4 segments per descriptor */
163 #define NBPF_SEGMENTS_PER_DESC 4
164 #define NBPF_DESCS_PER_PAGE ((PAGE_SIZE - sizeof(struct list_head)) / \
165 (sizeof(struct nbpf_desc) + \
166 NBPF_SEGMENTS_PER_DESC * \
167 (sizeof(struct nbpf_link_desc) + sizeof(struct nbpf_link_reg))))
168 #define NBPF_SEGMENTS_PER_PAGE (NBPF_SEGMENTS_PER_DESC * NBPF_DESCS_PER_PAGE)
170 struct nbpf_desc_page {
171 struct list_head node;
172 struct nbpf_desc desc[NBPF_DESCS_PER_PAGE];
173 struct nbpf_link_desc ldesc[NBPF_SEGMENTS_PER_PAGE];
174 struct nbpf_link_reg hwdesc[NBPF_SEGMENTS_PER_PAGE];
178 * struct nbpf_channel - one DMAC channel
179 * @dma_chan: standard dmaengine channel object
180 * @base: register address base
184 * @slave_addr: address for slave DMA
185 * @slave_width:slave data size in bytes
186 * @slave_burst:maximum slave burst size in bytes
187 * @terminal: DMA terminal, assigned to this channel
188 * @dmarq_cfg: DMA request line configuration - high / low, edge / level for NBPF_CHAN_CFG
189 * @flags: configuration flags from DT
190 * @lock: protect descriptor lists
191 * @free_links: list of free link descriptors
192 * @free: list of free descriptors
193 * @queued: list of queued descriptors
194 * @active: list of descriptors, scheduled for processing
195 * @done: list of completed descriptors, waiting post-processing
196 * @desc_page: list of additionally allocated descriptor pages - if any
198 struct nbpf_channel {
199 struct dma_chan dma_chan;
200 struct tasklet_struct tasklet;
202 struct nbpf_device *nbpf;
205 dma_addr_t slave_src_addr;
206 size_t slave_src_width;
207 size_t slave_src_burst;
208 dma_addr_t slave_dst_addr;
209 size_t slave_dst_width;
210 size_t slave_dst_burst;
211 unsigned int terminal;
215 struct list_head free_links;
216 struct list_head free;
217 struct list_head queued;
218 struct list_head active;
219 struct list_head done;
220 struct list_head desc_page;
221 struct nbpf_desc *running;
226 struct dma_device dma_dev;
229 const struct nbpf_config *config;
230 struct nbpf_channel chan[];
245 static struct nbpf_config nbpf_cfg[] = {
284 #define nbpf_to_chan(d) container_of(d, struct nbpf_channel, dma_chan)
287 * dmaengine drivers seem to have a lot in common and instead of sharing more
288 * code, they reimplement those common algorithms independently. In this driver
289 * we try to separate the hardware-specific part from the (largely) generic
290 * part. This improves code readability and makes it possible in the future to
291 * reuse the generic code in form of a helper library. That generic code should
292 * be suitable for various DMA controllers, using transfer descriptors in RAM
293 * and pushing one SG list at a time to the DMA controller.
296 /* Hardware-specific part */
298 static inline u32 nbpf_chan_read(struct nbpf_channel *chan,
301 u32 data = ioread32(chan->base + offset);
302 dev_dbg(chan->dma_chan.device->dev, "%s(0x%p + 0x%x) = 0x%x\n",
303 __func__, chan->base, offset, data);
307 static inline void nbpf_chan_write(struct nbpf_channel *chan,
308 unsigned int offset, u32 data)
310 iowrite32(data, chan->base + offset);
311 dev_dbg(chan->dma_chan.device->dev, "%s(0x%p + 0x%x) = 0x%x\n",
312 __func__, chan->base, offset, data);
315 static inline u32 nbpf_read(struct nbpf_device *nbpf,
318 u32 data = ioread32(nbpf->base + offset);
319 dev_dbg(nbpf->dma_dev.dev, "%s(0x%p + 0x%x) = 0x%x\n",
320 __func__, nbpf->base, offset, data);
324 static inline void nbpf_write(struct nbpf_device *nbpf,
325 unsigned int offset, u32 data)
327 iowrite32(data, nbpf->base + offset);
328 dev_dbg(nbpf->dma_dev.dev, "%s(0x%p + 0x%x) = 0x%x\n",
329 __func__, nbpf->base, offset, data);
332 static void nbpf_chan_halt(struct nbpf_channel *chan)
334 nbpf_chan_write(chan, NBPF_CHAN_CTRL, NBPF_CHAN_CTRL_CLREN);
337 static bool nbpf_status_get(struct nbpf_channel *chan)
339 u32 status = nbpf_read(chan->nbpf, NBPF_DSTAT_END);
341 return status & BIT(chan - chan->nbpf->chan);
344 static void nbpf_status_ack(struct nbpf_channel *chan)
346 nbpf_chan_write(chan, NBPF_CHAN_CTRL, NBPF_CHAN_CTRL_CLREND);
349 static u32 nbpf_error_get(struct nbpf_device *nbpf)
351 return nbpf_read(nbpf, NBPF_DSTAT_ER);
354 static struct nbpf_channel *nbpf_error_get_channel(struct nbpf_device *nbpf, u32 error)
356 return nbpf->chan + __ffs(error);
359 static void nbpf_error_clear(struct nbpf_channel *chan)
364 /* Stop the channel, make sure DMA has been aborted */
365 nbpf_chan_halt(chan);
367 for (i = 1000; i; i--) {
368 status = nbpf_chan_read(chan, NBPF_CHAN_STAT);
369 if (!(status & NBPF_CHAN_STAT_TACT))
375 dev_err(chan->dma_chan.device->dev,
376 "%s(): abort timeout, channel status 0x%x\n", __func__, status);
378 nbpf_chan_write(chan, NBPF_CHAN_CTRL, NBPF_CHAN_CTRL_SWRST);
381 static int nbpf_start(struct nbpf_desc *desc)
383 struct nbpf_channel *chan = desc->chan;
384 struct nbpf_link_desc *ldesc = list_first_entry(&desc->sg, struct nbpf_link_desc, node);
386 nbpf_chan_write(chan, NBPF_CHAN_NXLA, (u32)ldesc->hwdesc_dma_addr);
387 nbpf_chan_write(chan, NBPF_CHAN_CTRL, NBPF_CHAN_CTRL_SETEN | NBPF_CHAN_CTRL_CLRSUS);
388 chan->paused = false;
390 /* Software trigger MEMCPY - only MEMCPY uses the block mode */
391 if (ldesc->hwdesc->config & NBPF_CHAN_CFG_TM)
392 nbpf_chan_write(chan, NBPF_CHAN_CTRL, NBPF_CHAN_CTRL_STG);
394 dev_dbg(chan->nbpf->dma_dev.dev, "%s(): next 0x%x, cur 0x%x\n", __func__,
395 nbpf_chan_read(chan, NBPF_CHAN_NXLA), nbpf_chan_read(chan, NBPF_CHAN_CRLA));
400 static void nbpf_chan_prepare(struct nbpf_channel *chan)
402 chan->dmarq_cfg = (chan->flags & NBPF_SLAVE_RQ_HIGH ? NBPF_CHAN_CFG_HIEN : 0) |
403 (chan->flags & NBPF_SLAVE_RQ_LOW ? NBPF_CHAN_CFG_LOEN : 0) |
404 (chan->flags & NBPF_SLAVE_RQ_LEVEL ?
405 NBPF_CHAN_CFG_LVL | (NBPF_CHAN_CFG_AM & 0x200) : 0) |
409 static void nbpf_chan_prepare_default(struct nbpf_channel *chan)
411 /* Don't output DMAACK */
412 chan->dmarq_cfg = NBPF_CHAN_CFG_AM & 0x400;
417 static void nbpf_chan_configure(struct nbpf_channel *chan)
420 * We assume, that only the link mode and DMA request line configuration
421 * have to be set in the configuration register manually. Dynamic
422 * per-transfer configuration will be loaded from transfer descriptors.
424 nbpf_chan_write(chan, NBPF_CHAN_CFG, NBPF_CHAN_CFG_DMS | chan->dmarq_cfg);
427 static u32 nbpf_xfer_ds(struct nbpf_device *nbpf, size_t size)
429 /* Maximum supported bursts depend on the buffer size */
430 return min_t(int, __ffs(size), ilog2(nbpf->config->buffer_size * 8));
433 static size_t nbpf_xfer_size(struct nbpf_device *nbpf,
434 enum dma_slave_buswidth width, u32 burst)
442 case DMA_SLAVE_BUSWIDTH_8_BYTES:
446 case DMA_SLAVE_BUSWIDTH_4_BYTES:
450 case DMA_SLAVE_BUSWIDTH_2_BYTES:
455 pr_warn("%s(): invalid bus width %u\n", __func__, width);
456 case DMA_SLAVE_BUSWIDTH_1_BYTE:
460 return nbpf_xfer_ds(nbpf, size);
464 * We need a way to recognise slaves, whose data is sent "raw" over the bus,
465 * i.e. it isn't known in advance how many bytes will be received. Therefore
466 * the slave driver has to provide a "large enough" buffer and either read the
467 * buffer, when it is full, or detect, that some data has arrived, then wait for
468 * a timeout, if no more data arrives - receive what's already there. We want to
469 * handle such slaves in a special way to allow an optimised mode for other
470 * users, for whom the amount of data is known in advance. So far there's no way
471 * to recognise such slaves. We use a data-width check to distinguish between
472 * the SD host and the PL011 UART.
475 static int nbpf_prep_one(struct nbpf_link_desc *ldesc,
476 enum dma_transfer_direction direction,
477 dma_addr_t src, dma_addr_t dst, size_t size, bool last)
479 struct nbpf_link_reg *hwdesc = ldesc->hwdesc;
480 struct nbpf_desc *desc = ldesc->desc;
481 struct nbpf_channel *chan = desc->chan;
482 struct device *dev = chan->dma_chan.device->dev;
483 size_t mem_xfer, slave_xfer;
486 hwdesc->header = NBPF_HEADER_WBD | NBPF_HEADER_LV |
487 (last ? NBPF_HEADER_LE : 0);
489 hwdesc->src_addr = src;
490 hwdesc->dst_addr = dst;
491 hwdesc->transaction_size = size;
494 * set config: SAD, DAD, DDS, SDS, etc.
495 * Note on transfer sizes: the DMAC can perform unaligned DMA transfers,
496 * but it is important to have transaction size a multiple of both
497 * receiver and transmitter transfer sizes. It is also possible to use
498 * different RAM and device transfer sizes, and it does work well with
499 * some devices, e.g. with V08R07S01E SD host controllers, which can use
500 * 128 byte transfers. But this doesn't work with other devices,
501 * especially when the transaction size is unknown. This is the case,
502 * e.g. with serial drivers like amba-pl011.c. For reception it sets up
503 * the transaction size of 4K and if fewer bytes are received, it
504 * pauses DMA and reads out data received via DMA as well as those left
505 * in the Rx FIFO. For this to work with the RAM side using burst
506 * transfers we enable the SBE bit and terminate the transfer in our
507 * .device_pause handler.
509 mem_xfer = nbpf_xfer_ds(chan->nbpf, size);
513 can_burst = chan->slave_src_width >= 3;
514 slave_xfer = min(mem_xfer, can_burst ?
515 chan->slave_src_burst : chan->slave_src_width);
517 * Is the slave narrower than 64 bits, i.e. isn't using the full
518 * bus width and cannot use bursts?
520 if (mem_xfer > chan->slave_src_burst && !can_burst)
521 mem_xfer = chan->slave_src_burst;
522 /* Device-to-RAM DMA is unreliable without REQD set */
523 hwdesc->config = NBPF_CHAN_CFG_SAD | (NBPF_CHAN_CFG_DDS & (mem_xfer << 16)) |
524 (NBPF_CHAN_CFG_SDS & (slave_xfer << 12)) | NBPF_CHAN_CFG_REQD |
529 slave_xfer = min(mem_xfer, chan->slave_dst_width >= 3 ?
530 chan->slave_dst_burst : chan->slave_dst_width);
531 hwdesc->config = NBPF_CHAN_CFG_DAD | (NBPF_CHAN_CFG_SDS & (mem_xfer << 12)) |
532 (NBPF_CHAN_CFG_DDS & (slave_xfer << 16)) | NBPF_CHAN_CFG_REQD;
536 hwdesc->config = NBPF_CHAN_CFG_TCM | NBPF_CHAN_CFG_TM |
537 (NBPF_CHAN_CFG_SDS & (mem_xfer << 12)) |
538 (NBPF_CHAN_CFG_DDS & (mem_xfer << 16));
545 hwdesc->config |= chan->dmarq_cfg | (last ? 0 : NBPF_CHAN_CFG_DEM) |
548 dev_dbg(dev, "%s(): desc @ %pad: hdr 0x%x, cfg 0x%x, %zu @ %pad -> %pad\n",
549 __func__, &ldesc->hwdesc_dma_addr, hwdesc->header,
550 hwdesc->config, size, &src, &dst);
552 dma_sync_single_for_device(dev, ldesc->hwdesc_dma_addr, sizeof(*hwdesc),
558 static size_t nbpf_bytes_left(struct nbpf_channel *chan)
560 return nbpf_chan_read(chan, NBPF_CHAN_CUR_TR_BYTE);
563 static void nbpf_configure(struct nbpf_device *nbpf)
565 nbpf_write(nbpf, NBPF_CTRL, NBPF_CTRL_LVINT);
570 /* DMA ENGINE functions */
571 static void nbpf_issue_pending(struct dma_chan *dchan)
573 struct nbpf_channel *chan = nbpf_to_chan(dchan);
576 dev_dbg(dchan->device->dev, "Entry %s()\n", __func__);
578 spin_lock_irqsave(&chan->lock, flags);
579 if (list_empty(&chan->queued))
582 list_splice_tail_init(&chan->queued, &chan->active);
584 if (!chan->running) {
585 struct nbpf_desc *desc = list_first_entry(&chan->active,
586 struct nbpf_desc, node);
587 if (!nbpf_start(desc))
588 chan->running = desc;
592 spin_unlock_irqrestore(&chan->lock, flags);
595 static enum dma_status nbpf_tx_status(struct dma_chan *dchan,
596 dma_cookie_t cookie, struct dma_tx_state *state)
598 struct nbpf_channel *chan = nbpf_to_chan(dchan);
599 enum dma_status status = dma_cookie_status(dchan, cookie, state);
602 dma_cookie_t running;
605 spin_lock_irqsave(&chan->lock, flags);
606 running = chan->running ? chan->running->async_tx.cookie : -EINVAL;
608 if (cookie == running) {
609 state->residue = nbpf_bytes_left(chan);
610 dev_dbg(dchan->device->dev, "%s(): residue %u\n", __func__,
612 } else if (status == DMA_IN_PROGRESS) {
613 struct nbpf_desc *desc;
616 list_for_each_entry(desc, &chan->active, node)
617 if (desc->async_tx.cookie == cookie) {
623 list_for_each_entry(desc, &chan->queued, node)
624 if (desc->async_tx.cookie == cookie) {
630 state->residue = found ? desc->length : 0;
633 spin_unlock_irqrestore(&chan->lock, flags);
642 static dma_cookie_t nbpf_tx_submit(struct dma_async_tx_descriptor *tx)
644 struct nbpf_desc *desc = container_of(tx, struct nbpf_desc, async_tx);
645 struct nbpf_channel *chan = desc->chan;
649 spin_lock_irqsave(&chan->lock, flags);
650 cookie = dma_cookie_assign(tx);
651 list_add_tail(&desc->node, &chan->queued);
652 spin_unlock_irqrestore(&chan->lock, flags);
654 dev_dbg(chan->dma_chan.device->dev, "Entry %s(%d)\n", __func__, cookie);
659 static int nbpf_desc_page_alloc(struct nbpf_channel *chan)
661 struct dma_chan *dchan = &chan->dma_chan;
662 struct nbpf_desc_page *dpage = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
663 struct nbpf_link_desc *ldesc;
664 struct nbpf_link_reg *hwdesc;
665 struct nbpf_desc *desc;
669 struct device *dev = dchan->device->dev;
674 dev_dbg(dev, "%s(): alloc %lu descriptors, %lu segments, total alloc %zu\n",
675 __func__, NBPF_DESCS_PER_PAGE, NBPF_SEGMENTS_PER_PAGE, sizeof(*dpage));
677 for (i = 0, ldesc = dpage->ldesc, hwdesc = dpage->hwdesc;
678 i < ARRAY_SIZE(dpage->ldesc);
679 i++, ldesc++, hwdesc++) {
680 ldesc->hwdesc = hwdesc;
681 list_add_tail(&ldesc->node, &lhead);
682 ldesc->hwdesc_dma_addr = dma_map_single(dchan->device->dev,
683 hwdesc, sizeof(*hwdesc), DMA_TO_DEVICE);
685 dev_dbg(dev, "%s(): mapped 0x%p to %pad\n", __func__,
686 hwdesc, &ldesc->hwdesc_dma_addr);
689 for (i = 0, desc = dpage->desc;
690 i < ARRAY_SIZE(dpage->desc);
692 dma_async_tx_descriptor_init(&desc->async_tx, dchan);
693 desc->async_tx.tx_submit = nbpf_tx_submit;
695 INIT_LIST_HEAD(&desc->sg);
696 list_add_tail(&desc->node, &head);
700 * This function cannot be called from interrupt context, so, no need to
703 spin_lock_irq(&chan->lock);
704 list_splice_tail(&lhead, &chan->free_links);
705 list_splice_tail(&head, &chan->free);
706 list_add(&dpage->node, &chan->desc_page);
707 spin_unlock_irq(&chan->lock);
709 return ARRAY_SIZE(dpage->desc);
712 static void nbpf_desc_put(struct nbpf_desc *desc)
714 struct nbpf_channel *chan = desc->chan;
715 struct nbpf_link_desc *ldesc, *tmp;
718 spin_lock_irqsave(&chan->lock, flags);
719 list_for_each_entry_safe(ldesc, tmp, &desc->sg, node)
720 list_move(&ldesc->node, &chan->free_links);
722 list_add(&desc->node, &chan->free);
723 spin_unlock_irqrestore(&chan->lock, flags);
726 static void nbpf_scan_acked(struct nbpf_channel *chan)
728 struct nbpf_desc *desc, *tmp;
732 spin_lock_irqsave(&chan->lock, flags);
733 list_for_each_entry_safe(desc, tmp, &chan->done, node)
734 if (async_tx_test_ack(&desc->async_tx) && desc->user_wait) {
735 list_move(&desc->node, &head);
736 desc->user_wait = false;
738 spin_unlock_irqrestore(&chan->lock, flags);
740 list_for_each_entry_safe(desc, tmp, &head, node) {
741 list_del(&desc->node);
747 * We have to allocate descriptors with the channel lock dropped. This means,
748 * before we re-acquire the lock buffers can be taken already, so we have to
749 * re-check after re-acquiring the lock and possibly retry, if buffers are gone
752 static struct nbpf_desc *nbpf_desc_get(struct nbpf_channel *chan, size_t len)
754 struct nbpf_desc *desc = NULL;
755 struct nbpf_link_desc *ldesc, *prev = NULL;
757 nbpf_scan_acked(chan);
759 spin_lock_irq(&chan->lock);
764 if (list_empty(&chan->free)) {
765 /* No more free descriptors */
766 spin_unlock_irq(&chan->lock);
767 ret = nbpf_desc_page_alloc(chan);
770 spin_lock_irq(&chan->lock);
773 desc = list_first_entry(&chan->free, struct nbpf_desc, node);
774 list_del(&desc->node);
777 if (list_empty(&chan->free_links)) {
778 /* No more free link descriptors */
779 spin_unlock_irq(&chan->lock);
780 ret = nbpf_desc_page_alloc(chan);
785 spin_lock_irq(&chan->lock);
789 ldesc = list_first_entry(&chan->free_links,
790 struct nbpf_link_desc, node);
793 prev->hwdesc->next = (u32)ldesc->hwdesc_dma_addr;
796 list_move_tail(&ldesc->node, &desc->sg);
802 prev->hwdesc->next = 0;
804 spin_unlock_irq(&chan->lock);
809 static void nbpf_chan_idle(struct nbpf_channel *chan)
811 struct nbpf_desc *desc, *tmp;
815 spin_lock_irqsave(&chan->lock, flags);
817 list_splice_init(&chan->done, &head);
818 list_splice_init(&chan->active, &head);
819 list_splice_init(&chan->queued, &head);
821 chan->running = NULL;
823 spin_unlock_irqrestore(&chan->lock, flags);
825 list_for_each_entry_safe(desc, tmp, &head, node) {
826 dev_dbg(chan->nbpf->dma_dev.dev, "%s(): force-free desc %p cookie %d\n",
827 __func__, desc, desc->async_tx.cookie);
828 list_del(&desc->node);
833 static int nbpf_pause(struct dma_chan *dchan)
835 struct nbpf_channel *chan = nbpf_to_chan(dchan);
837 dev_dbg(dchan->device->dev, "Entry %s\n", __func__);
840 nbpf_chan_write(chan, NBPF_CHAN_CTRL, NBPF_CHAN_CTRL_SETSUS);
841 /* See comment in nbpf_prep_one() */
842 nbpf_chan_write(chan, NBPF_CHAN_CTRL, NBPF_CHAN_CTRL_CLREN);
847 static int nbpf_terminate_all(struct dma_chan *dchan)
849 struct nbpf_channel *chan = nbpf_to_chan(dchan);
851 dev_dbg(dchan->device->dev, "Entry %s\n", __func__);
852 dev_dbg(dchan->device->dev, "Terminating\n");
854 nbpf_chan_halt(chan);
855 nbpf_chan_idle(chan);
860 static int nbpf_config(struct dma_chan *dchan,
861 struct dma_slave_config *config)
863 struct nbpf_channel *chan = nbpf_to_chan(dchan);
865 dev_dbg(dchan->device->dev, "Entry %s\n", __func__);
868 * We could check config->slave_id to match chan->terminal here,
869 * but with DT they would be coming from the same source, so
870 * such a check would be superflous
873 chan->slave_dst_addr = config->dst_addr;
874 chan->slave_dst_width = nbpf_xfer_size(chan->nbpf,
875 config->dst_addr_width, 1);
876 chan->slave_dst_burst = nbpf_xfer_size(chan->nbpf,
877 config->dst_addr_width,
878 config->dst_maxburst);
879 chan->slave_src_addr = config->src_addr;
880 chan->slave_src_width = nbpf_xfer_size(chan->nbpf,
881 config->src_addr_width, 1);
882 chan->slave_src_burst = nbpf_xfer_size(chan->nbpf,
883 config->src_addr_width,
884 config->src_maxburst);
889 static struct dma_async_tx_descriptor *nbpf_prep_sg(struct nbpf_channel *chan,
890 struct scatterlist *src_sg, struct scatterlist *dst_sg,
891 size_t len, enum dma_transfer_direction direction,
894 struct nbpf_link_desc *ldesc;
895 struct scatterlist *mem_sg;
896 struct nbpf_desc *desc;
897 bool inc_src, inc_dst;
921 desc = nbpf_desc_get(chan, len);
925 desc->async_tx.flags = flags;
926 desc->async_tx.cookie = -EBUSY;
927 desc->user_wait = false;
930 * This is a private descriptor list, and we own the descriptor. No need
933 list_for_each_entry(ldesc, &desc->sg, node) {
934 int ret = nbpf_prep_one(ldesc, direction,
935 sg_dma_address(src_sg),
936 sg_dma_address(dst_sg),
943 data_len += sg_dma_len(mem_sg);
945 src_sg = sg_next(src_sg);
947 dst_sg = sg_next(dst_sg);
948 mem_sg = direction == DMA_DEV_TO_MEM ? dst_sg : src_sg;
952 desc->length = data_len;
954 /* The user has to return the descriptor to us ASAP via .tx_submit() */
955 return &desc->async_tx;
958 static struct dma_async_tx_descriptor *nbpf_prep_memcpy(
959 struct dma_chan *dchan, dma_addr_t dst, dma_addr_t src,
960 size_t len, unsigned long flags)
962 struct nbpf_channel *chan = nbpf_to_chan(dchan);
963 struct scatterlist dst_sg;
964 struct scatterlist src_sg;
966 sg_init_table(&dst_sg, 1);
967 sg_init_table(&src_sg, 1);
969 sg_dma_address(&dst_sg) = dst;
970 sg_dma_address(&src_sg) = src;
972 sg_dma_len(&dst_sg) = len;
973 sg_dma_len(&src_sg) = len;
975 dev_dbg(dchan->device->dev, "%s(): %zu @ %pad -> %pad\n",
976 __func__, len, &src, &dst);
978 return nbpf_prep_sg(chan, &src_sg, &dst_sg, 1,
979 DMA_MEM_TO_MEM, flags);
982 static struct dma_async_tx_descriptor *nbpf_prep_memcpy_sg(
983 struct dma_chan *dchan,
984 struct scatterlist *dst_sg, unsigned int dst_nents,
985 struct scatterlist *src_sg, unsigned int src_nents,
988 struct nbpf_channel *chan = nbpf_to_chan(dchan);
990 if (dst_nents != src_nents)
993 return nbpf_prep_sg(chan, src_sg, dst_sg, src_nents,
994 DMA_MEM_TO_MEM, flags);
997 static struct dma_async_tx_descriptor *nbpf_prep_slave_sg(
998 struct dma_chan *dchan, struct scatterlist *sgl, unsigned int sg_len,
999 enum dma_transfer_direction direction, unsigned long flags, void *context)
1001 struct nbpf_channel *chan = nbpf_to_chan(dchan);
1002 struct scatterlist slave_sg;
1004 dev_dbg(dchan->device->dev, "Entry %s()\n", __func__);
1006 sg_init_table(&slave_sg, 1);
1008 switch (direction) {
1009 case DMA_MEM_TO_DEV:
1010 sg_dma_address(&slave_sg) = chan->slave_dst_addr;
1011 return nbpf_prep_sg(chan, sgl, &slave_sg, sg_len,
1014 case DMA_DEV_TO_MEM:
1015 sg_dma_address(&slave_sg) = chan->slave_src_addr;
1016 return nbpf_prep_sg(chan, &slave_sg, sgl, sg_len,
1024 static int nbpf_alloc_chan_resources(struct dma_chan *dchan)
1026 struct nbpf_channel *chan = nbpf_to_chan(dchan);
1029 INIT_LIST_HEAD(&chan->free);
1030 INIT_LIST_HEAD(&chan->free_links);
1031 INIT_LIST_HEAD(&chan->queued);
1032 INIT_LIST_HEAD(&chan->active);
1033 INIT_LIST_HEAD(&chan->done);
1035 ret = nbpf_desc_page_alloc(chan);
1039 dev_dbg(dchan->device->dev, "Entry %s(): terminal %u\n", __func__,
1042 nbpf_chan_configure(chan);
1047 static void nbpf_free_chan_resources(struct dma_chan *dchan)
1049 struct nbpf_channel *chan = nbpf_to_chan(dchan);
1050 struct nbpf_desc_page *dpage, *tmp;
1052 dev_dbg(dchan->device->dev, "Entry %s()\n", __func__);
1054 nbpf_chan_halt(chan);
1055 nbpf_chan_idle(chan);
1056 /* Clean up for if a channel is re-used for MEMCPY after slave DMA */
1057 nbpf_chan_prepare_default(chan);
1059 list_for_each_entry_safe(dpage, tmp, &chan->desc_page, node) {
1060 struct nbpf_link_desc *ldesc;
1062 list_del(&dpage->node);
1063 for (i = 0, ldesc = dpage->ldesc;
1064 i < ARRAY_SIZE(dpage->ldesc);
1066 dma_unmap_single(dchan->device->dev, ldesc->hwdesc_dma_addr,
1067 sizeof(*ldesc->hwdesc), DMA_TO_DEVICE);
1068 free_page((unsigned long)dpage);
1072 static struct dma_chan *nbpf_of_xlate(struct of_phandle_args *dma_spec,
1073 struct of_dma *ofdma)
1075 struct nbpf_device *nbpf = ofdma->of_dma_data;
1076 struct dma_chan *dchan;
1077 struct nbpf_channel *chan;
1079 if (dma_spec->args_count != 2)
1082 dchan = dma_get_any_slave_channel(&nbpf->dma_dev);
1086 dev_dbg(dchan->device->dev, "Entry %s(%s)\n", __func__,
1087 dma_spec->np->name);
1089 chan = nbpf_to_chan(dchan);
1091 chan->terminal = dma_spec->args[0];
1092 chan->flags = dma_spec->args[1];
1094 nbpf_chan_prepare(chan);
1095 nbpf_chan_configure(chan);
1100 static void nbpf_chan_tasklet(unsigned long data)
1102 struct nbpf_channel *chan = (struct nbpf_channel *)data;
1103 struct nbpf_desc *desc, *tmp;
1104 dma_async_tx_callback callback;
1107 while (!list_empty(&chan->done)) {
1108 bool found = false, must_put, recycling = false;
1110 spin_lock_irq(&chan->lock);
1112 list_for_each_entry_safe(desc, tmp, &chan->done, node) {
1113 if (!desc->user_wait) {
1114 /* Newly completed descriptor, have to process */
1117 } else if (async_tx_test_ack(&desc->async_tx)) {
1119 * This descriptor was waiting for a user ACK,
1120 * it can be recycled now.
1122 list_del(&desc->node);
1123 spin_unlock_irq(&chan->lock);
1124 nbpf_desc_put(desc);
1134 /* This can happen if TERMINATE_ALL has been called */
1135 spin_unlock_irq(&chan->lock);
1139 dma_cookie_complete(&desc->async_tx);
1142 * With released lock we cannot dereference desc, maybe it's
1143 * still on the "done" list
1145 if (async_tx_test_ack(&desc->async_tx)) {
1146 list_del(&desc->node);
1149 desc->user_wait = true;
1153 callback = desc->async_tx.callback;
1154 param = desc->async_tx.callback_param;
1156 /* ack and callback completed descriptor */
1157 spin_unlock_irq(&chan->lock);
1163 nbpf_desc_put(desc);
1167 static irqreturn_t nbpf_chan_irq(int irq, void *dev)
1169 struct nbpf_channel *chan = dev;
1170 bool done = nbpf_status_get(chan);
1171 struct nbpf_desc *desc;
1178 nbpf_status_ack(chan);
1180 dev_dbg(&chan->dma_chan.dev->device, "%s()\n", __func__);
1182 spin_lock(&chan->lock);
1183 desc = chan->running;
1184 if (WARN_ON(!desc)) {
1192 list_move_tail(&desc->node, &chan->done);
1193 chan->running = NULL;
1195 if (!list_empty(&chan->active)) {
1196 desc = list_first_entry(&chan->active,
1197 struct nbpf_desc, node);
1198 if (!nbpf_start(desc))
1199 chan->running = desc;
1203 spin_unlock(&chan->lock);
1206 tasklet_schedule(&chan->tasklet);
1211 static irqreturn_t nbpf_err_irq(int irq, void *dev)
1213 struct nbpf_device *nbpf = dev;
1214 u32 error = nbpf_error_get(nbpf);
1216 dev_warn(nbpf->dma_dev.dev, "DMA error IRQ %u\n", irq);
1222 struct nbpf_channel *chan = nbpf_error_get_channel(nbpf, error);
1223 /* On error: abort all queued transfers, no callback */
1224 nbpf_error_clear(chan);
1225 nbpf_chan_idle(chan);
1226 error = nbpf_error_get(nbpf);
1232 static int nbpf_chan_probe(struct nbpf_device *nbpf, int n)
1234 struct dma_device *dma_dev = &nbpf->dma_dev;
1235 struct nbpf_channel *chan = nbpf->chan + n;
1239 chan->base = nbpf->base + NBPF_REG_CHAN_OFFSET + NBPF_REG_CHAN_SIZE * n;
1240 INIT_LIST_HEAD(&chan->desc_page);
1241 spin_lock_init(&chan->lock);
1242 chan->dma_chan.device = dma_dev;
1243 dma_cookie_init(&chan->dma_chan);
1244 nbpf_chan_prepare_default(chan);
1246 dev_dbg(dma_dev->dev, "%s(): channel %d: -> %p\n", __func__, n, chan->base);
1248 snprintf(chan->name, sizeof(chan->name), "nbpf %d", n);
1250 tasklet_init(&chan->tasklet, nbpf_chan_tasklet, (unsigned long)chan);
1251 ret = devm_request_irq(dma_dev->dev, chan->irq,
1252 nbpf_chan_irq, IRQF_SHARED,
1257 /* Add the channel to DMA device channel list */
1258 list_add_tail(&chan->dma_chan.device_node,
1259 &dma_dev->channels);
1264 static const struct of_device_id nbpf_match[] = {
1265 {.compatible = "renesas,nbpfaxi64dmac1b4", .data = &nbpf_cfg[NBPF1B4]},
1266 {.compatible = "renesas,nbpfaxi64dmac1b8", .data = &nbpf_cfg[NBPF1B8]},
1267 {.compatible = "renesas,nbpfaxi64dmac1b16", .data = &nbpf_cfg[NBPF1B16]},
1268 {.compatible = "renesas,nbpfaxi64dmac4b4", .data = &nbpf_cfg[NBPF4B4]},
1269 {.compatible = "renesas,nbpfaxi64dmac4b8", .data = &nbpf_cfg[NBPF4B8]},
1270 {.compatible = "renesas,nbpfaxi64dmac4b16", .data = &nbpf_cfg[NBPF4B16]},
1271 {.compatible = "renesas,nbpfaxi64dmac8b4", .data = &nbpf_cfg[NBPF8B4]},
1272 {.compatible = "renesas,nbpfaxi64dmac8b8", .data = &nbpf_cfg[NBPF8B8]},
1273 {.compatible = "renesas,nbpfaxi64dmac8b16", .data = &nbpf_cfg[NBPF8B16]},
1276 MODULE_DEVICE_TABLE(of, nbpf_match);
1278 static int nbpf_probe(struct platform_device *pdev)
1280 struct device *dev = &pdev->dev;
1281 const struct of_device_id *of_id = of_match_device(nbpf_match, dev);
1282 struct device_node *np = dev->of_node;
1283 struct nbpf_device *nbpf;
1284 struct dma_device *dma_dev;
1285 struct resource *iomem, *irq_res;
1286 const struct nbpf_config *cfg;
1288 int ret, irq, eirq, i;
1289 int irqbuf[9] /* maximum 8 channels + error IRQ */;
1290 unsigned int irqs = 0;
1292 BUILD_BUG_ON(sizeof(struct nbpf_desc_page) > PAGE_SIZE);
1295 if (!np || !of_id || !of_id->data)
1299 num_channels = cfg->num_channels;
1301 nbpf = devm_kzalloc(dev, sizeof(*nbpf) + num_channels *
1302 sizeof(nbpf->chan[0]), GFP_KERNEL);
1304 dev_err(dev, "Memory allocation failed\n");
1307 dma_dev = &nbpf->dma_dev;
1310 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1311 nbpf->base = devm_ioremap_resource(dev, iomem);
1312 if (IS_ERR(nbpf->base))
1313 return PTR_ERR(nbpf->base);
1315 nbpf->clk = devm_clk_get(dev, NULL);
1316 if (IS_ERR(nbpf->clk))
1317 return PTR_ERR(nbpf->clk);
1321 for (i = 0; irqs < ARRAY_SIZE(irqbuf); i++) {
1322 irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
1326 for (irq = irq_res->start; irq <= irq_res->end;
1332 * 3 IRQ resource schemes are supported:
1333 * 1. 1 shared IRQ for error and all channels
1334 * 2. 2 IRQs: one for error and one shared for all channels
1335 * 3. 1 IRQ for error and an own IRQ for each channel
1337 if (irqs != 1 && irqs != 2 && irqs != num_channels + 1)
1343 for (i = 0; i <= num_channels; i++)
1344 nbpf->chan[i].irq = irqbuf[0];
1346 eirq = platform_get_irq_byname(pdev, "error");
1350 if (irqs == num_channels + 1) {
1351 struct nbpf_channel *chan;
1353 for (i = 0, chan = nbpf->chan; i <= num_channels;
1355 /* Skip the error IRQ */
1356 if (irqbuf[i] == eirq)
1358 chan->irq = irqbuf[i];
1361 if (chan != nbpf->chan + num_channels)
1364 /* 2 IRQs and more than one channel */
1365 if (irqbuf[0] == eirq)
1370 for (i = 0; i <= num_channels; i++)
1371 nbpf->chan[i].irq = irq;
1375 ret = devm_request_irq(dev, eirq, nbpf_err_irq,
1376 IRQF_SHARED, "dma error", nbpf);
1380 INIT_LIST_HEAD(&dma_dev->channels);
1382 /* Create DMA Channel */
1383 for (i = 0; i < num_channels; i++) {
1384 ret = nbpf_chan_probe(nbpf, i);
1389 dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
1390 dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
1391 dma_cap_set(DMA_PRIVATE, dma_dev->cap_mask);
1392 dma_cap_set(DMA_SG, dma_dev->cap_mask);
1394 /* Common and MEMCPY operations */
1395 dma_dev->device_alloc_chan_resources
1396 = nbpf_alloc_chan_resources;
1397 dma_dev->device_free_chan_resources = nbpf_free_chan_resources;
1398 dma_dev->device_prep_dma_sg = nbpf_prep_memcpy_sg;
1399 dma_dev->device_prep_dma_memcpy = nbpf_prep_memcpy;
1400 dma_dev->device_tx_status = nbpf_tx_status;
1401 dma_dev->device_issue_pending = nbpf_issue_pending;
1404 * If we drop support for unaligned MEMCPY buffer addresses and / or
1405 * lengths by setting
1406 * dma_dev->copy_align = 4;
1407 * then we can set transfer length to 4 bytes in nbpf_prep_one() for
1411 /* Compulsory for DMA_SLAVE fields */
1412 dma_dev->device_prep_slave_sg = nbpf_prep_slave_sg;
1413 dma_dev->device_config = nbpf_config;
1414 dma_dev->device_pause = nbpf_pause;
1415 dma_dev->device_terminate_all = nbpf_terminate_all;
1417 dma_dev->src_addr_widths = NBPF_DMA_BUSWIDTHS;
1418 dma_dev->dst_addr_widths = NBPF_DMA_BUSWIDTHS;
1419 dma_dev->directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1421 platform_set_drvdata(pdev, nbpf);
1423 ret = clk_prepare_enable(nbpf->clk);
1427 nbpf_configure(nbpf);
1429 ret = dma_async_device_register(dma_dev);
1433 ret = of_dma_controller_register(np, nbpf_of_xlate, nbpf);
1435 goto e_dma_dev_unreg;
1440 dma_async_device_unregister(dma_dev);
1442 clk_disable_unprepare(nbpf->clk);
1447 static int nbpf_remove(struct platform_device *pdev)
1449 struct nbpf_device *nbpf = platform_get_drvdata(pdev);
1451 of_dma_controller_free(pdev->dev.of_node);
1452 dma_async_device_unregister(&nbpf->dma_dev);
1453 clk_disable_unprepare(nbpf->clk);
1458 static const struct platform_device_id nbpf_ids[] = {
1459 {"nbpfaxi64dmac1b4", (kernel_ulong_t)&nbpf_cfg[NBPF1B4]},
1460 {"nbpfaxi64dmac1b8", (kernel_ulong_t)&nbpf_cfg[NBPF1B8]},
1461 {"nbpfaxi64dmac1b16", (kernel_ulong_t)&nbpf_cfg[NBPF1B16]},
1462 {"nbpfaxi64dmac4b4", (kernel_ulong_t)&nbpf_cfg[NBPF4B4]},
1463 {"nbpfaxi64dmac4b8", (kernel_ulong_t)&nbpf_cfg[NBPF4B8]},
1464 {"nbpfaxi64dmac4b16", (kernel_ulong_t)&nbpf_cfg[NBPF4B16]},
1465 {"nbpfaxi64dmac8b4", (kernel_ulong_t)&nbpf_cfg[NBPF8B4]},
1466 {"nbpfaxi64dmac8b8", (kernel_ulong_t)&nbpf_cfg[NBPF8B8]},
1467 {"nbpfaxi64dmac8b16", (kernel_ulong_t)&nbpf_cfg[NBPF8B16]},
1470 MODULE_DEVICE_TABLE(platform, nbpf_ids);
1473 static int nbpf_runtime_suspend(struct device *dev)
1475 struct nbpf_device *nbpf = platform_get_drvdata(to_platform_device(dev));
1476 clk_disable_unprepare(nbpf->clk);
1480 static int nbpf_runtime_resume(struct device *dev)
1482 struct nbpf_device *nbpf = platform_get_drvdata(to_platform_device(dev));
1483 return clk_prepare_enable(nbpf->clk);
1487 static const struct dev_pm_ops nbpf_pm_ops = {
1488 SET_RUNTIME_PM_OPS(nbpf_runtime_suspend, nbpf_runtime_resume, NULL)
1491 static struct platform_driver nbpf_driver = {
1494 .of_match_table = nbpf_match,
1497 .id_table = nbpf_ids,
1498 .probe = nbpf_probe,
1499 .remove = nbpf_remove,
1502 module_platform_driver(nbpf_driver);
1504 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1505 MODULE_DESCRIPTION("dmaengine driver for NBPFAXI64* DMACs");
1506 MODULE_LICENSE("GPL v2");