Peter Tyser | 017f11f | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004,2007,2008 Freescale Semiconductor, Inc. |
| 3 | * (C) Copyright 2002, 2003 Motorola Inc. |
| 4 | * Xianghua Xiao (X.Xiao@motorola.com) |
| 5 | * |
| 6 | * (C) Copyright 2000 |
| 7 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 8 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 25 | * MA 02111-1307 USA |
| 26 | */ |
| 27 | |
| 28 | #include <config.h> |
| 29 | #include <common.h> |
Peter Tyser | a730393 | 2009-06-30 17:15:42 -0500 | [diff] [blame] | 30 | #include <asm/io.h> |
Peter Tyser | 017f11f | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 31 | #include <asm/fsl_dma.h> |
| 32 | |
Peter Tyser | 51402ac | 2009-06-30 17:15:43 -0500 | [diff] [blame] | 33 | /* Controller can only transfer 2^26 - 1 bytes at a time */ |
| 34 | #define FSL_DMA_MAX_SIZE (0x3ffffff) |
| 35 | |
Peter Tyser | 017f11f | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 36 | #if defined(CONFIG_MPC85xx) |
Peter Tyser | a730393 | 2009-06-30 17:15:42 -0500 | [diff] [blame] | 37 | ccsr_dma_t *dma_base = (void *)(CONFIG_SYS_MPC85xx_DMA_ADDR); |
Peter Tyser | 017f11f | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 38 | #elif defined(CONFIG_MPC86xx) |
Peter Tyser | a730393 | 2009-06-30 17:15:42 -0500 | [diff] [blame] | 39 | ccsr_dma_t *dma_base = (void *)(CONFIG_SYS_MPC86xx_DMA_ADDR); |
Peter Tyser | 017f11f | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 40 | #else |
| 41 | #error "Freescale DMA engine not supported on your processor" |
| 42 | #endif |
| 43 | |
| 44 | static void dma_sync(void) |
| 45 | { |
| 46 | #if defined(CONFIG_MPC85xx) |
| 47 | asm("sync; isync; msync"); |
| 48 | #elif defined(CONFIG_MPC86xx) |
| 49 | asm("sync; isync"); |
| 50 | #endif |
| 51 | } |
| 52 | |
| 53 | static uint dma_check(void) { |
| 54 | volatile fsl_dma_t *dma = &dma_base->dma[0]; |
Peter Tyser | a730393 | 2009-06-30 17:15:42 -0500 | [diff] [blame] | 55 | uint status; |
Peter Tyser | 017f11f | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 56 | |
| 57 | /* While the channel is busy, spin */ |
Peter Tyser | a730393 | 2009-06-30 17:15:42 -0500 | [diff] [blame] | 58 | do { |
| 59 | status = in_be32(&dma->sr); |
| 60 | } while (status & FSL_DMA_SR_CB); |
Peter Tyser | 017f11f | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 61 | |
| 62 | /* clear MR[CS] channel start bit */ |
Peter Tyser | 484919c | 2009-06-30 17:15:44 -0500 | [diff] [blame] | 63 | out_be32(&dma->mr, in_be32(&dma->mr) & ~FSL_DMA_MR_CS); |
Peter Tyser | 017f11f | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 64 | dma_sync(); |
| 65 | |
| 66 | if (status != 0) |
| 67 | printf ("DMA Error: status = %x\n", status); |
| 68 | |
| 69 | return status; |
| 70 | } |
| 71 | |
| 72 | void dma_init(void) { |
| 73 | volatile fsl_dma_t *dma = &dma_base->dma[0]; |
| 74 | |
Peter Tyser | a730393 | 2009-06-30 17:15:42 -0500 | [diff] [blame] | 75 | out_be32(&dma->satr, FSL_DMA_SATR_SREAD_NO_SNOOP); |
| 76 | out_be32(&dma->datr, FSL_DMA_DATR_DWRITE_NO_SNOOP); |
| 77 | out_be32(&dma->sr, 0xffffffff); /* clear any errors */ |
Peter Tyser | 017f11f | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 78 | dma_sync(); |
| 79 | } |
| 80 | |
Peter Tyser | 7892f61 | 2009-06-30 17:15:45 -0500 | [diff] [blame^] | 81 | int dmacpy(phys_addr_t dest, phys_addr_t src, phys_size_t count) { |
Peter Tyser | 017f11f | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 82 | volatile fsl_dma_t *dma = &dma_base->dma[0]; |
Peter Tyser | 51402ac | 2009-06-30 17:15:43 -0500 | [diff] [blame] | 83 | uint xfer_size; |
Peter Tyser | 017f11f | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 84 | |
Peter Tyser | 51402ac | 2009-06-30 17:15:43 -0500 | [diff] [blame] | 85 | while (count) { |
| 86 | xfer_size = MIN(FSL_DMA_MAX_SIZE, count); |
Peter Tyser | 017f11f | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 87 | |
Peter Tyser | 51402ac | 2009-06-30 17:15:43 -0500 | [diff] [blame] | 88 | out_be32(&dma->dar, (uint) dest); |
| 89 | out_be32(&dma->sar, (uint) src); |
| 90 | out_be32(&dma->bcr, xfer_size); |
Peter Tyser | 017f11f | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 91 | |
Peter Tyser | 51402ac | 2009-06-30 17:15:43 -0500 | [diff] [blame] | 92 | /* Disable bandwidth control, use direct transfer mode */ |
| 93 | out_be32(&dma->mr, FSL_DMA_MR_BWC_DIS | FSL_DMA_MR_CTM_DIRECT); |
| 94 | dma_sync(); |
Peter Tyser | 017f11f | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 95 | |
Peter Tyser | 51402ac | 2009-06-30 17:15:43 -0500 | [diff] [blame] | 96 | /* Start the transfer */ |
| 97 | out_be32(&dma->mr, FSL_DMA_MR_BWC_DIS | |
| 98 | FSL_DMA_MR_CTM_DIRECT | |
| 99 | FSL_DMA_MR_CS); |
| 100 | |
| 101 | count -= xfer_size; |
| 102 | src += xfer_size; |
| 103 | dest += xfer_size; |
| 104 | |
| 105 | dma_sync(); |
| 106 | |
| 107 | if (dma_check()) |
| 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | return 0; |
Peter Tyser | 017f11f | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 112 | } |