board.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 <stdio_dev.h>
  36. #include <asm/u-boot-x86.h>
  37. #include <asm/processor.h>
  38. #include <asm/init_helpers.h>
  39. #include <asm/init_wrappers.h>
  40. /*
  41. * Breath some life into the board...
  42. *
  43. * Initialize an SMC for serial comms, and carry out some hardware
  44. * tests.
  45. *
  46. * The first part of initialization is running from Flash memory;
  47. * its main purpose is to initialize the RAM so that we
  48. * can relocate the monitor code to RAM.
  49. */
  50. /*
  51. * All attempts to come up with a "common" initialization sequence
  52. * that works for all boards and architectures failed: some of the
  53. * requirements are just _too_ different. To get rid of the resulting
  54. * mess of board dependend #ifdef'ed code we now make the whole
  55. * initialization sequence configurable to the user.
  56. *
  57. * The requirements for any new initalization function is simple: it
  58. * receives a pointer to the "global data" structure as it's only
  59. * argument, and returns an integer return code, where 0 means
  60. * "continue" and != 0 means "fatal error, hang the system".
  61. */
  62. typedef int (init_fnc_t) (void);
  63. static int calculate_relocation_address(void);
  64. static int copy_gd_to_ram(void);
  65. init_fnc_t *init_sequence_f[] = {
  66. cpu_init_f,
  67. board_early_init_f,
  68. env_init,
  69. init_baudrate_f,
  70. serial_init,
  71. console_init_f,
  72. dram_init_f,
  73. calculate_relocation_address,
  74. NULL,
  75. };
  76. init_fnc_t *init_sequence_r[] = {
  77. init_bd_struct_r,
  78. mem_malloc_init_r,
  79. cpu_init_r,
  80. board_early_init_r,
  81. dram_init,
  82. interrupt_init,
  83. timer_init,
  84. display_banner,
  85. display_dram_config,
  86. #ifdef CONFIG_SERIAL_MULTI
  87. serial_initialize_r,
  88. #endif
  89. #ifndef CONFIG_SYS_NO_FLASH
  90. flash_init_r,
  91. #endif
  92. env_relocate_r,
  93. #ifdef CONFIG_CMD_NET
  94. init_ip_address_r,
  95. #endif
  96. #ifdef CONFIG_PCI
  97. pci_init_r,
  98. #endif
  99. stdio_init,
  100. jumptable_init_r,
  101. console_init_r,
  102. #ifdef CONFIG_MISC_INIT_R
  103. misc_init_r,
  104. #endif
  105. #if defined(CONFIG_CMD_PCMCIA) && !defined(CONFIG_CMD_IDE)
  106. pci_init_r,
  107. #endif
  108. #if defined(CONFIG_CMD_KGDB)
  109. kgdb_init_r,
  110. #endif
  111. enable_interrupts_r,
  112. #ifdef CONFIG_STATUS_LED
  113. status_led_set_r,
  114. #endif
  115. set_load_addr_r,
  116. #if defined(CONFIG_CMD_NET)
  117. set_bootfile_r,
  118. #endif
  119. #if defined(CONFIG_CMD_IDE)
  120. ide_init_r,
  121. #endif
  122. #if defined(CONFIG_CMD_SCSI)
  123. scsi_init_r,
  124. #endif
  125. #if defined(CONFIG_CMD_DOC)
  126. doc_init_r,
  127. #endif
  128. #ifdef CONFIG_BITBANGMII
  129. bb_miiphy_init_r,
  130. #endif
  131. #if defined(CONFIG_CMD_NET)
  132. eth_initialize_r,
  133. #ifdef CONFIG_RESET_PHY_R
  134. reset_phy_r,
  135. #endif
  136. #endif
  137. #ifdef CONFIG_LAST_STAGE_INIT
  138. last_stage_init,
  139. #endif
  140. NULL,
  141. };
  142. static void do_init_loop(init_fnc_t **init_fnc_ptr)
  143. {
  144. for (; *init_fnc_ptr; ++init_fnc_ptr) {
  145. WATCHDOG_RESET();
  146. if ((*init_fnc_ptr)() != 0)
  147. hang();
  148. }
  149. }
  150. static int calculate_relocation_address(void)
  151. {
  152. ulong text_start = (ulong)&__text_start;
  153. ulong bss_end = (ulong)&__bss_end;
  154. ulong dest_addr;
  155. /*
  156. * NOTE: All destination address are rounded down to 16-byte
  157. * boundary to satisfy various worst-case alignment
  158. * requirements
  159. */
  160. /* Global Data is at top of available memory */
  161. dest_addr = gd->ram_size;
  162. dest_addr -= GENERATED_GBL_DATA_SIZE;
  163. dest_addr &= ~15;
  164. gd->new_gd_addr = dest_addr;
  165. /* GDT is below Global Data */
  166. dest_addr -= X86_GDT_SIZE;
  167. dest_addr &= ~15;
  168. gd->gdt_addr = dest_addr;
  169. /* Stack is below GDT */
  170. gd->start_addr_sp = dest_addr;
  171. /* U-Boot is below the stack */
  172. dest_addr -= CONFIG_SYS_STACK_SIZE;
  173. dest_addr -= (bss_end - text_start);
  174. dest_addr &= ~15;
  175. gd->relocaddr = dest_addr;
  176. gd->reloc_off = (dest_addr - text_start);
  177. return 0;
  178. }
  179. /* Perform all steps necessary to get RAM initialised ready for relocation */
  180. void board_init_f(ulong boot_flags)
  181. {
  182. gd->flags = boot_flags;
  183. do_init_loop(init_sequence_f);
  184. /*
  185. * SDRAM is now initialised, U-Boot has been copied into SDRAM,
  186. * the BSS has been cleared etc. The final stack can now be setup
  187. * in SDRAM. Code execution will continue (momentarily) in Flash,
  188. * but with the stack in SDRAM and Global Data in temporary memory
  189. * (CPU cache)
  190. */
  191. board_init_f_r_trampoline(gd->start_addr_sp);
  192. /* NOTREACHED - board_init_f_r_trampoline() does not return */
  193. while (1)
  194. ;
  195. }
  196. void board_init_f_r(void)
  197. {
  198. if (copy_gd_to_ram() != 0)
  199. hang();
  200. if (init_cache() != 0)
  201. hang();
  202. relocate_code(0, gd, 0);
  203. /* NOTREACHED - relocate_code() does not return */
  204. while (1)
  205. ;
  206. }
  207. static int copy_gd_to_ram(void)
  208. {
  209. gd_t *ram_gd;
  210. /*
  211. * Global data is still in temporary memory (the CPU cache).
  212. * calculate_relocation_address() has set gd->new_gd_addr to
  213. * where the global data lives in RAM but getting it there
  214. * safely is a bit tricky due to the 'F-Segment Hack' that
  215. * we need to use for x86
  216. */
  217. ram_gd = (gd_t *)gd->new_gd_addr;
  218. memcpy((void *)ram_gd, gd, sizeof(gd_t));
  219. /*
  220. * Reload the Global Descriptor Table so FS points to the
  221. * in-RAM copy of Global Data (calculate_relocation_address()
  222. * has already calculated the in-RAM location of the GDT)
  223. */
  224. ram_gd->gd_addr = (ulong)ram_gd;
  225. init_gd(ram_gd, (u64 *)gd->gdt_addr);
  226. return 0;
  227. }
  228. void board_init_r(gd_t *id, ulong dest_addr)
  229. {
  230. gd->flags |= GD_FLG_RELOC;
  231. /* compiler optimization barrier needed for GCC >= 3.4 */
  232. __asm__ __volatile__("" : : : "memory");
  233. do_init_loop(init_sequence_r);
  234. /* main_loop() can return to retry autoboot, if so just run it again. */
  235. for (;;)
  236. main_loop();
  237. /* NOTREACHED - no way out of command loop except booting */
  238. }
  239. void hang(void)
  240. {
  241. puts("### ERROR ### Please RESET the board ###\n");
  242. for (;;)
  243. ;
  244. }