2 * Copyright (c) 2011 Justin Hibbits
3 * Copyright (c) 2009 Nathan Whitehorn
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/limits.h>
37 #include <sys/module.h>
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/openfirm.h>
42 #include "cpufreq_if.h"
43 #include "powerpc/powermac/pmuvar.h"
45 struct pmufreq_softc {
52 static void pmufreq_identify(driver_t *driver, device_t parent);
53 static int pmufreq_probe(device_t dev);
54 static int pmufreq_attach(device_t dev);
55 static int pmufreq_settings(device_t dev, struct cf_setting *sets, int *count);
56 static int pmufreq_set(device_t dev, const struct cf_setting *set);
57 static int pmufreq_get(device_t dev, struct cf_setting *set);
58 static int pmufreq_type(device_t dev, int *type);
60 static device_method_t pmufreq_methods[] = {
61 /* Device interface */
62 DEVMETHOD(device_identify, pmufreq_identify),
63 DEVMETHOD(device_probe, pmufreq_probe),
64 DEVMETHOD(device_attach, pmufreq_attach),
66 /* cpufreq interface */
67 DEVMETHOD(cpufreq_drv_set, pmufreq_set),
68 DEVMETHOD(cpufreq_drv_get, pmufreq_get),
69 DEVMETHOD(cpufreq_drv_type, pmufreq_type),
70 DEVMETHOD(cpufreq_drv_settings, pmufreq_settings),
75 static driver_t pmufreq_driver = {
78 sizeof(struct pmufreq_softc)
81 static devclass_t pmufreq_devclass;
82 DRIVER_MODULE(pmufreq, cpu, pmufreq_driver, pmufreq_devclass, 0, 0);
85 pmufreq_identify(driver_t *driver, device_t parent)
90 node = ofw_bus_get_node(parent);
91 if (OF_getprop(node, "min-clock-frequency", &min_freq, sizeof(min_freq)) == -1)
94 /* Make sure we're not being doubly invoked. */
95 if (device_find_child(parent, "pmufreq", -1) != NULL)
99 * We attach a child for every CPU since settings need to
100 * be performed on every CPU in the SMP case.
102 if (BUS_ADD_CHILD(parent, 10, "pmufreq", -1) == NULL)
103 device_printf(parent, "add pmufreq child failed\n");
107 pmufreq_probe(device_t dev)
109 struct pmufreq_softc *sc;
113 if (resource_disabled("pmufreq", 0))
116 sc = device_get_softc(dev);
117 node = ofw_bus_get_node(device_get_parent(dev));
119 * A scalable MPC7455 has min-clock-frequency/max-clock-frequency as OFW
120 * properties of the 'cpu' node.
122 if (OF_getprop(node, "min-clock-frequency", &min_freq, sizeof(min_freq)) == -1)
124 device_set_desc(dev, "PMU-based frequency scaling");
129 pmufreq_attach(device_t dev)
131 struct pmufreq_softc *sc;
134 sc = device_get_softc(dev);
137 node = ofw_bus_get_node(device_get_parent(dev));
138 OF_getprop(node, "min-clock-frequency", &sc->minfreq, sizeof(sc->minfreq));
139 OF_getprop(node, "max-clock-frequency", &sc->maxfreq, sizeof(sc->maxfreq));
140 OF_getprop(node, "rounded-clock-frequency", &sc->curfreq, sizeof(sc->curfreq));
141 sc->minfreq /= 1000000;
142 sc->maxfreq /= 1000000;
143 sc->curfreq /= 1000000;
145 cpufreq_register(dev);
150 pmufreq_settings(device_t dev, struct cf_setting *sets, int *count)
152 struct pmufreq_softc *sc;
154 sc = device_get_softc(dev);
155 if (sets == NULL || count == NULL)
160 /* Return a list of valid settings for this driver. */
161 memset(sets, CPUFREQ_VAL_UNKNOWN, sizeof(*sets) * 2);
163 sets[0].freq = sc->maxfreq; sets[0].dev = dev;
164 sets[1].freq = sc->minfreq; sets[1].dev = dev;
165 /* Set high latency for CPU frequency changes, it's a tedious process. */
166 sets[0].lat = INT_MAX;
167 sets[1].lat = INT_MAX;
174 pmufreq_set(device_t dev, const struct cf_setting *set)
176 struct pmufreq_softc *sc;
177 int error, speed_sel;
182 sc = device_get_softc(dev);
184 if (set->freq == sc->maxfreq)
189 error = pmu_set_speed(speed_sel);
191 sc->curfreq = set->freq;
197 pmufreq_get(device_t dev, struct cf_setting *set)
199 struct pmufreq_softc *sc;
203 sc = device_get_softc(dev);
205 set->freq = sc->curfreq;
212 pmufreq_type(device_t dev, int *type)
218 *type = CPUFREQ_TYPE_ABSOLUTE;