blob: 006284fdc2670d87b4e32c31db8f32e63e2eb600 [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
Ibai Erkiaga4f736182020-08-04 23:17:31 +010041#define ZYNQMP_VERSION_SIZE 7
42#define EFUSE_VCU_DIS_MASK 0x100
43#define EFUSE_VCU_DIS_SHIFT 8
44#define EFUSE_GPU_DIS_MASK 0x20
45#define EFUSE_GPU_DIS_SHIFT 5
46#define IDCODE2_PL_INIT_MASK 0x200
47#define IDCODE2_PL_INIT_SHIFT 9
48
Michal Simek04b7e622015-01-15 10:01:51 +010049DECLARE_GLOBAL_DATA_PTR;
50
Michal Simek8111aff2016-02-01 15:05:58 +010051#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
Ibai Erkiaga4f736182020-08-04 23:17:31 +010052 !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
53 defined(CONFIG_SPL_BUILD))
54
Michal Simek8111aff2016-02-01 15:05:58 +010055static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
56
Ibai Erkiaga4f736182020-08-04 23:17:31 +010057enum {
58 ZYNQMP_VARIANT_EG = BIT(0U),
59 ZYNQMP_VARIANT_EV = BIT(1U),
60 ZYNQMP_VARIANT_CG = BIT(2U),
61 ZYNQMP_VARIANT_DR = BIT(3U),
62};
63
Michal Simek8111aff2016-02-01 15:05:58 +010064static const struct {
Michal Simek6908b862017-11-06 12:55:59 +010065 u32 id;
Ibai Erkiaga4f736182020-08-04 23:17:31 +010066 u8 device;
67 u8 variants;
Michal Simek8111aff2016-02-01 15:05:58 +010068} zynqmp_devices[] = {
69 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +010070 .id = 0x04711093,
71 .device = 2,
72 .variants = ZYNQMP_VARIANT_EG | ZYNQMP_VARIANT_CG,
Michal Simek8111aff2016-02-01 15:05:58 +010073 },
74 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +010075 .id = 0x04710093,
76 .device = 3,
77 .variants = ZYNQMP_VARIANT_EG | ZYNQMP_VARIANT_CG,
Michal Simek50d8cef2017-08-22 14:58:53 +020078 },
79 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +010080 .id = 0x04721093,
81 .device = 4,
82 .variants = ZYNQMP_VARIANT_EG | ZYNQMP_VARIANT_CG |
83 ZYNQMP_VARIANT_EV,
Michal Simek50d8cef2017-08-22 14:58:53 +020084 },
85 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +010086 .id = 0x04720093,
87 .device = 5,
88 .variants = ZYNQMP_VARIANT_EG | ZYNQMP_VARIANT_CG |
89 ZYNQMP_VARIANT_EV,
Michal Simek8111aff2016-02-01 15:05:58 +010090 },
91 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +010092 .id = 0x04739093,
93 .device = 6,
94 .variants = ZYNQMP_VARIANT_EG | ZYNQMP_VARIANT_CG,
Michal Simek50d8cef2017-08-22 14:58:53 +020095 },
96 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +010097 .id = 0x04730093,
98 .device = 7,
99 .variants = ZYNQMP_VARIANT_EG | ZYNQMP_VARIANT_CG |
100 ZYNQMP_VARIANT_EV,
Michal Simek8111aff2016-02-01 15:05:58 +0100101 },
102 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100103 .id = 0x04738093,
104 .device = 9,
105 .variants = ZYNQMP_VARIANT_EG,
Michal Simek50d8cef2017-08-22 14:58:53 +0200106 },
107 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100108 .id = 0x04740093,
109 .device = 11,
110 .variants = ZYNQMP_VARIANT_EG,
Michal Simek50d8cef2017-08-22 14:58:53 +0200111 },
Michal Simek8111aff2016-02-01 15:05:58 +0100112 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100113 .id = 0x04750093,
114 .device = 15,
115 .variants = ZYNQMP_VARIANT_EG,
Michal Simek8111aff2016-02-01 15:05:58 +0100116 },
117 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100118 .id = 0x04759093,
119 .device = 17,
120 .variants = ZYNQMP_VARIANT_EG,
Michal Simek8111aff2016-02-01 15:05:58 +0100121 },
122 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100123 .id = 0x04758093,
124 .device = 19,
125 .variants = ZYNQMP_VARIANT_EG,
Michal Simek8111aff2016-02-01 15:05:58 +0100126 },
Michal Simekb510e532017-06-02 08:08:59 +0200127 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100128 .id = 0x047E1093,
129 .device = 21,
130 .variants = ZYNQMP_VARIANT_DR,
Michal Simekb510e532017-06-02 08:08:59 +0200131 },
132 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100133 .id = 0x047E3093,
134 .device = 23,
135 .variants = ZYNQMP_VARIANT_DR,
Michal Simekb510e532017-06-02 08:08:59 +0200136 },
137 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100138 .id = 0x047E5093,
139 .device = 25,
140 .variants = ZYNQMP_VARIANT_DR,
Michal Simekb510e532017-06-02 08:08:59 +0200141 },
142 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100143 .id = 0x047E4093,
144 .device = 27,
145 .variants = ZYNQMP_VARIANT_DR,
Michal Simekb510e532017-06-02 08:08:59 +0200146 },
147 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100148 .id = 0x047E0093,
149 .device = 28,
150 .variants = ZYNQMP_VARIANT_DR,
Michal Simekb510e532017-06-02 08:08:59 +0200151 },
152 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100153 .id = 0x047E2093,
154 .device = 29,
155 .variants = ZYNQMP_VARIANT_DR,
Michal Simekb510e532017-06-02 08:08:59 +0200156 },
Siva Durga Prasad Paladugu70866b42019-03-23 15:00:06 +0530157 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100158 .id = 0x047E6093,
159 .device = 39,
160 .variants = ZYNQMP_VARIANT_DR,
Siva Durga Prasad Paladugu70866b42019-03-23 15:00:06 +0530161 },
Siva Durga Prasad Paladugu85f61a82019-07-23 11:56:17 +0530162 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100163 .id = 0x047FB093,
164 .device = 48,
165 .variants = ZYNQMP_VARIANT_DR,
Siva Durga Prasad Paladugu85f61a82019-07-23 11:56:17 +0530166 },
167 {
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100168 .id = 0x047FE093,
169 .device = 49,
170 .variants = ZYNQMP_VARIANT_DR,
Siva Durga Prasad Paladugu85f61a82019-07-23 11:56:17 +0530171 },
Michal Simek8111aff2016-02-01 15:05:58 +0100172};
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530173
Michal Simek8111aff2016-02-01 15:05:58 +0100174static char *zynqmp_get_silicon_idcode_name(void)
175{
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100176 u32 i;
177 u32 idcode, idcode2;
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530178 static char name[ZYNQMP_VERSION_SIZE];
Ibai Erkiagac318ecb2020-08-04 23:17:30 +0100179 u32 ret_payload[PAYLOAD_ARG_CNT];
180
181 xilinx_pm_request(PM_GET_CHIPID, 0, 0, 0, 0, ret_payload);
182
183 /*
184 * Firmware returns:
185 * payload[0][31:0] = status of the operation
186 * payload[1]] = IDCODE
187 * payload[2][19:0] = Version
188 * payload[2][28:20] = EXTENDED_IDCODE
189 * payload[2][29] = PL_INIT
190 */
191
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100192 idcode = ret_payload[1];
193 idcode2 = ret_payload[2] >> ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
194 debug("%s, IDCODE: 0x%0X, IDCODE2: 0x%0X\r\n", __func__, idcode,
195 idcode2);
Michal Simek50d8cef2017-08-22 14:58:53 +0200196
Michal Simek8111aff2016-02-01 15:05:58 +0100197 for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100198 if (zynqmp_devices[i].id == (idcode & 0x0FFFFFFF))
199 break;
Michal Simek8111aff2016-02-01 15:05:58 +0100200 }
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530201
202 if (i >= ARRAY_SIZE(zynqmp_devices))
203 return "unknown";
204
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100205 /* Add device prefix to the name */
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530206 strncat(name, "zu", 2);
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100207 strncat(&name[2], simple_itoa(zynqmp_devices[i].device), 2);
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530208
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100209 if (zynqmp_devices[i].variants & ZYNQMP_VARIANT_EV) {
210 /* Devices with EV variant might be EG/CG/EV family */
211 if (idcode2 & IDCODE2_PL_INIT_MASK) {
212 u32 family = ((idcode2 & EFUSE_VCU_DIS_MASK) >>
213 EFUSE_VCU_DIS_SHIFT) << 1 |
214 ((idcode2 & EFUSE_GPU_DIS_MASK) >>
215 EFUSE_GPU_DIS_SHIFT);
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530216
Ibai Erkiaga4f736182020-08-04 23:17:31 +0100217 /*
218 * Get family name based on extended idcode values as
219 * determined on UG1087, EXTENDED_IDCODE register
220 * description
221 */
222 switch (family) {
223 case 0x00:
224 strncat(name, "ev", 2);
225 break;
226 case 0x10:
227 strncat(name, "eg", 2);
228 break;
229 case 0x11:
230 strncat(name, "cg", 2);
231 break;
232 default:
233 /* Do not append family name*/
234 break;
235 }
236 } else {
237 /*
238 * When PL powered down the VCU Disable efuse cannot be
239 * read. So, ignore the bit and just findout if it is CG
240 * or EG/EV variant.
241 */
242 strncat(name, (idcode2 & EFUSE_GPU_DIS_MASK) ? "cg" :
243 "e", 2);
244 }
245 } else if (zynqmp_devices[i].variants & ZYNQMP_VARIANT_CG) {
246 /* Devices with CG variant might be EG or CG family */
247 strncat(name, (idcode2 & EFUSE_GPU_DIS_MASK) ? "cg" : "eg", 2);
248 } else if (zynqmp_devices[i].variants & ZYNQMP_VARIANT_EG) {
249 strncat(name, "eg", 2);
250 } else if (zynqmp_devices[i].variants & ZYNQMP_VARIANT_DR) {
251 strncat(name, "dr", 2);
252 } else {
253 debug("Variant not identified\n");
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530254 }
255
256 return name;
Michal Simek8111aff2016-02-01 15:05:58 +0100257}
258#endif
259
Michal Simek8b353302017-02-07 14:32:26 +0100260int board_early_init_f(void)
261{
Michal Simek1a1ab5a2018-01-15 12:52:59 +0100262#if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
Michal Simek09a7d7d2020-01-07 09:02:52 +0100263 int ret;
264
Michal Simekc8785f22018-01-10 11:48:48 +0100265 ret = psu_init();
Michal Simek09a7d7d2020-01-07 09:02:52 +0100266 if (ret)
267 return ret;
Michal Simek1f55e572020-03-20 08:59:02 +0100268
269 /* Delay is required for clocks to be propagated */
270 udelay(1000000);
Michal Simeke0f36102017-07-12 13:08:41 +0200271#endif
272
Michal Simek09a7d7d2020-01-07 09:02:52 +0100273#ifdef CONFIG_DEBUG_UART
274 /* Uart debug for sure */
275 debug_uart_init();
276 puts("Debug uart enabled\n"); /* or printch() */
277#endif
278
279 return 0;
Michal Simek8b353302017-02-07 14:32:26 +0100280}
281
Michal Simek46900462020-02-11 12:43:14 +0100282static int multi_boot(void)
283{
284 u32 multiboot;
285
286 multiboot = readl(&csu_base->multi_boot);
287
Michal Simekc55f2d52020-05-27 12:50:33 +0200288 printf("Multiboot:\t%d\n", multiboot);
Michal Simek46900462020-02-11 12:43:14 +0100289
290 return 0;
291}
292
Mike Looijmans9863e2f2019-10-18 07:34:13 +0200293#define PS_SYSMON_ANALOG_BUS_VAL 0x3210
294#define PS_SYSMON_ANALOG_BUS_REG 0xFFA50914
295
Michal Simek04b7e622015-01-15 10:01:51 +0100296int board_init(void)
297{
Michal Simek826d7eca2020-03-04 08:48:16 +0100298#if defined(CONFIG_ZYNQMP_FIRMWARE)
Ibai Erkiaga2d9b95b2019-09-27 11:37:04 +0100299 struct udevice *dev;
300
301 uclass_get_device_by_name(UCLASS_FIRMWARE, "zynqmp-power", &dev);
302 if (!dev)
303 panic("PMU Firmware device not found - Enable it");
Michal Simek826d7eca2020-03-04 08:48:16 +0100304#endif
Ibai Erkiaga2d9b95b2019-09-27 11:37:04 +0100305
Luca Ceresoli23e65002019-05-21 18:06:43 +0200306#if defined(CONFIG_SPL_BUILD)
307 /* Check *at build time* if the filename is an non-empty string */
308 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
309 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
310 zynqmp_pm_cfg_obj_size);
311#endif
312
Michal Simekfb7242d2015-06-22 14:31:06 +0200313 printf("EL Level:\tEL%d\n", current_el());
314
Mike Looijmans9863e2f2019-10-18 07:34:13 +0200315 /* Bug in ROM sets wrong value in this register */
316 writel(PS_SYSMON_ANALOG_BUS_VAL, PS_SYSMON_ANALOG_BUS_REG);
317
Michal Simek8111aff2016-02-01 15:05:58 +0100318#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
319 !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
320 defined(CONFIG_SPL_BUILD))
Ibai Erkiagae91ca7c2020-08-04 23:17:29 +0100321 zynqmppl.name = zynqmp_get_silicon_idcode_name();
322 printf("Chip ID:\t%s\n", zynqmppl.name);
323 fpga_init();
324 fpga_add(fpga_xilinx, &zynqmppl);
Michal Simekbf0f9ca2018-04-19 15:43:38 +0200325#endif
326
Michal Simek46900462020-02-11 12:43:14 +0100327 if (current_el() == 3)
328 multi_boot();
329
Michal Simek04b7e622015-01-15 10:01:51 +0100330 return 0;
331}
332
333int board_early_init_r(void)
334{
335 u32 val;
336
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530337 if (current_el() != 3)
338 return 0;
339
Michal Simek245d5282017-07-12 10:32:18 +0200340 val = readl(&crlapb_base->timestamp_ref_ctrl);
341 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
342
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530343 if (!val) {
Michal Simekc23d3f82015-11-05 08:34:35 +0100344 val = readl(&crlapb_base->timestamp_ref_ctrl);
345 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
346 writel(val, &crlapb_base->timestamp_ref_ctrl);
Michal Simek04b7e622015-01-15 10:01:51 +0100347
Michal Simekc23d3f82015-11-05 08:34:35 +0100348 /* Program freq register in System counter */
349 writel(zynqmp_get_system_timer_freq(),
350 &iou_scntr_secure->base_frequency_id_register);
351 /* And enable system counter */
352 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
353 &iou_scntr_secure->counter_control_register);
354 }
Michal Simek04b7e622015-01-15 10:01:51 +0100355 return 0;
356}
357
Nitin Jainb2eb59b2018-02-16 12:56:17 +0530358unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
Simon Glassed38aef2020-05-10 11:40:03 -0600359 char *const argv[])
Nitin Jainb2eb59b2018-02-16 12:56:17 +0530360{
361 int ret = 0;
362
363 if (current_el() > 1) {
364 smp_kick_all_cpus();
365 dcache_disable();
366 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
367 ES_TO_AARCH64);
368 } else {
369 printf("FAIL: current EL is not above EL1\n");
370 ret = EINVAL;
371 }
372 return ret;
373}
374
Michal Simek8faa66a2016-02-08 09:34:53 +0100375#if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
Simon Glass2f949c32017-03-31 08:40:32 -0600376int dram_init_banksize(void)
Michal Simek8faa66a2016-02-08 09:34:53 +0100377{
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530378 int ret;
379
380 ret = fdtdec_setup_memory_banksize();
381 if (ret)
382 return ret;
383
384 mem_map_fill();
385
386 return 0;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500387}
Michal Simek8faa66a2016-02-08 09:34:53 +0100388
Tom Riniedcfdbd2016-12-09 07:56:54 -0500389int dram_init(void)
390{
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530391 if (fdtdec_setup_mem_size_base() != 0)
Nathan Rossiac04bfa2016-12-19 00:03:34 +1000392 return -EINVAL;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500393
394 return 0;
Michal Simek8faa66a2016-02-08 09:34:53 +0100395}
396#else
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530397int dram_init_banksize(void)
398{
399#if defined(CONFIG_NR_DRAM_BANKS)
400 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
401 gd->bd->bi_dram[0].size = get_effective_memsize();
402#endif
403
404 mem_map_fill();
405
406 return 0;
407}
408
Michal Simek04b7e622015-01-15 10:01:51 +0100409int dram_init(void)
410{
Michal Simek1b846212018-04-11 16:12:28 +0200411 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
412 CONFIG_SYS_SDRAM_SIZE);
Michal Simek04b7e622015-01-15 10:01:51 +0100413
414 return 0;
415}
Michal Simek8faa66a2016-02-08 09:34:53 +0100416#endif
Michal Simek04b7e622015-01-15 10:01:51 +0100417
Michal Simek04b7e622015-01-15 10:01:51 +0100418void reset_cpu(ulong addr)
419{
420}
421
Michal Simek342edfe2018-12-20 09:33:38 +0100422#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simek29b9b712018-05-17 14:06:06 +0200423static const struct {
424 u32 bit;
425 const char *name;
426} reset_reasons[] = {
427 { RESET_REASON_DEBUG_SYS, "DEBUG" },
428 { RESET_REASON_SOFT, "SOFT" },
429 { RESET_REASON_SRST, "SRST" },
430 { RESET_REASON_PSONLY, "PS-ONLY" },
431 { RESET_REASON_PMU, "PMU" },
432 { RESET_REASON_INTERNAL, "INTERNAL" },
433 { RESET_REASON_EXTERNAL, "EXTERNAL" },
434 {}
435};
436
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530437static int reset_reason(void)
Michal Simek29b9b712018-05-17 14:06:06 +0200438{
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530439 u32 reg;
440 int i, ret;
Michal Simek29b9b712018-05-17 14:06:06 +0200441 const char *reason = NULL;
442
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530443 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, &reg);
444 if (ret)
445 return -EINVAL;
Michal Simek29b9b712018-05-17 14:06:06 +0200446
447 puts("Reset reason:\t");
448
449 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530450 if (reg & reset_reasons[i].bit) {
Michal Simek29b9b712018-05-17 14:06:06 +0200451 reason = reset_reasons[i].name;
452 printf("%s ", reset_reasons[i].name);
453 break;
454 }
455 }
456
457 puts("\n");
458
459 env_set("reset_reason", reason);
460
Michal Simek4c4efde2020-03-23 14:02:01 +0100461 ret = zynqmp_mmio_write((ulong)&crlapb_base->reset_reason, ~0, ~0);
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530462 if (ret)
463 return -EINVAL;
Michal Simek29b9b712018-05-17 14:06:06 +0200464
465 return ret;
466}
467
Michal Simek1ca66d72019-02-14 13:14:30 +0100468static int set_fdtfile(void)
469{
470 char *compatible, *fdtfile;
471 const char *suffix = ".dtb";
472 const char *vendor = "xilinx/";
Igor Lantsmane167bac2020-06-24 14:33:46 +0200473 int fdt_compat_len;
Michal Simek1ca66d72019-02-14 13:14:30 +0100474
475 if (env_get("fdtfile"))
476 return 0;
477
Igor Lantsmane167bac2020-06-24 14:33:46 +0200478 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible",
479 &fdt_compat_len);
480 if (compatible && fdt_compat_len) {
481 char *name;
482
Michal Simek1ca66d72019-02-14 13:14:30 +0100483 debug("Compatible: %s\n", compatible);
484
Igor Lantsmane167bac2020-06-24 14:33:46 +0200485 name = strchr(compatible, ',');
486 if (!name)
487 return -EINVAL;
Michal Simek1ca66d72019-02-14 13:14:30 +0100488
Igor Lantsmane167bac2020-06-24 14:33:46 +0200489 name++;
490
491 fdtfile = calloc(1, strlen(vendor) + strlen(name) +
Michal Simek1ca66d72019-02-14 13:14:30 +0100492 strlen(suffix) + 1);
493 if (!fdtfile)
494 return -ENOMEM;
495
Igor Lantsmane167bac2020-06-24 14:33:46 +0200496 sprintf(fdtfile, "%s%s%s", vendor, name, suffix);
Michal Simek1ca66d72019-02-14 13:14:30 +0100497
498 env_set("fdtfile", fdtfile);
499 free(fdtfile);
500 }
501
502 return 0;
503}
504
Michal Simek9c91e612020-04-08 11:04:41 +0200505static u8 zynqmp_get_bootmode(void)
Michal Simek04b7e622015-01-15 10:01:51 +0100506{
Michal Simek9c91e612020-04-08 11:04:41 +0200507 u8 bootmode;
Michal Simek04b7e622015-01-15 10:01:51 +0100508 u32 reg = 0;
Michal Simek9c91e612020-04-08 11:04:41 +0200509 int ret;
510
511 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, &reg);
512 if (ret)
513 return -EINVAL;
514
515 if (reg >> BOOT_MODE_ALT_SHIFT)
516 reg >>= BOOT_MODE_ALT_SHIFT;
517
518 bootmode = reg & BOOT_MODES_MASK;
519
520 return bootmode;
521}
522
523int board_late_init(void)
524{
Michal Simek04b7e622015-01-15 10:01:51 +0100525 u8 bootmode;
Michal Simekf183a982018-04-25 11:20:43 +0200526 struct udevice *dev;
527 int bootseq = -1;
528 int bootseq_len = 0;
Michal Simek7410b142018-04-25 11:10:34 +0200529 int env_targets_len = 0;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200530 const char *mode;
531 char *new_targets;
Siva Durga Prasad Paladugu245c5562017-12-20 16:35:06 +0530532 char *env_targets;
Siva Durga Prasad Paladugue6fd3bb2017-02-21 17:58:28 +0530533 int ret;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200534
Michal Simek482f5492018-10-05 08:55:16 +0200535#if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
536 usb_ether_init();
537#endif
538
Michal Simekecfb6dc2016-04-22 14:28:54 +0200539 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
540 debug("Saved variables - Skipping\n");
541 return 0;
542 }
Michal Simek04b7e622015-01-15 10:01:51 +0100543
Michal Simekbab07b62020-07-28 12:45:47 +0200544 if (!CONFIG_IS_ENABLED(ENV_VARS_UBOOT_RUNTIME_CONFIG))
545 return 0;
546
Michal Simek1ca66d72019-02-14 13:14:30 +0100547 ret = set_fdtfile();
548 if (ret)
549 return ret;
550
Michal Simek9c91e612020-04-08 11:04:41 +0200551 bootmode = zynqmp_get_bootmode();
Michal Simek04b7e622015-01-15 10:01:51 +0100552
Michal Simekc5d95232015-09-20 17:20:42 +0200553 puts("Bootmode: ");
Michal Simek04b7e622015-01-15 10:01:51 +0100554 switch (bootmode) {
Michal Simek12398ea2016-08-19 14:14:52 +0200555 case USB_MODE:
556 puts("USB_MODE\n");
557 mode = "usb";
Michal Simek43380352017-12-01 15:18:24 +0100558 env_set("modeboot", "usb_dfu_spl");
Michal Simek12398ea2016-08-19 14:14:52 +0200559 break;
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530560 case JTAG_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200561 puts("JTAG_MODE\n");
Siva Durga Prasad Paladugu9c441702019-06-25 17:41:09 +0530562 mode = "jtag pxe dhcp";
Michal Simek43380352017-12-01 15:18:24 +0100563 env_set("modeboot", "jtagboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530564 break;
565 case QSPI_MODE_24BIT:
566 case QSPI_MODE_32BIT:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200567 mode = "qspi0";
Michal Simekc5d95232015-09-20 17:20:42 +0200568 puts("QSPI_MODE\n");
Michal Simek43380352017-12-01 15:18:24 +0100569 env_set("modeboot", "qspiboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530570 break;
Michal Simek02d66cd2015-04-15 15:02:28 +0200571 case EMMC_MODE:
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200572 puts("EMMC_MODE\n");
T Karthik Reddy19735c32019-12-17 06:41:42 -0700573 if (uclass_get_device_by_name(UCLASS_MMC,
574 "mmc@ff160000", &dev) &&
575 uclass_get_device_by_name(UCLASS_MMC,
576 "sdhci@ff160000", &dev)) {
577 puts("Boot from EMMC but without SD0 enabled!\n");
578 return -1;
579 }
580 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
581
582 mode = "mmc";
583 bootseq = dev->seq;
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200584 break;
585 case SD_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200586 puts("SD_MODE\n");
Michal Simekf183a982018-04-25 11:20:43 +0200587 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530588 "mmc@ff160000", &dev) &&
589 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200590 "sdhci@ff160000", &dev)) {
591 puts("Boot from SD0 but without SD0 enabled!\n");
592 return -1;
593 }
594 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
595
596 mode = "mmc";
597 bootseq = dev->seq;
Michal Simek43380352017-12-01 15:18:24 +0100598 env_set("modeboot", "sdboot");
Michal Simek04b7e622015-01-15 10:01:51 +0100599 break;
Siva Durga Prasad Paladugu29a77d22016-09-21 11:45:05 +0530600 case SD1_LSHFT_MODE:
601 puts("LVL_SHFT_");
602 /* fall through */
Michal Simek108e1842015-10-05 10:51:12 +0200603 case SD_MODE1:
Michal Simekc5d95232015-09-20 17:20:42 +0200604 puts("SD_MODE1\n");
Michal Simekf183a982018-04-25 11:20:43 +0200605 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530606 "mmc@ff170000", &dev) &&
607 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200608 "sdhci@ff170000", &dev)) {
609 puts("Boot from SD1 but without SD1 enabled!\n");
610 return -1;
611 }
612 debug("mmc1 device found at %p, seq %d\n", dev, dev->seq);
613
614 mode = "mmc";
615 bootseq = dev->seq;
Michal Simek43380352017-12-01 15:18:24 +0100616 env_set("modeboot", "sdboot");
Michal Simek108e1842015-10-05 10:51:12 +0200617 break;
618 case NAND_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200619 puts("NAND_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200620 mode = "nand0";
Michal Simek43380352017-12-01 15:18:24 +0100621 env_set("modeboot", "nandboot");
Michal Simek108e1842015-10-05 10:51:12 +0200622 break;
Michal Simek04b7e622015-01-15 10:01:51 +0100623 default:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200624 mode = "";
Michal Simek04b7e622015-01-15 10:01:51 +0100625 printf("Invalid Boot Mode:0x%x\n", bootmode);
626 break;
627 }
628
Michal Simekf183a982018-04-25 11:20:43 +0200629 if (bootseq >= 0) {
630 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
631 debug("Bootseq len: %x\n", bootseq_len);
632 }
633
Michal Simekecfb6dc2016-04-22 14:28:54 +0200634 /*
635 * One terminating char + one byte for space between mode
636 * and default boot_targets
637 */
Siva Durga Prasad Paladugu245c5562017-12-20 16:35:06 +0530638 env_targets = env_get("boot_targets");
Michal Simek7410b142018-04-25 11:10:34 +0200639 if (env_targets)
640 env_targets_len = strlen(env_targets);
641
Michal Simekf183a982018-04-25 11:20:43 +0200642 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
643 bootseq_len);
Michal Simek089b84d2018-06-13 09:42:41 +0200644 if (!new_targets)
645 return -ENOMEM;
Michal Simek7410b142018-04-25 11:10:34 +0200646
Michal Simekf183a982018-04-25 11:20:43 +0200647 if (bootseq >= 0)
648 sprintf(new_targets, "%s%x %s", mode, bootseq,
649 env_targets ? env_targets : "");
650 else
651 sprintf(new_targets, "%s %s", mode,
652 env_targets ? env_targets : "");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200653
Simon Glass6a38e412017-08-03 12:22:09 -0600654 env_set("boot_targets", new_targets);
Michal Simekecfb6dc2016-04-22 14:28:54 +0200655
Michal Simek29b9b712018-05-17 14:06:06 +0200656 reset_reason();
657
Michal Simek705d44a2020-03-31 12:39:37 +0200658 return board_late_init_xilinx();
Michal Simek04b7e622015-01-15 10:01:51 +0100659}
Michal Simek342edfe2018-12-20 09:33:38 +0100660#endif
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530661
662int checkboard(void)
663{
Michal Simek47ce9362016-01-25 11:04:21 +0100664 puts("Board: Xilinx ZynqMP\n");
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530665 return 0;
666}