colibri_imx6: fix video stdout in default environment
[oweals/u-boot.git] / drivers / power / exynos-tmu.c
1 /*
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 <log.h>
24 #include <tmu.h>
25 #include <asm/arch/tmu.h>
26 #include <asm/arch/power.h>
27
28 #define TRIMINFO_RELOAD         1
29 #define CORE_EN                 1
30 #define THERM_TRIP_EN           (1 << 12)
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 */
52 struct temperature_params {
53         /* minimum value in temperature code range */
54         unsigned min_val;
55         /* maximum value in temperature code range */
56         unsigned max_val;
57         /* temperature threshold to start warning */
58         unsigned start_warning;
59         /* temperature threshold CPU tripping */
60         unsigned start_tripping;
61         /* temperature threshold for HW tripping */
62         unsigned hardware_tripping;
63 };
64
65 /* Pre-defined values and thresholds for calibration of current temperature */
66 struct tmu_data {
67         /* pre-defined temperature thresholds */
68         struct temperature_params ts;
69         /* pre-defined efuse range minimum value */
70         unsigned efuse_min_value;
71         /* pre-defined efuse value for temperature calibration */
72         unsigned efuse_value;
73         /* pre-defined efuse range maximum value */
74         unsigned efuse_max_value;
75         /* current temperature sensing slope */
76         unsigned slope;
77 };
78
79 /* TMU device specific details and status */
80 struct tmu_info {
81         /* base Address for the TMU */
82         struct exynos5_tmu_reg *tmu_base;
83         /* mux Address for the TMU */
84         int tmu_mux;
85         /* pre-defined values for calibration and thresholds */
86         struct tmu_data data;
87         /* value required for triminfo_25 calibration */
88         unsigned te1;
89         /* value required for triminfo_85 calibration */
90         unsigned te2;
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 */
98 static 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  */
107 static int get_cur_temp(struct tmu_info *info)
108 {
109         struct exynos5_tmu_reg *reg = info->tmu_base;
110         ulong start;
111         int cur_temp = 0;
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          */
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;
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
138  * @return      enum tmu_status_t value, code indicating event to execute
139  */
140 enum 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);
150
151         if (!cur_temp)
152                 goto out;
153
154         *temp = cur_temp;
155
156         /* Temperature code lies between min 25 and max 125 */
157         if ((cur_temp >= data->ts.start_tripping) &&
158             (cur_temp <= data->ts.max_val))
159                 return TMU_STATUS_TRIPPED;
160
161         if (cur_temp >= data->ts.start_warning)
162                 return TMU_STATUS_WARNING;
163
164         if ((cur_temp < data->ts.start_warning) &&
165             (cur_temp >= data->ts.min_val))
166                 return TMU_STATUS_NORMAL;
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;
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
180  * @return      int value, 0 for success
181  */
182 static int get_tmu_fdt_values(struct tmu_info *info, const void *blob)
183 {
184 #if CONFIG_IS_ENABLED(OF_CONTROL)
185         fdt_addr_t addr;
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");
194                 return -ENODEV;
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          */
203         addr = fdtdec_get_addr(blob, node, "reg");
204         if (addr == FDT_ADDR_T_NONE) {
205                 debug("%s: Missing tmu-base\n", __func__);
206                 return -ENODEV;
207         }
208         info->tmu_base = (struct exynos5_tmu_reg *)addr;
209
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
217         info->data.ts.min_val = fdtdec_get_int(blob,
218                                 node, "samsung,min-temp", -1);
219         error |= (info->data.ts.min_val == -1);
220         info->data.ts.max_val = fdtdec_get_int(blob,
221                                 node, "samsung,max-temp", -1);
222         error |= (info->data.ts.max_val == -1);
223         info->data.ts.start_warning = fdtdec_get_int(blob,
224                                 node, "samsung,start-warning", -1);
225         error |= (info->data.ts.start_warning == -1);
226         info->data.ts.start_tripping = fdtdec_get_int(blob,
227                                 node, "samsung,start-tripping", -1);
228         error |= (info->data.ts.start_tripping == -1);
229         info->data.ts.hardware_tripping = fdtdec_get_int(blob,
230                                 node, "samsung,hw-tripping", -1);
231         error |= (info->data.ts.hardware_tripping == -1);
232         info->data.efuse_min_value = fdtdec_get_int(blob,
233                                 node, "samsung,efuse-min-value", -1);
234         error |= (info->data.efuse_min_value == -1);
235         info->data.efuse_value = fdtdec_get_int(blob,
236                                 node, "samsung,efuse-value", -1);
237         error |= (info->data.efuse_value == -1);
238         info->data.efuse_max_value = fdtdec_get_int(blob,
239                                 node, "samsung,efuse-max-value", -1);
240         error |= (info->data.efuse_max_value == -1);
241         info->data.slope = fdtdec_get_int(blob,
242                                 node, "samsung,slope", -1);
243         error |= (info->data.slope == -1);
244         info->dc_value = fdtdec_get_int(blob,
245                                 node, "samsung,dc-value", -1);
246         error |= (info->dc_value == -1);
247
248         if (error) {
249                 debug("fail to get tmu node properties\n");
250                 return -EINVAL;
251         }
252 #else
253         /* Non DT support may never be added. Just in case  */
254         return -ENODEV;
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  */
266 static void tmu_setup_parameters(struct tmu_info *info)
267 {
268         unsigned te_code, con;
269         unsigned warning_code, trip_code, hwtrip_code;
270         unsigned cooling_temp;
271         unsigned rising_value;
272         struct tmu_data *data = &info->data;
273         struct exynos5_tmu_reg *reg = info->tmu_base;
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;
293         hwtrip_code = data->ts.hardware_tripping
294                         + info->te1 - info->dc_value;
295
296         cooling_temp = 0;
297
298         rising_value = ((warning_code << 8) |
299                         (trip_code << 16) |
300                         (hwtrip_code << 24));
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);
320         con |= THERM_TRIP_EN | CORE_EN | (info->tmu_mux << 20);
321
322         writel(con, &reg->tmu_control);
323
324         /* Enable HW thermal trip */
325         set_hw_thermal_trip();
326
327         /* LEV1 LEV2 interrupt enable */
328         writel(INTEN_RISE1 | INTEN_RISE2, &reg->inten);
329 }
330
331 /*
332  * Initialize TMU device
333  *
334  * @param blob  FDT blob
335  * @return      int value, 0 for success
336  */
337 int 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;
345 ret:
346         return gbl_info.tmu_state;
347 }