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 79*db3123b4SSiva Durga Prasad Paladugu static int chip_id(unsigned char id) 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; 86*db3123b4SSiva Durga Prasad Paladugu int val = -EINVAL; 8747e60cbdSMichal Simek 8847e60cbdSMichal Simek smc_call(®s); 8947e60cbdSMichal Simek 900cba6abbSSoren Brinkmann /* 910cba6abbSSoren Brinkmann * SMC returns: 920cba6abbSSoren Brinkmann * regs[0][31:0] = status of the operation 930cba6abbSSoren Brinkmann * regs[0][63:32] = CSU.IDCODE register 940cba6abbSSoren Brinkmann * regs[1][31:0] = CSU.version register 950cba6abbSSoren Brinkmann */ 96*db3123b4SSiva Durga Prasad Paladugu switch (id) { 97*db3123b4SSiva Durga Prasad Paladugu case IDCODE: 980cba6abbSSoren Brinkmann regs.regs[0] = upper_32_bits(regs.regs[0]); 990cba6abbSSoren Brinkmann regs.regs[0] &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK | 1000cba6abbSSoren Brinkmann ZYNQMP_CSU_IDCODE_SVD_MASK; 1010cba6abbSSoren Brinkmann regs.regs[0] >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT; 102*db3123b4SSiva Durga Prasad Paladugu val = regs.regs[0]; 103*db3123b4SSiva Durga Prasad Paladugu break; 104*db3123b4SSiva Durga Prasad Paladugu case VERSION: 105*db3123b4SSiva Durga Prasad Paladugu regs.regs[1] = lower_32_bits(regs.regs[1]); 106*db3123b4SSiva Durga Prasad Paladugu regs.regs[1] &= ZYNQMP_CSU_SILICON_VER_MASK; 107*db3123b4SSiva Durga Prasad Paladugu val = regs.regs[1]; 108*db3123b4SSiva Durga Prasad Paladugu break; 109*db3123b4SSiva Durga Prasad Paladugu default: 110*db3123b4SSiva Durga Prasad Paladugu printf("%s, Invalid Req:0x%x\n", __func__, id); 111*db3123b4SSiva Durga Prasad Paladugu } 1120cba6abbSSoren Brinkmann 113*db3123b4SSiva Durga Prasad Paladugu return val; 11447e60cbdSMichal Simek } 11547e60cbdSMichal Simek 11647e60cbdSMichal Simek static char *zynqmp_get_silicon_idcode_name(void) 11747e60cbdSMichal Simek { 11847e60cbdSMichal Simek uint32_t i, id; 11947e60cbdSMichal Simek 120*db3123b4SSiva Durga Prasad Paladugu id = chip_id(IDCODE); 12147e60cbdSMichal Simek for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) { 12247e60cbdSMichal Simek if (zynqmp_devices[i].id == id) 12347e60cbdSMichal Simek return zynqmp_devices[i].name; 12447e60cbdSMichal Simek } 12547e60cbdSMichal Simek return "unknown"; 12647e60cbdSMichal Simek } 12747e60cbdSMichal Simek #endif 12847e60cbdSMichal Simek 129fb4000e8SMichal Simek int board_early_init_f(void) 130fb4000e8SMichal Simek { 131fb4000e8SMichal Simek #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CLK_ZYNQMP) 132fb4000e8SMichal Simek zynqmp_pmufw_version(); 133fb4000e8SMichal Simek #endif 13455de0929SMichal Simek 135fd1b635cSMichal Simek #if defined(CONFIG_SPL_BUILD) || defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED) 13655de0929SMichal Simek psu_init(); 13755de0929SMichal Simek #endif 13855de0929SMichal Simek 139fb4000e8SMichal Simek return 0; 140fb4000e8SMichal Simek } 141fb4000e8SMichal Simek 14247e60cbdSMichal Simek #define ZYNQMP_VERSION_SIZE 9 14347e60cbdSMichal Simek 14484c7204bSMichal Simek int board_init(void) 14584c7204bSMichal Simek { 146a0736efbSMichal Simek printf("EL Level:\tEL%d\n", current_el()); 147a0736efbSMichal Simek 14847e60cbdSMichal Simek #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \ 14947e60cbdSMichal Simek !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \ 15047e60cbdSMichal Simek defined(CONFIG_SPL_BUILD)) 15147e60cbdSMichal Simek if (current_el() != 3) { 15247e60cbdSMichal Simek static char version[ZYNQMP_VERSION_SIZE]; 15347e60cbdSMichal Simek 15447e60cbdSMichal Simek strncat(version, "xczu", ZYNQMP_VERSION_SIZE); 15547e60cbdSMichal Simek zynqmppl.name = strncat(version, 15647e60cbdSMichal Simek zynqmp_get_silicon_idcode_name(), 15747e60cbdSMichal Simek ZYNQMP_VERSION_SIZE); 15847e60cbdSMichal Simek printf("Chip ID:\t%s\n", zynqmppl.name); 15947e60cbdSMichal Simek fpga_init(); 16047e60cbdSMichal Simek fpga_add(fpga_xilinx, &zynqmppl); 16147e60cbdSMichal Simek } 16247e60cbdSMichal Simek #endif 16347e60cbdSMichal Simek 16484c7204bSMichal Simek return 0; 16584c7204bSMichal Simek } 16684c7204bSMichal Simek 16784c7204bSMichal Simek int board_early_init_r(void) 16884c7204bSMichal Simek { 16984c7204bSMichal Simek u32 val; 17084c7204bSMichal Simek 17190a35db4SMichal Simek val = readl(&crlapb_base->timestamp_ref_ctrl); 17290a35db4SMichal Simek val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT; 17390a35db4SMichal Simek 17490a35db4SMichal Simek if (current_el() == 3 && !val) { 17584c7204bSMichal Simek val = readl(&crlapb_base->timestamp_ref_ctrl); 17684c7204bSMichal Simek val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT; 17784c7204bSMichal Simek writel(val, &crlapb_base->timestamp_ref_ctrl); 17884c7204bSMichal Simek 1790785dfd8SMichal Simek /* Program freq register in System counter */ 1800785dfd8SMichal Simek writel(zynqmp_get_system_timer_freq(), 1810785dfd8SMichal Simek &iou_scntr_secure->base_frequency_id_register); 1820785dfd8SMichal Simek /* And enable system counter */ 1830785dfd8SMichal Simek writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN, 1840785dfd8SMichal Simek &iou_scntr_secure->counter_control_register); 1850785dfd8SMichal Simek } 18684c7204bSMichal Simek return 0; 18784c7204bSMichal Simek } 18884c7204bSMichal Simek 1896919b4bfSMichal Simek int zynq_board_read_rom_ethaddr(unsigned char *ethaddr) 1906919b4bfSMichal Simek { 1916919b4bfSMichal Simek #if defined(CONFIG_ZYNQ_GEM_EEPROM_ADDR) && \ 1926919b4bfSMichal Simek defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET) && \ 1936919b4bfSMichal Simek defined(CONFIG_ZYNQ_EEPROM_BUS) 1946919b4bfSMichal Simek i2c_set_bus_num(CONFIG_ZYNQ_EEPROM_BUS); 1956919b4bfSMichal Simek 1966919b4bfSMichal Simek if (eeprom_read(CONFIG_ZYNQ_GEM_EEPROM_ADDR, 1976919b4bfSMichal Simek CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET, 1986919b4bfSMichal Simek ethaddr, 6)) 1996919b4bfSMichal Simek printf("I2C EEPROM MAC address read failed\n"); 2006919b4bfSMichal Simek #endif 2016919b4bfSMichal Simek 2026919b4bfSMichal Simek return 0; 2036919b4bfSMichal Simek } 2046919b4bfSMichal Simek 2058d59d7f6SMichal Simek #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE) 20676b00acaSSimon Glass int dram_init_banksize(void) 207361a8799STom Rini { 208950f86caSNathan Rossi fdtdec_setup_memory_banksize(); 20976b00acaSSimon Glass 21076b00acaSSimon Glass return 0; 2118d59d7f6SMichal Simek } 2128d59d7f6SMichal Simek 2138d59d7f6SMichal Simek int dram_init(void) 2148d59d7f6SMichal Simek { 215950f86caSNathan Rossi if (fdtdec_setup_memory_size() != 0) 216950f86caSNathan Rossi return -EINVAL; 2178d59d7f6SMichal Simek 2188d59d7f6SMichal Simek return 0; 2198d59d7f6SMichal Simek } 2208d59d7f6SMichal Simek #else 22184c7204bSMichal Simek int dram_init(void) 22284c7204bSMichal Simek { 22384c7204bSMichal Simek gd->ram_size = CONFIG_SYS_SDRAM_SIZE; 22484c7204bSMichal Simek 22584c7204bSMichal Simek return 0; 22684c7204bSMichal Simek } 2278d59d7f6SMichal Simek #endif 22884c7204bSMichal Simek 22984c7204bSMichal Simek void reset_cpu(ulong addr) 23084c7204bSMichal Simek { 23184c7204bSMichal Simek } 23284c7204bSMichal Simek 23384c7204bSMichal Simek int board_late_init(void) 23484c7204bSMichal Simek { 23584c7204bSMichal Simek u32 reg = 0; 23684c7204bSMichal Simek u8 bootmode; 237b72894f1SMichal Simek const char *mode; 238b72894f1SMichal Simek char *new_targets; 239b72894f1SMichal Simek 240b72894f1SMichal Simek if (!(gd->flags & GD_FLG_ENV_DEFAULT)) { 241b72894f1SMichal Simek debug("Saved variables - Skipping\n"); 242b72894f1SMichal Simek return 0; 243b72894f1SMichal Simek } 24484c7204bSMichal Simek 24584c7204bSMichal Simek reg = readl(&crlapb_base->boot_mode); 24647359a03SMichal Simek if (reg >> BOOT_MODE_ALT_SHIFT) 24747359a03SMichal Simek reg >>= BOOT_MODE_ALT_SHIFT; 24847359a03SMichal Simek 24984c7204bSMichal Simek bootmode = reg & BOOT_MODES_MASK; 25084c7204bSMichal Simek 251fb90917cSMichal Simek puts("Bootmode: "); 25284c7204bSMichal Simek switch (bootmode) { 253d58fc12eSMichal Simek case USB_MODE: 254d58fc12eSMichal Simek puts("USB_MODE\n"); 255d58fc12eSMichal Simek mode = "usb"; 256d58fc12eSMichal Simek break; 2570a5bcc8cSSiva Durga Prasad Paladugu case JTAG_MODE: 258fb90917cSMichal Simek puts("JTAG_MODE\n"); 259b72894f1SMichal Simek mode = "pxe dhcp"; 2600a5bcc8cSSiva Durga Prasad Paladugu break; 2610a5bcc8cSSiva Durga Prasad Paladugu case QSPI_MODE_24BIT: 2620a5bcc8cSSiva Durga Prasad Paladugu case QSPI_MODE_32BIT: 263b72894f1SMichal Simek mode = "qspi0"; 264fb90917cSMichal Simek puts("QSPI_MODE\n"); 2650a5bcc8cSSiva Durga Prasad Paladugu break; 26639c56f55SMichal Simek case EMMC_MODE: 26778678feeSMichal Simek puts("EMMC_MODE\n"); 268b72894f1SMichal Simek mode = "mmc0"; 26978678feeSMichal Simek break; 27078678feeSMichal Simek case SD_MODE: 271fb90917cSMichal Simek puts("SD_MODE\n"); 272b72894f1SMichal Simek mode = "mmc0"; 27384c7204bSMichal Simek break; 274e1992276SSiva Durga Prasad Paladugu case SD1_LSHFT_MODE: 275e1992276SSiva Durga Prasad Paladugu puts("LVL_SHFT_"); 276e1992276SSiva Durga Prasad Paladugu /* fall through */ 277af813acdSMichal Simek case SD_MODE1: 278fb90917cSMichal Simek puts("SD_MODE1\n"); 2792d9925bcSMichal Simek #if defined(CONFIG_ZYNQ_SDHCI0) && defined(CONFIG_ZYNQ_SDHCI1) 280b72894f1SMichal Simek mode = "mmc1"; 281b72894f1SMichal Simek #else 282b72894f1SMichal Simek mode = "mmc0"; 2832d9925bcSMichal Simek #endif 284af813acdSMichal Simek break; 285af813acdSMichal Simek case NAND_MODE: 286fb90917cSMichal Simek puts("NAND_MODE\n"); 287b72894f1SMichal Simek mode = "nand0"; 288af813acdSMichal Simek break; 28984c7204bSMichal Simek default: 290b72894f1SMichal Simek mode = ""; 29184c7204bSMichal Simek printf("Invalid Boot Mode:0x%x\n", bootmode); 29284c7204bSMichal Simek break; 29384c7204bSMichal Simek } 29484c7204bSMichal Simek 295b72894f1SMichal Simek /* 296b72894f1SMichal Simek * One terminating char + one byte for space between mode 297b72894f1SMichal Simek * and default boot_targets 298b72894f1SMichal Simek */ 299b72894f1SMichal Simek new_targets = calloc(1, strlen(mode) + 300b72894f1SMichal Simek strlen(getenv("boot_targets")) + 2); 301b72894f1SMichal Simek 302b72894f1SMichal Simek sprintf(new_targets, "%s %s", mode, getenv("boot_targets")); 303b72894f1SMichal Simek setenv("boot_targets", new_targets); 304b72894f1SMichal Simek 30584c7204bSMichal Simek return 0; 30684c7204bSMichal Simek } 30784696ff5SSiva Durga Prasad Paladugu 30884696ff5SSiva Durga Prasad Paladugu int checkboard(void) 30984696ff5SSiva Durga Prasad Paladugu { 3105af08556SMichal Simek puts("Board: Xilinx ZynqMP\n"); 31184696ff5SSiva Durga Prasad Paladugu return 0; 31284696ff5SSiva Durga Prasad Paladugu } 31316fa00a7SSiva Durga Prasad Paladugu 31416fa00a7SSiva Durga Prasad Paladugu #ifdef CONFIG_USB_DWC3 315275bd6d1SMichal Simek static struct dwc3_device dwc3_device_data0 = { 31616fa00a7SSiva Durga Prasad Paladugu .maximum_speed = USB_SPEED_HIGH, 31716fa00a7SSiva Durga Prasad Paladugu .base = ZYNQMP_USB0_XHCI_BASEADDR, 31816fa00a7SSiva Durga Prasad Paladugu .dr_mode = USB_DR_MODE_PERIPHERAL, 31916fa00a7SSiva Durga Prasad Paladugu .index = 0, 32016fa00a7SSiva Durga Prasad Paladugu }; 32116fa00a7SSiva Durga Prasad Paladugu 322275bd6d1SMichal Simek static struct dwc3_device dwc3_device_data1 = { 323275bd6d1SMichal Simek .maximum_speed = USB_SPEED_HIGH, 324275bd6d1SMichal Simek .base = ZYNQMP_USB1_XHCI_BASEADDR, 325275bd6d1SMichal Simek .dr_mode = USB_DR_MODE_PERIPHERAL, 326275bd6d1SMichal Simek .index = 1, 327275bd6d1SMichal Simek }; 328275bd6d1SMichal Simek 3299feff385SMichal Simek int usb_gadget_handle_interrupts(int index) 33016fa00a7SSiva Durga Prasad Paladugu { 3319feff385SMichal Simek dwc3_uboot_handle_interrupt(index); 33216fa00a7SSiva Durga Prasad Paladugu return 0; 33316fa00a7SSiva Durga Prasad Paladugu } 33416fa00a7SSiva Durga Prasad Paladugu 33516fa00a7SSiva Durga Prasad Paladugu int board_usb_init(int index, enum usb_init_type init) 33616fa00a7SSiva Durga Prasad Paladugu { 337275bd6d1SMichal Simek debug("%s: index %x\n", __func__, index); 338275bd6d1SMichal Simek 3398ecd50c8SMichal Simek #if defined(CONFIG_USB_GADGET_DOWNLOAD) 3408ecd50c8SMichal Simek g_dnl_set_serialnumber(CONFIG_SYS_CONFIG_NAME); 3418ecd50c8SMichal Simek #endif 3428ecd50c8SMichal Simek 343275bd6d1SMichal Simek switch (index) { 344275bd6d1SMichal Simek case 0: 345275bd6d1SMichal Simek return dwc3_uboot_init(&dwc3_device_data0); 346275bd6d1SMichal Simek case 1: 347275bd6d1SMichal Simek return dwc3_uboot_init(&dwc3_device_data1); 348275bd6d1SMichal Simek }; 349275bd6d1SMichal Simek 350275bd6d1SMichal Simek return -1; 35116fa00a7SSiva Durga Prasad Paladugu } 35216fa00a7SSiva Durga Prasad Paladugu 35316fa00a7SSiva Durga Prasad Paladugu int board_usb_cleanup(int index, enum usb_init_type init) 35416fa00a7SSiva Durga Prasad Paladugu { 35516fa00a7SSiva Durga Prasad Paladugu dwc3_uboot_exit(index); 35616fa00a7SSiva Durga Prasad Paladugu return 0; 35716fa00a7SSiva Durga Prasad Paladugu } 35816fa00a7SSiva Durga Prasad Paladugu #endif 359