board.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * U-boot - board.c First C file to be called contains init routines
  3. *
  4. * Copyright (c) 2005 blackfin.uclinux.org
  5. *
  6. * (C) Copyright 2000-2004
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <command.h>
  29. #include <malloc.h>
  30. #include <devices.h>
  31. #include <version.h>
  32. #include <net.h>
  33. #include <environment.h>
  34. #include <i2c.h>
  35. #include "blackfin_board.h"
  36. #include <asm/cplb.h>
  37. #include "../drivers/smc91111.h"
  38. #if defined(CONFIG_BF537)&&defined(CONFIG_POST)
  39. #include <post.h>
  40. int post_flag;
  41. #endif
  42. #ifdef DEBUG
  43. #define pr_debug(fmt,arg...) printf(fmt,##arg)
  44. #else
  45. static inline int
  46. __attribute__ ((format(printf, 1, 2))) pr_debug(const char *fmt, ...)
  47. {
  48. return 0;
  49. }
  50. #endif
  51. #ifndef CFG_NO_FLASH
  52. extern flash_info_t flash_info[];
  53. #endif
  54. static inline u_long get_vco(void)
  55. {
  56. u_long msel;
  57. u_long vco;
  58. msel = (*pPLL_CTL >> 9) & 0x3F;
  59. if (0 == msel)
  60. msel = 64;
  61. vco = CONFIG_CLKIN_HZ;
  62. vco >>= (1 & *pPLL_CTL); /* DF bit */
  63. vco = msel * vco;
  64. return vco;
  65. }
  66. /*Get the Core clock*/
  67. u_long get_cclk(void)
  68. {
  69. u_long csel, ssel;
  70. if (*pPLL_STAT & 0x1)
  71. return CONFIG_CLKIN_HZ;
  72. ssel = *pPLL_DIV;
  73. csel = ((ssel >> 4) & 0x03);
  74. ssel &= 0xf;
  75. if (ssel && ssel < (1 << csel)) /* SCLK > CCLK */
  76. return get_vco() / ssel;
  77. return get_vco() >> csel;
  78. }
  79. /* Get the System clock */
  80. u_long get_sclk(void)
  81. {
  82. u_long ssel;
  83. if (*pPLL_STAT & 0x1)
  84. return CONFIG_CLKIN_HZ;
  85. ssel = (*pPLL_DIV & 0xf);
  86. return get_vco() / ssel;
  87. }
  88. static void mem_malloc_init(void)
  89. {
  90. mem_malloc_start = CFG_MALLOC_BASE;
  91. mem_malloc_end = (CFG_MALLOC_BASE + CFG_MALLOC_LEN);
  92. mem_malloc_brk = mem_malloc_start;
  93. memset((void *)mem_malloc_start, 0, mem_malloc_end - mem_malloc_start);
  94. }
  95. void *sbrk(ptrdiff_t increment)
  96. {
  97. ulong old = mem_malloc_brk;
  98. ulong new = old + increment;
  99. if ((new < mem_malloc_start) || (new > mem_malloc_end)) {
  100. return (NULL);
  101. }
  102. mem_malloc_brk = new;
  103. return ((void *)old);
  104. }
  105. static int display_banner(void)
  106. {
  107. sprintf(version_string, VERSION_STRING_FORMAT, VERSION_STRING);
  108. printf("%s\n", version_string);
  109. return (0);
  110. }
  111. static void display_flash_config(ulong size)
  112. {
  113. puts("FLASH: ");
  114. print_size(size, "\n");
  115. return;
  116. }
  117. static int init_baudrate(void)
  118. {
  119. DECLARE_GLOBAL_DATA_PTR;
  120. char tmp[64];
  121. int i = getenv_r("baudrate", tmp, sizeof(tmp));
  122. gd->bd->bi_baudrate = gd->baudrate = (i > 0)
  123. ? (int)simple_strtoul(tmp, NULL, 10)
  124. : CONFIG_BAUDRATE;
  125. return (0);
  126. }
  127. #ifdef DEBUG
  128. static void display_global_data(void)
  129. {
  130. DECLARE_GLOBAL_DATA_PTR;
  131. bd_t *bd;
  132. bd = gd->bd;
  133. printf("--flags:%x\n", gd->flags);
  134. printf("--board_type:%x\n", gd->board_type);
  135. printf("--baudrate:%x\n", gd->baudrate);
  136. printf("--have_console:%x\n", gd->have_console);
  137. printf("--ram_size:%x\n", gd->ram_size);
  138. printf("--reloc_off:%x\n", gd->reloc_off);
  139. printf("--env_addr:%x\n", gd->env_addr);
  140. printf("--env_valid:%x\n", gd->env_valid);
  141. printf("--bd:%x %x\n", gd->bd, bd);
  142. printf("---bi_baudrate:%x\n", bd->bi_baudrate);
  143. printf("---bi_ip_addr:%x\n", bd->bi_ip_addr);
  144. printf("---bi_enetaddr:%x %x %x %x %x %x\n",
  145. bd->bi_enetaddr[0],
  146. bd->bi_enetaddr[1],
  147. bd->bi_enetaddr[2],
  148. bd->bi_enetaddr[3], bd->bi_enetaddr[4], bd->bi_enetaddr[5]);
  149. printf("---bi_arch_number:%x\n", bd->bi_arch_number);
  150. printf("---bi_boot_params:%x\n", bd->bi_boot_params);
  151. printf("---bi_memstart:%x\n", bd->bi_memstart);
  152. printf("---bi_memsize:%x\n", bd->bi_memsize);
  153. printf("---bi_flashstart:%x\n", bd->bi_flashstart);
  154. printf("---bi_flashsize:%x\n", bd->bi_flashsize);
  155. printf("---bi_flashoffset:%x\n", bd->bi_flashoffset);
  156. printf("--jt:%x *:%x\n", gd->jt, *(gd->jt));
  157. }
  158. #endif
  159. /* we cover everything with 4 meg pages, and need an extra for L1 */
  160. unsigned int icplb_table[page_descriptor_table_size][2];
  161. unsigned int dcplb_table[page_descriptor_table_size][2];
  162. void init_cplbtables(void)
  163. {
  164. int i, j;
  165. j = 0;
  166. icplb_table[j][0] = 0xFFA00000;
  167. icplb_table[j][1] = L1_IMEMORY;
  168. j++;
  169. for (i = 0; i <= CONFIG_MEM_SIZE / 4; i++) {
  170. icplb_table[j][0] = (i * 4 * 1024 * 1024);
  171. if (i * 4 * 1024 * 1024 <= CFG_MONITOR_BASE
  172. && (i + 1) * 4 * 1024 * 1024 >= CFG_MONITOR_BASE) {
  173. icplb_table[j][1] = SDRAM_IKERNEL;
  174. } else {
  175. icplb_table[j][1] = SDRAM_IGENERIC;
  176. }
  177. j++;
  178. }
  179. #if defined(CONFIG_BF561)
  180. /* Async Memory space */
  181. for (i = 0; i < 3; i++) {
  182. icplb_table[j++][0] = 0x20000000 + i * 4 * 1024 * 1024;
  183. icplb_table[j++][1] = SDRAM_IGENERIC;
  184. }
  185. #else
  186. icplb_table[j][0] = 0x20000000;
  187. icplb_table[j][1] = SDRAM_IGENERIC;
  188. #endif
  189. j = 0;
  190. dcplb_table[j][0] = 0xFF800000;
  191. dcplb_table[j][1] = L1_DMEMORY;
  192. j++;
  193. for (i = 0; i < CONFIG_MEM_SIZE / 4; i++) {
  194. dcplb_table[j][0] = (i * 4 * 1024 * 1024);
  195. if (i * 4 * 1024 * 1024 <= CFG_MONITOR_BASE
  196. && (i + 1) * 4 * 1024 * 1024 >= CFG_MONITOR_BASE) {
  197. dcplb_table[j][1] = SDRAM_DKERNEL;
  198. } else {
  199. dcplb_table[j][1] = SDRAM_DGENERIC;
  200. }
  201. j++;
  202. }
  203. #if defined(CONFIG_BF561)
  204. /* MAC space */
  205. dcplb_table[j++][0] = CONFIG_ASYNC_EBIU_BASE;
  206. dcplb_table[j++][1] = SDRAM_EBIU;
  207. /* Flash space */
  208. for (i = 0; i < 2; i++) {
  209. dcplb_table[j++][0] = 0x20000000 + i * 4 * 1024 * 1024;
  210. dcplb_table[j++][1] = SDRAM_EBIU;
  211. }
  212. #else
  213. dcplb_table[j][0] = 0x20000000;
  214. dcplb_table[j][1] = SDRAM_EBIU;
  215. #endif
  216. }
  217. /*
  218. * All attempts to come up with a "common" initialization sequence
  219. * that works for all boards and architectures failed: some of the
  220. * requirements are just _too_ different. To get rid of the resulting
  221. * mess of board dependend #ifdef'ed code we now make the whole
  222. * initialization sequence configurable to the user.
  223. *
  224. * The requirements for any new initalization function is simple: it
  225. * receives a pointer to the "global data" structure as it's only
  226. * argument, and returns an integer return code, where 0 means
  227. * "continue" and != 0 means "fatal error, hang the system".
  228. */
  229. void board_init_f(ulong bootflag)
  230. {
  231. DECLARE_GLOBAL_DATA_PTR;
  232. ulong addr;
  233. bd_t *bd;
  234. int i;
  235. init_cplbtables();
  236. gd = (gd_t *) (CFG_GBL_DATA_ADDR);
  237. memset((void *)gd, 0, sizeof(gd_t));
  238. /* Board data initialization */
  239. addr = (CFG_GBL_DATA_ADDR + sizeof(gd_t));
  240. /* Align to 4 byte boundary */
  241. addr &= ~(4 - 1);
  242. bd = (bd_t *) addr;
  243. gd->bd = bd;
  244. memset((void *)bd, 0, sizeof(bd_t));
  245. /* Initialize */
  246. init_IRQ();
  247. env_init(); /* initialize environment */
  248. init_baudrate(); /* initialze baudrate settings */
  249. serial_init(); /* serial communications setup */
  250. console_init_f();
  251. #ifdef CONFIG_ICACHE_ON
  252. icache_enable();
  253. #endif
  254. #ifdef CONFIG_DCACHE_ON
  255. dcache_enable();
  256. #endif
  257. display_banner(); /* say that we are here */
  258. for (i = 0; i < page_descriptor_table_size; i++) {
  259. pr_debug
  260. ("data (%02i)= 0x%08x : 0x%08x intr = 0x%08x : 0x%08x\n",
  261. i, dcplb_table[i][0], dcplb_table[i][1], icplb_table[i][0],
  262. icplb_table[i][1]);
  263. }
  264. checkboard();
  265. #if defined(CONFIG_RTC_BF533) && (CONFIG_COMMANDS & CFG_CMD_DATE)
  266. rtc_init();
  267. #endif
  268. timer_init();
  269. printf("Clock: VCO: %lu MHz, Core: %lu MHz, System: %lu MHz\n",
  270. get_vco() / 1000000, get_cclk() / 1000000, get_sclk() / 1000000);
  271. printf("SDRAM: ");
  272. print_size(initdram(0), "\n");
  273. #if defined(CONFIG_BF537)&&defined(CONFIG_POST)
  274. post_init_f();
  275. post_bootmode_init();
  276. post_run(NULL, POST_ROM | post_bootmode_get(0));
  277. #endif
  278. board_init_r((gd_t *) gd, 0x20000010);
  279. }
  280. #if defined(CONFIG_SOFT_I2C) || defined(CONFIG_HARD_I2C)
  281. static int init_func_i2c(void)
  282. {
  283. puts("I2C: ");
  284. i2c_init(CFG_I2C_SPEED, CFG_I2C_SLAVE);
  285. puts("ready\n");
  286. return (0);
  287. }
  288. #endif
  289. void board_init_r(gd_t * id, ulong dest_addr)
  290. {
  291. DECLARE_GLOBAL_DATA_PTR;
  292. ulong size;
  293. extern void malloc_bin_reloc(void);
  294. char *s, *e;
  295. bd_t *bd;
  296. int i;
  297. gd = id;
  298. gd->flags |= GD_FLG_RELOC; /* tell others: relocation done */
  299. bd = gd->bd;
  300. #if defined(CONFIG_BF537) && defined(CONFIG_POST)
  301. post_output_backlog();
  302. post_reloc();
  303. #endif
  304. #if (CONFIG_STAMP || CONFIG_BF537 || CONFIG_EZKIT561) && !defined(CFG_NO_FLASH)
  305. /* There are some other pointer constants we must deal with */
  306. /* configure available FLASH banks */
  307. size = flash_init();
  308. display_flash_config(size);
  309. flash_protect(FLAG_PROTECT_SET, CFG_FLASH_BASE,
  310. CFG_FLASH_BASE + 0x1ffff, &flash_info[0]);
  311. bd->bi_flashstart = CFG_FLASH_BASE;
  312. bd->bi_flashsize = size;
  313. bd->bi_flashoffset = 0;
  314. #else
  315. bd->bi_flashstart = 0;
  316. bd->bi_flashsize = 0;
  317. bd->bi_flashoffset = 0;
  318. #endif
  319. /* initialize malloc() area */
  320. mem_malloc_init();
  321. malloc_bin_reloc();
  322. #ifdef CONFIG_SPI
  323. # if ! defined(CFG_ENV_IS_IN_EEPROM)
  324. spi_init_f();
  325. # endif
  326. spi_init_r();
  327. #endif
  328. /* relocate environment function pointers etc. */
  329. env_relocate();
  330. /* board MAC address */
  331. s = getenv("ethaddr");
  332. for (i = 0; i < 6; ++i) {
  333. bd->bi_enetaddr[i] = s ? simple_strtoul(s, &e, 16) : 0;
  334. if (s)
  335. s = (*e) ? e + 1 : e;
  336. }
  337. /* IP Address */
  338. bd->bi_ip_addr = getenv_IPaddr("ipaddr");
  339. /* Initialize devices */
  340. devices_init();
  341. jumptable_init();
  342. /* Initialize the console (after the relocation and devices init) */
  343. console_init_r();
  344. /* Initialize from environment */
  345. if ((s = getenv("loadaddr")) != NULL) {
  346. load_addr = simple_strtoul(s, NULL, 16);
  347. }
  348. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  349. if ((s = getenv("bootfile")) != NULL) {
  350. copy_filename(BootFile, s, sizeof(BootFile));
  351. }
  352. #endif
  353. #if (CONFIG_COMMANDS & CFG_CMD_NAND)
  354. puts("NAND: ");
  355. nand_init(); /* go init the NAND */
  356. #endif
  357. #if defined(CONFIG_MISC_INIT_R)
  358. /* miscellaneous platform dependent initialisations */
  359. misc_init_r();
  360. #endif
  361. #if ((BFIN_CPU == ADSP_BF537) || (BFIN_CPU == ADSP_BF536))
  362. printf("Net: ");
  363. eth_initialize(bd);
  364. #endif
  365. #ifdef CONFIG_DRIVER_SMC91111
  366. #ifdef SHARED_RESOURCES
  367. /* Switch to Ethernet */
  368. swap_to(ETHERNET);
  369. #endif
  370. if ((SMC_inw(BANK_SELECT) & UPPER_BYTE_MASK) != SMC_IDENT) {
  371. printf("ERROR: Can't find SMC91111 at address %x\n",
  372. SMC_BASE_ADDRESS);
  373. } else {
  374. printf("Net: SMC91111 at 0x%08X\n", SMC_BASE_ADDRESS);
  375. }
  376. #ifdef SHARED_RESOURCES
  377. swap_to(FLASH);
  378. #endif
  379. #endif
  380. #if defined(CONFIG_SOFT_I2C) || defined(CONFIG_HARD_I2C)
  381. init_func_i2c();
  382. #endif
  383. #ifdef DEBUG
  384. display_global_data();
  385. #endif
  386. #if defined(CONFIG_BF537) && defined(CONFIG_POST)
  387. if (post_flag)
  388. post_run(NULL, POST_RAM | post_bootmode_get(0));
  389. #endif
  390. /* main_loop() can return to retry autoboot, if so just run it again. */
  391. for (;;) {
  392. main_loop();
  393. }
  394. }
  395. void hang(void)
  396. {
  397. puts("### ERROR ### Please RESET the board ###\n");
  398. for (;;) ;
  399. }