1 /******************************************************************************
2  *
3  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
4  * Analog Devices, Inc.),
5  * Copyright (C) 2023-2024 Analog Devices, Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************************/
20 
21 #include "mxc_device.h"
22 #include "mxc_errors.h"
23 #include "mxc_assert.h"
24 #include "mxc_sys.h"
25 #include "ctb_reva.h"
26 #include "ctb_common.h"
27 #include "trng_regs.h"
28 
29 /* ************************************************************************* */
30 /* Global Control/Configuration functions                                    */
31 /* ************************************************************************* */
32 
33 /********************************************************/
34 
MXC_CTB_Init(uint32_t features)35 int MXC_CTB_Init(uint32_t features)
36 {
37     if (features & ~MXC_CTB_FEATURE_TRNG) {
38         MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_CTB);
39     }
40 
41     MXC_CTB_RevA_Init((mxc_ctb_reva_regs_t *)MXC_CTB, features);
42 
43     if (features & MXC_CTB_FEATURE_TRNG) {
44         MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_TRNG);
45     }
46 
47     return E_NO_ERROR;
48 }
49 
MXC_CTB_CheckFeatures(void)50 uint32_t MXC_CTB_CheckFeatures(void)
51 {
52     return MXC_CTB_FEATURE_DMA | MXC_CTB_FEATURE_ECC | MXC_CTB_FEATURE_CIPHER |
53            MXC_CTB_FEATURE_HASH | MXC_CTB_FEATURE_CRC | MXC_CTB_FEATURE_TRNG;
54 }
55 
MXC_CTB_EnableInt(void)56 void MXC_CTB_EnableInt(void)
57 {
58     MXC_CTB_RevA_EnableInt((mxc_ctb_reva_regs_t *)MXC_CTB);
59 }
60 
MXC_CTB_DisableInt(void)61 void MXC_CTB_DisableInt(void)
62 {
63     MXC_CTB_RevA_DisableInt((mxc_ctb_reva_regs_t *)MXC_CTB);
64 }
65 
MXC_CTB_Ready(void)66 int MXC_CTB_Ready(void)
67 {
68     return MXC_CTB_RevA_Ready((mxc_ctb_reva_regs_t *)MXC_CTB);
69 }
70 
MXC_CTB_DoneClear(uint32_t features)71 void MXC_CTB_DoneClear(uint32_t features)
72 {
73     MXC_CTB_RevA_DoneClear((mxc_ctb_reva_regs_t *)MXC_CTB, features);
74 }
75 
MXC_CTB_Done(void)76 uint32_t MXC_CTB_Done(void)
77 {
78     return MXC_CTB_RevA_Done((mxc_ctb_reva_regs_t *)MXC_CTB);
79 }
80 
MXC_CTB_Reset(uint32_t features)81 void MXC_CTB_Reset(uint32_t features)
82 {
83     MXC_CTB_RevA_Reset(features);
84 }
85 
MXC_CTB_CacheInvalidate(void)86 void MXC_CTB_CacheInvalidate(void)
87 {
88     MXC_CTB_RevA_CacheInvalidate();
89 }
90 
MXC_CTB_Shutdown(uint32_t features)91 int MXC_CTB_Shutdown(uint32_t features)
92 {
93     uint32_t new_features;
94 
95     int error = MXC_CTB_RevA_Shutdown(features);
96 
97     if (error != E_NO_ERROR) {
98         return error;
99     }
100 
101     new_features = MXC_CTB_GetEnabledFeatures();
102 
103     // If CTB is not needed, disable clock
104     if ((new_features & ~MXC_CTB_FEATURE_TRNG) == 0) {
105         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_CTB);
106     }
107 
108     // If shutting down TRNG, disable clock
109     if (features & MXC_CTB_FEATURE_TRNG) {
110         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_TRNG);
111     }
112 
113     return E_NO_ERROR;
114 }
115 
MXC_CTB_GetEnabledFeatures(void)116 uint32_t MXC_CTB_GetEnabledFeatures(void)
117 {
118     return MXC_CTB_RevA_GetEnabledFeatures();
119 }
120 
MXC_CTB_Handler(void)121 void MXC_CTB_Handler(void)
122 {
123     MXC_CTB_RevA_Handler((mxc_trng_reva_regs_t *)MXC_TRNG);
124 }
125 
126 /************************************/
127 /* CTB DMA - Used for all features  */
128 /************************************/
129 
MXC_CTB_DMA_SetReadSource(mxc_ctb_dma_read_source_t source)130 void MXC_CTB_DMA_SetReadSource(mxc_ctb_dma_read_source_t source)
131 {
132     MXC_CTB_RevA_DMA_SetReadSource((mxc_ctb_reva_regs_t *)MXC_CTB, source);
133 }
134 
MXC_CTB_DMA_GetReadSource(void)135 mxc_ctb_dma_read_source_t MXC_CTB_DMA_GetReadSource(void)
136 {
137     return MXC_CTB_RevA_DMA_GetReadSource((mxc_ctb_reva_regs_t *)MXC_CTB);
138 }
139 
MXC_CTB_DMA_SetWriteSource(mxc_ctb_dma_write_source_t source)140 void MXC_CTB_DMA_SetWriteSource(mxc_ctb_dma_write_source_t source)
141 {
142     MXC_CTB_RevA_DMA_SetWriteSource((mxc_ctb_reva_regs_t *)MXC_CTB, source);
143 }
144 
MXC_CTB_DMA_GetWriteSource(void)145 mxc_ctb_dma_write_source_t MXC_CTB_DMA_GetWriteSource(void)
146 {
147     return MXC_CTB_RevA_DMA_GetWriteSource((mxc_ctb_reva_regs_t *)MXC_CTB);
148 }
149 
MXC_CTB_DMA_SetSource(uint8_t * source)150 void MXC_CTB_DMA_SetSource(uint8_t *source)
151 {
152     MXC_ASSERT(((uint32_t)source > 0x100FFFFF) || ((uint32_t)source < 0x08000000));
153     MXC_CTB_RevA_DMA_SetSource((mxc_ctb_reva_regs_t *)MXC_CTB, source);
154 }
155 
MXC_CTB_DMA_SetDestination(uint8_t * dest)156 void MXC_CTB_DMA_SetDestination(uint8_t *dest)
157 {
158     MXC_CTB_RevA_DMA_SetDestination((mxc_ctb_reva_regs_t *)MXC_CTB, dest);
159 }
160 
MXC_CTB_DMA_SetupOperation(mxc_ctb_dma_req_t * req)161 int MXC_CTB_DMA_SetupOperation(mxc_ctb_dma_req_t *req)
162 {
163     return MXC_CTB_RevA_DMA_SetupOperation((mxc_ctb_reva_dma_req_t *)req);
164 }
165 
MXC_CTB_DMA_DoOperation(mxc_ctb_dma_req_t * req)166 int MXC_CTB_DMA_DoOperation(mxc_ctb_dma_req_t *req)
167 {
168     return MXC_CTB_RevA_DMA_DoOperation((mxc_ctb_reva_dma_req_t *)req);
169 }
170 
MXC_CTB_DMA_StartTransfer(uint32_t length)171 void MXC_CTB_DMA_StartTransfer(uint32_t length)
172 {
173     MXC_CTB_RevA_DMA_StartTransfer((mxc_ctb_reva_regs_t *)MXC_CTB, length);
174 }
175 
176 /* ************************************************************************* */
177 /* True Random Number Generator (TRNG) functions                             */
178 /* ************************************************************************* */
179 
MXC_CTB_TRNG_RandomInt(void)180 int MXC_CTB_TRNG_RandomInt(void)
181 {
182     return MXC_CTB_RevA_TRNG_RandomInt((mxc_trng_reva_regs_t *)MXC_TRNG);
183 }
184 
MXC_CTB_TRNG_Random(uint8_t * data,uint32_t len)185 int MXC_CTB_TRNG_Random(uint8_t *data, uint32_t len)
186 {
187     return MXC_CTB_RevA_TRNG_Random(data, len);
188 }
189 
MXC_CTB_TRNG_RandomAsync(uint8_t * data,uint32_t len,mxc_ctb_complete_cb_t callback)190 void MXC_CTB_TRNG_RandomAsync(uint8_t *data, uint32_t len, mxc_ctb_complete_cb_t callback)
191 {
192     MXC_CTB_RevA_TRNG_RandomAsync((mxc_trng_reva_regs_t *)MXC_TRNG, data, len, callback);
193 }
194 
195 /* ************************************************************************* */
196 /* Error Correction Code (ECC) functions                                     */
197 /* ************************************************************************* */
198 
199 /*******************************/
200 /* Low Level Functions         */
201 /*******************************/
202 
MXC_CTB_ECC_Enable(void)203 void MXC_CTB_ECC_Enable(void)
204 {
205     MXC_CTB_RevA_ECC_Enable((mxc_ctb_reva_regs_t *)MXC_CTB);
206 }
207 
MXC_CTB_ECC_Disable(void)208 void MXC_CTB_ECC_Disable(void)
209 {
210     MXC_CTB_RevA_ECC_Disable((mxc_ctb_reva_regs_t *)MXC_CTB);
211 }
212 
MXC_CTB_ECC_GetResult(void)213 uint32_t MXC_CTB_ECC_GetResult(void)
214 {
215     return MXC_CTB_RevA_ECC_GetResult((mxc_ctb_reva_regs_t *)MXC_CTB);
216 }
217 
218 /*******************************/
219 /* High Level Functions        */
220 /*******************************/
221 
MXC_CTB_ECC_Compute(mxc_ctb_ecc_req_t * req)222 int MXC_CTB_ECC_Compute(mxc_ctb_ecc_req_t *req)
223 {
224     return MXC_CTB_RevA_ECC_Compute((mxc_ctb_reva_ecc_req_t *)req);
225 }
226 
MXC_CTB_ECC_ErrorCheck(mxc_ctb_ecc_req_t * req)227 int MXC_CTB_ECC_ErrorCheck(mxc_ctb_ecc_req_t *req)
228 {
229     return MXC_CTB_RevA_ECC_ErrorCheck((mxc_ctb_reva_ecc_req_t *)req);
230 }
231 
MXC_CTB_ECC_ComputeAsync(mxc_ctb_ecc_req_t * req)232 void MXC_CTB_ECC_ComputeAsync(mxc_ctb_ecc_req_t *req)
233 {
234     MXC_CTB_RevA_ECC_ComputeAsync((mxc_ctb_reva_ecc_req_t *)req);
235 }
236 
MXC_CTB_ECC_ErrorCheckAsync(mxc_ctb_ecc_req_t * req)237 void MXC_CTB_ECC_ErrorCheckAsync(mxc_ctb_ecc_req_t *req)
238 {
239     MXC_CTB_RevA_ECC_ErrorCheckAsync((mxc_ctb_reva_ecc_req_t *)req);
240 }
241 
242 /* ************************************************************************* */
243 /* Cyclic Redundancy Check (CRC) functions                                   */
244 /* ************************************************************************* */
245 
246 /*******************************/
247 /* Low Level Functions         */
248 /*******************************/
249 
MXC_CTB_CRC_SetDirection(mxc_ctb_crc_bitorder_t bitOrder)250 void MXC_CTB_CRC_SetDirection(mxc_ctb_crc_bitorder_t bitOrder)
251 {
252     MXC_CTB_RevA_CRC_SetDirection((mxc_ctb_reva_regs_t *)MXC_CTB, bitOrder);
253 }
254 
MXC_CTB_CRC_GetDirection(void)255 mxc_ctb_crc_bitorder_t MXC_CTB_CRC_GetDirection(void)
256 {
257     return MXC_CTB_RevA_CRC_GetDirection((mxc_ctb_reva_regs_t *)MXC_CTB);
258 }
259 
MXC_CTB_CRC_SetPoly(uint32_t poly)260 void MXC_CTB_CRC_SetPoly(uint32_t poly)
261 {
262     MXC_CTB_RevA_CRC_SetPoly((mxc_ctb_reva_regs_t *)MXC_CTB, poly);
263 }
264 
MXC_CTB_CRC_GetPoly(void)265 uint32_t MXC_CTB_CRC_GetPoly(void)
266 {
267     return MXC_CTB_RevA_CRC_GetPoly((mxc_ctb_reva_regs_t *)MXC_CTB);
268 }
269 
MXC_CTB_CRC_GetResult(void)270 uint32_t MXC_CTB_CRC_GetResult(void)
271 {
272     return MXC_CTB_RevA_CRC_GetResult((mxc_ctb_reva_regs_t *)MXC_CTB);
273 }
274 
MXC_CTB_CRC_SetInitialValue(uint32_t seed)275 void MXC_CTB_CRC_SetInitialValue(uint32_t seed)
276 {
277     MXC_CTB_RevA_CRC_SetInitialValue(seed);
278 }
279 
MXC_CTB_CRC_SetFinalXORValue(uint32_t xor)280 void MXC_CTB_CRC_SetFinalXORValue(uint32_t xor)
281 {
282     MXC_CTB_RevA_CRC_SetFinalXORValue(xor);
283 }
284 
285 /*******************************/
286 /* High Level Functions        */
287 /*******************************/
288 
MXC_CTB_CRC_Compute(mxc_ctb_crc_req_t * req)289 int MXC_CTB_CRC_Compute(mxc_ctb_crc_req_t *req)
290 {
291     return MXC_CTB_RevA_CRC_Compute((mxc_ctb_reva_regs_t *)MXC_CTB, (mxc_ctb_reva_crc_req_t *)req);
292 }
293 
MXC_CTB_CRC_ComputeAsync(mxc_ctb_crc_req_t * req)294 void MXC_CTB_CRC_ComputeAsync(mxc_ctb_crc_req_t *req)
295 {
296     MXC_CTB_RevA_CRC_ComputeAsync((mxc_ctb_reva_regs_t *)MXC_CTB, (mxc_ctb_reva_crc_req_t *)req);
297 }
298 
299 /* ************************************************************************* */
300 /* Hash functions                                                            */
301 /* ************************************************************************* */
302 
303 /***********************/
304 /* Low Level Functions */
305 /***********************/
306 
MXC_CTB_Hash_GetBlockSize(mxc_ctb_hash_func_t function)307 unsigned int MXC_CTB_Hash_GetBlockSize(mxc_ctb_hash_func_t function)
308 {
309     return MXC_CTB_Common_Hash_GetBlockSize(function);
310 }
311 
MXC_CTB_Hash_GetDigestSize(mxc_ctb_hash_func_t function)312 unsigned int MXC_CTB_Hash_GetDigestSize(mxc_ctb_hash_func_t function)
313 {
314     return MXC_CTB_Common_Hash_GetDigestSize(function);
315 }
316 
MXC_CTB_Hash_SetFunction(mxc_ctb_hash_func_t function)317 void MXC_CTB_Hash_SetFunction(mxc_ctb_hash_func_t function)
318 {
319     MXC_CTB_RevA_Hash_SetFunction((mxc_ctb_reva_regs_t *)MXC_CTB, function);
320 }
321 
MXC_CTB_Hash_GetFunction(void)322 mxc_ctb_hash_func_t MXC_CTB_Hash_GetFunction(void)
323 {
324     return MXC_CTB_RevA_Hash_GetFunction((mxc_ctb_reva_regs_t *)MXC_CTB);
325 }
326 
MXC_CTB_Hash_SetAutoPad(int pad)327 void MXC_CTB_Hash_SetAutoPad(int pad)
328 {
329     MXC_CTB_RevA_Hash_SetAutoPad((mxc_ctb_reva_regs_t *)MXC_CTB, pad);
330 }
331 
MXC_CTB_Hash_GetAutoPad(void)332 int MXC_CTB_Hash_GetAutoPad(void)
333 {
334     return MXC_CTB_RevA_Hash_GetAutoPad((mxc_ctb_reva_regs_t *)MXC_CTB);
335 }
336 
MXC_CTB_Hash_GetResult(uint8_t * digest,int * len)337 void MXC_CTB_Hash_GetResult(uint8_t *digest, int *len)
338 {
339     MXC_CTB_RevA_Hash_GetResult((mxc_ctb_reva_regs_t *)MXC_CTB, digest, len);
340 }
341 
MXC_CTB_Hash_SetMessageSize(uint32_t size)342 void MXC_CTB_Hash_SetMessageSize(uint32_t size)
343 {
344     MXC_CTB_RevA_Hash_SetMessageSize((mxc_ctb_reva_regs_t *)MXC_CTB, size);
345 }
346 
MXC_CTB_Hash_SetSource(mxc_ctb_hash_source_t source)347 void MXC_CTB_Hash_SetSource(mxc_ctb_hash_source_t source)
348 {
349     MXC_CTB_RevA_Hash_SetSource((mxc_ctb_reva_regs_t *)MXC_CTB, source);
350 }
351 
MXC_CTB_Hash_GetSource(void)352 mxc_ctb_hash_source_t MXC_CTB_Hash_GetSource(void)
353 {
354     return MXC_CTB_RevA_Hash_GetSource((mxc_ctb_reva_regs_t *)MXC_CTB);
355 }
356 
MXC_CTB_Hash_InitializeHash(void)357 void MXC_CTB_Hash_InitializeHash(void)
358 {
359     MXC_CTB_RevA_Hash_InitializeHash((mxc_ctb_reva_regs_t *)MXC_CTB);
360 }
361 
362 /************************/
363 /* High Level Functions */
364 /************************/
365 
MXC_CTB_Hash_Compute(mxc_ctb_hash_req_t * req)366 int MXC_CTB_Hash_Compute(mxc_ctb_hash_req_t *req)
367 {
368     return MXC_CTB_RevA_Hash_Compute((mxc_ctb_reva_hash_req_t *)req);
369 }
370 
MXC_CTB_Hash_ComputeAsync(mxc_ctb_hash_req_t * req)371 void MXC_CTB_Hash_ComputeAsync(mxc_ctb_hash_req_t *req)
372 {
373     MXC_CTB_RevA_Hash_ComputeAsync((mxc_ctb_reva_hash_req_t *)req);
374 }
375 
376 /* ************************************************************************* */
377 /* Cipher functions                                                          */
378 /* ************************************************************************* */
379 
380 /************************/
381 /* Low Level Functions  */
382 /************************/
383 
MXC_CTB_Cipher_GetKeySize(mxc_ctb_cipher_t cipher)384 unsigned int MXC_CTB_Cipher_GetKeySize(mxc_ctb_cipher_t cipher)
385 {
386     return MXC_CTB_Common_Cipher_GetKeySize(cipher);
387 }
388 
MXC_CTB_Cipher_GetBlockSize(mxc_ctb_cipher_t cipher)389 unsigned int MXC_CTB_Cipher_GetBlockSize(mxc_ctb_cipher_t cipher)
390 {
391     return MXC_CTB_Common_Cipher_GetBlockSize(cipher);
392 }
393 
MXC_CTB_Cipher_SetMode(mxc_ctb_cipher_mode_t mode)394 void MXC_CTB_Cipher_SetMode(mxc_ctb_cipher_mode_t mode)
395 {
396     MXC_CTB_RevA_Cipher_SetMode((mxc_ctb_reva_regs_t *)MXC_CTB, mode);
397 }
398 
MXC_CTB_Cipher_GetMode(void)399 mxc_ctb_cipher_mode_t MXC_CTB_Cipher_GetMode(void)
400 {
401     return MXC_CTB_RevA_Cipher_GetMode((mxc_ctb_reva_regs_t *)MXC_CTB);
402 }
403 
MXC_CTB_Cipher_SetCipher(mxc_ctb_cipher_t cipher)404 void MXC_CTB_Cipher_SetCipher(mxc_ctb_cipher_t cipher)
405 {
406     MXC_CTB_RevA_Cipher_SetCipher((mxc_ctb_reva_regs_t *)MXC_CTB, cipher);
407 }
408 
MXC_CTB_Cipher_GetCipher(void)409 mxc_ctb_cipher_t MXC_CTB_Cipher_GetCipher(void)
410 {
411     return MXC_CTB_RevA_Cipher_GetCipher((mxc_ctb_reva_regs_t *)MXC_CTB);
412 }
413 
MXC_CTB_Cipher_SetKeySource(mxc_ctb_cipher_key_t source)414 void MXC_CTB_Cipher_SetKeySource(mxc_ctb_cipher_key_t source)
415 {
416     MXC_CTB_RevA_Cipher_SetKeySource((mxc_ctb_reva_regs_t *)MXC_CTB, source);
417 }
418 
MXC_CTB_Cipher_GetKeySource(void)419 mxc_ctb_cipher_key_t MXC_CTB_Cipher_GetKeySource(void)
420 {
421     return MXC_CTB_RevA_Cipher_GetKeySource((mxc_ctb_reva_regs_t *)MXC_CTB);
422 }
423 
MXC_CTB_Cipher_LoadKey(void)424 void MXC_CTB_Cipher_LoadKey(void)
425 {
426     MXC_CTB_RevA_Cipher_LoadKey((mxc_ctb_reva_regs_t *)MXC_CTB);
427 }
428 
MXC_CTB_Cipher_SetOperation(mxc_ctb_cipher_operation_t operation)429 void MXC_CTB_Cipher_SetOperation(mxc_ctb_cipher_operation_t operation)
430 {
431     MXC_CTB_RevA_Cipher_SetOperation((mxc_ctb_reva_regs_t *)MXC_CTB, operation);
432 }
433 
MXC_CTB_Cipher_SetKey(uint8_t * key,uint32_t len)434 void MXC_CTB_Cipher_SetKey(uint8_t *key, uint32_t len)
435 {
436     MXC_CTB_RevA_Cipher_SetKey((mxc_ctb_reva_regs_t *)MXC_CTB, key, len);
437 }
438 
MXC_CTB_Cipher_SetIV(uint8_t * iv,uint32_t len)439 void MXC_CTB_Cipher_SetIV(uint8_t *iv, uint32_t len)
440 {
441     MXC_CTB_RevA_Cipher_SetIV((mxc_ctb_reva_regs_t *)MXC_CTB, iv, len);
442 }
443 
MXC_CTB_Cipher_GetIV(uint8_t * ivOut,uint32_t len)444 void MXC_CTB_Cipher_GetIV(uint8_t *ivOut, uint32_t len)
445 {
446     MXC_CTB_RevA_Cipher_GetIV((mxc_ctb_reva_regs_t *)MXC_CTB, ivOut, len);
447 }
448 
449 /************************/
450 /* High Level Functions */
451 /************************/
452 
MXC_CTB_Cipher_Encrypt(mxc_ctb_cipher_req_t * req)453 int MXC_CTB_Cipher_Encrypt(mxc_ctb_cipher_req_t *req)
454 {
455     return MXC_CTB_RevA_Cipher_Encrypt((mxc_ctb_reva_cipher_req_t *)req);
456 }
457 
MXC_CTB_Cipher_Decrypt(mxc_ctb_cipher_req_t * req)458 int MXC_CTB_Cipher_Decrypt(mxc_ctb_cipher_req_t *req)
459 {
460     return MXC_CTB_RevA_Cipher_Decrypt((mxc_ctb_reva_cipher_req_t *)req);
461 }
462 
MXC_CTB_Cipher_EncryptAsync(mxc_ctb_cipher_req_t * req)463 void MXC_CTB_Cipher_EncryptAsync(mxc_ctb_cipher_req_t *req)
464 {
465     MXC_CTB_RevA_Cipher_EncryptAsync((mxc_ctb_reva_cipher_req_t *)req);
466 }
467 
MXC_CTB_Cipher_DecryptAsync(mxc_ctb_cipher_req_t * req)468 void MXC_CTB_Cipher_DecryptAsync(mxc_ctb_cipher_req_t *req)
469 {
470     MXC_CTB_RevA_Cipher_DecryptAsync((mxc_ctb_reva_cipher_req_t *)req);
471 }
472