exynos-tmu.c 8.9 KB

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