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 | |
| 9 | #include <common.h> |
| 10 | #include <cpu_func.h> |
| 11 | #include <fdtdec.h> |
| 12 | #include <init.h> |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 13 | #include <env_internal.h> |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 14 | #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 | |
| 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
| 32 | int board_init(void) |
| 33 | { |
| 34 | printf("EL Level:\tEL%d\n", current_el()); |
| 35 | |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | static u32 platform_id, platform_version; |
| 40 | |
| 41 | char *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 | |
| 76 | bool soc_detection(void) |
| 77 | { |
Michal Simek | ba73d6e | 2023-05-17 10:21:32 +0200 | [diff] [blame] | 78 | u32 version, ps_version; |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 79 | |
| 80 | version = readl(PMC_TAP_VERSION); |
| 81 | platform_id = FIELD_GET(PLATFORM_MASK, version); |
Michal Simek | ba73d6e | 2023-05-17 10:21:32 +0200 | [diff] [blame] | 82 | ps_version = FIELD_GET(PS_VERSION_MASK, version); |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 83 | |
| 84 | debug("idcode %x, version %x, usercode %x\n", |
| 85 | readl(PMC_TAP_IDCODE), version, |
| 86 | readl(PMC_TAP_USERCODE)); |
| 87 | |
Michal Simek | ba73d6e | 2023-05-17 10:21:32 +0200 | [diff] [blame] | 88 | debug("pmc_ver %lx, ps version %x, rtl version %lx\n", |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 89 | FIELD_GET(PMC_VERSION_MASK, version), |
Michal Simek | ba73d6e | 2023-05-17 10:21:32 +0200 | [diff] [blame] | 90 | ps_version, |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 91 | 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 Simek | ba73d6e | 2023-05-17 10:21:32 +0200 | [diff] [blame] | 97 | 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 Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 117 | } |
| 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 | |
| 125 | int 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 | |
| 136 | int board_early_init_r(void) |
| 137 | { |
Ashok Reddy Soma | 8162732 | 2023-01-10 08:44:07 +0100 | [diff] [blame] | 138 | 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 Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 182 | static 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 Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 197 | int board_late_init(void) |
| 198 | { |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 199 | 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 Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 208 | if (!(gd->flags & GD_FLG_ENV_DEFAULT)) { |
| 209 | debug("Saved variables - Skipping\n"); |
| 210 | return 0; |
| 211 | } |
| 212 | |
Simon Glass | 094778f | 2023-02-05 15:39:49 -0700 | [diff] [blame] | 213 | if (!IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)) |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 214 | return 0; |
| 215 | |
Ashok Reddy Soma | a7a5a1b | 2023-05-16 08:47:53 -0600 | [diff] [blame] | 216 | 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"); |
| 230 | mode = "xspi0"; |
| 231 | break; |
| 232 | case QSPI_MODE_32BIT: |
| 233 | puts("QSPI_MODE_32\n"); |
| 234 | mode = "xspi0"; |
| 235 | break; |
| 236 | case OSPI_MODE: |
| 237 | puts("OSPI_MODE\n"); |
| 238 | mode = "xspi0"; |
| 239 | break; |
| 240 | case EMMC_MODE: |
| 241 | puts("EMMC_MODE\n"); |
| 242 | mode = "mmc"; |
| 243 | bootseq = dev_seq(dev); |
| 244 | break; |
| 245 | case SD_MODE: |
| 246 | puts("SD_MODE\n"); |
| 247 | if (uclass_get_device_by_name(UCLASS_MMC, |
| 248 | "mmc@f1040000", &dev)) { |
| 249 | puts("Boot from SD0 but without SD0 enabled!\n"); |
| 250 | return -1; |
| 251 | } |
| 252 | debug("mmc0 device found at %p, seq %d\n", dev, dev_seq(dev)); |
| 253 | |
| 254 | mode = "mmc"; |
| 255 | bootseq = dev_seq(dev); |
| 256 | break; |
| 257 | case SD1_LSHFT_MODE: |
| 258 | puts("LVL_SHFT_"); |
| 259 | fallthrough; |
| 260 | case SD_MODE1: |
| 261 | puts("SD_MODE1\n"); |
| 262 | if (uclass_get_device_by_name(UCLASS_MMC, |
| 263 | "mmc@f1050000", &dev)) { |
| 264 | puts("Boot from SD1 but without SD1 enabled!\n"); |
| 265 | return -1; |
| 266 | } |
| 267 | debug("mmc1 device found at %p, seq %d\n", dev, dev_seq(dev)); |
| 268 | |
| 269 | mode = "mmc"; |
| 270 | bootseq = dev_seq(dev); |
| 271 | break; |
| 272 | default: |
| 273 | mode = ""; |
| 274 | printf("Invalid Boot Mode:0x%x\n", bootmode); |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | if (bootseq >= 0) { |
| 279 | bootseq_len = snprintf(NULL, 0, "%i", bootseq); |
| 280 | debug("Bootseq len: %x\n", bootseq_len); |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * One terminating char + one byte for space between mode |
| 285 | * and default boot_targets |
| 286 | */ |
| 287 | env_targets = env_get("boot_targets"); |
| 288 | if (env_targets) |
| 289 | env_targets_len = strlen(env_targets); |
| 290 | |
| 291 | new_targets = calloc(1, strlen(mode) + env_targets_len + 2 + |
| 292 | bootseq_len); |
| 293 | if (!new_targets) |
| 294 | return -ENOMEM; |
| 295 | |
| 296 | if (bootseq >= 0) |
| 297 | sprintf(new_targets, "%s%x %s", mode, bootseq, |
| 298 | env_targets ? env_targets : ""); |
| 299 | else |
| 300 | sprintf(new_targets, "%s %s", mode, |
| 301 | env_targets ? env_targets : ""); |
| 302 | |
| 303 | env_set("boot_targets", new_targets); |
| 304 | |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 305 | return board_late_init_xilinx(); |
| 306 | } |
| 307 | |
| 308 | int dram_init_banksize(void) |
| 309 | { |
| 310 | int ret; |
| 311 | |
| 312 | ret = fdtdec_setup_memory_banksize(); |
| 313 | if (ret) |
| 314 | return ret; |
| 315 | |
| 316 | mem_map_fill(); |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | int dram_init(void) |
| 322 | { |
| 323 | int ret; |
| 324 | |
Simon Glass | ea0ada3 | 2023-02-05 15:40:57 -0700 | [diff] [blame] | 325 | if (IS_ENABLED(CONFIG_SYS_MEM_RSVD_FOR_MMU)) |
Michal Simek | 2e53eb2 | 2022-09-19 14:21:02 +0200 | [diff] [blame] | 326 | ret = fdtdec_setup_mem_size_base(); |
| 327 | else |
| 328 | ret = fdtdec_setup_mem_size_base_lowest(); |
| 329 | |
| 330 | if (ret) |
| 331 | return -EINVAL; |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | void reset_cpu(void) |
| 337 | { |
| 338 | } |