imx_thermal.c 7.6 KB

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