11.7Sriastrad/* $NetBSD: ti_rng.c,v 1.7 2022/03/19 11:55:03 riastradh Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. The name of the author may not be used to endorse or promote products
131.1Sjmcneill *    derived from this software without specific prior written permission.
141.1Sjmcneill *
151.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
161.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
171.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
181.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
191.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
201.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
211.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
221.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
231.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251.1Sjmcneill * SUCH DAMAGE.
261.1Sjmcneill */
271.1Sjmcneill
281.1Sjmcneill#include <sys/cdefs.h>
291.7Sriastrad__KERNEL_RCSID(0, "$NetBSD: ti_rng.c,v 1.7 2022/03/19 11:55:03 riastradh Exp $");
301.1Sjmcneill
311.1Sjmcneill#include <sys/param.h>
321.1Sjmcneill#include <sys/systm.h>
331.1Sjmcneill#include <sys/device.h>
341.1Sjmcneill#include <sys/conf.h>
351.1Sjmcneill#include <sys/mutex.h>
361.1Sjmcneill#include <sys/bus.h>
371.1Sjmcneill#include <sys/rndsource.h>
381.1Sjmcneill
391.1Sjmcneill#include <dev/fdt/fdtvar.h>
401.1Sjmcneill
411.1Sjmcneill#include <arm/ti/ti_prcm.h>
421.1Sjmcneill#include <arm/ti/ti_rngreg.h>
431.1Sjmcneill
441.5Sthorpejstatic const struct device_compatible_entry compat_data[] = {
451.5Sthorpej	{ .compat = "ti,omap4-rng" },
461.5Sthorpej	DEVICE_COMPAT_EOL
471.1Sjmcneill};
481.1Sjmcneill
491.1Sjmcneillstruct ti_rng_softc {
501.1Sjmcneill	device_t sc_dev;
511.1Sjmcneill	bus_space_tag_t sc_iot;
521.1Sjmcneill	bus_space_handle_t sc_ioh;
531.1Sjmcneill
541.1Sjmcneill	krndsource_t sc_rndsource;
551.1Sjmcneill};
561.1Sjmcneill
571.1Sjmcneillstatic int	ti_rng_match(device_t, cfdata_t, void *);
581.1Sjmcneillstatic void	ti_rng_attach(device_t, device_t, void *);
591.1Sjmcneillstatic void	ti_rng_callback(size_t, void *);
601.1Sjmcneill
611.1SjmcneillCFATTACH_DECL_NEW(ti_rng, sizeof(struct ti_rng_softc),
621.1Sjmcneill    ti_rng_match, ti_rng_attach, NULL, NULL);
631.1Sjmcneill
641.1Sjmcneill#define RD4(sc, reg) \
651.1Sjmcneill	bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg))
661.1Sjmcneill#define WR4(sc, reg, val) \
671.1Sjmcneill	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
681.1Sjmcneill
691.1Sjmcneillstatic int
701.1Sjmcneillti_rng_match(device_t parent, cfdata_t match, void *aux)
711.1Sjmcneill{
721.1Sjmcneill	struct fdt_attach_args * const faa = aux;
731.1Sjmcneill
741.5Sthorpej	return of_compatible_match(faa->faa_phandle, compat_data);
751.1Sjmcneill}
761.1Sjmcneill
771.1Sjmcneillstatic void
781.1Sjmcneillti_rng_attach(device_t parent, device_t self, void *aux)
791.1Sjmcneill{
801.1Sjmcneill	struct ti_rng_softc *sc = device_private(self);
811.1Sjmcneill	struct fdt_attach_args * const faa = aux;
821.1Sjmcneill	const int phandle = faa->faa_phandle;
831.1Sjmcneill	bus_addr_t addr;
841.1Sjmcneill	bus_size_t size;
851.1Sjmcneill
861.1Sjmcneill	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
871.1Sjmcneill		aprint_error(": couldn't get registers\n");
881.1Sjmcneill		return;
891.1Sjmcneill	}
901.1Sjmcneill
911.2Sjmcneill	if (ti_prcm_enable_hwmod(phandle, 0) != 0) {
921.1Sjmcneill		aprint_error(": couldn't enable module\n");
931.1Sjmcneill		return;
941.1Sjmcneill	}
951.1Sjmcneill
961.1Sjmcneill	sc->sc_dev = self;
971.1Sjmcneill	sc->sc_iot = faa->faa_bst;
981.1Sjmcneill	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
991.1Sjmcneill		aprint_error(": couldn't map registers\n");
1001.1Sjmcneill		return;
1011.1Sjmcneill	}
1021.1Sjmcneill
1031.1Sjmcneill	if ((RD4(sc, TRNG_CONTROL_REG) & TRNG_CONTROL_ENABLE) == 0) {
1041.1Sjmcneill		WR4(sc, TRNG_CONFIG_REG,
1051.1Sjmcneill		    __SHIFTIN(0x21, TRNG_CONFIG_MIN_REFILL) |
1061.1Sjmcneill		    __SHIFTIN(0x22, TRNG_CONFIG_MAX_REFILL));
1071.1Sjmcneill		WR4(sc, TRNG_CONTROL_REG,
1081.1Sjmcneill		    __SHIFTIN(0x21, TRNG_CONTROL_STARTUP_CYCLES) |
1091.1Sjmcneill		    TRNG_CONTROL_ENABLE);
1101.1Sjmcneill	}
1111.1Sjmcneill
1121.4Sjmcneill	aprint_naive("\n");
1131.4Sjmcneill	aprint_normal(": RNG\n");
1141.4Sjmcneill
1151.1Sjmcneill	rndsource_setcb(&sc->sc_rndsource, ti_rng_callback, sc);
1161.1Sjmcneill	rnd_attach_source(&sc->sc_rndsource, device_xname(self), RND_TYPE_RNG,
1171.1Sjmcneill	    RND_FLAG_COLLECT_VALUE|RND_FLAG_HASCB);
1181.1Sjmcneill}
1191.1Sjmcneill
1201.1Sjmcneillstatic void
1211.1Sjmcneillti_rng_callback(size_t bytes_wanted, void *priv)
1221.1Sjmcneill{
1231.1Sjmcneill	struct ti_rng_softc * const sc = priv;
1241.1Sjmcneill	uint32_t buf[2];
1251.1Sjmcneill	u_int retry;
1261.1Sjmcneill
1271.1Sjmcneill	while (bytes_wanted) {
1281.1Sjmcneill		for (retry = 10; retry > 0; retry--) {
1291.1Sjmcneill			if (RD4(sc, TRNG_STATUS_REG) & TRNG_STATUS_READY)
1301.1Sjmcneill				break;
1311.1Sjmcneill			delay(10);
1321.1Sjmcneill		}
1331.1Sjmcneill		if (retry == 0)
1341.1Sjmcneill			break;
1351.1Sjmcneill		buf[0] = RD4(sc, TRNG_OUTPUT_L_REG);
1361.1Sjmcneill		buf[1] = RD4(sc, TRNG_OUTPUT_H_REG);
1371.1Sjmcneill		WR4(sc, TRNG_INTACK_REG, TRNG_INTACK_READY);
1381.1Sjmcneill		rnd_add_data_sync(&sc->sc_rndsource, buf, sizeof(buf),
1391.1Sjmcneill		    sizeof(buf) * NBBY);
1401.1Sjmcneill		bytes_wanted -= MIN(bytes_wanted, sizeof(buf));
1411.1Sjmcneill	}
1421.1Sjmcneill	explicit_memset(buf, 0, sizeof(buf));
1431.1Sjmcneill}
144