2018-12-12 22:33:05 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 Rubicon Communications, LLC (Netgate)
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/rman.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
|
|
|
#include <machine/bus.h>
|
|
|
|
#include <machine/resource.h>
|
|
|
|
#include <machine/intr.h>
|
2019-10-20 10:48:27 +00:00
|
|
|
#include <dev/extres/syscon/syscon.h>
|
2018-12-12 22:33:05 +00:00
|
|
|
|
|
|
|
#include <dev/ofw/ofw_bus.h>
|
|
|
|
#include <dev/ofw/ofw_bus_subr.h>
|
|
|
|
|
2019-10-20 10:48:27 +00:00
|
|
|
#include "syscon_if.h"
|
|
|
|
|
|
|
|
#define CONTROL0 0 /* Offset in config->regs[] array */
|
2018-12-12 22:33:05 +00:00
|
|
|
#define CONTROL0_TSEN_START (1 << 0)
|
|
|
|
#define CONTROL0_TSEN_RESET (1 << 1)
|
|
|
|
#define CONTROL0_TSEN_EN (1 << 2)
|
|
|
|
#define CONTROL0_CHANNEL_SHIFT 13
|
|
|
|
#define CONTROL0_CHANNEL_MASK 0xF
|
|
|
|
#define CONTROL0_OSR_SHIFT 24
|
|
|
|
#define CONTROL0_OSR_MAX 3 /* OSR = 512 * 4uS = ~2mS */
|
|
|
|
#define CONTROL0_MODE_SHIFT 30
|
|
|
|
#define CONTROL0_MODE_EXTERNAL 0x2
|
|
|
|
#define CONTROL0_MODE_MASK 0x3
|
|
|
|
|
2019-10-20 10:48:27 +00:00
|
|
|
#define CONTROL1 1 /* Offset in config->regs[] array */
|
2018-12-12 22:33:05 +00:00
|
|
|
/* This doesn't seems to work */
|
|
|
|
#define CONTROL1_TSEN_SENS_SHIFT 21
|
|
|
|
#define CONTROL1_TSEN_SENS_MASK 0x7
|
|
|
|
|
2019-10-20 10:48:27 +00:00
|
|
|
#define STATUS 2 /* Offset in config->regs[] array */
|
2018-12-12 22:33:05 +00:00
|
|
|
#define STATUS_TEMP_MASK 0x3FF
|
|
|
|
|
|
|
|
enum mv_thermal_type {
|
|
|
|
MV_AP806 = 1,
|
|
|
|
MV_CP110,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct mv_thermal_config {
|
|
|
|
enum mv_thermal_type type;
|
2019-10-20 10:48:27 +00:00
|
|
|
int regs[3];
|
2018-12-12 22:33:05 +00:00
|
|
|
int ncpus;
|
|
|
|
int64_t calib_mul;
|
|
|
|
int64_t calib_add;
|
|
|
|
int64_t calib_div;
|
|
|
|
uint32_t valid_mask;
|
|
|
|
bool signed_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct mv_thermal_softc {
|
|
|
|
device_t dev;
|
2019-10-20 10:48:27 +00:00
|
|
|
struct syscon *syscon;
|
2018-12-12 22:33:05 +00:00
|
|
|
struct mtx mtx;
|
|
|
|
|
2019-10-20 10:48:27 +00:00
|
|
|
struct mv_thermal_config *config;
|
|
|
|
int cur_sensor;
|
2018-12-12 22:33:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct mv_thermal_config mv_ap806_config = {
|
|
|
|
.type = MV_AP806,
|
2019-10-20 10:48:27 +00:00
|
|
|
.regs = {0x84, 0x88, 0x8C},
|
2018-12-12 22:33:05 +00:00
|
|
|
.ncpus = 4,
|
|
|
|
.calib_mul = 423,
|
|
|
|
.calib_add = -150000,
|
|
|
|
.calib_div = 100,
|
|
|
|
.valid_mask = (1 << 16),
|
|
|
|
.signed_value = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct mv_thermal_config mv_cp110_config = {
|
|
|
|
.type = MV_CP110,
|
2019-10-20 10:48:27 +00:00
|
|
|
.regs = {0x70, 0x74, 0x78},
|
2018-12-12 22:33:05 +00:00
|
|
|
.calib_mul = 2000096,
|
|
|
|
.calib_add = 1172499100,
|
|
|
|
.calib_div = 420100,
|
|
|
|
.valid_mask = (1 << 10),
|
|
|
|
.signed_value = false,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct ofw_compat_data compat_data[] = {
|
|
|
|
{"marvell,armada-ap806-thermal", (uintptr_t) &mv_ap806_config},
|
|
|
|
{"marvell,armada-cp110-thermal", (uintptr_t) &mv_cp110_config},
|
|
|
|
{NULL, 0}
|
|
|
|
};
|
|
|
|
|
2019-10-20 10:48:27 +00:00
|
|
|
#define RD4(sc, reg) \
|
|
|
|
SYSCON_READ_4((sc)->syscon, sc->config->regs[reg])
|
|
|
|
#define WR4(sc, reg, val) \
|
|
|
|
SYSCON_WRITE_4((sc)->syscon, sc->config->regs[reg], (val))
|
2018-12-12 22:33:05 +00:00
|
|
|
|
|
|
|
static inline int32_t sign_extend(uint32_t value, int index)
|
|
|
|
{
|
|
|
|
uint8_t shift;
|
|
|
|
|
|
|
|
shift = 31 - index;
|
|
|
|
return ((int32_t)(value << shift) >> shift);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mv_thermal_wait_sensor(struct mv_thermal_softc *sc)
|
|
|
|
{
|
|
|
|
uint32_t reg;
|
|
|
|
uint32_t timeout;
|
|
|
|
|
|
|
|
timeout = 100000;
|
|
|
|
while (--timeout > 0) {
|
2019-10-20 10:48:27 +00:00
|
|
|
reg = RD4(sc, STATUS);
|
2018-12-12 22:33:05 +00:00
|
|
|
if ((reg & sc->config->valid_mask) == sc->config->valid_mask)
|
|
|
|
break;
|
|
|
|
DELAY(100);
|
|
|
|
}
|
|
|
|
if (timeout == 0) {
|
|
|
|
return (ETIMEDOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mv_thermal_select_sensor(struct mv_thermal_softc *sc, int sensor)
|
|
|
|
{
|
|
|
|
uint32_t reg;
|
|
|
|
|
|
|
|
if (sc->cur_sensor == sensor)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/* Stop the current reading and reset the module */
|
2019-10-20 10:48:27 +00:00
|
|
|
reg = RD4(sc, CONTROL0);
|
2018-12-12 22:33:05 +00:00
|
|
|
reg &= ~(CONTROL0_TSEN_START | CONTROL0_TSEN_EN);
|
2019-10-20 10:48:27 +00:00
|
|
|
WR4(sc, CONTROL0, reg);
|
2018-12-12 22:33:05 +00:00
|
|
|
|
|
|
|
/* Switch to the selected sensor */
|
2019-10-20 10:48:27 +00:00
|
|
|
/*
|
2018-12-12 22:33:05 +00:00
|
|
|
* NOTE : Datasheet says to use CONTROL1 for selecting
|
|
|
|
* but when doing so the sensors >0 are never ready
|
|
|
|
* Do what Linux does using undocumented bits in CONTROL0
|
|
|
|
*/
|
|
|
|
/* This reset automatically to the sensor 0 */
|
|
|
|
reg &= ~(CONTROL0_MODE_MASK << CONTROL0_MODE_SHIFT);
|
|
|
|
if (sensor) {
|
|
|
|
/* Select external sensor */
|
|
|
|
reg |= CONTROL0_MODE_EXTERNAL << CONTROL0_MODE_SHIFT;
|
|
|
|
reg &= ~(CONTROL0_CHANNEL_MASK << CONTROL0_CHANNEL_SHIFT);
|
|
|
|
reg |= (sensor - 1) << CONTROL0_CHANNEL_SHIFT;
|
|
|
|
}
|
2019-10-20 10:48:27 +00:00
|
|
|
WR4(sc, CONTROL0, reg);
|
2018-12-12 22:33:05 +00:00
|
|
|
sc->cur_sensor = sensor;
|
|
|
|
|
|
|
|
/* Start the reading */
|
2019-10-20 10:48:27 +00:00
|
|
|
reg = RD4(sc, CONTROL0);
|
2018-12-12 22:33:05 +00:00
|
|
|
reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_EN;
|
2019-10-20 10:48:27 +00:00
|
|
|
WR4(sc, CONTROL0, reg);
|
2018-12-12 22:33:05 +00:00
|
|
|
|
|
|
|
return (mv_thermal_wait_sensor(sc));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mv_thermal_read_sensor(struct mv_thermal_softc *sc, int sensor, int *temp)
|
|
|
|
{
|
|
|
|
uint32_t reg;
|
|
|
|
int64_t sample, rv;
|
|
|
|
|
|
|
|
rv = mv_thermal_select_sensor(sc, sensor);
|
|
|
|
if (rv != 0)
|
|
|
|
return (rv);
|
|
|
|
|
2019-10-20 10:48:27 +00:00
|
|
|
reg = RD4(sc, STATUS) & STATUS_TEMP_MASK;
|
2018-12-12 22:33:05 +00:00
|
|
|
|
|
|
|
if (sc->config->signed_value)
|
|
|
|
sample = sign_extend(reg, fls(STATUS_TEMP_MASK) - 1);
|
|
|
|
else
|
|
|
|
sample = reg;
|
|
|
|
|
|
|
|
*temp = ((sample * sc->config->calib_mul) - sc->config->calib_add) /
|
|
|
|
sc->config->calib_div;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ap806_init(struct mv_thermal_softc *sc)
|
|
|
|
{
|
|
|
|
uint32_t reg;
|
|
|
|
|
|
|
|
/* Start the temp capture/conversion */
|
2019-10-20 10:48:27 +00:00
|
|
|
reg = RD4(sc, CONTROL0);
|
2018-12-12 22:33:05 +00:00
|
|
|
reg &= ~CONTROL0_TSEN_RESET;
|
|
|
|
reg |= CONTROL0_TSEN_START | CONTROL0_TSEN_EN;
|
|
|
|
|
|
|
|
/* Sample every ~2ms */
|
|
|
|
reg |= CONTROL0_OSR_MAX << CONTROL0_OSR_SHIFT;
|
|
|
|
|
2019-10-20 10:48:27 +00:00
|
|
|
WR4(sc, CONTROL0, reg);
|
2018-12-12 22:33:05 +00:00
|
|
|
|
|
|
|
/* Since we just started the module wait for the sensor to be ready */
|
|
|
|
mv_thermal_wait_sensor(sc);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cp110_init(struct mv_thermal_softc *sc)
|
|
|
|
{
|
|
|
|
uint32_t reg;
|
|
|
|
|
2019-10-20 10:48:27 +00:00
|
|
|
reg = RD4(sc, CONTROL1);
|
2018-12-12 22:33:05 +00:00
|
|
|
reg &= (1 << 7);
|
|
|
|
reg |= (1 << 8);
|
2019-10-20 10:48:27 +00:00
|
|
|
WR4(sc, CONTROL1, reg);
|
2018-12-12 22:33:05 +00:00
|
|
|
|
|
|
|
/* Sample every ~2ms */
|
2019-10-20 10:48:27 +00:00
|
|
|
reg = RD4(sc, CONTROL0);
|
2018-12-12 22:33:05 +00:00
|
|
|
reg |= CONTROL0_OSR_MAX << CONTROL0_OSR_SHIFT;
|
2019-10-20 10:48:27 +00:00
|
|
|
WR4(sc, CONTROL0, reg);
|
2018-12-12 22:33:05 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mv_thermal_sysctl(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
struct mv_thermal_softc *sc;
|
|
|
|
device_t dev = arg1;
|
|
|
|
int sensor = arg2;
|
|
|
|
int val = 0;
|
|
|
|
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
mtx_lock(&(sc)->mtx);
|
|
|
|
|
|
|
|
if (mv_thermal_read_sensor(sc, sensor, &val) == 0) {
|
|
|
|
/* Convert to Kelvin */
|
|
|
|
val = val + 2732;
|
|
|
|
} else {
|
|
|
|
device_printf(dev, "Timeout waiting for sensor\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
mtx_unlock(&(sc)->mtx);
|
|
|
|
return sysctl_handle_opaque(oidp, &val, sizeof(val), req);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mv_thermal_probe(device_t dev)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!ofw_bus_status_okay(dev))
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
|
|
|
|
return (ENXIO);
|
|
|
|
|
|
|
|
device_set_desc(dev, "Marvell Thermal Sensor Controller");
|
|
|
|
return (BUS_PROBE_DEFAULT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mv_thermal_attach(device_t dev)
|
|
|
|
{
|
|
|
|
struct mv_thermal_softc *sc;
|
|
|
|
struct sysctl_ctx_list *ctx;
|
|
|
|
struct sysctl_oid_list *oid;
|
2019-10-20 10:48:27 +00:00
|
|
|
phandle_t node;
|
2018-12-12 22:33:05 +00:00
|
|
|
char name[255];
|
|
|
|
char desc[255];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
sc->dev = dev;
|
2019-10-20 10:48:27 +00:00
|
|
|
node = ofw_bus_get_node(dev);
|
2018-12-12 22:33:05 +00:00
|
|
|
|
2019-10-20 10:48:27 +00:00
|
|
|
sc->config = (struct mv_thermal_config *)
|
|
|
|
ofw_bus_search_compatible(dev, compat_data)->ocd_data;
|
2018-12-12 22:33:05 +00:00
|
|
|
|
|
|
|
mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
|
|
|
|
|
2019-10-20 10:48:27 +00:00
|
|
|
|
|
|
|
if (SYSCON_GET_HANDLE(sc->dev, &sc->syscon) != 0 ||
|
|
|
|
sc->syscon == NULL) {
|
|
|
|
device_printf(dev, "cannot get syscon for device\n");
|
2018-12-12 22:33:05 +00:00
|
|
|
return (ENXIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->cur_sensor = -1;
|
|
|
|
switch (sc->config->type) {
|
|
|
|
case MV_AP806:
|
|
|
|
ap806_init(sc);
|
|
|
|
break;
|
|
|
|
case MV_CP110:
|
|
|
|
cp110_init(sc);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = device_get_sysctl_ctx(dev);
|
|
|
|
oid = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
|
|
|
|
/* There is always at least one sensor */
|
|
|
|
SYSCTL_ADD_PROC(ctx, oid, OID_AUTO, "internal",
|
2020-02-26 14:26:36 +00:00
|
|
|
CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
|
2018-12-12 22:33:05 +00:00
|
|
|
dev, 0, mv_thermal_sysctl,
|
|
|
|
"IK",
|
|
|
|
"Internal Temperature");
|
|
|
|
|
|
|
|
for (i = 0; i < sc->config->ncpus; i++) {
|
|
|
|
snprintf(name, sizeof(name), "cpu%d", i);
|
|
|
|
snprintf(desc, sizeof(desc), "CPU%d Temperature", i);
|
|
|
|
SYSCTL_ADD_PROC(ctx, oid, OID_AUTO, name,
|
2020-02-26 14:26:36 +00:00
|
|
|
CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
|
2018-12-12 22:33:05 +00:00
|
|
|
dev, i + 1, mv_thermal_sysctl,
|
|
|
|
"IK",
|
|
|
|
desc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mv_thermal_detach(device_t dev)
|
|
|
|
{
|
|
|
|
struct mv_thermal_softc *sc;
|
|
|
|
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static device_method_t mv_thermal_methods[] = {
|
|
|
|
/* Device interface */
|
|
|
|
DEVMETHOD(device_probe, mv_thermal_probe),
|
|
|
|
DEVMETHOD(device_attach, mv_thermal_attach),
|
|
|
|
DEVMETHOD(device_detach, mv_thermal_detach),
|
|
|
|
|
|
|
|
DEVMETHOD_END
|
|
|
|
};
|
|
|
|
|
|
|
|
static devclass_t mv_thermal_devclass;
|
|
|
|
|
|
|
|
static driver_t mv_thermal_driver = {
|
|
|
|
"mv_thermal",
|
|
|
|
mv_thermal_methods,
|
|
|
|
sizeof(struct mv_thermal_softc),
|
|
|
|
};
|
|
|
|
|
|
|
|
DRIVER_MODULE(mv_thermal, simplebus, mv_thermal_driver,
|
|
|
|
mv_thermal_devclass, 0, 0);
|