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