blob: f0d2224b332e89a85837119182c444c97e068f6d [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
9#include <common.h>
10#include <cpu_func.h>
11#include <fdtdec.h>
12#include <init.h>
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -060013#include <env_internal.h>
Michal Simek2e53eb22022-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 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
Michal Simek2e53eb22022-09-19 14:21:02 +0200197int board_late_init(void)
198{
Ashok Reddy Somaa7a5a1b2023-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;
204 const char *mode;
205 char *new_targets;
206 char *env_targets;
207
Michal Simek2e53eb22022-09-19 14:21:02 +0200208 if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
209 debug("Saved variables - Skipping\n");
210 return 0;
211 }
212
Simon Glass094778f2023-02-05 15:39:49 -0700213 if (!IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG))
Michal Simek2e53eb22022-09-19 14:21:02 +0200214 return 0;
215
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600216 bootmode = versal_net_get_bootmode();
217
218 puts("Bootmode: ");
219 switch (bootmode) {
220 case USB_MODE:
221 puts("USB_MODE\n");
222 mode = "usb_dfu0 usb_dfu1";
223 break;
224 case JTAG_MODE:
225 puts("JTAG_MODE\n");
226 mode = "jtag pxe dhcp";
227 break;
228 case QSPI_MODE_24BIT:
229 puts("QSPI_MODE_24\n");
Ashok Reddy Soma4c16d322023-06-14 03:30:58 -0600230 if (uclass_get_device_by_name(UCLASS_SPI,
231 "spi@f1030000", &dev)) {
232 puts("Boot from QSPI but without QSPI enabled!\n");
233 return -1;
234 }
235 mode = "xspi";
236 bootseq = dev_seq(dev);
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600237 break;
238 case QSPI_MODE_32BIT:
239 puts("QSPI_MODE_32\n");
Ashok Reddy Soma4c16d322023-06-14 03:30:58 -0600240 if (uclass_get_device_by_name(UCLASS_SPI,
241 "spi@f1030000", &dev)) {
242 puts("Boot from QSPI but without QSPI enabled!\n");
243 return -1;
244 }
245 mode = "xspi";
246 bootseq = dev_seq(dev);
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600247 break;
248 case OSPI_MODE:
249 puts("OSPI_MODE\n");
Ashok Reddy Soma4c16d322023-06-14 03:30:58 -0600250 if (uclass_get_device_by_name(UCLASS_SPI,
251 "spi@f1010000", &dev)) {
252 puts("Boot from OSPI but without OSPI enabled!\n");
253 return -1;
254 }
255 mode = "xspi";
256 bootseq = dev_seq(dev);
Ashok Reddy Somaa7a5a1b2023-05-16 08:47:53 -0600257 break;
258 case EMMC_MODE:
259 puts("EMMC_MODE\n");
260 mode = "mmc";
261 bootseq = dev_seq(dev);
262 break;
263 case SD_MODE:
264 puts("SD_MODE\n");
265 if (uclass_get_device_by_name(UCLASS_MMC,
266 "mmc@f1040000", &dev)) {
267 puts("Boot from SD0 but without SD0 enabled!\n");
268 return -1;
269 }
270 debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev));
271
272 mode = "mmc";
273 bootseq = dev_seq(dev);
274 break;
275 case SD1_LSHFT_MODE:
276 puts("LVL_SHFT_");
277 fallthrough;
278 case SD_MODE1:
279 puts("SD_MODE1\n");
280 if (uclass_get_device_by_name(UCLASS_MMC,
281 "mmc@f1050000", &dev)) {
282 puts("Boot from SD1 but without SD1 enabled!\n");
283 return -1;
284 }
285 debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev));
286
287 mode = "mmc";
288 bootseq = dev_seq(dev);
289 break;
290 default:
291 mode = "";
292 printf("Invalid Boot Mode:0x%x\n", bootmode);
293 break;
294 }
295
296 if (bootseq >= 0) {
297 bootseq_len = snprintf(NULL, 0, "%i", bootseq);
298 debug("Bootseq len: %x\n", bootseq_len);
299 }
300
301 /*
302 * One terminating char + one byte for space between mode
303 * and default boot_targets
304 */
305 env_targets = env_get("boot_targets");
306 if (env_targets)
307 env_targets_len = strlen(env_targets);
308
309 new_targets = calloc(1, strlen(mode) + env_targets_len + 2 +
310 bootseq_len);
311 if (!new_targets)
312 return -ENOMEM;
313
314 if (bootseq >= 0)
315 sprintf(new_targets, "%s%x %s", mode, bootseq,
316 env_targets ? env_targets : "");
317 else
318 sprintf(new_targets, "%s %s", mode,
319 env_targets ? env_targets : "");
320
321 env_set("boot_targets", new_targets);
322
Michal Simek2e53eb22022-09-19 14:21:02 +0200323 return board_late_init_xilinx();
324}
325
326int dram_init_banksize(void)
327{
328 int ret;
329
330 ret = fdtdec_setup_memory_banksize();
331 if (ret)
332 return ret;
333
334 mem_map_fill();
335
336 return 0;
337}
338
339int dram_init(void)
340{
341 int ret;
342
Simon Glassea0ada32023-02-05 15:40:57 -0700343 if (IS_ENABLED(CONFIG_SYS_MEM_RSVD_FOR_MMU))
Michal Simek2e53eb22022-09-19 14:21:02 +0200344 ret = fdtdec_setup_mem_size_base();
345 else
346 ret = fdtdec_setup_mem_size_base_lowest();
347
348 if (ret)
349 return -EINVAL;
350
351 return 0;
352}
353
354void reset_cpu(void)
355{
356}