1d3514c29SAdrian Chadd /*- 2d3514c29SAdrian Chadd * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3d3514c29SAdrian Chadd * 4d3514c29SAdrian Chadd * Copyright (c) 2021 Adrian Chadd <adrian@FreeBSD.org> 5d3514c29SAdrian Chadd * 6d3514c29SAdrian Chadd * Redistribution and use in source and binary forms, with or without 7d3514c29SAdrian Chadd * modification, are permitted provided that the following conditions 8d3514c29SAdrian Chadd * are met: 9d3514c29SAdrian Chadd * 1. Redistributions of source code must retain the above copyright 10d3514c29SAdrian Chadd * notice, this list of conditions and the following disclaimer. 11d3514c29SAdrian Chadd * 2. Redistributions in binary form must reproduce the above copyright 12d3514c29SAdrian Chadd * notice, this list of conditions and the following disclaimer in the 13d3514c29SAdrian Chadd * documentation and/or other materials provided with the distribution. 14d3514c29SAdrian Chadd * 15d3514c29SAdrian Chadd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16d3514c29SAdrian Chadd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17d3514c29SAdrian Chadd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18d3514c29SAdrian Chadd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19d3514c29SAdrian Chadd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20d3514c29SAdrian Chadd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21d3514c29SAdrian Chadd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22d3514c29SAdrian Chadd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23d3514c29SAdrian Chadd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24d3514c29SAdrian Chadd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25d3514c29SAdrian Chadd * SUCH DAMAGE. 26d3514c29SAdrian Chadd */ 27d3514c29SAdrian Chadd 28d3514c29SAdrian Chadd #include "opt_platform.h" 29d3514c29SAdrian Chadd 30d3514c29SAdrian Chadd #include <sys/cdefs.h> 31d3514c29SAdrian Chadd __FBSDID("$FreeBSD$"); 32d3514c29SAdrian Chadd 33d3514c29SAdrian Chadd #include <sys/param.h> 34d3514c29SAdrian Chadd #include <sys/systm.h> 35d3514c29SAdrian Chadd #include <sys/bus.h> 36d3514c29SAdrian Chadd #include <sys/reboot.h> 37d3514c29SAdrian Chadd #include <sys/devmap.h> 38d3514c29SAdrian Chadd #include <sys/smp.h> 39d3514c29SAdrian Chadd 40d3514c29SAdrian Chadd #include <vm/vm.h> 41d3514c29SAdrian Chadd 42d3514c29SAdrian Chadd #include <machine/cpu.h> 43d3514c29SAdrian Chadd #include <machine/bus.h> 44d3514c29SAdrian Chadd #include <machine/intr.h> 45d3514c29SAdrian Chadd #include <machine/machdep.h> 46d3514c29SAdrian Chadd #include <machine/platformvar.h> 47d3514c29SAdrian Chadd #include <machine/smp.h> 48d3514c29SAdrian Chadd 49d3514c29SAdrian Chadd #include <dev/fdt/fdt_common.h> 50d3514c29SAdrian Chadd #include <dev/ofw/openfirm.h> 51d3514c29SAdrian Chadd #include <dev/ofw/ofw_cpu.h> 52d3514c29SAdrian Chadd 53d3514c29SAdrian Chadd #include <arm/qualcomm/qcom_cpu_kpssv2_reg.h> 54d3514c29SAdrian Chadd #include <arm/qualcomm/qcom_cpu_kpssv2.h> 55d3514c29SAdrian Chadd 56d3514c29SAdrian Chadd #include "platform_if.h" 57d3514c29SAdrian Chadd 58d3514c29SAdrian Chadd /* 59d3514c29SAdrian Chadd * Since DELAY() hangs this early, we need some way to 60d3514c29SAdrian Chadd * delay things to settle. 61d3514c29SAdrian Chadd */ 62d3514c29SAdrian Chadd static inline void 63d3514c29SAdrian Chadd loop_delay(int usec) 64d3514c29SAdrian Chadd { 65d3514c29SAdrian Chadd int lcount = usec * 100000; 66d3514c29SAdrian Chadd 67d3514c29SAdrian Chadd for (volatile int i = 0; i < lcount; i++) 68d3514c29SAdrian Chadd ; 69d3514c29SAdrian Chadd } 70d3514c29SAdrian Chadd 71d3514c29SAdrian Chadd /* 72d3514c29SAdrian Chadd * This is the KPSSv2 (eg IPQ4018) regulator path for CPU 73d3514c29SAdrian Chadd * and shared L2 cache power-on. 74d3514c29SAdrian Chadd */ 75d3514c29SAdrian Chadd boolean_t 76d3514c29SAdrian Chadd qcom_cpu_kpssv2_regulator_start(u_int id, phandle_t node) 77d3514c29SAdrian Chadd { 78d3514c29SAdrian Chadd phandle_t acc_phandle, l2_phandle, saw_phandle; 79d3514c29SAdrian Chadd bus_space_tag_t acc_tag, saw_tag; 80d3514c29SAdrian Chadd bus_space_handle_t acc_handle, saw_handle; 81d3514c29SAdrian Chadd bus_size_t acc_sz, saw_sz; 82d3514c29SAdrian Chadd ssize_t sret; 83d3514c29SAdrian Chadd int ret; 84d3514c29SAdrian Chadd uint32_t reg_val; 85d3514c29SAdrian Chadd 86d3514c29SAdrian Chadd /* 87d3514c29SAdrian Chadd * We don't need to power up CPU 0! This will power it 88d3514c29SAdrian Chadd * down first and ... then everything hangs. 89d3514c29SAdrian Chadd */ 90d3514c29SAdrian Chadd if (id == 0) 91d3514c29SAdrian Chadd return true; 92d3514c29SAdrian Chadd 93d3514c29SAdrian Chadd /* 94d3514c29SAdrian Chadd * Walk the qcom,acc and next-level-cache entries to find their 95d3514c29SAdrian Chadd * child phandles and thus regulators. 96d3514c29SAdrian Chadd * 97d3514c29SAdrian Chadd * The qcom,acc is a phandle to a node. 98d3514c29SAdrian Chadd * 99d3514c29SAdrian Chadd * The next-level-cache actually is a phandle through to a qcom,saw 100d3514c29SAdrian Chadd * entry. 101d3514c29SAdrian Chadd */ 102d3514c29SAdrian Chadd sret = OF_getencprop(node, "qcom,acc", (void *) &acc_phandle, 103d3514c29SAdrian Chadd sizeof(acc_phandle)); 104d3514c29SAdrian Chadd if (sret != sizeof(acc_phandle)) 105d3514c29SAdrian Chadd panic("***couldn't get phandle for qcom,acc"); 106d3514c29SAdrian Chadd acc_phandle = OF_node_from_xref(acc_phandle); 107d3514c29SAdrian Chadd 108d3514c29SAdrian Chadd sret = OF_getencprop(node, "next-level-cache", (void *) &l2_phandle, 109d3514c29SAdrian Chadd sizeof(l2_phandle)); 110d3514c29SAdrian Chadd if (sret != sizeof(l2_phandle)) 111d3514c29SAdrian Chadd panic("***couldn't get phandle for next-level-cache"); 112d3514c29SAdrian Chadd l2_phandle = OF_node_from_xref(l2_phandle); 113d3514c29SAdrian Chadd 114d3514c29SAdrian Chadd sret = OF_getencprop(l2_phandle, "qcom,saw", (void *) &saw_phandle, 115d3514c29SAdrian Chadd sizeof(saw_phandle)); 116d3514c29SAdrian Chadd if (sret != sizeof(saw_phandle)) 117d3514c29SAdrian Chadd panic("***couldn't get phandle for qcom,saw"); 118d3514c29SAdrian Chadd l2_phandle = OF_node_from_xref(l2_phandle); 119d3514c29SAdrian Chadd 120d3514c29SAdrian Chadd /* 121d3514c29SAdrian Chadd * Now that we have the phandles referencing the correct locations, 122d3514c29SAdrian Chadd * do some KVA mappings so we can go access the registers. 123d3514c29SAdrian Chadd */ 124d3514c29SAdrian Chadd ret = OF_decode_addr(acc_phandle, 0, &acc_tag, &acc_handle, &acc_sz); 125d3514c29SAdrian Chadd if (ret != 0) 126d3514c29SAdrian Chadd panic("*** couldn't map qcom,acc space (%d)", ret); 127d3514c29SAdrian Chadd ret = OF_decode_addr(saw_phandle, 0, &saw_tag, &saw_handle, &saw_sz); 128d3514c29SAdrian Chadd if (ret != 0) 129d3514c29SAdrian Chadd panic("*** couldn't map next-level-cache -> " 130d3514c29SAdrian Chadd "qcom,saw space (%d)", ret); 131d3514c29SAdrian Chadd 132d3514c29SAdrian Chadd /* 133d3514c29SAdrian Chadd * Power sequencing to ensure the cores are off, then power them on 134d3514c29SAdrian Chadd * and bring them out of reset. 135d3514c29SAdrian Chadd */ 136d3514c29SAdrian Chadd 137d3514c29SAdrian Chadd /* 138d3514c29SAdrian Chadd * BHS: off 139d3514c29SAdrian Chadd * LDO: bypassed, powered off 140d3514c29SAdrian Chadd */ 141d3514c29SAdrian Chadd reg_val = (64 << QCOM_APC_PWR_GATE_CTL_BHS_CNT_SHIFT) 142d3514c29SAdrian Chadd | (0x3f << QCOM_APC_PWR_GATE_CTL_LDO_PWR_DWN_SHIFT) 143d3514c29SAdrian Chadd | QCOM_APC_PWR_GATE_CTL_BHS_EN; 144d3514c29SAdrian Chadd bus_space_write_4(acc_tag, acc_handle, QCOM_APC_PWR_GATE_CTL, reg_val); 145d3514c29SAdrian Chadd mb(); 146d3514c29SAdrian Chadd /* Settle time */ 147d3514c29SAdrian Chadd loop_delay(1); 148d3514c29SAdrian Chadd 149d3514c29SAdrian Chadd /* 150d3514c29SAdrian Chadd * Start up BHS segments. 151d3514c29SAdrian Chadd */ 152d3514c29SAdrian Chadd reg_val |= 0x3f << QCOM_APC_PWR_GATE_CTL_BHS_SEG_SHIFT; 153d3514c29SAdrian Chadd bus_space_write_4(acc_tag, acc_handle, QCOM_APC_PWR_GATE_CTL, reg_val); 154d3514c29SAdrian Chadd mb(); 155d3514c29SAdrian Chadd /* Settle time */ 156d3514c29SAdrian Chadd loop_delay(1); 157d3514c29SAdrian Chadd 158d3514c29SAdrian Chadd /* 159d3514c29SAdrian Chadd * Switch on the LDO bypass; BHS will now supply power. 160d3514c29SAdrian Chadd */ 161d3514c29SAdrian Chadd reg_val |= 0x3f << QCOM_APC_PWR_GATE_CTL_LDO_BYP_SHIFT; 162d3514c29SAdrian Chadd bus_space_write_4(acc_tag, acc_handle, QCOM_APC_PWR_GATE_CTL, reg_val); 163d3514c29SAdrian Chadd 164d3514c29SAdrian Chadd /* 165d3514c29SAdrian Chadd * Shared L2 regulator control. 166d3514c29SAdrian Chadd */ 167d3514c29SAdrian Chadd bus_space_write_4(saw_tag, saw_handle, QCOM_APCS_SAW2_2_VCTL, 0x10003); 168d3514c29SAdrian Chadd mb(); 169d3514c29SAdrian Chadd /* Settle time */ 170d3514c29SAdrian Chadd loop_delay(50); 171d3514c29SAdrian Chadd 172d3514c29SAdrian Chadd /* 173d3514c29SAdrian Chadd * Put the core in reset. 174d3514c29SAdrian Chadd */ 175d3514c29SAdrian Chadd reg_val = QCOM_APCS_CPU_PWR_CTL_COREPOR_RST 176d3514c29SAdrian Chadd | QCOM_APCS_CPU_PWR_CTL_CLAMP; 177d3514c29SAdrian Chadd bus_space_write_4(acc_tag, acc_handle, QCOM_APCS_CPU_PWR_CTL, reg_val); 178d3514c29SAdrian Chadd mb(); 179d3514c29SAdrian Chadd loop_delay(2); 180d3514c29SAdrian Chadd 181d3514c29SAdrian Chadd /* 182d3514c29SAdrian Chadd * Remove power-down clamp. 183d3514c29SAdrian Chadd */ 184d3514c29SAdrian Chadd reg_val &= ~QCOM_APCS_CPU_PWR_CTL_CLAMP; 185d3514c29SAdrian Chadd bus_space_write_4(acc_tag, acc_handle, QCOM_APCS_CPU_PWR_CTL, reg_val); 186d3514c29SAdrian Chadd mb(); 187d3514c29SAdrian Chadd loop_delay(2); 188d3514c29SAdrian Chadd 189d3514c29SAdrian Chadd /* 190d3514c29SAdrian Chadd * Clear core power reset. 191d3514c29SAdrian Chadd */ 192d3514c29SAdrian Chadd reg_val &= ~QCOM_APCS_CPU_PWR_CTL_COREPOR_RST; 193d3514c29SAdrian Chadd bus_space_write_4(acc_tag, acc_handle, QCOM_APCS_CPU_PWR_CTL, reg_val); 194d3514c29SAdrian Chadd mb(); 195d3514c29SAdrian Chadd 196d3514c29SAdrian Chadd /* 197d3514c29SAdrian Chadd * The power is ready, the core is out of reset, signal the core 198d3514c29SAdrian Chadd * to power up. 199d3514c29SAdrian Chadd */ 200d3514c29SAdrian Chadd reg_val |= QCOM_APCS_CPU_PWR_CTL_CORE_PWRD_UP; 201d3514c29SAdrian Chadd bus_space_write_4(acc_tag, acc_handle, QCOM_APCS_CPU_PWR_CTL, reg_val); 202d3514c29SAdrian Chadd mb(); 203d3514c29SAdrian Chadd 204d3514c29SAdrian Chadd /* 205d3514c29SAdrian Chadd * Finished with these KVA mappings, so release them. 206d3514c29SAdrian Chadd */ 207d3514c29SAdrian Chadd bus_space_unmap(acc_tag, acc_handle, acc_sz); 208d3514c29SAdrian Chadd bus_space_unmap(saw_tag, saw_handle, saw_sz); 209d3514c29SAdrian Chadd 210d3514c29SAdrian Chadd return true; 211d3514c29SAdrian Chadd } 212