misc_gen5.c 9.2 KB

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