1 /***************************************************************************//**
2 * \file cy_crypto_core_hw_v1.c
3 * \version 2.120
4 *
5 * \brief
6 *  This file provides the source code for the HAL API for the
7 *  in the Crypto driver.
8 *
9 ********************************************************************************
10 * \copyright
11 * Copyright (c) (2020-2022), Cypress Semiconductor Corporation (an Infineon company) or
12 * an affiliate of Cypress Semiconductor Corporation.
13 * SPDX-License-Identifier: Apache-2.0
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License");
16 * you may not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 *    http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 *******************************************************************************/
27 
28 #include "cy_device.h"
29 
30 #if defined(CY_IP_MXCRYPTO)
31 
32 #include "cy_crypto_core_hw_v1.h"
33 
34 #if defined(CY_CRYPTO_CFG_HW_V1_ENABLE)
35 
36 #if defined(__cplusplus)
37 extern "C" {
38 #endif
39 
40 #include "cy_syslib.h"
41 
42 /*******************************************************************************
43 * Function Name: Cy_Crypto_SetReg1Instr
44 ****************************************************************************//**
45 *
46 * Writes one 32-Bit data word into Crypto FIFO.
47 *
48 * \param base
49 * The pointer to the CRYPTO instance.
50 *
51 * \param data0
52 * The address of data to be placed into Crypto FIFO
53 * on the address CRYPTO_REGFILE_R0.
54 *
55 *******************************************************************************/
Cy_Crypto_SetReg1Instr(CRYPTO_Type * base,uint32_t data0)56 void Cy_Crypto_SetReg1Instr(CRYPTO_Type *base, uint32_t data0)
57 {
58     /* Check whether FIFO has enough space for 1 instruction */
59     while(Cy_Crypto_Core_GetFIFOUsed(base) >= (CY_CRYPTO_INSTR_FIFODEPTH - 1u))
60     {
61     }
62 
63     REG_CRYPTO_INSTR_FF_WR(base) = (uint32_t)(((uint32_t)CY_CRYPTO_V1_SET_REG1_OPC << CY_CRYPTO_OPCODE_POS) |
64                                    (uint32_t)CY_CRYPTO_REGFILE_R0);
65 
66     REG_CRYPTO_INSTR_FF_WR(base) = data0;
67 }
68 
69 /*******************************************************************************
70 * Function Name: Cy_Crypto_SetReg2Instr
71 ****************************************************************************//**
72 *
73 * Writes two 32-Bit data words into Crypto FIFO.
74 *
75 * \param base
76 * The pointer to the CRYPTO instance.
77 *
78 * \param data0
79 * The address of data to be placed into Crypto FIFO
80 * on the address CRYPTO_REGFILE_R0.
81 *
82 * \param data1
83 * The address of data to be placed into Crypto FIFO
84 * on the address CRYPTO_REGFILE_R1.
85 *
86 *******************************************************************************/
Cy_Crypto_SetReg2Instr(CRYPTO_Type * base,uint32_t data0,uint32_t data1)87 void Cy_Crypto_SetReg2Instr(CRYPTO_Type *base, uint32_t data0, uint32_t data1)
88 {
89     /* Check whether FIFO has enough space for 2 instructions */
90     while(Cy_Crypto_Core_GetFIFOUsed(base) >= (CY_CRYPTO_INSTR_FIFODEPTH - 2u))
91     {
92     }
93 
94     REG_CRYPTO_INSTR_FF_WR(base) = (uint32_t)(((uint32_t)CY_CRYPTO_V1_SET_REG2_OPC << CY_CRYPTO_OPCODE_POS) |
95                                    ((uint32_t)CY_CRYPTO_REGFILE_R1 << CY_CRYPTO_RSRC4_SHIFT) |
96                                    (uint32_t)CY_CRYPTO_REGFILE_R0);
97 
98     REG_CRYPTO_INSTR_FF_WR(base) = data0;
99     REG_CRYPTO_INSTR_FF_WR(base) = data1;
100 }
101 
102 /*******************************************************************************
103 * Function Name: Cy_Crypto_SetReg3Instr
104 ****************************************************************************//**
105 *
106 * Writes three 32-Bit data words into Crypto FIFO.
107 *
108 * \param base
109 * The pointer to the CRYPTO instance.
110 *
111 * \param data0
112 * The address of data to be placed into Crypto FIFO
113 * on the address CRYPTO_REGFILE_R0.
114 *
115 * \param data1
116 * The address of data to be placed into Crypto FIFO
117 * on the address CRYPTO_REGFILE_R1.
118 *
119 * \param data2
120 * The address of data to be be placed into Crypto FIFO
121 * on the address CRYPTO_REGFILE_R2.
122 *
123 *******************************************************************************/
Cy_Crypto_SetReg3Instr(CRYPTO_Type * base,uint32_t data0,uint32_t data1,uint32_t data2)124 void Cy_Crypto_SetReg3Instr(CRYPTO_Type *base, uint32_t data0, uint32_t data1, uint32_t data2)
125 {
126     /* Check whether FIFO has enough space for 3 instructions */
127     while(Cy_Crypto_Core_GetFIFOUsed(base) >= (CY_CRYPTO_INSTR_FIFODEPTH - 3u))
128     {
129     }
130 
131     REG_CRYPTO_INSTR_FF_WR(base) = (uint32_t)( ((uint32_t)CY_CRYPTO_V1_SET_REG3_OPC << CY_CRYPTO_OPCODE_POS) |
132                                    ((uint32_t)CY_CRYPTO_REGFILE_R2 << CY_CRYPTO_RSRC8_SHIFT) |
133                                    ((uint32_t)CY_CRYPTO_REGFILE_R1 << CY_CRYPTO_RSRC4_SHIFT) |
134                                    (uint32_t)CY_CRYPTO_REGFILE_R0 );
135 
136     REG_CRYPTO_INSTR_FF_WR(base) = data0;
137     REG_CRYPTO_INSTR_FF_WR(base) = data1;
138     REG_CRYPTO_INSTR_FF_WR(base) = data2;
139 }
140 
141 /*******************************************************************************
142 * Function Name: Cy_Crypto_SetReg4Instr
143 ****************************************************************************//**
144 *
145 * Writes four 32-Bit data words into Crypto FIFO.
146 *
147 * \param base
148 * The pointer to the CRYPTO instance.
149 *
150 * \param data0
151 * The address of data to be placed into Crypto FIFO
152 * on the address CRYPTO_REGFILE_R0.
153 *
154 * \param data1
155 * The address of data to be placed into Crypto FIFO
156 * on the address CRYPTO_REGFILE_R1.
157 *
158 * \param data2
159 * The address of data to be placed into Crypto FIFO
160 * on the address CRYPTO_REGFILE_R2.
161 *
162 * \param data3
163 * The address of data to be placed into Crypto FIFO
164 * on the address CRYPTO_REGFILE_R3.
165 *
166 *******************************************************************************/
Cy_Crypto_SetReg4Instr(CRYPTO_Type * base,uint32_t data0,uint32_t data1,uint32_t data2,uint32_t data3)167 void Cy_Crypto_SetReg4Instr(CRYPTO_Type *base, uint32_t data0, uint32_t data1, uint32_t data2, uint32_t data3)
168 {
169     /* Check whether FIFO has enough space for 4 instructions */
170     while(Cy_Crypto_Core_GetFIFOUsed(base) >= (CY_CRYPTO_INSTR_FIFODEPTH - 4u))
171     {
172     }
173 
174     REG_CRYPTO_INSTR_FF_WR(base) = (uint32_t)(((uint32_t)CY_CRYPTO_V1_SET_REG4_OPC << CY_CRYPTO_OPCODE_POS) |
175                                    ((uint32_t)CY_CRYPTO_REGFILE_R3 << CY_CRYPTO_RSRC12_SHIFT) |
176                                    ((uint32_t)CY_CRYPTO_REGFILE_R2 << CY_CRYPTO_RSRC8_SHIFT) |
177                                    ((uint32_t)CY_CRYPTO_REGFILE_R1 << CY_CRYPTO_RSRC4_SHIFT) |
178                                    (uint32_t)CY_CRYPTO_REGFILE_R0);
179 
180     REG_CRYPTO_INSTR_FF_WR(base) = data0;
181     REG_CRYPTO_INSTR_FF_WR(base) = data1;
182     REG_CRYPTO_INSTR_FF_WR(base) = data2;
183     REG_CRYPTO_INSTR_FF_WR(base) = data3;
184 }
185 
186 /*******************************************************************************
187 * Function Name: Cy_Crypto_Run0ParamInstr
188 *****************************************************************************//**
189 *
190 * Run the Crypto instruction without parameters.
191 *
192 * \param base
193 * The pointer to the CRYPTO instance.
194 *
195 * \param instr
196 * The Opcode of the called instruction.
197 *
198 *******************************************************************************/
Cy_Crypto_Run0ParamInstr(CRYPTO_Type * base,uint8_t instr)199 void Cy_Crypto_Run0ParamInstr(CRYPTO_Type *base, uint8_t instr)
200 {
201     /* Check whether FIFO has enough space for 1 instruction */
202     while(Cy_Crypto_Core_GetFIFOUsed(base) >= CY_CRYPTO_INSTR_FIFODEPTH)
203     {
204     }
205 
206     REG_CRYPTO_INSTR_FF_WR(base) = (uint32_t)((uint32_t)instr << CY_CRYPTO_OPCODE_POS);
207 }
208 
209 /*******************************************************************************
210 * Function Name: Cy_Crypto_Run1ParamInstr
211 *****************************************************************************//**
212 *
213 * Run the Crypto instruction with one parameter.
214 * The parameter must be placed into register 0
215 *
216 * \param base
217 * The pointer to the CRYPTO instance.
218 *
219 * \param instr
220 * The Opcode of the called instruction.
221 *
222 * \param rdst0Shift
223 * The shift for the instruction operand.
224 *
225 *******************************************************************************/
Cy_Crypto_Run1ParamInstr(CRYPTO_Type * base,uint8_t instr,uint32_t rdst0Shift)226 void Cy_Crypto_Run1ParamInstr(CRYPTO_Type *base, uint8_t instr, uint32_t rdst0Shift)
227 {
228     /* Check whether FIFO has enough space for 1 instruction */
229     while(Cy_Crypto_Core_GetFIFOUsed(base) >= CY_CRYPTO_INSTR_FIFODEPTH)
230     {
231     }
232 
233     REG_CRYPTO_INSTR_FF_WR(base) = (uint32_t)(((uint32_t)instr << CY_CRYPTO_OPCODE_POS) |
234                                    ((uint32_t)CY_CRYPTO_REGFILE_R0 << rdst0Shift));
235 }
236 
237 /*******************************************************************************
238 * Function Name: Cy_Crypto_Run2ParamInstr
239 *****************************************************************************//**
240 *
241 * Run the Crypto instruction with two parameters.
242 * The zero parameter must be placed into register 0,
243 * the first parameter must be placed into register 1.
244 *
245 * \param base
246 * The pointer to the CRYPTO instance.
247 *
248 * \param instr
249 * The Opcode of the called instruction.
250 *
251 * \param rdst0Shift
252 * The shift for the zero instruction operand.
253 *
254 * \param rdst1Shift
255 * The shift for the second instruction operand.
256 *
257 *******************************************************************************/
Cy_Crypto_Run2ParamInstr(CRYPTO_Type * base,uint8_t instr,uint32_t rdst0Shift,uint32_t rdst1Shift)258 void Cy_Crypto_Run2ParamInstr(CRYPTO_Type *base,
259                            uint8_t instr,
260                            uint32_t rdst0Shift,
261                            uint32_t rdst1Shift)
262 {
263     /* Check whether FIFO has enough space for 1 instruction */
264     while(Cy_Crypto_Core_GetFIFOUsed(base) >= CY_CRYPTO_INSTR_FIFODEPTH)
265     {
266     }
267 
268     REG_CRYPTO_INSTR_FF_WR(base) = (uint32_t)(((uint32_t)instr << CY_CRYPTO_OPCODE_POS) |
269                                    ((uint32_t)CY_CRYPTO_REGFILE_R1 << rdst1Shift) |
270                                    ((uint32_t)CY_CRYPTO_REGFILE_R0 << rdst0Shift));
271 }
272 
273 /*******************************************************************************
274 * Function Name: Cy_Crypto_Run3ParamInstr
275 *****************************************************************************//**
276 *
277 * Run the Crypto instruction with three parameters.
278 * The zero parameter must be placed into register 0,
279 * the first parameter must be placed into register 1,
280 * the second parameter must be placed into register 2.
281 *
282 * \param base
283 * The pointer to the CRYPTO instance.
284 *
285 * \param instr
286 * The Opcode of the called instruction.
287 *
288 * \param rdst0Shift
289 * The shift for the zero instruction operand.
290 *
291 * \param rdst1Shift
292 * The shift for the second instruction operand.
293 *
294 ** \param rdst2Shift
295 * The shift for the second instruction operand.
296 *
297 *******************************************************************************/
Cy_Crypto_Run3ParamInstr(CRYPTO_Type * base,uint8_t instr,uint8_t rdst0Shift,uint8_t rdst1Shift,uint8_t rdst2Shift)298 void Cy_Crypto_Run3ParamInstr(CRYPTO_Type *base,
299                            uint8_t instr,
300                            uint8_t rdst0Shift,
301                            uint8_t rdst1Shift,
302                            uint8_t rdst2Shift)
303 {
304     /* Check whether FIFO has enough space for 1 instruction */
305     while(Cy_Crypto_Core_GetFIFOUsed(base) >= CY_CRYPTO_INSTR_FIFODEPTH)
306     {
307     }
308 
309     REG_CRYPTO_INSTR_FF_WR(base) = (uint32_t)(((uint32_t)instr << CY_CRYPTO_OPCODE_POS) |
310                                    ((uint32_t)CY_CRYPTO_REGFILE_R2 << rdst2Shift) |
311                                    ((uint32_t)CY_CRYPTO_REGFILE_R1 << rdst1Shift) |
312                                    ((uint32_t)CY_CRYPTO_REGFILE_R0 << rdst0Shift));
313 }
314 
315 /*******************************************************************************
316 * Function Name: Cy_Crypto_Run4ParamInstr
317 *****************************************************************************//**
318 *
319 * Run the Crypto instruction with four parameters.
320 * The zero parameter must be placed into register 0,
321 * the first parameter must be placed into register 1,
322 * the second parameter must be placed into register 2,
323 * the third parameter must be placed into register 3.
324 *
325 * \param base
326 * The pointer to the CRYPTO instance.
327 *
328 * \param instr
329 * The Opcode of the called instruction.
330 *
331 * \param rdst0Shift
332 * The shift for the zero instruction operand.
333 *
334 * \param rdst1Shift
335 * The shift for the first instruction operand.
336 *
337 * \param rdst2Shift
338 * The shift for the second instruction operand.
339 *
340 * \param rdst3Shift
341 * The shift for the third instruction operand.
342 *
343 *******************************************************************************/
Cy_Crypto_Run4ParamInstr(CRYPTO_Type * base,uint8_t instr,uint32_t rdst0Shift,uint32_t rdst1Shift,uint32_t rdst2Shift,uint32_t rdst3Shift)344 void Cy_Crypto_Run4ParamInstr(CRYPTO_Type *base,
345                            uint8_t instr,
346                            uint32_t rdst0Shift,
347                            uint32_t rdst1Shift,
348                            uint32_t rdst2Shift,
349                            uint32_t rdst3Shift)
350 {
351     /* Check whether FIFO has enough space for 1 instruction */
352     while(Cy_Crypto_Core_GetFIFOUsed(base) >= CY_CRYPTO_INSTR_FIFODEPTH)
353     {
354     }
355 
356     REG_CRYPTO_INSTR_FF_WR(base) = (uint32_t)(((uint32_t)instr << CY_CRYPTO_OPCODE_POS) |
357                                    ((uint32_t)CY_CRYPTO_REGFILE_R3 << rdst3Shift) |
358                                    ((uint32_t)CY_CRYPTO_REGFILE_R2 << rdst2Shift) |
359                                    ((uint32_t)CY_CRYPTO_REGFILE_R1 << rdst1Shift) |
360                                    ((uint32_t)CY_CRYPTO_REGFILE_R0 << rdst0Shift));
361 }
362 
363 #if defined(__cplusplus)
364 }
365 #endif
366 
367 #endif /* defined(CY_CRYPTO_CFG_HW_V1_ENABLE) */
368 
369 #endif /* defined(CY_IP_MXCRYPTO) */
370 
371 
372 /* [] END OF FILE */
373