Home | History | Annotate | Line # | Download | only in nvidia
      1  1.13   thorpej /* $NetBSD: tegra_soctherm.c,v 1.13 2021/01/27 03:10:19 thorpej Exp $ */
      2   1.1  jmcneill 
      3   1.1  jmcneill /*-
      4   1.1  jmcneill  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      5   1.1  jmcneill  * All rights reserved.
      6   1.1  jmcneill  *
      7   1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8   1.1  jmcneill  * modification, are permitted provided that the following conditions
      9   1.1  jmcneill  * are met:
     10   1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12   1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15   1.1  jmcneill  *
     16   1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17   1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18   1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19   1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20   1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21   1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22   1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23   1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24   1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.1  jmcneill  * SUCH DAMAGE.
     27   1.1  jmcneill  */
     28   1.1  jmcneill 
     29   1.1  jmcneill #include <sys/cdefs.h>
     30  1.13   thorpej __KERNEL_RCSID(0, "$NetBSD: tegra_soctherm.c,v 1.13 2021/01/27 03:10:19 thorpej Exp $");
     31   1.1  jmcneill 
     32   1.1  jmcneill #include <sys/param.h>
     33   1.1  jmcneill #include <sys/bus.h>
     34   1.1  jmcneill #include <sys/device.h>
     35   1.1  jmcneill #include <sys/intr.h>
     36   1.1  jmcneill #include <sys/systm.h>
     37   1.1  jmcneill #include <sys/kernel.h>
     38   1.1  jmcneill #include <sys/kmem.h>
     39   1.1  jmcneill 
     40   1.1  jmcneill #include <dev/sysmon/sysmonvar.h>
     41   1.1  jmcneill 
     42   1.1  jmcneill #include <arm/nvidia/tegra_reg.h>
     43   1.1  jmcneill #include <arm/nvidia/tegra_socthermreg.h>
     44   1.1  jmcneill #include <arm/nvidia/tegra_var.h>
     45   1.1  jmcneill 
     46   1.2  jmcneill #include <dev/fdt/fdtvar.h>
     47   1.2  jmcneill 
     48   1.1  jmcneill #define FUSE_TSENSOR_CALIB_CP_TS_BASE	__BITS(12,0)
     49   1.1  jmcneill #define FUSE_TSENSOR_CALIB_FT_TS_BASE	__BITS(25,13)
     50   1.1  jmcneill 
     51   1.1  jmcneill #define FUSE_TSENSOR8_CALIB_REG		0x180
     52   1.1  jmcneill #define FUSE_TSENSOR8_CALIB_CP_TS_BASE	__BITS(9,0)
     53   1.1  jmcneill #define FUSE_TSENSOR8_CALIB_FT_TS_BASE	__BITS(20,10)
     54   1.1  jmcneill 
     55   1.1  jmcneill #define FUSE_SPARE_REALIGNMENT_REG	0x1fc
     56   1.1  jmcneill #define FUSE_SPARE_REALIGNMENT_CP	__BITS(5,0)
     57   1.1  jmcneill #define FUSE_SPARE_REALIGNMENT_FT	__BITS(25,21)
     58   1.1  jmcneill 
     59   1.1  jmcneill static int	tegra_soctherm_match(device_t, cfdata_t, void *);
     60   1.1  jmcneill static void	tegra_soctherm_attach(device_t, device_t, void *);
     61   1.1  jmcneill 
     62   1.1  jmcneill struct tegra_soctherm_config {
     63   1.1  jmcneill 	uint32_t init_pdiv;
     64   1.1  jmcneill 	uint32_t init_hotspot_off;
     65   1.1  jmcneill 	uint32_t nominal_calib_ft;
     66   1.1  jmcneill 	uint32_t nominal_calib_cp;
     67   1.1  jmcneill 	uint32_t tall;
     68   1.1  jmcneill 	uint32_t tsample;
     69   1.1  jmcneill 	uint32_t tiddq_en;
     70   1.1  jmcneill 	uint32_t ten_count;
     71   1.1  jmcneill 	uint32_t pdiv;
     72   1.1  jmcneill 	uint32_t tsample_ate;
     73   1.1  jmcneill 	uint32_t pdiv_ate;
     74   1.1  jmcneill };
     75   1.1  jmcneill 
     76   1.1  jmcneill static const struct tegra_soctherm_config tegra124_soctherm_config = {
     77   1.1  jmcneill 	.init_pdiv = 0x8888,
     78   1.1  jmcneill 	.init_hotspot_off = 0x60600,
     79   1.1  jmcneill 	.nominal_calib_ft = 105,
     80   1.1  jmcneill 	.nominal_calib_cp = 25,
     81   1.1  jmcneill 	.tall = 16300,
     82   1.1  jmcneill 	.tsample = 120,
     83   1.1  jmcneill 	.tiddq_en = 1,
     84   1.1  jmcneill 	.ten_count = 1,
     85   1.1  jmcneill 	.pdiv = 8,
     86   1.1  jmcneill 	.tsample_ate = 480,
     87   1.1  jmcneill 	.pdiv_ate = 8
     88   1.1  jmcneill };
     89   1.1  jmcneill 
     90   1.1  jmcneill struct tegra_soctherm_sensor {
     91   1.1  jmcneill 	envsys_data_t		s_data;
     92   1.1  jmcneill 	u_int			s_base;
     93   1.1  jmcneill 	u_int			s_fuse;
     94   1.1  jmcneill 	int			s_fuse_corr_alpha;
     95   1.1  jmcneill 	int			s_fuse_corr_beta;
     96   1.1  jmcneill 	int16_t			s_therm_a;
     97   1.1  jmcneill 	int16_t			s_therm_b;
     98   1.1  jmcneill };
     99   1.1  jmcneill 
    100   1.1  jmcneill static const struct tegra_soctherm_sensor tegra_soctherm_sensors[] = {
    101   1.1  jmcneill 	{ .s_data = { .desc = "CPU0" }, .s_base = 0x0c0, .s_fuse = 0x098,
    102   1.1  jmcneill 	  .s_fuse_corr_alpha = 1135400, .s_fuse_corr_beta = -6266900 },
    103   1.1  jmcneill 	{ .s_data = { .desc = "CPU1" }, .s_base = 0x0e0, .s_fuse = 0x084,
    104   1.1  jmcneill 	  .s_fuse_corr_alpha = 1122220, .s_fuse_corr_beta = -5700700 },
    105   1.1  jmcneill 	{ .s_data = { .desc = "CPU2" }, .s_base = 0x100, .s_fuse = 0x088,
    106   1.1  jmcneill 	  .s_fuse_corr_alpha = 1127000, .s_fuse_corr_beta = -6768200 },
    107   1.1  jmcneill 	{ .s_data = { .desc = "CPU3" }, .s_base = 0x120, .s_fuse = 0x12c,
    108   1.1  jmcneill 	  .s_fuse_corr_alpha = 1110900, .s_fuse_corr_beta = -6232000 },
    109   1.1  jmcneill 	{ .s_data = { .desc = "MEM0" }, .s_base = 0x140, .s_fuse = 0x158,
    110   1.1  jmcneill 	  .s_fuse_corr_alpha = 1122300, .s_fuse_corr_beta = -5936400 },
    111   1.1  jmcneill 	{ .s_data = { .desc = "MEM1" }, .s_base = 0x160, .s_fuse = 0x15c,
    112   1.1  jmcneill 	  .s_fuse_corr_alpha = 1145700, .s_fuse_corr_beta = -7124600 },
    113   1.1  jmcneill 	{ .s_data = { .desc = "GPU" },  .s_base = 0x180, .s_fuse = 0x154,
    114   1.1  jmcneill 	  .s_fuse_corr_alpha = 1120100, .s_fuse_corr_beta = -6000500 },
    115   1.1  jmcneill 	{ .s_data = { .desc = "PLLX" }, .s_base = 0x1a0, .s_fuse = 0x160,
    116   1.1  jmcneill 	  .s_fuse_corr_alpha = 1106500, .s_fuse_corr_beta = -6729300 },
    117   1.1  jmcneill };
    118   1.1  jmcneill 
    119   1.1  jmcneill struct tegra_soctherm_softc {
    120   1.1  jmcneill 	device_t		sc_dev;
    121   1.1  jmcneill 	bus_space_tag_t		sc_bst;
    122   1.1  jmcneill 	bus_space_handle_t	sc_bsh;
    123   1.3  jmcneill 	struct clk		*sc_clk_tsensor;
    124   1.3  jmcneill 	struct clk		*sc_clk_soctherm;
    125   1.3  jmcneill 	struct fdtbus_reset	*sc_rst_soctherm;
    126   1.1  jmcneill 
    127   1.1  jmcneill 	struct sysmon_envsys	*sc_sme;
    128   1.1  jmcneill 	struct tegra_soctherm_sensor *sc_sensors;
    129   1.1  jmcneill 	const struct tegra_soctherm_config *sc_config;
    130   1.1  jmcneill 
    131   1.1  jmcneill 	uint32_t		sc_base_cp;
    132   1.1  jmcneill 	uint32_t		sc_base_ft;
    133   1.1  jmcneill 	int32_t			sc_actual_temp_cp;
    134   1.1  jmcneill 	int32_t			sc_actual_temp_ft;
    135   1.1  jmcneill };
    136   1.1  jmcneill 
    137   1.3  jmcneill static int	tegra_soctherm_init_clocks(struct tegra_soctherm_softc *);
    138   1.4  jmcneill static void	tegra_soctherm_init_sensors(device_t);
    139   1.1  jmcneill static void	tegra_soctherm_init_sensor(struct tegra_soctherm_softc *,
    140   1.1  jmcneill 		    struct tegra_soctherm_sensor *);
    141   1.1  jmcneill static void	tegra_soctherm_refresh(struct sysmon_envsys *, envsys_data_t *);
    142   1.1  jmcneill static int	tegra_soctherm_decodeint(uint32_t, uint32_t);
    143   1.1  jmcneill static int64_t	tegra_soctherm_divide(int64_t, int64_t);
    144   1.1  jmcneill 
    145   1.1  jmcneill CFATTACH_DECL_NEW(tegra_soctherm, sizeof(struct tegra_soctherm_softc),
    146   1.1  jmcneill 	tegra_soctherm_match, tegra_soctherm_attach, NULL, NULL);
    147   1.1  jmcneill 
    148   1.1  jmcneill #define SOCTHERM_READ(sc, reg)			\
    149   1.1  jmcneill     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    150   1.1  jmcneill #define SOCTHERM_WRITE(sc, reg, val)		\
    151   1.1  jmcneill     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    152   1.1  jmcneill #define SOCTHERM_SET_CLEAR(sc, reg, set, clr)	\
    153   1.1  jmcneill     tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (set), (clr))
    154   1.1  jmcneill 
    155   1.1  jmcneill #define SENSOR_READ(sc, s, reg)			\
    156   1.1  jmcneill     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (s)->s_base + (reg))
    157   1.1  jmcneill #define SENSOR_WRITE(sc, s, reg, val)		\
    158   1.1  jmcneill     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (s)->s_base + (reg), (val))
    159   1.1  jmcneill #define SENSOR_SET_CLEAR(sc, s, reg, set, clr)	\
    160   1.1  jmcneill     tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (s)->s_base + (reg), (set), (clr))
    161   1.1  jmcneill 
    162  1.10   thorpej static const struct device_compatible_entry compat_data[] = {
    163  1.10   thorpej 	{ .compat = "nvidia,tegra124-soctherm",
    164  1.10   thorpej 	  .data = &tegra124_soctherm_config },
    165  1.10   thorpej 
    166  1.12   thorpej 	DEVICE_COMPAT_EOL
    167   1.5  jmcneill };
    168   1.5  jmcneill 
    169   1.1  jmcneill static int
    170   1.1  jmcneill tegra_soctherm_match(device_t parent, cfdata_t cf, void *aux)
    171   1.1  jmcneill {
    172   1.2  jmcneill 	struct fdt_attach_args * const faa = aux;
    173   1.2  jmcneill 
    174  1.13   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    175   1.1  jmcneill }
    176   1.1  jmcneill 
    177   1.1  jmcneill static void
    178   1.1  jmcneill tegra_soctherm_attach(device_t parent, device_t self, void *aux)
    179   1.1  jmcneill {
    180   1.1  jmcneill 	struct tegra_soctherm_softc * const sc = device_private(self);
    181   1.2  jmcneill 	struct fdt_attach_args * const faa = aux;
    182   1.5  jmcneill 	const int phandle = faa->faa_phandle;
    183   1.2  jmcneill 	bus_addr_t addr;
    184   1.2  jmcneill 	bus_size_t size;
    185   1.2  jmcneill 	int error;
    186   1.2  jmcneill 
    187   1.5  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    188   1.2  jmcneill 		aprint_error(": couldn't get registers\n");
    189   1.2  jmcneill 		return;
    190   1.2  jmcneill 	}
    191   1.5  jmcneill 	sc->sc_clk_tsensor = fdtbus_clock_get(phandle, "tsensor");
    192   1.3  jmcneill 	if (sc->sc_clk_tsensor == NULL) {
    193   1.3  jmcneill 		aprint_error(": couldn't get clock tsensor\n");
    194   1.3  jmcneill 		return;
    195   1.3  jmcneill 	}
    196   1.5  jmcneill 	sc->sc_clk_soctherm = fdtbus_clock_get(phandle, "soctherm");
    197   1.3  jmcneill 	if (sc->sc_clk_soctherm == NULL) {
    198   1.3  jmcneill 		aprint_error(": couldn't get clock soctherm\n");
    199   1.3  jmcneill 		return;
    200   1.3  jmcneill 	}
    201   1.5  jmcneill 	sc->sc_rst_soctherm = fdtbus_reset_get(phandle, "soctherm");
    202   1.3  jmcneill 	if (sc->sc_rst_soctherm == NULL) {
    203   1.3  jmcneill 		aprint_error(": couldn't get reset soctherm\n");
    204   1.3  jmcneill 		return;
    205   1.3  jmcneill 	}
    206   1.1  jmcneill 
    207   1.1  jmcneill 	sc->sc_dev = self;
    208   1.2  jmcneill 	sc->sc_bst = faa->faa_bst;
    209   1.2  jmcneill 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    210   1.2  jmcneill 	if (error) {
    211   1.9     skrll 		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr, error);
    212   1.2  jmcneill 		return;
    213   1.2  jmcneill 	}
    214   1.1  jmcneill 
    215   1.1  jmcneill 	aprint_naive("\n");
    216   1.1  jmcneill 	aprint_normal(": SOC_THERM\n");
    217   1.1  jmcneill 
    218  1.13   thorpej 	sc->sc_config = of_compatible_lookup(phandle, compat_data)->data;
    219   1.1  jmcneill 	if (sc->sc_config == NULL) {
    220   1.5  jmcneill 		aprint_error_dev(self, "unsupported SoC\n");
    221   1.1  jmcneill 		return;
    222   1.1  jmcneill 	}
    223   1.1  jmcneill 
    224   1.3  jmcneill 	if (tegra_soctherm_init_clocks(sc) != 0)
    225   1.3  jmcneill 		return;
    226   1.1  jmcneill 
    227   1.4  jmcneill 	config_defer(self, tegra_soctherm_init_sensors);
    228   1.1  jmcneill }
    229   1.1  jmcneill 
    230   1.3  jmcneill static int
    231   1.3  jmcneill tegra_soctherm_init_clocks(struct tegra_soctherm_softc *sc)
    232   1.3  jmcneill {
    233   1.3  jmcneill 	int error;
    234   1.3  jmcneill 
    235   1.3  jmcneill 	fdtbus_reset_assert(sc->sc_rst_soctherm);
    236   1.3  jmcneill 
    237   1.3  jmcneill 	error = clk_set_rate(sc->sc_clk_soctherm, 51000000);
    238   1.3  jmcneill 	if (error) {
    239   1.3  jmcneill 		aprint_error_dev(sc->sc_dev,
    240   1.3  jmcneill 		    "couldn't set soctherm rate: %d\n", error);
    241   1.3  jmcneill 		return error;
    242   1.3  jmcneill 	}
    243   1.3  jmcneill 
    244   1.3  jmcneill 	error = clk_set_rate(sc->sc_clk_tsensor, 400000);
    245   1.3  jmcneill 	if (error) {
    246   1.3  jmcneill 		aprint_error_dev(sc->sc_dev,
    247   1.3  jmcneill 		    "couldn't set tsensor rate: %d\n", error);
    248   1.3  jmcneill 		return error;
    249   1.3  jmcneill 	}
    250   1.3  jmcneill 
    251   1.3  jmcneill 	error = clk_enable(sc->sc_clk_tsensor);
    252   1.3  jmcneill 	if (error) {
    253   1.3  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't enable tsensor: %d\n",
    254   1.3  jmcneill 		    error);
    255   1.3  jmcneill 		return error;
    256   1.3  jmcneill 	}
    257   1.3  jmcneill 
    258   1.3  jmcneill 	error = clk_enable(sc->sc_clk_soctherm);
    259   1.3  jmcneill 	if (error) {
    260   1.3  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't enable soctherm: %d\n",
    261   1.3  jmcneill 		    error);
    262   1.3  jmcneill 		return error;
    263   1.3  jmcneill 	}
    264   1.3  jmcneill 
    265   1.3  jmcneill 	fdtbus_reset_deassert(sc->sc_rst_soctherm);
    266   1.3  jmcneill 
    267   1.3  jmcneill 	return 0;
    268   1.3  jmcneill }
    269   1.3  jmcneill 
    270   1.1  jmcneill static void
    271   1.4  jmcneill tegra_soctherm_init_sensors(device_t dev)
    272   1.1  jmcneill {
    273   1.4  jmcneill 	struct tegra_soctherm_softc * const sc = device_private(dev);
    274   1.1  jmcneill 	const struct tegra_soctherm_config *config = sc->sc_config;
    275   1.1  jmcneill 	const u_int nsensors = __arraycount(tegra_soctherm_sensors);
    276   1.1  jmcneill 	const size_t len = sizeof(*sc->sc_sensors) * nsensors;
    277   1.1  jmcneill 	uint32_t val;
    278   1.1  jmcneill 	u_int n;
    279   1.1  jmcneill 
    280   1.1  jmcneill 	val = tegra_fuse_read(FUSE_TSENSOR8_CALIB_REG);
    281   1.1  jmcneill 	sc->sc_base_cp = __SHIFTOUT(val, FUSE_TSENSOR8_CALIB_CP_TS_BASE);
    282   1.1  jmcneill 	sc->sc_base_ft = __SHIFTOUT(val, FUSE_TSENSOR8_CALIB_FT_TS_BASE);
    283   1.1  jmcneill 	val = tegra_fuse_read(FUSE_SPARE_REALIGNMENT_REG);
    284   1.1  jmcneill 	const int calib_cp = tegra_soctherm_decodeint(val,
    285   1.1  jmcneill 	    FUSE_SPARE_REALIGNMENT_CP);
    286   1.1  jmcneill 	const int calib_ft = tegra_soctherm_decodeint(val,
    287   1.1  jmcneill 	    FUSE_SPARE_REALIGNMENT_FT);
    288   1.1  jmcneill 	sc->sc_actual_temp_cp = 2 * config->nominal_calib_cp + calib_cp;
    289   1.1  jmcneill 	sc->sc_actual_temp_ft = 2 * config->nominal_calib_ft + calib_ft;
    290   1.1  jmcneill 
    291   1.1  jmcneill 	sc->sc_sme = sysmon_envsys_create();
    292   1.1  jmcneill 	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
    293   1.1  jmcneill 	sc->sc_sme->sme_cookie = sc;
    294   1.1  jmcneill 	sc->sc_sme->sme_refresh = tegra_soctherm_refresh;
    295   1.1  jmcneill 
    296   1.1  jmcneill 	sc->sc_sensors = kmem_zalloc(len, KM_SLEEP);
    297   1.1  jmcneill 	for (n = 0; n < nsensors; n++) {
    298   1.1  jmcneill 		sc->sc_sensors[n] = tegra_soctherm_sensors[n];
    299   1.1  jmcneill 		tegra_soctherm_init_sensor(sc, &sc->sc_sensors[n]);
    300   1.1  jmcneill 	}
    301   1.1  jmcneill 
    302   1.1  jmcneill 	SOCTHERM_WRITE(sc, SOC_THERM_TSENSOR_PDIV_REG, config->init_pdiv);
    303   1.1  jmcneill 	SOCTHERM_WRITE(sc, SOC_THERM_TSENSOR_HOTSPOT_OFF_REG,
    304   1.1  jmcneill 	    config->init_hotspot_off);
    305   1.1  jmcneill 
    306   1.1  jmcneill 	sysmon_envsys_register(sc->sc_sme);
    307   1.1  jmcneill }
    308   1.1  jmcneill 
    309   1.1  jmcneill static void
    310   1.1  jmcneill tegra_soctherm_init_sensor(struct tegra_soctherm_softc *sc,
    311   1.1  jmcneill     struct tegra_soctherm_sensor *s)
    312   1.1  jmcneill {
    313   1.1  jmcneill 	const struct tegra_soctherm_config *config = sc->sc_config;
    314   1.1  jmcneill 	int64_t temp_a, temp_b, tmp;
    315   1.1  jmcneill 	uint32_t val;
    316   1.1  jmcneill 
    317   1.1  jmcneill 	val = tegra_fuse_read(s->s_fuse);
    318   1.1  jmcneill 	const int calib_cp = tegra_soctherm_decodeint(val,
    319   1.1  jmcneill 	    FUSE_TSENSOR_CALIB_CP_TS_BASE);
    320   1.1  jmcneill 	const int calib_ft = tegra_soctherm_decodeint(val,
    321   1.1  jmcneill 	    FUSE_TSENSOR_CALIB_FT_TS_BASE);
    322   1.1  jmcneill 	const int actual_cp = sc->sc_base_cp * 64 + calib_cp;
    323   1.1  jmcneill 	const int actual_ft = sc->sc_base_ft * 32 + calib_ft;
    324   1.1  jmcneill 
    325   1.1  jmcneill 	const int64_t d_sensor = actual_ft - actual_cp;
    326   1.1  jmcneill 	const int64_t d_temp = sc->sc_actual_temp_ft - sc->sc_actual_temp_cp;
    327   1.1  jmcneill 	const int mult = config->pdiv * config->tsample_ate;
    328   1.1  jmcneill 	const int div = config->tsample * config->pdiv_ate;
    329   1.1  jmcneill 
    330   1.1  jmcneill 	temp_a = tegra_soctherm_divide(d_temp * 0x2000 * mult,
    331   1.1  jmcneill 	    d_sensor * div);
    332   1.1  jmcneill 	tmp = (int64_t)actual_ft * sc->sc_actual_temp_cp -
    333   1.1  jmcneill 	      (int64_t)actual_cp * sc->sc_actual_temp_ft;
    334   1.1  jmcneill 	temp_b = tegra_soctherm_divide(tmp, d_sensor);
    335   1.1  jmcneill 	temp_a = tegra_soctherm_divide(
    336   1.1  jmcneill 	    temp_a * s->s_fuse_corr_alpha, 1000000);
    337   1.1  jmcneill 	temp_b = (uint16_t)tegra_soctherm_divide(
    338   1.1  jmcneill 	    temp_b * s->s_fuse_corr_alpha + s->s_fuse_corr_beta, 1000000);
    339   1.1  jmcneill 
    340   1.1  jmcneill 	s->s_therm_a = (int16_t)temp_a;
    341   1.1  jmcneill 	s->s_therm_b = (int16_t)temp_b;
    342   1.1  jmcneill 
    343   1.1  jmcneill 	SENSOR_SET_CLEAR(sc, s, SOC_THERM_TSENSOR_CONFIG0_OFFSET,
    344   1.1  jmcneill 	    SOC_THERM_TSENSOR_CONFIG0_STATUS_CLR |
    345   1.1  jmcneill 	    SOC_THERM_TSENSOR_CONFIG0_STOP, 0);
    346   1.1  jmcneill 	SENSOR_WRITE(sc, s, SOC_THERM_TSENSOR_CONFIG0_OFFSET,
    347   1.1  jmcneill 	    __SHIFTIN(config->tall, SOC_THERM_TSENSOR_CONFIG0_TALL) |
    348   1.1  jmcneill 	    SOC_THERM_TSENSOR_CONFIG0_STOP);
    349   1.1  jmcneill 
    350   1.1  jmcneill 	SENSOR_WRITE(sc, s, SOC_THERM_TSENSOR_CONFIG1_OFFSET,
    351   1.1  jmcneill 	    __SHIFTIN(config->tsample - 1, SOC_THERM_TSENSOR_CONFIG1_TSAMPLE) |
    352   1.1  jmcneill 	    __SHIFTIN(config->tiddq_en, SOC_THERM_TSENSOR_CONFIG1_TIDDQ_EN) |
    353   1.1  jmcneill 	    __SHIFTIN(config->ten_count, SOC_THERM_TSENSOR_CONFIG1_TEN_COUNT) |
    354   1.1  jmcneill 	    SOC_THERM_TSENSOR_CONFIG1_TEMP_ENABLE);
    355   1.1  jmcneill 
    356   1.1  jmcneill 	SENSOR_WRITE(sc, s, SOC_THERM_TSENSOR_CONFIG2_OFFSET,
    357   1.1  jmcneill 	    __SHIFTIN((uint16_t)s->s_therm_a,
    358   1.1  jmcneill 		      SOC_THERM_TSENSOR_CONFIG2_THERM_A) |
    359   1.1  jmcneill 	    __SHIFTIN((uint16_t)s->s_therm_b,
    360   1.1  jmcneill 		      SOC_THERM_TSENSOR_CONFIG2_THERM_B));
    361   1.1  jmcneill 
    362   1.1  jmcneill 	SENSOR_SET_CLEAR(sc, s, SOC_THERM_TSENSOR_CONFIG0_OFFSET,
    363   1.1  jmcneill 	    0, SOC_THERM_TSENSOR_CONFIG0_STOP);
    364   1.1  jmcneill 
    365   1.1  jmcneill 	s->s_data.units = ENVSYS_STEMP;
    366   1.1  jmcneill 	s->s_data.state = ENVSYS_SINVALID;
    367   1.1  jmcneill 	sysmon_envsys_sensor_attach(sc->sc_sme, &s->s_data);
    368   1.1  jmcneill }
    369   1.1  jmcneill 
    370   1.1  jmcneill static void
    371   1.1  jmcneill tegra_soctherm_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
    372   1.1  jmcneill {
    373   1.1  jmcneill 	struct tegra_soctherm_softc * const sc = sme->sme_cookie;
    374   1.1  jmcneill 	struct tegra_soctherm_sensor *s = (struct tegra_soctherm_sensor *)edata;
    375   1.1  jmcneill 	uint32_t status;
    376   1.1  jmcneill 
    377   1.1  jmcneill 	status = SENSOR_READ(sc, s, SOC_THERM_TSENSOR_STATUS1_OFFSET);
    378   1.1  jmcneill 	if (status & SOC_THERM_TSENSOR_STATUS1_TEMP_VALID) {
    379   1.1  jmcneill 		const u_int temp = __SHIFTOUT(status,
    380   1.1  jmcneill 		    SOC_THERM_TSENSOR_STATUS1_TEMP);
    381   1.1  jmcneill 		int64_t val = ((temp >> 8) & 0xff) * 1000000;
    382   1.1  jmcneill 		if (temp & 0x80)
    383   1.1  jmcneill 			val += 500000;
    384   1.1  jmcneill 		if (temp & 0x02)
    385   1.1  jmcneill 			val = -val;
    386   1.1  jmcneill 		edata->value_cur = val + 273150000;
    387   1.1  jmcneill 		edata->state = ENVSYS_SVALID;
    388   1.1  jmcneill 	} else {
    389   1.1  jmcneill 		edata->state = ENVSYS_SINVALID;
    390   1.1  jmcneill 	}
    391   1.1  jmcneill }
    392   1.1  jmcneill 
    393   1.1  jmcneill static int
    394   1.1  jmcneill tegra_soctherm_decodeint(uint32_t val, uint32_t bitmask)
    395   1.1  jmcneill {
    396   1.1  jmcneill 	const uint32_t v = __SHIFTOUT(val, bitmask);
    397   1.1  jmcneill 	const int bits = popcount32(bitmask);
    398   1.1  jmcneill 	int ret = v << (32 - bits);
    399   1.1  jmcneill 	return ret >> (32 - bits);
    400   1.1  jmcneill }
    401   1.1  jmcneill 
    402   1.1  jmcneill static int64_t
    403   1.1  jmcneill tegra_soctherm_divide(int64_t num, int64_t denom)
    404   1.1  jmcneill {
    405   1.1  jmcneill 	int64_t ret = ((num << 16) * 2 + 1) / (2 * denom);
    406   1.1  jmcneill 	return ret >> 16;
    407   1.1  jmcneill }
    408