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