a3m071.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 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. #endif
  207. /*
  208. * No need to change the pin multiplexing (MPC5XXX_GPS_PORT_CONFIG)
  209. * as all 3 config versions (failsave level) have the same setup.
  210. */
  211. /*
  212. * Setup gpio_wkup_7 as watchdog AS INPUT to disable it - see
  213. * ticket #60
  214. *
  215. * MPC5XXX_WU_GPIO_DIR direction is already 0 (INPUT)
  216. * set bit 0(msb) to 1
  217. */
  218. setbits_be32((void *)MPC5XXX_WU_GPIO_ENABLE, CONFIG_WDOG_GPIO_PIN);
  219. #if defined(CONFIG_A4M2K)
  220. /* Setup USB[x] as MPCDiag[0..3] GPIO outputs */
  221. /* set USB0,6,7,8 (MPCDiag[0..3]) direction to output */
  222. gpio->simple_ddr |= 1 << (31 - 15);
  223. gpio->simple_ddr |= 1 << (31 - 14);
  224. gpio->simple_ddr |= 1 << (31 - 13);
  225. gpio->simple_ddr |= 1 << (31 - 12);
  226. /* enable USB0,6,7,8 (MPCDiag[0..3]) as GPIO */
  227. gpio->simple_gpioe |= 1 << (31 - 15);
  228. gpio->simple_gpioe |= 1 << (31 - 14);
  229. gpio->simple_gpioe |= 1 << (31 - 13);
  230. gpio->simple_gpioe |= 1 << (31 - 12);
  231. /* Setup PSC2[0..2] as STSLED[0..2] GPIO outputs */
  232. /* set PSC2[0..2] (STSLED[0..2]) direction to output */
  233. gpio->simple_ddr |= 1 << (31 - 27);
  234. gpio->simple_ddr |= 1 << (31 - 26);
  235. gpio->simple_ddr |= 1 << (31 - 25);
  236. /* enable PSC2[0..2] (STSLED[0..2]) as GPIO */
  237. gpio->simple_gpioe |= 1 << (31 - 27);
  238. gpio->simple_gpioe |= 1 << (31 - 26);
  239. gpio->simple_gpioe |= 1 << (31 - 25);
  240. /* Setup PSC6[2] as MRST2 self reset GPIO output */
  241. /* set PSC6[2]/IRDA_TX (MRST2) direction to output */
  242. gpio->simple_ddr |= 1 << (31 - 3);
  243. /* set PSC6[2]/IRDA_TX (MRST2) output as open drain */
  244. gpio->simple_ode |= 1 << (31 - 3);
  245. /* set PSC6[2]/IRDA_TX (MRST2) output as default high */
  246. gpio->simple_dvo |= 1 << (31 - 3);
  247. /* enable PSC6[2]/IRDA_TX (MRST2) as GPIO */
  248. gpio->simple_gpioe |= 1 << (31 - 3);
  249. /* Setup PSC6[3] as HARNSSCD harness code GPIO input */
  250. /* set PSC6[3]/IR_USB_CLK (HARNSSCD) direction to input */
  251. gpio->simple_ddr |= 0 << (31 - 2);
  252. /* enable PSC6[3]/IR_USB_CLK (HARNSSCD) as GPIO */
  253. gpio->simple_gpioe |= 1 << (31 - 2);
  254. #else
  255. /* setup GPIOs for status-leds if needed - see ticket #57 */
  256. if (failsavelevel > 0) {
  257. /* digiboard-version is OK */
  258. /* LED is LOW ACTIVE - so deactivate by set output to 1 */
  259. gpio->simple_dvo |= 1 << (31 - 12);
  260. gpio->simple_dvo |= 1 << (31 - 13);
  261. /* set GPIO direction to output */
  262. gpio->simple_ddr |= 1 << (31 - 12);
  263. gpio->simple_ddr |= 1 << (31 - 13);
  264. /* open drain config is set to "normal output" at reset */
  265. /* gpio->simple_ode &=~ ( 1 << (31-12) ); */
  266. /* gpio->simple_ode &=~ ( 1 << (31-13) ); */
  267. /* enable as GPIO */
  268. gpio->simple_gpioe |= 1 << (31 - 12);
  269. gpio->simple_gpioe |= 1 << (31 - 13);
  270. }
  271. /* setup fpga irq - see ticket #65 */
  272. if (failsavelevel > 1) {
  273. /*
  274. * The main irq initialisation is done in interrupts.c
  275. * mpc5xxx_init_irq
  276. */
  277. struct mpc5xxx_intr *intr =
  278. (struct mpc5xxx_intr *)(MPC5XXX_ICTL);
  279. setbits_be32(&intr->ctrl, 0x08C01801);
  280. /*
  281. * The MBAR+0x0524 Bit 21:23 CSe are ignored here due to the
  282. * already cleared (intr_ctrl) MBAR+0x0510 ECLR[0] bit above
  283. */
  284. }
  285. #endif
  286. }
  287. int checkboard(void)
  288. {
  289. int digiboardversion;
  290. int failsavelevel;
  291. int fpgaversion;
  292. get_revisions(&failsavelevel, &digiboardversion, &fpgaversion);
  293. #ifdef CONFIG_A4M2K
  294. puts("Board: A4M2K\n");
  295. printf(" digiboard IO version %u\n", digiboardversion);
  296. #else
  297. puts("Board: A3M071\n");
  298. printf("Rev: failsave level %u\n", failsavelevel);
  299. printf(" digiboard IO version %u\n", digiboardversion);
  300. if (failsavelevel > 0) /* only if fpga-version red */
  301. printf(" fpga IO version %u\n", fpgaversion);
  302. #endif
  303. return 0;
  304. }
  305. /* miscellaneous platform dependent initialisations */
  306. int misc_init_r(void)
  307. {
  308. /* adjust flash start and offset to detected values */
  309. gd->bd->bi_flashstart = flash_info[0].start[0];
  310. gd->bd->bi_flashoffset = 0;
  311. /* adjust mapping */
  312. out_be32((void *)MPC5XXX_BOOTCS_START,
  313. START_REG(gd->bd->bi_flashstart));
  314. out_be32((void *)MPC5XXX_CS0_START, START_REG(gd->bd->bi_flashstart));
  315. out_be32((void *)MPC5XXX_BOOTCS_STOP,
  316. STOP_REG(gd->bd->bi_flashstart, gd->bd->bi_flashsize));
  317. out_be32((void *)MPC5XXX_CS0_STOP,
  318. STOP_REG(gd->bd->bi_flashstart, gd->bd->bi_flashsize));
  319. return 0;
  320. }
  321. #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_OF_BOARD_SETUP)
  322. void ft_board_setup(void *blob, bd_t * bd)
  323. {
  324. ft_cpu_setup(blob, bd);
  325. }
  326. #endif /* defined(CONFIG_OF_FLAT_TREE) && defined(CONFIG_OF_BOARD_SETUP) */
  327. #ifdef CONFIG_SPL_OS_BOOT
  328. /*
  329. * A3M071 specific implementation of spl_start_uboot()
  330. *
  331. * RETURN
  332. * 0 if booting into OS is selected (default)
  333. * 1 if booting into U-Boot is selected
  334. */
  335. int spl_start_uboot(void)
  336. {
  337. char s[8];
  338. env_init();
  339. getenv_f("boot_os", s, sizeof(s));
  340. if ((s != NULL) && (strcmp(s, "yes") == 0))
  341. return 0;
  342. return 1;
  343. }
  344. #endif
  345. #if defined(CONFIG_HW_WATCHDOG)
  346. static int watchdog_toggle;
  347. void hw_watchdog_reset(void)
  348. {
  349. int val;
  350. /*
  351. * Check if watchdog is enabled via user command
  352. */
  353. if ((gd->flags & GD_FLG_RELOC) && watchdog_toggle) {
  354. /* Set direction to output */
  355. setbits_be32((void *)MPC5XXX_WU_GPIO_DIR, CONFIG_WDOG_GPIO_PIN);
  356. /*
  357. * Toggle watchdog output
  358. */
  359. val = (in_be32((void *)MPC5XXX_WU_GPIO_DATA_O) &
  360. CONFIG_WDOG_GPIO_PIN);
  361. if (val) {
  362. clrbits_be32((void *)MPC5XXX_WU_GPIO_DATA_O,
  363. CONFIG_WDOG_GPIO_PIN);
  364. } else {
  365. setbits_be32((void *)MPC5XXX_WU_GPIO_DATA_O,
  366. CONFIG_WDOG_GPIO_PIN);
  367. }
  368. }
  369. }
  370. int do_wdog_toggle(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  371. {
  372. if (argc != 2)
  373. goto usage;
  374. if (strncmp(argv[1], "on", 2) == 0)
  375. watchdog_toggle = 1;
  376. else if (strncmp(argv[1], "off", 3) == 0)
  377. watchdog_toggle = 0;
  378. else
  379. goto usage;
  380. return 0;
  381. usage:
  382. printf("Usage: wdogtoggle %s\n", cmdtp->usage);
  383. return 1;
  384. }
  385. U_BOOT_CMD(
  386. wdogtoggle, CONFIG_SYS_MAXARGS, 2, do_wdog_toggle,
  387. "toggle GPIO pin to service watchdog",
  388. "[on/off] - Switch watchdog toggling via GPIO pin on/off"
  389. );
  390. #endif