ctrl.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* * CAAM control-plane driver backend
  3. * Controller-level driver, kernel property detection, initialization
  4. *
  5. * Copyright 2008-2012 Freescale Semiconductor, Inc.
  6. */
  7. #include <linux/device.h>
  8. #include <linux/of_address.h>
  9. #include <linux/of_irq.h>
  10. #include <linux/sys_soc.h>
  11. #include "compat.h"
  12. #include "regs.h"
  13. #include "intern.h"
  14. #include "jr.h"
  15. #include "desc_constr.h"
  16. #include "ctrl.h"
  17. bool caam_little_end;
  18. EXPORT_SYMBOL(caam_little_end);
  19. bool caam_dpaa2;
  20. EXPORT_SYMBOL(caam_dpaa2);
  21. bool caam_imx;
  22. EXPORT_SYMBOL(caam_imx);
  23. #ifdef CONFIG_CAAM_QI
  24. #include "qi.h"
  25. #endif
  26. /*
  27. * i.MX targets tend to have clock control subsystems that can
  28. * enable/disable clocking to our device.
  29. */
  30. static inline struct clk *caam_drv_identify_clk(struct device *dev,
  31. char *clk_name)
  32. {
  33. return caam_imx ? devm_clk_get(dev, clk_name) : NULL;
  34. }
  35. /*
  36. * Descriptor to instantiate RNG State Handle 0 in normal mode and
  37. * load the JDKEK, TDKEK and TDSK registers
  38. */
  39. static void build_instantiation_desc(u32 *desc, int handle, int do_sk)
  40. {
  41. u32 *jump_cmd, op_flags;
  42. init_job_desc(desc, 0);
  43. op_flags = OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  44. (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT;
  45. /* INIT RNG in non-test mode */
  46. append_operation(desc, op_flags);
  47. if (!handle && do_sk) {
  48. /*
  49. * For SH0, Secure Keys must be generated as well
  50. */
  51. /* wait for done */
  52. jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1);
  53. set_jump_tgt_here(desc, jump_cmd);
  54. /*
  55. * load 1 to clear written reg:
  56. * resets the done interrrupt and returns the RNG to idle.
  57. */
  58. append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW);
  59. /* Initialize State Handle */
  60. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  61. OP_ALG_AAI_RNG4_SK);
  62. }
  63. append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
  64. }
  65. /* Descriptor for deinstantiation of State Handle 0 of the RNG block. */
  66. static void build_deinstantiation_desc(u32 *desc, int handle)
  67. {
  68. init_job_desc(desc, 0);
  69. /* Uninstantiate State Handle 0 */
  70. append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG |
  71. (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INITFINAL);
  72. append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT);
  73. }
  74. /*
  75. * run_descriptor_deco0 - runs a descriptor on DECO0, under direct control of
  76. * the software (no JR/QI used).
  77. * @ctrldev - pointer to device
  78. * @status - descriptor status, after being run
  79. *
  80. * Return: - 0 if no error occurred
  81. * - -ENODEV if the DECO couldn't be acquired
  82. * - -EAGAIN if an error occurred while executing the descriptor
  83. */
  84. static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc,
  85. u32 *status)
  86. {
  87. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
  88. struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl;
  89. struct caam_deco __iomem *deco = ctrlpriv->deco;
  90. unsigned int timeout = 100000;
  91. u32 deco_dbg_reg, flags;
  92. int i;
  93. if (ctrlpriv->virt_en == 1) {
  94. clrsetbits_32(&ctrl->deco_rsr, 0, DECORSR_JR0);
  95. while (!(rd_reg32(&ctrl->deco_rsr) & DECORSR_VALID) &&
  96. --timeout)
  97. cpu_relax();
  98. timeout = 100000;
  99. }
  100. clrsetbits_32(&ctrl->deco_rq, 0, DECORR_RQD0ENABLE);
  101. while (!(rd_reg32(&ctrl->deco_rq) & DECORR_DEN0) &&
  102. --timeout)
  103. cpu_relax();
  104. if (!timeout) {
  105. dev_err(ctrldev, "failed to acquire DECO 0\n");
  106. clrsetbits_32(&ctrl->deco_rq, DECORR_RQD0ENABLE, 0);
  107. return -ENODEV;
  108. }
  109. for (i = 0; i < desc_len(desc); i++)
  110. wr_reg32(&deco->descbuf[i], caam32_to_cpu(*(desc + i)));
  111. flags = DECO_JQCR_WHL;
  112. /*
  113. * If the descriptor length is longer than 4 words, then the
  114. * FOUR bit in JRCTRL register must be set.
  115. */
  116. if (desc_len(desc) >= 4)
  117. flags |= DECO_JQCR_FOUR;
  118. /* Instruct the DECO to execute it */
  119. clrsetbits_32(&deco->jr_ctl_hi, 0, flags);
  120. timeout = 10000000;
  121. do {
  122. deco_dbg_reg = rd_reg32(&deco->desc_dbg);
  123. /*
  124. * If an error occured in the descriptor, then
  125. * the DECO status field will be set to 0x0D
  126. */
  127. if ((deco_dbg_reg & DESC_DBG_DECO_STAT_MASK) ==
  128. DESC_DBG_DECO_STAT_HOST_ERR)
  129. break;
  130. cpu_relax();
  131. } while ((deco_dbg_reg & DESC_DBG_DECO_STAT_VALID) && --timeout);
  132. *status = rd_reg32(&deco->op_status_hi) &
  133. DECO_OP_STATUS_HI_ERR_MASK;
  134. if (ctrlpriv->virt_en == 1)
  135. clrsetbits_32(&ctrl->deco_rsr, DECORSR_JR0, 0);
  136. /* Mark the DECO as free */
  137. clrsetbits_32(&ctrl->deco_rq, DECORR_RQD0ENABLE, 0);
  138. if (!timeout)
  139. return -EAGAIN;
  140. return 0;
  141. }
  142. /*
  143. * instantiate_rng - builds and executes a descriptor on DECO0,
  144. * which initializes the RNG block.
  145. * @ctrldev - pointer to device
  146. * @state_handle_mask - bitmask containing the instantiation status
  147. * for the RNG4 state handles which exist in
  148. * the RNG4 block: 1 if it's been instantiated
  149. * by an external entry, 0 otherwise.
  150. * @gen_sk - generate data to be loaded into the JDKEK, TDKEK and TDSK;
  151. * Caution: this can be done only once; if the keys need to be
  152. * regenerated, a POR is required
  153. *
  154. * Return: - 0 if no error occurred
  155. * - -ENOMEM if there isn't enough memory to allocate the descriptor
  156. * - -ENODEV if DECO0 couldn't be acquired
  157. * - -EAGAIN if an error occurred when executing the descriptor
  158. * f.i. there was a RNG hardware error due to not "good enough"
  159. * entropy being aquired.
  160. */
  161. static int instantiate_rng(struct device *ctrldev, int state_handle_mask,
  162. int gen_sk)
  163. {
  164. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
  165. struct caam_ctrl __iomem *ctrl;
  166. u32 *desc, status = 0, rdsta_val;
  167. int ret = 0, sh_idx;
  168. ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
  169. desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL);
  170. if (!desc)
  171. return -ENOMEM;
  172. for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
  173. /*
  174. * If the corresponding bit is set, this state handle
  175. * was initialized by somebody else, so it's left alone.
  176. */
  177. if ((1 << sh_idx) & state_handle_mask)
  178. continue;
  179. /* Create the descriptor for instantiating RNG State Handle */
  180. build_instantiation_desc(desc, sh_idx, gen_sk);
  181. /* Try to run it through DECO0 */
  182. ret = run_descriptor_deco0(ctrldev, desc, &status);
  183. /*
  184. * If ret is not 0, or descriptor status is not 0, then
  185. * something went wrong. No need to try the next state
  186. * handle (if available), bail out here.
  187. * Also, if for some reason, the State Handle didn't get
  188. * instantiated although the descriptor has finished
  189. * without any error (HW optimizations for later
  190. * CAAM eras), then try again.
  191. */
  192. if (ret)
  193. break;
  194. rdsta_val = rd_reg32(&ctrl->r4tst[0].rdsta) & RDSTA_IFMASK;
  195. if ((status && status != JRSTA_SSRC_JUMP_HALT_CC) ||
  196. !(rdsta_val & (1 << sh_idx))) {
  197. ret = -EAGAIN;
  198. break;
  199. }
  200. dev_info(ctrldev, "Instantiated RNG4 SH%d\n", sh_idx);
  201. /* Clear the contents before recreating the descriptor */
  202. memset(desc, 0x00, CAAM_CMD_SZ * 7);
  203. }
  204. kfree(desc);
  205. return ret;
  206. }
  207. /*
  208. * deinstantiate_rng - builds and executes a descriptor on DECO0,
  209. * which deinitializes the RNG block.
  210. * @ctrldev - pointer to device
  211. * @state_handle_mask - bitmask containing the instantiation status
  212. * for the RNG4 state handles which exist in
  213. * the RNG4 block: 1 if it's been instantiated
  214. *
  215. * Return: - 0 if no error occurred
  216. * - -ENOMEM if there isn't enough memory to allocate the descriptor
  217. * - -ENODEV if DECO0 couldn't be acquired
  218. * - -EAGAIN if an error occurred when executing the descriptor
  219. */
  220. static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask)
  221. {
  222. u32 *desc, status;
  223. int sh_idx, ret = 0;
  224. desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL);
  225. if (!desc)
  226. return -ENOMEM;
  227. for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
  228. /*
  229. * If the corresponding bit is set, then it means the state
  230. * handle was initialized by us, and thus it needs to be
  231. * deinitialized as well
  232. */
  233. if ((1 << sh_idx) & state_handle_mask) {
  234. /*
  235. * Create the descriptor for deinstantating this state
  236. * handle
  237. */
  238. build_deinstantiation_desc(desc, sh_idx);
  239. /* Try to run it through DECO0 */
  240. ret = run_descriptor_deco0(ctrldev, desc, &status);
  241. if (ret ||
  242. (status && status != JRSTA_SSRC_JUMP_HALT_CC)) {
  243. dev_err(ctrldev,
  244. "Failed to deinstantiate RNG4 SH%d\n",
  245. sh_idx);
  246. break;
  247. }
  248. dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx);
  249. }
  250. }
  251. kfree(desc);
  252. return ret;
  253. }
  254. static int caam_remove(struct platform_device *pdev)
  255. {
  256. struct device *ctrldev;
  257. struct caam_drv_private *ctrlpriv;
  258. struct caam_ctrl __iomem *ctrl;
  259. ctrldev = &pdev->dev;
  260. ctrlpriv = dev_get_drvdata(ctrldev);
  261. ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
  262. /* Remove platform devices under the crypto node */
  263. of_platform_depopulate(ctrldev);
  264. #ifdef CONFIG_CAAM_QI
  265. if (ctrlpriv->qidev)
  266. caam_qi_shutdown(ctrlpriv->qidev);
  267. #endif
  268. /*
  269. * De-initialize RNG state handles initialized by this driver.
  270. * In case of SoCs with Management Complex, RNG is managed by MC f/w.
  271. */
  272. if (!ctrlpriv->mc_en && ctrlpriv->rng4_sh_init)
  273. deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init);
  274. /* Shut down debug views */
  275. #ifdef CONFIG_DEBUG_FS
  276. debugfs_remove_recursive(ctrlpriv->dfs_root);
  277. #endif
  278. /* Unmap controller region */
  279. iounmap(ctrl);
  280. /* shut clocks off before finalizing shutdown */
  281. clk_disable_unprepare(ctrlpriv->caam_ipg);
  282. if (ctrlpriv->caam_mem)
  283. clk_disable_unprepare(ctrlpriv->caam_mem);
  284. clk_disable_unprepare(ctrlpriv->caam_aclk);
  285. if (ctrlpriv->caam_emi_slow)
  286. clk_disable_unprepare(ctrlpriv->caam_emi_slow);
  287. return 0;
  288. }
  289. /*
  290. * kick_trng - sets the various parameters for enabling the initialization
  291. * of the RNG4 block in CAAM
  292. * @pdev - pointer to the platform device
  293. * @ent_delay - Defines the length (in system clocks) of each entropy sample.
  294. */
  295. static void kick_trng(struct platform_device *pdev, int ent_delay)
  296. {
  297. struct device *ctrldev = &pdev->dev;
  298. struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev);
  299. struct caam_ctrl __iomem *ctrl;
  300. struct rng4tst __iomem *r4tst;
  301. u32 val;
  302. ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl;
  303. r4tst = &ctrl->r4tst[0];
  304. /* put RNG4 into program mode */
  305. clrsetbits_32(&r4tst->rtmctl, 0, RTMCTL_PRGM);
  306. /*
  307. * Performance-wise, it does not make sense to
  308. * set the delay to a value that is lower
  309. * than the last one that worked (i.e. the state handles
  310. * were instantiated properly. Thus, instead of wasting
  311. * time trying to set the values controlling the sample
  312. * frequency, the function simply returns.
  313. */
  314. val = (rd_reg32(&r4tst->rtsdctl) & RTSDCTL_ENT_DLY_MASK)
  315. >> RTSDCTL_ENT_DLY_SHIFT;
  316. if (ent_delay <= val)
  317. goto start_rng;
  318. val = rd_reg32(&r4tst->rtsdctl);
  319. val = (val & ~RTSDCTL_ENT_DLY_MASK) |
  320. (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
  321. wr_reg32(&r4tst->rtsdctl, val);
  322. /* min. freq. count, equal to 1/4 of the entropy sample length */
  323. wr_reg32(&r4tst->rtfrqmin, ent_delay >> 2);
  324. /* disable maximum frequency count */
  325. wr_reg32(&r4tst->rtfrqmax, RTFRQMAX_DISABLE);
  326. /* read the control register */
  327. val = rd_reg32(&r4tst->rtmctl);
  328. start_rng:
  329. /*
  330. * select raw sampling in both entropy shifter
  331. * and statistical checker; ; put RNG4 into run mode
  332. */
  333. clrsetbits_32(&r4tst->rtmctl, RTMCTL_PRGM, RTMCTL_SAMP_MODE_RAW_ES_SC);
  334. }
  335. static int caam_get_era_from_hw(struct caam_ctrl __iomem *ctrl)
  336. {
  337. static const struct {
  338. u16 ip_id;
  339. u8 maj_rev;
  340. u8 era;
  341. } id[] = {
  342. {0x0A10, 1, 1},
  343. {0x0A10, 2, 2},
  344. {0x0A12, 1, 3},
  345. {0x0A14, 1, 3},
  346. {0x0A14, 2, 4},
  347. {0x0A16, 1, 4},
  348. {0x0A10, 3, 4},
  349. {0x0A11, 1, 4},
  350. {0x0A18, 1, 4},
  351. {0x0A11, 2, 5},
  352. {0x0A12, 2, 5},
  353. {0x0A13, 1, 5},
  354. {0x0A1C, 1, 5}
  355. };
  356. u32 ccbvid, id_ms;
  357. u8 maj_rev, era;
  358. u16 ip_id;
  359. int i;
  360. ccbvid = rd_reg32(&ctrl->perfmon.ccb_id);
  361. era = (ccbvid & CCBVID_ERA_MASK) >> CCBVID_ERA_SHIFT;
  362. if (era) /* This is '0' prior to CAAM ERA-6 */
  363. return era;
  364. id_ms = rd_reg32(&ctrl->perfmon.caam_id_ms);
  365. ip_id = (id_ms & SECVID_MS_IPID_MASK) >> SECVID_MS_IPID_SHIFT;
  366. maj_rev = (id_ms & SECVID_MS_MAJ_REV_MASK) >> SECVID_MS_MAJ_REV_SHIFT;
  367. for (i = 0; i < ARRAY_SIZE(id); i++)
  368. if (id[i].ip_id == ip_id && id[i].maj_rev == maj_rev)
  369. return id[i].era;
  370. return -ENOTSUPP;
  371. }
  372. /**
  373. * caam_get_era() - Return the ERA of the SEC on SoC, based
  374. * on "sec-era" optional property in the DTS. This property is updated
  375. * by u-boot.
  376. * In case this property is not passed an attempt to retrieve the CAAM
  377. * era via register reads will be made.
  378. **/
  379. static int caam_get_era(struct caam_ctrl __iomem *ctrl)
  380. {
  381. struct device_node *caam_node;
  382. int ret;
  383. u32 prop;
  384. caam_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0");
  385. ret = of_property_read_u32(caam_node, "fsl,sec-era", &prop);
  386. of_node_put(caam_node);
  387. if (!ret)
  388. return prop;
  389. else
  390. return caam_get_era_from_hw(ctrl);
  391. }
  392. static const struct of_device_id caam_match[] = {
  393. {
  394. .compatible = "fsl,sec-v4.0",
  395. },
  396. {
  397. .compatible = "fsl,sec4.0",
  398. },
  399. {},
  400. };
  401. MODULE_DEVICE_TABLE(of, caam_match);
  402. /* Probe routine for CAAM top (controller) level */
  403. static int caam_probe(struct platform_device *pdev)
  404. {
  405. int ret, ring, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN;
  406. u64 caam_id;
  407. static const struct soc_device_attribute imx_soc[] = {
  408. {.family = "Freescale i.MX"},
  409. {},
  410. };
  411. struct device *dev;
  412. struct device_node *nprop, *np;
  413. struct caam_ctrl __iomem *ctrl;
  414. struct caam_drv_private *ctrlpriv;
  415. struct clk *clk;
  416. #ifdef CONFIG_DEBUG_FS
  417. struct caam_perfmon *perfmon;
  418. #endif
  419. u32 scfgr, comp_params;
  420. u32 cha_vid_ls;
  421. int pg_size;
  422. int BLOCK_OFFSET = 0;
  423. ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(*ctrlpriv), GFP_KERNEL);
  424. if (!ctrlpriv)
  425. return -ENOMEM;
  426. dev = &pdev->dev;
  427. dev_set_drvdata(dev, ctrlpriv);
  428. nprop = pdev->dev.of_node;
  429. caam_imx = (bool)soc_device_match(imx_soc);
  430. /* Enable clocking */
  431. clk = caam_drv_identify_clk(&pdev->dev, "ipg");
  432. if (IS_ERR(clk)) {
  433. ret = PTR_ERR(clk);
  434. dev_err(&pdev->dev,
  435. "can't identify CAAM ipg clk: %d\n", ret);
  436. return ret;
  437. }
  438. ctrlpriv->caam_ipg = clk;
  439. if (!of_machine_is_compatible("fsl,imx7d") &&
  440. !of_machine_is_compatible("fsl,imx7s")) {
  441. clk = caam_drv_identify_clk(&pdev->dev, "mem");
  442. if (IS_ERR(clk)) {
  443. ret = PTR_ERR(clk);
  444. dev_err(&pdev->dev,
  445. "can't identify CAAM mem clk: %d\n", ret);
  446. return ret;
  447. }
  448. ctrlpriv->caam_mem = clk;
  449. }
  450. clk = caam_drv_identify_clk(&pdev->dev, "aclk");
  451. if (IS_ERR(clk)) {
  452. ret = PTR_ERR(clk);
  453. dev_err(&pdev->dev,
  454. "can't identify CAAM aclk clk: %d\n", ret);
  455. return ret;
  456. }
  457. ctrlpriv->caam_aclk = clk;
  458. if (!of_machine_is_compatible("fsl,imx6ul") &&
  459. !of_machine_is_compatible("fsl,imx7d") &&
  460. !of_machine_is_compatible("fsl,imx7s")) {
  461. clk = caam_drv_identify_clk(&pdev->dev, "emi_slow");
  462. if (IS_ERR(clk)) {
  463. ret = PTR_ERR(clk);
  464. dev_err(&pdev->dev,
  465. "can't identify CAAM emi_slow clk: %d\n", ret);
  466. return ret;
  467. }
  468. ctrlpriv->caam_emi_slow = clk;
  469. }
  470. ret = clk_prepare_enable(ctrlpriv->caam_ipg);
  471. if (ret < 0) {
  472. dev_err(&pdev->dev, "can't enable CAAM ipg clock: %d\n", ret);
  473. return ret;
  474. }
  475. if (ctrlpriv->caam_mem) {
  476. ret = clk_prepare_enable(ctrlpriv->caam_mem);
  477. if (ret < 0) {
  478. dev_err(&pdev->dev, "can't enable CAAM secure mem clock: %d\n",
  479. ret);
  480. goto disable_caam_ipg;
  481. }
  482. }
  483. ret = clk_prepare_enable(ctrlpriv->caam_aclk);
  484. if (ret < 0) {
  485. dev_err(&pdev->dev, "can't enable CAAM aclk clock: %d\n", ret);
  486. goto disable_caam_mem;
  487. }
  488. if (ctrlpriv->caam_emi_slow) {
  489. ret = clk_prepare_enable(ctrlpriv->caam_emi_slow);
  490. if (ret < 0) {
  491. dev_err(&pdev->dev, "can't enable CAAM emi slow clock: %d\n",
  492. ret);
  493. goto disable_caam_aclk;
  494. }
  495. }
  496. /* Get configuration properties from device tree */
  497. /* First, get register page */
  498. ctrl = of_iomap(nprop, 0);
  499. if (ctrl == NULL) {
  500. dev_err(dev, "caam: of_iomap() failed\n");
  501. ret = -ENOMEM;
  502. goto disable_caam_emi_slow;
  503. }
  504. caam_little_end = !(bool)(rd_reg32(&ctrl->perfmon.status) &
  505. (CSTA_PLEND | CSTA_ALT_PLEND));
  506. /* Finding the page size for using the CTPR_MS register */
  507. comp_params = rd_reg32(&ctrl->perfmon.comp_parms_ms);
  508. pg_size = (comp_params & CTPR_MS_PG_SZ_MASK) >> CTPR_MS_PG_SZ_SHIFT;
  509. /* Allocating the BLOCK_OFFSET based on the supported page size on
  510. * the platform
  511. */
  512. if (pg_size == 0)
  513. BLOCK_OFFSET = PG_SIZE_4K;
  514. else
  515. BLOCK_OFFSET = PG_SIZE_64K;
  516. ctrlpriv->ctrl = (struct caam_ctrl __iomem __force *)ctrl;
  517. ctrlpriv->assure = (struct caam_assurance __iomem __force *)
  518. ((__force uint8_t *)ctrl +
  519. BLOCK_OFFSET * ASSURE_BLOCK_NUMBER
  520. );
  521. ctrlpriv->deco = (struct caam_deco __iomem __force *)
  522. ((__force uint8_t *)ctrl +
  523. BLOCK_OFFSET * DECO_BLOCK_NUMBER
  524. );
  525. /* Get the IRQ of the controller (for security violations only) */
  526. ctrlpriv->secvio_irq = irq_of_parse_and_map(nprop, 0);
  527. /*
  528. * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel,
  529. * long pointers in master configuration register.
  530. * In case of SoCs with Management Complex, MC f/w performs
  531. * the configuration.
  532. */
  533. caam_dpaa2 = !!(comp_params & CTPR_MS_DPAA2);
  534. np = of_find_compatible_node(NULL, NULL, "fsl,qoriq-mc");
  535. ctrlpriv->mc_en = !!np;
  536. of_node_put(np);
  537. if (!ctrlpriv->mc_en)
  538. clrsetbits_32(&ctrl->mcr, MCFGR_AWCACHE_MASK | MCFGR_LONG_PTR,
  539. MCFGR_AWCACHE_CACH | MCFGR_AWCACHE_BUFF |
  540. MCFGR_WDENABLE | MCFGR_LARGE_BURST |
  541. (sizeof(dma_addr_t) == sizeof(u64) ?
  542. MCFGR_LONG_PTR : 0));
  543. /*
  544. * Read the Compile Time paramters and SCFGR to determine
  545. * if Virtualization is enabled for this platform
  546. */
  547. scfgr = rd_reg32(&ctrl->scfgr);
  548. ctrlpriv->virt_en = 0;
  549. if (comp_params & CTPR_MS_VIRT_EN_INCL) {
  550. /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
  551. * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SCFGR_VIRT_EN = 1
  552. */
  553. if ((comp_params & CTPR_MS_VIRT_EN_POR) ||
  554. (!(comp_params & CTPR_MS_VIRT_EN_POR) &&
  555. (scfgr & SCFGR_VIRT_EN)))
  556. ctrlpriv->virt_en = 1;
  557. } else {
  558. /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
  559. if (comp_params & CTPR_MS_VIRT_EN_POR)
  560. ctrlpriv->virt_en = 1;
  561. }
  562. if (ctrlpriv->virt_en == 1)
  563. clrsetbits_32(&ctrl->jrstart, 0, JRSTART_JR0_START |
  564. JRSTART_JR1_START | JRSTART_JR2_START |
  565. JRSTART_JR3_START);
  566. if (sizeof(dma_addr_t) == sizeof(u64)) {
  567. if (caam_dpaa2)
  568. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(49));
  569. else if (of_device_is_compatible(nprop, "fsl,sec-v5.0"))
  570. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40));
  571. else
  572. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(36));
  573. } else {
  574. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  575. }
  576. if (ret) {
  577. dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret);
  578. goto iounmap_ctrl;
  579. }
  580. ctrlpriv->era = caam_get_era(ctrl);
  581. ret = of_platform_populate(nprop, caam_match, NULL, dev);
  582. if (ret) {
  583. dev_err(dev, "JR platform devices creation error\n");
  584. goto iounmap_ctrl;
  585. }
  586. #ifdef CONFIG_DEBUG_FS
  587. /*
  588. * FIXME: needs better naming distinction, as some amalgamation of
  589. * "caam" and nprop->full_name. The OF name isn't distinctive,
  590. * but does separate instances
  591. */
  592. perfmon = (struct caam_perfmon __force *)&ctrl->perfmon;
  593. ctrlpriv->dfs_root = debugfs_create_dir(dev_name(dev), NULL);
  594. ctrlpriv->ctl = debugfs_create_dir("ctl", ctrlpriv->dfs_root);
  595. #endif
  596. ring = 0;
  597. for_each_available_child_of_node(nprop, np)
  598. if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") ||
  599. of_device_is_compatible(np, "fsl,sec4.0-job-ring")) {
  600. ctrlpriv->jr[ring] = (struct caam_job_ring __iomem __force *)
  601. ((__force uint8_t *)ctrl +
  602. (ring + JR_BLOCK_NUMBER) *
  603. BLOCK_OFFSET
  604. );
  605. ctrlpriv->total_jobrs++;
  606. ring++;
  607. }
  608. /* Check to see if (DPAA 1.x) QI present. If so, enable */
  609. ctrlpriv->qi_present = !!(comp_params & CTPR_MS_QI_MASK);
  610. if (ctrlpriv->qi_present && !caam_dpaa2) {
  611. ctrlpriv->qi = (struct caam_queue_if __iomem __force *)
  612. ((__force uint8_t *)ctrl +
  613. BLOCK_OFFSET * QI_BLOCK_NUMBER
  614. );
  615. /* This is all that's required to physically enable QI */
  616. wr_reg32(&ctrlpriv->qi->qi_control_lo, QICTL_DQEN);
  617. /* If QMAN driver is present, init CAAM-QI backend */
  618. #ifdef CONFIG_CAAM_QI
  619. ret = caam_qi_init(pdev);
  620. if (ret)
  621. dev_err(dev, "caam qi i/f init failed: %d\n", ret);
  622. #endif
  623. }
  624. /* If no QI and no rings specified, quit and go home */
  625. if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) {
  626. dev_err(dev, "no queues configured, terminating\n");
  627. ret = -ENOMEM;
  628. goto caam_remove;
  629. }
  630. cha_vid_ls = rd_reg32(&ctrl->perfmon.cha_id_ls);
  631. /*
  632. * If SEC has RNG version >= 4 and RNG state handle has not been
  633. * already instantiated, do RNG instantiation
  634. * In case of SoCs with Management Complex, RNG is managed by MC f/w.
  635. */
  636. if (!ctrlpriv->mc_en &&
  637. (cha_vid_ls & CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT >= 4) {
  638. ctrlpriv->rng4_sh_init =
  639. rd_reg32(&ctrl->r4tst[0].rdsta);
  640. /*
  641. * If the secure keys (TDKEK, JDKEK, TDSK), were already
  642. * generated, signal this to the function that is instantiating
  643. * the state handles. An error would occur if RNG4 attempts
  644. * to regenerate these keys before the next POR.
  645. */
  646. gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1;
  647. ctrlpriv->rng4_sh_init &= RDSTA_IFMASK;
  648. do {
  649. int inst_handles =
  650. rd_reg32(&ctrl->r4tst[0].rdsta) &
  651. RDSTA_IFMASK;
  652. /*
  653. * If either SH were instantiated by somebody else
  654. * (e.g. u-boot) then it is assumed that the entropy
  655. * parameters are properly set and thus the function
  656. * setting these (kick_trng(...)) is skipped.
  657. * Also, if a handle was instantiated, do not change
  658. * the TRNG parameters.
  659. */
  660. if (!(ctrlpriv->rng4_sh_init || inst_handles)) {
  661. dev_info(dev,
  662. "Entropy delay = %u\n",
  663. ent_delay);
  664. kick_trng(pdev, ent_delay);
  665. ent_delay += 400;
  666. }
  667. /*
  668. * if instantiate_rng(...) fails, the loop will rerun
  669. * and the kick_trng(...) function will modfiy the
  670. * upper and lower limits of the entropy sampling
  671. * interval, leading to a sucessful initialization of
  672. * the RNG.
  673. */
  674. ret = instantiate_rng(dev, inst_handles,
  675. gen_sk);
  676. if (ret == -EAGAIN)
  677. /*
  678. * if here, the loop will rerun,
  679. * so don't hog the CPU
  680. */
  681. cpu_relax();
  682. } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
  683. if (ret) {
  684. dev_err(dev, "failed to instantiate RNG");
  685. goto caam_remove;
  686. }
  687. /*
  688. * Set handles init'ed by this module as the complement of the
  689. * already initialized ones
  690. */
  691. ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK;
  692. /* Enable RDB bit so that RNG works faster */
  693. clrsetbits_32(&ctrl->scfgr, 0, SCFGR_RDBENABLE);
  694. }
  695. /* NOTE: RTIC detection ought to go here, around Si time */
  696. caam_id = (u64)rd_reg32(&ctrl->perfmon.caam_id_ms) << 32 |
  697. (u64)rd_reg32(&ctrl->perfmon.caam_id_ls);
  698. /* Report "alive" for developer to see */
  699. dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id,
  700. ctrlpriv->era);
  701. dev_info(dev, "job rings = %d, qi = %d\n",
  702. ctrlpriv->total_jobrs, ctrlpriv->qi_present);
  703. #ifdef CONFIG_DEBUG_FS
  704. debugfs_create_file("rq_dequeued", S_IRUSR | S_IRGRP | S_IROTH,
  705. ctrlpriv->ctl, &perfmon->req_dequeued,
  706. &caam_fops_u64_ro);
  707. debugfs_create_file("ob_rq_encrypted", S_IRUSR | S_IRGRP | S_IROTH,
  708. ctrlpriv->ctl, &perfmon->ob_enc_req,
  709. &caam_fops_u64_ro);
  710. debugfs_create_file("ib_rq_decrypted", S_IRUSR | S_IRGRP | S_IROTH,
  711. ctrlpriv->ctl, &perfmon->ib_dec_req,
  712. &caam_fops_u64_ro);
  713. debugfs_create_file("ob_bytes_encrypted", S_IRUSR | S_IRGRP | S_IROTH,
  714. ctrlpriv->ctl, &perfmon->ob_enc_bytes,
  715. &caam_fops_u64_ro);
  716. debugfs_create_file("ob_bytes_protected", S_IRUSR | S_IRGRP | S_IROTH,
  717. ctrlpriv->ctl, &perfmon->ob_prot_bytes,
  718. &caam_fops_u64_ro);
  719. debugfs_create_file("ib_bytes_decrypted", S_IRUSR | S_IRGRP | S_IROTH,
  720. ctrlpriv->ctl, &perfmon->ib_dec_bytes,
  721. &caam_fops_u64_ro);
  722. debugfs_create_file("ib_bytes_validated", S_IRUSR | S_IRGRP | S_IROTH,
  723. ctrlpriv->ctl, &perfmon->ib_valid_bytes,
  724. &caam_fops_u64_ro);
  725. /* Controller level - global status values */
  726. debugfs_create_file("fault_addr", S_IRUSR | S_IRGRP | S_IROTH,
  727. ctrlpriv->ctl, &perfmon->faultaddr,
  728. &caam_fops_u32_ro);
  729. debugfs_create_file("fault_detail", S_IRUSR | S_IRGRP | S_IROTH,
  730. ctrlpriv->ctl, &perfmon->faultdetail,
  731. &caam_fops_u32_ro);
  732. debugfs_create_file("fault_status", S_IRUSR | S_IRGRP | S_IROTH,
  733. ctrlpriv->ctl, &perfmon->status,
  734. &caam_fops_u32_ro);
  735. /* Internal covering keys (useful in non-secure mode only) */
  736. ctrlpriv->ctl_kek_wrap.data = (__force void *)&ctrlpriv->ctrl->kek[0];
  737. ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  738. ctrlpriv->ctl_kek = debugfs_create_blob("kek",
  739. S_IRUSR |
  740. S_IRGRP | S_IROTH,
  741. ctrlpriv->ctl,
  742. &ctrlpriv->ctl_kek_wrap);
  743. ctrlpriv->ctl_tkek_wrap.data = (__force void *)&ctrlpriv->ctrl->tkek[0];
  744. ctrlpriv->ctl_tkek_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  745. ctrlpriv->ctl_tkek = debugfs_create_blob("tkek",
  746. S_IRUSR |
  747. S_IRGRP | S_IROTH,
  748. ctrlpriv->ctl,
  749. &ctrlpriv->ctl_tkek_wrap);
  750. ctrlpriv->ctl_tdsk_wrap.data = (__force void *)&ctrlpriv->ctrl->tdsk[0];
  751. ctrlpriv->ctl_tdsk_wrap.size = KEK_KEY_SIZE * sizeof(u32);
  752. ctrlpriv->ctl_tdsk = debugfs_create_blob("tdsk",
  753. S_IRUSR |
  754. S_IRGRP | S_IROTH,
  755. ctrlpriv->ctl,
  756. &ctrlpriv->ctl_tdsk_wrap);
  757. #endif
  758. return 0;
  759. caam_remove:
  760. caam_remove(pdev);
  761. return ret;
  762. iounmap_ctrl:
  763. iounmap(ctrl);
  764. disable_caam_emi_slow:
  765. if (ctrlpriv->caam_emi_slow)
  766. clk_disable_unprepare(ctrlpriv->caam_emi_slow);
  767. disable_caam_aclk:
  768. clk_disable_unprepare(ctrlpriv->caam_aclk);
  769. disable_caam_mem:
  770. if (ctrlpriv->caam_mem)
  771. clk_disable_unprepare(ctrlpriv->caam_mem);
  772. disable_caam_ipg:
  773. clk_disable_unprepare(ctrlpriv->caam_ipg);
  774. return ret;
  775. }
  776. static struct platform_driver caam_driver = {
  777. .driver = {
  778. .name = "caam",
  779. .of_match_table = caam_match,
  780. },
  781. .probe = caam_probe,
  782. .remove = caam_remove,
  783. };
  784. module_platform_driver(caam_driver);
  785. MODULE_LICENSE("GPL");
  786. MODULE_DESCRIPTION("FSL CAAM request backend");
  787. MODULE_AUTHOR("Freescale Semiconductor - NMG/STC");