exynos-tmu.c 8.5 KB

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