exynos-tmu.c 9.4 KB

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