xref: /openbmc/u-boot/board/xilinx/zynqmp/zynqmp.c (revision 658b3a563983281b6617f57b6bf2aee2c92a2101)
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>
984c7204bSMichal Simek #include <netdev.h>
10679b994aSMichal Simek #include <sata.h>
116fe6f135SMichal Simek #include <ahci.h>
126fe6f135SMichal Simek #include <scsi.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>
1984c7204bSMichal Simek 
2084c7204bSMichal Simek DECLARE_GLOBAL_DATA_PTR;
2184c7204bSMichal Simek 
2284c7204bSMichal Simek int board_init(void)
2384c7204bSMichal Simek {
24a0736efbSMichal Simek 	printf("EL Level:\tEL%d\n", current_el());
25a0736efbSMichal Simek 
2684c7204bSMichal Simek 	return 0;
2784c7204bSMichal Simek }
2884c7204bSMichal Simek 
2984c7204bSMichal Simek int board_early_init_r(void)
3084c7204bSMichal Simek {
3184c7204bSMichal Simek 	u32 val;
3284c7204bSMichal Simek 
330785dfd8SMichal Simek 	if (current_el() == 3) {
3484c7204bSMichal Simek 		val = readl(&crlapb_base->timestamp_ref_ctrl);
3584c7204bSMichal Simek 		val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
3684c7204bSMichal Simek 		writel(val, &crlapb_base->timestamp_ref_ctrl);
3784c7204bSMichal Simek 
380785dfd8SMichal Simek 		/* Program freq register in System counter */
390785dfd8SMichal Simek 		writel(zynqmp_get_system_timer_freq(),
400785dfd8SMichal Simek 		       &iou_scntr_secure->base_frequency_id_register);
410785dfd8SMichal Simek 		/* And enable system counter */
420785dfd8SMichal Simek 		writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
430785dfd8SMichal Simek 		       &iou_scntr_secure->counter_control_register);
440785dfd8SMichal Simek 	}
4584c7204bSMichal Simek 	/* Program freq register in System counter and enable system counter */
4684c7204bSMichal Simek 	writel(gd->cpu_clk, &iou_scntr->base_frequency_id_register);
4784c7204bSMichal Simek 	writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_HDBG |
4884c7204bSMichal Simek 	       ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
4984c7204bSMichal Simek 	       &iou_scntr->counter_control_register);
5084c7204bSMichal Simek 
5184c7204bSMichal Simek 	return 0;
5284c7204bSMichal Simek }
5384c7204bSMichal Simek 
548d59d7f6SMichal Simek #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
558d59d7f6SMichal Simek /*
568d59d7f6SMichal Simek  * fdt_get_reg - Fill buffer by information from DT
578d59d7f6SMichal Simek  */
588d59d7f6SMichal Simek static phys_size_t fdt_get_reg(const void *fdt, int nodeoffset, void *buf,
598d59d7f6SMichal Simek 			       const u32 *cell, int n)
608d59d7f6SMichal Simek {
618d59d7f6SMichal Simek 	int i = 0, b, banks;
628d59d7f6SMichal Simek 	int parent_offset = fdt_parent_offset(fdt, nodeoffset);
638d59d7f6SMichal Simek 	int address_cells = fdt_address_cells(fdt, parent_offset);
648d59d7f6SMichal Simek 	int size_cells = fdt_size_cells(fdt, parent_offset);
658d59d7f6SMichal Simek 	char *p = buf;
66*658b3a56SMichal Simek 	u64 val;
67*658b3a56SMichal Simek 	u64 vals;
688d59d7f6SMichal Simek 
698d59d7f6SMichal Simek 	debug("%s: addr_cells=%x, size_cell=%x, buf=%p, cell=%p\n",
708d59d7f6SMichal Simek 	      __func__, address_cells, size_cells, buf, cell);
718d59d7f6SMichal Simek 
728d59d7f6SMichal Simek 	/* Check memory bank setup */
738d59d7f6SMichal Simek 	banks = n % (address_cells + size_cells);
748d59d7f6SMichal Simek 	if (banks)
758d59d7f6SMichal Simek 		panic("Incorrect memory setup cells=%d, ac=%d, sc=%d\n",
768d59d7f6SMichal Simek 		      n, address_cells, size_cells);
778d59d7f6SMichal Simek 
788d59d7f6SMichal Simek 	banks = n / (address_cells + size_cells);
798d59d7f6SMichal Simek 
808d59d7f6SMichal Simek 	for (b = 0; b < banks; b++) {
818d59d7f6SMichal Simek 		debug("%s: Bank #%d:\n", __func__, b);
828d59d7f6SMichal Simek 		if (address_cells == 2) {
838d59d7f6SMichal Simek 			val = cell[i + 1];
848d59d7f6SMichal Simek 			val <<= 32;
858d59d7f6SMichal Simek 			val |= cell[i];
868d59d7f6SMichal Simek 			val = fdt64_to_cpu(val);
878d59d7f6SMichal Simek 			debug("%s: addr64=%llx, ptr=%p, cell=%p\n",
888d59d7f6SMichal Simek 			      __func__, val, p, &cell[i]);
898d59d7f6SMichal Simek 			*(phys_addr_t *)p = val;
908d59d7f6SMichal Simek 		} else {
918d59d7f6SMichal Simek 			debug("%s: addr32=%x, ptr=%p\n",
928d59d7f6SMichal Simek 			      __func__, fdt32_to_cpu(cell[i]), p);
938d59d7f6SMichal Simek 			*(phys_addr_t *)p = fdt32_to_cpu(cell[i]);
948d59d7f6SMichal Simek 		}
958d59d7f6SMichal Simek 		p += sizeof(phys_addr_t);
968d59d7f6SMichal Simek 		i += address_cells;
978d59d7f6SMichal Simek 
988d59d7f6SMichal Simek 		debug("%s: pa=%p, i=%x, size=%zu\n", __func__, p, i,
998d59d7f6SMichal Simek 		      sizeof(phys_addr_t));
1008d59d7f6SMichal Simek 
1018d59d7f6SMichal Simek 		if (size_cells == 2) {
1028d59d7f6SMichal Simek 			vals = cell[i + 1];
1038d59d7f6SMichal Simek 			vals <<= 32;
1048d59d7f6SMichal Simek 			vals |= cell[i];
1058d59d7f6SMichal Simek 			vals = fdt64_to_cpu(vals);
1068d59d7f6SMichal Simek 
1078d59d7f6SMichal Simek 			debug("%s: size64=%llx, ptr=%p, cell=%p\n",
1088d59d7f6SMichal Simek 			      __func__, vals, p, &cell[i]);
1098d59d7f6SMichal Simek 			*(phys_size_t *)p = vals;
1108d59d7f6SMichal Simek 		} else {
1118d59d7f6SMichal Simek 			debug("%s: size32=%x, ptr=%p\n",
1128d59d7f6SMichal Simek 			      __func__, fdt32_to_cpu(cell[i]), p);
1138d59d7f6SMichal Simek 			*(phys_size_t *)p = fdt32_to_cpu(cell[i]);
1148d59d7f6SMichal Simek 		}
1158d59d7f6SMichal Simek 		p += sizeof(phys_size_t);
1168d59d7f6SMichal Simek 		i += size_cells;
1178d59d7f6SMichal Simek 
1188d59d7f6SMichal Simek 		debug("%s: ps=%p, i=%x, size=%zu\n",
1198d59d7f6SMichal Simek 		      __func__, p, i, sizeof(phys_size_t));
1208d59d7f6SMichal Simek 	}
1218d59d7f6SMichal Simek 
1228d59d7f6SMichal Simek 	/* Return the first address size */
1238d59d7f6SMichal Simek 	return *(phys_size_t *)((char *)buf + sizeof(phys_addr_t));
1248d59d7f6SMichal Simek }
1258d59d7f6SMichal Simek 
1268d59d7f6SMichal Simek #define FDT_REG_SIZE  sizeof(u32)
1278d59d7f6SMichal Simek /* Temp location for sharing data for storing */
1288d59d7f6SMichal Simek /* Up to 64-bit address + 64-bit size */
1298d59d7f6SMichal Simek static u8 tmp[CONFIG_NR_DRAM_BANKS * 16];
1308d59d7f6SMichal Simek 
1318d59d7f6SMichal Simek void dram_init_banksize(void)
1328d59d7f6SMichal Simek {
1338d59d7f6SMichal Simek 	int bank;
1348d59d7f6SMichal Simek 
1358d59d7f6SMichal Simek 	memcpy(&gd->bd->bi_dram[0], &tmp, sizeof(tmp));
1368d59d7f6SMichal Simek 
1378d59d7f6SMichal Simek 	for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
1388d59d7f6SMichal Simek 		debug("Bank #%d: start %llx\n", bank,
1398d59d7f6SMichal Simek 		      (unsigned long long)gd->bd->bi_dram[bank].start);
1408d59d7f6SMichal Simek 		debug("Bank #%d: size %llx\n", bank,
1418d59d7f6SMichal Simek 		      (unsigned long long)gd->bd->bi_dram[bank].size);
1428d59d7f6SMichal Simek 	}
1438d59d7f6SMichal Simek }
1448d59d7f6SMichal Simek 
1458d59d7f6SMichal Simek int dram_init(void)
1468d59d7f6SMichal Simek {
1478d59d7f6SMichal Simek 	int node, len;
1488d59d7f6SMichal Simek 	const void *blob = gd->fdt_blob;
1498d59d7f6SMichal Simek 	const u32 *cell;
1508d59d7f6SMichal Simek 
1518d59d7f6SMichal Simek 	memset(&tmp, 0, sizeof(tmp));
1528d59d7f6SMichal Simek 
1538d59d7f6SMichal Simek 	/* find or create "/memory" node. */
1548d59d7f6SMichal Simek 	node = fdt_subnode_offset(blob, 0, "memory");
1558d59d7f6SMichal Simek 	if (node < 0) {
1568d59d7f6SMichal Simek 		printf("%s: Can't get memory node\n", __func__);
1578d59d7f6SMichal Simek 		return node;
1588d59d7f6SMichal Simek 	}
1598d59d7f6SMichal Simek 
1608d59d7f6SMichal Simek 	/* Get pointer to cells and lenght of it */
1618d59d7f6SMichal Simek 	cell = fdt_getprop(blob, node, "reg", &len);
1628d59d7f6SMichal Simek 	if (!cell) {
1638d59d7f6SMichal Simek 		printf("%s: Can't get reg property\n", __func__);
1648d59d7f6SMichal Simek 		return -1;
1658d59d7f6SMichal Simek 	}
1668d59d7f6SMichal Simek 
1678d59d7f6SMichal Simek 	gd->ram_size = fdt_get_reg(blob, node, &tmp, cell, len / FDT_REG_SIZE);
1688d59d7f6SMichal Simek 
169*658b3a56SMichal Simek 	debug("%s: Initial DRAM size %llx\n", __func__, (u64)gd->ram_size);
1708d59d7f6SMichal Simek 
1718d59d7f6SMichal Simek 	return 0;
1728d59d7f6SMichal Simek }
1738d59d7f6SMichal Simek #else
17484c7204bSMichal Simek int dram_init(void)
17584c7204bSMichal Simek {
17684c7204bSMichal Simek 	gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
17784c7204bSMichal Simek 
17884c7204bSMichal Simek 	return 0;
17984c7204bSMichal Simek }
1808d59d7f6SMichal Simek #endif
18184c7204bSMichal Simek 
18284c7204bSMichal Simek void reset_cpu(ulong addr)
18384c7204bSMichal Simek {
18484c7204bSMichal Simek }
18584c7204bSMichal Simek 
1866fe6f135SMichal Simek #ifdef CONFIG_SCSI_AHCI_PLAT
1876fe6f135SMichal Simek void scsi_init(void)
1886fe6f135SMichal Simek {
189679b994aSMichal Simek #if defined(CONFIG_SATA_CEVA)
190679b994aSMichal Simek 	init_sata(0);
191679b994aSMichal Simek #endif
1926fe6f135SMichal Simek 	ahci_init((void __iomem *)ZYNQMP_SATA_BASEADDR);
1936fe6f135SMichal Simek 	scsi_scan(1);
1946fe6f135SMichal Simek }
1956fe6f135SMichal Simek #endif
1966fe6f135SMichal Simek 
19784c7204bSMichal Simek int board_late_init(void)
19884c7204bSMichal Simek {
19984c7204bSMichal Simek 	u32 reg = 0;
20084c7204bSMichal Simek 	u8 bootmode;
20184c7204bSMichal Simek 
20284c7204bSMichal Simek 	reg = readl(&crlapb_base->boot_mode);
20384c7204bSMichal Simek 	bootmode = reg & BOOT_MODES_MASK;
20484c7204bSMichal Simek 
205fb90917cSMichal Simek 	puts("Bootmode: ");
20684c7204bSMichal Simek 	switch (bootmode) {
2070a5bcc8cSSiva Durga Prasad Paladugu 	case JTAG_MODE:
208fb90917cSMichal Simek 		puts("JTAG_MODE\n");
209fb90917cSMichal Simek 		setenv("modeboot", "jtagboot");
2100a5bcc8cSSiva Durga Prasad Paladugu 		break;
2110a5bcc8cSSiva Durga Prasad Paladugu 	case QSPI_MODE_24BIT:
2120a5bcc8cSSiva Durga Prasad Paladugu 	case QSPI_MODE_32BIT:
2130a5bcc8cSSiva Durga Prasad Paladugu 		setenv("modeboot", "qspiboot");
214fb90917cSMichal Simek 		puts("QSPI_MODE\n");
2150a5bcc8cSSiva Durga Prasad Paladugu 		break;
21639c56f55SMichal Simek 	case EMMC_MODE:
21778678feeSMichal Simek 		puts("EMMC_MODE\n");
21878678feeSMichal Simek 		setenv("modeboot", "sdboot");
21978678feeSMichal Simek 		break;
22078678feeSMichal Simek 	case SD_MODE:
221fb90917cSMichal Simek 		puts("SD_MODE\n");
22284c7204bSMichal Simek 		setenv("modeboot", "sdboot");
22384c7204bSMichal Simek 		break;
224af813acdSMichal Simek 	case SD_MODE1:
225fb90917cSMichal Simek 		puts("SD_MODE1\n");
2262d9925bcSMichal Simek #if defined(CONFIG_ZYNQ_SDHCI0) && defined(CONFIG_ZYNQ_SDHCI1)
2272d9925bcSMichal Simek 		setenv("sdbootdev", "1");
2282d9925bcSMichal Simek #endif
2292d9925bcSMichal Simek 		setenv("modeboot", "sdboot");
230af813acdSMichal Simek 		break;
231af813acdSMichal Simek 	case NAND_MODE:
232fb90917cSMichal Simek 		puts("NAND_MODE\n");
233af813acdSMichal Simek 		setenv("modeboot", "nandboot");
234af813acdSMichal Simek 		break;
23584c7204bSMichal Simek 	default:
23684c7204bSMichal Simek 		printf("Invalid Boot Mode:0x%x\n", bootmode);
23784c7204bSMichal Simek 		break;
23884c7204bSMichal Simek 	}
23984c7204bSMichal Simek 
24084c7204bSMichal Simek 	return 0;
24184c7204bSMichal Simek }
24284696ff5SSiva Durga Prasad Paladugu 
24384696ff5SSiva Durga Prasad Paladugu int checkboard(void)
24484696ff5SSiva Durga Prasad Paladugu {
2455af08556SMichal Simek 	puts("Board: Xilinx ZynqMP\n");
24684696ff5SSiva Durga Prasad Paladugu 	return 0;
24784696ff5SSiva Durga Prasad Paladugu }
24816fa00a7SSiva Durga Prasad Paladugu 
24916fa00a7SSiva Durga Prasad Paladugu #ifdef CONFIG_USB_DWC3
25016fa00a7SSiva Durga Prasad Paladugu static struct dwc3_device dwc3_device_data = {
25116fa00a7SSiva Durga Prasad Paladugu 	.maximum_speed = USB_SPEED_HIGH,
25216fa00a7SSiva Durga Prasad Paladugu 	.base = ZYNQMP_USB0_XHCI_BASEADDR,
25316fa00a7SSiva Durga Prasad Paladugu 	.dr_mode = USB_DR_MODE_PERIPHERAL,
25416fa00a7SSiva Durga Prasad Paladugu 	.index = 0,
25516fa00a7SSiva Durga Prasad Paladugu };
25616fa00a7SSiva Durga Prasad Paladugu 
25716fa00a7SSiva Durga Prasad Paladugu int usb_gadget_handle_interrupts(void)
25816fa00a7SSiva Durga Prasad Paladugu {
25916fa00a7SSiva Durga Prasad Paladugu 	dwc3_uboot_handle_interrupt(0);
26016fa00a7SSiva Durga Prasad Paladugu 	return 0;
26116fa00a7SSiva Durga Prasad Paladugu }
26216fa00a7SSiva Durga Prasad Paladugu 
26316fa00a7SSiva Durga Prasad Paladugu int board_usb_init(int index, enum usb_init_type init)
26416fa00a7SSiva Durga Prasad Paladugu {
26516fa00a7SSiva Durga Prasad Paladugu 	return dwc3_uboot_init(&dwc3_device_data);
26616fa00a7SSiva Durga Prasad Paladugu }
26716fa00a7SSiva Durga Prasad Paladugu 
26816fa00a7SSiva Durga Prasad Paladugu int board_usb_cleanup(int index, enum usb_init_type init)
26916fa00a7SSiva Durga Prasad Paladugu {
27016fa00a7SSiva Durga Prasad Paladugu 	dwc3_uboot_exit(index);
27116fa00a7SSiva Durga Prasad Paladugu 	return 0;
27216fa00a7SSiva Durga Prasad Paladugu }
27316fa00a7SSiva Durga Prasad Paladugu #endif
274