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> 12*66345b8bSJacky Bai #include <drivers/delay_timer.h> 1309d40e0eSAntonio Nino Diaz #include <lib/mmio.h> 1409d40e0eSAntonio Nino Diaz #include <lib/psci/psci.h> 1509d40e0eSAntonio Nino Diaz #include <platform_def.h> 1609d40e0eSAntonio Nino Diaz #include <services/std_svc.h> 1709d40e0eSAntonio Nino Diaz 1809d40e0eSAntonio Nino Diaz #include <gpc.h> 1981136819SBai Ping 2081136819SBai Ping /* use wfi power down the core */ 2181136819SBai Ping void imx_set_cpu_pwr_off(unsigned int core_id) 2281136819SBai Ping { 23fe5e1c14SJacky Bai bakery_lock_get(&gpc_lock); 24fe5e1c14SJacky Bai 2581136819SBai Ping /* enable the wfi power down of the core */ 2681136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + LPCR_A53_AD, COREx_WFI_PDN(core_id) | 2781136819SBai Ping (1 << (core_id + 20))); 28fe5e1c14SJacky Bai 29fe5e1c14SJacky Bai bakery_lock_release(&gpc_lock); 30fe5e1c14SJacky Bai 3181136819SBai Ping /* assert the pcg pcr bit of the core */ 3281136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + COREx_PGC_PCR(core_id), 0x1); 3381136819SBai Ping }; 3481136819SBai Ping 3581136819SBai Ping /* if out of lpm, we need to do reverse steps */ 3681136819SBai Ping void imx_set_cpu_lpm(unsigned int core_id, bool pdn) 3781136819SBai Ping { 38fe5e1c14SJacky Bai bakery_lock_get(&gpc_lock); 39fe5e1c14SJacky Bai 4081136819SBai Ping if (pdn) { 4181136819SBai Ping /* enable the core WFI PDN & IRQ PUP */ 4281136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + LPCR_A53_AD, COREx_WFI_PDN(core_id) | 4381136819SBai Ping (1 << (core_id + 20)) | COREx_IRQ_WUP(core_id)); 4481136819SBai Ping /* assert the pcg pcr bit of the core */ 4581136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + COREx_PGC_PCR(core_id), 0x1); 4681136819SBai Ping } else { 4781136819SBai Ping /* disable CORE WFI PDN & IRQ PUP */ 4881136819SBai Ping mmio_clrbits_32(IMX_GPC_BASE + LPCR_A53_AD, COREx_WFI_PDN(core_id) | 4981136819SBai Ping COREx_IRQ_WUP(core_id)); 5081136819SBai Ping /* deassert the pcg pcr bit of the core */ 5181136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + COREx_PGC_PCR(core_id), 0x1); 5281136819SBai Ping } 53fe5e1c14SJacky Bai 54fe5e1c14SJacky Bai bakery_lock_release(&gpc_lock); 5581136819SBai Ping } 5681136819SBai Ping 5781136819SBai Ping void imx_pup_pdn_slot_config(int last_core, bool pdn) 5881136819SBai Ping { 5981136819SBai Ping if (pdn) { 6081136819SBai Ping /* SLOT0 for A53 PLAT power down */ 6181136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + SLTx_CFG(0), SLT_PLAT_PDN); 6281136819SBai Ping /* SLOT1 for A53 PLAT power up */ 6381136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + SLTx_CFG(1), SLT_PLAT_PUP); 6481136819SBai Ping /* SLOT2 for A53 primary core power up */ 6581136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + SLTx_CFG(2), SLT_COREx_PUP(last_core)); 6681136819SBai Ping /* ACK setting: PLAT ACK for PDN, CORE ACK for PUP */ 6781136819SBai Ping mmio_clrsetbits_32(IMX_GPC_BASE + PGC_ACK_SEL_A53, 0xFFFFFFFF, 6881136819SBai Ping A53_PLAT_PDN_ACK | A53_PLAT_PUP_ACK); 6981136819SBai Ping } else { 7081136819SBai Ping mmio_clrbits_32(IMX_GPC_BASE + SLTx_CFG(0), 0xFFFFFFFF); 7181136819SBai Ping mmio_clrbits_32(IMX_GPC_BASE + SLTx_CFG(1), 0xFFFFFFFF); 7281136819SBai Ping mmio_clrbits_32(IMX_GPC_BASE + SLTx_CFG(2), 0xFFFFFFFF); 7381136819SBai Ping mmio_clrsetbits_32(IMX_GPC_BASE + PGC_ACK_SEL_A53, 0xFFFFFFFF, 7481136819SBai Ping A53_DUMMY_PDN_ACK | A53_DUMMY_PUP_ACK); 7581136819SBai Ping } 7681136819SBai Ping } 7781136819SBai Ping 7881136819SBai Ping void imx_set_cluster_powerdown(unsigned int last_core, uint8_t power_state) 7981136819SBai Ping { 8081136819SBai Ping uint32_t val; 8181136819SBai Ping 8281136819SBai Ping if (is_local_state_off(power_state)) { 8381136819SBai Ping val = mmio_read_32(IMX_GPC_BASE + LPCR_A53_BSC); 8481136819SBai Ping val |= A53_LPM_STOP; /* enable C0-C1's STOP mode */ 8581136819SBai Ping val &= ~CPU_CLOCK_ON_LPM; /* disable CPU clock in LPM mode */ 8681136819SBai Ping mmio_write_32(IMX_GPC_BASE + LPCR_A53_BSC, val); 8781136819SBai Ping 8881136819SBai Ping /* enable C2-3's STOP mode */ 8981136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + LPCR_A53_BSC2, A53_LPM_STOP); 9081136819SBai Ping 9181136819SBai Ping /* enable PLAT/SCU power down */ 9281136819SBai Ping val = mmio_read_32(IMX_GPC_BASE + LPCR_A53_AD); 9381136819SBai Ping val &= ~EN_L2_WFI_PDN; 9481136819SBai Ping val |= L2PGE | EN_PLAT_PDN; 9581136819SBai Ping val &= ~COREx_IRQ_WUP(last_core); /* disable IRQ PUP for last core */ 9681136819SBai Ping val |= COREx_LPM_PUP(last_core); /* enable LPM PUP for last core */ 9781136819SBai Ping mmio_write_32(IMX_GPC_BASE + LPCR_A53_AD, val); 9881136819SBai Ping 9981136819SBai Ping imx_pup_pdn_slot_config(last_core, true); 10081136819SBai Ping 10181136819SBai Ping /* enable PLAT PGC */ 10281136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + A53_PLAT_PGC, 0x1); 10381136819SBai Ping } else { 10481136819SBai Ping /* clear PLAT PGC */ 10581136819SBai Ping mmio_clrbits_32(IMX_GPC_BASE + A53_PLAT_PGC, 0x1); 10681136819SBai Ping 10781136819SBai Ping /* clear the slot and ack for cluster power down */ 10881136819SBai Ping imx_pup_pdn_slot_config(last_core, false); 10981136819SBai Ping 11081136819SBai Ping val = mmio_read_32(IMX_GPC_BASE + LPCR_A53_BSC); 11181136819SBai Ping val &= ~A53_LPM_MASK; /* clear the C0~1 LPM */ 11281136819SBai Ping val |= CPU_CLOCK_ON_LPM; /* disable cpu clock in LPM */ 11381136819SBai Ping mmio_write_32(IMX_GPC_BASE + LPCR_A53_BSC, val); 11481136819SBai Ping 11581136819SBai Ping /* set A53 LPM to RUN mode */ 11681136819SBai Ping mmio_clrbits_32(IMX_GPC_BASE + LPCR_A53_BSC2, A53_LPM_MASK); 11781136819SBai Ping 11881136819SBai Ping /* clear PLAT/SCU power down */ 11981136819SBai Ping val = mmio_read_32(IMX_GPC_BASE + LPCR_A53_AD); 12081136819SBai Ping val |= EN_L2_WFI_PDN; 12181136819SBai Ping val &= ~(L2PGE | EN_PLAT_PDN); 12281136819SBai Ping val &= ~COREx_LPM_PUP(last_core); /* disable C0's LPM PUP */ 12381136819SBai Ping mmio_write_32(IMX_GPC_BASE + LPCR_A53_AD, val); 12481136819SBai Ping } 12581136819SBai Ping } 12681136819SBai Ping 12781136819SBai Ping void imx_gpc_init(void) 12881136819SBai Ping { 12981136819SBai Ping uint32_t val; 13081136819SBai Ping int i; 13181136819SBai Ping /* mask all the interrupt by default */ 1327696880aSLeonard Crestez for (i = 0; i < 4; i++) { 1337696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE0_A53 + i * 4, ~0x0); 1347696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE1_A53 + i * 4, ~0x0); 1357696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE2_A53 + i * 4, ~0x0); 1367696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE3_A53 + i * 4, ~0x0); 1377696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE0_M4 + i * 4, ~0x0); 1387696880aSLeonard Crestez } 13981136819SBai Ping /* Due to the hardware design requirement, need to make 14081136819SBai Ping * sure GPR interrupt(#32) is unmasked during RUN mode to 14181136819SBai Ping * avoid entering DSM mode by mistake. 14281136819SBai Ping */ 1437696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE0_A53, 0xFFFFFFFE); 1447696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE1_A53, 0xFFFFFFFE); 1457696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE2_A53, 0xFFFFFFFE); 1467696880aSLeonard Crestez mmio_write_32(IMX_GPC_BASE + IMR1_CORE3_A53, 0xFFFFFFFE); 14781136819SBai Ping 14881136819SBai Ping /* use external IRQs to wakeup C0~C3 from LPM */ 14981136819SBai Ping val = mmio_read_32(IMX_GPC_BASE + LPCR_A53_BSC); 15081136819SBai Ping val |= IRQ_SRC_A53_WUP; 15181136819SBai Ping /* clear the MASTER0 LPM handshake */ 15281136819SBai Ping val &= ~MASTER0_LPM_HSK; 15381136819SBai Ping mmio_write_32(IMX_GPC_BASE + LPCR_A53_BSC, val); 15481136819SBai Ping 15581136819SBai Ping /* mask M4 DSM trigger if M4 is NOT enabled */ 15681136819SBai Ping mmio_setbits_32(IMX_GPC_BASE + LPCR_M4, DSM_MODE_MASK); 15781136819SBai Ping 15881136819SBai Ping /* set all mix/PU in A53 domain */ 15981136819SBai Ping mmio_write_32(IMX_GPC_BASE + PGC_CPU_0_1_MAPPING, 0xfffd); 16081136819SBai Ping 16181136819SBai Ping /* set SCU timming */ 16281136819SBai Ping mmio_write_32(IMX_GPC_BASE + PGC_SCU_TIMING, 16381136819SBai Ping (0x59 << 10) | 0x5B | (0x2 << 20)); 16481136819SBai Ping 16581136819SBai Ping /* set DUMMY PDN/PUP ACK by default for A53 domain */ 16681136819SBai Ping mmio_write_32(IMX_GPC_BASE + PGC_ACK_SEL_A53, A53_DUMMY_PUP_ACK | 16781136819SBai Ping A53_DUMMY_PDN_ACK); 16881136819SBai Ping 16981136819SBai Ping /* disable DSM mode by default */ 17081136819SBai Ping mmio_clrbits_32(IMX_GPC_BASE + SLPCR, DSM_MODE_MASK); 17181136819SBai Ping 17281136819SBai Ping /* 17381136819SBai Ping * USB PHY power up needs to make sure RESET bit in SRC is clear, 17481136819SBai Ping * otherwise, the PU power up bit in GPC will NOT self-cleared. 17581136819SBai Ping * only need to do it once. 17681136819SBai Ping */ 17781136819SBai Ping mmio_clrbits_32(IMX_SRC_BASE + SRC_OTG1PHY_SCR, 0x1); 17881136819SBai Ping mmio_clrbits_32(IMX_SRC_BASE + SRC_OTG2PHY_SCR, 0x1); 179e1958506SLeonard Crestez 180*66345b8bSJacky Bai /* 181*66345b8bSJacky Bai * for USB OTG, the limitation are: 182*66345b8bSJacky Bai * 1. before system clock config, the IPG clock run at 12.5MHz, delay time 183*66345b8bSJacky Bai * should be longer than 82us. 184*66345b8bSJacky Bai * 2. after system clock config, ipg clock run at 66.5MHz, delay time 185*66345b8bSJacky Bai * be longer that 15.3 us. 186*66345b8bSJacky Bai * Add 100us to make sure the USB OTG SRC is clear safely. 187*66345b8bSJacky Bai */ 188*66345b8bSJacky Bai udelay(100); 18981136819SBai Ping } 190