blob: 5cbd8a432558207e776d7f02dd5958ea20753465 [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>
Dinh Nguyenbd48c062015-08-01 03:42:10 +02009#include <errno.h>
Marek Vasut6ab00db2015-07-25 19:33:56 +020010#include <fdtdec.h>
11#include <libfdt.h>
Pavel Machek230fe9b2014-09-08 14:08:45 +020012#include <altera.h>
Pavel Machek99b97102014-07-14 14:14:17 +020013#include <miiphy.h>
14#include <netdev.h>
Stefan Roesed0e932d2014-12-19 13:49:10 +010015#include <watchdog.h>
Pavel Machekde6da922014-09-09 14:03:28 +020016#include <asm/arch/reset_manager.h>
Dinh Nguyenbd48c062015-08-01 03:42:10 +020017#include <asm/arch/scan_manager.h>
Pavel Machek45d6e672014-09-08 14:08:45 +020018#include <asm/arch/system_manager.h>
Marek Vasut60d804c2014-09-15 03:58:22 +020019#include <asm/arch/nic301.h>
Pavel Machek13e81d42014-09-08 14:08:45 +020020#include <asm/arch/scu.h>
Marek Vasut60d804c2014-09-15 03:58:22 +020021#include <asm/pl310.h>
Dinh Nguyen77754402012-10-04 06:46:02 +000022
Marek Vasut6ab00db2015-07-25 19:33:56 +020023#include <dt-bindings/reset/altr,rst-mgr.h>
24
Dinh Nguyen77754402012-10-04 06:46:02 +000025DECLARE_GLOBAL_DATA_PTR;
26
Marek Vasut60d804c2014-09-15 03:58:22 +020027static struct pl310_regs *const pl310 =
28 (struct pl310_regs *)CONFIG_SYS_PL310_BASE;
Pavel Machek45d6e672014-09-08 14:08:45 +020029static struct socfpga_system_manager *sysmgr_regs =
30 (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
Marek Vasut7249faf2014-09-08 14:08:45 +020031static struct socfpga_reset_manager *reset_manager_base =
32 (struct socfpga_reset_manager *)SOCFPGA_RSTMGR_ADDRESS;
Marek Vasut60d804c2014-09-15 03:58:22 +020033static struct nic301_registers *nic301_regs =
34 (struct nic301_registers *)SOCFPGA_L3REGS_ADDRESS;
Pavel Machek13e81d42014-09-08 14:08:45 +020035static struct scu_registers *scu_regs =
36 (struct scu_registers *)SOCFPGA_MPUSCU_ADDRESS;
Pavel Machek45d6e672014-09-08 14:08:45 +020037
Dinh Nguyen77754402012-10-04 06:46:02 +000038int dram_init(void)
39{
40 gd->ram_size = get_ram_size((long *)PHYS_SDRAM_1, PHYS_SDRAM_1_SIZE);
41 return 0;
42}
Chin Liang See23f23f22014-06-10 02:23:45 -050043
Marek Vasut4ab333b2014-09-21 13:57:40 +020044void enable_caches(void)
45{
46#ifndef CONFIG_SYS_ICACHE_OFF
47 icache_enable();
48#endif
49#ifndef CONFIG_SYS_DCACHE_OFF
50 dcache_enable();
51#endif
52}
53
Dinh Nguyen8d8e13e2015-10-15 10:13:36 -050054void v7_outer_cache_enable(void)
55{
Marek Vasut07806972015-12-20 04:00:09 +010056 /* Disable the L2 cache */
57 clrbits_le32(&pl310->pl310_ctrl, L2X0_CTRL_EN);
Dinh Nguyen8d8e13e2015-10-15 10:13:36 -050058
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);
Marek Vasut07806972015-12-20 04:00:09 +010064
65 /* Enable the L2 cache */
66 setbits_le32(&pl310->pl310_ctrl, L2X0_CTRL_EN);
67}
68
69void v7_outer_cache_disable(void)
70{
71 /* Disable the L2 cache */
72 clrbits_le32(&pl310->pl310_ctrl, L2X0_CTRL_EN);
Dinh Nguyen8d8e13e2015-10-15 10:13:36 -050073}
74
Pavel Machek45d6e672014-09-08 14:08:45 +020075/*
76 * DesignWare Ethernet initialization
77 */
Simon Glassef48f6d2015-04-05 16:07:34 -060078#ifdef CONFIG_ETH_DESIGNWARE
Marek Vasut5f79d002016-03-21 13:38:11 +010079static void dwmac_deassert_reset(const unsigned int of_reset_id,
80 const u32 phymode)
Pavel Machek45d6e672014-09-08 14:08:45 +020081{
Marek Vasut6ab00db2015-07-25 19:33:56 +020082 u32 physhift, reset;
Pavel Machek45d6e672014-09-08 14:08:45 +020083
Marek Vasut6ab00db2015-07-25 19:33:56 +020084 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);
90 } else {
91 printf("GMAC: Invalid reset ID (%i)!\n", of_reset_id);
92 return;
93 }
Pavel Machek45d6e672014-09-08 14:08:45 +020094
95 /* Clearing emac0 PHY interface select to 0 */
96 clrbits_le32(&sysmgr_regs->emacgrp_ctrl,
97 SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << physhift);
98
99 /* configure to PHY interface select choosed */
100 setbits_le32(&sysmgr_regs->emacgrp_ctrl,
Marek Vasut5f79d002016-03-21 13:38:11 +0100101 phymode << physhift);
Pavel Machek45d6e672014-09-08 14:08:45 +0200102
103 /* Release the EMAC controller from reset */
Marek Vasuta71df7a2015-07-09 02:51:56 +0200104 socfpga_per_reset(reset, 0);
Marek Vasut6ab00db2015-07-25 19:33:56 +0200105}
106
Marek Vasut5f79d002016-03-21 13:38:11 +0100107static u32 dwmac_phymode_to_modereg(const char *phymode, u32 *modereg)
108{
109 if (!phymode)
110 return -EINVAL;
111
112 if (!strcmp(phymode, "mii") || !strcmp(phymode, "gmii")) {
113 *modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
114 return 0;
115 }
116
117 if (!strcmp(phymode, "rgmii")) {
118 *modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII;
119 return 0;
120 }
121
122 if (!strcmp(phymode, "rmii")) {
123 *modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII;
124 return 0;
125 }
126
127 return -EINVAL;
128}
129
Marek Vasute6e34ca2016-02-11 16:36:43 +0100130static int socfpga_eth_reset(void)
Marek Vasut6ab00db2015-07-25 19:33:56 +0200131{
132 const void *fdt = gd->fdt_blob;
133 struct fdtdec_phandle_args args;
Marek Vasut5f79d002016-03-21 13:38:11 +0100134 const char *phy_mode;
135 u32 phy_modereg;
Marek Vasut6ab00db2015-07-25 19:33:56 +0200136 int nodes[2]; /* Max. two GMACs */
137 int ret, count;
138 int i, node;
139
140 /* Put both GMACs into RESET state. */
141 socfpga_per_reset(SOCFPGA_RESET(EMAC0), 1);
142 socfpga_per_reset(SOCFPGA_RESET(EMAC1), 1);
143
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++) {
148 node = nodes[i];
149 if (node <= 0)
150 continue;
151
152 ret = fdtdec_parse_phandle_with_args(fdt, node, "resets",
153 "#reset-cells", 1, 0,
154 &args);
155 if (ret || (args.args_count != 1)) {
156 debug("GMAC%i: Failed to parse DT 'resets'!\n", i);
157 continue;
158 }
159
Marek Vasut5f79d002016-03-21 13:38:11 +0100160 phy_mode = fdt_getprop(fdt, node, "phy-mode", NULL);
161 ret = dwmac_phymode_to_modereg(phy_mode, &phy_modereg);
162 if (ret) {
163 debug("GMAC%i: Failed to parse DT 'phy-mode'!\n", i);
164 continue;
165 }
166
167 dwmac_deassert_reset(args.args[0], phy_modereg);
Marek Vasut6ab00db2015-07-25 19:33:56 +0200168 }
Pavel Machek45d6e672014-09-08 14:08:45 +0200169
Marek Vasute14d3f72015-07-25 18:47:02 +0200170 return 0;
Pavel Machek45d6e672014-09-08 14:08:45 +0200171}
Marek Vasute6e34ca2016-02-11 16:36:43 +0100172#else
173static int socfpga_eth_reset(void)
174{
Anatolij Gustschin5289c5f2016-05-06 17:16:31 +0200175 return 0;
Marek Vasute6e34ca2016-02-11 16:36:43 +0100176};
Pavel Machek45d6e672014-09-08 14:08:45 +0200177#endif
178
Marek Vasut9ec74142015-07-22 05:40:12 +0200179struct {
180 const char *mode;
181 const char *name;
182} bsel_str[] = {
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)", },
Marek Vasutd85e3112015-07-21 16:10:13 +0200191};
192
Dinh Nguyenbd48c062015-08-01 03:42:10 +0200193static const struct {
194 const u16 pn;
195 const char *name;
196 const char *var;
197} const socfpga_fpga_model[] = {
198 /* Cyclone V E */
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" },
215 /* Arria V */
216 { 0x2d03, "Arria V, D5", "av_d5" },
217};
218
219static int socfpga_fpga_id(const bool print_id)
220{
221 const u32 altera_mi = 0x6e;
222 const u32 id = scan_mgr_get_fpga_id();
223
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;
228 int i;
229
230 if ((mi != altera_mi) || (lsb != 1)) {
231 printf("FPGA: Not Altera chip ID\n");
232 return -EINVAL;
233 }
234
235 for (i = 0; i < ARRAY_SIZE(socfpga_fpga_model); i++)
236 if (pn == socfpga_fpga_model[i].pn)
237 break;
238
239 if (i == ARRAY_SIZE(socfpga_fpga_model)) {
240 printf("FPGA: Unknown Altera chip, ID 0x%08x\n", id);
241 return -EINVAL;
242 }
243
244 if (print_id)
245 printf("FPGA: Altera %s, version 0x%01x\n",
246 socfpga_fpga_model[i].name, version);
247 return i;
248}
249
Chin Liang See23f23f22014-06-10 02:23:45 -0500250/*
251 * Print CPU information
252 */
Marek Vasut9ec74142015-07-22 05:40:12 +0200253#if defined(CONFIG_DISPLAY_CPUINFO)
Chin Liang See23f23f22014-06-10 02:23:45 -0500254int print_cpuinfo(void)
255{
Marek Vasutd85e3112015-07-21 16:10:13 +0200256 const u32 bsel = readl(&sysmgr_regs->bootinfo) & 0x7;
Pavel Machekd5a3d3c2014-09-08 14:08:45 +0200257 puts("CPU: Altera SoCFPGA Platform\n");
Dinh Nguyenbd48c062015-08-01 03:42:10 +0200258 socfpga_fpga_id(1);
Marek Vasut9ec74142015-07-22 05:40:12 +0200259 printf("BOOT: %s\n", bsel_str[bsel].name);
260 return 0;
261}
262#endif
263
264#ifdef CONFIG_ARCH_MISC_INIT
265int arch_misc_init(void)
266{
267 const u32 bsel = readl(&sysmgr_regs->bootinfo) & 0x7;
Dinh Nguyenbd48c062015-08-01 03:42:10 +0200268 const int fpga_id = socfpga_fpga_id(0);
Marek Vasut9ec74142015-07-22 05:40:12 +0200269 setenv("bootmode", bsel_str[bsel].mode);
Dinh Nguyenbd48c062015-08-01 03:42:10 +0200270 if (fpga_id >= 0)
271 setenv("fpgatype", socfpga_fpga_model[fpga_id].var);
Marek Vasute6e34ca2016-02-11 16:36:43 +0100272 return socfpga_eth_reset();
Chin Liang See23f23f22014-06-10 02:23:45 -0500273}
274#endif
275
276#if defined(CONFIG_SYS_CONSOLE_IS_IN_ENV) && \
277defined(CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE)
278int overwrite_console(void)
279{
280 return 0;
281}
282#endif
283
Pavel Machek230fe9b2014-09-08 14:08:45 +0200284#ifdef CONFIG_FPGA
285/*
286 * FPGA programming support for SoC FPGA Cyclone V
287 */
288static Altera_desc altera_fpga[] = {
289 {
290 /* Family */
291 Altera_SoCFPGA,
292 /* Interface type */
293 fast_passive_parallel,
294 /* No limitation as additional data will be ignored */
295 -1,
296 /* No device function table */
297 NULL,
298 /* Base interface address specified in driver */
299 NULL,
300 /* No cookie implementation */
301 0
302 },
303};
304
305/* add device descriptor to FPGA device table */
306static void socfpga_fpga_add(void)
307{
308 int i;
309 fpga_init();
310 for (i = 0; i < ARRAY_SIZE(altera_fpga); i++)
311 fpga_add(fpga_altera, &altera_fpga[i]);
312}
313#else
314static inline void socfpga_fpga_add(void) {}
315#endif
316
Pavel Machekde6da922014-09-09 14:03:28 +0200317int arch_cpu_init(void)
318{
Stefan Roesed0e932d2014-12-19 13:49:10 +0100319#ifdef CONFIG_HW_WATCHDOG
320 /*
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
324 * booting.
325 */
326 hw_watchdog_init();
327#else
Pavel Machekde6da922014-09-09 14:03:28 +0200328 /*
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.
Marek Vasuta71df7a2015-07-09 02:51:56 +0200332 * Toggle watchdog reset, so watchdog in not running state.
Pavel Machekde6da922014-09-09 14:03:28 +0200333 */
Marek Vasuta71df7a2015-07-09 02:51:56 +0200334 socfpga_per_reset(SOCFPGA_RESET(L4WD0), 1);
335 socfpga_per_reset(SOCFPGA_RESET(L4WD0), 0);
Pavel Machekde6da922014-09-09 14:03:28 +0200336#endif
Stefan Roesed0e932d2014-12-19 13:49:10 +0100337
Pavel Machekde6da922014-09-09 14:03:28 +0200338 return 0;
339}
340
Pavel Machek13e81d42014-09-08 14:08:45 +0200341/*
342 * Convert all NIC-301 AMBA slaves from secure to non-secure
343 */
344static void socfpga_nic301_slave_ns(void)
345{
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);
352}
353
Marek Vasut7249faf2014-09-08 14:08:45 +0200354static uint32_t iswgrp_handoff[8];
355
Marek Vasutfc520892014-10-18 03:52:36 +0200356int arch_early_init_r(void)
Chin Liang See23f23f22014-06-10 02:23:45 -0500357{
Marek Vasut7249faf2014-09-08 14:08:45 +0200358 int i;
Marek Vasutef848612015-07-12 15:11:03 +0200359
360 /*
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 reseting the CPU.
366 */
367 writel(0xae9efebc, &sysmgr_regs->romcodegrp_warmramgrp_enable);
368
Marek Vasut7249faf2014-09-08 14:08:45 +0200369 for (i = 0; i < 8; i++) /* Cache initial SW setting regs */
370 iswgrp_handoff[i] = readl(&sysmgr_regs->iswgrp_handoff[i]);
371
Pavel Machek13e81d42014-09-08 14:08:45 +0200372 socfpga_bridges_reset(1);
373 socfpga_nic301_slave_ns();
374
375 /*
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)
379 */
380 setbits_le32(&scu_regs->sacr, 0xfff);
381
Marek Vasut60d804c2014-09-15 03:58:22 +0200382 /* Configure the L2 controller to make SDRAM start at 0 */
383#ifdef CONFIG_SOCFPGA_VIRTUAL_TARGET
384 writel(0x2, &nic301_regs->remap);
385#else
386 writel(0x1, &nic301_regs->remap); /* remap.mpuzero */
387 writel(0x1, &pl310->pl310_addr_filter_start);
388#endif
389
Pavel Machek230fe9b2014-09-08 14:08:45 +0200390 /* Add device descriptor to FPGA device table */
391 socfpga_fpga_add();
Stefan Roesea877bec2014-11-07 13:50:30 +0100392
393#ifdef CONFIG_DESIGNWARE_SPI
394 /* Get Designware SPI controller out of reset */
Marek Vasuta71df7a2015-07-09 02:51:56 +0200395 socfpga_per_reset(SOCFPGA_RESET(SPIM0), 0);
396 socfpga_per_reset(SOCFPGA_RESET(SPIM1), 0);
Stefan Roesea877bec2014-11-07 13:50:30 +0100397#endif
398
Marek Vasut8f7ed082015-12-20 04:00:43 +0100399#ifdef CONFIG_NAND_DENALI
400 socfpga_per_reset(SOCFPGA_RESET(NAND), 0);
401#endif
402
Chin Liang See23f23f22014-06-10 02:23:45 -0500403 return 0;
404}
Marek Vasut7249faf2014-09-08 14:08:45 +0200405
406static void socfpga_sdram_apply_static_cfg(void)
407{
408 const uint32_t staticcfg = SOCFPGA_SDR_ADDRESS + 0x505c;
409 const uint32_t applymask = 0x8;
410 uint32_t val = readl(staticcfg) | applymask;
411
412 /*
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.
420 *
421 * The code below uses 7 instructions, while the Cortex-A9 has
422 * 32-byte cachelines, thus the limit is 8 instructions total.
423 */
424 asm volatile(
425 ".align 5 \n"
426 " b 2f \n"
427 "1: str %0, [%1] \n"
428 " dsb \n"
429 " isb \n"
430 " b 3f \n"
431 "2: b 1b \n"
432 "3: nop \n"
433 : : "r"(val), "r"(staticcfg) : "memory", "cc");
434}
435
436int do_bridge(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
437{
438 if (argc != 2)
439 return CMD_RET_USAGE;
440
441 argv++;
442
443 switch (*argv[0]) {
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);
450 break;
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);
457 break;
458 default:
459 return CMD_RET_USAGE;
460 }
461
462 return 0;
463}
464
465U_BOOT_CMD(
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"
470 ""
471);