xref: /openbmc/u-boot/board/xilinx/zynqmp/zynqmp.c (revision 12308b128fa28d50c1586f60f3117f5213356756)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
284c7204bSMichal Simek /*
384c7204bSMichal Simek  * (C) Copyright 2014 - 2015 Xilinx, Inc.
484c7204bSMichal Simek  * Michal Simek <michal.simek@xilinx.com>
584c7204bSMichal Simek  */
684c7204bSMichal Simek 
784c7204bSMichal Simek #include <common.h>
8679b994aSMichal Simek #include <sata.h>
96fe6f135SMichal Simek #include <ahci.h>
106fe6f135SMichal Simek #include <scsi.h>
11b72894f1SMichal Simek #include <malloc.h>
124490e013SMichal Simek #include <wdt.h>
130785dfd8SMichal Simek #include <asm/arch/clk.h>
1484c7204bSMichal Simek #include <asm/arch/hardware.h>
1584c7204bSMichal Simek #include <asm/arch/sys_proto.h>
162ad341edSMichal Simek #include <asm/arch/psu_init_gpl.h>
1784c7204bSMichal Simek #include <asm/io.h>
182882b39dSMichal Simek #include <dm/device.h>
194490e013SMichal Simek #include <dm/uclass.h>
2016fa00a7SSiva Durga Prasad Paladugu #include <usb.h>
2116fa00a7SSiva Durga Prasad Paladugu #include <dwc3-uboot.h>
2247e60cbdSMichal Simek #include <zynqmppl.h>
236919b4bfSMichal Simek #include <i2c.h>
249feff385SMichal Simek #include <g_dnl.h>
2584c7204bSMichal Simek 
2684c7204bSMichal Simek DECLARE_GLOBAL_DATA_PTR;
2784c7204bSMichal Simek 
284490e013SMichal Simek #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_WDT)
294490e013SMichal Simek static struct udevice *watchdog_dev;
304490e013SMichal Simek #endif
314490e013SMichal Simek 
3247e60cbdSMichal Simek #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
3347e60cbdSMichal Simek     !defined(CONFIG_SPL_BUILD)
3447e60cbdSMichal Simek static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
3547e60cbdSMichal Simek 
3647e60cbdSMichal Simek static const struct {
378ebdf9efSMichal Simek 	u32 id;
38494fffe7SMichal Simek 	u32 ver;
3947e60cbdSMichal Simek 	char *name;
4083bf2ff0SSiva Durga Prasad Paladugu 	bool evexists;
4147e60cbdSMichal Simek } zynqmp_devices[] = {
4247e60cbdSMichal Simek 	{
4347e60cbdSMichal Simek 		.id = 0x10,
4447e60cbdSMichal Simek 		.name = "3eg",
4547e60cbdSMichal Simek 	},
4647e60cbdSMichal Simek 	{
47494fffe7SMichal Simek 		.id = 0x10,
48494fffe7SMichal Simek 		.ver = 0x2c,
49494fffe7SMichal Simek 		.name = "3cg",
50494fffe7SMichal Simek 	},
51494fffe7SMichal Simek 	{
5247e60cbdSMichal Simek 		.id = 0x11,
5347e60cbdSMichal Simek 		.name = "2eg",
5447e60cbdSMichal Simek 	},
5547e60cbdSMichal Simek 	{
56494fffe7SMichal Simek 		.id = 0x11,
57494fffe7SMichal Simek 		.ver = 0x2c,
58494fffe7SMichal Simek 		.name = "2cg",
59494fffe7SMichal Simek 	},
60494fffe7SMichal Simek 	{
6147e60cbdSMichal Simek 		.id = 0x20,
6247e60cbdSMichal Simek 		.name = "5ev",
6383bf2ff0SSiva Durga Prasad Paladugu 		.evexists = 1,
6447e60cbdSMichal Simek 	},
6547e60cbdSMichal Simek 	{
66494fffe7SMichal Simek 		.id = 0x20,
67494fffe7SMichal Simek 		.ver = 0x100,
68494fffe7SMichal Simek 		.name = "5eg",
6983bf2ff0SSiva Durga Prasad Paladugu 		.evexists = 1,
70494fffe7SMichal Simek 	},
71494fffe7SMichal Simek 	{
72494fffe7SMichal Simek 		.id = 0x20,
73494fffe7SMichal Simek 		.ver = 0x12c,
74494fffe7SMichal Simek 		.name = "5cg",
75494fffe7SMichal Simek 	},
76494fffe7SMichal Simek 	{
7747e60cbdSMichal Simek 		.id = 0x21,
7847e60cbdSMichal Simek 		.name = "4ev",
7983bf2ff0SSiva Durga Prasad Paladugu 		.evexists = 1,
8047e60cbdSMichal Simek 	},
8147e60cbdSMichal Simek 	{
82494fffe7SMichal Simek 		.id = 0x21,
83494fffe7SMichal Simek 		.ver = 0x100,
84494fffe7SMichal Simek 		.name = "4eg",
8583bf2ff0SSiva Durga Prasad Paladugu 		.evexists = 1,
86494fffe7SMichal Simek 	},
87494fffe7SMichal Simek 	{
88494fffe7SMichal Simek 		.id = 0x21,
89494fffe7SMichal Simek 		.ver = 0x12c,
90494fffe7SMichal Simek 		.name = "4cg",
91494fffe7SMichal Simek 	},
92494fffe7SMichal Simek 	{
9347e60cbdSMichal Simek 		.id = 0x30,
9447e60cbdSMichal Simek 		.name = "7ev",
9583bf2ff0SSiva Durga Prasad Paladugu 		.evexists = 1,
9647e60cbdSMichal Simek 	},
9747e60cbdSMichal Simek 	{
98494fffe7SMichal Simek 		.id = 0x30,
99494fffe7SMichal Simek 		.ver = 0x100,
100494fffe7SMichal Simek 		.name = "7eg",
10183bf2ff0SSiva Durga Prasad Paladugu 		.evexists = 1,
102494fffe7SMichal Simek 	},
103494fffe7SMichal Simek 	{
104494fffe7SMichal Simek 		.id = 0x30,
105494fffe7SMichal Simek 		.ver = 0x12c,
106494fffe7SMichal Simek 		.name = "7cg",
107494fffe7SMichal Simek 	},
108494fffe7SMichal Simek 	{
10947e60cbdSMichal Simek 		.id = 0x38,
11047e60cbdSMichal Simek 		.name = "9eg",
11147e60cbdSMichal Simek 	},
11247e60cbdSMichal Simek 	{
113494fffe7SMichal Simek 		.id = 0x38,
114494fffe7SMichal Simek 		.ver = 0x2c,
115494fffe7SMichal Simek 		.name = "9cg",
116494fffe7SMichal Simek 	},
117494fffe7SMichal Simek 	{
11847e60cbdSMichal Simek 		.id = 0x39,
11947e60cbdSMichal Simek 		.name = "6eg",
12047e60cbdSMichal Simek 	},
12147e60cbdSMichal Simek 	{
122494fffe7SMichal Simek 		.id = 0x39,
123494fffe7SMichal Simek 		.ver = 0x2c,
124494fffe7SMichal Simek 		.name = "6cg",
125494fffe7SMichal Simek 	},
126494fffe7SMichal Simek 	{
12747e60cbdSMichal Simek 		.id = 0x40,
12847e60cbdSMichal Simek 		.name = "11eg",
12947e60cbdSMichal Simek 	},
130494fffe7SMichal Simek 	{ /* For testing purpose only */
131494fffe7SMichal Simek 		.id = 0x50,
132494fffe7SMichal Simek 		.ver = 0x2c,
133494fffe7SMichal Simek 		.name = "15cg",
134494fffe7SMichal Simek 	},
13547e60cbdSMichal Simek 	{
13647e60cbdSMichal Simek 		.id = 0x50,
13747e60cbdSMichal Simek 		.name = "15eg",
13847e60cbdSMichal Simek 	},
13947e60cbdSMichal Simek 	{
14047e60cbdSMichal Simek 		.id = 0x58,
14147e60cbdSMichal Simek 		.name = "19eg",
14247e60cbdSMichal Simek 	},
14347e60cbdSMichal Simek 	{
14447e60cbdSMichal Simek 		.id = 0x59,
14547e60cbdSMichal Simek 		.name = "17eg",
14647e60cbdSMichal Simek 	},
147b030fedfSMichal Simek 	{
148b030fedfSMichal Simek 		.id = 0x61,
149b030fedfSMichal Simek 		.name = "21dr",
150b030fedfSMichal Simek 	},
151b030fedfSMichal Simek 	{
152b030fedfSMichal Simek 		.id = 0x63,
153b030fedfSMichal Simek 		.name = "23dr",
154b030fedfSMichal Simek 	},
155b030fedfSMichal Simek 	{
156b030fedfSMichal Simek 		.id = 0x65,
157b030fedfSMichal Simek 		.name = "25dr",
158b030fedfSMichal Simek 	},
159b030fedfSMichal Simek 	{
160b030fedfSMichal Simek 		.id = 0x64,
161b030fedfSMichal Simek 		.name = "27dr",
162b030fedfSMichal Simek 	},
163b030fedfSMichal Simek 	{
164b030fedfSMichal Simek 		.id = 0x60,
165b030fedfSMichal Simek 		.name = "28dr",
166b030fedfSMichal Simek 	},
167b030fedfSMichal Simek 	{
168b030fedfSMichal Simek 		.id = 0x62,
169b030fedfSMichal Simek 		.name = "29dr",
170b030fedfSMichal Simek 	},
17147e60cbdSMichal Simek };
17274ba69dbSSiva Durga Prasad Paladugu #endif
17347e60cbdSMichal Simek 
174f52bf5a3SSiva Durga Prasad Paladugu int chip_id(unsigned char id)
17547e60cbdSMichal Simek {
17647e60cbdSMichal Simek 	struct pt_regs regs;
17774ba69dbSSiva Durga Prasad Paladugu 	int val = -EINVAL;
17874ba69dbSSiva Durga Prasad Paladugu 
17974ba69dbSSiva Durga Prasad Paladugu 	if (current_el() != 3) {
18047e60cbdSMichal Simek 		regs.regs[0] = ZYNQMP_SIP_SVC_CSU_DMA_CHIPID;
18147e60cbdSMichal Simek 		regs.regs[1] = 0;
18247e60cbdSMichal Simek 		regs.regs[2] = 0;
18347e60cbdSMichal Simek 		regs.regs[3] = 0;
18447e60cbdSMichal Simek 
18547e60cbdSMichal Simek 		smc_call(&regs);
18647e60cbdSMichal Simek 
1870cba6abbSSoren Brinkmann 		/*
1880cba6abbSSoren Brinkmann 		 * SMC returns:
1890cba6abbSSoren Brinkmann 		 * regs[0][31:0]  = status of the operation
1900cba6abbSSoren Brinkmann 		 * regs[0][63:32] = CSU.IDCODE register
1910cba6abbSSoren Brinkmann 		 * regs[1][31:0]  = CSU.version register
192494fffe7SMichal Simek 		 * regs[1][63:32] = CSU.IDCODE2 register
1930cba6abbSSoren Brinkmann 		 */
194db3123b4SSiva Durga Prasad Paladugu 		switch (id) {
195db3123b4SSiva Durga Prasad Paladugu 		case IDCODE:
1960cba6abbSSoren Brinkmann 			regs.regs[0] = upper_32_bits(regs.regs[0]);
1970cba6abbSSoren Brinkmann 			regs.regs[0] &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
1980cba6abbSSoren Brinkmann 					ZYNQMP_CSU_IDCODE_SVD_MASK;
1990cba6abbSSoren Brinkmann 			regs.regs[0] >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
200db3123b4SSiva Durga Prasad Paladugu 			val = regs.regs[0];
201db3123b4SSiva Durga Prasad Paladugu 			break;
202db3123b4SSiva Durga Prasad Paladugu 		case VERSION:
203db3123b4SSiva Durga Prasad Paladugu 			regs.regs[1] = lower_32_bits(regs.regs[1]);
204db3123b4SSiva Durga Prasad Paladugu 			regs.regs[1] &= ZYNQMP_CSU_SILICON_VER_MASK;
205db3123b4SSiva Durga Prasad Paladugu 			val = regs.regs[1];
206db3123b4SSiva Durga Prasad Paladugu 			break;
207494fffe7SMichal Simek 		case IDCODE2:
208494fffe7SMichal Simek 			regs.regs[1] = lower_32_bits(regs.regs[1]);
209494fffe7SMichal Simek 			regs.regs[1] >>= ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
210494fffe7SMichal Simek 			val = regs.regs[1];
211494fffe7SMichal Simek 			break;
212db3123b4SSiva Durga Prasad Paladugu 		default:
213db3123b4SSiva Durga Prasad Paladugu 			printf("%s, Invalid Req:0x%x\n", __func__, id);
214db3123b4SSiva Durga Prasad Paladugu 		}
21574ba69dbSSiva Durga Prasad Paladugu 	} else {
21674ba69dbSSiva Durga Prasad Paladugu 		switch (id) {
21774ba69dbSSiva Durga Prasad Paladugu 		case IDCODE:
21874ba69dbSSiva Durga Prasad Paladugu 			val = readl(ZYNQMP_CSU_IDCODE_ADDR);
21974ba69dbSSiva Durga Prasad Paladugu 			val &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
22074ba69dbSSiva Durga Prasad Paladugu 			       ZYNQMP_CSU_IDCODE_SVD_MASK;
22174ba69dbSSiva Durga Prasad Paladugu 			val >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
22274ba69dbSSiva Durga Prasad Paladugu 			break;
22374ba69dbSSiva Durga Prasad Paladugu 		case VERSION:
22474ba69dbSSiva Durga Prasad Paladugu 			val = readl(ZYNQMP_CSU_VER_ADDR);
22574ba69dbSSiva Durga Prasad Paladugu 			val &= ZYNQMP_CSU_SILICON_VER_MASK;
22674ba69dbSSiva Durga Prasad Paladugu 			break;
22774ba69dbSSiva Durga Prasad Paladugu 		default:
22874ba69dbSSiva Durga Prasad Paladugu 			printf("%s, Invalid Req:0x%x\n", __func__, id);
22974ba69dbSSiva Durga Prasad Paladugu 		}
23074ba69dbSSiva Durga Prasad Paladugu 	}
2310cba6abbSSoren Brinkmann 
232db3123b4SSiva Durga Prasad Paladugu 	return val;
23347e60cbdSMichal Simek }
23447e60cbdSMichal Simek 
23583bf2ff0SSiva Durga Prasad Paladugu #define ZYNQMP_VERSION_SIZE		9
23683bf2ff0SSiva Durga Prasad Paladugu #define ZYNQMP_PL_STATUS_BIT		9
23783bf2ff0SSiva Durga Prasad Paladugu #define ZYNQMP_PL_STATUS_MASK		BIT(ZYNQMP_PL_STATUS_BIT)
23883bf2ff0SSiva Durga Prasad Paladugu #define ZYNQMP_CSU_VERSION_MASK		~(ZYNQMP_PL_STATUS_MASK)
23983bf2ff0SSiva Durga Prasad Paladugu 
24074ba69dbSSiva Durga Prasad Paladugu #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
24174ba69dbSSiva Durga Prasad Paladugu 	!defined(CONFIG_SPL_BUILD)
24247e60cbdSMichal Simek static char *zynqmp_get_silicon_idcode_name(void)
24347e60cbdSMichal Simek {
244494fffe7SMichal Simek 	u32 i, id, ver;
24583bf2ff0SSiva Durga Prasad Paladugu 	char *buf;
24683bf2ff0SSiva Durga Prasad Paladugu 	static char name[ZYNQMP_VERSION_SIZE];
24747e60cbdSMichal Simek 
248db3123b4SSiva Durga Prasad Paladugu 	id = chip_id(IDCODE);
249494fffe7SMichal Simek 	ver = chip_id(IDCODE2);
250494fffe7SMichal Simek 
25147e60cbdSMichal Simek 	for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
25283bf2ff0SSiva Durga Prasad Paladugu 		if ((zynqmp_devices[i].id == id) &&
25383bf2ff0SSiva Durga Prasad Paladugu 		    (zynqmp_devices[i].ver == (ver &
25483bf2ff0SSiva Durga Prasad Paladugu 		    ZYNQMP_CSU_VERSION_MASK))) {
25583bf2ff0SSiva Durga Prasad Paladugu 			strncat(name, "zu", 2);
25683bf2ff0SSiva Durga Prasad Paladugu 			strncat(name, zynqmp_devices[i].name,
25783bf2ff0SSiva Durga Prasad Paladugu 				ZYNQMP_VERSION_SIZE - 3);
25883bf2ff0SSiva Durga Prasad Paladugu 			break;
25947e60cbdSMichal Simek 		}
26083bf2ff0SSiva Durga Prasad Paladugu 	}
26183bf2ff0SSiva Durga Prasad Paladugu 
26283bf2ff0SSiva Durga Prasad Paladugu 	if (i >= ARRAY_SIZE(zynqmp_devices))
26347e60cbdSMichal Simek 		return "unknown";
26483bf2ff0SSiva Durga Prasad Paladugu 
26583bf2ff0SSiva Durga Prasad Paladugu 	if (!zynqmp_devices[i].evexists)
26683bf2ff0SSiva Durga Prasad Paladugu 		return name;
26783bf2ff0SSiva Durga Prasad Paladugu 
26883bf2ff0SSiva Durga Prasad Paladugu 	if (ver & ZYNQMP_PL_STATUS_MASK)
26983bf2ff0SSiva Durga Prasad Paladugu 		return name;
27083bf2ff0SSiva Durga Prasad Paladugu 
27183bf2ff0SSiva Durga Prasad Paladugu 	if (strstr(name, "eg") || strstr(name, "ev")) {
27283bf2ff0SSiva Durga Prasad Paladugu 		buf = strstr(name, "e");
27383bf2ff0SSiva Durga Prasad Paladugu 		*buf = '\0';
27483bf2ff0SSiva Durga Prasad Paladugu 	}
27583bf2ff0SSiva Durga Prasad Paladugu 
27683bf2ff0SSiva Durga Prasad Paladugu 	return name;
27747e60cbdSMichal Simek }
27847e60cbdSMichal Simek #endif
27947e60cbdSMichal Simek 
280fb4000e8SMichal Simek int board_early_init_f(void)
281fb4000e8SMichal Simek {
282f32e79f1SMichal Simek 	int ret = 0;
283fb4000e8SMichal Simek #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CLK_ZYNQMP)
284fb4000e8SMichal Simek 	zynqmp_pmufw_version();
285fb4000e8SMichal Simek #endif
28655de0929SMichal Simek 
28788f05a92SMichal Simek #if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
288f32e79f1SMichal Simek 	ret = psu_init();
28955de0929SMichal Simek #endif
29055de0929SMichal Simek 
2914490e013SMichal Simek #if defined(CONFIG_WDT) && !defined(CONFIG_SPL_BUILD)
2924490e013SMichal Simek 	/* bss is not cleared at time when watchdog_reset() is called */
2934490e013SMichal Simek 	watchdog_dev = NULL;
2944490e013SMichal Simek #endif
2954490e013SMichal Simek 
296f32e79f1SMichal Simek 	return ret;
297fb4000e8SMichal Simek }
298fb4000e8SMichal Simek 
29984c7204bSMichal Simek int board_init(void)
30084c7204bSMichal Simek {
301a0736efbSMichal Simek 	printf("EL Level:\tEL%d\n", current_el());
302a0736efbSMichal Simek 
30347e60cbdSMichal Simek #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
30447e60cbdSMichal Simek     !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
30547e60cbdSMichal Simek     defined(CONFIG_SPL_BUILD))
30647e60cbdSMichal Simek 	if (current_el() != 3) {
30783bf2ff0SSiva Durga Prasad Paladugu 		zynqmppl.name = zynqmp_get_silicon_idcode_name();
30847e60cbdSMichal Simek 		printf("Chip ID:\t%s\n", zynqmppl.name);
30947e60cbdSMichal Simek 		fpga_init();
31047e60cbdSMichal Simek 		fpga_add(fpga_xilinx, &zynqmppl);
31147e60cbdSMichal Simek 	}
31247e60cbdSMichal Simek #endif
31347e60cbdSMichal Simek 
3144490e013SMichal Simek #if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_WDT)
3151fbca0dbSMichal Simek 	if (uclass_get_device_by_seq(UCLASS_WDT, 0, &watchdog_dev)) {
3161fbca0dbSMichal Simek 		debug("Watchdog: Not found by seq!\n");
3174490e013SMichal Simek 		if (uclass_get_device(UCLASS_WDT, 0, &watchdog_dev)) {
3184490e013SMichal Simek 			puts("Watchdog: Not found!\n");
3191fbca0dbSMichal Simek 			return 0;
3201fbca0dbSMichal Simek 		}
3211fbca0dbSMichal Simek 	}
3221fbca0dbSMichal Simek 
3234490e013SMichal Simek 	wdt_start(watchdog_dev, 0, 0);
3244490e013SMichal Simek 	puts("Watchdog: Started\n");
3254490e013SMichal Simek #endif
3264490e013SMichal Simek 
32784c7204bSMichal Simek 	return 0;
32884c7204bSMichal Simek }
32984c7204bSMichal Simek 
3304490e013SMichal Simek #ifdef CONFIG_WATCHDOG
3314490e013SMichal Simek /* Called by macro WATCHDOG_RESET */
3324490e013SMichal Simek void watchdog_reset(void)
3334490e013SMichal Simek {
3344490e013SMichal Simek # if !defined(CONFIG_SPL_BUILD)
3354490e013SMichal Simek 	static ulong next_reset;
3364490e013SMichal Simek 	ulong now;
3374490e013SMichal Simek 
3384490e013SMichal Simek 	if (!watchdog_dev)
3394490e013SMichal Simek 		return;
3404490e013SMichal Simek 
3414490e013SMichal Simek 	now = timer_get_us();
3424490e013SMichal Simek 
3434490e013SMichal Simek 	/* Do not reset the watchdog too often */
3444490e013SMichal Simek 	if (now > next_reset) {
3454490e013SMichal Simek 		wdt_reset(watchdog_dev);
3464490e013SMichal Simek 		next_reset = now + 1000;
3474490e013SMichal Simek 	}
3484490e013SMichal Simek # endif
3494490e013SMichal Simek }
3504490e013SMichal Simek #endif
3514490e013SMichal Simek 
35284c7204bSMichal Simek int board_early_init_r(void)
35384c7204bSMichal Simek {
35484c7204bSMichal Simek 	u32 val;
35584c7204bSMichal Simek 
356ec60a279SSiva Durga Prasad Paladugu 	if (current_el() != 3)
357ec60a279SSiva Durga Prasad Paladugu 		return 0;
358ec60a279SSiva Durga Prasad Paladugu 
35990a35db4SMichal Simek 	val = readl(&crlapb_base->timestamp_ref_ctrl);
36090a35db4SMichal Simek 	val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
36190a35db4SMichal Simek 
362ec60a279SSiva Durga Prasad Paladugu 	if (!val) {
36384c7204bSMichal Simek 		val = readl(&crlapb_base->timestamp_ref_ctrl);
36484c7204bSMichal Simek 		val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
36584c7204bSMichal Simek 		writel(val, &crlapb_base->timestamp_ref_ctrl);
36684c7204bSMichal Simek 
3670785dfd8SMichal Simek 		/* Program freq register in System counter */
3680785dfd8SMichal Simek 		writel(zynqmp_get_system_timer_freq(),
3690785dfd8SMichal Simek 		       &iou_scntr_secure->base_frequency_id_register);
3700785dfd8SMichal Simek 		/* And enable system counter */
3710785dfd8SMichal Simek 		writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
3720785dfd8SMichal Simek 		       &iou_scntr_secure->counter_control_register);
3730785dfd8SMichal Simek 	}
37484c7204bSMichal Simek 	return 0;
37584c7204bSMichal Simek }
37684c7204bSMichal Simek 
3776919b4bfSMichal Simek int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
3786919b4bfSMichal Simek {
3796919b4bfSMichal Simek #if defined(CONFIG_ZYNQ_GEM_EEPROM_ADDR) && \
3806919b4bfSMichal Simek     defined(CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET) && \
3816919b4bfSMichal Simek     defined(CONFIG_ZYNQ_EEPROM_BUS)
3826919b4bfSMichal Simek 	i2c_set_bus_num(CONFIG_ZYNQ_EEPROM_BUS);
3836919b4bfSMichal Simek 
3846919b4bfSMichal Simek 	if (eeprom_read(CONFIG_ZYNQ_GEM_EEPROM_ADDR,
3856919b4bfSMichal Simek 			CONFIG_ZYNQ_GEM_I2C_MAC_OFFSET,
3866919b4bfSMichal Simek 			ethaddr, 6))
3876919b4bfSMichal Simek 		printf("I2C EEPROM MAC address read failed\n");
3886919b4bfSMichal Simek #endif
3896919b4bfSMichal Simek 
3906919b4bfSMichal Simek 	return 0;
3916919b4bfSMichal Simek }
3926919b4bfSMichal Simek 
39351916864SNitin Jain unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
39451916864SNitin Jain 			 char * const argv[])
39551916864SNitin Jain {
39651916864SNitin Jain 	int ret = 0;
39751916864SNitin Jain 
39851916864SNitin Jain 	if (current_el() > 1) {
39951916864SNitin Jain 		smp_kick_all_cpus();
40051916864SNitin Jain 		dcache_disable();
40151916864SNitin Jain 		armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
40251916864SNitin Jain 				    ES_TO_AARCH64);
40351916864SNitin Jain 	} else {
40451916864SNitin Jain 		printf("FAIL: current EL is not above EL1\n");
40551916864SNitin Jain 		ret = EINVAL;
40651916864SNitin Jain 	}
40751916864SNitin Jain 	return ret;
40851916864SNitin Jain }
40951916864SNitin Jain 
4108d59d7f6SMichal Simek #if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
41176b00acaSSimon Glass int dram_init_banksize(void)
412361a8799STom Rini {
4130678941aSNitin Jain 	int ret;
4140678941aSNitin Jain 
4150678941aSNitin Jain 	ret = fdtdec_setup_memory_banksize();
4160678941aSNitin Jain 	if (ret)
4170678941aSNitin Jain 		return ret;
4180678941aSNitin Jain 
4190678941aSNitin Jain 	mem_map_fill();
4200678941aSNitin Jain 
4210678941aSNitin Jain 	return 0;
4228d59d7f6SMichal Simek }
4238d59d7f6SMichal Simek 
4248d59d7f6SMichal Simek int dram_init(void)
4258d59d7f6SMichal Simek {
426*12308b12SSiva Durga Prasad Paladugu 	if (fdtdec_setup_mem_size_base() != 0)
427950f86caSNathan Rossi 		return -EINVAL;
4288d59d7f6SMichal Simek 
4298d59d7f6SMichal Simek 	return 0;
4308d59d7f6SMichal Simek }
4318d59d7f6SMichal Simek #else
4320678941aSNitin Jain int dram_init_banksize(void)
4330678941aSNitin Jain {
4340678941aSNitin Jain #if defined(CONFIG_NR_DRAM_BANKS)
4350678941aSNitin Jain 	gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
4360678941aSNitin Jain 	gd->bd->bi_dram[0].size = get_effective_memsize();
4370678941aSNitin Jain #endif
4380678941aSNitin Jain 
4390678941aSNitin Jain 	mem_map_fill();
4400678941aSNitin Jain 
4410678941aSNitin Jain 	return 0;
4420678941aSNitin Jain }
4430678941aSNitin Jain 
44484c7204bSMichal Simek int dram_init(void)
44584c7204bSMichal Simek {
44661dc92a2SMichal Simek 	gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
44761dc92a2SMichal Simek 				    CONFIG_SYS_SDRAM_SIZE);
44884c7204bSMichal Simek 
44984c7204bSMichal Simek 	return 0;
45084c7204bSMichal Simek }
4518d59d7f6SMichal Simek #endif
45284c7204bSMichal Simek 
45384c7204bSMichal Simek void reset_cpu(ulong addr)
45484c7204bSMichal Simek {
45584c7204bSMichal Simek }
45684c7204bSMichal Simek 
457d348beaaSMichal Simek static const struct {
458d348beaaSMichal Simek 	u32 bit;
459d348beaaSMichal Simek 	const char *name;
460d348beaaSMichal Simek } reset_reasons[] = {
461d348beaaSMichal Simek 	{ RESET_REASON_DEBUG_SYS, "DEBUG" },
462d348beaaSMichal Simek 	{ RESET_REASON_SOFT, "SOFT" },
463d348beaaSMichal Simek 	{ RESET_REASON_SRST, "SRST" },
464d348beaaSMichal Simek 	{ RESET_REASON_PSONLY, "PS-ONLY" },
465d348beaaSMichal Simek 	{ RESET_REASON_PMU, "PMU" },
466d348beaaSMichal Simek 	{ RESET_REASON_INTERNAL, "INTERNAL" },
467d348beaaSMichal Simek 	{ RESET_REASON_EXTERNAL, "EXTERNAL" },
468d348beaaSMichal Simek 	{}
469d348beaaSMichal Simek };
470d348beaaSMichal Simek 
471d348beaaSMichal Simek static u32 reset_reason(void)
472d348beaaSMichal Simek {
473d348beaaSMichal Simek 	u32 ret;
474d348beaaSMichal Simek 	int i;
475d348beaaSMichal Simek 	const char *reason = NULL;
476d348beaaSMichal Simek 
477d348beaaSMichal Simek 	ret = readl(&crlapb_base->reset_reason);
478d348beaaSMichal Simek 
479d348beaaSMichal Simek 	puts("Reset reason:\t");
480d348beaaSMichal Simek 
481d348beaaSMichal Simek 	for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
482d348beaaSMichal Simek 		if (ret & reset_reasons[i].bit) {
483d348beaaSMichal Simek 			reason = reset_reasons[i].name;
484d348beaaSMichal Simek 			printf("%s ", reset_reasons[i].name);
485d348beaaSMichal Simek 			break;
486d348beaaSMichal Simek 		}
487d348beaaSMichal Simek 	}
488d348beaaSMichal Simek 
489d348beaaSMichal Simek 	puts("\n");
490d348beaaSMichal Simek 
491d348beaaSMichal Simek 	env_set("reset_reason", reason);
492d348beaaSMichal Simek 
493d348beaaSMichal Simek 	writel(~0, &crlapb_base->reset_reason);
494d348beaaSMichal Simek 
495d348beaaSMichal Simek 	return ret;
496d348beaaSMichal Simek }
497d348beaaSMichal Simek 
49884c7204bSMichal Simek int board_late_init(void)
49984c7204bSMichal Simek {
50084c7204bSMichal Simek 	u32 reg = 0;
50184c7204bSMichal Simek 	u8 bootmode;
5022882b39dSMichal Simek 	struct udevice *dev;
5032882b39dSMichal Simek 	int bootseq = -1;
5042882b39dSMichal Simek 	int bootseq_len = 0;
5050478b0b9SMichal Simek 	int env_targets_len = 0;
506b72894f1SMichal Simek 	const char *mode;
507b72894f1SMichal Simek 	char *new_targets;
50801c42d3dSSiva Durga Prasad Paladugu 	char *env_targets;
509d1db89f4SSiva Durga Prasad Paladugu 	int ret;
510b72894f1SMichal Simek 
511b72894f1SMichal Simek 	if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
512b72894f1SMichal Simek 		debug("Saved variables - Skipping\n");
513b72894f1SMichal Simek 		return 0;
514b72894f1SMichal Simek 	}
51584c7204bSMichal Simek 
516d1db89f4SSiva Durga Prasad Paladugu 	ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, &reg);
517d1db89f4SSiva Durga Prasad Paladugu 	if (ret)
518d1db89f4SSiva Durga Prasad Paladugu 		return -EINVAL;
519d1db89f4SSiva Durga Prasad Paladugu 
52047359a03SMichal Simek 	if (reg >> BOOT_MODE_ALT_SHIFT)
52147359a03SMichal Simek 		reg >>= BOOT_MODE_ALT_SHIFT;
52247359a03SMichal Simek 
52384c7204bSMichal Simek 	bootmode = reg & BOOT_MODES_MASK;
52484c7204bSMichal Simek 
525fb90917cSMichal Simek 	puts("Bootmode: ");
52684c7204bSMichal Simek 	switch (bootmode) {
527d58fc12eSMichal Simek 	case USB_MODE:
528d58fc12eSMichal Simek 		puts("USB_MODE\n");
529d58fc12eSMichal Simek 		mode = "usb";
53007656ba5SMichal Simek 		env_set("modeboot", "usb_dfu_spl");
531d58fc12eSMichal Simek 		break;
5320a5bcc8cSSiva Durga Prasad Paladugu 	case JTAG_MODE:
533fb90917cSMichal Simek 		puts("JTAG_MODE\n");
534b72894f1SMichal Simek 		mode = "pxe dhcp";
53507656ba5SMichal Simek 		env_set("modeboot", "jtagboot");
5360a5bcc8cSSiva Durga Prasad Paladugu 		break;
5370a5bcc8cSSiva Durga Prasad Paladugu 	case QSPI_MODE_24BIT:
5380a5bcc8cSSiva Durga Prasad Paladugu 	case QSPI_MODE_32BIT:
539b72894f1SMichal Simek 		mode = "qspi0";
540fb90917cSMichal Simek 		puts("QSPI_MODE\n");
54107656ba5SMichal Simek 		env_set("modeboot", "qspiboot");
5420a5bcc8cSSiva Durga Prasad Paladugu 		break;
54339c56f55SMichal Simek 	case EMMC_MODE:
54478678feeSMichal Simek 		puts("EMMC_MODE\n");
545b72894f1SMichal Simek 		mode = "mmc0";
54607656ba5SMichal Simek 		env_set("modeboot", "emmcboot");
54778678feeSMichal Simek 		break;
54878678feeSMichal Simek 	case SD_MODE:
549fb90917cSMichal Simek 		puts("SD_MODE\n");
5502882b39dSMichal Simek 		if (uclass_get_device_by_name(UCLASS_MMC,
5512882b39dSMichal Simek 					      "sdhci@ff160000", &dev)) {
5522882b39dSMichal Simek 			puts("Boot from SD0 but without SD0 enabled!\n");
5532882b39dSMichal Simek 			return -1;
5542882b39dSMichal Simek 		}
5552882b39dSMichal Simek 		debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
5562882b39dSMichal Simek 
5572882b39dSMichal Simek 		mode = "mmc";
5582882b39dSMichal Simek 		bootseq = dev->seq;
55907656ba5SMichal Simek 		env_set("modeboot", "sdboot");
56084c7204bSMichal Simek 		break;
561e1992276SSiva Durga Prasad Paladugu 	case SD1_LSHFT_MODE:
562e1992276SSiva Durga Prasad Paladugu 		puts("LVL_SHFT_");
563e1992276SSiva Durga Prasad Paladugu 		/* fall through */
564af813acdSMichal Simek 	case SD_MODE1:
565fb90917cSMichal Simek 		puts("SD_MODE1\n");
5662882b39dSMichal Simek 		if (uclass_get_device_by_name(UCLASS_MMC,
5672882b39dSMichal Simek 					      "sdhci@ff170000", &dev)) {
5682882b39dSMichal Simek 			puts("Boot from SD1 but without SD1 enabled!\n");
5692882b39dSMichal Simek 			return -1;
5702882b39dSMichal Simek 		}
5712882b39dSMichal Simek 		debug("mmc1 device found at %p, seq %d\n", dev, dev->seq);
5722882b39dSMichal Simek 
5732882b39dSMichal Simek 		mode = "mmc";
5742882b39dSMichal Simek 		bootseq = dev->seq;
57507656ba5SMichal Simek 		env_set("modeboot", "sdboot");
576af813acdSMichal Simek 		break;
577af813acdSMichal Simek 	case NAND_MODE:
578fb90917cSMichal Simek 		puts("NAND_MODE\n");
579b72894f1SMichal Simek 		mode = "nand0";
58007656ba5SMichal Simek 		env_set("modeboot", "nandboot");
581af813acdSMichal Simek 		break;
58284c7204bSMichal Simek 	default:
583b72894f1SMichal Simek 		mode = "";
58484c7204bSMichal Simek 		printf("Invalid Boot Mode:0x%x\n", bootmode);
58584c7204bSMichal Simek 		break;
58684c7204bSMichal Simek 	}
58784c7204bSMichal Simek 
5882882b39dSMichal Simek 	if (bootseq >= 0) {
5892882b39dSMichal Simek 		bootseq_len = snprintf(NULL, 0, "%i", bootseq);
5902882b39dSMichal Simek 		debug("Bootseq len: %x\n", bootseq_len);
5912882b39dSMichal Simek 	}
5922882b39dSMichal Simek 
593b72894f1SMichal Simek 	/*
594b72894f1SMichal Simek 	 * One terminating char + one byte for space between mode
595b72894f1SMichal Simek 	 * and default boot_targets
596b72894f1SMichal Simek 	 */
59701c42d3dSSiva Durga Prasad Paladugu 	env_targets = env_get("boot_targets");
5980478b0b9SMichal Simek 	if (env_targets)
5990478b0b9SMichal Simek 		env_targets_len = strlen(env_targets);
6000478b0b9SMichal Simek 
6012882b39dSMichal Simek 	new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
6022882b39dSMichal Simek 			     bootseq_len);
6031e3e68f1SMichal Simek 	if (!new_targets)
6041e3e68f1SMichal Simek 		return -ENOMEM;
6050478b0b9SMichal Simek 
6062882b39dSMichal Simek 	if (bootseq >= 0)
6072882b39dSMichal Simek 		sprintf(new_targets, "%s%x %s", mode, bootseq,
6082882b39dSMichal Simek 			env_targets ? env_targets : "");
6092882b39dSMichal Simek 	else
6100478b0b9SMichal Simek 		sprintf(new_targets, "%s %s", mode,
6110478b0b9SMichal Simek 			env_targets ? env_targets : "");
612b72894f1SMichal Simek 
613382bee57SSimon Glass 	env_set("boot_targets", new_targets);
614b72894f1SMichal Simek 
615d348beaaSMichal Simek 	reset_reason();
616d348beaaSMichal Simek 
61784c7204bSMichal Simek 	return 0;
61884c7204bSMichal Simek }
61984696ff5SSiva Durga Prasad Paladugu 
62084696ff5SSiva Durga Prasad Paladugu int checkboard(void)
62184696ff5SSiva Durga Prasad Paladugu {
6225af08556SMichal Simek 	puts("Board: Xilinx ZynqMP\n");
62384696ff5SSiva Durga Prasad Paladugu 	return 0;
62484696ff5SSiva Durga Prasad Paladugu }
625