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