aboutsummaryrefslogtreecommitdiff
path: root/sys/arm/mv/mv_ts.c
blob: 7606859b2504b48d2f0f03a940ee2a37b660cb69 (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*-
 * Copyright (c) 2012 Hiroki Sato <hrs@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Driver for on-die thermal sensor in 88F6282 and 88F6283.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/sysctl.h>

#include <dev/fdt/fdt_common.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

#include <arm/mv/mvreg.h>
#include <arm/mv/mvvar.h>

static struct resource_spec mvts_res[] = {
	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
	{ -1, 0 }
};

struct mvts_softc {
	device_t		sc_dev;
	struct resource		*sc_res[sizeof(mvts_res)];
};

static int
ts_probe(device_t dev)
{
	uint32_t d, r;

	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	if (!ofw_bus_is_compatible(dev, "mrvl,ts"))
		return (ENXIO);
	soc_id(&d, &r);
	switch (d) {
	case MV_DEV_88F6282:
		break;
	default:
		device_printf(dev, "unsupported SoC (ID: 0x%08X)!\n", d);
		return (ENXIO);
	}
	device_set_desc(dev, "Marvell Thermal Sensor");

	return (0);
}

#define	MV_TEMP_VALID_BIT	(1 << 9)
#define	MV_TEMP_SENS_OFFS	10
#define	MV_TEMP_SENS_MASK	0x1ff
#define	MV_TEMP_SENS_READ_MAX	16
#define	TZ_ZEROC		2732
#define	MV_TEMP_CONVERT(x)	((((322 - x) * 100000) / 13625) + TZ_ZEROC)

/*
 * MSB                                 LSB
 * 0000 0000 0000 0000 0000 0000 0000 0000
 *                             ^- valid bit
 *                  |---------|
 *                         ^--- temperature (9 bits)
 */

static int
ts_sysctl_handler(SYSCTL_HANDLER_ARGS)
{
	struct mvts_softc *sc;
	device_t dev;
	uint32_t ret, ret0;
	u_int val;
	int i;

	dev = (device_t)arg1;
	sc = device_get_softc(dev);
	val = TZ_ZEROC;

	ret = bus_read_4(sc->sc_res[0], 0);
	if ((ret & MV_TEMP_VALID_BIT) == 0) {
		device_printf(dev, "temperature sensor is broken.\n");
		goto ts_sysctl_handle_int;
	}
	ret0 = 0;
	for (i = 0; i < MV_TEMP_SENS_READ_MAX; i++) {
		ret = bus_read_4(sc->sc_res[0], 0);
		ret = (ret >> MV_TEMP_SENS_OFFS) & MV_TEMP_SENS_MASK;

		/*
		 * Successive reads should returns the same value except
		 * for the LSB when the sensor is normal.
		 */
		if (((ret0 ^ ret) & 0x1fe) == 0)
			break;
		else
			ret0 = ret;
	}
	if (i == MV_TEMP_SENS_READ_MAX) {
		device_printf(dev, "temperature sensor is unstable.\n");
		goto ts_sysctl_handle_int;
	}
	val = (u_int)MV_TEMP_CONVERT(ret);

ts_sysctl_handle_int:
	return (sysctl_handle_int(oidp, &val, 0, req));
}

static int
ts_attach(device_t dev)
{
	struct mvts_softc *sc;
	struct sysctl_ctx_list *ctx;
	int error;

	sc = device_get_softc(dev);
	sc->sc_dev = dev;
	error = bus_alloc_resources(dev, mvts_res, sc->sc_res);
	if (error) {
		device_printf(dev, "could not allocate resources\n");
		return (ENXIO);
	}
	ctx = device_get_sysctl_ctx(dev);
	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
	    OID_AUTO, "temperature", CTLTYPE_INT | CTLFLAG_RD, dev,
	    0, ts_sysctl_handler, "IK", "Current Temperature");

	return (0);
}

static int
ts_detach(device_t dev)
{

	return (0);
}

static device_method_t ts_methods[] = {
	DEVMETHOD(device_probe,		ts_probe),
	DEVMETHOD(device_attach,	ts_attach),
	DEVMETHOD(device_detach,	ts_detach),
	{0, 0},
};

static driver_t ts_driver = {
	"mvts",
	ts_methods,
	sizeof(struct mvts_softc),
};

static devclass_t ts_devclass;
DRIVER_MODULE(mvts, simplebus, ts_driver, ts_devclass, 0, 0);
MODULE_VERSION(mvts, 1);