blob: 4d5913cff1d1f2d453d2ad4300013dd4d501bb72 [file] [log] [blame]
Michal Simek2e53eb22022-09-19 14:21:02 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2021 - 2022, Xilinx, Inc.
4 * Copyright (C) 2022, Advanced Micro Devices, Inc.
5 *
6 * Michal Simek <michal.simek@amd.com>
7 */
8
Michal Simek2e53eb22022-09-19 14:21:02 +02009#include <cpu_func.h>
10#include <fdtdec.h>
11#include <init.h>
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -060012#include <env_internal.h>
Michal Simek2e53eb22022-09-19 14:21:02 +020013#include <log.h>
14#include <malloc.h>
Prasad Kummari2301cac2024-09-05 17:27:59 +053015#include <spi.h>
Michal Simek2e53eb22022-09-19 14:21:02 +020016#include <time.h>
17#include <asm/cache.h>
18#include <asm/global_data.h>
19#include <asm/io.h>
20#include <asm/arch/hardware.h>
21#include <asm/arch/sys_proto.h>
22#include <dm/device.h>
23#include <dm/uclass.h>
24#include "../common/board.h"
25
26#include <linux/bitfield.h>
27#include <debug_uart.h>
28#include <generated/dt.h>
29
30DECLARE_GLOBAL_DATA_PTR;
31
32int board_init(void)
33{
34 printf("EL Level:\tEL%d\n", current_el());
35
36 return 0;
37}
38
39static u32 platform_id, platform_version;
40
41char *soc_name_decode(void)
42{
43 char *name, *platform_name;
44
45 switch (platform_id) {
46 case VERSAL_NET_SPP:
47 platform_name = "ipp";
48 break;
49 case VERSAL_NET_EMU:
50 platform_name = "emu";
51 break;
52 case VERSAL_NET_QEMU:
53 platform_name = "qemu";
54 break;
55 default:
56 return NULL;
57 }
58
59 /*
60 * --rev. are 6 chars
61 * max platform name is qemu which is 4 chars
62 * platform version number are 1+1
63 * Plus 1 char for \n
64 */
65 name = calloc(1, strlen(CONFIG_SYS_BOARD) + 13);
66 if (!name)
67 return NULL;
68
69 sprintf(name, "%s-%s-rev%d.%d", CONFIG_SYS_BOARD,
70 platform_name, platform_version / 10,
71 platform_version % 10);
72
73 return name;
74}
75
76bool soc_detection(void)
77{
Michal Simekba73d6e2023-05-17 10:21:32 +020078 u32 version, ps_version;
Michal Simek2e53eb22022-09-19 14:21:02 +020079
80 version = readl(PMC_TAP_VERSION);
81 platform_id = FIELD_GET(PLATFORM_MASK, version);
Michal Simekba73d6e2023-05-17 10:21:32 +020082 ps_version = FIELD_GET(PS_VERSION_MASK, version);
Michal Simek2e53eb22022-09-19 14:21:02 +020083
84 debug("idcode %x, version %x, usercode %x\n",
85 readl(PMC_TAP_IDCODE), version,
86 readl(PMC_TAP_USERCODE));
87
Michal Simekba73d6e2023-05-17 10:21:32 +020088 debug("pmc_ver %lx, ps version %x, rtl version %lx\n",
Michal Simek2e53eb22022-09-19 14:21:02 +020089 FIELD_GET(PMC_VERSION_MASK, version),
Michal Simekba73d6e2023-05-17 10:21:32 +020090 ps_version,
Michal Simek2e53eb22022-09-19 14:21:02 +020091 FIELD_GET(RTL_VERSION_MASK, version));
92
93 platform_version = FIELD_GET(PLATFORM_VERSION_MASK, version);
94
95 if (platform_id == VERSAL_NET_SPP ||
96 platform_id == VERSAL_NET_EMU) {
Michal Simekba73d6e2023-05-17 10:21:32 +020097 if (ps_version == PS_VERSION_PRODUCTION) {
98 /*
99 * ES1 version ends at 1.9 version where there was +9
100 * used because of IPP/SPP conversion. Production
101 * version have platform_version started from 0 again
102 * that's why adding +20 to continue with the same line.
103 * It means the last ES1 version ends at 1.9 version and
104 * new PRODUCTION line starts at 2.0.
105 */
106 platform_version += 20;
107 } else {
108 /*
109 * 9 is diff for
110 * 0 means 0.9 version
111 * 1 means 1.0 version
112 * 2 means 1.1 version
113 * etc,
114 */
115 platform_version += 9;
116 }
Michal Simek2e53eb22022-09-19 14:21:02 +0200117 }
118
119 debug("Platform id: %d version: %d.%d\n", platform_id,
120 platform_version / 10, platform_version % 10);
121
122 return true;
123}
124
125int board_early_init_f(void)
126{
127 if (IS_ENABLED(CONFIG_DEBUG_UART)) {
128 /* Uart debug for sure */
129 debug_uart_init();
130 puts("Debug uart enabled\n"); /* or printch() */
131 }
132
133 return 0;
134}
135
136int board_early_init_r(void)
137{
Ashok Reddy Soma81627322023-01-10 08:44:07 +0100138 u32 val;
139
140 if (current_el() != 3)
141 return 0;
142
143 debug("iou_switch ctrl div0 %x\n",
144 readl(&crlapb_base->iou_switch_ctrl));
145
146 writel(IOU_SWITCH_CTRL_CLKACT_BIT |
147 (CONFIG_IOU_SWITCH_DIVISOR0 << IOU_SWITCH_CTRL_DIVISOR0_SHIFT),
148 &crlapb_base->iou_switch_ctrl);
149
150 /* Global timer init - Program time stamp reference clk */
151 val = readl(&crlapb_base->timestamp_ref_ctrl);
152 val |= CRL_APB_TIMESTAMP_REF_CTRL_CLKACT_BIT;
153 writel(val, &crlapb_base->timestamp_ref_ctrl);
154
155 debug("ref ctrl 0x%x\n",
156 readl(&crlapb_base->timestamp_ref_ctrl));
157
158 /* Clear reset of timestamp reg */
159 writel(0, &crlapb_base->rst_timestamp);
160
161 /*
162 * Program freq register in System counter and
163 * enable system counter.
164 */
165 writel(CONFIG_COUNTER_FREQUENCY,
166 &iou_scntr_secure->base_frequency_id_register);
167
168 debug("counter val 0x%x\n",
169 readl(&iou_scntr_secure->base_frequency_id_register));
170
171 writel(IOU_SCNTRS_CONTROL_EN,
172 &iou_scntr_secure->counter_control_register);
173
174 debug("scntrs control 0x%x\n",
175 readl(&iou_scntr_secure->counter_control_register));
176 debug("timer 0x%llx\n", get_ticks());
177 debug("timer 0x%llx\n", get_ticks());
178
Michal Simek2e53eb22022-09-19 14:21:02 +0200179 return 0;
180}
181
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600182static u8 versal_net_get_bootmode(void)
183{
184 u8 bootmode;
185 u32 reg = 0;
186
187 reg = readl(&crp_base->boot_mode_usr);
188
189 if (reg >> BOOT_MODE_ALT_SHIFT)
190 reg >>= BOOT_MODE_ALT_SHIFT;
191
192 bootmode = reg & BOOT_MODES_MASK;
193
194 return bootmode;
195}
196
Venkatesh Yadav Abbarapu76244b32024-06-14 18:18:11 +0530197int spi_get_env_dev(void)
198{
199 struct udevice *dev;
Venkatesh Yadav Abbarapu76244b32024-06-14 18:18:11 +0530200 int bootseq = -1;
201
202 switch (versal_net_get_bootmode()) {
203 case QSPI_MODE_24BIT:
204 puts("QSPI_MODE_24\n");
205 if (uclass_get_device_by_name(UCLASS_SPI,
206 "spi@f1030000", &dev)) {
207 debug("QSPI driver for QSPI device is not present\n");
208 break;
209 }
Venkatesh Yadav Abbarapu76244b32024-06-14 18:18:11 +0530210 bootseq = dev_seq(dev);
211 break;
212 case QSPI_MODE_32BIT:
213 puts("QSPI_MODE_32\n");
214 if (uclass_get_device_by_name(UCLASS_SPI,
215 "spi@f1030000", &dev)) {
216 debug("QSPI driver for QSPI device is not present\n");
217 break;
218 }
Venkatesh Yadav Abbarapu76244b32024-06-14 18:18:11 +0530219 bootseq = dev_seq(dev);
220 break;
221 case OSPI_MODE:
222 puts("OSPI_MODE\n");
223 if (uclass_get_device_by_name(UCLASS_SPI,
224 "spi@f1010000", &dev)) {
225 debug("OSPI driver for OSPI device is not present\n");
226 break;
227 }
Venkatesh Yadav Abbarapu76244b32024-06-14 18:18:11 +0530228 bootseq = dev_seq(dev);
229 break;
230 default:
231 break;
232 }
233
234 debug("bootseq %d\n", bootseq);
235 return bootseq;
236}
237
Michal Simekb1634762023-09-05 13:30:07 +0200238static int boot_targets_setup(void)
Michal Simek2e53eb22022-09-19 14:21:02 +0200239{
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600240 u8 bootmode;
241 struct udevice *dev;
242 int bootseq = -1;
243 int bootseq_len = 0;
244 int env_targets_len = 0;
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530245 const char *mode = NULL;
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600246 char *new_targets;
247 char *env_targets;
248
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600249 bootmode = versal_net_get_bootmode();
250
251 puts("Bootmode: ");
252 switch (bootmode) {
253 case USB_MODE:
254 puts("USB_MODE\n");
255 mode = "usb_dfu0 usb_dfu1";
256 break;
257 case JTAG_MODE:
258 puts("JTAG_MODE\n");
259 mode = "jtag pxe dhcp";
260 break;
261 case QSPI_MODE_24BIT:
262 puts("QSPI_MODE_24\n");
Ashok Reddy Soma4c16d322023-06-14 03:30:58 -0600263 if (uclass_get_device_by_name(UCLASS_SPI,
264 "spi@f1030000", &dev)) {
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530265 debug("QSPI driver for QSPI device is not present\n");
266 break;
Ashok Reddy Soma4c16d322023-06-14 03:30:58 -0600267 }
268 mode = "xspi";
269 bootseq = dev_seq(dev);
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600270 break;
271 case QSPI_MODE_32BIT:
272 puts("QSPI_MODE_32\n");
Ashok Reddy Soma4c16d322023-06-14 03:30:58 -0600273 if (uclass_get_device_by_name(UCLASS_SPI,
274 "spi@f1030000", &dev)) {
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530275 debug("QSPI driver for QSPI device is not present\n");
276 break;
Ashok Reddy Soma4c16d322023-06-14 03:30:58 -0600277 }
278 mode = "xspi";
279 bootseq = dev_seq(dev);
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600280 break;
281 case OSPI_MODE:
282 puts("OSPI_MODE\n");
Ashok Reddy Soma4c16d322023-06-14 03:30:58 -0600283 if (uclass_get_device_by_name(UCLASS_SPI,
284 "spi@f1010000", &dev)) {
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530285 debug("OSPI driver for OSPI device is not present\n");
286 break;
Ashok Reddy Soma4c16d322023-06-14 03:30:58 -0600287 }
288 mode = "xspi";
289 bootseq = dev_seq(dev);
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600290 break;
291 case EMMC_MODE:
292 puts("EMMC_MODE\n");
293 mode = "mmc";
294 bootseq = dev_seq(dev);
295 break;
Polak, Leszekcddfc132023-10-08 14:34:42 +0000296 case SELECTMAP_MODE:
297 puts("SELECTMAP_MODE\n");
298 break;
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600299 case SD_MODE:
300 puts("SD_MODE\n");
301 if (uclass_get_device_by_name(UCLASS_MMC,
302 "mmc@f1040000", &dev)) {
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530303 debug("SD0 driver for SD0 device is not present\n");
304 break;
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600305 }
306 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
307
308 mode = "mmc";
309 bootseq = dev_seq(dev);
310 break;
311 case SD1_LSHFT_MODE:
312 puts("LVL_SHFT_");
313 fallthrough;
314 case SD_MODE1:
315 puts("SD_MODE1\n");
316 if (uclass_get_device_by_name(UCLASS_MMC,
317 "mmc@f1050000", &dev)) {
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530318 debug("SD1 driver for SD1 device is not present\n");
319 break;
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600320 }
321 debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev));
322
323 mode = "mmc";
324 bootseq = dev_seq(dev);
325 break;
326 default:
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600327 printf("Invalid Boot Mode:0x%x\n", bootmode);
328 break;
329 }
330
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530331 if (mode) {
332 if (bootseq >= 0) {
333 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
334 debug("Bootseq len: %x\n", bootseq_len);
335 }
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600336
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530337 /*
338 * One terminating char + one byte for space between mode
339 * and default boot_targets
340 */
341 env_targets = env_get("boot_targets");
342 if (env_targets)
343 env_targets_len = strlen(env_targets);
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600344
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530345 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
346 bootseq_len);
347 if (!new_targets)
348 return -ENOMEM;
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600349
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530350 if (bootseq >= 0)
351 sprintf(new_targets, "%s%x %s", mode, bootseq,
352 env_targets ? env_targets : "");
353 else
354 sprintf(new_targets, "%s %s", mode,
355 env_targets ? env_targets : "");
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600356
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530357 env_set("boot_targets", new_targets);
358 }
Michal Simekb1634762023-09-05 13:30:07 +0200359
360 return 0;
361}
362
363int board_late_init(void)
364{
365 int ret;
366
367 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
368 debug("Saved variables - Skipping\n");
369 return 0;
370 }
371
372 if (!IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG))
373 return 0;
374
375 if (IS_ENABLED(CONFIG_DISTRO_DEFAULTS)) {
376 ret = boot_targets_setup();
377 if (ret)
378 return ret;
379 }
380
Michal Simek2e53eb22022-09-19 14:21:02 +0200381 return board_late_init_xilinx();
382}
383
384int dram_init_banksize(void)
385{
386 int ret;
387
388 ret = fdtdec_setup_memory_banksize();
389 if (ret)
390 return ret;
391
392 mem_map_fill();
393
394 return 0;
395}
396
397int dram_init(void)
398{
399 int ret;
400
Simon Glassea0ada32023-02-05 15:40:57 -0700401 if (IS_ENABLED(CONFIG_SYS_MEM_RSVD_FOR_MMU))
Michal Simek2e53eb22022-09-19 14:21:02 +0200402 ret = fdtdec_setup_mem_size_base();
403 else
404 ret = fdtdec_setup_mem_size_base_lowest();
405
406 if (ret)
407 return -EINVAL;
408
409 return 0;
410}
411
412void reset_cpu(void)
413{
414}
Venkatesh Yadav Abbarapufc4884b2024-03-12 17:04:21 +0530415
Michal Simekf3a541f2024-03-22 12:43:17 +0100416#if defined(CONFIG_ENV_IS_NOWHERE)
Venkatesh Yadav Abbarapufc4884b2024-03-12 17:04:21 +0530417enum env_location env_get_location(enum env_operation op, int prio)
418{
419 u8 bootmode = versal_net_get_bootmode();
420
421 if (prio)
422 return ENVL_UNKNOWN;
423
424 switch (bootmode) {
425 case EMMC_MODE:
426 case SD_MODE:
427 case SD1_LSHFT_MODE:
428 case SD_MODE1:
429 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
430 return ENVL_FAT;
431 if (IS_ENABLED(CONFIG_ENV_IS_IN_EXT4))
432 return ENVL_EXT4;
433 return ENVL_NOWHERE;
434 case OSPI_MODE:
435 case QSPI_MODE_24BIT:
436 case QSPI_MODE_32BIT:
437 if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
438 return ENVL_SPI_FLASH;
439 return ENVL_NOWHERE;
440 case JTAG_MODE:
441 case SELECTMAP_MODE:
442 default:
443 return ENVL_NOWHERE;
444 }
445}
Michal Simekf3a541f2024-03-22 12:43:17 +0100446#endif