xref: /linux/drivers/crypto/ccree/cc_driver.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
14c3f9727SGilad Ben-Yossef // SPDX-License-Identifier: GPL-2.0
2dcf6285dSGilad Ben-Yossef /* Copyright (C) 2012-2019 ARM Limited or its affiliates. */
34c3f9727SGilad Ben-Yossef 
44c3f9727SGilad Ben-Yossef #include <linux/kernel.h>
54c3f9727SGilad Ben-Yossef #include <linux/module.h>
64c3f9727SGilad Ben-Yossef 
74c3f9727SGilad Ben-Yossef #include <linux/crypto.h>
84c3f9727SGilad Ben-Yossef #include <linux/moduleparam.h>
94c3f9727SGilad Ben-Yossef #include <linux/types.h>
104c3f9727SGilad Ben-Yossef #include <linux/interrupt.h>
114c3f9727SGilad Ben-Yossef #include <linux/platform_device.h>
124c3f9727SGilad Ben-Yossef #include <linux/slab.h>
134c3f9727SGilad Ben-Yossef #include <linux/spinlock.h>
144c3f9727SGilad Ben-Yossef #include <linux/of.h>
154c3f9727SGilad Ben-Yossef #include <linux/clk.h>
164c3f9727SGilad Ben-Yossef #include <linux/of_address.h>
178c7849a3SGeert Uytterhoeven #include <linux/pm_runtime.h>
184c3f9727SGilad Ben-Yossef 
194c3f9727SGilad Ben-Yossef #include "cc_driver.h"
204c3f9727SGilad Ben-Yossef #include "cc_request_mgr.h"
214c3f9727SGilad Ben-Yossef #include "cc_buffer_mgr.h"
224c3f9727SGilad Ben-Yossef #include "cc_debugfs.h"
2363ee04c8SGilad Ben-Yossef #include "cc_cipher.h"
24ff27e85aSGilad Ben-Yossef #include "cc_aead.h"
2563893811SGilad Ben-Yossef #include "cc_hash.h"
264c3f9727SGilad Ben-Yossef #include "cc_sram_mgr.h"
274c3f9727SGilad Ben-Yossef #include "cc_pm.h"
28ab8ec965SGilad Ben-Yossef #include "cc_fips.h"
294c3f9727SGilad Ben-Yossef 
304c3f9727SGilad Ben-Yossef bool cc_dump_desc;
314c3f9727SGilad Ben-Yossef module_param_named(dump_desc, cc_dump_desc, bool, 0600);
324c3f9727SGilad Ben-Yossef MODULE_PARM_DESC(cc_dump_desc, "Dump descriptors to kernel log as debugging aid");
334c3f9727SGilad Ben-Yossef bool cc_dump_bytes;
344c3f9727SGilad Ben-Yossef module_param_named(dump_bytes, cc_dump_bytes, bool, 0600);
354c3f9727SGilad Ben-Yossef MODULE_PARM_DESC(cc_dump_bytes, "Dump buffers to kernel log as debugging aid");
364c3f9727SGilad Ben-Yossef 
37181a9096SYueHaibing static bool cc_sec_disable;
38f98f6e21SGilad Ben-Yossef module_param_named(sec_disable, cc_sec_disable, bool, 0600);
39f98f6e21SGilad Ben-Yossef MODULE_PARM_DESC(cc_sec_disable, "Disable security functions");
40f98f6e21SGilad Ben-Yossef 
4127b3b22dSGilad Ben-Yossef struct cc_hw_data {
4227b3b22dSGilad Ben-Yossef 	char *name;
4327b3b22dSGilad Ben-Yossef 	enum cc_hw_rev rev;
4427b3b22dSGilad Ben-Yossef 	u32 sig;
45dcf6285dSGilad Ben-Yossef 	u32 cidr_0123;
46dcf6285dSGilad Ben-Yossef 	u32 pidr_0124;
471c876a90SGilad Ben-Yossef 	int std_bodies;
4827b3b22dSGilad Ben-Yossef };
4927b3b22dSGilad Ben-Yossef 
50dcf6285dSGilad Ben-Yossef #define CC_NUM_IDRS 4
51d84f6269SOfir Drang #define CC_HW_RESET_LOOP_COUNT 10
52dcf6285dSGilad Ben-Yossef 
53dcf6285dSGilad Ben-Yossef /* Note: PIDR3 holds CMOD/Rev so ignored for HW identification purposes */
54dcf6285dSGilad Ben-Yossef static const u32 pidr_0124_offsets[CC_NUM_IDRS] = {
55dcf6285dSGilad Ben-Yossef 	CC_REG(PERIPHERAL_ID_0), CC_REG(PERIPHERAL_ID_1),
56dcf6285dSGilad Ben-Yossef 	CC_REG(PERIPHERAL_ID_2), CC_REG(PERIPHERAL_ID_4)
57dcf6285dSGilad Ben-Yossef };
58dcf6285dSGilad Ben-Yossef 
59dcf6285dSGilad Ben-Yossef static const u32 cidr_0123_offsets[CC_NUM_IDRS] = {
60dcf6285dSGilad Ben-Yossef 	CC_REG(COMPONENT_ID_0), CC_REG(COMPONENT_ID_1),
61dcf6285dSGilad Ben-Yossef 	CC_REG(COMPONENT_ID_2), CC_REG(COMPONENT_ID_3)
62dcf6285dSGilad Ben-Yossef };
63dcf6285dSGilad Ben-Yossef 
6427b3b22dSGilad Ben-Yossef /* Hardware revisions defs. */
6527b3b22dSGilad Ben-Yossef 
661c876a90SGilad Ben-Yossef /* The 703 is a OSCCA only variant of the 713 */
671c876a90SGilad Ben-Yossef static const struct cc_hw_data cc703_hw = {
68dcf6285dSGilad Ben-Yossef 	.name = "703", .rev = CC_HW_REV_713, .cidr_0123 = 0xB105F00DU,
69dcf6285dSGilad Ben-Yossef 	.pidr_0124 = 0x040BB0D0U, .std_bodies = CC_STD_OSCCA
701c876a90SGilad Ben-Yossef };
711c876a90SGilad Ben-Yossef 
72e40fdb50SGilad Ben-Yossef static const struct cc_hw_data cc713_hw = {
73dcf6285dSGilad Ben-Yossef 	.name = "713", .rev = CC_HW_REV_713, .cidr_0123 = 0xB105F00DU,
74dcf6285dSGilad Ben-Yossef 	.pidr_0124 = 0x040BB0D0U, .std_bodies = CC_STD_ALL
75e40fdb50SGilad Ben-Yossef };
76e40fdb50SGilad Ben-Yossef 
7727b3b22dSGilad Ben-Yossef static const struct cc_hw_data cc712_hw = {
781c876a90SGilad Ben-Yossef 	.name = "712", .rev = CC_HW_REV_712, .sig =  0xDCC71200U,
791c876a90SGilad Ben-Yossef 	.std_bodies = CC_STD_ALL
8027b3b22dSGilad Ben-Yossef };
8127b3b22dSGilad Ben-Yossef 
8227b3b22dSGilad Ben-Yossef static const struct cc_hw_data cc710_hw = {
831c876a90SGilad Ben-Yossef 	.name = "710", .rev = CC_HW_REV_710, .sig =  0xDCC63200U,
841c876a90SGilad Ben-Yossef 	.std_bodies = CC_STD_ALL
8527b3b22dSGilad Ben-Yossef };
8627b3b22dSGilad Ben-Yossef 
8727b3b22dSGilad Ben-Yossef static const struct cc_hw_data cc630p_hw = {
881c876a90SGilad Ben-Yossef 	.name = "630P", .rev = CC_HW_REV_630, .sig = 0xDCC63000U,
891c876a90SGilad Ben-Yossef 	.std_bodies = CC_STD_ALL
9027b3b22dSGilad Ben-Yossef };
9127b3b22dSGilad Ben-Yossef 
9227b3b22dSGilad Ben-Yossef static const struct of_device_id arm_ccree_dev_of_match[] = {
931c876a90SGilad Ben-Yossef 	{ .compatible = "arm,cryptocell-703-ree", .data = &cc703_hw },
94e40fdb50SGilad Ben-Yossef 	{ .compatible = "arm,cryptocell-713-ree", .data = &cc713_hw },
9527b3b22dSGilad Ben-Yossef 	{ .compatible = "arm,cryptocell-712-ree", .data = &cc712_hw },
9627b3b22dSGilad Ben-Yossef 	{ .compatible = "arm,cryptocell-710-ree", .data = &cc710_hw },
9727b3b22dSGilad Ben-Yossef 	{ .compatible = "arm,cryptocell-630p-ree", .data = &cc630p_hw },
9827b3b22dSGilad Ben-Yossef 	{}
9927b3b22dSGilad Ben-Yossef };
10027b3b22dSGilad Ben-Yossef MODULE_DEVICE_TABLE(of, arm_ccree_dev_of_match);
10127b3b22dSGilad Ben-Yossef 
init_cc_cache_params(struct cc_drvdata * drvdata)10205c2a705SGilad Ben-Yossef static void init_cc_cache_params(struct cc_drvdata *drvdata)
10305c2a705SGilad Ben-Yossef {
10405c2a705SGilad Ben-Yossef 	struct device *dev = drvdata_to_dev(drvdata);
105cfd6fb45SArnd Bergmann 	u32 cache_params, ace_const, val;
106cfd6fb45SArnd Bergmann 	u64 mask;
10705c2a705SGilad Ben-Yossef 
10805c2a705SGilad Ben-Yossef 	/* compute CC_AXIM_CACHE_PARAMS */
10905c2a705SGilad Ben-Yossef 	cache_params = cc_ioread(drvdata, CC_REG(AXIM_CACHE_PARAMS));
11005c2a705SGilad Ben-Yossef 	dev_dbg(dev, "Cache params previous: 0x%08X\n", cache_params);
11105c2a705SGilad Ben-Yossef 
11205c2a705SGilad Ben-Yossef 	/* non cached or write-back, write allocate */
11305c2a705SGilad Ben-Yossef 	val = drvdata->coherent ? 0xb : 0x2;
11405c2a705SGilad Ben-Yossef 
11505c2a705SGilad Ben-Yossef 	mask = CC_GENMASK(CC_AXIM_CACHE_PARAMS_AWCACHE);
11605c2a705SGilad Ben-Yossef 	cache_params &= ~mask;
11705c2a705SGilad Ben-Yossef 	cache_params |= FIELD_PREP(mask, val);
11805c2a705SGilad Ben-Yossef 
11905c2a705SGilad Ben-Yossef 	mask = CC_GENMASK(CC_AXIM_CACHE_PARAMS_AWCACHE_LAST);
12005c2a705SGilad Ben-Yossef 	cache_params &= ~mask;
12105c2a705SGilad Ben-Yossef 	cache_params |= FIELD_PREP(mask, val);
12205c2a705SGilad Ben-Yossef 
12305c2a705SGilad Ben-Yossef 	mask = CC_GENMASK(CC_AXIM_CACHE_PARAMS_ARCACHE);
12405c2a705SGilad Ben-Yossef 	cache_params &= ~mask;
12505c2a705SGilad Ben-Yossef 	cache_params |= FIELD_PREP(mask, val);
12605c2a705SGilad Ben-Yossef 
12705c2a705SGilad Ben-Yossef 	drvdata->cache_params = cache_params;
12805c2a705SGilad Ben-Yossef 
12905c2a705SGilad Ben-Yossef 	dev_dbg(dev, "Cache params current: 0x%08X\n", cache_params);
13005c2a705SGilad Ben-Yossef 
13105c2a705SGilad Ben-Yossef 	if (drvdata->hw_rev <= CC_HW_REV_710)
13205c2a705SGilad Ben-Yossef 		return;
13305c2a705SGilad Ben-Yossef 
13405c2a705SGilad Ben-Yossef 	/* compute CC_AXIM_ACE_CONST */
13505c2a705SGilad Ben-Yossef 	ace_const = cc_ioread(drvdata, CC_REG(AXIM_ACE_CONST));
13605c2a705SGilad Ben-Yossef 	dev_dbg(dev, "ACE-const previous: 0x%08X\n", ace_const);
13705c2a705SGilad Ben-Yossef 
13805c2a705SGilad Ben-Yossef 	/* system or outer-sharable */
13905c2a705SGilad Ben-Yossef 	val = drvdata->coherent ? 0x2 : 0x3;
14005c2a705SGilad Ben-Yossef 
14105c2a705SGilad Ben-Yossef 	mask = CC_GENMASK(CC_AXIM_ACE_CONST_ARDOMAIN);
14205c2a705SGilad Ben-Yossef 	ace_const &= ~mask;
14305c2a705SGilad Ben-Yossef 	ace_const |= FIELD_PREP(mask, val);
14405c2a705SGilad Ben-Yossef 
14505c2a705SGilad Ben-Yossef 	mask = CC_GENMASK(CC_AXIM_ACE_CONST_AWDOMAIN);
14605c2a705SGilad Ben-Yossef 	ace_const &= ~mask;
14705c2a705SGilad Ben-Yossef 	ace_const |= FIELD_PREP(mask, val);
14805c2a705SGilad Ben-Yossef 
14905c2a705SGilad Ben-Yossef 	dev_dbg(dev, "ACE-const current: 0x%08X\n", ace_const);
15005c2a705SGilad Ben-Yossef 
15105c2a705SGilad Ben-Yossef 	drvdata->ace_const = ace_const;
15205c2a705SGilad Ben-Yossef }
15305c2a705SGilad Ben-Yossef 
cc_read_idr(struct cc_drvdata * drvdata,const u32 * idr_offsets)154dcf6285dSGilad Ben-Yossef static u32 cc_read_idr(struct cc_drvdata *drvdata, const u32 *idr_offsets)
155dcf6285dSGilad Ben-Yossef {
156dcf6285dSGilad Ben-Yossef 	int i;
157dcf6285dSGilad Ben-Yossef 	union {
158dcf6285dSGilad Ben-Yossef 		u8 regs[CC_NUM_IDRS];
159e59f755cSGilad Ben-Yossef 		__le32 val;
160dcf6285dSGilad Ben-Yossef 	} idr;
161dcf6285dSGilad Ben-Yossef 
162dcf6285dSGilad Ben-Yossef 	for (i = 0; i < CC_NUM_IDRS; ++i)
163dcf6285dSGilad Ben-Yossef 		idr.regs[i] = cc_ioread(drvdata, idr_offsets[i]);
164dcf6285dSGilad Ben-Yossef 
165dcf6285dSGilad Ben-Yossef 	return le32_to_cpu(idr.val);
166dcf6285dSGilad Ben-Yossef }
167dcf6285dSGilad Ben-Yossef 
__dump_byte_array(const char * name,const u8 * buf,size_t len)1684c3f9727SGilad Ben-Yossef void __dump_byte_array(const char *name, const u8 *buf, size_t len)
1694c3f9727SGilad Ben-Yossef {
1704c3f9727SGilad Ben-Yossef 	char prefix[64];
1714c3f9727SGilad Ben-Yossef 
1724c3f9727SGilad Ben-Yossef 	if (!buf)
1734c3f9727SGilad Ben-Yossef 		return;
1744c3f9727SGilad Ben-Yossef 
1754c3f9727SGilad Ben-Yossef 	snprintf(prefix, sizeof(prefix), "%s[%zu]: ", name, len);
1764c3f9727SGilad Ben-Yossef 
1774c3f9727SGilad Ben-Yossef 	print_hex_dump(KERN_DEBUG, prefix, DUMP_PREFIX_ADDRESS, 16, 1, buf,
1784c3f9727SGilad Ben-Yossef 		       len, false);
1794c3f9727SGilad Ben-Yossef }
1804c3f9727SGilad Ben-Yossef 
cc_isr(int irq,void * dev_id)1814c3f9727SGilad Ben-Yossef static irqreturn_t cc_isr(int irq, void *dev_id)
1824c3f9727SGilad Ben-Yossef {
1834c3f9727SGilad Ben-Yossef 	struct cc_drvdata *drvdata = (struct cc_drvdata *)dev_id;
1844c3f9727SGilad Ben-Yossef 	struct device *dev = drvdata_to_dev(drvdata);
1854c3f9727SGilad Ben-Yossef 	u32 irr;
1864c3f9727SGilad Ben-Yossef 	u32 imr;
1874c3f9727SGilad Ben-Yossef 
1884c3f9727SGilad Ben-Yossef 	/* STAT_OP_TYPE_GENERIC STAT_PHASE_0: Interrupt */
189e86eca41SHadar Gat 	/* if driver suspended return, probably shared interrupt */
1908f76b352SGeert Uytterhoeven 	if (pm_runtime_suspended(dev))
1913db617e7SOfir Drang 		return IRQ_NONE;
1924c3f9727SGilad Ben-Yossef 
1934c3f9727SGilad Ben-Yossef 	/* read the interrupt status */
1944c3f9727SGilad Ben-Yossef 	irr = cc_ioread(drvdata, CC_REG(HOST_IRR));
1954c3f9727SGilad Ben-Yossef 	dev_dbg(dev, "Got IRR=0x%08X\n", irr);
19627649c39SGilad Ben-Yossef 
19727649c39SGilad Ben-Yossef 	if (irr == 0) /* Probably shared interrupt line */
1984c3f9727SGilad Ben-Yossef 		return IRQ_NONE;
19927649c39SGilad Ben-Yossef 
2004c3f9727SGilad Ben-Yossef 	imr = cc_ioread(drvdata, CC_REG(HOST_IMR));
2014c3f9727SGilad Ben-Yossef 
2024c3f9727SGilad Ben-Yossef 	/* clear interrupt - must be before processing events */
2034c3f9727SGilad Ben-Yossef 	cc_iowrite(drvdata, CC_REG(HOST_ICR), irr);
2044c3f9727SGilad Ben-Yossef 
2054c3f9727SGilad Ben-Yossef 	drvdata->irq = irr;
2064c3f9727SGilad Ben-Yossef 	/* Completion interrupt - most probable */
207cadfd898SGilad Ben-Yossef 	if (irr & drvdata->comp_mask) {
208cadfd898SGilad Ben-Yossef 		/* Mask all completion interrupts - will be unmasked in
209cadfd898SGilad Ben-Yossef 		 * deferred service handler
2104c3f9727SGilad Ben-Yossef 		 */
211cadfd898SGilad Ben-Yossef 		cc_iowrite(drvdata, CC_REG(HOST_IMR), imr | drvdata->comp_mask);
212cadfd898SGilad Ben-Yossef 		irr &= ~drvdata->comp_mask;
2134c3f9727SGilad Ben-Yossef 		complete_request(drvdata);
2144c3f9727SGilad Ben-Yossef 	}
215ab8ec965SGilad Ben-Yossef #ifdef CONFIG_CRYPTO_FIPS
216ab8ec965SGilad Ben-Yossef 	/* TEE FIPS interrupt */
217ab8ec965SGilad Ben-Yossef 	if (irr & CC_GPR0_IRQ_MASK) {
218ab8ec965SGilad Ben-Yossef 		/* Mask interrupt - will be unmasked in Deferred service
219ab8ec965SGilad Ben-Yossef 		 * handler
220ab8ec965SGilad Ben-Yossef 		 */
221ab8ec965SGilad Ben-Yossef 		cc_iowrite(drvdata, CC_REG(HOST_IMR), imr | CC_GPR0_IRQ_MASK);
222ab8ec965SGilad Ben-Yossef 		irr &= ~CC_GPR0_IRQ_MASK;
223ab8ec965SGilad Ben-Yossef 		fips_handler(drvdata);
224ab8ec965SGilad Ben-Yossef 	}
225ab8ec965SGilad Ben-Yossef #endif
2264c3f9727SGilad Ben-Yossef 	/* AXI error interrupt */
2274c3f9727SGilad Ben-Yossef 	if (irr & CC_AXI_ERR_IRQ_MASK) {
2284c3f9727SGilad Ben-Yossef 		u32 axi_err;
2294c3f9727SGilad Ben-Yossef 
2304c3f9727SGilad Ben-Yossef 		/* Read the AXI error ID */
2314c3f9727SGilad Ben-Yossef 		axi_err = cc_ioread(drvdata, CC_REG(AXIM_MON_ERR));
2324c3f9727SGilad Ben-Yossef 		dev_dbg(dev, "AXI completion error: axim_mon_err=0x%08X\n",
2334c3f9727SGilad Ben-Yossef 			axi_err);
2344c3f9727SGilad Ben-Yossef 
2354c3f9727SGilad Ben-Yossef 		irr &= ~CC_AXI_ERR_IRQ_MASK;
2364c3f9727SGilad Ben-Yossef 	}
2374c3f9727SGilad Ben-Yossef 
2384c3f9727SGilad Ben-Yossef 	if (irr) {
2395c324a2fSGilad Ben-Yossef 		dev_dbg_ratelimited(dev, "IRR includes unknown cause bits (0x%08X)\n",
2404c3f9727SGilad Ben-Yossef 				    irr);
2414c3f9727SGilad Ben-Yossef 		/* Just warning */
2424c3f9727SGilad Ben-Yossef 	}
2434c3f9727SGilad Ben-Yossef 
2444c3f9727SGilad Ben-Yossef 	return IRQ_HANDLED;
2454c3f9727SGilad Ben-Yossef }
2464c3f9727SGilad Ben-Yossef 
cc_wait_for_reset_completion(struct cc_drvdata * drvdata)247d84f6269SOfir Drang bool cc_wait_for_reset_completion(struct cc_drvdata *drvdata)
248d84f6269SOfir Drang {
249d84f6269SOfir Drang 	unsigned int val;
250d84f6269SOfir Drang 	unsigned int i;
251d84f6269SOfir Drang 
252d84f6269SOfir Drang 	/* 712/710/63 has no reset completion indication, always return true */
253d84f6269SOfir Drang 	if (drvdata->hw_rev <= CC_HW_REV_712)
254d84f6269SOfir Drang 		return true;
255d84f6269SOfir Drang 
256d84f6269SOfir Drang 	for (i = 0; i < CC_HW_RESET_LOOP_COUNT; i++) {
257d84f6269SOfir Drang 		/* in cc7x3 NVM_IS_IDLE indicates that CC reset is
258d84f6269SOfir Drang 		 *  completed and device is fully functional
259d84f6269SOfir Drang 		 */
260d84f6269SOfir Drang 		val = cc_ioread(drvdata, CC_REG(NVM_IS_IDLE));
261d84f6269SOfir Drang 		if (val & CC_NVM_IS_IDLE_MASK) {
262d84f6269SOfir Drang 			/* hw indicate reset completed */
263d84f6269SOfir Drang 			return true;
264d84f6269SOfir Drang 		}
265d84f6269SOfir Drang 		/* allow scheduling other process on the processor */
266d84f6269SOfir Drang 		schedule();
267d84f6269SOfir Drang 	}
268d84f6269SOfir Drang 	/* reset not completed */
269d84f6269SOfir Drang 	return false;
270d84f6269SOfir Drang }
271d84f6269SOfir Drang 
init_cc_regs(struct cc_drvdata * drvdata)27205c2a705SGilad Ben-Yossef int init_cc_regs(struct cc_drvdata *drvdata)
2734c3f9727SGilad Ben-Yossef {
27405c2a705SGilad Ben-Yossef 	unsigned int val;
2754c3f9727SGilad Ben-Yossef 	struct device *dev = drvdata_to_dev(drvdata);
2764c3f9727SGilad Ben-Yossef 
2774c3f9727SGilad Ben-Yossef 	/* Unmask all AXI interrupt sources AXI_CFG1 register   */
2781fc16572SOfir Drang 	/* AXI interrupt config are obsoleted startign at cc7x3 */
2791fc16572SOfir Drang 	if (drvdata->hw_rev <= CC_HW_REV_712) {
2804c3f9727SGilad Ben-Yossef 		val = cc_ioread(drvdata, CC_REG(AXIM_CFG));
2814c3f9727SGilad Ben-Yossef 		cc_iowrite(drvdata, CC_REG(AXIM_CFG), val & ~CC_AXI_IRQ_MASK);
2824c3f9727SGilad Ben-Yossef 		dev_dbg(dev, "AXIM_CFG=0x%08X\n",
2834c3f9727SGilad Ben-Yossef 			cc_ioread(drvdata, CC_REG(AXIM_CFG)));
2841fc16572SOfir Drang 	}
2854c3f9727SGilad Ben-Yossef 
2864c3f9727SGilad Ben-Yossef 	/* Clear all pending interrupts */
2874c3f9727SGilad Ben-Yossef 	val = cc_ioread(drvdata, CC_REG(HOST_IRR));
2884c3f9727SGilad Ben-Yossef 	dev_dbg(dev, "IRR=0x%08X\n", val);
2894c3f9727SGilad Ben-Yossef 	cc_iowrite(drvdata, CC_REG(HOST_ICR), val);
2904c3f9727SGilad Ben-Yossef 
2914c3f9727SGilad Ben-Yossef 	/* Unmask relevant interrupt cause */
292cadfd898SGilad Ben-Yossef 	val = drvdata->comp_mask | CC_AXI_ERR_IRQ_MASK;
29327b3b22dSGilad Ben-Yossef 
29427b3b22dSGilad Ben-Yossef 	if (drvdata->hw_rev >= CC_HW_REV_712)
29527b3b22dSGilad Ben-Yossef 		val |= CC_GPR0_IRQ_MASK;
29627b3b22dSGilad Ben-Yossef 
29727b3b22dSGilad Ben-Yossef 	cc_iowrite(drvdata, CC_REG(HOST_IMR), ~val);
2984c3f9727SGilad Ben-Yossef 
29905c2a705SGilad Ben-Yossef 	cc_iowrite(drvdata, CC_REG(AXIM_CACHE_PARAMS), drvdata->cache_params);
30005c2a705SGilad Ben-Yossef 	if (drvdata->hw_rev >= CC_HW_REV_712)
30105c2a705SGilad Ben-Yossef 		cc_iowrite(drvdata, CC_REG(AXIM_ACE_CONST), drvdata->ace_const);
3024c3f9727SGilad Ben-Yossef 
3034c3f9727SGilad Ben-Yossef 	return 0;
3044c3f9727SGilad Ben-Yossef }
3054c3f9727SGilad Ben-Yossef 
init_cc_resources(struct platform_device * plat_dev)3064c3f9727SGilad Ben-Yossef static int init_cc_resources(struct platform_device *plat_dev)
3074c3f9727SGilad Ben-Yossef {
3084c3f9727SGilad Ben-Yossef 	struct resource *req_mem_cc_regs = NULL;
3094c3f9727SGilad Ben-Yossef 	struct cc_drvdata *new_drvdata;
3104c3f9727SGilad Ben-Yossef 	struct device *dev = &plat_dev->dev;
3114c3f9727SGilad Ben-Yossef 	struct device_node *np = dev->of_node;
312dcf6285dSGilad Ben-Yossef 	u32 val, hw_rev_pidr, sig_cidr;
3134c3f9727SGilad Ben-Yossef 	u64 dma_mask;
31427b3b22dSGilad Ben-Yossef 	const struct cc_hw_data *hw_rev;
31535f859fcSGilad Ben-Yossef 	struct clk *clk;
31633c4b310SGilad Ben-Yossef 	int irq;
3174c3f9727SGilad Ben-Yossef 	int rc = 0;
3184c3f9727SGilad Ben-Yossef 
3194c3f9727SGilad Ben-Yossef 	new_drvdata = devm_kzalloc(dev, sizeof(*new_drvdata), GFP_KERNEL);
3204c3f9727SGilad Ben-Yossef 	if (!new_drvdata)
3214c3f9727SGilad Ben-Yossef 		return -ENOMEM;
3224c3f9727SGilad Ben-Yossef 
3231b940e35SGeert Uytterhoeven 	hw_rev = of_device_get_match_data(dev);
32427b3b22dSGilad Ben-Yossef 	new_drvdata->hw_rev_name = hw_rev->name;
32527b3b22dSGilad Ben-Yossef 	new_drvdata->hw_rev = hw_rev->rev;
3261c876a90SGilad Ben-Yossef 	new_drvdata->std_bodies = hw_rev->std_bodies;
32727b3b22dSGilad Ben-Yossef 
32827b3b22dSGilad Ben-Yossef 	if (hw_rev->rev >= CC_HW_REV_712) {
32927b3b22dSGilad Ben-Yossef 		new_drvdata->axim_mon_offset = CC_REG(AXIM_MON_COMP);
330281a58c8SGilad Ben-Yossef 		new_drvdata->sig_offset = CC_REG(HOST_SIGNATURE_712);
331281a58c8SGilad Ben-Yossef 		new_drvdata->ver_offset = CC_REG(HOST_VERSION_712);
33227b3b22dSGilad Ben-Yossef 	} else {
33327b3b22dSGilad Ben-Yossef 		new_drvdata->axim_mon_offset = CC_REG(AXIM_MON_COMP8);
334281a58c8SGilad Ben-Yossef 		new_drvdata->sig_offset = CC_REG(HOST_SIGNATURE_630);
335281a58c8SGilad Ben-Yossef 		new_drvdata->ver_offset = CC_REG(HOST_VERSION_630);
33627b3b22dSGilad Ben-Yossef 	}
33727b3b22dSGilad Ben-Yossef 
338cadfd898SGilad Ben-Yossef 	new_drvdata->comp_mask = CC_COMP_IRQ_MASK;
339cadfd898SGilad Ben-Yossef 
3404c3f9727SGilad Ben-Yossef 	platform_set_drvdata(plat_dev, new_drvdata);
3414c3f9727SGilad Ben-Yossef 	new_drvdata->plat_dev = plat_dev;
3424c3f9727SGilad Ben-Yossef 
3432f272ef3SGeert Uytterhoeven 	clk = devm_clk_get_optional(dev, NULL);
344d83d631bSKrzysztof Kozlowski 	if (IS_ERR(clk))
345d83d631bSKrzysztof Kozlowski 		return dev_err_probe(dev, PTR_ERR(clk), "Error getting clock\n");
34635f859fcSGilad Ben-Yossef 	new_drvdata->clk = clk;
34735f859fcSGilad Ben-Yossef 
3484c3f9727SGilad Ben-Yossef 	new_drvdata->coherent = of_dma_is_coherent(np);
3494c3f9727SGilad Ben-Yossef 
3504c3f9727SGilad Ben-Yossef 	/* Get device resources */
3514c3f9727SGilad Ben-Yossef 	/* First CC registers space */
3524c3f9727SGilad Ben-Yossef 	/* Map registers space */
353e70a3298SYang Li 	new_drvdata->cc_base = devm_platform_get_and_ioremap_resource(plat_dev,
354e70a3298SYang Li 								      0, &req_mem_cc_regs);
355e0e638f7SYueHaibing 	if (IS_ERR(new_drvdata->cc_base))
3564c3f9727SGilad Ben-Yossef 		return PTR_ERR(new_drvdata->cc_base);
3574c3f9727SGilad Ben-Yossef 
3584c3f9727SGilad Ben-Yossef 	dev_dbg(dev, "Got MEM resource (%s): %pR\n", req_mem_cc_regs->name,
3594c3f9727SGilad Ben-Yossef 		req_mem_cc_regs);
3604c3f9727SGilad Ben-Yossef 	dev_dbg(dev, "CC registers mapped from %pa to 0x%p\n",
3614c3f9727SGilad Ben-Yossef 		&req_mem_cc_regs->start, new_drvdata->cc_base);
3624c3f9727SGilad Ben-Yossef 
3634c3f9727SGilad Ben-Yossef 	/* Then IRQ */
36433c4b310SGilad Ben-Yossef 	irq = platform_get_irq(plat_dev, 0);
36533c4b310SGilad Ben-Yossef 	if (irq < 0)
36633c4b310SGilad Ben-Yossef 		return irq;
3674c3f9727SGilad Ben-Yossef 
3684c3f9727SGilad Ben-Yossef 	init_completion(&new_drvdata->hw_queue_avail);
3694c3f9727SGilad Ben-Yossef 
370343ee6c4SGeert Uytterhoeven 	if (!dev->dma_mask)
371343ee6c4SGeert Uytterhoeven 		dev->dma_mask = &dev->coherent_dma_mask;
3724c3f9727SGilad Ben-Yossef 
3734c3f9727SGilad Ben-Yossef 	dma_mask = DMA_BIT_MASK(DMA_BIT_MASK_LEN);
374343ee6c4SGeert Uytterhoeven 	rc = dma_set_coherent_mask(dev, dma_mask);
3754c3f9727SGilad Ben-Yossef 	if (rc) {
376383ce25dSChristophe JAILLET 		dev_err(dev, "Failed in dma_set_coherent_mask, mask=%llx\n",
377383ce25dSChristophe JAILLET 			dma_mask);
3784c3f9727SGilad Ben-Yossef 		return rc;
3794c3f9727SGilad Ben-Yossef 	}
3804c3f9727SGilad Ben-Yossef 
3812f272ef3SGeert Uytterhoeven 	rc = clk_prepare_enable(new_drvdata->clk);
3824c3f9727SGilad Ben-Yossef 	if (rc) {
3834c3f9727SGilad Ben-Yossef 		dev_err(dev, "Failed to enable clock");
3844c3f9727SGilad Ben-Yossef 		return rc;
3854c3f9727SGilad Ben-Yossef 	}
3864c3f9727SGilad Ben-Yossef 
387cadfd898SGilad Ben-Yossef 	new_drvdata->sec_disabled = cc_sec_disable;
388cadfd898SGilad Ben-Yossef 
3898c7849a3SGeert Uytterhoeven 	pm_runtime_set_autosuspend_delay(dev, CC_SUSPEND_TIMEOUT);
3908c7849a3SGeert Uytterhoeven 	pm_runtime_use_autosuspend(dev);
3918c7849a3SGeert Uytterhoeven 	pm_runtime_set_active(dev);
3928c7849a3SGeert Uytterhoeven 	pm_runtime_enable(dev);
3938c7849a3SGeert Uytterhoeven 	rc = pm_runtime_get_sync(dev);
3948c7849a3SGeert Uytterhoeven 	if (rc < 0) {
3958c7849a3SGeert Uytterhoeven 		dev_err(dev, "pm_runtime_get_sync() failed: %d\n", rc);
3968c7849a3SGeert Uytterhoeven 		goto post_pm_err;
3978c7849a3SGeert Uytterhoeven 	}
3988c7849a3SGeert Uytterhoeven 
3993b1cbdacSGeert Uytterhoeven 	/* Wait for Cryptocell reset completion */
400d84f6269SOfir Drang 	if (!cc_wait_for_reset_completion(new_drvdata)) {
401d84f6269SOfir Drang 		dev_err(dev, "Cryptocell reset not completed");
402d84f6269SOfir Drang 	}
403d84f6269SOfir Drang 
404e40fdb50SGilad Ben-Yossef 	if (hw_rev->rev <= CC_HW_REV_712) {
4054c3f9727SGilad Ben-Yossef 		/* Verify correct mapping */
406f98f6e21SGilad Ben-Yossef 		val = cc_ioread(new_drvdata, new_drvdata->sig_offset);
407f98f6e21SGilad Ben-Yossef 		if (val != hw_rev->sig) {
4084c3f9727SGilad Ben-Yossef 			dev_err(dev, "Invalid CC signature: SIGNATURE=0x%08X != expected=0x%08X\n",
409f98f6e21SGilad Ben-Yossef 				val, hw_rev->sig);
4104c3f9727SGilad Ben-Yossef 			rc = -EINVAL;
4118c7849a3SGeert Uytterhoeven 			goto post_pm_err;
4124c3f9727SGilad Ben-Yossef 		}
413dcf6285dSGilad Ben-Yossef 		sig_cidr = val;
414dcf6285dSGilad Ben-Yossef 		hw_rev_pidr = cc_ioread(new_drvdata, new_drvdata->ver_offset);
415f98f6e21SGilad Ben-Yossef 	} else {
416dcf6285dSGilad Ben-Yossef 		/* Verify correct mapping */
417dcf6285dSGilad Ben-Yossef 		val = cc_read_idr(new_drvdata, pidr_0124_offsets);
418dcf6285dSGilad Ben-Yossef 		if (val != hw_rev->pidr_0124) {
419dcf6285dSGilad Ben-Yossef 			dev_err(dev, "Invalid CC PIDR: PIDR0124=0x%08X != expected=0x%08X\n",
420dcf6285dSGilad Ben-Yossef 				val,  hw_rev->pidr_0124);
421dcf6285dSGilad Ben-Yossef 			rc = -EINVAL;
4228c7849a3SGeert Uytterhoeven 			goto post_pm_err;
423dcf6285dSGilad Ben-Yossef 		}
424dcf6285dSGilad Ben-Yossef 		hw_rev_pidr = val;
425dcf6285dSGilad Ben-Yossef 
426dcf6285dSGilad Ben-Yossef 		val = cc_read_idr(new_drvdata, cidr_0123_offsets);
427dcf6285dSGilad Ben-Yossef 		if (val != hw_rev->cidr_0123) {
428dcf6285dSGilad Ben-Yossef 			dev_err(dev, "Invalid CC CIDR: CIDR0123=0x%08X != expected=0x%08X\n",
429dcf6285dSGilad Ben-Yossef 			val,  hw_rev->cidr_0123);
430dcf6285dSGilad Ben-Yossef 			rc = -EINVAL;
4318c7849a3SGeert Uytterhoeven 			goto post_pm_err;
432dcf6285dSGilad Ben-Yossef 		}
433dcf6285dSGilad Ben-Yossef 		sig_cidr = val;
434dcf6285dSGilad Ben-Yossef 
435303f99acSGilad Ben-Yossef 		/* Check HW engine configuration */
436303f99acSGilad Ben-Yossef 		val = cc_ioread(new_drvdata, CC_REG(HOST_REMOVE_INPUT_PINS));
437303f99acSGilad Ben-Yossef 		switch (val) {
438303f99acSGilad Ben-Yossef 		case CC_PINS_FULL:
439303f99acSGilad Ben-Yossef 			/* This is fine */
440303f99acSGilad Ben-Yossef 			break;
441303f99acSGilad Ben-Yossef 		case CC_PINS_SLIM:
442303f99acSGilad Ben-Yossef 			if (new_drvdata->std_bodies & CC_STD_NIST) {
443303f99acSGilad Ben-Yossef 				dev_warn(dev, "703 mode forced due to HW configuration.\n");
444303f99acSGilad Ben-Yossef 				new_drvdata->std_bodies = CC_STD_OSCCA;
445303f99acSGilad Ben-Yossef 			}
446303f99acSGilad Ben-Yossef 			break;
447303f99acSGilad Ben-Yossef 		default:
448aca24d48SColin Ian King 			dev_err(dev, "Unsupported engines configuration.\n");
449303f99acSGilad Ben-Yossef 			rc = -EINVAL;
4508c7849a3SGeert Uytterhoeven 			goto post_pm_err;
451303f99acSGilad Ben-Yossef 		}
452303f99acSGilad Ben-Yossef 
453dcf6285dSGilad Ben-Yossef 		/* Check security disable state */
454f98f6e21SGilad Ben-Yossef 		val = cc_ioread(new_drvdata, CC_REG(SECURITY_DISABLED));
455f98f6e21SGilad Ben-Yossef 		val &= CC_SECURITY_DISABLED_MASK;
456cadfd898SGilad Ben-Yossef 		new_drvdata->sec_disabled |= !!val;
457cadfd898SGilad Ben-Yossef 
458cadfd898SGilad Ben-Yossef 		if (!new_drvdata->sec_disabled) {
459cadfd898SGilad Ben-Yossef 			new_drvdata->comp_mask |= CC_CPP_SM4_ABORT_MASK;
460cadfd898SGilad Ben-Yossef 			if (new_drvdata->std_bodies & CC_STD_NIST)
461cadfd898SGilad Ben-Yossef 				new_drvdata->comp_mask |= CC_CPP_AES_ABORT_MASK;
462cadfd898SGilad Ben-Yossef 		}
463e40fdb50SGilad Ben-Yossef 	}
4644c3f9727SGilad Ben-Yossef 
465f98f6e21SGilad Ben-Yossef 	if (new_drvdata->sec_disabled)
466f98f6e21SGilad Ben-Yossef 		dev_info(dev, "Security Disabled mode is in effect. Security functions disabled.\n");
467f98f6e21SGilad Ben-Yossef 
4684c3f9727SGilad Ben-Yossef 	/* Display HW versions */
469dcf6285dSGilad Ben-Yossef 	dev_info(dev, "ARM CryptoCell %s Driver: HW version 0x%08X/0x%8X, Driver version %s\n",
470dcf6285dSGilad Ben-Yossef 		 hw_rev->name, hw_rev_pidr, sig_cidr, DRV_MODULE_VERSION);
4710b970d54Sofir.drang@arm.com 	/* register the driver isr function */
47233c4b310SGilad Ben-Yossef 	rc = devm_request_irq(dev, irq, cc_isr, IRQF_SHARED, "ccree",
47333c4b310SGilad Ben-Yossef 			      new_drvdata);
4740b970d54Sofir.drang@arm.com 	if (rc) {
47533c4b310SGilad Ben-Yossef 		dev_err(dev, "Could not register to interrupt %d\n", irq);
4768c7849a3SGeert Uytterhoeven 		goto post_pm_err;
4770b970d54Sofir.drang@arm.com 	}
47833c4b310SGilad Ben-Yossef 	dev_dbg(dev, "Registered to IRQ: %d\n", irq);
4794c3f9727SGilad Ben-Yossef 
48005c2a705SGilad Ben-Yossef 	init_cc_cache_params(new_drvdata);
48105c2a705SGilad Ben-Yossef 
48205c2a705SGilad Ben-Yossef 	rc = init_cc_regs(new_drvdata);
4834c3f9727SGilad Ben-Yossef 	if (rc) {
4844c3f9727SGilad Ben-Yossef 		dev_err(dev, "init_cc_regs failed\n");
4858c7849a3SGeert Uytterhoeven 		goto post_pm_err;
4864c3f9727SGilad Ben-Yossef 	}
4874c3f9727SGilad Ben-Yossef 
4884c3f9727SGilad Ben-Yossef 	rc = cc_debugfs_init(new_drvdata);
4894c3f9727SGilad Ben-Yossef 	if (rc) {
4904c3f9727SGilad Ben-Yossef 		dev_err(dev, "Failed registering debugfs interface\n");
4914c3f9727SGilad Ben-Yossef 		goto post_regs_err;
4924c3f9727SGilad Ben-Yossef 	}
4934c3f9727SGilad Ben-Yossef 
494ab8ec965SGilad Ben-Yossef 	rc = cc_fips_init(new_drvdata);
495ab8ec965SGilad Ben-Yossef 	if (rc) {
4967c06603eSHadar Gat 		dev_err(dev, "cc_fips_init failed 0x%x\n", rc);
497ab8ec965SGilad Ben-Yossef 		goto post_debugfs_err;
498ab8ec965SGilad Ben-Yossef 	}
4994c3f9727SGilad Ben-Yossef 	rc = cc_sram_mgr_init(new_drvdata);
5004c3f9727SGilad Ben-Yossef 	if (rc) {
5014c3f9727SGilad Ben-Yossef 		dev_err(dev, "cc_sram_mgr_init failed\n");
502ab8ec965SGilad Ben-Yossef 		goto post_fips_init_err;
5034c3f9727SGilad Ben-Yossef 	}
5044c3f9727SGilad Ben-Yossef 
5054c3f9727SGilad Ben-Yossef 	new_drvdata->mlli_sram_addr =
5064c3f9727SGilad Ben-Yossef 		cc_sram_alloc(new_drvdata, MAX_MLLI_BUFF_SIZE);
5074c3f9727SGilad Ben-Yossef 	if (new_drvdata->mlli_sram_addr == NULL_SRAM_ADDR) {
5084c3f9727SGilad Ben-Yossef 		rc = -ENOMEM;
509fc3b8c11SGeert Uytterhoeven 		goto post_fips_init_err;
5104c3f9727SGilad Ben-Yossef 	}
5114c3f9727SGilad Ben-Yossef 
5124c3f9727SGilad Ben-Yossef 	rc = cc_req_mgr_init(new_drvdata);
5134c3f9727SGilad Ben-Yossef 	if (rc) {
5144c3f9727SGilad Ben-Yossef 		dev_err(dev, "cc_req_mgr_init failed\n");
515fc3b8c11SGeert Uytterhoeven 		goto post_fips_init_err;
5164c3f9727SGilad Ben-Yossef 	}
5174c3f9727SGilad Ben-Yossef 
5184c3f9727SGilad Ben-Yossef 	rc = cc_buffer_mgr_init(new_drvdata);
5194c3f9727SGilad Ben-Yossef 	if (rc) {
5207c06603eSHadar Gat 		dev_err(dev, "cc_buffer_mgr_init failed\n");
5214c3f9727SGilad Ben-Yossef 		goto post_req_mgr_err;
5224c3f9727SGilad Ben-Yossef 	}
5234c3f9727SGilad Ben-Yossef 
524476c9ab7SGilad Ben-Yossef 	/* hash must be allocated first due to use of send_request_init()
525476c9ab7SGilad Ben-Yossef 	 * and dependency of AEAD on it
526476c9ab7SGilad Ben-Yossef 	 */
527476c9ab7SGilad Ben-Yossef 	rc = cc_hash_alloc(new_drvdata);
528476c9ab7SGilad Ben-Yossef 	if (rc) {
529476c9ab7SGilad Ben-Yossef 		dev_err(dev, "cc_hash_alloc failed\n");
530476c9ab7SGilad Ben-Yossef 		goto post_buf_mgr_err;
531476c9ab7SGilad Ben-Yossef 	}
532476c9ab7SGilad Ben-Yossef 
53363ee04c8SGilad Ben-Yossef 	/* Allocate crypto algs */
53463ee04c8SGilad Ben-Yossef 	rc = cc_cipher_alloc(new_drvdata);
53563ee04c8SGilad Ben-Yossef 	if (rc) {
53663ee04c8SGilad Ben-Yossef 		dev_err(dev, "cc_cipher_alloc failed\n");
537476c9ab7SGilad Ben-Yossef 		goto post_hash_err;
53863893811SGilad Ben-Yossef 	}
53963893811SGilad Ben-Yossef 
540ff27e85aSGilad Ben-Yossef 	rc = cc_aead_alloc(new_drvdata);
541ff27e85aSGilad Ben-Yossef 	if (rc) {
542ff27e85aSGilad Ben-Yossef 		dev_err(dev, "cc_aead_alloc failed\n");
543476c9ab7SGilad Ben-Yossef 		goto post_cipher_err;
544ff27e85aSGilad Ben-Yossef 	}
545ff27e85aSGilad Ben-Yossef 
546ab8ec965SGilad Ben-Yossef 	/* If we got here and FIPS mode is enabled
547ab8ec965SGilad Ben-Yossef 	 * it means all FIPS test passed, so let TEE
548ab8ec965SGilad Ben-Yossef 	 * know we're good.
549ab8ec965SGilad Ben-Yossef 	 */
550ab8ec965SGilad Ben-Yossef 	cc_set_ree_fips_status(new_drvdata, true);
551ab8ec965SGilad Ben-Yossef 
5528c7849a3SGeert Uytterhoeven 	pm_runtime_put(dev);
5534c3f9727SGilad Ben-Yossef 	return 0;
5544c3f9727SGilad Ben-Yossef 
55563893811SGilad Ben-Yossef post_cipher_err:
55663893811SGilad Ben-Yossef 	cc_cipher_free(new_drvdata);
557476c9ab7SGilad Ben-Yossef post_hash_err:
558476c9ab7SGilad Ben-Yossef 	cc_hash_free(new_drvdata);
5594c3f9727SGilad Ben-Yossef post_buf_mgr_err:
5604c3f9727SGilad Ben-Yossef 	 cc_buffer_mgr_fini(new_drvdata);
5614c3f9727SGilad Ben-Yossef post_req_mgr_err:
5624c3f9727SGilad Ben-Yossef 	cc_req_mgr_fini(new_drvdata);
563ab8ec965SGilad Ben-Yossef post_fips_init_err:
564ab8ec965SGilad Ben-Yossef 	cc_fips_fini(new_drvdata);
5654c3f9727SGilad Ben-Yossef post_debugfs_err:
5664c3f9727SGilad Ben-Yossef 	cc_debugfs_fini(new_drvdata);
5674c3f9727SGilad Ben-Yossef post_regs_err:
5684c3f9727SGilad Ben-Yossef 	fini_cc_regs(new_drvdata);
5698c7849a3SGeert Uytterhoeven post_pm_err:
5708c7849a3SGeert Uytterhoeven 	pm_runtime_put_noidle(dev);
5718c7849a3SGeert Uytterhoeven 	pm_runtime_disable(dev);
5728c7849a3SGeert Uytterhoeven 	pm_runtime_set_suspended(dev);
5732f272ef3SGeert Uytterhoeven 	clk_disable_unprepare(new_drvdata->clk);
5744c3f9727SGilad Ben-Yossef 	return rc;
5754c3f9727SGilad Ben-Yossef }
5764c3f9727SGilad Ben-Yossef 
fini_cc_regs(struct cc_drvdata * drvdata)5774c3f9727SGilad Ben-Yossef void fini_cc_regs(struct cc_drvdata *drvdata)
5784c3f9727SGilad Ben-Yossef {
5794c3f9727SGilad Ben-Yossef 	/* Mask all interrupts */
5804c3f9727SGilad Ben-Yossef 	cc_iowrite(drvdata, CC_REG(HOST_IMR), 0xFFFFFFFF);
5814c3f9727SGilad Ben-Yossef }
5824c3f9727SGilad Ben-Yossef 
cleanup_cc_resources(struct platform_device * plat_dev)5834c3f9727SGilad Ben-Yossef static void cleanup_cc_resources(struct platform_device *plat_dev)
5844c3f9727SGilad Ben-Yossef {
5858c7849a3SGeert Uytterhoeven 	struct device *dev = &plat_dev->dev;
5864c3f9727SGilad Ben-Yossef 	struct cc_drvdata *drvdata =
5874c3f9727SGilad Ben-Yossef 		(struct cc_drvdata *)platform_get_drvdata(plat_dev);
5884c3f9727SGilad Ben-Yossef 
589ff27e85aSGilad Ben-Yossef 	cc_aead_free(drvdata);
59063ee04c8SGilad Ben-Yossef 	cc_cipher_free(drvdata);
591476c9ab7SGilad Ben-Yossef 	cc_hash_free(drvdata);
5924c3f9727SGilad Ben-Yossef 	cc_buffer_mgr_fini(drvdata);
5934c3f9727SGilad Ben-Yossef 	cc_req_mgr_fini(drvdata);
594ab8ec965SGilad Ben-Yossef 	cc_fips_fini(drvdata);
5954c3f9727SGilad Ben-Yossef 	cc_debugfs_fini(drvdata);
5964c3f9727SGilad Ben-Yossef 	fini_cc_regs(drvdata);
5978c7849a3SGeert Uytterhoeven 	pm_runtime_put_noidle(dev);
5988c7849a3SGeert Uytterhoeven 	pm_runtime_disable(dev);
5998c7849a3SGeert Uytterhoeven 	pm_runtime_set_suspended(dev);
6002f272ef3SGeert Uytterhoeven 	clk_disable_unprepare(drvdata->clk);
6014c3f9727SGilad Ben-Yossef }
6024c3f9727SGilad Ben-Yossef 
cc_get_default_hash_len(struct cc_drvdata * drvdata)603f1e52fd0SYael Chemla unsigned int cc_get_default_hash_len(struct cc_drvdata *drvdata)
604f1e52fd0SYael Chemla {
605f1e52fd0SYael Chemla 	if (drvdata->hw_rev >= CC_HW_REV_712)
606f1e52fd0SYael Chemla 		return HASH_LEN_SIZE_712;
607f1e52fd0SYael Chemla 	else
608f1e52fd0SYael Chemla 		return HASH_LEN_SIZE_630;
609f1e52fd0SYael Chemla }
610f1e52fd0SYael Chemla 
ccree_probe(struct platform_device * plat_dev)6114c3f9727SGilad Ben-Yossef static int ccree_probe(struct platform_device *plat_dev)
6124c3f9727SGilad Ben-Yossef {
6134c3f9727SGilad Ben-Yossef 	int rc;
6144c3f9727SGilad Ben-Yossef 	struct device *dev = &plat_dev->dev;
6154c3f9727SGilad Ben-Yossef 
6164c3f9727SGilad Ben-Yossef 	/* Map registers space */
6174c3f9727SGilad Ben-Yossef 	rc = init_cc_resources(plat_dev);
6184c3f9727SGilad Ben-Yossef 	if (rc)
6194c3f9727SGilad Ben-Yossef 		return rc;
6204c3f9727SGilad Ben-Yossef 
6214c3f9727SGilad Ben-Yossef 	dev_info(dev, "ARM ccree device initialized\n");
6224c3f9727SGilad Ben-Yossef 
6234c3f9727SGilad Ben-Yossef 	return 0;
6244c3f9727SGilad Ben-Yossef }
6254c3f9727SGilad Ben-Yossef 
ccree_remove(struct platform_device * plat_dev)626*e7edfb41SUwe Kleine-König static void ccree_remove(struct platform_device *plat_dev)
6274c3f9727SGilad Ben-Yossef {
6284c3f9727SGilad Ben-Yossef 	struct device *dev = &plat_dev->dev;
6294c3f9727SGilad Ben-Yossef 
6304c3f9727SGilad Ben-Yossef 	dev_dbg(dev, "Releasing ccree resources...\n");
6314c3f9727SGilad Ben-Yossef 
6324c3f9727SGilad Ben-Yossef 	cleanup_cc_resources(plat_dev);
6334c3f9727SGilad Ben-Yossef 
6344c3f9727SGilad Ben-Yossef 	dev_info(dev, "ARM ccree device terminated\n");
6354c3f9727SGilad Ben-Yossef }
6364c3f9727SGilad Ben-Yossef 
6374c3f9727SGilad Ben-Yossef static struct platform_driver ccree_driver = {
6384c3f9727SGilad Ben-Yossef 	.driver = {
6394c3f9727SGilad Ben-Yossef 		   .name = "ccree",
6404c3f9727SGilad Ben-Yossef 		   .of_match_table = arm_ccree_dev_of_match,
6414c3f9727SGilad Ben-Yossef #ifdef CONFIG_PM
6424c3f9727SGilad Ben-Yossef 		   .pm = &ccree_pm,
6434c3f9727SGilad Ben-Yossef #endif
6444c3f9727SGilad Ben-Yossef 	},
6454c3f9727SGilad Ben-Yossef 	.probe = ccree_probe,
646*e7edfb41SUwe Kleine-König 	.remove_new = ccree_remove,
6474c3f9727SGilad Ben-Yossef };
6484c3f9727SGilad Ben-Yossef 
ccree_init(void)6494c3f9727SGilad Ben-Yossef static int __init ccree_init(void)
6504c3f9727SGilad Ben-Yossef {
6514f1c596dSGaosheng Cui 	int rc;
6524f1c596dSGaosheng Cui 
65354eedf0bSGreg Kroah-Hartman 	cc_debugfs_global_init();
6544c3f9727SGilad Ben-Yossef 
6554f1c596dSGaosheng Cui 	rc = platform_driver_register(&ccree_driver);
6564f1c596dSGaosheng Cui 	if (rc) {
6574f1c596dSGaosheng Cui 		cc_debugfs_global_fini();
6584f1c596dSGaosheng Cui 		return rc;
6594f1c596dSGaosheng Cui 	}
6604f1c596dSGaosheng Cui 
6614f1c596dSGaosheng Cui 	return 0;
6624c3f9727SGilad Ben-Yossef }
6634c3f9727SGilad Ben-Yossef module_init(ccree_init);
6644c3f9727SGilad Ben-Yossef 
ccree_exit(void)6654c3f9727SGilad Ben-Yossef static void __exit ccree_exit(void)
6664c3f9727SGilad Ben-Yossef {
6674c3f9727SGilad Ben-Yossef 	platform_driver_unregister(&ccree_driver);
6684c3f9727SGilad Ben-Yossef 	cc_debugfs_global_fini();
6694c3f9727SGilad Ben-Yossef }
6704c3f9727SGilad Ben-Yossef module_exit(ccree_exit);
6714c3f9727SGilad Ben-Yossef 
6724c3f9727SGilad Ben-Yossef /* Module description */
6734c3f9727SGilad Ben-Yossef MODULE_DESCRIPTION("ARM TrustZone CryptoCell REE Driver");
6744c3f9727SGilad Ben-Yossef MODULE_VERSION(DRV_MODULE_VERSION);
6754c3f9727SGilad Ben-Yossef MODULE_AUTHOR("ARM");
6764c3f9727SGilad Ben-Yossef MODULE_LICENSE("GPL v2");
677