Home | History | Annotate | Line # | Download | only in amlogic
      1 /* $NetBSD: meson_rng.c,v 1.5 2022/03/19 11:36:43 riastradh Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015-2019 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: meson_rng.c,v 1.5 2022/03/19 11:36:43 riastradh Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/device.h>
     35 #include <sys/systm.h>
     36 #include <sys/kernel.h>
     37 #include <sys/mutex.h>
     38 #include <sys/rndsource.h>
     39 
     40 #include <dev/fdt/fdtvar.h>
     41 
     42 static int	meson_rng_match(device_t, cfdata_t, void *);
     43 static void	meson_rng_attach(device_t, device_t, void *);
     44 
     45 static void	meson_rng_get(size_t, void *);
     46 
     47 struct meson_rng_softc {
     48 	device_t		sc_dev;
     49 	bus_space_tag_t		sc_bst;
     50 	bus_space_handle_t	sc_bsh;
     51 
     52 	krndsource_t		sc_rndsource;
     53 };
     54 
     55 static const struct device_compatible_entry compat_data[] = {
     56 	{ .compat = "amlogic,meson-rng" },
     57 	DEVICE_COMPAT_EOL
     58 };
     59 
     60 CFATTACH_DECL_NEW(meson_rng, sizeof(struct meson_rng_softc),
     61 	meson_rng_match, meson_rng_attach, NULL, NULL);
     62 
     63 static int
     64 meson_rng_match(device_t parent, cfdata_t cf, void *aux)
     65 {
     66 	struct fdt_attach_args * const faa = aux;
     67 
     68 	return of_compatible_match(faa->faa_phandle, compat_data);
     69 }
     70 
     71 static void
     72 meson_rng_attach(device_t parent, device_t self, void *aux)
     73 {
     74 	struct meson_rng_softc * const sc = device_private(self);
     75 	struct fdt_attach_args * const faa = aux;
     76 	const int phandle = faa->faa_phandle;
     77 	struct clk *clk;
     78 	bus_addr_t addr;
     79 	bus_size_t size;
     80 
     81 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
     82 		aprint_error(": couldn't get registers\n");
     83 		return;
     84 	}
     85 
     86 	sc->sc_dev = self;
     87 	sc->sc_bst = faa->faa_bst;
     88 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
     89 		aprint_error(": couldn't map registers\n");
     90 		return;
     91 	}
     92 
     93 	/* Core clock is optional */
     94 	clk = fdtbus_clock_get(phandle, "core");
     95 	if (clk != NULL && clk_enable(clk) != 0) {
     96 		aprint_error(": couldn't enable core clock\n");
     97 		return;
     98 	}
     99 
    100 	aprint_naive("\n");
    101 	aprint_normal(": Hardware RNG\n");
    102 
    103 	rndsource_setcb(&sc->sc_rndsource, meson_rng_get, sc);
    104 	rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
    105 	    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
    106 }
    107 
    108 static void
    109 meson_rng_get(size_t bytes_wanted, void *priv)
    110 {
    111 	struct meson_rng_softc * const sc = priv;
    112 	uint32_t data;
    113 
    114 	while (bytes_wanted) {
    115 		data = bus_space_read_4(sc->sc_bst, sc->sc_bsh, 0);
    116 		rnd_add_data_sync(&sc->sc_rndsource, &data, sizeof(data),
    117 		    sizeof(data) * NBBY);
    118 		bytes_wanted -= MIN(bytes_wanted, sizeof(data));
    119 	}
    120 	explicit_memset(&data, 0, sizeof(data));
    121 }
    122