misc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (C) 2012 Altera Corporation <www.altera.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <asm/io.h>
  8. #include <fdtdec.h>
  9. #include <libfdt.h>
  10. #include <altera.h>
  11. #include <miiphy.h>
  12. #include <netdev.h>
  13. #include <watchdog.h>
  14. #include <asm/arch/reset_manager.h>
  15. #include <asm/arch/system_manager.h>
  16. #include <asm/arch/dwmmc.h>
  17. #include <asm/arch/nic301.h>
  18. #include <asm/arch/scu.h>
  19. #include <asm/pl310.h>
  20. #include <dt-bindings/reset/altr,rst-mgr.h>
  21. DECLARE_GLOBAL_DATA_PTR;
  22. static struct pl310_regs *const pl310 =
  23. (struct pl310_regs *)CONFIG_SYS_PL310_BASE;
  24. static struct socfpga_system_manager *sysmgr_regs =
  25. (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
  26. static struct socfpga_reset_manager *reset_manager_base =
  27. (struct socfpga_reset_manager *)SOCFPGA_RSTMGR_ADDRESS;
  28. static struct nic301_registers *nic301_regs =
  29. (struct nic301_registers *)SOCFPGA_L3REGS_ADDRESS;
  30. static struct scu_registers *scu_regs =
  31. (struct scu_registers *)SOCFPGA_MPUSCU_ADDRESS;
  32. int dram_init(void)
  33. {
  34. gd->ram_size = get_ram_size((long *)PHYS_SDRAM_1, PHYS_SDRAM_1_SIZE);
  35. return 0;
  36. }
  37. void enable_caches(void)
  38. {
  39. #ifndef CONFIG_SYS_ICACHE_OFF
  40. icache_enable();
  41. #endif
  42. #ifndef CONFIG_SYS_DCACHE_OFF
  43. dcache_enable();
  44. #endif
  45. }
  46. /*
  47. * DesignWare Ethernet initialization
  48. */
  49. #ifdef CONFIG_ETH_DESIGNWARE
  50. static void dwmac_deassert_reset(const unsigned int of_reset_id)
  51. {
  52. u32 physhift, reset;
  53. if (of_reset_id == EMAC0_RESET) {
  54. physhift = SYSMGR_EMACGRP_CTRL_PHYSEL0_LSB;
  55. reset = SOCFPGA_RESET(EMAC0);
  56. } else if (of_reset_id == EMAC1_RESET) {
  57. physhift = SYSMGR_EMACGRP_CTRL_PHYSEL1_LSB;
  58. reset = SOCFPGA_RESET(EMAC1);
  59. } else {
  60. printf("GMAC: Invalid reset ID (%i)!\n", of_reset_id);
  61. return;
  62. }
  63. /* Clearing emac0 PHY interface select to 0 */
  64. clrbits_le32(&sysmgr_regs->emacgrp_ctrl,
  65. SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << physhift);
  66. /* configure to PHY interface select choosed */
  67. setbits_le32(&sysmgr_regs->emacgrp_ctrl,
  68. SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII << physhift);
  69. /* Release the EMAC controller from reset */
  70. socfpga_per_reset(reset, 0);
  71. }
  72. int cpu_eth_init(bd_t *bis)
  73. {
  74. const void *fdt = gd->fdt_blob;
  75. struct fdtdec_phandle_args args;
  76. int nodes[2]; /* Max. two GMACs */
  77. int ret, count;
  78. int i, node;
  79. /* Put both GMACs into RESET state. */
  80. socfpga_per_reset(SOCFPGA_RESET(EMAC0), 1);
  81. socfpga_per_reset(SOCFPGA_RESET(EMAC1), 1);
  82. count = fdtdec_find_aliases_for_id(fdt, "ethernet",
  83. COMPAT_ALTERA_SOCFPGA_DWMAC,
  84. nodes, ARRAY_SIZE(nodes));
  85. for (i = 0; i < count; i++) {
  86. node = nodes[i];
  87. if (node <= 0)
  88. continue;
  89. ret = fdtdec_parse_phandle_with_args(fdt, node, "resets",
  90. "#reset-cells", 1, 0,
  91. &args);
  92. if (ret || (args.args_count != 1)) {
  93. debug("GMAC%i: Failed to parse DT 'resets'!\n", i);
  94. continue;
  95. }
  96. dwmac_deassert_reset(args.args[0]);
  97. }
  98. return 0;
  99. }
  100. #endif
  101. #ifdef CONFIG_DWMMC
  102. /*
  103. * Initializes MMC controllers.
  104. * to override, implement board_mmc_init()
  105. */
  106. int cpu_mmc_init(bd_t *bis)
  107. {
  108. return socfpga_dwmmc_init(SOCFPGA_SDMMC_ADDRESS,
  109. CONFIG_HPS_SDMMC_BUSWIDTH, 0);
  110. }
  111. #endif
  112. struct {
  113. const char *mode;
  114. const char *name;
  115. } bsel_str[] = {
  116. { "rsvd", "Reserved", },
  117. { "fpga", "FPGA (HPS2FPGA Bridge)", },
  118. { "nand", "NAND Flash (1.8V)", },
  119. { "nand", "NAND Flash (3.0V)", },
  120. { "sd", "SD/MMC External Transceiver (1.8V)", },
  121. { "sd", "SD/MMC Internal Transceiver (3.0V)", },
  122. { "qspi", "QSPI Flash (1.8V)", },
  123. { "qspi", "QSPI Flash (3.0V)", },
  124. };
  125. /*
  126. * Print CPU information
  127. */
  128. #if defined(CONFIG_DISPLAY_CPUINFO)
  129. int print_cpuinfo(void)
  130. {
  131. const u32 bsel = readl(&sysmgr_regs->bootinfo) & 0x7;
  132. puts("CPU: Altera SoCFPGA Platform\n");
  133. printf("BOOT: %s\n", bsel_str[bsel].name);
  134. return 0;
  135. }
  136. #endif
  137. #ifdef CONFIG_ARCH_MISC_INIT
  138. int arch_misc_init(void)
  139. {
  140. const u32 bsel = readl(&sysmgr_regs->bootinfo) & 0x7;
  141. setenv("bootmode", bsel_str[bsel].mode);
  142. return 0;
  143. }
  144. #endif
  145. #if defined(CONFIG_SYS_CONSOLE_IS_IN_ENV) && \
  146. defined(CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE)
  147. int overwrite_console(void)
  148. {
  149. return 0;
  150. }
  151. #endif
  152. #ifdef CONFIG_FPGA
  153. /*
  154. * FPGA programming support for SoC FPGA Cyclone V
  155. */
  156. static Altera_desc altera_fpga[] = {
  157. {
  158. /* Family */
  159. Altera_SoCFPGA,
  160. /* Interface type */
  161. fast_passive_parallel,
  162. /* No limitation as additional data will be ignored */
  163. -1,
  164. /* No device function table */
  165. NULL,
  166. /* Base interface address specified in driver */
  167. NULL,
  168. /* No cookie implementation */
  169. 0
  170. },
  171. };
  172. /* add device descriptor to FPGA device table */
  173. static void socfpga_fpga_add(void)
  174. {
  175. int i;
  176. fpga_init();
  177. for (i = 0; i < ARRAY_SIZE(altera_fpga); i++)
  178. fpga_add(fpga_altera, &altera_fpga[i]);
  179. }
  180. #else
  181. static inline void socfpga_fpga_add(void) {}
  182. #endif
  183. int arch_cpu_init(void)
  184. {
  185. #ifdef CONFIG_HW_WATCHDOG
  186. /*
  187. * In case the watchdog is enabled, make sure to (re-)configure it
  188. * so that the defined timeout is valid. Otherwise the SPL (Perloader)
  189. * timeout value is still active which might too short for Linux
  190. * booting.
  191. */
  192. hw_watchdog_init();
  193. #else
  194. /*
  195. * If the HW watchdog is NOT enabled, make sure it is not running,
  196. * for example because it was enabled in the preloader. This might
  197. * trigger a watchdog-triggered reboot of Linux kernel later.
  198. * Toggle watchdog reset, so watchdog in not running state.
  199. */
  200. socfpga_per_reset(SOCFPGA_RESET(L4WD0), 1);
  201. socfpga_per_reset(SOCFPGA_RESET(L4WD0), 0);
  202. #endif
  203. return 0;
  204. }
  205. /*
  206. * Convert all NIC-301 AMBA slaves from secure to non-secure
  207. */
  208. static void socfpga_nic301_slave_ns(void)
  209. {
  210. writel(0x1, &nic301_regs->lwhps2fpgaregs);
  211. writel(0x1, &nic301_regs->hps2fpgaregs);
  212. writel(0x1, &nic301_regs->acp);
  213. writel(0x1, &nic301_regs->rom);
  214. writel(0x1, &nic301_regs->ocram);
  215. writel(0x1, &nic301_regs->sdrdata);
  216. }
  217. static uint32_t iswgrp_handoff[8];
  218. int arch_early_init_r(void)
  219. {
  220. int i;
  221. /*
  222. * Write magic value into magic register to unlock support for
  223. * issuing warm reset. The ancient kernel code expects this
  224. * value to be written into the register by the bootloader, so
  225. * to support that old code, we write it here instead of in the
  226. * reset_cpu() function just before reseting the CPU.
  227. */
  228. writel(0xae9efebc, &sysmgr_regs->romcodegrp_warmramgrp_enable);
  229. for (i = 0; i < 8; i++) /* Cache initial SW setting regs */
  230. iswgrp_handoff[i] = readl(&sysmgr_regs->iswgrp_handoff[i]);
  231. socfpga_bridges_reset(1);
  232. socfpga_nic301_slave_ns();
  233. /*
  234. * Private components security:
  235. * U-Boot : configure private timer, global timer and cpu component
  236. * access as non secure for kernel stage (as required by Linux)
  237. */
  238. setbits_le32(&scu_regs->sacr, 0xfff);
  239. /* Configure the L2 controller to make SDRAM start at 0 */
  240. #ifdef CONFIG_SOCFPGA_VIRTUAL_TARGET
  241. writel(0x2, &nic301_regs->remap);
  242. #else
  243. writel(0x1, &nic301_regs->remap); /* remap.mpuzero */
  244. writel(0x1, &pl310->pl310_addr_filter_start);
  245. #endif
  246. /* Add device descriptor to FPGA device table */
  247. socfpga_fpga_add();
  248. #ifdef CONFIG_DESIGNWARE_SPI
  249. /* Get Designware SPI controller out of reset */
  250. socfpga_per_reset(SOCFPGA_RESET(SPIM0), 0);
  251. socfpga_per_reset(SOCFPGA_RESET(SPIM1), 0);
  252. #endif
  253. return 0;
  254. }
  255. static void socfpga_sdram_apply_static_cfg(void)
  256. {
  257. const uint32_t staticcfg = SOCFPGA_SDR_ADDRESS + 0x505c;
  258. const uint32_t applymask = 0x8;
  259. uint32_t val = readl(staticcfg) | applymask;
  260. /*
  261. * SDRAM staticcfg register specific:
  262. * When applying the register setting, the CPU must not access
  263. * SDRAM. Luckily for us, we can abuse i-cache here to help us
  264. * circumvent the SDRAM access issue. The idea is to make sure
  265. * that the code is in one full i-cache line by branching past
  266. * it and back. Once it is in the i-cache, we execute the core
  267. * of the code and apply the register settings.
  268. *
  269. * The code below uses 7 instructions, while the Cortex-A9 has
  270. * 32-byte cachelines, thus the limit is 8 instructions total.
  271. */
  272. asm volatile(
  273. ".align 5 \n"
  274. " b 2f \n"
  275. "1: str %0, [%1] \n"
  276. " dsb \n"
  277. " isb \n"
  278. " b 3f \n"
  279. "2: b 1b \n"
  280. "3: nop \n"
  281. : : "r"(val), "r"(staticcfg) : "memory", "cc");
  282. }
  283. int do_bridge(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  284. {
  285. if (argc != 2)
  286. return CMD_RET_USAGE;
  287. argv++;
  288. switch (*argv[0]) {
  289. case 'e': /* Enable */
  290. writel(iswgrp_handoff[2], &sysmgr_regs->fpgaintfgrp_module);
  291. socfpga_sdram_apply_static_cfg();
  292. writel(iswgrp_handoff[3], SOCFPGA_SDR_ADDRESS + 0x5080);
  293. writel(iswgrp_handoff[0], &reset_manager_base->brg_mod_reset);
  294. writel(iswgrp_handoff[1], &nic301_regs->remap);
  295. break;
  296. case 'd': /* Disable */
  297. writel(0, &sysmgr_regs->fpgaintfgrp_module);
  298. writel(0, SOCFPGA_SDR_ADDRESS + 0x5080);
  299. socfpga_sdram_apply_static_cfg();
  300. writel(0, &reset_manager_base->brg_mod_reset);
  301. writel(1, &nic301_regs->remap);
  302. break;
  303. default:
  304. return CMD_RET_USAGE;
  305. }
  306. return 0;
  307. }
  308. U_BOOT_CMD(
  309. bridge, 2, 1, do_bridge,
  310. "SoCFPGA HPS FPGA bridge control",
  311. "enable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
  312. "bridge disable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
  313. ""
  314. );