board.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * (C) Copyright 2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  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. #include <common.h>
  24. #include <command.h>
  25. #include <malloc.h>
  26. #include <stdio_dev.h>
  27. #include <timestamp.h>
  28. #include <version.h>
  29. #include <net.h>
  30. #include <environment.h>
  31. #include <nand.h>
  32. #include <onenand_uboot.h>
  33. #include <spi.h>
  34. DECLARE_GLOBAL_DATA_PTR;
  35. #if ( ((CONFIG_ENV_ADDR+CONFIG_ENV_SIZE) < CONFIG_SYS_MONITOR_BASE) || \
  36. (CONFIG_ENV_ADDR >= (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN)) ) || \
  37. defined(CONFIG_ENV_IS_IN_NVRAM)
  38. #define TOTAL_MALLOC_LEN (CONFIG_SYS_MALLOC_LEN + CONFIG_ENV_SIZE)
  39. #else
  40. #define TOTAL_MALLOC_LEN CONFIG_SYS_MALLOC_LEN
  41. #endif
  42. #undef DEBUG
  43. extern int timer_init(void);
  44. extern int incaip_set_cpuclk(void);
  45. extern ulong uboot_end_data;
  46. extern ulong uboot_end;
  47. ulong monitor_flash_len;
  48. const char version_string[] =
  49. U_BOOT_VERSION" (" U_BOOT_DATE " - " U_BOOT_TIME ")";
  50. static char *failed = "*** failed ***\n";
  51. /*
  52. * mips_io_port_base is the begin of the address space to which x86 style
  53. * I/O ports are mapped.
  54. */
  55. unsigned long mips_io_port_base = -1;
  56. int __board_early_init_f(void)
  57. {
  58. /*
  59. * Nothing to do in this dummy implementation
  60. */
  61. return 0;
  62. }
  63. int board_early_init_f(void) __attribute__((weak, alias("__board_early_init_f")));
  64. static int init_func_ram (void)
  65. {
  66. #ifdef CONFIG_BOARD_TYPES
  67. int board_type = gd->board_type;
  68. #else
  69. int board_type = 0; /* use dummy arg */
  70. #endif
  71. puts ("DRAM: ");
  72. if ((gd->ram_size = initdram (board_type)) > 0) {
  73. print_size (gd->ram_size, "\n");
  74. return (0);
  75. }
  76. puts (failed);
  77. return (1);
  78. }
  79. static int display_banner(void)
  80. {
  81. printf ("\n\n%s\n\n", version_string);
  82. return (0);
  83. }
  84. #ifndef CONFIG_SYS_NO_FLASH
  85. static void display_flash_config(ulong size)
  86. {
  87. puts ("Flash: ");
  88. print_size (size, "\n");
  89. }
  90. #endif
  91. static int init_baudrate (void)
  92. {
  93. char tmp[64]; /* long enough for environment variables */
  94. int i = getenv_r ("baudrate", tmp, sizeof (tmp));
  95. gd->baudrate = (i > 0)
  96. ? (int) simple_strtoul (tmp, NULL, 10)
  97. : CONFIG_BAUDRATE;
  98. return (0);
  99. }
  100. /*
  101. * Breath some life into the board...
  102. *
  103. * The first part of initialization is running from Flash memory;
  104. * its main purpose is to initialize the RAM so that we
  105. * can relocate the monitor code to RAM.
  106. */
  107. /*
  108. * All attempts to come up with a "common" initialization sequence
  109. * that works for all boards and architectures failed: some of the
  110. * requirements are just _too_ different. To get rid of the resulting
  111. * mess of board dependend #ifdef'ed code we now make the whole
  112. * initialization sequence configurable to the user.
  113. *
  114. * The requirements for any new initalization function is simple: it
  115. * receives a pointer to the "global data" structure as it's only
  116. * argument, and returns an integer return code, where 0 means
  117. * "continue" and != 0 means "fatal error, hang the system".
  118. */
  119. typedef int (init_fnc_t) (void);
  120. init_fnc_t *init_sequence[] = {
  121. board_early_init_f,
  122. timer_init,
  123. env_init, /* initialize environment */
  124. #ifdef CONFIG_INCA_IP
  125. incaip_set_cpuclk, /* set cpu clock according to environment variable */
  126. #endif
  127. init_baudrate, /* initialze baudrate settings */
  128. serial_init, /* serial communications setup */
  129. console_init_f,
  130. display_banner, /* say that we are here */
  131. checkboard,
  132. init_func_ram,
  133. NULL,
  134. };
  135. void board_init_f(ulong bootflag)
  136. {
  137. gd_t gd_data, *id;
  138. bd_t *bd;
  139. init_fnc_t **init_fnc_ptr;
  140. ulong addr, addr_sp, len = (ulong)&uboot_end - CONFIG_SYS_MONITOR_BASE;
  141. ulong *s;
  142. #ifdef CONFIG_PURPLE
  143. void copy_code (ulong);
  144. #endif
  145. /* Pointer is writable since we allocated a register for it.
  146. */
  147. gd = &gd_data;
  148. /* compiler optimization barrier needed for GCC >= 3.4 */
  149. __asm__ __volatile__("": : :"memory");
  150. memset ((void *)gd, 0, sizeof (gd_t));
  151. for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
  152. if ((*init_fnc_ptr)() != 0) {
  153. hang ();
  154. }
  155. }
  156. /*
  157. * Now that we have DRAM mapped and working, we can
  158. * relocate the code and continue running from DRAM.
  159. */
  160. addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
  161. /* We can reserve some RAM "on top" here.
  162. */
  163. /* round down to next 4 kB limit.
  164. */
  165. addr &= ~(4096 - 1);
  166. debug ("Top of RAM usable for U-Boot at: %08lx\n", addr);
  167. /* Reserve memory for U-Boot code, data & bss
  168. * round down to next 16 kB limit
  169. */
  170. addr -= len;
  171. addr &= ~(16 * 1024 - 1);
  172. debug ("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
  173. /* Reserve memory for malloc() arena.
  174. */
  175. addr_sp = addr - TOTAL_MALLOC_LEN;
  176. debug ("Reserving %dk for malloc() at: %08lx\n",
  177. TOTAL_MALLOC_LEN >> 10, addr_sp);
  178. /*
  179. * (permanently) allocate a Board Info struct
  180. * and a permanent copy of the "global" data
  181. */
  182. addr_sp -= sizeof(bd_t);
  183. bd = (bd_t *)addr_sp;
  184. gd->bd = bd;
  185. debug ("Reserving %zu Bytes for Board Info at: %08lx\n",
  186. sizeof(bd_t), addr_sp);
  187. addr_sp -= sizeof(gd_t);
  188. id = (gd_t *)addr_sp;
  189. debug ("Reserving %zu Bytes for Global Data at: %08lx\n",
  190. sizeof (gd_t), addr_sp);
  191. /* Reserve memory for boot params.
  192. */
  193. addr_sp -= CONFIG_SYS_BOOTPARAMS_LEN;
  194. bd->bi_boot_params = addr_sp;
  195. debug ("Reserving %dk for boot params() at: %08lx\n",
  196. CONFIG_SYS_BOOTPARAMS_LEN >> 10, addr_sp);
  197. /*
  198. * Finally, we set up a new (bigger) stack.
  199. *
  200. * Leave some safety gap for SP, force alignment on 16 byte boundary
  201. * Clear initial stack frame
  202. */
  203. addr_sp -= 16;
  204. addr_sp &= ~0xF;
  205. s = (ulong *)addr_sp;
  206. *s-- = 0;
  207. *s-- = 0;
  208. addr_sp = (ulong)s;
  209. debug ("Stack Pointer at: %08lx\n", addr_sp);
  210. /*
  211. * Save local variables to board info struct
  212. */
  213. bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of DRAM memory */
  214. bd->bi_memsize = gd->ram_size; /* size of DRAM memory in bytes */
  215. bd->bi_baudrate = gd->baudrate; /* Console Baudrate */
  216. memcpy (id, (void *)gd, sizeof (gd_t));
  217. /* On the purple board we copy the code in a special way
  218. * in order to solve flash problems
  219. */
  220. #ifdef CONFIG_PURPLE
  221. copy_code(addr);
  222. #endif
  223. relocate_code (addr_sp, id, addr);
  224. /* NOTREACHED - relocate_code() does not return */
  225. }
  226. /************************************************************************
  227. *
  228. * This is the next part if the initialization sequence: we are now
  229. * running from RAM and have a "normal" C environment, i. e. global
  230. * data can be written, BSS has been cleared, the stack size in not
  231. * that critical any more, etc.
  232. *
  233. ************************************************************************
  234. */
  235. void board_init_r (gd_t *id, ulong dest_addr)
  236. {
  237. cmd_tbl_t *cmdtp;
  238. #ifndef CONFIG_SYS_NO_FLASH
  239. ulong size;
  240. #endif
  241. extern void malloc_bin_reloc (void);
  242. #ifndef CONFIG_ENV_IS_NOWHERE
  243. extern char * env_name_spec;
  244. #endif
  245. char *s;
  246. bd_t *bd;
  247. gd = id;
  248. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  249. debug ("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
  250. gd->reloc_off = dest_addr - CONFIG_SYS_MONITOR_BASE;
  251. monitor_flash_len = (ulong)&uboot_end_data - dest_addr;
  252. /*
  253. * We have to relocate the command table manually
  254. */
  255. for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
  256. ulong addr;
  257. addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
  258. #if 0
  259. printf ("Command \"%s\": 0x%08lx => 0x%08lx\n",
  260. cmdtp->name, (ulong) (cmdtp->cmd), addr);
  261. #endif
  262. cmdtp->cmd =
  263. (int (*)(struct cmd_tbl_s *, int, int, char *[]))addr;
  264. addr = (ulong)(cmdtp->name) + gd->reloc_off;
  265. cmdtp->name = (char *)addr;
  266. if (cmdtp->usage) {
  267. addr = (ulong)(cmdtp->usage) + gd->reloc_off;
  268. cmdtp->usage = (char *)addr;
  269. }
  270. #ifdef CONFIG_SYS_LONGHELP
  271. if (cmdtp->help) {
  272. addr = (ulong)(cmdtp->help) + gd->reloc_off;
  273. cmdtp->help = (char *)addr;
  274. }
  275. #endif
  276. }
  277. /* there are some other pointer constants we must deal with */
  278. #ifndef CONFIG_ENV_IS_NOWHERE
  279. env_name_spec += gd->reloc_off;
  280. #endif
  281. bd = gd->bd;
  282. /* The Malloc area is immediately below the monitor copy in DRAM */
  283. mem_malloc_init(CONFIG_SYS_MONITOR_BASE + gd->reloc_off -
  284. TOTAL_MALLOC_LEN, TOTAL_MALLOC_LEN);
  285. malloc_bin_reloc();
  286. #ifndef CONFIG_SYS_NO_FLASH
  287. /* configure available FLASH banks */
  288. size = flash_init();
  289. display_flash_config (size);
  290. bd->bi_flashsize = size;
  291. #endif
  292. bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
  293. #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
  294. bd->bi_flashoffset = monitor_flash_len; /* reserved area for U-Boot */
  295. #else
  296. bd->bi_flashoffset = 0;
  297. #endif
  298. #ifdef CONFIG_CMD_NAND
  299. puts ("NAND: ");
  300. nand_init (); /* go init the NAND */
  301. #endif
  302. #if defined(CONFIG_CMD_ONENAND)
  303. onenand_init();
  304. #endif
  305. /* relocate environment function pointers etc. */
  306. env_relocate();
  307. /* IP Address */
  308. bd->bi_ip_addr = getenv_IPaddr("ipaddr");
  309. #if defined(CONFIG_PCI)
  310. /*
  311. * Do pci configuration
  312. */
  313. pci_init();
  314. #endif
  315. /** leave this here (after malloc(), environment and PCI are working) **/
  316. /* Initialize stdio devices */
  317. stdio_init ();
  318. jumptable_init ();
  319. /* Initialize the console (after the relocation and devices init) */
  320. console_init_r ();
  321. /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
  322. /* Initialize from environment */
  323. if ((s = getenv ("loadaddr")) != NULL) {
  324. load_addr = simple_strtoul (s, NULL, 16);
  325. }
  326. #if defined(CONFIG_CMD_NET)
  327. if ((s = getenv ("bootfile")) != NULL) {
  328. copy_filename (BootFile, s, sizeof (BootFile));
  329. }
  330. #endif
  331. #ifdef CONFIG_CMD_SPI
  332. puts ("SPI: ");
  333. spi_init (); /* go init the SPI */
  334. puts ("ready\n");
  335. #endif
  336. #if defined(CONFIG_MISC_INIT_R)
  337. /* miscellaneous platform dependent initialisations */
  338. misc_init_r ();
  339. #endif
  340. #if defined(CONFIG_CMD_NET)
  341. #if defined(CONFIG_NET_MULTI)
  342. puts ("Net: ");
  343. #endif
  344. eth_initialize(gd->bd);
  345. #endif
  346. /* main_loop() can return to retry autoboot, if so just run it again. */
  347. for (;;) {
  348. main_loop ();
  349. }
  350. /* NOTREACHED - no way out of command loop except booting */
  351. }
  352. void hang (void)
  353. {
  354. puts ("### ERROR ### Please RESET the board ###\n");
  355. for (;;);
  356. }