board.c 11 KB

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