imx_thermal.c 7.9 KB

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