blob: 57259b60a02eaaea6f97f299aaa82287928fb6f7 [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)
Oleksandr Suvorovdae95a42022-07-22 17:16:04 +030051static xilinx_desc zynqmppl = {
52 xilinx_zynqmp, csu_dma, 1, &zynqmp_op, 0, &zynqmp_op, NULL,
53 ZYNQMP_FPGA_FLAGS
54};
Michal Simek8111aff2016-02-01 15:05:58 +010055#endif
56
Michal Simeke5710e32022-02-17 14:28:42 +010057int __maybe_unused psu_uboot_init(void)
Michal Simek8b353302017-02-07 14:32:26 +010058{
Michal Simek09a7d7d2020-01-07 09:02:52 +010059 int ret;
60
Michal Simekc8785f22018-01-10 11:48:48 +010061 ret = psu_init();
Michal Simek09a7d7d2020-01-07 09:02:52 +010062 if (ret)
63 return ret;
Michal Simek1f55e572020-03-20 08:59:02 +010064
Adrian Fiergolski8e87ecf2021-06-08 12:37:23 +020065 /*
66 * PS_SYSMON_ANALOG_BUS register determines mapping between SysMon
67 * supply sense channel to SysMon supply registers inside the IP.
68 * This register must be programmed to complete SysMon IP
69 * configuration. The default register configuration after
70 * power-up is incorrect. Hence, fix this by writing the
71 * correct value - 0x3210.
72 */
73 writel(ZYNQMP_PS_SYSMON_ANALOG_BUS_VAL,
74 ZYNQMP_AMS_PS_SYSMON_ANALOG_BUS);
75
Michal Simek1f55e572020-03-20 08:59:02 +010076 /* Delay is required for clocks to be propagated */
77 udelay(1000000);
Michal Simeke5710e32022-02-17 14:28:42 +010078
79 return 0;
80}
Michal Simeke0f36102017-07-12 13:08:41 +020081
Michal Simeke5710e32022-02-17 14:28:42 +010082#if !defined(CONFIG_SPL_BUILD)
83# if defined(CONFIG_DEBUG_UART_BOARD_INIT)
84void board_debug_uart_init(void)
85{
86# if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
87 psu_uboot_init();
88# endif
89}
90# endif
Michal Simek09a7d7d2020-01-07 09:02:52 +010091
Michal Simeke5710e32022-02-17 14:28:42 +010092# if defined(CONFIG_BOARD_EARLY_INIT_F)
93int board_early_init_f(void)
94{
95 int ret = 0;
96# if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED) && !defined(CONFIG_DEBUG_UART_BOARD_INIT)
97 ret = psu_uboot_init();
98# endif
99 return ret;
Michal Simek8b353302017-02-07 14:32:26 +0100100}
Michal Simeke5710e32022-02-17 14:28:42 +0100101# endif
Michal Simekba6fb832022-02-17 14:28:40 +0100102#endif
Michal Simek8b353302017-02-07 14:32:26 +0100103
Michal Simek46900462020-02-11 12:43:14 +0100104static int multi_boot(void)
105{
Michal Simek6aca2832021-07-27 16:17:31 +0200106 u32 multiboot = 0;
107 int ret;
Michal Simek46900462020-02-11 12:43:14 +0100108
Michal Simek6aca2832021-07-27 16:17:31 +0200109 ret = zynqmp_mmio_read((ulong)&csu_base->multi_boot, &multiboot);
110 if (ret)
111 return -EINVAL;
Michal Simek46900462020-02-11 12:43:14 +0100112
Michal Simek21e5c322021-07-27 14:05:27 +0200113 return multiboot;
Michal Simek46900462020-02-11 12:43:14 +0100114}
115
Jorge Ramirez-Ortiz34deca52021-10-13 15:48:00 +0200116#if defined(CONFIG_SPL_BUILD)
117static void restore_jtag(void)
118{
119 if (current_el() != 3)
120 return;
121
122 writel(CSU_JTAG_SEC_GATE_DISABLE, &csu_base->jtag_sec);
123 writel(CSU_JTAG_DAP_ENABLE_DEBUG, &csu_base->jtag_dap_cfg);
124 writel(CSU_JTAG_CHAIN_WR_SETUP, &csu_base->jtag_chain_status_wr);
125 writel(CRLAPB_DBG_LPD_CTRL_SETUP_CLK, &crlapb_base->dbg_lpd_ctrl);
126 writel(CRLAPB_RST_LPD_DBG_RESET, &crlapb_base->rst_lpd_dbg);
127 writel(CSU_PCAP_PROG_RELEASE_PL, &csu_base->pcap_prog);
128}
129#endif
130
Jorge Ramirez-Ortizb0e388a2021-10-13 19:04:47 +0200131static void print_secure_boot(void)
132{
133 u32 status = 0;
134
135 if (zynqmp_mmio_read((ulong)&csu_base->status, &status))
136 return;
137
138 printf("Secure Boot:\t%sauthenticated, %sencrypted\n",
139 status & ZYNQMP_CSU_STATUS_AUTHENTICATED ? "" : "not ",
140 status & ZYNQMP_CSU_STATUS_ENCRYPTED ? "" : "not ");
141}
142
Michal Simek04b7e622015-01-15 10:01:51 +0100143int board_init(void)
144{
Stefan Herbrechtsmeier3ff3bd42022-06-20 18:36:43 +0200145#if CONFIG_IS_ENABLED(FPGA) && defined(CONFIG_FPGA_ZYNQMPPL)
146 struct udevice *soc;
147 char name[SOC_MAX_STR_SIZE];
148 int ret;
149#endif
Michal Simek826d7eca2020-03-04 08:48:16 +0100150#if defined(CONFIG_ZYNQMP_FIRMWARE)
Ibai Erkiaga2d9b95b2019-09-27 11:37:04 +0100151 struct udevice *dev;
152
153 uclass_get_device_by_name(UCLASS_FIRMWARE, "zynqmp-power", &dev);
154 if (!dev)
155 panic("PMU Firmware device not found - Enable it");
Michal Simek826d7eca2020-03-04 08:48:16 +0100156#endif
Ibai Erkiaga2d9b95b2019-09-27 11:37:04 +0100157
Luca Ceresoli23e65002019-05-21 18:06:43 +0200158#if defined(CONFIG_SPL_BUILD)
159 /* Check *at build time* if the filename is an non-empty string */
160 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
161 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
162 zynqmp_pm_cfg_obj_size);
Michal Simekae9dc112021-02-02 16:34:48 +0100163 printf("Silicon version:\t%d\n", zynqmp_get_silicon_version());
Jorge Ramirez-Ortiz34deca52021-10-13 15:48:00 +0200164
165 /* the CSU disables the JTAG interface when secure boot is enabled */
Ricardo Salveti5b774f02021-11-04 16:28:02 -0300166 if (CONFIG_IS_ENABLED(ZYNQMP_RESTORE_JTAG))
Jorge Ramirez-Ortiz34deca52021-10-13 15:48:00 +0200167 restore_jtag();
Michal Simek394ee242020-08-03 13:01:45 +0200168#else
169 if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM))
170 xilinx_read_eeprom();
Luca Ceresoli23e65002019-05-21 18:06:43 +0200171#endif
172
Michal Simekfb7242d2015-06-22 14:31:06 +0200173 printf("EL Level:\tEL%d\n", current_el());
174
Michal Simek1aab1142020-09-09 14:41:56 +0200175#if CONFIG_IS_ENABLED(FPGA) && defined(CONFIG_FPGA_ZYNQMPPL)
Stefan Herbrechtsmeier3ff3bd42022-06-20 18:36:43 +0200176 ret = soc_get(&soc);
177 if (!ret) {
178 ret = soc_get_machine(soc, name, sizeof(name));
179 if (ret >= 0) {
180 zynqmppl.name = strdup(name);
181 fpga_init();
182 fpga_add(fpga_xilinx, &zynqmppl);
183 }
184 }
Michal Simekbf0f9ca2018-04-19 15:43:38 +0200185#endif
186
Jorge Ramirez-Ortizb0e388a2021-10-13 19:04:47 +0200187 /* display secure boot information */
188 print_secure_boot();
Michal Simek46900462020-02-11 12:43:14 +0100189 if (current_el() == 3)
Michal Simek21e5c322021-07-27 14:05:27 +0200190 printf("Multiboot:\t%d\n", multi_boot());
Michal Simek46900462020-02-11 12:43:14 +0100191
Michal Simek04b7e622015-01-15 10:01:51 +0100192 return 0;
193}
194
195int board_early_init_r(void)
196{
197 u32 val;
198
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530199 if (current_el() != 3)
200 return 0;
201
Michal Simek245d5282017-07-12 10:32:18 +0200202 val = readl(&crlapb_base->timestamp_ref_ctrl);
203 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
204
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530205 if (!val) {
Michal Simekc23d3f82015-11-05 08:34:35 +0100206 val = readl(&crlapb_base->timestamp_ref_ctrl);
207 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
208 writel(val, &crlapb_base->timestamp_ref_ctrl);
Michal Simek04b7e622015-01-15 10:01:51 +0100209
Michal Simekc23d3f82015-11-05 08:34:35 +0100210 /* Program freq register in System counter */
211 writel(zynqmp_get_system_timer_freq(),
212 &iou_scntr_secure->base_frequency_id_register);
213 /* And enable system counter */
214 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
215 &iou_scntr_secure->counter_control_register);
216 }
Michal Simek04b7e622015-01-15 10:01:51 +0100217 return 0;
218}
219
Nitin Jainb2eb59b2018-02-16 12:56:17 +0530220unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
Simon Glassed38aef2020-05-10 11:40:03 -0600221 char *const argv[])
Nitin Jainb2eb59b2018-02-16 12:56:17 +0530222{
223 int ret = 0;
224
225 if (current_el() > 1) {
226 smp_kick_all_cpus();
227 dcache_disable();
228 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
229 ES_TO_AARCH64);
230 } else {
231 printf("FAIL: current EL is not above EL1\n");
232 ret = EINVAL;
233 }
234 return ret;
235}
236
Michal Simek8faa66a2016-02-08 09:34:53 +0100237#if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
Simon Glass2f949c32017-03-31 08:40:32 -0600238int dram_init_banksize(void)
Michal Simek8faa66a2016-02-08 09:34:53 +0100239{
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530240 int ret;
241
242 ret = fdtdec_setup_memory_banksize();
243 if (ret)
244 return ret;
245
246 mem_map_fill();
247
248 return 0;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500249}
Michal Simek8faa66a2016-02-08 09:34:53 +0100250
Tom Riniedcfdbd2016-12-09 07:56:54 -0500251int dram_init(void)
252{
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530253 if (fdtdec_setup_mem_size_base() != 0)
Nathan Rossiac04bfa2016-12-19 00:03:34 +1000254 return -EINVAL;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500255
256 return 0;
Michal Simek8faa66a2016-02-08 09:34:53 +0100257}
Michal Simek97ab9612021-05-31 11:03:19 +0200258
Ashok Reddy Soma8aaae3d2022-07-07 10:45:37 +0200259#if defined(CONFIG_LMB)
Michal Simek97ab9612021-05-31 11:03:19 +0200260ulong board_get_usable_ram_top(ulong total_size)
261{
262 phys_size_t size;
263 phys_addr_t reg;
264 struct lmb lmb;
265
Michal Simek1b8da4a2022-04-29 11:52:27 +0200266 if (!total_size)
267 return gd->ram_top;
268
Michal Simekf14a4f22021-08-19 11:07:59 +0200269 if (!IS_ALIGNED((ulong)gd->fdt_blob, 0x8))
270 panic("Not 64bit aligned DT location: %p\n", gd->fdt_blob);
271
Michal Simek97ab9612021-05-31 11:03:19 +0200272 /* found enough not-reserved memory to relocated U-Boot */
273 lmb_init(&lmb);
274 lmb_add(&lmb, gd->ram_base, gd->ram_size);
275 boot_fdt_add_mem_rsv_regions(&lmb, (void *)gd->fdt_blob);
Michal Simek770264e2021-10-21 08:58:50 +0200276 size = ALIGN(CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE);
Michal Simek97ab9612021-05-31 11:03:19 +0200277 reg = lmb_alloc(&lmb, size, MMU_SECTION_SIZE);
278
279 if (!reg)
280 reg = gd->ram_top - size;
281
282 return reg + size;
283}
Ashok Reddy Soma8aaae3d2022-07-07 10:45:37 +0200284#endif
285
Michal Simek8faa66a2016-02-08 09:34:53 +0100286#else
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530287int dram_init_banksize(void)
288{
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530289 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
290 gd->bd->bi_dram[0].size = get_effective_memsize();
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530291
292 mem_map_fill();
293
294 return 0;
295}
296
Michal Simek04b7e622015-01-15 10:01:51 +0100297int dram_init(void)
298{
Michal Simek1b846212018-04-11 16:12:28 +0200299 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
300 CONFIG_SYS_SDRAM_SIZE);
Michal Simek04b7e622015-01-15 10:01:51 +0100301
302 return 0;
303}
Michal Simek8faa66a2016-02-08 09:34:53 +0100304#endif
Michal Simek04b7e622015-01-15 10:01:51 +0100305
Michal Simek2a220332021-07-13 16:39:26 +0200306#if !CONFIG_IS_ENABLED(SYSRESET)
Harald Seiler6f14d5f2020-12-15 16:47:52 +0100307void reset_cpu(void)
Michal Simek04b7e622015-01-15 10:01:51 +0100308{
309}
Michal Simek2a220332021-07-13 16:39:26 +0200310#endif
Michal Simek04b7e622015-01-15 10:01:51 +0100311
Michal Simek8ec30042020-08-20 10:54:45 +0200312static u8 __maybe_unused zynqmp_get_bootmode(void)
313{
314 u8 bootmode;
315 u32 reg = 0;
316 int ret;
317
318 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, &reg);
319 if (ret)
320 return -EINVAL;
321
Michal Simek58cc08c2021-07-28 12:25:49 +0200322 debug("HW boot mode: %x\n", reg & BOOT_MODES_MASK);
323 debug("ALT boot mode: %x\n", reg >> BOOT_MODE_ALT_SHIFT);
324
Michal Simek8ec30042020-08-20 10:54:45 +0200325 if (reg >> BOOT_MODE_ALT_SHIFT)
326 reg >>= BOOT_MODE_ALT_SHIFT;
327
328 bootmode = reg & BOOT_MODES_MASK;
329
330 return bootmode;
331}
332
Michal Simek342edfe2018-12-20 09:33:38 +0100333#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simek29b9b712018-05-17 14:06:06 +0200334static const struct {
335 u32 bit;
336 const char *name;
337} reset_reasons[] = {
338 { RESET_REASON_DEBUG_SYS, "DEBUG" },
339 { RESET_REASON_SOFT, "SOFT" },
340 { RESET_REASON_SRST, "SRST" },
341 { RESET_REASON_PSONLY, "PS-ONLY" },
342 { RESET_REASON_PMU, "PMU" },
343 { RESET_REASON_INTERNAL, "INTERNAL" },
344 { RESET_REASON_EXTERNAL, "EXTERNAL" },
345 {}
346};
347
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530348static int reset_reason(void)
Michal Simek29b9b712018-05-17 14:06:06 +0200349{
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530350 u32 reg;
351 int i, ret;
Michal Simek29b9b712018-05-17 14:06:06 +0200352 const char *reason = NULL;
353
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530354 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, &reg);
355 if (ret)
356 return -EINVAL;
Michal Simek29b9b712018-05-17 14:06:06 +0200357
358 puts("Reset reason:\t");
359
360 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530361 if (reg & reset_reasons[i].bit) {
Michal Simek29b9b712018-05-17 14:06:06 +0200362 reason = reset_reasons[i].name;
363 printf("%s ", reset_reasons[i].name);
364 break;
365 }
366 }
367
368 puts("\n");
369
370 env_set("reset_reason", reason);
371
Michal Simek0954c8c2021-02-09 08:50:22 +0100372 return 0;
Michal Simek29b9b712018-05-17 14:06:06 +0200373}
374
Michal Simek1ca66d72019-02-14 13:14:30 +0100375static int set_fdtfile(void)
376{
377 char *compatible, *fdtfile;
378 const char *suffix = ".dtb";
379 const char *vendor = "xilinx/";
Igor Lantsmane167bac2020-06-24 14:33:46 +0200380 int fdt_compat_len;
Michal Simek1ca66d72019-02-14 13:14:30 +0100381
382 if (env_get("fdtfile"))
383 return 0;
384
Igor Lantsmane167bac2020-06-24 14:33:46 +0200385 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible",
386 &fdt_compat_len);
387 if (compatible && fdt_compat_len) {
388 char *name;
389
Michal Simek1ca66d72019-02-14 13:14:30 +0100390 debug("Compatible: %s\n", compatible);
391
Igor Lantsmane167bac2020-06-24 14:33:46 +0200392 name = strchr(compatible, ',');
393 if (!name)
394 return -EINVAL;
Michal Simek1ca66d72019-02-14 13:14:30 +0100395
Igor Lantsmane167bac2020-06-24 14:33:46 +0200396 name++;
397
398 fdtfile = calloc(1, strlen(vendor) + strlen(name) +
Michal Simek1ca66d72019-02-14 13:14:30 +0100399 strlen(suffix) + 1);
400 if (!fdtfile)
401 return -ENOMEM;
402
Igor Lantsmane167bac2020-06-24 14:33:46 +0200403 sprintf(fdtfile, "%s%s%s", vendor, name, suffix);
Michal Simek1ca66d72019-02-14 13:14:30 +0100404
405 env_set("fdtfile", fdtfile);
406 free(fdtfile);
407 }
408
409 return 0;
410}
411
Michal Simek9c91e612020-04-08 11:04:41 +0200412int board_late_init(void)
413{
Michal Simek04b7e622015-01-15 10:01:51 +0100414 u8 bootmode;
Michal Simekf183a982018-04-25 11:20:43 +0200415 struct udevice *dev;
416 int bootseq = -1;
417 int bootseq_len = 0;
Michal Simek7410b142018-04-25 11:10:34 +0200418 int env_targets_len = 0;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200419 const char *mode;
420 char *new_targets;
Siva Durga Prasad Paladugu245c5562017-12-20 16:35:06 +0530421 char *env_targets;
Michal Simek7cb4cca2021-10-25 10:10:52 +0200422 int ret, multiboot;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200423
Michal Simek482f5492018-10-05 08:55:16 +0200424#if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
425 usb_ether_init();
426#endif
427
Michal Simekecfb6dc2016-04-22 14:28:54 +0200428 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
429 debug("Saved variables - Skipping\n");
430 return 0;
431 }
Michal Simek04b7e622015-01-15 10:01:51 +0100432
Michal Simekbab07b62020-07-28 12:45:47 +0200433 if (!CONFIG_IS_ENABLED(ENV_VARS_UBOOT_RUNTIME_CONFIG))
434 return 0;
435
Michal Simek1ca66d72019-02-14 13:14:30 +0100436 ret = set_fdtfile();
437 if (ret)
438 return ret;
439
Michal Simek7cb4cca2021-10-25 10:10:52 +0200440 multiboot = multi_boot();
441 if (multiboot >= 0)
442 env_set_hex("multiboot", multiboot);
443
Michal Simek9c91e612020-04-08 11:04:41 +0200444 bootmode = zynqmp_get_bootmode();
Michal Simek04b7e622015-01-15 10:01:51 +0100445
Michal Simekc5d95232015-09-20 17:20:42 +0200446 puts("Bootmode: ");
Michal Simek04b7e622015-01-15 10:01:51 +0100447 switch (bootmode) {
Michal Simek12398ea2016-08-19 14:14:52 +0200448 case USB_MODE:
449 puts("USB_MODE\n");
T Karthik Reddy9eee8e32021-03-24 23:37:57 -0600450 mode = "usb_dfu0 usb_dfu1";
Michal Simek43380352017-12-01 15:18:24 +0100451 env_set("modeboot", "usb_dfu_spl");
Michal Simek12398ea2016-08-19 14:14:52 +0200452 break;
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530453 case JTAG_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200454 puts("JTAG_MODE\n");
Siva Durga Prasad Paladugu9c441702019-06-25 17:41:09 +0530455 mode = "jtag pxe dhcp";
Michal Simek43380352017-12-01 15:18:24 +0100456 env_set("modeboot", "jtagboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530457 break;
458 case QSPI_MODE_24BIT:
459 case QSPI_MODE_32BIT:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200460 mode = "qspi0";
Michal Simekc5d95232015-09-20 17:20:42 +0200461 puts("QSPI_MODE\n");
Michal Simek43380352017-12-01 15:18:24 +0100462 env_set("modeboot", "qspiboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530463 break;
Michal Simek02d66cd2015-04-15 15:02:28 +0200464 case EMMC_MODE:
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200465 puts("EMMC_MODE\n");
T Karthik Reddy19735c32019-12-17 06:41:42 -0700466 if (uclass_get_device_by_name(UCLASS_MMC,
467 "mmc@ff160000", &dev) &&
468 uclass_get_device_by_name(UCLASS_MMC,
469 "sdhci@ff160000", &dev)) {
470 puts("Boot from EMMC but without SD0 enabled!\n");
471 return -1;
472 }
Simon Glass75e534b2020-12-16 21:20:07 -0700473 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
T Karthik Reddy19735c32019-12-17 06:41:42 -0700474
475 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700476 bootseq = dev_seq(dev);
Ashok Reddy Somaa10be052021-09-15 08:52:17 +0200477 env_set("modeboot", "emmcboot");
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200478 break;
479 case SD_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200480 puts("SD_MODE\n");
Michal Simekf183a982018-04-25 11:20:43 +0200481 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530482 "mmc@ff160000", &dev) &&
483 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200484 "sdhci@ff160000", &dev)) {
485 puts("Boot from SD0 but without SD0 enabled!\n");
486 return -1;
487 }
Simon Glass75e534b2020-12-16 21:20:07 -0700488 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
Michal Simekf183a982018-04-25 11:20:43 +0200489
490 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700491 bootseq = dev_seq(dev);
Michal Simek43380352017-12-01 15:18:24 +0100492 env_set("modeboot", "sdboot");
Michal Simek04b7e622015-01-15 10:01:51 +0100493 break;
Siva Durga Prasad Paladugu29a77d22016-09-21 11:45:05 +0530494 case SD1_LSHFT_MODE:
495 puts("LVL_SHFT_");
Michal Simek293f47b2021-10-18 13:30:04 +0200496 fallthrough;
Michal Simek108e1842015-10-05 10:51:12 +0200497 case SD_MODE1:
Michal Simekc5d95232015-09-20 17:20:42 +0200498 puts("SD_MODE1\n");
Michal Simekf183a982018-04-25 11:20:43 +0200499 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530500 "mmc@ff170000", &dev) &&
501 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200502 "sdhci@ff170000", &dev)) {
503 puts("Boot from SD1 but without SD1 enabled!\n");
504 return -1;
505 }
Simon Glass75e534b2020-12-16 21:20:07 -0700506 debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev));
Michal Simekf183a982018-04-25 11:20:43 +0200507
508 mode = "mmc";
Simon Glass75e534b2020-12-16 21:20:07 -0700509 bootseq = dev_seq(dev);
Michal Simek43380352017-12-01 15:18:24 +0100510 env_set("modeboot", "sdboot");
Michal Simek108e1842015-10-05 10:51:12 +0200511 break;
512 case NAND_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200513 puts("NAND_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200514 mode = "nand0";
Michal Simek43380352017-12-01 15:18:24 +0100515 env_set("modeboot", "nandboot");
Michal Simek108e1842015-10-05 10:51:12 +0200516 break;
Michal Simek04b7e622015-01-15 10:01:51 +0100517 default:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200518 mode = "";
Michal Simek04b7e622015-01-15 10:01:51 +0100519 printf("Invalid Boot Mode:0x%x\n", bootmode);
520 break;
521 }
522
Michal Simekf183a982018-04-25 11:20:43 +0200523 if (bootseq >= 0) {
524 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
525 debug("Bootseq len: %x\n", bootseq_len);
Michal Simek7a117c72021-01-11 13:46:58 +0100526 env_set_hex("bootseq", bootseq);
Michal Simekf183a982018-04-25 11:20:43 +0200527 }
528
Michal Simekecfb6dc2016-04-22 14:28:54 +0200529 /*
530 * One terminating char + one byte for space between mode
531 * and default boot_targets
532 */
Siva Durga Prasad Paladugu245c5562017-12-20 16:35:06 +0530533 env_targets = env_get("boot_targets");
Michal Simek7410b142018-04-25 11:10:34 +0200534 if (env_targets)
535 env_targets_len = strlen(env_targets);
536
Michal Simekf183a982018-04-25 11:20:43 +0200537 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
538 bootseq_len);
Michal Simek089b84d2018-06-13 09:42:41 +0200539 if (!new_targets)
540 return -ENOMEM;
Michal Simek7410b142018-04-25 11:10:34 +0200541
Michal Simekf183a982018-04-25 11:20:43 +0200542 if (bootseq >= 0)
543 sprintf(new_targets, "%s%x %s", mode, bootseq,
544 env_targets ? env_targets : "");
545 else
546 sprintf(new_targets, "%s %s", mode,
547 env_targets ? env_targets : "");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200548
Simon Glass6a38e412017-08-03 12:22:09 -0600549 env_set("boot_targets", new_targets);
Michal Simek77488232021-07-28 12:46:39 +0200550 free(new_targets);
Michal Simekecfb6dc2016-04-22 14:28:54 +0200551
Michal Simek29b9b712018-05-17 14:06:06 +0200552 reset_reason();
553
Michal Simek705d44a2020-03-31 12:39:37 +0200554 return board_late_init_xilinx();
Michal Simek04b7e622015-01-15 10:01:51 +0100555}
Michal Simek342edfe2018-12-20 09:33:38 +0100556#endif
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530557
558int checkboard(void)
559{
Michal Simek47ce9362016-01-25 11:04:21 +0100560 puts("Board: Xilinx ZynqMP\n");
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530561 return 0;
562}
Michal Simek8d4a8d42020-07-30 13:37:49 +0200563
Michal Simeke0026bf2021-05-19 15:16:19 +0200564int mmc_get_env_dev(void)
565{
566 struct udevice *dev;
567 int bootseq = 0;
568
569 switch (zynqmp_get_bootmode()) {
570 case EMMC_MODE:
571 case SD_MODE:
572 if (uclass_get_device_by_name(UCLASS_MMC,
573 "mmc@ff160000", &dev) &&
574 uclass_get_device_by_name(UCLASS_MMC,
575 "sdhci@ff160000", &dev)) {
576 return -1;
577 }
578 bootseq = dev_seq(dev);
579 break;
580 case SD1_LSHFT_MODE:
581 case SD_MODE1:
582 if (uclass_get_device_by_name(UCLASS_MMC,
583 "mmc@ff170000", &dev) &&
584 uclass_get_device_by_name(UCLASS_MMC,
585 "sdhci@ff170000", &dev)) {
586 return -1;
587 }
588 bootseq = dev_seq(dev);
589 break;
590 default:
591 break;
592 }
593
594 debug("bootseq %d\n", bootseq);
595
596 return bootseq;
597}
598
Michal Simek8d4a8d42020-07-30 13:37:49 +0200599enum env_location env_get_location(enum env_operation op, int prio)
600{
601 u32 bootmode = zynqmp_get_bootmode();
602
603 if (prio)
604 return ENVL_UNKNOWN;
605
606 switch (bootmode) {
607 case EMMC_MODE:
608 case SD_MODE:
609 case SD1_LSHFT_MODE:
610 case SD_MODE1:
611 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
612 return ENVL_FAT;
613 if (IS_ENABLED(CONFIG_ENV_IS_IN_EXT4))
614 return ENVL_EXT4;
Mike Looijmans682cf082021-07-02 10:28:36 +0200615 return ENVL_NOWHERE;
Michal Simek8d4a8d42020-07-30 13:37:49 +0200616 case NAND_MODE:
617 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
618 return ENVL_NAND;
619 if (IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
620 return ENVL_UBI;
Mike Looijmans682cf082021-07-02 10:28:36 +0200621 return ENVL_NOWHERE;
Michal Simek8d4a8d42020-07-30 13:37:49 +0200622 case QSPI_MODE_24BIT:
623 case QSPI_MODE_32BIT:
624 if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
625 return ENVL_SPI_FLASH;
Mike Looijmans682cf082021-07-02 10:28:36 +0200626 return ENVL_NOWHERE;
Michal Simek8d4a8d42020-07-30 13:37:49 +0200627 case JTAG_MODE:
628 default:
629 return ENVL_NOWHERE;
630 }
631}
Michal Simekcfb37602021-07-27 16:19:18 +0200632
633#if defined(CONFIG_SET_DFU_ALT_INFO)
634
635#define DFU_ALT_BUF_LEN SZ_1K
636
637void set_dfu_alt_info(char *interface, char *devstr)
638{
639 u8 multiboot;
640 int bootseq = 0;
641
642 ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN);
643
Sughosh Ganuccb36462022-04-15 11:29:34 +0530644 if (!CONFIG_IS_ENABLED(EFI_HAVE_CAPSULE_SUPPORT) &&
645 env_get("dfu_alt_info"))
Michal Simekcfb37602021-07-27 16:19:18 +0200646 return;
647
648 memset(buf, 0, sizeof(buf));
649
650 multiboot = multi_boot();
Michal Simek7cb4cca2021-10-25 10:10:52 +0200651 if (multiboot < 0)
652 multiboot = 0;
653
654 multiboot = env_get_hex("multiboot", multiboot);
Michal Simekcfb37602021-07-27 16:19:18 +0200655 debug("Multiboot: %d\n", multiboot);
656
657 switch (zynqmp_get_bootmode()) {
658 case EMMC_MODE:
659 case SD_MODE:
660 case SD1_LSHFT_MODE:
661 case SD_MODE1:
662 bootseq = mmc_get_env_dev();
663 if (!multiboot)
664 snprintf(buf, DFU_ALT_BUF_LEN,
665 "mmc %d:1=boot.bin fat %d 1;"
Michal Simek69103192021-10-18 14:02:15 +0200666 "%s fat %d 1",
667 bootseq, bootseq,
668 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME, bootseq);
Michal Simekcfb37602021-07-27 16:19:18 +0200669 else
670 snprintf(buf, DFU_ALT_BUF_LEN,
671 "mmc %d:1=boot%04d.bin fat %d 1;"
Michal Simek69103192021-10-18 14:02:15 +0200672 "%s fat %d 1",
673 bootseq, multiboot, bootseq,
674 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME, bootseq);
Michal Simekcfb37602021-07-27 16:19:18 +0200675 break;
Stefan Herbrechtsmeierfb027cd2022-06-20 18:36:46 +0200676#if defined(CONFIG_SYS_SPI_U_BOOT_OFFS)
Michal Simekcfb37602021-07-27 16:19:18 +0200677 case QSPI_MODE_24BIT:
678 case QSPI_MODE_32BIT:
679 snprintf(buf, DFU_ALT_BUF_LEN,
680 "sf 0:0=boot.bin raw %x 0x1500000;"
Michal Simek69103192021-10-18 14:02:15 +0200681 "%s raw 0x%x 0x500000",
682 multiboot * SZ_32K, CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
683 CONFIG_SYS_SPI_U_BOOT_OFFS);
Michal Simekcfb37602021-07-27 16:19:18 +0200684 break;
Stefan Herbrechtsmeierfb027cd2022-06-20 18:36:46 +0200685#endif
Michal Simekcfb37602021-07-27 16:19:18 +0200686 default:
687 return;
688 }
689
690 env_set("dfu_alt_info", buf);
691 puts("DFU alt info setting: done\n");
692}
693#endif