2 * arch/ppc/kernel/ppc4xx_dma.c
4 * IBM PPC4xx DMA engine core library
6 * Copyright 2000-2004 MontaVista Software Inc.
8 * Cleaned up and converted to new DCR access
9 * Matt Porter <mporter@kernel.crashing.org>
11 * Original code by Armin Kuster <akuster@mvista.com>
12 * and Pete Popov <ppopov@mvista.com>
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/config.h>
25 #include <linux/kernel.h>
27 #include <linux/miscdevice.h>
28 #include <linux/init.h>
29 #include <linux/module.h>
31 #include <asm/system.h>
33 #include <asm/ppc4xx_dma.h>
35 ppc_dma_ch_t dma_channels
[MAX_PPC4xx_DMA_CHANNELS
];
38 ppc4xx_get_dma_status(void)
40 return (mfdcr(DCRN_DMASR
));
44 ppc4xx_set_src_addr(int dmanr
, phys_addr_t src_addr
)
46 if (dmanr
>= MAX_PPC4xx_DMA_CHANNELS
) {
47 printk("set_src_addr: bad channel: %d\n", dmanr
);
51 #ifdef PPC4xx_DMA_64BIT
52 mtdcr(DCRN_DMASAH0
+ dmanr
*2, (u32
)(src_addr
>> 32));
54 mtdcr(DCRN_DMASA0
+ dmanr
*2, (u32
)src_addr
);
59 ppc4xx_set_dst_addr(int dmanr
, phys_addr_t dst_addr
)
61 if (dmanr
>= MAX_PPC4xx_DMA_CHANNELS
) {
62 printk("set_dst_addr: bad channel: %d\n", dmanr
);
66 #ifdef PPC4xx_DMA_64BIT
67 mtdcr(DCRN_DMADAH0
+ dmanr
*2, (u32
)(dst_addr
>> 32));
69 mtdcr(DCRN_DMADA0
+ dmanr
*2, (u32
)dst_addr
);
74 ppc4xx_enable_dma(unsigned int dmanr
)
77 ppc_dma_ch_t
*p_dma_ch
= &dma_channels
[dmanr
];
78 unsigned int status_bits
[] = { DMA_CS0
| DMA_TS0
| DMA_CH0_ERR
,
79 DMA_CS1
| DMA_TS1
| DMA_CH1_ERR
,
80 DMA_CS2
| DMA_TS2
| DMA_CH2_ERR
,
81 DMA_CS3
| DMA_TS3
| DMA_CH3_ERR
};
83 if (p_dma_ch
->in_use
) {
84 printk("enable_dma: channel %d in use\n", dmanr
);
88 if (dmanr
>= MAX_PPC4xx_DMA_CHANNELS
) {
89 printk("enable_dma: bad channel: %d\n", dmanr
);
93 if (p_dma_ch
->mode
== DMA_MODE_READ
) {
94 /* peripheral to memory */
95 ppc4xx_set_src_addr(dmanr
, 0);
96 ppc4xx_set_dst_addr(dmanr
, p_dma_ch
->addr
);
97 } else if (p_dma_ch
->mode
== DMA_MODE_WRITE
) {
98 /* memory to peripheral */
99 ppc4xx_set_src_addr(dmanr
, p_dma_ch
->addr
);
100 ppc4xx_set_dst_addr(dmanr
, 0);
103 /* for other xfer modes, the addresses are already set */
104 control
= mfdcr(DCRN_DMACR0
+ (dmanr
* 0x8));
106 control
&= ~(DMA_TM_MASK
| DMA_TD
); /* clear all mode bits */
107 if (p_dma_ch
->mode
== DMA_MODE_MM
) {
108 /* software initiated memory to memory */
109 control
|= DMA_ETD_OUTPUT
| DMA_TCE_ENABLE
;
112 mtdcr(DCRN_DMACR0
+ (dmanr
* 0x8), control
);
115 * Clear the CS, TS, RI bits for the channel from DMASR. This
116 * has been observed to happen correctly only after the mode and
117 * ETD/DCE bits in DMACRx are set above. Must do this before
118 * enabling the channel.
121 mtdcr(DCRN_DMASR
, status_bits
[dmanr
]);
124 * For device-paced transfers, Terminal Count Enable apparently
125 * must be on, and this must be turned on after the mode, etc.
126 * bits are cleared above (at least on Redwood-6).
129 if ((p_dma_ch
->mode
== DMA_MODE_MM_DEVATDST
) ||
130 (p_dma_ch
->mode
== DMA_MODE_MM_DEVATSRC
))
131 control
|= DMA_TCE_ENABLE
;
134 * Now enable the channel.
137 control
|= (p_dma_ch
->mode
| DMA_CE_ENABLE
);
139 mtdcr(DCRN_DMACR0
+ (dmanr
* 0x8), control
);
141 p_dma_ch
->in_use
= 1;
145 ppc4xx_disable_dma(unsigned int dmanr
)
147 unsigned int control
;
148 ppc_dma_ch_t
*p_dma_ch
= &dma_channels
[dmanr
];
150 if (!p_dma_ch
->in_use
) {
151 printk("disable_dma: channel %d not in use\n", dmanr
);
155 if (dmanr
>= MAX_PPC4xx_DMA_CHANNELS
) {
156 printk("disable_dma: bad channel: %d\n", dmanr
);
160 control
= mfdcr(DCRN_DMACR0
+ (dmanr
* 0x8));
161 control
&= ~DMA_CE_ENABLE
;
162 mtdcr(DCRN_DMACR0
+ (dmanr
* 0x8), control
);
164 p_dma_ch
->in_use
= 0;
168 * Sets the dma mode for single DMA transfers only.
169 * For scatter/gather transfers, the mode is passed to the
170 * alloc_dma_handle() function as one of the parameters.
172 * The mode is simply saved and used later. This allows
173 * the driver to call set_dma_mode() and set_dma_addr() in
176 * Valid mode values are:
178 * DMA_MODE_READ peripheral to memory
179 * DMA_MODE_WRITE memory to peripheral
180 * DMA_MODE_MM memory to memory
181 * DMA_MODE_MM_DEVATSRC device-paced memory to memory, device at src
182 * DMA_MODE_MM_DEVATDST device-paced memory to memory, device at dst
185 ppc4xx_set_dma_mode(unsigned int dmanr
, unsigned int mode
)
187 ppc_dma_ch_t
*p_dma_ch
= &dma_channels
[dmanr
];
189 if (dmanr
>= MAX_PPC4xx_DMA_CHANNELS
) {
190 printk("set_dma_mode: bad channel 0x%x\n", dmanr
);
191 return DMA_STATUS_BAD_CHANNEL
;
194 p_dma_ch
->mode
= mode
;
196 return DMA_STATUS_GOOD
;
200 * Sets the DMA Count register. Note that 'count' is in bytes.
201 * However, the DMA Count register counts the number of "transfers",
202 * where each transfer is equal to the bus width. Thus, count
203 * MUST be a multiple of the bus width.
206 ppc4xx_set_dma_count(unsigned int dmanr
, unsigned int count
)
208 ppc_dma_ch_t
*p_dma_ch
= &dma_channels
[dmanr
];
213 switch (p_dma_ch
->pwidth
) {
229 printk("set_dma_count: invalid bus width: 0x%x\n",
235 ("Warning: set_dma_count count 0x%x bus width %d\n",
236 count
, p_dma_ch
->pwidth
);
240 count
= count
>> p_dma_ch
->shift
;
242 mtdcr(DCRN_DMACT0
+ (dmanr
* 0x8), count
);
246 * Returns the number of bytes left to be transfered.
247 * After a DMA transfer, this should return zero.
248 * Reading this while a DMA transfer is still in progress will return
249 * unpredictable results.
252 ppc4xx_get_dma_residue(unsigned int dmanr
)
255 ppc_dma_ch_t
*p_dma_ch
= &dma_channels
[dmanr
];
257 if (dmanr
>= MAX_PPC4xx_DMA_CHANNELS
) {
258 printk("ppc4xx_get_dma_residue: bad channel 0x%x\n", dmanr
);
259 return DMA_STATUS_BAD_CHANNEL
;
262 count
= mfdcr(DCRN_DMACT0
+ (dmanr
* 0x8));
264 return (count
<< p_dma_ch
->shift
);
268 * Sets the DMA address for a memory to peripheral or peripheral
269 * to memory transfer. The address is just saved in the channel
270 * structure for now and used later in enable_dma().
273 ppc4xx_set_dma_addr(unsigned int dmanr
, phys_addr_t addr
)
275 ppc_dma_ch_t
*p_dma_ch
= &dma_channels
[dmanr
];
277 if (dmanr
>= MAX_PPC4xx_DMA_CHANNELS
) {
278 printk("ppc4xx_set_dma_addr: bad channel: %d\n", dmanr
);
285 switch (p_dma_ch
->pwidth
) {
289 if ((unsigned) addr
& 0x1)
293 if ((unsigned) addr
& 0x3)
297 if ((unsigned) addr
& 0x7)
301 printk("ppc4xx_set_dma_addr: invalid bus width: 0x%x\n",
306 printk("Warning: ppc4xx_set_dma_addr addr 0x%x bus width %d\n",
307 addr
, p_dma_ch
->pwidth
);
311 /* save dma address and program it later after we know the xfer mode */
312 p_dma_ch
->addr
= addr
;
316 * Sets both DMA addresses for a memory to memory transfer.
317 * For memory to peripheral or peripheral to memory transfers
318 * the function set_dma_addr() should be used instead.
321 ppc4xx_set_dma_addr2(unsigned int dmanr
, phys_addr_t src_dma_addr
,
322 phys_addr_t dst_dma_addr
)
324 if (dmanr
>= MAX_PPC4xx_DMA_CHANNELS
) {
325 printk("ppc4xx_set_dma_addr2: bad channel: %d\n", dmanr
);
331 ppc_dma_ch_t
*p_dma_ch
= &dma_channels
[dmanr
];
333 switch (p_dma_ch
->pwidth
) {
337 if (((unsigned) src_dma_addr
& 0x1) ||
338 ((unsigned) dst_dma_addr
& 0x1)
343 if (((unsigned) src_dma_addr
& 0x3) ||
344 ((unsigned) dst_dma_addr
& 0x3)
349 if (((unsigned) src_dma_addr
& 0x7) ||
350 ((unsigned) dst_dma_addr
& 0x7)
355 printk("ppc4xx_set_dma_addr2: invalid bus width: 0x%x\n",
361 ("Warning: ppc4xx_set_dma_addr2 src 0x%x dst 0x%x bus width %d\n",
362 src_dma_addr
, dst_dma_addr
, p_dma_ch
->pwidth
);
366 ppc4xx_set_src_addr(dmanr
, src_dma_addr
);
367 ppc4xx_set_dst_addr(dmanr
, dst_dma_addr
);
371 * Enables the channel interrupt.
373 * If performing a scatter/gatter transfer, this function
374 * MUST be called before calling alloc_dma_handle() and building
375 * the sgl list. Otherwise, interrupts will not be enabled, if
376 * they were previously disabled.
379 ppc4xx_enable_dma_interrupt(unsigned int dmanr
)
381 unsigned int control
;
382 ppc_dma_ch_t
*p_dma_ch
= &dma_channels
[dmanr
];
384 if (dmanr
>= MAX_PPC4xx_DMA_CHANNELS
) {
385 printk("ppc4xx_enable_dma_interrupt: bad channel: %d\n", dmanr
);
386 return DMA_STATUS_BAD_CHANNEL
;
389 p_dma_ch
->int_enable
= 1;
391 control
= mfdcr(DCRN_DMACR0
+ (dmanr
* 0x8));
392 control
|= DMA_CIE_ENABLE
; /* Channel Interrupt Enable */
393 mtdcr(DCRN_DMACR0
+ (dmanr
* 0x8), control
);
395 return DMA_STATUS_GOOD
;
399 * Disables the channel interrupt.
401 * If performing a scatter/gatter transfer, this function
402 * MUST be called before calling alloc_dma_handle() and building
403 * the sgl list. Otherwise, interrupts will not be disabled, if
404 * they were previously enabled.
407 ppc4xx_disable_dma_interrupt(unsigned int dmanr
)
409 unsigned int control
;
410 ppc_dma_ch_t
*p_dma_ch
= &dma_channels
[dmanr
];
412 if (dmanr
>= MAX_PPC4xx_DMA_CHANNELS
) {
413 printk("ppc4xx_disable_dma_interrupt: bad channel: %d\n", dmanr
);
414 return DMA_STATUS_BAD_CHANNEL
;
417 p_dma_ch
->int_enable
= 0;
419 control
= mfdcr(DCRN_DMACR0
+ (dmanr
* 0x8));
420 control
&= ~DMA_CIE_ENABLE
; /* Channel Interrupt Enable */
421 mtdcr(DCRN_DMACR0
+ (dmanr
* 0x8), control
);
423 return DMA_STATUS_GOOD
;
427 * Configures a DMA channel, including the peripheral bus width, if a
428 * peripheral is attached to the channel, the polarity of the DMAReq and
429 * DMAAck signals, etc. This information should really be setup by the boot
430 * code, since most likely the configuration won't change dynamically.
431 * If the kernel has to call this function, it's recommended that it's
432 * called from platform specific init code. The driver should not need to
433 * call this function.
436 ppc4xx_init_dma_channel(unsigned int dmanr
, ppc_dma_ch_t
* p_init
)
438 unsigned int polarity
;
439 uint32_t control
= 0;
440 ppc_dma_ch_t
*p_dma_ch
= &dma_channels
[dmanr
];
442 DMA_MODE_READ
= (unsigned long) DMA_TD
; /* Peripheral to Memory */
443 DMA_MODE_WRITE
= 0; /* Memory to Peripheral */
446 printk("ppc4xx_init_dma_channel: NULL p_init\n");
447 return DMA_STATUS_NULL_POINTER
;
450 if (dmanr
>= MAX_PPC4xx_DMA_CHANNELS
) {
451 printk("ppc4xx_init_dma_channel: bad channel %d\n", dmanr
);
452 return DMA_STATUS_BAD_CHANNEL
;
456 polarity
= mfdcr(DCRN_POL
);
461 /* Setup the control register based on the values passed to
462 * us in p_init. Then, over-write the control register with this
465 control
|= SET_DMA_CONTROL
;
467 /* clear all polarity signals and then "or" in new signal levels */
468 polarity
&= ~GET_DMA_POLARITY(dmanr
);
469 polarity
|= p_init
->polarity
;
471 mtdcr(DCRN_POL
, polarity
);
473 mtdcr(DCRN_DMACR0
+ (dmanr
* 0x8), control
);
475 /* save these values in our dma channel structure */
476 memcpy(p_dma_ch
, p_init
, sizeof (ppc_dma_ch_t
));
479 * The peripheral width values written in the control register are:
485 * Since the DMA count register takes the number of "transfers",
486 * we need to divide the count sent to us in certain
487 * functions by the appropriate number. It so happens that our
488 * right shift value is equal to the peripheral width value.
490 p_dma_ch
->shift
= p_init
->pwidth
;
493 * Save the control word for easy access.
495 p_dma_ch
->control
= control
;
497 mtdcr(DCRN_DMASR
, 0xffffffff); /* clear status register */
498 return DMA_STATUS_GOOD
;
502 * This function returns the channel configuration.
505 ppc4xx_get_channel_config(unsigned int dmanr
, ppc_dma_ch_t
* p_dma_ch
)
507 unsigned int polarity
;
508 unsigned int control
;
510 if (dmanr
>= MAX_PPC4xx_DMA_CHANNELS
) {
511 printk("ppc4xx_get_channel_config: bad channel %d\n", dmanr
);
512 return DMA_STATUS_BAD_CHANNEL
;
515 memcpy(p_dma_ch
, &dma_channels
[dmanr
], sizeof (ppc_dma_ch_t
));
518 polarity
= mfdcr(DCRN_POL
);
523 p_dma_ch
->polarity
= polarity
& GET_DMA_POLARITY(dmanr
);
524 control
= mfdcr(DCRN_DMACR0
+ (dmanr
* 0x8));
526 p_dma_ch
->cp
= GET_DMA_PRIORITY(control
);
527 p_dma_ch
->pwidth
= GET_DMA_PW(control
);
528 p_dma_ch
->psc
= GET_DMA_PSC(control
);
529 p_dma_ch
->pwc
= GET_DMA_PWC(control
);
530 p_dma_ch
->phc
= GET_DMA_PHC(control
);
531 p_dma_ch
->ce
= GET_DMA_CE_ENABLE(control
);
532 p_dma_ch
->int_enable
= GET_DMA_CIE_ENABLE(control
);
533 p_dma_ch
->shift
= GET_DMA_PW(control
);
535 #ifdef CONFIG_PPC4xx_EDMA
536 p_dma_ch
->pf
= GET_DMA_PREFETCH(control
);
538 p_dma_ch
->ch_enable
= GET_DMA_CH(control
);
539 p_dma_ch
->ece_enable
= GET_DMA_ECE(control
);
540 p_dma_ch
->tcd_disable
= GET_DMA_TCD(control
);
542 return DMA_STATUS_GOOD
;
546 * Sets the priority for the DMA channel dmanr.
547 * Since this is setup by the hardware init function, this function
548 * can be used to dynamically change the priority of a channel.
550 * Acceptable priorities:
559 ppc4xx_set_channel_priority(unsigned int dmanr
, unsigned int priority
)
561 unsigned int control
;
563 if (dmanr
>= MAX_PPC4xx_DMA_CHANNELS
) {
564 printk("ppc4xx_set_channel_priority: bad channel %d\n", dmanr
);
565 return DMA_STATUS_BAD_CHANNEL
;
568 if ((priority
!= PRIORITY_LOW
) &&
569 (priority
!= PRIORITY_MID_LOW
) &&
570 (priority
!= PRIORITY_MID_HIGH
) && (priority
!= PRIORITY_HIGH
)) {
571 printk("ppc4xx_set_channel_priority: bad priority: 0x%x\n", priority
);
574 control
= mfdcr(DCRN_DMACR0
+ (dmanr
* 0x8));
575 control
|= SET_DMA_PRIORITY(priority
);
576 mtdcr(DCRN_DMACR0
+ (dmanr
* 0x8), control
);
578 return DMA_STATUS_GOOD
;
582 * Returns the width of the peripheral attached to this channel. This assumes
583 * that someone who knows the hardware configuration, boot code or some other
584 * init code, already set the width.
586 * The return value is one of:
592 * The function returns 0 on error.
595 ppc4xx_get_peripheral_width(unsigned int dmanr
)
597 unsigned int control
;
599 if (dmanr
>= MAX_PPC4xx_DMA_CHANNELS
) {
600 printk("ppc4xx_get_peripheral_width: bad channel %d\n", dmanr
);
601 return DMA_STATUS_BAD_CHANNEL
;
604 control
= mfdcr(DCRN_DMACR0
+ (dmanr
* 0x8));
606 return (GET_DMA_PW(control
));
610 * Clears the channel status bits
613 ppc4xx_clr_dma_status(unsigned int dmanr
)
615 if (dmanr
>= MAX_PPC4xx_DMA_CHANNELS
) {
616 printk(KERN_ERR
"ppc4xx_clr_dma_status: bad channel: %d\n", dmanr
);
617 return DMA_STATUS_BAD_CHANNEL
;
619 mtdcr(DCRN_DMASR
, ((u32
)DMA_CH0_ERR
| (u32
)DMA_CS0
| (u32
)DMA_TS0
) >> dmanr
);
620 return DMA_STATUS_GOOD
;
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_init_dma_channel
);
689 EXPORT_SYMBOL(ppc4xx_get_channel_config
);
690 EXPORT_SYMBOL(ppc4xx_set_channel_priority
);
691 EXPORT_SYMBOL(ppc4xx_get_peripheral_width
);
692 EXPORT_SYMBOL(dma_channels
);
693 EXPORT_SYMBOL(ppc4xx_set_src_addr
);
694 EXPORT_SYMBOL(ppc4xx_set_dst_addr
);
695 EXPORT_SYMBOL(ppc4xx_set_dma_addr
);
696 EXPORT_SYMBOL(ppc4xx_set_dma_addr2
);
697 EXPORT_SYMBOL(ppc4xx_enable_dma
);
698 EXPORT_SYMBOL(ppc4xx_disable_dma
);
699 EXPORT_SYMBOL(ppc4xx_set_dma_mode
);
700 EXPORT_SYMBOL(ppc4xx_set_dma_count
);
701 EXPORT_SYMBOL(ppc4xx_get_dma_residue
);
702 EXPORT_SYMBOL(ppc4xx_enable_dma_interrupt
);
703 EXPORT_SYMBOL(ppc4xx_disable_dma_interrupt
);
704 EXPORT_SYMBOL(ppc4xx_get_dma_status
);
705 EXPORT_SYMBOL(ppc4xx_clr_dma_status
);
706 EXPORT_SYMBOL(ppc4xx_enable_burst
);
707 EXPORT_SYMBOL(ppc4xx_disable_burst
);
708 EXPORT_SYMBOL(ppc4xx_set_burst_size
);