]> Git Repo - J-u-boot.git/blame - drivers/power/exynos-tmu.c
Merge branch '2022-12-05-Kconfig-migrations-and-renames' into next
[J-u-boot.git] / drivers / power / exynos-tmu.c
CommitLineData
39d182d3
AS
1/*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
4 * Akshay Saraswat <[email protected]>
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>
f7ae49fc 23#include <log.h>
39d182d3
AS
24#include <tmu.h>
25#include <asm/arch/tmu.h>
3a0b1dae 26#include <asm/arch/power.h>
39d182d3
AS
27
28#define TRIMINFO_RELOAD 1
29#define CORE_EN 1
3a0b1dae 30#define THERM_TRIP_EN (1 << 12)
39d182d3
AS
31
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 */
1149ca00 54 unsigned min_val;
39d182d3 55 /* maximum value in temperature code range */
1149ca00 56 unsigned max_val;
39d182d3 57 /* temperature threshold to start warning */
1149ca00 58 unsigned start_warning;
39d182d3 59 /* temperature threshold CPU tripping */
1149ca00 60 unsigned start_tripping;
3a0b1dae 61 /* temperature threshold for HW tripping */
1149ca00 62 unsigned hardware_tripping;
39d182d3
AS
63};
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 */
1149ca00 70 unsigned efuse_min_value;
39d182d3 71 /* pre-defined efuse value for temperature calibration */
1149ca00 72 unsigned efuse_value;
39d182d3 73 /* pre-defined efuse range maximum value */
1149ca00 74 unsigned efuse_max_value;
39d182d3 75 /* current temperature sensing slope */
1149ca00 76 unsigned slope;
39d182d3
AS
77};
78
79/* TMU device specific details and status */
80struct tmu_info {
81 /* base Address for the TMU */
1149ca00 82 struct exynos5_tmu_reg *tmu_base;
eeb7d6a2
NKC
83 /* mux Address for the TMU */
84 int tmu_mux;
39d182d3
AS
85 /* pre-defined values for calibration and thresholds */
86 struct tmu_data data;
87 /* value required for triminfo_25 calibration */
1149ca00 88 unsigned te1;
39d182d3 89 /* value required for triminfo_85 calibration */
1149ca00 90 unsigned te2;
39d182d3
AS
91 /* 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 *
185f812c 105 * Return: current temperature of the chip as sensed by TMU
39d182d3
AS
106 */
107static int get_cur_temp(struct tmu_info *info)
108{
1149ca00
NKC
109 struct exynos5_tmu_reg *reg = info->tmu_base;
110 ulong start;
111 int cur_temp = 0;
39d182d3
AS
112
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 */
1149ca00
NKC
118 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;
39d182d3
AS
127
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
185f812c 138 * Return: enum tmu_status_t value, code indicating event to execute
39d182d3
AS
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);
1149ca00
NKC
150
151 if (!cur_temp)
152 goto out;
153
39d182d3
AS
154 *temp = cur_temp;
155
156 /* Temperature code lies between min 25 and max 125 */
1149ca00
NKC
157 if ((cur_temp >= data->ts.start_tripping) &&
158 (cur_temp <= data->ts.max_val))
39d182d3 159 return TMU_STATUS_TRIPPED;
1149ca00
NKC
160
161 if (cur_temp >= data->ts.start_warning)
39d182d3 162 return TMU_STATUS_WARNING;
1149ca00
NKC
163
164 if ((cur_temp < data->ts.start_warning) &&
165 (cur_temp >= data->ts.min_val))
39d182d3 166 return TMU_STATUS_NORMAL;
1149ca00
NKC
167
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;
39d182d3
AS
173}
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
185f812c 180 * Return: int value, 0 for success
39d182d3
AS
181 */
182static int get_tmu_fdt_values(struct tmu_info *info, const void *blob)
183{
0f925822 184#if CONFIG_IS_ENABLED(OF_CONTROL)
1149ca00 185 fdt_addr_t addr;
39d182d3
AS
186 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");
505cf475 194 return -ENODEV;
39d182d3
AS
195 }
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 */
1149ca00
NKC
203 addr = fdtdec_get_addr(blob, node, "reg");
204 if (addr == FDT_ADDR_T_NONE) {
39d182d3 205 debug("%s: Missing tmu-base\n", __func__);
505cf475 206 return -ENODEV;
39d182d3 207 }
1149ca00
NKC
208 info->tmu_base = (struct exynos5_tmu_reg *)addr;
209
eeb7d6a2
NKC
210 /* 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
39d182d3
AS
217 info->data.ts.min_val = fdtdec_get_int(blob,
218 node, "samsung,min-temp", -1);
1149ca00 219 error |= (info->data.ts.min_val == -1);
39d182d3
AS
220 info->data.ts.max_val = fdtdec_get_int(blob,
221 node, "samsung,max-temp", -1);
1149ca00 222 error |= (info->data.ts.max_val == -1);
39d182d3
AS
223 info->data.ts.start_warning = fdtdec_get_int(blob,
224 node, "samsung,start-warning", -1);
1149ca00 225 error |= (info->data.ts.start_warning == -1);
39d182d3
AS
226 info->data.ts.start_tripping = fdtdec_get_int(blob,
227 node, "samsung,start-tripping", -1);
1149ca00 228 error |= (info->data.ts.start_tripping == -1);
3a0b1dae
AS
229 info->data.ts.hardware_tripping = fdtdec_get_int(blob,
230 node, "samsung,hw-tripping", -1);
1149ca00 231 error |= (info->data.ts.hardware_tripping == -1);
39d182d3
AS
232 info->data.efuse_min_value = fdtdec_get_int(blob,
233 node, "samsung,efuse-min-value", -1);
1149ca00 234 error |= (info->data.efuse_min_value == -1);
39d182d3
AS
235 info->data.efuse_value = fdtdec_get_int(blob,
236 node, "samsung,efuse-value", -1);
1149ca00 237 error |= (info->data.efuse_value == -1);
39d182d3
AS
238 info->data.efuse_max_value = fdtdec_get_int(blob,
239 node, "samsung,efuse-max-value", -1);
1149ca00 240 error |= (info->data.efuse_max_value == -1);
39d182d3
AS
241 info->data.slope = fdtdec_get_int(blob,
242 node, "samsung,slope", -1);
1149ca00 243 error |= (info->data.slope == -1);
39d182d3
AS
244 info->dc_value = fdtdec_get_int(blob,
245 node, "samsung,dc-value", -1);
1149ca00 246 error |= (info->dc_value == -1);
39d182d3 247
1149ca00 248 if (error) {
39d182d3 249 debug("fail to get tmu node properties\n");
505cf475 250 return -EINVAL;
39d182d3 251 }
1149ca00
NKC
252#else
253 /* Non DT support may never be added. Just in case */
505cf475 254 return -ENODEV;
39d182d3
AS
255#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{
1149ca00
NKC
268 unsigned te_code, con;
269 unsigned warning_code, trip_code, hwtrip_code;
270 unsigned cooling_temp;
271 unsigned rising_value;
39d182d3 272 struct tmu_data *data = &info->data;
1149ca00 273 struct exynos5_tmu_reg *reg = info->tmu_base;
39d182d3
AS
274
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;
3a0b1dae
AS
293 hwtrip_code = data->ts.hardware_tripping
294 + info->te1 - info->dc_value;
295
39d182d3
AS
296 cooling_temp = 0;
297
3a0b1dae
AS
298 rising_value = ((warning_code << 8) |
299 (trip_code << 16) |
300 (hwtrip_code << 24));
39d182d3
AS
301
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);
eeb7d6a2 320 con |= THERM_TRIP_EN | CORE_EN | (info->tmu_mux << 20);
39d182d3
AS
321
322 writel(con, &reg->tmu_control);
323
3a0b1dae
AS
324 /* Enable HW thermal trip */
325 set_hw_thermal_trip();
326
327 /* LEV1 LEV2 interrupt enable */
328 writel(INTEN_RISE1 | INTEN_RISE2, &reg->inten);
39d182d3
AS
329}
330
331/*
332 * Initialize TMU device
333 *
334 * @param blob FDT blob
185f812c 335 * Return: int value, 0 for success
39d182d3
AS
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:
39d182d3
AS
346 return gbl_info.tmu_state;
347}
This page took 0.448304 seconds and 4 git commands to generate.