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> |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 12 | #include <dm.h> |
| 13 | #include <dm/uclass-internal.h> |
| 14 | #include <dm/pinctrl.h> |
Joao Paulo Goncalves | 908ee7b | 2024-03-20 09:16:32 -0300 | [diff] [blame] | 15 | #include <dm/ofnode.h> |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 16 | |
Andrew Davis | 336b079 | 2024-05-10 15:21:24 -0500 | [diff] [blame] | 17 | #include "../sysfw-loader.h" |
| 18 | #include "../common.h" |
| 19 | |
Nishanth Menon | d8af3ac | 2023-05-16 18:06:21 -0500 | [diff] [blame] | 20 | #define RTC_BASE_ADDRESS 0x2b1f0000 |
| 21 | #define REG_K3RTC_S_CNT_LSW (RTC_BASE_ADDRESS + 0x18) |
| 22 | #define REG_K3RTC_KICK0 (RTC_BASE_ADDRESS + 0x70) |
| 23 | #define REG_K3RTC_KICK1 (RTC_BASE_ADDRESS + 0x74) |
| 24 | |
| 25 | /* Magic values for lock/unlock */ |
| 26 | #define K3RTC_KICK0_UNLOCK_VALUE 0x83e70b13 |
| 27 | #define K3RTC_KICK1_UNLOCK_VALUE 0x95a4f1e0 |
| 28 | |
Joao Paulo Goncalves | 908ee7b | 2024-03-20 09:16:32 -0300 | [diff] [blame] | 29 | /* TISCI DEV ID for A53 Clock */ |
| 30 | #define AM62X_DEV_A53SS0_CORE_0_DEV_ID 135 |
| 31 | |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 32 | /* |
| 33 | * This uninitialized global variable would normal end up in the .bss section, |
| 34 | * but the .bss is cleared between writing and reading this variable, so move |
| 35 | * it to the .data section. |
| 36 | */ |
| 37 | u32 bootindex __section(".data"); |
| 38 | static struct rom_extended_boot_data bootdata __section(".data"); |
| 39 | |
| 40 | static void store_boot_info_from_rom(void) |
| 41 | { |
| 42 | bootindex = *(u32 *)(CONFIG_SYS_K3_BOOT_PARAM_TABLE_INDEX); |
Bryan Brattlof | 270537c | 2022-11-22 13:28:11 -0600 | [diff] [blame] | 43 | memcpy(&bootdata, (uintptr_t *)ROM_EXTENDED_BOOT_DATA_INFO, |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 44 | sizeof(struct rom_extended_boot_data)); |
| 45 | } |
| 46 | |
| 47 | static void ctrl_mmr_unlock(void) |
| 48 | { |
| 49 | /* Unlock all WKUP_CTRL_MMR0 module registers */ |
| 50 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 0); |
| 51 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 1); |
| 52 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 2); |
| 53 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 3); |
| 54 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 4); |
| 55 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 5); |
| 56 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 6); |
| 57 | mmr_unlock(WKUP_CTRL_MMR0_BASE, 7); |
| 58 | |
| 59 | /* Unlock all CTRL_MMR0 module registers */ |
| 60 | mmr_unlock(CTRL_MMR0_BASE, 0); |
| 61 | mmr_unlock(CTRL_MMR0_BASE, 1); |
| 62 | mmr_unlock(CTRL_MMR0_BASE, 2); |
| 63 | mmr_unlock(CTRL_MMR0_BASE, 4); |
| 64 | mmr_unlock(CTRL_MMR0_BASE, 6); |
| 65 | |
| 66 | /* Unlock all MCU_CTRL_MMR0 module registers */ |
| 67 | mmr_unlock(MCU_CTRL_MMR0_BASE, 0); |
| 68 | mmr_unlock(MCU_CTRL_MMR0_BASE, 1); |
| 69 | mmr_unlock(MCU_CTRL_MMR0_BASE, 2); |
| 70 | mmr_unlock(MCU_CTRL_MMR0_BASE, 3); |
| 71 | mmr_unlock(MCU_CTRL_MMR0_BASE, 4); |
| 72 | mmr_unlock(MCU_CTRL_MMR0_BASE, 6); |
| 73 | |
| 74 | /* Unlock PADCFG_CTRL_MMR padconf registers */ |
| 75 | mmr_unlock(PADCFG_MMR0_BASE, 1); |
| 76 | mmr_unlock(PADCFG_MMR1_BASE, 1); |
| 77 | } |
| 78 | |
Julien Panis | 01b00d4 | 2022-07-01 14:30:11 +0200 | [diff] [blame] | 79 | static __maybe_unused void enable_mcu_esm_reset(void) |
| 80 | { |
| 81 | /* Set CTRLMMR_MCU_RST_CTRL:MCU_ESM_ERROR_RST_EN_Z to '0' (low active) */ |
| 82 | u32 stat = readl(CTRLMMR_MCU_RST_CTRL); |
| 83 | |
| 84 | stat &= RST_CTRL_ESM_ERROR_RST_EN_Z_MASK; |
| 85 | writel(stat, CTRLMMR_MCU_RST_CTRL); |
| 86 | } |
Nishanth Menon | d8af3ac | 2023-05-16 18:06:21 -0500 | [diff] [blame] | 87 | |
Nishanth Menon | d8af3ac | 2023-05-16 18:06:21 -0500 | [diff] [blame] | 88 | /* |
| 89 | * RTC Erratum i2327 Workaround for Silicon Revision 1 |
| 90 | * |
| 91 | * Due to a bug in initial synchronization out of cold power on, |
| 92 | * IRQ status can get locked infinitely if we do not unlock RTC |
| 93 | * |
| 94 | * This workaround *must* be applied within 1 second of power on, |
| 95 | * So, this is closest point to be able to guarantee the max |
| 96 | * timing. |
| 97 | * |
| 98 | * https://www.ti.com/lit/er/sprz487c/sprz487c.pdf |
| 99 | */ |
Nishanth Menon | d654f68 | 2023-08-25 13:02:58 -0500 | [diff] [blame] | 100 | static __maybe_unused void rtc_erratumi2327_init(void) |
Nishanth Menon | d8af3ac | 2023-05-16 18:06:21 -0500 | [diff] [blame] | 101 | { |
| 102 | u32 counter; |
| 103 | |
| 104 | /* |
| 105 | * If counter has gone past 1, nothing we can do, leave |
| 106 | * system locked! This is the only way we know if RTC |
| 107 | * can be used for all practical purposes. |
| 108 | */ |
| 109 | counter = readl(REG_K3RTC_S_CNT_LSW); |
| 110 | if (counter > 1) |
| 111 | return; |
| 112 | /* |
| 113 | * Need to set this up at the very start |
| 114 | * MUST BE DONE under 1 second of boot. |
| 115 | */ |
| 116 | writel(K3RTC_KICK0_UNLOCK_VALUE, REG_K3RTC_KICK0); |
| 117 | writel(K3RTC_KICK1_UNLOCK_VALUE, REG_K3RTC_KICK1); |
Nishanth Menon | d8af3ac | 2023-05-16 18:06:21 -0500 | [diff] [blame] | 118 | } |
Joao Paulo Goncalves | 908ee7b | 2024-03-20 09:16:32 -0300 | [diff] [blame] | 119 | |
| 120 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 121 | static int get_a53_cpu_clock_index(ofnode node) |
| 122 | { |
| 123 | int count, i; |
| 124 | struct ofnode_phandle_args *args; |
| 125 | ofnode clknode; |
| 126 | |
| 127 | clknode = ofnode_path("/bus@f0000/system-controller@44043000/clock-controller"); |
| 128 | if (!ofnode_valid(clknode)) |
| 129 | return -1; |
| 130 | |
| 131 | count = ofnode_count_phandle_with_args(node, "assigned-clocks", "#clock-cells", 0); |
| 132 | |
| 133 | for (i = 0; i < count; i++) { |
| 134 | if (!ofnode_parse_phandle_with_args(node, "assigned-clocks", |
| 135 | "#clock-cells", 0, i, args)) { |
| 136 | if (ofnode_equal(clknode, args->node) && |
| 137 | args->args[0] == AM62X_DEV_A53SS0_CORE_0_DEV_ID) |
| 138 | return i; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | static void fixup_a53_cpu_freq_by_speed_grade(void) |
| 146 | { |
| 147 | int index, size; |
| 148 | u32 *rates; |
| 149 | ofnode node; |
| 150 | |
| 151 | node = ofnode_path("/a53@0"); |
| 152 | if (!ofnode_valid(node)) |
| 153 | return; |
| 154 | |
| 155 | rates = fdt_getprop_w(ofnode_to_fdt(node), ofnode_to_offset(node), |
| 156 | "assigned-clock-rates", &size); |
| 157 | |
| 158 | index = get_a53_cpu_clock_index(node); |
| 159 | |
| 160 | if (!rates || index < 0 || index >= (size / sizeof(u32))) { |
| 161 | printf("Wrong A53 assigned-clocks configuration\n"); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | rates[index] = cpu_to_fdt32(k3_get_a53_max_frequency()); |
| 166 | |
| 167 | printf("Changed A53 CPU frequency to %dHz (%c grade) in DT\n", |
| 168 | k3_get_a53_max_frequency(), k3_get_speed_grade()); |
| 169 | } |
| 170 | #else |
| 171 | static void fixup_a53_cpu_freq_by_speed_grade(void) |
| 172 | { |
| 173 | } |
| 174 | #endif |
Julien Panis | 01b00d4 | 2022-07-01 14:30:11 +0200 | [diff] [blame] | 175 | |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 176 | void board_init_f(ulong dummy) |
| 177 | { |
| 178 | struct udevice *dev; |
| 179 | int ret; |
| 180 | |
Nishanth Menon | 3bb13ba | 2023-08-25 13:02:57 -0500 | [diff] [blame] | 181 | if (IS_ENABLED(CONFIG_CPU_V7R)) { |
| 182 | setup_k3_mpu_regions(); |
| 183 | rtc_erratumi2327_init(); |
| 184 | } |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * Cannot delay this further as there is a chance that |
| 188 | * K3_BOOT_PARAM_TABLE_INDEX can be over written by SPL MALLOC section. |
| 189 | */ |
| 190 | store_boot_info_from_rom(); |
| 191 | |
| 192 | ctrl_mmr_unlock(); |
| 193 | |
| 194 | /* Init DM early */ |
| 195 | spl_early_init(); |
| 196 | |
| 197 | /* |
| 198 | * Process pinctrl for the serial0 and serial3, aka WKUP_UART0 and |
| 199 | * MAIN_UART1 modules and continue regardless of the result of pinctrl. |
| 200 | * Do this without probing the device, but instead by searching the |
| 201 | * device that would request the given sequence number if probed. The |
| 202 | * UARTs will be used by the DM firmware and TIFS firmware images |
| 203 | * respectively and the firmware depend on SPL to initialize the pin |
| 204 | * settings. |
| 205 | */ |
| 206 | ret = uclass_find_device_by_seq(UCLASS_SERIAL, 0, &dev); |
| 207 | if (!ret) |
| 208 | pinctrl_select_state(dev, "default"); |
| 209 | |
| 210 | ret = uclass_find_device_by_seq(UCLASS_SERIAL, 3, &dev); |
| 211 | if (!ret) |
| 212 | pinctrl_select_state(dev, "default"); |
| 213 | |
| 214 | preloader_console_init(); |
| 215 | |
Wadim Egorov | 51b71d4 | 2024-05-22 09:55:03 +0200 | [diff] [blame^] | 216 | do_board_detect(); |
| 217 | |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 218 | /* |
| 219 | * Allow establishing an early console as required for example when |
| 220 | * doing a UART-based boot. Note that this console may not "survive" |
| 221 | * through a SYSFW PM-init step and will need a re-init in some way |
| 222 | * due to changing module clock frequencies. |
| 223 | */ |
Nishanth Menon | 3bb13ba | 2023-08-25 13:02:57 -0500 | [diff] [blame] | 224 | if (IS_ENABLED(CONFIG_K3_EARLY_CONS)) |
| 225 | early_console_init(); |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 226 | |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 227 | /* |
| 228 | * Configure and start up system controller firmware. Provide |
| 229 | * the U-Boot console init function to the SYSFW post-PM configuration |
| 230 | * callback hook, effectively switching on (or over) the console |
| 231 | * output. |
| 232 | */ |
Nishanth Menon | 3bb13ba | 2023-08-25 13:02:57 -0500 | [diff] [blame] | 233 | if (IS_ENABLED(CONFIG_K3_LOAD_SYSFW)) { |
| 234 | ret = is_rom_loaded_sysfw(&bootdata); |
| 235 | if (!ret) |
| 236 | panic("ROM has not loaded TIFS firmware\n"); |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 237 | |
Nishanth Menon | 3bb13ba | 2023-08-25 13:02:57 -0500 | [diff] [blame] | 238 | k3_sysfw_loader(true, NULL, NULL); |
| 239 | } |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 240 | |
| 241 | /* |
Wadim Egorov | a85a8fa | 2024-04-03 15:59:09 +0200 | [diff] [blame] | 242 | * Relocate boot information to OCRAM (after TIFS has opend this |
| 243 | * region for us) so the next bootloader stages can keep access to |
| 244 | * primary vs backup bootmodes. |
| 245 | */ |
| 246 | if (IS_ENABLED(CONFIG_CPU_V7R)) |
| 247 | writel(bootindex, K3_BOOT_PARAM_TABLE_INDEX_OCRAM); |
| 248 | |
| 249 | /* |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 250 | * Force probe of clk_k3 driver here to ensure basic default clock |
| 251 | * configuration is always done. |
| 252 | */ |
| 253 | if (IS_ENABLED(CONFIG_SPL_CLK_K3)) { |
| 254 | ret = uclass_get_device_by_driver(UCLASS_CLK, |
| 255 | DM_DRIVER_GET(ti_clk), |
| 256 | &dev); |
| 257 | if (ret) |
| 258 | printf("Failed to initialize clk-k3!\n"); |
| 259 | } |
| 260 | |
| 261 | /* Output System Firmware version info */ |
| 262 | k3_sysfw_print_ver(); |
| 263 | |
Julien Panis | 01b00d4 | 2022-07-01 14:30:11 +0200 | [diff] [blame] | 264 | if (IS_ENABLED(CONFIG_ESM_K3)) { |
| 265 | /* Probe/configure ESM0 */ |
| 266 | ret = uclass_get_device_by_name(UCLASS_MISC, "esm@420000", &dev); |
| 267 | if (ret) |
| 268 | printf("esm main init failed: %d\n", ret); |
| 269 | |
| 270 | /* Probe/configure MCUESM */ |
| 271 | ret = uclass_get_device_by_name(UCLASS_MISC, "esm@4100000", &dev); |
| 272 | if (ret) |
| 273 | printf("esm mcu init failed: %d\n", ret); |
| 274 | |
| 275 | enable_mcu_esm_reset(); |
| 276 | } |
| 277 | |
Nishanth Menon | 3bb13ba | 2023-08-25 13:02:57 -0500 | [diff] [blame] | 278 | if (IS_ENABLED(CONFIG_K3_AM64_DDRSS)) { |
| 279 | ret = uclass_get_device(UCLASS_RAM, 0, &dev); |
| 280 | if (ret) |
| 281 | panic("DRAM init failed: %d\n", ret); |
| 282 | } |
Joao Paulo Goncalves | fc3557f | 2023-11-13 16:07:21 -0300 | [diff] [blame] | 283 | spl_enable_cache(); |
Joao Paulo Goncalves | 908ee7b | 2024-03-20 09:16:32 -0300 | [diff] [blame] | 284 | |
| 285 | fixup_a53_cpu_freq_by_speed_grade(); |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device) |
| 289 | { |
| 290 | u32 devstat = readl(CTRLMMR_MAIN_DEVSTAT); |
Martyn Welch | 5713c62 | 2022-12-20 18:38:18 +0000 | [diff] [blame] | 291 | u32 bootmode = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_MASK) >> |
| 292 | MAIN_DEVSTAT_PRIMARY_BOOTMODE_SHIFT; |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 293 | u32 bootmode_cfg = (devstat & MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_MASK) >> |
| 294 | MAIN_DEVSTAT_PRIMARY_BOOTMODE_CFG_SHIFT; |
| 295 | |
Martyn Welch | 5713c62 | 2022-12-20 18:38:18 +0000 | [diff] [blame] | 296 | switch (bootmode) { |
| 297 | case BOOT_DEVICE_EMMC: |
Michael Trimarchi | b96f93b | 2023-12-08 08:53:05 +0100 | [diff] [blame] | 298 | if (IS_ENABLED(CONFIG_SUPPORT_EMMC_BOOT)) |
| 299 | return MMCSD_MODE_EMMCBOOT; |
Nishanth Menon | dd34386 | 2023-08-25 13:03:00 -0500 | [diff] [blame] | 300 | if (IS_ENABLED(CONFIG_SPL_FS_FAT) || IS_ENABLED(CONFIG_SPL_FS_EXT4)) |
| 301 | return MMCSD_MODE_FS; |
Martyn Welch | 5713c62 | 2022-12-20 18:38:18 +0000 | [diff] [blame] | 302 | return MMCSD_MODE_EMMCBOOT; |
| 303 | case BOOT_DEVICE_MMC: |
| 304 | if (bootmode_cfg & MAIN_DEVSTAT_PRIMARY_MMC_FS_RAW_MASK) |
| 305 | return MMCSD_MODE_RAW; |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 306 | default: |
Martyn Welch | 5713c62 | 2022-12-20 18:38:18 +0000 | [diff] [blame] | 307 | return MMCSD_MODE_FS; |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 311 | u32 spl_boot_device(void) |
| 312 | { |
Wadim Egorov | 3eab206 | 2024-04-03 15:59:10 +0200 | [diff] [blame] | 313 | return get_boot_device(); |
Suman Anna | 27fa412 | 2022-05-25 13:38:42 +0530 | [diff] [blame] | 314 | } |