2 * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.Org>
3 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Generic DT based cpufreq driver
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/cpuset.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
49 #include <dev/extres/clk/clk.h>
50 #include <dev/extres/regulator/regulator.h>
52 #include "cpufreq_if.h"
55 #define DEBUG(dev, msg...) device_printf(dev, "cpufreq_dt: " msg);
57 #define DEBUG(dev, msg...)
65 struct cpufreq_dt_opp {
67 uint32_t uvolt_target;
76 struct cpufreq_dt_softc {
81 struct cpufreq_dt_opp *opp;
88 cpufreq_dt_notify(device_t dev, uint64_t freq)
90 struct cpufreq_dt_softc *sc;
94 sc = device_get_softc(dev);
97 if (CPU_ISSET(cpu, &sc->cpus)) {
104 static const struct cpufreq_dt_opp *
105 cpufreq_dt_find_opp(device_t dev, uint64_t freq)
107 struct cpufreq_dt_softc *sc;
110 sc = device_get_softc(dev);
112 DEBUG(dev, "Looking for freq %ju\n", freq);
113 for (n = 0; n < sc->nopp; n++)
114 if (CPUFREQ_CMP(sc->opp[n].freq, freq))
115 return (&sc->opp[n]);
117 DEBUG(dev, "Couldn't find one\n");
122 cpufreq_dt_opp_to_setting(device_t dev, const struct cpufreq_dt_opp *opp,
123 struct cf_setting *set)
125 struct cpufreq_dt_softc *sc;
127 sc = device_get_softc(dev);
129 memset(set, 0, sizeof(*set));
130 set->freq = opp->freq / 1000000;
131 set->volts = opp->uvolt_target / 1000;
132 set->power = CPUFREQ_VAL_UNKNOWN;
133 set->lat = opp->clk_latency;
138 cpufreq_dt_get(device_t dev, struct cf_setting *set)
140 struct cpufreq_dt_softc *sc;
141 const struct cpufreq_dt_opp *opp;
144 sc = device_get_softc(dev);
146 DEBUG(dev, "cpufreq_dt_get\n");
147 if (clk_get_freq(sc->clk, &freq) != 0)
150 opp = cpufreq_dt_find_opp(dev, freq);
152 device_printf(dev, "Can't find the current freq in opp\n");
156 cpufreq_dt_opp_to_setting(dev, opp, set);
158 DEBUG(dev, "Current freq %dMhz\n", set->freq);
163 cpufreq_dt_set(device_t dev, const struct cf_setting *set)
165 struct cpufreq_dt_softc *sc;
166 const struct cpufreq_dt_opp *opp, *copp;
170 sc = device_get_softc(dev);
172 if (clk_get_freq(sc->clk, &freq) != 0) {
173 device_printf(dev, "Can't get current clk freq\n");
176 /* Try to get current valtage by using regulator first. */
177 error = regulator_get_voltage(sc->reg, &uvolt);
180 * Try oppoints table as backup way. However,
181 * this is insufficient because the actual processor
182 * frequency may not be in the table. PLL frequency
183 * granularity can be different that granularity of
186 copp = cpufreq_dt_find_opp(sc->dev, freq);
189 "Can't find the current freq in opp\n");
192 uvolt = copp->uvolt_target;
196 opp = cpufreq_dt_find_opp(sc->dev, set->freq * 1000000);
198 device_printf(dev, "Couldn't find an opp for this freq\n");
201 DEBUG(sc->dev, "Current freq %ju, uvolt: %d\n", freq, uvolt);
202 DEBUG(sc->dev, "Target freq %ju, , uvolt: %d\n",
203 opp->freq, opp->uvolt_target);
205 if (uvolt < opp->uvolt_target) {
206 DEBUG(dev, "Changing regulator from %u to %u\n",
207 uvolt, opp->uvolt_target);
208 error = regulator_set_voltage(sc->reg,
212 DEBUG(dev, "Failed, backout\n");
217 DEBUG(dev, "Setting clk to %ju\n", opp->freq);
218 error = clk_set_freq(sc->clk, opp->freq, CLK_SET_ROUND_DOWN);
220 DEBUG(dev, "Failed, backout\n");
221 /* Restore previous voltage (best effort) */
222 error = regulator_set_voltage(sc->reg,
228 if (uvolt > opp->uvolt_target) {
229 DEBUG(dev, "Changing regulator from %u to %u\n",
230 uvolt, opp->uvolt_target);
231 error = regulator_set_voltage(sc->reg,
235 DEBUG(dev, "Failed to switch regulator to %d\n",
237 /* Restore previous CPU frequency (best effort) */
238 (void)clk_set_freq(sc->clk, copp->freq, 0);
243 if (clk_get_freq(sc->clk, &freq) == 0)
244 cpufreq_dt_notify(dev, freq);
251 cpufreq_dt_type(device_t dev, int *type)
256 *type = CPUFREQ_TYPE_ABSOLUTE;
261 cpufreq_dt_settings(device_t dev, struct cf_setting *sets, int *count)
263 struct cpufreq_dt_softc *sc;
266 DEBUG(dev, "cpufreq_dt_settings\n");
267 if (sets == NULL || count == NULL)
270 sc = device_get_softc(dev);
272 if (*count < sc->nopp) {
273 *count = (int)sc->nopp;
277 for (n = 0; n < sc->nopp; n++)
278 cpufreq_dt_opp_to_setting(dev, &sc->opp[n], &sets[n]);
280 *count = (int)sc->nopp;
286 cpufreq_dt_identify(driver_t *driver, device_t parent)
290 /* Properties must be listed under node /cpus/cpu@0 */
291 node = ofw_bus_get_node(parent);
293 /* The cpu@0 node must have the following properties */
294 if (!OF_hasprop(node, "clocks") ||
295 (!OF_hasprop(node, "cpu-supply") &&
296 !OF_hasprop(node, "cpu0-supply")))
299 if (!OF_hasprop(node, "operating-points") &&
300 !OF_hasprop(node, "operating-points-v2"))
303 if (device_find_child(parent, "cpufreq_dt", -1) != NULL)
306 if (BUS_ADD_CHILD(parent, 0, "cpufreq_dt", -1) == NULL)
307 device_printf(parent, "add cpufreq_dt child failed\n");
311 cpufreq_dt_probe(device_t dev)
315 node = ofw_bus_get_node(device_get_parent(dev));
317 if (!OF_hasprop(node, "clocks") ||
318 (!OF_hasprop(node, "cpu-supply") &&
319 !OF_hasprop(node, "cpu0-supply")))
323 if (!OF_hasprop(node, "operating-points") &&
324 !OF_hasprop(node, "operating-points-v2"))
327 device_set_desc(dev, "Generic cpufreq driver");
328 return (BUS_PROBE_GENERIC);
332 cpufreq_dt_oppv1_parse(struct cpufreq_dt_softc *sc, phandle_t node)
337 sc->nopp = OF_getencprop_alloc_multi(node, "operating-points",
338 sizeof(uint32_t) * 2, (void **)&opp);
342 if (OF_getencprop(node, "clock-latency", &lat, sizeof(lat)) == -1)
343 lat = CPUFREQ_VAL_UNKNOWN;
345 sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
347 for (n = 0; n < sc->nopp; n++) {
348 sc->opp[n].freq = opp[n * 2 + 0] * 1000;
349 sc->opp[n].uvolt_min = opp[n * 2 + 1];
350 sc->opp[n].uvolt_max = sc->opp[n].uvolt_min;
351 sc->opp[n].uvolt_target = sc->opp[n].uvolt_min;
352 sc->opp[n].clk_latency = lat;
355 device_printf(sc->dev, "%ju.%03ju MHz, %u uV\n",
356 sc->opp[n].freq / 1000000,
357 sc->opp[n].freq % 1000000,
358 sc->opp[n].uvolt_target);
360 free(opp, M_OFWPROP);
366 cpufreq_dt_oppv2_parse(struct cpufreq_dt_softc *sc, phandle_t node)
368 phandle_t opp, opp_table, opp_xref;
370 uint32_t *volts, lat;
373 if (OF_getencprop(node, "operating-points-v2", &opp_xref,
374 sizeof(opp_xref)) == -1) {
375 device_printf(sc->dev, "Cannot get xref to oppv2 table\n");
379 opp_table = OF_node_from_xref(opp_xref);
380 if (opp_table == opp_xref)
383 if (!OF_hasprop(opp_table, "opp-shared")) {
384 device_printf(sc->dev, "Only opp-shared is supported\n");
388 for (opp = OF_child(opp_table); opp > 0; opp = OF_peer(opp))
391 sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
393 for (i = 0, opp_table = OF_child(opp_table); opp_table > 0;
394 opp_table = OF_peer(opp_table), i++) {
395 /* opp-hz is a required property */
396 if (OF_getencprop(opp_table, "opp-hz", cell,
400 sc->opp[i].freq = cell[0];
401 sc->opp[i].freq <<= 32;
402 sc->opp[i].freq |= cell[1];
404 if (OF_getencprop(opp_table, "clock-latency", &lat,
406 sc->opp[i].clk_latency = CPUFREQ_VAL_UNKNOWN;
408 sc->opp[i].clk_latency = (int)lat;
410 if (OF_hasprop(opp_table, "turbo-mode"))
411 sc->opp[i].turbo_mode = true;
412 if (OF_hasprop(opp_table, "opp-suspend"))
413 sc->opp[i].opp_suspend = true;
415 nvolt = OF_getencprop_alloc_multi(opp_table, "opp-microvolt",
416 sizeof(*volts), (void **)&volts);
418 sc->opp[i].uvolt_target = volts[0];
419 sc->opp[i].uvolt_min = volts[0];
420 sc->opp[i].uvolt_max = volts[0];
421 } else if (nvolt == 3) {
422 sc->opp[i].uvolt_target = volts[0];
423 sc->opp[i].uvolt_min = volts[1];
424 sc->opp[i].uvolt_max = volts[2];
426 device_printf(sc->dev,
427 "Wrong count of opp-microvolt property\n");
429 free(sc->opp, M_DEVBUF);
435 device_printf(sc->dev, "%ju.%03ju Mhz (%u uV)\n",
436 sc->opp[i].freq / 1000000,
437 sc->opp[i].freq % 1000000,
438 sc->opp[i].uvolt_target);
444 cpufreq_dt_attach(device_t dev)
446 struct cpufreq_dt_softc *sc;
448 phandle_t cnode, opp, copp;
452 enum opp_version version;
454 sc = device_get_softc(dev);
456 node = ofw_bus_get_node(device_get_parent(dev));
458 if (regulator_get_by_ofw_property(dev, node,
459 "cpu-supply", &sc->reg) != 0) {
460 if (regulator_get_by_ofw_property(dev, node,
461 "cpu0-supply", &sc->reg) != 0) {
462 device_printf(dev, "no regulator for %s\n",
463 ofw_bus_get_name(device_get_parent(dev)));
468 if (clk_get_by_ofw_index(dev, node, 0, &sc->clk) != 0) {
469 device_printf(dev, "no clock for %s\n",
470 ofw_bus_get_name(device_get_parent(dev)));
471 regulator_release(sc->reg);
475 if (OF_hasprop(node, "operating-points")) {
477 rv = cpufreq_dt_oppv1_parse(sc, node);
479 device_printf(dev, "Failed to parse opp-v1 table\n");
482 OF_getencprop(node, "operating-points", &opp,
486 rv = cpufreq_dt_oppv2_parse(sc, node);
488 device_printf(dev, "Failed to parse opp-v2 table\n");
491 OF_getencprop(node, "operating-points-v2", &opp,
496 * Find all CPUs that share the same opp table
499 cpu = device_get_unit(device_get_parent(dev));
500 for (cnode = node; cnode > 0; cnode = OF_peer(cnode), cpu++) {
502 if (version == OPP_V1)
503 OF_getencprop(cnode, "operating-points", &copp,
505 else if (version == OPP_V2)
506 OF_getencprop(cnode, "operating-points-v2",
507 &copp, sizeof(copp));
509 CPU_SET(cpu, &sc->cpus);
512 if (clk_get_freq(sc->clk, &freq) == 0)
513 cpufreq_dt_notify(dev, freq);
515 cpufreq_register(dev);
521 static device_method_t cpufreq_dt_methods[] = {
522 /* Device interface */
523 DEVMETHOD(device_identify, cpufreq_dt_identify),
524 DEVMETHOD(device_probe, cpufreq_dt_probe),
525 DEVMETHOD(device_attach, cpufreq_dt_attach),
527 /* cpufreq interface */
528 DEVMETHOD(cpufreq_drv_get, cpufreq_dt_get),
529 DEVMETHOD(cpufreq_drv_set, cpufreq_dt_set),
530 DEVMETHOD(cpufreq_drv_type, cpufreq_dt_type),
531 DEVMETHOD(cpufreq_drv_settings, cpufreq_dt_settings),
536 static driver_t cpufreq_dt_driver = {
539 sizeof(struct cpufreq_dt_softc),
542 static devclass_t cpufreq_dt_devclass;
544 DRIVER_MODULE(cpufreq_dt, cpu, cpufreq_dt_driver, cpufreq_dt_devclass, 0, 0);
545 MODULE_VERSION(cpufreq_dt, 1);