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 <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)
203 u32 ret_payload[PAYLOAD_ARG_CNT];
205 xilinx_pm_request(PM_GET_CHIPID, 0, 0, 0, 0, ret_payload);
209 * payload[0][31:0] = status of the operation
210 * payload[1]] = IDCODE
211 * payload[2][19:0] = Version
212 * payload[2][28:20] = EXTENDED_IDCODE
213 * payload[2][29] = PL_INIT
217 val = ret_payload[1];
218 val &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
219 ZYNQMP_CSU_IDCODE_SVD_MASK;
220 val >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
223 val = ret_payload[2] & ZYNQMP_CSU_SILICON_VER_MASK;
226 val = ret_payload[2] >> ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
229 printf("%s, Invalid Req:0x%x\n", __func__, id);
235 #define ZYNQMP_VERSION_SIZE 9
236 #define ZYNQMP_PL_STATUS_BIT 9
237 #define ZYNQMP_IPDIS_VCU_BIT 8
238 #define ZYNQMP_PL_STATUS_MASK BIT(ZYNQMP_PL_STATUS_BIT)
239 #define ZYNQMP_CSU_VERSION_MASK ~(ZYNQMP_PL_STATUS_MASK)
240 #define ZYNQMP_CSU_VCUDIS_VER_MASK ZYNQMP_CSU_VERSION_MASK & \
241 ~BIT(ZYNQMP_IPDIS_VCU_BIT)
242 #define MAX_VARIANTS_EV 3
244 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
245 !defined(CONFIG_SPL_BUILD)
246 static char *zynqmp_get_silicon_idcode_name(void)
250 static char name[ZYNQMP_VERSION_SIZE];
252 id = chip_id(IDCODE);
253 ver = chip_id(IDCODE2);
254 debug("%s, ID: 0x%0X, Ver: 0x%0X\r\n", __func__, id, ver);
256 for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
257 if (zynqmp_devices[i].id == id) {
258 if (zynqmp_devices[i].evexists &&
259 !(ver & ZYNQMP_PL_STATUS_MASK))
261 if (zynqmp_devices[i].ver == (ver &
262 ZYNQMP_CSU_VERSION_MASK))
267 if (i >= ARRAY_SIZE(zynqmp_devices))
270 strncat(name, "zu", 2);
271 if (!zynqmp_devices[i].evexists ||
272 (ver & ZYNQMP_PL_STATUS_MASK)) {
273 strncat(name, zynqmp_devices[i].name,
274 ZYNQMP_VERSION_SIZE - 3);
279 * Here we are means, PL not powered up and ev variant
280 * exists. So, we need to ignore VCU disable bit(8) in
281 * version and findout if its CG or EG/EV variant.
283 for (j = 0; j < MAX_VARIANTS_EV; j++, i++) {
284 if ((zynqmp_devices[i].ver & ~BIT(ZYNQMP_IPDIS_VCU_BIT)) ==
285 (ver & ZYNQMP_CSU_VCUDIS_VER_MASK)) {
286 strncat(name, zynqmp_devices[i].name,
287 ZYNQMP_VERSION_SIZE - 3);
292 if (j >= MAX_VARIANTS_EV)
295 if (strstr(name, "eg") || strstr(name, "ev")) {
296 buf = strstr(name, "e");
304 int board_early_init_f(void)
306 #if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
313 /* Delay is required for clocks to be propagated */
317 #ifdef CONFIG_DEBUG_UART
318 /* Uart debug for sure */
320 puts("Debug uart enabled\n"); /* or printch() */
326 static int multi_boot(void)
330 multiboot = readl(&csu_base->multi_boot);
332 printf("Multiboot:\t%d\n", multiboot);
337 #define PS_SYSMON_ANALOG_BUS_VAL 0x3210
338 #define PS_SYSMON_ANALOG_BUS_REG 0xFFA50914
342 #if defined(CONFIG_ZYNQMP_FIRMWARE)
345 uclass_get_device_by_name(UCLASS_FIRMWARE, "zynqmp-power", &dev);
347 panic("PMU Firmware device not found - Enable it");
350 #if defined(CONFIG_SPL_BUILD)
351 /* Check *at build time* if the filename is an non-empty string */
352 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
353 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
354 zynqmp_pm_cfg_obj_size);
357 printf("EL Level:\tEL%d\n", current_el());
359 /* Bug in ROM sets wrong value in this register */
360 writel(PS_SYSMON_ANALOG_BUS_VAL, PS_SYSMON_ANALOG_BUS_REG);
362 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
363 !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
364 defined(CONFIG_SPL_BUILD))
365 if (current_el() != 3) {
366 zynqmppl.name = zynqmp_get_silicon_idcode_name();
367 printf("Chip ID:\t%s\n", zynqmppl.name);
369 fpga_add(fpga_xilinx, &zynqmppl);
373 if (current_el() == 3)
379 int board_early_init_r(void)
383 if (current_el() != 3)
386 val = readl(&crlapb_base->timestamp_ref_ctrl);
387 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
390 val = readl(&crlapb_base->timestamp_ref_ctrl);
391 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
392 writel(val, &crlapb_base->timestamp_ref_ctrl);
394 /* Program freq register in System counter */
395 writel(zynqmp_get_system_timer_freq(),
396 &iou_scntr_secure->base_frequency_id_register);
397 /* And enable system counter */
398 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
399 &iou_scntr_secure->counter_control_register);
404 unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
409 if (current_el() > 1) {
412 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
415 printf("FAIL: current EL is not above EL1\n");
421 #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
422 int dram_init_banksize(void)
426 ret = fdtdec_setup_memory_banksize();
437 if (fdtdec_setup_mem_size_base() != 0)
443 int dram_init_banksize(void)
445 #if defined(CONFIG_NR_DRAM_BANKS)
446 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
447 gd->bd->bi_dram[0].size = get_effective_memsize();
457 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
458 CONFIG_SYS_SDRAM_SIZE);
464 void reset_cpu(ulong addr)
468 #if defined(CONFIG_BOARD_LATE_INIT)
469 static const struct {
472 } reset_reasons[] = {
473 { RESET_REASON_DEBUG_SYS, "DEBUG" },
474 { RESET_REASON_SOFT, "SOFT" },
475 { RESET_REASON_SRST, "SRST" },
476 { RESET_REASON_PSONLY, "PS-ONLY" },
477 { RESET_REASON_PMU, "PMU" },
478 { RESET_REASON_INTERNAL, "INTERNAL" },
479 { RESET_REASON_EXTERNAL, "EXTERNAL" },
483 static int reset_reason(void)
487 const char *reason = NULL;
489 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, ®);
493 puts("Reset reason:\t");
495 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
496 if (reg & reset_reasons[i].bit) {
497 reason = reset_reasons[i].name;
498 printf("%s ", reset_reasons[i].name);
505 env_set("reset_reason", reason);
507 ret = zynqmp_mmio_write((ulong)&crlapb_base->reset_reason, ~0, ~0);
514 static int set_fdtfile(void)
516 char *compatible, *fdtfile;
517 const char *suffix = ".dtb";
518 const char *vendor = "xilinx/";
521 if (env_get("fdtfile"))
524 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible",
526 if (compatible && fdt_compat_len) {
529 debug("Compatible: %s\n", compatible);
531 name = strchr(compatible, ',');
537 fdtfile = calloc(1, strlen(vendor) + strlen(name) +
542 sprintf(fdtfile, "%s%s%s", vendor, name, suffix);
544 env_set("fdtfile", fdtfile);
551 static u8 zynqmp_get_bootmode(void)
557 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, ®);
561 if (reg >> BOOT_MODE_ALT_SHIFT)
562 reg >>= BOOT_MODE_ALT_SHIFT;
564 bootmode = reg & BOOT_MODES_MASK;
569 int board_late_init(void)
575 int env_targets_len = 0;
581 #if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
585 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
586 debug("Saved variables - Skipping\n");
594 bootmode = zynqmp_get_bootmode();
601 env_set("modeboot", "usb_dfu_spl");
605 mode = "jtag pxe dhcp";
606 env_set("modeboot", "jtagboot");
608 case QSPI_MODE_24BIT:
609 case QSPI_MODE_32BIT:
612 env_set("modeboot", "qspiboot");
616 if (uclass_get_device_by_name(UCLASS_MMC,
617 "mmc@ff160000", &dev) &&
618 uclass_get_device_by_name(UCLASS_MMC,
619 "sdhci@ff160000", &dev)) {
620 puts("Boot from EMMC but without SD0 enabled!\n");
623 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
630 if (uclass_get_device_by_name(UCLASS_MMC,
631 "mmc@ff160000", &dev) &&
632 uclass_get_device_by_name(UCLASS_MMC,
633 "sdhci@ff160000", &dev)) {
634 puts("Boot from SD0 but without SD0 enabled!\n");
637 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
641 env_set("modeboot", "sdboot");
648 if (uclass_get_device_by_name(UCLASS_MMC,
649 "mmc@ff170000", &dev) &&
650 uclass_get_device_by_name(UCLASS_MMC,
651 "sdhci@ff170000", &dev)) {
652 puts("Boot from SD1 but without SD1 enabled!\n");
655 debug("mmc1 device found at %p, seq %d\n", dev, dev->seq);
659 env_set("modeboot", "sdboot");
664 env_set("modeboot", "nandboot");
668 printf("Invalid Boot Mode:0x%x\n", bootmode);
673 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
674 debug("Bootseq len: %x\n", bootseq_len);
678 * One terminating char + one byte for space between mode
679 * and default boot_targets
681 env_targets = env_get("boot_targets");
683 env_targets_len = strlen(env_targets);
685 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
691 sprintf(new_targets, "%s%x %s", mode, bootseq,
692 env_targets ? env_targets : "");
694 sprintf(new_targets, "%s %s", mode,
695 env_targets ? env_targets : "");
697 env_set("boot_targets", new_targets);
701 return board_late_init_xilinx();
707 puts("Board: Xilinx ZynqMP\n");