xref: /openbmc/u-boot/board/xilinx/zynqmp/zynqmp.c (revision b72894f14da79cdcdab8007b079d922bd28e5ce7)
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>
12*b72894f1SMichal 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>
196919b4bfSMichal Simek #include <i2c.h>
2084c7204bSMichal Simek 
2184c7204bSMichal Simek DECLARE_GLOBAL_DATA_PTR;
2284c7204bSMichal Simek 
2384c7204bSMichal Simek int board_init(void)
2484c7204bSMichal Simek {
25a0736efbSMichal Simek 	printf("EL Level:\tEL%d\n", current_el());
26a0736efbSMichal Simek 
2784c7204bSMichal Simek 	return 0;
2884c7204bSMichal Simek }
2984c7204bSMichal Simek 
3084c7204bSMichal Simek int board_early_init_r(void)
3184c7204bSMichal Simek {
3284c7204bSMichal Simek 	u32 val;
3384c7204bSMichal Simek 
340785dfd8SMichal Simek 	if (current_el() == 3) {
3584c7204bSMichal Simek 		val = readl(&crlapb_base->timestamp_ref_ctrl);
3684c7204bSMichal Simek 		val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
3784c7204bSMichal Simek 		writel(val, &crlapb_base->timestamp_ref_ctrl);
3884c7204bSMichal Simek 
390785dfd8SMichal Simek 		/* Program freq register in System counter */
400785dfd8SMichal Simek 		writel(zynqmp_get_system_timer_freq(),
410785dfd8SMichal Simek 		       &iou_scntr_secure->base_frequency_id_register);
420785dfd8SMichal Simek 		/* And enable system counter */
430785dfd8SMichal Simek 		writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
440785dfd8SMichal Simek 		       &iou_scntr_secure->counter_control_register);
450785dfd8SMichal Simek 	}
4684c7204bSMichal Simek 	/* Program freq register in System counter and enable system counter */
4784c7204bSMichal Simek 	writel(gd->cpu_clk, &iou_scntr->base_frequency_id_register);
4884c7204bSMichal Simek 	writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_HDBG |
4984c7204bSMichal Simek 	       ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
5084c7204bSMichal Simek 	       &iou_scntr->counter_control_register);
5184c7204bSMichal Simek 
5284c7204bSMichal Simek 	return 0;
5384c7204bSMichal Simek }
5484c7204bSMichal Simek 
556919b4bfSMichal Simek int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
566919b4bfSMichal Simek {
576919b4bfSMichal Simek #if defined(CONFIG_ZYNQ_GEM_EEPROM_ADDR) && \
586919b4bfSMichal Simek     defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET) && \
596919b4bfSMichal Simek     defined(CONFIG_ZYNQ_EEPROM_BUS)
606919b4bfSMichal Simek 	i2c_set_bus_num(CONFIG_ZYNQ_EEPROM_BUS);
616919b4bfSMichal Simek 
626919b4bfSMichal Simek 	if (eeprom_read(CONFIG_ZYNQ_GEM_EEPROM_ADDR,
636919b4bfSMichal Simek 			CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET,
646919b4bfSMichal Simek 			ethaddr, 6))
656919b4bfSMichal Simek 		printf("I2C EEPROM MAC address read failed\n");
666919b4bfSMichal Simek #endif
676919b4bfSMichal Simek 
686919b4bfSMichal Simek 	return 0;
696919b4bfSMichal Simek }
706919b4bfSMichal Simek 
718d59d7f6SMichal Simek #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
728d59d7f6SMichal Simek /*
738d59d7f6SMichal Simek  * fdt_get_reg - Fill buffer by information from DT
748d59d7f6SMichal Simek  */
758d59d7f6SMichal Simek static phys_size_t fdt_get_reg(const void *fdt, int nodeoffset, void *buf,
768d59d7f6SMichal Simek 			       const u32 *cell, int n)
778d59d7f6SMichal Simek {
788d59d7f6SMichal Simek 	int i = 0, b, banks;
798d59d7f6SMichal Simek 	int parent_offset = fdt_parent_offset(fdt, nodeoffset);
808d59d7f6SMichal Simek 	int address_cells = fdt_address_cells(fdt, parent_offset);
818d59d7f6SMichal Simek 	int size_cells = fdt_size_cells(fdt, parent_offset);
828d59d7f6SMichal Simek 	char *p = buf;
83658b3a56SMichal Simek 	u64 val;
84658b3a56SMichal Simek 	u64 vals;
858d59d7f6SMichal Simek 
868d59d7f6SMichal Simek 	debug("%s: addr_cells=%x, size_cell=%x, buf=%p, cell=%p\n",
878d59d7f6SMichal Simek 	      __func__, address_cells, size_cells, buf, cell);
888d59d7f6SMichal Simek 
898d59d7f6SMichal Simek 	/* Check memory bank setup */
908d59d7f6SMichal Simek 	banks = n % (address_cells + size_cells);
918d59d7f6SMichal Simek 	if (banks)
928d59d7f6SMichal Simek 		panic("Incorrect memory setup cells=%d, ac=%d, sc=%d\n",
938d59d7f6SMichal Simek 		      n, address_cells, size_cells);
948d59d7f6SMichal Simek 
958d59d7f6SMichal Simek 	banks = n / (address_cells + size_cells);
968d59d7f6SMichal Simek 
978d59d7f6SMichal Simek 	for (b = 0; b < banks; b++) {
988d59d7f6SMichal Simek 		debug("%s: Bank #%d:\n", __func__, b);
998d59d7f6SMichal Simek 		if (address_cells == 2) {
1008d59d7f6SMichal Simek 			val = cell[i + 1];
1018d59d7f6SMichal Simek 			val <<= 32;
1028d59d7f6SMichal Simek 			val |= cell[i];
1038d59d7f6SMichal Simek 			val = fdt64_to_cpu(val);
1048d59d7f6SMichal Simek 			debug("%s: addr64=%llx, ptr=%p, cell=%p\n",
1058d59d7f6SMichal Simek 			      __func__, val, p, &cell[i]);
1068d59d7f6SMichal Simek 			*(phys_addr_t *)p = val;
1078d59d7f6SMichal Simek 		} else {
1088d59d7f6SMichal Simek 			debug("%s: addr32=%x, ptr=%p\n",
1098d59d7f6SMichal Simek 			      __func__, fdt32_to_cpu(cell[i]), p);
1108d59d7f6SMichal Simek 			*(phys_addr_t *)p = fdt32_to_cpu(cell[i]);
1118d59d7f6SMichal Simek 		}
1128d59d7f6SMichal Simek 		p += sizeof(phys_addr_t);
1138d59d7f6SMichal Simek 		i += address_cells;
1148d59d7f6SMichal Simek 
1158d59d7f6SMichal Simek 		debug("%s: pa=%p, i=%x, size=%zu\n", __func__, p, i,
1168d59d7f6SMichal Simek 		      sizeof(phys_addr_t));
1178d59d7f6SMichal Simek 
1188d59d7f6SMichal Simek 		if (size_cells == 2) {
1198d59d7f6SMichal Simek 			vals = cell[i + 1];
1208d59d7f6SMichal Simek 			vals <<= 32;
1218d59d7f6SMichal Simek 			vals |= cell[i];
1228d59d7f6SMichal Simek 			vals = fdt64_to_cpu(vals);
1238d59d7f6SMichal Simek 
1248d59d7f6SMichal Simek 			debug("%s: size64=%llx, ptr=%p, cell=%p\n",
1258d59d7f6SMichal Simek 			      __func__, vals, p, &cell[i]);
1268d59d7f6SMichal Simek 			*(phys_size_t *)p = vals;
1278d59d7f6SMichal Simek 		} else {
1288d59d7f6SMichal Simek 			debug("%s: size32=%x, ptr=%p\n",
1298d59d7f6SMichal Simek 			      __func__, fdt32_to_cpu(cell[i]), p);
1308d59d7f6SMichal Simek 			*(phys_size_t *)p = fdt32_to_cpu(cell[i]);
1318d59d7f6SMichal Simek 		}
1328d59d7f6SMichal Simek 		p += sizeof(phys_size_t);
1338d59d7f6SMichal Simek 		i += size_cells;
1348d59d7f6SMichal Simek 
1358d59d7f6SMichal Simek 		debug("%s: ps=%p, i=%x, size=%zu\n",
1368d59d7f6SMichal Simek 		      __func__, p, i, sizeof(phys_size_t));
1378d59d7f6SMichal Simek 	}
1388d59d7f6SMichal Simek 
1398d59d7f6SMichal Simek 	/* Return the first address size */
1408d59d7f6SMichal Simek 	return *(phys_size_t *)((char *)buf + sizeof(phys_addr_t));
1418d59d7f6SMichal Simek }
1428d59d7f6SMichal Simek 
1438d59d7f6SMichal Simek #define FDT_REG_SIZE  sizeof(u32)
1448d59d7f6SMichal Simek /* Temp location for sharing data for storing */
1458d59d7f6SMichal Simek /* Up to 64-bit address + 64-bit size */
1468d59d7f6SMichal Simek static u8 tmp[CONFIG_NR_DRAM_BANKS * 16];
1478d59d7f6SMichal Simek 
1488d59d7f6SMichal Simek void dram_init_banksize(void)
1498d59d7f6SMichal Simek {
1508d59d7f6SMichal Simek 	int bank;
1518d59d7f6SMichal Simek 
1528d59d7f6SMichal Simek 	memcpy(&gd->bd->bi_dram[0], &tmp, sizeof(tmp));
1538d59d7f6SMichal Simek 
1548d59d7f6SMichal Simek 	for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
1558d59d7f6SMichal Simek 		debug("Bank #%d: start %llx\n", bank,
1568d59d7f6SMichal Simek 		      (unsigned long long)gd->bd->bi_dram[bank].start);
1578d59d7f6SMichal Simek 		debug("Bank #%d: size %llx\n", bank,
1588d59d7f6SMichal Simek 		      (unsigned long long)gd->bd->bi_dram[bank].size);
1598d59d7f6SMichal Simek 	}
1608d59d7f6SMichal Simek }
1618d59d7f6SMichal Simek 
1628d59d7f6SMichal Simek int dram_init(void)
1638d59d7f6SMichal Simek {
1648d59d7f6SMichal Simek 	int node, len;
1658d59d7f6SMichal Simek 	const void *blob = gd->fdt_blob;
1668d59d7f6SMichal Simek 	const u32 *cell;
1678d59d7f6SMichal Simek 
1688d59d7f6SMichal Simek 	memset(&tmp, 0, sizeof(tmp));
1698d59d7f6SMichal Simek 
1708d59d7f6SMichal Simek 	/* find or create "/memory" node. */
1718d59d7f6SMichal Simek 	node = fdt_subnode_offset(blob, 0, "memory");
1728d59d7f6SMichal Simek 	if (node < 0) {
1738d59d7f6SMichal Simek 		printf("%s: Can't get memory node\n", __func__);
1748d59d7f6SMichal Simek 		return node;
1758d59d7f6SMichal Simek 	}
1768d59d7f6SMichal Simek 
1778d59d7f6SMichal Simek 	/* Get pointer to cells and lenght of it */
1788d59d7f6SMichal Simek 	cell = fdt_getprop(blob, node, "reg", &len);
1798d59d7f6SMichal Simek 	if (!cell) {
1808d59d7f6SMichal Simek 		printf("%s: Can't get reg property\n", __func__);
1818d59d7f6SMichal Simek 		return -1;
1828d59d7f6SMichal Simek 	}
1838d59d7f6SMichal Simek 
1848d59d7f6SMichal Simek 	gd->ram_size = fdt_get_reg(blob, node, &tmp, cell, len / FDT_REG_SIZE);
1858d59d7f6SMichal Simek 
186658b3a56SMichal Simek 	debug("%s: Initial DRAM size %llx\n", __func__, (u64)gd->ram_size);
1878d59d7f6SMichal Simek 
1888d59d7f6SMichal Simek 	return 0;
1898d59d7f6SMichal Simek }
1908d59d7f6SMichal Simek #else
19184c7204bSMichal Simek int dram_init(void)
19284c7204bSMichal Simek {
19384c7204bSMichal Simek 	gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
19484c7204bSMichal Simek 
19584c7204bSMichal Simek 	return 0;
19684c7204bSMichal Simek }
1978d59d7f6SMichal Simek #endif
19884c7204bSMichal Simek 
19984c7204bSMichal Simek void reset_cpu(ulong addr)
20084c7204bSMichal Simek {
20184c7204bSMichal Simek }
20284c7204bSMichal Simek 
2036fe6f135SMichal Simek #ifdef CONFIG_SCSI_AHCI_PLAT
2046fe6f135SMichal Simek void scsi_init(void)
2056fe6f135SMichal Simek {
206679b994aSMichal Simek #if defined(CONFIG_SATA_CEVA)
207679b994aSMichal Simek 	init_sata(0);
208679b994aSMichal Simek #endif
2096fe6f135SMichal Simek 	ahci_init((void __iomem *)ZYNQMP_SATA_BASEADDR);
2106fe6f135SMichal Simek 	scsi_scan(1);
2116fe6f135SMichal Simek }
2126fe6f135SMichal Simek #endif
2136fe6f135SMichal Simek 
21484c7204bSMichal Simek int board_late_init(void)
21584c7204bSMichal Simek {
21684c7204bSMichal Simek 	u32 reg = 0;
21784c7204bSMichal Simek 	u8 bootmode;
218*b72894f1SMichal Simek 	const char *mode;
219*b72894f1SMichal Simek 	char *new_targets;
220*b72894f1SMichal Simek 
221*b72894f1SMichal Simek 	if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
222*b72894f1SMichal Simek 		debug("Saved variables - Skipping\n");
223*b72894f1SMichal Simek 		return 0;
224*b72894f1SMichal Simek 	}
22584c7204bSMichal Simek 
22684c7204bSMichal Simek 	reg = readl(&crlapb_base->boot_mode);
22784c7204bSMichal Simek 	bootmode = reg & BOOT_MODES_MASK;
22884c7204bSMichal Simek 
229fb90917cSMichal Simek 	puts("Bootmode: ");
23084c7204bSMichal Simek 	switch (bootmode) {
2310a5bcc8cSSiva Durga Prasad Paladugu 	case JTAG_MODE:
232fb90917cSMichal Simek 		puts("JTAG_MODE\n");
233*b72894f1SMichal Simek 		mode = "pxe dhcp";
2340a5bcc8cSSiva Durga Prasad Paladugu 		break;
2350a5bcc8cSSiva Durga Prasad Paladugu 	case QSPI_MODE_24BIT:
2360a5bcc8cSSiva Durga Prasad Paladugu 	case QSPI_MODE_32BIT:
237*b72894f1SMichal Simek 		mode = "qspi0";
238fb90917cSMichal Simek 		puts("QSPI_MODE\n");
2390a5bcc8cSSiva Durga Prasad Paladugu 		break;
24039c56f55SMichal Simek 	case EMMC_MODE:
24178678feeSMichal Simek 		puts("EMMC_MODE\n");
242*b72894f1SMichal Simek 		mode = "mmc0";
24378678feeSMichal Simek 		break;
24478678feeSMichal Simek 	case SD_MODE:
245fb90917cSMichal Simek 		puts("SD_MODE\n");
246*b72894f1SMichal Simek 		mode = "mmc0";
24784c7204bSMichal Simek 		break;
248af813acdSMichal Simek 	case SD_MODE1:
249fb90917cSMichal Simek 		puts("SD_MODE1\n");
2502d9925bcSMichal Simek #if defined(CONFIG_ZYNQ_SDHCI0) && defined(CONFIG_ZYNQ_SDHCI1)
251*b72894f1SMichal Simek 		mode = "mmc1";
252*b72894f1SMichal Simek #else
253*b72894f1SMichal Simek 		mode = "mmc0";
2542d9925bcSMichal Simek #endif
255af813acdSMichal Simek 		break;
256af813acdSMichal Simek 	case NAND_MODE:
257fb90917cSMichal Simek 		puts("NAND_MODE\n");
258*b72894f1SMichal Simek 		mode = "nand0";
259af813acdSMichal Simek 		break;
26084c7204bSMichal Simek 	default:
261*b72894f1SMichal Simek 		mode = "";
26284c7204bSMichal Simek 		printf("Invalid Boot Mode:0x%x\n", bootmode);
26384c7204bSMichal Simek 		break;
26484c7204bSMichal Simek 	}
26584c7204bSMichal Simek 
266*b72894f1SMichal Simek 	/*
267*b72894f1SMichal Simek 	 * One terminating char + one byte for space between mode
268*b72894f1SMichal Simek 	 * and default boot_targets
269*b72894f1SMichal Simek 	 */
270*b72894f1SMichal Simek 	new_targets = calloc(1, strlen(mode) +
271*b72894f1SMichal Simek 				strlen(getenv("boot_targets")) + 2);
272*b72894f1SMichal Simek 
273*b72894f1SMichal Simek 	sprintf(new_targets, "%s %s", mode, getenv("boot_targets"));
274*b72894f1SMichal Simek 	setenv("boot_targets", new_targets);
275*b72894f1SMichal Simek 
27684c7204bSMichal Simek 	return 0;
27784c7204bSMichal Simek }
27884696ff5SSiva Durga Prasad Paladugu 
27984696ff5SSiva Durga Prasad Paladugu int checkboard(void)
28084696ff5SSiva Durga Prasad Paladugu {
2815af08556SMichal Simek 	puts("Board: Xilinx ZynqMP\n");
28284696ff5SSiva Durga Prasad Paladugu 	return 0;
28384696ff5SSiva Durga Prasad Paladugu }
28416fa00a7SSiva Durga Prasad Paladugu 
28516fa00a7SSiva Durga Prasad Paladugu #ifdef CONFIG_USB_DWC3
28616fa00a7SSiva Durga Prasad Paladugu static struct dwc3_device dwc3_device_data = {
28716fa00a7SSiva Durga Prasad Paladugu 	.maximum_speed = USB_SPEED_HIGH,
28816fa00a7SSiva Durga Prasad Paladugu 	.base = ZYNQMP_USB0_XHCI_BASEADDR,
28916fa00a7SSiva Durga Prasad Paladugu 	.dr_mode = USB_DR_MODE_PERIPHERAL,
29016fa00a7SSiva Durga Prasad Paladugu 	.index = 0,
29116fa00a7SSiva Durga Prasad Paladugu };
29216fa00a7SSiva Durga Prasad Paladugu 
29316fa00a7SSiva Durga Prasad Paladugu int usb_gadget_handle_interrupts(void)
29416fa00a7SSiva Durga Prasad Paladugu {
29516fa00a7SSiva Durga Prasad Paladugu 	dwc3_uboot_handle_interrupt(0);
29616fa00a7SSiva Durga Prasad Paladugu 	return 0;
29716fa00a7SSiva Durga Prasad Paladugu }
29816fa00a7SSiva Durga Prasad Paladugu 
29916fa00a7SSiva Durga Prasad Paladugu int board_usb_init(int index, enum usb_init_type init)
30016fa00a7SSiva Durga Prasad Paladugu {
30116fa00a7SSiva Durga Prasad Paladugu 	return dwc3_uboot_init(&dwc3_device_data);
30216fa00a7SSiva Durga Prasad Paladugu }
30316fa00a7SSiva Durga Prasad Paladugu 
30416fa00a7SSiva Durga Prasad Paladugu int board_usb_cleanup(int index, enum usb_init_type init)
30516fa00a7SSiva Durga Prasad Paladugu {
30616fa00a7SSiva Durga Prasad Paladugu 	dwc3_uboot_exit(index);
30716fa00a7SSiva Durga Prasad Paladugu 	return 0;
30816fa00a7SSiva Durga Prasad Paladugu }
30916fa00a7SSiva Durga Prasad Paladugu #endif
310