summaryrefslogtreecommitdiff
path: root/sys/dev/pci/pchtemp.c
blob: 59c95c4d84283ea3ff2bd204ec539a314dfead55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*	$OpenBSD: pchtemp.c,v 1.7 2022/03/11 18:00:51 mpi Exp $	*/
/*
 * Copyright (c) 2015 Mark Kettenis
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * Intel X99, C610, 9 Series and 100 Series PCH thermal sensor
 * controller driver
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/sensors.h>

#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <dev/pci/pcidevs.h>

#define PCHTEMP_PCI_TBAR	0x10

#define PCHTEMP_TEMP		0x00
#define PCHTEMP_TSEL		0x08
#define  PCHTEMP_TSEL_ETS	0x01

struct pchtemp_softc {
	struct device		sc_dev;

	bus_space_tag_t		sc_memt;
	bus_space_handle_t	sc_memh;
	bus_size_t		sc_mems;

	struct ksensor		sc_sensor;
	struct ksensordev	sc_sensordev;
};

int	pchtemp_match(struct device *, void *, void *);
void	pchtemp_attach(struct device *, struct device *, void *);
void	pchtemp_refresh(void *);

struct cfdriver pchtemp_cd = {
	NULL, "pchtemp", DV_DULL
};

const struct cfattach pchtemp_ca = {
	sizeof(struct pchtemp_softc), pchtemp_match, pchtemp_attach
};

const struct pci_matchid pchtemp_devices[] = {
	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_C610_THERM },
	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_8SERIES_LP_THERM },
	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_9SERIES_LP_THERM },
	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_100SERIES_THERM },
	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_100SERIES_LP_THERM },
	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_300SERIES_THERM },
	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_300SERIES_U_THERM },
	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_400SERIES_THERM },
	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_400SERIES_LP_THERM },
};

int
pchtemp_match(struct device *parent, void *match, void *aux)
{
	return (pci_matchbyid(aux, pchtemp_devices, nitems(pchtemp_devices)));
}

void
pchtemp_attach(struct device *parent, struct device *self, void *aux)
{
	struct pchtemp_softc *sc = (struct pchtemp_softc *)self;
	struct pci_attach_args *pa = aux;
	pcireg_t memtype;
	uint8_t tsel;

	memtype = PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT;
	if (pci_mapreg_map(pa, PCHTEMP_PCI_TBAR, memtype, 0,
	    &sc->sc_memt, &sc->sc_memh, NULL, &sc->sc_mems, 0)) {
		printf(": can't map registers\n");
		return;
	}

	tsel = bus_space_read_1(sc->sc_memt, sc->sc_memh, PCHTEMP_TSEL);
	if ((tsel & PCHTEMP_TSEL_ETS) == 0) {
		printf(": disabled\n");
		goto unmap;
	}

	pchtemp_refresh(sc);

	strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
	    sizeof(sc->sc_sensordev.xname));

	sc->sc_sensor.type = SENSOR_TEMP;
	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);

	if (sensor_task_register(sc, pchtemp_refresh, 5) == NULL) {
		printf(": can't register update task\n");
		goto unmap;
	}

	sensordev_install(&sc->sc_sensordev);

	printf("\n");
	return;

 unmap:
	bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems);
}

void
pchtemp_refresh(void *arg)
{
	struct pchtemp_softc *sc = arg;
	uint16_t temp;

	temp = bus_space_read_2(sc->sc_memt, sc->sc_memh, PCHTEMP_TEMP);
	sc->sc_sensor.value = (temp * 500000 - 50000000) + 273150000;
}