xref: /openbsd-src/sys/dev/pci/pchtemp.c (revision 897fc685943471cf985a0fe38ba076ea6fe74fa5)
1 /*	$OpenBSD: pchtemp.c,v 1.4 2016/05/24 07:59:24 reyk 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 };
67 
68 int
69 pchtemp_match(struct device *parent, void *match, void *aux)
70 {
71 	return (pci_matchbyid(aux, pchtemp_devices, nitems(pchtemp_devices)));
72 }
73 
74 void
75 pchtemp_attach(struct device *parent, struct device *self, void *aux)
76 {
77 	struct pchtemp_softc *sc = (struct pchtemp_softc *)self;
78 	struct pci_attach_args *pa = aux;
79 	pcireg_t memtype;
80 	uint8_t tsel;
81 
82 	memtype = PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT;
83 	if (pci_mapreg_map(pa, PCHTEMP_PCI_TBAR, memtype, 0,
84 	    &sc->sc_memt, &sc->sc_memh, NULL, &sc->sc_mems, 0)) {
85 		printf(": can't map registers\n");
86 		return;
87 	}
88 
89 	tsel = bus_space_read_1(sc->sc_memt, sc->sc_memh, PCHTEMP_TSEL);
90 	if ((tsel & PCHTEMP_TSEL_ETS) == 0) {
91 		printf(": disabled\n");
92 		goto unmap;
93 	}
94 
95 	pchtemp_refresh(sc);
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, pchtemp_refresh, 5) == NULL) {
104 		printf(": can't register update task\n");
105 		goto unmap;
106 	}
107 
108 	sensordev_install(&sc->sc_sensordev);
109 
110 	printf("\n");
111 	return;
112 
113  unmap:
114 	bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems);
115 }
116 
117 void
118 pchtemp_refresh(void *arg)
119 {
120 	struct pchtemp_softc *sc = arg;
121 	uint16_t temp;
122 
123 	temp = bus_space_read_2(sc->sc_memt, sc->sc_memh, PCHTEMP_TEMP);
124 	sc->sc_sensor.value = (temp * 500000 - 50000000) + 273150000;
125 }
126