2 * IBM PPC4xx DMA engine core library
4 * Copyright 2000-2004 MontaVista Software Inc.
6 * Cleaned up and converted to new DCR access
7 * Matt Porter <mporter@kernel.crashing.org>
9 * Original code by Armin Kuster <akuster@mvista.com>
10 * and Pete Popov <ppopov@mvista.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/config.h>
23 #include <linux/kernel.h>
25 #include <linux/miscdevice.h>
26 #include <linux/init.h>
27 #include <linux/module.h>
29 #include <asm/system.h>
32 #include <asm/ppc4xx_dma.h>
34 ppc_dma_ch_t dma_channels[MAX_PPC4xx_DMA_CHANNELS];
37 ppc4xx_get_dma_status(void)
39 return (mfdcr(DCRN_DMASR));
43 ppc4xx_set_src_addr(int dmanr, phys_addr_t src_addr)
45 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
46 printk("set_src_addr: bad channel: %d\n", dmanr);
50 #ifdef PPC4xx_DMA_64BIT
51 mtdcr(DCRN_DMASAH0 + dmanr*2, (u32)(src_addr >> 32));
53 mtdcr(DCRN_DMASA0 + dmanr*2, (u32)src_addr);
58 ppc4xx_set_dst_addr(int dmanr, phys_addr_t dst_addr)
60 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
61 printk("set_dst_addr: bad channel: %d\n", dmanr);
65 #ifdef PPC4xx_DMA_64BIT
66 mtdcr(DCRN_DMADAH0 + dmanr*2, (u32)(dst_addr >> 32));
68 mtdcr(DCRN_DMADA0 + dmanr*2, (u32)dst_addr);
73 ppc4xx_enable_dma(unsigned int dmanr)
76 ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
77 unsigned int status_bits[] = { DMA_CS0 | DMA_TS0 | DMA_CH0_ERR,
78 DMA_CS1 | DMA_TS1 | DMA_CH1_ERR,
79 DMA_CS2 | DMA_TS2 | DMA_CH2_ERR,
80 DMA_CS3 | DMA_TS3 | DMA_CH3_ERR};
82 if (p_dma_ch->in_use) {
83 printk("enable_dma: channel %d in use\n", dmanr);
87 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
88 printk("enable_dma: bad channel: %d\n", dmanr);
92 if (p_dma_ch->mode == DMA_MODE_READ) {
93 /* peripheral to memory */
94 ppc4xx_set_src_addr(dmanr, 0);
95 ppc4xx_set_dst_addr(dmanr, p_dma_ch->addr);
96 } else if (p_dma_ch->mode == DMA_MODE_WRITE) {
97 /* memory to peripheral */
98 ppc4xx_set_src_addr(dmanr, p_dma_ch->addr);
99 ppc4xx_set_dst_addr(dmanr, 0);
102 /* for other xfer modes, the addresses are already set */
103 control = mfdcr(DCRN_DMACR0 + (dmanr * 0x8));
105 control &= ~(DMA_TM_MASK | DMA_TD); /* clear all mode bits */
106 if (p_dma_ch->mode == DMA_MODE_MM) {
107 /* software initiated memory to memory */
108 control |= DMA_ETD_OUTPUT | DMA_TCE_ENABLE;
111 mtdcr(DCRN_DMACR0 + (dmanr * 0x8), control);
114 * Clear the CS, TS, RI bits for the channel from DMASR. This
115 * has been observed to happen correctly only after the mode and
116 * ETD/DCE bits in DMACRx are set above. Must do this before
117 * enabling the channel.
120 mtdcr(DCRN_DMASR, status_bits[dmanr]);
123 * For device-paced transfers, Terminal Count Enable apparently
124 * must be on, and this must be turned on after the mode, etc.
125 * bits are cleared above (at least on Redwood-6).
128 if ((p_dma_ch->mode == DMA_MODE_MM_DEVATDST) ||
129 (p_dma_ch->mode == DMA_MODE_MM_DEVATSRC))
130 control |= DMA_TCE_ENABLE;
133 * Now enable the channel.
136 control |= (p_dma_ch->mode | DMA_CE_ENABLE);
138 mtdcr(DCRN_DMACR0 + (dmanr * 0x8), control);
140 p_dma_ch->in_use = 1;
144 ppc4xx_disable_dma(unsigned int dmanr)
146 unsigned int control;
147 ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
149 if (!p_dma_ch->in_use) {
150 printk("disable_dma: channel %d not in use\n", dmanr);
154 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
155 printk("disable_dma: bad channel: %d\n", dmanr);
159 control = mfdcr(DCRN_DMACR0 + (dmanr * 0x8));
160 control &= ~DMA_CE_ENABLE;
161 mtdcr(DCRN_DMACR0 + (dmanr * 0x8), control);
163 p_dma_ch->in_use = 0;
167 * Sets the dma mode for single DMA transfers only.
168 * For scatter/gather transfers, the mode is passed to the
169 * alloc_dma_handle() function as one of the parameters.
171 * The mode is simply saved and used later. This allows
172 * the driver to call set_dma_mode() and set_dma_addr() in
175 * Valid mode values are:
177 * DMA_MODE_READ peripheral to memory
178 * DMA_MODE_WRITE memory to peripheral
179 * DMA_MODE_MM memory to memory
180 * DMA_MODE_MM_DEVATSRC device-paced memory to memory, device at src
181 * DMA_MODE_MM_DEVATDST device-paced memory to memory, device at dst
184 ppc4xx_set_dma_mode(unsigned int dmanr, unsigned int mode)
186 ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
188 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
189 printk("set_dma_mode: bad channel 0x%x\n", dmanr);
190 return DMA_STATUS_BAD_CHANNEL;
193 p_dma_ch->mode = mode;
195 return DMA_STATUS_GOOD;
199 * Sets the DMA Count register. Note that 'count' is in bytes.
200 * However, the DMA Count register counts the number of "transfers",
201 * where each transfer is equal to the bus width. Thus, count
202 * MUST be a multiple of the bus width.
205 ppc4xx_set_dma_count(unsigned int dmanr, unsigned int count)
207 ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
212 switch (p_dma_ch->pwidth) {
228 printk("set_dma_count: invalid bus width: 0x%x\n",
234 ("Warning: set_dma_count count 0x%x bus width %d\n",
235 count, p_dma_ch->pwidth);
239 count = count >> p_dma_ch->shift;
241 mtdcr(DCRN_DMACT0 + (dmanr * 0x8), count);
245 * Returns the number of bytes left to be transfered.
246 * After a DMA transfer, this should return zero.
247 * Reading this while a DMA transfer is still in progress will return
248 * unpredictable results.
251 ppc4xx_get_dma_residue(unsigned int dmanr)
254 ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
256 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
257 printk("ppc4xx_get_dma_residue: bad channel 0x%x\n", dmanr);
258 return DMA_STATUS_BAD_CHANNEL;
261 count = mfdcr(DCRN_DMACT0 + (dmanr * 0x8));
263 return (count << p_dma_ch->shift);
267 * Sets the DMA address for a memory to peripheral or peripheral
268 * to memory transfer. The address is just saved in the channel
269 * structure for now and used later in enable_dma().
272 ppc4xx_set_dma_addr(unsigned int dmanr, phys_addr_t addr)
274 ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
276 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
277 printk("ppc4xx_set_dma_addr: bad channel: %d\n", dmanr);
284 switch (p_dma_ch->pwidth) {
288 if ((unsigned) addr & 0x1)
292 if ((unsigned) addr & 0x3)
296 if ((unsigned) addr & 0x7)
300 printk("ppc4xx_set_dma_addr: invalid bus width: 0x%x\n",
305 printk("Warning: ppc4xx_set_dma_addr addr 0x%x bus width %d\n",
306 addr, p_dma_ch->pwidth);
310 /* save dma address and program it later after we know the xfer mode */
311 p_dma_ch->addr = addr;
315 * Sets both DMA addresses for a memory to memory transfer.
316 * For memory to peripheral or peripheral to memory transfers
317 * the function set_dma_addr() should be used instead.
320 ppc4xx_set_dma_addr2(unsigned int dmanr, phys_addr_t src_dma_addr,
321 phys_addr_t dst_dma_addr)
323 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
324 printk("ppc4xx_set_dma_addr2: bad channel: %d\n", dmanr);
330 ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
332 switch (p_dma_ch->pwidth) {
336 if (((unsigned) src_dma_addr & 0x1) ||
337 ((unsigned) dst_dma_addr & 0x1)
342 if (((unsigned) src_dma_addr & 0x3) ||
343 ((unsigned) dst_dma_addr & 0x3)
348 if (((unsigned) src_dma_addr & 0x7) ||
349 ((unsigned) dst_dma_addr & 0x7)
354 printk("ppc4xx_set_dma_addr2: invalid bus width: 0x%x\n",
360 ("Warning: ppc4xx_set_dma_addr2 src 0x%x dst 0x%x bus width %d\n",
361 src_dma_addr, dst_dma_addr, p_dma_ch->pwidth);
365 ppc4xx_set_src_addr(dmanr, src_dma_addr);
366 ppc4xx_set_dst_addr(dmanr, dst_dma_addr);
370 * Enables the channel interrupt.
372 * If performing a scatter/gatter transfer, this function
373 * MUST be called before calling alloc_dma_handle() and building
374 * the sgl list. Otherwise, interrupts will not be enabled, if
375 * they were previously disabled.
378 ppc4xx_enable_dma_interrupt(unsigned int dmanr)
380 unsigned int control;
381 ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
383 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
384 printk("ppc4xx_enable_dma_interrupt: bad channel: %d\n", dmanr);
385 return DMA_STATUS_BAD_CHANNEL;
388 p_dma_ch->int_enable = 1;
390 control = mfdcr(DCRN_DMACR0 + (dmanr * 0x8));
391 control |= DMA_CIE_ENABLE; /* Channel Interrupt Enable */
392 mtdcr(DCRN_DMACR0 + (dmanr * 0x8), control);
394 return DMA_STATUS_GOOD;
398 * Disables the channel interrupt.
400 * If performing a scatter/gatter transfer, this function
401 * MUST be called before calling alloc_dma_handle() and building
402 * the sgl list. Otherwise, interrupts will not be disabled, if
403 * they were previously enabled.
406 ppc4xx_disable_dma_interrupt(unsigned int dmanr)
408 unsigned int control;
409 ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
411 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
412 printk("ppc4xx_disable_dma_interrupt: bad channel: %d\n", dmanr);
413 return DMA_STATUS_BAD_CHANNEL;
416 p_dma_ch->int_enable = 0;
418 control = mfdcr(DCRN_DMACR0 + (dmanr * 0x8));
419 control &= ~DMA_CIE_ENABLE; /* Channel Interrupt Enable */
420 mtdcr(DCRN_DMACR0 + (dmanr * 0x8), control);
422 return DMA_STATUS_GOOD;
426 * Configures a DMA channel, including the peripheral bus width, if a
427 * peripheral is attached to the channel, the polarity of the DMAReq and
428 * DMAAck signals, etc. This information should really be setup by the boot
429 * code, since most likely the configuration won't change dynamically.
430 * If the kernel has to call this function, it's recommended that it's
431 * called from platform specific init code. The driver should not need to
432 * call this function.
435 ppc4xx_init_dma_channel(unsigned int dmanr, ppc_dma_ch_t * p_init)
437 unsigned int polarity;
438 uint32_t control = 0;
439 ppc_dma_ch_t *p_dma_ch = &dma_channels[dmanr];
441 DMA_MODE_READ = (unsigned long) DMA_TD; /* Peripheral to Memory */
442 DMA_MODE_WRITE = 0; /* Memory to Peripheral */
445 printk("ppc4xx_init_dma_channel: NULL p_init\n");
446 return DMA_STATUS_NULL_POINTER;
449 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
450 printk("ppc4xx_init_dma_channel: bad channel %d\n", dmanr);
451 return DMA_STATUS_BAD_CHANNEL;
455 polarity = mfdcr(DCRN_POL);
460 /* Setup the control register based on the values passed to
461 * us in p_init. Then, over-write the control register with this
464 control |= SET_DMA_CONTROL;
466 /* clear all polarity signals and then "or" in new signal levels */
467 polarity &= ~GET_DMA_POLARITY(dmanr);
468 polarity |= p_init->polarity;
470 mtdcr(DCRN_POL, polarity);
472 mtdcr(DCRN_DMACR0 + (dmanr * 0x8), control);
474 /* save these values in our dma channel structure */
475 memcpy(p_dma_ch, p_init, sizeof (ppc_dma_ch_t));
478 * The peripheral width values written in the control register are:
484 * Since the DMA count register takes the number of "transfers",
485 * we need to divide the count sent to us in certain
486 * functions by the appropriate number. It so happens that our
487 * right shift value is equal to the peripheral width value.
489 p_dma_ch->shift = p_init->pwidth;
492 * Save the control word for easy access.
494 p_dma_ch->control = control;
496 mtdcr(DCRN_DMASR, 0xffffffff); /* clear status register */
497 return DMA_STATUS_GOOD;
501 * This function returns the channel configuration.
504 ppc4xx_get_channel_config(unsigned int dmanr, ppc_dma_ch_t * p_dma_ch)
506 unsigned int polarity;
507 unsigned int control;
509 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
510 printk("ppc4xx_get_channel_config: bad channel %d\n", dmanr);
511 return DMA_STATUS_BAD_CHANNEL;
514 memcpy(p_dma_ch, &dma_channels[dmanr], sizeof (ppc_dma_ch_t));
517 polarity = mfdcr(DCRN_POL);
522 p_dma_ch->polarity = polarity & GET_DMA_POLARITY(dmanr);
523 control = mfdcr(DCRN_DMACR0 + (dmanr * 0x8));
525 p_dma_ch->cp = GET_DMA_PRIORITY(control);
526 p_dma_ch->pwidth = GET_DMA_PW(control);
527 p_dma_ch->psc = GET_DMA_PSC(control);
528 p_dma_ch->pwc = GET_DMA_PWC(control);
529 p_dma_ch->phc = GET_DMA_PHC(control);
530 p_dma_ch->ce = GET_DMA_CE_ENABLE(control);
531 p_dma_ch->int_enable = GET_DMA_CIE_ENABLE(control);
532 p_dma_ch->shift = GET_DMA_PW(control);
534 #ifdef CONFIG_PPC4xx_EDMA
535 p_dma_ch->pf = GET_DMA_PREFETCH(control);
537 p_dma_ch->ch_enable = GET_DMA_CH(control);
538 p_dma_ch->ece_enable = GET_DMA_ECE(control);
539 p_dma_ch->tcd_disable = GET_DMA_TCD(control);
541 return DMA_STATUS_GOOD;
545 * Sets the priority for the DMA channel dmanr.
546 * Since this is setup by the hardware init function, this function
547 * can be used to dynamically change the priority of a channel.
549 * Acceptable priorities:
558 ppc4xx_set_channel_priority(unsigned int dmanr, unsigned int priority)
560 unsigned int control;
562 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
563 printk("ppc4xx_set_channel_priority: bad channel %d\n", dmanr);
564 return DMA_STATUS_BAD_CHANNEL;
567 if ((priority != PRIORITY_LOW) &&
568 (priority != PRIORITY_MID_LOW) &&
569 (priority != PRIORITY_MID_HIGH) && (priority != PRIORITY_HIGH)) {
570 printk("ppc4xx_set_channel_priority: bad priority: 0x%x\n", priority);
573 control = mfdcr(DCRN_DMACR0 + (dmanr * 0x8));
574 control |= SET_DMA_PRIORITY(priority);
575 mtdcr(DCRN_DMACR0 + (dmanr * 0x8), control);
577 return DMA_STATUS_GOOD;
581 * Returns the width of the peripheral attached to this channel. This assumes
582 * that someone who knows the hardware configuration, boot code or some other
583 * init code, already set the width.
585 * The return value is one of:
591 * The function returns 0 on error.
594 ppc4xx_get_peripheral_width(unsigned int dmanr)
596 unsigned int control;
598 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
599 printk("ppc4xx_get_peripheral_width: bad channel %d\n", dmanr);
600 return DMA_STATUS_BAD_CHANNEL;
603 control = mfdcr(DCRN_DMACR0 + (dmanr * 0x8));
605 return (GET_DMA_PW(control));
609 * Clears the channel status bits
612 ppc4xx_clr_dma_status(unsigned int dmanr)
614 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
615 printk(KERN_ERR "ppc4xx_clr_dma_status: bad channel: %d\n", dmanr);
616 return DMA_STATUS_BAD_CHANNEL;
618 mtdcr(DCRN_DMASR, ((u32)DMA_CH0_ERR | (u32)DMA_CS0 | (u32)DMA_TS0) >> dmanr);
619 return DMA_STATUS_GOOD;
622 #ifdef CONFIG_PPC4xx_EDMA
624 * Enables the burst on the channel (BTEN bit in the control/count register)
626 * For scatter/gather dma, this function MUST be called before the
627 * ppc4xx_alloc_dma_handle() func as the chan count register is copied into the
628 * sgl list and used as each sgl element is added.
631 ppc4xx_enable_burst(unsigned int dmanr)
634 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
635 printk(KERN_ERR "ppc4xx_enable_burst: bad channel: %d\n", dmanr);
636 return DMA_STATUS_BAD_CHANNEL;
638 ctc = mfdcr(DCRN_DMACT0 + (dmanr * 0x8)) | DMA_CTC_BTEN;
639 mtdcr(DCRN_DMACT0 + (dmanr * 0x8), ctc);
640 return DMA_STATUS_GOOD;
643 * Disables the burst on the channel (BTEN bit in the control/count register)
645 * For scatter/gather dma, this function MUST be called before the
646 * ppc4xx_alloc_dma_handle() func as the chan count register is copied into the
647 * sgl list and used as each sgl element is added.
650 ppc4xx_disable_burst(unsigned int dmanr)
653 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
654 printk(KERN_ERR "ppc4xx_disable_burst: bad channel: %d\n", dmanr);
655 return DMA_STATUS_BAD_CHANNEL;
657 ctc = mfdcr(DCRN_DMACT0 + (dmanr * 0x8)) &~ DMA_CTC_BTEN;
658 mtdcr(DCRN_DMACT0 + (dmanr * 0x8), ctc);
659 return DMA_STATUS_GOOD;
662 * Sets the burst size (number of peripheral widths) for the channel
663 * (BSIZ bits in the control/count register))
670 * For scatter/gather dma, this function MUST be called before the
671 * ppc4xx_alloc_dma_handle() func as the chan count register is copied into the
672 * sgl list and used as each sgl element is added.
675 ppc4xx_set_burst_size(unsigned int dmanr, unsigned int bsize)
678 if (dmanr >= MAX_PPC4xx_DMA_CHANNELS) {
679 printk(KERN_ERR "ppc4xx_set_burst_size: bad channel: %d\n", dmanr);
680 return DMA_STATUS_BAD_CHANNEL;
682 ctc = mfdcr(DCRN_DMACT0 + (dmanr * 0x8)) &~ DMA_CTC_BSIZ_MSK;
683 ctc |= (bsize & DMA_CTC_BSIZ_MSK);
684 mtdcr(DCRN_DMACT0 + (dmanr * 0x8), ctc);
685 return DMA_STATUS_GOOD;
688 EXPORT_SYMBOL(ppc4xx_enable_burst);
689 EXPORT_SYMBOL(ppc4xx_disable_burst);
690 EXPORT_SYMBOL(ppc4xx_set_burst_size);
691 #endif /* CONFIG_PPC4xx_EDMA */
693 EXPORT_SYMBOL(ppc4xx_init_dma_channel);
694 EXPORT_SYMBOL(ppc4xx_get_channel_config);
695 EXPORT_SYMBOL(ppc4xx_set_channel_priority);
696 EXPORT_SYMBOL(ppc4xx_get_peripheral_width);
697 EXPORT_SYMBOL(dma_channels);
698 EXPORT_SYMBOL(ppc4xx_set_src_addr);
699 EXPORT_SYMBOL(ppc4xx_set_dst_addr);
700 EXPORT_SYMBOL(ppc4xx_set_dma_addr);
701 EXPORT_SYMBOL(ppc4xx_set_dma_addr2);
702 EXPORT_SYMBOL(ppc4xx_enable_dma);
703 EXPORT_SYMBOL(ppc4xx_disable_dma);
704 EXPORT_SYMBOL(ppc4xx_set_dma_mode);
705 EXPORT_SYMBOL(ppc4xx_set_dma_count);
706 EXPORT_SYMBOL(ppc4xx_get_dma_residue);
707 EXPORT_SYMBOL(ppc4xx_enable_dma_interrupt);
708 EXPORT_SYMBOL(ppc4xx_disable_dma_interrupt);
709 EXPORT_SYMBOL(ppc4xx_get_dma_status);
710 EXPORT_SYMBOL(ppc4xx_clr_dma_status);