board.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <command.h>
  29. #include <malloc.h>
  30. #include <stdio_dev.h>
  31. #include <config.h>
  32. #if defined(CONFIG_CMD_IDE)
  33. #include <ide.h>
  34. #endif
  35. #ifdef CONFIG_STATUS_LED
  36. #include <status_led.h>
  37. #endif
  38. #include <net.h>
  39. #include <serial.h>
  40. #include <version.h>
  41. #if defined(CONFIG_POST)
  42. #include <post.h>
  43. #endif
  44. #ifdef CONFIG_PS2KBD
  45. #include <keyboard.h>
  46. #endif
  47. #ifdef CONFIG_CMD_AMBAPP
  48. #include <ambapp.h>
  49. #endif
  50. DECLARE_GLOBAL_DATA_PTR;
  51. /* Debug options
  52. #define DEBUG_INIT_SEQUENCE
  53. #define DEBUG_MEM_LAYOUT
  54. #define DEBUG_COMMANDS
  55. */
  56. extern void timer_interrupt_init(void);
  57. extern void malloc_bin_reloc(void);
  58. extern int do_ambapp_print(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]);
  59. extern int prom_init(void);
  60. #if defined(CONFIG__CMD_DOC)
  61. void doc_init(void);
  62. #endif
  63. #if !defined(CONFIG_SYS_NO_FLASH)
  64. static char *failed = "*** failed ***\n";
  65. #endif
  66. #include <environment.h>
  67. ulong monitor_flash_len;
  68. /************************************************************************
  69. * Init Utilities *
  70. ************************************************************************
  71. * Some of this code should be moved into the core functions,
  72. * but let's get it working (again) first...
  73. */
  74. static int init_baudrate(void)
  75. {
  76. char tmp[64]; /* long enough for environment variables */
  77. int i = getenv_r("baudrate", tmp, sizeof(tmp));
  78. gd->baudrate = (i > 0)
  79. ? (int)simple_strtoul(tmp, NULL, 10)
  80. : CONFIG_BAUDRATE;
  81. return (0);
  82. }
  83. /***********************************************************************/
  84. /*
  85. * All attempts to come up with a "common" initialization sequence
  86. * that works for all boards and architectures failed: some of the
  87. * requirements are just _too_ different. To get rid of the resulting
  88. * mess of board dependend #ifdef'ed code we now make the whole
  89. * initialization sequence configurable to the user.
  90. *
  91. * The requirements for any new initalization function is simple: it
  92. * receives a pointer to the "global data" structure as it's only
  93. * argument, and returns an integer return code, where 0 means
  94. * "continue" and != 0 means "fatal error, hang the system".
  95. */
  96. typedef int (init_fnc_t) (void);
  97. #define WATCHDOG_RESET(x)
  98. /************************************************************************
  99. * Initialization sequence *
  100. ************************************************************************
  101. */
  102. init_fnc_t *init_sequence[] = {
  103. #if defined(CONFIG_BOARD_EARLY_INIT_F)
  104. board_early_init_f,
  105. #endif
  106. serial_init,
  107. init_timebase,
  108. #if defined(CONFIG_CMD_AMBAPP)
  109. ambapp_init_reloc,
  110. #endif
  111. env_init,
  112. init_baudrate,
  113. console_init_f,
  114. display_options,
  115. checkcpu,
  116. checkboard,
  117. #if defined(CONFIG_MISC_INIT_F)
  118. misc_init_f,
  119. #endif
  120. #ifdef CONFIG_POST
  121. post_init_f,
  122. #endif
  123. NULL, /* Terminate this list,
  124. * beware: this list will be relocated
  125. * which means that NULL will become
  126. * NULL+RELOC_OFFSET. We simply make
  127. * NULL be -RELOC_OFFSET instead.
  128. */
  129. };
  130. /************************************************************************
  131. *
  132. * This is the SPARC board initialization routine, running from RAM.
  133. *
  134. ************************************************************************
  135. */
  136. #ifdef DEBUG_INIT_SEQUENCE
  137. char *str_init_seq = "INIT_SEQ 00\n";
  138. char *str_init_seq_done = "\n\rInit sequence done...\r\n\r\n";
  139. #endif
  140. void board_init_f(ulong bootflag)
  141. {
  142. cmd_tbl_t *cmdtp;
  143. bd_t *bd;
  144. unsigned char *s;
  145. init_fnc_t **init_fnc_ptr;
  146. int j;
  147. int i;
  148. char *e;
  149. #ifndef CONFIG_SYS_NO_FLASH
  150. ulong flash_size;
  151. #endif
  152. gd = (gd_t *) (CONFIG_SYS_GBL_DATA_OFFSET);
  153. /* Clear initial global data */
  154. memset((void *)gd, 0, sizeof(gd_t));
  155. gd->bd = (bd_t *) (gd + 1); /* At end of global data */
  156. gd->baudrate = CONFIG_BAUDRATE;
  157. gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
  158. bd = gd->bd;
  159. bd->bi_memstart = CONFIG_SYS_RAM_BASE;
  160. bd->bi_memsize = CONFIG_SYS_RAM_SIZE;
  161. bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
  162. #if defined(CONFIG_SYS_SRAM_BASE) && defined(CONFIG_SYS_SRAM_SIZE)
  163. bd->bi_sramstart = CONFIG_SYS_SRAM_BASE;
  164. bd->bi_sramsize = CONFIG_SYS_SRAM_SIZE;
  165. #endif
  166. bd->bi_baudrate = CONFIG_BAUDRATE;
  167. bd->bi_bootflags = bootflag; /* boot / reboot flag (for LynxOS) */
  168. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  169. gd->reloc_off = CONFIG_SYS_RELOC_MONITOR_BASE - CONFIG_SYS_MONITOR_BASE;
  170. for (init_fnc_ptr = init_sequence, j = 0; *init_fnc_ptr;
  171. ++init_fnc_ptr, j++) {
  172. #ifdef DEBUG_INIT_SEQUENCE
  173. if (j > 9)
  174. str_init_seq[9] = '0' + (j / 10);
  175. str_init_seq[10] = '0' + (j - (j / 10) * 10);
  176. serial_puts(str_init_seq);
  177. #endif
  178. if ((*init_fnc_ptr + gd->reloc_off) () != 0) {
  179. hang();
  180. }
  181. }
  182. #ifdef DEBUG_INIT_SEQUENCE
  183. serial_puts(str_init_seq_done);
  184. #endif
  185. /*
  186. * Now that we have DRAM mapped and working, we can
  187. * relocate the code and continue running from DRAM.
  188. *
  189. * Reserve memory at end of RAM for (top down in that order):
  190. * - kernel log buffer
  191. * - protected RAM
  192. * - LCD framebuffer
  193. * - monitor code
  194. * - board info struct
  195. */
  196. #ifdef DEBUG_MEM_LAYOUT
  197. printf("CONFIG_SYS_MONITOR_BASE: 0x%lx\n", CONFIG_SYS_MONITOR_BASE);
  198. printf("CONFIG_ENV_ADDR: 0x%lx\n", CONFIG_ENV_ADDR);
  199. printf("CONFIG_SYS_RELOC_MONITOR_BASE: 0x%lx (%d)\n", CONFIG_SYS_RELOC_MONITOR_BASE,
  200. CONFIG_SYS_MONITOR_LEN);
  201. printf("CONFIG_SYS_MALLOC_BASE: 0x%lx (%d)\n", CONFIG_SYS_MALLOC_BASE,
  202. CONFIG_SYS_MALLOC_LEN);
  203. printf("CONFIG_SYS_INIT_SP_OFFSET: 0x%lx (%d)\n", CONFIG_SYS_INIT_SP_OFFSET,
  204. CONFIG_SYS_STACK_SIZE);
  205. printf("CONFIG_SYS_PROM_OFFSET: 0x%lx (%d)\n", CONFIG_SYS_PROM_OFFSET,
  206. CONFIG_SYS_PROM_SIZE);
  207. printf("CONFIG_SYS_GBL_DATA_OFFSET: 0x%lx (%d)\n", CONFIG_SYS_GBL_DATA_OFFSET,
  208. CONFIG_SYS_GBL_DATA_SIZE);
  209. #endif
  210. #ifdef CONFIG_POST
  211. post_bootmode_init();
  212. post_run(NULL, POST_ROM | post_bootmode_get(0));
  213. #endif
  214. /*
  215. * We have to relocate the command table manually
  216. */
  217. for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
  218. ulong addr;
  219. addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
  220. #if DEBUG_COMMANDS
  221. printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
  222. cmdtp->name, (ulong) (cmdtp->cmd), addr);
  223. #endif
  224. cmdtp->cmd =
  225. (int (*)(struct cmd_tbl_s *, int, int, char *[]))addr;
  226. addr = (ulong) (cmdtp->name) + gd->reloc_off;
  227. cmdtp->name = (char *)addr;
  228. if (cmdtp->usage) {
  229. addr = (ulong) (cmdtp->usage) + gd->reloc_off;
  230. cmdtp->usage = (char *)addr;
  231. }
  232. #ifdef CONFIG_SYS_LONGHELP
  233. if (cmdtp->help) {
  234. addr = (ulong) (cmdtp->help) + gd->reloc_off;
  235. cmdtp->help = (char *)addr;
  236. }
  237. #endif
  238. }
  239. #if defined(CONFIG_CMD_AMBAPP) && defined(CONFIG_SYS_AMBAPP_PRINT_ON_STARTUP)
  240. puts("AMBA:\n");
  241. do_ambapp_print(NULL, 0, 0, NULL);
  242. #endif
  243. /* initialize higher level parts of CPU like time base and timers */
  244. cpu_init_r();
  245. /* start timer */
  246. timer_interrupt_init();
  247. /*
  248. * Enable Interrupts before any calls to udelay,
  249. * the flash driver may use udelay resulting in
  250. * a hang if not timer0 IRQ is enabled.
  251. */
  252. interrupt_init();
  253. /* The Malloc area is immediately below the monitor copy in RAM */
  254. mem_malloc_init(CONFIG_SYS_MALLOC_BASE,
  255. CONFIG_SYS_MALLOC_END - CONFIG_SYS_MALLOC_BASE);
  256. malloc_bin_reloc();
  257. #if !defined(CONFIG_SYS_NO_FLASH)
  258. puts("FLASH: ");
  259. if ((flash_size = flash_init()) > 0) {
  260. # ifdef CONFIG_SYS_FLASH_CHECKSUM
  261. print_size(flash_size, "");
  262. /*
  263. * Compute and print flash CRC if flashchecksum is set to 'y'
  264. *
  265. * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
  266. */
  267. s = getenv("flashchecksum");
  268. if (s && (*s == 'y')) {
  269. printf(" CRC: %08lX",
  270. crc32(0, (const unsigned char *)CONFIG_SYS_FLASH_BASE,
  271. flash_size)
  272. );
  273. }
  274. putc('\n');
  275. # else /* !CONFIG_SYS_FLASH_CHECKSUM */
  276. print_size(flash_size, "\n");
  277. # endif /* CONFIG_SYS_FLASH_CHECKSUM */
  278. } else {
  279. puts(failed);
  280. hang();
  281. }
  282. bd->bi_flashstart = CONFIG_SYS_FLASH_BASE; /* update start of FLASH memory */
  283. bd->bi_flashsize = flash_size; /* size of FLASH memory (final value) */
  284. #if CONFIG_SYS_MONITOR_BASE == CONFIG_SYS_FLASH_BASE
  285. bd->bi_flashoffset = monitor_flash_len; /* reserved area for startup monitor */
  286. #else
  287. bd->bi_flashoffset = 0;
  288. #endif
  289. #else /* CONFIG_SYS_NO_FLASH */
  290. bd->bi_flashsize = 0;
  291. bd->bi_flashstart = 0;
  292. bd->bi_flashoffset = 0;
  293. #endif /* !CONFIG_SYS_NO_FLASH */
  294. #ifdef CONFIG_SPI
  295. # if !defined(CONFIG_ENV_IS_IN_EEPROM)
  296. spi_init_f();
  297. # endif
  298. spi_init_r();
  299. #endif
  300. /* relocate environment function pointers etc. */
  301. env_relocate();
  302. #if defined(CONFIG_BOARD_LATE_INIT)
  303. board_late_init();
  304. #endif
  305. #ifdef CONFIG_ID_EEPROM
  306. mac_read_from_eeprom();
  307. #endif
  308. /* IP Address */
  309. bd->bi_ip_addr = getenv_IPaddr("ipaddr");
  310. #if defined(CONFIG_PCI)
  311. /*
  312. * Do pci configuration
  313. */
  314. pci_init();
  315. #endif
  316. /* Initialize stdio devices */
  317. stdio_init();
  318. /* Initialize the jump table for applications */
  319. jumptable_init();
  320. /* Initialize the console (after the relocation and devices init) */
  321. console_init_r();
  322. #ifdef CONFIG_SERIAL_SOFTWARE_FIFO
  323. serial_buffered_init();
  324. #endif
  325. #ifdef CONFIG_STATUS_LED
  326. status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
  327. #endif
  328. udelay(20);
  329. set_timer(0);
  330. /* Initialize from environment */
  331. if ((s = getenv("loadaddr")) != NULL) {
  332. load_addr = simple_strtoul(s, NULL, 16);
  333. }
  334. #if defined(CONFIG_CMD_NET)
  335. if ((s = getenv("bootfile")) != NULL) {
  336. copy_filename(BootFile, s, sizeof(BootFile));
  337. }
  338. #endif /* CONFIG_CMD_NET */
  339. WATCHDOG_RESET();
  340. #if defined(CONFIG_CMD_DOC)
  341. WATCHDOG_RESET();
  342. puts("DOC: ");
  343. doc_init();
  344. #endif
  345. #if defined(CONFIG_CMD_NET)
  346. #if defined(CONFIG_NET_MULTI)
  347. WATCHDOG_RESET();
  348. puts("Net: ");
  349. #endif
  350. eth_initialize(bd);
  351. #endif
  352. #if defined(CONFIG_CMD_NET) && defined(CONFIG_RESET_PHY_R)
  353. WATCHDOG_RESET();
  354. debug("Reset Ethernet PHY\n");
  355. reset_phy();
  356. #endif
  357. #ifdef CONFIG_POST
  358. post_run(NULL, POST_RAM | post_bootmode_get(0));
  359. #endif
  360. #if defined(CONFIG_CMD_IDE)
  361. WATCHDOG_RESET();
  362. puts("IDE: ");
  363. ide_init();
  364. #endif /* CONFIG_CMD_IDE */
  365. #ifdef CONFIG_LAST_STAGE_INIT
  366. WATCHDOG_RESET();
  367. /*
  368. * Some parts can be only initialized if all others (like
  369. * Interrupts) are up and running (i.e. the PC-style ISA
  370. * keyboard).
  371. */
  372. last_stage_init();
  373. #endif
  374. #ifdef CONFIG_PS2KBD
  375. puts("PS/2: ");
  376. kbd_init();
  377. #endif
  378. prom_init();
  379. /* main_loop */
  380. for (;;) {
  381. WATCHDOG_RESET();
  382. main_loop();
  383. }
  384. }
  385. void hang(void)
  386. {
  387. puts("### ERROR ### Please RESET the board ###\n");
  388. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  389. show_boot_progress(-30);
  390. #endif
  391. for (;;) ;
  392. }
  393. /************************************************************************/