blob: 1d67e3f31859bd9385febd08643e580398a2d9d7 [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>
15#include <time.h>
16#include <asm/cache.h>
17#include <asm/global_data.h>
18#include <asm/io.h>
19#include <asm/arch/hardware.h>
20#include <asm/arch/sys_proto.h>
21#include <dm/device.h>
22#include <dm/uclass.h>
23#include "../common/board.h"
24
25#include <linux/bitfield.h>
26#include <debug_uart.h>
27#include <generated/dt.h>
28
29DECLARE_GLOBAL_DATA_PTR;
30
31int board_init(void)
32{
33 printf("EL Level:\tEL%d\n", current_el());
34
35 return 0;
36}
37
38static u32 platform_id, platform_version;
39
40char *soc_name_decode(void)
41{
42 char *name, *platform_name;
43
44 switch (platform_id) {
45 case VERSAL_NET_SPP:
46 platform_name = "ipp";
47 break;
48 case VERSAL_NET_EMU:
49 platform_name = "emu";
50 break;
51 case VERSAL_NET_QEMU:
52 platform_name = "qemu";
53 break;
54 default:
55 return NULL;
56 }
57
58 /*
59 * --rev. are 6 chars
60 * max platform name is qemu which is 4 chars
61 * platform version number are 1+1
62 * Plus 1 char for \n
63 */
64 name = calloc(1, strlen(CONFIG_SYS_BOARD) + 13);
65 if (!name)
66 return NULL;
67
68 sprintf(name, "%s-%s-rev%d.%d", CONFIG_SYS_BOARD,
69 platform_name, platform_version / 10,
70 platform_version % 10);
71
72 return name;
73}
74
75bool soc_detection(void)
76{
Michal Simekba73d6e2023-05-17 10:21:32 +020077 u32 version, ps_version;
Michal Simek2e53eb22022-09-19 14:21:02 +020078
79 version = readl(PMC_TAP_VERSION);
80 platform_id = FIELD_GET(PLATFORM_MASK, version);
Michal Simekba73d6e2023-05-17 10:21:32 +020081 ps_version = FIELD_GET(PS_VERSION_MASK, version);
Michal Simek2e53eb22022-09-19 14:21:02 +020082
83 debug("idcode %x, version %x, usercode %x\n",
84 readl(PMC_TAP_IDCODE), version,
85 readl(PMC_TAP_USERCODE));
86
Michal Simekba73d6e2023-05-17 10:21:32 +020087 debug("pmc_ver %lx, ps version %x, rtl version %lx\n",
Michal Simek2e53eb22022-09-19 14:21:02 +020088 FIELD_GET(PMC_VERSION_MASK, version),
Michal Simekba73d6e2023-05-17 10:21:32 +020089 ps_version,
Michal Simek2e53eb22022-09-19 14:21:02 +020090 FIELD_GET(RTL_VERSION_MASK, version));
91
92 platform_version = FIELD_GET(PLATFORM_VERSION_MASK, version);
93
94 if (platform_id == VERSAL_NET_SPP ||
95 platform_id == VERSAL_NET_EMU) {
Michal Simekba73d6e2023-05-17 10:21:32 +020096 if (ps_version == PS_VERSION_PRODUCTION) {
97 /*
98 * ES1 version ends at 1.9 version where there was +9
99 * used because of IPP/SPP conversion. Production
100 * version have platform_version started from 0 again
101 * that's why adding +20 to continue with the same line.
102 * It means the last ES1 version ends at 1.9 version and
103 * new PRODUCTION line starts at 2.0.
104 */
105 platform_version += 20;
106 } else {
107 /*
108 * 9 is diff for
109 * 0 means 0.9 version
110 * 1 means 1.0 version
111 * 2 means 1.1 version
112 * etc,
113 */
114 platform_version += 9;
115 }
Michal Simek2e53eb22022-09-19 14:21:02 +0200116 }
117
118 debug("Platform id: %d version: %d.%d\n", platform_id,
119 platform_version / 10, platform_version % 10);
120
121 return true;
122}
123
124int board_early_init_f(void)
125{
126 if (IS_ENABLED(CONFIG_DEBUG_UART)) {
127 /* Uart debug for sure */
128 debug_uart_init();
129 puts("Debug uart enabled\n"); /* or printch() */
130 }
131
132 return 0;
133}
134
135int board_early_init_r(void)
136{
Ashok Reddy Soma81627322023-01-10 08:44:07 +0100137 u32 val;
138
139 if (current_el() != 3)
140 return 0;
141
142 debug("iou_switch ctrl div0 %x\n",
143 readl(&crlapb_base->iou_switch_ctrl));
144
145 writel(IOU_SWITCH_CTRL_CLKACT_BIT |
146 (CONFIG_IOU_SWITCH_DIVISOR0 << IOU_SWITCH_CTRL_DIVISOR0_SHIFT),
147 &crlapb_base->iou_switch_ctrl);
148
149 /* Global timer init - Program time stamp reference clk */
150 val = readl(&crlapb_base->timestamp_ref_ctrl);
151 val |= CRL_APB_TIMESTAMP_REF_CTRL_CLKACT_BIT;
152 writel(val, &crlapb_base->timestamp_ref_ctrl);
153
154 debug("ref ctrl 0x%x\n",
155 readl(&crlapb_base->timestamp_ref_ctrl));
156
157 /* Clear reset of timestamp reg */
158 writel(0, &crlapb_base->rst_timestamp);
159
160 /*
161 * Program freq register in System counter and
162 * enable system counter.
163 */
164 writel(CONFIG_COUNTER_FREQUENCY,
165 &iou_scntr_secure->base_frequency_id_register);
166
167 debug("counter val 0x%x\n",
168 readl(&iou_scntr_secure->base_frequency_id_register));
169
170 writel(IOU_SCNTRS_CONTROL_EN,
171 &iou_scntr_secure->counter_control_register);
172
173 debug("scntrs control 0x%x\n",
174 readl(&iou_scntr_secure->counter_control_register));
175 debug("timer 0x%llx\n", get_ticks());
176 debug("timer 0x%llx\n", get_ticks());
177
Michal Simek2e53eb22022-09-19 14:21:02 +0200178 return 0;
179}
180
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600181static u8 versal_net_get_bootmode(void)
182{
183 u8 bootmode;
184 u32 reg = 0;
185
186 reg = readl(&crp_base->boot_mode_usr);
187
188 if (reg >> BOOT_MODE_ALT_SHIFT)
189 reg >>= BOOT_MODE_ALT_SHIFT;
190
191 bootmode = reg & BOOT_MODES_MASK;
192
193 return bootmode;
194}
195
Venkatesh Yadav Abbarapu76244b32024-06-14 18:18:11 +0530196int spi_get_env_dev(void)
197{
198 struct udevice *dev;
199 const char *mode = NULL;
200 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 }
210 mode = "xspi";
211 bootseq = dev_seq(dev);
212 break;
213 case QSPI_MODE_32BIT:
214 puts("QSPI_MODE_32\n");
215 if (uclass_get_device_by_name(UCLASS_SPI,
216 "spi@f1030000", &dev)) {
217 debug("QSPI driver for QSPI device is not present\n");
218 break;
219 }
220 mode = "xspi";
221 bootseq = dev_seq(dev);
222 break;
223 case OSPI_MODE:
224 puts("OSPI_MODE\n");
225 if (uclass_get_device_by_name(UCLASS_SPI,
226 "spi@f1010000", &dev)) {
227 debug("OSPI driver for OSPI device is not present\n");
228 break;
229 }
230 mode = "xspi";
231 bootseq = dev_seq(dev);
232 break;
233 default:
234 break;
235 }
236
237 debug("bootseq %d\n", bootseq);
238 return bootseq;
239}
240
Michal Simekb1634762023-09-05 13:30:07 +0200241static int boot_targets_setup(void)
Michal Simek2e53eb22022-09-19 14:21:02 +0200242{
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600243 u8 bootmode;
244 struct udevice *dev;
245 int bootseq = -1;
246 int bootseq_len = 0;
247 int env_targets_len = 0;
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530248 const char *mode = NULL;
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600249 char *new_targets;
250 char *env_targets;
251
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600252 bootmode = versal_net_get_bootmode();
253
254 puts("Bootmode: ");
255 switch (bootmode) {
256 case USB_MODE:
257 puts("USB_MODE\n");
258 mode = "usb_dfu0 usb_dfu1";
259 break;
260 case JTAG_MODE:
261 puts("JTAG_MODE\n");
262 mode = "jtag pxe dhcp";
263 break;
264 case QSPI_MODE_24BIT:
265 puts("QSPI_MODE_24\n");
Ashok Reddy Soma4c16d322023-06-14 03:30:58 -0600266 if (uclass_get_device_by_name(UCLASS_SPI,
267 "spi@f1030000", &dev)) {
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530268 debug("QSPI driver for QSPI device is not present\n");
269 break;
Ashok Reddy Soma4c16d322023-06-14 03:30:58 -0600270 }
271 mode = "xspi";
272 bootseq = dev_seq(dev);
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600273 break;
274 case QSPI_MODE_32BIT:
275 puts("QSPI_MODE_32\n");
Ashok Reddy Soma4c16d322023-06-14 03:30:58 -0600276 if (uclass_get_device_by_name(UCLASS_SPI,
277 "spi@f1030000", &dev)) {
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530278 debug("QSPI driver for QSPI device is not present\n");
279 break;
Ashok Reddy Soma4c16d322023-06-14 03:30:58 -0600280 }
281 mode = "xspi";
282 bootseq = dev_seq(dev);
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600283 break;
284 case OSPI_MODE:
285 puts("OSPI_MODE\n");
Ashok Reddy Soma4c16d322023-06-14 03:30:58 -0600286 if (uclass_get_device_by_name(UCLASS_SPI,
287 "spi@f1010000", &dev)) {
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530288 debug("OSPI driver for OSPI device is not present\n");
289 break;
Ashok Reddy Soma4c16d322023-06-14 03:30:58 -0600290 }
291 mode = "xspi";
292 bootseq = dev_seq(dev);
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600293 break;
294 case EMMC_MODE:
295 puts("EMMC_MODE\n");
296 mode = "mmc";
297 bootseq = dev_seq(dev);
298 break;
Polak, Leszekcddfc132023-10-08 14:34:42 +0000299 case SELECTMAP_MODE:
300 puts("SELECTMAP_MODE\n");
301 break;
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600302 case SD_MODE:
303 puts("SD_MODE\n");
304 if (uclass_get_device_by_name(UCLASS_MMC,
305 "mmc@f1040000", &dev)) {
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530306 debug("SD0 driver for SD0 device is not present\n");
307 break;
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600308 }
309 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
310
311 mode = "mmc";
312 bootseq = dev_seq(dev);
313 break;
314 case SD1_LSHFT_MODE:
315 puts("LVL_SHFT_");
316 fallthrough;
317 case SD_MODE1:
318 puts("SD_MODE1\n");
319 if (uclass_get_device_by_name(UCLASS_MMC,
320 "mmc@f1050000", &dev)) {
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530321 debug("SD1 driver for SD1 device is not present\n");
322 break;
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600323 }
324 debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev));
325
326 mode = "mmc";
327 bootseq = dev_seq(dev);
328 break;
329 default:
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600330 printf("Invalid Boot Mode:0x%x\n", bootmode);
331 break;
332 }
333
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530334 if (mode) {
335 if (bootseq >= 0) {
336 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
337 debug("Bootseq len: %x\n", bootseq_len);
338 }
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600339
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530340 /*
341 * One terminating char + one byte for space between mode
342 * and default boot_targets
343 */
344 env_targets = env_get("boot_targets");
345 if (env_targets)
346 env_targets_len = strlen(env_targets);
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600347
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530348 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
349 bootseq_len);
350 if (!new_targets)
351 return -ENOMEM;
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600352
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530353 if (bootseq >= 0)
354 sprintf(new_targets, "%s%x %s", mode, bootseq,
355 env_targets ? env_targets : "");
356 else
357 sprintf(new_targets, "%s %s", mode,
358 env_targets ? env_targets : "");
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600359
Venkatesh Yadav Abbarapu2c81c1d2023-09-04 08:50:33 +0530360 env_set("boot_targets", new_targets);
361 }
Michal Simekb1634762023-09-05 13:30:07 +0200362
363 return 0;
364}
365
366int board_late_init(void)
367{
368 int ret;
369
370 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
371 debug("Saved variables - Skipping\n");
372 return 0;
373 }
374
375 if (!IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG))
376 return 0;
377
378 if (IS_ENABLED(CONFIG_DISTRO_DEFAULTS)) {
379 ret = boot_targets_setup();
380 if (ret)
381 return ret;
382 }
383
Michal Simek2e53eb22022-09-19 14:21:02 +0200384 return board_late_init_xilinx();
385}
386
387int dram_init_banksize(void)
388{
389 int ret;
390
391 ret = fdtdec_setup_memory_banksize();
392 if (ret)
393 return ret;
394
395 mem_map_fill();
396
397 return 0;
398}
399
400int dram_init(void)
401{
402 int ret;
403
Simon Glassea0ada32023-02-05 15:40:57 -0700404 if (IS_ENABLED(CONFIG_SYS_MEM_RSVD_FOR_MMU))
Michal Simek2e53eb22022-09-19 14:21:02 +0200405 ret = fdtdec_setup_mem_size_base();
406 else
407 ret = fdtdec_setup_mem_size_base_lowest();
408
409 if (ret)
410 return -EINVAL;
411
412 return 0;
413}
414
415void reset_cpu(void)
416{
417}
Venkatesh Yadav Abbarapufc4884b2024-03-12 17:04:21 +0530418
Michal Simekf3a541f2024-03-22 12:43:17 +0100419#if defined(CONFIG_ENV_IS_NOWHERE)
Venkatesh Yadav Abbarapufc4884b2024-03-12 17:04:21 +0530420enum env_location env_get_location(enum env_operation op, int prio)
421{
422 u8 bootmode = versal_net_get_bootmode();
423
424 if (prio)
425 return ENVL_UNKNOWN;
426
427 switch (bootmode) {
428 case EMMC_MODE:
429 case SD_MODE:
430 case SD1_LSHFT_MODE:
431 case SD_MODE1:
432 if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT))
433 return ENVL_FAT;
434 if (IS_ENABLED(CONFIG_ENV_IS_IN_EXT4))
435 return ENVL_EXT4;
436 return ENVL_NOWHERE;
437 case OSPI_MODE:
438 case QSPI_MODE_24BIT:
439 case QSPI_MODE_32BIT:
440 if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH))
441 return ENVL_SPI_FLASH;
442 return ENVL_NOWHERE;
443 case JTAG_MODE:
444 case SELECTMAP_MODE:
445 default:
446 return ENVL_NOWHERE;
447 }
448}
Michal Simekf3a541f2024-03-22 12:43:17 +0100449#endif