1 /* $OpenBSD: km.c,v 1.2 2008/08/29 03:38:31 cnst Exp $ */
4 * Copyright (c) 2008/2010 Constantine A. Murenin <cnst+dfly@bugmail.mojo.ru>
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.
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.
19 #include <sys/param.h>
20 #include <sys/systm.h>
22 #include <sys/sensors.h>
24 #include <bus/pci/pcivar.h>
25 #include <bus/pci/pcidevs.h>
29 * AMD Family 10h Processors, Function 3 -- Miscellaneous Control
32 /* Function 3 Registers */
33 #define KM_REP_TEMP_CONTR_R 0xa4
34 #define KM_THERMTRIP_STAT_R 0xe4
35 #define KM_NORTHBRIDGE_CAP_R 0xe8
36 #define KM_CPUID_FAMILY_MODEL_R 0xfc
38 /* Operations on Reported Temperature Control Register */
39 #define KM_GET_CURTMP(r) (((r) >> 21) & 0x7ff)
41 /* Operations on Thermtrip Status Register */
42 #define KM_GET_DIODEOFFSET(r) (((r) >> 8) & 0x7f)
46 struct device *sc_dev;
47 struct ksensor sc_sensor;
48 struct ksensordev sc_sensordev;
51 static void km_identify(driver_t *, struct device *);
52 static int km_probe(struct device *);
53 static int km_attach(struct device *);
54 static int km_detach(struct device *);
55 static void km_refresh(void *);
57 static device_method_t km_methods[] = {
58 DEVMETHOD(device_identify, km_identify),
59 DEVMETHOD(device_probe, km_probe),
60 DEVMETHOD(device_attach, km_attach),
61 DEVMETHOD(device_detach, km_detach),
65 static driver_t km_driver = {
68 sizeof(struct km_softc)
71 static devclass_t km_devclass;
73 DRIVER_MODULE(km, hostb, km_driver, km_devclass, NULL, NULL);
77 km_identify(driver_t *driver, struct device *parent)
79 if (km_probe(parent) == ENXIO)
81 if (device_find_child(parent, driver->name, -1) != NULL)
83 device_add_child(parent, driver->name, -1);
87 km_probe(struct device *dev)
91 if (pci_get_vendor(dev) != PCI_VENDOR_AMD)
94 switch (pci_get_device(dev)) {
95 case PCI_PRODUCT_AMD_AMD64_F10_MISC:
98 case PCI_PRODUCT_AMD_AMD64_F11_MISC:
99 if (device_get_desc(dev) == NULL)
100 device_set_desc(dev, ten ?
101 "AMD Family 10h temperature sensor" :
102 "AMD Family 11h temperature sensor");
110 km_attach(struct device *dev)
114 sc = device_get_softc(dev);
117 strlcpy(sc->sc_sensordev.xname, device_get_nameunit(dev),
118 sizeof(sc->sc_sensordev.xname));
120 sc->sc_sensor.type = SENSOR_TEMP;
121 sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
123 if (sensor_task_register(sc, km_refresh, 5)) {
124 device_printf(dev, "unable to register update task\n");
128 sensordev_install(&sc->sc_sensordev);
133 km_detach(struct device *dev)
135 struct km_softc *sc = device_get_softc(dev);
137 sensordev_deinstall(&sc->sc_sensordev);
138 sensor_task_unregister(sc);
143 km_refresh(void *arg)
145 struct km_softc *sc = arg;
146 struct ksensor *s = &sc->sc_sensor;
150 r = pci_read_config(sc->sc_dev, KM_REP_TEMP_CONTR_R, 4);
151 c = KM_GET_CURTMP(r);
152 s->value = c * 125000 + 273150000;