1 /* $OpenBSD: amlrng.c,v 1.1 2019/08/27 22:21:52 kettenis Exp $ */ 2 /* 3 * Copyright (c) 2019 Mark Kettenis <kettenis@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/param.h> 19 #include <sys/systm.h> 20 #include <sys/device.h> 21 #include <sys/timeout.h> 22 23 #include <machine/bus.h> 24 #include <machine/fdt.h> 25 26 #include <dev/rndvar.h> 27 #include <dev/ofw/openfirm.h> 28 #include <dev/ofw/fdt.h> 29 30 /* Registers */ 31 #define RNG_DATA 0x0000 32 33 #define HREAD4(sc, reg) \ 34 (bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))) 35 36 struct amlrng_softc { 37 struct device sc_dev; 38 bus_space_tag_t sc_iot; 39 bus_space_handle_t sc_ioh; 40 41 struct timeout sc_to; 42 }; 43 44 int amlrng_match(struct device *, void *, void *); 45 void amlrng_attach(struct device *, struct device *, void *); 46 47 struct cfattach amlrng_ca = { 48 sizeof (struct amlrng_softc), amlrng_match, amlrng_attach 49 }; 50 51 struct cfdriver amlrng_cd = { 52 NULL, "amlrng", DV_DULL 53 }; 54 55 void amlrng_rnd(void *); 56 57 int 58 amlrng_match(struct device *parent, void *match, void *aux) 59 { 60 struct fdt_attach_args *faa = aux; 61 62 return OF_is_compatible(faa->fa_node, "amlogic,meson-rng"); 63 } 64 65 void 66 amlrng_attach(struct device *parent, struct device *self, void *aux) 67 { 68 struct amlrng_softc *sc = (struct amlrng_softc *)self; 69 struct fdt_attach_args *faa = aux; 70 71 if (faa->fa_nreg < 1) { 72 printf(": no registers\n"); 73 return; 74 } 75 76 sc->sc_iot = faa->fa_iot; 77 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, 78 faa->fa_reg[0].size, 0, &sc->sc_ioh)) { 79 printf(": can't map registers\n"); 80 return; 81 } 82 83 printf("\n"); 84 85 timeout_set(&sc->sc_to, amlrng_rnd, sc); 86 amlrng_rnd(sc); 87 } 88 89 void 90 amlrng_rnd(void *arg) 91 { 92 struct amlrng_softc *sc = arg; 93 94 enqueue_randomness(HREAD4(sc, RNG_DATA)); 95 timeout_add_sec(&sc->sc_to, 1); 96 } 97