1 /* $NetBSD: rng200.c,v 1.2 2020/04/30 03:40:53 riastradh 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/rndsource.h> 40 41 #include <dev/ic/rng200var.h> 42 #include <dev/ic/rng200reg.h> 43 44 #define READ4(sc, r) \ 45 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (r)) 46 47 #define WRITE4(sc, r, v) \ 48 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (r), (v)) 49 50 static void 51 rng200_reset(struct rng200_softc *sc) 52 { 53 uint32_t ctl, rng, rbg; 54 55 /* Disable RBG */ 56 ctl = READ4(sc, RNG200_CONTROL); 57 ctl &= ~RNG200_RBG_MASK; 58 WRITE4(sc, RNG200_CONTROL, ctl); 59 60 /* Clear interrupts */ 61 WRITE4(sc, RNG200_STATUS, 0xffffffff); 62 63 /* Reset RNG and RBG */ 64 rbg = READ4(sc, RNG200_RBG_RESET); 65 rng = READ4(sc, RNG200_RNG_RESET); 66 WRITE4(sc, RNG200_RBG_RESET, rbg | RBG_RESET); 67 WRITE4(sc, RNG200_RNG_RESET, rng | RNG_RESET); 68 WRITE4(sc, RNG200_RNG_RESET, rng); 69 WRITE4(sc, RNG200_RBG_RESET, rbg); 70 71 /* Enable RBG */ 72 WRITE4(sc, RNG200_CONTROL, ctl | RNG200_RBG_ENABLE); 73 } 74 75 static void 76 rng200_get(size_t bytes_wanted, void *priv) 77 { 78 struct rng200_softc * const sc = priv; 79 uint32_t w, data; 80 unsigned count; 81 82 mutex_spin_enter(&sc->sc_lock); 83 while (bytes_wanted) { 84 85 w = READ4(sc, RNG200_STATUS); 86 if ((w & (RNG200_MASTER_FAIL | RNG200_NIST_FAIL)) != 0) 87 rng200_reset(sc); 88 89 w = READ4(sc, RNG200_COUNT); 90 count = __SHIFTOUT(w, RNG200_COUNT_MASK); 91 92 if (count == 0) 93 break; 94 95 data = READ4(sc, RNG200_DATA); 96 rnd_add_data_sync(&sc->sc_rndsource, &data, 97 sizeof(data), sizeof(data) * NBBY); 98 bytes_wanted -= MIN(bytes_wanted, sizeof(data)); 99 } 100 explicit_memset(&data, 0, sizeof(data)); 101 mutex_spin_exit(&sc->sc_lock); 102 } 103 104 void 105 rng200_attach(struct rng200_softc *sc) 106 { 107 108 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); 109 110 rndsource_setcb(&sc->sc_rndsource, rng200_get, sc); 111 rnd_attach_source(&sc->sc_rndsource, sc->sc_name, 112 RND_TYPE_RNG, RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB); 113 } 114 115 void 116 rng200_detach(struct rng200_softc *sc) 117 { 118 119 rnd_detach_source(&sc->sc_rndsource); 120 mutex_destroy(&sc->sc_lock); 121 } 122 123