2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018 Rubicon Communications, LLC (Netgate)
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/sysctl.h>
44 #include <machine/bus.h>
45 #include <machine/resource.h>
46 #include <machine/intr.h>
48 #include <dev/fdt/simplebus.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
54 #define CONTROL0_TSEN_START (1 << 0)
55 #define CONTROL0_TSEN_RESET (1 << 1)
56 #define CONTROL0_TSEN_EN (1 << 2)
57 #define CONTROL0_CHANNEL_SHIFT 13
58 #define CONTROL0_CHANNEL_MASK 0xF
59 #define CONTROL0_OSR_SHIFT 24
60 #define CONTROL0_OSR_MAX 3 /* OSR = 512 * 4uS = ~2mS */
61 #define CONTROL0_MODE_SHIFT 30
62 #define CONTROL0_MODE_EXTERNAL 0x2
63 #define CONTROL0_MODE_MASK 0x3
66 /* This doesn't seems to work */
67 #define CONTROL1_TSEN_SENS_SHIFT 21
68 #define CONTROL1_TSEN_SENS_MASK 0x7
71 #define STATUS_TEMP_MASK 0x3FF
73 enum mv_thermal_type {
78 struct mv_thermal_config {
79 enum mv_thermal_type type;
88 struct mv_thermal_softc {
90 struct resource *res[2];
93 struct mv_thermal_config *config;
97 static struct mv_thermal_config mv_ap806_config = {
101 .calib_add = -150000,
103 .valid_mask = (1 << 16),
104 .signed_value = true,
107 static struct mv_thermal_config mv_cp110_config = {
109 .calib_mul = 2000096,
110 .calib_add = 1172499100,
112 .valid_mask = (1 << 10),
113 .signed_value = false,
116 static struct resource_spec mv_thermal_res_spec[] = {
117 { SYS_RES_MEMORY, 0, RF_ACTIVE },
118 { SYS_RES_MEMORY, 1, RF_ACTIVE },
122 static struct ofw_compat_data compat_data[] = {
123 {"marvell,armada-ap806-thermal", (uintptr_t) &mv_ap806_config},
124 {"marvell,armada-cp110-thermal", (uintptr_t) &mv_cp110_config},
128 #define RD_STA(sc, reg) bus_read_4((sc)->res[0], (reg))
129 #define WR_STA(sc, reg, val) bus_write_4((sc)->res[0], (reg), (val))
130 #define RD_CON(sc, reg) bus_read_4((sc)->res[1], (reg))
131 #define WR_CON(sc, reg, val) bus_write_4((sc)->res[1], (reg), (val))
133 static inline int32_t sign_extend(uint32_t value, int index)
138 return ((int32_t)(value << shift) >> shift);
142 mv_thermal_wait_sensor(struct mv_thermal_softc *sc)
148 while (--timeout > 0) {
149 reg = RD_STA(sc, STATUS);
150 if ((reg & sc->config->valid_mask) == sc->config->valid_mask)
162 mv_thermal_select_sensor(struct mv_thermal_softc *sc, int sensor)
166 if (sc->cur_sensor == sensor)
169 /* Stop the current reading and reset the module */
170 reg = RD_CON(sc, CONTROL0);
171 reg &= ~(CONTROL0_TSEN_START | CONTROL0_TSEN_EN);
172 WR_CON(sc, CONTROL0, reg);
174 /* Switch to the selected sensor */
176 * NOTE : Datasheet says to use CONTROL1 for selecting
177 * but when doing so the sensors >0 are never ready
178 * Do what Linux does using undocumented bits in CONTROL0
180 /* This reset automatically to the sensor 0 */
181 reg &= ~(CONTROL0_MODE_MASK << CONTROL0_MODE_SHIFT);
183 /* Select external sensor */
184 reg |= CONTROL0_MODE_EXTERNAL << CONTROL0_MODE_SHIFT;
185 reg &= ~(CONTROL0_CHANNEL_MASK << CONTROL0_CHANNEL_SHIFT);
186 reg |= (sensor - 1) << CONTROL0_CHANNEL_SHIFT;
188 WR_CON(sc, CONTROL0, reg);
189 sc->cur_sensor = sensor;
191 /* Start the reading */
192 reg = RD_CON(sc, CONTROL0);
193 reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_EN;
194 WR_CON(sc, CONTROL0, reg);
196 return (mv_thermal_wait_sensor(sc));
200 mv_thermal_read_sensor(struct mv_thermal_softc *sc, int sensor, int *temp)
205 rv = mv_thermal_select_sensor(sc, sensor);
209 reg = RD_STA(sc, STATUS) & STATUS_TEMP_MASK;
211 if (sc->config->signed_value)
212 sample = sign_extend(reg, fls(STATUS_TEMP_MASK) - 1);
216 *temp = ((sample * sc->config->calib_mul) - sc->config->calib_add) /
217 sc->config->calib_div;
223 ap806_init(struct mv_thermal_softc *sc)
227 /* Start the temp capture/conversion */
228 reg = RD_CON(sc, CONTROL0);
229 reg &= ~CONTROL0_TSEN_RESET;
230 reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_EN;
232 /* Sample every ~2ms */
233 reg |= CONTROL0_OSR_MAX << CONTROL0_OSR_SHIFT;
235 WR_CON(sc, CONTROL0, reg);
237 /* Since we just started the module wait for the sensor to be ready */
238 mv_thermal_wait_sensor(sc);
244 cp110_init(struct mv_thermal_softc *sc)
248 reg = RD_CON(sc, CONTROL1);
251 WR_CON(sc, CONTROL1, reg);
253 /* Sample every ~2ms */
254 reg = RD_CON(sc, CONTROL0);
255 reg |= CONTROL0_OSR_MAX << CONTROL0_OSR_SHIFT;
256 WR_CON(sc, CONTROL0, reg);
262 mv_thermal_sysctl(SYSCTL_HANDLER_ARGS)
264 struct mv_thermal_softc *sc;
269 sc = device_get_softc(dev);
270 mtx_lock(&(sc)->mtx);
272 if (mv_thermal_read_sensor(sc, sensor, &val) == 0) {
273 /* Convert to Kelvin */
276 device_printf(dev, "Timeout waiting for sensor\n");
279 mtx_unlock(&(sc)->mtx);
280 return sysctl_handle_opaque(oidp, &val, sizeof(val), req);
284 mv_thermal_probe(device_t dev)
287 if (!ofw_bus_status_okay(dev))
290 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
293 device_set_desc(dev, "Marvell Thermal Sensor Controller");
294 return (BUS_PROBE_DEFAULT);
298 mv_thermal_attach(device_t dev)
300 struct mv_thermal_softc *sc;
301 struct sysctl_ctx_list *ctx;
302 struct sysctl_oid_list *oid;
307 sc = device_get_softc(dev);
310 sc->config = (struct mv_thermal_config *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
312 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
314 if (bus_alloc_resources(dev, mv_thermal_res_spec, sc->res) != 0) {
315 device_printf(dev, "cannot allocate resources for device\n");
320 switch (sc->config->type) {
329 ctx = device_get_sysctl_ctx(dev);
330 oid = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
331 /* There is always at least one sensor */
332 SYSCTL_ADD_PROC(ctx, oid, OID_AUTO, "internal",
333 CTLTYPE_INT | CTLFLAG_RD,
334 dev, 0, mv_thermal_sysctl,
336 "Internal Temperature");
338 for (i = 0; i < sc->config->ncpus; i++) {
339 snprintf(name, sizeof(name), "cpu%d", i);
340 snprintf(desc, sizeof(desc), "CPU%d Temperature", i);
341 SYSCTL_ADD_PROC(ctx, oid, OID_AUTO, name,
342 CTLTYPE_INT | CTLFLAG_RD,
343 dev, i + 1, mv_thermal_sysctl,
352 mv_thermal_detach(device_t dev)
354 struct mv_thermal_softc *sc;
356 sc = device_get_softc(dev);
358 bus_release_resources(dev, mv_thermal_res_spec, sc->res);
363 static device_method_t mv_thermal_methods[] = {
364 /* Device interface */
365 DEVMETHOD(device_probe, mv_thermal_probe),
366 DEVMETHOD(device_attach, mv_thermal_attach),
367 DEVMETHOD(device_detach, mv_thermal_detach),
372 static devclass_t mv_thermal_devclass;
374 static driver_t mv_thermal_driver = {
377 sizeof(struct mv_thermal_softc),
380 DRIVER_MODULE(mv_thermal, simplebus, mv_thermal_driver,
381 mv_thermal_devclass, 0, 0);