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 11647e60cbdSMichal Simek #define ZYNQMP_VERSION_SIZE 9 11747e60cbdSMichal Simek 11884c7204bSMichal Simek int board_init(void) 11984c7204bSMichal Simek { 120a0736efbSMichal Simek printf("EL Level:\tEL%d\n", current_el()); 121a0736efbSMichal Simek 12247e60cbdSMichal Simek #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \ 12347e60cbdSMichal Simek !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \ 12447e60cbdSMichal Simek defined(CONFIG_SPL_BUILD)) 12547e60cbdSMichal Simek if (current_el() != 3) { 12647e60cbdSMichal Simek static char version[ZYNQMP_VERSION_SIZE]; 12747e60cbdSMichal Simek 12847e60cbdSMichal Simek strncat(version, "xczu", ZYNQMP_VERSION_SIZE); 12947e60cbdSMichal Simek zynqmppl.name = strncat(version, 13047e60cbdSMichal Simek zynqmp_get_silicon_idcode_name(), 13147e60cbdSMichal Simek ZYNQMP_VERSION_SIZE); 13247e60cbdSMichal Simek printf("Chip ID:\t%s\n", zynqmppl.name); 13347e60cbdSMichal Simek fpga_init(); 13447e60cbdSMichal Simek fpga_add(fpga_xilinx, &zynqmppl); 13547e60cbdSMichal Simek } 13647e60cbdSMichal Simek #endif 13747e60cbdSMichal Simek 13884c7204bSMichal Simek return 0; 13984c7204bSMichal Simek } 14084c7204bSMichal Simek 14184c7204bSMichal Simek int board_early_init_r(void) 14284c7204bSMichal Simek { 14384c7204bSMichal Simek u32 val; 14484c7204bSMichal Simek 1450785dfd8SMichal Simek if (current_el() == 3) { 14684c7204bSMichal Simek val = readl(&crlapb_base->timestamp_ref_ctrl); 14784c7204bSMichal Simek val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT; 14884c7204bSMichal Simek writel(val, &crlapb_base->timestamp_ref_ctrl); 14984c7204bSMichal Simek 1500785dfd8SMichal Simek /* Program freq register in System counter */ 1510785dfd8SMichal Simek writel(zynqmp_get_system_timer_freq(), 1520785dfd8SMichal Simek &iou_scntr_secure->base_frequency_id_register); 1530785dfd8SMichal Simek /* And enable system counter */ 1540785dfd8SMichal Simek writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN, 1550785dfd8SMichal Simek &iou_scntr_secure->counter_control_register); 1560785dfd8SMichal Simek } 15784c7204bSMichal Simek /* Program freq register in System counter and enable system counter */ 15884c7204bSMichal Simek writel(gd->cpu_clk, &iou_scntr->base_frequency_id_register); 15984c7204bSMichal Simek writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_HDBG | 16084c7204bSMichal Simek ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN, 16184c7204bSMichal Simek &iou_scntr->counter_control_register); 16284c7204bSMichal Simek 16384c7204bSMichal Simek return 0; 16484c7204bSMichal Simek } 16584c7204bSMichal Simek 1666919b4bfSMichal Simek int zynq_board_read_rom_ethaddr(unsigned char *ethaddr) 1676919b4bfSMichal Simek { 1686919b4bfSMichal Simek #if defined(CONFIG_ZYNQ_GEM_EEPROM_ADDR) && \ 1696919b4bfSMichal Simek defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET) && \ 1706919b4bfSMichal Simek defined(CONFIG_ZYNQ_EEPROM_BUS) 1716919b4bfSMichal Simek i2c_set_bus_num(CONFIG_ZYNQ_EEPROM_BUS); 1726919b4bfSMichal Simek 1736919b4bfSMichal Simek if (eeprom_read(CONFIG_ZYNQ_GEM_EEPROM_ADDR, 1746919b4bfSMichal Simek CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET, 1756919b4bfSMichal Simek ethaddr, 6)) 1766919b4bfSMichal Simek printf("I2C EEPROM MAC address read failed\n"); 1776919b4bfSMichal Simek #endif 1786919b4bfSMichal Simek 1796919b4bfSMichal Simek return 0; 1806919b4bfSMichal Simek } 1816919b4bfSMichal Simek 1828d59d7f6SMichal Simek #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE) 183361a8799STom Rini void dram_init_banksize(void) 184361a8799STom Rini { 185*950f86caSNathan Rossi fdtdec_setup_memory_banksize(); 1868d59d7f6SMichal Simek } 1878d59d7f6SMichal Simek 1888d59d7f6SMichal Simek int dram_init(void) 1898d59d7f6SMichal Simek { 190*950f86caSNathan Rossi if (fdtdec_setup_memory_size() != 0) 191*950f86caSNathan Rossi return -EINVAL; 1928d59d7f6SMichal Simek 1938d59d7f6SMichal Simek return 0; 1948d59d7f6SMichal Simek } 1958d59d7f6SMichal Simek #else 19684c7204bSMichal Simek int dram_init(void) 19784c7204bSMichal Simek { 19884c7204bSMichal Simek gd->ram_size = CONFIG_SYS_SDRAM_SIZE; 19984c7204bSMichal Simek 20084c7204bSMichal Simek return 0; 20184c7204bSMichal Simek } 2028d59d7f6SMichal Simek #endif 20384c7204bSMichal Simek 20484c7204bSMichal Simek void reset_cpu(ulong addr) 20584c7204bSMichal Simek { 20684c7204bSMichal Simek } 20784c7204bSMichal Simek 20884c7204bSMichal Simek int board_late_init(void) 20984c7204bSMichal Simek { 21084c7204bSMichal Simek u32 reg = 0; 21184c7204bSMichal Simek u8 bootmode; 212b72894f1SMichal Simek const char *mode; 213b72894f1SMichal Simek char *new_targets; 214b72894f1SMichal Simek 215b72894f1SMichal Simek if (!(gd->flags & GD_FLG_ENV_DEFAULT)) { 216b72894f1SMichal Simek debug("Saved variables - Skipping\n"); 217b72894f1SMichal Simek return 0; 218b72894f1SMichal Simek } 21984c7204bSMichal Simek 22084c7204bSMichal Simek reg = readl(&crlapb_base->boot_mode); 22147359a03SMichal Simek if (reg >> BOOT_MODE_ALT_SHIFT) 22247359a03SMichal Simek reg >>= BOOT_MODE_ALT_SHIFT; 22347359a03SMichal Simek 22484c7204bSMichal Simek bootmode = reg & BOOT_MODES_MASK; 22584c7204bSMichal Simek 226fb90917cSMichal Simek puts("Bootmode: "); 22784c7204bSMichal Simek switch (bootmode) { 228d58fc12eSMichal Simek case USB_MODE: 229d58fc12eSMichal Simek puts("USB_MODE\n"); 230d58fc12eSMichal Simek mode = "usb"; 231d58fc12eSMichal Simek break; 2320a5bcc8cSSiva Durga Prasad Paladugu case JTAG_MODE: 233fb90917cSMichal Simek puts("JTAG_MODE\n"); 234b72894f1SMichal Simek mode = "pxe dhcp"; 2350a5bcc8cSSiva Durga Prasad Paladugu break; 2360a5bcc8cSSiva Durga Prasad Paladugu case QSPI_MODE_24BIT: 2370a5bcc8cSSiva Durga Prasad Paladugu case QSPI_MODE_32BIT: 238b72894f1SMichal Simek mode = "qspi0"; 239fb90917cSMichal Simek puts("QSPI_MODE\n"); 2400a5bcc8cSSiva Durga Prasad Paladugu break; 24139c56f55SMichal Simek case EMMC_MODE: 24278678feeSMichal Simek puts("EMMC_MODE\n"); 243b72894f1SMichal Simek mode = "mmc0"; 24478678feeSMichal Simek break; 24578678feeSMichal Simek case SD_MODE: 246fb90917cSMichal Simek puts("SD_MODE\n"); 247b72894f1SMichal Simek mode = "mmc0"; 24884c7204bSMichal Simek break; 249e1992276SSiva Durga Prasad Paladugu case SD1_LSHFT_MODE: 250e1992276SSiva Durga Prasad Paladugu puts("LVL_SHFT_"); 251e1992276SSiva Durga Prasad Paladugu /* fall through */ 252af813acdSMichal Simek case SD_MODE1: 253fb90917cSMichal Simek puts("SD_MODE1\n"); 2542d9925bcSMichal Simek #if defined(CONFIG_ZYNQ_SDHCI0) && defined(CONFIG_ZYNQ_SDHCI1) 255b72894f1SMichal Simek mode = "mmc1"; 256b72894f1SMichal Simek #else 257b72894f1SMichal Simek mode = "mmc0"; 2582d9925bcSMichal Simek #endif 259af813acdSMichal Simek break; 260af813acdSMichal Simek case NAND_MODE: 261fb90917cSMichal Simek puts("NAND_MODE\n"); 262b72894f1SMichal Simek mode = "nand0"; 263af813acdSMichal Simek break; 26484c7204bSMichal Simek default: 265b72894f1SMichal Simek mode = ""; 26684c7204bSMichal Simek printf("Invalid Boot Mode:0x%x\n", bootmode); 26784c7204bSMichal Simek break; 26884c7204bSMichal Simek } 26984c7204bSMichal Simek 270b72894f1SMichal Simek /* 271b72894f1SMichal Simek * One terminating char + one byte for space between mode 272b72894f1SMichal Simek * and default boot_targets 273b72894f1SMichal Simek */ 274b72894f1SMichal Simek new_targets = calloc(1, strlen(mode) + 275b72894f1SMichal Simek strlen(getenv("boot_targets")) + 2); 276b72894f1SMichal Simek 277b72894f1SMichal Simek sprintf(new_targets, "%s %s", mode, getenv("boot_targets")); 278b72894f1SMichal Simek setenv("boot_targets", new_targets); 279b72894f1SMichal Simek 28084c7204bSMichal Simek return 0; 28184c7204bSMichal Simek } 28284696ff5SSiva Durga Prasad Paladugu 28384696ff5SSiva Durga Prasad Paladugu int checkboard(void) 28484696ff5SSiva Durga Prasad Paladugu { 2855af08556SMichal Simek puts("Board: Xilinx ZynqMP\n"); 28684696ff5SSiva Durga Prasad Paladugu return 0; 28784696ff5SSiva Durga Prasad Paladugu } 28816fa00a7SSiva Durga Prasad Paladugu 28916fa00a7SSiva Durga Prasad Paladugu #ifdef CONFIG_USB_DWC3 290275bd6d1SMichal Simek static struct dwc3_device dwc3_device_data0 = { 29116fa00a7SSiva Durga Prasad Paladugu .maximum_speed = USB_SPEED_HIGH, 29216fa00a7SSiva Durga Prasad Paladugu .base = ZYNQMP_USB0_XHCI_BASEADDR, 29316fa00a7SSiva Durga Prasad Paladugu .dr_mode = USB_DR_MODE_PERIPHERAL, 29416fa00a7SSiva Durga Prasad Paladugu .index = 0, 29516fa00a7SSiva Durga Prasad Paladugu }; 29616fa00a7SSiva Durga Prasad Paladugu 297275bd6d1SMichal Simek static struct dwc3_device dwc3_device_data1 = { 298275bd6d1SMichal Simek .maximum_speed = USB_SPEED_HIGH, 299275bd6d1SMichal Simek .base = ZYNQMP_USB1_XHCI_BASEADDR, 300275bd6d1SMichal Simek .dr_mode = USB_DR_MODE_PERIPHERAL, 301275bd6d1SMichal Simek .index = 1, 302275bd6d1SMichal Simek }; 303275bd6d1SMichal Simek 3049feff385SMichal Simek int usb_gadget_handle_interrupts(int index) 30516fa00a7SSiva Durga Prasad Paladugu { 3069feff385SMichal Simek dwc3_uboot_handle_interrupt(index); 30716fa00a7SSiva Durga Prasad Paladugu return 0; 30816fa00a7SSiva Durga Prasad Paladugu } 30916fa00a7SSiva Durga Prasad Paladugu 31016fa00a7SSiva Durga Prasad Paladugu int board_usb_init(int index, enum usb_init_type init) 31116fa00a7SSiva Durga Prasad Paladugu { 312275bd6d1SMichal Simek debug("%s: index %x\n", __func__, index); 313275bd6d1SMichal Simek 3148ecd50c8SMichal Simek #if defined(CONFIG_USB_GADGET_DOWNLOAD) 3158ecd50c8SMichal Simek g_dnl_set_serialnumber(CONFIG_SYS_CONFIG_NAME); 3168ecd50c8SMichal Simek #endif 3178ecd50c8SMichal Simek 318275bd6d1SMichal Simek switch (index) { 319275bd6d1SMichal Simek case 0: 320275bd6d1SMichal Simek return dwc3_uboot_init(&dwc3_device_data0); 321275bd6d1SMichal Simek case 1: 322275bd6d1SMichal Simek return dwc3_uboot_init(&dwc3_device_data1); 323275bd6d1SMichal Simek }; 324275bd6d1SMichal Simek 325275bd6d1SMichal Simek return -1; 32616fa00a7SSiva Durga Prasad Paladugu } 32716fa00a7SSiva Durga Prasad Paladugu 32816fa00a7SSiva Durga Prasad Paladugu int board_usb_cleanup(int index, enum usb_init_type init) 32916fa00a7SSiva Durga Prasad Paladugu { 33016fa00a7SSiva Durga Prasad Paladugu dwc3_uboot_exit(index); 33116fa00a7SSiva Durga Prasad Paladugu return 0; 33216fa00a7SSiva Durga Prasad Paladugu } 33316fa00a7SSiva Durga Prasad Paladugu #endif 334