1d3514c29SAdrian Chadd /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
31d3514c29SAdrian Chadd #include <sys/systm.h>
32d3514c29SAdrian Chadd #include <sys/bus.h>
33d3514c29SAdrian Chadd #include <sys/reboot.h>
34d3514c29SAdrian Chadd #include <sys/devmap.h>
35d3514c29SAdrian Chadd #include <sys/smp.h>
36d3514c29SAdrian Chadd
37d3514c29SAdrian Chadd #include <vm/vm.h>
38d3514c29SAdrian Chadd
39d3514c29SAdrian Chadd #include <machine/cpu.h>
40d3514c29SAdrian Chadd #include <machine/bus.h>
41d3514c29SAdrian Chadd #include <machine/intr.h>
42d3514c29SAdrian Chadd #include <machine/machdep.h>
43d3514c29SAdrian Chadd #include <machine/platformvar.h>
44d3514c29SAdrian Chadd #include <machine/smp.h>
45d3514c29SAdrian Chadd
46d3514c29SAdrian Chadd #include <dev/fdt/fdt_common.h>
47d3514c29SAdrian Chadd #include <dev/ofw/openfirm.h>
48d3514c29SAdrian Chadd #include <dev/ofw/ofw_cpu.h>
49d3514c29SAdrian Chadd
50d3514c29SAdrian Chadd #include <arm/qualcomm/qcom_cpu_kpssv2_reg.h>
51d3514c29SAdrian Chadd #include <arm/qualcomm/qcom_cpu_kpssv2.h>
52d3514c29SAdrian Chadd
53d3514c29SAdrian Chadd #include "platform_if.h"
54d3514c29SAdrian Chadd
55d3514c29SAdrian Chadd /*
56d3514c29SAdrian Chadd * Since DELAY() hangs this early, we need some way to
57d3514c29SAdrian Chadd * delay things to settle.
58d3514c29SAdrian Chadd */
59d3514c29SAdrian Chadd static inline void
loop_delay(int usec)60d3514c29SAdrian Chadd loop_delay(int usec)
61d3514c29SAdrian Chadd {
62d3514c29SAdrian Chadd int lcount = usec * 100000;
63d3514c29SAdrian Chadd
64d3514c29SAdrian Chadd for (volatile int i = 0; i < lcount; i++)
65d3514c29SAdrian Chadd ;
66d3514c29SAdrian Chadd }
67d3514c29SAdrian Chadd
68d3514c29SAdrian Chadd /*
69d3514c29SAdrian Chadd * This is the KPSSv2 (eg IPQ4018) regulator path for CPU
70d3514c29SAdrian Chadd * and shared L2 cache power-on.
71d3514c29SAdrian Chadd */
723ed646d8SMina Galić bool
qcom_cpu_kpssv2_regulator_start(u_int id,phandle_t node)73d3514c29SAdrian Chadd qcom_cpu_kpssv2_regulator_start(u_int id, phandle_t node)
74d3514c29SAdrian Chadd {
75d3514c29SAdrian Chadd phandle_t acc_phandle, l2_phandle, saw_phandle;
76d3514c29SAdrian Chadd bus_space_tag_t acc_tag, saw_tag;
77d3514c29SAdrian Chadd bus_space_handle_t acc_handle, saw_handle;
78d3514c29SAdrian Chadd bus_size_t acc_sz, saw_sz;
79d3514c29SAdrian Chadd ssize_t sret;
80d3514c29SAdrian Chadd int ret;
81d3514c29SAdrian Chadd uint32_t reg_val;
82d3514c29SAdrian Chadd
83d3514c29SAdrian Chadd /*
84d3514c29SAdrian Chadd * We don't need to power up CPU 0! This will power it
85d3514c29SAdrian Chadd * down first and ... then everything hangs.
86d3514c29SAdrian Chadd */
87d3514c29SAdrian Chadd if (id == 0)
88d3514c29SAdrian Chadd return true;
89d3514c29SAdrian Chadd
90d3514c29SAdrian Chadd /*
91d3514c29SAdrian Chadd * Walk the qcom,acc and next-level-cache entries to find their
92d3514c29SAdrian Chadd * child phandles and thus regulators.
93d3514c29SAdrian Chadd *
94d3514c29SAdrian Chadd * The qcom,acc is a phandle to a node.
95d3514c29SAdrian Chadd *
96d3514c29SAdrian Chadd * The next-level-cache actually is a phandle through to a qcom,saw
97d3514c29SAdrian Chadd * entry.
98d3514c29SAdrian Chadd */
99d3514c29SAdrian Chadd sret = OF_getencprop(node, "qcom,acc", (void *) &acc_phandle,
100d3514c29SAdrian Chadd sizeof(acc_phandle));
101d3514c29SAdrian Chadd if (sret != sizeof(acc_phandle))
102d3514c29SAdrian Chadd panic("***couldn't get phandle for qcom,acc");
103d3514c29SAdrian Chadd acc_phandle = OF_node_from_xref(acc_phandle);
104d3514c29SAdrian Chadd
105d3514c29SAdrian Chadd sret = OF_getencprop(node, "next-level-cache", (void *) &l2_phandle,
106d3514c29SAdrian Chadd sizeof(l2_phandle));
107d3514c29SAdrian Chadd if (sret != sizeof(l2_phandle))
108d3514c29SAdrian Chadd panic("***couldn't get phandle for next-level-cache");
109d3514c29SAdrian Chadd l2_phandle = OF_node_from_xref(l2_phandle);
110d3514c29SAdrian Chadd
111d3514c29SAdrian Chadd sret = OF_getencprop(l2_phandle, "qcom,saw", (void *) &saw_phandle,
112d3514c29SAdrian Chadd sizeof(saw_phandle));
113d3514c29SAdrian Chadd if (sret != sizeof(saw_phandle))
114d3514c29SAdrian Chadd panic("***couldn't get phandle for qcom,saw");
115d3514c29SAdrian Chadd l2_phandle = OF_node_from_xref(l2_phandle);
116d3514c29SAdrian Chadd
117d3514c29SAdrian Chadd /*
118d3514c29SAdrian Chadd * Now that we have the phandles referencing the correct locations,
119d3514c29SAdrian Chadd * do some KVA mappings so we can go access the registers.
120d3514c29SAdrian Chadd */
121d3514c29SAdrian Chadd ret = OF_decode_addr(acc_phandle, 0, &acc_tag, &acc_handle, &acc_sz);
122d3514c29SAdrian Chadd if (ret != 0)
123d3514c29SAdrian Chadd panic("*** couldn't map qcom,acc space (%d)", ret);
124d3514c29SAdrian Chadd ret = OF_decode_addr(saw_phandle, 0, &saw_tag, &saw_handle, &saw_sz);
125d3514c29SAdrian Chadd if (ret != 0)
126d3514c29SAdrian Chadd panic("*** couldn't map next-level-cache -> "
127d3514c29SAdrian Chadd "qcom,saw space (%d)", ret);
128d3514c29SAdrian Chadd
129d3514c29SAdrian Chadd /*
130d3514c29SAdrian Chadd * Power sequencing to ensure the cores are off, then power them on
131d3514c29SAdrian Chadd * and bring them out of reset.
132d3514c29SAdrian Chadd */
133d3514c29SAdrian Chadd
134d3514c29SAdrian Chadd /*
135d3514c29SAdrian Chadd * BHS: off
136d3514c29SAdrian Chadd * LDO: bypassed, powered off
137d3514c29SAdrian Chadd */
138d3514c29SAdrian Chadd reg_val = (64 << QCOM_APC_PWR_GATE_CTL_BHS_CNT_SHIFT)
139d3514c29SAdrian Chadd | (0x3f << QCOM_APC_PWR_GATE_CTL_LDO_PWR_DWN_SHIFT)
140d3514c29SAdrian Chadd | QCOM_APC_PWR_GATE_CTL_BHS_EN;
141d3514c29SAdrian Chadd bus_space_write_4(acc_tag, acc_handle, QCOM_APC_PWR_GATE_CTL, reg_val);
142d3514c29SAdrian Chadd mb();
143d3514c29SAdrian Chadd /* Settle time */
144d3514c29SAdrian Chadd loop_delay(1);
145d3514c29SAdrian Chadd
146d3514c29SAdrian Chadd /*
147d3514c29SAdrian Chadd * Start up BHS segments.
148d3514c29SAdrian Chadd */
149d3514c29SAdrian Chadd reg_val |= 0x3f << QCOM_APC_PWR_GATE_CTL_BHS_SEG_SHIFT;
150d3514c29SAdrian Chadd bus_space_write_4(acc_tag, acc_handle, QCOM_APC_PWR_GATE_CTL, reg_val);
151d3514c29SAdrian Chadd mb();
152d3514c29SAdrian Chadd /* Settle time */
153d3514c29SAdrian Chadd loop_delay(1);
154d3514c29SAdrian Chadd
155d3514c29SAdrian Chadd /*
156d3514c29SAdrian Chadd * Switch on the LDO bypass; BHS will now supply power.
157d3514c29SAdrian Chadd */
158d3514c29SAdrian Chadd reg_val |= 0x3f << QCOM_APC_PWR_GATE_CTL_LDO_BYP_SHIFT;
159d3514c29SAdrian Chadd bus_space_write_4(acc_tag, acc_handle, QCOM_APC_PWR_GATE_CTL, reg_val);
160d3514c29SAdrian Chadd
161d3514c29SAdrian Chadd /*
162d3514c29SAdrian Chadd * Shared L2 regulator control.
163d3514c29SAdrian Chadd */
164d3514c29SAdrian Chadd bus_space_write_4(saw_tag, saw_handle, QCOM_APCS_SAW2_2_VCTL, 0x10003);
165d3514c29SAdrian Chadd mb();
166d3514c29SAdrian Chadd /* Settle time */
167d3514c29SAdrian Chadd loop_delay(50);
168d3514c29SAdrian Chadd
169d3514c29SAdrian Chadd /*
170d3514c29SAdrian Chadd * Put the core in reset.
171d3514c29SAdrian Chadd */
172d3514c29SAdrian Chadd reg_val = QCOM_APCS_CPU_PWR_CTL_COREPOR_RST
173d3514c29SAdrian Chadd | QCOM_APCS_CPU_PWR_CTL_CLAMP;
174d3514c29SAdrian Chadd bus_space_write_4(acc_tag, acc_handle, QCOM_APCS_CPU_PWR_CTL, reg_val);
175d3514c29SAdrian Chadd mb();
176d3514c29SAdrian Chadd loop_delay(2);
177d3514c29SAdrian Chadd
178d3514c29SAdrian Chadd /*
179d3514c29SAdrian Chadd * Remove power-down clamp.
180d3514c29SAdrian Chadd */
181d3514c29SAdrian Chadd reg_val &= ~QCOM_APCS_CPU_PWR_CTL_CLAMP;
182d3514c29SAdrian Chadd bus_space_write_4(acc_tag, acc_handle, QCOM_APCS_CPU_PWR_CTL, reg_val);
183d3514c29SAdrian Chadd mb();
184d3514c29SAdrian Chadd loop_delay(2);
185d3514c29SAdrian Chadd
186d3514c29SAdrian Chadd /*
187d3514c29SAdrian Chadd * Clear core power reset.
188d3514c29SAdrian Chadd */
189d3514c29SAdrian Chadd reg_val &= ~QCOM_APCS_CPU_PWR_CTL_COREPOR_RST;
190d3514c29SAdrian Chadd bus_space_write_4(acc_tag, acc_handle, QCOM_APCS_CPU_PWR_CTL, reg_val);
191d3514c29SAdrian Chadd mb();
192d3514c29SAdrian Chadd
193d3514c29SAdrian Chadd /*
194d3514c29SAdrian Chadd * The power is ready, the core is out of reset, signal the core
195d3514c29SAdrian Chadd * to power up.
196d3514c29SAdrian Chadd */
197d3514c29SAdrian Chadd reg_val |= QCOM_APCS_CPU_PWR_CTL_CORE_PWRD_UP;
198d3514c29SAdrian Chadd bus_space_write_4(acc_tag, acc_handle, QCOM_APCS_CPU_PWR_CTL, reg_val);
199d3514c29SAdrian Chadd mb();
200d3514c29SAdrian Chadd
201d3514c29SAdrian Chadd /*
202d3514c29SAdrian Chadd * Finished with these KVA mappings, so release them.
203d3514c29SAdrian Chadd */
204d3514c29SAdrian Chadd bus_space_unmap(acc_tag, acc_handle, acc_sz);
205d3514c29SAdrian Chadd bus_space_unmap(saw_tag, saw_handle, saw_sz);
206d3514c29SAdrian Chadd
207d3514c29SAdrian Chadd return true;
208d3514c29SAdrian Chadd }
209