ctrl.c 25 KB

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