jh71x0_temp.c revision 1.1 1 /* $NetBSD: jh71x0_temp.c,v 1.1 2025/01/03 11:49:04 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2025 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nick Hudson
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: jh71x0_temp.c,v 1.1 2025/01/03 11:49:04 skrll Exp $");
34
35 #include <sys/param.h>
36
37 #include <dev/fdt/fdtvar.h>
38
39 #include <dev/sysmon/sysmonvar.h>
40
41 struct jh71x0_temp_softc {
42 device_t sc_dev;
43 bus_space_tag_t sc_bst;
44 bus_space_handle_t sc_bsh;
45 int sc_phandle;
46
47 struct sysmon_envsys * sc_sme;
48 envsys_data_t sc_sensor;
49 };
50
51 #define RD4(sc, reg) \
52 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
53 #define WR4(sc, reg, val) \
54 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
55
56 /* Register definitions */
57 #define JH71X0_TEMP 0x0000
58 #define JH71X0_TEMP_RSTN __BIT(0)
59 #define JH71X0_TEMP_PD __BIT(1)
60 #define JH71X0_TEMP_RUN __BIT(2)
61 #define JH71X0_TEMP_DOUT_MASK __BITS(27, 16)
62
63 /* DOUT to Celcius conversion constants */
64 #define JH71X0TEMP_Y1000 237500L
65 #define JH71X0TEMP_Z 4094L
66 #define JH71X0TEMP_K1000 81100L
67
68 /* Calculate the temperature in milli Celcius */
69 static int32_t
70 jh71x0_temp_get(struct jh71x0_temp_softc *sc)
71 {
72 uint32_t temp = RD4(sc, JH71X0_TEMP);
73 uint32_t dout = __SHIFTOUT(temp, JH71X0_TEMP_DOUT_MASK);
74
75 return (dout * JH71X0TEMP_Y1000) / JH71X0TEMP_Z - JH71X0TEMP_K1000;
76 }
77
78 static void
79 jh71x0_temp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
80 {
81 struct jh71x0_temp_softc * const sc = sme->sme_cookie;
82
83 // Convert milli Celcius to micro Kelvin
84 sc->sc_sensor.value_cur = 273150000 + 1000 * jh71x0_temp_get(sc);
85 sc->sc_sensor.state = ENVSYS_SVALID;
86 }
87
88 static void
89 jh71x0_temp_init(struct jh71x0_temp_softc *sc)
90 {
91 /* Power down */
92 WR4(sc, JH71X0_TEMP, JH71X0_TEMP_PD);
93 delay(1);
94
95 /* Power up with reset asserted */
96 WR4(sc, JH71X0_TEMP, 0);
97 delay(60);
98
99 /* Deassert reset */
100 WR4(sc, JH71X0_TEMP, JH71X0_TEMP_RSTN);
101 delay(1);
102
103 /* Start measuring */
104 WR4(sc, JH71X0_TEMP, JH71X0_TEMP_RSTN | JH71X0_TEMP_RUN);
105 }
106
107 /* Compat string(s) */
108 static const struct device_compatible_entry compat_data[] = {
109 { .compat = "starfive,jh7100-temp" },
110 { .compat = "starfive,jh7110-temp" },
111 DEVICE_COMPAT_EOL
112 };
113
114 static int
115 jh71x0_temp_match(device_t parent, cfdata_t cf, void *aux)
116 {
117 struct fdt_attach_args * const faa = aux;
118
119 return of_compatible_match(faa->faa_phandle, compat_data);
120 }
121
122 static void
123 jh71x0_temp_attach(device_t parent, device_t self, void *aux)
124 {
125 struct jh71x0_temp_softc * const sc = device_private(self);
126 struct fdt_attach_args * const faa = aux;
127 const int phandle = faa->faa_phandle;
128 bus_addr_t addr;
129 bus_size_t size;
130 int error;
131
132 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
133 aprint_error(": couldn't get registers\n");
134 return;
135 }
136
137 sc->sc_dev = self;
138 sc->sc_phandle = phandle;
139 sc->sc_bst = faa->faa_bst;
140
141 error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
142 if (error) {
143 aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr,
144 error);
145 return;
146 }
147
148 const char *crs[] = { "bus", "sense" };
149 for (size_t i = 0; i < __arraycount(crs); i++) {
150 const char *cr = crs[i];
151
152 error = fdtbus_clock_enable(phandle, cr, true);
153 if (error) {
154 aprint_error(": couldn't enable clock '%s'\n", cr);
155 return;
156 }
157 struct fdtbus_reset * rst = fdtbus_reset_get(phandle, cr);
158 if (rst == NULL) {
159 aprint_error(": couldn't get reset '%s'\n", cr);
160 return;
161 }
162 error = fdtbus_reset_deassert(rst);
163 if (error) {
164 aprint_error(": couldn't de-assert reset '%s'\n", cr);
165 return;
166 }
167 }
168
169 aprint_naive("\n");
170 aprint_normal(": JH71x0 temperature sensor\n");
171
172 jh71x0_temp_init(sc);
173
174 sc->sc_sme = sysmon_envsys_create();
175 /* Initialize sensor data. */
176 sc->sc_sensor.units = ENVSYS_STEMP;
177 sc->sc_sensor.state = ENVSYS_SINVALID;
178 (void)strlcpy(sc->sc_sensor.desc, device_xname(self),
179 sizeof(sc->sc_sensor.desc));
180 if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
181 sysmon_envsys_destroy(sc->sc_sme);
182 return;
183 }
184
185 /* Hook into system monitor. */
186 sc->sc_sme->sme_name = device_xname(self);
187 sc->sc_sme->sme_cookie = sc;
188 sc->sc_sme->sme_refresh = jh71x0_temp_refresh;
189
190 if (sysmon_envsys_register(sc->sc_sme)) {
191 aprint_error_dev(self, "unable to register with sysmon\n");
192 sysmon_envsys_destroy(sc->sc_sme);
193 }
194 }
195
196 CFATTACH_DECL_NEW(jh71x0_temp, sizeof(struct jh71x0_temp_softc),
197 jh71x0_temp_match, jh71x0_temp_attach, NULL, NULL);
198