Home | History | Annotate | Line # | Download | only in pci
amdpm.c revision 1.9.2.2
      1 /*	$NetBSD: amdpm.c,v 1.9.2.2 2006/03/01 09:28:21 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  * 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 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: amdpm.c,v 1.9.2.2 2006/03/01 09:28:21 yamt Exp $");
     41 
     42 #include "opt_amdpm.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/device.h>
     48 #include <sys/callout.h>
     49 #include <sys/rnd.h>
     50 
     51 #include <dev/i2c/i2cvar.h>
     52 
     53 #include <dev/pci/pcivar.h>
     54 #include <dev/pci/pcireg.h>
     55 #include <dev/pci/pcidevs.h>
     56 
     57 #include <dev/pci/amdpmreg.h>
     58 #include <dev/pci/amdpmvar.h>
     59 #include <dev/pci/amdpm_smbusreg.h>
     60 
     61 static void	amdpm_rnd_callout(void *);
     62 
     63 #ifdef AMDPM_RND_COUNTERS
     64 #define	AMDPM_RNDCNT_INCR(ev)	(ev)->ev_count++
     65 #else
     66 #define	AMDPM_RNDCNT_INCR(ev)	/* nothing */
     67 #endif
     68 
     69 static int
     70 amdpm_match(struct device *parent, struct cfdata *match, void *aux)
     71 {
     72 	struct pci_attach_args *pa = aux;
     73 
     74 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_AMD)
     75 		return (0);
     76 
     77 	switch (PCI_PRODUCT(pa->pa_id)) {
     78 	case PCI_PRODUCT_AMD_PBC768_PMC:
     79 	case PCI_PRODUCT_AMD_PBC8111_ACPI:
     80 		return (1);
     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 reg;
     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 	sc->sc_pc = pa->pa_pc;
    102 	sc->sc_tag = pa->pa_tag;
    103 	sc->sc_iot = pa->pa_iot;
    104 
    105 #if 0
    106 	aprint_normal("%s: ", sc->sc_dev.dv_xname);
    107 	pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
    108 #endif
    109 
    110 	/* enable random # generation and pm i/o space for AMD-8111 */
    111 	if (PCI_PRODUCT(pa->pa_id)  == PCI_PRODUCT_AMD_PBC8111_ACPI) {
    112 		reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
    113 		pci_conf_write(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG, reg|
    114 		    AMDPM_RNGEN|AMDPM_PMIOEN);
    115 	}
    116 
    117 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
    118 	if ((reg & AMDPM_PMIOEN) == 0) {
    119 		aprint_error("%s: PMxx space isn't enabled\n",
    120 		    sc->sc_dev.dv_xname);
    121 		return;
    122 	}
    123 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_PMPTR);
    124 	if (bus_space_map(sc->sc_iot, AMDPM_PMBASE(reg), AMDPM_PMSIZE,
    125 	    0, &sc->sc_ioh)) {
    126 		aprint_error("%s: failed to map PMxx space\n",
    127 		    sc->sc_dev.dv_xname);
    128 		return;
    129 	}
    130 
    131 	/* try to attach devices on the smbus */
    132 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC8111_ACPI) {
    133 		amdpm_smbus_attach(sc);
    134 	}
    135 
    136 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
    137 	pci_conf_write(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG, reg | AMDPM_RNGEN);
    138 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
    139 	if (reg & AMDPM_RNGEN) {
    140 		/* Check to see if we can read data from the RNG. */
    141 		(void) bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    142 		    AMDPM_RNGDATA);
    143 		for (i = 0; i < 1000; i++) {
    144 			pmreg = bus_space_read_4(sc->sc_iot,
    145 			    sc->sc_ioh, AMDPM_RNGSTAT);
    146 			if (pmreg & AMDPM_RNGDONE)
    147 				break;
    148 			delay(1);
    149 		}
    150 		if ((pmreg & AMDPM_RNGDONE) != 0) {
    151 			aprint_normal("%s: "
    152 			    "random number generator enabled (apprx. %dms)\n",
    153 			    sc->sc_dev.dv_xname, i);
    154 			callout_init(&sc->sc_rnd_ch);
    155 			rnd_attach_source(&sc->sc_rnd_source,
    156 			    sc->sc_dev.dv_xname, RND_TYPE_RNG,
    157 			    /*
    158 			     * XXX Careful!  The use of RND_FLAG_NO_ESTIMATE
    159 			     * XXX here is unobvious: we later feed raw bits
    160 			     * XXX into the "entropy pool" with rnd_add_data,
    161 			     * XXX explicitly supplying an entropy estimate.
    162 			     * XXX In this context, NO_ESTIMATE serves only
    163 			     * XXX to prevent rnd_add_data from trying to
    164 			     * XXX use the *time at which we added the data*
    165 			     * XXX as entropy, which is not a good idea since
    166 			     * XXX we add data periodically from a callout.
    167 			     */
    168 			    RND_FLAG_NO_ESTIMATE);
    169 #ifdef AMDPM_RND_COUNTERS
    170 			evcnt_attach_dynamic(&sc->sc_rnd_hits, EVCNT_TYPE_MISC,
    171 			    NULL, sc->sc_dev.dv_xname, "rnd hits");
    172 			evcnt_attach_dynamic(&sc->sc_rnd_miss, EVCNT_TYPE_MISC,
    173 			    NULL, sc->sc_dev.dv_xname, "rnd miss");
    174 			for (i = 0; i < 256; i++) {
    175 				evcnt_attach_dynamic(&sc->sc_rnd_data[i],
    176 				    EVCNT_TYPE_MISC, NULL, sc->sc_dev.dv_xname,
    177 				    "rnd data");
    178 			}
    179 #endif
    180 			amdpm_rnd_callout(sc);
    181 		}
    182 	}
    183 }
    184 
    185 CFATTACH_DECL(amdpm, sizeof(struct amdpm_softc),
    186     amdpm_match, amdpm_attach, NULL, NULL);
    187 
    188 static void
    189 amdpm_rnd_callout(void *v)
    190 {
    191 	struct amdpm_softc *sc = v;
    192 	u_int32_t reg;
    193 #ifdef AMDPM_RND_COUNTERS
    194 	int i;
    195 #endif
    196 
    197 	if ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGSTAT) &
    198 	    AMDPM_RNGDONE) != 0) {
    199 		reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGDATA);
    200 		rnd_add_data(&sc->sc_rnd_source, &reg,
    201 		    sizeof(reg), sizeof(reg) * NBBY);
    202 #ifdef AMDPM_RND_COUNTERS
    203 		AMDPM_RNDCNT_INCR(&sc->sc_rnd_hits);
    204 		for (i = 0; i < sizeof(reg); i++, reg >>= NBBY)
    205 			AMDPM_RNDCNT_INCR(&sc->sc_rnd_data[reg & 0xff]);
    206 #endif
    207 	} else
    208 		AMDPM_RNDCNT_INCR(&sc->sc_rnd_miss);
    209 	callout_reset(&sc->sc_rnd_ch, 1, amdpm_rnd_callout, sc);
    210 }
    211