xref: /netbsd-src/sys/arch/mips/ingenic/ingenic_rng.c (revision 8ecbf5f02b752fcb7debe1a8fab1dc82602bc760)
1 /*	$NetBSD: ingenic_rng.c,v 1.5 2020/04/30 03:40:53 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.5 2020/04/30 03:40:53 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 	kmutex_t		sc_lock;
63 	krndsource_t		sc_rndsource;
64 };
65 
66 CFATTACH_DECL_NEW(ingenic_rng, sizeof(struct ingenic_rng_softc),
67 	ingenic_rng_match, ingenic_rng_attach, NULL, NULL);
68 
69 static int
70 ingenic_rng_match(device_t parent, cfdata_t cf, void *aux)
71 {
72 	const struct apbus_attach_args *aa = aux;
73 
74 	return !(strcmp(aa->aa_name, "jzrng"));
75 }
76 
77 static void
78 ingenic_rng_attach(device_t parent, device_t self, void *aux)
79 {
80 	struct ingenic_rng_softc * const sc = device_private(self);
81 	const struct apbus_attach_args * const aa = aux;
82 	bus_addr_t addr = aa->aa_addr;
83 	int error;
84 
85 	sc->sc_dev = self;
86 	sc->sc_bst = aa->aa_bst;
87 	if (addr == 0)
88 		addr = JZ_RNG;
89 
90 	error = bus_space_map(aa->aa_bst, addr, 4, 0, &sc->sc_bsh);
91 	if (error) {
92 		aprint_error_dev(self,
93 		    "can't map registers for %s: %d\n", aa->aa_name, error);
94 		return;
95 	}
96 
97 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
98 
99 	aprint_naive(": Ingenic random number generator\n");
100 	aprint_normal(": Ingenic random number generator\n");
101 
102 	rndsource_setcb(&sc->sc_rndsource, ingenic_rng_get, sc);
103 	rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
104 	    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
105 }
106 
107 static void
108 ingenic_rng_get(size_t bytes_wanted, void *priv)
109 {
110 	struct ingenic_rng_softc * const sc = priv;
111 	uint32_t data;
112 
113 	mutex_spin_enter(&sc->sc_lock);
114 	while (bytes_wanted) {
115 		data = bus_space_read_4(sc->sc_bst, sc->sc_bsh, 0);
116 		delay(1);
117 		rnd_add_data_sync(&sc->sc_rndsource, &data, sizeof(data),
118 		    sizeof(data) * NBBY);
119 		bytes_wanted -= MIN(bytes_wanted, sizeof(data));
120 	}
121 	explicit_memset(&data, 0, sizeof(data));
122 	mutex_spin_exit(&sc->sc_lock);
123 }
124