blob: 3c92b1a5825f5a422770f60b617dd56683928997 [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>
Michal Simek09a7d7d2020-01-07 09:02:52 +01009#include <debug_uart.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060010#include <env.h>
Simon Glassa7b51302019-11-14 12:57:46 -070011#include <init.h>
Michal Simekd54b1af2015-09-30 17:26:55 +020012#include <sata.h>
Michal Simekb216cc12015-07-23 13:27:40 +020013#include <ahci.h>
14#include <scsi.h>
Michal Simekecfb6dc2016-04-22 14:28:54 +020015#include <malloc.h>
Michal Simekbf0f9ca2018-04-19 15:43:38 +020016#include <wdt.h>
Michal Simekc23d3f82015-11-05 08:34:35 +010017#include <asm/arch/clk.h>
Michal Simek04b7e622015-01-15 10:01:51 +010018#include <asm/arch/hardware.h>
19#include <asm/arch/sys_proto.h>
Michal Simekf2f08642018-01-10 09:36:09 +010020#include <asm/arch/psu_init_gpl.h>
Michal Simek04b7e622015-01-15 10:01:51 +010021#include <asm/io.h>
Michal Simekf183a982018-04-25 11:20:43 +020022#include <dm/device.h>
Michal Simekbf0f9ca2018-04-19 15:43:38 +020023#include <dm/uclass.h>
Siva Durga Prasad Paladuguba1f68e2015-08-04 13:03:26 +053024#include <usb.h>
25#include <dwc3-uboot.h>
Michal Simek8111aff2016-02-01 15:05:58 +010026#include <zynqmppl.h>
Ibai Erkiagac8a3efa2019-09-27 11:37:01 +010027#include <zynqmp_firmware.h>
Michal Simek76d0a772016-09-01 11:16:40 +020028#include <g_dnl.h>
T Karthik Reddy0d2e7fe2019-08-20 09:30:57 +053029#include <linux/sizes.h>
Michal Simek04b7e622015-01-15 10:01:51 +010030
Luca Ceresoli23e65002019-05-21 18:06:43 +020031#include "pm_cfg_obj.h"
32
Michal Simek04b7e622015-01-15 10:01:51 +010033DECLARE_GLOBAL_DATA_PTR;
34
Michal Simek8111aff2016-02-01 15:05:58 +010035#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
36 !defined(CONFIG_SPL_BUILD)
37static xilinx_desc zynqmppl = XILINX_ZYNQMP_DESC;
38
39static const struct {
Michal Simek6908b862017-11-06 12:55:59 +010040 u32 id;
Michal Simek50d8cef2017-08-22 14:58:53 +020041 u32 ver;
Michal Simek8111aff2016-02-01 15:05:58 +010042 char *name;
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053043 bool evexists;
Michal Simek8111aff2016-02-01 15:05:58 +010044} zynqmp_devices[] = {
45 {
46 .id = 0x10,
47 .name = "3eg",
48 },
49 {
Michal Simek50d8cef2017-08-22 14:58:53 +020050 .id = 0x10,
51 .ver = 0x2c,
52 .name = "3cg",
53 },
54 {
Michal Simek8111aff2016-02-01 15:05:58 +010055 .id = 0x11,
56 .name = "2eg",
57 },
58 {
Michal Simek50d8cef2017-08-22 14:58:53 +020059 .id = 0x11,
60 .ver = 0x2c,
61 .name = "2cg",
62 },
63 {
Michal Simek8111aff2016-02-01 15:05:58 +010064 .id = 0x20,
65 .name = "5ev",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053066 .evexists = 1,
Michal Simek8111aff2016-02-01 15:05:58 +010067 },
68 {
Michal Simek50d8cef2017-08-22 14:58:53 +020069 .id = 0x20,
70 .ver = 0x100,
71 .name = "5eg",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053072 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +020073 },
74 {
75 .id = 0x20,
76 .ver = 0x12c,
77 .name = "5cg",
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +053078 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +020079 },
80 {
Michal Simek8111aff2016-02-01 15:05:58 +010081 .id = 0x21,
82 .name = "4ev",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053083 .evexists = 1,
Michal Simek8111aff2016-02-01 15:05:58 +010084 },
85 {
Michal Simek50d8cef2017-08-22 14:58:53 +020086 .id = 0x21,
87 .ver = 0x100,
88 .name = "4eg",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +053089 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +020090 },
91 {
92 .id = 0x21,
93 .ver = 0x12c,
94 .name = "4cg",
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +053095 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +020096 },
97 {
Michal Simek8111aff2016-02-01 15:05:58 +010098 .id = 0x30,
99 .name = "7ev",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530100 .evexists = 1,
Michal Simek8111aff2016-02-01 15:05:58 +0100101 },
102 {
Michal Simek50d8cef2017-08-22 14:58:53 +0200103 .id = 0x30,
104 .ver = 0x100,
105 .name = "7eg",
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530106 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +0200107 },
108 {
109 .id = 0x30,
110 .ver = 0x12c,
111 .name = "7cg",
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530112 .evexists = 1,
Michal Simek50d8cef2017-08-22 14:58:53 +0200113 },
114 {
Michal Simek8111aff2016-02-01 15:05:58 +0100115 .id = 0x38,
116 .name = "9eg",
117 },
118 {
Michal Simek50d8cef2017-08-22 14:58:53 +0200119 .id = 0x38,
120 .ver = 0x2c,
121 .name = "9cg",
122 },
123 {
Michal Simek8111aff2016-02-01 15:05:58 +0100124 .id = 0x39,
125 .name = "6eg",
126 },
127 {
Michal Simek50d8cef2017-08-22 14:58:53 +0200128 .id = 0x39,
129 .ver = 0x2c,
130 .name = "6cg",
131 },
132 {
Michal Simek8111aff2016-02-01 15:05:58 +0100133 .id = 0x40,
134 .name = "11eg",
135 },
Michal Simek50d8cef2017-08-22 14:58:53 +0200136 { /* For testing purpose only */
137 .id = 0x50,
138 .ver = 0x2c,
139 .name = "15cg",
140 },
Michal Simek8111aff2016-02-01 15:05:58 +0100141 {
142 .id = 0x50,
143 .name = "15eg",
144 },
145 {
146 .id = 0x58,
147 .name = "19eg",
148 },
149 {
150 .id = 0x59,
151 .name = "17eg",
152 },
Michal Simekb510e532017-06-02 08:08:59 +0200153 {
154 .id = 0x61,
155 .name = "21dr",
156 },
157 {
158 .id = 0x63,
159 .name = "23dr",
160 },
161 {
162 .id = 0x65,
163 .name = "25dr",
164 },
165 {
166 .id = 0x64,
167 .name = "27dr",
168 },
169 {
170 .id = 0x60,
171 .name = "28dr",
172 },
173 {
174 .id = 0x62,
175 .name = "29dr",
176 },
Siva Durga Prasad Paladugu70866b42019-03-23 15:00:06 +0530177 {
178 .id = 0x66,
179 .name = "39dr",
180 },
Siva Durga Prasad Paladugu85f61a82019-07-23 11:56:17 +0530181 {
182 .id = 0x7b,
183 .name = "48dr",
184 },
185 {
186 .id = 0x7e,
187 .name = "49dr",
188 },
Michal Simek8111aff2016-02-01 15:05:58 +0100189};
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530190#endif
Michal Simek8111aff2016-02-01 15:05:58 +0100191
Siva Durga Prasad Paladugucd35d522017-07-25 11:51:38 +0530192int chip_id(unsigned char id)
Michal Simek8111aff2016-02-01 15:05:58 +0100193{
194 struct pt_regs regs;
Siva Durga Prasad Paladugu9f0a8e92017-07-25 11:51:36 +0530195 int val = -EINVAL;
Michal Simek8111aff2016-02-01 15:05:58 +0100196
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530197 if (current_el() != 3) {
198 regs.regs[0] = ZYNQMP_SIP_SVC_CSU_DMA_CHIPID;
199 regs.regs[1] = 0;
200 regs.regs[2] = 0;
201 regs.regs[3] = 0;
Michal Simek8111aff2016-02-01 15:05:58 +0100202
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530203 smc_call(&regs);
204
205 /*
206 * SMC returns:
207 * regs[0][31:0] = status of the operation
208 * regs[0][63:32] = CSU.IDCODE register
209 * regs[1][31:0] = CSU.version register
Michal Simek50d8cef2017-08-22 14:58:53 +0200210 * regs[1][63:32] = CSU.IDCODE2 register
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530211 */
212 switch (id) {
213 case IDCODE:
214 regs.regs[0] = upper_32_bits(regs.regs[0]);
215 regs.regs[0] &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
216 ZYNQMP_CSU_IDCODE_SVD_MASK;
217 regs.regs[0] >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
218 val = regs.regs[0];
219 break;
220 case VERSION:
221 regs.regs[1] = lower_32_bits(regs.regs[1]);
222 regs.regs[1] &= ZYNQMP_CSU_SILICON_VER_MASK;
223 val = regs.regs[1];
224 break;
Michal Simek50d8cef2017-08-22 14:58:53 +0200225 case IDCODE2:
226 regs.regs[1] = lower_32_bits(regs.regs[1]);
227 regs.regs[1] >>= ZYNQMP_CSU_VERSION_EMPTY_SHIFT;
228 val = regs.regs[1];
229 break;
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530230 default:
231 printf("%s, Invalid Req:0x%x\n", __func__, id);
232 }
233 } else {
234 switch (id) {
235 case IDCODE:
236 val = readl(ZYNQMP_CSU_IDCODE_ADDR);
237 val &= ZYNQMP_CSU_IDCODE_DEVICE_CODE_MASK |
238 ZYNQMP_CSU_IDCODE_SVD_MASK;
239 val >>= ZYNQMP_CSU_IDCODE_SVD_SHIFT;
240 break;
241 case VERSION:
242 val = readl(ZYNQMP_CSU_VER_ADDR);
243 val &= ZYNQMP_CSU_SILICON_VER_MASK;
244 break;
245 default:
246 printf("%s, Invalid Req:0x%x\n", __func__, id);
247 }
Siva Durga Prasad Paladugu9f0a8e92017-07-25 11:51:36 +0530248 }
Soren Brinkmannd7696a52016-09-29 11:44:41 -0700249
Siva Durga Prasad Paladugu9f0a8e92017-07-25 11:51:36 +0530250 return val;
Michal Simek8111aff2016-02-01 15:05:58 +0100251}
252
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530253#define ZYNQMP_VERSION_SIZE 9
254#define ZYNQMP_PL_STATUS_BIT 9
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530255#define ZYNQMP_IPDIS_VCU_BIT 8
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530256#define ZYNQMP_PL_STATUS_MASK BIT(ZYNQMP_PL_STATUS_BIT)
257#define ZYNQMP_CSU_VERSION_MASK ~(ZYNQMP_PL_STATUS_MASK)
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530258#define ZYNQMP_CSU_VCUDIS_VER_MASK ZYNQMP_CSU_VERSION_MASK & \
259 ~BIT(ZYNQMP_IPDIS_VCU_BIT)
260#define MAX_VARIANTS_EV 3
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530261
Siva Durga Prasad Paladugu8d526532017-07-25 11:51:37 +0530262#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
263 !defined(CONFIG_SPL_BUILD)
Michal Simek8111aff2016-02-01 15:05:58 +0100264static char *zynqmp_get_silicon_idcode_name(void)
265{
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530266 u32 i, id, ver, j;
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530267 char *buf;
268 static char name[ZYNQMP_VERSION_SIZE];
Michal Simek8111aff2016-02-01 15:05:58 +0100269
Siva Durga Prasad Paladugu9f0a8e92017-07-25 11:51:36 +0530270 id = chip_id(IDCODE);
Michal Simek50d8cef2017-08-22 14:58:53 +0200271 ver = chip_id(IDCODE2);
272
Michal Simek8111aff2016-02-01 15:05:58 +0100273 for (i = 0; i < ARRAY_SIZE(zynqmp_devices); i++) {
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530274 if (zynqmp_devices[i].id == id) {
275 if (zynqmp_devices[i].evexists &&
276 !(ver & ZYNQMP_PL_STATUS_MASK))
277 break;
278 if (zynqmp_devices[i].ver == (ver &
279 ZYNQMP_CSU_VERSION_MASK))
280 break;
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530281 }
Michal Simek8111aff2016-02-01 15:05:58 +0100282 }
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530283
284 if (i >= ARRAY_SIZE(zynqmp_devices))
285 return "unknown";
286
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530287 strncat(name, "zu", 2);
288 if (!zynqmp_devices[i].evexists ||
289 (ver & ZYNQMP_PL_STATUS_MASK)) {
290 strncat(name, zynqmp_devices[i].name,
291 ZYNQMP_VERSION_SIZE - 3);
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530292 return name;
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530293 }
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530294
Siva Durga Prasad Paladugu951c0192018-10-26 17:47:55 +0530295 /*
296 * Here we are means, PL not powered up and ev variant
297 * exists. So, we need to ignore VCU disable bit(8) in
298 * version and findout if its CG or EG/EV variant.
299 */
300 for (j = 0; j < MAX_VARIANTS_EV; j++, i++) {
301 if ((zynqmp_devices[i].ver & ~BIT(ZYNQMP_IPDIS_VCU_BIT)) ==
302 (ver & ZYNQMP_CSU_VCUDIS_VER_MASK)) {
303 strncat(name, zynqmp_devices[i].name,
304 ZYNQMP_VERSION_SIZE - 3);
305 break;
306 }
307 }
308
309 if (j >= MAX_VARIANTS_EV)
310 return "unknown";
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530311
312 if (strstr(name, "eg") || strstr(name, "ev")) {
313 buf = strstr(name, "e");
314 *buf = '\0';
315 }
316
317 return name;
Michal Simek8111aff2016-02-01 15:05:58 +0100318}
319#endif
320
Michal Simek8b353302017-02-07 14:32:26 +0100321int board_early_init_f(void)
322{
Michal Simek1a1ab5a2018-01-15 12:52:59 +0100323#if defined(CONFIG_ZYNQMP_PSU_INIT_ENABLED)
Michal Simek09a7d7d2020-01-07 09:02:52 +0100324 int ret;
325
Michal Simekc8785f22018-01-10 11:48:48 +0100326 ret = psu_init();
Michal Simek09a7d7d2020-01-07 09:02:52 +0100327 if (ret)
328 return ret;
Michal Simek1f55e572020-03-20 08:59:02 +0100329
330 /* Delay is required for clocks to be propagated */
331 udelay(1000000);
Michal Simeke0f36102017-07-12 13:08:41 +0200332#endif
333
Michal Simek09a7d7d2020-01-07 09:02:52 +0100334#ifdef CONFIG_DEBUG_UART
335 /* Uart debug for sure */
336 debug_uart_init();
337 puts("Debug uart enabled\n"); /* or printch() */
338#endif
339
340 return 0;
Michal Simek8b353302017-02-07 14:32:26 +0100341}
342
Michal Simek46900462020-02-11 12:43:14 +0100343static int multi_boot(void)
344{
345 u32 multiboot;
346
347 multiboot = readl(&csu_base->multi_boot);
348
349 printf("Multiboot:\t%x\n", multiboot);
350
351 return 0;
352}
353
Michal Simek04b7e622015-01-15 10:01:51 +0100354int board_init(void)
355{
Michal Simek826d7eca2020-03-04 08:48:16 +0100356#if defined(CONFIG_ZYNQMP_FIRMWARE)
Ibai Erkiaga2d9b95b2019-09-27 11:37:04 +0100357 struct udevice *dev;
358
359 uclass_get_device_by_name(UCLASS_FIRMWARE, "zynqmp-power", &dev);
360 if (!dev)
361 panic("PMU Firmware device not found - Enable it");
Michal Simek826d7eca2020-03-04 08:48:16 +0100362#endif
Ibai Erkiaga2d9b95b2019-09-27 11:37:04 +0100363
Luca Ceresoli23e65002019-05-21 18:06:43 +0200364#if defined(CONFIG_SPL_BUILD)
365 /* Check *at build time* if the filename is an non-empty string */
366 if (sizeof(CONFIG_ZYNQMP_SPL_PM_CFG_OBJ_FILE) > 1)
367 zynqmp_pmufw_load_config_object(zynqmp_pm_cfg_obj,
368 zynqmp_pm_cfg_obj_size);
369#endif
370
Michal Simekfb7242d2015-06-22 14:31:06 +0200371 printf("EL Level:\tEL%d\n", current_el());
372
Michal Simek8111aff2016-02-01 15:05:58 +0100373#if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_ZYNQMPPL) && \
374 !defined(CONFIG_SPL_BUILD) || (defined(CONFIG_SPL_FPGA_SUPPORT) && \
375 defined(CONFIG_SPL_BUILD))
376 if (current_el() != 3) {
Siva Durga Prasad Paladuguba2622d2018-03-02 16:20:10 +0530377 zynqmppl.name = zynqmp_get_silicon_idcode_name();
Michal Simek8111aff2016-02-01 15:05:58 +0100378 printf("Chip ID:\t%s\n", zynqmppl.name);
379 fpga_init();
380 fpga_add(fpga_xilinx, &zynqmppl);
381 }
Michal Simekbf0f9ca2018-04-19 15:43:38 +0200382#endif
383
Michal Simek46900462020-02-11 12:43:14 +0100384 if (current_el() == 3)
385 multi_boot();
386
Michal Simek04b7e622015-01-15 10:01:51 +0100387 return 0;
388}
389
390int board_early_init_r(void)
391{
392 u32 val;
393
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530394 if (current_el() != 3)
395 return 0;
396
Michal Simek245d5282017-07-12 10:32:18 +0200397 val = readl(&crlapb_base->timestamp_ref_ctrl);
398 val &= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
399
Siva Durga Prasad Paladugu64d90002017-12-07 15:05:30 +0530400 if (!val) {
Michal Simekc23d3f82015-11-05 08:34:35 +0100401 val = readl(&crlapb_base->timestamp_ref_ctrl);
402 val |= ZYNQMP_CRL_APB_TIMESTAMP_REF_CTRL_CLKACT;
403 writel(val, &crlapb_base->timestamp_ref_ctrl);
Michal Simek04b7e622015-01-15 10:01:51 +0100404
Michal Simekc23d3f82015-11-05 08:34:35 +0100405 /* Program freq register in System counter */
406 writel(zynqmp_get_system_timer_freq(),
407 &iou_scntr_secure->base_frequency_id_register);
408 /* And enable system counter */
409 writel(ZYNQMP_IOU_SCNTR_COUNTER_CONTROL_REGISTER_EN,
410 &iou_scntr_secure->counter_control_register);
411 }
Michal Simek04b7e622015-01-15 10:01:51 +0100412 return 0;
413}
414
Nitin Jainb2eb59b2018-02-16 12:56:17 +0530415unsigned long do_go_exec(ulong (*entry)(int, char * const []), int argc,
416 char * const argv[])
417{
418 int ret = 0;
419
420 if (current_el() > 1) {
421 smp_kick_all_cpus();
422 dcache_disable();
423 armv8_switch_to_el1(0x0, 0, 0, 0, (unsigned long)entry,
424 ES_TO_AARCH64);
425 } else {
426 printf("FAIL: current EL is not above EL1\n");
427 ret = EINVAL;
428 }
429 return ret;
430}
431
Michal Simek8faa66a2016-02-08 09:34:53 +0100432#if !defined(CONFIG_SYS_SDRAM_BASE) && !defined(CONFIG_SYS_SDRAM_SIZE)
Simon Glass2f949c32017-03-31 08:40:32 -0600433int dram_init_banksize(void)
Michal Simek8faa66a2016-02-08 09:34:53 +0100434{
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530435 int ret;
436
437 ret = fdtdec_setup_memory_banksize();
438 if (ret)
439 return ret;
440
441 mem_map_fill();
442
443 return 0;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500444}
Michal Simek8faa66a2016-02-08 09:34:53 +0100445
Tom Riniedcfdbd2016-12-09 07:56:54 -0500446int dram_init(void)
447{
Siva Durga Prasad Paladugub3d55ea2018-07-16 15:56:11 +0530448 if (fdtdec_setup_mem_size_base() != 0)
Nathan Rossiac04bfa2016-12-19 00:03:34 +1000449 return -EINVAL;
Tom Riniedcfdbd2016-12-09 07:56:54 -0500450
451 return 0;
Michal Simek8faa66a2016-02-08 09:34:53 +0100452}
453#else
Nitin Jain9bcc76f2018-04-20 12:30:40 +0530454int dram_init_banksize(void)
455{
456#if defined(CONFIG_NR_DRAM_BANKS)
457 gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
458 gd->bd->bi_dram[0].size = get_effective_memsize();
459#endif
460
461 mem_map_fill();
462
463 return 0;
464}
465
Michal Simek04b7e622015-01-15 10:01:51 +0100466int dram_init(void)
467{
Michal Simek1b846212018-04-11 16:12:28 +0200468 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
469 CONFIG_SYS_SDRAM_SIZE);
Michal Simek04b7e622015-01-15 10:01:51 +0100470
471 return 0;
472}
Michal Simek8faa66a2016-02-08 09:34:53 +0100473#endif
Michal Simek04b7e622015-01-15 10:01:51 +0100474
Michal Simek04b7e622015-01-15 10:01:51 +0100475void reset_cpu(ulong addr)
476{
477}
478
Michal Simek342edfe2018-12-20 09:33:38 +0100479#if defined(CONFIG_BOARD_LATE_INIT)
Michal Simek29b9b712018-05-17 14:06:06 +0200480static const struct {
481 u32 bit;
482 const char *name;
483} reset_reasons[] = {
484 { RESET_REASON_DEBUG_SYS, "DEBUG" },
485 { RESET_REASON_SOFT, "SOFT" },
486 { RESET_REASON_SRST, "SRST" },
487 { RESET_REASON_PSONLY, "PS-ONLY" },
488 { RESET_REASON_PMU, "PMU" },
489 { RESET_REASON_INTERNAL, "INTERNAL" },
490 { RESET_REASON_EXTERNAL, "EXTERNAL" },
491 {}
492};
493
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530494static int reset_reason(void)
Michal Simek29b9b712018-05-17 14:06:06 +0200495{
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530496 u32 reg;
497 int i, ret;
Michal Simek29b9b712018-05-17 14:06:06 +0200498 const char *reason = NULL;
499
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530500 ret = zynqmp_mmio_read((ulong)&crlapb_base->reset_reason, &reg);
501 if (ret)
502 return -EINVAL;
Michal Simek29b9b712018-05-17 14:06:06 +0200503
504 puts("Reset reason:\t");
505
506 for (i = 0; i < ARRAY_SIZE(reset_reasons); i++) {
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530507 if (reg & reset_reasons[i].bit) {
Michal Simek29b9b712018-05-17 14:06:06 +0200508 reason = reset_reasons[i].name;
509 printf("%s ", reset_reasons[i].name);
510 break;
511 }
512 }
513
514 puts("\n");
515
516 env_set("reset_reason", reason);
517
Michal Simek4c4efde2020-03-23 14:02:01 +0100518 ret = zynqmp_mmio_write((ulong)&crlapb_base->reset_reason, ~0, ~0);
T Karthik Reddy09b6def2019-03-13 20:24:18 +0530519 if (ret)
520 return -EINVAL;
Michal Simek29b9b712018-05-17 14:06:06 +0200521
522 return ret;
523}
524
Michal Simek1ca66d72019-02-14 13:14:30 +0100525static int set_fdtfile(void)
526{
527 char *compatible, *fdtfile;
528 const char *suffix = ".dtb";
529 const char *vendor = "xilinx/";
530
531 if (env_get("fdtfile"))
532 return 0;
533
534 compatible = (char *)fdt_getprop(gd->fdt_blob, 0, "compatible", NULL);
535 if (compatible) {
536 debug("Compatible: %s\n", compatible);
537
538 /* Discard vendor prefix */
539 strsep(&compatible, ",");
540
541 fdtfile = calloc(1, strlen(vendor) + strlen(compatible) +
542 strlen(suffix) + 1);
543 if (!fdtfile)
544 return -ENOMEM;
545
546 sprintf(fdtfile, "%s%s%s", vendor, compatible, suffix);
547
548 env_set("fdtfile", fdtfile);
549 free(fdtfile);
550 }
551
552 return 0;
553}
554
Michal Simek04b7e622015-01-15 10:01:51 +0100555int board_late_init(void)
556{
557 u32 reg = 0;
558 u8 bootmode;
Michal Simekf183a982018-04-25 11:20:43 +0200559 struct udevice *dev;
560 int bootseq = -1;
561 int bootseq_len = 0;
Michal Simek7410b142018-04-25 11:10:34 +0200562 int env_targets_len = 0;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200563 const char *mode;
564 char *new_targets;
Siva Durga Prasad Paladugu245c5562017-12-20 16:35:06 +0530565 char *env_targets;
Siva Durga Prasad Paladugue6fd3bb2017-02-21 17:58:28 +0530566 int ret;
T Karthik Reddy0d2e7fe2019-08-20 09:30:57 +0530567 ulong initrd_hi;
Michal Simekecfb6dc2016-04-22 14:28:54 +0200568
Michal Simek482f5492018-10-05 08:55:16 +0200569#if defined(CONFIG_USB_ETHER) && !defined(CONFIG_USB_GADGET_DOWNLOAD)
570 usb_ether_init();
571#endif
572
Michal Simekecfb6dc2016-04-22 14:28:54 +0200573 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
574 debug("Saved variables - Skipping\n");
575 return 0;
576 }
Michal Simek04b7e622015-01-15 10:01:51 +0100577
Michal Simek1ca66d72019-02-14 13:14:30 +0100578 ret = set_fdtfile();
579 if (ret)
580 return ret;
581
Siva Durga Prasad Paladugue6fd3bb2017-02-21 17:58:28 +0530582 ret = zynqmp_mmio_read((ulong)&crlapb_base->boot_mode, &reg);
583 if (ret)
584 return -EINVAL;
585
Michal Simek833e0c42016-10-25 11:43:02 +0200586 if (reg >> BOOT_MODE_ALT_SHIFT)
587 reg >>= BOOT_MODE_ALT_SHIFT;
588
Michal Simek04b7e622015-01-15 10:01:51 +0100589 bootmode = reg & BOOT_MODES_MASK;
590
Michal Simekc5d95232015-09-20 17:20:42 +0200591 puts("Bootmode: ");
Michal Simek04b7e622015-01-15 10:01:51 +0100592 switch (bootmode) {
Michal Simek12398ea2016-08-19 14:14:52 +0200593 case USB_MODE:
594 puts("USB_MODE\n");
595 mode = "usb";
Michal Simek43380352017-12-01 15:18:24 +0100596 env_set("modeboot", "usb_dfu_spl");
Michal Simek12398ea2016-08-19 14:14:52 +0200597 break;
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530598 case JTAG_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200599 puts("JTAG_MODE\n");
Siva Durga Prasad Paladugu9c441702019-06-25 17:41:09 +0530600 mode = "jtag pxe dhcp";
Michal Simek43380352017-12-01 15:18:24 +0100601 env_set("modeboot", "jtagboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530602 break;
603 case QSPI_MODE_24BIT:
604 case QSPI_MODE_32BIT:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200605 mode = "qspi0";
Michal Simekc5d95232015-09-20 17:20:42 +0200606 puts("QSPI_MODE\n");
Michal Simek43380352017-12-01 15:18:24 +0100607 env_set("modeboot", "qspiboot");
Siva Durga Prasad Paladugu30f0fc72015-03-13 11:10:26 +0530608 break;
Michal Simek02d66cd2015-04-15 15:02:28 +0200609 case EMMC_MODE:
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200610 puts("EMMC_MODE\n");
T Karthik Reddy19735c32019-12-17 06:41:42 -0700611 if (uclass_get_device_by_name(UCLASS_MMC,
612 "mmc@ff160000", &dev) &&
613 uclass_get_device_by_name(UCLASS_MMC,
614 "sdhci@ff160000", &dev)) {
615 puts("Boot from EMMC but without SD0 enabled!\n");
616 return -1;
617 }
618 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
619
620 mode = "mmc";
621 bootseq = dev->seq;
Michal Simekdf7ff0a2015-10-05 15:59:38 +0200622 break;
623 case SD_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200624 puts("SD_MODE\n");
Michal Simekf183a982018-04-25 11:20:43 +0200625 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530626 "mmc@ff160000", &dev) &&
627 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200628 "sdhci@ff160000", &dev)) {
629 puts("Boot from SD0 but without SD0 enabled!\n");
630 return -1;
631 }
632 debug("mmc0 device found at %p, seq %d\n", dev, dev->seq);
633
634 mode = "mmc";
635 bootseq = dev->seq;
Michal Simek43380352017-12-01 15:18:24 +0100636 env_set("modeboot", "sdboot");
Michal Simek04b7e622015-01-15 10:01:51 +0100637 break;
Siva Durga Prasad Paladugu29a77d22016-09-21 11:45:05 +0530638 case SD1_LSHFT_MODE:
639 puts("LVL_SHFT_");
640 /* fall through */
Michal Simek108e1842015-10-05 10:51:12 +0200641 case SD_MODE1:
Michal Simekc5d95232015-09-20 17:20:42 +0200642 puts("SD_MODE1\n");
Michal Simekf183a982018-04-25 11:20:43 +0200643 if (uclass_get_device_by_name(UCLASS_MMC,
Siva Durga Prasad Paladugue91778d2019-01-03 15:44:24 +0530644 "mmc@ff170000", &dev) &&
645 uclass_get_device_by_name(UCLASS_MMC,
Michal Simekf183a982018-04-25 11:20:43 +0200646 "sdhci@ff170000", &dev)) {
647 puts("Boot from SD1 but without SD1 enabled!\n");
648 return -1;
649 }
650 debug("mmc1 device found at %p, seq %d\n", dev, dev->seq);
651
652 mode = "mmc";
653 bootseq = dev->seq;
Michal Simek43380352017-12-01 15:18:24 +0100654 env_set("modeboot", "sdboot");
Michal Simek108e1842015-10-05 10:51:12 +0200655 break;
656 case NAND_MODE:
Michal Simekc5d95232015-09-20 17:20:42 +0200657 puts("NAND_MODE\n");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200658 mode = "nand0";
Michal Simek43380352017-12-01 15:18:24 +0100659 env_set("modeboot", "nandboot");
Michal Simek108e1842015-10-05 10:51:12 +0200660 break;
Michal Simek04b7e622015-01-15 10:01:51 +0100661 default:
Michal Simekecfb6dc2016-04-22 14:28:54 +0200662 mode = "";
Michal Simek04b7e622015-01-15 10:01:51 +0100663 printf("Invalid Boot Mode:0x%x\n", bootmode);
664 break;
665 }
666
Michal Simekf183a982018-04-25 11:20:43 +0200667 if (bootseq >= 0) {
668 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
669 debug("Bootseq len: %x\n", bootseq_len);
670 }
671
Michal Simekecfb6dc2016-04-22 14:28:54 +0200672 /*
673 * One terminating char + one byte for space between mode
674 * and default boot_targets
675 */
Siva Durga Prasad Paladugu245c5562017-12-20 16:35:06 +0530676 env_targets = env_get("boot_targets");
Michal Simek7410b142018-04-25 11:10:34 +0200677 if (env_targets)
678 env_targets_len = strlen(env_targets);
679
Michal Simekf183a982018-04-25 11:20:43 +0200680 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
681 bootseq_len);
Michal Simek089b84d2018-06-13 09:42:41 +0200682 if (!new_targets)
683 return -ENOMEM;
Michal Simek7410b142018-04-25 11:10:34 +0200684
Michal Simekf183a982018-04-25 11:20:43 +0200685 if (bootseq >= 0)
686 sprintf(new_targets, "%s%x %s", mode, bootseq,
687 env_targets ? env_targets : "");
688 else
689 sprintf(new_targets, "%s %s", mode,
690 env_targets ? env_targets : "");
Michal Simekecfb6dc2016-04-22 14:28:54 +0200691
Simon Glass6a38e412017-08-03 12:22:09 -0600692 env_set("boot_targets", new_targets);
Michal Simekecfb6dc2016-04-22 14:28:54 +0200693
T Karthik Reddy0d2e7fe2019-08-20 09:30:57 +0530694 initrd_hi = gd->start_addr_sp - CONFIG_STACK_SIZE;
695 initrd_hi = round_down(initrd_hi, SZ_16M);
696 env_set_addr("initrd_high", (void *)initrd_hi);
697
T Karthik Reddy5fa6c1e2019-12-18 03:34:41 -0700698 env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET);
699
Michal Simek29b9b712018-05-17 14:06:06 +0200700 reset_reason();
701
Michal Simek04b7e622015-01-15 10:01:51 +0100702 return 0;
703}
Michal Simek342edfe2018-12-20 09:33:38 +0100704#endif
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530705
706int checkboard(void)
707{
Michal Simek47ce9362016-01-25 11:04:21 +0100708 puts("Board: Xilinx ZynqMP\n");
Siva Durga Prasad Paladugu650e0a32015-08-04 13:01:05 +0530709 return 0;
710}