blob: 4fba7b527c6b8be96ede822692f4ae771ae26d84 [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -060023#include <log.h>
Akshay Saraswat39d182d2013-02-25 01:13:00 +000024#include <tmu.h>
25#include <asm/arch/tmu.h>
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +000026#include <asm/arch/power.h>
Akshay Saraswat39d182d2013-02-25 01:13:00 +000027
28#define TRIMINFO_RELOAD 1
29#define CORE_EN 1
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +000030#define THERM_TRIP_EN (1 << 12)
Akshay Saraswat39d182d2013-02-25 01:13:00 +000031
32#define INTEN_RISE0 1
33#define INTEN_RISE1 (1 << 4)
34#define INTEN_RISE2 (1 << 8)
35#define INTEN_FALL0 (1 << 16)
36#define INTEN_FALL1 (1 << 20)
37#define INTEN_FALL2 (1 << 24)
38
39#define TRIM_INFO_MASK 0xff
40
41#define INTCLEAR_RISE0 1
42#define INTCLEAR_RISE1 (1 << 4)
43#define INTCLEAR_RISE2 (1 << 8)
44#define INTCLEAR_FALL0 (1 << 16)
45#define INTCLEAR_FALL1 (1 << 20)
46#define INTCLEAR_FALL2 (1 << 24)
47#define INTCLEARALL (INTCLEAR_RISE0 | INTCLEAR_RISE1 | \
48 INTCLEAR_RISE2 | INTCLEAR_FALL0 | \
49 INTCLEAR_FALL1 | INTCLEAR_FALL2)
50
51/* Tmeperature threshold values for various thermal events */
52struct temperature_params {
53 /* minimum value in temperature code range */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070054 unsigned min_val;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000055 /* maximum value in temperature code range */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070056 unsigned max_val;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000057 /* temperature threshold to start warning */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070058 unsigned start_warning;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000059 /* temperature threshold CPU tripping */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070060 unsigned start_tripping;
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +000061 /* temperature threshold for HW tripping */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070062 unsigned hardware_tripping;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000063};
64
65/* Pre-defined values and thresholds for calibration of current temperature */
66struct tmu_data {
67 /* pre-defined temperature thresholds */
68 struct temperature_params ts;
69 /* pre-defined efuse range minimum value */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070070 unsigned efuse_min_value;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000071 /* pre-defined efuse value for temperature calibration */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070072 unsigned efuse_value;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000073 /* pre-defined efuse range maximum value */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070074 unsigned efuse_max_value;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000075 /* current temperature sensing slope */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070076 unsigned slope;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000077};
78
79/* TMU device specific details and status */
80struct tmu_info {
81 /* base Address for the TMU */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070082 struct exynos5_tmu_reg *tmu_base;
Naveen Krishna Chatradhieeb7d6a2013-04-05 15:21:39 -070083 /* mux Address for the TMU */
84 int tmu_mux;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000085 /* pre-defined values for calibration and thresholds */
86 struct tmu_data data;
87 /* value required for triminfo_25 calibration */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070088 unsigned te1;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000089 /* value required for triminfo_85 calibration */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -070090 unsigned te2;
Akshay Saraswat39d182d2013-02-25 01:13:00 +000091 /* Value for measured data calibration */
92 int dc_value;
93 /* enum value indicating status of the TMU */
94 int tmu_state;
95};
96
97/* Global struct tmu_info variable to store init values */
98static struct tmu_info gbl_info;
99
100/*
101 * Get current temperature code from register,
102 * then calculate and calibrate it's value
103 * in degree celsius.
104 *
105 * @return current temperature of the chip as sensed by TMU
106 */
107static int get_cur_temp(struct tmu_info *info)
108{
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700109 struct exynos5_tmu_reg *reg = info->tmu_base;
110 ulong start;
111 int cur_temp = 0;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000112
113 /*
114 * Temperature code range between min 25 and max 125.
115 * May run more than once for first call as initial sensing
116 * has not yet happened.
117 */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700118 if (info->tmu_state == TMU_STATUS_NORMAL) {
119 start = get_timer(0);
120 do {
121 cur_temp = readl(&reg->current_temp) & 0xff;
122 } while ((cur_temp == 0) || (get_timer(start) > 100));
123 }
124
125 if (cur_temp == 0)
126 return cur_temp;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000127
128 /* Calibrate current temperature */
129 cur_temp = cur_temp - info->te1 + info->dc_value;
130
131 return cur_temp;
132}
133
134/*
135 * Monitors status of the TMU device and exynos temperature
136 *
137 * @param temp pointer to the current temperature value
138 * @return enum tmu_status_t value, code indicating event to execute
139 */
140enum tmu_status_t tmu_monitor(int *temp)
141{
142 int cur_temp;
143 struct tmu_data *data = &gbl_info.data;
144
145 if (gbl_info.tmu_state == TMU_STATUS_INIT)
146 return TMU_STATUS_INIT;
147
148 /* Read current temperature of the SOC */
149 cur_temp = get_cur_temp(&gbl_info);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700150
151 if (!cur_temp)
152 goto out;
153
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000154 *temp = cur_temp;
155
156 /* Temperature code lies between min 25 and max 125 */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700157 if ((cur_temp >= data->ts.start_tripping) &&
158 (cur_temp <= data->ts.max_val))
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000159 return TMU_STATUS_TRIPPED;
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700160
161 if (cur_temp >= data->ts.start_warning)
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000162 return TMU_STATUS_WARNING;
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700163
164 if ((cur_temp < data->ts.start_warning) &&
165 (cur_temp >= data->ts.min_val))
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000166 return TMU_STATUS_NORMAL;
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700167
168 out:
169 /* Temperature code does not lie between min 25 and max 125 */
170 gbl_info.tmu_state = TMU_STATUS_INIT;
171 debug("EXYNOS_TMU: Thermal reading failed\n");
172 return TMU_STATUS_INIT;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000173}
174
175/*
176 * Get TMU specific pre-defined values from FDT
177 *
178 * @param info pointer to the tmu_info struct
179 * @param blob FDT blob
180 * @return int value, 0 for success
181 */
182static int get_tmu_fdt_values(struct tmu_info *info, const void *blob)
183{
Masahiro Yamada0f925822015-08-12 07:31:55 +0900184#if CONFIG_IS_ENABLED(OF_CONTROL)
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700185 fdt_addr_t addr;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000186 int node;
187 int error = 0;
188
189 /* Get the node from FDT for TMU */
190 node = fdtdec_next_compatible(blob, 0,
191 COMPAT_SAMSUNG_EXYNOS_TMU);
192 if (node < 0) {
193 debug("EXYNOS_TMU: No node for tmu in device tree\n");
Jaehoon Chung505cf472016-12-15 20:49:50 +0900194 return -ENODEV;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000195 }
196
197 /*
198 * Get the pre-defined TMU specific values from FDT.
199 * All of these are expected to be correct otherwise
200 * miscalculation of register values in tmu_setup_parameters
201 * may result in misleading current temperature.
202 */
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700203 addr = fdtdec_get_addr(blob, node, "reg");
204 if (addr == FDT_ADDR_T_NONE) {
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000205 debug("%s: Missing tmu-base\n", __func__);
Jaehoon Chung505cf472016-12-15 20:49:50 +0900206 return -ENODEV;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000207 }
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700208 info->tmu_base = (struct exynos5_tmu_reg *)addr;
209
Naveen Krishna Chatradhieeb7d6a2013-04-05 15:21:39 -0700210 /* Optional field. */
211 info->tmu_mux = fdtdec_get_int(blob,
212 node, "samsung,mux", -1);
213 /* Take default value as per the user manual b(110) */
214 if (info->tmu_mux == -1)
215 info->tmu_mux = 0x6;
216
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000217 info->data.ts.min_val = fdtdec_get_int(blob,
218 node, "samsung,min-temp", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700219 error |= (info->data.ts.min_val == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000220 info->data.ts.max_val = fdtdec_get_int(blob,
221 node, "samsung,max-temp", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700222 error |= (info->data.ts.max_val == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000223 info->data.ts.start_warning = fdtdec_get_int(blob,
224 node, "samsung,start-warning", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700225 error |= (info->data.ts.start_warning == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000226 info->data.ts.start_tripping = fdtdec_get_int(blob,
227 node, "samsung,start-tripping", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700228 error |= (info->data.ts.start_tripping == -1);
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +0000229 info->data.ts.hardware_tripping = fdtdec_get_int(blob,
230 node, "samsung,hw-tripping", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700231 error |= (info->data.ts.hardware_tripping == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000232 info->data.efuse_min_value = fdtdec_get_int(blob,
233 node, "samsung,efuse-min-value", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700234 error |= (info->data.efuse_min_value == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000235 info->data.efuse_value = fdtdec_get_int(blob,
236 node, "samsung,efuse-value", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700237 error |= (info->data.efuse_value == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000238 info->data.efuse_max_value = fdtdec_get_int(blob,
239 node, "samsung,efuse-max-value", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700240 error |= (info->data.efuse_max_value == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000241 info->data.slope = fdtdec_get_int(blob,
242 node, "samsung,slope", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700243 error |= (info->data.slope == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000244 info->dc_value = fdtdec_get_int(blob,
245 node, "samsung,dc-value", -1);
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700246 error |= (info->dc_value == -1);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000247
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700248 if (error) {
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000249 debug("fail to get tmu node properties\n");
Jaehoon Chung505cf472016-12-15 20:49:50 +0900250 return -EINVAL;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000251 }
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700252#else
253 /* Non DT support may never be added. Just in case */
Jaehoon Chung505cf472016-12-15 20:49:50 +0900254 return -ENODEV;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000255#endif
256
257 return 0;
258}
259
260/*
261 * Calibrate and calculate threshold values and
262 * enable interrupt levels
263 *
264 * @param info pointer to the tmu_info struct
265 */
266static void tmu_setup_parameters(struct tmu_info *info)
267{
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700268 unsigned te_code, con;
269 unsigned warning_code, trip_code, hwtrip_code;
270 unsigned cooling_temp;
271 unsigned rising_value;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000272 struct tmu_data *data = &info->data;
Naveen Krishna Chatradhi1149ca02013-04-05 15:21:38 -0700273 struct exynos5_tmu_reg *reg = info->tmu_base;
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000274
275 /* Must reload for reading efuse value from triminfo register */
276 writel(TRIMINFO_RELOAD, &reg->triminfo_control);
277
278 /* Get the compensation parameter */
279 te_code = readl(&reg->triminfo);
280 info->te1 = te_code & TRIM_INFO_MASK;
281 info->te2 = ((te_code >> 8) & TRIM_INFO_MASK);
282
283 if ((data->efuse_min_value > info->te1) ||
284 (info->te1 > data->efuse_max_value)
285 || (info->te2 != 0))
286 info->te1 = data->efuse_value;
287
288 /* Get RISING & FALLING Threshold value */
289 warning_code = data->ts.start_warning
290 + info->te1 - info->dc_value;
291 trip_code = data->ts.start_tripping
292 + info->te1 - info->dc_value;
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +0000293 hwtrip_code = data->ts.hardware_tripping
294 + info->te1 - info->dc_value;
295
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000296 cooling_temp = 0;
297
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +0000298 rising_value = ((warning_code << 8) |
299 (trip_code << 16) |
300 (hwtrip_code << 24));
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000301
302 /* Set interrupt level */
303 writel(rising_value, &reg->threshold_temp_rise);
304 writel(cooling_temp, &reg->threshold_temp_fall);
305
306 /*
307 * Init TMU control tuning parameters
308 * [28:24] VREF - Voltage reference
309 * [15:13] THERM_TRIP_MODE - Tripping mode
310 * [12] THERM_TRIP_EN - Thermal tripping enable
311 * [11:8] BUF_SLOPE_SEL - Gain of amplifier
312 * [6] THERM_TRIP_BY_TQ_EN - Tripping by TQ pin
313 */
314 writel(data->slope, &reg->tmu_control);
315
316 writel(INTCLEARALL, &reg->intclear);
317
318 /* TMU core enable */
319 con = readl(&reg->tmu_control);
Naveen Krishna Chatradhieeb7d6a2013-04-05 15:21:39 -0700320 con |= THERM_TRIP_EN | CORE_EN | (info->tmu_mux << 20);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000321
322 writel(con, &reg->tmu_control);
323
Akshay Saraswat3a0b1da2013-02-25 01:13:06 +0000324 /* Enable HW thermal trip */
325 set_hw_thermal_trip();
326
327 /* LEV1 LEV2 interrupt enable */
328 writel(INTEN_RISE1 | INTEN_RISE2, &reg->inten);
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000329}
330
331/*
332 * Initialize TMU device
333 *
334 * @param blob FDT blob
335 * @return int value, 0 for success
336 */
337int tmu_init(const void *blob)
338{
339 gbl_info.tmu_state = TMU_STATUS_INIT;
340 if (get_tmu_fdt_values(&gbl_info, blob) < 0)
341 goto ret;
342
343 tmu_setup_parameters(&gbl_info);
344 gbl_info.tmu_state = TMU_STATUS_NORMAL;
345ret:
Akshay Saraswat39d182d2013-02-25 01:13:00 +0000346 return gbl_info.tmu_state;
347}