1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2014 Freescale Semiconductor, Inc.
4 * Author: Nitin Garg <nitin.garg@freescale.com>
5 * Ye Li <Ye.Li@freescale.com>
14 #include <asm/arch/clock.h>
15 #include <asm/arch/sys_proto.h>
19 #include <linux/delay.h>
20 #include <linux/math64.h>
22 #include <imx_thermal.h>
24 /* board will busyloop until this many degrees C below CPU max temperature */
25 #define TEMPERATURE_HOT_DELTA 5 /* CPU maxT - 5C */
26 #define FACTOR0 10000000
28 #define FACTOR2 4148468
29 #define OFFSET 3580661
30 #define MEASURE_FREQ 327
31 #define TEMPERATURE_MIN -40
32 #define TEMPERATURE_HOT 85
33 #define TEMPERATURE_MAX 125
35 #define TEMPSENSE0_TEMP_CNT_SHIFT 8
36 #define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
37 #define TEMPSENSE0_FINISHED (1 << 2)
38 #define TEMPSENSE0_MEASURE_TEMP (1 << 1)
39 #define TEMPSENSE0_POWER_DOWN (1 << 0)
40 #define MISC0_REFTOP_SELBIASOFF (1 << 3)
41 #define TEMPSENSE1_MEASURE_FREQ 0xffff
50 #if defined(CONFIG_MX6)
51 static int read_cpu_temperature(struct udevice *dev)
54 unsigned int reg, n_meas;
55 const struct imx_thermal_plat *pdata = dev_get_plat(dev);
56 struct anatop_regs *anatop = (struct anatop_regs *)pdata->regs;
57 struct thermal_data *priv = dev_get_priv(dev);
58 u32 fuse = priv->fuse;
66 * [31:20] - sensor value @ 25C
67 * We use universal formula now and only need sensor value @ 25C
68 * slope = 0.4445388 - (0.0016549 * 25C fuse)
71 t1 = 25; /* t1 always 25C */
74 * Derived from linear interpolation:
75 * slope = 0.4445388 - (0.0016549 * 25C fuse)
76 * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
78 * offset = OFFSET / 1000000
79 * (Nmeas - n1) / (Tmeas - t1 - offset) = slope
80 * We want to reduce this down to the minimum computation necessary
81 * for each temperature read. Also, we want Tmeas in millicelsius
82 * and we don't want to lose precision from integer division. So...
83 * Tmeas = (Nmeas - n1) / slope + t1 + offset
84 * milli_Tmeas = 1000000 * (Nmeas - n1) / slope + 1000000 * t1 + OFFSET
85 * milli_Tmeas = -1000000 * (n1 - Nmeas) / slope + 1000000 * t1 + OFFSET
86 * Let constant c1 = (-1000000 / slope)
87 * milli_Tmeas = (n1 - Nmeas) * c1 + 1000000 * t1 + OFFSET
88 * Let constant c2 = n1 *c1 + 1000000 * t1
89 * milli_Tmeas = (c2 - Nmeas * c1) + OFFSET
90 * Tmeas = ((c2 - Nmeas * c1) + OFFSET) / 1000000
94 temp64 = div_s64_rem(temp64, FACTOR1 * n1 - FACTOR2, &rem);
96 c2 = n1 * c1 + 1000000 * t1;
99 * now we only use single measure, every time we read
100 * the temperature, we will power on/down anadig thermal
103 writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_clr);
104 writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_set);
106 /* setup measure freq */
107 reg = readl(&anatop->tempsense1);
108 reg &= ~TEMPSENSE1_MEASURE_FREQ;
110 writel(reg, &anatop->tempsense1);
112 /* start the measurement process */
113 writel(TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_clr);
114 writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
115 writel(TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_set);
117 /* make sure that the latest temp is valid */
118 while ((readl(&anatop->tempsense0) &
119 TEMPSENSE0_FINISHED) == 0)
122 /* read temperature count */
123 reg = readl(&anatop->tempsense0);
124 n_meas = (reg & TEMPSENSE0_TEMP_CNT_MASK)
125 >> TEMPSENSE0_TEMP_CNT_SHIFT;
126 writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
128 /* Tmeas = (c2 - Nmeas * c1 + OFFSET) / 1000000 */
129 temperature = div_s64_rem(c2 - n_meas * c1 + OFFSET, 1000000, &rem);
131 /* power down anatop thermal sensor */
132 writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
133 writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_clr);
137 #elif defined(CONFIG_MX7)
138 static int read_cpu_temperature(struct udevice *dev)
140 unsigned int reg, tmp;
141 unsigned int raw_25c, te1;
143 unsigned int *priv = dev_get_priv(dev);
145 struct mxc_ccm_anatop_reg *ccm_anatop = (struct mxc_ccm_anatop_reg *)
149 * [31:21] sensor value @ 25C
150 * [20:18] hot temperature value
151 * [17:9] sensor value of room
152 * [8:0] sensor value of hot
155 raw_25c = fuse >> 21;
159 te1 = (fuse >> 9) & 0x1ff;
162 * now we only use single measure, every time we read
163 * the temperature, we will power on/down anadig thermal
166 writel(TEMPMON_HW_ANADIG_TEMPSENSE1_POWER_DOWN_MASK, &ccm_anatop->tempsense1_clr);
167 writel(PMU_REF_REFTOP_SELFBIASOFF_MASK, &ccm_anatop->ref_set);
169 /* write measure freq */
170 reg = readl(&ccm_anatop->tempsense1);
171 reg &= ~TEMPMON_HW_ANADIG_TEMPSENSE1_MEASURE_FREQ_MASK;
172 reg |= TEMPMON_HW_ANADIG_TEMPSENSE1_MEASURE_FREQ(MEASURE_FREQ);
173 writel(reg, &ccm_anatop->tempsense1);
175 writel(TEMPMON_HW_ANADIG_TEMPSENSE1_MEASURE_TEMP_MASK, &ccm_anatop->tempsense1_clr);
176 writel(TEMPMON_HW_ANADIG_TEMPSENSE1_FINISHED_MASK, &ccm_anatop->tempsense1_clr);
177 writel(TEMPMON_HW_ANADIG_TEMPSENSE1_MEASURE_TEMP_MASK, &ccm_anatop->tempsense1_set);
179 if (soc_rev() >= CHIP_REV_1_1) {
180 while ((readl(&ccm_anatop->tempsense1) &
181 TEMPMON_HW_ANADIG_TEMPSENSE1_FINISHED_MASK) == 0)
183 reg = readl(&ccm_anatop->tempsense1);
184 tmp = (reg & TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_MASK)
185 >> TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_SHIFT;
188 * Since we can not rely on finish bit, use 10ms
189 * delay to get temperature. From RM, 17us is
190 * enough to get data, but to gurantee to get
191 * the data, delay 10ms here.
194 reg = readl(&ccm_anatop->tempsense1);
195 tmp = (reg & TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_MASK)
196 >> TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_SHIFT;
199 writel(TEMPMON_HW_ANADIG_TEMPSENSE1_FINISHED_MASK, &ccm_anatop->tempsense1_clr);
201 /* power down anatop thermal sensor */
202 writel(TEMPMON_HW_ANADIG_TEMPSENSE1_POWER_DOWN_MASK, &ccm_anatop->tempsense1_set);
203 writel(PMU_REF_REFTOP_SELFBIASOFF_MASK, &ccm_anatop->ref_clr);
206 temperature = tmp - (te1 - raw_25c);
212 int imx_thermal_get_temp(struct udevice *dev, int *temp)
214 struct thermal_data *priv = dev_get_priv(dev);
217 cpu_tmp = read_cpu_temperature(dev);
219 while (cpu_tmp >= priv->critical) {
220 printf("CPU Temperature (%dC) too close to max (%dC)",
221 cpu_tmp, priv->maxc);
222 puts(" waiting...\n");
224 cpu_tmp = read_cpu_temperature(dev);
232 static const struct dm_thermal_ops imx_thermal_ops = {
233 .get_temp = imx_thermal_get_temp,
236 static int imx_thermal_probe(struct udevice *dev)
238 unsigned int fuse = ~0;
240 const struct imx_thermal_plat *pdata = dev_get_plat(dev);
241 struct thermal_data *priv = dev_get_priv(dev);
243 /* Read Temperature calibration data fuse */
244 fuse_read(pdata->fuse_bank, pdata->fuse_word, &fuse);
246 if (is_soc_type(MXC_SOC_MX6)) {
247 /* Check for valid fuse */
248 if (fuse == 0 || fuse == ~0) {
249 debug("CPU: Thermal invalid data, fuse: 0x%x\n",
253 } else if (is_soc_type(MXC_SOC_MX7)) {
254 /* No Calibration data in FUSE? */
255 if ((fuse & 0x3ffff) == 0)
257 /* We do not support 105C TE2 */
258 if (((fuse & 0x1c0000) >> 18) == 0x6)
262 /* set critical cooling temp */
263 get_cpu_temp_grade(&priv->minc, &priv->maxc);
264 priv->critical = priv->maxc - TEMPERATURE_HOT_DELTA;
267 enable_thermal_clk();
272 U_BOOT_DRIVER(imx_thermal) = {
273 .name = "imx_thermal",
274 .id = UCLASS_THERMAL,
275 .ops = &imx_thermal_ops,
276 .probe = imx_thermal_probe,
277 .priv_auto = sizeof(struct thermal_data),
278 .flags = DM_FLAG_PRE_RELOC,