Macpaul Lin | 6471556 | 2011-09-23 17:03:19 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Andes Technology Corporation |
| 3 | * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com> |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | .text |
| 25 | |
| 26 | #include <common.h> |
| 27 | #include <config.h> |
| 28 | |
| 29 | #include <asm/macro.h> |
| 30 | #include <generated/asm-offsets.h> |
| 31 | |
| 32 | /* |
| 33 | * parameters for Synopsys DWC DDR2/DDR1 Memory Controller |
| 34 | */ |
| 35 | #define DDR2C_BASE_A (CONFIG_DWCDDR21MCTL_BASE) |
| 36 | #define DDR2C_CCR_A (DDR2C_BASE_A + DWCDDR21MCTL_CCR) |
| 37 | #define DDR2C_DCR_A (DDR2C_BASE_A + DWCDDR21MCTL_DCR) |
| 38 | #define DDR2C_IOCR_A (DDR2C_BASE_A + DWCDDR21MCTL_IOCR) |
| 39 | #define DDR2C_CSR_A (DDR2C_BASE_A + DWCDDR21MCTL_CSR) |
| 40 | #define DDR2C_DRR_A (DDR2C_BASE_A + DWCDDR21MCTL_DRR) |
| 41 | #define DDR2C_DLLCR0_A (DDR2C_BASE_A + DWCDDR21MCTL_DLLCR0) |
| 42 | #define DDR2C_DLLCR1_A (DDR2C_BASE_A + DWCDDR21MCTL_DLLCR1) |
| 43 | #define DDR2C_DLLCR2_A (DDR2C_BASE_A + DWCDDR21MCTL_DLLCR2) |
| 44 | #define DDR2C_DLLCR3_A (DDR2C_BASE_A + DWCDDR21MCTL_DLLCR3) |
| 45 | #define DDR2C_DLLCR4_A (DDR2C_BASE_A + DWCDDR21MCTL_DLLCR4) |
| 46 | #define DDR2C_DLLCR5_A (DDR2C_BASE_A + DWCDDR21MCTL_DLLCR5) |
| 47 | #define DDR2C_DLLCR6_A (DDR2C_BASE_A + DWCDDR21MCTL_DLLCR6) |
| 48 | #define DDR2C_DLLCR7_A (DDR2C_BASE_A + DWCDDR21MCTL_DLLCR7) |
| 49 | #define DDR2C_DLLCR8_A (DDR2C_BASE_A + DWCDDR21MCTL_DLLCR8) |
| 50 | #define DDR2C_DLLCR9_A (DDR2C_BASE_A + DWCDDR21MCTL_DLLCR9) |
| 51 | #define DDR2C_RSLR0_A (DDR2C_BASE_A + DWCDDR21MCTL_RSLR0) |
| 52 | #define DDR2C_RDGR0_A (DDR2C_BASE_A + DWCDDR21MCTL_RDGR0) |
| 53 | #define DDR2C_DTAR_A (DDR2C_BASE_A + DWCDDR21MCTL_DTAR) |
| 54 | #define DDR2C_MR_A (DDR2C_BASE_A + DWCDDR21MCTL_MR) |
| 55 | |
| 56 | #define DDR2C_CCR_D CONFIG_SYS_DWCDDR21MCTL_CCR |
| 57 | #define DDR2C_CCR_D2 CONFIG_SYS_DWCDDR21MCTL_CCR2 |
| 58 | #define DDR2C_DCR_D CONFIG_SYS_DWCDDR21MCTL_DCR |
| 59 | #define DDR2C_IOCR_D CONFIG_SYS_DWCDDR21MCTL_IOCR |
| 60 | #define DDR2C_CSR_D CONFIG_SYS_DWCDDR21MCTL_CSR |
| 61 | #define DDR2C_DRR_D CONFIG_SYS_DWCDDR21MCTL_DRR |
| 62 | #define DDR2C_RSLR0_D CONFIG_SYS_DWCDDR21MCTL_RSLR0 |
| 63 | #define DDR2C_RDGR0_D CONFIG_SYS_DWCDDR21MCTL_RDGR0 |
| 64 | #define DDR2C_DTAR_D CONFIG_SYS_DWCDDR21MCTL_DTAR |
| 65 | #define DDR2C_MR_D CONFIG_SYS_DWCDDR21MCTL_MR |
| 66 | |
| 67 | #define DDR2C_DLLCR0_D CONFIG_SYS_DWCDDR21MCTL_DLLCR0 /* 0-9 are same */ |
| 68 | |
| 69 | /* |
| 70 | * parameters for the ahbc controller |
| 71 | */ |
| 72 | #define AHBC_CR_A (CONFIG_FTAHBC020S_BASE + FTAHBC020S_CR) |
| 73 | #define AHBC_BSR6_A (CONFIG_FTAHBC020S_BASE + FTAHBC020S_SLAVE_BSR_6) |
| 74 | |
| 75 | #define AHBC_BSR6_D CONFIG_SYS_FTAHBC020S_SLAVE_BSR_6 |
| 76 | |
| 77 | /* |
| 78 | * parameters for the ANDES PCU controller |
| 79 | */ |
| 80 | #define PCU_PCS4_A (CONFIG_ANDES_PCU_BASE + ANDES_PCU_PCS4) |
| 81 | #define PCU_PCS4_D CONFIG_SYS_ANDES_CPU_PCS4 |
| 82 | |
| 83 | /* |
| 84 | * numeric 7 segment display |
| 85 | */ |
| 86 | .macro led, num |
| 87 | write32 CONFIG_DEBUG_LED, \num |
| 88 | .endm |
| 89 | |
| 90 | /* |
| 91 | * Waiting for SDRAM to set up |
| 92 | */ |
| 93 | /* |
| 94 | .macro wait_sdram |
| 95 | li $r0, DDR2C_CSR_A |
| 96 | 1: |
| 97 | lwi $r1, [$r0+FTSDMC021_CR2] |
| 98 | bnez $r1, 1b |
| 99 | .endm |
| 100 | */ |
| 101 | |
| 102 | #ifndef CONFIG_SKIP_LOWLEVEL_INIT |
| 103 | .globl lowlevel_init |
| 104 | lowlevel_init: |
| 105 | move $r10, $lp |
| 106 | |
| 107 | /* U200 */ |
| 108 | ! led 0x00 |
| 109 | ! jal scale_to_500mhz |
| 110 | |
| 111 | led 0x10 |
| 112 | jal mem_init |
| 113 | |
| 114 | led 0x20 |
| 115 | jal remap |
| 116 | |
| 117 | led 0x30 |
| 118 | ret $r10 |
| 119 | |
| 120 | scale_to_500mhz: |
| 121 | move $r11, $lp |
| 122 | |
| 123 | /* |
| 124 | * scale to 500Mhz |
| 125 | */ |
| 126 | led 0x01 |
| 127 | write32 PCU_PCS4_A, 0x1102000f ! save data to PCS4 |
| 128 | |
| 129 | move $lp, $r11 |
| 130 | ret |
| 131 | |
| 132 | mem_init: |
| 133 | move $r11, $lp |
| 134 | |
| 135 | /* |
| 136 | * config AHB Controller |
| 137 | */ |
| 138 | led 0x12 |
| 139 | write32 AHBC_BSR6_A, AHBC_BSR6_D |
| 140 | |
| 141 | /* |
| 142 | * config Synopsys DWC DDR2/DDR1 Memory Controller |
| 143 | */ |
| 144 | ddr2c_init: |
| 145 | set_dcr: |
| 146 | led 0x14 |
| 147 | write32 DDR2C_DCR_A, DDR2C_DCR_D ! 0x000020d4 |
| 148 | |
| 149 | auto_sizing: |
| 150 | /* |
| 151 | * ebios: $r10->$r7, $r11->$r8, $r12->$r9, $r13->$r12, $r14->$r13 |
| 152 | */ |
| 153 | set_iocr: |
| 154 | led 0x19 |
| 155 | write32 DDR2C_IOCR_A, DDR2C_IOCR_D |
| 156 | set_drr: |
| 157 | led 0x16 |
| 158 | write32 DDR2C_DRR_A, DDR2C_DRR_D ! 0x00034812 |
| 159 | set_dllcr: |
| 160 | led 0x18 |
| 161 | write32 DDR2C_DLLCR0_A, DDR2C_DLLCR0_D |
| 162 | write32 DDR2C_DLLCR1_A, DDR2C_DLLCR0_D |
| 163 | write32 DDR2C_DLLCR2_A, DDR2C_DLLCR0_D |
| 164 | write32 DDR2C_DLLCR3_A, DDR2C_DLLCR0_D |
| 165 | write32 DDR2C_DLLCR4_A, DDR2C_DLLCR0_D |
| 166 | write32 DDR2C_DLLCR5_A, DDR2C_DLLCR0_D |
| 167 | write32 DDR2C_DLLCR6_A, DDR2C_DLLCR0_D |
| 168 | write32 DDR2C_DLLCR7_A, DDR2C_DLLCR0_D |
| 169 | write32 DDR2C_DLLCR8_A, DDR2C_DLLCR0_D |
| 170 | write32 DDR2C_DLLCR9_A, DDR2C_DLLCR0_D |
| 171 | set_rslr0: |
| 172 | write32 DDR2C_RSLR0_A, DDR2C_RSLR0_D ! 0x00000040 |
| 173 | set_rdgr0: |
| 174 | write32 DDR2C_RDGR0_A, DDR2C_RDGR0_D ! 0x000055cf |
| 175 | set_dtar: |
| 176 | led 0x15 |
| 177 | write32 DDR2C_DTAR_A, DDR2C_DTAR_D ! 0x00100000 |
| 178 | set_mode: |
| 179 | led 0x17 |
| 180 | write32 DDR2C_MR_A, DDR2C_MR_D ! 0x00000852 |
| 181 | set_ccr: |
| 182 | write32 DDR2C_CCR_A, DDR2C_CCR_D |
| 183 | |
| 184 | #ifdef TRIGGER_INIT: |
| 185 | trigger_init: |
| 186 | write32 DDR2C_CCR_A, DDR2C_CCR_D ! 0x80020000 |
| 187 | |
| 188 | /* Wait for ddr init state to be set */ |
| 189 | msync ALL |
| 190 | isb |
| 191 | |
| 192 | /* Wait until the config initialization is finish */ |
| 193 | 1: |
| 194 | la $r4, DDR2C_CSR_A |
| 195 | lwi $r5, [$r4] |
| 196 | srli $r5, $r5, 23 |
| 197 | bnez $r5, 1b |
| 198 | #endif |
| 199 | |
| 200 | data_training: |
| 201 | ! write32 DDR2C_CCR_A, DDR2C_CCR_D2 ! 0x40020004 |
| 202 | |
| 203 | /* Wait for ddr init state to be set */ |
| 204 | msync ALL |
| 205 | isb |
| 206 | |
| 207 | /* wait until the ddr data trainning is complete */ |
| 208 | 1: |
| 209 | la $r4, DDR2C_CSR_A |
| 210 | lwi $r5, [$r4] |
| 211 | srli $r6, $r5, 23 |
| 212 | bnez $r6, 1b |
| 213 | |
| 214 | lwi $r1, [$r4] |
| 215 | srli $r6, $r5, 20 |
| 216 | li $r5, 0x00ffffff |
| 217 | swi $r1, [$r4] |
| 218 | bnez $r6, ddr2c_init |
| 219 | |
| 220 | led 0x1a |
| 221 | move $lp, $r11 |
| 222 | ret |
| 223 | |
| 224 | remap: |
| 225 | move $r11, $lp |
| 226 | #ifdef __NDS32_N1213_43U1H__ /* NDS32 V0 ISA - AG101 Only */ |
| 227 | bal 2f |
| 228 | relo_base: |
| 229 | move $r0, $lp |
| 230 | #else |
| 231 | relo_base: |
| 232 | mfusr $r0, $pc |
| 233 | #endif /* __NDS32_N1213_43U1H__ */ |
| 234 | |
| 235 | /* |
| 236 | * Remapping |
| 237 | */ |
| 238 | #ifdef CONFIG_MEM_REMAP |
| 239 | /* |
| 240 | * Copy ROM code to SDRAM base for memory remap layout. |
| 241 | * This is not the real relocation, the real relocation is the function |
| 242 | * relocate_code() is start.S which supports the systems is memory |
| 243 | * remapped or not. |
| 244 | */ |
| 245 | /* |
| 246 | * Doing memory remap is essential for preparing some non-OS or RTOS |
| 247 | * applications. |
| 248 | * |
| 249 | * This is also a must on ADP-AG101 board. |
| 250 | * The reason is because the ROM/FLASH circuit on PCB board. |
| 251 | * AG101-A0 board has 2 jumpers MA17 and SW5 to configure which |
| 252 | * ROM/FLASH is used to boot. |
| 253 | * |
| 254 | * When SW5 = "0101", MA17 = LO, the ROM is connected to BANK0, |
| 255 | * and the FLASH is connected to BANK1. |
| 256 | * When SW5 = "1010", MA17 = HI, the ROM is disabled (still at BANK0), |
| 257 | * and the FLASH is connected to BANK0. |
| 258 | * It will occur problem when doing flash probing if the flash is at |
| 259 | * BANK0 (0x00000000) while memory remapping was skipped. |
| 260 | * |
| 261 | * Other board like ADP-AG101P may not enable this since there is only |
| 262 | * a FLASH connected to bank0. |
| 263 | */ |
| 264 | led 0x21 |
| 265 | li $r4, PHYS_SDRAM_0_AT_INIT /* 0x10000000 */ |
| 266 | li $r5, 0x0 |
| 267 | la $r1, relo_base /* get $pc or $lp */ |
| 268 | sub $r2, $r0, $r1 |
| 269 | sethi $r6, hi20(_end) |
| 270 | ori $r6, $r6, lo12(_end) |
| 271 | add $r6, $r6, $r2 |
| 272 | 1: |
| 273 | lwi.p $r7, [$r5], #4 |
| 274 | swi.p $r7, [$r4], #4 |
| 275 | blt $r5, $r6, 1b |
| 276 | |
| 277 | /* set remap bit */ |
| 278 | /* |
| 279 | * MEM remap bit is operational |
| 280 | * - use it to map writeable memory at 0x00000000, in place of flash |
| 281 | * - before remap: flash/rom 0x00000000, sdram: 0x10000000-0x4fffffff |
| 282 | * - after remap: flash/rom 0x80000000, sdram: 0x00000000 |
| 283 | */ |
| 284 | led 0x2c |
| 285 | setbf15 AHBC_CR_A, FTAHBC020S_CR_REMAP ! 0x1 |
| 286 | |
| 287 | #endif /* #ifdef CONFIG_MEM_REMAP */ |
| 288 | move $lp, $r11 |
| 289 | 2: |
| 290 | ret |
| 291 | |
| 292 | .globl show_led |
| 293 | show_led: |
| 294 | li $r8, (CONFIG_DEBUG_LED) |
| 295 | swi $r7, [$r8] |
| 296 | ret |
| 297 | #endif /* #ifndef CONFIG_SKIP_LOWLEVEL_INIT */ |