1319336a9SJared McNeill /*-
241a76289SEmmanuel Vadot * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.Org>
3319336a9SJared McNeill * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
4319336a9SJared McNeill *
5319336a9SJared McNeill * Redistribution and use in source and binary forms, with or without
6319336a9SJared McNeill * modification, are permitted provided that the following conditions
7319336a9SJared McNeill * are met:
8319336a9SJared McNeill * 1. Redistributions of source code must retain the above copyright
9319336a9SJared McNeill * notice, this list of conditions and the following disclaimer.
10319336a9SJared McNeill * 2. Redistributions in binary form must reproduce the above copyright
11319336a9SJared McNeill * notice, this list of conditions and the following disclaimer in the
12319336a9SJared McNeill * documentation and/or other materials provided with the distribution.
13319336a9SJared McNeill *
14319336a9SJared McNeill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15319336a9SJared McNeill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16319336a9SJared McNeill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17319336a9SJared McNeill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18319336a9SJared McNeill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19319336a9SJared McNeill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20319336a9SJared McNeill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21319336a9SJared McNeill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22319336a9SJared McNeill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23319336a9SJared McNeill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24319336a9SJared McNeill * SUCH DAMAGE.
25319336a9SJared McNeill */
26319336a9SJared McNeill
27319336a9SJared McNeill /*
28319336a9SJared McNeill * Generic DT based cpufreq driver
29319336a9SJared McNeill */
30319336a9SJared McNeill
31319336a9SJared McNeill #include <sys/param.h>
32319336a9SJared McNeill #include <sys/systm.h>
33319336a9SJared McNeill #include <sys/bus.h>
34319336a9SJared McNeill #include <sys/rman.h>
35319336a9SJared McNeill #include <sys/kernel.h>
36319336a9SJared McNeill #include <sys/module.h>
37319336a9SJared McNeill #include <sys/cpu.h>
38319336a9SJared McNeill #include <sys/cpuset.h>
39319336a9SJared McNeill #include <sys/smp.h>
40319336a9SJared McNeill
41319336a9SJared McNeill #include <dev/ofw/ofw_bus.h>
42319336a9SJared McNeill #include <dev/ofw/ofw_bus_subr.h>
43319336a9SJared McNeill
44be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
45*b2f0caf1SEmmanuel Vadot #include <dev/regulator/regulator.h>
46319336a9SJared McNeill
47319336a9SJared McNeill #include "cpufreq_if.h"
48319336a9SJared McNeill
4941a76289SEmmanuel Vadot #if 0
504707401cSEmmanuel Vadot #define DPRINTF(dev, msg...) device_printf(dev, "cpufreq_dt: " msg);
5141a76289SEmmanuel Vadot #else
524707401cSEmmanuel Vadot #define DPRINTF(dev, msg...)
5341a76289SEmmanuel Vadot #endif
5441a76289SEmmanuel Vadot
5541a76289SEmmanuel Vadot enum opp_version {
5641a76289SEmmanuel Vadot OPP_V1 = 1,
5741a76289SEmmanuel Vadot OPP_V2,
5841a76289SEmmanuel Vadot };
5941a76289SEmmanuel Vadot
60319336a9SJared McNeill struct cpufreq_dt_opp {
6141a76289SEmmanuel Vadot uint64_t freq;
6241a76289SEmmanuel Vadot uint32_t uvolt_target;
6341a76289SEmmanuel Vadot uint32_t uvolt_min;
6441a76289SEmmanuel Vadot uint32_t uvolt_max;
6541a76289SEmmanuel Vadot uint32_t uamps;
6641a76289SEmmanuel Vadot uint32_t clk_latency;
6741a76289SEmmanuel Vadot bool turbo_mode;
6841a76289SEmmanuel Vadot bool opp_suspend;
69319336a9SJared McNeill };
70319336a9SJared McNeill
7105860ffdSAdrian Chadd #define CPUFREQ_DT_HAVE_REGULATOR(sc) ((sc)->reg != NULL)
7205860ffdSAdrian Chadd
73319336a9SJared McNeill struct cpufreq_dt_softc {
7441a76289SEmmanuel Vadot device_t dev;
75319336a9SJared McNeill clk_t clk;
76319336a9SJared McNeill regulator_t reg;
77319336a9SJared McNeill
78319336a9SJared McNeill struct cpufreq_dt_opp *opp;
79319336a9SJared McNeill ssize_t nopp;
80319336a9SJared McNeill
81a969e975SEmmanuel Vadot int cpu;
82319336a9SJared McNeill cpuset_t cpus;
83319336a9SJared McNeill };
84319336a9SJared McNeill
85319336a9SJared McNeill static void
cpufreq_dt_notify(device_t dev,uint64_t freq)86319336a9SJared McNeill cpufreq_dt_notify(device_t dev, uint64_t freq)
87319336a9SJared McNeill {
88319336a9SJared McNeill struct cpufreq_dt_softc *sc;
89319336a9SJared McNeill struct pcpu *pc;
90319336a9SJared McNeill int cpu;
91319336a9SJared McNeill
92319336a9SJared McNeill sc = device_get_softc(dev);
93319336a9SJared McNeill
94319336a9SJared McNeill CPU_FOREACH(cpu) {
95319336a9SJared McNeill if (CPU_ISSET(cpu, &sc->cpus)) {
96319336a9SJared McNeill pc = pcpu_find(cpu);
97319336a9SJared McNeill pc->pc_clock = freq;
98319336a9SJared McNeill }
99319336a9SJared McNeill }
100319336a9SJared McNeill }
101319336a9SJared McNeill
102319336a9SJared McNeill static const struct cpufreq_dt_opp *
cpufreq_dt_find_opp(device_t dev,uint64_t freq)10341a76289SEmmanuel Vadot cpufreq_dt_find_opp(device_t dev, uint64_t freq)
104319336a9SJared McNeill {
105319336a9SJared McNeill struct cpufreq_dt_softc *sc;
10617c17872SEmmanuel Vadot uint64_t diff, best_diff;
10717c17872SEmmanuel Vadot ssize_t n, best_n;
108319336a9SJared McNeill
109319336a9SJared McNeill sc = device_get_softc(dev);
110319336a9SJared McNeill
11117c17872SEmmanuel Vadot diff = 0;
11217c17872SEmmanuel Vadot best_diff = ~0;
1134707401cSEmmanuel Vadot DPRINTF(dev, "Looking for freq %ju\n", freq);
11417c17872SEmmanuel Vadot for (n = 0; n < sc->nopp; n++) {
11517c17872SEmmanuel Vadot diff = abs64((int64_t)sc->opp[n].freq - (int64_t)freq);
11617c17872SEmmanuel Vadot DPRINTF(dev, "Testing %ju, diff is %ju\n", sc->opp[n].freq, diff);
11717c17872SEmmanuel Vadot if (diff < best_diff) {
11817c17872SEmmanuel Vadot best_diff = diff;
11917c17872SEmmanuel Vadot best_n = n;
12017c17872SEmmanuel Vadot DPRINTF(dev, "%ju is best for now\n", sc->opp[n].freq);
12117c17872SEmmanuel Vadot }
12217c17872SEmmanuel Vadot }
123319336a9SJared McNeill
12417c17872SEmmanuel Vadot DPRINTF(dev, "Will use %ju\n", sc->opp[best_n].freq);
12517c17872SEmmanuel Vadot return (&sc->opp[best_n]);
126319336a9SJared McNeill }
127319336a9SJared McNeill
128319336a9SJared McNeill static void
cpufreq_dt_opp_to_setting(device_t dev,const struct cpufreq_dt_opp * opp,struct cf_setting * set)129319336a9SJared McNeill cpufreq_dt_opp_to_setting(device_t dev, const struct cpufreq_dt_opp *opp,
130319336a9SJared McNeill struct cf_setting *set)
131319336a9SJared McNeill {
132319336a9SJared McNeill
133319336a9SJared McNeill memset(set, 0, sizeof(*set));
13441a76289SEmmanuel Vadot set->freq = opp->freq / 1000000;
13541a76289SEmmanuel Vadot set->volts = opp->uvolt_target / 1000;
136319336a9SJared McNeill set->power = CPUFREQ_VAL_UNKNOWN;
13741a76289SEmmanuel Vadot set->lat = opp->clk_latency;
138319336a9SJared McNeill set->dev = dev;
139319336a9SJared McNeill }
140319336a9SJared McNeill
141319336a9SJared McNeill static int
cpufreq_dt_get(device_t dev,struct cf_setting * set)142319336a9SJared McNeill cpufreq_dt_get(device_t dev, struct cf_setting *set)
143319336a9SJared McNeill {
144319336a9SJared McNeill struct cpufreq_dt_softc *sc;
145319336a9SJared McNeill const struct cpufreq_dt_opp *opp;
146319336a9SJared McNeill uint64_t freq;
147319336a9SJared McNeill
148319336a9SJared McNeill sc = device_get_softc(dev);
149319336a9SJared McNeill
1504707401cSEmmanuel Vadot DPRINTF(dev, "cpufreq_dt_get\n");
151319336a9SJared McNeill if (clk_get_freq(sc->clk, &freq) != 0)
152319336a9SJared McNeill return (ENXIO);
153319336a9SJared McNeill
15441a76289SEmmanuel Vadot opp = cpufreq_dt_find_opp(dev, freq);
15541a76289SEmmanuel Vadot if (opp == NULL) {
15641a76289SEmmanuel Vadot device_printf(dev, "Can't find the current freq in opp\n");
157319336a9SJared McNeill return (ENOENT);
15841a76289SEmmanuel Vadot }
159319336a9SJared McNeill
160319336a9SJared McNeill cpufreq_dt_opp_to_setting(dev, opp, set);
161319336a9SJared McNeill
1624707401cSEmmanuel Vadot DPRINTF(dev, "Current freq %dMhz\n", set->freq);
163319336a9SJared McNeill return (0);
164319336a9SJared McNeill }
165319336a9SJared McNeill
166319336a9SJared McNeill static int
cpufreq_dt_set(device_t dev,const struct cf_setting * set)167319336a9SJared McNeill cpufreq_dt_set(device_t dev, const struct cf_setting *set)
168319336a9SJared McNeill {
169319336a9SJared McNeill struct cpufreq_dt_softc *sc;
170319336a9SJared McNeill const struct cpufreq_dt_opp *opp, *copp;
171319336a9SJared McNeill uint64_t freq;
172a701764eSMichal Meloun int uvolt, error;
173319336a9SJared McNeill
174319336a9SJared McNeill sc = device_get_softc(dev);
175319336a9SJared McNeill
1764707401cSEmmanuel Vadot DPRINTF(dev, "Working on cpu %d\n", sc->cpu);
1774707401cSEmmanuel Vadot DPRINTF(dev, "We have %d cpu on this dev\n", CPU_COUNT(&sc->cpus));
178a969e975SEmmanuel Vadot if (!CPU_ISSET(sc->cpu, &sc->cpus)) {
1794707401cSEmmanuel Vadot DPRINTF(dev, "Not for this CPU\n");
180a969e975SEmmanuel Vadot return (0);
181a969e975SEmmanuel Vadot }
182a969e975SEmmanuel Vadot
18341a76289SEmmanuel Vadot if (clk_get_freq(sc->clk, &freq) != 0) {
18441a76289SEmmanuel Vadot device_printf(dev, "Can't get current clk freq\n");
185319336a9SJared McNeill return (ENXIO);
18641a76289SEmmanuel Vadot }
18705860ffdSAdrian Chadd
18805860ffdSAdrian Chadd /*
18905860ffdSAdrian Chadd * Only do the regulator work if it's required.
19005860ffdSAdrian Chadd */
19105860ffdSAdrian Chadd if (CPUFREQ_DT_HAVE_REGULATOR(sc)) {
192a701764eSMichal Meloun /* Try to get current valtage by using regulator first. */
193a701764eSMichal Meloun error = regulator_get_voltage(sc->reg, &uvolt);
194a701764eSMichal Meloun if (error != 0) {
195a701764eSMichal Meloun /*
196a701764eSMichal Meloun * Try oppoints table as backup way. However,
197a701764eSMichal Meloun * this is insufficient because the actual processor
198a701764eSMichal Meloun * frequency may not be in the table. PLL frequency
199a701764eSMichal Meloun * granularity can be different that granularity of
200a701764eSMichal Meloun * oppoint table.
201a701764eSMichal Meloun */
20241a76289SEmmanuel Vadot copp = cpufreq_dt_find_opp(sc->dev, freq);
20341a76289SEmmanuel Vadot if (copp == NULL) {
204a701764eSMichal Meloun device_printf(dev,
205a701764eSMichal Meloun "Can't find the current freq in opp\n");
206319336a9SJared McNeill return (ENOENT);
20741a76289SEmmanuel Vadot }
208a701764eSMichal Meloun uvolt = copp->uvolt_target;
209a701764eSMichal Meloun }
21005860ffdSAdrian Chadd } else
21105860ffdSAdrian Chadd uvolt = 0;
212a701764eSMichal Meloun
21341a76289SEmmanuel Vadot opp = cpufreq_dt_find_opp(sc->dev, set->freq * 1000000);
21441a76289SEmmanuel Vadot if (opp == NULL) {
21541a76289SEmmanuel Vadot device_printf(dev, "Couldn't find an opp for this freq\n");
216319336a9SJared McNeill return (EINVAL);
217319336a9SJared McNeill }
2184707401cSEmmanuel Vadot DPRINTF(sc->dev, "Current freq %ju, uvolt: %d\n", freq, uvolt);
2194707401cSEmmanuel Vadot DPRINTF(sc->dev, "Target freq %ju, , uvolt: %d\n",
220a701764eSMichal Meloun opp->freq, opp->uvolt_target);
221319336a9SJared McNeill
22205860ffdSAdrian Chadd if (CPUFREQ_DT_HAVE_REGULATOR(sc) && (uvolt < opp->uvolt_target)) {
2234707401cSEmmanuel Vadot DPRINTF(dev, "Changing regulator from %u to %u\n",
224a701764eSMichal Meloun uvolt, opp->uvolt_target);
22541a76289SEmmanuel Vadot error = regulator_set_voltage(sc->reg,
22641a76289SEmmanuel Vadot opp->uvolt_min,
22741a76289SEmmanuel Vadot opp->uvolt_max);
228319336a9SJared McNeill if (error != 0) {
2294707401cSEmmanuel Vadot DPRINTF(dev, "Failed, backout\n");
23041a76289SEmmanuel Vadot return (ENXIO);
23141a76289SEmmanuel Vadot }
23241a76289SEmmanuel Vadot }
23341a76289SEmmanuel Vadot
2344707401cSEmmanuel Vadot DPRINTF(dev, "Setting clk to %ju\n", opp->freq);
235a701764eSMichal Meloun error = clk_set_freq(sc->clk, opp->freq, CLK_SET_ROUND_DOWN);
23641a76289SEmmanuel Vadot if (error != 0) {
2374707401cSEmmanuel Vadot DPRINTF(dev, "Failed, backout\n");
238319336a9SJared McNeill /* Restore previous voltage (best effort) */
23905860ffdSAdrian Chadd if (CPUFREQ_DT_HAVE_REGULATOR(sc))
24041a76289SEmmanuel Vadot error = regulator_set_voltage(sc->reg,
24141a76289SEmmanuel Vadot copp->uvolt_min,
24241a76289SEmmanuel Vadot copp->uvolt_max);
243319336a9SJared McNeill return (ENXIO);
244319336a9SJared McNeill }
245319336a9SJared McNeill
24605860ffdSAdrian Chadd if (CPUFREQ_DT_HAVE_REGULATOR(sc) && (uvolt > opp->uvolt_target)) {
2474707401cSEmmanuel Vadot DPRINTF(dev, "Changing regulator from %u to %u\n",
248a701764eSMichal Meloun uvolt, opp->uvolt_target);
24941a76289SEmmanuel Vadot error = regulator_set_voltage(sc->reg,
25041a76289SEmmanuel Vadot opp->uvolt_min,
25141a76289SEmmanuel Vadot opp->uvolt_max);
252319336a9SJared McNeill if (error != 0) {
2534707401cSEmmanuel Vadot DPRINTF(dev, "Failed to switch regulator to %d\n",
25441a76289SEmmanuel Vadot opp->uvolt_target);
255319336a9SJared McNeill /* Restore previous CPU frequency (best effort) */
256a701764eSMichal Meloun (void)clk_set_freq(sc->clk, copp->freq, 0);
257319336a9SJared McNeill return (ENXIO);
258319336a9SJared McNeill }
259319336a9SJared McNeill }
260319336a9SJared McNeill
261319336a9SJared McNeill if (clk_get_freq(sc->clk, &freq) == 0)
262319336a9SJared McNeill cpufreq_dt_notify(dev, freq);
263319336a9SJared McNeill
264319336a9SJared McNeill return (0);
265319336a9SJared McNeill }
266319336a9SJared McNeill
267319336a9SJared McNeill static int
cpufreq_dt_type(device_t dev,int * type)268319336a9SJared McNeill cpufreq_dt_type(device_t dev, int *type)
269319336a9SJared McNeill {
270319336a9SJared McNeill if (type == NULL)
271319336a9SJared McNeill return (EINVAL);
272319336a9SJared McNeill
273319336a9SJared McNeill *type = CPUFREQ_TYPE_ABSOLUTE;
274319336a9SJared McNeill return (0);
275319336a9SJared McNeill }
276319336a9SJared McNeill
277319336a9SJared McNeill static int
cpufreq_dt_settings(device_t dev,struct cf_setting * sets,int * count)278319336a9SJared McNeill cpufreq_dt_settings(device_t dev, struct cf_setting *sets, int *count)
279319336a9SJared McNeill {
280319336a9SJared McNeill struct cpufreq_dt_softc *sc;
281319336a9SJared McNeill ssize_t n;
282319336a9SJared McNeill
2834707401cSEmmanuel Vadot DPRINTF(dev, "cpufreq_dt_settings\n");
284319336a9SJared McNeill if (sets == NULL || count == NULL)
285319336a9SJared McNeill return (EINVAL);
286319336a9SJared McNeill
287319336a9SJared McNeill sc = device_get_softc(dev);
288319336a9SJared McNeill
289319336a9SJared McNeill if (*count < sc->nopp) {
290319336a9SJared McNeill *count = (int)sc->nopp;
291319336a9SJared McNeill return (E2BIG);
292319336a9SJared McNeill }
293319336a9SJared McNeill
294319336a9SJared McNeill for (n = 0; n < sc->nopp; n++)
295319336a9SJared McNeill cpufreq_dt_opp_to_setting(dev, &sc->opp[n], &sets[n]);
296319336a9SJared McNeill
297319336a9SJared McNeill *count = (int)sc->nopp;
298319336a9SJared McNeill
299319336a9SJared McNeill return (0);
300319336a9SJared McNeill }
301319336a9SJared McNeill
302319336a9SJared McNeill static void
cpufreq_dt_identify(driver_t * driver,device_t parent)303319336a9SJared McNeill cpufreq_dt_identify(driver_t *driver, device_t parent)
304319336a9SJared McNeill {
305319336a9SJared McNeill phandle_t node;
306319336a9SJared McNeill
307319336a9SJared McNeill /* Properties must be listed under node /cpus/cpu@0 */
308319336a9SJared McNeill node = ofw_bus_get_node(parent);
309319336a9SJared McNeill
310319336a9SJared McNeill /* The cpu@0 node must have the following properties */
31105860ffdSAdrian Chadd if (!OF_hasprop(node, "clocks"))
312319336a9SJared McNeill return;
313319336a9SJared McNeill
31441a76289SEmmanuel Vadot if (!OF_hasprop(node, "operating-points") &&
31541a76289SEmmanuel Vadot !OF_hasprop(node, "operating-points-v2"))
31641a76289SEmmanuel Vadot return;
31741a76289SEmmanuel Vadot
318319336a9SJared McNeill if (device_find_child(parent, "cpufreq_dt", -1) != NULL)
319319336a9SJared McNeill return;
320319336a9SJared McNeill
321d3a8f98aSAlexander Motin if (BUS_ADD_CHILD(parent, 0, "cpufreq_dt", device_get_unit(parent))
322d3a8f98aSAlexander Motin == NULL)
323319336a9SJared McNeill device_printf(parent, "add cpufreq_dt child failed\n");
324319336a9SJared McNeill }
325319336a9SJared McNeill
326319336a9SJared McNeill static int
cpufreq_dt_probe(device_t dev)327319336a9SJared McNeill cpufreq_dt_probe(device_t dev)
328319336a9SJared McNeill {
329319336a9SJared McNeill phandle_t node;
330319336a9SJared McNeill
331319336a9SJared McNeill node = ofw_bus_get_node(device_get_parent(dev));
332319336a9SJared McNeill
33305860ffdSAdrian Chadd /*
33405860ffdSAdrian Chadd * Note - supply isn't required here for probe; we'll check
33505860ffdSAdrian Chadd * it out in more detail during attach.
33605860ffdSAdrian Chadd */
33705860ffdSAdrian Chadd if (!OF_hasprop(node, "clocks"))
338319336a9SJared McNeill return (ENXIO);
339319336a9SJared McNeill
34041a76289SEmmanuel Vadot if (!OF_hasprop(node, "operating-points") &&
34141a76289SEmmanuel Vadot !OF_hasprop(node, "operating-points-v2"))
34241a76289SEmmanuel Vadot return (ENXIO);
34341a76289SEmmanuel Vadot
344319336a9SJared McNeill device_set_desc(dev, "Generic cpufreq driver");
345319336a9SJared McNeill return (BUS_PROBE_GENERIC);
346319336a9SJared McNeill }
347319336a9SJared McNeill
348319336a9SJared McNeill static int
cpufreq_dt_oppv1_parse(struct cpufreq_dt_softc * sc,phandle_t node)34941a76289SEmmanuel Vadot cpufreq_dt_oppv1_parse(struct cpufreq_dt_softc *sc, phandle_t node)
35041a76289SEmmanuel Vadot {
35141a76289SEmmanuel Vadot uint32_t *opp, lat;
35241a76289SEmmanuel Vadot ssize_t n;
35341a76289SEmmanuel Vadot
35441a76289SEmmanuel Vadot sc->nopp = OF_getencprop_alloc_multi(node, "operating-points",
35541a76289SEmmanuel Vadot sizeof(uint32_t) * 2, (void **)&opp);
35641a76289SEmmanuel Vadot if (sc->nopp == -1)
35741a76289SEmmanuel Vadot return (ENXIO);
35841a76289SEmmanuel Vadot
35941a76289SEmmanuel Vadot if (OF_getencprop(node, "clock-latency", &lat, sizeof(lat)) == -1)
36041a76289SEmmanuel Vadot lat = CPUFREQ_VAL_UNKNOWN;
36141a76289SEmmanuel Vadot
36241a76289SEmmanuel Vadot sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
36341a76289SEmmanuel Vadot
36441a76289SEmmanuel Vadot for (n = 0; n < sc->nopp; n++) {
36541a76289SEmmanuel Vadot sc->opp[n].freq = opp[n * 2 + 0] * 1000;
36641a76289SEmmanuel Vadot sc->opp[n].uvolt_min = opp[n * 2 + 1];
36741a76289SEmmanuel Vadot sc->opp[n].uvolt_max = sc->opp[n].uvolt_min;
36841a76289SEmmanuel Vadot sc->opp[n].uvolt_target = sc->opp[n].uvolt_min;
36941a76289SEmmanuel Vadot sc->opp[n].clk_latency = lat;
37041a76289SEmmanuel Vadot
37141a76289SEmmanuel Vadot if (bootverbose)
37241a76289SEmmanuel Vadot device_printf(sc->dev, "%ju.%03ju MHz, %u uV\n",
37341a76289SEmmanuel Vadot sc->opp[n].freq / 1000000,
37441a76289SEmmanuel Vadot sc->opp[n].freq % 1000000,
37541a76289SEmmanuel Vadot sc->opp[n].uvolt_target);
37641a76289SEmmanuel Vadot }
37741a76289SEmmanuel Vadot free(opp, M_OFWPROP);
37841a76289SEmmanuel Vadot
37941a76289SEmmanuel Vadot return (0);
38041a76289SEmmanuel Vadot }
38141a76289SEmmanuel Vadot
38241a76289SEmmanuel Vadot static int
cpufreq_dt_oppv2_parse(struct cpufreq_dt_softc * sc,phandle_t node)38341a76289SEmmanuel Vadot cpufreq_dt_oppv2_parse(struct cpufreq_dt_softc *sc, phandle_t node)
38441a76289SEmmanuel Vadot {
38541a76289SEmmanuel Vadot phandle_t opp, opp_table, opp_xref;
38641a76289SEmmanuel Vadot pcell_t cell[2];
38741a76289SEmmanuel Vadot uint32_t *volts, lat;
38841a76289SEmmanuel Vadot int nvolt, i;
38941a76289SEmmanuel Vadot
39005860ffdSAdrian Chadd /*
39105860ffdSAdrian Chadd * operating-points-v2 does not require the voltage entries
39205860ffdSAdrian Chadd * and a regulator. So, it's OK if they're not there.
39305860ffdSAdrian Chadd */
39441a76289SEmmanuel Vadot if (OF_getencprop(node, "operating-points-v2", &opp_xref,
39541a76289SEmmanuel Vadot sizeof(opp_xref)) == -1) {
39641a76289SEmmanuel Vadot device_printf(sc->dev, "Cannot get xref to oppv2 table\n");
39741a76289SEmmanuel Vadot return (ENXIO);
39841a76289SEmmanuel Vadot }
39941a76289SEmmanuel Vadot
40041a76289SEmmanuel Vadot opp_table = OF_node_from_xref(opp_xref);
40141a76289SEmmanuel Vadot if (opp_table == opp_xref)
40241a76289SEmmanuel Vadot return (ENXIO);
40341a76289SEmmanuel Vadot
40441a76289SEmmanuel Vadot if (!OF_hasprop(opp_table, "opp-shared")) {
40541a76289SEmmanuel Vadot device_printf(sc->dev, "Only opp-shared is supported\n");
40641a76289SEmmanuel Vadot return (ENXIO);
40741a76289SEmmanuel Vadot }
40841a76289SEmmanuel Vadot
40941a76289SEmmanuel Vadot for (opp = OF_child(opp_table); opp > 0; opp = OF_peer(opp))
41041a76289SEmmanuel Vadot sc->nopp += 1;
41141a76289SEmmanuel Vadot
41241a76289SEmmanuel Vadot sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
41341a76289SEmmanuel Vadot
41441a76289SEmmanuel Vadot for (i = 0, opp_table = OF_child(opp_table); opp_table > 0;
41541a76289SEmmanuel Vadot opp_table = OF_peer(opp_table), i++) {
41641a76289SEmmanuel Vadot /* opp-hz is a required property */
41741a76289SEmmanuel Vadot if (OF_getencprop(opp_table, "opp-hz", cell,
41841a76289SEmmanuel Vadot sizeof(cell)) == -1)
41941a76289SEmmanuel Vadot continue;
42041a76289SEmmanuel Vadot
42141a76289SEmmanuel Vadot sc->opp[i].freq = cell[0];
42241a76289SEmmanuel Vadot sc->opp[i].freq <<= 32;
42341a76289SEmmanuel Vadot sc->opp[i].freq |= cell[1];
42441a76289SEmmanuel Vadot
42541a76289SEmmanuel Vadot if (OF_getencprop(opp_table, "clock-latency", &lat,
42641a76289SEmmanuel Vadot sizeof(lat)) == -1)
42741a76289SEmmanuel Vadot sc->opp[i].clk_latency = CPUFREQ_VAL_UNKNOWN;
42841a76289SEmmanuel Vadot else
42941a76289SEmmanuel Vadot sc->opp[i].clk_latency = (int)lat;
43041a76289SEmmanuel Vadot
43141a76289SEmmanuel Vadot if (OF_hasprop(opp_table, "turbo-mode"))
43241a76289SEmmanuel Vadot sc->opp[i].turbo_mode = true;
43341a76289SEmmanuel Vadot if (OF_hasprop(opp_table, "opp-suspend"))
43441a76289SEmmanuel Vadot sc->opp[i].opp_suspend = true;
43541a76289SEmmanuel Vadot
43605860ffdSAdrian Chadd if (CPUFREQ_DT_HAVE_REGULATOR(sc)) {
43705860ffdSAdrian Chadd nvolt = OF_getencprop_alloc_multi(opp_table,
43805860ffdSAdrian Chadd "opp-microvolt", sizeof(*volts), (void **)&volts);
43941a76289SEmmanuel Vadot if (nvolt == 1) {
44041a76289SEmmanuel Vadot sc->opp[i].uvolt_target = volts[0];
44141a76289SEmmanuel Vadot sc->opp[i].uvolt_min = volts[0];
44241a76289SEmmanuel Vadot sc->opp[i].uvolt_max = volts[0];
44341a76289SEmmanuel Vadot } else if (nvolt == 3) {
44441a76289SEmmanuel Vadot sc->opp[i].uvolt_target = volts[0];
44541a76289SEmmanuel Vadot sc->opp[i].uvolt_min = volts[1];
44641a76289SEmmanuel Vadot sc->opp[i].uvolt_max = volts[2];
44741a76289SEmmanuel Vadot } else {
44841a76289SEmmanuel Vadot device_printf(sc->dev,
44941a76289SEmmanuel Vadot "Wrong count of opp-microvolt property\n");
45041a76289SEmmanuel Vadot OF_prop_free(volts);
45141a76289SEmmanuel Vadot free(sc->opp, M_DEVBUF);
45241a76289SEmmanuel Vadot return (ENXIO);
45341a76289SEmmanuel Vadot }
45441a76289SEmmanuel Vadot OF_prop_free(volts);
45505860ffdSAdrian Chadd } else {
45605860ffdSAdrian Chadd /* No regulator required; don't add anything */
45705860ffdSAdrian Chadd sc->opp[i].uvolt_target = 0;
45805860ffdSAdrian Chadd sc->opp[i].uvolt_min = 0;
45905860ffdSAdrian Chadd sc->opp[i].uvolt_max = 0;
46005860ffdSAdrian Chadd }
46141a76289SEmmanuel Vadot
46241a76289SEmmanuel Vadot if (bootverbose)
46341a76289SEmmanuel Vadot device_printf(sc->dev, "%ju.%03ju Mhz (%u uV)\n",
46441a76289SEmmanuel Vadot sc->opp[i].freq / 1000000,
46541a76289SEmmanuel Vadot sc->opp[i].freq % 1000000,
46641a76289SEmmanuel Vadot sc->opp[i].uvolt_target);
46741a76289SEmmanuel Vadot }
46841a76289SEmmanuel Vadot return (0);
46941a76289SEmmanuel Vadot }
47041a76289SEmmanuel Vadot
47141a76289SEmmanuel Vadot static int
cpufreq_dt_attach(device_t dev)472319336a9SJared McNeill cpufreq_dt_attach(device_t dev)
473319336a9SJared McNeill {
474319336a9SJared McNeill struct cpufreq_dt_softc *sc;
47541a76289SEmmanuel Vadot phandle_t node;
47641a76289SEmmanuel Vadot phandle_t cnode, opp, copp;
477ddef7bb5SEmmanuel Vadot int cpu;
47841a76289SEmmanuel Vadot uint64_t freq;
47941a76289SEmmanuel Vadot int rv = 0;
480a969e975SEmmanuel Vadot char device_type[16];
48141a76289SEmmanuel Vadot enum opp_version version;
482319336a9SJared McNeill
483319336a9SJared McNeill sc = device_get_softc(dev);
48441a76289SEmmanuel Vadot sc->dev = dev;
485319336a9SJared McNeill node = ofw_bus_get_node(device_get_parent(dev));
486a969e975SEmmanuel Vadot sc->cpu = device_get_unit(device_get_parent(dev));
48705860ffdSAdrian Chadd sc->reg = NULL;
4884b23e1e5SEmmanuel Vadot
4894707401cSEmmanuel Vadot DPRINTF(dev, "cpu=%d\n", sc->cpu);
490a969e975SEmmanuel Vadot if (sc->cpu >= mp_ncpus) {
4914b23e1e5SEmmanuel Vadot device_printf(dev, "Not attaching as cpu is not present\n");
49205860ffdSAdrian Chadd rv = ENXIO;
49305860ffdSAdrian Chadd goto error;
4944b23e1e5SEmmanuel Vadot }
495319336a9SJared McNeill
49605860ffdSAdrian Chadd /*
49705860ffdSAdrian Chadd * Cache if we have the regulator supply but don't error out
49805860ffdSAdrian Chadd * quite yet. If it's operating-points-v2 then regulator
49905860ffdSAdrian Chadd * and voltage entries are optional.
50005860ffdSAdrian Chadd */
50105860ffdSAdrian Chadd if (regulator_get_by_ofw_property(dev, node, "cpu-supply",
50205860ffdSAdrian Chadd &sc->reg) == 0)
50305860ffdSAdrian Chadd device_printf(dev, "Found cpu-supply\n");
50405860ffdSAdrian Chadd else if (regulator_get_by_ofw_property(dev, node, "cpu0-supply",
50505860ffdSAdrian Chadd &sc->reg) == 0)
50605860ffdSAdrian Chadd device_printf(dev, "Found cpu0-supply\n");
50705860ffdSAdrian Chadd
50805860ffdSAdrian Chadd /*
50905860ffdSAdrian Chadd * Determine which operating mode we're in. Error out if we expect
51005860ffdSAdrian Chadd * a regulator but we're not getting it.
51105860ffdSAdrian Chadd */
51205860ffdSAdrian Chadd if (OF_hasprop(node, "operating-points"))
51305860ffdSAdrian Chadd version = OPP_V1;
51405860ffdSAdrian Chadd else if (OF_hasprop(node, "operating-points-v2"))
51505860ffdSAdrian Chadd version = OPP_V2;
51605860ffdSAdrian Chadd else {
51705860ffdSAdrian Chadd device_printf(dev,
51805860ffdSAdrian Chadd "didn't find a valid operating-points or v2 node\n");
51905860ffdSAdrian Chadd rv = ENXIO;
52005860ffdSAdrian Chadd goto error;
52105860ffdSAdrian Chadd }
52205860ffdSAdrian Chadd
52305860ffdSAdrian Chadd /*
52405860ffdSAdrian Chadd * Now, we only enforce needing a regulator for v1.
52505860ffdSAdrian Chadd */
52605860ffdSAdrian Chadd if ((version == OPP_V1) && !CPUFREQ_DT_HAVE_REGULATOR(sc)) {
527319336a9SJared McNeill device_printf(dev, "no regulator for %s\n",
528319336a9SJared McNeill ofw_bus_get_name(device_get_parent(dev)));
52905860ffdSAdrian Chadd rv = ENXIO;
53005860ffdSAdrian Chadd goto error;
531a701764eSMichal Meloun }
532319336a9SJared McNeill
533319336a9SJared McNeill if (clk_get_by_ofw_index(dev, node, 0, &sc->clk) != 0) {
534319336a9SJared McNeill device_printf(dev, "no clock for %s\n",
535319336a9SJared McNeill ofw_bus_get_name(device_get_parent(dev)));
53605860ffdSAdrian Chadd rv = ENXIO;
53705860ffdSAdrian Chadd goto error;
538319336a9SJared McNeill }
539319336a9SJared McNeill
54005860ffdSAdrian Chadd if (version == OPP_V1) {
54141a76289SEmmanuel Vadot rv = cpufreq_dt_oppv1_parse(sc, node);
54241a76289SEmmanuel Vadot if (rv != 0) {
54341a76289SEmmanuel Vadot device_printf(dev, "Failed to parse opp-v1 table\n");
54405860ffdSAdrian Chadd goto error;
545319336a9SJared McNeill }
54641a76289SEmmanuel Vadot OF_getencprop(node, "operating-points", &opp,
54741a76289SEmmanuel Vadot sizeof(opp));
54805860ffdSAdrian Chadd } else if (version == OPP_V2) {
54941a76289SEmmanuel Vadot rv = cpufreq_dt_oppv2_parse(sc, node);
55041a76289SEmmanuel Vadot if (rv != 0) {
55141a76289SEmmanuel Vadot device_printf(dev, "Failed to parse opp-v2 table\n");
55205860ffdSAdrian Chadd goto error;
55341a76289SEmmanuel Vadot }
55441a76289SEmmanuel Vadot OF_getencprop(node, "operating-points-v2", &opp,
55541a76289SEmmanuel Vadot sizeof(opp));
55605860ffdSAdrian Chadd } else {
55705860ffdSAdrian Chadd device_printf(dev, "operating points version is incorrect\n");
55805860ffdSAdrian Chadd goto error;
55941a76289SEmmanuel Vadot }
560319336a9SJared McNeill
561319336a9SJared McNeill /*
56241a76289SEmmanuel Vadot * Find all CPUs that share the same opp table
563319336a9SJared McNeill */
564319336a9SJared McNeill CPU_ZERO(&sc->cpus);
565a969e975SEmmanuel Vadot cnode = OF_parent(node);
566a969e975SEmmanuel Vadot for (cpu = 0, cnode = OF_child(cnode); cnode > 0; cnode = OF_peer(cnode)) {
567a969e975SEmmanuel Vadot if (OF_getprop(cnode, "device_type", device_type, sizeof(device_type)) <= 0)
568a969e975SEmmanuel Vadot continue;
569a969e975SEmmanuel Vadot if (strcmp(device_type, "cpu") != 0)
570a969e975SEmmanuel Vadot continue;
571a969e975SEmmanuel Vadot if (cpu == sc->cpu) {
5724707401cSEmmanuel Vadot DPRINTF(dev, "Skipping our cpu\n");
5737c962201SEmmanuel Vadot CPU_SET(cpu, &sc->cpus);
5747c962201SEmmanuel Vadot cpu++;
575a969e975SEmmanuel Vadot continue;
576a969e975SEmmanuel Vadot }
5774707401cSEmmanuel Vadot DPRINTF(dev, "Testing CPU %d\n", cpu);
57841a76289SEmmanuel Vadot copp = -1;
57941a76289SEmmanuel Vadot if (version == OPP_V1)
58041a76289SEmmanuel Vadot OF_getencprop(cnode, "operating-points", &copp,
58141a76289SEmmanuel Vadot sizeof(copp));
58241a76289SEmmanuel Vadot else if (version == OPP_V2)
58341a76289SEmmanuel Vadot OF_getencprop(cnode, "operating-points-v2",
58441a76289SEmmanuel Vadot &copp, sizeof(copp));
585a969e975SEmmanuel Vadot if (opp == copp) {
5864707401cSEmmanuel Vadot DPRINTF(dev, "CPU %d is using the same opp as this one (%d)\n",
5874707401cSEmmanuel Vadot cpu, sc->cpu);
588319336a9SJared McNeill CPU_SET(cpu, &sc->cpus);
589319336a9SJared McNeill }
590a969e975SEmmanuel Vadot cpu++;
591a969e975SEmmanuel Vadot }
592319336a9SJared McNeill
593319336a9SJared McNeill if (clk_get_freq(sc->clk, &freq) == 0)
594319336a9SJared McNeill cpufreq_dt_notify(dev, freq);
595319336a9SJared McNeill
596319336a9SJared McNeill cpufreq_register(dev);
597319336a9SJared McNeill
598319336a9SJared McNeill return (0);
59905860ffdSAdrian Chadd error:
60005860ffdSAdrian Chadd if (CPUFREQ_DT_HAVE_REGULATOR(sc))
60105860ffdSAdrian Chadd regulator_release(sc->reg);
60205860ffdSAdrian Chadd return (rv);
603319336a9SJared McNeill }
604319336a9SJared McNeill
605319336a9SJared McNeill static device_method_t cpufreq_dt_methods[] = {
606319336a9SJared McNeill /* Device interface */
607319336a9SJared McNeill DEVMETHOD(device_identify, cpufreq_dt_identify),
608319336a9SJared McNeill DEVMETHOD(device_probe, cpufreq_dt_probe),
609319336a9SJared McNeill DEVMETHOD(device_attach, cpufreq_dt_attach),
610319336a9SJared McNeill
611319336a9SJared McNeill /* cpufreq interface */
612319336a9SJared McNeill DEVMETHOD(cpufreq_drv_get, cpufreq_dt_get),
613319336a9SJared McNeill DEVMETHOD(cpufreq_drv_set, cpufreq_dt_set),
614319336a9SJared McNeill DEVMETHOD(cpufreq_drv_type, cpufreq_dt_type),
615319336a9SJared McNeill DEVMETHOD(cpufreq_drv_settings, cpufreq_dt_settings),
616319336a9SJared McNeill
617319336a9SJared McNeill DEVMETHOD_END
618319336a9SJared McNeill };
619319336a9SJared McNeill
620319336a9SJared McNeill static driver_t cpufreq_dt_driver = {
621319336a9SJared McNeill "cpufreq_dt",
622319336a9SJared McNeill cpufreq_dt_methods,
623319336a9SJared McNeill sizeof(struct cpufreq_dt_softc),
624319336a9SJared McNeill };
625319336a9SJared McNeill
626b3407dccSJohn Baldwin DRIVER_MODULE(cpufreq_dt, cpu, cpufreq_dt_driver, 0, 0);
627319336a9SJared McNeill MODULE_VERSION(cpufreq_dt, 1);
628