misc_gen5.c 9.2 KB

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