Software APIs
otp.h
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_OTP_H_
6 #define OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_OTP_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
12 #include "sw/device/silicon_creator/lib/error.h"
13 
14 #include "otp_ctrl_regs.h" // Generated.
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /**
21  * The following constants represent the expected number of sec_mmio register
22  * writes performed by functions provided by this module. See
23  * `SEC_MMIO_WRITE_INCREMENT()` for more details.
24  *
25  * Example:
26  * ```
27  * otp_creator_sw_cfg_lockdown();
28  * SEC_MMIO_WRITE_INCREMENT(kOtpSecMmioCreatorSwCfgLockDown);
29  * ```
30  */
31 enum {
32  /**
33  * Individual register reads/writes.
34  */
35  kOtpSecMmioCreatorSwCfgLockDown = 1,
36 };
37 
38 /**
39  * OTP partition information.
40  */
41 typedef struct otp_partition_info {
42  /**
43  * The absolute OTP address at which this partition starts.
44  */
45  uint32_t start_addr;
46  /**
47  * Size (in bytes) of the partition, excluding the digest field.
48  */
49  size_t size;
50  /**
51  * The absolute OTP address at which this partition's digest starts.
52  */
53  uint32_t digest_addr;
54  /**
55  * The alignment mask for this partition.
56  *
57  * A valid address for this partition must be such that
58  * `addr & align_mask == 0`.
59  */
60  uint32_t align_mask;
62 
63 /**
64  * OTP partitions whose fields are readable after being locked.
65  */
66 typedef enum otp_partition {
67  kOtpPartitionCreatorSwCfg = 0,
68  kOtpPartitionOwnerSwCfg = 1,
69  kOtpPartitionRotCreatorAuthCodesign = 2,
70  kOtpPartitionRotCreatorAuthState = 3,
71  kOtpPartitionHwCfg0 = 4,
72  kOtpPartitionHwCfg1 = 5,
73  kOtpPartitionNumPartitions = 6,
74 } otp_partition_t;
75 
76 /**
77  * Table of OTP partition information.
78  */
79 extern const otp_partition_info_t kOtpPartitions[];
80 
81 /**
82  * Perform a blocking 32-bit read from the memory mapped software config
83  * partitions.
84  *
85  * @param address The address to read from offset from the start of OTP memory.
86  * @return The 32-bit value from OTP.
87  */
89 uint32_t otp_read32(uint32_t address);
90 
91 /**
92  * Perform a blocking 64-bit read from the memory mapped software config
93  * partitions.
94  *
95  * @param address The address to read from offset from the start of OTP memory.
96  * @return The 64-bit value from OTP.
97  */
99 uint64_t otp_read64(uint32_t address);
100 
101 /**
102  * Perform a blocking read of `num_words` 32-bit words from the memory mapped
103  * software config partitions.
104  *
105  * @param address The address to read from offset from the start of OTP memory.
106  * @param data The output buffer of at least length `num_words`.
107  * @param num_words The number of 32-bit words to read from OTP.
108  */
109 void otp_read(uint32_t address, uint32_t *data, size_t num_words);
110 
111 /**
112  * Read a partition's 64-bit digest from the corresponding CSRs.
113  *
114  * @param partition The OTP partition whose digest should be read.
115  * @return The 64-bit digest value.
116  */
117 uint64_t otp_partition_digest_read(otp_partition_t partition);
118 
119 /**
120  * Perform a blocking 32-bit read from the Direct Access Interface (DAI).
121  *
122  * This enables reading any readable 32-bit OTP field that is not in the memory
123  * mapped software config partitions (i.e., partitions other than the
124  * {CREATOR,OWNER}_SW_CFG partitions).
125  *
126  * @param partition The OTP partition to read from.
127  * @param relative_address The address to read from, relative to the start of
128  * the OTP partition.
129  * @return The 32-bit value from OTP.
130  */
131 uint32_t otp_dai_read32(otp_partition_t partition, uint32_t address);
132 
133 /**
134  * Perform a blocking 64-bit read from the Direct Access Interface (DAI).
135  *
136  * This enables reading any readable 64-bit OTP field that is not in the memory
137  * mapped software config partitions (i.e., partitions other than the
138  * {CREATOR,OWNER}_SW_CFG partitions).
139  *
140  * @param partition The OTP partition to read from.
141  * @param relative_address The address to read from, relative to the start of
142  * the OTP partition.
143  * @return The 64-bit value from OTP.
144  */
145 uint64_t otp_dai_read64(otp_partition_t partition, uint32_t address);
146 
147 /**
148  * Perform a blocking read of `num_words` 32-bit words from the Direct Access
149  * Interface (DAI).
150  *
151  * Note: this should only be used to read 32-bit granule OTP regions.
152  *
153  * @param partition The OTP partition to read from.
154  * @param relative_address The address to read from, relative to the start of
155  * the OTP partition.
156  * @param data The output buffer of at least length `num_words`.
157  * @param num_words The number of 32-bit words to read from OTP.
158  */
159 rom_error_t otp_dai_read(otp_partition_t partition, uint32_t address,
160  uint32_t *data, size_t num_words);
161 
162 /**
163  * Disables read access to CREATOR_SW_CFG partition until next reset.
164  *
165  * This function must be called in ROM_EXT before handing over execution to the
166  * first owner boot stage.
167  */
168 void otp_creator_sw_cfg_lockdown(void);
169 
170 #ifdef __cplusplus
171 }
172 #endif
173 
174 #endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_OTP_H_