meson_thermal.c revision 1.3 1 1.3 thorpej /* $NetBSD: meson_thermal.c,v 1.3 2021/01/18 02:35:48 thorpej Exp $ */
2 1.1 ryo
3 1.1 ryo /*
4 1.1 ryo * Copyright (c) 2021 Ryo Shimizu <ryo (at) nerv.org>
5 1.1 ryo * All rights reserved.
6 1.1 ryo *
7 1.1 ryo * Redistribution and use in source and binary forms, with or without
8 1.1 ryo * modification, are permitted provided that the following conditions
9 1.1 ryo * are met:
10 1.1 ryo * 1. Redistributions of source code must retain the above copyright
11 1.1 ryo * notice, this list of conditions and the following disclaimer.
12 1.1 ryo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 ryo * notice, this list of conditions and the following disclaimer in the
14 1.1 ryo * documentation and/or other materials provided with the distribution.
15 1.1 ryo *
16 1.1 ryo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 1.1 ryo * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 1.1 ryo * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 ryo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 1.1 ryo * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 1.1 ryo * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 1.1 ryo * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 ryo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 1.1 ryo * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 1.1 ryo * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 ryo * POSSIBILITY OF SUCH DAMAGE.
27 1.1 ryo */
28 1.1 ryo
29 1.1 ryo #include <sys/cdefs.h>
30 1.3 thorpej __KERNEL_RCSID(0, "$NetBSD: meson_thermal.c,v 1.3 2021/01/18 02:35:48 thorpej Exp $");
31 1.1 ryo
32 1.1 ryo #include <sys/param.h>
33 1.1 ryo #include <sys/types.h>
34 1.1 ryo #include <sys/bus.h>
35 1.1 ryo #include <sys/device.h>
36 1.1 ryo
37 1.1 ryo #include <dev/fdt/fdtvar.h>
38 1.1 ryo #include <dev/sysmon/sysmonvar.h>
39 1.1 ryo
40 1.1 ryo #define TS_CFG_REG1 0x01
41 1.1 ryo #define TS_CFG_REG1_ANA_EN_VCM __BIT(10)
42 1.1 ryo #define TS_CFG_REG1_ANA_EN_VBG __BIT(9)
43 1.1 ryo #define TS_CFG_REG1_FILTER_EN __BIT(5)
44 1.1 ryo #define TS_CFG_REG1_DEM_EN __BIT(3)
45 1.1 ryo #define TS_CFG_REG1_ANA_CH_SEL __BITS(2,0)
46 1.1 ryo #define TS_CFG_REG2 0x02
47 1.1 ryo #define TS_CFG_REG3 0x03
48 1.1 ryo #define TS_CFG_REG4 0x04
49 1.1 ryo #define TS_CFG_REG5 0x05
50 1.1 ryo #define TS_CFG_REG6 0x06
51 1.1 ryo #define TS_CFG_REG7 0x07
52 1.1 ryo #define TS_STAT0_REG 0x10
53 1.1 ryo #define TS_STAT0_FILTER_OUT __BITS(15,0)
54 1.1 ryo #define TS_STAT1_REG 0x11
55 1.1 ryo #define TS_STAT2_REG 0x12
56 1.1 ryo #define TS_STAT3_REG 0x13
57 1.1 ryo #define TS_STAT4_REG 0x14
58 1.1 ryo #define TS_STAT5_REG 0x15
59 1.1 ryo #define TS_STAT6_REG 0x16
60 1.1 ryo #define TS_STAT7_REG 0x17
61 1.1 ryo #define TS_STAT8_REG 0x18
62 1.1 ryo #define TS_STAT9_REG 0x19
63 1.1 ryo
64 1.1 ryo #define THERMAL_READ_REG(sc, reg) \
65 1.1 ryo bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg) * 4)
66 1.1 ryo #define THERMAL_WRITE_REG(sc, reg, val) \
67 1.1 ryo bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg) * 4, (val))
68 1.1 ryo
69 1.1 ryo #define AOSECURE_READ(sc, reg) \
70 1.1 ryo bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh_ao, (reg))
71 1.1 ryo #define AOSECURE_WRITE(sc, reg, val) \
72 1.1 ryo bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh_ao, (reg), (val))
73 1.1 ryo
74 1.1 ryo
75 1.1 ryo struct meson_thermal_config {
76 1.1 ryo const char *name;
77 1.1 ryo bus_size_t aosec_reg;
78 1.1 ryo };
79 1.1 ryo
80 1.1 ryo static struct meson_thermal_config thermal_cpu_conf = {
81 1.1 ryo .name = "CPU",
82 1.1 ryo .aosec_reg = 0x128
83 1.1 ryo };
84 1.1 ryo
85 1.1 ryo static struct meson_thermal_config thermal_ddr_conf = {
86 1.1 ryo .name = "DDR",
87 1.1 ryo .aosec_reg = 0xf0
88 1.1 ryo };
89 1.1 ryo
90 1.3 thorpej static const struct device_compatible_entry compat_data[] = {
91 1.3 thorpej { .compat = "amlogic,g12a-cpu-thermal", .data = &thermal_cpu_conf },
92 1.3 thorpej { .compat = "amlogic,g12a-ddr-thermal", .data = &thermal_ddr_conf },
93 1.3 thorpej
94 1.3 thorpej { 0 }
95 1.1 ryo };
96 1.1 ryo
97 1.1 ryo struct meson_thermal_softc {
98 1.1 ryo device_t sc_dev;
99 1.1 ryo bus_space_tag_t sc_bst;
100 1.1 ryo bus_space_handle_t sc_bsh;
101 1.1 ryo bus_space_handle_t sc_bsh_ao;
102 1.3 thorpej const struct meson_thermal_config *sc_conf;
103 1.1 ryo int sc_phandle;
104 1.1 ryo int sc_ao_calib;
105 1.1 ryo
106 1.1 ryo struct sysmon_envsys *sc_sme;
107 1.1 ryo envsys_data_t sc_sensor_temp;
108 1.1 ryo };
109 1.1 ryo
110 1.1 ryo
111 1.1 ryo static void
112 1.1 ryo meson_thermal_init(struct meson_thermal_softc *sc)
113 1.1 ryo {
114 1.1 ryo uint32_t val;
115 1.1 ryo
116 1.1 ryo val = THERMAL_READ_REG(sc, TS_CFG_REG1);
117 1.1 ryo val |= TS_CFG_REG1_ANA_EN_VCM;
118 1.1 ryo val |= TS_CFG_REG1_ANA_EN_VBG;
119 1.1 ryo val |= TS_CFG_REG1_FILTER_EN;
120 1.1 ryo val |= TS_CFG_REG1_DEM_EN;
121 1.1 ryo val &= ~TS_CFG_REG1_ANA_CH_SEL;
122 1.1 ryo val |= __SHIFTIN(3, TS_CFG_REG1_ANA_CH_SEL);
123 1.1 ryo THERMAL_WRITE_REG(sc, TS_CFG_REG1, val);
124 1.1 ryo
125 1.1 ryo /* read calibration value in ao-secure */
126 1.1 ryo #define TS_AO_CALIB_VERSION_MASK __BITS(31,24)
127 1.1 ryo #define TS_AO_CALIB_SIGN_MASK __BIT(15)
128 1.1 ryo #define TS_AO_CALIB_TEMP_MASK __BITS(14,0)
129 1.1 ryo val = AOSECURE_READ(sc, sc->sc_conf->aosec_reg);
130 1.1 ryo if ((val & TS_AO_CALIB_VERSION_MASK) != 0) {
131 1.1 ryo sc->sc_ao_calib = (val & TS_AO_CALIB_TEMP_MASK);
132 1.1 ryo if ((val & TS_AO_CALIB_SIGN_MASK) != 0)
133 1.1 ryo sc->sc_ao_calib *= -1;
134 1.1 ryo } else {
135 1.1 ryo sc->sc_ao_calib = 0;
136 1.1 ryo }
137 1.1 ryo }
138 1.1 ryo
139 1.1 ryo static int
140 1.1 ryo meson_get_temperature(struct meson_thermal_softc *sc)
141 1.1 ryo {
142 1.1 ryo int val, temp;
143 1.1 ryo int64_t factor, uptat;
144 1.1 ryo
145 1.1 ryo val = THERMAL_READ_REG(sc, TS_STAT0_REG) & TS_STAT0_FILTER_OUT;
146 1.1 ryo
147 1.1 ryo #define CALIB_A 9411
148 1.1 ryo #define CALIB_B 3159
149 1.1 ryo #define CALIB_m_1024 4342 /* 4.24 */
150 1.1 ryo #define CALIB_n_1024 3318 /* 3.24 */
151 1.1 ryo
152 1.1 ryo factor = (val * CALIB_n_1024) / 1024;
153 1.1 ryo uptat = (val * CALIB_m_1024) / 1024;
154 1.1 ryo
155 1.1 ryo uptat = (uptat * (1 << 16)) / ((1 << 16) + factor);
156 1.1 ryo temp = ((uptat + sc->sc_ao_calib) * CALIB_A);
157 1.1 ryo temp = (temp - (CALIB_B * (1 << 16))) * 100000LL / (1 << 16);
158 1.1 ryo
159 1.1 ryo return temp; /* microcelsius */
160 1.1 ryo }
161 1.1 ryo
162 1.1 ryo static void
163 1.1 ryo meson_thermal_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
164 1.1 ryo {
165 1.1 ryo struct meson_thermal_softc *sc = sme->sme_cookie;
166 1.1 ryo
167 1.1 ryo edata->value_cur = meson_get_temperature(sc) + 273150000;
168 1.1 ryo edata->state = ENVSYS_SVALID;
169 1.1 ryo }
170 1.1 ryo
171 1.1 ryo static int
172 1.1 ryo meson_thermal_match(device_t parent, cfdata_t cf, void *aux)
173 1.1 ryo {
174 1.1 ryo struct fdt_attach_args * const faa = aux;
175 1.1 ryo
176 1.1 ryo return of_match_compat_data(faa->faa_phandle, compat_data);
177 1.1 ryo }
178 1.1 ryo
179 1.1 ryo static void
180 1.1 ryo meson_thermal_attach(device_t parent, device_t self, void *aux)
181 1.1 ryo {
182 1.1 ryo struct meson_thermal_softc * const sc = device_private(self);
183 1.1 ryo struct fdt_attach_args * const faa = aux;
184 1.1 ryo bus_addr_t addr;
185 1.1 ryo bus_size_t size, aosize;
186 1.1 ryo int phandle, phandle_aosec;
187 1.1 ryo
188 1.1 ryo sc->sc_dev = self;
189 1.1 ryo sc->sc_bst = faa->faa_bst;
190 1.1 ryo sc->sc_phandle = phandle = faa->faa_phandle;
191 1.3 thorpej sc->sc_conf = of_search_compatible(phandle, compat_data)->data;
192 1.1 ryo
193 1.1 ryo if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
194 1.1 ryo aprint_error(": couldn't get registers\n");
195 1.1 ryo goto attach_failure0;
196 1.1 ryo }
197 1.1 ryo if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
198 1.1 ryo aprint_error(": couldn't map registers\n");
199 1.1 ryo goto attach_failure0;
200 1.1 ryo }
201 1.1 ryo
202 1.1 ryo phandle_aosec = fdtbus_get_phandle(phandle, "amlogic,ao-secure");
203 1.1 ryo if (fdtbus_get_reg(phandle_aosec, 0, &addr, &aosize) != 0) {
204 1.1 ryo aprint_error(": couldn't get registers\n");
205 1.1 ryo goto attach_failure1;
206 1.1 ryo }
207 1.1 ryo if (bus_space_map(sc->sc_bst, addr, aosize, 0, &sc->sc_bsh_ao) != 0) {
208 1.1 ryo aprint_error(": couldn't map registers\n");
209 1.1 ryo goto attach_failure1;
210 1.1 ryo }
211 1.1 ryo
212 1.1 ryo if (fdtbus_clock_enable_index(phandle, 0, true) != 0) {
213 1.1 ryo aprint_error(": couldn't enable clock\n");
214 1.1 ryo goto attach_failure2;
215 1.1 ryo }
216 1.1 ryo
217 1.1 ryo meson_thermal_init(sc);
218 1.1 ryo
219 1.1 ryo aprint_naive("\n");
220 1.1 ryo aprint_normal(": %s TEMP Sensor\n", sc->sc_conf->name);
221 1.1 ryo
222 1.1 ryo sc->sc_sme = sysmon_envsys_create();
223 1.1 ryo sc->sc_sme->sme_name = device_xname(self);
224 1.1 ryo sc->sc_sme->sme_cookie = sc;
225 1.1 ryo sc->sc_sme->sme_flags = 0;
226 1.1 ryo sc->sc_sme->sme_events_timeout = 1;
227 1.1 ryo sc->sc_sme->sme_refresh = meson_thermal_refresh;
228 1.1 ryo sc->sc_sensor_temp.units = ENVSYS_STEMP;
229 1.1 ryo sc->sc_sensor_temp.state = ENVSYS_SINVALID;
230 1.2 macallan snprintf(sc->sc_sensor_temp.desc, ENVSYS_DESCLEN,
231 1.2 macallan "%s", sc->sc_conf->name);
232 1.1 ryo
233 1.1 ryo sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor_temp);
234 1.1 ryo sysmon_envsys_register(sc->sc_sme);
235 1.1 ryo
236 1.1 ryo meson_thermal_refresh(sc->sc_sme, &sc->sc_sensor_temp);
237 1.1 ryo return;
238 1.1 ryo
239 1.1 ryo attach_failure2:
240 1.1 ryo bus_space_unmap(sc->sc_bst, sc->sc_bsh_ao, aosize);
241 1.1 ryo attach_failure1:
242 1.1 ryo bus_space_unmap(sc->sc_bst, sc->sc_bsh, size);
243 1.1 ryo attach_failure0:
244 1.1 ryo return;
245 1.1 ryo }
246 1.1 ryo
247 1.1 ryo CFATTACH_DECL_NEW(meson_thermal, sizeof(struct meson_thermal_softc),
248 1.1 ryo meson_thermal_match, meson_thermal_attach, NULL, NULL);
249