aboutsummaryrefslogtreecommitdiff
path: root/sys/arm/nvidia/tegra_soctherm.c
blob: d90e97e5915950da478b02a2fea481a5aaf8f04a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/*-
 * Copyright (c) 2016 Michal Meloun <mmel@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

/*
 * Thermometer and thermal zones driver for Tegra SoCs.
 * Calibration data and algo are taken from Linux, because this part of SoC
 * is undocumented in TRM.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/gpio.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/malloc.h>
#include <sys/rman.h>
#include <sys/sysctl.h>

#include <machine/bus.h>

#include <dev/extres/clk/clk.h>
#include <dev/extres/hwreset/hwreset.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

#include <arm/nvidia/tegra_efuse.h>
#include <dt-bindings/thermal/tegra124-soctherm.h>
#include "tegra_soctherm_if.h"

/* Per sensors registers - base is 0x0c0*/
#define	TSENSOR_CONFIG0				0x000
#define	 TSENSOR_CONFIG0_TALL(x)			(((x) & 0xFFFFF) << 8)
#define	 TSENSOR_CONFIG0_STATUS_CLR			(1 << 5)
#define	 TSENSOR_CONFIG0_TCALC_OVERFLOW			(1 << 4)
#define	 TSENSOR_CONFIG0_OVERFLOW			(1 << 3)
#define	 TSENSOR_CONFIG0_CPTR_OVERFLOW			(1 << 2)
#define	 TSENSOR_CONFIG0_RO_SEL				(1 << 1)
#define	 TSENSOR_CONFIG0_STOP				(1 << 0)

#define	TSENSOR_CONFIG1				0x004
#define	 TSENSOR_CONFIG1_TEMP_ENABLE			(1U << 31)
#define	 TSENSOR_CONFIG1_TEN_COUNT(x)			(((x) & 0x3F) << 24)
#define	 TSENSOR_CONFIG1_TIDDQ_EN(x)			(((x) & 0x3F) << 15)
#define	 TSENSOR_CONFIG1_TSAMPLE(x)			(((x) & 0x3FF) << 0)

#define	TSENSOR_CONFIG2				0x008
#define	TSENSOR_CONFIG2_THERMA(x)			(((x) & 0xFFFF) << 16)
#define	TSENSOR_CONFIG2_THERMB(x)			(((x) & 0xFFFF) << 0)

#define	TSENSOR_STATUS0				0x00c
#define	 TSENSOR_STATUS0_CAPTURE_VALID			(1U << 31)
#define	 TSENSOR_STATUS0_CAPTURE(x)			(((x) >> 0) & 0xffff)

#define	TSENSOR_STATUS1				0x010
#define	 TSENSOR_STATUS1_TEMP_VALID			(1U << 31)
#define	 TSENSOR_STATUS1_TEMP(x)			(((x) >> 0) & 0xffff)

#define	TSENSOR_STATUS2				0x014
#define	 TSENSOR_STATUS2_TEMP_MAX(x)			(((x) >> 16) & 0xffff)
#define	 TSENSOR_STATUS2_TEMP_MIN(x)			(((x) >>  0) & 0xffff)


/* Readbacks */
#define	READBACK_VALUE(x)				(((x) >> 8) & 0xff)
#define	READBACK_ADD_HALF				(1 << 7)
#define	READBACK_NEGATE					(1 << 0)

/* Global registers */
#define	TSENSOR_PDIV				0x1c0
#define	TSENSOR_HOTSPOT_OFF			0x1c4
#define	TSENSOR_TEMP1				0x1c8
#define	TSENSOR_TEMP2				0x1cc

/* Fuses */
#define	 FUSE_TSENSOR_CALIB_CP_TS_BASE_SHIFT		0
#define	 FUSE_TSENSOR_CALIB_CP_TS_BASE_BITS		13
#define	 FUSE_TSENSOR_CALIB_FT_TS_BASE_SHIFT		13
#define	 FUSE_TSENSOR_CALIB_FT_TS_BASE_BITS		13

/* Layout is different for Tegra124 and Tegra210 */
#define	FUSE_TSENSOR_COMMON			0x180
#define	 TEGRA124_FUSE_COMMON_CP_TS_BASE(x)		(((x) >>  0) & 0x3ff)
#define	 TEGRA124_FUSE_COMMON_FT_TS_BASE(x)		(((x) >> 10) & 0x7ff)
#define	 TEGRA124_FUSE_COMMON_SHIFT_FT_SHIFT		21
#define	 TEGRA124_FUSE_COMMON_SHIFT_FT_BITS 		5

#define	 TEGRA210_FUSE_COMMON_CP_TS_BASE(x)		(((x) >>  11) & 0x3ff)
#define	 TEGRA210_FUSE_COMMON_FT_TS_BASE(x)		(((x) >> 21) & 0x7ff)
#define	 TEGRA210_FUSE_COMMON_SHIFT_CP_SHIFT		0
#define	 TEGRA210_FUSE_COMMON_SHIFT_CP_BITS		6
#define	 TEGRA210_FUSE_COMMON_SHIFT_FT_SHIFT		6
#define	 TEGRA210_FUSE_COMMON_SHIFT_FT_BITS 		5


/* Only for Tegra124 */
#define	FUSE_SPARE_REALIGNMENT_REG		0x1fc
#define	 FUSE_SPARE_REALIGNMENT_REG_SHIFT_CP_SHIFT 	0
#define	 FUSE_SPARE_REALIGNMENT_REG_SHIFT_CP_BITS 	6

#define	TEGRA124_NOMINAL_CALIB_FT	105
#define	TEGRA124_NOMINAL_CALIB_CP	25

#define	TEGRA210_NOMINAL_CALIB_FT	105
#define	TEGRA210_NOMINAL_CALIB_CP	25

#define	WR4(_sc, _r, _v)	bus_write_4((_sc)->mem_res, (_r), (_v))
#define	RD4(_sc, _r)		bus_read_4((_sc)->mem_res, (_r))

static struct sysctl_ctx_list soctherm_sysctl_ctx;

struct tsensor_cfg {
	uint32_t		tall;
	uint32_t		tsample;
	uint32_t		tiddq_en;
	uint32_t		ten_count;
	uint32_t		pdiv;
	uint32_t		tsample_ate;
	uint32_t		pdiv_ate;
};

struct soctherm_shared_cal {
	uint32_t		base_cp;
	uint32_t		base_ft;
	int32_t			actual_temp_cp;
	int32_t			actual_temp_ft;
};

struct tsensor {
	char 			*name;
	int			id;
	bus_addr_t		sensor_base;
	bus_addr_t		calib_fuse;
	int 			fuse_corr_alpha;
	int			fuse_corr_beta;

	int16_t			therm_a;
	int16_t			therm_b;
};

struct soctherm_soc;
struct soctherm_softc {
	device_t		dev;
	struct resource		*mem_res;
	struct resource		*irq_res;
	void			*irq_ih;

	clk_t			tsensor_clk;
	clk_t			soctherm_clk;
	hwreset_t		reset;

	struct soctherm_soc	*soc;
	struct soctherm_shared_cal shared_cal;
};

struct soctherm_soc {
	void			(*shared_cal)(struct soctherm_softc *sc);
	uint32_t		tsensor_pdiv;
	uint32_t		tsensor_hotspot_off;
	struct tsensor_cfg	*tsensor_cfg;
	struct tsensor		*tsensors;
	int			ntsensors;
};

/* Tegra124 config */

static struct tsensor_cfg t124_tsensor_config = {
	.tall = 16300,
	.tsample = 120,
	.tiddq_en = 1,
	.ten_count = 1,
	.pdiv = 8,
	.tsample_ate = 480,
	.pdiv_ate = 8
};

static struct tsensor t124_tsensors[] = {
	{
		.name = "cpu0",
		.id = TEGRA124_SOCTHERM_SENSOR_CPU,
		.sensor_base = 0x0c0,
		.calib_fuse = 0x098,
		.fuse_corr_alpha = 1135400,
		.fuse_corr_beta = -6266900,
	},
	{
		.name = "cpu1",
		.id = -1,
		.sensor_base = 0x0e0,
		.calib_fuse = 0x084,
		.fuse_corr_alpha = 1122220,
		.fuse_corr_beta = -5700700,
	},
	{
		.name = "cpu2",
		.id = -1,
		.sensor_base = 0x100,
		.calib_fuse = 0x088,
		.fuse_corr_alpha = 1127000,
		.fuse_corr_beta = -6768200,
	},
	{
		.name = "cpu3",
		.id = -1,
		.sensor_base = 0x120,
		.calib_fuse = 0x12c,
		.fuse_corr_alpha = 1110900,
		.fuse_corr_beta = -6232000,
	},
	{
		.name = "mem0",
		.id = TEGRA124_SOCTHERM_SENSOR_MEM,
		.sensor_base = 0x140,
		.calib_fuse = 0x158,
		.fuse_corr_alpha = 1122300,
		.fuse_corr_beta = -5936400,
	},
	{
		.name = "mem1",
		.id = -1,
		.sensor_base = 0x160,
		.calib_fuse = 0x15c,
		.fuse_corr_alpha = 1145700,
		.fuse_corr_beta = -7124600,
	},
	{
		.name = "gpu",
		.id = TEGRA124_SOCTHERM_SENSOR_GPU,
		.sensor_base = 0x180,
		.calib_fuse = 0x154,
		.fuse_corr_alpha = 1120100,
		.fuse_corr_beta = -6000500,
	},
	{
		.name = "pllX",
		.id = TEGRA124_SOCTHERM_SENSOR_PLLX,
		.sensor_base = 0x1a0,
		.calib_fuse = 0x160,
		.fuse_corr_alpha = 1106500,
		.fuse_corr_beta = -6729300,
	},
};

static void tegra124_shared_cal(struct soctherm_softc *sc);

static struct soctherm_soc tegra124_soc = {
	.shared_cal = tegra124_shared_cal,
	.tsensor_pdiv = 0x8888,
	.tsensor_hotspot_off = 0x00060600 ,
	.tsensor_cfg = &t124_tsensor_config,
	.tsensors = t124_tsensors,
	.ntsensors = nitems(t124_tsensors),
};

/* Tegra210 config */
static struct tsensor_cfg t210_tsensor_config = {
	.tall = 16300,
	.tsample = 120,
	.tiddq_en = 1,
	.ten_count = 1,
	.pdiv = 8,
	.tsample_ate = 480,
	.pdiv_ate = 8
};

static struct tsensor t210_tsensors[] = {
	{
		.name = "cpu0",
		.id = TEGRA124_SOCTHERM_SENSOR_CPU,
		.sensor_base = 0x0c0,
		.calib_fuse = 0x098,
		.fuse_corr_alpha = 1085000,
		.fuse_corr_beta = 3244200,
	},
	{
		.name = "cpu1",
		.id = -1,
		.sensor_base = 0x0e0,
		.calib_fuse = 0x084,
		.fuse_corr_alpha = 1126200,
		.fuse_corr_beta = -67500,
	},
	{
		.name = "cpu2",
		.id = -1,
		.sensor_base = 0x100,
		.calib_fuse = 0x088,
		.fuse_corr_alpha = 1098400,
		.fuse_corr_beta = 2251100,
	},
	{
		.name = "cpu3",
		.id = -1,
		.sensor_base = 0x120,
		.calib_fuse = 0x12c,
		.fuse_corr_alpha = 1108000,
		.fuse_corr_beta = 602700,
	},
	{
		.name = "mem0",
		.id = TEGRA124_SOCTHERM_SENSOR_MEM,
		.sensor_base = 0x140,
		.calib_fuse = 0x158,
		.fuse_corr_alpha = 1069200,
		.fuse_corr_beta = 3549900,
	},
	{
		.name = "mem1",
		.id = -1,
		.sensor_base = 0x160,
		.calib_fuse = 0x15c,
		.fuse_corr_alpha = 1173700,
		.fuse_corr_beta = -6263600,
	},
	{
		.name = "gpu",
		.id = TEGRA124_SOCTHERM_SENSOR_GPU,
		.sensor_base = 0x180,
		.calib_fuse = 0x154,
		.fuse_corr_alpha = 1074300,
		.fuse_corr_beta = 2734900,
	},
	{
		.name = "pllX",
		.id = TEGRA124_SOCTHERM_SENSOR_PLLX,
		.sensor_base = 0x1a0,
		.calib_fuse = 0x160,
		.fuse_corr_alpha = 1039700,
		.fuse_corr_beta = 6829100,
	},
};

static void tegra210_shared_cal(struct soctherm_softc *sc);

static struct soctherm_soc tegra210_soc = {
	.shared_cal = tegra210_shared_cal,
	.tsensor_pdiv = 0x8888,
	.tsensor_hotspot_off = 0x000A0500 ,
	.tsensor_cfg = &t210_tsensor_config,
	.tsensors = t210_tsensors,
	.ntsensors = nitems(t210_tsensors),
};

static struct ofw_compat_data compat_data[] = {
	{"nvidia,tegra124-soctherm", (uintptr_t)&tegra124_soc},
	{"nvidia,tegra210-soctherm", (uintptr_t)&tegra210_soc},
	{NULL,				0},
};

/* Extract signed integer bitfield from register */
static int
extract_signed(uint32_t reg, int shift, int bits)
{
	int32_t val;
	uint32_t mask;

	mask = (1 << bits) - 1;
	val = ((reg >> shift) & mask) << (32 - bits);
	val >>= 32 - bits;
	return ((int32_t)val);
}

static inline
int64_t div64_s64_precise(int64_t a, int64_t b)
{
	int64_t r, al;

	al = a << 16;
	r = (al * 2 + 1) / (2 * b);
	return (r >> 16);
}

static void
tegra124_shared_cal(struct soctherm_softc *sc)
{
	uint32_t val;
	int calib_cp, calib_ft;
	struct soctherm_shared_cal *cal;

	cal = &sc->shared_cal;
	val = tegra_fuse_read_4(FUSE_TSENSOR_COMMON);
	cal->base_cp = TEGRA124_FUSE_COMMON_CP_TS_BASE(val);
	cal->base_ft = TEGRA124_FUSE_COMMON_FT_TS_BASE(val);

	calib_ft = extract_signed(val,
	    TEGRA124_FUSE_COMMON_SHIFT_FT_SHIFT,
	    TEGRA124_FUSE_COMMON_SHIFT_FT_BITS);

	val = tegra_fuse_read_4(FUSE_SPARE_REALIGNMENT_REG);
	calib_cp = extract_signed(val,
	    FUSE_SPARE_REALIGNMENT_REG_SHIFT_CP_SHIFT,
	    FUSE_SPARE_REALIGNMENT_REG_SHIFT_CP_BITS);

	cal->actual_temp_cp = 2 * TEGRA124_NOMINAL_CALIB_CP + calib_cp;
	cal->actual_temp_ft = 2 * TEGRA124_NOMINAL_CALIB_FT + calib_ft;
#ifdef DEBUG
	printf("%s: base_cp: %u, base_ft: %d,"
	    " actual_temp_cp: %d, actual_temp_ft: %d\n",
	    __func__, cal->base_cp, cal->base_ft,
	    cal->actual_temp_cp, cal->actual_temp_ft);
#endif
}

static void
tegra210_shared_cal(struct soctherm_softc *sc)
{
	uint32_t val;
	int calib_cp, calib_ft;
	struct soctherm_shared_cal *cal;

	cal = &sc->shared_cal;

	val = tegra_fuse_read_4(FUSE_TSENSOR_COMMON);
	cal->base_cp = TEGRA210_FUSE_COMMON_CP_TS_BASE(val);
	cal->base_ft = TEGRA210_FUSE_COMMON_FT_TS_BASE(val);

	calib_ft = extract_signed(val,
	    TEGRA210_FUSE_COMMON_SHIFT_FT_SHIFT,
	    TEGRA210_FUSE_COMMON_SHIFT_FT_BITS);
	calib_cp = extract_signed(val,
	    TEGRA210_FUSE_COMMON_SHIFT_CP_SHIFT,
	    TEGRA210_FUSE_COMMON_SHIFT_CP_BITS);

	cal->actual_temp_cp = 2 * TEGRA210_NOMINAL_CALIB_CP + calib_cp;
	cal->actual_temp_ft = 2 * TEGRA210_NOMINAL_CALIB_FT + calib_ft;
#ifdef DEBUG
	printf("%s: base_cp: %u, base_ft: %d,"
	    " actual_temp_cp: %d, actual_temp_ft: %d\n",
	    __func__, cal->base_cp, cal->base_ft,
	    cal->actual_temp_cp, cal->actual_temp_ft);
#endif
}

static void
tsensor_calibration(struct soctherm_softc *sc, struct tsensor *sensor)
{
	uint32_t val;
	int mult, div, calib_cp, calib_ft;
	int actual_tsensor_ft, actual_tsensor_cp, delta_sens, delta_temp;
	int temp_a, temp_b;
	struct tsensor_cfg *cfg;
	struct soctherm_shared_cal *cal;
	int64_t tmp;

	cfg = sc->soc->tsensor_cfg;
	cal = &sc->shared_cal;

	val =  tegra_fuse_read_4(sensor->calib_fuse);
	calib_cp = extract_signed(val,
	    FUSE_TSENSOR_CALIB_CP_TS_BASE_SHIFT,
	    FUSE_TSENSOR_CALIB_CP_TS_BASE_BITS);
	actual_tsensor_cp = cal->base_cp * 64 + calib_cp;

	calib_ft = extract_signed(val,
	    FUSE_TSENSOR_CALIB_FT_TS_BASE_SHIFT,
	    FUSE_TSENSOR_CALIB_FT_TS_BASE_BITS);
	actual_tsensor_ft = cal->base_ft * 32 + calib_ft;

	delta_sens = actual_tsensor_ft - actual_tsensor_cp;
	delta_temp = cal->actual_temp_ft - cal->actual_temp_cp;
	mult = cfg->pdiv * cfg->tsample_ate;
	div = cfg->tsample * cfg->pdiv_ate;

	temp_a = div64_s64_precise((int64_t) delta_temp * (1LL << 13) * mult,
				   (int64_t) delta_sens * div);

	tmp = (int64_t)actual_tsensor_ft * cal->actual_temp_cp -
	      (int64_t)actual_tsensor_cp * cal->actual_temp_ft;
	temp_b = div64_s64_precise(tmp, (int64_t)delta_sens);

	temp_a = div64_s64_precise((int64_t)temp_a * sensor->fuse_corr_alpha,
				   1000000);
	temp_b = div64_s64_precise((int64_t)temp_b * sensor->fuse_corr_alpha +
				   sensor->fuse_corr_beta, 1000000);
	sensor->therm_a = (int16_t)temp_a;
	sensor->therm_b = (int16_t)temp_b;
#ifdef DEBUG
	printf("%s: sensor %s fuse: 0x%08X (0x%04X, 0x%04X)"
	    " calib_cp: %d(0x%04X), calib_ft: %d(0x%04X)\n",
	    __func__, sensor->name, val, val & 0x1FFF, (val >> 13) & 0x1FFF,
	    calib_cp, calib_cp, calib_ft, calib_ft);
	printf("therma: 0x%04X(%d), thermb: 0x%04X(%d)\n",
	    (uint16_t)sensor->therm_a, sensor->therm_a,
	    (uint16_t)sensor->therm_b, sensor->therm_b);
#endif
}

static void
soctherm_init_tsensor(struct soctherm_softc *sc, struct tsensor *sensor)
{
	struct tsensor_cfg *cfg;
	uint32_t val;

	cfg = sc->soc->tsensor_cfg;
	tsensor_calibration(sc, sensor);

	val = RD4(sc, sensor->sensor_base + TSENSOR_CONFIG0);
	val |= TSENSOR_CONFIG0_STOP;
	val |= TSENSOR_CONFIG0_STATUS_CLR;
	WR4(sc, sensor->sensor_base + TSENSOR_CONFIG0, val);

	val = TSENSOR_CONFIG0_TALL(cfg->tall);
	val |= TSENSOR_CONFIG0_STOP;
	WR4(sc, sensor->sensor_base + TSENSOR_CONFIG0, val);

	val = TSENSOR_CONFIG1_TSAMPLE(cfg->tsample - 1);
	val |= TSENSOR_CONFIG1_TIDDQ_EN(cfg->tiddq_en);
	val |= TSENSOR_CONFIG1_TEN_COUNT(cfg->ten_count);
	val |= TSENSOR_CONFIG1_TEMP_ENABLE;
	WR4(sc, sensor->sensor_base + TSENSOR_CONFIG1, val);

	val = TSENSOR_CONFIG2_THERMA((uint16_t)sensor->therm_a) |
	     TSENSOR_CONFIG2_THERMB((uint16_t)sensor->therm_b);
	WR4(sc, sensor->sensor_base + TSENSOR_CONFIG2, val);

	val = RD4(sc, sensor->sensor_base + TSENSOR_CONFIG0);
	val &= ~TSENSOR_CONFIG0_STOP;
	WR4(sc, sensor->sensor_base + TSENSOR_CONFIG0, val);
#ifdef DEBUG
	printf(" Sensor: %s  cfg:0x%08X, 0x%08X, 0x%08X,"
	    " sts:0x%08X, 0x%08X, 0x%08X\n", sensor->name,
	    RD4(sc, sensor->sensor_base + TSENSOR_CONFIG0),
	    RD4(sc, sensor->sensor_base + TSENSOR_CONFIG1),
	    RD4(sc, sensor->sensor_base + TSENSOR_CONFIG2),
	    RD4(sc, sensor->sensor_base + TSENSOR_STATUS0),
	    RD4(sc, sensor->sensor_base + TSENSOR_STATUS1),
	    RD4(sc, sensor->sensor_base + TSENSOR_STATUS2)
	    );
#endif
}

static int
soctherm_convert_raw(uint32_t val)
{
	int32_t t;

	t = READBACK_VALUE(val) * 1000;
	if (val & READBACK_ADD_HALF)
		t += 500;
	if (val & READBACK_NEGATE)
		t *= -1;

	return (t);
}

static int
soctherm_read_temp(struct soctherm_softc *sc, struct tsensor *sensor, int *temp)
{
	int timeout;
	uint32_t val;

	/* wait for valid sample */
	for (timeout = 100; timeout > 0; timeout--) {
		val = RD4(sc, sensor->sensor_base + TSENSOR_STATUS1);
		if ((val & TSENSOR_STATUS1_TEMP_VALID) != 0)
			break;
		DELAY(100);
	}
	if (timeout <= 0)
		device_printf(sc->dev, "Sensor %s timeouted\n", sensor->name);
	*temp = soctherm_convert_raw(val);
#ifdef DEBUG
	printf("%s: Raw: 0x%08X, temp: %d\n", __func__, val, *temp);
	printf(" Sensor: %s  cfg:0x%08X, 0x%08X, 0x%08X,"
	    " sts:0x%08X, 0x%08X, 0x%08X\n", sensor->name,
	    RD4(sc, sensor->sensor_base + TSENSOR_CONFIG0),
	    RD4(sc, sensor->sensor_base + TSENSOR_CONFIG1),
	    RD4(sc, sensor->sensor_base + TSENSOR_CONFIG2),
	    RD4(sc, sensor->sensor_base + TSENSOR_STATUS0),
	    RD4(sc, sensor->sensor_base + TSENSOR_STATUS1),
	    RD4(sc, sensor->sensor_base + TSENSOR_STATUS2)
	    );
#endif
	return (0);
}

static int
soctherm_get_temp(device_t dev, device_t cdev, uintptr_t id, int *val)
{
	struct soctherm_softc *sc;
	int i;

	sc = device_get_softc(dev);
	/* The direct sensor map starts at 0x100 */
	if (id >= 0x100) {
		id -= 0x100;
		if (id >= sc->soc->ntsensors)
			return (ERANGE);
		return(soctherm_read_temp(sc, sc->soc->tsensors + id, val));
	}
	/* Linux (DT) compatible thermal zones */
	for (i = 0; i < sc->soc->ntsensors; i++) {
		if (sc->soc->tsensors->id == id) {
			return(soctherm_read_temp(sc, sc->soc->tsensors + id,
			    val));
		}
	}
	return (ERANGE);
}

static int
soctherm_sysctl_temperature(SYSCTL_HANDLER_ARGS)
{
	struct soctherm_softc *sc;
	int val;
	int rv;
	int id;

	/* Write request */
	if (req->newptr != NULL)
		return (EINVAL);

	sc = arg1;
	id = arg2;

	if (id >= sc->soc->ntsensors)
		return (ERANGE);
	rv =  soctherm_read_temp(sc, sc->soc->tsensors + id, &val);
	if (rv != 0)
		return (rv);

	val = val / 100;
	val +=  2731;
	rv = sysctl_handle_int(oidp, &val, 0, req);
	return (rv);
}

static int
soctherm_init_sysctl(struct soctherm_softc *sc)
{
	int i;
	struct sysctl_oid *oid, *tmp;

	sysctl_ctx_init(&soctherm_sysctl_ctx);
	/* create node for hw.temp */
	oid = SYSCTL_ADD_NODE(&soctherm_sysctl_ctx,
	    SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, "temperature",
	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
	if (oid == NULL)
		return (ENXIO);

	/* Add sensors */
	for (i = sc->soc->ntsensors  - 1; i >= 0; i--) {
		tmp = SYSCTL_ADD_PROC(&soctherm_sysctl_ctx,
		    SYSCTL_CHILDREN(oid), OID_AUTO, sc->soc->tsensors[i].name,
		    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT, sc, i,
		    soctherm_sysctl_temperature, "IK", "SoC Temperature");
		if (tmp == NULL)
			return (ENXIO);
	}

	return (0);
}

static int
soctherm_probe(device_t dev)
{

	if (!ofw_bus_status_okay(dev))
		return (ENXIO);

	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
		return (ENXIO);

	device_set_desc(dev, "Tegra temperature sensors");
	return (BUS_PROBE_DEFAULT);
}

static int
soctherm_attach(device_t dev)
{
	struct soctherm_softc *sc;
	phandle_t node;
	int i, rid, rv;

	sc = device_get_softc(dev);
	sc->dev = dev;
	sc->soc = (struct soctherm_soc *)ofw_bus_search_compatible(dev,
	   compat_data)->ocd_data;
	node = ofw_bus_get_node(sc->dev);

	rid = 0;
	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
	    RF_ACTIVE);
	if (sc->mem_res == NULL) {
		device_printf(dev, "Cannot allocate memory resources\n");
		goto fail;
	}

	rid = 0;
	sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
	if (sc->irq_res == NULL) {
		device_printf(dev, "Cannot allocate IRQ resources\n");
		goto fail;
	}

/*
	if ((bus_setup_intr(dev, sc->irq_res, INTR_TYPE_MISC,
	    soctherm_intr, NULL, sc, &sc->irq_ih))) {
		device_printf(dev,
		    "WARNING: unable to register interrupt handler\n");
		goto fail;
	}
*/

	/* OWF resources */
	rv = hwreset_get_by_ofw_name(dev, 0, "soctherm", &sc->reset);
	if (rv != 0) {
		device_printf(dev, "Cannot get fuse reset\n");
		goto fail;
	}
	rv = clk_get_by_ofw_name(dev, 0, "tsensor", &sc->tsensor_clk);
	if (rv != 0) {
		device_printf(dev, "Cannot get 'tsensor' clock: %d\n", rv);
		goto fail;
	}
	rv = clk_get_by_ofw_name(dev, 0, "soctherm", &sc->soctherm_clk);
	if (rv != 0) {
		device_printf(dev, "Cannot get 'soctherm' clock: %d\n", rv);
		goto fail;
	}

	rv = hwreset_assert(sc->reset);
	if (rv != 0) {
		device_printf(dev, "Cannot assert reset\n");
		goto fail;
	}
	rv = clk_enable(sc->tsensor_clk);
	if (rv != 0) {
		device_printf(dev, "Cannot enable 'tsensor' clock: %d\n", rv);
		goto fail;
	}
	rv = clk_enable(sc->soctherm_clk);
	if (rv != 0) {
		device_printf(dev, "Cannot enable 'soctherm' clock: %d\n", rv);
		goto fail;
	}
	rv = hwreset_deassert(sc->reset);
	if (rv != 0) {
		device_printf(dev, "Cannot clear reset\n");
		goto fail;
	}

	sc->soc->shared_cal(sc);

	WR4(sc, TSENSOR_PDIV, sc->soc->tsensor_pdiv);
	WR4(sc, TSENSOR_HOTSPOT_OFF, sc->soc->tsensor_hotspot_off);

	for (i = 0; i < sc->soc->ntsensors; i++)
		soctherm_init_tsensor(sc, sc->soc->tsensors + i);

	rv = soctherm_init_sysctl(sc);
	if (rv != 0) {
		device_printf(sc->dev, "Cannot initialize sysctls\n");
		goto fail;
	}

	OF_device_register_xref(OF_xref_from_node(node), dev);
	return (bus_generic_attach(dev));

fail:
	if (sc->irq_ih != NULL)
		bus_teardown_intr(dev, sc->irq_res, sc->irq_ih);
	sysctl_ctx_free(&soctherm_sysctl_ctx);
	if (sc->tsensor_clk != NULL)
		clk_release(sc->tsensor_clk);
	if (sc->soctherm_clk != NULL)
		clk_release(sc->soctherm_clk);
	if (sc->reset != NULL)
		hwreset_release(sc->reset);
	if (sc->irq_res != NULL)
		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
	if (sc->mem_res != NULL)
		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);

	return (ENXIO);
}

static int
soctherm_detach(device_t dev)
{
	struct soctherm_softc *sc;
	sc = device_get_softc(dev);

	if (sc->irq_ih != NULL)
		bus_teardown_intr(dev, sc->irq_res, sc->irq_ih);
	sysctl_ctx_free(&soctherm_sysctl_ctx);
	if (sc->tsensor_clk != NULL)
		clk_release(sc->tsensor_clk);
	if (sc->soctherm_clk != NULL)
		clk_release(sc->soctherm_clk);
	if (sc->reset != NULL)
		hwreset_release(sc->reset);
	if (sc->irq_res != NULL)
		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
	if (sc->mem_res != NULL)
		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);

	return (ENXIO);
}

static device_method_t tegra_soctherm_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,			soctherm_probe),
	DEVMETHOD(device_attach,		soctherm_attach),
	DEVMETHOD(device_detach,		soctherm_detach),

	/* SOCTHERM interface */
	DEVMETHOD(tegra_soctherm_get_temperature, soctherm_get_temp),

	DEVMETHOD_END
};

static devclass_t tegra_soctherm_devclass;
static DEFINE_CLASS_0(soctherm, tegra_soctherm_driver, tegra_soctherm_methods,
    sizeof(struct soctherm_softc));
EARLY_DRIVER_MODULE(tegra_soctherm, simplebus, tegra_soctherm_driver,
    tegra_soctherm_devclass, NULL, NULL, 79);