1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2014 - 2015 Xilinx, Inc.
4 * Michal Simek <michal.simek@xilinx.com>
10 #include <debug_uart.h>
19 #include <asm/arch/clk.h>
20 #include <asm/arch/hardware.h>
21 #include <asm/arch/sys_proto.h>
22 #include <asm/arch/psu_init_gpl.h>
23 #include <asm/cache.h>
25 #include <dm/device.h>
26 #include <dm/uclass.h>
28 #include <dwc3-uboot.h>
30 #include <zynqmp_firmware.h>
32 #include "../common/board.h"
34 #include "pm_cfg_obj.h"
36 DECLARE_GLOBAL_DATA_PTR;
38 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
39 !defined(CONFIG_SPL_BUILD)
40 static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
47 } zynqmp_devices[] = {
139 { /* For testing purpose only */
195 int chip_id(unsigned char id)
200 if (current_el() != 3) {
201 regs.regs[0] = ZYNQMP_SIP_SVC_CSU_DMA_CHIPID;
210 * regs[0][31:0] = status of the operation
211 * regs[0][63:32] = CSU.IDCODE register
212 * regs[1][31:0] = CSU.version register
213 * regs[1][63:32] = CSU.IDCODE2 register
217 regs.regs[0] = upper_32_bits(regs.regs[0]);
218 regs.regs[0] &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
219 ZYNQMP_CSU_IDCODE_SVD_MASK;
220 regs.regs[0] >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
224 regs.regs[1] = lower_32_bits(regs.regs[1]);
225 regs.regs[1] &= ZYNQMP_CSU_SILICON_VER_MASK;
229 regs.regs[1] = lower_32_bits(regs.regs[1]);
230 regs.regs[1] >>= ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
234 printf("%s, Invalid Req:0x%x\n", __func__, id);
239 val = readl(ZYNQMP_CSU_IDCODE_ADDR);
240 val &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
241 ZYNQMP_CSU_IDCODE_SVD_MASK;
242 val >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
245 val = readl(ZYNQMP_CSU_VER_ADDR);
246 val &= ZYNQMP_CSU_SILICON_VER_MASK;
249 printf("%s, Invalid Req:0x%x\n", __func__, id);
256 #define ZYNQMP_VERSION_SIZE 9
257 #define ZYNQMP_PL_STATUS_BIT 9
258 #define ZYNQMP_IPDIS_VCU_BIT 8
259 #define ZYNQMP_PL_STATUS_MASK BIT(ZYNQMP_PL_STATUS_BIT)
260 #define ZYNQMP_CSU_VERSION_MASK ~(ZYNQMP_PL_STATUS_MASK)
261 #define ZYNQMP_CSU_VCUDIS_VER_MASK ZYNQMP_CSU_VERSION_MASK & \
262 ~BIT(ZYNQMP_IPDIS_VCU_BIT)
263 #define MAX_VARIANTS_EV 3
265 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
266 !defined(CONFIG_SPL_BUILD)
267 static char *zynqmp_get_silicon_idcode_name(void)
271 static char name[ZYNQMP_VERSION_SIZE];
273 id = chip_id(IDCODE);
274 ver = chip_id(IDCODE2);
276 for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
277 if (zynqmp_devices[i].id == id) {
278 if (zynqmp_devices[i].evexists &&
279 !(ver & ZYNQMP_PL_STATUS_MASK))
281 if (zynqmp_devices[i].ver == (ver &
282 ZYNQMP_CSU_VERSION_MASK))
287 if (i >= ARRAY_SIZE(zynqmp_devices))
290 strncat(name, "zu", 2);
291 if (!zynqmp_devices[i].evexists ||
292 (ver & ZYNQMP_PL_STATUS_MASK)) {
293 strncat(name, zynqmp_devices[i].name,
294 ZYNQMP_VERSION_SIZE - 3);
299 * Here we are means, PL not powered up and ev variant
300 * exists. So, we need to ignore VCU disable bit(8) in
301 * version and findout if its CG or EG/EV variant.
303 for (j = 0; j < MAX_VARIANTS_EV; j++, i++) {
304 if ((zynqmp_devices[i].ver & ~BIT(ZYNQMP_IPDIS_VCU_BIT)) ==
305 (ver & ZYNQMP_CSU_VCUDIS_VER_MASK)) {
306 strncat(name, zynqmp_devices[i].name,
307 ZYNQMP_VERSION_SIZE - 3);
312 if (j >= MAX_VARIANTS_EV)
315 if (strstr(name, "eg") || strstr(name, "ev")) {
316 buf = strstr(name, "e");
324 int board_early_init_f(void)
326 #if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
333 /* Delay is required for clocks to be propagated */
337 #ifdef CONFIG_DEBUG_UART
338 /* Uart debug for sure */
340 puts("Debug uart enabled\n"); /* or printch() */
346 static int multi_boot(void)
350 multiboot = readl(&csu_base->multi_boot);
352 printf("Multiboot:\t%x\n", multiboot);
359 #if defined(CONFIG_ZYNQMP_FIRMWARE)
362 uclass_get_device_by_name(UCLASS_FIRMWARE, "zynqmp-power", &dev);
364 panic("PMU Firmware device not found - Enable it");
367 #if defined(CONFIG_SPL_BUILD)
368 /* Check *at build time* if the filename is an non-empty string */
369 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
370 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
371 zynqmp_pm_cfg_obj_size);
374 printf("EL Level:\tEL%d\n", current_el());
376 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
377 !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
378 defined(CONFIG_SPL_BUILD))
379 if (current_el() != 3) {
380 zynqmppl.name = zynqmp_get_silicon_idcode_name();
381 printf("Chip ID:\t%s\n", zynqmppl.name);
383 fpga_add(fpga_xilinx, &zynqmppl);
387 if (current_el() == 3)
393 int board_early_init_r(void)
397 if (current_el() != 3)
400 val = readl(&crlapb_base->timestamp_ref_ctrl);
401 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
404 val = readl(&crlapb_base->timestamp_ref_ctrl);
405 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
406 writel(val, &crlapb_base->timestamp_ref_ctrl);
408 /* Program freq register in System counter */
409 writel(zynqmp_get_system_timer_freq(),
410 &iou_scntr_secure->base_frequency_id_register);
411 /* And enable system counter */
412 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
413 &iou_scntr_secure->counter_control_register);
418 unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
423 if (current_el() > 1) {
426 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
429 printf("FAIL: current EL is not above EL1\n");
435 #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
436 int dram_init_banksize(void)
440 ret = fdtdec_setup_memory_banksize();
451 if (fdtdec_setup_mem_size_base() != 0)
457 int dram_init_banksize(void)
459 #if defined(CONFIG_NR_DRAM_BANKS)
460 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
461 gd->bd->bi_dram[0].size = get_effective_memsize();
471 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
472 CONFIG_SYS_SDRAM_SIZE);
478 void reset_cpu(ulong addr)
482 #if defined(CONFIG_BOARD_LATE_INIT)
483 static const struct {
486 } reset_reasons[] = {
487 { RESET_REASON_DEBUG_SYS, "DEBUG" },
488 { RESET_REASON_SOFT, "SOFT" },
489 { RESET_REASON_SRST, "SRST" },
490 { RESET_REASON_PSONLY, "PS-ONLY" },
491 { RESET_REASON_PMU, "PMU" },
492 { RESET_REASON_INTERNAL, "INTERNAL" },
493 { RESET_REASON_EXTERNAL, "EXTERNAL" },
497 static int reset_reason(void)
501 const char *reason = NULL;
503 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, ®);
507 puts("Reset reason:\t");
509 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
510 if (reg & reset_reasons[i].bit) {
511 reason = reset_reasons[i].name;
512 printf("%s ", reset_reasons[i].name);
519 env_set("reset_reason", reason);
521 ret = zynqmp_mmio_write((ulong)&crlapb_base->reset_reason, ~0, ~0);
528 static int set_fdtfile(void)
530 char *compatible, *fdtfile;
531 const char *suffix = ".dtb";
532 const char *vendor = "xilinx/";
534 if (env_get("fdtfile"))
537 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible", NULL);
539 debug("Compatible: %s\n", compatible);
541 /* Discard vendor prefix */
542 strsep(&compatible, ",");
544 fdtfile = calloc(1, strlen(vendor) + strlen(compatible) +
549 sprintf(fdtfile, "%s%s%s", vendor, compatible, suffix);
551 env_set("fdtfile", fdtfile);
558 static u8 zynqmp_get_bootmode(void)
564 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, ®);
568 if (reg >> BOOT_MODE_ALT_SHIFT)
569 reg >>= BOOT_MODE_ALT_SHIFT;
571 bootmode = reg & BOOT_MODES_MASK;
576 int board_late_init(void)
582 int env_targets_len = 0;
588 #if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
592 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
593 debug("Saved variables - Skipping\n");
601 bootmode = zynqmp_get_bootmode();
608 env_set("modeboot", "usb_dfu_spl");
612 mode = "jtag pxe dhcp";
613 env_set("modeboot", "jtagboot");
615 case QSPI_MODE_24BIT:
616 case QSPI_MODE_32BIT:
619 env_set("modeboot", "qspiboot");
623 if (uclass_get_device_by_name(UCLASS_MMC,
624 "mmc@ff160000", &dev) &&
625 uclass_get_device_by_name(UCLASS_MMC,
626 "sdhci@ff160000", &dev)) {
627 puts("Boot from EMMC but without SD0 enabled!\n");
630 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
637 if (uclass_get_device_by_name(UCLASS_MMC,
638 "mmc@ff160000", &dev) &&
639 uclass_get_device_by_name(UCLASS_MMC,
640 "sdhci@ff160000", &dev)) {
641 puts("Boot from SD0 but without SD0 enabled!\n");
644 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
648 env_set("modeboot", "sdboot");
655 if (uclass_get_device_by_name(UCLASS_MMC,
656 "mmc@ff170000", &dev) &&
657 uclass_get_device_by_name(UCLASS_MMC,
658 "sdhci@ff170000", &dev)) {
659 puts("Boot from SD1 but without SD1 enabled!\n");
662 debug("mmc1 device found at %p, seq %d\n", dev, dev->seq);
666 env_set("modeboot", "sdboot");
671 env_set("modeboot", "nandboot");
675 printf("Invalid Boot Mode:0x%x\n", bootmode);
680 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
681 debug("Bootseq len: %x\n", bootseq_len);
685 * One terminating char + one byte for space between mode
686 * and default boot_targets
688 env_targets = env_get("boot_targets");
690 env_targets_len = strlen(env_targets);
692 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
698 sprintf(new_targets, "%s%x %s", mode, bootseq,
699 env_targets ? env_targets : "");
701 sprintf(new_targets, "%s %s", mode,
702 env_targets ? env_targets : "");
704 env_set("boot_targets", new_targets);
708 return board_late_init_xilinx();
714 puts("Board: Xilinx ZynqMP\n");