Home | History | Annotate | Line # | Download | only in pci
piixpcib.c revision 1.8
      1 /* $NetBSD: piixpcib.c,v 1.8 2006/09/24 03:53:08 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2004, 2006 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Minoura Makoto, Matthew R. Green, and Jared D. McNeill.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Intel PIIX4 PCI-ISA bridge device driver with CPU frequency scaling support
     41  *
     42  * Based on the FreeBSD 'smist' cpufreq driver by Bruno Ducrot
     43  */
     44 
     45 #include <sys/cdefs.h>
     46 __KERNEL_RCSID(0, "$NetBSD: piixpcib.c,v 1.8 2006/09/24 03:53:08 jmcneill Exp $");
     47 
     48 #include <sys/types.h>
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/device.h>
     52 #include <sys/sysctl.h>
     53 #include <machine/bus.h>
     54 
     55 #include <machine/frame.h>
     56 #include <machine/bioscall.h>
     57 
     58 #include <dev/pci/pcivar.h>
     59 #include <dev/pci/pcireg.h>
     60 #include <dev/pci/pcidevs.h>
     61 
     62 #include <i386/pci/piixreg.h>
     63 
     64 #define		PIIX4_PIRQRC	0x60
     65 
     66 struct piixpcib_softc {
     67 	struct device	sc_dev;
     68 
     69 	pci_chipset_tag_t sc_pc;
     70 	pcitag_t	sc_pcitag;
     71 
     72 	int		sc_smi_cmd;
     73 	int		sc_smi_data;
     74 	int		sc_command;
     75 	int		sc_flags;
     76 
     77 	bus_space_tag_t	sc_iot;
     78 	bus_space_handle_t sc_ioh;
     79 
     80 	void		*sc_powerhook;
     81 	struct pci_conf_state sc_pciconf;
     82 
     83 	pcireg_t	sc_pirqrc;
     84 	uint8_t		sc_elcr[2];
     85 };
     86 
     87 static int piixpcibmatch(struct device *, struct cfdata *, void *);
     88 static void piixpcibattach(struct device *, struct device *, void *);
     89 
     90 static void piixpcib_powerhook(int, void *);
     91 
     92 static void speedstep_configure(struct piixpcib_softc *,
     93 				struct pci_attach_args *);
     94 static int speedstep_sysctl_helper(SYSCTLFN_ARGS);
     95 
     96 struct piixpcib_softc *speedstep_cookie;	/* XXX */
     97 
     98 /* Defined in arch/i386/pci/pcib.c. */
     99 extern void pcibattach(struct device *, struct device *, void *);
    100 
    101 CFATTACH_DECL(piixpcib, sizeof(struct piixpcib_softc),
    102     piixpcibmatch, piixpcibattach, NULL, NULL);
    103 
    104 /*
    105  * Autoconf callbacks.
    106  */
    107 static int
    108 piixpcibmatch(struct device *parent, struct cfdata *match, void *aux)
    109 {
    110 	struct pci_attach_args *pa;
    111 
    112 	pa = (struct pci_attach_args *)aux;
    113 
    114 	/* We are ISA bridge, of course */
    115 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_BRIDGE ||
    116 	    (PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_ISA &&
    117 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_BRIDGE_MISC)) {
    118 		return 0;
    119 	}
    120 
    121 	/* Matches only Intel PIIX4 */
    122 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_INTEL) {
    123 		switch (PCI_PRODUCT(pa->pa_id)) {
    124 		case PCI_PRODUCT_INTEL_82371AB_ISA:	/* PIIX4 */
    125 		case PCI_PRODUCT_INTEL_82440MX_PMC:	/* PIIX4 in MX440 */
    126 			return 10;
    127 		}
    128 	}
    129 
    130 	return 0;
    131 }
    132 
    133 static void
    134 piixpcibattach(struct device *parent, struct device *self, void *aux)
    135 {
    136 	struct pci_attach_args *pa;
    137 	struct piixpcib_softc *sc;
    138 
    139 	pa = (struct pci_attach_args *)aux;
    140 	sc = (struct piixpcib_softc *)self;
    141 
    142 	sc->sc_pc = pa->pa_pc;
    143 	sc->sc_pcitag = pa->pa_tag;
    144 	sc->sc_iot = pa->pa_iot;
    145 
    146 	pcibattach(parent, self, aux);
    147 
    148 	/* Set up SpeedStep. */
    149 	speedstep_configure(sc, pa);
    150 
    151 	/* Map edge/level control registers */
    152 	if (bus_space_map(sc->sc_iot, PIIX_REG_ELCR, PIIX_REG_ELCR_SIZE, 0,
    153 	    &sc->sc_ioh)) {
    154 		aprint_error("%s: can't map edge/level control registers\n",
    155 		    sc->sc_dev.dv_xname);
    156 		return;
    157 	}
    158 
    159 	sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
    160 	    piixpcib_powerhook, sc);
    161 	if (sc->sc_powerhook == NULL)
    162 		aprint_error("%s: can't establish powerhook\n",
    163 		    sc->sc_dev.dv_xname);
    164 
    165 	return;
    166 }
    167 
    168 static void
    169 piixpcib_powerhook(int why, void *opaque)
    170 {
    171 	struct piixpcib_softc *sc;
    172 	pci_chipset_tag_t pc;
    173 	pcitag_t tag;
    174 
    175 	sc = (struct piixpcib_softc *)opaque;
    176 	pc = sc->sc_pc;
    177 	tag = sc->sc_pcitag;
    178 
    179 	switch (why) {
    180 	case PWR_SUSPEND:
    181 		pci_conf_capture(pc, tag, &sc->sc_pciconf);
    182 
    183 		/* capture PIRQX route control registers */
    184 		sc->sc_pirqrc = pci_conf_read(pc, tag, PIIX4_PIRQRC);
    185 
    186 		/* capture edge/level control registers */
    187 		sc->sc_elcr[0] = bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0);
    188 		sc->sc_elcr[1] = bus_space_read_1(sc->sc_iot, sc->sc_ioh, 1);
    189 		break;
    190 	case PWR_RESUME:
    191 		pci_conf_restore(pc, tag, &sc->sc_pciconf);
    192 
    193 		/* restore PIRQX route control registers */
    194 		pci_conf_write(pc, tag, PIIX4_PIRQRC, sc->sc_pirqrc);
    195 
    196 		/* restore edge/level control registers */
    197 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, 0, sc->sc_elcr[0]);
    198 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, 1, sc->sc_elcr[1]);
    199 		break;
    200 	}
    201 
    202 	return;
    203 }
    204 
    205 /*
    206  * Intel PIIX4 (SMI) SpeedStep support.
    207  */
    208 
    209 #define PIIXPCIB_GSIC		0x47534943
    210 #define	PIIXPCIB_GETOWNER	0
    211 #define	PIIXPCIB_GETSTATE	1
    212 #define	PIIXPCIB_SETSTATE	2
    213 #define	PIIXPCIB_GETFREQS	4
    214 
    215 #define	PIIXPCIB_SPEEDSTEP_HIGH	0
    216 #define	PIIXPCIB_SPEEDSTEP_LOW	1
    217 
    218 static void
    219 piixpcib_int15_gsic_call(int *sig, int *smicmd, int *cmd, int *smidata, int *flags)
    220 {
    221 	struct bioscallregs regs;
    222 
    223 	memset(&regs, 0, sizeof(struct bioscallregs));
    224 	regs.EAX = 0x0000e980;	/* IST support */
    225 	regs.EDX = PIIXPCIB_GSIC;
    226 	bioscall(0x15, &regs);
    227 
    228 	if (regs.EAX == PIIXPCIB_GSIC) {
    229 		*sig = regs.EAX;
    230 		*smicmd = regs.EBX & 0xff;
    231 		*cmd = (regs.EBX >> 16) & 0xff;
    232 		*smidata = regs.ECX;
    233 		*flags = regs.EDX;
    234 	} else
    235 		*sig = *smicmd = *cmd = *smidata = *flags = -1;
    236 
    237 	return;
    238 }
    239 
    240 static int
    241 piixpcib_set_ownership(struct piixpcib_softc *sc)
    242 {
    243 	int rv;
    244 	paddr_t pmagic;
    245 	static char magic[] = "Copyright (c) 1999 Intel Corporation";
    246 
    247 	pmagic = vtophys((vaddr_t)magic);
    248 
    249 	__asm__ __volatile__(
    250 	    "movl $0, %%edi\n\t"
    251 	    "out %%al, (%%dx)\n"
    252 	    : "=D" (rv)
    253 	    : "a" (sc->sc_command),
    254 	      "b" (0),
    255 	      "c" (0),
    256 	      "d" (sc->sc_smi_cmd),
    257 	      "S" (pmagic)
    258 	);
    259 
    260 	return (rv ? ENXIO : 0);
    261 }
    262 
    263 static int
    264 piixpcib_getset_state(struct piixpcib_softc *sc, int *state, int function)
    265 {
    266 	int new;
    267 	int rv;
    268 	int eax;
    269 
    270 #ifdef DIAGNOSTIC
    271 	if (function != PIIXPCIB_GETSTATE &&
    272 	    function != PIIXPCIB_SETSTATE) {
    273 		aprint_error("%s: GSI called with invalid function %d\n",
    274 		    sc->sc_dev.dv_xname, function);
    275 		return EINVAL;
    276 	}
    277 #endif
    278 
    279 	__asm__ __volatile__(
    280 	    "movl $0, %%edi\n\t"
    281 	    "out %%al, (%%dx)\n"
    282 	    : "=a" (eax),
    283 	      "=b" (new),
    284 	      "=D" (rv)
    285 	    : "a" (sc->sc_command),
    286 	      "b" (function),
    287 	      "c" (*state),
    288 	      "d" (sc->sc_smi_cmd),
    289 	      "S" (0)
    290 	);
    291 
    292 	*state = new & 1;
    293 
    294 	switch (function) {
    295 	case PIIXPCIB_GETSTATE:
    296 		if (eax)
    297 			return ENXIO;
    298 		break;
    299 	case PIIXPCIB_SETSTATE:
    300 		if (rv)
    301 			return ENXIO;
    302 		break;
    303 	}
    304 
    305 	return 0;
    306 }
    307 
    308 static int
    309 piixpcib_get(struct piixpcib_softc *sc)
    310 {
    311 	int rv;
    312 	int state;
    313 
    314 	state = 0; 	/* XXX gcc */
    315 
    316 	rv = piixpcib_getset_state(sc, &state, PIIXPCIB_GETSTATE);
    317 	if (rv)
    318 		return rv;
    319 
    320 	return state & 1;
    321 }
    322 
    323 static int
    324 piixpcib_set(struct piixpcib_softc *sc, int state)
    325 {
    326 	int rv, s;
    327 	int try;
    328 
    329 	if (state != PIIXPCIB_SPEEDSTEP_HIGH &&
    330 	    state != PIIXPCIB_SPEEDSTEP_LOW)
    331 		return ENXIO;
    332 	if (piixpcib_get(sc) == state)
    333 		return 0;
    334 
    335 	try = 5;
    336 
    337 	s = splhigh();
    338 
    339 	do {
    340 		rv = piixpcib_getset_state(sc, &state, PIIXPCIB_SETSTATE);
    341 		if (rv)
    342 			delay(200);
    343 	} while (rv && --try);
    344 
    345 	splx(s);
    346 
    347 	return rv;
    348 }
    349 
    350 static void
    351 speedstep_configure(struct piixpcib_softc *sc, struct pci_attach_args *pa)
    352 {
    353 	const struct sysctlnode	*node, *ssnode;
    354 	int sig, smicmd, cmd, smidata, flags;
    355 	int rv;
    356 
    357 	piixpcib_int15_gsic_call(&sig, &smicmd, &cmd, &smidata, &flags);
    358 
    359 	if (sig != -1) {
    360 		sc->sc_smi_cmd = smicmd;
    361 		sc->sc_smi_data = smidata;
    362 		if (cmd == 0x80) {
    363 			aprint_debug("%s: GSIC returned cmd 0x80, should be 0x82\n",
    364 			    sc->sc_dev.dv_xname);
    365 			cmd = 0x82;
    366 		}
    367 		sc->sc_command = (sig & 0xffffff00) | (cmd & 0xff);
    368 		sc->sc_flags = flags;
    369 	} else {
    370 		/* setup some defaults */
    371 		sc->sc_smi_cmd = 0xb2;
    372 		sc->sc_smi_data = 0xb3;
    373 		sc->sc_command = 0x47534982;
    374 		sc->sc_flags = 0;
    375 	}
    376 
    377 	if (piixpcib_set_ownership(sc) != 0) {
    378 		aprint_error("%s: unable to claim ownership from the BIOS\n",
    379 		    sc->sc_dev.dv_xname);
    380 		return;		/* If we can't claim ownership from the BIOS, bail */
    381 	}
    382 
    383 	/* Put in machdep.speedstep_state (0 for low, 1 for high). */
    384 	if ((rv = sysctl_createv(NULL, 0, NULL, &node,
    385 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
    386 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL)) != 0)
    387 		goto err;
    388 
    389 	/* CTLFLAG_ANYWRITE? kernel option like EST? */
    390 	if ((rv = sysctl_createv(NULL, 0, &node, &ssnode,
    391 	    CTLFLAG_READWRITE, CTLTYPE_INT, "speedstep_state", NULL,
    392 	    speedstep_sysctl_helper, 0, NULL, 0, CTL_CREATE,
    393 	    CTL_EOL)) != 0)
    394 		goto err;
    395 
    396 	/* XXX save the sc for IO tag/handle */
    397 	speedstep_cookie = sc;
    398 
    399 	aprint_verbose("%s: SpeedStep SMI enabled\n", sc->sc_dev.dv_xname);
    400 
    401 	return;
    402 
    403 err:
    404 	aprint_normal("%s: sysctl_createv failed (rv = %d)\n", __func__, rv);
    405 
    406 	return;
    407 }
    408 
    409 /*
    410  * get/set the SpeedStep state: 0 == low power, 1 == high power.
    411  */
    412 static int
    413 speedstep_sysctl_helper(SYSCTLFN_ARGS)
    414 {
    415 	struct sysctlnode node;
    416 	struct piixpcib_softc *sc;
    417 	uint8_t	state, state2;
    418 	int ostate, nstate, error;
    419 
    420 	sc = speedstep_cookie;
    421 	error = 0;
    422 
    423 	state = piixpcib_get(sc);
    424 	if (state == PIIXPCIB_SPEEDSTEP_HIGH)
    425 		ostate = 1;
    426 	else
    427 		ostate = 0;
    428 	nstate = ostate;
    429 
    430 	node = *rnode;
    431 	node.sysctl_data = &nstate;
    432 
    433 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    434 	if (error || newp == NULL)
    435 		goto out;
    436 
    437 	/* Only two states are available */
    438 	if (nstate != 0 && nstate != 1) {
    439 		error = EINVAL;
    440 		goto out;
    441 	}
    442 
    443 	state2 = piixpcib_get(sc);
    444 	if (state2 == PIIXPCIB_SPEEDSTEP_HIGH)
    445 		ostate = 1;
    446 	else
    447 		ostate = 0;
    448 
    449 	if (ostate != nstate)
    450 	{
    451 		if (nstate == 0)
    452 			state2 = PIIXPCIB_SPEEDSTEP_LOW;
    453 		else
    454 			state2 = PIIXPCIB_SPEEDSTEP_HIGH;
    455 
    456 		error = piixpcib_set(sc, state2);
    457 	}
    458 out:
    459 	return (error);
    460 }
    461