blob: 56e3b36ca99e89ce46654965f908f7f7e3cdaf81 [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
Sean Anderson69949e62024-09-05 13:18:32 -040075 /* Disable secure access for boot devices */
76 writel(0x04920492, ZYNQMP_IOU_SECURE_SLCR);
77 writel(0x00920492, ZYNQMP_IOU_SECURE_SLCR + 4);
78
Michal Simek1f55e572020-03-20 08:59:02 +010079 /* Delay is required for clocks to be propagated */
80 udelay(1000000);
Michal Simeke5710e32022-02-17 14:28:42 +010081
82 return 0;
83}
Michal Simeke0f36102017-07-12 13:08:41 +020084
Michal Simeke5710e32022-02-17 14:28:42 +010085#if !defined(CONFIG_SPL_BUILD)
86# if defined(CONFIG_DEBUG_UART_BOARD_INIT)
87void board_debug_uart_init(void)
88{
89# if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
90 psu_uboot_init();
91# endif
92}
93# endif
Michal Simek09a7d7d2020-01-07 09:02:52 +010094
Michal Simeke5710e32022-02-17 14:28:42 +010095# if defined(CONFIG_BOARD_EARLY_INIT_F)
96int board_early_init_f(void)
97{
98 int ret = 0;
99# if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED) && !defined(CONFIG_DEBUG_UART_BOARD_INIT)
100 ret = psu_uboot_init();
101# endif
102 return ret;
Michal Simek8b353302017-02-07 14:32:26 +0100103}
Michal Simeke5710e32022-02-17 14:28:42 +0100104# endif
Michal Simekba6fb832022-02-17 14:28:40 +0100105#endif
Michal Simek8b353302017-02-07 14:32:26 +0100106
Michal Simek46900462020-02-11 12:43:14 +0100107static int multi_boot(void)
108{
Michal Simek6aca2832021-07-27 16:17:31 +0200109 u32 multiboot = 0;
110 int ret;
Michal Simek46900462020-02-11 12:43:14 +0100111
Michal Simek6aca2832021-07-27 16:17:31 +0200112 ret = zynqmp_mmio_read((ulong)&csu_base->multi_boot, &multiboot);
113 if (ret)
114 return -EINVAL;
Michal Simek46900462020-02-11 12:43:14 +0100115
Michal Simek21e5c322021-07-27 14:05:27 +0200116 return multiboot;
Michal Simek46900462020-02-11 12:43:14 +0100117}
118
Jorge Ramirez-Ortiz34deca52021-10-13 15:48:00 +0200119#if defined(CONFIG_SPL_BUILD)
120static void restore_jtag(void)
121{
122 if (current_el() != 3)
123 return;
124
125 writel(CSU_JTAG_SEC_GATE_DISABLE, &csu_base->jtag_sec);
126 writel(CSU_JTAG_DAP_ENABLE_DEBUG, &csu_base->jtag_dap_cfg);
127 writel(CSU_JTAG_CHAIN_WR_SETUP, &csu_base->jtag_chain_status_wr);
128 writel(CRLAPB_DBG_LPD_CTRL_SETUP_CLK, &crlapb_base->dbg_lpd_ctrl);
129 writel(CRLAPB_RST_LPD_DBG_RESET, &crlapb_base->rst_lpd_dbg);
130 writel(CSU_PCAP_PROG_RELEASE_PL, &csu_base->pcap_prog);
131}
132#endif
133
Jorge Ramirez-Ortizb0e388a2021-10-13 19:04:47 +0200134static void print_secure_boot(void)
135{
136 u32 status = 0;
137
138 if (zynqmp_mmio_read((ulong)&csu_base->status, &status))
139 return;
140
141 printf("Secure Boot:\t%sauthenticated, %sencrypted\n",
142 status & ZYNQMP_CSU_STATUS_AUTHENTICATED ? "" : "not ",
143 status & ZYNQMP_CSU_STATUS_ENCRYPTED ? "" : "not ");
144}
145
Michal Simek04b7e622015-01-15 10:01:51 +0100146int board_init(void)
147{
Stefan Herbrechtsmeier3ff3bd42022-06-20 18:36:43 +0200148#if CONFIG_IS_ENABLED(FPGA) && defined(CONFIG_FPGA_ZYNQMPPL)
149 struct udevice *soc;
150 char name[SOC_MAX_STR_SIZE];
151 int ret;
152#endif
Michal Simek3d49c952022-10-05 11:39:27 +0200153
154#if defined(CONFIG_SPL_BUILD)
155 /* Check *at build time* if the filename is an non-empty string */
156 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
157 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
158 zynqmp_pm_cfg_obj_size);
Ibai Erkiaga2d9b95b2019-09-27 11:37:04 +0100159
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
Tom Rinibb4dd962022-11-16 13:10:37 -0500234#if !defined(CFG_SYS_SDRAM_BASE) && !defined(CFG_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
Michal Simek8faa66a2016-02-08 09:34:53 +0100256#else
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530257int dram_init_banksize(void)
258{
Tom Rinibb4dd962022-11-16 13:10:37 -0500259 gd->bd->bi_dram[0].start = CFG_SYS_SDRAM_BASE;
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530260 gd->bd->bi_dram[0].size = get_effective_memsize();
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530261
262 mem_map_fill();
263
264 return 0;
265}
266
Michal Simek04b7e622015-01-15 10:01:51 +0100267int dram_init(void)
268{
Tom Rinibb4dd962022-11-16 13:10:37 -0500269 gd->ram_size = get_ram_size((void *)CFG_SYS_SDRAM_BASE,
270 CFG_SYS_SDRAM_SIZE);
Michal Simek04b7e622015-01-15 10:01:51 +0100271
272 return 0;
273}
Michal Simek8faa66a2016-02-08 09:34:53 +0100274#endif
Michal Simek04b7e622015-01-15 10:01:51 +0100275
Michal Simek2a220332021-07-13 16:39:26 +0200276#if !CONFIG_IS_ENABLED(SYSRESET)
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100277void reset_cpu(void)
Michal Simek04b7e622015-01-15 10:01:51 +0100278{
Lukas Funke45f61df2024-06-07 11:26:08 +0200279 if (!IS_ENABLED(CONFIG_ZYNQMP_FIRMWARE)) {
280 log_warning("reset failed: ZYNQMP_FIRMWARE disabled");
281 return;
282 }
283
284 /* In case of !CONFIG_ZYNQMP_FIRMWARE the call to 'xilinx_pm_request()'
285 * will be removed by the compiler due to the early return.
286 * If CONFIG_ZYNQMP_FIRMWARE is defined in SPL 'xilinx_pm_request()'
287 * will send command over IPI and requires pmufw to be present.
288 */
289 xilinx_pm_request(PM_RESET_ASSERT, ZYNQMP_PM_RESET_SOFT,
290 PM_RESET_ACTION_ASSERT, 0, 0, NULL);
Michal Simek04b7e622015-01-15 10:01:51 +0100291}
Michal Simek2a220332021-07-13 16:39:26 +0200292#endif
Michal Simek04b7e622015-01-15 10:01:51 +0100293
Michal Simek8ec30042020-08-20 10:54:45 +0200294static u8 __maybe_unused zynqmp_get_bootmode(void)
295{
296 u8 bootmode;
297 u32 reg = 0;
298 int ret;
299
300 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, &reg);
301 if (ret)
302 return -EINVAL;
303
Michal Simek58cc08c2021-07-28 12:25:49 +0200304 debug("HW boot mode: %x\n", reg & BOOT_MODES_MASK);
305 debug("ALT boot mode: %x\n", reg >> BOOT_MODE_ALT_SHIFT);
306
Michal Simek8ec30042020-08-20 10:54:45 +0200307 if (reg >> BOOT_MODE_ALT_SHIFT)
308 reg >>= BOOT_MODE_ALT_SHIFT;
309
310 bootmode = reg & BOOT_MODES_MASK;
311
312 return bootmode;
313}
314
Michal Simek342edfe2018-12-20 09:33:38 +0100315#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simek29b9b712018-05-17 14:06:06 +0200316static const struct {
317 u32 bit;
318 const char *name;
319} reset_reasons[] = {
320 { RESET_REASON_DEBUG_SYS, "DEBUG" },
321 { RESET_REASON_SOFT, "SOFT" },
322 { RESET_REASON_SRST, "SRST" },
323 { RESET_REASON_PSONLY, "PS-ONLY" },
324 { RESET_REASON_PMU, "PMU" },
325 { RESET_REASON_INTERNAL, "INTERNAL" },
326 { RESET_REASON_EXTERNAL, "EXTERNAL" },
327 {}
328};
329
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530330static int reset_reason(void)
Michal Simek29b9b712018-05-17 14:06:06 +0200331{
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530332 u32 reg;
333 int i, ret;
Michal Simek29b9b712018-05-17 14:06:06 +0200334 const char *reason = NULL;
335
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530336 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, &reg);
337 if (ret)
338 return -EINVAL;
Michal Simek29b9b712018-05-17 14:06:06 +0200339
340 puts("Reset reason:\t");
341
342 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530343 if (reg & reset_reasons[i].bit) {
Michal Simek29b9b712018-05-17 14:06:06 +0200344 reason = reset_reasons[i].name;
345 printf("%s ", reset_reasons[i].name);
346 break;
347 }
348 }
349
350 puts("\n");
351
352 env_set("reset_reason", reason);
353
Michal Simek0954c8c2021-02-09 08:50:22 +0100354 return 0;
Michal Simek29b9b712018-05-17 14:06:06 +0200355}
356
Michal Simek1ca66d72019-02-14 13:14:30 +0100357static int set_fdtfile(void)
358{
359 char *compatible, *fdtfile;
360 const char *suffix = ".dtb";
361 const char *vendor = "xilinx/";
Igor Lantsmane167bac2020-06-24 14:33:46 +0200362 int fdt_compat_len;
Michal Simek1ca66d72019-02-14 13:14:30 +0100363
364 if (env_get("fdtfile"))
365 return 0;
366
Igor Lantsmane167bac2020-06-24 14:33:46 +0200367 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible",
368 &fdt_compat_len);
369 if (compatible && fdt_compat_len) {
370 char *name;
371
Michal Simek1ca66d72019-02-14 13:14:30 +0100372 debug("Compatible: %s\n", compatible);
373
Igor Lantsmane167bac2020-06-24 14:33:46 +0200374 name = strchr(compatible, ',');
375 if (!name)
376 return -EINVAL;
Michal Simek1ca66d72019-02-14 13:14:30 +0100377
Igor Lantsmane167bac2020-06-24 14:33:46 +0200378 name++;
379
380 fdtfile = calloc(1, strlen(vendor) + strlen(name) +
Michal Simek1ca66d72019-02-14 13:14:30 +0100381 strlen(suffix) + 1);
382 if (!fdtfile)
383 return -ENOMEM;
384
Igor Lantsmane167bac2020-06-24 14:33:46 +0200385 sprintf(fdtfile, "%s%s%s", vendor, name, suffix);
Michal Simek1ca66d72019-02-14 13:14:30 +0100386
387 env_set("fdtfile", fdtfile);
388 free(fdtfile);
389 }
390
391 return 0;
392}
393
Michal Simekb1634762023-09-05 13:30:07 +0200394static int boot_targets_setup(void)
Michal Simek9c91e612020-04-08 11:04:41 +0200395{
Michal Simek04b7e622015-01-15 10:01:51 +0100396 u8 bootmode;
Michal Simekf183a982018-04-25 11:20:43 +0200397 struct udevice *dev;
398 int bootseq = -1;
399 int bootseq_len = 0;
Michal Simek7410b142018-04-25 11:10:34 +0200400 int env_targets_len = 0;
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530401 const char *mode = NULL;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200402 char *new_targets;
Siva Durga Prasad Paladugu245c5562017-12-20 16:35:06 +0530403 char *env_targets;
Michal Simek7cb4cca2021-10-25 10:10:52 +0200404
Michal Simek9c91e612020-04-08 11:04:41 +0200405 bootmode = zynqmp_get_bootmode();
Michal Simek04b7e622015-01-15 10:01:51 +0100406
Michal Simekc5d95232015-09-20 17:20:42 +0200407 puts("Bootmode: ");
Michal Simek04b7e622015-01-15 10:01:51 +0100408 switch (bootmode) {
Michal Simek12398ea2016-08-19 14:14:52 +0200409 case USB_MODE:
410 puts("USB_MODE\n");
T Karthik Reddy9eee8e32021-03-24 23:37:57 -0600411 mode = "usb_dfu0 usb_dfu1";
Michal Simek43380352017-12-01 15:18:24 +0100412 env_set("modeboot", "usb_dfu_spl");
Michal Simek12398ea2016-08-19 14:14:52 +0200413 break;
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530414 case JTAG_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200415 puts("JTAG_MODE\n");
Siva Durga Prasad Paladugu9c441702019-06-25 17:41:09 +0530416 mode = "jtag pxe dhcp";
Michal Simek43380352017-12-01 15:18:24 +0100417 env_set("modeboot", "jtagboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530418 break;
419 case QSPI_MODE_24BIT:
420 case QSPI_MODE_32BIT:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200421 mode = "qspi0";
Michal Simekc5d95232015-09-20 17:20:42 +0200422 puts("QSPI_MODE\n");
Michal Simek43380352017-12-01 15:18:24 +0100423 env_set("modeboot", "qspiboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530424 break;
Michal Simek02d66cd2015-04-15 15:02:28 +0200425 case EMMC_MODE:
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200426 puts("EMMC_MODE\n");
T Karthik Reddy19735c32019-12-17 06:41:42 -0700427 if (uclass_get_device_by_name(UCLASS_MMC,
428 "mmc@ff160000", &dev) &&
429 uclass_get_device_by_name(UCLASS_MMC,
430 "sdhci@ff160000", &dev)) {
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530431 debug("SD0 driver for SD0 device is not present\n");
432 break;
T Karthik Reddy19735c32019-12-17 06:41:42 -0700433 }
Simon Glass75e534b2020-12-16 21:20:07 -0700434 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
T Karthik Reddy19735c32019-12-17 06:41:42 -0700435
436 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700437 bootseq = dev_seq(dev);
Ashok Reddy Somaa10be052021-09-15 08:52:17 +0200438 env_set("modeboot", "emmcboot");
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200439 break;
440 case SD_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200441 puts("SD_MODE\n");
Michal Simekf183a982018-04-25 11:20:43 +0200442 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530443 "mmc@ff160000", &dev) &&
444 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200445 "sdhci@ff160000", &dev)) {
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530446 debug("SD0 driver for SD0 device is not present\n");
447 break;
Michal Simekf183a982018-04-25 11:20:43 +0200448 }
Simon Glass75e534b2020-12-16 21:20:07 -0700449 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
Michal Simekf183a982018-04-25 11:20:43 +0200450
451 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700452 bootseq = dev_seq(dev);
Michal Simek43380352017-12-01 15:18:24 +0100453 env_set("modeboot", "sdboot");
Michal Simek04b7e622015-01-15 10:01:51 +0100454 break;
Siva Durga Prasad Paladugu29a77d22016-09-21 11:45:05 +0530455 case SD1_LSHFT_MODE:
456 puts("LVL_SHFT_");
Michal Simek293f47b2021-10-18 13:30:04 +0200457 fallthrough;
Michal Simek108e1842015-10-05 10:51:12 +0200458 case SD_MODE1:
Michal Simekc5d95232015-09-20 17:20:42 +0200459 puts("SD_MODE1\n");
Michal Simekf183a982018-04-25 11:20:43 +0200460 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530461 "mmc@ff170000", &dev) &&
462 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200463 "sdhci@ff170000", &dev)) {
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530464 debug("SD1 driver for SD1 device is not present\n");
465 break;
Michal Simekf183a982018-04-25 11:20:43 +0200466 }
Simon Glass75e534b2020-12-16 21:20:07 -0700467 debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev));
Michal Simekf183a982018-04-25 11:20:43 +0200468
469 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700470 bootseq = dev_seq(dev);
Michal Simek43380352017-12-01 15:18:24 +0100471 env_set("modeboot", "sdboot");
Michal Simek108e1842015-10-05 10:51:12 +0200472 break;
473 case NAND_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200474 puts("NAND_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200475 mode = "nand0";
Michal Simek43380352017-12-01 15:18:24 +0100476 env_set("modeboot", "nandboot");
Michal Simek108e1842015-10-05 10:51:12 +0200477 break;
Michal Simek04b7e622015-01-15 10:01:51 +0100478 default:
479 printf("Invalid Boot Mode:0x%x\n", bootmode);
480 break;
481 }
482
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530483 if (mode) {
484 if (bootseq >= 0) {
485 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
486 debug("Bootseq len: %x\n", bootseq_len);
487 env_set_hex("bootseq", bootseq);
488 }
Michal Simekf183a982018-04-25 11:20:43 +0200489
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530490 /*
491 * One terminating char + one byte for space between mode
492 * and default boot_targets
493 */
494 env_targets = env_get("boot_targets");
495 if (env_targets)
496 env_targets_len = strlen(env_targets);
Michal Simek7410b142018-04-25 11:10:34 +0200497
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530498 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
499 bootseq_len);
500 if (!new_targets)
501 return -ENOMEM;
Michal Simek7410b142018-04-25 11:10:34 +0200502
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530503 if (bootseq >= 0)
504 sprintf(new_targets, "%s%x %s", mode, bootseq,
505 env_targets ? env_targets : "");
506 else
507 sprintf(new_targets, "%s %s", mode,
508 env_targets ? env_targets : "");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200509
Venkatesh Yadav Abbarapu6555dcc2023-09-04 08:50:35 +0530510 env_set("boot_targets", new_targets);
511 free(new_targets);
512 }
Michal Simekecfb6dc2016-04-22 14:28:54 +0200513
Michal Simekb1634762023-09-05 13:30:07 +0200514 return 0;
515}
516
517int board_late_init(void)
518{
519 int ret, multiboot;
520
521#if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
522 usb_ether_init();
523#endif
524
Kory Maincent9f894932024-05-29 12:01:06 +0200525 multiboot = multi_boot();
526 if (multiboot >= 0)
527 env_set_hex("multiboot", multiboot);
528
Michal Simekb1634762023-09-05 13:30:07 +0200529 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
530 debug("Saved variables - Skipping\n");
531 return 0;
532 }
533
534 if (!IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG))
535 return 0;
536
537 ret = set_fdtfile();
538 if (ret)
539 return ret;
540
Michal Simekb1634762023-09-05 13:30:07 +0200541 if (IS_ENABLED(CONFIG_DISTRO_DEFAULTS)) {
542 ret = boot_targets_setup();
543 if (ret)
544 return ret;
545 }
546
Michal Simek29b9b712018-05-17 14:06:06 +0200547 reset_reason();
548
Michal Simek705d44a2020-03-31 12:39:37 +0200549 return board_late_init_xilinx();
Michal Simek04b7e622015-01-15 10:01:51 +0100550}
Michal Simek342edfe2018-12-20 09:33:38 +0100551#endif
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530552
553int checkboard(void)
554{
Michal Simek47ce9362016-01-25 11:04:21 +0100555 puts("Board: Xilinx ZynqMP\n");
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530556 return 0;
557}
Michal Simek8d4a8d42020-07-30 13:37:49 +0200558
Michal Simeke0026bf2021-05-19 15:16:19 +0200559int mmc_get_env_dev(void)
560{
561 struct udevice *dev;
562 int bootseq = 0;
563
564 switch (zynqmp_get_bootmode()) {
565 case EMMC_MODE:
566 case SD_MODE:
567 if (uclass_get_device_by_name(UCLASS_MMC,
568 "mmc@ff160000", &dev) &&
569 uclass_get_device_by_name(UCLASS_MMC,
570 "sdhci@ff160000", &dev)) {
571 return -1;
572 }
573 bootseq = dev_seq(dev);
574 break;
575 case SD1_LSHFT_MODE:
576 case SD_MODE1:
577 if (uclass_get_device_by_name(UCLASS_MMC,
578 "mmc@ff170000", &dev) &&
579 uclass_get_device_by_name(UCLASS_MMC,
580 "sdhci@ff170000", &dev)) {
581 return -1;
582 }
583 bootseq = dev_seq(dev);
584 break;
585 default:
586 break;
587 }
588
589 debug("bootseq %d\n", bootseq);
590
591 return bootseq;
592}
593
Michal Simekf3a541f2024-03-22 12:43:17 +0100594#if defined(CONFIG_ENV_IS_NOWHERE)
Michal Simek8d4a8d42020-07-30 13:37:49 +0200595enum env_location env_get_location(enum env_operation op, int prio)
596{
597 u32 bootmode = zynqmp_get_bootmode();
598
599 if (prio)
600 return ENVL_UNKNOWN;
601
602 switch (bootmode) {
603 case EMMC_MODE:
604 case SD_MODE:
605 case SD1_LSHFT_MODE:
606 case SD_MODE1:
607 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
608 return ENVL_FAT;
609 if (IS_ENABLED(CONFIG_ENV_IS_IN_EXT4))
610 return ENVL_EXT4;
Mike Looijmans682cf082021-07-02 10:28:36 +0200611 return ENVL_NOWHERE;
Michal Simek8d4a8d42020-07-30 13:37:49 +0200612 case NAND_MODE:
613 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
614 return ENVL_NAND;
615 if (IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
616 return ENVL_UBI;
Mike Looijmans682cf082021-07-02 10:28:36 +0200617 return ENVL_NOWHERE;
Michal Simek8d4a8d42020-07-30 13:37:49 +0200618 case QSPI_MODE_24BIT:
619 case QSPI_MODE_32BIT:
620 if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
621 return ENVL_SPI_FLASH;
Mike Looijmans682cf082021-07-02 10:28:36 +0200622 return ENVL_NOWHERE;
Michal Simek8d4a8d42020-07-30 13:37:49 +0200623 case JTAG_MODE:
624 default:
625 return ENVL_NOWHERE;
626 }
627}
Michal Simekf3a541f2024-03-22 12:43:17 +0100628#endif
Michal Simekcfb37602021-07-27 16:19:18 +0200629
630#if defined(CONFIG_SET_DFU_ALT_INFO)
631
632#define DFU_ALT_BUF_LEN SZ_1K
633
Michal Simek733cd9e2024-03-22 13:09:19 +0100634static void mtd_found_part(u32 *base, u32 *size)
635{
636 struct mtd_info *part, *mtd;
637
638 mtd_probe_devices();
639
640 mtd = get_mtd_device_nm("nor0");
641 if (!IS_ERR_OR_NULL(mtd)) {
642 list_for_each_entry(part, &mtd->partitions, node) {
643 debug("0x%012llx-0x%012llx : \"%s\"\n",
644 part->offset, part->offset + part->size,
645 part->name);
646
647 if (*base >= part->offset &&
648 *base < part->offset + part->size) {
649 debug("Found my partition: %d/%s\n",
650 part->index, part->name);
651 *base = part->offset;
652 *size = part->size;
653 break;
654 }
655 }
656 }
657}
658
Michal Simekcfb37602021-07-27 16:19:18 +0200659void set_dfu_alt_info(char *interface, char *devstr)
660{
Michal Simek9fced422022-12-02 14:06:15 +0100661 int multiboot, bootseq = 0, len = 0;
Michal Simekcfb37602021-07-27 16:19:18 +0200662
663 ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN);
664
Michal Simekf0d6f462022-08-09 16:32:52 +0200665 if (env_get("dfu_alt_info"))
Michal Simekcfb37602021-07-27 16:19:18 +0200666 return;
667
668 memset(buf, 0, sizeof(buf));
669
670 multiboot = multi_boot();
Michal Simek7cb4cca2021-10-25 10:10:52 +0200671 if (multiboot < 0)
672 multiboot = 0;
673
674 multiboot = env_get_hex("multiboot", multiboot);
Michal Simekcfb37602021-07-27 16:19:18 +0200675 debug("Multiboot: %d\n", multiboot);
676
677 switch (zynqmp_get_bootmode()) {
678 case EMMC_MODE:
679 case SD_MODE:
680 case SD1_LSHFT_MODE:
681 case SD_MODE1:
682 bootseq = mmc_get_env_dev();
Michal Simek9fced422022-12-02 14:06:15 +0100683
684 len += snprintf(buf + len, DFU_ALT_BUF_LEN, "mmc %d=boot",
685 bootseq);
686
687 if (multiboot)
688 len += snprintf(buf + len, DFU_ALT_BUF_LEN,
689 "%04d", multiboot);
690
691 len += snprintf(buf + len, DFU_ALT_BUF_LEN, ".bin fat %d 1",
692 bootseq);
693#if defined(CONFIG_SPL_FS_LOAD_PAYLOAD_NAME)
Michal Simek64962b62024-03-22 13:09:18 +0100694 if (strlen(CONFIG_SPL_FS_LOAD_PAYLOAD_NAME))
695 len += snprintf(buf + len, DFU_ALT_BUF_LEN,
696 ";%s fat %d 1",
697 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
698 bootseq);
Michal Simek9fced422022-12-02 14:06:15 +0100699#endif
Michal Simekcfb37602021-07-27 16:19:18 +0200700 break;
701 case QSPI_MODE_24BIT:
702 case QSPI_MODE_32BIT:
Michal Simek733cd9e2024-03-22 13:09:19 +0100703 {
704 u32 base = multiboot * SZ_32K;
705 u32 size = 0x1500000;
706 u32 limit = size;
707
708 mtd_found_part(&base, &limit);
709
710#if defined(CONFIG_SYS_SPI_U_BOOT_OFFS)
711 size = limit;
712 limit = CONFIG_SYS_SPI_U_BOOT_OFFS;
713#endif
714
Michal Simek64962b62024-03-22 13:09:18 +0100715 len += snprintf(buf + len, DFU_ALT_BUF_LEN,
Michal Simek733cd9e2024-03-22 13:09:19 +0100716 "sf 0:0=boot.bin raw 0x%x 0x%x",
717 base, limit);
718#if defined(CONFIG_SPL_FS_LOAD_PAYLOAD_NAME) && defined(CONFIG_SYS_SPI_U_BOOT_OFFS)
719 if (strlen(CONFIG_SPL_FS_LOAD_PAYLOAD_NAME))
720 len += snprintf(buf + len, DFU_ALT_BUF_LEN,
721 ";%s raw 0x%x 0x%x",
722 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
723 base + limit, size - limit);
Stefan Herbrechtsmeierfb027cd2022-06-20 18:36:46 +0200724#endif
Michal Simek733cd9e2024-03-22 13:09:19 +0100725 }
Michal Simek9fced422022-12-02 14:06:15 +0100726 break;
Michal Simekcfb37602021-07-27 16:19:18 +0200727 default:
728 return;
729 }
730
731 env_set("dfu_alt_info", buf);
732 puts("DFU alt info setting: done\n");
733}
734#endif
Michal Simek55666ce2023-11-10 13:34:35 +0100735
736#if defined(CONFIG_SPL_SPI_LOAD)
737unsigned int spl_spi_get_uboot_offs(struct spi_flash *flash)
738{
739 u32 offset;
740 int multiboot = multi_boot();
741
742 offset = multiboot * SZ_32K;
743 offset += CONFIG_SYS_SPI_U_BOOT_OFFS;
744
745 log_info("SPI offset:\t0x%x\n", offset);
746
747 return offset;
748}
749#endif