Home | History | Annotate | Line # | Download | only in sunxi
sunxi_thermal.c revision 1.6
      1  1.6  bsiegert /* $NetBSD: sunxi_thermal.c,v 1.6 2018/08/21 14:09:41 bsiegert 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.6  bsiegert __KERNEL_RCSID(0, "$NetBSD: sunxi_thermal.c,v 1.6 2018/08/21 14:09:41 bsiegert 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.1  jmcneill static struct of_compat_data compat_data[] = {
    309  1.1  jmcneill 	{ "allwinner,sun8i-a83t-ts",	(uintptr_t)&a83t_config },
    310  1.1  jmcneill 	{ "allwinner,sun8i-h3-ts",	(uintptr_t)&h3_config },
    311  1.1  jmcneill 	{ "allwinner,sun50i-a64-ts",	(uintptr_t)&a64_config },
    312  1.5  jmcneill 	{ "allwinner,sun50i-h5-ts",	(uintptr_t)&h5_config },
    313  1.1  jmcneill 	{ NULL,				(uintptr_t)NULL }
    314  1.1  jmcneill };
    315  1.1  jmcneill 
    316  1.1  jmcneill struct sunxi_thermal_softc {
    317  1.1  jmcneill 	device_t			dev;
    318  1.1  jmcneill 	int				phandle;
    319  1.1  jmcneill 	bus_space_tag_t			bst;
    320  1.1  jmcneill 	bus_space_handle_t		bsh;
    321  1.1  jmcneill 	struct sunxi_thermal_config	*conf;
    322  1.1  jmcneill 
    323  1.1  jmcneill 	kmutex_t			lock;
    324  1.1  jmcneill 	callout_t			tick;
    325  1.1  jmcneill 
    326  1.1  jmcneill 	struct sysmon_envsys		*sme;
    327  1.1  jmcneill 	envsys_data_t			data[MAX_SENSORS];
    328  1.1  jmcneill };
    329  1.1  jmcneill 
    330  1.1  jmcneill #define	RD4(sc, reg)		\
    331  1.1  jmcneill 	bus_space_read_4((sc)->bst, (sc)->bsh, (reg))
    332  1.1  jmcneill #define	WR4(sc, reg, val)	\
    333  1.1  jmcneill 	bus_space_write_4((sc)->bst, (sc)->bsh, (reg), (val))
    334  1.1  jmcneill 
    335  1.1  jmcneill static int
    336  1.1  jmcneill sunxi_thermal_init(struct sunxi_thermal_softc *sc)
    337  1.1  jmcneill {
    338  1.1  jmcneill 	uint32_t calib[2];
    339  1.1  jmcneill 	int error;
    340  1.1  jmcneill 
    341  1.1  jmcneill 	if (sc->conf->calib0_mask != 0 || sc->conf->calib1_mask != 0) {
    342  1.1  jmcneill 		/* Read calibration settings from SRAM */
    343  1.1  jmcneill 		error = sunxi_sid_read_tscalib(calib);
    344  1.1  jmcneill 		if (error != 0)
    345  1.1  jmcneill 			return error;
    346  1.1  jmcneill 
    347  1.1  jmcneill 		calib[0] &= sc->conf->calib0_mask;
    348  1.1  jmcneill 		calib[1] &= sc->conf->calib1_mask;
    349  1.1  jmcneill 
    350  1.1  jmcneill 		/* Write calibration settings to thermal controller */
    351  1.1  jmcneill 		if (calib[0] != 0)
    352  1.1  jmcneill 			WR4(sc, THS_CALIB0, calib[0]);
    353  1.1  jmcneill 		if (calib[1] != 0)
    354  1.1  jmcneill 			WR4(sc, THS_CALIB1, calib[1]);
    355  1.1  jmcneill 	}
    356  1.1  jmcneill 
    357  1.1  jmcneill 	/* Configure ADC acquire time (CLK_IN/(N+1)) and enable sensors */
    358  1.1  jmcneill 	WR4(sc, THS_CTRL1, ADC_CALI_EN);
    359  1.1  jmcneill 	WR4(sc, THS_CTRL0, sc->conf->adc_acquire_time);
    360  1.1  jmcneill 	WR4(sc, THS_CTRL2, sc->conf->adc_acquire_time << SENSOR_ACQ1_SHIFT);
    361  1.1  jmcneill 
    362  1.1  jmcneill 	/* Enable average filter */
    363  1.1  jmcneill 	WR4(sc, THS_FILTER, sc->conf->filter);
    364  1.1  jmcneill 
    365  1.1  jmcneill 	/* Enable interrupts */
    366  1.1  jmcneill 	WR4(sc, THS_INTS, RD4(sc, THS_INTS));
    367  1.1  jmcneill 	WR4(sc, THS_INTC, sc->conf->intc | SHUT_INT_ALL | ALARM_INT_ALL);
    368  1.1  jmcneill 
    369  1.1  jmcneill 	/* Enable sensors */
    370  1.1  jmcneill 	WR4(sc, THS_CTRL2, RD4(sc, THS_CTRL2) | SENSOR_ENABLE_ALL);
    371  1.1  jmcneill 
    372  1.1  jmcneill 	return 0;
    373  1.1  jmcneill }
    374  1.1  jmcneill 
    375  1.1  jmcneill static int
    376  1.1  jmcneill sunxi_thermal_gettemp(struct sunxi_thermal_softc *sc, int sensor)
    377  1.1  jmcneill {
    378  1.1  jmcneill 	uint32_t val;
    379  1.1  jmcneill 
    380  1.1  jmcneill 	val = RD4(sc, THS_DATA0 + (sensor * 4));
    381  1.1  jmcneill 
    382  1.5  jmcneill 	return sc->conf->to_temp(sensor, val);
    383  1.1  jmcneill }
    384  1.1  jmcneill 
    385  1.1  jmcneill static int
    386  1.1  jmcneill sunxi_thermal_getshut(struct sunxi_thermal_softc *sc, int sensor)
    387  1.1  jmcneill {
    388  1.1  jmcneill 	uint32_t val;
    389  1.1  jmcneill 
    390  1.1  jmcneill 	val = RD4(sc, THS_SHUTDOWN0_CTRL + (sensor * 4));
    391  1.1  jmcneill 	val = (val >> SHUT_T_HOT_SHIFT) & SHUT_T_HOT_MASK;
    392  1.1  jmcneill 
    393  1.5  jmcneill 	return sc->conf->to_temp(sensor, val);
    394  1.1  jmcneill }
    395  1.1  jmcneill 
    396  1.1  jmcneill static void
    397  1.1  jmcneill sunxi_thermal_setshut(struct sunxi_thermal_softc *sc, int sensor, int temp)
    398  1.1  jmcneill {
    399  1.1  jmcneill 	uint32_t val;
    400  1.1  jmcneill 
    401  1.1  jmcneill 	val = RD4(sc, THS_SHUTDOWN0_CTRL + (sensor * 4));
    402  1.1  jmcneill 	val &= ~(SHUT_T_HOT_MASK << SHUT_T_HOT_SHIFT);
    403  1.5  jmcneill 	val |= (sc->conf->to_reg(sensor, temp) << SHUT_T_HOT_SHIFT);
    404  1.1  jmcneill 	WR4(sc, THS_SHUTDOWN0_CTRL + (sensor * 4), val);
    405  1.1  jmcneill }
    406  1.1  jmcneill 
    407  1.1  jmcneill static int
    408  1.1  jmcneill sunxi_thermal_gethyst(struct sunxi_thermal_softc *sc, int sensor)
    409  1.1  jmcneill {
    410  1.1  jmcneill 	uint32_t val;
    411  1.1  jmcneill 
    412  1.1  jmcneill 	val = RD4(sc, THS_ALARM0_CTRL + (sensor * 4));
    413  1.1  jmcneill 	val = (val >> ALARM_T_HYST_SHIFT) & ALARM_T_HYST_MASK;
    414  1.1  jmcneill 
    415  1.5  jmcneill 	return sc->conf->to_temp(sensor, val);
    416  1.1  jmcneill }
    417  1.1  jmcneill 
    418  1.1  jmcneill static int
    419  1.1  jmcneill sunxi_thermal_getalarm(struct sunxi_thermal_softc *sc, int sensor)
    420  1.1  jmcneill {
    421  1.1  jmcneill 	uint32_t val;
    422  1.1  jmcneill 
    423  1.1  jmcneill 	val = RD4(sc, THS_ALARM0_CTRL + (sensor * 4));
    424  1.1  jmcneill 	val = (val >> ALARM_T_HOT_SHIFT) & ALARM_T_HOT_MASK;
    425  1.1  jmcneill 
    426  1.5  jmcneill 	return sc->conf->to_temp(sensor, val);
    427  1.1  jmcneill }
    428  1.1  jmcneill 
    429  1.1  jmcneill static void
    430  1.1  jmcneill sunxi_thermal_setalarm(struct sunxi_thermal_softc *sc, int sensor, int temp)
    431  1.1  jmcneill {
    432  1.1  jmcneill 	uint32_t val;
    433  1.1  jmcneill 
    434  1.1  jmcneill 	val = RD4(sc, THS_ALARM0_CTRL + (sensor * 4));
    435  1.1  jmcneill 	val &= ~(ALARM_T_HOT_MASK << ALARM_T_HOT_SHIFT);
    436  1.5  jmcneill 	val |= (sc->conf->to_reg(sensor, temp) << ALARM_T_HOT_SHIFT);
    437  1.1  jmcneill 	WR4(sc, THS_ALARM0_CTRL + (sensor * 4), val);
    438  1.1  jmcneill }
    439  1.1  jmcneill 
    440  1.1  jmcneill static void
    441  1.1  jmcneill sunxi_thermal_task_shut(void *arg)
    442  1.1  jmcneill {
    443  1.1  jmcneill 	struct sunxi_thermal_softc * const sc = arg;
    444  1.1  jmcneill 
    445  1.1  jmcneill 	device_printf(sc->dev,
    446  1.1  jmcneill 	    "WARNING - current temperature exceeds safe limits\n");
    447  1.1  jmcneill 
    448  1.1  jmcneill 	cpu_reboot(RB_POWERDOWN, NULL);
    449  1.1  jmcneill }
    450  1.1  jmcneill 
    451  1.1  jmcneill static void
    452  1.1  jmcneill sunxi_thermal_task_alarm(void *arg)
    453  1.1  jmcneill {
    454  1.1  jmcneill 	struct sunxi_thermal_softc * const sc = arg;
    455  1.1  jmcneill 
    456  1.1  jmcneill 	const int alarm_val = sunxi_thermal_getalarm(sc, 0);
    457  1.1  jmcneill 	const int temp_val = sunxi_thermal_gettemp(sc, 0);
    458  1.1  jmcneill 
    459  1.1  jmcneill 	if (temp_val < alarm_val)
    460  1.1  jmcneill 		pmf_event_inject(NULL, PMFE_THROTTLE_DISABLE);
    461  1.1  jmcneill 	else
    462  1.1  jmcneill 		callout_schedule(&sc->tick, hz);
    463  1.1  jmcneill }
    464  1.1  jmcneill 
    465  1.1  jmcneill static void
    466  1.1  jmcneill sunxi_thermal_tick(void *arg)
    467  1.1  jmcneill {
    468  1.1  jmcneill 	struct sunxi_thermal_softc * const sc = arg;
    469  1.1  jmcneill 
    470  1.1  jmcneill 	sysmon_task_queue_sched(0, sunxi_thermal_task_alarm, sc);
    471  1.1  jmcneill }
    472  1.1  jmcneill 
    473  1.1  jmcneill static int
    474  1.1  jmcneill sunxi_thermal_intr(void *arg)
    475  1.1  jmcneill {
    476  1.1  jmcneill 	struct sunxi_thermal_softc * const sc = arg;
    477  1.1  jmcneill 	uint32_t ints;
    478  1.1  jmcneill 
    479  1.1  jmcneill 	mutex_enter(&sc->lock);
    480  1.1  jmcneill 
    481  1.1  jmcneill 	ints = RD4(sc, THS_INTS);
    482  1.1  jmcneill 	WR4(sc, THS_INTS, ints);
    483  1.1  jmcneill 
    484  1.1  jmcneill 	if ((ints & SHUT_INT_ALL) != 0)
    485  1.1  jmcneill 		sysmon_task_queue_sched(0, sunxi_thermal_task_shut, sc);
    486  1.1  jmcneill 
    487  1.1  jmcneill 	if ((ints & ALARM_INT_ALL) != 0) {
    488  1.1  jmcneill 		pmf_event_inject(NULL, PMFE_THROTTLE_ENABLE);
    489  1.1  jmcneill 		sysmon_task_queue_sched(0, sunxi_thermal_task_alarm, sc);
    490  1.1  jmcneill 	}
    491  1.1  jmcneill 
    492  1.1  jmcneill 	mutex_exit(&sc->lock);
    493  1.1  jmcneill 
    494  1.1  jmcneill 	return 1;
    495  1.1  jmcneill }
    496  1.1  jmcneill 
    497  1.1  jmcneill static void
    498  1.1  jmcneill sunxi_thermal_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    499  1.1  jmcneill {
    500  1.1  jmcneill 	struct sunxi_thermal_softc * const sc = sme->sme_cookie;
    501  1.1  jmcneill 
    502  1.1  jmcneill 	const int64_t temp = sunxi_thermal_gettemp(sc, edata->private);
    503  1.1  jmcneill 
    504  1.1  jmcneill 	edata->value_cur = temp * 1000000 + TEMP_C_TO_K;
    505  1.1  jmcneill 	edata->state = ENVSYS_SVALID;
    506  1.1  jmcneill }
    507  1.1  jmcneill 
    508  1.1  jmcneill static int
    509  1.1  jmcneill sunxi_thermal_init_clocks(struct sunxi_thermal_softc *sc)
    510  1.1  jmcneill {
    511  1.1  jmcneill 	struct fdtbus_reset *rst;
    512  1.1  jmcneill 	struct clk *clk;
    513  1.1  jmcneill 	int error;
    514  1.1  jmcneill 
    515  1.1  jmcneill 	clk = fdtbus_clock_get(sc->phandle, "ahb");
    516  1.1  jmcneill 	if (clk == NULL)
    517  1.1  jmcneill 		return ENXIO;
    518  1.1  jmcneill 	error = clk_enable(clk);
    519  1.1  jmcneill 	if (error != 0)
    520  1.1  jmcneill 		return error;
    521  1.1  jmcneill 
    522  1.1  jmcneill 	clk = fdtbus_clock_get(sc->phandle, "ths");
    523  1.1  jmcneill 	if (clk == NULL)
    524  1.1  jmcneill 		return ENXIO;
    525  1.1  jmcneill 	error = clk_set_rate(clk, sc->conf->clk_rate);
    526  1.1  jmcneill 	if (error != 0)
    527  1.1  jmcneill 		return error;
    528  1.1  jmcneill 	error = clk_enable(clk);
    529  1.1  jmcneill 	if (error != 0)
    530  1.1  jmcneill 		return error;
    531  1.1  jmcneill 
    532  1.1  jmcneill 	rst = fdtbus_reset_get_index(sc->phandle, 0);
    533  1.1  jmcneill 	if (rst == NULL)
    534  1.1  jmcneill 		return ENXIO;
    535  1.1  jmcneill 	error = fdtbus_reset_deassert(rst);
    536  1.1  jmcneill 	if (error != 0)
    537  1.1  jmcneill 		return error;
    538  1.1  jmcneill 
    539  1.1  jmcneill 	return 0;
    540  1.1  jmcneill }
    541  1.1  jmcneill 
    542  1.1  jmcneill static int
    543  1.1  jmcneill sunxi_thermal_match(device_t parent, cfdata_t cf, void *aux)
    544  1.1  jmcneill {
    545  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    546  1.1  jmcneill 
    547  1.1  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    548  1.1  jmcneill }
    549  1.1  jmcneill 
    550  1.1  jmcneill static void
    551  1.1  jmcneill sunxi_thermal_attach(device_t parent, device_t self, void *aux)
    552  1.1  jmcneill {
    553  1.1  jmcneill 	struct sunxi_thermal_softc * const sc = device_private(self);
    554  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    555  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    556  1.1  jmcneill 	char intrstr[128];
    557  1.1  jmcneill 	bus_addr_t addr;
    558  1.1  jmcneill 	bus_size_t size;
    559  1.1  jmcneill 	void *ih;
    560  1.1  jmcneill 	int i;
    561  1.1  jmcneill 
    562  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    563  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    564  1.1  jmcneill 		return;
    565  1.1  jmcneill 	}
    566  1.1  jmcneill 
    567  1.1  jmcneill 	sc->dev = self;
    568  1.1  jmcneill 	sc->phandle = phandle;
    569  1.1  jmcneill 	sc->bst = faa->faa_bst;
    570  1.1  jmcneill 	sc->conf = (void *)of_search_compatible(phandle, compat_data)->data;
    571  1.1  jmcneill 	if (bus_space_map(sc->bst, addr, size, 0, &sc->bsh) != 0) {
    572  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    573  1.1  jmcneill 		return;
    574  1.1  jmcneill 	}
    575  1.1  jmcneill 	mutex_init(&sc->lock, MUTEX_DEFAULT, IPL_VM);
    576  1.1  jmcneill 	callout_init(&sc->tick, CALLOUT_MPSAFE);
    577  1.1  jmcneill 	callout_setfunc(&sc->tick, sunxi_thermal_tick, sc);
    578  1.1  jmcneill 
    579  1.1  jmcneill 	if (sunxi_thermal_init_clocks(sc) != 0) {
    580  1.1  jmcneill 		aprint_error(": couldn't enable clocks\n");
    581  1.1  jmcneill 		return;
    582  1.1  jmcneill 	}
    583  1.1  jmcneill 
    584  1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    585  1.1  jmcneill 		aprint_error(": couldn't decode interrupt\n");
    586  1.1  jmcneill 		return;
    587  1.1  jmcneill 	}
    588  1.1  jmcneill 
    589  1.1  jmcneill 	aprint_naive("\n");
    590  1.1  jmcneill 	aprint_normal(": Thermal sensor controller\n");
    591  1.1  jmcneill 
    592  1.1  jmcneill 	ih = fdtbus_intr_establish(phandle, 0, IPL_VM, FDT_INTR_MPSAFE,
    593  1.1  jmcneill 	    sunxi_thermal_intr, sc);
    594  1.1  jmcneill 	if (ih == NULL) {
    595  1.1  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    596  1.1  jmcneill 		    intrstr);
    597  1.1  jmcneill 		return;
    598  1.1  jmcneill 	}
    599  1.1  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    600  1.1  jmcneill 
    601  1.1  jmcneill 	for (i = 0; i < sc->conf->nsensors; i++) {
    602  1.1  jmcneill 		if (sc->conf->sensors[i].init_alarm > 0)
    603  1.1  jmcneill 			sunxi_thermal_setalarm(sc, i,
    604  1.1  jmcneill 			    sc->conf->sensors[i].init_alarm);
    605  1.1  jmcneill 		if (sc->conf->sensors[i].init_shut > 0)
    606  1.1  jmcneill 			sunxi_thermal_setshut(sc, i,
    607  1.1  jmcneill 			    sc->conf->sensors[i].init_shut);
    608  1.1  jmcneill 	}
    609  1.1  jmcneill 
    610  1.1  jmcneill 	if (sunxi_thermal_init(sc) != 0) {
    611  1.1  jmcneill 		aprint_error_dev(self, "failed to initialize sensors\n");
    612  1.1  jmcneill 		return;
    613  1.1  jmcneill 	}
    614  1.1  jmcneill 
    615  1.1  jmcneill 	sc->sme = sysmon_envsys_create();
    616  1.1  jmcneill 	sc->sme->sme_name = device_xname(self);
    617  1.1  jmcneill 	sc->sme->sme_cookie = sc;
    618  1.1  jmcneill 	sc->sme->sme_refresh = sunxi_thermal_refresh;
    619  1.1  jmcneill 	for (i = 0; i < sc->conf->nsensors; i++) {
    620  1.1  jmcneill 		sc->data[i].private = i;
    621  1.1  jmcneill 		sc->data[i].units = ENVSYS_STEMP;
    622  1.1  jmcneill 		sc->data[i].state = ENVSYS_SINVALID;
    623  1.4  jmcneill 		strlcpy(sc->data[i].desc, sc->conf->sensors[i].desc,
    624  1.4  jmcneill 		    sizeof(sc->data[i].desc));
    625  1.1  jmcneill 		sysmon_envsys_sensor_attach(sc->sme, &sc->data[i]);
    626  1.1  jmcneill 	}
    627  1.1  jmcneill 	sysmon_envsys_register(sc->sme);
    628  1.1  jmcneill 
    629  1.1  jmcneill 	for (i = 0; i < sc->conf->nsensors; i++) {
    630  1.1  jmcneill 		device_printf(self,
    631  1.1  jmcneill 		    "%s: alarm %dC hyst %dC shut %dC\n",
    632  1.1  jmcneill 		    sc->conf->sensors[i].name,
    633  1.1  jmcneill 		    sunxi_thermal_getalarm(sc, i),
    634  1.1  jmcneill 		    sunxi_thermal_gethyst(sc, i),
    635  1.1  jmcneill 		    sunxi_thermal_getshut(sc, i));
    636  1.1  jmcneill 	}
    637  1.1  jmcneill }
    638  1.1  jmcneill 
    639  1.1  jmcneill CFATTACH_DECL_NEW(sunxi_thermal, sizeof(struct sunxi_thermal_softc),
    640  1.1  jmcneill     sunxi_thermal_match, sunxi_thermal_attach, NULL, NULL);
    641