1 /* 2 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 #include <cortex_a57.h> 9 #include <arch_helpers.h> 10 #include <common/debug.h> 11 #include <drivers/delay_timer.h> 12 #include <lib/mmio.h> 13 #include <lib/psci/psci.h> 14 #include <plat/common/platform.h> 15 16 #include <bpmp.h> 17 #include <flowctrl.h> 18 #include <memctrl.h> 19 #include <pmc.h> 20 #include <platform_def.h> 21 #include <security_engine.h> 22 #include <tegra_def.h> 23 #include <tegra_private.h> 24 #include <tegra_platform.h> 25 #include <utils.h> 26 27 /* 28 * Register used to clear CPU reset signals. Each CPU has two reset 29 * signals: CPU reset (3:0) and Core reset (19:16). 30 */ 31 #define CPU_CMPLX_RESET_CLR 0x454 32 #define CPU_CORE_RESET_MASK 0x10001 33 34 /* Clock and Reset controller registers for system clock's settings */ 35 #define SCLK_RATE 0x30 36 #define SCLK_BURST_POLICY 0x28 37 #define SCLK_BURST_POLICY_DEFAULT 0x10000000 38 39 static int cpu_powergate_mask[PLATFORM_MAX_CPUS_PER_CLUSTER]; 40 static bool tegra_bpmp_available = true; 41 42 int32_t tegra_soc_validate_power_state(unsigned int power_state, 43 psci_power_state_t *req_state) 44 { 45 int state_id = psci_get_pstate_id(power_state); 46 const plat_params_from_bl2_t *plat_params = bl31_get_plat_params(); 47 48 /* Sanity check the requested state id */ 49 switch (state_id) { 50 case PSTATE_ID_CORE_POWERDN: 51 /* 52 * Core powerdown request only for afflvl 0 53 */ 54 req_state->pwr_domain_state[MPIDR_AFFLVL0] = state_id & 0xff; 55 56 break; 57 58 case PSTATE_ID_CLUSTER_IDLE: 59 60 /* 61 * Cluster idle request for afflvl 0 62 */ 63 req_state->pwr_domain_state[MPIDR_AFFLVL0] = PSTATE_ID_CORE_POWERDN; 64 req_state->pwr_domain_state[MPIDR_AFFLVL1] = state_id; 65 break; 66 67 case PSTATE_ID_SOC_POWERDN: 68 69 /* 70 * sc7entry-fw must be present in the system when the bpmp 71 * firmware is not present, for a successful System Suspend 72 * entry. 73 */ 74 if (!tegra_bpmp_init() && !plat_params->sc7entry_fw_base) 75 return PSCI_E_NOT_SUPPORTED; 76 77 /* 78 * System powerdown request only for afflvl 2 79 */ 80 for (uint32_t i = MPIDR_AFFLVL0; i < PLAT_MAX_PWR_LVL; i++) 81 req_state->pwr_domain_state[i] = PLAT_MAX_OFF_STATE; 82 83 req_state->pwr_domain_state[PLAT_MAX_PWR_LVL] = 84 PLAT_SYS_SUSPEND_STATE_ID; 85 86 break; 87 88 default: 89 ERROR("%s: unsupported state id (%d)\n", __func__, state_id); 90 return PSCI_E_INVALID_PARAMS; 91 } 92 93 return PSCI_E_SUCCESS; 94 } 95 96 /******************************************************************************* 97 * Platform handler to calculate the proper target power level at the 98 * specified affinity level. 99 ******************************************************************************/ 100 plat_local_state_t tegra_soc_get_target_pwr_state(unsigned int lvl, 101 const plat_local_state_t *states, 102 unsigned int ncpu) 103 { 104 plat_local_state_t target = PSCI_LOCAL_STATE_RUN; 105 int cpu = plat_my_core_pos(); 106 int core_pos = read_mpidr() & MPIDR_CPU_MASK; 107 uint32_t bpmp_reply, data[3], val; 108 int ret; 109 110 /* get the power state at this level */ 111 if (lvl == MPIDR_AFFLVL1) 112 target = *(states + core_pos); 113 if (lvl == MPIDR_AFFLVL2) 114 target = *(states + cpu); 115 116 if ((lvl == MPIDR_AFFLVL1) && (target == PSTATE_ID_CLUSTER_IDLE)) { 117 118 /* initialize the bpmp interface */ 119 ret = tegra_bpmp_init(); 120 if (ret != 0U) { 121 122 /* Cluster idle not allowed */ 123 target = PSCI_LOCAL_STATE_RUN; 124 125 /******************************************* 126 * BPMP is not present, so handle CC6 entry 127 * from the CPU 128 ******************************************/ 129 130 /* check if cluster idle state has been enabled */ 131 val = mmio_read_32(TEGRA_CL_DVFS_BASE + DVFS_DFLL_CTRL); 132 if (val == ENABLE_CLOSED_LOOP) { 133 /* 134 * flag to indicate that BPMP firmware is not 135 * available and the CPU has to handle entry/exit 136 * for all power states 137 */ 138 tegra_bpmp_available = false; 139 140 /* 141 * Acquire the cluster idle lock to stop 142 * other CPUs from powering up. 143 */ 144 tegra_fc_ccplex_pgexit_lock(); 145 146 /* Cluster idle only from the last standing CPU */ 147 if (tegra_pmc_is_last_on_cpu() && tegra_fc_is_ccx_allowed()) { 148 /* Cluster idle allowed */ 149 target = PSTATE_ID_CLUSTER_IDLE; 150 } else { 151 /* release cluster idle lock */ 152 tegra_fc_ccplex_pgexit_unlock(); 153 } 154 } 155 } else { 156 157 /* Cluster power-down */ 158 data[0] = (uint32_t)cpu; 159 data[1] = TEGRA_PM_CC6; 160 data[2] = TEGRA_PM_SC1; 161 ret = tegra_bpmp_send_receive_atomic(MRQ_DO_IDLE, 162 (void *)&data, (int)sizeof(data), 163 (void *)&bpmp_reply, 164 (int)sizeof(bpmp_reply)); 165 166 /* check if cluster power down is allowed */ 167 if ((ret != 0L) || (bpmp_reply != BPMP_CCx_ALLOWED)) { 168 169 /* Cluster power down not allowed */ 170 target = PSCI_LOCAL_STATE_RUN; 171 } 172 } 173 174 } else if (((lvl == MPIDR_AFFLVL2) || (lvl == MPIDR_AFFLVL1)) && 175 (target == PSTATE_ID_SOC_POWERDN)) { 176 177 /* System Suspend */ 178 target = PSTATE_ID_SOC_POWERDN; 179 180 } else { 181 ; /* do nothing */ 182 } 183 184 return target; 185 } 186 187 int tegra_soc_pwr_domain_suspend(const psci_power_state_t *target_state) 188 { 189 u_register_t mpidr = read_mpidr(); 190 const plat_local_state_t *pwr_domain_state = 191 target_state->pwr_domain_state; 192 unsigned int stateid_afflvl2 = pwr_domain_state[MPIDR_AFFLVL2]; 193 unsigned int stateid_afflvl1 = pwr_domain_state[MPIDR_AFFLVL1]; 194 unsigned int stateid_afflvl0 = pwr_domain_state[MPIDR_AFFLVL0]; 195 uint32_t cfg; 196 int ret = PSCI_E_SUCCESS; 197 uint32_t val; 198 199 if (stateid_afflvl2 == PSTATE_ID_SOC_POWERDN) { 200 201 assert((stateid_afflvl0 == PLAT_MAX_OFF_STATE) || 202 (stateid_afflvl0 == PSTATE_ID_SOC_POWERDN)); 203 assert((stateid_afflvl1 == PLAT_MAX_OFF_STATE) || 204 (stateid_afflvl1 == PSTATE_ID_SOC_POWERDN)); 205 206 if (tegra_chipid_is_t210_b01()) { 207 208 /* Suspend se/se2 and pka1 */ 209 if (tegra_se_suspend() != 0) { 210 ret = PSCI_E_INTERN_FAIL; 211 } 212 } 213 214 } else if (stateid_afflvl1 == PSTATE_ID_CLUSTER_IDLE) { 215 216 assert(stateid_afflvl0 == PSTATE_ID_CORE_POWERDN); 217 218 if (!tegra_bpmp_available) { 219 220 /* Find if the platform uses OVR2/MAX77621 PMIC */ 221 cfg = mmio_read_32(TEGRA_CL_DVFS_BASE + DVFS_DFLL_OUTPUT_CFG); 222 if (cfg & DFLL_OUTPUT_CFG_CLK_EN_BIT) { 223 /* OVR2 */ 224 225 /* PWM tristate */ 226 val = mmio_read_32(TEGRA_MISC_BASE + PINMUX_AUX_DVFS_PWM); 227 val |= PINMUX_PWM_TRISTATE; 228 mmio_write_32(TEGRA_MISC_BASE + PINMUX_AUX_DVFS_PWM, val); 229 230 /* 231 * SCRATCH201[1] is being used to identify CPU 232 * PMIC in warmboot code. 233 * 0 : OVR2 234 * 1 : MAX77621 235 */ 236 tegra_pmc_write_32(PMC_SCRATCH201, 0x0); 237 } else { 238 /* MAX77621 */ 239 tegra_pmc_write_32(PMC_SCRATCH201, 0x2); 240 } 241 } 242 243 /* Prepare for cluster idle */ 244 tegra_fc_cluster_idle(mpidr); 245 246 } else if (stateid_afflvl0 == PSTATE_ID_CORE_POWERDN) { 247 248 /* Prepare for cpu powerdn */ 249 tegra_fc_cpu_powerdn(mpidr); 250 251 } else { 252 ERROR("%s: Unknown state id (%d, %d, %d)\n", __func__, 253 stateid_afflvl2, stateid_afflvl1, stateid_afflvl0); 254 ret = PSCI_E_NOT_SUPPORTED; 255 } 256 257 return ret; 258 } 259 260 static void tegra_reset_all_dma_masters(void) 261 { 262 uint32_t val, mask; 263 264 /* 265 * Reset all possible DMA masters in the system. 266 */ 267 val = GPU_RESET_BIT; 268 mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_GPU_RESET_REG_OFFSET, val); 269 270 val = NVENC_RESET_BIT | TSECB_RESET_BIT | APE_RESET_BIT | 271 NVJPG_RESET_BIT | NVDEC_RESET_BIT; 272 mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_Y, val); 273 274 val = HOST1X_RESET_BIT | ISP_RESET_BIT | USBD_RESET_BIT | 275 VI_RESET_BIT | SDMMC4_RESET_BIT | SDMMC1_RESET_BIT | 276 SDMMC2_RESET_BIT; 277 mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_L, val); 278 279 val = USB2_RESET_BIT | APBDMA_RESET_BIT | AHBDMA_RESET_BIT; 280 mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_H, val); 281 282 val = XUSB_DEV_RESET_BIT | XUSB_HOST_RESET_BIT | TSEC_RESET_BIT | 283 PCIE_RESET_BIT | SDMMC3_RESET_BIT; 284 mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_U, val); 285 286 val = SE_RESET_BIT | HDA_RESET_BIT | SATA_RESET_BIT; 287 mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_V, val); 288 289 /* 290 * If any of the DMA masters are still alive, assume 291 * that the system has been compromised and reboot. 292 */ 293 val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_GPU_RESET_REG_OFFSET); 294 mask = GPU_RESET_BIT; 295 if ((val & mask) != mask) 296 tegra_pmc_system_reset(); 297 298 mask = NVENC_RESET_BIT | TSECB_RESET_BIT | APE_RESET_BIT | 299 NVJPG_RESET_BIT | NVDEC_RESET_BIT; 300 val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_Y); 301 if ((val & mask) != mask) 302 tegra_pmc_system_reset(); 303 304 mask = HOST1X_RESET_BIT | ISP_RESET_BIT | USBD_RESET_BIT | 305 VI_RESET_BIT | SDMMC4_RESET_BIT | SDMMC1_RESET_BIT | 306 SDMMC2_RESET_BIT; 307 val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_L); 308 if ((val & mask) != mask) 309 tegra_pmc_system_reset(); 310 311 mask = USB2_RESET_BIT | APBDMA_RESET_BIT | AHBDMA_RESET_BIT; 312 val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_H); 313 if ((val & mask) != mask) 314 tegra_pmc_system_reset(); 315 316 mask = XUSB_DEV_RESET_BIT | XUSB_HOST_RESET_BIT | TSEC_RESET_BIT | 317 PCIE_RESET_BIT | SDMMC3_RESET_BIT; 318 val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_U); 319 if ((val & mask) != mask) 320 tegra_pmc_system_reset(); 321 322 val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_SET_V); 323 mask = SE_RESET_BIT | HDA_RESET_BIT | SATA_RESET_BIT; 324 if ((val & mask) != mask) 325 tegra_pmc_system_reset(); 326 } 327 328 int tegra_soc_pwr_domain_power_down_wfi(const psci_power_state_t *target_state) 329 { 330 u_register_t mpidr = read_mpidr(); 331 const plat_local_state_t *pwr_domain_state = 332 target_state->pwr_domain_state; 333 unsigned int stateid_afflvl2 = pwr_domain_state[PLAT_MAX_PWR_LVL]; 334 const plat_params_from_bl2_t *plat_params = bl31_get_plat_params(); 335 uint32_t val; 336 337 if (stateid_afflvl2 == PSTATE_ID_SOC_POWERDN) { 338 339 if (tegra_chipid_is_t210_b01()) { 340 /* Save tzram contents */ 341 tegra_se_save_tzram(); 342 } 343 344 /* 345 * The CPU needs to load the System suspend entry firmware 346 * if nothing is running on the BPMP. 347 */ 348 if (!tegra_bpmp_available) { 349 350 /* 351 * BPMP firmware is not running on the co-processor, so 352 * we need to explicitly load the firmware to enable 353 * entry/exit to/from System Suspend and set the BPMP 354 * on its way. 355 */ 356 357 /* Power off BPMP before we proceed */ 358 tegra_fc_bpmp_off(); 359 360 /* bond out IRAM banks B, C and D */ 361 mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_BOND_OUT_U, 362 IRAM_B_LOCK_BIT | IRAM_C_LOCK_BIT | 363 IRAM_D_LOCK_BIT); 364 365 /* bond out APB/AHB DMAs */ 366 mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_BOND_OUT_H, 367 APB_DMA_LOCK_BIT | AHB_DMA_LOCK_BIT); 368 369 /* Power off BPMP before we proceed */ 370 tegra_fc_bpmp_off(); 371 372 /* 373 * Reset all the hardware blocks that can act as DMA 374 * masters on the bus. 375 */ 376 tegra_reset_all_dma_masters(); 377 378 /* clean up IRAM of any cruft */ 379 zeromem((void *)(uintptr_t)TEGRA_IRAM_BASE, 380 TEGRA_IRAM_A_SIZE); 381 382 /* Copy the firmware to BPMP's internal RAM */ 383 (void)memcpy((void *)(uintptr_t)TEGRA_IRAM_BASE, 384 (const void *)(plat_params->sc7entry_fw_base + SC7ENTRY_FW_HEADER_SIZE_BYTES), 385 plat_params->sc7entry_fw_size - SC7ENTRY_FW_HEADER_SIZE_BYTES); 386 387 /* Power on the BPMP and execute from IRAM base */ 388 tegra_fc_bpmp_on(TEGRA_IRAM_BASE); 389 390 /* Wait until BPMP powers up */ 391 do { 392 val = mmio_read_32(TEGRA_RES_SEMA_BASE + STA_OFFSET); 393 } while (val != SIGN_OF_LIFE); 394 } 395 396 /* enter system suspend */ 397 tegra_fc_soc_powerdn(mpidr); 398 } 399 400 return PSCI_E_SUCCESS; 401 } 402 403 int tegra_soc_pwr_domain_on_finish(const psci_power_state_t *target_state) 404 { 405 const plat_params_from_bl2_t *plat_params = bl31_get_plat_params(); 406 uint32_t cfg; 407 uint32_t val, entrypoint = 0; 408 uint64_t offset; 409 410 /* platform parameter passed by the previous bootloader */ 411 if (plat_params->l2_ecc_parity_prot_dis != 1) { 412 /* Enable ECC Parity Protection for Cortex-A57 CPUs */ 413 val = read_l2ctlr_el1(); 414 val |= (uint64_t)CORTEX_A57_L2_ECC_PARITY_PROTECTION_BIT; 415 write_l2ctlr_el1(val); 416 } 417 418 /* 419 * Check if we are exiting from SOC_POWERDN. 420 */ 421 if (target_state->pwr_domain_state[PLAT_MAX_PWR_LVL] == 422 PLAT_SYS_SUSPEND_STATE_ID) { 423 424 /* 425 * Security engine resume 426 */ 427 if (tegra_chipid_is_t210_b01()) { 428 tegra_se_resume(); 429 } 430 431 /* 432 * Lock scratch registers which hold the CPU vectors 433 */ 434 tegra_pmc_lock_cpu_vectors(); 435 436 /* 437 * Enable WRAP to INCR burst type conversions for 438 * incoming requests on the AXI slave ports. 439 */ 440 val = mmio_read_32(TEGRA_MSELECT_BASE + MSELECT_CONFIG); 441 val &= ~ENABLE_UNSUP_TX_ERRORS; 442 val |= ENABLE_WRAP_TO_INCR_BURSTS; 443 mmio_write_32(TEGRA_MSELECT_BASE + MSELECT_CONFIG, val); 444 445 /* 446 * Restore Boot and Power Management Processor (BPMP) reset 447 * address and reset it, if it is supported by the platform. 448 */ 449 if (!tegra_bpmp_available) { 450 tegra_fc_bpmp_off(); 451 } else { 452 entrypoint = tegra_pmc_read_32(PMC_SCRATCH39); 453 tegra_fc_bpmp_on(entrypoint); 454 } 455 456 /* sc7entry-fw is part of TZDRAM area */ 457 if (plat_params->sc7entry_fw_base != 0U) { 458 offset = plat_params->tzdram_base - plat_params->sc7entry_fw_base; 459 tegra_memctrl_tzdram_setup(plat_params->sc7entry_fw_base, 460 plat_params->tzdram_size + offset); 461 } 462 } 463 464 /* 465 * Check if we are exiting cluster idle state 466 */ 467 if (target_state->pwr_domain_state[MPIDR_AFFLVL1] == 468 PSTATE_ID_CLUSTER_IDLE) { 469 470 if (!tegra_bpmp_available) { 471 472 /* PWM un-tristate */ 473 cfg = mmio_read_32(TEGRA_CL_DVFS_BASE + DVFS_DFLL_OUTPUT_CFG); 474 if (cfg & DFLL_OUTPUT_CFG_CLK_EN_BIT) { 475 val = mmio_read_32(TEGRA_MISC_BASE + PINMUX_AUX_DVFS_PWM); 476 val &= ~PINMUX_PWM_TRISTATE; 477 mmio_write_32(TEGRA_MISC_BASE + PINMUX_AUX_DVFS_PWM, val); 478 } 479 480 /* release cluster idle lock */ 481 tegra_fc_ccplex_pgexit_unlock(); 482 } 483 } 484 485 /* 486 * T210 has a dedicated ARMv7 boot and power mgmt processor, BPMP. It's 487 * used for power management and boot purposes. Inform the BPMP that 488 * we have completed the cluster power up. 489 */ 490 tegra_fc_lock_active_cluster(); 491 492 return PSCI_E_SUCCESS; 493 } 494 495 int tegra_soc_pwr_domain_on(u_register_t mpidr) 496 { 497 int cpu = mpidr & MPIDR_CPU_MASK; 498 uint32_t mask = CPU_CORE_RESET_MASK << cpu; 499 500 /* Deassert CPU reset signals */ 501 mmio_write_32(TEGRA_CAR_RESET_BASE + CPU_CMPLX_RESET_CLR, mask); 502 503 /* Turn on CPU using flow controller or PMC */ 504 if (cpu_powergate_mask[cpu] == 0) { 505 tegra_pmc_cpu_on(cpu); 506 cpu_powergate_mask[cpu] = 1; 507 } else { 508 tegra_fc_cpu_on(cpu); 509 } 510 511 return PSCI_E_SUCCESS; 512 } 513 514 int tegra_soc_pwr_domain_off(const psci_power_state_t *target_state) 515 { 516 tegra_fc_cpu_off(read_mpidr() & MPIDR_CPU_MASK); 517 return PSCI_E_SUCCESS; 518 } 519 520 int tegra_soc_prepare_system_reset(void) 521 { 522 /* 523 * Set System Clock (SCLK) to POR default so that the clock source 524 * for the PMC APB clock would not be changed due to system reset. 525 */ 526 mmio_write_32((uintptr_t)TEGRA_CAR_RESET_BASE + SCLK_BURST_POLICY, 527 SCLK_BURST_POLICY_DEFAULT); 528 mmio_write_32((uintptr_t)TEGRA_CAR_RESET_BASE + SCLK_RATE, 0); 529 530 /* Wait 1 ms to make sure clock source/device logic is stabilized. */ 531 mdelay(1); 532 533 return PSCI_E_SUCCESS; 534 } 535