Files
2026-08-24 10:03:24 +08:00

172 lines
3.7 KiB
C

/*
* Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved.
*
* SPDX-License-Identifier: MIT
*/
#ifndef __HW_CRYPTO_H__
#define __HW_CRYPTO_H__
//#include "secure_apb.h"
#include "register.h"
#include <stdint.h>
#include <stddef.h>
#include "interrupt.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __unused
#define __unused __maybe_unused
#endif
#define AML_CRYPTO_DEBUG (0)
#define AML_CRYPTO_TEST (0)
#define DMA_64_BIT (0)
#define cryptCRYPTO_THREAD_IDX (4)
#define configCryptoDmaUseIsr (0)
#define DMA_TIME_OUT 10000
#define UNSUPPORT_ADDR_START 0x10000000
#define UNSUPPORT_ADDR_END 0x10008000
#if AML_CRYPTO_TEST
#define SZ 32
#define MAGIC 0x87
#define TEST_DDR_IN 0x1080000
#define TEST_DDR_OUT 0x1090000
#endif
#if defined(BOARD_A1) || defined(BOARD_C1) || defined(BOARD_C2)
#if cryptCRYPTO_THREAD_IDX >= 4
#error "only 4 threads in A1"
#endif
#endif
#define DMA_T0 DMA_CORE_DSC_0
#define DMA_T1 DMA_CORE_DSC_1
#define DMA_T2 DMA_CORE_DSC_2
#define DMA_T3 DMA_CORE_DSC_3
#define DMA_T4 DMA_CORE_DSC_4
#define DMA_T5 DMA_CORE_DSC_5
#define DMA_STS0 DMA_CORE_IRQ_STS_0
#define DMA_STS1 DMA_CORE_IRQ_STS_1
#define DMA_STS2 DMA_CORE_IRQ_STS_2
#define DMA_STS3 DMA_CORE_IRQ_STS_3
#define DMA_STS4 DMA_CORE_IRQ_STS_4
#define DMA_STS5 DMA_CORE_IRQ_STS_5
#define cryptIRQ_CRYPTO_DMA_THREAD_X \
(IRQ_DMA_THREAD0 + cryptCRYPTO_THREAD_IDX)
#define cryptDMA_THREAD_REG(thread) (DMA_T0 + (thread << 2))
#define cryptDMA_STATUS_REG(thread) (DMA_STS0 + (thread << 2))
#define cryptDMA_KEY_IV_LEN (0x30)
enum aml_crypt_mode {
AML_ENCRYPT = 0,
AML_DECRYPT = 1
};
enum aml_opmode {
MODE_ECB = 0,
MODE_CBC = 1,
MODE_CTR = 2
};
#define aml_crypt_mode_t enum aml_crypt_mode
#define aml_opmode_t enum aml_opmode
#define cryptAES_KEYSIZE_128 16
#define cryptAES_KEYSIZE_192 24
#define cryptAES_KEYSIZE_256 32
#define cryptAES_BLOCK_SIZE 16
/* definitions for dma */
/* In block, which is 512 bytes */
#define cryptMAX_BLOCK_TRANSFER (0x1fe00)
#define cryptDMA_BLOCK_SIZE (0x200)
#define cryptDMA_BLOCK_SIZE_SHIFT (9)
#define cryptMAX_BLOCK_TRANSFER_LEN \
(cryptMAX_BLOCK_TRANSFER * cryptDMA_BLOCK_SIZE)
#define cryptKEY_IV_LEN (48)
#define MAX_DESCRIPTORS (2)
#define MAX_BYTES_TRANSFER (67239423) /* (2^17 - 1) * 512 + (2^17 - 1) */
/* operation mode of DMA */
#define cryptOP_MODE_SHA 0
#define cryptOP_MODE_HMAC_I 1
#define cryptOP_MODE_HMAC_O 2
/* modes of DMA */
#define cryptDMA_MODE_DMA 0x0
#define cryptDMA_MODE_KEY 0x1
#define cryptDMA_MODE_MEMSET 0x2
/* 0x3 cryptDMA_MODE skipped */
/* 0x4 cryptDMA_MODE skipped */
#define cryptDMA_MODE_SHA1 0x5
#define cryptDMA_MODE_SHA256 0x6
#define cryptDMA_MODE_SHA224 0x7
#define cryptDMA_MODE_AES128 0x8
#define cryptDMA_MODE_AES192 0x9
#define cryptDMA_MODE_AES256 0xa
/* 0xb cryptDMA_MODE skipped */
#define cryptDMA_MODE_DES 0xc
/* 0xd cryptDMA_MODE skipped */
#define cryptDMA_MODE_TDES_2K 0xe
#define cryptDMA_MODE_TDES_3K 0xf
#define cryptoDMA_STS_ERROR (0x02)
#define cryptoDMA_STS_ERROR_REASON (0xfff0)
#pragma pack(1)
/* Main Descriptor */
struct dma_dsc {
union {
uint32_t d32;
struct {
unsigned length:17;
unsigned irq:1;
unsigned eoc:1;
unsigned loop:1;
unsigned mode:4;
unsigned begin:1;
unsigned end:1;
unsigned op_mode:2;
unsigned enc_sha_only:1;
unsigned block:1;
unsigned link_error:1;
unsigned owner:1;
} b;
} dsc_cfg;
#if DMA_64_BIT
uint64_t src_addr;
uint64_t tgt_addr;
#else
uint32_t src_addr;
uint32_t tgt_addr;
#endif
};
#pragma pack(push, 1)
struct vdma_info {
uint32_t type;
uint32_t size;
void *src;
void *dest;
};
#pragma pack(pop)
void vVDMAInit(void);
int xVDMATransfer(struct vdma_info *info);
#if AML_CRYPTO_TEST
void xDmaTest(uint32_t dir);
#endif
#ifdef __cplusplus
}
#endif
#endif