blob: b159464d82e79499e035df75d6a16866f62c19da [file] [log] [blame]
Ye.Lie3568d22014-11-20 21:14:13 +08001/*
2 * (C) Copyright 2014 Freescale Semiconductor, Inc.
3 * Author: Nitin Garg <nitin.garg@freescale.com>
4 * Ye Li <Ye.Li@freescale.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <config.h>
10#include <common.h>
11#include <div64.h>
12#include <fuse.h>
13#include <asm/io.h>
14#include <asm/arch/clock.h>
Tim Harveya91db952015-05-18 06:56:47 -070015#include <asm/arch/sys_proto.h>
Ye.Lie3568d22014-11-20 21:14:13 +080016#include <dm.h>
17#include <errno.h>
18#include <malloc.h>
19#include <thermal.h>
20#include <imx_thermal.h>
21
Tim Harveybe56de62015-05-21 08:40:06 -070022/* board will busyloop until this many degrees C below CPU max temperature */
23#define TEMPERATURE_HOT_DELTA 5 /* CPU maxT - 5C */
Ye.Lie3568d22014-11-20 21:14:13 +080024#define FACTOR0 10000000
Peng Fan4fac4172017-04-18 20:41:52 +080025#define FACTOR1 15423
26#define FACTOR2 4148468
27#define OFFSET 3580661
Ye.Lie3568d22014-11-20 21:14:13 +080028#define MEASURE_FREQ 327
Adrian Alonsod1aa6f22015-09-02 13:54:22 -050029#define TEMPERATURE_MIN -40
30#define TEMPERATURE_HOT 85
31#define TEMPERATURE_MAX 125
Ye.Lie3568d22014-11-20 21:14:13 +080032
33#define TEMPSENSE0_TEMP_CNT_SHIFT 8
34#define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
35#define TEMPSENSE0_FINISHED (1 << 2)
36#define TEMPSENSE0_MEASURE_TEMP (1 << 1)
37#define TEMPSENSE0_POWER_DOWN (1 << 0)
38#define MISC0_REFTOP_SELBIASOFF (1 << 3)
39#define TEMPSENSE1_MEASURE_FREQ 0xffff
40
Tim Harveya91db952015-05-18 06:56:47 -070041struct thermal_data {
42 unsigned int fuse;
Tim Harveybe56de62015-05-21 08:40:06 -070043 int critical;
Tim Harveya91db952015-05-18 06:56:47 -070044 int minc;
45 int maxc;
46};
47
Adrian Alonsod1aa6f22015-09-02 13:54:22 -050048#if defined(CONFIG_MX6)
49static int read_cpu_temperature(struct udevice *dev)
Ye.Lie3568d22014-11-20 21:14:13 +080050{
51 int temperature;
52 unsigned int reg, n_meas;
53 const struct imx_thermal_plat *pdata = dev_get_platdata(dev);
54 struct anatop_regs *anatop = (struct anatop_regs *)pdata->regs;
Tim Harveya91db952015-05-18 06:56:47 -070055 struct thermal_data *priv = dev_get_priv(dev);
56 u32 fuse = priv->fuse;
Ye.Lie3568d22014-11-20 21:14:13 +080057 int t1, n1;
Peng Fan4fac4172017-04-18 20:41:52 +080058 u64 c1, c2;
Ye.Lie3568d22014-11-20 21:14:13 +080059 u64 temp64;
60
61 /*
62 * Sensor data layout:
63 * [31:20] - sensor value @ 25C
64 * We use universal formula now and only need sensor value @ 25C
Peng Fan4fac4172017-04-18 20:41:52 +080065 * slope = 0.4445388 - (0.0016549 * 25C fuse)
Ye.Lie3568d22014-11-20 21:14:13 +080066 */
67 n1 = fuse >> 20;
68 t1 = 25; /* t1 always 25C */
69
70 /*
71 * Derived from linear interpolation:
Peng Fan4fac4172017-04-18 20:41:52 +080072 * slope = 0.4445388 - (0.0016549 * 25C fuse)
Ye.Lie3568d22014-11-20 21:14:13 +080073 * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
Peng Fan4fac4172017-04-18 20:41:52 +080074 * offset = 3.580661
75 * offset = OFFSET / 1000000
76 * (Nmeas - n1) / (Tmeas - t1 - offset) = slope
Ye.Lie3568d22014-11-20 21:14:13 +080077 * We want to reduce this down to the minimum computation necessary
78 * for each temperature read. Also, we want Tmeas in millicelsius
79 * and we don't want to lose precision from integer division. So...
Peng Fan4fac4172017-04-18 20:41:52 +080080 * Tmeas = (Nmeas - n1) / slope + t1 + offset
81 * milli_Tmeas = 1000000 * (Nmeas - n1) / slope + 1000000 * t1 + OFFSET
82 * milli_Tmeas = -1000000 * (n1 - Nmeas) / slope + 1000000 * t1 + OFFSET
83 * Let constant c1 = (-1000000 / slope)
84 * milli_Tmeas = (n1 - Nmeas) * c1 + 1000000 * t1 + OFFSET
85 * Let constant c2 = n1 *c1 + 1000000 * t1
86 * milli_Tmeas = (c2 - Nmeas * c1) + OFFSET
87 * Tmeas = ((c2 - Nmeas * c1) + OFFSET) / 1000000
Ye.Lie3568d22014-11-20 21:14:13 +080088 */
89 temp64 = FACTOR0;
Peng Fan4fac4172017-04-18 20:41:52 +080090 temp64 *= 1000000;
Ye.Lie3568d22014-11-20 21:14:13 +080091 do_div(temp64, FACTOR1 * n1 - FACTOR2);
92 c1 = temp64;
Peng Fan4fac4172017-04-18 20:41:52 +080093 c2 = n1 * c1 + 1000000 * t1;
Ye.Lie3568d22014-11-20 21:14:13 +080094
95 /*
96 * now we only use single measure, every time we read
97 * the temperature, we will power on/down anadig thermal
98 * module
99 */
100 writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_clr);
101 writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_set);
102
103 /* setup measure freq */
104 reg = readl(&anatop->tempsense1);
105 reg &= ~TEMPSENSE1_MEASURE_FREQ;
106 reg |= MEASURE_FREQ;
107 writel(reg, &anatop->tempsense1);
108
109 /* start the measurement process */
110 writel(TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_clr);
111 writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
112 writel(TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_set);
113
114 /* make sure that the latest temp is valid */
115 while ((readl(&anatop->tempsense0) &
116 TEMPSENSE0_FINISHED) == 0)
117 udelay(10000);
118
119 /* read temperature count */
120 reg = readl(&anatop->tempsense0);
121 n_meas = (reg & TEMPSENSE0_TEMP_CNT_MASK)
122 >> TEMPSENSE0_TEMP_CNT_SHIFT;
123 writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
124
Peng Fan4fac4172017-04-18 20:41:52 +0800125 /* Tmeas = (c2 - Nmeas * c1 + OFFSET) / 1000000 */
126 temperature = lldiv(c2 - n_meas * c1 + OFFSET, 1000000);
Ye.Lie3568d22014-11-20 21:14:13 +0800127
128 /* power down anatop thermal sensor */
129 writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
130 writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_clr);
131
132 return temperature;
133}
Adrian Alonsod1aa6f22015-09-02 13:54:22 -0500134#elif defined(CONFIG_MX7)
135static int read_cpu_temperature(struct udevice *dev)
136{
Peng Fanfcbe8c52016-01-04 21:12:22 +0800137 unsigned int reg, tmp;
Adrian Alonsod1aa6f22015-09-02 13:54:22 -0500138 unsigned int raw_25c, te1;
139 int temperature;
140 unsigned int *priv = dev_get_priv(dev);
141 u32 fuse = *priv;
142 struct mxc_ccm_anatop_reg *ccm_anatop = (struct mxc_ccm_anatop_reg *)
143 ANATOP_BASE_ADDR;
144 /*
145 * fuse data layout:
146 * [31:21] sensor value @ 25C
147 * [20:18] hot temperature value
148 * [17:9] sensor value of room
149 * [8:0] sensor value of hot
150 */
151
152 raw_25c = fuse >> 21;
153 if (raw_25c == 0)
154 raw_25c = 25;
155
156 te1 = (fuse >> 9) & 0x1ff;
157
158 /*
159 * now we only use single measure, every time we read
160 * the temperature, we will power on/down anadig thermal
161 * module
162 */
163 writel(TEMPMON_HW_ANADIG_TEMPSENSE1_POWER_DOWN_MASK, &ccm_anatop->tempsense1_clr);
164 writel(PMU_REF_REFTOP_SELFBIASOFF_MASK, &ccm_anatop->ref_set);
165
166 /* write measure freq */
167 reg = readl(&ccm_anatop->tempsense1);
168 reg &= ~TEMPMON_HW_ANADIG_TEMPSENSE1_MEASURE_FREQ_MASK;
169 reg |= TEMPMON_HW_ANADIG_TEMPSENSE1_MEASURE_FREQ(MEASURE_FREQ);
170 writel(reg, &ccm_anatop->tempsense1);
171
172 writel(TEMPMON_HW_ANADIG_TEMPSENSE1_MEASURE_TEMP_MASK, &ccm_anatop->tempsense1_clr);
173 writel(TEMPMON_HW_ANADIG_TEMPSENSE1_FINISHED_MASK, &ccm_anatop->tempsense1_clr);
174 writel(TEMPMON_HW_ANADIG_TEMPSENSE1_MEASURE_TEMP_MASK, &ccm_anatop->tempsense1_set);
175
Peng Fanfcbe8c52016-01-04 21:12:22 +0800176 if (soc_rev() >= CHIP_REV_1_1) {
177 while ((readl(&ccm_anatop->tempsense1) &
178 TEMPMON_HW_ANADIG_TEMPSENSE1_FINISHED_MASK) == 0)
179 ;
Adrian Alonsod1aa6f22015-09-02 13:54:22 -0500180 reg = readl(&ccm_anatop->tempsense1);
181 tmp = (reg & TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_MASK)
182 >> TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_SHIFT;
Peng Fanfcbe8c52016-01-04 21:12:22 +0800183 } else {
184 /*
185 * Since we can not rely on finish bit, use 10ms
186 * delay to get temperature. From RM, 17us is
187 * enough to get data, but to gurantee to get
188 * the data, delay 10ms here.
189 */
190 udelay(10000);
191 reg = readl(&ccm_anatop->tempsense1);
192 tmp = (reg & TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_MASK)
193 >> TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_SHIFT;
194 }
Adrian Alonsod1aa6f22015-09-02 13:54:22 -0500195
196 writel(TEMPMON_HW_ANADIG_TEMPSENSE1_FINISHED_MASK, &ccm_anatop->tempsense1_clr);
197
198 /* power down anatop thermal sensor */
199 writel(TEMPMON_HW_ANADIG_TEMPSENSE1_POWER_DOWN_MASK, &ccm_anatop->tempsense1_set);
200 writel(PMU_REF_REFTOP_SELFBIASOFF_MASK, &ccm_anatop->ref_clr);
201
202 /* Single point */
203 temperature = tmp - (te1 - raw_25c);
204
205 return temperature;
206}
207#endif
Ye.Lie3568d22014-11-20 21:14:13 +0800208
209int imx_thermal_get_temp(struct udevice *dev, int *temp)
210{
Tim Harveya91db952015-05-18 06:56:47 -0700211 struct thermal_data *priv = dev_get_priv(dev);
Ye.Lie3568d22014-11-20 21:14:13 +0800212 int cpu_tmp = 0;
213
Adrian Alonsod1aa6f22015-09-02 13:54:22 -0500214 cpu_tmp = read_cpu_temperature(dev);
215
Tim Harvey3b7ad212015-06-09 06:40:22 -0700216 while (cpu_tmp >= priv->critical) {
217 printf("CPU Temperature (%dC) too close to max (%dC)",
218 cpu_tmp, priv->maxc);
219 puts(" waiting...\n");
220 udelay(5000000);
Adrian Alonsod1aa6f22015-09-02 13:54:22 -0500221 cpu_tmp = read_cpu_temperature(dev);
Ye.Lie3568d22014-11-20 21:14:13 +0800222 }
223
224 *temp = cpu_tmp;
225
226 return 0;
227}
228
229static const struct dm_thermal_ops imx_thermal_ops = {
230 .get_temp = imx_thermal_get_temp,
231};
232
233static int imx_thermal_probe(struct udevice *dev)
234{
235 unsigned int fuse = ~0;
236
237 const struct imx_thermal_plat *pdata = dev_get_platdata(dev);
Tim Harveya91db952015-05-18 06:56:47 -0700238 struct thermal_data *priv = dev_get_priv(dev);
Ye.Lie3568d22014-11-20 21:14:13 +0800239
240 /* Read Temperature calibration data fuse */
241 fuse_read(pdata->fuse_bank, pdata->fuse_word, &fuse);
242
Adrian Alonso1368f992015-09-02 13:54:13 -0500243 if (is_soc_type(MXC_SOC_MX6)) {
244 /* Check for valid fuse */
245 if (fuse == 0 || fuse == ~0) {
Fabio Estevamc8434cc2015-09-08 14:43:09 -0300246 debug("CPU: Thermal invalid data, fuse: 0x%x\n",
Adrian Alonsod1aa6f22015-09-02 13:54:22 -0500247 fuse);
Adrian Alonso1368f992015-09-02 13:54:13 -0500248 return -EPERM;
249 }
Adrian Alonsod1aa6f22015-09-02 13:54:22 -0500250 } else if (is_soc_type(MXC_SOC_MX7)) {
251 /* No Calibration data in FUSE? */
252 if ((fuse & 0x3ffff) == 0)
253 return -EPERM;
254 /* We do not support 105C TE2 */
255 if (((fuse & 0x1c0000) >> 18) == 0x6)
256 return -EPERM;
Ye.Lie3568d22014-11-20 21:14:13 +0800257 }
258
Tim Harveybe56de62015-05-21 08:40:06 -0700259 /* set critical cooling temp */
Tim Harveya91db952015-05-18 06:56:47 -0700260 get_cpu_temp_grade(&priv->minc, &priv->maxc);
Tim Harveybe56de62015-05-21 08:40:06 -0700261 priv->critical = priv->maxc - TEMPERATURE_HOT_DELTA;
Tim Harveya91db952015-05-18 06:56:47 -0700262 priv->fuse = fuse;
Ye.Lie3568d22014-11-20 21:14:13 +0800263
264 enable_thermal_clk();
265
266 return 0;
267}
268
269U_BOOT_DRIVER(imx_thermal) = {
270 .name = "imx_thermal",
271 .id = UCLASS_THERMAL,
272 .ops = &imx_thermal_ops,
273 .probe = imx_thermal_probe,
Tim Harveya91db952015-05-18 06:56:47 -0700274 .priv_auto_alloc_size = sizeof(struct thermal_data),
Ye.Lie3568d22014-11-20 21:14:13 +0800275 .flags = DM_FLAG_PRE_RELOC,
276};