board_f.c 22 KB

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