cvmx-rng.h revision 210284
1/***********************license start***************
2 *  Copyright (c) 2003-2008 Cavium Networks (support@cavium.com). All rights
3 *  reserved.
4 *
5 *
6 *  Redistribution and use in source and binary forms, with or without
7 *  modification, are permitted provided that the following conditions are
8 *  met:
9 *
10 *      * Redistributions of source code must retain the above copyright
11 *        notice, this list of conditions and the following disclaimer.
12 *
13 *      * Redistributions in binary form must reproduce the above
14 *        copyright notice, this list of conditions and the following
15 *        disclaimer in the documentation and/or other materials provided
16 *        with the distribution.
17 *
18 *      * Neither the name of Cavium Networks nor the names of
19 *        its contributors may be used to endorse or promote products
20 *        derived from this software without specific prior written
21 *        permission.
22 *
23 *  TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
24 *  AND WITH ALL FAULTS AND CAVIUM NETWORKS MAKES NO PROMISES, REPRESENTATIONS
25 *  OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
26 *  RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
27 *  REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
28 *  DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
29 *  OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
30 *  PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT, QUIET
31 *  POSSESSION OR CORRESPONDENCE TO DESCRIPTION.  THE ENTIRE RISK ARISING OUT
32 *  OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
33 *
34 *
35 *  For any questions regarding licensing please contact marketing@caviumnetworks.com
36 *
37 ***********************license end**************************************/
38
39
40
41
42
43
44/**
45 * @file
46 *
47 * Function and structure definitions for random number generator hardware
48 *
49 * <hr>$Revision: 41586 $<hr>
50 */
51
52
53#ifndef __CMVX_RNG_H__
54#define __CMVX_RNG_H__
55
56#ifdef	__cplusplus
57extern "C" {
58#endif
59
60#define CVMX_RNG_LOAD_ADDRESS   CVMX_ADD_IO_SEG(cvmx_build_io_address(CVMX_OCT_DID_RNG, 0))
61
62/**
63 * Structure describing the data format used for IOBDMA stores to the RNG.
64 */
65typedef union
66{
67    uint64_t        u64;
68    struct {
69        uint64_t    scraddr : 8;    /**< the (64-bit word) location in scratchpad to write to (if len != 0) */
70        uint64_t    len     : 8;    /**< the number of words in the response (0 => no response) */
71        uint64_t    did     : 5;    /**< the ID of the device on the non-coherent bus */
72        uint64_t    subdid  : 3;    /**< the sub ID of the device on the non-coherent bus */
73        uint64_t    addr    :40;    /**< the address that will appear in the first tick on the NCB bus */
74    } s;
75} cvmx_rng_iobdma_data_t;
76
77/**
78 * Enables the random number generator. Must be called before RNG is used
79 */
80static inline void cvmx_rng_enable(void)
81{
82    cvmx_rnm_ctl_status_t rnm_ctl_status;
83    rnm_ctl_status.u64 = 0;
84    rnm_ctl_status.s.ent_en = 1;
85    rnm_ctl_status.s.rng_en = 1;
86    cvmx_write_csr(CVMX_RNM_CTL_STATUS, rnm_ctl_status.u64);
87}
88/**
89 * Reads 8 bits of random data from Random number generator
90 *
91 * @return random data
92 */
93static inline uint8_t cvmx_rng_get_random8(void)
94{
95    return cvmx_read64_uint8(CVMX_RNG_LOAD_ADDRESS);
96}
97
98/**
99 * Reads 16 bits of random data from Random number generator
100 *
101 * @return random data
102 */
103static inline uint16_t cvmx_rng_get_random16(void)
104{
105    return cvmx_read64_uint16(CVMX_RNG_LOAD_ADDRESS);
106}
107
108/**
109 * Reads 32 bits of random data from Random number generator
110 *
111 * @return random data
112 */
113static inline uint32_t cvmx_rng_get_random32(void)
114{
115    return cvmx_read64_uint32(CVMX_RNG_LOAD_ADDRESS);
116}
117
118/**
119 * Reads 64 bits of random data from Random number generator
120 *
121 * @return random data
122 */
123static inline uint64_t cvmx_rng_get_random64(void)
124{
125    return cvmx_read64_uint64(CVMX_RNG_LOAD_ADDRESS);
126}
127
128/**
129 * Requests random data from the RNG block asynchronously using and IOBDMA operation.
130 * The random data will be written into the cores
131 * local memory at the specified address.  A SYNCIOBDMA
132 * operation should be issued to stall for completion of the write.
133 *
134 * @param scr_addr  Address in scratch memory to put the result
135 *                  MUST be a multiple of 8 bytes
136 * @param num_bytes Number of bytes of random data to write at
137 *                  scr_addr
138 *                  MUST be a multiple of 8 bytes
139 *
140 * @return 0 on success
141 *         1 on error
142 */
143static inline int cvmx_rng_request_random_async(uint64_t scr_addr, uint64_t num_bytes)
144{
145    cvmx_rng_iobdma_data_t data;
146
147    if (num_bytes & 0x7 || scr_addr & 0x7)
148        return(1);
149
150    /* scr_addr must be 8 byte aligned */
151    data.s.scraddr = scr_addr >> 3;
152    data.s.len = num_bytes >> 3;
153    data.s.did = CVMX_OCT_DID_RNG;
154    cvmx_send_single(data.u64);
155    return(0);
156}
157
158#ifdef	__cplusplus
159}
160#endif
161
162#endif /*  __CMVX_RNG_H__  */
163