Home | History | Annotate | Line # | Download | only in dev
obio.c revision 1.49
      1 /*	$NetBSD: obio.c,v 1.49 2021/04/24 23:36:41 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 1998	Internet Research Institute, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by
     18  *	Internet Research Institute, Inc.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.49 2021/04/24 23:36:41 thorpej Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/device.h>
     41 #include <sys/sysctl.h>
     42 
     43 #include <dev/pci/pcivar.h>
     44 #include <dev/pci/pcidevs.h>
     45 
     46 #include <dev/ofw/openfirm.h>
     47 
     48 #include <machine/autoconf.h>
     49 
     50 #include <macppc/dev/obiovar.h>
     51 
     52 #include <powerpc/cpu.h>
     53 #include <sys/cpufreq.h>
     54 
     55 #include "opt_obio.h"
     56 
     57 #ifdef OBIO_DEBUG
     58 # define DPRINTF printf
     59 #else
     60 # define DPRINTF while (0) printf
     61 #endif
     62 
     63 static void obio_attach(device_t, device_t, void *);
     64 static int obio_match(device_t, cfdata_t, void *);
     65 static int obio_print(void *, const char *);
     66 
     67 struct obio_softc {
     68 	device_t sc_dev;
     69 	bus_space_tag_t sc_tag;
     70 	bus_space_handle_t sc_bh;
     71 	int sc_node;
     72 #ifdef OBIO_SPEED_CONTROL
     73 	int sc_voltage;
     74 	int sc_busspeed;
     75 	int sc_spd_hi, sc_spd_lo;
     76 	struct cpufreq sc_cf;
     77 #endif
     78 };
     79 
     80 static struct obio_softc *obio0 = NULL;
     81 
     82 #ifdef OBIO_SPEED_CONTROL
     83 static void obio_setup_gpios(struct obio_softc *, int);
     84 static void obio_set_cpu_speed(struct obio_softc *, int);
     85 static int  obio_get_cpu_speed(struct obio_softc *);
     86 static int  sysctl_cpuspeed_temp(SYSCTLFN_ARGS);
     87 static int  sysctl_cpuspeed_cur(SYSCTLFN_ARGS);
     88 static int  sysctl_cpuspeed_available(SYSCTLFN_ARGS);
     89 static void obio_get_freq(void *, void *);
     90 static void obio_set_freq(void *, void *);
     91 static const char *keylargo[] = {"Keylargo",
     92 				 "AAPL,Keylargo",
     93 				 NULL};
     94 
     95 #endif
     96 
     97 CFATTACH_DECL_NEW(obio, sizeof(struct obio_softc),
     98     obio_match, obio_attach, NULL, NULL);
     99 
    100 int
    101 obio_match(device_t parent, cfdata_t cf, void *aux)
    102 {
    103 	struct pci_attach_args *pa = aux;
    104 
    105 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE)
    106 		switch (PCI_PRODUCT(pa->pa_id)) {
    107 		case PCI_PRODUCT_APPLE_GC:
    108 		case PCI_PRODUCT_APPLE_OHARE:
    109 		case PCI_PRODUCT_APPLE_HEATHROW:
    110 		case PCI_PRODUCT_APPLE_PADDINGTON:
    111 		case PCI_PRODUCT_APPLE_KEYLARGO:
    112 		case PCI_PRODUCT_APPLE_PANGEA_MACIO:
    113 		case PCI_PRODUCT_APPLE_INTREPID:
    114 		case PCI_PRODUCT_APPLE_K2:
    115 		case PCI_PRODUCT_APPLE_SHASTA:
    116 			return 1;
    117 		}
    118 
    119 	return 0;
    120 }
    121 
    122 /*
    123  * Attach all the sub-devices we can find
    124  */
    125 void
    126 obio_attach(device_t parent, device_t self, void *aux)
    127 {
    128 	struct obio_softc *sc = device_private(self);
    129 	struct pci_attach_args *pa = aux;
    130 	struct confargs ca;
    131 	bus_space_handle_t bsh;
    132 	int node, child, namelen, error;
    133 	u_int reg[20];
    134 	int intr[6], parent_intr = 0, parent_nintr = 0;
    135 	int map_size = 0x1000;
    136 	char name[32];
    137 	char compat[32];
    138 
    139 	sc->sc_dev = self;
    140 #ifdef OBIO_SPEED_CONTROL
    141 	sc->sc_voltage = -1;
    142 	sc->sc_busspeed = -1;
    143 	sc->sc_spd_lo = 600;
    144 	sc->sc_spd_hi = 800;
    145 #endif
    146 
    147 	switch (PCI_PRODUCT(pa->pa_id)) {
    148 
    149 	case PCI_PRODUCT_APPLE_GC:
    150 	case PCI_PRODUCT_APPLE_OHARE:
    151 	case PCI_PRODUCT_APPLE_HEATHROW:
    152 	case PCI_PRODUCT_APPLE_PADDINGTON:
    153 	case PCI_PRODUCT_APPLE_KEYLARGO:
    154 	case PCI_PRODUCT_APPLE_PANGEA_MACIO:
    155 	case PCI_PRODUCT_APPLE_INTREPID:
    156 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    157 		if (node == -1)
    158 			node = OF_finddevice("mac-io");
    159 		if (node == -1)
    160 			node = OF_finddevice("/pci/mac-io");
    161 		break;
    162 	case PCI_PRODUCT_APPLE_K2:
    163 	case PCI_PRODUCT_APPLE_SHASTA:
    164 		node = OF_finddevice("mac-io");
    165 		map_size = 0x10000;
    166 		break;
    167 
    168 	default:
    169 		node = -1;
    170 		break;
    171 	}
    172 	if (node == -1)
    173 		panic("macio not found or unknown");
    174 
    175 	sc->sc_node = node;
    176 
    177 #if defined (PMAC_G5)
    178 	if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 20)
    179 	{
    180 		return;
    181 	}
    182 #else
    183 	if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 12)
    184 		return;
    185 #endif /* PMAC_G5 */
    186 
    187 	/*
    188 	 * XXX
    189 	 * This relies on the primary obio always attaching first which is
    190 	 * true on the PowerBook 3400c and similar machines but may or may
    191 	 * not work on others. We can't rely on the node name since Apple
    192 	 * didn't follow anything remotely resembling a consistent naming
    193 	 * scheme.
    194 	 */
    195 	if (obio0 == NULL)
    196 		obio0 = sc;
    197 
    198 	ca.ca_baseaddr = reg[2];
    199 	ca.ca_tag = pa->pa_memt;
    200 	sc->sc_tag = pa->pa_memt;
    201 	error = bus_space_map (pa->pa_memt, ca.ca_baseaddr, map_size, 0, &bsh);
    202 	if (error)
    203 		panic(": failed to map mac-io %#x", ca.ca_baseaddr);
    204 	sc->sc_bh = bsh;
    205 
    206 	printf(": addr 0x%x\n", ca.ca_baseaddr);
    207 
    208 	/* Enable internal modem (KeyLargo) */
    209 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_KEYLARGO) {
    210 		aprint_normal("%s: enabling KeyLargo internal modem\n",
    211 		    device_xname(self));
    212 		bus_space_write_4(ca.ca_tag, bsh, 0x40,
    213 		    bus_space_read_4(ca.ca_tag, bsh, 0x40) & ~(1<<25));
    214 	}
    215 
    216 	/* Enable internal modem (Pangea) */
    217 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PANGEA_MACIO) {
    218 		/* set reset */
    219 		bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x03, 0x04);
    220 		/* power modem on */
    221 		bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x02, 0x04);
    222 		/* unset reset */
    223 		bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x03, 0x05);
    224 	}
    225 
    226 	/* Gatwick and Paddington use same product ID */
    227 	namelen = OF_getprop(node, "compatible", compat, sizeof(compat));
    228 
    229 	if (strcmp(compat, "gatwick") == 0) {
    230 		parent_nintr = OF_getprop(node, "AAPL,interrupts", intr,
    231 					sizeof(intr));
    232 		parent_intr = intr[0];
    233 	} else {
    234   		/* Enable CD and microphone sound input. */
    235 		if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PADDINGTON)
    236 			bus_space_write_1(ca.ca_tag, bsh, 0x37, 0x03);
    237 	}
    238 
    239 	for (child = OF_child(node); child; child = OF_peer(child)) {
    240 		namelen = OF_getprop(child, "name", name, sizeof(name));
    241 		if (namelen < 0)
    242 			continue;
    243 		if (namelen >= sizeof(name))
    244 			continue;
    245 
    246 #ifdef OBIO_SPEED_CONTROL
    247 		if (strcmp(name, "gpio") == 0) {
    248 
    249 			obio_setup_gpios(sc, child);
    250 			continue;
    251 		}
    252 #endif
    253 
    254 		name[namelen] = 0;
    255 		ca.ca_name = name;
    256 		ca.ca_node = child;
    257 		ca.ca_tag = pa->pa_memt;
    258 
    259 		ca.ca_nreg = OF_getprop(child, "reg", reg, sizeof(reg));
    260 
    261 		if (strcmp(compat, "gatwick") != 0) {
    262 			ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
    263 					sizeof(intr));
    264 			if (ca.ca_nintr == -1)
    265 				ca.ca_nintr = OF_getprop(child, "interrupts", intr,
    266 						sizeof(intr));
    267 		} else {
    268 			intr[0] = parent_intr;
    269 			ca.ca_nintr = parent_nintr;
    270 		}
    271 		ca.ca_reg = reg;
    272 		ca.ca_intr = intr;
    273 
    274 		config_found(self, &ca, obio_print,
    275 		    CFARG_DEVHANDLE, devhandle_from_of(child),
    276 		    CFARG_EOL);
    277 	}
    278 }
    279 
    280 static const char * const skiplist[] = {
    281 	"interrupt-controller",
    282 	"gpio",
    283 	"escc-legacy",
    284 	"timer",
    285 	"i2c",
    286 	"power-mgt",
    287 	"escc",
    288 	"battery",
    289 	"backlight"
    290 
    291 };
    292 
    293 #define N_LIST (sizeof(skiplist) / sizeof(skiplist[0]))
    294 
    295 int
    296 obio_print(void *aux, const char *obio)
    297 {
    298 	struct confargs *ca = aux;
    299 	int i;
    300 
    301 	for (i = 0; i < N_LIST; i++)
    302 		if (strcmp(ca->ca_name, skiplist[i]) == 0)
    303 			return QUIET;
    304 
    305 	if (obio)
    306 		aprint_normal("%s at %s", ca->ca_name, obio);
    307 
    308 	if (ca->ca_nreg > 0)
    309 		aprint_normal(" offset 0x%x", ca->ca_reg[0]);
    310 
    311 	return UNCONF;
    312 }
    313 
    314 void obio_write_4(int offset, uint32_t value)
    315 {
    316 	if (obio0 == NULL)
    317 		return;
    318 	bus_space_write_4(obio0->sc_tag, obio0->sc_bh, offset, value);
    319 }
    320 
    321 void obio_write_1(int offset, uint8_t value)
    322 {
    323 	if (obio0 == NULL)
    324 		return;
    325 	bus_space_write_1(obio0->sc_tag, obio0->sc_bh, offset, value);
    326 }
    327 
    328 uint32_t obio_read_4(int offset)
    329 {
    330 	if (obio0 == NULL)
    331 		return 0xffffffff;
    332 	return bus_space_read_4(obio0->sc_tag, obio0->sc_bh, offset);
    333 }
    334 
    335 uint8_t obio_read_1(int offset)
    336 {
    337 	if (obio0 == NULL)
    338 		return 0xff;
    339 	return bus_space_read_1(obio0->sc_tag, obio0->sc_bh, offset);
    340 }
    341 
    342 int
    343 obio_space_map(bus_addr_t addr, bus_size_t size, bus_space_handle_t *bh)
    344 {
    345 	if (obio0 == NULL)
    346 		return 0xff;
    347 	return bus_space_subregion(obio0->sc_tag, obio0->sc_bh,
    348 	    addr & 0xfffff, size, bh);
    349 }
    350 
    351 #ifdef OBIO_SPEED_CONTROL
    352 
    353 static void
    354 obio_setup_cpufreq(device_t dev)
    355 {
    356 	struct obio_softc *sc = device_private(dev);
    357 	int ret;
    358 
    359 	ret = cpufreq_register(&sc->sc_cf);
    360 	if (ret != 0)
    361 		aprint_error_dev(sc->sc_dev, "cpufreq_register() failed, error %d\n", ret);
    362 }
    363 
    364 static void
    365 obio_setup_gpios(struct obio_softc *sc, int node)
    366 {
    367 	uint32_t gpio_base, reg[6];
    368 	const struct sysctlnode *sysctl_node, *me, *freq;
    369 	struct cpufreq *cf = &sc->sc_cf;
    370 	char name[32];
    371 	int child, use_dfs, cpunode, hiclock;
    372 
    373 	if (! of_compatible(sc->sc_node, keylargo))
    374 		return;
    375 
    376 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 4)
    377 		return;
    378 
    379 	gpio_base = reg[0];
    380 	DPRINTF("gpio_base: %02x\n", gpio_base);
    381 
    382 	/* now look for voltage and bus speed gpios */
    383 	use_dfs = 0;
    384 	for (child = OF_child(node); child; child = OF_peer(child)) {
    385 
    386 		if (OF_getprop(child, "name", name, sizeof(name)) < 1)
    387 			continue;
    388 
    389 		if (OF_getprop(child, "reg", reg, sizeof(reg)) < 4)
    390 			continue;
    391 
    392 		/*
    393 		 * These register offsets either have to be added to the obio
    394 		 * base address or to the gpio base address. This differs
    395 		 * even in the same OF-tree! So we guess the offset is
    396 		 * based on obio when it is larger than the gpio_base.
    397 		 */
    398 		if (reg[0] >= gpio_base)
    399 			reg[0] -= gpio_base;
    400 
    401 		if (strcmp(name, "frequency-gpio") == 0) {
    402 			DPRINTF("found frequency_gpio at %02x\n", reg[0]);
    403 			sc->sc_busspeed = gpio_base + reg[0];
    404 		}
    405 		if (strcmp(name, "voltage-gpio") == 0) {
    406 			DPRINTF("found voltage_gpio at %02x\n", reg[0]);
    407 			sc->sc_voltage = gpio_base + reg[0];
    408 		}
    409 		if (strcmp(name, "cpu-vcore-select") == 0) {
    410 			DPRINTF("found cpu-vcore-select at %02x\n", reg[0]);
    411 			sc->sc_voltage = gpio_base + reg[0];
    412 			/* frequency gpio is not needed, we use cpu's DFS */
    413 			use_dfs = 1;
    414 		}
    415 	}
    416 
    417 	if ((sc->sc_voltage < 0) || (sc->sc_busspeed < 0 && !use_dfs))
    418 		return;
    419 
    420 	printf("%s: enabling Intrepid CPU speed control\n",
    421 	    device_xname(sc->sc_dev));
    422 
    423 	sc->sc_spd_lo = curcpu()->ci_khz / 1000;
    424 	hiclock = 0;
    425 	cpunode = OF_finddevice("/cpus/@0");
    426 	OF_getprop(cpunode, "clock-frequency", &hiclock, 4);
    427 	if (hiclock != 0)
    428 		sc->sc_spd_hi = (hiclock + 500000) / 1000000;
    429 	printf("hiclock: %d\n", sc->sc_spd_hi);
    430 	if (use_dfs) sc->sc_spd_lo = sc->sc_spd_hi / 2;
    431 
    432 	sysctl_node = NULL;
    433 
    434 	if (sysctl_createv(NULL, 0, NULL,
    435 	    &me,
    436 	    CTLFLAG_READWRITE, CTLTYPE_NODE, "cpu", NULL, NULL,
    437 	    0, NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL) != 0)
    438 		printf("couldn't create 'cpu' node\n");
    439 
    440 	if (sysctl_createv(NULL, 0, NULL,
    441 	    &freq,
    442 	    CTLFLAG_READWRITE, CTLTYPE_NODE, "frequency", NULL, NULL,
    443 	    0, NULL, 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL) != 0)
    444 		printf("couldn't create 'frequency' node\n");
    445 
    446 	if (sysctl_createv(NULL, 0, NULL,
    447 	    &sysctl_node,
    448 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC,
    449 	    CTLTYPE_INT, "target", "CPU speed", sysctl_cpuspeed_temp,
    450 	    0, (void *)sc, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
    451 	    CTL_CREATE, CTL_EOL) == 0) {
    452 	} else
    453 		printf("couldn't create 'target' node\n");
    454 
    455 	if (sysctl_createv(NULL, 0, NULL,
    456 	    &sysctl_node,
    457 	    CTLFLAG_READWRITE,
    458 	    CTLTYPE_INT, "current", NULL, sysctl_cpuspeed_cur,
    459 	    1, (void *)sc, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
    460 	    CTL_CREATE, CTL_EOL) == 0) {
    461 	} else
    462 		printf("couldn't create 'current' node\n");
    463 
    464 	if (sysctl_createv(NULL, 0, NULL,
    465 	    &sysctl_node,
    466 	    CTLFLAG_READWRITE,
    467 	    CTLTYPE_STRING, "available", NULL, sysctl_cpuspeed_available,
    468 	    2, (void *)sc, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
    469 	    CTL_CREATE, CTL_EOL) == 0) {
    470 	} else
    471 		printf("couldn't create 'available' node\n");
    472 	printf("speed: %d\n", curcpu()->ci_khz);
    473 
    474 	/* support cpufreq */
    475 	snprintf(cf->cf_name, CPUFREQ_NAME_MAX, "Intrepid");
    476 	cf->cf_state[0].cfs_freq = sc->sc_spd_hi;
    477 	cf->cf_state[1].cfs_freq = sc->sc_spd_lo;
    478 	cf->cf_state_count = 2;
    479 	cf->cf_mp = FALSE;
    480 	cf->cf_cookie = sc;
    481 	cf->cf_get_freq = obio_get_freq;
    482 	cf->cf_set_freq = obio_set_freq;
    483 	/*
    484 	 * XXX
    485 	 * cpufreq_register() calls xc_broadcast() which relies on kthreads
    486 	 * running so we need to postpone it
    487 	 */
    488 	config_interrupts(sc->sc_dev, obio_setup_cpufreq);
    489 }
    490 
    491 static void
    492 obio_set_cpu_speed(struct obio_softc *sc, int fast)
    493 {
    494 
    495 	if (sc->sc_voltage < 0)
    496 		return;
    497 
    498 	if (sc->sc_busspeed >= 0) {
    499 		/* set voltage and speed via gpio */
    500 		if (fast) {
    501 			bus_space_write_1(sc->sc_tag, sc->sc_bh,
    502 			    sc->sc_voltage, 5);
    503 			bus_space_write_1(sc->sc_tag, sc->sc_bh,
    504 			    sc->sc_busspeed, 5);
    505 		} else {
    506 			bus_space_write_1(sc->sc_tag, sc->sc_bh,
    507 			    sc->sc_busspeed, 4);
    508 			bus_space_write_1(sc->sc_tag, sc->sc_bh,
    509 			    sc->sc_voltage, 4);
    510 		}
    511 	}
    512 	else {
    513 		/* set voltage via gpio and speed via the 7447A's DFS bit */
    514 		if (fast) {
    515 			bus_space_write_1(sc->sc_tag, sc->sc_bh,
    516 			    sc->sc_voltage, 5);
    517 			DELAY(1000);
    518 		}
    519 
    520 		/* set DFS for all cpus */
    521 		cpu_set_dfs(fast ? 1 : 2);
    522 		DELAY(100);
    523 
    524 		if (!fast) {
    525 			bus_space_write_1(sc->sc_tag, sc->sc_bh,
    526 			    sc->sc_voltage, 4);
    527 			DELAY(1000);
    528 		}
    529 	}
    530 }
    531 
    532 static int
    533 obio_get_cpu_speed(struct obio_softc *sc)
    534 {
    535 
    536 	if (sc->sc_voltage < 0)
    537 		return 0;
    538 
    539 	if (sc->sc_busspeed >= 0) {
    540 		if (bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_busspeed)
    541 		    & 1)
    542 			return 1;
    543 	}
    544 	else
    545 		return cpu_get_dfs() == 1;
    546 
    547 	return 0;
    548 }
    549 
    550 static void
    551 obio_get_freq(void *cookie, void *spd)
    552 {
    553 	struct obio_softc *sc = cookie;
    554 	uint32_t *freq;
    555 
    556 	freq = spd;
    557 	if (obio_get_cpu_speed(sc) == 0) {
    558 		*freq = sc->sc_spd_lo;
    559 	} else
    560 		*freq = sc->sc_spd_hi;
    561 }
    562 
    563 static void
    564 obio_set_freq(void *cookie, void *spd)
    565 {
    566 	struct obio_softc *sc = cookie;
    567 	uint32_t *freq;
    568 
    569 	freq = spd;
    570 	if (*freq == sc->sc_spd_lo) {
    571 		obio_set_cpu_speed(sc, 0);
    572 	} else if (*freq == sc->sc_spd_hi) {
    573 		obio_set_cpu_speed(sc, 1);
    574 	} else
    575 		aprint_error_dev(sc->sc_dev, "%s(%d) bogus CPU speed\n", __func__, *freq);
    576 }
    577 
    578 static int
    579 sysctl_cpuspeed_temp(SYSCTLFN_ARGS)
    580 {
    581 	struct sysctlnode node = *rnode;
    582 	struct obio_softc *sc = node.sysctl_data;
    583 	int speed, mhz;
    584 
    585 	speed = obio_get_cpu_speed(sc);
    586 	switch (speed) {
    587 		case 0:
    588 			mhz = sc->sc_spd_lo;
    589 			break;
    590 		case 1:
    591 			mhz = sc->sc_spd_hi;
    592 			break;
    593 		default:
    594 			speed = -1;
    595 	}
    596 	node.sysctl_data = &mhz;
    597 	if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
    598 		int new_reg;
    599 
    600 		new_reg = *(int *)node.sysctl_data;
    601 		if (new_reg == sc->sc_spd_lo) {
    602 			obio_set_cpu_speed(sc, 0);
    603 		} else if (new_reg == sc->sc_spd_hi) {
    604 			obio_set_cpu_speed(sc, 1);
    605 		} else {
    606 			printf("%s: new_reg %d\n", __func__, new_reg);
    607 			return EINVAL;
    608 		}
    609 		return 0;
    610 	}
    611 	return EINVAL;
    612 }
    613 
    614 static int
    615 sysctl_cpuspeed_cur(SYSCTLFN_ARGS)
    616 {
    617 	struct sysctlnode node = *rnode;
    618 	struct obio_softc *sc = node.sysctl_data;
    619 	int speed, mhz;
    620 
    621 	speed = obio_get_cpu_speed(sc);
    622 	switch (speed) {
    623 		case 0:
    624 			mhz = sc->sc_spd_lo;
    625 			break;
    626 		case 1:
    627 			mhz = sc->sc_spd_hi;
    628 			break;
    629 		default:
    630 			speed = -1;
    631 	}
    632 	node.sysctl_data = &mhz;
    633 	return sysctl_lookup(SYSCTLFN_CALL(&node));
    634 }
    635 
    636 static int
    637 sysctl_cpuspeed_available(SYSCTLFN_ARGS)
    638 {
    639 	struct sysctlnode node = *rnode;
    640 	struct obio_softc *sc = node.sysctl_data;
    641 	char buf[128];
    642 
    643 	snprintf(buf, 128, "%d %d", sc->sc_spd_lo, sc->sc_spd_hi);
    644 	node.sysctl_data = buf;
    645 	return(sysctl_lookup(SYSCTLFN_CALL(&node)));
    646 }
    647 
    648 SYSCTL_SETUP(sysctl_ams_setup, "sysctl obio subtree setup")
    649 {
    650 
    651 	sysctl_createv(NULL, 0, NULL, NULL,
    652 		       CTLFLAG_PERMANENT,
    653 		       CTLTYPE_NODE, "machdep", NULL,
    654 		       NULL, 0, NULL, 0,
    655 		       CTL_MACHDEP, CTL_EOL);
    656 }
    657 
    658 #endif /* OBIO_SPEEDCONTROL */
    659