ti_rng.c revision 1.4
11.4Sjmcneill/* $NetBSD: ti_rng.c,v 1.4 2020/06/03 15:59:22 jmcneill Exp $ */ 21.1Sjmcneill 31.1Sjmcneill/*- 41.1Sjmcneill * Copyright (c) 2015 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. The name of the author may not be used to endorse or promote products 131.1Sjmcneill * derived from this software without specific prior written permission. 141.1Sjmcneill * 151.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 161.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 171.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 181.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 191.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 201.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 211.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 221.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 231.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 241.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 251.1Sjmcneill * SUCH DAMAGE. 261.1Sjmcneill */ 271.1Sjmcneill 281.1Sjmcneill#include <sys/cdefs.h> 291.4Sjmcneill__KERNEL_RCSID(0, "$NetBSD: ti_rng.c,v 1.4 2020/06/03 15:59:22 jmcneill Exp $"); 301.1Sjmcneill 311.1Sjmcneill#include <sys/param.h> 321.1Sjmcneill#include <sys/systm.h> 331.1Sjmcneill#include <sys/device.h> 341.1Sjmcneill#include <sys/conf.h> 351.1Sjmcneill#include <sys/mutex.h> 361.1Sjmcneill#include <sys/bus.h> 371.1Sjmcneill#include <sys/rndsource.h> 381.1Sjmcneill 391.1Sjmcneill#include <dev/fdt/fdtvar.h> 401.1Sjmcneill 411.1Sjmcneill#include <arm/ti/ti_prcm.h> 421.1Sjmcneill#include <arm/ti/ti_rngreg.h> 431.1Sjmcneill 441.1Sjmcneillstatic const char * const compatible[] = { 451.1Sjmcneill "ti,omap4-rng", 461.1Sjmcneill NULL 471.1Sjmcneill}; 481.1Sjmcneill 491.1Sjmcneillstruct ti_rng_softc { 501.1Sjmcneill device_t sc_dev; 511.1Sjmcneill bus_space_tag_t sc_iot; 521.1Sjmcneill bus_space_handle_t sc_ioh; 531.1Sjmcneill 541.1Sjmcneill kmutex_t sc_lock; 551.1Sjmcneill krndsource_t sc_rndsource; 561.1Sjmcneill}; 571.1Sjmcneill 581.1Sjmcneillstatic int ti_rng_match(device_t, cfdata_t, void *); 591.1Sjmcneillstatic void ti_rng_attach(device_t, device_t, void *); 601.1Sjmcneillstatic void ti_rng_callback(size_t, void *); 611.1Sjmcneill 621.1SjmcneillCFATTACH_DECL_NEW(ti_rng, sizeof(struct ti_rng_softc), 631.1Sjmcneill ti_rng_match, ti_rng_attach, NULL, NULL); 641.1Sjmcneill 651.1Sjmcneill#define RD4(sc, reg) \ 661.1Sjmcneill bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)) 671.1Sjmcneill#define WR4(sc, reg, val) \ 681.1Sjmcneill bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val)) 691.1Sjmcneill 701.1Sjmcneillstatic int 711.1Sjmcneillti_rng_match(device_t parent, cfdata_t match, void *aux) 721.1Sjmcneill{ 731.1Sjmcneill struct fdt_attach_args * const faa = aux; 741.1Sjmcneill 751.1Sjmcneill return of_match_compatible(faa->faa_phandle, compatible); 761.1Sjmcneill} 771.1Sjmcneill 781.1Sjmcneillstatic void 791.1Sjmcneillti_rng_attach(device_t parent, device_t self, void *aux) 801.1Sjmcneill{ 811.1Sjmcneill struct ti_rng_softc *sc = device_private(self); 821.1Sjmcneill struct fdt_attach_args * const faa = aux; 831.1Sjmcneill const int phandle = faa->faa_phandle; 841.1Sjmcneill bus_addr_t addr; 851.1Sjmcneill bus_size_t size; 861.1Sjmcneill 871.1Sjmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 881.1Sjmcneill aprint_error(": couldn't get registers\n"); 891.1Sjmcneill return; 901.1Sjmcneill } 911.1Sjmcneill 921.2Sjmcneill if (ti_prcm_enable_hwmod(phandle, 0) != 0) { 931.1Sjmcneill aprint_error(": couldn't enable module\n"); 941.1Sjmcneill return; 951.1Sjmcneill } 961.1Sjmcneill 971.1Sjmcneill sc->sc_dev = self; 981.1Sjmcneill sc->sc_iot = faa->faa_bst; 991.1Sjmcneill if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) { 1001.1Sjmcneill aprint_error(": couldn't map registers\n"); 1011.1Sjmcneill return; 1021.1Sjmcneill } 1031.1Sjmcneill 1041.1Sjmcneill mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); 1051.1Sjmcneill 1061.1Sjmcneill if ((RD4(sc, TRNG_CONTROL_REG) & TRNG_CONTROL_ENABLE) == 0) { 1071.1Sjmcneill WR4(sc, TRNG_CONFIG_REG, 1081.1Sjmcneill __SHIFTIN(0x21, TRNG_CONFIG_MIN_REFILL) | 1091.1Sjmcneill __SHIFTIN(0x22, TRNG_CONFIG_MAX_REFILL)); 1101.1Sjmcneill WR4(sc, TRNG_CONTROL_REG, 1111.1Sjmcneill __SHIFTIN(0x21, TRNG_CONTROL_STARTUP_CYCLES) | 1121.1Sjmcneill TRNG_CONTROL_ENABLE); 1131.1Sjmcneill } 1141.1Sjmcneill 1151.4Sjmcneill aprint_naive("\n"); 1161.4Sjmcneill aprint_normal(": RNG\n"); 1171.4Sjmcneill 1181.1Sjmcneill rndsource_setcb(&sc->sc_rndsource, ti_rng_callback, sc); 1191.1Sjmcneill rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG, 1201.1Sjmcneill RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB); 1211.1Sjmcneill} 1221.1Sjmcneill 1231.1Sjmcneillstatic void 1241.1Sjmcneillti_rng_callback(size_t bytes_wanted, void *priv) 1251.1Sjmcneill{ 1261.1Sjmcneill struct ti_rng_softc * const sc = priv; 1271.1Sjmcneill uint32_t buf[2]; 1281.1Sjmcneill u_int retry; 1291.1Sjmcneill 1301.1Sjmcneill mutex_enter(&sc->sc_lock); 1311.1Sjmcneill while (bytes_wanted) { 1321.1Sjmcneill for (retry = 10; retry > 0; retry--) { 1331.1Sjmcneill if (RD4(sc, TRNG_STATUS_REG) & TRNG_STATUS_READY) 1341.1Sjmcneill break; 1351.1Sjmcneill delay(10); 1361.1Sjmcneill } 1371.1Sjmcneill if (retry == 0) 1381.1Sjmcneill break; 1391.1Sjmcneill buf[0] = RD4(sc, TRNG_OUTPUT_L_REG); 1401.1Sjmcneill buf[1] = RD4(sc, TRNG_OUTPUT_H_REG); 1411.1Sjmcneill WR4(sc, TRNG_INTACK_REG, TRNG_INTACK_READY); 1421.1Sjmcneill rnd_add_data_sync(&sc->sc_rndsource, buf, sizeof(buf), 1431.1Sjmcneill sizeof(buf) * NBBY); 1441.1Sjmcneill bytes_wanted -= MIN(bytes_wanted, sizeof(buf)); 1451.1Sjmcneill } 1461.1Sjmcneill explicit_memset(buf, 0, sizeof(buf)); 1471.1Sjmcneill mutex_exit(&sc->sc_lock); 1481.1Sjmcneill} 149