Home | History | Annotate | Line # | Download | only in broadcom
      1 /*-
      2  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to The NetBSD Foundation
      6  * by Matt Thomas of 3am Software Foundry.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  * POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #define RNG_PRIVATE
     31 
     32 #include "locators.h"
     33 
     34 #include <sys/cdefs.h>
     35 
     36 __KERNEL_RCSID(1, "$NetBSD: bcm53xx_rng.c,v 1.10 2017/01/08 05:05:27 christos Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/bus.h>
     40 #include <sys/callout.h>
     41 #include <sys/device.h>
     42 #include <sys/intr.h>
     43 #include <sys/rndsource.h>
     44 #include <sys/systm.h>
     45 
     46 #include <arm/broadcom/bcm53xx_reg.h>
     47 #include <arm/broadcom/bcm53xx_var.h>
     48 
     49 static int bcmrng_ccb_match(device_t, cfdata_t, void *);
     50 static void bcmrng_ccb_attach(device_t, device_t, void *);
     51 
     52 struct bcmrng_softc {
     53 	device_t sc_dev;
     54 	bus_space_tag_t sc_bst;
     55 	bus_space_handle_t sc_bsh;
     56 	krndsource_t sc_rnd_source;
     57 	struct callout sc_rnd_callout;
     58 	kmutex_t *sc_lock;
     59 #ifdef RNG_USE_INTR
     60 	size_t sc_intr_amount;
     61 	void *sc_ih;
     62 #endif
     63 };
     64 
     65 static void bcmrng_callout(void *arg);
     66 static size_t bcmrng_empty(struct bcmrng_softc *);
     67 #ifdef RNG_USE_INTR
     68 static int bcmrng_intr(void *);
     69 #endif
     70 
     71 CFATTACH_DECL_NEW(bcmrng_ccb, sizeof(struct bcmrng_softc),
     72 	bcmrng_ccb_match, bcmrng_ccb_attach, NULL, NULL);
     73 
     74 static int
     75 bcmrng_ccb_match(device_t parent, cfdata_t cf, void *aux)
     76 {
     77 	struct bcmccb_attach_args * const ccbaa = aux;
     78 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
     79 
     80 	if (strcmp(cf->cf_name, loc->loc_name))
     81 		return 0;
     82 
     83 	KASSERT(cf->cf_loc[BCMCCBCF_PORT] == BCMCCBCF_PORT_DEFAULT);
     84 
     85 	return 1;
     86 }
     87 
     88 static inline uint32_t
     89 bcmrng_read_4(struct bcmrng_softc *sc, bus_size_t o)
     90 {
     91 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
     92 }
     93 
     94 static inline void
     95 bcmrng_read_multi_4(struct bcmrng_softc *sc, bus_size_t o, uint32_t *p,
     96     size_t c)
     97 {
     98 	return bus_space_read_multi_4(sc->sc_bst, sc->sc_bsh, o, p, c);
     99 }
    100 
    101 __unused static inline void
    102 bcmrng_write_4(struct bcmrng_softc *sc, bus_size_t o, uint32_t v)
    103 {
    104 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
    105 }
    106 
    107 /*
    108  * To be called from initarm for an early start.
    109  */
    110 void
    111 bcm53xx_rng_start(bus_space_tag_t bst, bus_space_handle_t bsh)
    112 {
    113 	/*
    114 	 * Disable the interrupt and enable the RNG.
    115 	 */
    116 	bus_space_write_4(bst, bsh, CCB_BASE + RNG_BASE + RNG_INT_MASK, RNG_INT_OFF);
    117 	bus_space_write_4(bst, bsh, CCB_BASE + RNG_BASE + RNG_CTRL,
    118 	    bus_space_read_4(bst, bsh, CCB_BASE + RNG_BASE + RNG_CTRL) | RNG_RBGEN);
    119 }
    120 
    121 static void
    122 bcmrng_ccb_attach(device_t parent, device_t self, void *aux)
    123 {
    124 	struct bcmrng_softc * const sc = device_private(self);
    125 	struct bcmccb_attach_args * const ccbaa = aux;
    126 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
    127 
    128 	sc->sc_dev = self;
    129 
    130 	/* make sure it's running */
    131 	bcm53xx_rng_start(ccbaa->ccbaa_ccb_bst, ccbaa->ccbaa_ccb_bsh);
    132 
    133 	sc->sc_bst = ccbaa->ccbaa_ccb_bst;
    134 	bus_space_subregion(sc->sc_bst, ccbaa->ccbaa_ccb_bsh,
    135 	    loc->loc_offset, loc->loc_size, &sc->sc_bsh);
    136 
    137 	aprint_naive("\n");
    138 	aprint_normal(": Random Number Generator (%u bit FIFO)\n",
    139 	    32 * bcmrng_read_4(sc, RNG_FF_THRESHOLD));
    140 
    141 	sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTCLOCK);
    142 
    143 	callout_init(&sc->sc_rnd_callout, CALLOUT_MPSAFE);
    144 
    145 	rnd_attach_source(&sc->sc_rnd_source, device_xname(self), RND_TYPE_RNG,
    146 	    RND_FLAG_COLLECT_VALUE);
    147 
    148 #ifdef RNG_USE_INTR
    149 	sc->sc_ih = intr_establish(loc->loc_intrs[0], IPL_VM, IST_LEVEL,
    150 	    bcmrng_intr, sc);
    151 	if (sc->sc_ih == NULL) {
    152 		aprint_error_dev(self, "failed to establish interrupt %d\n",
    153 		     loc->loc_intrs[0]);
    154 	} else {
    155 		aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intrs[0]);
    156 	}
    157 #else
    158 	bcmrng_callout(sc);
    159 #endif
    160 }
    161 
    162 size_t
    163 bcmrng_empty(struct bcmrng_softc *sc)
    164 {
    165 	mutex_enter(sc->sc_lock);
    166 	uint32_t data[128];
    167 	size_t nwords = __SHIFTOUT(bcmrng_read_4(sc, RNG_STATUS), RNG_VAL);
    168 	if (nwords == 0) {
    169 		mutex_exit(sc->sc_lock);
    170 		return 0;
    171 	}
    172 	if (nwords > __arraycount(data))
    173 		nwords = __arraycount(data);
    174 
    175 	bcmrng_read_multi_4(sc, RNG_DATA, data, nwords);
    176 
    177 	rnd_add_data(&sc->sc_rnd_source, data, sizeof(data), sizeof(data) * 8);
    178 	mutex_exit(sc->sc_lock);
    179 	//aprint_debug_dev(sc->sc_dev, "added %zu words\n", nwords);
    180 	return nwords;
    181 }
    182 
    183 void
    184 bcmrng_callout(void *arg)
    185 {
    186 	struct bcmrng_softc * const sc = arg;
    187 
    188 	bcmrng_empty(sc);
    189 
    190 	callout_reset(&sc->sc_rnd_callout, 1, bcmrng_callout, sc);
    191 }
    192 
    193 #ifdef RNG_USE_INTR
    194 int
    195 bcmrng_intr(void *arg)
    196 {
    197 	struct bcmrng_softc * const sc = arg;
    198 
    199 	sc->sc_intr_amount += bcmrng_empty(sc);
    200 	if (sc->sc_intr_amount >= 0x10000) {
    201 		bcmrng_callout(sc);
    202 		bcmrng_write_4(sc, RNG_INT_MASK, RNG_INT_OFF);
    203 	}
    204 	return 1;
    205 }
    206 #endif
    207