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