xref: /freebsd/sys/arm/allwinner/aw_thermal.c (revision d96700a6da2afa88607fbd7405ade439424d10d9)
1 /*-
2  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 /*
30  * Allwinner thermal sensor controller
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/rman.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #include <sys/reboot.h>
43 #include <sys/module.h>
44 #include <machine/bus.h>
45 
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 
49 #include <dev/extres/clk/clk.h>
50 #include <dev/extres/hwreset/hwreset.h>
51 
52 #include <arm/allwinner/aw_sid.h>
53 
54 #define	THS_CTRL0		0x00
55 #define	THS_CTRL1		0x04
56 #define	 ADC_CALI_EN		(1 << 17)
57 #define	THS_CTRL2		0x40
58 #define	 SENSOR_ACQ1_SHIFT	16
59 #define	 SENSOR2_EN		(1 << 2)
60 #define	 SENSOR1_EN		(1 << 1)
61 #define	 SENSOR0_EN		(1 << 0)
62 #define	THS_INTC		0x44
63 #define	THS_INTS		0x48
64 #define	 THS2_DATA_IRQ_STS	(1 << 10)
65 #define	 THS1_DATA_IRQ_STS	(1 << 9)
66 #define	 THS0_DATA_IRQ_STS	(1 << 8)
67 #define	 SHUT_INT2_STS		(1 << 6)
68 #define	 SHUT_INT1_STS		(1 << 5)
69 #define	 SHUT_INT0_STS		(1 << 4)
70 #define	 ALARM_INT2_STS		(1 << 2)
71 #define	 ALARM_INT1_STS		(1 << 1)
72 #define	 ALARM_INT0_STS		(1 << 0)
73 #define	THS_FILTER		0x70
74 #define	THS_CALIB0		0x74
75 #define	THS_CALIB1		0x78
76 #define	THS_DATA0		0x80
77 #define	THS_DATA1		0x84
78 #define	THS_DATA2		0x88
79 #define	 DATA_MASK		0xfff
80 
81 #define	A83T_ADC_ACQUIRE_TIME	0x17
82 #define	A83T_FILTER		0x4
83 #define	A83T_INTC		0x1000
84 #define	A83T_TEMP_BASE		2719000
85 #define	A83T_TEMP_DIV		14186
86 #define	A83T_CLK_RATE		24000000
87 
88 #define	A64_ADC_ACQUIRE_TIME	0x190
89 #define	A64_FILTER		0x6
90 #define A64_INTC		0x18000
91 #define	A64_TEMP_BASE		2170000
92 #define	A64_TEMP_DIV		8560
93 #define	A64_CLK_RATE		4000000
94 
95 #define	TEMP_C_TO_K		273
96 #define	SENSOR_ENABLE_ALL	(SENSOR0_EN|SENSOR1_EN|SENSOR2_EN)
97 #define	SHUT_INT_ALL		(SHUT_INT0_STS|SHUT_INT1_STS|SHUT_INT2_STS)
98 
99 #define	MAX_SENSORS	3
100 
101 struct aw_thermal_sensor {
102 	const char		*name;
103 	const char		*desc;
104 };
105 
106 struct aw_thermal_config {
107 	struct aw_thermal_sensor	sensors[MAX_SENSORS];
108 	int				nsensors;
109 	uint64_t			clk_rate;
110 	uint32_t			adc_acquire_time;
111 	uint32_t			filter;
112 	uint32_t			intc;
113 	uint32_t			temp_base;
114 	uint32_t			temp_div;
115 };
116 
117 static const struct aw_thermal_config a83t_config = {
118 	.nsensors = 3,
119 	.sensors = {
120 		[0] = {
121 			.name = "cluster0",
122 			.desc = "CPU cluster 0 temperature",
123 		},
124 		[1] = {
125 			.name = "cluster1",
126 			.desc = "CPU cluster 1 temperature",
127 		},
128 		[2] = {
129 			.name = "gpu",
130 			.desc = "GPU temperature",
131 		},
132 	},
133 	.clk_rate = A83T_CLK_RATE,
134 	.adc_acquire_time = A83T_ADC_ACQUIRE_TIME,
135 	.filter = A83T_FILTER,
136 	.intc = A83T_INTC,
137 	.temp_base = A83T_TEMP_BASE,
138 	.temp_div = A83T_TEMP_DIV,
139 };
140 
141 static const struct aw_thermal_config a64_config = {
142 	.nsensors = 3,
143 	.sensors = {
144 		[0] = {
145 			.name = "cpu",
146 			.desc = "CPU temperature",
147 		},
148 		[1] = {
149 			.name = "gpu1",
150 			.desc = "GPU temperature 1",
151 		},
152 		[2] = {
153 			.name = "gpu2",
154 			.desc = "GPU temperature 2",
155 		},
156 	},
157 	.clk_rate = A64_CLK_RATE,
158 	.adc_acquire_time = A64_ADC_ACQUIRE_TIME,
159 	.filter = A64_FILTER,
160 	.intc = A64_INTC,
161 	.temp_base = A64_TEMP_BASE,
162 	.temp_div = A64_TEMP_DIV,
163 };
164 
165 static struct ofw_compat_data compat_data[] = {
166 	{ "allwinner,sun8i-a83t-ts",	(uintptr_t)&a83t_config },
167 	{ "allwinner,sun50i-a64-ts",	(uintptr_t)&a64_config },
168 	{ NULL,				(uintptr_t)NULL }
169 };
170 
171 #define	THS_CONF(d)		\
172 	(void *)ofw_bus_search_compatible((d), compat_data)->ocd_data
173 
174 struct aw_thermal_softc {
175 	struct resource			*res[2];
176 	struct aw_thermal_config	*conf;
177 };
178 
179 static struct resource_spec aw_thermal_spec[] = {
180 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
181 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
182 	{ -1, 0 }
183 };
184 
185 #define	RD4(sc, reg)		bus_read_4((sc)->res[0], (reg))
186 #define	WR4(sc, reg, val)	bus_write_4((sc)->res[0], (reg), (val))
187 
188 static int
189 aw_thermal_init(struct aw_thermal_softc *sc)
190 {
191 	uint32_t calib0, calib1;
192 	int error;
193 
194 	/* Read calibration settings from SRAM */
195 	error = aw_sid_read_tscalib(&calib0, &calib1);
196 	if (error != 0)
197 		return (error);
198 
199 	/* Write calibration settings to thermal controller */
200 	WR4(sc, THS_CALIB0, calib0);
201 	WR4(sc, THS_CALIB1, calib1);
202 
203 	/* Configure ADC acquire time (CLK_IN/(N+1)) and enable sensors */
204 	WR4(sc, THS_CTRL1, ADC_CALI_EN);
205 	WR4(sc, THS_CTRL0, sc->conf->adc_acquire_time);
206 	WR4(sc, THS_CTRL2, sc->conf->adc_acquire_time << SENSOR_ACQ1_SHIFT);
207 
208 	/* Enable average filter */
209 	WR4(sc, THS_FILTER, sc->conf->filter);
210 
211 	/* Enable interrupts */
212 	WR4(sc, THS_INTS, RD4(sc, THS_INTS));
213 	WR4(sc, THS_INTC, sc->conf->intc | SHUT_INT_ALL);
214 
215 	/* Enable sensors */
216 	WR4(sc, THS_CTRL2, RD4(sc, THS_CTRL2) | SENSOR_ENABLE_ALL);
217 
218 	return (0);
219 }
220 
221 static int
222 aw_thermal_reg_to_temp(struct aw_thermal_softc *sc, uint32_t val)
223 {
224 	return ((sc->conf->temp_base - val * 1000) / sc->conf->temp_div);
225 }
226 
227 static int
228 aw_thermal_gettemp(struct aw_thermal_softc *sc, int sensor)
229 {
230 	uint32_t val;
231 
232 	val = RD4(sc, THS_DATA0 + (sensor * 4));
233 
234 	return (aw_thermal_reg_to_temp(sc, val) + TEMP_C_TO_K);
235 }
236 
237 static int
238 aw_thermal_sysctl(SYSCTL_HANDLER_ARGS)
239 {
240 	struct aw_thermal_softc *sc;
241 	int sensor, val;
242 
243 	sc = arg1;
244 	sensor = arg2;
245 
246 	val = aw_thermal_gettemp(sc, sensor);
247 
248 	return sysctl_handle_opaque(oidp, &val, sizeof(val), req);
249 }
250 
251 static void
252 aw_thermal_intr(void *arg)
253 {
254 	struct aw_thermal_softc *sc;
255 	device_t dev;
256 	uint32_t ints;
257 
258 	dev = arg;
259 	sc = device_get_softc(dev);
260 
261 	ints = RD4(sc, THS_INTS);
262 	WR4(sc, THS_INTS, ints);
263 
264 	if ((ints & SHUT_INT_ALL) != 0) {
265 		device_printf(dev,
266 		   "WARNING - current temperature exceeds safe limits\n");
267 		shutdown_nice(RB_POWEROFF);
268 	}
269 }
270 
271 static int
272 aw_thermal_probe(device_t dev)
273 {
274 	if (!ofw_bus_status_okay(dev))
275 		return (ENXIO);
276 
277 	if (THS_CONF(dev) == NULL)
278 		return (ENXIO);
279 
280 	device_set_desc(dev, "Allwinner Thermal Sensor Controller");
281 	return (BUS_PROBE_DEFAULT);
282 }
283 
284 static int
285 aw_thermal_attach(device_t dev)
286 {
287 	struct aw_thermal_softc *sc;
288 	clk_t clk_ahb, clk_ths;
289 	hwreset_t rst;
290 	int i, error;
291 	void *ih;
292 
293 	sc = device_get_softc(dev);
294 	clk_ahb = clk_ths = NULL;
295 	rst = NULL;
296 	ih = NULL;
297 
298 	sc->conf = THS_CONF(dev);
299 
300 	if (bus_alloc_resources(dev, aw_thermal_spec, sc->res) != 0) {
301 		device_printf(dev, "cannot allocate resources for device\n");
302 		return (ENXIO);
303 	}
304 
305 	if (clk_get_by_ofw_name(dev, 0, "ahb", &clk_ahb) == 0) {
306 		error = clk_enable(clk_ahb);
307 		if (error != 0) {
308 			device_printf(dev, "cannot enable ahb clock\n");
309 			goto fail;
310 		}
311 	}
312 	if (clk_get_by_ofw_name(dev, 0, "ths", &clk_ths) == 0) {
313 		error = clk_set_freq(clk_ths, sc->conf->clk_rate, 0);
314 		if (error != 0) {
315 			device_printf(dev, "cannot set ths clock rate\n");
316 			goto fail;
317 		}
318 		error = clk_enable(clk_ths);
319 		if (error != 0) {
320 			device_printf(dev, "cannot enable ths clock\n");
321 			goto fail;
322 		}
323 	}
324 	if (hwreset_get_by_ofw_idx(dev, 0, 0, &rst) == 0) {
325 		error = hwreset_deassert(rst);
326 		if (error != 0) {
327 			device_printf(dev, "cannot de-assert reset\n");
328 			goto fail;
329 		}
330 	}
331 
332 	error = bus_setup_intr(dev, sc->res[1], INTR_TYPE_MISC | INTR_MPSAFE,
333 	    NULL, aw_thermal_intr, dev, &ih);
334 	if (error != 0) {
335 		device_printf(dev, "cannot setup interrupt handler\n");
336 		goto fail;
337 	}
338 
339 	if (aw_thermal_init(sc) != 0)
340 		goto fail;
341 
342 	for (i = 0; i < sc->conf->nsensors; i++)
343 		SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
344 		    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
345 		    OID_AUTO, sc->conf->sensors[i].name,
346 		    CTLTYPE_INT | CTLFLAG_RD,
347 		    sc, i, aw_thermal_sysctl, "IK0",
348 		    sc->conf->sensors[i].desc);
349 
350 	return (0);
351 
352 fail:
353 	if (ih != NULL)
354 		bus_teardown_intr(dev, sc->res[1], ih);
355 	if (rst != NULL)
356 		hwreset_release(rst);
357 	if (clk_ahb != NULL)
358 		clk_release(clk_ahb);
359 	if (clk_ths != NULL)
360 		clk_release(clk_ths);
361 	bus_release_resources(dev, aw_thermal_spec, sc->res);
362 
363 	return (ENXIO);
364 }
365 
366 static device_method_t aw_thermal_methods[] = {
367 	/* Device interface */
368 	DEVMETHOD(device_probe,		aw_thermal_probe),
369 	DEVMETHOD(device_attach,	aw_thermal_attach),
370 
371 	DEVMETHOD_END
372 };
373 
374 static driver_t aw_thermal_driver = {
375 	"aw_thermal",
376 	aw_thermal_methods,
377 	sizeof(struct aw_thermal_softc),
378 };
379 
380 static devclass_t aw_thermal_devclass;
381 
382 DRIVER_MODULE(aw_thermal, simplebus, aw_thermal_driver, aw_thermal_devclass,
383     0, 0);
384 MODULE_VERSION(aw_thermal, 1);
385