1 /* $OpenBSD: pchtemp.c,v 1.7 2022/03/11 18:00:51 mpi Exp $ */
2 /*
3 * Copyright (c) 2015 Mark Kettenis
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 /*
19 * Intel X99, C610, 9 Series and 100 Series PCH thermal sensor
20 * controller driver
21 */
22
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/device.h>
26 #include <sys/sensors.h>
27
28 #include <dev/pci/pcireg.h>
29 #include <dev/pci/pcivar.h>
30 #include <dev/pci/pcidevs.h>
31
32 #define PCHTEMP_PCI_TBAR 0x10
33
34 #define PCHTEMP_TEMP 0x00
35 #define PCHTEMP_TSEL 0x08
36 #define PCHTEMP_TSEL_ETS 0x01
37
38 struct pchtemp_softc {
39 struct device sc_dev;
40
41 bus_space_tag_t sc_memt;
42 bus_space_handle_t sc_memh;
43 bus_size_t sc_mems;
44
45 struct ksensor sc_sensor;
46 struct ksensordev sc_sensordev;
47 };
48
49 int pchtemp_match(struct device *, void *, void *);
50 void pchtemp_attach(struct device *, struct device *, void *);
51 void pchtemp_refresh(void *);
52
53 struct cfdriver pchtemp_cd = {
54 NULL, "pchtemp", DV_DULL
55 };
56
57 const struct cfattach pchtemp_ca = {
58 sizeof(struct pchtemp_softc), pchtemp_match, pchtemp_attach
59 };
60
61 const struct pci_matchid pchtemp_devices[] = {
62 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_C610_THERM },
63 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_8SERIES_LP_THERM },
64 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_9SERIES_LP_THERM },
65 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_100SERIES_THERM },
66 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_100SERIES_LP_THERM },
67 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_300SERIES_THERM },
68 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_300SERIES_U_THERM },
69 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_400SERIES_THERM },
70 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_400SERIES_LP_THERM },
71 };
72
73 int
pchtemp_match(struct device * parent,void * match,void * aux)74 pchtemp_match(struct device *parent, void *match, void *aux)
75 {
76 return (pci_matchbyid(aux, pchtemp_devices, nitems(pchtemp_devices)));
77 }
78
79 void
pchtemp_attach(struct device * parent,struct device * self,void * aux)80 pchtemp_attach(struct device *parent, struct device *self, void *aux)
81 {
82 struct pchtemp_softc *sc = (struct pchtemp_softc *)self;
83 struct pci_attach_args *pa = aux;
84 pcireg_t memtype;
85 uint8_t tsel;
86
87 memtype = PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT;
88 if (pci_mapreg_map(pa, PCHTEMP_PCI_TBAR, memtype, 0,
89 &sc->sc_memt, &sc->sc_memh, NULL, &sc->sc_mems, 0)) {
90 printf(": can't map registers\n");
91 return;
92 }
93
94 tsel = bus_space_read_1(sc->sc_memt, sc->sc_memh, PCHTEMP_TSEL);
95 if ((tsel & PCHTEMP_TSEL_ETS) == 0) {
96 printf(": disabled\n");
97 goto unmap;
98 }
99
100 pchtemp_refresh(sc);
101
102 strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
103 sizeof(sc->sc_sensordev.xname));
104
105 sc->sc_sensor.type = SENSOR_TEMP;
106 sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
107
108 if (sensor_task_register(sc, pchtemp_refresh, 5) == NULL) {
109 printf(": can't register update task\n");
110 goto unmap;
111 }
112
113 sensordev_install(&sc->sc_sensordev);
114
115 printf("\n");
116 return;
117
118 unmap:
119 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems);
120 }
121
122 void
pchtemp_refresh(void * arg)123 pchtemp_refresh(void *arg)
124 {
125 struct pchtemp_softc *sc = arg;
126 uint16_t temp;
127
128 temp = bus_space_read_2(sc->sc_memt, sc->sc_memh, PCHTEMP_TEMP);
129 sc->sc_sensor.value = (temp * 500000 - 50000000) + 273150000;
130 }
131