jr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Copyright 2008-2014 Freescale Semiconductor, Inc.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. *
  6. * Based on CAAM driver in drivers/crypto/caam in Linux
  7. */
  8. #include <common.h>
  9. #include <malloc.h>
  10. #include "fsl_sec.h"
  11. #include "jr.h"
  12. #include "jobdesc.h"
  13. #define CIRC_CNT(head, tail, size) (((head) - (tail)) & (size - 1))
  14. #define CIRC_SPACE(head, tail, size) CIRC_CNT((tail), (head) + 1, (size))
  15. struct jobring jr;
  16. static inline void start_jr0(void)
  17. {
  18. ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
  19. u32 ctpr_ms = sec_in32(&sec->ctpr_ms);
  20. u32 scfgr = sec_in32(&sec->scfgr);
  21. if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_INCL) {
  22. /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
  23. * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SEC_SCFGR_VIRT_EN = 1
  24. */
  25. if ((ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR) ||
  26. (!(ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR) &&
  27. (scfgr & SEC_SCFGR_VIRT_EN)))
  28. sec_out32(&sec->jrstartr, CONFIG_JRSTARTR_JR0);
  29. } else {
  30. /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
  31. if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR)
  32. sec_out32(&sec->jrstartr, CONFIG_JRSTARTR_JR0);
  33. }
  34. }
  35. static inline void jr_reset_liodn(void)
  36. {
  37. ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
  38. sec_out32(&sec->jrliodnr[0].ls, 0);
  39. }
  40. static inline void jr_disable_irq(void)
  41. {
  42. struct jr_regs *regs = (struct jr_regs *)CONFIG_SYS_FSL_JR0_ADDR;
  43. uint32_t jrcfg = sec_in32(&regs->jrcfg1);
  44. jrcfg = jrcfg | JR_INTMASK;
  45. sec_out32(&regs->jrcfg1, jrcfg);
  46. }
  47. static void jr_initregs(void)
  48. {
  49. struct jr_regs *regs = (struct jr_regs *)CONFIG_SYS_FSL_JR0_ADDR;
  50. phys_addr_t ip_base = virt_to_phys((void *)jr.input_ring);
  51. phys_addr_t op_base = virt_to_phys((void *)jr.output_ring);
  52. #ifdef CONFIG_PHYS_64BIT
  53. sec_out32(&regs->irba_h, ip_base >> 32);
  54. #else
  55. sec_out32(&regs->irba_h, 0x0);
  56. #endif
  57. sec_out32(&regs->irba_l, (uint32_t)ip_base);
  58. #ifdef CONFIG_PHYS_64BIT
  59. sec_out32(&regs->orba_h, op_base >> 32);
  60. #else
  61. sec_out32(&regs->orba_h, 0x0);
  62. #endif
  63. sec_out32(&regs->orba_l, (uint32_t)op_base);
  64. sec_out32(&regs->ors, JR_SIZE);
  65. sec_out32(&regs->irs, JR_SIZE);
  66. if (!jr.irq)
  67. jr_disable_irq();
  68. }
  69. static int jr_init(void)
  70. {
  71. memset(&jr, 0, sizeof(struct jobring));
  72. jr.jq_id = DEFAULT_JR_ID;
  73. jr.irq = DEFAULT_IRQ;
  74. #ifdef CONFIG_FSL_CORENET
  75. jr.liodn = DEFAULT_JR_LIODN;
  76. #endif
  77. jr.size = JR_SIZE;
  78. jr.input_ring = (dma_addr_t *)malloc(JR_SIZE * sizeof(dma_addr_t));
  79. if (!jr.input_ring)
  80. return -1;
  81. jr.output_ring =
  82. (struct op_ring *)malloc(JR_SIZE * sizeof(struct op_ring));
  83. if (!jr.output_ring)
  84. return -1;
  85. memset(jr.input_ring, 0, JR_SIZE * sizeof(dma_addr_t));
  86. memset(jr.output_ring, 0, JR_SIZE * sizeof(struct op_ring));
  87. start_jr0();
  88. jr_initregs();
  89. return 0;
  90. }
  91. static int jr_sw_cleanup(void)
  92. {
  93. jr.head = 0;
  94. jr.tail = 0;
  95. jr.read_idx = 0;
  96. jr.write_idx = 0;
  97. memset(jr.info, 0, sizeof(jr.info));
  98. memset(jr.input_ring, 0, jr.size * sizeof(dma_addr_t));
  99. memset(jr.output_ring, 0, jr.size * sizeof(struct op_ring));
  100. return 0;
  101. }
  102. static int jr_hw_reset(void)
  103. {
  104. struct jr_regs *regs = (struct jr_regs *)CONFIG_SYS_FSL_JR0_ADDR;
  105. uint32_t timeout = 100000;
  106. uint32_t jrint, jrcr;
  107. sec_out32(&regs->jrcr, JRCR_RESET);
  108. do {
  109. jrint = sec_in32(&regs->jrint);
  110. } while (((jrint & JRINT_ERR_HALT_MASK) ==
  111. JRINT_ERR_HALT_INPROGRESS) && --timeout);
  112. jrint = sec_in32(&regs->jrint);
  113. if (((jrint & JRINT_ERR_HALT_MASK) !=
  114. JRINT_ERR_HALT_INPROGRESS) && timeout == 0)
  115. return -1;
  116. timeout = 100000;
  117. sec_out32(&regs->jrcr, JRCR_RESET);
  118. do {
  119. jrcr = sec_in32(&regs->jrcr);
  120. } while ((jrcr & JRCR_RESET) && --timeout);
  121. if (timeout == 0)
  122. return -1;
  123. return 0;
  124. }
  125. /* -1 --- error, can't enqueue -- no space available */
  126. static int jr_enqueue(uint32_t *desc_addr,
  127. void (*callback)(uint32_t desc, uint32_t status, void *arg),
  128. void *arg)
  129. {
  130. struct jr_regs *regs = (struct jr_regs *)CONFIG_SYS_FSL_JR0_ADDR;
  131. int head = jr.head;
  132. dma_addr_t desc_phys_addr = virt_to_phys(desc_addr);
  133. if (sec_in32(&regs->irsa) == 0 ||
  134. CIRC_SPACE(jr.head, jr.tail, jr.size) <= 0)
  135. return -1;
  136. jr.input_ring[head] = desc_phys_addr;
  137. jr.info[head].desc_phys_addr = desc_phys_addr;
  138. jr.info[head].desc_addr = (uint32_t)desc_addr;
  139. jr.info[head].callback = (void *)callback;
  140. jr.info[head].arg = arg;
  141. jr.info[head].op_done = 0;
  142. jr.head = (head + 1) & (jr.size - 1);
  143. sec_out32(&regs->irja, 1);
  144. return 0;
  145. }
  146. static int jr_dequeue(void)
  147. {
  148. struct jr_regs *regs = (struct jr_regs *)CONFIG_SYS_FSL_JR0_ADDR;
  149. int head = jr.head;
  150. int tail = jr.tail;
  151. int idx, i, found;
  152. void (*callback)(uint32_t desc, uint32_t status, void *arg);
  153. void *arg = NULL;
  154. while (sec_in32(&regs->orsf) && CIRC_CNT(jr.head, jr.tail, jr.size)) {
  155. found = 0;
  156. dma_addr_t op_desc = jr.output_ring[jr.tail].desc;
  157. uint32_t status = jr.output_ring[jr.tail].status;
  158. uint32_t desc_virt;
  159. for (i = 0; CIRC_CNT(head, tail + i, jr.size) >= 1; i++) {
  160. idx = (tail + i) & (jr.size - 1);
  161. if (op_desc == jr.info[idx].desc_phys_addr) {
  162. desc_virt = jr.info[idx].desc_addr;
  163. found = 1;
  164. break;
  165. }
  166. }
  167. /* Error condition if match not found */
  168. if (!found)
  169. return -1;
  170. jr.info[idx].op_done = 1;
  171. callback = (void *)jr.info[idx].callback;
  172. arg = jr.info[idx].arg;
  173. /* When the job on tail idx gets done, increment
  174. * tail till the point where job completed out of oredr has
  175. * been taken into account
  176. */
  177. if (idx == tail)
  178. do {
  179. tail = (tail + 1) & (jr.size - 1);
  180. } while (jr.info[tail].op_done);
  181. jr.tail = tail;
  182. jr.read_idx = (jr.read_idx + 1) & (jr.size - 1);
  183. sec_out32(&regs->orjr, 1);
  184. jr.info[idx].op_done = 0;
  185. callback(desc_virt, status, arg);
  186. }
  187. return 0;
  188. }
  189. static void desc_done(uint32_t desc, uint32_t status, void *arg)
  190. {
  191. struct result *x = arg;
  192. x->status = status;
  193. caam_jr_strstatus(status);
  194. x->done = 1;
  195. }
  196. int run_descriptor_jr(uint32_t *desc)
  197. {
  198. unsigned long long timeval = get_ticks();
  199. unsigned long long timeout = usec2ticks(CONFIG_SEC_DEQ_TIMEOUT);
  200. struct result op;
  201. int ret = 0;
  202. memset(&op, sizeof(op), 0);
  203. ret = jr_enqueue(desc, desc_done, &op);
  204. if (ret) {
  205. debug("Error in SEC enq\n");
  206. ret = JQ_ENQ_ERR;
  207. goto out;
  208. }
  209. timeval = get_ticks();
  210. timeout = usec2ticks(CONFIG_SEC_DEQ_TIMEOUT);
  211. while (op.done != 1) {
  212. ret = jr_dequeue();
  213. if (ret) {
  214. debug("Error in SEC deq\n");
  215. ret = JQ_DEQ_ERR;
  216. goto out;
  217. }
  218. if ((get_ticks() - timeval) > timeout) {
  219. debug("SEC Dequeue timed out\n");
  220. ret = JQ_DEQ_TO_ERR;
  221. goto out;
  222. }
  223. }
  224. if (!op.status) {
  225. debug("Error %x\n", op.status);
  226. ret = op.status;
  227. }
  228. out:
  229. return ret;
  230. }
  231. int jr_reset(void)
  232. {
  233. if (jr_hw_reset() < 0)
  234. return -1;
  235. /* Clean up the jobring structure maintained by software */
  236. jr_sw_cleanup();
  237. return 0;
  238. }
  239. int sec_reset(void)
  240. {
  241. ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
  242. uint32_t mcfgr = sec_in32(&sec->mcfgr);
  243. uint32_t timeout = 100000;
  244. mcfgr |= MCFGR_SWRST;
  245. sec_out32(&sec->mcfgr, mcfgr);
  246. mcfgr |= MCFGR_DMA_RST;
  247. sec_out32(&sec->mcfgr, mcfgr);
  248. do {
  249. mcfgr = sec_in32(&sec->mcfgr);
  250. } while ((mcfgr & MCFGR_DMA_RST) == MCFGR_DMA_RST && --timeout);
  251. if (timeout == 0)
  252. return -1;
  253. timeout = 100000;
  254. do {
  255. mcfgr = sec_in32(&sec->mcfgr);
  256. } while ((mcfgr & MCFGR_SWRST) == MCFGR_SWRST && --timeout);
  257. if (timeout == 0)
  258. return -1;
  259. return 0;
  260. }
  261. static int instantiate_rng(void)
  262. {
  263. struct result op;
  264. u32 *desc;
  265. u32 rdsta_val;
  266. int ret = 0;
  267. ccsr_sec_t __iomem *sec =
  268. (ccsr_sec_t __iomem *)CONFIG_SYS_FSL_SEC_ADDR;
  269. struct rng4tst __iomem *rng =
  270. (struct rng4tst __iomem *)&sec->rng;
  271. memset(&op, 0, sizeof(struct result));
  272. desc = malloc(sizeof(int) * 6);
  273. if (!desc) {
  274. printf("cannot allocate RNG init descriptor memory\n");
  275. return -1;
  276. }
  277. inline_cnstr_jobdesc_rng_instantiation(desc);
  278. ret = run_descriptor_jr(desc);
  279. if (ret)
  280. printf("RNG: Instantiation failed with error %x\n", ret);
  281. rdsta_val = sec_in32(&rng->rdsta);
  282. if (op.status || !(rdsta_val & RNG_STATE0_HANDLE_INSTANTIATED))
  283. return -1;
  284. return ret;
  285. }
  286. static u8 get_rng_vid(void)
  287. {
  288. ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
  289. u32 cha_vid = sec_in32(&sec->chavid_ls);
  290. return (cha_vid & SEC_CHAVID_RNG_LS_MASK) >> SEC_CHAVID_LS_RNG_SHIFT;
  291. }
  292. /*
  293. * By default, the TRNG runs for 200 clocks per sample;
  294. * 1200 clocks per sample generates better entropy.
  295. */
  296. static void kick_trng(int ent_delay)
  297. {
  298. ccsr_sec_t __iomem *sec =
  299. (ccsr_sec_t __iomem *)CONFIG_SYS_FSL_SEC_ADDR;
  300. struct rng4tst __iomem *rng =
  301. (struct rng4tst __iomem *)&sec->rng;
  302. u32 val;
  303. /* put RNG4 into program mode */
  304. sec_setbits32(&rng->rtmctl, RTMCTL_PRGM);
  305. /* rtsdctl bits 0-15 contain "Entropy Delay, which defines the
  306. * length (in system clocks) of each Entropy sample taken
  307. * */
  308. val = sec_in32(&rng->rtsdctl);
  309. val = (val & ~RTSDCTL_ENT_DLY_MASK) |
  310. (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
  311. sec_out32(&rng->rtsdctl, val);
  312. /* min. freq. count, equal to 1/4 of the entropy sample length */
  313. sec_out32(&rng->rtfreqmin, ent_delay >> 2);
  314. /* max. freq. count, equal to 8 times the entropy sample length */
  315. sec_out32(&rng->rtfreqmax, ent_delay << 3);
  316. /* put RNG4 into run mode */
  317. sec_clrbits32(&rng->rtmctl, RTMCTL_PRGM);
  318. }
  319. static int rng_init(void)
  320. {
  321. int ret, ent_delay = RTSDCTL_ENT_DLY_MIN;
  322. ccsr_sec_t __iomem *sec =
  323. (ccsr_sec_t __iomem *)CONFIG_SYS_FSL_SEC_ADDR;
  324. struct rng4tst __iomem *rng =
  325. (struct rng4tst __iomem *)&sec->rng;
  326. u32 rdsta = sec_in32(&rng->rdsta);
  327. /* Check if RNG state 0 handler is already instantiated */
  328. if (rdsta & RNG_STATE0_HANDLE_INSTANTIATED)
  329. return 0;
  330. do {
  331. /*
  332. * If either of the SH's were instantiated by somebody else
  333. * then it is assumed that the entropy
  334. * parameters are properly set and thus the function
  335. * setting these (kick_trng(...)) is skipped.
  336. * Also, if a handle was instantiated, do not change
  337. * the TRNG parameters.
  338. */
  339. kick_trng(ent_delay);
  340. ent_delay += 400;
  341. /*
  342. * if instantiate_rng(...) fails, the loop will rerun
  343. * and the kick_trng(...) function will modfiy the
  344. * upper and lower limits of the entropy sampling
  345. * interval, leading to a sucessful initialization of
  346. * the RNG.
  347. */
  348. ret = instantiate_rng();
  349. } while ((ret == -1) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
  350. if (ret) {
  351. printf("RNG: Failed to instantiate RNG\n");
  352. return ret;
  353. }
  354. /* Enable RDB bit so that RNG works faster */
  355. sec_setbits32(&sec->scfgr, SEC_SCFGR_RDBENABLE);
  356. return ret;
  357. }
  358. int sec_init(void)
  359. {
  360. int ret = 0;
  361. #ifdef CONFIG_PHYS_64BIT
  362. ccsr_sec_t *sec = (void *)CONFIG_SYS_FSL_SEC_ADDR;
  363. uint32_t mcr = sec_in32(&sec->mcfgr);
  364. sec_out32(&sec->mcfgr, mcr | 1 << MCFGR_PS_SHIFT);
  365. #endif
  366. ret = jr_init();
  367. if (ret < 0) {
  368. printf("SEC initialization failed\n");
  369. return -1;
  370. }
  371. if (get_rng_vid() >= 4) {
  372. if (rng_init() < 0) {
  373. printf("RNG instantiation failed\n");
  374. return -1;
  375. }
  376. printf("SEC: RNG instantiated\n");
  377. }
  378. return ret;
  379. }