Home | History | Annotate | Line # | Download | only in broadcom
      1 /*	$NetBSD: bcm2835_rng.c,v 1.17 2022/03/19 11:55:03 riastradh Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jared D. McNeill
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: bcm2835_rng.c,v 1.17 2022/03/19 11:55:03 riastradh Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/device.h>
     38 #include <sys/kernel.h>
     39 #include <sys/bus.h>
     40 #include <sys/rndsource.h>
     41 
     42 #include <arm/broadcom/bcm2835reg.h>
     43 #include <arm/broadcom/bcm2835_intr.h>
     44 
     45 #include <dev/fdt/fdtvar.h>
     46 
     47 #define RNG_CTRL		0x00
     48 #define  RNG_CTRL_EN		__BIT(0)
     49 #define RNG_STATUS		0x04
     50 #define  RNG_STATUS_CNT		__BITS(31,24)
     51 #define RNG_DATA		0x08
     52 
     53 #define RNG_DATA_MAX		256
     54 
     55 struct bcm2835rng_softc {
     56 	device_t		sc_dev;
     57 
     58 	bus_space_tag_t		sc_iot;
     59 	bus_space_handle_t	sc_ioh;
     60 
     61 	krndsource_t		sc_rndsource;
     62 };
     63 
     64 static int bcmrng_match(device_t, cfdata_t, void *);
     65 static void bcmrng_attach(device_t, device_t, void *);
     66 static void bcmrng_get(size_t, void *);
     67 
     68 CFATTACH_DECL_NEW(bcmrng_fdt, sizeof(struct bcm2835rng_softc),
     69     bcmrng_match, bcmrng_attach, NULL, NULL);
     70 
     71 static const struct device_compatible_entry compat_data[] = {
     72 	{ .compat = "brcm,bcm2835-rng" },
     73 	DEVICE_COMPAT_EOL
     74 };
     75 
     76 /* ARGSUSED */
     77 static int
     78 bcmrng_match(device_t parent, cfdata_t match, void *aux)
     79 {
     80 	struct fdt_attach_args * const faa = aux;
     81 
     82 	return of_compatible_match(faa->faa_phandle, compat_data);
     83 }
     84 
     85 static void
     86 bcmrng_attach(device_t parent, device_t self, void *aux)
     87 {
     88 	struct bcm2835rng_softc *sc = device_private(self);
     89 	struct fdt_attach_args * const faa = aux;
     90 	const int phandle = faa->faa_phandle;
     91 	uint32_t ctrl;
     92 	bus_addr_t addr;
     93 	bus_size_t size;
     94 	int error;
     95 
     96 	aprint_naive("\n");
     97 	aprint_normal(": RNG\n");
     98 
     99 	sc->sc_dev = self;
    100 	sc->sc_iot = faa->faa_bst;
    101 
    102 	error = fdtbus_get_reg(phandle, 0, &addr, &size);
    103 	if (error) {
    104 		aprint_error_dev(sc->sc_dev, "unable to map device\n");
    105 		return;
    106 	}
    107 
    108 	if (bus_space_map(sc->sc_iot, addr, size, 0,
    109 	    &sc->sc_ioh)) {
    110 		aprint_error_dev(sc->sc_dev, "unable to map device\n");
    111 		return;
    112 	}
    113 
    114 	/* discard initial numbers, broadcom says they are "less random" */
    115 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS, 0x40000);
    116 
    117 	/* enable rng */
    118 	ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL);
    119 	ctrl |= RNG_CTRL_EN;
    120 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, RNG_CTRL, ctrl);
    121 
    122 	/* set up an rndsource */
    123 	rndsource_setcb(&sc->sc_rndsource, &bcmrng_get, sc);
    124 	rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
    125 	    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
    126 }
    127 
    128 static void
    129 bcmrng_get(size_t bytes_wanted, void *arg)
    130 {
    131 	struct bcm2835rng_softc *sc = arg;
    132 	uint32_t status, cnt;
    133 	uint32_t buf[RNG_DATA_MAX]; /* 1k on the stack */
    134 
    135 	while (bytes_wanted) {
    136 		status = bus_space_read_4(sc->sc_iot, sc->sc_ioh, RNG_STATUS);
    137 		cnt = __SHIFTOUT(status, RNG_STATUS_CNT);
    138 		KASSERT(cnt < RNG_DATA_MAX);
    139 		if (cnt == 0)
    140 			continue;	/* XXX Busy-waiting seems wrong...  */
    141 		bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh, RNG_DATA, buf,
    142 		    cnt);
    143 		rnd_add_data_sync(&sc->sc_rndsource, buf, (cnt * 4),
    144 		    (cnt * 4 * NBBY));
    145 		bytes_wanted -= MIN(bytes_wanted, (cnt * 4));
    146 	}
    147 	explicit_memset(buf, 0, sizeof(buf));
    148 }
    149