Home | History | Annotate | Line # | Download | only in pci
amdpm.c revision 1.12
      1  1.12   xtraeme /*	$NetBSD: amdpm.c,v 1.12 2006/06/17 15:05:15 xtraeme Exp $	*/
      2   1.1     enami 
      3   1.1     enami /*-
      4   1.1     enami  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5   1.1     enami  * All rights reserved.
      6   1.1     enami  *
      7   1.1     enami  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1     enami  * by Enami Tsugutomo.
      9   1.1     enami  *
     10   1.1     enami  * Redistribution and use in source and binary forms, with or without
     11   1.1     enami  * modification, are permitted provided that the following conditions
     12   1.1     enami  * are met:
     13   1.1     enami  * 1. Redistributions of source code must retain the above copyright
     14   1.1     enami  *    notice, this list of conditions and the following disclaimer.
     15   1.1     enami  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     enami  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     enami  *    documentation and/or other materials provided with the distribution.
     18   1.1     enami  * 3. All advertising materials mentioning features or use of this software
     19   1.1     enami  *    must display the following acknowledgement:
     20   1.1     enami  *	This product includes software developed by the NetBSD
     21   1.1     enami  *	Foundation, Inc. and its contributors.
     22   1.1     enami  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1     enami  *    contributors may be used to endorse or promote products derived
     24   1.1     enami  *    from this software without specific prior written permission.
     25   1.1     enami  *
     26   1.1     enami  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1     enami  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1     enami  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1     enami  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1     enami  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1     enami  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1     enami  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1     enami  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1     enami  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1     enami  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1     enami  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1     enami  */
     38   1.1     enami 
     39   1.1     enami #include <sys/cdefs.h>
     40  1.12   xtraeme __KERNEL_RCSID(0, "$NetBSD: amdpm.c,v 1.12 2006/06/17 15:05:15 xtraeme Exp $");
     41   1.1     enami 
     42   1.1     enami #include "opt_amdpm.h"
     43   1.1     enami 
     44   1.1     enami #include <sys/param.h>
     45   1.1     enami #include <sys/systm.h>
     46   1.1     enami #include <sys/kernel.h>
     47   1.1     enami #include <sys/device.h>
     48   1.1     enami #include <sys/callout.h>
     49   1.1     enami #include <sys/rnd.h>
     50   1.1     enami 
     51  1.12   xtraeme #ifdef __HAVE_TIMECOUNTER
     52  1.12   xtraeme #include <sys/timetc.h>
     53  1.12   xtraeme #include <machine/bus.h>
     54  1.12   xtraeme #endif
     55  1.12   xtraeme 
     56  1.11       tls #include <dev/i2c/i2cvar.h>
     57  1.11       tls 
     58   1.1     enami #include <dev/pci/pcivar.h>
     59   1.1     enami #include <dev/pci/pcireg.h>
     60   1.1     enami #include <dev/pci/pcidevs.h>
     61   1.1     enami 
     62   1.1     enami #include <dev/pci/amdpmreg.h>
     63  1.11       tls #include <dev/pci/amdpmvar.h>
     64  1.11       tls #include <dev/pci/amdpm_smbusreg.h>
     65   1.1     enami 
     66   1.8   thorpej static void	amdpm_rnd_callout(void *);
     67   1.1     enami 
     68  1.12   xtraeme #ifdef __HAVE_TIMECOUNTER
     69  1.12   xtraeme uint32_t	amdpm_get_timecount(struct timecounter *);
     70  1.12   xtraeme 
     71  1.12   xtraeme #ifndef AMDPM_FREQUENCY
     72  1.12   xtraeme #define AMDPM_FREQUENCY	3579545
     73  1.12   xtraeme #endif
     74  1.12   xtraeme 
     75  1.12   xtraeme static struct timecounter amdpm_timecounter = {
     76  1.12   xtraeme 	amdpm_get_timecount,
     77  1.12   xtraeme 	0,
     78  1.12   xtraeme 	0xffffff,
     79  1.12   xtraeme 	AMDPM_FREQUENCY,
     80  1.12   xtraeme 	"amdpm",
     81  1.12   xtraeme 	1000
     82  1.12   xtraeme };
     83  1.12   xtraeme #endif
     84  1.12   xtraeme 
     85   1.1     enami #ifdef AMDPM_RND_COUNTERS
     86   1.1     enami #define	AMDPM_RNDCNT_INCR(ev)	(ev)->ev_count++
     87   1.1     enami #else
     88   1.1     enami #define	AMDPM_RNDCNT_INCR(ev)	/* nothing */
     89   1.1     enami #endif
     90   1.1     enami 
     91   1.8   thorpej static int
     92   1.1     enami amdpm_match(struct device *parent, struct cfdata *match, void *aux)
     93   1.1     enami {
     94   1.1     enami 	struct pci_attach_args *pa = aux;
     95   1.1     enami 
     96  1.10    kleink 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_AMD)
     97  1.10    kleink 		return (0);
     98  1.10    kleink 
     99  1.10    kleink 	switch (PCI_PRODUCT(pa->pa_id)) {
    100  1.10    kleink 	case PCI_PRODUCT_AMD_PBC768_PMC:
    101  1.10    kleink 	case PCI_PRODUCT_AMD_PBC8111_ACPI:
    102   1.1     enami 		return (1);
    103  1.10    kleink 	}
    104  1.10    kleink 
    105   1.1     enami 	return (0);
    106   1.1     enami }
    107   1.1     enami 
    108   1.8   thorpej static void
    109   1.1     enami amdpm_attach(struct device *parent, struct device *self, void *aux)
    110   1.1     enami {
    111   1.1     enami 	struct amdpm_softc *sc = (struct amdpm_softc *) self;
    112   1.1     enami 	struct pci_attach_args *pa = aux;
    113  1.10    kleink 	char devinfo[256];
    114   1.1     enami 	pcireg_t reg;
    115   1.1     enami 	u_int32_t pmreg;
    116   1.1     enami 	int i;
    117   1.1     enami 
    118   1.5   thorpej 	aprint_naive("\n");
    119  1.10    kleink 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    120  1.10    kleink 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
    121  1.10    kleink 	    PCI_REVISION(pa->pa_class));
    122   1.1     enami 
    123   1.1     enami 	sc->sc_pc = pa->pa_pc;
    124   1.1     enami 	sc->sc_tag = pa->pa_tag;
    125   1.1     enami 	sc->sc_iot = pa->pa_iot;
    126   1.1     enami 
    127   1.1     enami #if 0
    128   1.5   thorpej 	aprint_normal("%s: ", sc->sc_dev.dv_xname);
    129   1.1     enami 	pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
    130   1.1     enami #endif
    131   1.1     enami 
    132  1.11       tls 	/* enable random # generation and pm i/o space for AMD-8111 */
    133  1.11       tls 	if (PCI_PRODUCT(pa->pa_id)  == PCI_PRODUCT_AMD_PBC8111_ACPI) {
    134  1.11       tls 		reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
    135  1.11       tls 		pci_conf_write(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG, reg|
    136  1.11       tls 		    AMDPM_RNGEN|AMDPM_PMIOEN);
    137  1.11       tls 	}
    138  1.11       tls 
    139   1.1     enami 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
    140   1.1     enami 	if ((reg & AMDPM_PMIOEN) == 0) {
    141   1.5   thorpej 		aprint_error("%s: PMxx space isn't enabled\n",
    142   1.5   thorpej 		    sc->sc_dev.dv_xname);
    143   1.1     enami 		return;
    144   1.1     enami 	}
    145   1.1     enami 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_PMPTR);
    146   1.1     enami 	if (bus_space_map(sc->sc_iot, AMDPM_PMBASE(reg), AMDPM_PMSIZE,
    147   1.1     enami 	    0, &sc->sc_ioh)) {
    148   1.5   thorpej 		aprint_error("%s: failed to map PMxx space\n",
    149   1.5   thorpej 		    sc->sc_dev.dv_xname);
    150   1.1     enami 		return;
    151   1.1     enami 	}
    152   1.1     enami 
    153  1.12   xtraeme #ifdef __HAVE_TIMECOUNTER
    154  1.12   xtraeme 	if ((reg & AMDPM_TMRRST) == 0 && (reg & AMDPM_STOPTMR) == 0 &&
    155  1.12   xtraeme 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC768_PMC) {
    156  1.12   xtraeme 		aprint_normal(": %d-bit timer at %" PRIu64 "Hz",
    157  1.12   xtraeme 		    (reg & AMDPM_TMR32) ? 32 : 24,
    158  1.12   xtraeme 		    amdpm_timecounter.tc_frequency);
    159  1.12   xtraeme 
    160  1.12   xtraeme 		if (reg & AMDPM_TMR32)
    161  1.12   xtraeme 			amdpm_timecounter.tc_counter_mask = 0xffffffffu;
    162  1.12   xtraeme 		tc_init(&amdpm_timecounter);
    163  1.12   xtraeme 	}
    164  1.12   xtraeme #endif
    165  1.12   xtraeme 
    166  1.11       tls 	/* try to attach devices on the smbus */
    167  1.11       tls 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC8111_ACPI) {
    168  1.11       tls 		amdpm_smbus_attach(sc);
    169  1.11       tls 	}
    170  1.11       tls 
    171   1.1     enami 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
    172   1.9  christos 	pci_conf_write(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG, reg | AMDPM_RNGEN);
    173   1.9  christos 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
    174   1.1     enami 	if (reg & AMDPM_RNGEN) {
    175   1.1     enami 		/* Check to see if we can read data from the RNG. */
    176   1.1     enami 		(void) bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    177   1.1     enami 		    AMDPM_RNGDATA);
    178   1.1     enami 		for (i = 0; i < 1000; i++) {
    179   1.1     enami 			pmreg = bus_space_read_4(sc->sc_iot,
    180   1.1     enami 			    sc->sc_ioh, AMDPM_RNGSTAT);
    181   1.1     enami 			if (pmreg & AMDPM_RNGDONE)
    182   1.1     enami 				break;
    183   1.1     enami 			delay(1);
    184   1.1     enami 		}
    185   1.1     enami 		if ((pmreg & AMDPM_RNGDONE) != 0) {
    186   1.5   thorpej 			aprint_normal("%s: "
    187   1.1     enami 			    "random number generator enabled (apprx. %dms)\n",
    188   1.1     enami 			    sc->sc_dev.dv_xname, i);
    189   1.1     enami 			callout_init(&sc->sc_rnd_ch);
    190   1.1     enami 			rnd_attach_source(&sc->sc_rnd_source,
    191   1.7       tls 			    sc->sc_dev.dv_xname, RND_TYPE_RNG,
    192   1.7       tls 			    /*
    193   1.7       tls 			     * XXX Careful!  The use of RND_FLAG_NO_ESTIMATE
    194   1.7       tls 			     * XXX here is unobvious: we later feed raw bits
    195   1.7       tls 			     * XXX into the "entropy pool" with rnd_add_data,
    196   1.7       tls 			     * XXX explicitly supplying an entropy estimate.
    197   1.7       tls 			     * XXX In this context, NO_ESTIMATE serves only
    198   1.7       tls 			     * XXX to prevent rnd_add_data from trying to
    199   1.7       tls 			     * XXX use the *time at which we added the data*
    200   1.7       tls 			     * XXX as entropy, which is not a good idea since
    201   1.7       tls 			     * XXX we add data periodically from a callout.
    202   1.7       tls 			     */
    203   1.7       tls 			    RND_FLAG_NO_ESTIMATE);
    204   1.1     enami #ifdef AMDPM_RND_COUNTERS
    205   1.1     enami 			evcnt_attach_dynamic(&sc->sc_rnd_hits, EVCNT_TYPE_MISC,
    206   1.1     enami 			    NULL, sc->sc_dev.dv_xname, "rnd hits");
    207   1.1     enami 			evcnt_attach_dynamic(&sc->sc_rnd_miss, EVCNT_TYPE_MISC,
    208   1.1     enami 			    NULL, sc->sc_dev.dv_xname, "rnd miss");
    209   1.1     enami 			for (i = 0; i < 256; i++) {
    210   1.1     enami 				evcnt_attach_dynamic(&sc->sc_rnd_data[i],
    211   1.1     enami 				    EVCNT_TYPE_MISC, NULL, sc->sc_dev.dv_xname,
    212   1.1     enami 				    "rnd data");
    213   1.1     enami 			}
    214   1.1     enami #endif
    215   1.1     enami 			amdpm_rnd_callout(sc);
    216   1.1     enami 		}
    217   1.1     enami 	}
    218   1.1     enami }
    219   1.1     enami 
    220   1.8   thorpej CFATTACH_DECL(amdpm, sizeof(struct amdpm_softc),
    221   1.8   thorpej     amdpm_match, amdpm_attach, NULL, NULL);
    222   1.8   thorpej 
    223   1.8   thorpej static void
    224   1.1     enami amdpm_rnd_callout(void *v)
    225   1.1     enami {
    226   1.1     enami 	struct amdpm_softc *sc = v;
    227   1.1     enami 	u_int32_t reg;
    228   1.1     enami #ifdef AMDPM_RND_COUNTERS
    229   1.1     enami 	int i;
    230   1.1     enami #endif
    231   1.1     enami 
    232   1.1     enami 	if ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGSTAT) &
    233   1.1     enami 	    AMDPM_RNGDONE) != 0) {
    234   1.1     enami 		reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGDATA);
    235   1.1     enami 		rnd_add_data(&sc->sc_rnd_source, &reg,
    236   1.1     enami 		    sizeof(reg), sizeof(reg) * NBBY);
    237   1.1     enami #ifdef AMDPM_RND_COUNTERS
    238   1.1     enami 		AMDPM_RNDCNT_INCR(&sc->sc_rnd_hits);
    239   1.1     enami 		for (i = 0; i < sizeof(reg); i++, reg >>= NBBY)
    240   1.1     enami 			AMDPM_RNDCNT_INCR(&sc->sc_rnd_data[reg & 0xff]);
    241   1.1     enami #endif
    242   1.1     enami 	} else
    243   1.1     enami 		AMDPM_RNDCNT_INCR(&sc->sc_rnd_miss);
    244   1.1     enami 	callout_reset(&sc->sc_rnd_ch, 1, amdpm_rnd_callout, sc);
    245   1.1     enami }
    246  1.12   xtraeme 
    247  1.12   xtraeme #ifdef __HAVE_TIMECOUNTER
    248  1.12   xtraeme uint32_t
    249  1.12   xtraeme amdpm_get_timecount(struct timecounter *tc)
    250  1.12   xtraeme {
    251  1.12   xtraeme 	struct amdpm_softc *sc = tc->tc_priv;
    252  1.12   xtraeme 
    253  1.12   xtraeme 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_TMR);
    254  1.12   xtraeme }
    255  1.12   xtraeme #endif
    256