blob: 6a662677a7cb0133ef37b5f355ceee2cb9a9054c [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 */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070053 unsigned min_val;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000054 /* maximum value in temperature code range */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070055 unsigned max_val;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000056 /* temperature threshold to start warning */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070057 unsigned start_warning;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000058 /* temperature threshold CPU tripping */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070059 unsigned start_tripping;
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +000060 /* temperature threshold for HW tripping */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070061 unsigned 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 */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070069 unsigned efuse_min_value;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000070 /* pre-defined efuse value for temperature calibration */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070071 unsigned efuse_value;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000072 /* pre-defined efuse range maximum value */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070073 unsigned efuse_max_value;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000074 /* current temperature sensing slope */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070075 unsigned slope;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000076};
77
78/* TMU device specific details and status */
79struct tmu_info {
80 /* base Address for the TMU */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070081 struct exynos5_tmu_reg *tmu_base;
Naveen Krishna Chatradhieeb7d6a2013-04-05 15:21:39 -070082 /* mux Address for the TMU */
83 int tmu_mux;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000084 /* pre-defined values for calibration and thresholds */
85 struct tmu_data data;
86 /* value required for triminfo_25 calibration */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070087 unsigned te1;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000088 /* value required for triminfo_85 calibration */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070089 unsigned te2;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000090 /* Value for measured data calibration */
91 int dc_value;
92 /* enum value indicating status of the TMU */
93 int tmu_state;
94};
95
96/* Global struct tmu_info variable to store init values */
97static struct tmu_info gbl_info;
98
99/*
100 * Get current temperature code from register,
101 * then calculate and calibrate it's value
102 * in degree celsius.
103 *
104 * @return current temperature of the chip as sensed by TMU
105 */
106static int get_cur_temp(struct tmu_info *info)
107{
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700108 struct exynos5_tmu_reg *reg = info->tmu_base;
109 ulong start;
110 int cur_temp = 0;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000111
112 /*
113 * Temperature code range between min 25 and max 125.
114 * May run more than once for first call as initial sensing
115 * has not yet happened.
116 */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700117 if (info->tmu_state == TMU_STATUS_NORMAL) {
118 start = get_timer(0);
119 do {
120 cur_temp = readl(&reg->current_temp) & 0xff;
121 } while ((cur_temp == 0) || (get_timer(start) > 100));
122 }
123
124 if (cur_temp == 0)
125 return cur_temp;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000126
127 /* Calibrate current temperature */
128 cur_temp = cur_temp - info->te1 + info->dc_value;
129
130 return cur_temp;
131}
132
133/*
134 * Monitors status of the TMU device and exynos temperature
135 *
136 * @param temp pointer to the current temperature value
137 * @return enum tmu_status_t value, code indicating event to execute
138 */
139enum tmu_status_t tmu_monitor(int *temp)
140{
141 int cur_temp;
142 struct tmu_data *data = &gbl_info.data;
143
144 if (gbl_info.tmu_state == TMU_STATUS_INIT)
145 return TMU_STATUS_INIT;
146
147 /* Read current temperature of the SOC */
148 cur_temp = get_cur_temp(&gbl_info);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700149
150 if (!cur_temp)
151 goto out;
152
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000153 *temp = cur_temp;
154
155 /* Temperature code lies between min 25 and max 125 */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700156 if ((cur_temp >= data->ts.start_tripping) &&
157 (cur_temp <= data->ts.max_val))
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000158 return TMU_STATUS_TRIPPED;
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700159
160 if (cur_temp >= data->ts.start_warning)
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000161 return TMU_STATUS_WARNING;
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700162
163 if ((cur_temp < data->ts.start_warning) &&
164 (cur_temp >= data->ts.min_val))
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000165 return TMU_STATUS_NORMAL;
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700166
167 out:
168 /* Temperature code does not lie between min 25 and max 125 */
169 gbl_info.tmu_state = TMU_STATUS_INIT;
170 debug("EXYNOS_TMU: Thermal reading failed\n");
171 return TMU_STATUS_INIT;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000172}
173
174/*
175 * Get TMU specific pre-defined values from FDT
176 *
177 * @param info pointer to the tmu_info struct
178 * @param blob FDT blob
179 * @return int value, 0 for success
180 */
181static int get_tmu_fdt_values(struct tmu_info *info, const void *blob)
182{
Masahiro Yamada0f925822015-08-12 07:31:55 +0900183#if CONFIG_IS_ENABLED(OF_CONTROL)
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700184 fdt_addr_t addr;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000185 int node;
186 int error = 0;
187
188 /* Get the node from FDT for TMU */
189 node = fdtdec_next_compatible(blob, 0,
190 COMPAT_SAMSUNG_EXYNOS_TMU);
191 if (node < 0) {
192 debug("EXYNOS_TMU: No node for tmu in device tree\n");
Jaehoon Chung505cf472016-12-15 20:49:50 +0900193 return -ENODEV;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000194 }
195
196 /*
197 * Get the pre-defined TMU specific values from FDT.
198 * All of these are expected to be correct otherwise
199 * miscalculation of register values in tmu_setup_parameters
200 * may result in misleading current temperature.
201 */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700202 addr = fdtdec_get_addr(blob, node, "reg");
203 if (addr == FDT_ADDR_T_NONE) {
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000204 debug("%s: Missing tmu-base\n", __func__);
Jaehoon Chung505cf472016-12-15 20:49:50 +0900205 return -ENODEV;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000206 }
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700207 info->tmu_base = (struct exynos5_tmu_reg *)addr;
208
Naveen Krishna Chatradhieeb7d6a2013-04-05 15:21:39 -0700209 /* Optional field. */
210 info->tmu_mux = fdtdec_get_int(blob,
211 node, "samsung,mux", -1);
212 /* Take default value as per the user manual b(110) */
213 if (info->tmu_mux == -1)
214 info->tmu_mux = 0x6;
215
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000216 info->data.ts.min_val = fdtdec_get_int(blob,
217 node, "samsung,min-temp", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700218 error |= (info->data.ts.min_val == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000219 info->data.ts.max_val = fdtdec_get_int(blob,
220 node, "samsung,max-temp", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700221 error |= (info->data.ts.max_val == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000222 info->data.ts.start_warning = fdtdec_get_int(blob,
223 node, "samsung,start-warning", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700224 error |= (info->data.ts.start_warning == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000225 info->data.ts.start_tripping = fdtdec_get_int(blob,
226 node, "samsung,start-tripping", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700227 error |= (info->data.ts.start_tripping == -1);
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +0000228 info->data.ts.hardware_tripping = fdtdec_get_int(blob,
229 node, "samsung,hw-tripping", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700230 error |= (info->data.ts.hardware_tripping == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000231 info->data.efuse_min_value = fdtdec_get_int(blob,
232 node, "samsung,efuse-min-value", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700233 error |= (info->data.efuse_min_value == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000234 info->data.efuse_value = fdtdec_get_int(blob,
235 node, "samsung,efuse-value", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700236 error |= (info->data.efuse_value == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000237 info->data.efuse_max_value = fdtdec_get_int(blob,
238 node, "samsung,efuse-max-value", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700239 error |= (info->data.efuse_max_value == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000240 info->data.slope = fdtdec_get_int(blob,
241 node, "samsung,slope", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700242 error |= (info->data.slope == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000243 info->dc_value = fdtdec_get_int(blob,
244 node, "samsung,dc-value", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700245 error |= (info->dc_value == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000246
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700247 if (error) {
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000248 debug("fail to get tmu node properties\n");
Jaehoon Chung505cf472016-12-15 20:49:50 +0900249 return -EINVAL;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000250 }
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700251#else
252 /* Non DT support may never be added. Just in case */
Jaehoon Chung505cf472016-12-15 20:49:50 +0900253 return -ENODEV;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000254#endif
255
256 return 0;
257}
258
259/*
260 * Calibrate and calculate threshold values and
261 * enable interrupt levels
262 *
263 * @param info pointer to the tmu_info struct
264 */
265static void tmu_setup_parameters(struct tmu_info *info)
266{
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700267 unsigned te_code, con;
268 unsigned warning_code, trip_code, hwtrip_code;
269 unsigned cooling_temp;
270 unsigned rising_value;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000271 struct tmu_data *data = &info->data;
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700272 struct exynos5_tmu_reg *reg = info->tmu_base;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000273
274 /* Must reload for reading efuse value from triminfo register */
275 writel(TRIMINFO_RELOAD, &reg->triminfo_control);
276
277 /* Get the compensation parameter */
278 te_code = readl(&reg->triminfo);
279 info->te1 = te_code & TRIM_INFO_MASK;
280 info->te2 = ((te_code >> 8) & TRIM_INFO_MASK);
281
282 if ((data->efuse_min_value > info->te1) ||
283 (info->te1 > data->efuse_max_value)
284 || (info->te2 != 0))
285 info->te1 = data->efuse_value;
286
287 /* Get RISING & FALLING Threshold value */
288 warning_code = data->ts.start_warning
289 + info->te1 - info->dc_value;
290 trip_code = data->ts.start_tripping
291 + info->te1 - info->dc_value;
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +0000292 hwtrip_code = data->ts.hardware_tripping
293 + info->te1 - info->dc_value;
294
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000295 cooling_temp = 0;
296
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +0000297 rising_value = ((warning_code << 8) |
298 (trip_code << 16) |
299 (hwtrip_code << 24));
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000300
301 /* Set interrupt level */
302 writel(rising_value, &reg->threshold_temp_rise);
303 writel(cooling_temp, &reg->threshold_temp_fall);
304
305 /*
306 * Init TMU control tuning parameters
307 * [28:24] VREF - Voltage reference
308 * [15:13] THERM_TRIP_MODE - Tripping mode
309 * [12] THERM_TRIP_EN - Thermal tripping enable
310 * [11:8] BUF_SLOPE_SEL - Gain of amplifier
311 * [6] THERM_TRIP_BY_TQ_EN - Tripping by TQ pin
312 */
313 writel(data->slope, &reg->tmu_control);
314
315 writel(INTCLEARALL, &reg->intclear);
316
317 /* TMU core enable */
318 con = readl(&reg->tmu_control);
Naveen Krishna Chatradhieeb7d6a2013-04-05 15:21:39 -0700319 con |= THERM_TRIP_EN | CORE_EN | (info->tmu_mux << 20);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000320
321 writel(con, &reg->tmu_control);
322
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +0000323 /* Enable HW thermal trip */
324 set_hw_thermal_trip();
325
326 /* LEV1 LEV2 interrupt enable */
327 writel(INTEN_RISE1 | INTEN_RISE2, &reg->inten);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000328}
329
330/*
331 * Initialize TMU device
332 *
333 * @param blob FDT blob
334 * @return int value, 0 for success
335 */
336int tmu_init(const void *blob)
337{
338 gbl_info.tmu_state = TMU_STATUS_INIT;
339 if (get_tmu_fdt_values(&gbl_info, blob) < 0)
340 goto ret;
341
342 tmu_setup_parameters(&gbl_info);
343 gbl_info.tmu_state = TMU_STATUS_NORMAL;
344ret:
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000345 return gbl_info.tmu_state;
346}