1 /* $OpenBSD: km.c,v 1.8 2013/09/17 13:42:34 kettenis 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/15h 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 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_AMD64_15_0x_MISC }, 75 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_AMD64_15_1x_MISC } 76 }; 77 78 79 int 80 km_match(struct device *parent, void *match, void *aux) 81 { 82 /* successful match supersedes pchb(4) */ 83 return pci_matchbyid((struct pci_attach_args *)aux, km_devices, 84 sizeof(km_devices) / sizeof(km_devices[0])) * 2; 85 } 86 87 void 88 km_attach(struct device *parent, struct device *self, void *aux) 89 { 90 struct km_softc *sc = (struct km_softc *)self; 91 struct pci_attach_args *pa = aux; 92 93 sc->sc_pc = pa->pa_pc; 94 sc->sc_pcitag = pa->pa_tag; 95 96 strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname, 97 sizeof(sc->sc_sensordev.xname)); 98 99 sc->sc_sensor.type = SENSOR_TEMP; 100 sensor_attach(&sc->sc_sensordev, &sc->sc_sensor); 101 102 if (sensor_task_register(sc, km_refresh, 5) == NULL) { 103 printf(": unable to register update task\n"); 104 return; 105 } 106 107 sensordev_install(&sc->sc_sensordev); 108 109 printf("\n"); 110 } 111 112 void 113 km_refresh(void *arg) 114 { 115 struct km_softc *sc = arg; 116 struct ksensor *s = &sc->sc_sensor; 117 pcireg_t r; 118 int c; 119 120 r = pci_conf_read(sc->sc_pc, sc->sc_pcitag, KM_REP_TEMP_CONTR_R); 121 c = KM_GET_CURTMP(r); 122 s->value = c * 125000 + 273150000; 123 } 124