xref: /netbsd-src/sys/arch/arm/broadcom/bcm2838_rng.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /*	$NetBSD: bcm2838_rng.c,v 1.2 2021/01/27 03:10:19 thorpej Exp $ */
2a50e8b83Smlelstv 
3a50e8b83Smlelstv /*-
4a50e8b83Smlelstv  * Copyright (c) 2019 The NetBSD Foundation, Inc.
5a50e8b83Smlelstv  * All rights reserved.
6a50e8b83Smlelstv  *
7a50e8b83Smlelstv  * This code is derived from software contributed to The NetBSD Foundation
8a50e8b83Smlelstv  * by Jared D. McNeill
9a50e8b83Smlelstv  *
10a50e8b83Smlelstv  * Redistribution and use in source and binary forms, with or without
11a50e8b83Smlelstv  * modification, are permitted provided that the following conditions
12a50e8b83Smlelstv  * are met:
13a50e8b83Smlelstv  * 1. Redistributions of source code must retain the above copyright
14a50e8b83Smlelstv  *    notice, this list of conditions and the following disclaimer.
15a50e8b83Smlelstv  * 2. Redistributions in binary form must reproduce the above copyright
16a50e8b83Smlelstv  *    notice, this list of conditions and the following disclaimer in the
17a50e8b83Smlelstv  *    documentation and/or other materials provided with the distribution.
18a50e8b83Smlelstv  *
19a50e8b83Smlelstv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20a50e8b83Smlelstv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21a50e8b83Smlelstv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22a50e8b83Smlelstv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23a50e8b83Smlelstv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24a50e8b83Smlelstv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25a50e8b83Smlelstv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26a50e8b83Smlelstv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27a50e8b83Smlelstv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28a50e8b83Smlelstv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29a50e8b83Smlelstv  * POSSIBILITY OF SUCH DAMAGE.
30a50e8b83Smlelstv  */
31a50e8b83Smlelstv 
32a50e8b83Smlelstv #include <sys/cdefs.h>
33*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: bcm2838_rng.c,v 1.2 2021/01/27 03:10:19 thorpej Exp $");
34a50e8b83Smlelstv 
35a50e8b83Smlelstv #include <sys/param.h>
36a50e8b83Smlelstv #include <sys/device.h>
37a50e8b83Smlelstv 
38a50e8b83Smlelstv #include <dev/ic/rng200var.h>
39a50e8b83Smlelstv #include <dev/fdt/fdtvar.h>
40a50e8b83Smlelstv 
41a50e8b83Smlelstv struct bcm2838rng_softc {
42a50e8b83Smlelstv 	device_t		sc_dev;
43a50e8b83Smlelstv 	struct rng200_softc	sc_rng200;
44a50e8b83Smlelstv };
45a50e8b83Smlelstv 
46a50e8b83Smlelstv static int bcm2838rng_match(device_t, cfdata_t, void *);
47a50e8b83Smlelstv static void bcm2838rng_attach(device_t, device_t, void *);
48a50e8b83Smlelstv 
49a50e8b83Smlelstv CFATTACH_DECL_NEW(bcm2838rng_fdt, sizeof(struct bcm2838rng_softc),
50a50e8b83Smlelstv     bcm2838rng_match, bcm2838rng_attach, NULL, NULL);
51a50e8b83Smlelstv 
52*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
53*6e54367aSthorpej 	{ .compat = "brcm,bcm2838-rng200" },
54*6e54367aSthorpej 	DEVICE_COMPAT_EOL
55*6e54367aSthorpej };
56*6e54367aSthorpej 
57a50e8b83Smlelstv /* ARGSUSED */
58a50e8b83Smlelstv static int
bcm2838rng_match(device_t parent,cfdata_t match,void * aux)59a50e8b83Smlelstv bcm2838rng_match(device_t parent, cfdata_t match, void *aux)
60a50e8b83Smlelstv {
61a50e8b83Smlelstv 	struct fdt_attach_args * const faa = aux;
62a50e8b83Smlelstv 
63*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
64a50e8b83Smlelstv }
65a50e8b83Smlelstv 
66a50e8b83Smlelstv static void
bcm2838rng_attach(device_t parent,device_t self,void * aux)67a50e8b83Smlelstv bcm2838rng_attach(device_t parent, device_t self, void *aux)
68a50e8b83Smlelstv {
69a50e8b83Smlelstv 	struct bcm2838rng_softc *sc = device_private(self);
70a50e8b83Smlelstv 	struct fdt_attach_args * const faa = aux;
71a50e8b83Smlelstv 	bus_addr_t addr;
72a50e8b83Smlelstv 	bus_size_t size;
73a50e8b83Smlelstv 	bus_space_handle_t bsh;
74a50e8b83Smlelstv 	int error;
75a50e8b83Smlelstv 
76a50e8b83Smlelstv 	sc->sc_dev = self;
77a50e8b83Smlelstv 
78a50e8b83Smlelstv 	error = fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size);
79a50e8b83Smlelstv 	if (error) {
80a50e8b83Smlelstv 		aprint_error_dev(sc->sc_dev, ": couldn't get registers\n");
81a50e8b83Smlelstv 		return;
82a50e8b83Smlelstv 	}
83a50e8b83Smlelstv 
84a50e8b83Smlelstv 	if (bus_space_map(faa->faa_bst, addr, size, 0, &bsh)) {
85a50e8b83Smlelstv 		aprint_error_dev(sc->sc_dev, ": unable to map device\n");
86a50e8b83Smlelstv 		return;
87a50e8b83Smlelstv 	}
88a50e8b83Smlelstv 
89a50e8b83Smlelstv 	aprint_naive("\n");
90a50e8b83Smlelstv 	aprint_normal(": Hardware RNG\n");
91a50e8b83Smlelstv 
92a50e8b83Smlelstv 	sc->sc_rng200.sc_bst = faa->faa_bst;
93a50e8b83Smlelstv 	sc->sc_rng200.sc_bsh = bsh;
94a50e8b83Smlelstv 	sc->sc_rng200.sc_name = device_xname(sc->sc_dev);
95a50e8b83Smlelstv 	rng200_attach(&sc->sc_rng200);
96a50e8b83Smlelstv }
97