board.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. /*
  2. * (C) Copyright 2003
  3. * Josef Baumgartner <josef.baumgartner@telex.de>
  4. *
  5. * (C) Copyright 2000-2002
  6. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <watchdog.h>
  12. #include <command.h>
  13. #include <malloc.h>
  14. #include <stdio_dev.h>
  15. #include <linux/compiler.h>
  16. #include <asm/immap.h>
  17. #if defined(CONFIG_CMD_IDE)
  18. #include <ide.h>
  19. #endif
  20. #if defined(CONFIG_CMD_SCSI)
  21. #include <scsi.h>
  22. #endif
  23. #if defined(CONFIG_CMD_KGDB)
  24. #include <kgdb.h>
  25. #endif
  26. #ifdef CONFIG_STATUS_LED
  27. #include <status_led.h>
  28. #endif
  29. #include <net.h>
  30. #include <serial.h>
  31. #ifdef CONFIG_SYS_ALLOC_DPRAM
  32. #include <commproc.h>
  33. #endif
  34. #include <version.h>
  35. #if defined(CONFIG_HARD_I2C) || \
  36. defined(CONFIG_SYS_I2C)
  37. #include <i2c.h>
  38. #endif
  39. #ifdef CONFIG_CMD_SPI
  40. #include <spi.h>
  41. #endif
  42. #ifdef CONFIG_BITBANGMII
  43. #include <miiphy.h>
  44. #endif
  45. #include <nand.h>
  46. DECLARE_GLOBAL_DATA_PTR;
  47. static char *failed = "*** failed ***\n";
  48. #include <environment.h>
  49. extern ulong __init_end;
  50. extern ulong __bss_end;
  51. #if defined(CONFIG_WATCHDOG)
  52. # undef INIT_FUNC_WATCHDOG_INIT
  53. # define INIT_FUNC_WATCHDOG_INIT watchdog_init,
  54. # define WATCHDOG_DISABLE watchdog_disable
  55. extern int watchdog_init(void);
  56. extern int watchdog_disable(void);
  57. #else
  58. # define INIT_FUNC_WATCHDOG_INIT /* undef */
  59. # define WATCHDOG_DISABLE /* undef */
  60. #endif /* CONFIG_WATCHDOG */
  61. ulong monitor_flash_len;
  62. /************************************************************************
  63. * Utilities *
  64. ************************************************************************
  65. */
  66. /*
  67. * All attempts to come up with a "common" initialization sequence
  68. * that works for all boards and architectures failed: some of the
  69. * requirements are just _too_ different. To get rid of the resulting
  70. * mess of board dependend #ifdef'ed code we now make the whole
  71. * initialization sequence configurable to the user.
  72. *
  73. * The requirements for any new initalization function is simple: it
  74. * receives a pointer to the "global data" structure as it's only
  75. * argument, and returns an integer return code, where 0 means
  76. * "continue" and != 0 means "fatal error, hang the system".
  77. */
  78. typedef int (init_fnc_t) (void);
  79. /************************************************************************
  80. * Init Utilities
  81. ************************************************************************
  82. * Some of this code should be moved into the core functions,
  83. * but let's get it working (again) first...
  84. */
  85. static int init_baudrate (void)
  86. {
  87. gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
  88. return 0;
  89. }
  90. /***********************************************************************/
  91. static int init_func_ram (void)
  92. {
  93. int board_type = 0; /* use dummy arg */
  94. puts ("DRAM: ");
  95. if ((gd->ram_size = initdram (board_type)) > 0) {
  96. print_size (gd->ram_size, "\n");
  97. return (0);
  98. }
  99. puts (failed);
  100. return (1);
  101. }
  102. /***********************************************************************/
  103. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
  104. static int init_func_i2c (void)
  105. {
  106. puts ("I2C: ");
  107. #ifdef CONFIG_SYS_I2C
  108. i2c_init_all();
  109. #else
  110. i2c_init (CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
  111. #endif
  112. puts ("ready\n");
  113. return (0);
  114. }
  115. #endif
  116. #if defined(CONFIG_HARD_SPI)
  117. static int init_func_spi (void)
  118. {
  119. puts ("SPI: ");
  120. spi_init ();
  121. puts ("ready\n");
  122. return (0);
  123. }
  124. #endif
  125. /***********************************************************************/
  126. /************************************************************************
  127. * Initialization sequence *
  128. ************************************************************************
  129. */
  130. init_fnc_t *init_sequence[] = {
  131. get_clocks,
  132. env_init,
  133. init_baudrate,
  134. serial_init,
  135. console_init_f,
  136. display_options,
  137. checkcpu,
  138. checkboard,
  139. #if defined(CONFIG_HARD_I2C) || defined(CONFIG_SYS_I2C)
  140. init_func_i2c,
  141. #endif
  142. #if defined(CONFIG_HARD_SPI)
  143. init_func_spi,
  144. #endif
  145. init_func_ram,
  146. #if defined(CONFIG_SYS_DRAM_TEST)
  147. testdram,
  148. #endif /* CONFIG_SYS_DRAM_TEST */
  149. INIT_FUNC_WATCHDOG_INIT
  150. NULL, /* Terminate this list */
  151. };
  152. /************************************************************************
  153. *
  154. * This is the first part of the initialization sequence that is
  155. * implemented in C, but still running from ROM.
  156. *
  157. * The main purpose is to provide a (serial) console interface as
  158. * soon as possible (so we can see any error messages), and to
  159. * initialize the RAM so that we can relocate the monitor code to
  160. * RAM.
  161. *
  162. * Be aware of the restrictions: global data is read-only, BSS is not
  163. * initialized, and stack space is limited to a few kB.
  164. *
  165. ************************************************************************
  166. */
  167. void
  168. board_init_f (ulong bootflag)
  169. {
  170. bd_t *bd;
  171. ulong len, addr, addr_sp;
  172. ulong *paddr;
  173. gd_t *id;
  174. init_fnc_t **init_fnc_ptr;
  175. #ifdef CONFIG_PRAM
  176. ulong reg;
  177. #endif
  178. /* Pointer is writable since we allocated a register for it */
  179. gd = (gd_t *) (CONFIG_SYS_INIT_RAM_ADDR + CONFIG_SYS_GBL_DATA_OFFSET);
  180. /* compiler optimization barrier needed for GCC >= 3.4 */
  181. __asm__ __volatile__("": : :"memory");
  182. /* Clear initial global data */
  183. memset ((void *) gd, 0, sizeof (gd_t));
  184. for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
  185. if ((*init_fnc_ptr)() != 0) {
  186. hang ();
  187. }
  188. }
  189. /*
  190. * Now that we have DRAM mapped and working, we can
  191. * relocate the code and continue running from DRAM.
  192. *
  193. * Reserve memory at end of RAM for (top down in that order):
  194. * - protected RAM
  195. * - LCD framebuffer
  196. * - monitor code
  197. * - board info struct
  198. */
  199. len = (ulong)&__bss_end - CONFIG_SYS_MONITOR_BASE;
  200. addr = CONFIG_SYS_SDRAM_BASE + gd->ram_size;
  201. #ifdef CONFIG_LOGBUFFER
  202. /* reserve kernel log buffer */
  203. addr -= (LOGBUFF_RESERVE);
  204. debug ("Reserving %dk for kernel logbuffer at %08lx\n", LOGBUFF_LEN, addr);
  205. #endif
  206. #ifdef CONFIG_PRAM
  207. /*
  208. * reserve protected RAM
  209. */
  210. reg = getenv_ulong("pram", 10, CONFIG_PRAM);
  211. addr -= (reg << 10); /* size is in kB */
  212. debug ("Reserving %ldk for protected RAM at %08lx\n", reg, addr);
  213. #endif /* CONFIG_PRAM */
  214. /* round down to next 4 kB limit */
  215. addr &= ~(4096 - 1);
  216. debug ("Top of RAM usable for U-Boot at: %08lx\n", addr);
  217. #ifdef CONFIG_LCD
  218. #ifdef CONFIG_FB_ADDR
  219. gd->fb_base = CONFIG_FB_ADDR;
  220. #else
  221. /* reserve memory for LCD display (always full pages) */
  222. addr = lcd_setmem (addr);
  223. gd->fb_base = addr;
  224. #endif /* CONFIG_FB_ADDR */
  225. #endif /* CONFIG_LCD */
  226. /*
  227. * reserve memory for U-Boot code, data & bss
  228. * round down to next 4 kB limit
  229. */
  230. addr -= len;
  231. addr &= ~(4096 - 1);
  232. debug ("Reserving %ldk for U-Boot at: %08lx\n", len >> 10, addr);
  233. /*
  234. * reserve memory for malloc() arena
  235. */
  236. addr_sp = addr - TOTAL_MALLOC_LEN;
  237. debug ("Reserving %dk for malloc() at: %08lx\n",
  238. TOTAL_MALLOC_LEN >> 10, addr_sp);
  239. /*
  240. * (permanently) allocate a Board Info struct
  241. * and a permanent copy of the "global" data
  242. */
  243. addr_sp -= sizeof (bd_t);
  244. bd = (bd_t *) addr_sp;
  245. gd->bd = bd;
  246. debug ("Reserving %zu Bytes for Board Info at: %08lx\n",
  247. sizeof (bd_t), addr_sp);
  248. addr_sp -= sizeof (gd_t);
  249. id = (gd_t *) addr_sp;
  250. debug ("Reserving %zu Bytes for Global Data at: %08lx\n",
  251. sizeof (gd_t), addr_sp);
  252. /* Reserve memory for boot params. */
  253. addr_sp -= CONFIG_SYS_BOOTPARAMS_LEN;
  254. bd->bi_boot_params = addr_sp;
  255. debug ("Reserving %dk for boot parameters at: %08lx\n",
  256. CONFIG_SYS_BOOTPARAMS_LEN >> 10, addr_sp);
  257. /*
  258. * Finally, we set up a new (bigger) stack.
  259. *
  260. * Leave some safety gap for SP, force alignment on 16 byte boundary
  261. * Clear initial stack frame
  262. */
  263. addr_sp -= 16;
  264. addr_sp &= ~0xF;
  265. paddr = (ulong *)addr_sp;
  266. *paddr-- = 0;
  267. *paddr-- = 0;
  268. addr_sp = (ulong)paddr;
  269. debug ("Stack Pointer at: %08lx\n", addr_sp);
  270. /*
  271. * Save local variables to board info struct
  272. */
  273. bd->bi_memstart = CONFIG_SYS_SDRAM_BASE; /* start of DRAM memory */
  274. bd->bi_memsize = gd->ram_size; /* size of DRAM memory in bytes */
  275. #ifdef CONFIG_SYS_INIT_RAM_ADDR
  276. bd->bi_sramstart = CONFIG_SYS_INIT_RAM_ADDR; /* start of SRAM memory */
  277. bd->bi_sramsize = CONFIG_SYS_INIT_RAM_SIZE; /* size of SRAM memory */
  278. #endif
  279. bd->bi_mbar_base = CONFIG_SYS_MBAR; /* base of internal registers */
  280. bd->bi_bootflags = bootflag; /* boot / reboot flag (for LynxOS) */
  281. WATCHDOG_RESET ();
  282. bd->bi_intfreq = gd->cpu_clk; /* Internal Freq, in Hz */
  283. bd->bi_busfreq = gd->bus_clk; /* Bus Freq, in Hz */
  284. #ifdef CONFIG_PCI
  285. bd->bi_pcifreq = gd->pci_clk; /* PCI Freq in Hz */
  286. #endif
  287. #ifdef CONFIG_EXTRA_CLOCK
  288. bd->bi_inpfreq = gd->arch.inp_clk; /* input Freq in Hz */
  289. bd->bi_vcofreq = gd->arch.vco_clk; /* vco Freq in Hz */
  290. bd->bi_flbfreq = gd->arch.flb_clk; /* flexbus Freq in Hz */
  291. #endif
  292. #ifdef CONFIG_SYS_EXTBDINFO
  293. strncpy (bd->bi_s_version, "1.2", sizeof (bd->bi_s_version));
  294. strncpy (bd->bi_r_version, U_BOOT_VERSION, sizeof (bd->bi_r_version));
  295. #endif
  296. WATCHDOG_RESET ();
  297. #ifdef CONFIG_POST
  298. post_bootmode_init();
  299. post_run (NULL, POST_ROM | post_bootmode_get(0));
  300. #endif
  301. WATCHDOG_RESET();
  302. memcpy (id, (void *)gd, sizeof (gd_t));
  303. debug ("Start relocate of code from %08x to %08lx\n", CONFIG_SYS_MONITOR_BASE, addr);
  304. relocate_code (addr_sp, id, addr);
  305. /* NOTREACHED - jump_to_ram() does not return */
  306. }
  307. /************************************************************************
  308. *
  309. * This is the next part if the initialization sequence: we are now
  310. * running from RAM and have a "normal" C environment, i. e. global
  311. * data can be written, BSS has been cleared, the stack size in not
  312. * that critical any more, etc.
  313. *
  314. ************************************************************************
  315. */
  316. void board_init_r (gd_t *id, ulong dest_addr)
  317. {
  318. char *s __maybe_unused;
  319. bd_t *bd;
  320. #ifndef CONFIG_ENV_IS_NOWHERE
  321. extern char * env_name_spec;
  322. #endif
  323. #ifndef CONFIG_SYS_NO_FLASH
  324. ulong flash_size;
  325. #endif
  326. gd = id; /* initialize RAM version of global data */
  327. bd = gd->bd;
  328. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  329. WATCHDOG_RESET ();
  330. gd->reloc_off = dest_addr - CONFIG_SYS_MONITOR_BASE;
  331. serial_initialize();
  332. debug("Now running in RAM - U-Boot at: %08lx\n", dest_addr);
  333. monitor_flash_len = (ulong)&__init_end - dest_addr;
  334. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  335. /*
  336. * We have to relocate the command table manually
  337. */
  338. fixup_cmdtable(ll_entry_start(cmd_tbl_t, cmd),
  339. ll_entry_count(cmd_tbl_t, cmd));
  340. #endif /* defined(CONFIG_NEEDS_MANUAL_RELOC) */
  341. /* there are some other pointer constants we must deal with */
  342. #ifndef CONFIG_ENV_IS_NOWHERE
  343. env_name_spec += gd->reloc_off;
  344. #endif
  345. WATCHDOG_RESET ();
  346. #ifdef CONFIG_LOGBUFFER
  347. logbuff_init_ptrs ();
  348. #endif
  349. #ifdef CONFIG_POST
  350. post_output_backlog ();
  351. post_reloc ();
  352. #endif
  353. WATCHDOG_RESET();
  354. #if 0
  355. /* instruction cache enabled in cpu_init_f() for faster relocation */
  356. icache_enable (); /* it's time to enable the instruction cache */
  357. #endif
  358. /*
  359. * Setup trap handlers
  360. */
  361. trap_init (CONFIG_SYS_SDRAM_BASE);
  362. /* The Malloc area is immediately below the monitor copy in DRAM */
  363. mem_malloc_init (CONFIG_SYS_MONITOR_BASE + gd->reloc_off -
  364. TOTAL_MALLOC_LEN, TOTAL_MALLOC_LEN);
  365. #if !defined(CONFIG_SYS_NO_FLASH)
  366. puts ("Flash: ");
  367. if ((flash_size = flash_init ()) > 0) {
  368. # ifdef CONFIG_SYS_FLASH_CHECKSUM
  369. print_size (flash_size, "");
  370. /*
  371. * Compute and print flash CRC if flashchecksum is set to 'y'
  372. *
  373. * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
  374. */
  375. if (getenv_yesno("flashchecksum") == 1) {
  376. printf (" CRC: %08X",
  377. crc32 (0,
  378. (const unsigned char *) CONFIG_SYS_FLASH_BASE,
  379. flash_size)
  380. );
  381. }
  382. putc ('\n');
  383. # else /* !CONFIG_SYS_FLASH_CHECKSUM */
  384. print_size (flash_size, "\n");
  385. # endif /* CONFIG_SYS_FLASH_CHECKSUM */
  386. } else {
  387. puts (failed);
  388. hang ();
  389. }
  390. bd->bi_flashstart = CONFIG_SYS_FLASH_BASE; /* update start of FLASH memory */
  391. bd->bi_flashsize = flash_size; /* size of FLASH memory (final value) */
  392. bd->bi_flashoffset = 0;
  393. #else /* CONFIG_SYS_NO_FLASH */
  394. bd->bi_flashsize = 0;
  395. bd->bi_flashstart = 0;
  396. bd->bi_flashoffset = 0;
  397. #endif /* !CONFIG_SYS_NO_FLASH */
  398. WATCHDOG_RESET ();
  399. /* initialize higher level parts of CPU like time base and timers */
  400. cpu_init_r ();
  401. WATCHDOG_RESET ();
  402. #ifdef CONFIG_SPI
  403. # if !defined(CONFIG_ENV_IS_IN_EEPROM)
  404. spi_init_f ();
  405. # endif
  406. spi_init_r ();
  407. #endif
  408. #if defined(CONFIG_SYS_I2C)
  409. /* Adjust I2C subsystem pointers after relocation */
  410. i2c_reloc_fixup();
  411. #endif
  412. /* relocate environment function pointers etc. */
  413. env_relocate ();
  414. WATCHDOG_RESET ();
  415. #if defined(CONFIG_PCI)
  416. /*
  417. * Do pci configuration
  418. */
  419. pci_init ();
  420. #endif
  421. /** leave this here (after malloc(), environment and PCI are working) **/
  422. /* Initialize stdio devices */
  423. stdio_init ();
  424. /* Initialize the jump table for applications */
  425. jumptable_init ();
  426. /* Initialize the console (after the relocation and devices init) */
  427. console_init_r ();
  428. #if defined(CONFIG_MISC_INIT_R)
  429. /* miscellaneous platform dependent initialisations */
  430. misc_init_r ();
  431. #endif
  432. #if defined(CONFIG_CMD_KGDB)
  433. WATCHDOG_RESET ();
  434. puts ("KGDB: ");
  435. kgdb_init ();
  436. #endif
  437. debug ("U-Boot relocated to %08lx\n", dest_addr);
  438. /*
  439. * Enable Interrupts
  440. */
  441. interrupt_init ();
  442. /* Must happen after interrupts are initialized since
  443. * an irq handler gets installed
  444. */
  445. timer_init();
  446. #ifdef CONFIG_STATUS_LED
  447. status_led_set (STATUS_LED_BOOT, STATUS_LED_BLINKING);
  448. #endif
  449. udelay (20);
  450. /* Insert function pointers now that we have relocated the code */
  451. /* Initialize from environment */
  452. load_addr = getenv_ulong("loadaddr", 16, load_addr);
  453. WATCHDOG_RESET ();
  454. #if defined(CONFIG_CMD_DOC)
  455. WATCHDOG_RESET ();
  456. puts ("DOC: ");
  457. doc_init ();
  458. #endif
  459. #if defined(CONFIG_CMD_NAND)
  460. WATCHDOG_RESET ();
  461. puts ("NAND: ");
  462. nand_init(); /* go init the NAND */
  463. #endif
  464. #ifdef CONFIG_BITBANGMII
  465. bb_miiphy_init();
  466. #endif
  467. #if defined(CONFIG_CMD_NET)
  468. WATCHDOG_RESET();
  469. #if defined(FEC_ENET)
  470. eth_init(bd);
  471. #endif
  472. puts ("Net: ");
  473. eth_initialize (bd);
  474. #endif
  475. #ifdef CONFIG_POST
  476. post_run (NULL, POST_RAM | post_bootmode_get(0));
  477. #endif
  478. #if defined(CONFIG_CMD_PCMCIA) \
  479. && !defined(CONFIG_CMD_IDE)
  480. WATCHDOG_RESET ();
  481. puts ("PCMCIA:");
  482. pcmcia_init ();
  483. #endif
  484. #if defined(CONFIG_CMD_IDE)
  485. WATCHDOG_RESET ();
  486. puts ("IDE: ");
  487. ide_init ();
  488. #endif
  489. #ifdef CONFIG_LAST_STAGE_INIT
  490. WATCHDOG_RESET ();
  491. /*
  492. * Some parts can be only initialized if all others (like
  493. * Interrupts) are up and running (i.e. the PC-style ISA
  494. * keyboard).
  495. */
  496. last_stage_init ();
  497. #endif
  498. #if defined(CONFIG_PRAM) || defined(CONFIG_LOGBUFFER)
  499. /*
  500. * Export available size of memory for Linux,
  501. * taking into account the protected RAM at top of memory
  502. */
  503. {
  504. ulong pram = 0;
  505. char memsz[32];
  506. #ifdef CONFIG_PRAM
  507. pram = getenv_ulong("pram", 10, CONFIG_PRAM);
  508. #endif
  509. #ifdef CONFIG_LOGBUFFER
  510. /* Also take the logbuffer into account (pram is in kB) */
  511. pram += (LOGBUFF_LEN+LOGBUFF_OVERHEAD)/1024;
  512. #endif
  513. sprintf (memsz, "%ldk", (bd->bi_memsize / 1024) - pram);
  514. setenv ("mem", memsz);
  515. }
  516. #endif
  517. #ifdef CONFIG_WATCHDOG
  518. /* disable watchdog if environment is set */
  519. if ((s = getenv ("watchdog")) != NULL) {
  520. if (strncmp (s, "off", 3) == 0) {
  521. WATCHDOG_DISABLE ();
  522. }
  523. }
  524. #endif /* CONFIG_WATCHDOG*/
  525. /* Initialization complete - start the monitor */
  526. /* main_loop() can return to retry autoboot, if so just run it again. */
  527. for (;;) {
  528. WATCHDOG_RESET ();
  529. main_loop ();
  530. }
  531. /* NOTREACHED - no way out of command loop except booting */
  532. }