1 /* $OpenBSD: octrng.c,v 1.5 2014/05/07 14:44:54 pirofti 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 (aa->aa_name == NULL || 80 strcmp(aa->aa_name, cf->cf_driver->cd_name) != 0) 81 return (0); 82 83 84 return (1); 85 } 86 87 void 88 octrng_attach(struct device *parent, struct device *self, void *aux) 89 { 90 struct octrng_softc *sc = (void *)self; 91 sc->sc_io = aux; 92 93 uint64_t control_reg; 94 95 sc->sc_iot = sc->sc_io->aa_bust; 96 97 if (bus_space_map(sc->sc_iot, OCTEON_RNG_BASE, OCTRNG_MAP_SIZE, 0, 98 &sc->sc_ioh)) { 99 printf(": can't map registers"); 100 } 101 102 control_reg = octeon_xkphys_read_8(OCTRNG_CONTROL_ADDR); 103 control_reg |= (OCTRNG_ENABLE_OUTPUT | OCTRNG_ENABLE_ENTROPY); 104 octeon_xkphys_write_8(OCTRNG_CONTROL_ADDR, control_reg); 105 106 timeout_set(&sc->sc_to, octrng_rnd, sc); 107 108 timeout_add_sec(&sc->sc_to, 5); 109 110 printf("\n"); 111 } 112 113 void 114 octrng_rnd(void *arg) 115 { 116 struct octrng_softc *sc = arg; 117 uint64_t value; 118 119 value = bus_space_read_8(sc->sc_iot, sc->sc_ioh, OCTRNG_ENTROPY_REG); 120 121 DPRINTF(("%#llX ", value)); /* WARNING: very verbose */ 122 123 add_true_randomness(value); 124 timeout_add_msec(&sc->sc_to, 10); 125 } 126