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