blob: 990ca1650aa4bef9b83eee0e5b5a198e30ab6ea0 [file] [log] [blame]
Michal Simekf6aebdf2022-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
9#include <common.h>
10#include <cpu_func.h>
11#include <fdtdec.h>
12#include <init.h>
Ashok Reddy Somaded539f2023-05-16 08:47:53 -060013#include <env_internal.h>
Michal Simekf6aebdf2022-09-19 14:21:02 +020014#include <log.h>
15#include <malloc.h>
16#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 Simek3e95bf92023-05-17 10:21:32 +020078 u32 version, ps_version;
Michal Simekf6aebdf2022-09-19 14:21:02 +020079
80 version = readl(PMC_TAP_VERSION);
81 platform_id = FIELD_GET(PLATFORM_MASK, version);
Michal Simek3e95bf92023-05-17 10:21:32 +020082 ps_version = FIELD_GET(PS_VERSION_MASK, version);
Michal Simekf6aebdf2022-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 Simek3e95bf92023-05-17 10:21:32 +020088 debug("pmc_ver %lx, ps version %x, rtl version %lx\n",
Michal Simekf6aebdf2022-09-19 14:21:02 +020089 FIELD_GET(PMC_VERSION_MASK, version),
Michal Simek3e95bf92023-05-17 10:21:32 +020090 ps_version,
Michal Simekf6aebdf2022-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 Simek3e95bf92023-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 Simekf6aebdf2022-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 Soma724379d2023-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 Simekf6aebdf2022-09-19 14:21:02 +0200179 return 0;
180}
181
Ashok Reddy Somaded539f2023-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
Michal Simek6ec17a22023-09-05 13:30:07 +0200197static int boot_targets_setup(void)
Michal Simekf6aebdf2022-09-19 14:21:02 +0200198{
Ashok Reddy Somaded539f2023-05-16 08:47:53 -0600199 u8 bootmode;
200 struct udevice *dev;
201 int bootseq = -1;
202 int bootseq_len = 0;
203 int env_targets_len = 0;
Venkatesh Yadav Abbarapuc55eb1d2023-09-04 08:50:33 +0530204 const char *mode = NULL;
Ashok Reddy Somaded539f2023-05-16 08:47:53 -0600205 char *new_targets;
206 char *env_targets;
207
Ashok Reddy Somaded539f2023-05-16 08:47:53 -0600208 bootmode = versal_net_get_bootmode();
209
210 puts("Bootmode: ");
211 switch (bootmode) {
212 case USB_MODE:
213 puts("USB_MODE\n");
214 mode = "usb_dfu0 usb_dfu1";
215 break;
216 case JTAG_MODE:
217 puts("JTAG_MODE\n");
218 mode = "jtag pxe dhcp";
219 break;
220 case QSPI_MODE_24BIT:
221 puts("QSPI_MODE_24\n");
Ashok Reddy Soma9a453652023-06-14 03:30:58 -0600222 if (uclass_get_device_by_name(UCLASS_SPI,
223 "spi@f1030000", &dev)) {
Venkatesh Yadav Abbarapuc55eb1d2023-09-04 08:50:33 +0530224 debug("QSPI driver for QSPI device is not present\n");
225 break;
Ashok Reddy Soma9a453652023-06-14 03:30:58 -0600226 }
227 mode = "xspi";
228 bootseq = dev_seq(dev);
Ashok Reddy Somaded539f2023-05-16 08:47:53 -0600229 break;
230 case QSPI_MODE_32BIT:
231 puts("QSPI_MODE_32\n");
Ashok Reddy Soma9a453652023-06-14 03:30:58 -0600232 if (uclass_get_device_by_name(UCLASS_SPI,
233 "spi@f1030000", &dev)) {
Venkatesh Yadav Abbarapuc55eb1d2023-09-04 08:50:33 +0530234 debug("QSPI driver for QSPI device is not present\n");
235 break;
Ashok Reddy Soma9a453652023-06-14 03:30:58 -0600236 }
237 mode = "xspi";
238 bootseq = dev_seq(dev);
Ashok Reddy Somaded539f2023-05-16 08:47:53 -0600239 break;
240 case OSPI_MODE:
241 puts("OSPI_MODE\n");
Ashok Reddy Soma9a453652023-06-14 03:30:58 -0600242 if (uclass_get_device_by_name(UCLASS_SPI,
243 "spi@f1010000", &dev)) {
Venkatesh Yadav Abbarapuc55eb1d2023-09-04 08:50:33 +0530244 debug("OSPI driver for OSPI device is not present\n");
245 break;
Ashok Reddy Soma9a453652023-06-14 03:30:58 -0600246 }
247 mode = "xspi";
248 bootseq = dev_seq(dev);
Ashok Reddy Somaded539f2023-05-16 08:47:53 -0600249 break;
250 case EMMC_MODE:
251 puts("EMMC_MODE\n");
252 mode = "mmc";
253 bootseq = dev_seq(dev);
254 break;
Polak, Leszekfacfa562023-10-08 14:34:42 +0000255 case SELECTMAP_MODE:
256 puts("SELECTMAP_MODE\n");
257 break;
Ashok Reddy Somaded539f2023-05-16 08:47:53 -0600258 case SD_MODE:
259 puts("SD_MODE\n");
260 if (uclass_get_device_by_name(UCLASS_MMC,
261 "mmc@f1040000", &dev)) {
Venkatesh Yadav Abbarapuc55eb1d2023-09-04 08:50:33 +0530262 debug("SD0 driver for SD0 device is not present\n");
263 break;
Ashok Reddy Somaded539f2023-05-16 08:47:53 -0600264 }
265 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
266
267 mode = "mmc";
268 bootseq = dev_seq(dev);
269 break;
270 case SD1_LSHFT_MODE:
271 puts("LVL_SHFT_");
272 fallthrough;
273 case SD_MODE1:
274 puts("SD_MODE1\n");
275 if (uclass_get_device_by_name(UCLASS_MMC,
276 "mmc@f1050000", &dev)) {
Venkatesh Yadav Abbarapuc55eb1d2023-09-04 08:50:33 +0530277 debug("SD1 driver for SD1 device is not present\n");
278 break;
Ashok Reddy Somaded539f2023-05-16 08:47:53 -0600279 }
280 debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev));
281
282 mode = "mmc";
283 bootseq = dev_seq(dev);
284 break;
285 default:
Ashok Reddy Somaded539f2023-05-16 08:47:53 -0600286 printf("Invalid Boot Mode:0x%x\n", bootmode);
287 break;
288 }
289
Venkatesh Yadav Abbarapuc55eb1d2023-09-04 08:50:33 +0530290 if (mode) {
291 if (bootseq >= 0) {
292 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
293 debug("Bootseq len: %x\n", bootseq_len);
294 }
295
296 /*
297 * One terminating char + one byte for space between mode
298 * and default boot_targets
299 */
300 env_targets = env_get("boot_targets");
301 if (env_targets)
302 env_targets_len = strlen(env_targets);
303
304 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
305 bootseq_len);
306 if (!new_targets)
307 return -ENOMEM;
308
309 if (bootseq >= 0)
310 sprintf(new_targets, "%s%x %s", mode, bootseq,
311 env_targets ? env_targets : "");
312 else
313 sprintf(new_targets, "%s %s", mode,
314 env_targets ? env_targets : "");
315
316 env_set("boot_targets", new_targets);
Ashok Reddy Somaded539f2023-05-16 08:47:53 -0600317 }
Michal Simek6ec17a22023-09-05 13:30:07 +0200318
319 return 0;
320}
321
322int board_late_init(void)
323{
324 int ret;
325
326 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
327 debug("Saved variables - Skipping\n");
328 return 0;
329 }
330
331 if (!IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG))
332 return 0;
333
334 if (IS_ENABLED(CONFIG_DISTRO_DEFAULTS)) {
335 ret = boot_targets_setup();
336 if (ret)
337 return ret;
338 }
339
Michal Simekf6aebdf2022-09-19 14:21:02 +0200340 return board_late_init_xilinx();
341}
342
343int dram_init_banksize(void)
344{
345 int ret;
346
347 ret = fdtdec_setup_memory_banksize();
348 if (ret)
349 return ret;
350
351 mem_map_fill();
352
353 return 0;
354}
355
356int dram_init(void)
357{
358 int ret;
359
Simon Glass1f3a3672023-02-05 15:40:57 -0700360 if (IS_ENABLED(CONFIG_SYS_MEM_RSVD_FOR_MMU))
Michal Simekf6aebdf2022-09-19 14:21:02 +0200361 ret = fdtdec_setup_mem_size_base();
362 else
363 ret = fdtdec_setup_mem_size_base_lowest();
364
365 if (ret)
366 return -EINVAL;
367
368 return 0;
369}
370
371void reset_cpu(void)
372{
373}