]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - drivers/thermal/imx_thermal.c
thermal: imx_thermal: use CPU temperature grade for trip points
[karo-tx-uboot.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 #define FACTOR0                 10000000
23 #define FACTOR1                 15976
24 #define FACTOR2                 4297157
25 #define MEASURE_FREQ            327
26
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
34
35 struct thermal_data {
36         unsigned int fuse;
37         int passive;
38         int minc;
39         int maxc;
40 };
41
42 static int read_cpu_temperature(struct udevice *dev)
43 {
44         int temperature;
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;
50         int t1, n1;
51         u32 c1, c2;
52         u64 temp64;
53
54         /*
55          * Sensor data layout:
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)
59          */
60         n1 = fuse >> 20;
61         t1 = 25; /* t1 always 25C */
62
63         /*
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
78          */
79         temp64 = FACTOR0;
80         temp64 *= 1000;
81         do_div(temp64, FACTOR1 * n1 - FACTOR2);
82         c1 = temp64;
83         c2 = n1 * c1 + 1000 * t1;
84
85         /*
86          * now we only use single measure, every time we read
87          * the temperature, we will power on/down anadig thermal
88          * module
89          */
90         writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_clr);
91         writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_set);
92
93         /* setup measure freq */
94         reg = readl(&anatop->tempsense1);
95         reg &= ~TEMPSENSE1_MEASURE_FREQ;
96         reg |= MEASURE_FREQ;
97         writel(reg, &anatop->tempsense1);
98
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);
103
104         /* make sure that the latest temp is valid */
105         while ((readl(&anatop->tempsense0) &
106                 TEMPSENSE0_FINISHED) == 0)
107                 udelay(10000);
108
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);
114
115         /* milli_Tmeas = c2 - Nmeas * c1 */
116         temperature = (c2 - n_meas * c1)/1000;
117
118         /* power down anatop thermal sensor */
119         writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
120         writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_clr);
121
122         return temperature;
123 }
124
125 int imx_thermal_get_temp(struct udevice *dev, int *temp)
126 {
127         struct thermal_data *priv = dev_get_priv(dev);
128         int cpu_tmp = 0;
129
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",
134                                cpu_tmp);
135                         udelay(5000000);
136                         cpu_tmp = read_cpu_temperature(dev);
137                 } else {
138                         break;
139                 }
140         }
141
142         *temp = cpu_tmp;
143
144         return 0;
145 }
146
147 static const struct dm_thermal_ops imx_thermal_ops = {
148         .get_temp       = imx_thermal_get_temp,
149 };
150
151 static int imx_thermal_probe(struct udevice *dev)
152 {
153         unsigned int fuse = ~0;
154
155         const struct imx_thermal_plat *pdata = dev_get_platdata(dev);
156         struct thermal_data *priv = dev_get_priv(dev);
157
158         /* Read Temperature calibration data fuse */
159         fuse_read(pdata->fuse_bank, pdata->fuse_word, &fuse);
160
161         /* Check for valid fuse */
162         if (fuse == 0 || fuse == ~0) {
163                 printf("CPU:   Thermal invalid data, fuse: 0x%x\n", fuse);
164                 return -EPERM;
165         }
166
167         /* set passive cooling temp to max - 20C */
168         get_cpu_temp_grade(&priv->minc, &priv->maxc);
169         priv->passive = priv->maxc - 20;
170         priv->fuse = fuse;
171
172         enable_thermal_clk();
173
174         return 0;
175 }
176
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,
184 };