blob: 8bdc67748ec2c55635859fc9cfefb81715d2851c [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>
Simon Glass52559322019-11-14 12:57:46 -070010#include <init.h>
Michal Simek679b9942015-09-30 17:26:55 +020011#include <sata.h>
Michal Simek6fe6f132015-07-23 13:27:40 +020012#include <ahci.h>
13#include <scsi.h>
Michal Simekb72894f2016-04-22 14:28:54 +020014#include <malloc.h>
Michal Simek4490e012018-04-19 15:43:38 +020015#include <wdt.h>
Michal Simek0785dfd2015-11-05 08:34:35 +010016#include <asm/arch/clk.h>
Michal Simek84c72042015-01-15 10:01:51 +010017#include <asm/arch/hardware.h>
18#include <asm/arch/sys_proto.h>
Michal Simek2ad341e2018-01-10 09:36:09 +010019#include <asm/arch/psu_init_gpl.h>
Michal Simek84c72042015-01-15 10:01:51 +010020#include <asm/io.h>
Michal Simek2882b392018-04-25 11:20:43 +020021#include <dm/device.h>
Michal Simek4490e012018-04-19 15:43:38 +020022#include <dm/uclass.h>
Siva Durga Prasad Paladugu16fa00a2015-08-04 13:03:26 +053023#include <usb.h>
24#include <dwc3-uboot.h>
Michal Simek47e60cb2016-02-01 15:05:58 +010025#include <zynqmppl.h>
Ibai Erkiaga009ab7b2019-09-27 11:37:01 +010026#include <zynqmp_firmware.h>
Michal Simek9feff382016-09-01 11:16:40 +020027#include <g_dnl.h>
T Karthik Reddya69814c2019-08-20 09:30:57 +053028#include <linux/sizes.h>
Michal Simek84c72042015-01-15 10:01:51 +010029
Luca Ceresolic28a9cf2019-05-21 18:06:43 +020030#include "pm_cfg_obj.h"
31
Michal Simek84c72042015-01-15 10:01:51 +010032DECLARE_GLOBAL_DATA_PTR;
33
Michal Simek47e60cb2016-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 Simek8ebdf9e2017-11-06 12:55:59 +010039 u32 id;
Michal Simek494fffe2017-08-22 14:58:53 +020040 u32 ver;
Michal Simek47e60cb2016-02-01 15:05:58 +010041 char *name;
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +053042 bool evexists;
Michal Simek47e60cb2016-02-01 15:05:58 +010043} zynqmp_devices[] = {
44 {
45 .id = 0x10,
46 .name = "3eg",
47 },
48 {
Michal Simek494fffe2017-08-22 14:58:53 +020049 .id = 0x10,
50 .ver = 0x2c,
51 .name = "3cg",
52 },
53 {
Michal Simek47e60cb2016-02-01 15:05:58 +010054 .id = 0x11,
55 .name = "2eg",
56 },
57 {
Michal Simek494fffe2017-08-22 14:58:53 +020058 .id = 0x11,
59 .ver = 0x2c,
60 .name = "2cg",
61 },
62 {
Michal Simek47e60cb2016-02-01 15:05:58 +010063 .id = 0x20,
64 .name = "5ev",
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +053065 .evexists = 1,
Michal Simek47e60cb2016-02-01 15:05:58 +010066 },
67 {
Michal Simek494fffe2017-08-22 14:58:53 +020068 .id = 0x20,
69 .ver = 0x100,
70 .name = "5eg",
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +053071 .evexists = 1,
Michal Simek494fffe2017-08-22 14:58:53 +020072 },
73 {
74 .id = 0x20,
75 .ver = 0x12c,
76 .name = "5cg",
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +053077 .evexists = 1,
Michal Simek494fffe2017-08-22 14:58:53 +020078 },
79 {
Michal Simek47e60cb2016-02-01 15:05:58 +010080 .id = 0x21,
81 .name = "4ev",
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +053082 .evexists = 1,
Michal Simek47e60cb2016-02-01 15:05:58 +010083 },
84 {
Michal Simek494fffe2017-08-22 14:58:53 +020085 .id = 0x21,
86 .ver = 0x100,
87 .name = "4eg",
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +053088 .evexists = 1,
Michal Simek494fffe2017-08-22 14:58:53 +020089 },
90 {
91 .id = 0x21,
92 .ver = 0x12c,
93 .name = "4cg",
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +053094 .evexists = 1,
Michal Simek494fffe2017-08-22 14:58:53 +020095 },
96 {
Michal Simek47e60cb2016-02-01 15:05:58 +010097 .id = 0x30,
98 .name = "7ev",
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +053099 .evexists = 1,
Michal Simek47e60cb2016-02-01 15:05:58 +0100100 },
101 {
Michal Simek494fffe2017-08-22 14:58:53 +0200102 .id = 0x30,
103 .ver = 0x100,
104 .name = "7eg",
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530105 .evexists = 1,
Michal Simek494fffe2017-08-22 14:58:53 +0200106 },
107 {
108 .id = 0x30,
109 .ver = 0x12c,
110 .name = "7cg",
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +0530111 .evexists = 1,
Michal Simek494fffe2017-08-22 14:58:53 +0200112 },
113 {
Michal Simek47e60cb2016-02-01 15:05:58 +0100114 .id = 0x38,
115 .name = "9eg",
116 },
117 {
Michal Simek494fffe2017-08-22 14:58:53 +0200118 .id = 0x38,
119 .ver = 0x2c,
120 .name = "9cg",
121 },
122 {
Michal Simek47e60cb2016-02-01 15:05:58 +0100123 .id = 0x39,
124 .name = "6eg",
125 },
126 {
Michal Simek494fffe2017-08-22 14:58:53 +0200127 .id = 0x39,
128 .ver = 0x2c,
129 .name = "6cg",
130 },
131 {
Michal Simek47e60cb2016-02-01 15:05:58 +0100132 .id = 0x40,
133 .name = "11eg",
134 },
Michal Simek494fffe2017-08-22 14:58:53 +0200135 { /* For testing purpose only */
136 .id = 0x50,
137 .ver = 0x2c,
138 .name = "15cg",
139 },
Michal Simek47e60cb2016-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 Simekb030fed2017-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 Paladuguc7490902019-03-23 15:00:06 +0530176 {
177 .id = 0x66,
178 .name = "39dr",
179 },
Siva Durga Prasad Paladugu134b0c82019-07-23 11:56:17 +0530180 {
181 .id = 0x7b,
182 .name = "48dr",
183 },
184 {
185 .id = 0x7e,
186 .name = "49dr",
187 },
Michal Simek47e60cb2016-02-01 15:05:58 +0100188};
Siva Durga Prasad Paladugu74ba69d2017-07-25 11:51:37 +0530189#endif
Michal Simek47e60cb2016-02-01 15:05:58 +0100190
Siva Durga Prasad Paladuguf52bf5a2017-07-25 11:51:38 +0530191int chip_id(unsigned char id)
Michal Simek47e60cb2016-02-01 15:05:58 +0100192{
193 struct pt_regs regs;
Siva Durga Prasad Paladugudb3123b2017-07-25 11:51:36 +0530194 int val = -EINVAL;
Michal Simek47e60cb2016-02-01 15:05:58 +0100195
Siva Durga Prasad Paladugu74ba69d2017-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 Simek47e60cb2016-02-01 15:05:58 +0100201
Siva Durga Prasad Paladugu74ba69d2017-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 Simek494fffe2017-08-22 14:58:53 +0200209 * regs[1][63:32] = CSU.IDCODE2 register
Siva Durga Prasad Paladugu74ba69d2017-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 Simek494fffe2017-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 Paladugu74ba69d2017-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 Paladugudb3123b2017-07-25 11:51:36 +0530247 }
Soren Brinkmann0cba6ab2016-09-29 11:44:41 -0700248
Siva Durga Prasad Paladugudb3123b2017-07-25 11:51:36 +0530249 return val;
Michal Simek47e60cb2016-02-01 15:05:58 +0100250}
251
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530252#define ZYNQMP_VERSION_SIZE 9
253#define ZYNQMP_PL_STATUS_BIT 9
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +0530254#define ZYNQMP_IPDIS_VCU_BIT 8
Siva Durga Prasad Paladugu83bf2ff2018-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 Paladugu5473f242018-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 Paladugu83bf2ff2018-03-02 16:20:10 +0530260
Siva Durga Prasad Paladugu74ba69d2017-07-25 11:51:37 +0530261#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
262 !defined(CONFIG_SPL_BUILD)
Michal Simek47e60cb2016-02-01 15:05:58 +0100263static char *zynqmp_get_silicon_idcode_name(void)
264{
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +0530265 u32 i, id, ver, j;
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530266 char *buf;
267 static char name[ZYNQMP_VERSION_SIZE];
Michal Simek47e60cb2016-02-01 15:05:58 +0100268
Siva Durga Prasad Paladugudb3123b2017-07-25 11:51:36 +0530269 id = chip_id(IDCODE);
Michal Simek494fffe2017-08-22 14:58:53 +0200270 ver = chip_id(IDCODE2);
271
Michal Simek47e60cb2016-02-01 15:05:58 +0100272 for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
Siva Durga Prasad Paladugu5473f242018-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 Paladugu83bf2ff2018-03-02 16:20:10 +0530280 }
Michal Simek47e60cb2016-02-01 15:05:58 +0100281 }
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530282
283 if (i >= ARRAY_SIZE(zynqmp_devices))
284 return "unknown";
285
Siva Durga Prasad Paladugu5473f242018-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 Paladugu83bf2ff2018-03-02 16:20:10 +0530291 return name;
Siva Durga Prasad Paladugu5473f242018-10-26 17:47:55 +0530292 }
Siva Durga Prasad Paladugu83bf2ff2018-03-02 16:20:10 +0530293
Siva Durga Prasad Paladugu5473f242018-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 Paladugu83bf2ff2018-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 Simek47e60cb2016-02-01 15:05:58 +0100317}
318#endif
319
Michal Simekfb4000e2017-02-07 14:32:26 +0100320int board_early_init_f(void)
321{
Michal Simekf32e79f2018-01-10 11:48:48 +0100322 int ret = 0;
Michal Simek55de0922017-07-12 13:08:41 +0200323
Michal Simek88f05a92018-01-15 12:52:59 +0100324#if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
Michal Simekf32e79f2018-01-10 11:48:48 +0100325 ret = psu_init();
Michal Simek55de0922017-07-12 13:08:41 +0200326#endif
327
Michal Simekf32e79f2018-01-10 11:48:48 +0100328 return ret;
Michal Simekfb4000e2017-02-07 14:32:26 +0100329}
330
Michal Simek84c72042015-01-15 10:01:51 +0100331int board_init(void)
332{
Ibai Erkiaga325a22d2019-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 Ceresolic28a9cf2019-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 Simeka0736ef2015-06-22 14:31:06 +0200346 printf("EL Level:\tEL%d\n", current_el());
347
Michal Simek47e60cb2016-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 Paladugu83bf2ff2018-03-02 16:20:10 +0530352 zynqmppl.name = zynqmp_get_silicon_idcode_name();
Michal Simek47e60cb2016-02-01 15:05:58 +0100353 printf("Chip ID:\t%s\n", zynqmppl.name);
354 fpga_init();
355 fpga_add(fpga_xilinx, &zynqmppl);
356 }
357#endif
358
Michal Simek84c72042015-01-15 10:01:51 +0100359 return 0;
360}
361
362int board_early_init_r(void)
363{
364 u32 val;
365
Siva Durga Prasad Paladuguec60a272017-12-07 15:05:30 +0530366 if (current_el() != 3)
367 return 0;
368
Michal Simek90a35db2017-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 Paladuguec60a272017-12-07 15:05:30 +0530372 if (!val) {
Michal Simek0785dfd2015-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 Simek84c72042015-01-15 10:01:51 +0100376
Michal Simek0785dfd2015-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 Simek84c72042015-01-15 10:01:51 +0100384 return 0;
385}
386
Nitin Jain51916862018-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 Simek8d59d7f2016-02-08 09:34:53 +0100404#if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
Simon Glass76b00ac2017-03-31 08:40:32 -0600405int dram_init_banksize(void)
Tom Rini361a8792016-12-09 07:56:54 -0500406{
Nitin Jain06789412018-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;
Michal Simek8d59d7f2016-02-08 09:34:53 +0100416}
417
418int dram_init(void)
419{
Siva Durga Prasad Paladugu12308b12018-07-16 15:56:11 +0530420 if (fdtdec_setup_mem_size_base() != 0)
Nathan Rossi950f86c2016-12-19 00:03:34 +1000421 return -EINVAL;
Michal Simek8d59d7f2016-02-08 09:34:53 +0100422
423 return 0;
424}
425#else
Nitin Jain06789412018-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 Simek84c72042015-01-15 10:01:51 +0100438int dram_init(void)
439{
Michal Simek61dc92a2018-04-11 16:12:28 +0200440 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
441 CONFIG_SYS_SDRAM_SIZE);
Michal Simek84c72042015-01-15 10:01:51 +0100442
443 return 0;
444}
Michal Simek8d59d7f2016-02-08 09:34:53 +0100445#endif
Michal Simek84c72042015-01-15 10:01:51 +0100446
Michal Simek84c72042015-01-15 10:01:51 +0100447void reset_cpu(ulong addr)
448{
449}
450
Michal Simek0bf3f9c2018-12-20 09:33:38 +0100451#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simekd348bea2018-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 Reddybe523722019-03-13 20:24:18 +0530466static int reset_reason(void)
Michal Simekd348bea2018-05-17 14:06:06 +0200467{
T Karthik Reddybe523722019-03-13 20:24:18 +0530468 u32 reg;
469 int i, ret;
Michal Simekd348bea2018-05-17 14:06:06 +0200470 const char *reason = NULL;
471
T Karthik Reddybe523722019-03-13 20:24:18 +0530472 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, &reg);
473 if (ret)
474 return -EINVAL;
Michal Simekd348bea2018-05-17 14:06:06 +0200475
476 puts("Reset reason:\t");
477
478 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
T Karthik Reddybe523722019-03-13 20:24:18 +0530479 if (reg & reset_reasons[i].bit) {
Michal Simekd348bea2018-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 Reddybe523722019-03-13 20:24:18 +0530490 ret = zynqmp_mmio_write(~0, ~0, (ulong)&crlapb_base->reset_reason);
491 if (ret)
492 return -EINVAL;
Michal Simekd348bea2018-05-17 14:06:06 +0200493
494 return ret;
495}
496
Michal Simek91d7e0c2019-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 Simek84c72042015-01-15 10:01:51 +0100527int board_late_init(void)
528{
529 u32 reg = 0;
530 u8 bootmode;
Michal Simek2882b392018-04-25 11:20:43 +0200531 struct udevice *dev;
532 int bootseq = -1;
533 int bootseq_len = 0;
Michal Simek0478b0b2018-04-25 11:10:34 +0200534 int env_targets_len = 0;
Michal Simekb72894f2016-04-22 14:28:54 +0200535 const char *mode;
536 char *new_targets;
Siva Durga Prasad Paladugu01c42d32017-12-20 16:35:06 +0530537 char *env_targets;
Siva Durga Prasad Paladugud1db89f2017-02-21 17:58:28 +0530538 int ret;
T Karthik Reddya69814c2019-08-20 09:30:57 +0530539 ulong initrd_hi;
Michal Simekb72894f2016-04-22 14:28:54 +0200540
Michal Simeke615f392018-10-05 08:55:16 +0200541#if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
542 usb_ether_init();
543#endif
544
Michal Simekb72894f2016-04-22 14:28:54 +0200545 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
546 debug("Saved variables - Skipping\n");
547 return 0;
548 }
Michal Simek84c72042015-01-15 10:01:51 +0100549
Michal Simek91d7e0c2019-02-14 13:14:30 +0100550 ret = set_fdtfile();
551 if (ret)
552 return ret;
553
Siva Durga Prasad Paladugud1db89f2017-02-21 17:58:28 +0530554 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, &reg);
555 if (ret)
556 return -EINVAL;
557
Michal Simek47359a02016-10-25 11:43:02 +0200558 if (reg >> BOOT_MODE_ALT_SHIFT)
559 reg >>= BOOT_MODE_ALT_SHIFT;
560
Michal Simek84c72042015-01-15 10:01:51 +0100561 bootmode = reg & BOOT_MODES_MASK;
562
Michal Simekfb909172015-09-20 17:20:42 +0200563 puts("Bootmode: ");
Michal Simek84c72042015-01-15 10:01:51 +0100564 switch (bootmode) {
Michal Simekd58fc122016-08-19 14:14:52 +0200565 case USB_MODE:
566 puts("USB_MODE\n");
567 mode = "usb";
Michal Simek07656ba2017-12-01 15:18:24 +0100568 env_set("modeboot", "usb_dfu_spl");
Michal Simekd58fc122016-08-19 14:14:52 +0200569 break;
Siva Durga Prasad Paladugu0a5bcc82015-03-13 11:10:26 +0530570 case JTAG_MODE:
Michal Simekfb909172015-09-20 17:20:42 +0200571 puts("JTAG_MODE\n");
Siva Durga Prasad Paladugu5d2274c2019-06-25 17:41:09 +0530572 mode = "jtag pxe dhcp";
Michal Simek07656ba2017-12-01 15:18:24 +0100573 env_set("modeboot", "jtagboot");
Siva Durga Prasad Paladugu0a5bcc82015-03-13 11:10:26 +0530574 break;
575 case QSPI_MODE_24BIT:
576 case QSPI_MODE_32BIT:
Michal Simekb72894f2016-04-22 14:28:54 +0200577 mode = "qspi0";
Michal Simekfb909172015-09-20 17:20:42 +0200578 puts("QSPI_MODE\n");
Michal Simek07656ba2017-12-01 15:18:24 +0100579 env_set("modeboot", "qspiboot");
Siva Durga Prasad Paladugu0a5bcc82015-03-13 11:10:26 +0530580 break;
Michal Simek39c56f52015-04-15 15:02:28 +0200581 case EMMC_MODE:
Michal Simek78678fe2015-10-05 15:59:38 +0200582 puts("EMMC_MODE\n");
T Karthik Reddy18be60b2019-12-17 06:41:42 -0700583 if (uclass_get_device_by_name(UCLASS_MMC,
584 "mmc@ff160000", &dev) &&
585 uclass_get_device_by_name(UCLASS_MMC,
586 "sdhci@ff160000", &dev)) {
587 puts("Boot from EMMC 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 Simek78678fe2015-10-05 15:59:38 +0200594 break;
595 case SD_MODE:
Michal Simekfb909172015-09-20 17:20:42 +0200596 puts("SD_MODE\n");
Michal Simek2882b392018-04-25 11:20:43 +0200597 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue7c9de62019-01-03 15:44:24 +0530598 "mmc@ff160000", &dev) &&
599 uclass_get_device_by_name(UCLASS_MMC,
Michal Simek2882b392018-04-25 11:20:43 +0200600 "sdhci@ff160000", &dev)) {
601 puts("Boot from SD0 but without SD0 enabled!\n");
602 return -1;
603 }
604 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
605
606 mode = "mmc";
607 bootseq = dev->seq;
Michal Simek07656ba2017-12-01 15:18:24 +0100608 env_set("modeboot", "sdboot");
Michal Simek84c72042015-01-15 10:01:51 +0100609 break;
Siva Durga Prasad Paladugue1992272016-09-21 11:45:05 +0530610 case SD1_LSHFT_MODE:
611 puts("LVL_SHFT_");
612 /* fall through */
Michal Simekaf813ac2015-10-05 10:51:12 +0200613 case SD_MODE1:
Michal Simekfb909172015-09-20 17:20:42 +0200614 puts("SD_MODE1\n");
Michal Simek2882b392018-04-25 11:20:43 +0200615 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue7c9de62019-01-03 15:44:24 +0530616 "mmc@ff170000", &dev) &&
617 uclass_get_device_by_name(UCLASS_MMC,
Michal Simek2882b392018-04-25 11:20:43 +0200618 "sdhci@ff170000", &dev)) {
619 puts("Boot from SD1 but without SD1 enabled!\n");
620 return -1;
621 }
622 debug("mmc1 device found at %p, seq %d\n", dev, dev->seq);
623
624 mode = "mmc";
625 bootseq = dev->seq;
Michal Simek07656ba2017-12-01 15:18:24 +0100626 env_set("modeboot", "sdboot");
Michal Simekaf813ac2015-10-05 10:51:12 +0200627 break;
628 case NAND_MODE:
Michal Simekfb909172015-09-20 17:20:42 +0200629 puts("NAND_MODE\n");
Michal Simekb72894f2016-04-22 14:28:54 +0200630 mode = "nand0";
Michal Simek07656ba2017-12-01 15:18:24 +0100631 env_set("modeboot", "nandboot");
Michal Simekaf813ac2015-10-05 10:51:12 +0200632 break;
Michal Simek84c72042015-01-15 10:01:51 +0100633 default:
Michal Simekb72894f2016-04-22 14:28:54 +0200634 mode = "";
Michal Simek84c72042015-01-15 10:01:51 +0100635 printf("Invalid Boot Mode:0x%x\n", bootmode);
636 break;
637 }
638
Michal Simek2882b392018-04-25 11:20:43 +0200639 if (bootseq >= 0) {
640 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
641 debug("Bootseq len: %x\n", bootseq_len);
642 }
643
Michal Simekb72894f2016-04-22 14:28:54 +0200644 /*
645 * One terminating char + one byte for space between mode
646 * and default boot_targets
647 */
Siva Durga Prasad Paladugu01c42d32017-12-20 16:35:06 +0530648 env_targets = env_get("boot_targets");
Michal Simek0478b0b2018-04-25 11:10:34 +0200649 if (env_targets)
650 env_targets_len = strlen(env_targets);
651
Michal Simek2882b392018-04-25 11:20:43 +0200652 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
653 bootseq_len);
Michal Simek1e3e68f2018-06-13 09:42:41 +0200654 if (!new_targets)
655 return -ENOMEM;
Michal Simek0478b0b2018-04-25 11:10:34 +0200656
Michal Simek2882b392018-04-25 11:20:43 +0200657 if (bootseq >= 0)
658 sprintf(new_targets, "%s%x %s", mode, bootseq,
659 env_targets ? env_targets : "");
660 else
661 sprintf(new_targets, "%s %s", mode,
662 env_targets ? env_targets : "");
Michal Simekb72894f2016-04-22 14:28:54 +0200663
Simon Glass382bee52017-08-03 12:22:09 -0600664 env_set("boot_targets", new_targets);
Michal Simekb72894f2016-04-22 14:28:54 +0200665
T Karthik Reddya69814c2019-08-20 09:30:57 +0530666 initrd_hi = gd->start_addr_sp - CONFIG_STACK_SIZE;
667 initrd_hi = round_down(initrd_hi, SZ_16M);
668 env_set_addr("initrd_high", (void *)initrd_hi);
669
T Karthik Reddy53c69de2019-12-18 03:34:41 -0700670 env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
671
Michal Simekd348bea2018-05-17 14:06:06 +0200672 reset_reason();
673
Michal Simek84c72042015-01-15 10:01:51 +0100674 return 0;
675}
Michal Simek0bf3f9c2018-12-20 09:33:38 +0100676#endif
Siva Durga Prasad Paladugu84696ff2015-08-04 13:01:05 +0530677
678int checkboard(void)
679{
Michal Simek5af08552016-01-25 11:04:21 +0100680 puts("Board: Xilinx ZynqMP\n");
Siva Durga Prasad Paladugu84696ff2015-08-04 13:01:05 +0530681 return 0;
682}