Home | History | Annotate | Line # | Download | only in ingenic
      1 /*	$NetBSD: ingenic_rng.c,v 1.7 2022/03/19 11:55:03 riastradh Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015 Michael McConville
      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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: ingenic_rng.c,v 1.7 2022/03/19 11:55:03 riastradh Exp $");
     31 
     32 /*
     33  * adapted from Jared McNeill's amlogic_rng.c
     34  */
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/device.h>
     39 #include <sys/mutex.h>
     40 #include <sys/kernel.h>
     41 #include <sys/mutex.h>
     42 #include <sys/bus.h>
     43 #include <sys/rndsource.h>
     44 
     45 #include <mips/ingenic/ingenic_var.h>
     46 #include <mips/ingenic/ingenic_regs.h>
     47 
     48 #include "opt_ingenic.h"
     49 
     50 struct ingenic_rng_softc;
     51 
     52 static int	ingenic_rng_match(device_t, cfdata_t, void *);
     53 static void	ingenic_rng_attach(device_t, device_t, void *);
     54 
     55 static void	ingenic_rng_get(size_t, void *);
     56 
     57 struct ingenic_rng_softc {
     58 	device_t		sc_dev;
     59 	bus_space_tag_t		sc_bst;
     60 	bus_space_handle_t	sc_bsh;
     61 
     62 	krndsource_t		sc_rndsource;
     63 };
     64 
     65 CFATTACH_DECL_NEW(ingenic_rng, sizeof(struct ingenic_rng_softc),
     66 	ingenic_rng_match, ingenic_rng_attach, NULL, NULL);
     67 
     68 static int
     69 ingenic_rng_match(device_t parent, cfdata_t cf, void *aux)
     70 {
     71 	const struct apbus_attach_args *aa = aux;
     72 
     73 	return !(strcmp(aa->aa_name, "jzrng"));
     74 }
     75 
     76 static void
     77 ingenic_rng_attach(device_t parent, device_t self, void *aux)
     78 {
     79 	struct ingenic_rng_softc * const sc = device_private(self);
     80 	const struct apbus_attach_args * const aa = aux;
     81 	bus_addr_t addr = aa->aa_addr;
     82 	int error;
     83 
     84 	sc->sc_dev = self;
     85 	sc->sc_bst = aa->aa_bst;
     86 	if (addr == 0)
     87 		addr = JZ_RNG;
     88 
     89 	error = bus_space_map(aa->aa_bst, addr, 4, 0, &sc->sc_bsh);
     90 	if (error) {
     91 		aprint_error_dev(self,
     92 		    "can't map registers for %s: %d\n", aa->aa_name, error);
     93 		return;
     94 	}
     95 
     96 	aprint_naive(": Ingenic random number generator\n");
     97 	aprint_normal(": Ingenic random number generator\n");
     98 
     99 	rndsource_setcb(&sc->sc_rndsource, ingenic_rng_get, sc);
    100 	rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
    101 	    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
    102 }
    103 
    104 static void
    105 ingenic_rng_get(size_t bytes_wanted, void *priv)
    106 {
    107 	struct ingenic_rng_softc * const sc = priv;
    108 	uint32_t data;
    109 
    110 	while (bytes_wanted) {
    111 		data = bus_space_read_4(sc->sc_bst, sc->sc_bsh, 0);
    112 		delay(1);
    113 		rnd_add_data_sync(&sc->sc_rndsource, &data, sizeof(data),
    114 		    sizeof(data) * NBBY);
    115 		bytes_wanted -= MIN(bytes_wanted, sizeof(data));
    116 	}
    117 	explicit_memset(&data, 0, sizeof(data));
    118 }
    119