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 <tmu.h> 24 #include <asm/arch/tmu.h> 25 #include <asm/arch/power.h> 26 27 #define TRIMINFO_RELOAD 1 28 #define CORE_EN 1 29 #define THERM_TRIP_EN (1 << 12) 30 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 */ 51 struct temperature_params { 52 /* minimum value in temperature code range */ 53 unsigned min_val; 54 /* maximum value in temperature code range */ 55 unsigned max_val; 56 /* temperature threshold to start warning */ 57 unsigned start_warning; 58 /* temperature threshold CPU tripping */ 59 unsigned start_tripping; 60 /* temperature threshold for HW tripping */ 61 unsigned hardware_tripping; 62 }; 63 64 /* Pre-defined values and thresholds for calibration of current temperature */ 65 struct tmu_data { 66 /* pre-defined temperature thresholds */ 67 struct temperature_params ts; 68 /* pre-defined efuse range minimum value */ 69 unsigned efuse_min_value; 70 /* pre-defined efuse value for temperature calibration */ 71 unsigned efuse_value; 72 /* pre-defined efuse range maximum value */ 73 unsigned efuse_max_value; 74 /* current temperature sensing slope */ 75 unsigned slope; 76 }; 77 78 /* TMU device specific details and status */ 79 struct tmu_info { 80 /* base Address for the TMU */ 81 struct exynos5_tmu_reg *tmu_base; 82 /* pre-defined values for calibration and thresholds */ 83 struct tmu_data data; 84 /* value required for triminfo_25 calibration */ 85 unsigned te1; 86 /* value required for triminfo_85 calibration */ 87 unsigned te2; 88 /* Value for measured data calibration */ 89 int dc_value; 90 /* enum value indicating status of the TMU */ 91 int tmu_state; 92 }; 93 94 /* Global struct tmu_info variable to store init values */ 95 static struct tmu_info gbl_info; 96 97 /* 98 * Get current temperature code from register, 99 * then calculate and calibrate it's value 100 * in degree celsius. 101 * 102 * @return current temperature of the chip as sensed by TMU 103 */ 104 static int get_cur_temp(struct tmu_info *info) 105 { 106 struct exynos5_tmu_reg *reg = info->tmu_base; 107 ulong start; 108 int cur_temp = 0; 109 110 /* 111 * Temperature code range between min 25 and max 125. 112 * May run more than once for first call as initial sensing 113 * has not yet happened. 114 */ 115 if (info->tmu_state == TMU_STATUS_NORMAL) { 116 start = get_timer(0); 117 do { 118 cur_temp = readl(®->current_temp) & 0xff; 119 } while ((cur_temp == 0) || (get_timer(start) > 100)); 120 } 121 122 if (cur_temp == 0) 123 return cur_temp; 124 125 /* Calibrate current temperature */ 126 cur_temp = cur_temp - info->te1 + info->dc_value; 127 128 return cur_temp; 129 } 130 131 /* 132 * Monitors status of the TMU device and exynos temperature 133 * 134 * @param temp pointer to the current temperature value 135 * @return enum tmu_status_t value, code indicating event to execute 136 */ 137 enum tmu_status_t tmu_monitor(int *temp) 138 { 139 int cur_temp; 140 struct tmu_data *data = &gbl_info.data; 141 142 if (gbl_info.tmu_state == TMU_STATUS_INIT) 143 return TMU_STATUS_INIT; 144 145 /* Read current temperature of the SOC */ 146 cur_temp = get_cur_temp(&gbl_info); 147 148 if (!cur_temp) 149 goto out; 150 151 *temp = cur_temp; 152 153 /* Temperature code lies between min 25 and max 125 */ 154 if ((cur_temp >= data->ts.start_tripping) && 155 (cur_temp <= data->ts.max_val)) 156 return TMU_STATUS_TRIPPED; 157 158 if (cur_temp >= data->ts.start_warning) 159 return TMU_STATUS_WARNING; 160 161 if ((cur_temp < data->ts.start_warning) && 162 (cur_temp >= data->ts.min_val)) 163 return TMU_STATUS_NORMAL; 164 165 out: 166 /* Temperature code does not lie between min 25 and max 125 */ 167 gbl_info.tmu_state = TMU_STATUS_INIT; 168 debug("EXYNOS_TMU: Thermal reading failed\n"); 169 return TMU_STATUS_INIT; 170 } 171 172 /* 173 * Get TMU specific pre-defined values from FDT 174 * 175 * @param info pointer to the tmu_info struct 176 * @param blob FDT blob 177 * @return int value, 0 for success 178 */ 179 static int get_tmu_fdt_values(struct tmu_info *info, const void *blob) 180 { 181 #ifdef CONFIG_OF_CONTROL 182 fdt_addr_t addr; 183 int node; 184 int error = 0; 185 186 /* Get the node from FDT for TMU */ 187 node = fdtdec_next_compatible(blob, 0, 188 COMPAT_SAMSUNG_EXYNOS_TMU); 189 if (node < 0) { 190 debug("EXYNOS_TMU: No node for tmu in device tree\n"); 191 return -1; 192 } 193 194 /* 195 * Get the pre-defined TMU specific values from FDT. 196 * All of these are expected to be correct otherwise 197 * miscalculation of register values in tmu_setup_parameters 198 * may result in misleading current temperature. 199 */ 200 addr = fdtdec_get_addr(blob, node, "reg"); 201 if (addr == FDT_ADDR_T_NONE) { 202 debug("%s: Missing tmu-base\n", __func__); 203 return -1; 204 } 205 info->tmu_base = (struct exynos5_tmu_reg *)addr; 206 207 info->data.ts.min_val = fdtdec_get_int(blob, 208 node, "samsung,min-temp", -1); 209 error |= (info->data.ts.min_val == -1); 210 info->data.ts.max_val = fdtdec_get_int(blob, 211 node, "samsung,max-temp", -1); 212 error |= (info->data.ts.max_val == -1); 213 info->data.ts.start_warning = fdtdec_get_int(blob, 214 node, "samsung,start-warning", -1); 215 error |= (info->data.ts.start_warning == -1); 216 info->data.ts.start_tripping = fdtdec_get_int(blob, 217 node, "samsung,start-tripping", -1); 218 error |= (info->data.ts.start_tripping == -1); 219 info->data.ts.hardware_tripping = fdtdec_get_int(blob, 220 node, "samsung,hw-tripping", -1); 221 error |= (info->data.ts.hardware_tripping == -1); 222 info->data.efuse_min_value = fdtdec_get_int(blob, 223 node, "samsung,efuse-min-value", -1); 224 error |= (info->data.efuse_min_value == -1); 225 info->data.efuse_value = fdtdec_get_int(blob, 226 node, "samsung,efuse-value", -1); 227 error |= (info->data.efuse_value == -1); 228 info->data.efuse_max_value = fdtdec_get_int(blob, 229 node, "samsung,efuse-max-value", -1); 230 error |= (info->data.efuse_max_value == -1); 231 info->data.slope = fdtdec_get_int(blob, 232 node, "samsung,slope", -1); 233 error |= (info->data.slope == -1); 234 info->dc_value = fdtdec_get_int(blob, 235 node, "samsung,dc-value", -1); 236 error |= (info->dc_value == -1); 237 238 if (error) { 239 debug("fail to get tmu node properties\n"); 240 return -1; 241 } 242 #else 243 /* Non DT support may never be added. Just in case */ 244 return -1; 245 #endif 246 247 return 0; 248 } 249 250 /* 251 * Calibrate and calculate threshold values and 252 * enable interrupt levels 253 * 254 * @param info pointer to the tmu_info struct 255 */ 256 static void tmu_setup_parameters(struct tmu_info *info) 257 { 258 unsigned te_code, con; 259 unsigned warning_code, trip_code, hwtrip_code; 260 unsigned cooling_temp; 261 unsigned rising_value; 262 struct tmu_data *data = &info->data; 263 struct exynos5_tmu_reg *reg = info->tmu_base; 264 265 /* Must reload for reading efuse value from triminfo register */ 266 writel(TRIMINFO_RELOAD, ®->triminfo_control); 267 268 /* Get the compensation parameter */ 269 te_code = readl(®->triminfo); 270 info->te1 = te_code & TRIM_INFO_MASK; 271 info->te2 = ((te_code >> 8) & TRIM_INFO_MASK); 272 273 if ((data->efuse_min_value > info->te1) || 274 (info->te1 > data->efuse_max_value) 275 || (info->te2 != 0)) 276 info->te1 = data->efuse_value; 277 278 /* Get RISING & FALLING Threshold value */ 279 warning_code = data->ts.start_warning 280 + info->te1 - info->dc_value; 281 trip_code = data->ts.start_tripping 282 + info->te1 - info->dc_value; 283 hwtrip_code = data->ts.hardware_tripping 284 + info->te1 - info->dc_value; 285 286 cooling_temp = 0; 287 288 rising_value = ((warning_code << 8) | 289 (trip_code << 16) | 290 (hwtrip_code << 24)); 291 292 /* Set interrupt level */ 293 writel(rising_value, ®->threshold_temp_rise); 294 writel(cooling_temp, ®->threshold_temp_fall); 295 296 /* 297 * Init TMU control tuning parameters 298 * [28:24] VREF - Voltage reference 299 * [15:13] THERM_TRIP_MODE - Tripping mode 300 * [12] THERM_TRIP_EN - Thermal tripping enable 301 * [11:8] BUF_SLOPE_SEL - Gain of amplifier 302 * [6] THERM_TRIP_BY_TQ_EN - Tripping by TQ pin 303 */ 304 writel(data->slope, ®->tmu_control); 305 306 writel(INTCLEARALL, ®->intclear); 307 308 /* TMU core enable */ 309 con = readl(®->tmu_control); 310 con |= THERM_TRIP_EN | CORE_EN; 311 312 writel(con, ®->tmu_control); 313 314 /* Enable HW thermal trip */ 315 set_hw_thermal_trip(); 316 317 /* LEV1 LEV2 interrupt enable */ 318 writel(INTEN_RISE1 | INTEN_RISE2, ®->inten); 319 } 320 321 /* 322 * Initialize TMU device 323 * 324 * @param blob FDT blob 325 * @return int value, 0 for success 326 */ 327 int tmu_init(const void *blob) 328 { 329 gbl_info.tmu_state = TMU_STATUS_INIT; 330 if (get_tmu_fdt_values(&gbl_info, blob) < 0) 331 goto ret; 332 333 tmu_setup_parameters(&gbl_info); 334 gbl_info.tmu_state = TMU_STATUS_NORMAL; 335 ret: 336 return gbl_info.tmu_state; 337 } 338