board.c 11 KB

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