blob: d4b3e65a3eac0bfac781572d15e8fe6115c02cce [file] [log] [blame]
Akshay Saraswat39d182d2013-02-25 01:13:00 +00001/*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
4 * Akshay Saraswat <akshay.s@samsung.com>
5 *
6 * EXYNOS - Thermal Management Unit
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17 * MA 02111-1307 USA
18 */
19
20#include <common.h>
21#include <errno.h>
22#include <fdtdec.h>
23#include <tmu.h>
24#include <asm/arch/tmu.h>
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +000025#include <asm/arch/power.h>
Akshay Saraswat39d182d2013-02-25 01:13:00 +000026
27#define TRIMINFO_RELOAD 1
28#define CORE_EN 1
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +000029#define THERM_TRIP_EN (1 << 12)
Akshay Saraswat39d182d2013-02-25 01:13:00 +000030
31#define INTEN_RISE0 1
32#define INTEN_RISE1 (1 << 4)
33#define INTEN_RISE2 (1 << 8)
34#define INTEN_FALL0 (1 << 16)
35#define INTEN_FALL1 (1 << 20)
36#define INTEN_FALL2 (1 << 24)
37
38#define TRIM_INFO_MASK 0xff
39
40#define INTCLEAR_RISE0 1
41#define INTCLEAR_RISE1 (1 << 4)
42#define INTCLEAR_RISE2 (1 << 8)
43#define INTCLEAR_FALL0 (1 << 16)
44#define INTCLEAR_FALL1 (1 << 20)
45#define INTCLEAR_FALL2 (1 << 24)
46#define INTCLEARALL (INTCLEAR_RISE0 | INTCLEAR_RISE1 | \
47 INTCLEAR_RISE2 | INTCLEAR_FALL0 | \
48 INTCLEAR_FALL1 | INTCLEAR_FALL2)
49
50/* Tmeperature threshold values for various thermal events */
51struct temperature_params {
52 /* minimum value in temperature code range */
53 unsigned int min_val;
54 /* maximum value in temperature code range */
55 unsigned int max_val;
56 /* temperature threshold to start warning */
57 unsigned int start_warning;
58 /* temperature threshold CPU tripping */
59 unsigned int start_tripping;
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +000060 /* temperature threshold for HW tripping */
61 unsigned int hardware_tripping;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000062};
63
64/* Pre-defined values and thresholds for calibration of current temperature */
65struct tmu_data {
66 /* pre-defined temperature thresholds */
67 struct temperature_params ts;
68 /* pre-defined efuse range minimum value */
69 unsigned int efuse_min_value;
70 /* pre-defined efuse value for temperature calibration */
71 unsigned int efuse_value;
72 /* pre-defined efuse range maximum value */
73 unsigned int efuse_max_value;
74 /* current temperature sensing slope */
75 unsigned int slope;
76};
77
78/* TMU device specific details and status */
79struct tmu_info {
80 /* base Address for the TMU */
81 unsigned tmu_base;
82 /* pre-defined values for calibration and thresholds */
83 struct tmu_data data;
84 /* value required for triminfo_25 calibration */
85 unsigned int te1;
86 /* value required for triminfo_85 calibration */
87 unsigned int te2;
88 /* Value for measured data calibration */
89 int dc_value;
90 /* enum value indicating status of the TMU */
91 int tmu_state;
92};
93
94/* Global struct tmu_info variable to store init values */
95static struct tmu_info gbl_info;
96
97/*
98 * Get current temperature code from register,
99 * then calculate and calibrate it's value
100 * in degree celsius.
101 *
102 * @return current temperature of the chip as sensed by TMU
103 */
104static int get_cur_temp(struct tmu_info *info)
105{
106 int cur_temp;
107 struct exynos5_tmu_reg *reg = (struct exynos5_tmu_reg *)info->tmu_base;
108
109 /*
110 * Temperature code range between min 25 and max 125.
111 * May run more than once for first call as initial sensing
112 * has not yet happened.
113 */
114 do {
115 cur_temp = readl(&reg->current_temp) & 0xff;
116 } while (cur_temp == 0 && info->tmu_state == TMU_STATUS_NORMAL);
117
118 /* Calibrate current temperature */
119 cur_temp = cur_temp - info->te1 + info->dc_value;
120
121 return cur_temp;
122}
123
124/*
125 * Monitors status of the TMU device and exynos temperature
126 *
127 * @param temp pointer to the current temperature value
128 * @return enum tmu_status_t value, code indicating event to execute
129 */
130enum tmu_status_t tmu_monitor(int *temp)
131{
132 int cur_temp;
133 struct tmu_data *data = &gbl_info.data;
134
135 if (gbl_info.tmu_state == TMU_STATUS_INIT)
136 return TMU_STATUS_INIT;
137
138 /* Read current temperature of the SOC */
139 cur_temp = get_cur_temp(&gbl_info);
140 *temp = cur_temp;
141
142 /* Temperature code lies between min 25 and max 125 */
143 if (cur_temp >= data->ts.start_tripping &&
144 cur_temp <= data->ts.max_val) {
145 return TMU_STATUS_TRIPPED;
146 } else if (cur_temp >= data->ts.start_warning) {
147 return TMU_STATUS_WARNING;
148 } else if (cur_temp < data->ts.start_warning &&
149 cur_temp >= data->ts.min_val) {
150 return TMU_STATUS_NORMAL;
151 } else {
152 /* Temperature code does not lie between min 25 and max 125 */
153 gbl_info.tmu_state = TMU_STATUS_INIT;
154 debug("EXYNOS_TMU: Thermal reading failed\n");
155 return TMU_STATUS_INIT;
156 }
157}
158
159/*
160 * Get TMU specific pre-defined values from FDT
161 *
162 * @param info pointer to the tmu_info struct
163 * @param blob FDT blob
164 * @return int value, 0 for success
165 */
166static int get_tmu_fdt_values(struct tmu_info *info, const void *blob)
167{
168#ifdef CONFIG_OF_CONTROL
169 int node;
170 int error = 0;
171
172 /* Get the node from FDT for TMU */
173 node = fdtdec_next_compatible(blob, 0,
174 COMPAT_SAMSUNG_EXYNOS_TMU);
175 if (node < 0) {
176 debug("EXYNOS_TMU: No node for tmu in device tree\n");
177 return -1;
178 }
179
180 /*
181 * Get the pre-defined TMU specific values from FDT.
182 * All of these are expected to be correct otherwise
183 * miscalculation of register values in tmu_setup_parameters
184 * may result in misleading current temperature.
185 */
186 info->tmu_base = fdtdec_get_addr(blob, node, "reg");
187 if (info->tmu_base == FDT_ADDR_T_NONE) {
188 debug("%s: Missing tmu-base\n", __func__);
189 return -1;
190 }
191 info->data.ts.min_val = fdtdec_get_int(blob,
192 node, "samsung,min-temp", -1);
193 error |= info->data.ts.min_val;
194 info->data.ts.max_val = fdtdec_get_int(blob,
195 node, "samsung,max-temp", -1);
196 error |= info->data.ts.max_val;
197 info->data.ts.start_warning = fdtdec_get_int(blob,
198 node, "samsung,start-warning", -1);
199 error |= info->data.ts.start_warning;
200 info->data.ts.start_tripping = fdtdec_get_int(blob,
201 node, "samsung,start-tripping", -1);
202 error |= info->data.ts.start_tripping;
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +0000203 info->data.ts.hardware_tripping = fdtdec_get_int(blob,
204 node, "samsung,hw-tripping", -1);
205 error |= info->data.ts.hardware_tripping;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000206 info->data.efuse_min_value = fdtdec_get_int(blob,
207 node, "samsung,efuse-min-value", -1);
208 error |= info->data.efuse_min_value;
209 info->data.efuse_value = fdtdec_get_int(blob,
210 node, "samsung,efuse-value", -1);
211 error |= info->data.efuse_value;
212 info->data.efuse_max_value = fdtdec_get_int(blob,
213 node, "samsung,efuse-max-value", -1);
214 error |= info->data.efuse_max_value;
215 info->data.slope = fdtdec_get_int(blob,
216 node, "samsung,slope", -1);
217 error |= info->data.slope;
218 info->dc_value = fdtdec_get_int(blob,
219 node, "samsung,dc-value", -1);
220 error |= info->dc_value;
221
222 if (error == -1) {
223 debug("fail to get tmu node properties\n");
224 return -1;
225 }
226#endif
227
228 return 0;
229}
230
231/*
232 * Calibrate and calculate threshold values and
233 * enable interrupt levels
234 *
235 * @param info pointer to the tmu_info struct
236 */
237static void tmu_setup_parameters(struct tmu_info *info)
238{
239 unsigned int te_code, con;
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +0000240 unsigned int warning_code, trip_code, hwtrip_code;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000241 unsigned int cooling_temp;
242 unsigned int rising_value;
243 struct tmu_data *data = &info->data;
244 struct exynos5_tmu_reg *reg = (struct exynos5_tmu_reg *)info->tmu_base;
245
246 /* Must reload for reading efuse value from triminfo register */
247 writel(TRIMINFO_RELOAD, &reg->triminfo_control);
248
249 /* Get the compensation parameter */
250 te_code = readl(&reg->triminfo);
251 info->te1 = te_code & TRIM_INFO_MASK;
252 info->te2 = ((te_code >> 8) & TRIM_INFO_MASK);
253
254 if ((data->efuse_min_value > info->te1) ||
255 (info->te1 > data->efuse_max_value)
256 || (info->te2 != 0))
257 info->te1 = data->efuse_value;
258
259 /* Get RISING & FALLING Threshold value */
260 warning_code = data->ts.start_warning
261 + info->te1 - info->dc_value;
262 trip_code = data->ts.start_tripping
263 + info->te1 - info->dc_value;
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +0000264 hwtrip_code = data->ts.hardware_tripping
265 + info->te1 - info->dc_value;
266
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000267 cooling_temp = 0;
268
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +0000269 rising_value = ((warning_code << 8) |
270 (trip_code << 16) |
271 (hwtrip_code << 24));
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000272
273 /* Set interrupt level */
274 writel(rising_value, &reg->threshold_temp_rise);
275 writel(cooling_temp, &reg->threshold_temp_fall);
276
277 /*
278 * Init TMU control tuning parameters
279 * [28:24] VREF - Voltage reference
280 * [15:13] THERM_TRIP_MODE - Tripping mode
281 * [12] THERM_TRIP_EN - Thermal tripping enable
282 * [11:8] BUF_SLOPE_SEL - Gain of amplifier
283 * [6] THERM_TRIP_BY_TQ_EN - Tripping by TQ pin
284 */
285 writel(data->slope, &reg->tmu_control);
286
287 writel(INTCLEARALL, &reg->intclear);
288
289 /* TMU core enable */
290 con = readl(&reg->tmu_control);
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +0000291 con |= THERM_TRIP_EN | CORE_EN;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000292
293 writel(con, &reg->tmu_control);
294
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +0000295 /* Enable HW thermal trip */
296 set_hw_thermal_trip();
297
298 /* LEV1 LEV2 interrupt enable */
299 writel(INTEN_RISE1 | INTEN_RISE2, &reg->inten);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000300}
301
302/*
303 * Initialize TMU device
304 *
305 * @param blob FDT blob
306 * @return int value, 0 for success
307 */
308int tmu_init(const void *blob)
309{
310 gbl_info.tmu_state = TMU_STATUS_INIT;
311 if (get_tmu_fdt_values(&gbl_info, blob) < 0)
312 goto ret;
313
314 tmu_setup_parameters(&gbl_info);
315 gbl_info.tmu_state = TMU_STATUS_NORMAL;
316ret:
317
318 return gbl_info.tmu_state;
319}