board.c 11 KB

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