blob: d653bf624bc7bd685531cda6bd0fe2735301a510 [file] [log] [blame]
Dinh Nguyen77754402012-10-04 06:46:02 +00001/*
2 * Copyright (C) 2012 Altera Corporation <www.altera.com>
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Dinh Nguyen77754402012-10-04 06:46:02 +00005 */
6
7#include <common.h>
8#include <asm/io.h>
Pavel Machek230fe9b2014-09-08 14:08:45 +02009#include <altera.h>
Pavel Machek99b97102014-07-14 14:14:17 +020010#include <miiphy.h>
11#include <netdev.h>
Stefan Roesed0e932d2014-12-19 13:49:10 +010012#include <watchdog.h>
Pavel Machekde6da922014-09-09 14:03:28 +020013#include <asm/arch/reset_manager.h>
Pavel Machek45d6e672014-09-08 14:08:45 +020014#include <asm/arch/system_manager.h>
Pavel Machek4e736862014-09-08 14:08:45 +020015#include <asm/arch/dwmmc.h>
Marek Vasut60d804c2014-09-15 03:58:22 +020016#include <asm/arch/nic301.h>
Pavel Machek13e81d42014-09-08 14:08:45 +020017#include <asm/arch/scu.h>
Marek Vasut60d804c2014-09-15 03:58:22 +020018#include <asm/pl310.h>
Dinh Nguyen77754402012-10-04 06:46:02 +000019
20DECLARE_GLOBAL_DATA_PTR;
21
Marek Vasut60d804c2014-09-15 03:58:22 +020022static struct pl310_regs *const pl310 =
23 (struct pl310_regs *)CONFIG_SYS_PL310_BASE;
Pavel Machek45d6e672014-09-08 14:08:45 +020024static struct socfpga_system_manager *sysmgr_regs =
25 (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
Marek Vasut7249faf2014-09-08 14:08:45 +020026static struct socfpga_reset_manager *reset_manager_base =
27 (struct socfpga_reset_manager *)SOCFPGA_RSTMGR_ADDRESS;
Marek Vasut60d804c2014-09-15 03:58:22 +020028static struct nic301_registers *nic301_regs =
29 (struct nic301_registers *)SOCFPGA_L3REGS_ADDRESS;
Pavel Machek13e81d42014-09-08 14:08:45 +020030static struct scu_registers *scu_regs =
31 (struct scu_registers *)SOCFPGA_MPUSCU_ADDRESS;
Pavel Machek45d6e672014-09-08 14:08:45 +020032
Dinh Nguyen77754402012-10-04 06:46:02 +000033int dram_init(void)
34{
35 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_1, PHYS_SDRAM_1_SIZE);
36 return 0;
37}
Chin Liang See23f23f22014-06-10 02:23:45 -050038
Marek Vasut4ab333b2014-09-21 13:57:40 +020039void enable_caches(void)
40{
41#ifndef CONFIG_SYS_ICACHE_OFF
42 icache_enable();
43#endif
44#ifndef CONFIG_SYS_DCACHE_OFF
45 dcache_enable();
46#endif
47}
48
Pavel Machek45d6e672014-09-08 14:08:45 +020049/*
50 * DesignWare Ethernet initialization
51 */
Simon Glassef48f6d2015-04-05 16:07:34 -060052#ifdef CONFIG_ETH_DESIGNWARE
Pavel Machek45d6e672014-09-08 14:08:45 +020053int cpu_eth_init(bd_t *bis)
54{
55#if CONFIG_EMAC_BASE == SOCFPGA_EMAC0_ADDRESS
56 const int physhift = SYSMGR_EMACGRP_CTRL_PHYSEL0_LSB;
Marek Vasuta71df7a2015-07-09 02:51:56 +020057 const u32 reset = SOCFPGA_RESET(EMAC0);
Pavel Machek45d6e672014-09-08 14:08:45 +020058#elif CONFIG_EMAC_BASE == SOCFPGA_EMAC1_ADDRESS
59 const int physhift = SYSMGR_EMACGRP_CTRL_PHYSEL1_LSB;
Marek Vasuta71df7a2015-07-09 02:51:56 +020060 const u32 reset = SOCFPGA_RESET(EMAC1);
Pavel Machek45d6e672014-09-08 14:08:45 +020061#else
62#error "Incorrect CONFIG_EMAC_BASE value!"
63#endif
64
65 /* Initialize EMAC. This needs to be done at least once per boot. */
66
67 /*
68 * Putting the EMAC controller to reset when configuring the PHY
69 * interface select at System Manager
70 */
Marek Vasuta71df7a2015-07-09 02:51:56 +020071 socfpga_per_reset(SOCFPGA_RESET(EMAC0), 1);
72 socfpga_per_reset(SOCFPGA_RESET(EMAC1), 1);
Pavel Machek45d6e672014-09-08 14:08:45 +020073
74 /* Clearing emac0 PHY interface select to 0 */
75 clrbits_le32(&sysmgr_regs->emacgrp_ctrl,
76 SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << physhift);
77
78 /* configure to PHY interface select choosed */
79 setbits_le32(&sysmgr_regs->emacgrp_ctrl,
80 SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII << physhift);
81
82 /* Release the EMAC controller from reset */
Marek Vasuta71df7a2015-07-09 02:51:56 +020083 socfpga_per_reset(reset, 0);
Pavel Machek45d6e672014-09-08 14:08:45 +020084
85 /* initialize and register the emac */
86 return designware_initialize(CONFIG_EMAC_BASE,
87 CONFIG_PHY_INTERFACE_MODE);
88}
89#endif
90
Pavel Machek4e736862014-09-08 14:08:45 +020091#ifdef CONFIG_DWMMC
92/*
93 * Initializes MMC controllers.
94 * to override, implement board_mmc_init()
95 */
96int cpu_mmc_init(bd_t *bis)
97{
98 return socfpga_dwmmc_init(SOCFPGA_SDMMC_ADDRESS,
99 CONFIG_HPS_SDMMC_BUSWIDTH, 0);
100}
101#endif
102
Chin Liang See23f23f22014-06-10 02:23:45 -0500103#if defined(CONFIG_DISPLAY_CPUINFO)
Marek Vasutd85e3112015-07-21 16:10:13 +0200104const char * const bsel_str[] = {
105 "Reserved",
106 "FPGA (HPS2FPGA Bridge)",
107 "NAND Flash (1.8V)",
108 "NAND Flash (3.0V)",
109 "SD/MMC External Transceiver (1.8V)",
110 "SD/MMC Internal Transceiver (3.0V)",
111 "QSPI Flash (1.8V)",
112 "QSPI Flash (3.0V)",
113};
114
Chin Liang See23f23f22014-06-10 02:23:45 -0500115/*
116 * Print CPU information
117 */
118int print_cpuinfo(void)
119{
Marek Vasutd85e3112015-07-21 16:10:13 +0200120 const u32 bsel = readl(&sysmgr_regs->bootinfo) & 0x7;
Pavel Machekd5a3d3c2014-09-08 14:08:45 +0200121 puts("CPU: Altera SoCFPGA Platform\n");
Marek Vasutd85e3112015-07-21 16:10:13 +0200122 printf("BOOT: %s\n", bsel_str[bsel]);
Chin Liang See23f23f22014-06-10 02:23:45 -0500123 return 0;
124}
125#endif
126
127#if defined(CONFIG_SYS_CONSOLE_IS_IN_ENV) && \
128defined(CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE)
129int overwrite_console(void)
130{
131 return 0;
132}
133#endif
134
Pavel Machek230fe9b2014-09-08 14:08:45 +0200135#ifdef CONFIG_FPGA
136/*
137 * FPGA programming support for SoC FPGA Cyclone V
138 */
139static Altera_desc altera_fpga[] = {
140 {
141 /* Family */
142 Altera_SoCFPGA,
143 /* Interface type */
144 fast_passive_parallel,
145 /* No limitation as additional data will be ignored */
146 -1,
147 /* No device function table */
148 NULL,
149 /* Base interface address specified in driver */
150 NULL,
151 /* No cookie implementation */
152 0
153 },
154};
155
156/* add device descriptor to FPGA device table */
157static void socfpga_fpga_add(void)
158{
159 int i;
160 fpga_init();
161 for (i = 0; i < ARRAY_SIZE(altera_fpga); i++)
162 fpga_add(fpga_altera, &altera_fpga[i]);
163}
164#else
165static inline void socfpga_fpga_add(void) {}
166#endif
167
Pavel Machekde6da922014-09-09 14:03:28 +0200168int arch_cpu_init(void)
169{
Stefan Roesed0e932d2014-12-19 13:49:10 +0100170#ifdef CONFIG_HW_WATCHDOG
171 /*
172 * In case the watchdog is enabled, make sure to (re-)configure it
173 * so that the defined timeout is valid. Otherwise the SPL (Perloader)
174 * timeout value is still active which might too short for Linux
175 * booting.
176 */
177 hw_watchdog_init();
178#else
Pavel Machekde6da922014-09-09 14:03:28 +0200179 /*
180 * If the HW watchdog is NOT enabled, make sure it is not running,
181 * for example because it was enabled in the preloader. This might
182 * trigger a watchdog-triggered reboot of Linux kernel later.
Marek Vasuta71df7a2015-07-09 02:51:56 +0200183 * Toggle watchdog reset, so watchdog in not running state.
Pavel Machekde6da922014-09-09 14:03:28 +0200184 */
Marek Vasuta71df7a2015-07-09 02:51:56 +0200185 socfpga_per_reset(SOCFPGA_RESET(L4WD0), 1);
186 socfpga_per_reset(SOCFPGA_RESET(L4WD0), 0);
Pavel Machekde6da922014-09-09 14:03:28 +0200187#endif
Stefan Roesed0e932d2014-12-19 13:49:10 +0100188
Pavel Machekde6da922014-09-09 14:03:28 +0200189 return 0;
190}
191
Pavel Machek13e81d42014-09-08 14:08:45 +0200192/*
193 * Convert all NIC-301 AMBA slaves from secure to non-secure
194 */
195static 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
Marek Vasut7249faf2014-09-08 14:08:45 +0200205static uint32_t iswgrp_handoff[8];
206
Marek Vasutfc520892014-10-18 03:52:36 +0200207int arch_early_init_r(void)
Chin Liang See23f23f22014-06-10 02:23:45 -0500208{
Marek Vasut7249faf2014-09-08 14:08:45 +0200209 int i;
Marek Vasutef848612015-07-12 15:11:03 +0200210
211 /*
212 * Write magic value into magic register to unlock support for
213 * issuing warm reset. The ancient kernel code expects this
214 * value to be written into the register by the bootloader, so
215 * to support that old code, we write it here instead of in the
216 * reset_cpu() function just before reseting the CPU.
217 */
218 writel(0xae9efebc, &sysmgr_regs->romcodegrp_warmramgrp_enable);
219
Marek Vasut7249faf2014-09-08 14:08:45 +0200220 for (i = 0; i < 8; i++) /* Cache initial SW setting regs */
221 iswgrp_handoff[i] = readl(&sysmgr_regs->iswgrp_handoff[i]);
222
Pavel Machek13e81d42014-09-08 14:08:45 +0200223 socfpga_bridges_reset(1);
224 socfpga_nic301_slave_ns();
225
226 /*
227 * Private components security:
228 * U-Boot : configure private timer, global timer and cpu component
229 * access as non secure for kernel stage (as required by Linux)
230 */
231 setbits_le32(&scu_regs->sacr, 0xfff);
232
Marek Vasut60d804c2014-09-15 03:58:22 +0200233 /* Configure the L2 controller to make SDRAM start at 0 */
234#ifdef CONFIG_SOCFPGA_VIRTUAL_TARGET
235 writel(0x2, &nic301_regs->remap);
236#else
237 writel(0x1, &nic301_regs->remap); /* remap.mpuzero */
238 writel(0x1, &pl310->pl310_addr_filter_start);
239#endif
240
Pavel Machek230fe9b2014-09-08 14:08:45 +0200241 /* Add device descriptor to FPGA device table */
242 socfpga_fpga_add();
Stefan Roesea877bec2014-11-07 13:50:30 +0100243
244#ifdef CONFIG_DESIGNWARE_SPI
245 /* Get Designware SPI controller out of reset */
Marek Vasuta71df7a2015-07-09 02:51:56 +0200246 socfpga_per_reset(SOCFPGA_RESET(SPIM0), 0);
247 socfpga_per_reset(SOCFPGA_RESET(SPIM1), 0);
Stefan Roesea877bec2014-11-07 13:50:30 +0100248#endif
249
Chin Liang See23f23f22014-06-10 02:23:45 -0500250 return 0;
251}
Marek Vasut7249faf2014-09-08 14:08:45 +0200252
253static void socfpga_sdram_apply_static_cfg(void)
254{
255 const uint32_t staticcfg = SOCFPGA_SDR_ADDRESS + 0x505c;
256 const uint32_t applymask = 0x8;
257 uint32_t val = readl(staticcfg) | applymask;
258
259 /*
260 * SDRAM staticcfg register specific:
261 * When applying the register setting, the CPU must not access
262 * SDRAM. Luckily for us, we can abuse i-cache here to help us
263 * circumvent the SDRAM access issue. The idea is to make sure
264 * that the code is in one full i-cache line by branching past
265 * it and back. Once it is in the i-cache, we execute the core
266 * of the code and apply the register settings.
267 *
268 * The code below uses 7 instructions, while the Cortex-A9 has
269 * 32-byte cachelines, thus the limit is 8 instructions total.
270 */
271 asm volatile(
272 ".align 5 \n"
273 " b 2f \n"
274 "1: str %0, [%1] \n"
275 " dsb \n"
276 " isb \n"
277 " b 3f \n"
278 "2: b 1b \n"
279 "3: nop \n"
280 : : "r"(val), "r"(staticcfg) : "memory", "cc");
281}
282
283int do_bridge(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
284{
285 if (argc != 2)
286 return CMD_RET_USAGE;
287
288 argv++;
289
290 switch (*argv[0]) {
291 case 'e': /* Enable */
292 writel(iswgrp_handoff[2], &sysmgr_regs->fpgaintfgrp_module);
293 socfpga_sdram_apply_static_cfg();
294 writel(iswgrp_handoff[3], SOCFPGA_SDR_ADDRESS + 0x5080);
295 writel(iswgrp_handoff[0], &reset_manager_base->brg_mod_reset);
296 writel(iswgrp_handoff[1], &nic301_regs->remap);
297 break;
298 case 'd': /* Disable */
299 writel(0, &sysmgr_regs->fpgaintfgrp_module);
300 writel(0, SOCFPGA_SDR_ADDRESS + 0x5080);
301 socfpga_sdram_apply_static_cfg();
302 writel(0, &reset_manager_base->brg_mod_reset);
303 writel(1, &nic301_regs->remap);
304 break;
305 default:
306 return CMD_RET_USAGE;
307 }
308
309 return 0;
310}
311
312U_BOOT_CMD(
313 bridge, 2, 1, do_bridge,
314 "SoCFPGA HPS FPGA bridge control",
315 "enable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
316 "bridge disable - Enable HPS-to-FPGA, FPGA-to-HPS, LWHPS-to-FPGA bridges\n"
317 ""
318);