xref: /aosp_15_r20/external/coreboot/src/vendorcode/cavium/bdk/libbdk-driver/bdk-driver-rnm.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /***********************license start***********************************
2 * Copyright (c) 2003-2017  Cavium Inc. ([email protected]). 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 Inc. 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 * This Software, including technical data, may be subject to U.S. export
24 * control laws, including the U.S. Export Administration Act and its
25 * associateint bdk_rng_init(bdk_node_t node)
26 * d regulations, and may be subject to export or import
27 * regulations in other countries.
28 *
29 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
30 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS OR
31 * WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH RESPECT
32 * TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
33 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
34 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY) WARRANTIES
35 * OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A PARTICULAR
36 * PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET ENJOYMENT,
37 * QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE ENTIRE  RISK
38 * ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES WITH YOU.
39 ***********************license end**************************************/
40 #include <bdk.h>
41 #include "libbdk-arch/bdk-csrs-pccpf.h"
42 #include "libbdk-arch/bdk-csrs-rnm.h"
43 
44 #include <libbdk-hal/bdk-rng.h>
45 #include <libbdk-hal/device/bdk-device.h>
46 #define RNG_DEVID ((BDK_PCC_PROD_E_GEN << 24) | BDK_PCC_VENDOR_E_CAVIUM | (BDK_PCC_DEV_IDL_E_RNM << 16))
47 
48 BDK_REQUIRE_DEFINE(RNM);
49 
50 /**
51  * Reads 8 bits of random data from Random number generator
52  *
53  * @return random data
54  */
bdk_rng_get_random8(void)55 uint8_t bdk_rng_get_random8(void)
56 {
57 
58     return bdk_read64_uint8(bdk_numa_get_address(bdk_numa_local(), BDK_RNM_RANDOM));
59 }
60 
61 /**
62  * Reads 16 bits of random data from Random number generator
63  *
64  * @return random data
65  */
bdk_rng_get_random16(void)66 uint16_t bdk_rng_get_random16(void)
67 {
68     return bdk_read64_uint16(bdk_numa_get_address(bdk_numa_local(), BDK_RNM_RANDOM));
69 }
70 
71 /**
72  * Reads 32 bits of random data from Random number generator
73  *
74  * @return random data
75  */
bdk_rng_get_random32(void)76 uint32_t bdk_rng_get_random32(void)
77 {
78     return bdk_read64_uint32(bdk_numa_get_address(bdk_numa_local(), BDK_RNM_RANDOM));
79 }
80 
81 /**
82  * Reads 64 bits of random data from Random number generator
83  *
84  * @return random data
85  */
bdk_rng_get_random64(void)86 uint64_t bdk_rng_get_random64(void)
87 {
88     return bdk_read64_uint64(bdk_numa_get_address(bdk_numa_local(), BDK_RNM_RANDOM));
89 }
90 
91 /**
92  * RNM init() function
93  *
94  * @param device RNM to initialize
95  *
96  * @return Zero on success, negative on failure
97  */
bdk_rng_init(bdk_node_t node)98 int bdk_rng_init(bdk_node_t node)
99 {
100     const bdk_device_t *device = bdk_device_lookup(node, RNG_DEVID, 0);
101     if (!device)
102     {
103         bdk_error("RNM: ECAM device not found\n");
104         return -1;
105     }
106     BDK_BAR_MODIFY(c, device, BDK_RNM_CTL_STATUS,
107         c.s.ent_en = 1;
108         c.s.rng_en = 1);
109     /* Read back after enable so we know it is done. Needed on t88 pass 2.0 emulator and t81 real hardware !!!! */
110     BDK_BAR_READ(device, BDK_RNM_CTL_STATUS);
111 
112     /* Errata (RNM-22528) First consecutive reads to RNM_RANDOM return same
113        value. Before using the random entropy, read RNM_RANDOM at least once
114        and discard the data */
115     bdk_rng_get_random64();
116     return 0;
117 }
118 
119