blob: 106c3953e1fca68149267b0d760ccf2b3a04e652 [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.
4 * Michal Simek <michal.simek@xilinx.com>
Michal Simek04b7e622015-01-15 10:01:51 +01005 */
6
7#include <common.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>
Michal Simek97ab9612021-05-31 11:03:19 +020015#include <image.h>
16#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060017#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060018#include <net.h>
Michal Simekd54b1af2015-09-30 17:26:55 +020019#include <sata.h>
Michal Simekb216cc12015-07-23 13:27:40 +020020#include <ahci.h>
21#include <scsi.h>
Stefan Herbrechtsmeier3ff3bd42022-06-20 18:36:43 +020022#include <soc.h>
Michal Simekecfb6dc2016-04-22 14:28:54 +020023#include <malloc.h>
Michal Simekcfb37602021-07-27 16:19:18 +020024#include <memalign.h>
Michal Simekbf0f9ca2018-04-19 15:43:38 +020025#include <wdt.h>
Michal Simekc23d3f82015-11-05 08:34:35 +010026#include <asm/arch/clk.h>
Michal Simek04b7e622015-01-15 10:01:51 +010027#include <asm/arch/hardware.h>
28#include <asm/arch/sys_proto.h>
Michal Simekf2f08642018-01-10 09:36:09 +010029#include <asm/arch/psu_init_gpl.h>
Simon Glass274e0b02020-05-10 11:39:56 -060030#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060031#include <asm/global_data.h>
Michal Simek04b7e622015-01-15 10:01:51 +010032#include <asm/io.h>
Simon Glass6b9f0102020-05-10 11:40:06 -060033#include <asm/ptrace.h>
Michal Simekf183a982018-04-25 11:20:43 +020034#include <dm/device.h>
Michal Simekbf0f9ca2018-04-19 15:43:38 +020035#include <dm/uclass.h>
Siva Durga Prasad Paladuguba1f68e2015-08-04 13:03:26 +053036#include <usb.h>
37#include <dwc3-uboot.h>
Michal Simek8111aff2016-02-01 15:05:58 +010038#include <zynqmppl.h>
Ibai Erkiagac8a3efa2019-09-27 11:37:01 +010039#include <zynqmp_firmware.h>
Michal Simek76d0a772016-09-01 11:16:40 +020040#include <g_dnl.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060041#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060042#include <linux/delay.h>
43#include <linux/sizes.h>
Michal Simek705d44a2020-03-31 12:39:37 +020044#include "../common/board.h"
Michal Simek04b7e622015-01-15 10:01:51 +010045
Luca Ceresoli23e65002019-05-21 18:06:43 +020046#include "pm_cfg_obj.h"
47
Michal Simek04b7e622015-01-15 10:01:51 +010048DECLARE_GLOBAL_DATA_PTR;
49
Michal Simek1aab1142020-09-09 14:41:56 +020050#if CONFIG_IS_ENABLED(FPGA) && defined(CONFIG_FPGA_ZYNQMPPL)
Michal Simek8111aff2016-02-01 15:05:58 +010051static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
Michal Simek8111aff2016-02-01 15:05:58 +010052#endif
53
Michal Simeke5710e32022-02-17 14:28:42 +010054int __maybe_unused psu_uboot_init(void)
Michal Simek8b353302017-02-07 14:32:26 +010055{
Michal Simek09a7d7d2020-01-07 09:02:52 +010056 int ret;
57
Michal Simekc8785f22018-01-10 11:48:48 +010058 ret = psu_init();
Michal Simek09a7d7d2020-01-07 09:02:52 +010059 if (ret)
60 return ret;
Michal Simek1f55e572020-03-20 08:59:02 +010061
Adrian Fiergolski8e87ecf2021-06-08 12:37:23 +020062 /*
63 * PS_SYSMON_ANALOG_BUS register determines mapping between SysMon
64 * supply sense channel to SysMon supply registers inside the IP.
65 * This register must be programmed to complete SysMon IP
66 * configuration. The default register configuration after
67 * power-up is incorrect. Hence, fix this by writing the
68 * correct value - 0x3210.
69 */
70 writel(ZYNQMP_PS_SYSMON_ANALOG_BUS_VAL,
71 ZYNQMP_AMS_PS_SYSMON_ANALOG_BUS);
72
Michal Simek1f55e572020-03-20 08:59:02 +010073 /* Delay is required for clocks to be propagated */
74 udelay(1000000);
Michal Simeke5710e32022-02-17 14:28:42 +010075
76 return 0;
77}
Michal Simeke0f36102017-07-12 13:08:41 +020078
Michal Simeke5710e32022-02-17 14:28:42 +010079#if !defined(CONFIG_SPL_BUILD)
80# if defined(CONFIG_DEBUG_UART_BOARD_INIT)
81void board_debug_uart_init(void)
82{
83# if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
84 psu_uboot_init();
85# endif
86}
87# endif
Michal Simek09a7d7d2020-01-07 09:02:52 +010088
Michal Simeke5710e32022-02-17 14:28:42 +010089# if defined(CONFIG_BOARD_EARLY_INIT_F)
90int board_early_init_f(void)
91{
92 int ret = 0;
93# if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED) && !defined(CONFIG_DEBUG_UART_BOARD_INIT)
94 ret = psu_uboot_init();
95# endif
96 return ret;
Michal Simek8b353302017-02-07 14:32:26 +010097}
Michal Simeke5710e32022-02-17 14:28:42 +010098# endif
Michal Simekba6fb832022-02-17 14:28:40 +010099#endif
Michal Simek8b353302017-02-07 14:32:26 +0100100
Michal Simek46900462020-02-11 12:43:14 +0100101static int multi_boot(void)
102{
Michal Simek6aca2832021-07-27 16:17:31 +0200103 u32 multiboot = 0;
104 int ret;
Michal Simek46900462020-02-11 12:43:14 +0100105
Michal Simek6aca2832021-07-27 16:17:31 +0200106 ret = zynqmp_mmio_read((ulong)&csu_base->multi_boot, &multiboot);
107 if (ret)
108 return -EINVAL;
Michal Simek46900462020-02-11 12:43:14 +0100109
Michal Simek21e5c322021-07-27 14:05:27 +0200110 return multiboot;
Michal Simek46900462020-02-11 12:43:14 +0100111}
112
Jorge Ramirez-Ortiz34deca52021-10-13 15:48:00 +0200113#if defined(CONFIG_SPL_BUILD)
114static void restore_jtag(void)
115{
116 if (current_el() != 3)
117 return;
118
119 writel(CSU_JTAG_SEC_GATE_DISABLE, &csu_base->jtag_sec);
120 writel(CSU_JTAG_DAP_ENABLE_DEBUG, &csu_base->jtag_dap_cfg);
121 writel(CSU_JTAG_CHAIN_WR_SETUP, &csu_base->jtag_chain_status_wr);
122 writel(CRLAPB_DBG_LPD_CTRL_SETUP_CLK, &crlapb_base->dbg_lpd_ctrl);
123 writel(CRLAPB_RST_LPD_DBG_RESET, &crlapb_base->rst_lpd_dbg);
124 writel(CSU_PCAP_PROG_RELEASE_PL, &csu_base->pcap_prog);
125}
126#endif
127
Jorge Ramirez-Ortizb0e388a2021-10-13 19:04:47 +0200128static void print_secure_boot(void)
129{
130 u32 status = 0;
131
132 if (zynqmp_mmio_read((ulong)&csu_base->status, &status))
133 return;
134
135 printf("Secure Boot:\t%sauthenticated, %sencrypted\n",
136 status & ZYNQMP_CSU_STATUS_AUTHENTICATED ? "" : "not ",
137 status & ZYNQMP_CSU_STATUS_ENCRYPTED ? "" : "not ");
138}
139
Michal Simek04b7e622015-01-15 10:01:51 +0100140int board_init(void)
141{
Stefan Herbrechtsmeier3ff3bd42022-06-20 18:36:43 +0200142#if CONFIG_IS_ENABLED(FPGA) && defined(CONFIG_FPGA_ZYNQMPPL)
143 struct udevice *soc;
144 char name[SOC_MAX_STR_SIZE];
145 int ret;
146#endif
Michal Simek826d7eca2020-03-04 08:48:16 +0100147#if defined(CONFIG_ZYNQMP_FIRMWARE)
Ibai Erkiaga2d9b95b2019-09-27 11:37:04 +0100148 struct udevice *dev;
149
150 uclass_get_device_by_name(UCLASS_FIRMWARE, "zynqmp-power", &dev);
151 if (!dev)
152 panic("PMU Firmware device not found - Enable it");
Michal Simek826d7eca2020-03-04 08:48:16 +0100153#endif
Ibai Erkiaga2d9b95b2019-09-27 11:37:04 +0100154
Luca Ceresoli23e65002019-05-21 18:06:43 +0200155#if defined(CONFIG_SPL_BUILD)
156 /* Check *at build time* if the filename is an non-empty string */
157 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
158 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
159 zynqmp_pm_cfg_obj_size);
Michal Simekae9dc112021-02-02 16:34:48 +0100160 printf("Silicon version:\t%d\n", zynqmp_get_silicon_version());
Jorge Ramirez-Ortiz34deca52021-10-13 15:48:00 +0200161
162 /* the CSU disables the JTAG interface when secure boot is enabled */
Ricardo Salveti5b774f02021-11-04 16:28:02 -0300163 if (CONFIG_IS_ENABLED(ZYNQMP_RESTORE_JTAG))
Jorge Ramirez-Ortiz34deca52021-10-13 15:48:00 +0200164 restore_jtag();
Michal Simek394ee242020-08-03 13:01:45 +0200165#else
166 if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM))
167 xilinx_read_eeprom();
Luca Ceresoli23e65002019-05-21 18:06:43 +0200168#endif
169
Michal Simekfb7242d2015-06-22 14:31:06 +0200170 printf("EL Level:\tEL%d\n", current_el());
171
Michal Simek1aab1142020-09-09 14:41:56 +0200172#if CONFIG_IS_ENABLED(FPGA) && defined(CONFIG_FPGA_ZYNQMPPL)
Stefan Herbrechtsmeier3ff3bd42022-06-20 18:36:43 +0200173 ret = soc_get(&soc);
174 if (!ret) {
175 ret = soc_get_machine(soc, name, sizeof(name));
176 if (ret >= 0) {
177 zynqmppl.name = strdup(name);
178 fpga_init();
179 fpga_add(fpga_xilinx, &zynqmppl);
180 }
181 }
Michal Simekbf0f9ca2018-04-19 15:43:38 +0200182#endif
183
Jorge Ramirez-Ortizb0e388a2021-10-13 19:04:47 +0200184 /* display secure boot information */
185 print_secure_boot();
Michal Simek46900462020-02-11 12:43:14 +0100186 if (current_el() == 3)
Michal Simek21e5c322021-07-27 14:05:27 +0200187 printf("Multiboot:\t%d\n", multi_boot());
Michal Simek46900462020-02-11 12:43:14 +0100188
Michal Simek04b7e622015-01-15 10:01:51 +0100189 return 0;
190}
191
192int board_early_init_r(void)
193{
194 u32 val;
195
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530196 if (current_el() != 3)
197 return 0;
198
Michal Simek245d5282017-07-12 10:32:18 +0200199 val = readl(&crlapb_base->timestamp_ref_ctrl);
200 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
201
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530202 if (!val) {
Michal Simekc23d3f82015-11-05 08:34:35 +0100203 val = readl(&crlapb_base->timestamp_ref_ctrl);
204 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
205 writel(val, &crlapb_base->timestamp_ref_ctrl);
Michal Simek04b7e622015-01-15 10:01:51 +0100206
Michal Simekc23d3f82015-11-05 08:34:35 +0100207 /* Program freq register in System counter */
208 writel(zynqmp_get_system_timer_freq(),
209 &iou_scntr_secure->base_frequency_id_register);
210 /* And enable system counter */
211 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
212 &iou_scntr_secure->counter_control_register);
213 }
Michal Simek04b7e622015-01-15 10:01:51 +0100214 return 0;
215}
216
Nitin Jainb2eb59b2018-02-16 12:56:17 +0530217unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
Simon Glassed38aef2020-05-10 11:40:03 -0600218 char *const argv[])
Nitin Jainb2eb59b2018-02-16 12:56:17 +0530219{
220 int ret = 0;
221
222 if (current_el() > 1) {
223 smp_kick_all_cpus();
224 dcache_disable();
225 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
226 ES_TO_AARCH64);
227 } else {
228 printf("FAIL: current EL is not above EL1\n");
229 ret = EINVAL;
230 }
231 return ret;
232}
233
Michal Simek8faa66a2016-02-08 09:34:53 +0100234#if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
Simon Glass2f949c32017-03-31 08:40:32 -0600235int dram_init_banksize(void)
Michal Simek8faa66a2016-02-08 09:34:53 +0100236{
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530237 int ret;
238
239 ret = fdtdec_setup_memory_banksize();
240 if (ret)
241 return ret;
242
243 mem_map_fill();
244
245 return 0;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500246}
Michal Simek8faa66a2016-02-08 09:34:53 +0100247
Tom Riniedcfdbd2016-12-09 07:56:54 -0500248int dram_init(void)
249{
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530250 if (fdtdec_setup_mem_size_base() != 0)
Nathan Rossiac04bfa2016-12-19 00:03:34 +1000251 return -EINVAL;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500252
253 return 0;
Michal Simek8faa66a2016-02-08 09:34:53 +0100254}
Michal Simek97ab9612021-05-31 11:03:19 +0200255
256ulong board_get_usable_ram_top(ulong total_size)
257{
258 phys_size_t size;
259 phys_addr_t reg;
260 struct lmb lmb;
261
Michal Simek1b8da4a2022-04-29 11:52:27 +0200262 if (!total_size)
263 return gd->ram_top;
264
Michal Simekf14a4f22021-08-19 11:07:59 +0200265 if (!IS_ALIGNED((ulong)gd->fdt_blob, 0x8))
266 panic("Not 64bit aligned DT location: %p\n", gd->fdt_blob);
267
Michal Simek97ab9612021-05-31 11:03:19 +0200268 /* found enough not-reserved memory to relocated U-Boot */
269 lmb_init(&lmb);
270 lmb_add(&lmb, gd->ram_base, gd->ram_size);
271 boot_fdt_add_mem_rsv_regions(&lmb, (void *)gd->fdt_blob);
Michal Simek770264e2021-10-21 08:58:50 +0200272 size = ALIGN(CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE);
Michal Simek97ab9612021-05-31 11:03:19 +0200273 reg = lmb_alloc(&lmb, size, MMU_SECTION_SIZE);
274
275 if (!reg)
276 reg = gd->ram_top - size;
277
278 return reg + size;
279}
Michal Simek8faa66a2016-02-08 09:34:53 +0100280#else
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530281int dram_init_banksize(void)
282{
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530283 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
284 gd->bd->bi_dram[0].size = get_effective_memsize();
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530285
286 mem_map_fill();
287
288 return 0;
289}
290
Michal Simek04b7e622015-01-15 10:01:51 +0100291int dram_init(void)
292{
Michal Simek1b846212018-04-11 16:12:28 +0200293 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
294 CONFIG_SYS_SDRAM_SIZE);
Michal Simek04b7e622015-01-15 10:01:51 +0100295
296 return 0;
297}
Michal Simek8faa66a2016-02-08 09:34:53 +0100298#endif
Michal Simek04b7e622015-01-15 10:01:51 +0100299
Michal Simek2a220332021-07-13 16:39:26 +0200300#if !CONFIG_IS_ENABLED(SYSRESET)
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100301void reset_cpu(void)
Michal Simek04b7e622015-01-15 10:01:51 +0100302{
303}
Michal Simek2a220332021-07-13 16:39:26 +0200304#endif
Michal Simek04b7e622015-01-15 10:01:51 +0100305
Michal Simek8ec30042020-08-20 10:54:45 +0200306static u8 __maybe_unused zynqmp_get_bootmode(void)
307{
308 u8 bootmode;
309 u32 reg = 0;
310 int ret;
311
312 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, &reg);
313 if (ret)
314 return -EINVAL;
315
Michal Simek58cc08c2021-07-28 12:25:49 +0200316 debug("HW boot mode: %x\n", reg & BOOT_MODES_MASK);
317 debug("ALT boot mode: %x\n", reg >> BOOT_MODE_ALT_SHIFT);
318
Michal Simek8ec30042020-08-20 10:54:45 +0200319 if (reg >> BOOT_MODE_ALT_SHIFT)
320 reg >>= BOOT_MODE_ALT_SHIFT;
321
322 bootmode = reg & BOOT_MODES_MASK;
323
324 return bootmode;
325}
326
Michal Simek342edfe2018-12-20 09:33:38 +0100327#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simek29b9b712018-05-17 14:06:06 +0200328static const struct {
329 u32 bit;
330 const char *name;
331} reset_reasons[] = {
332 { RESET_REASON_DEBUG_SYS, "DEBUG" },
333 { RESET_REASON_SOFT, "SOFT" },
334 { RESET_REASON_SRST, "SRST" },
335 { RESET_REASON_PSONLY, "PS-ONLY" },
336 { RESET_REASON_PMU, "PMU" },
337 { RESET_REASON_INTERNAL, "INTERNAL" },
338 { RESET_REASON_EXTERNAL, "EXTERNAL" },
339 {}
340};
341
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530342static int reset_reason(void)
Michal Simek29b9b712018-05-17 14:06:06 +0200343{
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530344 u32 reg;
345 int i, ret;
Michal Simek29b9b712018-05-17 14:06:06 +0200346 const char *reason = NULL;
347
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530348 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, &reg);
349 if (ret)
350 return -EINVAL;
Michal Simek29b9b712018-05-17 14:06:06 +0200351
352 puts("Reset reason:\t");
353
354 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530355 if (reg & reset_reasons[i].bit) {
Michal Simek29b9b712018-05-17 14:06:06 +0200356 reason = reset_reasons[i].name;
357 printf("%s ", reset_reasons[i].name);
358 break;
359 }
360 }
361
362 puts("\n");
363
364 env_set("reset_reason", reason);
365
Michal Simek0954c8c2021-02-09 08:50:22 +0100366 return 0;
Michal Simek29b9b712018-05-17 14:06:06 +0200367}
368
Michal Simek1ca66d72019-02-14 13:14:30 +0100369static int set_fdtfile(void)
370{
371 char *compatible, *fdtfile;
372 const char *suffix = ".dtb";
373 const char *vendor = "xilinx/";
Igor Lantsmane167bac2020-06-24 14:33:46 +0200374 int fdt_compat_len;
Michal Simek1ca66d72019-02-14 13:14:30 +0100375
376 if (env_get("fdtfile"))
377 return 0;
378
Igor Lantsmane167bac2020-06-24 14:33:46 +0200379 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible",
380 &fdt_compat_len);
381 if (compatible && fdt_compat_len) {
382 char *name;
383
Michal Simek1ca66d72019-02-14 13:14:30 +0100384 debug("Compatible: %s\n", compatible);
385
Igor Lantsmane167bac2020-06-24 14:33:46 +0200386 name = strchr(compatible, ',');
387 if (!name)
388 return -EINVAL;
Michal Simek1ca66d72019-02-14 13:14:30 +0100389
Igor Lantsmane167bac2020-06-24 14:33:46 +0200390 name++;
391
392 fdtfile = calloc(1, strlen(vendor) + strlen(name) +
Michal Simek1ca66d72019-02-14 13:14:30 +0100393 strlen(suffix) + 1);
394 if (!fdtfile)
395 return -ENOMEM;
396
Igor Lantsmane167bac2020-06-24 14:33:46 +0200397 sprintf(fdtfile, "%s%s%s", vendor, name, suffix);
Michal Simek1ca66d72019-02-14 13:14:30 +0100398
399 env_set("fdtfile", fdtfile);
400 free(fdtfile);
401 }
402
403 return 0;
404}
405
Michal Simek9c91e612020-04-08 11:04:41 +0200406int board_late_init(void)
407{
Michal Simek04b7e622015-01-15 10:01:51 +0100408 u8 bootmode;
Michal Simekf183a982018-04-25 11:20:43 +0200409 struct udevice *dev;
410 int bootseq = -1;
411 int bootseq_len = 0;
Michal Simek7410b142018-04-25 11:10:34 +0200412 int env_targets_len = 0;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200413 const char *mode;
414 char *new_targets;
Siva Durga Prasad Paladugu245c5562017-12-20 16:35:06 +0530415 char *env_targets;
Michal Simek7cb4cca2021-10-25 10:10:52 +0200416 int ret, multiboot;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200417
Michal Simek482f5492018-10-05 08:55:16 +0200418#if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
419 usb_ether_init();
420#endif
421
Michal Simekecfb6dc2016-04-22 14:28:54 +0200422 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
423 debug("Saved variables - Skipping\n");
424 return 0;
425 }
Michal Simek04b7e622015-01-15 10:01:51 +0100426
Michal Simekbab07b62020-07-28 12:45:47 +0200427 if (!CONFIG_IS_ENABLED(ENV_VARS_UBOOT_RUNTIME_CONFIG))
428 return 0;
429
Michal Simek1ca66d72019-02-14 13:14:30 +0100430 ret = set_fdtfile();
431 if (ret)
432 return ret;
433
Michal Simek7cb4cca2021-10-25 10:10:52 +0200434 multiboot = multi_boot();
435 if (multiboot >= 0)
436 env_set_hex("multiboot", multiboot);
437
Michal Simek9c91e612020-04-08 11:04:41 +0200438 bootmode = zynqmp_get_bootmode();
Michal Simek04b7e622015-01-15 10:01:51 +0100439
Michal Simekc5d95232015-09-20 17:20:42 +0200440 puts("Bootmode: ");
Michal Simek04b7e622015-01-15 10:01:51 +0100441 switch (bootmode) {
Michal Simek12398ea2016-08-19 14:14:52 +0200442 case USB_MODE:
443 puts("USB_MODE\n");
T Karthik Reddy9eee8e32021-03-24 23:37:57 -0600444 mode = "usb_dfu0 usb_dfu1";
Michal Simek43380352017-12-01 15:18:24 +0100445 env_set("modeboot", "usb_dfu_spl");
Michal Simek12398ea2016-08-19 14:14:52 +0200446 break;
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530447 case JTAG_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200448 puts("JTAG_MODE\n");
Siva Durga Prasad Paladugu9c441702019-06-25 17:41:09 +0530449 mode = "jtag pxe dhcp";
Michal Simek43380352017-12-01 15:18:24 +0100450 env_set("modeboot", "jtagboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530451 break;
452 case QSPI_MODE_24BIT:
453 case QSPI_MODE_32BIT:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200454 mode = "qspi0";
Michal Simekc5d95232015-09-20 17:20:42 +0200455 puts("QSPI_MODE\n");
Michal Simek43380352017-12-01 15:18:24 +0100456 env_set("modeboot", "qspiboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530457 break;
Michal Simek02d66cd2015-04-15 15:02:28 +0200458 case EMMC_MODE:
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200459 puts("EMMC_MODE\n");
T Karthik Reddy19735c32019-12-17 06:41:42 -0700460 if (uclass_get_device_by_name(UCLASS_MMC,
461 "mmc@ff160000", &dev) &&
462 uclass_get_device_by_name(UCLASS_MMC,
463 "sdhci@ff160000", &dev)) {
464 puts("Boot from EMMC but without SD0 enabled!\n");
465 return -1;
466 }
Simon Glass75e534b2020-12-16 21:20:07 -0700467 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
T Karthik Reddy19735c32019-12-17 06:41:42 -0700468
469 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700470 bootseq = dev_seq(dev);
Ashok Reddy Somaa10be052021-09-15 08:52:17 +0200471 env_set("modeboot", "emmcboot");
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200472 break;
473 case SD_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200474 puts("SD_MODE\n");
Michal Simekf183a982018-04-25 11:20:43 +0200475 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530476 "mmc@ff160000", &dev) &&
477 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200478 "sdhci@ff160000", &dev)) {
479 puts("Boot from SD0 but without SD0 enabled!\n");
480 return -1;
481 }
Simon Glass75e534b2020-12-16 21:20:07 -0700482 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
Michal Simekf183a982018-04-25 11:20:43 +0200483
484 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700485 bootseq = dev_seq(dev);
Michal Simek43380352017-12-01 15:18:24 +0100486 env_set("modeboot", "sdboot");
Michal Simek04b7e622015-01-15 10:01:51 +0100487 break;
Siva Durga Prasad Paladugu29a77d22016-09-21 11:45:05 +0530488 case SD1_LSHFT_MODE:
489 puts("LVL_SHFT_");
Michal Simek293f47b2021-10-18 13:30:04 +0200490 fallthrough;
Michal Simek108e1842015-10-05 10:51:12 +0200491 case SD_MODE1:
Michal Simekc5d95232015-09-20 17:20:42 +0200492 puts("SD_MODE1\n");
Michal Simekf183a982018-04-25 11:20:43 +0200493 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530494 "mmc@ff170000", &dev) &&
495 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200496 "sdhci@ff170000", &dev)) {
497 puts("Boot from SD1 but without SD1 enabled!\n");
498 return -1;
499 }
Simon Glass75e534b2020-12-16 21:20:07 -0700500 debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev));
Michal Simekf183a982018-04-25 11:20:43 +0200501
502 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700503 bootseq = dev_seq(dev);
Michal Simek43380352017-12-01 15:18:24 +0100504 env_set("modeboot", "sdboot");
Michal Simek108e1842015-10-05 10:51:12 +0200505 break;
506 case NAND_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200507 puts("NAND_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200508 mode = "nand0";
Michal Simek43380352017-12-01 15:18:24 +0100509 env_set("modeboot", "nandboot");
Michal Simek108e1842015-10-05 10:51:12 +0200510 break;
Michal Simek04b7e622015-01-15 10:01:51 +0100511 default:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200512 mode = "";
Michal Simek04b7e622015-01-15 10:01:51 +0100513 printf("Invalid Boot Mode:0x%x\n", bootmode);
514 break;
515 }
516
Michal Simekf183a982018-04-25 11:20:43 +0200517 if (bootseq >= 0) {
518 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
519 debug("Bootseq len: %x\n", bootseq_len);
Michal Simek7a117c72021-01-11 13:46:58 +0100520 env_set_hex("bootseq", bootseq);
Michal Simekf183a982018-04-25 11:20:43 +0200521 }
522
Michal Simekecfb6dc2016-04-22 14:28:54 +0200523 /*
524 * One terminating char + one byte for space between mode
525 * and default boot_targets
526 */
Siva Durga Prasad Paladugu245c5562017-12-20 16:35:06 +0530527 env_targets = env_get("boot_targets");
Michal Simek7410b142018-04-25 11:10:34 +0200528 if (env_targets)
529 env_targets_len = strlen(env_targets);
530
Michal Simekf183a982018-04-25 11:20:43 +0200531 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
532 bootseq_len);
Michal Simek089b84d2018-06-13 09:42:41 +0200533 if (!new_targets)
534 return -ENOMEM;
Michal Simek7410b142018-04-25 11:10:34 +0200535
Michal Simekf183a982018-04-25 11:20:43 +0200536 if (bootseq >= 0)
537 sprintf(new_targets, "%s%x %s", mode, bootseq,
538 env_targets ? env_targets : "");
539 else
540 sprintf(new_targets, "%s %s", mode,
541 env_targets ? env_targets : "");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200542
Simon Glass6a38e412017-08-03 12:22:09 -0600543 env_set("boot_targets", new_targets);
Michal Simek77488232021-07-28 12:46:39 +0200544 free(new_targets);
Michal Simekecfb6dc2016-04-22 14:28:54 +0200545
Michal Simek29b9b712018-05-17 14:06:06 +0200546 reset_reason();
547
Michal Simek705d44a2020-03-31 12:39:37 +0200548 return board_late_init_xilinx();
Michal Simek04b7e622015-01-15 10:01:51 +0100549}
Michal Simek342edfe2018-12-20 09:33:38 +0100550#endif
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530551
552int checkboard(void)
553{
Michal Simek47ce9362016-01-25 11:04:21 +0100554 puts("Board: Xilinx ZynqMP\n");
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530555 return 0;
556}
Michal Simek8d4a8d42020-07-30 13:37:49 +0200557
Michal Simeke0026bf2021-05-19 15:16:19 +0200558int mmc_get_env_dev(void)
559{
560 struct udevice *dev;
561 int bootseq = 0;
562
563 switch (zynqmp_get_bootmode()) {
564 case EMMC_MODE:
565 case SD_MODE:
566 if (uclass_get_device_by_name(UCLASS_MMC,
567 "mmc@ff160000", &dev) &&
568 uclass_get_device_by_name(UCLASS_MMC,
569 "sdhci@ff160000", &dev)) {
570 return -1;
571 }
572 bootseq = dev_seq(dev);
573 break;
574 case SD1_LSHFT_MODE:
575 case SD_MODE1:
576 if (uclass_get_device_by_name(UCLASS_MMC,
577 "mmc@ff170000", &dev) &&
578 uclass_get_device_by_name(UCLASS_MMC,
579 "sdhci@ff170000", &dev)) {
580 return -1;
581 }
582 bootseq = dev_seq(dev);
583 break;
584 default:
585 break;
586 }
587
588 debug("bootseq %d\n", bootseq);
589
590 return bootseq;
591}
592
Michal Simek8d4a8d42020-07-30 13:37:49 +0200593enum env_location env_get_location(enum env_operation op, int prio)
594{
595 u32 bootmode = zynqmp_get_bootmode();
596
597 if (prio)
598 return ENVL_UNKNOWN;
599
600 switch (bootmode) {
601 case EMMC_MODE:
602 case SD_MODE:
603 case SD1_LSHFT_MODE:
604 case SD_MODE1:
605 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
606 return ENVL_FAT;
607 if (IS_ENABLED(CONFIG_ENV_IS_IN_EXT4))
608 return ENVL_EXT4;
Mike Looijmans682cf082021-07-02 10:28:36 +0200609 return ENVL_NOWHERE;
Michal Simek8d4a8d42020-07-30 13:37:49 +0200610 case NAND_MODE:
611 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
612 return ENVL_NAND;
613 if (IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
614 return ENVL_UBI;
Mike Looijmans682cf082021-07-02 10:28:36 +0200615 return ENVL_NOWHERE;
Michal Simek8d4a8d42020-07-30 13:37:49 +0200616 case QSPI_MODE_24BIT:
617 case QSPI_MODE_32BIT:
618 if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
619 return ENVL_SPI_FLASH;
Mike Looijmans682cf082021-07-02 10:28:36 +0200620 return ENVL_NOWHERE;
Michal Simek8d4a8d42020-07-30 13:37:49 +0200621 case JTAG_MODE:
622 default:
623 return ENVL_NOWHERE;
624 }
625}
Michal Simekcfb37602021-07-27 16:19:18 +0200626
627#if defined(CONFIG_SET_DFU_ALT_INFO)
628
629#define DFU_ALT_BUF_LEN SZ_1K
630
631void set_dfu_alt_info(char *interface, char *devstr)
632{
633 u8 multiboot;
634 int bootseq = 0;
635
636 ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN);
637
Sughosh Ganuccb36462022-04-15 11:29:34 +0530638 if (!CONFIG_IS_ENABLED(EFI_HAVE_CAPSULE_SUPPORT) &&
639 env_get("dfu_alt_info"))
Michal Simekcfb37602021-07-27 16:19:18 +0200640 return;
641
642 memset(buf, 0, sizeof(buf));
643
644 multiboot = multi_boot();
Michal Simek7cb4cca2021-10-25 10:10:52 +0200645 if (multiboot < 0)
646 multiboot = 0;
647
648 multiboot = env_get_hex("multiboot", multiboot);
Michal Simekcfb37602021-07-27 16:19:18 +0200649 debug("Multiboot: %d\n", multiboot);
650
651 switch (zynqmp_get_bootmode()) {
652 case EMMC_MODE:
653 case SD_MODE:
654 case SD1_LSHFT_MODE:
655 case SD_MODE1:
656 bootseq = mmc_get_env_dev();
657 if (!multiboot)
658 snprintf(buf, DFU_ALT_BUF_LEN,
659 "mmc %d:1=boot.bin fat %d 1;"
Michal Simek69103192021-10-18 14:02:15 +0200660 "%s fat %d 1",
661 bootseq, bootseq,
662 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME, bootseq);
Michal Simekcfb37602021-07-27 16:19:18 +0200663 else
664 snprintf(buf, DFU_ALT_BUF_LEN,
665 "mmc %d:1=boot%04d.bin fat %d 1;"
Michal Simek69103192021-10-18 14:02:15 +0200666 "%s fat %d 1",
667 bootseq, multiboot, bootseq,
668 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME, bootseq);
Michal Simekcfb37602021-07-27 16:19:18 +0200669 break;
Stefan Herbrechtsmeierfb027cd2022-06-20 18:36:46 +0200670#if defined(CONFIG_SYS_SPI_U_BOOT_OFFS)
Michal Simekcfb37602021-07-27 16:19:18 +0200671 case QSPI_MODE_24BIT:
672 case QSPI_MODE_32BIT:
673 snprintf(buf, DFU_ALT_BUF_LEN,
674 "sf 0:0=boot.bin raw %x 0x1500000;"
Michal Simek69103192021-10-18 14:02:15 +0200675 "%s raw 0x%x 0x500000",
676 multiboot * SZ_32K, CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
677 CONFIG_SYS_SPI_U_BOOT_OFFS);
Michal Simekcfb37602021-07-27 16:19:18 +0200678 break;
Stefan Herbrechtsmeierfb027cd2022-06-20 18:36:46 +0200679#endif
Michal Simekcfb37602021-07-27 16:19:18 +0200680 default:
681 return;
682 }
683
684 env_set("dfu_alt_info", buf);
685 puts("DFU alt info setting: done\n");
686}
687#endif