board.c 10 KB

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