1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2014 - 2015 Xilinx, Inc.
10 #include <debug_uart.h>
20 #include <asm/arch/clk.h>
21 #include <asm/arch/hardware.h>
22 #include <asm/arch/sys_proto.h>
23 #include <asm/arch/psu_init_gpl.h>
24 #include <asm/cache.h>
26 #include <asm/ptrace.h>
27 #include <dm/device.h>
28 #include <dm/uclass.h>
30 #include <dwc3-uboot.h>
32 #include <zynqmp_firmware.h>
34 #include <linux/bitops.h>
35 #include <linux/delay.h>
36 #include <linux/sizes.h>
37 #include "../common/board.h"
39 #include "pm_cfg_obj.h"
41 DECLARE_GLOBAL_DATA_PTR;
43 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
44 !defined(CONFIG_SPL_BUILD)
45 static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
52 } zynqmp_devices[] = {
144 { /* For testing purpose only */
200 int chip_id(unsigned char id)
205 if (current_el() != 3) {
206 regs.regs[0] = ZYNQMP_SIP_SVC_CSU_DMA_CHIPID;
215 * regs[0][31:0] = status of the operation
216 * regs[0][63:32] = CSU.IDCODE register
217 * regs[1][31:0] = CSU.version register
218 * regs[1][63:32] = CSU.IDCODE2 register
222 regs.regs[0] = upper_32_bits(regs.regs[0]);
223 regs.regs[0] &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
224 ZYNQMP_CSU_IDCODE_SVD_MASK;
225 regs.regs[0] >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
229 regs.regs[1] = lower_32_bits(regs.regs[1]);
230 regs.regs[1] &= ZYNQMP_CSU_SILICON_VER_MASK;
234 regs.regs[1] = lower_32_bits(regs.regs[1]);
235 regs.regs[1] >>= ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
239 printf("%s, Invalid Req:0x%x\n", __func__, id);
244 val = readl(ZYNQMP_CSU_IDCODE_ADDR);
245 val &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
246 ZYNQMP_CSU_IDCODE_SVD_MASK;
247 val >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
250 val = readl(ZYNQMP_CSU_VER_ADDR);
251 val &= ZYNQMP_CSU_SILICON_VER_MASK;
254 printf("%s, Invalid Req:0x%x\n", __func__, id);
261 #define ZYNQMP_VERSION_SIZE 9
262 #define ZYNQMP_PL_STATUS_BIT 9
263 #define ZYNQMP_IPDIS_VCU_BIT 8
264 #define ZYNQMP_PL_STATUS_MASK BIT(ZYNQMP_PL_STATUS_BIT)
265 #define ZYNQMP_CSU_VERSION_MASK ~(ZYNQMP_PL_STATUS_MASK)
266 #define ZYNQMP_CSU_VCUDIS_VER_MASK ZYNQMP_CSU_VERSION_MASK & \
267 ~BIT(ZYNQMP_IPDIS_VCU_BIT)
268 #define MAX_VARIANTS_EV 3
270 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
271 !defined(CONFIG_SPL_BUILD)
272 static char *zynqmp_get_silicon_idcode_name(void)
276 static char name[ZYNQMP_VERSION_SIZE];
278 id = chip_id(IDCODE);
279 ver = chip_id(IDCODE2);
281 for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
282 if (zynqmp_devices[i].id == id) {
283 if (zynqmp_devices[i].evexists &&
284 !(ver & ZYNQMP_PL_STATUS_MASK))
286 if (zynqmp_devices[i].ver == (ver &
287 ZYNQMP_CSU_VERSION_MASK))
292 if (i >= ARRAY_SIZE(zynqmp_devices))
295 strncat(name, "zu", 2);
296 if (!zynqmp_devices[i].evexists ||
297 (ver & ZYNQMP_PL_STATUS_MASK)) {
298 strncat(name, zynqmp_devices[i].name,
299 ZYNQMP_VERSION_SIZE - 3);
304 * Here we are means, PL not powered up and ev variant
305 * exists. So, we need to ignore VCU disable bit(8) in
306 * version and findout if its CG or EG/EV variant.
308 for (j = 0; j < MAX_VARIANTS_EV; j++, i++) {
309 if ((zynqmp_devices[i].ver & ~BIT(ZYNQMP_IPDIS_VCU_BIT)) ==
310 (ver & ZYNQMP_CSU_VCUDIS_VER_MASK)) {
311 strncat(name, zynqmp_devices[i].name,
312 ZYNQMP_VERSION_SIZE - 3);
317 if (j >= MAX_VARIANTS_EV)
320 if (strstr(name, "eg") || strstr(name, "ev")) {
321 buf = strstr(name, "e");
329 int board_early_init_f(void)
331 #if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
338 /* Delay is required for clocks to be propagated */
342 #ifdef CONFIG_DEBUG_UART
343 /* Uart debug for sure */
345 puts("Debug uart enabled\n"); /* or printch() */
351 static int multi_boot(void)
355 multiboot = readl(&csu_base->multi_boot);
357 printf("Multiboot:\t%x\n", multiboot);
362 #define PS_SYSMON_ANALOG_BUS_VAL 0x3210
363 #define PS_SYSMON_ANALOG_BUS_REG 0xFFA50914
367 #if defined(CONFIG_ZYNQMP_FIRMWARE)
370 uclass_get_device_by_name(UCLASS_FIRMWARE, "zynqmp-power", &dev);
372 panic("PMU Firmware device not found - Enable it");
375 #if defined(CONFIG_SPL_BUILD)
376 /* Check *at build time* if the filename is an non-empty string */
377 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
378 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
379 zynqmp_pm_cfg_obj_size);
382 printf("EL Level:\tEL%d\n", current_el());
384 /* Bug in ROM sets wrong value in this register */
385 writel(PS_SYSMON_ANALOG_BUS_VAL, PS_SYSMON_ANALOG_BUS_REG);
387 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
388 !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
389 defined(CONFIG_SPL_BUILD))
390 if (current_el() != 3) {
391 zynqmppl.name = zynqmp_get_silicon_idcode_name();
392 printf("Chip ID:\t%s\n", zynqmppl.name);
394 fpga_add(fpga_xilinx, &zynqmppl);
398 if (current_el() == 3)
404 int board_early_init_r(void)
408 if (current_el() != 3)
411 val = readl(&crlapb_base->timestamp_ref_ctrl);
412 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
415 val = readl(&crlapb_base->timestamp_ref_ctrl);
416 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
417 writel(val, &crlapb_base->timestamp_ref_ctrl);
419 /* Program freq register in System counter */
420 writel(zynqmp_get_system_timer_freq(),
421 &iou_scntr_secure->base_frequency_id_register);
422 /* And enable system counter */
423 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
424 &iou_scntr_secure->counter_control_register);
429 unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
434 if (current_el() > 1) {
437 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
440 printf("FAIL: current EL is not above EL1\n");
446 #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
447 int dram_init_banksize(void)
451 ret = fdtdec_setup_memory_banksize();
462 if (fdtdec_setup_mem_size_base() != 0)
468 int dram_init_banksize(void)
470 #if defined(CONFIG_NR_DRAM_BANKS)
471 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
472 gd->bd->bi_dram[0].size = get_effective_memsize();
482 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
483 CONFIG_SYS_SDRAM_SIZE);
489 void reset_cpu(ulong addr)
493 #if defined(CONFIG_BOARD_LATE_INIT)
494 static const struct {
497 } reset_reasons[] = {
498 { RESET_REASON_DEBUG_SYS, "DEBUG" },
499 { RESET_REASON_SOFT, "SOFT" },
500 { RESET_REASON_SRST, "SRST" },
501 { RESET_REASON_PSONLY, "PS-ONLY" },
502 { RESET_REASON_PMU, "PMU" },
503 { RESET_REASON_INTERNAL, "INTERNAL" },
504 { RESET_REASON_EXTERNAL, "EXTERNAL" },
508 static int reset_reason(void)
512 const char *reason = NULL;
514 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, ®);
518 puts("Reset reason:\t");
520 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
521 if (reg & reset_reasons[i].bit) {
522 reason = reset_reasons[i].name;
523 printf("%s ", reset_reasons[i].name);
530 env_set("reset_reason", reason);
532 ret = zynqmp_mmio_write((ulong)&crlapb_base->reset_reason, ~0, ~0);
539 static int set_fdtfile(void)
541 char *compatible, *fdtfile;
542 const char *suffix = ".dtb";
543 const char *vendor = "xilinx/";
545 if (env_get("fdtfile"))
548 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible", NULL);
550 debug("Compatible: %s\n", compatible);
552 /* Discard vendor prefix */
553 strsep(&compatible, ",");
555 fdtfile = calloc(1, strlen(vendor) + strlen(compatible) +
560 sprintf(fdtfile, "%s%s%s", vendor, compatible, suffix);
562 env_set("fdtfile", fdtfile);
569 static u8 zynqmp_get_bootmode(void)
575 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, ®);
579 if (reg >> BOOT_MODE_ALT_SHIFT)
580 reg >>= BOOT_MODE_ALT_SHIFT;
582 bootmode = reg & BOOT_MODES_MASK;
587 int board_late_init(void)
593 int env_targets_len = 0;
599 #if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
603 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
604 debug("Saved variables - Skipping\n");
612 bootmode = zynqmp_get_bootmode();
619 env_set("modeboot", "usb_dfu_spl");
623 mode = "jtag pxe dhcp";
624 env_set("modeboot", "jtagboot");
626 case QSPI_MODE_24BIT:
627 case QSPI_MODE_32BIT:
630 env_set("modeboot", "qspiboot");
634 if (uclass_get_device_by_name(UCLASS_MMC,
635 "mmc@ff160000", &dev) &&
636 uclass_get_device_by_name(UCLASS_MMC,
637 "sdhci@ff160000", &dev)) {
638 puts("Boot from EMMC but without SD0 enabled!\n");
641 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
648 if (uclass_get_device_by_name(UCLASS_MMC,
649 "mmc@ff160000", &dev) &&
650 uclass_get_device_by_name(UCLASS_MMC,
651 "sdhci@ff160000", &dev)) {
652 puts("Boot from SD0 but without SD0 enabled!\n");
655 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
659 env_set("modeboot", "sdboot");
666 if (uclass_get_device_by_name(UCLASS_MMC,
667 "mmc@ff170000", &dev) &&
668 uclass_get_device_by_name(UCLASS_MMC,
669 "sdhci@ff170000", &dev)) {
670 puts("Boot from SD1 but without SD1 enabled!\n");
673 debug("mmc1 device found at %p, seq %d\n", dev, dev->seq);
677 env_set("modeboot", "sdboot");
682 env_set("modeboot", "nandboot");
686 printf("Invalid Boot Mode:0x%x\n", bootmode);
691 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
692 debug("Bootseq len: %x\n", bootseq_len);
696 * One terminating char + one byte for space between mode
697 * and default boot_targets
699 env_targets = env_get("boot_targets");
701 env_targets_len = strlen(env_targets);
703 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
709 sprintf(new_targets, "%s%x %s", mode, bootseq,
710 env_targets ? env_targets : "");
712 sprintf(new_targets, "%s %s", mode,
713 env_targets ? env_targets : "");
715 env_set("boot_targets", new_targets);
719 return board_late_init_xilinx();
725 puts("Board: Xilinx ZynqMP\n");