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