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 DPRINTF(dev, msg...) device_printf(dev, "cpufreq_dt: " msg);
57 #define DPRINTF(dev, msg...)
65 struct cpufreq_dt_opp {
67 uint32_t uvolt_target;
76 #define CPUFREQ_DT_HAVE_REGULATOR(sc) ((sc)->reg != NULL)
78 struct cpufreq_dt_softc {
83 struct cpufreq_dt_opp *opp;
91 cpufreq_dt_notify(device_t dev, uint64_t freq)
93 struct cpufreq_dt_softc *sc;
97 sc = device_get_softc(dev);
100 if (CPU_ISSET(cpu, &sc->cpus)) {
107 static const struct cpufreq_dt_opp *
108 cpufreq_dt_find_opp(device_t dev, uint64_t freq)
110 struct cpufreq_dt_softc *sc;
113 sc = device_get_softc(dev);
115 DPRINTF(dev, "Looking for freq %ju\n", freq);
116 for (n = 0; n < sc->nopp; n++)
117 if (CPUFREQ_CMP(sc->opp[n].freq, freq))
118 return (&sc->opp[n]);
120 DPRINTF(dev, "Couldn't find one\n");
125 cpufreq_dt_opp_to_setting(device_t dev, const struct cpufreq_dt_opp *opp,
126 struct cf_setting *set)
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 DPRINTF(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 DPRINTF(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 DPRINTF(dev, "Working on cpu %d\n", sc->cpu);
173 DPRINTF(dev, "We have %d cpu on this dev\n", CPU_COUNT(&sc->cpus));
174 if (!CPU_ISSET(sc->cpu, &sc->cpus)) {
175 DPRINTF(dev, "Not for this CPU\n");
179 if (clk_get_freq(sc->clk, &freq) != 0) {
180 device_printf(dev, "Can't get current clk freq\n");
185 * Only do the regulator work if it's required.
187 if (CPUFREQ_DT_HAVE_REGULATOR(sc)) {
188 /* Try to get current valtage by using regulator first. */
189 error = regulator_get_voltage(sc->reg, &uvolt);
192 * Try oppoints table as backup way. However,
193 * this is insufficient because the actual processor
194 * frequency may not be in the table. PLL frequency
195 * granularity can be different that granularity of
198 copp = cpufreq_dt_find_opp(sc->dev, freq);
201 "Can't find the current freq in opp\n");
204 uvolt = copp->uvolt_target;
209 opp = cpufreq_dt_find_opp(sc->dev, set->freq * 1000000);
211 device_printf(dev, "Couldn't find an opp for this freq\n");
214 DPRINTF(sc->dev, "Current freq %ju, uvolt: %d\n", freq, uvolt);
215 DPRINTF(sc->dev, "Target freq %ju, , uvolt: %d\n",
216 opp->freq, opp->uvolt_target);
218 if (CPUFREQ_DT_HAVE_REGULATOR(sc) && (uvolt < opp->uvolt_target)) {
219 DPRINTF(dev, "Changing regulator from %u to %u\n",
220 uvolt, opp->uvolt_target);
221 error = regulator_set_voltage(sc->reg,
225 DPRINTF(dev, "Failed, backout\n");
230 DPRINTF(dev, "Setting clk to %ju\n", opp->freq);
231 error = clk_set_freq(sc->clk, opp->freq, CLK_SET_ROUND_DOWN);
233 DPRINTF(dev, "Failed, backout\n");
234 /* Restore previous voltage (best effort) */
235 if (CPUFREQ_DT_HAVE_REGULATOR(sc))
236 error = regulator_set_voltage(sc->reg,
242 if (CPUFREQ_DT_HAVE_REGULATOR(sc) && (uvolt > opp->uvolt_target)) {
243 DPRINTF(dev, "Changing regulator from %u to %u\n",
244 uvolt, opp->uvolt_target);
245 error = regulator_set_voltage(sc->reg,
249 DPRINTF(dev, "Failed to switch regulator to %d\n",
251 /* Restore previous CPU frequency (best effort) */
252 (void)clk_set_freq(sc->clk, copp->freq, 0);
257 if (clk_get_freq(sc->clk, &freq) == 0)
258 cpufreq_dt_notify(dev, freq);
264 cpufreq_dt_type(device_t dev, int *type)
269 *type = CPUFREQ_TYPE_ABSOLUTE;
274 cpufreq_dt_settings(device_t dev, struct cf_setting *sets, int *count)
276 struct cpufreq_dt_softc *sc;
279 DPRINTF(dev, "cpufreq_dt_settings\n");
280 if (sets == NULL || count == NULL)
283 sc = device_get_softc(dev);
285 if (*count < sc->nopp) {
286 *count = (int)sc->nopp;
290 for (n = 0; n < sc->nopp; n++)
291 cpufreq_dt_opp_to_setting(dev, &sc->opp[n], &sets[n]);
293 *count = (int)sc->nopp;
299 cpufreq_dt_identify(driver_t *driver, device_t parent)
303 /* Properties must be listed under node /cpus/cpu@0 */
304 node = ofw_bus_get_node(parent);
306 /* The cpu@0 node must have the following properties */
307 if (!OF_hasprop(node, "clocks"))
310 if (!OF_hasprop(node, "operating-points") &&
311 !OF_hasprop(node, "operating-points-v2"))
314 if (device_find_child(parent, "cpufreq_dt", -1) != NULL)
317 if (BUS_ADD_CHILD(parent, 0, "cpufreq_dt", device_get_unit(parent))
319 device_printf(parent, "add cpufreq_dt child failed\n");
323 cpufreq_dt_probe(device_t dev)
327 node = ofw_bus_get_node(device_get_parent(dev));
330 * Note - supply isn't required here for probe; we'll check
331 * it out in more detail during attach.
333 if (!OF_hasprop(node, "clocks"))
336 if (!OF_hasprop(node, "operating-points") &&
337 !OF_hasprop(node, "operating-points-v2"))
340 device_set_desc(dev, "Generic cpufreq driver");
341 return (BUS_PROBE_GENERIC);
345 cpufreq_dt_oppv1_parse(struct cpufreq_dt_softc *sc, phandle_t node)
350 sc->nopp = OF_getencprop_alloc_multi(node, "operating-points",
351 sizeof(uint32_t) * 2, (void **)&opp);
355 if (OF_getencprop(node, "clock-latency", &lat, sizeof(lat)) == -1)
356 lat = CPUFREQ_VAL_UNKNOWN;
358 sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
360 for (n = 0; n < sc->nopp; n++) {
361 sc->opp[n].freq = opp[n * 2 + 0] * 1000;
362 sc->opp[n].uvolt_min = opp[n * 2 + 1];
363 sc->opp[n].uvolt_max = sc->opp[n].uvolt_min;
364 sc->opp[n].uvolt_target = sc->opp[n].uvolt_min;
365 sc->opp[n].clk_latency = lat;
368 device_printf(sc->dev, "%ju.%03ju MHz, %u uV\n",
369 sc->opp[n].freq / 1000000,
370 sc->opp[n].freq % 1000000,
371 sc->opp[n].uvolt_target);
373 free(opp, M_OFWPROP);
379 cpufreq_dt_oppv2_parse(struct cpufreq_dt_softc *sc, phandle_t node)
381 phandle_t opp, opp_table, opp_xref;
383 uint32_t *volts, lat;
387 * operating-points-v2 does not require the voltage entries
388 * and a regulator. So, it's OK if they're not there.
390 if (OF_getencprop(node, "operating-points-v2", &opp_xref,
391 sizeof(opp_xref)) == -1) {
392 device_printf(sc->dev, "Cannot get xref to oppv2 table\n");
396 opp_table = OF_node_from_xref(opp_xref);
397 if (opp_table == opp_xref)
400 if (!OF_hasprop(opp_table, "opp-shared")) {
401 device_printf(sc->dev, "Only opp-shared is supported\n");
405 for (opp = OF_child(opp_table); opp > 0; opp = OF_peer(opp))
408 sc->opp = malloc(sizeof(*sc->opp) * sc->nopp, M_DEVBUF, M_WAITOK);
410 for (i = 0, opp_table = OF_child(opp_table); opp_table > 0;
411 opp_table = OF_peer(opp_table), i++) {
412 /* opp-hz is a required property */
413 if (OF_getencprop(opp_table, "opp-hz", cell,
417 sc->opp[i].freq = cell[0];
418 sc->opp[i].freq <<= 32;
419 sc->opp[i].freq |= cell[1];
421 if (OF_getencprop(opp_table, "clock-latency", &lat,
423 sc->opp[i].clk_latency = CPUFREQ_VAL_UNKNOWN;
425 sc->opp[i].clk_latency = (int)lat;
427 if (OF_hasprop(opp_table, "turbo-mode"))
428 sc->opp[i].turbo_mode = true;
429 if (OF_hasprop(opp_table, "opp-suspend"))
430 sc->opp[i].opp_suspend = true;
432 if (CPUFREQ_DT_HAVE_REGULATOR(sc)) {
433 nvolt = OF_getencprop_alloc_multi(opp_table,
434 "opp-microvolt", sizeof(*volts), (void **)&volts);
436 sc->opp[i].uvolt_target = volts[0];
437 sc->opp[i].uvolt_min = volts[0];
438 sc->opp[i].uvolt_max = volts[0];
439 } else if (nvolt == 3) {
440 sc->opp[i].uvolt_target = volts[0];
441 sc->opp[i].uvolt_min = volts[1];
442 sc->opp[i].uvolt_max = volts[2];
444 device_printf(sc->dev,
445 "Wrong count of opp-microvolt property\n");
447 free(sc->opp, M_DEVBUF);
452 /* No regulator required; don't add anything */
453 sc->opp[i].uvolt_target = 0;
454 sc->opp[i].uvolt_min = 0;
455 sc->opp[i].uvolt_max = 0;
459 device_printf(sc->dev, "%ju.%03ju Mhz (%u uV)\n",
460 sc->opp[i].freq / 1000000,
461 sc->opp[i].freq % 1000000,
462 sc->opp[i].uvolt_target);
468 cpufreq_dt_attach(device_t dev)
470 struct cpufreq_dt_softc *sc;
472 phandle_t cnode, opp, copp;
476 char device_type[16];
477 enum opp_version version;
479 sc = device_get_softc(dev);
481 node = ofw_bus_get_node(device_get_parent(dev));
482 sc->cpu = device_get_unit(device_get_parent(dev));
485 DPRINTF(dev, "cpu=%d\n", sc->cpu);
486 if (sc->cpu >= mp_ncpus) {
487 device_printf(dev, "Not attaching as cpu is not present\n");
493 * Cache if we have the regulator supply but don't error out
494 * quite yet. If it's operating-points-v2 then regulator
495 * and voltage entries are optional.
497 if (regulator_get_by_ofw_property(dev, node, "cpu-supply",
499 device_printf(dev, "Found cpu-supply\n");
500 else if (regulator_get_by_ofw_property(dev, node, "cpu0-supply",
502 device_printf(dev, "Found cpu0-supply\n");
505 * Determine which operating mode we're in. Error out if we expect
506 * a regulator but we're not getting it.
508 if (OF_hasprop(node, "operating-points"))
510 else if (OF_hasprop(node, "operating-points-v2"))
514 "didn't find a valid operating-points or v2 node\n");
520 * Now, we only enforce needing a regulator for v1.
522 if ((version == OPP_V1) && !CPUFREQ_DT_HAVE_REGULATOR(sc)) {
523 device_printf(dev, "no regulator for %s\n",
524 ofw_bus_get_name(device_get_parent(dev)));
529 if (clk_get_by_ofw_index(dev, node, 0, &sc->clk) != 0) {
530 device_printf(dev, "no clock for %s\n",
531 ofw_bus_get_name(device_get_parent(dev)));
536 if (version == OPP_V1) {
537 rv = cpufreq_dt_oppv1_parse(sc, node);
539 device_printf(dev, "Failed to parse opp-v1 table\n");
542 OF_getencprop(node, "operating-points", &opp,
544 } else if (version == OPP_V2) {
545 rv = cpufreq_dt_oppv2_parse(sc, node);
547 device_printf(dev, "Failed to parse opp-v2 table\n");
550 OF_getencprop(node, "operating-points-v2", &opp,
553 device_printf(dev, "operating points version is incorrect\n");
558 * Find all CPUs that share the same opp table
561 cnode = OF_parent(node);
562 for (cpu = 0, cnode = OF_child(cnode); cnode > 0; cnode = OF_peer(cnode)) {
563 if (OF_getprop(cnode, "device_type", device_type, sizeof(device_type)) <= 0)
565 if (strcmp(device_type, "cpu") != 0)
567 if (cpu == sc->cpu) {
568 DPRINTF(dev, "Skipping our cpu\n");
569 CPU_SET(cpu, &sc->cpus);
573 DPRINTF(dev, "Testing CPU %d\n", cpu);
575 if (version == OPP_V1)
576 OF_getencprop(cnode, "operating-points", &copp,
578 else if (version == OPP_V2)
579 OF_getencprop(cnode, "operating-points-v2",
580 &copp, sizeof(copp));
582 DPRINTF(dev, "CPU %d is using the same opp as this one (%d)\n",
584 CPU_SET(cpu, &sc->cpus);
589 if (clk_get_freq(sc->clk, &freq) == 0)
590 cpufreq_dt_notify(dev, freq);
592 cpufreq_register(dev);
596 if (CPUFREQ_DT_HAVE_REGULATOR(sc))
597 regulator_release(sc->reg);
601 static device_method_t cpufreq_dt_methods[] = {
602 /* Device interface */
603 DEVMETHOD(device_identify, cpufreq_dt_identify),
604 DEVMETHOD(device_probe, cpufreq_dt_probe),
605 DEVMETHOD(device_attach, cpufreq_dt_attach),
607 /* cpufreq interface */
608 DEVMETHOD(cpufreq_drv_get, cpufreq_dt_get),
609 DEVMETHOD(cpufreq_drv_set, cpufreq_dt_set),
610 DEVMETHOD(cpufreq_drv_type, cpufreq_dt_type),
611 DEVMETHOD(cpufreq_drv_settings, cpufreq_dt_settings),
616 static driver_t cpufreq_dt_driver = {
619 sizeof(struct cpufreq_dt_softc),
622 static devclass_t cpufreq_dt_devclass;
624 DRIVER_MODULE(cpufreq_dt, cpu, cpufreq_dt_driver, cpufreq_dt_devclass, 0, 0);
625 MODULE_VERSION(cpufreq_dt, 1);