1 /* $OpenBSD: km.c,v 1.5 2011/05/31 00:19:55 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 2008 Constantine A. Murenin <cnst+openbsd@bugmail.mojo.ru> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/param.h> 20 #include <sys/systm.h> 21 #include <sys/device.h> 22 #include <sys/sensors.h> 23 24 #include <machine/bus.h> 25 26 #include <dev/pci/pcireg.h> 27 #include <dev/pci/pcivar.h> 28 #include <dev/pci/pcidevs.h> 29 30 31 /* 32 * AMD Family 10h/11h/14h Processors, Function 3 -- Miscellaneous Control 33 */ 34 35 /* Function 3 Registers */ 36 #define KM_REP_TEMP_CONTR_R 0xa4 37 #define KM_THERMTRIP_STAT_R 0xe4 38 #define KM_NORTHBRIDGE_CAP_R 0xe8 39 #define KM_CPUID_FAMILY_MODEL_R 0xfc 40 41 /* Operations on Reported Temperature Control Register */ 42 #define KM_GET_CURTMP(r) (((r) >> 21) & 0x7ff) 43 44 /* Operations on Thermtrip Status Register */ 45 #define KM_GET_DIODEOFFSET(r) (((r) >> 8) & 0x7f) 46 47 48 struct km_softc { 49 struct device sc_dev; 50 51 pci_chipset_tag_t sc_pc; 52 pcitag_t sc_pcitag; 53 54 struct ksensor sc_sensor; 55 struct ksensordev sc_sensordev; 56 }; 57 58 int km_match(struct device *, void *, void *); 59 void km_attach(struct device *, struct device *, void *); 60 void km_refresh(void *); 61 62 struct cfattach km_ca = { 63 sizeof(struct km_softc), km_match, km_attach 64 }; 65 66 struct cfdriver km_cd = { 67 NULL, "km", DV_DULL 68 }; 69 70 static const struct pci_matchid km_devices[] = { 71 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_AMD64_10_MISC }, 72 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_AMD64_11_MISC }, 73 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_AMD64_14_MISC } 74 }; 75 76 77 int 78 km_match(struct device *parent, void *match, void *aux) 79 { 80 /* successful match supersedes pchb(4) */ 81 return pci_matchbyid((struct pci_attach_args *)aux, km_devices, 82 sizeof(km_devices) / sizeof(km_devices[0])) * 2; 83 } 84 85 void 86 km_attach(struct device *parent, struct device *self, void *aux) 87 { 88 struct km_softc *sc = (struct km_softc *)self; 89 struct pci_attach_args *pa = aux; 90 91 sc->sc_pc = pa->pa_pc; 92 sc->sc_pcitag = pa->pa_tag; 93 94 strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname, 95 sizeof(sc->sc_sensordev.xname)); 96 97 sc->sc_sensor.type = SENSOR_TEMP; 98 sensor_attach(&sc->sc_sensordev, &sc->sc_sensor); 99 100 if (sensor_task_register(sc, km_refresh, 5) == NULL) { 101 printf(": unable to register update task\n"); 102 return; 103 } 104 105 sensordev_install(&sc->sc_sensordev); 106 107 printf("\n"); 108 } 109 110 void 111 km_refresh(void *arg) 112 { 113 struct km_softc *sc = arg; 114 struct ksensor *s = &sc->sc_sensor; 115 pcireg_t r; 116 int c; 117 118 r = pci_conf_read(sc->sc_pc, sc->sc_pcitag, KM_REP_TEMP_CONTR_R); 119 c = KM_GET_CURTMP(r); 120 s->value = c * 125000 + 273150000; 121 } 122