xref: /OK3568_Linux_fs/u-boot/drivers/crypto/rockchip/crypto_v2_util.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd
4  */
5 
6 #include <common.h>
7 #include <rockchip/crypto_v2_util.h>
8 
9 /* ------------------------------------------------------------
10  **
11  * @brief This function executes a reversed words copy on a specified buffer.
12  *
13  *        on a 6 words buffer:
14  *
15  *        buff[5] <---> buff[0]
16  *        buff[4] <---> buff[1]
17  *        buff[3] <---> buff[2]
18  *
19  * @param[in] dst_ptr - The counter buffer.
20  * @param[in] size    - The counter size in words.
21  *
22  */
util_reverse_words_buff(u32 * buff_ptr,u32 size_words)23 void util_reverse_words_buff(u32 *buff_ptr, u32 size_words)
24 {
25 	u32 i;
26 	u32 temp;
27 	u32 *high_swap_ptr, *low_swap_ptr;
28 
29 	/* initialize the source and the destination poision */
30 	high_swap_ptr = buff_ptr + size_words - 1;
31 	low_swap_ptr  = buff_ptr;
32 
33 	/* execute the reverse memcpoy */
34 	for (i = 0; i < (size_words / 2); i++) {
35 		temp				= *high_swap_ptr;
36 		*(high_swap_ptr--)	= *low_swap_ptr;
37 		*(low_swap_ptr++)	= temp;
38 	}
39 } /* END OF util_reverse_words_buff */
40 
41 /* ------------------------------------------------------------
42  **
43  * @brief This function executes a reversed byte copy on a specified buffer.
44  *
45  *        on a 6 byte buffer:
46  *
47  *        buff[5] <---> buff[0]
48  *        buff[4] <---> buff[1]
49  *        buff[3] <---> buff[2]
50  *
51  * @param[in] dst_ptr - The counter buffer.
52  * @param[in] src_ptr - The counter size in bytes.
53  *
54  */
util_reverse_buff(u8 * buff_ptr,u32 size)55 void util_reverse_buff(u8 *buff_ptr, u32 size)
56 {
57 	u32 i;
58 	u32 temp;
59 	u8 *high_swap_ptr, *low_swap_ptr;
60 
61 	/* initialize the source and the destination poision */
62 	high_swap_ptr = buff_ptr + size - 1;
63 	low_swap_ptr  = buff_ptr;
64 
65 	/* execute the reverse memcpoy */
66 	for (i = 0; i < (size / 2); i++) {
67 		temp			= *high_swap_ptr;
68 		*(high_swap_ptr--)	= *low_swap_ptr;
69 		*(low_swap_ptr++)	= temp;
70 	}
71 } /* END OF util_reverse_buff */
72 
73 /* ------------------------------------------------------------
74  **
75  * @brief This function executes a reverse bytes copying from one buffer to
76  * another buffer.
77  *
78  * @param[in] dst_ptr - The pointer to destination buffer.
79  * @param[in] src_ptr - The pointer to source buffer.
80  * @param[in] size    - The size in bytes.
81  *
82  */
util_reverse_memcpy(u8 * dst_ptr,const u8 * src_ptr,u32 size)83 void util_reverse_memcpy(u8 *dst_ptr, const u8 *src_ptr, u32 size)
84 {
85 	u32 i;
86 	u32 buff_dst_pos, buff_src_pos;
87 
88 	/* execute the reverse copy in case of different buffers */
89 	/* initialize the source and the destination position */
90 	buff_dst_pos = size - 1;
91 	buff_src_pos = 0;
92 
93 	for (i = 0; i < size; i++)
94 		dst_ptr[buff_dst_pos--] = src_ptr[buff_src_pos++];
95 } /* END OF util_reverse_memcpy */
96 
97 /* ------------------------------------------------------------
98  **
99  * @brief This function executes a memory copy between 2 buffers.
100  *
101  * @param[in] dst_ptr - The first counter buffer.
102  * @param[in] src_ptr - The second counter buffer.
103  * @param[in] size    - the first counter size in words.
104  *
105  */
util_word_memcpy(u32 * dst_ptr,u32 * src_ptr,u32 size)106 void util_word_memcpy(u32 *dst_ptr, u32 *src_ptr, u32 size)
107 {
108 	u32 i;
109 
110 	/* execute the reverse memcpoy */
111 	for (i = 0; i < size; i++)
112 		dst_ptr[i] = src_ptr[i];
113 } /* END OF util_memcpy */
114 
115 /* ------------------------------------------------------------
116  **
117  * @brief This function executes a reverse bytes copying from one buffer
118  * to another buffer.
119  *
120  * @param[in] dst_ptr - The pointer to destination buffer.
121  * @param[in] src_ptr - The pointer to source buffer.
122  * @param[in] size    - The size in words.
123  *
124  */
util_reverse_word_memcpy(u32 * dst_ptr,u32 * src_ptr,u32 size)125 void util_reverse_word_memcpy(u32 *dst_ptr, u32 *src_ptr, u32 size)
126 {
127 	u32 i;
128 	u32 buff_dst_pos, buff_src_pos;
129 
130 	/* execute the reverse copy in case of different buffers */
131 	/* initialize the source and the destination position */
132 	buff_dst_pos = size - 1;
133 	buff_src_pos = 0;
134 
135 	for (i = 0; i < size; i++)
136 		dst_ptr[buff_dst_pos--] = src_ptr[buff_src_pos++];
137 } /* END OF util_reverse_memcpy */
138 
139 /* ------------------------------------------------------------
140  **
141  * @brief This function executes a memory set operation on a buffer.
142  *
143  * @param[in] buff_ptr - the buffer.
144  * @param[in] val      - The value to set the buffer.
145  * @param[in] size     - the buffers size in words.
146  *
147  */
util_word_memset(u32 * buff_ptr,u32 val,u32 size)148 void util_word_memset(u32 *buff_ptr, u32 val, u32 size)
149 {
150 	u32 i;
151 
152 	/* execute the reverse memcpoy */
153 	for (i = 0; i < size; i++)
154 		buff_ptr[i] = val;
155 } /* END OF util_memcpy */
156