amdpm.c revision 1.12 1 /* $NetBSD: amdpm.c,v 1.12 2006/06/17 15:05:15 xtraeme 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.12 2006/06/17 15:05:15 xtraeme 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 #ifdef __HAVE_TIMECOUNTER
52 #include <sys/timetc.h>
53 #include <machine/bus.h>
54 #endif
55
56 #include <dev/i2c/i2cvar.h>
57
58 #include <dev/pci/pcivar.h>
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcidevs.h>
61
62 #include <dev/pci/amdpmreg.h>
63 #include <dev/pci/amdpmvar.h>
64 #include <dev/pci/amdpm_smbusreg.h>
65
66 static void amdpm_rnd_callout(void *);
67
68 #ifdef __HAVE_TIMECOUNTER
69 uint32_t amdpm_get_timecount(struct timecounter *);
70
71 #ifndef AMDPM_FREQUENCY
72 #define AMDPM_FREQUENCY 3579545
73 #endif
74
75 static struct timecounter amdpm_timecounter = {
76 amdpm_get_timecount,
77 0,
78 0xffffff,
79 AMDPM_FREQUENCY,
80 "amdpm",
81 1000
82 };
83 #endif
84
85 #ifdef AMDPM_RND_COUNTERS
86 #define AMDPM_RNDCNT_INCR(ev) (ev)->ev_count++
87 #else
88 #define AMDPM_RNDCNT_INCR(ev) /* nothing */
89 #endif
90
91 static int
92 amdpm_match(struct device *parent, struct cfdata *match, void *aux)
93 {
94 struct pci_attach_args *pa = aux;
95
96 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_AMD)
97 return (0);
98
99 switch (PCI_PRODUCT(pa->pa_id)) {
100 case PCI_PRODUCT_AMD_PBC768_PMC:
101 case PCI_PRODUCT_AMD_PBC8111_ACPI:
102 return (1);
103 }
104
105 return (0);
106 }
107
108 static void
109 amdpm_attach(struct device *parent, struct device *self, void *aux)
110 {
111 struct amdpm_softc *sc = (struct amdpm_softc *) self;
112 struct pci_attach_args *pa = aux;
113 char devinfo[256];
114 pcireg_t reg;
115 u_int32_t pmreg;
116 int i;
117
118 aprint_naive("\n");
119 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
120 aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
121 PCI_REVISION(pa->pa_class));
122
123 sc->sc_pc = pa->pa_pc;
124 sc->sc_tag = pa->pa_tag;
125 sc->sc_iot = pa->pa_iot;
126
127 #if 0
128 aprint_normal("%s: ", sc->sc_dev.dv_xname);
129 pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
130 #endif
131
132 /* enable random # generation and pm i/o space for AMD-8111 */
133 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC8111_ACPI) {
134 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
135 pci_conf_write(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG, reg|
136 AMDPM_RNGEN|AMDPM_PMIOEN);
137 }
138
139 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
140 if ((reg & AMDPM_PMIOEN) == 0) {
141 aprint_error("%s: PMxx space isn't enabled\n",
142 sc->sc_dev.dv_xname);
143 return;
144 }
145 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_PMPTR);
146 if (bus_space_map(sc->sc_iot, AMDPM_PMBASE(reg), AMDPM_PMSIZE,
147 0, &sc->sc_ioh)) {
148 aprint_error("%s: failed to map PMxx space\n",
149 sc->sc_dev.dv_xname);
150 return;
151 }
152
153 #ifdef __HAVE_TIMECOUNTER
154 if ((reg & AMDPM_TMRRST) == 0 && (reg & AMDPM_STOPTMR) == 0 &&
155 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC768_PMC) {
156 aprint_normal(": %d-bit timer at %" PRIu64 "Hz",
157 (reg & AMDPM_TMR32) ? 32 : 24,
158 amdpm_timecounter.tc_frequency);
159
160 if (reg & AMDPM_TMR32)
161 amdpm_timecounter.tc_counter_mask = 0xffffffffu;
162 tc_init(&amdpm_timecounter);
163 }
164 #endif
165
166 /* try to attach devices on the smbus */
167 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC8111_ACPI) {
168 amdpm_smbus_attach(sc);
169 }
170
171 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
172 pci_conf_write(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG, reg | AMDPM_RNGEN);
173 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
174 if (reg & AMDPM_RNGEN) {
175 /* Check to see if we can read data from the RNG. */
176 (void) bus_space_read_4(sc->sc_iot, sc->sc_ioh,
177 AMDPM_RNGDATA);
178 for (i = 0; i < 1000; i++) {
179 pmreg = bus_space_read_4(sc->sc_iot,
180 sc->sc_ioh, AMDPM_RNGSTAT);
181 if (pmreg & AMDPM_RNGDONE)
182 break;
183 delay(1);
184 }
185 if ((pmreg & AMDPM_RNGDONE) != 0) {
186 aprint_normal("%s: "
187 "random number generator enabled (apprx. %dms)\n",
188 sc->sc_dev.dv_xname, i);
189 callout_init(&sc->sc_rnd_ch);
190 rnd_attach_source(&sc->sc_rnd_source,
191 sc->sc_dev.dv_xname, RND_TYPE_RNG,
192 /*
193 * XXX Careful! The use of RND_FLAG_NO_ESTIMATE
194 * XXX here is unobvious: we later feed raw bits
195 * XXX into the "entropy pool" with rnd_add_data,
196 * XXX explicitly supplying an entropy estimate.
197 * XXX In this context, NO_ESTIMATE serves only
198 * XXX to prevent rnd_add_data from trying to
199 * XXX use the *time at which we added the data*
200 * XXX as entropy, which is not a good idea since
201 * XXX we add data periodically from a callout.
202 */
203 RND_FLAG_NO_ESTIMATE);
204 #ifdef AMDPM_RND_COUNTERS
205 evcnt_attach_dynamic(&sc->sc_rnd_hits, EVCNT_TYPE_MISC,
206 NULL, sc->sc_dev.dv_xname, "rnd hits");
207 evcnt_attach_dynamic(&sc->sc_rnd_miss, EVCNT_TYPE_MISC,
208 NULL, sc->sc_dev.dv_xname, "rnd miss");
209 for (i = 0; i < 256; i++) {
210 evcnt_attach_dynamic(&sc->sc_rnd_data[i],
211 EVCNT_TYPE_MISC, NULL, sc->sc_dev.dv_xname,
212 "rnd data");
213 }
214 #endif
215 amdpm_rnd_callout(sc);
216 }
217 }
218 }
219
220 CFATTACH_DECL(amdpm, sizeof(struct amdpm_softc),
221 amdpm_match, amdpm_attach, NULL, NULL);
222
223 static void
224 amdpm_rnd_callout(void *v)
225 {
226 struct amdpm_softc *sc = v;
227 u_int32_t reg;
228 #ifdef AMDPM_RND_COUNTERS
229 int i;
230 #endif
231
232 if ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGSTAT) &
233 AMDPM_RNGDONE) != 0) {
234 reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGDATA);
235 rnd_add_data(&sc->sc_rnd_source, ®,
236 sizeof(reg), sizeof(reg) * NBBY);
237 #ifdef AMDPM_RND_COUNTERS
238 AMDPM_RNDCNT_INCR(&sc->sc_rnd_hits);
239 for (i = 0; i < sizeof(reg); i++, reg >>= NBBY)
240 AMDPM_RNDCNT_INCR(&sc->sc_rnd_data[reg & 0xff]);
241 #endif
242 } else
243 AMDPM_RNDCNT_INCR(&sc->sc_rnd_miss);
244 callout_reset(&sc->sc_rnd_ch, 1, amdpm_rnd_callout, sc);
245 }
246
247 #ifdef __HAVE_TIMECOUNTER
248 uint32_t
249 amdpm_get_timecount(struct timecounter *tc)
250 {
251 struct amdpm_softc *sc = tc->tc_priv;
252
253 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_TMR);
254 }
255 #endif
256