xref: /netbsd-src/sys/dev/pci/amdpm.c (revision a536ee5124e62c9a0051a252f7833dc8f50f44c9)
1 /*	$NetBSD: amdpm.c,v 1.36 2012/10/27 17:18:28 chs 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: amdpm.c,v 1.36 2012/10/27 17:18:28 chs Exp $");
34 
35 #include "opt_amdpm.h"
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/device.h>
41 #include <sys/callout.h>
42 #include <sys/rnd.h>
43 
44 #include <sys/bus.h>
45 #include <dev/ic/acpipmtimer.h>
46 
47 #include <dev/i2c/i2cvar.h>
48 
49 #include <dev/pci/pcivar.h>
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcidevs.h>
52 
53 #include <dev/pci/amdpmreg.h>
54 #include <dev/pci/amdpmvar.h>
55 #include <dev/pci/amdpm_smbusreg.h>
56 
57 static void	amdpm_rnd_callout(void *);
58 
59 #ifdef AMDPM_RND_COUNTERS
60 #define	AMDPM_RNDCNT_INCR(ev)	(ev)->ev_count++
61 #endif
62 
63 static int
64 amdpm_match(device_t parent, cfdata_t match, void *aux)
65 {
66 	struct pci_attach_args *pa = aux;
67 
68 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD) {
69 		switch (PCI_PRODUCT(pa->pa_id)) {
70 		case PCI_PRODUCT_AMD_PBC768_PMC:
71 		case PCI_PRODUCT_AMD_PBC8111_ACPI:
72 			return (1);
73 		}
74 	}
75 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_NVIDIA) {
76 		switch (PCI_PRODUCT(pa->pa_id)) {
77 		case PCI_PRODUCT_NVIDIA_XBOX_SMBUS:
78 			return (1);
79 		}
80 	}
81 
82 	return (0);
83 }
84 
85 static void
86 amdpm_attach(device_t parent, device_t self, void *aux)
87 {
88 	struct amdpm_softc *sc = device_private(self);
89 	struct pci_attach_args *pa = aux;
90 	pcireg_t confreg, pmptrreg;
91 	u_int32_t pmreg;
92 	int i;
93 
94 	pci_aprint_devinfo(pa, NULL);
95 
96 	sc->sc_dev = self;
97 
98 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_NVIDIA_XBOX_SMBUS)
99 		sc->sc_nforce = 1;
100 	else
101 		sc->sc_nforce = 0;
102 
103 	sc->sc_pc = pa->pa_pc;
104 	sc->sc_tag = pa->pa_tag;
105 	sc->sc_iot = pa->pa_iot;
106 	sc->sc_pa = pa;
107 
108 #if 0
109 	aprint_normal_dev(self, "");
110 	pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
111 #endif
112 
113 	confreg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
114 	/* enable pm i/o space for AMD-8111 and nForce */
115 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC8111_ACPI ||
116 	    sc->sc_nforce)
117 		confreg |= AMDPM_PMIOEN;
118 
119 	/* Enable random number generation for everyone */
120 	pci_conf_write(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG,
121 	    confreg | AMDPM_RNGEN);
122 	confreg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_CONFREG);
123 
124 	if ((confreg & AMDPM_PMIOEN) == 0) {
125 		aprint_error_dev(self, "PMxx space isn't enabled\n");
126 		return;
127 	}
128 
129 	if (sc->sc_nforce) {
130 		pmptrreg = pci_conf_read(pa->pa_pc, pa->pa_tag, NFORCE_PMPTR);
131 		aprint_normal_dev(self, "power management at 0x%04x\n",
132 		    NFORCE_PMBASE(pmptrreg));
133 		if (bus_space_map(sc->sc_iot, NFORCE_PMBASE(pmptrreg),
134 		    AMDPM_PMSIZE, 0, &sc->sc_ioh)) {
135 			aprint_error_dev(self, "failed to map PMxx space\n");
136 			return;
137 		}
138 	} else {
139 		pmptrreg = pci_conf_read(pa->pa_pc, pa->pa_tag, AMDPM_PMPTR);
140 		if (bus_space_map(sc->sc_iot, AMDPM_PMBASE(pmptrreg),
141 		    AMDPM_PMSIZE, 0, &sc->sc_ioh)) {
142 			aprint_error_dev(self, "failed to map PMxx space\n");
143 			return;
144 		}
145 	}
146 
147 	/* don't attach a timecounter on nforce boards */
148 	if ((confreg & AMDPM_TMRRST) == 0 && (confreg & AMDPM_STOPTMR) == 0 &&
149 	    !sc->sc_nforce) {
150 		acpipmtimer_attach(self, sc->sc_iot, sc->sc_ioh,
151 		  AMDPM_TMR, ((confreg & AMDPM_TMR32) ? ACPIPMT_32BIT : 0));
152 	}
153 
154 	/* try to attach devices on the smbus */
155 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC8111_ACPI ||
156 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_PBC768_PMC ||
157 	    sc->sc_nforce) {
158 		amdpm_smbus_attach(sc);
159 	}
160 
161 	if (confreg & AMDPM_RNGEN) {
162 		/* Check to see if we can read data from the RNG. */
163 		(void) bus_space_read_4(sc->sc_iot, sc->sc_ioh,
164 		    AMDPM_RNGDATA);
165 		for (i = 0; i < 1000; i++) {
166 			pmreg = bus_space_read_4(sc->sc_iot,
167 			    sc->sc_ioh, AMDPM_RNGSTAT);
168 			if (pmreg & AMDPM_RNGDONE)
169 				break;
170 			delay(1);
171 		}
172 		if ((pmreg & AMDPM_RNGDONE) != 0) {
173 			aprint_normal_dev(self, ""
174 			    "random number generator enabled (apprx. %dms)\n",
175 			    i);
176 			callout_init(&sc->sc_rnd_ch, 0);
177 			rnd_attach_source(&sc->sc_rnd_source,
178 			    device_xname(self), RND_TYPE_RNG,
179 			    /*
180 			     * XXX Careful!  The use of RND_FLAG_NO_ESTIMATE
181 			     * XXX here is unobvious: we later feed raw bits
182 			     * XXX into the "entropy pool" with rnd_add_data,
183 			     * XXX explicitly supplying an entropy estimate.
184 			     * XXX In this context, NO_ESTIMATE serves only
185 			     * XXX to prevent rnd_add_data from trying to
186 			     * XXX use the *time at which we added the data*
187 			     * XXX as entropy, which is not a good idea since
188 			     * XXX we add data periodically from a callout.
189 			     */
190 			    RND_FLAG_NO_ESTIMATE);
191 #ifdef AMDPM_RND_COUNTERS
192 			evcnt_attach_dynamic(&sc->sc_rnd_hits, EVCNT_TYPE_MISC,
193 			    NULL, device_xname(self), "rnd hits");
194 			evcnt_attach_dynamic(&sc->sc_rnd_miss, EVCNT_TYPE_MISC,
195 			    NULL, device_xname(self), "rnd miss");
196 			for (i = 0; i < 256; i++) {
197 				evcnt_attach_dynamic(&sc->sc_rnd_data[i],
198 				    EVCNT_TYPE_MISC, NULL, device_xname(self),
199 				    "rnd data");
200 			}
201 #endif
202 			amdpm_rnd_callout(sc);
203 		}
204 	}
205 }
206 
207 CFATTACH_DECL_NEW(amdpm, sizeof(struct amdpm_softc),
208     amdpm_match, amdpm_attach, NULL, NULL);
209 
210 static void
211 amdpm_rnd_callout(void *v)
212 {
213 	struct amdpm_softc *sc = v;
214 	u_int32_t rngreg;
215 #ifdef AMDPM_RND_COUNTERS
216 	int i;
217 #endif
218 
219 	if ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, AMDPM_RNGSTAT) &
220 	    AMDPM_RNGDONE) != 0) {
221 		rngreg = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
222 		    AMDPM_RNGDATA);
223 		rnd_add_data(&sc->sc_rnd_source, &rngreg,
224 		    sizeof(rngreg), sizeof(rngreg) * NBBY);
225 #ifdef AMDPM_RND_COUNTERS
226 		AMDPM_RNDCNT_INCR(&sc->sc_rnd_hits);
227 		for (i = 0; i < sizeof(rngreg); i++, rngreg >>= NBBY)
228 			AMDPM_RNDCNT_INCR(&sc->sc_rnd_data[rngreg & 0xff]);
229 #endif
230 	}
231 #ifdef AMDPM_RND_COUNTERS
232 	else
233 		AMDPM_RNDCNT_INCR(&sc->sc_rnd_miss);
234 #endif
235 	callout_reset(&sc->sc_rnd_ch, 1, amdpm_rnd_callout, sc);
236 }
237