board_f.c 27 KB

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