xref: /freebsd/sys/arm/mv/mv_thermal.c (revision 62e8ccc3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/bus.h>
31 
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/rman.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/sysctl.h>
38 
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <machine/intr.h>
42 #include <dev/syscon/syscon.h>
43 
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 
47 #include "syscon_if.h"
48 
49 #define	CONTROL0		0	/* Offset in config->regs[] array */
50 #define	 CONTROL0_TSEN_START	(1 << 0)
51 #define	 CONTROL0_TSEN_RESET	(1 << 1)
52 #define	 CONTROL0_TSEN_EN	(1 << 2)
53 #define	 CONTROL0_CHANNEL_SHIFT	13
54 #define	 CONTROL0_CHANNEL_MASK	0xF
55 #define	 CONTROL0_OSR_SHIFT	24
56 #define	 CONTROL0_OSR_MAX	3	/* OSR = 512 * 4uS = ~2mS */
57 #define	 CONTROL0_MODE_SHIFT	30
58 #define	 CONTROL0_MODE_EXTERNAL	0x2
59 #define	 CONTROL0_MODE_MASK	0x3
60 
61 #define	CONTROL1		1	/* Offset in config->regs[] array */
62 /* This doesn't seems to work */
63 #define	CONTROL1_TSEN_SENS_SHIFT	21
64 #define	CONTROL1_TSEN_SENS_MASK		0x7
65 
66 #define	STATUS			2	/* Offset in config->regs[] array */
67 #define	STATUS_TEMP_MASK	0x3FF
68 
69 enum mv_thermal_type {
70 	MV_AP806 = 1,
71 	MV_CP110,
72 };
73 
74 struct mv_thermal_config {
75 	enum mv_thermal_type	type;
76 	int			regs[3];
77 	int			ncpus;
78 	int64_t			calib_mul;
79 	int64_t			calib_add;
80 	int64_t			calib_div;
81 	uint32_t		valid_mask;
82 	bool			signed_value;
83 };
84 
85 struct mv_thermal_softc {
86 	device_t		dev;
87 	struct syscon		*syscon;
88 	struct mtx		mtx;
89 
90 	struct mv_thermal_config *config;
91 	int			cur_sensor;
92 };
93 
94 static struct mv_thermal_config mv_ap806_config = {
95 	.type = MV_AP806,
96 	.regs = {0x84, 0x88, 0x8C},
97 	.ncpus = 4,
98 	.calib_mul = 423,
99 	.calib_add = -150000,
100 	.calib_div = 100,
101 	.valid_mask = (1 << 16),
102 	.signed_value = true,
103 };
104 
105 static struct mv_thermal_config mv_cp110_config = {
106 	.type = MV_CP110,
107 	.regs = {0x70, 0x74, 0x78},
108 	.calib_mul = 2000096,
109 	.calib_add = 1172499100,
110 	.calib_div = 420100,
111 	.valid_mask = (1 << 10),
112 	.signed_value = false,
113 };
114 
115 static struct ofw_compat_data compat_data[] = {
116 	{"marvell,armada-ap806-thermal", (uintptr_t) &mv_ap806_config},
117 	{"marvell,armada-cp110-thermal", (uintptr_t) &mv_cp110_config},
118 	{NULL,             0}
119 };
120 
121 #define	RD4(sc, reg)							\
122     SYSCON_READ_4((sc)->syscon, sc->config->regs[reg])
123 #define	WR4(sc, reg, val)						\
124 	SYSCON_WRITE_4((sc)->syscon, sc->config->regs[reg], (val))
125 
sign_extend(uint32_t value,int index)126 static inline int32_t sign_extend(uint32_t value, int index)
127 {
128 	uint8_t shift;
129 
130 	shift = 31 - index;
131 	return ((int32_t)(value << shift) >> shift);
132 }
133 
134 static int
mv_thermal_wait_sensor(struct mv_thermal_softc * sc)135 mv_thermal_wait_sensor(struct mv_thermal_softc *sc)
136 {
137 	uint32_t reg;
138 	uint32_t timeout;
139 
140 	timeout = 100000;
141 	while (--timeout > 0) {
142 		reg = RD4(sc, STATUS);
143 		if ((reg & sc->config->valid_mask) == sc->config->valid_mask)
144 			break;
145 		DELAY(100);
146 	}
147 	if (timeout == 0) {
148 		return (ETIMEDOUT);
149 	}
150 
151 	return (0);
152 }
153 
154 static int
mv_thermal_select_sensor(struct mv_thermal_softc * sc,int sensor)155 mv_thermal_select_sensor(struct mv_thermal_softc *sc, int sensor)
156 {
157 	uint32_t reg;
158 
159 	if (sc->cur_sensor == sensor)
160 		return (0);
161 
162 	/* Stop the current reading and reset the module */
163 	reg = RD4(sc, CONTROL0);
164 	reg &= ~(CONTROL0_TSEN_START | CONTROL0_TSEN_EN);
165 	WR4(sc, CONTROL0, reg);
166 
167 	/* Switch to the selected sensor */
168 	/*
169 	 * NOTE : Datasheet says to use CONTROL1 for selecting
170 	 * but when doing so the sensors >0 are never ready
171 	 * Do what Linux does using undocumented bits in CONTROL0
172 	 */
173 	/* This reset automatically to the sensor 0 */
174 	reg &= ~(CONTROL0_MODE_MASK << CONTROL0_MODE_SHIFT);
175 	if (sensor) {
176 		/* Select external sensor */
177 		reg |= CONTROL0_MODE_EXTERNAL << CONTROL0_MODE_SHIFT;
178 		reg &= ~(CONTROL0_CHANNEL_MASK << CONTROL0_CHANNEL_SHIFT);
179 		reg |= (sensor - 1) << CONTROL0_CHANNEL_SHIFT;
180 	}
181 	WR4(sc, CONTROL0, reg);
182 	sc->cur_sensor = sensor;
183 
184 	/* Start the reading */
185 	reg = RD4(sc, CONTROL0);
186 	reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_EN;
187 	WR4(sc, CONTROL0, reg);
188 
189 	return (mv_thermal_wait_sensor(sc));
190 }
191 
192 static int
mv_thermal_read_sensor(struct mv_thermal_softc * sc,int sensor,int * temp)193 mv_thermal_read_sensor(struct mv_thermal_softc *sc, int sensor, int *temp)
194 {
195 	uint32_t reg;
196 	int64_t sample, rv;
197 
198 	rv = mv_thermal_select_sensor(sc, sensor);
199 	if (rv != 0)
200 		return (rv);
201 
202 	reg = RD4(sc, STATUS) & STATUS_TEMP_MASK;
203 
204 	if (sc->config->signed_value)
205 		sample = sign_extend(reg, fls(STATUS_TEMP_MASK) - 1);
206 	else
207 		sample = reg;
208 
209 	*temp = ((sample * sc->config->calib_mul) - sc->config->calib_add) /
210 		sc->config->calib_div;
211 
212 	return (0);
213 }
214 
215 static int
ap806_init(struct mv_thermal_softc * sc)216 ap806_init(struct mv_thermal_softc *sc)
217 {
218 	uint32_t reg;
219 
220 	/* Start the temp capture/conversion */
221 	reg = RD4(sc, CONTROL0);
222 	reg &= ~CONTROL0_TSEN_RESET;
223 	reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_EN;
224 
225 	/* Sample every ~2ms */
226 	reg |= CONTROL0_OSR_MAX << CONTROL0_OSR_SHIFT;
227 
228 	WR4(sc, CONTROL0, reg);
229 
230 	/* Since we just started the module wait for the sensor to be ready */
231 	mv_thermal_wait_sensor(sc);
232 
233 	return (0);
234 }
235 
236 static int
cp110_init(struct mv_thermal_softc * sc)237 cp110_init(struct mv_thermal_softc *sc)
238 {
239 	uint32_t reg;
240 
241 	reg = RD4(sc, CONTROL1);
242 	reg &= (1 << 7);
243 	reg |= (1 << 8);
244 	WR4(sc, CONTROL1, reg);
245 
246 	/* Sample every ~2ms */
247 	reg = RD4(sc, CONTROL0);
248 	reg |= CONTROL0_OSR_MAX << CONTROL0_OSR_SHIFT;
249 	WR4(sc, CONTROL0, reg);
250 
251 	return (0);
252 }
253 
254 static int
mv_thermal_sysctl(SYSCTL_HANDLER_ARGS)255 mv_thermal_sysctl(SYSCTL_HANDLER_ARGS)
256 {
257 	struct mv_thermal_softc *sc;
258 	device_t dev = arg1;
259 	int sensor = arg2;
260 	int val = 0;
261 
262 	sc = device_get_softc(dev);
263 	mtx_lock(&(sc)->mtx);
264 
265 	if (mv_thermal_read_sensor(sc, sensor, &val) == 0) {
266 		/* Convert to Kelvin */
267 		val = val + 2732;
268 	} else {
269 		device_printf(dev, "Timeout waiting for sensor\n");
270 	}
271 
272 	mtx_unlock(&(sc)->mtx);
273 	return sysctl_handle_opaque(oidp, &val, sizeof(val), req);
274 }
275 
276 static int
mv_thermal_probe(device_t dev)277 mv_thermal_probe(device_t dev)
278 {
279 
280 	if (!ofw_bus_status_okay(dev))
281 		return (ENXIO);
282 
283 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
284 		return (ENXIO);
285 
286 	device_set_desc(dev, "Marvell Thermal Sensor Controller");
287 	return (BUS_PROBE_DEFAULT);
288 }
289 
290 static int
mv_thermal_attach(device_t dev)291 mv_thermal_attach(device_t dev)
292 {
293 	struct mv_thermal_softc *sc;
294 	struct sysctl_ctx_list *ctx;
295 	struct sysctl_oid_list *oid;
296 	char name[255];
297 	char desc[255];
298 	int i;
299 
300 	sc = device_get_softc(dev);
301 	sc->dev = dev;
302 
303 	sc->config = (struct mv_thermal_config *)
304 	    ofw_bus_search_compatible(dev, compat_data)->ocd_data;
305 
306 	mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
307 
308 	if (SYSCON_GET_HANDLE(sc->dev, &sc->syscon) != 0 ||
309 	    sc->syscon == NULL) {
310 		device_printf(dev, "cannot get syscon for device\n");
311 		return (ENXIO);
312 	}
313 
314 	sc->cur_sensor = -1;
315 	switch (sc->config->type) {
316 	case MV_AP806:
317 		ap806_init(sc);
318 		break;
319 	case MV_CP110:
320 		cp110_init(sc);
321 		break;
322 	}
323 
324 	ctx = device_get_sysctl_ctx(dev);
325 	oid = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
326 	/* There is always at least one sensor */
327 	SYSCTL_ADD_PROC(ctx, oid, OID_AUTO, "internal",
328 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
329 	    dev, 0, mv_thermal_sysctl,
330 	    "IK",
331 	    "Internal Temperature");
332 
333 	for (i = 0; i < sc->config->ncpus; i++) {
334 		snprintf(name, sizeof(name), "cpu%d", i);
335 		snprintf(desc, sizeof(desc), "CPU%d Temperature", i);
336 		SYSCTL_ADD_PROC(ctx, oid, OID_AUTO, name,
337 		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
338 		    dev, i + 1, mv_thermal_sysctl,
339 		    "IK",
340 		    desc);
341 	}
342 
343 	return (0);
344 }
345 
346 static int
mv_thermal_detach(device_t dev)347 mv_thermal_detach(device_t dev)
348 {
349 	return (0);
350 }
351 
352 static device_method_t mv_thermal_methods[] = {
353 	/* Device interface */
354 	DEVMETHOD(device_probe,		mv_thermal_probe),
355 	DEVMETHOD(device_attach,	mv_thermal_attach),
356 	DEVMETHOD(device_detach,	mv_thermal_detach),
357 
358 	DEVMETHOD_END
359 };
360 
361 static driver_t mv_thermal_driver = {
362 	"mv_thermal",
363 	mv_thermal_methods,
364 	sizeof(struct mv_thermal_softc),
365 };
366 
367 DRIVER_MODULE(mv_thermal, simplebus, mv_thermal_driver, 0, 0);
368