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 = (struct anatop_regs *)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 reg = readl(&anatop->tempsense1);
97 reg &= ~TEMPSENSE1_MEASURE_FREQ;
99 writel(reg, &anatop->tempsense1);
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);
106 /* make sure that the latest temp is valid */
107 while ((readl(&anatop->tempsense0) &
108 TEMPSENSE0_FINISHED) == 0)
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);
117 /* milli_Tmeas = c2 - Nmeas * c1 */
118 temperature = (c2 - n_meas * c1)/1000;
120 /* power down anatop thermal sensor */
121 writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
122 writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_clr);
127 int imx_thermal_get_temp(struct udevice *dev, int *temp)
129 struct thermal_data *priv = dev_get_priv(dev);
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");
139 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)
156 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 fuse_read(pdata->fuse_bank, pdata->fuse_word, &fuse);
164 /* Check for valid fuse */
165 if (fuse == 0 || fuse == ~0) {
166 printf("CPU: Thermal invalid data, fuse: 0x%x\n", fuse);
170 /* set critical cooling temp */
171 get_cpu_temp_grade(&priv->minc, &priv->maxc);
172 priv->critical = priv->maxc - TEMPERATURE_HOT_DELTA;
175 enable_thermal_clk();
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,