1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2008 by NXP Semiconductors
5 * @Descr: LPC3250 DMA controller interface support functions
7 * Copyright (c) 2015 Tyco Fire Protection Products.
13 #include <asm/arch/dma.h>
14 #include <asm/arch/cpu.h>
15 #include <asm/arch/clk.h>
16 #include <asm/arch/sys_proto.h>
18 #include <linux/bitops.h>
19 #include <linux/delay.h>
21 /* DMA controller channel register structure */
22 struct dmac_chan_reg {
31 /* DMA controller register structures */
43 u32 sw_last_burst_req;
44 u32 sw_last_single_req;
48 struct dmac_chan_reg dma_chan[8];
51 #define DMA_NO_OF_CHANNELS 8
53 /* config register definitions */
54 #define DMAC_CTRL_ENABLE (1 << 0) /* For enabling the DMA controller */
58 static struct dma_reg *dma = (struct dma_reg *)DMA_BASE;
60 int lpc32xx_dma_get_channel(void)
64 if (!alloc_ch) { /* First time caller */
66 * DMA clock are enable by "lpc32xx_dma_init()" and should
67 * be call by board "board_early_init_f()" function.
71 * Make sure DMA controller and all channels are disabled.
72 * Controller is in little-endian mode. Disable sync signals.
74 writel(0, &dma->config);
75 writel(0, &dma->sync);
77 /* Clear interrupt and error statuses */
78 writel(0xFF, &dma->int_tc_clear);
79 writel(0xFF, &dma->raw_tc_stat);
80 writel(0xFF, &dma->int_err_clear);
81 writel(0xFF, &dma->raw_err_stat);
83 /* Enable DMA controller */
84 writel(DMAC_CTRL_ENABLE, &dma->config);
89 /* Check if all the available channels are busy */
90 if (unlikely(i == DMA_NO_OF_CHANNELS))
92 alloc_ch |= BIT_MASK(i);
96 int lpc32xx_dma_start_xfer(unsigned int channel,
97 const struct lpc32xx_dmac_ll *desc, u32 config)
99 if (unlikely(((BIT_MASK(channel) & alloc_ch) == 0) ||
100 (channel >= DMA_NO_OF_CHANNELS))) {
101 pr_err("Request for xfer on unallocated channel %d", channel);
104 writel(BIT_MASK(channel), &dma->int_tc_clear);
105 writel(BIT_MASK(channel), &dma->int_err_clear);
106 writel(desc->dma_src, &dma->dma_chan[channel].src_addr);
107 writel(desc->dma_dest, &dma->dma_chan[channel].dest_addr);
108 writel(desc->next_lli, &dma->dma_chan[channel].lli);
109 writel(desc->next_ctrl, &dma->dma_chan[channel].control);
110 writel(config, &dma->dma_chan[channel].config_ch);
115 int lpc32xx_dma_wait_status(unsigned int channel)
120 /* Check if given channel is valid */
121 if (unlikely(channel >= DMA_NO_OF_CHANNELS)) {
122 pr_err("Request for status on unallocated channel %d", channel);
126 start = get_timer(0);
128 reg = readl(&dma->raw_tc_stat);
129 reg |= readl(dma->raw_err_stat);
130 if (reg & BIT_MASK(channel))
133 if (get_timer(start) > CONFIG_SYS_HZ) {
134 pr_err("DMA status timeout channel %d\n", channel);
140 if (unlikely(readl(&dma->raw_err_stat) & BIT_MASK(channel))) {
141 setbits_le32(&dma->int_err_clear, BIT_MASK(channel));
142 setbits_le32(&dma->raw_err_stat, BIT_MASK(channel));
143 pr_err("DMA error on channel %d\n", channel);
146 setbits_le32(&dma->int_tc_clear, BIT_MASK(channel));
147 setbits_le32(&dma->raw_tc_stat, BIT_MASK(channel));