board_f.c 25 KB

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