board.c 11 KB

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