1 /* $NetBSD: ingenic_rng.c,v 1.4 2016/02/17 20:12:42 macallan Exp $ */ 2 3 /*- 4 * Copyright (c) 2015 Michael McConville 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: ingenic_rng.c,v 1.4 2016/02/17 20:12:42 macallan Exp $"); 31 32 /* 33 * adapted from Jared McNeill's amlogic_rng.c 34 */ 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/device.h> 39 #include <sys/mutex.h> 40 #include <sys/kernel.h> 41 #include <sys/mutex.h> 42 #include <sys/bus.h> 43 #include <sys/rndpool.h> 44 #include <sys/rndsource.h> 45 46 #include <mips/ingenic/ingenic_var.h> 47 #include <mips/ingenic/ingenic_regs.h> 48 49 #include "opt_ingenic.h" 50 51 struct ingenic_rng_softc; 52 53 static int ingenic_rng_match(device_t, cfdata_t, void *); 54 static void ingenic_rng_attach(device_t, device_t, void *); 55 56 static void ingenic_rng_get(size_t, void *); 57 58 struct ingenic_rng_softc { 59 device_t sc_dev; 60 bus_space_tag_t sc_bst; 61 bus_space_handle_t sc_bsh; 62 63 kmutex_t sc_lock; 64 krndsource_t sc_rndsource; 65 }; 66 67 CFATTACH_DECL_NEW(ingenic_rng, sizeof(struct ingenic_rng_softc), 68 ingenic_rng_match, ingenic_rng_attach, NULL, NULL); 69 70 static int 71 ingenic_rng_match(device_t parent, cfdata_t cf, void *aux) 72 { 73 const struct apbus_attach_args *aa = aux; 74 75 return !(strcmp(aa->aa_name, "jzrng")); 76 } 77 78 static void 79 ingenic_rng_attach(device_t parent, device_t self, void *aux) 80 { 81 struct ingenic_rng_softc * const sc = device_private(self); 82 const struct apbus_attach_args * const aa = aux; 83 bus_addr_t addr = aa->aa_addr; 84 int error; 85 86 sc->sc_dev = self; 87 sc->sc_bst = aa->aa_bst; 88 if (addr == 0) 89 addr = JZ_RNG; 90 91 error = bus_space_map(aa->aa_bst, addr, 4, 0, &sc->sc_bsh); 92 if (error) { 93 aprint_error_dev(self, 94 "can't map registers for %s: %d\n", aa->aa_name, error); 95 return; 96 } 97 98 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM); 99 100 aprint_naive(": Ingenic random number generator\n"); 101 aprint_normal(": Ingenic random number generator\n"); 102 103 rndsource_setcb(&sc->sc_rndsource, ingenic_rng_get, sc); 104 rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG, 105 RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB); 106 107 ingenic_rng_get(RND_POOLBITS / NBBY, sc); 108 } 109 110 static void 111 ingenic_rng_get(size_t bytes_wanted, void *priv) 112 { 113 struct ingenic_rng_softc * const sc = priv; 114 uint32_t data; 115 116 mutex_spin_enter(&sc->sc_lock); 117 while (bytes_wanted) { 118 data = bus_space_read_4(sc->sc_bst, sc->sc_bsh, 0); 119 delay(1); 120 rnd_add_data_sync(&sc->sc_rndsource, &data, sizeof(data), 121 sizeof(data) * NBBY); 122 bytes_wanted -= MIN(bytes_wanted, sizeof(data)); 123 } 124 explicit_memset(&data, 0, sizeof(data)); 125 mutex_spin_exit(&sc->sc_lock); 126 } 127