board_f.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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 = getenv_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. __maybe_unused
  189. static int zero_global_data(void)
  190. {
  191. memset((void *)gd, '\0', sizeof(gd_t));
  192. return 0;
  193. }
  194. static int setup_mon_len(void)
  195. {
  196. #if defined(__ARM__) || defined(__MICROBLAZE__)
  197. gd->mon_len = (ulong)&__bss_end - (ulong)_start;
  198. #elif defined(CONFIG_SANDBOX) || defined(CONFIG_EFI_APP)
  199. gd->mon_len = (ulong)&_end - (ulong)_init;
  200. #elif defined(CONFIG_NIOS2) || defined(CONFIG_XTENSA)
  201. gd->mon_len = CONFIG_SYS_MONITOR_LEN;
  202. #elif defined(CONFIG_NDS32) || defined(CONFIG_SH)
  203. gd->mon_len = (ulong)(&__bss_end) - (ulong)(&_start);
  204. #elif defined(CONFIG_SYS_MONITOR_BASE)
  205. /* TODO: use (ulong)&__bss_end - (ulong)&__text_start; ? */
  206. gd->mon_len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
  207. #endif
  208. return 0;
  209. }
  210. __weak int arch_cpu_init(void)
  211. {
  212. return 0;
  213. }
  214. __weak int mach_cpu_init(void)
  215. {
  216. return 0;
  217. }
  218. /* Get the top of usable RAM */
  219. __weak ulong board_get_usable_ram_top(ulong total_size)
  220. {
  221. #ifdef CONFIG_SYS_SDRAM_BASE
  222. /*
  223. * Detect whether we have so much RAM that it goes past the end of our
  224. * 32-bit address space. If so, clip the usable RAM so it doesn't.
  225. */
  226. if (gd->ram_top < CONFIG_SYS_SDRAM_BASE)
  227. /*
  228. * Will wrap back to top of 32-bit space when reservations
  229. * are made.
  230. */
  231. return 0;
  232. #endif
  233. return gd->ram_top;
  234. }
  235. static int setup_dest_addr(void)
  236. {
  237. debug("Monitor len: %08lX\n", gd->mon_len);
  238. /*
  239. * Ram is setup, size stored in gd !!
  240. */
  241. debug("Ram size: %08lX\n", (ulong)gd->ram_size);
  242. #if defined(CONFIG_SYS_MEM_TOP_HIDE)
  243. /*
  244. * Subtract specified amount of memory to hide so that it won't
  245. * get "touched" at all by U-Boot. By fixing up gd->ram_size
  246. * the Linux kernel should now get passed the now "corrected"
  247. * memory size and won't touch it either. This should work
  248. * for arch/ppc and arch/powerpc. Only Linux board ports in
  249. * arch/powerpc with bootwrapper support, that recalculate the
  250. * memory size from the SDRAM controller setup will have to
  251. * get fixed.
  252. */
  253. gd->ram_size -= CONFIG_SYS_MEM_TOP_HIDE;
  254. #endif
  255. #ifdef CONFIG_SYS_SDRAM_BASE
  256. gd->ram_top = CONFIG_SYS_SDRAM_BASE;
  257. #endif
  258. gd->ram_top += get_effective_memsize();
  259. gd->ram_top = board_get_usable_ram_top(gd->mon_len);
  260. gd->relocaddr = gd->ram_top;
  261. debug("Ram top: %08lX\n", (ulong)gd->ram_top);
  262. #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
  263. /*
  264. * We need to make sure the location we intend to put secondary core
  265. * boot code is reserved and not used by any part of u-boot
  266. */
  267. if (gd->relocaddr > determine_mp_bootpg(NULL)) {
  268. gd->relocaddr = determine_mp_bootpg(NULL);
  269. debug("Reserving MP boot page to %08lx\n", gd->relocaddr);
  270. }
  271. #endif
  272. return 0;
  273. }
  274. #if defined(CONFIG_LOGBUFFER)
  275. static int reserve_logbuffer(void)
  276. {
  277. #ifndef CONFIG_ALT_LB_ADDR
  278. /* reserve kernel log buffer */
  279. gd->relocaddr -= LOGBUFF_RESERVE;
  280. debug("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN,
  281. gd->relocaddr);
  282. #endif
  283. return 0;
  284. }
  285. #endif
  286. #ifdef CONFIG_PRAM
  287. /* reserve protected RAM */
  288. static int reserve_pram(void)
  289. {
  290. ulong reg;
  291. reg = getenv_ulong("pram", 10, CONFIG_PRAM);
  292. gd->relocaddr -= (reg << 10); /* size is in kB */
  293. debug("Reserving %ldk for protected RAM at %08lx\n", reg,
  294. gd->relocaddr);
  295. return 0;
  296. }
  297. #endif /* CONFIG_PRAM */
  298. /* Round memory pointer down to next 4 kB limit */
  299. static int reserve_round_4k(void)
  300. {
  301. gd->relocaddr &= ~(4096 - 1);
  302. return 0;
  303. }
  304. #ifdef CONFIG_ARM
  305. static int reserve_mmu(void)
  306. {
  307. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
  308. /* reserve TLB table */
  309. gd->arch.tlb_size = PGTABLE_SIZE;
  310. gd->relocaddr -= gd->arch.tlb_size;
  311. /* round down to next 64 kB limit */
  312. gd->relocaddr &= ~(0x10000 - 1);
  313. gd->arch.tlb_addr = gd->relocaddr;
  314. debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
  315. gd->arch.tlb_addr + gd->arch.tlb_size);
  316. #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
  317. /*
  318. * Record allocated tlb_addr in case gd->tlb_addr to be overwritten
  319. * with location within secure ram.
  320. */
  321. gd->arch.tlb_allocated = gd->arch.tlb_addr;
  322. #endif
  323. #endif
  324. return 0;
  325. }
  326. #endif
  327. static int reserve_video(void)
  328. {
  329. #ifdef CONFIG_DM_VIDEO
  330. ulong addr;
  331. int ret;
  332. addr = gd->relocaddr;
  333. ret = video_reserve(&addr);
  334. if (ret)
  335. return ret;
  336. gd->relocaddr = addr;
  337. #elif defined(CONFIG_LCD)
  338. # ifdef CONFIG_FB_ADDR
  339. gd->fb_base = CONFIG_FB_ADDR;
  340. # else
  341. /* reserve memory for LCD display (always full pages) */
  342. gd->relocaddr = lcd_setmem(gd->relocaddr);
  343. gd->fb_base = gd->relocaddr;
  344. # endif /* CONFIG_FB_ADDR */
  345. #elif defined(CONFIG_VIDEO) && \
  346. (!defined(CONFIG_PPC)) && \
  347. !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
  348. !defined(CONFIG_M68K)
  349. /* reserve memory for video display (always full pages) */
  350. gd->relocaddr = video_setmem(gd->relocaddr);
  351. gd->fb_base = gd->relocaddr;
  352. #endif
  353. return 0;
  354. }
  355. static int reserve_trace(void)
  356. {
  357. #ifdef CONFIG_TRACE
  358. gd->relocaddr -= CONFIG_TRACE_BUFFER_SIZE;
  359. gd->trace_buff = map_sysmem(gd->relocaddr, CONFIG_TRACE_BUFFER_SIZE);
  360. debug("Reserving %dk for trace data at: %08lx\n",
  361. CONFIG_TRACE_BUFFER_SIZE >> 10, gd->relocaddr);
  362. #endif
  363. return 0;
  364. }
  365. static int reserve_uboot(void)
  366. {
  367. /*
  368. * reserve memory for U-Boot code, data & bss
  369. * round down to next 4 kB limit
  370. */
  371. gd->relocaddr -= gd->mon_len;
  372. gd->relocaddr &= ~(4096 - 1);
  373. #ifdef CONFIG_E500
  374. /* round down to next 64 kB limit so that IVPR stays aligned */
  375. gd->relocaddr &= ~(65536 - 1);
  376. #endif
  377. debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10,
  378. gd->relocaddr);
  379. gd->start_addr_sp = gd->relocaddr;
  380. return 0;
  381. }
  382. /* reserve memory for malloc() area */
  383. static int reserve_malloc(void)
  384. {
  385. gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
  386. debug("Reserving %dk for malloc() at: %08lx\n",
  387. TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
  388. return 0;
  389. }
  390. /* (permanently) allocate a Board Info struct */
  391. static int reserve_board(void)
  392. {
  393. if (!gd->bd) {
  394. gd->start_addr_sp -= sizeof(bd_t);
  395. gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
  396. memset(gd->bd, '\0', sizeof(bd_t));
  397. debug("Reserving %zu Bytes for Board Info at: %08lx\n",
  398. sizeof(bd_t), gd->start_addr_sp);
  399. }
  400. return 0;
  401. }
  402. static int setup_machine(void)
  403. {
  404. #ifdef CONFIG_MACH_TYPE
  405. gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
  406. #endif
  407. return 0;
  408. }
  409. static int reserve_global_data(void)
  410. {
  411. gd->start_addr_sp -= sizeof(gd_t);
  412. gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
  413. debug("Reserving %zu Bytes for Global Data at: %08lx\n",
  414. sizeof(gd_t), gd->start_addr_sp);
  415. return 0;
  416. }
  417. static int reserve_fdt(void)
  418. {
  419. #ifndef CONFIG_OF_EMBED
  420. /*
  421. * If the device tree is sitting immediately above our image then we
  422. * must relocate it. If it is embedded in the data section, then it
  423. * will be relocated with other data.
  424. */
  425. if (gd->fdt_blob) {
  426. gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
  427. gd->start_addr_sp -= gd->fdt_size;
  428. gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
  429. debug("Reserving %lu Bytes for FDT at: %08lx\n",
  430. gd->fdt_size, gd->start_addr_sp);
  431. }
  432. #endif
  433. return 0;
  434. }
  435. static int reserve_bootstage(void)
  436. {
  437. #ifdef CONFIG_BOOTSTAGE
  438. int size = bootstage_get_size();
  439. gd->start_addr_sp -= size;
  440. gd->new_bootstage = map_sysmem(gd->start_addr_sp, size);
  441. debug("Reserving %#x Bytes for bootstage at: %08lx\n", size,
  442. gd->start_addr_sp);
  443. #endif
  444. return 0;
  445. }
  446. int arch_reserve_stacks(void)
  447. {
  448. return 0;
  449. }
  450. static int reserve_stacks(void)
  451. {
  452. /* make stack pointer 16-byte aligned */
  453. gd->start_addr_sp -= 16;
  454. gd->start_addr_sp &= ~0xf;
  455. /*
  456. * let the architecture-specific code tailor gd->start_addr_sp and
  457. * gd->irq_sp
  458. */
  459. return arch_reserve_stacks();
  460. }
  461. static int display_new_sp(void)
  462. {
  463. debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
  464. return 0;
  465. }
  466. #if defined(CONFIG_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
  467. defined(CONFIG_SH)
  468. static int setup_board_part1(void)
  469. {
  470. bd_t *bd = gd->bd;
  471. /*
  472. * Save local variables to board info struct
  473. */
  474. bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of memory */
  475. bd->bi_memsize = gd->ram_size; /* size in bytes */
  476. #ifdef CONFIG_SYS_SRAM_BASE
  477. bd->bi_sramstart = CONFIG_SYS_SRAM_BASE; /* start of SRAM */
  478. bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE; /* size of SRAM */
  479. #endif
  480. #if defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
  481. bd->bi_immr_base = CONFIG_SYS_IMMR; /* base of IMMR register */
  482. #endif
  483. #if defined(CONFIG_MPC5xxx) || defined(CONFIG_M68K)
  484. bd->bi_mbar_base = CONFIG_SYS_MBAR; /* base of internal registers */
  485. #endif
  486. #if defined(CONFIG_MPC83xx)
  487. bd->bi_immrbar = CONFIG_SYS_IMMR;
  488. #endif
  489. return 0;
  490. }
  491. #endif
  492. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  493. static int setup_board_part2(void)
  494. {
  495. bd_t *bd = gd->bd;
  496. bd->bi_intfreq = gd->cpu_clk; /* Internal Freq, in Hz */
  497. bd->bi_busfreq = gd->bus_clk; /* Bus Freq, in Hz */
  498. #if defined(CONFIG_CPM2)
  499. bd->bi_cpmfreq = gd->arch.cpm_clk;
  500. bd->bi_brgfreq = gd->arch.brg_clk;
  501. bd->bi_sccfreq = gd->arch.scc_clk;
  502. bd->bi_vco = gd->arch.vco_out;
  503. #endif /* CONFIG_CPM2 */
  504. #if defined(CONFIG_MPC512X)
  505. bd->bi_ipsfreq = gd->arch.ips_clk;
  506. #endif /* CONFIG_MPC512X */
  507. #if defined(CONFIG_MPC5xxx)
  508. bd->bi_ipbfreq = gd->arch.ipb_clk;
  509. bd->bi_pcifreq = gd->pci_clk;
  510. #endif /* CONFIG_MPC5xxx */
  511. #if defined(CONFIG_M68K) && defined(CONFIG_PCI)
  512. bd->bi_pcifreq = gd->pci_clk;
  513. #endif
  514. #if defined(CONFIG_EXTRA_CLOCK)
  515. bd->bi_inpfreq = gd->arch.inp_clk; /* input Freq in Hz */
  516. bd->bi_vcofreq = gd->arch.vco_clk; /* vco Freq in Hz */
  517. bd->bi_flbfreq = gd->arch.flb_clk; /* flexbus Freq in Hz */
  518. #endif
  519. return 0;
  520. }
  521. #endif
  522. #ifdef CONFIG_POST
  523. static int init_post(void)
  524. {
  525. post_bootmode_init();
  526. post_run(NULL, POST_ROM | post_bootmode_get(0));
  527. return 0;
  528. }
  529. #endif
  530. static int reloc_fdt(void)
  531. {
  532. #ifndef CONFIG_OF_EMBED
  533. if (gd->flags & GD_FLG_SKIP_RELOC)
  534. return 0;
  535. if (gd->new_fdt) {
  536. memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
  537. gd->fdt_blob = gd->new_fdt;
  538. }
  539. #endif
  540. return 0;
  541. }
  542. static int reloc_bootstage(void)
  543. {
  544. #ifdef CONFIG_BOOTSTAGE
  545. if (gd->flags & GD_FLG_SKIP_RELOC)
  546. return 0;
  547. if (gd->new_bootstage) {
  548. int size = bootstage_get_size();
  549. debug("Copying bootstage from %p to %p, size %x\n",
  550. gd->bootstage, gd->new_bootstage, size);
  551. memcpy(gd->new_bootstage, gd->bootstage, size);
  552. gd->bootstage = gd->new_bootstage;
  553. }
  554. #endif
  555. return 0;
  556. }
  557. static int setup_reloc(void)
  558. {
  559. if (gd->flags & GD_FLG_SKIP_RELOC) {
  560. debug("Skipping relocation due to flag\n");
  561. return 0;
  562. }
  563. #ifdef CONFIG_SYS_TEXT_BASE
  564. #ifdef ARM
  565. gd->reloc_off = gd->relocaddr - (unsigned long)__image_copy_start;
  566. #elif defined(CONFIG_M68K)
  567. /*
  568. * On all ColdFire arch cpu, monitor code starts always
  569. * just after the default vector table location, so at 0x400
  570. */
  571. gd->reloc_off = gd->relocaddr - (CONFIG_SYS_TEXT_BASE + 0x400);
  572. #else
  573. gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
  574. #endif
  575. #endif
  576. memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
  577. debug("Relocation Offset is: %08lx\n", gd->reloc_off);
  578. debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
  579. gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
  580. gd->start_addr_sp);
  581. return 0;
  582. }
  583. #ifdef CONFIG_OF_BOARD_FIXUP
  584. static int fix_fdt(void)
  585. {
  586. return board_fix_fdt((void *)gd->fdt_blob);
  587. }
  588. #endif
  589. /* ARM calls relocate_code from its crt0.S */
  590. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
  591. !CONFIG_IS_ENABLED(X86_64)
  592. static int jump_to_copy(void)
  593. {
  594. if (gd->flags & GD_FLG_SKIP_RELOC)
  595. return 0;
  596. /*
  597. * x86 is special, but in a nice way. It uses a trampoline which
  598. * enables the dcache if possible.
  599. *
  600. * For now, other archs use relocate_code(), which is implemented
  601. * similarly for all archs. When we do generic relocation, hopefully
  602. * we can make all archs enable the dcache prior to relocation.
  603. */
  604. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  605. /*
  606. * SDRAM and console are now initialised. The final stack can now
  607. * be setup in SDRAM. Code execution will continue in Flash, but
  608. * with the stack in SDRAM and Global Data in temporary memory
  609. * (CPU cache)
  610. */
  611. arch_setup_gd(gd->new_gd);
  612. board_init_f_r_trampoline(gd->start_addr_sp);
  613. #else
  614. relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
  615. #endif
  616. return 0;
  617. }
  618. #endif
  619. /* Record the board_init_f() bootstage (after arch_cpu_init()) */
  620. static int initf_bootstage(void)
  621. {
  622. bool from_spl = IS_ENABLED(CONFIG_SPL_BOOTSTAGE) &&
  623. IS_ENABLED(CONFIG_BOOTSTAGE_STASH);
  624. int ret;
  625. ret = bootstage_init(!from_spl);
  626. if (ret)
  627. return ret;
  628. if (from_spl) {
  629. const void *stash = map_sysmem(CONFIG_BOOTSTAGE_STASH_ADDR,
  630. CONFIG_BOOTSTAGE_STASH_SIZE);
  631. ret = bootstage_unstash(stash, CONFIG_BOOTSTAGE_STASH_SIZE);
  632. if (ret && ret != -ENOENT) {
  633. debug("Failed to unstash bootstage: err=%d\n", ret);
  634. return ret;
  635. }
  636. }
  637. bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
  638. return 0;
  639. }
  640. static int initf_console_record(void)
  641. {
  642. #if defined(CONFIG_CONSOLE_RECORD) && defined(CONFIG_SYS_MALLOC_F_LEN)
  643. return console_record_init();
  644. #else
  645. return 0;
  646. #endif
  647. }
  648. static int initf_dm(void)
  649. {
  650. #if defined(CONFIG_DM) && defined(CONFIG_SYS_MALLOC_F_LEN)
  651. int ret;
  652. bootstage_start(BOOTSTATE_ID_ACCUM_DM_F, "dm_f");
  653. ret = dm_init_and_scan(true);
  654. bootstage_accum(BOOTSTATE_ID_ACCUM_DM_F);
  655. if (ret)
  656. return ret;
  657. #endif
  658. #ifdef CONFIG_TIMER_EARLY
  659. ret = dm_timer_init();
  660. if (ret)
  661. return ret;
  662. #endif
  663. return 0;
  664. }
  665. /* Architecture-specific memory reservation */
  666. __weak int reserve_arch(void)
  667. {
  668. return 0;
  669. }
  670. __weak int arch_cpu_init_dm(void)
  671. {
  672. return 0;
  673. }
  674. static const init_fnc_t init_sequence_f[] = {
  675. setup_mon_len,
  676. #ifdef CONFIG_OF_CONTROL
  677. fdtdec_setup,
  678. #endif
  679. #ifdef CONFIG_TRACE
  680. trace_early_init,
  681. #endif
  682. initf_malloc,
  683. initf_bootstage, /* uses its own timer, so does not need DM */
  684. initf_console_record,
  685. #if defined(CONFIG_HAVE_FSP)
  686. arch_fsp_init,
  687. #endif
  688. arch_cpu_init, /* basic arch cpu dependent setup */
  689. mach_cpu_init, /* SoC/machine dependent CPU setup */
  690. initf_dm,
  691. arch_cpu_init_dm,
  692. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  693. board_early_init_f,
  694. #endif
  695. #if defined(CONFIG_PPC) || defined(CONFIG_SYS_FSL_CLK) || defined(CONFIG_M68K)
  696. /* get CPU and bus clocks according to the environment variable */
  697. get_clocks, /* get CPU and bus clocks (etc.) */
  698. #endif
  699. #if !defined(CONFIG_M68K)
  700. timer_init, /* initialize timer */
  701. #endif
  702. #if defined(CONFIG_BOARD_POSTCLK_INIT)
  703. board_postclk_init,
  704. #endif
  705. env_init, /* initialize environment */
  706. init_baud_rate, /* initialze baudrate settings */
  707. serial_init, /* serial communications setup */
  708. console_init_f, /* stage 1 init of console */
  709. display_options, /* say that we are here */
  710. display_text_info, /* show debugging info if required */
  711. #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SH) || \
  712. defined(CONFIG_X86)
  713. checkcpu,
  714. #endif
  715. #if defined(CONFIG_DISPLAY_CPUINFO)
  716. print_cpuinfo, /* display cpu info (and speed) */
  717. #endif
  718. #if defined(CONFIG_DISPLAY_BOARDINFO)
  719. show_board_info,
  720. #endif
  721. INIT_FUNC_WATCHDOG_INIT
  722. #if defined(CONFIG_MISC_INIT_F)
  723. misc_init_f,
  724. #endif
  725. INIT_FUNC_WATCHDOG_RESET
  726. #if defined(CONFIG_SYS_I2C)
  727. init_func_i2c,
  728. #endif
  729. #if defined(CONFIG_HARD_SPI)
  730. init_func_spi,
  731. #endif
  732. announce_dram_init,
  733. dram_init, /* configure available RAM banks */
  734. #ifdef CONFIG_POST
  735. post_init_f,
  736. #endif
  737. INIT_FUNC_WATCHDOG_RESET
  738. #if defined(CONFIG_SYS_DRAM_TEST)
  739. testdram,
  740. #endif /* CONFIG_SYS_DRAM_TEST */
  741. INIT_FUNC_WATCHDOG_RESET
  742. #ifdef CONFIG_POST
  743. init_post,
  744. #endif
  745. INIT_FUNC_WATCHDOG_RESET
  746. /*
  747. * Now that we have DRAM mapped and working, we can
  748. * relocate the code and continue running from DRAM.
  749. *
  750. * Reserve memory at end of RAM for (top down in that order):
  751. * - area that won't get touched by U-Boot and Linux (optional)
  752. * - kernel log buffer
  753. * - protected RAM
  754. * - LCD framebuffer
  755. * - monitor code
  756. * - board info struct
  757. */
  758. setup_dest_addr,
  759. #if defined(CONFIG_LOGBUFFER)
  760. reserve_logbuffer,
  761. #endif
  762. #ifdef CONFIG_PRAM
  763. reserve_pram,
  764. #endif
  765. reserve_round_4k,
  766. #ifdef CONFIG_ARM
  767. reserve_mmu,
  768. #endif
  769. reserve_video,
  770. reserve_trace,
  771. reserve_uboot,
  772. reserve_malloc,
  773. reserve_board,
  774. setup_machine,
  775. reserve_global_data,
  776. reserve_fdt,
  777. reserve_bootstage,
  778. reserve_arch,
  779. reserve_stacks,
  780. dram_init_banksize,
  781. show_dram_config,
  782. #if defined(CONFIG_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
  783. defined(CONFIG_SH)
  784. setup_board_part1,
  785. #endif
  786. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  787. INIT_FUNC_WATCHDOG_RESET
  788. setup_board_part2,
  789. #endif
  790. display_new_sp,
  791. #ifdef CONFIG_SYS_EXTBDINFO
  792. setup_board_extra,
  793. #endif
  794. #ifdef CONFIG_OF_BOARD_FIXUP
  795. fix_fdt,
  796. #endif
  797. INIT_FUNC_WATCHDOG_RESET
  798. reloc_fdt,
  799. reloc_bootstage,
  800. setup_reloc,
  801. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  802. copy_uboot_to_ram,
  803. do_elf_reloc_fixups,
  804. clear_bss,
  805. #endif
  806. #if defined(CONFIG_XTENSA)
  807. clear_bss,
  808. #endif
  809. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
  810. !CONFIG_IS_ENABLED(X86_64)
  811. jump_to_copy,
  812. #endif
  813. NULL,
  814. };
  815. void board_init_f(ulong boot_flags)
  816. {
  817. #ifdef CONFIG_SYS_GENERIC_GLOBAL_DATA
  818. /*
  819. * For some architectures, global data is initialized and used before
  820. * calling this function. The data should be preserved. For others,
  821. * CONFIG_SYS_GENERIC_GLOBAL_DATA should be defined and use the stack
  822. * here to host global data until relocation.
  823. */
  824. gd_t data;
  825. gd = &data;
  826. /*
  827. * Clear global data before it is accessed at debug print
  828. * in initcall_run_list. Otherwise the debug print probably
  829. * get the wrong value of gd->have_console.
  830. */
  831. zero_global_data();
  832. #endif
  833. gd->flags = boot_flags;
  834. gd->have_console = 0;
  835. if (initcall_run_list(init_sequence_f))
  836. hang();
  837. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
  838. !defined(CONFIG_EFI_APP) && !CONFIG_IS_ENABLED(X86_64)
  839. /* NOTREACHED - jump_to_copy() does not return */
  840. hang();
  841. #endif
  842. }
  843. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  844. /*
  845. * For now this code is only used on x86.
  846. *
  847. * init_sequence_f_r is the list of init functions which are run when
  848. * U-Boot is executing from Flash with a semi-limited 'C' environment.
  849. * The following limitations must be considered when implementing an
  850. * '_f_r' function:
  851. * - 'static' variables are read-only
  852. * - Global Data (gd->xxx) is read/write
  853. *
  854. * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
  855. * supported). It _should_, if possible, copy global data to RAM and
  856. * initialise the CPU caches (to speed up the relocation process)
  857. *
  858. * NOTE: At present only x86 uses this route, but it is intended that
  859. * all archs will move to this when generic relocation is implemented.
  860. */
  861. static const init_fnc_t init_sequence_f_r[] = {
  862. #if !CONFIG_IS_ENABLED(X86_64)
  863. init_cache_f_r,
  864. #endif
  865. NULL,
  866. };
  867. void board_init_f_r(void)
  868. {
  869. if (initcall_run_list(init_sequence_f_r))
  870. hang();
  871. /*
  872. * The pre-relocation drivers may be using memory that has now gone
  873. * away. Mark serial as unavailable - this will fall back to the debug
  874. * UART if available.
  875. */
  876. gd->flags &= ~GD_FLG_SERIAL_READY;
  877. /*
  878. * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
  879. * Transfer execution from Flash to RAM by calculating the address
  880. * of the in-RAM copy of board_init_r() and calling it
  881. */
  882. (board_init_r + gd->reloc_off)((gd_t *)gd, gd->relocaddr);
  883. /* NOTREACHED - board_init_r() does not return */
  884. hang();
  885. }
  886. #endif /* CONFIG_X86 */