blob: aac2eb7bc1a2db8f2059b1bdb0f8556a8229bd3c [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 Glass370382c2019-11-14 12:57:35 -07008#include <cpu_func.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06009#include <env.h>
Simon Glassa7b51302019-11-14 12:57:46 -070010#include <init.h>
Michal Simekd54b1af2015-09-30 17:26:55 +020011#include <sata.h>
Michal Simekb216cc12015-07-23 13:27:40 +020012#include <ahci.h>
13#include <scsi.h>
Michal Simekecfb6dc2016-04-22 14:28:54 +020014#include <malloc.h>
Michal Simekbf0f9ca2018-04-19 15:43:38 +020015#include <wdt.h>
Michal Simekc23d3f82015-11-05 08:34:35 +010016#include <asm/arch/clk.h>
Michal Simek04b7e622015-01-15 10:01:51 +010017#include <asm/arch/hardware.h>
18#include <asm/arch/sys_proto.h>
Michal Simekf2f08642018-01-10 09:36:09 +010019#include <asm/arch/psu_init_gpl.h>
Michal Simek04b7e622015-01-15 10:01:51 +010020#include <asm/io.h>
Michal Simekf183a982018-04-25 11:20:43 +020021#include <dm/device.h>
Michal Simekbf0f9ca2018-04-19 15:43:38 +020022#include <dm/uclass.h>
Siva Durga Prasad Paladuguba1f68e2015-08-04 13:03:26 +053023#include <usb.h>
24#include <dwc3-uboot.h>
Michal Simek8111aff2016-02-01 15:05:58 +010025#include <zynqmppl.h>
Ibai Erkiagac8a3efa2019-09-27 11:37:01 +010026#include <zynqmp_firmware.h>
Michal Simek76d0a772016-09-01 11:16:40 +020027#include <g_dnl.h>
T Karthik Reddy0d2e7fe2019-08-20 09:30:57 +053028#include <linux/sizes.h>
Michal Simek04b7e622015-01-15 10:01:51 +010029
Luca Ceresoli23e65002019-05-21 18:06:43 +020030#include "pm_cfg_obj.h"
31
Michal Simek04b7e622015-01-15 10:01:51 +010032DECLARE_GLOBAL_DATA_PTR;
33
Michal Simek8111aff2016-02-01 15:05:58 +010034#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
35 !defined(CONFIG_SPL_BUILD)
36static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
37
38static const struct {
Michal Simek6908b862017-11-06 12:55:59 +010039 u32 id;
Michal Simek50d8cef2017-08-22 14:58:53 +020040 u32 ver;
Michal Simek8111aff2016-02-01 15:05:58 +010041 char *name;
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053042 bool evexists;
Michal Simek8111aff2016-02-01 15:05:58 +010043} zynqmp_devices[] = {
44 {
45 .id = 0x10,
46 .name = "3eg",
47 },
48 {
Michal Simek50d8cef2017-08-22 14:58:53 +020049 .id = 0x10,
50 .ver = 0x2c,
51 .name = "3cg",
52 },
53 {
Michal Simek8111aff2016-02-01 15:05:58 +010054 .id = 0x11,
55 .name = "2eg",
56 },
57 {
Michal Simek50d8cef2017-08-22 14:58:53 +020058 .id = 0x11,
59 .ver = 0x2c,
60 .name = "2cg",
61 },
62 {
Michal Simek8111aff2016-02-01 15:05:58 +010063 .id = 0x20,
64 .name = "5ev",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053065 .evexists = 1,
Michal Simek8111aff2016-02-01 15:05:58 +010066 },
67 {
Michal Simek50d8cef2017-08-22 14:58:53 +020068 .id = 0x20,
69 .ver = 0x100,
70 .name = "5eg",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053071 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +020072 },
73 {
74 .id = 0x20,
75 .ver = 0x12c,
76 .name = "5cg",
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +053077 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +020078 },
79 {
Michal Simek8111aff2016-02-01 15:05:58 +010080 .id = 0x21,
81 .name = "4ev",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053082 .evexists = 1,
Michal Simek8111aff2016-02-01 15:05:58 +010083 },
84 {
Michal Simek50d8cef2017-08-22 14:58:53 +020085 .id = 0x21,
86 .ver = 0x100,
87 .name = "4eg",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053088 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +020089 },
90 {
91 .id = 0x21,
92 .ver = 0x12c,
93 .name = "4cg",
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +053094 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +020095 },
96 {
Michal Simek8111aff2016-02-01 15:05:58 +010097 .id = 0x30,
98 .name = "7ev",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053099 .evexists = 1,
Michal Simek8111aff2016-02-01 15:05:58 +0100100 },
101 {
Michal Simek50d8cef2017-08-22 14:58:53 +0200102 .id = 0x30,
103 .ver = 0x100,
104 .name = "7eg",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530105 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +0200106 },
107 {
108 .id = 0x30,
109 .ver = 0x12c,
110 .name = "7cg",
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530111 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +0200112 },
113 {
Michal Simek8111aff2016-02-01 15:05:58 +0100114 .id = 0x38,
115 .name = "9eg",
116 },
117 {
Michal Simek50d8cef2017-08-22 14:58:53 +0200118 .id = 0x38,
119 .ver = 0x2c,
120 .name = "9cg",
121 },
122 {
Michal Simek8111aff2016-02-01 15:05:58 +0100123 .id = 0x39,
124 .name = "6eg",
125 },
126 {
Michal Simek50d8cef2017-08-22 14:58:53 +0200127 .id = 0x39,
128 .ver = 0x2c,
129 .name = "6cg",
130 },
131 {
Michal Simek8111aff2016-02-01 15:05:58 +0100132 .id = 0x40,
133 .name = "11eg",
134 },
Michal Simek50d8cef2017-08-22 14:58:53 +0200135 { /* For testing purpose only */
136 .id = 0x50,
137 .ver = 0x2c,
138 .name = "15cg",
139 },
Michal Simek8111aff2016-02-01 15:05:58 +0100140 {
141 .id = 0x50,
142 .name = "15eg",
143 },
144 {
145 .id = 0x58,
146 .name = "19eg",
147 },
148 {
149 .id = 0x59,
150 .name = "17eg",
151 },
Michal Simekb510e532017-06-02 08:08:59 +0200152 {
153 .id = 0x61,
154 .name = "21dr",
155 },
156 {
157 .id = 0x63,
158 .name = "23dr",
159 },
160 {
161 .id = 0x65,
162 .name = "25dr",
163 },
164 {
165 .id = 0x64,
166 .name = "27dr",
167 },
168 {
169 .id = 0x60,
170 .name = "28dr",
171 },
172 {
173 .id = 0x62,
174 .name = "29dr",
175 },
Siva Durga Prasad Paladugu70866b42019-03-23 15:00:06 +0530176 {
177 .id = 0x66,
178 .name = "39dr",
179 },
Siva Durga Prasad Paladugu85f61a82019-07-23 11:56:17 +0530180 {
181 .id = 0x7b,
182 .name = "48dr",
183 },
184 {
185 .id = 0x7e,
186 .name = "49dr",
187 },
Michal Simek8111aff2016-02-01 15:05:58 +0100188};
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530189#endif
Michal Simek8111aff2016-02-01 15:05:58 +0100190
Siva Durga Prasad Paladugucd35d522017-07-25 11:51:38 +0530191int chip_id(unsigned char id)
Michal Simek8111aff2016-02-01 15:05:58 +0100192{
193 struct pt_regs regs;
Siva Durga Prasad Paladugu9f0a8e92017-07-25 11:51:36 +0530194 int val = -EINVAL;
Michal Simek8111aff2016-02-01 15:05:58 +0100195
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530196 if (current_el() != 3) {
197 regs.regs[0] = ZYNQMP_SIP_SVC_CSU_DMA_CHIPID;
198 regs.regs[1] = 0;
199 regs.regs[2] = 0;
200 regs.regs[3] = 0;
Michal Simek8111aff2016-02-01 15:05:58 +0100201
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530202 smc_call(&regs);
203
204 /*
205 * SMC returns:
206 * regs[0][31:0] = status of the operation
207 * regs[0][63:32] = CSU.IDCODE register
208 * regs[1][31:0] = CSU.version register
Michal Simek50d8cef2017-08-22 14:58:53 +0200209 * regs[1][63:32] = CSU.IDCODE2 register
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530210 */
211 switch (id) {
212 case IDCODE:
213 regs.regs[0] = upper_32_bits(regs.regs[0]);
214 regs.regs[0] &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
215 ZYNQMP_CSU_IDCODE_SVD_MASK;
216 regs.regs[0] >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
217 val = regs.regs[0];
218 break;
219 case VERSION:
220 regs.regs[1] = lower_32_bits(regs.regs[1]);
221 regs.regs[1] &= ZYNQMP_CSU_SILICON_VER_MASK;
222 val = regs.regs[1];
223 break;
Michal Simek50d8cef2017-08-22 14:58:53 +0200224 case IDCODE2:
225 regs.regs[1] = lower_32_bits(regs.regs[1]);
226 regs.regs[1] >>= ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
227 val = regs.regs[1];
228 break;
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530229 default:
230 printf("%s, Invalid Req:0x%x\n", __func__, id);
231 }
232 } else {
233 switch (id) {
234 case IDCODE:
235 val = readl(ZYNQMP_CSU_IDCODE_ADDR);
236 val &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
237 ZYNQMP_CSU_IDCODE_SVD_MASK;
238 val >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
239 break;
240 case VERSION:
241 val = readl(ZYNQMP_CSU_VER_ADDR);
242 val &= ZYNQMP_CSU_SILICON_VER_MASK;
243 break;
244 default:
245 printf("%s, Invalid Req:0x%x\n", __func__, id);
246 }
Siva Durga Prasad Paladugu9f0a8e92017-07-25 11:51:36 +0530247 }
Soren Brinkmannd7696a52016-09-29 11:44:41 -0700248
Siva Durga Prasad Paladugu9f0a8e92017-07-25 11:51:36 +0530249 return val;
Michal Simek8111aff2016-02-01 15:05:58 +0100250}
251
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530252#define ZYNQMP_VERSION_SIZE 9
253#define ZYNQMP_PL_STATUS_BIT 9
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530254#define ZYNQMP_IPDIS_VCU_BIT 8
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530255#define ZYNQMP_PL_STATUS_MASK BIT(ZYNQMP_PL_STATUS_BIT)
256#define ZYNQMP_CSU_VERSION_MASK ~(ZYNQMP_PL_STATUS_MASK)
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530257#define ZYNQMP_CSU_VCUDIS_VER_MASK ZYNQMP_CSU_VERSION_MASK & \
258 ~BIT(ZYNQMP_IPDIS_VCU_BIT)
259#define MAX_VARIANTS_EV 3
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530260
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530261#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
262 !defined(CONFIG_SPL_BUILD)
Michal Simek8111aff2016-02-01 15:05:58 +0100263static char *zynqmp_get_silicon_idcode_name(void)
264{
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530265 u32 i, id, ver, j;
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530266 char *buf;
267 static char name[ZYNQMP_VERSION_SIZE];
Michal Simek8111aff2016-02-01 15:05:58 +0100268
Siva Durga Prasad Paladugu9f0a8e92017-07-25 11:51:36 +0530269 id = chip_id(IDCODE);
Michal Simek50d8cef2017-08-22 14:58:53 +0200270 ver = chip_id(IDCODE2);
271
Michal Simek8111aff2016-02-01 15:05:58 +0100272 for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530273 if (zynqmp_devices[i].id == id) {
274 if (zynqmp_devices[i].evexists &&
275 !(ver & ZYNQMP_PL_STATUS_MASK))
276 break;
277 if (zynqmp_devices[i].ver == (ver &
278 ZYNQMP_CSU_VERSION_MASK))
279 break;
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530280 }
Michal Simek8111aff2016-02-01 15:05:58 +0100281 }
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530282
283 if (i >= ARRAY_SIZE(zynqmp_devices))
284 return "unknown";
285
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530286 strncat(name, "zu", 2);
287 if (!zynqmp_devices[i].evexists ||
288 (ver & ZYNQMP_PL_STATUS_MASK)) {
289 strncat(name, zynqmp_devices[i].name,
290 ZYNQMP_VERSION_SIZE - 3);
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530291 return name;
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530292 }
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530293
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530294 /*
295 * Here we are means, PL not powered up and ev variant
296 * exists. So, we need to ignore VCU disable bit(8) in
297 * version and findout if its CG or EG/EV variant.
298 */
299 for (j = 0; j < MAX_VARIANTS_EV; j++, i++) {
300 if ((zynqmp_devices[i].ver & ~BIT(ZYNQMP_IPDIS_VCU_BIT)) ==
301 (ver & ZYNQMP_CSU_VCUDIS_VER_MASK)) {
302 strncat(name, zynqmp_devices[i].name,
303 ZYNQMP_VERSION_SIZE - 3);
304 break;
305 }
306 }
307
308 if (j >= MAX_VARIANTS_EV)
309 return "unknown";
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530310
311 if (strstr(name, "eg") || strstr(name, "ev")) {
312 buf = strstr(name, "e");
313 *buf = '\0';
314 }
315
316 return name;
Michal Simek8111aff2016-02-01 15:05:58 +0100317}
318#endif
319
Michal Simek8b353302017-02-07 14:32:26 +0100320int board_early_init_f(void)
321{
Michal Simekc8785f22018-01-10 11:48:48 +0100322 int ret = 0;
Michal Simeke0f36102017-07-12 13:08:41 +0200323
Michal Simek1a1ab5a2018-01-15 12:52:59 +0100324#if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
Michal Simekc8785f22018-01-10 11:48:48 +0100325 ret = psu_init();
Michal Simeke0f36102017-07-12 13:08:41 +0200326#endif
327
Michal Simekc8785f22018-01-10 11:48:48 +0100328 return ret;
Michal Simek8b353302017-02-07 14:32:26 +0100329}
330
Michal Simek04b7e622015-01-15 10:01:51 +0100331int board_init(void)
332{
Ibai Erkiaga2d9b95b2019-09-27 11:37:04 +0100333 struct udevice *dev;
334
335 uclass_get_device_by_name(UCLASS_FIRMWARE, "zynqmp-power", &dev);
336 if (!dev)
337 panic("PMU Firmware device not found - Enable it");
338
Luca Ceresoli23e65002019-05-21 18:06:43 +0200339#if defined(CONFIG_SPL_BUILD)
340 /* Check *at build time* if the filename is an non-empty string */
341 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
342 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
343 zynqmp_pm_cfg_obj_size);
344#endif
345
Michal Simekfb7242d2015-06-22 14:31:06 +0200346 printf("EL Level:\tEL%d\n", current_el());
347
Michal Simek8111aff2016-02-01 15:05:58 +0100348#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
349 !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
350 defined(CONFIG_SPL_BUILD))
351 if (current_el() != 3) {
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530352 zynqmppl.name = zynqmp_get_silicon_idcode_name();
Michal Simek8111aff2016-02-01 15:05:58 +0100353 printf("Chip ID:\t%s\n", zynqmppl.name);
354 fpga_init();
355 fpga_add(fpga_xilinx, &zynqmppl);
356 }
Michal Simekbf0f9ca2018-04-19 15:43:38 +0200357#endif
358
Michal Simek04b7e622015-01-15 10:01:51 +0100359 return 0;
360}
361
362int board_early_init_r(void)
363{
364 u32 val;
365
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530366 if (current_el() != 3)
367 return 0;
368
Michal Simek245d5282017-07-12 10:32:18 +0200369 val = readl(&crlapb_base->timestamp_ref_ctrl);
370 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
371
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530372 if (!val) {
Michal Simekc23d3f82015-11-05 08:34:35 +0100373 val = readl(&crlapb_base->timestamp_ref_ctrl);
374 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
375 writel(val, &crlapb_base->timestamp_ref_ctrl);
Michal Simek04b7e622015-01-15 10:01:51 +0100376
Michal Simekc23d3f82015-11-05 08:34:35 +0100377 /* Program freq register in System counter */
378 writel(zynqmp_get_system_timer_freq(),
379 &iou_scntr_secure->base_frequency_id_register);
380 /* And enable system counter */
381 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
382 &iou_scntr_secure->counter_control_register);
383 }
Michal Simek04b7e622015-01-15 10:01:51 +0100384 return 0;
385}
386
Nitin Jainb2eb59b2018-02-16 12:56:17 +0530387unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
388 char * const argv[])
389{
390 int ret = 0;
391
392 if (current_el() > 1) {
393 smp_kick_all_cpus();
394 dcache_disable();
395 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
396 ES_TO_AARCH64);
397 } else {
398 printf("FAIL: current EL is not above EL1\n");
399 ret = EINVAL;
400 }
401 return ret;
402}
403
Michal Simek8faa66a2016-02-08 09:34:53 +0100404#if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
Simon Glass2f949c32017-03-31 08:40:32 -0600405int dram_init_banksize(void)
Michal Simek8faa66a2016-02-08 09:34:53 +0100406{
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530407 int ret;
408
409 ret = fdtdec_setup_memory_banksize();
410 if (ret)
411 return ret;
412
413 mem_map_fill();
414
415 return 0;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500416}
Michal Simek8faa66a2016-02-08 09:34:53 +0100417
Tom Riniedcfdbd2016-12-09 07:56:54 -0500418int dram_init(void)
419{
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530420 if (fdtdec_setup_mem_size_base() != 0)
Nathan Rossiac04bfa2016-12-19 00:03:34 +1000421 return -EINVAL;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500422
423 return 0;
Michal Simek8faa66a2016-02-08 09:34:53 +0100424}
425#else
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530426int dram_init_banksize(void)
427{
428#if defined(CONFIG_NR_DRAM_BANKS)
429 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
430 gd->bd->bi_dram[0].size = get_effective_memsize();
431#endif
432
433 mem_map_fill();
434
435 return 0;
436}
437
Michal Simek04b7e622015-01-15 10:01:51 +0100438int dram_init(void)
439{
Michal Simek1b846212018-04-11 16:12:28 +0200440 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
441 CONFIG_SYS_SDRAM_SIZE);
Michal Simek04b7e622015-01-15 10:01:51 +0100442
443 return 0;
444}
Michal Simek8faa66a2016-02-08 09:34:53 +0100445#endif
Michal Simek04b7e622015-01-15 10:01:51 +0100446
Michal Simek04b7e622015-01-15 10:01:51 +0100447void reset_cpu(ulong addr)
448{
449}
450
Michal Simek342edfe2018-12-20 09:33:38 +0100451#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simek29b9b712018-05-17 14:06:06 +0200452static const struct {
453 u32 bit;
454 const char *name;
455} reset_reasons[] = {
456 { RESET_REASON_DEBUG_SYS, "DEBUG" },
457 { RESET_REASON_SOFT, "SOFT" },
458 { RESET_REASON_SRST, "SRST" },
459 { RESET_REASON_PSONLY, "PS-ONLY" },
460 { RESET_REASON_PMU, "PMU" },
461 { RESET_REASON_INTERNAL, "INTERNAL" },
462 { RESET_REASON_EXTERNAL, "EXTERNAL" },
463 {}
464};
465
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530466static int reset_reason(void)
Michal Simek29b9b712018-05-17 14:06:06 +0200467{
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530468 u32 reg;
469 int i, ret;
Michal Simek29b9b712018-05-17 14:06:06 +0200470 const char *reason = NULL;
471
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530472 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, &reg);
473 if (ret)
474 return -EINVAL;
Michal Simek29b9b712018-05-17 14:06:06 +0200475
476 puts("Reset reason:\t");
477
478 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530479 if (reg & reset_reasons[i].bit) {
Michal Simek29b9b712018-05-17 14:06:06 +0200480 reason = reset_reasons[i].name;
481 printf("%s ", reset_reasons[i].name);
482 break;
483 }
484 }
485
486 puts("\n");
487
488 env_set("reset_reason", reason);
489
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530490 ret = zynqmp_mmio_write(~0, ~0, (ulong)&crlapb_base->reset_reason);
491 if (ret)
492 return -EINVAL;
Michal Simek29b9b712018-05-17 14:06:06 +0200493
494 return ret;
495}
496
Michal Simek1ca66d72019-02-14 13:14:30 +0100497static int set_fdtfile(void)
498{
499 char *compatible, *fdtfile;
500 const char *suffix = ".dtb";
501 const char *vendor = "xilinx/";
502
503 if (env_get("fdtfile"))
504 return 0;
505
506 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible", NULL);
507 if (compatible) {
508 debug("Compatible: %s\n", compatible);
509
510 /* Discard vendor prefix */
511 strsep(&compatible, ",");
512
513 fdtfile = calloc(1, strlen(vendor) + strlen(compatible) +
514 strlen(suffix) + 1);
515 if (!fdtfile)
516 return -ENOMEM;
517
518 sprintf(fdtfile, "%s%s%s", vendor, compatible, suffix);
519
520 env_set("fdtfile", fdtfile);
521 free(fdtfile);
522 }
523
524 return 0;
525}
526
Michal Simek04b7e622015-01-15 10:01:51 +0100527int board_late_init(void)
528{
529 u32 reg = 0;
530 u8 bootmode;
Michal Simekf183a982018-04-25 11:20:43 +0200531 struct udevice *dev;
532 int bootseq = -1;
533 int bootseq_len = 0;
Michal Simek7410b142018-04-25 11:10:34 +0200534 int env_targets_len = 0;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200535 const char *mode;
536 char *new_targets;
Siva Durga Prasad Paladugu245c5562017-12-20 16:35:06 +0530537 char *env_targets;
Siva Durga Prasad Paladugue6fd3bb2017-02-21 17:58:28 +0530538 int ret;
T Karthik Reddy0d2e7fe2019-08-20 09:30:57 +0530539 ulong initrd_hi;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200540
Michal Simek482f5492018-10-05 08:55:16 +0200541#if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
542 usb_ether_init();
543#endif
544
Michal Simekecfb6dc2016-04-22 14:28:54 +0200545 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
546 debug("Saved variables - Skipping\n");
547 return 0;
548 }
Michal Simek04b7e622015-01-15 10:01:51 +0100549
Michal Simek1ca66d72019-02-14 13:14:30 +0100550 ret = set_fdtfile();
551 if (ret)
552 return ret;
553
Siva Durga Prasad Paladugue6fd3bb2017-02-21 17:58:28 +0530554 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, &reg);
555 if (ret)
556 return -EINVAL;
557
Michal Simek833e0c42016-10-25 11:43:02 +0200558 if (reg >> BOOT_MODE_ALT_SHIFT)
559 reg >>= BOOT_MODE_ALT_SHIFT;
560
Michal Simek04b7e622015-01-15 10:01:51 +0100561 bootmode = reg & BOOT_MODES_MASK;
562
Michal Simekc5d95232015-09-20 17:20:42 +0200563 puts("Bootmode: ");
Michal Simek04b7e622015-01-15 10:01:51 +0100564 switch (bootmode) {
Michal Simek12398ea2016-08-19 14:14:52 +0200565 case USB_MODE:
566 puts("USB_MODE\n");
567 mode = "usb";
Michal Simek43380352017-12-01 15:18:24 +0100568 env_set("modeboot", "usb_dfu_spl");
Michal Simek12398ea2016-08-19 14:14:52 +0200569 break;
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530570 case JTAG_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200571 puts("JTAG_MODE\n");
Siva Durga Prasad Paladugu9c441702019-06-25 17:41:09 +0530572 mode = "jtag pxe dhcp";
Michal Simek43380352017-12-01 15:18:24 +0100573 env_set("modeboot", "jtagboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530574 break;
575 case QSPI_MODE_24BIT:
576 case QSPI_MODE_32BIT:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200577 mode = "qspi0";
Michal Simekc5d95232015-09-20 17:20:42 +0200578 puts("QSPI_MODE\n");
Michal Simek43380352017-12-01 15:18:24 +0100579 env_set("modeboot", "qspiboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530580 break;
Michal Simek02d66cd2015-04-15 15:02:28 +0200581 case EMMC_MODE:
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200582 puts("EMMC_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200583 mode = "mmc0";
Michal Simek43380352017-12-01 15:18:24 +0100584 env_set("modeboot", "emmcboot");
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200585 break;
586 case SD_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200587 puts("SD_MODE\n");
Michal Simekf183a982018-04-25 11:20:43 +0200588 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530589 "mmc@ff160000", &dev) &&
590 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200591 "sdhci@ff160000", &dev)) {
592 puts("Boot from SD0 but without SD0 enabled!\n");
593 return -1;
594 }
595 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
596
597 mode = "mmc";
598 bootseq = dev->seq;
Michal Simek43380352017-12-01 15:18:24 +0100599 env_set("modeboot", "sdboot");
Michal Simek04b7e622015-01-15 10:01:51 +0100600 break;
Siva Durga Prasad Paladugu29a77d22016-09-21 11:45:05 +0530601 case SD1_LSHFT_MODE:
602 puts("LVL_SHFT_");
603 /* fall through */
Michal Simek108e1842015-10-05 10:51:12 +0200604 case SD_MODE1:
Michal Simekc5d95232015-09-20 17:20:42 +0200605 puts("SD_MODE1\n");
Michal Simekf183a982018-04-25 11:20:43 +0200606 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530607 "mmc@ff170000", &dev) &&
608 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200609 "sdhci@ff170000", &dev)) {
610 puts("Boot from SD1 but without SD1 enabled!\n");
611 return -1;
612 }
613 debug("mmc1 device found at %p, seq %d\n", dev, dev->seq);
614
615 mode = "mmc";
616 bootseq = dev->seq;
Michal Simek43380352017-12-01 15:18:24 +0100617 env_set("modeboot", "sdboot");
Michal Simek108e1842015-10-05 10:51:12 +0200618 break;
619 case NAND_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200620 puts("NAND_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200621 mode = "nand0";
Michal Simek43380352017-12-01 15:18:24 +0100622 env_set("modeboot", "nandboot");
Michal Simek108e1842015-10-05 10:51:12 +0200623 break;
Michal Simek04b7e622015-01-15 10:01:51 +0100624 default:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200625 mode = "";
Michal Simek04b7e622015-01-15 10:01:51 +0100626 printf("Invalid Boot Mode:0x%x\n", bootmode);
627 break;
628 }
629
Michal Simekf183a982018-04-25 11:20:43 +0200630 if (bootseq >= 0) {
631 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
632 debug("Bootseq len: %x\n", bootseq_len);
633 }
634
Michal Simekecfb6dc2016-04-22 14:28:54 +0200635 /*
636 * One terminating char + one byte for space between mode
637 * and default boot_targets
638 */
Siva Durga Prasad Paladugu245c5562017-12-20 16:35:06 +0530639 env_targets = env_get("boot_targets");
Michal Simek7410b142018-04-25 11:10:34 +0200640 if (env_targets)
641 env_targets_len = strlen(env_targets);
642
Michal Simekf183a982018-04-25 11:20:43 +0200643 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
644 bootseq_len);
Michal Simek089b84d2018-06-13 09:42:41 +0200645 if (!new_targets)
646 return -ENOMEM;
Michal Simek7410b142018-04-25 11:10:34 +0200647
Michal Simekf183a982018-04-25 11:20:43 +0200648 if (bootseq >= 0)
649 sprintf(new_targets, "%s%x %s", mode, bootseq,
650 env_targets ? env_targets : "");
651 else
652 sprintf(new_targets, "%s %s", mode,
653 env_targets ? env_targets : "");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200654
Simon Glass6a38e412017-08-03 12:22:09 -0600655 env_set("boot_targets", new_targets);
Michal Simekecfb6dc2016-04-22 14:28:54 +0200656
T Karthik Reddy0d2e7fe2019-08-20 09:30:57 +0530657 initrd_hi = gd->start_addr_sp - CONFIG_STACK_SIZE;
658 initrd_hi = round_down(initrd_hi, SZ_16M);
659 env_set_addr("initrd_high", (void *)initrd_hi);
660
Michal Simek29b9b712018-05-17 14:06:06 +0200661 reset_reason();
662
Michal Simek04b7e622015-01-15 10:01:51 +0100663 return 0;
664}
Michal Simek342edfe2018-12-20 09:33:38 +0100665#endif
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530666
667int checkboard(void)
668{
Michal Simek47ce9362016-01-25 11:04:21 +0100669 puts("Board: Xilinx ZynqMP\n");
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530670 return 0;
671}