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> |
| 11 | #include <spl.h> |
| 12 | #include <asm/io.h> |
Keerthy | 05d670e | 2021-04-23 11:27:33 -0500 | [diff] [blame] | 13 | #include <asm/arch/hardware.h> |
Dave Gerlach | 8e0689b | 2021-04-23 11:27:36 -0500 | [diff] [blame] | 14 | #include <asm/arch/sysfw-loader.h> |
| 15 | #include <asm/arch/sys_proto.h> |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 16 | #include "common.h" |
Dave Gerlach | 8e0689b | 2021-04-23 11:27:36 -0500 | [diff] [blame] | 17 | #include <asm/arch/sys_proto.h> |
| 18 | #include <linux/soc/ti/ti_sci_protocol.h> |
| 19 | #include <dm.h> |
| 20 | #include <dm/uclass-internal.h> |
| 21 | #include <dm/pinctrl.h> |
Dave Gerlach | a89f7a4 | 2021-04-23 11:27:37 -0500 | [diff] [blame] | 22 | #include <mmc.h> |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 23 | |
| 24 | #if defined(CONFIG_SPL_BUILD) |
| 25 | |
Dave Gerlach | eaef129 | 2021-04-23 11:27:34 -0500 | [diff] [blame] | 26 | static void ctrl_mmr_unlock(void) |
| 27 | { |
| 28 | /* Unlock all PADCFG_MMR1 module registers */ |
| 29 | mmr_unlock(PADCFG_MMR1_BASE, 1); |
| 30 | |
| 31 | /* Unlock all CTRL_MMR0 module registers */ |
| 32 | mmr_unlock(CTRL_MMR0_BASE, 0); |
| 33 | mmr_unlock(CTRL_MMR0_BASE, 1); |
| 34 | mmr_unlock(CTRL_MMR0_BASE, 2); |
| 35 | mmr_unlock(CTRL_MMR0_BASE, 3); |
| 36 | mmr_unlock(CTRL_MMR0_BASE, 5); |
| 37 | mmr_unlock(CTRL_MMR0_BASE, 6); |
| 38 | } |
| 39 | |
Dave Gerlach | b27a9f2 | 2021-04-23 11:27:35 -0500 | [diff] [blame] | 40 | /* |
| 41 | * This uninitialized global variable would normal end up in the .bss section, |
| 42 | * but the .bss is cleared between writing and reading this variable, so move |
| 43 | * it to the .data section. |
| 44 | */ |
| 45 | u32 bootindex __section(".data"); |
| 46 | static struct rom_extended_boot_data bootdata __section(.data); |
| 47 | |
| 48 | static void store_boot_info_from_rom(void) |
| 49 | { |
| 50 | bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX); |
| 51 | memcpy(&bootdata, (uintptr_t *)ROM_ENTENDED_BOOT_DATA_INFO, |
| 52 | sizeof(struct rom_extended_boot_data)); |
| 53 | } |
| 54 | |
Dave Gerlach | a89f7a4 | 2021-04-23 11:27:37 -0500 | [diff] [blame] | 55 | #if defined(CONFIG_K3_LOAD_SYSFW) && CONFIG_IS_ENABLED(DM_MMC) |
| 56 | void k3_mmc_stop_clock(void) |
| 57 | { |
| 58 | if (spl_boot_device() == BOOT_DEVICE_MMC1) { |
| 59 | struct mmc *mmc = find_mmc_device(0); |
| 60 | |
| 61 | if (!mmc) |
| 62 | return; |
| 63 | |
| 64 | mmc->saved_clock = mmc->clock; |
| 65 | mmc_set_clock(mmc, 0, true); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void k3_mmc_restart_clock(void) |
| 70 | { |
| 71 | if (spl_boot_device() == BOOT_DEVICE_MMC1) { |
| 72 | struct mmc *mmc = find_mmc_device(0); |
| 73 | |
| 74 | if (!mmc) |
| 75 | return; |
| 76 | |
| 77 | mmc_set_clock(mmc, mmc->saved_clock, false); |
| 78 | } |
| 79 | } |
| 80 | #else |
| 81 | void k3_mmc_stop_clock(void) {} |
| 82 | void k3_mmc_restart_clock(void) {} |
| 83 | #endif |
| 84 | |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 85 | void board_init_f(ulong dummy) |
| 86 | { |
Dave Gerlach | 8e0689b | 2021-04-23 11:27:36 -0500 | [diff] [blame] | 87 | #if defined(CONFIG_K3_LOAD_SYSFW) |
| 88 | struct udevice *dev; |
| 89 | int ret; |
| 90 | #endif |
| 91 | |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 92 | #if defined(CONFIG_CPU_V7R) |
| 93 | setup_k3_mpu_regions(); |
| 94 | #endif |
| 95 | |
Dave Gerlach | b27a9f2 | 2021-04-23 11:27:35 -0500 | [diff] [blame] | 96 | /* |
| 97 | * Cannot delay this further as there is a chance that |
| 98 | * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section. |
| 99 | */ |
| 100 | store_boot_info_from_rom(); |
| 101 | |
Dave Gerlach | eaef129 | 2021-04-23 11:27:34 -0500 | [diff] [blame] | 102 | ctrl_mmr_unlock(); |
| 103 | |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 104 | /* Init DM early */ |
| 105 | spl_early_init(); |
| 106 | |
| 107 | preloader_console_init(); |
Dave Gerlach | 8e0689b | 2021-04-23 11:27:36 -0500 | [diff] [blame] | 108 | |
| 109 | #if defined(CONFIG_K3_LOAD_SYSFW) |
| 110 | /* |
| 111 | * Process pinctrl for serial3 a.k.a. MAIN UART1 module and continue |
| 112 | * regardless of the result of pinctrl. Do this without probing the |
| 113 | * device, but instead by searching the device that would request the |
| 114 | * given sequence number if probed. The UART will be used by the system |
| 115 | * firmware (SYSFW) image for various purposes and SYSFW depends on us |
| 116 | * to initialize its pin settings. |
| 117 | */ |
| 118 | ret = uclass_find_device_by_seq(UCLASS_SERIAL, 3, &dev); |
| 119 | if (!ret) |
| 120 | pinctrl_select_state(dev, "default"); |
| 121 | |
| 122 | /* |
| 123 | * Load, start up, and configure system controller firmware. |
| 124 | * This will determine whether or not ROM has already loaded |
| 125 | * system firmware and if so, will only perform needed config |
| 126 | * and not attempt to load firmware again. |
| 127 | */ |
Dave Gerlach | a89f7a4 | 2021-04-23 11:27:37 -0500 | [diff] [blame] | 128 | k3_sysfw_loader(is_rom_loaded_sysfw(&bootdata), k3_mmc_stop_clock, |
| 129 | k3_mmc_restart_clock); |
Dave Gerlach | 8e0689b | 2021-04-23 11:27:36 -0500 | [diff] [blame] | 130 | #endif |
| 131 | |
| 132 | /* Output System Firmware version info */ |
| 133 | k3_sysfw_print_ver(); |
Dave Gerlach | ad38f51 | 2021-05-04 18:00:53 -0500 | [diff] [blame] | 134 | |
| 135 | #if defined(CONFIG_K3_AM64_DDRSS) |
| 136 | ret = uclass_get_device(UCLASS_RAM, 0, &dev); |
| 137 | if (ret) |
| 138 | panic("DRAM init failed: %d\n", ret); |
| 139 | #endif |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 140 | } |
Keerthy | 05d670e | 2021-04-23 11:27:33 -0500 | [diff] [blame] | 141 | |
| 142 | u32 spl_boot_mode(const u32 boot_device) |
| 143 | { |
| 144 | switch (boot_device) { |
| 145 | case BOOT_DEVICE_MMC1: |
| 146 | return MMCSD_MODE_EMMCBOOT; |
| 147 | |
| 148 | case BOOT_DEVICE_MMC2: |
| 149 | return MMCSD_MODE_FS; |
| 150 | |
| 151 | default: |
| 152 | return MMCSD_MODE_RAW; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static u32 __get_backup_bootmedia(u32 main_devstat) |
| 157 | { |
| 158 | u32 bkup_bootmode = |
| 159 | (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >> |
| 160 | MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT; |
| 161 | u32 bkup_bootmode_cfg = |
| 162 | (main_devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >> |
| 163 | MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT; |
| 164 | |
| 165 | switch (bkup_bootmode) { |
| 166 | case BACKUP_BOOT_DEVICE_UART: |
| 167 | return BOOT_DEVICE_UART; |
| 168 | |
| 169 | case BACKUP_BOOT_DEVICE_USB: |
| 170 | return BOOT_DEVICE_USB; |
| 171 | |
| 172 | case BACKUP_BOOT_DEVICE_ETHERNET: |
| 173 | return BOOT_DEVICE_ETHERNET; |
| 174 | |
| 175 | case BACKUP_BOOT_DEVICE_MMC: |
| 176 | if (bkup_bootmode_cfg) |
| 177 | return BOOT_DEVICE_MMC2; |
| 178 | return BOOT_DEVICE_MMC1; |
| 179 | |
| 180 | case BACKUP_BOOT_DEVICE_SPI: |
| 181 | return BOOT_DEVICE_SPI; |
| 182 | |
| 183 | case BACKUP_BOOT_DEVICE_I2C: |
| 184 | return BOOT_DEVICE_I2C; |
| 185 | }; |
| 186 | |
| 187 | return BOOT_DEVICE_RAM; |
| 188 | } |
| 189 | |
| 190 | static u32 __get_primary_bootmedia(u32 main_devstat) |
| 191 | { |
| 192 | u32 bootmode = (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >> |
| 193 | MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT; |
| 194 | u32 bootmode_cfg = |
| 195 | (main_devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >> |
| 196 | MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT; |
| 197 | |
| 198 | switch (bootmode) { |
| 199 | case BOOT_DEVICE_OSPI: |
| 200 | fallthrough; |
| 201 | case BOOT_DEVICE_QSPI: |
| 202 | fallthrough; |
| 203 | case BOOT_DEVICE_XSPI: |
| 204 | fallthrough; |
| 205 | case BOOT_DEVICE_SPI: |
| 206 | return BOOT_DEVICE_SPI; |
| 207 | |
| 208 | case BOOT_DEVICE_ETHERNET_RGMII: |
| 209 | fallthrough; |
| 210 | case BOOT_DEVICE_ETHERNET_RMII: |
| 211 | return BOOT_DEVICE_ETHERNET; |
| 212 | |
| 213 | case BOOT_DEVICE_EMMC: |
| 214 | return BOOT_DEVICE_MMC1; |
| 215 | |
| 216 | case BOOT_DEVICE_MMC: |
| 217 | if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >> |
| 218 | MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT) |
| 219 | return BOOT_DEVICE_MMC2; |
| 220 | return BOOT_DEVICE_MMC1; |
| 221 | |
| 222 | case BOOT_DEVICE_NOBOOT: |
| 223 | return BOOT_DEVICE_RAM; |
| 224 | } |
| 225 | |
| 226 | return bootmode; |
| 227 | } |
| 228 | |
| 229 | u32 spl_boot_device(void) |
| 230 | { |
| 231 | u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT); |
| 232 | |
| 233 | if (bootindex == K3_PRIMARY_BOOTMODE) |
| 234 | return __get_primary_bootmedia(devstat); |
| 235 | else |
| 236 | return __get_backup_bootmedia(devstat); |
| 237 | } |
Dave Gerlach | 96571ec | 2021-04-23 11:27:32 -0500 | [diff] [blame] | 238 | #endif |
Suman Anna | 320c020 | 2021-04-23 11:27:38 -0500 | [diff] [blame] | 239 | |
| 240 | #if defined(CONFIG_SYS_K3_SPL_ATF) |
| 241 | |
| 242 | #define AM64X_DEV_RTI8 127 |
| 243 | #define AM64X_DEV_RTI9 128 |
| 244 | #define AM64X_DEV_R5FSS0_CORE0 121 |
| 245 | #define AM64X_DEV_R5FSS0_CORE1 122 |
| 246 | |
| 247 | void release_resources_for_core_shutdown(void) |
| 248 | { |
| 249 | struct ti_sci_handle *ti_sci = get_ti_sci_handle(); |
| 250 | struct ti_sci_dev_ops *dev_ops = &ti_sci->ops.dev_ops; |
| 251 | struct ti_sci_proc_ops *proc_ops = &ti_sci->ops.proc_ops; |
| 252 | int ret; |
| 253 | u32 i; |
| 254 | |
| 255 | const u32 put_device_ids[] = { |
| 256 | AM64X_DEV_RTI9, |
| 257 | AM64X_DEV_RTI8, |
| 258 | }; |
| 259 | |
| 260 | /* Iterate through list of devices to put (shutdown) */ |
| 261 | for (i = 0; i < ARRAY_SIZE(put_device_ids); i++) { |
| 262 | u32 id = put_device_ids[i]; |
| 263 | |
| 264 | ret = dev_ops->put_device(ti_sci, id); |
| 265 | if (ret) |
| 266 | panic("Failed to put device %u (%d)\n", id, ret); |
| 267 | } |
| 268 | |
| 269 | const u32 put_core_ids[] = { |
| 270 | AM64X_DEV_R5FSS0_CORE1, |
| 271 | AM64X_DEV_R5FSS0_CORE0, /* Handle CPU0 after CPU1 */ |
| 272 | }; |
| 273 | |
| 274 | /* Iterate through list of cores to put (shutdown) */ |
| 275 | for (i = 0; i < ARRAY_SIZE(put_core_ids); i++) { |
| 276 | u32 id = put_core_ids[i]; |
| 277 | |
| 278 | /* |
| 279 | * Queue up the core shutdown request. Note that this call |
| 280 | * needs to be followed up by an actual invocation of an WFE |
| 281 | * or WFI CPU instruction. |
| 282 | */ |
| 283 | ret = proc_ops->proc_shutdown_no_wait(ti_sci, id); |
| 284 | if (ret) |
| 285 | panic("Failed sending core %u shutdown message (%d)\n", |
| 286 | id, ret); |
| 287 | } |
| 288 | } |
| 289 | #endif |