2 * Copyright (C) 2012-2017 Altera Corporation <www.altera.com>
4 * SPDX-License-Identifier: GPL-2.0+
16 #include <asm/arch/misc.h>
17 #include <asm/arch/reset_manager.h>
18 #include <asm/arch/scan_manager.h>
19 #include <asm/arch/sdram.h>
20 #include <asm/arch/system_manager.h>
21 #include <asm/arch/nic301.h>
22 #include <asm/arch/scu.h>
23 #include <asm/pl310.h>
25 #include <dt-bindings/reset/altr,rst-mgr.h>
27 DECLARE_GLOBAL_DATA_PTR;
29 static struct pl310_regs *const pl310 =
30 (struct pl310_regs *)CONFIG_SYS_PL310_BASE;
31 static struct socfpga_system_manager *sysmgr_regs =
32 (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
33 static struct nic301_registers *nic301_regs =
34 (struct nic301_registers *)SOCFPGA_L3REGS_ADDRESS;
35 static struct scu_registers *scu_regs =
36 (struct scu_registers *)SOCFPGA_MPUSCU_ADDRESS;
39 * DesignWare Ethernet initialization
41 #ifdef CONFIG_ETH_DESIGNWARE
42 void dwmac_deassert_reset(const unsigned int of_reset_id,
47 if (of_reset_id == EMAC0_RESET) {
48 physhift = SYSMGR_EMACGRP_CTRL_PHYSEL0_LSB;
49 reset = SOCFPGA_RESET(EMAC0);
50 } else if (of_reset_id == EMAC1_RESET) {
51 physhift = SYSMGR_EMACGRP_CTRL_PHYSEL1_LSB;
52 reset = SOCFPGA_RESET(EMAC1);
54 printf("GMAC: Invalid reset ID (%i)!\n", of_reset_id);
58 /* configure to PHY interface select choosed */
59 clrsetbits_le32(&sysmgr_regs->emacgrp_ctrl,
60 SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << physhift,
63 /* Release the EMAC controller from reset */
64 socfpga_per_reset(reset, 0);
67 static u32 dwmac_phymode_to_modereg(const char *phymode, u32 *modereg)
72 if (!strcmp(phymode, "mii") || !strcmp(phymode, "gmii")) {
73 *modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
77 if (!strcmp(phymode, "rgmii")) {
78 *modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII;
82 if (!strcmp(phymode, "rmii")) {
83 *modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII;
90 static int socfpga_eth_reset(void)
92 const void *fdt = gd->fdt_blob;
93 struct fdtdec_phandle_args args;
96 int nodes[2]; /* Max. two GMACs */
100 /* Put both GMACs into RESET state. */
101 socfpga_per_reset(SOCFPGA_RESET(EMAC0), 1);
102 socfpga_per_reset(SOCFPGA_RESET(EMAC1), 1);
104 count = fdtdec_find_aliases_for_id(fdt, "ethernet",
105 COMPAT_ALTERA_SOCFPGA_DWMAC,
106 nodes, ARRAY_SIZE(nodes));
107 for (i = 0; i < count; i++) {
112 ret = fdtdec_parse_phandle_with_args(fdt, node, "resets",
113 "#reset-cells", 1, 0,
115 if (ret || (args.args_count != 1)) {
116 debug("GMAC%i: Failed to parse DT 'resets'!\n", i);
120 phy_mode = fdt_getprop(fdt, node, "phy-mode", NULL);
121 ret = dwmac_phymode_to_modereg(phy_mode, &phy_modereg);
123 debug("GMAC%i: Failed to parse DT 'phy-mode'!\n", i);
127 dwmac_deassert_reset(args.args[0], phy_modereg);
133 static int socfpga_eth_reset(void)
139 static const struct {
143 } socfpga_fpga_model[] = {
145 { 0x2b15, "Cyclone V, E/A2", "cv_e_a2" },
146 { 0x2b05, "Cyclone V, E/A4", "cv_e_a4" },
147 { 0x2b22, "Cyclone V, E/A5", "cv_e_a5" },
148 { 0x2b13, "Cyclone V, E/A7", "cv_e_a7" },
149 { 0x2b14, "Cyclone V, E/A9", "cv_e_a9" },
150 /* Cyclone V GX/GT */
151 { 0x2b01, "Cyclone V, GX/C3", "cv_gx_c3" },
152 { 0x2b12, "Cyclone V, GX/C4", "cv_gx_c4" },
153 { 0x2b02, "Cyclone V, GX/C5 or GT/D5", "cv_gx_c5" },
154 { 0x2b03, "Cyclone V, GX/C7 or GT/D7", "cv_gx_c7" },
155 { 0x2b04, "Cyclone V, GX/C9 or GT/D9", "cv_gx_c9" },
156 /* Cyclone V SE/SX/ST */
157 { 0x2d11, "Cyclone V, SE/A2 or SX/C2", "cv_se_a2" },
158 { 0x2d01, "Cyclone V, SE/A4 or SX/C4", "cv_se_a4" },
159 { 0x2d12, "Cyclone V, SE/A5 or SX/C5 or ST/D5", "cv_se_a5" },
160 { 0x2d02, "Cyclone V, SE/A6 or SX/C6 or ST/D6", "cv_se_a6" },
162 { 0x2d03, "Arria V, D5", "av_d5" },
165 static int socfpga_fpga_id(const bool print_id)
167 const u32 altera_mi = 0x6e;
168 const u32 id = scan_mgr_get_fpga_id();
170 const u32 lsb = id & 0x00000001;
171 const u32 mi = (id >> 1) & 0x000007ff;
172 const u32 pn = (id >> 12) & 0x0000ffff;
173 const u32 version = (id >> 28) & 0x0000000f;
176 if ((mi != altera_mi) || (lsb != 1)) {
177 printf("FPGA: Not Altera chip ID\n");
181 for (i = 0; i < ARRAY_SIZE(socfpga_fpga_model); i++)
182 if (pn == socfpga_fpga_model[i].pn)
185 if (i == ARRAY_SIZE(socfpga_fpga_model)) {
186 printf("FPGA: Unknown Altera chip, ID 0x%08x\n", id);
191 printf("FPGA: Altera %s, version 0x%01x\n",
192 socfpga_fpga_model[i].name, version);
197 * Print CPU information
199 #if defined(CONFIG_DISPLAY_CPUINFO)
200 int print_cpuinfo(void)
203 SYSMGR_GET_BOOTINFO_BSEL(readl(&sysmgr_regs->bootinfo));
205 puts("CPU: Altera SoCFPGA Platform\n");
208 printf("BOOT: %s\n", bsel_str[bsel].name);
213 #ifdef CONFIG_ARCH_MISC_INIT
214 int arch_misc_init(void)
216 const u32 bsel = readl(&sysmgr_regs->bootinfo) & 0x7;
217 const int fpga_id = socfpga_fpga_id(0);
218 env_set("bootmode", bsel_str[bsel].mode);
220 env_set("fpgatype", socfpga_fpga_model[fpga_id].var);
221 return socfpga_eth_reset();
226 * Convert all NIC-301 AMBA slaves from secure to non-secure
228 static void socfpga_nic301_slave_ns(void)
230 writel(0x1, &nic301_regs->lwhps2fpgaregs);
231 writel(0x1, &nic301_regs->hps2fpgaregs);
232 writel(0x1, &nic301_regs->acp);
233 writel(0x1, &nic301_regs->rom);
234 writel(0x1, &nic301_regs->ocram);
235 writel(0x1, &nic301_regs->sdrdata);
238 static u32 iswgrp_handoff[8];
240 int arch_early_init_r(void)
245 * Write magic value into magic register to unlock support for
246 * issuing warm reset. The ancient kernel code expects this
247 * value to be written into the register by the bootloader, so
248 * to support that old code, we write it here instead of in the
249 * reset_cpu() function just before resetting the CPU.
251 writel(0xae9efebc, &sysmgr_regs->romcodegrp_warmramgrp_enable);
253 for (i = 0; i < 8; i++) /* Cache initial SW setting regs */
254 iswgrp_handoff[i] = readl(&sysmgr_regs->iswgrp_handoff[i]);
256 socfpga_bridges_reset(1);
258 socfpga_nic301_slave_ns();
261 * Private components security:
262 * U-Boot : configure private timer, global timer and cpu component
263 * access as non secure for kernel stage (as required by Linux)
265 setbits_le32(&scu_regs->sacr, 0xfff);
267 /* Configure the L2 controller to make SDRAM start at 0 */
268 #ifdef CONFIG_SOCFPGA_VIRTUAL_TARGET
269 writel(0x2, &nic301_regs->remap);
271 writel(0x1, &nic301_regs->remap); /* remap.mpuzero */
272 writel(0x1, &pl310->pl310_addr_filter_start);
275 /* Add device descriptor to FPGA device table */
278 #ifdef CONFIG_DESIGNWARE_SPI
279 /* Get Designware SPI controller out of reset */
280 socfpga_per_reset(SOCFPGA_RESET(SPIM0), 0);
281 socfpga_per_reset(SOCFPGA_RESET(SPIM1), 0);
284 #ifdef CONFIG_NAND_DENALI
285 socfpga_per_reset(SOCFPGA_RESET(NAND), 0);
291 #ifndef CONFIG_SPL_BUILD
292 static struct socfpga_reset_manager *reset_manager_base =
293 (struct socfpga_reset_manager *)SOCFPGA_RSTMGR_ADDRESS;
294 static struct socfpga_sdr_ctrl *sdr_ctrl =
295 (struct socfpga_sdr_ctrl *)SDR_CTRLGRP_ADDRESS;
297 static void socfpga_sdram_apply_static_cfg(void)
299 const u32 applymask = 0x8;
300 u32 val = readl(&sdr_ctrl->static_cfg) | applymask;
303 * SDRAM staticcfg register specific:
304 * When applying the register setting, the CPU must not access
305 * SDRAM. Luckily for us, we can abuse i-cache here to help us
306 * circumvent the SDRAM access issue. The idea is to make sure
307 * that the code is in one full i-cache line by branching past
308 * it and back. Once it is in the i-cache, we execute the core
309 * of the code and apply the register settings.
311 * The code below uses 7 instructions, while the Cortex-A9 has
312 * 32-byte cachelines, thus the limit is 8 instructions total.
323 : : "r"(val), "r"(&sdr_ctrl->static_cfg) : "memory", "cc");
326 static int do_bridge(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
329 return CMD_RET_USAGE;
334 case 'e': /* Enable */
335 writel(iswgrp_handoff[2], &sysmgr_regs->fpgaintfgrp_module);
336 socfpga_sdram_apply_static_cfg();
337 writel(iswgrp_handoff[3], &sdr_ctrl->fpgaport_rst);
338 writel(iswgrp_handoff[0], &reset_manager_base->brg_mod_reset);
339 writel(iswgrp_handoff[1], &nic301_regs->remap);
341 case 'd': /* Disable */
342 writel(0, &sysmgr_regs->fpgaintfgrp_module);
343 writel(0, &sdr_ctrl->fpgaport_rst);
344 socfpga_sdram_apply_static_cfg();
345 writel(0, &reset_manager_base->brg_mod_reset);
346 writel(1, &nic301_regs->remap);
349 return CMD_RET_USAGE;
356 bridge, 2, 1, do_bridge,
357 "SoCFPGA HPS FPGA bridge control",
358 "enable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
359 "bridge disable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"