18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Support for Marvell's Cryptographic Engine and Security Accelerator (CESA)
48c2ecf20Sopenharmony_ci * that can be found on the following platform: Orion, Kirkwood, Armada. This
58c2ecf20Sopenharmony_ci * driver supports the TDMA engine on platforms on which it is available.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Boris Brezillon <boris.brezillon@free-electrons.com>
88c2ecf20Sopenharmony_ci * Author: Arnaud Ebalard <arno@natisbad.org>
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * This work is based on an initial version written by
118c2ecf20Sopenharmony_ci * Sebastian Andrzej Siewior < sebastian at breakpoint dot cc >
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/delay.h>
158c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
168c2ecf20Sopenharmony_ci#include <linux/genalloc.h>
178c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
188c2ecf20Sopenharmony_ci#include <linux/io.h>
198c2ecf20Sopenharmony_ci#include <linux/kthread.h>
208c2ecf20Sopenharmony_ci#include <linux/mbus.h>
218c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
228c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
238c2ecf20Sopenharmony_ci#include <linux/slab.h>
248c2ecf20Sopenharmony_ci#include <linux/module.h>
258c2ecf20Sopenharmony_ci#include <linux/clk.h>
268c2ecf20Sopenharmony_ci#include <linux/of.h>
278c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
288c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include "cesa.h"
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* Limit of the crypto queue before reaching the backlog */
338c2ecf20Sopenharmony_ci#define CESA_CRYPTO_DEFAULT_MAX_QLEN 128
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistruct mv_cesa_dev *cesa_dev;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistruct crypto_async_request *
388c2ecf20Sopenharmony_cimv_cesa_dequeue_req_locked(struct mv_cesa_engine *engine,
398c2ecf20Sopenharmony_ci			   struct crypto_async_request **backlog)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	struct crypto_async_request *req;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	*backlog = crypto_get_backlog(&engine->queue);
448c2ecf20Sopenharmony_ci	req = crypto_dequeue_request(&engine->queue);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	if (!req)
478c2ecf20Sopenharmony_ci		return NULL;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	return req;
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic void mv_cesa_rearm_engine(struct mv_cesa_engine *engine)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	struct crypto_async_request *req = NULL, *backlog = NULL;
558c2ecf20Sopenharmony_ci	struct mv_cesa_ctx *ctx;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	spin_lock_bh(&engine->lock);
598c2ecf20Sopenharmony_ci	if (!engine->req) {
608c2ecf20Sopenharmony_ci		req = mv_cesa_dequeue_req_locked(engine, &backlog);
618c2ecf20Sopenharmony_ci		engine->req = req;
628c2ecf20Sopenharmony_ci	}
638c2ecf20Sopenharmony_ci	spin_unlock_bh(&engine->lock);
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	if (!req)
668c2ecf20Sopenharmony_ci		return;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	if (backlog)
698c2ecf20Sopenharmony_ci		backlog->complete(backlog, -EINPROGRESS);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	ctx = crypto_tfm_ctx(req->tfm);
728c2ecf20Sopenharmony_ci	ctx->ops->step(req);
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic int mv_cesa_std_process(struct mv_cesa_engine *engine, u32 status)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	struct crypto_async_request *req;
788c2ecf20Sopenharmony_ci	struct mv_cesa_ctx *ctx;
798c2ecf20Sopenharmony_ci	int res;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	req = engine->req;
828c2ecf20Sopenharmony_ci	ctx = crypto_tfm_ctx(req->tfm);
838c2ecf20Sopenharmony_ci	res = ctx->ops->process(req, status);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	if (res == 0) {
868c2ecf20Sopenharmony_ci		ctx->ops->complete(req);
878c2ecf20Sopenharmony_ci		mv_cesa_engine_enqueue_complete_request(engine, req);
888c2ecf20Sopenharmony_ci	} else if (res == -EINPROGRESS) {
898c2ecf20Sopenharmony_ci		ctx->ops->step(req);
908c2ecf20Sopenharmony_ci	}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	return res;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic int mv_cesa_int_process(struct mv_cesa_engine *engine, u32 status)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	if (engine->chain.first && engine->chain.last)
988c2ecf20Sopenharmony_ci		return mv_cesa_tdma_process(engine, status);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	return mv_cesa_std_process(engine, status);
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic inline void
1048c2ecf20Sopenharmony_cimv_cesa_complete_req(struct mv_cesa_ctx *ctx, struct crypto_async_request *req,
1058c2ecf20Sopenharmony_ci		     int res)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	ctx->ops->cleanup(req);
1088c2ecf20Sopenharmony_ci	local_bh_disable();
1098c2ecf20Sopenharmony_ci	req->complete(req, res);
1108c2ecf20Sopenharmony_ci	local_bh_enable();
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic irqreturn_t mv_cesa_int(int irq, void *priv)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	struct mv_cesa_engine *engine = priv;
1168c2ecf20Sopenharmony_ci	struct crypto_async_request *req;
1178c2ecf20Sopenharmony_ci	struct mv_cesa_ctx *ctx;
1188c2ecf20Sopenharmony_ci	u32 status, mask;
1198c2ecf20Sopenharmony_ci	irqreturn_t ret = IRQ_NONE;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	while (true) {
1228c2ecf20Sopenharmony_ci		int res;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci		mask = mv_cesa_get_int_mask(engine);
1258c2ecf20Sopenharmony_ci		status = readl(engine->regs + CESA_SA_INT_STATUS);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci		if (!(status & mask))
1288c2ecf20Sopenharmony_ci			break;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci		/*
1318c2ecf20Sopenharmony_ci		 * TODO: avoid clearing the FPGA_INT_STATUS if this not
1328c2ecf20Sopenharmony_ci		 * relevant on some platforms.
1338c2ecf20Sopenharmony_ci		 */
1348c2ecf20Sopenharmony_ci		writel(~status, engine->regs + CESA_SA_FPGA_INT_STATUS);
1358c2ecf20Sopenharmony_ci		writel(~status, engine->regs + CESA_SA_INT_STATUS);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci		/* Process fetched requests */
1388c2ecf20Sopenharmony_ci		res = mv_cesa_int_process(engine, status & mask);
1398c2ecf20Sopenharmony_ci		ret = IRQ_HANDLED;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci		spin_lock_bh(&engine->lock);
1428c2ecf20Sopenharmony_ci		req = engine->req;
1438c2ecf20Sopenharmony_ci		if (res != -EINPROGRESS)
1448c2ecf20Sopenharmony_ci			engine->req = NULL;
1458c2ecf20Sopenharmony_ci		spin_unlock_bh(&engine->lock);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci		ctx = crypto_tfm_ctx(req->tfm);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci		if (res && res != -EINPROGRESS)
1508c2ecf20Sopenharmony_ci			mv_cesa_complete_req(ctx, req, res);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci		/* Launch the next pending request */
1538c2ecf20Sopenharmony_ci		mv_cesa_rearm_engine(engine);
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci		/* Iterate over the complete queue */
1568c2ecf20Sopenharmony_ci		while (true) {
1578c2ecf20Sopenharmony_ci			req = mv_cesa_engine_dequeue_complete_request(engine);
1588c2ecf20Sopenharmony_ci			if (!req)
1598c2ecf20Sopenharmony_ci				break;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci			ctx = crypto_tfm_ctx(req->tfm);
1628c2ecf20Sopenharmony_ci			mv_cesa_complete_req(ctx, req, 0);
1638c2ecf20Sopenharmony_ci		}
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	return ret;
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ciint mv_cesa_queue_req(struct crypto_async_request *req,
1708c2ecf20Sopenharmony_ci		      struct mv_cesa_req *creq)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	int ret;
1738c2ecf20Sopenharmony_ci	struct mv_cesa_engine *engine = creq->engine;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	spin_lock_bh(&engine->lock);
1768c2ecf20Sopenharmony_ci	ret = crypto_enqueue_request(&engine->queue, req);
1778c2ecf20Sopenharmony_ci	if ((mv_cesa_req_get_type(creq) == CESA_DMA_REQ) &&
1788c2ecf20Sopenharmony_ci	    (ret == -EINPROGRESS || ret == -EBUSY))
1798c2ecf20Sopenharmony_ci		mv_cesa_tdma_chain(engine, creq);
1808c2ecf20Sopenharmony_ci	spin_unlock_bh(&engine->lock);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	if (ret != -EINPROGRESS)
1838c2ecf20Sopenharmony_ci		return ret;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	mv_cesa_rearm_engine(engine);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	return -EINPROGRESS;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic int mv_cesa_add_algs(struct mv_cesa_dev *cesa)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	int ret;
1938c2ecf20Sopenharmony_ci	int i, j;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	for (i = 0; i < cesa->caps->ncipher_algs; i++) {
1968c2ecf20Sopenharmony_ci		ret = crypto_register_skcipher(cesa->caps->cipher_algs[i]);
1978c2ecf20Sopenharmony_ci		if (ret)
1988c2ecf20Sopenharmony_ci			goto err_unregister_crypto;
1998c2ecf20Sopenharmony_ci	}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	for (i = 0; i < cesa->caps->nahash_algs; i++) {
2028c2ecf20Sopenharmony_ci		ret = crypto_register_ahash(cesa->caps->ahash_algs[i]);
2038c2ecf20Sopenharmony_ci		if (ret)
2048c2ecf20Sopenharmony_ci			goto err_unregister_ahash;
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	return 0;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_cierr_unregister_ahash:
2108c2ecf20Sopenharmony_ci	for (j = 0; j < i; j++)
2118c2ecf20Sopenharmony_ci		crypto_unregister_ahash(cesa->caps->ahash_algs[j]);
2128c2ecf20Sopenharmony_ci	i = cesa->caps->ncipher_algs;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cierr_unregister_crypto:
2158c2ecf20Sopenharmony_ci	for (j = 0; j < i; j++)
2168c2ecf20Sopenharmony_ci		crypto_unregister_skcipher(cesa->caps->cipher_algs[j]);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	return ret;
2198c2ecf20Sopenharmony_ci}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_cistatic void mv_cesa_remove_algs(struct mv_cesa_dev *cesa)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	int i;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	for (i = 0; i < cesa->caps->nahash_algs; i++)
2268c2ecf20Sopenharmony_ci		crypto_unregister_ahash(cesa->caps->ahash_algs[i]);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	for (i = 0; i < cesa->caps->ncipher_algs; i++)
2298c2ecf20Sopenharmony_ci		crypto_unregister_skcipher(cesa->caps->cipher_algs[i]);
2308c2ecf20Sopenharmony_ci}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistatic struct skcipher_alg *orion_cipher_algs[] = {
2338c2ecf20Sopenharmony_ci	&mv_cesa_ecb_des_alg,
2348c2ecf20Sopenharmony_ci	&mv_cesa_cbc_des_alg,
2358c2ecf20Sopenharmony_ci	&mv_cesa_ecb_des3_ede_alg,
2368c2ecf20Sopenharmony_ci	&mv_cesa_cbc_des3_ede_alg,
2378c2ecf20Sopenharmony_ci	&mv_cesa_ecb_aes_alg,
2388c2ecf20Sopenharmony_ci	&mv_cesa_cbc_aes_alg,
2398c2ecf20Sopenharmony_ci};
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic struct ahash_alg *orion_ahash_algs[] = {
2428c2ecf20Sopenharmony_ci	&mv_md5_alg,
2438c2ecf20Sopenharmony_ci	&mv_sha1_alg,
2448c2ecf20Sopenharmony_ci	&mv_ahmac_md5_alg,
2458c2ecf20Sopenharmony_ci	&mv_ahmac_sha1_alg,
2468c2ecf20Sopenharmony_ci};
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic struct skcipher_alg *armada_370_cipher_algs[] = {
2498c2ecf20Sopenharmony_ci	&mv_cesa_ecb_des_alg,
2508c2ecf20Sopenharmony_ci	&mv_cesa_cbc_des_alg,
2518c2ecf20Sopenharmony_ci	&mv_cesa_ecb_des3_ede_alg,
2528c2ecf20Sopenharmony_ci	&mv_cesa_cbc_des3_ede_alg,
2538c2ecf20Sopenharmony_ci	&mv_cesa_ecb_aes_alg,
2548c2ecf20Sopenharmony_ci	&mv_cesa_cbc_aes_alg,
2558c2ecf20Sopenharmony_ci};
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic struct ahash_alg *armada_370_ahash_algs[] = {
2588c2ecf20Sopenharmony_ci	&mv_md5_alg,
2598c2ecf20Sopenharmony_ci	&mv_sha1_alg,
2608c2ecf20Sopenharmony_ci	&mv_sha256_alg,
2618c2ecf20Sopenharmony_ci	&mv_ahmac_md5_alg,
2628c2ecf20Sopenharmony_ci	&mv_ahmac_sha1_alg,
2638c2ecf20Sopenharmony_ci	&mv_ahmac_sha256_alg,
2648c2ecf20Sopenharmony_ci};
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic const struct mv_cesa_caps orion_caps = {
2678c2ecf20Sopenharmony_ci	.nengines = 1,
2688c2ecf20Sopenharmony_ci	.cipher_algs = orion_cipher_algs,
2698c2ecf20Sopenharmony_ci	.ncipher_algs = ARRAY_SIZE(orion_cipher_algs),
2708c2ecf20Sopenharmony_ci	.ahash_algs = orion_ahash_algs,
2718c2ecf20Sopenharmony_ci	.nahash_algs = ARRAY_SIZE(orion_ahash_algs),
2728c2ecf20Sopenharmony_ci	.has_tdma = false,
2738c2ecf20Sopenharmony_ci};
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cistatic const struct mv_cesa_caps kirkwood_caps = {
2768c2ecf20Sopenharmony_ci	.nengines = 1,
2778c2ecf20Sopenharmony_ci	.cipher_algs = orion_cipher_algs,
2788c2ecf20Sopenharmony_ci	.ncipher_algs = ARRAY_SIZE(orion_cipher_algs),
2798c2ecf20Sopenharmony_ci	.ahash_algs = orion_ahash_algs,
2808c2ecf20Sopenharmony_ci	.nahash_algs = ARRAY_SIZE(orion_ahash_algs),
2818c2ecf20Sopenharmony_ci	.has_tdma = true,
2828c2ecf20Sopenharmony_ci};
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_cistatic const struct mv_cesa_caps armada_370_caps = {
2858c2ecf20Sopenharmony_ci	.nengines = 1,
2868c2ecf20Sopenharmony_ci	.cipher_algs = armada_370_cipher_algs,
2878c2ecf20Sopenharmony_ci	.ncipher_algs = ARRAY_SIZE(armada_370_cipher_algs),
2888c2ecf20Sopenharmony_ci	.ahash_algs = armada_370_ahash_algs,
2898c2ecf20Sopenharmony_ci	.nahash_algs = ARRAY_SIZE(armada_370_ahash_algs),
2908c2ecf20Sopenharmony_ci	.has_tdma = true,
2918c2ecf20Sopenharmony_ci};
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic const struct mv_cesa_caps armada_xp_caps = {
2948c2ecf20Sopenharmony_ci	.nengines = 2,
2958c2ecf20Sopenharmony_ci	.cipher_algs = armada_370_cipher_algs,
2968c2ecf20Sopenharmony_ci	.ncipher_algs = ARRAY_SIZE(armada_370_cipher_algs),
2978c2ecf20Sopenharmony_ci	.ahash_algs = armada_370_ahash_algs,
2988c2ecf20Sopenharmony_ci	.nahash_algs = ARRAY_SIZE(armada_370_ahash_algs),
2998c2ecf20Sopenharmony_ci	.has_tdma = true,
3008c2ecf20Sopenharmony_ci};
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_cistatic const struct of_device_id mv_cesa_of_match_table[] = {
3038c2ecf20Sopenharmony_ci	{ .compatible = "marvell,orion-crypto", .data = &orion_caps },
3048c2ecf20Sopenharmony_ci	{ .compatible = "marvell,kirkwood-crypto", .data = &kirkwood_caps },
3058c2ecf20Sopenharmony_ci	{ .compatible = "marvell,dove-crypto", .data = &kirkwood_caps },
3068c2ecf20Sopenharmony_ci	{ .compatible = "marvell,armada-370-crypto", .data = &armada_370_caps },
3078c2ecf20Sopenharmony_ci	{ .compatible = "marvell,armada-xp-crypto", .data = &armada_xp_caps },
3088c2ecf20Sopenharmony_ci	{ .compatible = "marvell,armada-375-crypto", .data = &armada_xp_caps },
3098c2ecf20Sopenharmony_ci	{ .compatible = "marvell,armada-38x-crypto", .data = &armada_xp_caps },
3108c2ecf20Sopenharmony_ci	{}
3118c2ecf20Sopenharmony_ci};
3128c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mv_cesa_of_match_table);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic void
3158c2ecf20Sopenharmony_cimv_cesa_conf_mbus_windows(struct mv_cesa_engine *engine,
3168c2ecf20Sopenharmony_ci			  const struct mbus_dram_target_info *dram)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	void __iomem *iobase = engine->regs;
3198c2ecf20Sopenharmony_ci	int i;
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++) {
3228c2ecf20Sopenharmony_ci		writel(0, iobase + CESA_TDMA_WINDOW_CTRL(i));
3238c2ecf20Sopenharmony_ci		writel(0, iobase + CESA_TDMA_WINDOW_BASE(i));
3248c2ecf20Sopenharmony_ci	}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	for (i = 0; i < dram->num_cs; i++) {
3278c2ecf20Sopenharmony_ci		const struct mbus_dram_window *cs = dram->cs + i;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci		writel(((cs->size - 1) & 0xffff0000) |
3308c2ecf20Sopenharmony_ci		       (cs->mbus_attr << 8) |
3318c2ecf20Sopenharmony_ci		       (dram->mbus_dram_target_id << 4) | 1,
3328c2ecf20Sopenharmony_ci		       iobase + CESA_TDMA_WINDOW_CTRL(i));
3338c2ecf20Sopenharmony_ci		writel(cs->base, iobase + CESA_TDMA_WINDOW_BASE(i));
3348c2ecf20Sopenharmony_ci	}
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistatic int mv_cesa_dev_dma_init(struct mv_cesa_dev *cesa)
3388c2ecf20Sopenharmony_ci{
3398c2ecf20Sopenharmony_ci	struct device *dev = cesa->dev;
3408c2ecf20Sopenharmony_ci	struct mv_cesa_dev_dma *dma;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	if (!cesa->caps->has_tdma)
3438c2ecf20Sopenharmony_ci		return 0;
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
3468c2ecf20Sopenharmony_ci	if (!dma)
3478c2ecf20Sopenharmony_ci		return -ENOMEM;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	dma->tdma_desc_pool = dmam_pool_create("tdma_desc", dev,
3508c2ecf20Sopenharmony_ci					sizeof(struct mv_cesa_tdma_desc),
3518c2ecf20Sopenharmony_ci					16, 0);
3528c2ecf20Sopenharmony_ci	if (!dma->tdma_desc_pool)
3538c2ecf20Sopenharmony_ci		return -ENOMEM;
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci	dma->op_pool = dmam_pool_create("cesa_op", dev,
3568c2ecf20Sopenharmony_ci					sizeof(struct mv_cesa_op_ctx), 16, 0);
3578c2ecf20Sopenharmony_ci	if (!dma->op_pool)
3588c2ecf20Sopenharmony_ci		return -ENOMEM;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	dma->cache_pool = dmam_pool_create("cesa_cache", dev,
3618c2ecf20Sopenharmony_ci					   CESA_MAX_HASH_BLOCK_SIZE, 1, 0);
3628c2ecf20Sopenharmony_ci	if (!dma->cache_pool)
3638c2ecf20Sopenharmony_ci		return -ENOMEM;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	dma->padding_pool = dmam_pool_create("cesa_padding", dev, 72, 1, 0);
3668c2ecf20Sopenharmony_ci	if (!dma->padding_pool)
3678c2ecf20Sopenharmony_ci		return -ENOMEM;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	cesa->dma = dma;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	return 0;
3728c2ecf20Sopenharmony_ci}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_cistatic int mv_cesa_get_sram(struct platform_device *pdev, int idx)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	struct mv_cesa_dev *cesa = platform_get_drvdata(pdev);
3778c2ecf20Sopenharmony_ci	struct mv_cesa_engine *engine = &cesa->engines[idx];
3788c2ecf20Sopenharmony_ci	const char *res_name = "sram";
3798c2ecf20Sopenharmony_ci	struct resource *res;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	engine->pool = of_gen_pool_get(cesa->dev->of_node,
3828c2ecf20Sopenharmony_ci				       "marvell,crypto-srams", idx);
3838c2ecf20Sopenharmony_ci	if (engine->pool) {
3848c2ecf20Sopenharmony_ci		engine->sram = gen_pool_dma_alloc(engine->pool,
3858c2ecf20Sopenharmony_ci						  cesa->sram_size,
3868c2ecf20Sopenharmony_ci						  &engine->sram_dma);
3878c2ecf20Sopenharmony_ci		if (engine->sram)
3888c2ecf20Sopenharmony_ci			return 0;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci		engine->pool = NULL;
3918c2ecf20Sopenharmony_ci		return -ENOMEM;
3928c2ecf20Sopenharmony_ci	}
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	if (cesa->caps->nengines > 1) {
3958c2ecf20Sopenharmony_ci		if (!idx)
3968c2ecf20Sopenharmony_ci			res_name = "sram0";
3978c2ecf20Sopenharmony_ci		else
3988c2ecf20Sopenharmony_ci			res_name = "sram1";
3998c2ecf20Sopenharmony_ci	}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
4028c2ecf20Sopenharmony_ci					   res_name);
4038c2ecf20Sopenharmony_ci	if (!res || resource_size(res) < cesa->sram_size)
4048c2ecf20Sopenharmony_ci		return -EINVAL;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	engine->sram = devm_ioremap_resource(cesa->dev, res);
4078c2ecf20Sopenharmony_ci	if (IS_ERR(engine->sram))
4088c2ecf20Sopenharmony_ci		return PTR_ERR(engine->sram);
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	engine->sram_dma = dma_map_resource(cesa->dev, res->start,
4118c2ecf20Sopenharmony_ci					    cesa->sram_size,
4128c2ecf20Sopenharmony_ci					    DMA_BIDIRECTIONAL, 0);
4138c2ecf20Sopenharmony_ci	if (dma_mapping_error(cesa->dev, engine->sram_dma))
4148c2ecf20Sopenharmony_ci		return -ENOMEM;
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	return 0;
4178c2ecf20Sopenharmony_ci}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cistatic void mv_cesa_put_sram(struct platform_device *pdev, int idx)
4208c2ecf20Sopenharmony_ci{
4218c2ecf20Sopenharmony_ci	struct mv_cesa_dev *cesa = platform_get_drvdata(pdev);
4228c2ecf20Sopenharmony_ci	struct mv_cesa_engine *engine = &cesa->engines[idx];
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	if (engine->pool)
4258c2ecf20Sopenharmony_ci		gen_pool_free(engine->pool, (unsigned long)engine->sram,
4268c2ecf20Sopenharmony_ci			      cesa->sram_size);
4278c2ecf20Sopenharmony_ci	else
4288c2ecf20Sopenharmony_ci		dma_unmap_resource(cesa->dev, engine->sram_dma,
4298c2ecf20Sopenharmony_ci				   cesa->sram_size, DMA_BIDIRECTIONAL, 0);
4308c2ecf20Sopenharmony_ci}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_cistatic int mv_cesa_probe(struct platform_device *pdev)
4338c2ecf20Sopenharmony_ci{
4348c2ecf20Sopenharmony_ci	const struct mv_cesa_caps *caps = &orion_caps;
4358c2ecf20Sopenharmony_ci	const struct mbus_dram_target_info *dram;
4368c2ecf20Sopenharmony_ci	const struct of_device_id *match;
4378c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
4388c2ecf20Sopenharmony_ci	struct mv_cesa_dev *cesa;
4398c2ecf20Sopenharmony_ci	struct mv_cesa_engine *engines;
4408c2ecf20Sopenharmony_ci	int irq, ret, i, cpu;
4418c2ecf20Sopenharmony_ci	u32 sram_size;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	if (cesa_dev) {
4448c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Only one CESA device authorized\n");
4458c2ecf20Sopenharmony_ci		return -EEXIST;
4468c2ecf20Sopenharmony_ci	}
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci	if (dev->of_node) {
4498c2ecf20Sopenharmony_ci		match = of_match_node(mv_cesa_of_match_table, dev->of_node);
4508c2ecf20Sopenharmony_ci		if (!match || !match->data)
4518c2ecf20Sopenharmony_ci			return -ENOTSUPP;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci		caps = match->data;
4548c2ecf20Sopenharmony_ci	}
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	cesa = devm_kzalloc(dev, sizeof(*cesa), GFP_KERNEL);
4578c2ecf20Sopenharmony_ci	if (!cesa)
4588c2ecf20Sopenharmony_ci		return -ENOMEM;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	cesa->caps = caps;
4618c2ecf20Sopenharmony_ci	cesa->dev = dev;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	sram_size = CESA_SA_DEFAULT_SRAM_SIZE;
4648c2ecf20Sopenharmony_ci	of_property_read_u32(cesa->dev->of_node, "marvell,crypto-sram-size",
4658c2ecf20Sopenharmony_ci			     &sram_size);
4668c2ecf20Sopenharmony_ci	if (sram_size < CESA_SA_MIN_SRAM_SIZE)
4678c2ecf20Sopenharmony_ci		sram_size = CESA_SA_MIN_SRAM_SIZE;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	cesa->sram_size = sram_size;
4708c2ecf20Sopenharmony_ci	cesa->engines = devm_kcalloc(dev, caps->nengines, sizeof(*engines),
4718c2ecf20Sopenharmony_ci				     GFP_KERNEL);
4728c2ecf20Sopenharmony_ci	if (!cesa->engines)
4738c2ecf20Sopenharmony_ci		return -ENOMEM;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	spin_lock_init(&cesa->lock);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	cesa->regs = devm_platform_ioremap_resource_byname(pdev, "regs");
4788c2ecf20Sopenharmony_ci	if (IS_ERR(cesa->regs))
4798c2ecf20Sopenharmony_ci		return PTR_ERR(cesa->regs);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	ret = mv_cesa_dev_dma_init(cesa);
4828c2ecf20Sopenharmony_ci	if (ret)
4838c2ecf20Sopenharmony_ci		return ret;
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	dram = mv_mbus_dram_info_nooverlap();
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, cesa);
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	for (i = 0; i < caps->nengines; i++) {
4908c2ecf20Sopenharmony_ci		struct mv_cesa_engine *engine = &cesa->engines[i];
4918c2ecf20Sopenharmony_ci		char res_name[7];
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci		engine->id = i;
4948c2ecf20Sopenharmony_ci		spin_lock_init(&engine->lock);
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci		ret = mv_cesa_get_sram(pdev, i);
4978c2ecf20Sopenharmony_ci		if (ret)
4988c2ecf20Sopenharmony_ci			goto err_cleanup;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci		irq = platform_get_irq(pdev, i);
5018c2ecf20Sopenharmony_ci		if (irq < 0) {
5028c2ecf20Sopenharmony_ci			ret = irq;
5038c2ecf20Sopenharmony_ci			goto err_cleanup;
5048c2ecf20Sopenharmony_ci		}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci		engine->irq = irq;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci		/*
5098c2ecf20Sopenharmony_ci		 * Not all platforms can gate the CESA clocks: do not complain
5108c2ecf20Sopenharmony_ci		 * if the clock does not exist.
5118c2ecf20Sopenharmony_ci		 */
5128c2ecf20Sopenharmony_ci		snprintf(res_name, sizeof(res_name), "cesa%d", i);
5138c2ecf20Sopenharmony_ci		engine->clk = devm_clk_get(dev, res_name);
5148c2ecf20Sopenharmony_ci		if (IS_ERR(engine->clk)) {
5158c2ecf20Sopenharmony_ci			engine->clk = devm_clk_get(dev, NULL);
5168c2ecf20Sopenharmony_ci			if (IS_ERR(engine->clk))
5178c2ecf20Sopenharmony_ci				engine->clk = NULL;
5188c2ecf20Sopenharmony_ci		}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci		snprintf(res_name, sizeof(res_name), "cesaz%d", i);
5218c2ecf20Sopenharmony_ci		engine->zclk = devm_clk_get(dev, res_name);
5228c2ecf20Sopenharmony_ci		if (IS_ERR(engine->zclk))
5238c2ecf20Sopenharmony_ci			engine->zclk = NULL;
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci		ret = clk_prepare_enable(engine->clk);
5268c2ecf20Sopenharmony_ci		if (ret)
5278c2ecf20Sopenharmony_ci			goto err_cleanup;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci		ret = clk_prepare_enable(engine->zclk);
5308c2ecf20Sopenharmony_ci		if (ret)
5318c2ecf20Sopenharmony_ci			goto err_cleanup;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci		engine->regs = cesa->regs + CESA_ENGINE_OFF(i);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci		if (dram && cesa->caps->has_tdma)
5368c2ecf20Sopenharmony_ci			mv_cesa_conf_mbus_windows(engine, dram);
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci		writel(0, engine->regs + CESA_SA_INT_STATUS);
5398c2ecf20Sopenharmony_ci		writel(CESA_SA_CFG_STOP_DIG_ERR,
5408c2ecf20Sopenharmony_ci		       engine->regs + CESA_SA_CFG);
5418c2ecf20Sopenharmony_ci		writel(engine->sram_dma & CESA_SA_SRAM_MSK,
5428c2ecf20Sopenharmony_ci		       engine->regs + CESA_SA_DESC_P0);
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci		ret = devm_request_threaded_irq(dev, irq, NULL, mv_cesa_int,
5458c2ecf20Sopenharmony_ci						IRQF_ONESHOT,
5468c2ecf20Sopenharmony_ci						dev_name(&pdev->dev),
5478c2ecf20Sopenharmony_ci						engine);
5488c2ecf20Sopenharmony_ci		if (ret)
5498c2ecf20Sopenharmony_ci			goto err_cleanup;
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci		/* Set affinity */
5528c2ecf20Sopenharmony_ci		cpu = cpumask_local_spread(engine->id, NUMA_NO_NODE);
5538c2ecf20Sopenharmony_ci		irq_set_affinity_hint(irq, get_cpu_mask(cpu));
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci		crypto_init_queue(&engine->queue, CESA_CRYPTO_DEFAULT_MAX_QLEN);
5568c2ecf20Sopenharmony_ci		atomic_set(&engine->load, 0);
5578c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&engine->complete_queue);
5588c2ecf20Sopenharmony_ci	}
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	cesa_dev = cesa;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	ret = mv_cesa_add_algs(cesa);
5638c2ecf20Sopenharmony_ci	if (ret) {
5648c2ecf20Sopenharmony_ci		cesa_dev = NULL;
5658c2ecf20Sopenharmony_ci		goto err_cleanup;
5668c2ecf20Sopenharmony_ci	}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	dev_info(dev, "CESA device successfully registered\n");
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	return 0;
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_cierr_cleanup:
5738c2ecf20Sopenharmony_ci	for (i = 0; i < caps->nengines; i++) {
5748c2ecf20Sopenharmony_ci		clk_disable_unprepare(cesa->engines[i].zclk);
5758c2ecf20Sopenharmony_ci		clk_disable_unprepare(cesa->engines[i].clk);
5768c2ecf20Sopenharmony_ci		mv_cesa_put_sram(pdev, i);
5778c2ecf20Sopenharmony_ci		if (cesa->engines[i].irq > 0)
5788c2ecf20Sopenharmony_ci			irq_set_affinity_hint(cesa->engines[i].irq, NULL);
5798c2ecf20Sopenharmony_ci	}
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	return ret;
5828c2ecf20Sopenharmony_ci}
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_cistatic int mv_cesa_remove(struct platform_device *pdev)
5858c2ecf20Sopenharmony_ci{
5868c2ecf20Sopenharmony_ci	struct mv_cesa_dev *cesa = platform_get_drvdata(pdev);
5878c2ecf20Sopenharmony_ci	int i;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	mv_cesa_remove_algs(cesa);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	for (i = 0; i < cesa->caps->nengines; i++) {
5928c2ecf20Sopenharmony_ci		clk_disable_unprepare(cesa->engines[i].zclk);
5938c2ecf20Sopenharmony_ci		clk_disable_unprepare(cesa->engines[i].clk);
5948c2ecf20Sopenharmony_ci		mv_cesa_put_sram(pdev, i);
5958c2ecf20Sopenharmony_ci		irq_set_affinity_hint(cesa->engines[i].irq, NULL);
5968c2ecf20Sopenharmony_ci	}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	return 0;
5998c2ecf20Sopenharmony_ci}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_cistatic const struct platform_device_id mv_cesa_plat_id_table[] = {
6028c2ecf20Sopenharmony_ci	{ .name = "mv_crypto" },
6038c2ecf20Sopenharmony_ci	{ /* sentinel */ },
6048c2ecf20Sopenharmony_ci};
6058c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(platform, mv_cesa_plat_id_table);
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_cistatic struct platform_driver marvell_cesa = {
6088c2ecf20Sopenharmony_ci	.probe		= mv_cesa_probe,
6098c2ecf20Sopenharmony_ci	.remove		= mv_cesa_remove,
6108c2ecf20Sopenharmony_ci	.id_table	= mv_cesa_plat_id_table,
6118c2ecf20Sopenharmony_ci	.driver		= {
6128c2ecf20Sopenharmony_ci		.name	= "marvell-cesa",
6138c2ecf20Sopenharmony_ci		.of_match_table = mv_cesa_of_match_table,
6148c2ecf20Sopenharmony_ci	},
6158c2ecf20Sopenharmony_ci};
6168c2ecf20Sopenharmony_cimodule_platform_driver(marvell_cesa);
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:mv_crypto");
6198c2ecf20Sopenharmony_ciMODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
6208c2ecf20Sopenharmony_ciMODULE_AUTHOR("Arnaud Ebalard <arno@natisbad.org>");
6218c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Support for Marvell's cryptographic engine");
6228c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
623