1 /* $NetBSD: meson_rng.c,v 1.2 2019/04/21 14:13:55 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2015-2019 Jared D. McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: meson_rng.c,v 1.2 2019/04/21 14:13:55 jmcneill Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/bus.h> 34 #include <sys/device.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/mutex.h> 38 #include <sys/rndpool.h> 39 #include <sys/rndsource.h> 40 41 #include <dev/fdt/fdtvar.h> 42 43 static int meson_rng_match(device_t, cfdata_t, void *); 44 static void meson_rng_attach(device_t, device_t, void *); 45 46 static void meson_rng_get(size_t, void *); 47 48 struct meson_rng_softc { 49 device_t sc_dev; 50 bus_space_tag_t sc_bst; 51 bus_space_handle_t sc_bsh; 52 53 kmutex_t sc_lock; 54 krndsource_t sc_rndsource; 55 }; 56 57 static const char * const compatible[] = { 58 "amlogic,meson-rng", 59 NULL 60 }; 61 62 CFATTACH_DECL_NEW(meson_rng, sizeof(struct meson_rng_softc), 63 meson_rng_match, meson_rng_attach, NULL, NULL); 64 65 static int 66 meson_rng_match(device_t parent, cfdata_t cf, void *aux) 67 { 68 struct fdt_attach_args * const faa = aux; 69 70 return of_match_compatible(faa->faa_phandle, compatible); 71 } 72 73 static void 74 meson_rng_attach(device_t parent, device_t self, void *aux) 75 { 76 struct meson_rng_softc * const sc = device_private(self); 77 struct fdt_attach_args * const faa = aux; 78 const int phandle = faa->faa_phandle; 79 struct clk *clk; 80 bus_addr_t addr; 81 bus_size_t size; 82 83 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 84 aprint_error(": couldn't get registers\n"); 85 return; 86 } 87 88 sc->sc_dev = self; 89 sc->sc_bst = faa->faa_bst; 90 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 91 aprint_error(": couldn't map registers\n"); 92 return; 93 } 94 95 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); 96 97 /* Core clock is optional */ 98 clk = fdtbus_clock_get(phandle, "core"); 99 if (clk != NULL && clk_enable(clk) != 0) { 100 aprint_error(": couldn't enable core clock\n"); 101 return; 102 } 103 104 aprint_naive("\n"); 105 aprint_normal(": Hardware RNG\n"); 106 107 rndsource_setcb(&sc->sc_rndsource, meson_rng_get, sc); 108 rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG, 109 RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB); 110 111 meson_rng_get(RND_POOLBITS / NBBY, sc); 112 } 113 114 static void 115 meson_rng_get(size_t bytes_wanted, void *priv) 116 { 117 struct meson_rng_softc * const sc = priv; 118 uint32_t data; 119 120 mutex_spin_enter(&sc->sc_lock); 121 while (bytes_wanted) { 122 data = bus_space_read_4(sc->sc_bst, sc->sc_bsh, 0); 123 rnd_add_data_sync(&sc->sc_rndsource, &data, sizeof(data), 124 sizeof(data) * NBBY); 125 bytes_wanted -= MIN(bytes_wanted, sizeof(data)); 126 } 127 explicit_memset(&data, 0, sizeof(data)); 128 mutex_spin_exit(&sc->sc_lock); 129 } 130