imx_thermal.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2014 Freescale Semiconductor, Inc.
  4. * Author: Nitin Garg <nitin.garg@freescale.com>
  5. * Ye Li <Ye.Li@freescale.com>
  6. */
  7. #include <config.h>
  8. #include <common.h>
  9. #include <div64.h>
  10. #include <fuse.h>
  11. #include <log.h>
  12. #include <asm/io.h>
  13. #include <asm/arch/clock.h>
  14. #include <asm/arch/sys_proto.h>
  15. #include <dm.h>
  16. #include <errno.h>
  17. #include <malloc.h>
  18. #include <linux/delay.h>
  19. #include <linux/math64.h>
  20. #include <thermal.h>
  21. #include <imx_thermal.h>
  22. /* board will busyloop until this many degrees C below CPU max temperature */
  23. #define TEMPERATURE_HOT_DELTA 5 /* CPU maxT - 5C */
  24. #define FACTOR0 10000000
  25. #define FACTOR1 15423
  26. #define FACTOR2 4148468
  27. #define OFFSET 3580661
  28. #define MEASURE_FREQ 327
  29. #define TEMPERATURE_MIN -40
  30. #define TEMPERATURE_HOT 85
  31. #define TEMPERATURE_MAX 125
  32. #define TEMPSENSE0_TEMP_CNT_SHIFT 8
  33. #define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
  34. #define TEMPSENSE0_FINISHED (1 << 2)
  35. #define TEMPSENSE0_MEASURE_TEMP (1 << 1)
  36. #define TEMPSENSE0_POWER_DOWN (1 << 0)
  37. #define MISC0_REFTOP_SELBIASOFF (1 << 3)
  38. #define TEMPSENSE1_MEASURE_FREQ 0xffff
  39. struct thermal_data {
  40. unsigned int fuse;
  41. int critical;
  42. int minc;
  43. int maxc;
  44. };
  45. #if defined(CONFIG_MX6)
  46. static int read_cpu_temperature(struct udevice *dev)
  47. {
  48. int temperature;
  49. unsigned int reg, n_meas;
  50. const struct imx_thermal_plat *pdata = dev_get_plat(dev);
  51. struct anatop_regs *anatop = (struct anatop_regs *)pdata->regs;
  52. struct thermal_data *priv = dev_get_priv(dev);
  53. u32 fuse = priv->fuse;
  54. int t1, n1;
  55. s64 c1, c2;
  56. s64 temp64;
  57. s32 rem;
  58. /*
  59. * Sensor data layout:
  60. * [31:20] - sensor value @ 25C
  61. * We use universal formula now and only need sensor value @ 25C
  62. * slope = 0.4445388 - (0.0016549 * 25C fuse)
  63. */
  64. n1 = fuse >> 20;
  65. t1 = 25; /* t1 always 25C */
  66. /*
  67. * Derived from linear interpolation:
  68. * slope = 0.4445388 - (0.0016549 * 25C fuse)
  69. * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
  70. * offset = 3.580661
  71. * offset = OFFSET / 1000000
  72. * (Nmeas - n1) / (Tmeas - t1 - offset) = slope
  73. * We want to reduce this down to the minimum computation necessary
  74. * for each temperature read. Also, we want Tmeas in millicelsius
  75. * and we don't want to lose precision from integer division. So...
  76. * Tmeas = (Nmeas - n1) / slope + t1 + offset
  77. * milli_Tmeas = 1000000 * (Nmeas - n1) / slope + 1000000 * t1 + OFFSET
  78. * milli_Tmeas = -1000000 * (n1 - Nmeas) / slope + 1000000 * t1 + OFFSET
  79. * Let constant c1 = (-1000000 / slope)
  80. * milli_Tmeas = (n1 - Nmeas) * c1 + 1000000 * t1 + OFFSET
  81. * Let constant c2 = n1 *c1 + 1000000 * t1
  82. * milli_Tmeas = (c2 - Nmeas * c1) + OFFSET
  83. * Tmeas = ((c2 - Nmeas * c1) + OFFSET) / 1000000
  84. */
  85. temp64 = FACTOR0;
  86. temp64 *= 1000000;
  87. temp64 = div_s64_rem(temp64, FACTOR1 * n1 - FACTOR2, &rem);
  88. c1 = temp64;
  89. c2 = n1 * c1 + 1000000 * t1;
  90. /*
  91. * now we only use single measure, every time we read
  92. * the temperature, we will power on/down anadig thermal
  93. * module
  94. */
  95. writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_clr);
  96. writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_set);
  97. /* setup measure freq */
  98. reg = readl(&anatop->tempsense1);
  99. reg &= ~TEMPSENSE1_MEASURE_FREQ;
  100. reg |= MEASURE_FREQ;
  101. writel(reg, &anatop->tempsense1);
  102. /* start the measurement process */
  103. writel(TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_clr);
  104. writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
  105. writel(TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_set);
  106. /* make sure that the latest temp is valid */
  107. while ((readl(&anatop->tempsense0) &
  108. TEMPSENSE0_FINISHED) == 0)
  109. udelay(10000);
  110. /* read temperature count */
  111. reg = readl(&anatop->tempsense0);
  112. n_meas = (reg & TEMPSENSE0_TEMP_CNT_MASK)
  113. >> TEMPSENSE0_TEMP_CNT_SHIFT;
  114. writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
  115. /* Tmeas = (c2 - Nmeas * c1 + OFFSET) / 1000000 */
  116. temperature = div_s64_rem(c2 - n_meas * c1 + OFFSET, 1000000, &rem);
  117. /* power down anatop thermal sensor */
  118. writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
  119. writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_clr);
  120. return temperature;
  121. }
  122. #elif defined(CONFIG_MX7)
  123. static int read_cpu_temperature(struct udevice *dev)
  124. {
  125. unsigned int reg, tmp;
  126. unsigned int raw_25c, te1;
  127. int temperature;
  128. unsigned int *priv = dev_get_priv(dev);
  129. u32 fuse = *priv;
  130. struct mxc_ccm_anatop_reg *ccm_anatop = (struct mxc_ccm_anatop_reg *)
  131. ANATOP_BASE_ADDR;
  132. /*
  133. * fuse data layout:
  134. * [31:21] sensor value @ 25C
  135. * [20:18] hot temperature value
  136. * [17:9] sensor value of room
  137. * [8:0] sensor value of hot
  138. */
  139. raw_25c = fuse >> 21;
  140. if (raw_25c == 0)
  141. raw_25c = 25;
  142. te1 = (fuse >> 9) & 0x1ff;
  143. /*
  144. * now we only use single measure, every time we read
  145. * the temperature, we will power on/down anadig thermal
  146. * module
  147. */
  148. writel(TEMPMON_HW_ANADIG_TEMPSENSE1_POWER_DOWN_MASK, &ccm_anatop->tempsense1_clr);
  149. writel(PMU_REF_REFTOP_SELFBIASOFF_MASK, &ccm_anatop->ref_set);
  150. /* write measure freq */
  151. reg = readl(&ccm_anatop->tempsense1);
  152. reg &= ~TEMPMON_HW_ANADIG_TEMPSENSE1_MEASURE_FREQ_MASK;
  153. reg |= TEMPMON_HW_ANADIG_TEMPSENSE1_MEASURE_FREQ(MEASURE_FREQ);
  154. writel(reg, &ccm_anatop->tempsense1);
  155. writel(TEMPMON_HW_ANADIG_TEMPSENSE1_MEASURE_TEMP_MASK, &ccm_anatop->tempsense1_clr);
  156. writel(TEMPMON_HW_ANADIG_TEMPSENSE1_FINISHED_MASK, &ccm_anatop->tempsense1_clr);
  157. writel(TEMPMON_HW_ANADIG_TEMPSENSE1_MEASURE_TEMP_MASK, &ccm_anatop->tempsense1_set);
  158. if (soc_rev() >= CHIP_REV_1_1) {
  159. while ((readl(&ccm_anatop->tempsense1) &
  160. TEMPMON_HW_ANADIG_TEMPSENSE1_FINISHED_MASK) == 0)
  161. ;
  162. reg = readl(&ccm_anatop->tempsense1);
  163. tmp = (reg & TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_MASK)
  164. >> TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_SHIFT;
  165. } else {
  166. /*
  167. * Since we can not rely on finish bit, use 10ms
  168. * delay to get temperature. From RM, 17us is
  169. * enough to get data, but to gurantee to get
  170. * the data, delay 10ms here.
  171. */
  172. udelay(10000);
  173. reg = readl(&ccm_anatop->tempsense1);
  174. tmp = (reg & TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_MASK)
  175. >> TEMPMON_HW_ANADIG_TEMPSENSE1_TEMP_VALUE_SHIFT;
  176. }
  177. writel(TEMPMON_HW_ANADIG_TEMPSENSE1_FINISHED_MASK, &ccm_anatop->tempsense1_clr);
  178. /* power down anatop thermal sensor */
  179. writel(TEMPMON_HW_ANADIG_TEMPSENSE1_POWER_DOWN_MASK, &ccm_anatop->tempsense1_set);
  180. writel(PMU_REF_REFTOP_SELFBIASOFF_MASK, &ccm_anatop->ref_clr);
  181. /* Single point */
  182. temperature = tmp - (te1 - raw_25c);
  183. return temperature;
  184. }
  185. #endif
  186. int imx_thermal_get_temp(struct udevice *dev, int *temp)
  187. {
  188. struct thermal_data *priv = dev_get_priv(dev);
  189. int cpu_tmp = 0;
  190. cpu_tmp = read_cpu_temperature(dev);
  191. while (cpu_tmp >= priv->critical) {
  192. printf("CPU Temperature (%dC) too close to max (%dC)",
  193. cpu_tmp, priv->maxc);
  194. puts(" waiting...\n");
  195. udelay(5000000);
  196. cpu_tmp = read_cpu_temperature(dev);
  197. }
  198. *temp = cpu_tmp;
  199. return 0;
  200. }
  201. static const struct dm_thermal_ops imx_thermal_ops = {
  202. .get_temp = imx_thermal_get_temp,
  203. };
  204. static int imx_thermal_probe(struct udevice *dev)
  205. {
  206. unsigned int fuse = ~0;
  207. const struct imx_thermal_plat *pdata = dev_get_plat(dev);
  208. struct thermal_data *priv = dev_get_priv(dev);
  209. /* Read Temperature calibration data fuse */
  210. fuse_read(pdata->fuse_bank, pdata->fuse_word, &fuse);
  211. if (is_soc_type(MXC_SOC_MX6)) {
  212. /* Check for valid fuse */
  213. if (fuse == 0 || fuse == ~0) {
  214. debug("CPU: Thermal invalid data, fuse: 0x%x\n",
  215. fuse);
  216. return -EPERM;
  217. }
  218. } else if (is_soc_type(MXC_SOC_MX7)) {
  219. /* No Calibration data in FUSE? */
  220. if ((fuse & 0x3ffff) == 0)
  221. return -EPERM;
  222. /* We do not support 105C TE2 */
  223. if (((fuse & 0x1c0000) >> 18) == 0x6)
  224. return -EPERM;
  225. }
  226. /* set critical cooling temp */
  227. get_cpu_temp_grade(&priv->minc, &priv->maxc);
  228. priv->critical = priv->maxc - TEMPERATURE_HOT_DELTA;
  229. priv->fuse = fuse;
  230. enable_thermal_clk();
  231. return 0;
  232. }
  233. U_BOOT_DRIVER(imx_thermal) = {
  234. .name = "imx_thermal",
  235. .id = UCLASS_THERMAL,
  236. .ops = &imx_thermal_ops,
  237. .probe = imx_thermal_probe,
  238. .priv_auto = sizeof(struct thermal_data),
  239. .flags = DM_FLAG_PRE_RELOC,
  240. };