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>
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 "../common/board.h"
36 #include "pm_cfg_obj.h"
38 DECLARE_GLOBAL_DATA_PTR;
40 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
41 !defined(CONFIG_SPL_BUILD)
42 static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
49 } zynqmp_devices[] = {
141 { /* For testing purpose only */
197 int chip_id(unsigned char id)
202 if (current_el() != 3) {
203 regs.regs[0] = ZYNQMP_SIP_SVC_CSU_DMA_CHIPID;
212 * regs[0][31:0] = status of the operation
213 * regs[0][63:32] = CSU.IDCODE register
214 * regs[1][31:0] = CSU.version register
215 * regs[1][63:32] = CSU.IDCODE2 register
219 regs.regs[0] = upper_32_bits(regs.regs[0]);
220 regs.regs[0] &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
221 ZYNQMP_CSU_IDCODE_SVD_MASK;
222 regs.regs[0] >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
226 regs.regs[1] = lower_32_bits(regs.regs[1]);
227 regs.regs[1] &= ZYNQMP_CSU_SILICON_VER_MASK;
231 regs.regs[1] = lower_32_bits(regs.regs[1]);
232 regs.regs[1] >>= ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
236 printf("%s, Invalid Req:0x%x\n", __func__, id);
241 val = readl(ZYNQMP_CSU_IDCODE_ADDR);
242 val &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
243 ZYNQMP_CSU_IDCODE_SVD_MASK;
244 val >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
247 val = readl(ZYNQMP_CSU_VER_ADDR);
248 val &= ZYNQMP_CSU_SILICON_VER_MASK;
251 printf("%s, Invalid Req:0x%x\n", __func__, id);
258 #define ZYNQMP_VERSION_SIZE 9
259 #define ZYNQMP_PL_STATUS_BIT 9
260 #define ZYNQMP_IPDIS_VCU_BIT 8
261 #define ZYNQMP_PL_STATUS_MASK BIT(ZYNQMP_PL_STATUS_BIT)
262 #define ZYNQMP_CSU_VERSION_MASK ~(ZYNQMP_PL_STATUS_MASK)
263 #define ZYNQMP_CSU_VCUDIS_VER_MASK ZYNQMP_CSU_VERSION_MASK & \
264 ~BIT(ZYNQMP_IPDIS_VCU_BIT)
265 #define MAX_VARIANTS_EV 3
267 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
268 !defined(CONFIG_SPL_BUILD)
269 static char *zynqmp_get_silicon_idcode_name(void)
273 static char name[ZYNQMP_VERSION_SIZE];
275 id = chip_id(IDCODE);
276 ver = chip_id(IDCODE2);
278 for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
279 if (zynqmp_devices[i].id == id) {
280 if (zynqmp_devices[i].evexists &&
281 !(ver & ZYNQMP_PL_STATUS_MASK))
283 if (zynqmp_devices[i].ver == (ver &
284 ZYNQMP_CSU_VERSION_MASK))
289 if (i >= ARRAY_SIZE(zynqmp_devices))
292 strncat(name, "zu", 2);
293 if (!zynqmp_devices[i].evexists ||
294 (ver & ZYNQMP_PL_STATUS_MASK)) {
295 strncat(name, zynqmp_devices[i].name,
296 ZYNQMP_VERSION_SIZE - 3);
301 * Here we are means, PL not powered up and ev variant
302 * exists. So, we need to ignore VCU disable bit(8) in
303 * version and findout if its CG or EG/EV variant.
305 for (j = 0; j < MAX_VARIANTS_EV; j++, i++) {
306 if ((zynqmp_devices[i].ver & ~BIT(ZYNQMP_IPDIS_VCU_BIT)) ==
307 (ver & ZYNQMP_CSU_VCUDIS_VER_MASK)) {
308 strncat(name, zynqmp_devices[i].name,
309 ZYNQMP_VERSION_SIZE - 3);
314 if (j >= MAX_VARIANTS_EV)
317 if (strstr(name, "eg") || strstr(name, "ev")) {
318 buf = strstr(name, "e");
326 int board_early_init_f(void)
328 #if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
335 /* Delay is required for clocks to be propagated */
339 #ifdef CONFIG_DEBUG_UART
340 /* Uart debug for sure */
342 puts("Debug uart enabled\n"); /* or printch() */
348 static int multi_boot(void)
352 multiboot = readl(&csu_base->multi_boot);
354 printf("Multiboot:\t%x\n", multiboot);
361 #if defined(CONFIG_ZYNQMP_FIRMWARE)
364 uclass_get_device_by_name(UCLASS_FIRMWARE, "zynqmp-power", &dev);
366 panic("PMU Firmware device not found - Enable it");
369 #if defined(CONFIG_SPL_BUILD)
370 /* Check *at build time* if the filename is an non-empty string */
371 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
372 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
373 zynqmp_pm_cfg_obj_size);
376 printf("EL Level:\tEL%d\n", current_el());
378 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
379 !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
380 defined(CONFIG_SPL_BUILD))
381 if (current_el() != 3) {
382 zynqmppl.name = zynqmp_get_silicon_idcode_name();
383 printf("Chip ID:\t%s\n", zynqmppl.name);
385 fpga_add(fpga_xilinx, &zynqmppl);
389 if (current_el() == 3)
395 int board_early_init_r(void)
399 if (current_el() != 3)
402 val = readl(&crlapb_base->timestamp_ref_ctrl);
403 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
406 val = readl(&crlapb_base->timestamp_ref_ctrl);
407 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
408 writel(val, &crlapb_base->timestamp_ref_ctrl);
410 /* Program freq register in System counter */
411 writel(zynqmp_get_system_timer_freq(),
412 &iou_scntr_secure->base_frequency_id_register);
413 /* And enable system counter */
414 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
415 &iou_scntr_secure->counter_control_register);
420 unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
425 if (current_el() > 1) {
428 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
431 printf("FAIL: current EL is not above EL1\n");
437 #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
438 int dram_init_banksize(void)
442 ret = fdtdec_setup_memory_banksize();
453 if (fdtdec_setup_mem_size_base() != 0)
459 int dram_init_banksize(void)
461 #if defined(CONFIG_NR_DRAM_BANKS)
462 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
463 gd->bd->bi_dram[0].size = get_effective_memsize();
473 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
474 CONFIG_SYS_SDRAM_SIZE);
480 void reset_cpu(ulong addr)
484 #if defined(CONFIG_BOARD_LATE_INIT)
485 static const struct {
488 } reset_reasons[] = {
489 { RESET_REASON_DEBUG_SYS, "DEBUG" },
490 { RESET_REASON_SOFT, "SOFT" },
491 { RESET_REASON_SRST, "SRST" },
492 { RESET_REASON_PSONLY, "PS-ONLY" },
493 { RESET_REASON_PMU, "PMU" },
494 { RESET_REASON_INTERNAL, "INTERNAL" },
495 { RESET_REASON_EXTERNAL, "EXTERNAL" },
499 static int reset_reason(void)
503 const char *reason = NULL;
505 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, ®);
509 puts("Reset reason:\t");
511 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
512 if (reg & reset_reasons[i].bit) {
513 reason = reset_reasons[i].name;
514 printf("%s ", reset_reasons[i].name);
521 env_set("reset_reason", reason);
523 ret = zynqmp_mmio_write((ulong)&crlapb_base->reset_reason, ~0, ~0);
530 static int set_fdtfile(void)
532 char *compatible, *fdtfile;
533 const char *suffix = ".dtb";
534 const char *vendor = "xilinx/";
536 if (env_get("fdtfile"))
539 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible", NULL);
541 debug("Compatible: %s\n", compatible);
543 /* Discard vendor prefix */
544 strsep(&compatible, ",");
546 fdtfile = calloc(1, strlen(vendor) + strlen(compatible) +
551 sprintf(fdtfile, "%s%s%s", vendor, compatible, suffix);
553 env_set("fdtfile", fdtfile);
560 static u8 zynqmp_get_bootmode(void)
566 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, ®);
570 if (reg >> BOOT_MODE_ALT_SHIFT)
571 reg >>= BOOT_MODE_ALT_SHIFT;
573 bootmode = reg & BOOT_MODES_MASK;
578 int board_late_init(void)
584 int env_targets_len = 0;
590 #if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
594 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
595 debug("Saved variables - Skipping\n");
603 bootmode = zynqmp_get_bootmode();
610 env_set("modeboot", "usb_dfu_spl");
614 mode = "jtag pxe dhcp";
615 env_set("modeboot", "jtagboot");
617 case QSPI_MODE_24BIT:
618 case QSPI_MODE_32BIT:
621 env_set("modeboot", "qspiboot");
625 if (uclass_get_device_by_name(UCLASS_MMC,
626 "mmc@ff160000", &dev) &&
627 uclass_get_device_by_name(UCLASS_MMC,
628 "sdhci@ff160000", &dev)) {
629 puts("Boot from EMMC but without SD0 enabled!\n");
632 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
639 if (uclass_get_device_by_name(UCLASS_MMC,
640 "mmc@ff160000", &dev) &&
641 uclass_get_device_by_name(UCLASS_MMC,
642 "sdhci@ff160000", &dev)) {
643 puts("Boot from SD0 but without SD0 enabled!\n");
646 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
650 env_set("modeboot", "sdboot");
657 if (uclass_get_device_by_name(UCLASS_MMC,
658 "mmc@ff170000", &dev) &&
659 uclass_get_device_by_name(UCLASS_MMC,
660 "sdhci@ff170000", &dev)) {
661 puts("Boot from SD1 but without SD1 enabled!\n");
664 debug("mmc1 device found at %p, seq %d\n", dev, dev->seq);
668 env_set("modeboot", "sdboot");
673 env_set("modeboot", "nandboot");
677 printf("Invalid Boot Mode:0x%x\n", bootmode);
682 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
683 debug("Bootseq len: %x\n", bootseq_len);
687 * One terminating char + one byte for space between mode
688 * and default boot_targets
690 env_targets = env_get("boot_targets");
692 env_targets_len = strlen(env_targets);
694 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
700 sprintf(new_targets, "%s%x %s", mode, bootseq,
701 env_targets ? env_targets : "");
703 sprintf(new_targets, "%s %s", mode,
704 env_targets ? env_targets : "");
706 env_set("boot_targets", new_targets);
710 return board_late_init_xilinx();
716 puts("Board: Xilinx ZynqMP\n");