aboutsummaryrefslogtreecommitdiff
path: root/sys/arm/qualcomm/qcom_cpu_kpssv2.c
blob: 4193f952549b0eede1b237be9f6957dbd3bd8106 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2021 Adrian Chadd <adrian@FreeBSD.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "opt_platform.h"

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/reboot.h>
#include <sys/devmap.h>
#include <sys/smp.h>

#include <vm/vm.h>

#include <machine/cpu.h>
#include <machine/bus.h>
#include <machine/intr.h>
#include <machine/machdep.h>
#include <machine/platformvar.h>
#include <machine/smp.h>

#include <dev/fdt/fdt_common.h>
#include <dev/ofw/openfirm.h>
#include <dev/ofw/ofw_cpu.h>

#include <arm/qualcomm/qcom_cpu_kpssv2_reg.h>
#include <arm/qualcomm/qcom_cpu_kpssv2.h>

#include "platform_if.h"

/*
 * Since DELAY() hangs this early, we need some way to
 * delay things to settle.
 */
static inline void
loop_delay(int usec)
{
	int lcount = usec * 100000;

	for (volatile int i = 0; i < lcount; i++)
		;
}

/*
 * This is the KPSSv2 (eg IPQ4018) regulator path for CPU
 * and shared L2 cache power-on.
 */
boolean_t
qcom_cpu_kpssv2_regulator_start(u_int id, phandle_t node)
{
	phandle_t acc_phandle, l2_phandle, saw_phandle;
	bus_space_tag_t acc_tag, saw_tag;
	bus_space_handle_t acc_handle, saw_handle;
	bus_size_t acc_sz, saw_sz;
	ssize_t sret;
	int ret;
	uint32_t reg_val;

	/*
	 * We don't need to power up CPU 0!  This will power it
	 * down first and ... then everything hangs.
	 */
	if (id == 0)
		return true;

	/*
	 * Walk the qcom,acc and next-level-cache entries to find their
	 * child phandles and thus regulators.
	 *
	 * The qcom,acc is a phandle to a node.
	 *
	 * The next-level-cache actually is a phandle through to a qcom,saw
	 * entry.
	 */
	sret = OF_getencprop(node, "qcom,acc", (void *) &acc_phandle,
	    sizeof(acc_phandle));
	if (sret != sizeof(acc_phandle))
		panic("***couldn't get phandle for qcom,acc");
	acc_phandle = OF_node_from_xref(acc_phandle);

	sret = OF_getencprop(node, "next-level-cache", (void *) &l2_phandle,
	    sizeof(l2_phandle));
	if (sret != sizeof(l2_phandle))
		panic("***couldn't get phandle for next-level-cache");
	l2_phandle = OF_node_from_xref(l2_phandle);

	sret = OF_getencprop(l2_phandle, "qcom,saw", (void *) &saw_phandle,
	    sizeof(saw_phandle));
	if (sret != sizeof(saw_phandle))
		panic("***couldn't get phandle for qcom,saw");
	l2_phandle = OF_node_from_xref(l2_phandle);

	/*
	 * Now that we have the phandles referencing the correct locations,
	 * do some KVA mappings so we can go access the registers.
	 */
	ret = OF_decode_addr(acc_phandle, 0, &acc_tag, &acc_handle, &acc_sz);
	if (ret != 0)
		panic("*** couldn't map qcom,acc space (%d)", ret);
	ret = OF_decode_addr(saw_phandle, 0, &saw_tag, &saw_handle, &saw_sz);
	if (ret != 0)
		panic("*** couldn't map next-level-cache -> "
		    "qcom,saw space (%d)", ret);

	/*
	 * Power sequencing to ensure the cores are off, then power them on
	 * and bring them out of reset.
	 */

	/*
	 * BHS: off
	 * LDO: bypassed, powered off
	 */
	reg_val = (64 << QCOM_APC_PWR_GATE_CTL_BHS_CNT_SHIFT)
	    | (0x3f << QCOM_APC_PWR_GATE_CTL_LDO_PWR_DWN_SHIFT)
	    | QCOM_APC_PWR_GATE_CTL_BHS_EN;
	bus_space_write_4(acc_tag, acc_handle, QCOM_APC_PWR_GATE_CTL, reg_val);
	mb();
	/* Settle time */
	loop_delay(1);

	/*
	 * Start up BHS segments.
	 */
	reg_val |= 0x3f << QCOM_APC_PWR_GATE_CTL_BHS_SEG_SHIFT;
	bus_space_write_4(acc_tag, acc_handle, QCOM_APC_PWR_GATE_CTL, reg_val);
	mb();
	/* Settle time */
	loop_delay(1);

	/*
	 * Switch on the LDO bypass; BHS will now supply power.
	 */
	reg_val |= 0x3f << QCOM_APC_PWR_GATE_CTL_LDO_BYP_SHIFT;
	bus_space_write_4(acc_tag, acc_handle, QCOM_APC_PWR_GATE_CTL, reg_val);

	/*
	 * Shared L2 regulator control.
	 */
	bus_space_write_4(saw_tag, saw_handle, QCOM_APCS_SAW2_2_VCTL, 0x10003);
	mb();
	/* Settle time */
	loop_delay(50);

	/*
	 * Put the core in reset.
	 */
	reg_val = QCOM_APCS_CPU_PWR_CTL_COREPOR_RST
	    | QCOM_APCS_CPU_PWR_CTL_CLAMP;
	bus_space_write_4(acc_tag, acc_handle, QCOM_APCS_CPU_PWR_CTL, reg_val);
	mb();
	loop_delay(2);

	/*
	 * Remove power-down clamp.
	 */
	reg_val &= ~QCOM_APCS_CPU_PWR_CTL_CLAMP;
	bus_space_write_4(acc_tag, acc_handle, QCOM_APCS_CPU_PWR_CTL, reg_val);
	mb();
	loop_delay(2);

	/*
	 * Clear core power reset.
	 */
	reg_val &= ~QCOM_APCS_CPU_PWR_CTL_COREPOR_RST;
	bus_space_write_4(acc_tag, acc_handle, QCOM_APCS_CPU_PWR_CTL, reg_val);
	mb();

	/*
	 * The power is ready, the core is out of reset, signal the core
	 * to power up.
	 */
	reg_val |= QCOM_APCS_CPU_PWR_CTL_CORE_PWRD_UP;
	bus_space_write_4(acc_tag, acc_handle, QCOM_APCS_CPU_PWR_CTL, reg_val);
	mb();

	/*
	 * Finished with these KVA mappings, so release them.
	 */
	bus_space_unmap(acc_tag, acc_handle, acc_sz);
	bus_space_unmap(saw_tag, saw_handle, saw_sz);

	return true;
}