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