board.c 11 KB

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