board.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * U-boot - board.c First C file to be called contains init routines
  3. *
  4. * Copyright (c) 2005-2008 Analog Devices Inc.
  5. *
  6. * (C) Copyright 2000-2004
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. *
  9. * Licensed under the GPL-2 or later.
  10. */
  11. #include <common.h>
  12. #include <command.h>
  13. #include <stdio_dev.h>
  14. #include <serial.h>
  15. #include <environment.h>
  16. #include <malloc.h>
  17. #include <mmc.h>
  18. #include <net.h>
  19. #include <status_led.h>
  20. #include <version.h>
  21. #include <asm/cplb.h>
  22. #include <asm/mach-common/bits/mpu.h>
  23. #include <kgdb.h>
  24. #ifdef CONFIG_CMD_NAND
  25. #include <nand.h> /* cannot even include nand.h if it isnt configured */
  26. #endif
  27. #ifdef CONFIG_BITBANGMII
  28. #include <miiphy.h>
  29. #endif
  30. #if defined(CONFIG_POST)
  31. #include <post.h>
  32. int post_flag;
  33. #endif
  34. DECLARE_GLOBAL_DATA_PTR;
  35. __attribute__((always_inline))
  36. static inline void serial_early_puts(const char *s)
  37. {
  38. #ifdef CONFIG_DEBUG_EARLY_SERIAL
  39. serial_puts("Early: ");
  40. serial_puts(s);
  41. #endif
  42. }
  43. static int display_banner(void)
  44. {
  45. display_options();
  46. printf("CPU: ADSP %s "
  47. "(Detected Rev: 0.%d) "
  48. "(%s boot)\n",
  49. gd->bd->bi_cpu,
  50. bfin_revid(),
  51. get_bfin_boot_mode(CONFIG_BFIN_BOOT_MODE));
  52. return 0;
  53. }
  54. static int init_baudrate(void)
  55. {
  56. gd->baudrate = getenv_ulong("baudrate", 10, CONFIG_BAUDRATE);
  57. return 0;
  58. }
  59. static void display_global_data(void)
  60. {
  61. bd_t *bd;
  62. #ifndef CONFIG_DEBUG_EARLY_SERIAL
  63. return;
  64. #endif
  65. bd = gd->bd;
  66. printf(" gd: %p\n", gd);
  67. printf(" |-flags: %lx\n", gd->flags);
  68. printf(" |-board_type: %lx\n", gd->board_type);
  69. printf(" |-baudrate: %lu\n", gd->baudrate);
  70. printf(" |-have_console: %lx\n", gd->have_console);
  71. printf(" |-ram_size: %lx\n", gd->ram_size);
  72. printf(" |-env_addr: %lx\n", gd->env_addr);
  73. printf(" |-env_valid: %lx\n", gd->env_valid);
  74. printf(" |-jt(%p): %p\n", gd->jt, *(gd->jt));
  75. printf(" \\-bd: %p\n", gd->bd);
  76. printf(" |-bi_baudrate: %x\n", bd->bi_baudrate);
  77. printf(" |-bi_ip_addr: %lx\n", bd->bi_ip_addr);
  78. printf(" |-bi_boot_params: %lx\n", bd->bi_boot_params);
  79. printf(" |-bi_memstart: %lx\n", bd->bi_memstart);
  80. printf(" |-bi_memsize: %lx\n", bd->bi_memsize);
  81. printf(" |-bi_flashstart: %lx\n", bd->bi_flashstart);
  82. printf(" |-bi_flashsize: %lx\n", bd->bi_flashsize);
  83. printf(" \\-bi_flashoffset: %lx\n", bd->bi_flashoffset);
  84. }
  85. #define CPLB_PAGE_SIZE (4 * 1024 * 1024)
  86. #define CPLB_PAGE_MASK (~(CPLB_PAGE_SIZE - 1))
  87. void init_cplbtables(void)
  88. {
  89. volatile uint32_t *ICPLB_ADDR, *ICPLB_DATA;
  90. volatile uint32_t *DCPLB_ADDR, *DCPLB_DATA;
  91. uint32_t extern_memory;
  92. size_t i;
  93. void icplb_add(uint32_t addr, uint32_t data)
  94. {
  95. *(ICPLB_ADDR + i) = addr;
  96. *(ICPLB_DATA + i) = data;
  97. }
  98. void dcplb_add(uint32_t addr, uint32_t data)
  99. {
  100. *(DCPLB_ADDR + i) = addr;
  101. *(DCPLB_DATA + i) = data;
  102. }
  103. /* populate a few common entries ... we'll let
  104. * the memory map and cplb exception handler do
  105. * the rest of the work.
  106. */
  107. i = 0;
  108. ICPLB_ADDR = (uint32_t *)ICPLB_ADDR0;
  109. ICPLB_DATA = (uint32_t *)ICPLB_DATA0;
  110. DCPLB_ADDR = (uint32_t *)DCPLB_ADDR0;
  111. DCPLB_DATA = (uint32_t *)DCPLB_DATA0;
  112. icplb_add(0xFFA00000, L1_IMEMORY);
  113. dcplb_add(0xFF800000, L1_DMEMORY);
  114. ++i;
  115. if (CONFIG_MEM_SIZE) {
  116. uint32_t mbase = CONFIG_SYS_MONITOR_BASE;
  117. uint32_t mend = mbase + CONFIG_SYS_MONITOR_LEN;
  118. mbase &= CPLB_PAGE_MASK;
  119. mend &= CPLB_PAGE_MASK;
  120. icplb_add(mbase, SDRAM_IKERNEL);
  121. dcplb_add(mbase, SDRAM_DKERNEL);
  122. ++i;
  123. /*
  124. * If the monitor crosses a 4 meg boundary, we'll need
  125. * to lock two entries for it. We assume it doesn't
  126. * cross two 4 meg boundaries ...
  127. */
  128. if (mbase != mend) {
  129. icplb_add(mend, SDRAM_IKERNEL);
  130. dcplb_add(mend, SDRAM_DKERNEL);
  131. ++i;
  132. }
  133. }
  134. icplb_add(0x20000000, SDRAM_INON_CHBL);
  135. dcplb_add(0x20000000, SDRAM_EBIU);
  136. ++i;
  137. /* Add entries for the rest of external RAM up to the bootrom */
  138. extern_memory = 0;
  139. #ifdef CONFIG_DEBUG_NULL_PTR
  140. icplb_add(extern_memory, (SDRAM_IKERNEL & ~PAGE_SIZE_MASK) | PAGE_SIZE_1KB);
  141. dcplb_add(extern_memory, (SDRAM_DKERNEL & ~PAGE_SIZE_MASK) | PAGE_SIZE_1KB);
  142. ++i;
  143. icplb_add(extern_memory, SDRAM_IKERNEL);
  144. dcplb_add(extern_memory, SDRAM_DKERNEL);
  145. extern_memory += CPLB_PAGE_SIZE;
  146. ++i;
  147. #endif
  148. while (i < 16 && extern_memory < (CONFIG_SYS_MONITOR_BASE & CPLB_PAGE_MASK)) {
  149. icplb_add(extern_memory, SDRAM_IGENERIC);
  150. dcplb_add(extern_memory, SDRAM_DGENERIC);
  151. extern_memory += CPLB_PAGE_SIZE;
  152. ++i;
  153. }
  154. while (i < 16) {
  155. icplb_add(0, 0);
  156. dcplb_add(0, 0);
  157. ++i;
  158. }
  159. }
  160. /*
  161. * All attempts to come up with a "common" initialization sequence
  162. * that works for all boards and architectures failed: some of the
  163. * requirements are just _too_ different. To get rid of the resulting
  164. * mess of board dependend #ifdef'ed code we now make the whole
  165. * initialization sequence configurable to the user.
  166. *
  167. * The requirements for any new initalization function is simple: it
  168. * receives a pointer to the "global data" structure as it's only
  169. * argument, and returns an integer return code, where 0 means
  170. * "continue" and != 0 means "fatal error, hang the system".
  171. */
  172. extern int watchdog_init(void);
  173. extern int exception_init(void);
  174. extern int irq_init(void);
  175. extern int timer_init(void);
  176. void board_init_f(ulong bootflag)
  177. {
  178. bd_t *bd;
  179. char buf[32];
  180. #ifdef CONFIG_BOARD_EARLY_INIT_F
  181. serial_early_puts("Board early init flash\n");
  182. board_early_init_f();
  183. #endif
  184. serial_early_puts("Init CPLB tables\n");
  185. init_cplbtables();
  186. serial_early_puts("Exceptions setup\n");
  187. exception_init();
  188. #ifndef CONFIG_ICACHE_OFF
  189. serial_early_puts("Turn on ICACHE\n");
  190. icache_enable();
  191. #endif
  192. #ifndef CONFIG_DCACHE_OFF
  193. serial_early_puts("Turn on DCACHE\n");
  194. dcache_enable();
  195. #endif
  196. #ifdef CONFIG_WATCHDOG
  197. serial_early_puts("Setting up external watchdog\n");
  198. watchdog_init();
  199. #endif
  200. #ifdef DEBUG
  201. if (GENERATED_GBL_DATA_SIZE < sizeof(*gd))
  202. hang();
  203. #endif
  204. serial_early_puts("Init global data\n");
  205. gd = (gd_t *) (CONFIG_SYS_GBL_DATA_ADDR);
  206. memset((void *)gd, 0, GENERATED_GBL_DATA_SIZE);
  207. bd = (bd_t *) (CONFIG_SYS_BD_INFO_ADDR);
  208. gd->bd = bd;
  209. memset((void *)bd, 0, GENERATED_BD_INFO_SIZE);
  210. bd->bi_r_version = version_string;
  211. bd->bi_cpu = MK_STR(CONFIG_BFIN_CPU);
  212. bd->bi_board_name = BFIN_BOARD_NAME;
  213. bd->bi_vco = get_vco();
  214. bd->bi_cclk = get_cclk();
  215. bd->bi_sclk = get_sclk();
  216. bd->bi_memstart = CONFIG_SYS_SDRAM_BASE;
  217. bd->bi_memsize = CONFIG_SYS_MAX_RAM_SIZE;
  218. /* Initialize */
  219. serial_early_puts("IRQ init\n");
  220. irq_init();
  221. serial_early_puts("Environment init\n");
  222. env_init();
  223. serial_early_puts("Baudrate init\n");
  224. init_baudrate();
  225. serial_early_puts("Serial init\n");
  226. serial_init();
  227. #ifdef CONFIG_SERIAL_MULTI
  228. serial_initialize();
  229. #endif
  230. serial_early_puts("Console init flash\n");
  231. console_init_f();
  232. serial_early_puts("End of early debugging\n");
  233. display_banner();
  234. checkboard();
  235. timer_init();
  236. printf("Clock: VCO: %s MHz, ", strmhz(buf, get_vco()));
  237. printf("Core: %s MHz, ", strmhz(buf, get_cclk()));
  238. printf("System: %s MHz\n", strmhz(buf, get_sclk()));
  239. if (CONFIG_MEM_SIZE) {
  240. printf("RAM: ");
  241. print_size(bd->bi_memsize, "\n");
  242. }
  243. #if defined(CONFIG_POST)
  244. post_init_f();
  245. post_bootmode_init();
  246. post_run(NULL, POST_ROM | post_bootmode_get(0));
  247. #endif
  248. board_init_r((gd_t *) gd, 0x20000010);
  249. }
  250. static void board_net_init_r(bd_t *bd)
  251. {
  252. #ifdef CONFIG_BITBANGMII
  253. bb_miiphy_init();
  254. #endif
  255. #ifdef CONFIG_CMD_NET
  256. char *s;
  257. if ((s = getenv("bootfile")) != NULL)
  258. copy_filename(BootFile, s, sizeof(BootFile));
  259. bd->bi_ip_addr = getenv_IPaddr("ipaddr");
  260. printf("Net: ");
  261. eth_initialize(gd->bd);
  262. #endif
  263. }
  264. void board_init_r(gd_t * id, ulong dest_addr)
  265. {
  266. char *s;
  267. bd_t *bd;
  268. gd = id;
  269. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  270. bd = gd->bd;
  271. #if defined(CONFIG_POST)
  272. post_output_backlog();
  273. #endif
  274. /* initialize malloc() area */
  275. mem_malloc_init(CONFIG_SYS_MALLOC_BASE, CONFIG_SYS_MALLOC_LEN);
  276. #if !defined(CONFIG_SYS_NO_FLASH)
  277. /* Initialize the flash and protect u-boot by default */
  278. extern flash_info_t flash_info[];
  279. puts("Flash: ");
  280. ulong size = flash_init();
  281. print_size(size, "\n");
  282. flash_protect(FLAG_PROTECT_SET, CONFIG_SYS_FLASH_BASE,
  283. CONFIG_SYS_FLASH_BASE + CONFIG_SYS_MONITOR_LEN - 1,
  284. &flash_info[0]);
  285. bd->bi_flashstart = CONFIG_SYS_FLASH_BASE;
  286. bd->bi_flashsize = size;
  287. bd->bi_flashoffset = 0;
  288. #else
  289. bd->bi_flashstart = 0;
  290. bd->bi_flashsize = 0;
  291. bd->bi_flashoffset = 0;
  292. #endif
  293. #ifdef CONFIG_CMD_NAND
  294. puts("NAND: ");
  295. nand_init(); /* go init the NAND */
  296. #endif
  297. #ifdef CONFIG_GENERIC_MMC
  298. puts("MMC: ");
  299. mmc_initialize(bd);
  300. #endif
  301. /* relocate environment function pointers etc. */
  302. env_relocate();
  303. /* Initialize stdio devices */
  304. stdio_init();
  305. jumptable_init();
  306. /* Initialize the console (after the relocation and devices init) */
  307. console_init_r();
  308. #ifdef CONFIG_CMD_KGDB
  309. puts("KGDB: ");
  310. kgdb_init();
  311. #endif
  312. #ifdef CONFIG_STATUS_LED
  313. status_led_set(STATUS_LED_BOOT, STATUS_LED_BLINKING);
  314. status_led_set(STATUS_LED_CRASH, STATUS_LED_OFF);
  315. #endif
  316. /* Initialize from environment */
  317. load_addr = getenv_ulong("loadaddr", 16, load_addr);
  318. #if defined(CONFIG_MISC_INIT_R)
  319. /* miscellaneous platform dependent initialisations */
  320. misc_init_r();
  321. #endif
  322. board_net_init_r(bd);
  323. display_global_data();
  324. #if defined(CONFIG_POST)
  325. if (post_flag)
  326. post_run(NULL, POST_RAM | post_bootmode_get(0));
  327. #endif
  328. if (CONFIG_MEM_SIZE && bfin_os_log_check()) {
  329. puts("\nLog buffer from operating system:\n");
  330. bfin_os_log_dump();
  331. puts("\n");
  332. }
  333. /* main_loop() can return to retry autoboot, if so just run it again. */
  334. for (;;)
  335. main_loop();
  336. }
  337. void hang(void)
  338. {
  339. #ifdef CONFIG_STATUS_LED
  340. status_led_set(STATUS_LED_BOOT, STATUS_LED_OFF);
  341. status_led_set(STATUS_LED_CRASH, STATUS_LED_BLINKING);
  342. #endif
  343. puts("### ERROR ### Please RESET the board ###\n");
  344. while (1)
  345. /* If a JTAG emulator is hooked up, we'll automatically trigger
  346. * a breakpoint in it. If one isn't, this is just a NOP.
  347. */
  348. asm("emuexcpt;");
  349. }