xref: /netbsd-src/sys/arch/arm/amlogic/meson_thermal.c (revision 90313c06e62e910bf0d1bb24faa9d17dcefd0ab6)
1*90313c06Smsaitoh /* $NetBSD: meson_thermal.c,v 1.7 2024/02/07 04:20:26 msaitoh Exp $ */
28afae5d5Sryo 
38afae5d5Sryo /*
4*90313c06Smsaitoh  * Copyright (c) 2021 Ryo Shimizu
58afae5d5Sryo  * All rights reserved.
68afae5d5Sryo  *
78afae5d5Sryo  * Redistribution and use in source and binary forms, with or without
88afae5d5Sryo  * modification, are permitted provided that the following conditions
98afae5d5Sryo  * are met:
108afae5d5Sryo  * 1. Redistributions of source code must retain the above copyright
118afae5d5Sryo  *    notice, this list of conditions and the following disclaimer.
128afae5d5Sryo  * 2. Redistributions in binary form must reproduce the above copyright
138afae5d5Sryo  *    notice, this list of conditions and the following disclaimer in the
148afae5d5Sryo  *    documentation and/or other materials provided with the distribution.
158afae5d5Sryo  *
168afae5d5Sryo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
178afae5d5Sryo  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
188afae5d5Sryo  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198afae5d5Sryo  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
208afae5d5Sryo  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
218afae5d5Sryo  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
228afae5d5Sryo  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238afae5d5Sryo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
248afae5d5Sryo  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
258afae5d5Sryo  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
268afae5d5Sryo  * POSSIBILITY OF SUCH DAMAGE.
278afae5d5Sryo  */
288afae5d5Sryo 
298afae5d5Sryo #include <sys/cdefs.h>
30*90313c06Smsaitoh __KERNEL_RCSID(0, "$NetBSD: meson_thermal.c,v 1.7 2024/02/07 04:20:26 msaitoh Exp $");
318afae5d5Sryo 
328afae5d5Sryo #include <sys/param.h>
338afae5d5Sryo #include <sys/types.h>
348afae5d5Sryo #include <sys/bus.h>
358afae5d5Sryo #include <sys/device.h>
368afae5d5Sryo 
378afae5d5Sryo #include <dev/fdt/fdtvar.h>
388afae5d5Sryo #include <dev/sysmon/sysmonvar.h>
398afae5d5Sryo 
408afae5d5Sryo #define TS_CFG_REG1	0x01
418afae5d5Sryo #define  TS_CFG_REG1_ANA_EN_VCM		__BIT(10)
428afae5d5Sryo #define  TS_CFG_REG1_ANA_EN_VBG		__BIT(9)
438afae5d5Sryo #define  TS_CFG_REG1_FILTER_EN		__BIT(5)
448afae5d5Sryo #define  TS_CFG_REG1_DEM_EN		__BIT(3)
458afae5d5Sryo #define  TS_CFG_REG1_ANA_CH_SEL		__BITS(2,0)
468afae5d5Sryo #define TS_CFG_REG2	0x02
478afae5d5Sryo #define TS_CFG_REG3	0x03
488afae5d5Sryo #define TS_CFG_REG4	0x04
498afae5d5Sryo #define TS_CFG_REG5	0x05
508afae5d5Sryo #define TS_CFG_REG6	0x06
518afae5d5Sryo #define TS_CFG_REG7	0x07
528afae5d5Sryo #define TS_STAT0_REG	0x10
538afae5d5Sryo #define  TS_STAT0_FILTER_OUT		__BITS(15,0)
548afae5d5Sryo #define TS_STAT1_REG	0x11
558afae5d5Sryo #define TS_STAT2_REG	0x12
568afae5d5Sryo #define TS_STAT3_REG	0x13
578afae5d5Sryo #define TS_STAT4_REG	0x14
588afae5d5Sryo #define TS_STAT5_REG	0x15
598afae5d5Sryo #define TS_STAT6_REG	0x16
608afae5d5Sryo #define TS_STAT7_REG	0x17
618afae5d5Sryo #define TS_STAT8_REG	0x18
628afae5d5Sryo #define TS_STAT9_REG	0x19
638afae5d5Sryo 
648afae5d5Sryo #define THERMAL_READ_REG(sc, reg) \
658afae5d5Sryo 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg) * 4)
668afae5d5Sryo #define THERMAL_WRITE_REG(sc, reg, val) \
678afae5d5Sryo 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg) * 4, (val))
688afae5d5Sryo 
698afae5d5Sryo #define AOSECURE_READ(sc, reg) \
708afae5d5Sryo 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh_ao, (reg))
718afae5d5Sryo #define AOSECURE_WRITE(sc, reg, val) \
728afae5d5Sryo 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh_ao, (reg), (val))
738afae5d5Sryo 
748afae5d5Sryo 
758afae5d5Sryo struct meson_thermal_config {
768afae5d5Sryo 	const char *name;
778afae5d5Sryo 	bus_size_t aosec_reg;
788afae5d5Sryo };
798afae5d5Sryo 
808afae5d5Sryo static struct meson_thermal_config thermal_cpu_conf = {
818afae5d5Sryo 	.name = "CPU",
828afae5d5Sryo 	.aosec_reg = 0x128
838afae5d5Sryo };
848afae5d5Sryo 
858afae5d5Sryo static struct meson_thermal_config thermal_ddr_conf = {
868afae5d5Sryo 	.name = "DDR",
878afae5d5Sryo 	.aosec_reg = 0xf0
888afae5d5Sryo };
898afae5d5Sryo 
90646c0f59Sthorpej static const struct device_compatible_entry compat_data[] = {
91646c0f59Sthorpej 	{ .compat = "amlogic,g12a-cpu-thermal", .data = &thermal_cpu_conf },
92646c0f59Sthorpej 	{ .compat = "amlogic,g12a-ddr-thermal", .data = &thermal_ddr_conf },
932dcdd1cdSthorpej 	DEVICE_COMPAT_EOL
948afae5d5Sryo };
958afae5d5Sryo 
968afae5d5Sryo struct meson_thermal_softc {
978afae5d5Sryo 	device_t sc_dev;
988afae5d5Sryo 	bus_space_tag_t sc_bst;
998afae5d5Sryo 	bus_space_handle_t sc_bsh;
1008afae5d5Sryo 	bus_space_handle_t sc_bsh_ao;
101646c0f59Sthorpej 	const struct meson_thermal_config *sc_conf;
1028afae5d5Sryo 	int sc_phandle;
1038afae5d5Sryo 	int sc_ao_calib;
1048afae5d5Sryo 
1058afae5d5Sryo 	struct sysmon_envsys *sc_sme;
1068afae5d5Sryo 	envsys_data_t sc_sensor_temp;
1078afae5d5Sryo };
1088afae5d5Sryo 
1098afae5d5Sryo 
1108afae5d5Sryo static void
meson_thermal_init(struct meson_thermal_softc * sc)1118afae5d5Sryo meson_thermal_init(struct meson_thermal_softc *sc)
1128afae5d5Sryo {
1138afae5d5Sryo 	uint32_t val;
1148afae5d5Sryo 
1158afae5d5Sryo 	val = THERMAL_READ_REG(sc, TS_CFG_REG1);
1168afae5d5Sryo 	val |= TS_CFG_REG1_ANA_EN_VCM;
1178afae5d5Sryo 	val |= TS_CFG_REG1_ANA_EN_VBG;
1188afae5d5Sryo 	val |= TS_CFG_REG1_FILTER_EN;
1198afae5d5Sryo 	val |= TS_CFG_REG1_DEM_EN;
1208afae5d5Sryo 	val &= ~TS_CFG_REG1_ANA_CH_SEL;
1218afae5d5Sryo 	val |= __SHIFTIN(3, TS_CFG_REG1_ANA_CH_SEL);
1228afae5d5Sryo 	THERMAL_WRITE_REG(sc, TS_CFG_REG1, val);
1238afae5d5Sryo 
1248afae5d5Sryo 	/* read calibration value in ao-secure */
1258afae5d5Sryo #define TS_AO_CALIB_VERSION_MASK	__BITS(31,24)
1268afae5d5Sryo #define TS_AO_CALIB_SIGN_MASK		__BIT(15)
1278afae5d5Sryo #define TS_AO_CALIB_TEMP_MASK		__BITS(14,0)
1288afae5d5Sryo 	val = AOSECURE_READ(sc, sc->sc_conf->aosec_reg);
1298afae5d5Sryo 	if ((val & TS_AO_CALIB_VERSION_MASK) != 0) {
1308afae5d5Sryo 		sc->sc_ao_calib = (val & TS_AO_CALIB_TEMP_MASK);
1318afae5d5Sryo 		if ((val & TS_AO_CALIB_SIGN_MASK) != 0)
1328afae5d5Sryo 			sc->sc_ao_calib *= -1;
1338afae5d5Sryo 	} else {
1348afae5d5Sryo 		sc->sc_ao_calib = 0;
1358afae5d5Sryo 	}
1368afae5d5Sryo }
1378afae5d5Sryo 
1388afae5d5Sryo static int
meson_get_temperature(struct meson_thermal_softc * sc)1398afae5d5Sryo meson_get_temperature(struct meson_thermal_softc *sc)
1408afae5d5Sryo {
1418afae5d5Sryo 	int val, temp;
1428afae5d5Sryo 	int64_t factor, uptat;
1438afae5d5Sryo 
1448afae5d5Sryo 	val = THERMAL_READ_REG(sc, TS_STAT0_REG) & TS_STAT0_FILTER_OUT;
1458afae5d5Sryo 
1468afae5d5Sryo #define CALIB_A		9411
1478afae5d5Sryo #define CALIB_B		3159
1488afae5d5Sryo #define CALIB_m_1024	4342	/* 4.24 */
1498afae5d5Sryo #define CALIB_n_1024	3318	/* 3.24 */
1508afae5d5Sryo 
1518afae5d5Sryo 	factor = (val * CALIB_n_1024) / 1024;
1528afae5d5Sryo 	uptat = (val * CALIB_m_1024) / 1024;
1538afae5d5Sryo 
1548afae5d5Sryo 	uptat = (uptat * (1 << 16)) / ((1 << 16) + factor);
1558afae5d5Sryo 	temp = ((uptat + sc->sc_ao_calib) * CALIB_A);
1568afae5d5Sryo 	temp = (temp - (CALIB_B * (1 << 16))) * 100000LL / (1 << 16);
1578afae5d5Sryo 
1588afae5d5Sryo 	return temp;	/* microcelsius */
1598afae5d5Sryo }
1608afae5d5Sryo 
1618afae5d5Sryo static void
meson_thermal_refresh(struct sysmon_envsys * sme,envsys_data_t * edata)1628afae5d5Sryo meson_thermal_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
1638afae5d5Sryo {
1648afae5d5Sryo 	struct meson_thermal_softc *sc = sme->sme_cookie;
1658afae5d5Sryo 
1668afae5d5Sryo 	edata->value_cur = meson_get_temperature(sc) + 273150000;
1678afae5d5Sryo 	edata->state = ENVSYS_SVALID;
1688afae5d5Sryo }
1698afae5d5Sryo 
1708afae5d5Sryo static int
meson_thermal_match(device_t parent,cfdata_t cf,void * aux)1718afae5d5Sryo meson_thermal_match(device_t parent, cfdata_t cf, void *aux)
1728afae5d5Sryo {
1738afae5d5Sryo 	struct fdt_attach_args * const faa = aux;
1748afae5d5Sryo 
1756e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
1768afae5d5Sryo }
1778afae5d5Sryo 
1788afae5d5Sryo static void
meson_thermal_attach(device_t parent,device_t self,void * aux)1798afae5d5Sryo meson_thermal_attach(device_t parent, device_t self, void *aux)
1808afae5d5Sryo {
1818afae5d5Sryo 	struct meson_thermal_softc * const sc = device_private(self);
1828afae5d5Sryo 	struct fdt_attach_args * const faa = aux;
1838afae5d5Sryo 	bus_addr_t addr;
1848afae5d5Sryo 	bus_size_t size, aosize;
1858afae5d5Sryo 	int phandle, phandle_aosec;
1868afae5d5Sryo 
1878afae5d5Sryo 	sc->sc_dev = self;
1888afae5d5Sryo 	sc->sc_bst = faa->faa_bst;
1898afae5d5Sryo 	sc->sc_phandle = phandle = faa->faa_phandle;
1906e54367aSthorpej 	sc->sc_conf = of_compatible_lookup(phandle, compat_data)->data;
1918afae5d5Sryo 
1928afae5d5Sryo 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
1938afae5d5Sryo 		aprint_error(": couldn't get registers\n");
1948afae5d5Sryo 		goto attach_failure0;
1958afae5d5Sryo 	}
1968afae5d5Sryo 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
1978afae5d5Sryo 		aprint_error(": couldn't map registers\n");
1988afae5d5Sryo 		goto attach_failure0;
1998afae5d5Sryo 	}
2008afae5d5Sryo 
2018afae5d5Sryo 	phandle_aosec = fdtbus_get_phandle(phandle, "amlogic,ao-secure");
2028afae5d5Sryo 	if (fdtbus_get_reg(phandle_aosec, 0, &addr, &aosize) != 0) {
2038afae5d5Sryo 		aprint_error(": couldn't get registers\n");
2048afae5d5Sryo 		goto attach_failure1;
2058afae5d5Sryo 	}
2068afae5d5Sryo 	if (bus_space_map(sc->sc_bst, addr, aosize, 0, &sc->sc_bsh_ao) != 0) {
2078afae5d5Sryo 		aprint_error(": couldn't map registers\n");
2088afae5d5Sryo 		goto attach_failure1;
2098afae5d5Sryo 	}
2108afae5d5Sryo 
2118afae5d5Sryo 	if (fdtbus_clock_enable_index(phandle, 0, true) != 0) {
2128afae5d5Sryo 		aprint_error(": couldn't enable clock\n");
2138afae5d5Sryo 		goto attach_failure2;
2148afae5d5Sryo 	}
2158afae5d5Sryo 
2168afae5d5Sryo 	meson_thermal_init(sc);
2178afae5d5Sryo 
2188afae5d5Sryo 	aprint_naive("\n");
2198afae5d5Sryo 	aprint_normal(": %s TEMP Sensor\n", sc->sc_conf->name);
2208afae5d5Sryo 
2218afae5d5Sryo 	sc->sc_sme = sysmon_envsys_create();
2228afae5d5Sryo 	sc->sc_sme->sme_name = device_xname(self);
2238afae5d5Sryo 	sc->sc_sme->sme_cookie = sc;
2248afae5d5Sryo 	sc->sc_sme->sme_flags = 0;
2258afae5d5Sryo 	sc->sc_sme->sme_events_timeout = 1;
2268afae5d5Sryo 	sc->sc_sme->sme_refresh = meson_thermal_refresh;
2278afae5d5Sryo 	sc->sc_sensor_temp.units = ENVSYS_STEMP;
2288afae5d5Sryo 	sc->sc_sensor_temp.state = ENVSYS_SINVALID;
229733b7a15Smacallan 	snprintf(sc->sc_sensor_temp.desc, ENVSYS_DESCLEN,
230733b7a15Smacallan 	    "%s", sc->sc_conf->name);
2318afae5d5Sryo 
2328afae5d5Sryo 	sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor_temp);
2338afae5d5Sryo 	sysmon_envsys_register(sc->sc_sme);
2348afae5d5Sryo 
2358afae5d5Sryo 	meson_thermal_refresh(sc->sc_sme, &sc->sc_sensor_temp);
2368afae5d5Sryo 	return;
2378afae5d5Sryo 
2388afae5d5Sryo  attach_failure2:
2398afae5d5Sryo 	bus_space_unmap(sc->sc_bst, sc->sc_bsh_ao, aosize);
2408afae5d5Sryo  attach_failure1:
2418afae5d5Sryo 	bus_space_unmap(sc->sc_bst, sc->sc_bsh, size);
2428afae5d5Sryo  attach_failure0:
2438afae5d5Sryo 	return;
2448afae5d5Sryo }
2458afae5d5Sryo 
2468afae5d5Sryo CFATTACH_DECL_NEW(meson_thermal, sizeof(struct meson_thermal_softc),
2478afae5d5Sryo     meson_thermal_match, meson_thermal_attach, NULL, NULL);
248