exynos-tmu.c 9.1 KB

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