bcm2835_rng.c revision 1.1
11.1Sjmcneill/* $NetBSD: bcm2835_rng.c,v 1.1 2013/01/25 00:04:06 jmcneill Exp $ */ 21.1Sjmcneill 31.1Sjmcneill/*- 41.1Sjmcneill * Copyright (c) 2012 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.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: bcm2835_rng.c,v 1.1 2013/01/25 00:04:06 jmcneill 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.1Sjmcneill 421.1Sjmcneill#include <arm/broadcom/bcm_amba.h> 431.1Sjmcneill#include <arm/broadcom/bcm2835reg.h> 441.1Sjmcneill#include <arm/broadcom/bcm2835_intr.h> 451.1Sjmcneill 461.1Sjmcneill#define RNG_CTRL 0x00 471.1Sjmcneill#define RNG_CTRL_EN __BIT(0) 481.1Sjmcneill#define RNG_STATUS 0x04 491.1Sjmcneill#define RNG_STATUS_CNT_MASK __BITS(31,24) 501.1Sjmcneill#define RNG_STATUS_CNT_SHIFT 24 511.1Sjmcneill#define RNG_DATA 0x08 521.1Sjmcneill 531.1Sjmcneill#define RNG_DATA_MAX 256 541.1Sjmcneill 551.1Sjmcneillstruct bcm2835rng_softc { 561.1Sjmcneill device_t sc_dev; 571.1Sjmcneill 581.1Sjmcneill bus_space_tag_t sc_iot; 591.1Sjmcneill bus_space_handle_t sc_ioh; 601.1Sjmcneill 611.1Sjmcneill krndsource_t sc_rnd; 621.1Sjmcneill callout_t sc_tick; 631.1Sjmcneill 641.1Sjmcneill uint32_t sc_data[RNG_DATA_MAX]; 651.1Sjmcneill}; 661.1Sjmcneill 671.1Sjmcneillstatic int bcmrng_match(device_t, cfdata_t, void *); 681.1Sjmcneillstatic void bcmrng_attach(device_t, device_t, void *); 691.1Sjmcneill 701.1Sjmcneillstatic void bcmrng_tick(void *); 711.1Sjmcneill 721.1SjmcneillCFATTACH_DECL_NEW(bcmrng_amba, sizeof(struct bcm2835rng_softc), 731.1Sjmcneill bcmrng_match, bcmrng_attach, NULL, NULL); 741.1Sjmcneill 751.1Sjmcneill/* ARGSUSED */ 761.1Sjmcneillstatic int 771.1Sjmcneillbcmrng_match(device_t parent, cfdata_t match, void *aux) 781.1Sjmcneill{ 791.1Sjmcneill struct amba_attach_args *aaa = aux; 801.1Sjmcneill 811.1Sjmcneill if (strcmp(aaa->aaa_name, "bcmrng") != 0) 821.1Sjmcneill return 0; 831.1Sjmcneill 841.1Sjmcneill return 1; 851.1Sjmcneill} 861.1Sjmcneill 871.1Sjmcneillstatic void 881.1Sjmcneillbcmrng_attach(device_t parent, device_t self, void *aux) 891.1Sjmcneill{ 901.1Sjmcneill struct bcm2835rng_softc *sc = device_private(self); 911.1Sjmcneill struct amba_attach_args *aaa = aux; 921.1Sjmcneill uint32_t ctrl; 931.1Sjmcneill 941.1Sjmcneill aprint_naive("\n"); 951.1Sjmcneill aprint_normal(": RNG\n"); 961.1Sjmcneill 971.1Sjmcneill sc->sc_dev = self; 981.1Sjmcneill sc->sc_iot = aaa->aaa_iot; 991.1Sjmcneill 1001.1Sjmcneill if (bus_space_map(aaa->aaa_iot, aaa->aaa_addr, BCM2835_RNG_SIZE, 0, 1011.1Sjmcneill &sc->sc_ioh)) { 1021.1Sjmcneill aprint_error_dev(sc->sc_dev, "unable to map device\n"); 1031.1Sjmcneill return; 1041.1Sjmcneill } 1051.1Sjmcneill 1061.1Sjmcneill rnd_attach_source(&sc->sc_rnd, device_xname(self), RND_TYPE_RNG, 1071.1Sjmcneill RND_FLAG_NO_ESTIMATE); 1081.1Sjmcneill 1091.1Sjmcneill callout_init(&sc->sc_tick, CALLOUT_MPSAFE); 1101.1Sjmcneill callout_setfunc(&sc->sc_tick, bcmrng_tick, sc); 1111.1Sjmcneill 1121.1Sjmcneill /* enable rng */ 1131.1Sjmcneill ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL); 1141.1Sjmcneill ctrl |= RNG_CTRL_EN; 1151.1Sjmcneill bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL, ctrl); 1161.1Sjmcneill 1171.1Sjmcneill /* start timer */ 1181.1Sjmcneill bcmrng_tick(sc); 1191.1Sjmcneill} 1201.1Sjmcneill 1211.1Sjmcneillstatic void 1221.1Sjmcneillbcmrng_tick(void *priv) 1231.1Sjmcneill{ 1241.1Sjmcneill struct bcm2835rng_softc *sc = priv; 1251.1Sjmcneill uint32_t status; 1261.1Sjmcneill int cnt, i; 1271.1Sjmcneill 1281.1Sjmcneill status = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS); 1291.1Sjmcneill cnt = (status & RNG_STATUS_CNT_MASK) >> RNG_STATUS_CNT_SHIFT; 1301.1Sjmcneill for (i = 0; i < cnt; i++) { 1311.1Sjmcneill sc->sc_data[i] = bus_space_read_4(sc->sc_iot, sc->sc_ioh, 1321.1Sjmcneill RNG_DATA); 1331.1Sjmcneill } 1341.1Sjmcneill if (cnt > 0) { 1351.1Sjmcneill rnd_add_data(&sc->sc_rnd, sc->sc_data, 1361.1Sjmcneill cnt * 4, cnt * 4 * NBBY); 1371.1Sjmcneill } 1381.1Sjmcneill 1391.1Sjmcneill callout_schedule(&sc->sc_tick, 1); 1401.1Sjmcneill} 141