]> git.ipfire.org Git - thirdparty/u-boot.git/blob - drivers/thermal/imx_thermal.c
thermal: imx_thermal: increase critical temperature threshold
[thirdparty/u-boot.git] / drivers / thermal / imx_thermal.c
1 /*
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>
15 #include <asm/arch/sys_proto.h>
16 #include <dm.h>
17 #include <errno.h>
18 #include <malloc.h>
19 #include <thermal.h>
20 #include <imx_thermal.h>
21
22 /* board will busyloop until this many degrees C below CPU max temperature */
23 #define TEMPERATURE_HOT_DELTA 5 /* CPU maxT - 5C */
24 #define FACTOR0 10000000
25 #define FACTOR1 15976
26 #define FACTOR2 4297157
27 #define MEASURE_FREQ 327
28
29 #define TEMPSENSE0_TEMP_CNT_SHIFT 8
30 #define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
31 #define TEMPSENSE0_FINISHED (1 << 2)
32 #define TEMPSENSE0_MEASURE_TEMP (1 << 1)
33 #define TEMPSENSE0_POWER_DOWN (1 << 0)
34 #define MISC0_REFTOP_SELBIASOFF (1 << 3)
35 #define TEMPSENSE1_MEASURE_FREQ 0xffff
36
37 struct thermal_data {
38 unsigned int fuse;
39 int critical;
40 int minc;
41 int maxc;
42 };
43
44 static int read_cpu_temperature(struct udevice *dev)
45 {
46 int temperature;
47 unsigned int reg, n_meas;
48 const struct imx_thermal_plat *pdata = dev_get_platdata(dev);
49 struct anatop_regs *anatop = (struct anatop_regs *)pdata->regs;
50 struct thermal_data *priv = dev_get_priv(dev);
51 u32 fuse = priv->fuse;
52 int t1, n1;
53 u32 c1, c2;
54 u64 temp64;
55
56 /*
57 * Sensor data layout:
58 * [31:20] - sensor value @ 25C
59 * We use universal formula now and only need sensor value @ 25C
60 * slope = 0.4297157 - (0.0015976 * 25C fuse)
61 */
62 n1 = fuse >> 20;
63 t1 = 25; /* t1 always 25C */
64
65 /*
66 * Derived from linear interpolation:
67 * slope = 0.4297157 - (0.0015976 * 25C fuse)
68 * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
69 * (Nmeas - n1) / (Tmeas - t1) = slope
70 * We want to reduce this down to the minimum computation necessary
71 * for each temperature read. Also, we want Tmeas in millicelsius
72 * and we don't want to lose precision from integer division. So...
73 * Tmeas = (Nmeas - n1) / slope + t1
74 * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1
75 * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1
76 * Let constant c1 = (-1000 / slope)
77 * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1
78 * Let constant c2 = n1 *c1 + 1000 * t1
79 * milli_Tmeas = c2 - Nmeas * c1
80 */
81 temp64 = FACTOR0;
82 temp64 *= 1000;
83 do_div(temp64, FACTOR1 * n1 - FACTOR2);
84 c1 = temp64;
85 c2 = n1 * c1 + 1000 * t1;
86
87 /*
88 * now we only use single measure, every time we read
89 * the temperature, we will power on/down anadig thermal
90 * module
91 */
92 writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_clr);
93 writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_set);
94
95 /* setup measure freq */
96 reg = readl(&anatop->tempsense1);
97 reg &= ~TEMPSENSE1_MEASURE_FREQ;
98 reg |= MEASURE_FREQ;
99 writel(reg, &anatop->tempsense1);
100
101 /* start the measurement process */
102 writel(TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_clr);
103 writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
104 writel(TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_set);
105
106 /* make sure that the latest temp is valid */
107 while ((readl(&anatop->tempsense0) &
108 TEMPSENSE0_FINISHED) == 0)
109 udelay(10000);
110
111 /* read temperature count */
112 reg = readl(&anatop->tempsense0);
113 n_meas = (reg & TEMPSENSE0_TEMP_CNT_MASK)
114 >> TEMPSENSE0_TEMP_CNT_SHIFT;
115 writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
116
117 /* milli_Tmeas = c2 - Nmeas * c1 */
118 temperature = (c2 - n_meas * c1)/1000;
119
120 /* power down anatop thermal sensor */
121 writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
122 writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_clr);
123
124 return temperature;
125 }
126
127 int imx_thermal_get_temp(struct udevice *dev, int *temp)
128 {
129 struct thermal_data *priv = dev_get_priv(dev);
130 int cpu_tmp = 0;
131
132 cpu_tmp = read_cpu_temperature(dev);
133 while (cpu_tmp > priv->minc && cpu_tmp < priv->maxc) {
134 if (cpu_tmp >= priv->critical) {
135 printf("CPU Temperature (%dC) too close to max (%dC)",
136 cpu_tmp, priv->maxc);
137 puts(" waiting...\n");
138 udelay(5000000);
139 cpu_tmp = read_cpu_temperature(dev);
140 } else {
141 break;
142 }
143 }
144
145 *temp = cpu_tmp;
146
147 return 0;
148 }
149
150 static const struct dm_thermal_ops imx_thermal_ops = {
151 .get_temp = imx_thermal_get_temp,
152 };
153
154 static int imx_thermal_probe(struct udevice *dev)
155 {
156 unsigned int fuse = ~0;
157
158 const struct imx_thermal_plat *pdata = dev_get_platdata(dev);
159 struct thermal_data *priv = dev_get_priv(dev);
160
161 /* Read Temperature calibration data fuse */
162 fuse_read(pdata->fuse_bank, pdata->fuse_word, &fuse);
163
164 /* Check for valid fuse */
165 if (fuse == 0 || fuse == ~0) {
166 printf("CPU: Thermal invalid data, fuse: 0x%x\n", fuse);
167 return -EPERM;
168 }
169
170 /* set critical cooling temp */
171 get_cpu_temp_grade(&priv->minc, &priv->maxc);
172 priv->critical = priv->maxc - TEMPERATURE_HOT_DELTA;
173 priv->fuse = fuse;
174
175 enable_thermal_clk();
176
177 return 0;
178 }
179
180 U_BOOT_DRIVER(imx_thermal) = {
181 .name = "imx_thermal",
182 .id = UCLASS_THERMAL,
183 .ops = &imx_thermal_ops,
184 .probe = imx_thermal_probe,
185 .priv_auto_alloc_size = sizeof(struct thermal_data),
186 .flags = DM_FLAG_PRE_RELOC,
187 };