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