Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright 2017~2020 NXP |
| 4 | * |
| 5 | */ |
| 6 | |
| 7 | #include <config.h> |
| 8 | #include <common.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 9 | #include <asm/global_data.h> |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 10 | #include <asm/io.h> |
| 11 | #include <asm/arch/clock.h> |
| 12 | #include <asm/arch/sys_proto.h> |
| 13 | #include <dm.h> |
| 14 | #include <dm/device-internal.h> |
| 15 | #include <dm/device.h> |
| 16 | #include <errno.h> |
| 17 | #include <fuse.h> |
Tim Harvey | 85abf04 | 2020-10-12 12:21:54 -0700 | [diff] [blame] | 18 | #include <linux/delay.h> |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 19 | #include <malloc.h> |
| 20 | #include <thermal.h> |
| 21 | |
| 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
| 24 | #define SITES_MAX 16 |
Wolfgang Denk | 0cf207e | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 25 | #define FLAGS_VER2 0x1 |
| 26 | #define FLAGS_VER3 0x2 |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 27 | |
| 28 | #define TMR_DISABLE 0x0 |
| 29 | #define TMR_ME 0x80000000 |
| 30 | #define TMR_ALPF 0x0c000000 |
| 31 | #define TMTMIR_DEFAULT 0x00000002 |
| 32 | #define TIER_DISABLE 0x0 |
| 33 | |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 34 | #define TER_EN 0x80000000 |
| 35 | #define TER_ADC_PD 0x40000000 |
Peng Fan | 634fe73 | 2020-05-03 22:19:51 +0800 | [diff] [blame] | 36 | #define TER_ALPF 0x3 |
| 37 | |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 38 | /* |
| 39 | * i.MX TMU Registers |
| 40 | */ |
| 41 | struct imx_tmu_site_regs { |
| 42 | u32 tritsr; /* Immediate Temperature Site Register */ |
| 43 | u32 tratsr; /* Average Temperature Site Register */ |
| 44 | u8 res0[0x8]; |
| 45 | }; |
| 46 | |
| 47 | struct imx_tmu_regs { |
| 48 | u32 tmr; /* Mode Register */ |
| 49 | u32 tsr; /* Status Register */ |
| 50 | u32 tmtmir; /* Temperature measurement interval Register */ |
| 51 | u8 res0[0x14]; |
| 52 | u32 tier; /* Interrupt Enable Register */ |
| 53 | u32 tidr; /* Interrupt Detect Register */ |
| 54 | u32 tiscr; /* Interrupt Site Capture Register */ |
| 55 | u32 ticscr; /* Interrupt Critical Site Capture Register */ |
| 56 | u8 res1[0x10]; |
| 57 | u32 tmhtcrh; /* High Temperature Capture Register */ |
| 58 | u32 tmhtcrl; /* Low Temperature Capture Register */ |
| 59 | u8 res2[0x8]; |
| 60 | u32 tmhtitr; /* High Temperature Immediate Threshold */ |
| 61 | u32 tmhtatr; /* High Temperature Average Threshold */ |
| 62 | u32 tmhtactr; /* High Temperature Average Crit Threshold */ |
| 63 | u8 res3[0x24]; |
| 64 | u32 ttcfgr; /* Temperature Configuration Register */ |
| 65 | u32 tscfgr; /* Sensor Configuration Register */ |
| 66 | u8 res4[0x78]; |
| 67 | struct imx_tmu_site_regs site[SITES_MAX]; |
| 68 | u8 res5[0x9f8]; |
| 69 | u32 ipbrr0; /* IP Block Revision Register 0 */ |
| 70 | u32 ipbrr1; /* IP Block Revision Register 1 */ |
| 71 | u8 res6[0x310]; |
| 72 | u32 ttr0cr; /* Temperature Range 0 Control Register */ |
| 73 | u32 ttr1cr; /* Temperature Range 1 Control Register */ |
| 74 | u32 ttr2cr; /* Temperature Range 2 Control Register */ |
| 75 | u32 ttr3cr; /* Temperature Range 3 Control Register */ |
| 76 | }; |
| 77 | |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 78 | struct imx_tmu_regs_v2 { |
| 79 | u32 ter; /* TMU enable Register */ |
| 80 | u32 tsr; /* Status Register */ |
| 81 | u32 tier; /* Interrupt enable register */ |
| 82 | u32 tidr; /* Interrupt detect register */ |
| 83 | u32 tmhtitr; /* Monitor high temperature immediate threshold register */ |
| 84 | u32 tmhtatr; /* Monitor high temperature average threshold register */ |
| 85 | u32 tmhtactr; /* TMU monitor high temperature average critical threshold register */ |
| 86 | u32 tscr; /* Sensor value capture register */ |
| 87 | u32 tritsr; /* Report immediate temperature site register 0 */ |
| 88 | u32 tratsr; /* Report average temperature site register 0 */ |
| 89 | u32 tasr; /* Amplifier setting register */ |
| 90 | u32 ttmc; /* Test MUX control */ |
| 91 | u32 tcaliv; |
| 92 | }; |
| 93 | |
Peng Fan | 634fe73 | 2020-05-03 22:19:51 +0800 | [diff] [blame] | 94 | struct imx_tmu_regs_v3 { |
| 95 | u32 ter; /* TMU enable Register */ |
| 96 | u32 tps; /* Status Register */ |
| 97 | u32 tier; /* Interrupt enable register */ |
| 98 | u32 tidr; /* Interrupt detect register */ |
| 99 | u32 tmhtitr; /* Monitor high temperature immediate threshold register */ |
| 100 | u32 tmhtatr; /* Monitor high temperature average threshold register */ |
| 101 | u32 tmhtactr; /* TMU monitor high temperature average critical threshold register */ |
| 102 | u32 tscr; /* Sensor value capture register */ |
| 103 | u32 tritsr; /* Report immediate temperature site register 0 */ |
| 104 | u32 tratsr; /* Report average temperature site register 0 */ |
| 105 | u32 tasr; /* Amplifier setting register */ |
| 106 | u32 ttmc; /* Test MUX control */ |
| 107 | u32 tcaliv0; |
| 108 | u32 tcaliv1; |
| 109 | u32 tcaliv_m40; |
| 110 | u32 trim; |
| 111 | }; |
| 112 | |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 113 | union tmu_regs { |
| 114 | struct imx_tmu_regs regs_v1; |
| 115 | struct imx_tmu_regs_v2 regs_v2; |
Peng Fan | 634fe73 | 2020-05-03 22:19:51 +0800 | [diff] [blame] | 116 | struct imx_tmu_regs_v3 regs_v3; |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 117 | }; |
| 118 | |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 119 | struct imx_tmu_plat { |
| 120 | int critical; |
| 121 | int alert; |
| 122 | int polling_delay; |
| 123 | int id; |
| 124 | bool zone_node; |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 125 | union tmu_regs *regs; |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | static int read_temperature(struct udevice *dev, int *temp) |
| 129 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 130 | struct imx_tmu_plat *pdata = dev_get_plat(dev); |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 131 | ulong drv_data = dev_get_driver_data(dev); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 132 | u32 val; |
Peng Fan | b5447b9 | 2020-05-03 22:19:49 +0800 | [diff] [blame] | 133 | u32 retry = 10; |
Peng Fan | 951bf19 | 2020-05-03 22:19:50 +0800 | [diff] [blame] | 134 | u32 valid = 0; |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 135 | |
| 136 | do { |
Peng Fan | b5447b9 | 2020-05-03 22:19:49 +0800 | [diff] [blame] | 137 | mdelay(100); |
| 138 | retry--; |
| 139 | |
Peng Fan | 634fe73 | 2020-05-03 22:19:51 +0800 | [diff] [blame] | 140 | if (drv_data & FLAGS_VER3) { |
| 141 | val = readl(&pdata->regs->regs_v3.tritsr); |
| 142 | valid = val & (1 << (30 + pdata->id)); |
| 143 | } else if (drv_data & FLAGS_VER2) { |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 144 | val = readl(&pdata->regs->regs_v2.tritsr); |
Peng Fan | 951bf19 | 2020-05-03 22:19:50 +0800 | [diff] [blame] | 145 | /* |
| 146 | * Check if TEMP is in valid range, the V bit in TRITSR |
| 147 | * only reflects the RAW uncalibrated data |
| 148 | */ |
| 149 | valid = ((val & 0xff) < 10 || (val & 0xff) > 125) ? 0 : 1; |
| 150 | } else { |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 151 | val = readl(&pdata->regs->regs_v1.site[pdata->id].tritsr); |
Peng Fan | 951bf19 | 2020-05-03 22:19:50 +0800 | [diff] [blame] | 152 | valid = val & 0x80000000; |
| 153 | } |
| 154 | } while (!valid && retry > 0); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 155 | |
Peng Fan | 634fe73 | 2020-05-03 22:19:51 +0800 | [diff] [blame] | 156 | if (retry > 0) { |
| 157 | if (drv_data & FLAGS_VER3) { |
| 158 | val = (val >> (pdata->id * 16)) & 0xff; |
| 159 | if (val & 0x80) /* Negative */ |
| 160 | val = (~(val & 0x7f) + 1); |
| 161 | |
| 162 | *temp = val; |
| 163 | if (*temp < -40 || *temp > 125) /* Check the range */ |
| 164 | return -EINVAL; |
| 165 | |
| 166 | *temp *= 1000; |
| 167 | } else { |
| 168 | *temp = (val & 0xff) * 1000; |
| 169 | } |
| 170 | } else { |
Peng Fan | b5447b9 | 2020-05-03 22:19:49 +0800 | [diff] [blame] | 171 | return -EINVAL; |
Peng Fan | 634fe73 | 2020-05-03 22:19:51 +0800 | [diff] [blame] | 172 | } |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | int imx_tmu_get_temp(struct udevice *dev, int *temp) |
| 178 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 179 | struct imx_tmu_plat *pdata = dev_get_plat(dev); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 180 | int cpu_tmp = 0; |
| 181 | int ret; |
| 182 | |
| 183 | ret = read_temperature(dev, &cpu_tmp); |
| 184 | if (ret) |
| 185 | return ret; |
| 186 | |
| 187 | while (cpu_tmp >= pdata->alert) { |
| 188 | printf("CPU Temperature (%dC) has beyond alert (%dC), close to critical (%dC)", cpu_tmp, pdata->alert, pdata->critical); |
| 189 | puts(" waiting...\n"); |
| 190 | mdelay(pdata->polling_delay); |
| 191 | ret = read_temperature(dev, &cpu_tmp); |
| 192 | if (ret) |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | *temp = cpu_tmp / 1000; |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static const struct dm_thermal_ops imx_tmu_ops = { |
| 202 | .get_temp = imx_tmu_get_temp, |
| 203 | }; |
| 204 | |
| 205 | static int imx_tmu_calibration(struct udevice *dev) |
| 206 | { |
| 207 | int i, val, len, ret; |
| 208 | u32 range[4]; |
| 209 | const fdt32_t *calibration; |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 210 | struct imx_tmu_plat *pdata = dev_get_plat(dev); |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 211 | ulong drv_data = dev_get_driver_data(dev); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 212 | |
| 213 | debug("%s\n", __func__); |
| 214 | |
Peng Fan | 634fe73 | 2020-05-03 22:19:51 +0800 | [diff] [blame] | 215 | if (drv_data & (FLAGS_VER2 | FLAGS_VER3)) |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 216 | return 0; |
| 217 | |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 218 | ret = dev_read_u32_array(dev, "fsl,tmu-range", range, 4); |
| 219 | if (ret) { |
| 220 | printf("TMU: missing calibration range, ret = %d.\n", ret); |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | /* Init temperature range registers */ |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 225 | writel(range[0], &pdata->regs->regs_v1.ttr0cr); |
| 226 | writel(range[1], &pdata->regs->regs_v1.ttr1cr); |
| 227 | writel(range[2], &pdata->regs->regs_v1.ttr2cr); |
| 228 | writel(range[3], &pdata->regs->regs_v1.ttr3cr); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 229 | |
| 230 | calibration = dev_read_prop(dev, "fsl,tmu-calibration", &len); |
| 231 | if (!calibration || len % 8) { |
| 232 | printf("TMU: invalid calibration data.\n"); |
| 233 | return -ENODEV; |
| 234 | } |
| 235 | |
| 236 | for (i = 0; i < len; i += 8, calibration += 2) { |
| 237 | val = fdt32_to_cpu(*calibration); |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 238 | writel(val, &pdata->regs->regs_v1.ttcfgr); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 239 | val = fdt32_to_cpu(*(calibration + 1)); |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 240 | writel(val, &pdata->regs->regs_v1.tscfgr); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
Peng Fan | 8489740 | 2020-05-03 22:19:48 +0800 | [diff] [blame] | 246 | void __weak imx_tmu_arch_init(void *reg_base) |
| 247 | { |
| 248 | } |
| 249 | |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 250 | static void imx_tmu_init(struct udevice *dev) |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 251 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 252 | struct imx_tmu_plat *pdata = dev_get_plat(dev); |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 253 | ulong drv_data = dev_get_driver_data(dev); |
| 254 | |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 255 | debug("%s\n", __func__); |
| 256 | |
Peng Fan | 634fe73 | 2020-05-03 22:19:51 +0800 | [diff] [blame] | 257 | if (drv_data & FLAGS_VER3) { |
| 258 | /* Disable monitoring */ |
| 259 | writel(0x0, &pdata->regs->regs_v3.ter); |
| 260 | |
| 261 | /* Disable interrupt, using polling instead */ |
| 262 | writel(0x0, &pdata->regs->regs_v3.tier); |
| 263 | |
| 264 | } else if (drv_data & FLAGS_VER2) { |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 265 | /* Disable monitoring */ |
| 266 | writel(0x0, &pdata->regs->regs_v2.ter); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 267 | |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 268 | /* Disable interrupt, using polling instead */ |
| 269 | writel(0x0, &pdata->regs->regs_v2.tier); |
| 270 | } else { |
| 271 | /* Disable monitoring */ |
| 272 | writel(TMR_DISABLE, &pdata->regs->regs_v1.tmr); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 273 | |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 274 | /* Disable interrupt, using polling instead */ |
| 275 | writel(TIER_DISABLE, &pdata->regs->regs_v1.tier); |
| 276 | |
| 277 | /* Set update_interval */ |
| 278 | writel(TMTMIR_DEFAULT, &pdata->regs->regs_v1.tmtmir); |
| 279 | } |
Peng Fan | 8489740 | 2020-05-03 22:19:48 +0800 | [diff] [blame] | 280 | |
| 281 | imx_tmu_arch_init((void *)pdata->regs); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static int imx_tmu_enable_msite(struct udevice *dev) |
| 285 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 286 | struct imx_tmu_plat *pdata = dev_get_plat(dev); |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 287 | ulong drv_data = dev_get_driver_data(dev); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 288 | u32 reg; |
| 289 | |
| 290 | debug("%s\n", __func__); |
| 291 | |
| 292 | if (!pdata->regs) |
| 293 | return -EIO; |
| 294 | |
Peng Fan | 634fe73 | 2020-05-03 22:19:51 +0800 | [diff] [blame] | 295 | if (drv_data & FLAGS_VER3) { |
| 296 | reg = readl(&pdata->regs->regs_v3.ter); |
| 297 | reg &= ~TER_EN; |
| 298 | writel(reg, &pdata->regs->regs_v3.ter); |
| 299 | |
| 300 | writel(pdata->id << 30, &pdata->regs->regs_v3.tps); |
| 301 | |
| 302 | reg &= ~TER_ALPF; |
| 303 | reg |= 0x1; |
| 304 | reg &= ~TER_ADC_PD; |
| 305 | writel(reg, &pdata->regs->regs_v3.ter); |
| 306 | |
| 307 | /* Enable monitor */ |
| 308 | reg |= TER_EN; |
| 309 | writel(reg, &pdata->regs->regs_v3.ter); |
| 310 | } else if (drv_data & FLAGS_VER2) { |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 311 | reg = readl(&pdata->regs->regs_v2.ter); |
| 312 | reg &= ~TER_EN; |
| 313 | writel(reg, &pdata->regs->regs_v2.ter); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 314 | |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 315 | reg &= ~TER_ALPF; |
| 316 | reg |= 0x1; |
| 317 | writel(reg, &pdata->regs->regs_v2.ter); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 318 | |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 319 | /* Enable monitor */ |
| 320 | reg |= TER_EN; |
| 321 | writel(reg, &pdata->regs->regs_v2.ter); |
| 322 | } else { |
| 323 | /* Clear the ME before setting MSITE and ALPF*/ |
| 324 | reg = readl(&pdata->regs->regs_v1.tmr); |
| 325 | reg &= ~TMR_ME; |
| 326 | writel(reg, &pdata->regs->regs_v1.tmr); |
| 327 | |
| 328 | reg |= 1 << (15 - pdata->id); |
| 329 | reg |= TMR_ALPF; |
| 330 | writel(reg, &pdata->regs->regs_v1.tmr); |
| 331 | |
| 332 | /* Enable ME */ |
| 333 | reg |= TMR_ME; |
| 334 | writel(reg, &pdata->regs->regs_v1.tmr); |
| 335 | } |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int imx_tmu_bind(struct udevice *dev) |
| 341 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 342 | struct imx_tmu_plat *pdata = dev_get_plat(dev); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 343 | int ret; |
| 344 | ofnode node, offset; |
| 345 | const char *name; |
| 346 | const void *prop; |
Tim Harvey | deb78ff | 2021-02-05 16:11:05 -0800 | [diff] [blame] | 347 | int minc, maxc; |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 348 | |
| 349 | debug("%s dev name %s\n", __func__, dev->name); |
| 350 | |
| 351 | prop = dev_read_prop(dev, "compatible", NULL); |
| 352 | if (!prop) |
| 353 | return 0; |
| 354 | |
| 355 | pdata->zone_node = 1; |
Tim Harvey | deb78ff | 2021-02-05 16:11:05 -0800 | [diff] [blame] | 356 | /* default alert/crit temps based on temp grade */ |
| 357 | get_cpu_temp_grade(&minc, &maxc); |
| 358 | pdata->critical = maxc * 1000; |
| 359 | pdata->alert = (maxc - 10) * 1000; |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 360 | |
| 361 | node = ofnode_path("/thermal-zones"); |
| 362 | ofnode_for_each_subnode(offset, node) { |
| 363 | /* Bind the subnode to this driver */ |
| 364 | name = ofnode_get_name(offset); |
| 365 | |
| 366 | ret = device_bind_with_driver_data(dev, dev->driver, name, |
| 367 | dev->driver_data, offset, |
| 368 | NULL); |
| 369 | if (ret) |
| 370 | printf("Error binding driver '%s': %d\n", |
| 371 | dev->driver->name, ret); |
| 372 | } |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | static int imx_tmu_parse_fdt(struct udevice *dev) |
| 378 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 379 | struct imx_tmu_plat *pdata = dev_get_plat(dev), *p_parent_data; |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 380 | struct ofnode_phandle_args args; |
| 381 | ofnode trips_np; |
| 382 | int ret; |
| 383 | |
| 384 | debug("%s dev name %s\n", __func__, dev->name); |
| 385 | |
| 386 | if (pdata->zone_node) { |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 387 | pdata->regs = (union tmu_regs *)dev_read_addr_ptr(dev); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 388 | |
| 389 | if (!pdata->regs) |
| 390 | return -EINVAL; |
| 391 | return 0; |
| 392 | } |
| 393 | |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 394 | p_parent_data = dev_get_plat(dev->parent); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 395 | if (p_parent_data->zone_node) |
| 396 | pdata->regs = p_parent_data->regs; |
| 397 | |
| 398 | ret = dev_read_phandle_with_args(dev, "thermal-sensors", |
| 399 | "#thermal-sensor-cells", |
| 400 | 0, 0, &args); |
| 401 | if (ret) |
| 402 | return ret; |
| 403 | |
| 404 | if (!ofnode_equal(args.node, dev_ofnode(dev->parent))) |
| 405 | return -EFAULT; |
| 406 | |
| 407 | if (args.args_count >= 1) |
| 408 | pdata->id = args.args[0]; |
| 409 | else |
| 410 | pdata->id = 0; |
| 411 | |
| 412 | debug("args.args_count %d, id %d\n", args.args_count, pdata->id); |
| 413 | |
| 414 | pdata->polling_delay = dev_read_u32_default(dev, "polling-delay", 1000); |
| 415 | |
| 416 | trips_np = ofnode_path("/thermal-zones/cpu-thermal/trips"); |
| 417 | ofnode_for_each_subnode(trips_np, trips_np) { |
| 418 | const char *type; |
| 419 | |
| 420 | type = ofnode_get_property(trips_np, "type", NULL); |
| 421 | if (!type) |
| 422 | continue; |
| 423 | if (!strcmp(type, "critical")) |
| 424 | pdata->critical = ofnode_read_u32_default(trips_np, "temperature", 85); |
| 425 | else if (strcmp(type, "passive") == 0) |
| 426 | pdata->alert = ofnode_read_u32_default(trips_np, "temperature", 80); |
| 427 | else |
| 428 | continue; |
| 429 | } |
| 430 | |
| 431 | debug("id %d polling_delay %d, critical %d, alert %d\n", |
| 432 | pdata->id, pdata->polling_delay, pdata->critical, pdata->alert); |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | static int imx_tmu_probe(struct udevice *dev) |
| 438 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 439 | struct imx_tmu_plat *pdata = dev_get_plat(dev); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 440 | int ret; |
| 441 | |
| 442 | ret = imx_tmu_parse_fdt(dev); |
| 443 | if (ret) { |
| 444 | printf("Error in parsing TMU FDT %d\n", ret); |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | if (pdata->zone_node) { |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 449 | imx_tmu_init(dev); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 450 | imx_tmu_calibration(dev); |
Tim Harvey | deb78ff | 2021-02-05 16:11:05 -0800 | [diff] [blame] | 451 | imx_tmu_enable_msite(dev); |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 452 | } else { |
| 453 | imx_tmu_enable_msite(dev); |
| 454 | } |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static const struct udevice_id imx_tmu_ids[] = { |
| 460 | { .compatible = "fsl,imx8mq-tmu", }, |
Peng Fan | fc8657b | 2020-05-03 22:19:47 +0800 | [diff] [blame] | 461 | { .compatible = "fsl,imx8mm-tmu", .data = FLAGS_VER2, }, |
Peng Fan | 634fe73 | 2020-05-03 22:19:51 +0800 | [diff] [blame] | 462 | { .compatible = "fsl,imx8mp-tmu", .data = FLAGS_VER3, }, |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 463 | { } |
| 464 | }; |
| 465 | |
| 466 | U_BOOT_DRIVER(imx_tmu) = { |
| 467 | .name = "imx_tmu", |
| 468 | .id = UCLASS_THERMAL, |
| 469 | .ops = &imx_tmu_ops, |
| 470 | .of_match = imx_tmu_ids, |
| 471 | .bind = imx_tmu_bind, |
| 472 | .probe = imx_tmu_probe, |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 473 | .plat_auto = sizeof(struct imx_tmu_plat), |
Peng Fan | 2cd7f47 | 2020-05-03 22:19:46 +0800 | [diff] [blame] | 474 | .flags = DM_FLAG_PRE_RELOC, |
| 475 | }; |