1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2013, ARM Ltd 4 * Copyright (c) 2014, Allwinner Technology Co., Ltd. 5 * Copyright (c) 2018, Linaro Limited 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright notice, 12 * this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright notice, 15 * this list of conditions and the following disclaimer in the documentation 16 * and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <compiler.h> 32 #include <console.h> 33 #include <io.h> 34 #include <stdint.h> 35 #include <kernel/boot.h> 36 #include <kernel/misc.h> 37 #include <kernel/panic.h> 38 #include <kernel/delay.h> 39 #include <mm/core_mmu.h> 40 #include <mm/core_memprot.h> 41 #include <mm/tee_pager.h> 42 #include <platform_config.h> 43 #include <sm/optee_smc.h> 44 #include <sm/psci.h> 45 #include <arm32.h> 46 47 #define REG_CPUCFG_RES0 (0x0000) 48 #define REG_CPUCFG_CPU_RST(cpu) (0x0040 + (cpu) * (0x0040)) 49 #define REG_CPUCFG_GEN_CTRL (0x0184) 50 #define REG_CPUCFG_PRIV0 (0x01a4) 51 #define REG_CPUCFG_DBG_CTRL1 (0x01e4) 52 #define REG_PRCM_CPU_PWROFF (0x0100) 53 #define REG_PRCM_CPU_PWR_CLAMP(cpu) (0x0140 + (cpu) * (0x0004)) 54 55 int psci_features(uint32_t psci_fid) 56 { 57 switch (psci_fid) { 58 #ifdef CFG_BOOT_SECONDARY_REQUEST 59 case PSCI_CPU_ON: 60 return 0; 61 #endif 62 63 default: 64 return PSCI_RET_NOT_SUPPORTED; 65 } 66 } 67 68 #ifdef CFG_BOOT_SECONDARY_REQUEST 69 int psci_cpu_on(uint32_t core_idx, uint32_t entry, 70 uint32_t context_id) 71 { 72 vaddr_t base = (vaddr_t)phys_to_virt(SUNXI_PRCM_BASE, MEM_AREA_IO_SEC); 73 vaddr_t cpucfg = (vaddr_t)phys_to_virt(SUNXI_CPUCFG_BASE, 74 MEM_AREA_IO_SEC); 75 uint32_t tmpff; 76 uint32_t val; 77 78 assert(base); 79 assert(cpucfg); 80 81 if ((core_idx == 0) || (core_idx >= CFG_TEE_CORE_NB_CORE)) 82 return PSCI_RET_INVALID_PARAMETERS; 83 84 /* set secondary cores' NS entry addresses */ 85 boot_set_core_ns_entry(core_idx, entry, context_id); 86 87 val = virt_to_phys((void *)TEE_TEXT_VA_START); 88 89 /* set entry address */ 90 DMSG("set entry address for CPU %d", core_idx); 91 io_write32(cpucfg + REG_CPUCFG_PRIV0, val); 92 93 /* assert reset on target CPU */ 94 DMSG("assert reset on target CPU %d", core_idx); 95 io_write32(cpucfg + REG_CPUCFG_CPU_RST(core_idx), 0); 96 97 /* invalidate L1 cache */ 98 DMSG("invalidate L1 cache for CPU %d", core_idx); 99 io_clrbits32(cpucfg + REG_CPUCFG_GEN_CTRL, BIT32(core_idx)); 100 101 /* lock CPU (Disable external debug access) */ 102 DMSG("lock CPU %d", core_idx); 103 io_clrbits32(cpucfg + REG_CPUCFG_DBG_CTRL1, BIT32(core_idx)); 104 105 /* release clamp */ 106 DMSG("release clamp for CPU %d", core_idx); 107 tmpff = 0x1ff; 108 do { 109 tmpff >>= 1; 110 io_write32(base + REG_PRCM_CPU_PWR_CLAMP(core_idx), tmpff); 111 } while (tmpff); 112 mdelay(10); 113 114 /* clear power gating */ 115 DMSG("clear power gating for CPU %d", core_idx); 116 io_clrbits32(base + REG_PRCM_CPU_PWROFF, BIT32(core_idx)); 117 udelay(1000); 118 119 /* de-assert reset on target CPU */ 120 DMSG("de-assert reset on target CPU %d", core_idx); 121 io_write32(cpucfg + REG_CPUCFG_CPU_RST(core_idx), 0x03); 122 123 /* unlock CPU (enable external debug access) */ 124 DMSG("unlock CPU %d", core_idx); 125 io_setbits32(cpucfg + REG_CPUCFG_DBG_CTRL1, BIT32(core_idx)); 126 127 return PSCI_RET_SUCCESS; 128 } 129 130 int __noreturn psci_cpu_off(void) 131 { 132 uint32_t core_id; 133 vaddr_t base = (vaddr_t)phys_to_virt(SUNXI_PRCM_BASE, MEM_AREA_IO_SEC); 134 vaddr_t cpucfg = (vaddr_t)phys_to_virt(SUNXI_CPUCFG_BASE, 135 MEM_AREA_IO_SEC); 136 137 core_id = get_core_pos(); 138 139 DMSG("core_id: %" PRIu32, core_id); 140 141 #ifdef CFG_PSCI_ARM32 142 psci_armv7_cpu_off(); 143 #endif /* CFG_PSCI_ARM32 */ 144 145 assert(base); 146 assert(cpucfg); 147 148 /* set power gating */ 149 DMSG("set power gating for cpu %d", core_id); 150 io_setbits32(base + REG_PRCM_CPU_PWROFF, BIT32(core_id)); 151 152 /* Activate power clamp */ 153 DMSG("Activate power clamp for cpu %d", core_id); 154 io_write32(base + REG_PRCM_CPU_PWR_CLAMP(core_id), 0xff); 155 156 while (true) 157 wfi(); 158 } 159 #endif 160