imx_thermal.c 7.9 KB

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