1 //*****************************************************************************
2 //
3 //! @file am_hal_otp.c
4 //!
5 //! @brief Implementation for One-Time Programmable Functionality
6 //!
7 //! @addtogroup otp_4p OTP - One-Time Programmable
8 //! @ingroup apollo4p_hal
9 //! @{
10 //
11 //*****************************************************************************
12
13 //*****************************************************************************
14 //
15 // Copyright (c) 2023, Ambiq Micro, Inc.
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are met:
20 //
21 // 1. Redistributions of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
23 //
24 // 2. Redistributions in binary form must reproduce the above copyright
25 // notice, this list of conditions and the following disclaimer in the
26 // documentation and/or other materials provided with the distribution.
27 //
28 // 3. Neither the name of the copyright holder nor the names of its
29 // contributors may be used to endorse or promote products derived from this
30 // software without specific prior written permission.
31 //
32 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
36 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 // POSSIBILITY OF SUCH DAMAGE.
43 //
44 // This is part of revision release_sdk_4_4_0-3c5977e664 of the AmbiqSuite Development Package.
45 //
46 //*****************************************************************************
47 #include <stdint.h>
48 #include <stdbool.h>
49 #include "am_mcu_apollo.h"
50
51 //#define OTP_ACCESS_WA
52
53 /* poll on the AIB acknowledge bit */
54 #define AM_HAL_OTP_WAIT_ON_AIB_ACK_BIT() \
55 while (CRYPTO->AIBFUSEPROGCOMPLETED_b.AIBFUSEPROGCOMPLETED == 0)
56
57 //*****************************************************************************
58 //
59 //! @brief Validate the OTP Offset and whether it is within range
60 //!
61 //! @param offset - word aligned offset in OTP to be read
62 //!
63 //! @return Returns AM_HAL_STATUS_SUCCESS or OUT_OF_RANGE
64 //
65 //*****************************************************************************
validate_otp_offset(uint32_t offset)66 static uint32_t validate_otp_offset(uint32_t offset)
67 {
68 if ((offset & 0x3) || (offset > (AM_REG_OTP_SIZE - 4)))
69 {
70 return AM_HAL_STATUS_OUT_OF_RANGE;
71 }
72 else
73 {
74 return AM_HAL_STATUS_SUCCESS;
75 }
76 }
77
78 //*****************************************************************************
79 //
80 // Read OTP word
81 //
82 // This will retrieve the OTP information
83 //
84 //*****************************************************************************
am_hal_otp_read_word(uint32_t offset,uint32_t * pVal)85 uint32_t am_hal_otp_read_word(uint32_t offset, uint32_t *pVal)
86 {
87 uint32_t status = AM_HAL_STATUS_SUCCESS;
88
89 #ifndef AM_HAL_DISABLE_API_VALIDATION
90 status = validate_otp_offset(offset);
91 if (status != AM_HAL_STATUS_SUCCESS)
92 {
93 return status;
94 }
95 if (!pVal)
96 {
97 return AM_HAL_STATUS_INVALID_ARG;
98 }
99 #endif
100 if ((PWRCTRL->DEVPWRSTATUS_b.PWRSTCRYPTO == 0) || (CRYPTO->HOSTCCISIDLE_b.HOSTCCISIDLE == 0))
101 {
102 // Crypto is not accessible
103 return AM_HAL_STATUS_INVALID_OPERATION;
104 }
105
106 #ifdef OTP_ACCESS_WA
107 AM_CRITICAL_BEGIN;
108 *pVal = am_hal_load_ui32((uint32_t *)(AM_REG_OTP_BASEADDR + offset));
109 AM_CRITICAL_END;
110 #else
111 *pVal = AM_REGVAL(AM_REG_OTP_BASEADDR + offset);
112 #endif
113 return status;
114 }
115
116 #ifdef OTP_ACCESS_WA
117
118 //
119 //! Writes the contents of R1 to the OTP offset pointed to by R0.
120 //
121 uint16_t otp_write_word_asm[] =
122 {
123 0xf242, 0x0300, // movw r3, #8192 ; 0x2000
124 0xf2c4, 0x030c, // movt r3, #16396 ; 0x400c
125 0x5019, // str r1, [r3, r0]
126
127 0xf641, 0x7304, // movw r3, #7940 ; 0x1f04
128 0xf2c4, 0x030c, // movt r3, #16396 ; 0x400c
129 0x6818, // ldr r0, [r3, #0] (loop)
130 0x07c0, // lsls r0, r0, #31
131 0xd0fc, // beq.n <loop>
132 0x4770, // bx lr
133 };
134
135 typedef void (*otp_write_word_t)(uint32_t, uint32_t);
136
137 otp_write_word_t otp_write_word_func = (otp_write_word_t)((uint8_t *)otp_write_word_asm + 1);
138 #endif
139
140 //*****************************************************************************
141 //
142 // Write OTP word
143 //
144 // This will write a word to the supplied offset in the OTP
145 //
146 //*****************************************************************************
am_hal_otp_write_word(uint32_t offset,uint32_t value)147 uint32_t am_hal_otp_write_word(uint32_t offset, uint32_t value)
148 {
149 uint32_t status = AM_HAL_STATUS_SUCCESS;
150
151 #ifndef AM_HAL_DISABLE_API_VALIDATION
152 status = validate_otp_offset(offset);
153 if (status != AM_HAL_STATUS_SUCCESS)
154 {
155 return status;
156 }
157 #endif
158 if ((PWRCTRL->DEVPWRSTATUS_b.PWRSTCRYPTO == 0) || (CRYPTO->HOSTCCISIDLE_b.HOSTCCISIDLE == 0))
159 {
160 // Crypto is not accessible
161 return AM_HAL_STATUS_INVALID_OPERATION;
162 }
163 #ifdef OTP_ACCESS_WA
164 AM_CRITICAL_BEGIN;
165 otp_write_word_func(offset, value);
166 // Read back the value to compare
167 if ((am_hal_load_ui32((uint32_t *)(AM_REG_OTP_BASEADDR + offset)) & value) != value)
168 {
169 status = AM_HAL_STATUS_FAIL;
170 }
171
172 AM_CRITICAL_END;
173 #else
174 AM_REGVAL(AM_REG_OTP_BASEADDR + offset) = value;
175 AM_HAL_OTP_WAIT_ON_AIB_ACK_BIT();
176 // Read back the value to compare
177 if ((AM_REGVAL(AM_REG_OTP_BASEADDR + offset) & value) != value)
178 {
179 status = AM_HAL_STATUS_FAIL;
180 }
181 #endif
182
183 return status;
184 }
185
186 //*****************************************************************************
187 //
188 // End Doxygen group.
189 //! @}
190 //
191 //*****************************************************************************
192