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