11.2Sthorpej/*	$NetBSD: bcm2838_rng.c,v 1.2 2021/01/27 03:10:19 thorpej Exp $ */
21.1Smlelstv
31.1Smlelstv/*-
41.1Smlelstv * Copyright (c) 2019 The NetBSD Foundation, Inc.
51.1Smlelstv * All rights reserved.
61.1Smlelstv *
71.1Smlelstv * This code is derived from software contributed to The NetBSD Foundation
81.1Smlelstv * by Jared D. McNeill
91.1Smlelstv *
101.1Smlelstv * Redistribution and use in source and binary forms, with or without
111.1Smlelstv * modification, are permitted provided that the following conditions
121.1Smlelstv * are met:
131.1Smlelstv * 1. Redistributions of source code must retain the above copyright
141.1Smlelstv *    notice, this list of conditions and the following disclaimer.
151.1Smlelstv * 2. Redistributions in binary form must reproduce the above copyright
161.1Smlelstv *    notice, this list of conditions and the following disclaimer in the
171.1Smlelstv *    documentation and/or other materials provided with the distribution.
181.1Smlelstv *
191.1Smlelstv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Smlelstv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Smlelstv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Smlelstv * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Smlelstv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Smlelstv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Smlelstv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Smlelstv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Smlelstv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Smlelstv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Smlelstv * POSSIBILITY OF SUCH DAMAGE.
301.1Smlelstv */
311.1Smlelstv
321.1Smlelstv#include <sys/cdefs.h>
331.2Sthorpej__KERNEL_RCSID(0, "$NetBSD: bcm2838_rng.c,v 1.2 2021/01/27 03:10:19 thorpej Exp $");
341.1Smlelstv
351.1Smlelstv#include <sys/param.h>
361.1Smlelstv#include <sys/device.h>
371.1Smlelstv
381.1Smlelstv#include <dev/ic/rng200var.h>
391.1Smlelstv#include <dev/fdt/fdtvar.h>
401.1Smlelstv
411.1Smlelstvstruct bcm2838rng_softc {
421.1Smlelstv	device_t		sc_dev;
431.1Smlelstv	struct rng200_softc	sc_rng200;
441.1Smlelstv};
451.1Smlelstv
461.1Smlelstvstatic int bcm2838rng_match(device_t, cfdata_t, void *);
471.1Smlelstvstatic void bcm2838rng_attach(device_t, device_t, void *);
481.1Smlelstv
491.1SmlelstvCFATTACH_DECL_NEW(bcm2838rng_fdt, sizeof(struct bcm2838rng_softc),
501.1Smlelstv    bcm2838rng_match, bcm2838rng_attach, NULL, NULL);
511.1Smlelstv
521.2Sthorpejstatic const struct device_compatible_entry compat_data[] = {
531.2Sthorpej	{ .compat = "brcm,bcm2838-rng200" },
541.2Sthorpej	DEVICE_COMPAT_EOL
551.2Sthorpej};
561.2Sthorpej
571.1Smlelstv/* ARGSUSED */
581.1Smlelstvstatic int
591.1Smlelstvbcm2838rng_match(device_t parent, cfdata_t match, void *aux)
601.1Smlelstv{
611.1Smlelstv	struct fdt_attach_args * const faa = aux;
621.1Smlelstv
631.2Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
641.1Smlelstv}
651.1Smlelstv
661.1Smlelstvstatic void
671.1Smlelstvbcm2838rng_attach(device_t parent, device_t self, void *aux)
681.1Smlelstv{
691.1Smlelstv	struct bcm2838rng_softc *sc = device_private(self);
701.1Smlelstv	struct fdt_attach_args * const faa = aux;
711.1Smlelstv	bus_addr_t addr;
721.1Smlelstv	bus_size_t size;
731.1Smlelstv	bus_space_handle_t bsh;
741.1Smlelstv	int error;
751.1Smlelstv
761.1Smlelstv	sc->sc_dev = self;
771.1Smlelstv
781.1Smlelstv	error = fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size);
791.1Smlelstv	if (error) {
801.1Smlelstv		aprint_error_dev(sc->sc_dev, ": couldn't get registers\n");
811.1Smlelstv		return;
821.1Smlelstv	}
831.1Smlelstv
841.1Smlelstv	if (bus_space_map(faa->faa_bst, addr, size, 0, &bsh)) {
851.1Smlelstv		aprint_error_dev(sc->sc_dev, ": unable to map device\n");
861.1Smlelstv		return;
871.1Smlelstv	}
881.1Smlelstv
891.1Smlelstv	aprint_naive("\n");
901.1Smlelstv	aprint_normal(": Hardware RNG\n");
911.1Smlelstv
921.1Smlelstv	sc->sc_rng200.sc_bst = faa->faa_bst;
931.1Smlelstv	sc->sc_rng200.sc_bsh = bsh;
941.1Smlelstv	sc->sc_rng200.sc_name = device_xname(sc->sc_dev);
951.1Smlelstv	rng200_attach(&sc->sc_rng200);
961.1Smlelstv}
97