1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2014 - 2015 Xilinx, Inc.
4 * Michal Simek <michal.simek@amd.com>
10 #include <debug_uart.h>
13 #include <env_internal.h>
24 #include <asm/arch/clk.h>
25 #include <asm/arch/hardware.h>
26 #include <asm/arch/sys_proto.h>
27 #include <asm/arch/psu_init_gpl.h>
28 #include <asm/cache.h>
29 #include <asm/global_data.h>
31 #include <asm/ptrace.h>
32 #include <dm/device.h>
33 #include <dm/uclass.h>
35 #include <dwc3-uboot.h>
37 #include <zynqmp_firmware.h>
39 #include <linux/bitops.h>
40 #include <linux/delay.h>
41 #include <linux/sizes.h>
42 #include "../common/board.h"
44 #include "pm_cfg_obj.h"
46 DECLARE_GLOBAL_DATA_PTR;
48 #if CONFIG_IS_ENABLED(FPGA) && defined(CONFIG_FPGA_ZYNQMPPL)
49 static xilinx_desc zynqmppl = {
50 xilinx_zynqmp, csu_dma, 1, &zynqmp_op, 0, &zynqmp_op, NULL,
55 int __maybe_unused psu_uboot_init(void)
64 * PS_SYSMON_ANALOG_BUS register determines mapping between SysMon
65 * supply sense channel to SysMon supply registers inside the IP.
66 * This register must be programmed to complete SysMon IP
67 * configuration. The default register configuration after
68 * power-up is incorrect. Hence, fix this by writing the
69 * correct value - 0x3210.
71 writel(ZYNQMP_PS_SYSMON_ANALOG_BUS_VAL,
72 ZYNQMP_AMS_PS_SYSMON_ANALOG_BUS);
74 /* Delay is required for clocks to be propagated */
80 #if !defined(CONFIG_SPL_BUILD)
81 # if defined(CONFIG_DEBUG_UART_BOARD_INIT)
82 void board_debug_uart_init(void)
84 # if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
90 # if defined(CONFIG_BOARD_EARLY_INIT_F)
91 int board_early_init_f(void)
94 # if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED) && !defined(CONFIG_DEBUG_UART_BOARD_INIT)
95 ret = psu_uboot_init();
102 static int multi_boot(void)
107 ret = zynqmp_mmio_read((ulong)&csu_base->multi_boot, &multiboot);
114 #if defined(CONFIG_SPL_BUILD)
115 static void restore_jtag(void)
117 if (current_el() != 3)
120 writel(CSU_JTAG_SEC_GATE_DISABLE, &csu_base->jtag_sec);
121 writel(CSU_JTAG_DAP_ENABLE_DEBUG, &csu_base->jtag_dap_cfg);
122 writel(CSU_JTAG_CHAIN_WR_SETUP, &csu_base->jtag_chain_status_wr);
123 writel(CRLAPB_DBG_LPD_CTRL_SETUP_CLK, &crlapb_base->dbg_lpd_ctrl);
124 writel(CRLAPB_RST_LPD_DBG_RESET, &crlapb_base->rst_lpd_dbg);
125 writel(CSU_PCAP_PROG_RELEASE_PL, &csu_base->pcap_prog);
129 static void print_secure_boot(void)
133 if (zynqmp_mmio_read((ulong)&csu_base->status, &status))
136 printf("Secure Boot:\t%sauthenticated, %sencrypted\n",
137 status & ZYNQMP_CSU_STATUS_AUTHENTICATED ? "" : "not ",
138 status & ZYNQMP_CSU_STATUS_ENCRYPTED ? "" : "not ");
143 #if CONFIG_IS_ENABLED(FPGA) && defined(CONFIG_FPGA_ZYNQMPPL)
145 char name[SOC_MAX_STR_SIZE];
149 #if defined(CONFIG_SPL_BUILD)
150 /* Check *at build time* if the filename is an non-empty string */
151 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
152 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
153 zynqmp_pm_cfg_obj_size);
156 #if defined(CONFIG_ZYNQMP_FIRMWARE)
159 uclass_get_device_by_name(UCLASS_FIRMWARE, "zynqmp-power", &dev);
161 panic("PMU Firmware device not found - Enable it");
164 #if defined(CONFIG_SPL_BUILD)
165 printf("Silicon version:\t%d\n", zynqmp_get_silicon_version());
167 /* the CSU disables the JTAG interface when secure boot is enabled */
168 if (CONFIG_IS_ENABLED(ZYNQMP_RESTORE_JTAG))
171 if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM))
172 xilinx_read_eeprom();
175 printf("EL Level:\tEL%d\n", current_el());
177 #if CONFIG_IS_ENABLED(FPGA) && defined(CONFIG_FPGA_ZYNQMPPL)
180 ret = soc_get_machine(soc, name, sizeof(name));
182 zynqmppl.name = strdup(name);
184 fpga_add(fpga_xilinx, &zynqmppl);
189 /* display secure boot information */
191 if (current_el() == 3)
192 printf("Multiboot:\t%d\n", multi_boot());
197 int board_early_init_r(void)
201 if (current_el() != 3)
204 val = readl(&crlapb_base->timestamp_ref_ctrl);
205 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
208 val = readl(&crlapb_base->timestamp_ref_ctrl);
209 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
210 writel(val, &crlapb_base->timestamp_ref_ctrl);
212 /* Program freq register in System counter */
213 writel(zynqmp_get_system_timer_freq(),
214 &iou_scntr_secure->base_frequency_id_register);
215 /* And enable system counter */
216 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
217 &iou_scntr_secure->counter_control_register);
222 unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
227 if (current_el() > 1) {
230 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
233 printf("FAIL: current EL is not above EL1\n");
239 #if !defined(CFG_SYS_SDRAM_BASE) && !defined(CFG_SYS_SDRAM_SIZE)
240 int dram_init_banksize(void)
244 ret = fdtdec_setup_memory_banksize();
255 if (fdtdec_setup_mem_size_base() != 0)
262 int dram_init_banksize(void)
264 gd->bd->bi_dram[0].start = CFG_SYS_SDRAM_BASE;
265 gd->bd->bi_dram[0].size = get_effective_memsize();
274 gd->ram_size = get_ram_size((void *)CFG_SYS_SDRAM_BASE,
281 #if !CONFIG_IS_ENABLED(SYSRESET)
287 static u8 __maybe_unused zynqmp_get_bootmode(void)
293 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, ®);
297 debug("HW boot mode: %x\n", reg & BOOT_MODES_MASK);
298 debug("ALT boot mode: %x\n", reg >> BOOT_MODE_ALT_SHIFT);
300 if (reg >> BOOT_MODE_ALT_SHIFT)
301 reg >>= BOOT_MODE_ALT_SHIFT;
303 bootmode = reg & BOOT_MODES_MASK;
308 #if defined(CONFIG_BOARD_LATE_INIT)
309 static const struct {
312 } reset_reasons[] = {
313 { RESET_REASON_DEBUG_SYS, "DEBUG" },
314 { RESET_REASON_SOFT, "SOFT" },
315 { RESET_REASON_SRST, "SRST" },
316 { RESET_REASON_PSONLY, "PS-ONLY" },
317 { RESET_REASON_PMU, "PMU" },
318 { RESET_REASON_INTERNAL, "INTERNAL" },
319 { RESET_REASON_EXTERNAL, "EXTERNAL" },
323 static int reset_reason(void)
327 const char *reason = NULL;
329 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, ®);
333 puts("Reset reason:\t");
335 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
336 if (reg & reset_reasons[i].bit) {
337 reason = reset_reasons[i].name;
338 printf("%s ", reset_reasons[i].name);
345 env_set("reset_reason", reason);
350 static int set_fdtfile(void)
352 char *compatible, *fdtfile;
353 const char *suffix = ".dtb";
354 const char *vendor = "xilinx/";
357 if (env_get("fdtfile"))
360 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible",
362 if (compatible && fdt_compat_len) {
365 debug("Compatible: %s\n", compatible);
367 name = strchr(compatible, ',');
373 fdtfile = calloc(1, strlen(vendor) + strlen(name) +
378 sprintf(fdtfile, "%s%s%s", vendor, name, suffix);
380 env_set("fdtfile", fdtfile);
387 int board_late_init(void)
393 int env_targets_len = 0;
399 #if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
403 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
404 debug("Saved variables - Skipping\n");
408 if (!IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG))
415 multiboot = multi_boot();
417 env_set_hex("multiboot", multiboot);
419 bootmode = zynqmp_get_bootmode();
425 mode = "usb_dfu0 usb_dfu1";
426 env_set("modeboot", "usb_dfu_spl");
430 mode = "jtag pxe dhcp";
431 env_set("modeboot", "jtagboot");
433 case QSPI_MODE_24BIT:
434 case QSPI_MODE_32BIT:
437 env_set("modeboot", "qspiboot");
441 if (uclass_get_device_by_name(UCLASS_MMC,
442 "mmc@ff160000", &dev) &&
443 uclass_get_device_by_name(UCLASS_MMC,
444 "sdhci@ff160000", &dev)) {
445 puts("Boot from EMMC but without SD0 enabled!\n");
448 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
451 bootseq = dev_seq(dev);
452 env_set("modeboot", "emmcboot");
456 if (uclass_get_device_by_name(UCLASS_MMC,
457 "mmc@ff160000", &dev) &&
458 uclass_get_device_by_name(UCLASS_MMC,
459 "sdhci@ff160000", &dev)) {
460 puts("Boot from SD0 but without SD0 enabled!\n");
463 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
466 bootseq = dev_seq(dev);
467 env_set("modeboot", "sdboot");
474 if (uclass_get_device_by_name(UCLASS_MMC,
475 "mmc@ff170000", &dev) &&
476 uclass_get_device_by_name(UCLASS_MMC,
477 "sdhci@ff170000", &dev)) {
478 puts("Boot from SD1 but without SD1 enabled!\n");
481 debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev));
484 bootseq = dev_seq(dev);
485 env_set("modeboot", "sdboot");
490 env_set("modeboot", "nandboot");
494 printf("Invalid Boot Mode:0x%x\n", bootmode);
499 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
500 debug("Bootseq len: %x\n", bootseq_len);
501 env_set_hex("bootseq", bootseq);
505 * One terminating char + one byte for space between mode
506 * and default boot_targets
508 env_targets = env_get("boot_targets");
510 env_targets_len = strlen(env_targets);
512 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
518 sprintf(new_targets, "%s%x %s", mode, bootseq,
519 env_targets ? env_targets : "");
521 sprintf(new_targets, "%s %s", mode,
522 env_targets ? env_targets : "");
524 env_set("boot_targets", new_targets);
529 return board_late_init_xilinx();
535 puts("Board: Xilinx ZynqMP\n");
539 int mmc_get_env_dev(void)
544 switch (zynqmp_get_bootmode()) {
547 if (uclass_get_device_by_name(UCLASS_MMC,
548 "mmc@ff160000", &dev) &&
549 uclass_get_device_by_name(UCLASS_MMC,
550 "sdhci@ff160000", &dev)) {
553 bootseq = dev_seq(dev);
557 if (uclass_get_device_by_name(UCLASS_MMC,
558 "mmc@ff170000", &dev) &&
559 uclass_get_device_by_name(UCLASS_MMC,
560 "sdhci@ff170000", &dev)) {
563 bootseq = dev_seq(dev);
569 debug("bootseq %d\n", bootseq);
574 enum env_location env_get_location(enum env_operation op, int prio)
576 u32 bootmode = zynqmp_get_bootmode();
586 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
588 if (IS_ENABLED(CONFIG_ENV_IS_IN_EXT4))
592 if (IS_ENABLED(CONFIG_ENV_IS_IN_NAND))
594 if (IS_ENABLED(CONFIG_ENV_IS_IN_UBI))
597 case QSPI_MODE_24BIT:
598 case QSPI_MODE_32BIT:
599 if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
600 return ENVL_SPI_FLASH;
608 #if defined(CONFIG_SET_DFU_ALT_INFO)
610 #define DFU_ALT_BUF_LEN SZ_1K
612 void set_dfu_alt_info(char *interface, char *devstr)
614 int multiboot, bootseq = 0, len = 0;
616 ALLOC_CACHE_ALIGN_BUFFER(char, buf, DFU_ALT_BUF_LEN);
618 if (env_get("dfu_alt_info"))
621 memset(buf, 0, sizeof(buf));
623 multiboot = multi_boot();
627 multiboot = env_get_hex("multiboot", multiboot);
628 debug("Multiboot: %d\n", multiboot);
630 switch (zynqmp_get_bootmode()) {
635 bootseq = mmc_get_env_dev();
637 len += snprintf(buf + len, DFU_ALT_BUF_LEN, "mmc %d=boot",
641 len += snprintf(buf + len, DFU_ALT_BUF_LEN,
644 len += snprintf(buf + len, DFU_ALT_BUF_LEN, ".bin fat %d 1",
646 #if defined(CONFIG_SPL_FS_LOAD_PAYLOAD_NAME)
647 len += snprintf(buf + len, DFU_ALT_BUF_LEN, ";%s fat %d 1",
648 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME, bootseq);
651 case QSPI_MODE_24BIT:
652 case QSPI_MODE_32BIT:
653 len += snprintf(buf + len, DFU_ALT_BUF_LEN,
654 "sf 0:0=boot.bin raw %x 0x1500000",
656 #if defined(CONFIG_SPL_FS_LOAD_PAYLOAD_NAME) && defined(CONFIG_SYS_SPI_U_BOOT_OFFS)
657 len += snprintf(buf + len, DFU_ALT_BUF_LEN,
658 ";%s raw 0x%x 0x500000",
659 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME,
660 CONFIG_SYS_SPI_U_BOOT_OFFS);
667 env_set("dfu_alt_info", buf);
668 puts("DFU alt info setting: done\n");