1 /* $OpenBSD: bcm2835_rng.c,v 1.3 2020/05/29 04:42:25 deraadt Exp $ */ 2 /* 3 * Copyright (c) 2018 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_EN (1 << 0) 32 #define RNG_STATUS 0x0004 33 #define RNG_STATUS_COUNT(x) ((x) >> 24) 34 #define RNG_DATA 0x0008 35 36 #define HREAD4(sc, reg) \ 37 (bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))) 38 #define HWRITE4(sc, reg, val) \ 39 bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val)) 40 41 struct bcmrng_softc { 42 struct device sc_dev; 43 bus_space_tag_t sc_iot; 44 bus_space_handle_t sc_ioh; 45 46 struct timeout sc_to; 47 }; 48 49 int bcmrng_match(struct device *, void *, void *); 50 void bcmrng_attach(struct device *, struct device *, void *); 51 52 struct cfattach bcmrng_ca = { 53 sizeof (struct bcmrng_softc), bcmrng_match, bcmrng_attach 54 }; 55 56 struct cfdriver bcmrng_cd = { 57 NULL, "bcmrng", DV_DULL 58 }; 59 60 void bcmrng_rnd(void *); 61 62 int 63 bcmrng_match(struct device *parent, void *match, void *aux) 64 { 65 struct fdt_attach_args *faa = aux; 66 67 return OF_is_compatible(faa->fa_node, "brcm,bcm2835-rng"); 68 } 69 70 void 71 bcmrng_attach(struct device *parent, struct device *self, void *aux) 72 { 73 struct bcmrng_softc *sc = (struct bcmrng_softc *)self; 74 struct fdt_attach_args *faa = aux; 75 76 if (faa->fa_nreg < 1) { 77 printf(": no registers\n"); 78 return; 79 } 80 81 sc->sc_iot = faa->fa_iot; 82 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr, 83 faa->fa_reg[0].size, 0, &sc->sc_ioh)) { 84 printf(": can't map registers\n"); 85 return; 86 } 87 88 printf("\n"); 89 90 /* Discard initial numbers which may be "less" random. */ 91 HWRITE4(sc, RNG_STATUS, 250000); 92 HWRITE4(sc, RNG_CTRL, RNG_CTRL_EN); 93 94 timeout_set(&sc->sc_to, bcmrng_rnd, sc); 95 bcmrng_rnd(sc); 96 } 97 98 void 99 bcmrng_rnd(void *arg) 100 { 101 struct bcmrng_softc *sc = arg; 102 uint32_t status, data; 103 int i, count; 104 105 status = HREAD4(sc, RNG_STATUS); 106 count = MIN(4, RNG_STATUS_COUNT(status)); 107 for (i = 0; i < count; i++) { 108 data = HREAD4(sc, RNG_DATA); 109 enqueue_randomness(data); 110 } 111 112 timeout_add_sec(&sc->sc_to, 1); 113 } 114