blob: 101bd5ef2d743f39a5b49627a55e10bfac9e7466 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ye.Lid049afa2014-11-20 21:14:13 +08002/*
3 * (C) Copyright 2014 Freescale Semiconductor, Inc.
4 * Author: Nitin Garg <nitin.garg@freescale.com>
5 * Ye Li <Ye.Li@freescale.com>
Ye.Lid049afa2014-11-20 21:14:13 +08006 */
7
8#include <config.h>
9#include <common.h>
10#include <div64.h>
11#include <fuse.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Ye.Lid049afa2014-11-20 21:14:13 +080013#include <asm/io.h>
14#include <asm/arch/clock.h>
Tim Harvey291f73b2015-05-18 06:56:47 -070015#include <asm/arch/sys_proto.h>
Ye.Lid049afa2014-11-20 21:14:13 +080016#include <dm.h>
17#include <errno.h>
18#include <malloc.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Peng Fan4922d6e2017-04-18 20:41:53 +080020#include <linux/math64.h>
Ye.Lid049afa2014-11-20 21:14:13 +080021#include <thermal.h>
22#include <imx_thermal.h>
23
Tim Harveyffaa3a12015-05-21 08:40:06 -070024/* board will busyloop until this many degrees C below CPU max temperature */
25#define TEMPERATURE_HOT_DELTA 5 /* CPU maxT - 5C */
Ye.Lid049afa2014-11-20 21:14:13 +080026#define FACTOR0 10000000
Peng Fan46c8e532017-04-18 20:41:52 +080027#define FACTOR1 15423
28#define FACTOR2 4148468
29#define OFFSET 3580661
Ye.Lid049afa2014-11-20 21:14:13 +080030#define MEASURE_FREQ 327
Adrian Alonsoc69675e2015-09-02 13:54:22 -050031#define TEMPERATURE_MIN -40
32#define TEMPERATURE_HOT 85
33#define TEMPERATURE_MAX 125
Ye.Lid049afa2014-11-20 21:14:13 +080034
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
42
Tim Harvey291f73b2015-05-18 06:56:47 -070043struct thermal_data {
44 unsigned int fuse;
Tim Harveyffaa3a12015-05-21 08:40:06 -070045 int critical;
Tim Harvey291f73b2015-05-18 06:56:47 -070046 int minc;
47 int maxc;
48};
49
Adrian Alonsoc69675e2015-09-02 13:54:22 -050050#if defined(CONFIG_MX6)
51static int read_cpu_temperature(struct udevice *dev)
Ye.Lid049afa2014-11-20 21:14:13 +080052{
53 int temperature;
54 unsigned int reg, n_meas;
55 const struct imx_thermal_plat *pdata = dev_get_platdata(dev);
56 struct anatop_regs *anatop = (struct anatop_regs *)pdata->regs;
Tim Harvey291f73b2015-05-18 06:56:47 -070057 struct thermal_data *priv = dev_get_priv(dev);
58 u32 fuse = priv->fuse;
Ye.Lid049afa2014-11-20 21:14:13 +080059 int t1, n1;
Peng Fan4922d6e2017-04-18 20:41:53 +080060 s64 c1, c2;
61 s64 temp64;
62 s32 rem;
Ye.Lid049afa2014-11-20 21:14:13 +080063
64 /*
65 * Sensor data layout:
66 * [31:20] - sensor value @ 25C
67 * We use universal formula now and only need sensor value @ 25C
Peng Fan46c8e532017-04-18 20:41:52 +080068 * slope = 0.4445388 - (0.0016549 * 25C fuse)
Ye.Lid049afa2014-11-20 21:14:13 +080069 */
70 n1 = fuse >> 20;
71 t1 = 25; /* t1 always 25C */
72
73 /*
74 * Derived from linear interpolation:
Peng Fan46c8e532017-04-18 20:41:52 +080075 * slope = 0.4445388 - (0.0016549 * 25C fuse)
Ye.Lid049afa2014-11-20 21:14:13 +080076 * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
Peng Fan46c8e532017-04-18 20:41:52 +080077 * offset = 3.580661
78 * offset = OFFSET / 1000000
79 * (Nmeas - n1) / (Tmeas - t1 - offset) = slope
Ye.Lid049afa2014-11-20 21:14:13 +080080 * 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...
Peng Fan46c8e532017-04-18 20:41:52 +080083 * 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
Ye.Lid049afa2014-11-20 21:14:13 +080091 */
92 temp64 = FACTOR0;
Peng Fan46c8e532017-04-18 20:41:52 +080093 temp64 *= 1000000;
Peng Fan4922d6e2017-04-18 20:41:53 +080094 temp64 = div_s64_rem(temp64, FACTOR1 * n1 - FACTOR2, &rem);
Ye.Lid049afa2014-11-20 21:14:13 +080095 c1 = temp64;
Peng Fan46c8e532017-04-18 20:41:52 +080096 c2 = n1 * c1 + 1000000 * t1;
Ye.Lid049afa2014-11-20 21:14:13 +080097
98 /*
99 * now we only use single measure, every time we read
100 * the temperature, we will power on/down anadig thermal
101 * module
102 */
103 writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_clr);
104 writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_set);
105
106 /* setup measure freq */
107 reg = readl(&anatop->tempsense1);
108 reg &= ~TEMPSENSE1_MEASURE_FREQ;
109 reg |= MEASURE_FREQ;
110 writel(reg, &anatop->tempsense1);
111
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);
116
117 /* make sure that the latest temp is valid */
118 while ((readl(&anatop->tempsense0) &
119 TEMPSENSE0_FINISHED) == 0)
120 udelay(10000);
121
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);
127
Peng Fan46c8e532017-04-18 20:41:52 +0800128 /* Tmeas = (c2 - Nmeas * c1 + OFFSET) / 1000000 */
Peng Fan4922d6e2017-04-18 20:41:53 +0800129 temperature = div_s64_rem(c2 - n_meas * c1 + OFFSET, 1000000, &rem);
Ye.Lid049afa2014-11-20 21:14:13 +0800130
131 /* power down anatop thermal sensor */
132 writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
133 writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_clr);
134
135 return temperature;
136}
Adrian Alonsoc69675e2015-09-02 13:54:22 -0500137#elif defined(CONFIG_MX7)
138static int read_cpu_temperature(struct udevice *dev)
139{
Peng Fan585b84a2016-01-04 21:12:22 +0800140 unsigned int reg, tmp;
Adrian Alonsoc69675e2015-09-02 13:54:22 -0500141 unsigned int raw_25c, te1;
142 int temperature;
143 unsigned int *priv = dev_get_priv(dev);
144 u32 fuse = *priv;
145 struct mxc_ccm_anatop_reg *ccm_anatop = (struct mxc_ccm_anatop_reg *)
146 ANATOP_BASE_ADDR;
147 /*
148 * fuse data layout:
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
153 */
154
155 raw_25c = fuse >> 21;
156 if (raw_25c == 0)
157 raw_25c = 25;
158
159 te1 = (fuse >> 9) & 0x1ff;
160
161 /*
162 * now we only use single measure, every time we read
163 * the temperature, we will power on/down anadig thermal
164 * module
165 */
166 writel(TEMPMON_HW_ANADIG_TEMPSENSE1_POWER_DOWN_MASK, &ccm_anatop->tempsense1_clr);
167 writel(PMU_REF_REFTOP_SELFBIASOFF_MASK, &ccm_anatop->ref_set);
168
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);
174
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);
178
Peng Fan585b84a2016-01-04 21:12:22 +0800179 if (soc_rev() >= CHIP_REV_1_1) {
180 while ((readl(&ccm_anatop->tempsense1) &
181 TEMPMON_HW_ANADIG_TEMPSENSE1_FINISHED_MASK) == 0)
182 ;
183 reg = readl(&ccm_anatop->tempsense1);
184 tmp = (reg & TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_MASK)
185 >> TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_SHIFT;
186 } else {
Adrian Alonsoc69675e2015-09-02 13:54:22 -0500187 /*
Peng Fan585b84a2016-01-04 21:12:22 +0800188 * 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.
Adrian Alonsoc69675e2015-09-02 13:54:22 -0500192 */
Peng Fan585b84a2016-01-04 21:12:22 +0800193 udelay(10000);
Adrian Alonsoc69675e2015-09-02 13:54:22 -0500194 reg = readl(&ccm_anatop->tempsense1);
195 tmp = (reg & TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_MASK)
196 >> TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_SHIFT;
Peng Fan585b84a2016-01-04 21:12:22 +0800197 }
Adrian Alonsoc69675e2015-09-02 13:54:22 -0500198
199 writel(TEMPMON_HW_ANADIG_TEMPSENSE1_FINISHED_MASK, &ccm_anatop->tempsense1_clr);
200
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);
204
205 /* Single point */
206 temperature = tmp - (te1 - raw_25c);
207
208 return temperature;
209}
210#endif
Ye.Lid049afa2014-11-20 21:14:13 +0800211
212int imx_thermal_get_temp(struct udevice *dev, int *temp)
213{
Tim Harvey291f73b2015-05-18 06:56:47 -0700214 struct thermal_data *priv = dev_get_priv(dev);
Ye.Lid049afa2014-11-20 21:14:13 +0800215 int cpu_tmp = 0;
216
Adrian Alonsoc69675e2015-09-02 13:54:22 -0500217 cpu_tmp = read_cpu_temperature(dev);
218
Tim Harveyf802d212015-06-09 06:40:22 -0700219 while (cpu_tmp >= priv->critical) {
220 printf("CPU Temperature (%dC) too close to max (%dC)",
221 cpu_tmp, priv->maxc);
222 puts(" waiting...\n");
223 udelay(5000000);
Adrian Alonsoc69675e2015-09-02 13:54:22 -0500224 cpu_tmp = read_cpu_temperature(dev);
Ye.Lid049afa2014-11-20 21:14:13 +0800225 }
226
227 *temp = cpu_tmp;
228
229 return 0;
230}
231
232static const struct dm_thermal_ops imx_thermal_ops = {
233 .get_temp = imx_thermal_get_temp,
234};
235
236static int imx_thermal_probe(struct udevice *dev)
237{
238 unsigned int fuse = ~0;
239
240 const struct imx_thermal_plat *pdata = dev_get_platdata(dev);
Tim Harvey291f73b2015-05-18 06:56:47 -0700241 struct thermal_data *priv = dev_get_priv(dev);
Ye.Lid049afa2014-11-20 21:14:13 +0800242
243 /* Read Temperature calibration data fuse */
244 fuse_read(pdata->fuse_bank, pdata->fuse_word, &fuse);
245
Adrian Alonsoce08c362015-09-02 13:54:13 -0500246 if (is_soc_type(MXC_SOC_MX6)) {
247 /* Check for valid fuse */
248 if (fuse == 0 || fuse == ~0) {
Fabio Estevameab45b12015-09-08 14:43:09 -0300249 debug("CPU: Thermal invalid data, fuse: 0x%x\n",
Adrian Alonsoc69675e2015-09-02 13:54:22 -0500250 fuse);
Adrian Alonsoce08c362015-09-02 13:54:13 -0500251 return -EPERM;
252 }
Adrian Alonsoc69675e2015-09-02 13:54:22 -0500253 } else if (is_soc_type(MXC_SOC_MX7)) {
254 /* No Calibration data in FUSE? */
255 if ((fuse & 0x3ffff) == 0)
256 return -EPERM;
257 /* We do not support 105C TE2 */
258 if (((fuse & 0x1c0000) >> 18) == 0x6)
259 return -EPERM;
Ye.Lid049afa2014-11-20 21:14:13 +0800260 }
261
Tim Harveyffaa3a12015-05-21 08:40:06 -0700262 /* set critical cooling temp */
Tim Harvey291f73b2015-05-18 06:56:47 -0700263 get_cpu_temp_grade(&priv->minc, &priv->maxc);
Tim Harveyffaa3a12015-05-21 08:40:06 -0700264 priv->critical = priv->maxc - TEMPERATURE_HOT_DELTA;
Tim Harvey291f73b2015-05-18 06:56:47 -0700265 priv->fuse = fuse;
Ye.Lid049afa2014-11-20 21:14:13 +0800266
267 enable_thermal_clk();
268
269 return 0;
270}
271
272U_BOOT_DRIVER(imx_thermal) = {
273 .name = "imx_thermal",
274 .id = UCLASS_THERMAL,
275 .ops = &imx_thermal_ops,
276 .probe = imx_thermal_probe,
Tim Harvey291f73b2015-05-18 06:56:47 -0700277 .priv_auto_alloc_size = sizeof(struct thermal_data),
Ye.Lid049afa2014-11-20 21:14:13 +0800278 .flags = DM_FLAG_PRE_RELOC,
279};