board.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* SPARC Board initialization
  2. *
  3. * (C) Copyright 2000-2006
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * (C) Copyright 2007
  7. * Daniel Hellstrom, Gaisler Research, daniel@gaisler.com.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <command.h>
  13. #include <malloc.h>
  14. #include <stdio_dev.h>
  15. #include <config.h>
  16. #if defined(CONFIG_CMD_IDE)
  17. #include <ide.h>
  18. #endif
  19. #ifdef CONFIG_STATUS_LED
  20. #include <status_led.h>
  21. #endif
  22. #include <net.h>
  23. #include <serial.h>
  24. #include <version.h>
  25. #if defined(CONFIG_POST)
  26. #include <post.h>
  27. #endif
  28. #ifdef CONFIG_PS2KBD
  29. #include <keyboard.h>
  30. #endif
  31. #ifdef CONFIG_CMD_AMBAPP
  32. #include <ambapp.h>
  33. #endif
  34. #ifdef CONFIG_BITBANGMII
  35. #include <miiphy.h>
  36. #endif
  37. DECLARE_GLOBAL_DATA_PTR;
  38. /* Debug options
  39. #define DEBUG_INIT_SEQUENCE
  40. #define DEBUG_MEM_LAYOUT
  41. #define DEBUG_COMMANDS
  42. */
  43. extern void timer_interrupt_init(void);
  44. extern int do_ambapp_print(cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]);
  45. extern int prom_init(void);
  46. #if defined(CONFIG__CMD_DOC)
  47. void doc_init(void);
  48. #endif
  49. #if !defined(CONFIG_SYS_NO_FLASH)
  50. static char *failed = "*** failed ***\n";
  51. #endif
  52. #include <environment.h>
  53. ulong monitor_flash_len;
  54. /************************************************************************
  55. * Init Utilities *
  56. ************************************************************************
  57. * Some of this code should be moved into the core functions,
  58. * but let's get it working (again) first...
  59. */
  60. static int init_baudrate(void)
  61. {
  62. gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
  63. return 0;
  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. #define WATCHDOG_RESET(x)
  80. /************************************************************************
  81. * Initialization sequence *
  82. ************************************************************************
  83. */
  84. init_fnc_t *init_sequence[] = {
  85. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  86. board_early_init_f,
  87. #endif
  88. serial_init,
  89. init_timebase,
  90. #if defined(CONFIG_CMD_AMBAPP)
  91. ambapp_init_reloc,
  92. #endif
  93. env_init,
  94. init_baudrate,
  95. console_init_f,
  96. display_options,
  97. checkcpu,
  98. checkboard,
  99. #if defined(CONFIG_MISC_INIT_F)
  100. misc_init_f,
  101. #endif
  102. #ifdef CONFIG_POST
  103. post_init_f,
  104. #endif
  105. NULL, /* Terminate this list,
  106. * beware: this list will be relocated
  107. * which means that NULL will become
  108. * NULL+RELOC_OFFSET. We simply make
  109. * NULL be -RELOC_OFFSET instead.
  110. */
  111. };
  112. /************************************************************************
  113. *
  114. * This is the SPARC board initialization routine, running from RAM.
  115. *
  116. ************************************************************************
  117. */
  118. #ifdef DEBUG_INIT_SEQUENCE
  119. char *str_init_seq = "INIT_SEQ 00\n";
  120. char *str_init_seq_done = "\n\rInit sequence done...\r\n\r\n";
  121. #endif
  122. void board_init_f(ulong bootflag)
  123. {
  124. bd_t *bd;
  125. init_fnc_t **init_fnc_ptr;
  126. int j;
  127. #ifndef CONFIG_SYS_NO_FLASH
  128. ulong flash_size;
  129. #endif
  130. gd = (gd_t *) (CONFIG_SYS_GBL_DATA_OFFSET);
  131. /* Clear initial global data */
  132. memset((void *)gd, 0, sizeof(gd_t));
  133. gd->bd = (bd_t *) (gd + 1); /* At end of global data */
  134. gd->baudrate = CONFIG_BAUDRATE;
  135. gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
  136. bd = gd->bd;
  137. bd->bi_memstart = CONFIG_SYS_RAM_BASE;
  138. bd->bi_memsize = CONFIG_SYS_RAM_SIZE;
  139. bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
  140. #if defined(CONFIG_SYS_SRAM_BASE) && defined(CONFIG_SYS_SRAM_SIZE)
  141. bd->bi_sramstart = CONFIG_SYS_SRAM_BASE;
  142. bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE;
  143. #endif
  144. bd->bi_bootflags = bootflag; /* boot / reboot flag (for LynxOS) */
  145. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  146. gd->reloc_off = CONFIG_SYS_RELOC_MONITOR_BASE - CONFIG_SYS_MONITOR_BASE;
  147. for (init_fnc_ptr = init_sequence, j = 0; *init_fnc_ptr;
  148. ++init_fnc_ptr, j++) {
  149. #ifdef DEBUG_INIT_SEQUENCE
  150. if (j > 9)
  151. str_init_seq[9] = '0' + (j / 10);
  152. str_init_seq[10] = '0' + (j - (j / 10) * 10);
  153. serial_puts(str_init_seq);
  154. #endif
  155. if ((*init_fnc_ptr + gd->reloc_off) () != 0) {
  156. hang();
  157. }
  158. }
  159. #ifdef DEBUG_INIT_SEQUENCE
  160. serial_puts(str_init_seq_done);
  161. #endif
  162. /*
  163. * Now that we have DRAM mapped and working, we can
  164. * relocate the code and continue running from DRAM.
  165. *
  166. * Reserve memory at end of RAM for (top down in that order):
  167. * - kernel log buffer
  168. * - protected RAM
  169. * - LCD framebuffer
  170. * - monitor code
  171. * - board info struct
  172. */
  173. #ifdef DEBUG_MEM_LAYOUT
  174. printf("CONFIG_SYS_MONITOR_BASE: 0x%lx\n", CONFIG_SYS_MONITOR_BASE);
  175. printf("CONFIG_ENV_ADDR: 0x%lx\n", CONFIG_ENV_ADDR);
  176. printf("CONFIG_SYS_RELOC_MONITOR_BASE: 0x%lx (%d)\n", CONFIG_SYS_RELOC_MONITOR_BASE,
  177. CONFIG_SYS_MONITOR_LEN);
  178. printf("CONFIG_SYS_MALLOC_BASE: 0x%lx (%d)\n", CONFIG_SYS_MALLOC_BASE,
  179. CONFIG_SYS_MALLOC_LEN);
  180. printf("CONFIG_SYS_INIT_SP_OFFSET: 0x%lx (%d)\n", CONFIG_SYS_INIT_SP_OFFSET,
  181. CONFIG_SYS_STACK_SIZE);
  182. printf("CONFIG_SYS_PROM_OFFSET: 0x%lx (%d)\n", CONFIG_SYS_PROM_OFFSET,
  183. CONFIG_SYS_PROM_SIZE);
  184. printf("CONFIG_SYS_GBL_DATA_OFFSET: 0x%lx (%d)\n", CONFIG_SYS_GBL_DATA_OFFSET,
  185. GENERATED_GBL_DATA_SIZE);
  186. #endif
  187. #ifdef CONFIG_POST
  188. post_bootmode_init();
  189. post_run(NULL, POST_ROM | post_bootmode_get(0));
  190. #endif
  191. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  192. /*
  193. * We have to relocate the command table manually
  194. */
  195. fixup_cmdtable(ll_entry_start(cmd_tbl_t, cmd),
  196. ll_entry_count(cmd_tbl_t, cmd));
  197. #endif /* defined(CONFIG_NEEDS_MANUAL_RELOC) */
  198. #if defined(CONFIG_CMD_AMBAPP) && defined(CONFIG_SYS_AMBAPP_PRINT_ON_STARTUP)
  199. puts("AMBA:\n");
  200. do_ambapp_print(NULL, 0, 0, NULL);
  201. #endif
  202. /* initialize higher level parts of CPU like time base and timers */
  203. cpu_init_r();
  204. /* start timer */
  205. timer_interrupt_init();
  206. /*
  207. * Enable Interrupts before any calls to udelay,
  208. * the flash driver may use udelay resulting in
  209. * a hang if not timer0 IRQ is enabled.
  210. */
  211. interrupt_init();
  212. /* The Malloc area is immediately below the monitor copy in RAM */
  213. mem_malloc_init(CONFIG_SYS_MALLOC_BASE,
  214. CONFIG_SYS_MALLOC_END - CONFIG_SYS_MALLOC_BASE);
  215. #if !defined(CONFIG_SYS_NO_FLASH)
  216. puts("Flash: ");
  217. if ((flash_size = flash_init()) > 0) {
  218. # ifdef CONFIG_SYS_FLASH_CHECKSUM
  219. print_size(flash_size, "");
  220. /*
  221. * Compute and print flash CRC if flashchecksum is set to 'y'
  222. *
  223. * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
  224. */
  225. if (getenv_yesno("flashchecksum") == 1) {
  226. printf(" CRC: %08lX",
  227. crc32(0, (const unsigned char *)CONFIG_SYS_FLASH_BASE,
  228. flash_size)
  229. );
  230. }
  231. putc('\n');
  232. # else /* !CONFIG_SYS_FLASH_CHECKSUM */
  233. print_size(flash_size, "\n");
  234. # endif /* CONFIG_SYS_FLASH_CHECKSUM */
  235. } else {
  236. puts(failed);
  237. hang();
  238. }
  239. bd->bi_flashstart = CONFIG_SYS_FLASH_BASE; /* update start of FLASH memory */
  240. bd->bi_flashsize = flash_size; /* size of FLASH memory (final value) */
  241. #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
  242. bd->bi_flashoffset = monitor_flash_len; /* reserved area for startup monitor */
  243. #else
  244. bd->bi_flashoffset = 0;
  245. #endif
  246. #else /* CONFIG_SYS_NO_FLASH */
  247. bd->bi_flashsize = 0;
  248. bd->bi_flashstart = 0;
  249. bd->bi_flashoffset = 0;
  250. #endif /* !CONFIG_SYS_NO_FLASH */
  251. #ifdef CONFIG_SPI
  252. # if !defined(CONFIG_ENV_IS_IN_EEPROM)
  253. spi_init_f();
  254. # endif
  255. spi_init_r();
  256. #endif
  257. /* relocate environment function pointers etc. */
  258. env_relocate();
  259. #if defined(CONFIG_BOARD_LATE_INIT)
  260. board_late_init();
  261. #endif
  262. #ifdef CONFIG_ID_EEPROM
  263. mac_read_from_eeprom();
  264. #endif
  265. #if defined(CONFIG_PCI)
  266. /*
  267. * Do pci configuration
  268. */
  269. pci_init();
  270. #endif
  271. /* Initialize stdio devices */
  272. stdio_init();
  273. /* Initialize the jump table for applications */
  274. jumptable_init();
  275. /* Initialize the console (after the relocation and devices init) */
  276. console_init_r();
  277. #ifdef CONFIG_STATUS_LED
  278. status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
  279. #endif
  280. udelay(20);
  281. /* Initialize from environment */
  282. load_addr = getenv_ulong("loadaddr", 16, load_addr);
  283. WATCHDOG_RESET();
  284. #if defined(CONFIG_CMD_DOC)
  285. WATCHDOG_RESET();
  286. puts("DOC: ");
  287. doc_init();
  288. #endif
  289. #ifdef CONFIG_BITBANGMII
  290. bb_miiphy_init();
  291. #endif
  292. #if defined(CONFIG_CMD_NET)
  293. WATCHDOG_RESET();
  294. puts("Net: ");
  295. eth_initialize(bd);
  296. #endif
  297. #if defined(CONFIG_CMD_NET) && defined(CONFIG_RESET_PHY_R)
  298. WATCHDOG_RESET();
  299. debug("Reset Ethernet PHY\n");
  300. reset_phy();
  301. #endif
  302. #ifdef CONFIG_POST
  303. post_run(NULL, POST_RAM | post_bootmode_get(0));
  304. #endif
  305. #if defined(CONFIG_CMD_IDE)
  306. WATCHDOG_RESET();
  307. puts("IDE: ");
  308. ide_init();
  309. #endif /* CONFIG_CMD_IDE */
  310. #ifdef CONFIG_LAST_STAGE_INIT
  311. WATCHDOG_RESET();
  312. /*
  313. * Some parts can be only initialized if all others (like
  314. * Interrupts) are up and running (i.e. the PC-style ISA
  315. * keyboard).
  316. */
  317. last_stage_init();
  318. #endif
  319. #ifdef CONFIG_PS2KBD
  320. puts("PS/2: ");
  321. kbd_init();
  322. #endif
  323. prom_init();
  324. /* main_loop */
  325. for (;;) {
  326. WATCHDOG_RESET();
  327. main_loop();
  328. }
  329. }
  330. /************************************************************************/