board_f.c 24 KB

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