xref: /openbsd-src/sys/dev/pci/pchtemp.c (revision c020cf82e0cc147236f01a8dca7052034cf9d30d)
1 /*	$OpenBSD: pchtemp.c,v 1.5 2020/06/06 18:56:32 deraadt 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 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_9SERIES_LP_THERM },
64 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_100SERIES_THERM },
65 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_100SERIES_LP_THERM },
66 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_300SERIES_U_THERM },
67 };
68 
69 int
70 pchtemp_match(struct device *parent, void *match, void *aux)
71 {
72 	return (pci_matchbyid(aux, pchtemp_devices, nitems(pchtemp_devices)));
73 }
74 
75 void
76 pchtemp_attach(struct device *parent, struct device *self, void *aux)
77 {
78 	struct pchtemp_softc *sc = (struct pchtemp_softc *)self;
79 	struct pci_attach_args *pa = aux;
80 	pcireg_t memtype;
81 	uint8_t tsel;
82 
83 	memtype = PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT;
84 	if (pci_mapreg_map(pa, PCHTEMP_PCI_TBAR, memtype, 0,
85 	    &sc->sc_memt, &sc->sc_memh, NULL, &sc->sc_mems, 0)) {
86 		printf(": can't map registers\n");
87 		return;
88 	}
89 
90 	tsel = bus_space_read_1(sc->sc_memt, sc->sc_memh, PCHTEMP_TSEL);
91 	if ((tsel & PCHTEMP_TSEL_ETS) == 0) {
92 		printf(": disabled\n");
93 		goto unmap;
94 	}
95 
96 	pchtemp_refresh(sc);
97 
98 	strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
99 	    sizeof(sc->sc_sensordev.xname));
100 
101 	sc->sc_sensor.type = SENSOR_TEMP;
102 	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
103 
104 	if (sensor_task_register(sc, pchtemp_refresh, 5) == NULL) {
105 		printf(": can't register update task\n");
106 		goto unmap;
107 	}
108 
109 	sensordev_install(&sc->sc_sensordev);
110 
111 	printf("\n");
112 	return;
113 
114  unmap:
115 	bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems);
116 }
117 
118 void
119 pchtemp_refresh(void *arg)
120 {
121 	struct pchtemp_softc *sc = arg;
122 	uint16_t temp;
123 
124 	temp = bus_space_read_2(sc->sc_memt, sc->sc_memh, PCHTEMP_TEMP);
125 	sc->sc_sensor.value = (temp * 500000 - 50000000) + 273150000;
126 }
127