imx_thermal.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <dm.h>
  15. #include <errno.h>
  16. #include <malloc.h>
  17. #include <thermal.h>
  18. #include <imx_thermal.h>
  19. #define TEMPERATURE_MIN -40
  20. #define TEMPERATURE_HOT 80
  21. #define TEMPERATURE_MAX 125
  22. #define FACTOR0 10000000
  23. #define FACTOR1 15976
  24. #define FACTOR2 4297157
  25. #define MEASURE_FREQ 327
  26. #define TEMPSENSE0_TEMP_CNT_SHIFT 8
  27. #define TEMPSENSE0_TEMP_CNT_MASK (0xfff << TEMPSENSE0_TEMP_CNT_SHIFT)
  28. #define TEMPSENSE0_FINISHED (1 << 2)
  29. #define TEMPSENSE0_MEASURE_TEMP (1 << 1)
  30. #define TEMPSENSE0_POWER_DOWN (1 << 0)
  31. #define MISC0_REFTOP_SELBIASOFF (1 << 3)
  32. #define TEMPSENSE1_MEASURE_FREQ 0xffff
  33. static int read_cpu_temperature(struct udevice *dev)
  34. {
  35. int temperature;
  36. unsigned int reg, n_meas;
  37. const struct imx_thermal_plat *pdata = dev_get_platdata(dev);
  38. struct anatop_regs *anatop = (struct anatop_regs *)pdata->regs;
  39. unsigned int *priv = dev_get_priv(dev);
  40. u32 fuse = *priv;
  41. int t1, n1;
  42. u32 c1, c2;
  43. u64 temp64;
  44. /*
  45. * Sensor data layout:
  46. * [31:20] - sensor value @ 25C
  47. * We use universal formula now and only need sensor value @ 25C
  48. * slope = 0.4297157 - (0.0015976 * 25C fuse)
  49. */
  50. n1 = fuse >> 20;
  51. t1 = 25; /* t1 always 25C */
  52. /*
  53. * Derived from linear interpolation:
  54. * slope = 0.4297157 - (0.0015976 * 25C fuse)
  55. * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
  56. * (Nmeas - n1) / (Tmeas - t1) = slope
  57. * We want to reduce this down to the minimum computation necessary
  58. * for each temperature read. Also, we want Tmeas in millicelsius
  59. * and we don't want to lose precision from integer division. So...
  60. * Tmeas = (Nmeas - n1) / slope + t1
  61. * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1
  62. * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1
  63. * Let constant c1 = (-1000 / slope)
  64. * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1
  65. * Let constant c2 = n1 *c1 + 1000 * t1
  66. * milli_Tmeas = c2 - Nmeas * c1
  67. */
  68. temp64 = FACTOR0;
  69. temp64 *= 1000;
  70. do_div(temp64, FACTOR1 * n1 - FACTOR2);
  71. c1 = temp64;
  72. c2 = n1 * c1 + 1000 * t1;
  73. /*
  74. * now we only use single measure, every time we read
  75. * the temperature, we will power on/down anadig thermal
  76. * module
  77. */
  78. writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_clr);
  79. writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_set);
  80. /* setup measure freq */
  81. reg = readl(&anatop->tempsense1);
  82. reg &= ~TEMPSENSE1_MEASURE_FREQ;
  83. reg |= MEASURE_FREQ;
  84. writel(reg, &anatop->tempsense1);
  85. /* start the measurement process */
  86. writel(TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_clr);
  87. writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
  88. writel(TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_set);
  89. /* make sure that the latest temp is valid */
  90. while ((readl(&anatop->tempsense0) &
  91. TEMPSENSE0_FINISHED) == 0)
  92. udelay(10000);
  93. /* read temperature count */
  94. reg = readl(&anatop->tempsense0);
  95. n_meas = (reg & TEMPSENSE0_TEMP_CNT_MASK)
  96. >> TEMPSENSE0_TEMP_CNT_SHIFT;
  97. writel(TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
  98. /* milli_Tmeas = c2 - Nmeas * c1 */
  99. temperature = (c2 - n_meas * c1)/1000;
  100. /* power down anatop thermal sensor */
  101. writel(TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
  102. writel(MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_clr);
  103. return temperature;
  104. }
  105. int imx_thermal_get_temp(struct udevice *dev, int *temp)
  106. {
  107. int cpu_tmp = 0;
  108. cpu_tmp = read_cpu_temperature(dev);
  109. while (cpu_tmp > TEMPERATURE_MIN && cpu_tmp < TEMPERATURE_MAX) {
  110. if (cpu_tmp >= TEMPERATURE_HOT) {
  111. printf("CPU Temperature is %d C, too hot to boot, waiting...\n",
  112. cpu_tmp);
  113. udelay(5000000);
  114. cpu_tmp = read_cpu_temperature(dev);
  115. } else {
  116. break;
  117. }
  118. }
  119. *temp = cpu_tmp;
  120. return 0;
  121. }
  122. static const struct dm_thermal_ops imx_thermal_ops = {
  123. .get_temp = imx_thermal_get_temp,
  124. };
  125. static int imx_thermal_probe(struct udevice *dev)
  126. {
  127. unsigned int fuse = ~0;
  128. const struct imx_thermal_plat *pdata = dev_get_platdata(dev);
  129. unsigned int *priv = dev_get_priv(dev);
  130. /* Read Temperature calibration data fuse */
  131. fuse_read(pdata->fuse_bank, pdata->fuse_word, &fuse);
  132. /* Check for valid fuse */
  133. if (fuse == 0 || fuse == ~0) {
  134. printf("CPU: Thermal invalid data, fuse: 0x%x\n", fuse);
  135. return -EPERM;
  136. }
  137. *priv = fuse;
  138. enable_thermal_clk();
  139. return 0;
  140. }
  141. U_BOOT_DRIVER(imx_thermal) = {
  142. .name = "imx_thermal",
  143. .id = UCLASS_THERMAL,
  144. .ops = &imx_thermal_ops,
  145. .probe = imx_thermal_probe,
  146. .priv_auto_alloc_size = sizeof(unsigned int),
  147. .flags = DM_FLAG_PRE_RELOC,
  148. };