bcm2835_rng.c revision 1.9
11.9Sriastrad/*	$NetBSD: bcm2835_rng.c,v 1.9 2013/08/29 21:54:11 riastradh Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.3Sskrll * Copyright (c) 2013 The NetBSD Foundation, Inc.
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * This code is derived from software contributed to The NetBSD Foundation
81.1Sjmcneill * by Jared D. McNeill
91.1Sjmcneill *
101.1Sjmcneill * Redistribution and use in source and binary forms, with or without
111.1Sjmcneill * modification, are permitted provided that the following conditions
121.1Sjmcneill * are met:
131.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
141.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
151.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
161.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
171.1Sjmcneill *    documentation and/or other materials provided with the distribution.
181.1Sjmcneill *
191.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sjmcneill * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sjmcneill * POSSIBILITY OF SUCH DAMAGE.
301.1Sjmcneill */
311.1Sjmcneill
321.1Sjmcneill#include <sys/cdefs.h>
331.9Sriastrad__KERNEL_RCSID(0, "$NetBSD: bcm2835_rng.c,v 1.9 2013/08/29 21:54:11 riastradh Exp $");
341.1Sjmcneill
351.1Sjmcneill#include <sys/param.h>
361.1Sjmcneill#include <sys/systm.h>
371.1Sjmcneill#include <sys/device.h>
381.1Sjmcneill#include <sys/kernel.h>
391.1Sjmcneill#include <sys/bus.h>
401.1Sjmcneill#include <sys/rnd.h>
411.4Stls#include <sys/atomic.h>
421.9Sriastrad#include <sys/intr.h>
431.1Sjmcneill
441.1Sjmcneill#include <arm/broadcom/bcm_amba.h>
451.1Sjmcneill#include <arm/broadcom/bcm2835reg.h>
461.1Sjmcneill#include <arm/broadcom/bcm2835_intr.h>
471.1Sjmcneill
481.1Sjmcneill#define RNG_CTRL		0x00
491.1Sjmcneill#define  RNG_CTRL_EN		__BIT(0)
501.1Sjmcneill#define RNG_STATUS		0x04
511.9Sriastrad#define  RNG_STATUS_CNT		__BITS(31,24)
521.1Sjmcneill#define RNG_DATA		0x08
531.1Sjmcneill
541.1Sjmcneill#define RNG_DATA_MAX		256
551.1Sjmcneill
561.1Sjmcneillstruct bcm2835rng_softc {
571.9Sriastrad	device_t		sc_dev;
581.1Sjmcneill
591.9Sriastrad	bus_space_tag_t		sc_iot;
601.9Sriastrad	bus_space_handle_t	sc_ioh;
611.1Sjmcneill
621.9Sriastrad	kmutex_t		sc_intr_lock;
631.9Sriastrad	unsigned int		sc_bytes_wanted;
641.9Sriastrad	void 			*sc_sih;
651.4Stls
661.9Sriastrad	kmutex_t		sc_rnd_lock;
671.9Sriastrad	krndsource_t		sc_rndsource;
681.1Sjmcneill};
691.1Sjmcneill
701.1Sjmcneillstatic int bcmrng_match(device_t, cfdata_t, void *);
711.1Sjmcneillstatic void bcmrng_attach(device_t, device_t, void *);
721.9Sriastradstatic void bcmrng_get(struct bcm2835rng_softc *);
731.9Sriastradstatic void bcmrng_get_cb(size_t, void *);
741.9Sriastradstatic void bcmrng_get_intr(void *);
751.1Sjmcneill
761.1SjmcneillCFATTACH_DECL_NEW(bcmrng_amba, sizeof(struct bcm2835rng_softc),
771.1Sjmcneill    bcmrng_match, bcmrng_attach, NULL, NULL);
781.1Sjmcneill
791.1Sjmcneill/* ARGSUSED */
801.1Sjmcneillstatic int
811.1Sjmcneillbcmrng_match(device_t parent, cfdata_t match, void *aux)
821.1Sjmcneill{
831.1Sjmcneill	struct amba_attach_args *aaa = aux;
841.1Sjmcneill
851.1Sjmcneill	if (strcmp(aaa->aaa_name, "bcmrng") != 0)
861.1Sjmcneill		return 0;
871.1Sjmcneill
881.1Sjmcneill	return 1;
891.1Sjmcneill}
901.1Sjmcneill
911.1Sjmcneillstatic void
921.1Sjmcneillbcmrng_attach(device_t parent, device_t self, void *aux)
931.1Sjmcneill{
941.6Sskrll	struct bcm2835rng_softc *sc = device_private(self);
951.1Sjmcneill 	struct amba_attach_args *aaa = aux;
961.1Sjmcneill	uint32_t ctrl;
971.1Sjmcneill
981.1Sjmcneill	aprint_naive("\n");
991.1Sjmcneill	aprint_normal(": RNG\n");
1001.1Sjmcneill
1011.1Sjmcneill	sc->sc_dev = self;
1021.1Sjmcneill	sc->sc_iot = aaa->aaa_iot;
1031.1Sjmcneill
1041.1Sjmcneill	if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, BCM2835_RNG_SIZE, 0,
1051.1Sjmcneill	    &sc->sc_ioh)) {
1061.1Sjmcneill		aprint_error_dev(sc->sc_dev, "unable to map device\n");
1071.9Sriastrad		goto fail0;
1081.1Sjmcneill	}
1091.1Sjmcneill
1101.2Sjmcneill	/* discard initial numbers, broadcom says they are "less random" */
1111.2Sjmcneill	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS, 0x40000);
1121.2Sjmcneill
1131.1Sjmcneill	/* enable rng */
1141.1Sjmcneill	ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL);
1151.1Sjmcneill	ctrl |= RNG_CTRL_EN;
1161.1Sjmcneill	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL, ctrl);
1171.8Sriastrad
1181.9Sriastrad	/* set up a softint for adding data */
1191.9Sriastrad	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SERIAL);
1201.9Sriastrad	sc->sc_bytes_wanted = 0;
1211.9Sriastrad	sc->sc_sih = softint_establish(SOFTINT_SERIAL|SOFTINT_MPSAFE,
1221.9Sriastrad	    &bcmrng_get_intr, sc);
1231.9Sriastrad	if (sc->sc_sih == NULL) {
1241.9Sriastrad		aprint_error_dev(sc->sc_dev, "unable to establish softint");
1251.9Sriastrad		goto fail1;
1261.9Sriastrad	}
1271.9Sriastrad
1281.9Sriastrad	/* set up an rndsource */
1291.9Sriastrad	mutex_init(&sc->sc_rnd_lock, MUTEX_DEFAULT, IPL_SERIAL);
1301.9Sriastrad	rndsource_setcb(&sc->sc_rndsource, &bcmrng_get_cb, sc);
1311.9Sriastrad	rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
1321.9Sriastrad	    RND_FLAG_NO_ESTIMATE|RND_FLAG_HASCB);
1331.9Sriastrad
1341.8Sriastrad	/* get some initial entropy ASAP */
1351.9Sriastrad	bcmrng_get_cb(RND_POOLBITS / NBBY, sc);
1361.9Sriastrad
1371.9Sriastrad	/* Success!  */
1381.9Sriastrad	return;
1391.9Sriastrad
1401.9Sriastradfail1:	mutex_destroy(&sc->sc_intr_lock);
1411.9Sriastrad	bus_space_unmap(aaa->aaa_iot, sc->sc_ioh, BCM2835_RNG_SIZE);
1421.9Sriastradfail0:	return;
1431.1Sjmcneill}
1441.1Sjmcneill
1451.1Sjmcneillstatic void
1461.9Sriastradbcmrng_get(struct bcm2835rng_softc *sc)
1471.1Sjmcneill{
1481.9Sriastrad	uint32_t status, cnt;
1491.9Sriastrad	uint32_t buf[RNG_DATA_MAX]; /* 1k on the stack */
1501.9Sriastrad
1511.9Sriastrad	mutex_spin_enter(&sc->sc_intr_lock);
1521.9Sriastrad	while (sc->sc_bytes_wanted) {
1531.9Sriastrad		status = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS);
1541.9Sriastrad		cnt = __SHIFTOUT(status, RNG_STATUS_CNT);
1551.9Sriastrad		KASSERT(cnt < RNG_DATA_MAX);
1561.9Sriastrad		if (cnt == 0)
1571.9Sriastrad			continue;	/* XXX Busy-waiting seems wrong...  */
1581.9Sriastrad		bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh, RNG_DATA, buf,
1591.9Sriastrad		    cnt);
1601.9Sriastrad
1611.9Sriastrad		/*
1621.9Sriastrad		 * This lock dance is necessary because rnd_add_data
1631.9Sriastrad		 * may call bcmrng_get_cb which takes the intr lock.
1641.9Sriastrad		 */
1651.9Sriastrad		mutex_spin_exit(&sc->sc_intr_lock);
1661.9Sriastrad		mutex_spin_enter(&sc->sc_rnd_lock);
1671.9Sriastrad		rnd_add_data(&sc->sc_rndsource, buf, (cnt * 4),
1681.9Sriastrad		    (cnt * 4 * NBBY));
1691.9Sriastrad		mutex_spin_exit(&sc->sc_rnd_lock);
1701.9Sriastrad		mutex_spin_enter(&sc->sc_intr_lock);
1711.9Sriastrad		sc->sc_bytes_wanted -= MIN(sc->sc_bytes_wanted, (cnt * 4));
1721.1Sjmcneill	}
1731.9Sriastrad	explicit_memset(buf, 0, sizeof(buf));
1741.9Sriastrad	mutex_spin_exit(&sc->sc_intr_lock);
1751.9Sriastrad}
1761.9Sriastrad
1771.9Sriastradstatic void
1781.9Sriastradbcmrng_get_cb(size_t bytes_wanted, void *arg)
1791.9Sriastrad{
1801.9Sriastrad	struct bcm2835rng_softc *sc = arg;
1811.1Sjmcneill
1821.9Sriastrad	/*
1831.9Sriastrad	 * Deferring to a softint is necessary until the rnd(9) locking
1841.9Sriastrad	 * is fixed.
1851.9Sriastrad	 */
1861.9Sriastrad	mutex_spin_enter(&sc->sc_intr_lock);
1871.9Sriastrad	if (sc->sc_bytes_wanted == 0)
1881.9Sriastrad		softint_schedule(sc->sc_sih);
1891.9Sriastrad	if (bytes_wanted > (UINT_MAX - sc->sc_bytes_wanted))
1901.9Sriastrad		sc->sc_bytes_wanted = UINT_MAX;
1911.9Sriastrad	else
1921.9Sriastrad		sc->sc_bytes_wanted += bytes_wanted;
1931.9Sriastrad	mutex_spin_exit(&sc->sc_intr_lock);
1941.9Sriastrad}
1951.9Sriastrad
1961.9Sriastradstatic void
1971.9Sriastradbcmrng_get_intr(void *arg)
1981.9Sriastrad{
1991.9Sriastrad	struct bcm2835rng_softc *const sc = arg;
2001.4Stls
2011.9Sriastrad	bcmrng_get(sc);
2021.1Sjmcneill}
203