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
11#include "hw/top/dt/otp_ctrl.h"
13#include "sw/device/silicon_creator/lib/error.h"
14
15#include "hw/top/otp_ctrl_regs.h" // Generated.
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21/**
22 * The following constants represent the expected number of sec_mmio register
23 * writes performed by functions provided by this module. See
24 * `SEC_MMIO_WRITE_INCREMENT()` for more details.
25 *
26 * Example:
27 * ```
28 * otp_creator_sw_cfg_lockdown();
29 * SEC_MMIO_WRITE_INCREMENT(kOtpSecMmioCreatorSwCfgLockDown);
30 * ```
31 */
32enum {
33 /**
34 * Individual register reads/writes.
35 */
36 kOtpSecMmioCreatorSwCfgLockDown = 1,
37};
38
39/**
40 * Perform a blocking 32-bit read from the memory mapped software config
41 * partitions.
42 *
43 * @param address The address to read from offset from the start of OTP memory.
44 * @return The 32-bit value from OTP.
45 */
47uint32_t otp_read32(uint32_t address);
48
49/**
50 * Perform a blocking 64-bit read from the memory mapped software config
51 * partitions.
52 *
53 * @param address The address to read from offset from the start of OTP memory.
54 * @return The 64-bit value from OTP.
55 */
57uint64_t otp_read64(uint32_t address);
58
59/**
60 * Perform a blocking read of `num_words` 32-bit words from the memory mapped
61 * software config partitions.
62 *
63 * @param address The address to read from offset from the start of OTP memory.
64 * @param data The output buffer of at least length `num_words`.
65 * @param num_words The number of 32-bit words to read from OTP.
66 */
67void otp_read(uint32_t address, uint32_t *data, size_t num_words);
68
69/**
70 * Return information for OTP partitions whose fields are readable by SW
71 * after being locked.
72 *
73 * @param partition The OTP partition identifier.
74 * @return The OTP partition information
75 */
76dt_otp_partition_info_t otp_readable_partition_info(otp_partition_t partition);
77
78/**
79 * Read a partition's 64-bit digest from the corresponding CSRs.
80 *
81 * @param partition The OTP partition whose digest should be read.
82 * @return The 64-bit digest value.
83 */
84uint64_t otp_partition_digest_read(otp_partition_t partition);
85
86/**
87 * Perform a blocking 32-bit read from the Direct Access Interface (DAI).
88 *
89 * This enables reading any readable 32-bit OTP field that is not in the memory
90 * mapped software config partitions (i.e., partitions other than the
91 * {CREATOR,OWNER}_SW_CFG partitions).
92 *
93 * @param partition The OTP partition to read from.
94 * @param relative_address The address to read from, relative to the start of
95 * the OTP partition.
96 * @return The 32-bit value from OTP.
97 */
98uint32_t otp_dai_read32(otp_partition_t partition, uint32_t address);
99
100/**
101 * Perform a blocking 64-bit read from the Direct Access Interface (DAI).
102 *
103 * This enables reading any readable 64-bit OTP field that is not in the memory
104 * mapped software config partitions (i.e., partitions other than the
105 * {CREATOR,OWNER}_SW_CFG partitions).
106 *
107 * @param partition The OTP partition to read from.
108 * @param relative_address The address to read from, relative to the start of
109 * the OTP partition.
110 * @return The 64-bit value from OTP.
111 */
112uint64_t otp_dai_read64(otp_partition_t partition, uint32_t address);
113
114/**
115 * Perform a blocking read of `num_words` 32-bit words from the Direct Access
116 * Interface (DAI).
117 *
118 * Note: this should only be used to read 32-bit granule OTP regions.
119 *
120 * @param partition The OTP partition to read from.
121 * @param relative_address The address to read from, relative to the start of
122 * the OTP partition.
123 * @param data The output buffer of at least length `num_words`.
124 * @param num_words The number of 32-bit words to read from OTP.
125 */
126rom_error_t otp_dai_read(otp_partition_t partition, uint32_t address,
127 uint32_t *data, size_t num_words);
128
129/**
130 * Disables read access to CREATOR_SW_CFG partition until next reset.
131 *
132 * This function must be called in ROM_EXT before handing over execution to the
133 * first owner boot stage.
134 */
135void otp_creator_sw_cfg_lockdown(void);
136
137#ifdef __cplusplus
138}
139#endif
140
141#endif // OPENTITAN_SW_DEVICE_SILICON_CREATOR_LIB_DRIVERS_OTP_H_