2 * Freescale i.MX28 APBH DMA driver
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 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <linux/list.h>
29 #include <asm/errno.h>
31 #include <asm/arch/clock.h>
32 #include <asm/arch/imx-regs.h>
33 #include <asm/arch/sys_proto.h>
34 #include <asm/imx-common/dma.h>
35 #include <asm/imx-common/regs-apbh.h>
37 static struct mxs_dma_chan mxs_dma_channels[MXS_MAX_DMA_CHANNELS];
40 * Test is the DMA channel is valid channel
42 int mxs_dma_validate_chan(int channel)
44 struct mxs_dma_chan *pchan;
46 if ((channel < 0) || (channel >= MXS_MAX_DMA_CHANNELS))
49 pchan = mxs_dma_channels + channel;
50 if (!(pchan->flags & MXS_DMA_FLAGS_ALLOCATED))
57 * Return the address of the command within a descriptor.
59 static unsigned int mxs_dma_cmd_address(struct mxs_dma_desc *desc)
61 return desc->address + offsetof(struct mxs_dma_desc, cmd);
65 * Read a DMA channel's hardware semaphore.
67 * As used by the MXS platform's DMA software, the DMA channel's hardware
68 * semaphore reflects the number of DMA commands the hardware will process, but
69 * has not yet finished. This is a volatile value read directly from hardware,
70 * so it must be be viewed as immediately stale.
72 * If the channel is not marked busy, or has finished processing all its
73 * commands, this value should be zero.
75 * See mxs_dma_append() for details on how DMA command blocks must be configured
76 * to maintain the expected behavior of the semaphore's value.
78 static int mxs_dma_read_semaphore(int channel)
80 struct mxs_apbh_regs *apbh_regs =
81 (struct mxs_apbh_regs *)MXS_APBH_BASE;
85 ret = mxs_dma_validate_chan(channel);
89 tmp = readl(&apbh_regs->ch[channel].hw_apbh_ch_sema);
91 tmp &= APBH_CHn_SEMA_PHORE_MASK;
92 tmp >>= APBH_CHn_SEMA_PHORE_OFFSET;
97 #ifndef CONFIG_SYS_DCACHE_OFF
98 void mxs_dma_flush_desc(struct mxs_dma_desc *desc)
103 addr = (uint32_t)desc;
104 size = roundup(sizeof(struct mxs_dma_desc), MXS_DMA_ALIGNMENT);
106 flush_dcache_range(addr, addr + size);
109 inline void mxs_dma_flush_desc(struct mxs_dma_desc *desc) {}
113 * Enable a DMA channel.
115 * If the given channel has any DMA descriptors on its active list, this
116 * function causes the DMA hardware to begin processing them.
118 * This function marks the DMA channel as "busy," whether or not there are any
119 * descriptors to process.
121 static int mxs_dma_enable(int channel)
123 struct mxs_apbh_regs *apbh_regs =
124 (struct mxs_apbh_regs *)MXS_APBH_BASE;
126 struct mxs_dma_chan *pchan;
127 struct mxs_dma_desc *pdesc;
130 ret = mxs_dma_validate_chan(channel);
134 pchan = mxs_dma_channels + channel;
136 if (pchan->pending_num == 0) {
137 pchan->flags |= MXS_DMA_FLAGS_BUSY;
141 pdesc = list_first_entry(&pchan->active, struct mxs_dma_desc, node);
145 if (pchan->flags & MXS_DMA_FLAGS_BUSY) {
146 if (!(pdesc->cmd.data & MXS_DMA_DESC_CHAIN))
149 sem = mxs_dma_read_semaphore(channel);
154 pdesc = list_entry(pdesc->node.next,
155 struct mxs_dma_desc, node);
156 writel(mxs_dma_cmd_address(pdesc),
157 &apbh_regs->ch[channel].hw_apbh_ch_nxtcmdar);
159 writel(pchan->pending_num,
160 &apbh_regs->ch[channel].hw_apbh_ch_sema);
161 pchan->active_num += pchan->pending_num;
162 pchan->pending_num = 0;
164 pchan->active_num += pchan->pending_num;
165 pchan->pending_num = 0;
166 writel(mxs_dma_cmd_address(pdesc),
167 &apbh_regs->ch[channel].hw_apbh_ch_nxtcmdar);
168 writel(pchan->active_num,
169 &apbh_regs->ch[channel].hw_apbh_ch_sema);
170 writel(1 << (channel + APBH_CTRL0_CLKGATE_CHANNEL_OFFSET),
171 &apbh_regs->hw_apbh_ctrl0_clr);
174 pchan->flags |= MXS_DMA_FLAGS_BUSY;
179 * Disable a DMA channel.
181 * This function shuts down a DMA channel and marks it as "not busy." Any
182 * descriptors on the active list are immediately moved to the head of the
183 * "done" list, whether or not they have actually been processed by the
184 * hardware. The "ready" flags of these descriptors are NOT cleared, so they
185 * still appear to be active.
187 * This function immediately shuts down a DMA channel's hardware, aborting any
188 * I/O that may be in progress, potentially leaving I/O hardware in an undefined
189 * state. It is unwise to call this function if there is ANY chance the hardware
190 * is still processing a command.
192 static int mxs_dma_disable(int channel)
194 struct mxs_dma_chan *pchan;
195 struct mxs_apbh_regs *apbh_regs =
196 (struct mxs_apbh_regs *)MXS_APBH_BASE;
199 ret = mxs_dma_validate_chan(channel);
203 pchan = mxs_dma_channels + channel;
205 if (!(pchan->flags & MXS_DMA_FLAGS_BUSY))
208 writel(1 << (channel + APBH_CTRL0_CLKGATE_CHANNEL_OFFSET),
209 &apbh_regs->hw_apbh_ctrl0_set);
211 pchan->flags &= ~MXS_DMA_FLAGS_BUSY;
212 pchan->active_num = 0;
213 pchan->pending_num = 0;
214 list_splice_init(&pchan->active, &pchan->done);
220 * Resets the DMA channel hardware.
222 static int mxs_dma_reset(int channel)
224 struct mxs_apbh_regs *apbh_regs =
225 (struct mxs_apbh_regs *)MXS_APBH_BASE;
227 #if defined(CONFIG_MX23)
228 uint32_t setreg = (uint32_t)(&apbh_regs->hw_apbh_ctrl0_set);
229 uint32_t offset = APBH_CTRL0_RESET_CHANNEL_OFFSET;
230 #elif (defined(CONFIG_MX28) || defined(CONFIG_MX6))
231 uint32_t setreg = (uint32_t)(&apbh_regs->hw_apbh_channel_ctrl_set);
232 uint32_t offset = APBH_CHANNEL_CTRL_RESET_CHANNEL_OFFSET;
235 ret = mxs_dma_validate_chan(channel);
239 writel(1 << (channel + offset), setreg);
245 * Enable or disable DMA interrupt.
247 * This function enables the given DMA channel to interrupt the CPU.
249 static int mxs_dma_enable_irq(int channel, int enable)
251 struct mxs_apbh_regs *apbh_regs =
252 (struct mxs_apbh_regs *)MXS_APBH_BASE;
255 ret = mxs_dma_validate_chan(channel);
260 writel(1 << (channel + APBH_CTRL1_CH_CMDCMPLT_IRQ_EN_OFFSET),
261 &apbh_regs->hw_apbh_ctrl1_set);
263 writel(1 << (channel + APBH_CTRL1_CH_CMDCMPLT_IRQ_EN_OFFSET),
264 &apbh_regs->hw_apbh_ctrl1_clr);
270 * Clear DMA interrupt.
272 * The software that is using the DMA channel must register to receive its
273 * interrupts and, when they arrive, must call this function to clear them.
275 static int mxs_dma_ack_irq(int channel)
277 struct mxs_apbh_regs *apbh_regs =
278 (struct mxs_apbh_regs *)MXS_APBH_BASE;
281 ret = mxs_dma_validate_chan(channel);
285 writel(1 << channel, &apbh_regs->hw_apbh_ctrl1_clr);
286 writel(1 << channel, &apbh_regs->hw_apbh_ctrl2_clr);
292 * Request to reserve a DMA channel
294 static int mxs_dma_request(int channel)
296 struct mxs_dma_chan *pchan;
298 if ((channel < 0) || (channel >= MXS_MAX_DMA_CHANNELS))
301 pchan = mxs_dma_channels + channel;
302 if ((pchan->flags & MXS_DMA_FLAGS_VALID) != MXS_DMA_FLAGS_VALID)
305 if (pchan->flags & MXS_DMA_FLAGS_ALLOCATED)
308 pchan->flags |= MXS_DMA_FLAGS_ALLOCATED;
309 pchan->active_num = 0;
310 pchan->pending_num = 0;
312 INIT_LIST_HEAD(&pchan->active);
313 INIT_LIST_HEAD(&pchan->done);
319 * Release a DMA channel.
321 * This function releases a DMA channel from its current owner.
323 * The channel will NOT be released if it's marked "busy" (see
326 int mxs_dma_release(int channel)
328 struct mxs_dma_chan *pchan;
331 ret = mxs_dma_validate_chan(channel);
335 pchan = mxs_dma_channels + channel;
337 if (pchan->flags & MXS_DMA_FLAGS_BUSY)
341 pchan->active_num = 0;
342 pchan->pending_num = 0;
343 pchan->flags &= ~MXS_DMA_FLAGS_ALLOCATED;
349 * Allocate DMA descriptor
351 struct mxs_dma_desc *mxs_dma_desc_alloc(void)
353 struct mxs_dma_desc *pdesc;
356 size = roundup(sizeof(struct mxs_dma_desc), MXS_DMA_ALIGNMENT);
357 pdesc = memalign(MXS_DMA_ALIGNMENT, size);
362 memset(pdesc, 0, sizeof(*pdesc));
363 pdesc->address = (dma_addr_t)pdesc;
369 * Free DMA descriptor
371 void mxs_dma_desc_free(struct mxs_dma_desc *pdesc)
380 * Add a DMA descriptor to a channel.
382 * If the descriptor list for this channel is not empty, this function sets the
383 * CHAIN bit and the NEXTCMD_ADDR fields in the last descriptor's DMA command so
384 * it will chain to the new descriptor's command.
386 * Then, this function marks the new descriptor as "ready," adds it to the end
387 * of the active descriptor list, and increments the count of pending
390 * The MXS platform DMA software imposes some rules on DMA commands to maintain
391 * important invariants. These rules are NOT checked, but they must be carefully
392 * applied by software that uses MXS DMA channels.
395 * The DMA channel's hardware semaphore must reflect the number of DMA
396 * commands the hardware will process, but has not yet finished.
399 * A DMA channel begins processing commands when its hardware semaphore is
400 * written with a value greater than zero, and it stops processing commands
401 * when the semaphore returns to zero.
403 * When a channel finishes a DMA command, it will decrement its semaphore if
404 * the DECREMENT_SEMAPHORE bit is set in that command's flags bits.
406 * In principle, it's not necessary for the DECREMENT_SEMAPHORE to be set,
407 * unless it suits the purposes of the software. For example, one could
408 * construct a series of five DMA commands, with the DECREMENT_SEMAPHORE
409 * bit set only in the last one. Then, setting the DMA channel's hardware
410 * semaphore to one would cause the entire series of five commands to be
411 * processed. However, this example would violate the invariant given above.
414 * ALL DMA commands MUST have the DECREMENT_SEMAPHORE bit set so that the DMA
415 * channel's hardware semaphore will be decremented EVERY time a command is
418 int mxs_dma_desc_append(int channel, struct mxs_dma_desc *pdesc)
420 struct mxs_dma_chan *pchan;
421 struct mxs_dma_desc *last;
424 ret = mxs_dma_validate_chan(channel);
428 pchan = mxs_dma_channels + channel;
430 pdesc->cmd.next = mxs_dma_cmd_address(pdesc);
431 pdesc->flags |= MXS_DMA_DESC_FIRST | MXS_DMA_DESC_LAST;
433 if (!list_empty(&pchan->active)) {
434 last = list_entry(pchan->active.prev, struct mxs_dma_desc,
437 pdesc->flags &= ~MXS_DMA_DESC_FIRST;
438 last->flags &= ~MXS_DMA_DESC_LAST;
440 last->cmd.next = mxs_dma_cmd_address(pdesc);
441 last->cmd.data |= MXS_DMA_DESC_CHAIN;
443 mxs_dma_flush_desc(last);
445 pdesc->flags |= MXS_DMA_DESC_READY;
446 if (pdesc->flags & MXS_DMA_DESC_FIRST)
447 pchan->pending_num++;
448 list_add_tail(&pdesc->node, &pchan->active);
450 mxs_dma_flush_desc(pdesc);
456 * Clean up processed DMA descriptors.
458 * This function removes processed DMA descriptors from the "active" list. Pass
459 * in a non-NULL list head to get the descriptors moved to your list. Pass NULL
460 * to get the descriptors moved to the channel's "done" list. Descriptors on
461 * the "done" list can be retrieved with mxs_dma_get_finished().
463 * This function marks the DMA channel as "not busy" if no unprocessed
464 * descriptors remain on the "active" list.
466 static int mxs_dma_finish(int channel, struct list_head *head)
469 struct mxs_dma_chan *pchan;
470 struct list_head *p, *q;
471 struct mxs_dma_desc *pdesc;
474 ret = mxs_dma_validate_chan(channel);
478 pchan = mxs_dma_channels + channel;
480 sem = mxs_dma_read_semaphore(channel);
484 if (sem == pchan->active_num)
487 list_for_each_safe(p, q, &pchan->active) {
488 if ((pchan->active_num) <= sem)
491 pdesc = list_entry(p, struct mxs_dma_desc, node);
492 pdesc->flags &= ~MXS_DMA_DESC_READY;
495 list_move_tail(p, head);
497 list_move_tail(p, &pchan->done);
499 if (pdesc->flags & MXS_DMA_DESC_LAST)
504 pchan->flags &= ~MXS_DMA_FLAGS_BUSY;
510 * Wait for DMA channel to complete
512 static int mxs_dma_wait_complete(uint32_t timeout, unsigned int chan)
514 struct mxs_apbh_regs *apbh_regs =
515 (struct mxs_apbh_regs *)MXS_APBH_BASE;
518 ret = mxs_dma_validate_chan(chan);
522 if (mxs_wait_mask_set(&apbh_regs->hw_apbh_ctrl1_reg,
523 1 << chan, timeout)) {
532 * Execute the DMA channel
534 int mxs_dma_go(int chan)
536 uint32_t timeout = 10000000;
539 LIST_HEAD(tmp_desc_list);
541 mxs_dma_enable_irq(chan, 1);
542 mxs_dma_enable(chan);
544 /* Wait for DMA to finish. */
545 ret = mxs_dma_wait_complete(timeout, chan);
547 /* Clear out the descriptors we just ran. */
548 mxs_dma_finish(chan, &tmp_desc_list);
550 /* Shut the DMA channel down. */
551 mxs_dma_ack_irq(chan);
553 mxs_dma_enable_irq(chan, 0);
554 mxs_dma_disable(chan);
560 * Initialize the DMA hardware
562 void mxs_dma_init(void)
564 struct mxs_apbh_regs *apbh_regs =
565 (struct mxs_apbh_regs *)MXS_APBH_BASE;
567 mxs_reset_block(&apbh_regs->hw_apbh_ctrl0_reg);
569 #ifdef CONFIG_APBH_DMA_BURST8
570 writel(APBH_CTRL0_AHB_BURST8_EN,
571 &apbh_regs->hw_apbh_ctrl0_set);
573 writel(APBH_CTRL0_AHB_BURST8_EN,
574 &apbh_regs->hw_apbh_ctrl0_clr);
577 #ifdef CONFIG_APBH_DMA_BURST
578 writel(APBH_CTRL0_APB_BURST_EN,
579 &apbh_regs->hw_apbh_ctrl0_set);
581 writel(APBH_CTRL0_APB_BURST_EN,
582 &apbh_regs->hw_apbh_ctrl0_clr);
586 int mxs_dma_init_channel(int channel)
588 struct mxs_dma_chan *pchan;
591 pchan = mxs_dma_channels + channel;
592 pchan->flags = MXS_DMA_FLAGS_VALID;
594 ret = mxs_dma_request(channel);
597 printf("MXS DMA: Can't acquire DMA channel %i\n",
602 mxs_dma_reset(channel);
603 mxs_dma_ack_irq(channel);