184c7204bSMichal Simek /* 284c7204bSMichal Simek * (C) Copyright 2014 - 2015 Xilinx, Inc. 384c7204bSMichal Simek * Michal Simek <michal.simek@xilinx.com> 484c7204bSMichal Simek * 584c7204bSMichal Simek * SPDX-License-Identifier: GPL-2.0+ 684c7204bSMichal Simek */ 784c7204bSMichal Simek 884c7204bSMichal Simek #include <common.h> 9679b994aSMichal Simek #include <sata.h> 106fe6f135SMichal Simek #include <ahci.h> 116fe6f135SMichal Simek #include <scsi.h> 12b72894f1SMichal Simek #include <malloc.h> 130785dfd8SMichal Simek #include <asm/arch/clk.h> 1484c7204bSMichal Simek #include <asm/arch/hardware.h> 1584c7204bSMichal Simek #include <asm/arch/sys_proto.h> 1684c7204bSMichal Simek #include <asm/io.h> 1716fa00a7SSiva Durga Prasad Paladugu #include <usb.h> 1816fa00a7SSiva Durga Prasad Paladugu #include <dwc3-uboot.h> 1947e60cbdSMichal Simek #include <zynqmppl.h> 206919b4bfSMichal Simek #include <i2c.h> 219feff385SMichal Simek #include <g_dnl.h> 2284c7204bSMichal Simek 2384c7204bSMichal Simek DECLARE_GLOBAL_DATA_PTR; 2484c7204bSMichal Simek 2547e60cbdSMichal Simek #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \ 2647e60cbdSMichal Simek !defined(CONFIG_SPL_BUILD) 2747e60cbdSMichal Simek static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC; 2847e60cbdSMichal Simek 2947e60cbdSMichal Simek static const struct { 3047e60cbdSMichal Simek uint32_t id; 3147e60cbdSMichal Simek char *name; 3247e60cbdSMichal Simek } zynqmp_devices[] = { 3347e60cbdSMichal Simek { 3447e60cbdSMichal Simek .id = 0x10, 3547e60cbdSMichal Simek .name = "3eg", 3647e60cbdSMichal Simek }, 3747e60cbdSMichal Simek { 3847e60cbdSMichal Simek .id = 0x11, 3947e60cbdSMichal Simek .name = "2eg", 4047e60cbdSMichal Simek }, 4147e60cbdSMichal Simek { 4247e60cbdSMichal Simek .id = 0x20, 4347e60cbdSMichal Simek .name = "5ev", 4447e60cbdSMichal Simek }, 4547e60cbdSMichal Simek { 4647e60cbdSMichal Simek .id = 0x21, 4747e60cbdSMichal Simek .name = "4ev", 4847e60cbdSMichal Simek }, 4947e60cbdSMichal Simek { 5047e60cbdSMichal Simek .id = 0x30, 5147e60cbdSMichal Simek .name = "7ev", 5247e60cbdSMichal Simek }, 5347e60cbdSMichal Simek { 5447e60cbdSMichal Simek .id = 0x38, 5547e60cbdSMichal Simek .name = "9eg", 5647e60cbdSMichal Simek }, 5747e60cbdSMichal Simek { 5847e60cbdSMichal Simek .id = 0x39, 5947e60cbdSMichal Simek .name = "6eg", 6047e60cbdSMichal Simek }, 6147e60cbdSMichal Simek { 6247e60cbdSMichal Simek .id = 0x40, 6347e60cbdSMichal Simek .name = "11eg", 6447e60cbdSMichal Simek }, 6547e60cbdSMichal Simek { 6647e60cbdSMichal Simek .id = 0x50, 6747e60cbdSMichal Simek .name = "15eg", 6847e60cbdSMichal Simek }, 6947e60cbdSMichal Simek { 7047e60cbdSMichal Simek .id = 0x58, 7147e60cbdSMichal Simek .name = "19eg", 7247e60cbdSMichal Simek }, 7347e60cbdSMichal Simek { 7447e60cbdSMichal Simek .id = 0x59, 7547e60cbdSMichal Simek .name = "17eg", 7647e60cbdSMichal Simek }, 7747e60cbdSMichal Simek }; 7847e60cbdSMichal Simek 7947e60cbdSMichal Simek static int chip_id(void) 8047e60cbdSMichal Simek { 8147e60cbdSMichal Simek struct pt_regs regs; 8247e60cbdSMichal Simek regs.regs[0] = ZYNQMP_SIP_SVC_CSU_DMA_CHIPID; 8347e60cbdSMichal Simek regs.regs[1] = 0; 8447e60cbdSMichal Simek regs.regs[2] = 0; 8547e60cbdSMichal Simek regs.regs[3] = 0; 8647e60cbdSMichal Simek 8747e60cbdSMichal Simek smc_call(®s); 8847e60cbdSMichal Simek 890cba6abbSSoren Brinkmann /* 900cba6abbSSoren Brinkmann * SMC returns: 910cba6abbSSoren Brinkmann * regs[0][31:0] = status of the operation 920cba6abbSSoren Brinkmann * regs[0][63:32] = CSU.IDCODE register 930cba6abbSSoren Brinkmann * regs[1][31:0] = CSU.version register 940cba6abbSSoren Brinkmann */ 950cba6abbSSoren Brinkmann regs.regs[0] = upper_32_bits(regs.regs[0]); 960cba6abbSSoren Brinkmann regs.regs[0] &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK | 970cba6abbSSoren Brinkmann ZYNQMP_CSU_IDCODE_SVD_MASK; 980cba6abbSSoren Brinkmann regs.regs[0] >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT; 990cba6abbSSoren Brinkmann 10047e60cbdSMichal Simek return regs.regs[0]; 10147e60cbdSMichal Simek } 10247e60cbdSMichal Simek 10347e60cbdSMichal Simek static char *zynqmp_get_silicon_idcode_name(void) 10447e60cbdSMichal Simek { 10547e60cbdSMichal Simek uint32_t i, id; 10647e60cbdSMichal Simek 10747e60cbdSMichal Simek id = chip_id(); 10847e60cbdSMichal Simek for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) { 10947e60cbdSMichal Simek if (zynqmp_devices[i].id == id) 11047e60cbdSMichal Simek return zynqmp_devices[i].name; 11147e60cbdSMichal Simek } 11247e60cbdSMichal Simek return "unknown"; 11347e60cbdSMichal Simek } 11447e60cbdSMichal Simek #endif 11547e60cbdSMichal Simek 116*fb4000e8SMichal Simek int board_early_init_f(void) 117*fb4000e8SMichal Simek { 118*fb4000e8SMichal Simek #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CLK_ZYNQMP) 119*fb4000e8SMichal Simek zynqmp_pmufw_version(); 120*fb4000e8SMichal Simek #endif 121*fb4000e8SMichal Simek return 0; 122*fb4000e8SMichal Simek } 123*fb4000e8SMichal Simek 12447e60cbdSMichal Simek #define ZYNQMP_VERSION_SIZE 9 12547e60cbdSMichal Simek 12684c7204bSMichal Simek int board_init(void) 12784c7204bSMichal Simek { 128a0736efbSMichal Simek printf("EL Level:\tEL%d\n", current_el()); 129a0736efbSMichal Simek 13047e60cbdSMichal Simek #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \ 13147e60cbdSMichal Simek !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \ 13247e60cbdSMichal Simek defined(CONFIG_SPL_BUILD)) 13347e60cbdSMichal Simek if (current_el() != 3) { 13447e60cbdSMichal Simek static char version[ZYNQMP_VERSION_SIZE]; 13547e60cbdSMichal Simek 13647e60cbdSMichal Simek strncat(version, "xczu", ZYNQMP_VERSION_SIZE); 13747e60cbdSMichal Simek zynqmppl.name = strncat(version, 13847e60cbdSMichal Simek zynqmp_get_silicon_idcode_name(), 13947e60cbdSMichal Simek ZYNQMP_VERSION_SIZE); 14047e60cbdSMichal Simek printf("Chip ID:\t%s\n", zynqmppl.name); 14147e60cbdSMichal Simek fpga_init(); 14247e60cbdSMichal Simek fpga_add(fpga_xilinx, &zynqmppl); 14347e60cbdSMichal Simek } 14447e60cbdSMichal Simek #endif 14547e60cbdSMichal Simek 14684c7204bSMichal Simek return 0; 14784c7204bSMichal Simek } 14884c7204bSMichal Simek 14984c7204bSMichal Simek int board_early_init_r(void) 15084c7204bSMichal Simek { 15184c7204bSMichal Simek u32 val; 15284c7204bSMichal Simek 1530785dfd8SMichal Simek if (current_el() == 3) { 15484c7204bSMichal Simek val = readl(&crlapb_base->timestamp_ref_ctrl); 15584c7204bSMichal Simek val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT; 15684c7204bSMichal Simek writel(val, &crlapb_base->timestamp_ref_ctrl); 15784c7204bSMichal Simek 1580785dfd8SMichal Simek /* Program freq register in System counter */ 1590785dfd8SMichal Simek writel(zynqmp_get_system_timer_freq(), 1600785dfd8SMichal Simek &iou_scntr_secure->base_frequency_id_register); 1610785dfd8SMichal Simek /* And enable system counter */ 1620785dfd8SMichal Simek writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN, 1630785dfd8SMichal Simek &iou_scntr_secure->counter_control_register); 1640785dfd8SMichal Simek } 16584c7204bSMichal Simek /* Program freq register in System counter and enable system counter */ 16684c7204bSMichal Simek writel(gd->cpu_clk, &iou_scntr->base_frequency_id_register); 16784c7204bSMichal Simek writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_HDBG | 16884c7204bSMichal Simek ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN, 16984c7204bSMichal Simek &iou_scntr->counter_control_register); 17084c7204bSMichal Simek 17184c7204bSMichal Simek return 0; 17284c7204bSMichal Simek } 17384c7204bSMichal Simek 1746919b4bfSMichal Simek int zynq_board_read_rom_ethaddr(unsigned char *ethaddr) 1756919b4bfSMichal Simek { 1766919b4bfSMichal Simek #if defined(CONFIG_ZYNQ_GEM_EEPROM_ADDR) && \ 1776919b4bfSMichal Simek defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET) && \ 1786919b4bfSMichal Simek defined(CONFIG_ZYNQ_EEPROM_BUS) 1796919b4bfSMichal Simek i2c_set_bus_num(CONFIG_ZYNQ_EEPROM_BUS); 1806919b4bfSMichal Simek 1816919b4bfSMichal Simek if (eeprom_read(CONFIG_ZYNQ_GEM_EEPROM_ADDR, 1826919b4bfSMichal Simek CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET, 1836919b4bfSMichal Simek ethaddr, 6)) 1846919b4bfSMichal Simek printf("I2C EEPROM MAC address read failed\n"); 1856919b4bfSMichal Simek #endif 1866919b4bfSMichal Simek 1876919b4bfSMichal Simek return 0; 1886919b4bfSMichal Simek } 1896919b4bfSMichal Simek 1908d59d7f6SMichal Simek #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE) 19176b00acaSSimon Glass int dram_init_banksize(void) 192361a8799STom Rini { 193950f86caSNathan Rossi fdtdec_setup_memory_banksize(); 19476b00acaSSimon Glass 19576b00acaSSimon Glass return 0; 1968d59d7f6SMichal Simek } 1978d59d7f6SMichal Simek 1988d59d7f6SMichal Simek int dram_init(void) 1998d59d7f6SMichal Simek { 200950f86caSNathan Rossi if (fdtdec_setup_memory_size() != 0) 201950f86caSNathan Rossi return -EINVAL; 2028d59d7f6SMichal Simek 2038d59d7f6SMichal Simek return 0; 2048d59d7f6SMichal Simek } 2058d59d7f6SMichal Simek #else 20684c7204bSMichal Simek int dram_init(void) 20784c7204bSMichal Simek { 20884c7204bSMichal Simek gd->ram_size = CONFIG_SYS_SDRAM_SIZE; 20984c7204bSMichal Simek 21084c7204bSMichal Simek return 0; 21184c7204bSMichal Simek } 2128d59d7f6SMichal Simek #endif 21384c7204bSMichal Simek 21484c7204bSMichal Simek void reset_cpu(ulong addr) 21584c7204bSMichal Simek { 21684c7204bSMichal Simek } 21784c7204bSMichal Simek 21884c7204bSMichal Simek int board_late_init(void) 21984c7204bSMichal Simek { 22084c7204bSMichal Simek u32 reg = 0; 22184c7204bSMichal Simek u8 bootmode; 222b72894f1SMichal Simek const char *mode; 223b72894f1SMichal Simek char *new_targets; 224b72894f1SMichal Simek 225b72894f1SMichal Simek if (!(gd->flags & GD_FLG_ENV_DEFAULT)) { 226b72894f1SMichal Simek debug("Saved variables - Skipping\n"); 227b72894f1SMichal Simek return 0; 228b72894f1SMichal Simek } 22984c7204bSMichal Simek 23084c7204bSMichal Simek reg = readl(&crlapb_base->boot_mode); 23147359a03SMichal Simek if (reg >> BOOT_MODE_ALT_SHIFT) 23247359a03SMichal Simek reg >>= BOOT_MODE_ALT_SHIFT; 23347359a03SMichal Simek 23484c7204bSMichal Simek bootmode = reg & BOOT_MODES_MASK; 23584c7204bSMichal Simek 236fb90917cSMichal Simek puts("Bootmode: "); 23784c7204bSMichal Simek switch (bootmode) { 238d58fc12eSMichal Simek case USB_MODE: 239d58fc12eSMichal Simek puts("USB_MODE\n"); 240d58fc12eSMichal Simek mode = "usb"; 241d58fc12eSMichal Simek break; 2420a5bcc8cSSiva Durga Prasad Paladugu case JTAG_MODE: 243fb90917cSMichal Simek puts("JTAG_MODE\n"); 244b72894f1SMichal Simek mode = "pxe dhcp"; 2450a5bcc8cSSiva Durga Prasad Paladugu break; 2460a5bcc8cSSiva Durga Prasad Paladugu case QSPI_MODE_24BIT: 2470a5bcc8cSSiva Durga Prasad Paladugu case QSPI_MODE_32BIT: 248b72894f1SMichal Simek mode = "qspi0"; 249fb90917cSMichal Simek puts("QSPI_MODE\n"); 2500a5bcc8cSSiva Durga Prasad Paladugu break; 25139c56f55SMichal Simek case EMMC_MODE: 25278678feeSMichal Simek puts("EMMC_MODE\n"); 253b72894f1SMichal Simek mode = "mmc0"; 25478678feeSMichal Simek break; 25578678feeSMichal Simek case SD_MODE: 256fb90917cSMichal Simek puts("SD_MODE\n"); 257b72894f1SMichal Simek mode = "mmc0"; 25884c7204bSMichal Simek break; 259e1992276SSiva Durga Prasad Paladugu case SD1_LSHFT_MODE: 260e1992276SSiva Durga Prasad Paladugu puts("LVL_SHFT_"); 261e1992276SSiva Durga Prasad Paladugu /* fall through */ 262af813acdSMichal Simek case SD_MODE1: 263fb90917cSMichal Simek puts("SD_MODE1\n"); 2642d9925bcSMichal Simek #if defined(CONFIG_ZYNQ_SDHCI0) && defined(CONFIG_ZYNQ_SDHCI1) 265b72894f1SMichal Simek mode = "mmc1"; 266b72894f1SMichal Simek #else 267b72894f1SMichal Simek mode = "mmc0"; 2682d9925bcSMichal Simek #endif 269af813acdSMichal Simek break; 270af813acdSMichal Simek case NAND_MODE: 271fb90917cSMichal Simek puts("NAND_MODE\n"); 272b72894f1SMichal Simek mode = "nand0"; 273af813acdSMichal Simek break; 27484c7204bSMichal Simek default: 275b72894f1SMichal Simek mode = ""; 27684c7204bSMichal Simek printf("Invalid Boot Mode:0x%x\n", bootmode); 27784c7204bSMichal Simek break; 27884c7204bSMichal Simek } 27984c7204bSMichal Simek 280b72894f1SMichal Simek /* 281b72894f1SMichal Simek * One terminating char + one byte for space between mode 282b72894f1SMichal Simek * and default boot_targets 283b72894f1SMichal Simek */ 284b72894f1SMichal Simek new_targets = calloc(1, strlen(mode) + 285b72894f1SMichal Simek strlen(getenv("boot_targets")) + 2); 286b72894f1SMichal Simek 287b72894f1SMichal Simek sprintf(new_targets, "%s %s", mode, getenv("boot_targets")); 288b72894f1SMichal Simek setenv("boot_targets", new_targets); 289b72894f1SMichal Simek 29084c7204bSMichal Simek return 0; 29184c7204bSMichal Simek } 29284696ff5SSiva Durga Prasad Paladugu 29384696ff5SSiva Durga Prasad Paladugu int checkboard(void) 29484696ff5SSiva Durga Prasad Paladugu { 2955af08556SMichal Simek puts("Board: Xilinx ZynqMP\n"); 29684696ff5SSiva Durga Prasad Paladugu return 0; 29784696ff5SSiva Durga Prasad Paladugu } 29816fa00a7SSiva Durga Prasad Paladugu 29916fa00a7SSiva Durga Prasad Paladugu #ifdef CONFIG_USB_DWC3 300275bd6d1SMichal Simek static struct dwc3_device dwc3_device_data0 = { 30116fa00a7SSiva Durga Prasad Paladugu .maximum_speed = USB_SPEED_HIGH, 30216fa00a7SSiva Durga Prasad Paladugu .base = ZYNQMP_USB0_XHCI_BASEADDR, 30316fa00a7SSiva Durga Prasad Paladugu .dr_mode = USB_DR_MODE_PERIPHERAL, 30416fa00a7SSiva Durga Prasad Paladugu .index = 0, 30516fa00a7SSiva Durga Prasad Paladugu }; 30616fa00a7SSiva Durga Prasad Paladugu 307275bd6d1SMichal Simek static struct dwc3_device dwc3_device_data1 = { 308275bd6d1SMichal Simek .maximum_speed = USB_SPEED_HIGH, 309275bd6d1SMichal Simek .base = ZYNQMP_USB1_XHCI_BASEADDR, 310275bd6d1SMichal Simek .dr_mode = USB_DR_MODE_PERIPHERAL, 311275bd6d1SMichal Simek .index = 1, 312275bd6d1SMichal Simek }; 313275bd6d1SMichal Simek 3149feff385SMichal Simek int usb_gadget_handle_interrupts(int index) 31516fa00a7SSiva Durga Prasad Paladugu { 3169feff385SMichal Simek dwc3_uboot_handle_interrupt(index); 31716fa00a7SSiva Durga Prasad Paladugu return 0; 31816fa00a7SSiva Durga Prasad Paladugu } 31916fa00a7SSiva Durga Prasad Paladugu 32016fa00a7SSiva Durga Prasad Paladugu int board_usb_init(int index, enum usb_init_type init) 32116fa00a7SSiva Durga Prasad Paladugu { 322275bd6d1SMichal Simek debug("%s: index %x\n", __func__, index); 323275bd6d1SMichal Simek 3248ecd50c8SMichal Simek #if defined(CONFIG_USB_GADGET_DOWNLOAD) 3258ecd50c8SMichal Simek g_dnl_set_serialnumber(CONFIG_SYS_CONFIG_NAME); 3268ecd50c8SMichal Simek #endif 3278ecd50c8SMichal Simek 328275bd6d1SMichal Simek switch (index) { 329275bd6d1SMichal Simek case 0: 330275bd6d1SMichal Simek return dwc3_uboot_init(&dwc3_device_data0); 331275bd6d1SMichal Simek case 1: 332275bd6d1SMichal Simek return dwc3_uboot_init(&dwc3_device_data1); 333275bd6d1SMichal Simek }; 334275bd6d1SMichal Simek 335275bd6d1SMichal Simek return -1; 33616fa00a7SSiva Durga Prasad Paladugu } 33716fa00a7SSiva Durga Prasad Paladugu 33816fa00a7SSiva Durga Prasad Paladugu int board_usb_cleanup(int index, enum usb_init_type init) 33916fa00a7SSiva Durga Prasad Paladugu { 34016fa00a7SSiva Durga Prasad Paladugu dwc3_uboot_exit(index); 34116fa00a7SSiva Durga Prasad Paladugu return 0; 34216fa00a7SSiva Durga Prasad Paladugu } 34316fa00a7SSiva Durga Prasad Paladugu #endif 344