1 /* $NetBSD: rng200.c,v 1.1 2019/09/01 14:44:14 mlelstv Exp $ */ 2 3 /* 4 * Copyright (c) 2019 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Michael van Elst 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Driver for the Broadcom iProc RNG200 34 */ 35 36 #include <sys/param.h> 37 #include <sys/bus.h> 38 #include <sys/mutex.h> 39 #include <sys/rndpool.h> 40 #include <sys/rndsource.h> 41 42 #include <dev/ic/rng200var.h> 43 #include <dev/ic/rng200reg.h> 44 45 #define READ4(sc, r) \ 46 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (r)) 47 48 #define WRITE4(sc, r, v) \ 49 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (r), (v)) 50 51 static void 52 rng200_reset(struct rng200_softc *sc) 53 { 54 uint32_t ctl, rng, rbg; 55 56 /* Disable RBG */ 57 ctl = READ4(sc, RNG200_CONTROL); 58 ctl &= ~RNG200_RBG_MASK; 59 WRITE4(sc, RNG200_CONTROL, ctl); 60 61 /* Clear interrupts */ 62 WRITE4(sc, RNG200_STATUS, 0xffffffff); 63 64 /* Reset RNG and RBG */ 65 rbg = READ4(sc, RNG200_RBG_RESET); 66 rng = READ4(sc, RNG200_RNG_RESET); 67 WRITE4(sc, RNG200_RBG_RESET, rbg | RBG_RESET); 68 WRITE4(sc, RNG200_RNG_RESET, rng | RNG_RESET); 69 WRITE4(sc, RNG200_RNG_RESET, rng); 70 WRITE4(sc, RNG200_RBG_RESET, rbg); 71 72 /* Enable RBG */ 73 WRITE4(sc, RNG200_CONTROL, ctl | RNG200_RBG_ENABLE); 74 } 75 76 static void 77 rng200_get(size_t bytes_wanted, void *priv) 78 { 79 struct rng200_softc * const sc = priv; 80 uint32_t w, data; 81 unsigned count; 82 83 mutex_spin_enter(&sc->sc_lock); 84 while (bytes_wanted) { 85 86 w = READ4(sc, RNG200_STATUS); 87 if ((w & (RNG200_MASTER_FAIL | RNG200_NIST_FAIL)) != 0) 88 rng200_reset(sc); 89 90 w = READ4(sc, RNG200_COUNT); 91 count = __SHIFTOUT(w, RNG200_COUNT_MASK); 92 93 if (count == 0) 94 break; 95 96 data = READ4(sc, RNG200_DATA); 97 rnd_add_data_sync(&sc->sc_rndsource, &data, 98 sizeof(data), sizeof(data) * NBBY); 99 bytes_wanted -= MIN(bytes_wanted, sizeof(data)); 100 } 101 explicit_memset(&data, 0, sizeof(data)); 102 mutex_spin_exit(&sc->sc_lock); 103 } 104 105 void 106 rng200_attach(struct rng200_softc *sc) 107 { 108 109 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); 110 111 rndsource_setcb(&sc->sc_rndsource, rng200_get, sc); 112 rnd_attach_source(&sc->sc_rndsource, sc->sc_name, 113 RND_TYPE_RNG, RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB); 114 115 rng200_get(RND_POOLBITS / NBBY, sc); 116 } 117 118 void 119 rng200_detach(struct rng200_softc *sc) 120 { 121 122 rnd_detach_source(&sc->sc_rndsource); 123 mutex_destroy(&sc->sc_lock); 124 } 125 126