2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Nathan Whitehorn
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include "cpufreq_if.h"
46 static void dfs_identify(driver_t *driver, device_t parent);
47 static int dfs_probe(device_t dev);
48 static int dfs_attach(device_t dev);
49 static int dfs_settings(device_t dev, struct cf_setting *sets, int *count);
50 static int dfs_set(device_t dev, const struct cf_setting *set);
51 static int dfs_get(device_t dev, struct cf_setting *set);
52 static int dfs_type(device_t dev, int *type);
54 static device_method_t dfs_methods[] = {
55 /* Device interface */
56 DEVMETHOD(device_identify, dfs_identify),
57 DEVMETHOD(device_probe, dfs_probe),
58 DEVMETHOD(device_attach, dfs_attach),
60 /* cpufreq interface */
61 DEVMETHOD(cpufreq_drv_set, dfs_set),
62 DEVMETHOD(cpufreq_drv_get, dfs_get),
63 DEVMETHOD(cpufreq_drv_type, dfs_type),
64 DEVMETHOD(cpufreq_drv_settings, dfs_settings),
68 static driver_t dfs_driver = {
71 sizeof(struct dfs_softc)
74 static devclass_t dfs_devclass;
75 DRIVER_MODULE(dfs, cpu, dfs_driver, dfs_devclass, 0, 0);
78 * Bits of the HID1 register to enable DFS. See page 2-24 of "MPC7450
79 * RISC Microprocessor Family Reference Manual", rev. 5.
82 #define HID1_DFS2 (1UL << 22)
83 #define HID1_DFS4 (1UL << 23)
86 dfs_identify(driver_t *driver, device_t parent)
91 /* Check for an MPC 7447A or 7448 CPU */
100 /* Make sure we're not being doubly invoked. */
101 if (device_find_child(parent, "dfs", -1) != NULL)
105 * We attach a child for every CPU since settings need to
106 * be performed on every CPU in the SMP case.
108 if (BUS_ADD_CHILD(parent, 10, "dfs", -1) == NULL)
109 device_printf(parent, "add dfs child failed\n");
113 dfs_probe(device_t dev)
115 if (resource_disabled("dfs", 0))
118 device_set_desc(dev, "Dynamic Frequency Switching");
123 dfs_attach(device_t dev)
125 struct dfs_softc *sc;
128 sc = device_get_softc(dev);
131 vers = mfpvr() >> 16;
133 /* The 7448 supports divide-by-four as well */
137 cpufreq_register(dev);
142 dfs_settings(device_t dev, struct cf_setting *sets, int *count)
144 struct dfs_softc *sc;
147 sc = device_get_softc(dev);
148 states = sc->dfs4 ? 3 : 2;
149 if (sets == NULL || count == NULL)
154 /* Return a list of valid settings for this driver. */
155 memset(sets, CPUFREQ_VAL_UNKNOWN, sizeof(*sets) * states);
157 sets[0].freq = 10000; sets[0].dev = dev;
158 sets[1].freq = 5000; sets[1].dev = dev;
169 dfs_set(device_t dev, const struct cf_setting *set)
176 hid1 = mfspr(SPR_HID1);
177 hid1 &= ~(HID1_DFS2 | HID1_DFS4);
179 if (set->freq == 5000)
181 else if (set->freq == 2500)
185 * Now set the HID1 register with new values. Calling sequence
186 * taken from page 2-26 of the MPC7450 family CPU manual.
190 mtspr(SPR_HID1, hid1);
191 powerpc_sync(); isync();
197 dfs_get(device_t dev, struct cf_setting *set)
199 struct dfs_softc *sc;
204 sc = device_get_softc(dev);
206 memset(set, CPUFREQ_VAL_UNKNOWN, sizeof(*set));
208 hid1 = mfspr(SPR_HID1);
211 if (hid1 & HID1_DFS2)
213 else if (sc->dfs4 && (hid1 & HID1_DFS4))
222 dfs_type(device_t dev, int *type)
228 *type = CPUFREQ_TYPE_RELATIVE;