1 /* $OpenBSD: kate.c,v 1.2 2008/03/27 04:52:03 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 NPT Family 0Fh Processors, Function 3 -- Miscellaneous Control
32 /* Function 3 Registers */
33 #define K_THERMTRIP_STAT_R 0xe4
34 #define K_NORTHBRIDGE_CAP_R 0xe8
35 #define K_CPUID_FAMILY_MODEL_R 0xfc
37 /* Bits within Thermtrip Status Register */
38 #define K_THERM_SENSE_SEL (1 << 6)
39 #define K_THERM_SENSE_CORE_SEL (1 << 2)
41 /* Flip core and sensor selection bits */
42 #define K_T_SEL_C0(v) (v |= K_THERM_SENSE_CORE_SEL)
43 #define K_T_SEL_C1(v) (v &= ~(K_THERM_SENSE_CORE_SEL))
44 #define K_T_SEL_S0(v) (v &= ~(K_THERM_SENSE_SEL))
45 #define K_T_SEL_S1(v) (v |= K_THERM_SENSE_SEL)
49 * Revision Guide for AMD NPT Family 0Fh Processors,
50 * Publication # 33610, Revision 3.30, February 2008
54 const uint32_t cpuid[5];
56 { "BH-F", { 0x00040FB0, 0x00040F80, 0, 0, 0 } }, /* F2 */
57 { "DH-F", { 0x00040FF0, 0x00050FF0, 0x00040FC0, 0, 0 } }, /* F2, F3 */
58 { "JH-F", { 0x00040F10, 0x00040F30, 0x000C0F10, 0, 0 } }, /* F2, F3 */
59 { "BH-G", { 0x00060FB0, 0x00060F80, 0, 0, 0 } }, /* G1, G2 */
60 { "DH-G", { 0x00070FF0, 0x00060FF0,
61 0x00060FC0, 0x00070FC0, 0 } } /* G1, G2 */
66 struct device *sc_dev;
68 struct ksensor sc_sensors[4];
69 struct ksensordev sc_sensordev;
76 static void kate_identify(driver_t *, struct device *);
77 static int kate_probe(struct device *);
78 static int kate_attach(struct device *);
79 static int kate_detach(struct device *);
80 static void kate_refresh(void *);
82 static device_method_t kate_methods[] = {
83 DEVMETHOD(device_identify, kate_identify),
84 DEVMETHOD(device_probe, kate_probe),
85 DEVMETHOD(device_attach, kate_attach),
86 DEVMETHOD(device_detach, kate_detach),
90 static driver_t kate_driver = {
93 sizeof(struct kate_softc)
96 static devclass_t kate_devclass;
98 DRIVER_MODULE(kate, hostb, kate_driver, kate_devclass, NULL, NULL);
102 kate_identify(driver_t *driver, struct device *parent)
104 if (kate_probe(parent) == ENXIO)
106 if (device_find_child(parent, driver->name, -1) != NULL)
108 device_add_child(parent, driver->name, -1);
112 kate_probe(struct device *dev)
115 struct kate_softc ks;
116 struct kate_softc *sc = &ks;
121 if (pci_get_vendor(dev) != PCI_VENDOR_AMD ||
122 pci_get_device(dev) != PCI_PRODUCT_AMD_AMD64_MISC)
125 /* just in case we probe successfully, set the description */
126 if (device_get_desc(dev) == NULL)
128 "AMD Family 0Fh temperature sensors");
131 * First, let's probe for chips at or after Revision F, which is
132 * when the temperature readings were officially introduced.
134 c = pci_read_config(dev, K_CPUID_FAMILY_MODEL_R, 4);
135 for (i = 0; i < sizeof(kate_proc) / sizeof(kate_proc[0]); i++)
136 for (j = 0; kate_proc[i].cpuid[j] != 0; j++)
137 if ((c & ~0xf) == kate_proc[i].cpuid[j])
142 * If the probe above was not successful, let's try to actually
143 * read the sensors from the chip, and see if they make any sense.
149 for (i = 0; i < 4; i++)
150 if (!(sc->sc_sensors[i].flags & SENSOR_FINVALID))
152 #endif /* !KATE_STRICT */
158 kate_attach(struct device *dev)
160 struct kate_softc *sc;
164 sc = device_get_softc(dev);
167 c = pci_read_config(dev, K_CPUID_FAMILY_MODEL_R, 4);
168 for (i = 0; i < sizeof(kate_proc) / sizeof(kate_proc[0]) &&
169 sc->sc_rev == '\0'; i++)
170 for (j = 0; kate_proc[i].cpuid[j] != 0; j++)
171 if ((c & ~0xf) == kate_proc[i].cpuid[j]) {
172 sc->sc_rev = kate_proc[i].rev[3];
173 device_printf(dev, "core rev %.4s%.1x\n",
174 kate_proc[i].rev, c & 0xf);
178 if (c != 0x0 && sc->sc_rev == '\0') {
179 /* CPUID Family Model Register was introduced in Revision F */
180 sc->sc_rev = 'G'; /* newer than E, assume G */
181 device_printf(dev, "cpuid 0x%x\n", c);
184 d = pci_read_config(dev, K_NORTHBRIDGE_CAP_R, 4);
185 cmpcap = (d >> 12) & 0x3;
192 if ((sc->sc_sensors[0].flags & SENSOR_FINVALID) &&
193 (sc->sc_sensors[1].flags & SENSOR_FINVALID))
195 if ((sc->sc_sensors[4].flags & SENSOR_FINVALID))
199 sc->sc_ii = cmpcap ? 0 : 2;
201 #endif /* !KATE_STRICT */
203 strlcpy(sc->sc_sensordev.xname, device_get_nameunit(dev),
204 sizeof(sc->sc_sensordev.xname));
206 for (i = sc->sc_ii; i < sc->sc_in; i++) {
207 sc->sc_sensors[i].type = SENSOR_TEMP;
208 sensor_attach(&sc->sc_sensordev, &sc->sc_sensors[i]);
211 if (sensor_task_register(sc, kate_refresh, 5)) {
212 device_printf(dev, "unable to register update task\n");
216 sensordev_install(&sc->sc_sensordev);
221 kate_detach(struct device *dev)
223 struct kate_softc *sc = device_get_softc(dev);
225 sensordev_deinstall(&sc->sc_sensordev);
226 sensor_task_unregister(sc);
231 kate_refresh(void *arg)
233 struct kate_softc *sc = arg;
234 struct ksensor *s = sc->sc_sensors;
238 t = pci_read_config(sc->sc_dev, K_THERMTRIP_STAT_R, 4);
240 for (i = sc->sc_ii; i < sc->sc_in; i++) {
259 m = t & (K_THERM_SENSE_CORE_SEL | K_THERM_SENSE_SEL);
260 pci_write_config(sc->sc_dev, K_THERMTRIP_STAT_R, t, 4);
261 t = pci_read_config(sc->sc_dev, K_THERMTRIP_STAT_R, 4);
262 v = 0x3ff & (t >> 14);
264 if (sc->sc_rev != 'G')
266 #endif /* KATE_STRICT */
267 if ((t & (K_THERM_SENSE_CORE_SEL | K_THERM_SENSE_SEL)) == m &&
269 s[i].flags &= ~SENSOR_FINVALID;
271 s[i].flags |= SENSOR_FINVALID;
272 s[i].value = (v * 250000 - 49000000) + 273150000;