162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * amlogic.h - hardware cryptographic offloader for Amlogic SoC 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2018-2019 Corentin LABBE <clabbe@baylibre.com> 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci#include <crypto/aes.h> 862306a36Sopenharmony_ci#include <crypto/engine.h> 962306a36Sopenharmony_ci#include <crypto/skcipher.h> 1062306a36Sopenharmony_ci#include <linux/debugfs.h> 1162306a36Sopenharmony_ci#include <linux/crypto.h> 1262306a36Sopenharmony_ci#include <linux/scatterlist.h> 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#define MODE_KEY 1 1562306a36Sopenharmony_ci#define MODE_AES_128 0x8 1662306a36Sopenharmony_ci#define MODE_AES_192 0x9 1762306a36Sopenharmony_ci#define MODE_AES_256 0xa 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#define MESON_DECRYPT 0 2062306a36Sopenharmony_ci#define MESON_ENCRYPT 1 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci#define MESON_OPMODE_ECB 0 2362306a36Sopenharmony_ci#define MESON_OPMODE_CBC 1 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci#define MAXFLOW 2 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ci#define MAXDESC 64 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci#define DESC_LAST BIT(18) 3062306a36Sopenharmony_ci#define DESC_ENCRYPTION BIT(28) 3162306a36Sopenharmony_ci#define DESC_OWN BIT(31) 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci/* 3462306a36Sopenharmony_ci * struct meson_desc - Descriptor for DMA operations 3562306a36Sopenharmony_ci * Note that without datasheet, some are unknown 3662306a36Sopenharmony_ci * @t_status: Descriptor of the cipher operation (see description below) 3762306a36Sopenharmony_ci * @t_src: Physical address of data to read 3862306a36Sopenharmony_ci * @t_dst: Physical address of data to write 3962306a36Sopenharmony_ci * t_status is segmented like this: 4062306a36Sopenharmony_ci * @len: 0-16 length of data to operate 4162306a36Sopenharmony_ci * @irq: 17 Ignored by hardware 4262306a36Sopenharmony_ci * @eoc: 18 End means the descriptor is the last 4362306a36Sopenharmony_ci * @loop: 19 Unknown 4462306a36Sopenharmony_ci * @mode: 20-23 Type of algorithm (AES, SHA) 4562306a36Sopenharmony_ci * @begin: 24 Unknown 4662306a36Sopenharmony_ci * @end: 25 Unknown 4762306a36Sopenharmony_ci * @op_mode: 26-27 Blockmode (CBC, ECB) 4862306a36Sopenharmony_ci * @enc: 28 0 means decryption, 1 is for encryption 4962306a36Sopenharmony_ci * @block: 29 Unknown 5062306a36Sopenharmony_ci * @error: 30 Unknown 5162306a36Sopenharmony_ci * @owner: 31 owner of the descriptor, 1 own by HW 5262306a36Sopenharmony_ci */ 5362306a36Sopenharmony_cistruct meson_desc { 5462306a36Sopenharmony_ci __le32 t_status; 5562306a36Sopenharmony_ci __le32 t_src; 5662306a36Sopenharmony_ci __le32 t_dst; 5762306a36Sopenharmony_ci}; 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci/* 6062306a36Sopenharmony_ci * struct meson_flow - Information used by each flow 6162306a36Sopenharmony_ci * @engine: ptr to the crypto_engine for this flow 6262306a36Sopenharmony_ci * @keylen: keylen for this flow operation 6362306a36Sopenharmony_ci * @complete: completion for the current task on this flow 6462306a36Sopenharmony_ci * @status: set to 1 by interrupt if task is done 6562306a36Sopenharmony_ci * @t_phy: Physical address of task 6662306a36Sopenharmony_ci * @tl: pointer to the current ce_task for this flow 6762306a36Sopenharmony_ci * @stat_req: number of request done by this flow 6862306a36Sopenharmony_ci */ 6962306a36Sopenharmony_cistruct meson_flow { 7062306a36Sopenharmony_ci struct crypto_engine *engine; 7162306a36Sopenharmony_ci struct completion complete; 7262306a36Sopenharmony_ci int status; 7362306a36Sopenharmony_ci unsigned int keylen; 7462306a36Sopenharmony_ci dma_addr_t t_phy; 7562306a36Sopenharmony_ci struct meson_desc *tl; 7662306a36Sopenharmony_ci#ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG 7762306a36Sopenharmony_ci unsigned long stat_req; 7862306a36Sopenharmony_ci#endif 7962306a36Sopenharmony_ci}; 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci/* 8262306a36Sopenharmony_ci * struct meson_dev - main container for all this driver information 8362306a36Sopenharmony_ci * @base: base address of amlogic-crypto 8462306a36Sopenharmony_ci * @busclk: bus clock for amlogic-crypto 8562306a36Sopenharmony_ci * @dev: the platform device 8662306a36Sopenharmony_ci * @chanlist: array of all flow 8762306a36Sopenharmony_ci * @flow: flow to use in next request 8862306a36Sopenharmony_ci * @irqs: IRQ numbers for amlogic-crypto 8962306a36Sopenharmony_ci * @dbgfs_dir: Debugfs dentry for statistic directory 9062306a36Sopenharmony_ci * @dbgfs_stats: Debugfs dentry for statistic counters 9162306a36Sopenharmony_ci */ 9262306a36Sopenharmony_cistruct meson_dev { 9362306a36Sopenharmony_ci void __iomem *base; 9462306a36Sopenharmony_ci struct clk *busclk; 9562306a36Sopenharmony_ci struct device *dev; 9662306a36Sopenharmony_ci struct meson_flow *chanlist; 9762306a36Sopenharmony_ci atomic_t flow; 9862306a36Sopenharmony_ci int irqs[MAXFLOW]; 9962306a36Sopenharmony_ci#ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG 10062306a36Sopenharmony_ci struct dentry *dbgfs_dir; 10162306a36Sopenharmony_ci#endif 10262306a36Sopenharmony_ci}; 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci/* 10562306a36Sopenharmony_ci * struct meson_cipher_req_ctx - context for a skcipher request 10662306a36Sopenharmony_ci * @op_dir: direction (encrypt vs decrypt) for this request 10762306a36Sopenharmony_ci * @flow: the flow to use for this request 10862306a36Sopenharmony_ci */ 10962306a36Sopenharmony_cistruct meson_cipher_req_ctx { 11062306a36Sopenharmony_ci u32 op_dir; 11162306a36Sopenharmony_ci int flow; 11262306a36Sopenharmony_ci struct skcipher_request fallback_req; // keep at the end 11362306a36Sopenharmony_ci}; 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci/* 11662306a36Sopenharmony_ci * struct meson_cipher_tfm_ctx - context for a skcipher TFM 11762306a36Sopenharmony_ci * @key: pointer to key data 11862306a36Sopenharmony_ci * @keylen: len of the key 11962306a36Sopenharmony_ci * @keymode: The keymode(type and size of key) associated with this TFM 12062306a36Sopenharmony_ci * @mc: pointer to the private data of driver handling this TFM 12162306a36Sopenharmony_ci * @fallback_tfm: pointer to the fallback TFM 12262306a36Sopenharmony_ci */ 12362306a36Sopenharmony_cistruct meson_cipher_tfm_ctx { 12462306a36Sopenharmony_ci u32 *key; 12562306a36Sopenharmony_ci u32 keylen; 12662306a36Sopenharmony_ci u32 keymode; 12762306a36Sopenharmony_ci struct meson_dev *mc; 12862306a36Sopenharmony_ci struct crypto_skcipher *fallback_tfm; 12962306a36Sopenharmony_ci}; 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci/* 13262306a36Sopenharmony_ci * struct meson_alg_template - crypto_alg template 13362306a36Sopenharmony_ci * @type: the CRYPTO_ALG_TYPE for this template 13462306a36Sopenharmony_ci * @blockmode: the type of block operation 13562306a36Sopenharmony_ci * @mc: pointer to the meson_dev structure associated with this template 13662306a36Sopenharmony_ci * @alg: one of sub struct must be used 13762306a36Sopenharmony_ci * @stat_req: number of request done on this template 13862306a36Sopenharmony_ci * @stat_fb: total of all data len done on this template 13962306a36Sopenharmony_ci */ 14062306a36Sopenharmony_cistruct meson_alg_template { 14162306a36Sopenharmony_ci u32 type; 14262306a36Sopenharmony_ci u32 blockmode; 14362306a36Sopenharmony_ci union { 14462306a36Sopenharmony_ci struct skcipher_engine_alg skcipher; 14562306a36Sopenharmony_ci } alg; 14662306a36Sopenharmony_ci struct meson_dev *mc; 14762306a36Sopenharmony_ci#ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG 14862306a36Sopenharmony_ci unsigned long stat_req; 14962306a36Sopenharmony_ci unsigned long stat_fb; 15062306a36Sopenharmony_ci#endif 15162306a36Sopenharmony_ci}; 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ciint meson_enqueue(struct crypto_async_request *areq, u32 type); 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ciint meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, 15662306a36Sopenharmony_ci unsigned int keylen); 15762306a36Sopenharmony_ciint meson_cipher_init(struct crypto_tfm *tfm); 15862306a36Sopenharmony_civoid meson_cipher_exit(struct crypto_tfm *tfm); 15962306a36Sopenharmony_ciint meson_skdecrypt(struct skcipher_request *areq); 16062306a36Sopenharmony_ciint meson_skencrypt(struct skcipher_request *areq); 16162306a36Sopenharmony_ciint meson_handle_cipher_request(struct crypto_engine *engine, void *areq); 162