xref: /freebsd/sys/arm/mv/mv_thermal.c (revision 3231251399f9e60fc7175b6352eb5291e9296fe8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018 Rubicon Communications, LLC (Netgate)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/rman.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/sysctl.h>
43 
44 #include <machine/bus.h>
45 #include <machine/resource.h>
46 #include <machine/intr.h>
47 
48 #include <dev/fdt/simplebus.h>
49 
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
52 
53 #define	CONTROL0		0x00
54 #define	 CONTROL0_TSEN_START	(1 << 0)
55 #define	 CONTROL0_TSEN_RESET	(1 << 1)
56 #define	 CONTROL0_TSEN_EN	(1 << 2)
57 #define	 CONTROL0_CHANNEL_SHIFT	13
58 #define	 CONTROL0_CHANNEL_MASK	0xF
59 #define	 CONTROL0_OSR_SHIFT	24
60 #define	 CONTROL0_OSR_MAX	3	/* OSR = 512 * 4uS = ~2mS */
61 #define	 CONTROL0_MODE_SHIFT	30
62 #define	 CONTROL0_MODE_EXTERNAL	0x2
63 #define	 CONTROL0_MODE_MASK	0x3
64 
65 #define	CONTROL1	0x04
66 /* This doesn't seems to work */
67 #define	CONTROL1_TSEN_SENS_SHIFT	21
68 #define	CONTROL1_TSEN_SENS_MASK		0x7
69 
70 #define	STATUS			0x00
71 #define	STATUS_TEMP_MASK	0x3FF
72 
73 enum mv_thermal_type {
74 	MV_AP806 = 1,
75 	MV_CP110,
76 };
77 
78 struct mv_thermal_config {
79 	enum mv_thermal_type	type;
80 	int			ncpus;
81 	int64_t			calib_mul;
82 	int64_t			calib_add;
83 	int64_t			calib_div;
84 	uint32_t		valid_mask;
85 	bool			signed_value;
86 };
87 
88 struct mv_thermal_softc {
89 	device_t		dev;
90 	struct resource		*res[2];
91 	struct mtx		mtx;
92 
93 	struct mv_thermal_config	*config;
94 	int				cur_sensor;
95 };
96 
97 static struct mv_thermal_config mv_ap806_config = {
98 	.type = MV_AP806,
99 	.ncpus = 4,
100 	.calib_mul = 423,
101 	.calib_add = -150000,
102 	.calib_div = 100,
103 	.valid_mask = (1 << 16),
104 	.signed_value = true,
105 };
106 
107 static struct mv_thermal_config mv_cp110_config = {
108 	.type = MV_CP110,
109 	.calib_mul = 2000096,
110 	.calib_add = 1172499100,
111 	.calib_div = 420100,
112 	.valid_mask = (1 << 10),
113 	.signed_value = false,
114 };
115 
116 static struct resource_spec mv_thermal_res_spec[] = {
117 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
118 	{ SYS_RES_MEMORY,	1,	RF_ACTIVE },
119 	{ -1, 0 }
120 };
121 
122 static struct ofw_compat_data compat_data[] = {
123 	{"marvell,armada-ap806-thermal", (uintptr_t) &mv_ap806_config},
124 	{"marvell,armada-cp110-thermal", (uintptr_t) &mv_cp110_config},
125 	{NULL,             0}
126 };
127 
128 #define	RD_STA(sc, reg)		bus_read_4((sc)->res[0], (reg))
129 #define	WR_STA(sc, reg, val)	bus_write_4((sc)->res[0], (reg), (val))
130 #define	RD_CON(sc, reg)		bus_read_4((sc)->res[1], (reg))
131 #define	WR_CON(sc, reg, val)	bus_write_4((sc)->res[1], (reg), (val))
132 
133 static inline int32_t sign_extend(uint32_t value, int index)
134 {
135 	uint8_t shift;
136 
137 	shift = 31 - index;
138 	return ((int32_t)(value << shift) >> shift);
139 }
140 
141 static int
142 mv_thermal_wait_sensor(struct mv_thermal_softc *sc)
143 {
144 	uint32_t reg;
145 	uint32_t timeout;
146 
147 	timeout = 100000;
148 	while (--timeout > 0) {
149 		reg = RD_STA(sc, STATUS);
150 		if ((reg & sc->config->valid_mask) == sc->config->valid_mask)
151 			break;
152 		DELAY(100);
153 	}
154 	if (timeout == 0) {
155 		return (ETIMEDOUT);
156 	}
157 
158 	return (0);
159 }
160 
161 static int
162 mv_thermal_select_sensor(struct mv_thermal_softc *sc, int sensor)
163 {
164 	uint32_t reg;
165 
166 	if (sc->cur_sensor == sensor)
167 		return (0);
168 
169 	/* Stop the current reading and reset the module */
170 	reg = RD_CON(sc, CONTROL0);
171 	reg &= ~(CONTROL0_TSEN_START | CONTROL0_TSEN_EN);
172 	WR_CON(sc, CONTROL0, reg);
173 
174 	/* Switch to the selected sensor */
175 	/*
176 	 * NOTE : Datasheet says to use CONTROL1 for selecting
177 	 * but when doing so the sensors >0 are never ready
178 	 * Do what Linux does using undocumented bits in CONTROL0
179 	 */
180 	/* This reset automatically to the sensor 0 */
181 	reg &= ~(CONTROL0_MODE_MASK << CONTROL0_MODE_SHIFT);
182 	if (sensor) {
183 		/* Select external sensor */
184 		reg |= CONTROL0_MODE_EXTERNAL << CONTROL0_MODE_SHIFT;
185 		reg &= ~(CONTROL0_CHANNEL_MASK << CONTROL0_CHANNEL_SHIFT);
186 		reg |= (sensor - 1) << CONTROL0_CHANNEL_SHIFT;
187 	}
188 	WR_CON(sc, CONTROL0, reg);
189 	sc->cur_sensor = sensor;
190 
191 	/* Start the reading */
192 	reg = RD_CON(sc, CONTROL0);
193 	reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_EN;
194 	WR_CON(sc, CONTROL0, reg);
195 
196 	return (mv_thermal_wait_sensor(sc));
197 }
198 
199 static int
200 mv_thermal_read_sensor(struct mv_thermal_softc *sc, int sensor, int *temp)
201 {
202 	uint32_t reg;
203 	int64_t sample, rv;
204 
205 	rv = mv_thermal_select_sensor(sc, sensor);
206 	if (rv != 0)
207 		return (rv);
208 
209 	reg = RD_STA(sc, STATUS) & STATUS_TEMP_MASK;
210 
211 	if (sc->config->signed_value)
212 		sample = sign_extend(reg, fls(STATUS_TEMP_MASK) - 1);
213 	else
214 		sample = reg;
215 
216 	*temp = ((sample * sc->config->calib_mul) - sc->config->calib_add) /
217 		sc->config->calib_div;
218 
219 	return (0);
220 }
221 
222 static int
223 ap806_init(struct mv_thermal_softc *sc)
224 {
225 	uint32_t reg;
226 
227 	/* Start the temp capture/conversion */
228 	reg = RD_CON(sc, CONTROL0);
229 	reg &= ~CONTROL0_TSEN_RESET;
230 	reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_EN;
231 
232 	/* Sample every ~2ms */
233 	reg |= CONTROL0_OSR_MAX << CONTROL0_OSR_SHIFT;
234 
235 	WR_CON(sc, CONTROL0, reg);
236 
237 	/* Since we just started the module wait for the sensor to be ready */
238 	mv_thermal_wait_sensor(sc);
239 
240 	return (0);
241 }
242 
243 static int
244 cp110_init(struct mv_thermal_softc *sc)
245 {
246 	uint32_t reg;
247 
248 	reg = RD_CON(sc, CONTROL1);
249 	reg &= (1 << 7);
250 	reg |= (1 << 8);
251 	WR_CON(sc, CONTROL1, reg);
252 
253 	/* Sample every ~2ms */
254 	reg = RD_CON(sc, CONTROL0);
255 	reg |= CONTROL0_OSR_MAX << CONTROL0_OSR_SHIFT;
256 	WR_CON(sc, CONTROL0, reg);
257 
258 	return (0);
259 }
260 
261 static int
262 mv_thermal_sysctl(SYSCTL_HANDLER_ARGS)
263 {
264 	struct mv_thermal_softc *sc;
265 	device_t dev = arg1;
266 	int sensor = arg2;
267 	int val = 0;
268 
269 	sc = device_get_softc(dev);
270 	mtx_lock(&(sc)->mtx);
271 
272 	if (mv_thermal_read_sensor(sc, sensor, &val) == 0) {
273 		/* Convert to Kelvin */
274 		val = val + 2732;
275 	} else {
276 		device_printf(dev, "Timeout waiting for sensor\n");
277 	}
278 
279 	mtx_unlock(&(sc)->mtx);
280 	return sysctl_handle_opaque(oidp, &val, sizeof(val), req);
281 }
282 
283 static int
284 mv_thermal_probe(device_t dev)
285 {
286 
287 	if (!ofw_bus_status_okay(dev))
288 		return (ENXIO);
289 
290 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
291 		return (ENXIO);
292 
293 	device_set_desc(dev, "Marvell Thermal Sensor Controller");
294 	return (BUS_PROBE_DEFAULT);
295 }
296 
297 static int
298 mv_thermal_attach(device_t dev)
299 {
300 	struct mv_thermal_softc *sc;
301 	struct sysctl_ctx_list *ctx;
302 	struct sysctl_oid_list *oid;
303 	char name[255];
304 	char desc[255];
305 	int i;
306 
307 	sc = device_get_softc(dev);
308 	sc->dev = dev;
309 
310 	sc->config = (struct mv_thermal_config *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
311 
312 	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
313 
314 	if (bus_alloc_resources(dev, mv_thermal_res_spec, sc->res) != 0) {
315 		device_printf(dev, "cannot allocate resources for device\n");
316 		return (ENXIO);
317 	}
318 
319 	sc->cur_sensor = -1;
320 	switch (sc->config->type) {
321 	case MV_AP806:
322 		ap806_init(sc);
323 		break;
324 	case MV_CP110:
325 		cp110_init(sc);
326 		break;
327 	}
328 
329 	ctx = device_get_sysctl_ctx(dev);
330 	oid = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
331 	/* There is always at least one sensor */
332 	SYSCTL_ADD_PROC(ctx, oid, OID_AUTO, "internal",
333 	    CTLTYPE_INT | CTLFLAG_RD,
334 	    dev, 0, mv_thermal_sysctl,
335 	    "IK",
336 	    "Internal Temperature");
337 
338 	for (i = 0; i < sc->config->ncpus; i++) {
339 		snprintf(name, sizeof(name), "cpu%d", i);
340 		snprintf(desc, sizeof(desc), "CPU%d Temperature", i);
341 		SYSCTL_ADD_PROC(ctx, oid, OID_AUTO, name,
342 		    CTLTYPE_INT | CTLFLAG_RD,
343 		    dev, i + 1, mv_thermal_sysctl,
344 		    "IK",
345 		    desc);
346 	}
347 
348 	return (0);
349 }
350 
351 static int
352 mv_thermal_detach(device_t dev)
353 {
354 	struct mv_thermal_softc *sc;
355 
356 	sc = device_get_softc(dev);
357 
358 	bus_release_resources(dev, mv_thermal_res_spec, sc->res);
359 
360 	return (0);
361 }
362 
363 static device_method_t mv_thermal_methods[] = {
364 	/* Device interface */
365 	DEVMETHOD(device_probe,		mv_thermal_probe),
366 	DEVMETHOD(device_attach,	mv_thermal_attach),
367 	DEVMETHOD(device_detach,	mv_thermal_detach),
368 
369 	DEVMETHOD_END
370 };
371 
372 static devclass_t mv_thermal_devclass;
373 
374 static driver_t mv_thermal_driver = {
375 	"mv_thermal",
376 	mv_thermal_methods,
377 	sizeof(struct mv_thermal_softc),
378 };
379 
380 DRIVER_MODULE(mv_thermal, simplebus, mv_thermal_driver,
381     mv_thermal_devclass, 0, 0);
382