18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Cryptographic API.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Support for OMAP AES HW ACCELERATOR defines
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (c) 2015 Texas Instruments Incorporated
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci#ifndef __OMAP_AES_H__
108c2ecf20Sopenharmony_ci#define __OMAP_AES_H__
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <crypto/aes.h>
138c2ecf20Sopenharmony_ci#include <crypto/engine.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define DST_MAXBURST			4
168c2ecf20Sopenharmony_ci#define DMA_MIN				(DST_MAXBURST * sizeof(u32))
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * OMAP TRM gives bitfields as start:end, where start is the higher bit
228c2ecf20Sopenharmony_ci * number. For example 7:0
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci#define FLD_MASK(start, end)	(((1 << ((start) - (end) + 1)) - 1) << (end))
258c2ecf20Sopenharmony_ci#define FLD_VAL(val, start, end) (((val) << (end)) & FLD_MASK(start, end))
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define AES_REG_KEY(dd, x)		((dd)->pdata->key_ofs - \
288c2ecf20Sopenharmony_ci						(((x) ^ 0x01) * 0x04))
298c2ecf20Sopenharmony_ci#define AES_REG_IV(dd, x)		((dd)->pdata->iv_ofs + ((x) * 0x04))
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define AES_REG_CTRL(dd)		((dd)->pdata->ctrl_ofs)
328c2ecf20Sopenharmony_ci#define AES_REG_CTRL_CONTEXT_READY	BIT(31)
338c2ecf20Sopenharmony_ci#define AES_REG_CTRL_CTR_WIDTH_MASK	GENMASK(8, 7)
348c2ecf20Sopenharmony_ci#define AES_REG_CTRL_CTR_WIDTH_32	0
358c2ecf20Sopenharmony_ci#define AES_REG_CTRL_CTR_WIDTH_64	BIT(7)
368c2ecf20Sopenharmony_ci#define AES_REG_CTRL_CTR_WIDTH_96	BIT(8)
378c2ecf20Sopenharmony_ci#define AES_REG_CTRL_CTR_WIDTH_128	GENMASK(8, 7)
388c2ecf20Sopenharmony_ci#define AES_REG_CTRL_GCM		GENMASK(17, 16)
398c2ecf20Sopenharmony_ci#define AES_REG_CTRL_CTR		BIT(6)
408c2ecf20Sopenharmony_ci#define AES_REG_CTRL_CBC		BIT(5)
418c2ecf20Sopenharmony_ci#define AES_REG_CTRL_KEY_SIZE		GENMASK(4, 3)
428c2ecf20Sopenharmony_ci#define AES_REG_CTRL_DIRECTION		BIT(2)
438c2ecf20Sopenharmony_ci#define AES_REG_CTRL_INPUT_READY	BIT(1)
448c2ecf20Sopenharmony_ci#define AES_REG_CTRL_OUTPUT_READY	BIT(0)
458c2ecf20Sopenharmony_ci#define AES_REG_CTRL_MASK		GENMASK(24, 2)
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define AES_REG_C_LEN_0			0x54
488c2ecf20Sopenharmony_ci#define AES_REG_C_LEN_1			0x58
498c2ecf20Sopenharmony_ci#define AES_REG_A_LEN			0x5C
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define AES_REG_DATA_N(dd, x)		((dd)->pdata->data_ofs + ((x) * 0x04))
528c2ecf20Sopenharmony_ci#define AES_REG_TAG_N(dd, x)		(0x70 + ((x) * 0x04))
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define AES_REG_REV(dd)			((dd)->pdata->rev_ofs)
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define AES_REG_MASK(dd)		((dd)->pdata->mask_ofs)
578c2ecf20Sopenharmony_ci#define AES_REG_MASK_SIDLE		BIT(6)
588c2ecf20Sopenharmony_ci#define AES_REG_MASK_START		BIT(5)
598c2ecf20Sopenharmony_ci#define AES_REG_MASK_DMA_OUT_EN		BIT(3)
608c2ecf20Sopenharmony_ci#define AES_REG_MASK_DMA_IN_EN		BIT(2)
618c2ecf20Sopenharmony_ci#define AES_REG_MASK_SOFTRESET		BIT(1)
628c2ecf20Sopenharmony_ci#define AES_REG_AUTOIDLE		BIT(0)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define AES_REG_LENGTH_N(x)		(0x54 + ((x) * 0x04))
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define AES_REG_IRQ_STATUS(dd)         ((dd)->pdata->irq_status_ofs)
678c2ecf20Sopenharmony_ci#define AES_REG_IRQ_ENABLE(dd)         ((dd)->pdata->irq_enable_ofs)
688c2ecf20Sopenharmony_ci#define AES_REG_IRQ_DATA_IN            BIT(1)
698c2ecf20Sopenharmony_ci#define AES_REG_IRQ_DATA_OUT           BIT(2)
708c2ecf20Sopenharmony_ci#define DEFAULT_TIMEOUT		(5 * HZ)
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define DEFAULT_AUTOSUSPEND_DELAY	1000
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci#define FLAGS_MODE_MASK		0x001f
758c2ecf20Sopenharmony_ci#define FLAGS_ENCRYPT		BIT(0)
768c2ecf20Sopenharmony_ci#define FLAGS_CBC		BIT(1)
778c2ecf20Sopenharmony_ci#define FLAGS_CTR		BIT(2)
788c2ecf20Sopenharmony_ci#define FLAGS_GCM		BIT(3)
798c2ecf20Sopenharmony_ci#define FLAGS_RFC4106_GCM	BIT(4)
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci#define FLAGS_INIT		BIT(5)
828c2ecf20Sopenharmony_ci#define FLAGS_FAST		BIT(6)
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci#define FLAGS_IN_DATA_ST_SHIFT	8
858c2ecf20Sopenharmony_ci#define FLAGS_OUT_DATA_ST_SHIFT	10
868c2ecf20Sopenharmony_ci#define FLAGS_ASSOC_DATA_ST_SHIFT	12
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#define AES_BLOCK_WORDS		(AES_BLOCK_SIZE >> 2)
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistruct omap_aes_gcm_result {
918c2ecf20Sopenharmony_ci	struct completion completion;
928c2ecf20Sopenharmony_ci	int err;
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistruct omap_aes_ctx {
968c2ecf20Sopenharmony_ci	struct crypto_engine_ctx enginectx;
978c2ecf20Sopenharmony_ci	int		keylen;
988c2ecf20Sopenharmony_ci	u32		key[AES_KEYSIZE_256 / sizeof(u32)];
998c2ecf20Sopenharmony_ci	u8		nonce[4];
1008c2ecf20Sopenharmony_ci	struct crypto_skcipher	*fallback;
1018c2ecf20Sopenharmony_ci};
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistruct omap_aes_gcm_ctx {
1048c2ecf20Sopenharmony_ci	struct omap_aes_ctx	octx;
1058c2ecf20Sopenharmony_ci	struct crypto_aes_ctx	actx;
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistruct omap_aes_reqctx {
1098c2ecf20Sopenharmony_ci	struct omap_aes_dev *dd;
1108c2ecf20Sopenharmony_ci	unsigned long mode;
1118c2ecf20Sopenharmony_ci	u8 iv[AES_BLOCK_SIZE];
1128c2ecf20Sopenharmony_ci	u32 auth_tag[AES_BLOCK_SIZE / sizeof(u32)];
1138c2ecf20Sopenharmony_ci	struct skcipher_request fallback_req;	// keep at the end
1148c2ecf20Sopenharmony_ci};
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci#define OMAP_AES_QUEUE_LENGTH	1
1178c2ecf20Sopenharmony_ci#define OMAP_AES_CACHE_SIZE	0
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistruct omap_aes_algs_info {
1208c2ecf20Sopenharmony_ci	struct skcipher_alg	*algs_list;
1218c2ecf20Sopenharmony_ci	unsigned int		size;
1228c2ecf20Sopenharmony_ci	unsigned int		registered;
1238c2ecf20Sopenharmony_ci};
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistruct omap_aes_aead_algs {
1268c2ecf20Sopenharmony_ci	struct aead_alg	*algs_list;
1278c2ecf20Sopenharmony_ci	unsigned int	size;
1288c2ecf20Sopenharmony_ci	unsigned int	registered;
1298c2ecf20Sopenharmony_ci};
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistruct omap_aes_pdata {
1328c2ecf20Sopenharmony_ci	struct omap_aes_algs_info	*algs_info;
1338c2ecf20Sopenharmony_ci	unsigned int	algs_info_size;
1348c2ecf20Sopenharmony_ci	struct omap_aes_aead_algs	*aead_algs_info;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	void		(*trigger)(struct omap_aes_dev *dd, int length);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	u32		key_ofs;
1398c2ecf20Sopenharmony_ci	u32		iv_ofs;
1408c2ecf20Sopenharmony_ci	u32		ctrl_ofs;
1418c2ecf20Sopenharmony_ci	u32		data_ofs;
1428c2ecf20Sopenharmony_ci	u32		rev_ofs;
1438c2ecf20Sopenharmony_ci	u32		mask_ofs;
1448c2ecf20Sopenharmony_ci	u32             irq_enable_ofs;
1458c2ecf20Sopenharmony_ci	u32             irq_status_ofs;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	u32		dma_enable_in;
1488c2ecf20Sopenharmony_ci	u32		dma_enable_out;
1498c2ecf20Sopenharmony_ci	u32		dma_start;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	u32		major_mask;
1528c2ecf20Sopenharmony_ci	u32		major_shift;
1538c2ecf20Sopenharmony_ci	u32		minor_mask;
1548c2ecf20Sopenharmony_ci	u32		minor_shift;
1558c2ecf20Sopenharmony_ci};
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistruct omap_aes_dev {
1588c2ecf20Sopenharmony_ci	struct list_head	list;
1598c2ecf20Sopenharmony_ci	unsigned long		phys_base;
1608c2ecf20Sopenharmony_ci	void __iomem		*io_base;
1618c2ecf20Sopenharmony_ci	struct omap_aes_ctx	*ctx;
1628c2ecf20Sopenharmony_ci	struct device		*dev;
1638c2ecf20Sopenharmony_ci	unsigned long		flags;
1648c2ecf20Sopenharmony_ci	int			err;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	struct tasklet_struct	done_task;
1678c2ecf20Sopenharmony_ci	struct aead_queue	aead_queue;
1688c2ecf20Sopenharmony_ci	spinlock_t		lock;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	struct skcipher_request		*req;
1718c2ecf20Sopenharmony_ci	struct aead_request		*aead_req;
1728c2ecf20Sopenharmony_ci	struct crypto_engine		*engine;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	/*
1758c2ecf20Sopenharmony_ci	 * total is used by PIO mode for book keeping so introduce
1768c2ecf20Sopenharmony_ci	 * variable total_save as need it to calc page_order
1778c2ecf20Sopenharmony_ci	 */
1788c2ecf20Sopenharmony_ci	size_t				total;
1798c2ecf20Sopenharmony_ci	size_t				total_save;
1808c2ecf20Sopenharmony_ci	size_t				assoc_len;
1818c2ecf20Sopenharmony_ci	size_t				authsize;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	struct scatterlist		*in_sg;
1848c2ecf20Sopenharmony_ci	struct scatterlist		*out_sg;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	/* Buffers for copying for unaligned cases */
1878c2ecf20Sopenharmony_ci	struct scatterlist		in_sgl[2];
1888c2ecf20Sopenharmony_ci	struct scatterlist		out_sgl;
1898c2ecf20Sopenharmony_ci	struct scatterlist		*orig_out;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	struct scatter_walk		in_walk;
1928c2ecf20Sopenharmony_ci	struct scatter_walk		out_walk;
1938c2ecf20Sopenharmony_ci	struct dma_chan		*dma_lch_in;
1948c2ecf20Sopenharmony_ci	struct dma_chan		*dma_lch_out;
1958c2ecf20Sopenharmony_ci	int			in_sg_len;
1968c2ecf20Sopenharmony_ci	int			out_sg_len;
1978c2ecf20Sopenharmony_ci	int			pio_only;
1988c2ecf20Sopenharmony_ci	const struct omap_aes_pdata	*pdata;
1998c2ecf20Sopenharmony_ci};
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ciu32 omap_aes_read(struct omap_aes_dev *dd, u32 offset);
2028c2ecf20Sopenharmony_civoid omap_aes_write(struct omap_aes_dev *dd, u32 offset, u32 value);
2038c2ecf20Sopenharmony_cistruct omap_aes_dev *omap_aes_find_dev(struct omap_aes_reqctx *rctx);
2048c2ecf20Sopenharmony_ciint omap_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
2058c2ecf20Sopenharmony_ci			unsigned int keylen);
2068c2ecf20Sopenharmony_ciint omap_aes_4106gcm_setkey(struct crypto_aead *tfm, const u8 *key,
2078c2ecf20Sopenharmony_ci			    unsigned int keylen);
2088c2ecf20Sopenharmony_ciint omap_aes_gcm_encrypt(struct aead_request *req);
2098c2ecf20Sopenharmony_ciint omap_aes_gcm_decrypt(struct aead_request *req);
2108c2ecf20Sopenharmony_ciint omap_aes_gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize);
2118c2ecf20Sopenharmony_ciint omap_aes_4106gcm_encrypt(struct aead_request *req);
2128c2ecf20Sopenharmony_ciint omap_aes_4106gcm_decrypt(struct aead_request *req);
2138c2ecf20Sopenharmony_ciint omap_aes_4106gcm_setauthsize(struct crypto_aead *parent,
2148c2ecf20Sopenharmony_ci				 unsigned int authsize);
2158c2ecf20Sopenharmony_ciint omap_aes_gcm_cra_init(struct crypto_aead *tfm);
2168c2ecf20Sopenharmony_ciint omap_aes_write_ctrl(struct omap_aes_dev *dd);
2178c2ecf20Sopenharmony_ciint omap_aes_crypt_dma_start(struct omap_aes_dev *dd);
2188c2ecf20Sopenharmony_ciint omap_aes_crypt_dma_stop(struct omap_aes_dev *dd);
2198c2ecf20Sopenharmony_civoid omap_aes_gcm_dma_out_callback(void *data);
2208c2ecf20Sopenharmony_civoid omap_aes_clear_copy_flags(struct omap_aes_dev *dd);
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci#endif
223