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 #define FACTOR0 10000000
24 #define FACTOR2 4297157
25 #define MEASURE_FREQ 327
27 #define TEMPSENSE0_TEMP_CNT_SHIFT 8
28 #define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
29 #define TEMPSENSE0_FINISHED (1 << 2)
30 #define TEMPSENSE0_MEASURE_TEMP (1 << 1)
31 #define TEMPSENSE0_POWER_DOWN (1 << 0)
32 #define MISC0_REFTOP_SELBIASOFF (1 << 3)
33 #define TEMPSENSE1_MEASURE_FREQ 0xffff
42 static int read_cpu_temperature(struct udevice *dev)
45 unsigned int reg, n_meas;
46 const struct imx_thermal_plat *pdata = dev_get_platdata(dev);
47 struct anatop_regs *anatop = (struct anatop_regs *)pdata->regs;
48 struct thermal_data *priv = dev_get_priv(dev);
49 u32 fuse = priv->fuse;
56 * [31:20] - sensor value @ 25C
57 * We use universal formula now and only need sensor value @ 25C
58 * slope = 0.4297157 - (0.0015976 * 25C fuse)
61 t1 = 25; /* t1 always 25C */
64 * Derived from linear interpolation:
65 * slope = 0.4297157 - (0.0015976 * 25C fuse)
66 * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
67 * (Nmeas - n1) / (Tmeas - t1) = slope
68 * We want to reduce this down to the minimum computation necessary
69 * for each temperature read. Also, we want Tmeas in millicelsius
70 * and we don't want to lose precision from integer division. So...
71 * Tmeas = (Nmeas - n1) / slope + t1
72 * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1
73 * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1
74 * Let constant c1 = (-1000 / slope)
75 * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1
76 * Let constant c2 = n1 *c1 + 1000 * t1
77 * milli_Tmeas = c2 - Nmeas * c1
81 do_div(temp64, FACTOR1 * n1 - FACTOR2);
83 c2 = n1 * c1 + 1000 * t1;
86 * now we only use single measure, every time we read
87 * the temperature, we will power on/down anadig thermal
90 writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_clr);
91 writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_set);
93 /* setup measure freq */
94 reg = readl(&anatop->tempsense1);
95 reg &= ~TEMPSENSE1_MEASURE_FREQ;
97 writel(reg, &anatop->tempsense1);
99 /* start the measurement process */
100 writel(TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_clr);
101 writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
102 writel(TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_set);
104 /* make sure that the latest temp is valid */
105 while ((readl(&anatop->tempsense0) &
106 TEMPSENSE0_FINISHED) == 0)
109 /* read temperature count */
110 reg = readl(&anatop->tempsense0);
111 n_meas = (reg & TEMPSENSE0_TEMP_CNT_MASK)
112 >> TEMPSENSE0_TEMP_CNT_SHIFT;
113 writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
115 /* milli_Tmeas = c2 - Nmeas * c1 */
116 temperature = (c2 - n_meas * c1)/1000;
118 /* power down anatop thermal sensor */
119 writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
120 writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_clr);
125 int imx_thermal_get_temp(struct udevice *dev, int *temp)
127 struct thermal_data *priv = dev_get_priv(dev);
130 cpu_tmp = read_cpu_temperature(dev);
131 while (cpu_tmp > priv->minc && cpu_tmp < priv->maxc) {
132 if (cpu_tmp >= priv->passive) {
133 printf("CPU Temperature is %d C, too hot to boot, waiting...\n",
136 cpu_tmp = read_cpu_temperature(dev);
147 static const struct dm_thermal_ops imx_thermal_ops = {
148 .get_temp = imx_thermal_get_temp,
151 static int imx_thermal_probe(struct udevice *dev)
153 unsigned int fuse = ~0;
155 const struct imx_thermal_plat *pdata = dev_get_platdata(dev);
156 struct thermal_data *priv = dev_get_priv(dev);
158 /* Read Temperature calibration data fuse */
159 fuse_read(pdata->fuse_bank, pdata->fuse_word, &fuse);
161 /* Check for valid fuse */
162 if (fuse == 0 || fuse == ~0) {
163 printf("CPU: Thermal invalid data, fuse: 0x%x\n", fuse);
167 /* set passive cooling temp to max - 20C */
168 get_cpu_temp_grade(&priv->minc, &priv->maxc);
169 priv->passive = priv->maxc - 20;
172 enable_thermal_clk();
177 U_BOOT_DRIVER(imx_thermal) = {
178 .name = "imx_thermal",
179 .id = UCLASS_THERMAL,
180 .ops = &imx_thermal_ops,
181 .probe = imx_thermal_probe,
182 .priv_auto_alloc_size = sizeof(struct thermal_data),
183 .flags = DM_FLAG_PRE_RELOC,