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