summaryrefslogtreecommitdiff
path: root/sys/dev/pci/km.c
blob: 48589ff4cf6ec642b7bb42fb3d8829ecdf6ccd4f (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
/*	$OpenBSD: km.c,v 1.4 2009/01/26 15:07:49 kettenis Exp $	*/

/*
 * Copyright (c) 2008 Constantine A. Murenin <cnst+openbsd@bugmail.mojo.ru>
 *
 * 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.
 */

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

#include <machine/bus.h>

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


/*
 * AMD Family 10h/11h Processors, Function 3 -- Miscellaneous Control
 */

/* Function 3 Registers */
#define KM_REP_TEMP_CONTR_R	0xa4
#define KM_THERMTRIP_STAT_R	0xe4
#define KM_NORTHBRIDGE_CAP_R	0xe8
#define KM_CPUID_FAMILY_MODEL_R	0xfc

/* Operations on Reported Temperature Control Register */
#define KM_GET_CURTMP(r)	(((r) >> 21) & 0x7ff)

/* Operations on Thermtrip Status Register */
#define KM_GET_DIODEOFFSET(r)	(((r) >> 8) & 0x7f)


struct km_softc {
	struct device		sc_dev;

	pci_chipset_tag_t	sc_pc;
	pcitag_t		sc_pcitag;

	struct ksensor		sc_sensor;
	struct ksensordev	sc_sensordev;
};

int	km_match(struct device *, void *, void *);
void	km_attach(struct device *, struct device *, void *);
void	km_refresh(void *);

struct cfattach km_ca = {
	sizeof(struct km_softc), km_match, km_attach
};

struct cfdriver km_cd = {
	NULL, "km", DV_DULL
};

static const struct pci_matchid km_devices[] = {
	{ PCI_VENDOR_AMD, PCI_PRODUCT_AMD_AMD64_10_MISC },
	{ PCI_VENDOR_AMD, PCI_PRODUCT_AMD_AMD64_11_MISC }
};


int
km_match(struct device *parent, void *match, void *aux)
{
	/* successful match supersedes pchb(4) */
	return pci_matchbyid((struct pci_attach_args *)aux, km_devices,
	    sizeof(km_devices) / sizeof(km_devices[0])) * 2;
}

void
km_attach(struct device *parent, struct device *self, void *aux)
{
	struct km_softc		*sc = (struct km_softc *)self;
	struct pci_attach_args	*pa = aux;

	sc->sc_pc = pa->pa_pc;
	sc->sc_pcitag = pa->pa_tag;

	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, km_refresh, 5) == NULL) {
		printf(": unable to register update task\n");
		return;
	}

	sensordev_install(&sc->sc_sensordev);

	printf("\n");
}

void
km_refresh(void *arg)
{
	struct km_softc	*sc = arg;
	struct ksensor	*s = &sc->sc_sensor;
	pcireg_t	r;
	int		c;

	r = pci_conf_read(sc->sc_pc, sc->sc_pcitag, KM_REP_TEMP_CONTR_R);
	c = KM_GET_CURTMP(r);
	s->value = c * 125000 + 273150000;
}