board.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * (C) Copyright 2000-2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <watchdog.h>
  25. #include <command.h>
  26. #include <malloc.h>
  27. #include <devices.h>
  28. #include <syscall.h>
  29. #if (CONFIG_COMMANDS & CFG_CMD_IDE)
  30. #include <ide.h>
  31. #endif
  32. #if (CONFIG_COMMANDS & CFG_CMD_SCSI)
  33. #include <scsi.h>
  34. #endif
  35. #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
  36. #include <kgdb.h>
  37. #endif
  38. #ifdef CONFIG_STATUS_LED
  39. #include <status_led.h>
  40. #endif
  41. #include <net.h>
  42. #if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
  43. #include <cmd_bedbug.h>
  44. #endif
  45. #ifdef CFG_ALLOC_DPRAM
  46. #include <commproc.h>
  47. #endif
  48. #include <version.h>
  49. static char *failed = "*** failed ***\n";
  50. #ifdef CONFIG_PCU_E
  51. extern flash_info_t flash_info[];
  52. #endif
  53. #if defined(CFG_ENV_IS_IN_FLASH)
  54. # ifndef CFG_ENV_ADDR
  55. # define CFG_ENV_ADDR (CFG_FLASH_BASE + CFG_ENV_OFFSET)
  56. # endif
  57. # ifndef CFG_ENV_SIZE
  58. # define CFG_ENV_SIZE CFG_ENV_SECT_SIZE
  59. # endif
  60. # if (CFG_ENV_ADDR >= CFG_MONITOR_BASE) && \
  61. (CFG_ENV_ADDR+CFG_ENV_SIZE) < (CFG_MONITOR_BASE + CFG_MONITOR_LEN)
  62. # define ENV_IS_EMBEDDED
  63. # endif
  64. #endif /* CFG_ENV_IS_IN_FLASH */
  65. #if ( ((CFG_ENV_ADDR+CFG_ENV_SIZE) < CFG_MONITOR_BASE) || \
  66. (CFG_ENV_ADDR >= (CFG_MONITOR_BASE + CFG_MONITOR_LEN)) ) || \
  67. defined(CFG_ENV_IS_IN_NVRAM)
  68. #define TOTAL_MALLOC_LEN (CFG_MALLOC_LEN + CFG_ENV_SIZE)
  69. #else
  70. #define TOTAL_MALLOC_LEN CFG_MALLOC_LEN
  71. #endif
  72. /*
  73. * Begin and End of memory area for malloc(), and current "brk"
  74. */
  75. static ulong mem_malloc_start = 0;
  76. static ulong mem_malloc_end = 0;
  77. static ulong mem_malloc_brk = 0;
  78. /************************************************************************
  79. * Utilities *
  80. ************************************************************************
  81. */
  82. /*
  83. * The Malloc area is immediately below the monitor copy in DRAM
  84. */
  85. static void mem_malloc_init (ulong dest_addr)
  86. {
  87. mem_malloc_end = dest_addr;
  88. mem_malloc_start = dest_addr - TOTAL_MALLOC_LEN;
  89. mem_malloc_brk = mem_malloc_start;
  90. memset ((void *) mem_malloc_start, 0,
  91. mem_malloc_end - mem_malloc_start);
  92. }
  93. void *sbrk (ptrdiff_t increment)
  94. {
  95. ulong old = mem_malloc_brk;
  96. ulong new = old + increment;
  97. if ((new < mem_malloc_start) || (new > mem_malloc_end)) {
  98. return (NULL);
  99. }
  100. mem_malloc_brk = new;
  101. return ((void *) old);
  102. }
  103. char *strmhz (char *buf, long hz)
  104. {
  105. long l, n;
  106. long m;
  107. n = hz / 1000000L;
  108. l = sprintf (buf, "%ld", n);
  109. m = (hz % 1000000L) / 1000L;
  110. if (m != 0)
  111. sprintf (buf + l, ".%03ld", m);
  112. return (buf);
  113. }
  114. static void syscalls_init (int reloc_off)
  115. {
  116. ulong *addr;
  117. addr = (ulong *) syscall_tbl;
  118. syscall_tbl[SYSCALL_MALLOC] = (void *) malloc;
  119. syscall_tbl[SYSCALL_FREE] = (void *) free;
  120. syscall_tbl[SYSCALL_INSTALL_HDLR] = (void *) irq_install_handler;
  121. syscall_tbl[SYSCALL_FREE_HDLR] = (void *) irq_free_handler;
  122. addr = (ulong *) 0xc00; /* syscall ISR addr */
  123. /* patch ISR code */
  124. *addr++ |= (ulong) syscall_tbl >> 16;
  125. *addr++ |= (ulong) syscall_tbl & 0xFFFF;
  126. *addr++ |= NR_SYSCALLS >> 16;
  127. *addr++ |= NR_SYSCALLS & 0xFFFF;
  128. }
  129. /************************************************************************
  130. *
  131. * This is the first part of the initialization sequence that is
  132. * implemented in C, but still running from ROM.
  133. *
  134. * The main purpose is to provide a (serial) console interface as
  135. * soon as possible (so we can see any error messages), and to
  136. * initialize the RAM so that we can relocate the monitor code to
  137. * RAM.
  138. *
  139. * Be aware of the restrictions: global data is read-only, BSS is not
  140. * initialized, and stack space is limited to a few kB.
  141. *
  142. ************************************************************************
  143. */
  144. gd_t *global_data;
  145. static gd_t gdata;
  146. static bd_t bdata;
  147. void board_init_f (ulong bootflag)
  148. {
  149. DECLARE_GLOBAL_DATA_PTR;
  150. bd_t *bd;
  151. ulong reg, len, addr, addr_sp, dram_size;
  152. int i, baudrate, board_type;
  153. char *s, *e;
  154. uchar tmp[64]; /* long enough for environment variables */
  155. /* Pointer to initial global data area */
  156. gd = global_data = &gdata;
  157. bd = gd->bd = &bdata;
  158. init_timebase ();
  159. env_init ();
  160. i = getenv_r ("baudrate", tmp, sizeof (tmp));
  161. baudrate =
  162. (i > 0) ? (int) simple_strtoul (tmp, NULL,
  163. 10) : CONFIG_BAUDRATE;
  164. bd->bi_baudrate = baudrate; /* Console Baudrate */
  165. /* set up serial port */
  166. serial_init ();
  167. /* Initialize the console (before the relocation) */
  168. console_init_f ();
  169. #ifdef DEBUG
  170. if (sizeof (init_data_t) > CFG_INIT_DATA_SIZE) {
  171. printf ("PANIC: sizeof(init_data_t)=%d > CFG_INIT_DATA_SIZE=%d\n", sizeof (init_data_t), CFG_INIT_DATA_SIZE);
  172. hang ();
  173. }
  174. #endif /* DEBUG */
  175. /* now we can use standard printf/puts/getc/tstc functions */
  176. display_options ();
  177. puts ("CPU: "); /* Check CPU */
  178. if (checkcpu () < 0) {
  179. puts (failed);
  180. hang ();
  181. }
  182. puts ("Board: "); /* Check Board */
  183. if ((board_type = checkboard ()) < 0) {
  184. puts (failed);
  185. hang ();
  186. }
  187. puts ("DRAM: ");
  188. if ((dram_size = initdram (board_type)) > 0) {
  189. printf ("%2ld MB\n", dram_size >> 20);
  190. } else {
  191. puts (failed);
  192. hang ();
  193. }
  194. #if defined(CFG_DRAM_TEST)
  195. if (testdram () != 0) {
  196. hang ();
  197. }
  198. #endif /* CFG_DRAM_TEST */
  199. /*
  200. * Now that we have DRAM mapped and working, we can
  201. * relocate the code and continue running from DRAM.
  202. *
  203. * Reserve memory at end of RAM for (top down in that order):
  204. * - protected RAM
  205. * - LCD framebuffer
  206. * - monitor code
  207. * - board info struct
  208. */
  209. len = get_endaddr () - CFG_MONITOR_BASE;
  210. if (len > CFG_MONITOR_LEN) {
  211. printf ("*** u-boot size %ld > reserved memory (%d)\n",
  212. len, CFG_MONITOR_LEN);
  213. hang ();
  214. }
  215. if (CFG_MONITOR_LEN > len)
  216. len = CFG_MONITOR_LEN;
  217. addr = CFG_SDRAM_BASE + dram_size;
  218. addr -= len;
  219. /*
  220. * Save local variables to board info struct
  221. */
  222. bd->bi_memstart = CFG_SDRAM_BASE; /* start of DRAM memory */
  223. bd->bi_memsize = dram_size; /* size of DRAM memory in bytes */
  224. bd->bi_bootflags = bootflag; /* boot / reboot flag (for LynxOS) */
  225. i = getenv_r ("ethaddr", tmp, sizeof (tmp));
  226. s = (i > 0) ? tmp : NULL;
  227. for (reg = 0; reg < 6; ++reg) {
  228. bd->bi_enetaddr[reg] = s ? simple_strtoul (s, &e, 16) : 0;
  229. if (s)
  230. s = (*e) ? e + 1 : e;
  231. }
  232. bd->bi_intfreq = get_gclk_freq (); /* Internal Freq, in Hz */
  233. bd->bi_busfreq = get_bus_freq (get_gclk_freq ()); /* Bus Freq, in Hz */
  234. #ifdef CFG_EXTBDINFO
  235. strncpy (bd->bi_s_version, "1.2", sizeof (bd->bi_s_version));
  236. strncpy (bd->bi_r_version, PPCBOOT_VERSION,
  237. sizeof (bd->bi_r_version));
  238. bd->bi_procfreq = get_gclk_freq (); /* Processor Speed, In Hz */
  239. bd->bi_plb_busfreq = bd->bi_busfreq;
  240. #endif
  241. board_init_final (addr);
  242. }
  243. /************************************************************************
  244. *
  245. * This is the next part if the initialization sequence: we are now
  246. * running from RAM and have a "normal" C environment, i. e. global
  247. * data can be written, BSS has been cleared, the stack size in not
  248. * that critical any more, etc.
  249. *
  250. ************************************************************************
  251. */
  252. void board_init_final (ulong dest_addr)
  253. {
  254. DECLARE_GLOBAL_DATA_PTR;
  255. char *s;
  256. cmd_tbl_t *cmdtp;
  257. ulong flash_size;
  258. bd_t *bd;
  259. bd = gd->bd;
  260. /* icache_enable(); /XX* it's time to enable the instruction cache */
  261. /*
  262. * Setup trap handlers
  263. */
  264. trap_init (dest_addr);
  265. puts ("FLASH: ");
  266. if ((flash_size = flash_init ()) > 0) {
  267. #ifdef CFG_FLASH_CHECKSUM
  268. if (flash_size >= (1 << 20)) {
  269. printf ("%2ld MB", flash_size >> 20);
  270. } else {
  271. printf ("%2ld kB", flash_size >> 10);
  272. }
  273. /*
  274. * Compute and print flash CRC if flashchecksum is set to 'y'
  275. *
  276. * NOTE: Maybe we should add some WATCHDOG_RESET()? XXX
  277. */
  278. s = getenv ("flashchecksum");
  279. if (s && (*s == 'y')) {
  280. printf (" CRC: %08lX",
  281. crc32 (0,
  282. (const unsigned char *) CFG_FLASH_BASE,
  283. flash_size)
  284. );
  285. }
  286. putc ('\n');
  287. #else
  288. if (flash_size >= (1 << 20)) {
  289. printf ("%2ld MB\n", flash_size >> 20);
  290. } else {
  291. printf ("%2ld kB\n", flash_size >> 10);
  292. }
  293. #endif /* CFG_FLASH_CHECKSUM */
  294. } else {
  295. puts (failed);
  296. hang ();
  297. }
  298. bd->bi_flashstart = CFG_FLASH_BASE; /* update start of FLASH memory */
  299. bd->bi_flashsize = flash_size; /* size of FLASH memory (final value) */
  300. bd->bi_flashoffset = 0x10000; /* reserved area for startup monitor */
  301. WATCHDOG_RESET ();
  302. /* initialize higher level parts of CPU like time base and timers */
  303. cpu_init_r ();
  304. WATCHDOG_RESET ();
  305. /* initialize malloc() area */
  306. mem_malloc_init (dest_addr);
  307. #ifdef CONFIG_SPI
  308. # if !defined(CFG_ENV_IS_IN_EEPROM)
  309. spi_init_f ();
  310. # endif
  311. spi_init_r ();
  312. #endif
  313. /* relocate environment function pointers etc. */
  314. env_relocate ();
  315. /* IP Address */
  316. bd->bi_ip_addr = getenv_IPaddr ("ipaddr");
  317. WATCHDOG_RESET ();
  318. /* allocate syscalls table (console_init_r will fill it in */
  319. syscall_tbl = (void **) malloc (NR_SYSCALLS * sizeof (void *));
  320. /* Initialize the console (after the relocation and devices init) */
  321. #if (CONFIG_COMMANDS & CFG_CMD_NET) && ( \
  322. defined(CONFIG_CCM) || \
  323. defined(CONFIG_EP8260) || \
  324. defined(CONFIG_IP860) || \
  325. defined(CONFIG_IVML24) || \
  326. defined(CONFIG_IVMS8) || \
  327. defined(CONFIG_LWMON) || \
  328. defined(CONFIG_MPC8260ADS) || \
  329. defined(CONFIG_PCU_E) || \
  330. defined(CONFIG_RPXSUPER) || \
  331. defined(CONFIG_SPD823TS) )
  332. WATCHDOG_RESET ();
  333. # ifdef DEBUG
  334. puts ("Reset Ethernet PHY\n");
  335. # endif
  336. reset_phy ();
  337. #endif
  338. #if (CONFIG_COMMANDS & CFG_CMD_KGDB)
  339. WATCHDOG_RESET ();
  340. puts ("KGDB: ");
  341. kgdb_init ();
  342. #endif
  343. /*
  344. * Enable Interrupts
  345. */
  346. interrupt_init ();
  347. udelay (20);
  348. set_timer (0);
  349. /* Insert function pointers now that we have relocated the code */
  350. /* Initialize from environment */
  351. if ((s = getenv ("loadaddr")) != NULL) {
  352. load_addr = simple_strtoul (s, NULL, 16);
  353. }
  354. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  355. if ((s = getenv ("bootfile")) != NULL) {
  356. copy_filename (BootFile, s, sizeof (BootFile));
  357. }
  358. #endif /* CFG_CMD_NET */
  359. WATCHDOG_RESET ();
  360. #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI)
  361. WATCHDOG_RESET ();
  362. puts ("Net: ");
  363. eth_initialize (bd);
  364. #endif
  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. #if (CONFIG_COMMANDS & CFG_CMD_BEDBUG)
  375. WATCHDOG_RESET ();
  376. bedbug_init ();
  377. #endif
  378. #ifdef CONFIG_PRAM
  379. /*
  380. * Export available size of memory for Linux,
  381. * taking into account the protected RAM at top of memory
  382. */
  383. {
  384. ulong pram;
  385. char *s;
  386. uchar memsz[32];
  387. if ((s = getenv ("pram")) != NULL) {
  388. pram = simple_strtoul (s, NULL, 10);
  389. } else {
  390. pram = CONFIG_PRAM;
  391. }
  392. sprintf (memsz, "%ldk", (bd->bi_memsize / 1024) - pram);
  393. setenv ("mem", memsz);
  394. }
  395. #endif
  396. /* Initialization complete - start the monitor */
  397. /* main_loop() can return to retry autoboot, if so just run it again. */
  398. for (;;) {
  399. WATCHDOG_RESET ();
  400. main_loop ();
  401. }
  402. /* NOTREACHED - no way out of command loop except booting */
  403. }
  404. void hang (void)
  405. {
  406. puts ("### ERROR ### Please RESET the board ###\n");
  407. for (;;);
  408. }