blob: bcfd56a91287ee2f73ab8453c9e3b2e84b429321 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek04b7e622015-01-15 10:01:51 +01002/*
3 * (C) Copyright 2014 - 2015 Xilinx, Inc.
4 * Michal Simek <michal.simek@xilinx.com>
Michal Simek04b7e622015-01-15 10:01:51 +01005 */
6
7#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -06008#include <command.h>
Simon Glass370382c2019-11-14 12:57:35 -07009#include <cpu_func.h>
Michal Simek09a7d7d2020-01-07 09:02:52 +010010#include <debug_uart.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060011#include <env.h>
Simon Glassa7b51302019-11-14 12:57:46 -070012#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <net.h>
Michal Simekd54b1af2015-09-30 17:26:55 +020015#include <sata.h>
Michal Simekb216cc12015-07-23 13:27:40 +020016#include <ahci.h>
17#include <scsi.h>
Michal Simekecfb6dc2016-04-22 14:28:54 +020018#include <malloc.h>
Michal Simekbf0f9ca2018-04-19 15:43:38 +020019#include <wdt.h>
Michal Simekc23d3f82015-11-05 08:34:35 +010020#include <asm/arch/clk.h>
Michal Simek04b7e622015-01-15 10:01:51 +010021#include <asm/arch/hardware.h>
22#include <asm/arch/sys_proto.h>
Michal Simekf2f08642018-01-10 09:36:09 +010023#include <asm/arch/psu_init_gpl.h>
Simon Glass274e0b02020-05-10 11:39:56 -060024#include <asm/cache.h>
Michal Simek04b7e622015-01-15 10:01:51 +010025#include <asm/io.h>
Simon Glass6b9f0102020-05-10 11:40:06 -060026#include <asm/ptrace.h>
Michal Simekf183a982018-04-25 11:20:43 +020027#include <dm/device.h>
Michal Simekbf0f9ca2018-04-19 15:43:38 +020028#include <dm/uclass.h>
Siva Durga Prasad Paladuguba1f68e2015-08-04 13:03:26 +053029#include <usb.h>
30#include <dwc3-uboot.h>
Michal Simek8111aff2016-02-01 15:05:58 +010031#include <zynqmppl.h>
Ibai Erkiagac8a3efa2019-09-27 11:37:01 +010032#include <zynqmp_firmware.h>
Michal Simek76d0a772016-09-01 11:16:40 +020033#include <g_dnl.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060034#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060035#include <linux/delay.h>
36#include <linux/sizes.h>
Michal Simek705d44a2020-03-31 12:39:37 +020037#include "../common/board.h"
Michal Simek04b7e622015-01-15 10:01:51 +010038
Luca Ceresoli23e65002019-05-21 18:06:43 +020039#include "pm_cfg_obj.h"
40
Michal Simek04b7e622015-01-15 10:01:51 +010041DECLARE_GLOBAL_DATA_PTR;
42
Michal Simek8111aff2016-02-01 15:05:58 +010043#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
44 !defined(CONFIG_SPL_BUILD)
45static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
46
47static const struct {
Michal Simek6908b862017-11-06 12:55:59 +010048 u32 id;
Michal Simek50d8cef2017-08-22 14:58:53 +020049 u32 ver;
Michal Simek8111aff2016-02-01 15:05:58 +010050 char *name;
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053051 bool evexists;
Michal Simek8111aff2016-02-01 15:05:58 +010052} zynqmp_devices[] = {
53 {
54 .id = 0x10,
55 .name = "3eg",
56 },
57 {
Michal Simek50d8cef2017-08-22 14:58:53 +020058 .id = 0x10,
59 .ver = 0x2c,
60 .name = "3cg",
61 },
62 {
Michal Simek8111aff2016-02-01 15:05:58 +010063 .id = 0x11,
64 .name = "2eg",
65 },
66 {
Michal Simek50d8cef2017-08-22 14:58:53 +020067 .id = 0x11,
68 .ver = 0x2c,
69 .name = "2cg",
70 },
71 {
Michal Simek8111aff2016-02-01 15:05:58 +010072 .id = 0x20,
73 .name = "5ev",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053074 .evexists = 1,
Michal Simek8111aff2016-02-01 15:05:58 +010075 },
76 {
Michal Simek50d8cef2017-08-22 14:58:53 +020077 .id = 0x20,
78 .ver = 0x100,
79 .name = "5eg",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053080 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +020081 },
82 {
83 .id = 0x20,
84 .ver = 0x12c,
85 .name = "5cg",
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +053086 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +020087 },
88 {
Michal Simek8111aff2016-02-01 15:05:58 +010089 .id = 0x21,
90 .name = "4ev",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053091 .evexists = 1,
Michal Simek8111aff2016-02-01 15:05:58 +010092 },
93 {
Michal Simek50d8cef2017-08-22 14:58:53 +020094 .id = 0x21,
95 .ver = 0x100,
96 .name = "4eg",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053097 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +020098 },
99 {
100 .id = 0x21,
101 .ver = 0x12c,
102 .name = "4cg",
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530103 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +0200104 },
105 {
Michal Simek8111aff2016-02-01 15:05:58 +0100106 .id = 0x30,
107 .name = "7ev",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530108 .evexists = 1,
Michal Simek8111aff2016-02-01 15:05:58 +0100109 },
110 {
Michal Simek50d8cef2017-08-22 14:58:53 +0200111 .id = 0x30,
112 .ver = 0x100,
113 .name = "7eg",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530114 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +0200115 },
116 {
117 .id = 0x30,
118 .ver = 0x12c,
119 .name = "7cg",
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530120 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +0200121 },
122 {
Michal Simek8111aff2016-02-01 15:05:58 +0100123 .id = 0x38,
124 .name = "9eg",
125 },
126 {
Michal Simek50d8cef2017-08-22 14:58:53 +0200127 .id = 0x38,
128 .ver = 0x2c,
129 .name = "9cg",
130 },
131 {
Michal Simek8111aff2016-02-01 15:05:58 +0100132 .id = 0x39,
133 .name = "6eg",
134 },
135 {
Michal Simek50d8cef2017-08-22 14:58:53 +0200136 .id = 0x39,
137 .ver = 0x2c,
138 .name = "6cg",
139 },
140 {
Michal Simek8111aff2016-02-01 15:05:58 +0100141 .id = 0x40,
142 .name = "11eg",
143 },
Michal Simek50d8cef2017-08-22 14:58:53 +0200144 { /* For testing purpose only */
145 .id = 0x50,
146 .ver = 0x2c,
147 .name = "15cg",
148 },
Michal Simek8111aff2016-02-01 15:05:58 +0100149 {
150 .id = 0x50,
151 .name = "15eg",
152 },
153 {
154 .id = 0x58,
155 .name = "19eg",
156 },
157 {
158 .id = 0x59,
159 .name = "17eg",
160 },
Michal Simekb510e532017-06-02 08:08:59 +0200161 {
162 .id = 0x61,
163 .name = "21dr",
164 },
165 {
166 .id = 0x63,
167 .name = "23dr",
168 },
169 {
170 .id = 0x65,
171 .name = "25dr",
172 },
173 {
174 .id = 0x64,
175 .name = "27dr",
176 },
177 {
178 .id = 0x60,
179 .name = "28dr",
180 },
181 {
182 .id = 0x62,
183 .name = "29dr",
184 },
Siva Durga Prasad Paladugu70866b42019-03-23 15:00:06 +0530185 {
186 .id = 0x66,
187 .name = "39dr",
188 },
Siva Durga Prasad Paladugu85f61a82019-07-23 11:56:17 +0530189 {
190 .id = 0x7b,
191 .name = "48dr",
192 },
193 {
194 .id = 0x7e,
195 .name = "49dr",
196 },
Michal Simek8111aff2016-02-01 15:05:58 +0100197};
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530198#endif
Michal Simek8111aff2016-02-01 15:05:58 +0100199
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530200#define ZYNQMP_VERSION_SIZE 9
201#define ZYNQMP_PL_STATUS_BIT 9
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530202#define ZYNQMP_IPDIS_VCU_BIT 8
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530203#define ZYNQMP_PL_STATUS_MASK BIT(ZYNQMP_PL_STATUS_BIT)
204#define ZYNQMP_CSU_VERSION_MASK ~(ZYNQMP_PL_STATUS_MASK)
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530205#define ZYNQMP_CSU_VCUDIS_VER_MASK ZYNQMP_CSU_VERSION_MASK & \
206 ~BIT(ZYNQMP_IPDIS_VCU_BIT)
207#define MAX_VARIANTS_EV 3
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530208
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530209#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
210 !defined(CONFIG_SPL_BUILD)
Michal Simek8111aff2016-02-01 15:05:58 +0100211static char *zynqmp_get_silicon_idcode_name(void)
212{
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530213 u32 i, id, ver, j;
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530214 char *buf;
215 static char name[ZYNQMP_VERSION_SIZE];
Ibai Erkiagac318ecb2020-08-04 23:17:30 +0100216 u32 ret_payload[PAYLOAD_ARG_CNT];
217
218 xilinx_pm_request(PM_GET_CHIPID, 0, 0, 0, 0, ret_payload);
219
220 /*
221 * Firmware returns:
222 * payload[0][31:0] = status of the operation
223 * payload[1]] = IDCODE
224 * payload[2][19:0] = Version
225 * payload[2][28:20] = EXTENDED_IDCODE
226 * payload[2][29] = PL_INIT
227 */
228
229 /* Get IDCODE field */
230 id = ret_payload[1];
231 id &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK | ZYNQMP_CSU_IDCODE_SVD_MASK;
232 id >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
233
234 /* Shift silicon version info */
235 ver = ret_payload[2] >> ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
Michal Simek8111aff2016-02-01 15:05:58 +0100236
Ibai Erkiagaf44eda42020-08-04 23:17:28 +0100237 debug("%s, ID: 0x%0X, Ver: 0x%0X\r\n", __func__, id, ver);
Michal Simek50d8cef2017-08-22 14:58:53 +0200238
Michal Simek8111aff2016-02-01 15:05:58 +0100239 for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530240 if (zynqmp_devices[i].id == id) {
241 if (zynqmp_devices[i].evexists &&
242 !(ver & ZYNQMP_PL_STATUS_MASK))
243 break;
244 if (zynqmp_devices[i].ver == (ver &
245 ZYNQMP_CSU_VERSION_MASK))
246 break;
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530247 }
Michal Simek8111aff2016-02-01 15:05:58 +0100248 }
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530249
250 if (i >= ARRAY_SIZE(zynqmp_devices))
251 return "unknown";
252
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530253 strncat(name, "zu", 2);
254 if (!zynqmp_devices[i].evexists ||
255 (ver & ZYNQMP_PL_STATUS_MASK)) {
256 strncat(name, zynqmp_devices[i].name,
257 ZYNQMP_VERSION_SIZE - 3);
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530258 return name;
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530259 }
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530260
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530261 /*
262 * Here we are means, PL not powered up and ev variant
263 * exists. So, we need to ignore VCU disable bit(8) in
264 * version and findout if its CG or EG/EV variant.
265 */
266 for (j = 0; j < MAX_VARIANTS_EV; j++, i++) {
267 if ((zynqmp_devices[i].ver & ~BIT(ZYNQMP_IPDIS_VCU_BIT)) ==
268 (ver & ZYNQMP_CSU_VCUDIS_VER_MASK)) {
269 strncat(name, zynqmp_devices[i].name,
270 ZYNQMP_VERSION_SIZE - 3);
271 break;
272 }
273 }
274
275 if (j >= MAX_VARIANTS_EV)
276 return "unknown";
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530277
278 if (strstr(name, "eg") || strstr(name, "ev")) {
279 buf = strstr(name, "e");
280 *buf = '\0';
281 }
282
283 return name;
Michal Simek8111aff2016-02-01 15:05:58 +0100284}
285#endif
286
Michal Simek8b353302017-02-07 14:32:26 +0100287int board_early_init_f(void)
288{
Michal Simek1a1ab5a2018-01-15 12:52:59 +0100289#if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
Michal Simek09a7d7d2020-01-07 09:02:52 +0100290 int ret;
291
Michal Simekc8785f22018-01-10 11:48:48 +0100292 ret = psu_init();
Michal Simek09a7d7d2020-01-07 09:02:52 +0100293 if (ret)
294 return ret;
Michal Simek1f55e572020-03-20 08:59:02 +0100295
296 /* Delay is required for clocks to be propagated */
297 udelay(1000000);
Michal Simeke0f36102017-07-12 13:08:41 +0200298#endif
299
Michal Simek09a7d7d2020-01-07 09:02:52 +0100300#ifdef CONFIG_DEBUG_UART
301 /* Uart debug for sure */
302 debug_uart_init();
303 puts("Debug uart enabled\n"); /* or printch() */
304#endif
305
306 return 0;
Michal Simek8b353302017-02-07 14:32:26 +0100307}
308
Michal Simek46900462020-02-11 12:43:14 +0100309static int multi_boot(void)
310{
311 u32 multiboot;
312
313 multiboot = readl(&csu_base->multi_boot);
314
Michal Simekc55f2d52020-05-27 12:50:33 +0200315 printf("Multiboot:\t%d\n", multiboot);
Michal Simek46900462020-02-11 12:43:14 +0100316
317 return 0;
318}
319
Mike Looijmans9863e2f2019-10-18 07:34:13 +0200320#define PS_SYSMON_ANALOG_BUS_VAL 0x3210
321#define PS_SYSMON_ANALOG_BUS_REG 0xFFA50914
322
Michal Simek04b7e622015-01-15 10:01:51 +0100323int board_init(void)
324{
Michal Simek826d7eca2020-03-04 08:48:16 +0100325#if defined(CONFIG_ZYNQMP_FIRMWARE)
Ibai Erkiaga2d9b95b2019-09-27 11:37:04 +0100326 struct udevice *dev;
327
328 uclass_get_device_by_name(UCLASS_FIRMWARE, "zynqmp-power", &dev);
329 if (!dev)
330 panic("PMU Firmware device not found - Enable it");
Michal Simek826d7eca2020-03-04 08:48:16 +0100331#endif
Ibai Erkiaga2d9b95b2019-09-27 11:37:04 +0100332
Luca Ceresoli23e65002019-05-21 18:06:43 +0200333#if defined(CONFIG_SPL_BUILD)
334 /* Check *at build time* if the filename is an non-empty string */
335 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
336 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
337 zynqmp_pm_cfg_obj_size);
338#endif
339
Michal Simekfb7242d2015-06-22 14:31:06 +0200340 printf("EL Level:\tEL%d\n", current_el());
341
Mike Looijmans9863e2f2019-10-18 07:34:13 +0200342 /* Bug in ROM sets wrong value in this register */
343 writel(PS_SYSMON_ANALOG_BUS_VAL, PS_SYSMON_ANALOG_BUS_REG);
344
Michal Simek8111aff2016-02-01 15:05:58 +0100345#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
346 !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
347 defined(CONFIG_SPL_BUILD))
Ibai Erkiagae91ca7c2020-08-04 23:17:29 +0100348 zynqmppl.name = zynqmp_get_silicon_idcode_name();
349 printf("Chip ID:\t%s\n", zynqmppl.name);
350 fpga_init();
351 fpga_add(fpga_xilinx, &zynqmppl);
Michal Simekbf0f9ca2018-04-19 15:43:38 +0200352#endif
353
Michal Simek46900462020-02-11 12:43:14 +0100354 if (current_el() == 3)
355 multi_boot();
356
Michal Simek04b7e622015-01-15 10:01:51 +0100357 return 0;
358}
359
360int board_early_init_r(void)
361{
362 u32 val;
363
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530364 if (current_el() != 3)
365 return 0;
366
Michal Simek245d5282017-07-12 10:32:18 +0200367 val = readl(&crlapb_base->timestamp_ref_ctrl);
368 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
369
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530370 if (!val) {
Michal Simekc23d3f82015-11-05 08:34:35 +0100371 val = readl(&crlapb_base->timestamp_ref_ctrl);
372 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
373 writel(val, &crlapb_base->timestamp_ref_ctrl);
Michal Simek04b7e622015-01-15 10:01:51 +0100374
Michal Simekc23d3f82015-11-05 08:34:35 +0100375 /* Program freq register in System counter */
376 writel(zynqmp_get_system_timer_freq(),
377 &iou_scntr_secure->base_frequency_id_register);
378 /* And enable system counter */
379 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
380 &iou_scntr_secure->counter_control_register);
381 }
Michal Simek04b7e622015-01-15 10:01:51 +0100382 return 0;
383}
384
Nitin Jainb2eb59b2018-02-16 12:56:17 +0530385unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
Simon Glassed38aef2020-05-10 11:40:03 -0600386 char *const argv[])
Nitin Jainb2eb59b2018-02-16 12:56:17 +0530387{
388 int ret = 0;
389
390 if (current_el() > 1) {
391 smp_kick_all_cpus();
392 dcache_disable();
393 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
394 ES_TO_AARCH64);
395 } else {
396 printf("FAIL: current EL is not above EL1\n");
397 ret = EINVAL;
398 }
399 return ret;
400}
401
Michal Simek8faa66a2016-02-08 09:34:53 +0100402#if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
Simon Glass2f949c32017-03-31 08:40:32 -0600403int dram_init_banksize(void)
Michal Simek8faa66a2016-02-08 09:34:53 +0100404{
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530405 int ret;
406
407 ret = fdtdec_setup_memory_banksize();
408 if (ret)
409 return ret;
410
411 mem_map_fill();
412
413 return 0;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500414}
Michal Simek8faa66a2016-02-08 09:34:53 +0100415
Tom Riniedcfdbd2016-12-09 07:56:54 -0500416int dram_init(void)
417{
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530418 if (fdtdec_setup_mem_size_base() != 0)
Nathan Rossiac04bfa2016-12-19 00:03:34 +1000419 return -EINVAL;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500420
421 return 0;
Michal Simek8faa66a2016-02-08 09:34:53 +0100422}
423#else
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530424int dram_init_banksize(void)
425{
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530426 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
427 gd->bd->bi_dram[0].size = get_effective_memsize();
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530428
429 mem_map_fill();
430
431 return 0;
432}
433
Michal Simek04b7e622015-01-15 10:01:51 +0100434int dram_init(void)
435{
Michal Simek1b846212018-04-11 16:12:28 +0200436 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
437 CONFIG_SYS_SDRAM_SIZE);
Michal Simek04b7e622015-01-15 10:01:51 +0100438
439 return 0;
440}
Michal Simek8faa66a2016-02-08 09:34:53 +0100441#endif
Michal Simek04b7e622015-01-15 10:01:51 +0100442
Michal Simek04b7e622015-01-15 10:01:51 +0100443void reset_cpu(ulong addr)
444{
445}
446
Michal Simek342edfe2018-12-20 09:33:38 +0100447#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simek29b9b712018-05-17 14:06:06 +0200448static const struct {
449 u32 bit;
450 const char *name;
451} reset_reasons[] = {
452 { RESET_REASON_DEBUG_SYS, "DEBUG" },
453 { RESET_REASON_SOFT, "SOFT" },
454 { RESET_REASON_SRST, "SRST" },
455 { RESET_REASON_PSONLY, "PS-ONLY" },
456 { RESET_REASON_PMU, "PMU" },
457 { RESET_REASON_INTERNAL, "INTERNAL" },
458 { RESET_REASON_EXTERNAL, "EXTERNAL" },
459 {}
460};
461
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530462static int reset_reason(void)
Michal Simek29b9b712018-05-17 14:06:06 +0200463{
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530464 u32 reg;
465 int i, ret;
Michal Simek29b9b712018-05-17 14:06:06 +0200466 const char *reason = NULL;
467
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530468 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, &reg);
469 if (ret)
470 return -EINVAL;
Michal Simek29b9b712018-05-17 14:06:06 +0200471
472 puts("Reset reason:\t");
473
474 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530475 if (reg & reset_reasons[i].bit) {
Michal Simek29b9b712018-05-17 14:06:06 +0200476 reason = reset_reasons[i].name;
477 printf("%s ", reset_reasons[i].name);
478 break;
479 }
480 }
481
482 puts("\n");
483
484 env_set("reset_reason", reason);
485
Michal Simek4c4efde2020-03-23 14:02:01 +0100486 ret = zynqmp_mmio_write((ulong)&crlapb_base->reset_reason, ~0, ~0);
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530487 if (ret)
488 return -EINVAL;
Michal Simek29b9b712018-05-17 14:06:06 +0200489
490 return ret;
491}
492
Michal Simek1ca66d72019-02-14 13:14:30 +0100493static int set_fdtfile(void)
494{
495 char *compatible, *fdtfile;
496 const char *suffix = ".dtb";
497 const char *vendor = "xilinx/";
Igor Lantsmane167bac2020-06-24 14:33:46 +0200498 int fdt_compat_len;
Michal Simek1ca66d72019-02-14 13:14:30 +0100499
500 if (env_get("fdtfile"))
501 return 0;
502
Igor Lantsmane167bac2020-06-24 14:33:46 +0200503 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible",
504 &fdt_compat_len);
505 if (compatible && fdt_compat_len) {
506 char *name;
507
Michal Simek1ca66d72019-02-14 13:14:30 +0100508 debug("Compatible: %s\n", compatible);
509
Igor Lantsmane167bac2020-06-24 14:33:46 +0200510 name = strchr(compatible, ',');
511 if (!name)
512 return -EINVAL;
Michal Simek1ca66d72019-02-14 13:14:30 +0100513
Igor Lantsmane167bac2020-06-24 14:33:46 +0200514 name++;
515
516 fdtfile = calloc(1, strlen(vendor) + strlen(name) +
Michal Simek1ca66d72019-02-14 13:14:30 +0100517 strlen(suffix) + 1);
518 if (!fdtfile)
519 return -ENOMEM;
520
Igor Lantsmane167bac2020-06-24 14:33:46 +0200521 sprintf(fdtfile, "%s%s%s", vendor, name, suffix);
Michal Simek1ca66d72019-02-14 13:14:30 +0100522
523 env_set("fdtfile", fdtfile);
524 free(fdtfile);
525 }
526
527 return 0;
528}
529
Michal Simek9c91e612020-04-08 11:04:41 +0200530static u8 zynqmp_get_bootmode(void)
Michal Simek04b7e622015-01-15 10:01:51 +0100531{
Michal Simek9c91e612020-04-08 11:04:41 +0200532 u8 bootmode;
Michal Simek04b7e622015-01-15 10:01:51 +0100533 u32 reg = 0;
Michal Simek9c91e612020-04-08 11:04:41 +0200534 int ret;
535
536 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, &reg);
537 if (ret)
538 return -EINVAL;
539
540 if (reg >> BOOT_MODE_ALT_SHIFT)
541 reg >>= BOOT_MODE_ALT_SHIFT;
542
543 bootmode = reg & BOOT_MODES_MASK;
544
545 return bootmode;
546}
547
548int board_late_init(void)
549{
Michal Simek04b7e622015-01-15 10:01:51 +0100550 u8 bootmode;
Michal Simekf183a982018-04-25 11:20:43 +0200551 struct udevice *dev;
552 int bootseq = -1;
553 int bootseq_len = 0;
Michal Simek7410b142018-04-25 11:10:34 +0200554 int env_targets_len = 0;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200555 const char *mode;
556 char *new_targets;
Siva Durga Prasad Paladugu245c5562017-12-20 16:35:06 +0530557 char *env_targets;
Siva Durga Prasad Paladugue6fd3bb2017-02-21 17:58:28 +0530558 int ret;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200559
Michal Simek482f5492018-10-05 08:55:16 +0200560#if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
561 usb_ether_init();
562#endif
563
Michal Simekecfb6dc2016-04-22 14:28:54 +0200564 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
565 debug("Saved variables - Skipping\n");
566 return 0;
567 }
Michal Simek04b7e622015-01-15 10:01:51 +0100568
Michal Simekbab07b62020-07-28 12:45:47 +0200569 if (!CONFIG_IS_ENABLED(ENV_VARS_UBOOT_RUNTIME_CONFIG))
570 return 0;
571
Michal Simek1ca66d72019-02-14 13:14:30 +0100572 ret = set_fdtfile();
573 if (ret)
574 return ret;
575
Michal Simek9c91e612020-04-08 11:04:41 +0200576 bootmode = zynqmp_get_bootmode();
Michal Simek04b7e622015-01-15 10:01:51 +0100577
Michal Simekc5d95232015-09-20 17:20:42 +0200578 puts("Bootmode: ");
Michal Simek04b7e622015-01-15 10:01:51 +0100579 switch (bootmode) {
Michal Simek12398ea2016-08-19 14:14:52 +0200580 case USB_MODE:
581 puts("USB_MODE\n");
582 mode = "usb";
Michal Simek43380352017-12-01 15:18:24 +0100583 env_set("modeboot", "usb_dfu_spl");
Michal Simek12398ea2016-08-19 14:14:52 +0200584 break;
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530585 case JTAG_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200586 puts("JTAG_MODE\n");
Siva Durga Prasad Paladugu9c441702019-06-25 17:41:09 +0530587 mode = "jtag pxe dhcp";
Michal Simek43380352017-12-01 15:18:24 +0100588 env_set("modeboot", "jtagboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530589 break;
590 case QSPI_MODE_24BIT:
591 case QSPI_MODE_32BIT:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200592 mode = "qspi0";
Michal Simekc5d95232015-09-20 17:20:42 +0200593 puts("QSPI_MODE\n");
Michal Simek43380352017-12-01 15:18:24 +0100594 env_set("modeboot", "qspiboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530595 break;
Michal Simek02d66cd2015-04-15 15:02:28 +0200596 case EMMC_MODE:
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200597 puts("EMMC_MODE\n");
T Karthik Reddy19735c32019-12-17 06:41:42 -0700598 if (uclass_get_device_by_name(UCLASS_MMC,
599 "mmc@ff160000", &dev) &&
600 uclass_get_device_by_name(UCLASS_MMC,
601 "sdhci@ff160000", &dev)) {
602 puts("Boot from EMMC but without SD0 enabled!\n");
603 return -1;
604 }
605 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
606
607 mode = "mmc";
608 bootseq = dev->seq;
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200609 break;
610 case SD_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200611 puts("SD_MODE\n");
Michal Simekf183a982018-04-25 11:20:43 +0200612 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530613 "mmc@ff160000", &dev) &&
614 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200615 "sdhci@ff160000", &dev)) {
616 puts("Boot from SD0 but without SD0 enabled!\n");
617 return -1;
618 }
619 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
620
621 mode = "mmc";
622 bootseq = dev->seq;
Michal Simek43380352017-12-01 15:18:24 +0100623 env_set("modeboot", "sdboot");
Michal Simek04b7e622015-01-15 10:01:51 +0100624 break;
Siva Durga Prasad Paladugu29a77d22016-09-21 11:45:05 +0530625 case SD1_LSHFT_MODE:
626 puts("LVL_SHFT_");
627 /* fall through */
Michal Simek108e1842015-10-05 10:51:12 +0200628 case SD_MODE1:
Michal Simekc5d95232015-09-20 17:20:42 +0200629 puts("SD_MODE1\n");
Michal Simekf183a982018-04-25 11:20:43 +0200630 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530631 "mmc@ff170000", &dev) &&
632 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200633 "sdhci@ff170000", &dev)) {
634 puts("Boot from SD1 but without SD1 enabled!\n");
635 return -1;
636 }
637 debug("mmc1 device found at %p, seq %d\n", dev, dev->seq);
638
639 mode = "mmc";
640 bootseq = dev->seq;
Michal Simek43380352017-12-01 15:18:24 +0100641 env_set("modeboot", "sdboot");
Michal Simek108e1842015-10-05 10:51:12 +0200642 break;
643 case NAND_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200644 puts("NAND_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200645 mode = "nand0";
Michal Simek43380352017-12-01 15:18:24 +0100646 env_set("modeboot", "nandboot");
Michal Simek108e1842015-10-05 10:51:12 +0200647 break;
Michal Simek04b7e622015-01-15 10:01:51 +0100648 default:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200649 mode = "";
Michal Simek04b7e622015-01-15 10:01:51 +0100650 printf("Invalid Boot Mode:0x%x\n", bootmode);
651 break;
652 }
653
Michal Simekf183a982018-04-25 11:20:43 +0200654 if (bootseq >= 0) {
655 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
656 debug("Bootseq len: %x\n", bootseq_len);
657 }
658
Michal Simekecfb6dc2016-04-22 14:28:54 +0200659 /*
660 * One terminating char + one byte for space between mode
661 * and default boot_targets
662 */
Siva Durga Prasad Paladugu245c5562017-12-20 16:35:06 +0530663 env_targets = env_get("boot_targets");
Michal Simek7410b142018-04-25 11:10:34 +0200664 if (env_targets)
665 env_targets_len = strlen(env_targets);
666
Michal Simekf183a982018-04-25 11:20:43 +0200667 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
668 bootseq_len);
Michal Simek089b84d2018-06-13 09:42:41 +0200669 if (!new_targets)
670 return -ENOMEM;
Michal Simek7410b142018-04-25 11:10:34 +0200671
Michal Simekf183a982018-04-25 11:20:43 +0200672 if (bootseq >= 0)
673 sprintf(new_targets, "%s%x %s", mode, bootseq,
674 env_targets ? env_targets : "");
675 else
676 sprintf(new_targets, "%s %s", mode,
677 env_targets ? env_targets : "");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200678
Simon Glass6a38e412017-08-03 12:22:09 -0600679 env_set("boot_targets", new_targets);
Michal Simekecfb6dc2016-04-22 14:28:54 +0200680
Michal Simek29b9b712018-05-17 14:06:06 +0200681 reset_reason();
682
Michal Simek705d44a2020-03-31 12:39:37 +0200683 return board_late_init_xilinx();
Michal Simek04b7e622015-01-15 10:01:51 +0100684}
Michal Simek342edfe2018-12-20 09:33:38 +0100685#endif
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530686
687int checkboard(void)
688{
Michal Simek47ce9362016-01-25 11:04:21 +0100689 puts("Board: Xilinx ZynqMP\n");
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530690 return 0;
691}