blob: ca45abbb8e1d9b5c891cf19af24767c708592c38 [file] [log] [blame]
Peng Fan2cd7f472020-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 Glass401d1c42020-10-30 21:38:53 -06009#include <asm/global_data.h>
Peng Fan2cd7f472020-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 Harvey85abf042020-10-12 12:21:54 -070018#include <linux/delay.h>
Peng Fan2cd7f472020-05-03 22:19:46 +080019#include <malloc.h>
20#include <thermal.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
24#define SITES_MAX 16
Wolfgang Denk0cf207e2021-09-27 17:42:39 +020025#define FLAGS_VER2 0x1
26#define FLAGS_VER3 0x2
Peng Fan2cd7f472020-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 Fanfc8657b2020-05-03 22:19:47 +080034#define TER_EN 0x80000000
35#define TER_ADC_PD 0x40000000
Peng Fan634fe732020-05-03 22:19:51 +080036#define TER_ALPF 0x3
37
Peng Fan2cd7f472020-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 Fanfc8657b2020-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 Fan634fe732020-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 Fanfc8657b2020-05-03 22:19:47 +0800113union tmu_regs {
114 struct imx_tmu_regs regs_v1;
115 struct imx_tmu_regs_v2 regs_v2;
Peng Fan634fe732020-05-03 22:19:51 +0800116 struct imx_tmu_regs_v3 regs_v3;
Peng Fanfc8657b2020-05-03 22:19:47 +0800117};
118
Peng Fan2cd7f472020-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 Fanfc8657b2020-05-03 22:19:47 +0800125 union tmu_regs *regs;
Peng Fan2cd7f472020-05-03 22:19:46 +0800126};
127
128static int read_temperature(struct udevice *dev, int *temp)
129{
Simon Glassc69cda22020-12-03 16:55:20 -0700130 struct imx_tmu_plat *pdata = dev_get_plat(dev);
Peng Fanfc8657b2020-05-03 22:19:47 +0800131 ulong drv_data = dev_get_driver_data(dev);
Peng Fan2cd7f472020-05-03 22:19:46 +0800132 u32 val;
Peng Fanb5447b92020-05-03 22:19:49 +0800133 u32 retry = 10;
Peng Fan951bf192020-05-03 22:19:50 +0800134 u32 valid = 0;
Peng Fan2cd7f472020-05-03 22:19:46 +0800135
136 do {
Peng Fanb5447b92020-05-03 22:19:49 +0800137 mdelay(100);
138 retry--;
139
Peng Fan634fe732020-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 Fanfc8657b2020-05-03 22:19:47 +0800144 val = readl(&pdata->regs->regs_v2.tritsr);
Peng Fan951bf192020-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 Fanfc8657b2020-05-03 22:19:47 +0800151 val = readl(&pdata->regs->regs_v1.site[pdata->id].tritsr);
Peng Fan951bf192020-05-03 22:19:50 +0800152 valid = val & 0x80000000;
153 }
154 } while (!valid && retry > 0);
Peng Fan2cd7f472020-05-03 22:19:46 +0800155
Peng Fan634fe732020-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 Fanb5447b92020-05-03 22:19:49 +0800171 return -EINVAL;
Peng Fan634fe732020-05-03 22:19:51 +0800172 }
Peng Fan2cd7f472020-05-03 22:19:46 +0800173
174 return 0;
175}
176
177int imx_tmu_get_temp(struct udevice *dev, int *temp)
178{
Simon Glassc69cda22020-12-03 16:55:20 -0700179 struct imx_tmu_plat *pdata = dev_get_plat(dev);
Peng Fan2cd7f472020-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 Glassc69cda22020-12-03 16:55:20 -0700210 struct imx_tmu_plat *pdata = dev_get_plat(dev);
Peng Fanfc8657b2020-05-03 22:19:47 +0800211 ulong drv_data = dev_get_driver_data(dev);
Peng Fan2cd7f472020-05-03 22:19:46 +0800212
213 debug("%s\n", __func__);
214
Peng Fan634fe732020-05-03 22:19:51 +0800215 if (drv_data & (FLAGS_VER2 | FLAGS_VER3))
Peng Fanfc8657b2020-05-03 22:19:47 +0800216 return 0;
217
Peng Fan2cd7f472020-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 Fanfc8657b2020-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 Fan2cd7f472020-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 Fanfc8657b2020-05-03 22:19:47 +0800238 writel(val, &pdata->regs->regs_v1.ttcfgr);
Peng Fan2cd7f472020-05-03 22:19:46 +0800239 val = fdt32_to_cpu(*(calibration + 1));
Peng Fanfc8657b2020-05-03 22:19:47 +0800240 writel(val, &pdata->regs->regs_v1.tscfgr);
Peng Fan2cd7f472020-05-03 22:19:46 +0800241 }
242
243 return 0;
244}
245
Peng Fan84897402020-05-03 22:19:48 +0800246void __weak imx_tmu_arch_init(void *reg_base)
247{
248}
249
Peng Fanfc8657b2020-05-03 22:19:47 +0800250static void imx_tmu_init(struct udevice *dev)
Peng Fan2cd7f472020-05-03 22:19:46 +0800251{
Simon Glassc69cda22020-12-03 16:55:20 -0700252 struct imx_tmu_plat *pdata = dev_get_plat(dev);
Peng Fanfc8657b2020-05-03 22:19:47 +0800253 ulong drv_data = dev_get_driver_data(dev);
254
Peng Fan2cd7f472020-05-03 22:19:46 +0800255 debug("%s\n", __func__);
256
Peng Fan634fe732020-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 Fanfc8657b2020-05-03 22:19:47 +0800265 /* Disable monitoring */
266 writel(0x0, &pdata->regs->regs_v2.ter);
Peng Fan2cd7f472020-05-03 22:19:46 +0800267
Peng Fanfc8657b2020-05-03 22:19:47 +0800268 /* 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 Fan2cd7f472020-05-03 22:19:46 +0800273
Peng Fanfc8657b2020-05-03 22:19:47 +0800274 /* 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 Fan84897402020-05-03 22:19:48 +0800280
281 imx_tmu_arch_init((void *)pdata->regs);
Peng Fan2cd7f472020-05-03 22:19:46 +0800282}
283
284static int imx_tmu_enable_msite(struct udevice *dev)
285{
Simon Glassc69cda22020-12-03 16:55:20 -0700286 struct imx_tmu_plat *pdata = dev_get_plat(dev);
Peng Fanfc8657b2020-05-03 22:19:47 +0800287 ulong drv_data = dev_get_driver_data(dev);
Peng Fan2cd7f472020-05-03 22:19:46 +0800288 u32 reg;
289
290 debug("%s\n", __func__);
291
292 if (!pdata->regs)
293 return -EIO;
294
Peng Fan634fe732020-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 Fanfc8657b2020-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 Fan2cd7f472020-05-03 22:19:46 +0800314
Peng Fanfc8657b2020-05-03 22:19:47 +0800315 reg &= ~TER_ALPF;
316 reg |= 0x1;
317 writel(reg, &pdata->regs->regs_v2.ter);
Peng Fan2cd7f472020-05-03 22:19:46 +0800318
Peng Fanfc8657b2020-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 Fan2cd7f472020-05-03 22:19:46 +0800336
337 return 0;
338}
339
340static int imx_tmu_bind(struct udevice *dev)
341{
Simon Glassc69cda22020-12-03 16:55:20 -0700342 struct imx_tmu_plat *pdata = dev_get_plat(dev);
Peng Fan2cd7f472020-05-03 22:19:46 +0800343 int ret;
344 ofnode node, offset;
345 const char *name;
346 const void *prop;
Tim Harveydeb78ff2021-02-05 16:11:05 -0800347 int minc, maxc;
Peng Fan2cd7f472020-05-03 22:19:46 +0800348
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 Harveydeb78ff2021-02-05 16:11:05 -0800356 /* 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 Fan2cd7f472020-05-03 22:19:46 +0800360
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
377static int imx_tmu_parse_fdt(struct udevice *dev)
378{
Simon Glassc69cda22020-12-03 16:55:20 -0700379 struct imx_tmu_plat *pdata = dev_get_plat(dev), *p_parent_data;
Peng Fan2cd7f472020-05-03 22:19:46 +0800380 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 Fanfc8657b2020-05-03 22:19:47 +0800387 pdata->regs = (union tmu_regs *)dev_read_addr_ptr(dev);
Peng Fan2cd7f472020-05-03 22:19:46 +0800388
389 if (!pdata->regs)
390 return -EINVAL;
391 return 0;
392 }
393
Simon Glassc69cda22020-12-03 16:55:20 -0700394 p_parent_data = dev_get_plat(dev->parent);
Peng Fan2cd7f472020-05-03 22:19:46 +0800395 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
437static int imx_tmu_probe(struct udevice *dev)
438{
Simon Glassc69cda22020-12-03 16:55:20 -0700439 struct imx_tmu_plat *pdata = dev_get_plat(dev);
Peng Fan2cd7f472020-05-03 22:19:46 +0800440 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 Fanfc8657b2020-05-03 22:19:47 +0800449 imx_tmu_init(dev);
Peng Fan2cd7f472020-05-03 22:19:46 +0800450 imx_tmu_calibration(dev);
Tim Harveydeb78ff2021-02-05 16:11:05 -0800451 imx_tmu_enable_msite(dev);
Peng Fan2cd7f472020-05-03 22:19:46 +0800452 } else {
453 imx_tmu_enable_msite(dev);
454 }
455
456 return 0;
457}
458
459static const struct udevice_id imx_tmu_ids[] = {
460 { .compatible = "fsl,imx8mq-tmu", },
Peng Fanfc8657b2020-05-03 22:19:47 +0800461 { .compatible = "fsl,imx8mm-tmu", .data = FLAGS_VER2, },
Peng Fan634fe732020-05-03 22:19:51 +0800462 { .compatible = "fsl,imx8mp-tmu", .data = FLAGS_VER3, },
Peng Fan2cd7f472020-05-03 22:19:46 +0800463 { }
464};
465
466U_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 Glasscaa4daa2020-12-03 16:55:18 -0700473 .plat_auto = sizeof(struct imx_tmu_plat),
Peng Fan2cd7f472020-05-03 22:19:46 +0800474 .flags = DM_FLAG_PRE_RELOC,
475};