Module Name: src Committed By: snj Date: Wed Jun 10 16:38:05 UTC 2015
Modified Files: src/sys/arch/arm/omap [netbsd-7]: am335x_prcm.h files.omap2 omap2_reg.h src/sys/arch/evbarm/conf [netbsd-7]: BEAGLEBONE Added Files: src/sys/arch/arm/omap [netbsd-7]: am335x_trng.c am335x_trngreg.h Log Message: Pull up following revision(s) (requested by jmcneill in ticket #830): sys/arch/arm/omap/am335x_prcm.h: revision 1.9 sys/arch/arm/omap/am335x_trngreg.h: revision 1.1 sys/arch/arm/omap/am335x_trng.c: revision 1.1 sys/arch/arm/omap/files.omap2: revision 1.31 sys/arch/arm/omap/omap2_reg.h: revision 1.30 sys/arch/evbarm/conf/BEAGLEBONE: revision 1.32 Add driver for AM335x TRNG module. -- enable trng driver To generate a diff of this commit: cvs rdiff -u -r1.8 -r1.8.2.1 src/sys/arch/arm/omap/am335x_prcm.h cvs rdiff -u -r0 -r1.1.2.2 src/sys/arch/arm/omap/am335x_trng.c \ src/sys/arch/arm/omap/am335x_trngreg.h cvs rdiff -u -r1.28.2.1 -r1.28.2.2 src/sys/arch/arm/omap/files.omap2 \ src/sys/arch/arm/omap/omap2_reg.h cvs rdiff -u -r1.24.2.1 -r1.24.2.2 src/sys/arch/evbarm/conf/BEAGLEBONE 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/omap/am335x_prcm.h diff -u src/sys/arch/arm/omap/am335x_prcm.h:1.8 src/sys/arch/arm/omap/am335x_prcm.h:1.8.2.1 --- src/sys/arch/arm/omap/am335x_prcm.h:1.8 Sun Jul 20 23:08:43 2014 +++ src/sys/arch/arm/omap/am335x_prcm.h Wed Jun 10 16:38:05 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: am335x_prcm.h,v 1.8 2014/07/20 23:08:43 bouyer Exp $ */ +/* $NetBSD: am335x_prcm.h,v 1.8.2.1 2015/06/10 16:38:05 snj Exp $ */ /* * TI OMAP Power, Reset, and Clock Management on the AM335x @@ -63,6 +63,7 @@ struct omap_module { #define CM_PER_TIMER2_CLKCTRL 0x080 #define CM_PER_TIMER3_CLKCTRL 0x084 #define CM_PER_TIMER4_CLKCTRL 0x088 +#define CM_PER_RNG_CLKCTRL 0x090 #define CM_PER_GPIO1_CLKCTRL 0x0AC #define CM_PER_GPIO2_CLKCTRL 0x0B0 #define CM_PER_GPIO3_CLKCTRL 0x0B4 Index: src/sys/arch/arm/omap/files.omap2 diff -u src/sys/arch/arm/omap/files.omap2:1.28.2.1 src/sys/arch/arm/omap/files.omap2:1.28.2.2 --- src/sys/arch/arm/omap/files.omap2:1.28.2.1 Sun Apr 19 04:37:17 2015 +++ src/sys/arch/arm/omap/files.omap2 Wed Jun 10 16:38:05 2015 @@ -1,4 +1,4 @@ -# $NetBSD: files.omap2,v 1.28.2.1 2015/04/19 04:37:17 msaitoh Exp $ +# $NetBSD: files.omap2,v 1.28.2.2 2015/06/10 16:38:05 snj Exp $ # # Configuration info for Texas Instruments OMAP2/OMAP3 CPU support # Based on xscale/files.pxa2x0 @@ -174,6 +174,10 @@ device edma attach edma at obio file arch/arm/omap/omap_edma.c edma needs-flag +device trng +attach trng at obio +file arch/arm/omap/am335x_trng.c trng + # these bus space methods are not bus-specific ... # file arch/arm/omap/omap_nobyteacc_space.c emifs | gpmc Index: src/sys/arch/arm/omap/omap2_reg.h diff -u src/sys/arch/arm/omap/omap2_reg.h:1.28.2.1 src/sys/arch/arm/omap/omap2_reg.h:1.28.2.2 --- src/sys/arch/arm/omap/omap2_reg.h:1.28.2.1 Sun Apr 19 04:37:17 2015 +++ src/sys/arch/arm/omap/omap2_reg.h Wed Jun 10 16:38:05 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: omap2_reg.h,v 1.28.2.1 2015/04/19 04:37:17 msaitoh Exp $ */ +/* $NetBSD: omap2_reg.h,v 1.28.2.2 2015/06/10 16:38:05 snj Exp $ */ /* * Copyright (c) 2007 Microsoft @@ -912,4 +912,10 @@ #define AM335X_INT_TCERRINT0 112 #define AM335X_INT_TCERRINT1 113 #define AM335X_INT_TCERRINT2 114 + +/* TRNG */ +#define AM335X_TRNG_BASE 0x48310000 +#define AM335X_TRNG_SIZE 0x2000 +#define AM335X_INT_TRNG 111 + #endif /* _ARM_OMAP_OMAP2_REG_H_ */ Index: src/sys/arch/evbarm/conf/BEAGLEBONE diff -u src/sys/arch/evbarm/conf/BEAGLEBONE:1.24.2.1 src/sys/arch/evbarm/conf/BEAGLEBONE:1.24.2.2 --- src/sys/arch/evbarm/conf/BEAGLEBONE:1.24.2.1 Sun Apr 19 04:37:17 2015 +++ src/sys/arch/evbarm/conf/BEAGLEBONE Wed Jun 10 16:38:05 2015 @@ -1,5 +1,5 @@ # -# $NetBSD: BEAGLEBONE,v 1.24.2.1 2015/04/19 04:37:17 msaitoh Exp $ +# $NetBSD: BEAGLEBONE,v 1.24.2.2 2015/06/10 16:38:05 snj Exp $ # # BEAGLEBONE -- TI AM335x board Kernel # @@ -247,6 +247,9 @@ omapdmtimer2 at obio0 addr 0x48044000 si # Watchdog timers omapwdt32k* at obio0 addr 0x44e35000 size 0x1000 # WDT1 +# Random number generator +trng* at obio0 addr 0x48310000 size 0x2000 intr 111 # TRNG + # onboard video, experimental. Video mode is hardcoded in the driver #tifb* at obio0 addr 0x4830E000 size 0x1000 intr 36 Added files: Index: src/sys/arch/arm/omap/am335x_trng.c diff -u /dev/null src/sys/arch/arm/omap/am335x_trng.c:1.1.2.2 --- /dev/null Wed Jun 10 16:38:05 2015 +++ src/sys/arch/arm/omap/am335x_trng.c Wed Jun 10 16:38:05 2015 @@ -0,0 +1,184 @@ +/* $NetBSD: am335x_trng.c,v 1.1.2.2 2015/06/10 16:38:05 snj 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: am335x_trng.c,v 1.1.2.2 2015/06/10 16:38:05 snj Exp $"); + +#include "opt_omap.h" + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/device.h> +#include <sys/conf.h> +#include <sys/intr.h> +#include <sys/mutex.h> +#include <sys/bus.h> +#include <sys/rndpool.h> +#include <sys/rndsource.h> + +#include <arm/omap/am335x_prcm.h> +#include <arm/omap/omap2_prcm.h> +#include <arm/omap/sitara_cm.h> +#include <arm/omap/sitara_cmreg.h> + +#include <arm/omap/omap2_reg.h> +#include <arm/omap/omap2_obiovar.h> + +#include <arm/omap/am335x_trngreg.h> + +static const struct omap_module rng_module = + { AM335X_PRCM_CM_PER, CM_PER_RNG_CLKCTRL }; + +struct trng_softc { + device_t sc_dev; + bus_space_tag_t sc_iot; + bus_space_handle_t sc_ioh; + + kmutex_t sc_intr_lock; + kmutex_t sc_rnd_lock; + u_int sc_bytes_wanted; + void *sc_sih; + krndsource_t sc_rndsource; +}; + +static int trng_match(device_t, cfdata_t, void *); +static void trng_attach(device_t, device_t, void *); +static void trng_softintr(void *); +static void trng_callback(size_t, void *); + +CFATTACH_DECL_NEW(trng, sizeof(struct trng_softc), + trng_match, trng_attach, NULL, NULL); + +#define TRNG_READ(sc, reg) \ + bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)) +#define TRNG_WRITE(sc, reg, val) \ + bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val)) + +static int +trng_match(device_t parent, cfdata_t match, void *aux) +{ + struct obio_attach_args *obio = aux; + + if (obio->obio_addr == AM335X_TRNG_BASE && + obio->obio_size == AM335X_TRNG_SIZE && + obio->obio_intr == AM335X_INT_TRNG) + return 1; + + return 0; +} + +static void +trng_attach(device_t parent, device_t self, void *aux) +{ + struct trng_softc *sc = device_private(self); + struct obio_attach_args *obio = aux; + + sc->sc_dev = self; + sc->sc_iot = obio->obio_iot; + if (bus_space_map(obio->obio_iot, obio->obio_addr, obio->obio_size, + 0, &sc->sc_ioh) != 0) { + aprint_error(": couldn't map address spcae\n"); + return; + } + mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SERIAL); + mutex_init(&sc->sc_rnd_lock, MUTEX_DEFAULT, IPL_SERIAL); + sc->sc_bytes_wanted = 0; + sc->sc_sih = softint_establish(SOFTINT_SERIAL|SOFTINT_MPSAFE, + trng_softintr, sc); + if (sc->sc_sih == NULL) { + aprint_error(": couldn't establish softint\n"); + return; + } + + prcm_module_enable(&rng_module); + + if ((TRNG_READ(sc, TRNG_CONTROL_REG) & TRNG_CONTROL_ENABLE) == 0) { + TRNG_WRITE(sc, TRNG_CONFIG_REG, + __SHIFTIN(0x21, TRNG_CONFIG_MIN_REFILL) | + __SHIFTIN(0x22, TRNG_CONFIG_MAX_REFILL)); + TRNG_WRITE(sc, TRNG_CONTROL_REG, + __SHIFTIN(0x21, TRNG_CONTROL_STARTUP_CYCLES) | + TRNG_CONTROL_ENABLE); + } + + rndsource_setcb(&sc->sc_rndsource, trng_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("\n"); + + trng_callback(RND_POOLBITS / NBBY, sc); +} + +static void +trng_softintr(void *priv) +{ + struct trng_softc * const sc = priv; + uint32_t buf[2]; + u_int retry; + + mutex_enter(&sc->sc_intr_lock); + while (sc->sc_bytes_wanted) { + for (retry = 10; retry > 0; retry--) { + if (TRNG_READ(sc, TRNG_STATUS_REG) & TRNG_STATUS_READY) + break; + delay(10); + } + if (retry == 0) + break; + buf[0] = TRNG_READ(sc, TRNG_OUTPUT_L_REG); + buf[1] = TRNG_READ(sc, TRNG_OUTPUT_H_REG); + TRNG_WRITE(sc, TRNG_INTACK_REG, TRNG_INTACK_READY); + mutex_exit(&sc->sc_intr_lock); + mutex_enter(&sc->sc_rnd_lock); + rnd_add_data(&sc->sc_rndsource, buf, sizeof(buf), + sizeof(buf) * NBBY); + mutex_exit(&sc->sc_rnd_lock); + mutex_enter(&sc->sc_intr_lock); + sc->sc_bytes_wanted -= MIN(sc->sc_bytes_wanted, sizeof(buf)); + } + explicit_memset(buf, 0, sizeof(buf)); + mutex_exit(&sc->sc_intr_lock); +} + +static void +trng_callback(size_t bytes_wanted, void *priv) +{ + struct trng_softc * const sc = priv; + + mutex_enter(&sc->sc_intr_lock); + if (sc->sc_bytes_wanted == 0) { + softint_schedule(sc->sc_sih); + } + if (bytes_wanted > (UINT_MAX - sc->sc_bytes_wanted)) { + sc->sc_bytes_wanted = UINT_MAX; + } else { + sc->sc_bytes_wanted += bytes_wanted; + } + mutex_exit(&sc->sc_intr_lock); +} Index: src/sys/arch/arm/omap/am335x_trngreg.h diff -u /dev/null src/sys/arch/arm/omap/am335x_trngreg.h:1.1.2.2 --- /dev/null Wed Jun 10 16:38:05 2015 +++ src/sys/arch/arm/omap/am335x_trngreg.h Wed Jun 10 16:38:05 2015 @@ -0,0 +1,44 @@ +/* $NetBSD: am335x_trngreg.h,v 1.1.2.2 2015/06/10 16:38:05 snj 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 _AM335X_TRNGREG_H +#define _AM335X_TRNGREG_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 /* !_AM335X_TRNGREG_H */