Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * AM642: SoC specific initialization |
| 4 | * |
| 5 | * Copyright (C) 2020-2021 Texas Instruments Incorporated - https://www.ti.com/ |
| 6 | * Keerthy <j-keerthy@ti.com> |
| 7 | * Dave Gerlach <d-gerlach@ti.com> |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Aswath Govindraju | 70bc5e3 | 2021-06-04 22:00:33 +0530 | [diff] [blame] | 11 | #include <fdt_support.h> |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 12 | #include <spl.h> |
| 13 | #include <asm/io.h> |
Keerthy | 05d670e | 2021-04-23 11:27:33 -0500 | [diff] [blame] | 14 | #include <asm/arch/hardware.h> |
Dave Gerlach | 8e0689b | 2021-04-23 11:27:36 -0500 | [diff] [blame] | 15 | #include <asm/arch/sysfw-loader.h> |
| 16 | #include <asm/arch/sys_proto.h> |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 17 | #include "common.h" |
Dave Gerlach | 8e0689b | 2021-04-23 11:27:36 -0500 | [diff] [blame] | 18 | #include <asm/arch/sys_proto.h> |
| 19 | #include <linux/soc/ti/ti_sci_protocol.h> |
| 20 | #include <dm.h> |
| 21 | #include <dm/uclass-internal.h> |
| 22 | #include <dm/pinctrl.h> |
Dave Gerlach | a89f7a4 | 2021-04-23 11:27:37 -0500 | [diff] [blame] | 23 | #include <mmc.h> |
Lokesh Vutla | 17ad6e0 | 2021-05-06 16:44:52 +0530 | [diff] [blame] | 24 | #include <dm/root.h> |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 25 | |
| 26 | #if defined(CONFIG_SPL_BUILD) |
Hari Nagalla | d383c5e | 2022-03-09 14:42:30 -0600 | [diff] [blame^] | 27 | #define MCU_CTRL_MMR0_BASE 0x04500000 |
| 28 | #define CTRLMMR_MCU_RST_CTRL 0x04518170 |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 29 | |
Dave Gerlach | eaef129 | 2021-04-23 11:27:34 -0500 | [diff] [blame] | 30 | static void ctrl_mmr_unlock(void) |
| 31 | { |
| 32 | /* Unlock all PADCFG_MMR1 module registers */ |
| 33 | mmr_unlock(PADCFG_MMR1_BASE, 1); |
| 34 | |
Hari Nagalla | d383c5e | 2022-03-09 14:42:30 -0600 | [diff] [blame^] | 35 | /* Unlock all MCU_CTRL_MMR0 module registers */ |
| 36 | mmr_unlock(MCU_CTRL_MMR0_BASE, 0); |
| 37 | mmr_unlock(MCU_CTRL_MMR0_BASE, 1); |
| 38 | mmr_unlock(MCU_CTRL_MMR0_BASE, 2); |
| 39 | mmr_unlock(MCU_CTRL_MMR0_BASE, 3); |
| 40 | mmr_unlock(MCU_CTRL_MMR0_BASE, 4); |
| 41 | mmr_unlock(MCU_CTRL_MMR0_BASE, 6); |
| 42 | |
Dave Gerlach | eaef129 | 2021-04-23 11:27:34 -0500 | [diff] [blame] | 43 | /* Unlock all CTRL_MMR0 module registers */ |
| 44 | mmr_unlock(CTRL_MMR0_BASE, 0); |
| 45 | mmr_unlock(CTRL_MMR0_BASE, 1); |
| 46 | mmr_unlock(CTRL_MMR0_BASE, 2); |
| 47 | mmr_unlock(CTRL_MMR0_BASE, 3); |
| 48 | mmr_unlock(CTRL_MMR0_BASE, 5); |
| 49 | mmr_unlock(CTRL_MMR0_BASE, 6); |
| 50 | } |
| 51 | |
Dave Gerlach | b27a9f2 | 2021-04-23 11:27:35 -0500 | [diff] [blame] | 52 | /* |
| 53 | * This uninitialized global variable would normal end up in the .bss section, |
| 54 | * but the .bss is cleared between writing and reading this variable, so move |
| 55 | * it to the .data section. |
| 56 | */ |
| 57 | u32 bootindex __section(".data"); |
Marek BehĂșn | 4bebdd3 | 2021-05-20 13:23:52 +0200 | [diff] [blame] | 58 | static struct rom_extended_boot_data bootdata __section(".data"); |
Dave Gerlach | b27a9f2 | 2021-04-23 11:27:35 -0500 | [diff] [blame] | 59 | |
| 60 | static void store_boot_info_from_rom(void) |
| 61 | { |
| 62 | bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX); |
| 63 | memcpy(&bootdata, (uintptr_t *)ROM_ENTENDED_BOOT_DATA_INFO, |
| 64 | sizeof(struct rom_extended_boot_data)); |
| 65 | } |
| 66 | |
Dave Gerlach | a89f7a4 | 2021-04-23 11:27:37 -0500 | [diff] [blame] | 67 | #if defined(CONFIG_K3_LOAD_SYSFW) && CONFIG_IS_ENABLED(DM_MMC) |
| 68 | void k3_mmc_stop_clock(void) |
| 69 | { |
| 70 | if (spl_boot_device() == BOOT_DEVICE_MMC1) { |
| 71 | struct mmc *mmc = find_mmc_device(0); |
| 72 | |
| 73 | if (!mmc) |
| 74 | return; |
| 75 | |
| 76 | mmc->saved_clock = mmc->clock; |
| 77 | mmc_set_clock(mmc, 0, true); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void k3_mmc_restart_clock(void) |
| 82 | { |
| 83 | if (spl_boot_device() == BOOT_DEVICE_MMC1) { |
| 84 | struct mmc *mmc = find_mmc_device(0); |
| 85 | |
| 86 | if (!mmc) |
| 87 | return; |
| 88 | |
| 89 | mmc_set_clock(mmc, mmc->saved_clock, false); |
| 90 | } |
| 91 | } |
| 92 | #else |
| 93 | void k3_mmc_stop_clock(void) {} |
| 94 | void k3_mmc_restart_clock(void) {} |
| 95 | #endif |
| 96 | |
Lokesh Vutla | 17ad6e0 | 2021-05-06 16:44:52 +0530 | [diff] [blame] | 97 | #ifdef CONFIG_SPL_OF_LIST |
| 98 | void do_dt_magic(void) |
| 99 | { |
| 100 | int ret, rescan; |
| 101 | |
| 102 | if (IS_ENABLED(CONFIG_TI_I2C_BOARD_DETECT)) |
| 103 | do_board_detect(); |
| 104 | |
| 105 | /* |
| 106 | * Board detection has been done. |
| 107 | * Let us see if another dtb wouldn't be a better match |
| 108 | * for our board |
| 109 | */ |
| 110 | if (IS_ENABLED(CONFIG_CPU_V7R)) { |
| 111 | ret = fdtdec_resetup(&rescan); |
| 112 | if (!ret && rescan) { |
| 113 | dm_uninit(); |
| 114 | dm_init_and_scan(true); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | #endif |
| 119 | |
Aswath Govindraju | 70bc5e3 | 2021-06-04 22:00:33 +0530 | [diff] [blame] | 120 | #if CONFIG_IS_ENABLED(USB_STORAGE) |
| 121 | static int fixup_usb_boot(const void *fdt_blob) |
| 122 | { |
| 123 | int ret = 0; |
| 124 | |
| 125 | switch (spl_boot_device()) { |
| 126 | case BOOT_DEVICE_USB: |
| 127 | /* |
| 128 | * If the boot mode is host, fixup the dr_mode to host |
| 129 | * before cdns3 bind takes place |
| 130 | */ |
| 131 | ret = fdt_find_and_setprop((void *)fdt_blob, |
| 132 | "/bus@f4000/cdns-usb@f900000/usb@f400000", |
| 133 | "dr_mode", "host", 5, 0); |
| 134 | if (ret) |
| 135 | printf("%s: fdt_find_and_setprop() failed:%d\n", |
| 136 | __func__, ret); |
| 137 | fallthrough; |
| 138 | default: |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | int fdtdec_board_setup(const void *fdt_blob) |
| 146 | { |
| 147 | /* Can use the pointer from the function parameters */ |
| 148 | return fixup_usb_boot(fdt_blob); |
| 149 | } |
| 150 | #endif |
| 151 | |
Hari Nagalla | d383c5e | 2022-03-09 14:42:30 -0600 | [diff] [blame^] | 152 | #if defined(CONFIG_ESM_K3) |
| 153 | static void enable_mcu_esm_reset(void) |
| 154 | { |
| 155 | /* Set CTRLMMR_MCU_RST_CTRL:MCU_ESM_ERROR_RST_EN_Z to '0' (low active) */ |
| 156 | u32 stat = readl(CTRLMMR_MCU_RST_CTRL); |
| 157 | |
| 158 | stat &= 0xFFFDFFFF; |
| 159 | writel(stat, CTRLMMR_MCU_RST_CTRL); |
| 160 | } |
| 161 | #endif |
| 162 | |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 163 | void board_init_f(ulong dummy) |
| 164 | { |
Hari Nagalla | d383c5e | 2022-03-09 14:42:30 -0600 | [diff] [blame^] | 165 | #if defined(CONFIG_K3_LOAD_SYSFW) || defined(CONFIG_K3_AM64_DDRSS) || defined(CONFIG_ESM_K3) |
Dave Gerlach | 8e0689b | 2021-04-23 11:27:36 -0500 | [diff] [blame] | 166 | struct udevice *dev; |
| 167 | int ret; |
| 168 | #endif |
| 169 | |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 170 | #if defined(CONFIG_CPU_V7R) |
| 171 | setup_k3_mpu_regions(); |
| 172 | #endif |
| 173 | |
Dave Gerlach | b27a9f2 | 2021-04-23 11:27:35 -0500 | [diff] [blame] | 174 | /* |
| 175 | * Cannot delay this further as there is a chance that |
| 176 | * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section. |
| 177 | */ |
| 178 | store_boot_info_from_rom(); |
| 179 | |
Dave Gerlach | eaef129 | 2021-04-23 11:27:34 -0500 | [diff] [blame] | 180 | ctrl_mmr_unlock(); |
| 181 | |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 182 | /* Init DM early */ |
| 183 | spl_early_init(); |
| 184 | |
| 185 | preloader_console_init(); |
Dave Gerlach | 8e0689b | 2021-04-23 11:27:36 -0500 | [diff] [blame] | 186 | |
Lokesh Vutla | 17ad6e0 | 2021-05-06 16:44:52 +0530 | [diff] [blame] | 187 | do_dt_magic(); |
| 188 | |
Dave Gerlach | 8e0689b | 2021-04-23 11:27:36 -0500 | [diff] [blame] | 189 | #if defined(CONFIG_K3_LOAD_SYSFW) |
| 190 | /* |
| 191 | * Process pinctrl for serial3 a.k.a. MAIN UART1 module and continue |
| 192 | * regardless of the result of pinctrl. Do this without probing the |
| 193 | * device, but instead by searching the device that would request the |
| 194 | * given sequence number if probed. The UART will be used by the system |
| 195 | * firmware (SYSFW) image for various purposes and SYSFW depends on us |
| 196 | * to initialize its pin settings. |
| 197 | */ |
| 198 | ret = uclass_find_device_by_seq(UCLASS_SERIAL, 3, &dev); |
| 199 | if (!ret) |
| 200 | pinctrl_select_state(dev, "default"); |
| 201 | |
| 202 | /* |
| 203 | * Load, start up, and configure system controller firmware. |
| 204 | * This will determine whether or not ROM has already loaded |
| 205 | * system firmware and if so, will only perform needed config |
| 206 | * and not attempt to load firmware again. |
| 207 | */ |
Dave Gerlach | a89f7a4 | 2021-04-23 11:27:37 -0500 | [diff] [blame] | 208 | k3_sysfw_loader(is_rom_loaded_sysfw(&bootdata), k3_mmc_stop_clock, |
| 209 | k3_mmc_restart_clock); |
Dave Gerlach | 8e0689b | 2021-04-23 11:27:36 -0500 | [diff] [blame] | 210 | #endif |
| 211 | |
| 212 | /* Output System Firmware version info */ |
| 213 | k3_sysfw_print_ver(); |
Dave Gerlach | ad38f51 | 2021-05-04 18:00:53 -0500 | [diff] [blame] | 214 | |
Hari Nagalla | d383c5e | 2022-03-09 14:42:30 -0600 | [diff] [blame^] | 215 | #if defined(CONFIG_ESM_K3) |
| 216 | /* Probe/configure ESM0 */ |
| 217 | ret = uclass_get_device_by_name(UCLASS_MISC, "esm@420000", &dev); |
| 218 | if (ret) |
| 219 | printf("esm main init failed: %d\n", ret); |
| 220 | |
| 221 | /* Probe/configure MCUESM */ |
| 222 | ret = uclass_get_device_by_name(UCLASS_MISC, "esm@4100000", &dev); |
| 223 | if (ret) |
| 224 | printf("esm mcu init failed: %d\n", ret); |
| 225 | |
| 226 | enable_mcu_esm_reset(); |
| 227 | #endif |
| 228 | |
Dave Gerlach | ad38f51 | 2021-05-04 18:00:53 -0500 | [diff] [blame] | 229 | #if defined(CONFIG_K3_AM64_DDRSS) |
| 230 | ret = uclass_get_device(UCLASS_RAM, 0, &dev); |
| 231 | if (ret) |
| 232 | panic("DRAM init failed: %d\n", ret); |
| 233 | #endif |
Vignesh Raghavendra | 82cc43e | 2021-12-24 12:55:32 +0530 | [diff] [blame] | 234 | if (IS_ENABLED(CONFIG_SPL_ETH) && IS_ENABLED(CONFIG_TI_AM65_CPSW_NUSS) && |
| 235 | spl_boot_device() == BOOT_DEVICE_ETHERNET) { |
| 236 | struct udevice *cpswdev; |
| 237 | |
| 238 | if (uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(am65_cpsw_nuss), &cpswdev)) |
| 239 | printf("Failed to probe am65_cpsw_nuss driver\n"); |
| 240 | } |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 241 | } |
Keerthy | 05d670e | 2021-04-23 11:27:33 -0500 | [diff] [blame] | 242 | |
Aswath Govindraju | dc32ddf | 2021-07-26 20:58:02 +0530 | [diff] [blame] | 243 | u32 spl_mmc_boot_mode(const u32 boot_device) |
Keerthy | 05d670e | 2021-04-23 11:27:33 -0500 | [diff] [blame] | 244 | { |
| 245 | switch (boot_device) { |
| 246 | case BOOT_DEVICE_MMC1: |
| 247 | return MMCSD_MODE_EMMCBOOT; |
| 248 | |
| 249 | case BOOT_DEVICE_MMC2: |
| 250 | return MMCSD_MODE_FS; |
| 251 | |
| 252 | default: |
| 253 | return MMCSD_MODE_RAW; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | static u32 __get_backup_bootmedia(u32 main_devstat) |
| 258 | { |
| 259 | u32 bkup_bootmode = |
| 260 | (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >> |
| 261 | MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT; |
| 262 | u32 bkup_bootmode_cfg = |
| 263 | (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >> |
| 264 | MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT; |
| 265 | |
| 266 | switch (bkup_bootmode) { |
| 267 | case BACKUP_BOOT_DEVICE_UART: |
| 268 | return BOOT_DEVICE_UART; |
| 269 | |
Aswath Govindraju | 8a05c9a | 2021-06-04 22:00:32 +0530 | [diff] [blame] | 270 | case BACKUP_BOOT_DEVICE_DFU: |
| 271 | if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK) |
| 272 | return BOOT_DEVICE_USB; |
| 273 | return BOOT_DEVICE_DFU; |
| 274 | |
Keerthy | 05d670e | 2021-04-23 11:27:33 -0500 | [diff] [blame] | 275 | |
| 276 | case BACKUP_BOOT_DEVICE_ETHERNET: |
| 277 | return BOOT_DEVICE_ETHERNET; |
| 278 | |
| 279 | case BACKUP_BOOT_DEVICE_MMC: |
| 280 | if (bkup_bootmode_cfg) |
| 281 | return BOOT_DEVICE_MMC2; |
| 282 | return BOOT_DEVICE_MMC1; |
| 283 | |
| 284 | case BACKUP_BOOT_DEVICE_SPI: |
| 285 | return BOOT_DEVICE_SPI; |
| 286 | |
| 287 | case BACKUP_BOOT_DEVICE_I2C: |
| 288 | return BOOT_DEVICE_I2C; |
| 289 | }; |
| 290 | |
| 291 | return BOOT_DEVICE_RAM; |
| 292 | } |
| 293 | |
| 294 | static u32 __get_primary_bootmedia(u32 main_devstat) |
| 295 | { |
| 296 | u32 bootmode = (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >> |
| 297 | MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT; |
| 298 | u32 bootmode_cfg = |
| 299 | (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >> |
| 300 | MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT; |
| 301 | |
| 302 | switch (bootmode) { |
| 303 | case BOOT_DEVICE_OSPI: |
| 304 | fallthrough; |
| 305 | case BOOT_DEVICE_QSPI: |
| 306 | fallthrough; |
| 307 | case BOOT_DEVICE_XSPI: |
| 308 | fallthrough; |
| 309 | case BOOT_DEVICE_SPI: |
| 310 | return BOOT_DEVICE_SPI; |
| 311 | |
| 312 | case BOOT_DEVICE_ETHERNET_RGMII: |
| 313 | fallthrough; |
| 314 | case BOOT_DEVICE_ETHERNET_RMII: |
| 315 | return BOOT_DEVICE_ETHERNET; |
| 316 | |
| 317 | case BOOT_DEVICE_EMMC: |
| 318 | return BOOT_DEVICE_MMC1; |
| 319 | |
| 320 | case BOOT_DEVICE_MMC: |
| 321 | if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >> |
| 322 | MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT) |
| 323 | return BOOT_DEVICE_MMC2; |
| 324 | return BOOT_DEVICE_MMC1; |
| 325 | |
Aswath Govindraju | 8a05c9a | 2021-06-04 22:00:32 +0530 | [diff] [blame] | 326 | case BOOT_DEVICE_DFU: |
| 327 | if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >> |
| 328 | MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT) |
| 329 | return BOOT_DEVICE_USB; |
| 330 | return BOOT_DEVICE_DFU; |
| 331 | |
Keerthy | 05d670e | 2021-04-23 11:27:33 -0500 | [diff] [blame] | 332 | case BOOT_DEVICE_NOBOOT: |
| 333 | return BOOT_DEVICE_RAM; |
| 334 | } |
| 335 | |
| 336 | return bootmode; |
| 337 | } |
| 338 | |
| 339 | u32 spl_boot_device(void) |
| 340 | { |
| 341 | u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT); |
| 342 | |
| 343 | if (bootindex == K3_PRIMARY_BOOTMODE) |
| 344 | return __get_primary_bootmedia(devstat); |
| 345 | else |
| 346 | return __get_backup_bootmedia(devstat); |
| 347 | } |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 348 | #endif |
Suman Anna | 320c020 | 2021-04-23 11:27:38 -0500 | [diff] [blame] | 349 | |
| 350 | #if defined(CONFIG_SYS_K3_SPL_ATF) |
| 351 | |
| 352 | #define AM64X_DEV_RTI8 127 |
| 353 | #define AM64X_DEV_RTI9 128 |
| 354 | #define AM64X_DEV_R5FSS0_CORE0 121 |
| 355 | #define AM64X_DEV_R5FSS0_CORE1 122 |
| 356 | |
| 357 | void release_resources_for_core_shutdown(void) |
| 358 | { |
| 359 | struct ti_sci_handle *ti_sci = get_ti_sci_handle(); |
| 360 | struct ti_sci_dev_ops *dev_ops = &ti_sci->ops.dev_ops; |
| 361 | struct ti_sci_proc_ops *proc_ops = &ti_sci->ops.proc_ops; |
| 362 | int ret; |
| 363 | u32 i; |
| 364 | |
| 365 | const u32 put_device_ids[] = { |
| 366 | AM64X_DEV_RTI9, |
| 367 | AM64X_DEV_RTI8, |
| 368 | }; |
| 369 | |
| 370 | /* Iterate through list of devices to put (shutdown) */ |
| 371 | for (i = 0; i < ARRAY_SIZE(put_device_ids); i++) { |
| 372 | u32 id = put_device_ids[i]; |
| 373 | |
| 374 | ret = dev_ops->put_device(ti_sci, id); |
| 375 | if (ret) |
| 376 | panic("Failed to put device %u (%d)\n", id, ret); |
| 377 | } |
| 378 | |
| 379 | const u32 put_core_ids[] = { |
| 380 | AM64X_DEV_R5FSS0_CORE1, |
| 381 | AM64X_DEV_R5FSS0_CORE0, /* Handle CPU0 after CPU1 */ |
| 382 | }; |
| 383 | |
| 384 | /* Iterate through list of cores to put (shutdown) */ |
| 385 | for (i = 0; i < ARRAY_SIZE(put_core_ids); i++) { |
| 386 | u32 id = put_core_ids[i]; |
| 387 | |
| 388 | /* |
| 389 | * Queue up the core shutdown request. Note that this call |
| 390 | * needs to be followed up by an actual invocation of an WFE |
| 391 | * or WFI CPU instruction. |
| 392 | */ |
| 393 | ret = proc_ops->proc_shutdown_no_wait(ti_sci, id); |
| 394 | if (ret) |
| 395 | panic("Failed sending core %u shutdown message (%d)\n", |
| 396 | id, ret); |
| 397 | } |
| 398 | } |
| 399 | #endif |