blob: 836c28526ffb9a0aad6c3f0b27044475adbc763e [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michal Simek84c72042015-01-15 10:01:51 +01002/*
3 * (C) Copyright 2014 - 2015 Xilinx, Inc.
4 * Michal Simek <michal.simek@xilinx.com>
Michal Simek84c72042015-01-15 10:01:51 +01005 */
6
7#include <common.h>
Simon Glass62270f42019-11-14 12:57:35 -07008#include <cpu_func.h>
Simon Glass9fb625c2019-08-01 09:46:51 -06009#include <env.h>
Michal Simek679b9942015-09-30 17:26:55 +020010#include <sata.h>
Michal Simek6fe6f132015-07-23 13:27:40 +020011#include <ahci.h>
12#include <scsi.h>
Michal Simekb72894f2016-04-22 14:28:54 +020013#include <malloc.h>
Michal Simek4490e012018-04-19 15:43:38 +020014#include <wdt.h>
Michal Simek0785dfd2015-11-05 08:34:35 +010015#include <asm/arch/clk.h>
Michal Simek84c72042015-01-15 10:01:51 +010016#include <asm/arch/hardware.h>
17#include <asm/arch/sys_proto.h>
Michal Simek2ad341e2018-01-10 09:36:09 +010018#include <asm/arch/psu_init_gpl.h>
Michal Simek84c72042015-01-15 10:01:51 +010019#include <asm/io.h>
Michal Simek2882b392018-04-25 11:20:43 +020020#include <dm/device.h>
Michal Simek4490e012018-04-19 15:43:38 +020021#include <dm/uclass.h>
Siva Durga Prasad Paladugu16fa00a2015-08-04 13:03:26 +053022#include <usb.h>
23#include <dwc3-uboot.h>
Michal Simek47e60cb2016-02-01 15:05:58 +010024#include <zynqmppl.h>
Ibai Erkiaga009ab7b2019-09-27 11:37:01 +010025#include <zynqmp_firmware.h>
Michal Simek9feff382016-09-01 11:16:40 +020026#include <g_dnl.h>
T Karthik Reddya69814c2019-08-20 09:30:57 +053027#include <linux/sizes.h>
Michal Simek84c72042015-01-15 10:01:51 +010028
Luca Ceresolic28a9cf2019-05-21 18:06:43 +020029#include "pm_cfg_obj.h"
30
Michal Simek84c72042015-01-15 10:01:51 +010031DECLARE_GLOBAL_DATA_PTR;
32
Michal Simek47e60cb2016-02-01 15:05:58 +010033#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
34 !defined(CONFIG_SPL_BUILD)
35static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
36
37static const struct {
Michal Simek8ebdf9e2017-11-06 12:55:59 +010038 u32 id;
Michal Simek494fffe2017-08-22 14:58:53 +020039 u32 ver;
Michal Simek47e60cb2016-02-01 15:05:58 +010040 char *name;
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +053041 bool evexists;
Michal Simek47e60cb2016-02-01 15:05:58 +010042} zynqmp_devices[] = {
43 {
44 .id = 0x10,
45 .name = "3eg",
46 },
47 {
Michal Simek494fffe2017-08-22 14:58:53 +020048 .id = 0x10,
49 .ver = 0x2c,
50 .name = "3cg",
51 },
52 {
Michal Simek47e60cb2016-02-01 15:05:58 +010053 .id = 0x11,
54 .name = "2eg",
55 },
56 {
Michal Simek494fffe2017-08-22 14:58:53 +020057 .id = 0x11,
58 .ver = 0x2c,
59 .name = "2cg",
60 },
61 {
Michal Simek47e60cb2016-02-01 15:05:58 +010062 .id = 0x20,
63 .name = "5ev",
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +053064 .evexists = 1,
Michal Simek47e60cb2016-02-01 15:05:58 +010065 },
66 {
Michal Simek494fffe2017-08-22 14:58:53 +020067 .id = 0x20,
68 .ver = 0x100,
69 .name = "5eg",
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +053070 .evexists = 1,
Michal Simek494fffe2017-08-22 14:58:53 +020071 },
72 {
73 .id = 0x20,
74 .ver = 0x12c,
75 .name = "5cg",
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +053076 .evexists = 1,
Michal Simek494fffe2017-08-22 14:58:53 +020077 },
78 {
Michal Simek47e60cb2016-02-01 15:05:58 +010079 .id = 0x21,
80 .name = "4ev",
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +053081 .evexists = 1,
Michal Simek47e60cb2016-02-01 15:05:58 +010082 },
83 {
Michal Simek494fffe2017-08-22 14:58:53 +020084 .id = 0x21,
85 .ver = 0x100,
86 .name = "4eg",
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +053087 .evexists = 1,
Michal Simek494fffe2017-08-22 14:58:53 +020088 },
89 {
90 .id = 0x21,
91 .ver = 0x12c,
92 .name = "4cg",
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +053093 .evexists = 1,
Michal Simek494fffe2017-08-22 14:58:53 +020094 },
95 {
Michal Simek47e60cb2016-02-01 15:05:58 +010096 .id = 0x30,
97 .name = "7ev",
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +053098 .evexists = 1,
Michal Simek47e60cb2016-02-01 15:05:58 +010099 },
100 {
Michal Simek494fffe2017-08-22 14:58:53 +0200101 .id = 0x30,
102 .ver = 0x100,
103 .name = "7eg",
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530104 .evexists = 1,
Michal Simek494fffe2017-08-22 14:58:53 +0200105 },
106 {
107 .id = 0x30,
108 .ver = 0x12c,
109 .name = "7cg",
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +0530110 .evexists = 1,
Michal Simek494fffe2017-08-22 14:58:53 +0200111 },
112 {
Michal Simek47e60cb2016-02-01 15:05:58 +0100113 .id = 0x38,
114 .name = "9eg",
115 },
116 {
Michal Simek494fffe2017-08-22 14:58:53 +0200117 .id = 0x38,
118 .ver = 0x2c,
119 .name = "9cg",
120 },
121 {
Michal Simek47e60cb2016-02-01 15:05:58 +0100122 .id = 0x39,
123 .name = "6eg",
124 },
125 {
Michal Simek494fffe2017-08-22 14:58:53 +0200126 .id = 0x39,
127 .ver = 0x2c,
128 .name = "6cg",
129 },
130 {
Michal Simek47e60cb2016-02-01 15:05:58 +0100131 .id = 0x40,
132 .name = "11eg",
133 },
Michal Simek494fffe2017-08-22 14:58:53 +0200134 { /* For testing purpose only */
135 .id = 0x50,
136 .ver = 0x2c,
137 .name = "15cg",
138 },
Michal Simek47e60cb2016-02-01 15:05:58 +0100139 {
140 .id = 0x50,
141 .name = "15eg",
142 },
143 {
144 .id = 0x58,
145 .name = "19eg",
146 },
147 {
148 .id = 0x59,
149 .name = "17eg",
150 },
Michal Simekb030fed2017-06-02 08:08:59 +0200151 {
152 .id = 0x61,
153 .name = "21dr",
154 },
155 {
156 .id = 0x63,
157 .name = "23dr",
158 },
159 {
160 .id = 0x65,
161 .name = "25dr",
162 },
163 {
164 .id = 0x64,
165 .name = "27dr",
166 },
167 {
168 .id = 0x60,
169 .name = "28dr",
170 },
171 {
172 .id = 0x62,
173 .name = "29dr",
174 },
Siva Durga Prasad Paladuguc7490902019-03-23 15:00:06 +0530175 {
176 .id = 0x66,
177 .name = "39dr",
178 },
Siva Durga Prasad Paladugu134b0c82019-07-23 11:56:17 +0530179 {
180 .id = 0x7b,
181 .name = "48dr",
182 },
183 {
184 .id = 0x7e,
185 .name = "49dr",
186 },
Michal Simek47e60cb2016-02-01 15:05:58 +0100187};
Siva Durga Prasad Paladugu74ba69d2017-07-25 11:51:37 +0530188#endif
Michal Simek47e60cb2016-02-01 15:05:58 +0100189
Siva Durga Prasad Paladuguf52bf5a2017-07-25 11:51:38 +0530190int chip_id(unsigned char id)
Michal Simek47e60cb2016-02-01 15:05:58 +0100191{
192 struct pt_regs regs;
Siva Durga Prasad Paladugudb3123b2017-07-25 11:51:36 +0530193 int val = -EINVAL;
Michal Simek47e60cb2016-02-01 15:05:58 +0100194
Siva Durga Prasad Paladugu74ba69d2017-07-25 11:51:37 +0530195 if (current_el() != 3) {
196 regs.regs[0] = ZYNQMP_SIP_SVC_CSU_DMA_CHIPID;
197 regs.regs[1] = 0;
198 regs.regs[2] = 0;
199 regs.regs[3] = 0;
Michal Simek47e60cb2016-02-01 15:05:58 +0100200
Siva Durga Prasad Paladugu74ba69d2017-07-25 11:51:37 +0530201 smc_call(&regs);
202
203 /*
204 * SMC returns:
205 * regs[0][31:0] = status of the operation
206 * regs[0][63:32] = CSU.IDCODE register
207 * regs[1][31:0] = CSU.version register
Michal Simek494fffe2017-08-22 14:58:53 +0200208 * regs[1][63:32] = CSU.IDCODE2 register
Siva Durga Prasad Paladugu74ba69d2017-07-25 11:51:37 +0530209 */
210 switch (id) {
211 case IDCODE:
212 regs.regs[0] = upper_32_bits(regs.regs[0]);
213 regs.regs[0] &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
214 ZYNQMP_CSU_IDCODE_SVD_MASK;
215 regs.regs[0] >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
216 val = regs.regs[0];
217 break;
218 case VERSION:
219 regs.regs[1] = lower_32_bits(regs.regs[1]);
220 regs.regs[1] &= ZYNQMP_CSU_SILICON_VER_MASK;
221 val = regs.regs[1];
222 break;
Michal Simek494fffe2017-08-22 14:58:53 +0200223 case IDCODE2:
224 regs.regs[1] = lower_32_bits(regs.regs[1]);
225 regs.regs[1] >>= ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
226 val = regs.regs[1];
227 break;
Siva Durga Prasad Paladugu74ba69d2017-07-25 11:51:37 +0530228 default:
229 printf("%s, Invalid Req:0x%x\n", __func__, id);
230 }
231 } else {
232 switch (id) {
233 case IDCODE:
234 val = readl(ZYNQMP_CSU_IDCODE_ADDR);
235 val &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
236 ZYNQMP_CSU_IDCODE_SVD_MASK;
237 val >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
238 break;
239 case VERSION:
240 val = readl(ZYNQMP_CSU_VER_ADDR);
241 val &= ZYNQMP_CSU_SILICON_VER_MASK;
242 break;
243 default:
244 printf("%s, Invalid Req:0x%x\n", __func__, id);
245 }
Siva Durga Prasad Paladugudb3123b2017-07-25 11:51:36 +0530246 }
Soren Brinkmann0cba6ab2016-09-29 11:44:41 -0700247
Siva Durga Prasad Paladugudb3123b2017-07-25 11:51:36 +0530248 return val;
Michal Simek47e60cb2016-02-01 15:05:58 +0100249}
250
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530251#define ZYNQMP_VERSION_SIZE 9
252#define ZYNQMP_PL_STATUS_BIT 9
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +0530253#define ZYNQMP_IPDIS_VCU_BIT 8
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530254#define ZYNQMP_PL_STATUS_MASK BIT(ZYNQMP_PL_STATUS_BIT)
255#define ZYNQMP_CSU_VERSION_MASK ~(ZYNQMP_PL_STATUS_MASK)
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +0530256#define ZYNQMP_CSU_VCUDIS_VER_MASK ZYNQMP_CSU_VERSION_MASK & \
257 ~BIT(ZYNQMP_IPDIS_VCU_BIT)
258#define MAX_VARIANTS_EV 3
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530259
Siva Durga Prasad Paladugu74ba69d2017-07-25 11:51:37 +0530260#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
261 !defined(CONFIG_SPL_BUILD)
Michal Simek47e60cb2016-02-01 15:05:58 +0100262static char *zynqmp_get_silicon_idcode_name(void)
263{
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +0530264 u32 i, id, ver, j;
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530265 char *buf;
266 static char name[ZYNQMP_VERSION_SIZE];
Michal Simek47e60cb2016-02-01 15:05:58 +0100267
Siva Durga Prasad Paladugudb3123b2017-07-25 11:51:36 +0530268 id = chip_id(IDCODE);
Michal Simek494fffe2017-08-22 14:58:53 +0200269 ver = chip_id(IDCODE2);
270
Michal Simek47e60cb2016-02-01 15:05:58 +0100271 for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +0530272 if (zynqmp_devices[i].id == id) {
273 if (zynqmp_devices[i].evexists &&
274 !(ver & ZYNQMP_PL_STATUS_MASK))
275 break;
276 if (zynqmp_devices[i].ver == (ver &
277 ZYNQMP_CSU_VERSION_MASK))
278 break;
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530279 }
Michal Simek47e60cb2016-02-01 15:05:58 +0100280 }
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530281
282 if (i >= ARRAY_SIZE(zynqmp_devices))
283 return "unknown";
284
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +0530285 strncat(name, "zu", 2);
286 if (!zynqmp_devices[i].evexists ||
287 (ver & ZYNQMP_PL_STATUS_MASK)) {
288 strncat(name, zynqmp_devices[i].name,
289 ZYNQMP_VERSION_SIZE - 3);
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530290 return name;
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +0530291 }
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530292
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +0530293 /*
294 * Here we are means, PL not powered up and ev variant
295 * exists. So, we need to ignore VCU disable bit(8) in
296 * version and findout if its CG or EG/EV variant.
297 */
298 for (j = 0; j < MAX_VARIANTS_EV; j++, i++) {
299 if ((zynqmp_devices[i].ver & ~BIT(ZYNQMP_IPDIS_VCU_BIT)) ==
300 (ver & ZYNQMP_CSU_VCUDIS_VER_MASK)) {
301 strncat(name, zynqmp_devices[i].name,
302 ZYNQMP_VERSION_SIZE - 3);
303 break;
304 }
305 }
306
307 if (j >= MAX_VARIANTS_EV)
308 return "unknown";
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530309
310 if (strstr(name, "eg") || strstr(name, "ev")) {
311 buf = strstr(name, "e");
312 *buf = '\0';
313 }
314
315 return name;
Michal Simek47e60cb2016-02-01 15:05:58 +0100316}
317#endif
318
Michal Simekfb4000e2017-02-07 14:32:26 +0100319int board_early_init_f(void)
320{
Michal Simekf32e79f2018-01-10 11:48:48 +0100321 int ret = 0;
Michal Simek55de0922017-07-12 13:08:41 +0200322
Michal Simek88f05a92018-01-15 12:52:59 +0100323#if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
Michal Simekf32e79f2018-01-10 11:48:48 +0100324 ret = psu_init();
Michal Simek55de0922017-07-12 13:08:41 +0200325#endif
326
Michal Simekf32e79f2018-01-10 11:48:48 +0100327 return ret;
Michal Simekfb4000e2017-02-07 14:32:26 +0100328}
329
Michal Simek84c72042015-01-15 10:01:51 +0100330int board_init(void)
331{
Ibai Erkiaga325a22d2019-09-27 11:37:04 +0100332 struct udevice *dev;
333
334 uclass_get_device_by_name(UCLASS_FIRMWARE, "zynqmp-power", &dev);
335 if (!dev)
336 panic("PMU Firmware device not found - Enable it");
337
Luca Ceresolic28a9cf2019-05-21 18:06:43 +0200338#if defined(CONFIG_SPL_BUILD)
339 /* Check *at build time* if the filename is an non-empty string */
340 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
341 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
342 zynqmp_pm_cfg_obj_size);
343#endif
344
Michal Simeka0736ef2015-06-22 14:31:06 +0200345 printf("EL Level:\tEL%d\n", current_el());
346
Michal Simek47e60cb2016-02-01 15:05:58 +0100347#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
348 !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
349 defined(CONFIG_SPL_BUILD))
350 if (current_el() != 3) {
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530351 zynqmppl.name = zynqmp_get_silicon_idcode_name();
Michal Simek47e60cb2016-02-01 15:05:58 +0100352 printf("Chip ID:\t%s\n", zynqmppl.name);
353 fpga_init();
354 fpga_add(fpga_xilinx, &zynqmppl);
355 }
356#endif
357
Michal Simek84c72042015-01-15 10:01:51 +0100358 return 0;
359}
360
361int board_early_init_r(void)
362{
363 u32 val;
364
Siva Durga Prasad Paladuguec60a272017-12-07 15:05:30 +0530365 if (current_el() != 3)
366 return 0;
367
Michal Simek90a35db2017-07-12 10:32:18 +0200368 val = readl(&crlapb_base->timestamp_ref_ctrl);
369 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
370
Siva Durga Prasad Paladuguec60a272017-12-07 15:05:30 +0530371 if (!val) {
Michal Simek0785dfd2015-11-05 08:34:35 +0100372 val = readl(&crlapb_base->timestamp_ref_ctrl);
373 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
374 writel(val, &crlapb_base->timestamp_ref_ctrl);
Michal Simek84c72042015-01-15 10:01:51 +0100375
Michal Simek0785dfd2015-11-05 08:34:35 +0100376 /* Program freq register in System counter */
377 writel(zynqmp_get_system_timer_freq(),
378 &iou_scntr_secure->base_frequency_id_register);
379 /* And enable system counter */
380 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
381 &iou_scntr_secure->counter_control_register);
382 }
Michal Simek84c72042015-01-15 10:01:51 +0100383 return 0;
384}
385
Nitin Jain51916862018-02-16 12:56:17 +0530386unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
387 char * const argv[])
388{
389 int ret = 0;
390
391 if (current_el() > 1) {
392 smp_kick_all_cpus();
393 dcache_disable();
394 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
395 ES_TO_AARCH64);
396 } else {
397 printf("FAIL: current EL is not above EL1\n");
398 ret = EINVAL;
399 }
400 return ret;
401}
402
Michal Simek8d59d7f2016-02-08 09:34:53 +0100403#if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
Simon Glass76b00ac2017-03-31 08:40:32 -0600404int dram_init_banksize(void)
Tom Rini361a8792016-12-09 07:56:54 -0500405{
Nitin Jain06789412018-04-20 12:30:40 +0530406 int ret;
407
408 ret = fdtdec_setup_memory_banksize();
409 if (ret)
410 return ret;
411
412 mem_map_fill();
413
414 return 0;
Michal Simek8d59d7f2016-02-08 09:34:53 +0100415}
416
417int dram_init(void)
418{
Siva Durga Prasad Paladugu12308b12018-07-16 15:56:11 +0530419 if (fdtdec_setup_mem_size_base() != 0)
Nathan Rossi950f86c2016-12-19 00:03:34 +1000420 return -EINVAL;
Michal Simek8d59d7f2016-02-08 09:34:53 +0100421
422 return 0;
423}
424#else
Nitin Jain06789412018-04-20 12:30:40 +0530425int dram_init_banksize(void)
426{
427#if defined(CONFIG_NR_DRAM_BANKS)
428 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
429 gd->bd->bi_dram[0].size = get_effective_memsize();
430#endif
431
432 mem_map_fill();
433
434 return 0;
435}
436
Michal Simek84c72042015-01-15 10:01:51 +0100437int dram_init(void)
438{
Michal Simek61dc92a2018-04-11 16:12:28 +0200439 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
440 CONFIG_SYS_SDRAM_SIZE);
Michal Simek84c72042015-01-15 10:01:51 +0100441
442 return 0;
443}
Michal Simek8d59d7f2016-02-08 09:34:53 +0100444#endif
Michal Simek84c72042015-01-15 10:01:51 +0100445
Michal Simek84c72042015-01-15 10:01:51 +0100446void reset_cpu(ulong addr)
447{
448}
449
Michal Simek0bf3f9c2018-12-20 09:33:38 +0100450#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simekd348bea2018-05-17 14:06:06 +0200451static const struct {
452 u32 bit;
453 const char *name;
454} reset_reasons[] = {
455 { RESET_REASON_DEBUG_SYS, "DEBUG" },
456 { RESET_REASON_SOFT, "SOFT" },
457 { RESET_REASON_SRST, "SRST" },
458 { RESET_REASON_PSONLY, "PS-ONLY" },
459 { RESET_REASON_PMU, "PMU" },
460 { RESET_REASON_INTERNAL, "INTERNAL" },
461 { RESET_REASON_EXTERNAL, "EXTERNAL" },
462 {}
463};
464
T Karthik Reddybe523722019-03-13 20:24:18 +0530465static int reset_reason(void)
Michal Simekd348bea2018-05-17 14:06:06 +0200466{
T Karthik Reddybe523722019-03-13 20:24:18 +0530467 u32 reg;
468 int i, ret;
Michal Simekd348bea2018-05-17 14:06:06 +0200469 const char *reason = NULL;
470
T Karthik Reddybe523722019-03-13 20:24:18 +0530471 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, &reg);
472 if (ret)
473 return -EINVAL;
Michal Simekd348bea2018-05-17 14:06:06 +0200474
475 puts("Reset reason:\t");
476
477 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
T Karthik Reddybe523722019-03-13 20:24:18 +0530478 if (reg & reset_reasons[i].bit) {
Michal Simekd348bea2018-05-17 14:06:06 +0200479 reason = reset_reasons[i].name;
480 printf("%s ", reset_reasons[i].name);
481 break;
482 }
483 }
484
485 puts("\n");
486
487 env_set("reset_reason", reason);
488
T Karthik Reddybe523722019-03-13 20:24:18 +0530489 ret = zynqmp_mmio_write(~0, ~0, (ulong)&crlapb_base->reset_reason);
490 if (ret)
491 return -EINVAL;
Michal Simekd348bea2018-05-17 14:06:06 +0200492
493 return ret;
494}
495
Michal Simek91d7e0c2019-02-14 13:14:30 +0100496static int set_fdtfile(void)
497{
498 char *compatible, *fdtfile;
499 const char *suffix = ".dtb";
500 const char *vendor = "xilinx/";
501
502 if (env_get("fdtfile"))
503 return 0;
504
505 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible", NULL);
506 if (compatible) {
507 debug("Compatible: %s\n", compatible);
508
509 /* Discard vendor prefix */
510 strsep(&compatible, ",");
511
512 fdtfile = calloc(1, strlen(vendor) + strlen(compatible) +
513 strlen(suffix) + 1);
514 if (!fdtfile)
515 return -ENOMEM;
516
517 sprintf(fdtfile, "%s%s%s", vendor, compatible, suffix);
518
519 env_set("fdtfile", fdtfile);
520 free(fdtfile);
521 }
522
523 return 0;
524}
525
Michal Simek84c72042015-01-15 10:01:51 +0100526int board_late_init(void)
527{
528 u32 reg = 0;
529 u8 bootmode;
Michal Simek2882b392018-04-25 11:20:43 +0200530 struct udevice *dev;
531 int bootseq = -1;
532 int bootseq_len = 0;
Michal Simek0478b0b2018-04-25 11:10:34 +0200533 int env_targets_len = 0;
Michal Simekb72894f2016-04-22 14:28:54 +0200534 const char *mode;
535 char *new_targets;
Siva Durga Prasad Paladugu01c42d32017-12-20 16:35:06 +0530536 char *env_targets;
Siva Durga Prasad Paladugud1db89f2017-02-21 17:58:28 +0530537 int ret;
T Karthik Reddya69814c2019-08-20 09:30:57 +0530538 ulong initrd_hi;
Michal Simekb72894f2016-04-22 14:28:54 +0200539
Michal Simeke615f392018-10-05 08:55:16 +0200540#if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
541 usb_ether_init();
542#endif
543
Michal Simekb72894f2016-04-22 14:28:54 +0200544 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
545 debug("Saved variables - Skipping\n");
546 return 0;
547 }
Michal Simek84c72042015-01-15 10:01:51 +0100548
Michal Simek91d7e0c2019-02-14 13:14:30 +0100549 ret = set_fdtfile();
550 if (ret)
551 return ret;
552
Siva Durga Prasad Paladugud1db89f2017-02-21 17:58:28 +0530553 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, &reg);
554 if (ret)
555 return -EINVAL;
556
Michal Simek47359a02016-10-25 11:43:02 +0200557 if (reg >> BOOT_MODE_ALT_SHIFT)
558 reg >>= BOOT_MODE_ALT_SHIFT;
559
Michal Simek84c72042015-01-15 10:01:51 +0100560 bootmode = reg & BOOT_MODES_MASK;
561
Michal Simekfb909172015-09-20 17:20:42 +0200562 puts("Bootmode: ");
Michal Simek84c72042015-01-15 10:01:51 +0100563 switch (bootmode) {
Michal Simekd58fc122016-08-19 14:14:52 +0200564 case USB_MODE:
565 puts("USB_MODE\n");
566 mode = "usb";
Michal Simek07656ba2017-12-01 15:18:24 +0100567 env_set("modeboot", "usb_dfu_spl");
Michal Simekd58fc122016-08-19 14:14:52 +0200568 break;
Siva Durga Prasad Paladugu0a5bcc82015-03-13 11:10:26 +0530569 case JTAG_MODE:
Michal Simekfb909172015-09-20 17:20:42 +0200570 puts("JTAG_MODE\n");
Siva Durga Prasad Paladugu5d2274c2019-06-25 17:41:09 +0530571 mode = "jtag pxe dhcp";
Michal Simek07656ba2017-12-01 15:18:24 +0100572 env_set("modeboot", "jtagboot");
Siva Durga Prasad Paladugu0a5bcc82015-03-13 11:10:26 +0530573 break;
574 case QSPI_MODE_24BIT:
575 case QSPI_MODE_32BIT:
Michal Simekb72894f2016-04-22 14:28:54 +0200576 mode = "qspi0";
Michal Simekfb909172015-09-20 17:20:42 +0200577 puts("QSPI_MODE\n");
Michal Simek07656ba2017-12-01 15:18:24 +0100578 env_set("modeboot", "qspiboot");
Siva Durga Prasad Paladugu0a5bcc82015-03-13 11:10:26 +0530579 break;
Michal Simek39c56f52015-04-15 15:02:28 +0200580 case EMMC_MODE:
Michal Simek78678fe2015-10-05 15:59:38 +0200581 puts("EMMC_MODE\n");
Michal Simekb72894f2016-04-22 14:28:54 +0200582 mode = "mmc0";
Michal Simek07656ba2017-12-01 15:18:24 +0100583 env_set("modeboot", "emmcboot");
Michal Simek78678fe2015-10-05 15:59:38 +0200584 break;
585 case SD_MODE:
Michal Simekfb909172015-09-20 17:20:42 +0200586 puts("SD_MODE\n");
Michal Simek2882b392018-04-25 11:20:43 +0200587 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue7c9de62019-01-03 15:44:24 +0530588 "mmc@ff160000", &dev) &&
589 uclass_get_device_by_name(UCLASS_MMC,
Michal Simek2882b392018-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 Simek07656ba2017-12-01 15:18:24 +0100598 env_set("modeboot", "sdboot");
Michal Simek84c72042015-01-15 10:01:51 +0100599 break;
Siva Durga Prasad Paladugue1992272016-09-21 11:45:05 +0530600 case SD1_LSHFT_MODE:
601 puts("LVL_SHFT_");
602 /* fall through */
Michal Simekaf813ac2015-10-05 10:51:12 +0200603 case SD_MODE1:
Michal Simekfb909172015-09-20 17:20:42 +0200604 puts("SD_MODE1\n");
Michal Simek2882b392018-04-25 11:20:43 +0200605 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue7c9de62019-01-03 15:44:24 +0530606 "mmc@ff170000", &dev) &&
607 uclass_get_device_by_name(UCLASS_MMC,
Michal Simek2882b392018-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 Simek07656ba2017-12-01 15:18:24 +0100616 env_set("modeboot", "sdboot");
Michal Simekaf813ac2015-10-05 10:51:12 +0200617 break;
618 case NAND_MODE:
Michal Simekfb909172015-09-20 17:20:42 +0200619 puts("NAND_MODE\n");
Michal Simekb72894f2016-04-22 14:28:54 +0200620 mode = "nand0";
Michal Simek07656ba2017-12-01 15:18:24 +0100621 env_set("modeboot", "nandboot");
Michal Simekaf813ac2015-10-05 10:51:12 +0200622 break;
Michal Simek84c72042015-01-15 10:01:51 +0100623 default:
Michal Simekb72894f2016-04-22 14:28:54 +0200624 mode = "";
Michal Simek84c72042015-01-15 10:01:51 +0100625 printf("Invalid Boot Mode:0x%x\n", bootmode);
626 break;
627 }
628
Michal Simek2882b392018-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 Simekb72894f2016-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 Paladugu01c42d32017-12-20 16:35:06 +0530638 env_targets = env_get("boot_targets");
Michal Simek0478b0b2018-04-25 11:10:34 +0200639 if (env_targets)
640 env_targets_len = strlen(env_targets);
641
Michal Simek2882b392018-04-25 11:20:43 +0200642 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
643 bootseq_len);
Michal Simek1e3e68f2018-06-13 09:42:41 +0200644 if (!new_targets)
645 return -ENOMEM;
Michal Simek0478b0b2018-04-25 11:10:34 +0200646
Michal Simek2882b392018-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 Simekb72894f2016-04-22 14:28:54 +0200653
Simon Glass382bee52017-08-03 12:22:09 -0600654 env_set("boot_targets", new_targets);
Michal Simekb72894f2016-04-22 14:28:54 +0200655
T Karthik Reddya69814c2019-08-20 09:30:57 +0530656 initrd_hi = gd->start_addr_sp - CONFIG_STACK_SIZE;
657 initrd_hi = round_down(initrd_hi, SZ_16M);
658 env_set_addr("initrd_high", (void *)initrd_hi);
659
Michal Simekd348bea2018-05-17 14:06:06 +0200660 reset_reason();
661
Michal Simek84c72042015-01-15 10:01:51 +0100662 return 0;
663}
Michal Simek0bf3f9c2018-12-20 09:33:38 +0100664#endif
Siva Durga Prasad Paladugu84696ff2015-08-04 13:01:05 +0530665
666int checkboard(void)
667{
Michal Simek5af08552016-01-25 11:04:21 +0100668 puts("Board: Xilinx ZynqMP\n");
Siva Durga Prasad Paladugu84696ff2015-08-04 13:01:05 +0530669 return 0;
670}