Page MenuHomeFreeBSD

D60989.1795168850.diff
No OneTemporary

Authored By
Unknown
Size
27 KB
Referenced Files
None
Subscribers
None

D60989.1795168850.diff

diff --git a/share/man/man4/Makefile b/share/man/man4/Makefile
--- a/share/man/man4/Makefile
+++ b/share/man/man4/Makefile
@@ -526,6 +526,7 @@
rights.4 \
rl.4 \
rndtest.4 \
+ rng90.4 \
route.4 \
rtnetlink.4 \
rtsx.4 \
diff --git a/share/man/man4/rng90.4 b/share/man/man4/rng90.4
new file mode 100644
--- /dev/null
+++ b/share/man/man4/rng90.4
@@ -0,0 +1,116 @@
+.\" SPDX-License-Identifier: BSD-2-Clause
+.\" Copyright (c) 2026 Patrick Tudor.
+.Dd November 11, 2026
+.Dt RNG90 4
+.Os
+.Sh NAME
+.Nm rng90
+.Nd Microchip RNG90 I2C random number generator
+.Sh SYNOPSIS
+.Cd "kldload rng90"
+.Pp
+For a hinted device:
+.Cd "hint.rng90.0.at=\(dqiicbus0\(dq"
+.Cd "hint.rng90.0.addr=0x80"
+.Cd "hint.rng90.0.feed=1"
+.Sh DESCRIPTION
+The
+.Nm
+driver provides access to a Microchip RNG90 at I2C address 0x40.
+The address in
+.Xr device.hints 5
+is shifted left by one bit; device-tree nodes instead use
+.Li "compatible = \(dqmicrochip,rng90\(dq"
+and
+.Li "reg = <0x40>" .
+No automatic address scan is performed.
+.Pp
+The root-only
+.Pa /dev/rng90N
+device returns at most 32 bytes per blocking read.
+Wake, identity, frame length, status and CRC are verified before returning data.
+Nonblocking reads return
+.Er EAGAIN .
+Raw-read blocks are not also submitted to the kernel random pool.
+.Pp
+The
+.Va dev.rng90.N.feed
+sysctl enables asynchronous collection for the kernel random subsystem.
+It defaults to zero; the hint
+.Va feed=1
+turns it on at attach for a host whose operator has validated the device.
+Collection takes one block about every 100 ms and backs off to one attempt
+per second after a failed one.
+Only verified device output is submitted.
+The module registers a dedicated asynchronous
+.Li PURE_RNG90
+source shared by all attached RNG90 devices.
+Each 32-byte block is divided into four 8-byte harvest events so the random
+framework is offered every byte without reducing an oversized event to a hash.
+The periodic task does not rearm while collection is disabled, suspended, or
+latched after device health failures.
+Applications should continue to use the normal system random interfaces.
+.Pp
+The read-only sysctls
+.Va raw_blocks ,
+.Va submitted_blocks ,
+.Va errors ,
+.Va health_failures ,
+.Va last_status
+and
+.Va last_error
+report per-device activity.
+.Va submitted_blocks
+counts blocks offered to the random framework, not entropy credited or
+retained by it.
+The last error uses the protocol-result enumeration in
+.Pa sys/dev/rng90/rng90_protocol.h .
+.Pp
+Three health or self-test failures without an intervening verified block
+latch the device until detach and reattach.
+Suspend and feed changes do not clear this latch.
+.Sh FILES
+.Bl -tag -width /dev/rng90N
+.It Pa /dev/rng90N
+Raw device output, accessible to root.
+.El
+.Sh EXAMPLES
+An RNG90 behind a Silicon Labs CP2112 USB bridge, in
+.Pa /boot/loader.conf :
+.Bd -literal -offset indent
+hw.usb.quirk.0="0x10c4 0xea90 0x0000 0xffff UQ_HID_IGNORE"
+cp2112_load="YES"
+rng90_load="YES"
+hint.rng90.0.at="iicbus0"
+hint.rng90.0.addr="0x80"
+hint.rng90.0.feed="1"
+.Ed
+.Pp
+The quirk is required:
+.Xr usbhid 4
+is enabled by default and probes ahead of
+.Xr cp2112 4 ,
+so without it the bridge never provides an I2C bus.
+Check the bus number with
+.Xr devinfo 8
+before trusting
+.Li iicbus0 .
+.Sh SEE ALSO
+.Xr cp2112 4 ,
+.Xr iicbus 4 ,
+.Xr random 4 ,
+.Xr device.hints 5
+.Sh AUTHORS
+.An Patrick Tudor
+.Sh CAVEATS
+The driver has run on the CP2112 path; the device-tree path is compiled but
+untested.
+An individual controller transfer can outlast the protocol polling deadline.
+The driver does not take ownership of the bus for its transfers:
+.Xr iicbus 4
+marks the requesting device busy, a busy device refuses to detach, and the USB
+stack panics when a driver under an unplugged bridge will not detach.
+Every transfer is a single message, which the controller executes atomically.
+Detach sends nothing to the chip and stops any transfer still retrying:
+a controller that does not return from a transfer after hardware removal can
+still stall detach.
diff --git a/sys/conf/NOTES b/sys/conf/NOTES
--- a/sys/conf/NOTES
+++ b/sys/conf/NOTES
@@ -2306,6 +2306,7 @@
device isl12xx # Intersil ISL12xx RTC
device lm75 # LM75 compatible temperature sensor
device nxprtc # NXP RTCs: PCA/PFC212x PCA/PCF85xx
+device rng90 # Microchip RNG90 random number generator
device rtc8583 # Epson RTC-8583
device s35390a # Seiko Instruments S-35390A RTC
device sy8106a # Silergy Corp. SY8106A buck regulator
diff --git a/sys/conf/files b/sys/conf/files
--- a/sys/conf/files
+++ b/sys/conf/files
@@ -2925,6 +2925,8 @@
dev/rge/if_rge_sysctl.c optional rge
dev/rl/if_rl.c optional rl pci
dev/rndtest/rndtest.c optional rndtest
+dev/rng90/rng90.c optional rng90 iicbus
+dev/rng90/rng90_protocol.c optional rng90 iicbus
#
dev/rtsx/rtsx.c optional rtsx pci
#
diff --git a/sys/dev/random/fenestrasX/fx_pool.c b/sys/dev/random/fenestrasX/fx_pool.c
--- a/sys/dev/random/fenestrasX/fx_pool.c
+++ b/sys/dev/random/fenestrasX/fx_pool.c
@@ -212,6 +212,9 @@
[RANDOM_PURE_GLXSB] = {
.entc_cls = &fxrng_hi_push,
},
+ [RANDOM_PURE_RNG90] = {
+ .entc_cls = &fxrng_hi_push,
+ },
};
CTASSERT(nitems(fxrng_ent_char) == ENTROPYSOURCE);
diff --git a/sys/dev/random/random_harvestq.c b/sys/dev/random/random_harvestq.c
--- a/sys/dev/random/random_harvestq.c
+++ b/sys/dev/random/random_harvestq.c
@@ -677,6 +677,7 @@
[RANDOM_PURE_ARM_TRNG] = "PURE_ARM_TRNG",
[RANDOM_PURE_SAFE] = "PURE_SAFE",
[RANDOM_PURE_GLXSB] = "PURE_GLXSB",
+ [RANDOM_PURE_RNG90] = "PURE_RNG90",
/* "ENTROPYSOURCE" */
};
CTASSERT(nitems(random_source_descr) == ENTROPYSOURCE);
diff --git a/sys/dev/rng90/rng90.c b/sys/dev/rng90/rng90.c
new file mode 100644
--- /dev/null
+++ b/sys/dev/rng90/rng90.c
@@ -0,0 +1,501 @@
+/*-
+ * Copyright (c) 2026 Patrick Tudor <ptudor@ptudor.net>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#include "opt_platform.h"
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/conf.h>
+#include <sys/fcntl.h>
+#include <sys/kernel.h>
+#include <sys/lock.h>
+#include <sys/malloc.h>
+#include <sys/module.h>
+#include <sys/proc.h>
+#include <sys/random.h>
+#include <sys/sx.h>
+#include <sys/sysctl.h>
+#include <sys/systm.h>
+#include <sys/taskqueue.h>
+#include <sys/uio.h>
+
+#include <dev/iicbus/iicbus.h>
+#include <dev/iicbus/iiconf.h>
+#include <dev/random/randomdev.h>
+#ifdef FDT
+#include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
+#endif
+
+#include "rng90_protocol.h"
+
+struct rng90_softc {
+ device_t dev;
+ struct sx lock;
+ struct r90_device chip;
+ struct cdev *cdev;
+ struct taskqueue *queue;
+ struct timeout_task poll;
+ struct sysctl_ctx_list sysctls;
+ bool dying, suspended, used; /* protected by lock */
+ bool bus_failed; /* controller error ended this operation */
+ sbintime_t quiet_until; /* no new operation before this time */
+ int feed;
+ struct {
+ uint64_t reads, submitted, errors;
+ enum r90_result last_error;
+ } stats;
+};
+
+/* One asynchronous source registration for the module, including all units. */
+static const struct random_source rng90_source = {
+ .rs_ident = "Microchip RNG90",
+ .rs_source = RANDOM_PURE_RNG90,
+ .rs_read = NULL,
+};
+
+/* random_harvest_queue() preserves at most two uint32_t values per event. */
+#define RNG90_HARVEST_SIZE (2 * sizeof(uint32_t))
+_Static_assert(R90_BLOCK_SIZE % RNG90_HARVEST_SIZE == 0,
+ "RNG90 block must contain whole harvest events");
+
+static enum r90_result
+rng90_transfer(void *arg, bool read, uint8_t *data, size_t length)
+{
+ struct rng90_softc *sc = arg;
+ struct iic_msg message;
+ int error;
+
+ sx_assert(&sc->lock, SA_XLOCKED);
+ /* Reject transfers after shutdown or a controller error. */
+ if (sc->dying || sc->bus_failed)
+ return (R90_IO);
+ sc->used = true;
+ message.slave = iicbus_get_addr(sc->dev);
+ message.flags = read ? IIC_M_RD : IIC_M_WR;
+ message.len = (uint16_t)length;
+ message.buf = data;
+ /*
+ * One message, one STOP, and no bus ownership: iicbus_request_bus()
+ * marks the requesting device busy, a busy device makes device_detach()
+ * fail, and the USB stack panics when a driver under an unplugged
+ * bridge will not detach ("A USB driver would not detach", with a
+ * transfer in flight). Each call here is a single
+ * message, which the controller executes atomically, so sharing the
+ * bus with other targets loses nothing; the driver never needs a
+ * sequence that spans calls.
+ */
+ error = iicbus_transfer(sc->dev, &message, 1);
+ if (error == IIC_NOERR)
+ return (message.len == length ? R90_OK : R90_FRAME);
+ if (error == IIC_ENOACK)
+ return (R90_NACK);
+ /*
+ * Any other controller error ends this operation and quiets the
+ * device for a second. The protocol engine would otherwise retry at
+ * once, and a transfer submitted to a bridge that has just been
+ * unplugged, before the USB stack has detached it, never completes in
+ * cp2112(4): the poll then sits in its cv_wait holding this lock, and
+ * detach waits on the poll with the USB enumeration lock held. The one
+ * transfer in flight at the
+ * disconnect does complete, with an error, so this is the only
+ * transfer the driver ever has outstanding on a vanished device. A
+ * second later a gone device has been detached and a live one is
+ * woken and identified afresh.
+ */
+ sc->bus_failed = true;
+ sc->quiet_until = sbinuptime() + SBT_1S;
+ return (error == IIC_ETIMEOUT ? R90_TIMEOUT : R90_IO);
+}
+
+static void
+rng90_delay(void *arg, unsigned int us)
+{
+ struct rng90_softc *sc = arg;
+
+ sx_assert(&sc->lock, SA_XLOCKED);
+ /*
+ * When the transport refuses transfers, retries fail fast. Skip their
+ * settling delays too.
+ */
+ if (sc->dying || sc->bus_failed)
+ return;
+ pause_sbt("rng90io", (sbintime_t)us * SBT_1US, 0, C_PREL(1));
+}
+
+static uint64_t
+rng90_now(void *arg __unused)
+{
+ sbintime_t t = sbinuptime();
+
+ return ((uint64_t)(t / SBT_1S) * 1000000 +
+ (uint64_t)(t % SBT_1S) * 1000000 / SBT_1S);
+}
+
+static int
+rng90_get_block(struct rng90_softc *sc, uint8_t *block)
+{
+ enum r90_result error;
+
+ sx_assert(&sc->lock, SA_XLOCKED);
+ if (sc->dying)
+ return (ENXIO);
+ if (sc->suspended)
+ return (EHOSTDOWN);
+ if (sbinuptime() < sc->quiet_until) {
+ sc->stats.errors++;
+ return (EIO);
+ }
+ sc->bus_failed = false;
+ error = r90_read_block(&sc->chip, block);
+ sc->stats.last_error = error;
+ if (error == R90_OK)
+ return (0);
+ sc->stats.errors++;
+ if (error == R90_IDENTITY)
+ return (ENXIO);
+ if (error == R90_TIMEOUT)
+ return (ETIMEDOUT);
+ return (EIO);
+}
+
+static void
+rng90_harvest(const uint8_t block[R90_BLOCK_SIZE])
+{
+ size_t offset;
+
+ for (offset = 0; offset < R90_BLOCK_SIZE;
+ offset += RNG90_HARVEST_SIZE)
+ random_harvest_queue(block + offset, RNG90_HARVEST_SIZE,
+ RANDOM_PURE_RNG90);
+}
+
+static void
+rng90_poll(void *arg, int pending __unused)
+{
+ struct rng90_softc *sc = arg;
+ uint8_t block[R90_BLOCK_SIZE];
+ bool failed = false;
+
+ sx_xlock(&sc->lock);
+ if (!sc->dying && !sc->suspended && sc->feed &&
+ sc->chip.health_failures < R90_HEALTH_LIMIT) {
+ if (rng90_get_block(sc, block) == 0) {
+ rng90_harvest(block);
+ sc->stats.submitted++;
+ } else
+ failed = true;
+ }
+ explicit_bzero(block, sizeof(block));
+ /* Do not leave a periodic wakeup when collection cannot run. */
+ if (!sc->dying && !sc->suspended && sc->feed &&
+ sc->chip.health_failures < R90_HEALTH_LIMIT)
+ taskqueue_enqueue_timeout(sc->queue, &sc->poll,
+ failed ? hz : MAX(1, hz / 10));
+ sx_xunlock(&sc->lock);
+}
+
+static int
+rng90_read(struct cdev *dev, struct uio *uio, int flags)
+{
+ struct rng90_softc *sc = dev->si_drv1;
+ uint8_t block[R90_BLOCK_SIZE];
+ int error;
+
+ if (uio->uio_resid == 0)
+ return (0);
+ /* No cache: nonblocking requests start no I2C operation. */
+ if (flags & O_NONBLOCK)
+ return (EAGAIN);
+ error = sx_xlock_sig(&sc->lock);
+ if (error != 0)
+ return (error);
+ error = rng90_get_block(sc, block);
+ if (error == 0) {
+ error = uiomove(block,
+ MIN((size_t)uio->uio_resid, sizeof(block)), uio);
+ if (error == 0)
+ sc->stats.reads++;
+ }
+ /* Raw reads are never also submitted to the entropy pool. */
+ explicit_bzero(block, sizeof(block));
+ sx_xunlock(&sc->lock);
+ return (error);
+}
+
+static struct cdevsw rng90_cdevsw = {
+ .d_version = D_VERSION,
+ .d_read = rng90_read,
+ .d_name = "rng90",
+};
+
+/* Caller holds the lock. */
+static int
+rng90_set_feed(struct rng90_softc *sc, int value)
+{
+ sx_assert(&sc->lock, SA_XLOCKED);
+ if (sc->dying)
+ return (ENXIO);
+ if (value && sc->chip.health_failures >= R90_HEALTH_LIMIT)
+ return (EIO);
+ if (value && !sc->feed) {
+ sc->feed = 1;
+ if (!sc->suspended)
+ taskqueue_enqueue_timeout(sc->queue, &sc->poll,
+ MAX(1, hz / 10));
+ } else if (!value && sc->feed) {
+ sc->feed = 0;
+ if (sc->used && !sc->suspended)
+ (void)r90_sleep(&sc->chip);
+ }
+ return (0);
+}
+
+static int
+rng90_feed_sysctl(SYSCTL_HANDLER_ARGS)
+{
+ struct rng90_softc *sc = arg1;
+ int error, value;
+
+ (void)arg2;
+ sx_xlock(&sc->lock);
+ value = sc->feed;
+ sx_xunlock(&sc->lock);
+ error = sysctl_handle_int(oidp, &value, 0, req);
+ if (error != 0 || req->newptr == NULL)
+ return (error);
+ if (value != 0 && value != 1)
+ return (EINVAL);
+ sx_xlock(&sc->lock);
+ error = rng90_set_feed(sc, value);
+ sx_xunlock(&sc->lock);
+ return (error);
+}
+
+static int
+rng90_stat_sysctl(SYSCTL_HANDLER_ARGS)
+{
+ struct rng90_softc *sc = arg1;
+ uint64_t value;
+
+ sx_xlock(&sc->lock);
+ switch (arg2) {
+ case 0:
+ value = sc->stats.reads;
+ break;
+ case 1:
+ value = sc->stats.submitted;
+ break;
+ case 2:
+ value = sc->stats.errors;
+ break;
+ case 3:
+ value = sc->chip.health_failures;
+ break;
+ case 4:
+ value = sc->chip.last_status;
+ break;
+ default:
+ value = sc->stats.last_error;
+ break;
+ }
+ sx_xunlock(&sc->lock);
+ return (sysctl_handle_64(oidp, &value, 0, req));
+}
+
+static int
+rng90_probe(device_t dev)
+{
+#ifdef FDT
+ if (ofw_bus_get_node(dev) > 0) {
+ if (!ofw_bus_status_okay(dev) ||
+ !ofw_bus_is_compatible(dev, "microchip,rng90"))
+ return (ENXIO);
+ } else
+#endif
+ {
+ /* A hint must name rng90; never probe arbitrary I2C chips. */
+ if (device_get_name(dev) == NULL ||
+ strcmp(device_get_name(dev), "rng90") != 0)
+ return (ENXIO);
+ }
+ /* Iicbus stores the address shifted left, unlike a DT reg cell. */
+ if (iicbus_get_addr(dev) != (R90_ADDRESS << 1))
+ return (ENXIO);
+ device_set_desc(dev, "Microchip RNG90 random number generator");
+ return (BUS_PROBE_DEFAULT);
+}
+
+static int
+rng90_attach(device_t dev)
+{
+ struct rng90_softc *sc = device_get_softc(dev);
+ struct make_dev_args args;
+ struct sysctl_oid_list *children;
+ const struct r90_transport io = {
+ .cookie = sc,
+ .transfer = rng90_transfer,
+ .delay_us = rng90_delay,
+ .now_us = rng90_now,
+ };
+ static const struct {
+ const char *name;
+ const char *description;
+ } stats[] = {
+ { "raw_blocks", "Successful raw device reads" },
+ { "submitted_blocks",
+ "Verified 32-byte blocks offered to the random framework" },
+ { "errors",
+ "Failed block requests, including quiet-period refusals" },
+ { "health_failures",
+ "Health or self-test failures since the last "
+ "verified block" },
+ { "last_status", "Last device status byte" },
+ { "last_error", "Last protocol result (enum r90_result)" },
+ };
+ unsigned int i;
+ int error, feed;
+
+ sc->dev = dev;
+ sx_init(&sc->lock, "rng90 protocol");
+ r90_init(&sc->chip, &io);
+ sc->queue = taskqueue_create("rng90", M_WAITOK,
+ taskqueue_thread_enqueue, &sc->queue);
+ TIMEOUT_TASK_INIT(sc->queue, &sc->poll, 0, rng90_poll, sc);
+ error = taskqueue_start_threads(&sc->queue, 1, PI_SOFT, "%s",
+ device_get_nameunit(dev));
+ if (error != 0)
+ goto fail_queue;
+ make_dev_args_init(&args);
+ args.mda_devsw = &rng90_cdevsw;
+ args.mda_uid = UID_ROOT;
+ args.mda_gid = GID_WHEEL;
+ args.mda_mode = 0600;
+ args.mda_si_drv1 = sc;
+ error = make_dev_s(&args, &sc->cdev, "rng90%d", device_get_unit(dev));
+ if (error != 0)
+ goto fail_queue;
+ sysctl_ctx_init(&sc->sysctls);
+ children = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
+ SYSCTL_ADD_PROC(&sc->sysctls, children, OID_AUTO, "feed",
+ CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
+ rng90_feed_sysctl, "I", "Enable RNG90 entropy collection");
+ for (i = 0; i < nitems(stats); i++)
+ SYSCTL_ADD_PROC(&sc->sysctls, children, OID_AUTO, stats[i].name,
+ CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE, sc, i,
+ rng90_stat_sysctl, "QU", stats[i].description);
+ /*
+ * Enable feed (via hint.rng90.N.feed="1") to begin entropy collection
+ * at attach.
+ */
+ feed = 0;
+ if (resource_int_value(device_get_name(dev), device_get_unit(dev),
+ "feed", &feed) == 0 && feed != 0) {
+ sx_xlock(&sc->lock);
+ error = rng90_set_feed(sc, 1);
+ sx_xunlock(&sc->lock);
+ if (error != 0)
+ device_printf(dev, "feed hint not applied: error %d\n",
+ error);
+ }
+ device_printf(dev, "raw device ready; entropy feed %s\n",
+ sc->feed ? "enabled by hint" : "disabled");
+ return (0);
+
+fail_queue:
+ taskqueue_free(sc->queue);
+ sx_destroy(&sc->lock);
+ return (error);
+}
+
+static int
+rng90_suspend(device_t dev)
+{
+ struct rng90_softc *sc = device_get_softc(dev);
+
+ sx_xlock(&sc->lock);
+ sc->suspended = true;
+ if (sc->used)
+ (void)r90_sleep(&sc->chip);
+ sx_xunlock(&sc->lock);
+ return (0);
+}
+
+static int
+rng90_resume(device_t dev)
+{
+ struct rng90_softc *sc = device_get_softc(dev);
+
+ sx_xlock(&sc->lock);
+ r90_forget(&sc->chip);
+ sc->suspended = false;
+ if (sc->feed && sc->chip.health_failures < R90_HEALTH_LIMIT)
+ taskqueue_enqueue_timeout(sc->queue, &sc->poll,
+ MAX(1, hz / 10));
+ sx_xunlock(&sc->lock);
+ return (0);
+}
+
+static int
+rng90_detach(device_t dev)
+{
+ struct rng90_softc *sc = device_get_softc(dev);
+
+ sx_xlock(&sc->lock);
+ sc->dying = true;
+ sx_xunlock(&sc->lock);
+ while (taskqueue_cancel_timeout(sc->queue, &sc->poll, NULL) != 0)
+ taskqueue_drain_timeout(sc->queue, &sc->poll);
+ taskqueue_free(sc->queue);
+ /* Drain control and raw-read callers before destroying their lock. */
+ sysctl_ctx_free(&sc->sysctls);
+ destroy_dev(sc->cdev);
+ sx_destroy(&sc->lock);
+ return (0);
+}
+
+static device_method_t rng90_methods[] = {
+ DEVMETHOD(device_probe, rng90_probe),
+ DEVMETHOD(device_attach, rng90_attach),
+ DEVMETHOD(device_detach, rng90_detach),
+ DEVMETHOD(device_suspend, rng90_suspend),
+ DEVMETHOD(device_resume, rng90_resume),
+ DEVMETHOD(device_shutdown, rng90_suspend),
+ DEVMETHOD_END
+};
+
+static driver_t rng90_driver = {
+ .name = "rng90",
+ .methods = rng90_methods,
+ .size = sizeof(struct rng90_softc),
+};
+
+static int
+rng90_modevent(module_t module __unused, int event, void *arg __unused)
+{
+ switch (event) {
+ case MOD_LOAD:
+ random_source_register(&rng90_source);
+ break;
+ case MOD_UNLOAD:
+ random_source_deregister(&rng90_source);
+ break;
+ default:
+ break;
+ }
+ return (0);
+}
+
+DRIVER_MODULE(rng90, iicbus, rng90_driver, rng90_modevent, NULL);
+MODULE_VERSION(rng90, 1);
+MODULE_DEPEND(rng90, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
+MODULE_DEPEND(rng90, random_harvestq, 1, 1, 1);
+#ifdef FDT
+static const struct ofw_compat_data rng90_compat[] = {
+ { "microchip,rng90", 1 },
+ { NULL, 0 }
+};
+IICBUS_FDT_PNP_INFO(rng90_compat);
+#endif
diff --git a/sys/dev/rng90/rng90_protocol.h b/sys/dev/rng90/rng90_protocol.h
new file mode 100644
--- /dev/null
+++ b/sys/dev/rng90/rng90_protocol.h
@@ -0,0 +1,52 @@
+/*-
+ * Copyright (c) 2026 Patrick Tudor <ptudor@ptudor.net>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#ifndef RNG90_PROTOCOL_H
+#define RNG90_PROTOCOL_H
+
+#ifdef _KERNEL
+#include <sys/types.h>
+#else
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#endif
+
+#define R90_BLOCK_SIZE 32
+#define R90_ADDRESS 0x40
+#define R90_HEALTH_LIMIT 3
+
+enum r90_result {
+ R90_OK, R90_NACK, R90_IO, R90_TIMEOUT, R90_FRAME,
+ R90_STATUS, R90_HEALTH, R90_IDENTITY, R90_LATCHED, R90_RESYNC
+};
+
+/* Each transfer must finish with STOP. OK means the entire buffer moved. */
+struct r90_transport {
+ void *cookie;
+ enum r90_result (*transfer)(void *, bool, uint8_t *, size_t);
+ void (*delay_us)(void *, unsigned int);
+ uint64_t (*now_us)(void *);
+};
+
+enum r90_power { R90_UNKNOWN, R90_ASLEEP, R90_AWAKE };
+struct r90_device {
+ struct r90_transport io;
+ enum r90_power power;
+ unsigned int health_failures;
+ bool first;
+ uint8_t last_status;
+};
+
+void r90_init(struct r90_device *, const struct r90_transport *);
+void r90_forget(struct r90_device *);
+enum r90_result r90_identify(struct r90_device *);
+enum r90_result r90_sleep(struct r90_device *);
+/* Output remains untouched unless a complete, verified block is available. */
+enum r90_result r90_read_block(struct r90_device *, uint8_t [R90_BLOCK_SIZE]);
+uint16_t r90_crc(const uint8_t *, size_t);
+void r90_erase(void *, size_t);
+
+#endif
diff --git a/sys/dev/rng90/rng90_protocol.c b/sys/dev/rng90/rng90_protocol.c
new file mode 100644
--- /dev/null
+++ b/sys/dev/rng90/rng90_protocol.c
@@ -0,0 +1,253 @@
+/*-
+ * Copyright (c) 2026 Patrick Tudor <ptudor@ptudor.net>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+/* Microchip DS40002499A, sections 3.2-3.4, 5.2-5.6 and 6.1-6.2. */
+#ifdef _KERNEL
+#include <sys/param.h>
+#include <sys/systm.h>
+#else
+#include <string.h>
+#endif
+#include "rng90_protocol.h"
+
+#define R90_SETTLE_US 92000
+
+void
+r90_erase(void *buffer, size_t length)
+{
+ volatile uint8_t *p = buffer;
+
+ while (length-- != 0)
+ *p++ = 0;
+}
+
+uint16_t
+r90_crc(const uint8_t *bytes, size_t length)
+{
+ uint16_t crc = 0;
+ unsigned int bit, feedback;
+
+ while (length-- != 0) {
+ for (bit = 0; bit < 8; bit++) {
+ feedback = ((crc >> 15) ^ (*bytes >> bit)) & 1;
+ crc = (uint16_t)(crc << 1);
+ if (feedback != 0)
+ crc ^= 0x8005;
+ }
+ bytes++;
+ }
+ return (crc);
+}
+
+void
+r90_forget(struct r90_device *d)
+{
+ d->power = R90_UNKNOWN;
+ d->first = true;
+}
+
+void
+r90_init(struct r90_device *d, const struct r90_transport *io)
+{
+ memset(d, 0, sizeof(*d));
+ d->io = *io;
+ r90_forget(d);
+}
+
+static enum r90_result
+word(struct r90_device *d, uint8_t value)
+{
+ return (d->io.transfer(d->io.cookie, false, &value, 1));
+}
+
+static enum r90_result
+status(struct r90_device *d, uint8_t value)
+{
+ d->last_status = value;
+ if (value == 0x07 || value == 0x08) {
+ if (d->health_failures < R90_HEALTH_LIMIT)
+ d->health_failures++;
+ return (R90_HEALTH);
+ }
+ if (value == 0x11 || value == 0xff)
+ return (R90_RESYNC);
+ /* Even a success status is not a Random result. */
+ return (R90_STATUS);
+}
+
+static enum r90_result
+receive(struct r90_device *d, uint8_t *reply, size_t size)
+{
+ enum r90_result error;
+ size_t count;
+ uint16_t crc;
+
+ memset(reply, 0, size);
+ error = d->io.transfer(d->io.cookie, true, reply, size);
+ if (error != R90_OK)
+ return (error);
+ count = reply[0];
+ if (count != size && count != 4)
+ return (R90_FRAME);
+ crc = (uint16_t)(reply[count - 2] | (reply[count - 1] << 8));
+ if (r90_crc(reply, count - 2) != crc)
+ return (R90_FRAME);
+ if (count != size)
+ return (status(d, reply[1]));
+ return (R90_OK);
+}
+
+static enum r90_result
+command(struct r90_device *d, uint8_t opcode, uint8_t *reply, size_t size,
+ unsigned int initial_us, unsigned int limit_us)
+{
+ uint8_t packet[28] = { 0 };
+ uint16_t crc;
+ size_t count;
+ uint64_t started;
+ enum r90_result error;
+
+ error = word(d, 0x00);
+ if (error != R90_OK)
+ return (error);
+ count = opcode == 0x16 ? 27 : 7;
+ packet[0] = 0x03;
+ packet[1] = (uint8_t)count;
+ packet[2] = opcode;
+ /* Param1/2 and the required 20 Random filler bytes are all zero. */
+ crc = r90_crc(packet + 1, count - 2);
+ packet[count - 1] = (uint8_t)crc;
+ packet[count] = (uint8_t)(crc >> 8);
+ error = d->io.transfer(d->io.cookie, false, packet, count + 1);
+ r90_erase(packet, sizeof(packet));
+ if (error != R90_OK)
+ return (error);
+ started = d->io.now_us(d->io.cookie);
+ d->io.delay_us(d->io.cookie, initial_us);
+ for (;;) {
+ error = receive(d, reply, size);
+ if (error != R90_NACK)
+ return (error);
+ if (d->io.now_us(d->io.cookie) - started >= limit_us)
+ return (R90_TIMEOUT);
+ d->io.delay_us(d->io.cookie, 1000);
+ }
+}
+
+static enum r90_result
+wake(struct r90_device *d)
+{
+ uint8_t reply[4];
+ enum r90_result error;
+
+ /* An asleep RNG90 wakes on its own address, not a general call. */
+ error = word(d, 0x00);
+ if (error != R90_OK && error != R90_NACK)
+ return (error);
+ d->io.delay_us(d->io.cookie, 2000);
+ /* Acknowledged Sleep discards a stale command/result before wake. */
+ error = word(d, 0x01);
+ if (error != R90_OK)
+ return (error);
+ error = word(d, 0x00);
+ if (error != R90_OK && error != R90_NACK)
+ return (error);
+ d->io.delay_us(d->io.cookie, 2000);
+ error = receive(d, reply, sizeof(reply));
+ if (error == R90_OK && reply[1] != 0x11)
+ error = status(d, reply[1]);
+ r90_erase(reply, sizeof(reply));
+ return (error);
+}
+
+static bool
+retryable(enum r90_result error)
+{
+ return (error == R90_NACK || error == R90_IO || error == R90_TIMEOUT ||
+ error == R90_FRAME || error == R90_RESYNC);
+}
+
+enum r90_result
+r90_identify(struct r90_device *d)
+{
+ uint8_t reply[7];
+ enum r90_result error = R90_IO;
+ unsigned int attempt;
+
+ if (d->health_failures >= R90_HEALTH_LIMIT)
+ return (R90_LATCHED);
+ if (d->power == R90_AWAKE)
+ return (R90_OK);
+ for (attempt = 0; attempt < 2; attempt++) {
+ if (attempt != 0)
+ d->io.delay_us(d->io.cookie, R90_SETTLE_US);
+ error = wake(d);
+ if (!retryable(error))
+ break;
+ }
+ if (error == R90_OK) {
+ error = command(d, 0x30, reply, sizeof(reply), 1000, 10000);
+ if (error == R90_OK && reply[2] != 0xd0)
+ error = R90_IDENTITY;
+ }
+ r90_erase(reply, sizeof(reply));
+ if (error == R90_OK) {
+ d->power = R90_AWAKE;
+ d->first = true;
+ } else {
+ r90_forget(d);
+ }
+ return (error);
+}
+
+enum r90_result
+r90_sleep(struct r90_device *d)
+{
+ enum r90_result error = R90_OK;
+ unsigned int attempt;
+
+ if (d->power != R90_ASLEEP) {
+ for (attempt = 0; attempt < 2; attempt++) {
+ if (attempt != 0)
+ d->io.delay_us(d->io.cookie, R90_SETTLE_US);
+ error = word(d, 0x01);
+ if (error == R90_OK)
+ break;
+ }
+ }
+ d->power = error == R90_OK ? R90_ASLEEP : R90_UNKNOWN;
+ d->first = true;
+ return (error);
+}
+
+enum r90_result
+r90_read_block(struct r90_device *d, uint8_t output[R90_BLOCK_SIZE])
+{
+ uint8_t reply[35];
+ enum r90_result error;
+ unsigned int attempt;
+
+ error = r90_identify(d);
+ if (error != R90_OK)
+ return (error);
+ for (attempt = 0; attempt < 2; attempt++) {
+ error = command(d, 0x16, reply, sizeof(reply),
+ d->first ? 57000 : 20200, d->first ? 92000 : 34000);
+ if (error == R90_OK) {
+ memcpy(output, reply + 1, R90_BLOCK_SIZE);
+ d->first = false;
+ d->health_failures = 0;
+ break;
+ }
+ r90_forget(d);
+ if (attempt != 0 || !retryable(error))
+ break;
+ error = r90_identify(d);
+ if (error != R90_OK)
+ break;
+ }
+ r90_erase(reply, sizeof(reply));
+ return (error);
+}
diff --git a/sys/modules/Makefile b/sys/modules/Makefile
--- a/sys/modules/Makefile
+++ b/sys/modules/Makefile
@@ -357,6 +357,7 @@
re \
rge \
rl \
+ rng90 \
${_rockchip} \
rtsx \
${_rtw88} \
diff --git a/sys/modules/rng90/Makefile b/sys/modules/rng90/Makefile
new file mode 100644
--- /dev/null
+++ b/sys/modules/rng90/Makefile
@@ -0,0 +1,7 @@
+.PATH: ${SRCTOP}/sys/dev/rng90
+
+KMOD= rng90
+SRCS= rng90.c rng90_protocol.c
+SRCS+= device_if.h bus_if.h iicbus_if.h ofw_bus_if.h opt_platform.h
+
+.include <bsd.kmod.mk>
diff --git a/sys/sys/random.h b/sys/sys/random.h
--- a/sys/sys/random.h
+++ b/sys/sys/random.h
@@ -104,6 +104,7 @@
RANDOM_PURE_ARM_TRNG,
RANDOM_PURE_SAFE,
RANDOM_PURE_GLXSB,
+ RANDOM_PURE_RNG90,
ENTROPYSOURCE
};
_Static_assert(ENTROPYSOURCE <= 32,

File Metadata

Mime Type
text/plain
Expires
Fri, Nov 20, 10:00 AM (54 m, 50 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41863476
Default Alt Text
D60989.1795168850.diff (27 KB)

Event Timeline