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