board.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * (C) Copyright 2002
  3. * Daniel Engström, Omicron Ceti AB, daniel@omicron.se
  4. *
  5. * (C) Copyright 2002
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * (C) Copyright 2002
  9. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  10. * Marius Groeger <mgroeger@sysgo.de>
  11. *
  12. * See file CREDITS for list of people who contributed to this
  13. * project.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of
  18. * the License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  28. * MA 02111-1307 USA
  29. */
  30. #include <common.h>
  31. #include <watchdog.h>
  32. #include <command.h>
  33. #include <stdio_dev.h>
  34. #include <timestamp.h>
  35. #include <version.h>
  36. #include <malloc.h>
  37. #include <net.h>
  38. #include <ide.h>
  39. #include <serial.h>
  40. #include <asm/u-boot-i386.h>
  41. #include <elf.h>
  42. #ifdef CONFIG_BITBANGMII
  43. #include <miiphy.h>
  44. #endif
  45. /*
  46. * Pointer to initial global data area
  47. *
  48. * Here we initialize it.
  49. */
  50. #undef XTRN_DECLARE_GLOBAL_DATA_PTR
  51. #define XTRN_DECLARE_GLOBAL_DATA_PTR /* empty = allocate here */
  52. DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_GD_ADDR);
  53. /* Exports from the Linker Script */
  54. extern ulong __text_start;
  55. extern ulong __data_end;
  56. extern ulong __rel_dyn_start;
  57. extern ulong __rel_dyn_end;
  58. extern ulong __bss_start;
  59. extern ulong __bss_end;
  60. const char version_string[] =
  61. U_BOOT_VERSION" (" U_BOOT_DATE " - " U_BOOT_TIME ")";
  62. /************************************************************************
  63. * Init Utilities *
  64. ************************************************************************
  65. * Some of this code should be moved into the core functions,
  66. * or dropped completely,
  67. * but let's get it working (again) first...
  68. */
  69. static int init_baudrate (void)
  70. {
  71. char tmp[64]; /* long enough for environment variables */
  72. int i = getenv_f("baudrate", tmp, 64);
  73. gd->baudrate = (i != 0)
  74. ? (int) simple_strtoul (tmp, NULL, 10)
  75. : CONFIG_BAUDRATE;
  76. return (0);
  77. }
  78. static int display_banner (void)
  79. {
  80. printf ("\n\n%s\n\n", version_string);
  81. /*
  82. printf ("U-Boot code: %08lX -> %08lX data: %08lX -> %08lX\n"
  83. " BSS: %08lX -> %08lX stack: %08lX -> %08lX\n",
  84. i386boot_start, i386boot_romdata_start-1,
  85. i386boot_romdata_dest, i386boot_romdata_dest+i386boot_romdata_size-1,
  86. i386boot_bss_start, i386boot_bss_start+i386boot_bss_size-1,
  87. i386boot_bss_start+i386boot_bss_size,
  88. i386boot_bss_start+i386boot_bss_size+CONFIG_SYS_STACK_SIZE-1);
  89. */
  90. return (0);
  91. }
  92. /*
  93. * WARNING: this code looks "cleaner" than the PowerPC version, but
  94. * has the disadvantage that you either get nothing, or everything.
  95. * On PowerPC, you might see "DRAM: " before the system hangs - which
  96. * gives a simple yet clear indication which part of the
  97. * initialization if failing.
  98. */
  99. static int display_dram_config (void)
  100. {
  101. int i;
  102. puts ("DRAM Configuration:\n");
  103. for (i=0; i<CONFIG_NR_DRAM_BANKS; i++) {
  104. printf ("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
  105. print_size (gd->bd->bi_dram[i].size, "\n");
  106. }
  107. return (0);
  108. }
  109. static void display_flash_config (ulong size)
  110. {
  111. puts ("Flash: ");
  112. print_size (size, "\n");
  113. }
  114. /*
  115. * Breath some life into the board...
  116. *
  117. * Initialize an SMC for serial comms, and carry out some hardware
  118. * tests.
  119. *
  120. * The first part of initialization is running from Flash memory;
  121. * its main purpose is to initialize the RAM so that we
  122. * can relocate the monitor code to RAM.
  123. */
  124. /*
  125. * All attempts to come up with a "common" initialization sequence
  126. * that works for all boards and architectures failed: some of the
  127. * requirements are just _too_ different. To get rid of the resulting
  128. * mess of board dependend #ifdef'ed code we now make the whole
  129. * initialization sequence configurable to the user.
  130. *
  131. * The requirements for any new initalization function is simple: it
  132. * receives a pointer to the "global data" structure as it's only
  133. * argument, and returns an integer return code, where 0 means
  134. * "continue" and != 0 means "fatal error, hang the system".
  135. */
  136. typedef int (init_fnc_t) (void);
  137. init_fnc_t *init_sequence[] = {
  138. cpu_init_r, /* basic cpu dependent setup */
  139. board_early_init_r, /* basic board dependent setup */
  140. dram_init, /* configure available RAM banks */
  141. interrupt_init, /* set up exceptions */
  142. timer_init,
  143. env_init, /* initialize environment */
  144. init_baudrate, /* initialze baudrate settings */
  145. serial_init, /* serial communications setup */
  146. display_banner,
  147. display_dram_config,
  148. NULL,
  149. };
  150. gd_t *gd;
  151. /*
  152. * Load U-Boot into RAM, initialize BSS, perform relocation adjustments
  153. */
  154. void board_init_f(ulong boot_flags)
  155. {
  156. void *text_start = &__text_start;
  157. void *data_end = &__data_end;
  158. void *rel_dyn_start = &__rel_dyn_start;
  159. void *rel_dyn_end = &__rel_dyn_end;
  160. void *bss_start = &__bss_start;
  161. void *bss_end = &__bss_end;
  162. ulong *dst_addr;
  163. ulong *src_addr;
  164. ulong *end_addr;
  165. void *addr_sp;
  166. void *dest_addr;
  167. ulong rel_offset;
  168. Elf32_Rel *re_src;
  169. Elf32_Rel *re_end;
  170. gd->flags = boot_flags;
  171. if (dram_init_f() != 0)
  172. hang();
  173. /* Calculate destination RAM Address and relocation offset */
  174. dest_addr = (void *)gd->ram_size;
  175. addr_sp = dest_addr;
  176. dest_addr -= CONFIG_SYS_STACK_SIZE;
  177. dest_addr -= (bss_end - text_start);
  178. rel_offset = text_start - dest_addr;
  179. /* First stage CPU initialization */
  180. if (cpu_init_f() != 0)
  181. hang();
  182. /* First stage Board initialization */
  183. if (board_early_init_f() != 0)
  184. hang();
  185. /* Copy U-Boot into RAM */
  186. dst_addr = (ulong *)dest_addr;
  187. src_addr = (ulong *)(text_start + gd->load_off);
  188. end_addr = (ulong *)(data_end + gd->load_off);
  189. while (src_addr < end_addr)
  190. *dst_addr++ = *src_addr++;
  191. /* Clear BSS */
  192. dst_addr = (ulong *)(bss_start - rel_offset);
  193. end_addr = (ulong *)(bss_end - rel_offset);
  194. while (dst_addr < end_addr)
  195. *dst_addr++ = 0x00000000;
  196. /* Perform relocation adjustments */
  197. re_src = (Elf32_Rel *)(rel_dyn_start + gd->load_off);
  198. re_end = (Elf32_Rel *)(rel_dyn_end + gd->load_off);
  199. do {
  200. if (re_src->r_offset >= CONFIG_SYS_TEXT_BASE)
  201. if (*(Elf32_Addr *)(re_src->r_offset - rel_offset) >= CONFIG_SYS_TEXT_BASE)
  202. *(Elf32_Addr *)(re_src->r_offset - rel_offset) -= rel_offset;
  203. } while (re_src++ < re_end);
  204. gd->reloc_off = rel_offset;
  205. gd->flags |= GD_FLG_RELOC;
  206. /* Enter the relocated U-Boot! */
  207. relocate_code((ulong)addr_sp, gd, (ulong)dest_addr);
  208. /* NOTREACHED - relocate_code() does not return */
  209. while(1);
  210. }
  211. void board_init_r(gd_t *id, ulong dest_addr)
  212. {
  213. char *s;
  214. int i;
  215. ulong size;
  216. static bd_t bd_data;
  217. static gd_t gd_data;
  218. init_fnc_t **init_fnc_ptr;
  219. show_boot_progress(0x21);
  220. /* Global data pointer is now writable */
  221. gd = &gd_data;
  222. memcpy(gd, id, sizeof(gd_t));
  223. /* compiler optimization barrier needed for GCC >= 3.4 */
  224. __asm__ __volatile__("": : :"memory");
  225. gd->bd = &bd_data;
  226. memset (gd->bd, 0, sizeof (bd_t));
  227. show_boot_progress(0x22);
  228. gd->baudrate = CONFIG_BAUDRATE;
  229. mem_malloc_init((((ulong)dest_addr - CONFIG_SYS_MALLOC_LEN)+3)&~3,
  230. CONFIG_SYS_MALLOC_LEN);
  231. for (init_fnc_ptr = init_sequence, i=0; *init_fnc_ptr; ++init_fnc_ptr, i++) {
  232. show_boot_progress(0xa130|i);
  233. if ((*init_fnc_ptr)() != 0) {
  234. hang ();
  235. }
  236. }
  237. show_boot_progress(0x23);
  238. #ifdef CONFIG_SERIAL_MULTI
  239. serial_initialize();
  240. #endif
  241. /* configure available FLASH banks */
  242. size = flash_init();
  243. display_flash_config(size);
  244. show_boot_progress(0x24);
  245. show_boot_progress(0x25);
  246. /* initialize environment */
  247. env_relocate ();
  248. show_boot_progress(0x26);
  249. #ifdef CONFIG_CMD_NET
  250. /* IP Address */
  251. bd_data.bi_ip_addr = getenv_IPaddr ("ipaddr");
  252. #endif
  253. #if defined(CONFIG_PCI)
  254. /*
  255. * Do pci configuration
  256. */
  257. pci_init();
  258. #endif
  259. show_boot_progress(0x27);
  260. stdio_init ();
  261. jumptable_init ();
  262. /* Initialize the console (after the relocation and devices init) */
  263. console_init_r();
  264. #ifdef CONFIG_MISC_INIT_R
  265. /* miscellaneous platform dependent initialisations */
  266. misc_init_r();
  267. #endif
  268. #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE)
  269. WATCHDOG_RESET();
  270. puts ("PCMCIA:");
  271. pcmcia_init();
  272. #endif
  273. #if defined(CONFIG_CMD_KGDB)
  274. WATCHDOG_RESET();
  275. puts("KGDB: ");
  276. kgdb_init();
  277. #endif
  278. /* enable exceptions */
  279. enable_interrupts();
  280. show_boot_progress(0x28);
  281. #ifdef CONFIG_STATUS_LED
  282. status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
  283. #endif
  284. udelay(20);
  285. set_timer (0);
  286. /* Initialize from environment */
  287. if ((s = getenv ("loadaddr")) != NULL) {
  288. load_addr = simple_strtoul (s, NULL, 16);
  289. }
  290. #if defined(CONFIG_CMD_NET)
  291. if ((s = getenv ("bootfile")) != NULL) {
  292. copy_filename (BootFile, s, sizeof (BootFile));
  293. }
  294. #endif
  295. WATCHDOG_RESET();
  296. #if defined(CONFIG_CMD_IDE)
  297. WATCHDOG_RESET();
  298. puts("IDE: ");
  299. ide_init();
  300. #endif
  301. #if defined(CONFIG_CMD_SCSI)
  302. WATCHDOG_RESET();
  303. puts("SCSI: ");
  304. scsi_init();
  305. #endif
  306. #if defined(CONFIG_CMD_DOC)
  307. WATCHDOG_RESET();
  308. puts("DOC: ");
  309. doc_init();
  310. #endif
  311. #ifdef CONFIG_BITBANGMII
  312. bb_miiphy_init();
  313. #endif
  314. #if defined(CONFIG_CMD_NET)
  315. #if defined(CONFIG_NET_MULTI)
  316. WATCHDOG_RESET();
  317. puts("Net: ");
  318. #endif
  319. eth_initialize(gd->bd);
  320. #endif
  321. #if ( defined(CONFIG_CMD_NET)) && (0)
  322. WATCHDOG_RESET();
  323. # ifdef DEBUG
  324. puts ("Reset Ethernet PHY\n");
  325. # endif
  326. reset_phy();
  327. #endif
  328. #ifdef CONFIG_LAST_STAGE_INIT
  329. WATCHDOG_RESET();
  330. /*
  331. * Some parts can be only initialized if all others (like
  332. * Interrupts) are up and running (i.e. the PC-style ISA
  333. * keyboard).
  334. */
  335. last_stage_init();
  336. #endif
  337. #ifdef CONFIG_POST
  338. post_run (NULL, POST_RAM | post_bootmode_get(0));
  339. #endif
  340. show_boot_progress(0x29);
  341. /* main_loop() can return to retry autoboot, if so just run it again. */
  342. for (;;) {
  343. main_loop();
  344. }
  345. /* NOTREACHED - no way out of command loop except booting */
  346. }
  347. void hang (void)
  348. {
  349. puts ("### ERROR ### Please RESET the board ###\n");
  350. for (;;);
  351. }
  352. unsigned long do_go_exec (ulong (*entry)(int, char * const []), int argc, char * const argv[])
  353. {
  354. unsigned long ret = 0;
  355. char **argv_tmp;
  356. /*
  357. * x86 does not use a dedicated register to pass the pointer to
  358. * the global_data, so it is instead passed as argv[-1]. By using
  359. * argv[-1], the called 'Application' can use the contents of
  360. * argv natively. However, to safely use argv[-1] a new copy of
  361. * argv is needed with the extra element
  362. */
  363. argv_tmp = malloc(sizeof(char *) * (argc + 1));
  364. if (argv_tmp) {
  365. argv_tmp[0] = (char *)gd;
  366. memcpy(&argv_tmp[1], argv, (size_t)(sizeof(char *) * argc));
  367. ret = (entry) (argc, &argv_tmp[1]);
  368. free(argv_tmp);
  369. }
  370. return ret;
  371. }
  372. void setup_pcat_compatibility(void)
  373. __attribute__((weak, alias("__setup_pcat_compatibility")));
  374. void __setup_pcat_compatibility(void)
  375. {
  376. }