Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * AM625: SoC specific initialization |
| 4 | * |
| 5 | * Copyright (C) 2020-2022 Texas Instruments Incorporated - https://www.ti.com/ |
| 6 | * Suman Anna <s-anna@ti.com> |
| 7 | */ |
| 8 | |
| 9 | #include <spl.h> |
| 10 | #include <asm/io.h> |
| 11 | #include <asm/arch/hardware.h> |
Andrew Davis | f179985 | 2023-04-06 11:38:16 -0500 | [diff] [blame] | 12 | #include "sysfw-loader.h" |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 13 | #include "common.h" |
| 14 | #include <dm.h> |
| 15 | #include <dm/uclass-internal.h> |
| 16 | #include <dm/pinctrl.h> |
| 17 | |
Nishanth Menon | d8af3ac | 2023-05-16 18:06:21 -0500 | [diff] [blame] | 18 | #define RTC_BASE_ADDRESS 0x2b1f0000 |
| 19 | #define REG_K3RTC_S_CNT_LSW (RTC_BASE_ADDRESS + 0x18) |
| 20 | #define REG_K3RTC_KICK0 (RTC_BASE_ADDRESS + 0x70) |
| 21 | #define REG_K3RTC_KICK1 (RTC_BASE_ADDRESS + 0x74) |
| 22 | |
| 23 | /* Magic values for lock/unlock */ |
| 24 | #define K3RTC_KICK0_UNLOCK_VALUE 0x83e70b13 |
| 25 | #define K3RTC_KICK1_UNLOCK_VALUE 0x95a4f1e0 |
| 26 | |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 27 | /* |
| 28 | * This uninitialized global variable would normal end up in the .bss section, |
| 29 | * but the .bss is cleared between writing and reading this variable, so move |
| 30 | * it to the .data section. |
| 31 | */ |
| 32 | u32 bootindex __section(".data"); |
| 33 | static struct rom_extended_boot_data bootdata __section(".data"); |
| 34 | |
| 35 | static void store_boot_info_from_rom(void) |
| 36 | { |
| 37 | bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX); |
Bryan Brattlof | 270537c | 2022-11-22 13:28:11 -0600 | [diff] [blame] | 38 | memcpy(&bootdata, (uintptr_t *)ROM_EXTENDED_BOOT_DATA_INFO, |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 39 | sizeof(struct rom_extended_boot_data)); |
| 40 | } |
| 41 | |
| 42 | static void ctrl_mmr_unlock(void) |
| 43 | { |
| 44 | /* Unlock all WKUP_CTRL_MMR0 module registers */ |
| 45 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 0); |
| 46 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 1); |
| 47 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 2); |
| 48 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 3); |
| 49 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 4); |
| 50 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 5); |
| 51 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 6); |
| 52 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 7); |
| 53 | |
| 54 | /* Unlock all CTRL_MMR0 module registers */ |
| 55 | mmr_unlock(CTRL_MMR0_BASE, 0); |
| 56 | mmr_unlock(CTRL_MMR0_BASE, 1); |
| 57 | mmr_unlock(CTRL_MMR0_BASE, 2); |
| 58 | mmr_unlock(CTRL_MMR0_BASE, 4); |
| 59 | mmr_unlock(CTRL_MMR0_BASE, 6); |
| 60 | |
| 61 | /* Unlock all MCU_CTRL_MMR0 module registers */ |
| 62 | mmr_unlock(MCU_CTRL_MMR0_BASE, 0); |
| 63 | mmr_unlock(MCU_CTRL_MMR0_BASE, 1); |
| 64 | mmr_unlock(MCU_CTRL_MMR0_BASE, 2); |
| 65 | mmr_unlock(MCU_CTRL_MMR0_BASE, 3); |
| 66 | mmr_unlock(MCU_CTRL_MMR0_BASE, 4); |
| 67 | mmr_unlock(MCU_CTRL_MMR0_BASE, 6); |
| 68 | |
| 69 | /* Unlock PADCFG_CTRL_MMR padconf registers */ |
| 70 | mmr_unlock(PADCFG_MMR0_BASE, 1); |
| 71 | mmr_unlock(PADCFG_MMR1_BASE, 1); |
| 72 | } |
| 73 | |
Julien Panis | 01b00d4 | 2022-07-01 14:30:11 +0200 | [diff] [blame] | 74 | static __maybe_unused void enable_mcu_esm_reset(void) |
| 75 | { |
| 76 | /* Set CTRLMMR_MCU_RST_CTRL:MCU_ESM_ERROR_RST_EN_Z to '0' (low active) */ |
| 77 | u32 stat = readl(CTRLMMR_MCU_RST_CTRL); |
| 78 | |
| 79 | stat &= RST_CTRL_ESM_ERROR_RST_EN_Z_MASK; |
| 80 | writel(stat, CTRLMMR_MCU_RST_CTRL); |
| 81 | } |
Nishanth Menon | d8af3ac | 2023-05-16 18:06:21 -0500 | [diff] [blame] | 82 | |
| 83 | #if defined(CONFIG_CPU_V7R) |
| 84 | |
| 85 | /* |
| 86 | * RTC Erratum i2327 Workaround for Silicon Revision 1 |
| 87 | * |
| 88 | * Due to a bug in initial synchronization out of cold power on, |
| 89 | * IRQ status can get locked infinitely if we do not unlock RTC |
| 90 | * |
| 91 | * This workaround *must* be applied within 1 second of power on, |
| 92 | * So, this is closest point to be able to guarantee the max |
| 93 | * timing. |
| 94 | * |
| 95 | * https://www.ti.com/lit/er/sprz487c/sprz487c.pdf |
| 96 | */ |
| 97 | void rtc_erratumi2327_init(void) |
| 98 | { |
| 99 | u32 counter; |
| 100 | |
| 101 | /* |
| 102 | * If counter has gone past 1, nothing we can do, leave |
| 103 | * system locked! This is the only way we know if RTC |
| 104 | * can be used for all practical purposes. |
| 105 | */ |
| 106 | counter = readl(REG_K3RTC_S_CNT_LSW); |
| 107 | if (counter > 1) |
| 108 | return; |
| 109 | /* |
| 110 | * Need to set this up at the very start |
| 111 | * MUST BE DONE under 1 second of boot. |
| 112 | */ |
| 113 | writel(K3RTC_KICK0_UNLOCK_VALUE, REG_K3RTC_KICK0); |
| 114 | writel(K3RTC_KICK1_UNLOCK_VALUE, REG_K3RTC_KICK1); |
| 115 | return; |
| 116 | } |
| 117 | #endif |
Julien Panis | 01b00d4 | 2022-07-01 14:30:11 +0200 | [diff] [blame] | 118 | |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 119 | void board_init_f(ulong dummy) |
| 120 | { |
| 121 | struct udevice *dev; |
| 122 | int ret; |
| 123 | |
| 124 | #if defined(CONFIG_CPU_V7R) |
| 125 | setup_k3_mpu_regions(); |
Nishanth Menon | d8af3ac | 2023-05-16 18:06:21 -0500 | [diff] [blame] | 126 | rtc_erratumi2327_init(); |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 127 | #endif |
| 128 | |
| 129 | /* |
| 130 | * Cannot delay this further as there is a chance that |
| 131 | * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section. |
| 132 | */ |
| 133 | store_boot_info_from_rom(); |
| 134 | |
| 135 | ctrl_mmr_unlock(); |
| 136 | |
| 137 | /* Init DM early */ |
| 138 | spl_early_init(); |
| 139 | |
| 140 | /* |
| 141 | * Process pinctrl for the serial0 and serial3, aka WKUP_UART0 and |
| 142 | * MAIN_UART1 modules and continue regardless of the result of pinctrl. |
| 143 | * Do this without probing the device, but instead by searching the |
| 144 | * device that would request the given sequence number if probed. The |
| 145 | * UARTs will be used by the DM firmware and TIFS firmware images |
| 146 | * respectively and the firmware depend on SPL to initialize the pin |
| 147 | * settings. |
| 148 | */ |
| 149 | ret = uclass_find_device_by_seq(UCLASS_SERIAL, 0, &dev); |
| 150 | if (!ret) |
| 151 | pinctrl_select_state(dev, "default"); |
| 152 | |
| 153 | ret = uclass_find_device_by_seq(UCLASS_SERIAL, 3, &dev); |
| 154 | if (!ret) |
| 155 | pinctrl_select_state(dev, "default"); |
| 156 | |
| 157 | preloader_console_init(); |
| 158 | |
| 159 | #ifdef CONFIG_K3_EARLY_CONS |
| 160 | /* |
| 161 | * Allow establishing an early console as required for example when |
| 162 | * doing a UART-based boot. Note that this console may not "survive" |
| 163 | * through a SYSFW PM-init step and will need a re-init in some way |
| 164 | * due to changing module clock frequencies. |
| 165 | */ |
| 166 | early_console_init(); |
| 167 | #endif |
| 168 | |
| 169 | #if defined(CONFIG_K3_LOAD_SYSFW) |
| 170 | /* |
| 171 | * Configure and start up system controller firmware. Provide |
| 172 | * the U-Boot console init function to the SYSFW post-PM configuration |
| 173 | * callback hook, effectively switching on (or over) the console |
| 174 | * output. |
| 175 | */ |
| 176 | ret = is_rom_loaded_sysfw(&bootdata); |
| 177 | if (!ret) |
| 178 | panic("ROM has not loaded TIFS firmware\n"); |
| 179 | |
| 180 | k3_sysfw_loader(true, NULL, NULL); |
| 181 | #endif |
| 182 | |
| 183 | /* |
| 184 | * Force probe of clk_k3 driver here to ensure basic default clock |
| 185 | * configuration is always done. |
| 186 | */ |
| 187 | if (IS_ENABLED(CONFIG_SPL_CLK_K3)) { |
| 188 | ret = uclass_get_device_by_driver(UCLASS_CLK, |
| 189 | DM_DRIVER_GET(ti_clk), |
| 190 | &dev); |
| 191 | if (ret) |
| 192 | printf("Failed to initialize clk-k3!\n"); |
| 193 | } |
| 194 | |
| 195 | /* Output System Firmware version info */ |
| 196 | k3_sysfw_print_ver(); |
| 197 | |
Julien Panis | 01b00d4 | 2022-07-01 14:30:11 +0200 | [diff] [blame] | 198 | if (IS_ENABLED(CONFIG_ESM_K3)) { |
| 199 | /* Probe/configure ESM0 */ |
| 200 | ret = uclass_get_device_by_name(UCLASS_MISC, "esm@420000", &dev); |
| 201 | if (ret) |
| 202 | printf("esm main init failed: %d\n", ret); |
| 203 | |
| 204 | /* Probe/configure MCUESM */ |
| 205 | ret = uclass_get_device_by_name(UCLASS_MISC, "esm@4100000", &dev); |
| 206 | if (ret) |
| 207 | printf("esm mcu init failed: %d\n", ret); |
| 208 | |
| 209 | enable_mcu_esm_reset(); |
| 210 | } |
| 211 | |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 212 | #if defined(CONFIG_K3_AM64_DDRSS) |
| 213 | ret = uclass_get_device(UCLASS_RAM, 0, &dev); |
| 214 | if (ret) |
| 215 | panic("DRAM init failed: %d\n", ret); |
| 216 | #endif |
| 217 | } |
| 218 | |
| 219 | u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device) |
| 220 | { |
| 221 | u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT); |
Martyn Welch | 5713c62 | 2022-12-20 18:38:18 +0000 | [diff] [blame] | 222 | u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >> |
| 223 | MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT; |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 224 | u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >> |
| 225 | MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT; |
| 226 | |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 227 | |
Martyn Welch | 5713c62 | 2022-12-20 18:38:18 +0000 | [diff] [blame] | 228 | switch (bootmode) { |
| 229 | case BOOT_DEVICE_EMMC: |
| 230 | return MMCSD_MODE_EMMCBOOT; |
| 231 | case BOOT_DEVICE_MMC: |
| 232 | if (bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_FS_RAW_MASK) |
| 233 | return MMCSD_MODE_RAW; |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 234 | default: |
Martyn Welch | 5713c62 | 2022-12-20 18:38:18 +0000 | [diff] [blame] | 235 | return MMCSD_MODE_FS; |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | static u32 __get_backup_bootmedia(u32 devstat) |
| 240 | { |
| 241 | u32 bkup_bootmode = (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_MASK) >> |
| 242 | MAIN_DEVSTAT_BACKUP_BOOTMODE_SHIFT; |
| 243 | u32 bkup_bootmode_cfg = |
| 244 | (devstat & MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_MASK) >> |
| 245 | MAIN_DEVSTAT_BACKUP_BOOTMODE_CFG_SHIFT; |
| 246 | |
| 247 | switch (bkup_bootmode) { |
| 248 | case BACKUP_BOOT_DEVICE_UART: |
| 249 | return BOOT_DEVICE_UART; |
| 250 | |
| 251 | case BACKUP_BOOT_DEVICE_USB: |
| 252 | return BOOT_DEVICE_USB; |
| 253 | |
| 254 | case BACKUP_BOOT_DEVICE_ETHERNET: |
| 255 | return BOOT_DEVICE_ETHERNET; |
| 256 | |
| 257 | case BACKUP_BOOT_DEVICE_MMC: |
| 258 | if (bkup_bootmode_cfg) |
| 259 | return BOOT_DEVICE_MMC2; |
| 260 | return BOOT_DEVICE_MMC1; |
| 261 | |
| 262 | case BACKUP_BOOT_DEVICE_SPI: |
| 263 | return BOOT_DEVICE_SPI; |
| 264 | |
| 265 | case BACKUP_BOOT_DEVICE_I2C: |
| 266 | return BOOT_DEVICE_I2C; |
| 267 | |
| 268 | case BACKUP_BOOT_DEVICE_DFU: |
| 269 | if (bkup_bootmode_cfg & MAIN_DEVSTAT_BACKUP_USB_MODE_MASK) |
| 270 | return BOOT_DEVICE_USB; |
| 271 | return BOOT_DEVICE_DFU; |
| 272 | }; |
| 273 | |
| 274 | return BOOT_DEVICE_RAM; |
| 275 | } |
| 276 | |
| 277 | static u32 __get_primary_bootmedia(u32 devstat) |
| 278 | { |
| 279 | u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >> |
| 280 | MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT; |
| 281 | u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >> |
| 282 | MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT; |
| 283 | |
| 284 | switch (bootmode) { |
| 285 | case BOOT_DEVICE_OSPI: |
| 286 | fallthrough; |
| 287 | case BOOT_DEVICE_QSPI: |
| 288 | fallthrough; |
| 289 | case BOOT_DEVICE_XSPI: |
| 290 | fallthrough; |
| 291 | case BOOT_DEVICE_SPI: |
| 292 | return BOOT_DEVICE_SPI; |
| 293 | |
| 294 | case BOOT_DEVICE_ETHERNET_RGMII: |
| 295 | fallthrough; |
| 296 | case BOOT_DEVICE_ETHERNET_RMII: |
| 297 | return BOOT_DEVICE_ETHERNET; |
| 298 | |
| 299 | case BOOT_DEVICE_EMMC: |
| 300 | return BOOT_DEVICE_MMC1; |
| 301 | |
| 302 | case BOOT_DEVICE_MMC: |
| 303 | if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_PORT_MASK) >> |
| 304 | MAIN_DEVSTAT_PRIMARY_MMC_PORT_SHIFT) |
| 305 | return BOOT_DEVICE_MMC2; |
| 306 | return BOOT_DEVICE_MMC1; |
| 307 | |
| 308 | case BOOT_DEVICE_DFU: |
| 309 | if ((bootmode_cfg & MAIN_DEVSTAT_PRIMARY_USB_MODE_MASK) >> |
| 310 | MAIN_DEVSTAT_PRIMARY_USB_MODE_SHIFT) |
| 311 | return BOOT_DEVICE_USB; |
| 312 | return BOOT_DEVICE_DFU; |
| 313 | |
| 314 | case BOOT_DEVICE_NOBOOT: |
| 315 | return BOOT_DEVICE_RAM; |
| 316 | } |
| 317 | |
| 318 | return bootmode; |
| 319 | } |
| 320 | |
| 321 | u32 spl_boot_device(void) |
| 322 | { |
| 323 | u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT); |
| 324 | u32 bootmedia; |
| 325 | |
| 326 | if (bootindex == K3_PRIMARY_BOOTMODE) |
| 327 | bootmedia = __get_primary_bootmedia(devstat); |
| 328 | else |
| 329 | bootmedia = __get_backup_bootmedia(devstat); |
| 330 | |
| 331 | debug("am625_init: %s: devstat = 0x%x bootmedia = 0x%x bootindex = %d\n", |
| 332 | __func__, devstat, bootmedia, bootindex); |
| 333 | |
| 334 | return bootmedia; |
| 335 | } |