board.c 9.6 KB

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