1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2014 - 2015 Xilinx, Inc.
9 #include <debug_uart.h>
18 #include <asm/arch/clk.h>
19 #include <asm/arch/hardware.h>
20 #include <asm/arch/sys_proto.h>
21 #include <asm/arch/psu_init_gpl.h>
22 #include <asm/cache.h>
24 #include <dm/device.h>
25 #include <dm/uclass.h>
27 #include <dwc3-uboot.h>
29 #include <zynqmp_firmware.h>
31 #include "../common/board.h"
33 #include "pm_cfg_obj.h"
35 DECLARE_GLOBAL_DATA_PTR;
37 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
38 !defined(CONFIG_SPL_BUILD)
39 static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
46 } zynqmp_devices[] = {
138 { /* For testing purpose only */
194 int chip_id(unsigned char id)
199 if (current_el() != 3) {
200 regs.regs[0] = ZYNQMP_SIP_SVC_CSU_DMA_CHIPID;
209 * regs[0][31:0] = status of the operation
210 * regs[0][63:32] = CSU.IDCODE register
211 * regs[1][31:0] = CSU.version register
212 * regs[1][63:32] = CSU.IDCODE2 register
216 regs.regs[0] = upper_32_bits(regs.regs[0]);
217 regs.regs[0] &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
218 ZYNQMP_CSU_IDCODE_SVD_MASK;
219 regs.regs[0] >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
223 regs.regs[1] = lower_32_bits(regs.regs[1]);
224 regs.regs[1] &= ZYNQMP_CSU_SILICON_VER_MASK;
228 regs.regs[1] = lower_32_bits(regs.regs[1]);
229 regs.regs[1] >>= ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
233 printf("%s, Invalid Req:0x%x\n", __func__, id);
238 val = readl(ZYNQMP_CSU_IDCODE_ADDR);
239 val &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
240 ZYNQMP_CSU_IDCODE_SVD_MASK;
241 val >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
244 val = readl(ZYNQMP_CSU_VER_ADDR);
245 val &= ZYNQMP_CSU_SILICON_VER_MASK;
248 printf("%s, Invalid Req:0x%x\n", __func__, id);
255 #define ZYNQMP_VERSION_SIZE 9
256 #define ZYNQMP_PL_STATUS_BIT 9
257 #define ZYNQMP_IPDIS_VCU_BIT 8
258 #define ZYNQMP_PL_STATUS_MASK BIT(ZYNQMP_PL_STATUS_BIT)
259 #define ZYNQMP_CSU_VERSION_MASK ~(ZYNQMP_PL_STATUS_MASK)
260 #define ZYNQMP_CSU_VCUDIS_VER_MASK ZYNQMP_CSU_VERSION_MASK & \
261 ~BIT(ZYNQMP_IPDIS_VCU_BIT)
262 #define MAX_VARIANTS_EV 3
264 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
265 !defined(CONFIG_SPL_BUILD)
266 static char *zynqmp_get_silicon_idcode_name(void)
270 static char name[ZYNQMP_VERSION_SIZE];
272 id = chip_id(IDCODE);
273 ver = chip_id(IDCODE2);
275 for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
276 if (zynqmp_devices[i].id == id) {
277 if (zynqmp_devices[i].evexists &&
278 !(ver & ZYNQMP_PL_STATUS_MASK))
280 if (zynqmp_devices[i].ver == (ver &
281 ZYNQMP_CSU_VERSION_MASK))
286 if (i >= ARRAY_SIZE(zynqmp_devices))
289 strncat(name, "zu", 2);
290 if (!zynqmp_devices[i].evexists ||
291 (ver & ZYNQMP_PL_STATUS_MASK)) {
292 strncat(name, zynqmp_devices[i].name,
293 ZYNQMP_VERSION_SIZE - 3);
298 * Here we are means, PL not powered up and ev variant
299 * exists. So, we need to ignore VCU disable bit(8) in
300 * version and findout if its CG or EG/EV variant.
302 for (j = 0; j < MAX_VARIANTS_EV; j++, i++) {
303 if ((zynqmp_devices[i].ver & ~BIT(ZYNQMP_IPDIS_VCU_BIT)) ==
304 (ver & ZYNQMP_CSU_VCUDIS_VER_MASK)) {
305 strncat(name, zynqmp_devices[i].name,
306 ZYNQMP_VERSION_SIZE - 3);
311 if (j >= MAX_VARIANTS_EV)
314 if (strstr(name, "eg") || strstr(name, "ev")) {
315 buf = strstr(name, "e");
323 int board_early_init_f(void)
325 #if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
332 /* Delay is required for clocks to be propagated */
336 #ifdef CONFIG_DEBUG_UART
337 /* Uart debug for sure */
339 puts("Debug uart enabled\n"); /* or printch() */
345 static int multi_boot(void)
349 multiboot = readl(&csu_base->multi_boot);
351 printf("Multiboot:\t%x\n", multiboot);
358 #if defined(CONFIG_ZYNQMP_FIRMWARE)
361 uclass_get_device_by_name(UCLASS_FIRMWARE, "zynqmp-power", &dev);
363 panic("PMU Firmware device not found - Enable it");
366 #if defined(CONFIG_SPL_BUILD)
367 /* Check *at build time* if the filename is an non-empty string */
368 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
369 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
370 zynqmp_pm_cfg_obj_size);
373 printf("EL Level:\tEL%d\n", current_el());
375 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
376 !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
377 defined(CONFIG_SPL_BUILD))
378 if (current_el() != 3) {
379 zynqmppl.name = zynqmp_get_silicon_idcode_name();
380 printf("Chip ID:\t%s\n", zynqmppl.name);
382 fpga_add(fpga_xilinx, &zynqmppl);
386 if (current_el() == 3)
392 int board_early_init_r(void)
396 if (current_el() != 3)
399 val = readl(&crlapb_base->timestamp_ref_ctrl);
400 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
403 val = readl(&crlapb_base->timestamp_ref_ctrl);
404 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
405 writel(val, &crlapb_base->timestamp_ref_ctrl);
407 /* Program freq register in System counter */
408 writel(zynqmp_get_system_timer_freq(),
409 &iou_scntr_secure->base_frequency_id_register);
410 /* And enable system counter */
411 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
412 &iou_scntr_secure->counter_control_register);
417 unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
422 if (current_el() > 1) {
425 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
428 printf("FAIL: current EL is not above EL1\n");
434 #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
435 int dram_init_banksize(void)
439 ret = fdtdec_setup_memory_banksize();
450 if (fdtdec_setup_mem_size_base() != 0)
456 int dram_init_banksize(void)
458 #if defined(CONFIG_NR_DRAM_BANKS)
459 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
460 gd->bd->bi_dram[0].size = get_effective_memsize();
470 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
471 CONFIG_SYS_SDRAM_SIZE);
477 void reset_cpu(ulong addr)
481 #if defined(CONFIG_BOARD_LATE_INIT)
482 static const struct {
485 } reset_reasons[] = {
486 { RESET_REASON_DEBUG_SYS, "DEBUG" },
487 { RESET_REASON_SOFT, "SOFT" },
488 { RESET_REASON_SRST, "SRST" },
489 { RESET_REASON_PSONLY, "PS-ONLY" },
490 { RESET_REASON_PMU, "PMU" },
491 { RESET_REASON_INTERNAL, "INTERNAL" },
492 { RESET_REASON_EXTERNAL, "EXTERNAL" },
496 static int reset_reason(void)
500 const char *reason = NULL;
502 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, ®);
506 puts("Reset reason:\t");
508 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
509 if (reg & reset_reasons[i].bit) {
510 reason = reset_reasons[i].name;
511 printf("%s ", reset_reasons[i].name);
518 env_set("reset_reason", reason);
520 ret = zynqmp_mmio_write((ulong)&crlapb_base->reset_reason, ~0, ~0);
527 static int set_fdtfile(void)
529 char *compatible, *fdtfile;
530 const char *suffix = ".dtb";
531 const char *vendor = "xilinx/";
533 if (env_get("fdtfile"))
536 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible", NULL);
538 debug("Compatible: %s\n", compatible);
540 /* Discard vendor prefix */
541 strsep(&compatible, ",");
543 fdtfile = calloc(1, strlen(vendor) + strlen(compatible) +
548 sprintf(fdtfile, "%s%s%s", vendor, compatible, suffix);
550 env_set("fdtfile", fdtfile);
557 static u8 zynqmp_get_bootmode(void)
563 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, ®);
567 if (reg >> BOOT_MODE_ALT_SHIFT)
568 reg >>= BOOT_MODE_ALT_SHIFT;
570 bootmode = reg & BOOT_MODES_MASK;
575 int board_late_init(void)
581 int env_targets_len = 0;
587 #if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
591 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
592 debug("Saved variables - Skipping\n");
600 bootmode = zynqmp_get_bootmode();
607 env_set("modeboot", "usb_dfu_spl");
611 mode = "jtag pxe dhcp";
612 env_set("modeboot", "jtagboot");
614 case QSPI_MODE_24BIT:
615 case QSPI_MODE_32BIT:
618 env_set("modeboot", "qspiboot");
622 if (uclass_get_device_by_name(UCLASS_MMC,
623 "mmc@ff160000", &dev) &&
624 uclass_get_device_by_name(UCLASS_MMC,
625 "sdhci@ff160000", &dev)) {
626 puts("Boot from EMMC but without SD0 enabled!\n");
629 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
636 if (uclass_get_device_by_name(UCLASS_MMC,
637 "mmc@ff160000", &dev) &&
638 uclass_get_device_by_name(UCLASS_MMC,
639 "sdhci@ff160000", &dev)) {
640 puts("Boot from SD0 but without SD0 enabled!\n");
643 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
647 env_set("modeboot", "sdboot");
654 if (uclass_get_device_by_name(UCLASS_MMC,
655 "mmc@ff170000", &dev) &&
656 uclass_get_device_by_name(UCLASS_MMC,
657 "sdhci@ff170000", &dev)) {
658 puts("Boot from SD1 but without SD1 enabled!\n");
661 debug("mmc1 device found at %p, seq %d\n", dev, dev->seq);
665 env_set("modeboot", "sdboot");
670 env_set("modeboot", "nandboot");
674 printf("Invalid Boot Mode:0x%x\n", bootmode);
679 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
680 debug("Bootseq len: %x\n", bootseq_len);
684 * One terminating char + one byte for space between mode
685 * and default boot_targets
687 env_targets = env_get("boot_targets");
689 env_targets_len = strlen(env_targets);
691 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
697 sprintf(new_targets, "%s%x %s", mode, bootseq,
698 env_targets ? env_targets : "");
700 sprintf(new_targets, "%s %s", mode,
701 env_targets ? env_targets : "");
703 env_set("boot_targets", new_targets);
707 return board_late_init_xilinx();
713 puts("Board: Xilinx ZynqMP\n");