2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 * Allwinner thermal sensor controller
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/eventhandler.h>
41 #include <sys/kernel.h>
42 #include <sys/sysctl.h>
43 #include <sys/reboot.h>
44 #include <sys/module.h>
46 #include <sys/taskqueue.h>
47 #include <machine/bus.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
52 #include <dev/extres/clk/clk.h>
53 #include <dev/extres/hwreset/hwreset.h>
54 #include <dev/extres/nvmem/nvmem.h>
56 #include <arm/allwinner/aw_sid.h>
58 #include "cpufreq_if.h"
61 #define THS_CTRL0 0x00
62 #define THS_CTRL1 0x04
63 #define ADC_CALI_EN (1 << 17)
64 #define THS_CTRL2 0x40
65 #define SENSOR_ACQ1_SHIFT 16
66 #define SENSOR2_EN (1 << 2)
67 #define SENSOR1_EN (1 << 1)
68 #define SENSOR0_EN (1 << 0)
70 #define THS_THERMAL_PER_SHIFT 12
72 #define THS2_DATA_IRQ_STS (1 << 10)
73 #define THS1_DATA_IRQ_STS (1 << 9)
74 #define THS0_DATA_IRQ_STS (1 << 8)
75 #define SHUT_INT2_STS (1 << 6)
76 #define SHUT_INT1_STS (1 << 5)
77 #define SHUT_INT0_STS (1 << 4)
78 #define ALARM_INT2_STS (1 << 2)
79 #define ALARM_INT1_STS (1 << 1)
80 #define ALARM_INT0_STS (1 << 0)
81 #define THS_ALARM0_CTRL 0x50
82 #define ALARM_T_HOT_MASK 0xfff
83 #define ALARM_T_HOT_SHIFT 16
84 #define ALARM_T_HYST_MASK 0xfff
85 #define ALARM_T_HYST_SHIFT 0
86 #define THS_SHUTDOWN0_CTRL 0x60
87 #define SHUT_T_HOT_MASK 0xfff
88 #define SHUT_T_HOT_SHIFT 16
89 #define THS_FILTER 0x70
90 #define THS_CALIB0 0x74
91 #define THS_CALIB1 0x78
92 #define THS_DATA0 0x80
93 #define THS_DATA1 0x84
94 #define THS_DATA2 0x88
95 #define DATA_MASK 0xfff
97 #define A83T_CLK_RATE 24000000
98 #define A83T_ADC_ACQUIRE_TIME 23 /* 24Mhz/(23 + 1) = 1us */
99 #define A83T_THERMAL_PER 1 /* 4096 * (1 + 1) / 24Mhz = 341 us */
100 #define A83T_FILTER 0x5 /* Filter enabled, avg of 4 */
101 #define A83T_TEMP_BASE 2719000
102 #define A83T_TEMP_MUL 1000
103 #define A83T_TEMP_DIV 14186
105 #define A64_CLK_RATE 4000000
106 #define A64_ADC_ACQUIRE_TIME 400 /* 4Mhz/(400 + 1) = 100 us */
107 #define A64_THERMAL_PER 24 /* 4096 * (24 + 1) / 4Mhz = 25.6 ms */
108 #define A64_FILTER 0x6 /* Filter enabled, avg of 8 */
109 #define A64_TEMP_BASE 2170000
110 #define A64_TEMP_MUL 1000
111 #define A64_TEMP_DIV 8560
113 #define H3_CLK_RATE 4000000
114 #define H3_ADC_ACQUIRE_TIME 0x3f
115 #define H3_THERMAL_PER 401
116 #define H3_FILTER 0x6 /* Filter enabled, avg of 8 */
117 #define H3_TEMP_BASE 217
118 #define H3_TEMP_MUL 1000
119 #define H3_TEMP_DIV 8253
120 #define H3_TEMP_MINUS 1794000
121 #define H3_INIT_ALARM 90 /* degC */
122 #define H3_INIT_SHUT 105 /* degC */
124 #define H5_CLK_RATE 24000000
125 #define H5_ADC_ACQUIRE_TIME 479 /* 24Mhz/479 = 20us */
126 #define H5_THERMAL_PER 58 /* 4096 * (58 + 1) / 24Mhz = 10ms */
127 #define H5_FILTER 0x6 /* Filter enabled, avg of 8 */
128 #define H5_TEMP_BASE 233832448
129 #define H5_TEMP_MUL 124885
130 #define H5_TEMP_DIV 20
131 #define H5_TEMP_BASE_CPU 271581184
132 #define H5_TEMP_MUL_CPU 152253
133 #define H5_TEMP_BASE_GPU 289406976
134 #define H5_TEMP_MUL_GPU 166724
135 #define H5_INIT_CPU_ALARM 80 /* degC */
136 #define H5_INIT_CPU_SHUT 96 /* degC */
137 #define H5_INIT_GPU_ALARM 84 /* degC */
138 #define H5_INIT_GPU_SHUT 100 /* degC */
140 #define TEMP_C_TO_K 273
141 #define SENSOR_ENABLE_ALL (SENSOR0_EN|SENSOR1_EN|SENSOR2_EN)
142 #define SHUT_INT_ALL (SHUT_INT0_STS|SHUT_INT1_STS|SHUT_INT2_STS)
143 #define ALARM_INT_ALL (ALARM_INT0_STS)
145 #define MAX_SENSORS 3
146 #define MAX_CF_LEVELS 64
148 #define THROTTLE_ENABLE_DEFAULT 1
150 /* Enable thermal throttling */
151 static int aw_thermal_throttle_enable = THROTTLE_ENABLE_DEFAULT;
152 TUNABLE_INT("hw.aw_thermal.throttle_enable", &aw_thermal_throttle_enable);
154 struct aw_thermal_sensor {
161 struct aw_thermal_config {
162 struct aw_thermal_sensor sensors[MAX_SENSORS];
165 uint32_t adc_acquire_time;
168 uint32_t thermal_per;
169 int (*to_temp)(uint32_t, int);
170 uint32_t (*to_reg)(int, int);
175 uint32_t calib0_mask, calib1_mask;
179 a83t_to_temp(uint32_t val, int sensor)
181 return ((A83T_TEMP_BASE - (val * A83T_TEMP_MUL)) / A83T_TEMP_DIV);
184 static const struct aw_thermal_config a83t_config = {
189 .desc = "CPU cluster 0 temperature",
193 .desc = "CPU cluster 1 temperature",
197 .desc = "GPU temperature",
200 .clk_rate = A83T_CLK_RATE,
201 .adc_acquire_time = A83T_ADC_ACQUIRE_TIME,
203 .filter = A83T_FILTER,
204 .thermal_per = A83T_THERMAL_PER,
205 .to_temp = a83t_to_temp,
206 .calib0_mask = 0xffffffff,
207 .calib1_mask = 0xffff,
211 a64_to_temp(uint32_t val, int sensor)
213 return ((A64_TEMP_BASE - (val * A64_TEMP_MUL)) / A64_TEMP_DIV);
216 static const struct aw_thermal_config a64_config = {
221 .desc = "CPU temperature",
225 .desc = "GPU temperature 1",
229 .desc = "GPU temperature 2",
232 .clk_rate = A64_CLK_RATE,
233 .adc_acquire_time = A64_ADC_ACQUIRE_TIME,
235 .filter = A64_FILTER,
236 .thermal_per = A64_THERMAL_PER,
237 .to_temp = a64_to_temp,
238 .calib0_mask = 0xffffffff,
239 .calib1_mask = 0xffff,
243 h3_to_temp(uint32_t val, int sensor)
245 return (H3_TEMP_BASE - ((val * H3_TEMP_MUL) / H3_TEMP_DIV));
249 h3_to_reg(int val, int sensor)
251 return ((H3_TEMP_MINUS - (val * H3_TEMP_DIV)) / H3_TEMP_MUL);
254 static const struct aw_thermal_config h3_config = {
259 .desc = "CPU temperature",
260 .init_alarm = H3_INIT_ALARM,
261 .init_shut = H3_INIT_SHUT,
264 .clk_rate = H3_CLK_RATE,
265 .adc_acquire_time = H3_ADC_ACQUIRE_TIME,
268 .thermal_per = H3_THERMAL_PER,
269 .to_temp = h3_to_temp,
271 .calib0_mask = 0xffff,
275 h5_to_temp(uint32_t val, int sensor)
279 /* Temp is lower than 70 degrees */
281 tmp = H5_TEMP_BASE - (val * H5_TEMP_MUL);
287 tmp = H5_TEMP_BASE_CPU - (val * H5_TEMP_MUL_CPU);
288 else if (sensor == 1)
289 tmp = H5_TEMP_BASE_GPU - (val * H5_TEMP_MUL_GPU);
291 printf("Unknown sensor %d\n", sensor);
300 h5_to_reg(int val, int sensor)
305 tmp = H5_TEMP_BASE - (val << H5_TEMP_DIV);
309 tmp = H5_TEMP_BASE_CPU - (val << H5_TEMP_DIV);
310 tmp /= H5_TEMP_MUL_CPU;
311 } else if (sensor == 1) {
312 tmp = H5_TEMP_BASE_GPU - (val << H5_TEMP_DIV);
313 tmp /= H5_TEMP_MUL_GPU;
315 printf("Unknown sensor %d\n", sensor);
320 return ((uint32_t)tmp);
323 static const struct aw_thermal_config h5_config = {
328 .desc = "CPU temperature",
329 .init_alarm = H5_INIT_CPU_ALARM,
330 .init_shut = H5_INIT_CPU_SHUT,
334 .desc = "GPU temperature",
335 .init_alarm = H5_INIT_GPU_ALARM,
336 .init_shut = H5_INIT_GPU_SHUT,
339 .clk_rate = H5_CLK_RATE,
340 .adc_acquire_time = H5_ADC_ACQUIRE_TIME,
342 .thermal_per = H5_THERMAL_PER,
343 .to_temp = h5_to_temp,
345 .calib0_mask = 0xffffffff,
348 static struct ofw_compat_data compat_data[] = {
349 { "allwinner,sun8i-a83t-ths", (uintptr_t)&a83t_config },
350 { "allwinner,sun8i-h3-ths", (uintptr_t)&h3_config },
351 { "allwinner,sun50i-a64-ths", (uintptr_t)&a64_config },
352 { "allwinner,sun50i-h5-ths", (uintptr_t)&h5_config },
353 { NULL, (uintptr_t)NULL }
356 #define THS_CONF(d) \
357 (void *)ofw_bus_search_compatible((d), compat_data)->ocd_data
359 struct aw_thermal_softc {
361 struct resource *res[2];
362 struct aw_thermal_config *conf;
367 struct cf_level levels[MAX_CF_LEVELS];
368 eventhandler_tag cf_pre_tag;
374 static struct resource_spec aw_thermal_spec[] = {
375 { SYS_RES_MEMORY, 0, RF_ACTIVE },
376 { SYS_RES_IRQ, 0, RF_ACTIVE },
380 #define RD4(sc, reg) bus_read_4((sc)->res[0], (reg))
381 #define WR4(sc, reg, val) bus_write_4((sc)->res[0], (reg), (val))
384 aw_thermal_init(struct aw_thermal_softc *sc)
390 node = ofw_bus_get_node(sc->dev);
391 if (nvmem_get_cell_len(node, "ths-calib") > sizeof(calib)) {
392 device_printf(sc->dev, "ths-calib nvmem cell is too large\n");
395 error = nvmem_read_cell_by_name(node, "ths-calib",
396 (void *)&calib, nvmem_get_cell_len(node, "ths-calib"));
397 /* Read calibration settings from EFUSE */
399 device_printf(sc->dev, "Cannot read THS efuse\n");
403 calib[0] &= sc->conf->calib0_mask;
404 calib[1] &= sc->conf->calib1_mask;
406 /* Write calibration settings to thermal controller */
408 WR4(sc, THS_CALIB0, calib[0]);
410 WR4(sc, THS_CALIB1, calib[1]);
412 /* Configure ADC acquire time (CLK_IN/(N+1)) and enable sensors */
413 WR4(sc, THS_CTRL1, ADC_CALI_EN);
414 WR4(sc, THS_CTRL0, sc->conf->adc_acquire_time);
415 WR4(sc, THS_CTRL2, sc->conf->adc_acquire_time << SENSOR_ACQ1_SHIFT);
417 /* Set thermal period */
418 WR4(sc, THS_INTC, sc->conf->thermal_per << THS_THERMAL_PER_SHIFT);
420 /* Enable average filter */
421 WR4(sc, THS_FILTER, sc->conf->filter);
423 /* Enable interrupts */
424 WR4(sc, THS_INTS, RD4(sc, THS_INTS));
425 WR4(sc, THS_INTC, RD4(sc, THS_INTC) | SHUT_INT_ALL | ALARM_INT_ALL);
428 WR4(sc, THS_CTRL2, RD4(sc, THS_CTRL2) | SENSOR_ENABLE_ALL);
434 aw_thermal_gettemp(struct aw_thermal_softc *sc, int sensor)
438 val = RD4(sc, THS_DATA0 + (sensor * 4));
440 return (sc->conf->to_temp(val, sensor));
444 aw_thermal_getshut(struct aw_thermal_softc *sc, int sensor)
448 val = RD4(sc, THS_SHUTDOWN0_CTRL + (sensor * 4));
449 val = (val >> SHUT_T_HOT_SHIFT) & SHUT_T_HOT_MASK;
451 return (sc->conf->to_temp(val, sensor));
455 aw_thermal_setshut(struct aw_thermal_softc *sc, int sensor, int temp)
459 val = RD4(sc, THS_SHUTDOWN0_CTRL + (sensor * 4));
460 val &= ~(SHUT_T_HOT_MASK << SHUT_T_HOT_SHIFT);
461 val |= (sc->conf->to_reg(temp, sensor) << SHUT_T_HOT_SHIFT);
462 WR4(sc, THS_SHUTDOWN0_CTRL + (sensor * 4), val);
466 aw_thermal_gethyst(struct aw_thermal_softc *sc, int sensor)
470 val = RD4(sc, THS_ALARM0_CTRL + (sensor * 4));
471 val = (val >> ALARM_T_HYST_SHIFT) & ALARM_T_HYST_MASK;
473 return (sc->conf->to_temp(val, sensor));
477 aw_thermal_getalarm(struct aw_thermal_softc *sc, int sensor)
481 val = RD4(sc, THS_ALARM0_CTRL + (sensor * 4));
482 val = (val >> ALARM_T_HOT_SHIFT) & ALARM_T_HOT_MASK;
484 return (sc->conf->to_temp(val, sensor));
488 aw_thermal_setalarm(struct aw_thermal_softc *sc, int sensor, int temp)
492 val = RD4(sc, THS_ALARM0_CTRL + (sensor * 4));
493 val &= ~(ALARM_T_HOT_MASK << ALARM_T_HOT_SHIFT);
494 val |= (sc->conf->to_reg(temp, sensor) << ALARM_T_HOT_SHIFT);
495 WR4(sc, THS_ALARM0_CTRL + (sensor * 4), val);
499 aw_thermal_sysctl(SYSCTL_HANDLER_ARGS)
501 struct aw_thermal_softc *sc;
507 val = aw_thermal_gettemp(sc, sensor) + TEMP_C_TO_K;
509 return sysctl_handle_opaque(oidp, &val, sizeof(val), req);
513 aw_thermal_throttle(struct aw_thermal_softc *sc, int enable)
518 if (enable == sc->throttle)
522 /* Set the lowest available frequency */
523 cf_dev = devclass_get_device(devclass_find("cpufreq"), 0);
526 count = MAX_CF_LEVELS;
527 error = CPUFREQ_LEVELS(cf_dev, sc->levels, &count);
528 if (error != 0 || count == 0)
530 sc->min_freq = sc->levels[count - 1].total_set.freq;
531 error = CPUFREQ_SET(cf_dev, &sc->levels[count - 1],
537 sc->throttle = enable;
541 aw_thermal_cf_task(void *arg, int pending)
543 struct aw_thermal_softc *sc;
547 aw_thermal_throttle(sc, 1);
551 aw_thermal_cf_pre_change(void *arg, const struct cf_level *level, int *status)
553 struct aw_thermal_softc *sc;
554 int temp_cur, temp_alarm;
558 if (aw_thermal_throttle_enable == 0 || sc->throttle == 0 ||
559 level->total_set.freq == sc->min_freq)
562 temp_cur = aw_thermal_gettemp(sc, 0);
563 temp_alarm = aw_thermal_getalarm(sc, 0);
565 if (temp_cur < temp_alarm)
566 aw_thermal_throttle(sc, 0);
572 aw_thermal_intr(void *arg)
574 struct aw_thermal_softc *sc;
579 sc = device_get_softc(dev);
581 ints = RD4(sc, THS_INTS);
582 WR4(sc, THS_INTS, ints);
584 if ((ints & SHUT_INT_ALL) != 0) {
586 "WARNING - current temperature exceeds safe limits\n");
587 shutdown_nice(RB_POWEROFF);
590 if ((ints & ALARM_INT_ALL) != 0)
591 taskqueue_enqueue(taskqueue_thread, &sc->cf_task);
595 aw_thermal_probe(device_t dev)
597 if (!ofw_bus_status_okay(dev))
600 if (THS_CONF(dev) == NULL)
603 device_set_desc(dev, "Allwinner Thermal Sensor Controller");
604 return (BUS_PROBE_DEFAULT);
608 aw_thermal_attach(device_t dev)
610 struct aw_thermal_softc *sc;
615 sc = device_get_softc(dev);
620 sc->conf = THS_CONF(dev);
621 TASK_INIT(&sc->cf_task, 0, aw_thermal_cf_task, sc);
623 if (bus_alloc_resources(dev, aw_thermal_spec, sc->res) != 0) {
624 device_printf(dev, "cannot allocate resources for device\n");
628 if (clk_get_by_ofw_name(dev, 0, "apb", &sc->clk_apb) == 0) {
629 error = clk_enable(sc->clk_apb);
631 device_printf(dev, "cannot enable apb clock\n");
636 if (clk_get_by_ofw_name(dev, 0, "ths", &sc->clk_ths) == 0) {
637 error = clk_set_freq(sc->clk_ths, sc->conf->clk_rate, 0);
639 device_printf(dev, "cannot set ths clock rate\n");
642 error = clk_enable(sc->clk_ths);
644 device_printf(dev, "cannot enable ths clock\n");
649 if (hwreset_get_by_ofw_idx(dev, 0, 0, &rst) == 0) {
650 error = hwreset_deassert(rst);
652 device_printf(dev, "cannot de-assert reset\n");
657 error = bus_setup_intr(dev, sc->res[1], INTR_TYPE_MISC | INTR_MPSAFE,
658 NULL, aw_thermal_intr, dev, &ih);
660 device_printf(dev, "cannot setup interrupt handler\n");
664 for (i = 0; i < sc->conf->nsensors; i++) {
665 if (sc->conf->sensors[i].init_alarm > 0)
666 aw_thermal_setalarm(sc, i,
667 sc->conf->sensors[i].init_alarm);
668 if (sc->conf->sensors[i].init_shut > 0)
669 aw_thermal_setshut(sc, i,
670 sc->conf->sensors[i].init_shut);
673 if (aw_thermal_init(sc) != 0)
676 for (i = 0; i < sc->conf->nsensors; i++)
677 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
678 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
679 OID_AUTO, sc->conf->sensors[i].name,
680 CTLTYPE_INT | CTLFLAG_RD,
681 sc, i, aw_thermal_sysctl, "IK0",
682 sc->conf->sensors[i].desc);
685 for (i = 0; i < sc->conf->nsensors; i++) {
687 "%s: alarm %dC hyst %dC shut %dC\n",
688 sc->conf->sensors[i].name,
689 aw_thermal_getalarm(sc, i),
690 aw_thermal_gethyst(sc, i),
691 aw_thermal_getshut(sc, i));
694 sc->cf_pre_tag = EVENTHANDLER_REGISTER(cpufreq_pre_change,
695 aw_thermal_cf_pre_change, sc, EVENTHANDLER_PRI_FIRST);
701 bus_teardown_intr(dev, sc->res[1], ih);
703 hwreset_release(rst);
704 if (sc->clk_apb != NULL)
705 clk_release(sc->clk_apb);
706 if (sc->clk_ths != NULL)
707 clk_release(sc->clk_ths);
708 bus_release_resources(dev, aw_thermal_spec, sc->res);
713 static device_method_t aw_thermal_methods[] = {
714 /* Device interface */
715 DEVMETHOD(device_probe, aw_thermal_probe),
716 DEVMETHOD(device_attach, aw_thermal_attach),
721 static driver_t aw_thermal_driver = {
724 sizeof(struct aw_thermal_softc),
727 static devclass_t aw_thermal_devclass;
729 DRIVER_MODULE(aw_thermal, simplebus, aw_thermal_driver, aw_thermal_devclass,
731 MODULE_VERSION(aw_thermal, 1);
732 MODULE_DEPEND(aw_thermal, aw_sid, 1, 1, 1);
733 SIMPLEBUS_PNP_INFO(compat_data);