cc_driver.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2012-2019 ARM Limited or its affiliates. */
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/crypto.h>
  6. #include <linux/moduleparam.h>
  7. #include <linux/types.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/of.h>
  13. #include <linux/clk.h>
  14. #include <linux/of_address.h>
  15. #include "cc_driver.h"
  16. #include "cc_request_mgr.h"
  17. #include "cc_buffer_mgr.h"
  18. #include "cc_debugfs.h"
  19. #include "cc_cipher.h"
  20. #include "cc_aead.h"
  21. #include "cc_hash.h"
  22. #include "cc_sram_mgr.h"
  23. #include "cc_pm.h"
  24. #include "cc_fips.h"
  25. bool cc_dump_desc;
  26. module_param_named(dump_desc, cc_dump_desc, bool, 0600);
  27. MODULE_PARM_DESC(cc_dump_desc, "Dump descriptors to kernel log as debugging aid");
  28. bool cc_dump_bytes;
  29. module_param_named(dump_bytes, cc_dump_bytes, bool, 0600);
  30. MODULE_PARM_DESC(cc_dump_bytes, "Dump buffers to kernel log as debugging aid");
  31. static bool cc_sec_disable;
  32. module_param_named(sec_disable, cc_sec_disable, bool, 0600);
  33. MODULE_PARM_DESC(cc_sec_disable, "Disable security functions");
  34. struct cc_hw_data {
  35. char *name;
  36. enum cc_hw_rev rev;
  37. u32 sig;
  38. u32 cidr_0123;
  39. u32 pidr_0124;
  40. int std_bodies;
  41. };
  42. #define CC_NUM_IDRS 4
  43. #define CC_HW_RESET_LOOP_COUNT 10
  44. /* Note: PIDR3 holds CMOD/Rev so ignored for HW identification purposes */
  45. static const u32 pidr_0124_offsets[CC_NUM_IDRS] = {
  46. CC_REG(PERIPHERAL_ID_0), CC_REG(PERIPHERAL_ID_1),
  47. CC_REG(PERIPHERAL_ID_2), CC_REG(PERIPHERAL_ID_4)
  48. };
  49. static const u32 cidr_0123_offsets[CC_NUM_IDRS] = {
  50. CC_REG(COMPONENT_ID_0), CC_REG(COMPONENT_ID_1),
  51. CC_REG(COMPONENT_ID_2), CC_REG(COMPONENT_ID_3)
  52. };
  53. /* Hardware revisions defs. */
  54. /* The 703 is a OSCCA only variant of the 713 */
  55. static const struct cc_hw_data cc703_hw = {
  56. .name = "703", .rev = CC_HW_REV_713, .cidr_0123 = 0xB105F00DU,
  57. .pidr_0124 = 0x040BB0D0U, .std_bodies = CC_STD_OSCCA
  58. };
  59. static const struct cc_hw_data cc713_hw = {
  60. .name = "713", .rev = CC_HW_REV_713, .cidr_0123 = 0xB105F00DU,
  61. .pidr_0124 = 0x040BB0D0U, .std_bodies = CC_STD_ALL
  62. };
  63. static const struct cc_hw_data cc712_hw = {
  64. .name = "712", .rev = CC_HW_REV_712, .sig = 0xDCC71200U,
  65. .std_bodies = CC_STD_ALL
  66. };
  67. static const struct cc_hw_data cc710_hw = {
  68. .name = "710", .rev = CC_HW_REV_710, .sig = 0xDCC63200U,
  69. .std_bodies = CC_STD_ALL
  70. };
  71. static const struct cc_hw_data cc630p_hw = {
  72. .name = "630P", .rev = CC_HW_REV_630, .sig = 0xDCC63000U,
  73. .std_bodies = CC_STD_ALL
  74. };
  75. static const struct of_device_id arm_ccree_dev_of_match[] = {
  76. { .compatible = "arm,cryptocell-703-ree", .data = &cc703_hw },
  77. { .compatible = "arm,cryptocell-713-ree", .data = &cc713_hw },
  78. { .compatible = "arm,cryptocell-712-ree", .data = &cc712_hw },
  79. { .compatible = "arm,cryptocell-710-ree", .data = &cc710_hw },
  80. { .compatible = "arm,cryptocell-630p-ree", .data = &cc630p_hw },
  81. {}
  82. };
  83. MODULE_DEVICE_TABLE(of, arm_ccree_dev_of_match);
  84. static u32 cc_read_idr(struct cc_drvdata *drvdata, const u32 *idr_offsets)
  85. {
  86. int i;
  87. union {
  88. u8 regs[CC_NUM_IDRS];
  89. __le32 val;
  90. } idr;
  91. for (i = 0; i < CC_NUM_IDRS; ++i)
  92. idr.regs[i] = cc_ioread(drvdata, idr_offsets[i]);
  93. return le32_to_cpu(idr.val);
  94. }
  95. void __dump_byte_array(const char *name, const u8 *buf, size_t len)
  96. {
  97. char prefix[64];
  98. if (!buf)
  99. return;
  100. snprintf(prefix, sizeof(prefix), "%s[%zu]: ", name, len);
  101. print_hex_dump(KERN_DEBUG, prefix, DUMP_PREFIX_ADDRESS, 16, 1, buf,
  102. len, false);
  103. }
  104. static irqreturn_t cc_isr(int irq, void *dev_id)
  105. {
  106. struct cc_drvdata *drvdata = (struct cc_drvdata *)dev_id;
  107. struct device *dev = drvdata_to_dev(drvdata);
  108. u32 irr;
  109. u32 imr;
  110. /* STAT_OP_TYPE_GENERIC STAT_PHASE_0: Interrupt */
  111. /* if driver suspended return, probebly shared interrupt */
  112. if (cc_pm_is_dev_suspended(dev))
  113. return IRQ_NONE;
  114. /* read the interrupt status */
  115. irr = cc_ioread(drvdata, CC_REG(HOST_IRR));
  116. dev_dbg(dev, "Got IRR=0x%08X\n", irr);
  117. if (irr == 0) /* Probably shared interrupt line */
  118. return IRQ_NONE;
  119. imr = cc_ioread(drvdata, CC_REG(HOST_IMR));
  120. /* clear interrupt - must be before processing events */
  121. cc_iowrite(drvdata, CC_REG(HOST_ICR), irr);
  122. drvdata->irq = irr;
  123. /* Completion interrupt - most probable */
  124. if (irr & drvdata->comp_mask) {
  125. /* Mask all completion interrupts - will be unmasked in
  126. * deferred service handler
  127. */
  128. cc_iowrite(drvdata, CC_REG(HOST_IMR), imr | drvdata->comp_mask);
  129. irr &= ~drvdata->comp_mask;
  130. complete_request(drvdata);
  131. }
  132. #ifdef CONFIG_CRYPTO_FIPS
  133. /* TEE FIPS interrupt */
  134. if (irr & CC_GPR0_IRQ_MASK) {
  135. /* Mask interrupt - will be unmasked in Deferred service
  136. * handler
  137. */
  138. cc_iowrite(drvdata, CC_REG(HOST_IMR), imr | CC_GPR0_IRQ_MASK);
  139. irr &= ~CC_GPR0_IRQ_MASK;
  140. fips_handler(drvdata);
  141. }
  142. #endif
  143. /* AXI error interrupt */
  144. if (irr & CC_AXI_ERR_IRQ_MASK) {
  145. u32 axi_err;
  146. /* Read the AXI error ID */
  147. axi_err = cc_ioread(drvdata, CC_REG(AXIM_MON_ERR));
  148. dev_dbg(dev, "AXI completion error: axim_mon_err=0x%08X\n",
  149. axi_err);
  150. irr &= ~CC_AXI_ERR_IRQ_MASK;
  151. }
  152. if (irr) {
  153. dev_dbg_ratelimited(dev, "IRR includes unknown cause bits (0x%08X)\n",
  154. irr);
  155. /* Just warning */
  156. }
  157. return IRQ_HANDLED;
  158. }
  159. bool cc_wait_for_reset_completion(struct cc_drvdata *drvdata)
  160. {
  161. unsigned int val;
  162. unsigned int i;
  163. /* 712/710/63 has no reset completion indication, always return true */
  164. if (drvdata->hw_rev <= CC_HW_REV_712)
  165. return true;
  166. for (i = 0; i < CC_HW_RESET_LOOP_COUNT; i++) {
  167. /* in cc7x3 NVM_IS_IDLE indicates that CC reset is
  168. * completed and device is fully functional
  169. */
  170. val = cc_ioread(drvdata, CC_REG(NVM_IS_IDLE));
  171. if (val & CC_NVM_IS_IDLE_MASK) {
  172. /* hw indicate reset completed */
  173. return true;
  174. }
  175. /* allow scheduling other process on the processor */
  176. schedule();
  177. }
  178. /* reset not completed */
  179. return false;
  180. }
  181. int init_cc_regs(struct cc_drvdata *drvdata, bool is_probe)
  182. {
  183. unsigned int val, cache_params;
  184. struct device *dev = drvdata_to_dev(drvdata);
  185. /* Unmask all AXI interrupt sources AXI_CFG1 register */
  186. /* AXI interrupt config are obsoleted startign at cc7x3 */
  187. if (drvdata->hw_rev <= CC_HW_REV_712) {
  188. val = cc_ioread(drvdata, CC_REG(AXIM_CFG));
  189. cc_iowrite(drvdata, CC_REG(AXIM_CFG), val & ~CC_AXI_IRQ_MASK);
  190. dev_dbg(dev, "AXIM_CFG=0x%08X\n",
  191. cc_ioread(drvdata, CC_REG(AXIM_CFG)));
  192. }
  193. /* Clear all pending interrupts */
  194. val = cc_ioread(drvdata, CC_REG(HOST_IRR));
  195. dev_dbg(dev, "IRR=0x%08X\n", val);
  196. cc_iowrite(drvdata, CC_REG(HOST_ICR), val);
  197. /* Unmask relevant interrupt cause */
  198. val = drvdata->comp_mask | CC_AXI_ERR_IRQ_MASK;
  199. if (drvdata->hw_rev >= CC_HW_REV_712)
  200. val |= CC_GPR0_IRQ_MASK;
  201. cc_iowrite(drvdata, CC_REG(HOST_IMR), ~val);
  202. cache_params = (drvdata->coherent ? CC_COHERENT_CACHE_PARAMS : 0x0);
  203. val = cc_ioread(drvdata, CC_REG(AXIM_CACHE_PARAMS));
  204. if (is_probe)
  205. dev_dbg(dev, "Cache params previous: 0x%08X\n", val);
  206. cc_iowrite(drvdata, CC_REG(AXIM_CACHE_PARAMS), cache_params);
  207. val = cc_ioread(drvdata, CC_REG(AXIM_CACHE_PARAMS));
  208. if (is_probe)
  209. dev_dbg(dev, "Cache params current: 0x%08X (expect: 0x%08X)\n",
  210. val, cache_params);
  211. return 0;
  212. }
  213. static int init_cc_resources(struct platform_device *plat_dev)
  214. {
  215. struct resource *req_mem_cc_regs = NULL;
  216. struct cc_drvdata *new_drvdata;
  217. struct device *dev = &plat_dev->dev;
  218. struct device_node *np = dev->of_node;
  219. u32 val, hw_rev_pidr, sig_cidr;
  220. u64 dma_mask;
  221. const struct cc_hw_data *hw_rev;
  222. const struct of_device_id *dev_id;
  223. struct clk *clk;
  224. int rc = 0;
  225. new_drvdata = devm_kzalloc(dev, sizeof(*new_drvdata), GFP_KERNEL);
  226. if (!new_drvdata)
  227. return -ENOMEM;
  228. dev_id = of_match_node(arm_ccree_dev_of_match, np);
  229. if (!dev_id)
  230. return -ENODEV;
  231. hw_rev = (struct cc_hw_data *)dev_id->data;
  232. new_drvdata->hw_rev_name = hw_rev->name;
  233. new_drvdata->hw_rev = hw_rev->rev;
  234. new_drvdata->std_bodies = hw_rev->std_bodies;
  235. if (hw_rev->rev >= CC_HW_REV_712) {
  236. new_drvdata->axim_mon_offset = CC_REG(AXIM_MON_COMP);
  237. new_drvdata->sig_offset = CC_REG(HOST_SIGNATURE_712);
  238. new_drvdata->ver_offset = CC_REG(HOST_VERSION_712);
  239. } else {
  240. new_drvdata->axim_mon_offset = CC_REG(AXIM_MON_COMP8);
  241. new_drvdata->sig_offset = CC_REG(HOST_SIGNATURE_630);
  242. new_drvdata->ver_offset = CC_REG(HOST_VERSION_630);
  243. }
  244. new_drvdata->comp_mask = CC_COMP_IRQ_MASK;
  245. platform_set_drvdata(plat_dev, new_drvdata);
  246. new_drvdata->plat_dev = plat_dev;
  247. clk = devm_clk_get(dev, NULL);
  248. if (IS_ERR(clk))
  249. switch (PTR_ERR(clk)) {
  250. /* Clock is optional so this might be fine */
  251. case -ENOENT:
  252. break;
  253. /* Clock not available, let's try again soon */
  254. case -EPROBE_DEFER:
  255. return -EPROBE_DEFER;
  256. default:
  257. dev_err(dev, "Error getting clock: %ld\n",
  258. PTR_ERR(clk));
  259. return PTR_ERR(clk);
  260. }
  261. new_drvdata->clk = clk;
  262. new_drvdata->coherent = of_dma_is_coherent(np);
  263. /* Get device resources */
  264. /* First CC registers space */
  265. req_mem_cc_regs = platform_get_resource(plat_dev, IORESOURCE_MEM, 0);
  266. /* Map registers space */
  267. new_drvdata->cc_base = devm_ioremap_resource(dev, req_mem_cc_regs);
  268. if (IS_ERR(new_drvdata->cc_base)) {
  269. dev_err(dev, "Failed to ioremap registers");
  270. return PTR_ERR(new_drvdata->cc_base);
  271. }
  272. dev_dbg(dev, "Got MEM resource (%s): %pR\n", req_mem_cc_regs->name,
  273. req_mem_cc_regs);
  274. dev_dbg(dev, "CC registers mapped from %pa to 0x%p\n",
  275. &req_mem_cc_regs->start, new_drvdata->cc_base);
  276. /* Then IRQ */
  277. new_drvdata->irq = platform_get_irq(plat_dev, 0);
  278. if (new_drvdata->irq < 0)
  279. return new_drvdata->irq;
  280. init_completion(&new_drvdata->hw_queue_avail);
  281. if (!plat_dev->dev.dma_mask)
  282. plat_dev->dev.dma_mask = &plat_dev->dev.coherent_dma_mask;
  283. dma_mask = DMA_BIT_MASK(DMA_BIT_MASK_LEN);
  284. while (dma_mask > 0x7fffffffUL) {
  285. if (dma_supported(&plat_dev->dev, dma_mask)) {
  286. rc = dma_set_coherent_mask(&plat_dev->dev, dma_mask);
  287. if (!rc)
  288. break;
  289. }
  290. dma_mask >>= 1;
  291. }
  292. if (rc) {
  293. dev_err(dev, "Failed in dma_set_mask, mask=%llx\n", dma_mask);
  294. return rc;
  295. }
  296. rc = cc_clk_on(new_drvdata);
  297. if (rc) {
  298. dev_err(dev, "Failed to enable clock");
  299. return rc;
  300. }
  301. new_drvdata->sec_disabled = cc_sec_disable;
  302. /* wait for Crytpcell reset completion */
  303. if (!cc_wait_for_reset_completion(new_drvdata)) {
  304. dev_err(dev, "Cryptocell reset not completed");
  305. }
  306. if (hw_rev->rev <= CC_HW_REV_712) {
  307. /* Verify correct mapping */
  308. val = cc_ioread(new_drvdata, new_drvdata->sig_offset);
  309. if (val != hw_rev->sig) {
  310. dev_err(dev, "Invalid CC signature: SIGNATURE=0x%08X != expected=0x%08X\n",
  311. val, hw_rev->sig);
  312. rc = -EINVAL;
  313. goto post_clk_err;
  314. }
  315. sig_cidr = val;
  316. hw_rev_pidr = cc_ioread(new_drvdata, new_drvdata->ver_offset);
  317. } else {
  318. /* Verify correct mapping */
  319. val = cc_read_idr(new_drvdata, pidr_0124_offsets);
  320. if (val != hw_rev->pidr_0124) {
  321. dev_err(dev, "Invalid CC PIDR: PIDR0124=0x%08X != expected=0x%08X\n",
  322. val, hw_rev->pidr_0124);
  323. rc = -EINVAL;
  324. goto post_clk_err;
  325. }
  326. hw_rev_pidr = val;
  327. val = cc_read_idr(new_drvdata, cidr_0123_offsets);
  328. if (val != hw_rev->cidr_0123) {
  329. dev_err(dev, "Invalid CC CIDR: CIDR0123=0x%08X != expected=0x%08X\n",
  330. val, hw_rev->cidr_0123);
  331. rc = -EINVAL;
  332. goto post_clk_err;
  333. }
  334. sig_cidr = val;
  335. /* Check HW engine configuration */
  336. val = cc_ioread(new_drvdata, CC_REG(HOST_REMOVE_INPUT_PINS));
  337. switch (val) {
  338. case CC_PINS_FULL:
  339. /* This is fine */
  340. break;
  341. case CC_PINS_SLIM:
  342. if (new_drvdata->std_bodies & CC_STD_NIST) {
  343. dev_warn(dev, "703 mode forced due to HW configuration.\n");
  344. new_drvdata->std_bodies = CC_STD_OSCCA;
  345. }
  346. break;
  347. default:
  348. dev_err(dev, "Unsupported engines configuration.\n");
  349. rc = -EINVAL;
  350. goto post_clk_err;
  351. }
  352. /* Check security disable state */
  353. val = cc_ioread(new_drvdata, CC_REG(SECURITY_DISABLED));
  354. val &= CC_SECURITY_DISABLED_MASK;
  355. new_drvdata->sec_disabled |= !!val;
  356. if (!new_drvdata->sec_disabled) {
  357. new_drvdata->comp_mask |= CC_CPP_SM4_ABORT_MASK;
  358. if (new_drvdata->std_bodies & CC_STD_NIST)
  359. new_drvdata->comp_mask |= CC_CPP_AES_ABORT_MASK;
  360. }
  361. }
  362. if (new_drvdata->sec_disabled)
  363. dev_info(dev, "Security Disabled mode is in effect. Security functions disabled.\n");
  364. /* Display HW versions */
  365. dev_info(dev, "ARM CryptoCell %s Driver: HW version 0x%08X/0x%8X, Driver version %s\n",
  366. hw_rev->name, hw_rev_pidr, sig_cidr, DRV_MODULE_VERSION);
  367. /* register the driver isr function */
  368. rc = devm_request_irq(dev, new_drvdata->irq, cc_isr,
  369. IRQF_SHARED, "ccree", new_drvdata);
  370. if (rc) {
  371. dev_err(dev, "Could not register to interrupt %d\n",
  372. new_drvdata->irq);
  373. goto post_clk_err;
  374. }
  375. dev_dbg(dev, "Registered to IRQ: %d\n", new_drvdata->irq);
  376. rc = init_cc_regs(new_drvdata, true);
  377. if (rc) {
  378. dev_err(dev, "init_cc_regs failed\n");
  379. goto post_clk_err;
  380. }
  381. rc = cc_debugfs_init(new_drvdata);
  382. if (rc) {
  383. dev_err(dev, "Failed registering debugfs interface\n");
  384. goto post_regs_err;
  385. }
  386. rc = cc_fips_init(new_drvdata);
  387. if (rc) {
  388. dev_err(dev, "CC_FIPS_INIT failed 0x%x\n", rc);
  389. goto post_debugfs_err;
  390. }
  391. rc = cc_sram_mgr_init(new_drvdata);
  392. if (rc) {
  393. dev_err(dev, "cc_sram_mgr_init failed\n");
  394. goto post_fips_init_err;
  395. }
  396. new_drvdata->mlli_sram_addr =
  397. cc_sram_alloc(new_drvdata, MAX_MLLI_BUFF_SIZE);
  398. if (new_drvdata->mlli_sram_addr == NULL_SRAM_ADDR) {
  399. dev_err(dev, "Failed to alloc MLLI Sram buffer\n");
  400. rc = -ENOMEM;
  401. goto post_sram_mgr_err;
  402. }
  403. rc = cc_req_mgr_init(new_drvdata);
  404. if (rc) {
  405. dev_err(dev, "cc_req_mgr_init failed\n");
  406. goto post_sram_mgr_err;
  407. }
  408. rc = cc_buffer_mgr_init(new_drvdata);
  409. if (rc) {
  410. dev_err(dev, "buffer_mgr_init failed\n");
  411. goto post_req_mgr_err;
  412. }
  413. rc = cc_pm_init(new_drvdata);
  414. if (rc) {
  415. dev_err(dev, "ssi_power_mgr_init failed\n");
  416. goto post_buf_mgr_err;
  417. }
  418. /* Allocate crypto algs */
  419. rc = cc_cipher_alloc(new_drvdata);
  420. if (rc) {
  421. dev_err(dev, "cc_cipher_alloc failed\n");
  422. goto post_buf_mgr_err;
  423. }
  424. /* hash must be allocated before aead since hash exports APIs */
  425. rc = cc_hash_alloc(new_drvdata);
  426. if (rc) {
  427. dev_err(dev, "cc_hash_alloc failed\n");
  428. goto post_cipher_err;
  429. }
  430. rc = cc_aead_alloc(new_drvdata);
  431. if (rc) {
  432. dev_err(dev, "cc_aead_alloc failed\n");
  433. goto post_hash_err;
  434. }
  435. /* All set, we can allow autosuspend */
  436. cc_pm_go(new_drvdata);
  437. /* If we got here and FIPS mode is enabled
  438. * it means all FIPS test passed, so let TEE
  439. * know we're good.
  440. */
  441. cc_set_ree_fips_status(new_drvdata, true);
  442. return 0;
  443. post_hash_err:
  444. cc_hash_free(new_drvdata);
  445. post_cipher_err:
  446. cc_cipher_free(new_drvdata);
  447. post_buf_mgr_err:
  448. cc_buffer_mgr_fini(new_drvdata);
  449. post_req_mgr_err:
  450. cc_req_mgr_fini(new_drvdata);
  451. post_sram_mgr_err:
  452. cc_sram_mgr_fini(new_drvdata);
  453. post_fips_init_err:
  454. cc_fips_fini(new_drvdata);
  455. post_debugfs_err:
  456. cc_debugfs_fini(new_drvdata);
  457. post_regs_err:
  458. fini_cc_regs(new_drvdata);
  459. post_clk_err:
  460. cc_clk_off(new_drvdata);
  461. return rc;
  462. }
  463. void fini_cc_regs(struct cc_drvdata *drvdata)
  464. {
  465. /* Mask all interrupts */
  466. cc_iowrite(drvdata, CC_REG(HOST_IMR), 0xFFFFFFFF);
  467. }
  468. static void cleanup_cc_resources(struct platform_device *plat_dev)
  469. {
  470. struct cc_drvdata *drvdata =
  471. (struct cc_drvdata *)platform_get_drvdata(plat_dev);
  472. cc_aead_free(drvdata);
  473. cc_hash_free(drvdata);
  474. cc_cipher_free(drvdata);
  475. cc_pm_fini(drvdata);
  476. cc_buffer_mgr_fini(drvdata);
  477. cc_req_mgr_fini(drvdata);
  478. cc_sram_mgr_fini(drvdata);
  479. cc_fips_fini(drvdata);
  480. cc_debugfs_fini(drvdata);
  481. fini_cc_regs(drvdata);
  482. cc_clk_off(drvdata);
  483. }
  484. int cc_clk_on(struct cc_drvdata *drvdata)
  485. {
  486. struct clk *clk = drvdata->clk;
  487. int rc;
  488. if (IS_ERR(clk))
  489. /* Not all devices have a clock associated with CCREE */
  490. return 0;
  491. rc = clk_prepare_enable(clk);
  492. if (rc)
  493. return rc;
  494. return 0;
  495. }
  496. unsigned int cc_get_default_hash_len(struct cc_drvdata *drvdata)
  497. {
  498. if (drvdata->hw_rev >= CC_HW_REV_712)
  499. return HASH_LEN_SIZE_712;
  500. else
  501. return HASH_LEN_SIZE_630;
  502. }
  503. void cc_clk_off(struct cc_drvdata *drvdata)
  504. {
  505. struct clk *clk = drvdata->clk;
  506. if (IS_ERR(clk))
  507. /* Not all devices have a clock associated with CCREE */
  508. return;
  509. clk_disable_unprepare(clk);
  510. }
  511. static int ccree_probe(struct platform_device *plat_dev)
  512. {
  513. int rc;
  514. struct device *dev = &plat_dev->dev;
  515. /* Map registers space */
  516. rc = init_cc_resources(plat_dev);
  517. if (rc)
  518. return rc;
  519. dev_info(dev, "ARM ccree device initialized\n");
  520. return 0;
  521. }
  522. static int ccree_remove(struct platform_device *plat_dev)
  523. {
  524. struct device *dev = &plat_dev->dev;
  525. dev_dbg(dev, "Releasing ccree resources...\n");
  526. cleanup_cc_resources(plat_dev);
  527. dev_info(dev, "ARM ccree device terminated\n");
  528. return 0;
  529. }
  530. static struct platform_driver ccree_driver = {
  531. .driver = {
  532. .name = "ccree",
  533. .of_match_table = arm_ccree_dev_of_match,
  534. #ifdef CONFIG_PM
  535. .pm = &ccree_pm,
  536. #endif
  537. },
  538. .probe = ccree_probe,
  539. .remove = ccree_remove,
  540. };
  541. static int __init ccree_init(void)
  542. {
  543. cc_hash_global_init();
  544. cc_debugfs_global_init();
  545. return platform_driver_register(&ccree_driver);
  546. }
  547. module_init(ccree_init);
  548. static void __exit ccree_exit(void)
  549. {
  550. platform_driver_unregister(&ccree_driver);
  551. cc_debugfs_global_fini();
  552. }
  553. module_exit(ccree_exit);
  554. /* Module description */
  555. MODULE_DESCRIPTION("ARM TrustZone CryptoCell REE Driver");
  556. MODULE_VERSION(DRV_MODULE_VERSION);
  557. MODULE_AUTHOR("ARM");
  558. MODULE_LICENSE("GPL v2");