board_f.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * (C) Copyright 2002-2006
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * (C) Copyright 2002
  7. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Marius Groeger <mgroeger@sysgo.de>
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <console.h>
  14. #include <environment.h>
  15. #include <dm.h>
  16. #include <fdtdec.h>
  17. #include <fs.h>
  18. #include <i2c.h>
  19. #include <initcall.h>
  20. #include <init_helpers.h>
  21. #include <logbuff.h>
  22. #include <malloc.h>
  23. #include <mapmem.h>
  24. #include <os.h>
  25. #include <post.h>
  26. #include <relocate.h>
  27. #include <spi.h>
  28. #include <status_led.h>
  29. #include <timer.h>
  30. #include <trace.h>
  31. #include <video.h>
  32. #include <watchdog.h>
  33. #ifdef CONFIG_MACH_TYPE
  34. #include <asm/mach-types.h>
  35. #endif
  36. #if defined(CONFIG_MP) && defined(CONFIG_PPC)
  37. #include <asm/mp.h>
  38. #endif
  39. #include <asm/io.h>
  40. #include <asm/sections.h>
  41. #include <dm/root.h>
  42. #include <linux/errno.h>
  43. /*
  44. * Pointer to initial global data area
  45. *
  46. * Here we initialize it if needed.
  47. */
  48. #ifdef XTRN_DECLARE_GLOBAL_DATA_PTR
  49. #undef XTRN_DECLARE_GLOBAL_DATA_PTR
  50. #define XTRN_DECLARE_GLOBAL_DATA_PTR /* empty = allocate here */
  51. DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_GD_ADDR);
  52. #else
  53. DECLARE_GLOBAL_DATA_PTR;
  54. #endif
  55. /*
  56. * TODO(sjg@chromium.org): IMO this code should be
  57. * refactored to a single function, something like:
  58. *
  59. * void led_set_state(enum led_colour_t colour, int on);
  60. */
  61. /************************************************************************
  62. * Coloured LED functionality
  63. ************************************************************************
  64. * May be supplied by boards if desired
  65. */
  66. __weak void coloured_LED_init(void) {}
  67. __weak void red_led_on(void) {}
  68. __weak void red_led_off(void) {}
  69. __weak void green_led_on(void) {}
  70. __weak void green_led_off(void) {}
  71. __weak void yellow_led_on(void) {}
  72. __weak void yellow_led_off(void) {}
  73. __weak void blue_led_on(void) {}
  74. __weak void blue_led_off(void) {}
  75. /*
  76. * Why is gd allocated a register? Prior to reloc it might be better to
  77. * just pass it around to each function in this file?
  78. *
  79. * After reloc one could argue that it is hardly used and doesn't need
  80. * to be in a register. Or if it is it should perhaps hold pointers to all
  81. * global data for all modules, so that post-reloc we can avoid the massive
  82. * literal pool we get on ARM. Or perhaps just encourage each module to use
  83. * a structure...
  84. */
  85. #if defined(CONFIG_WATCHDOG) || defined(CONFIG_HW_WATCHDOG)
  86. static int init_func_watchdog_init(void)
  87. {
  88. # if defined(CONFIG_HW_WATCHDOG) && \
  89. (defined(CONFIG_M68K) || defined(CONFIG_MICROBLAZE) || \
  90. defined(CONFIG_SH) || defined(CONFIG_AT91SAM9_WATCHDOG) || \
  91. defined(CONFIG_DESIGNWARE_WATCHDOG) || \
  92. defined(CONFIG_IMX_WATCHDOG))
  93. hw_watchdog_init();
  94. puts(" Watchdog enabled\n");
  95. # endif
  96. WATCHDOG_RESET();
  97. return 0;
  98. }
  99. int init_func_watchdog_reset(void)
  100. {
  101. WATCHDOG_RESET();
  102. return 0;
  103. }
  104. #endif /* CONFIG_WATCHDOG */
  105. __weak void board_add_ram_info(int use_default)
  106. {
  107. /* please define platform specific board_add_ram_info() */
  108. }
  109. static int init_baud_rate(void)
  110. {
  111. gd->baudrate = env_get_ulong("baudrate", 10, CONFIG_BAUDRATE);
  112. return 0;
  113. }
  114. static int display_text_info(void)
  115. {
  116. #if !defined(CONFIG_SANDBOX) && !defined(CONFIG_EFI_APP)
  117. ulong bss_start, bss_end, text_base;
  118. bss_start = (ulong)&__bss_start;
  119. bss_end = (ulong)&__bss_end;
  120. #ifdef CONFIG_SYS_TEXT_BASE
  121. text_base = CONFIG_SYS_TEXT_BASE;
  122. #else
  123. text_base = CONFIG_SYS_MONITOR_BASE;
  124. #endif
  125. debug("U-Boot code: %08lX -> %08lX BSS: -> %08lX\n",
  126. text_base, bss_start, bss_end);
  127. #endif
  128. return 0;
  129. }
  130. static int announce_dram_init(void)
  131. {
  132. puts("DRAM: ");
  133. return 0;
  134. }
  135. static int show_dram_config(void)
  136. {
  137. unsigned long long size;
  138. #ifdef CONFIG_NR_DRAM_BANKS
  139. int i;
  140. debug("\nRAM Configuration:\n");
  141. for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  142. size += gd->bd->bi_dram[i].size;
  143. debug("Bank #%d: %llx ", i,
  144. (unsigned long long)(gd->bd->bi_dram[i].start));
  145. #ifdef DEBUG
  146. print_size(gd->bd->bi_dram[i].size, "\n");
  147. #endif
  148. }
  149. debug("\nDRAM: ");
  150. #else
  151. size = gd->ram_size;
  152. #endif
  153. print_size(size, "");
  154. board_add_ram_info(0);
  155. putc('\n');
  156. return 0;
  157. }
  158. __weak int dram_init_banksize(void)
  159. {
  160. #if defined(CONFIG_NR_DRAM_BANKS) && defined(CONFIG_SYS_SDRAM_BASE)
  161. gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
  162. gd->bd->bi_dram[0].size = get_effective_memsize();
  163. #endif
  164. return 0;
  165. }
  166. #if defined(CONFIG_SYS_I2C)
  167. static int init_func_i2c(void)
  168. {
  169. puts("I2C: ");
  170. #ifdef CONFIG_SYS_I2C
  171. i2c_init_all();
  172. #else
  173. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  174. #endif
  175. puts("ready\n");
  176. return 0;
  177. }
  178. #endif
  179. #if defined(CONFIG_HARD_SPI)
  180. static int init_func_spi(void)
  181. {
  182. puts("SPI: ");
  183. spi_init();
  184. puts("ready\n");
  185. return 0;
  186. }
  187. #endif
  188. static int setup_mon_len(void)
  189. {
  190. #if defined(__ARM__) || defined(__MICROBLAZE__)
  191. gd->mon_len = (ulong)&__bss_end - (ulong)_start;
  192. #elif defined(CONFIG_SANDBOX) || defined(CONFIG_EFI_APP)
  193. gd->mon_len = (ulong)&_end - (ulong)_init;
  194. #elif defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
  195. gd->mon_len = CONFIG_SYS_MONITOR_LEN;
  196. #elif defined(CONFIG_NDS32) || defined(CONFIG_SH)
  197. gd->mon_len = (ulong)(&__bss_end) - (ulong)(&_start);
  198. #elif defined(CONFIG_SYS_MONITOR_BASE)
  199. /* TODO: use (ulong)&__bss_end - (ulong)&__text_start; ? */
  200. gd->mon_len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
  201. #endif
  202. return 0;
  203. }
  204. __weak int arch_cpu_init(void)
  205. {
  206. return 0;
  207. }
  208. __weak int mach_cpu_init(void)
  209. {
  210. return 0;
  211. }
  212. /* Get the top of usable RAM */
  213. __weak ulong board_get_usable_ram_top(ulong total_size)
  214. {
  215. #ifdef CONFIG_SYS_SDRAM_BASE
  216. /*
  217. * Detect whether we have so much RAM that it goes past the end of our
  218. * 32-bit address space. If so, clip the usable RAM so it doesn't.
  219. */
  220. if (gd->ram_top < CONFIG_SYS_SDRAM_BASE)
  221. /*
  222. * Will wrap back to top of 32-bit space when reservations
  223. * are made.
  224. */
  225. return 0;
  226. #endif
  227. return gd->ram_top;
  228. }
  229. static int setup_dest_addr(void)
  230. {
  231. debug("Monitor len: %08lX\n", gd->mon_len);
  232. /*
  233. * Ram is setup, size stored in gd !!
  234. */
  235. debug("Ram size: %08lX\n", (ulong)gd->ram_size);
  236. #if defined(CONFIG_SYS_MEM_TOP_HIDE)
  237. /*
  238. * Subtract specified amount of memory to hide so that it won't
  239. * get "touched" at all by U-Boot. By fixing up gd->ram_size
  240. * the Linux kernel should now get passed the now "corrected"
  241. * memory size and won't touch it either. This should work
  242. * for arch/ppc and arch/powerpc. Only Linux board ports in
  243. * arch/powerpc with bootwrapper support, that recalculate the
  244. * memory size from the SDRAM controller setup will have to
  245. * get fixed.
  246. */
  247. gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
  248. #endif
  249. #ifdef CONFIG_SYS_SDRAM_BASE
  250. gd->ram_top = CONFIG_SYS_SDRAM_BASE;
  251. #endif
  252. gd->ram_top += get_effective_memsize();
  253. gd->ram_top = board_get_usable_ram_top(gd->mon_len);
  254. gd->relocaddr = gd->ram_top;
  255. debug("Ram top: %08lX\n", (ulong)gd->ram_top);
  256. #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
  257. /*
  258. * We need to make sure the location we intend to put secondary core
  259. * boot code is reserved and not used by any part of u-boot
  260. */
  261. if (gd->relocaddr > determine_mp_bootpg(NULL)) {
  262. gd->relocaddr = determine_mp_bootpg(NULL);
  263. debug("Reserving MP boot page to %08lx\n", gd->relocaddr);
  264. }
  265. #endif
  266. return 0;
  267. }
  268. #if defined(CONFIG_LOGBUFFER)
  269. static int reserve_logbuffer(void)
  270. {
  271. #ifndef CONFIG_ALT_LB_ADDR
  272. /* reserve kernel log buffer */
  273. gd->relocaddr -= LOGBUFF_RESERVE;
  274. debug("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN,
  275. gd->relocaddr);
  276. #endif
  277. return 0;
  278. }
  279. #endif
  280. #ifdef CONFIG_PRAM
  281. /* reserve protected RAM */
  282. static int reserve_pram(void)
  283. {
  284. ulong reg;
  285. reg = env_get_ulong("pram", 10, CONFIG_PRAM);
  286. gd->relocaddr -= (reg << 10); /* size is in kB */
  287. debug("Reserving %ldk for protected RAM at %08lx\n", reg,
  288. gd->relocaddr);
  289. return 0;
  290. }
  291. #endif /* CONFIG_PRAM */
  292. /* Round memory pointer down to next 4 kB limit */
  293. static int reserve_round_4k(void)
  294. {
  295. gd->relocaddr &= ~(4096 - 1);
  296. return 0;
  297. }
  298. #ifdef CONFIG_ARM
  299. __weak int reserve_mmu(void)
  300. {
  301. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
  302. /* reserve TLB table */
  303. gd->arch.tlb_size = PGTABLE_SIZE;
  304. gd->relocaddr -= gd->arch.tlb_size;
  305. /* round down to next 64 kB limit */
  306. gd->relocaddr &= ~(0x10000 - 1);
  307. gd->arch.tlb_addr = gd->relocaddr;
  308. debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
  309. gd->arch.tlb_addr + gd->arch.tlb_size);
  310. #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
  311. /*
  312. * Record allocated tlb_addr in case gd->tlb_addr to be overwritten
  313. * with location within secure ram.
  314. */
  315. gd->arch.tlb_allocated = gd->arch.tlb_addr;
  316. #endif
  317. #endif
  318. return 0;
  319. }
  320. #endif
  321. static int reserve_video(void)
  322. {
  323. #ifdef CONFIG_DM_VIDEO
  324. ulong addr;
  325. int ret;
  326. addr = gd->relocaddr;
  327. ret = video_reserve(&addr);
  328. if (ret)
  329. return ret;
  330. gd->relocaddr = addr;
  331. #elif defined(CONFIG_LCD)
  332. # ifdef CONFIG_FB_ADDR
  333. gd->fb_base = CONFIG_FB_ADDR;
  334. # else
  335. /* reserve memory for LCD display (always full pages) */
  336. gd->relocaddr = lcd_setmem(gd->relocaddr);
  337. gd->fb_base = gd->relocaddr;
  338. # endif /* CONFIG_FB_ADDR */
  339. #elif defined(CONFIG_VIDEO) && \
  340. (!defined(CONFIG_PPC)) && \
  341. !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
  342. !defined(CONFIG_M68K)
  343. /* reserve memory for video display (always full pages) */
  344. gd->relocaddr = video_setmem(gd->relocaddr);
  345. gd->fb_base = gd->relocaddr;
  346. #endif
  347. return 0;
  348. }
  349. static int reserve_trace(void)
  350. {
  351. #ifdef CONFIG_TRACE
  352. gd->relocaddr -= CONFIG_TRACE_BUFFER_SIZE;
  353. gd->trace_buff = map_sysmem(gd->relocaddr, CONFIG_TRACE_BUFFER_SIZE);
  354. debug("Reserving %dk for trace data at: %08lx\n",
  355. CONFIG_TRACE_BUFFER_SIZE >> 10, gd->relocaddr);
  356. #endif
  357. return 0;
  358. }
  359. static int reserve_uboot(void)
  360. {
  361. /*
  362. * reserve memory for U-Boot code, data & bss
  363. * round down to next 4 kB limit
  364. */
  365. gd->relocaddr -= gd->mon_len;
  366. gd->relocaddr &= ~(4096 - 1);
  367. #if defined(CONFIG_E500) || defined(CONFIG_MIPS)
  368. /* round down to next 64 kB limit so that IVPR stays aligned */
  369. gd->relocaddr &= ~(65536 - 1);
  370. #endif
  371. debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10,
  372. gd->relocaddr);
  373. gd->start_addr_sp = gd->relocaddr;
  374. return 0;
  375. }
  376. /* reserve memory for malloc() area */
  377. static int reserve_malloc(void)
  378. {
  379. gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
  380. debug("Reserving %dk for malloc() at: %08lx\n",
  381. TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
  382. return 0;
  383. }
  384. /* (permanently) allocate a Board Info struct */
  385. static int reserve_board(void)
  386. {
  387. if (!gd->bd) {
  388. gd->start_addr_sp -= sizeof(bd_t);
  389. gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
  390. memset(gd->bd, '\0', sizeof(bd_t));
  391. debug("Reserving %zu Bytes for Board Info at: %08lx\n",
  392. sizeof(bd_t), gd->start_addr_sp);
  393. }
  394. return 0;
  395. }
  396. static int setup_machine(void)
  397. {
  398. #ifdef CONFIG_MACH_TYPE
  399. gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
  400. #endif
  401. return 0;
  402. }
  403. static int reserve_global_data(void)
  404. {
  405. gd->start_addr_sp -= sizeof(gd_t);
  406. gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
  407. debug("Reserving %zu Bytes for Global Data at: %08lx\n",
  408. sizeof(gd_t), gd->start_addr_sp);
  409. return 0;
  410. }
  411. static int reserve_fdt(void)
  412. {
  413. #ifndef CONFIG_OF_EMBED
  414. /*
  415. * If the device tree is sitting immediately above our image then we
  416. * must relocate it. If it is embedded in the data section, then it
  417. * will be relocated with other data.
  418. */
  419. if (gd->fdt_blob) {
  420. gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
  421. gd->start_addr_sp -= gd->fdt_size;
  422. gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
  423. debug("Reserving %lu Bytes for FDT at: %08lx\n",
  424. gd->fdt_size, gd->start_addr_sp);
  425. }
  426. #endif
  427. return 0;
  428. }
  429. static int reserve_bootstage(void)
  430. {
  431. #ifdef CONFIG_BOOTSTAGE
  432. int size = bootstage_get_size();
  433. gd->start_addr_sp -= size;
  434. gd->new_bootstage = map_sysmem(gd->start_addr_sp, size);
  435. debug("Reserving %#x Bytes for bootstage at: %08lx\n", size,
  436. gd->start_addr_sp);
  437. #endif
  438. return 0;
  439. }
  440. int arch_reserve_stacks(void)
  441. {
  442. return 0;
  443. }
  444. static int reserve_stacks(void)
  445. {
  446. /* make stack pointer 16-byte aligned */
  447. gd->start_addr_sp -= 16;
  448. gd->start_addr_sp &= ~0xf;
  449. /*
  450. * let the architecture-specific code tailor gd->start_addr_sp and
  451. * gd->irq_sp
  452. */
  453. return arch_reserve_stacks();
  454. }
  455. static int display_new_sp(void)
  456. {
  457. debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
  458. return 0;
  459. }
  460. #if defined(CONFIG_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
  461. defined(CONFIG_SH)
  462. static int setup_board_part1(void)
  463. {
  464. bd_t *bd = gd->bd;
  465. /*
  466. * Save local variables to board info struct
  467. */
  468. bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of memory */
  469. bd->bi_memsize = gd->ram_size; /* size in bytes */
  470. #ifdef CONFIG_SYS_SRAM_BASE
  471. bd->bi_sramstart = CONFIG_SYS_SRAM_BASE; /* start of SRAM */
  472. bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE; /* size of SRAM */
  473. #endif
  474. #if defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
  475. bd->bi_immr_base = CONFIG_SYS_IMMR; /* base of IMMR register */
  476. #endif
  477. #if defined(CONFIG_M68K)
  478. bd->bi_mbar_base = CONFIG_SYS_MBAR; /* base of internal registers */
  479. #endif
  480. #if defined(CONFIG_MPC83xx)
  481. bd->bi_immrbar = CONFIG_SYS_IMMR;
  482. #endif
  483. return 0;
  484. }
  485. #endif
  486. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  487. static int setup_board_part2(void)
  488. {
  489. bd_t *bd = gd->bd;
  490. bd->bi_intfreq = gd->cpu_clk; /* Internal Freq, in Hz */
  491. bd->bi_busfreq = gd->bus_clk; /* Bus Freq, in Hz */
  492. #if defined(CONFIG_CPM2)
  493. bd->bi_cpmfreq = gd->arch.cpm_clk;
  494. bd->bi_brgfreq = gd->arch.brg_clk;
  495. bd->bi_sccfreq = gd->arch.scc_clk;
  496. bd->bi_vco = gd->arch.vco_out;
  497. #endif /* CONFIG_CPM2 */
  498. #if defined(CONFIG_M68K) && defined(CONFIG_PCI)
  499. bd->bi_pcifreq = gd->pci_clk;
  500. #endif
  501. #if defined(CONFIG_EXTRA_CLOCK)
  502. bd->bi_inpfreq = gd->arch.inp_clk; /* input Freq in Hz */
  503. bd->bi_vcofreq = gd->arch.vco_clk; /* vco Freq in Hz */
  504. bd->bi_flbfreq = gd->arch.flb_clk; /* flexbus Freq in Hz */
  505. #endif
  506. return 0;
  507. }
  508. #endif
  509. #ifdef CONFIG_POST
  510. static int init_post(void)
  511. {
  512. post_bootmode_init();
  513. post_run(NULL, POST_ROM | post_bootmode_get(0));
  514. return 0;
  515. }
  516. #endif
  517. static int reloc_fdt(void)
  518. {
  519. #ifndef CONFIG_OF_EMBED
  520. if (gd->flags & GD_FLG_SKIP_RELOC)
  521. return 0;
  522. if (gd->new_fdt) {
  523. memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
  524. gd->fdt_blob = gd->new_fdt;
  525. }
  526. #endif
  527. return 0;
  528. }
  529. static int reloc_bootstage(void)
  530. {
  531. #ifdef CONFIG_BOOTSTAGE
  532. if (gd->flags & GD_FLG_SKIP_RELOC)
  533. return 0;
  534. if (gd->new_bootstage) {
  535. int size = bootstage_get_size();
  536. debug("Copying bootstage from %p to %p, size %x\n",
  537. gd->bootstage, gd->new_bootstage, size);
  538. memcpy(gd->new_bootstage, gd->bootstage, size);
  539. gd->bootstage = gd->new_bootstage;
  540. }
  541. #endif
  542. return 0;
  543. }
  544. static int setup_reloc(void)
  545. {
  546. if (gd->flags & GD_FLG_SKIP_RELOC) {
  547. debug("Skipping relocation due to flag\n");
  548. return 0;
  549. }
  550. #ifdef CONFIG_SYS_TEXT_BASE
  551. #ifdef ARM
  552. gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
  553. #elif defined(CONFIG_M68K)
  554. /*
  555. * On all ColdFire arch cpu, monitor code starts always
  556. * just after the default vector table location, so at 0x400
  557. */
  558. gd->reloc_off = gd->relocaddr - (CONFIG_SYS_TEXT_BASE + 0x400);
  559. #else
  560. gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
  561. #endif
  562. #endif
  563. memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
  564. debug("Relocation Offset is: %08lx\n", gd->reloc_off);
  565. debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
  566. gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
  567. gd->start_addr_sp);
  568. return 0;
  569. }
  570. #ifdef CONFIG_OF_BOARD_FIXUP
  571. static int fix_fdt(void)
  572. {
  573. return board_fix_fdt((void *)gd->fdt_blob);
  574. }
  575. #endif
  576. /* ARM calls relocate_code from its crt0.S */
  577. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
  578. !CONFIG_IS_ENABLED(X86_64)
  579. static int jump_to_copy(void)
  580. {
  581. if (gd->flags & GD_FLG_SKIP_RELOC)
  582. return 0;
  583. /*
  584. * x86 is special, but in a nice way. It uses a trampoline which
  585. * enables the dcache if possible.
  586. *
  587. * For now, other archs use relocate_code(), which is implemented
  588. * similarly for all archs. When we do generic relocation, hopefully
  589. * we can make all archs enable the dcache prior to relocation.
  590. */
  591. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  592. /*
  593. * SDRAM and console are now initialised. The final stack can now
  594. * be setup in SDRAM. Code execution will continue in Flash, but
  595. * with the stack in SDRAM and Global Data in temporary memory
  596. * (CPU cache)
  597. */
  598. arch_setup_gd(gd->new_gd);
  599. board_init_f_r_trampoline(gd->start_addr_sp);
  600. #else
  601. relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
  602. #endif
  603. return 0;
  604. }
  605. #endif
  606. /* Record the board_init_f() bootstage (after arch_cpu_init()) */
  607. static int initf_bootstage(void)
  608. {
  609. bool from_spl = IS_ENABLED(CONFIG_SPL_BOOTSTAGE) &&
  610. IS_ENABLED(CONFIG_BOOTSTAGE_STASH);
  611. int ret;
  612. ret = bootstage_init(!from_spl);
  613. if (ret)
  614. return ret;
  615. if (from_spl) {
  616. const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
  617. CONFIG_BOOTSTAGE_STASH_SIZE);
  618. ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
  619. if (ret && ret != -ENOENT) {
  620. debug("Failed to unstash bootstage: err=%d\n", ret);
  621. return ret;
  622. }
  623. }
  624. bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
  625. return 0;
  626. }
  627. static int initf_console_record(void)
  628. {
  629. #if defined(CONFIG_CONSOLE_RECORD) && CONFIG_VAL(SYS_MALLOC_F_LEN)
  630. return console_record_init();
  631. #else
  632. return 0;
  633. #endif
  634. }
  635. static int initf_dm(void)
  636. {
  637. #if defined(CONFIG_DM) && CONFIG_VAL(SYS_MALLOC_F_LEN)
  638. int ret;
  639. bootstage_start(BOOTSTATE_ID_ACCUM_DM_F, "dm_f");
  640. ret = dm_init_and_scan(true);
  641. bootstage_accum(BOOTSTATE_ID_ACCUM_DM_F);
  642. if (ret)
  643. return ret;
  644. #endif
  645. #ifdef CONFIG_TIMER_EARLY
  646. ret = dm_timer_init();
  647. if (ret)
  648. return ret;
  649. #endif
  650. return 0;
  651. }
  652. /* Architecture-specific memory reservation */
  653. __weak int reserve_arch(void)
  654. {
  655. return 0;
  656. }
  657. __weak int arch_cpu_init_dm(void)
  658. {
  659. return 0;
  660. }
  661. static const init_fnc_t init_sequence_f[] = {
  662. setup_mon_len,
  663. #ifdef CONFIG_OF_CONTROL
  664. fdtdec_setup,
  665. #endif
  666. #ifdef CONFIG_TRACE
  667. trace_early_init,
  668. #endif
  669. initf_malloc,
  670. initf_bootstage, /* uses its own timer, so does not need DM */
  671. initf_console_record,
  672. #if defined(CONFIG_HAVE_FSP)
  673. arch_fsp_init,
  674. #endif
  675. arch_cpu_init, /* basic arch cpu dependent setup */
  676. mach_cpu_init, /* SoC/machine dependent CPU setup */
  677. initf_dm,
  678. arch_cpu_init_dm,
  679. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  680. board_early_init_f,
  681. #endif
  682. #if defined(CONFIG_PPC) || defined(CONFIG_SYS_FSL_CLK) || defined(CONFIG_M68K)
  683. /* get CPU and bus clocks according to the environment variable */
  684. get_clocks, /* get CPU and bus clocks (etc.) */
  685. #endif
  686. #if !defined(CONFIG_M68K)
  687. timer_init, /* initialize timer */
  688. #endif
  689. #if defined(CONFIG_BOARD_POSTCLK_INIT)
  690. board_postclk_init,
  691. #endif
  692. env_init, /* initialize environment */
  693. init_baud_rate, /* initialze baudrate settings */
  694. serial_init, /* serial communications setup */
  695. console_init_f, /* stage 1 init of console */
  696. display_options, /* say that we are here */
  697. display_text_info, /* show debugging info if required */
  698. #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SH) || \
  699. defined(CONFIG_X86)
  700. checkcpu,
  701. #endif
  702. #if defined(CONFIG_DISPLAY_CPUINFO)
  703. print_cpuinfo, /* display cpu info (and speed) */
  704. #endif
  705. #if defined(CONFIG_DTB_RESELECT)
  706. embedded_dtb_select,
  707. #endif
  708. #if defined(CONFIG_DISPLAY_BOARDINFO)
  709. show_board_info,
  710. #endif
  711. INIT_FUNC_WATCHDOG_INIT
  712. #if defined(CONFIG_MISC_INIT_F)
  713. misc_init_f,
  714. #endif
  715. INIT_FUNC_WATCHDOG_RESET
  716. #if defined(CONFIG_SYS_I2C)
  717. init_func_i2c,
  718. #endif
  719. #if defined(CONFIG_HARD_SPI)
  720. init_func_spi,
  721. #endif
  722. announce_dram_init,
  723. dram_init, /* configure available RAM banks */
  724. #ifdef CONFIG_POST
  725. post_init_f,
  726. #endif
  727. INIT_FUNC_WATCHDOG_RESET
  728. #if defined(CONFIG_SYS_DRAM_TEST)
  729. testdram,
  730. #endif /* CONFIG_SYS_DRAM_TEST */
  731. INIT_FUNC_WATCHDOG_RESET
  732. #ifdef CONFIG_POST
  733. init_post,
  734. #endif
  735. INIT_FUNC_WATCHDOG_RESET
  736. /*
  737. * Now that we have DRAM mapped and working, we can
  738. * relocate the code and continue running from DRAM.
  739. *
  740. * Reserve memory at end of RAM for (top down in that order):
  741. * - area that won't get touched by U-Boot and Linux (optional)
  742. * - kernel log buffer
  743. * - protected RAM
  744. * - LCD framebuffer
  745. * - monitor code
  746. * - board info struct
  747. */
  748. setup_dest_addr,
  749. #if defined(CONFIG_LOGBUFFER)
  750. reserve_logbuffer,
  751. #endif
  752. #ifdef CONFIG_PRAM
  753. reserve_pram,
  754. #endif
  755. reserve_round_4k,
  756. #ifdef CONFIG_ARM
  757. reserve_mmu,
  758. #endif
  759. reserve_video,
  760. reserve_trace,
  761. reserve_uboot,
  762. reserve_malloc,
  763. reserve_board,
  764. setup_machine,
  765. reserve_global_data,
  766. reserve_fdt,
  767. reserve_bootstage,
  768. reserve_arch,
  769. reserve_stacks,
  770. dram_init_banksize,
  771. show_dram_config,
  772. #if defined(CONFIG_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
  773. defined(CONFIG_SH)
  774. setup_board_part1,
  775. #endif
  776. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  777. INIT_FUNC_WATCHDOG_RESET
  778. setup_board_part2,
  779. #endif
  780. display_new_sp,
  781. #ifdef CONFIG_OF_BOARD_FIXUP
  782. fix_fdt,
  783. #endif
  784. INIT_FUNC_WATCHDOG_RESET
  785. reloc_fdt,
  786. reloc_bootstage,
  787. setup_reloc,
  788. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  789. copy_uboot_to_ram,
  790. do_elf_reloc_fixups,
  791. clear_bss,
  792. #endif
  793. #if defined(CONFIG_XTENSA)
  794. clear_bss,
  795. #endif
  796. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
  797. !CONFIG_IS_ENABLED(X86_64)
  798. jump_to_copy,
  799. #endif
  800. NULL,
  801. };
  802. void board_init_f(ulong boot_flags)
  803. {
  804. gd->flags = boot_flags;
  805. gd->have_console = 0;
  806. if (initcall_run_list(init_sequence_f))
  807. hang();
  808. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
  809. !defined(CONFIG_EFI_APP) && !CONFIG_IS_ENABLED(X86_64)
  810. /* NOTREACHED - jump_to_copy() does not return */
  811. hang();
  812. #endif
  813. }
  814. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  815. /*
  816. * For now this code is only used on x86.
  817. *
  818. * init_sequence_f_r is the list of init functions which are run when
  819. * U-Boot is executing from Flash with a semi-limited 'C' environment.
  820. * The following limitations must be considered when implementing an
  821. * '_f_r' function:
  822. * - 'static' variables are read-only
  823. * - Global Data (gd->xxx) is read/write
  824. *
  825. * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
  826. * supported). It _should_, if possible, copy global data to RAM and
  827. * initialise the CPU caches (to speed up the relocation process)
  828. *
  829. * NOTE: At present only x86 uses this route, but it is intended that
  830. * all archs will move to this when generic relocation is implemented.
  831. */
  832. static const init_fnc_t init_sequence_f_r[] = {
  833. #if !CONFIG_IS_ENABLED(X86_64)
  834. init_cache_f_r,
  835. #endif
  836. NULL,
  837. };
  838. void board_init_f_r(void)
  839. {
  840. if (initcall_run_list(init_sequence_f_r))
  841. hang();
  842. /*
  843. * The pre-relocation drivers may be using memory that has now gone
  844. * away. Mark serial as unavailable - this will fall back to the debug
  845. * UART if available.
  846. */
  847. gd->flags &= ~GD_FLG_SERIAL_READY;
  848. #ifdef CONFIG_TIMER
  849. gd->timer = NULL;
  850. #endif
  851. /*
  852. * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
  853. * Transfer execution from Flash to RAM by calculating the address
  854. * of the in-RAM copy of board_init_r() and calling it
  855. */
  856. (board_init_r + gd->reloc_off)((gd_t *)gd, gd->relocaddr);
  857. /* NOTREACHED - board_init_r() does not return */
  858. hang();
  859. }
  860. #endif /* CONFIG_X86 */