exynos-tmu.c 9.4 KB

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