18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Crypto acceleration support for Rockchip RK3288
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2015, Fuzhou Rockchip Electronics Co., Ltd
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Zain Wang <zain.wang@rock-chips.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Some ideas are from marvell/cesa.c and s5p-sss.c driver.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci#include <linux/device.h>
128c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
138c2ecf20Sopenharmony_ci#include "rk3288_crypto.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci * IC can not process zero message hash,
178c2ecf20Sopenharmony_ci * so we put the fixed hash out when met zero message.
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic bool rk_ahash_need_fallback(struct ahash_request *req)
218c2ecf20Sopenharmony_ci{
228c2ecf20Sopenharmony_ci	struct scatterlist *sg;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci	sg = req->src;
258c2ecf20Sopenharmony_ci	while (sg) {
268c2ecf20Sopenharmony_ci		if (!IS_ALIGNED(sg->offset, sizeof(u32))) {
278c2ecf20Sopenharmony_ci			return true;
288c2ecf20Sopenharmony_ci		}
298c2ecf20Sopenharmony_ci		if (sg->length % 4) {
308c2ecf20Sopenharmony_ci			return true;
318c2ecf20Sopenharmony_ci		}
328c2ecf20Sopenharmony_ci		sg = sg_next(sg);
338c2ecf20Sopenharmony_ci	}
348c2ecf20Sopenharmony_ci	return false;
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic int rk_ahash_digest_fb(struct ahash_request *areq)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	struct rk_ahash_rctx *rctx = ahash_request_ctx(areq);
408c2ecf20Sopenharmony_ci	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
418c2ecf20Sopenharmony_ci	struct rk_ahash_ctx *tfmctx = crypto_ahash_ctx(tfm);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	ahash_request_set_tfm(&rctx->fallback_req, tfmctx->fallback_tfm);
448c2ecf20Sopenharmony_ci	rctx->fallback_req.base.flags = areq->base.flags &
458c2ecf20Sopenharmony_ci					CRYPTO_TFM_REQ_MAY_SLEEP;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	rctx->fallback_req.nbytes = areq->nbytes;
488c2ecf20Sopenharmony_ci	rctx->fallback_req.src = areq->src;
498c2ecf20Sopenharmony_ci	rctx->fallback_req.result = areq->result;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	return crypto_ahash_digest(&rctx->fallback_req);
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic int zero_message_process(struct ahash_request *req)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
578c2ecf20Sopenharmony_ci	int rk_digest_size = crypto_ahash_digestsize(tfm);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	switch (rk_digest_size) {
608c2ecf20Sopenharmony_ci	case SHA1_DIGEST_SIZE:
618c2ecf20Sopenharmony_ci		memcpy(req->result, sha1_zero_message_hash, rk_digest_size);
628c2ecf20Sopenharmony_ci		break;
638c2ecf20Sopenharmony_ci	case SHA256_DIGEST_SIZE:
648c2ecf20Sopenharmony_ci		memcpy(req->result, sha256_zero_message_hash, rk_digest_size);
658c2ecf20Sopenharmony_ci		break;
668c2ecf20Sopenharmony_ci	case MD5_DIGEST_SIZE:
678c2ecf20Sopenharmony_ci		memcpy(req->result, md5_zero_message_hash, rk_digest_size);
688c2ecf20Sopenharmony_ci		break;
698c2ecf20Sopenharmony_ci	default:
708c2ecf20Sopenharmony_ci		return -EINVAL;
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	return 0;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic void rk_ahash_reg_init(struct ahash_request *req)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	struct rk_ahash_rctx *rctx = ahash_request_ctx(req);
798c2ecf20Sopenharmony_ci	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
808c2ecf20Sopenharmony_ci	struct rk_ahash_ctx *tctx = crypto_ahash_ctx(tfm);
818c2ecf20Sopenharmony_ci	struct rk_crypto_info *dev = tctx->dev;
828c2ecf20Sopenharmony_ci	int reg_status;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	reg_status = CRYPTO_READ(dev, RK_CRYPTO_CTRL) |
858c2ecf20Sopenharmony_ci		     RK_CRYPTO_HASH_FLUSH | _SBF(0xffff, 16);
868c2ecf20Sopenharmony_ci	CRYPTO_WRITE(dev, RK_CRYPTO_CTRL, reg_status);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	reg_status = CRYPTO_READ(dev, RK_CRYPTO_CTRL);
898c2ecf20Sopenharmony_ci	reg_status &= (~RK_CRYPTO_HASH_FLUSH);
908c2ecf20Sopenharmony_ci	reg_status |= _SBF(0xffff, 16);
918c2ecf20Sopenharmony_ci	CRYPTO_WRITE(dev, RK_CRYPTO_CTRL, reg_status);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	memset_io(dev->reg + RK_CRYPTO_HASH_DOUT_0, 0, 32);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	CRYPTO_WRITE(dev, RK_CRYPTO_INTENA, RK_CRYPTO_HRDMA_ERR_ENA |
968c2ecf20Sopenharmony_ci					    RK_CRYPTO_HRDMA_DONE_ENA);
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	CRYPTO_WRITE(dev, RK_CRYPTO_INTSTS, RK_CRYPTO_HRDMA_ERR_INT |
998c2ecf20Sopenharmony_ci					    RK_CRYPTO_HRDMA_DONE_INT);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	CRYPTO_WRITE(dev, RK_CRYPTO_HASH_CTRL, rctx->mode |
1028c2ecf20Sopenharmony_ci					       RK_CRYPTO_HASH_SWAP_DO);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	CRYPTO_WRITE(dev, RK_CRYPTO_CONF, RK_CRYPTO_BYTESWAP_HRFIFO |
1058c2ecf20Sopenharmony_ci					  RK_CRYPTO_BYTESWAP_BRFIFO |
1068c2ecf20Sopenharmony_ci					  RK_CRYPTO_BYTESWAP_BTFIFO);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	CRYPTO_WRITE(dev, RK_CRYPTO_HASH_MSG_LEN, req->nbytes);
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic int rk_ahash_init(struct ahash_request *req)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	struct rk_ahash_rctx *rctx = ahash_request_ctx(req);
1148c2ecf20Sopenharmony_ci	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1158c2ecf20Sopenharmony_ci	struct rk_ahash_ctx *ctx = crypto_ahash_ctx(tfm);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
1188c2ecf20Sopenharmony_ci	rctx->fallback_req.base.flags = req->base.flags &
1198c2ecf20Sopenharmony_ci					CRYPTO_TFM_REQ_MAY_SLEEP;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	return crypto_ahash_init(&rctx->fallback_req);
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic int rk_ahash_update(struct ahash_request *req)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	struct rk_ahash_rctx *rctx = ahash_request_ctx(req);
1278c2ecf20Sopenharmony_ci	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1288c2ecf20Sopenharmony_ci	struct rk_ahash_ctx *ctx = crypto_ahash_ctx(tfm);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
1318c2ecf20Sopenharmony_ci	rctx->fallback_req.base.flags = req->base.flags &
1328c2ecf20Sopenharmony_ci					CRYPTO_TFM_REQ_MAY_SLEEP;
1338c2ecf20Sopenharmony_ci	rctx->fallback_req.nbytes = req->nbytes;
1348c2ecf20Sopenharmony_ci	rctx->fallback_req.src = req->src;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	return crypto_ahash_update(&rctx->fallback_req);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic int rk_ahash_final(struct ahash_request *req)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	struct rk_ahash_rctx *rctx = ahash_request_ctx(req);
1428c2ecf20Sopenharmony_ci	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1438c2ecf20Sopenharmony_ci	struct rk_ahash_ctx *ctx = crypto_ahash_ctx(tfm);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
1468c2ecf20Sopenharmony_ci	rctx->fallback_req.base.flags = req->base.flags &
1478c2ecf20Sopenharmony_ci					CRYPTO_TFM_REQ_MAY_SLEEP;
1488c2ecf20Sopenharmony_ci	rctx->fallback_req.result = req->result;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	return crypto_ahash_final(&rctx->fallback_req);
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic int rk_ahash_finup(struct ahash_request *req)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	struct rk_ahash_rctx *rctx = ahash_request_ctx(req);
1568c2ecf20Sopenharmony_ci	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1578c2ecf20Sopenharmony_ci	struct rk_ahash_ctx *ctx = crypto_ahash_ctx(tfm);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
1608c2ecf20Sopenharmony_ci	rctx->fallback_req.base.flags = req->base.flags &
1618c2ecf20Sopenharmony_ci					CRYPTO_TFM_REQ_MAY_SLEEP;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	rctx->fallback_req.nbytes = req->nbytes;
1648c2ecf20Sopenharmony_ci	rctx->fallback_req.src = req->src;
1658c2ecf20Sopenharmony_ci	rctx->fallback_req.result = req->result;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	return crypto_ahash_finup(&rctx->fallback_req);
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic int rk_ahash_import(struct ahash_request *req, const void *in)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	struct rk_ahash_rctx *rctx = ahash_request_ctx(req);
1738c2ecf20Sopenharmony_ci	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1748c2ecf20Sopenharmony_ci	struct rk_ahash_ctx *ctx = crypto_ahash_ctx(tfm);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
1778c2ecf20Sopenharmony_ci	rctx->fallback_req.base.flags = req->base.flags &
1788c2ecf20Sopenharmony_ci					CRYPTO_TFM_REQ_MAY_SLEEP;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	return crypto_ahash_import(&rctx->fallback_req, in);
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic int rk_ahash_export(struct ahash_request *req, void *out)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	struct rk_ahash_rctx *rctx = ahash_request_ctx(req);
1868c2ecf20Sopenharmony_ci	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
1878c2ecf20Sopenharmony_ci	struct rk_ahash_ctx *ctx = crypto_ahash_ctx(tfm);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	ahash_request_set_tfm(&rctx->fallback_req, ctx->fallback_tfm);
1908c2ecf20Sopenharmony_ci	rctx->fallback_req.base.flags = req->base.flags &
1918c2ecf20Sopenharmony_ci					CRYPTO_TFM_REQ_MAY_SLEEP;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	return crypto_ahash_export(&rctx->fallback_req, out);
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic int rk_ahash_digest(struct ahash_request *req)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	struct rk_ahash_ctx *tctx = crypto_tfm_ctx(req->base.tfm);
1998c2ecf20Sopenharmony_ci	struct rk_crypto_info *dev = tctx->dev;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	if (rk_ahash_need_fallback(req))
2028c2ecf20Sopenharmony_ci		return rk_ahash_digest_fb(req);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (!req->nbytes)
2058c2ecf20Sopenharmony_ci		return zero_message_process(req);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	return crypto_transfer_hash_request_to_engine(dev->engine, req);
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistatic void crypto_ahash_dma_start(struct rk_crypto_info *dev, struct scatterlist *sg)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	CRYPTO_WRITE(dev, RK_CRYPTO_HRDMAS, sg_dma_address(sg));
2138c2ecf20Sopenharmony_ci	CRYPTO_WRITE(dev, RK_CRYPTO_HRDMAL, sg_dma_len(sg) / 4);
2148c2ecf20Sopenharmony_ci	CRYPTO_WRITE(dev, RK_CRYPTO_CTRL, RK_CRYPTO_HASH_START |
2158c2ecf20Sopenharmony_ci					  (RK_CRYPTO_HASH_START << 16));
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic int rk_hash_prepare(struct crypto_engine *engine, void *breq)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	struct ahash_request *areq = container_of(breq, struct ahash_request, base);
2218c2ecf20Sopenharmony_ci	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
2228c2ecf20Sopenharmony_ci	struct rk_ahash_rctx *rctx = ahash_request_ctx(areq);
2238c2ecf20Sopenharmony_ci	struct rk_ahash_ctx *tctx = crypto_ahash_ctx(tfm);
2248c2ecf20Sopenharmony_ci	int ret;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	ret = dma_map_sg(tctx->dev->dev, areq->src, sg_nents(areq->src), DMA_TO_DEVICE);
2278c2ecf20Sopenharmony_ci	if (ret <= 0)
2288c2ecf20Sopenharmony_ci		return -EINVAL;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	rctx->nrsg = ret;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	return 0;
2338c2ecf20Sopenharmony_ci}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic int rk_hash_unprepare(struct crypto_engine *engine, void *breq)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	struct ahash_request *areq = container_of(breq, struct ahash_request, base);
2388c2ecf20Sopenharmony_ci	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
2398c2ecf20Sopenharmony_ci	struct rk_ahash_rctx *rctx = ahash_request_ctx(areq);
2408c2ecf20Sopenharmony_ci	struct rk_ahash_ctx *tctx = crypto_ahash_ctx(tfm);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	dma_unmap_sg(tctx->dev->dev, areq->src, rctx->nrsg, DMA_TO_DEVICE);
2438c2ecf20Sopenharmony_ci	return 0;
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_cistatic int rk_hash_run(struct crypto_engine *engine, void *breq)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	struct ahash_request *areq = container_of(breq, struct ahash_request, base);
2498c2ecf20Sopenharmony_ci	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
2508c2ecf20Sopenharmony_ci	struct rk_ahash_rctx *rctx = ahash_request_ctx(areq);
2518c2ecf20Sopenharmony_ci	struct rk_ahash_ctx *tctx = crypto_ahash_ctx(tfm);
2528c2ecf20Sopenharmony_ci	struct scatterlist *sg = areq->src;
2538c2ecf20Sopenharmony_ci	int err = 0;
2548c2ecf20Sopenharmony_ci	int i;
2558c2ecf20Sopenharmony_ci	u32 v;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	rctx->mode = 0;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	switch (crypto_ahash_digestsize(tfm)) {
2608c2ecf20Sopenharmony_ci	case SHA1_DIGEST_SIZE:
2618c2ecf20Sopenharmony_ci		rctx->mode = RK_CRYPTO_HASH_SHA1;
2628c2ecf20Sopenharmony_ci		break;
2638c2ecf20Sopenharmony_ci	case SHA256_DIGEST_SIZE:
2648c2ecf20Sopenharmony_ci		rctx->mode = RK_CRYPTO_HASH_SHA256;
2658c2ecf20Sopenharmony_ci		break;
2668c2ecf20Sopenharmony_ci	case MD5_DIGEST_SIZE:
2678c2ecf20Sopenharmony_ci		rctx->mode = RK_CRYPTO_HASH_MD5;
2688c2ecf20Sopenharmony_ci		break;
2698c2ecf20Sopenharmony_ci	default:
2708c2ecf20Sopenharmony_ci		err =  -EINVAL;
2718c2ecf20Sopenharmony_ci		goto theend;
2728c2ecf20Sopenharmony_ci	}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	rk_ahash_reg_init(areq);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	while (sg) {
2778c2ecf20Sopenharmony_ci		reinit_completion(&tctx->dev->complete);
2788c2ecf20Sopenharmony_ci		tctx->dev->status = 0;
2798c2ecf20Sopenharmony_ci		crypto_ahash_dma_start(tctx->dev, sg);
2808c2ecf20Sopenharmony_ci		wait_for_completion_interruptible_timeout(&tctx->dev->complete,
2818c2ecf20Sopenharmony_ci							  msecs_to_jiffies(2000));
2828c2ecf20Sopenharmony_ci		if (!tctx->dev->status) {
2838c2ecf20Sopenharmony_ci			dev_err(tctx->dev->dev, "DMA timeout\n");
2848c2ecf20Sopenharmony_ci			err = -EFAULT;
2858c2ecf20Sopenharmony_ci			goto theend;
2868c2ecf20Sopenharmony_ci		}
2878c2ecf20Sopenharmony_ci		sg = sg_next(sg);
2888c2ecf20Sopenharmony_ci	}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci		/*
2918c2ecf20Sopenharmony_ci		 * it will take some time to process date after last dma
2928c2ecf20Sopenharmony_ci		 * transmission.
2938c2ecf20Sopenharmony_ci		 *
2948c2ecf20Sopenharmony_ci		 * waiting time is relative with the last date len,
2958c2ecf20Sopenharmony_ci		 * so cannot set a fixed time here.
2968c2ecf20Sopenharmony_ci		 * 10us makes system not call here frequently wasting
2978c2ecf20Sopenharmony_ci		 * efficiency, and make it response quickly when dma
2988c2ecf20Sopenharmony_ci		 * complete.
2998c2ecf20Sopenharmony_ci		 */
3008c2ecf20Sopenharmony_ci	while (!CRYPTO_READ(tctx->dev, RK_CRYPTO_HASH_STS))
3018c2ecf20Sopenharmony_ci		udelay(10);
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	for (i = 0; i < crypto_ahash_digestsize(tfm) / 4; i++) {
3048c2ecf20Sopenharmony_ci		v = readl(tctx->dev->reg + RK_CRYPTO_HASH_DOUT_0 + i * 4);
3058c2ecf20Sopenharmony_ci		put_unaligned_le32(v, areq->result + i * 4);
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_citheend:
3098c2ecf20Sopenharmony_ci	local_bh_disable();
3108c2ecf20Sopenharmony_ci	crypto_finalize_hash_request(engine, breq, err);
3118c2ecf20Sopenharmony_ci	local_bh_enable();
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	return 0;
3148c2ecf20Sopenharmony_ci}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_cistatic int rk_cra_hash_init(struct crypto_tfm *tfm)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	struct rk_ahash_ctx *tctx = crypto_tfm_ctx(tfm);
3198c2ecf20Sopenharmony_ci	struct rk_crypto_tmp *algt;
3208c2ecf20Sopenharmony_ci	struct ahash_alg *alg = __crypto_ahash_alg(tfm->__crt_alg);
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	const char *alg_name = crypto_tfm_alg_name(tfm);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	algt = container_of(alg, struct rk_crypto_tmp, alg.hash);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	tctx->dev = algt->dev;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	/* for fallback */
3298c2ecf20Sopenharmony_ci	tctx->fallback_tfm = crypto_alloc_ahash(alg_name, 0,
3308c2ecf20Sopenharmony_ci					       CRYPTO_ALG_NEED_FALLBACK);
3318c2ecf20Sopenharmony_ci	if (IS_ERR(tctx->fallback_tfm)) {
3328c2ecf20Sopenharmony_ci		dev_err(tctx->dev->dev, "Could not load fallback driver.\n");
3338c2ecf20Sopenharmony_ci		return PTR_ERR(tctx->fallback_tfm);
3348c2ecf20Sopenharmony_ci	}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
3378c2ecf20Sopenharmony_ci				 sizeof(struct rk_ahash_rctx) +
3388c2ecf20Sopenharmony_ci				 crypto_ahash_reqsize(tctx->fallback_tfm));
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	tctx->enginectx.op.do_one_request = rk_hash_run;
3418c2ecf20Sopenharmony_ci	tctx->enginectx.op.prepare_request = rk_hash_prepare;
3428c2ecf20Sopenharmony_ci	tctx->enginectx.op.unprepare_request = rk_hash_unprepare;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	return 0;
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic void rk_cra_hash_exit(struct crypto_tfm *tfm)
3488c2ecf20Sopenharmony_ci{
3498c2ecf20Sopenharmony_ci	struct rk_ahash_ctx *tctx = crypto_tfm_ctx(tfm);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	crypto_free_ahash(tctx->fallback_tfm);
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistruct rk_crypto_tmp rk_ahash_sha1 = {
3558c2ecf20Sopenharmony_ci	.type = ALG_TYPE_HASH,
3568c2ecf20Sopenharmony_ci	.alg.hash = {
3578c2ecf20Sopenharmony_ci		.init = rk_ahash_init,
3588c2ecf20Sopenharmony_ci		.update = rk_ahash_update,
3598c2ecf20Sopenharmony_ci		.final = rk_ahash_final,
3608c2ecf20Sopenharmony_ci		.finup = rk_ahash_finup,
3618c2ecf20Sopenharmony_ci		.export = rk_ahash_export,
3628c2ecf20Sopenharmony_ci		.import = rk_ahash_import,
3638c2ecf20Sopenharmony_ci		.digest = rk_ahash_digest,
3648c2ecf20Sopenharmony_ci		.halg = {
3658c2ecf20Sopenharmony_ci			 .digestsize = SHA1_DIGEST_SIZE,
3668c2ecf20Sopenharmony_ci			 .statesize = sizeof(struct sha1_state),
3678c2ecf20Sopenharmony_ci			 .base = {
3688c2ecf20Sopenharmony_ci				  .cra_name = "sha1",
3698c2ecf20Sopenharmony_ci				  .cra_driver_name = "rk-sha1",
3708c2ecf20Sopenharmony_ci				  .cra_priority = 300,
3718c2ecf20Sopenharmony_ci				  .cra_flags = CRYPTO_ALG_ASYNC |
3728c2ecf20Sopenharmony_ci					       CRYPTO_ALG_NEED_FALLBACK,
3738c2ecf20Sopenharmony_ci				  .cra_blocksize = SHA1_BLOCK_SIZE,
3748c2ecf20Sopenharmony_ci				  .cra_ctxsize = sizeof(struct rk_ahash_ctx),
3758c2ecf20Sopenharmony_ci				  .cra_alignmask = 3,
3768c2ecf20Sopenharmony_ci				  .cra_init = rk_cra_hash_init,
3778c2ecf20Sopenharmony_ci				  .cra_exit = rk_cra_hash_exit,
3788c2ecf20Sopenharmony_ci				  .cra_module = THIS_MODULE,
3798c2ecf20Sopenharmony_ci				  }
3808c2ecf20Sopenharmony_ci			 }
3818c2ecf20Sopenharmony_ci	}
3828c2ecf20Sopenharmony_ci};
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_cistruct rk_crypto_tmp rk_ahash_sha256 = {
3858c2ecf20Sopenharmony_ci	.type = ALG_TYPE_HASH,
3868c2ecf20Sopenharmony_ci	.alg.hash = {
3878c2ecf20Sopenharmony_ci		.init = rk_ahash_init,
3888c2ecf20Sopenharmony_ci		.update = rk_ahash_update,
3898c2ecf20Sopenharmony_ci		.final = rk_ahash_final,
3908c2ecf20Sopenharmony_ci		.finup = rk_ahash_finup,
3918c2ecf20Sopenharmony_ci		.export = rk_ahash_export,
3928c2ecf20Sopenharmony_ci		.import = rk_ahash_import,
3938c2ecf20Sopenharmony_ci		.digest = rk_ahash_digest,
3948c2ecf20Sopenharmony_ci		.halg = {
3958c2ecf20Sopenharmony_ci			 .digestsize = SHA256_DIGEST_SIZE,
3968c2ecf20Sopenharmony_ci			 .statesize = sizeof(struct sha256_state),
3978c2ecf20Sopenharmony_ci			 .base = {
3988c2ecf20Sopenharmony_ci				  .cra_name = "sha256",
3998c2ecf20Sopenharmony_ci				  .cra_driver_name = "rk-sha256",
4008c2ecf20Sopenharmony_ci				  .cra_priority = 300,
4018c2ecf20Sopenharmony_ci				  .cra_flags = CRYPTO_ALG_ASYNC |
4028c2ecf20Sopenharmony_ci					       CRYPTO_ALG_NEED_FALLBACK,
4038c2ecf20Sopenharmony_ci				  .cra_blocksize = SHA256_BLOCK_SIZE,
4048c2ecf20Sopenharmony_ci				  .cra_ctxsize = sizeof(struct rk_ahash_ctx),
4058c2ecf20Sopenharmony_ci				  .cra_alignmask = 3,
4068c2ecf20Sopenharmony_ci				  .cra_init = rk_cra_hash_init,
4078c2ecf20Sopenharmony_ci				  .cra_exit = rk_cra_hash_exit,
4088c2ecf20Sopenharmony_ci				  .cra_module = THIS_MODULE,
4098c2ecf20Sopenharmony_ci				  }
4108c2ecf20Sopenharmony_ci			 }
4118c2ecf20Sopenharmony_ci	}
4128c2ecf20Sopenharmony_ci};
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cistruct rk_crypto_tmp rk_ahash_md5 = {
4158c2ecf20Sopenharmony_ci	.type = ALG_TYPE_HASH,
4168c2ecf20Sopenharmony_ci	.alg.hash = {
4178c2ecf20Sopenharmony_ci		.init = rk_ahash_init,
4188c2ecf20Sopenharmony_ci		.update = rk_ahash_update,
4198c2ecf20Sopenharmony_ci		.final = rk_ahash_final,
4208c2ecf20Sopenharmony_ci		.finup = rk_ahash_finup,
4218c2ecf20Sopenharmony_ci		.export = rk_ahash_export,
4228c2ecf20Sopenharmony_ci		.import = rk_ahash_import,
4238c2ecf20Sopenharmony_ci		.digest = rk_ahash_digest,
4248c2ecf20Sopenharmony_ci		.halg = {
4258c2ecf20Sopenharmony_ci			 .digestsize = MD5_DIGEST_SIZE,
4268c2ecf20Sopenharmony_ci			 .statesize = sizeof(struct md5_state),
4278c2ecf20Sopenharmony_ci			 .base = {
4288c2ecf20Sopenharmony_ci				  .cra_name = "md5",
4298c2ecf20Sopenharmony_ci				  .cra_driver_name = "rk-md5",
4308c2ecf20Sopenharmony_ci				  .cra_priority = 300,
4318c2ecf20Sopenharmony_ci				  .cra_flags = CRYPTO_ALG_ASYNC |
4328c2ecf20Sopenharmony_ci					       CRYPTO_ALG_NEED_FALLBACK,
4338c2ecf20Sopenharmony_ci				  .cra_blocksize = SHA1_BLOCK_SIZE,
4348c2ecf20Sopenharmony_ci				  .cra_ctxsize = sizeof(struct rk_ahash_ctx),
4358c2ecf20Sopenharmony_ci				  .cra_alignmask = 3,
4368c2ecf20Sopenharmony_ci				  .cra_init = rk_cra_hash_init,
4378c2ecf20Sopenharmony_ci				  .cra_exit = rk_cra_hash_exit,
4388c2ecf20Sopenharmony_ci				  .cra_module = THIS_MODULE,
4398c2ecf20Sopenharmony_ci				  }
4408c2ecf20Sopenharmony_ci			}
4418c2ecf20Sopenharmony_ci	}
4428c2ecf20Sopenharmony_ci};
443