Macpaul Lin | 463d47f | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002-2006 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * Copyright (C) 2011 Andes Technology Corporation |
| 6 | * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com> |
| 7 | * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com> |
| 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 <common.h> |
| 29 | #include <command.h> |
| 30 | #include <malloc.h> |
| 31 | #include <stdio_dev.h> |
| 32 | #include <timestamp.h> |
| 33 | #include <version.h> |
| 34 | #include <net.h> |
| 35 | #include <serial.h> |
| 36 | #include <nand.h> |
| 37 | #include <onenand_uboot.h> |
| 38 | #include <mmc.h> |
| 39 | |
| 40 | DECLARE_GLOBAL_DATA_PTR; |
| 41 | |
| 42 | ulong monitor_flash_len; |
| 43 | |
| 44 | /* |
| 45 | * Init Utilities |
| 46 | */ |
| 47 | |
| 48 | #if !defined(CONFIG_BAUDRATE) |
| 49 | #define CONFIG_BAUDRATE 38400 |
| 50 | #endif |
| 51 | static int init_baudrate(void) |
| 52 | { |
Macpaul Lin | 569bc62 | 2011-10-24 16:43:17 +0800 | [diff] [blame] | 53 | gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE); |
Macpaul Lin | 463d47f | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * WARNING: this code looks "cleaner" than the PowerPC version, but |
| 59 | * has the disadvantage that you either get nothing, or everything. |
| 60 | * On PowerPC, you might see "DRAM: " before the system hangs - which |
| 61 | * gives a simple yet clear indication which part of the |
| 62 | * initialization if failing. |
| 63 | */ |
| 64 | static int display_dram_config(void) |
| 65 | { |
| 66 | int i; |
| 67 | |
| 68 | #ifdef DEBUG |
| 69 | puts("RAM Configuration:\n"); |
| 70 | |
| 71 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 72 | printf("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start); |
| 73 | print_size(gd->bd->bi_dram[i].size, "\n"); |
| 74 | } |
| 75 | #else |
| 76 | ulong size = 0; |
| 77 | |
| 78 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) |
| 79 | size += gd->bd->bi_dram[i].size; |
| 80 | |
| 81 | puts("DRAM: "); |
| 82 | print_size(size, "\n"); |
| 83 | #endif |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | #ifndef CONFIG_SYS_NO_FLASH |
| 89 | static void display_flash_config(ulong size) |
| 90 | { |
| 91 | puts("Flash: "); |
| 92 | print_size(size, "\n"); |
| 93 | } |
| 94 | #endif /* CONFIG_SYS_NO_FLASH */ |
| 95 | |
| 96 | #if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI) |
| 97 | #include <pci.h> |
| 98 | static int nds32_pci_init(void) |
| 99 | { |
| 100 | pci_init(); |
| 101 | return 0; |
| 102 | } |
| 103 | #endif /* CONFIG_CMD_PCI || CONFIG_PCI */ |
| 104 | |
| 105 | #if defined(CONFIG_PMU) || defined(CONFIG_PCU) |
Mike Frysinger | 11a05fb | 2012-08-06 13:46:37 +0000 | [diff] [blame] | 106 | #ifndef CONFIG_SKIP_LOWLEVEL_INIT |
Macpaul Lin | 463d47f | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 107 | static int pmu_init(void) |
| 108 | { |
| 109 | #if defined(CONFIG_FTPMU010_POWER) |
| 110 | #ifdef __NDS32_N1213_43U1H__ /* AG101: internal definition in toolchain */ |
| 111 | ftpmu010_sdram_clk_disable(CONFIG_SYS_FTPMU010_PDLLCR0_HCLKOUTDIS); |
| 112 | ftpmu010_mfpsr_select_dev(FTPMU010_MFPSR_AC97CLKSEL); |
| 113 | ftpmu010_sdramhtc_set(CONFIG_SYS_FTPMU010_SDRAMHTC); |
| 114 | #endif /* __NDS32_N1213_43U1H__ */ |
| 115 | #endif |
| 116 | return 0; |
| 117 | } |
| 118 | #endif |
Mike Frysinger | 11a05fb | 2012-08-06 13:46:37 +0000 | [diff] [blame] | 119 | #endif |
Macpaul Lin | 463d47f | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 120 | |
| 121 | /* |
| 122 | * Breathe some life into the board... |
| 123 | * |
| 124 | * Initialize a serial port as console, and carry out some hardware |
| 125 | * tests. |
| 126 | * |
| 127 | * The first part of initialization is running from Flash memory; |
| 128 | * its main purpose is to initialize the RAM so that we |
| 129 | * can relocate the monitor code to RAM. |
| 130 | */ |
| 131 | |
| 132 | /* |
| 133 | * All attempts to come up with a "common" initialization sequence |
| 134 | * that works for all boards and architectures failed: some of the |
| 135 | * requirements are just _too_ different. To get rid of the resulting |
| 136 | * mess of board dependent #ifdef'ed code we now make the whole |
| 137 | * initialization sequence configurable to the user. |
| 138 | * |
| 139 | * The requirements for any new initalization function is simple: it |
| 140 | * receives a pointer to the "global data" structure as it's only |
| 141 | * argument, and returns an integer return code, where 0 means |
| 142 | * "continue" and != 0 means "fatal error, hang the system". |
| 143 | */ |
| 144 | typedef int (init_fnc_t)(void); |
| 145 | |
| 146 | void __dram_init_banksize(void) |
| 147 | { |
| 148 | gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE; |
| 149 | gd->bd->bi_dram[0].size = gd->ram_size; |
| 150 | } |
| 151 | void dram_init_banksize(void) |
| 152 | __attribute__((weak, alias("__dram_init_banksize"))); |
| 153 | |
| 154 | init_fnc_t *init_sequence[] = { |
| 155 | #if defined(CONFIG_ARCH_CPU_INIT) |
| 156 | arch_cpu_init, /* basic arch cpu dependent setup */ |
| 157 | #endif |
| 158 | #if defined(CONFIG_PMU) || defined(CONFIG_PCU) |
| 159 | #ifndef CONFIG_SKIP_LOWLEVEL_INIT |
| 160 | pmu_init, |
| 161 | #endif |
| 162 | #endif |
| 163 | board_init, /* basic board dependent setup */ |
| 164 | #if defined(CONFIG_USE_IRQ) |
| 165 | interrupt_init, /* set up exceptions */ |
| 166 | #endif |
| 167 | timer_init, /* initialize timer */ |
| 168 | env_init, /* initialize environment */ |
| 169 | init_baudrate, /* initialze baudrate settings */ |
| 170 | serial_init, /* serial communications setup */ |
| 171 | console_init_f, /* stage 1 init of console */ |
| 172 | #if defined(CONFIG_DISPLAY_BOARDINFO) |
| 173 | checkboard, /* display board info */ |
| 174 | #endif |
| 175 | #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SOFT_I2C) |
| 176 | init_func_i2c, |
| 177 | #endif |
| 178 | dram_init, /* configure available RAM banks */ |
| 179 | display_dram_config, |
| 180 | NULL, |
| 181 | }; |
| 182 | |
| 183 | void board_init_f(ulong bootflag) |
| 184 | { |
| 185 | bd_t *bd; |
| 186 | init_fnc_t **init_fnc_ptr; |
| 187 | gd_t *id; |
| 188 | ulong addr, addr_sp; |
| 189 | |
| 190 | /* Pointer is writable since we allocated a register for it */ |
| 191 | gd = (gd_t *) ((CONFIG_SYS_INIT_SP_ADDR) & ~0x07); |
| 192 | |
| 193 | memset((void *)gd, 0, GENERATED_GBL_DATA_SIZE); |
| 194 | |
| 195 | gd->mon_len = (unsigned int)(&__bss_end__) - (unsigned int)(&_start); |
| 196 | |
| 197 | for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) { |
| 198 | if ((*init_fnc_ptr)() != 0) |
| 199 | hang(); |
| 200 | } |
| 201 | |
| 202 | debug("monitor len: %08lX\n", gd->mon_len); |
| 203 | /* |
| 204 | * Ram is setup, size stored in gd !! |
| 205 | */ |
| 206 | debug("ramsize: %08lX\n", gd->ram_size); |
| 207 | |
| 208 | addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size; |
| 209 | |
Macpaul Lin | 463d47f | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 210 | /* round down to next 4 kB limit */ |
| 211 | addr &= ~(4096 - 1); |
| 212 | debug("Top of RAM usable for U-Boot at: %08lx\n", addr); |
| 213 | |
| 214 | #ifdef CONFIG_LCD |
| 215 | #ifdef CONFIG_FB_ADDR |
| 216 | gd->fb_base = CONFIG_FB_ADDR; |
| 217 | #else |
| 218 | /* reserve memory for LCD display (always full pages) */ |
| 219 | addr = lcd_setmem(addr); |
| 220 | gd->fb_base = addr; |
| 221 | #endif /* CONFIG_FB_ADDR */ |
| 222 | #endif /* CONFIG_LCD */ |
| 223 | |
| 224 | /* |
| 225 | * reserve memory for U-Boot code, data & bss |
| 226 | * round down to next 4 kB limit |
| 227 | */ |
| 228 | addr -= gd->mon_len; |
| 229 | addr &= ~(4096 - 1); |
| 230 | |
| 231 | debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10, addr); |
| 232 | |
| 233 | /* |
| 234 | * reserve memory for malloc() arena |
| 235 | */ |
| 236 | addr_sp = addr - TOTAL_MALLOC_LEN; |
| 237 | debug("Reserving %dk for malloc() at: %08lx\n", |
| 238 | TOTAL_MALLOC_LEN >> 10, addr_sp); |
| 239 | /* |
| 240 | * (permanently) allocate a Board Info struct |
| 241 | * and a permanent copy of the "global" data |
| 242 | */ |
| 243 | addr_sp -= GENERATED_BD_INFO_SIZE; |
| 244 | bd = (bd_t *) addr_sp; |
| 245 | gd->bd = bd; |
| 246 | memset((void *)bd, 0, GENERATED_BD_INFO_SIZE); |
| 247 | debug("Reserving %zu Bytes for Board Info at: %08lx\n", |
| 248 | GENERATED_BD_INFO_SIZE, addr_sp); |
| 249 | |
| 250 | addr_sp -= GENERATED_GBL_DATA_SIZE; |
| 251 | id = (gd_t *) addr_sp; |
| 252 | debug("Reserving %zu Bytes for Global Data at: %08lx\n", |
| 253 | GENERATED_GBL_DATA_SIZE, addr_sp); |
| 254 | |
| 255 | /* setup stackpointer for exeptions */ |
| 256 | gd->irq_sp = addr_sp; |
| 257 | #ifdef CONFIG_USE_IRQ |
| 258 | addr_sp -= (CONFIG_STACKSIZE_IRQ + CONFIG_STACKSIZE_FIQ); |
| 259 | debug("Reserving %zu Bytes for IRQ stack at: %08lx\n", |
| 260 | CONFIG_STACKSIZE_IRQ+CONFIG_STACKSIZE_FIQ, addr_sp); |
| 261 | #endif |
| 262 | /* leave 3 words for abort-stack */ |
| 263 | addr_sp -= 12; |
| 264 | |
| 265 | /* 8-byte alignment for ABI compliance */ |
| 266 | addr_sp &= ~0x07; |
| 267 | debug("New Stack Pointer is: %08lx\n", addr_sp); |
| 268 | |
| 269 | gd->bd->bi_baudrate = gd->baudrate; |
| 270 | /* Ram isn't board specific, so move it to board code ... */ |
| 271 | dram_init_banksize(); |
| 272 | display_dram_config(); /* and display it */ |
| 273 | |
| 274 | gd->relocaddr = addr; |
| 275 | gd->start_addr_sp = addr_sp; |
| 276 | |
| 277 | gd->reloc_off = addr - _TEXT_BASE; |
| 278 | |
| 279 | debug("relocation Offset is: %08lx\n", gd->reloc_off); |
| 280 | memcpy(id, (void *)gd, GENERATED_GBL_DATA_SIZE); |
| 281 | |
| 282 | relocate_code(addr_sp, id, addr); |
| 283 | |
| 284 | /* NOTREACHED - relocate_code() does not return */ |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * This is the next part if the initialization sequence: we are now |
| 289 | * running from RAM and have a "normal" C environment, i. e. global |
| 290 | * data can be written, BSS has been cleared, the stack size in not |
| 291 | * that critical any more, etc. |
| 292 | */ |
| 293 | void board_init_r(gd_t *id, ulong dest_addr) |
| 294 | { |
Macpaul Lin | 463d47f | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 295 | bd_t *bd; |
| 296 | ulong malloc_start; |
| 297 | |
Macpaul Lin | 463d47f | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 298 | gd = id; |
| 299 | bd = gd->bd; |
| 300 | |
| 301 | gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */ |
| 302 | |
| 303 | monitor_flash_len = &_end - &_start; |
| 304 | debug("monitor flash len: %08lX\n", monitor_flash_len); |
| 305 | |
| 306 | board_init(); /* Setup chipselects */ |
| 307 | |
| 308 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 309 | /* |
| 310 | * We have to relocate the command table manually |
| 311 | */ |
Marek Vasut | 6c7c946 | 2012-10-12 10:27:04 +0000 | [diff] [blame] | 312 | fixup_cmdtable(ll_entry_start(cmd_tbl_t, cmd), |
| 313 | ll_entry_count(cmd_tbl_t, cmd)); |
Macpaul Lin | 463d47f | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 314 | #endif /* defined(CONFIG_NEEDS_MANUAL_RELOC) */ |
| 315 | |
Macpaul Lin | 463d47f | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 316 | serial_initialize(); |
Macpaul Lin | 463d47f | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 317 | |
| 318 | debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr); |
| 319 | |
| 320 | /* The Malloc area is immediately below the monitor copy in DRAM */ |
| 321 | malloc_start = dest_addr - TOTAL_MALLOC_LEN; |
| 322 | mem_malloc_init(malloc_start, TOTAL_MALLOC_LEN); |
| 323 | malloc_bin_reloc(); |
| 324 | |
| 325 | #ifndef CONFIG_SYS_NO_FLASH |
| 326 | /* configure available FLASH banks */ |
| 327 | gd->bd->bi_flashstart = CONFIG_SYS_FLASH_BASE; |
| 328 | gd->bd->bi_flashsize = flash_init(); |
| 329 | gd->bd->bi_flashoffset = CONFIG_SYS_FLASH_BASE + gd->bd->bi_flashsize; |
| 330 | |
| 331 | if (gd->bd->bi_flashsize) |
| 332 | display_flash_config(gd->bd->bi_flashsize); |
| 333 | #endif /* CONFIG_SYS_NO_FLASH */ |
| 334 | |
| 335 | #if defined(CONFIG_CMD_NAND) |
| 336 | puts("NAND: "); |
| 337 | nand_init(); /* go init the NAND */ |
| 338 | #endif |
| 339 | |
Macpaul Lin | a230854 | 2011-11-18 17:01:31 +0800 | [diff] [blame] | 340 | #if defined(CONFIG_CMD_IDE) |
| 341 | puts("IDE: "); |
| 342 | ide_init(); |
| 343 | #endif |
| 344 | |
Macpaul Lin | 463d47f | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 345 | #ifdef CONFIG_GENERIC_MMC |
| 346 | puts("MMC: "); |
| 347 | mmc_initialize(gd->bd); |
| 348 | #endif |
| 349 | |
| 350 | /* initialize environment */ |
| 351 | env_relocate(); |
| 352 | |
| 353 | #if defined(CONFIG_CMD_PCI) || defined(CONFIG_PCI) |
Macpaul Lin | 4ef8067 | 2011-11-25 17:14:51 +0800 | [diff] [blame] | 354 | puts("PCI: "); |
Macpaul Lin | 463d47f | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 355 | nds32_pci_init(); |
| 356 | #endif |
| 357 | |
Macpaul Lin | 463d47f | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 358 | stdio_init(); /* get the devices list going. */ |
| 359 | |
| 360 | jumptable_init(); |
| 361 | |
| 362 | #if defined(CONFIG_API) |
| 363 | /* Initialize API */ |
| 364 | api_init(); |
| 365 | #endif |
| 366 | |
| 367 | console_init_r(); /* fully init console as a device */ |
| 368 | |
| 369 | #if defined(CONFIG_ARCH_MISC_INIT) |
| 370 | /* miscellaneous arch dependent initialisations */ |
| 371 | arch_misc_init(); |
| 372 | #endif |
| 373 | #if defined(CONFIG_MISC_INIT_R) |
| 374 | /* miscellaneous platform dependent initialisations */ |
| 375 | misc_init_r(); |
| 376 | #endif |
| 377 | |
| 378 | #if defined(CONFIG_USE_IRQ) |
| 379 | /* set up exceptions */ |
| 380 | interrupt_init(); |
| 381 | /* enable exceptions */ |
| 382 | enable_interrupts(); |
| 383 | #endif |
| 384 | |
| 385 | /* Initialize from environment */ |
Macpaul Lin | 569bc62 | 2011-10-24 16:43:17 +0800 | [diff] [blame] | 386 | load_addr = getenv_ulong("loadaddr", 16, load_addr); |
Macpaul Lin | 463d47f | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 387 | |
Nobuhiro Iwamatsu | 6abcf2d | 2012-04-17 16:41:14 +0000 | [diff] [blame] | 388 | #ifdef CONFIG_BOARD_LATE_INIT |
Macpaul Lin | 463d47f | 2011-10-11 22:33:19 +0000 | [diff] [blame] | 389 | board_late_init(); |
| 390 | #endif |
| 391 | |
| 392 | #if defined(CONFIG_CMD_NET) |
| 393 | puts("Net: "); |
| 394 | |
| 395 | eth_initialize(gd->bd); |
| 396 | #if defined(CONFIG_RESET_PHY_R) |
| 397 | debug("Reset Ethernet PHY\n"); |
| 398 | reset_phy(); |
| 399 | #endif |
| 400 | #endif |
| 401 | |
| 402 | /* main_loop() can return to retry autoboot, if so just run it again. */ |
| 403 | for (;;) |
| 404 | main_loop(); |
| 405 | |
| 406 | /* NOTREACHED - no way out of command loop except booting */ |
| 407 | } |
| 408 | |
| 409 | void hang(void) |
| 410 | { |
| 411 | puts("### ERROR ### Please RESET the board ###\n"); |
| 412 | for (;;) |
| 413 | ; |
| 414 | } |