sunxi_thermal.c revision 1.1 1 1.1 jmcneill /* $NetBSD: sunxi_thermal.c,v 1.1 2017/10/05 01:30:26 jmcneill 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.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: sunxi_thermal.c,v 1.1 2017/10/05 01:30:26 jmcneill 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.1 jmcneill #define TEMP_C_TO_K 273150000
112 1.1 jmcneill #define SENSOR_ENABLE_ALL (SENSOR0_EN|SENSOR1_EN|SENSOR2_EN)
113 1.1 jmcneill #define SHUT_INT_ALL (SHUT_INT0_STS|SHUT_INT1_STS|SHUT_INT2_STS)
114 1.1 jmcneill #define ALARM_INT_ALL (ALARM_INT0_STS)
115 1.1 jmcneill
116 1.1 jmcneill #define MAX_SENSORS 3
117 1.1 jmcneill
118 1.1 jmcneill #if notyet
119 1.1 jmcneill #define THROTTLE_ENABLE_DEFAULT 1
120 1.1 jmcneill
121 1.1 jmcneill /* Enable thermal throttling */
122 1.1 jmcneill static int sunxi_thermal_throttle_enable = THROTTLE_ENABLE_DEFAULT;
123 1.1 jmcneill #endif
124 1.1 jmcneill
125 1.1 jmcneill struct sunxi_thermal_sensor {
126 1.1 jmcneill const char *name;
127 1.1 jmcneill const char *desc;
128 1.1 jmcneill int init_alarm;
129 1.1 jmcneill int init_shut;
130 1.1 jmcneill };
131 1.1 jmcneill
132 1.1 jmcneill struct sunxi_thermal_config {
133 1.1 jmcneill struct sunxi_thermal_sensor sensors[MAX_SENSORS];
134 1.1 jmcneill int nsensors;
135 1.1 jmcneill uint64_t clk_rate;
136 1.1 jmcneill uint32_t adc_acquire_time;
137 1.1 jmcneill int adc_cali_en;
138 1.1 jmcneill uint32_t filter;
139 1.1 jmcneill uint32_t intc;
140 1.1 jmcneill int (*to_temp)(uint32_t);
141 1.1 jmcneill uint32_t (*to_reg)(int);
142 1.1 jmcneill int temp_base;
143 1.1 jmcneill int temp_mul;
144 1.1 jmcneill int temp_div;
145 1.1 jmcneill int calib0, calib1;
146 1.1 jmcneill uint32_t calib0_mask, calib1_mask;
147 1.1 jmcneill };
148 1.1 jmcneill
149 1.1 jmcneill static int
150 1.1 jmcneill a83t_to_temp(uint32_t val)
151 1.1 jmcneill {
152 1.1 jmcneill return ((A83T_TEMP_BASE - (val * A83T_TEMP_MUL)) / A83T_TEMP_DIV);
153 1.1 jmcneill }
154 1.1 jmcneill
155 1.1 jmcneill static const struct sunxi_thermal_config a83t_config = {
156 1.1 jmcneill .nsensors = 3,
157 1.1 jmcneill .sensors = {
158 1.1 jmcneill [0] = {
159 1.1 jmcneill .name = "cluster0",
160 1.1 jmcneill .desc = "CPU cluster 0 temperature",
161 1.1 jmcneill },
162 1.1 jmcneill [1] = {
163 1.1 jmcneill .name = "cluster1",
164 1.1 jmcneill .desc = "CPU cluster 1 temperature",
165 1.1 jmcneill },
166 1.1 jmcneill [2] = {
167 1.1 jmcneill .name = "gpu",
168 1.1 jmcneill .desc = "GPU temperature",
169 1.1 jmcneill },
170 1.1 jmcneill },
171 1.1 jmcneill .clk_rate = A83T_CLK_RATE,
172 1.1 jmcneill .adc_acquire_time = A83T_ADC_ACQUIRE_TIME,
173 1.1 jmcneill .adc_cali_en = 1,
174 1.1 jmcneill .filter = A83T_FILTER,
175 1.1 jmcneill .intc = A83T_INTC,
176 1.1 jmcneill .to_temp = a83t_to_temp,
177 1.1 jmcneill .calib0_mask = 0xffffffff,
178 1.1 jmcneill .calib1_mask = 0xffffffff,
179 1.1 jmcneill };
180 1.1 jmcneill
181 1.1 jmcneill static int
182 1.1 jmcneill a64_to_temp(uint32_t val)
183 1.1 jmcneill {
184 1.1 jmcneill return ((A64_TEMP_BASE - (val * A64_TEMP_MUL)) / A64_TEMP_DIV);
185 1.1 jmcneill }
186 1.1 jmcneill
187 1.1 jmcneill static const struct sunxi_thermal_config a64_config = {
188 1.1 jmcneill .nsensors = 3,
189 1.1 jmcneill .sensors = {
190 1.1 jmcneill [0] = {
191 1.1 jmcneill .name = "cpu",
192 1.1 jmcneill .desc = "CPU temperature",
193 1.1 jmcneill },
194 1.1 jmcneill [1] = {
195 1.1 jmcneill .name = "gpu1",
196 1.1 jmcneill .desc = "GPU temperature 1",
197 1.1 jmcneill },
198 1.1 jmcneill [2] = {
199 1.1 jmcneill .name = "gpu2",
200 1.1 jmcneill .desc = "GPU temperature 2",
201 1.1 jmcneill },
202 1.1 jmcneill },
203 1.1 jmcneill .clk_rate = A64_CLK_RATE,
204 1.1 jmcneill .adc_acquire_time = A64_ADC_ACQUIRE_TIME,
205 1.1 jmcneill .filter = A64_FILTER,
206 1.1 jmcneill .intc = A64_INTC,
207 1.1 jmcneill .to_temp = a64_to_temp,
208 1.1 jmcneill };
209 1.1 jmcneill
210 1.1 jmcneill static int
211 1.1 jmcneill h3_to_temp(uint32_t val)
212 1.1 jmcneill {
213 1.1 jmcneill return (H3_TEMP_BASE - ((val * H3_TEMP_MUL) / H3_TEMP_DIV));
214 1.1 jmcneill }
215 1.1 jmcneill
216 1.1 jmcneill static uint32_t
217 1.1 jmcneill h3_to_reg(int val)
218 1.1 jmcneill {
219 1.1 jmcneill return ((H3_TEMP_MINUS - (val * H3_TEMP_DIV)) / H3_TEMP_MUL);
220 1.1 jmcneill }
221 1.1 jmcneill
222 1.1 jmcneill static const struct sunxi_thermal_config h3_config = {
223 1.1 jmcneill .nsensors = 1,
224 1.1 jmcneill .sensors = {
225 1.1 jmcneill [0] = {
226 1.1 jmcneill .name = "cpu",
227 1.1 jmcneill .desc = "CPU temperature",
228 1.1 jmcneill .init_alarm = H3_INIT_ALARM,
229 1.1 jmcneill .init_shut = H3_INIT_SHUT,
230 1.1 jmcneill },
231 1.1 jmcneill },
232 1.1 jmcneill .clk_rate = H3_CLK_RATE,
233 1.1 jmcneill .adc_acquire_time = H3_ADC_ACQUIRE_TIME,
234 1.1 jmcneill .filter = H3_FILTER,
235 1.1 jmcneill .intc = H3_INTC,
236 1.1 jmcneill .to_temp = h3_to_temp,
237 1.1 jmcneill .to_reg = h3_to_reg,
238 1.1 jmcneill .calib0_mask = 0xfff,
239 1.1 jmcneill };
240 1.1 jmcneill
241 1.1 jmcneill static struct of_compat_data compat_data[] = {
242 1.1 jmcneill { "allwinner,sun8i-a83t-ts", (uintptr_t)&a83t_config },
243 1.1 jmcneill { "allwinner,sun8i-h3-ts", (uintptr_t)&h3_config },
244 1.1 jmcneill { "allwinner,sun50i-a64-ts", (uintptr_t)&a64_config },
245 1.1 jmcneill { NULL, (uintptr_t)NULL }
246 1.1 jmcneill };
247 1.1 jmcneill
248 1.1 jmcneill struct sunxi_thermal_softc {
249 1.1 jmcneill device_t dev;
250 1.1 jmcneill int phandle;
251 1.1 jmcneill bus_space_tag_t bst;
252 1.1 jmcneill bus_space_handle_t bsh;
253 1.1 jmcneill struct sunxi_thermal_config *conf;
254 1.1 jmcneill
255 1.1 jmcneill kmutex_t lock;
256 1.1 jmcneill callout_t tick;
257 1.1 jmcneill
258 1.1 jmcneill struct sysmon_envsys *sme;
259 1.1 jmcneill envsys_data_t data[MAX_SENSORS];
260 1.1 jmcneill };
261 1.1 jmcneill
262 1.1 jmcneill #define RD4(sc, reg) \
263 1.1 jmcneill bus_space_read_4((sc)->bst, (sc)->bsh, (reg))
264 1.1 jmcneill #define WR4(sc, reg, val) \
265 1.1 jmcneill bus_space_write_4((sc)->bst, (sc)->bsh, (reg), (val))
266 1.1 jmcneill
267 1.1 jmcneill static int
268 1.1 jmcneill sunxi_thermal_init(struct sunxi_thermal_softc *sc)
269 1.1 jmcneill {
270 1.1 jmcneill uint32_t calib[2];
271 1.1 jmcneill int error;
272 1.1 jmcneill
273 1.1 jmcneill if (sc->conf->calib0_mask != 0 || sc->conf->calib1_mask != 0) {
274 1.1 jmcneill /* Read calibration settings from SRAM */
275 1.1 jmcneill error = sunxi_sid_read_tscalib(calib);
276 1.1 jmcneill if (error != 0)
277 1.1 jmcneill return error;
278 1.1 jmcneill
279 1.1 jmcneill calib[0] &= sc->conf->calib0_mask;
280 1.1 jmcneill calib[1] &= sc->conf->calib1_mask;
281 1.1 jmcneill
282 1.1 jmcneill /* Write calibration settings to thermal controller */
283 1.1 jmcneill if (calib[0] != 0)
284 1.1 jmcneill WR4(sc, THS_CALIB0, calib[0]);
285 1.1 jmcneill if (calib[1] != 0)
286 1.1 jmcneill WR4(sc, THS_CALIB1, calib[1]);
287 1.1 jmcneill }
288 1.1 jmcneill
289 1.1 jmcneill /* Configure ADC acquire time (CLK_IN/(N+1)) and enable sensors */
290 1.1 jmcneill WR4(sc, THS_CTRL1, ADC_CALI_EN);
291 1.1 jmcneill WR4(sc, THS_CTRL0, sc->conf->adc_acquire_time);
292 1.1 jmcneill WR4(sc, THS_CTRL2, sc->conf->adc_acquire_time << SENSOR_ACQ1_SHIFT);
293 1.1 jmcneill
294 1.1 jmcneill /* Enable average filter */
295 1.1 jmcneill WR4(sc, THS_FILTER, sc->conf->filter);
296 1.1 jmcneill
297 1.1 jmcneill /* Enable interrupts */
298 1.1 jmcneill WR4(sc, THS_INTS, RD4(sc, THS_INTS));
299 1.1 jmcneill WR4(sc, THS_INTC, sc->conf->intc | SHUT_INT_ALL | ALARM_INT_ALL);
300 1.1 jmcneill
301 1.1 jmcneill /* Enable sensors */
302 1.1 jmcneill WR4(sc, THS_CTRL2, RD4(sc, THS_CTRL2) | SENSOR_ENABLE_ALL);
303 1.1 jmcneill
304 1.1 jmcneill return 0;
305 1.1 jmcneill }
306 1.1 jmcneill
307 1.1 jmcneill static int
308 1.1 jmcneill sunxi_thermal_gettemp(struct sunxi_thermal_softc *sc, int sensor)
309 1.1 jmcneill {
310 1.1 jmcneill uint32_t val;
311 1.1 jmcneill
312 1.1 jmcneill val = RD4(sc, THS_DATA0 + (sensor * 4));
313 1.1 jmcneill
314 1.1 jmcneill return sc->conf->to_temp(val);
315 1.1 jmcneill }
316 1.1 jmcneill
317 1.1 jmcneill static int
318 1.1 jmcneill sunxi_thermal_getshut(struct sunxi_thermal_softc *sc, int sensor)
319 1.1 jmcneill {
320 1.1 jmcneill uint32_t val;
321 1.1 jmcneill
322 1.1 jmcneill val = RD4(sc, THS_SHUTDOWN0_CTRL + (sensor * 4));
323 1.1 jmcneill val = (val >> SHUT_T_HOT_SHIFT) & SHUT_T_HOT_MASK;
324 1.1 jmcneill
325 1.1 jmcneill return sc->conf->to_temp(val);
326 1.1 jmcneill }
327 1.1 jmcneill
328 1.1 jmcneill static void
329 1.1 jmcneill sunxi_thermal_setshut(struct sunxi_thermal_softc *sc, int sensor, int temp)
330 1.1 jmcneill {
331 1.1 jmcneill uint32_t val;
332 1.1 jmcneill
333 1.1 jmcneill val = RD4(sc, THS_SHUTDOWN0_CTRL + (sensor * 4));
334 1.1 jmcneill val &= ~(SHUT_T_HOT_MASK << SHUT_T_HOT_SHIFT);
335 1.1 jmcneill val |= (sc->conf->to_reg(temp) << SHUT_T_HOT_SHIFT);
336 1.1 jmcneill WR4(sc, THS_SHUTDOWN0_CTRL + (sensor * 4), val);
337 1.1 jmcneill }
338 1.1 jmcneill
339 1.1 jmcneill static int
340 1.1 jmcneill sunxi_thermal_gethyst(struct sunxi_thermal_softc *sc, int sensor)
341 1.1 jmcneill {
342 1.1 jmcneill uint32_t val;
343 1.1 jmcneill
344 1.1 jmcneill val = RD4(sc, THS_ALARM0_CTRL + (sensor * 4));
345 1.1 jmcneill val = (val >> ALARM_T_HYST_SHIFT) & ALARM_T_HYST_MASK;
346 1.1 jmcneill
347 1.1 jmcneill return sc->conf->to_temp(val);
348 1.1 jmcneill }
349 1.1 jmcneill
350 1.1 jmcneill static int
351 1.1 jmcneill sunxi_thermal_getalarm(struct sunxi_thermal_softc *sc, int sensor)
352 1.1 jmcneill {
353 1.1 jmcneill uint32_t val;
354 1.1 jmcneill
355 1.1 jmcneill val = RD4(sc, THS_ALARM0_CTRL + (sensor * 4));
356 1.1 jmcneill val = (val >> ALARM_T_HOT_SHIFT) & ALARM_T_HOT_MASK;
357 1.1 jmcneill
358 1.1 jmcneill return sc->conf->to_temp(val);
359 1.1 jmcneill }
360 1.1 jmcneill
361 1.1 jmcneill static void
362 1.1 jmcneill sunxi_thermal_setalarm(struct sunxi_thermal_softc *sc, int sensor, int temp)
363 1.1 jmcneill {
364 1.1 jmcneill uint32_t val;
365 1.1 jmcneill
366 1.1 jmcneill val = RD4(sc, THS_ALARM0_CTRL + (sensor * 4));
367 1.1 jmcneill val &= ~(ALARM_T_HOT_MASK << ALARM_T_HOT_SHIFT);
368 1.1 jmcneill val |= (sc->conf->to_reg(temp) << ALARM_T_HOT_SHIFT);
369 1.1 jmcneill WR4(sc, THS_ALARM0_CTRL + (sensor * 4), val);
370 1.1 jmcneill }
371 1.1 jmcneill
372 1.1 jmcneill static void
373 1.1 jmcneill sunxi_thermal_task_shut(void *arg)
374 1.1 jmcneill {
375 1.1 jmcneill struct sunxi_thermal_softc * const sc = arg;
376 1.1 jmcneill
377 1.1 jmcneill device_printf(sc->dev,
378 1.1 jmcneill "WARNING - current temperature exceeds safe limits\n");
379 1.1 jmcneill
380 1.1 jmcneill cpu_reboot(RB_POWERDOWN, NULL);
381 1.1 jmcneill }
382 1.1 jmcneill
383 1.1 jmcneill static void
384 1.1 jmcneill sunxi_thermal_task_alarm(void *arg)
385 1.1 jmcneill {
386 1.1 jmcneill struct sunxi_thermal_softc * const sc = arg;
387 1.1 jmcneill
388 1.1 jmcneill const int alarm_val = sunxi_thermal_getalarm(sc, 0);
389 1.1 jmcneill const int temp_val = sunxi_thermal_gettemp(sc, 0);
390 1.1 jmcneill
391 1.1 jmcneill if (temp_val < alarm_val)
392 1.1 jmcneill pmf_event_inject(NULL, PMFE_THROTTLE_DISABLE);
393 1.1 jmcneill else
394 1.1 jmcneill callout_schedule(&sc->tick, hz);
395 1.1 jmcneill }
396 1.1 jmcneill
397 1.1 jmcneill static void
398 1.1 jmcneill sunxi_thermal_tick(void *arg)
399 1.1 jmcneill {
400 1.1 jmcneill struct sunxi_thermal_softc * const sc = arg;
401 1.1 jmcneill
402 1.1 jmcneill sysmon_task_queue_sched(0, sunxi_thermal_task_alarm, sc);
403 1.1 jmcneill }
404 1.1 jmcneill
405 1.1 jmcneill static int
406 1.1 jmcneill sunxi_thermal_intr(void *arg)
407 1.1 jmcneill {
408 1.1 jmcneill struct sunxi_thermal_softc * const sc = arg;
409 1.1 jmcneill uint32_t ints;
410 1.1 jmcneill
411 1.1 jmcneill mutex_enter(&sc->lock);
412 1.1 jmcneill
413 1.1 jmcneill ints = RD4(sc, THS_INTS);
414 1.1 jmcneill WR4(sc, THS_INTS, ints);
415 1.1 jmcneill
416 1.1 jmcneill if ((ints & SHUT_INT_ALL) != 0)
417 1.1 jmcneill sysmon_task_queue_sched(0, sunxi_thermal_task_shut, sc);
418 1.1 jmcneill
419 1.1 jmcneill if ((ints & ALARM_INT_ALL) != 0) {
420 1.1 jmcneill pmf_event_inject(NULL, PMFE_THROTTLE_ENABLE);
421 1.1 jmcneill sysmon_task_queue_sched(0, sunxi_thermal_task_alarm, sc);
422 1.1 jmcneill }
423 1.1 jmcneill
424 1.1 jmcneill mutex_exit(&sc->lock);
425 1.1 jmcneill
426 1.1 jmcneill return 1;
427 1.1 jmcneill }
428 1.1 jmcneill
429 1.1 jmcneill static void
430 1.1 jmcneill sunxi_thermal_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
431 1.1 jmcneill {
432 1.1 jmcneill struct sunxi_thermal_softc * const sc = sme->sme_cookie;
433 1.1 jmcneill
434 1.1 jmcneill const int64_t temp = sunxi_thermal_gettemp(sc, edata->private);
435 1.1 jmcneill
436 1.1 jmcneill edata->value_cur = temp * 1000000 + TEMP_C_TO_K;
437 1.1 jmcneill edata->state = ENVSYS_SVALID;
438 1.1 jmcneill }
439 1.1 jmcneill
440 1.1 jmcneill static int
441 1.1 jmcneill sunxi_thermal_init_clocks(struct sunxi_thermal_softc *sc)
442 1.1 jmcneill {
443 1.1 jmcneill struct fdtbus_reset *rst;
444 1.1 jmcneill struct clk *clk;
445 1.1 jmcneill int error;
446 1.1 jmcneill
447 1.1 jmcneill clk = fdtbus_clock_get(sc->phandle, "ahb");
448 1.1 jmcneill if (clk == NULL)
449 1.1 jmcneill return ENXIO;
450 1.1 jmcneill error = clk_enable(clk);
451 1.1 jmcneill if (error != 0)
452 1.1 jmcneill return error;
453 1.1 jmcneill
454 1.1 jmcneill clk = fdtbus_clock_get(sc->phandle, "ths");
455 1.1 jmcneill if (clk == NULL)
456 1.1 jmcneill return ENXIO;
457 1.1 jmcneill error = clk_set_rate(clk, sc->conf->clk_rate);
458 1.1 jmcneill if (error != 0)
459 1.1 jmcneill return error;
460 1.1 jmcneill error = clk_enable(clk);
461 1.1 jmcneill if (error != 0)
462 1.1 jmcneill return error;
463 1.1 jmcneill
464 1.1 jmcneill rst = fdtbus_reset_get_index(sc->phandle, 0);
465 1.1 jmcneill if (rst == NULL)
466 1.1 jmcneill return ENXIO;
467 1.1 jmcneill error = fdtbus_reset_deassert(rst);
468 1.1 jmcneill if (error != 0)
469 1.1 jmcneill return error;
470 1.1 jmcneill
471 1.1 jmcneill return 0;
472 1.1 jmcneill }
473 1.1 jmcneill
474 1.1 jmcneill static int
475 1.1 jmcneill sunxi_thermal_match(device_t parent, cfdata_t cf, void *aux)
476 1.1 jmcneill {
477 1.1 jmcneill struct fdt_attach_args * const faa = aux;
478 1.1 jmcneill
479 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
480 1.1 jmcneill }
481 1.1 jmcneill
482 1.1 jmcneill static void
483 1.1 jmcneill sunxi_thermal_attach(device_t parent, device_t self, void *aux)
484 1.1 jmcneill {
485 1.1 jmcneill struct sunxi_thermal_softc * const sc = device_private(self);
486 1.1 jmcneill struct fdt_attach_args * const faa = aux;
487 1.1 jmcneill const int phandle = faa->faa_phandle;
488 1.1 jmcneill char intrstr[128];
489 1.1 jmcneill bus_addr_t addr;
490 1.1 jmcneill bus_size_t size;
491 1.1 jmcneill void *ih;
492 1.1 jmcneill int i;
493 1.1 jmcneill
494 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
495 1.1 jmcneill aprint_error(": couldn't get registers\n");
496 1.1 jmcneill return;
497 1.1 jmcneill }
498 1.1 jmcneill
499 1.1 jmcneill sc->dev = self;
500 1.1 jmcneill sc->phandle = phandle;
501 1.1 jmcneill sc->bst = faa->faa_bst;
502 1.1 jmcneill sc->conf = (void *)of_search_compatible(phandle, compat_data)->data;
503 1.1 jmcneill if (bus_space_map(sc->bst, addr, size, 0, &sc->bsh) != 0) {
504 1.1 jmcneill aprint_error(": couldn't map registers\n");
505 1.1 jmcneill return;
506 1.1 jmcneill }
507 1.1 jmcneill mutex_init(&sc->lock, MUTEX_DEFAULT, IPL_VM);
508 1.1 jmcneill callout_init(&sc->tick, CALLOUT_MPSAFE);
509 1.1 jmcneill callout_setfunc(&sc->tick, sunxi_thermal_tick, sc);
510 1.1 jmcneill
511 1.1 jmcneill if (sunxi_thermal_init_clocks(sc) != 0) {
512 1.1 jmcneill aprint_error(": couldn't enable clocks\n");
513 1.1 jmcneill return;
514 1.1 jmcneill }
515 1.1 jmcneill
516 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
517 1.1 jmcneill aprint_error(": couldn't decode interrupt\n");
518 1.1 jmcneill return;
519 1.1 jmcneill }
520 1.1 jmcneill
521 1.1 jmcneill aprint_naive("\n");
522 1.1 jmcneill aprint_normal(": Thermal sensor controller\n");
523 1.1 jmcneill
524 1.1 jmcneill ih = fdtbus_intr_establish(phandle, 0, IPL_VM, FDT_INTR_MPSAFE,
525 1.1 jmcneill sunxi_thermal_intr, sc);
526 1.1 jmcneill if (ih == NULL) {
527 1.1 jmcneill aprint_error_dev(self, "couldn't establish interrupt on %s\n",
528 1.1 jmcneill intrstr);
529 1.1 jmcneill return;
530 1.1 jmcneill }
531 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
532 1.1 jmcneill
533 1.1 jmcneill for (i = 0; i < sc->conf->nsensors; i++) {
534 1.1 jmcneill if (sc->conf->sensors[i].init_alarm > 0)
535 1.1 jmcneill sunxi_thermal_setalarm(sc, i,
536 1.1 jmcneill sc->conf->sensors[i].init_alarm);
537 1.1 jmcneill if (sc->conf->sensors[i].init_shut > 0)
538 1.1 jmcneill sunxi_thermal_setshut(sc, i,
539 1.1 jmcneill sc->conf->sensors[i].init_shut);
540 1.1 jmcneill }
541 1.1 jmcneill
542 1.1 jmcneill if (sunxi_thermal_init(sc) != 0) {
543 1.1 jmcneill aprint_error_dev(self, "failed to initialize sensors\n");
544 1.1 jmcneill return;
545 1.1 jmcneill }
546 1.1 jmcneill
547 1.1 jmcneill sc->sme = sysmon_envsys_create();
548 1.1 jmcneill sc->sme->sme_name = device_xname(self);
549 1.1 jmcneill sc->sme->sme_cookie = sc;
550 1.1 jmcneill sc->sme->sme_refresh = sunxi_thermal_refresh;
551 1.1 jmcneill for (i = 0; i < sc->conf->nsensors; i++) {
552 1.1 jmcneill sc->data[i].private = i;
553 1.1 jmcneill sc->data[i].units = ENVSYS_STEMP;
554 1.1 jmcneill sc->data[i].state = ENVSYS_SINVALID;
555 1.1 jmcneill snprintf(sc->data[i].desc, sizeof(sc->data[i].desc),
556 1.1 jmcneill sc->conf->sensors[i].desc);
557 1.1 jmcneill sysmon_envsys_sensor_attach(sc->sme, &sc->data[i]);
558 1.1 jmcneill }
559 1.1 jmcneill sysmon_envsys_register(sc->sme);
560 1.1 jmcneill
561 1.1 jmcneill for (i = 0; i < sc->conf->nsensors; i++) {
562 1.1 jmcneill device_printf(self,
563 1.1 jmcneill "%s: alarm %dC hyst %dC shut %dC\n",
564 1.1 jmcneill sc->conf->sensors[i].name,
565 1.1 jmcneill sunxi_thermal_getalarm(sc, i),
566 1.1 jmcneill sunxi_thermal_gethyst(sc, i),
567 1.1 jmcneill sunxi_thermal_getshut(sc, i));
568 1.1 jmcneill }
569 1.1 jmcneill }
570 1.1 jmcneill
571 1.1 jmcneill CFATTACH_DECL_NEW(sunxi_thermal, sizeof(struct sunxi_thermal_softc),
572 1.1 jmcneill sunxi_thermal_match, sunxi_thermal_attach, NULL, NULL);
573