2 * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.Org>
3 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Generic DT based cpufreq driver
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
44 #include <sys/cpuset.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
50 #include <dev/extres/clk/clk.h>
51 #include <dev/extres/regulator/regulator.h>
53 #include "cpufreq_if.h"
56 #define DEBUG(dev, msg...) device_printf(dev, "cpufreq_dt: " msg);
58 #define DEBUG(dev, msg...)
66 struct cpufreq_dt_opp {
68 uint32_t uvolt_target;
77 struct cpufreq_dt_softc {
82 struct cpufreq_dt_opp *opp;
89 cpufreq_dt_notify(device_t dev, uint64_t freq)
91 struct cpufreq_dt_softc *sc;
95 sc = device_get_softc(dev);
98 if (CPU_ISSET(cpu, &sc->cpus)) {
105 static const struct cpufreq_dt_opp *
106 cpufreq_dt_find_opp(device_t dev, uint64_t freq)
108 struct cpufreq_dt_softc *sc;
111 sc = device_get_softc(dev);
113 DEBUG(dev, "Looking for freq %ju\n", freq);
114 for (n = 0; n < sc->nopp; n++)
115 if (CPUFREQ_CMP(sc->opp[n].freq, freq))
116 return (&sc->opp[n]);
118 DEBUG(dev, "Couldn't find one\n");
123 cpufreq_dt_opp_to_setting(device_t dev, const struct cpufreq_dt_opp *opp,
124 struct cf_setting *set)
126 struct cpufreq_dt_softc *sc;
128 sc = device_get_softc(dev);
130 memset(set, 0, sizeof(*set));
131 set->freq = opp->freq / 1000000;
132 set->volts = opp->uvolt_target / 1000;
133 set->power = CPUFREQ_VAL_UNKNOWN;
134 set->lat = opp->clk_latency;
139 cpufreq_dt_get(device_t dev, struct cf_setting *set)
141 struct cpufreq_dt_softc *sc;
142 const struct cpufreq_dt_opp *opp;
145 sc = device_get_softc(dev);
147 DEBUG(dev, "cpufreq_dt_get\n");
148 if (clk_get_freq(sc->clk, &freq) != 0)
151 opp = cpufreq_dt_find_opp(dev, freq);
153 device_printf(dev, "Can't find the current freq in opp\n");
157 cpufreq_dt_opp_to_setting(dev, opp, set);
159 DEBUG(dev, "Current freq %dMhz\n", set->freq);
164 cpufreq_dt_set(device_t dev, const struct cf_setting *set)
166 struct cpufreq_dt_softc *sc;
167 const struct cpufreq_dt_opp *opp, *copp;
171 sc = device_get_softc(dev);
173 if (clk_get_freq(sc->clk, &freq) != 0) {
174 device_printf(dev, "Can't get current clk freq\n");
178 DEBUG(sc->dev, "Current freq %ju\n", freq);
179 DEBUG(sc->dev, "Target freq %ju\n", (uint64_t)set->freq * 1000000);
180 copp = cpufreq_dt_find_opp(sc->dev, freq);
182 device_printf(dev, "Can't find the current freq in opp\n");
185 opp = cpufreq_dt_find_opp(sc->dev, set->freq * 1000000);
187 device_printf(dev, "Couldn't find an opp for this freq\n");
191 if (copp->uvolt_target < opp->uvolt_target) {
192 DEBUG(dev, "Changing regulator from %u to %u\n",
193 copp->uvolt_target, opp->uvolt_target);
194 error = regulator_set_voltage(sc->reg,
198 DEBUG(dev, "Failed, backout\n");
203 DEBUG(dev, "Setting clk to %ju\n", opp->freq);
204 error = clk_set_freq(sc->clk, opp->freq, 0);
206 DEBUG(dev, "Failed, backout\n");
207 /* Restore previous voltage (best effort) */
208 error = regulator_set_voltage(sc->reg,
214 if (copp->uvolt_target > opp->uvolt_target) {
215 error = regulator_set_voltage(sc->reg,
219 DEBUG(dev, "Failed to switch regulator to %d\n",
221 /* Restore previous CPU frequency (best effort) */
222 (void)clk_set_freq(sc->clk,
228 if (clk_get_freq(sc->clk, &freq) == 0)
229 cpufreq_dt_notify(dev, freq);
236 cpufreq_dt_type(device_t dev, int *type)
241 *type = CPUFREQ_TYPE_ABSOLUTE;
246 cpufreq_dt_settings(device_t dev, struct cf_setting *sets, int *count)
248 struct cpufreq_dt_softc *sc;
251 DEBUG(dev, "cpufreq_dt_settings\n");
252 if (sets == NULL || count == NULL)
255 sc = device_get_softc(dev);
257 if (*count < sc->nopp) {
258 *count = (int)sc->nopp;
262 for (n = 0; n < sc->nopp; n++)
263 cpufreq_dt_opp_to_setting(dev, &sc->opp[n], &sets[n]);
265 *count = (int)sc->nopp;
271 cpufreq_dt_identify(driver_t *driver, device_t parent)
275 /* Properties must be listed under node /cpus/cpu@0 */
276 node = ofw_bus_get_node(parent);
278 /* The cpu@0 node must have the following properties */
279 if (!OF_hasprop(node, "clocks") ||
280 !OF_hasprop(node, "cpu-supply"))
283 if (!OF_hasprop(node, "operating-points") &&
284 !OF_hasprop(node, "operating-points-v2"))
287 if (device_find_child(parent, "cpufreq_dt", -1) != NULL)
290 if (BUS_ADD_CHILD(parent, 0, "cpufreq_dt", -1) == NULL)
291 device_printf(parent, "add cpufreq_dt child failed\n");
295 cpufreq_dt_probe(device_t dev)
299 node = ofw_bus_get_node(device_get_parent(dev));
301 if (!OF_hasprop(node, "clocks") ||
302 !OF_hasprop(node, "cpu-supply"))
305 if (!OF_hasprop(node, "operating-points") &&
306 !OF_hasprop(node, "operating-points-v2"))
309 device_set_desc(dev, "Generic cpufreq driver");
310 return (BUS_PROBE_GENERIC);
314 cpufreq_dt_oppv1_parse(struct cpufreq_dt_softc *sc, phandle_t node)
319 sc->nopp = OF_getencprop_alloc_multi(node, "operating-points",
320 sizeof(uint32_t) * 2, (void **)&opp);
324 if (OF_getencprop(node, "clock-latency", &lat, sizeof(lat)) == -1)
325 lat = CPUFREQ_VAL_UNKNOWN;
327 sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
329 for (n = 0; n < sc->nopp; n++) {
330 sc->opp[n].freq = opp[n * 2 + 0] * 1000;
331 sc->opp[n].uvolt_min = opp[n * 2 + 1];
332 sc->opp[n].uvolt_max = sc->opp[n].uvolt_min;
333 sc->opp[n].uvolt_target = sc->opp[n].uvolt_min;
334 sc->opp[n].clk_latency = lat;
337 device_printf(sc->dev, "%ju.%03ju MHz, %u uV\n",
338 sc->opp[n].freq / 1000000,
339 sc->opp[n].freq % 1000000,
340 sc->opp[n].uvolt_target);
342 free(opp, M_OFWPROP);
348 cpufreq_dt_oppv2_parse(struct cpufreq_dt_softc *sc, phandle_t node)
350 phandle_t opp, opp_table, opp_xref;
352 uint32_t *volts, lat;
355 if (OF_getencprop(node, "operating-points-v2", &opp_xref,
356 sizeof(opp_xref)) == -1) {
357 device_printf(sc->dev, "Cannot get xref to oppv2 table\n");
361 opp_table = OF_node_from_xref(opp_xref);
362 if (opp_table == opp_xref)
365 if (!OF_hasprop(opp_table, "opp-shared")) {
366 device_printf(sc->dev, "Only opp-shared is supported\n");
370 for (opp = OF_child(opp_table); opp > 0; opp = OF_peer(opp))
373 sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
375 for (i = 0, opp_table = OF_child(opp_table); opp_table > 0;
376 opp_table = OF_peer(opp_table), i++) {
377 /* opp-hz is a required property */
378 if (OF_getencprop(opp_table, "opp-hz", cell,
382 sc->opp[i].freq = cell[0];
383 sc->opp[i].freq <<= 32;
384 sc->opp[i].freq |= cell[1];
386 if (OF_getencprop(opp_table, "clock-latency", &lat,
388 sc->opp[i].clk_latency = CPUFREQ_VAL_UNKNOWN;
390 sc->opp[i].clk_latency = (int)lat;
392 if (OF_hasprop(opp_table, "turbo-mode"))
393 sc->opp[i].turbo_mode = true;
394 if (OF_hasprop(opp_table, "opp-suspend"))
395 sc->opp[i].opp_suspend = true;
397 nvolt = OF_getencprop_alloc_multi(opp_table, "opp-microvolt",
398 sizeof(*volts), (void **)&volts);
400 sc->opp[i].uvolt_target = volts[0];
401 sc->opp[i].uvolt_min = volts[0];
402 sc->opp[i].uvolt_max = volts[0];
403 } else if (nvolt == 3) {
404 sc->opp[i].uvolt_target = volts[0];
405 sc->opp[i].uvolt_min = volts[1];
406 sc->opp[i].uvolt_max = volts[2];
408 device_printf(sc->dev,
409 "Wrong count of opp-microvolt property\n");
411 free(sc->opp, M_DEVBUF);
417 device_printf(sc->dev, "%ju.%03ju Mhz (%u uV)\n",
418 sc->opp[i].freq / 1000000,
419 sc->opp[i].freq % 1000000,
420 sc->opp[i].uvolt_target);
426 cpufreq_dt_attach(device_t dev)
428 struct cpufreq_dt_softc *sc;
430 phandle_t cnode, opp, copp;
434 enum opp_version version;
436 sc = device_get_softc(dev);
438 node = ofw_bus_get_node(device_get_parent(dev));
440 if (regulator_get_by_ofw_property(dev, node,
441 "cpu-supply", &sc->reg) != 0) {
442 device_printf(dev, "no regulator for %s\n",
443 ofw_bus_get_name(device_get_parent(dev)));
447 if (clk_get_by_ofw_index(dev, node, 0, &sc->clk) != 0) {
448 device_printf(dev, "no clock for %s\n",
449 ofw_bus_get_name(device_get_parent(dev)));
450 regulator_release(sc->reg);
454 if (OF_hasprop(node, "operating-points")) {
456 rv = cpufreq_dt_oppv1_parse(sc, node);
458 device_printf(dev, "Failed to parse opp-v1 table\n");
461 OF_getencprop(node, "operating-points", &opp,
465 rv = cpufreq_dt_oppv2_parse(sc, node);
467 device_printf(dev, "Failed to parse opp-v2 table\n");
470 OF_getencprop(node, "operating-points-v2", &opp,
475 * Find all CPUs that share the same opp table
478 cpu = device_get_unit(device_get_parent(dev));
479 for (cnode = node; cnode > 0; cnode = OF_peer(cnode), cpu++) {
481 if (version == OPP_V1)
482 OF_getencprop(cnode, "operating-points", &copp,
484 else if (version == OPP_V2)
485 OF_getencprop(cnode, "operating-points-v2",
486 &copp, sizeof(copp));
488 CPU_SET(cpu, &sc->cpus);
491 if (clk_get_freq(sc->clk, &freq) == 0)
492 cpufreq_dt_notify(dev, freq);
494 cpufreq_register(dev);
500 static device_method_t cpufreq_dt_methods[] = {
501 /* Device interface */
502 DEVMETHOD(device_identify, cpufreq_dt_identify),
503 DEVMETHOD(device_probe, cpufreq_dt_probe),
504 DEVMETHOD(device_attach, cpufreq_dt_attach),
506 /* cpufreq interface */
507 DEVMETHOD(cpufreq_drv_get, cpufreq_dt_get),
508 DEVMETHOD(cpufreq_drv_set, cpufreq_dt_set),
509 DEVMETHOD(cpufreq_drv_type, cpufreq_dt_type),
510 DEVMETHOD(cpufreq_drv_settings, cpufreq_dt_settings),
515 static driver_t cpufreq_dt_driver = {
518 sizeof(struct cpufreq_dt_softc),
521 static devclass_t cpufreq_dt_devclass;
523 DRIVER_MODULE(cpufreq_dt, cpu, cpufreq_dt_driver, cpufreq_dt_devclass, 0, 0);
524 MODULE_VERSION(cpufreq_dt, 1);