bcm2835_rng.c revision 1.13
11.13Sskrll/* $NetBSD: bcm2835_rng.c,v 1.13 2017/12/10 21:38:26 skrll 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.13Sskrll__KERNEL_RCSID(0, "$NetBSD: bcm2835_rng.c,v 1.13 2017/12/10 21:38:26 skrll 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.1Sjmcneill 431.1Sjmcneill#include <arm/broadcom/bcm2835reg.h> 441.1Sjmcneill#include <arm/broadcom/bcm2835_intr.h> 451.1Sjmcneill 461.13Sskrll#include <dev/fdt/fdtvar.h> 471.13Sskrll 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.12Sriastrad kmutex_t sc_lock; 631.9Sriastrad krndsource_t sc_rndsource; 641.1Sjmcneill}; 651.1Sjmcneill 661.1Sjmcneillstatic int bcmrng_match(device_t, cfdata_t, void *); 671.1Sjmcneillstatic void bcmrng_attach(device_t, device_t, void *); 681.12Sriastradstatic void bcmrng_get(size_t, void *); 691.1Sjmcneill 701.13SskrllCFATTACH_DECL_NEW(bcmrng_fdt, sizeof(struct bcm2835rng_softc), 711.1Sjmcneill bcmrng_match, bcmrng_attach, NULL, NULL); 721.1Sjmcneill 731.1Sjmcneill/* ARGSUSED */ 741.1Sjmcneillstatic int 751.1Sjmcneillbcmrng_match(device_t parent, cfdata_t match, void *aux) 761.1Sjmcneill{ 771.13Sskrll const char * const compatible[] = { "brcm,bcm2835-rng", NULL }; 781.13Sskrll struct fdt_attach_args * const faa = aux; 791.1Sjmcneill 801.13Sskrll return of_match_compatible(faa->faa_phandle, compatible); 811.1Sjmcneill} 821.1Sjmcneill 831.1Sjmcneillstatic void 841.1Sjmcneillbcmrng_attach(device_t parent, device_t self, void *aux) 851.1Sjmcneill{ 861.6Sskrll struct bcm2835rng_softc *sc = device_private(self); 871.13Sskrll struct fdt_attach_args * const faa = aux; 881.13Sskrll const int phandle = faa->faa_phandle; 891.1Sjmcneill uint32_t ctrl; 901.13Sskrll bus_addr_t addr; 911.13Sskrll bus_size_t size; 921.13Sskrll int error; 931.1Sjmcneill 941.1Sjmcneill aprint_naive("\n"); 951.1Sjmcneill aprint_normal(": RNG\n"); 961.1Sjmcneill 971.1Sjmcneill sc->sc_dev = self; 981.13Sskrll sc->sc_iot = faa->faa_bst; 991.13Sskrll 1001.13Sskrll error = fdtbus_get_reg(phandle, 0, &addr, &size); 1011.13Sskrll if (error) { 1021.13Sskrll aprint_error_dev(sc->sc_dev, "unable to map device\n"); 1031.13Sskrll return; 1041.13Sskrll } 1051.1Sjmcneill 1061.13Sskrll if (bus_space_map(sc->sc_iot, addr, size, 0, 1071.1Sjmcneill &sc->sc_ioh)) { 1081.1Sjmcneill aprint_error_dev(sc->sc_dev, "unable to map device\n"); 1091.12Sriastrad return; 1101.1Sjmcneill } 1111.1Sjmcneill 1121.2Sjmcneill /* discard initial numbers, broadcom says they are "less random" */ 1131.2Sjmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS, 0x40000); 1141.2Sjmcneill 1151.1Sjmcneill /* enable rng */ 1161.1Sjmcneill ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL); 1171.1Sjmcneill ctrl |= RNG_CTRL_EN; 1181.1Sjmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL, ctrl); 1191.8Sriastrad 1201.9Sriastrad /* set up an rndsource */ 1211.12Sriastrad mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); 1221.12Sriastrad rndsource_setcb(&sc->sc_rndsource, &bcmrng_get, sc); 1231.9Sriastrad rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG, 1241.10Stls RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB); 1251.9Sriastrad 1261.8Sriastrad /* get some initial entropy ASAP */ 1271.12Sriastrad bcmrng_get(RND_POOLBITS / NBBY, sc); 1281.1Sjmcneill} 1291.1Sjmcneill 1301.1Sjmcneillstatic void 1311.12Sriastradbcmrng_get(size_t bytes_wanted, void *arg) 1321.1Sjmcneill{ 1331.12Sriastrad struct bcm2835rng_softc *sc = arg; 1341.9Sriastrad uint32_t status, cnt; 1351.9Sriastrad uint32_t buf[RNG_DATA_MAX]; /* 1k on the stack */ 1361.9Sriastrad 1371.12Sriastrad mutex_spin_enter(&sc->sc_lock); 1381.12Sriastrad while (bytes_wanted) { 1391.9Sriastrad status = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS); 1401.9Sriastrad cnt = __SHIFTOUT(status, RNG_STATUS_CNT); 1411.9Sriastrad KASSERT(cnt < RNG_DATA_MAX); 1421.9Sriastrad if (cnt == 0) 1431.9Sriastrad continue; /* XXX Busy-waiting seems wrong... */ 1441.9Sriastrad bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh, RNG_DATA, buf, 1451.9Sriastrad cnt); 1461.12Sriastrad rnd_add_data_sync(&sc->sc_rndsource, buf, (cnt * 4), 1471.9Sriastrad (cnt * 4 * NBBY)); 1481.12Sriastrad bytes_wanted -= MIN(bytes_wanted, (cnt * 4)); 1491.1Sjmcneill } 1501.9Sriastrad explicit_memset(buf, 0, sizeof(buf)); 1511.12Sriastrad mutex_spin_exit(&sc->sc_lock); 1521.1Sjmcneill} 153