amdpm.c revision 1.5.2.2 1 1.5.2.1 skrll /* $NetBSD: amdpm.c,v 1.5.2.2 2004/09/18 14:49:02 skrll 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.5.2.1 skrll __KERNEL_RCSID(0, "$NetBSD: amdpm.c,v 1.5.2.2 2004/09/18 14:49:02 skrll 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.1 enami #include <dev/pci/pcivar.h>
52 1.1 enami #include <dev/pci/pcireg.h>
53 1.1 enami #include <dev/pci/pcidevs.h>
54 1.1 enami
55 1.1 enami #include <dev/pci/amdpmreg.h>
56 1.1 enami
57 1.1 enami struct amdpm_softc {
58 1.1 enami struct device sc_dev;
59 1.1 enami
60 1.1 enami pci_chipset_tag_t sc_pc;
61 1.1 enami pcitag_t sc_tag;
62 1.1 enami
63 1.1 enami bus_space_tag_t sc_iot;
64 1.1 enami bus_space_handle_t sc_ioh; /* PMxx space */
65 1.1 enami
66 1.1 enami struct callout sc_rnd_ch;
67 1.1 enami rndsource_element_t sc_rnd_source;
68 1.1 enami #ifdef AMDPM_RND_COUNTERS
69 1.1 enami struct evcnt sc_rnd_hits;
70 1.1 enami struct evcnt sc_rnd_miss;
71 1.1 enami struct evcnt sc_rnd_data[256];
72 1.1 enami #endif
73 1.1 enami };
74 1.1 enami
75 1.1 enami int amdpm_match(struct device *, struct cfdata *, void *);
76 1.1 enami void amdpm_attach(struct device *, struct device *, void *);
77 1.1 enami void amdpm_rnd_callout(void *);
78 1.1 enami
79 1.3 thorpej CFATTACH_DECL(amdpm, sizeof(struct amdpm_softc),
80 1.4 thorpej amdpm_match, amdpm_attach, NULL, NULL);
81 1.1 enami
82 1.1 enami #ifdef AMDPM_RND_COUNTERS
83 1.1 enami #define AMDPM_RNDCNT_INCR(ev) (ev)->ev_count++
84 1.1 enami #else
85 1.1 enami #define AMDPM_RNDCNT_INCR(ev) /* nothing */
86 1.1 enami #endif
87 1.1 enami
88 1.1 enami int
89 1.1 enami amdpm_match(struct device *parent, struct cfdata *match, void *aux)
90 1.1 enami {
91 1.1 enami struct pci_attach_args *pa = aux;
92 1.1 enami
93 1.1 enami if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD &&
94 1.1 enami PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC768_PMC)
95 1.1 enami return (1);
96 1.1 enami return (0);
97 1.1 enami }
98 1.1 enami
99 1.1 enami void
100 1.1 enami amdpm_attach(struct device *parent, struct device *self, void *aux)
101 1.1 enami {
102 1.1 enami struct amdpm_softc *sc = (struct amdpm_softc *) self;
103 1.1 enami struct pci_attach_args *pa = aux;
104 1.1 enami pcireg_t reg;
105 1.1 enami u_int32_t pmreg;
106 1.1 enami int i;
107 1.1 enami
108 1.5 thorpej aprint_naive("\n");
109 1.5 thorpej aprint_normal("\n");
110 1.1 enami
111 1.1 enami sc->sc_pc = pa->pa_pc;
112 1.1 enami sc->sc_tag = pa->pa_tag;
113 1.1 enami sc->sc_iot = pa->pa_iot;
114 1.1 enami
115 1.1 enami #if 0
116 1.5 thorpej aprint_normal("%s: ", sc->sc_dev.dv_xname);
117 1.1 enami pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
118 1.1 enami #endif
119 1.1 enami
120 1.1 enami reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
121 1.1 enami if ((reg & AMDPM_PMIOEN) == 0) {
122 1.5 thorpej aprint_error("%s: PMxx space isn't enabled\n",
123 1.5 thorpej sc->sc_dev.dv_xname);
124 1.1 enami return;
125 1.1 enami }
126 1.1 enami reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_PMPTR);
127 1.1 enami if (bus_space_map(sc->sc_iot, AMDPM_PMBASE(reg), AMDPM_PMSIZE,
128 1.1 enami 0, &sc->sc_ioh)) {
129 1.5 thorpej aprint_error("%s: failed to map PMxx space\n",
130 1.5 thorpej sc->sc_dev.dv_xname);
131 1.1 enami return;
132 1.1 enami }
133 1.1 enami
134 1.1 enami reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
135 1.1 enami if (reg & AMDPM_RNGEN) {
136 1.1 enami /* Check to see if we can read data from the RNG. */
137 1.1 enami (void) bus_space_read_4(sc->sc_iot, sc->sc_ioh,
138 1.1 enami AMDPM_RNGDATA);
139 1.1 enami for (i = 0; i < 1000; i++) {
140 1.1 enami pmreg = bus_space_read_4(sc->sc_iot,
141 1.1 enami sc->sc_ioh, AMDPM_RNGSTAT);
142 1.1 enami if (pmreg & AMDPM_RNGDONE)
143 1.1 enami break;
144 1.1 enami delay(1);
145 1.1 enami }
146 1.1 enami if ((pmreg & AMDPM_RNGDONE) != 0) {
147 1.5 thorpej aprint_normal("%s: "
148 1.1 enami "random number generator enabled (apprx. %dms)\n",
149 1.1 enami sc->sc_dev.dv_xname, i);
150 1.1 enami callout_init(&sc->sc_rnd_ch);
151 1.1 enami rnd_attach_source(&sc->sc_rnd_source,
152 1.1 enami sc->sc_dev.dv_xname, RND_TYPE_RNG,
153 1.1 enami /*
154 1.5.2.1 skrll * XXX Careful! The use of RND_FLAG_NO_ESTIMATE
155 1.5.2.1 skrll * XXX here is unobvious: we later feed raw bits
156 1.5.2.1 skrll * XXX into the "entropy pool" with rnd_add_data,
157 1.5.2.1 skrll * XXX explicitly supplying an entropy estimate.
158 1.5.2.1 skrll * XXX In this context, NO_ESTIMATE serves only
159 1.5.2.1 skrll * XXX to prevent rnd_add_data from trying to
160 1.5.2.1 skrll * XXX use the *time at which we added the data*
161 1.5.2.1 skrll * XXX as entropy, which is not a good idea since
162 1.5.2.1 skrll * XXX we add data periodically from a callout.
163 1.1 enami */
164 1.1 enami RND_FLAG_NO_ESTIMATE);
165 1.1 enami #ifdef AMDPM_RND_COUNTERS
166 1.1 enami evcnt_attach_dynamic(&sc->sc_rnd_hits, EVCNT_TYPE_MISC,
167 1.1 enami NULL, sc->sc_dev.dv_xname, "rnd hits");
168 1.1 enami evcnt_attach_dynamic(&sc->sc_rnd_miss, EVCNT_TYPE_MISC,
169 1.1 enami NULL, sc->sc_dev.dv_xname, "rnd miss");
170 1.1 enami for (i = 0; i < 256; i++) {
171 1.1 enami evcnt_attach_dynamic(&sc->sc_rnd_data[i],
172 1.1 enami EVCNT_TYPE_MISC, NULL, sc->sc_dev.dv_xname,
173 1.1 enami "rnd data");
174 1.1 enami }
175 1.1 enami #endif
176 1.1 enami amdpm_rnd_callout(sc);
177 1.1 enami }
178 1.1 enami }
179 1.1 enami }
180 1.1 enami
181 1.1 enami void
182 1.1 enami amdpm_rnd_callout(void *v)
183 1.1 enami {
184 1.1 enami struct amdpm_softc *sc = v;
185 1.1 enami u_int32_t reg;
186 1.1 enami #ifdef AMDPM_RND_COUNTERS
187 1.1 enami int i;
188 1.1 enami #endif
189 1.1 enami
190 1.1 enami if ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGSTAT) &
191 1.1 enami AMDPM_RNGDONE) != 0) {
192 1.1 enami reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGDATA);
193 1.1 enami rnd_add_data(&sc->sc_rnd_source, ®,
194 1.1 enami sizeof(reg), sizeof(reg) * NBBY);
195 1.1 enami #ifdef AMDPM_RND_COUNTERS
196 1.1 enami AMDPM_RNDCNT_INCR(&sc->sc_rnd_hits);
197 1.1 enami for (i = 0; i < sizeof(reg); i++, reg >>= NBBY)
198 1.1 enami AMDPM_RNDCNT_INCR(&sc->sc_rnd_data[reg & 0xff]);
199 1.1 enami #endif
200 1.1 enami } else
201 1.1 enami AMDPM_RNDCNT_INCR(&sc->sc_rnd_miss);
202 1.1 enami callout_reset(&sc->sc_rnd_ch, 1, amdpm_rnd_callout, sc);
203 1.1 enami }
204