• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  *
4  * This program is free software; you can redistribute  it and/or modify it
5  * under  the terms of  the GNU General  Public License as published by the
6  * Free Software Foundation;  either version 2 of the  License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18 
19 #include "drv_ioctl_otp.h"
20 #include "drv_osal_lib.h"
21 
hi_otp_print_arr_u8(const hi_char * name,const hi_u8 * msg,hi_u32 msg_len)22 hi_void hi_otp_print_arr_u8(const hi_char *name, const hi_u8 *msg, hi_u32 msg_len)
23 {
24 #if HI_OTP_TEST
25     hi_u32 i;
26 
27     if (name != HI_NULL) {
28         HI_PRINT("%s: \n", name);
29     }
30 
31     for (i = 0; i < msg_len; i++) {
32         if (i != 0 && align_16_bytes(i)) {
33             HI_PRINT("\n");
34         }
35         HI_PRINT("%02X ", msg[i]);
36     }
37     HI_PRINT("\n");
38 #endif
39 }
40 
hi_otp_print_arr_u32(const hi_char * name,const hi_u32 * msg,hi_u32 msg_len)41 hi_void hi_otp_print_arr_u32(const hi_char *name, const hi_u32 *msg, hi_u32 msg_len)
42 {
43 #if HI_OTP_TEST
44     hi_u32 i;
45 
46     if (name != HI_NULL) {
47         HI_PRINT("%s: \n", name);
48     }
49 
50     for (i = 0; i < msg_len; i++) {
51         if (i != 0 && align_16_bytes(i)) {
52             HI_PRINT("\n");
53         }
54         HI_PRINT("%08X ", msg[i]);
55     }
56     HI_PRINT("\n");
57 #endif
58 }
59 
hi_otp_word_big_endian(const hi_u32 word_val,hi_u8 * byte_buf,hi_u32 len)60 hi_s32 hi_otp_word_big_endian(const hi_u32 word_val, hi_u8 *byte_buf, hi_u32 len)
61 {
62     hi_otp_check_formula_fail(len != WORD_BYTE_WIDTH, HI_ERR_OTP_INVALID_PARAM);
63 
64     byte_buf[WORD_IDX_0] = (word_val >> OFFSET_3_BYTE) & 0xff;
65     byte_buf[WORD_IDX_1] = (word_val >> OFFSET_2_BYTE) & 0xff;
66     byte_buf[WORD_IDX_2] = (word_val >> OFFSET_1_BYTE) & 0xff;
67     byte_buf[WORD_IDX_3] = (word_val >> OFFSET_0_BYTE) & 0xff;
68 
69     return HI_SUCCESS;
70 }
71 
hi_otp_crc16_modbus(const hi_u8 * msg,hi_u32 msg_len)72 hi_u16 hi_otp_crc16_modbus(const hi_u8 *msg, hi_u32 msg_len)
73 {
74     hi_u32 i;
75     hi_u16 crc_in = 0xFFFF;
76     hi_u16 crc_poly = 0x8005;
77     hi_u8 crc_char = 0;
78 
79     while (msg_len--) {
80         crc_char = *(msg++);
81         crc_in ^= (crc_char << OFFSET_1_BYTE);
82         for (i = 0; i < BYTE_BIT_WIDTH; i++) {
83             if (crc_in & 0x8000) {
84                 crc_in = (crc_in << 1) ^ crc_poly;
85             } else {
86                 crc_in = crc_in << 1;
87             }
88         }
89     }
90 
91     return (crc_in) ;
92 }
93 
otp_memset(void * src,hi_u32 len1,int num,hi_u32 len2)94 hi_s32 otp_memset(void *src, hi_u32 len1, int num, hi_u32 len2)
95 {
96     if (len1 < len2) {
97         hi_otp_err("len1 < len2!!!\n");
98         return HI_ERR_OTP_INVALID_PARAM;
99     }
100     memset(src, num, len2);
101     return EOK;
102 }
103 
otp_memcpy(void * dst,hi_u32 len1,const void * src,hi_u32 len2)104 hi_s32 otp_memcpy(void *dst, hi_u32 len1, const void *src, hi_u32 len2)
105 {
106     if (len1 < len2) {
107         hi_otp_err("len1 < len2!!!\n");
108         return HI_ERR_OTP_INVALID_PARAM;
109     }
110     memcpy(dst, src, len2);
111     return EOK;
112 }
113 
114 
115 
116