xref: /netbsd-src/sys/arch/arm/broadcom/bcm2838_rng.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1 /*	$NetBSD: bcm2838_rng.c,v 1.2 2021/01/27 03:10:19 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2019 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: bcm2838_rng.c,v 1.2 2021/01/27 03:10:19 thorpej Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/device.h>
37 
38 #include <dev/ic/rng200var.h>
39 #include <dev/fdt/fdtvar.h>
40 
41 struct bcm2838rng_softc {
42 	device_t		sc_dev;
43 	struct rng200_softc	sc_rng200;
44 };
45 
46 static int bcm2838rng_match(device_t, cfdata_t, void *);
47 static void bcm2838rng_attach(device_t, device_t, void *);
48 
49 CFATTACH_DECL_NEW(bcm2838rng_fdt, sizeof(struct bcm2838rng_softc),
50     bcm2838rng_match, bcm2838rng_attach, NULL, NULL);
51 
52 static const struct device_compatible_entry compat_data[] = {
53 	{ .compat = "brcm,bcm2838-rng200" },
54 	DEVICE_COMPAT_EOL
55 };
56 
57 /* ARGSUSED */
58 static int
bcm2838rng_match(device_t parent,cfdata_t match,void * aux)59 bcm2838rng_match(device_t parent, cfdata_t match, void *aux)
60 {
61 	struct fdt_attach_args * const faa = aux;
62 
63 	return of_compatible_match(faa->faa_phandle, compat_data);
64 }
65 
66 static void
bcm2838rng_attach(device_t parent,device_t self,void * aux)67 bcm2838rng_attach(device_t parent, device_t self, void *aux)
68 {
69 	struct bcm2838rng_softc *sc = device_private(self);
70 	struct fdt_attach_args * const faa = aux;
71 	bus_addr_t addr;
72 	bus_size_t size;
73 	bus_space_handle_t bsh;
74 	int error;
75 
76 	sc->sc_dev = self;
77 
78 	error = fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size);
79 	if (error) {
80 		aprint_error_dev(sc->sc_dev, ": couldn't get registers\n");
81 		return;
82 	}
83 
84 	if (bus_space_map(faa->faa_bst, addr, size, 0, &bsh)) {
85 		aprint_error_dev(sc->sc_dev, ": unable to map device\n");
86 		return;
87 	}
88 
89 	aprint_naive("\n");
90 	aprint_normal(": Hardware RNG\n");
91 
92 	sc->sc_rng200.sc_bst = faa->faa_bst;
93 	sc->sc_rng200.sc_bsh = bsh;
94 	sc->sc_rng200.sc_name = device_xname(sc->sc_dev);
95 	rng200_attach(&sc->sc_rng200);
96 }
97