Module Name:    src
Committed By:   macallan
Date:           Fri Aug  7 17:39:58 UTC 2015

Modified Files:
        src/sys/arch/mips/conf: files.ingenic
        src/sys/arch/mips/ingenic: apbus.c
Added Files:
        src/sys/arch/mips/ingenic: ingenic_rng.c

Log Message:
add driver for jz4780 random number generator
>From Michael McConville


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/mips/conf/files.ingenic
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/mips/ingenic/apbus.c
cvs rdiff -u -r0 -r1.1 src/sys/arch/mips/ingenic/ingenic_rng.c

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/mips/conf/files.ingenic
diff -u src/sys/arch/mips/conf/files.ingenic:1.6 src/sys/arch/mips/conf/files.ingenic:1.7
--- src/sys/arch/mips/conf/files.ingenic:1.6	Sat Jul 11 19:00:04 2015
+++ src/sys/arch/mips/conf/files.ingenic	Fri Aug  7 17:39:58 2015
@@ -1,4 +1,4 @@
-#	$NetBSD: files.ingenic,v 1.6 2015/07/11 19:00:04 macallan Exp $
+#	$NetBSD: files.ingenic,v 1.7 2015/08/07 17:39:58 macallan Exp $
 
 include "dev/scsipi/files.scsipi"		# SCSI devices
 include "dev/ata/files.ata"			# ATA devices
@@ -40,3 +40,8 @@ device 	jzmmc : sdmmcbus
 attach 	jzmmc at apbus
 file 	arch/mips/ingenic/jzmmc.c	jzmmc
 defflag	opt_ingenic.h	JZMMC_DEBUG
+
+# Random number generator
+device	jzrng
+attach	jzrng at apbus with ingenic_rng
+file	arch/mips/ingenic/ingenic_rng.c	ingenic_rng

Index: src/sys/arch/mips/ingenic/apbus.c
diff -u src/sys/arch/mips/ingenic/apbus.c:1.16 src/sys/arch/mips/ingenic/apbus.c:1.17
--- src/sys/arch/mips/ingenic/apbus.c:1.16	Sat Jul 11 19:00:04 2015
+++ src/sys/arch/mips/ingenic/apbus.c	Fri Aug  7 17:39:58 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: apbus.c,v 1.16 2015/07/11 19:00:04 macallan Exp $ */
+/*	$NetBSD: apbus.c,v 1.17 2015/08/07 17:39:58 macallan Exp $ */
 
 /*-
  * Copyright (c) 2014 Michael Lorenz
@@ -29,7 +29,7 @@
 /* catch-all for on-chip peripherals */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: apbus.c,v 1.16 2015/07/11 19:00:04 macallan Exp $");
+__KERNEL_RCSID(0, "$NetBSD: apbus.c,v 1.17 2015/08/07 17:39:58 macallan Exp $");
 
 #include "locators.h"
 #define	_MIPS_BUS_DMA_PRIVATE
@@ -96,6 +96,7 @@ static const apbus_dev_t apbus_devs[] = 
 	{ "jzmmc",	JZ_MSC1_BASE,   36, CLK_MSC1, 0, JZ_MSC1CDR},
 	{ "jzmmc",	JZ_MSC2_BASE,   35, CLK_MSC2, 0, JZ_MSC2CDR},
 	{ "jzfb",	JZ_LCDC0_BASE,  31, CLK_LCD, CLK_HDMI, 0},
+	{ "jzrng",	JZ_RNG,		-1, 0, 0, 0},
 	{ NULL,		-1,             -1, 0, 0, 0}
 };
 
@@ -160,6 +161,9 @@ apbus_attach(device_t parent, device_t s
 	reg &= ~CLK_AHB_MON;	/* AHB_MON clock */
 	writereg(JZ_CLKGR1, reg);
 
+	/* enable RNG */
+	writereg(JZ_ERNG, 1);
+
 	/* wake up the USB part */
 	reg = readreg(JZ_OPCR);
 	reg |= OPCR_SPENDN0 | OPCR_SPENDN1;
@@ -232,6 +236,8 @@ apbus_attach(device_t parent, device_t s
 	printf("JZ_SRBC   %08x\n", readreg(JZ_SRBC));
 	printf("JZ_OPCR   %08x\n", readreg(JZ_OPCR));
 	printf("JZ_UHCCDR %08x\n", readreg(JZ_UHCCDR));
+	printf("JZ_ERNG   %08x\n", readreg(JZ_ERNG));
+	printf("JZ_RNG    %08x\n", readreg(JZ_RNG));
 #endif
 
 	for (const apbus_dev_t *adv = apbus_devs; adv->name != NULL; adv++) {

Added files:

Index: src/sys/arch/mips/ingenic/ingenic_rng.c
diff -u /dev/null src/sys/arch/mips/ingenic/ingenic_rng.c:1.1
--- /dev/null	Fri Aug  7 17:39:58 2015
+++ src/sys/arch/mips/ingenic/ingenic_rng.c	Fri Aug  7 17:39:58 2015
@@ -0,0 +1,173 @@
+/*	$NetBSD: ingenic_rng.c,v 1.1 2015/08/07 17:39:58 macallan Exp $ */
+
+/*-
+ * Copyright (c) 2015 Michael McConville
+ * 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. 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: ingenic_rng.c,v 1.1 2015/08/07 17:39:58 macallan Exp $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/device.h>
+#include <sys/mutex.h>
+#include <sys/kernel.h>
+#include <sys/mutex.h>
+#include <sys/callout.h>
+#include <sys/bus.h>
+#include <sys/workqueue.h>
+#include <sys/rndpool.h>
+#include <sys/rndsource.h>
+
+#include <mips/ingenic/ingenic_var.h>
+#include <mips/ingenic/ingenic_regs.h>
+
+#include "opt_ingenic.h"
+
+struct ingenic_rng_softc;
+
+static int	ingenic_rng_match(device_t, cfdata_t, void *);
+static void	ingenic_rng_attach(device_t, device_t, void *);
+
+static void	ingenic_rng_get(struct ingenic_rng_softc *);
+static void	ingenic_rng_get_intr(void *);
+static void	ingenic_rng_get_cb(size_t, void *);
+
+struct ingenic_rng_softc {
+	device_t		sc_dev;
+	bus_space_tag_t		sc_bst;
+	bus_space_handle_t	sc_bsh;
+
+	void *			sc_sih;
+
+	krndsource_t		sc_rndsource;
+	size_t			sc_bytes_wanted;
+
+	kmutex_t		sc_intr_lock;
+	kmutex_t		sc_rnd_lock;
+};
+
+CFATTACH_DECL_NEW(ingenic_rng, sizeof(struct ingenic_rng_softc),
+	ingenic_rng_match, ingenic_rng_attach, NULL, NULL);
+
+static int
+ingenic_rng_match(device_t parent, cfdata_t cf, void *aux)
+{
+	struct apbus_attach_args *aa = aux;
+
+	if (strcmp(aa->aa_name, "jzrng") == 0) {
+		return 1;
+	} else {
+		return 0;
+	}
+}
+
+static void
+ingenic_rng_attach(device_t parent, device_t self, void *aux)
+{
+	struct ingenic_rng_softc * const sc = device_private(self);
+	struct apbus_attach_args * const aa = aux;
+	int error;
+
+	sc->sc_dev = self;
+	sc->sc_bst = aa->aa_bst;
+	if (aa->aa_addr == 0) {
+		aa->aa_addr = JZ_RNG;
+	}
+
+	error = bus_space_map(aa->aa_bst, aa->aa_addr, 4, 0, &sc->sc_bsh);
+	if (error) {
+		aprint_error_dev(self,
+			"can't map registers for %s: %d\n", aa->aa_name, error);
+		return;
+	}
+
+	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SERIAL);
+	mutex_init(&sc->sc_rnd_lock, MUTEX_DEFAULT, IPL_SERIAL);
+	
+	aprint_naive(": Ingenic random number generator\n");
+	aprint_normal(": Ingenic random number generator\n");
+
+	sc->sc_sih = softint_establish(SOFTINT_SERIAL|SOFTINT_MPSAFE,
+	    ingenic_rng_get_intr, sc);
+	if (sc->sc_sih == NULL) {
+		aprint_error_dev(self, "couldn't establish softint\n");
+		return;
+	}
+
+	rndsource_setcb(&sc->sc_rndsource, ingenic_rng_get_cb, sc);
+	rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
+	    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
+
+	ingenic_rng_get_cb(RND_POOLBITS / NBBY, sc);
+}
+
+static void
+ingenic_rng_get(struct ingenic_rng_softc *sc)
+{
+	uint32_t data;
+
+	mutex_spin_enter(&sc->sc_intr_lock);
+	while (sc->sc_bytes_wanted) {
+		bus_space_read_region_4(sc->sc_bst, sc->sc_bsh, 0, &data, 1);
+#if 0
+		device_printf(sc->sc_dev, "random output: %x\n", data);
+#endif
+		mutex_spin_exit(&sc->sc_intr_lock);
+		mutex_spin_enter(&sc->sc_rnd_lock);
+		rnd_add_data(&sc->sc_rndsource, &data, sizeof(data),
+		    sizeof(data) * NBBY);
+		mutex_spin_exit(&sc->sc_rnd_lock);
+		mutex_spin_enter(&sc->sc_intr_lock);
+		sc->sc_bytes_wanted -= MIN(sc->sc_bytes_wanted, sizeof(data));
+	}
+	explicit_memset(&data, 0, sizeof(data));
+	mutex_spin_exit(&sc->sc_intr_lock);
+}
+
+static void
+ingenic_rng_get_cb(size_t bytes_wanted, void *priv)
+{
+	struct ingenic_rng_softc * const sc = priv;
+
+	mutex_spin_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_spin_exit(&sc->sc_intr_lock);
+}
+
+static void
+ingenic_rng_get_intr(void *priv)
+{
+	struct ingenic_rng_softc * const sc = priv;
+
+	ingenic_rng_get(sc);
+}

Reply via email to