181136819SBai Ping /* 281136819SBai Ping * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved. 381136819SBai Ping * 481136819SBai Ping * SPDX-License-Identifier: BSD-3-Clause 581136819SBai Ping */ 681136819SBai Ping 781136819SBai Ping #include <stdlib.h> 881136819SBai Ping #include <stdint.h> 981136819SBai Ping #include <stdbool.h> 1009d40e0eSAntonio Nino Diaz 1109d40e0eSAntonio Nino Diaz #include <common/debug.h> 1209d40e0eSAntonio Nino Diaz #include <lib/mmio.h> 1309d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h> 1409d40e0eSAntonio Nino Diaz #include <platform_def.h> 1509d40e0eSAntonio Nino Diaz #include <services/std_svc.h> 1609d40e0eSAntonio Nino Diaz 1709d40e0eSAntonio Nino Diaz #include <gpc.h> 1881136819SBai Ping 1981136819SBai Ping /* use wfi power down the core */ 2081136819SBai Ping void imx_set_cpu_pwr_off(unsigned int core_id) 2181136819SBai Ping { 22*fe5e1c14SJacky Bai bakery_lock_get(&gpc_lock); 23*fe5e1c14SJacky Bai 2481136819SBai Ping /* enable the wfi power down of the core */ 2581136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + LPCR_A53_AD, COREx_WFI_PDN(core_id) | 2681136819SBai Ping (1 << (core_id + 20))); 27*fe5e1c14SJacky Bai 28*fe5e1c14SJacky Bai bakery_lock_release(&gpc_lock); 29*fe5e1c14SJacky Bai 3081136819SBai Ping /* assert the pcg pcr bit of the core */ 3181136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + COREx_PGC_PCR(core_id), 0x1); 3281136819SBai Ping }; 3381136819SBai Ping 3481136819SBai Ping /* if out of lpm, we need to do reverse steps */ 3581136819SBai Ping void imx_set_cpu_lpm(unsigned int core_id, bool pdn) 3681136819SBai Ping { 37*fe5e1c14SJacky Bai bakery_lock_get(&gpc_lock); 38*fe5e1c14SJacky Bai 3981136819SBai Ping if (pdn) { 4081136819SBai Ping /* enable the core WFI PDN & IRQ PUP */ 4181136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + LPCR_A53_AD, COREx_WFI_PDN(core_id) | 4281136819SBai Ping (1 << (core_id + 20)) | COREx_IRQ_WUP(core_id)); 4381136819SBai Ping /* assert the pcg pcr bit of the core */ 4481136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + COREx_PGC_PCR(core_id), 0x1); 4581136819SBai Ping } else { 4681136819SBai Ping /* disable CORE WFI PDN & IRQ PUP */ 4781136819SBai Ping mmio_clrbits_32(IMX_GPC_BASE + LPCR_A53_AD, COREx_WFI_PDN(core_id) | 4881136819SBai Ping COREx_IRQ_WUP(core_id)); 4981136819SBai Ping /* deassert the pcg pcr bit of the core */ 5081136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + COREx_PGC_PCR(core_id), 0x1); 5181136819SBai Ping } 52*fe5e1c14SJacky Bai 53*fe5e1c14SJacky Bai bakery_lock_release(&gpc_lock); 5481136819SBai Ping } 5581136819SBai Ping 5681136819SBai Ping void imx_pup_pdn_slot_config(int last_core, bool pdn) 5781136819SBai Ping { 5881136819SBai Ping if (pdn) { 5981136819SBai Ping /* SLOT0 for A53 PLAT power down */ 6081136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + SLTx_CFG(0), SLT_PLAT_PDN); 6181136819SBai Ping /* SLOT1 for A53 PLAT power up */ 6281136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + SLTx_CFG(1), SLT_PLAT_PUP); 6381136819SBai Ping /* SLOT2 for A53 primary core power up */ 6481136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + SLTx_CFG(2), SLT_COREx_PUP(last_core)); 6581136819SBai Ping /* ACK setting: PLAT ACK for PDN, CORE ACK for PUP */ 6681136819SBai Ping mmio_clrsetbits_32(IMX_GPC_BASE + PGC_ACK_SEL_A53, 0xFFFFFFFF, 6781136819SBai Ping A53_PLAT_PDN_ACK | A53_PLAT_PUP_ACK); 6881136819SBai Ping } else { 6981136819SBai Ping mmio_clrbits_32(IMX_GPC_BASE + SLTx_CFG(0), 0xFFFFFFFF); 7081136819SBai Ping mmio_clrbits_32(IMX_GPC_BASE + SLTx_CFG(1), 0xFFFFFFFF); 7181136819SBai Ping mmio_clrbits_32(IMX_GPC_BASE + SLTx_CFG(2), 0xFFFFFFFF); 7281136819SBai Ping mmio_clrsetbits_32(IMX_GPC_BASE + PGC_ACK_SEL_A53, 0xFFFFFFFF, 7381136819SBai Ping A53_DUMMY_PDN_ACK | A53_DUMMY_PUP_ACK); 7481136819SBai Ping } 7581136819SBai Ping } 7681136819SBai Ping 7781136819SBai Ping void imx_set_cluster_powerdown(unsigned int last_core, uint8_t power_state) 7881136819SBai Ping { 7981136819SBai Ping uint32_t val; 8081136819SBai Ping 8181136819SBai Ping if (is_local_state_off(power_state)) { 8281136819SBai Ping val = mmio_read_32(IMX_GPC_BASE + LPCR_A53_BSC); 8381136819SBai Ping val |= A53_LPM_STOP; /* enable C0-C1's STOP mode */ 8481136819SBai Ping val &= ~CPU_CLOCK_ON_LPM; /* disable CPU clock in LPM mode */ 8581136819SBai Ping mmio_write_32(IMX_GPC_BASE + LPCR_A53_BSC, val); 8681136819SBai Ping 8781136819SBai Ping /* enable C2-3's STOP mode */ 8881136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + LPCR_A53_BSC2, A53_LPM_STOP); 8981136819SBai Ping 9081136819SBai Ping /* enable PLAT/SCU power down */ 9181136819SBai Ping val = mmio_read_32(IMX_GPC_BASE + LPCR_A53_AD); 9281136819SBai Ping val &= ~EN_L2_WFI_PDN; 9381136819SBai Ping val |= L2PGE | EN_PLAT_PDN; 9481136819SBai Ping val &= ~COREx_IRQ_WUP(last_core); /* disable IRQ PUP for last core */ 9581136819SBai Ping val |= COREx_LPM_PUP(last_core); /* enable LPM PUP for last core */ 9681136819SBai Ping mmio_write_32(IMX_GPC_BASE + LPCR_A53_AD, val); 9781136819SBai Ping 9881136819SBai Ping imx_pup_pdn_slot_config(last_core, true); 9981136819SBai Ping 10081136819SBai Ping /* enable PLAT PGC */ 10181136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + A53_PLAT_PGC, 0x1); 10281136819SBai Ping } else { 10381136819SBai Ping /* clear PLAT PGC */ 10481136819SBai Ping mmio_clrbits_32(IMX_GPC_BASE + A53_PLAT_PGC, 0x1); 10581136819SBai Ping 10681136819SBai Ping /* clear the slot and ack for cluster power down */ 10781136819SBai Ping imx_pup_pdn_slot_config(last_core, false); 10881136819SBai Ping 10981136819SBai Ping val = mmio_read_32(IMX_GPC_BASE + LPCR_A53_BSC); 11081136819SBai Ping val &= ~A53_LPM_MASK; /* clear the C0~1 LPM */ 11181136819SBai Ping val |= CPU_CLOCK_ON_LPM; /* disable cpu clock in LPM */ 11281136819SBai Ping mmio_write_32(IMX_GPC_BASE + LPCR_A53_BSC, val); 11381136819SBai Ping 11481136819SBai Ping /* set A53 LPM to RUN mode */ 11581136819SBai Ping mmio_clrbits_32(IMX_GPC_BASE + LPCR_A53_BSC2, A53_LPM_MASK); 11681136819SBai Ping 11781136819SBai Ping /* clear PLAT/SCU power down */ 11881136819SBai Ping val = mmio_read_32(IMX_GPC_BASE + LPCR_A53_AD); 11981136819SBai Ping val |= EN_L2_WFI_PDN; 12081136819SBai Ping val &= ~(L2PGE | EN_PLAT_PDN); 12181136819SBai Ping val &= ~COREx_LPM_PUP(last_core); /* disable C0's LPM PUP */ 12281136819SBai Ping mmio_write_32(IMX_GPC_BASE + LPCR_A53_AD, val); 12381136819SBai Ping } 12481136819SBai Ping } 12581136819SBai Ping 12681136819SBai Ping void imx_gpc_init(void) 12781136819SBai Ping { 12881136819SBai Ping uint32_t val; 12981136819SBai Ping int i; 13081136819SBai Ping /* mask all the interrupt by default */ 1317696880aSLeonard Crestez for (i = 0; i < 4; i++) { 1327696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE0_A53 + i * 4, ~0x0); 1337696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE1_A53 + i * 4, ~0x0); 1347696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE2_A53 + i * 4, ~0x0); 1357696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE3_A53 + i * 4, ~0x0); 1367696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE0_M4 + i * 4, ~0x0); 1377696880aSLeonard Crestez } 13881136819SBai Ping /* Due to the hardware design requirement, need to make 13981136819SBai Ping * sure GPR interrupt(#32) is unmasked during RUN mode to 14081136819SBai Ping * avoid entering DSM mode by mistake. 14181136819SBai Ping */ 1427696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE0_A53, 0xFFFFFFFE); 1437696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE1_A53, 0xFFFFFFFE); 1447696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE2_A53, 0xFFFFFFFE); 1457696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE3_A53, 0xFFFFFFFE); 14681136819SBai Ping 14781136819SBai Ping /* use external IRQs to wakeup C0~C3 from LPM */ 14881136819SBai Ping val = mmio_read_32(IMX_GPC_BASE + LPCR_A53_BSC); 14981136819SBai Ping val |= IRQ_SRC_A53_WUP; 15081136819SBai Ping /* clear the MASTER0 LPM handshake */ 15181136819SBai Ping val &= ~MASTER0_LPM_HSK; 15281136819SBai Ping mmio_write_32(IMX_GPC_BASE + LPCR_A53_BSC, val); 15381136819SBai Ping 15481136819SBai Ping /* mask M4 DSM trigger if M4 is NOT enabled */ 15581136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + LPCR_M4, DSM_MODE_MASK); 15681136819SBai Ping 15781136819SBai Ping /* set all mix/PU in A53 domain */ 15881136819SBai Ping mmio_write_32(IMX_GPC_BASE + PGC_CPU_0_1_MAPPING, 0xfffd); 15981136819SBai Ping 16081136819SBai Ping /* set SCU timming */ 16181136819SBai Ping mmio_write_32(IMX_GPC_BASE + PGC_SCU_TIMING, 16281136819SBai Ping (0x59 << 10) | 0x5B | (0x2 << 20)); 16381136819SBai Ping 16481136819SBai Ping /* set DUMMY PDN/PUP ACK by default for A53 domain */ 16581136819SBai Ping mmio_write_32(IMX_GPC_BASE + PGC_ACK_SEL_A53, A53_DUMMY_PUP_ACK | 16681136819SBai Ping A53_DUMMY_PDN_ACK); 16781136819SBai Ping 16881136819SBai Ping /* disable DSM mode by default */ 16981136819SBai Ping mmio_clrbits_32(IMX_GPC_BASE + SLPCR, DSM_MODE_MASK); 17081136819SBai Ping 17181136819SBai Ping /* 17281136819SBai Ping * USB PHY power up needs to make sure RESET bit in SRC is clear, 17381136819SBai Ping * otherwise, the PU power up bit in GPC will NOT self-cleared. 17481136819SBai Ping * only need to do it once. 17581136819SBai Ping */ 17681136819SBai Ping mmio_clrbits_32(IMX_SRC_BASE + SRC_OTG1PHY_SCR, 0x1); 17781136819SBai Ping mmio_clrbits_32(IMX_SRC_BASE + SRC_OTG2PHY_SCR, 0x1); 178e1958506SLeonard Crestez 179e1958506SLeonard Crestez /* enable all the power domain by default */ 180e1958506SLeonard Crestez mmio_write_32(IMX_GPC_BASE + PU_PGC_UP_TRG, 0x3fcf); 18181136819SBai Ping } 182