blob: 21c2fabce1b888fa3cb127b22eebb955ddef46c5 [file] [log] [blame]
Akshay Saraswat32e61e22013-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
Akshay Saraswat32e61e22013-02-25 01:13:00 +000020#include <errno.h>
21#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060022#include <log.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060023#include <time.h>
Akshay Saraswat32e61e22013-02-25 01:13:00 +000024#include <tmu.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060025#include <asm/io.h>
Akshay Saraswat32e61e22013-02-25 01:13:00 +000026#include <asm/arch/tmu.h>
Akshay Saraswatb9fa8c12013-02-25 01:13:06 +000027#include <asm/arch/power.h>
Akshay Saraswat32e61e22013-02-25 01:13:00 +000028
29#define TRIMINFO_RELOAD 1
30#define CORE_EN 1
Akshay Saraswatb9fa8c12013-02-25 01:13:06 +000031#define THERM_TRIP_EN (1 << 12)
Akshay Saraswat32e61e22013-02-25 01:13:00 +000032
33#define INTEN_RISE0 1
34#define INTEN_RISE1 (1 << 4)
35#define INTEN_RISE2 (1 << 8)
36#define INTEN_FALL0 (1 << 16)
37#define INTEN_FALL1 (1 << 20)
38#define INTEN_FALL2 (1 << 24)
39
40#define TRIM_INFO_MASK 0xff
41
42#define INTCLEAR_RISE0 1
43#define INTCLEAR_RISE1 (1 << 4)
44#define INTCLEAR_RISE2 (1 << 8)
45#define INTCLEAR_FALL0 (1 << 16)
46#define INTCLEAR_FALL1 (1 << 20)
47#define INTCLEAR_FALL2 (1 << 24)
48#define INTCLEARALL (INTCLEAR_RISE0 | INTCLEAR_RISE1 | \
49 INTCLEAR_RISE2 | INTCLEAR_FALL0 | \
50 INTCLEAR_FALL1 | INTCLEAR_FALL2)
51
52/* Tmeperature threshold values for various thermal events */
53struct temperature_params {
54 /* minimum value in temperature code range */
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -070055 unsigned min_val;
Akshay Saraswat32e61e22013-02-25 01:13:00 +000056 /* maximum value in temperature code range */
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -070057 unsigned max_val;
Akshay Saraswat32e61e22013-02-25 01:13:00 +000058 /* temperature threshold to start warning */
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -070059 unsigned start_warning;
Akshay Saraswat32e61e22013-02-25 01:13:00 +000060 /* temperature threshold CPU tripping */
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -070061 unsigned start_tripping;
Akshay Saraswatb9fa8c12013-02-25 01:13:06 +000062 /* temperature threshold for HW tripping */
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -070063 unsigned hardware_tripping;
Akshay Saraswat32e61e22013-02-25 01:13:00 +000064};
65
66/* Pre-defined values and thresholds for calibration of current temperature */
67struct tmu_data {
68 /* pre-defined temperature thresholds */
69 struct temperature_params ts;
70 /* pre-defined efuse range minimum value */
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -070071 unsigned efuse_min_value;
Akshay Saraswat32e61e22013-02-25 01:13:00 +000072 /* pre-defined efuse value for temperature calibration */
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -070073 unsigned efuse_value;
Akshay Saraswat32e61e22013-02-25 01:13:00 +000074 /* pre-defined efuse range maximum value */
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -070075 unsigned efuse_max_value;
Akshay Saraswat32e61e22013-02-25 01:13:00 +000076 /* current temperature sensing slope */
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -070077 unsigned slope;
Akshay Saraswat32e61e22013-02-25 01:13:00 +000078};
79
80/* TMU device specific details and status */
81struct tmu_info {
82 /* base Address for the TMU */
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -070083 struct exynos5_tmu_reg *tmu_base;
Naveen Krishna Chatradhib3d364f2013-04-05 15:21:39 -070084 /* mux Address for the TMU */
85 int tmu_mux;
Akshay Saraswat32e61e22013-02-25 01:13:00 +000086 /* pre-defined values for calibration and thresholds */
87 struct tmu_data data;
88 /* value required for triminfo_25 calibration */
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -070089 unsigned te1;
Akshay Saraswat32e61e22013-02-25 01:13:00 +000090 /* value required for triminfo_85 calibration */
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -070091 unsigned te2;
Akshay Saraswat32e61e22013-02-25 01:13:00 +000092 /* Value for measured data calibration */
93 int dc_value;
94 /* enum value indicating status of the TMU */
95 int tmu_state;
96};
97
98/* Global struct tmu_info variable to store init values */
99static struct tmu_info gbl_info;
100
101/*
102 * Get current temperature code from register,
103 * then calculate and calibrate it's value
104 * in degree celsius.
105 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100106 * Return: current temperature of the chip as sensed by TMU
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000107 */
108static int get_cur_temp(struct tmu_info *info)
109{
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700110 struct exynos5_tmu_reg *reg = info->tmu_base;
111 ulong start;
112 int cur_temp = 0;
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000113
114 /*
115 * Temperature code range between min 25 and max 125.
116 * May run more than once for first call as initial sensing
117 * has not yet happened.
118 */
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700119 if (info->tmu_state == TMU_STATUS_NORMAL) {
120 start = get_timer(0);
121 do {
122 cur_temp = readl(&reg->current_temp) & 0xff;
123 } while ((cur_temp == 0) || (get_timer(start) > 100));
124 }
125
126 if (cur_temp == 0)
127 return cur_temp;
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000128
129 /* Calibrate current temperature */
130 cur_temp = cur_temp - info->te1 + info->dc_value;
131
132 return cur_temp;
133}
134
135/*
136 * Monitors status of the TMU device and exynos temperature
137 *
138 * @param temp pointer to the current temperature value
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100139 * Return: enum tmu_status_t value, code indicating event to execute
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000140 */
141enum tmu_status_t tmu_monitor(int *temp)
142{
143 int cur_temp;
144 struct tmu_data *data = &gbl_info.data;
145
146 if (gbl_info.tmu_state == TMU_STATUS_INIT)
147 return TMU_STATUS_INIT;
148
149 /* Read current temperature of the SOC */
150 cur_temp = get_cur_temp(&gbl_info);
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700151
152 if (!cur_temp)
153 goto out;
154
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000155 *temp = cur_temp;
156
157 /* Temperature code lies between min 25 and max 125 */
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700158 if ((cur_temp >= data->ts.start_tripping) &&
159 (cur_temp <= data->ts.max_val))
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000160 return TMU_STATUS_TRIPPED;
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700161
162 if (cur_temp >= data->ts.start_warning)
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000163 return TMU_STATUS_WARNING;
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700164
165 if ((cur_temp < data->ts.start_warning) &&
166 (cur_temp >= data->ts.min_val))
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000167 return TMU_STATUS_NORMAL;
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700168
169 out:
170 /* Temperature code does not lie between min 25 and max 125 */
171 gbl_info.tmu_state = TMU_STATUS_INIT;
172 debug("EXYNOS_TMU: Thermal reading failed\n");
173 return TMU_STATUS_INIT;
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000174}
175
176/*
177 * Get TMU specific pre-defined values from FDT
178 *
179 * @param info pointer to the tmu_info struct
180 * @param blob FDT blob
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100181 * Return: int value, 0 for success
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000182 */
183static int get_tmu_fdt_values(struct tmu_info *info, const void *blob)
184{
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900185#if CONFIG_IS_ENABLED(OF_CONTROL)
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700186 fdt_addr_t addr;
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000187 int node;
188 int error = 0;
189
190 /* Get the node from FDT for TMU */
191 node = fdtdec_next_compatible(blob, 0,
192 COMPAT_SAMSUNG_EXYNOS_TMU);
193 if (node < 0) {
194 debug("EXYNOS_TMU: No node for tmu in device tree\n");
Jaehoon Chung10e80f32016-12-15 20:49:50 +0900195 return -ENODEV;
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000196 }
197
198 /*
199 * Get the pre-defined TMU specific values from FDT.
200 * All of these are expected to be correct otherwise
201 * miscalculation of register values in tmu_setup_parameters
202 * may result in misleading current temperature.
203 */
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700204 addr = fdtdec_get_addr(blob, node, "reg");
205 if (addr == FDT_ADDR_T_NONE) {
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000206 debug("%s: Missing tmu-base\n", __func__);
Jaehoon Chung10e80f32016-12-15 20:49:50 +0900207 return -ENODEV;
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000208 }
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700209 info->tmu_base = (struct exynos5_tmu_reg *)addr;
210
Naveen Krishna Chatradhib3d364f2013-04-05 15:21:39 -0700211 /* Optional field. */
212 info->tmu_mux = fdtdec_get_int(blob,
213 node, "samsung,mux", -1);
214 /* Take default value as per the user manual b(110) */
215 if (info->tmu_mux == -1)
216 info->tmu_mux = 0x6;
217
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000218 info->data.ts.min_val = fdtdec_get_int(blob,
219 node, "samsung,min-temp", -1);
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700220 error |= (info->data.ts.min_val == -1);
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000221 info->data.ts.max_val = fdtdec_get_int(blob,
222 node, "samsung,max-temp", -1);
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700223 error |= (info->data.ts.max_val == -1);
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000224 info->data.ts.start_warning = fdtdec_get_int(blob,
225 node, "samsung,start-warning", -1);
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700226 error |= (info->data.ts.start_warning == -1);
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000227 info->data.ts.start_tripping = fdtdec_get_int(blob,
228 node, "samsung,start-tripping", -1);
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700229 error |= (info->data.ts.start_tripping == -1);
Akshay Saraswatb9fa8c12013-02-25 01:13:06 +0000230 info->data.ts.hardware_tripping = fdtdec_get_int(blob,
231 node, "samsung,hw-tripping", -1);
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700232 error |= (info->data.ts.hardware_tripping == -1);
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000233 info->data.efuse_min_value = fdtdec_get_int(blob,
234 node, "samsung,efuse-min-value", -1);
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700235 error |= (info->data.efuse_min_value == -1);
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000236 info->data.efuse_value = fdtdec_get_int(blob,
237 node, "samsung,efuse-value", -1);
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700238 error |= (info->data.efuse_value == -1);
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000239 info->data.efuse_max_value = fdtdec_get_int(blob,
240 node, "samsung,efuse-max-value", -1);
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700241 error |= (info->data.efuse_max_value == -1);
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000242 info->data.slope = fdtdec_get_int(blob,
243 node, "samsung,slope", -1);
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700244 error |= (info->data.slope == -1);
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000245 info->dc_value = fdtdec_get_int(blob,
246 node, "samsung,dc-value", -1);
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700247 error |= (info->dc_value == -1);
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000248
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700249 if (error) {
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000250 debug("fail to get tmu node properties\n");
Jaehoon Chung10e80f32016-12-15 20:49:50 +0900251 return -EINVAL;
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000252 }
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700253#else
254 /* Non DT support may never be added. Just in case */
Jaehoon Chung10e80f32016-12-15 20:49:50 +0900255 return -ENODEV;
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000256#endif
257
258 return 0;
259}
260
261/*
262 * Calibrate and calculate threshold values and
263 * enable interrupt levels
264 *
265 * @param info pointer to the tmu_info struct
266 */
267static void tmu_setup_parameters(struct tmu_info *info)
268{
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700269 unsigned te_code, con;
270 unsigned warning_code, trip_code, hwtrip_code;
271 unsigned cooling_temp;
272 unsigned rising_value;
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000273 struct tmu_data *data = &info->data;
Naveen Krishna Chatradhi09ffe602013-04-05 15:21:38 -0700274 struct exynos5_tmu_reg *reg = info->tmu_base;
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000275
276 /* Must reload for reading efuse value from triminfo register */
277 writel(TRIMINFO_RELOAD, &reg->triminfo_control);
278
279 /* Get the compensation parameter */
280 te_code = readl(&reg->triminfo);
281 info->te1 = te_code & TRIM_INFO_MASK;
282 info->te2 = ((te_code >> 8) & TRIM_INFO_MASK);
283
284 if ((data->efuse_min_value > info->te1) ||
285 (info->te1 > data->efuse_max_value)
286 || (info->te2 != 0))
287 info->te1 = data->efuse_value;
288
289 /* Get RISING & FALLING Threshold value */
290 warning_code = data->ts.start_warning
291 + info->te1 - info->dc_value;
292 trip_code = data->ts.start_tripping
293 + info->te1 - info->dc_value;
Akshay Saraswatb9fa8c12013-02-25 01:13:06 +0000294 hwtrip_code = data->ts.hardware_tripping
295 + info->te1 - info->dc_value;
296
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000297 cooling_temp = 0;
298
Akshay Saraswatb9fa8c12013-02-25 01:13:06 +0000299 rising_value = ((warning_code << 8) |
300 (trip_code << 16) |
301 (hwtrip_code << 24));
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000302
303 /* Set interrupt level */
304 writel(rising_value, &reg->threshold_temp_rise);
305 writel(cooling_temp, &reg->threshold_temp_fall);
306
307 /*
308 * Init TMU control tuning parameters
309 * [28:24] VREF - Voltage reference
310 * [15:13] THERM_TRIP_MODE - Tripping mode
311 * [12] THERM_TRIP_EN - Thermal tripping enable
312 * [11:8] BUF_SLOPE_SEL - Gain of amplifier
313 * [6] THERM_TRIP_BY_TQ_EN - Tripping by TQ pin
314 */
315 writel(data->slope, &reg->tmu_control);
316
317 writel(INTCLEARALL, &reg->intclear);
318
319 /* TMU core enable */
320 con = readl(&reg->tmu_control);
Naveen Krishna Chatradhib3d364f2013-04-05 15:21:39 -0700321 con |= THERM_TRIP_EN | CORE_EN | (info->tmu_mux << 20);
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000322
323 writel(con, &reg->tmu_control);
324
Akshay Saraswatb9fa8c12013-02-25 01:13:06 +0000325 /* Enable HW thermal trip */
326 set_hw_thermal_trip();
327
328 /* LEV1 LEV2 interrupt enable */
329 writel(INTEN_RISE1 | INTEN_RISE2, &reg->inten);
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000330}
331
332/*
333 * Initialize TMU device
334 *
335 * @param blob FDT blob
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100336 * Return: int value, 0 for success
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000337 */
338int tmu_init(const void *blob)
339{
340 gbl_info.tmu_state = TMU_STATUS_INIT;
341 if (get_tmu_fdt_values(&gbl_info, blob) < 0)
342 goto ret;
343
344 tmu_setup_parameters(&gbl_info);
345 gbl_info.tmu_state = TMU_STATUS_NORMAL;
346ret:
Akshay Saraswat32e61e22013-02-25 01:13:00 +0000347 return gbl_info.tmu_state;
348}