13d3afab1SImre Vadasz /* $OpenBSD: km.c,v 1.8 2013/09/17 13:42:34 kettenis Exp $ */ 239990074SConstantine A. Murenin 339990074SConstantine A. Murenin /* 4909d094eSSascha Wildner * Copyright (c) 2008/2010 Constantine A. Murenin <cnst+dfly@bugmail.mojo.ru> 539990074SConstantine A. Murenin * 639990074SConstantine A. Murenin * Permission to use, copy, modify, and distribute this software for any 739990074SConstantine A. Murenin * purpose with or without fee is hereby granted, provided that the above 839990074SConstantine A. Murenin * copyright notice and this permission notice appear in all copies. 939990074SConstantine A. Murenin * 1039990074SConstantine A. Murenin * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 1139990074SConstantine A. Murenin * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1239990074SConstantine A. Murenin * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1339990074SConstantine A. Murenin * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1439990074SConstantine A. Murenin * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1539990074SConstantine A. Murenin * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1639990074SConstantine A. Murenin * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1739990074SConstantine A. Murenin */ 1839990074SConstantine A. Murenin 1939990074SConstantine A. Murenin #include <sys/param.h> 2039990074SConstantine A. Murenin #include <sys/systm.h> 2139990074SConstantine A. Murenin #include <sys/bus.h> 2239990074SConstantine A. Murenin #include <sys/sensors.h> 2339990074SConstantine A. Murenin 24*77887bbeSImre Vadasz #include <machine/md_var.h> 25*77887bbeSImre Vadasz #include <machine/specialreg.h> 26*77887bbeSImre Vadasz 2739990074SConstantine A. Murenin #include <bus/pci/pcivar.h> 28dcb4b80dSSascha Wildner #include "pcidevs.h" 2939990074SConstantine A. Murenin 3039990074SConstantine A. Murenin 3139990074SConstantine A. Murenin /* 323d3afab1SImre Vadasz * AMD Family 10h/11h/14h/15h Processors, Function 3 -- Miscellaneous Control 3339990074SConstantine A. Murenin */ 3439990074SConstantine A. Murenin 3539990074SConstantine A. Murenin /* Function 3 Registers */ 3639990074SConstantine A. Murenin #define KM_REP_TEMP_CONTR_R 0xa4 3739990074SConstantine A. Murenin #define KM_THERMTRIP_STAT_R 0xe4 3839990074SConstantine A. Murenin #define KM_NORTHBRIDGE_CAP_R 0xe8 3939990074SConstantine A. Murenin #define KM_CPUID_FAMILY_MODEL_R 0xfc 4039990074SConstantine A. Murenin 4139990074SConstantine A. Murenin /* Operations on Reported Temperature Control Register */ 4239990074SConstantine A. Murenin #define KM_GET_CURTMP(r) (((r) >> 21) & 0x7ff) 4339990074SConstantine A. Murenin 4439990074SConstantine A. Murenin /* Operations on Thermtrip Status Register */ 4539990074SConstantine A. Murenin #define KM_GET_DIODEOFFSET(r) (((r) >> 8) & 0x7f) 4639990074SConstantine A. Murenin 4739990074SConstantine A. Murenin 4839990074SConstantine A. Murenin struct km_softc { 4939990074SConstantine A. Murenin struct device *sc_dev; 5039990074SConstantine A. Murenin struct ksensor sc_sensor; 5139990074SConstantine A. Murenin struct ksensordev sc_sensordev; 5239990074SConstantine A. Murenin }; 5339990074SConstantine A. Murenin 5439990074SConstantine A. Murenin static void km_identify(driver_t *, struct device *); 5539990074SConstantine A. Murenin static int km_probe(struct device *); 5639990074SConstantine A. Murenin static int km_attach(struct device *); 5739990074SConstantine A. Murenin static int km_detach(struct device *); 5839990074SConstantine A. Murenin static void km_refresh(void *); 5939990074SConstantine A. Murenin 6039990074SConstantine A. Murenin static device_method_t km_methods[] = { 6139990074SConstantine A. Murenin DEVMETHOD(device_identify, km_identify), 6239990074SConstantine A. Murenin DEVMETHOD(device_probe, km_probe), 6339990074SConstantine A. Murenin DEVMETHOD(device_attach, km_attach), 6439990074SConstantine A. Murenin DEVMETHOD(device_detach, km_detach), 6539990074SConstantine A. Murenin { NULL, NULL } 6639990074SConstantine A. Murenin }; 6739990074SConstantine A. Murenin 6839990074SConstantine A. Murenin static driver_t km_driver = { 6939990074SConstantine A. Murenin "km", 7039990074SConstantine A. Murenin km_methods, 7139990074SConstantine A. Murenin sizeof(struct km_softc) 7239990074SConstantine A. Murenin }; 7339990074SConstantine A. Murenin 7439990074SConstantine A. Murenin static devclass_t km_devclass; 7539990074SConstantine A. Murenin 7639990074SConstantine A. Murenin DRIVER_MODULE(km, hostb, km_driver, km_devclass, NULL, NULL); 7739990074SConstantine A. Murenin 7839990074SConstantine A. Murenin 7939990074SConstantine A. Murenin static void 8039990074SConstantine A. Murenin km_identify(driver_t *driver, struct device *parent) 8139990074SConstantine A. Murenin { 8239990074SConstantine A. Murenin if (km_probe(parent) == ENXIO) 8339990074SConstantine A. Murenin return; 8439990074SConstantine A. Murenin if (device_find_child(parent, driver->name, -1) != NULL) 8539990074SConstantine A. Murenin return; 8639990074SConstantine A. Murenin device_add_child(parent, driver->name, -1); 8739990074SConstantine A. Murenin } 8839990074SConstantine A. Murenin 8939990074SConstantine A. Murenin static int 9039990074SConstantine A. Murenin km_probe(struct device *dev) 9139990074SConstantine A. Murenin { 923d3afab1SImre Vadasz char *desc; 9339990074SConstantine A. Murenin 9439990074SConstantine A. Murenin if (pci_get_vendor(dev) != PCI_VENDOR_AMD) 9539990074SConstantine A. Murenin return ENXIO; 9639990074SConstantine A. Murenin 9739990074SConstantine A. Murenin switch (pci_get_device(dev)) { 9839990074SConstantine A. Murenin case PCI_PRODUCT_AMD_AMD64_F10_MISC: 993d3afab1SImre Vadasz desc = "AMD Family 10h temperature sensor"; 1003d3afab1SImre Vadasz break; 10139990074SConstantine A. Murenin case PCI_PRODUCT_AMD_AMD64_F11_MISC: 1023d3afab1SImre Vadasz desc = "AMD Family 11h temperature sensor"; 1033d3afab1SImre Vadasz break; 1043d3afab1SImre Vadasz case PCI_PRODUCT_AMD_AMD64_F14_MISC: 105*77887bbeSImre Vadasz if (CPUID_TO_FAMILY(cpu_id) == 0x12) 106*77887bbeSImre Vadasz desc = "AMD Family 12h temperature sensor"; 107*77887bbeSImre Vadasz else 1083d3afab1SImre Vadasz desc = "AMD Family 14h temperature sensor"; 1093d3afab1SImre Vadasz break; 1103d3afab1SImre Vadasz case PCI_PRODUCT_AMD_AMD64_F15_0x_MISC: 1113d3afab1SImre Vadasz desc = "AMD Family 15/0xh temperature sensor"; 1123d3afab1SImre Vadasz break; 1133d3afab1SImre Vadasz case PCI_PRODUCT_AMD_AMD64_F15_1x_MISC: 1143d3afab1SImre Vadasz desc = "AMD Family 15/1xh temperature sensor"; 1153d3afab1SImre Vadasz break; 11655676870SImre Vadasz case PCI_PRODUCT_AMD_AMD64_F15_3x_MISC: 11755676870SImre Vadasz desc = "AMD Family 15/3xh temperature sensor"; 11855676870SImre Vadasz break; 11955676870SImre Vadasz case PCI_PRODUCT_AMD_AMD64_F16_MISC: 12055676870SImre Vadasz desc = "AMD Family 16h temperature sensor"; 12155676870SImre Vadasz break; 12239990074SConstantine A. Murenin default: 12339990074SConstantine A. Murenin return ENXIO; 12439990074SConstantine A. Murenin } 1253d3afab1SImre Vadasz 1263d3afab1SImre Vadasz if (device_get_desc(dev) == NULL) 1273d3afab1SImre Vadasz device_set_desc(dev, desc); 1283d3afab1SImre Vadasz return 0; 12939990074SConstantine A. Murenin } 13039990074SConstantine A. Murenin 13139990074SConstantine A. Murenin static int 13239990074SConstantine A. Murenin km_attach(struct device *dev) 13339990074SConstantine A. Murenin { 13439990074SConstantine A. Murenin struct km_softc *sc; 13539990074SConstantine A. Murenin 13639990074SConstantine A. Murenin sc = device_get_softc(dev); 13739990074SConstantine A. Murenin sc->sc_dev = dev; 13839990074SConstantine A. Murenin 13939990074SConstantine A. Murenin strlcpy(sc->sc_sensordev.xname, device_get_nameunit(dev), 14039990074SConstantine A. Murenin sizeof(sc->sc_sensordev.xname)); 14139990074SConstantine A. Murenin 14239990074SConstantine A. Murenin sc->sc_sensor.type = SENSOR_TEMP; 14339990074SConstantine A. Murenin sensor_attach(&sc->sc_sensordev, &sc->sc_sensor); 14439990074SConstantine A. Murenin 14539990074SConstantine A. Murenin if (sensor_task_register(sc, km_refresh, 5)) { 14639990074SConstantine A. Murenin device_printf(dev, "unable to register update task\n"); 14739990074SConstantine A. Murenin return ENXIO; 14839990074SConstantine A. Murenin } 14939990074SConstantine A. Murenin 15039990074SConstantine A. Murenin sensordev_install(&sc->sc_sensordev); 15139990074SConstantine A. Murenin return 0; 15239990074SConstantine A. Murenin } 15339990074SConstantine A. Murenin 15439990074SConstantine A. Murenin static int 15539990074SConstantine A. Murenin km_detach(struct device *dev) 15639990074SConstantine A. Murenin { 15739990074SConstantine A. Murenin struct km_softc *sc = device_get_softc(dev); 15839990074SConstantine A. Murenin 15939990074SConstantine A. Murenin sensordev_deinstall(&sc->sc_sensordev); 16039990074SConstantine A. Murenin sensor_task_unregister(sc); 16139990074SConstantine A. Murenin return 0; 16239990074SConstantine A. Murenin } 16339990074SConstantine A. Murenin 16439990074SConstantine A. Murenin static void 16539990074SConstantine A. Murenin km_refresh(void *arg) 16639990074SConstantine A. Murenin { 16739990074SConstantine A. Murenin struct km_softc *sc = arg; 16839990074SConstantine A. Murenin struct ksensor *s = &sc->sc_sensor; 16939990074SConstantine A. Murenin uint32_t r; 17039990074SConstantine A. Murenin int c; 17139990074SConstantine A. Murenin 17239990074SConstantine A. Murenin r = pci_read_config(sc->sc_dev, KM_REP_TEMP_CONTR_R, 4); 17339990074SConstantine A. Murenin c = KM_GET_CURTMP(r); 17439990074SConstantine A. Murenin s->value = c * 125000 + 273150000; 17539990074SConstantine A. Murenin } 176