xref: /freebsd/sys/dev/cpufreq/cpufreq_dt.c (revision a701764e9c50ecc0df7b87e4fd6f85d3237b3683)
1319336a9SJared McNeill /*-
241a76289SEmmanuel Vadot  * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.Org>
3319336a9SJared McNeill  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
4319336a9SJared McNeill  * All rights reserved.
5319336a9SJared McNeill  *
6319336a9SJared McNeill  * Redistribution and use in source and binary forms, with or without
7319336a9SJared McNeill  * modification, are permitted provided that the following conditions
8319336a9SJared McNeill  * are met:
9319336a9SJared McNeill  * 1. Redistributions of source code must retain the above copyright
10319336a9SJared McNeill  *    notice, this list of conditions and the following disclaimer.
11319336a9SJared McNeill  * 2. Redistributions in binary form must reproduce the above copyright
12319336a9SJared McNeill  *    notice, this list of conditions and the following disclaimer in the
13319336a9SJared McNeill  *    documentation and/or other materials provided with the distribution.
14319336a9SJared McNeill  *
15319336a9SJared McNeill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16319336a9SJared McNeill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17319336a9SJared McNeill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18319336a9SJared McNeill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19319336a9SJared McNeill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20319336a9SJared McNeill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21319336a9SJared McNeill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22319336a9SJared McNeill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23319336a9SJared McNeill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24319336a9SJared McNeill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25319336a9SJared McNeill  * SUCH DAMAGE.
26319336a9SJared McNeill  *
27319336a9SJared McNeill  * $FreeBSD$
28319336a9SJared McNeill  */
29319336a9SJared McNeill 
30319336a9SJared McNeill /*
31319336a9SJared McNeill  * Generic DT based cpufreq driver
32319336a9SJared McNeill  */
33319336a9SJared McNeill 
34319336a9SJared McNeill #include <sys/cdefs.h>
35319336a9SJared McNeill __FBSDID("$FreeBSD$");
36319336a9SJared McNeill 
37319336a9SJared McNeill #include <sys/param.h>
38319336a9SJared McNeill #include <sys/systm.h>
39319336a9SJared McNeill #include <sys/bus.h>
40319336a9SJared McNeill #include <sys/rman.h>
41319336a9SJared McNeill #include <sys/kernel.h>
42319336a9SJared McNeill #include <sys/module.h>
43319336a9SJared McNeill #include <sys/cpu.h>
44319336a9SJared McNeill #include <sys/cpuset.h>
45319336a9SJared McNeill #include <sys/smp.h>
46319336a9SJared McNeill 
47319336a9SJared McNeill #include <dev/ofw/ofw_bus.h>
48319336a9SJared McNeill #include <dev/ofw/ofw_bus_subr.h>
49319336a9SJared McNeill 
50319336a9SJared McNeill #include <dev/extres/clk/clk.h>
51319336a9SJared McNeill #include <dev/extres/regulator/regulator.h>
52319336a9SJared McNeill 
53319336a9SJared McNeill #include "cpufreq_if.h"
54319336a9SJared McNeill 
5541a76289SEmmanuel Vadot #if 0
5641a76289SEmmanuel Vadot #define DEBUG(dev, msg...) device_printf(dev, "cpufreq_dt: " msg);
5741a76289SEmmanuel Vadot #else
5841a76289SEmmanuel Vadot #define DEBUG(dev, msg...)
5941a76289SEmmanuel Vadot #endif
6041a76289SEmmanuel Vadot 
6141a76289SEmmanuel Vadot enum opp_version {
6241a76289SEmmanuel Vadot 	OPP_V1 = 1,
6341a76289SEmmanuel Vadot 	OPP_V2,
6441a76289SEmmanuel Vadot };
6541a76289SEmmanuel Vadot 
66319336a9SJared McNeill struct cpufreq_dt_opp {
6741a76289SEmmanuel Vadot 	uint64_t	freq;
6841a76289SEmmanuel Vadot 	uint32_t	uvolt_target;
6941a76289SEmmanuel Vadot 	uint32_t	uvolt_min;
7041a76289SEmmanuel Vadot 	uint32_t	uvolt_max;
7141a76289SEmmanuel Vadot 	uint32_t	uamps;
7241a76289SEmmanuel Vadot 	uint32_t	clk_latency;
7341a76289SEmmanuel Vadot 	bool		turbo_mode;
7441a76289SEmmanuel Vadot 	bool		opp_suspend;
75319336a9SJared McNeill };
76319336a9SJared McNeill 
77319336a9SJared McNeill struct cpufreq_dt_softc {
7841a76289SEmmanuel Vadot 	device_t dev;
79319336a9SJared McNeill 	clk_t clk;
80319336a9SJared McNeill 	regulator_t reg;
81319336a9SJared McNeill 
82319336a9SJared McNeill 	struct cpufreq_dt_opp *opp;
83319336a9SJared McNeill 	ssize_t nopp;
84319336a9SJared McNeill 
85319336a9SJared McNeill 	cpuset_t cpus;
86319336a9SJared McNeill };
87319336a9SJared McNeill 
88319336a9SJared McNeill static void
89319336a9SJared McNeill cpufreq_dt_notify(device_t dev, uint64_t freq)
90319336a9SJared McNeill {
91319336a9SJared McNeill 	struct cpufreq_dt_softc *sc;
92319336a9SJared McNeill 	struct pcpu *pc;
93319336a9SJared McNeill 	int cpu;
94319336a9SJared McNeill 
95319336a9SJared McNeill 	sc = device_get_softc(dev);
96319336a9SJared McNeill 
97319336a9SJared McNeill 	CPU_FOREACH(cpu) {
98319336a9SJared McNeill 		if (CPU_ISSET(cpu, &sc->cpus)) {
99319336a9SJared McNeill 			pc = pcpu_find(cpu);
100319336a9SJared McNeill 			pc->pc_clock = freq;
101319336a9SJared McNeill 		}
102319336a9SJared McNeill 	}
103319336a9SJared McNeill }
104319336a9SJared McNeill 
105319336a9SJared McNeill static const struct cpufreq_dt_opp *
10641a76289SEmmanuel Vadot cpufreq_dt_find_opp(device_t dev, uint64_t freq)
107319336a9SJared McNeill {
108319336a9SJared McNeill 	struct cpufreq_dt_softc *sc;
109319336a9SJared McNeill 	ssize_t n;
110319336a9SJared McNeill 
111319336a9SJared McNeill 	sc = device_get_softc(dev);
112319336a9SJared McNeill 
11341a76289SEmmanuel Vadot 	DEBUG(dev, "Looking for freq %ju\n", freq);
114319336a9SJared McNeill 	for (n = 0; n < sc->nopp; n++)
11541a76289SEmmanuel Vadot 		if (CPUFREQ_CMP(sc->opp[n].freq, freq))
116319336a9SJared McNeill 			return (&sc->opp[n]);
117319336a9SJared McNeill 
11841a76289SEmmanuel Vadot 	DEBUG(dev, "Couldn't find one\n");
119319336a9SJared McNeill 	return (NULL);
120319336a9SJared McNeill }
121319336a9SJared McNeill 
122319336a9SJared McNeill static void
123319336a9SJared McNeill cpufreq_dt_opp_to_setting(device_t dev, const struct cpufreq_dt_opp *opp,
124319336a9SJared McNeill     struct cf_setting *set)
125319336a9SJared McNeill {
126319336a9SJared McNeill 	struct cpufreq_dt_softc *sc;
127319336a9SJared McNeill 
128319336a9SJared McNeill 	sc = device_get_softc(dev);
129319336a9SJared McNeill 
130319336a9SJared McNeill 	memset(set, 0, sizeof(*set));
13141a76289SEmmanuel Vadot 	set->freq = opp->freq / 1000000;
13241a76289SEmmanuel Vadot 	set->volts = opp->uvolt_target / 1000;
133319336a9SJared McNeill 	set->power = CPUFREQ_VAL_UNKNOWN;
13441a76289SEmmanuel Vadot 	set->lat = opp->clk_latency;
135319336a9SJared McNeill 	set->dev = dev;
136319336a9SJared McNeill }
137319336a9SJared McNeill 
138319336a9SJared McNeill static int
139319336a9SJared McNeill cpufreq_dt_get(device_t dev, struct cf_setting *set)
140319336a9SJared McNeill {
141319336a9SJared McNeill 	struct cpufreq_dt_softc *sc;
142319336a9SJared McNeill 	const struct cpufreq_dt_opp *opp;
143319336a9SJared McNeill 	uint64_t freq;
144319336a9SJared McNeill 
145319336a9SJared McNeill 	sc = device_get_softc(dev);
146319336a9SJared McNeill 
14741a76289SEmmanuel Vadot 	DEBUG(dev, "cpufreq_dt_get\n");
148319336a9SJared McNeill 	if (clk_get_freq(sc->clk, &freq) != 0)
149319336a9SJared McNeill 		return (ENXIO);
150319336a9SJared McNeill 
15141a76289SEmmanuel Vadot 	opp = cpufreq_dt_find_opp(dev, freq);
15241a76289SEmmanuel Vadot 	if (opp == NULL) {
15341a76289SEmmanuel Vadot 		device_printf(dev, "Can't find the current freq in opp\n");
154319336a9SJared McNeill 		return (ENOENT);
15541a76289SEmmanuel Vadot 	}
156319336a9SJared McNeill 
157319336a9SJared McNeill 	cpufreq_dt_opp_to_setting(dev, opp, set);
158319336a9SJared McNeill 
15941a76289SEmmanuel Vadot 	DEBUG(dev, "Current freq %dMhz\n", set->freq);
160319336a9SJared McNeill 	return (0);
161319336a9SJared McNeill }
162319336a9SJared McNeill 
163319336a9SJared McNeill static int
164319336a9SJared McNeill cpufreq_dt_set(device_t dev, const struct cf_setting *set)
165319336a9SJared McNeill {
166319336a9SJared McNeill 	struct cpufreq_dt_softc *sc;
167319336a9SJared McNeill 	const struct cpufreq_dt_opp *opp, *copp;
168319336a9SJared McNeill 	uint64_t freq;
169*a701764eSMichal Meloun 	int uvolt, error;
170319336a9SJared McNeill 
171319336a9SJared McNeill 	sc = device_get_softc(dev);
172319336a9SJared McNeill 
17341a76289SEmmanuel Vadot 	if (clk_get_freq(sc->clk, &freq) != 0) {
17441a76289SEmmanuel Vadot 		device_printf(dev, "Can't get current clk freq\n");
175319336a9SJared McNeill 		return (ENXIO);
17641a76289SEmmanuel Vadot 	}
177*a701764eSMichal Meloun 	/* Try to get current valtage by using regulator first. */
178*a701764eSMichal Meloun 	error = regulator_get_voltage(sc->reg, &uvolt);
179*a701764eSMichal Meloun 	if (error != 0) {
180*a701764eSMichal Meloun 		/*
181*a701764eSMichal Meloun 		 * Try oppoints table as backup way. However,
182*a701764eSMichal Meloun 		 * this is insufficient because the actual processor
183*a701764eSMichal Meloun 		 * frequency may not be in the table. PLL frequency
184*a701764eSMichal Meloun 		 * granularity can be different that granularity of
185*a701764eSMichal Meloun 		 * oppoint table.
186*a701764eSMichal Meloun 		 */
18741a76289SEmmanuel Vadot 		copp = cpufreq_dt_find_opp(sc->dev, freq);
18841a76289SEmmanuel Vadot 		if (copp == NULL) {
189*a701764eSMichal Meloun 			device_printf(dev,
190*a701764eSMichal Meloun 			    "Can't find the current freq in opp\n");
191319336a9SJared McNeill 			return (ENOENT);
19241a76289SEmmanuel Vadot 		}
193*a701764eSMichal Meloun 		uvolt = copp->uvolt_target;
194*a701764eSMichal Meloun 
195*a701764eSMichal Meloun 	}
196*a701764eSMichal Meloun 
19741a76289SEmmanuel Vadot 	opp = cpufreq_dt_find_opp(sc->dev, set->freq * 1000000);
19841a76289SEmmanuel Vadot 	if (opp == NULL) {
19941a76289SEmmanuel Vadot 		device_printf(dev, "Couldn't find an opp for this freq\n");
200319336a9SJared McNeill 		return (EINVAL);
201319336a9SJared McNeill 	}
202*a701764eSMichal Meloun 	DEBUG(sc->dev, "Current freq %ju, uvolt: %d\n", freq, uvolt);
203*a701764eSMichal Meloun 	DEBUG(sc->dev, "Target freq %ju, , uvolt: %d\n",
204*a701764eSMichal Meloun 	    opp->freq, opp->uvolt_target);
205319336a9SJared McNeill 
206*a701764eSMichal Meloun 	if (uvolt < opp->uvolt_target) {
20741a76289SEmmanuel Vadot 		DEBUG(dev, "Changing regulator from %u to %u\n",
208*a701764eSMichal Meloun 		    uvolt, opp->uvolt_target);
20941a76289SEmmanuel Vadot 		error = regulator_set_voltage(sc->reg,
21041a76289SEmmanuel Vadot 		    opp->uvolt_min,
21141a76289SEmmanuel Vadot 		    opp->uvolt_max);
212319336a9SJared McNeill 		if (error != 0) {
21341a76289SEmmanuel Vadot 			DEBUG(dev, "Failed, backout\n");
21441a76289SEmmanuel Vadot 			return (ENXIO);
21541a76289SEmmanuel Vadot 		}
21641a76289SEmmanuel Vadot 	}
21741a76289SEmmanuel Vadot 
21841a76289SEmmanuel Vadot 	DEBUG(dev, "Setting clk to %ju\n", opp->freq);
219*a701764eSMichal Meloun 	error = clk_set_freq(sc->clk, opp->freq, CLK_SET_ROUND_DOWN);
22041a76289SEmmanuel Vadot 	if (error != 0) {
22141a76289SEmmanuel Vadot 		DEBUG(dev, "Failed, backout\n");
222319336a9SJared McNeill 		/* Restore previous voltage (best effort) */
22341a76289SEmmanuel Vadot 		error = regulator_set_voltage(sc->reg,
22441a76289SEmmanuel Vadot 		    copp->uvolt_min,
22541a76289SEmmanuel Vadot 		    copp->uvolt_max);
226319336a9SJared McNeill 		return (ENXIO);
227319336a9SJared McNeill 	}
228319336a9SJared McNeill 
229*a701764eSMichal Meloun 	if (uvolt > opp->uvolt_target) {
230*a701764eSMichal Meloun 		DEBUG(dev, "Changing regulator from %u to %u\n",
231*a701764eSMichal Meloun 		    uvolt, opp->uvolt_target);
23241a76289SEmmanuel Vadot 		error = regulator_set_voltage(sc->reg,
23341a76289SEmmanuel Vadot 		    opp->uvolt_min,
23441a76289SEmmanuel Vadot 		    opp->uvolt_max);
235319336a9SJared McNeill 		if (error != 0) {
23641a76289SEmmanuel Vadot 			DEBUG(dev, "Failed to switch regulator to %d\n",
23741a76289SEmmanuel Vadot 			    opp->uvolt_target);
238319336a9SJared McNeill 			/* Restore previous CPU frequency (best effort) */
239*a701764eSMichal Meloun 			(void)clk_set_freq(sc->clk, copp->freq, 0);
240319336a9SJared McNeill 			return (ENXIO);
241319336a9SJared McNeill 		}
242319336a9SJared McNeill 	}
243319336a9SJared McNeill 
244319336a9SJared McNeill 	if (clk_get_freq(sc->clk, &freq) == 0)
245319336a9SJared McNeill 		cpufreq_dt_notify(dev, freq);
246319336a9SJared McNeill 
247319336a9SJared McNeill 	return (0);
248319336a9SJared McNeill }
249319336a9SJared McNeill 
250319336a9SJared McNeill 
251319336a9SJared McNeill static int
252319336a9SJared McNeill cpufreq_dt_type(device_t dev, int *type)
253319336a9SJared McNeill {
254319336a9SJared McNeill 	if (type == NULL)
255319336a9SJared McNeill 		return (EINVAL);
256319336a9SJared McNeill 
257319336a9SJared McNeill 	*type = CPUFREQ_TYPE_ABSOLUTE;
258319336a9SJared McNeill 	return (0);
259319336a9SJared McNeill }
260319336a9SJared McNeill 
261319336a9SJared McNeill static int
262319336a9SJared McNeill cpufreq_dt_settings(device_t dev, struct cf_setting *sets, int *count)
263319336a9SJared McNeill {
264319336a9SJared McNeill 	struct cpufreq_dt_softc *sc;
265319336a9SJared McNeill 	ssize_t n;
266319336a9SJared McNeill 
26741a76289SEmmanuel Vadot 	DEBUG(dev, "cpufreq_dt_settings\n");
268319336a9SJared McNeill 	if (sets == NULL || count == NULL)
269319336a9SJared McNeill 		return (EINVAL);
270319336a9SJared McNeill 
271319336a9SJared McNeill 	sc = device_get_softc(dev);
272319336a9SJared McNeill 
273319336a9SJared McNeill 	if (*count < sc->nopp) {
274319336a9SJared McNeill 		*count = (int)sc->nopp;
275319336a9SJared McNeill 		return (E2BIG);
276319336a9SJared McNeill 	}
277319336a9SJared McNeill 
278319336a9SJared McNeill 	for (n = 0; n < sc->nopp; n++)
279319336a9SJared McNeill 		cpufreq_dt_opp_to_setting(dev, &sc->opp[n], &sets[n]);
280319336a9SJared McNeill 
281319336a9SJared McNeill 	*count = (int)sc->nopp;
282319336a9SJared McNeill 
283319336a9SJared McNeill 	return (0);
284319336a9SJared McNeill }
285319336a9SJared McNeill 
286319336a9SJared McNeill static void
287319336a9SJared McNeill cpufreq_dt_identify(driver_t *driver, device_t parent)
288319336a9SJared McNeill {
289319336a9SJared McNeill 	phandle_t node;
290319336a9SJared McNeill 
291319336a9SJared McNeill 	/* Properties must be listed under node /cpus/cpu@0 */
292319336a9SJared McNeill 	node = ofw_bus_get_node(parent);
293319336a9SJared McNeill 
294319336a9SJared McNeill 	/* The cpu@0 node must have the following properties */
29541a76289SEmmanuel Vadot 	if (!OF_hasprop(node, "clocks") ||
296*a701764eSMichal Meloun 	    (!OF_hasprop(node, "cpu-supply") &&
297*a701764eSMichal Meloun 	    !OF_hasprop(node, "cpu0-supply")))
298319336a9SJared McNeill 		return;
299319336a9SJared McNeill 
30041a76289SEmmanuel Vadot 	if (!OF_hasprop(node, "operating-points") &&
30141a76289SEmmanuel Vadot 	    !OF_hasprop(node, "operating-points-v2"))
30241a76289SEmmanuel Vadot 		return;
30341a76289SEmmanuel Vadot 
304319336a9SJared McNeill 	if (device_find_child(parent, "cpufreq_dt", -1) != NULL)
305319336a9SJared McNeill 		return;
306319336a9SJared McNeill 
307319336a9SJared McNeill 	if (BUS_ADD_CHILD(parent, 0, "cpufreq_dt", -1) == NULL)
308319336a9SJared McNeill 		device_printf(parent, "add cpufreq_dt child failed\n");
309319336a9SJared McNeill }
310319336a9SJared McNeill 
311319336a9SJared McNeill static int
312319336a9SJared McNeill cpufreq_dt_probe(device_t dev)
313319336a9SJared McNeill {
314319336a9SJared McNeill 	phandle_t node;
315319336a9SJared McNeill 
316319336a9SJared McNeill 	node = ofw_bus_get_node(device_get_parent(dev));
317319336a9SJared McNeill 
31841a76289SEmmanuel Vadot 	if (!OF_hasprop(node, "clocks") ||
319*a701764eSMichal Meloun 	    (!OF_hasprop(node, "cpu-supply") &&
320*a701764eSMichal Meloun 	    !OF_hasprop(node, "cpu0-supply")))
321*a701764eSMichal Meloun 
322319336a9SJared McNeill 		return (ENXIO);
323319336a9SJared McNeill 
32441a76289SEmmanuel Vadot 	if (!OF_hasprop(node, "operating-points") &&
32541a76289SEmmanuel Vadot 	  !OF_hasprop(node, "operating-points-v2"))
32641a76289SEmmanuel Vadot 		return (ENXIO);
32741a76289SEmmanuel Vadot 
328319336a9SJared McNeill 	device_set_desc(dev, "Generic cpufreq driver");
329319336a9SJared McNeill 	return (BUS_PROBE_GENERIC);
330319336a9SJared McNeill }
331319336a9SJared McNeill 
332319336a9SJared McNeill static int
33341a76289SEmmanuel Vadot cpufreq_dt_oppv1_parse(struct cpufreq_dt_softc *sc, phandle_t node)
33441a76289SEmmanuel Vadot {
33541a76289SEmmanuel Vadot 	uint32_t *opp, lat;
33641a76289SEmmanuel Vadot 	ssize_t n;
33741a76289SEmmanuel Vadot 
33841a76289SEmmanuel Vadot 	sc->nopp = OF_getencprop_alloc_multi(node, "operating-points",
33941a76289SEmmanuel Vadot 	    sizeof(uint32_t) * 2, (void **)&opp);
34041a76289SEmmanuel Vadot 	if (sc->nopp == -1)
34141a76289SEmmanuel Vadot 		return (ENXIO);
34241a76289SEmmanuel Vadot 
34341a76289SEmmanuel Vadot 	if (OF_getencprop(node, "clock-latency", &lat, sizeof(lat)) == -1)
34441a76289SEmmanuel Vadot 		lat = CPUFREQ_VAL_UNKNOWN;
34541a76289SEmmanuel Vadot 
34641a76289SEmmanuel Vadot 	sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
34741a76289SEmmanuel Vadot 
34841a76289SEmmanuel Vadot 	for (n = 0; n < sc->nopp; n++) {
34941a76289SEmmanuel Vadot 		sc->opp[n].freq = opp[n * 2 + 0] * 1000;
35041a76289SEmmanuel Vadot 		sc->opp[n].uvolt_min = opp[n * 2 + 1];
35141a76289SEmmanuel Vadot 		sc->opp[n].uvolt_max = sc->opp[n].uvolt_min;
35241a76289SEmmanuel Vadot 		sc->opp[n].uvolt_target = sc->opp[n].uvolt_min;
35341a76289SEmmanuel Vadot 		sc->opp[n].clk_latency = lat;
35441a76289SEmmanuel Vadot 
35541a76289SEmmanuel Vadot 		if (bootverbose)
35641a76289SEmmanuel Vadot 			device_printf(sc->dev, "%ju.%03ju MHz, %u uV\n",
35741a76289SEmmanuel Vadot 			    sc->opp[n].freq / 1000000,
35841a76289SEmmanuel Vadot 			    sc->opp[n].freq % 1000000,
35941a76289SEmmanuel Vadot 			    sc->opp[n].uvolt_target);
36041a76289SEmmanuel Vadot 	}
36141a76289SEmmanuel Vadot 	free(opp, M_OFWPROP);
36241a76289SEmmanuel Vadot 
36341a76289SEmmanuel Vadot 	return (0);
36441a76289SEmmanuel Vadot }
36541a76289SEmmanuel Vadot 
36641a76289SEmmanuel Vadot static int
36741a76289SEmmanuel Vadot cpufreq_dt_oppv2_parse(struct cpufreq_dt_softc *sc, phandle_t node)
36841a76289SEmmanuel Vadot {
36941a76289SEmmanuel Vadot 	phandle_t opp, opp_table, opp_xref;
37041a76289SEmmanuel Vadot 	pcell_t cell[2];
37141a76289SEmmanuel Vadot 	uint32_t *volts, lat;
37241a76289SEmmanuel Vadot 	int nvolt, i;
37341a76289SEmmanuel Vadot 
37441a76289SEmmanuel Vadot 	if (OF_getencprop(node, "operating-points-v2", &opp_xref,
37541a76289SEmmanuel Vadot 	    sizeof(opp_xref)) == -1) {
37641a76289SEmmanuel Vadot 		device_printf(sc->dev, "Cannot get xref to oppv2 table\n");
37741a76289SEmmanuel Vadot 		return (ENXIO);
37841a76289SEmmanuel Vadot 	}
37941a76289SEmmanuel Vadot 
38041a76289SEmmanuel Vadot 	opp_table = OF_node_from_xref(opp_xref);
38141a76289SEmmanuel Vadot 	if (opp_table == opp_xref)
38241a76289SEmmanuel Vadot 		return (ENXIO);
38341a76289SEmmanuel Vadot 
38441a76289SEmmanuel Vadot 	if (!OF_hasprop(opp_table, "opp-shared")) {
38541a76289SEmmanuel Vadot 		device_printf(sc->dev, "Only opp-shared is supported\n");
38641a76289SEmmanuel Vadot 		return (ENXIO);
38741a76289SEmmanuel Vadot 	}
38841a76289SEmmanuel Vadot 
38941a76289SEmmanuel Vadot 	for (opp = OF_child(opp_table); opp > 0; opp = OF_peer(opp))
39041a76289SEmmanuel Vadot 		sc->nopp += 1;
39141a76289SEmmanuel Vadot 
39241a76289SEmmanuel Vadot 	sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
39341a76289SEmmanuel Vadot 
39441a76289SEmmanuel Vadot 	for (i = 0, opp_table = OF_child(opp_table); opp_table > 0;
39541a76289SEmmanuel Vadot 	     opp_table = OF_peer(opp_table), i++) {
39641a76289SEmmanuel Vadot 		/* opp-hz is a required property */
39741a76289SEmmanuel Vadot 		if (OF_getencprop(opp_table, "opp-hz", cell,
39841a76289SEmmanuel Vadot 		    sizeof(cell)) == -1)
39941a76289SEmmanuel Vadot 			continue;
40041a76289SEmmanuel Vadot 
40141a76289SEmmanuel Vadot 		sc->opp[i].freq = cell[0];
40241a76289SEmmanuel Vadot 		sc->opp[i].freq <<= 32;
40341a76289SEmmanuel Vadot 		sc->opp[i].freq |= cell[1];
40441a76289SEmmanuel Vadot 
40541a76289SEmmanuel Vadot 		if (OF_getencprop(opp_table, "clock-latency", &lat,
40641a76289SEmmanuel Vadot 		    sizeof(lat)) == -1)
40741a76289SEmmanuel Vadot 			sc->opp[i].clk_latency = CPUFREQ_VAL_UNKNOWN;
40841a76289SEmmanuel Vadot 		else
40941a76289SEmmanuel Vadot 			sc->opp[i].clk_latency = (int)lat;
41041a76289SEmmanuel Vadot 
41141a76289SEmmanuel Vadot 		if (OF_hasprop(opp_table, "turbo-mode"))
41241a76289SEmmanuel Vadot 			sc->opp[i].turbo_mode = true;
41341a76289SEmmanuel Vadot 		if (OF_hasprop(opp_table, "opp-suspend"))
41441a76289SEmmanuel Vadot 			sc->opp[i].opp_suspend = true;
41541a76289SEmmanuel Vadot 
41641a76289SEmmanuel Vadot 		nvolt = OF_getencprop_alloc_multi(opp_table, "opp-microvolt",
41741a76289SEmmanuel Vadot 		  sizeof(*volts), (void **)&volts);
41841a76289SEmmanuel Vadot 		if (nvolt == 1) {
41941a76289SEmmanuel Vadot 			sc->opp[i].uvolt_target = volts[0];
42041a76289SEmmanuel Vadot 			sc->opp[i].uvolt_min = volts[0];
42141a76289SEmmanuel Vadot 			sc->opp[i].uvolt_max = volts[0];
42241a76289SEmmanuel Vadot 		} else if (nvolt == 3) {
42341a76289SEmmanuel Vadot 			sc->opp[i].uvolt_target = volts[0];
42441a76289SEmmanuel Vadot 			sc->opp[i].uvolt_min = volts[1];
42541a76289SEmmanuel Vadot 			sc->opp[i].uvolt_max = volts[2];
42641a76289SEmmanuel Vadot 		} else {
42741a76289SEmmanuel Vadot 			device_printf(sc->dev,
42841a76289SEmmanuel Vadot 			    "Wrong count of opp-microvolt property\n");
42941a76289SEmmanuel Vadot 			OF_prop_free(volts);
43041a76289SEmmanuel Vadot 			free(sc->opp, M_DEVBUF);
43141a76289SEmmanuel Vadot 			return (ENXIO);
43241a76289SEmmanuel Vadot 		}
43341a76289SEmmanuel Vadot 		OF_prop_free(volts);
43441a76289SEmmanuel Vadot 
43541a76289SEmmanuel Vadot 		if (bootverbose)
43641a76289SEmmanuel Vadot 			device_printf(sc->dev, "%ju.%03ju Mhz (%u uV)\n",
43741a76289SEmmanuel Vadot 			    sc->opp[i].freq / 1000000,
43841a76289SEmmanuel Vadot 			    sc->opp[i].freq % 1000000,
43941a76289SEmmanuel Vadot 			    sc->opp[i].uvolt_target);
44041a76289SEmmanuel Vadot 	}
44141a76289SEmmanuel Vadot 	return (0);
44241a76289SEmmanuel Vadot }
44341a76289SEmmanuel Vadot 
44441a76289SEmmanuel Vadot static int
445319336a9SJared McNeill cpufreq_dt_attach(device_t dev)
446319336a9SJared McNeill {
447319336a9SJared McNeill 	struct cpufreq_dt_softc *sc;
44841a76289SEmmanuel Vadot 	phandle_t node;
44941a76289SEmmanuel Vadot 	phandle_t cnode, opp, copp;
450319336a9SJared McNeill 	int cpu;
45141a76289SEmmanuel Vadot 	uint64_t freq;
45241a76289SEmmanuel Vadot 	int rv = 0;
45341a76289SEmmanuel Vadot 	enum opp_version version;
454319336a9SJared McNeill 
455319336a9SJared McNeill 	sc = device_get_softc(dev);
45641a76289SEmmanuel Vadot 	sc->dev = dev;
457319336a9SJared McNeill 	node = ofw_bus_get_node(device_get_parent(dev));
458319336a9SJared McNeill 
459319336a9SJared McNeill 	if (regulator_get_by_ofw_property(dev, node,
460319336a9SJared McNeill 	    "cpu-supply", &sc->reg) != 0) {
461*a701764eSMichal Meloun 		if (regulator_get_by_ofw_property(dev, node,
462*a701764eSMichal Meloun 		    "cpu0-supply", &sc->reg) != 0) {
463319336a9SJared McNeill 			device_printf(dev, "no regulator for %s\n",
464319336a9SJared McNeill 			    ofw_bus_get_name(device_get_parent(dev)));
465319336a9SJared McNeill 			return (ENXIO);
466319336a9SJared McNeill 		}
467*a701764eSMichal Meloun 	}
468319336a9SJared McNeill 
469319336a9SJared McNeill 	if (clk_get_by_ofw_index(dev, node, 0, &sc->clk) != 0) {
470319336a9SJared McNeill 		device_printf(dev, "no clock for %s\n",
471319336a9SJared McNeill 		    ofw_bus_get_name(device_get_parent(dev)));
472319336a9SJared McNeill 		regulator_release(sc->reg);
473319336a9SJared McNeill 		return (ENXIO);
474319336a9SJared McNeill 	}
475319336a9SJared McNeill 
47641a76289SEmmanuel Vadot 	if (OF_hasprop(node, "operating-points")) {
47741a76289SEmmanuel Vadot 		version = OPP_V1;
47841a76289SEmmanuel Vadot 		rv = cpufreq_dt_oppv1_parse(sc, node);
47941a76289SEmmanuel Vadot 		if (rv != 0) {
48041a76289SEmmanuel Vadot 			device_printf(dev, "Failed to parse opp-v1 table\n");
48141a76289SEmmanuel Vadot 			return (rv);
482319336a9SJared McNeill 		}
48341a76289SEmmanuel Vadot 		OF_getencprop(node, "operating-points", &opp,
48441a76289SEmmanuel Vadot 		    sizeof(opp));
48541a76289SEmmanuel Vadot 	} else {
48641a76289SEmmanuel Vadot 		version = OPP_V2;
48741a76289SEmmanuel Vadot 		rv = cpufreq_dt_oppv2_parse(sc, node);
48841a76289SEmmanuel Vadot 		if (rv != 0) {
48941a76289SEmmanuel Vadot 			device_printf(dev, "Failed to parse opp-v2 table\n");
49041a76289SEmmanuel Vadot 			return (rv);
49141a76289SEmmanuel Vadot 		}
49241a76289SEmmanuel Vadot 		OF_getencprop(node, "operating-points-v2", &opp,
49341a76289SEmmanuel Vadot 		    sizeof(opp));
49441a76289SEmmanuel Vadot 	}
495319336a9SJared McNeill 
496319336a9SJared McNeill 	/*
49741a76289SEmmanuel Vadot 	 * Find all CPUs that share the same opp table
498319336a9SJared McNeill 	 */
499319336a9SJared McNeill 	CPU_ZERO(&sc->cpus);
500319336a9SJared McNeill 	cpu = device_get_unit(device_get_parent(dev));
501319336a9SJared McNeill 	for (cnode = node; cnode > 0; cnode = OF_peer(cnode), cpu++) {
50241a76289SEmmanuel Vadot 		copp = -1;
50341a76289SEmmanuel Vadot 		if (version == OPP_V1)
50441a76289SEmmanuel Vadot 			OF_getencprop(cnode, "operating-points", &copp,
50541a76289SEmmanuel Vadot 			    sizeof(copp));
50641a76289SEmmanuel Vadot 		else if (version == OPP_V2)
50741a76289SEmmanuel Vadot 			OF_getencprop(cnode, "operating-points-v2",
50841a76289SEmmanuel Vadot 			    &copp, sizeof(copp));
50941a76289SEmmanuel Vadot 		if (opp == copp)
510319336a9SJared McNeill 			CPU_SET(cpu, &sc->cpus);
511319336a9SJared McNeill 	}
512319336a9SJared McNeill 
513319336a9SJared McNeill 	if (clk_get_freq(sc->clk, &freq) == 0)
514319336a9SJared McNeill 		cpufreq_dt_notify(dev, freq);
515319336a9SJared McNeill 
516319336a9SJared McNeill 	cpufreq_register(dev);
517319336a9SJared McNeill 
518319336a9SJared McNeill 	return (0);
519319336a9SJared McNeill }
520319336a9SJared McNeill 
521319336a9SJared McNeill 
522319336a9SJared McNeill static device_method_t cpufreq_dt_methods[] = {
523319336a9SJared McNeill 	/* Device interface */
524319336a9SJared McNeill 	DEVMETHOD(device_identify,	cpufreq_dt_identify),
525319336a9SJared McNeill 	DEVMETHOD(device_probe,		cpufreq_dt_probe),
526319336a9SJared McNeill 	DEVMETHOD(device_attach,	cpufreq_dt_attach),
527319336a9SJared McNeill 
528319336a9SJared McNeill 	/* cpufreq interface */
529319336a9SJared McNeill 	DEVMETHOD(cpufreq_drv_get,	cpufreq_dt_get),
530319336a9SJared McNeill 	DEVMETHOD(cpufreq_drv_set,	cpufreq_dt_set),
531319336a9SJared McNeill 	DEVMETHOD(cpufreq_drv_type,	cpufreq_dt_type),
532319336a9SJared McNeill 	DEVMETHOD(cpufreq_drv_settings,	cpufreq_dt_settings),
533319336a9SJared McNeill 
534319336a9SJared McNeill 	DEVMETHOD_END
535319336a9SJared McNeill };
536319336a9SJared McNeill 
537319336a9SJared McNeill static driver_t cpufreq_dt_driver = {
538319336a9SJared McNeill 	"cpufreq_dt",
539319336a9SJared McNeill 	cpufreq_dt_methods,
540319336a9SJared McNeill 	sizeof(struct cpufreq_dt_softc),
541319336a9SJared McNeill };
542319336a9SJared McNeill 
543319336a9SJared McNeill static devclass_t cpufreq_dt_devclass;
544319336a9SJared McNeill 
545319336a9SJared McNeill DRIVER_MODULE(cpufreq_dt, cpu, cpufreq_dt_driver, cpufreq_dt_devclass, 0, 0);
546319336a9SJared McNeill MODULE_VERSION(cpufreq_dt, 1);
547