blob: 0c331e332230956f5e7386f3f3f9904e9f729b46 [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 Glass5e6201b2019-08-01 09:46:51 -06008#include <env.h>
Michal Simekd54b1af2015-09-30 17:26:55 +02009#include <sata.h>
Michal Simekb216cc12015-07-23 13:27:40 +020010#include <ahci.h>
11#include <scsi.h>
Michal Simekecfb6dc2016-04-22 14:28:54 +020012#include <malloc.h>
Michal Simekbf0f9ca2018-04-19 15:43:38 +020013#include <wdt.h>
Michal Simekc23d3f82015-11-05 08:34:35 +010014#include <asm/arch/clk.h>
Michal Simek04b7e622015-01-15 10:01:51 +010015#include <asm/arch/hardware.h>
16#include <asm/arch/sys_proto.h>
Michal Simekf2f08642018-01-10 09:36:09 +010017#include <asm/arch/psu_init_gpl.h>
Michal Simek04b7e622015-01-15 10:01:51 +010018#include <asm/io.h>
Michal Simekf183a982018-04-25 11:20:43 +020019#include <dm/device.h>
Michal Simekbf0f9ca2018-04-19 15:43:38 +020020#include <dm/uclass.h>
Siva Durga Prasad Paladuguba1f68e2015-08-04 13:03:26 +053021#include <usb.h>
22#include <dwc3-uboot.h>
Michal Simek8111aff2016-02-01 15:05:58 +010023#include <zynqmppl.h>
Michal Simek76d0a772016-09-01 11:16:40 +020024#include <g_dnl.h>
T Karthik Reddy0d2e7fe2019-08-20 09:30:57 +053025#include <linux/sizes.h>
Michal Simek04b7e622015-01-15 10:01:51 +010026
Luca Ceresoli23e65002019-05-21 18:06:43 +020027#include "pm_cfg_obj.h"
28
Michal Simek04b7e622015-01-15 10:01:51 +010029DECLARE_GLOBAL_DATA_PTR;
30
Michal Simek8111aff2016-02-01 15:05:58 +010031#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
32 !defined(CONFIG_SPL_BUILD)
33static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
34
35static const struct {
Michal Simek6908b862017-11-06 12:55:59 +010036 u32 id;
Michal Simek50d8cef2017-08-22 14:58:53 +020037 u32 ver;
Michal Simek8111aff2016-02-01 15:05:58 +010038 char *name;
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053039 bool evexists;
Michal Simek8111aff2016-02-01 15:05:58 +010040} zynqmp_devices[] = {
41 {
42 .id = 0x10,
43 .name = "3eg",
44 },
45 {
Michal Simek50d8cef2017-08-22 14:58:53 +020046 .id = 0x10,
47 .ver = 0x2c,
48 .name = "3cg",
49 },
50 {
Michal Simek8111aff2016-02-01 15:05:58 +010051 .id = 0x11,
52 .name = "2eg",
53 },
54 {
Michal Simek50d8cef2017-08-22 14:58:53 +020055 .id = 0x11,
56 .ver = 0x2c,
57 .name = "2cg",
58 },
59 {
Michal Simek8111aff2016-02-01 15:05:58 +010060 .id = 0x20,
61 .name = "5ev",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053062 .evexists = 1,
Michal Simek8111aff2016-02-01 15:05:58 +010063 },
64 {
Michal Simek50d8cef2017-08-22 14:58:53 +020065 .id = 0x20,
66 .ver = 0x100,
67 .name = "5eg",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053068 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +020069 },
70 {
71 .id = 0x20,
72 .ver = 0x12c,
73 .name = "5cg",
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +053074 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +020075 },
76 {
Michal Simek8111aff2016-02-01 15:05:58 +010077 .id = 0x21,
78 .name = "4ev",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053079 .evexists = 1,
Michal Simek8111aff2016-02-01 15:05:58 +010080 },
81 {
Michal Simek50d8cef2017-08-22 14:58:53 +020082 .id = 0x21,
83 .ver = 0x100,
84 .name = "4eg",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053085 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +020086 },
87 {
88 .id = 0x21,
89 .ver = 0x12c,
90 .name = "4cg",
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +053091 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +020092 },
93 {
Michal Simek8111aff2016-02-01 15:05:58 +010094 .id = 0x30,
95 .name = "7ev",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053096 .evexists = 1,
Michal Simek8111aff2016-02-01 15:05:58 +010097 },
98 {
Michal Simek50d8cef2017-08-22 14:58:53 +020099 .id = 0x30,
100 .ver = 0x100,
101 .name = "7eg",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530102 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +0200103 },
104 {
105 .id = 0x30,
106 .ver = 0x12c,
107 .name = "7cg",
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530108 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +0200109 },
110 {
Michal Simek8111aff2016-02-01 15:05:58 +0100111 .id = 0x38,
112 .name = "9eg",
113 },
114 {
Michal Simek50d8cef2017-08-22 14:58:53 +0200115 .id = 0x38,
116 .ver = 0x2c,
117 .name = "9cg",
118 },
119 {
Michal Simek8111aff2016-02-01 15:05:58 +0100120 .id = 0x39,
121 .name = "6eg",
122 },
123 {
Michal Simek50d8cef2017-08-22 14:58:53 +0200124 .id = 0x39,
125 .ver = 0x2c,
126 .name = "6cg",
127 },
128 {
Michal Simek8111aff2016-02-01 15:05:58 +0100129 .id = 0x40,
130 .name = "11eg",
131 },
Michal Simek50d8cef2017-08-22 14:58:53 +0200132 { /* For testing purpose only */
133 .id = 0x50,
134 .ver = 0x2c,
135 .name = "15cg",
136 },
Michal Simek8111aff2016-02-01 15:05:58 +0100137 {
138 .id = 0x50,
139 .name = "15eg",
140 },
141 {
142 .id = 0x58,
143 .name = "19eg",
144 },
145 {
146 .id = 0x59,
147 .name = "17eg",
148 },
Michal Simekb510e532017-06-02 08:08:59 +0200149 {
150 .id = 0x61,
151 .name = "21dr",
152 },
153 {
154 .id = 0x63,
155 .name = "23dr",
156 },
157 {
158 .id = 0x65,
159 .name = "25dr",
160 },
161 {
162 .id = 0x64,
163 .name = "27dr",
164 },
165 {
166 .id = 0x60,
167 .name = "28dr",
168 },
169 {
170 .id = 0x62,
171 .name = "29dr",
172 },
Siva Durga Prasad Paladugu70866b42019-03-23 15:00:06 +0530173 {
174 .id = 0x66,
175 .name = "39dr",
176 },
Michal Simek8111aff2016-02-01 15:05:58 +0100177};
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530178#endif
Michal Simek8111aff2016-02-01 15:05:58 +0100179
Siva Durga Prasad Paladugucd35d522017-07-25 11:51:38 +0530180int chip_id(unsigned char id)
Michal Simek8111aff2016-02-01 15:05:58 +0100181{
182 struct pt_regs regs;
Siva Durga Prasad Paladugu9f0a8e92017-07-25 11:51:36 +0530183 int val = -EINVAL;
Michal Simek8111aff2016-02-01 15:05:58 +0100184
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530185 if (current_el() != 3) {
186 regs.regs[0] = ZYNQMP_SIP_SVC_CSU_DMA_CHIPID;
187 regs.regs[1] = 0;
188 regs.regs[2] = 0;
189 regs.regs[3] = 0;
Michal Simek8111aff2016-02-01 15:05:58 +0100190
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530191 smc_call(&regs);
192
193 /*
194 * SMC returns:
195 * regs[0][31:0] = status of the operation
196 * regs[0][63:32] = CSU.IDCODE register
197 * regs[1][31:0] = CSU.version register
Michal Simek50d8cef2017-08-22 14:58:53 +0200198 * regs[1][63:32] = CSU.IDCODE2 register
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530199 */
200 switch (id) {
201 case IDCODE:
202 regs.regs[0] = upper_32_bits(regs.regs[0]);
203 regs.regs[0] &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
204 ZYNQMP_CSU_IDCODE_SVD_MASK;
205 regs.regs[0] >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
206 val = regs.regs[0];
207 break;
208 case VERSION:
209 regs.regs[1] = lower_32_bits(regs.regs[1]);
210 regs.regs[1] &= ZYNQMP_CSU_SILICON_VER_MASK;
211 val = regs.regs[1];
212 break;
Michal Simek50d8cef2017-08-22 14:58:53 +0200213 case IDCODE2:
214 regs.regs[1] = lower_32_bits(regs.regs[1]);
215 regs.regs[1] >>= ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
216 val = regs.regs[1];
217 break;
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530218 default:
219 printf("%s, Invalid Req:0x%x\n", __func__, id);
220 }
221 } else {
222 switch (id) {
223 case IDCODE:
224 val = readl(ZYNQMP_CSU_IDCODE_ADDR);
225 val &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
226 ZYNQMP_CSU_IDCODE_SVD_MASK;
227 val >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
228 break;
229 case VERSION:
230 val = readl(ZYNQMP_CSU_VER_ADDR);
231 val &= ZYNQMP_CSU_SILICON_VER_MASK;
232 break;
233 default:
234 printf("%s, Invalid Req:0x%x\n", __func__, id);
235 }
Siva Durga Prasad Paladugu9f0a8e92017-07-25 11:51:36 +0530236 }
Soren Brinkmannd7696a52016-09-29 11:44:41 -0700237
Siva Durga Prasad Paladugu9f0a8e92017-07-25 11:51:36 +0530238 return val;
Michal Simek8111aff2016-02-01 15:05:58 +0100239}
240
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530241#define ZYNQMP_VERSION_SIZE 9
242#define ZYNQMP_PL_STATUS_BIT 9
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530243#define ZYNQMP_IPDIS_VCU_BIT 8
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530244#define ZYNQMP_PL_STATUS_MASK BIT(ZYNQMP_PL_STATUS_BIT)
245#define ZYNQMP_CSU_VERSION_MASK ~(ZYNQMP_PL_STATUS_MASK)
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530246#define ZYNQMP_CSU_VCUDIS_VER_MASK ZYNQMP_CSU_VERSION_MASK & \
247 ~BIT(ZYNQMP_IPDIS_VCU_BIT)
248#define MAX_VARIANTS_EV 3
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530249
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530250#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
251 !defined(CONFIG_SPL_BUILD)
Michal Simek8111aff2016-02-01 15:05:58 +0100252static char *zynqmp_get_silicon_idcode_name(void)
253{
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530254 u32 i, id, ver, j;
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530255 char *buf;
256 static char name[ZYNQMP_VERSION_SIZE];
Michal Simek8111aff2016-02-01 15:05:58 +0100257
Siva Durga Prasad Paladugu9f0a8e92017-07-25 11:51:36 +0530258 id = chip_id(IDCODE);
Michal Simek50d8cef2017-08-22 14:58:53 +0200259 ver = chip_id(IDCODE2);
260
Michal Simek8111aff2016-02-01 15:05:58 +0100261 for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530262 if (zynqmp_devices[i].id == id) {
263 if (zynqmp_devices[i].evexists &&
264 !(ver & ZYNQMP_PL_STATUS_MASK))
265 break;
266 if (zynqmp_devices[i].ver == (ver &
267 ZYNQMP_CSU_VERSION_MASK))
268 break;
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530269 }
Michal Simek8111aff2016-02-01 15:05:58 +0100270 }
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530271
272 if (i >= ARRAY_SIZE(zynqmp_devices))
273 return "unknown";
274
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530275 strncat(name, "zu", 2);
276 if (!zynqmp_devices[i].evexists ||
277 (ver & ZYNQMP_PL_STATUS_MASK)) {
278 strncat(name, zynqmp_devices[i].name,
279 ZYNQMP_VERSION_SIZE - 3);
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530280 return name;
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530281 }
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530282
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530283 /*
284 * Here we are means, PL not powered up and ev variant
285 * exists. So, we need to ignore VCU disable bit(8) in
286 * version and findout if its CG or EG/EV variant.
287 */
288 for (j = 0; j < MAX_VARIANTS_EV; j++, i++) {
289 if ((zynqmp_devices[i].ver & ~BIT(ZYNQMP_IPDIS_VCU_BIT)) ==
290 (ver & ZYNQMP_CSU_VCUDIS_VER_MASK)) {
291 strncat(name, zynqmp_devices[i].name,
292 ZYNQMP_VERSION_SIZE - 3);
293 break;
294 }
295 }
296
297 if (j >= MAX_VARIANTS_EV)
298 return "unknown";
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530299
300 if (strstr(name, "eg") || strstr(name, "ev")) {
301 buf = strstr(name, "e");
302 *buf = '\0';
303 }
304
305 return name;
Michal Simek8111aff2016-02-01 15:05:58 +0100306}
307#endif
308
Michal Simek8b353302017-02-07 14:32:26 +0100309int board_early_init_f(void)
310{
Michal Simekc8785f22018-01-10 11:48:48 +0100311 int ret = 0;
Michal Simek8b353302017-02-07 14:32:26 +0100312#if !defined(CONFIG_SPL_BUILD) && defined(CONFIG_CLK_ZYNQMP)
Siva Durga Prasad Paladugu8406d472018-08-21 15:44:49 +0530313 u32 pm_api_version;
314
315 pm_api_version = zynqmp_pmufw_version();
316 printf("PMUFW:\tv%d.%d\n",
317 pm_api_version >> ZYNQMP_PM_VERSION_MAJOR_SHIFT,
318 pm_api_version & ZYNQMP_PM_VERSION_MINOR_MASK);
319
320 if (pm_api_version < ZYNQMP_PM_VERSION)
321 panic("PMUFW version error. Expected: v%d.%d\n",
322 ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR);
Michal Simek8b353302017-02-07 14:32:26 +0100323#endif
Michal Simeke0f36102017-07-12 13:08:41 +0200324
Michal Simek1a1ab5a2018-01-15 12:52:59 +0100325#if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
Michal Simekc8785f22018-01-10 11:48:48 +0100326 ret = psu_init();
Michal Simeke0f36102017-07-12 13:08:41 +0200327#endif
328
Michal Simekc8785f22018-01-10 11:48:48 +0100329 return ret;
Michal Simek8b353302017-02-07 14:32:26 +0100330}
331
Michal Simek04b7e622015-01-15 10:01:51 +0100332int board_init(void)
333{
Luca Ceresoli23e65002019-05-21 18:06:43 +0200334#if defined(CONFIG_SPL_BUILD)
335 /* Check *at build time* if the filename is an non-empty string */
336 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
337 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
338 zynqmp_pm_cfg_obj_size);
339#endif
340
Michal Simekfb7242d2015-06-22 14:31:06 +0200341 printf("EL Level:\tEL%d\n", current_el());
342
Michal Simek8111aff2016-02-01 15:05:58 +0100343#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
344 !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
345 defined(CONFIG_SPL_BUILD))
346 if (current_el() != 3) {
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530347 zynqmppl.name = zynqmp_get_silicon_idcode_name();
Michal Simek8111aff2016-02-01 15:05:58 +0100348 printf("Chip ID:\t%s\n", zynqmppl.name);
349 fpga_init();
350 fpga_add(fpga_xilinx, &zynqmppl);
351 }
Michal Simekbf0f9ca2018-04-19 15:43:38 +0200352#endif
353
Michal Simek04b7e622015-01-15 10:01:51 +0100354 return 0;
355}
356
357int board_early_init_r(void)
358{
359 u32 val;
360
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530361 if (current_el() != 3)
362 return 0;
363
Michal Simek245d5282017-07-12 10:32:18 +0200364 val = readl(&crlapb_base->timestamp_ref_ctrl);
365 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
366
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530367 if (!val) {
Michal Simekc23d3f82015-11-05 08:34:35 +0100368 val = readl(&crlapb_base->timestamp_ref_ctrl);
369 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
370 writel(val, &crlapb_base->timestamp_ref_ctrl);
Michal Simek04b7e622015-01-15 10:01:51 +0100371
Michal Simekc23d3f82015-11-05 08:34:35 +0100372 /* Program freq register in System counter */
373 writel(zynqmp_get_system_timer_freq(),
374 &iou_scntr_secure->base_frequency_id_register);
375 /* And enable system counter */
376 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
377 &iou_scntr_secure->counter_control_register);
378 }
Michal Simek04b7e622015-01-15 10:01:51 +0100379 return 0;
380}
381
Nitin Jainb2eb59b2018-02-16 12:56:17 +0530382unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
383 char * const argv[])
384{
385 int ret = 0;
386
387 if (current_el() > 1) {
388 smp_kick_all_cpus();
389 dcache_disable();
390 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
391 ES_TO_AARCH64);
392 } else {
393 printf("FAIL: current EL is not above EL1\n");
394 ret = EINVAL;
395 }
396 return ret;
397}
398
Michal Simek8faa66a2016-02-08 09:34:53 +0100399#if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
Simon Glass2f949c32017-03-31 08:40:32 -0600400int dram_init_banksize(void)
Michal Simek8faa66a2016-02-08 09:34:53 +0100401{
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530402 int ret;
403
404 ret = fdtdec_setup_memory_banksize();
405 if (ret)
406 return ret;
407
408 mem_map_fill();
409
410 return 0;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500411}
Michal Simek8faa66a2016-02-08 09:34:53 +0100412
Tom Riniedcfdbd2016-12-09 07:56:54 -0500413int dram_init(void)
414{
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530415 if (fdtdec_setup_mem_size_base() != 0)
Nathan Rossiac04bfa2016-12-19 00:03:34 +1000416 return -EINVAL;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500417
418 return 0;
Michal Simek8faa66a2016-02-08 09:34:53 +0100419}
420#else
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530421int dram_init_banksize(void)
422{
423#if defined(CONFIG_NR_DRAM_BANKS)
424 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
425 gd->bd->bi_dram[0].size = get_effective_memsize();
426#endif
427
428 mem_map_fill();
429
430 return 0;
431}
432
Michal Simek04b7e622015-01-15 10:01:51 +0100433int dram_init(void)
434{
Michal Simek1b846212018-04-11 16:12:28 +0200435 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
436 CONFIG_SYS_SDRAM_SIZE);
Michal Simek04b7e622015-01-15 10:01:51 +0100437
438 return 0;
439}
Michal Simek8faa66a2016-02-08 09:34:53 +0100440#endif
Michal Simek04b7e622015-01-15 10:01:51 +0100441
Michal Simek04b7e622015-01-15 10:01:51 +0100442void reset_cpu(ulong addr)
443{
444}
445
Michal Simek342edfe2018-12-20 09:33:38 +0100446#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simek29b9b712018-05-17 14:06:06 +0200447static const struct {
448 u32 bit;
449 const char *name;
450} reset_reasons[] = {
451 { RESET_REASON_DEBUG_SYS, "DEBUG" },
452 { RESET_REASON_SOFT, "SOFT" },
453 { RESET_REASON_SRST, "SRST" },
454 { RESET_REASON_PSONLY, "PS-ONLY" },
455 { RESET_REASON_PMU, "PMU" },
456 { RESET_REASON_INTERNAL, "INTERNAL" },
457 { RESET_REASON_EXTERNAL, "EXTERNAL" },
458 {}
459};
460
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530461static int reset_reason(void)
Michal Simek29b9b712018-05-17 14:06:06 +0200462{
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530463 u32 reg;
464 int i, ret;
Michal Simek29b9b712018-05-17 14:06:06 +0200465 const char *reason = NULL;
466
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530467 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, &reg);
468 if (ret)
469 return -EINVAL;
Michal Simek29b9b712018-05-17 14:06:06 +0200470
471 puts("Reset reason:\t");
472
473 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530474 if (reg & reset_reasons[i].bit) {
Michal Simek29b9b712018-05-17 14:06:06 +0200475 reason = reset_reasons[i].name;
476 printf("%s ", reset_reasons[i].name);
477 break;
478 }
479 }
480
481 puts("\n");
482
483 env_set("reset_reason", reason);
484
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530485 ret = zynqmp_mmio_write(~0, ~0, (ulong)&crlapb_base->reset_reason);
486 if (ret)
487 return -EINVAL;
Michal Simek29b9b712018-05-17 14:06:06 +0200488
489 return ret;
490}
491
Michal Simek1ca66d72019-02-14 13:14:30 +0100492static int set_fdtfile(void)
493{
494 char *compatible, *fdtfile;
495 const char *suffix = ".dtb";
496 const char *vendor = "xilinx/";
497
498 if (env_get("fdtfile"))
499 return 0;
500
501 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible", NULL);
502 if (compatible) {
503 debug("Compatible: %s\n", compatible);
504
505 /* Discard vendor prefix */
506 strsep(&compatible, ",");
507
508 fdtfile = calloc(1, strlen(vendor) + strlen(compatible) +
509 strlen(suffix) + 1);
510 if (!fdtfile)
511 return -ENOMEM;
512
513 sprintf(fdtfile, "%s%s%s", vendor, compatible, suffix);
514
515 env_set("fdtfile", fdtfile);
516 free(fdtfile);
517 }
518
519 return 0;
520}
521
Michal Simek04b7e622015-01-15 10:01:51 +0100522int board_late_init(void)
523{
524 u32 reg = 0;
525 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;
T Karthik Reddy0d2e7fe2019-08-20 09:30:57 +0530534 ulong initrd_hi;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200535
Michal Simek482f5492018-10-05 08:55:16 +0200536#if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
537 usb_ether_init();
538#endif
539
Michal Simekecfb6dc2016-04-22 14:28:54 +0200540 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
541 debug("Saved variables - Skipping\n");
542 return 0;
543 }
Michal Simek04b7e622015-01-15 10:01:51 +0100544
Michal Simek1ca66d72019-02-14 13:14:30 +0100545 ret = set_fdtfile();
546 if (ret)
547 return ret;
548
Siva Durga Prasad Paladugue6fd3bb2017-02-21 17:58:28 +0530549 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, &reg);
550 if (ret)
551 return -EINVAL;
552
Michal Simek833e0c42016-10-25 11:43:02 +0200553 if (reg >> BOOT_MODE_ALT_SHIFT)
554 reg >>= BOOT_MODE_ALT_SHIFT;
555
Michal Simek04b7e622015-01-15 10:01:51 +0100556 bootmode = reg & BOOT_MODES_MASK;
557
Michal Simekc5d95232015-09-20 17:20:42 +0200558 puts("Bootmode: ");
Michal Simek04b7e622015-01-15 10:01:51 +0100559 switch (bootmode) {
Michal Simek12398ea2016-08-19 14:14:52 +0200560 case USB_MODE:
561 puts("USB_MODE\n");
562 mode = "usb";
Michal Simek43380352017-12-01 15:18:24 +0100563 env_set("modeboot", "usb_dfu_spl");
Michal Simek12398ea2016-08-19 14:14:52 +0200564 break;
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530565 case JTAG_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200566 puts("JTAG_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200567 mode = "pxe dhcp";
Michal Simek43380352017-12-01 15:18:24 +0100568 env_set("modeboot", "jtagboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530569 break;
570 case QSPI_MODE_24BIT:
571 case QSPI_MODE_32BIT:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200572 mode = "qspi0";
Michal Simekc5d95232015-09-20 17:20:42 +0200573 puts("QSPI_MODE\n");
Michal Simek43380352017-12-01 15:18:24 +0100574 env_set("modeboot", "qspiboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530575 break;
Michal Simek02d66cd2015-04-15 15:02:28 +0200576 case EMMC_MODE:
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200577 puts("EMMC_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200578 mode = "mmc0";
Michal Simek43380352017-12-01 15:18:24 +0100579 env_set("modeboot", "emmcboot");
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200580 break;
581 case SD_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200582 puts("SD_MODE\n");
Michal Simekf183a982018-04-25 11:20:43 +0200583 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530584 "mmc@ff160000", &dev) &&
585 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200586 "sdhci@ff160000", &dev)) {
587 puts("Boot from SD0 but without SD0 enabled!\n");
588 return -1;
589 }
590 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
591
592 mode = "mmc";
593 bootseq = dev->seq;
Michal Simek43380352017-12-01 15:18:24 +0100594 env_set("modeboot", "sdboot");
Michal Simek04b7e622015-01-15 10:01:51 +0100595 break;
Siva Durga Prasad Paladugu29a77d22016-09-21 11:45:05 +0530596 case SD1_LSHFT_MODE:
597 puts("LVL_SHFT_");
598 /* fall through */
Michal Simek108e1842015-10-05 10:51:12 +0200599 case SD_MODE1:
Michal Simekc5d95232015-09-20 17:20:42 +0200600 puts("SD_MODE1\n");
Michal Simekf183a982018-04-25 11:20:43 +0200601 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530602 "mmc@ff170000", &dev) &&
603 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200604 "sdhci@ff170000", &dev)) {
605 puts("Boot from SD1 but without SD1 enabled!\n");
606 return -1;
607 }
608 debug("mmc1 device found at %p, seq %d\n", dev, dev->seq);
609
610 mode = "mmc";
611 bootseq = dev->seq;
Michal Simek43380352017-12-01 15:18:24 +0100612 env_set("modeboot", "sdboot");
Michal Simek108e1842015-10-05 10:51:12 +0200613 break;
614 case NAND_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200615 puts("NAND_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200616 mode = "nand0";
Michal Simek43380352017-12-01 15:18:24 +0100617 env_set("modeboot", "nandboot");
Michal Simek108e1842015-10-05 10:51:12 +0200618 break;
Michal Simek04b7e622015-01-15 10:01:51 +0100619 default:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200620 mode = "";
Michal Simek04b7e622015-01-15 10:01:51 +0100621 printf("Invalid Boot Mode:0x%x\n", bootmode);
622 break;
623 }
624
Michal Simekf183a982018-04-25 11:20:43 +0200625 if (bootseq >= 0) {
626 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
627 debug("Bootseq len: %x\n", bootseq_len);
628 }
629
Michal Simekecfb6dc2016-04-22 14:28:54 +0200630 /*
631 * One terminating char + one byte for space between mode
632 * and default boot_targets
633 */
Siva Durga Prasad Paladugu245c5562017-12-20 16:35:06 +0530634 env_targets = env_get("boot_targets");
Michal Simek7410b142018-04-25 11:10:34 +0200635 if (env_targets)
636 env_targets_len = strlen(env_targets);
637
Michal Simekf183a982018-04-25 11:20:43 +0200638 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
639 bootseq_len);
Michal Simek089b84d2018-06-13 09:42:41 +0200640 if (!new_targets)
641 return -ENOMEM;
Michal Simek7410b142018-04-25 11:10:34 +0200642
Michal Simekf183a982018-04-25 11:20:43 +0200643 if (bootseq >= 0)
644 sprintf(new_targets, "%s%x %s", mode, bootseq,
645 env_targets ? env_targets : "");
646 else
647 sprintf(new_targets, "%s %s", mode,
648 env_targets ? env_targets : "");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200649
Simon Glass6a38e412017-08-03 12:22:09 -0600650 env_set("boot_targets", new_targets);
Michal Simekecfb6dc2016-04-22 14:28:54 +0200651
T Karthik Reddy0d2e7fe2019-08-20 09:30:57 +0530652 initrd_hi = gd->start_addr_sp - CONFIG_STACK_SIZE;
653 initrd_hi = round_down(initrd_hi, SZ_16M);
654 env_set_addr("initrd_high", (void *)initrd_hi);
655
Michal Simek29b9b712018-05-17 14:06:06 +0200656 reset_reason();
657
Michal Simek04b7e622015-01-15 10:01:51 +0100658 return 0;
659}
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}