sunxi_thermal.c revision 1.14.4.1 1 1.14.4.1 martin /* $NetBSD: sunxi_thermal.c,v 1.14.4.1 2023/05/04 18:56:36 martin Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2016-2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill /*
30 1.1 jmcneill * Allwinner thermal sensor controller
31 1.1 jmcneill */
32 1.1 jmcneill
33 1.1 jmcneill #include <sys/cdefs.h>
34 1.14.4.1 martin __KERNEL_RCSID(0, "$NetBSD: sunxi_thermal.c,v 1.14.4.1 2023/05/04 18:56:36 martin Exp $");
35 1.1 jmcneill
36 1.1 jmcneill #include <sys/param.h>
37 1.1 jmcneill #include <sys/systm.h>
38 1.1 jmcneill #include <sys/device.h>
39 1.1 jmcneill #include <sys/kernel.h>
40 1.1 jmcneill #include <sys/reboot.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <dev/sysmon/sysmonvar.h>
43 1.1 jmcneill #include <dev/sysmon/sysmon_taskq.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/fdt/fdtvar.h>
46 1.1 jmcneill
47 1.1 jmcneill #include <arm/sunxi/sunxi_sid.h>
48 1.1 jmcneill
49 1.1 jmcneill #define THS_CTRL0 0x00
50 1.1 jmcneill #define THS_CTRL1 0x04
51 1.1 jmcneill #define ADC_CALI_EN (1 << 17)
52 1.1 jmcneill #define THS_CTRL2 0x40
53 1.1 jmcneill #define SENSOR_ACQ1_SHIFT 16
54 1.1 jmcneill #define SENSOR2_EN (1 << 2)
55 1.1 jmcneill #define SENSOR1_EN (1 << 1)
56 1.1 jmcneill #define SENSOR0_EN (1 << 0)
57 1.1 jmcneill #define THS_INTC 0x44
58 1.1 jmcneill #define THS_INTS 0x48
59 1.1 jmcneill #define THS2_DATA_IRQ_STS (1 << 10)
60 1.1 jmcneill #define THS1_DATA_IRQ_STS (1 << 9)
61 1.1 jmcneill #define THS0_DATA_IRQ_STS (1 << 8)
62 1.1 jmcneill #define SHUT_INT2_STS (1 << 6)
63 1.1 jmcneill #define SHUT_INT1_STS (1 << 5)
64 1.1 jmcneill #define SHUT_INT0_STS (1 << 4)
65 1.1 jmcneill #define ALARM_INT2_STS (1 << 2)
66 1.1 jmcneill #define ALARM_INT1_STS (1 << 1)
67 1.1 jmcneill #define ALARM_INT0_STS (1 << 0)
68 1.1 jmcneill #define THS_ALARM0_CTRL 0x50
69 1.1 jmcneill #define ALARM_T_HOT_MASK 0xfff
70 1.1 jmcneill #define ALARM_T_HOT_SHIFT 16
71 1.1 jmcneill #define ALARM_T_HYST_MASK 0xfff
72 1.1 jmcneill #define ALARM_T_HYST_SHIFT 0
73 1.1 jmcneill #define THS_SHUTDOWN0_CTRL 0x60
74 1.1 jmcneill #define SHUT_T_HOT_MASK 0xfff
75 1.1 jmcneill #define SHUT_T_HOT_SHIFT 16
76 1.1 jmcneill #define THS_FILTER 0x70
77 1.1 jmcneill #define THS_CALIB0 0x74
78 1.1 jmcneill #define THS_CALIB1 0x78
79 1.1 jmcneill #define THS_DATA0 0x80
80 1.1 jmcneill #define THS_DATA1 0x84
81 1.1 jmcneill #define THS_DATA2 0x88
82 1.1 jmcneill #define DATA_MASK 0xfff
83 1.1 jmcneill
84 1.1 jmcneill #define A83T_ADC_ACQUIRE_TIME 0x17
85 1.1 jmcneill #define A83T_FILTER 0x4
86 1.1 jmcneill #define A83T_INTC 0x1000
87 1.1 jmcneill #define A83T_TEMP_BASE 2719000
88 1.1 jmcneill #define A83T_TEMP_MUL 1000
89 1.1 jmcneill #define A83T_TEMP_DIV 14186
90 1.1 jmcneill #define A83T_CLK_RATE 24000000
91 1.1 jmcneill
92 1.1 jmcneill #define A64_ADC_ACQUIRE_TIME 0x190
93 1.1 jmcneill #define A64_FILTER 0x6
94 1.1 jmcneill #define A64_INTC 0x18000
95 1.1 jmcneill #define A64_TEMP_BASE 2170000
96 1.1 jmcneill #define A64_TEMP_MUL 1000
97 1.1 jmcneill #define A64_TEMP_DIV 8560
98 1.1 jmcneill #define A64_CLK_RATE 4000000
99 1.1 jmcneill
100 1.1 jmcneill #define H3_ADC_ACQUIRE_TIME 0x3f
101 1.1 jmcneill #define H3_FILTER 0x6
102 1.1 jmcneill #define H3_INTC 0x191000
103 1.1 jmcneill #define H3_TEMP_BASE 217
104 1.1 jmcneill #define H3_TEMP_MUL 1000
105 1.1 jmcneill #define H3_TEMP_DIV 8253
106 1.1 jmcneill #define H3_TEMP_MINUS 1794000
107 1.1 jmcneill #define H3_CLK_RATE 4000000
108 1.1 jmcneill #define H3_INIT_ALARM 90 /* degC */
109 1.1 jmcneill #define H3_INIT_SHUT 105 /* degC */
110 1.1 jmcneill
111 1.5 jmcneill #define H5_ADC_ACQUIRE_TIME 0x1df
112 1.5 jmcneill #define H5_FILTER 0x6
113 1.5 jmcneill #define H5_INTC 0x3a070
114 1.5 jmcneill #define H5_TEMP_DIV 20
115 1.5 jmcneill #define H5_TEMP_BASE_L 233832448
116 1.5 jmcneill #define H5_TEMP_MUL_L 124885
117 1.5 jmcneill #define H5_TEMP_BASE_H_0 271581184
118 1.5 jmcneill #define H5_TEMP_MUL_H_0 152253
119 1.5 jmcneill #define H5_TEMP_BASE_H_1 289406976
120 1.5 jmcneill #define H5_TEMP_MUL_H_1 166723
121 1.5 jmcneill #define H5_CLK_RATE 24000000
122 1.5 jmcneill #define H5_INIT_ALARM 105 /* degC */
123 1.5 jmcneill #define H5_INIT_SHUT 120 /* degC */
124 1.5 jmcneill
125 1.1 jmcneill #define TEMP_C_TO_K 273150000
126 1.1 jmcneill #define SENSOR_ENABLE_ALL (SENSOR0_EN|SENSOR1_EN|SENSOR2_EN)
127 1.1 jmcneill #define SHUT_INT_ALL (SHUT_INT0_STS|SHUT_INT1_STS|SHUT_INT2_STS)
128 1.1 jmcneill #define ALARM_INT_ALL (ALARM_INT0_STS)
129 1.1 jmcneill
130 1.1 jmcneill #define MAX_SENSORS 3
131 1.1 jmcneill
132 1.1 jmcneill #if notyet
133 1.1 jmcneill #define THROTTLE_ENABLE_DEFAULT 1
134 1.1 jmcneill
135 1.1 jmcneill /* Enable thermal throttling */
136 1.1 jmcneill static int sunxi_thermal_throttle_enable = THROTTLE_ENABLE_DEFAULT;
137 1.1 jmcneill #endif
138 1.1 jmcneill
139 1.1 jmcneill struct sunxi_thermal_sensor {
140 1.1 jmcneill const char *name;
141 1.1 jmcneill const char *desc;
142 1.1 jmcneill int init_alarm;
143 1.1 jmcneill int init_shut;
144 1.1 jmcneill };
145 1.1 jmcneill
146 1.1 jmcneill struct sunxi_thermal_config {
147 1.1 jmcneill struct sunxi_thermal_sensor sensors[MAX_SENSORS];
148 1.1 jmcneill int nsensors;
149 1.1 jmcneill uint64_t clk_rate;
150 1.1 jmcneill uint32_t adc_acquire_time;
151 1.1 jmcneill int adc_cali_en;
152 1.1 jmcneill uint32_t filter;
153 1.1 jmcneill uint32_t intc;
154 1.5 jmcneill int (*to_temp)(u_int, uint32_t);
155 1.5 jmcneill uint32_t (*to_reg)(u_int, int);
156 1.1 jmcneill int calib0, calib1;
157 1.1 jmcneill uint32_t calib0_mask, calib1_mask;
158 1.1 jmcneill };
159 1.1 jmcneill
160 1.1 jmcneill static int
161 1.5 jmcneill a83t_to_temp(u_int sensor, uint32_t val)
162 1.1 jmcneill {
163 1.1 jmcneill return ((A83T_TEMP_BASE - (val * A83T_TEMP_MUL)) / A83T_TEMP_DIV);
164 1.1 jmcneill }
165 1.1 jmcneill
166 1.1 jmcneill static const struct sunxi_thermal_config a83t_config = {
167 1.1 jmcneill .nsensors = 3,
168 1.1 jmcneill .sensors = {
169 1.1 jmcneill [0] = {
170 1.1 jmcneill .name = "cluster0",
171 1.1 jmcneill .desc = "CPU cluster 0 temperature",
172 1.1 jmcneill },
173 1.1 jmcneill [1] = {
174 1.1 jmcneill .name = "cluster1",
175 1.1 jmcneill .desc = "CPU cluster 1 temperature",
176 1.1 jmcneill },
177 1.1 jmcneill [2] = {
178 1.1 jmcneill .name = "gpu",
179 1.1 jmcneill .desc = "GPU temperature",
180 1.1 jmcneill },
181 1.1 jmcneill },
182 1.1 jmcneill .clk_rate = A83T_CLK_RATE,
183 1.1 jmcneill .adc_acquire_time = A83T_ADC_ACQUIRE_TIME,
184 1.1 jmcneill .adc_cali_en = 1,
185 1.1 jmcneill .filter = A83T_FILTER,
186 1.1 jmcneill .intc = A83T_INTC,
187 1.1 jmcneill .to_temp = a83t_to_temp,
188 1.1 jmcneill .calib0_mask = 0xffffffff,
189 1.1 jmcneill .calib1_mask = 0xffffffff,
190 1.1 jmcneill };
191 1.1 jmcneill
192 1.1 jmcneill static int
193 1.5 jmcneill a64_to_temp(u_int sensor, uint32_t val)
194 1.1 jmcneill {
195 1.1 jmcneill return ((A64_TEMP_BASE - (val * A64_TEMP_MUL)) / A64_TEMP_DIV);
196 1.1 jmcneill }
197 1.1 jmcneill
198 1.1 jmcneill static const struct sunxi_thermal_config a64_config = {
199 1.1 jmcneill .nsensors = 3,
200 1.1 jmcneill .sensors = {
201 1.1 jmcneill [0] = {
202 1.1 jmcneill .name = "cpu",
203 1.1 jmcneill .desc = "CPU temperature",
204 1.1 jmcneill },
205 1.1 jmcneill [1] = {
206 1.1 jmcneill .name = "gpu1",
207 1.1 jmcneill .desc = "GPU temperature 1",
208 1.1 jmcneill },
209 1.1 jmcneill [2] = {
210 1.1 jmcneill .name = "gpu2",
211 1.1 jmcneill .desc = "GPU temperature 2",
212 1.1 jmcneill },
213 1.1 jmcneill },
214 1.1 jmcneill .clk_rate = A64_CLK_RATE,
215 1.1 jmcneill .adc_acquire_time = A64_ADC_ACQUIRE_TIME,
216 1.1 jmcneill .filter = A64_FILTER,
217 1.1 jmcneill .intc = A64_INTC,
218 1.1 jmcneill .to_temp = a64_to_temp,
219 1.1 jmcneill };
220 1.1 jmcneill
221 1.1 jmcneill static int
222 1.5 jmcneill h3_to_temp(u_int sensor, uint32_t val)
223 1.1 jmcneill {
224 1.1 jmcneill return (H3_TEMP_BASE - ((val * H3_TEMP_MUL) / H3_TEMP_DIV));
225 1.1 jmcneill }
226 1.1 jmcneill
227 1.1 jmcneill static uint32_t
228 1.5 jmcneill h3_to_reg(u_int sensor, int val)
229 1.1 jmcneill {
230 1.1 jmcneill return ((H3_TEMP_MINUS - (val * H3_TEMP_DIV)) / H3_TEMP_MUL);
231 1.1 jmcneill }
232 1.1 jmcneill
233 1.1 jmcneill static const struct sunxi_thermal_config h3_config = {
234 1.1 jmcneill .nsensors = 1,
235 1.1 jmcneill .sensors = {
236 1.1 jmcneill [0] = {
237 1.1 jmcneill .name = "cpu",
238 1.1 jmcneill .desc = "CPU temperature",
239 1.1 jmcneill .init_alarm = H3_INIT_ALARM,
240 1.1 jmcneill .init_shut = H3_INIT_SHUT,
241 1.1 jmcneill },
242 1.1 jmcneill },
243 1.1 jmcneill .clk_rate = H3_CLK_RATE,
244 1.1 jmcneill .adc_acquire_time = H3_ADC_ACQUIRE_TIME,
245 1.1 jmcneill .filter = H3_FILTER,
246 1.1 jmcneill .intc = H3_INTC,
247 1.1 jmcneill .to_temp = h3_to_temp,
248 1.1 jmcneill .to_reg = h3_to_reg,
249 1.1 jmcneill .calib0_mask = 0xfff,
250 1.1 jmcneill };
251 1.1 jmcneill
252 1.5 jmcneill static int
253 1.5 jmcneill h5_to_temp(u_int sensor, uint32_t val)
254 1.5 jmcneill {
255 1.5 jmcneill int base, mul;
256 1.5 jmcneill
257 1.5 jmcneill if (val >= 0x500) {
258 1.5 jmcneill base = H5_TEMP_BASE_L;
259 1.5 jmcneill mul = H5_TEMP_MUL_L;
260 1.5 jmcneill } else {
261 1.5 jmcneill base = sensor == 0 ? H5_TEMP_BASE_H_0 : H5_TEMP_BASE_H_1;
262 1.6 bsiegert mul = sensor == 0 ? H5_TEMP_MUL_H_0 : H5_TEMP_MUL_H_1;
263 1.5 jmcneill }
264 1.5 jmcneill
265 1.5 jmcneill return (base - val * mul) >> H5_TEMP_DIV;
266 1.5 jmcneill }
267 1.5 jmcneill
268 1.5 jmcneill static uint32_t
269 1.5 jmcneill h5_to_reg(u_int sensor, int val)
270 1.5 jmcneill {
271 1.5 jmcneill int base, mul;
272 1.5 jmcneill
273 1.5 jmcneill if (val <= 70) {
274 1.5 jmcneill base = H5_TEMP_BASE_L;
275 1.5 jmcneill mul = H5_TEMP_MUL_L;
276 1.5 jmcneill } else {
277 1.5 jmcneill base = sensor == 0 ? H5_TEMP_BASE_H_0 : H5_TEMP_BASE_H_1;
278 1.6 bsiegert mul = sensor == 0 ? H5_TEMP_MUL_H_0 : H5_TEMP_MUL_H_1;
279 1.5 jmcneill }
280 1.5 jmcneill
281 1.5 jmcneill return (base - (val << H5_TEMP_DIV)) / mul;
282 1.5 jmcneill }
283 1.5 jmcneill
284 1.5 jmcneill static const struct sunxi_thermal_config h5_config = {
285 1.5 jmcneill .nsensors = 2,
286 1.5 jmcneill .sensors = {
287 1.5 jmcneill [0] = {
288 1.5 jmcneill .name = "cpu",
289 1.5 jmcneill .desc = "CPU temperature",
290 1.5 jmcneill .init_alarm = H5_INIT_ALARM,
291 1.5 jmcneill .init_shut = H5_INIT_SHUT,
292 1.5 jmcneill },
293 1.5 jmcneill [1] = {
294 1.5 jmcneill .name = "gpu",
295 1.5 jmcneill .desc = "GPU temperature",
296 1.5 jmcneill .init_alarm = H5_INIT_ALARM,
297 1.5 jmcneill .init_shut = H5_INIT_SHUT,
298 1.5 jmcneill },
299 1.5 jmcneill },
300 1.5 jmcneill .clk_rate = H5_CLK_RATE,
301 1.5 jmcneill .adc_acquire_time = H5_ADC_ACQUIRE_TIME,
302 1.5 jmcneill .filter = H5_FILTER,
303 1.5 jmcneill .intc = H5_INTC,
304 1.5 jmcneill .to_temp = h5_to_temp,
305 1.5 jmcneill .to_reg = h5_to_reg,
306 1.5 jmcneill };
307 1.5 jmcneill
308 1.10 thorpej static struct device_compatible_entry compat_data[] = {
309 1.14 jmcneill { .compat = "allwinner,sun8i-a83t-ths", .data = &a83t_config },
310 1.14 jmcneill { .compat = "allwinner,sun8i-h3-ths", .data = &h3_config },
311 1.14 jmcneill { .compat = "allwinner,sun50i-a64-ths", .data = &a64_config },
312 1.14 jmcneill { .compat = "allwinner,sun50i-h5-ths", .data = &h5_config },
313 1.14 jmcneill
314 1.14 jmcneill /*
315 1.14 jmcneill * DTCOMPAT: Old compat strings. Do not add to this list.
316 1.14 jmcneill */
317 1.10 thorpej { .compat = "allwinner,sun8i-a83t-ts", .data = &a83t_config },
318 1.10 thorpej { .compat = "allwinner,sun8i-h3-ts", .data = &h3_config },
319 1.10 thorpej { .compat = "allwinner,sun50i-a64-ts", .data = &a64_config },
320 1.10 thorpej { .compat = "allwinner,sun50i-h5-ts", .data = &h5_config },
321 1.12 thorpej DEVICE_COMPAT_EOL
322 1.1 jmcneill };
323 1.1 jmcneill
324 1.1 jmcneill struct sunxi_thermal_softc {
325 1.1 jmcneill device_t dev;
326 1.1 jmcneill int phandle;
327 1.1 jmcneill bus_space_tag_t bst;
328 1.1 jmcneill bus_space_handle_t bsh;
329 1.10 thorpej const struct sunxi_thermal_config *conf;
330 1.1 jmcneill
331 1.1 jmcneill kmutex_t lock;
332 1.1 jmcneill callout_t tick;
333 1.1 jmcneill
334 1.1 jmcneill struct sysmon_envsys *sme;
335 1.1 jmcneill envsys_data_t data[MAX_SENSORS];
336 1.1 jmcneill };
337 1.1 jmcneill
338 1.1 jmcneill #define RD4(sc, reg) \
339 1.1 jmcneill bus_space_read_4((sc)->bst, (sc)->bsh, (reg))
340 1.1 jmcneill #define WR4(sc, reg, val) \
341 1.1 jmcneill bus_space_write_4((sc)->bst, (sc)->bsh, (reg), (val))
342 1.1 jmcneill
343 1.1 jmcneill static int
344 1.1 jmcneill sunxi_thermal_init(struct sunxi_thermal_softc *sc)
345 1.1 jmcneill {
346 1.1 jmcneill uint32_t calib[2];
347 1.1 jmcneill int error;
348 1.1 jmcneill
349 1.1 jmcneill if (sc->conf->calib0_mask != 0 || sc->conf->calib1_mask != 0) {
350 1.1 jmcneill /* Read calibration settings from SRAM */
351 1.1 jmcneill error = sunxi_sid_read_tscalib(calib);
352 1.1 jmcneill if (error != 0)
353 1.1 jmcneill return error;
354 1.1 jmcneill
355 1.1 jmcneill calib[0] &= sc->conf->calib0_mask;
356 1.1 jmcneill calib[1] &= sc->conf->calib1_mask;
357 1.1 jmcneill
358 1.1 jmcneill /* Write calibration settings to thermal controller */
359 1.1 jmcneill if (calib[0] != 0)
360 1.1 jmcneill WR4(sc, THS_CALIB0, calib[0]);
361 1.1 jmcneill if (calib[1] != 0)
362 1.1 jmcneill WR4(sc, THS_CALIB1, calib[1]);
363 1.1 jmcneill }
364 1.1 jmcneill
365 1.1 jmcneill /* Configure ADC acquire time (CLK_IN/(N+1)) and enable sensors */
366 1.1 jmcneill WR4(sc, THS_CTRL1, ADC_CALI_EN);
367 1.1 jmcneill WR4(sc, THS_CTRL0, sc->conf->adc_acquire_time);
368 1.1 jmcneill WR4(sc, THS_CTRL2, sc->conf->adc_acquire_time << SENSOR_ACQ1_SHIFT);
369 1.1 jmcneill
370 1.1 jmcneill /* Enable average filter */
371 1.1 jmcneill WR4(sc, THS_FILTER, sc->conf->filter);
372 1.1 jmcneill
373 1.1 jmcneill /* Enable interrupts */
374 1.1 jmcneill WR4(sc, THS_INTS, RD4(sc, THS_INTS));
375 1.1 jmcneill WR4(sc, THS_INTC, sc->conf->intc | SHUT_INT_ALL | ALARM_INT_ALL);
376 1.1 jmcneill
377 1.1 jmcneill /* Enable sensors */
378 1.1 jmcneill WR4(sc, THS_CTRL2, RD4(sc, THS_CTRL2) | SENSOR_ENABLE_ALL);
379 1.1 jmcneill
380 1.1 jmcneill return 0;
381 1.1 jmcneill }
382 1.1 jmcneill
383 1.1 jmcneill static int
384 1.1 jmcneill sunxi_thermal_gettemp(struct sunxi_thermal_softc *sc, int sensor)
385 1.1 jmcneill {
386 1.1 jmcneill uint32_t val;
387 1.1 jmcneill
388 1.1 jmcneill val = RD4(sc, THS_DATA0 + (sensor * 4));
389 1.1 jmcneill
390 1.5 jmcneill return sc->conf->to_temp(sensor, val);
391 1.1 jmcneill }
392 1.1 jmcneill
393 1.1 jmcneill static int
394 1.1 jmcneill sunxi_thermal_getshut(struct sunxi_thermal_softc *sc, int sensor)
395 1.1 jmcneill {
396 1.1 jmcneill uint32_t val;
397 1.1 jmcneill
398 1.1 jmcneill val = RD4(sc, THS_SHUTDOWN0_CTRL + (sensor * 4));
399 1.1 jmcneill val = (val >> SHUT_T_HOT_SHIFT) & SHUT_T_HOT_MASK;
400 1.1 jmcneill
401 1.5 jmcneill return sc->conf->to_temp(sensor, val);
402 1.1 jmcneill }
403 1.1 jmcneill
404 1.1 jmcneill static void
405 1.1 jmcneill sunxi_thermal_setshut(struct sunxi_thermal_softc *sc, int sensor, int temp)
406 1.1 jmcneill {
407 1.1 jmcneill uint32_t val;
408 1.1 jmcneill
409 1.1 jmcneill val = RD4(sc, THS_SHUTDOWN0_CTRL + (sensor * 4));
410 1.1 jmcneill val &= ~(SHUT_T_HOT_MASK << SHUT_T_HOT_SHIFT);
411 1.5 jmcneill val |= (sc->conf->to_reg(sensor, temp) << SHUT_T_HOT_SHIFT);
412 1.1 jmcneill WR4(sc, THS_SHUTDOWN0_CTRL + (sensor * 4), val);
413 1.1 jmcneill }
414 1.1 jmcneill
415 1.1 jmcneill static int
416 1.1 jmcneill sunxi_thermal_gethyst(struct sunxi_thermal_softc *sc, int sensor)
417 1.1 jmcneill {
418 1.1 jmcneill uint32_t val;
419 1.1 jmcneill
420 1.1 jmcneill val = RD4(sc, THS_ALARM0_CTRL + (sensor * 4));
421 1.1 jmcneill val = (val >> ALARM_T_HYST_SHIFT) & ALARM_T_HYST_MASK;
422 1.1 jmcneill
423 1.5 jmcneill return sc->conf->to_temp(sensor, val);
424 1.1 jmcneill }
425 1.1 jmcneill
426 1.1 jmcneill static int
427 1.1 jmcneill sunxi_thermal_getalarm(struct sunxi_thermal_softc *sc, int sensor)
428 1.1 jmcneill {
429 1.1 jmcneill uint32_t val;
430 1.1 jmcneill
431 1.1 jmcneill val = RD4(sc, THS_ALARM0_CTRL + (sensor * 4));
432 1.1 jmcneill val = (val >> ALARM_T_HOT_SHIFT) & ALARM_T_HOT_MASK;
433 1.1 jmcneill
434 1.5 jmcneill return sc->conf->to_temp(sensor, val);
435 1.1 jmcneill }
436 1.1 jmcneill
437 1.1 jmcneill static void
438 1.1 jmcneill sunxi_thermal_setalarm(struct sunxi_thermal_softc *sc, int sensor, int temp)
439 1.1 jmcneill {
440 1.1 jmcneill uint32_t val;
441 1.1 jmcneill
442 1.1 jmcneill val = RD4(sc, THS_ALARM0_CTRL + (sensor * 4));
443 1.1 jmcneill val &= ~(ALARM_T_HOT_MASK << ALARM_T_HOT_SHIFT);
444 1.5 jmcneill val |= (sc->conf->to_reg(sensor, temp) << ALARM_T_HOT_SHIFT);
445 1.1 jmcneill WR4(sc, THS_ALARM0_CTRL + (sensor * 4), val);
446 1.1 jmcneill }
447 1.1 jmcneill
448 1.1 jmcneill static void
449 1.1 jmcneill sunxi_thermal_task_shut(void *arg)
450 1.1 jmcneill {
451 1.1 jmcneill struct sunxi_thermal_softc * const sc = arg;
452 1.1 jmcneill
453 1.1 jmcneill device_printf(sc->dev,
454 1.1 jmcneill "WARNING - current temperature exceeds safe limits\n");
455 1.1 jmcneill
456 1.8 thorpej kern_reboot(RB_POWERDOWN, NULL);
457 1.1 jmcneill }
458 1.1 jmcneill
459 1.1 jmcneill static void
460 1.1 jmcneill sunxi_thermal_task_alarm(void *arg)
461 1.1 jmcneill {
462 1.1 jmcneill struct sunxi_thermal_softc * const sc = arg;
463 1.1 jmcneill
464 1.1 jmcneill const int alarm_val = sunxi_thermal_getalarm(sc, 0);
465 1.1 jmcneill const int temp_val = sunxi_thermal_gettemp(sc, 0);
466 1.1 jmcneill
467 1.1 jmcneill if (temp_val < alarm_val)
468 1.1 jmcneill pmf_event_inject(NULL, PMFE_THROTTLE_DISABLE);
469 1.1 jmcneill else
470 1.1 jmcneill callout_schedule(&sc->tick, hz);
471 1.1 jmcneill }
472 1.1 jmcneill
473 1.1 jmcneill static void
474 1.1 jmcneill sunxi_thermal_tick(void *arg)
475 1.1 jmcneill {
476 1.1 jmcneill struct sunxi_thermal_softc * const sc = arg;
477 1.1 jmcneill
478 1.1 jmcneill sysmon_task_queue_sched(0, sunxi_thermal_task_alarm, sc);
479 1.1 jmcneill }
480 1.1 jmcneill
481 1.1 jmcneill static int
482 1.1 jmcneill sunxi_thermal_intr(void *arg)
483 1.1 jmcneill {
484 1.1 jmcneill struct sunxi_thermal_softc * const sc = arg;
485 1.1 jmcneill uint32_t ints;
486 1.1 jmcneill
487 1.1 jmcneill mutex_enter(&sc->lock);
488 1.1 jmcneill
489 1.1 jmcneill ints = RD4(sc, THS_INTS);
490 1.1 jmcneill WR4(sc, THS_INTS, ints);
491 1.1 jmcneill
492 1.1 jmcneill if ((ints & SHUT_INT_ALL) != 0)
493 1.1 jmcneill sysmon_task_queue_sched(0, sunxi_thermal_task_shut, sc);
494 1.1 jmcneill
495 1.1 jmcneill if ((ints & ALARM_INT_ALL) != 0) {
496 1.1 jmcneill pmf_event_inject(NULL, PMFE_THROTTLE_ENABLE);
497 1.1 jmcneill sysmon_task_queue_sched(0, sunxi_thermal_task_alarm, sc);
498 1.1 jmcneill }
499 1.1 jmcneill
500 1.1 jmcneill mutex_exit(&sc->lock);
501 1.1 jmcneill
502 1.1 jmcneill return 1;
503 1.1 jmcneill }
504 1.1 jmcneill
505 1.1 jmcneill static void
506 1.1 jmcneill sunxi_thermal_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
507 1.1 jmcneill {
508 1.1 jmcneill struct sunxi_thermal_softc * const sc = sme->sme_cookie;
509 1.1 jmcneill
510 1.1 jmcneill const int64_t temp = sunxi_thermal_gettemp(sc, edata->private);
511 1.1 jmcneill
512 1.1 jmcneill edata->value_cur = temp * 1000000 + TEMP_C_TO_K;
513 1.1 jmcneill edata->state = ENVSYS_SVALID;
514 1.1 jmcneill }
515 1.1 jmcneill
516 1.1 jmcneill static int
517 1.1 jmcneill sunxi_thermal_init_clocks(struct sunxi_thermal_softc *sc)
518 1.1 jmcneill {
519 1.1 jmcneill struct fdtbus_reset *rst;
520 1.1 jmcneill struct clk *clk;
521 1.1 jmcneill int error;
522 1.1 jmcneill
523 1.14.4.1 martin clk = fdtbus_clock_get(sc->phandle, "bus");
524 1.14.4.1 martin if (clk == NULL) {
525 1.14.4.1 martin /* DTCOMPAT */
526 1.14.4.1 martin clk = fdtbus_clock_get(sc->phandle, "ahb");
527 1.14.4.1 martin }
528 1.7 jmcneill if (clk) {
529 1.7 jmcneill error = clk_enable(clk);
530 1.7 jmcneill if (error != 0)
531 1.7 jmcneill return error;
532 1.7 jmcneill }
533 1.1 jmcneill
534 1.14.4.1 martin clk = fdtbus_clock_get(sc->phandle, "mod");
535 1.14.4.1 martin if (clk == NULL) {
536 1.14.4.1 martin /* DTCOMPAT */
537 1.14.4.1 martin clk = fdtbus_clock_get(sc->phandle, "ths");
538 1.14.4.1 martin }
539 1.7 jmcneill if (clk) {
540 1.7 jmcneill error = clk_set_rate(clk, sc->conf->clk_rate);
541 1.7 jmcneill if (error != 0)
542 1.7 jmcneill return error;
543 1.7 jmcneill error = clk_enable(clk);
544 1.7 jmcneill if (error != 0)
545 1.7 jmcneill return error;
546 1.7 jmcneill }
547 1.1 jmcneill
548 1.1 jmcneill rst = fdtbus_reset_get_index(sc->phandle, 0);
549 1.7 jmcneill if (rst) {
550 1.7 jmcneill error = fdtbus_reset_deassert(rst);
551 1.7 jmcneill if (error != 0)
552 1.7 jmcneill return error;
553 1.7 jmcneill }
554 1.1 jmcneill
555 1.1 jmcneill return 0;
556 1.1 jmcneill }
557 1.1 jmcneill
558 1.1 jmcneill static int
559 1.1 jmcneill sunxi_thermal_match(device_t parent, cfdata_t cf, void *aux)
560 1.1 jmcneill {
561 1.1 jmcneill struct fdt_attach_args * const faa = aux;
562 1.1 jmcneill
563 1.13 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
564 1.1 jmcneill }
565 1.1 jmcneill
566 1.1 jmcneill static void
567 1.1 jmcneill sunxi_thermal_attach(device_t parent, device_t self, void *aux)
568 1.1 jmcneill {
569 1.1 jmcneill struct sunxi_thermal_softc * const sc = device_private(self);
570 1.1 jmcneill struct fdt_attach_args * const faa = aux;
571 1.1 jmcneill const int phandle = faa->faa_phandle;
572 1.1 jmcneill char intrstr[128];
573 1.1 jmcneill bus_addr_t addr;
574 1.1 jmcneill bus_size_t size;
575 1.1 jmcneill void *ih;
576 1.1 jmcneill int i;
577 1.1 jmcneill
578 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
579 1.1 jmcneill aprint_error(": couldn't get registers\n");
580 1.1 jmcneill return;
581 1.1 jmcneill }
582 1.1 jmcneill
583 1.1 jmcneill sc->dev = self;
584 1.1 jmcneill sc->phandle = phandle;
585 1.1 jmcneill sc->bst = faa->faa_bst;
586 1.13 thorpej sc->conf = of_compatible_lookup(phandle, compat_data)->data;
587 1.1 jmcneill if (bus_space_map(sc->bst, addr, size, 0, &sc->bsh) != 0) {
588 1.1 jmcneill aprint_error(": couldn't map registers\n");
589 1.1 jmcneill return;
590 1.1 jmcneill }
591 1.1 jmcneill mutex_init(&sc->lock, MUTEX_DEFAULT, IPL_VM);
592 1.1 jmcneill callout_init(&sc->tick, CALLOUT_MPSAFE);
593 1.1 jmcneill callout_setfunc(&sc->tick, sunxi_thermal_tick, sc);
594 1.1 jmcneill
595 1.1 jmcneill if (sunxi_thermal_init_clocks(sc) != 0) {
596 1.1 jmcneill aprint_error(": couldn't enable clocks\n");
597 1.1 jmcneill return;
598 1.1 jmcneill }
599 1.1 jmcneill
600 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
601 1.1 jmcneill aprint_error(": couldn't decode interrupt\n");
602 1.1 jmcneill return;
603 1.1 jmcneill }
604 1.1 jmcneill
605 1.1 jmcneill aprint_naive("\n");
606 1.1 jmcneill aprint_normal(": Thermal sensor controller\n");
607 1.1 jmcneill
608 1.9 jmcneill ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, FDT_INTR_MPSAFE,
609 1.9 jmcneill sunxi_thermal_intr, sc, device_xname(self));
610 1.1 jmcneill if (ih == NULL) {
611 1.1 jmcneill aprint_error_dev(self, "couldn't establish interrupt on %s\n",
612 1.1 jmcneill intrstr);
613 1.1 jmcneill return;
614 1.1 jmcneill }
615 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
616 1.1 jmcneill
617 1.1 jmcneill for (i = 0; i < sc->conf->nsensors; i++) {
618 1.1 jmcneill if (sc->conf->sensors[i].init_alarm > 0)
619 1.1 jmcneill sunxi_thermal_setalarm(sc, i,
620 1.1 jmcneill sc->conf->sensors[i].init_alarm);
621 1.1 jmcneill if (sc->conf->sensors[i].init_shut > 0)
622 1.1 jmcneill sunxi_thermal_setshut(sc, i,
623 1.1 jmcneill sc->conf->sensors[i].init_shut);
624 1.1 jmcneill }
625 1.1 jmcneill
626 1.1 jmcneill if (sunxi_thermal_init(sc) != 0) {
627 1.1 jmcneill aprint_error_dev(self, "failed to initialize sensors\n");
628 1.1 jmcneill return;
629 1.1 jmcneill }
630 1.1 jmcneill
631 1.1 jmcneill sc->sme = sysmon_envsys_create();
632 1.1 jmcneill sc->sme->sme_name = device_xname(self);
633 1.1 jmcneill sc->sme->sme_cookie = sc;
634 1.1 jmcneill sc->sme->sme_refresh = sunxi_thermal_refresh;
635 1.1 jmcneill for (i = 0; i < sc->conf->nsensors; i++) {
636 1.1 jmcneill sc->data[i].private = i;
637 1.1 jmcneill sc->data[i].units = ENVSYS_STEMP;
638 1.1 jmcneill sc->data[i].state = ENVSYS_SINVALID;
639 1.4 jmcneill strlcpy(sc->data[i].desc, sc->conf->sensors[i].desc,
640 1.4 jmcneill sizeof(sc->data[i].desc));
641 1.1 jmcneill sysmon_envsys_sensor_attach(sc->sme, &sc->data[i]);
642 1.1 jmcneill }
643 1.1 jmcneill sysmon_envsys_register(sc->sme);
644 1.1 jmcneill
645 1.1 jmcneill for (i = 0; i < sc->conf->nsensors; i++) {
646 1.1 jmcneill device_printf(self,
647 1.1 jmcneill "%s: alarm %dC hyst %dC shut %dC\n",
648 1.1 jmcneill sc->conf->sensors[i].name,
649 1.1 jmcneill sunxi_thermal_getalarm(sc, i),
650 1.1 jmcneill sunxi_thermal_gethyst(sc, i),
651 1.1 jmcneill sunxi_thermal_getshut(sc, i));
652 1.1 jmcneill }
653 1.1 jmcneill }
654 1.1 jmcneill
655 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_thermal, sizeof(struct sunxi_thermal_softc),
656 1.1 jmcneill sunxi_thermal_match, sunxi_thermal_attach, NULL, NULL);
657