Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2004,2007,2008 Freescale Semiconductor, Inc. |
| 4 | * (C) Copyright 2002, 2003 Motorola Inc. |
| 5 | * Xianghua Xiao (X.Xiao@motorola.com) |
| 6 | * |
| 7 | * (C) Copyright 2000 |
| 8 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <config.h> |
Peter Tyser | 46f2b34 | 2009-06-30 17:15:42 -0500 | [diff] [blame] | 12 | #include <asm/io.h> |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 13 | #include <asm/fsl_dma.h> |
| 14 | |
Peter Tyser | 6ac5128 | 2009-06-30 17:15:43 -0500 | [diff] [blame] | 15 | /* Controller can only transfer 2^26 - 1 bytes at a time */ |
| 16 | #define FSL_DMA_MAX_SIZE (0x3ffffff) |
| 17 | |
Peter Tyser | 6f33a35 | 2009-06-30 17:15:51 -0500 | [diff] [blame] | 18 | #if defined(CONFIG_MPC83xx) |
| 19 | #define FSL_DMA_MR_DEFAULT (FSL_DMA_MR_CTM_DIRECT | FSL_DMA_MR_DMSEN) |
| 20 | #else |
| 21 | #define FSL_DMA_MR_DEFAULT (FSL_DMA_MR_BWC_DIS | FSL_DMA_MR_CTM_DIRECT) |
| 22 | #endif |
| 23 | |
| 24 | |
| 25 | #if defined(CONFIG_MPC83xx) |
Tom Rini | d5c3bf2 | 2022-10-28 20:27:12 -0400 | [diff] [blame] | 26 | dma83xx_t *dma_base = (void *)(CFG_SYS_MPC83xx_DMA_ADDR); |
Peter Tyser | 6f33a35 | 2009-06-30 17:15:51 -0500 | [diff] [blame] | 27 | #elif defined(CONFIG_MPC85xx) |
Tom Rini | d5c3bf2 | 2022-10-28 20:27:12 -0400 | [diff] [blame] | 28 | ccsr_dma_t *dma_base = (void *)(CFG_SYS_MPC85xx_DMA_ADDR); |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 29 | #elif defined(CONFIG_MPC86xx) |
Peter Tyser | 46f2b34 | 2009-06-30 17:15:42 -0500 | [diff] [blame] | 30 | ccsr_dma_t *dma_base = (void *)(CONFIG_SYS_MPC86xx_DMA_ADDR); |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 31 | #else |
| 32 | #error "Freescale DMA engine not supported on your processor" |
| 33 | #endif |
| 34 | |
| 35 | static void dma_sync(void) |
| 36 | { |
| 37 | #if defined(CONFIG_MPC85xx) |
| 38 | asm("sync; isync; msync"); |
| 39 | #elif defined(CONFIG_MPC86xx) |
| 40 | asm("sync; isync"); |
| 41 | #endif |
| 42 | } |
| 43 | |
Peter Tyser | 6f33a35 | 2009-06-30 17:15:51 -0500 | [diff] [blame] | 44 | static void out_dma32(volatile unsigned *addr, int val) |
| 45 | { |
| 46 | #if defined(CONFIG_MPC83xx) |
| 47 | out_le32(addr, val); |
| 48 | #else |
| 49 | out_be32(addr, val); |
| 50 | #endif |
| 51 | } |
| 52 | |
| 53 | static uint in_dma32(volatile unsigned *addr) |
| 54 | { |
| 55 | #if defined(CONFIG_MPC83xx) |
| 56 | return in_le32(addr); |
| 57 | #else |
| 58 | return in_be32(addr); |
| 59 | #endif |
| 60 | } |
| 61 | |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 62 | static uint dma_check(void) { |
| 63 | volatile fsl_dma_t *dma = &dma_base->dma[0]; |
Peter Tyser | 46f2b34 | 2009-06-30 17:15:42 -0500 | [diff] [blame] | 64 | uint status; |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 65 | |
| 66 | /* While the channel is busy, spin */ |
Peter Tyser | 46f2b34 | 2009-06-30 17:15:42 -0500 | [diff] [blame] | 67 | do { |
Peter Tyser | 6f33a35 | 2009-06-30 17:15:51 -0500 | [diff] [blame] | 68 | status = in_dma32(&dma->sr); |
Peter Tyser | 46f2b34 | 2009-06-30 17:15:42 -0500 | [diff] [blame] | 69 | } while (status & FSL_DMA_SR_CB); |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 70 | |
| 71 | /* clear MR[CS] channel start bit */ |
Peter Tyser | 6f33a35 | 2009-06-30 17:15:51 -0500 | [diff] [blame] | 72 | out_dma32(&dma->mr, in_dma32(&dma->mr) & ~FSL_DMA_MR_CS); |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 73 | dma_sync(); |
| 74 | |
| 75 | if (status != 0) |
| 76 | printf ("DMA Error: status = %x\n", status); |
| 77 | |
| 78 | return status; |
| 79 | } |
| 80 | |
Peter Tyser | 6f33a35 | 2009-06-30 17:15:51 -0500 | [diff] [blame] | 81 | #if !defined(CONFIG_MPC83xx) |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 82 | void dma_init(void) { |
| 83 | volatile fsl_dma_t *dma = &dma_base->dma[0]; |
| 84 | |
Peter Tyser | 6f33a35 | 2009-06-30 17:15:51 -0500 | [diff] [blame] | 85 | out_dma32(&dma->satr, FSL_DMA_SATR_SREAD_SNOOP); |
| 86 | out_dma32(&dma->datr, FSL_DMA_DATR_DWRITE_SNOOP); |
| 87 | out_dma32(&dma->sr, 0xffffffff); /* clear any errors */ |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 88 | dma_sync(); |
| 89 | } |
Peter Tyser | 6f33a35 | 2009-06-30 17:15:51 -0500 | [diff] [blame] | 90 | #endif |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 91 | |
Peter Tyser | 86ff89b | 2009-06-30 17:15:45 -0500 | [diff] [blame] | 92 | int dmacpy(phys_addr_t dest, phys_addr_t src, phys_size_t count) { |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 93 | volatile fsl_dma_t *dma = &dma_base->dma[0]; |
Peter Tyser | 6ac5128 | 2009-06-30 17:15:43 -0500 | [diff] [blame] | 94 | uint xfer_size; |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 95 | |
Peter Tyser | 6ac5128 | 2009-06-30 17:15:43 -0500 | [diff] [blame] | 96 | while (count) { |
Masahiro Yamada | b62b39b | 2014-09-18 13:28:06 +0900 | [diff] [blame] | 97 | xfer_size = min(FSL_DMA_MAX_SIZE, count); |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 98 | |
York Sun | 3244736 | 2010-08-27 16:25:50 -0500 | [diff] [blame] | 99 | out_dma32(&dma->dar, (u32) (dest & 0xFFFFFFFF)); |
| 100 | out_dma32(&dma->sar, (u32) (src & 0xFFFFFFFF)); |
Ira W. Snyder | 05e9444 | 2011-03-01 14:40:55 -0800 | [diff] [blame] | 101 | #if !defined(CONFIG_MPC83xx) |
York Sun | 3244736 | 2010-08-27 16:25:50 -0500 | [diff] [blame] | 102 | out_dma32(&dma->satr, |
| 103 | in_dma32(&dma->satr) | (u32)((u64)src >> 32)); |
| 104 | out_dma32(&dma->datr, |
| 105 | in_dma32(&dma->datr) | (u32)((u64)dest >> 32)); |
Ira W. Snyder | 05e9444 | 2011-03-01 14:40:55 -0800 | [diff] [blame] | 106 | #endif |
Peter Tyser | 6f33a35 | 2009-06-30 17:15:51 -0500 | [diff] [blame] | 107 | out_dma32(&dma->bcr, xfer_size); |
| 108 | dma_sync(); |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 109 | |
Peter Tyser | 6f33a35 | 2009-06-30 17:15:51 -0500 | [diff] [blame] | 110 | /* Prepare mode register */ |
| 111 | out_dma32(&dma->mr, FSL_DMA_MR_DEFAULT); |
Peter Tyser | 6ac5128 | 2009-06-30 17:15:43 -0500 | [diff] [blame] | 112 | dma_sync(); |
| 113 | |
| 114 | /* Start the transfer */ |
Peter Tyser | 6f33a35 | 2009-06-30 17:15:51 -0500 | [diff] [blame] | 115 | out_dma32(&dma->mr, FSL_DMA_MR_DEFAULT | FSL_DMA_MR_CS); |
Peter Tyser | 6ac5128 | 2009-06-30 17:15:43 -0500 | [diff] [blame] | 116 | |
| 117 | count -= xfer_size; |
| 118 | src += xfer_size; |
| 119 | dest += xfer_size; |
| 120 | |
| 121 | dma_sync(); |
| 122 | |
| 123 | if (dma_check()) |
| 124 | return -1; |
| 125 | } |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 126 | |
Peter Tyser | 6ac5128 | 2009-06-30 17:15:43 -0500 | [diff] [blame] | 127 | return 0; |
Peter Tyser | ae7a7d4 | 2009-06-30 17:15:40 -0500 | [diff] [blame] | 128 | } |
Peter Tyser | 4e928b5 | 2009-06-30 17:15:48 -0500 | [diff] [blame] | 129 | |
Peter Tyser | 6f33a35 | 2009-06-30 17:15:51 -0500 | [diff] [blame] | 130 | /* |
| 131 | * 85xx/86xx use dma to initialize SDRAM when !CONFIG_ECC_INIT_VIA_DDRCONTROLLER |
Peter Tyser | 6f33a35 | 2009-06-30 17:15:51 -0500 | [diff] [blame] | 132 | */ |
| 133 | #if ((!defined CONFIG_MPC83xx && defined(CONFIG_DDR_ECC) && \ |
Tom Rini | ddda564 | 2021-08-21 13:50:12 -0400 | [diff] [blame] | 134 | !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER))) |
Tom Rini | d73175a | 2022-12-02 16:42:35 -0500 | [diff] [blame] | 135 | void dma_meminit(uint size) |
Peter Tyser | 4e928b5 | 2009-06-30 17:15:48 -0500 | [diff] [blame] | 136 | { |
| 137 | uint *p = 0; |
| 138 | uint i = 0; |
| 139 | |
| 140 | for (*p = 0; p < (uint *)(8 * 1024); p++) { |
| 141 | if (((uint)p & 0x1f) == 0) |
| 142 | ppcDcbz((ulong)p); |
| 143 | |
Tom Rini | d73175a | 2022-12-02 16:42:35 -0500 | [diff] [blame] | 144 | *p = (uint)0xDEADBEEF; |
Peter Tyser | 4e928b5 | 2009-06-30 17:15:48 -0500 | [diff] [blame] | 145 | |
| 146 | if (((uint)p & 0x1c) == 0x1c) |
| 147 | ppcDcbf((ulong)p); |
| 148 | } |
| 149 | |
| 150 | dmacpy(0x002000, 0, 0x002000); /* 8K */ |
| 151 | dmacpy(0x004000, 0, 0x004000); /* 16K */ |
| 152 | dmacpy(0x008000, 0, 0x008000); /* 32K */ |
| 153 | dmacpy(0x010000, 0, 0x010000); /* 64K */ |
| 154 | dmacpy(0x020000, 0, 0x020000); /* 128K */ |
| 155 | dmacpy(0x040000, 0, 0x040000); /* 256K */ |
| 156 | dmacpy(0x080000, 0, 0x080000); /* 512K */ |
| 157 | dmacpy(0x100000, 0, 0x100000); /* 1M */ |
| 158 | dmacpy(0x200000, 0, 0x200000); /* 2M */ |
| 159 | dmacpy(0x400000, 0, 0x400000); /* 4M */ |
| 160 | |
| 161 | for (i = 1; i < size / 0x800000; i++) |
| 162 | dmacpy((0x800000 * i), 0, 0x800000); |
| 163 | } |
| 164 | #endif |