Home | History | Annotate | Line # | Download | only in pci
amdpm.c revision 1.29.4.2
      1 /*	$NetBSD: amdpm.c,v 1.29.4.2 2009/05/16 10:41:32 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.2 2009/05/16 10:41:32 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(device_t parent, cfdata_t match, void *aux)
     65 {
     66 	struct pci_attach_args *pa = aux;
     67 
     68 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD) {
     69 		switch (PCI_PRODUCT(pa->pa_id)) {
     70 		case PCI_PRODUCT_AMD_PBC768_PMC:
     71 		case PCI_PRODUCT_AMD_PBC8111_ACPI:
     72 			return (1);
     73 		}
     74 	}
     75 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NVIDIA) {
     76 		switch (PCI_PRODUCT(pa->pa_id)) {
     77 		case PCI_PRODUCT_NVIDIA_XBOX_SMBUS:
     78 			return (1);
     79 		}
     80 	}
     81 
     82 	return (0);
     83 }
     84 
     85 static void
     86 amdpm_attach(device_t parent, device_t self, void *aux)
     87 {
     88 	struct amdpm_softc *sc = device_private(self);
     89 	struct pci_attach_args *pa = aux;
     90 	char devinfo[256];
     91 	pcireg_t confreg, pmptrreg;
     92 	u_int32_t pmreg;
     93 	int i;
     94 
     95 	aprint_naive("\n");
     96 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
     97 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
     98 	    PCI_REVISION(pa->pa_class));
     99 
    100 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NVIDIA_XBOX_SMBUS)
    101 		sc->sc_nforce = 1;
    102 	else
    103 		sc->sc_nforce = 0;
    104 
    105 	sc->sc_pc = pa->pa_pc;
    106 	sc->sc_tag = pa->pa_tag;
    107 	sc->sc_iot = pa->pa_iot;
    108 	sc->sc_pa = pa;
    109 
    110 #if 0
    111 	aprint_normal_dev(&sc->sc_dev, "");
    112 	pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
    113 #endif
    114 
    115 	confreg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
    116 	/* enable pm i/o space for AMD-8111 and nForce */
    117 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC8111_ACPI ||
    118 	    sc->sc_nforce)
    119 		confreg |= AMDPM_PMIOEN;
    120 
    121 	/* Enable random number generation for everyone */
    122 	pci_conf_write(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG,
    123 	    confreg | AMDPM_RNGEN);
    124 	confreg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
    125 
    126 	if ((confreg & AMDPM_PMIOEN) == 0) {
    127 		aprint_error_dev(&sc->sc_dev, "PMxx space isn't enabled\n");
    128 		return;
    129 	}
    130 
    131 	if (sc->sc_nforce) {
    132 		pmptrreg = pci_conf_read(pa->pa_pc, pa->pa_tag, NFORCE_PMPTR);
    133 		aprint_normal_dev(&sc->sc_dev, "power management at 0x%04x\n",
    134 		    NFORCE_PMBASE(pmptrreg));
    135 		if (bus_space_map(sc->sc_iot, NFORCE_PMBASE(pmptrreg),
    136 		    AMDPM_PMSIZE, 0, &sc->sc_ioh)) {
    137 			aprint_error_dev(&sc->sc_dev, "failed to map PMxx space\n");
    138 			return;
    139 		}
    140 	} else {
    141 		pmptrreg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_PMPTR);
    142 		if (bus_space_map(sc->sc_iot, AMDPM_PMBASE(pmptrreg),
    143 		    AMDPM_PMSIZE, 0, &sc->sc_ioh)) {
    144 			aprint_error_dev(&sc->sc_dev, "failed to map PMxx space\n");
    145 			return;
    146 		}
    147 	}
    148 
    149 	/* don't attach a timecounter on nforce boards */
    150 	if ((confreg & AMDPM_TMRRST) == 0 && (confreg & AMDPM_STOPTMR) == 0 &&
    151 	    !sc->sc_nforce) {
    152 		acpipmtimer_attach(&sc->sc_dev, sc->sc_iot, sc->sc_ioh,
    153 		  AMDPM_TMR, ((confreg & AMDPM_TMR32) ? ACPIPMT_32BIT : 0));
    154 	}
    155 
    156 	/* try to attach devices on the smbus */
    157 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC8111_ACPI ||
    158 	    sc->sc_nforce) {
    159 		amdpm_smbus_attach(sc);
    160 	}
    161 
    162 	if (confreg & AMDPM_RNGEN) {
    163 		/* Check to see if we can read data from the RNG. */
    164 		(void) bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    165 		    AMDPM_RNGDATA);
    166 		for (i = 0; i < 1000; i++) {
    167 			pmreg = bus_space_read_4(sc->sc_iot,
    168 			    sc->sc_ioh, AMDPM_RNGSTAT);
    169 			if (pmreg & AMDPM_RNGDONE)
    170 				break;
    171 			delay(1);
    172 		}
    173 		if ((pmreg & AMDPM_RNGDONE) != 0) {
    174 			aprint_normal_dev(&sc->sc_dev, ""
    175 			    "random number generator enabled (apprx. %dms)\n",
    176 			    i);
    177 			callout_init(&sc->sc_rnd_ch, 0);
    178 			rnd_attach_source(&sc->sc_rnd_source,
    179 			    device_xname(&sc->sc_dev), RND_TYPE_RNG,
    180 			    /*
    181 			     * XXX Careful!  The use of RND_FLAG_NO_ESTIMATE
    182 			     * XXX here is unobvious: we later feed raw bits
    183 			     * XXX into the "entropy pool" with rnd_add_data,
    184 			     * XXX explicitly supplying an entropy estimate.
    185 			     * XXX In this context, NO_ESTIMATE serves only
    186 			     * XXX to prevent rnd_add_data from trying to
    187 			     * XXX use the *time at which we added the data*
    188 			     * XXX as entropy, which is not a good idea since
    189 			     * XXX we add data periodically from a callout.
    190 			     */
    191 			    RND_FLAG_NO_ESTIMATE);
    192 #ifdef AMDPM_RND_COUNTERS
    193 			evcnt_attach_dynamic(&sc->sc_rnd_hits, EVCNT_TYPE_MISC,
    194 			    NULL, device_xname(&sc->sc_dev), "rnd hits");
    195 			evcnt_attach_dynamic(&sc->sc_rnd_miss, EVCNT_TYPE_MISC,
    196 			    NULL, device_xname(&sc->sc_dev), "rnd miss");
    197 			for (i = 0; i < 256; i++) {
    198 				evcnt_attach_dynamic(&sc->sc_rnd_data[i],
    199 				    EVCNT_TYPE_MISC, NULL, device_xname(&sc->sc_dev),
    200 				    "rnd data");
    201 			}
    202 #endif
    203 			amdpm_rnd_callout(sc);
    204 		}
    205 	}
    206 }
    207 
    208 CFATTACH_DECL(amdpm, sizeof(struct amdpm_softc),
    209     amdpm_match, amdpm_attach, NULL, NULL);
    210 
    211 static void
    212 amdpm_rnd_callout(void *v)
    213 {
    214 	struct amdpm_softc *sc = v;
    215 	u_int32_t rngreg;
    216 #ifdef AMDPM_RND_COUNTERS
    217 	int i;
    218 #endif
    219 
    220 	if ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGSTAT) &
    221 	    AMDPM_RNGDONE) != 0) {
    222 		rngreg = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    223 		    AMDPM_RNGDATA);
    224 		rnd_add_data(&sc->sc_rnd_source, &rngreg,
    225 		    sizeof(rngreg), sizeof(rngreg) * NBBY);
    226 #ifdef AMDPM_RND_COUNTERS
    227 		AMDPM_RNDCNT_INCR(&sc->sc_rnd_hits);
    228 		for (i = 0; i < sizeof(rngreg); i++, rngreg >>= NBBY)
    229 			AMDPM_RNDCNT_INCR(&sc->sc_rnd_data[rngreg & 0xff]);
    230 #endif
    231 	}
    232 #ifdef AMDPM_RND_COUNTERS
    233 	else
    234 		AMDPM_RNDCNT_INCR(&sc->sc_rnd_miss);
    235 #endif
    236 	callout_reset(&sc->sc_rnd_ch, 1, amdpm_rnd_callout, sc);
    237 }
    238