Home | History | Annotate | Line # | Download | only in broadcom
bcm53xx_rng.c revision 1.1.2.1
      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.1.2.1 2012/11/20 03:01:03 tls Exp $");
     37 
     38 #include <sys/bus.h>
     39 #include <sys/callout.h>
     40 #include <sys/device.h>
     41 #include <sys/intr.h>
     42 #include <sys/rnd.h>
     43 #include <sys/systm.h>
     44 
     45 #include <arm/broadcom/bcm53xx_reg.h>
     46 #include <arm/broadcom/bcm53xx_var.h>
     47 
     48 static int bcmrng_ccb_match(device_t, cfdata_t, void *);
     49 static void bcmrng_ccb_attach(device_t, device_t, void *);
     50 
     51 struct bcmrng_softc {
     52 	device_t sc_dev;
     53 	bus_space_tag_t sc_bst;
     54 	bus_space_handle_t sc_bsh;
     55 	krndsource_t sc_rnd_source;
     56 	struct callout sc_rnd_callout;
     57 	kmutex_t *sc_lock;
     58 #ifdef RNG_USE_INTR
     59 	size_t sc_intr_amount;
     60 	void *sc_ih;
     61 #endif
     62 };
     63 
     64 static void bcmrng_callout(void *arg);
     65 static size_t bcmrng_empty(struct bcmrng_softc *);
     66 #ifdef RNG_USE_INTR
     67 static int bcmrng_intr(void *);
     68 #endif
     69 
     70 CFATTACH_DECL_NEW(bcmrng_ccb, sizeof(struct bcmrng_softc),
     71 	bcmrng_ccb_match, bcmrng_ccb_attach, NULL, NULL);
     72 
     73 static int
     74 bcmrng_ccb_match(device_t parent, cfdata_t cf, void *aux)
     75 {
     76 	struct bcmccb_attach_args * const ccbaa = aux;
     77 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
     78 
     79 	if (strcmp(cf->cf_name, loc->loc_name))
     80 		return 0;
     81 
     82 	KASSERT(cf->cf_loc[BCMCCBCF_PORT] == BCMCCBCF_PORT_DEFAULT);
     83 
     84 	return 1;
     85 }
     86 
     87 static inline uint32_t
     88 bcmrng_read_4(struct bcmrng_softc *sc, bus_size_t o)
     89 {
     90 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh, o);
     91 }
     92 
     93 static inline void
     94 bcmrng_write_4(struct bcmrng_softc *sc, bus_size_t o, uint32_t v)
     95 {
     96 	bus_space_write_4(sc->sc_bst, sc->sc_bsh, o, v);
     97 }
     98 
     99 /*
    100  * To be called from initarm for an early start.
    101  */
    102 void
    103 bcm53xx_rng_start(bus_space_tag_t bst, bus_space_handle_t bsh)
    104 {
    105 	/*
    106 	 * Disable the interrupt and enable the RNG.
    107 	 */
    108 	bus_space_write_4(bst, bsh, CCB_BASE + RNG_BASE + RNG_INT_MASK, RNG_INT_OFF);
    109 	bus_space_write_4(bst, bsh, CCB_BASE + RNG_BASE + RNG_CTRL,
    110 	    bus_space_read_4(bst, bsh, CCB_BASE + RNG_BASE + RNG_CTRL) | RNG_RBGEN);
    111 }
    112 
    113 static void
    114 bcmrng_ccb_attach(device_t parent, device_t self, void *aux)
    115 {
    116 	struct bcmrng_softc * const sc = device_private(self);
    117 	struct bcmccb_attach_args * const ccbaa = aux;
    118 	const struct bcm_locators * const loc = &ccbaa->ccbaa_loc;
    119 
    120 	sc->sc_dev = self;
    121 
    122 	/* make sure it's running */
    123 	bcm53xx_rng_start(ccbaa->ccbaa_ccb_bst, ccbaa->ccbaa_ccb_bsh);
    124 
    125 	sc->sc_bst = ccbaa->ccbaa_ccb_bst;
    126 	bus_space_subregion(sc->sc_bst, ccbaa->ccbaa_ccb_bsh,
    127 	    loc->loc_offset, loc->loc_size, &sc->sc_bsh);
    128 
    129 	aprint_naive("\n");
    130 	aprint_normal(": Random Number Generator (%u bit FIFO)\n",
    131 	    32 * bcmrng_read_4(sc, RNG_FF_THRESHOLD));
    132 
    133 	sc->sc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTCLOCK);
    134 
    135 	callout_init(&sc->sc_rnd_callout, CALLOUT_MPSAFE);
    136 
    137 	rnd_attach_source(&sc->sc_rnd_source, device_xname(self), RND_TYPE_RNG,
    138 	    RND_FLAG_NO_ESTIMATE);
    139 
    140 #ifdef RNG_USE_INTR
    141 	sc->sc_ih = intr_establish(loc->loc_intrs[0], IPL_VM, IST_LEVEL,
    142 	    bcmrng_intr, sc);
    143 	if (sc->sc_ih == NULL) {
    144 		aprint_error_dev(self, "failed to establish interrupt %d\n",
    145 		     loc->loc_intrs[0]);
    146 	} else {
    147 		aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intrs[0]);
    148 	}
    149 #else
    150 	bcmrng_callout(sc);
    151 #endif
    152 }
    153 
    154 size_t
    155 bcmrng_empty(struct bcmrng_softc *sc)
    156 {
    157 	mutex_enter(sc->sc_lock);
    158 	size_t nwords = __SHIFTOUT(bcmrng_read_4(sc, RNG_STATUS), RNG_VAL);
    159 	if (nwords == 0) {
    160 		mutex_exit(sc->sc_lock);
    161 		return 0;
    162 	}
    163 
    164 	uint32_t data[nwords];
    165 	while (nwords-- > 0) {
    166 		/*
    167 		 * It's random data, who cares what order we load it in?
    168 		 */
    169 		data[nwords] = bcmrng_read_4(sc, RNG_DATA);
    170 	}
    171 	rnd_add_data(&sc->sc_rnd_source, data, sizeof(data), sizeof(data) * 8);
    172 	mutex_exit(sc->sc_lock);
    173 	//aprint_debug_dev(sc->sc_dev, "added %zu words\n", __arraycount(data));
    174 	return __arraycount(data);
    175 }
    176 
    177 void
    178 bcmrng_callout(void *arg)
    179 {
    180 	struct bcmrng_softc * const sc = arg;
    181 
    182 	bcmrng_empty(sc);
    183 
    184 	callout_reset(&sc->sc_rnd_callout, 1, bcmrng_callout, sc);
    185 }
    186 
    187 #ifdef RNG_USE_INTR
    188 int
    189 bcmrng_intr(void *arg)
    190 {
    191 	struct bcmrng_softc * const sc = arg;
    192 
    193 	sc->sc_intr_amount += bcmrng_empty(sc);
    194 	if (sc->sc_intr_amount >= 0x10000) {
    195 		bcmrng_callout(sc);
    196 		bcmrng_write_4(sc, RNG_INT_MASK, RNG_INT_OFF);
    197 	}
    198 	return 1;
    199 }
    200 #endif
    201