blob: 02cefecd0d222f8a02b925ceb23b9b64fb97153a [file] [log] [blame]
Peng Fan10de5792020-05-03 22:19:46 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2017~2020 NXP
4 *
5 */
6
7#include <config.h>
8#include <common.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Peng Fan10de5792020-05-03 22:19:46 +080010#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 Harveya77d3092020-10-12 12:21:54 -070018#include <linux/delay.h>
Peng Fan10de5792020-05-03 22:19:46 +080019#include <malloc.h>
20#include <thermal.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
24#define SITES_MAX 16
Peng Fanbef7d2a2020-05-03 22:19:47 +080025#define FLAGS_VER2 0x1
Peng Fanfed211f2020-05-03 22:19:51 +080026#define FLAGS_VER3 0x2
Peng Fan10de5792020-05-03 22:19:46 +080027
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 Fanbef7d2a2020-05-03 22:19:47 +080034#define TER_EN 0x80000000
35#define TER_ADC_PD 0x40000000
Peng Fanfed211f2020-05-03 22:19:51 +080036#define TER_ALPF 0x3
37
Peng Fan10de5792020-05-03 22:19:46 +080038/*
39 * i.MX TMU Registers
40 */
41struct imx_tmu_site_regs {
42 u32 tritsr; /* Immediate Temperature Site Register */
43 u32 tratsr; /* Average Temperature Site Register */
44 u8 res0[0x8];
45};
46
47struct 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 Fanbef7d2a2020-05-03 22:19:47 +080078struct 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 Fanfed211f2020-05-03 22:19:51 +080094struct 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 Fanbef7d2a2020-05-03 22:19:47 +0800113union tmu_regs {
114 struct imx_tmu_regs regs_v1;
115 struct imx_tmu_regs_v2 regs_v2;
Peng Fanfed211f2020-05-03 22:19:51 +0800116 struct imx_tmu_regs_v3 regs_v3;
Peng Fanbef7d2a2020-05-03 22:19:47 +0800117};
118
Peng Fan10de5792020-05-03 22:19:46 +0800119struct imx_tmu_plat {
120 int critical;
121 int alert;
122 int polling_delay;
123 int id;
124 bool zone_node;
Peng Fanbef7d2a2020-05-03 22:19:47 +0800125 union tmu_regs *regs;
Peng Fan10de5792020-05-03 22:19:46 +0800126};
127
128static int read_temperature(struct udevice *dev, int *temp)
129{
Simon Glassfa20e932020-12-03 16:55:20 -0700130 struct imx_tmu_plat *pdata = dev_get_plat(dev);
Peng Fanbef7d2a2020-05-03 22:19:47 +0800131 ulong drv_data = dev_get_driver_data(dev);
Peng Fan10de5792020-05-03 22:19:46 +0800132 u32 val;
Peng Fancabe2312020-05-03 22:19:49 +0800133 u32 retry = 10;
Peng Fan6eff2872020-05-03 22:19:50 +0800134 u32 valid = 0;
Peng Fan10de5792020-05-03 22:19:46 +0800135
136 do {
Peng Fancabe2312020-05-03 22:19:49 +0800137 mdelay(100);
138 retry--;
139
Peng Fanfed211f2020-05-03 22:19:51 +0800140 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 Fanbef7d2a2020-05-03 22:19:47 +0800144 val = readl(&pdata->regs->regs_v2.tritsr);
Peng Fan6eff2872020-05-03 22:19:50 +0800145 /*
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 Fanbef7d2a2020-05-03 22:19:47 +0800151 val = readl(&pdata->regs->regs_v1.site[pdata->id].tritsr);
Peng Fan6eff2872020-05-03 22:19:50 +0800152 valid = val & 0x80000000;
153 }
154 } while (!valid && retry > 0);
Peng Fan10de5792020-05-03 22:19:46 +0800155
Peng Fanfed211f2020-05-03 22:19:51 +0800156 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 Fancabe2312020-05-03 22:19:49 +0800171 return -EINVAL;
Peng Fanfed211f2020-05-03 22:19:51 +0800172 }
Peng Fan10de5792020-05-03 22:19:46 +0800173
174 return 0;
175}
176
177int imx_tmu_get_temp(struct udevice *dev, int *temp)
178{
Simon Glassfa20e932020-12-03 16:55:20 -0700179 struct imx_tmu_plat *pdata = dev_get_plat(dev);
Peng Fan10de5792020-05-03 22:19:46 +0800180 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
201static const struct dm_thermal_ops imx_tmu_ops = {
202 .get_temp = imx_tmu_get_temp,
203};
204
205static int imx_tmu_calibration(struct udevice *dev)
206{
207 int i, val, len, ret;
208 u32 range[4];
209 const fdt32_t *calibration;
Simon Glassfa20e932020-12-03 16:55:20 -0700210 struct imx_tmu_plat *pdata = dev_get_plat(dev);
Peng Fanbef7d2a2020-05-03 22:19:47 +0800211 ulong drv_data = dev_get_driver_data(dev);
Peng Fan10de5792020-05-03 22:19:46 +0800212
213 debug("%s\n", __func__);
214
Peng Fanfed211f2020-05-03 22:19:51 +0800215 if (drv_data & (FLAGS_VER2 | FLAGS_VER3))
Peng Fanbef7d2a2020-05-03 22:19:47 +0800216 return 0;
217
Peng Fan10de5792020-05-03 22:19:46 +0800218 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 Fanbef7d2a2020-05-03 22:19:47 +0800225 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 Fan10de5792020-05-03 22:19:46 +0800229
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 Fanbef7d2a2020-05-03 22:19:47 +0800238 writel(val, &pdata->regs->regs_v1.ttcfgr);
Peng Fan10de5792020-05-03 22:19:46 +0800239 val = fdt32_to_cpu(*(calibration + 1));
Peng Fanbef7d2a2020-05-03 22:19:47 +0800240 writel(val, &pdata->regs->regs_v1.tscfgr);
Peng Fan10de5792020-05-03 22:19:46 +0800241 }
242
243 return 0;
244}
245
Peng Fan4f5026c2020-05-03 22:19:48 +0800246void __weak imx_tmu_arch_init(void *reg_base)
247{
248}
249
Peng Fanbef7d2a2020-05-03 22:19:47 +0800250static void imx_tmu_init(struct udevice *dev)
Peng Fan10de5792020-05-03 22:19:46 +0800251{
Simon Glassfa20e932020-12-03 16:55:20 -0700252 struct imx_tmu_plat *pdata = dev_get_plat(dev);
Peng Fanbef7d2a2020-05-03 22:19:47 +0800253 ulong drv_data = dev_get_driver_data(dev);
254
Peng Fan10de5792020-05-03 22:19:46 +0800255 debug("%s\n", __func__);
256
Peng Fanfed211f2020-05-03 22:19:51 +0800257 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 Fanbef7d2a2020-05-03 22:19:47 +0800265 /* Disable monitoring */
266 writel(0x0, &pdata->regs->regs_v2.ter);
267
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 Fan10de5792020-05-03 22:19:46 +0800273
Peng Fanbef7d2a2020-05-03 22:19:47 +0800274 /* Disable interrupt, using polling instead */
275 writel(TIER_DISABLE, &pdata->regs->regs_v1.tier);
Peng Fan10de5792020-05-03 22:19:46 +0800276
Peng Fanbef7d2a2020-05-03 22:19:47 +0800277 /* Set update_interval */
278 writel(TMTMIR_DEFAULT, &pdata->regs->regs_v1.tmtmir);
279 }
Peng Fan4f5026c2020-05-03 22:19:48 +0800280
281 imx_tmu_arch_init((void *)pdata->regs);
Peng Fan10de5792020-05-03 22:19:46 +0800282}
283
284static int imx_tmu_enable_msite(struct udevice *dev)
285{
Simon Glassfa20e932020-12-03 16:55:20 -0700286 struct imx_tmu_plat *pdata = dev_get_plat(dev);
Peng Fanbef7d2a2020-05-03 22:19:47 +0800287 ulong drv_data = dev_get_driver_data(dev);
Peng Fan10de5792020-05-03 22:19:46 +0800288 u32 reg;
289
290 debug("%s\n", __func__);
291
292 if (!pdata->regs)
293 return -EIO;
294
Peng Fanfed211f2020-05-03 22:19:51 +0800295 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 Fanbef7d2a2020-05-03 22:19:47 +0800311 reg = readl(&pdata->regs->regs_v2.ter);
312 reg &= ~TER_EN;
313 writel(reg, &pdata->regs->regs_v2.ter);
Peng Fan10de5792020-05-03 22:19:46 +0800314
Peng Fanbef7d2a2020-05-03 22:19:47 +0800315 reg &= ~TER_ALPF;
316 reg |= 0x1;
317 writel(reg, &pdata->regs->regs_v2.ter);
Peng Fan10de5792020-05-03 22:19:46 +0800318
Peng Fanbef7d2a2020-05-03 22:19:47 +0800319 /* 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 Fan10de5792020-05-03 22:19:46 +0800336
337 return 0;
338}
339
340static int imx_tmu_bind(struct udevice *dev)
341{
Simon Glassfa20e932020-12-03 16:55:20 -0700342 struct imx_tmu_plat *pdata = dev_get_plat(dev);
Peng Fan10de5792020-05-03 22:19:46 +0800343 int ret;
344 ofnode node, offset;
345 const char *name;
346 const void *prop;
347
348 debug("%s dev name %s\n", __func__, dev->name);
349
350 prop = dev_read_prop(dev, "compatible", NULL);
351 if (!prop)
352 return 0;
353
354 pdata->zone_node = 1;
355
356 node = ofnode_path("/thermal-zones");
357 ofnode_for_each_subnode(offset, node) {
358 /* Bind the subnode to this driver */
359 name = ofnode_get_name(offset);
360
361 ret = device_bind_with_driver_data(dev, dev->driver, name,
362 dev->driver_data, offset,
363 NULL);
364 if (ret)
365 printf("Error binding driver '%s': %d\n",
366 dev->driver->name, ret);
367 }
368
369 return 0;
370}
371
372static int imx_tmu_parse_fdt(struct udevice *dev)
373{
Simon Glassfa20e932020-12-03 16:55:20 -0700374 struct imx_tmu_plat *pdata = dev_get_plat(dev), *p_parent_data;
Peng Fan10de5792020-05-03 22:19:46 +0800375 struct ofnode_phandle_args args;
376 ofnode trips_np;
377 int ret;
378
379 debug("%s dev name %s\n", __func__, dev->name);
380
381 if (pdata->zone_node) {
Peng Fanbef7d2a2020-05-03 22:19:47 +0800382 pdata->regs = (union tmu_regs *)dev_read_addr_ptr(dev);
Peng Fan10de5792020-05-03 22:19:46 +0800383
384 if (!pdata->regs)
385 return -EINVAL;
386 return 0;
387 }
388
Simon Glassfa20e932020-12-03 16:55:20 -0700389 p_parent_data = dev_get_plat(dev->parent);
Peng Fan10de5792020-05-03 22:19:46 +0800390 if (p_parent_data->zone_node)
391 pdata->regs = p_parent_data->regs;
392
393 ret = dev_read_phandle_with_args(dev, "thermal-sensors",
394 "#thermal-sensor-cells",
395 0, 0, &args);
396 if (ret)
397 return ret;
398
399 if (!ofnode_equal(args.node, dev_ofnode(dev->parent)))
400 return -EFAULT;
401
402 if (args.args_count >= 1)
403 pdata->id = args.args[0];
404 else
405 pdata->id = 0;
406
407 debug("args.args_count %d, id %d\n", args.args_count, pdata->id);
408
409 pdata->polling_delay = dev_read_u32_default(dev, "polling-delay", 1000);
410
411 trips_np = ofnode_path("/thermal-zones/cpu-thermal/trips");
412 ofnode_for_each_subnode(trips_np, trips_np) {
413 const char *type;
414
415 type = ofnode_get_property(trips_np, "type", NULL);
416 if (!type)
417 continue;
418 if (!strcmp(type, "critical"))
419 pdata->critical = ofnode_read_u32_default(trips_np, "temperature", 85);
420 else if (strcmp(type, "passive") == 0)
421 pdata->alert = ofnode_read_u32_default(trips_np, "temperature", 80);
422 else
423 continue;
424 }
425
426 debug("id %d polling_delay %d, critical %d, alert %d\n",
427 pdata->id, pdata->polling_delay, pdata->critical, pdata->alert);
428
429 return 0;
430}
431
432static int imx_tmu_probe(struct udevice *dev)
433{
Simon Glassfa20e932020-12-03 16:55:20 -0700434 struct imx_tmu_plat *pdata = dev_get_plat(dev);
Peng Fan10de5792020-05-03 22:19:46 +0800435 int ret;
436
437 ret = imx_tmu_parse_fdt(dev);
438 if (ret) {
439 printf("Error in parsing TMU FDT %d\n", ret);
440 return ret;
441 }
442
443 if (pdata->zone_node) {
Peng Fanbef7d2a2020-05-03 22:19:47 +0800444 imx_tmu_init(dev);
Peng Fan10de5792020-05-03 22:19:46 +0800445 imx_tmu_calibration(dev);
446 } else {
447 imx_tmu_enable_msite(dev);
448 }
449
450 return 0;
451}
452
453static const struct udevice_id imx_tmu_ids[] = {
454 { .compatible = "fsl,imx8mq-tmu", },
Peng Fanbef7d2a2020-05-03 22:19:47 +0800455 { .compatible = "fsl,imx8mm-tmu", .data = FLAGS_VER2, },
Peng Fanfed211f2020-05-03 22:19:51 +0800456 { .compatible = "fsl,imx8mp-tmu", .data = FLAGS_VER3, },
Peng Fan10de5792020-05-03 22:19:46 +0800457 { }
458};
459
460U_BOOT_DRIVER(imx_tmu) = {
461 .name = "imx_tmu",
462 .id = UCLASS_THERMAL,
463 .ops = &imx_tmu_ops,
464 .of_match = imx_tmu_ids,
465 .bind = imx_tmu_bind,
466 .probe = imx_tmu_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700467 .plat_auto = sizeof(struct imx_tmu_plat),
Peng Fan10de5792020-05-03 22:19:46 +0800468 .flags = DM_FLAG_PRE_RELOC,
469};