Module Name:    src
Committed By:   jmcneill
Date:           Mon Oct 28 23:58:00 UTC 2019

Modified Files:
        src/sys/arch/arm/ti: am3_prcm.c files.ti
Added Files:
        src/sys/arch/arm/ti: ti_rng.c ti_rngreg.h

Log Message:
Add support for hardware RNG.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/arm/ti/am3_prcm.c
cvs rdiff -u -r1.13 -r1.14 src/sys/arch/arm/ti/files.ti
cvs rdiff -u -r0 -r1.1 src/sys/arch/arm/ti/ti_rng.c \
    src/sys/arch/arm/ti/ti_rngreg.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/arm/ti/am3_prcm.c
diff -u src/sys/arch/arm/ti/am3_prcm.c:1.6 src/sys/arch/arm/ti/am3_prcm.c:1.7
--- src/sys/arch/arm/ti/am3_prcm.c:1.6	Mon Oct 28 22:21:35 2019
+++ src/sys/arch/arm/ti/am3_prcm.c	Mon Oct 28 23:57:59 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: am3_prcm.c,v 1.6 2019/10/28 22:21:35 jmcneill Exp $ */
+/* $NetBSD: am3_prcm.c,v 1.7 2019/10/28 23:57:59 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2017 Jared McNeill <jmcne...@invisible.ca>
@@ -28,7 +28,7 @@
 
 #include <sys/cdefs.h>
 
-__KERNEL_RCSID(1, "$NetBSD: am3_prcm.c,v 1.6 2019/10/28 22:21:35 jmcneill Exp $");
+__KERNEL_RCSID(1, "$NetBSD: am3_prcm.c,v 1.7 2019/10/28 23:57:59 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -124,6 +124,8 @@ static struct ti_prcm_clk am3_prcm_clks[
 	AM3_PRCM_HWMOD_PER("tptc2", 0x100, "PERIPH_CLK"),
 
 	AM3_PRCM_HWMOD_PER("usb_otg_hs", 0x1c, "PERIPH_CLK"),
+
+	AM3_PRCM_HWMOD_PER("rng", 0x90, "PERIPH_CLK"),
 };
 
 static int

Index: src/sys/arch/arm/ti/files.ti
diff -u src/sys/arch/arm/ti/files.ti:1.13 src/sys/arch/arm/ti/files.ti:1.14
--- src/sys/arch/arm/ti/files.ti:1.13	Mon Oct 28 22:21:35 2019
+++ src/sys/arch/arm/ti/files.ti	Mon Oct 28 23:57:59 2019
@@ -1,4 +1,4 @@
-#	$NetBSD: files.ti,v 1.13 2019/10/28 22:21:35 jmcneill Exp $
+#	$NetBSD: files.ti,v 1.14 2019/10/28 23:57:59 jmcneill Exp $
 #
 
 file	arch/arm/ti/ti_platform.c	soc_ti
@@ -80,6 +80,11 @@ file	arch/arm/ti/ti_otg.c		ti_otg
 attach	motg at fdt with ti_motg
 file	arch/arm/ti/ti_motg.c		ti_motg
 
+# RNG
+device	tirng
+attach	tirng at fdt with ti_rng
+file	arch/arm/ti/ti_rng.c		ti_rng
+
 # SOC parameters
 defflag	opt_soc.h			SOC_TI
 defflag	opt_soc.h			SOC_TI_AM335X: SOC_TI

Added files:

Index: src/sys/arch/arm/ti/ti_rng.c
diff -u /dev/null src/sys/arch/arm/ti/ti_rng.c:1.1
--- /dev/null	Mon Oct 28 23:58:00 2019
+++ src/sys/arch/arm/ti/ti_rng.c	Mon Oct 28 23:57:59 2019
@@ -0,0 +1,151 @@
+/* $NetBSD: ti_rng.c,v 1.1 2019/10/28 23:57:59 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2015 Jared D. McNeill <jmcne...@invisible.ca>
+ * All rights reserved.
+ *
+ * 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. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: ti_rng.c,v 1.1 2019/10/28 23:57:59 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <sys/conf.h>
+#include <sys/mutex.h>
+#include <sys/bus.h>
+#include <sys/rndpool.h>
+#include <sys/rndsource.h>
+
+#include <dev/fdt/fdtvar.h>
+
+#include <arm/ti/ti_prcm.h>
+#include <arm/ti/ti_rngreg.h>
+
+static const char * const compatible[] = {
+	"ti,omap4-rng",
+	NULL
+};
+
+struct ti_rng_softc {
+	device_t sc_dev;
+	bus_space_tag_t sc_iot;
+	bus_space_handle_t sc_ioh;
+
+	kmutex_t sc_lock;
+	krndsource_t sc_rndsource;
+};
+
+static int	ti_rng_match(device_t, cfdata_t, void *);
+static void	ti_rng_attach(device_t, device_t, void *);
+static void	ti_rng_callback(size_t, void *);
+
+CFATTACH_DECL_NEW(ti_rng, sizeof(struct ti_rng_softc),
+    ti_rng_match, ti_rng_attach, NULL, NULL);
+
+#define RD4(sc, reg) \
+	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
+#define WR4(sc, reg, val) \
+	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
+
+static int
+ti_rng_match(device_t parent, cfdata_t match, void *aux)
+{
+	struct fdt_attach_args * const faa = aux;
+
+	return of_match_compatible(faa->faa_phandle, compatible);
+}
+
+static void
+ti_rng_attach(device_t parent, device_t self, void *aux)
+{
+	struct ti_rng_softc *sc = device_private(self);
+	struct fdt_attach_args * const faa = aux;
+	const int phandle = faa->faa_phandle;
+	bus_addr_t addr;
+	bus_size_t size;
+
+	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
+		aprint_error(": couldn't get registers\n");
+		return;
+	}
+
+	if (ti_prcm_enable_hwmod(OF_parent(phandle), 0) != 0) {
+		aprint_error(": couldn't enable module\n");
+		return;
+	}
+
+	sc->sc_dev = self;
+	sc->sc_iot = faa->faa_bst;
+	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
+		aprint_error(": couldn't map registers\n");
+		return;
+	}
+
+	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
+
+	if ((RD4(sc, TRNG_CONTROL_REG) & TRNG_CONTROL_ENABLE) == 0) {
+		WR4(sc, TRNG_CONFIG_REG,
+		    __SHIFTIN(0x21, TRNG_CONFIG_MIN_REFILL) |
+		    __SHIFTIN(0x22, TRNG_CONFIG_MAX_REFILL));
+		WR4(sc, TRNG_CONTROL_REG,
+		    __SHIFTIN(0x21, TRNG_CONTROL_STARTUP_CYCLES) |
+		    TRNG_CONTROL_ENABLE);
+	}
+
+	rndsource_setcb(&sc->sc_rndsource, ti_rng_callback, sc);
+	rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
+	    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
+
+	aprint_naive("\n");
+	aprint_normal(": RNG\n");
+
+	ti_rng_callback(RND_POOLBITS / NBBY, sc);
+}
+
+static void
+ti_rng_callback(size_t bytes_wanted, void *priv)
+{
+	struct ti_rng_softc * const sc = priv;
+	uint32_t buf[2];
+	u_int retry;
+
+	mutex_enter(&sc->sc_lock);
+	while (bytes_wanted) {
+		for (retry = 10; retry > 0; retry--) {
+			if (RD4(sc, TRNG_STATUS_REG) & TRNG_STATUS_READY)
+				break;
+			delay(10);
+		}
+		if (retry == 0)
+			break;
+		buf[0] = RD4(sc, TRNG_OUTPUT_L_REG);
+		buf[1] = RD4(sc, TRNG_OUTPUT_H_REG);
+		WR4(sc, TRNG_INTACK_REG, TRNG_INTACK_READY);
+		rnd_add_data_sync(&sc->sc_rndsource, buf, sizeof(buf),
+		    sizeof(buf) * NBBY);
+		bytes_wanted -= MIN(bytes_wanted, sizeof(buf));
+	}
+	explicit_memset(buf, 0, sizeof(buf));
+	mutex_exit(&sc->sc_lock);
+}
Index: src/sys/arch/arm/ti/ti_rngreg.h
diff -u /dev/null src/sys/arch/arm/ti/ti_rngreg.h:1.1
--- /dev/null	Mon Oct 28 23:58:00 2019
+++ src/sys/arch/arm/ti/ti_rngreg.h	Mon Oct 28 23:57:59 2019
@@ -0,0 +1,44 @@
+/* $NetBSD: ti_rngreg.h,v 1.1 2019/10/28 23:57:59 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2015 Jared D. McNeill <jmcne...@invisible.ca>
+ * All rights reserved.
+ *
+ * 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. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
+ */
+
+#ifndef _TI_RNGREG_H
+#define _TI_RNGREG_H
+
+#define TRNG_OUTPUT_L_REG	0x00
+#define TRNG_OUTPUT_H_REG	0x04
+#define TRNG_STATUS_REG		0x08
+#define TRNG_STATUS_READY		__BIT(0)
+#define TRNG_INTACK_REG		0x10
+#define TRNG_INTACK_READY		__BIT(0)
+#define TRNG_CONTROL_REG	0x14
+#define TRNG_CONTROL_STARTUP_CYCLES	__BITS(31,16)
+#define TRNG_CONTROL_ENABLE		__BIT(10)
+#define TRNG_CONFIG_REG		0x18
+#define TRNG_CONFIG_MAX_REFILL		__BITS(31,16)
+#define TRNG_CONFIG_MIN_REFILL		__BITS(7,0)
+
+#endif /* !_TI_RNGREG_H */

Reply via email to