a3m071.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * (C) Copyright 2003-2004
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * (C) Copyright 2004
  6. * Mark Jonas, Freescale Semiconductor, mark.jonas@motorola.com.
  7. *
  8. * (C) Copyright 2006
  9. * MicroSys GmbH
  10. *
  11. * Copyright 2012-2013 Stefan Roese <sr@denx.de>
  12. *
  13. * See file CREDITS for list of people who contributed to this
  14. * project.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. */
  21. #include <common.h>
  22. #include <command.h>
  23. #include <mpc5xxx.h>
  24. #include <pci.h>
  25. #include <miiphy.h>
  26. #include <linux/compiler.h>
  27. #include <asm/processor.h>
  28. #include <asm/io.h>
  29. #ifdef CONFIG_A4M2K
  30. #include "is46r16320d.h"
  31. #else
  32. #include "mt46v16m16-75.h"
  33. #endif
  34. DECLARE_GLOBAL_DATA_PTR;
  35. #if !defined(CONFIG_SYS_RAMBOOT) && \
  36. (defined(CONFIG_SPL) && defined(CONFIG_SPL_BUILD))
  37. static void sdram_start(int hi_addr)
  38. {
  39. long hi_addr_bit = hi_addr ? 0x01000000 : 0;
  40. long control = SDRAM_CONTROL | hi_addr_bit;
  41. /* unlock mode register */
  42. out_be32((void *)MPC5XXX_SDRAM_CTRL, control | 0x80000000);
  43. /* precharge all banks */
  44. out_be32((void *)MPC5XXX_SDRAM_CTRL, control | 0x80000002);
  45. #ifdef SDRAM_DDR
  46. /* set mode register: extended mode */
  47. out_be32((void *)MPC5XXX_SDRAM_MODE, SDRAM_EMODE);
  48. /* set mode register: reset DLL */
  49. out_be32((void *)MPC5XXX_SDRAM_MODE, SDRAM_MODE | 0x04000000);
  50. #endif
  51. /* precharge all banks */
  52. out_be32((void *)MPC5XXX_SDRAM_CTRL, control | 0x80000002);
  53. /* auto refresh */
  54. out_be32((void *)MPC5XXX_SDRAM_CTRL, control | 0x80000004);
  55. /* set mode register */
  56. out_be32((void *)MPC5XXX_SDRAM_MODE, SDRAM_MODE);
  57. /* normal operation */
  58. out_be32((void *)MPC5XXX_SDRAM_CTRL, control);
  59. /*
  60. * Wait a short while for the DLL to lock before accessing
  61. * the SDRAM
  62. */
  63. udelay(100);
  64. }
  65. #endif
  66. /*
  67. * ATTENTION: Although partially referenced initdram does NOT make real use
  68. * use of CONFIG_SYS_SDRAM_BASE. The code does not work if
  69. * CONFIG_SYS_SDRAM_BASE is something else than 0x00000000.
  70. */
  71. phys_size_t initdram(int board_type)
  72. {
  73. ulong dramsize = 0;
  74. ulong dramsize2 = 0;
  75. uint svr, pvr;
  76. #if !defined(CONFIG_SYS_RAMBOOT) && \
  77. (defined(CONFIG_SPL) && defined(CONFIG_SPL_BUILD))
  78. ulong test1, test2;
  79. /* setup SDRAM chip selects */
  80. out_be32((void *)MPC5XXX_SDRAM_CS0CFG, 0x0000001e); /* 2GB at 0x0 */
  81. out_be32((void *)MPC5XXX_SDRAM_CS1CFG, 0x80000000); /* disabled */
  82. /* setup config registers */
  83. out_be32((void *)MPC5XXX_SDRAM_CONFIG1, SDRAM_CONFIG1);
  84. out_be32((void *)MPC5XXX_SDRAM_CONFIG2, SDRAM_CONFIG2);
  85. #ifdef SDRAM_DDR
  86. /* set tap delay */
  87. out_be32((void *)MPC5XXX_CDM_PORCFG, SDRAM_TAPDELAY);
  88. #endif
  89. /* find RAM size using SDRAM CS0 only */
  90. sdram_start(0);
  91. test1 = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE, 0x80000000);
  92. sdram_start(1);
  93. test2 = get_ram_size((long *)CONFIG_SYS_SDRAM_BASE, 0x80000000);
  94. if (test1 > test2) {
  95. sdram_start(0);
  96. dramsize = test1;
  97. } else {
  98. dramsize = test2;
  99. }
  100. /* memory smaller than 1MB is impossible */
  101. if (dramsize < (1 << 20))
  102. dramsize = 0;
  103. /* set SDRAM CS0 size according to the amount of RAM found */
  104. if (dramsize > 0) {
  105. out_be32((void *)MPC5XXX_SDRAM_CS0CFG,
  106. 0x13 + __builtin_ffs(dramsize >> 20) - 1);
  107. } else {
  108. out_be32((void *)MPC5XXX_SDRAM_CS0CFG, 0); /* disabled */
  109. }
  110. #else /* CONFIG_SYS_RAMBOOT */
  111. /* retrieve size of memory connected to SDRAM CS0 */
  112. dramsize = in_be32((void *)MPC5XXX_SDRAM_CS0CFG) & 0xFF;
  113. if (dramsize >= 0x13)
  114. dramsize = (1 << (dramsize - 0x13)) << 20;
  115. else
  116. dramsize = 0;
  117. /* retrieve size of memory connected to SDRAM CS1 */
  118. dramsize2 = in_be32((void *)MPC5XXX_SDRAM_CS1CFG) & 0xFF;
  119. if (dramsize2 >= 0x13)
  120. dramsize2 = (1 << (dramsize2 - 0x13)) << 20;
  121. else
  122. dramsize2 = 0;
  123. #endif /* CONFIG_SYS_RAMBOOT */
  124. /*
  125. * On MPC5200B we need to set the special configuration delay in the
  126. * DDR controller. Please refer to Freescale's AN3221 "MPC5200B SDRAM
  127. * Initialization and Configuration", 3.3.1 SDelay--MBAR + 0x0190:
  128. *
  129. * "The SDelay should be written to a value of 0x00000004. It is
  130. * required to account for changes caused by normal wafer processing
  131. * parameters."
  132. */
  133. svr = get_svr();
  134. pvr = get_pvr();
  135. if ((SVR_MJREV(svr) >= 2) && (PVR_MAJ(pvr) == 1) && (PVR_MIN(pvr) == 4))
  136. out_be32((void *)MPC5XXX_SDRAM_SDELAY, 0x04);
  137. return dramsize + dramsize2;
  138. }
  139. static void get_revisions(int *failsavelevel, int *digiboardversion,
  140. int *fpgaversion)
  141. {
  142. struct mpc5xxx_gpt_0_7 *gpt = (struct mpc5xxx_gpt_0_7 *)MPC5XXX_GPT;
  143. u8 val;
  144. /* read digitalboard-version from TMR[2..4] */
  145. val = 0;
  146. val |= (gpt->gpt2.sr & (1 << (31 - 23))) ? (1) : 0;
  147. val |= (gpt->gpt3.sr & (1 << (31 - 23))) ? (1 << 1) : 0;
  148. val |= (gpt->gpt4.sr & (1 << (31 - 23))) ? (1 << 2) : 0;
  149. *digiboardversion = val;
  150. /*
  151. * A4M2K only supports digiboardversion. No failsavelevel and
  152. * fpgaversion here.
  153. */
  154. #if !defined(CONFIG_A4M2K)
  155. /*
  156. * Figure out failsavelevel
  157. * see ticket dsvk#59
  158. */
  159. *failsavelevel = 0; /* 0=failsave, 1=board ok, 2=fpga ok */
  160. if (*digiboardversion == 0) {
  161. *failsavelevel = 1; /* digiboard-version ok */
  162. /* read fpga-version from TMR[5..7] */
  163. val = 0;
  164. val |= (gpt->gpt5.sr & (1 << (31 - 23))) ? (1) : 0;
  165. val |= (gpt->gpt6.sr & (1 << (31 - 23))) ? (1 << 1) : 0;
  166. val |= (gpt->gpt7.sr & (1 << (31 - 23))) ? (1 << 2) : 0;
  167. *fpgaversion = val;
  168. if (*fpgaversion == 1)
  169. *failsavelevel = 2; /* fpga-version ok */
  170. }
  171. #endif
  172. }
  173. /*
  174. * This function is called from the SPL U-Boot version for
  175. * early init stuff, that needs to be done for OS (e.g. Linux)
  176. * booting. Doing it later in the real U-Boot would not work
  177. * in case that the SPL U-Boot boots Linux directly.
  178. */
  179. void spl_board_init(void)
  180. {
  181. struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
  182. struct mpc5xxx_mmap_ctl *mm =
  183. (struct mpc5xxx_mmap_ctl *)CONFIG_SYS_MBAR;
  184. #if defined(CONFIG_A4M2K)
  185. /* enable CS3 and CS5 (FPGA) */
  186. setbits_be32(&mm->ipbi_ws_ctrl, (1 << 19) | (1 << 21));
  187. #else
  188. int digiboardversion;
  189. int failsavelevel;
  190. int fpgaversion;
  191. u32 val;
  192. get_revisions(&failsavelevel, &digiboardversion, &fpgaversion);
  193. val = in_be32(&mm->ipbi_ws_ctrl);
  194. /* first clear bits 19..21 (CS3...5) */
  195. val &= ~((1 << 19) | (1 << 20) | (1 << 21));
  196. if (failsavelevel == 2) {
  197. /* FPGA ok */
  198. val |= (1 << 19) | (1 << 21);
  199. }
  200. if (failsavelevel >= 1) {
  201. /* at least digiboard-version ok */
  202. val |= (1 << 20);
  203. }
  204. /* And write new value back to register */
  205. out_be32(&mm->ipbi_ws_ctrl, val);
  206. /* Setup pin multiplexing */
  207. if (failsavelevel == 2) {
  208. /* fpga-version ok */
  209. #if defined(CONFIG_SYS_GPS_PORT_CONFIG_2)
  210. out_be32(&gpio->port_config, CONFIG_SYS_GPS_PORT_CONFIG_2);
  211. #endif
  212. } else if (failsavelevel == 1) {
  213. /* digiboard-version ok - fpga not */
  214. #if defined(CONFIG_SYS_GPS_PORT_CONFIG_1)
  215. out_be32(&gpio->port_config, CONFIG_SYS_GPS_PORT_CONFIG_1);
  216. #endif
  217. } else {
  218. /* full failsave-mode */
  219. #if defined(CONFIG_SYS_GPS_PORT_CONFIG)
  220. out_be32(&gpio->port_config, CONFIG_SYS_GPS_PORT_CONFIG);
  221. #endif
  222. }
  223. #endif
  224. /*
  225. * Setup gpio_wkup_7 as watchdog AS INPUT to disable it - see
  226. * ticket #60
  227. *
  228. * MPC5XXX_WU_GPIO_DIR direction is already 0 (INPUT)
  229. * set bit 0(msb) to 1
  230. */
  231. setbits_be32((void *)MPC5XXX_WU_GPIO_ENABLE, CONFIG_WDOG_GPIO_PIN);
  232. #if defined(CONFIG_A4M2K)
  233. /* Setup USB[x] as MPCDiag[0..3] GPIO outputs */
  234. /* set USB0,6,7,8 (MPCDiag[0..3]) direction to output */
  235. gpio->simple_ddr |= 1 << (31 - 15);
  236. gpio->simple_ddr |= 1 << (31 - 14);
  237. gpio->simple_ddr |= 1 << (31 - 13);
  238. gpio->simple_ddr |= 1 << (31 - 12);
  239. /* enable USB0,6,7,8 (MPCDiag[0..3]) as GPIO */
  240. gpio->simple_gpioe |= 1 << (31 - 15);
  241. gpio->simple_gpioe |= 1 << (31 - 14);
  242. gpio->simple_gpioe |= 1 << (31 - 13);
  243. gpio->simple_gpioe |= 1 << (31 - 12);
  244. /* Setup PSC2[0..2] as STSLED[0..2] GPIO outputs */
  245. /* set PSC2[0..2] (STSLED[0..2]) direction to output */
  246. gpio->simple_ddr |= 1 << (31 - 27);
  247. gpio->simple_ddr |= 1 << (31 - 26);
  248. gpio->simple_ddr |= 1 << (31 - 25);
  249. /* enable PSC2[0..2] (STSLED[0..2]) as GPIO */
  250. gpio->simple_gpioe |= 1 << (31 - 27);
  251. gpio->simple_gpioe |= 1 << (31 - 26);
  252. gpio->simple_gpioe |= 1 << (31 - 25);
  253. /* Setup PSC6[2] as MRST2 self reset GPIO output */
  254. /* set PSC6[2]/IRDA_TX (MRST2) direction to output */
  255. gpio->simple_ddr |= 1 << (31 - 3);
  256. /* set PSC6[2]/IRDA_TX (MRST2) output as open drain */
  257. gpio->simple_ode |= 1 << (31 - 3);
  258. /* set PSC6[2]/IRDA_TX (MRST2) output as default high */
  259. gpio->simple_dvo |= 1 << (31 - 3);
  260. /* enable PSC6[2]/IRDA_TX (MRST2) as GPIO */
  261. gpio->simple_gpioe |= 1 << (31 - 3);
  262. /* Setup PSC6[3] as HARNSSCD harness code GPIO input */
  263. /* set PSC6[3]/IR_USB_CLK (HARNSSCD) direction to input */
  264. gpio->simple_ddr |= 0 << (31 - 2);
  265. /* enable PSC6[3]/IR_USB_CLK (HARNSSCD) as GPIO */
  266. gpio->simple_gpioe |= 1 << (31 - 2);
  267. #else
  268. /* setup GPIOs for status-leds if needed - see ticket #57 */
  269. if (failsavelevel > 0) {
  270. /* digiboard-version is OK */
  271. /* LED is LOW ACTIVE - so deactivate by set output to 1 */
  272. gpio->simple_dvo |= 1 << (31 - 12);
  273. gpio->simple_dvo |= 1 << (31 - 13);
  274. /* set GPIO direction to output */
  275. gpio->simple_ddr |= 1 << (31 - 12);
  276. gpio->simple_ddr |= 1 << (31 - 13);
  277. /* open drain config is set to "normal output" at reset */
  278. /* gpio->simple_ode &=~ ( 1 << (31-12) ); */
  279. /* gpio->simple_ode &=~ ( 1 << (31-13) ); */
  280. /* enable as GPIO */
  281. gpio->simple_gpioe |= 1 << (31 - 12);
  282. gpio->simple_gpioe |= 1 << (31 - 13);
  283. }
  284. /* setup fpga irq - see ticket #65 */
  285. if (failsavelevel > 1) {
  286. /*
  287. * The main irq initialisation is done in interrupts.c
  288. * mpc5xxx_init_irq
  289. */
  290. struct mpc5xxx_intr *intr =
  291. (struct mpc5xxx_intr *)(MPC5XXX_ICTL);
  292. setbits_be32(&intr->ctrl, 0x08C01801);
  293. /*
  294. * The MBAR+0x0524 Bit 21:23 CSe are ignored here due to the
  295. * already cleared (intr_ctrl) MBAR+0x0510 ECLR[0] bit above
  296. */
  297. }
  298. #endif
  299. }
  300. int checkboard(void)
  301. {
  302. int digiboardversion;
  303. int failsavelevel;
  304. int fpgaversion;
  305. get_revisions(&failsavelevel, &digiboardversion, &fpgaversion);
  306. #ifdef CONFIG_A4M2K
  307. puts("Board: A4M2K\n");
  308. printf(" digiboard IO version %u\n", digiboardversion);
  309. #else
  310. puts("Board: A3M071\n");
  311. printf("Rev: failsave level %u\n", failsavelevel);
  312. printf(" digiboard IO version %u\n", digiboardversion);
  313. if (failsavelevel > 0) /* only if fpga-version red */
  314. printf(" fpga IO version %u\n", fpgaversion);
  315. #endif
  316. return 0;
  317. }
  318. /* miscellaneous platform dependent initialisations */
  319. int misc_init_r(void)
  320. {
  321. /* adjust flash start and offset to detected values */
  322. gd->bd->bi_flashstart = flash_info[0].start[0];
  323. gd->bd->bi_flashoffset = 0;
  324. /* adjust mapping */
  325. out_be32((void *)MPC5XXX_BOOTCS_START,
  326. START_REG(gd->bd->bi_flashstart));
  327. out_be32((void *)MPC5XXX_CS0_START, START_REG(gd->bd->bi_flashstart));
  328. out_be32((void *)MPC5XXX_BOOTCS_STOP,
  329. STOP_REG(gd->bd->bi_flashstart, gd->bd->bi_flashsize));
  330. out_be32((void *)MPC5XXX_CS0_STOP,
  331. STOP_REG(gd->bd->bi_flashstart, gd->bd->bi_flashsize));
  332. return 0;
  333. }
  334. #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
  335. void ft_board_setup(void *blob, bd_t * bd)
  336. {
  337. ft_cpu_setup(blob, bd);
  338. }
  339. #endif /* defined(CONFIG_OF_FLAT_TREE) && defined(CONFIG_OF_BOARD_SETUP) */
  340. #ifdef CONFIG_SPL_OS_BOOT
  341. /*
  342. * A3M071 specific implementation of spl_start_uboot()
  343. *
  344. * RETURN
  345. * 0 if booting into OS is selected (default)
  346. * 1 if booting into U-Boot is selected
  347. */
  348. int spl_start_uboot(void)
  349. {
  350. char s[8];
  351. env_init();
  352. getenv_f("boot_os", s, sizeof(s));
  353. if ((s != NULL) && (strcmp(s, "yes") == 0))
  354. return 0;
  355. return 1;
  356. }
  357. #endif
  358. #if defined(CONFIG_HW_WATCHDOG)
  359. static int watchdog_toggle;
  360. void hw_watchdog_reset(void)
  361. {
  362. int val;
  363. /*
  364. * Check if watchdog is enabled via user command
  365. */
  366. if ((gd->flags & GD_FLG_RELOC) && watchdog_toggle) {
  367. /* Set direction to output */
  368. setbits_be32((void *)MPC5XXX_WU_GPIO_DIR, CONFIG_WDOG_GPIO_PIN);
  369. /*
  370. * Toggle watchdog output
  371. */
  372. val = (in_be32((void *)MPC5XXX_WU_GPIO_DATA_O) &
  373. CONFIG_WDOG_GPIO_PIN);
  374. if (val) {
  375. clrbits_be32((void *)MPC5XXX_WU_GPIO_DATA_O,
  376. CONFIG_WDOG_GPIO_PIN);
  377. } else {
  378. setbits_be32((void *)MPC5XXX_WU_GPIO_DATA_O,
  379. CONFIG_WDOG_GPIO_PIN);
  380. }
  381. }
  382. }
  383. int do_wdog_toggle(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  384. {
  385. if (argc != 2)
  386. goto usage;
  387. if (strncmp(argv[1], "on", 2) == 0)
  388. watchdog_toggle = 1;
  389. else if (strncmp(argv[1], "off", 3) == 0)
  390. watchdog_toggle = 0;
  391. else
  392. goto usage;
  393. return 0;
  394. usage:
  395. printf("Usage: wdogtoggle %s\n", cmdtp->usage);
  396. return 1;
  397. }
  398. U_BOOT_CMD(
  399. wdogtoggle, CONFIG_SYS_MAXARGS, 2, do_wdog_toggle,
  400. "toggle GPIO pin to service watchdog",
  401. "[on/off] - Switch watchdog toggling via GPIO pin on/off"
  402. );
  403. #endif