Home | History | Annotate | Line # | Download | only in dev
obio.c revision 1.34
      1  1.34  macallan /*	$NetBSD: obio.c,v 1.34 2011/07/26 08:36:02 macallan 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.34  macallan __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.34 2011/07/26 08:36:02 macallan 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.30       phx 
     54  1.27   garbled #include "opt_obio.h"
     55  1.27   garbled 
     56  1.27   garbled #ifdef OBIO_DEBUG
     57  1.27   garbled # define DPRINTF printf
     58  1.27   garbled #else
     59  1.27   garbled # define DPRINTF while (0) printf
     60  1.27   garbled #endif
     61  1.27   garbled 
     62  1.33      matt static void obio_attach(device_t, device_t, void *);
     63  1.33      matt static int obio_match(device_t, cfdata_t, void *);
     64  1.27   garbled static int obio_print(void *, const char *);
     65   1.1    tsubai 
     66   1.1    tsubai struct obio_softc {
     67  1.34  macallan 	device_t sc_dev;
     68  1.27   garbled 	bus_space_tag_t sc_tag;
     69  1.27   garbled 	bus_space_handle_t sc_bh;
     70   1.1    tsubai 	int sc_node;
     71  1.27   garbled #ifdef OBIO_SPEED_CONTROL
     72  1.27   garbled 	int sc_voltage;
     73  1.27   garbled 	int sc_busspeed;
     74  1.32  macallan 	int sc_spd_hi, sc_spd_lo;
     75  1.27   garbled #endif
     76   1.1    tsubai };
     77   1.1    tsubai 
     78  1.28  macallan static struct obio_softc *obio0 = NULL;
     79  1.28  macallan 
     80  1.27   garbled #ifdef OBIO_SPEED_CONTROL
     81  1.27   garbled static void obio_setup_gpios(struct obio_softc *, int);
     82  1.27   garbled static void obio_set_cpu_speed(struct obio_softc *, int);
     83  1.27   garbled static int  obio_get_cpu_speed(struct obio_softc *);
     84  1.27   garbled static int  sysctl_cpuspeed_temp(SYSCTLFN_ARGS);
     85  1.32  macallan static int  sysctl_cpuspeed_cur(SYSCTLFN_ARGS);
     86  1.32  macallan static int  sysctl_cpuspeed_available(SYSCTLFN_ARGS);
     87  1.27   garbled 
     88  1.27   garbled static const char *keylargo[] = {"Keylargo",
     89  1.27   garbled 				 "AAPL,Keylargo",
     90  1.27   garbled 				 NULL};
     91  1.27   garbled 
     92  1.27   garbled #endif
     93   1.1    tsubai 
     94  1.34  macallan CFATTACH_DECL_NEW(obio, sizeof(struct obio_softc),
     95  1.16   thorpej     obio_match, obio_attach, NULL, NULL);
     96   1.1    tsubai 
     97   1.1    tsubai int
     98  1.33      matt obio_match(device_t parent, cfdata_t cf, void *aux)
     99   1.1    tsubai {
    100   1.1    tsubai 	struct pci_attach_args *pa = aux;
    101   1.1    tsubai 
    102   1.2    tsubai 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE)
    103   1.2    tsubai 		switch (PCI_PRODUCT(pa->pa_id)) {
    104   1.7    tsubai 		case PCI_PRODUCT_APPLE_GC:
    105   1.7    tsubai 		case PCI_PRODUCT_APPLE_OHARE:
    106   1.7    tsubai 		case PCI_PRODUCT_APPLE_HEATHROW:
    107   1.7    tsubai 		case PCI_PRODUCT_APPLE_PADDINGTON:
    108   1.7    tsubai 		case PCI_PRODUCT_APPLE_KEYLARGO:
    109  1.14    tsubai 		case PCI_PRODUCT_APPLE_PANGEA_MACIO:
    110  1.18  hamajima 		case PCI_PRODUCT_APPLE_INTREPID:
    111  1.24   sanjayl 		case PCI_PRODUCT_APPLE_K2:
    112   1.2    tsubai 			return 1;
    113   1.2    tsubai 		}
    114   1.1    tsubai 
    115   1.1    tsubai 	return 0;
    116   1.1    tsubai }
    117   1.1    tsubai 
    118   1.1    tsubai /*
    119   1.1    tsubai  * Attach all the sub-devices we can find
    120   1.1    tsubai  */
    121   1.1    tsubai void
    122  1.33      matt obio_attach(device_t parent, device_t self, void *aux)
    123   1.1    tsubai {
    124  1.33      matt 	struct obio_softc *sc = device_private(self);
    125   1.2    tsubai 	struct pci_attach_args *pa = aux;
    126   1.1    tsubai 	struct confargs ca;
    127  1.27   garbled 	bus_space_handle_t bsh;
    128  1.27   garbled 	int node, child, namelen, error;
    129   1.1    tsubai 	u_int reg[20];
    130  1.21    briggs 	int intr[6], parent_intr = 0, parent_nintr = 0;
    131   1.1    tsubai 	char name[32];
    132  1.21    briggs 	char compat[32];
    133   1.1    tsubai 
    134  1.34  macallan 	sc->sc_dev = self;
    135  1.27   garbled #ifdef OBIO_SPEED_CONTROL
    136  1.27   garbled 	sc->sc_voltage = -1;
    137  1.27   garbled 	sc->sc_busspeed = -1;
    138  1.32  macallan 	sc->sc_spd_lo = 600;
    139  1.32  macallan 	sc->sc_spd_hi = 800;
    140  1.27   garbled #endif
    141  1.27   garbled 
    142   1.2    tsubai 	switch (PCI_PRODUCT(pa->pa_id)) {
    143   1.2    tsubai 
    144   1.7    tsubai 	case PCI_PRODUCT_APPLE_GC:
    145   1.7    tsubai 	case PCI_PRODUCT_APPLE_OHARE:
    146   1.7    tsubai 	case PCI_PRODUCT_APPLE_HEATHROW:
    147   1.7    tsubai 	case PCI_PRODUCT_APPLE_PADDINGTON:
    148   1.7    tsubai 	case PCI_PRODUCT_APPLE_KEYLARGO:
    149  1.14    tsubai 	case PCI_PRODUCT_APPLE_PANGEA_MACIO:
    150  1.18  hamajima 	case PCI_PRODUCT_APPLE_INTREPID:
    151  1.21    briggs 		node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    152   1.6    tsubai 		if (node == -1)
    153  1.21    briggs 			node = OF_finddevice("mac-io");
    154  1.21    briggs 			if (node == -1)
    155  1.21    briggs 				node = OF_finddevice("/pci/mac-io");
    156   1.2    tsubai 		break;
    157  1.24   sanjayl 	case PCI_PRODUCT_APPLE_K2:
    158  1.24   sanjayl 		node = OF_finddevice("mac-io");
    159  1.24   sanjayl 		break;
    160   1.2    tsubai 
    161   1.2    tsubai 	default:
    162  1.27   garbled 		node = -1;
    163  1.27   garbled 		break;
    164   1.2    tsubai 	}
    165  1.27   garbled 	if (node == -1)
    166  1.27   garbled 		panic("macio not found or unknown");
    167   1.2    tsubai 
    168   1.1    tsubai 	sc->sc_node = node;
    169   1.1    tsubai 
    170  1.24   sanjayl #if defined (PMAC_G5)
    171  1.24   sanjayl 	if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 20)
    172  1.24   sanjayl 	{
    173  1.24   sanjayl 		return;
    174  1.24   sanjayl 	}
    175  1.24   sanjayl #else
    176   1.1    tsubai 	if (OF_getprop(node, "assigned-addresses", reg, sizeof(reg)) < 12)
    177   1.1    tsubai 		return;
    178  1.24   sanjayl #endif /* PMAC_G5 */
    179  1.24   sanjayl 
    180  1.28  macallan 	/*
    181  1.28  macallan 	 * XXX
    182  1.28  macallan 	 * This relies on the primary obio always attaching first which is
    183  1.28  macallan 	 * true on the PowerBook 3400c and similar machines but may or may
    184  1.28  macallan 	 * not work on others. We can't rely on the node name since Apple
    185  1.28  macallan 	 * didn't follow anything remotely resembling a consistent naming
    186  1.28  macallan 	 * scheme.
    187  1.28  macallan 	 */
    188  1.28  macallan 	if (obio0 == NULL)
    189  1.28  macallan 		obio0 = sc;
    190  1.28  macallan 
    191   1.1    tsubai 	ca.ca_baseaddr = reg[2];
    192  1.27   garbled 	ca.ca_tag = pa->pa_memt;
    193  1.27   garbled 	sc->sc_tag = pa->pa_memt;
    194  1.27   garbled 	error = bus_space_map (pa->pa_memt, ca.ca_baseaddr, 0x80, 0, &bsh);
    195  1.27   garbled 	if (error)
    196  1.27   garbled 		panic(": failed to map mac-io %#x", ca.ca_baseaddr);
    197  1.27   garbled 	sc->sc_bh = bsh;
    198   1.1    tsubai 
    199   1.1    tsubai 	printf(": addr 0x%x\n", ca.ca_baseaddr);
    200  1.13    tsubai 
    201  1.20    briggs 	/* Enable internal modem (KeyLargo) */
    202  1.20    briggs 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_KEYLARGO) {
    203  1.27   garbled 		aprint_normal("%s: enabling KeyLargo internal modem\n",
    204  1.27   garbled 		    self->dv_xname);
    205  1.27   garbled 		bus_space_write_4(ca.ca_tag, bsh, 0x40,
    206  1.27   garbled 		    bus_space_read_4(ca.ca_tag, bsh, 0x40) & ~(1<<25));
    207  1.20    briggs 	}
    208  1.26  macallan 
    209  1.20    briggs 	/* Enable internal modem (Pangea) */
    210  1.20    briggs 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PANGEA_MACIO) {
    211  1.27   garbled 		/* set reset */
    212  1.27   garbled 		bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x03, 0x04);
    213  1.27   garbled 		/* power modem on */
    214  1.27   garbled 		bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x02, 0x04);
    215  1.27   garbled 		/* unset reset */
    216  1.27   garbled 		bus_space_write_1(ca.ca_tag, bsh, 0x006a + 0x03, 0x05);
    217  1.20    briggs 	}
    218  1.20    briggs 
    219  1.21    briggs 	/* Gatwick and Paddington use same product ID */
    220  1.21    briggs 	namelen = OF_getprop(node, "compatible", compat, sizeof(compat));
    221   1.1    tsubai 
    222  1.21    briggs 	if (strcmp(compat, "gatwick") == 0) {
    223  1.21    briggs 		parent_nintr = OF_getprop(node, "AAPL,interrupts", intr,
    224  1.21    briggs 					sizeof(intr));
    225  1.21    briggs 		parent_intr = intr[0];
    226  1.21    briggs 	} else {
    227  1.21    briggs   		/* Enable CD and microphone sound input. */
    228  1.21    briggs 		if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_PADDINGTON)
    229  1.27   garbled 			bus_space_write_1(ca.ca_tag, bsh, 0x37, 0x03);
    230  1.21    briggs 	}
    231  1.27   garbled 
    232   1.1    tsubai 	for (child = OF_child(node); child; child = OF_peer(child)) {
    233   1.1    tsubai 		namelen = OF_getprop(child, "name", name, sizeof(name));
    234   1.1    tsubai 		if (namelen < 0)
    235   1.1    tsubai 			continue;
    236   1.1    tsubai 		if (namelen >= sizeof(name))
    237   1.1    tsubai 			continue;
    238   1.1    tsubai 
    239  1.27   garbled #ifdef OBIO_SPEED_CONTROL
    240  1.27   garbled 		if (strcmp(name, "gpio") == 0) {
    241  1.27   garbled 
    242  1.27   garbled 			obio_setup_gpios(sc, child);
    243  1.27   garbled 			continue;
    244  1.27   garbled 		}
    245  1.27   garbled #endif
    246  1.27   garbled 
    247   1.1    tsubai 		name[namelen] = 0;
    248   1.1    tsubai 		ca.ca_name = name;
    249   1.1    tsubai 		ca.ca_node = child;
    250  1.25  macallan 		ca.ca_tag = pa->pa_memt;
    251  1.26  macallan 
    252  1.21    briggs 		ca.ca_nreg = OF_getprop(child, "reg", reg, sizeof(reg));
    253  1.26  macallan 
    254  1.21    briggs 		if (strcmp(compat, "gatwick") != 0) {
    255  1.21    briggs 			ca.ca_nintr = OF_getprop(child, "AAPL,interrupts", intr,
    256   1.5    tsubai 					sizeof(intr));
    257  1.21    briggs 			if (ca.ca_nintr == -1)
    258  1.21    briggs 				ca.ca_nintr = OF_getprop(child, "interrupts", intr,
    259  1.21    briggs 						sizeof(intr));
    260  1.21    briggs 		} else {
    261  1.21    briggs 			intr[0] = parent_intr;
    262  1.21    briggs 			ca.ca_nintr = parent_nintr;
    263  1.21    briggs 		}
    264   1.1    tsubai 		ca.ca_reg = reg;
    265   1.1    tsubai 		ca.ca_intr = intr;
    266   1.1    tsubai 
    267   1.1    tsubai 		config_found(self, &ca, obio_print);
    268   1.1    tsubai 	}
    269   1.1    tsubai }
    270   1.1    tsubai 
    271  1.27   garbled static const char * const skiplist[] = {
    272   1.8    tsubai 	"interrupt-controller",
    273   1.8    tsubai 	"gpio",
    274   1.8    tsubai 	"escc-legacy",
    275   1.8    tsubai 	"timer",
    276   1.9    tsubai 	"i2c",
    277  1.24   sanjayl 	"power-mgt",
    278  1.27   garbled 	"escc",
    279  1.27   garbled 	"battery",
    280  1.27   garbled 	"backlight"
    281  1.24   sanjayl 
    282   1.8    tsubai };
    283   1.8    tsubai 
    284   1.8    tsubai #define N_LIST (sizeof(skiplist) / sizeof(skiplist[0]))
    285   1.8    tsubai 
    286   1.1    tsubai int
    287  1.29       dsl obio_print(void *aux, const char *obio)
    288   1.1    tsubai {
    289   1.1    tsubai 	struct confargs *ca = aux;
    290   1.8    tsubai 	int i;
    291   1.8    tsubai 
    292   1.8    tsubai 	for (i = 0; i < N_LIST; i++)
    293   1.8    tsubai 		if (strcmp(ca->ca_name, skiplist[i]) == 0)
    294   1.8    tsubai 			return QUIET;
    295   1.1    tsubai 
    296   1.1    tsubai 	if (obio)
    297  1.17   thorpej 		aprint_normal("%s at %s", ca->ca_name, obio);
    298   1.1    tsubai 
    299   1.1    tsubai 	if (ca->ca_nreg > 0)
    300  1.17   thorpej 		aprint_normal(" offset 0x%x", ca->ca_reg[0]);
    301   1.1    tsubai 
    302   1.1    tsubai 	return UNCONF;
    303   1.1    tsubai }
    304  1.27   garbled 
    305  1.28  macallan void obio_write_4(int offset, uint32_t value)
    306  1.28  macallan {
    307  1.28  macallan 	if (obio0 == NULL)
    308  1.28  macallan 		return;
    309  1.28  macallan 	bus_space_write_4(obio0->sc_tag, obio0->sc_bh, offset, value);
    310  1.28  macallan }
    311  1.28  macallan 
    312  1.28  macallan void obio_write_1(int offset, uint8_t value)
    313  1.28  macallan {
    314  1.28  macallan 	if (obio0 == NULL)
    315  1.28  macallan 		return;
    316  1.28  macallan 	bus_space_write_1(obio0->sc_tag, obio0->sc_bh, offset, value);
    317  1.28  macallan }
    318  1.28  macallan 
    319  1.28  macallan uint32_t obio_read_4(int offset)
    320  1.28  macallan {
    321  1.28  macallan 	if (obio0 == NULL)
    322  1.28  macallan 		return 0xffffffff;
    323  1.28  macallan 	return bus_space_read_4(obio0->sc_tag, obio0->sc_bh, offset);
    324  1.28  macallan }
    325  1.28  macallan 
    326  1.28  macallan uint8_t obio_read_1(int offset)
    327  1.28  macallan {
    328  1.28  macallan 	if (obio0 == NULL)
    329  1.28  macallan 		return 0xff;
    330  1.28  macallan 	return bus_space_read_1(obio0->sc_tag, obio0->sc_bh, offset);
    331  1.28  macallan }
    332  1.28  macallan 
    333  1.27   garbled #ifdef OBIO_SPEED_CONTROL
    334  1.27   garbled 
    335  1.27   garbled static void
    336  1.27   garbled obio_setup_gpios(struct obio_softc *sc, int node)
    337  1.27   garbled {
    338  1.31       phx 	uint32_t gpio_base, reg[6];
    339  1.32  macallan 	struct sysctlnode *sysctl_node, *me, *freq;
    340  1.27   garbled 	char name[32];
    341  1.32  macallan 	int child, use_dfs, cpunode, hiclock;
    342  1.27   garbled 
    343  1.27   garbled 	if (of_compatible(sc->sc_node, keylargo) == -1)
    344  1.27   garbled 		return;
    345  1.27   garbled 
    346  1.27   garbled 	if (OF_getprop(node, "reg", reg, sizeof(reg)) < 4)
    347  1.27   garbled 		return;
    348  1.27   garbled 
    349  1.27   garbled 	gpio_base = reg[0];
    350  1.27   garbled 	DPRINTF("gpio_base: %02x\n", gpio_base);
    351  1.27   garbled 
    352  1.27   garbled 	/* now look for voltage and bus speed gpios */
    353  1.30       phx 	use_dfs = 0;
    354  1.27   garbled 	for (child = OF_child(node); child; child = OF_peer(child)) {
    355  1.27   garbled 
    356  1.27   garbled 		if (OF_getprop(child, "name", name, sizeof(name)) < 1)
    357  1.27   garbled 			continue;
    358  1.27   garbled 
    359  1.27   garbled 		if (OF_getprop(child, "reg", reg, sizeof(reg)) < 4)
    360  1.27   garbled 			continue;
    361  1.27   garbled 
    362  1.31       phx 		/*
    363  1.31       phx 		 * These register offsets either have to be added to the obio
    364  1.31       phx 		 * base address or to the gpio base address. This differs
    365  1.31       phx 		 * even in the same OF-tree! So we guess the offset is
    366  1.31       phx 		 * based on obio when it is larger than the gpio_base.
    367  1.31       phx 		 */
    368  1.31       phx 		if (reg[0] >= gpio_base)
    369  1.31       phx 			reg[0] -= gpio_base;
    370  1.31       phx 
    371  1.27   garbled 		if (strcmp(name, "frequency-gpio") == 0) {
    372  1.27   garbled 			DPRINTF("found frequency_gpio at %02x\n", reg[0]);
    373  1.27   garbled 			sc->sc_busspeed = gpio_base + reg[0];
    374  1.27   garbled 		}
    375  1.27   garbled 		if (strcmp(name, "voltage-gpio") == 0) {
    376  1.27   garbled 			DPRINTF("found voltage_gpio at %02x\n", reg[0]);
    377  1.27   garbled 			sc->sc_voltage = gpio_base + reg[0];
    378  1.27   garbled 		}
    379  1.30       phx 		if (strcmp(name, "cpu-vcore-select") == 0) {
    380  1.30       phx 			DPRINTF("found cpu-vcore-select at %02x\n", reg[0]);
    381  1.30       phx 			sc->sc_voltage = gpio_base + reg[0];
    382  1.30       phx 			/* frequency gpio is not needed, we use cpu's DFS */
    383  1.30       phx 			use_dfs = 1;
    384  1.30       phx 		}
    385  1.27   garbled 	}
    386  1.27   garbled 
    387  1.30       phx 	if ((sc->sc_voltage < 0) || (sc->sc_busspeed < 0 && !use_dfs))
    388  1.27   garbled 		return;
    389  1.27   garbled 
    390  1.27   garbled 	printf("%s: enabling Intrepid CPU speed control\n",
    391  1.34  macallan 	    device_xname(sc->sc_dev));
    392  1.27   garbled 
    393  1.32  macallan 	sc->sc_spd_lo = curcpu()->ci_khz / 1000;
    394  1.32  macallan 	hiclock = 0;
    395  1.32  macallan 	cpunode = OF_finddevice("/cpus/@0");
    396  1.32  macallan 	OF_getprop(cpunode, "clock-frequency", &hiclock, 4);
    397  1.32  macallan 	printf("hiclock: %d\n", (hiclock + 500000) / 1000000);
    398  1.30       phx 	sysctl_node = NULL;
    399  1.32  macallan 
    400  1.32  macallan 	if (sysctl_createv(NULL, 0, NULL,
    401  1.32  macallan 	    (const struct sysctlnode **)&me,
    402  1.32  macallan 	    CTLFLAG_READWRITE, CTLTYPE_NODE, "intrepid", NULL, NULL,
    403  1.32  macallan 	    0, NULL, 0, CTL_MACHDEP, CTL_CREATE, CTL_EOL) != 0)
    404  1.32  macallan 		printf("couldn't create 'interpid' node\n");
    405  1.32  macallan 
    406  1.32  macallan 	if (sysctl_createv(NULL, 0, NULL,
    407  1.32  macallan 	    (const struct sysctlnode **)&freq,
    408  1.32  macallan 	    CTLFLAG_READWRITE, CTLTYPE_NODE, "frequency", NULL, NULL,
    409  1.32  macallan 	    0, NULL, 0, CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL) != 0)
    410  1.32  macallan 		printf("couldn't create 'frequency' node\n");
    411  1.32  macallan 
    412  1.32  macallan 	if (sysctl_createv(NULL, 0, NULL,
    413  1.27   garbled 	    (const struct sysctlnode **)&sysctl_node,
    414  1.27   garbled 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC | CTLFLAG_IMMEDIATE,
    415  1.32  macallan 	    CTLTYPE_INT, "target", "CPU speed", sysctl_cpuspeed_temp,
    416  1.32  macallan 	    0, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
    417  1.32  macallan 	    CTL_CREATE, CTL_EOL) == 0) {
    418  1.32  macallan 		sysctl_node->sysctl_data = (void *)sc;
    419  1.32  macallan 	} else
    420  1.32  macallan 		printf("couldn't create 'target' node\n");
    421  1.32  macallan 
    422  1.32  macallan 	if (sysctl_createv(NULL, 0, NULL,
    423  1.32  macallan 	    (const struct sysctlnode **)&sysctl_node,
    424  1.32  macallan 	    CTLFLAG_READWRITE | CTLFLAG_IMMEDIATE,
    425  1.32  macallan 	    CTLTYPE_INT, "current", NULL, sysctl_cpuspeed_cur,
    426  1.32  macallan 	    1, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
    427  1.32  macallan 	    CTL_CREATE, CTL_EOL) == 0) {
    428  1.27   garbled 		sysctl_node->sysctl_data = (void *)sc;
    429  1.32  macallan 	} else
    430  1.32  macallan 		printf("couldn't create 'current' node\n");
    431  1.32  macallan 
    432  1.32  macallan 	if (sysctl_createv(NULL, 0, NULL,
    433  1.32  macallan 	    (const struct sysctlnode **)&sysctl_node,
    434  1.32  macallan 	    CTLFLAG_READWRITE,
    435  1.32  macallan 	    CTLTYPE_STRING, "available", NULL, sysctl_cpuspeed_available,
    436  1.32  macallan 	    2, NULL, 0, CTL_MACHDEP, me->sysctl_num, freq->sysctl_num,
    437  1.32  macallan 	    CTL_CREATE, CTL_EOL) == 0) {
    438  1.32  macallan 		sysctl_node->sysctl_data = (void *)sc;
    439  1.32  macallan 	} else
    440  1.32  macallan 		printf("couldn't create 'available' node\n");
    441  1.32  macallan 	printf("speed: %d\n", curcpu()->ci_khz);
    442  1.27   garbled }
    443  1.27   garbled 
    444  1.27   garbled static void
    445  1.27   garbled obio_set_cpu_speed(struct obio_softc *sc, int fast)
    446  1.27   garbled {
    447  1.27   garbled 
    448  1.30       phx 	if (sc->sc_voltage < 0)
    449  1.27   garbled 		return;
    450  1.27   garbled 
    451  1.30       phx 	if (sc->sc_busspeed >= 0) {
    452  1.30       phx 		/* set voltage and speed via gpio */
    453  1.30       phx 		if (fast) {
    454  1.30       phx 			bus_space_write_1(sc->sc_tag, sc->sc_bh,
    455  1.30       phx 			    sc->sc_voltage, 5);
    456  1.30       phx 			bus_space_write_1(sc->sc_tag, sc->sc_bh,
    457  1.30       phx 			    sc->sc_busspeed, 5);
    458  1.30       phx 		} else {
    459  1.30       phx 			bus_space_write_1(sc->sc_tag, sc->sc_bh,
    460  1.30       phx 			    sc->sc_busspeed, 4);
    461  1.30       phx 			bus_space_write_1(sc->sc_tag, sc->sc_bh,
    462  1.30       phx 			    sc->sc_voltage, 4);
    463  1.30       phx 		}
    464  1.30       phx 	}
    465  1.30       phx 	else {
    466  1.30       phx 		/* set voltage via gpio and speed via the 7447A's DFS bit */
    467  1.30       phx 		if (fast) {
    468  1.30       phx 			bus_space_write_1(sc->sc_tag, sc->sc_bh,
    469  1.30       phx 			    sc->sc_voltage, 5);
    470  1.30       phx 			DELAY(1000);
    471  1.30       phx 		}
    472  1.30       phx 
    473  1.30       phx 		/* set DFS for all cpus */
    474  1.30       phx 		cpu_set_dfs(fast ? 1 : 2);
    475  1.30       phx 		DELAY(100);
    476  1.30       phx 
    477  1.30       phx 		if (!fast) {
    478  1.30       phx 			bus_space_write_1(sc->sc_tag, sc->sc_bh,
    479  1.30       phx 			    sc->sc_voltage, 4);
    480  1.30       phx 			DELAY(1000);
    481  1.30       phx 		}
    482  1.27   garbled 	}
    483  1.27   garbled }
    484  1.27   garbled 
    485  1.27   garbled static int
    486  1.27   garbled obio_get_cpu_speed(struct obio_softc *sc)
    487  1.27   garbled {
    488  1.27   garbled 
    489  1.30       phx 	if (sc->sc_voltage < 0)
    490  1.27   garbled 		return 0;
    491  1.27   garbled 
    492  1.30       phx 	if (sc->sc_busspeed >= 0) {
    493  1.30       phx 		if (bus_space_read_1(sc->sc_tag, sc->sc_bh, sc->sc_busspeed)
    494  1.30       phx 		    & 1)
    495  1.31       phx 			return 1;
    496  1.30       phx 	}
    497  1.30       phx 	else
    498  1.30       phx 		return cpu_get_dfs() == 1;
    499  1.27   garbled 
    500  1.27   garbled 	return 0;
    501  1.27   garbled }
    502  1.27   garbled 
    503  1.27   garbled static int
    504  1.27   garbled sysctl_cpuspeed_temp(SYSCTLFN_ARGS)
    505  1.27   garbled {
    506  1.27   garbled 	struct sysctlnode node = *rnode;
    507  1.27   garbled 	struct obio_softc *sc = node.sysctl_data;
    508  1.32  macallan 	int speed, mhz;
    509  1.27   garbled 
    510  1.27   garbled 	speed = obio_get_cpu_speed(sc);
    511  1.32  macallan 	switch (speed) {
    512  1.32  macallan 		case 0:
    513  1.32  macallan 			mhz = sc->sc_spd_lo;
    514  1.32  macallan 			break;
    515  1.32  macallan 		case 1:
    516  1.32  macallan 			mhz = sc->sc_spd_hi;
    517  1.32  macallan 			break;
    518  1.32  macallan 		default:
    519  1.32  macallan 			speed = -1;
    520  1.32  macallan 	}
    521  1.32  macallan 	node.sysctl_idata = mhz;
    522  1.32  macallan 	node.sysctl_data = &mhz;
    523  1.32  macallan 	if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
    524  1.32  macallan 		int new_reg;
    525  1.32  macallan 
    526  1.32  macallan 		new_reg = node.sysctl_idata;
    527  1.32  macallan 		if (new_reg == sc->sc_spd_lo) {
    528  1.32  macallan 			obio_set_cpu_speed(sc, 0);
    529  1.32  macallan 		} else if (new_reg == sc->sc_spd_hi) {
    530  1.32  macallan 			obio_set_cpu_speed(sc, 1);
    531  1.32  macallan 		} else {
    532  1.32  macallan 			printf("%s: new_reg %d\n", __func__, new_reg);
    533  1.32  macallan 			return EINVAL;
    534  1.27   garbled 		}
    535  1.32  macallan 		return 0;
    536  1.27   garbled 	}
    537  1.32  macallan 	return EINVAL;
    538  1.32  macallan }
    539  1.32  macallan 
    540  1.32  macallan static int
    541  1.32  macallan sysctl_cpuspeed_cur(SYSCTLFN_ARGS)
    542  1.32  macallan {
    543  1.32  macallan 	struct sysctlnode node = *rnode;
    544  1.32  macallan 	struct obio_softc *sc = node.sysctl_data;
    545  1.32  macallan 	int speed, mhz;
    546  1.32  macallan 
    547  1.32  macallan 	speed = obio_get_cpu_speed(sc);
    548  1.32  macallan 	switch (speed) {
    549  1.32  macallan 		case 0:
    550  1.32  macallan 			mhz = sc->sc_spd_lo;
    551  1.32  macallan 			break;
    552  1.32  macallan 		case 1:
    553  1.32  macallan 			mhz = sc->sc_spd_hi;
    554  1.32  macallan 			break;
    555  1.32  macallan 		default:
    556  1.32  macallan 			speed = -1;
    557  1.32  macallan 	}
    558  1.32  macallan 	node.sysctl_idata = mhz;
    559  1.32  macallan 	node.sysctl_data = &mhz;
    560  1.32  macallan 	return sysctl_lookup(SYSCTLFN_CALL(&node));
    561  1.32  macallan }
    562  1.32  macallan 
    563  1.32  macallan static int
    564  1.32  macallan sysctl_cpuspeed_available(SYSCTLFN_ARGS)
    565  1.32  macallan {
    566  1.32  macallan 	struct sysctlnode node = *rnode;
    567  1.32  macallan 	struct obio_softc *sc = node.sysctl_data;
    568  1.32  macallan 	char buf[128];
    569  1.32  macallan 	int speed;
    570  1.32  macallan 
    571  1.32  macallan 	speed = obio_get_cpu_speed(sc);
    572  1.32  macallan 	snprintf(buf, 128, "%d %d", sc->sc_spd_lo, sc->sc_spd_hi);
    573  1.32  macallan 	node.sysctl_data = buf;
    574  1.32  macallan 	return(sysctl_lookup(SYSCTLFN_CALL(&node)));
    575  1.32  macallan }
    576  1.32  macallan 
    577  1.32  macallan SYSCTL_SETUP(sysctl_ams_setup, "sysctl obio subtree setup")
    578  1.32  macallan {
    579  1.32  macallan 
    580  1.32  macallan 	sysctl_createv(NULL, 0, NULL, NULL,
    581  1.32  macallan 		       CTLFLAG_PERMANENT,
    582  1.32  macallan 		       CTLTYPE_NODE, "machdep", NULL,
    583  1.32  macallan 		       NULL, 0, NULL, 0,
    584  1.32  macallan 		       CTL_MACHDEP, CTL_EOL);
    585  1.27   garbled }
    586  1.27   garbled 
    587  1.27   garbled #endif /* OBIO_SPEEDCONTROL */
    588