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