board_f.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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) || defined(CONFIG_SH)
  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_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
  543. defined(CONFIG_SH)
  544. static int setup_board_part1(void)
  545. {
  546. bd_t *bd = gd->bd;
  547. /*
  548. * Save local variables to board info struct
  549. */
  550. bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of memory */
  551. bd->bi_memsize = gd->ram_size; /* size in bytes */
  552. #ifdef CONFIG_SYS_SRAM_BASE
  553. bd->bi_sramstart = CONFIG_SYS_SRAM_BASE; /* start of SRAM */
  554. bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE; /* size of SRAM */
  555. #endif
  556. #if defined(CONFIG_8xx) || defined(CONFIG_MPC8260) || defined(CONFIG_5xx) || \
  557. defined(CONFIG_E500) || defined(CONFIG_MPC86xx)
  558. bd->bi_immr_base = CONFIG_SYS_IMMR; /* base of IMMR register */
  559. #endif
  560. #if defined(CONFIG_MPC5xxx) || defined(CONFIG_M68K)
  561. bd->bi_mbar_base = CONFIG_SYS_MBAR; /* base of internal registers */
  562. #endif
  563. #if defined(CONFIG_MPC83xx)
  564. bd->bi_immrbar = CONFIG_SYS_IMMR;
  565. #endif
  566. return 0;
  567. }
  568. #endif
  569. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  570. static int setup_board_part2(void)
  571. {
  572. bd_t *bd = gd->bd;
  573. bd->bi_intfreq = gd->cpu_clk; /* Internal Freq, in Hz */
  574. bd->bi_busfreq = gd->bus_clk; /* Bus Freq, in Hz */
  575. #if defined(CONFIG_CPM2)
  576. bd->bi_cpmfreq = gd->arch.cpm_clk;
  577. bd->bi_brgfreq = gd->arch.brg_clk;
  578. bd->bi_sccfreq = gd->arch.scc_clk;
  579. bd->bi_vco = gd->arch.vco_out;
  580. #endif /* CONFIG_CPM2 */
  581. #if defined(CONFIG_MPC512X)
  582. bd->bi_ipsfreq = gd->arch.ips_clk;
  583. #endif /* CONFIG_MPC512X */
  584. #if defined(CONFIG_MPC5xxx)
  585. bd->bi_ipbfreq = gd->arch.ipb_clk;
  586. bd->bi_pcifreq = gd->pci_clk;
  587. #endif /* CONFIG_MPC5xxx */
  588. #if defined(CONFIG_M68K) && defined(CONFIG_PCI)
  589. bd->bi_pcifreq = gd->pci_clk;
  590. #endif
  591. #if defined(CONFIG_EXTRA_CLOCK)
  592. bd->bi_inpfreq = gd->arch.inp_clk; /* input Freq in Hz */
  593. bd->bi_vcofreq = gd->arch.vco_clk; /* vco Freq in Hz */
  594. bd->bi_flbfreq = gd->arch.flb_clk; /* flexbus Freq in Hz */
  595. #endif
  596. return 0;
  597. }
  598. #endif
  599. #ifdef CONFIG_SYS_EXTBDINFO
  600. static int setup_board_extra(void)
  601. {
  602. bd_t *bd = gd->bd;
  603. strncpy((char *) bd->bi_s_version, "1.2", sizeof(bd->bi_s_version));
  604. strncpy((char *) bd->bi_r_version, U_BOOT_VERSION,
  605. sizeof(bd->bi_r_version));
  606. bd->bi_procfreq = gd->cpu_clk; /* Processor Speed, In Hz */
  607. bd->bi_plb_busfreq = gd->bus_clk;
  608. #if defined(CONFIG_405GP) || defined(CONFIG_405EP) || \
  609. defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
  610. defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  611. bd->bi_pci_busfreq = get_PCI_freq();
  612. bd->bi_opbfreq = get_OPB_freq();
  613. #elif defined(CONFIG_XILINX_405)
  614. bd->bi_pci_busfreq = get_PCI_freq();
  615. #endif
  616. return 0;
  617. }
  618. #endif
  619. #ifdef CONFIG_POST
  620. static int init_post(void)
  621. {
  622. post_bootmode_init();
  623. post_run(NULL, POST_ROM | post_bootmode_get(0));
  624. return 0;
  625. }
  626. #endif
  627. static int setup_dram_config(void)
  628. {
  629. /* Ram is board specific, so move it to board code ... */
  630. dram_init_banksize();
  631. return 0;
  632. }
  633. static int reloc_fdt(void)
  634. {
  635. #ifndef CONFIG_OF_EMBED
  636. if (gd->flags & GD_FLG_SKIP_RELOC)
  637. return 0;
  638. if (gd->new_fdt) {
  639. memcpy(gd->new_fdt, gd->fdt_blob, gd->fdt_size);
  640. gd->fdt_blob = gd->new_fdt;
  641. }
  642. #endif
  643. return 0;
  644. }
  645. static int setup_reloc(void)
  646. {
  647. if (gd->flags & GD_FLG_SKIP_RELOC) {
  648. debug("Skipping relocation due to flag\n");
  649. return 0;
  650. }
  651. #ifdef CONFIG_SYS_TEXT_BASE
  652. gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
  653. #ifdef CONFIG_M68K
  654. /*
  655. * On all ColdFire arch cpu, monitor code starts always
  656. * just after the default vector table location, so at 0x400
  657. */
  658. gd->reloc_off = gd->relocaddr - (CONFIG_SYS_TEXT_BASE + 0x400);
  659. #endif
  660. #endif
  661. memcpy(gd->new_gd, (char *)gd, sizeof(gd_t));
  662. debug("Relocation Offset is: %08lx\n", gd->reloc_off);
  663. debug("Relocating to %08lx, new gd at %08lx, sp at %08lx\n",
  664. gd->relocaddr, (ulong)map_to_sysmem(gd->new_gd),
  665. gd->start_addr_sp);
  666. return 0;
  667. }
  668. /* ARM calls relocate_code from its crt0.S */
  669. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
  670. static int jump_to_copy(void)
  671. {
  672. if (gd->flags & GD_FLG_SKIP_RELOC)
  673. return 0;
  674. /*
  675. * x86 is special, but in a nice way. It uses a trampoline which
  676. * enables the dcache if possible.
  677. *
  678. * For now, other archs use relocate_code(), which is implemented
  679. * similarly for all archs. When we do generic relocation, hopefully
  680. * we can make all archs enable the dcache prior to relocation.
  681. */
  682. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  683. /*
  684. * SDRAM and console are now initialised. The final stack can now
  685. * be setup in SDRAM. Code execution will continue in Flash, but
  686. * with the stack in SDRAM and Global Data in temporary memory
  687. * (CPU cache)
  688. */
  689. arch_setup_gd(gd->new_gd);
  690. board_init_f_r_trampoline(gd->start_addr_sp);
  691. #else
  692. relocate_code(gd->start_addr_sp, gd->new_gd, gd->relocaddr);
  693. #endif
  694. return 0;
  695. }
  696. #endif
  697. /* Record the board_init_f() bootstage (after arch_cpu_init()) */
  698. static int mark_bootstage(void)
  699. {
  700. bootstage_mark_name(BOOTSTAGE_ID_START_UBOOT_F, "board_init_f");
  701. return 0;
  702. }
  703. static int initf_console_record(void)
  704. {
  705. #if defined(CONFIG_CONSOLE_RECORD) && defined(CONFIG_SYS_MALLOC_F_LEN)
  706. return console_record_init();
  707. #else
  708. return 0;
  709. #endif
  710. }
  711. static int initf_dm(void)
  712. {
  713. #if defined(CONFIG_DM) && defined(CONFIG_SYS_MALLOC_F_LEN)
  714. int ret;
  715. ret = dm_init_and_scan(true);
  716. if (ret)
  717. return ret;
  718. #endif
  719. #ifdef CONFIG_TIMER_EARLY
  720. ret = dm_timer_init();
  721. if (ret)
  722. return ret;
  723. #endif
  724. return 0;
  725. }
  726. /* Architecture-specific memory reservation */
  727. __weak int reserve_arch(void)
  728. {
  729. return 0;
  730. }
  731. __weak int arch_cpu_init_dm(void)
  732. {
  733. return 0;
  734. }
  735. static init_fnc_t init_sequence_f[] = {
  736. #ifdef CONFIG_SANDBOX
  737. setup_ram_buf,
  738. #endif
  739. setup_mon_len,
  740. #ifdef CONFIG_OF_CONTROL
  741. fdtdec_setup,
  742. #endif
  743. #ifdef CONFIG_TRACE
  744. trace_early_init,
  745. #endif
  746. initf_malloc,
  747. initf_console_record,
  748. #if defined(CONFIG_X86) && defined(CONFIG_HAVE_FSP)
  749. x86_fsp_init,
  750. #endif
  751. arch_cpu_init, /* basic arch cpu dependent setup */
  752. mach_cpu_init, /* SoC/machine dependent CPU setup */
  753. initf_dm,
  754. arch_cpu_init_dm,
  755. mark_bootstage, /* need timer, go after init dm */
  756. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  757. board_early_init_f,
  758. #endif
  759. /* TODO: can any of this go into arch_cpu_init()? */
  760. #if defined(CONFIG_PPC) && !defined(CONFIG_8xx_CPUCLK_DEFAULT)
  761. get_clocks, /* get CPU and bus clocks (etc.) */
  762. #if defined(CONFIG_TQM8xxL) && !defined(CONFIG_TQM866M) \
  763. && !defined(CONFIG_TQM885D)
  764. adjust_sdram_tbs_8xx,
  765. #endif
  766. /* TODO: can we rename this to timer_init()? */
  767. init_timebase,
  768. #endif
  769. #if defined(CONFIG_ARM) || defined(CONFIG_MIPS) || \
  770. defined(CONFIG_BLACKFIN) || defined(CONFIG_NDS32) || \
  771. defined(CONFIG_SH) || defined(CONFIG_SPARC)
  772. timer_init, /* initialize timer */
  773. #endif
  774. #if defined(CONFIG_BOARD_POSTCLK_INIT)
  775. board_postclk_init,
  776. #endif
  777. #if defined(CONFIG_SYS_FSL_CLK) || defined(CONFIG_M68K)
  778. get_clocks,
  779. #endif
  780. env_init, /* initialize environment */
  781. #if defined(CONFIG_8xx_CPUCLK_DEFAULT)
  782. /* get CPU and bus clocks according to the environment variable */
  783. get_clocks_866,
  784. /* adjust sdram refresh rate according to the new clock */
  785. sdram_adjust_866,
  786. init_timebase,
  787. #endif
  788. init_baud_rate, /* initialze baudrate settings */
  789. serial_init, /* serial communications setup */
  790. console_init_f, /* stage 1 init of console */
  791. #ifdef CONFIG_SANDBOX
  792. sandbox_early_getopt_check,
  793. #endif
  794. display_options, /* say that we are here */
  795. display_text_info, /* show debugging info if required */
  796. #if defined(CONFIG_MPC8260)
  797. prt_8260_rsr,
  798. prt_8260_clks,
  799. #endif /* CONFIG_MPC8260 */
  800. #if defined(CONFIG_MPC83xx)
  801. prt_83xx_rsr,
  802. #endif
  803. #if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SH)
  804. checkcpu,
  805. #endif
  806. #if defined(CONFIG_DISPLAY_CPUINFO)
  807. print_cpuinfo, /* display cpu info (and speed) */
  808. #endif
  809. #if defined(CONFIG_DISPLAY_BOARDINFO)
  810. show_board_info,
  811. #endif
  812. INIT_FUNC_WATCHDOG_INIT
  813. #if defined(CONFIG_MISC_INIT_F)
  814. misc_init_f,
  815. #endif
  816. INIT_FUNC_WATCHDOG_RESET
  817. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
  818. init_func_i2c,
  819. #endif
  820. #if defined(CONFIG_HARD_SPI)
  821. init_func_spi,
  822. #endif
  823. announce_dram_init,
  824. /* TODO: unify all these dram functions? */
  825. #if defined(CONFIG_ARM) || defined(CONFIG_X86) || defined(CONFIG_NDS32) || \
  826. defined(CONFIG_MICROBLAZE) || defined(CONFIG_AVR32) || \
  827. defined(CONFIG_SH)
  828. dram_init, /* configure available RAM banks */
  829. #endif
  830. #if defined(CONFIG_MIPS) || defined(CONFIG_PPC) || defined(CONFIG_M68K)
  831. init_func_ram,
  832. #endif
  833. #ifdef CONFIG_POST
  834. post_init_f,
  835. #endif
  836. INIT_FUNC_WATCHDOG_RESET
  837. #if defined(CONFIG_SYS_DRAM_TEST)
  838. testdram,
  839. #endif /* CONFIG_SYS_DRAM_TEST */
  840. INIT_FUNC_WATCHDOG_RESET
  841. #ifdef CONFIG_POST
  842. init_post,
  843. #endif
  844. INIT_FUNC_WATCHDOG_RESET
  845. /*
  846. * Now that we have DRAM mapped and working, we can
  847. * relocate the code and continue running from DRAM.
  848. *
  849. * Reserve memory at end of RAM for (top down in that order):
  850. * - area that won't get touched by U-Boot and Linux (optional)
  851. * - kernel log buffer
  852. * - protected RAM
  853. * - LCD framebuffer
  854. * - monitor code
  855. * - board info struct
  856. */
  857. setup_dest_addr,
  858. #if defined(CONFIG_BLACKFIN) || defined(CONFIG_XTENSA)
  859. /* Blackfin u-boot monitor should be on top of the ram */
  860. reserve_uboot,
  861. #endif
  862. #if defined(CONFIG_SPARC)
  863. reserve_prom,
  864. #endif
  865. #if defined(CONFIG_LOGBUFFER) && !defined(CONFIG_ALT_LB_ADDR)
  866. reserve_logbuffer,
  867. #endif
  868. #ifdef CONFIG_PRAM
  869. reserve_pram,
  870. #endif
  871. reserve_round_4k,
  872. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) && \
  873. defined(CONFIG_ARM)
  874. reserve_mmu,
  875. #endif
  876. #ifdef CONFIG_DM_VIDEO
  877. reserve_video,
  878. #else
  879. # ifdef CONFIG_LCD
  880. reserve_lcd,
  881. # endif
  882. /* TODO: Why the dependency on CONFIG_8xx? */
  883. # if defined(CONFIG_VIDEO) && (!defined(CONFIG_PPC) || defined(CONFIG_8xx)) && \
  884. !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
  885. !defined(CONFIG_BLACKFIN) && !defined(CONFIG_M68K)
  886. reserve_legacy_video,
  887. # endif
  888. #endif /* CONFIG_DM_VIDEO */
  889. reserve_trace,
  890. #if !defined(CONFIG_BLACKFIN) && !defined(CONFIG_XTENSA)
  891. reserve_uboot,
  892. #endif
  893. #ifndef CONFIG_SPL_BUILD
  894. reserve_malloc,
  895. reserve_board,
  896. #endif
  897. setup_machine,
  898. reserve_global_data,
  899. reserve_fdt,
  900. reserve_arch,
  901. reserve_stacks,
  902. setup_dram_config,
  903. show_dram_config,
  904. #if defined(CONFIG_M68K) || defined(CONFIG_MIPS) || defined(CONFIG_PPC) || \
  905. defined(CONFIG_SH)
  906. setup_board_part1,
  907. #endif
  908. #if defined(CONFIG_PPC) || defined(CONFIG_M68K)
  909. INIT_FUNC_WATCHDOG_RESET
  910. setup_board_part2,
  911. #endif
  912. display_new_sp,
  913. #ifdef CONFIG_SYS_EXTBDINFO
  914. setup_board_extra,
  915. #endif
  916. INIT_FUNC_WATCHDOG_RESET
  917. reloc_fdt,
  918. setup_reloc,
  919. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  920. copy_uboot_to_ram,
  921. clear_bss,
  922. do_elf_reloc_fixups,
  923. #endif
  924. #if defined(CONFIG_XTENSA)
  925. clear_bss,
  926. #endif
  927. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX)
  928. jump_to_copy,
  929. #endif
  930. NULL,
  931. };
  932. void board_init_f(ulong boot_flags)
  933. {
  934. #ifdef CONFIG_SYS_GENERIC_GLOBAL_DATA
  935. /*
  936. * For some architectures, global data is initialized and used before
  937. * calling this function. The data should be preserved. For others,
  938. * CONFIG_SYS_GENERIC_GLOBAL_DATA should be defined and use the stack
  939. * here to host global data until relocation.
  940. */
  941. gd_t data;
  942. gd = &data;
  943. /*
  944. * Clear global data before it is accessed at debug print
  945. * in initcall_run_list. Otherwise the debug print probably
  946. * get the wrong value of gd->have_console.
  947. */
  948. zero_global_data();
  949. #endif
  950. gd->flags = boot_flags;
  951. gd->have_console = 0;
  952. if (initcall_run_list(init_sequence_f))
  953. hang();
  954. #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
  955. !defined(CONFIG_EFI_APP)
  956. /* NOTREACHED - jump_to_copy() does not return */
  957. hang();
  958. #endif
  959. }
  960. #if defined(CONFIG_X86) || defined(CONFIG_ARC)
  961. /*
  962. * For now this code is only used on x86.
  963. *
  964. * init_sequence_f_r is the list of init functions which are run when
  965. * U-Boot is executing from Flash with a semi-limited 'C' environment.
  966. * The following limitations must be considered when implementing an
  967. * '_f_r' function:
  968. * - 'static' variables are read-only
  969. * - Global Data (gd->xxx) is read/write
  970. *
  971. * The '_f_r' sequence must, as a minimum, copy U-Boot to RAM (if
  972. * supported). It _should_, if possible, copy global data to RAM and
  973. * initialise the CPU caches (to speed up the relocation process)
  974. *
  975. * NOTE: At present only x86 uses this route, but it is intended that
  976. * all archs will move to this when generic relocation is implemented.
  977. */
  978. static init_fnc_t init_sequence_f_r[] = {
  979. init_cache_f_r,
  980. NULL,
  981. };
  982. void board_init_f_r(void)
  983. {
  984. if (initcall_run_list(init_sequence_f_r))
  985. hang();
  986. /*
  987. * The pre-relocation drivers may be using memory that has now gone
  988. * away. Mark serial as unavailable - this will fall back to the debug
  989. * UART if available.
  990. */
  991. gd->flags &= ~GD_FLG_SERIAL_READY;
  992. /*
  993. * U-Boot has been copied into SDRAM, the BSS has been cleared etc.
  994. * Transfer execution from Flash to RAM by calculating the address
  995. * of the in-RAM copy of board_init_r() and calling it
  996. */
  997. (board_init_r + gd->reloc_off)((gd_t *)gd, gd->relocaddr);
  998. /* NOTREACHED - board_init_r() does not return */
  999. hang();
  1000. }
  1001. #endif /* CONFIG_X86 */