bcm2835_rng.c revision 1.11
11.11Sriastrad/* $NetBSD: bcm2835_rng.c,v 1.11 2015/04/13 21:18:40 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.11Sriastrad__KERNEL_RCSID(0, "$NetBSD: bcm2835_rng.c,v 1.11 2015/04/13 21:18:40 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.11Sriastrad#include <sys/rndpool.h> 411.11Sriastrad#include <sys/rndsource.h> 421.4Stls#include <sys/atomic.h> 431.9Sriastrad#include <sys/intr.h> 441.1Sjmcneill 451.1Sjmcneill#include <arm/broadcom/bcm_amba.h> 461.1Sjmcneill#include <arm/broadcom/bcm2835reg.h> 471.1Sjmcneill#include <arm/broadcom/bcm2835_intr.h> 481.1Sjmcneill 491.1Sjmcneill#define RNG_CTRL 0x00 501.1Sjmcneill#define RNG_CTRL_EN __BIT(0) 511.1Sjmcneill#define RNG_STATUS 0x04 521.9Sriastrad#define RNG_STATUS_CNT __BITS(31,24) 531.1Sjmcneill#define RNG_DATA 0x08 541.1Sjmcneill 551.1Sjmcneill#define RNG_DATA_MAX 256 561.1Sjmcneill 571.1Sjmcneillstruct bcm2835rng_softc { 581.9Sriastrad device_t sc_dev; 591.1Sjmcneill 601.9Sriastrad bus_space_tag_t sc_iot; 611.9Sriastrad bus_space_handle_t sc_ioh; 621.1Sjmcneill 631.9Sriastrad kmutex_t sc_intr_lock; 641.9Sriastrad unsigned int sc_bytes_wanted; 651.9Sriastrad void *sc_sih; 661.4Stls 671.9Sriastrad kmutex_t sc_rnd_lock; 681.9Sriastrad krndsource_t sc_rndsource; 691.1Sjmcneill}; 701.1Sjmcneill 711.1Sjmcneillstatic int bcmrng_match(device_t, cfdata_t, void *); 721.1Sjmcneillstatic void bcmrng_attach(device_t, device_t, void *); 731.9Sriastradstatic void bcmrng_get(struct bcm2835rng_softc *); 741.9Sriastradstatic void bcmrng_get_cb(size_t, void *); 751.9Sriastradstatic void bcmrng_get_intr(void *); 761.1Sjmcneill 771.1SjmcneillCFATTACH_DECL_NEW(bcmrng_amba, sizeof(struct bcm2835rng_softc), 781.1Sjmcneill bcmrng_match, bcmrng_attach, NULL, NULL); 791.1Sjmcneill 801.1Sjmcneill/* ARGSUSED */ 811.1Sjmcneillstatic int 821.1Sjmcneillbcmrng_match(device_t parent, cfdata_t match, void *aux) 831.1Sjmcneill{ 841.1Sjmcneill struct amba_attach_args *aaa = aux; 851.1Sjmcneill 861.1Sjmcneill if (strcmp(aaa->aaa_name, "bcmrng") != 0) 871.1Sjmcneill return 0; 881.1Sjmcneill 891.1Sjmcneill return 1; 901.1Sjmcneill} 911.1Sjmcneill 921.1Sjmcneillstatic void 931.1Sjmcneillbcmrng_attach(device_t parent, device_t self, void *aux) 941.1Sjmcneill{ 951.6Sskrll struct bcm2835rng_softc *sc = device_private(self); 961.1Sjmcneill struct amba_attach_args *aaa = aux; 971.1Sjmcneill uint32_t ctrl; 981.1Sjmcneill 991.1Sjmcneill aprint_naive("\n"); 1001.1Sjmcneill aprint_normal(": RNG\n"); 1011.1Sjmcneill 1021.1Sjmcneill sc->sc_dev = self; 1031.1Sjmcneill sc->sc_iot = aaa->aaa_iot; 1041.1Sjmcneill 1051.1Sjmcneill if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, BCM2835_RNG_SIZE, 0, 1061.1Sjmcneill &sc->sc_ioh)) { 1071.1Sjmcneill aprint_error_dev(sc->sc_dev, "unable to map device\n"); 1081.9Sriastrad goto fail0; 1091.1Sjmcneill } 1101.1Sjmcneill 1111.2Sjmcneill /* discard initial numbers, broadcom says they are "less random" */ 1121.2Sjmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS, 0x40000); 1131.2Sjmcneill 1141.1Sjmcneill /* enable rng */ 1151.1Sjmcneill ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL); 1161.1Sjmcneill ctrl |= RNG_CTRL_EN; 1171.1Sjmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL, ctrl); 1181.8Sriastrad 1191.9Sriastrad /* set up a softint for adding data */ 1201.9Sriastrad mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SERIAL); 1211.9Sriastrad sc->sc_bytes_wanted = 0; 1221.9Sriastrad sc->sc_sih = softint_establish(SOFTINT_SERIAL|SOFTINT_MPSAFE, 1231.9Sriastrad &bcmrng_get_intr, sc); 1241.9Sriastrad if (sc->sc_sih == NULL) { 1251.9Sriastrad aprint_error_dev(sc->sc_dev, "unable to establish softint"); 1261.9Sriastrad goto fail1; 1271.9Sriastrad } 1281.9Sriastrad 1291.9Sriastrad /* set up an rndsource */ 1301.9Sriastrad mutex_init(&sc->sc_rnd_lock, MUTEX_DEFAULT, IPL_SERIAL); 1311.9Sriastrad rndsource_setcb(&sc->sc_rndsource, &bcmrng_get_cb, sc); 1321.9Sriastrad rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG, 1331.10Stls RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB); 1341.9Sriastrad 1351.8Sriastrad /* get some initial entropy ASAP */ 1361.9Sriastrad bcmrng_get_cb(RND_POOLBITS / NBBY, sc); 1371.9Sriastrad 1381.9Sriastrad /* Success! */ 1391.9Sriastrad return; 1401.9Sriastrad 1411.9Sriastradfail1: mutex_destroy(&sc->sc_intr_lock); 1421.9Sriastrad bus_space_unmap(aaa->aaa_iot, sc->sc_ioh, BCM2835_RNG_SIZE); 1431.9Sriastradfail0: return; 1441.1Sjmcneill} 1451.1Sjmcneill 1461.1Sjmcneillstatic void 1471.9Sriastradbcmrng_get(struct bcm2835rng_softc *sc) 1481.1Sjmcneill{ 1491.9Sriastrad uint32_t status, cnt; 1501.9Sriastrad uint32_t buf[RNG_DATA_MAX]; /* 1k on the stack */ 1511.9Sriastrad 1521.9Sriastrad mutex_spin_enter(&sc->sc_intr_lock); 1531.9Sriastrad while (sc->sc_bytes_wanted) { 1541.9Sriastrad status = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS); 1551.9Sriastrad cnt = __SHIFTOUT(status, RNG_STATUS_CNT); 1561.9Sriastrad KASSERT(cnt < RNG_DATA_MAX); 1571.9Sriastrad if (cnt == 0) 1581.9Sriastrad continue; /* XXX Busy-waiting seems wrong... */ 1591.9Sriastrad bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh, RNG_DATA, buf, 1601.9Sriastrad cnt); 1611.9Sriastrad 1621.9Sriastrad /* 1631.9Sriastrad * This lock dance is necessary because rnd_add_data 1641.9Sriastrad * may call bcmrng_get_cb which takes the intr lock. 1651.9Sriastrad */ 1661.9Sriastrad mutex_spin_exit(&sc->sc_intr_lock); 1671.9Sriastrad mutex_spin_enter(&sc->sc_rnd_lock); 1681.9Sriastrad rnd_add_data(&sc->sc_rndsource, buf, (cnt * 4), 1691.9Sriastrad (cnt * 4 * NBBY)); 1701.9Sriastrad mutex_spin_exit(&sc->sc_rnd_lock); 1711.9Sriastrad mutex_spin_enter(&sc->sc_intr_lock); 1721.9Sriastrad sc->sc_bytes_wanted -= MIN(sc->sc_bytes_wanted, (cnt * 4)); 1731.1Sjmcneill } 1741.9Sriastrad explicit_memset(buf, 0, sizeof(buf)); 1751.9Sriastrad mutex_spin_exit(&sc->sc_intr_lock); 1761.9Sriastrad} 1771.9Sriastrad 1781.9Sriastradstatic void 1791.9Sriastradbcmrng_get_cb(size_t bytes_wanted, void *arg) 1801.9Sriastrad{ 1811.9Sriastrad struct bcm2835rng_softc *sc = arg; 1821.1Sjmcneill 1831.9Sriastrad /* 1841.9Sriastrad * Deferring to a softint is necessary until the rnd(9) locking 1851.9Sriastrad * is fixed. 1861.9Sriastrad */ 1871.9Sriastrad mutex_spin_enter(&sc->sc_intr_lock); 1881.9Sriastrad if (sc->sc_bytes_wanted == 0) 1891.9Sriastrad softint_schedule(sc->sc_sih); 1901.9Sriastrad if (bytes_wanted > (UINT_MAX - sc->sc_bytes_wanted)) 1911.9Sriastrad sc->sc_bytes_wanted = UINT_MAX; 1921.9Sriastrad else 1931.9Sriastrad sc->sc_bytes_wanted += bytes_wanted; 1941.9Sriastrad mutex_spin_exit(&sc->sc_intr_lock); 1951.9Sriastrad} 1961.9Sriastrad 1971.9Sriastradstatic void 1981.9Sriastradbcmrng_get_intr(void *arg) 1991.9Sriastrad{ 2001.9Sriastrad struct bcm2835rng_softc *const sc = arg; 2011.4Stls 2021.9Sriastrad bcmrng_get(sc); 2031.1Sjmcneill} 204