2 * Copyright (C) 2012 Altera Corporation <www.altera.com>
4 * SPDX-License-Identifier: GPL-2.0+
16 #include <asm/arch/reset_manager.h>
17 #include <asm/arch/scan_manager.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>
23 #include <dt-bindings/reset/altr,rst-mgr.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 static struct pl310_regs *const pl310 =
28 (struct pl310_regs *)CONFIG_SYS_PL310_BASE;
29 static struct socfpga_system_manager *sysmgr_regs =
30 (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
31 static struct socfpga_reset_manager *reset_manager_base =
32 (struct socfpga_reset_manager *)SOCFPGA_RSTMGR_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;
40 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_1, PHYS_SDRAM_1_SIZE);
44 void enable_caches(void)
46 #ifndef CONFIG_SYS_ICACHE_OFF
49 #ifndef CONFIG_SYS_DCACHE_OFF
54 void v7_outer_cache_enable(void)
56 /* Disable the L2 cache */
57 clrbits_le32(&pl310->pl310_ctrl, L2X0_CTRL_EN);
59 /* enable BRESP, instruction and data prefetch, full line of zeroes */
60 setbits_le32(&pl310->pl310_aux_ctrl,
61 L310_AUX_CTRL_DATA_PREFETCH_MASK |
62 L310_AUX_CTRL_INST_PREFETCH_MASK |
63 L310_SHARED_ATT_OVERRIDE_ENABLE);
65 /* Enable the L2 cache */
66 setbits_le32(&pl310->pl310_ctrl, L2X0_CTRL_EN);
69 void v7_outer_cache_disable(void)
71 /* Disable the L2 cache */
72 clrbits_le32(&pl310->pl310_ctrl, L2X0_CTRL_EN);
76 * DesignWare Ethernet initialization
78 #ifdef CONFIG_ETH_DESIGNWARE
79 static void dwmac_deassert_reset(const unsigned int of_reset_id,
84 if (of_reset_id == EMAC0_RESET) {
85 physhift = SYSMGR_EMACGRP_CTRL_PHYSEL0_LSB;
86 reset = SOCFPGA_RESET(EMAC0);
87 } else if (of_reset_id == EMAC1_RESET) {
88 physhift = SYSMGR_EMACGRP_CTRL_PHYSEL1_LSB;
89 reset = SOCFPGA_RESET(EMAC1);
91 printf("GMAC: Invalid reset ID (%i)!\n", of_reset_id);
95 /* Clearing emac0 PHY interface select to 0 */
96 clrbits_le32(&sysmgr_regs->emacgrp_ctrl,
97 SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << physhift);
99 /* configure to PHY interface select choosed */
100 setbits_le32(&sysmgr_regs->emacgrp_ctrl,
101 phymode << physhift);
103 /* Release the EMAC controller from reset */
104 socfpga_per_reset(reset, 0);
107 static u32 dwmac_phymode_to_modereg(const char *phymode, u32 *modereg)
112 if (!strcmp(phymode, "mii") || !strcmp(phymode, "gmii")) {
113 *modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
117 if (!strcmp(phymode, "rgmii")) {
118 *modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII;
122 if (!strcmp(phymode, "rmii")) {
123 *modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII;
130 static int socfpga_eth_reset(void)
132 const void *fdt = gd->fdt_blob;
133 struct fdtdec_phandle_args args;
134 const char *phy_mode;
136 int nodes[2]; /* Max. two GMACs */
140 /* Put both GMACs into RESET state. */
141 socfpga_per_reset(SOCFPGA_RESET(EMAC0), 1);
142 socfpga_per_reset(SOCFPGA_RESET(EMAC1), 1);
144 count = fdtdec_find_aliases_for_id(fdt, "ethernet",
145 COMPAT_ALTERA_SOCFPGA_DWMAC,
146 nodes, ARRAY_SIZE(nodes));
147 for (i = 0; i < count; i++) {
152 ret = fdtdec_parse_phandle_with_args(fdt, node, "resets",
153 "#reset-cells", 1, 0,
155 if (ret || (args.args_count != 1)) {
156 debug("GMAC%i: Failed to parse DT 'resets'!\n", i);
160 phy_mode = fdt_getprop(fdt, node, "phy-mode", NULL);
161 ret = dwmac_phymode_to_modereg(phy_mode, &phy_modereg);
163 debug("GMAC%i: Failed to parse DT 'phy-mode'!\n", i);
167 dwmac_deassert_reset(args.args[0], phy_modereg);
173 static int socfpga_eth_reset(void)
183 { "rsvd", "Reserved", },
184 { "fpga", "FPGA (HPS2FPGA Bridge)", },
185 { "nand", "NAND Flash (1.8V)", },
186 { "nand", "NAND Flash (3.0V)", },
187 { "sd", "SD/MMC External Transceiver (1.8V)", },
188 { "sd", "SD/MMC Internal Transceiver (3.0V)", },
189 { "qspi", "QSPI Flash (1.8V)", },
190 { "qspi", "QSPI Flash (3.0V)", },
193 static const struct {
197 } const socfpga_fpga_model[] = {
199 { 0x2b15, "Cyclone V, E/A2", "cv_e_a2" },
200 { 0x2b05, "Cyclone V, E/A4", "cv_e_a4" },
201 { 0x2b22, "Cyclone V, E/A5", "cv_e_a5" },
202 { 0x2b13, "Cyclone V, E/A7", "cv_e_a7" },
203 { 0x2b14, "Cyclone V, E/A9", "cv_e_a9" },
204 /* Cyclone V GX/GT */
205 { 0x2b01, "Cyclone V, GX/C3", "cv_gx_c3" },
206 { 0x2b12, "Cyclone V, GX/C4", "cv_gx_c4" },
207 { 0x2b02, "Cyclone V, GX/C5 or GT/D5", "cv_gx_c5" },
208 { 0x2b03, "Cyclone V, GX/C7 or GT/D7", "cv_gx_c7" },
209 { 0x2b04, "Cyclone V, GX/C9 or GT/D9", "cv_gx_c9" },
210 /* Cyclone V SE/SX/ST */
211 { 0x2d11, "Cyclone V, SE/A2 or SX/C2", "cv_se_a2" },
212 { 0x2d01, "Cyclone V, SE/A4 or SX/C4", "cv_se_a4" },
213 { 0x2d12, "Cyclone V, SE/A5 or SX/C5 or ST/D5", "cv_se_a5" },
214 { 0x2d02, "Cyclone V, SE/A6 or SX/C6 or ST/D6", "cv_se_a6" },
216 { 0x2d03, "Arria V, D5", "av_d5" },
219 static int socfpga_fpga_id(const bool print_id)
221 const u32 altera_mi = 0x6e;
222 const u32 id = scan_mgr_get_fpga_id();
224 const u32 lsb = id & 0x00000001;
225 const u32 mi = (id >> 1) & 0x000007ff;
226 const u32 pn = (id >> 12) & 0x0000ffff;
227 const u32 version = (id >> 28) & 0x0000000f;
230 if ((mi != altera_mi) || (lsb != 1)) {
231 printf("FPGA: Not Altera chip ID\n");
235 for (i = 0; i < ARRAY_SIZE(socfpga_fpga_model); i++)
236 if (pn == socfpga_fpga_model[i].pn)
239 if (i == ARRAY_SIZE(socfpga_fpga_model)) {
240 printf("FPGA: Unknown Altera chip, ID 0x%08x\n", id);
245 printf("FPGA: Altera %s, version 0x%01x\n",
246 socfpga_fpga_model[i].name, version);
251 * Print CPU information
253 #if defined(CONFIG_DISPLAY_CPUINFO)
254 int print_cpuinfo(void)
256 const u32 bsel = readl(&sysmgr_regs->bootinfo) & 0x7;
257 puts("CPU: Altera SoCFPGA Platform\n");
259 printf("BOOT: %s\n", bsel_str[bsel].name);
264 #ifdef CONFIG_ARCH_MISC_INIT
265 int arch_misc_init(void)
267 const u32 bsel = readl(&sysmgr_regs->bootinfo) & 0x7;
268 const int fpga_id = socfpga_fpga_id(0);
269 setenv("bootmode", bsel_str[bsel].mode);
271 setenv("fpgatype", socfpga_fpga_model[fpga_id].var);
272 return socfpga_eth_reset();
276 #if defined(CONFIG_SYS_CONSOLE_IS_IN_ENV) && \
277 defined(CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE)
278 int overwrite_console(void)
286 * FPGA programming support for SoC FPGA Cyclone V
288 static Altera_desc altera_fpga[] = {
293 fast_passive_parallel,
294 /* No limitation as additional data will be ignored */
296 /* No device function table */
298 /* Base interface address specified in driver */
300 /* No cookie implementation */
305 /* add device descriptor to FPGA device table */
306 static void socfpga_fpga_add(void)
310 for (i = 0; i < ARRAY_SIZE(altera_fpga); i++)
311 fpga_add(fpga_altera, &altera_fpga[i]);
314 static inline void socfpga_fpga_add(void) {}
317 int arch_cpu_init(void)
319 #ifdef CONFIG_HW_WATCHDOG
321 * In case the watchdog is enabled, make sure to (re-)configure it
322 * so that the defined timeout is valid. Otherwise the SPL (Perloader)
323 * timeout value is still active which might too short for Linux
329 * If the HW watchdog is NOT enabled, make sure it is not running,
330 * for example because it was enabled in the preloader. This might
331 * trigger a watchdog-triggered reboot of Linux kernel later.
332 * Toggle watchdog reset, so watchdog in not running state.
334 socfpga_per_reset(SOCFPGA_RESET(L4WD0), 1);
335 socfpga_per_reset(SOCFPGA_RESET(L4WD0), 0);
342 * Convert all NIC-301 AMBA slaves from secure to non-secure
344 static void socfpga_nic301_slave_ns(void)
346 writel(0x1, &nic301_regs->lwhps2fpgaregs);
347 writel(0x1, &nic301_regs->hps2fpgaregs);
348 writel(0x1, &nic301_regs->acp);
349 writel(0x1, &nic301_regs->rom);
350 writel(0x1, &nic301_regs->ocram);
351 writel(0x1, &nic301_regs->sdrdata);
354 static uint32_t iswgrp_handoff[8];
356 int arch_early_init_r(void)
361 * Write magic value into magic register to unlock support for
362 * issuing warm reset. The ancient kernel code expects this
363 * value to be written into the register by the bootloader, so
364 * to support that old code, we write it here instead of in the
365 * reset_cpu() function just before resetting the CPU.
367 writel(0xae9efebc, &sysmgr_regs->romcodegrp_warmramgrp_enable);
369 for (i = 0; i < 8; i++) /* Cache initial SW setting regs */
370 iswgrp_handoff[i] = readl(&sysmgr_regs->iswgrp_handoff[i]);
372 socfpga_bridges_reset(1);
373 socfpga_nic301_slave_ns();
376 * Private components security:
377 * U-Boot : configure private timer, global timer and cpu component
378 * access as non secure for kernel stage (as required by Linux)
380 setbits_le32(&scu_regs->sacr, 0xfff);
382 /* Configure the L2 controller to make SDRAM start at 0 */
383 #ifdef CONFIG_SOCFPGA_VIRTUAL_TARGET
384 writel(0x2, &nic301_regs->remap);
386 writel(0x1, &nic301_regs->remap); /* remap.mpuzero */
387 writel(0x1, &pl310->pl310_addr_filter_start);
390 /* Add device descriptor to FPGA device table */
393 #ifdef CONFIG_DESIGNWARE_SPI
394 /* Get Designware SPI controller out of reset */
395 socfpga_per_reset(SOCFPGA_RESET(SPIM0), 0);
396 socfpga_per_reset(SOCFPGA_RESET(SPIM1), 0);
399 #ifdef CONFIG_NAND_DENALI
400 socfpga_per_reset(SOCFPGA_RESET(NAND), 0);
406 static void socfpga_sdram_apply_static_cfg(void)
408 const uint32_t staticcfg = SOCFPGA_SDR_ADDRESS + 0x505c;
409 const uint32_t applymask = 0x8;
410 uint32_t val = readl(staticcfg) | applymask;
413 * SDRAM staticcfg register specific:
414 * When applying the register setting, the CPU must not access
415 * SDRAM. Luckily for us, we can abuse i-cache here to help us
416 * circumvent the SDRAM access issue. The idea is to make sure
417 * that the code is in one full i-cache line by branching past
418 * it and back. Once it is in the i-cache, we execute the core
419 * of the code and apply the register settings.
421 * The code below uses 7 instructions, while the Cortex-A9 has
422 * 32-byte cachelines, thus the limit is 8 instructions total.
433 : : "r"(val), "r"(staticcfg) : "memory", "cc");
436 int do_bridge(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
439 return CMD_RET_USAGE;
444 case 'e': /* Enable */
445 writel(iswgrp_handoff[2], &sysmgr_regs->fpgaintfgrp_module);
446 socfpga_sdram_apply_static_cfg();
447 writel(iswgrp_handoff[3], SOCFPGA_SDR_ADDRESS + 0x5080);
448 writel(iswgrp_handoff[0], &reset_manager_base->brg_mod_reset);
449 writel(iswgrp_handoff[1], &nic301_regs->remap);
451 case 'd': /* Disable */
452 writel(0, &sysmgr_regs->fpgaintfgrp_module);
453 writel(0, SOCFPGA_SDR_ADDRESS + 0x5080);
454 socfpga_sdram_apply_static_cfg();
455 writel(0, &reset_manager_base->brg_mod_reset);
456 writel(1, &nic301_regs->remap);
459 return CMD_RET_USAGE;
466 bridge, 2, 1, do_bridge,
467 "SoCFPGA HPS FPGA bridge control",
468 "enable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
469 "bridge disable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"