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>
47 #include <dev/extres/syscon/syscon.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
52 #include "syscon_if.h"
54 #define CONTROL0 0 /* Offset in config->regs[] array */
55 #define CONTROL0_TSEN_START (1 << 0)
56 #define CONTROL0_TSEN_RESET (1 << 1)
57 #define CONTROL0_TSEN_EN (1 << 2)
58 #define CONTROL0_CHANNEL_SHIFT 13
59 #define CONTROL0_CHANNEL_MASK 0xF
60 #define CONTROL0_OSR_SHIFT 24
61 #define CONTROL0_OSR_MAX 3 /* OSR = 512 * 4uS = ~2mS */
62 #define CONTROL0_MODE_SHIFT 30
63 #define CONTROL0_MODE_EXTERNAL 0x2
64 #define CONTROL0_MODE_MASK 0x3
66 #define CONTROL1 1 /* Offset in config->regs[] array */
67 /* This doesn't seems to work */
68 #define CONTROL1_TSEN_SENS_SHIFT 21
69 #define CONTROL1_TSEN_SENS_MASK 0x7
71 #define STATUS 2 /* Offset in config->regs[] array */
72 #define STATUS_TEMP_MASK 0x3FF
74 enum mv_thermal_type {
79 struct mv_thermal_config {
80 enum mv_thermal_type type;
90 struct mv_thermal_softc {
92 struct syscon *syscon;
95 struct mv_thermal_config *config;
99 static struct mv_thermal_config mv_ap806_config = {
101 .regs = {0x84, 0x88, 0x8C},
104 .calib_add = -150000,
106 .valid_mask = (1 << 16),
107 .signed_value = true,
110 static struct mv_thermal_config mv_cp110_config = {
112 .regs = {0x70, 0x74, 0x78},
113 .calib_mul = 2000096,
114 .calib_add = 1172499100,
116 .valid_mask = (1 << 10),
117 .signed_value = false,
120 static struct ofw_compat_data compat_data[] = {
121 {"marvell,armada-ap806-thermal", (uintptr_t) &mv_ap806_config},
122 {"marvell,armada-cp110-thermal", (uintptr_t) &mv_cp110_config},
126 #define RD4(sc, reg) \
127 SYSCON_READ_4((sc)->syscon, sc->config->regs[reg])
128 #define WR4(sc, reg, val) \
129 SYSCON_WRITE_4((sc)->syscon, sc->config->regs[reg], (val))
131 static inline int32_t sign_extend(uint32_t value, int index)
136 return ((int32_t)(value << shift) >> shift);
140 mv_thermal_wait_sensor(struct mv_thermal_softc *sc)
146 while (--timeout > 0) {
147 reg = RD4(sc, STATUS);
148 if ((reg & sc->config->valid_mask) == sc->config->valid_mask)
160 mv_thermal_select_sensor(struct mv_thermal_softc *sc, int sensor)
164 if (sc->cur_sensor == sensor)
167 /* Stop the current reading and reset the module */
168 reg = RD4(sc, CONTROL0);
169 reg &= ~(CONTROL0_TSEN_START | CONTROL0_TSEN_EN);
170 WR4(sc, CONTROL0, reg);
172 /* Switch to the selected sensor */
174 * NOTE : Datasheet says to use CONTROL1 for selecting
175 * but when doing so the sensors >0 are never ready
176 * Do what Linux does using undocumented bits in CONTROL0
178 /* This reset automatically to the sensor 0 */
179 reg &= ~(CONTROL0_MODE_MASK << CONTROL0_MODE_SHIFT);
181 /* Select external sensor */
182 reg |= CONTROL0_MODE_EXTERNAL << CONTROL0_MODE_SHIFT;
183 reg &= ~(CONTROL0_CHANNEL_MASK << CONTROL0_CHANNEL_SHIFT);
184 reg |= (sensor - 1) << CONTROL0_CHANNEL_SHIFT;
186 WR4(sc, CONTROL0, reg);
187 sc->cur_sensor = sensor;
189 /* Start the reading */
190 reg = RD4(sc, CONTROL0);
191 reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_EN;
192 WR4(sc, CONTROL0, reg);
194 return (mv_thermal_wait_sensor(sc));
198 mv_thermal_read_sensor(struct mv_thermal_softc *sc, int sensor, int *temp)
203 rv = mv_thermal_select_sensor(sc, sensor);
207 reg = RD4(sc, STATUS) & STATUS_TEMP_MASK;
209 if (sc->config->signed_value)
210 sample = sign_extend(reg, fls(STATUS_TEMP_MASK) - 1);
214 *temp = ((sample * sc->config->calib_mul) - sc->config->calib_add) /
215 sc->config->calib_div;
221 ap806_init(struct mv_thermal_softc *sc)
225 /* Start the temp capture/conversion */
226 reg = RD4(sc, CONTROL0);
227 reg &= ~CONTROL0_TSEN_RESET;
228 reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_EN;
230 /* Sample every ~2ms */
231 reg |= CONTROL0_OSR_MAX << CONTROL0_OSR_SHIFT;
233 WR4(sc, CONTROL0, reg);
235 /* Since we just started the module wait for the sensor to be ready */
236 mv_thermal_wait_sensor(sc);
242 cp110_init(struct mv_thermal_softc *sc)
246 reg = RD4(sc, CONTROL1);
249 WR4(sc, CONTROL1, reg);
251 /* Sample every ~2ms */
252 reg = RD4(sc, CONTROL0);
253 reg |= CONTROL0_OSR_MAX << CONTROL0_OSR_SHIFT;
254 WR4(sc, CONTROL0, reg);
260 mv_thermal_sysctl(SYSCTL_HANDLER_ARGS)
262 struct mv_thermal_softc *sc;
267 sc = device_get_softc(dev);
268 mtx_lock(&(sc)->mtx);
270 if (mv_thermal_read_sensor(sc, sensor, &val) == 0) {
271 /* Convert to Kelvin */
274 device_printf(dev, "Timeout waiting for sensor\n");
277 mtx_unlock(&(sc)->mtx);
278 return sysctl_handle_opaque(oidp, &val, sizeof(val), req);
282 mv_thermal_probe(device_t dev)
285 if (!ofw_bus_status_okay(dev))
288 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
291 device_set_desc(dev, "Marvell Thermal Sensor Controller");
292 return (BUS_PROBE_DEFAULT);
296 mv_thermal_attach(device_t dev)
298 struct mv_thermal_softc *sc;
299 struct sysctl_ctx_list *ctx;
300 struct sysctl_oid_list *oid;
306 sc = device_get_softc(dev);
308 node = ofw_bus_get_node(dev);
310 sc->config = (struct mv_thermal_config *)
311 ofw_bus_search_compatible(dev, compat_data)->ocd_data;
313 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
316 if (SYSCON_GET_HANDLE(sc->dev, &sc->syscon) != 0 ||
317 sc->syscon == NULL) {
318 device_printf(dev, "cannot get syscon for device\n");
323 switch (sc->config->type) {
332 ctx = device_get_sysctl_ctx(dev);
333 oid = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
334 /* There is always at least one sensor */
335 SYSCTL_ADD_PROC(ctx, oid, OID_AUTO, "internal",
336 CTLTYPE_INT | CTLFLAG_RD,
337 dev, 0, mv_thermal_sysctl,
339 "Internal Temperature");
341 for (i = 0; i < sc->config->ncpus; i++) {
342 snprintf(name, sizeof(name), "cpu%d", i);
343 snprintf(desc, sizeof(desc), "CPU%d Temperature", i);
344 SYSCTL_ADD_PROC(ctx, oid, OID_AUTO, name,
345 CTLTYPE_INT | CTLFLAG_RD,
346 dev, i + 1, mv_thermal_sysctl,
355 mv_thermal_detach(device_t dev)
357 struct mv_thermal_softc *sc;
359 sc = device_get_softc(dev);
364 static device_method_t mv_thermal_methods[] = {
365 /* Device interface */
366 DEVMETHOD(device_probe, mv_thermal_probe),
367 DEVMETHOD(device_attach, mv_thermal_attach),
368 DEVMETHOD(device_detach, mv_thermal_detach),
373 static devclass_t mv_thermal_devclass;
375 static driver_t mv_thermal_driver = {
378 sizeof(struct mv_thermal_softc),
381 DRIVER_MODULE(mv_thermal, simplebus, mv_thermal_driver,
382 mv_thermal_devclass, 0, 0);