xref: /dragonfly/sys/dev/powermng/km/km.c (revision 61c0377f)
1 /*	$OpenBSD: km.c,v 1.8 2013/09/17 13:42:34 kettenis 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 "pcidevs.h"
26 
27 
28 /*
29  * AMD Family 10h/11h/14h/15h 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 	char *desc;
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 		desc = "AMD Family 10h temperature sensor";
97 		break;
98 	case PCI_PRODUCT_AMD_AMD64_F11_MISC:
99 		desc = "AMD Family 11h temperature sensor";
100 		break;
101 	case PCI_PRODUCT_AMD_AMD64_F14_MISC:
102 		desc = "AMD Family 14h temperature sensor";
103 		break;
104 	case PCI_PRODUCT_AMD_AMD64_F15_0x_MISC:
105 		desc = "AMD Family 15/0xh temperature sensor";
106 		break;
107 	case PCI_PRODUCT_AMD_AMD64_F15_1x_MISC:
108 		desc = "AMD Family 15/1xh temperature sensor";
109 		break;
110 	default:
111 		return ENXIO;
112 	}
113 
114 	if (device_get_desc(dev) == NULL)
115 		device_set_desc(dev, desc);
116 	return 0;
117 }
118 
119 static int
120 km_attach(struct device *dev)
121 {
122 	struct km_softc	*sc;
123 
124 	sc = device_get_softc(dev);
125 	sc->sc_dev = dev;
126 
127 	strlcpy(sc->sc_sensordev.xname, device_get_nameunit(dev),
128 	    sizeof(sc->sc_sensordev.xname));
129 
130 	sc->sc_sensor.type = SENSOR_TEMP;
131 	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
132 
133 	if (sensor_task_register(sc, km_refresh, 5)) {
134 		device_printf(dev, "unable to register update task\n");
135 		return ENXIO;
136 	}
137 
138 	sensordev_install(&sc->sc_sensordev);
139 	return 0;
140 }
141 
142 static int
143 km_detach(struct device *dev)
144 {
145 	struct km_softc	*sc = device_get_softc(dev);
146 
147 	sensordev_deinstall(&sc->sc_sensordev);
148 	sensor_task_unregister(sc);
149 	return 0;
150 }
151 
152 static void
153 km_refresh(void *arg)
154 {
155 	struct km_softc	*sc = arg;
156 	struct ksensor	*s = &sc->sc_sensor;
157 	uint32_t	r;
158 	int		c;
159 
160 	r = pci_read_config(sc->sc_dev, KM_REP_TEMP_CONTR_R, 4);
161 	c = KM_GET_CURTMP(r);
162 	s->value = c * 125000 + 273150000;
163 }
164