1 /* $OpenBSD: km.c,v 1.14 2022/03/11 18:00:50 mpi 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/pcivar.h>
27 #include <dev/pci/pcidevs.h>
28
29
30 /*
31 * AMD Family > 10h Processors, Function 3 -- Miscellaneous Control
32 */
33
34 /* Function 3 Registers */
35 #define KM_REP_TEMP_CONTR_R 0xa4
36 #define KM_THERMTRIP_STAT_R 0xe4
37 #define KM_NORTHBRIDGE_CAP_R 0xe8
38 #define KM_CPUID_FAMILY_MODEL_R 0xfc
39
40 /* Operations on Reported Temperature Control Register */
41 #define KM_GET_CURTMP(r) (((r) >> 21) & 0x7ff)
42
43 /* Operations on Thermtrip Status Register */
44 #define KM_GET_DIODEOFFSET(r) (((r) >> 8) & 0x7f)
45
46
47 struct km_softc {
48 struct device sc_dev;
49
50 pci_chipset_tag_t sc_pc;
51 pcitag_t sc_pcitag;
52
53 struct ksensor sc_sensor;
54 struct ksensordev sc_sensordev;
55 };
56
57 int km_match(struct device *, void *, void *);
58 void km_attach(struct device *, struct device *, void *);
59 void km_refresh(void *);
60
61 const struct cfattach km_ca = {
62 sizeof(struct km_softc), km_match, km_attach
63 };
64
65 struct cfdriver km_cd = {
66 NULL, "km", DV_DULL
67 };
68
69 static const struct pci_matchid km_devices[] = {
70 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_10_MISC },
71 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_11_MISC },
72 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_14_MISC },
73 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_15_0X_MISC },
74 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_15_1X_MISC },
75 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_16_MISC },
76 { PCI_VENDOR_AMD, PCI_PRODUCT_AMD_16_3X_MISC }
77 };
78
79
80 int
km_match(struct device * parent,void * match,void * aux)81 km_match(struct device *parent, void *match, void *aux)
82 {
83 /* successful match supersedes pchb(4) */
84 return pci_matchbyid((struct pci_attach_args *)aux, km_devices,
85 sizeof(km_devices) / sizeof(km_devices[0])) * 2;
86 }
87
88 void
km_attach(struct device * parent,struct device * self,void * aux)89 km_attach(struct device *parent, struct device *self, void *aux)
90 {
91 struct km_softc *sc = (struct km_softc *)self;
92 struct pci_attach_args *pa = aux;
93
94 sc->sc_pc = pa->pa_pc;
95 sc->sc_pcitag = pa->pa_tag;
96
97 strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
98 sizeof(sc->sc_sensordev.xname));
99
100 sc->sc_sensor.type = SENSOR_TEMP;
101 sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
102
103 if (sensor_task_register(sc, km_refresh, 5) == NULL) {
104 printf(": unable to register update task\n");
105 return;
106 }
107
108 sensordev_install(&sc->sc_sensordev);
109
110 printf("\n");
111 }
112
113 void
km_refresh(void * arg)114 km_refresh(void *arg)
115 {
116 struct km_softc *sc = arg;
117 struct ksensor *s = &sc->sc_sensor;
118 pcireg_t r;
119 int c;
120
121 r = pci_conf_read(sc->sc_pc, sc->sc_pcitag, KM_REP_TEMP_CONTR_R);
122 c = KM_GET_CURTMP(r);
123 s->value = c * 125000 + 273150000;
124 }
125