meson_rng.c revision 1.1
11.1Sjmcneill/* $NetBSD: meson_rng.c,v 1.1 2019/01/19 20:56:03 jmcneill Exp $ */ 21.1Sjmcneill 31.1Sjmcneill/*- 41.1Sjmcneill * Copyright (c) 2015-2019 Jared D. McNeill <jmcneill@invisible.ca> 51.1Sjmcneill * All rights reserved. 61.1Sjmcneill * 71.1Sjmcneill * Redistribution and use in source and binary forms, with or without 81.1Sjmcneill * modification, are permitted provided that the following conditions 91.1Sjmcneill * are met: 101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright 111.1Sjmcneill * notice, this list of conditions and the following disclaimer. 121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright 131.1Sjmcneill * notice, this list of conditions and the following disclaimer in the 141.1Sjmcneill * documentation and/or other materials provided with the distribution. 151.1Sjmcneill * 161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 261.1Sjmcneill * SUCH DAMAGE. 271.1Sjmcneill */ 281.1Sjmcneill 291.1Sjmcneill#include <sys/cdefs.h> 301.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: meson_rng.c,v 1.1 2019/01/19 20:56:03 jmcneill Exp $"); 311.1Sjmcneill 321.1Sjmcneill#include <sys/param.h> 331.1Sjmcneill#include <sys/bus.h> 341.1Sjmcneill#include <sys/device.h> 351.1Sjmcneill#include <sys/systm.h> 361.1Sjmcneill#include <sys/kernel.h> 371.1Sjmcneill#include <sys/mutex.h> 381.1Sjmcneill#include <sys/rndpool.h> 391.1Sjmcneill#include <sys/rndsource.h> 401.1Sjmcneill 411.1Sjmcneill#include <dev/fdt/fdtvar.h> 421.1Sjmcneill 431.1Sjmcneillstatic int meson_rng_match(device_t, cfdata_t, void *); 441.1Sjmcneillstatic void meson_rng_attach(device_t, device_t, void *); 451.1Sjmcneill 461.1Sjmcneillstatic void meson_rng_get(size_t, void *); 471.1Sjmcneill 481.1Sjmcneillstruct meson_rng_softc { 491.1Sjmcneill device_t sc_dev; 501.1Sjmcneill bus_space_tag_t sc_bst; 511.1Sjmcneill bus_space_handle_t sc_bsh; 521.1Sjmcneill 531.1Sjmcneill kmutex_t sc_lock; 541.1Sjmcneill krndsource_t sc_rndsource; 551.1Sjmcneill}; 561.1Sjmcneill 571.1Sjmcneillstatic const char * const compatible[] = { 581.1Sjmcneill "amlogic,meson8b-rng", 591.1Sjmcneill NULL 601.1Sjmcneill}; 611.1Sjmcneill 621.1SjmcneillCFATTACH_DECL_NEW(meson_rng, sizeof(struct meson_rng_softc), 631.1Sjmcneill meson_rng_match, meson_rng_attach, NULL, NULL); 641.1Sjmcneill 651.1Sjmcneillstatic int 661.1Sjmcneillmeson_rng_match(device_t parent, cfdata_t cf, void *aux) 671.1Sjmcneill{ 681.1Sjmcneill struct fdt_attach_args * const faa = aux; 691.1Sjmcneill 701.1Sjmcneill return of_match_compatible(faa->faa_phandle, compatible); 711.1Sjmcneill} 721.1Sjmcneill 731.1Sjmcneillstatic void 741.1Sjmcneillmeson_rng_attach(device_t parent, device_t self, void *aux) 751.1Sjmcneill{ 761.1Sjmcneill struct meson_rng_softc * const sc = device_private(self); 771.1Sjmcneill struct fdt_attach_args * const faa = aux; 781.1Sjmcneill const int phandle = faa->faa_phandle; 791.1Sjmcneill struct clk *clk; 801.1Sjmcneill bus_addr_t addr; 811.1Sjmcneill bus_size_t size; 821.1Sjmcneill 831.1Sjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 841.1Sjmcneill aprint_error(": couldn't get registers\n"); 851.1Sjmcneill return; 861.1Sjmcneill } 871.1Sjmcneill 881.1Sjmcneill sc->sc_dev = self; 891.1Sjmcneill sc->sc_bst = faa->faa_bst; 901.1Sjmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 911.1Sjmcneill aprint_error(": couldn't map registers\n"); 921.1Sjmcneill return; 931.1Sjmcneill } 941.1Sjmcneill 951.1Sjmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); 961.1Sjmcneill 971.1Sjmcneill clk = fdtbus_clock_get(phandle, "core"); 981.1Sjmcneill if (clk == NULL) { 991.1Sjmcneill aprint_error(": couldn't get core clock\n"); 1001.1Sjmcneill return; 1011.1Sjmcneill } 1021.1Sjmcneill if (clk_enable(clk) != 0) { 1031.1Sjmcneill aprint_error(": couldn't enable core clock\n"); 1041.1Sjmcneill return; 1051.1Sjmcneill } 1061.1Sjmcneill 1071.1Sjmcneill aprint_naive("\n"); 1081.1Sjmcneill aprint_normal(": Hardware RNG\n"); 1091.1Sjmcneill 1101.1Sjmcneill rndsource_setcb(&sc->sc_rndsource, meson_rng_get, sc); 1111.1Sjmcneill rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG, 1121.1Sjmcneill RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB); 1131.1Sjmcneill 1141.1Sjmcneill meson_rng_get(RND_POOLBITS / NBBY, sc); 1151.1Sjmcneill} 1161.1Sjmcneill 1171.1Sjmcneillstatic void 1181.1Sjmcneillmeson_rng_get(size_t bytes_wanted, void *priv) 1191.1Sjmcneill{ 1201.1Sjmcneill struct meson_rng_softc * const sc = priv; 1211.1Sjmcneill uint32_t data[2]; 1221.1Sjmcneill 1231.1Sjmcneill mutex_spin_enter(&sc->sc_lock); 1241.1Sjmcneill while (bytes_wanted) { 1251.1Sjmcneill bus_space_read_region_4(sc->sc_bst, sc->sc_bsh, 0, data, 2); 1261.1Sjmcneill rnd_add_data_sync(&sc->sc_rndsource, data, sizeof(data), 1271.1Sjmcneill sizeof(data) * NBBY); 1281.1Sjmcneill bytes_wanted -= MIN(bytes_wanted, sizeof(data)); 1291.1Sjmcneill } 1301.1Sjmcneill explicit_memset(data, 0, sizeof(data)); 1311.1Sjmcneill mutex_spin_exit(&sc->sc_lock); 1321.1Sjmcneill} 133