2 * (C) Copyright 2014 Freescale Semiconductor, Inc.
3 * Author: Nitin Garg <nitin.garg@freescale.com>
4 * Ye Li <Ye.Li@freescale.com>
6 * SPDX-License-Identifier: GPL-2.0+
14 #include <asm/arch/clock.h>
15 #include <asm/arch/sys_proto.h>
20 #include <imx_thermal.h>
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
26 #define FACTOR2 4297157
27 #define MEASURE_FREQ 327
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
44 static int read_cpu_temperature(struct udevice *dev)
47 unsigned int reg, n_meas;
48 const struct imx_thermal_plat *pdata = dev_get_platdata(dev);
49 struct anatop_regs *anatop = pdata->regs;
50 struct thermal_data *priv = dev_get_priv(dev);
51 u32 fuse = priv->fuse;
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)
63 t1 = 25; /* t1 always 25C */
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
83 do_div(temp64, FACTOR1 * n1 - FACTOR2);
85 c2 = n1 * c1 + 1000 * t1;
88 * now we only use single measure, every time we read
89 * the temperature, we will power on/down anadig thermal
92 writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_clr);
93 writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_set);
95 /* setup measure freq */
96 writel(MEASURE_FREQ, &anatop->tempsense1);
98 /* start the measurement process */
99 writel(TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_clr);
100 writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
101 writel(TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_set);
103 /* make sure that the latest temp is valid */
104 const int max_loops = 30;
107 while (((reg = readl(&anatop->tempsense0)) & TEMPSENSE0_FINISHED) == 0) {
109 if (++loops >= max_loops)
112 if ((readl(&anatop->tempsense0) & TEMPSENSE0_FINISHED) == 0)
115 /* read temperature count */
116 reg = readl(&anatop->tempsense0);
117 n_meas = (reg & TEMPSENSE0_TEMP_CNT_MASK)
118 >> TEMPSENSE0_TEMP_CNT_SHIFT;
119 writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
121 /* milli_Tmeas = c2 - Nmeas * c1 */
122 temperature = (long)(c2 - n_meas * c1) / 1000;
124 /* power down anatop thermal sensor */
125 writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
126 writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_clr);
131 int imx_thermal_get_temp(struct udevice *dev, int *temp)
133 struct thermal_data *priv = dev_get_priv(dev);
136 cpu_tmp = read_cpu_temperature(dev);
137 while (cpu_tmp >= priv->critical) {
138 printf("CPU Temperature (%dC) too close to max (%dC)",
139 cpu_tmp, priv->maxc);
140 puts(" waiting...\n");
142 cpu_tmp = read_cpu_temperature(dev);
150 static const struct dm_thermal_ops imx_thermal_ops = {
151 .get_temp = imx_thermal_get_temp,
154 static int imx_thermal_probe(struct udevice *dev)
157 unsigned int fuse = ~0;
158 const struct imx_thermal_plat *pdata = dev_get_platdata(dev);
159 struct thermal_data *priv = dev_get_priv(dev);
161 /* Read Temperature calibration data fuse */
162 if ((ret = fuse_read(pdata->fuse_bank, pdata->fuse_word, &fuse))) {
163 printf("Failed to read temp calib fuse: %d\n", ret);
167 /* Check for valid fuse */
168 if (fuse == 0 || fuse == ~0) {
169 static int first = 1;
172 printf("CPU: Thermal invalid data, fuse: %#10x\n", fuse);
178 /* set critical cooling temp */
179 get_cpu_temp_grade(&priv->minc, &priv->maxc);
180 priv->critical = priv->maxc - TEMPERATURE_HOT_DELTA;
183 enable_thermal_clk();
188 U_BOOT_DRIVER(imx_thermal) = {
189 .name = "imx_thermal",
190 .id = UCLASS_THERMAL,
191 .ops = &imx_thermal_ops,
192 .probe = imx_thermal_probe,
193 .priv_auto_alloc_size = sizeof(struct thermal_data),
194 .flags = DM_FLAG_PRE_RELOC,