board.c 11 KB

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