xref: /dflybsd-src/sys/dev/powermng/km/km.c (revision 3d3afab1ee14d167f82ed6f2513d0db43a562755)
1*3d3afab1SImre Vadasz /*	$OpenBSD: km.c,v 1.8 2013/09/17 13:42:34 kettenis Exp $	*/
239990074SConstantine A. Murenin 
339990074SConstantine A. Murenin /*
4*3d3afab1SImre Vadasz  * Copyright (c) 2008/2010 Constantine A. Murenin <cnst+openbsd@bugmail.mojo.ru>
539990074SConstantine A. Murenin  *
639990074SConstantine A. Murenin  * Permission to use, copy, modify, and distribute this software for any
739990074SConstantine A. Murenin  * purpose with or without fee is hereby granted, provided that the above
839990074SConstantine A. Murenin  * copyright notice and this permission notice appear in all copies.
939990074SConstantine A. Murenin  *
1039990074SConstantine A. Murenin  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1139990074SConstantine A. Murenin  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1239990074SConstantine A. Murenin  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1339990074SConstantine A. Murenin  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1439990074SConstantine A. Murenin  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1539990074SConstantine A. Murenin  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1639990074SConstantine A. Murenin  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1739990074SConstantine A. Murenin  */
1839990074SConstantine A. Murenin 
1939990074SConstantine A. Murenin #include <sys/param.h>
2039990074SConstantine A. Murenin #include <sys/systm.h>
2139990074SConstantine A. Murenin #include <sys/bus.h>
2239990074SConstantine A. Murenin #include <sys/sensors.h>
2339990074SConstantine A. Murenin 
2439990074SConstantine A. Murenin #include <bus/pci/pcivar.h>
25dcb4b80dSSascha Wildner #include "pcidevs.h"
2639990074SConstantine A. Murenin 
2739990074SConstantine A. Murenin 
2839990074SConstantine A. Murenin /*
29*3d3afab1SImre Vadasz  * AMD Family 10h/11h/14h/15h Processors, Function 3 -- Miscellaneous Control
3039990074SConstantine A. Murenin  */
3139990074SConstantine A. Murenin 
3239990074SConstantine A. Murenin /* Function 3 Registers */
3339990074SConstantine A. Murenin #define KM_REP_TEMP_CONTR_R	0xa4
3439990074SConstantine A. Murenin #define KM_THERMTRIP_STAT_R	0xe4
3539990074SConstantine A. Murenin #define KM_NORTHBRIDGE_CAP_R	0xe8
3639990074SConstantine A. Murenin #define KM_CPUID_FAMILY_MODEL_R	0xfc
3739990074SConstantine A. Murenin 
3839990074SConstantine A. Murenin /* Operations on Reported Temperature Control Register */
3939990074SConstantine A. Murenin #define KM_GET_CURTMP(r)	(((r) >> 21) & 0x7ff)
4039990074SConstantine A. Murenin 
4139990074SConstantine A. Murenin /* Operations on Thermtrip Status Register */
4239990074SConstantine A. Murenin #define KM_GET_DIODEOFFSET(r)	(((r) >> 8) & 0x7f)
4339990074SConstantine A. Murenin 
4439990074SConstantine A. Murenin 
4539990074SConstantine A. Murenin struct km_softc {
4639990074SConstantine A. Murenin 	struct device		*sc_dev;
4739990074SConstantine A. Murenin 	struct ksensor		sc_sensor;
4839990074SConstantine A. Murenin 	struct ksensordev	sc_sensordev;
4939990074SConstantine A. Murenin };
5039990074SConstantine A. Murenin 
5139990074SConstantine A. Murenin static void	km_identify(driver_t *, struct device *);
5239990074SConstantine A. Murenin static int	km_probe(struct device *);
5339990074SConstantine A. Murenin static int	km_attach(struct device *);
5439990074SConstantine A. Murenin static int	km_detach(struct device *);
5539990074SConstantine A. Murenin static void	km_refresh(void *);
5639990074SConstantine A. Murenin 
5739990074SConstantine A. Murenin static device_method_t km_methods[] = {
5839990074SConstantine A. Murenin 	DEVMETHOD(device_identify,	km_identify),
5939990074SConstantine A. Murenin 	DEVMETHOD(device_probe,		km_probe),
6039990074SConstantine A. Murenin 	DEVMETHOD(device_attach,	km_attach),
6139990074SConstantine A. Murenin 	DEVMETHOD(device_detach,	km_detach),
6239990074SConstantine A. Murenin 	{ NULL, NULL }
6339990074SConstantine A. Murenin };
6439990074SConstantine A. Murenin 
6539990074SConstantine A. Murenin static driver_t km_driver = {
6639990074SConstantine A. Murenin 	"km",
6739990074SConstantine A. Murenin 	km_methods,
6839990074SConstantine A. Murenin 	sizeof(struct km_softc)
6939990074SConstantine A. Murenin };
7039990074SConstantine A. Murenin 
7139990074SConstantine A. Murenin static devclass_t km_devclass;
7239990074SConstantine A. Murenin 
7339990074SConstantine A. Murenin DRIVER_MODULE(km, hostb, km_driver, km_devclass, NULL, NULL);
7439990074SConstantine A. Murenin 
7539990074SConstantine A. Murenin 
7639990074SConstantine A. Murenin static void
7739990074SConstantine A. Murenin km_identify(driver_t *driver, struct device *parent)
7839990074SConstantine A. Murenin {
7939990074SConstantine A. Murenin 	if (km_probe(parent) == ENXIO)
8039990074SConstantine A. Murenin 		return;
8139990074SConstantine A. Murenin 	if (device_find_child(parent, driver->name, -1) != NULL)
8239990074SConstantine A. Murenin 		return;
8339990074SConstantine A. Murenin 	device_add_child(parent, driver->name, -1);
8439990074SConstantine A. Murenin }
8539990074SConstantine A. Murenin 
8639990074SConstantine A. Murenin static int
8739990074SConstantine A. Murenin km_probe(struct device *dev)
8839990074SConstantine A. Murenin {
89*3d3afab1SImre Vadasz 	char *desc;
9039990074SConstantine A. Murenin 
9139990074SConstantine A. Murenin 	if (pci_get_vendor(dev) != PCI_VENDOR_AMD)
9239990074SConstantine A. Murenin 		return ENXIO;
9339990074SConstantine A. Murenin 
9439990074SConstantine A. Murenin 	switch (pci_get_device(dev)) {
9539990074SConstantine A. Murenin 	case PCI_PRODUCT_AMD_AMD64_F10_MISC:
96*3d3afab1SImre Vadasz 		desc = "AMD Family 10h temperature sensor";
97*3d3afab1SImre Vadasz 		break;
9839990074SConstantine A. Murenin 	case PCI_PRODUCT_AMD_AMD64_F11_MISC:
99*3d3afab1SImre Vadasz 		desc = "AMD Family 11h temperature sensor";
100*3d3afab1SImre Vadasz 		break;
101*3d3afab1SImre Vadasz 	case PCI_PRODUCT_AMD_AMD64_F14_MISC:
102*3d3afab1SImre Vadasz 		desc = "AMD Family 14h temperature sensor";
103*3d3afab1SImre Vadasz 		break;
104*3d3afab1SImre Vadasz 	case PCI_PRODUCT_AMD_AMD64_F15_0x_MISC:
105*3d3afab1SImre Vadasz 		desc = "AMD Family 15/0xh temperature sensor";
106*3d3afab1SImre Vadasz 		break;
107*3d3afab1SImre Vadasz 	case PCI_PRODUCT_AMD_AMD64_F15_1x_MISC:
108*3d3afab1SImre Vadasz 		desc = "AMD Family 15/1xh temperature sensor";
109*3d3afab1SImre Vadasz 		break;
11039990074SConstantine A. Murenin 	default:
11139990074SConstantine A. Murenin 		return ENXIO;
11239990074SConstantine A. Murenin 	}
113*3d3afab1SImre Vadasz 
114*3d3afab1SImre Vadasz 	if (device_get_desc(dev) == NULL)
115*3d3afab1SImre Vadasz 		device_set_desc(dev, desc);
116*3d3afab1SImre Vadasz 	return 0;
11739990074SConstantine A. Murenin }
11839990074SConstantine A. Murenin 
11939990074SConstantine A. Murenin static int
12039990074SConstantine A. Murenin km_attach(struct device *dev)
12139990074SConstantine A. Murenin {
12239990074SConstantine A. Murenin 	struct km_softc	*sc;
12339990074SConstantine A. Murenin 
12439990074SConstantine A. Murenin 	sc = device_get_softc(dev);
12539990074SConstantine A. Murenin 	sc->sc_dev = dev;
12639990074SConstantine A. Murenin 
12739990074SConstantine A. Murenin 	strlcpy(sc->sc_sensordev.xname, device_get_nameunit(dev),
12839990074SConstantine A. Murenin 	    sizeof(sc->sc_sensordev.xname));
12939990074SConstantine A. Murenin 
13039990074SConstantine A. Murenin 	sc->sc_sensor.type = SENSOR_TEMP;
13139990074SConstantine A. Murenin 	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
13239990074SConstantine A. Murenin 
13339990074SConstantine A. Murenin 	if (sensor_task_register(sc, km_refresh, 5)) {
13439990074SConstantine A. Murenin 		device_printf(dev, "unable to register update task\n");
13539990074SConstantine A. Murenin 		return ENXIO;
13639990074SConstantine A. Murenin 	}
13739990074SConstantine A. Murenin 
13839990074SConstantine A. Murenin 	sensordev_install(&sc->sc_sensordev);
13939990074SConstantine A. Murenin 	return 0;
14039990074SConstantine A. Murenin }
14139990074SConstantine A. Murenin 
14239990074SConstantine A. Murenin static int
14339990074SConstantine A. Murenin km_detach(struct device *dev)
14439990074SConstantine A. Murenin {
14539990074SConstantine A. Murenin 	struct km_softc	*sc = device_get_softc(dev);
14639990074SConstantine A. Murenin 
14739990074SConstantine A. Murenin 	sensordev_deinstall(&sc->sc_sensordev);
14839990074SConstantine A. Murenin 	sensor_task_unregister(sc);
14939990074SConstantine A. Murenin 	return 0;
15039990074SConstantine A. Murenin }
15139990074SConstantine A. Murenin 
15239990074SConstantine A. Murenin static void
15339990074SConstantine A. Murenin km_refresh(void *arg)
15439990074SConstantine A. Murenin {
15539990074SConstantine A. Murenin 	struct km_softc	*sc = arg;
15639990074SConstantine A. Murenin 	struct ksensor	*s = &sc->sc_sensor;
15739990074SConstantine A. Murenin 	uint32_t	r;
15839990074SConstantine A. Murenin 	int		c;
15939990074SConstantine A. Murenin 
16039990074SConstantine A. Murenin 	r = pci_read_config(sc->sc_dev, KM_REP_TEMP_CONTR_R, 4);
16139990074SConstantine A. Murenin 	c = KM_GET_CURTMP(r);
16239990074SConstantine A. Murenin 	s->value = c * 125000 + 273150000;
16339990074SConstantine A. Murenin }
164