amdpm.c revision 1.1.6.2 1 1.1.6.2 jdolecek /* $NetBSD: amdpm.c,v 1.1.6.2 2002/06/23 17:47:32 jdolecek Exp $ */
2 1.1.6.2 jdolecek
3 1.1.6.2 jdolecek /*-
4 1.1.6.2 jdolecek * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1.6.2 jdolecek * All rights reserved.
6 1.1.6.2 jdolecek *
7 1.1.6.2 jdolecek * This code is derived from software contributed to The NetBSD Foundation
8 1.1.6.2 jdolecek * by Enami Tsugutomo.
9 1.1.6.2 jdolecek *
10 1.1.6.2 jdolecek * Redistribution and use in source and binary forms, with or without
11 1.1.6.2 jdolecek * modification, are permitted provided that the following conditions
12 1.1.6.2 jdolecek * are met:
13 1.1.6.2 jdolecek * 1. Redistributions of source code must retain the above copyright
14 1.1.6.2 jdolecek * notice, this list of conditions and the following disclaimer.
15 1.1.6.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.6.2 jdolecek * notice, this list of conditions and the following disclaimer in the
17 1.1.6.2 jdolecek * documentation and/or other materials provided with the distribution.
18 1.1.6.2 jdolecek * 3. All advertising materials mentioning features or use of this software
19 1.1.6.2 jdolecek * must display the following acknowledgement:
20 1.1.6.2 jdolecek * This product includes software developed by the NetBSD
21 1.1.6.2 jdolecek * Foundation, Inc. and its contributors.
22 1.1.6.2 jdolecek * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.6.2 jdolecek * contributors may be used to endorse or promote products derived
24 1.1.6.2 jdolecek * from this software without specific prior written permission.
25 1.1.6.2 jdolecek *
26 1.1.6.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.6.2 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.6.2 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.6.2 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.6.2 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.6.2 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.6.2 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.6.2 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.6.2 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.6.2 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.6.2 jdolecek * POSSIBILITY OF SUCH DAMAGE.
37 1.1.6.2 jdolecek */
38 1.1.6.2 jdolecek
39 1.1.6.2 jdolecek #include <sys/cdefs.h>
40 1.1.6.2 jdolecek __KERNEL_RCSID(0, "$NetBSD: amdpm.c,v 1.1.6.2 2002/06/23 17:47:32 jdolecek Exp $");
41 1.1.6.2 jdolecek
42 1.1.6.2 jdolecek #include "opt_amdpm.h"
43 1.1.6.2 jdolecek
44 1.1.6.2 jdolecek #include <sys/param.h>
45 1.1.6.2 jdolecek #include <sys/systm.h>
46 1.1.6.2 jdolecek #include <sys/kernel.h>
47 1.1.6.2 jdolecek #include <sys/device.h>
48 1.1.6.2 jdolecek #include <sys/callout.h>
49 1.1.6.2 jdolecek #include <sys/rnd.h>
50 1.1.6.2 jdolecek
51 1.1.6.2 jdolecek #include <dev/pci/pcivar.h>
52 1.1.6.2 jdolecek #include <dev/pci/pcireg.h>
53 1.1.6.2 jdolecek #include <dev/pci/pcidevs.h>
54 1.1.6.2 jdolecek
55 1.1.6.2 jdolecek #include <dev/pci/amdpmreg.h>
56 1.1.6.2 jdolecek
57 1.1.6.2 jdolecek struct amdpm_softc {
58 1.1.6.2 jdolecek struct device sc_dev;
59 1.1.6.2 jdolecek
60 1.1.6.2 jdolecek pci_chipset_tag_t sc_pc;
61 1.1.6.2 jdolecek pcitag_t sc_tag;
62 1.1.6.2 jdolecek
63 1.1.6.2 jdolecek bus_space_tag_t sc_iot;
64 1.1.6.2 jdolecek bus_space_handle_t sc_ioh; /* PMxx space */
65 1.1.6.2 jdolecek
66 1.1.6.2 jdolecek struct callout sc_rnd_ch;
67 1.1.6.2 jdolecek rndsource_element_t sc_rnd_source;
68 1.1.6.2 jdolecek #ifdef AMDPM_RND_COUNTERS
69 1.1.6.2 jdolecek struct evcnt sc_rnd_hits;
70 1.1.6.2 jdolecek struct evcnt sc_rnd_miss;
71 1.1.6.2 jdolecek struct evcnt sc_rnd_data[256];
72 1.1.6.2 jdolecek #endif
73 1.1.6.2 jdolecek };
74 1.1.6.2 jdolecek
75 1.1.6.2 jdolecek int amdpm_match(struct device *, struct cfdata *, void *);
76 1.1.6.2 jdolecek void amdpm_attach(struct device *, struct device *, void *);
77 1.1.6.2 jdolecek void amdpm_rnd_callout(void *);
78 1.1.6.2 jdolecek
79 1.1.6.2 jdolecek struct cfattach amdpm_ca = {
80 1.1.6.2 jdolecek sizeof(struct amdpm_softc), amdpm_match, amdpm_attach
81 1.1.6.2 jdolecek };
82 1.1.6.2 jdolecek
83 1.1.6.2 jdolecek #ifdef AMDPM_RND_COUNTERS
84 1.1.6.2 jdolecek #define AMDPM_RNDCNT_INCR(ev) (ev)->ev_count++
85 1.1.6.2 jdolecek #else
86 1.1.6.2 jdolecek #define AMDPM_RNDCNT_INCR(ev) /* nothing */
87 1.1.6.2 jdolecek #endif
88 1.1.6.2 jdolecek
89 1.1.6.2 jdolecek int
90 1.1.6.2 jdolecek amdpm_match(struct device *parent, struct cfdata *match, void *aux)
91 1.1.6.2 jdolecek {
92 1.1.6.2 jdolecek struct pci_attach_args *pa = aux;
93 1.1.6.2 jdolecek
94 1.1.6.2 jdolecek if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD &&
95 1.1.6.2 jdolecek PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC768_PMC)
96 1.1.6.2 jdolecek return (1);
97 1.1.6.2 jdolecek return (0);
98 1.1.6.2 jdolecek }
99 1.1.6.2 jdolecek
100 1.1.6.2 jdolecek void
101 1.1.6.2 jdolecek amdpm_attach(struct device *parent, struct device *self, void *aux)
102 1.1.6.2 jdolecek {
103 1.1.6.2 jdolecek struct amdpm_softc *sc = (struct amdpm_softc *) self;
104 1.1.6.2 jdolecek struct pci_attach_args *pa = aux;
105 1.1.6.2 jdolecek pcireg_t reg;
106 1.1.6.2 jdolecek u_int32_t pmreg;
107 1.1.6.2 jdolecek int i;
108 1.1.6.2 jdolecek
109 1.1.6.2 jdolecek printf("\n");
110 1.1.6.2 jdolecek
111 1.1.6.2 jdolecek sc->sc_pc = pa->pa_pc;
112 1.1.6.2 jdolecek sc->sc_tag = pa->pa_tag;
113 1.1.6.2 jdolecek sc->sc_iot = pa->pa_iot;
114 1.1.6.2 jdolecek
115 1.1.6.2 jdolecek #if 0
116 1.1.6.2 jdolecek printf("%s: ", sc->sc_dev.dv_xname);
117 1.1.6.2 jdolecek pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
118 1.1.6.2 jdolecek #endif
119 1.1.6.2 jdolecek
120 1.1.6.2 jdolecek reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
121 1.1.6.2 jdolecek if ((reg & AMDPM_PMIOEN) == 0) {
122 1.1.6.2 jdolecek printf("%s: PMxx space isn't enabled\n", sc->sc_dev.dv_xname);
123 1.1.6.2 jdolecek return;
124 1.1.6.2 jdolecek }
125 1.1.6.2 jdolecek reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_PMPTR);
126 1.1.6.2 jdolecek if (bus_space_map(sc->sc_iot, AMDPM_PMBASE(reg), AMDPM_PMSIZE,
127 1.1.6.2 jdolecek 0, &sc->sc_ioh)) {
128 1.1.6.2 jdolecek printf("%s: failed to map PMxx space\n", sc->sc_dev.dv_xname);
129 1.1.6.2 jdolecek return;
130 1.1.6.2 jdolecek }
131 1.1.6.2 jdolecek
132 1.1.6.2 jdolecek reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
133 1.1.6.2 jdolecek if (reg & AMDPM_RNGEN) {
134 1.1.6.2 jdolecek /* Check to see if we can read data from the RNG. */
135 1.1.6.2 jdolecek (void) bus_space_read_4(sc->sc_iot, sc->sc_ioh,
136 1.1.6.2 jdolecek AMDPM_RNGDATA);
137 1.1.6.2 jdolecek for (i = 0; i < 1000; i++) {
138 1.1.6.2 jdolecek pmreg = bus_space_read_4(sc->sc_iot,
139 1.1.6.2 jdolecek sc->sc_ioh, AMDPM_RNGSTAT);
140 1.1.6.2 jdolecek if (pmreg & AMDPM_RNGDONE)
141 1.1.6.2 jdolecek break;
142 1.1.6.2 jdolecek delay(1);
143 1.1.6.2 jdolecek }
144 1.1.6.2 jdolecek if ((pmreg & AMDPM_RNGDONE) != 0) {
145 1.1.6.2 jdolecek printf("%s: "
146 1.1.6.2 jdolecek "random number generator enabled (apprx. %dms)\n",
147 1.1.6.2 jdolecek sc->sc_dev.dv_xname, i);
148 1.1.6.2 jdolecek callout_init(&sc->sc_rnd_ch);
149 1.1.6.2 jdolecek rnd_attach_source(&sc->sc_rnd_source,
150 1.1.6.2 jdolecek sc->sc_dev.dv_xname, RND_TYPE_RNG,
151 1.1.6.2 jdolecek /*
152 1.1.6.2 jdolecek * We can't estimate entropy since we poll
153 1.1.6.2 jdolecek * random data periodically.
154 1.1.6.2 jdolecek */
155 1.1.6.2 jdolecek RND_FLAG_NO_ESTIMATE);
156 1.1.6.2 jdolecek #ifdef AMDPM_RND_COUNTERS
157 1.1.6.2 jdolecek evcnt_attach_dynamic(&sc->sc_rnd_hits, EVCNT_TYPE_MISC,
158 1.1.6.2 jdolecek NULL, sc->sc_dev.dv_xname, "rnd hits");
159 1.1.6.2 jdolecek evcnt_attach_dynamic(&sc->sc_rnd_miss, EVCNT_TYPE_MISC,
160 1.1.6.2 jdolecek NULL, sc->sc_dev.dv_xname, "rnd miss");
161 1.1.6.2 jdolecek for (i = 0; i < 256; i++) {
162 1.1.6.2 jdolecek evcnt_attach_dynamic(&sc->sc_rnd_data[i],
163 1.1.6.2 jdolecek EVCNT_TYPE_MISC, NULL, sc->sc_dev.dv_xname,
164 1.1.6.2 jdolecek "rnd data");
165 1.1.6.2 jdolecek }
166 1.1.6.2 jdolecek #endif
167 1.1.6.2 jdolecek amdpm_rnd_callout(sc);
168 1.1.6.2 jdolecek }
169 1.1.6.2 jdolecek }
170 1.1.6.2 jdolecek }
171 1.1.6.2 jdolecek
172 1.1.6.2 jdolecek void
173 1.1.6.2 jdolecek amdpm_rnd_callout(void *v)
174 1.1.6.2 jdolecek {
175 1.1.6.2 jdolecek struct amdpm_softc *sc = v;
176 1.1.6.2 jdolecek u_int32_t reg;
177 1.1.6.2 jdolecek #ifdef AMDPM_RND_COUNTERS
178 1.1.6.2 jdolecek int i;
179 1.1.6.2 jdolecek #endif
180 1.1.6.2 jdolecek
181 1.1.6.2 jdolecek if ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGSTAT) &
182 1.1.6.2 jdolecek AMDPM_RNGDONE) != 0) {
183 1.1.6.2 jdolecek reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGDATA);
184 1.1.6.2 jdolecek rnd_add_data(&sc->sc_rnd_source, ®,
185 1.1.6.2 jdolecek sizeof(reg), sizeof(reg) * NBBY);
186 1.1.6.2 jdolecek #ifdef AMDPM_RND_COUNTERS
187 1.1.6.2 jdolecek AMDPM_RNDCNT_INCR(&sc->sc_rnd_hits);
188 1.1.6.2 jdolecek for (i = 0; i < sizeof(reg); i++, reg >>= NBBY)
189 1.1.6.2 jdolecek AMDPM_RNDCNT_INCR(&sc->sc_rnd_data[reg & 0xff]);
190 1.1.6.2 jdolecek #endif
191 1.1.6.2 jdolecek } else
192 1.1.6.2 jdolecek AMDPM_RNDCNT_INCR(&sc->sc_rnd_miss);
193 1.1.6.2 jdolecek callout_reset(&sc->sc_rnd_ch, 1, amdpm_rnd_callout, sc);
194 1.1.6.2 jdolecek }
195