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