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