1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2014 - 2015 Xilinx, Inc.
13 #include <asm/arch/clk.h>
14 #include <asm/arch/hardware.h>
15 #include <asm/arch/sys_proto.h>
16 #include <asm/arch/psu_init_gpl.h>
18 #include <dm/device.h>
19 #include <dm/uclass.h>
21 #include <dwc3-uboot.h>
26 DECLARE_GLOBAL_DATA_PTR;
28 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_WDT)
29 static struct udevice *watchdog_dev;
32 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
33 !defined(CONFIG_SPL_BUILD)
34 static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
41 } zynqmp_devices[] = {
133 { /* For testing purpose only */
177 int chip_id(unsigned char id)
182 if (current_el() != 3) {
183 regs.regs[0] = ZYNQMP_SIP_SVC_CSU_DMA_CHIPID;
192 * regs[0][31:0] = status of the operation
193 * regs[0][63:32] = CSU.IDCODE register
194 * regs[1][31:0] = CSU.version register
195 * regs[1][63:32] = CSU.IDCODE2 register
199 regs.regs[0] = upper_32_bits(regs.regs[0]);
200 regs.regs[0] &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
201 ZYNQMP_CSU_IDCODE_SVD_MASK;
202 regs.regs[0] >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
206 regs.regs[1] = lower_32_bits(regs.regs[1]);
207 regs.regs[1] &= ZYNQMP_CSU_SILICON_VER_MASK;
211 regs.regs[1] = lower_32_bits(regs.regs[1]);
212 regs.regs[1] >>= ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
216 printf("%s, Invalid Req:0x%x\n", __func__, id);
221 val = readl(ZYNQMP_CSU_IDCODE_ADDR);
222 val &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
223 ZYNQMP_CSU_IDCODE_SVD_MASK;
224 val >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
227 val = readl(ZYNQMP_CSU_VER_ADDR);
228 val &= ZYNQMP_CSU_SILICON_VER_MASK;
231 printf("%s, Invalid Req:0x%x\n", __func__, id);
238 #define ZYNQMP_VERSION_SIZE 9
239 #define ZYNQMP_PL_STATUS_BIT 9
240 #define ZYNQMP_IPDIS_VCU_BIT 8
241 #define ZYNQMP_PL_STATUS_MASK BIT(ZYNQMP_PL_STATUS_BIT)
242 #define ZYNQMP_CSU_VERSION_MASK ~(ZYNQMP_PL_STATUS_MASK)
243 #define ZYNQMP_CSU_VCUDIS_VER_MASK ZYNQMP_CSU_VERSION_MASK & \
244 ~BIT(ZYNQMP_IPDIS_VCU_BIT)
245 #define MAX_VARIANTS_EV 3
247 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
248 !defined(CONFIG_SPL_BUILD)
249 static char *zynqmp_get_silicon_idcode_name(void)
253 static char name[ZYNQMP_VERSION_SIZE];
255 id = chip_id(IDCODE);
256 ver = chip_id(IDCODE2);
258 for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
259 if (zynqmp_devices[i].id == id) {
260 if (zynqmp_devices[i].evexists &&
261 !(ver & ZYNQMP_PL_STATUS_MASK))
263 if (zynqmp_devices[i].ver == (ver &
264 ZYNQMP_CSU_VERSION_MASK))
269 if (i >= ARRAY_SIZE(zynqmp_devices))
272 strncat(name, "zu", 2);
273 if (!zynqmp_devices[i].evexists ||
274 (ver & ZYNQMP_PL_STATUS_MASK)) {
275 strncat(name, zynqmp_devices[i].name,
276 ZYNQMP_VERSION_SIZE - 3);
281 * Here we are means, PL not powered up and ev variant
282 * exists. So, we need to ignore VCU disable bit(8) in
283 * version and findout if its CG or EG/EV variant.
285 for (j = 0; j < MAX_VARIANTS_EV; j++, i++) {
286 if ((zynqmp_devices[i].ver & ~BIT(ZYNQMP_IPDIS_VCU_BIT)) ==
287 (ver & ZYNQMP_CSU_VCUDIS_VER_MASK)) {
288 strncat(name, zynqmp_devices[i].name,
289 ZYNQMP_VERSION_SIZE - 3);
294 if (j >= MAX_VARIANTS_EV)
297 if (strstr(name, "eg") || strstr(name, "ev")) {
298 buf = strstr(name, "e");
306 int board_early_init_f(void)
309 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CLK_ZYNQMP)
312 pm_api_version = zynqmp_pmufw_version();
313 printf("PMUFW:\tv%d.%d\n",
314 pm_api_version >> ZYNQMP_PM_VERSION_MAJOR_SHIFT,
315 pm_api_version & ZYNQMP_PM_VERSION_MINOR_MASK);
317 if (pm_api_version < ZYNQMP_PM_VERSION)
318 panic("PMUFW version error. Expected: v%d.%d\n",
319 ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR);
322 #if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
326 #if defined(CONFIG_WDT) && !defined(CONFIG_SPL_BUILD)
327 /* bss is not cleared at time when watchdog_reset() is called */
336 printf("EL Level:\tEL%d\n", current_el());
338 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
339 !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
340 defined(CONFIG_SPL_BUILD))
341 if (current_el() != 3) {
342 zynqmppl.name = zynqmp_get_silicon_idcode_name();
343 printf("Chip ID:\t%s\n", zynqmppl.name);
345 fpga_add(fpga_xilinx, &zynqmppl);
349 #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_WDT)
350 if (uclass_get_device_by_seq(UCLASS_WDT, 0, &watchdog_dev)) {
351 debug("Watchdog: Not found by seq!\n");
352 if (uclass_get_device(UCLASS_WDT, 0, &watchdog_dev)) {
353 puts("Watchdog: Not found!\n");
358 wdt_start(watchdog_dev, 0, 0);
359 puts("Watchdog: Started\n");
365 #ifdef CONFIG_WATCHDOG
366 /* Called by macro WATCHDOG_RESET */
367 void watchdog_reset(void)
369 # if !defined(CONFIG_SPL_BUILD)
370 static ulong next_reset;
376 now = timer_get_us();
378 /* Do not reset the watchdog too often */
379 if (now > next_reset) {
380 wdt_reset(watchdog_dev);
381 next_reset = now + 1000;
387 int board_early_init_r(void)
391 if (current_el() != 3)
394 val = readl(&crlapb_base->timestamp_ref_ctrl);
395 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
398 val = readl(&crlapb_base->timestamp_ref_ctrl);
399 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
400 writel(val, &crlapb_base->timestamp_ref_ctrl);
402 /* Program freq register in System counter */
403 writel(zynqmp_get_system_timer_freq(),
404 &iou_scntr_secure->base_frequency_id_register);
405 /* And enable system counter */
406 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
407 &iou_scntr_secure->counter_control_register);
412 int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
414 #if defined(CONFIG_ZYNQ_GEM_EEPROM_ADDR) && \
415 defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET) && \
416 defined(CONFIG_ZYNQ_EEPROM_BUS)
417 i2c_set_bus_num(CONFIG_ZYNQ_EEPROM_BUS);
419 if (eeprom_read(CONFIG_ZYNQ_GEM_EEPROM_ADDR,
420 CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET,
422 printf("I2C EEPROM MAC address read failed\n");
428 unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
433 if (current_el() > 1) {
436 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
439 printf("FAIL: current EL is not above EL1\n");
445 #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
446 int dram_init_banksize(void)
450 ret = fdtdec_setup_memory_banksize();
461 if (fdtdec_setup_mem_size_base() != 0)
467 int dram_init_banksize(void)
469 #if defined(CONFIG_NR_DRAM_BANKS)
470 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
471 gd->bd->bi_dram[0].size = get_effective_memsize();
481 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
482 CONFIG_SYS_SDRAM_SIZE);
488 void reset_cpu(ulong addr)
492 static const struct {
495 } reset_reasons[] = {
496 { RESET_REASON_DEBUG_SYS, "DEBUG" },
497 { RESET_REASON_SOFT, "SOFT" },
498 { RESET_REASON_SRST, "SRST" },
499 { RESET_REASON_PSONLY, "PS-ONLY" },
500 { RESET_REASON_PMU, "PMU" },
501 { RESET_REASON_INTERNAL, "INTERNAL" },
502 { RESET_REASON_EXTERNAL, "EXTERNAL" },
506 static u32 reset_reason(void)
510 const char *reason = NULL;
512 ret = readl(&crlapb_base->reset_reason);
514 puts("Reset reason:\t");
516 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
517 if (ret & reset_reasons[i].bit) {
518 reason = reset_reasons[i].name;
519 printf("%s ", reset_reasons[i].name);
526 env_set("reset_reason", reason);
528 writel(~0, &crlapb_base->reset_reason);
533 int board_late_init(void)
540 int env_targets_len = 0;
546 #if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
550 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
551 debug("Saved variables - Skipping\n");
555 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, ®);
559 if (reg >> BOOT_MODE_ALT_SHIFT)
560 reg >>= BOOT_MODE_ALT_SHIFT;
562 bootmode = reg & BOOT_MODES_MASK;
569 env_set("modeboot", "usb_dfu_spl");
574 env_set("modeboot", "jtagboot");
576 case QSPI_MODE_24BIT:
577 case QSPI_MODE_32BIT:
580 env_set("modeboot", "qspiboot");
585 env_set("modeboot", "emmcboot");
589 if (uclass_get_device_by_name(UCLASS_MMC,
590 "sdhci@ff160000", &dev)) {
591 puts("Boot from SD0 but without SD0 enabled!\n");
594 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
598 env_set("modeboot", "sdboot");
605 if (uclass_get_device_by_name(UCLASS_MMC,
606 "sdhci@ff170000", &dev)) {
607 puts("Boot from SD1 but without SD1 enabled!\n");
610 debug("mmc1 device found at %p, seq %d\n", dev, dev->seq);
614 env_set("modeboot", "sdboot");
619 env_set("modeboot", "nandboot");
623 printf("Invalid Boot Mode:0x%x\n", bootmode);
628 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
629 debug("Bootseq len: %x\n", bootseq_len);
633 * One terminating char + one byte for space between mode
634 * and default boot_targets
636 env_targets = env_get("boot_targets");
638 env_targets_len = strlen(env_targets);
640 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
646 sprintf(new_targets, "%s%x %s", mode, bootseq,
647 env_targets ? env_targets : "");
649 sprintf(new_targets, "%s %s", mode,
650 env_targets ? env_targets : "");
652 env_set("boot_targets", new_targets);
661 puts("Board: Xilinx ZynqMP\n");