diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..c4b33bc --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,123 @@ +name: Build CRB + +on: + pull_request: + branches: + - develop + - master + - main + - dev + +env: + PULL_REQUEST_ID: ${{ github.event.number }} + CI_ROOT_PATH: /data/github-actions + TFTP_ROOT_PATH: /data/tftp_server + NFS_ROOT_PATH: /data/nfs_server + +defaults: + run: + shell: bash + +jobs: + build_image: + name: Build + runs-on: [self-hosted, k510_buildroot] + timeout-minutes: 60 + container: + image: ai.b-bug.org:5000/k510_sdk_github + env: + CONF: k510_${{ matrix.cfg }}_defconfig + ENV: development + volumes: + - /data:/data + strategy: + matrix: + cfg: ["crb_lp3_v0_1", "crb_lp3_v1_2"] + steps: + - uses: actions/checkout@v3 + with: + clean: false + + - name: Install toolchain + run: | + set -x + test -f toolchain/nds64le-linux-glibc-v5d.txz || wget -c https://ai.b-bug.org/k510_buildroot/toolchain/nds64le-linux-glibc-v5d.txz -O toolchain/nds64le-linux-glibc-v5d.txz + test -f toolchain/nds64le-elf-mculib-v5d.txz || wget -c https://ai.b-bug.org/k510_buildroot/toolchain/nds64le-elf-mculib-v5d.txz -O toolchain/nds64le-elf-mculib-v5d.txz + - name: Download package + run: | + set -x + test -f pkg-download/dl.tar.gz && rm pkg-download/dl.tar.gz + wget -qc https://ai.b-bug.org/k510_buildroot/pkg-download/dl.tar.gz -O pkg-download/dl.tar.gz + echo "dl.tar.gz file md5 check" + md5sum pkg-download/dl.tar.gz + test -d dl_bak && rm -rf dl_bak + test -d dl && mv dl dl_bak + tar -xvf pkg-download/dl.tar.gz + test -d dl/ai/ || mkdir -p dl/ai/ + wget -qc https://ai.b-bug.org/k510_buildroot/pkg-download/ai_kmodel_data.tar.xz -O dl/ai/ai_kmodel_data.tar.xz + echo "ai_kmodel_data.tar.xz file md5 check" + md5sum dl/ai/ai_kmodel_data.tar.xz + test -d dl/riscv-pk-k510 && (test -d dl/riscv-pk-k510 || mv dl_bak/riscv-pk-k510 dl/) + test -d dl_bak && rm -rf dl_bak + + - name: Build image + run: | + sed -i "s/BR2_JLEVEL=5/BR2_JLEVEL=14/g" configs/$CONF + rm -rf $CONF + make CONF=$CONF + + - name: Save image to CI root + run: | + set -x + export HW=`echo ${{ matrix.cfg }}|awk -F '_' '{print $1"_"$3"_"$4}'` + export SAVE_DIR=$PULL_REQUEST_ID"_"$GITHUB_RUN_ID + echo $HW + echo $SAVE_DIR + sudo mkdir -p $CI_ROOT_PATH/k510_buildroot/$HW/$SAVE_DIR/ + sudo cp -r $CONF/images/* $CI_ROOT_PATH/k510_buildroot/$HW/$SAVE_DIR/ + + - name: Save image to TFTP + run: | + set -x + export FILE_SUFFIX=`echo ${{ matrix.cfg }}|awk -F '_' '{print $1"-"$3"_"$4}'`-$PULL_REQUEST_ID-$GITHUB_RUN_ID + echo $FILE_SUFFIX + sudo cp $CONF/images/bootm-bbl.img $TFTP_ROOT_PATH/bootm-bbl-$FILE_SUFFIX.img + sudo cp $CONF/images/k510.dtb $TFTP_ROOT_PATH/k510-$FILE_SUFFIX.dtb + + - name: Save image to NFS + run: | + set -x + export HW=`echo ${{ matrix.cfg }}|awk -F '_' '{print $1"_"$3"_"$4}'` + export SAVE_DIR=$PULL_REQUEST_ID"_"$GITHUB_RUN_ID + echo $HW + echo $SAVE_DIR + sudo mkdir -p $NFS_ROOT_PATH/k510_buildroot/$HW/$SAVE_DIR/ + sudo tar -xf $CONF/images/rootfs.tar -C $NFS_ROOT_PATH/k510_buildroot/$HW/$PULL_REQUEST_ID"_"$GITHUB_RUN_ID + + load_image: + name: Load + needs: build_image + timeout-minutes: 10 + runs-on: [self-hosted, k510_buildroot] + container: + image: ai.b-bug.org:5000/k510_sdk_github + volumes: + - /data:/data + strategy: + matrix: + cfg: ["crb_lp3_v0_1", "crb_lp3_v1_2"] + steps: + - name: Load image + run: | + set -x + test -d bin && rm -rf bin + mkdir ./bin + wget -qc https://ai.b-bug.org/k510_buildroot/github/ci -O ./bin/ci + chmod +x ./bin/ci + export HW_MODEL=`echo ${{ matrix.cfg }}|awk -F '_' '{print $1}'` + export HW_VER=`echo ${{ matrix.cfg }}|awk -F '_' '{print $3"_"$4}'` + echo $HW_MODEL + echo $HW_VER + echo $PULL_REQUEST_ID + echo $GITHUB_RUN_ID + ./bin/ci --project k510_buildroot --merge_id $PULL_REQUEST_ID --pipeline_id $GITHUB_RUN_ID --hw_ver $HW_VER --hw_model $HW_MODEL diff --git a/Makefile b/Makefile old mode 100644 new mode 100755 index 8fb95c0..51ee018 --- a/Makefile +++ b/Makefile @@ -107,6 +107,19 @@ dl: wget -c https://github.com/kendryte/k510_buildroot/releases/download/v1.5/dl.tar.gz -O pkg-download/dl.tar.gz; tar -zxf pkg-download/dl.tar.gz; mkdir -p dl/ai/; wget -c https://github.com/kendryte/k510_buildroot/releases/download/v1.5/ai_kmodel_data.tar.xz -O dl/ai/ai_kmodel_data.tar.xz; + (set -x; NNCASE_VER=v1.6.0 ;\ + NNCASE_SITE=https://github.com/kendryte/nncase/releases/download;\ + NNCASE_PIP_VD=1.6.0.20220505 ;\ + mkdir -p dl/nncase_linux_runtime; cd dl/nncase_linux_runtime; \ + wget -c $${NNCASE_SITE}/$${NNCASE_VER}/nncase-k510-1.6.0.tgz ; \ + wget -c $${NNCASE_SITE}/$${NNCASE_VER}/nncase-$${NNCASE_PIP_VD}-cp310-cp310-manylinux_2_24_x86_64.whl; \ + wget -c $${NNCASE_SITE}/$${NNCASE_VER}/nncase-$${NNCASE_PIP_VD}-cp36-cp36m-manylinux_2_24_x86_64.whl;\ + wget -c $${NNCASE_SITE}/$${NNCASE_VER}/nncase-$${NNCASE_PIP_VD}-cp37-cp37m-manylinux_2_24_x86_64.whl;\ + wget -c $${NNCASE_SITE}/$${NNCASE_VER}/nncase-$${NNCASE_PIP_VD}-cp38-cp38-manylinux_2_24_x86_64.whl;\ + wget -c $${NNCASE_SITE}/$${NNCASE_VER}/nncase-$${NNCASE_PIP_VD}-cp39-cp39-manylinux_2_24_x86_64.whl; \ + ) + + down_toolchain: diff --git a/package/ai/code/openpose/pafprocess.cc b/package/ai/code/openpose/pafprocess.cc index 1514186..1994df7 100644 --- a/package/ai/code/openpose/pafprocess.cc +++ b/package/ai/code/openpose/pafprocess.cc @@ -1,27 +1,3 @@ -/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ #include #include #include diff --git a/package/k510_evb_test/src/bsp/controler/aes.c b/package/k510_evb_test/src/bsp/controler/aes.c index d04bd2a..f556048 100644 --- a/package/k510_evb_test/src/bsp/controler/aes.c +++ b/package/k510_evb_test/src/bsp/controler/aes.c @@ -1,755 +1,779 @@ -#include "platform.h" -#include "aes.h" - -volatile aes_t *const aes = (volatile aes_t *)AES_BASE_ADDR; - -static void aes_clk_init() -{ - // sysctl_clock_enable(SYSCTL_CLOCK_AES); - // sysctl_reset(SYSCTL_RESET_AES); -} - -static void aes_write_aad(uint32_t aad_data) -{ - aes->aes_aad_data = aad_data; -} - -static void aes_write_text(uint32_t text_data) -{ - aes->aes_text_data = text_data; -} - -static void gcm_write_tag(uint32_t *tag) -{ - aes->gcm_in_tag[0] = tag[3]; - aes->gcm_in_tag[1] = tag[2]; - aes->gcm_in_tag[2] = tag[1]; - aes->gcm_in_tag[3] = tag[0]; -} - -static uint32_t aes_get_data_in_flag(void) -{ - return aes->data_in_flag; -} - -static uint32_t aes_get_data_out_flag(void) -{ - return aes->data_out_flag; -} - -static uint32_t gcm_get_tag_in_flag(void) -{ - return aes->tag_in_flag; -} - -static uint32_t aes_read_out_data(void) -{ - return aes->aes_out_data; -} - -static uint32_t gcm_get_tag_chk(void) -{ - return aes->tag_chk; -} - -static void gcm_clear_chk_tag(void) -{ - aes->tag_clear = 0; -} - -static uint32_t gcm_check_tag(uint32_t *gcm_tag) -{ - while(!gcm_get_tag_in_flag()) - ; - gcm_write_tag(gcm_tag); - while(!gcm_get_tag_chk()) - ; - if(gcm_get_tag_chk() == 0x2) - { - gcm_clear_chk_tag(); - return 1; - } else - { - gcm_clear_chk_tag(); - return 0; - } -} - -void aes_input_bytes(const uint8_t *input_data, size_t input_data_len, aes_cipher_mode_t cipher_mode) -{ - size_t padding_len, uint32_num, uint8_num, remainder, i; - uint32_t uint32_data; - uint8_t uint8_data[4] = {0}; - - padding_len = ((input_data_len + 15) / 16) * 16; - uint32_num = input_data_len / 4; - for(i = 0; i < uint32_num; i++) - { - uint32_data = *((uint32_t *)(&input_data[i * 4])); - while(!aes_get_data_in_flag()) - ; - aes_write_text(uint32_data); - } - uint8_num = 4 * uint32_num; - remainder = input_data_len % 4; - if(remainder) - { - switch(remainder) - { - case 1: - uint8_data[0] = input_data[uint8_num]; - break; - case 2: - uint8_data[0] = input_data[uint8_num]; - uint8_data[1] = input_data[uint8_num + 1]; - break; - case 3: - uint8_data[0] = input_data[uint8_num]; - uint8_data[1] = input_data[uint8_num + 1]; - uint8_data[2] = input_data[uint8_num + 2]; - break; - default: - break; - } - uint32_data = *((uint32_t *)(&uint8_data[0])); - while(!aes_get_data_in_flag()) - ; - aes_write_text(uint32_data); - } - if((cipher_mode == AES_ECB) || (cipher_mode == AES_CBC)) - { - uint32_num = (padding_len - input_data_len) / 4; - for(i = 0; i < uint32_num; i++) - { - while(!aes_get_data_in_flag()) - ; - aes_write_text(0); - } - uint32_num = padding_len / 4; - } -} - -static void aes_process_with_dma(dma_ch_t channel, uint8_t* input_data, uint32_t input_len, uint8_t* output_data, uint32_t output_len, aes_cipher_mode_t cipher_mode) -{ - aes->dma_sel = 1; - peri_dma_cfg_t cfg = { - .channel = channel, - .dev_sel = AES_RX, - .ch_priority = 1, - .mode = LINE_MODE, - .src_type = FIXED_REG_ADDRESS, - .buswidth = FOUR_BYTE, - .dev_burst_len = 0, - .line_size = output_len, - .saddr = (void*)(&aes->aes_out_data), - .daddr = (void*)output_data, - .dev_timeout = 0xFFF - }; - peri_dma_config(&cfg); - peri_dma_start(channel); - aes_input_bytes(input_data, input_len, cipher_mode); - peri_dma_wait_done_or_timeout(channel); - aes->dma_sel = 0; -} - -static void process_less_80_bytes(uint8_t *input_data, uint8_t *output_data, size_t input_data_len, aes_cipher_mode_t cipher_mode) -{ - size_t padding_len, uint32_num, uint8_num, remainder, i; - uint32_t uint32_data; - uint8_t uint8_data[4] = {0}; - - padding_len = ((input_data_len + 15) / 16) * 16; - uint32_num = input_data_len / 4; - for(i = 0; i < uint32_num; i++) - { - uint32_data = *((uint32_t *)(&input_data[i * 4])); - while(!aes_get_data_in_flag()) - ; - aes_write_text(uint32_data); - } - uint8_num = 4 * uint32_num; - remainder = input_data_len % 4; - if(remainder) - { - switch(remainder) - { - case 1: - uint8_data[0] = input_data[uint8_num]; - break; - case 2: - uint8_data[0] = input_data[uint8_num]; - uint8_data[1] = input_data[uint8_num + 1]; - break; - case 3: - uint8_data[0] = input_data[uint8_num]; - uint8_data[1] = input_data[uint8_num + 1]; - uint8_data[2] = input_data[uint8_num + 2]; - break; - default: - break; - } - uint32_data = *((uint32_t *)(&uint8_data[0])); - while(!aes_get_data_in_flag()) - ; - aes_write_text(uint32_data); - } - if((cipher_mode == AES_ECB) || (cipher_mode == AES_CBC)) - { - uint32_num = (padding_len - input_data_len) / 4; - for(i = 0; i < uint32_num; i++) - { - while(!aes_get_data_in_flag()) - ; - aes_write_text(0); - } - uint32_num = padding_len / 4; - } - for(i = 0; i < uint32_num; i++) - { - while(!aes_get_data_out_flag()) - ; - *((uint32_t *)(&output_data[i * 4])) = aes_read_out_data(); - } - if((cipher_mode == AES_GCM) && (remainder)) - { - while(!aes_get_data_out_flag()) - ; - *((uint32_t *)(&uint8_data[0])) = aes_read_out_data(); - switch(remainder) - { - case 1: - output_data[uint32_num * 4] = uint8_data[0]; - break; - case 2: - output_data[uint32_num * 4] = uint8_data[0]; - output_data[(i * 4) + 1] = uint8_data[1]; - break; - case 3: - output_data[uint32_num * 4] = uint8_data[0]; - output_data[(i * 4) + 1] = uint8_data[1]; - output_data[(i * 4) + 2] = uint8_data[2]; - break; - default: - break; - } - } -} - -void gcm_get_tag(uint8_t *gcm_tag) -{ - uint32_t uint32_tag; - uint8_t i = 0; - - uint32_tag = aes->gcm_out_tag[3]; - gcm_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff); - gcm_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff); - gcm_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff); - gcm_tag[i++] = (uint8_t)((uint32_tag)&0xff); - - uint32_tag = aes->gcm_out_tag[2]; - gcm_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff); - gcm_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff); - gcm_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff); - gcm_tag[i++] = (uint8_t)((uint32_tag)&0xff); - - uint32_tag = aes->gcm_out_tag[1]; - gcm_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff); - gcm_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff); - gcm_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff); - gcm_tag[i++] = (uint8_t)((uint32_tag)&0xff); - - uint32_tag = aes->gcm_out_tag[0]; - gcm_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff); - gcm_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff); - gcm_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff); - gcm_tag[i++] = (uint8_t)((uint32_tag)&0xff); - - gcm_check_tag((uint32_t *)gcm_tag); -} - -void aes_init(uint8_t *input_key, size_t input_key_len, uint8_t *iv, size_t iv_len, uint8_t *gcm_aad, - aes_cipher_mode_t cipher_mode, aes_encrypt_sel_t encrypt_sel, size_t gcm_aad_len, size_t input_data_len) -{ - size_t remainder, uint32_num, uint8_num, i; - uint32_t uint32_data; - uint8_t uint8_data[4] = {0}; - size_t padding_len = input_data_len; - aes_clk_init(); - if((cipher_mode == AES_ECB) || (cipher_mode == AES_CBC)) - padding_len = ((input_data_len + 15) / 16) * 16; - aes->aes_endian |= 1; - uint32_num = input_key_len / 4; - for(i = 0; i < uint32_num; i++) - { - if(i < 4) - aes->aes_key[i] = *((uint32_t *)(&input_key[input_key_len - (4 * i) - 4])); - else - aes->aes_key_ext[i - 4] = *((uint32_t *)(&input_key[input_key_len - (4 * i) - 4])); - } - - uint32_num = iv_len / 4; - for(i = 0; i < uint32_num; i++) - aes->aes_iv[i] = *((uint32_t *)(&iv[iv_len - (4 * i) - 4])); - - aes->mode_ctl.kmode = input_key_len / 8 - 2; /* b'00:AES_128 b'01:AES_192 b'10:AES_256 b'11:RESERVED */ - aes->mode_ctl.cipher_mode = cipher_mode; - aes->encrypt_sel = encrypt_sel; - aes->gb_aad_num = gcm_aad_len - 1; - aes->gb_pc_num = padding_len - 1; - aes->gb_aes_en |= 1; - - if(cipher_mode == AES_GCM) - { - uint32_num = gcm_aad_len / 4; - for(i = 0; i < uint32_num; i++) - { - uint32_data = *((uint32_t *)(&gcm_aad[i * 4])); - while(!aes_get_data_in_flag()) - ; - aes_write_aad(uint32_data); - } - uint8_num = 4 * uint32_num; - remainder = gcm_aad_len % 4; - if(remainder) - { - switch(remainder) - { - case 1: - uint8_data[0] = gcm_aad[uint8_num]; - break; - case 2: - uint8_data[0] = gcm_aad[uint8_num]; - uint8_data[1] = gcm_aad[uint8_num + 1]; - break; - case 3: - uint8_data[0] = gcm_aad[uint8_num]; - uint8_data[1] = gcm_aad[uint8_num + 1]; - uint8_data[2] = gcm_aad[uint8_num + 2]; - break; - default: - break; - } - uint32_data = *((uint32_t *)(&uint8_data[0])); - while(!aes_get_data_in_flag()) - ; - aes_write_aad(uint32_data); - } - } -} - -void aes_process(uint8_t *input_data, uint8_t *output_data, size_t input_data_len, aes_cipher_mode_t cipher_mode) -{ - size_t temp_len = 0; - uint32_t i = 0; - - if(input_data_len >= 80) - { - for(i = 0; i < (input_data_len / 80); i++) - process_less_80_bytes(&input_data[i * 80], &output_data[i * 80], 80, cipher_mode); - } - temp_len = input_data_len % 80; - if(temp_len) - process_less_80_bytes(&input_data[i * 80], &output_data[i * 80], temp_len, cipher_mode); -} - -void aes_ecb128_hard_decrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(input_key, AES_128, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len); - aes_process(input_data, output_data, padding_len, AES_ECB); -} - -void aes_ecb128_hard_encrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(input_key, AES_128, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process(input_data, output_data, input_len, AES_ECB); -} - -void aes_ecb192_hard_decrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(input_key, AES_192, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len); - aes_process(input_data, output_data, padding_len, AES_ECB); -} - -void aes_ecb192_hard_encrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(input_key, AES_192, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process(input_data, output_data, input_len, AES_ECB); -} - -void aes_ecb256_hard_decrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(input_key, AES_256, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len); - aes_process(input_data, output_data, padding_len, AES_ECB); -} - -void aes_ecb256_hard_encrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(input_key, AES_256, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process(input_data, output_data, input_len, AES_ECB); -} - -void aes_cbc128_hard_decrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_DECRYPTION, 0L, input_len); - aes_process(input_data, output_data, padding_len, AES_CBC); -} - -void aes_cbc128_hard_encrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process(input_data, output_data, input_len, AES_CBC); -} - -void aes_cbc192_hard_decrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_DECRYPTION, 0L, input_len); - aes_process(input_data, output_data, padding_len, AES_CBC); -} - -void aes_cbc192_hard_encrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process(input_data, output_data, input_len, AES_CBC); -} - -void aes_cbc256_hard_decrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_DECRYPTION, 0L, input_len); - aes_process(input_data, output_data, padding_len, AES_CBC); -} - -void aes_cbc256_hard_encrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process(input_data, output_data, input_len, AES_CBC); -} - -void aes_gcm128_hard_decrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag) -{ - aes_init(context->input_key, AES_128, context->iv, IV_LEN_96, context->gcm_aad, - AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len); - aes_process(input_data, output_data, input_len, AES_GCM); - gcm_get_tag(gcm_tag); -} - -void aes_gcm128_hard_encrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag) -{ - aes_init(context->input_key, AES_128, context->iv, IV_LEN_96, context->gcm_aad, - AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len); - aes_process(input_data, output_data, input_len, AES_GCM); - gcm_get_tag(gcm_tag); -} - -void aes_gcm192_hard_decrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag) -{ - aes_init(context->input_key, AES_192, context->iv, IV_LEN_96, context->gcm_aad, - AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len); - aes_process(input_data, output_data, input_len, AES_GCM); - gcm_get_tag(gcm_tag); -} - -void aes_gcm192_hard_encrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag) -{ - aes_init(context->input_key, AES_192, context->iv, IV_LEN_96, context->gcm_aad, - AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len); - aes_process(input_data, output_data, input_len, AES_GCM); - gcm_get_tag(gcm_tag); -} - -void aes_gcm256_hard_decrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag) -{ - aes_init(context->input_key, AES_256, context->iv, IV_LEN_96, context->gcm_aad, - AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len); - aes_process(input_data, output_data, input_len, AES_GCM); - gcm_get_tag(gcm_tag); -} - -void aes_gcm256_hard_encrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag) -{ - aes_init(context->input_key, AES_256, context->iv, IV_LEN_96, context->gcm_aad, - AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len); - aes_process(input_data, output_data, input_len, AES_GCM); - gcm_get_tag(gcm_tag); -} - -void aes_cfb128_hard_decrypt(cfb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_DECRYPTION, 0, input_len); - aes_process(input_data, output_data, input_len, AES_CFB); -} - -void aes_cfb128_hard_encrypt(cfb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_ENCRYPTION, 0, input_len); - aes_process(input_data, output_data, input_len, AES_CFB); -} - -void aes_cfb192_hard_decrypt(cfb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_DECRYPTION, 0, input_len); - aes_process(input_data, output_data, input_len, AES_CFB); -} - -void aes_cfb192_hard_encrypt(cfb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_ENCRYPTION, 0, input_len); - aes_process(input_data, output_data, input_len, AES_CFB); -} - -void aes_cfb256_hard_decrypt(cfb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_DECRYPTION, 0, input_len); - aes_process(input_data, output_data, input_len, AES_CFB); -} - -void aes_cfb256_hard_encrypt(cfb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_ENCRYPTION, 0, input_len); - aes_process(input_data, output_data, input_len, AES_CFB); -} - -void aes_ofb128_hard_decrypt(ofb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_DECRYPTION, 0, input_len); - aes_process(input_data, output_data, input_len, AES_OFB); -} - -void aes_ofb128_hard_encrypt(ofb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_ENCRYPTION, 0, input_len); - aes_process(input_data, output_data, input_len, AES_OFB); -} - -void aes_ofb192_hard_decrypt(ofb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_DECRYPTION, 0, input_len); - aes_process(input_data, output_data, input_len, AES_OFB); -} - -void aes_ofb192_hard_encrypt(ofb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_ENCRYPTION, 0, input_len); - aes_process(input_data, output_data, input_len, AES_OFB); -} - -void aes_ofb256_hard_decrypt(ofb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_DECRYPTION, 0, input_len); - aes_process(input_data, output_data, input_len, AES_OFB); -} - -void aes_ofb256_hard_encrypt(ofb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) -{ - aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_ENCRYPTION, 0, input_len); - aes_process(input_data, output_data, input_len, AES_OFB); -} - -void aes_ecb128_hard_decrypt_dma(dma_ch_t channel, uint8_t* input_key, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(input_key, AES_128, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_ECB); -} - -void aes_ecb128_hard_encrypt_dma(dma_ch_t channel, uint8_t* input_key, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(input_key, AES_128, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_ECB); -} - -void aes_ecb192_hard_decrypt_dma(dma_ch_t channel, uint8_t* input_key, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(input_key, AES_192, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_ECB); -} - -void aes_ecb192_hard_encrypt_dma(dma_ch_t channel, uint8_t* input_key, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(input_key, AES_192, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_ECB); -} - -void aes_ecb256_hard_decrypt_dma(dma_ch_t channel, uint8_t* input_key, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(input_key, AES_256, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_ECB); -} - -void aes_ecb256_hard_encrypt_dma(dma_ch_t channel, uint8_t* input_key, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(input_key, AES_256, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_ECB); -} - -void aes_cbc128_hard_decrypt_dma(dma_ch_t channel, cbc_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_DECRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_CBC); -} - -void aes_cbc128_hard_encrypt_dma(dma_ch_t channel, cbc_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_CBC); -} - -void aes_cbc192_hard_decrypt_dma(dma_ch_t channel, cbc_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_DECRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_CBC); -} - -void aes_cbc192_hard_encrypt_dma(dma_ch_t channel, cbc_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_CBC); -} - -void aes_cbc256_hard_decrypt_dma(dma_ch_t channel, cbc_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_DECRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_CBC); -} - -void aes_cbc256_hard_encrypt_dma(dma_ch_t channel, cbc_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - size_t padding_len = ((input_len + 15) / 16) * 16; - aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_CBC); -} - -void aes_gcm128_hard_decrypt_dma(dma_ch_t channel, gcm_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data, uint8_t *gcm_tag) -{ - aes_init(context->input_key, AES_128, context->iv, IV_LEN_96, context->gcm_aad, - AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_GCM); - gcm_get_tag(gcm_tag); -} - -void aes_gcm128_hard_encrypt_dma(dma_ch_t channel, gcm_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data, uint8_t *gcm_tag) -{ - aes_init(context->input_key, AES_128, context->iv, IV_LEN_96, context->gcm_aad, - AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_GCM); - gcm_get_tag(gcm_tag); -} - -void aes_gcm192_hard_decrypt_dma(dma_ch_t channel, gcm_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data, uint8_t *gcm_tag) -{ - aes_init(context->input_key, AES_192, context->iv, IV_LEN_96, context->gcm_aad, - AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_GCM); - gcm_get_tag(gcm_tag); -} - -void aes_gcm192_hard_encrypt_dma(dma_ch_t channel, gcm_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data, uint8_t *gcm_tag) -{ - aes_init(context->input_key, AES_192, context->iv, IV_LEN_96, context->gcm_aad, - AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_GCM); - gcm_get_tag(gcm_tag); -} - -void aes_gcm256_hard_decrypt_dma(dma_ch_t channel, gcm_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data, uint8_t *gcm_tag) -{ - aes_init(context->input_key, AES_256, context->iv, IV_LEN_96, context->gcm_aad, - AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_GCM); - gcm_get_tag(gcm_tag); -} - -void aes_gcm256_hard_encrypt_dma(dma_ch_t channel, gcm_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data, uint8_t *gcm_tag) -{ - aes_init(context->input_key, AES_256, context->iv, IV_LEN_96, context->gcm_aad, - AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_GCM); - gcm_get_tag(gcm_tag); -} - -void aes_cfb128_hard_decrypt_dma(dma_ch_t channel, cfb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_DECRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_CFB); -} - -void aes_cfb128_hard_encrypt_dma(dma_ch_t channel, cfb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_CFB); -} - -void aes_cfb192_hard_decrypt_dma(dma_ch_t channel, cfb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_DECRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_CFB); -} - -void aes_cfb192_hard_encrypt_dma(dma_ch_t channel, cfb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_CFB); -} - -void aes_cfb256_hard_decrypt_dma(dma_ch_t channel, cfb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_DECRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_CFB); -} - -void aes_cfb256_hard_encrypt_dma(dma_ch_t channel, cfb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_CFB); -} - -void aes_ofb128_hard_decrypt_dma(dma_ch_t channel, ofb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_DECRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_OFB); -} - -void aes_ofb128_hard_encrypt_dma(dma_ch_t channel, ofb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_OFB); -} - -void aes_ofb192_hard_decrypt_dma(dma_ch_t channel, ofb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_DECRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_OFB); -} - -void aes_ofb192_hard_encrypt_dma(dma_ch_t channel, ofb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_OFB); -} - -void aes_ofb256_hard_decrypt_dma(dma_ch_t channel, ofb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_DECRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_OFB); -} - -void aes_ofb256_hard_encrypt_dma(dma_ch_t channel, ofb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) -{ - aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_ENCRYPTION, 0L, input_len); - aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_OFB); +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "platform.h" +#include "aes.h" + +volatile aes_t *const aes = (volatile aes_t *)AES_BASE_ADDR; + +static void aes_clk_init() +{ + // sysctl_clock_enable(SYSCTL_CLOCK_AES); + // sysctl_reset(SYSCTL_RESET_AES); +} + +static void aes_write_aad(uint32_t aad_data) +{ + aes->aes_aad_data = aad_data; +} + +static void aes_write_text(uint32_t text_data) +{ + aes->aes_text_data = text_data; +} + +static void gcm_write_tag(uint32_t *tag) +{ + aes->gcm_in_tag[0] = tag[3]; + aes->gcm_in_tag[1] = tag[2]; + aes->gcm_in_tag[2] = tag[1]; + aes->gcm_in_tag[3] = tag[0]; +} + +static uint32_t aes_get_data_in_flag(void) +{ + return aes->data_in_flag; +} + +static uint32_t aes_get_data_out_flag(void) +{ + return aes->data_out_flag; +} + +static uint32_t gcm_get_tag_in_flag(void) +{ + return aes->tag_in_flag; +} + +static uint32_t aes_read_out_data(void) +{ + return aes->aes_out_data; +} + +static uint32_t gcm_get_tag_chk(void) +{ + return aes->tag_chk; +} + +static void gcm_clear_chk_tag(void) +{ + aes->tag_clear = 0; +} + +static uint32_t gcm_check_tag(uint32_t *gcm_tag) +{ + while(!gcm_get_tag_in_flag()) + ; + gcm_write_tag(gcm_tag); + while(!gcm_get_tag_chk()) + ; + if(gcm_get_tag_chk() == 0x2) + { + gcm_clear_chk_tag(); + return 1; + } else + { + gcm_clear_chk_tag(); + return 0; + } +} + +void aes_input_bytes(const uint8_t *input_data, size_t input_data_len, aes_cipher_mode_t cipher_mode) +{ + size_t padding_len, uint32_num, uint8_num, remainder, i; + uint32_t uint32_data; + uint8_t uint8_data[4] = {0}; + + padding_len = ((input_data_len + 15) / 16) * 16; + uint32_num = input_data_len / 4; + for(i = 0; i < uint32_num; i++) + { + uint32_data = *((uint32_t *)(&input_data[i * 4])); + while(!aes_get_data_in_flag()) + ; + aes_write_text(uint32_data); + } + uint8_num = 4 * uint32_num; + remainder = input_data_len % 4; + if(remainder) + { + switch(remainder) + { + case 1: + uint8_data[0] = input_data[uint8_num]; + break; + case 2: + uint8_data[0] = input_data[uint8_num]; + uint8_data[1] = input_data[uint8_num + 1]; + break; + case 3: + uint8_data[0] = input_data[uint8_num]; + uint8_data[1] = input_data[uint8_num + 1]; + uint8_data[2] = input_data[uint8_num + 2]; + break; + default: + break; + } + uint32_data = *((uint32_t *)(&uint8_data[0])); + while(!aes_get_data_in_flag()) + ; + aes_write_text(uint32_data); + } + if((cipher_mode == AES_ECB) || (cipher_mode == AES_CBC)) + { + uint32_num = (padding_len - input_data_len) / 4; + for(i = 0; i < uint32_num; i++) + { + while(!aes_get_data_in_flag()) + ; + aes_write_text(0); + } + uint32_num = padding_len / 4; + } +} + +static void aes_process_with_dma(dma_ch_t channel, uint8_t* input_data, uint32_t input_len, uint8_t* output_data, uint32_t output_len, aes_cipher_mode_t cipher_mode) +{ + aes->dma_sel = 1; + peri_dma_cfg_t cfg = { + .channel = channel, + .dev_sel = AES_RX, + .ch_priority = 1, + .mode = LINE_MODE, + .src_type = FIXED_REG_ADDRESS, + .buswidth = FOUR_BYTE, + .dev_burst_len = 0, + .line_size = output_len, + .saddr = (void*)(&aes->aes_out_data), + .daddr = (void*)output_data, + .dev_timeout = 0xFFF + }; + peri_dma_config(&cfg); + peri_dma_start(channel); + aes_input_bytes(input_data, input_len, cipher_mode); + peri_dma_wait_done_or_timeout(channel); + aes->dma_sel = 0; +} + +static void process_less_80_bytes(uint8_t *input_data, uint8_t *output_data, size_t input_data_len, aes_cipher_mode_t cipher_mode) +{ + size_t padding_len, uint32_num, uint8_num, remainder, i; + uint32_t uint32_data; + uint8_t uint8_data[4] = {0}; + + padding_len = ((input_data_len + 15) / 16) * 16; + uint32_num = input_data_len / 4; + for(i = 0; i < uint32_num; i++) + { + uint32_data = *((uint32_t *)(&input_data[i * 4])); + while(!aes_get_data_in_flag()) + ; + aes_write_text(uint32_data); + } + uint8_num = 4 * uint32_num; + remainder = input_data_len % 4; + if(remainder) + { + switch(remainder) + { + case 1: + uint8_data[0] = input_data[uint8_num]; + break; + case 2: + uint8_data[0] = input_data[uint8_num]; + uint8_data[1] = input_data[uint8_num + 1]; + break; + case 3: + uint8_data[0] = input_data[uint8_num]; + uint8_data[1] = input_data[uint8_num + 1]; + uint8_data[2] = input_data[uint8_num + 2]; + break; + default: + break; + } + uint32_data = *((uint32_t *)(&uint8_data[0])); + while(!aes_get_data_in_flag()) + ; + aes_write_text(uint32_data); + } + if((cipher_mode == AES_ECB) || (cipher_mode == AES_CBC)) + { + uint32_num = (padding_len - input_data_len) / 4; + for(i = 0; i < uint32_num; i++) + { + while(!aes_get_data_in_flag()) + ; + aes_write_text(0); + } + uint32_num = padding_len / 4; + } + for(i = 0; i < uint32_num; i++) + { + while(!aes_get_data_out_flag()) + ; + *((uint32_t *)(&output_data[i * 4])) = aes_read_out_data(); + } + if((cipher_mode == AES_GCM) && (remainder)) + { + while(!aes_get_data_out_flag()) + ; + *((uint32_t *)(&uint8_data[0])) = aes_read_out_data(); + switch(remainder) + { + case 1: + output_data[uint32_num * 4] = uint8_data[0]; + break; + case 2: + output_data[uint32_num * 4] = uint8_data[0]; + output_data[(i * 4) + 1] = uint8_data[1]; + break; + case 3: + output_data[uint32_num * 4] = uint8_data[0]; + output_data[(i * 4) + 1] = uint8_data[1]; + output_data[(i * 4) + 2] = uint8_data[2]; + break; + default: + break; + } + } +} + +void gcm_get_tag(uint8_t *gcm_tag) +{ + uint32_t uint32_tag; + uint8_t i = 0; + + uint32_tag = aes->gcm_out_tag[3]; + gcm_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff); + gcm_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff); + gcm_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff); + gcm_tag[i++] = (uint8_t)((uint32_tag)&0xff); + + uint32_tag = aes->gcm_out_tag[2]; + gcm_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff); + gcm_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff); + gcm_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff); + gcm_tag[i++] = (uint8_t)((uint32_tag)&0xff); + + uint32_tag = aes->gcm_out_tag[1]; + gcm_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff); + gcm_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff); + gcm_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff); + gcm_tag[i++] = (uint8_t)((uint32_tag)&0xff); + + uint32_tag = aes->gcm_out_tag[0]; + gcm_tag[i++] = (uint8_t)((uint32_tag >> 24) & 0xff); + gcm_tag[i++] = (uint8_t)((uint32_tag >> 16) & 0xff); + gcm_tag[i++] = (uint8_t)((uint32_tag >> 8) & 0xff); + gcm_tag[i++] = (uint8_t)((uint32_tag)&0xff); + + gcm_check_tag((uint32_t *)gcm_tag); +} + +void aes_init(uint8_t *input_key, size_t input_key_len, uint8_t *iv, size_t iv_len, uint8_t *gcm_aad, + aes_cipher_mode_t cipher_mode, aes_encrypt_sel_t encrypt_sel, size_t gcm_aad_len, size_t input_data_len) +{ + size_t remainder, uint32_num, uint8_num, i; + uint32_t uint32_data; + uint8_t uint8_data[4] = {0}; + size_t padding_len = input_data_len; + aes_clk_init(); + if((cipher_mode == AES_ECB) || (cipher_mode == AES_CBC)) + padding_len = ((input_data_len + 15) / 16) * 16; + aes->aes_endian |= 1; + uint32_num = input_key_len / 4; + for(i = 0; i < uint32_num; i++) + { + if(i < 4) + aes->aes_key[i] = *((uint32_t *)(&input_key[input_key_len - (4 * i) - 4])); + else + aes->aes_key_ext[i - 4] = *((uint32_t *)(&input_key[input_key_len - (4 * i) - 4])); + } + + uint32_num = iv_len / 4; + for(i = 0; i < uint32_num; i++) + aes->aes_iv[i] = *((uint32_t *)(&iv[iv_len - (4 * i) - 4])); + + aes->mode_ctl.kmode = input_key_len / 8 - 2; /* b'00:AES_128 b'01:AES_192 b'10:AES_256 b'11:RESERVED */ + aes->mode_ctl.cipher_mode = cipher_mode; + aes->encrypt_sel = encrypt_sel; + aes->gb_aad_num = gcm_aad_len - 1; + aes->gb_pc_num = padding_len - 1; + aes->gb_aes_en |= 1; + + if(cipher_mode == AES_GCM) + { + uint32_num = gcm_aad_len / 4; + for(i = 0; i < uint32_num; i++) + { + uint32_data = *((uint32_t *)(&gcm_aad[i * 4])); + while(!aes_get_data_in_flag()) + ; + aes_write_aad(uint32_data); + } + uint8_num = 4 * uint32_num; + remainder = gcm_aad_len % 4; + if(remainder) + { + switch(remainder) + { + case 1: + uint8_data[0] = gcm_aad[uint8_num]; + break; + case 2: + uint8_data[0] = gcm_aad[uint8_num]; + uint8_data[1] = gcm_aad[uint8_num + 1]; + break; + case 3: + uint8_data[0] = gcm_aad[uint8_num]; + uint8_data[1] = gcm_aad[uint8_num + 1]; + uint8_data[2] = gcm_aad[uint8_num + 2]; + break; + default: + break; + } + uint32_data = *((uint32_t *)(&uint8_data[0])); + while(!aes_get_data_in_flag()) + ; + aes_write_aad(uint32_data); + } + } +} + +void aes_process(uint8_t *input_data, uint8_t *output_data, size_t input_data_len, aes_cipher_mode_t cipher_mode) +{ + size_t temp_len = 0; + uint32_t i = 0; + + if(input_data_len >= 80) + { + for(i = 0; i < (input_data_len / 80); i++) + process_less_80_bytes(&input_data[i * 80], &output_data[i * 80], 80, cipher_mode); + } + temp_len = input_data_len % 80; + if(temp_len) + process_less_80_bytes(&input_data[i * 80], &output_data[i * 80], temp_len, cipher_mode); +} + +void aes_ecb128_hard_decrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(input_key, AES_128, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len); + aes_process(input_data, output_data, padding_len, AES_ECB); +} + +void aes_ecb128_hard_encrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(input_key, AES_128, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process(input_data, output_data, input_len, AES_ECB); +} + +void aes_ecb192_hard_decrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(input_key, AES_192, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len); + aes_process(input_data, output_data, padding_len, AES_ECB); +} + +void aes_ecb192_hard_encrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(input_key, AES_192, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process(input_data, output_data, input_len, AES_ECB); +} + +void aes_ecb256_hard_decrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(input_key, AES_256, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len); + aes_process(input_data, output_data, padding_len, AES_ECB); +} + +void aes_ecb256_hard_encrypt(uint8_t *input_key, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(input_key, AES_256, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process(input_data, output_data, input_len, AES_ECB); +} + +void aes_cbc128_hard_decrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_DECRYPTION, 0L, input_len); + aes_process(input_data, output_data, padding_len, AES_CBC); +} + +void aes_cbc128_hard_encrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process(input_data, output_data, input_len, AES_CBC); +} + +void aes_cbc192_hard_decrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_DECRYPTION, 0L, input_len); + aes_process(input_data, output_data, padding_len, AES_CBC); +} + +void aes_cbc192_hard_encrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process(input_data, output_data, input_len, AES_CBC); +} + +void aes_cbc256_hard_decrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_DECRYPTION, 0L, input_len); + aes_process(input_data, output_data, padding_len, AES_CBC); +} + +void aes_cbc256_hard_encrypt(cbc_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process(input_data, output_data, input_len, AES_CBC); +} + +void aes_gcm128_hard_decrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag) +{ + aes_init(context->input_key, AES_128, context->iv, IV_LEN_96, context->gcm_aad, + AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len); + aes_process(input_data, output_data, input_len, AES_GCM); + gcm_get_tag(gcm_tag); +} + +void aes_gcm128_hard_encrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag) +{ + aes_init(context->input_key, AES_128, context->iv, IV_LEN_96, context->gcm_aad, + AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len); + aes_process(input_data, output_data, input_len, AES_GCM); + gcm_get_tag(gcm_tag); +} + +void aes_gcm192_hard_decrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag) +{ + aes_init(context->input_key, AES_192, context->iv, IV_LEN_96, context->gcm_aad, + AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len); + aes_process(input_data, output_data, input_len, AES_GCM); + gcm_get_tag(gcm_tag); +} + +void aes_gcm192_hard_encrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag) +{ + aes_init(context->input_key, AES_192, context->iv, IV_LEN_96, context->gcm_aad, + AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len); + aes_process(input_data, output_data, input_len, AES_GCM); + gcm_get_tag(gcm_tag); +} + +void aes_gcm256_hard_decrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag) +{ + aes_init(context->input_key, AES_256, context->iv, IV_LEN_96, context->gcm_aad, + AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len); + aes_process(input_data, output_data, input_len, AES_GCM); + gcm_get_tag(gcm_tag); +} + +void aes_gcm256_hard_encrypt(gcm_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data, uint8_t *gcm_tag) +{ + aes_init(context->input_key, AES_256, context->iv, IV_LEN_96, context->gcm_aad, + AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len); + aes_process(input_data, output_data, input_len, AES_GCM); + gcm_get_tag(gcm_tag); +} + +void aes_cfb128_hard_decrypt(cfb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_DECRYPTION, 0, input_len); + aes_process(input_data, output_data, input_len, AES_CFB); +} + +void aes_cfb128_hard_encrypt(cfb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_ENCRYPTION, 0, input_len); + aes_process(input_data, output_data, input_len, AES_CFB); +} + +void aes_cfb192_hard_decrypt(cfb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_DECRYPTION, 0, input_len); + aes_process(input_data, output_data, input_len, AES_CFB); +} + +void aes_cfb192_hard_encrypt(cfb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_ENCRYPTION, 0, input_len); + aes_process(input_data, output_data, input_len, AES_CFB); +} + +void aes_cfb256_hard_decrypt(cfb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_DECRYPTION, 0, input_len); + aes_process(input_data, output_data, input_len, AES_CFB); +} + +void aes_cfb256_hard_encrypt(cfb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_ENCRYPTION, 0, input_len); + aes_process(input_data, output_data, input_len, AES_CFB); +} + +void aes_ofb128_hard_decrypt(ofb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_DECRYPTION, 0, input_len); + aes_process(input_data, output_data, input_len, AES_OFB); +} + +void aes_ofb128_hard_encrypt(ofb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_ENCRYPTION, 0, input_len); + aes_process(input_data, output_data, input_len, AES_OFB); +} + +void aes_ofb192_hard_decrypt(ofb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_DECRYPTION, 0, input_len); + aes_process(input_data, output_data, input_len, AES_OFB); +} + +void aes_ofb192_hard_encrypt(ofb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_ENCRYPTION, 0, input_len); + aes_process(input_data, output_data, input_len, AES_OFB); +} + +void aes_ofb256_hard_decrypt(ofb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_DECRYPTION, 0, input_len); + aes_process(input_data, output_data, input_len, AES_OFB); +} + +void aes_ofb256_hard_encrypt(ofb_context_t *context, uint8_t *input_data, size_t input_len, uint8_t *output_data) +{ + aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_ENCRYPTION, 0, input_len); + aes_process(input_data, output_data, input_len, AES_OFB); +} + +void aes_ecb128_hard_decrypt_dma(dma_ch_t channel, uint8_t* input_key, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(input_key, AES_128, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_ECB); +} + +void aes_ecb128_hard_encrypt_dma(dma_ch_t channel, uint8_t* input_key, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(input_key, AES_128, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_ECB); +} + +void aes_ecb192_hard_decrypt_dma(dma_ch_t channel, uint8_t* input_key, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(input_key, AES_192, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_ECB); +} + +void aes_ecb192_hard_encrypt_dma(dma_ch_t channel, uint8_t* input_key, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(input_key, AES_192, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_ECB); +} + +void aes_ecb256_hard_decrypt_dma(dma_ch_t channel, uint8_t* input_key, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(input_key, AES_256, NULL, 0L, NULL, AES_ECB, AES_HARD_DECRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_ECB); +} + +void aes_ecb256_hard_encrypt_dma(dma_ch_t channel, uint8_t* input_key, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(input_key, AES_256, NULL, 0L, NULL, AES_ECB, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_ECB); +} + +void aes_cbc128_hard_decrypt_dma(dma_ch_t channel, cbc_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_DECRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_CBC); +} + +void aes_cbc128_hard_encrypt_dma(dma_ch_t channel, cbc_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_CBC); +} + +void aes_cbc192_hard_decrypt_dma(dma_ch_t channel, cbc_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_DECRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_CBC); +} + +void aes_cbc192_hard_encrypt_dma(dma_ch_t channel, cbc_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_CBC); +} + +void aes_cbc256_hard_decrypt_dma(dma_ch_t channel, cbc_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_DECRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_CBC); +} + +void aes_cbc256_hard_encrypt_dma(dma_ch_t channel, cbc_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + size_t padding_len = ((input_len + 15) / 16) * 16; + aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CBC, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, padding_len, AES_CBC); +} + +void aes_gcm128_hard_decrypt_dma(dma_ch_t channel, gcm_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data, uint8_t *gcm_tag) +{ + aes_init(context->input_key, AES_128, context->iv, IV_LEN_96, context->gcm_aad, + AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_GCM); + gcm_get_tag(gcm_tag); +} + +void aes_gcm128_hard_encrypt_dma(dma_ch_t channel, gcm_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data, uint8_t *gcm_tag) +{ + aes_init(context->input_key, AES_128, context->iv, IV_LEN_96, context->gcm_aad, + AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_GCM); + gcm_get_tag(gcm_tag); +} + +void aes_gcm192_hard_decrypt_dma(dma_ch_t channel, gcm_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data, uint8_t *gcm_tag) +{ + aes_init(context->input_key, AES_192, context->iv, IV_LEN_96, context->gcm_aad, + AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_GCM); + gcm_get_tag(gcm_tag); +} + +void aes_gcm192_hard_encrypt_dma(dma_ch_t channel, gcm_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data, uint8_t *gcm_tag) +{ + aes_init(context->input_key, AES_192, context->iv, IV_LEN_96, context->gcm_aad, + AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_GCM); + gcm_get_tag(gcm_tag); +} + +void aes_gcm256_hard_decrypt_dma(dma_ch_t channel, gcm_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data, uint8_t *gcm_tag) +{ + aes_init(context->input_key, AES_256, context->iv, IV_LEN_96, context->gcm_aad, + AES_GCM, AES_HARD_DECRYPTION, context->gcm_aad_len, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_GCM); + gcm_get_tag(gcm_tag); +} + +void aes_gcm256_hard_encrypt_dma(dma_ch_t channel, gcm_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data, uint8_t *gcm_tag) +{ + aes_init(context->input_key, AES_256, context->iv, IV_LEN_96, context->gcm_aad, + AES_GCM, AES_HARD_ENCRYPTION, context->gcm_aad_len, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_GCM); + gcm_get_tag(gcm_tag); +} + +void aes_cfb128_hard_decrypt_dma(dma_ch_t channel, cfb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_DECRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_CFB); +} + +void aes_cfb128_hard_encrypt_dma(dma_ch_t channel, cfb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_CFB); +} + +void aes_cfb192_hard_decrypt_dma(dma_ch_t channel, cfb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_DECRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_CFB); +} + +void aes_cfb192_hard_encrypt_dma(dma_ch_t channel, cfb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_CFB); +} + +void aes_cfb256_hard_decrypt_dma(dma_ch_t channel, cfb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_DECRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_CFB); +} + +void aes_cfb256_hard_encrypt_dma(dma_ch_t channel, cfb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_CFB, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_CFB); +} + +void aes_ofb128_hard_decrypt_dma(dma_ch_t channel, ofb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_DECRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_OFB); +} + +void aes_ofb128_hard_encrypt_dma(dma_ch_t channel, ofb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + aes_init(context->input_key, AES_128, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_OFB); +} + +void aes_ofb192_hard_decrypt_dma(dma_ch_t channel, ofb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_DECRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_OFB); +} + +void aes_ofb192_hard_encrypt_dma(dma_ch_t channel, ofb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + aes_init(context->input_key, AES_192, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_OFB); +} + +void aes_ofb256_hard_decrypt_dma(dma_ch_t channel, ofb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_DECRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_OFB); +} + +void aes_ofb256_hard_encrypt_dma(dma_ch_t channel, ofb_context_t* context, uint8_t* input_data, size_t input_len, uint8_t* output_data) +{ + aes_init(context->input_key, AES_256, context->iv, IV_LEN_128, NULL, AES_OFB, AES_HARD_ENCRYPTION, 0L, input_len); + aes_process_with_dma(channel, input_data, input_len, output_data, input_len, AES_OFB); } \ No newline at end of file diff --git a/package/k510_evb_test/src/bsp/controler/audio.c b/package/k510_evb_test/src/bsp/controler/audio.c index 6a74eff..cbfe62a 100644 --- a/package/k510_evb_test/src/bsp/controler/audio.c +++ b/package/k510_evb_test/src/bsp/controler/audio.c @@ -1,208 +1,232 @@ -#include "audio.h" - -volatile audio_out_reg_s *audio_out_reg = (volatile audio_out_reg_s *)(long)(AUDIO_BASE_ADDR+0xc00); -volatile audio_in_reg_s *audio_in_reg = (volatile audio_in_reg_s *)(long)(AUDIO_BASE_ADDR+0x400); - -void audio_i2s_in_init() -{ - audio_in_reg->audio_in_agc_para_4.agc_bypass = AUDIO_ENABLE; -} - -void audio_pdm_in_init(uint32_t pdm_rx_num, - audio_in_pdm_edge_e pdm_rx_edge, - audio_in_align_e pdm_rx_align, - uint32_t pdm_rx_tx_sample_freq) -{ - /* pmd in init - note: andio in 没有DMA使能寄存器,直接用CPU或者dma收å‘å³å¯ - dmaé—¨é™è®¾ç½®æˆ1 - */ - audio_in_reg->audio_in_agc_para_4.agc_bypass = AUDIO_ENABLE; - audio_in_reg->audio_in_pdm_ch_conf.pdm_chl_num = pdm_rx_num; - audio_in_reg->audio_in_pdm_ch_conf.pdm_pn_sel = pdm_rx_edge; - - k510_logci("audio_in_reg->audio_in_pdm_ch_conf.pdm_chl_num = %d\r\n",audio_in_reg->audio_in_pdm_ch_conf.pdm_chl_num); - k510_logci("audio_in_reg->audio_in_pdm_ch_conf.pdm_pn_sel = %d\r\n",audio_in_reg->audio_in_pdm_ch_conf.pdm_pn_sel); - - if(16000 == pdm_rx_tx_sample_freq) - { - audio_in_reg->audio_in_pdm_conf_1.ord_select = 1; - - audio_in_reg->audio_in_pdm_clk_spike.pdmin_clk_spike_cnt = 0; - audio_in_reg->audio_in_pdm_clk_spike.pdmin_clk_spike_th = 0; - - audio_in_reg->audio_in_pdm_ch_spike.pdmin_ch0_spike_cnt = 0; - audio_in_reg->audio_in_pdm_ch_spike.pdmin_ch0_spike_th = 0; - } - else if(44100 == pdm_rx_tx_sample_freq) //44.1K - { - audio_in_reg->audio_in_pdm_conf_1.ord_select = 0; - audio_in_reg->audio_in_pdm_clk_spike.pdmin_clk_spike_cnt = 0; - audio_in_reg->audio_in_pdm_clk_spike.pdmin_clk_spike_th = 0; - - audio_in_reg->audio_in_pdm_ch_spike.pdmin_ch0_spike_cnt = 0; - audio_in_reg->audio_in_pdm_ch_spike.pdmin_ch0_spike_th = 0; - } - else - { - k510_logci("audio_pdm_in_init pdm_rx_tx_sample_freq error!\r\n"); - while(1); - } - - audio_in_reg->audio_in_pdm_conf_0.hbf_enable = 1; - audio_in_reg->audio_in_pdm_conf_0.mpf_enable = 1; - audio_in_reg->audio_in_pdm_conf_0.pdm_in_endian = pdm_rx_align; - audio_in_reg->audio_in_pdm_conf_0.pdm_fifo_clear = 0; - audio_in_reg->audio_in_pdm_conf_0.pdm_dma_fifo_th = 1; - - audio_in_reg->audio_in_pdm_conf_0.pdm_enable = 1; -} - -void audio_tdm_in_init(uint32_t tdm_io_num, uint32_t tdm_rx_num, audio_in_align_e tdm_rx_align) -{ - audio_in_reg->audio_in_tdm_ctl.tdm_enable = AUDIO_DISABLE; - /*bypass agc*/ - audio_in_reg->audio_in_agc_para_4.agc_bypass = AUDIO_ENABLE; - - audio_in_reg->audio_in_tdm_config.tdm_width_mode = 1; /*24 bit*/ - audio_in_reg->audio_in_tdm_config.tdm_clk_dly = 1; /* 麦克风在frame_sync下一æ‹å‡ºæ•°æ® */ //0 - audio_in_reg->audio_in_tdm_config.tdm_in_endian = tdm_rx_align; /* left align */ - audio_in_reg->audio_in_tdm_config.tdm_dma_fifo_th = 1; - - audio_in_reg->audio_in_tdm_channel_cfg0.io0_channel_num = 0; - audio_in_reg->audio_in_tdm_channel_cfg0.io1_channel_num = 0; - audio_in_reg->audio_in_tdm_channel_cfg0.io2_channel_num = 0; - audio_in_reg->audio_in_tdm_channel_cfg0.io3_channel_num = 0; - - audio_in_reg->audio_in_tdm_channel_cfg0.io4_channel_num = 0; - audio_in_reg->audio_in_tdm_channel_cfg0.io5_channel_num = 0; - audio_in_reg->audio_in_tdm_channel_cfg0.io6_channel_num = 0; - audio_in_reg->audio_in_tdm_channel_cfg0.io7_channel_num = 0; - - audio_in_reg->audio_in_tdm_channel_cfg1.io8_channel_num = 0; - audio_in_reg->audio_in_tdm_channel_cfg1.io9_channel_num = 0; - audio_in_reg->audio_in_tdm_channel_cfg1.io10_channel_num = 0; - audio_in_reg->audio_in_tdm_channel_cfg1.io11_channel_num = 0; - audio_in_reg->audio_in_tdm_channel_cfg1.io12_channel_num = 0; - audio_in_reg->audio_in_tdm_channel_cfg1.io13_channel_num = 0; - audio_in_reg->audio_in_tdm_channel_cfg1.io14_channel_num = 0; - audio_in_reg->audio_in_tdm_channel_cfg1.io15_channel_num = 0; - - audio_in_reg->audio_in_tdm_fsync_config.tdm_fsync_div_high = 1; - audio_in_reg->audio_in_tdm_ctl.tdm_intr = 0; - - if((tdm_io_num == 1) && (tdm_rx_num == 2))/*1 io 2 channel*/ - { - audio_in_reg->audio_in_tdm_config.tdm_channel_mode = 7; /*ionum l*/ - audio_in_reg->audio_in_tdm_channel_cfg0.io0_channel_num = 2; - audio_in_reg->audio_in_tdm_fsync_config.tdm_fsync_div_low = 63;/* 2 channels 64bit*/ - } - else if((tdm_io_num == 2) && (tdm_rx_num == 4)) /*2 io * 4 channel = 8 channel*/ - { - audio_in_reg->audio_in_tdm_config.tdm_channel_mode = 6; /*ionum 2*/ - audio_in_reg->audio_in_tdm_channel_cfg0.io0_channel_num = 4; - audio_in_reg->audio_in_tdm_channel_cfg0.io1_channel_num = 4; - audio_in_reg->audio_in_tdm_fsync_config.tdm_fsync_div_low = 63+64;/* 4 channels 128 bit*/ - } - else - { - k510_logci("please check io and ch config!\r\n"); - while(1); - } - - audio_in_reg->audio_in_tdm_ctl.tdm_enable = AUDIO_ENABLE; -} - -void audio_i2s_out_init(uint32_t word_len) -{ - audio_out_reg->audio_out_ctl.data_type = (word_len == 24) ? AUDIO_OUT_TYPE_24BIT:AUDIO_OUT_TYPE_32BIT; - audio_out_reg->audio_out_ctl.mode = AUDIO_OUT_MODE_I2S; /* i2s/pdm/tdm mode */ - audio_out_reg->audio_out_ctl.enable = AUDIO_ENABLE; /* enable audio out */ -} - -void audio_pdm_out_init(bool dma_enable,uint32_t ch_number,audio_out_pdm_edge_e edge, audio_sample_e sample) -{ - audio_out_reg->audio_out_ctl.enable = AUDIO_DISABLE; /* disable audio out */ - audio_out_reg->audio_out_ctl.data_type = AUDIO_OUT_TYPE_24BIT; - audio_out_reg->audio_out_ctl.loop_en = AUDIO_DISABLE; /* disable loopback */ - - if(true == dma_enable) - { - k510_logci("pdm enable dma!\r\n"); - audio_out_reg->audio_out_ctl.dma_enable = AUDIO_ENABLE; /* enable dma */ - } - else - { - k510_logci("pdm disable dma!\r\n"); - audio_out_reg->audio_out_ctl.dma_enable = AUDIO_DISABLE; /* disable dma */ - } - audio_out_reg->audio_out_ctl.dma_threshold = 1; - audio_out_reg->audio_out_ctl.channel_num = ch_number; /* channel */ - audio_out_reg->audio_out_ctl.mode = AUDIO_OUT_MODE_PDM; /* i2s/pdm/tdm mode */ - audio_out_reg->audio_out_ctl.fir2_out_bypass = AUDIO_ENABLE; - - audio_out_reg->audio_out_fifo.ref_fifo_th = 0; - audio_out_reg->audio_out_fifo.audio_fifo_clear = AUDIO_DISABLE; - - audio_out_reg->audio_out_pdm_conf.pcm_sync_en = 1; - audio_out_reg->audio_out_pdm_conf.pdm_spike_th = 0x0; - - audio_out_reg->audio_out_filter.loop_fir_bypass = AUDIO_DISABLE; - audio_out_reg->audio_out_filter.pdm_cic_filter_bypass = AUDIO_DISABLE; - audio_out_reg->audio_out_filter.pdm_cic_mp_filter_bypass = AUDIO_DISABLE; - audio_out_reg->audio_out_filter.pdm_hbf_filter_bypass = AUDIO_DISABLE; - - audio_out_reg->audio_out_pdm_conf.pdm_pn_sel = edge; - - if(sample == AUDIO_SAMPLE_16K) - { - audio_out_reg->audio_out_pdm_conf.pdm_ord_sel = AUDIO_PDM_OUT_CIC32; /*cic 32 2.048MHz*/ - } - else - { - audio_out_reg->audio_out_pdm_conf.pdm_ord_sel = AUDIO_PDM_OUT_CIC16; /*cic 16 2.8224MHz*/ - } - /* enable pdm */ - - audio_out_reg->pn_delay = 9; - k510_logci("audio pdm out pn delay = %d\r\n",audio_out_reg->pn_delay); - audio_out_reg->audio_out_ctl.enable = AUDIO_ENABLE; /* enable audio out */ -} - -void audio_tdm_out_init(bool tx_dma_enable) -{ - //output - audio_out_reg->audio_out_ctl.enable = AUDIO_DISABLE; /* disable audio out */ - audio_out_reg->audio_out_ctl.align = 1; /* right align */ - audio_out_reg->audio_out_ctl.data_type = 0; /* 32 bit */ - audio_out_reg->audio_out_ctl.loop_en = AUDIO_DISABLE; /* disable loopback */ - - if(true == tx_dma_enable) - { - k510_logci("pcm enable dma!\r\n"); - audio_out_reg->audio_out_ctl.dma_enable = AUDIO_ENABLE; /* enable dma */ - } - else - { - k510_logci("pcm disable dma!\r\n"); - audio_out_reg->audio_out_ctl.dma_enable = AUDIO_DISABLE; /* disable dma */ - } - - audio_out_reg->audio_out_ctl.dma_threshold = 1; - audio_out_reg->audio_out_ctl.channel_num = 2; /* channel 2 */ - audio_out_reg->audio_out_ctl.mode = AUDIO_OUT_MODE_TDM; /* i2s/pdm/tdm mode */ - audio_out_reg->audio_out_ctl.fir2_out_bypass = AUDIO_ENABLE; - - audio_out_reg->audio_out_fifo.ref_fifo_th = 0; - audio_out_reg->audio_out_fifo.audio_fifo_clear = AUDIO_DISABLE; - - audio_out_reg->audio_out_pdm_conf.pcm_sync_en = 1; - audio_out_reg->audio_out_pdm_conf.pdm_spike_th = 0x0; - - audio_out_reg->audio_out_tdm_conf.frame_sync_total = 64; /* 64 cycles 2ch *32 bit = 64 64*/ - audio_out_reg->audio_out_tdm_conf.frame_sync_high = 32; /* frame sync 1 sclk 32*/ - - audio_out_reg->audio_out_tdm_conf.frame_sclk_delay = 0; /* frame sync delay 1 sclk */ - - audio_out_reg->audio_out_ctl.enable = AUDIO_ENABLE; /* enable audio out */ -} +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "audio.h" + +volatile audio_out_reg_s *audio_out_reg = (volatile audio_out_reg_s *)(long)(AUDIO_BASE_ADDR+0xc00); +volatile audio_in_reg_s *audio_in_reg = (volatile audio_in_reg_s *)(long)(AUDIO_BASE_ADDR+0x400); + +void audio_i2s_in_init() +{ + audio_in_reg->audio_in_agc_para_4.agc_bypass = AUDIO_ENABLE; +} + +void audio_pdm_in_init(uint32_t pdm_rx_num, + audio_in_pdm_edge_e pdm_rx_edge, + audio_in_align_e pdm_rx_align, + uint32_t pdm_rx_tx_sample_freq) +{ + /* pmd in init + note: andio in 没有DMA使能寄存器,直接用CPU或者dma收å‘å³å¯ + dmaé—¨é™è®¾ç½®æˆ1 + */ + audio_in_reg->audio_in_agc_para_4.agc_bypass = AUDIO_ENABLE; + audio_in_reg->audio_in_pdm_ch_conf.pdm_chl_num = pdm_rx_num; + audio_in_reg->audio_in_pdm_ch_conf.pdm_pn_sel = pdm_rx_edge; + + k510_logci("audio_in_reg->audio_in_pdm_ch_conf.pdm_chl_num = %d\r\n",audio_in_reg->audio_in_pdm_ch_conf.pdm_chl_num); + k510_logci("audio_in_reg->audio_in_pdm_ch_conf.pdm_pn_sel = %d\r\n",audio_in_reg->audio_in_pdm_ch_conf.pdm_pn_sel); + + if(16000 == pdm_rx_tx_sample_freq) + { + audio_in_reg->audio_in_pdm_conf_1.ord_select = 1; + + audio_in_reg->audio_in_pdm_clk_spike.pdmin_clk_spike_cnt = 0; + audio_in_reg->audio_in_pdm_clk_spike.pdmin_clk_spike_th = 0; + + audio_in_reg->audio_in_pdm_ch_spike.pdmin_ch0_spike_cnt = 0; + audio_in_reg->audio_in_pdm_ch_spike.pdmin_ch0_spike_th = 0; + } + else if(44100 == pdm_rx_tx_sample_freq) //44.1K + { + audio_in_reg->audio_in_pdm_conf_1.ord_select = 0; + audio_in_reg->audio_in_pdm_clk_spike.pdmin_clk_spike_cnt = 0; + audio_in_reg->audio_in_pdm_clk_spike.pdmin_clk_spike_th = 0; + + audio_in_reg->audio_in_pdm_ch_spike.pdmin_ch0_spike_cnt = 0; + audio_in_reg->audio_in_pdm_ch_spike.pdmin_ch0_spike_th = 0; + } + else + { + k510_logci("audio_pdm_in_init pdm_rx_tx_sample_freq error!\r\n"); + while(1); + } + + audio_in_reg->audio_in_pdm_conf_0.hbf_enable = 1; + audio_in_reg->audio_in_pdm_conf_0.mpf_enable = 1; + audio_in_reg->audio_in_pdm_conf_0.pdm_in_endian = pdm_rx_align; + audio_in_reg->audio_in_pdm_conf_0.pdm_fifo_clear = 0; + audio_in_reg->audio_in_pdm_conf_0.pdm_dma_fifo_th = 1; + + audio_in_reg->audio_in_pdm_conf_0.pdm_enable = 1; +} + +void audio_tdm_in_init(uint32_t tdm_io_num, uint32_t tdm_rx_num, audio_in_align_e tdm_rx_align) +{ + audio_in_reg->audio_in_tdm_ctl.tdm_enable = AUDIO_DISABLE; + /*bypass agc*/ + audio_in_reg->audio_in_agc_para_4.agc_bypass = AUDIO_ENABLE; + + audio_in_reg->audio_in_tdm_config.tdm_width_mode = 1; /*24 bit*/ + audio_in_reg->audio_in_tdm_config.tdm_clk_dly = 1; /* 麦克风在frame_sync下一æ‹å‡ºæ•°æ® */ //0 + audio_in_reg->audio_in_tdm_config.tdm_in_endian = tdm_rx_align; /* left align */ + audio_in_reg->audio_in_tdm_config.tdm_dma_fifo_th = 1; + + audio_in_reg->audio_in_tdm_channel_cfg0.io0_channel_num = 0; + audio_in_reg->audio_in_tdm_channel_cfg0.io1_channel_num = 0; + audio_in_reg->audio_in_tdm_channel_cfg0.io2_channel_num = 0; + audio_in_reg->audio_in_tdm_channel_cfg0.io3_channel_num = 0; + + audio_in_reg->audio_in_tdm_channel_cfg0.io4_channel_num = 0; + audio_in_reg->audio_in_tdm_channel_cfg0.io5_channel_num = 0; + audio_in_reg->audio_in_tdm_channel_cfg0.io6_channel_num = 0; + audio_in_reg->audio_in_tdm_channel_cfg0.io7_channel_num = 0; + + audio_in_reg->audio_in_tdm_channel_cfg1.io8_channel_num = 0; + audio_in_reg->audio_in_tdm_channel_cfg1.io9_channel_num = 0; + audio_in_reg->audio_in_tdm_channel_cfg1.io10_channel_num = 0; + audio_in_reg->audio_in_tdm_channel_cfg1.io11_channel_num = 0; + audio_in_reg->audio_in_tdm_channel_cfg1.io12_channel_num = 0; + audio_in_reg->audio_in_tdm_channel_cfg1.io13_channel_num = 0; + audio_in_reg->audio_in_tdm_channel_cfg1.io14_channel_num = 0; + audio_in_reg->audio_in_tdm_channel_cfg1.io15_channel_num = 0; + + audio_in_reg->audio_in_tdm_fsync_config.tdm_fsync_div_high = 1; + audio_in_reg->audio_in_tdm_ctl.tdm_intr = 0; + + if((tdm_io_num == 1) && (tdm_rx_num == 2))/*1 io 2 channel*/ + { + audio_in_reg->audio_in_tdm_config.tdm_channel_mode = 7; /*ionum l*/ + audio_in_reg->audio_in_tdm_channel_cfg0.io0_channel_num = 2; + audio_in_reg->audio_in_tdm_fsync_config.tdm_fsync_div_low = 63;/* 2 channels 64bit*/ + } + else if((tdm_io_num == 2) && (tdm_rx_num == 4)) /*2 io * 4 channel = 8 channel*/ + { + audio_in_reg->audio_in_tdm_config.tdm_channel_mode = 6; /*ionum 2*/ + audio_in_reg->audio_in_tdm_channel_cfg0.io0_channel_num = 4; + audio_in_reg->audio_in_tdm_channel_cfg0.io1_channel_num = 4; + audio_in_reg->audio_in_tdm_fsync_config.tdm_fsync_div_low = 63+64;/* 4 channels 128 bit*/ + } + else + { + k510_logci("please check io and ch config!\r\n"); + while(1); + } + + audio_in_reg->audio_in_tdm_ctl.tdm_enable = AUDIO_ENABLE; +} + +void audio_i2s_out_init(uint32_t word_len) +{ + audio_out_reg->audio_out_ctl.data_type = (word_len == 24) ? AUDIO_OUT_TYPE_24BIT:AUDIO_OUT_TYPE_32BIT; + audio_out_reg->audio_out_ctl.mode = AUDIO_OUT_MODE_I2S; /* i2s/pdm/tdm mode */ + audio_out_reg->audio_out_ctl.enable = AUDIO_ENABLE; /* enable audio out */ +} + +void audio_pdm_out_init(bool dma_enable,uint32_t ch_number,audio_out_pdm_edge_e edge, audio_sample_e sample) +{ + audio_out_reg->audio_out_ctl.enable = AUDIO_DISABLE; /* disable audio out */ + audio_out_reg->audio_out_ctl.data_type = AUDIO_OUT_TYPE_24BIT; + audio_out_reg->audio_out_ctl.loop_en = AUDIO_DISABLE; /* disable loopback */ + + if(true == dma_enable) + { + k510_logci("pdm enable dma!\r\n"); + audio_out_reg->audio_out_ctl.dma_enable = AUDIO_ENABLE; /* enable dma */ + } + else + { + k510_logci("pdm disable dma!\r\n"); + audio_out_reg->audio_out_ctl.dma_enable = AUDIO_DISABLE; /* disable dma */ + } + audio_out_reg->audio_out_ctl.dma_threshold = 1; + audio_out_reg->audio_out_ctl.channel_num = ch_number; /* channel */ + audio_out_reg->audio_out_ctl.mode = AUDIO_OUT_MODE_PDM; /* i2s/pdm/tdm mode */ + audio_out_reg->audio_out_ctl.fir2_out_bypass = AUDIO_ENABLE; + + audio_out_reg->audio_out_fifo.ref_fifo_th = 0; + audio_out_reg->audio_out_fifo.audio_fifo_clear = AUDIO_DISABLE; + + audio_out_reg->audio_out_pdm_conf.pcm_sync_en = 1; + audio_out_reg->audio_out_pdm_conf.pdm_spike_th = 0x0; + + audio_out_reg->audio_out_filter.loop_fir_bypass = AUDIO_DISABLE; + audio_out_reg->audio_out_filter.pdm_cic_filter_bypass = AUDIO_DISABLE; + audio_out_reg->audio_out_filter.pdm_cic_mp_filter_bypass = AUDIO_DISABLE; + audio_out_reg->audio_out_filter.pdm_hbf_filter_bypass = AUDIO_DISABLE; + + audio_out_reg->audio_out_pdm_conf.pdm_pn_sel = edge; + + if(sample == AUDIO_SAMPLE_16K) + { + audio_out_reg->audio_out_pdm_conf.pdm_ord_sel = AUDIO_PDM_OUT_CIC32; /*cic 32 2.048MHz*/ + } + else + { + audio_out_reg->audio_out_pdm_conf.pdm_ord_sel = AUDIO_PDM_OUT_CIC16; /*cic 16 2.8224MHz*/ + } + /* enable pdm */ + + audio_out_reg->pn_delay = 9; + k510_logci("audio pdm out pn delay = %d\r\n",audio_out_reg->pn_delay); + audio_out_reg->audio_out_ctl.enable = AUDIO_ENABLE; /* enable audio out */ +} + +void audio_tdm_out_init(bool tx_dma_enable) +{ + //output + audio_out_reg->audio_out_ctl.enable = AUDIO_DISABLE; /* disable audio out */ + audio_out_reg->audio_out_ctl.align = 1; /* right align */ + audio_out_reg->audio_out_ctl.data_type = 0; /* 32 bit */ + audio_out_reg->audio_out_ctl.loop_en = AUDIO_DISABLE; /* disable loopback */ + + if(true == tx_dma_enable) + { + k510_logci("pcm enable dma!\r\n"); + audio_out_reg->audio_out_ctl.dma_enable = AUDIO_ENABLE; /* enable dma */ + } + else + { + k510_logci("pcm disable dma!\r\n"); + audio_out_reg->audio_out_ctl.dma_enable = AUDIO_DISABLE; /* disable dma */ + } + + audio_out_reg->audio_out_ctl.dma_threshold = 1; + audio_out_reg->audio_out_ctl.channel_num = 2; /* channel 2 */ + audio_out_reg->audio_out_ctl.mode = AUDIO_OUT_MODE_TDM; /* i2s/pdm/tdm mode */ + audio_out_reg->audio_out_ctl.fir2_out_bypass = AUDIO_ENABLE; + + audio_out_reg->audio_out_fifo.ref_fifo_th = 0; + audio_out_reg->audio_out_fifo.audio_fifo_clear = AUDIO_DISABLE; + + audio_out_reg->audio_out_pdm_conf.pcm_sync_en = 1; + audio_out_reg->audio_out_pdm_conf.pdm_spike_th = 0x0; + + audio_out_reg->audio_out_tdm_conf.frame_sync_total = 64; /* 64 cycles 2ch *32 bit = 64 64*/ + audio_out_reg->audio_out_tdm_conf.frame_sync_high = 32; /* frame sync 1 sclk 32*/ + + audio_out_reg->audio_out_tdm_conf.frame_sclk_delay = 0; /* frame sync delay 1 sclk */ + + audio_out_reg->audio_out_ctl.enable = AUDIO_ENABLE; /* enable audio out */ +} diff --git a/package/k510_evb_test/src/bsp/controler/ddr_init.c b/package/k510_evb_test/src/bsp/controler/ddr_init.c index 2efd983..7f65b0c 100644 --- a/package/k510_evb_test/src/bsp/controler/ddr_init.c +++ b/package/k510_evb_test/src/bsp/controler/ddr_init.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include #include "lpddr3.h" diff --git a/package/k510_evb_test/src/bsp/controler/dmac.c b/package/k510_evb_test/src/bsp/controler/dmac.c index 1356739..a3a693e 100644 --- a/package/k510_evb_test/src/bsp/controler/dmac.c +++ b/package/k510_evb_test/src/bsp/controler/dmac.c @@ -1,323 +1,347 @@ -#include -#include "platform.h" -#include "dmac.h" - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wpointer-to-int-cast" -#pragma GCC diagnostic ignored "-Wint-to-pointer-cast" - -#define PERI_DEV_SEL 0x260 - -typedef struct sdma { - volatile uint32_t int_raw; - volatile uint32_t int_en; - volatile uint32_t int_state; - volatile uint32_t ch_weight; -} sdma_t; - -typedef struct sdma_ch { - volatile uint32_t ch_ctl; - volatile uint32_t ch_cfg0; - volatile uint32_t ch_cfg1; - volatile uint32_t ch_saddr; - volatile uint32_t ch_daddr; - volatile uint32_t ch_user_data; - volatile uint32_t ch_llt_cfg; - volatile uint32_t ch_state; -} sdma_ch_t; - -typedef struct llt_entry { - volatile uint32_t saddr; - volatile uint32_t daddr; - volatile uint32_t size; - volatile uint32_t cfg; -} llt_entry_t; - -typedef struct pdma { - volatile uint32_t int_raw0; - volatile uint32_t int_raw1; - volatile uint32_t int_en0; - volatile uint32_t int_en1; - volatile uint32_t int_state0; - volatile uint32_t int_state1; -} pdma_t; - -typedef struct pdma_ch { - volatile uint32_t ch_ctl; - volatile uint32_t ch_cfg0; - volatile uint32_t ch_cfg1; - volatile uint32_t ch_saddr; - volatile uint32_t ch_daddr; - volatile uint32_t ch_dev_timeout; - volatile uint32_t ch_state; -} pdma_ch_t; - -typedef struct pdma_dev_sel { - volatile uint32_t ch_dev_sel[16]; -} pdma_dev_sel_t; - -static volatile sdma_t* const sdma = (volatile sdma_t*)SDMA_BASE_ADDR; -static volatile pdma_t* const pdma = (volatile pdma_t*)PDMA_BASE_ADDR; - -static volatile sdma_ch_t* const sdma_ch[] = { - (volatile sdma_ch_t*)(SDMA_BASE_ADDR + 0x20), - (volatile sdma_ch_t*)(SDMA_BASE_ADDR + 0x40), - (volatile sdma_ch_t*)(SDMA_BASE_ADDR + 0x60), - (volatile sdma_ch_t*)(SDMA_BASE_ADDR + 0x80), -}; - -static volatile pdma_ch_t* const pdma_ch[] = { - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x20), - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x40), - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x60), - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x80), - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0xA0), - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0xC0), - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0xE0), - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x100), - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x120), - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x140), - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x160), - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x180), - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x1A0), - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x1C0), - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x1E0), - (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x200), -}; - -static volatile pdma_dev_sel_t* const pdma_ch_dev_sel = (volatile pdma_dev_sel_t*)(PDMA_BASE_ADDR + PERI_DEV_SEL); - -#define set_bits(reg, mask, offset, val) do { \ - (reg) &= ~((mask) << (offset)); \ - (reg) |= (val) << (offset); \ -} while (0) - -static void sys_dma_int_en(uint8_t ch, bool en) -{ - const uint32_t t[2][4] = { - { 0xEEE, 0xDDD, 0xBBB, 0x777 }, - { 0x111, 0x222, 0x444, 0x888 } - }; - if (en) { - sdma->int_en |= t[1][ch]; - } else { - sdma->int_en &= t[0][ch]; - } -} - -static void sys_dma_clear_int(uint8_t ch) -{ - const uint32_t t[4] = { 0x111, 0x222, 0x444, 0x888 }; - sdma->int_raw |= t[ch]; -} - -static void peri_dma_int_en(uint8_t ch, bool en) -{ - if (en) { - pdma->int_en0 |= (1 << (ch + 16) | 1 << ch); - pdma->int_en1 |= 1 << ch; - } else { - pdma->int_en0 &= ~(1 << (ch + 16) | 1 << ch); - pdma->int_en1 &= ~(1 << ch); - } -} - -void peri_dma_clear_int(uint8_t ch) -{ - pdma->int_raw0 = (1 << (ch + 16) | 1 << ch); - pdma->int_raw1 = 1 << ch; -} - -void peri_dma_clear_done(uint8_t ch) -{ - pdma->int_raw0 = (1 << ch); -} - -void peri_dma_clear_half_done(uint8_t ch) -{ - pdma->int_raw0 = (1 << (ch + 16)); -} - -void peri_dma_clear_timeout(uint8_t ch) -{ - pdma->int_raw1 = (1 << ch); -} - - -int sys_dma_config(const sys_dma_cfg_t* cfg) -{ - uint8_t ch = cfg->channel; - if (ch >= DMA_CHANNEL_4) return -1; - - while (!(sdma_ch[ch]->ch_state & 0x1)) - ; - sys_dma_int_en(ch, false); - sys_dma_clear_int(ch); - if (cfg->mode == LINE_MODE) { - set_bits(sdma_ch[ch]->ch_cfg0, 0xFFFFF, 0, cfg->cfg.line_mode_cfg->line_size); - set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 16, 0); - set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 18, cfg->cfg.line_mode_cfg->endian); - set_bits(sdma_ch[ch]->ch_cfg1, 0x1, 20, cfg->cfg.line_mode_cfg->fixed_word); - set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 24, cfg->cfg.line_mode_cfg->user_data_size); - set_bits(sdma_ch[ch]->ch_saddr, 0xFFFFFFFF, 0, (uint32_t)cfg->cfg.line_mode_cfg->saddr); - set_bits(sdma_ch[ch]->ch_daddr, 0xFFFFFFFF, 0, (uint32_t)cfg->cfg.line_mode_cfg->daddr); - set_bits(sdma_ch[ch]->ch_user_data, 0xFFFFFFFF, 0, cfg->cfg.line_mode_cfg->user_data); - } - else if (cfg->mode == RECT_MODE) { - set_bits(sdma_ch[ch]->ch_cfg0, 0xFFFFF, 0, cfg->cfg.rect_mode_cfg->line_size); - set_bits(sdma_ch[ch]->ch_cfg0, 0x7FF, 20, cfg->cfg.rect_mode_cfg->line_num); - set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 16, 1); - set_bits(sdma_ch[ch]->ch_cfg1, 0xFFFF, 0, cfg->cfg.rect_mode_cfg->line_space); - set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 18, cfg->cfg.rect_mode_cfg->endian); - set_bits(sdma_ch[ch]->ch_cfg1, 0x1, 20, cfg->cfg.rect_mode_cfg->fixed_word); - set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 24, cfg->cfg.rect_mode_cfg->user_data_size); - set_bits(sdma_ch[ch]->ch_saddr, 0xFFFFFFFF, 0, (uint32_t)cfg->cfg.rect_mode_cfg->saddr); - set_bits(sdma_ch[ch]->ch_daddr, 0xFFFFFFFF, 0, (uint32_t)cfg->cfg.rect_mode_cfg->daddr); - set_bits(sdma_ch[ch]->ch_user_data, 0xFFFFFFFF, 0, cfg->cfg.rect_mode_cfg->user_data); - } - else { - set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 16, 2); - set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 18, cfg->cfg.llt_mode_cfg->endian); - sdma_ch[ch]->ch_llt_cfg = cfg->cfg.llt_mode_cfg->llt_saddr | 0x100 | - (cfg->cfg.llt_mode_cfg->llt_len << 12) | 0x80000 | - (cfg->cfg.llt_mode_cfg->llt_grp_num << 20) | 0x8000000; - } - return 0; -} - -void sys_dma_config_llt(const llt_cfg_t* llt_cfg) -{ - volatile llt_entry_t* entry = (volatile llt_entry_t*)(SDMA_BASE_ADDR + 0x100 + (0x10 * llt_cfg->entry)); - entry->saddr = (uint32_t)llt_cfg->saddr; - entry->daddr = (uint32_t)llt_cfg->daddr; - entry->size = llt_cfg->size; - entry->cfg = (llt_cfg->channel << 4) | (llt_cfg->first ? 2 : 0) | (llt_cfg->done ? 0 : 1); -} - -void sys_dma_start(dma_ch_t channel) -{ - sys_dma_int_en(channel, true); - sdma_ch[channel]->ch_ctl = 0x3; -} - -void sys_dma_stop(dma_ch_t channel) -{ - sdma_ch[channel]->ch_ctl = 0x3 << 4; -} - -void sys_dma_pause(dma_ch_t channel) -{ - sdma_ch[channel]->ch_ctl = 0x3 << 8; -} - -void sys_dma_continue(dma_ch_t channel) -{ - sdma_ch[channel]->ch_ctl = 0x2 << 8; -} - -void sys_dma_wait_idle(dma_ch_t channel) -{ - while(!(sdma_ch[channel]->ch_state & 0x1)) - ; -} - -void sys_dma_wait_done(dma_ch_t channel) -{ - while(!(sdma->int_state & (0x1 << channel))) - ; - sdma->int_raw = 0x1 << channel; -} - -void sys_dma_wait_half_done(dma_ch_t channel) -{ - while(!(sdma->int_state & (0x10 << channel))) - ; - sdma->int_raw = 0x10 << channel; -} - -void sys_dma_wait_llt_event(dma_ch_t channel) -{ - while(!(sdma->int_state & (0x100 << channel))) - ; - sdma->int_raw = 0x100 << channel; -} - -bool sys_dma_is_idle(dma_ch_t channel) -{ - return sdma_ch[channel]->ch_state & 0x1; -} - -int peri_dma_config(const peri_dma_cfg_t* cfg) -{ - uint8_t ch = cfg->channel; - if (ch >= DMA_CHANNEL_MAX) return -1; - - while (!(pdma_ch[ch]->ch_state & 0x1)) - ; - peri_dma_int_en(ch, false); - peri_dma_clear_int(ch); - pdma_ch_dev_sel->ch_dev_sel[ch] = cfg->dev_sel; - pdma_ch[ch]->ch_ctl = cfg->endian << 2; - uint32_t cfg0 = cfg->src_type | (cfg->buswidth << 2) | - (cfg->mode << 4) | ((cfg->ch_priority ? cfg->ch_priority : 1) << 8) | - (cfg->dev_burst_len << 12); - if (cfg->line_num) - cfg0 |= cfg->line_num << 16; - pdma_ch[ch]->ch_cfg0 = cfg0; - pdma_ch[ch]->ch_cfg1 = cfg->line_size | (cfg->line_space << 16); - pdma_ch[ch]->ch_saddr = (uint32_t)cfg->saddr; - pdma_ch[ch]->ch_daddr = (uint32_t)cfg->daddr; - if (cfg->dev_timeout) - pdma_ch[ch]->ch_dev_timeout = cfg->dev_timeout; - - return 0; -} - -void peri_dma_start(dma_ch_t channel) -{ - peri_dma_int_en(channel, true); - pdma_ch[channel]->ch_ctl |= 0x3; -} - -void peri_dma_stop(dma_ch_t channel) -{ - pdma_ch[channel]->ch_ctl |= 0x3 << 4; -} - -void peri_dma_pause(dma_ch_t channel) -{ - pdma_ch[channel]->ch_ctl |= 0x3 << 8; -} - -void peri_dma_continue(dma_ch_t channel) -{ - pdma_ch[channel]->ch_ctl |= 0x2 << 8; -} - -void peri_dma_wait_done_or_timeout(dma_ch_t channel) -{ - while (!((pdma->int_state0 >> channel) & 0x1) && !((pdma->int_state1 >> channel) & 0x1)) - ; -} - -bool peri_dma_is_done(dma_ch_t channel) -{ - return (pdma->int_state0 >> channel) & 0x1; -} - -bool peri_dma_is_timeout(dma_ch_t channel) -{ - return (pdma->int_state1 >> channel) & 0x1; -} - -uint32_t peri_dma_get_state0() -{ - return pdma->int_state0; -} - -uint32_t peri_dma_get_state1() -{ - return pdma->int_state1; -} - -#pragma GCC diagnostic pop +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include +#include "platform.h" +#include "dmac.h" + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wpointer-to-int-cast" +#pragma GCC diagnostic ignored "-Wint-to-pointer-cast" + +#define PERI_DEV_SEL 0x260 + +typedef struct sdma { + volatile uint32_t int_raw; + volatile uint32_t int_en; + volatile uint32_t int_state; + volatile uint32_t ch_weight; +} sdma_t; + +typedef struct sdma_ch { + volatile uint32_t ch_ctl; + volatile uint32_t ch_cfg0; + volatile uint32_t ch_cfg1; + volatile uint32_t ch_saddr; + volatile uint32_t ch_daddr; + volatile uint32_t ch_user_data; + volatile uint32_t ch_llt_cfg; + volatile uint32_t ch_state; +} sdma_ch_t; + +typedef struct llt_entry { + volatile uint32_t saddr; + volatile uint32_t daddr; + volatile uint32_t size; + volatile uint32_t cfg; +} llt_entry_t; + +typedef struct pdma { + volatile uint32_t int_raw0; + volatile uint32_t int_raw1; + volatile uint32_t int_en0; + volatile uint32_t int_en1; + volatile uint32_t int_state0; + volatile uint32_t int_state1; +} pdma_t; + +typedef struct pdma_ch { + volatile uint32_t ch_ctl; + volatile uint32_t ch_cfg0; + volatile uint32_t ch_cfg1; + volatile uint32_t ch_saddr; + volatile uint32_t ch_daddr; + volatile uint32_t ch_dev_timeout; + volatile uint32_t ch_state; +} pdma_ch_t; + +typedef struct pdma_dev_sel { + volatile uint32_t ch_dev_sel[16]; +} pdma_dev_sel_t; + +static volatile sdma_t* const sdma = (volatile sdma_t*)SDMA_BASE_ADDR; +static volatile pdma_t* const pdma = (volatile pdma_t*)PDMA_BASE_ADDR; + +static volatile sdma_ch_t* const sdma_ch[] = { + (volatile sdma_ch_t*)(SDMA_BASE_ADDR + 0x20), + (volatile sdma_ch_t*)(SDMA_BASE_ADDR + 0x40), + (volatile sdma_ch_t*)(SDMA_BASE_ADDR + 0x60), + (volatile sdma_ch_t*)(SDMA_BASE_ADDR + 0x80), +}; + +static volatile pdma_ch_t* const pdma_ch[] = { + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x20), + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x40), + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x60), + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x80), + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0xA0), + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0xC0), + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0xE0), + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x100), + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x120), + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x140), + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x160), + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x180), + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x1A0), + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x1C0), + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x1E0), + (volatile pdma_ch_t*)(PDMA_BASE_ADDR + 0x200), +}; + +static volatile pdma_dev_sel_t* const pdma_ch_dev_sel = (volatile pdma_dev_sel_t*)(PDMA_BASE_ADDR + PERI_DEV_SEL); + +#define set_bits(reg, mask, offset, val) do { \ + (reg) &= ~((mask) << (offset)); \ + (reg) |= (val) << (offset); \ +} while (0) + +static void sys_dma_int_en(uint8_t ch, bool en) +{ + const uint32_t t[2][4] = { + { 0xEEE, 0xDDD, 0xBBB, 0x777 }, + { 0x111, 0x222, 0x444, 0x888 } + }; + if (en) { + sdma->int_en |= t[1][ch]; + } else { + sdma->int_en &= t[0][ch]; + } +} + +static void sys_dma_clear_int(uint8_t ch) +{ + const uint32_t t[4] = { 0x111, 0x222, 0x444, 0x888 }; + sdma->int_raw |= t[ch]; +} + +static void peri_dma_int_en(uint8_t ch, bool en) +{ + if (en) { + pdma->int_en0 |= (1 << (ch + 16) | 1 << ch); + pdma->int_en1 |= 1 << ch; + } else { + pdma->int_en0 &= ~(1 << (ch + 16) | 1 << ch); + pdma->int_en1 &= ~(1 << ch); + } +} + +void peri_dma_clear_int(uint8_t ch) +{ + pdma->int_raw0 = (1 << (ch + 16) | 1 << ch); + pdma->int_raw1 = 1 << ch; +} + +void peri_dma_clear_done(uint8_t ch) +{ + pdma->int_raw0 = (1 << ch); +} + +void peri_dma_clear_half_done(uint8_t ch) +{ + pdma->int_raw0 = (1 << (ch + 16)); +} + +void peri_dma_clear_timeout(uint8_t ch) +{ + pdma->int_raw1 = (1 << ch); +} + + +int sys_dma_config(const sys_dma_cfg_t* cfg) +{ + uint8_t ch = cfg->channel; + if (ch >= DMA_CHANNEL_4) return -1; + + while (!(sdma_ch[ch]->ch_state & 0x1)) + ; + sys_dma_int_en(ch, false); + sys_dma_clear_int(ch); + if (cfg->mode == LINE_MODE) { + set_bits(sdma_ch[ch]->ch_cfg0, 0xFFFFF, 0, cfg->cfg.line_mode_cfg->line_size); + set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 16, 0); + set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 18, cfg->cfg.line_mode_cfg->endian); + set_bits(sdma_ch[ch]->ch_cfg1, 0x1, 20, cfg->cfg.line_mode_cfg->fixed_word); + set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 24, cfg->cfg.line_mode_cfg->user_data_size); + set_bits(sdma_ch[ch]->ch_saddr, 0xFFFFFFFF, 0, (uint32_t)cfg->cfg.line_mode_cfg->saddr); + set_bits(sdma_ch[ch]->ch_daddr, 0xFFFFFFFF, 0, (uint32_t)cfg->cfg.line_mode_cfg->daddr); + set_bits(sdma_ch[ch]->ch_user_data, 0xFFFFFFFF, 0, cfg->cfg.line_mode_cfg->user_data); + } + else if (cfg->mode == RECT_MODE) { + set_bits(sdma_ch[ch]->ch_cfg0, 0xFFFFF, 0, cfg->cfg.rect_mode_cfg->line_size); + set_bits(sdma_ch[ch]->ch_cfg0, 0x7FF, 20, cfg->cfg.rect_mode_cfg->line_num); + set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 16, 1); + set_bits(sdma_ch[ch]->ch_cfg1, 0xFFFF, 0, cfg->cfg.rect_mode_cfg->line_space); + set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 18, cfg->cfg.rect_mode_cfg->endian); + set_bits(sdma_ch[ch]->ch_cfg1, 0x1, 20, cfg->cfg.rect_mode_cfg->fixed_word); + set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 24, cfg->cfg.rect_mode_cfg->user_data_size); + set_bits(sdma_ch[ch]->ch_saddr, 0xFFFFFFFF, 0, (uint32_t)cfg->cfg.rect_mode_cfg->saddr); + set_bits(sdma_ch[ch]->ch_daddr, 0xFFFFFFFF, 0, (uint32_t)cfg->cfg.rect_mode_cfg->daddr); + set_bits(sdma_ch[ch]->ch_user_data, 0xFFFFFFFF, 0, cfg->cfg.rect_mode_cfg->user_data); + } + else { + set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 16, 2); + set_bits(sdma_ch[ch]->ch_cfg1, 0x3, 18, cfg->cfg.llt_mode_cfg->endian); + sdma_ch[ch]->ch_llt_cfg = cfg->cfg.llt_mode_cfg->llt_saddr | 0x100 | + (cfg->cfg.llt_mode_cfg->llt_len << 12) | 0x80000 | + (cfg->cfg.llt_mode_cfg->llt_grp_num << 20) | 0x8000000; + } + return 0; +} + +void sys_dma_config_llt(const llt_cfg_t* llt_cfg) +{ + volatile llt_entry_t* entry = (volatile llt_entry_t*)(SDMA_BASE_ADDR + 0x100 + (0x10 * llt_cfg->entry)); + entry->saddr = (uint32_t)llt_cfg->saddr; + entry->daddr = (uint32_t)llt_cfg->daddr; + entry->size = llt_cfg->size; + entry->cfg = (llt_cfg->channel << 4) | (llt_cfg->first ? 2 : 0) | (llt_cfg->done ? 0 : 1); +} + +void sys_dma_start(dma_ch_t channel) +{ + sys_dma_int_en(channel, true); + sdma_ch[channel]->ch_ctl = 0x3; +} + +void sys_dma_stop(dma_ch_t channel) +{ + sdma_ch[channel]->ch_ctl = 0x3 << 4; +} + +void sys_dma_pause(dma_ch_t channel) +{ + sdma_ch[channel]->ch_ctl = 0x3 << 8; +} + +void sys_dma_continue(dma_ch_t channel) +{ + sdma_ch[channel]->ch_ctl = 0x2 << 8; +} + +void sys_dma_wait_idle(dma_ch_t channel) +{ + while(!(sdma_ch[channel]->ch_state & 0x1)) + ; +} + +void sys_dma_wait_done(dma_ch_t channel) +{ + while(!(sdma->int_state & (0x1 << channel))) + ; + sdma->int_raw = 0x1 << channel; +} + +void sys_dma_wait_half_done(dma_ch_t channel) +{ + while(!(sdma->int_state & (0x10 << channel))) + ; + sdma->int_raw = 0x10 << channel; +} + +void sys_dma_wait_llt_event(dma_ch_t channel) +{ + while(!(sdma->int_state & (0x100 << channel))) + ; + sdma->int_raw = 0x100 << channel; +} + +bool sys_dma_is_idle(dma_ch_t channel) +{ + return sdma_ch[channel]->ch_state & 0x1; +} + +int peri_dma_config(const peri_dma_cfg_t* cfg) +{ + uint8_t ch = cfg->channel; + if (ch >= DMA_CHANNEL_MAX) return -1; + + while (!(pdma_ch[ch]->ch_state & 0x1)) + ; + peri_dma_int_en(ch, false); + peri_dma_clear_int(ch); + pdma_ch_dev_sel->ch_dev_sel[ch] = cfg->dev_sel; + pdma_ch[ch]->ch_ctl = cfg->endian << 2; + uint32_t cfg0 = cfg->src_type | (cfg->buswidth << 2) | + (cfg->mode << 4) | ((cfg->ch_priority ? cfg->ch_priority : 1) << 8) | + (cfg->dev_burst_len << 12); + if (cfg->line_num) + cfg0 |= cfg->line_num << 16; + pdma_ch[ch]->ch_cfg0 = cfg0; + pdma_ch[ch]->ch_cfg1 = cfg->line_size | (cfg->line_space << 16); + pdma_ch[ch]->ch_saddr = (uint32_t)cfg->saddr; + pdma_ch[ch]->ch_daddr = (uint32_t)cfg->daddr; + if (cfg->dev_timeout) + pdma_ch[ch]->ch_dev_timeout = cfg->dev_timeout; + + return 0; +} + +void peri_dma_start(dma_ch_t channel) +{ + peri_dma_int_en(channel, true); + pdma_ch[channel]->ch_ctl |= 0x3; +} + +void peri_dma_stop(dma_ch_t channel) +{ + pdma_ch[channel]->ch_ctl |= 0x3 << 4; +} + +void peri_dma_pause(dma_ch_t channel) +{ + pdma_ch[channel]->ch_ctl |= 0x3 << 8; +} + +void peri_dma_continue(dma_ch_t channel) +{ + pdma_ch[channel]->ch_ctl |= 0x2 << 8; +} + +void peri_dma_wait_done_or_timeout(dma_ch_t channel) +{ + while (!((pdma->int_state0 >> channel) & 0x1) && !((pdma->int_state1 >> channel) & 0x1)) + ; +} + +bool peri_dma_is_done(dma_ch_t channel) +{ + return (pdma->int_state0 >> channel) & 0x1; +} + +bool peri_dma_is_timeout(dma_ch_t channel) +{ + return (pdma->int_state1 >> channel) & 0x1; +} + +uint32_t peri_dma_get_state0() +{ + return pdma->int_state0; +} + +uint32_t peri_dma_get_state1() +{ + return pdma->int_state1; +} + +#pragma GCC diagnostic pop diff --git a/package/k510_evb_test/src/bsp/controler/emac.c b/package/k510_evb_test/src/bsp/controler/emac.c index 079da3d..21a49b1 100644 --- a/package/k510_evb_test/src/bsp/controler/emac.c +++ b/package/k510_evb_test/src/bsp/controler/emac.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include "emac.h" #include "platform.h" #include "emac_regs_non_vm.h" diff --git a/package/k510_evb_test/src/bsp/controler/gpio.c b/package/k510_evb_test/src/bsp/controler/gpio.c index bc02309..68c7fac 100644 --- a/package/k510_evb_test/src/bsp/controler/gpio.c +++ b/package/k510_evb_test/src/bsp/controler/gpio.c @@ -1,16 +1,26 @@ -/* Copyright 2018 Canaan Inc. +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "gpio.h" #include "muxpin.h" diff --git a/package/k510_evb_test/src/bsp/controler/h264.c b/package/k510_evb_test/src/bsp/controler/h264.c index d0c085f..071d248 100644 --- a/package/k510_evb_test/src/bsp/controler/h264.c +++ b/package/k510_evb_test/src/bsp/controler/h264.c @@ -1,6 +1,27 @@ -/* - * Copyright (c) 2019 Canaan INC. - * */ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include //#include diff --git a/package/k510_evb_test/src/bsp/controler/i2c.c b/package/k510_evb_test/src/bsp/controler/i2c.c index 3258739..7e5980b 100644 --- a/package/k510_evb_test/src/bsp/controler/i2c.c +++ b/package/k510_evb_test/src/bsp/controler/i2c.c @@ -1,16 +1,26 @@ -/* Copyright 2018 Canaan Inc. +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include "i2c.h" diff --git a/package/k510_evb_test/src/bsp/controler/i2s.c b/package/k510_evb_test/src/bsp/controler/i2s.c index dadb0c4..48f1462 100644 --- a/package/k510_evb_test/src/bsp/controler/i2s.c +++ b/package/k510_evb_test/src/bsp/controler/i2s.c @@ -1,16 +1,26 @@ -/* Copyright 2018 Canaan Inc. +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "stdlib.h" #include "io.h" diff --git a/package/k510_evb_test/src/bsp/controler/i2s_slave.c b/package/k510_evb_test/src/bsp/controler/i2s_slave.c index 39fdd51..5a92b1f 100644 --- a/package/k510_evb_test/src/bsp/controler/i2s_slave.c +++ b/package/k510_evb_test/src/bsp/controler/i2s_slave.c @@ -1,16 +1,26 @@ -/* Copyright 2018 Canaan Inc. +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "io.h" #include "i2s.h" diff --git a/package/k510_evb_test/src/bsp/controler/mailbox.c b/package/k510_evb_test/src/bsp/controler/mailbox.c index 263de46..9890503 100644 --- a/package/k510_evb_test/src/bsp/controler/mailbox.c +++ b/package/k510_evb_test/src/bsp/controler/mailbox.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include "k510.h" #include "mailbox.h" #include "sleep.h" diff --git a/package/k510_evb_test/src/bsp/controler/muxpin.c b/package/k510_evb_test/src/bsp/controler/muxpin.c index 1b63f07..4aa8f31 100644 --- a/package/k510_evb_test/src/bsp/controler/muxpin.c +++ b/package/k510_evb_test/src/bsp/controler/muxpin.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include "muxpin.h" #include "k510.h" #include diff --git a/package/k510_evb_test/src/bsp/controler/muxpin_default.c b/package/k510_evb_test/src/bsp/controler/muxpin_default.c index b3876ba..7a8960e 100644 --- a/package/k510_evb_test/src/bsp/controler/muxpin_default.c +++ b/package/k510_evb_test/src/bsp/controler/muxpin_default.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include #ifdef _EVBLP4 diff --git a/package/k510_evb_test/src/bsp/controler/noc_bandwidth.c b/package/k510_evb_test/src/bsp/controler/noc_bandwidth.c index fd56354..0d757fd 100644 --- a/package/k510_evb_test/src/bsp/controler/noc_bandwidth.c +++ b/package/k510_evb_test/src/bsp/controler/noc_bandwidth.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include #include #include diff --git a/package/k510_evb_test/src/bsp/controler/otp.c b/package/k510_evb_test/src/bsp/controler/otp.c index 8900879..6b246bb 100644 --- a/package/k510_evb_test/src/bsp/controler/otp.c +++ b/package/k510_evb_test/src/bsp/controler/otp.c @@ -1,676 +1,700 @@ -#include "core_v5.h" -#include "platform.h" -#include "sleep.h" -#include "sysctl_boot.h" -#include "sysctl_rst.h" -#include "otp.h" - -volatile OTP_CTRL_S *otp_ctrl = (volatile OTP_CTRL_S *)(OTP_CONTROLLER_BASE_ADDR); - -volatile OTP_WRITE_PROTECT_S *otp_write_protect = (volatile OTP_WRITE_PROTECT_S *)(OTP_WRITE_PROTECT_ADDR); -volatile OTP_READ_PROTECT_S *otp_read_protect = (volatile OTP_READ_PROTECT_S *)(OTP_READ_PROTECT_ADDR); -volatile OTP_FUNC_S *otp_func_enable_disable = (volatile OTP_FUNC_S *)(OTP_FUNC_ADDR); - -bool otp_get_status(OTP_STATUS_E eStatus) -{ - if(OTP_BYPASS_STATUS == eStatus) - { - return sysctl_boot_get_otp_bypass(); - } - else - { - return false; - } -} - -bool otp_read_word(volatile uint32_t addr, uint32_t *buff, uint32_t len) -{ - uint32_t wait_us; - - if(true == otp_get_status(OTP_BYPASS_STATUS)) - { - return false; - } - - /* otp enable */ - otp_ctrl->enable = OTP_ENABLE; - otp_ctrl->test_row = 0; - wait_us = 0; - while(1) - { - if((OTP_NEED_NOTHING_INIT == otp_ctrl->init_done) || (OTP_INIT_SUCCESS == otp_ctrl->init_done)) - { - break; - } - else - { - if(wait_us++ > 2000) - { - otp_ctrl->enable = OTP_DISABLE; - return false; - } - else - { - usleep(1); - } - } - } - - /* set otp read */ - otp_ctrl->mode = OTP_MODE_READ; - wait_us = 0; - while(1) - { - if(OTP_MODE_READ == otp_ctrl->mode_status) - { - break; - } - else - { - if(wait_us++ > 2000) - { - wait_us = 0; - otp_ctrl->mode = OTP_MODE_STANDBY_DEP; - while(1) - { - if((OTP_MODE_STANDBY_DEP == otp_ctrl->mode_status) || (wait_us++ > 2000)) - { - break; - } - else - { - usleep(1); - } - } - otp_ctrl->enable = OTP_DISABLE; - return false; - } - else - { - usleep(1); - } - } - } - - for(int i = 0; i < len; i ++) - { - buff[i] = *(uint32_t *)(long)(addr + 4*i); - } - - otp_ctrl->mode = OTP_MODE_STANDBY_DEP; - wait_us = 0; - while(1) - { - if(OTP_MODE_STANDBY_DEP == otp_ctrl->mode_status) - { - break; - } - else - { - if(wait_us++ > 2000) - { - otp_ctrl->enable = OTP_DISABLE; - return false; - } - else - { - usleep(1); - } - } - } - - otp_ctrl->enable = OTP_DISABLE; - return true; -} - -bool otp_write_word(uint32_t addr, uint32_t *buff, uint32_t len) -{ - uint32_t wait_us; - - if(true == otp_get_status(OTP_BYPASS_STATUS)) - { - return false; - } - /* otp enable */ - otp_ctrl->enable = OTP_ENABLE; - otp_ctrl->test_row = 0; - wait_us = 0; - while(1) - { - if((OTP_NEED_NOTHING_INIT == otp_ctrl->init_done) || (OTP_INIT_SUCCESS == otp_ctrl->init_done)) - { - break; - } - else - { - if(wait_us++ > 2000) - { - return false; - } - else - { - usleep(1); - } - } - } - - /* set otp program */ - otp_ctrl->mode = OTP_MODE_PROGRAM; - wait_us = 0; - while(1) - { - if(OTP_MODE_PROGRAM == otp_ctrl->mode_status) - { - break; - } - else - { - if(wait_us++ > 2000) - { - wait_us = 0; - otp_ctrl->mode = OTP_MODE_STANDBY_DEP; - while(1) - { - if((OTP_MODE_STANDBY_DEP == otp_ctrl->mode_status) || (wait_us++ > 2000)) - { - break; - } - else - { - usleep(1); - } - } - otp_ctrl->enable = OTP_DISABLE; - return false; - } - else - { - usleep(1); - } - } - } - - /* wait for program success*/ - for(int i = 0; i < len; i++) - { - /* wait for ready */ - wait_us = 0; - while(1) - { - if(OTP_PROGRAM_SUCCESS == otp_ctrl->write_ready) - { - break; - } - else - { - if(wait_us++ > 2000) - { - wait_us = 0; - otp_ctrl->mode = OTP_MODE_STANDBY_DEP; - while(1) - { - if((OTP_MODE_STANDBY_DEP == otp_ctrl->mode_status) || (wait_us++ > 2000)) - { - break; - } - else - { - usleep(1); - } - } - otp_ctrl->enable = OTP_DISABLE; - return false; - } - else - { - usleep(1); - } - } - } - - /* write data */ - *(uint32_t *)(long)(addr + 4*i) = buff[i]; - - /* wait for program success*/ - wait_us = 0; - while(1) - { - if(OTP_PROGRAM_SUCCESS == otp_ctrl->write_ready) - { - break; - } - else - { - if(wait_us++ > 2000) - { - wait_us = 0; - otp_ctrl->mode = OTP_MODE_STANDBY_DEP; - while(1) - { - if((OTP_MODE_STANDBY_DEP == otp_ctrl->mode_status) || (wait_us++ > 2000)) - { - break; - } - else - { - usleep(1); - } - } - otp_ctrl->enable = OTP_DISABLE; - return false; - } - else - { - usleep(1); - } - } - } - } - - otp_ctrl->mode = OTP_MODE_STANDBY_DEP; - wait_us = 0; - while(1) - { - if(OTP_MODE_STANDBY_DEP == otp_ctrl->mode_status) - { - break; - } - else - { - if(wait_us++ > 2000) - { - otp_ctrl->enable = OTP_DISABLE; - return false; - } - else - { - usleep(1); - } - } - } - - otp_ctrl->enable = OTP_DISABLE; - return true; -} - -bool otp_get_block_write_protect(OTP_BLOCK_E block) -{ - uint32_t protect_flag = OTP_DEFUALT_WORD_VALUE; - - uint32_t protect_addr; - - if((block >= OTP_BLOCK_DATA_1K_0) && (block <= OTP_BLOCK_DATA_1K_28)) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_data_1k[block - OTP_BLOCK_DATA_1K_0]; - } - else if((block >= OTP_BLOCK_DATA_256_0) && (block <= OTP_BLOCK_DATA_256_3)) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_data_256[block - OTP_BLOCK_DATA_256_0]; - } - else if((block >= OTP_BLOCK_DATA_128_0) && (block <= OTP_BLOCK_DATA_128_4)) - { - protect_addr = (uint32_t)(long)&otp_write_protect->opt_block_data_128[block - OTP_BLOCK_DATA_128_0]; - } - else if((block >= OTP_BLOCK_PRODUCT_INFO0) && (block <= OTP_BLOCK_PRODUCT_INFO3)) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_product_info[block - OTP_BLOCK_PRODUCT_INFO0]; - } - else if((block >= OTP_BLOCK_PATCH_0) && (block <= OTP_BLOCK_PATCH_2)) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_patch[block - OTP_BLOCK_PATCH_0]; - } - else if(OTP_BLOCK_UART_INFO == block) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_uart_info; - } - else if(OTP_BLOCK_RESERVED_0 == block) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_reserved_0; - } - else if(OTP_BLOCK_SPI0_SDIO0_LINE == block) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_spi0_sdio0_line; - } - else if(OTP_BLOCK_RESERVED_1 == block) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_reserved_1; - } - else if(OTP_BLOCK_SHA256_SIGN == block) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_sha256_sign; - } - else if((block >= OTP_BLOCK_SHA256_HASH0) && (block <= OTP_BLOCK_SHA256_HASH1)) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_sha256_hash[block - OTP_BLOCK_SHA256_HASH0]; - } - else if((block >= OTP_BLOCK_AES_KEY0) && (block <= OTP_BLOCK_AES_KEY1)) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_aes_key[block - OTP_BLOCK_AES_KEY0]; - } - else if(OTP_BLOCK_RESERVED_2 == block) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_reserved_2; - } - else if(block == OTP_BLOCK_RSA2048_KEY0) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_rsa2048_key0; - } - else if(block == OTP_BLOCK_RSA2048_KEY1) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_rsa2048_key1; - } - else - { - return false; - } - - /* read error */ - protect_flag = OTP_DEFUALT_WORD_VALUE; - if(true != otp_read_word(protect_addr,&protect_flag,1)) - { - return false; - } - /* already write protect */ - if(OTP_DEFUALT_WORD_VALUE != protect_flag) - { - return true; - } - else - { - return false; - } -} - -bool otp_set_block_write_protect(OTP_BLOCK_E block) -{ - uint32_t protect_flag = OTP_DEFUALT_WORD_VALUE; - - uint32_t protect_addr; - - if((block >= OTP_BLOCK_DATA_1K_0) && (block <= OTP_BLOCK_DATA_1K_28)) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_data_1k[block - OTP_BLOCK_DATA_1K_0]; - } - else if((block >= OTP_BLOCK_DATA_256_0) && (block <= OTP_BLOCK_DATA_256_3)) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_data_256[block - OTP_BLOCK_DATA_256_0]; - } - else if((block >= OTP_BLOCK_DATA_128_0) && (block <= OTP_BLOCK_DATA_128_4)) - { - protect_addr = (uint32_t)(long)&otp_write_protect->opt_block_data_128[block - OTP_BLOCK_DATA_128_0]; - } - else if((block >= OTP_BLOCK_PRODUCT_INFO0) && (block <= OTP_BLOCK_PRODUCT_INFO3)) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_product_info[block - OTP_BLOCK_PRODUCT_INFO0]; - } - else if((block >= OTP_BLOCK_PATCH_0) && (block <= OTP_BLOCK_PATCH_2)) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_patch[block - OTP_BLOCK_PATCH_0]; - } - else if(OTP_BLOCK_UART_INFO == block) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_uart_info; - } - else if(OTP_BLOCK_RESERVED_0 == block) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_reserved_0; - } - else if(OTP_BLOCK_SPI0_SDIO0_LINE == block) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_spi0_sdio0_line; - } - else if(OTP_BLOCK_RESERVED_1 == block) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_reserved_1; - } - else if(OTP_BLOCK_SHA256_SIGN == block) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_sha256_sign; - } - else if((block >= OTP_BLOCK_SHA256_HASH0) && (block <= OTP_BLOCK_SHA256_HASH1)) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_sha256_hash[block - OTP_BLOCK_SHA256_HASH0]; - } - else if((block >= OTP_BLOCK_AES_KEY0) && (block <= OTP_BLOCK_AES_KEY1)) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_aes_key[block - OTP_BLOCK_AES_KEY0]; - } - else if(OTP_BLOCK_RESERVED_2 == block) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_reserved_2; - } - else if(block == OTP_BLOCK_RSA2048_KEY0) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_rsa2048_key0; - } - else if(block == OTP_BLOCK_RSA2048_KEY1) - { - protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_rsa2048_key1; - } - else - { - return false; - } - - /* read error */ - protect_flag = OTP_DEFUALT_WORD_VALUE; - if(true != otp_read_word(protect_addr,&protect_flag,1)) - { - return false; - } - /* already write protect */ - if(OTP_DEFUALT_WORD_VALUE != protect_flag) - { - return true; - } - - /* write protect */ - protect_flag = 0; - (void)otp_write_word(protect_addr,&protect_flag,1); - protect_flag = OTP_DEFUALT_WORD_VALUE; - if(true != otp_read_word(protect_addr,&protect_flag,1)) - { - return false; - } - if(OTP_DEFUALT_WORD_VALUE != protect_flag) - { - return true; - } - else - { - return false; - } -} - -bool otp_get_block_read_protect(OTP_BLOCK_E block) -{ - uint32_t protect_flag = OTP_DEFUALT_WORD_VALUE; - uint32_t protect_addr; - - if(block == OTP_BLOCK_SHA256_HASH0) - protect_addr = (uint32_t)(long)&otp_read_protect->otp_sha256_hash[0]; - else if(block == OTP_BLOCK_SHA256_HASH1) - protect_addr = (uint32_t)(long)&otp_read_protect->otp_sha256_hash[1]; - else if(block == OTP_BLOCK_AES_KEY0) - protect_addr = (uint32_t)(long)&otp_read_protect->otp_aes_key[0]; - else if(block == OTP_BLOCK_AES_KEY1) - protect_addr = (uint32_t)(long)&otp_read_protect->otp_aes_key[1]; - else - return false; - - /* read error */ - protect_flag = OTP_DEFUALT_WORD_VALUE; - if(true != otp_read_word(protect_addr,&protect_flag,1)) - { - return false; - } - /* already write protect */ - if(OTP_DEFUALT_WORD_VALUE != protect_flag) - { - return true; - } - else - { - return false; - } -} - -bool otp_set_block_read_protect(OTP_BLOCK_E block) -{ - uint32_t protect_flag = OTP_DEFUALT_WORD_VALUE; - uint32_t protect_addr; - - if(block == OTP_BLOCK_SHA256_HASH0) - protect_addr = (uint32_t)(long)&otp_read_protect->otp_sha256_hash[0]; - else if(block == OTP_BLOCK_SHA256_HASH1) - protect_addr = (uint32_t)(long)&otp_read_protect->otp_sha256_hash[1]; - else if(block == OTP_BLOCK_AES_KEY0) - protect_addr = (uint32_t)(long)&otp_read_protect->otp_aes_key[0]; - else if(block == OTP_BLOCK_AES_KEY1) - protect_addr = (uint32_t)(long)&otp_read_protect->otp_aes_key[1]; - else - return false; - - /* read error */ - protect_flag = OTP_DEFUALT_WORD_VALUE; - if(true != otp_read_word(protect_addr,&protect_flag,1)) - { - return false; - } - /* already write protect */ - if(OTP_DEFUALT_WORD_VALUE != protect_flag) - { - return true; - } - - /* write protect */ - protect_flag = ~OTP_DEFUALT_WORD_VALUE; - (void)otp_write_word(protect_addr,&protect_flag,1); - protect_flag = OTP_DEFUALT_WORD_VALUE; - if(true != otp_read_word(protect_addr,&protect_flag,1)) - { - return false; - } - if(OTP_DEFUALT_WORD_VALUE != protect_flag) - { - return true; - } - else - { - return false; - } -} - -bool otp_get_func_flag(OTP_FUNC_E eFunc) -{ - uint32_t func_flag = OTP_DEFUALT_WORD_VALUE; - uint32_t func_addr; - - if(eFunc >= OTP_FUNC_MAX) - { - return false; - } - - func_addr = (uint32_t)(long)&otp_func_enable_disable->otp_func_flag[eFunc]; - - if(true != otp_read_word(func_addr, &func_flag,1)) - { - return false; - } - else - { - if(OTP_DEFUALT_WORD_VALUE != func_flag) - { - return true; - } - else - { - return false; - } - } -} - -bool otp_set_func_flag(OTP_FUNC_E eFunc) -{ - uint32_t func_flag = OTP_DEFUALT_WORD_VALUE; - uint32_t func_addr; - - if(eFunc >= OTP_FUNC_MAX) - { - return false; - } - - func_addr = (uint32_t)(long)&otp_func_enable_disable->otp_func_flag[eFunc]; - - if(true != otp_read_word(func_addr,&func_flag,1)) - { - return false; - } - - if(OTP_DEFUALT_WORD_VALUE != func_flag) - { - return true; - } - - func_flag = ~OTP_DEFUALT_WORD_VALUE; - (void)otp_write_word(func_addr,&func_flag,1); - - func_flag = OTP_DEFUALT_WORD_VALUE; - if(true != otp_read_word(func_addr,&func_flag,1)) - { - return false; - } - if(OTP_DEFUALT_WORD_VALUE != func_flag) - { - return true; - } - else - { - return false; - } -} - -void otp_select_key(OTP_SEL_CRYPTER_E crypter, OTP_SEL_KEY_E key) -{ - if(OTP_SEL_AES == crypter) - { - if(key == OTP_SEL_KEY_0) - { - otp_ctrl->aes_sha256_key_sel &= ~(1 << 0); - } - else - { - otp_ctrl->aes_sha256_key_sel |= (1 << 0); - } - } - else if(OTP_SEL_SHA256 ==crypter) - { - if(key == OTP_SEL_KEY_0) - { - otp_ctrl->aes_sha256_key_sel &= ~(1 << 4); - } - else - { - otp_ctrl->aes_sha256_key_sel |= (1 << 4); - } - } - else - { - - } -} - -void otp_reset(void) -{ - if(true == otp_get_status(OTP_BYPASS_STATUS)) - { - return; - } - otp_ctrl->mode = OTP_MODE_STANDBY_DEP; - while(otp_ctrl->mode_status != OTP_MODE_STANDBY_DEP) - { - } - otp_ctrl->enable = OTP_DISABLE; - usleep(100); - sysctl_reset(SYSCTL_RESET_OTP); -} +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "core_v5.h" +#include "platform.h" +#include "sleep.h" +#include "sysctl_boot.h" +#include "sysctl_rst.h" +#include "otp.h" + +volatile OTP_CTRL_S *otp_ctrl = (volatile OTP_CTRL_S *)(OTP_CONTROLLER_BASE_ADDR); + +volatile OTP_WRITE_PROTECT_S *otp_write_protect = (volatile OTP_WRITE_PROTECT_S *)(OTP_WRITE_PROTECT_ADDR); +volatile OTP_READ_PROTECT_S *otp_read_protect = (volatile OTP_READ_PROTECT_S *)(OTP_READ_PROTECT_ADDR); +volatile OTP_FUNC_S *otp_func_enable_disable = (volatile OTP_FUNC_S *)(OTP_FUNC_ADDR); + +bool otp_get_status(OTP_STATUS_E eStatus) +{ + if(OTP_BYPASS_STATUS == eStatus) + { + return sysctl_boot_get_otp_bypass(); + } + else + { + return false; + } +} + +bool otp_read_word(volatile uint32_t addr, uint32_t *buff, uint32_t len) +{ + uint32_t wait_us; + + if(true == otp_get_status(OTP_BYPASS_STATUS)) + { + return false; + } + + /* otp enable */ + otp_ctrl->enable = OTP_ENABLE; + otp_ctrl->test_row = 0; + wait_us = 0; + while(1) + { + if((OTP_NEED_NOTHING_INIT == otp_ctrl->init_done) || (OTP_INIT_SUCCESS == otp_ctrl->init_done)) + { + break; + } + else + { + if(wait_us++ > 2000) + { + otp_ctrl->enable = OTP_DISABLE; + return false; + } + else + { + usleep(1); + } + } + } + + /* set otp read */ + otp_ctrl->mode = OTP_MODE_READ; + wait_us = 0; + while(1) + { + if(OTP_MODE_READ == otp_ctrl->mode_status) + { + break; + } + else + { + if(wait_us++ > 2000) + { + wait_us = 0; + otp_ctrl->mode = OTP_MODE_STANDBY_DEP; + while(1) + { + if((OTP_MODE_STANDBY_DEP == otp_ctrl->mode_status) || (wait_us++ > 2000)) + { + break; + } + else + { + usleep(1); + } + } + otp_ctrl->enable = OTP_DISABLE; + return false; + } + else + { + usleep(1); + } + } + } + + for(int i = 0; i < len; i ++) + { + buff[i] = *(uint32_t *)(long)(addr + 4*i); + } + + otp_ctrl->mode = OTP_MODE_STANDBY_DEP; + wait_us = 0; + while(1) + { + if(OTP_MODE_STANDBY_DEP == otp_ctrl->mode_status) + { + break; + } + else + { + if(wait_us++ > 2000) + { + otp_ctrl->enable = OTP_DISABLE; + return false; + } + else + { + usleep(1); + } + } + } + + otp_ctrl->enable = OTP_DISABLE; + return true; +} + +bool otp_write_word(uint32_t addr, uint32_t *buff, uint32_t len) +{ + uint32_t wait_us; + + if(true == otp_get_status(OTP_BYPASS_STATUS)) + { + return false; + } + /* otp enable */ + otp_ctrl->enable = OTP_ENABLE; + otp_ctrl->test_row = 0; + wait_us = 0; + while(1) + { + if((OTP_NEED_NOTHING_INIT == otp_ctrl->init_done) || (OTP_INIT_SUCCESS == otp_ctrl->init_done)) + { + break; + } + else + { + if(wait_us++ > 2000) + { + return false; + } + else + { + usleep(1); + } + } + } + + /* set otp program */ + otp_ctrl->mode = OTP_MODE_PROGRAM; + wait_us = 0; + while(1) + { + if(OTP_MODE_PROGRAM == otp_ctrl->mode_status) + { + break; + } + else + { + if(wait_us++ > 2000) + { + wait_us = 0; + otp_ctrl->mode = OTP_MODE_STANDBY_DEP; + while(1) + { + if((OTP_MODE_STANDBY_DEP == otp_ctrl->mode_status) || (wait_us++ > 2000)) + { + break; + } + else + { + usleep(1); + } + } + otp_ctrl->enable = OTP_DISABLE; + return false; + } + else + { + usleep(1); + } + } + } + + /* wait for program success*/ + for(int i = 0; i < len; i++) + { + /* wait for ready */ + wait_us = 0; + while(1) + { + if(OTP_PROGRAM_SUCCESS == otp_ctrl->write_ready) + { + break; + } + else + { + if(wait_us++ > 2000) + { + wait_us = 0; + otp_ctrl->mode = OTP_MODE_STANDBY_DEP; + while(1) + { + if((OTP_MODE_STANDBY_DEP == otp_ctrl->mode_status) || (wait_us++ > 2000)) + { + break; + } + else + { + usleep(1); + } + } + otp_ctrl->enable = OTP_DISABLE; + return false; + } + else + { + usleep(1); + } + } + } + + /* write data */ + *(uint32_t *)(long)(addr + 4*i) = buff[i]; + + /* wait for program success*/ + wait_us = 0; + while(1) + { + if(OTP_PROGRAM_SUCCESS == otp_ctrl->write_ready) + { + break; + } + else + { + if(wait_us++ > 2000) + { + wait_us = 0; + otp_ctrl->mode = OTP_MODE_STANDBY_DEP; + while(1) + { + if((OTP_MODE_STANDBY_DEP == otp_ctrl->mode_status) || (wait_us++ > 2000)) + { + break; + } + else + { + usleep(1); + } + } + otp_ctrl->enable = OTP_DISABLE; + return false; + } + else + { + usleep(1); + } + } + } + } + + otp_ctrl->mode = OTP_MODE_STANDBY_DEP; + wait_us = 0; + while(1) + { + if(OTP_MODE_STANDBY_DEP == otp_ctrl->mode_status) + { + break; + } + else + { + if(wait_us++ > 2000) + { + otp_ctrl->enable = OTP_DISABLE; + return false; + } + else + { + usleep(1); + } + } + } + + otp_ctrl->enable = OTP_DISABLE; + return true; +} + +bool otp_get_block_write_protect(OTP_BLOCK_E block) +{ + uint32_t protect_flag = OTP_DEFUALT_WORD_VALUE; + + uint32_t protect_addr; + + if((block >= OTP_BLOCK_DATA_1K_0) && (block <= OTP_BLOCK_DATA_1K_28)) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_data_1k[block - OTP_BLOCK_DATA_1K_0]; + } + else if((block >= OTP_BLOCK_DATA_256_0) && (block <= OTP_BLOCK_DATA_256_3)) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_data_256[block - OTP_BLOCK_DATA_256_0]; + } + else if((block >= OTP_BLOCK_DATA_128_0) && (block <= OTP_BLOCK_DATA_128_4)) + { + protect_addr = (uint32_t)(long)&otp_write_protect->opt_block_data_128[block - OTP_BLOCK_DATA_128_0]; + } + else if((block >= OTP_BLOCK_PRODUCT_INFO0) && (block <= OTP_BLOCK_PRODUCT_INFO3)) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_product_info[block - OTP_BLOCK_PRODUCT_INFO0]; + } + else if((block >= OTP_BLOCK_PATCH_0) && (block <= OTP_BLOCK_PATCH_2)) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_patch[block - OTP_BLOCK_PATCH_0]; + } + else if(OTP_BLOCK_UART_INFO == block) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_uart_info; + } + else if(OTP_BLOCK_RESERVED_0 == block) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_reserved_0; + } + else if(OTP_BLOCK_SPI0_SDIO0_LINE == block) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_spi0_sdio0_line; + } + else if(OTP_BLOCK_RESERVED_1 == block) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_reserved_1; + } + else if(OTP_BLOCK_SHA256_SIGN == block) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_sha256_sign; + } + else if((block >= OTP_BLOCK_SHA256_HASH0) && (block <= OTP_BLOCK_SHA256_HASH1)) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_sha256_hash[block - OTP_BLOCK_SHA256_HASH0]; + } + else if((block >= OTP_BLOCK_AES_KEY0) && (block <= OTP_BLOCK_AES_KEY1)) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_aes_key[block - OTP_BLOCK_AES_KEY0]; + } + else if(OTP_BLOCK_RESERVED_2 == block) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_reserved_2; + } + else if(block == OTP_BLOCK_RSA2048_KEY0) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_rsa2048_key0; + } + else if(block == OTP_BLOCK_RSA2048_KEY1) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_rsa2048_key1; + } + else + { + return false; + } + + /* read error */ + protect_flag = OTP_DEFUALT_WORD_VALUE; + if(true != otp_read_word(protect_addr,&protect_flag,1)) + { + return false; + } + /* already write protect */ + if(OTP_DEFUALT_WORD_VALUE != protect_flag) + { + return true; + } + else + { + return false; + } +} + +bool otp_set_block_write_protect(OTP_BLOCK_E block) +{ + uint32_t protect_flag = OTP_DEFUALT_WORD_VALUE; + + uint32_t protect_addr; + + if((block >= OTP_BLOCK_DATA_1K_0) && (block <= OTP_BLOCK_DATA_1K_28)) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_data_1k[block - OTP_BLOCK_DATA_1K_0]; + } + else if((block >= OTP_BLOCK_DATA_256_0) && (block <= OTP_BLOCK_DATA_256_3)) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_data_256[block - OTP_BLOCK_DATA_256_0]; + } + else if((block >= OTP_BLOCK_DATA_128_0) && (block <= OTP_BLOCK_DATA_128_4)) + { + protect_addr = (uint32_t)(long)&otp_write_protect->opt_block_data_128[block - OTP_BLOCK_DATA_128_0]; + } + else if((block >= OTP_BLOCK_PRODUCT_INFO0) && (block <= OTP_BLOCK_PRODUCT_INFO3)) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_product_info[block - OTP_BLOCK_PRODUCT_INFO0]; + } + else if((block >= OTP_BLOCK_PATCH_0) && (block <= OTP_BLOCK_PATCH_2)) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_patch[block - OTP_BLOCK_PATCH_0]; + } + else if(OTP_BLOCK_UART_INFO == block) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_uart_info; + } + else if(OTP_BLOCK_RESERVED_0 == block) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_reserved_0; + } + else if(OTP_BLOCK_SPI0_SDIO0_LINE == block) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_spi0_sdio0_line; + } + else if(OTP_BLOCK_RESERVED_1 == block) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_reserved_1; + } + else if(OTP_BLOCK_SHA256_SIGN == block) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_sha256_sign; + } + else if((block >= OTP_BLOCK_SHA256_HASH0) && (block <= OTP_BLOCK_SHA256_HASH1)) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_sha256_hash[block - OTP_BLOCK_SHA256_HASH0]; + } + else if((block >= OTP_BLOCK_AES_KEY0) && (block <= OTP_BLOCK_AES_KEY1)) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_aes_key[block - OTP_BLOCK_AES_KEY0]; + } + else if(OTP_BLOCK_RESERVED_2 == block) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_reserved_2; + } + else if(block == OTP_BLOCK_RSA2048_KEY0) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_rsa2048_key0; + } + else if(block == OTP_BLOCK_RSA2048_KEY1) + { + protect_addr = (uint32_t)(long)&otp_write_protect->otp_block_rsa2048_key1; + } + else + { + return false; + } + + /* read error */ + protect_flag = OTP_DEFUALT_WORD_VALUE; + if(true != otp_read_word(protect_addr,&protect_flag,1)) + { + return false; + } + /* already write protect */ + if(OTP_DEFUALT_WORD_VALUE != protect_flag) + { + return true; + } + + /* write protect */ + protect_flag = 0; + (void)otp_write_word(protect_addr,&protect_flag,1); + protect_flag = OTP_DEFUALT_WORD_VALUE; + if(true != otp_read_word(protect_addr,&protect_flag,1)) + { + return false; + } + if(OTP_DEFUALT_WORD_VALUE != protect_flag) + { + return true; + } + else + { + return false; + } +} + +bool otp_get_block_read_protect(OTP_BLOCK_E block) +{ + uint32_t protect_flag = OTP_DEFUALT_WORD_VALUE; + uint32_t protect_addr; + + if(block == OTP_BLOCK_SHA256_HASH0) + protect_addr = (uint32_t)(long)&otp_read_protect->otp_sha256_hash[0]; + else if(block == OTP_BLOCK_SHA256_HASH1) + protect_addr = (uint32_t)(long)&otp_read_protect->otp_sha256_hash[1]; + else if(block == OTP_BLOCK_AES_KEY0) + protect_addr = (uint32_t)(long)&otp_read_protect->otp_aes_key[0]; + else if(block == OTP_BLOCK_AES_KEY1) + protect_addr = (uint32_t)(long)&otp_read_protect->otp_aes_key[1]; + else + return false; + + /* read error */ + protect_flag = OTP_DEFUALT_WORD_VALUE; + if(true != otp_read_word(protect_addr,&protect_flag,1)) + { + return false; + } + /* already write protect */ + if(OTP_DEFUALT_WORD_VALUE != protect_flag) + { + return true; + } + else + { + return false; + } +} + +bool otp_set_block_read_protect(OTP_BLOCK_E block) +{ + uint32_t protect_flag = OTP_DEFUALT_WORD_VALUE; + uint32_t protect_addr; + + if(block == OTP_BLOCK_SHA256_HASH0) + protect_addr = (uint32_t)(long)&otp_read_protect->otp_sha256_hash[0]; + else if(block == OTP_BLOCK_SHA256_HASH1) + protect_addr = (uint32_t)(long)&otp_read_protect->otp_sha256_hash[1]; + else if(block == OTP_BLOCK_AES_KEY0) + protect_addr = (uint32_t)(long)&otp_read_protect->otp_aes_key[0]; + else if(block == OTP_BLOCK_AES_KEY1) + protect_addr = (uint32_t)(long)&otp_read_protect->otp_aes_key[1]; + else + return false; + + /* read error */ + protect_flag = OTP_DEFUALT_WORD_VALUE; + if(true != otp_read_word(protect_addr,&protect_flag,1)) + { + return false; + } + /* already write protect */ + if(OTP_DEFUALT_WORD_VALUE != protect_flag) + { + return true; + } + + /* write protect */ + protect_flag = ~OTP_DEFUALT_WORD_VALUE; + (void)otp_write_word(protect_addr,&protect_flag,1); + protect_flag = OTP_DEFUALT_WORD_VALUE; + if(true != otp_read_word(protect_addr,&protect_flag,1)) + { + return false; + } + if(OTP_DEFUALT_WORD_VALUE != protect_flag) + { + return true; + } + else + { + return false; + } +} + +bool otp_get_func_flag(OTP_FUNC_E eFunc) +{ + uint32_t func_flag = OTP_DEFUALT_WORD_VALUE; + uint32_t func_addr; + + if(eFunc >= OTP_FUNC_MAX) + { + return false; + } + + func_addr = (uint32_t)(long)&otp_func_enable_disable->otp_func_flag[eFunc]; + + if(true != otp_read_word(func_addr, &func_flag,1)) + { + return false; + } + else + { + if(OTP_DEFUALT_WORD_VALUE != func_flag) + { + return true; + } + else + { + return false; + } + } +} + +bool otp_set_func_flag(OTP_FUNC_E eFunc) +{ + uint32_t func_flag = OTP_DEFUALT_WORD_VALUE; + uint32_t func_addr; + + if(eFunc >= OTP_FUNC_MAX) + { + return false; + } + + func_addr = (uint32_t)(long)&otp_func_enable_disable->otp_func_flag[eFunc]; + + if(true != otp_read_word(func_addr,&func_flag,1)) + { + return false; + } + + if(OTP_DEFUALT_WORD_VALUE != func_flag) + { + return true; + } + + func_flag = ~OTP_DEFUALT_WORD_VALUE; + (void)otp_write_word(func_addr,&func_flag,1); + + func_flag = OTP_DEFUALT_WORD_VALUE; + if(true != otp_read_word(func_addr,&func_flag,1)) + { + return false; + } + if(OTP_DEFUALT_WORD_VALUE != func_flag) + { + return true; + } + else + { + return false; + } +} + +void otp_select_key(OTP_SEL_CRYPTER_E crypter, OTP_SEL_KEY_E key) +{ + if(OTP_SEL_AES == crypter) + { + if(key == OTP_SEL_KEY_0) + { + otp_ctrl->aes_sha256_key_sel &= ~(1 << 0); + } + else + { + otp_ctrl->aes_sha256_key_sel |= (1 << 0); + } + } + else if(OTP_SEL_SHA256 ==crypter) + { + if(key == OTP_SEL_KEY_0) + { + otp_ctrl->aes_sha256_key_sel &= ~(1 << 4); + } + else + { + otp_ctrl->aes_sha256_key_sel |= (1 << 4); + } + } + else + { + + } +} + +void otp_reset(void) +{ + if(true == otp_get_status(OTP_BYPASS_STATUS)) + { + return; + } + otp_ctrl->mode = OTP_MODE_STANDBY_DEP; + while(otp_ctrl->mode_status != OTP_MODE_STANDBY_DEP) + { + } + otp_ctrl->enable = OTP_DISABLE; + usleep(100); + sysctl_reset(SYSCTL_RESET_OTP); +} diff --git a/package/k510_evb_test/src/bsp/controler/pwm.c b/package/k510_evb_test/src/bsp/controler/pwm.c index 85a1087..a180dbc 100644 --- a/package/k510_evb_test/src/bsp/controler/pwm.c +++ b/package/k510_evb_test/src/bsp/controler/pwm.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include "pwm.h" #include diff --git a/package/k510_evb_test/src/bsp/controler/rtc.c b/package/k510_evb_test/src/bsp/controler/rtc.c index e39a5d6..59c6603 100644 --- a/package/k510_evb_test/src/bsp/controler/rtc.c +++ b/package/k510_evb_test/src/bsp/controler/rtc.c @@ -1,16 +1,26 @@ -/* Copyright 2018 Canaan Inc. +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include diff --git a/package/k510_evb_test/src/bsp/controler/sdio/sd_host_emmc.c b/package/k510_evb_test/src/bsp/controler/sdio/sd_host_emmc.c index 5d52ff7..baccd51 100644 --- a/package/k510_evb_test/src/bsp/controler/sdio/sd_host_emmc.c +++ b/package/k510_evb_test/src/bsp/controler/sdio/sd_host_emmc.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include #include "sd_host_emmc.h" #include "sd_host_global.h" diff --git a/package/k510_evb_test/src/bsp/controler/sdio/sd_host_global.c b/package/k510_evb_test/src/bsp/controler/sdio/sd_host_global.c index 596b72d..a34da00 100644 --- a/package/k510_evb_test/src/bsp/controler/sdio/sd_host_global.c +++ b/package/k510_evb_test/src/bsp/controler/sdio/sd_host_global.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include #include #include "sd_host_global.h" diff --git a/package/k510_evb_test/src/bsp/controler/sdio/sd_host_sdio.c b/package/k510_evb_test/src/bsp/controler/sdio/sd_host_sdio.c index 760b75a..6f6338b 100644 --- a/package/k510_evb_test/src/bsp/controler/sdio/sd_host_sdio.c +++ b/package/k510_evb_test/src/bsp/controler/sdio/sd_host_sdio.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include #include "sd_host_sdio.h" #include "sd_host_global.h" diff --git a/package/k510_evb_test/src/bsp/controler/sha256.c b/package/k510_evb_test/src/bsp/controler/sha256.c index fda5fe3..811e405 100644 --- a/package/k510_evb_test/src/bsp/controler/sha256.c +++ b/package/k510_evb_test/src/bsp/controler/sha256.c @@ -1,133 +1,157 @@ -#include -#include "platform.h" -#include "sha256.h" - -#define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) -#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) -#define BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | (ROTL((x), 8) & 0x00ff00ffL)) -#define BYTESWAP64(x) byteswap64(x) - -volatile sha256_t *const sha256 = (volatile sha256_t *)SHA_BASE_ADDR; -static dma_ch_t channel = DMA_CHANNEL_0; -static peri_dma_cfg_t sha_dma_cfg = { - .dev_sel = SHA_TX, - .ch_priority = 1, - .mode = LINE_MODE, - .src_type = CONTINUOUS_ADDRESS, - .buswidth = FOUR_BYTE, - .dev_burst_len = 0, - .line_size = 64, - .dev_timeout = 0xFFF -}; - -static const uint8_t padding[64] = - { - 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - -static inline uint64_t byteswap64(uint64_t x) -{ - uint32_t a = (uint32_t)(x >> 32); - uint32_t b = (uint32_t)x; - return ((uint64_t)BYTESWAP(b) << 32) | (uint64_t)BYTESWAP(a); -} - -void sha256_init(sha256_context_t *context, size_t input_len, sha256_cfg_t* cfg) -{ - sha256->sha_num_reg.sha_data_th = (uint32_t)((input_len + SHA256_BLOCK_LEN + 8) / SHA256_BLOCK_LEN); - sha256->sha_function_reg_0.sha_endian = SHA256_BIG_ENDIAN; - if (cfg->init_hash) { - for (int i = 0; i < 8; ++i) { - sha256->init_hash[i] = *(cfg->init_hash + i); - } - } - sha256->sha_function_reg_0.sha_otp_en = cfg->opt_hash_en ? 1 : 0; - sha256->double_sha = cfg->double_sha_en ? 1 : 0; - sha256->sha_function_reg_1.dma_en = cfg->dma_en ? 1 : 0; - sha256->sha_function_reg_0.sha_en = ENABLE_SHA; - if (cfg->dma_en) { - channel = cfg->dma_ch; - sha_dma_cfg.channel = cfg->dma_ch; - sha_dma_cfg.daddr = (void*)(&sha256->sha_data_in0); - } - context->total_len = 0L; - context->buffer_len = 0L; -} - -void sha256_update(sha256_context_t *context, const void *input, size_t input_len) -{ - const uint8_t *data = input; - size_t buffer_bytes_left; - size_t bytes_to_copy; - uint32_t i; - - while(input_len) - { - buffer_bytes_left = SHA256_BLOCK_LEN - context->buffer_len; - bytes_to_copy = buffer_bytes_left; - if(bytes_to_copy > input_len) - bytes_to_copy = input_len; - memcpy(&context->buffer.bytes[context->buffer_len], data, bytes_to_copy); - context->total_len += bytes_to_copy * 8L; - context->buffer_len += bytes_to_copy; - data += bytes_to_copy; - input_len -= bytes_to_copy; - if(context->buffer_len == SHA256_BLOCK_LEN) - { - if (sha256->sha_function_reg_1.dma_en) { - sha_dma_cfg.saddr = (void*)(&context->buffer.words[0]); - peri_dma_config(&sha_dma_cfg); - peri_dma_start(channel); - peri_dma_wait_done_or_timeout(channel); - } else { - for(i = 0; i < 16; i++) - { - while(sha256->sha_function_reg_1.fifo_in_full) - ; - sha256->sha_data_in0 = context->buffer.words[i]; - } - } - context->buffer_len = 0L; - } - } -} - -void sha256_final(sha256_context_t *context, uint8_t *output) -{ - size_t bytes_to_pad; - size_t length_pad; - uint32_t i; - - bytes_to_pad = 120L - context->buffer_len; - if(bytes_to_pad > 64L) - bytes_to_pad -= 64L; - length_pad = BYTESWAP64(context->total_len); - sha256_update(context, padding, bytes_to_pad); - sha256_update(context, &length_pad, 8L); - if(!sha256->double_sha) - while(!(sha256->sha_function_reg_0.sha_en)); - else - while(!(sha256->double_sha)); - if(output) - { - for(i = 0; i < SHA256_HASH_WORDS; i++) - { - *((uint32_t *)output) = sha256->sha_result[SHA256_HASH_WORDS - i - 1]; - output += 4; - } - } -} - -void sha256_hard_calculate(const uint8_t* input, size_t input_len, uint8_t* output, sha256_cfg_t* cfg) -{ - sha256_context_t sha; - sha256_init(&sha, input_len, cfg); - sha256_update(&sha, input, input_len); - sha256_final(&sha, output); -} +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include +#include "platform.h" +#include "sha256.h" + +#define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) +#define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) +#define BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | (ROTL((x), 8) & 0x00ff00ffL)) +#define BYTESWAP64(x) byteswap64(x) + +volatile sha256_t *const sha256 = (volatile sha256_t *)SHA_BASE_ADDR; +static dma_ch_t channel = DMA_CHANNEL_0; +static peri_dma_cfg_t sha_dma_cfg = { + .dev_sel = SHA_TX, + .ch_priority = 1, + .mode = LINE_MODE, + .src_type = CONTINUOUS_ADDRESS, + .buswidth = FOUR_BYTE, + .dev_burst_len = 0, + .line_size = 64, + .dev_timeout = 0xFFF +}; + +static const uint8_t padding[64] = + { + 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + +static inline uint64_t byteswap64(uint64_t x) +{ + uint32_t a = (uint32_t)(x >> 32); + uint32_t b = (uint32_t)x; + return ((uint64_t)BYTESWAP(b) << 32) | (uint64_t)BYTESWAP(a); +} + +void sha256_init(sha256_context_t *context, size_t input_len, sha256_cfg_t* cfg) +{ + sha256->sha_num_reg.sha_data_th = (uint32_t)((input_len + SHA256_BLOCK_LEN + 8) / SHA256_BLOCK_LEN); + sha256->sha_function_reg_0.sha_endian = SHA256_BIG_ENDIAN; + if (cfg->init_hash) { + for (int i = 0; i < 8; ++i) { + sha256->init_hash[i] = *(cfg->init_hash + i); + } + } + sha256->sha_function_reg_0.sha_otp_en = cfg->opt_hash_en ? 1 : 0; + sha256->double_sha = cfg->double_sha_en ? 1 : 0; + sha256->sha_function_reg_1.dma_en = cfg->dma_en ? 1 : 0; + sha256->sha_function_reg_0.sha_en = ENABLE_SHA; + if (cfg->dma_en) { + channel = cfg->dma_ch; + sha_dma_cfg.channel = cfg->dma_ch; + sha_dma_cfg.daddr = (void*)(&sha256->sha_data_in0); + } + context->total_len = 0L; + context->buffer_len = 0L; +} + +void sha256_update(sha256_context_t *context, const void *input, size_t input_len) +{ + const uint8_t *data = input; + size_t buffer_bytes_left; + size_t bytes_to_copy; + uint32_t i; + + while(input_len) + { + buffer_bytes_left = SHA256_BLOCK_LEN - context->buffer_len; + bytes_to_copy = buffer_bytes_left; + if(bytes_to_copy > input_len) + bytes_to_copy = input_len; + memcpy(&context->buffer.bytes[context->buffer_len], data, bytes_to_copy); + context->total_len += bytes_to_copy * 8L; + context->buffer_len += bytes_to_copy; + data += bytes_to_copy; + input_len -= bytes_to_copy; + if(context->buffer_len == SHA256_BLOCK_LEN) + { + if (sha256->sha_function_reg_1.dma_en) { + sha_dma_cfg.saddr = (void*)(&context->buffer.words[0]); + peri_dma_config(&sha_dma_cfg); + peri_dma_start(channel); + peri_dma_wait_done_or_timeout(channel); + } else { + for(i = 0; i < 16; i++) + { + while(sha256->sha_function_reg_1.fifo_in_full) + ; + sha256->sha_data_in0 = context->buffer.words[i]; + } + } + context->buffer_len = 0L; + } + } +} + +void sha256_final(sha256_context_t *context, uint8_t *output) +{ + size_t bytes_to_pad; + size_t length_pad; + uint32_t i; + + bytes_to_pad = 120L - context->buffer_len; + if(bytes_to_pad > 64L) + bytes_to_pad -= 64L; + length_pad = BYTESWAP64(context->total_len); + sha256_update(context, padding, bytes_to_pad); + sha256_update(context, &length_pad, 8L); + if(!sha256->double_sha) + while(!(sha256->sha_function_reg_0.sha_en)); + else + while(!(sha256->double_sha)); + if(output) + { + for(i = 0; i < SHA256_HASH_WORDS; i++) + { + *((uint32_t *)output) = sha256->sha_result[SHA256_HASH_WORDS - i - 1]; + output += 4; + } + } +} + +void sha256_hard_calculate(const uint8_t* input, size_t input_len, uint8_t* output, sha256_cfg_t* cfg) +{ + sha256_context_t sha; + sha256_init(&sha, input_len, cfg); + sha256_update(&sha, input, input_len); + sha256_final(&sha, output); +} diff --git a/package/k510_evb_test/src/bsp/controler/spi.c b/package/k510_evb_test/src/bsp/controler/spi.c index 9ef6374..b621edb 100644 --- a/package/k510_evb_test/src/bsp/controler/spi.c +++ b/package/k510_evb_test/src/bsp/controler/spi.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include #include #include "platform.h" diff --git a/package/k510_evb_test/src/bsp/controler/sysctl_boot.c b/package/k510_evb_test/src/bsp/controler/sysctl_boot.c index 118d869..f32cae5 100644 --- a/package/k510_evb_test/src/bsp/controler/sysctl_boot.c +++ b/package/k510_evb_test/src/bsp/controler/sysctl_boot.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include "platform.h" #include "sysctl_boot.h" diff --git a/package/k510_evb_test/src/bsp/controler/sysctl_clk.c b/package/k510_evb_test/src/bsp/controler/sysctl_clk.c index 48bb53a..8c134a9 100644 --- a/package/k510_evb_test/src/bsp/controler/sysctl_clk.c +++ b/package/k510_evb_test/src/bsp/controler/sysctl_clk.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include #include #include "platform.h" diff --git a/package/k510_evb_test/src/bsp/controler/sysctl_pwr.c b/package/k510_evb_test/src/bsp/controler/sysctl_pwr.c index c205383..0d0f964 100644 --- a/package/k510_evb_test/src/bsp/controler/sysctl_pwr.c +++ b/package/k510_evb_test/src/bsp/controler/sysctl_pwr.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include #include "sleep.h" #include "sysctl_pwr.h" diff --git a/package/k510_evb_test/src/bsp/controler/sysctl_rst.c b/package/k510_evb_test/src/bsp/controler/sysctl_rst.c index adcd876..12d0cfb 100644 --- a/package/k510_evb_test/src/bsp/controler/sysctl_rst.c +++ b/package/k510_evb_test/src/bsp/controler/sysctl_rst.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include #include "platform.h" #include "sleep.h" diff --git a/package/k510_evb_test/src/bsp/controler/timer.c b/package/k510_evb_test/src/bsp/controler/timer.c index 533674e..8f96290 100644 --- a/package/k510_evb_test/src/bsp/controler/timer.c +++ b/package/k510_evb_test/src/bsp/controler/timer.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include #include "timer.h" #include diff --git a/package/k510_evb_test/src/bsp/controler/trng.c b/package/k510_evb_test/src/bsp/controler/trng.c index a4d9efa..ccbd805 100644 --- a/package/k510_evb_test/src/bsp/controler/trng.c +++ b/package/k510_evb_test/src/bsp/controler/trng.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include #include "platform.h" #include "trng.h" diff --git a/package/k510_evb_test/src/bsp/controler/tsensor.c b/package/k510_evb_test/src/bsp/controler/tsensor.c index 71d2894..a6ddc4e 100644 --- a/package/k510_evb_test/src/bsp/controler/tsensor.c +++ b/package/k510_evb_test/src/bsp/controler/tsensor.c @@ -1,47 +1,71 @@ -#include -#include - -volatile tsensor_t* const tsensor = (volatile tsensor_t* const)(0x970E0300UL); - -int tsenor_set_ratio(uint8_t ratio) -{ - if (ratio > 99) - { - k510_loge("Tsensor ratio large than 99\n"); - return -1; - } - tsensor->clk_ratio = ratio; - return 0; -} - -uint8_t tsensor_get_ratio(void) -{ - return tsensor->clk_ratio & 0xFF; -} - -int tsensor_set_work_mode(tsensor_work_mode_t mode) -{ - if (mode == TSENSOR_SINGLE_MODE) - { - tsensor->wr_an = 0x80800000; - } - else if (mode == TSENSOR_CONTINUE_MODE) - { - tsensor->wr_an = 0x80000000; - } - else - { - return -1; - } - - return 0; -} - -void tsensor_get_temperature(tsensor_temperature_t *temp) -{ - temp->status = tsensor->status; - for (uint32_t i = 0; i < 5; i++) - { - temp->temperature[i] = ((double)tsensor_get_data(i) / TSENSOR_PARAM) * TSENSOR_Y - TSENSOR_X; - } -} +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include +#include + +volatile tsensor_t* const tsensor = (volatile tsensor_t* const)(0x970E0300UL); + +int tsenor_set_ratio(uint8_t ratio) +{ + if (ratio > 99) + { + k510_loge("Tsensor ratio large than 99\n"); + return -1; + } + tsensor->clk_ratio = ratio; + return 0; +} + +uint8_t tsensor_get_ratio(void) +{ + return tsensor->clk_ratio & 0xFF; +} + +int tsensor_set_work_mode(tsensor_work_mode_t mode) +{ + if (mode == TSENSOR_SINGLE_MODE) + { + tsensor->wr_an = 0x80800000; + } + else if (mode == TSENSOR_CONTINUE_MODE) + { + tsensor->wr_an = 0x80000000; + } + else + { + return -1; + } + + return 0; +} + +void tsensor_get_temperature(tsensor_temperature_t *temp) +{ + temp->status = tsensor->status; + for (uint32_t i = 0; i < 5; i++) + { + temp->temperature[i] = ((double)tsensor_get_data(i) / TSENSOR_PARAM) * TSENSOR_Y - TSENSOR_X; + } +} diff --git a/package/k510_evb_test/src/bsp/controler/uart.c b/package/k510_evb_test/src/bsp/controler/uart.c index 2472808..9a97a01 100644 --- a/package/k510_evb_test/src/bsp/controler/uart.c +++ b/package/k510_evb_test/src/bsp/controler/uart.c @@ -1,3 +1,27 @@ +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ #include "platform.h" #include "k510.h" #include "uart.h" diff --git a/package/k510_evb_test/src/bsp/controler/vad.c b/package/k510_evb_test/src/bsp/controler/vad.c index b7aadc0..5da5c12 100644 --- a/package/k510_evb_test/src/bsp/controler/vad.c +++ b/package/k510_evb_test/src/bsp/controler/vad.c @@ -1,16 +1,26 @@ -/* Copyright 2018 Canaan Inc. +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include diff --git a/package/k510_evb_test/src/bsp/controler/video/bt1120/bt1120_drv.c b/package/k510_evb_test/src/bsp/controler/video/bt1120/bt1120_drv.c index 75b6bc9..c05c15d 100644 --- a/package/k510_evb_test/src/bsp/controler/video/bt1120/bt1120_drv.c +++ b/package/k510_evb_test/src/bsp/controler/video/bt1120/bt1120_drv.c @@ -1,126 +1,134 @@ -/****************************************************************************** - - Copyright (C), 2020, CANAAN Tech. Co., Ltd. - - ****************************************************************************** - File Name : bt1120_drv.c - Version : Initial Draft - Author : - Created : - Description : bt1120 driver interface - History : - 1.Date : - Author : - Modification: Created file - -******************************************************************************/ - -#include "video/isp_com.h" -#include "video/bt1120/bt1120_reg.h" -#include "video/bt1120/bt1120_drv.h" - -extern unsigned int *reg_bt1120_wrap_base; -#define BT1120_DRV_REG_CTX(pstCtx) pstCtx = (S_BT1120_REGS_TYPE *)(reg_bt1120_wrap_base) -/* -* -*/ -static int BT1120_DRV_SetSize(BT1120_WRAP_CTL_S *pstBt1120Wrap) -{ - BT1120_CHECK_POINTER(pstBt1120Wrap); - - S_BT1120_REGS_TYPE *apstBt1120Reg = NULL; - BT1120_DRV_REG_CTX(apstBt1120Reg); - - U_BT_CTRL_VBIT stCtrlVbit; - stCtrlVbit.u32 = 0; - stCtrlVbit.bits.bt_vbit_start = pstBt1120Wrap->stCtrlVbit.bt_vbit_start; - stCtrlVbit.bits.bt_vbit_end = pstBt1120Wrap->stCtrlVbit.bt_vbit_end; - BT1120_DRV_SET_CtlVBit(apstBt1120Reg,stCtrlVbit.u32); - - U_BT_CTRL_SAV stCtrlSav; - stCtrlSav.u32 = 0; - stCtrlSav.bits.bt_sav_start = pstBt1120Wrap->bt_sav_start; - BT1120_DRV_SET_CtlSav(apstBt1120Reg, stCtrlSav.u32); - - U_BT_IMAGE_SIZE_TOTAL stSizeTotal; - stSizeTotal.u32 = 0; - stSizeTotal.bits.img_hsize_total= pstBt1120Wrap->stSizeTotal.img_hsize_total; - stSizeTotal.bits.img_vsize_total= pstBt1120Wrap->stSizeTotal.img_vsize_total; - BT1120_DRV_SET_ImgSizeTotal(apstBt1120Reg,stSizeTotal.u32); - - U_BT_IMAGE_SIZE_ACTIVE stSizeActive; - stSizeActive.u32 = 0; - stSizeActive.bits.img_hsize_active= pstBt1120Wrap->stSizeActive.img_hsize_active; - stSizeActive.bits.img_vsize_active= pstBt1120Wrap->stSizeActive.img_vsize_active; - BT1120_DRV_SET_ImgSizeActive(apstBt1120Reg,stSizeActive.u32); - return 0; -} -/* -* -*/ -static int BT1120_DRV_setCtl(BT1120_WRAP_CTL_S *pstBt1120Wrap) -{ - BT1120_CHECK_POINTER(pstBt1120Wrap); - - S_BT1120_REGS_TYPE *apstBt1120Reg = NULL; - BT1120_DRV_REG_CTX(apstBt1120Reg); - - U_BT_IN_SEL stInSel; - stInSel.u32 = 0; - stInSel.bits.bt_in_sel = pstBt1120Wrap->bt_in_sel; - BT1120_DRV_SET_InSel(apstBt1120Reg,stInSel.u32); - - U_BT_CTRL_MODE stCtrlMode; - stCtrlMode.u32 = 0; - // stCtrlMode.bits.bt_en = bt1120WrapInfo->stCtrlMode.bt_en; - stCtrlMode.bits.crc_init_val = pstBt1120Wrap->stCtrlMode.crc_init_val; - stCtrlMode.bits.crc_order = pstBt1120Wrap->stCtrlMode.crc_order; - stCtrlMode.bits.yc_reorder = pstBt1120Wrap->stCtrlMode.yc_reorder; - BT1120_DRV_SET_CtlMode(apstBt1120Reg,stCtrlMode.u32); - - //BT1120_DRV_SetSize(pstBt1120Wrap); - - U_BT_CONFIG_DONE stConfigDone; - stConfigDone.u32 = 0; - // stConfigDone.bits.config_done = bt1120WrapInfo->stConfigDone.config_done; - stConfigDone.bits.wp_clr = pstBt1120Wrap->stConfigDone.wp_clr; - BT1120_DRV_SET_ConfigDone(apstBt1120Reg,stConfigDone.u32); - return 0; -} -/* -* -*/ -static int BT1120_DRV_EnBt1120(unsigned int enable,unsigned int config_done) -{ - S_BT1120_REGS_TYPE *apstBt1120Reg = NULL; - BT1120_DRV_REG_CTX(apstBt1120Reg); - - U_BT_CTRL_MODE stCtrlMode; - stCtrlMode.u32 = BT1120_DRV_GET_CtlMode(apstBt1120Reg); - stCtrlMode.bits.bt_en = enable; - BT1120_DRV_SET_CtlMode(apstBt1120Reg,stCtrlMode.u32); - - U_BT_CONFIG_DONE stConfigDone; - stConfigDone.u32 = BT1120_DRV_GET_ConfigDone(apstBt1120Reg); - stConfigDone.bits.config_done = config_done;//1; - BT1120_DRV_SET_ConfigDone(apstBt1120Reg,stConfigDone.u32); - - return 0; -} -/* -* -*/ -int BT1120_DRV_SetBt1120(BT1120_WRAP_CTL_S *pstBt1120Wrap) -{ - BT1120_CHECK_POINTER(pstBt1120Wrap); - - BT1120_DRV_SetSize(pstBt1120Wrap); - - BT1120_DRV_setCtl(pstBt1120Wrap); - - unsigned int enable = pstBt1120Wrap->stCtrlMode.bt_en; - unsigned int config_done = pstBt1120Wrap->stConfigDone.config_done; - BT1120_DRV_EnBt1120(enable,config_done); - - return 0; -} +/* Copyright (c) 2022, Canaan Bright Sight Co., Ltd + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "video/isp_com.h" +#include "video/bt1120/bt1120_reg.h" +#include "video/bt1120/bt1120_drv.h" + +extern unsigned int *reg_bt1120_wrap_base; +#define BT1120_DRV_REG_CTX(pstCtx) pstCtx = (S_BT1120_REGS_TYPE *)(reg_bt1120_wrap_base) +/* +* +*/ +static int BT1120_DRV_SetSize(BT1120_WRAP_CTL_S *pstBt1120Wrap) +{ + BT1120_CHECK_POINTER(pstBt1120Wrap); + + S_BT1120_REGS_TYPE *apstBt1120Reg = NULL; + BT1120_DRV_REG_CTX(apstBt1120Reg); + + U_BT_CTRL_VBIT stCtrlVbit; + stCtrlVbit.u32 = 0; + stCtrlVbit.bits.bt_vbit_start = pstBt1120Wrap->stCtrlVbit.bt_vbit_start; + stCtrlVbit.bits.bt_vbit_end = pstBt1120Wrap->stCtrlVbit.bt_vbit_end; + BT1120_DRV_SET_CtlVBit(apstBt1120Reg,stCtrlVbit.u32); + + U_BT_CTRL_SAV stCtrlSav; + stCtrlSav.u32 = 0; + stCtrlSav.bits.bt_sav_start = pstBt1120Wrap->bt_sav_start; + BT1120_DRV_SET_CtlSav(apstBt1120Reg, stCtrlSav.u32); + + U_BT_IMAGE_SIZE_TOTAL stSizeTotal; + stSizeTotal.u32 = 0; + stSizeTotal.bits.img_hsize_total= pstBt1120Wrap->stSizeTotal.img_hsize_total; + stSizeTotal.bits.img_vsize_total= pstBt1120Wrap->stSizeTotal.img_vsize_total; + BT1120_DRV_SET_ImgSizeTotal(apstBt1120Reg,stSizeTotal.u32); + + U_BT_IMAGE_SIZE_ACTIVE stSizeActive; + stSizeActive.u32 = 0; + stSizeActive.bits.img_hsize_active= pstBt1120Wrap->stSizeActive.img_hsize_active; + stSizeActive.bits.img_vsize_active= pstBt1120Wrap->stSizeActive.img_vsize_active; + BT1120_DRV_SET_ImgSizeActive(apstBt1120Reg,stSizeActive.u32); + return 0; +} +/* +* +*/ +static int BT1120_DRV_setCtl(BT1120_WRAP_CTL_S *pstBt1120Wrap) +{ + BT1120_CHECK_POINTER(pstBt1120Wrap); + + S_BT1120_REGS_TYPE *apstBt1120Reg = NULL; + BT1120_DRV_REG_CTX(apstBt1120Reg); + + U_BT_IN_SEL stInSel; + stInSel.u32 = 0; + stInSel.bits.bt_in_sel = pstBt1120Wrap->bt_in_sel; + BT1120_DRV_SET_InSel(apstBt1120Reg,stInSel.u32); + + U_BT_CTRL_MODE stCtrlMode; + stCtrlMode.u32 = 0; + // stCtrlMode.bits.bt_en = bt1120WrapInfo->stCtrlMode.bt_en; + stCtrlMode.bits.crc_init_val = pstBt1120Wrap->stCtrlMode.crc_init_val; + stCtrlMode.bits.crc_order = pstBt1120Wrap->stCtrlMode.crc_order; + stCtrlMode.bits.yc_reorder = pstBt1120Wrap->stCtrlMode.yc_reorder; + BT1120_DRV_SET_CtlMode(apstBt1120Reg,stCtrlMode.u32); + + //BT1120_DRV_SetSize(pstBt1120Wrap); + + U_BT_CONFIG_DONE stConfigDone; + stConfigDone.u32 = 0; + // stConfigDone.bits.config_done = bt1120WrapInfo->stConfigDone.config_done; + stConfigDone.bits.wp_clr = pstBt1120Wrap->stConfigDone.wp_clr; + BT1120_DRV_SET_ConfigDone(apstBt1120Reg,stConfigDone.u32); + return 0; +} +/* +* +*/ +static int BT1120_DRV_EnBt1120(unsigned int enable,unsigned int config_done) +{ + S_BT1120_REGS_TYPE *apstBt1120Reg = NULL; + BT1120_DRV_REG_CTX(apstBt1120Reg); + + U_BT_CTRL_MODE stCtrlMode; + stCtrlMode.u32 = BT1120_DRV_GET_CtlMode(apstBt1120Reg); + stCtrlMode.bits.bt_en = enable; + BT1120_DRV_SET_CtlMode(apstBt1120Reg,stCtrlMode.u32); + + U_BT_CONFIG_DONE stConfigDone; + stConfigDone.u32 = BT1120_DRV_GET_ConfigDone(apstBt1120Reg); + stConfigDone.bits.config_done = config_done;//1; + BT1120_DRV_SET_ConfigDone(apstBt1120Reg,stConfigDone.u32); + + return 0; +} +/* +* +*/ +int BT1120_DRV_SetBt1120(BT1120_WRAP_CTL_S *pstBt1120Wrap) +{ + BT1120_CHECK_POINTER(pstBt1120Wrap); + + BT1120_DRV_SetSize(pstBt1120Wrap); + + BT1120_DRV_setCtl(pstBt1120Wrap); + + unsigned int enable = pstBt1120Wrap->stCtrlMode.bt_en; + unsigned int config_done = pstBt1120Wrap->stConfigDone.config_done; + BT1120_DRV_EnBt1120(enable,config_done); + + return 0; +} diff --git a/package/k510_evb_test/src/bsp/controler/video/bt1120/bt1120_reg.c b/package/k510_evb_test/src/bsp/controler/video/bt1120/bt1120_reg.c index 55db3e2..9942cfc 100644 --- a/package/k510_evb_test/src/bsp/controler/video/bt1120/bt1120_reg.c +++ b/package/k510_evb_test/src/bsp/controler/video/bt1120/bt1120_reg.c @@ -1,137 +1,145 @@ -/****************************************************************************** - - Copyright (C), 2020, CANAAN Tech. Co., Ltd. - - ****************************************************************************** - File Name : bt1120_reg.c - Version : Initial Draft - Author : - Created : - Description : bt1120 driver interface - History : - 1.Date : - Author : - Modification: Created file - -******************************************************************************/ -//#include "video/bt1120/bt1120.h" -#include