Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 1 | // 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 Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 9 | #include <cpu_func.h> |
| 10 | #include <fdtdec.h> |
| 11 | #include <init.h> |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 12 | #include <env_internal.h> |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 13 | #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 | |
| 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
| 31 | int board_init(void) |
| 32 | { |
| 33 | printf("EL Level:\tEL%d\n", current_el()); |
| 34 | |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | static u32 platform_id, platform_version; |
| 39 | |
| 40 | char *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 | |
| 75 | bool soc_detection(void) |
| 76 | { |
Michal Simek | ba73d6e | 2023-05-17 10:21:32 +0200 | [diff] [blame] | 77 | u32 version, ps_version; |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 78 | |
| 79 | version = readl(PMC_TAP_VERSION); |
| 80 | platform_id = FIELD_GET(PLATFORM_MASK, version); |
Michal Simek | ba73d6e | 2023-05-17 10:21:32 +0200 | [diff] [blame] | 81 | ps_version = FIELD_GET(PS_VERSION_MASK, version); |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 82 | |
| 83 | debug("idcode %x, version %x, usercode %x\n", |
| 84 | readl(PMC_TAP_IDCODE), version, |
| 85 | readl(PMC_TAP_USERCODE)); |
| 86 | |
Michal Simek | ba73d6e | 2023-05-17 10:21:32 +0200 | [diff] [blame] | 87 | debug("pmc_ver %lx, ps version %x, rtl version %lx\n", |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 88 | FIELD_GET(PMC_VERSION_MASK, version), |
Michal Simek | ba73d6e | 2023-05-17 10:21:32 +0200 | [diff] [blame] | 89 | ps_version, |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 90 | 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 Simek | ba73d6e | 2023-05-17 10:21:32 +0200 | [diff] [blame] | 96 | 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 Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 116 | } |
| 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 | |
| 124 | int 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 | |
| 135 | int board_early_init_r(void) |
| 136 | { |
Ashok Reddy Soma | 8162732 | 2023-01-10 08:44:07 +0100 | [diff] [blame] | 137 | 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 Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 181 | static 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 | |
Michal Simek | b163476 | 2023-09-05 13:30:07 +0200 | [diff] [blame] | 196 | static int boot_targets_setup(void) |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 197 | { |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 198 | u8 bootmode; |
| 199 | struct udevice *dev; |
| 200 | int bootseq = -1; |
| 201 | int bootseq_len = 0; |
| 202 | int env_targets_len = 0; |
Venkatesh Yadav Abbarapu | 2c81c1d | 2023-09-04 08:50:33 +0530 | [diff] [blame] | 203 | const char *mode = NULL; |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 204 | char *new_targets; |
| 205 | char *env_targets; |
| 206 | |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 207 | bootmode = versal_net_get_bootmode(); |
| 208 | |
| 209 | puts("Bootmode: "); |
| 210 | switch (bootmode) { |
| 211 | case USB_MODE: |
| 212 | puts("USB_MODE\n"); |
| 213 | mode = "usb_dfu0 usb_dfu1"; |
| 214 | break; |
| 215 | case JTAG_MODE: |
| 216 | puts("JTAG_MODE\n"); |
| 217 | mode = "jtag pxe dhcp"; |
| 218 | break; |
| 219 | case QSPI_MODE_24BIT: |
| 220 | puts("QSPI_MODE_24\n"); |
Ashok Reddy Soma | 4c16d32 | 2023-06-14 03:30:58 -0600 | [diff] [blame] | 221 | if (uclass_get_device_by_name(UCLASS_SPI, |
| 222 | "spi@f1030000", &dev)) { |
Venkatesh Yadav Abbarapu | 2c81c1d | 2023-09-04 08:50:33 +0530 | [diff] [blame] | 223 | debug("QSPI driver for QSPI device is not present\n"); |
| 224 | break; |
Ashok Reddy Soma | 4c16d32 | 2023-06-14 03:30:58 -0600 | [diff] [blame] | 225 | } |
| 226 | mode = "xspi"; |
| 227 | bootseq = dev_seq(dev); |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 228 | break; |
| 229 | case QSPI_MODE_32BIT: |
| 230 | puts("QSPI_MODE_32\n"); |
Ashok Reddy Soma | 4c16d32 | 2023-06-14 03:30:58 -0600 | [diff] [blame] | 231 | if (uclass_get_device_by_name(UCLASS_SPI, |
| 232 | "spi@f1030000", &dev)) { |
Venkatesh Yadav Abbarapu | 2c81c1d | 2023-09-04 08:50:33 +0530 | [diff] [blame] | 233 | debug("QSPI driver for QSPI device is not present\n"); |
| 234 | break; |
Ashok Reddy Soma | 4c16d32 | 2023-06-14 03:30:58 -0600 | [diff] [blame] | 235 | } |
| 236 | mode = "xspi"; |
| 237 | bootseq = dev_seq(dev); |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 238 | break; |
| 239 | case OSPI_MODE: |
| 240 | puts("OSPI_MODE\n"); |
Ashok Reddy Soma | 4c16d32 | 2023-06-14 03:30:58 -0600 | [diff] [blame] | 241 | if (uclass_get_device_by_name(UCLASS_SPI, |
| 242 | "spi@f1010000", &dev)) { |
Venkatesh Yadav Abbarapu | 2c81c1d | 2023-09-04 08:50:33 +0530 | [diff] [blame] | 243 | debug("OSPI driver for OSPI device is not present\n"); |
| 244 | break; |
Ashok Reddy Soma | 4c16d32 | 2023-06-14 03:30:58 -0600 | [diff] [blame] | 245 | } |
| 246 | mode = "xspi"; |
| 247 | bootseq = dev_seq(dev); |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 248 | break; |
| 249 | case EMMC_MODE: |
| 250 | puts("EMMC_MODE\n"); |
| 251 | mode = "mmc"; |
| 252 | bootseq = dev_seq(dev); |
| 253 | break; |
Polak, Leszek | cddfc13 | 2023-10-08 14:34:42 +0000 | [diff] [blame] | 254 | case SELECTMAP_MODE: |
| 255 | puts("SELECTMAP_MODE\n"); |
| 256 | break; |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 257 | case SD_MODE: |
| 258 | puts("SD_MODE\n"); |
| 259 | if (uclass_get_device_by_name(UCLASS_MMC, |
| 260 | "mmc@f1040000", &dev)) { |
Venkatesh Yadav Abbarapu | 2c81c1d | 2023-09-04 08:50:33 +0530 | [diff] [blame] | 261 | debug("SD0 driver for SD0 device is not present\n"); |
| 262 | break; |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 263 | } |
| 264 | debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev)); |
| 265 | |
| 266 | mode = "mmc"; |
| 267 | bootseq = dev_seq(dev); |
| 268 | break; |
| 269 | case SD1_LSHFT_MODE: |
| 270 | puts("LVL_SHFT_"); |
| 271 | fallthrough; |
| 272 | case SD_MODE1: |
| 273 | puts("SD_MODE1\n"); |
| 274 | if (uclass_get_device_by_name(UCLASS_MMC, |
| 275 | "mmc@f1050000", &dev)) { |
Venkatesh Yadav Abbarapu | 2c81c1d | 2023-09-04 08:50:33 +0530 | [diff] [blame] | 276 | debug("SD1 driver for SD1 device is not present\n"); |
| 277 | break; |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 278 | } |
| 279 | debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev)); |
| 280 | |
| 281 | mode = "mmc"; |
| 282 | bootseq = dev_seq(dev); |
| 283 | break; |
| 284 | default: |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 285 | printf("Invalid Boot Mode:0x%x\n", bootmode); |
| 286 | break; |
| 287 | } |
| 288 | |
Venkatesh Yadav Abbarapu | 2c81c1d | 2023-09-04 08:50:33 +0530 | [diff] [blame] | 289 | if (mode) { |
| 290 | if (bootseq >= 0) { |
| 291 | bootseq_len = snprintf(NULL, 0, "%i", bootseq); |
| 292 | debug("Bootseq len: %x\n", bootseq_len); |
| 293 | } |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 294 | |
Venkatesh Yadav Abbarapu | 2c81c1d | 2023-09-04 08:50:33 +0530 | [diff] [blame] | 295 | /* |
| 296 | * One terminating char + one byte for space between mode |
| 297 | * and default boot_targets |
| 298 | */ |
| 299 | env_targets = env_get("boot_targets"); |
| 300 | if (env_targets) |
| 301 | env_targets_len = strlen(env_targets); |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 302 | |
Venkatesh Yadav Abbarapu | 2c81c1d | 2023-09-04 08:50:33 +0530 | [diff] [blame] | 303 | new_targets = calloc(1, strlen(mode) + env_targets_len + 2 + |
| 304 | bootseq_len); |
| 305 | if (!new_targets) |
| 306 | return -ENOMEM; |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 307 | |
Venkatesh Yadav Abbarapu | 2c81c1d | 2023-09-04 08:50:33 +0530 | [diff] [blame] | 308 | if (bootseq >= 0) |
| 309 | sprintf(new_targets, "%s%x %s", mode, bootseq, |
| 310 | env_targets ? env_targets : ""); |
| 311 | else |
| 312 | sprintf(new_targets, "%s %s", mode, |
| 313 | env_targets ? env_targets : ""); |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 314 | |
Venkatesh Yadav Abbarapu | 2c81c1d | 2023-09-04 08:50:33 +0530 | [diff] [blame] | 315 | env_set("boot_targets", new_targets); |
| 316 | } |
Michal Simek | b163476 | 2023-09-05 13:30:07 +0200 | [diff] [blame] | 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | int board_late_init(void) |
| 322 | { |
| 323 | int ret; |
| 324 | |
| 325 | if (!(gd->flags & GD_FLG_ENV_DEFAULT)) { |
| 326 | debug("Saved variables - Skipping\n"); |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | if (!IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)) |
| 331 | return 0; |
| 332 | |
| 333 | if (IS_ENABLED(CONFIG_DISTRO_DEFAULTS)) { |
| 334 | ret = boot_targets_setup(); |
| 335 | if (ret) |
| 336 | return ret; |
| 337 | } |
| 338 | |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 339 | return board_late_init_xilinx(); |
| 340 | } |
| 341 | |
| 342 | int dram_init_banksize(void) |
| 343 | { |
| 344 | int ret; |
| 345 | |
| 346 | ret = fdtdec_setup_memory_banksize(); |
| 347 | if (ret) |
| 348 | return ret; |
| 349 | |
| 350 | mem_map_fill(); |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | int dram_init(void) |
| 356 | { |
| 357 | int ret; |
| 358 | |
Simon Glass | ea0ada3 | 2023-02-05 15:40:57 -0700 | [diff] [blame] | 359 | if (IS_ENABLED(CONFIG_SYS_MEM_RSVD_FOR_MMU)) |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 360 | ret = fdtdec_setup_mem_size_base(); |
| 361 | else |
| 362 | ret = fdtdec_setup_mem_size_base_lowest(); |
| 363 | |
| 364 | if (ret) |
| 365 | return -EINVAL; |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | void reset_cpu(void) |
| 371 | { |
| 372 | } |
Venkatesh Yadav Abbarapu | fc4884b | 2024-03-12 17:04:21 +0530 | [diff] [blame] | 373 | |
Michal Simek | f3a541f | 2024-03-22 12:43:17 +0100 | [diff] [blame] | 374 | #if defined(CONFIG_ENV_IS_NOWHERE) |
Venkatesh Yadav Abbarapu | fc4884b | 2024-03-12 17:04:21 +0530 | [diff] [blame] | 375 | enum env_location env_get_location(enum env_operation op, int prio) |
| 376 | { |
| 377 | u8 bootmode = versal_net_get_bootmode(); |
| 378 | |
| 379 | if (prio) |
| 380 | return ENVL_UNKNOWN; |
| 381 | |
| 382 | switch (bootmode) { |
| 383 | case EMMC_MODE: |
| 384 | case SD_MODE: |
| 385 | case SD1_LSHFT_MODE: |
| 386 | case SD_MODE1: |
| 387 | if (IS_ENABLED(CONFIG_ENV_IS_IN_FAT)) |
| 388 | return ENVL_FAT; |
| 389 | if (IS_ENABLED(CONFIG_ENV_IS_IN_EXT4)) |
| 390 | return ENVL_EXT4; |
| 391 | return ENVL_NOWHERE; |
| 392 | case OSPI_MODE: |
| 393 | case QSPI_MODE_24BIT: |
| 394 | case QSPI_MODE_32BIT: |
| 395 | if (IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH)) |
| 396 | return ENVL_SPI_FLASH; |
| 397 | return ENVL_NOWHERE; |
| 398 | case JTAG_MODE: |
| 399 | case SELECTMAP_MODE: |
| 400 | default: |
| 401 | return ENVL_NOWHERE; |
| 402 | } |
| 403 | } |
Michal Simek | f3a541f | 2024-03-22 12:43:17 +0100 | [diff] [blame] | 404 | #endif |