board_f.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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 <linux/compiler.h>
  14. #include <version.h>
  15. #include <console.h>
  16. #include <environment.h>
  17. #include <dm.h>
  18. #include <fdtdec.h>
  19. #include <fs.h>
  20. #if defined(CONFIG_CMD_IDE)
  21. #include <ide.h>
  22. #endif
  23. #include <i2c.h>
  24. #include <initcall.h>
  25. #include <logbuff.h>
  26. #include <malloc.h>
  27. #include <mapmem.h>
  28. /* TODO: Can we move these into arch/ headers? */
  29. #ifdef CONFIG_8xx
  30. #include <mpc8xx.h>
  31. #endif
  32. #ifdef CONFIG_5xx
  33. #include <mpc5xx.h>
  34. #endif
  35. #ifdef CONFIG_MPC5xxx
  36. #include <mpc5xxx.h>
  37. #endif
  38. #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
  39. #include <asm/mp.h>
  40. #endif
  41. #include <os.h>
  42. #include <post.h>
  43. #include <spi.h>
  44. #include <status_led.h>
  45. #include <trace.h>
  46. #include <video.h>
  47. #include <watchdog.h>
  48. #include <asm/errno.h>
  49. #include <asm/io.h>
  50. #include <asm/sections.h>
  51. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  52. #include <asm/init_helpers.h>
  53. #include <asm/relocate.h>
  54. #endif
  55. #ifdef CONFIG_SANDBOX
  56. #include <asm/state.h>
  57. #endif
  58. #include <dm/root.h>
  59. #include <linux/compiler.h>
  60. /*
  61. * Pointer to initial global data area
  62. *
  63. * Here we initialize it if needed.
  64. */
  65. #ifdef XTRN_DECLARE_GLOBAL_DATA_PTR
  66. #undef XTRN_DECLARE_GLOBAL_DATA_PTR
  67. #define XTRN_DECLARE_GLOBAL_DATA_PTR /* empty = allocate here */
  68. DECLARE_GLOBAL_DATA_PTR = (gd_t *) (CONFIG_SYS_INIT_GD_ADDR);
  69. #else
  70. DECLARE_GLOBAL_DATA_PTR;
  71. #endif
  72. /*
  73. * TODO(sjg@chromium.org): IMO this code should be
  74. * refactored to a single function, something like:
  75. *
  76. * void led_set_state(enum led_colour_t colour, int on);
  77. */
  78. /************************************************************************
  79. * Coloured LED functionality
  80. ************************************************************************
  81. * May be supplied by boards if desired
  82. */
  83. __weak void coloured_LED_init(void) {}
  84. __weak void red_led_on(void) {}
  85. __weak void red_led_off(void) {}
  86. __weak void green_led_on(void) {}
  87. __weak void green_led_off(void) {}
  88. __weak void yellow_led_on(void) {}
  89. __weak void yellow_led_off(void) {}
  90. __weak void blue_led_on(void) {}
  91. __weak void blue_led_off(void) {}
  92. /*
  93. * Why is gd allocated a register? Prior to reloc it might be better to
  94. * just pass it around to each function in this file?
  95. *
  96. * After reloc one could argue that it is hardly used and doesn't need
  97. * to be in a register. Or if it is it should perhaps hold pointers to all
  98. * global data for all modules, so that post-reloc we can avoid the massive
  99. * literal pool we get on ARM. Or perhaps just encourage each module to use
  100. * a structure...
  101. */
  102. /*
  103. * Could the CONFIG_SPL_BUILD infection become a flag in gd?
  104. */
  105. #if defined(CONFIG_WATCHDOG) || defined(CONFIG_HW_WATCHDOG)
  106. static int init_func_watchdog_init(void)
  107. {
  108. # if defined(CONFIG_HW_WATCHDOG) && (defined(CONFIG_BLACKFIN) || \
  109. defined(CONFIG_M68K) || defined(CONFIG_MICROBLAZE) || \
  110. defined(CONFIG_SH) || defined(CONFIG_AT91SAM9_WATCHDOG) || \
  111. defined(CONFIG_IMX_WATCHDOG))
  112. hw_watchdog_init();
  113. # endif
  114. puts(" Watchdog enabled\n");
  115. WATCHDOG_RESET();
  116. return 0;
  117. }
  118. int init_func_watchdog_reset(void)
  119. {
  120. WATCHDOG_RESET();
  121. return 0;
  122. }
  123. #endif /* CONFIG_WATCHDOG */
  124. __weak void board_add_ram_info(int use_default)
  125. {
  126. /* please define platform specific board_add_ram_info() */
  127. }
  128. static int init_baud_rate(void)
  129. {
  130. gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
  131. return 0;
  132. }
  133. static int display_text_info(void)
  134. {
  135. #if !defined(CONFIG_SANDBOX) && !defined(CONFIG_EFI_APP)
  136. ulong bss_start, bss_end, text_base;
  137. bss_start = (ulong)&__bss_start;
  138. bss_end = (ulong)&__bss_end;
  139. #ifdef CONFIG_SYS_TEXT_BASE
  140. text_base = CONFIG_SYS_TEXT_BASE;
  141. #else
  142. text_base = CONFIG_SYS_MONITOR_BASE;
  143. #endif
  144. debug("U-Boot code: %08lX -> %08lX BSS: -> %08lX\n",
  145. text_base, bss_start, bss_end);
  146. #endif
  147. #ifdef CONFIG_MODEM_SUPPORT
  148. debug("Modem Support enabled\n");
  149. #endif
  150. #ifdef CONFIG_USE_IRQ
  151. debug("IRQ Stack: %08lx\n", IRQ_STACK_START);
  152. debug("FIQ Stack: %08lx\n", FIQ_STACK_START);
  153. #endif
  154. return 0;
  155. }
  156. static int announce_dram_init(void)
  157. {
  158. puts("DRAM: ");
  159. return 0;
  160. }
  161. #if defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_M68K)
  162. static int init_func_ram(void)
  163. {
  164. #ifdef CONFIG_BOARD_TYPES
  165. int board_type = gd->board_type;
  166. #else
  167. int board_type = 0; /* use dummy arg */
  168. #endif
  169. gd->ram_size = initdram(board_type);
  170. if (gd->ram_size > 0)
  171. return 0;
  172. puts("*** failed ***\n");
  173. return 1;
  174. }
  175. #endif
  176. static int show_dram_config(void)
  177. {
  178. unsigned long long size;
  179. #ifdef CONFIG_NR_DRAM_BANKS
  180. int i;
  181. debug("\nRAM Configuration:\n");
  182. for (i = size = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  183. size += gd->bd->bi_dram[i].size;
  184. debug("Bank #%d: %llx ", i,
  185. (unsigned long long)(gd->bd->bi_dram[i].start));
  186. #ifdef DEBUG
  187. print_size(gd->bd->bi_dram[i].size, "\n");
  188. #endif
  189. }
  190. debug("\nDRAM: ");
  191. #else
  192. size = gd->ram_size;
  193. #endif
  194. print_size(size, "");
  195. board_add_ram_info(0);
  196. putc('\n');
  197. return 0;
  198. }
  199. __weak void dram_init_banksize(void)
  200. {
  201. #if defined(CONFIG_NR_DRAM_BANKS) && defined(CONFIG_SYS_SDRAM_BASE)
  202. gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
  203. gd->bd->bi_dram[0].size = get_effective_memsize();
  204. #endif
  205. }
  206. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
  207. static int init_func_i2c(void)
  208. {
  209. puts("I2C: ");
  210. #ifdef CONFIG_SYS_I2C
  211. i2c_init_all();
  212. #else
  213. i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  214. #endif
  215. puts("ready\n");
  216. return 0;
  217. }
  218. #endif
  219. #if defined(CONFIG_HARD_SPI)
  220. static int init_func_spi(void)
  221. {
  222. puts("SPI: ");
  223. spi_init();
  224. puts("ready\n");
  225. return 0;
  226. }
  227. #endif
  228. __maybe_unused
  229. static int zero_global_data(void)
  230. {
  231. memset((void *)gd, '\0', sizeof(gd_t));
  232. return 0;
  233. }
  234. static int setup_mon_len(void)
  235. {
  236. #if defined(__ARM__) || defined(__MICROBLAZE__)
  237. gd->mon_len = (ulong)&__bss_end - (ulong)_start;
  238. #elif defined(CONFIG_SANDBOX) || defined(CONFIG_EFI_APP)
  239. gd->mon_len = (ulong)&_end - (ulong)_init;
  240. #elif defined(CONFIG_BLACKFIN) || defined(CONFIG_NIOS2)
  241. gd->mon_len = CONFIG_SYS_MONITOR_LEN;
  242. #elif defined(CONFIG_NDS32)
  243. gd->mon_len = (ulong)(&__bss_end) - (ulong)(&_start);
  244. #else
  245. /* TODO: use (ulong)&__bss_end - (ulong)&__text_start; ? */
  246. gd->mon_len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
  247. #endif
  248. return 0;
  249. }
  250. __weak int arch_cpu_init(void)
  251. {
  252. return 0;
  253. }
  254. #ifdef CONFIG_SANDBOX
  255. static int setup_ram_buf(void)
  256. {
  257. struct sandbox_state *state = state_get_current();
  258. gd->arch.ram_buf = state->ram_buf;
  259. gd->ram_size = state->ram_size;
  260. return 0;
  261. }
  262. #endif
  263. /* Get the top of usable RAM */
  264. __weak ulong board_get_usable_ram_top(ulong total_size)
  265. {
  266. #ifdef CONFIG_SYS_SDRAM_BASE
  267. /*
  268. * Detect whether we have so much RAM that it goes past the end of our
  269. * 32-bit address space. If so, clip the usable RAM so it doesn't.
  270. */
  271. if (gd->ram_top < CONFIG_SYS_SDRAM_BASE)
  272. /*
  273. * Will wrap back to top of 32-bit space when reservations
  274. * are made.
  275. */
  276. return 0;
  277. #endif
  278. return gd->ram_top;
  279. }
  280. __weak phys_size_t board_reserve_ram_top(phys_size_t ram_size)
  281. {
  282. #ifdef CONFIG_SYS_MEM_TOP_HIDE
  283. return ram_size - CONFIG_SYS_MEM_TOP_HIDE;
  284. #else
  285. return ram_size;
  286. #endif
  287. }
  288. static int setup_dest_addr(void)
  289. {
  290. debug("Monitor len: %08lX\n", gd->mon_len);
  291. /*
  292. * Ram is setup, size stored in gd !!
  293. */
  294. debug("Ram size: %08lX\n", (ulong)gd->ram_size);
  295. #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
  296. /* Reserve memory for secure MMU tables, and/or security monitor */
  297. gd->ram_size -= CONFIG_SYS_MEM_RESERVE_SECURE;
  298. /*
  299. * Record secure memory location. Need recalcuate if memory splits
  300. * into banks, or the ram base is not zero.
  301. */
  302. gd->secure_ram = gd->ram_size;
  303. #endif
  304. /*
  305. * Subtract specified amount of memory to hide so that it won't
  306. * get "touched" at all by U-Boot. By fixing up gd->ram_size
  307. * the Linux kernel should now get passed the now "corrected"
  308. * memory size and won't touch it either. This has been used
  309. * by arch/powerpc exclusively. Now ARMv8 takes advantage of
  310. * thie mechanism. If memory is split into banks, addresses
  311. * need to be calculated.
  312. */
  313. gd->ram_size = board_reserve_ram_top(gd->ram_size);
  314. #ifdef CONFIG_SYS_SDRAM_BASE
  315. gd->ram_top = CONFIG_SYS_SDRAM_BASE;
  316. #endif
  317. gd->ram_top += get_effective_memsize();
  318. gd->ram_top = board_get_usable_ram_top(gd->mon_len);
  319. gd->relocaddr = gd->ram_top;
  320. debug("Ram top: %08lX\n", (ulong)gd->ram_top);
  321. #if defined(CONFIG_MP) && (defined(CONFIG_MPC86xx) || defined(CONFIG_E500))
  322. /*
  323. * We need to make sure the location we intend to put secondary core
  324. * boot code is reserved and not used by any part of u-boot
  325. */
  326. if (gd->relocaddr > determine_mp_bootpg(NULL)) {
  327. gd->relocaddr = determine_mp_bootpg(NULL);
  328. debug("Reserving MP boot page to %08lx\n", gd->relocaddr);
  329. }
  330. #endif
  331. return 0;
  332. }
  333. #if defined(CONFIG_SPARC)
  334. static int reserve_prom(void)
  335. {
  336. /* defined in arch/sparc/cpu/leon?/prom.c */
  337. extern void *__prom_start_reloc;
  338. int size = 8192; /* page table = 2k, prom = 6k */
  339. gd->relocaddr -= size;
  340. __prom_start_reloc = map_sysmem(gd->relocaddr + 2048, size - 2048);
  341. debug("Reserving %dk for PROM and page table at %08lx\n", size,
  342. gd->relocaddr);
  343. return 0;
  344. }
  345. #endif
  346. #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
  347. static int reserve_logbuffer(void)
  348. {
  349. /* reserve kernel log buffer */
  350. gd->relocaddr -= LOGBUFF_RESERVE;
  351. debug("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN,
  352. gd->relocaddr);
  353. return 0;
  354. }
  355. #endif
  356. #ifdef CONFIG_PRAM
  357. /* reserve protected RAM */
  358. static int reserve_pram(void)
  359. {
  360. ulong reg;
  361. reg = getenv_ulong("pram", 10, CONFIG_PRAM);
  362. gd->relocaddr -= (reg << 10); /* size is in kB */
  363. debug("Reserving %ldk for protected RAM at %08lx\n", reg,
  364. gd->relocaddr);
  365. return 0;
  366. }
  367. #endif /* CONFIG_PRAM */
  368. /* Round memory pointer down to next 4 kB limit */
  369. static int reserve_round_4k(void)
  370. {
  371. gd->relocaddr &= ~(4096 - 1);
  372. return 0;
  373. }
  374. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
  375. defined(CONFIG_ARM)
  376. static int reserve_mmu(void)
  377. {
  378. /* reserve TLB table */
  379. gd->arch.tlb_size = PGTABLE_SIZE;
  380. gd->relocaddr -= gd->arch.tlb_size;
  381. /* round down to next 64 kB limit */
  382. gd->relocaddr &= ~(0x10000 - 1);
  383. gd->arch.tlb_addr = gd->relocaddr;
  384. debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
  385. gd->arch.tlb_addr + gd->arch.tlb_size);
  386. return 0;
  387. }
  388. #endif
  389. #ifdef CONFIG_DM_VIDEO
  390. static int reserve_video(void)
  391. {
  392. ulong addr;
  393. int ret;
  394. addr = gd->relocaddr;
  395. ret = video_reserve(&addr);
  396. if (ret)
  397. return ret;
  398. gd->relocaddr = addr;
  399. return 0;
  400. }
  401. #else
  402. # ifdef CONFIG_LCD
  403. static int reserve_lcd(void)
  404. {
  405. # ifdef CONFIG_FB_ADDR
  406. gd->fb_base = CONFIG_FB_ADDR;
  407. # else
  408. /* reserve memory for LCD display (always full pages) */
  409. gd->relocaddr = lcd_setmem(gd->relocaddr);
  410. gd->fb_base = gd->relocaddr;
  411. # endif /* CONFIG_FB_ADDR */
  412. return 0;
  413. }
  414. # endif /* CONFIG_LCD */
  415. # if defined(CONFIG_VIDEO) && (!defined(CONFIG_PPC) || defined(CONFIG_8xx)) && \
  416. !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
  417. !defined(CONFIG_BLACKFIN) && !defined(CONFIG_M68K)
  418. static int reserve_legacy_video(void)
  419. {
  420. /* reserve memory for video display (always full pages) */
  421. gd->relocaddr = video_setmem(gd->relocaddr);
  422. gd->fb_base = gd->relocaddr;
  423. return 0;
  424. }
  425. # endif
  426. #endif /* !CONFIG_DM_VIDEO */
  427. static int reserve_trace(void)
  428. {
  429. #ifdef CONFIG_TRACE
  430. gd->relocaddr -= CONFIG_TRACE_BUFFER_SIZE;
  431. gd->trace_buff = map_sysmem(gd->relocaddr, CONFIG_TRACE_BUFFER_SIZE);
  432. debug("Reserving %dk for trace data at: %08lx\n",
  433. CONFIG_TRACE_BUFFER_SIZE >> 10, gd->relocaddr);
  434. #endif
  435. return 0;
  436. }
  437. static int reserve_uboot(void)
  438. {
  439. /*
  440. * reserve memory for U-Boot code, data & bss
  441. * round down to next 4 kB limit
  442. */
  443. gd->relocaddr -= gd->mon_len;
  444. gd->relocaddr &= ~(4096 - 1);
  445. #ifdef CONFIG_E500
  446. /* round down to next 64 kB limit so that IVPR stays aligned */
  447. gd->relocaddr &= ~(65536 - 1);
  448. #endif
  449. debug("Reserving %ldk for U-Boot at: %08lx\n", gd->mon_len >> 10,
  450. gd->relocaddr);
  451. gd->start_addr_sp = gd->relocaddr;
  452. return 0;
  453. }
  454. #ifndef CONFIG_SPL_BUILD
  455. /* reserve memory for malloc() area */
  456. static int reserve_malloc(void)
  457. {
  458. gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
  459. debug("Reserving %dk for malloc() at: %08lx\n",
  460. TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
  461. return 0;
  462. }
  463. /* (permanently) allocate a Board Info struct */
  464. static int reserve_board(void)
  465. {
  466. if (!gd->bd) {
  467. gd->start_addr_sp -= sizeof(bd_t);
  468. gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
  469. memset(gd->bd, '\0', sizeof(bd_t));
  470. debug("Reserving %zu Bytes for Board Info at: %08lx\n",
  471. sizeof(bd_t), gd->start_addr_sp);
  472. }
  473. return 0;
  474. }
  475. #endif
  476. static int setup_machine(void)
  477. {
  478. #ifdef CONFIG_MACH_TYPE
  479. gd->bd->bi_arch_number = CONFIG_MACH_TYPE; /* board id for Linux */
  480. #endif
  481. return 0;
  482. }
  483. static int reserve_global_data(void)
  484. {
  485. gd->start_addr_sp -= sizeof(gd_t);
  486. gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
  487. debug("Reserving %zu Bytes for Global Data at: %08lx\n",
  488. sizeof(gd_t), gd->start_addr_sp);
  489. return 0;
  490. }
  491. static int reserve_fdt(void)
  492. {
  493. #ifndef CONFIG_OF_EMBED
  494. /*
  495. * If the device tree is sitting immediately above our image then we
  496. * must relocate it. If it is embedded in the data section, then it
  497. * will be relocated with other data.
  498. */
  499. if (gd->fdt_blob) {
  500. gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
  501. gd->start_addr_sp -= gd->fdt_size;
  502. gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
  503. debug("Reserving %lu Bytes for FDT at: %08lx\n",
  504. gd->fdt_size, gd->start_addr_sp);
  505. }
  506. #endif
  507. return 0;
  508. }
  509. int arch_reserve_stacks(void)
  510. {
  511. return 0;
  512. }
  513. static int reserve_stacks(void)
  514. {
  515. /* make stack pointer 16-byte aligned */
  516. gd->start_addr_sp -= 16;
  517. gd->start_addr_sp &= ~0xf;
  518. /*
  519. * let the architecture-specific code tailor gd->start_addr_sp and
  520. * gd->irq_sp
  521. */
  522. return arch_reserve_stacks();
  523. }
  524. static int display_new_sp(void)
  525. {
  526. debug("New Stack Pointer is: %08lx\n", gd->start_addr_sp);
  527. return 0;
  528. }
  529. #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
  530. static int setup_board_part1(void)
  531. {
  532. bd_t *bd = gd->bd;
  533. /*
  534. * Save local variables to board info struct
  535. */
  536. bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of memory */
  537. bd->bi_memsize = gd->ram_size; /* size in bytes */
  538. #ifdef CONFIG_SYS_SRAM_BASE
  539. bd->bi_sramstart = CONFIG_SYS_SRAM_BASE; /* start of SRAM */
  540. bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE; /* size of SRAM */
  541. #endif
  542. #if defined(CONFIG_8xx) || defined(CONFIG_MPC8260) || defined(CONFIG_5xx) || \
  543. defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
  544. bd->bi_immr_base = CONFIG_SYS_IMMR; /* base of IMMR register */
  545. #endif
  546. #if defined(CONFIG_MPC5xxx) || defined(CONFIG_M68K)
  547. bd->bi_mbar_base = CONFIG_SYS_MBAR; /* base of internal registers */
  548. #endif
  549. #if defined(CONFIG_MPC83xx)
  550. bd->bi_immrbar = CONFIG_SYS_IMMR;
  551. #endif
  552. return 0;
  553. }
  554. #endif
  555. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  556. static int setup_board_part2(void)
  557. {
  558. bd_t *bd = gd->bd;
  559. bd->bi_intfreq = gd->cpu_clk; /* Internal Freq, in Hz */
  560. bd->bi_busfreq = gd->bus_clk; /* Bus Freq, in Hz */
  561. #if defined(CONFIG_CPM2)
  562. bd->bi_cpmfreq = gd->arch.cpm_clk;
  563. bd->bi_brgfreq = gd->arch.brg_clk;
  564. bd->bi_sccfreq = gd->arch.scc_clk;
  565. bd->bi_vco = gd->arch.vco_out;
  566. #endif /* CONFIG_CPM2 */
  567. #if defined(CONFIG_MPC512X)
  568. bd->bi_ipsfreq = gd->arch.ips_clk;
  569. #endif /* CONFIG_MPC512X */
  570. #if defined(CONFIG_MPC5xxx)
  571. bd->bi_ipbfreq = gd->arch.ipb_clk;
  572. bd->bi_pcifreq = gd->pci_clk;
  573. #endif /* CONFIG_MPC5xxx */
  574. #if defined(CONFIG_M68K) && defined(CONFIG_PCI)
  575. bd->bi_pcifreq = gd->pci_clk;
  576. #endif
  577. #if defined(CONFIG_EXTRA_CLOCK)
  578. bd->bi_inpfreq = gd->arch.inp_clk; /* input Freq in Hz */
  579. bd->bi_vcofreq = gd->arch.vco_clk; /* vco Freq in Hz */
  580. bd->bi_flbfreq = gd->arch.flb_clk; /* flexbus Freq in Hz */
  581. #endif
  582. return 0;
  583. }
  584. #endif
  585. #ifdef CONFIG_SYS_EXTBDINFO
  586. static int setup_board_extra(void)
  587. {
  588. bd_t *bd = gd->bd;
  589. strncpy((char *) bd->bi_s_version, "1.2", sizeof(bd->bi_s_version));
  590. strncpy((char *) bd->bi_r_version, U_BOOT_VERSION,
  591. sizeof(bd->bi_r_version));
  592. bd->bi_procfreq = gd->cpu_clk; /* Processor Speed, In Hz */
  593. bd->bi_plb_busfreq = gd->bus_clk;
  594. #if defined(CONFIG_405GP) || defined(CONFIG_405EP) || \
  595. defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
  596. defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  597. bd->bi_pci_busfreq = get_PCI_freq();
  598. bd->bi_opbfreq = get_OPB_freq();
  599. #elif defined(CONFIG_XILINX_405)
  600. bd->bi_pci_busfreq = get_PCI_freq();
  601. #endif
  602. return 0;
  603. }
  604. #endif
  605. #ifdef CONFIG_POST
  606. static int init_post(void)
  607. {
  608. post_bootmode_init();
  609. post_run(NULL, POST_ROM | post_bootmode_get(0));
  610. return 0;
  611. }
  612. #endif
  613. static int setup_dram_config(void)
  614. {
  615. /* Ram is board specific, so move it to board code ... */
  616. dram_init_banksize();
  617. return 0;
  618. }
  619. static int reloc_fdt(void)
  620. {
  621. #ifndef CONFIG_OF_EMBED
  622. if (gd->flags & GD_FLG_SKIP_RELOC)
  623. return 0;
  624. if (gd->new_fdt) {
  625. memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
  626. gd->fdt_blob = gd->new_fdt;
  627. }
  628. #endif
  629. return 0;
  630. }
  631. static int setup_reloc(void)
  632. {
  633. if (gd->flags & GD_FLG_SKIP_RELOC) {
  634. debug("Skipping relocation due to flag\n");
  635. return 0;
  636. }
  637. #ifdef CONFIG_SYS_TEXT_BASE
  638. gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
  639. #ifdef CONFIG_M68K
  640. /*
  641. * On all ColdFire arch cpu, monitor code starts always
  642. * just after the default vector table location, so at 0x400
  643. */
  644. gd->reloc_off = gd->relocaddr - (CONFIG_SYS_TEXT_BASE + 0x400);
  645. #endif
  646. #endif
  647. memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
  648. debug("Relocation Offset is: %08lx\n", gd->reloc_off);
  649. debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
  650. gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
  651. gd->start_addr_sp);
  652. return 0;
  653. }
  654. /* ARM calls relocate_code from its crt0.S */
  655. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
  656. static int jump_to_copy(void)
  657. {
  658. if (gd->flags & GD_FLG_SKIP_RELOC)
  659. return 0;
  660. /*
  661. * x86 is special, but in a nice way. It uses a trampoline which
  662. * enables the dcache if possible.
  663. *
  664. * For now, other archs use relocate_code(), which is implemented
  665. * similarly for all archs. When we do generic relocation, hopefully
  666. * we can make all archs enable the dcache prior to relocation.
  667. */
  668. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  669. /*
  670. * SDRAM and console are now initialised. The final stack can now
  671. * be setup in SDRAM. Code execution will continue in Flash, but
  672. * with the stack in SDRAM and Global Data in temporary memory
  673. * (CPU cache)
  674. */
  675. arch_setup_gd(gd->new_gd);
  676. board_init_f_r_trampoline(gd->start_addr_sp);
  677. #else
  678. relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
  679. #endif
  680. return 0;
  681. }
  682. #endif
  683. /* Record the board_init_f() bootstage (after arch_cpu_init()) */
  684. static int mark_bootstage(void)
  685. {
  686. bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
  687. return 0;
  688. }
  689. static int initf_console_record(void)
  690. {
  691. #if defined(CONFIG_CONSOLE_RECORD) && defined(CONFIG_SYS_MALLOC_F_LEN)
  692. return console_record_init();
  693. #else
  694. return 0;
  695. #endif
  696. }
  697. static int initf_dm(void)
  698. {
  699. #if defined(CONFIG_DM) && defined(CONFIG_SYS_MALLOC_F_LEN)
  700. int ret;
  701. ret = dm_init_and_scan(true);
  702. if (ret)
  703. return ret;
  704. #endif
  705. return 0;
  706. }
  707. /* Architecture-specific memory reservation */
  708. __weak int reserve_arch(void)
  709. {
  710. return 0;
  711. }
  712. __weak int arch_cpu_init_dm(void)
  713. {
  714. return 0;
  715. }
  716. static init_fnc_t init_sequence_f[] = {
  717. #ifdef CONFIG_SANDBOX
  718. setup_ram_buf,
  719. #endif
  720. setup_mon_len,
  721. #ifdef CONFIG_OF_CONTROL
  722. fdtdec_setup,
  723. #endif
  724. #ifdef CONFIG_TRACE
  725. trace_early_init,
  726. #endif
  727. initf_malloc,
  728. initf_console_record,
  729. #if defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx)
  730. /* TODO: can this go into arch_cpu_init()? */
  731. probecpu,
  732. #endif
  733. #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
  734. x86_fsp_init,
  735. #endif
  736. arch_cpu_init, /* basic arch cpu dependent setup */
  737. initf_dm,
  738. arch_cpu_init_dm,
  739. mark_bootstage, /* need timer, go after init dm */
  740. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  741. board_early_init_f,
  742. #endif
  743. /* TODO: can any of this go into arch_cpu_init()? */
  744. #if defined(CONFIG_PPC) && !defined(CONFIG_8xx_CPUCLK_DEFAULT)
  745. get_clocks, /* get CPU and bus clocks (etc.) */
  746. #if defined(CONFIG_TQM8xxL) && !defined(CONFIG_TQM866M) \
  747. && !defined(CONFIG_TQM885D)
  748. adjust_sdram_tbs_8xx,
  749. #endif
  750. /* TODO: can we rename this to timer_init()? */
  751. init_timebase,
  752. #endif
  753. #if defined(CONFIG_ARM) || defined(CONFIG_MIPS) || \
  754. defined(CONFIG_BLACKFIN) || defined(CONFIG_NDS32) || \
  755. defined(CONFIG_SPARC)
  756. timer_init, /* initialize timer */
  757. #endif
  758. #ifdef CONFIG_SYS_ALLOC_DPRAM
  759. #if !defined(CONFIG_CPM2)
  760. dpram_init,
  761. #endif
  762. #endif
  763. #if defined(CONFIG_BOARD_POSTCLK_INIT)
  764. board_postclk_init,
  765. #endif
  766. #if defined(CONFIG_SYS_FSL_CLK) || defined(CONFIG_M68K)
  767. get_clocks,
  768. #endif
  769. env_init, /* initialize environment */
  770. #if defined(CONFIG_8xx_CPUCLK_DEFAULT)
  771. /* get CPU and bus clocks according to the environment variable */
  772. get_clocks_866,
  773. /* adjust sdram refresh rate according to the new clock */
  774. sdram_adjust_866,
  775. init_timebase,
  776. #endif
  777. init_baud_rate, /* initialze baudrate settings */
  778. serial_init, /* serial communications setup */
  779. console_init_f, /* stage 1 init of console */
  780. #ifdef CONFIG_SANDBOX
  781. sandbox_early_getopt_check,
  782. #endif
  783. #ifdef CONFIG_OF_CONTROL
  784. fdtdec_prepare_fdt,
  785. #endif
  786. display_options, /* say that we are here */
  787. display_text_info, /* show debugging info if required */
  788. #if defined(CONFIG_MPC8260)
  789. prt_8260_rsr,
  790. prt_8260_clks,
  791. #endif /* CONFIG_MPC8260 */
  792. #if defined(CONFIG_MPC83xx)
  793. prt_83xx_rsr,
  794. #endif
  795. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  796. checkcpu,
  797. #endif
  798. print_cpuinfo, /* display cpu info (and speed) */
  799. #if defined(CONFIG_MPC5xxx)
  800. prt_mpc5xxx_clks,
  801. #endif /* CONFIG_MPC5xxx */
  802. #if defined(CONFIG_DISPLAY_BOARDINFO)
  803. show_board_info,
  804. #endif
  805. INIT_FUNC_WATCHDOG_INIT
  806. #if defined(CONFIG_MISC_INIT_F)
  807. misc_init_f,
  808. #endif
  809. INIT_FUNC_WATCHDOG_RESET
  810. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
  811. init_func_i2c,
  812. #endif
  813. #if defined(CONFIG_HARD_SPI)
  814. init_func_spi,
  815. #endif
  816. announce_dram_init,
  817. /* TODO: unify all these dram functions? */
  818. #if defined(CONFIG_ARM) || defined(CONFIG_X86) || defined(CONFIG_NDS32) || \
  819. defined(CONFIG_MICROBLAZE) || defined(CONFIG_AVR32)
  820. dram_init, /* configure available RAM banks */
  821. #endif
  822. #if defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_M68K)
  823. init_func_ram,
  824. #endif
  825. #ifdef CONFIG_POST
  826. post_init_f,
  827. #endif
  828. INIT_FUNC_WATCHDOG_RESET
  829. #if defined(CONFIG_SYS_DRAM_TEST)
  830. testdram,
  831. #endif /* CONFIG_SYS_DRAM_TEST */
  832. INIT_FUNC_WATCHDOG_RESET
  833. #ifdef CONFIG_POST
  834. init_post,
  835. #endif
  836. INIT_FUNC_WATCHDOG_RESET
  837. /*
  838. * Now that we have DRAM mapped and working, we can
  839. * relocate the code and continue running from DRAM.
  840. *
  841. * Reserve memory at end of RAM for (top down in that order):
  842. * - area that won't get touched by U-Boot and Linux (optional)
  843. * - kernel log buffer
  844. * - protected RAM
  845. * - LCD framebuffer
  846. * - monitor code
  847. * - board info struct
  848. */
  849. setup_dest_addr,
  850. #if defined(CONFIG_BLACKFIN)
  851. /* Blackfin u-boot monitor should be on top of the ram */
  852. reserve_uboot,
  853. #endif
  854. #if defined(CONFIG_SPARC)
  855. reserve_prom,
  856. #endif
  857. #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
  858. reserve_logbuffer,
  859. #endif
  860. #ifdef CONFIG_PRAM
  861. reserve_pram,
  862. #endif
  863. reserve_round_4k,
  864. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
  865. defined(CONFIG_ARM)
  866. reserve_mmu,
  867. #endif
  868. #ifdef CONFIG_DM_VIDEO
  869. reserve_video,
  870. #else
  871. # ifdef CONFIG_LCD
  872. reserve_lcd,
  873. # endif
  874. /* TODO: Why the dependency on CONFIG_8xx? */
  875. # if defined(CONFIG_VIDEO) && (!defined(CONFIG_PPC) || defined(CONFIG_8xx)) && \
  876. !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
  877. !defined(CONFIG_BLACKFIN) && !defined(CONFIG_M68K)
  878. reserve_legacy_video,
  879. # endif
  880. #endif /* CONFIG_DM_VIDEO */
  881. reserve_trace,
  882. #if !defined(CONFIG_BLACKFIN)
  883. reserve_uboot,
  884. #endif
  885. #ifndef CONFIG_SPL_BUILD
  886. reserve_malloc,
  887. reserve_board,
  888. #endif
  889. setup_machine,
  890. reserve_global_data,
  891. reserve_fdt,
  892. reserve_arch,
  893. reserve_stacks,
  894. setup_dram_config,
  895. show_dram_config,
  896. #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_MIPS)
  897. setup_board_part1,
  898. #endif
  899. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  900. INIT_FUNC_WATCHDOG_RESET
  901. setup_board_part2,
  902. #endif
  903. display_new_sp,
  904. #ifdef CONFIG_SYS_EXTBDINFO
  905. setup_board_extra,
  906. #endif
  907. INIT_FUNC_WATCHDOG_RESET
  908. reloc_fdt,
  909. setup_reloc,
  910. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  911. copy_uboot_to_ram,
  912. clear_bss,
  913. do_elf_reloc_fixups,
  914. #endif
  915. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
  916. jump_to_copy,
  917. #endif
  918. NULL,
  919. };
  920. void board_init_f(ulong boot_flags)
  921. {
  922. #ifdef CONFIG_SYS_GENERIC_GLOBAL_DATA
  923. /*
  924. * For some archtectures, global data is initialized and used before
  925. * calling this function. The data should be preserved. For others,
  926. * CONFIG_SYS_GENERIC_GLOBAL_DATA should be defined and use the stack
  927. * here to host global data until relocation.
  928. */
  929. gd_t data;
  930. gd = &data;
  931. /*
  932. * Clear global data before it is accessed at debug print
  933. * in initcall_run_list. Otherwise the debug print probably
  934. * get the wrong vaule of gd->have_console.
  935. */
  936. zero_global_data();
  937. #endif
  938. gd->flags = boot_flags;
  939. gd->have_console = 0;
  940. if (initcall_run_list(init_sequence_f))
  941. hang();
  942. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
  943. !defined(CONFIG_EFI_APP)
  944. /* NOTREACHED - jump_to_copy() does not return */
  945. hang();
  946. #endif
  947. }
  948. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  949. /*
  950. * For now this code is only used on x86.
  951. *
  952. * init_sequence_f_r is the list of init functions which are run when
  953. * U-Boot is executing from Flash with a semi-limited 'C' environment.
  954. * The following limitations must be considered when implementing an
  955. * '_f_r' function:
  956. * - 'static' variables are read-only
  957. * - Global Data (gd->xxx) is read/write
  958. *
  959. * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
  960. * supported). It _should_, if possible, copy global data to RAM and
  961. * initialise the CPU caches (to speed up the relocation process)
  962. *
  963. * NOTE: At present only x86 uses this route, but it is intended that
  964. * all archs will move to this when generic relocation is implemented.
  965. */
  966. static init_fnc_t init_sequence_f_r[] = {
  967. init_cache_f_r,
  968. NULL,
  969. };
  970. void board_init_f_r(void)
  971. {
  972. if (initcall_run_list(init_sequence_f_r))
  973. hang();
  974. /*
  975. * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
  976. * Transfer execution from Flash to RAM by calculating the address
  977. * of the in-RAM copy of board_init_r() and calling it
  978. */
  979. (board_init_r + gd->reloc_off)((gd_t *)gd, gd->relocaddr);
  980. /* NOTREACHED - board_init_r() does not return */
  981. hang();
  982. }
  983. #endif /* CONFIG_X86 */