summaryrefslogtreecommitdiff
path: root/sys/dev
diff options
context:
space:
mode:
authorenami <enami@NetBSD.org>2002-06-02 02:44:27 +0000
committerenami <enami@NetBSD.org>2002-06-02 02:44:27 +0000
commit461e42e9ef2ad050f96b8f5df0a75803e56ca28f (patch)
tree39e928ce1131eab6805b5cf62084197b2b66284b /sys/dev
parenta257b81df856dcd5677d5d51a176da0622a4fb78 (diff)
Collect random number from AMD 768MPX power management controller.
Diffstat (limited to 'sys/dev')
-rw-r--r--sys/dev/DEVNAMES3
-rw-r--r--sys/dev/pci/amdpm.c194
-rw-r--r--sys/dev/pci/amdpmreg.h58
-rw-r--r--sys/dev/pci/files.pci8
4 files changed, 261 insertions, 2 deletions
diff --git a/sys/dev/DEVNAMES b/sys/dev/DEVNAMES
index ed912a963c5..484d4e67984 100644
--- a/sys/dev/DEVNAMES
+++ b/sys/dev/DEVNAMES
@@ -1,4 +1,4 @@
-# $NetBSD: DEVNAMES,v 1.115 2002/05/31 00:27:00 augustss Exp $
+# $NetBSD: DEVNAMES,v 1.116 2002/06/02 02:44:27 enami Exp $
#
# This file contains all used device names and defined attributes in
# alphabetical order. New devices added to the system somewhere should first
@@ -51,6 +51,7 @@ alpha_pci_sgmap_pte32 alpha Attribute
alpha_pci_sgmap_pte64 alpha Attribute
alpha_sgmap alpha Attribute
alpha_shared_intr alpha Attribute
+amdpm MI
amibus_b16 amiga Attribute
amibus_bl amiga Attribute
amibus_wb amiga Attribute
diff --git a/sys/dev/pci/amdpm.c b/sys/dev/pci/amdpm.c
new file mode 100644
index 00000000000..c0d4dbca845
--- /dev/null
+++ b/sys/dev/pci/amdpm.c
@@ -0,0 +1,194 @@
+/* $NetBSD: amdpm.c,v 1.1 2002/06/02 02:44:28 enami Exp $ */
+
+/*-
+ * Copyright (c) 2002 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Enami Tsugutomo.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the NetBSD
+ * Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: amdpm.c,v 1.1 2002/06/02 02:44:28 enami Exp $");
+
+#include "opt_amdpm.h"
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/device.h>
+#include <sys/callout.h>
+#include <sys/rnd.h>
+
+#include <dev/pci/pcivar.h>
+#include <dev/pci/pcireg.h>
+#include <dev/pci/pcidevs.h>
+
+#include <dev/pci/amdpmreg.h>
+
+struct amdpm_softc {
+ struct device sc_dev;
+
+ pci_chipset_tag_t sc_pc;
+ pcitag_t sc_tag;
+
+ bus_space_tag_t sc_iot;
+ bus_space_handle_t sc_ioh; /* PMxx space */
+
+ struct callout sc_rnd_ch;
+ rndsource_element_t sc_rnd_source;
+#ifdef AMDPM_RND_COUNTERS
+ struct evcnt sc_rnd_hits;
+ struct evcnt sc_rnd_miss;
+ struct evcnt sc_rnd_data[256];
+#endif
+};
+
+int amdpm_match(struct device *, struct cfdata *, void *);
+void amdpm_attach(struct device *, struct device *, void *);
+void amdpm_rnd_callout(void *);
+
+struct cfattach amdpm_ca = {
+ sizeof(struct amdpm_softc), amdpm_match, amdpm_attach
+};
+
+#ifdef AMDPM_RND_COUNTERS
+#define AMDPM_RNDCNT_INCR(ev) (ev)->ev_count++
+#else
+#define AMDPM_RNDCNT_INCR(ev) /* nothing */
+#endif
+
+int
+amdpm_match(struct device *parent, struct cfdata *match, void *aux)
+{
+ struct pci_attach_args *pa = aux;
+
+ if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD &&
+ PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC768_PMC)
+ return (1);
+ return (0);
+}
+
+void
+amdpm_attach(struct device *parent, struct device *self, void *aux)
+{
+ struct amdpm_softc *sc = (struct amdpm_softc *) self;
+ struct pci_attach_args *pa = aux;
+ pcireg_t reg;
+ u_int32_t pmreg;
+ int i;
+
+ printf("\n");
+
+ sc->sc_pc = pa->pa_pc;
+ sc->sc_tag = pa->pa_tag;
+ sc->sc_iot = pa->pa_iot;
+
+#if 0
+ printf("%s: ", sc->sc_dev.dv_xname);
+ pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
+#endif
+
+ reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
+ if ((reg & AMDPM_PMIOEN) == 0) {
+ printf("%s: PMxx space isn't enabled\n", sc->sc_dev.dv_xname);
+ return;
+ }
+ reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_PMPTR);
+ if (bus_space_map(sc->sc_iot, AMDPM_PMBASE(reg), AMDPM_PMSIZE,
+ 0, &sc->sc_ioh)) {
+ printf("%s: failed to map PMxx space\n", sc->sc_dev.dv_xname);
+ return;
+ }
+
+ reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
+ if (reg & AMDPM_RNGEN) {
+ /* Check to see if we can read data from the RNG. */
+ (void) bus_space_read_4(sc->sc_iot, sc->sc_ioh,
+ AMDPM_RNGDATA);
+ for (i = 0; i < 1000; i++) {
+ pmreg = bus_space_read_4(sc->sc_iot,
+ sc->sc_ioh, AMDPM_RNGSTAT);
+ if (pmreg & AMDPM_RNGDONE)
+ break;
+ delay(1);
+ }
+ if ((pmreg & AMDPM_RNGDONE) != 0) {
+ printf("%s: "
+ "random number generator enabled (apprx. %dms)\n",
+ sc->sc_dev.dv_xname, i);
+ callout_init(&sc->sc_rnd_ch);
+ rnd_attach_source(&sc->sc_rnd_source,
+ sc->sc_dev.dv_xname, RND_TYPE_RNG,
+ /*
+ * We can't estimate entropy since we poll
+ * random data periodically.
+ */
+ RND_FLAG_NO_ESTIMATE);
+#ifdef AMDPM_RND_COUNTERS
+ evcnt_attach_dynamic(&sc->sc_rnd_hits, EVCNT_TYPE_MISC,
+ NULL, sc->sc_dev.dv_xname, "rnd hits");
+ evcnt_attach_dynamic(&sc->sc_rnd_miss, EVCNT_TYPE_MISC,
+ NULL, sc->sc_dev.dv_xname, "rnd miss");
+ for (i = 0; i < 256; i++) {
+ evcnt_attach_dynamic(&sc->sc_rnd_data[i],
+ EVCNT_TYPE_MISC, NULL, sc->sc_dev.dv_xname,
+ "rnd data");
+ }
+#endif
+ amdpm_rnd_callout(sc);
+ }
+ }
+}
+
+void
+amdpm_rnd_callout(void *v)
+{
+ struct amdpm_softc *sc = v;
+ u_int32_t reg;
+#ifdef AMDPM_RND_COUNTERS
+ int i;
+#endif
+
+ if ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGSTAT) &
+ AMDPM_RNGDONE) != 0) {
+ reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGDATA);
+ rnd_add_data(&sc->sc_rnd_source, &reg,
+ sizeof(reg), sizeof(reg) * NBBY);
+#ifdef AMDPM_RND_COUNTERS
+ AMDPM_RNDCNT_INCR(&sc->sc_rnd_hits);
+ for (i = 0; i < sizeof(reg); i++, reg >>= NBBY)
+ AMDPM_RNDCNT_INCR(&sc->sc_rnd_data[reg & 0xff]);
+#endif
+ } else
+ AMDPM_RNDCNT_INCR(&sc->sc_rnd_miss);
+ callout_reset(&sc->sc_rnd_ch, 1, amdpm_rnd_callout, sc);
+}
diff --git a/sys/dev/pci/amdpmreg.h b/sys/dev/pci/amdpmreg.h
new file mode 100644
index 00000000000..e71b25bb709
--- /dev/null
+++ b/sys/dev/pci/amdpmreg.h
@@ -0,0 +1,58 @@
+/* $NetBSD: amdpmreg.h,v 1.1 2002/06/02 02:44:28 enami Exp $ */
+
+/*-
+ * Copyright (c) 2002 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Enami Tsugutomo.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the NetBSD
+ * Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#define AMDPM_CONFREG 0x40
+
+/* 0x40: General Configuration 1 Register */
+#define AMDPM_RNGEN 0x00000080 /* random number generator enable */
+
+/* 0x41: General Configuration 2 Register */
+#define AMDPM_PMIOEN 0x00008000 /* system management IO space enable */
+
+/* 0x42: SCI Interrupt Configuration Register */
+/* 0x43: Previous Power State Register */
+
+#define AMDPM_PMPTR 0x58 /* PMxx System Management IO space
+ Pointer */
+#define AMDPM_PMBASE(x) ((x) & 0xff00) /* PMxx base address */
+#define AMDPM_PMSIZE 256 /* PMxx space size */
+
+/* Registers in PMxx space */
+#define AMDPM_RNGDATA 0xf0 /* 32 bit random data register */
+#define AMDPM_RNGSTAT 0xf4 /* RNG status register */
+#define AMDPM_RNGDONE 0x00000001 /* Random number generation complete */
diff --git a/sys/dev/pci/files.pci b/sys/dev/pci/files.pci
index fe5407d4af9..b59f5426d58 100644
--- a/sys/dev/pci/files.pci
+++ b/sys/dev/pci/files.pci
@@ -1,4 +1,4 @@
-# $NetBSD: files.pci,v 1.173 2002/05/18 07:23:06 matt Exp $
+# $NetBSD: files.pci,v 1.174 2002/06/02 02:44:27 enami Exp $
#
# Config file and device description for machine-independent PCI code.
# Included by ports that need it. Requires that the SCSI files be
@@ -518,6 +518,12 @@ device viaenv: sysmon_envsys
attach viaenv at viapm
file dev/pci/viaenv.c viaenv needs-flag
+# AMD 768MPX power management controller
+defflag opt_amdpm.h AMDPM_RND_COUNTERS
+device amdpm {}
+attach amdpm at pci
+file dev/pci/amdpm.c amdpm
+
# Hi/fn 7751
device hifn {}
attach hifn at pci