]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/thermal/imx_thermal.c
arm: mx6: use imx6_thermal driver
[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 /* 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 = 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         writel(MEASURE_FREQ, &anatop->tempsense1);
97
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);
102
103         /* make sure that the latest temp is valid */
104         const int max_loops = 30;
105         int loops = 0;
106
107         while (((reg = readl(&anatop->tempsense0)) & TEMPSENSE0_FINISHED) == 0) {
108                 udelay(5);
109                 if (++loops >= max_loops)
110                         break;
111         }
112         if ((readl(&anatop->tempsense0) & TEMPSENSE0_FINISHED) == 0)
113                 return 0;
114
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);
120
121         /* milli_Tmeas = c2 - Nmeas * c1 */
122         temperature = (long)(c2 - n_meas * c1) / 1000;
123
124         /* power down anatop thermal sensor */
125         writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
126         writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_clr);
127
128         return temperature;
129 }
130
131 int imx_thermal_get_temp(struct udevice *dev, int *temp)
132 {
133         struct thermal_data *priv = dev_get_priv(dev);
134         int cpu_tmp = 0;
135
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");
141                 udelay(5000000);
142                 cpu_tmp = read_cpu_temperature(dev);
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         int ret;
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);
160
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);
164                 return ret;
165         }
166
167         /* Check for valid fuse */
168         if (fuse == 0 || fuse == ~0) {
169                 static int first = 1;
170
171                 if (first) {
172                         printf("CPU:   Thermal invalid data, fuse: %#10x\n", fuse);
173                         first = 0;
174                 }
175                 return -EINVAL;
176         }
177
178         /* set critical cooling temp */
179         get_cpu_temp_grade(&priv->minc, &priv->maxc);
180         priv->critical = priv->maxc - TEMPERATURE_HOT_DELTA;
181         priv->fuse = fuse;
182
183         enable_thermal_clk();
184
185         return 0;
186 }
187
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,
195 };