1 /*
2  * Copyright (c) 2021-2022 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include "hpm_common.h"
9 #include "hpm_crc_drv.h"
10 
crc_get_default_channel_config(crc_channel_config_t * cfg)11 void crc_get_default_channel_config(crc_channel_config_t *cfg)
12 {
13     cfg->preset = crc_preset_crc32;
14     cfg->poly = 0x4C11DB7ul;
15     cfg->init = 0xFFFFFFFul;
16     cfg->in_byte_order = crc_in_byte_order_lsb;
17     cfg->refout = crc_refout_true;
18     cfg->refin = crc_refin_true;
19     cfg->poly_width = CRC_POLY_WIDTH_32;
20     cfg->xorout = 0xFFFFFFF;
21 }
22 
crc_setup_channel_config(CRC_Type * ptr,uint32_t ch_index,crc_channel_config_t * cfg)23 hpm_stat_t crc_setup_channel_config(CRC_Type *ptr, uint32_t ch_index,
24                             crc_channel_config_t *cfg)
25 {
26     ptr->CHN[ch_index].PRE_SET = cfg->preset;
27 
28     if (!ptr->CHN[ch_index].PRE_SET) {
29         ptr->CHN[ch_index].POLY = cfg->poly;
30         ptr->CHN[ch_index].INIT_DATA = cfg->init;
31         ptr->CHN[ch_index].XOROUT = cfg->xorout;
32         ptr->CHN[ch_index].MISC_SETTING = CRC_CHN_MISC_SETTING_REV_OUT_SET(cfg->refout) |
33                                         CRC_CHN_MISC_SETTING_REV_IN_SET(cfg->refin) |
34                                         CRC_CHN_MISC_SETTING_POLY_WIDTH_SET(cfg->poly_width);
35     }
36 
37     ptr->CHN[ch_index].MISC_SETTING |= CRC_CHN_MISC_SETTING_BYTE_REV_SET(cfg->in_byte_order);
38     return status_success;
39 }
40 
crc_calc_block_bytes(CRC_Type * ptr,uint32_t ch_index,uint8_t * pbuffer,uint32_t length)41 void crc_calc_block_bytes(CRC_Type *ptr, uint32_t ch_index, uint8_t *pbuffer, uint32_t length)
42 {
43     uint32_t addr = (uint32_t)&ptr->CHN[ch_index].DATA;
44 
45     for (uint32_t i = 0; i < length; i++) {
46         CRC_REG_WRITE8(addr, pbuffer[i]);
47     }
48 }
49 
crc_calc_block_half_words(CRC_Type * ptr,uint32_t ch_index,uint16_t * pbuffer,uint32_t length)50 void crc_calc_block_half_words(CRC_Type *ptr, uint32_t ch_index, uint16_t *pbuffer, uint32_t length)
51 {
52     uint32_t addr = (uint32_t)&ptr->CHN[ch_index].DATA;
53 
54     for (uint32_t i = 0; i < length; i++) {
55         CRC_REG_WRITE16(addr, pbuffer[i]);
56     }
57 }
58 
crc_calc_block_words(CRC_Type * ptr,uint32_t ch_index,uint32_t * pbuffer,uint32_t length)59 void crc_calc_block_words(CRC_Type *ptr, uint32_t ch_index, uint32_t *pbuffer, uint32_t length)
60 {
61     uint32_t addr = (uint32_t)&ptr->CHN[ch_index].DATA;
62 
63     for (uint32_t i = 0; i < length; i++) {
64         CRC_REG_WRITE32(addr, pbuffer[i]);
65     }
66 }
67 
crc_calc_large_block_fast(CRC_Type * ptr,uint32_t ch_index,uint8_t * pbuffer,uint32_t length)68 void crc_calc_large_block_fast(CRC_Type *ptr, uint32_t ch_index, uint8_t *pbuffer, uint32_t length)
69 {
70     if (length < 16) {
71         crc_calc_block_bytes(ptr, ch_index, pbuffer, length);
72         return;
73     }
74 
75     uint32_t addr = (uint32_t)&ptr->CHN[ch_index].DATA;
76     uint32_t start_byte_addr = (uint32_t)pbuffer;
77     uint32_t start_byte_len = 4ul - (start_byte_addr & 0x03ul);
78     uint32_t word_addr = start_byte_addr + start_byte_len;
79     uint32_t word_len = (length - start_byte_len) & (~0x03ul);
80     uint32_t end_byte_addr = word_addr + word_len;
81     uint32_t end_byte_len = (length - start_byte_len - word_len);
82 
83     for (uint32_t i = 0; i < start_byte_len; i++) {
84         CRC_REG_WRITE8(addr, *(volatile uint8_t *)(start_byte_addr + i));
85     }
86 
87     for (uint32_t i = 0; i < word_len; i += 4) {
88         CRC_REG_WRITE32(addr, *(volatile uint32_t *)(word_addr + i));
89     }
90 
91     for (uint32_t i = 0; i < end_byte_len; i++) {
92         CRC_REG_WRITE8(addr, *(volatile uint8_t *)(end_byte_addr + i));
93     }
94 }
95 
96