board.c 11 KB

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