fsl_ocotp.c
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2020 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "fsl_ocotp.h"
9 
10 /*******************************************************************************
11  * Definitions
12  ******************************************************************************/
13 /* Component ID definition, used by tools. */
14 #ifndef FSL_COMPONENT_ID
15 #define FSL_COMPONENT_ID "platform.drivers.ocotp"
16 #endif
17 
18 /* Wait time should be not less than 150ns . */
19 #define OCOTP_TIMING_WAIT_NS (uint64_t)150
20 /* Relex time should be not less than 100ns . */
21 #define OCOTP_TIMING_RELEX_NS (uint64_t)100
22 /* Program time should be rang from 9000ns~11000ns. */
23 #define OCOTP_TIMING_PROGRAM_NS (uint64_t)10000
24 /* Read time should be less than 40ns. */
25 #define OCOTP_TIMING_READ_NS (uint64_t)40
26 
27 /* Unlock key is 0x3E77. */
28 #define OCOTP_WRITE_UNLOCK_KEY (0x3E77)
29 /*******************************************************************************
30  * Prototypes
31  ******************************************************************************/
32 
39 static void OCOTP_SetReadTiming(OCOTP_Type *base, ocotp_timing_t timingConfig);
40 
47 static void OCOTP_SetWriteTiming(OCOTP_Type *base, ocotp_timing_t timingConfig);
48 
49 /*******************************************************************************
50  * Variables
51  ******************************************************************************/
52 /* Timing configuration for OCOTP controller. */
54 
55 /*******************************************************************************
56  * Code
57  *******************************************************************************/
58 /* Reload the shadow register. */
60 {
61  assert(NULL != base);
62 
63  /* Make sure the OCOTP is ready, Overlapped accesses are not supported by the controller. */
64  while (OCOTP_CheckBusyStatus(base))
65  {
66  }
67 
68  /* Clear access error status bit. */
70 
71  /* Set the read timing. */
73 
74  /* Wait for the OCOTP controller not busy. */
75  while (OCOTP_CheckBusyStatus(base))
76  {
77  }
78 
79  /* Set reload bit. */
81 
82  /* Wait for the OCOTP controller not busy. */
83  while (OCOTP_CheckBusyStatus(base))
84  {
85  }
86  /* Wait for shadow register reload complete. this bit will be auto clear by OCOTP once operation is complete. */
88  {
89  }
90 }
91 
92 static void OCOTP_SetReadTiming(OCOTP_Type *base, ocotp_timing_t timingConfig)
93 {
94  uint32_t timingValue = base->TIMING;
95 
97  timingValue |= OCOTP_TIMING_RELAX(timingConfig.relax) | OCOTP_TIMING_STROBE_READ(timingConfig.strobe_read) |
98  OCOTP_TIMING_WAIT(timingConfig.wait);
99  base->TIMING = timingValue;
100 }
101 
102 static void OCOTP_SetWriteTiming(OCOTP_Type *base, ocotp_timing_t timingConfig)
103 {
104  uint32_t timingValue = base->TIMING;
105 
107  timingValue |= OCOTP_TIMING_RELAX(timingConfig.relax) | OCOTP_TIMING_STROBE_PROG(timingConfig.strobe_prog) |
108  OCOTP_TIMING_WAIT(timingConfig.wait);
109 
110  base->TIMING = timingValue;
111 }
112 
113 /* Initializes OCOTP controller. */
114 void OCOTP_Init(OCOTP_Type *base, uint32_t srcClock_Hz)
115 {
116  assert(NULL != base);
117  assert(0UL != srcClock_Hz);
118 
119 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
120  /* Enable OCOTP clock */
122 #endif
123 
124  /* tWait time shoule be higher than OCOTP_TIMING_WAIT_NS. */
125  s_timingConfig.wait = (OCOTP_TIMING_WAIT_NS * srcClock_Hz + 1000000000) / 1000000000 - 1;
126 
127  /* tRelax time shoule be higher than OCOTP_TIMING_RELEX_NS. */
128  s_timingConfig.relax = (OCOTP_TIMING_RELEX_NS * srcClock_Hz + 1000000000) / 1000000000 - 1;
129 
130  /* tStrobe_prog time should be close to OCOTP_TIMING_PROGRAM_NS, only add half of 1000000000. */
132  (OCOTP_TIMING_PROGRAM_NS * srcClock_Hz + 500000000) / 1000000000 + 2 * (s_timingConfig.relax + 1) - 1;
133 
134  /* tStrobe_read time should be higher than OCOTP_TIMING_READ_NS. */
136  (OCOTP_TIMING_READ_NS * srcClock_Hz + 1000000000) / 1000000000 + 2 * (s_timingConfig.relax + 1) - 1;
137 }
138 
139 /* De-init OCOTP controller. */
141 {
142  assert(NULL != base);
143 
144  s_timingConfig.wait = 0UL;
145  s_timingConfig.relax = 0UL;
148 
149 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
150  /* Disable OCOTP clock */
152 #endif
153 }
154 
155 /* Read the fuse shadow register. */
156 uint32_t OCOTP_ReadFuseShadowRegister(OCOTP_Type *base, uint32_t address)
157 {
158  assert(NULL != base);
159 
160  /* Make sure the OCOTP is ready, Overlapped accesses are not supported by the controller. */
161  while (OCOTP_CheckBusyStatus(base))
162  {
163  }
164 
165  /* If ERROR bit was set, clear access error status bit. */
166  if (OCOTP_CheckErrorStatus(base))
167  {
169  }
170 
171  /* Set the read timing. */
173 
174  /* Wait for busy bit is cleared. */
175  while (OCOTP_CheckBusyStatus(base))
176  {
177  }
178 
179  /* Clear access error status bit. */
180  if (OCOTP_CheckErrorStatus(base))
181  {
183  }
184 
185  /* Write requested address to register. */
187  base->CTRL_SET = OCOTP_CTRL_SET_ADDR(address);
188 
189  /* Set OCOTP auto read enable. */
191 
192  /* Wait for busy bit is cleared, and no error occurred on controller. */
193  while (OCOTP_CheckBusyStatus(base))
194  {
195  }
196 
197  /* If ERROR bit was set, this may be mean that the accsee to the register was wrong. */
198  if (OCOTP_CheckErrorStatus(base))
199  {
200  /* Clear access error status bit. */
202  }
203 
204  /* Read the fuse data. */
205  return (base->READ_FUSE_DATA);
206 }
207 
208 /* Write the fuse shadow register. */
209 status_t OCOTP_WriteFuseShadowRegister(OCOTP_Type *base, uint32_t address, uint32_t data)
210 {
211  assert(NULL != base);
212 
213  /* Make sure the OCOTP is ready, Overlapped accesses are not supported by the controller. */
214  while (OCOTP_CheckBusyStatus(base))
215  {
216  }
217 
218  /* Clear access error status bit. */
219  if (OCOTP_CheckErrorStatus(base))
220  {
222  }
223 
224  /* Set write timing for OCOTP controller. */
226 
227  /* Wait for busy bit is cleared. */
228  while (OCOTP_CheckBusyStatus(base))
229  {
230  }
231 
232  /* Clear access error status bit. */
233  if (OCOTP_CheckErrorStatus(base))
234  {
236  }
237 
238  /* Write requested address and unlock key to register. */
241 
242  /* Write data to register. */
243  base->DATA = data;
244 
245  /* Wait for busy bit is cleared, and no error occurred on controller. */
246  while (OCOTP_CheckBusyStatus(base))
247  {
248  }
249 
250  /* If ERROR bit was set, this may be mean that the accsee to the register was wrong. */
251  if (OCOTP_CheckErrorStatus(base))
252  {
253  /* Clear access error status bit. */
255 
257  }
258 
259  /* Reload the fuse register. */
261 
262  return kStatus_Success;
263 }
OCOTP_SetWriteTiming
static void OCOTP_SetWriteTiming(OCOTP_Type *base, ocotp_timing_t timingConfig)
Set write timing configuration.
Definition: fsl_ocotp.c:102
OCOTP_CheckBusyStatus
static bool OCOTP_CheckBusyStatus(OCOTP_Type *base)
Checking the BUSY bit in CTRL register. Checking this BUSY bit will help confirm if the OCOTP control...
Definition: fsl_ocotp.h:82
kCLOCK_Ocotp
@ kCLOCK_Ocotp
Definition: fsl_clock.h:491
OCOTP_CTRL_WR_UNLOCK
#define OCOTP_CTRL_WR_UNLOCK(x)
Definition: MIMXRT1052.h:27826
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
OCOTP_TIMING_RELEX_NS
#define OCOTP_TIMING_RELEX_NS
Definition: fsl_ocotp.c:21
OCOTP_WRITE_UNLOCK_KEY
#define OCOTP_WRITE_UNLOCK_KEY
Definition: fsl_ocotp.c:28
OCOTP_WriteFuseShadowRegister
status_t OCOTP_WriteFuseShadowRegister(OCOTP_Type *base, uint32_t address, uint32_t data)
Write the fuse shadow register with the fuse addess and data. Please make sure the wrtie address is n...
Definition: fsl_ocotp.c:209
OCOTP_Deinit
void OCOTP_Deinit(OCOTP_Type *base)
De-initializes OCOTP controller.
Definition: fsl_ocotp.c:140
_ocotp_timing
OCOTP timing structure. Note that, these value are used for calcalating the read/write timings....
Definition: fsl_ocotp.h:44
OCOTP_SetReadTiming
static void OCOTP_SetReadTiming(OCOTP_Type *base, ocotp_timing_t timingConfig)
Set read timing configuration.
Definition: fsl_ocotp.c:92
OCOTP_Type::DATA
__IO uint32_t DATA
Definition: MIMXRT1052.h:27705
s_timingConfig
ocotp_timing_t s_timingConfig
Definition: fsl_ocotp.c:53
OCOTP_TIMING_RELAX
#define OCOTP_TIMING_RELAX(x)
Definition: MIMXRT1052.h:27893
_ocotp_timing::relax
uint32_t relax
Definition: fsl_ocotp.h:47
OCOTP_TIMING_WAIT_MASK
#define OCOTP_TIMING_WAIT_MASK
Definition: MIMXRT1052.h:27897
OCOTP_ReloadShadowRegister
void OCOTP_ReloadShadowRegister(OCOTP_Type *base)
Reload the shadow register. This function will help reload the shadow register without reseting the O...
Definition: fsl_ocotp.c:59
fsl_ocotp.h
OCOTP_ReadFuseShadowRegister
uint32_t OCOTP_ReadFuseShadowRegister(OCOTP_Type *base, uint32_t address)
Read the fuse shadow register with the fuse addess.
Definition: fsl_ocotp.c:156
OCOTP_ClearErrorStatus
static void OCOTP_ClearErrorStatus(OCOTP_Type *base)
Clear the error bit if this bit is set.
Definition: fsl_ocotp.h:103
kStatus_OCOTP_AccessError
@ kStatus_OCOTP_AccessError
Definition: fsl_ocotp.h:29
OCOTP_CTRL_WR_UNLOCK_MASK
#define OCOTP_CTRL_WR_UNLOCK_MASK
Definition: MIMXRT1052.h:27824
OCOTP_TIMING_WAIT_NS
#define OCOTP_TIMING_WAIT_NS
Definition: fsl_ocotp.c:19
OCOTP_CTRL_SET_ADDR
#define OCOTP_CTRL_SET_ADDR(x)
Definition: MIMXRT1052.h:27833
OCOTP_TIMING_STROBE_READ
#define OCOTP_TIMING_STROBE_READ(x)
Definition: MIMXRT1052.h:27896
_ocotp_timing::strobe_prog
uint32_t strobe_prog
Definition: fsl_ocotp.h:48
OCOTP_Type::READ_CTRL
__IO uint32_t READ_CTRL
Definition: MIMXRT1052.h:27707
CLOCK_EnableClock
static void CLOCK_EnableClock(clock_ip_name_t name)
Enable the clock for specific IP.
Definition: fsl_clock.h:1059
OCOTP_CTRL_RELOAD_SHADOWS
#define OCOTP_CTRL_RELOAD_SHADOWS(x)
Definition: MIMXRT1052.h:27823
OCOTP_CTRL_CLR_ADDR_MASK
#define OCOTP_CTRL_CLR_ADDR_MASK
Definition: MIMXRT1052.h:27850
OCOTP_READ_CTRL_READ_FUSE_MASK
#define OCOTP_READ_CTRL_READ_FUSE_MASK
Definition: MIMXRT1052.h:27911
OCOTP_Type::READ_FUSE_DATA
__IO uint32_t READ_FUSE_DATA
Definition: MIMXRT1052.h:27709
OCOTP_Type::TIMING
__IO uint32_t TIMING
Definition: MIMXRT1052.h:27703
OCOTP_TIMING_STROBE_READ_MASK
#define OCOTP_TIMING_STROBE_READ_MASK
Definition: MIMXRT1052.h:27894
OCOTP_Type::CTRL
__IO uint32_t CTRL
Definition: MIMXRT1052.h:27699
OCOTP_Init
void OCOTP_Init(OCOTP_Type *base, uint32_t srcClock_Hz)
Initializes OCOTP controller.
Definition: fsl_ocotp.c:114
OCOTP_TIMING_READ_NS
#define OCOTP_TIMING_READ_NS
Definition: fsl_ocotp.c:25
_ocotp_timing::strobe_read
uint32_t strobe_read
Definition: fsl_ocotp.h:49
OCOTP_TIMING_PROGRAM_NS
#define OCOTP_TIMING_PROGRAM_NS
Definition: fsl_ocotp.c:23
CLOCK_DisableClock
static void CLOCK_DisableClock(clock_ip_name_t name)
Disable the clock for specific IP.
Definition: fsl_clock.h:1069
OCOTP_TIMING_RELAX_MASK
#define OCOTP_TIMING_RELAX_MASK
Definition: MIMXRT1052.h:27891
OCOTP_Type
Definition: MIMXRT1052.h:27698
OCOTP_Type::CTRL_CLR
__IO uint32_t CTRL_CLR
Definition: MIMXRT1052.h:27701
OCOTP_CheckErrorStatus
static bool OCOTP_CheckErrorStatus(OCOTP_Type *base)
Checking the ERROR bit in CTRL register.
Definition: fsl_ocotp.h:93
_ocotp_timing::wait
uint32_t wait
Definition: fsl_ocotp.h:46
OCOTP_CTRL_RELOAD_SHADOWS_MASK
#define OCOTP_CTRL_RELOAD_SHADOWS_MASK
Definition: MIMXRT1052.h:27821
OCOTP_Type::CTRL_SET
__IO uint32_t CTRL_SET
Definition: MIMXRT1052.h:27700
status_t
int32_t status_t
Type used for all status and error return values.
Definition: fsl_common.h:189
kStatus_Success
@ kStatus_Success
Definition: fsl_common.h:179
OCOTP_TIMING_STROBE_PROG_MASK
#define OCOTP_TIMING_STROBE_PROG_MASK
Definition: MIMXRT1052.h:27888
OCOTP_TIMING_STROBE_PROG
#define OCOTP_TIMING_STROBE_PROG(x)
Definition: MIMXRT1052.h:27890
OCOTP_TIMING_WAIT
#define OCOTP_TIMING_WAIT(x)
Definition: MIMXRT1052.h:27899


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:13:56