board.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. *
  4. * (C) Copyright 2002-2006
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * (C) Copyright 2002
  8. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  9. * Marius Groeger <mgroeger@sysgo.de>
  10. *
  11. * See file CREDITS for list of people who contributed to this
  12. * project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  27. * MA 02111-1307 USA
  28. */
  29. /*
  30. * This file was taken from ARM and changed to remove things we don't
  31. * need. This is most of it, so have tried to avoid being over-zealous!
  32. * For example, we want to have an emulation of the 'DRAM' used by
  33. * U-Boot.
  34. *
  35. * has been talk upstream of unifying the architectures w.r.t board.c,
  36. * so the less change here the better.
  37. */
  38. #include <common.h>
  39. #include <command.h>
  40. #include <malloc.h>
  41. #include <stdio_dev.h>
  42. #include <timestamp.h>
  43. #include <version.h>
  44. #include <serial.h>
  45. #include <os.h>
  46. DECLARE_GLOBAL_DATA_PTR;
  47. static gd_t gd_mem;
  48. /************************************************************************
  49. * Init Utilities *
  50. ************************************************************************
  51. * Some of this code should be moved into the core functions,
  52. * or dropped completely,
  53. * but let's get it working (again) first...
  54. */
  55. static int display_banner(void)
  56. {
  57. display_options();
  58. return 0;
  59. }
  60. /**
  61. * Configure and report on the DRAM configuration, which in our case is
  62. * fairly simple.
  63. */
  64. static int display_dram_config(void)
  65. {
  66. ulong size = 0;
  67. int i;
  68. debug("RAM Configuration:\n");
  69. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  70. #ifdef DEBUG
  71. printf("Bank #%d: %08lx ", i, gd->bd->bi_dram[i].start);
  72. print_size(gd->bd->bi_dram[i].size, "\n");
  73. #endif
  74. size += gd->bd->bi_dram[i].size;
  75. }
  76. puts("DRAM: ");
  77. print_size(size, "\n");
  78. return 0;
  79. }
  80. /*
  81. * Breathe some life into the board...
  82. *
  83. * Initialize a serial port as console, and carry out some hardware
  84. * tests.
  85. *
  86. * The first part of initialization is running from Flash memory;
  87. * its main purpose is to initialize the RAM so that we
  88. * can relocate the monitor code to RAM.
  89. */
  90. /*
  91. * All attempts to come up with a "common" initialization sequence
  92. * that works for all boards and architectures failed: some of the
  93. * requirements are just _too_ different. To get rid of the resulting
  94. * mess of board dependent #ifdef'ed code we now make the whole
  95. * initialization sequence configurable to the user.
  96. *
  97. * The requirements for any new initalization function is simple: it
  98. * receives a pointer to the "global data" structure as it's only
  99. * argument, and returns an integer return code, where 0 means
  100. * "continue" and != 0 means "fatal error, hang the system".
  101. */
  102. typedef int (init_fnc_t) (void);
  103. void __dram_init_banksize(void)
  104. {
  105. gd->bd->bi_dram[0].start = 0;
  106. gd->bd->bi_dram[0].size = gd->ram_size;
  107. }
  108. void dram_init_banksize(void)
  109. __attribute__((weak, alias("__dram_init_banksize")));
  110. init_fnc_t *init_sequence[] = {
  111. #if defined(CONFIG_ARCH_CPU_INIT)
  112. arch_cpu_init, /* basic arch cpu dependent setup */
  113. #endif
  114. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  115. board_early_init_f,
  116. #endif
  117. timer_init, /* initialize timer */
  118. env_init, /* initialize environment */
  119. serial_init, /* serial communications setup */
  120. console_init_f, /* stage 1 init of console */
  121. sandbox_early_getopt_check, /* process command line flags (err/help) */
  122. display_banner, /* say that we are here */
  123. #if defined(CONFIG_DISPLAY_CPUINFO)
  124. print_cpuinfo, /* display cpu info (and speed) */
  125. #endif
  126. #if defined(CONFIG_DISPLAY_BOARDINFO)
  127. checkboard, /* display board info */
  128. #endif
  129. dram_init, /* configure available RAM banks */
  130. NULL,
  131. };
  132. void board_init_f(ulong bootflag)
  133. {
  134. init_fnc_t **init_fnc_ptr;
  135. uchar *mem;
  136. unsigned long addr_sp, addr, size;
  137. gd = &gd_mem;
  138. assert(gd);
  139. memset((void *)gd, 0, sizeof(gd_t));
  140. #if defined(CONFIG_OF_EMBED)
  141. /* Get a pointer to the FDT */
  142. gd->fdt_blob = _binary_dt_dtb_start;
  143. #elif defined(CONFIG_OF_SEPARATE)
  144. /* FDT is at end of image */
  145. gd->fdt_blob = (void *)(_end_ofs + _TEXT_BASE);
  146. #endif
  147. for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
  148. if ((*init_fnc_ptr)() != 0)
  149. hang();
  150. }
  151. size = CONFIG_SYS_SDRAM_SIZE;
  152. mem = os_malloc(CONFIG_SYS_SDRAM_SIZE);
  153. assert(mem);
  154. gd->ram_buf = mem;
  155. addr = (ulong)(mem + size);
  156. /*
  157. * reserve memory for malloc() arena
  158. */
  159. addr_sp = addr - TOTAL_MALLOC_LEN;
  160. debug("Reserving %dk for malloc() at: %08lx\n",
  161. TOTAL_MALLOC_LEN >> 10, addr_sp);
  162. /*
  163. * (permanently) allocate a Board Info struct
  164. * and a permanent copy of the "global" data
  165. */
  166. addr_sp -= sizeof(bd_t);
  167. gd->bd = (bd_t *) addr_sp;
  168. debug("Reserving %zu Bytes for Board Info at: %08lx\n",
  169. sizeof(bd_t), addr_sp);
  170. /* Ram ist board specific, so move it to board code ... */
  171. dram_init_banksize();
  172. display_dram_config(); /* and display it */
  173. /* We don't relocate, so just run the post-relocation code */
  174. board_init_r(NULL, 0);
  175. /* NOTREACHED - no way out of command loop except booting */
  176. }
  177. /************************************************************************
  178. *
  179. * This is the next part if the initialization sequence: we are now
  180. * running from RAM and have a "normal" C environment, i. e. global
  181. * data can be written, BSS has been cleared, the stack size in not
  182. * that critical any more, etc.
  183. *
  184. ************************************************************************
  185. */
  186. void board_init_r(gd_t *id, ulong dest_addr)
  187. {
  188. if (id)
  189. gd = id;
  190. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  191. #ifdef CONFIG_SERIAL_MULTI
  192. serial_initialize();
  193. #endif
  194. #ifdef CONFIG_POST
  195. post_output_backlog();
  196. #endif
  197. /* The Malloc area is at the top of simulated DRAM */
  198. mem_malloc_init((ulong)gd->ram_buf + gd->ram_size - TOTAL_MALLOC_LEN,
  199. TOTAL_MALLOC_LEN);
  200. /* initialize environment */
  201. env_relocate();
  202. stdio_init(); /* get the devices list going. */
  203. jumptable_init();
  204. console_init_r(); /* fully init console as a device */
  205. #if defined(CONFIG_DISPLAY_BOARDINFO_LATE)
  206. checkboard();
  207. #endif
  208. #if defined(CONFIG_ARCH_MISC_INIT)
  209. /* miscellaneous arch dependent initialisations */
  210. arch_misc_init();
  211. #endif
  212. #if defined(CONFIG_MISC_INIT_R)
  213. /* miscellaneous platform dependent initialisations */
  214. misc_init_r();
  215. #endif
  216. /* set up exceptions */
  217. interrupt_init();
  218. /* enable exceptions */
  219. enable_interrupts();
  220. #ifdef CONFIG_BOARD_LATE_INIT
  221. board_late_init();
  222. #endif
  223. #ifdef CONFIG_POST
  224. post_run(NULL, POST_RAM | post_bootmode_get(0));
  225. #endif
  226. sandbox_main_loop_init();
  227. /*
  228. * For now, run the main loop. Later we might let this be done
  229. * in the main program.
  230. */
  231. while (1)
  232. main_loop();
  233. /* NOTREACHED - no way out of command loop except booting */
  234. }
  235. void hang(void)
  236. {
  237. puts("### ERROR ### Please RESET the board ###\n");
  238. for (;;)
  239. ;
  240. }