1 /* $OpenBSD: bcm2711_rng.c,v 1.3 2022/04/06 18:59:28 naddy Exp $ */ 2 /* 3 * Copyright (c) 2020 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/ofw/openfirm.h> 27 #include <dev/ofw/fdt.h> 28 29 /* Registers */ 30 #define RNG_CTRL 0x0000 31 #define RNG_CTRL_RBGEN_MASK (0x1fff << 0) 32 #define RNG_CTRL_RBGEN_EN (1 << 0) 33 #define RNG_FIFO_DATA 0x0020 34 #define RNG_FIFO_COUNT 0x0024 35 #define RNG_FIFO_COUNT_MASK (0xff << 0) 36 37 #define HREAD4(sc, reg) \ 38 (bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))) 39 #define HWRITE4(sc, reg, val) \ 40 bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val)) 41 42 struct bcmirng_softc { 43 struct device sc_dev; 44 bus_space_tag_t sc_iot; 45 bus_space_handle_t sc_ioh; 46 47 struct timeout sc_to; 48 }; 49 50 int bcmirng_match(struct device *, void *, void *); 51 void bcmirng_attach(struct device *, struct device *, void *); 52 53 const struct cfattach bcmirng_ca = { 54 sizeof (struct bcmirng_softc), bcmirng_match, bcmirng_attach 55 }; 56 57 struct cfdriver bcmirng_cd = { 58 NULL, "bcmirng", DV_DULL 59 }; 60 61 void bcmirng_rnd(void *); 62 63 int 64 bcmirng_match(struct device *parent, void *match, void *aux) 65 { 66 struct fdt_attach_args *faa = aux; 67 68 return (OF_is_compatible(faa->fa_node, "brcm,bcm2711-rng200") || 69 OF_is_compatible(faa->fa_node, "brcm,bcm2838-rng200")); 70 } 71 72 void 73 bcmirng_attach(struct device *parent, struct device *self, void *aux) 74 { 75 struct bcmirng_softc *sc = (struct bcmirng_softc *)self; 76 struct fdt_attach_args *faa = aux; 77 78 if (faa->fa_nreg < 1) { 79 printf(": no registers\n"); 80 return; 81 } 82 83 sc->sc_iot = faa->fa_iot; 84 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, 85 faa->fa_reg[0].size, 0, &sc->sc_ioh)) { 86 printf(": can't map registers\n"); 87 return; 88 } 89 90 printf("\n"); 91 92 HWRITE4(sc, RNG_CTRL, RNG_CTRL_RBGEN_EN); 93 94 timeout_set(&sc->sc_to, bcmirng_rnd, sc); 95 bcmirng_rnd(sc); 96 } 97 98 void 99 bcmirng_rnd(void *arg) 100 { 101 struct bcmirng_softc *sc = arg; 102 uint32_t data; 103 int count, i; 104 105 count = MAX(4, HREAD4(sc, RNG_FIFO_COUNT) & RNG_FIFO_COUNT_MASK); 106 for (i = 0; i < count; i++) { 107 data = HREAD4(sc, RNG_FIFO_DATA); 108 enqueue_randomness(data); 109 } 110 111 timeout_add_sec(&sc->sc_to, 1); 112 } 113