board.c 10.0 KB

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