1 /* $OpenBSD: octrng.c,v 1.6 2015/07/19 21:11:47 jasper Exp $ */ 2 /* 3 * Copyright (c) 2013 Paul Irofti <pirofti@openbsd.org> 4 * 5 * Permission to use, copy, modify, and/or 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 <dev/rndvar.h> 24 25 #include <machine/octeonvar.h> 26 #include <machine/octeonreg.h> 27 #include <octeon/dev/iobusvar.h> 28 29 int octrng_match(struct device *, void *, void *); 30 void octrng_attach(struct device *, struct device *, void *); 31 void octrng_rnd(void *arg); 32 33 #ifdef OCTRNG_DEBUG 34 #define DPRINTF(x) printf x 35 #else 36 #define DPRINTF(x) 37 #endif 38 39 #define OCTRNG_MAP_SIZE 8ULL 40 41 /* OCTRNG_ENTROPY_ADDR 0x8001400000000000ULL */ 42 #define OCTRNG_MAJORDID 8ULL 43 #define OCTRNG_ENTROPY_ADDR (((uint64_t)1 << 48) | \ 44 ((uint64_t)(OCTRNG_MAJORDID & 0x1f) << 43)) 45 #define OCTRNG_ENTROPY_REG 0 46 47 #define OCTRNG_CONTROL_ADDR 0x0001180040000000ULL 48 49 #define OCTRNG_RESET (1ULL << 3) 50 #define OCTRNG_ENABLE_OUTPUT (1ULL << 1) 51 #define OCTRNG_ENABLE_ENTROPY (1ULL << 0) 52 53 struct octrng_softc { 54 struct device sc_dev; 55 struct timeout sc_to; 56 struct iobus_attach_args *sc_io; 57 58 bus_space_tag_t sc_iot; 59 bus_space_handle_t sc_ioh; 60 }; 61 62 struct cfattach octrng_ca = { 63 sizeof(struct octrng_softc), octrng_match, octrng_attach 64 }; 65 66 struct cfdriver octrng_cd = { 67 NULL, "octrng", DV_DULL 68 }; 69 70 71 int 72 octrng_match(struct device *parent, void *match, void *aux) 73 { 74 struct iobus_attach_args *aa = aux; 75 struct cfdata *cf = match; 76 77 /* XXX: check for board type */ 78 79 if (strcmp(aa->aa_name, cf->cf_driver->cd_name) != 0) 80 return (0); 81 82 return (1); 83 } 84 85 void 86 octrng_attach(struct device *parent, struct device *self, void *aux) 87 { 88 struct octrng_softc *sc = (void *)self; 89 sc->sc_io = aux; 90 91 uint64_t control_reg; 92 93 sc->sc_iot = sc->sc_io->aa_bust; 94 95 if (bus_space_map(sc->sc_iot, OCTEON_RNG_BASE, OCTRNG_MAP_SIZE, 0, 96 &sc->sc_ioh)) { 97 printf(": can't map registers"); 98 } 99 100 control_reg = octeon_xkphys_read_8(OCTRNG_CONTROL_ADDR); 101 control_reg |= (OCTRNG_ENABLE_OUTPUT | OCTRNG_ENABLE_ENTROPY); 102 octeon_xkphys_write_8(OCTRNG_CONTROL_ADDR, control_reg); 103 104 timeout_set(&sc->sc_to, octrng_rnd, sc); 105 106 timeout_add_sec(&sc->sc_to, 5); 107 108 printf("\n"); 109 } 110 111 void 112 octrng_rnd(void *arg) 113 { 114 struct octrng_softc *sc = arg; 115 uint64_t value; 116 117 value = bus_space_read_8(sc->sc_iot, sc->sc_ioh, OCTRNG_ENTROPY_REG); 118 119 DPRINTF(("%#llX ", value)); /* WARNING: very verbose */ 120 121 add_true_randomness(value); 122 timeout_add_msec(&sc->sc_to, 10); 123 } 124