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