blob: 42ca8d0b6b3de9ac51d7f33689c91a283857e844 [file] [log] [blame]
Ye.Lid049afa2014-11-20 21:14:13 +08001/*
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>
Tim Harvey291f73b2015-05-18 06:56:47 -070015#include <asm/arch/sys_proto.h>
Ye.Lid049afa2014-11-20 21:14:13 +080016#include <dm.h>
17#include <errno.h>
18#include <malloc.h>
19#include <thermal.h>
20#include <imx_thermal.h>
21
Tim Harveyffaa3a12015-05-21 08:40:06 -070022/* board will busyloop until this many degrees C below CPU max temperature */
23#define TEMPERATURE_HOT_DELTA 5 /* CPU maxT - 5C */
Ye.Lid049afa2014-11-20 21:14:13 +080024#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
Tim Harvey291f73b2015-05-18 06:56:47 -070037struct thermal_data {
38 unsigned int fuse;
Tim Harveyffaa3a12015-05-21 08:40:06 -070039 int critical;
Tim Harvey291f73b2015-05-18 06:56:47 -070040 int minc;
41 int maxc;
42};
43
Ye.Lid049afa2014-11-20 21:14:13 +080044static 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 = (struct anatop_regs *)pdata->regs;
Tim Harvey291f73b2015-05-18 06:56:47 -070050 struct thermal_data *priv = dev_get_priv(dev);
51 u32 fuse = priv->fuse;
Ye.Lid049afa2014-11-20 21:14:13 +080052 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 reg = readl(&anatop->tempsense1);
97 reg &= ~TEMPSENSE1_MEASURE_FREQ;
98 reg |= MEASURE_FREQ;
99 writel(reg, &anatop->tempsense1);
100
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);
105
106 /* make sure that the latest temp is valid */
107 while ((readl(&anatop->tempsense0) &
108 TEMPSENSE0_FINISHED) == 0)
109 udelay(10000);
110
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);
116
117 /* milli_Tmeas = c2 - Nmeas * c1 */
118 temperature = (c2 - n_meas * c1)/1000;
119
120 /* power down anatop thermal sensor */
121 writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
122 writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_clr);
123
124 return temperature;
125}
126
127int imx_thermal_get_temp(struct udevice *dev, int *temp)
128{
Tim Harvey291f73b2015-05-18 06:56:47 -0700129 struct thermal_data *priv = dev_get_priv(dev);
Ye.Lid049afa2014-11-20 21:14:13 +0800130 int cpu_tmp = 0;
131
132 cpu_tmp = read_cpu_temperature(dev);
Tim Harveyf802d212015-06-09 06:40:22 -0700133 while (cpu_tmp >= priv->critical) {
134 printf("CPU Temperature (%dC) too close to max (%dC)",
135 cpu_tmp, priv->maxc);
136 puts(" waiting...\n");
137 udelay(5000000);
138 cpu_tmp = read_cpu_temperature(dev);
Ye.Lid049afa2014-11-20 21:14:13 +0800139 }
140
141 *temp = cpu_tmp;
142
143 return 0;
144}
145
146static const struct dm_thermal_ops imx_thermal_ops = {
147 .get_temp = imx_thermal_get_temp,
148};
149
150static int imx_thermal_probe(struct udevice *dev)
151{
152 unsigned int fuse = ~0;
153
154 const struct imx_thermal_plat *pdata = dev_get_platdata(dev);
Tim Harvey291f73b2015-05-18 06:56:47 -0700155 struct thermal_data *priv = dev_get_priv(dev);
Ye.Lid049afa2014-11-20 21:14:13 +0800156
157 /* Read Temperature calibration data fuse */
158 fuse_read(pdata->fuse_bank, pdata->fuse_word, &fuse);
159
160 /* Check for valid fuse */
161 if (fuse == 0 || fuse == ~0) {
162 printf("CPU: Thermal invalid data, fuse: 0x%x\n", fuse);
163 return -EPERM;
Ye.Lid049afa2014-11-20 21:14:13 +0800164 }
165
Tim Harveyffaa3a12015-05-21 08:40:06 -0700166 /* set critical cooling temp */
Tim Harvey291f73b2015-05-18 06:56:47 -0700167 get_cpu_temp_grade(&priv->minc, &priv->maxc);
Tim Harveyffaa3a12015-05-21 08:40:06 -0700168 priv->critical = priv->maxc - TEMPERATURE_HOT_DELTA;
Tim Harvey291f73b2015-05-18 06:56:47 -0700169 priv->fuse = fuse;
Ye.Lid049afa2014-11-20 21:14:13 +0800170
171 enable_thermal_clk();
172
173 return 0;
174}
175
176U_BOOT_DRIVER(imx_thermal) = {
177 .name = "imx_thermal",
178 .id = UCLASS_THERMAL,
179 .ops = &imx_thermal_ops,
180 .probe = imx_thermal_probe,
Tim Harvey291f73b2015-05-18 06:56:47 -0700181 .priv_auto_alloc_size = sizeof(struct thermal_data),
Ye.Lid049afa2014-11-20 21:14:13 +0800182 .flags = DM_FLAG_PRE_RELOC,
183};