xref: /netbsd-src/sys/dev/pci/voyager/pwmclock.c (revision 482eef70502290f7cbd2cb9a24a4f41e6bacd98d)
1*482eef70Srin /*	$NetBSD: pwmclock.c,v 1.12 2020/05/29 12:30:41 rin Exp $	*/
2e9495f5cSmacallan 
3e9495f5cSmacallan /*
4e9495f5cSmacallan  * Copyright (c) 2011 Michael Lorenz
5e9495f5cSmacallan  * All rights reserved.
6e9495f5cSmacallan  *
7e9495f5cSmacallan  * Redistribution and use in source and binary forms, with or without
8e9495f5cSmacallan  * modification, are permitted provided that the following conditions
9e9495f5cSmacallan  * are met:
10e9495f5cSmacallan  * 1. Redistributions of source code must retain the above copyright
11e9495f5cSmacallan  *    notice, this list of conditions and the following disclaimer.
12e9495f5cSmacallan  * 2. Redistributions in binary form must reproduce the above copyright
13e9495f5cSmacallan  *    notice, this list of conditions and the following disclaimer in the
14e9495f5cSmacallan  *    documentation and/or other materials provided with the distribution.
15e9495f5cSmacallan  *
16e9495f5cSmacallan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17e9495f5cSmacallan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18e9495f5cSmacallan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19e9495f5cSmacallan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20e9495f5cSmacallan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21e9495f5cSmacallan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22e9495f5cSmacallan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23e9495f5cSmacallan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24e9495f5cSmacallan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25e9495f5cSmacallan  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26e9495f5cSmacallan  */
27e9495f5cSmacallan 
28e9495f5cSmacallan #include <sys/cdefs.h>
29*482eef70Srin __KERNEL_RCSID(0, "$NetBSD: pwmclock.c,v 1.12 2020/05/29 12:30:41 rin Exp $");
30e9495f5cSmacallan 
31e9495f5cSmacallan #include <sys/param.h>
32e9495f5cSmacallan #include <sys/systm.h>
33e9495f5cSmacallan #include <sys/kernel.h>
34e9495f5cSmacallan #include <sys/device.h>
35e9495f5cSmacallan #include <sys/cpu.h>
36e9495f5cSmacallan #include <sys/timetc.h>
37e9495f5cSmacallan #include <sys/sysctl.h>
38e9495f5cSmacallan 
39e9495f5cSmacallan #include <dev/pci/voyagervar.h>
40e9495f5cSmacallan #include <dev/ic/sm502reg.h>
41e9495f5cSmacallan 
42e9495f5cSmacallan #include <mips/mips3_clock.h>
43e9495f5cSmacallan #include <mips/locore.h>
44e9495f5cSmacallan #include <mips/bonito/bonitoreg.h>
45e9495f5cSmacallan #include <mips/bonito/bonitovar.h>
46e9495f5cSmacallan 
472f868507Smacallan #include "opt_pwmclock.h"
482f868507Smacallan 
49e9495f5cSmacallan #ifdef PWMCLOCK_DEBUG
50e9495f5cSmacallan #define DPRINTF aprint_error
51e9495f5cSmacallan #else
52e9495f5cSmacallan #define DPRINTF while (0) printf
53e9495f5cSmacallan #endif
54e9495f5cSmacallan 
55e9495f5cSmacallan int pwmclock_intr(void *);
56e9495f5cSmacallan 
57e9495f5cSmacallan struct pwmclock_softc {
58e9495f5cSmacallan 	device_t sc_dev;
59e9495f5cSmacallan 	bus_space_tag_t sc_memt;
60e9495f5cSmacallan 	bus_space_handle_t sc_regh;
61e9495f5cSmacallan 	uint32_t sc_reg, sc_last;
62e9495f5cSmacallan 	uint32_t sc_scale[8];
63e9495f5cSmacallan 	uint32_t sc_count;	/* should probably be 64 bit */
64e9495f5cSmacallan 	int sc_step;
65e9495f5cSmacallan 	int sc_step_wanted;
66b463d521Smacallan 	void *sc_shutdown_cookie;
67e9495f5cSmacallan };
68e9495f5cSmacallan 
69e9495f5cSmacallan static int	pwmclock_match(device_t, cfdata_t, void *);
70e9495f5cSmacallan static void	pwmclock_attach(device_t, device_t, void *);
71e9495f5cSmacallan 
72e9495f5cSmacallan CFATTACH_DECL_NEW(pwmclock, sizeof(struct pwmclock_softc),
73e9495f5cSmacallan     pwmclock_match, pwmclock_attach, NULL, NULL);
74e9495f5cSmacallan 
75e9495f5cSmacallan static void pwmclock_start(void);
76e9495f5cSmacallan static u_int get_pwmclock_timecount(struct timecounter *);
77e9495f5cSmacallan 
78e9495f5cSmacallan struct pwmclock_softc *pwmclock;
79e9495f5cSmacallan extern void (*initclocks_ptr)(void);
80e9495f5cSmacallan 
81e9495f5cSmacallan /* 0, 1/4, 3/8, 1/2, 5/8, 3/4, 7/8, 1 */
82e9495f5cSmacallan static int scale_m[] = {1, 1, 3, 1, 5, 3, 7, 1};
83e9495f5cSmacallan static int scale_d[] = {0, 4, 8, 2, 8, 4, 8, 1};
84e9495f5cSmacallan 
85e9495f5cSmacallan #define scale(x, f) (x * scale_d[f] / scale_m[f])
86e9495f5cSmacallan 
87e9495f5cSmacallan void pwmclock_set_speed(struct pwmclock_softc *, int);
88e9495f5cSmacallan static int  pwmclock_cpuspeed_temp(SYSCTLFN_ARGS);
89e9495f5cSmacallan static int  pwmclock_cpuspeed_cur(SYSCTLFN_ARGS);
90e9495f5cSmacallan static int  pwmclock_cpuspeed_available(SYSCTLFN_ARGS);
91e9495f5cSmacallan 
92b463d521Smacallan static void pwmclock_shutdown(void *);
93b463d521Smacallan 
94e9495f5cSmacallan static struct timecounter pwmclock_timecounter = {
95*482eef70Srin 	.tc_get_timecount = get_pwmclock_timecount,
96*482eef70Srin 	.tc_counter_mask = 0xffffffff,
97*482eef70Srin 	.tc_name = "pwm",
98*482eef70Srin 	.tc_quality = 100,
99e9495f5cSmacallan };
100e9495f5cSmacallan 
101e9495f5cSmacallan static int
pwmclock_match(device_t parent,cfdata_t match,void * aux)102e9495f5cSmacallan pwmclock_match(device_t parent, cfdata_t match, void *aux)
103e9495f5cSmacallan {
104e9495f5cSmacallan 	struct voyager_attach_args *vaa = (struct voyager_attach_args *)aux;
105e9495f5cSmacallan 
106e9495f5cSmacallan 	if (strcmp(vaa->vaa_name, "pwmclock") == 0) return 100;
107e9495f5cSmacallan 	return 0;
108e9495f5cSmacallan }
109e9495f5cSmacallan 
110e9495f5cSmacallan static uint32_t
pwmclock_wait_edge(struct pwmclock_softc * sc)111e9495f5cSmacallan pwmclock_wait_edge(struct pwmclock_softc *sc)
112e9495f5cSmacallan {
113e9495f5cSmacallan 	/* clear interrupt */
114e9495f5cSmacallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PWM1, sc->sc_reg);
115111cf9c7Smacallan 	while ((bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_PWM1) &
116111cf9c7Smacallan 	    SM502_PWM_INTR_PENDING) == 0);
117e9495f5cSmacallan 	return mips3_cp0_count_read();
118e9495f5cSmacallan }
119e9495f5cSmacallan 
120e9495f5cSmacallan static void
pwmclock_attach(device_t parent,device_t self,void * aux)121e9495f5cSmacallan pwmclock_attach(device_t parent, device_t self, void *aux)
122e9495f5cSmacallan {
123e9495f5cSmacallan 	struct pwmclock_softc *sc = device_private(self);
124e9495f5cSmacallan 	struct voyager_attach_args *vaa = aux;
125e9495f5cSmacallan 	const struct sysctlnode *sysctl_node, *me, *freq;
126e9495f5cSmacallan 	uint32_t reg, last, curr, diff, acc;
127e9495f5cSmacallan 	int i, clk;
128e9495f5cSmacallan 
129e9495f5cSmacallan 	sc->sc_dev = self;
130e9495f5cSmacallan 	sc->sc_memt = vaa->vaa_tag;
131e9495f5cSmacallan 	sc->sc_regh = vaa->vaa_regh;
132e9495f5cSmacallan 
133e9495f5cSmacallan 	aprint_normal("\n");
134e9495f5cSmacallan 
1354e8b65a1Sskrll 	/* NULL here gets us the clockframe */
1364e8b65a1Sskrll 	voyager_establish_intr(parent, 22, pwmclock_intr, NULL);
137e9495f5cSmacallan 	reg = voyager_set_pwm(100, 100); /* 100Hz, 10% duty cycle */
138111cf9c7Smacallan 	reg |= SM502_PWM_ENABLE | SM502_PWM_ENABLE_INTR |
139111cf9c7Smacallan 	       SM502_PWM_INTR_PENDING;
140e9495f5cSmacallan 	sc->sc_reg = reg;
141e9495f5cSmacallan 	pwmclock = sc;
142e9495f5cSmacallan 	initclocks_ptr = pwmclock_start;
143e9495f5cSmacallan 
144b463d521Smacallan 	/*
145b463d521Smacallan 	 * Establish a hook so on shutdown we can set the CPU clock back to
146b463d521Smacallan 	 * full speed. This is necessary because PMON doesn't change the
147b463d521Smacallan 	 * clock scale register on a warm boot, the MIPS clock code gets
148b463d521Smacallan 	 * confused if we're too slow and the loongson-specific bits run
149b463d521Smacallan 	 * too late in the boot process
150b463d521Smacallan 	 */
151b463d521Smacallan 	sc->sc_shutdown_cookie = shutdownhook_establish(pwmclock_shutdown, sc);
152b463d521Smacallan 
153e9495f5cSmacallan 	/* ok, let's see how far the cycle counter gets between interrupts */
1542f868507Smacallan 	DPRINTF("calibrating CPU timer...\n");
155e9495f5cSmacallan 	for (clk = 1; clk < 8; clk++) {
1567e36e71bSmacallan 
157111cf9c7Smacallan 		REGVAL(LS2F_CHIPCFG0) =
158111cf9c7Smacallan 		    (REGVAL(LS2F_CHIPCFG0) & ~LS2FCFG_FREQSCALE_MASK) | clk;
159111cf9c7Smacallan 		bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PWM1,
160111cf9c7Smacallan 		    sc->sc_reg);
161e9495f5cSmacallan 		acc = 0;
162e9495f5cSmacallan 		last = pwmclock_wait_edge(sc);
163e9495f5cSmacallan 		for (i = 0; i < 16; i++) {
164e9495f5cSmacallan 			curr = pwmclock_wait_edge(sc);
165e9495f5cSmacallan 			diff = curr - last;
166e9495f5cSmacallan 			acc += diff;
167e9495f5cSmacallan 			last = curr;
168e9495f5cSmacallan 		}
169e9495f5cSmacallan 		sc->sc_scale[clk] = (acc >> 4) / 5000;
170e9495f5cSmacallan 	}
1712f868507Smacallan #ifdef PWMCLOCK_DEBUG
172e9495f5cSmacallan 	for (clk = 1; clk < 8; clk++) {
173111cf9c7Smacallan 		aprint_normal_dev(sc->sc_dev, "%d/8: %d\n", clk + 1,
174111cf9c7Smacallan 		    sc->sc_scale[clk]);
175e9495f5cSmacallan 	}
1762f868507Smacallan #endif
177e9495f5cSmacallan 	sc->sc_step = 7;
178e9495f5cSmacallan 	sc->sc_step_wanted = 7;
179e9495f5cSmacallan 
180e9495f5cSmacallan 	/* now setup sysctl */
181e9495f5cSmacallan 	if (sysctl_createv(NULL, 0, NULL,
182e9495f5cSmacallan 	    &me,
183e9495f5cSmacallan 	    CTLFLAG_READWRITE, CTLTYPE_NODE, "loongson", NULL, NULL,
184e9495f5cSmacallan 	    0, NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL) != 0)
185111cf9c7Smacallan 		aprint_error_dev(sc->sc_dev,
186111cf9c7Smacallan 		    "couldn't create 'loongson' node\n");
187e9495f5cSmacallan 
188e9495f5cSmacallan 	if (sysctl_createv(NULL, 0, NULL,
189e9495f5cSmacallan 	    &freq,
190111cf9c7Smacallan 	    CTLFLAG_READWRITE, CTLTYPE_NODE, "frequency", NULL, NULL, 0, NULL,
191111cf9c7Smacallan 	    0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL) != 0)
192111cf9c7Smacallan 		aprint_error_dev(sc->sc_dev,
193111cf9c7Smacallan 		    "couldn't create 'frequency' node\n");
194e9495f5cSmacallan 
195e9495f5cSmacallan 	if (sysctl_createv(NULL, 0, NULL,
196e9495f5cSmacallan 	    &sysctl_node,
197e9495f5cSmacallan 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
198e9495f5cSmacallan 	    CTLTYPE_INT, "target", "CPU speed", pwmclock_cpuspeed_temp,
199e21a34c2Sdsl 	    0, (void *)sc, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
200e9495f5cSmacallan 	    CTL_CREATE, CTL_EOL) == 0) {
201e9495f5cSmacallan 	} else
202111cf9c7Smacallan 		aprint_error_dev(sc->sc_dev,
203111cf9c7Smacallan 		    "couldn't create 'target' node\n");
204e9495f5cSmacallan 
205e9495f5cSmacallan 	if (sysctl_createv(NULL, 0, NULL,
206e9495f5cSmacallan 	    &sysctl_node,
207e9495f5cSmacallan 	    CTLFLAG_READWRITE,
208e9495f5cSmacallan 	    CTLTYPE_INT, "current", NULL, pwmclock_cpuspeed_cur,
209e21a34c2Sdsl 	    1, (void *)sc, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
210e9495f5cSmacallan 	    CTL_CREATE, CTL_EOL) == 0) {
211e9495f5cSmacallan 	} else
212111cf9c7Smacallan 		aprint_error_dev(sc->sc_dev,
213111cf9c7Smacallan 		    "couldn't create 'current' node\n");
214e9495f5cSmacallan 
215e9495f5cSmacallan 	if (sysctl_createv(NULL, 0, NULL,
216e9495f5cSmacallan 	    &sysctl_node,
217e9495f5cSmacallan 	    CTLFLAG_READWRITE,
218e9495f5cSmacallan 	    CTLTYPE_STRING, "available", NULL, pwmclock_cpuspeed_available,
219e21a34c2Sdsl 	    2, (void *)sc, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
220e9495f5cSmacallan 	    CTL_CREATE, CTL_EOL) == 0) {
221e9495f5cSmacallan 	} else
222111cf9c7Smacallan 		aprint_error_dev(sc->sc_dev,
223111cf9c7Smacallan 		    "couldn't create 'available' node\n");
224e9495f5cSmacallan }
225e9495f5cSmacallan 
226b463d521Smacallan static void
pwmclock_shutdown(void * cookie)227b463d521Smacallan pwmclock_shutdown(void *cookie)
228b463d521Smacallan {
229b463d521Smacallan 	struct pwmclock_softc *sc = cookie;
230b463d521Smacallan 
231b463d521Smacallan 	/* just in case the interrupt handler runs again after this */
232b463d521Smacallan 	sc->sc_step_wanted = 7;
233b463d521Smacallan 	/* set the clock to full speed */
234111cf9c7Smacallan 	REGVAL(LS2F_CHIPCFG0) =
235111cf9c7Smacallan 	    (REGVAL(LS2F_CHIPCFG0) & ~LS2FCFG_FREQSCALE_MASK) | 7;
236b463d521Smacallan }
237b463d521Smacallan 
238e9495f5cSmacallan void
pwmclock_set_speed(struct pwmclock_softc * sc,int speed)239e9495f5cSmacallan pwmclock_set_speed(struct pwmclock_softc *sc, int speed)
240e9495f5cSmacallan {
241e9495f5cSmacallan 
242e9495f5cSmacallan 	if ((speed < 1) || (speed > 7))
243e9495f5cSmacallan 		return;
244e9495f5cSmacallan 	sc->sc_step_wanted = speed;
245e9495f5cSmacallan 	DPRINTF("%s: %d\n", __func__, speed);
246e9495f5cSmacallan }
247e9495f5cSmacallan 
248e9495f5cSmacallan /*
249e9495f5cSmacallan  * the PWM interrupt handler
250e9495f5cSmacallan  * we don't have a CPU clock independent, high resolution counter so we're
251e9495f5cSmacallan  * stuck with a PWM that can't count and a CP0 counter that slows down or
252e9495f5cSmacallan  * speeds up with the actual CPU speed. In order to still get halfway
253e9495f5cSmacallan  * accurate time we do the following:
254e9495f5cSmacallan  * - only change CPU speed in the timer interrupt
255e9495f5cSmacallan  * - each timer interrupt we measure how many CP0 cycles passed since last
256e9495f5cSmacallan  *   time, adjust for CPU speed since we can be sure it didn't change, use
257e9495f5cSmacallan  *   that to update a separate counter
258e9495f5cSmacallan  * - when reading the time counter we take the number of CP0 ticks since
259e9495f5cSmacallan  *   the last timer interrupt, scale it to CPU clock, return that plus the
260e9495f5cSmacallan  *   interrupt updated counter mentioned above to get something close to
261e9495f5cSmacallan  *   CP0 running at full speed
262e9495f5cSmacallan  * - when changing CPU speed do it as close to taking the time from CP0 as
263e9495f5cSmacallan  *   possible to keep the period of time we spend with CP0 running at the
264e9495f5cSmacallan  *   wrong frequency as short as possible - hopefully short enough to stay
265e9495f5cSmacallan  *   insignificant compared to other noise since switching speeds isn't
266e9495f5cSmacallan  *   going to happen all that often
267e9495f5cSmacallan  */
268e9495f5cSmacallan 
269e9495f5cSmacallan int
pwmclock_intr(void * cookie)270e9495f5cSmacallan pwmclock_intr(void *cookie)
271e9495f5cSmacallan {
2724e8b65a1Sskrll 	struct clockframe *cf = cookie;
2734e8b65a1Sskrll 	struct pwmclock_softc *sc = pwmclock;
274e9495f5cSmacallan 	uint32_t reg, now, diff;
275e9495f5cSmacallan 
276e9495f5cSmacallan 	/* is it us? */
277e9495f5cSmacallan 	reg = bus_space_read_4(sc->sc_memt, sc->sc_regh, SM502_PWM1);
278e9495f5cSmacallan 	if ((reg & SM502_PWM_INTR_PENDING) == 0)
279e9495f5cSmacallan 		return 0;
280e9495f5cSmacallan 
281e9495f5cSmacallan 	/* yes, it's us, so clear the interrupt */
282e9495f5cSmacallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PWM1, sc->sc_reg);
283e9495f5cSmacallan 
284e9495f5cSmacallan 	/*
285e9495f5cSmacallan 	 * this looks kinda funny but what we want here is this:
286e9495f5cSmacallan 	 * - reading the counter and changing the CPU clock should be as
287e9495f5cSmacallan 	 *   close together as possible in order to remain halfway accurate
288e9495f5cSmacallan 	 * - we need to use the previous sc_step in order to scale the
289e9495f5cSmacallan 	 *   interval passed since the last clock interrupt correctly, so
290e9495f5cSmacallan 	 *   we only change sc_step after doing that
291e9495f5cSmacallan 	 */
292e9495f5cSmacallan 	if (sc->sc_step_wanted != sc->sc_step) {
2937e36e71bSmacallan 
294e9495f5cSmacallan 		REGVAL(LS2F_CHIPCFG0) =
295e9495f5cSmacallan 		    (REGVAL(LS2F_CHIPCFG0) & ~LS2FCFG_FREQSCALE_MASK) |
296e9495f5cSmacallan 		     sc->sc_step_wanted;
297e9495f5cSmacallan 	}
298e9495f5cSmacallan 
299e9495f5cSmacallan 	now = mips3_cp0_count_read();
300e9495f5cSmacallan 	diff = now - sc->sc_last;
301e9495f5cSmacallan 	sc->sc_count += scale(diff, sc->sc_step);
302e9495f5cSmacallan 	sc->sc_last = now;
303e9495f5cSmacallan 	if (sc->sc_step_wanted != sc->sc_step) {
304e9495f5cSmacallan 		sc->sc_step = sc->sc_step_wanted;
305e9495f5cSmacallan 	}
306f215d8ebSchristos 
3074e8b65a1Sskrll 	hardclock(cf);
308e9495f5cSmacallan 
309e9495f5cSmacallan 	return 1;
310e9495f5cSmacallan }
311e9495f5cSmacallan 
312e9495f5cSmacallan static void
pwmclock_start(void)313b148f574Smacallan pwmclock_start(void)
314e9495f5cSmacallan {
315e9495f5cSmacallan 	struct pwmclock_softc *sc = pwmclock;
316e9495f5cSmacallan 	sc->sc_count = 0;
317e9495f5cSmacallan 	sc->sc_last = mips3_cp0_count_read();
318e9495f5cSmacallan 	pwmclock_timecounter.tc_frequency = curcpu()->ci_cpu_freq / 2;
319e9495f5cSmacallan 	tc_init(&pwmclock_timecounter);
320e9495f5cSmacallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, SM502_PWM1, sc->sc_reg);
321e9495f5cSmacallan }
322e9495f5cSmacallan 
323e9495f5cSmacallan static u_int
get_pwmclock_timecount(struct timecounter * tc)324e9495f5cSmacallan get_pwmclock_timecount(struct timecounter *tc)
325e9495f5cSmacallan {
326e9495f5cSmacallan 	struct pwmclock_softc *sc = pwmclock;
327e9495f5cSmacallan 	uint32_t now, diff;
328e9495f5cSmacallan 
329e9495f5cSmacallan 	now = mips3_cp0_count_read();
330e9495f5cSmacallan 	diff = now - sc->sc_last;
331e9495f5cSmacallan 	return sc->sc_count + scale(diff, sc->sc_step);
332e9495f5cSmacallan }
333e9495f5cSmacallan 
334e9495f5cSmacallan static int
pwmclock_cpuspeed_temp(SYSCTLFN_ARGS)335e9495f5cSmacallan pwmclock_cpuspeed_temp(SYSCTLFN_ARGS)
336e9495f5cSmacallan {
337e9495f5cSmacallan 	struct sysctlnode node = *rnode;
338e9495f5cSmacallan 	struct pwmclock_softc *sc = node.sysctl_data;
339e9495f5cSmacallan 	int mhz, i;
340e9495f5cSmacallan 
341e9495f5cSmacallan 	mhz = sc->sc_scale[sc->sc_step_wanted];
342e9495f5cSmacallan 
343e9495f5cSmacallan 	node.sysctl_data = &mhz;
344e9495f5cSmacallan 	if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
345e9495f5cSmacallan 		int new_reg;
346e9495f5cSmacallan 
347e9495f5cSmacallan 		new_reg = *(int *)node.sysctl_data;
348e9495f5cSmacallan 		i = 1;
349e9495f5cSmacallan 		while ((i < 8) && (sc->sc_scale[i] != new_reg))
350e9495f5cSmacallan 			i++;
351e9495f5cSmacallan 		if (i > 7)
352e9495f5cSmacallan 			return EINVAL;
353e9495f5cSmacallan 		pwmclock_set_speed(sc, i);
354e9495f5cSmacallan 		return 0;
355e9495f5cSmacallan 	}
356e9495f5cSmacallan 	return EINVAL;
357e9495f5cSmacallan }
358e9495f5cSmacallan 
359e9495f5cSmacallan static int
pwmclock_cpuspeed_cur(SYSCTLFN_ARGS)360e9495f5cSmacallan pwmclock_cpuspeed_cur(SYSCTLFN_ARGS)
361e9495f5cSmacallan {
362e9495f5cSmacallan 	struct sysctlnode node = *rnode;
363e9495f5cSmacallan 	struct pwmclock_softc *sc = node.sysctl_data;
364e9495f5cSmacallan 	int mhz;
365e9495f5cSmacallan 
366e9495f5cSmacallan 	mhz = sc->sc_scale[sc->sc_step];
367e9495f5cSmacallan 	node.sysctl_data = &mhz;
368e9495f5cSmacallan 	return sysctl_lookup(SYSCTLFN_CALL(&node));
369e9495f5cSmacallan }
370e9495f5cSmacallan 
371e9495f5cSmacallan static int
pwmclock_cpuspeed_available(SYSCTLFN_ARGS)372e9495f5cSmacallan pwmclock_cpuspeed_available(SYSCTLFN_ARGS)
373e9495f5cSmacallan {
374e9495f5cSmacallan 	struct sysctlnode node = *rnode;
375e9495f5cSmacallan 	struct pwmclock_softc *sc = node.sysctl_data;
376e9495f5cSmacallan 	char buf[128];
377e9495f5cSmacallan 
378e9495f5cSmacallan 	snprintf(buf, 128, "%d %d %d %d %d %d %d", sc->sc_scale[1],
379e9495f5cSmacallan 	    sc->sc_scale[2], sc->sc_scale[3], sc->sc_scale[4],
380e9495f5cSmacallan 	    sc->sc_scale[5], sc->sc_scale[6], sc->sc_scale[7]);
381e9495f5cSmacallan 	node.sysctl_data = buf;
382e9495f5cSmacallan 	return(sysctl_lookup(SYSCTLFN_CALL(&node)));
383e9495f5cSmacallan }
384e9495f5cSmacallan 
385e9495f5cSmacallan SYSCTL_SETUP(sysctl_ams_setup, "sysctl obio subtree setup")
386e9495f5cSmacallan {
387e9495f5cSmacallan 
388e9495f5cSmacallan 	sysctl_createv(NULL, 0, NULL, NULL,
389e9495f5cSmacallan 		       CTLFLAG_PERMANENT,
390e9495f5cSmacallan 		       CTLTYPE_NODE, "machdep", NULL,
391e9495f5cSmacallan 		       NULL, 0, NULL, 0,
392e9495f5cSmacallan 		       CTL_MACHDEP, CTL_EOL);
393e9495f5cSmacallan }
394