2 * Freescale i.MX28 APBH DMA driver
4 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5 * on behalf of DENX Software Engineering GmbH
7 * Based on code from LTIB:
8 * Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved.
10 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/list.h>
17 #include <linux/errno.h>
19 #include <asm/arch/clock.h>
20 #include <asm/arch/imx-regs.h>
21 #include <asm/arch/sys_proto.h>
22 #include <asm/imx-common/dma.h>
23 #include <asm/imx-common/regs-apbh.h>
25 static struct mxs_dma_chan mxs_dma_channels[MXS_MAX_DMA_CHANNELS];
28 * Test is the DMA channel is valid channel
30 int mxs_dma_validate_chan(int channel)
32 struct mxs_dma_chan *pchan;
34 if ((channel < 0) || (channel >= MXS_MAX_DMA_CHANNELS))
37 pchan = mxs_dma_channels + channel;
38 if (!(pchan->flags & MXS_DMA_FLAGS_ALLOCATED))
45 * Return the address of the command within a descriptor.
47 static unsigned int mxs_dma_cmd_address(struct mxs_dma_desc *desc)
49 return desc->address + offsetof(struct mxs_dma_desc, cmd);
53 * Read a DMA channel's hardware semaphore.
55 * As used by the MXS platform's DMA software, the DMA channel's hardware
56 * semaphore reflects the number of DMA commands the hardware will process, but
57 * has not yet finished. This is a volatile value read directly from hardware,
58 * so it must be be viewed as immediately stale.
60 * If the channel is not marked busy, or has finished processing all its
61 * commands, this value should be zero.
63 * See mxs_dma_append() for details on how DMA command blocks must be configured
64 * to maintain the expected behavior of the semaphore's value.
66 static int mxs_dma_read_semaphore(int channel)
68 struct mxs_apbh_regs *apbh_regs =
69 (struct mxs_apbh_regs *)MXS_APBH_BASE;
73 ret = mxs_dma_validate_chan(channel);
77 tmp = readl(&apbh_regs->ch[channel].hw_apbh_ch_sema);
79 tmp &= APBH_CHn_SEMA_PHORE_MASK;
80 tmp >>= APBH_CHn_SEMA_PHORE_OFFSET;
85 #ifndef CONFIG_SYS_DCACHE_OFF
86 void mxs_dma_flush_desc(struct mxs_dma_desc *desc)
91 addr = (uint32_t)desc;
92 size = roundup(sizeof(struct mxs_dma_desc), MXS_DMA_ALIGNMENT);
94 flush_dcache_range(addr, addr + size);
97 inline void mxs_dma_flush_desc(struct mxs_dma_desc *desc) {}
101 * Enable a DMA channel.
103 * If the given channel has any DMA descriptors on its active list, this
104 * function causes the DMA hardware to begin processing them.
106 * This function marks the DMA channel as "busy," whether or not there are any
107 * descriptors to process.
109 static int mxs_dma_enable(int channel)
111 struct mxs_apbh_regs *apbh_regs =
112 (struct mxs_apbh_regs *)MXS_APBH_BASE;
114 struct mxs_dma_chan *pchan;
115 struct mxs_dma_desc *pdesc;
118 ret = mxs_dma_validate_chan(channel);
122 pchan = mxs_dma_channels + channel;
124 if (pchan->pending_num == 0) {
125 pchan->flags |= MXS_DMA_FLAGS_BUSY;
129 pdesc = list_first_entry(&pchan->active, struct mxs_dma_desc, node);
133 if (pchan->flags & MXS_DMA_FLAGS_BUSY) {
134 if (!(pdesc->cmd.data & MXS_DMA_DESC_CHAIN))
137 sem = mxs_dma_read_semaphore(channel);
142 pdesc = list_entry(pdesc->node.next,
143 struct mxs_dma_desc, node);
144 writel(mxs_dma_cmd_address(pdesc),
145 &apbh_regs->ch[channel].hw_apbh_ch_nxtcmdar);
147 writel(pchan->pending_num,
148 &apbh_regs->ch[channel].hw_apbh_ch_sema);
149 pchan->active_num += pchan->pending_num;
150 pchan->pending_num = 0;
152 pchan->active_num += pchan->pending_num;
153 pchan->pending_num = 0;
154 writel(mxs_dma_cmd_address(pdesc),
155 &apbh_regs->ch[channel].hw_apbh_ch_nxtcmdar);
156 writel(pchan->active_num,
157 &apbh_regs->ch[channel].hw_apbh_ch_sema);
158 writel(1 << (channel + APBH_CTRL0_CLKGATE_CHANNEL_OFFSET),
159 &apbh_regs->hw_apbh_ctrl0_clr);
162 pchan->flags |= MXS_DMA_FLAGS_BUSY;
167 * Disable a DMA channel.
169 * This function shuts down a DMA channel and marks it as "not busy." Any
170 * descriptors on the active list are immediately moved to the head of the
171 * "done" list, whether or not they have actually been processed by the
172 * hardware. The "ready" flags of these descriptors are NOT cleared, so they
173 * still appear to be active.
175 * This function immediately shuts down a DMA channel's hardware, aborting any
176 * I/O that may be in progress, potentially leaving I/O hardware in an undefined
177 * state. It is unwise to call this function if there is ANY chance the hardware
178 * is still processing a command.
180 static int mxs_dma_disable(int channel)
182 struct mxs_dma_chan *pchan;
183 struct mxs_apbh_regs *apbh_regs =
184 (struct mxs_apbh_regs *)MXS_APBH_BASE;
187 ret = mxs_dma_validate_chan(channel);
191 pchan = mxs_dma_channels + channel;
193 if (!(pchan->flags & MXS_DMA_FLAGS_BUSY))
196 writel(1 << (channel + APBH_CTRL0_CLKGATE_CHANNEL_OFFSET),
197 &apbh_regs->hw_apbh_ctrl0_set);
199 pchan->flags &= ~MXS_DMA_FLAGS_BUSY;
200 pchan->active_num = 0;
201 pchan->pending_num = 0;
202 list_splice_init(&pchan->active, &pchan->done);
208 * Resets the DMA channel hardware.
210 static int mxs_dma_reset(int channel)
212 struct mxs_apbh_regs *apbh_regs =
213 (struct mxs_apbh_regs *)MXS_APBH_BASE;
215 #if defined(CONFIG_MX23)
216 uint32_t setreg = (uint32_t)(&apbh_regs->hw_apbh_ctrl0_set);
217 uint32_t offset = APBH_CTRL0_RESET_CHANNEL_OFFSET;
218 #elif (defined(CONFIG_MX28) || defined(CONFIG_MX6) || defined(CONFIG_MX7))
219 uint32_t setreg = (uint32_t)(&apbh_regs->hw_apbh_channel_ctrl_set);
220 uint32_t offset = APBH_CHANNEL_CTRL_RESET_CHANNEL_OFFSET;
223 ret = mxs_dma_validate_chan(channel);
227 writel(1 << (channel + offset), setreg);
233 * Enable or disable DMA interrupt.
235 * This function enables the given DMA channel to interrupt the CPU.
237 static int mxs_dma_enable_irq(int channel, int enable)
239 struct mxs_apbh_regs *apbh_regs =
240 (struct mxs_apbh_regs *)MXS_APBH_BASE;
243 ret = mxs_dma_validate_chan(channel);
248 writel(1 << (channel + APBH_CTRL1_CH_CMDCMPLT_IRQ_EN_OFFSET),
249 &apbh_regs->hw_apbh_ctrl1_set);
251 writel(1 << (channel + APBH_CTRL1_CH_CMDCMPLT_IRQ_EN_OFFSET),
252 &apbh_regs->hw_apbh_ctrl1_clr);
258 * Clear DMA interrupt.
260 * The software that is using the DMA channel must register to receive its
261 * interrupts and, when they arrive, must call this function to clear them.
263 static int mxs_dma_ack_irq(int channel)
265 struct mxs_apbh_regs *apbh_regs =
266 (struct mxs_apbh_regs *)MXS_APBH_BASE;
269 ret = mxs_dma_validate_chan(channel);
273 writel(1 << channel, &apbh_regs->hw_apbh_ctrl1_clr);
274 writel(1 << channel, &apbh_regs->hw_apbh_ctrl2_clr);
280 * Request to reserve a DMA channel
282 static int mxs_dma_request(int channel)
284 struct mxs_dma_chan *pchan;
286 if ((channel < 0) || (channel >= MXS_MAX_DMA_CHANNELS))
289 pchan = mxs_dma_channels + channel;
290 if ((pchan->flags & MXS_DMA_FLAGS_VALID) != MXS_DMA_FLAGS_VALID)
293 if (pchan->flags & MXS_DMA_FLAGS_ALLOCATED)
296 pchan->flags |= MXS_DMA_FLAGS_ALLOCATED;
297 pchan->active_num = 0;
298 pchan->pending_num = 0;
300 INIT_LIST_HEAD(&pchan->active);
301 INIT_LIST_HEAD(&pchan->done);
307 * Release a DMA channel.
309 * This function releases a DMA channel from its current owner.
311 * The channel will NOT be released if it's marked "busy" (see
314 int mxs_dma_release(int channel)
316 struct mxs_dma_chan *pchan;
319 ret = mxs_dma_validate_chan(channel);
323 pchan = mxs_dma_channels + channel;
325 if (pchan->flags & MXS_DMA_FLAGS_BUSY)
329 pchan->active_num = 0;
330 pchan->pending_num = 0;
331 pchan->flags &= ~MXS_DMA_FLAGS_ALLOCATED;
337 * Allocate DMA descriptor
339 struct mxs_dma_desc *mxs_dma_desc_alloc(void)
341 struct mxs_dma_desc *pdesc;
344 size = roundup(sizeof(struct mxs_dma_desc), MXS_DMA_ALIGNMENT);
345 pdesc = memalign(MXS_DMA_ALIGNMENT, size);
350 memset(pdesc, 0, sizeof(*pdesc));
351 pdesc->address = (dma_addr_t)pdesc;
357 * Free DMA descriptor
359 void mxs_dma_desc_free(struct mxs_dma_desc *pdesc)
368 * Add a DMA descriptor to a channel.
370 * If the descriptor list for this channel is not empty, this function sets the
371 * CHAIN bit and the NEXTCMD_ADDR fields in the last descriptor's DMA command so
372 * it will chain to the new descriptor's command.
374 * Then, this function marks the new descriptor as "ready," adds it to the end
375 * of the active descriptor list, and increments the count of pending
378 * The MXS platform DMA software imposes some rules on DMA commands to maintain
379 * important invariants. These rules are NOT checked, but they must be carefully
380 * applied by software that uses MXS DMA channels.
383 * The DMA channel's hardware semaphore must reflect the number of DMA
384 * commands the hardware will process, but has not yet finished.
387 * A DMA channel begins processing commands when its hardware semaphore is
388 * written with a value greater than zero, and it stops processing commands
389 * when the semaphore returns to zero.
391 * When a channel finishes a DMA command, it will decrement its semaphore if
392 * the DECREMENT_SEMAPHORE bit is set in that command's flags bits.
394 * In principle, it's not necessary for the DECREMENT_SEMAPHORE to be set,
395 * unless it suits the purposes of the software. For example, one could
396 * construct a series of five DMA commands, with the DECREMENT_SEMAPHORE
397 * bit set only in the last one. Then, setting the DMA channel's hardware
398 * semaphore to one would cause the entire series of five commands to be
399 * processed. However, this example would violate the invariant given above.
402 * ALL DMA commands MUST have the DECREMENT_SEMAPHORE bit set so that the DMA
403 * channel's hardware semaphore will be decremented EVERY time a command is
406 int mxs_dma_desc_append(int channel, struct mxs_dma_desc *pdesc)
408 struct mxs_dma_chan *pchan;
409 struct mxs_dma_desc *last;
412 ret = mxs_dma_validate_chan(channel);
416 pchan = mxs_dma_channels + channel;
418 pdesc->cmd.next = mxs_dma_cmd_address(pdesc);
419 pdesc->flags |= MXS_DMA_DESC_FIRST | MXS_DMA_DESC_LAST;
421 if (!list_empty(&pchan->active)) {
422 last = list_entry(pchan->active.prev, struct mxs_dma_desc,
425 pdesc->flags &= ~MXS_DMA_DESC_FIRST;
426 last->flags &= ~MXS_DMA_DESC_LAST;
428 last->cmd.next = mxs_dma_cmd_address(pdesc);
429 last->cmd.data |= MXS_DMA_DESC_CHAIN;
431 mxs_dma_flush_desc(last);
433 pdesc->flags |= MXS_DMA_DESC_READY;
434 if (pdesc->flags & MXS_DMA_DESC_FIRST)
435 pchan->pending_num++;
436 list_add_tail(&pdesc->node, &pchan->active);
438 mxs_dma_flush_desc(pdesc);
444 * Clean up processed DMA descriptors.
446 * This function removes processed DMA descriptors from the "active" list. Pass
447 * in a non-NULL list head to get the descriptors moved to your list. Pass NULL
448 * to get the descriptors moved to the channel's "done" list. Descriptors on
449 * the "done" list can be retrieved with mxs_dma_get_finished().
451 * This function marks the DMA channel as "not busy" if no unprocessed
452 * descriptors remain on the "active" list.
454 static int mxs_dma_finish(int channel, struct list_head *head)
457 struct mxs_dma_chan *pchan;
458 struct list_head *p, *q;
459 struct mxs_dma_desc *pdesc;
462 ret = mxs_dma_validate_chan(channel);
466 pchan = mxs_dma_channels + channel;
468 sem = mxs_dma_read_semaphore(channel);
472 if (sem == pchan->active_num)
475 list_for_each_safe(p, q, &pchan->active) {
476 if ((pchan->active_num) <= sem)
479 pdesc = list_entry(p, struct mxs_dma_desc, node);
480 pdesc->flags &= ~MXS_DMA_DESC_READY;
483 list_move_tail(p, head);
485 list_move_tail(p, &pchan->done);
487 if (pdesc->flags & MXS_DMA_DESC_LAST)
492 pchan->flags &= ~MXS_DMA_FLAGS_BUSY;
498 * Wait for DMA channel to complete
500 static int mxs_dma_wait_complete(uint32_t timeout, unsigned int chan)
502 struct mxs_apbh_regs *apbh_regs =
503 (struct mxs_apbh_regs *)MXS_APBH_BASE;
506 ret = mxs_dma_validate_chan(chan);
510 if (mxs_wait_mask_set(&apbh_regs->hw_apbh_ctrl1_reg,
511 1 << chan, timeout)) {
520 * Execute the DMA channel
522 int mxs_dma_go(int chan)
524 uint32_t timeout = 10000000;
527 LIST_HEAD(tmp_desc_list);
529 mxs_dma_enable_irq(chan, 1);
530 mxs_dma_enable(chan);
532 /* Wait for DMA to finish. */
533 ret = mxs_dma_wait_complete(timeout, chan);
535 /* Clear out the descriptors we just ran. */
536 mxs_dma_finish(chan, &tmp_desc_list);
538 /* Shut the DMA channel down. */
539 mxs_dma_ack_irq(chan);
541 mxs_dma_enable_irq(chan, 0);
542 mxs_dma_disable(chan);
548 * Execute a continuously running circular DMA descriptor.
549 * NOTE: This is not intended for general use, but rather
550 * for the LCD driver in Smart-LCD mode. It allows
551 * continuous triggering of the RUN bit there.
553 void mxs_dma_circ_start(int chan, struct mxs_dma_desc *pdesc)
555 struct mxs_apbh_regs *apbh_regs =
556 (struct mxs_apbh_regs *)MXS_APBH_BASE;
558 mxs_dma_flush_desc(pdesc);
560 mxs_dma_enable_irq(chan, 1);
562 writel(mxs_dma_cmd_address(pdesc),
563 &apbh_regs->ch[chan].hw_apbh_ch_nxtcmdar);
564 writel(1, &apbh_regs->ch[chan].hw_apbh_ch_sema);
565 writel(1 << (chan + APBH_CTRL0_CLKGATE_CHANNEL_OFFSET),
566 &apbh_regs->hw_apbh_ctrl0_clr);
570 * Initialize the DMA hardware
572 void mxs_dma_init(void)
574 struct mxs_apbh_regs *apbh_regs =
575 (struct mxs_apbh_regs *)MXS_APBH_BASE;
577 mxs_reset_block(&apbh_regs->hw_apbh_ctrl0_reg);
579 #ifdef CONFIG_APBH_DMA_BURST8
580 writel(APBH_CTRL0_AHB_BURST8_EN,
581 &apbh_regs->hw_apbh_ctrl0_set);
583 writel(APBH_CTRL0_AHB_BURST8_EN,
584 &apbh_regs->hw_apbh_ctrl0_clr);
587 #ifdef CONFIG_APBH_DMA_BURST
588 writel(APBH_CTRL0_APB_BURST_EN,
589 &apbh_regs->hw_apbh_ctrl0_set);
591 writel(APBH_CTRL0_APB_BURST_EN,
592 &apbh_regs->hw_apbh_ctrl0_clr);
596 int mxs_dma_init_channel(int channel)
598 struct mxs_dma_chan *pchan;
601 pchan = mxs_dma_channels + channel;
602 pchan->flags = MXS_DMA_FLAGS_VALID;
604 ret = mxs_dma_request(channel);
607 printf("MXS DMA: Can't acquire DMA channel %i\n",
612 mxs_dma_reset(channel);
613 mxs_dma_ack_irq(channel);