board_f.c 25 KB

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