xref: /dragonfly/sys/dev/powermng/km/km.c (revision bcb3e04d)
1 /*	$OpenBSD: km.c,v 1.2 2008/08/29 03:38:31 cnst Exp $	*/
2 
3 /*
4  * Copyright (c) 2008/2010 Constantine A. Murenin <cnst+dfly@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/bus.h>
22 #include <sys/sensors.h>
23 
24 #include <bus/pci/pcivar.h>
25 #include <bus/pci/pcidevs.h>
26 
27 
28 /*
29  * AMD Family 10h Processors, Function 3 -- Miscellaneous Control
30  */
31 
32 /* Function 3 Registers */
33 #define KM_REP_TEMP_CONTR_R	0xa4
34 #define KM_THERMTRIP_STAT_R	0xe4
35 #define KM_NORTHBRIDGE_CAP_R	0xe8
36 #define KM_CPUID_FAMILY_MODEL_R	0xfc
37 
38 /* Operations on Reported Temperature Control Register */
39 #define KM_GET_CURTMP(r)	(((r) >> 21) & 0x7ff)
40 
41 /* Operations on Thermtrip Status Register */
42 #define KM_GET_DIODEOFFSET(r)	(((r) >> 8) & 0x7f)
43 
44 
45 struct km_softc {
46 	struct device		*sc_dev;
47 	struct ksensor		sc_sensor;
48 	struct ksensordev	sc_sensordev;
49 };
50 
51 static void	km_identify(driver_t *, struct device *);
52 static int	km_probe(struct device *);
53 static int	km_attach(struct device *);
54 static int	km_detach(struct device *);
55 static void	km_refresh(void *);
56 
57 static device_method_t km_methods[] = {
58 	DEVMETHOD(device_identify,	km_identify),
59 	DEVMETHOD(device_probe,		km_probe),
60 	DEVMETHOD(device_attach,	km_attach),
61 	DEVMETHOD(device_detach,	km_detach),
62 	{ NULL, NULL }
63 };
64 
65 static driver_t km_driver = {
66 	"km",
67 	km_methods,
68 	sizeof(struct km_softc)
69 };
70 
71 static devclass_t km_devclass;
72 
73 DRIVER_MODULE(km, hostb, km_driver, km_devclass, NULL, NULL);
74 
75 
76 static void
77 km_identify(driver_t *driver, struct device *parent)
78 {
79 	if (km_probe(parent) == ENXIO)
80 		return;
81 	if (device_find_child(parent, driver->name, -1) != NULL)
82 		return;
83 	device_add_child(parent, driver->name, -1);
84 }
85 
86 static int
87 km_probe(struct device *dev)
88 {
89 	int ten = 0;
90 
91 	if (pci_get_vendor(dev) != PCI_VENDOR_AMD)
92 		return ENXIO;
93 
94 	switch (pci_get_device(dev)) {
95 	case PCI_PRODUCT_AMD_AMD64_F10_MISC:
96 		ten = 1;
97 		/* FALLTHROUGH */
98 	case PCI_PRODUCT_AMD_AMD64_F11_MISC:
99 		if (device_get_desc(dev) == NULL)
100 			device_set_desc(dev, ten ?
101 			    "AMD Family 10h temperature sensor" :
102 			    "AMD Family 11h temperature sensor");
103 		return 0;
104 	default:
105 		return ENXIO;
106 	}
107 }
108 
109 static int
110 km_attach(struct device *dev)
111 {
112 	struct km_softc	*sc;
113 
114 	sc = device_get_softc(dev);
115 	sc->sc_dev = dev;
116 
117 	strlcpy(sc->sc_sensordev.xname, device_get_nameunit(dev),
118 	    sizeof(sc->sc_sensordev.xname));
119 
120 	sc->sc_sensor.type = SENSOR_TEMP;
121 	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
122 
123 	if (sensor_task_register(sc, km_refresh, 5)) {
124 		device_printf(dev, "unable to register update task\n");
125 		return ENXIO;
126 	}
127 
128 	sensordev_install(&sc->sc_sensordev);
129 	return 0;
130 }
131 
132 static int
133 km_detach(struct device *dev)
134 {
135 	struct km_softc	*sc = device_get_softc(dev);
136 
137 	sensordev_deinstall(&sc->sc_sensordev);
138 	sensor_task_unregister(sc);
139 	return 0;
140 }
141 
142 static void
143 km_refresh(void *arg)
144 {
145 	struct km_softc	*sc = arg;
146 	struct ksensor	*s = &sc->sc_sensor;
147 	uint32_t	r;
148 	int		c;
149 
150 	r = pci_read_config(sc->sc_dev, KM_REP_TEMP_CONTR_R, 4);
151 	c = KM_GET_CURTMP(r);
152 	s->value = c * 125000 + 273150000;
153 }
154