18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * amlogic.h - hardware cryptographic offloader for Amlogic SoC
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2018-2019 Corentin LABBE <clabbe@baylibre.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#include <crypto/aes.h>
88c2ecf20Sopenharmony_ci#include <crypto/engine.h>
98c2ecf20Sopenharmony_ci#include <crypto/skcipher.h>
108c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
118c2ecf20Sopenharmony_ci#include <linux/crypto.h>
128c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define MODE_KEY 1
158c2ecf20Sopenharmony_ci#define MODE_AES_128 0x8
168c2ecf20Sopenharmony_ci#define MODE_AES_192 0x9
178c2ecf20Sopenharmony_ci#define MODE_AES_256 0xa
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define MESON_DECRYPT 0
208c2ecf20Sopenharmony_ci#define MESON_ENCRYPT 1
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define MESON_OPMODE_ECB 0
238c2ecf20Sopenharmony_ci#define MESON_OPMODE_CBC 1
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define MAXFLOW 2
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define MAXDESC 64
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define DESC_LAST BIT(18)
308c2ecf20Sopenharmony_ci#define DESC_ENCRYPTION BIT(28)
318c2ecf20Sopenharmony_ci#define DESC_OWN BIT(31)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/*
348c2ecf20Sopenharmony_ci * struct meson_desc - Descriptor for DMA operations
358c2ecf20Sopenharmony_ci * Note that without datasheet, some are unknown
368c2ecf20Sopenharmony_ci * @t_status:	Descriptor of the cipher operation (see description below)
378c2ecf20Sopenharmony_ci * @t_src:	Physical address of data to read
388c2ecf20Sopenharmony_ci * @t_dst:	Physical address of data to write
398c2ecf20Sopenharmony_ci * t_status is segmented like this:
408c2ecf20Sopenharmony_ci * @len:	0-16	length of data to operate
418c2ecf20Sopenharmony_ci * @irq:	17	Ignored by hardware
428c2ecf20Sopenharmony_ci * @eoc:	18	End means the descriptor is the last
438c2ecf20Sopenharmony_ci * @loop:	19	Unknown
448c2ecf20Sopenharmony_ci * @mode:	20-23	Type of algorithm (AES, SHA)
458c2ecf20Sopenharmony_ci * @begin:	24	Unknown
468c2ecf20Sopenharmony_ci * @end:	25	Unknown
478c2ecf20Sopenharmony_ci * @op_mode:	26-27	Blockmode (CBC, ECB)
488c2ecf20Sopenharmony_ci * @enc:	28	0 means decryption, 1 is for encryption
498c2ecf20Sopenharmony_ci * @block:	29	Unknown
508c2ecf20Sopenharmony_ci * @error:	30	Unknown
518c2ecf20Sopenharmony_ci * @owner:	31	owner of the descriptor, 1 own by HW
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_cistruct meson_desc {
548c2ecf20Sopenharmony_ci	__le32 t_status;
558c2ecf20Sopenharmony_ci	__le32 t_src;
568c2ecf20Sopenharmony_ci	__le32 t_dst;
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/*
608c2ecf20Sopenharmony_ci * struct meson_flow - Information used by each flow
618c2ecf20Sopenharmony_ci * @engine:	ptr to the crypto_engine for this flow
628c2ecf20Sopenharmony_ci * @keylen:	keylen for this flow operation
638c2ecf20Sopenharmony_ci * @complete:	completion for the current task on this flow
648c2ecf20Sopenharmony_ci * @status:	set to 1 by interrupt if task is done
658c2ecf20Sopenharmony_ci * @t_phy:	Physical address of task
668c2ecf20Sopenharmony_ci * @tl:		pointer to the current ce_task for this flow
678c2ecf20Sopenharmony_ci * @stat_req:	number of request done by this flow
688c2ecf20Sopenharmony_ci */
698c2ecf20Sopenharmony_cistruct meson_flow {
708c2ecf20Sopenharmony_ci	struct crypto_engine *engine;
718c2ecf20Sopenharmony_ci	struct completion complete;
728c2ecf20Sopenharmony_ci	int status;
738c2ecf20Sopenharmony_ci	unsigned int keylen;
748c2ecf20Sopenharmony_ci	dma_addr_t t_phy;
758c2ecf20Sopenharmony_ci	struct meson_desc *tl;
768c2ecf20Sopenharmony_ci#ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
778c2ecf20Sopenharmony_ci	unsigned long stat_req;
788c2ecf20Sopenharmony_ci#endif
798c2ecf20Sopenharmony_ci};
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci/*
828c2ecf20Sopenharmony_ci * struct meson_dev - main container for all this driver information
838c2ecf20Sopenharmony_ci * @base:	base address of amlogic-crypto
848c2ecf20Sopenharmony_ci * @busclk:	bus clock for amlogic-crypto
858c2ecf20Sopenharmony_ci * @dev:	the platform device
868c2ecf20Sopenharmony_ci * @chanlist:	array of all flow
878c2ecf20Sopenharmony_ci * @flow:	flow to use in next request
888c2ecf20Sopenharmony_ci * @irqs:	IRQ numbers for amlogic-crypto
898c2ecf20Sopenharmony_ci * @dbgfs_dir:	Debugfs dentry for statistic directory
908c2ecf20Sopenharmony_ci * @dbgfs_stats: Debugfs dentry for statistic counters
918c2ecf20Sopenharmony_ci */
928c2ecf20Sopenharmony_cistruct meson_dev {
938c2ecf20Sopenharmony_ci	void __iomem *base;
948c2ecf20Sopenharmony_ci	struct clk *busclk;
958c2ecf20Sopenharmony_ci	struct device *dev;
968c2ecf20Sopenharmony_ci	struct meson_flow *chanlist;
978c2ecf20Sopenharmony_ci	atomic_t flow;
988c2ecf20Sopenharmony_ci	int irqs[MAXFLOW];
998c2ecf20Sopenharmony_ci#ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
1008c2ecf20Sopenharmony_ci	struct dentry *dbgfs_dir;
1018c2ecf20Sopenharmony_ci#endif
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/*
1058c2ecf20Sopenharmony_ci * struct meson_cipher_req_ctx - context for a skcipher request
1068c2ecf20Sopenharmony_ci * @op_dir:	direction (encrypt vs decrypt) for this request
1078c2ecf20Sopenharmony_ci * @flow:	the flow to use for this request
1088c2ecf20Sopenharmony_ci */
1098c2ecf20Sopenharmony_cistruct meson_cipher_req_ctx {
1108c2ecf20Sopenharmony_ci	u32 op_dir;
1118c2ecf20Sopenharmony_ci	int flow;
1128c2ecf20Sopenharmony_ci	struct skcipher_request fallback_req;	// keep at the end
1138c2ecf20Sopenharmony_ci};
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/*
1168c2ecf20Sopenharmony_ci * struct meson_cipher_tfm_ctx - context for a skcipher TFM
1178c2ecf20Sopenharmony_ci * @enginectx:		crypto_engine used by this TFM
1188c2ecf20Sopenharmony_ci * @key:		pointer to key data
1198c2ecf20Sopenharmony_ci * @keylen:		len of the key
1208c2ecf20Sopenharmony_ci * @keymode:		The keymode(type and size of key) associated with this TFM
1218c2ecf20Sopenharmony_ci * @mc:			pointer to the private data of driver handling this TFM
1228c2ecf20Sopenharmony_ci * @fallback_tfm:	pointer to the fallback TFM
1238c2ecf20Sopenharmony_ci */
1248c2ecf20Sopenharmony_cistruct meson_cipher_tfm_ctx {
1258c2ecf20Sopenharmony_ci	struct crypto_engine_ctx enginectx;
1268c2ecf20Sopenharmony_ci	u32 *key;
1278c2ecf20Sopenharmony_ci	u32 keylen;
1288c2ecf20Sopenharmony_ci	u32 keymode;
1298c2ecf20Sopenharmony_ci	struct meson_dev *mc;
1308c2ecf20Sopenharmony_ci	struct crypto_skcipher *fallback_tfm;
1318c2ecf20Sopenharmony_ci};
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci/*
1348c2ecf20Sopenharmony_ci * struct meson_alg_template - crypto_alg template
1358c2ecf20Sopenharmony_ci * @type:		the CRYPTO_ALG_TYPE for this template
1368c2ecf20Sopenharmony_ci * @blockmode:		the type of block operation
1378c2ecf20Sopenharmony_ci * @mc:			pointer to the meson_dev structure associated with this template
1388c2ecf20Sopenharmony_ci * @alg:		one of sub struct must be used
1398c2ecf20Sopenharmony_ci * @stat_req:		number of request done on this template
1408c2ecf20Sopenharmony_ci * @stat_fb:		total of all data len done on this template
1418c2ecf20Sopenharmony_ci */
1428c2ecf20Sopenharmony_cistruct meson_alg_template {
1438c2ecf20Sopenharmony_ci	u32 type;
1448c2ecf20Sopenharmony_ci	u32 blockmode;
1458c2ecf20Sopenharmony_ci	union {
1468c2ecf20Sopenharmony_ci		struct skcipher_alg skcipher;
1478c2ecf20Sopenharmony_ci	} alg;
1488c2ecf20Sopenharmony_ci	struct meson_dev *mc;
1498c2ecf20Sopenharmony_ci#ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
1508c2ecf20Sopenharmony_ci	unsigned long stat_req;
1518c2ecf20Sopenharmony_ci	unsigned long stat_fb;
1528c2ecf20Sopenharmony_ci#endif
1538c2ecf20Sopenharmony_ci};
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ciint meson_enqueue(struct crypto_async_request *areq, u32 type);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ciint meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
1588c2ecf20Sopenharmony_ci		     unsigned int keylen);
1598c2ecf20Sopenharmony_ciint meson_cipher_init(struct crypto_tfm *tfm);
1608c2ecf20Sopenharmony_civoid meson_cipher_exit(struct crypto_tfm *tfm);
1618c2ecf20Sopenharmony_ciint meson_skdecrypt(struct skcipher_request *areq);
1628c2ecf20Sopenharmony_ciint meson_skencrypt(struct skcipher_request *areq);
163