summaryrefslogtreecommitdiff
path: root/sys/dev/fdt
diff options
context:
space:
mode:
authorMark Kettenis <kettenis@cvs.openbsd.org>2018-02-24 11:07:00 +0000
committerMark Kettenis <kettenis@cvs.openbsd.org>2018-02-24 11:07:00 +0000
commitd3cf5fd1188e12edf0452a08454568aca1518724 (patch)
tree50a2980bc49d1c63189d59d69e4eacaae5552c06 /sys/dev/fdt
parent77ac26abb8171d442ae7dc7873aa562c528874a2 (diff)
Add bcmtemp(4), a driver for the temperature sensor on the Raspberry Pi.
Diffstat (limited to 'sys/dev/fdt')
-rw-r--r--sys/dev/fdt/bcm2835_temp.c124
-rw-r--r--sys/dev/fdt/files.fdt6
2 files changed, 129 insertions, 1 deletions
diff --git a/sys/dev/fdt/bcm2835_temp.c b/sys/dev/fdt/bcm2835_temp.c
new file mode 100644
index 00000000000..c5aab990656
--- /dev/null
+++ b/sys/dev/fdt/bcm2835_temp.c
@@ -0,0 +1,124 @@
+/* $OpenBSD: bcm2835_temp.c,v 1.1 2018/02/24 11:06:59 kettenis Exp $ */
+/*
+ * Copyright (c) 2018 Mark Kettenis <kettenis@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <sys/sensors.h>
+
+#include <machine/bus.h>
+#include <machine/fdt.h>
+
+#include <dev/ofw/openfirm.h>
+#include <dev/ofw/fdt.h>
+
+/* Registers */
+#define TS_TSENSCTL 0x0000
+#define TS_TSENSSTAT 0x0004
+#define TS_TSENSSTAT_VALID (1 << 10)
+#define TS_TSENSSTAT_DATA(x) ((x) & 0x3ff)
+
+#define HREAD4(sc, reg) \
+ (bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
+#define HWRITE4(sc, reg, val) \
+ bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
+
+struct bcmtemp_softc {
+ struct device sc_dev;
+ bus_space_tag_t sc_iot;
+ bus_space_handle_t sc_ioh;
+
+ int32_t sc_slope;
+ int32_t sc_offset;
+
+ struct ksensor sc_sensor;
+ struct ksensordev sc_sensordev;
+};
+
+int bcmtemp_match(struct device *, void *, void *);
+void bcmtemp_attach(struct device *, struct device *, void *);
+
+struct cfattach bcmtemp_ca = {
+ sizeof (struct bcmtemp_softc), bcmtemp_match, bcmtemp_attach
+};
+
+struct cfdriver bcmtemp_cd = {
+ NULL, "bcmtemp", DV_DULL
+};
+
+void bcmtemp_refresh_sensors(void *);
+
+int
+bcmtemp_match(struct device *parent, void *match, void *aux)
+{
+ struct fdt_attach_args *faa = aux;
+
+ return (OF_is_compatible(faa->fa_node, "brcm,bcm2836-thermal") ||
+ OF_is_compatible(faa->fa_node, "brcm,bcm2837-thermal"));
+}
+
+void
+bcmtemp_attach(struct device *parent, struct device *self, void *aux)
+{
+ struct bcmtemp_softc *sc = (struct bcmtemp_softc *)self;
+ struct fdt_attach_args *faa = aux;
+
+ if (faa->fa_nreg < 1) {
+ printf(": no registers\n");
+ return;
+ }
+
+ sc->sc_iot = faa->fa_iot;
+ if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
+ faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
+ printf(": can't map registers\n");
+ return;
+ }
+
+ printf("\n");
+
+ /* XXX fetch these from /thermal-zones. */
+ sc->sc_slope = -538;
+ if (OF_is_compatible(faa->fa_node, "brcm,bcm2836-thermal"))
+ sc->sc_offset = 407000;
+ else
+ sc->sc_offset = 412000;
+
+ /* Register sensors. */
+ strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
+ sizeof(sc->sc_sensordev.xname));
+ sc->sc_sensor.type = SENSOR_TEMP;
+ sc->sc_sensor.flags = SENSOR_FINVALID;
+ sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
+ sensordev_install(&sc->sc_sensordev);
+ sensor_task_register(sc, bcmtemp_refresh_sensors, 5);
+}
+
+void
+bcmtemp_refresh_sensors(void *arg)
+{
+ struct bcmtemp_softc *sc = arg;
+ int32_t code, temp;
+
+ code = HREAD4(sc, TS_TSENSSTAT);
+ temp = sc->sc_offset + TS_TSENSSTAT_DATA(code) * sc->sc_slope;
+ sc->sc_sensor.value = 273150000 + 1000 * temp;
+ if (code & TS_TSENSSTAT_VALID)
+ sc->sc_sensor.flags &= ~SENSOR_FINVALID;
+ else
+ sc->sc_sensor.flags |= SENSOR_FINVALID;
+}
diff --git a/sys/dev/fdt/files.fdt b/sys/dev/fdt/files.fdt
index 65286f96bd3..d5d71a7dd31 100644
--- a/sys/dev/fdt/files.fdt
+++ b/sys/dev/fdt/files.fdt
@@ -1,4 +1,4 @@
-# $OpenBSD: files.fdt,v 1.37 2018/01/17 10:17:33 kettenis Exp $
+# $OpenBSD: files.fdt,v 1.38 2018/02/24 11:06:59 kettenis Exp $
#
# Config file and device description for machine-independent FDT code.
# Included by ports that need it.
@@ -57,6 +57,10 @@ device bcmdog
attach bcmdog at fdt
file dev/fdt/bcm2835_dog.c bcmdog
+device bcmtemp
+attach bcmtemp at fdt
+file dev/fdt/bcm2835_temp.c bcmdog
+
device exrtc
attach exrtc at fdt
file dev/fdt/exrtc.c exrtc