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