misc_gen5.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Copyright (C) 2012-2017 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 <errno.h>
  9. #include <fdtdec.h>
  10. #include <libfdt.h>
  11. #include <altera.h>
  12. #include <miiphy.h>
  13. #include <netdev.h>
  14. #include <watchdog.h>
  15. #include <asm/arch/misc.h>
  16. #include <asm/arch/reset_manager.h>
  17. #include <asm/arch/scan_manager.h>
  18. #include <asm/arch/sdram.h>
  19. #include <asm/arch/system_manager.h>
  20. #include <asm/arch/nic301.h>
  21. #include <asm/arch/scu.h>
  22. #include <asm/pl310.h>
  23. #include <dt-bindings/reset/altr,rst-mgr.h>
  24. DECLARE_GLOBAL_DATA_PTR;
  25. static struct pl310_regs *const pl310 =
  26. (struct pl310_regs *)CONFIG_SYS_PL310_BASE;
  27. static struct socfpga_system_manager *sysmgr_regs =
  28. (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
  29. static struct nic301_registers *nic301_regs =
  30. (struct nic301_registers *)SOCFPGA_L3REGS_ADDRESS;
  31. static struct scu_registers *scu_regs =
  32. (struct scu_registers *)SOCFPGA_MPUSCU_ADDRESS;
  33. /*
  34. * DesignWare Ethernet initialization
  35. */
  36. #ifdef CONFIG_ETH_DESIGNWARE
  37. void dwmac_deassert_reset(const unsigned int of_reset_id,
  38. const u32 phymode)
  39. {
  40. u32 physhift, reset;
  41. if (of_reset_id == EMAC0_RESET) {
  42. physhift = SYSMGR_EMACGRP_CTRL_PHYSEL0_LSB;
  43. reset = SOCFPGA_RESET(EMAC0);
  44. } else if (of_reset_id == EMAC1_RESET) {
  45. physhift = SYSMGR_EMACGRP_CTRL_PHYSEL1_LSB;
  46. reset = SOCFPGA_RESET(EMAC1);
  47. } else {
  48. printf("GMAC: Invalid reset ID (%i)!\n", of_reset_id);
  49. return;
  50. }
  51. /* configure to PHY interface select choosed */
  52. clrsetbits_le32(&sysmgr_regs->emacgrp_ctrl,
  53. SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << physhift,
  54. phymode << physhift);
  55. /* Release the EMAC controller from reset */
  56. socfpga_per_reset(reset, 0);
  57. }
  58. static u32 dwmac_phymode_to_modereg(const char *phymode, u32 *modereg)
  59. {
  60. if (!phymode)
  61. return -EINVAL;
  62. if (!strcmp(phymode, "mii") || !strcmp(phymode, "gmii")) {
  63. *modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
  64. return 0;
  65. }
  66. if (!strcmp(phymode, "rgmii")) {
  67. *modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII;
  68. return 0;
  69. }
  70. if (!strcmp(phymode, "rmii")) {
  71. *modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII;
  72. return 0;
  73. }
  74. return -EINVAL;
  75. }
  76. static int socfpga_eth_reset(void)
  77. {
  78. const void *fdt = gd->fdt_blob;
  79. struct fdtdec_phandle_args args;
  80. const char *phy_mode;
  81. u32 phy_modereg;
  82. int nodes[2]; /* Max. two GMACs */
  83. int ret, count;
  84. int i, node;
  85. /* Put both GMACs into RESET state. */
  86. socfpga_per_reset(SOCFPGA_RESET(EMAC0), 1);
  87. socfpga_per_reset(SOCFPGA_RESET(EMAC1), 1);
  88. count = fdtdec_find_aliases_for_id(fdt, "ethernet",
  89. COMPAT_ALTERA_SOCFPGA_DWMAC,
  90. nodes, ARRAY_SIZE(nodes));
  91. for (i = 0; i < count; i++) {
  92. node = nodes[i];
  93. if (node <= 0)
  94. continue;
  95. ret = fdtdec_parse_phandle_with_args(fdt, node, "resets",
  96. "#reset-cells", 1, 0,
  97. &args);
  98. if (ret || (args.args_count != 1)) {
  99. debug("GMAC%i: Failed to parse DT 'resets'!\n", i);
  100. continue;
  101. }
  102. phy_mode = fdt_getprop(fdt, node, "phy-mode", NULL);
  103. ret = dwmac_phymode_to_modereg(phy_mode, &phy_modereg);
  104. if (ret) {
  105. debug("GMAC%i: Failed to parse DT 'phy-mode'!\n", i);
  106. continue;
  107. }
  108. dwmac_deassert_reset(args.args[0], phy_modereg);
  109. }
  110. return 0;
  111. }
  112. #else
  113. static int socfpga_eth_reset(void)
  114. {
  115. return 0;
  116. };
  117. #endif
  118. static const struct {
  119. const u16 pn;
  120. const char *name;
  121. const char *var;
  122. } socfpga_fpga_model[] = {
  123. /* Cyclone V E */
  124. { 0x2b15, "Cyclone V, E/A2", "cv_e_a2" },
  125. { 0x2b05, "Cyclone V, E/A4", "cv_e_a4" },
  126. { 0x2b22, "Cyclone V, E/A5", "cv_e_a5" },
  127. { 0x2b13, "Cyclone V, E/A7", "cv_e_a7" },
  128. { 0x2b14, "Cyclone V, E/A9", "cv_e_a9" },
  129. /* Cyclone V GX/GT */
  130. { 0x2b01, "Cyclone V, GX/C3", "cv_gx_c3" },
  131. { 0x2b12, "Cyclone V, GX/C4", "cv_gx_c4" },
  132. { 0x2b02, "Cyclone V, GX/C5 or GT/D5", "cv_gx_c5" },
  133. { 0x2b03, "Cyclone V, GX/C7 or GT/D7", "cv_gx_c7" },
  134. { 0x2b04, "Cyclone V, GX/C9 or GT/D9", "cv_gx_c9" },
  135. /* Cyclone V SE/SX/ST */
  136. { 0x2d11, "Cyclone V, SE/A2 or SX/C2", "cv_se_a2" },
  137. { 0x2d01, "Cyclone V, SE/A4 or SX/C4", "cv_se_a4" },
  138. { 0x2d12, "Cyclone V, SE/A5 or SX/C5 or ST/D5", "cv_se_a5" },
  139. { 0x2d02, "Cyclone V, SE/A6 or SX/C6 or ST/D6", "cv_se_a6" },
  140. /* Arria V */
  141. { 0x2d03, "Arria V, D5", "av_d5" },
  142. };
  143. static int socfpga_fpga_id(const bool print_id)
  144. {
  145. const u32 altera_mi = 0x6e;
  146. const u32 id = scan_mgr_get_fpga_id();
  147. const u32 lsb = id & 0x00000001;
  148. const u32 mi = (id >> 1) & 0x000007ff;
  149. const u32 pn = (id >> 12) & 0x0000ffff;
  150. const u32 version = (id >> 28) & 0x0000000f;
  151. int i;
  152. if ((mi != altera_mi) || (lsb != 1)) {
  153. printf("FPGA: Not Altera chip ID\n");
  154. return -EINVAL;
  155. }
  156. for (i = 0; i < ARRAY_SIZE(socfpga_fpga_model); i++)
  157. if (pn == socfpga_fpga_model[i].pn)
  158. break;
  159. if (i == ARRAY_SIZE(socfpga_fpga_model)) {
  160. printf("FPGA: Unknown Altera chip, ID 0x%08x\n", id);
  161. return -EINVAL;
  162. }
  163. if (print_id)
  164. printf("FPGA: Altera %s, version 0x%01x\n",
  165. socfpga_fpga_model[i].name, version);
  166. return i;
  167. }
  168. /*
  169. * Print CPU information
  170. */
  171. #if defined(CONFIG_DISPLAY_CPUINFO)
  172. int print_cpuinfo(void)
  173. {
  174. const u32 bsel =
  175. SYSMGR_GET_BOOTINFO_BSEL(readl(&sysmgr_regs->bootinfo));
  176. puts("CPU: Altera SoCFPGA Platform\n");
  177. socfpga_fpga_id(1);
  178. printf("BOOT: %s\n", bsel_str[bsel].name);
  179. return 0;
  180. }
  181. #endif
  182. #ifdef CONFIG_ARCH_MISC_INIT
  183. int arch_misc_init(void)
  184. {
  185. const u32 bsel = readl(&sysmgr_regs->bootinfo) & 0x7;
  186. const int fpga_id = socfpga_fpga_id(0);
  187. env_set("bootmode", bsel_str[bsel].mode);
  188. if (fpga_id >= 0)
  189. env_set("fpgatype", socfpga_fpga_model[fpga_id].var);
  190. return socfpga_eth_reset();
  191. }
  192. #endif
  193. /*
  194. * Convert all NIC-301 AMBA slaves from secure to non-secure
  195. */
  196. static void socfpga_nic301_slave_ns(void)
  197. {
  198. writel(0x1, &nic301_regs->lwhps2fpgaregs);
  199. writel(0x1, &nic301_regs->hps2fpgaregs);
  200. writel(0x1, &nic301_regs->acp);
  201. writel(0x1, &nic301_regs->rom);
  202. writel(0x1, &nic301_regs->ocram);
  203. writel(0x1, &nic301_regs->sdrdata);
  204. }
  205. static u32 iswgrp_handoff[8];
  206. int arch_early_init_r(void)
  207. {
  208. int i;
  209. /*
  210. * Write magic value into magic register to unlock support for
  211. * issuing warm reset. The ancient kernel code expects this
  212. * value to be written into the register by the bootloader, so
  213. * to support that old code, we write it here instead of in the
  214. * reset_cpu() function just before resetting the CPU.
  215. */
  216. writel(0xae9efebc, &sysmgr_regs->romcodegrp_warmramgrp_enable);
  217. for (i = 0; i < 8; i++) /* Cache initial SW setting regs */
  218. iswgrp_handoff[i] = readl(&sysmgr_regs->iswgrp_handoff[i]);
  219. socfpga_bridges_reset(1);
  220. socfpga_nic301_slave_ns();
  221. /*
  222. * Private components security:
  223. * U-Boot : configure private timer, global timer and cpu component
  224. * access as non secure for kernel stage (as required by Linux)
  225. */
  226. setbits_le32(&scu_regs->sacr, 0xfff);
  227. /* Configure the L2 controller to make SDRAM start at 0 */
  228. #ifdef CONFIG_SOCFPGA_VIRTUAL_TARGET
  229. writel(0x2, &nic301_regs->remap);
  230. #else
  231. writel(0x1, &nic301_regs->remap); /* remap.mpuzero */
  232. writel(0x1, &pl310->pl310_addr_filter_start);
  233. #endif
  234. /* Add device descriptor to FPGA device table */
  235. socfpga_fpga_add();
  236. #ifdef CONFIG_DESIGNWARE_SPI
  237. /* Get Designware SPI controller out of reset */
  238. socfpga_per_reset(SOCFPGA_RESET(SPIM0), 0);
  239. socfpga_per_reset(SOCFPGA_RESET(SPIM1), 0);
  240. #endif
  241. #ifdef CONFIG_NAND_DENALI
  242. socfpga_per_reset(SOCFPGA_RESET(NAND), 0);
  243. #endif
  244. return 0;
  245. }
  246. #ifndef CONFIG_SPL_BUILD
  247. static struct socfpga_reset_manager *reset_manager_base =
  248. (struct socfpga_reset_manager *)SOCFPGA_RSTMGR_ADDRESS;
  249. static struct socfpga_sdr_ctrl *sdr_ctrl =
  250. (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
  251. static void socfpga_sdram_apply_static_cfg(void)
  252. {
  253. const u32 applymask = 0x8;
  254. u32 val = readl(&sdr_ctrl->static_cfg) | applymask;
  255. /*
  256. * SDRAM staticcfg register specific:
  257. * When applying the register setting, the CPU must not access
  258. * SDRAM. Luckily for us, we can abuse i-cache here to help us
  259. * circumvent the SDRAM access issue. The idea is to make sure
  260. * that the code is in one full i-cache line by branching past
  261. * it and back. Once it is in the i-cache, we execute the core
  262. * of the code and apply the register settings.
  263. *
  264. * The code below uses 7 instructions, while the Cortex-A9 has
  265. * 32-byte cachelines, thus the limit is 8 instructions total.
  266. */
  267. asm volatile(
  268. ".align 5 \n"
  269. " b 2f \n"
  270. "1: str %0, [%1] \n"
  271. " dsb \n"
  272. " isb \n"
  273. " b 3f \n"
  274. "2: b 1b \n"
  275. "3: nop \n"
  276. : : "r"(val), "r"(&sdr_ctrl->static_cfg) : "memory", "cc");
  277. }
  278. static int do_bridge(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  279. {
  280. if (argc != 2)
  281. return CMD_RET_USAGE;
  282. argv++;
  283. switch (*argv[0]) {
  284. case 'e': /* Enable */
  285. writel(iswgrp_handoff[2], &sysmgr_regs->fpgaintfgrp_module);
  286. socfpga_sdram_apply_static_cfg();
  287. writel(iswgrp_handoff[3], &sdr_ctrl->fpgaport_rst);
  288. writel(iswgrp_handoff[0], &reset_manager_base->brg_mod_reset);
  289. writel(iswgrp_handoff[1], &nic301_regs->remap);
  290. break;
  291. case 'd': /* Disable */
  292. writel(0, &sysmgr_regs->fpgaintfgrp_module);
  293. writel(0, &sdr_ctrl->fpgaport_rst);
  294. socfpga_sdram_apply_static_cfg();
  295. writel(0, &reset_manager_base->brg_mod_reset);
  296. writel(1, &nic301_regs->remap);
  297. break;
  298. default:
  299. return CMD_RET_USAGE;
  300. }
  301. return 0;
  302. }
  303. U_BOOT_CMD(
  304. bridge, 2, 1, do_bridge,
  305. "SoCFPGA HPS FPGA bridge control",
  306. "enable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
  307. "bridge disable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
  308. ""
  309. );
  310. #endif