1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the Cirrus Logic EP93xx DMA Controller
5 * Copyright (C) 2011 Mika Westerberg
7 * DMA M2P implementation is based on the original
8 * arch/arm/mach-ep93xx/dma-m2p.c which has following copyrights:
10 * Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
11 * Copyright (C) 2006 Applied Data Systems
12 * Copyright (C) 2009 Ryan Mallon <rmallon@gmail.com>
14 * This driver is based on dw_dmac and amba-pl08x drivers.
17 #include <linux/clk.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/dmaengine.h>
21 #include <linux/module.h>
22 #include <linux/mod_devicetable.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
26 #include <linux/platform_data/dma-ep93xx.h>
28 #include "dmaengine.h"
31 #define M2P_CONTROL 0x0000
32 #define M2P_CONTROL_STALLINT BIT(0)
33 #define M2P_CONTROL_NFBINT BIT(1)
34 #define M2P_CONTROL_CH_ERROR_INT BIT(3)
35 #define M2P_CONTROL_ENABLE BIT(4)
36 #define M2P_CONTROL_ICE BIT(6)
38 #define M2P_INTERRUPT 0x0004
39 #define M2P_INTERRUPT_STALL BIT(0)
40 #define M2P_INTERRUPT_NFB BIT(1)
41 #define M2P_INTERRUPT_ERROR BIT(3)
43 #define M2P_PPALLOC 0x0008
44 #define M2P_STATUS 0x000c
46 #define M2P_MAXCNT0 0x0020
47 #define M2P_BASE0 0x0024
48 #define M2P_MAXCNT1 0x0030
49 #define M2P_BASE1 0x0034
51 #define M2P_STATE_IDLE 0
52 #define M2P_STATE_STALL 1
53 #define M2P_STATE_ON 2
54 #define M2P_STATE_NEXT 3
57 #define M2M_CONTROL 0x0000
58 #define M2M_CONTROL_DONEINT BIT(2)
59 #define M2M_CONTROL_ENABLE BIT(3)
60 #define M2M_CONTROL_START BIT(4)
61 #define M2M_CONTROL_DAH BIT(11)
62 #define M2M_CONTROL_SAH BIT(12)
63 #define M2M_CONTROL_PW_SHIFT 9
64 #define M2M_CONTROL_PW_8 (0 << M2M_CONTROL_PW_SHIFT)
65 #define M2M_CONTROL_PW_16 (1 << M2M_CONTROL_PW_SHIFT)
66 #define M2M_CONTROL_PW_32 (2 << M2M_CONTROL_PW_SHIFT)
67 #define M2M_CONTROL_PW_MASK (3 << M2M_CONTROL_PW_SHIFT)
68 #define M2M_CONTROL_TM_SHIFT 13
69 #define M2M_CONTROL_TM_TX (1 << M2M_CONTROL_TM_SHIFT)
70 #define M2M_CONTROL_TM_RX (2 << M2M_CONTROL_TM_SHIFT)
71 #define M2M_CONTROL_NFBINT BIT(21)
72 #define M2M_CONTROL_RSS_SHIFT 22
73 #define M2M_CONTROL_RSS_SSPRX (1 << M2M_CONTROL_RSS_SHIFT)
74 #define M2M_CONTROL_RSS_SSPTX (2 << M2M_CONTROL_RSS_SHIFT)
75 #define M2M_CONTROL_RSS_IDE (3 << M2M_CONTROL_RSS_SHIFT)
76 #define M2M_CONTROL_NO_HDSK BIT(24)
77 #define M2M_CONTROL_PWSC_SHIFT 25
79 #define M2M_INTERRUPT 0x0004
80 #define M2M_INTERRUPT_MASK 6
82 #define M2M_STATUS 0x000c
83 #define M2M_STATUS_CTL_SHIFT 1
84 #define M2M_STATUS_CTL_IDLE (0 << M2M_STATUS_CTL_SHIFT)
85 #define M2M_STATUS_CTL_STALL (1 << M2M_STATUS_CTL_SHIFT)
86 #define M2M_STATUS_CTL_MEMRD (2 << M2M_STATUS_CTL_SHIFT)
87 #define M2M_STATUS_CTL_MEMWR (3 << M2M_STATUS_CTL_SHIFT)
88 #define M2M_STATUS_CTL_BWCWAIT (4 << M2M_STATUS_CTL_SHIFT)
89 #define M2M_STATUS_CTL_MASK (7 << M2M_STATUS_CTL_SHIFT)
90 #define M2M_STATUS_BUF_SHIFT 4
91 #define M2M_STATUS_BUF_NO (0 << M2M_STATUS_BUF_SHIFT)
92 #define M2M_STATUS_BUF_ON (1 << M2M_STATUS_BUF_SHIFT)
93 #define M2M_STATUS_BUF_NEXT (2 << M2M_STATUS_BUF_SHIFT)
94 #define M2M_STATUS_BUF_MASK (3 << M2M_STATUS_BUF_SHIFT)
95 #define M2M_STATUS_DONE BIT(6)
97 #define M2M_BCR0 0x0010
98 #define M2M_BCR1 0x0014
99 #define M2M_SAR_BASE0 0x0018
100 #define M2M_SAR_BASE1 0x001c
101 #define M2M_DAR_BASE0 0x002c
102 #define M2M_DAR_BASE1 0x0030
104 #define DMA_MAX_CHAN_BYTES 0xffff
105 #define DMA_MAX_CHAN_DESCRIPTORS 32
107 struct ep93xx_dma_engine;
108 static int ep93xx_dma_slave_config_write(struct dma_chan *chan,
109 enum dma_transfer_direction dir,
110 struct dma_slave_config *config);
113 * struct ep93xx_dma_desc - EP93xx specific transaction descriptor
114 * @src_addr: source address of the transaction
115 * @dst_addr: destination address of the transaction
116 * @size: size of the transaction (in bytes)
117 * @complete: this descriptor is completed
118 * @txd: dmaengine API descriptor
119 * @tx_list: list of linked descriptors
120 * @node: link used for putting this into a channel queue
122 struct ep93xx_dma_desc {
127 struct dma_async_tx_descriptor txd;
128 struct list_head tx_list;
129 struct list_head node;
133 * struct ep93xx_dma_chan - an EP93xx DMA M2P/M2M channel
134 * @chan: dmaengine API channel
135 * @edma: pointer to the engine device
136 * @regs: memory mapped registers
137 * @irq: interrupt number of the channel
138 * @clk: clock used by this channel
139 * @tasklet: channel specific tasklet used for callbacks
140 * @lock: lock protecting the fields following
141 * @flags: flags for the channel
142 * @buffer: which buffer to use next (0/1)
143 * @active: flattened chain of descriptors currently being processed
144 * @queue: pending descriptors which are handled next
145 * @free_list: list of free descriptors which can be used
146 * @runtime_addr: physical address currently used as dest/src (M2M only). This
147 * is set via .device_config before slave operation is
149 * @runtime_ctrl: M2M runtime values for the control register.
150 * @slave_config: slave configuration
152 * As EP93xx DMA controller doesn't support real chained DMA descriptors we
153 * will have slightly different scheme here: @active points to a head of
154 * flattened DMA descriptor chain.
156 * @queue holds pending transactions. These are linked through the first
157 * descriptor in the chain. When a descriptor is moved to the @active queue,
158 * the first and chained descriptors are flattened into a single list.
160 * @chan.private holds pointer to &struct ep93xx_dma_data which contains
161 * necessary channel configuration information. For memcpy channels this must
164 struct ep93xx_dma_chan {
165 struct dma_chan chan;
166 const struct ep93xx_dma_engine *edma;
170 struct tasklet_struct tasklet;
171 /* protects the fields following */
174 /* Channel is configured for cyclic transfers */
175 #define EP93XX_DMA_IS_CYCLIC 0
178 struct list_head active;
179 struct list_head queue;
180 struct list_head free_list;
183 struct dma_slave_config slave_config;
187 * struct ep93xx_dma_engine - the EP93xx DMA engine instance
188 * @dma_dev: holds the dmaengine device
189 * @m2m: is this an M2M or M2P device
190 * @hw_setup: method which sets the channel up for operation
191 * @hw_synchronize: synchronizes DMA channel termination to current context
192 * @hw_shutdown: shuts the channel down and flushes whatever is left
193 * @hw_submit: pushes active descriptor(s) to the hardware
194 * @hw_interrupt: handle the interrupt
195 * @num_channels: number of channels for this instance
196 * @channels: array of channels
198 * There is one instance of this struct for the M2P channels and one for the
199 * M2M channels. hw_xxx() methods are used to perform operations which are
200 * different on M2M and M2P channels. These methods are called with channel
201 * lock held and interrupts disabled so they cannot sleep.
203 struct ep93xx_dma_engine {
204 struct dma_device dma_dev;
206 int (*hw_setup)(struct ep93xx_dma_chan *);
207 void (*hw_synchronize)(struct ep93xx_dma_chan *);
208 void (*hw_shutdown)(struct ep93xx_dma_chan *);
209 void (*hw_submit)(struct ep93xx_dma_chan *);
210 int (*hw_interrupt)(struct ep93xx_dma_chan *);
211 #define INTERRUPT_UNKNOWN 0
212 #define INTERRUPT_DONE 1
213 #define INTERRUPT_NEXT_BUFFER 2
216 struct ep93xx_dma_chan channels[];
219 static inline struct device *chan2dev(struct ep93xx_dma_chan *edmac)
221 return &edmac->chan.dev->device;
224 static struct ep93xx_dma_chan *to_ep93xx_dma_chan(struct dma_chan *chan)
226 return container_of(chan, struct ep93xx_dma_chan, chan);
230 * ep93xx_dma_set_active - set new active descriptor chain
232 * @desc: head of the new active descriptor chain
234 * Sets @desc to be the head of the new active descriptor chain. This is the
235 * chain which is processed next. The active list must be empty before calling
238 * Called with @edmac->lock held and interrupts disabled.
240 static void ep93xx_dma_set_active(struct ep93xx_dma_chan *edmac,
241 struct ep93xx_dma_desc *desc)
243 BUG_ON(!list_empty(&edmac->active));
245 list_add_tail(&desc->node, &edmac->active);
247 /* Flatten the @desc->tx_list chain into @edmac->active list */
248 while (!list_empty(&desc->tx_list)) {
249 struct ep93xx_dma_desc *d = list_first_entry(&desc->tx_list,
250 struct ep93xx_dma_desc, node);
253 * We copy the callback parameters from the first descriptor
254 * to all the chained descriptors. This way we can call the
255 * callback without having to find out the first descriptor in
256 * the chain. Useful for cyclic transfers.
258 d->txd.callback = desc->txd.callback;
259 d->txd.callback_param = desc->txd.callback_param;
261 list_move_tail(&d->node, &edmac->active);
265 /* Called with @edmac->lock held and interrupts disabled */
266 static struct ep93xx_dma_desc *
267 ep93xx_dma_get_active(struct ep93xx_dma_chan *edmac)
269 return list_first_entry_or_null(&edmac->active,
270 struct ep93xx_dma_desc, node);
274 * ep93xx_dma_advance_active - advances to the next active descriptor
277 * Function advances active descriptor to the next in the @edmac->active and
278 * returns %true if we still have descriptors in the chain to process.
279 * Otherwise returns %false.
281 * When the channel is in cyclic mode always returns %true.
283 * Called with @edmac->lock held and interrupts disabled.
285 static bool ep93xx_dma_advance_active(struct ep93xx_dma_chan *edmac)
287 struct ep93xx_dma_desc *desc;
289 list_rotate_left(&edmac->active);
291 if (test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags))
294 desc = ep93xx_dma_get_active(edmac);
299 * If txd.cookie is set it means that we are back in the first
300 * descriptor in the chain and hence done with it.
302 return !desc->txd.cookie;
306 * M2P DMA implementation
309 static void m2p_set_control(struct ep93xx_dma_chan *edmac, u32 control)
311 writel(control, edmac->regs + M2P_CONTROL);
313 * EP93xx User's Guide states that we must perform a dummy read after
314 * write to the control register.
316 readl(edmac->regs + M2P_CONTROL);
319 static int m2p_hw_setup(struct ep93xx_dma_chan *edmac)
321 struct ep93xx_dma_data *data = edmac->chan.private;
324 writel(data->port & 0xf, edmac->regs + M2P_PPALLOC);
326 control = M2P_CONTROL_CH_ERROR_INT | M2P_CONTROL_ICE
327 | M2P_CONTROL_ENABLE;
328 m2p_set_control(edmac, control);
335 static inline u32 m2p_channel_state(struct ep93xx_dma_chan *edmac)
337 return (readl(edmac->regs + M2P_STATUS) >> 4) & 0x3;
340 static void m2p_hw_synchronize(struct ep93xx_dma_chan *edmac)
345 spin_lock_irqsave(&edmac->lock, flags);
346 control = readl(edmac->regs + M2P_CONTROL);
347 control &= ~(M2P_CONTROL_STALLINT | M2P_CONTROL_NFBINT);
348 m2p_set_control(edmac, control);
349 spin_unlock_irqrestore(&edmac->lock, flags);
351 while (m2p_channel_state(edmac) >= M2P_STATE_ON)
355 static void m2p_hw_shutdown(struct ep93xx_dma_chan *edmac)
357 m2p_set_control(edmac, 0);
359 while (m2p_channel_state(edmac) != M2P_STATE_IDLE)
360 dev_warn(chan2dev(edmac), "M2P: Not yet IDLE\n");
363 static void m2p_fill_desc(struct ep93xx_dma_chan *edmac)
365 struct ep93xx_dma_desc *desc;
368 desc = ep93xx_dma_get_active(edmac);
370 dev_warn(chan2dev(edmac), "M2P: empty descriptor list\n");
374 if (ep93xx_dma_chan_direction(&edmac->chan) == DMA_MEM_TO_DEV)
375 bus_addr = desc->src_addr;
377 bus_addr = desc->dst_addr;
379 if (edmac->buffer == 0) {
380 writel(desc->size, edmac->regs + M2P_MAXCNT0);
381 writel(bus_addr, edmac->regs + M2P_BASE0);
383 writel(desc->size, edmac->regs + M2P_MAXCNT1);
384 writel(bus_addr, edmac->regs + M2P_BASE1);
390 static void m2p_hw_submit(struct ep93xx_dma_chan *edmac)
392 u32 control = readl(edmac->regs + M2P_CONTROL);
394 m2p_fill_desc(edmac);
395 control |= M2P_CONTROL_STALLINT;
397 if (ep93xx_dma_advance_active(edmac)) {
398 m2p_fill_desc(edmac);
399 control |= M2P_CONTROL_NFBINT;
402 m2p_set_control(edmac, control);
405 static int m2p_hw_interrupt(struct ep93xx_dma_chan *edmac)
407 u32 irq_status = readl(edmac->regs + M2P_INTERRUPT);
410 if (irq_status & M2P_INTERRUPT_ERROR) {
411 struct ep93xx_dma_desc *desc = ep93xx_dma_get_active(edmac);
413 /* Clear the error interrupt */
414 writel(1, edmac->regs + M2P_INTERRUPT);
417 * It seems that there is no easy way of reporting errors back
418 * to client so we just report the error here and continue as
421 * Revisit this when there is a mechanism to report back the
424 dev_err(chan2dev(edmac),
425 "DMA transfer failed! Details:\n"
427 "\tsrc_addr : 0x%08x\n"
428 "\tdst_addr : 0x%08x\n"
430 desc->txd.cookie, desc->src_addr, desc->dst_addr,
435 * Even latest E2 silicon revision sometimes assert STALL interrupt
436 * instead of NFB. Therefore we treat them equally, basing on the
437 * amount of data we still have to transfer.
439 if (!(irq_status & (M2P_INTERRUPT_STALL | M2P_INTERRUPT_NFB)))
440 return INTERRUPT_UNKNOWN;
442 if (ep93xx_dma_advance_active(edmac)) {
443 m2p_fill_desc(edmac);
444 return INTERRUPT_NEXT_BUFFER;
447 /* Disable interrupts */
448 control = readl(edmac->regs + M2P_CONTROL);
449 control &= ~(M2P_CONTROL_STALLINT | M2P_CONTROL_NFBINT);
450 m2p_set_control(edmac, control);
452 return INTERRUPT_DONE;
456 * M2M DMA implementation
459 static int m2m_hw_setup(struct ep93xx_dma_chan *edmac)
461 const struct ep93xx_dma_data *data = edmac->chan.private;
465 /* This is memcpy channel, nothing to configure */
466 writel(control, edmac->regs + M2M_CONTROL);
470 switch (data->port) {
473 * This was found via experimenting - anything less than 5
474 * causes the channel to perform only a partial transfer which
475 * leads to problems since we don't get DONE interrupt then.
477 control = (5 << M2M_CONTROL_PWSC_SHIFT);
478 control |= M2M_CONTROL_NO_HDSK;
480 if (data->direction == DMA_MEM_TO_DEV) {
481 control |= M2M_CONTROL_DAH;
482 control |= M2M_CONTROL_TM_TX;
483 control |= M2M_CONTROL_RSS_SSPTX;
485 control |= M2M_CONTROL_SAH;
486 control |= M2M_CONTROL_TM_RX;
487 control |= M2M_CONTROL_RSS_SSPRX;
493 * This IDE part is totally untested. Values below are taken
494 * from the EP93xx Users's Guide and might not be correct.
496 if (data->direction == DMA_MEM_TO_DEV) {
497 /* Worst case from the UG */
498 control = (3 << M2M_CONTROL_PWSC_SHIFT);
499 control |= M2M_CONTROL_DAH;
500 control |= M2M_CONTROL_TM_TX;
502 control = (2 << M2M_CONTROL_PWSC_SHIFT);
503 control |= M2M_CONTROL_SAH;
504 control |= M2M_CONTROL_TM_RX;
507 control |= M2M_CONTROL_NO_HDSK;
508 control |= M2M_CONTROL_RSS_IDE;
509 control |= M2M_CONTROL_PW_16;
516 writel(control, edmac->regs + M2M_CONTROL);
520 static void m2m_hw_shutdown(struct ep93xx_dma_chan *edmac)
522 /* Just disable the channel */
523 writel(0, edmac->regs + M2M_CONTROL);
526 static void m2m_fill_desc(struct ep93xx_dma_chan *edmac)
528 struct ep93xx_dma_desc *desc;
530 desc = ep93xx_dma_get_active(edmac);
532 dev_warn(chan2dev(edmac), "M2M: empty descriptor list\n");
536 if (edmac->buffer == 0) {
537 writel(desc->src_addr, edmac->regs + M2M_SAR_BASE0);
538 writel(desc->dst_addr, edmac->regs + M2M_DAR_BASE0);
539 writel(desc->size, edmac->regs + M2M_BCR0);
541 writel(desc->src_addr, edmac->regs + M2M_SAR_BASE1);
542 writel(desc->dst_addr, edmac->regs + M2M_DAR_BASE1);
543 writel(desc->size, edmac->regs + M2M_BCR1);
549 static void m2m_hw_submit(struct ep93xx_dma_chan *edmac)
551 struct ep93xx_dma_data *data = edmac->chan.private;
552 u32 control = readl(edmac->regs + M2M_CONTROL);
555 * Since we allow clients to configure PW (peripheral width) we always
556 * clear PW bits here and then set them according what is given in
557 * the runtime configuration.
559 control &= ~M2M_CONTROL_PW_MASK;
560 control |= edmac->runtime_ctrl;
562 m2m_fill_desc(edmac);
563 control |= M2M_CONTROL_DONEINT;
565 if (ep93xx_dma_advance_active(edmac)) {
566 m2m_fill_desc(edmac);
567 control |= M2M_CONTROL_NFBINT;
571 * Now we can finally enable the channel. For M2M channel this must be
572 * done _after_ the BCRx registers are programmed.
574 control |= M2M_CONTROL_ENABLE;
575 writel(control, edmac->regs + M2M_CONTROL);
579 * For memcpy channels the software trigger must be asserted
580 * in order to start the memcpy operation.
582 control |= M2M_CONTROL_START;
583 writel(control, edmac->regs + M2M_CONTROL);
588 * According to EP93xx User's Guide, we should receive DONE interrupt when all
589 * M2M DMA controller transactions complete normally. This is not always the
590 * case - sometimes EP93xx M2M DMA asserts DONE interrupt when the DMA channel
591 * is still running (channel Buffer FSM in DMA_BUF_ON state, and channel
592 * Control FSM in DMA_MEM_RD state, observed at least in IDE-DMA operation).
593 * In effect, disabling the channel when only DONE bit is set could stop
594 * currently running DMA transfer. To avoid this, we use Buffer FSM and
595 * Control FSM to check current state of DMA channel.
597 static int m2m_hw_interrupt(struct ep93xx_dma_chan *edmac)
599 u32 status = readl(edmac->regs + M2M_STATUS);
600 u32 ctl_fsm = status & M2M_STATUS_CTL_MASK;
601 u32 buf_fsm = status & M2M_STATUS_BUF_MASK;
602 bool done = status & M2M_STATUS_DONE;
605 struct ep93xx_dma_desc *desc;
607 /* Accept only DONE and NFB interrupts */
608 if (!(readl(edmac->regs + M2M_INTERRUPT) & M2M_INTERRUPT_MASK))
609 return INTERRUPT_UNKNOWN;
612 /* Clear the DONE bit */
613 writel(0, edmac->regs + M2M_INTERRUPT);
617 * Check whether we are done with descriptors or not. This, together
618 * with DMA channel state, determines action to take in interrupt.
620 desc = ep93xx_dma_get_active(edmac);
621 last_done = !desc || desc->txd.cookie;
624 * Use M2M DMA Buffer FSM and Control FSM to check current state of
625 * DMA channel. Using DONE and NFB bits from channel status register
626 * or bits from channel interrupt register is not reliable.
629 (buf_fsm == M2M_STATUS_BUF_NO ||
630 buf_fsm == M2M_STATUS_BUF_ON)) {
632 * Two buffers are ready for update when Buffer FSM is in
633 * DMA_NO_BUF state. Only one buffer can be prepared without
634 * disabling the channel or polling the DONE bit.
635 * To simplify things, always prepare only one buffer.
637 if (ep93xx_dma_advance_active(edmac)) {
638 m2m_fill_desc(edmac);
639 if (done && !edmac->chan.private) {
640 /* Software trigger for memcpy channel */
641 control = readl(edmac->regs + M2M_CONTROL);
642 control |= M2M_CONTROL_START;
643 writel(control, edmac->regs + M2M_CONTROL);
645 return INTERRUPT_NEXT_BUFFER;
652 * Disable the channel only when Buffer FSM is in DMA_NO_BUF state
653 * and Control FSM is in DMA_STALL state.
656 buf_fsm == M2M_STATUS_BUF_NO &&
657 ctl_fsm == M2M_STATUS_CTL_STALL) {
658 /* Disable interrupts and the channel */
659 control = readl(edmac->regs + M2M_CONTROL);
660 control &= ~(M2M_CONTROL_DONEINT | M2M_CONTROL_NFBINT
661 | M2M_CONTROL_ENABLE);
662 writel(control, edmac->regs + M2M_CONTROL);
663 return INTERRUPT_DONE;
667 * Nothing to do this time.
669 return INTERRUPT_NEXT_BUFFER;
673 * DMA engine API implementation
676 static struct ep93xx_dma_desc *
677 ep93xx_dma_desc_get(struct ep93xx_dma_chan *edmac)
679 struct ep93xx_dma_desc *desc, *_desc;
680 struct ep93xx_dma_desc *ret = NULL;
683 spin_lock_irqsave(&edmac->lock, flags);
684 list_for_each_entry_safe(desc, _desc, &edmac->free_list, node) {
685 if (async_tx_test_ack(&desc->txd)) {
686 list_del_init(&desc->node);
688 /* Re-initialize the descriptor */
692 desc->complete = false;
693 desc->txd.cookie = 0;
694 desc->txd.callback = NULL;
695 desc->txd.callback_param = NULL;
701 spin_unlock_irqrestore(&edmac->lock, flags);
705 static void ep93xx_dma_desc_put(struct ep93xx_dma_chan *edmac,
706 struct ep93xx_dma_desc *desc)
711 spin_lock_irqsave(&edmac->lock, flags);
712 list_splice_init(&desc->tx_list, &edmac->free_list);
713 list_add(&desc->node, &edmac->free_list);
714 spin_unlock_irqrestore(&edmac->lock, flags);
719 * ep93xx_dma_advance_work - start processing the next pending transaction
722 * If we have pending transactions queued and we are currently idling, this
723 * function takes the next queued transaction from the @edmac->queue and
724 * pushes it to the hardware for execution.
726 static void ep93xx_dma_advance_work(struct ep93xx_dma_chan *edmac)
728 struct ep93xx_dma_desc *new;
731 spin_lock_irqsave(&edmac->lock, flags);
732 if (!list_empty(&edmac->active) || list_empty(&edmac->queue)) {
733 spin_unlock_irqrestore(&edmac->lock, flags);
737 /* Take the next descriptor from the pending queue */
738 new = list_first_entry(&edmac->queue, struct ep93xx_dma_desc, node);
739 list_del_init(&new->node);
741 ep93xx_dma_set_active(edmac, new);
743 /* Push it to the hardware */
744 edmac->edma->hw_submit(edmac);
745 spin_unlock_irqrestore(&edmac->lock, flags);
748 static void ep93xx_dma_tasklet(struct tasklet_struct *t)
750 struct ep93xx_dma_chan *edmac = from_tasklet(edmac, t, tasklet);
751 struct ep93xx_dma_desc *desc, *d;
752 struct dmaengine_desc_callback cb;
755 memset(&cb, 0, sizeof(cb));
756 spin_lock_irq(&edmac->lock);
758 * If dma_terminate_all() was called before we get to run, the active
759 * list has become empty. If that happens we aren't supposed to do
760 * anything more than call ep93xx_dma_advance_work().
762 desc = ep93xx_dma_get_active(edmac);
764 if (desc->complete) {
765 /* mark descriptor complete for non cyclic case only */
766 if (!test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags))
767 dma_cookie_complete(&desc->txd);
768 list_splice_init(&edmac->active, &list);
770 dmaengine_desc_get_callback(&desc->txd, &cb);
772 spin_unlock_irq(&edmac->lock);
774 /* Pick up the next descriptor from the queue */
775 ep93xx_dma_advance_work(edmac);
777 /* Now we can release all the chained descriptors */
778 list_for_each_entry_safe(desc, d, &list, node) {
779 dma_descriptor_unmap(&desc->txd);
780 ep93xx_dma_desc_put(edmac, desc);
783 dmaengine_desc_callback_invoke(&cb, NULL);
786 static irqreturn_t ep93xx_dma_interrupt(int irq, void *dev_id)
788 struct ep93xx_dma_chan *edmac = dev_id;
789 struct ep93xx_dma_desc *desc;
790 irqreturn_t ret = IRQ_HANDLED;
792 spin_lock(&edmac->lock);
794 desc = ep93xx_dma_get_active(edmac);
796 dev_warn(chan2dev(edmac),
797 "got interrupt while active list is empty\n");
798 spin_unlock(&edmac->lock);
802 switch (edmac->edma->hw_interrupt(edmac)) {
804 desc->complete = true;
805 tasklet_schedule(&edmac->tasklet);
808 case INTERRUPT_NEXT_BUFFER:
809 if (test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags))
810 tasklet_schedule(&edmac->tasklet);
814 dev_warn(chan2dev(edmac), "unknown interrupt!\n");
819 spin_unlock(&edmac->lock);
824 * ep93xx_dma_tx_submit - set the prepared descriptor(s) to be executed
825 * @tx: descriptor to be executed
827 * Function will execute given descriptor on the hardware or if the hardware
828 * is busy, queue the descriptor to be executed later on. Returns cookie which
829 * can be used to poll the status of the descriptor.
831 static dma_cookie_t ep93xx_dma_tx_submit(struct dma_async_tx_descriptor *tx)
833 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(tx->chan);
834 struct ep93xx_dma_desc *desc;
838 spin_lock_irqsave(&edmac->lock, flags);
839 cookie = dma_cookie_assign(tx);
841 desc = container_of(tx, struct ep93xx_dma_desc, txd);
844 * If nothing is currently prosessed, we push this descriptor
845 * directly to the hardware. Otherwise we put the descriptor
846 * to the pending queue.
848 if (list_empty(&edmac->active)) {
849 ep93xx_dma_set_active(edmac, desc);
850 edmac->edma->hw_submit(edmac);
852 list_add_tail(&desc->node, &edmac->queue);
855 spin_unlock_irqrestore(&edmac->lock, flags);
860 * ep93xx_dma_alloc_chan_resources - allocate resources for the channel
861 * @chan: channel to allocate resources
863 * Function allocates necessary resources for the given DMA channel and
864 * returns number of allocated descriptors for the channel. Negative errno
865 * is returned in case of failure.
867 static int ep93xx_dma_alloc_chan_resources(struct dma_chan *chan)
869 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
870 struct ep93xx_dma_data *data = chan->private;
871 const char *name = dma_chan_name(chan);
874 /* Sanity check the channel parameters */
875 if (!edmac->edma->m2m) {
878 if (data->port < EP93XX_DMA_I2S1 ||
879 data->port > EP93XX_DMA_IRDA)
881 if (data->direction != ep93xx_dma_chan_direction(chan))
885 switch (data->port) {
888 if (!is_slave_direction(data->direction))
897 if (data && data->name)
900 ret = clk_prepare_enable(edmac->clk);
904 ret = request_irq(edmac->irq, ep93xx_dma_interrupt, 0, name, edmac);
906 goto fail_clk_disable;
908 spin_lock_irq(&edmac->lock);
909 dma_cookie_init(&edmac->chan);
910 ret = edmac->edma->hw_setup(edmac);
911 spin_unlock_irq(&edmac->lock);
916 for (i = 0; i < DMA_MAX_CHAN_DESCRIPTORS; i++) {
917 struct ep93xx_dma_desc *desc;
919 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
921 dev_warn(chan2dev(edmac), "not enough descriptors\n");
925 INIT_LIST_HEAD(&desc->tx_list);
927 dma_async_tx_descriptor_init(&desc->txd, chan);
928 desc->txd.flags = DMA_CTRL_ACK;
929 desc->txd.tx_submit = ep93xx_dma_tx_submit;
931 ep93xx_dma_desc_put(edmac, desc);
937 free_irq(edmac->irq, edmac);
939 clk_disable_unprepare(edmac->clk);
945 * ep93xx_dma_free_chan_resources - release resources for the channel
948 * Function releases all the resources allocated for the given channel.
949 * The channel must be idle when this is called.
951 static void ep93xx_dma_free_chan_resources(struct dma_chan *chan)
953 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
954 struct ep93xx_dma_desc *desc, *d;
958 BUG_ON(!list_empty(&edmac->active));
959 BUG_ON(!list_empty(&edmac->queue));
961 spin_lock_irqsave(&edmac->lock, flags);
962 edmac->edma->hw_shutdown(edmac);
963 edmac->runtime_addr = 0;
964 edmac->runtime_ctrl = 0;
966 list_splice_init(&edmac->free_list, &list);
967 spin_unlock_irqrestore(&edmac->lock, flags);
969 list_for_each_entry_safe(desc, d, &list, node)
972 clk_disable_unprepare(edmac->clk);
973 free_irq(edmac->irq, edmac);
977 * ep93xx_dma_prep_dma_memcpy - prepare a memcpy DMA operation
979 * @dest: destination bus address
980 * @src: source bus address
981 * @len: size of the transaction
982 * @flags: flags for the descriptor
984 * Returns a valid DMA descriptor or %NULL in case of failure.
986 static struct dma_async_tx_descriptor *
987 ep93xx_dma_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest,
988 dma_addr_t src, size_t len, unsigned long flags)
990 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
991 struct ep93xx_dma_desc *desc, *first;
992 size_t bytes, offset;
995 for (offset = 0; offset < len; offset += bytes) {
996 desc = ep93xx_dma_desc_get(edmac);
998 dev_warn(chan2dev(edmac), "couldn't get descriptor\n");
1002 bytes = min_t(size_t, len - offset, DMA_MAX_CHAN_BYTES);
1004 desc->src_addr = src + offset;
1005 desc->dst_addr = dest + offset;
1011 list_add_tail(&desc->node, &first->tx_list);
1014 first->txd.cookie = -EBUSY;
1015 first->txd.flags = flags;
1019 ep93xx_dma_desc_put(edmac, first);
1024 * ep93xx_dma_prep_slave_sg - prepare a slave DMA operation
1026 * @sgl: list of buffers to transfer
1027 * @sg_len: number of entries in @sgl
1028 * @dir: direction of tha DMA transfer
1029 * @flags: flags for the descriptor
1030 * @context: operation context (ignored)
1032 * Returns a valid DMA descriptor or %NULL in case of failure.
1034 static struct dma_async_tx_descriptor *
1035 ep93xx_dma_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl,
1036 unsigned int sg_len, enum dma_transfer_direction dir,
1037 unsigned long flags, void *context)
1039 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
1040 struct ep93xx_dma_desc *desc, *first;
1041 struct scatterlist *sg;
1044 if (!edmac->edma->m2m && dir != ep93xx_dma_chan_direction(chan)) {
1045 dev_warn(chan2dev(edmac),
1046 "channel was configured with different direction\n");
1050 if (test_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags)) {
1051 dev_warn(chan2dev(edmac),
1052 "channel is already used for cyclic transfers\n");
1056 ep93xx_dma_slave_config_write(chan, dir, &edmac->slave_config);
1059 for_each_sg(sgl, sg, sg_len, i) {
1060 size_t len = sg_dma_len(sg);
1062 if (len > DMA_MAX_CHAN_BYTES) {
1063 dev_warn(chan2dev(edmac), "too big transfer size %zu\n",
1068 desc = ep93xx_dma_desc_get(edmac);
1070 dev_warn(chan2dev(edmac), "couldn't get descriptor\n");
1074 if (dir == DMA_MEM_TO_DEV) {
1075 desc->src_addr = sg_dma_address(sg);
1076 desc->dst_addr = edmac->runtime_addr;
1078 desc->src_addr = edmac->runtime_addr;
1079 desc->dst_addr = sg_dma_address(sg);
1086 list_add_tail(&desc->node, &first->tx_list);
1089 first->txd.cookie = -EBUSY;
1090 first->txd.flags = flags;
1095 ep93xx_dma_desc_put(edmac, first);
1100 * ep93xx_dma_prep_dma_cyclic - prepare a cyclic DMA operation
1102 * @dma_addr: DMA mapped address of the buffer
1103 * @buf_len: length of the buffer (in bytes)
1104 * @period_len: length of a single period
1105 * @dir: direction of the operation
1106 * @flags: tx descriptor status flags
1108 * Prepares a descriptor for cyclic DMA operation. This means that once the
1109 * descriptor is submitted, we will be submitting in a @period_len sized
1110 * buffers and calling callback once the period has been elapsed. Transfer
1111 * terminates only when client calls dmaengine_terminate_all() for this
1114 * Returns a valid DMA descriptor or %NULL in case of failure.
1116 static struct dma_async_tx_descriptor *
1117 ep93xx_dma_prep_dma_cyclic(struct dma_chan *chan, dma_addr_t dma_addr,
1118 size_t buf_len, size_t period_len,
1119 enum dma_transfer_direction dir, unsigned long flags)
1121 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
1122 struct ep93xx_dma_desc *desc, *first;
1125 if (!edmac->edma->m2m && dir != ep93xx_dma_chan_direction(chan)) {
1126 dev_warn(chan2dev(edmac),
1127 "channel was configured with different direction\n");
1131 if (test_and_set_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags)) {
1132 dev_warn(chan2dev(edmac),
1133 "channel is already used for cyclic transfers\n");
1137 if (period_len > DMA_MAX_CHAN_BYTES) {
1138 dev_warn(chan2dev(edmac), "too big period length %zu\n",
1143 ep93xx_dma_slave_config_write(chan, dir, &edmac->slave_config);
1145 /* Split the buffer into period size chunks */
1147 for (offset = 0; offset < buf_len; offset += period_len) {
1148 desc = ep93xx_dma_desc_get(edmac);
1150 dev_warn(chan2dev(edmac), "couldn't get descriptor\n");
1154 if (dir == DMA_MEM_TO_DEV) {
1155 desc->src_addr = dma_addr + offset;
1156 desc->dst_addr = edmac->runtime_addr;
1158 desc->src_addr = edmac->runtime_addr;
1159 desc->dst_addr = dma_addr + offset;
1162 desc->size = period_len;
1167 list_add_tail(&desc->node, &first->tx_list);
1170 first->txd.cookie = -EBUSY;
1175 ep93xx_dma_desc_put(edmac, first);
1180 * ep93xx_dma_synchronize - Synchronizes the termination of transfers to the
1184 * Synchronizes the DMA channel termination to the current context. When this
1185 * function returns it is guaranteed that all transfers for previously issued
1186 * descriptors have stopped and it is safe to free the memory associated
1187 * with them. Furthermore it is guaranteed that all complete callback functions
1188 * for a previously submitted descriptor have finished running and it is safe to
1189 * free resources accessed from within the complete callbacks.
1191 static void ep93xx_dma_synchronize(struct dma_chan *chan)
1193 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
1195 if (edmac->edma->hw_synchronize)
1196 edmac->edma->hw_synchronize(edmac);
1200 * ep93xx_dma_terminate_all - terminate all transactions
1203 * Stops all DMA transactions. All descriptors are put back to the
1204 * @edmac->free_list and callbacks are _not_ called.
1206 static int ep93xx_dma_terminate_all(struct dma_chan *chan)
1208 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
1209 struct ep93xx_dma_desc *desc, *_d;
1210 unsigned long flags;
1213 spin_lock_irqsave(&edmac->lock, flags);
1214 /* First we disable and flush the DMA channel */
1215 edmac->edma->hw_shutdown(edmac);
1216 clear_bit(EP93XX_DMA_IS_CYCLIC, &edmac->flags);
1217 list_splice_init(&edmac->active, &list);
1218 list_splice_init(&edmac->queue, &list);
1220 * We then re-enable the channel. This way we can continue submitting
1221 * the descriptors by just calling ->hw_submit() again.
1223 edmac->edma->hw_setup(edmac);
1224 spin_unlock_irqrestore(&edmac->lock, flags);
1226 list_for_each_entry_safe(desc, _d, &list, node)
1227 ep93xx_dma_desc_put(edmac, desc);
1232 static int ep93xx_dma_slave_config(struct dma_chan *chan,
1233 struct dma_slave_config *config)
1235 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
1237 memcpy(&edmac->slave_config, config, sizeof(*config));
1242 static int ep93xx_dma_slave_config_write(struct dma_chan *chan,
1243 enum dma_transfer_direction dir,
1244 struct dma_slave_config *config)
1246 struct ep93xx_dma_chan *edmac = to_ep93xx_dma_chan(chan);
1247 enum dma_slave_buswidth width;
1248 unsigned long flags;
1251 if (!edmac->edma->m2m)
1255 case DMA_DEV_TO_MEM:
1256 width = config->src_addr_width;
1257 addr = config->src_addr;
1260 case DMA_MEM_TO_DEV:
1261 width = config->dst_addr_width;
1262 addr = config->dst_addr;
1270 case DMA_SLAVE_BUSWIDTH_1_BYTE:
1273 case DMA_SLAVE_BUSWIDTH_2_BYTES:
1274 ctrl = M2M_CONTROL_PW_16;
1276 case DMA_SLAVE_BUSWIDTH_4_BYTES:
1277 ctrl = M2M_CONTROL_PW_32;
1283 spin_lock_irqsave(&edmac->lock, flags);
1284 edmac->runtime_addr = addr;
1285 edmac->runtime_ctrl = ctrl;
1286 spin_unlock_irqrestore(&edmac->lock, flags);
1292 * ep93xx_dma_tx_status - check if a transaction is completed
1294 * @cookie: transaction specific cookie
1295 * @state: state of the transaction is stored here if given
1297 * This function can be used to query state of a given transaction.
1299 static enum dma_status ep93xx_dma_tx_status(struct dma_chan *chan,
1300 dma_cookie_t cookie,
1301 struct dma_tx_state *state)
1303 return dma_cookie_status(chan, cookie, state);
1307 * ep93xx_dma_issue_pending - push pending transactions to the hardware
1310 * When this function is called, all pending transactions are pushed to the
1311 * hardware and executed.
1313 static void ep93xx_dma_issue_pending(struct dma_chan *chan)
1315 ep93xx_dma_advance_work(to_ep93xx_dma_chan(chan));
1318 static int __init ep93xx_dma_probe(struct platform_device *pdev)
1320 struct ep93xx_dma_platform_data *pdata = dev_get_platdata(&pdev->dev);
1321 struct ep93xx_dma_engine *edma;
1322 struct dma_device *dma_dev;
1326 edma_size = pdata->num_channels * sizeof(struct ep93xx_dma_chan);
1327 edma = kzalloc(sizeof(*edma) + edma_size, GFP_KERNEL);
1331 dma_dev = &edma->dma_dev;
1332 edma->m2m = platform_get_device_id(pdev)->driver_data;
1333 edma->num_channels = pdata->num_channels;
1335 INIT_LIST_HEAD(&dma_dev->channels);
1336 for (i = 0; i < pdata->num_channels; i++) {
1337 const struct ep93xx_dma_chan_data *cdata = &pdata->channels[i];
1338 struct ep93xx_dma_chan *edmac = &edma->channels[i];
1340 edmac->chan.device = dma_dev;
1341 edmac->regs = cdata->base;
1342 edmac->irq = cdata->irq;
1345 edmac->clk = clk_get(NULL, cdata->name);
1346 if (IS_ERR(edmac->clk)) {
1347 dev_warn(&pdev->dev, "failed to get clock for %s\n",
1352 spin_lock_init(&edmac->lock);
1353 INIT_LIST_HEAD(&edmac->active);
1354 INIT_LIST_HEAD(&edmac->queue);
1355 INIT_LIST_HEAD(&edmac->free_list);
1356 tasklet_setup(&edmac->tasklet, ep93xx_dma_tasklet);
1358 list_add_tail(&edmac->chan.device_node,
1359 &dma_dev->channels);
1362 dma_cap_zero(dma_dev->cap_mask);
1363 dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
1364 dma_cap_set(DMA_CYCLIC, dma_dev->cap_mask);
1366 dma_dev->dev = &pdev->dev;
1367 dma_dev->device_alloc_chan_resources = ep93xx_dma_alloc_chan_resources;
1368 dma_dev->device_free_chan_resources = ep93xx_dma_free_chan_resources;
1369 dma_dev->device_prep_slave_sg = ep93xx_dma_prep_slave_sg;
1370 dma_dev->device_prep_dma_cyclic = ep93xx_dma_prep_dma_cyclic;
1371 dma_dev->device_config = ep93xx_dma_slave_config;
1372 dma_dev->device_synchronize = ep93xx_dma_synchronize;
1373 dma_dev->device_terminate_all = ep93xx_dma_terminate_all;
1374 dma_dev->device_issue_pending = ep93xx_dma_issue_pending;
1375 dma_dev->device_tx_status = ep93xx_dma_tx_status;
1377 dma_set_max_seg_size(dma_dev->dev, DMA_MAX_CHAN_BYTES);
1380 dma_cap_set(DMA_MEMCPY, dma_dev->cap_mask);
1381 dma_dev->device_prep_dma_memcpy = ep93xx_dma_prep_dma_memcpy;
1383 edma->hw_setup = m2m_hw_setup;
1384 edma->hw_shutdown = m2m_hw_shutdown;
1385 edma->hw_submit = m2m_hw_submit;
1386 edma->hw_interrupt = m2m_hw_interrupt;
1388 dma_cap_set(DMA_PRIVATE, dma_dev->cap_mask);
1390 edma->hw_synchronize = m2p_hw_synchronize;
1391 edma->hw_setup = m2p_hw_setup;
1392 edma->hw_shutdown = m2p_hw_shutdown;
1393 edma->hw_submit = m2p_hw_submit;
1394 edma->hw_interrupt = m2p_hw_interrupt;
1397 ret = dma_async_device_register(dma_dev);
1398 if (unlikely(ret)) {
1399 for (i = 0; i < edma->num_channels; i++) {
1400 struct ep93xx_dma_chan *edmac = &edma->channels[i];
1401 if (!IS_ERR_OR_NULL(edmac->clk))
1402 clk_put(edmac->clk);
1406 dev_info(dma_dev->dev, "EP93xx M2%s DMA ready\n",
1407 edma->m2m ? "M" : "P");
1413 static const struct platform_device_id ep93xx_dma_driver_ids[] = {
1414 { "ep93xx-dma-m2p", 0 },
1415 { "ep93xx-dma-m2m", 1 },
1419 static struct platform_driver ep93xx_dma_driver = {
1421 .name = "ep93xx-dma",
1423 .id_table = ep93xx_dma_driver_ids,
1426 static int __init ep93xx_dma_module_init(void)
1428 return platform_driver_probe(&ep93xx_dma_driver, ep93xx_dma_probe);
1430 subsys_initcall(ep93xx_dma_module_init);
1432 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
1433 MODULE_DESCRIPTION("EP93xx DMA driver");
1434 MODULE_LICENSE("GPL");