Home | History | Annotate | Line # | Download | only in pci
amdpm.c revision 1.29.4.1
      1 /*	$NetBSD: amdpm.c,v 1.29.4.1 2008/05/16 02:24:42 yamt Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Enami Tsugutomo.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: amdpm.c,v 1.29.4.1 2008/05/16 02:24:42 yamt Exp $");
     34 
     35 #include "opt_amdpm.h"
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/device.h>
     41 #include <sys/callout.h>
     42 #include <sys/rnd.h>
     43 
     44 #include <sys/bus.h>
     45 #include <dev/ic/acpipmtimer.h>
     46 
     47 #include <dev/i2c/i2cvar.h>
     48 
     49 #include <dev/pci/pcivar.h>
     50 #include <dev/pci/pcireg.h>
     51 #include <dev/pci/pcidevs.h>
     52 
     53 #include <dev/pci/amdpmreg.h>
     54 #include <dev/pci/amdpmvar.h>
     55 #include <dev/pci/amdpm_smbusreg.h>
     56 
     57 static void	amdpm_rnd_callout(void *);
     58 
     59 #ifdef AMDPM_RND_COUNTERS
     60 #define	AMDPM_RNDCNT_INCR(ev)	(ev)->ev_count++
     61 #endif
     62 
     63 static int
     64 amdpm_match(struct device *parent, struct cfdata *match,
     65     void *aux)
     66 {
     67 	struct pci_attach_args *pa = aux;
     68 
     69 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD) {
     70 		switch (PCI_PRODUCT(pa->pa_id)) {
     71 		case PCI_PRODUCT_AMD_PBC768_PMC:
     72 		case PCI_PRODUCT_AMD_PBC8111_ACPI:
     73 			return (1);
     74 		}
     75 	}
     76 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NVIDIA) {
     77 		switch (PCI_PRODUCT(pa->pa_id)) {
     78 		case PCI_PRODUCT_NVIDIA_XBOX_SMBUS:
     79 			return (1);
     80 		}
     81 	}
     82 
     83 	return (0);
     84 }
     85 
     86 static void
     87 amdpm_attach(struct device *parent, struct device *self, void *aux)
     88 {
     89 	struct amdpm_softc *sc = (struct amdpm_softc *) self;
     90 	struct pci_attach_args *pa = aux;
     91 	char devinfo[256];
     92 	pcireg_t confreg, pmptrreg;
     93 	u_int32_t pmreg;
     94 	int i;
     95 
     96 	aprint_naive("\n");
     97 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
     98 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
     99 	    PCI_REVISION(pa->pa_class));
    100 
    101 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NVIDIA_XBOX_SMBUS)
    102 		sc->sc_nforce = 1;
    103 	else
    104 		sc->sc_nforce = 0;
    105 
    106 	sc->sc_pc = pa->pa_pc;
    107 	sc->sc_tag = pa->pa_tag;
    108 	sc->sc_iot = pa->pa_iot;
    109 	sc->sc_pa = pa;
    110 
    111 #if 0
    112 	aprint_normal_dev(&sc->sc_dev, "");
    113 	pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
    114 #endif
    115 
    116 	confreg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
    117 	/* enable pm i/o space for AMD-8111 and nForce */
    118 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC8111_ACPI ||
    119 	    sc->sc_nforce)
    120 		confreg |= AMDPM_PMIOEN;
    121 
    122 	/* Enable random number generation for everyone */
    123 	pci_conf_write(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG,
    124 	    confreg | AMDPM_RNGEN);
    125 	confreg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
    126 
    127 	if ((confreg & AMDPM_PMIOEN) == 0) {
    128 		aprint_error_dev(&sc->sc_dev, "PMxx space isn't enabled\n");
    129 		return;
    130 	}
    131 
    132 	if (sc->sc_nforce) {
    133 		pmptrreg = pci_conf_read(pa->pa_pc, pa->pa_tag, NFORCE_PMPTR);
    134 		aprint_normal_dev(&sc->sc_dev, "power management at 0x%04x\n",
    135 		    NFORCE_PMBASE(pmptrreg));
    136 		if (bus_space_map(sc->sc_iot, NFORCE_PMBASE(pmptrreg),
    137 		    AMDPM_PMSIZE, 0, &sc->sc_ioh)) {
    138 			aprint_error_dev(&sc->sc_dev, "failed to map PMxx space\n");
    139 			return;
    140 		}
    141 	} else {
    142 		pmptrreg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_PMPTR);
    143 		if (bus_space_map(sc->sc_iot, AMDPM_PMBASE(pmptrreg),
    144 		    AMDPM_PMSIZE, 0, &sc->sc_ioh)) {
    145 			aprint_error_dev(&sc->sc_dev, "failed to map PMxx space\n");
    146 			return;
    147 		}
    148 	}
    149 
    150 	/* don't attach a timecounter on nforce boards */
    151 	if ((confreg & AMDPM_TMRRST) == 0 && (confreg & AMDPM_STOPTMR) == 0 &&
    152 	    !sc->sc_nforce) {
    153 		acpipmtimer_attach(&sc->sc_dev, sc->sc_iot, sc->sc_ioh,
    154 		  AMDPM_TMR, ((confreg & AMDPM_TMR32) ? ACPIPMT_32BIT : 0));
    155 	}
    156 
    157 	/* try to attach devices on the smbus */
    158 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC8111_ACPI ||
    159 	    sc->sc_nforce) {
    160 		amdpm_smbus_attach(sc);
    161 	}
    162 
    163 	if (confreg & AMDPM_RNGEN) {
    164 		/* Check to see if we can read data from the RNG. */
    165 		(void) bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    166 		    AMDPM_RNGDATA);
    167 		for (i = 0; i < 1000; i++) {
    168 			pmreg = bus_space_read_4(sc->sc_iot,
    169 			    sc->sc_ioh, AMDPM_RNGSTAT);
    170 			if (pmreg & AMDPM_RNGDONE)
    171 				break;
    172 			delay(1);
    173 		}
    174 		if ((pmreg & AMDPM_RNGDONE) != 0) {
    175 			aprint_normal_dev(&sc->sc_dev, ""
    176 			    "random number generator enabled (apprx. %dms)\n",
    177 			    i);
    178 			callout_init(&sc->sc_rnd_ch, 0);
    179 			rnd_attach_source(&sc->sc_rnd_source,
    180 			    device_xname(&sc->sc_dev), RND_TYPE_RNG,
    181 			    /*
    182 			     * XXX Careful!  The use of RND_FLAG_NO_ESTIMATE
    183 			     * XXX here is unobvious: we later feed raw bits
    184 			     * XXX into the "entropy pool" with rnd_add_data,
    185 			     * XXX explicitly supplying an entropy estimate.
    186 			     * XXX In this context, NO_ESTIMATE serves only
    187 			     * XXX to prevent rnd_add_data from trying to
    188 			     * XXX use the *time at which we added the data*
    189 			     * XXX as entropy, which is not a good idea since
    190 			     * XXX we add data periodically from a callout.
    191 			     */
    192 			    RND_FLAG_NO_ESTIMATE);
    193 #ifdef AMDPM_RND_COUNTERS
    194 			evcnt_attach_dynamic(&sc->sc_rnd_hits, EVCNT_TYPE_MISC,
    195 			    NULL, device_xname(&sc->sc_dev), "rnd hits");
    196 			evcnt_attach_dynamic(&sc->sc_rnd_miss, EVCNT_TYPE_MISC,
    197 			    NULL, device_xname(&sc->sc_dev), "rnd miss");
    198 			for (i = 0; i < 256; i++) {
    199 				evcnt_attach_dynamic(&sc->sc_rnd_data[i],
    200 				    EVCNT_TYPE_MISC, NULL, device_xname(&sc->sc_dev),
    201 				    "rnd data");
    202 			}
    203 #endif
    204 			amdpm_rnd_callout(sc);
    205 		}
    206 	}
    207 }
    208 
    209 CFATTACH_DECL(amdpm, sizeof(struct amdpm_softc),
    210     amdpm_match, amdpm_attach, NULL, NULL);
    211 
    212 static void
    213 amdpm_rnd_callout(void *v)
    214 {
    215 	struct amdpm_softc *sc = v;
    216 	u_int32_t rngreg;
    217 #ifdef AMDPM_RND_COUNTERS
    218 	int i;
    219 #endif
    220 
    221 	if ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGSTAT) &
    222 	    AMDPM_RNGDONE) != 0) {
    223 		rngreg = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    224 		    AMDPM_RNGDATA);
    225 		rnd_add_data(&sc->sc_rnd_source, &rngreg,
    226 		    sizeof(rngreg), sizeof(rngreg) * NBBY);
    227 #ifdef AMDPM_RND_COUNTERS
    228 		AMDPM_RNDCNT_INCR(&sc->sc_rnd_hits);
    229 		for (i = 0; i < sizeof(rngreg); i++, rngreg >>= NBBY)
    230 			AMDPM_RNDCNT_INCR(&sc->sc_rnd_data[rngreg & 0xff]);
    231 #endif
    232 	}
    233 #ifdef AMDPM_RND_COUNTERS
    234 	else
    235 		AMDPM_RNDCNT_INCR(&sc->sc_rnd_miss);
    236 #endif
    237 	callout_reset(&sc->sc_rnd_ch, 1, amdpm_rnd_callout, sc);
    238 }
    239