2 * Copyright (c) 2017 Semihalf.
3 * Copyright (c) 2017 Stormshield.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/sysctl.h>
32 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/resource.h>
42 #include <machine/fdt.h>
44 #include <dev/ofw/ofw_bus_subr.h>
46 #define READOUT_TO_C(temp) ((temp) / 1000)
51 #define TSEN_STAT_READOUT_VALID 0x1
53 #define A380_TSEN_CTRL_RESET (1 << 8)
55 struct armada_thermal_softc;
57 typedef struct armada_thermal_data {
58 /* Initialize the sensor */
59 void (*tsen_init)(struct armada_thermal_softc *);
61 /* Test for a valid sensor value */
62 boolean_t (*is_valid)(struct armada_thermal_softc *);
64 /* Formula coefficients: temp = (b + m * reg) / div */
71 /* Shift and mask to access the sensor temperature */
77 static boolean_t armada_tsen_readout_valid(struct armada_thermal_softc *);
78 static int armada_tsen_get_temp(struct armada_thermal_softc *, u_long *);
79 static void armada380_tsen_init(struct armada_thermal_softc *);
80 static void armada_temp_update(void *);
82 static const armada_tdata_t armada380_tdata = {
83 .tsen_init = armada380_tsen_init,
84 .is_valid = armada_tsen_readout_valid,
88 .coef_b = 1172499100UL,
94 static int armada_thermal_probe(device_t);
95 static int armada_thermal_attach(device_t);
96 static int armada_thermal_detach(device_t);
98 static device_method_t armada_thermal_methods[] = {
99 DEVMETHOD(device_probe, armada_thermal_probe),
100 DEVMETHOD(device_attach, armada_thermal_attach),
101 DEVMETHOD(device_detach, armada_thermal_detach),
106 struct armada_thermal_softc {
109 struct resource *stat_res;
110 struct resource *ctrl_res;
112 struct callout temp_upd;
113 struct mtx temp_upd_mtx;
115 const armada_tdata_t *tdata;
117 u_long chip_temperature;
120 static driver_t armada_thermal_driver = {
122 armada_thermal_methods,
123 sizeof(struct armada_thermal_softc)
126 static devclass_t armada_thermal_devclass;
128 DRIVER_MODULE(armada_thermal, simplebus, armada_thermal_driver,
129 armada_thermal_devclass, 0, 0);
130 DRIVER_MODULE(armada_thermal, ofwbus, armada_thermal_driver,
131 armada_thermal_devclass, 0, 0);
134 armada_thermal_probe(device_t dev)
136 struct armada_thermal_softc *sc;
138 sc = device_get_softc(dev);
140 if (!ofw_bus_status_okay(dev))
143 if (ofw_bus_is_compatible(dev, "marvell,armada380-thermal")) {
144 device_set_desc(dev, "Armada380 Thermal Control");
145 sc->tdata = &armada380_tdata;
147 return (BUS_PROBE_DEFAULT);
154 armada_thermal_attach(device_t dev)
156 struct armada_thermal_softc *sc;
157 const armada_tdata_t *tdata;
158 struct sysctl_ctx_list *sctx;
159 struct sysctl_oid_list *schildren;
163 sc = device_get_softc(dev);
165 /* Allocate CTRL and STAT register spaces */
167 sc->stat_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
169 if (sc->stat_res == NULL) {
171 "Could not allocate memory for the status register\n");
176 sc->ctrl_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
178 if (sc->ctrl_res == NULL) {
180 "Could not allocate memory for the control register\n");
181 bus_release_resource(dev, SYS_RES_MEMORY,
182 rman_get_rid(sc->stat_res), sc->stat_res);
187 /* Now initialize the sensor */
189 tdata->tsen_init(sc);
190 /* Set initial temperature value */
191 for (timeout = 1000; timeout > 0; timeout--) {
192 if (armada_tsen_get_temp(sc, &sc->chip_temperature) == 0)
197 bus_release_resource(dev, SYS_RES_MEMORY,
198 rman_get_rid(sc->stat_res), sc->stat_res);
200 bus_release_resource(dev, SYS_RES_MEMORY,
201 rman_get_rid(sc->ctrl_res), sc->ctrl_res);
205 /* Initialize mutex */
206 mtx_init(&sc->temp_upd_mtx, "Armada Thermal", NULL, MTX_DEF);
207 /* Set up the temperature update callout */
208 callout_init_mtx(&sc->temp_upd, &sc->temp_upd_mtx, 0);
209 /* Schedule callout */
210 callout_reset(&sc->temp_upd, hz, armada_temp_update, sc);
212 sctx = device_get_sysctl_ctx(dev);
213 schildren = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
214 SYSCTL_ADD_LONG(sctx, schildren, OID_AUTO, "temperature",
215 CTLFLAG_RD, &sc->chip_temperature, "SoC temperature");
221 armada_thermal_detach(device_t dev)
223 struct armada_thermal_softc *sc;
225 sc = device_get_softc(dev);
227 if (!device_is_attached(dev))
230 callout_drain(&sc->temp_upd);
232 sc->chip_temperature = 0;
234 bus_release_resource(dev, SYS_RES_MEMORY,
235 rman_get_rid(sc->stat_res), sc->stat_res);
238 bus_release_resource(dev, SYS_RES_MEMORY,
239 rman_get_rid(sc->ctrl_res), sc->ctrl_res);
246 armada_tsen_readout_valid(struct armada_thermal_softc *sc)
248 const armada_tdata_t *tdata;
253 tsen_stat = bus_read_4(sc->stat_res, 0);
255 tsen_stat >>= tdata->is_valid_shift;
256 is_valid = ((tsen_stat & TSEN_STAT_READOUT_VALID) != 0);
262 armada_tsen_get_temp(struct armada_thermal_softc *sc, u_long *temp)
264 const armada_tdata_t *tdata;
270 /* Check if the readout is valid */
271 if ((tdata->is_valid != NULL) && !tdata->is_valid(sc))
274 reg = bus_read_4(sc->stat_res, 0);
275 reg = (reg >> tdata->temp_shift) & tdata->temp_mask;
277 /* Get formula coefficients */
280 div = tdata->coef_div;
283 tmp = ((m * reg) - b) / div;
285 tmp = (b - (m * reg)) / div;
287 *temp = READOUT_TO_C(tmp);
293 armada380_tsen_init(struct armada_thermal_softc *sc)
297 tsen_ctrl = bus_read_4(sc->ctrl_res, 0);
298 if ((tsen_ctrl & A380_TSEN_CTRL_RESET) == 0) {
299 tsen_ctrl |= A380_TSEN_CTRL_RESET;
300 bus_write_4(sc->ctrl_res, 0, tsen_ctrl);
306 armada_temp_update(void *arg)
308 struct armada_thermal_softc *sc;
311 /* Update temperature value, keel old if the readout is not valid */
312 (void)armada_tsen_get_temp(sc, &sc->chip_temperature);
314 callout_reset(&sc->temp_upd, hz, armada_temp_update, sc);