blob: e6331c0e4d81c313f73e43ff63f45efdad6089c1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek04b7e622015-01-15 10:01:51 +01002/*
3 * (C) Copyright 2014 - 2015 Xilinx, Inc.
Michal Simeka8c94362023-07-10 14:35:49 +02004 * Michal Simek <michal.simek@amd.com>
Michal Simek04b7e622015-01-15 10:01:51 +01005 */
6
Tom Rinidec7ea02024-05-20 13:35:03 -06007#include <config.h>
Simon Glassed38aef2020-05-10 11:40:03 -06008#include <command.h>
Simon Glass370382c2019-11-14 12:57:35 -07009#include <cpu_func.h>
Michal Simek09a7d7d2020-01-07 09:02:52 +010010#include <debug_uart.h>
Michal Simekcfb37602021-07-27 16:19:18 +020011#include <dfu.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060012#include <env.h>
Michal Simek8d4a8d42020-07-30 13:37:49 +020013#include <env_internal.h>
Simon Glassa7b51302019-11-14 12:57:46 -070014#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060016#include <net.h>
Michal Simekd54b1af2015-09-30 17:26:55 +020017#include <sata.h>
Michal Simekb216cc12015-07-23 13:27:40 +020018#include <ahci.h>
19#include <scsi.h>
Stefan Herbrechtsmeier3ff3bd42022-06-20 18:36:43 +020020#include <soc.h>
Venkatesh Yadav Abbarapuad11fa42024-02-07 14:03:28 +053021#include <spl.h>
Michal Simekecfb6dc2016-04-22 14:28:54 +020022#include <malloc.h>
Michal Simekcfb37602021-07-27 16:19:18 +020023#include <memalign.h>
Michal Simekbf0f9ca2018-04-19 15:43:38 +020024#include <wdt.h>
Michal Simekc23d3f82015-11-05 08:34:35 +010025#include <asm/arch/clk.h>
Michal Simek04b7e622015-01-15 10:01:51 +010026#include <asm/arch/hardware.h>
27#include <asm/arch/sys_proto.h>
Michal Simekf2f08642018-01-10 09:36:09 +010028#include <asm/arch/psu_init_gpl.h>
Simon Glass274e0b02020-05-10 11:39:56 -060029#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060030#include <asm/global_data.h>
Michal Simek04b7e622015-01-15 10:01:51 +010031#include <asm/io.h>
Simon Glass6b9f0102020-05-10 11:40:06 -060032#include <asm/ptrace.h>
Michal Simekf183a982018-04-25 11:20:43 +020033#include <dm/device.h>
Michal Simekbf0f9ca2018-04-19 15:43:38 +020034#include <dm/uclass.h>
Siva Durga Prasad Paladuguba1f68e2015-08-04 13:03:26 +053035#include <usb.h>
36#include <dwc3-uboot.h>
Michal Simek8111aff2016-02-01 15:05:58 +010037#include <zynqmppl.h>
Ibai Erkiagac8a3efa2019-09-27 11:37:01 +010038#include <zynqmp_firmware.h>
Michal Simek76d0a772016-09-01 11:16:40 +020039#include <g_dnl.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060040#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060041#include <linux/delay.h>
42#include <linux/sizes.h>
Michal Simek705d44a2020-03-31 12:39:37 +020043#include "../common/board.h"
Michal Simek04b7e622015-01-15 10:01:51 +010044
Luca Ceresoli23e65002019-05-21 18:06:43 +020045#include "pm_cfg_obj.h"
46
Michal Simek04b7e622015-01-15 10:01:51 +010047DECLARE_GLOBAL_DATA_PTR;
48
Michal Simek1aab1142020-09-09 14:41:56 +020049#if CONFIG_IS_ENABLED(FPGA) && defined(CONFIG_FPGA_ZYNQMPPL)
Oleksandr Suvorovdae95a42022-07-22 17:16:04 +030050static xilinx_desc zynqmppl = {
51 xilinx_zynqmp, csu_dma, 1, &zynqmp_op, 0, &zynqmp_op, NULL,
52 ZYNQMP_FPGA_FLAGS
53};
Michal Simek8111aff2016-02-01 15:05:58 +010054#endif
55
Michal Simeke5710e32022-02-17 14:28:42 +010056int __maybe_unused psu_uboot_init(void)
Michal Simek8b353302017-02-07 14:32:26 +010057{
Michal Simek09a7d7d2020-01-07 09:02:52 +010058 int ret;
59
Michal Simekc8785f22018-01-10 11:48:48 +010060 ret = psu_init();
Michal Simek09a7d7d2020-01-07 09:02:52 +010061 if (ret)
62 return ret;
Michal Simek1f55e572020-03-20 08:59:02 +010063
Adrian Fiergolski8e87ecf2021-06-08 12:37:23 +020064 /*
65 * PS_SYSMON_ANALOG_BUS register determines mapping between SysMon
66 * supply sense channel to SysMon supply registers inside the IP.
67 * This register must be programmed to complete SysMon IP
68 * configuration. The default register configuration after
69 * power-up is incorrect. Hence, fix this by writing the
70 * correct value - 0x3210.
71 */
72 writel(ZYNQMP_PS_SYSMON_ANALOG_BUS_VAL,
73 ZYNQMP_AMS_PS_SYSMON_ANALOG_BUS);
74
Michal Simek1f55e572020-03-20 08:59:02 +010075 /* Delay is required for clocks to be propagated */
76 udelay(1000000);
Michal Simeke5710e32022-02-17 14:28:42 +010077
78 return 0;
79}
Michal Simeke0f36102017-07-12 13:08:41 +020080
Michal Simeke5710e32022-02-17 14:28:42 +010081#if !defined(CONFIG_SPL_BUILD)
82# if defined(CONFIG_DEBUG_UART_BOARD_INIT)
83void board_debug_uart_init(void)
84{
85# if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
86 psu_uboot_init();
87# endif
88}
89# endif
Michal Simek09a7d7d2020-01-07 09:02:52 +010090
Michal Simeke5710e32022-02-17 14:28:42 +010091# if defined(CONFIG_BOARD_EARLY_INIT_F)
92int board_early_init_f(void)
93{
94 int ret = 0;
95# if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED) && !defined(CONFIG_DEBUG_UART_BOARD_INIT)
96 ret = psu_uboot_init();
97# endif
98 return ret;
Michal Simek8b353302017-02-07 14:32:26 +010099}
Michal Simeke5710e32022-02-17 14:28:42 +0100100# endif
Michal Simekba6fb832022-02-17 14:28:40 +0100101#endif
Michal Simek8b353302017-02-07 14:32:26 +0100102
Michal Simek46900462020-02-11 12:43:14 +0100103static int multi_boot(void)
104{
Michal Simek6aca2832021-07-27 16:17:31 +0200105 u32 multiboot = 0;
106 int ret;
Michal Simek46900462020-02-11 12:43:14 +0100107
Michal Simek6aca2832021-07-27 16:17:31 +0200108 ret = zynqmp_mmio_read((ulong)&csu_base->multi_boot, &multiboot);
109 if (ret)
110 return -EINVAL;
Michal Simek46900462020-02-11 12:43:14 +0100111
Michal Simek21e5c322021-07-27 14:05:27 +0200112 return multiboot;
Michal Simek46900462020-02-11 12:43:14 +0100113}
114
Jorge Ramirez-Ortiz34deca52021-10-13 15:48:00 +0200115#if defined(CONFIG_SPL_BUILD)
116static void restore_jtag(void)
117{
118 if (current_el() != 3)
119 return;
120
121 writel(CSU_JTAG_SEC_GATE_DISABLE, &csu_base->jtag_sec);
122 writel(CSU_JTAG_DAP_ENABLE_DEBUG, &csu_base->jtag_dap_cfg);
123 writel(CSU_JTAG_CHAIN_WR_SETUP, &csu_base->jtag_chain_status_wr);
124 writel(CRLAPB_DBG_LPD_CTRL_SETUP_CLK, &crlapb_base->dbg_lpd_ctrl);
125 writel(CRLAPB_RST_LPD_DBG_RESET, &crlapb_base->rst_lpd_dbg);
126 writel(CSU_PCAP_PROG_RELEASE_PL, &csu_base->pcap_prog);
127}
128#endif
129
Jorge Ramirez-Ortizb0e388a2021-10-13 19:04:47 +0200130static void print_secure_boot(void)
131{
132 u32 status = 0;
133
134 if (zynqmp_mmio_read((ulong)&csu_base->status, &status))
135 return;
136
137 printf("Secure Boot:\t%sauthenticated, %sencrypted\n",
138 status & ZYNQMP_CSU_STATUS_AUTHENTICATED ? "" : "not ",
139 status & ZYNQMP_CSU_STATUS_ENCRYPTED ? "" : "not ");
140}
141
Michal Simek04b7e622015-01-15 10:01:51 +0100142int board_init(void)
143{
Stefan Herbrechtsmeier3ff3bd42022-06-20 18:36:43 +0200144#if CONFIG_IS_ENABLED(FPGA) && defined(CONFIG_FPGA_ZYNQMPPL)
145 struct udevice *soc;
146 char name[SOC_MAX_STR_SIZE];
147 int ret;
148#endif
Michal Simek3d49c952022-10-05 11:39:27 +0200149
150#if defined(CONFIG_SPL_BUILD)
151 /* Check *at build time* if the filename is an non-empty string */
152 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
153 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
154 zynqmp_pm_cfg_obj_size);
Ibai Erkiaga2d9b95b2019-09-27 11:37:04 +0100155
Michal Simekae9dc112021-02-02 16:34:48 +0100156 printf("Silicon version:\t%d\n", zynqmp_get_silicon_version());
Jorge Ramirez-Ortiz34deca52021-10-13 15:48:00 +0200157
158 /* the CSU disables the JTAG interface when secure boot is enabled */
Ricardo Salveti5b774f02021-11-04 16:28:02 -0300159 if (CONFIG_IS_ENABLED(ZYNQMP_RESTORE_JTAG))
Jorge Ramirez-Ortiz34deca52021-10-13 15:48:00 +0200160 restore_jtag();
Michal Simek394ee242020-08-03 13:01:45 +0200161#else
162 if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM))
163 xilinx_read_eeprom();
Luca Ceresoli23e65002019-05-21 18:06:43 +0200164#endif
165
Michal Simekfb7242d2015-06-22 14:31:06 +0200166 printf("EL Level:\tEL%d\n", current_el());
167
Michal Simek1aab1142020-09-09 14:41:56 +0200168#if CONFIG_IS_ENABLED(FPGA) && defined(CONFIG_FPGA_ZYNQMPPL)
Stefan Herbrechtsmeier3ff3bd42022-06-20 18:36:43 +0200169 ret = soc_get(&soc);
170 if (!ret) {
171 ret = soc_get_machine(soc, name, sizeof(name));
172 if (ret >= 0) {
173 zynqmppl.name = strdup(name);
174 fpga_init();
175 fpga_add(fpga_xilinx, &zynqmppl);
176 }
177 }
Michal Simekbf0f9ca2018-04-19 15:43:38 +0200178#endif
179
Jorge Ramirez-Ortizb0e388a2021-10-13 19:04:47 +0200180 /* display secure boot information */
181 print_secure_boot();
Michal Simek46900462020-02-11 12:43:14 +0100182 if (current_el() == 3)
Michal Simek21e5c322021-07-27 14:05:27 +0200183 printf("Multiboot:\t%d\n", multi_boot());
Michal Simek46900462020-02-11 12:43:14 +0100184
Michal Simek04b7e622015-01-15 10:01:51 +0100185 return 0;
186}
187
188int board_early_init_r(void)
189{
190 u32 val;
191
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530192 if (current_el() != 3)
193 return 0;
194
Michal Simek245d5282017-07-12 10:32:18 +0200195 val = readl(&crlapb_base->timestamp_ref_ctrl);
196 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
197
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530198 if (!val) {
Michal Simekc23d3f82015-11-05 08:34:35 +0100199 val = readl(&crlapb_base->timestamp_ref_ctrl);
200 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
201 writel(val, &crlapb_base->timestamp_ref_ctrl);
Michal Simek04b7e622015-01-15 10:01:51 +0100202
Michal Simekc23d3f82015-11-05 08:34:35 +0100203 /* Program freq register in System counter */
204 writel(zynqmp_get_system_timer_freq(),
205 &iou_scntr_secure->base_frequency_id_register);
206 /* And enable system counter */
207 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
208 &iou_scntr_secure->counter_control_register);
209 }
Michal Simek04b7e622015-01-15 10:01:51 +0100210 return 0;
211}
212
Nitin Jainb2eb59b2018-02-16 12:56:17 +0530213unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
Simon Glassed38aef2020-05-10 11:40:03 -0600214 char *const argv[])
Nitin Jainb2eb59b2018-02-16 12:56:17 +0530215{
216 int ret = 0;
217
218 if (current_el() > 1) {
219 smp_kick_all_cpus();
220 dcache_disable();
221 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
222 ES_TO_AARCH64);
223 } else {
224 printf("FAIL: current EL is not above EL1\n");
225 ret = EINVAL;
226 }
227 return ret;
228}
229
Tom Rinibb4dd962022-11-16 13:10:37 -0500230#if !defined(CFG_SYS_SDRAM_BASE) && !defined(CFG_SYS_SDRAM_SIZE)
Simon Glass2f949c32017-03-31 08:40:32 -0600231int dram_init_banksize(void)
Michal Simek8faa66a2016-02-08 09:34:53 +0100232{
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530233 int ret;
234
235 ret = fdtdec_setup_memory_banksize();
236 if (ret)
237 return ret;
238
239 mem_map_fill();
240
241 return 0;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500242}
Michal Simek8faa66a2016-02-08 09:34:53 +0100243
Tom Riniedcfdbd2016-12-09 07:56:54 -0500244int dram_init(void)
245{
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530246 if (fdtdec_setup_mem_size_base() != 0)
Nathan Rossiac04bfa2016-12-19 00:03:34 +1000247 return -EINVAL;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500248
249 return 0;
Michal Simek8faa66a2016-02-08 09:34:53 +0100250}
Michal Simek97ab9612021-05-31 11:03:19 +0200251
Michal Simek8faa66a2016-02-08 09:34:53 +0100252#else
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530253int dram_init_banksize(void)
254{
Tom Rinibb4dd962022-11-16 13:10:37 -0500255 gd->bd->bi_dram[0].start = CFG_SYS_SDRAM_BASE;
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530256 gd->bd->bi_dram[0].size = get_effective_memsize();
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530257
258 mem_map_fill();
259
260 return 0;
261}
262
Michal Simek04b7e622015-01-15 10:01:51 +0100263int dram_init(void)
264{
Tom Rinibb4dd962022-11-16 13:10:37 -0500265 gd->ram_size = get_ram_size((void *)CFG_SYS_SDRAM_BASE,
266 CFG_SYS_SDRAM_SIZE);
Michal Simek04b7e622015-01-15 10:01:51 +0100267
268 return 0;
269}
Michal Simek8faa66a2016-02-08 09:34:53 +0100270#endif
Michal Simek04b7e622015-01-15 10:01:51 +0100271
Michal Simek2a220332021-07-13 16:39:26 +0200272#if !CONFIG_IS_ENABLED(SYSRESET)
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100273void reset_cpu(void)
Michal Simek04b7e622015-01-15 10:01:51 +0100274{
Lukas Funke45f61df2024-06-07 11:26:08 +0200275 if (!IS_ENABLED(CONFIG_ZYNQMP_FIRMWARE)) {
276 log_warning("reset failed: ZYNQMP_FIRMWARE disabled");
277 return;
278 }
279
280 /* In case of !CONFIG_ZYNQMP_FIRMWARE the call to 'xilinx_pm_request()'
281 * will be removed by the compiler due to the early return.
282 * If CONFIG_ZYNQMP_FIRMWARE is defined in SPL 'xilinx_pm_request()'
283 * will send command over IPI and requires pmufw to be present.
284 */
285 xilinx_pm_request(PM_RESET_ASSERT, ZYNQMP_PM_RESET_SOFT,
286 PM_RESET_ACTION_ASSERT, 0, 0, NULL);
Michal Simek04b7e622015-01-15 10:01:51 +0100287}
Michal Simek2a220332021-07-13 16:39:26 +0200288#endif
Michal Simek04b7e622015-01-15 10:01:51 +0100289
Michal Simek8ec30042020-08-20 10:54:45 +0200290static u8 __maybe_unused zynqmp_get_bootmode(void)
291{
292 u8 bootmode;
293 u32 reg = 0;
294 int ret;
295
296 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, &reg);
297 if (ret)
298 return -EINVAL;
299
Michal Simek58cc08c2021-07-28 12:25:49 +0200300 debug("HW boot mode: %x\n", reg & BOOT_MODES_MASK);
301 debug("ALT boot mode: %x\n", reg >> BOOT_MODE_ALT_SHIFT);
302
Michal Simek8ec30042020-08-20 10:54:45 +0200303 if (reg >> BOOT_MODE_ALT_SHIFT)
304 reg >>= BOOT_MODE_ALT_SHIFT;
305
306 bootmode = reg & BOOT_MODES_MASK;
307
308 return bootmode;
309}
310
Michal Simek342edfe2018-12-20 09:33:38 +0100311#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simek29b9b712018-05-17 14:06:06 +0200312static const struct {
313 u32 bit;
314 const char *name;
315} reset_reasons[] = {
316 { RESET_REASON_DEBUG_SYS, "DEBUG" },
317 { RESET_REASON_SOFT, "SOFT" },
318 { RESET_REASON_SRST, "SRST" },
319 { RESET_REASON_PSONLY, "PS-ONLY" },
320 { RESET_REASON_PMU, "PMU" },
321 { RESET_REASON_INTERNAL, "INTERNAL" },
322 { RESET_REASON_EXTERNAL, "EXTERNAL" },
323 {}
324};
325
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530326static int reset_reason(void)
Michal Simek29b9b712018-05-17 14:06:06 +0200327{
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530328 u32 reg;
329 int i, ret;
Michal Simek29b9b712018-05-17 14:06:06 +0200330 const char *reason = NULL;
331
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530332 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, &reg);
333 if (ret)
334 return -EINVAL;
Michal Simek29b9b712018-05-17 14:06:06 +0200335
336 puts("Reset reason:\t");
337
338 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530339 if (reg & reset_reasons[i].bit) {
Michal Simek29b9b712018-05-17 14:06:06 +0200340 reason = reset_reasons[i].name;
341 printf("%s ", reset_reasons[i].name);
342 break;
343 }
344 }
345
346 puts("\n");
347
348 env_set("reset_reason", reason);
349
Michal Simek0954c8c2021-02-09 08:50:22 +0100350 return 0;
Michal Simek29b9b712018-05-17 14:06:06 +0200351}
352
Michal Simek1ca66d72019-02-14 13:14:30 +0100353static int set_fdtfile(void)
354{
355 char *compatible, *fdtfile;
356 const char *suffix = ".dtb";
357 const char *vendor = "xilinx/";
Igor Lantsmane167bac2020-06-24 14:33:46 +0200358 int fdt_compat_len;
Michal Simek1ca66d72019-02-14 13:14:30 +0100359
360 if (env_get("fdtfile"))
361 return 0;
362
Igor Lantsmane167bac2020-06-24 14:33:46 +0200363 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible",
364 &fdt_compat_len);
365 if (compatible && fdt_compat_len) {
366 char *name;
367
Michal Simek1ca66d72019-02-14 13:14:30 +0100368 debug("Compatible: %s\n", compatible);
369
Igor Lantsmane167bac2020-06-24 14:33:46 +0200370 name = strchr(compatible, ',');
371 if (!name)
372 return -EINVAL;
Michal Simek1ca66d72019-02-14 13:14:30 +0100373
Igor Lantsmane167bac2020-06-24 14:33:46 +0200374 name++;
375
376 fdtfile = calloc(1, strlen(vendor) + strlen(name) +
Michal Simek1ca66d72019-02-14 13:14:30 +0100377 strlen(suffix) + 1);
378 if (!fdtfile)
379 return -ENOMEM;
380
Igor Lantsmane167bac2020-06-24 14:33:46 +0200381 sprintf(fdtfile, "%s%s%s", vendor, name, suffix);
Michal Simek1ca66d72019-02-14 13:14:30 +0100382
383 env_set("fdtfile", fdtfile);
384 free(fdtfile);
385 }
386
387 return 0;
388}
389
Michal Simekb1634762023-09-05 13:30:07 +0200390static int boot_targets_setup(void)
Michal Simek9c91e612020-04-08 11:04:41 +0200391{
Michal Simek04b7e622015-01-15 10:01:51 +0100392 u8 bootmode;
Michal Simekf183a982018-04-25 11:20:43 +0200393 struct udevice *dev;
394 int bootseq = -1;
395 int bootseq_len = 0;
Michal Simek7410b142018-04-25 11:10:34 +0200396 int env_targets_len = 0;
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530397 const char *mode = NULL;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200398 char *new_targets;
Siva Durga Prasad Paladugu245c5562017-12-20 16:35:06 +0530399 char *env_targets;
Michal Simek7cb4cca2021-10-25 10:10:52 +0200400
Michal Simek9c91e612020-04-08 11:04:41 +0200401 bootmode = zynqmp_get_bootmode();
Michal Simek04b7e622015-01-15 10:01:51 +0100402
Michal Simekc5d95232015-09-20 17:20:42 +0200403 puts("Bootmode: ");
Michal Simek04b7e622015-01-15 10:01:51 +0100404 switch (bootmode) {
Michal Simek12398ea2016-08-19 14:14:52 +0200405 case USB_MODE:
406 puts("USB_MODE\n");
T Karthik Reddy9eee8e32021-03-24 23:37:57 -0600407 mode = "usb_dfu0 usb_dfu1";
Michal Simek43380352017-12-01 15:18:24 +0100408 env_set("modeboot", "usb_dfu_spl");
Michal Simek12398ea2016-08-19 14:14:52 +0200409 break;
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530410 case JTAG_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200411 puts("JTAG_MODE\n");
Siva Durga Prasad Paladugu9c441702019-06-25 17:41:09 +0530412 mode = "jtag pxe dhcp";
Michal Simek43380352017-12-01 15:18:24 +0100413 env_set("modeboot", "jtagboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530414 break;
415 case QSPI_MODE_24BIT:
416 case QSPI_MODE_32BIT:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200417 mode = "qspi0";
Michal Simekc5d95232015-09-20 17:20:42 +0200418 puts("QSPI_MODE\n");
Michal Simek43380352017-12-01 15:18:24 +0100419 env_set("modeboot", "qspiboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530420 break;
Michal Simek02d66cd2015-04-15 15:02:28 +0200421 case EMMC_MODE:
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200422 puts("EMMC_MODE\n");
T Karthik Reddy19735c32019-12-17 06:41:42 -0700423 if (uclass_get_device_by_name(UCLASS_MMC,
424 "mmc@ff160000", &dev) &&
425 uclass_get_device_by_name(UCLASS_MMC,
426 "sdhci@ff160000", &dev)) {
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530427 debug("SD0 driver for SD0 device is not present\n");
428 break;
T Karthik Reddy19735c32019-12-17 06:41:42 -0700429 }
Simon Glass75e534b2020-12-16 21:20:07 -0700430 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
T Karthik Reddy19735c32019-12-17 06:41:42 -0700431
432 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700433 bootseq = dev_seq(dev);
Ashok Reddy Somaa10be052021-09-15 08:52:17 +0200434 env_set("modeboot", "emmcboot");
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200435 break;
436 case SD_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200437 puts("SD_MODE\n");
Michal Simekf183a982018-04-25 11:20:43 +0200438 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530439 "mmc@ff160000", &dev) &&
440 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200441 "sdhci@ff160000", &dev)) {
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530442 debug("SD0 driver for SD0 device is not present\n");
443 break;
Michal Simekf183a982018-04-25 11:20:43 +0200444 }
Simon Glass75e534b2020-12-16 21:20:07 -0700445 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
Michal Simekf183a982018-04-25 11:20:43 +0200446
447 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700448 bootseq = dev_seq(dev);
Michal Simek43380352017-12-01 15:18:24 +0100449 env_set("modeboot", "sdboot");
Michal Simek04b7e622015-01-15 10:01:51 +0100450 break;
Siva Durga Prasad Paladugu29a77d22016-09-21 11:45:05 +0530451 case SD1_LSHFT_MODE:
452 puts("LVL_SHFT_");
Michal Simek293f47b2021-10-18 13:30:04 +0200453 fallthrough;
Michal Simek108e1842015-10-05 10:51:12 +0200454 case SD_MODE1:
Michal Simekc5d95232015-09-20 17:20:42 +0200455 puts("SD_MODE1\n");
Michal Simekf183a982018-04-25 11:20:43 +0200456 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530457 "mmc@ff170000", &dev) &&
458 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200459 "sdhci@ff170000", &dev)) {
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530460 debug("SD1 driver for SD1 device is not present\n");
461 break;
Michal Simekf183a982018-04-25 11:20:43 +0200462 }
Simon Glass75e534b2020-12-16 21:20:07 -0700463 debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev));
Michal Simekf183a982018-04-25 11:20:43 +0200464
465 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700466 bootseq = dev_seq(dev);
Michal Simek43380352017-12-01 15:18:24 +0100467 env_set("modeboot", "sdboot");
Michal Simek108e1842015-10-05 10:51:12 +0200468 break;
469 case NAND_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200470 puts("NAND_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200471 mode = "nand0";
Michal Simek43380352017-12-01 15:18:24 +0100472 env_set("modeboot", "nandboot");
Michal Simek108e1842015-10-05 10:51:12 +0200473 break;
Michal Simek04b7e622015-01-15 10:01:51 +0100474 default:
475 printf("Invalid Boot Mode:0x%x\n", bootmode);
476 break;
477 }
478
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530479 if (mode) {
480 if (bootseq >= 0) {
481 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
482 debug("Bootseq len: %x\n", bootseq_len);
483 env_set_hex("bootseq", bootseq);
484 }
Michal Simekf183a982018-04-25 11:20:43 +0200485
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530486 /*
487 * One terminating char + one byte for space between mode
488 * and default boot_targets
489 */
490 env_targets = env_get("boot_targets");
491 if (env_targets)
492 env_targets_len = strlen(env_targets);
Michal Simek7410b142018-04-25 11:10:34 +0200493
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530494 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
495 bootseq_len);
496 if (!new_targets)
497 return -ENOMEM;
Michal Simek7410b142018-04-25 11:10:34 +0200498
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530499 if (bootseq >= 0)
500 sprintf(new_targets, "%s%x %s", mode, bootseq,
501 env_targets ? env_targets : "");
502 else
503 sprintf(new_targets, "%s %s", mode,
504 env_targets ? env_targets : "");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200505
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530506 env_set("boot_targets", new_targets);
507 free(new_targets);
508 }
Michal Simekecfb6dc2016-04-22 14:28:54 +0200509
Michal Simekb1634762023-09-05 13:30:07 +0200510 return 0;
511}
512
513int board_late_init(void)
514{
515 int ret, multiboot;
516
517#if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
518 usb_ether_init();
519#endif
520
Kory Maincent9f894932024-05-29 12:01:06 +0200521 multiboot = multi_boot();
522 if (multiboot >= 0)
523 env_set_hex("multiboot", multiboot);
524
Michal Simekb1634762023-09-05 13:30:07 +0200525 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
526 debug("Saved variables - Skipping\n");
527 return 0;
528 }
529
530 if (!IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG))
531 return 0;
532
533 ret = set_fdtfile();
534 if (ret)
535 return ret;
536
Michal Simekb1634762023-09-05 13:30:07 +0200537 if (IS_ENABLED(CONFIG_DISTRO_DEFAULTS)) {
538 ret = boot_targets_setup();
539 if (ret)
540 return ret;
541 }
542
Michal Simek29b9b712018-05-17 14:06:06 +0200543 reset_reason();
544
Michal Simek705d44a2020-03-31 12:39:37 +0200545 return board_late_init_xilinx();
Michal Simek04b7e622015-01-15 10:01:51 +0100546}
Michal Simek342edfe2018-12-20 09:33:38 +0100547#endif
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530548
549int checkboard(void)
550{
Michal Simek47ce9362016-01-25 11:04:21 +0100551 puts("Board: Xilinx ZynqMP\n");
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530552 return 0;
553}
Michal Simek8d4a8d42020-07-30 13:37:49 +0200554
Michal Simeke0026bf2021-05-19 15:16:19 +0200555int mmc_get_env_dev(void)
556{
557 struct udevice *dev;
558 int bootseq = 0;
559
560 switch (zynqmp_get_bootmode()) {
561 case EMMC_MODE:
562 case SD_MODE:
563 if (uclass_get_device_by_name(UCLASS_MMC,
564 "mmc@ff160000", &dev) &&
565 uclass_get_device_by_name(UCLASS_MMC,
566 "sdhci@ff160000", &dev)) {
567 return -1;
568 }
569 bootseq = dev_seq(dev);
570 break;
571 case SD1_LSHFT_MODE:
572 case SD_MODE1:
573 if (uclass_get_device_by_name(UCLASS_MMC,
574 "mmc@ff170000", &dev) &&
575 uclass_get_device_by_name(UCLASS_MMC,
576 "sdhci@ff170000", &dev)) {
577 return -1;
578 }
579 bootseq = dev_seq(dev);
580 break;
581 default:
582 break;
583 }
584
585 debug("bootseq %d\n", bootseq);
586
587 return bootseq;
588}
589
Michal Simekf3a541f2024-03-22 12:43:17 +0100590#if defined(CONFIG_ENV_IS_NOWHERE)
Michal Simek8d4a8d42020-07-30 13:37:49 +0200591enum env_location env_get_location(enum env_operation op, int prio)
592{
593 u32 bootmode = zynqmp_get_bootmode();
594
595 if (prio)
596 return ENVL_UNKNOWN;
597
598 switch (bootmode) {
599 case EMMC_MODE:
600 case SD_MODE:
601 case SD1_LSHFT_MODE:
602 case SD_MODE1:
603 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
604 return ENVL_FAT;
605 if (IS_ENABLED(CONFIG_ENV_IS_IN_EXT4))
606 return ENVL_EXT4;
Mike Looijmans682cf082021-07-02 10:28:36 +0200607 return ENVL_NOWHERE;
Michal Simek8d4a8d42020-07-30 13:37:49 +0200608 case NAND_MODE:
609 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
610 return ENVL_NAND;
611 if (IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
612 return ENVL_UBI;
Mike Looijmans682cf082021-07-02 10:28:36 +0200613 return ENVL_NOWHERE;
Michal Simek8d4a8d42020-07-30 13:37:49 +0200614 case QSPI_MODE_24BIT:
615 case QSPI_MODE_32BIT:
616 if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
617 return ENVL_SPI_FLASH;
Mike Looijmans682cf082021-07-02 10:28:36 +0200618 return ENVL_NOWHERE;
Michal Simek8d4a8d42020-07-30 13:37:49 +0200619 case JTAG_MODE:
620 default:
621 return ENVL_NOWHERE;
622 }
623}
Michal Simekf3a541f2024-03-22 12:43:17 +0100624#endif
Michal Simekcfb37602021-07-27 16:19:18 +0200625
626#if defined(CONFIG_SET_DFU_ALT_INFO)
627
628#define DFU_ALT_BUF_LEN SZ_1K
629
Michal Simek733cd9e2024-03-22 13:09:19 +0100630static void mtd_found_part(u32 *base, u32 *size)
631{
632 struct mtd_info *part, *mtd;
633
634 mtd_probe_devices();
635
636 mtd = get_mtd_device_nm("nor0");
637 if (!IS_ERR_OR_NULL(mtd)) {
638 list_for_each_entry(part, &mtd->partitions, node) {
639 debug("0x%012llx-0x%012llx : \"%s\"\n",
640 part->offset, part->offset + part->size,
641 part->name);
642
643 if (*base >= part->offset &&
644 *base < part->offset + part->size) {
645 debug("Found my partition: %d/%s\n",
646 part->index, part->name);
647 *base = part->offset;
648 *size = part->size;
649 break;
650 }
651 }
652 }
653}
654
Michal Simekcfb37602021-07-27 16:19:18 +0200655void set_dfu_alt_info(char *interface, char *devstr)
656{
Michal Simek9fced422022-12-02 14:06:15 +0100657 int multiboot, bootseq = 0, len = 0;
Michal Simekcfb37602021-07-27 16:19:18 +0200658
659 ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN);
660
Michal Simekf0d6f462022-08-09 16:32:52 +0200661 if (env_get("dfu_alt_info"))
Michal Simekcfb37602021-07-27 16:19:18 +0200662 return;
663
664 memset(buf, 0, sizeof(buf));
665
666 multiboot = multi_boot();
Michal Simek7cb4cca2021-10-25 10:10:52 +0200667 if (multiboot < 0)
668 multiboot = 0;
669
670 multiboot = env_get_hex("multiboot", multiboot);
Michal Simekcfb37602021-07-27 16:19:18 +0200671 debug("Multiboot: %d\n", multiboot);
672
673 switch (zynqmp_get_bootmode()) {
674 case EMMC_MODE:
675 case SD_MODE:
676 case SD1_LSHFT_MODE:
677 case SD_MODE1:
678 bootseq = mmc_get_env_dev();
Michal Simek9fced422022-12-02 14:06:15 +0100679
680 len += snprintf(buf + len, DFU_ALT_BUF_LEN, "mmc %d=boot",
681 bootseq);
682
683 if (multiboot)
684 len += snprintf(buf + len, DFU_ALT_BUF_LEN,
685 "%04d", multiboot);
686
687 len += snprintf(buf + len, DFU_ALT_BUF_LEN, ".bin fat %d 1",
688 bootseq);
689#if defined(CONFIG_SPL_FS_LOAD_PAYLOAD_NAME)
Michal Simek64962b62024-03-22 13:09:18 +0100690 if (strlen(CONFIG_SPL_FS_LOAD_PAYLOAD_NAME))
691 len += snprintf(buf + len, DFU_ALT_BUF_LEN,
692 ";%s fat %d 1",
693 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
694 bootseq);
Michal Simek9fced422022-12-02 14:06:15 +0100695#endif
Michal Simekcfb37602021-07-27 16:19:18 +0200696 break;
697 case QSPI_MODE_24BIT:
698 case QSPI_MODE_32BIT:
Michal Simek733cd9e2024-03-22 13:09:19 +0100699 {
700 u32 base = multiboot * SZ_32K;
701 u32 size = 0x1500000;
702 u32 limit = size;
703
704 mtd_found_part(&base, &limit);
705
706#if defined(CONFIG_SYS_SPI_U_BOOT_OFFS)
707 size = limit;
708 limit = CONFIG_SYS_SPI_U_BOOT_OFFS;
709#endif
710
Michal Simek64962b62024-03-22 13:09:18 +0100711 len += snprintf(buf + len, DFU_ALT_BUF_LEN,
Michal Simek733cd9e2024-03-22 13:09:19 +0100712 "sf 0:0=boot.bin raw 0x%x 0x%x",
713 base, limit);
714#if defined(CONFIG_SPL_FS_LOAD_PAYLOAD_NAME) && defined(CONFIG_SYS_SPI_U_BOOT_OFFS)
715 if (strlen(CONFIG_SPL_FS_LOAD_PAYLOAD_NAME))
716 len += snprintf(buf + len, DFU_ALT_BUF_LEN,
717 ";%s raw 0x%x 0x%x",
718 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
719 base + limit, size - limit);
Stefan Herbrechtsmeierfb027cd2022-06-20 18:36:46 +0200720#endif
Michal Simek733cd9e2024-03-22 13:09:19 +0100721 }
Michal Simek9fced422022-12-02 14:06:15 +0100722 break;
Michal Simekcfb37602021-07-27 16:19:18 +0200723 default:
724 return;
725 }
726
727 env_set("dfu_alt_info", buf);
728 puts("DFU alt info setting: done\n");
729}
730#endif
Michal Simek55666ce2023-11-10 13:34:35 +0100731
732#if defined(CONFIG_SPL_SPI_LOAD)
733unsigned int spl_spi_get_uboot_offs(struct spi_flash *flash)
734{
735 u32 offset;
736 int multiboot = multi_boot();
737
738 offset = multiboot * SZ_32K;
739 offset += CONFIG_SYS_SPI_U_BOOT_OFFS;
740
741 log_info("SPI offset:\t0x%x\n", offset);
742
743 return offset;
744}
745#endif