jr.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2008-2014 Freescale Semiconductor, Inc.
  4. *
  5. * Based on CAAM driver in drivers/crypto/caam in Linux
  6. */
  7. #include <common.h>
  8. #include <malloc.h>
  9. #include "fsl_sec.h"
  10. #include "jr.h"
  11. #include "jobdesc.h"
  12. #include "desc_constr.h"
  13. #ifdef CONFIG_FSL_CORENET
  14. #include <asm/fsl_pamu.h>
  15. #endif
  16. #define CIRC_CNT(head, tail, size) (((head) - (tail)) & (size - 1))
  17. #define CIRC_SPACE(head, tail, size) CIRC_CNT((tail), (head) + 1, (size))
  18. uint32_t sec_offset[CONFIG_SYS_FSL_MAX_NUM_OF_SEC] = {
  19. 0,
  20. #if defined(CONFIG_ARCH_C29X)
  21. CONFIG_SYS_FSL_SEC_IDX_OFFSET,
  22. 2 * CONFIG_SYS_FSL_SEC_IDX_OFFSET
  23. #endif
  24. };
  25. #define SEC_ADDR(idx) \
  26. ((CONFIG_SYS_FSL_SEC_ADDR + sec_offset[idx]))
  27. #define SEC_JR0_ADDR(idx) \
  28. (SEC_ADDR(idx) + \
  29. (CONFIG_SYS_FSL_JR0_OFFSET - CONFIG_SYS_FSL_SEC_OFFSET))
  30. struct jobring jr0[CONFIG_SYS_FSL_MAX_NUM_OF_SEC];
  31. static inline void start_jr0(uint8_t sec_idx)
  32. {
  33. ccsr_sec_t *sec = (void *)SEC_ADDR(sec_idx);
  34. u32 ctpr_ms = sec_in32(&sec->ctpr_ms);
  35. u32 scfgr = sec_in32(&sec->scfgr);
  36. if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_INCL) {
  37. /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or
  38. * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SEC_SCFGR_VIRT_EN = 1
  39. */
  40. if ((ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR) ||
  41. (scfgr & SEC_SCFGR_VIRT_EN))
  42. sec_out32(&sec->jrstartr, CONFIG_JRSTARTR_JR0);
  43. } else {
  44. /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */
  45. if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR)
  46. sec_out32(&sec->jrstartr, CONFIG_JRSTARTR_JR0);
  47. }
  48. }
  49. static inline void jr_reset_liodn(uint8_t sec_idx)
  50. {
  51. ccsr_sec_t *sec = (void *)SEC_ADDR(sec_idx);
  52. sec_out32(&sec->jrliodnr[0].ls, 0);
  53. }
  54. static inline void jr_disable_irq(uint8_t sec_idx)
  55. {
  56. struct jr_regs *regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx);
  57. uint32_t jrcfg = sec_in32(&regs->jrcfg1);
  58. jrcfg = jrcfg | JR_INTMASK;
  59. sec_out32(&regs->jrcfg1, jrcfg);
  60. }
  61. static void jr_initregs(uint8_t sec_idx)
  62. {
  63. struct jr_regs *regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx);
  64. struct jobring *jr = &jr0[sec_idx];
  65. phys_addr_t ip_base = virt_to_phys((void *)jr->input_ring);
  66. phys_addr_t op_base = virt_to_phys((void *)jr->output_ring);
  67. #ifdef CONFIG_PHYS_64BIT
  68. sec_out32(&regs->irba_h, ip_base >> 32);
  69. #else
  70. sec_out32(&regs->irba_h, 0x0);
  71. #endif
  72. sec_out32(&regs->irba_l, (uint32_t)ip_base);
  73. #ifdef CONFIG_PHYS_64BIT
  74. sec_out32(&regs->orba_h, op_base >> 32);
  75. #else
  76. sec_out32(&regs->orba_h, 0x0);
  77. #endif
  78. sec_out32(&regs->orba_l, (uint32_t)op_base);
  79. sec_out32(&regs->ors, JR_SIZE);
  80. sec_out32(&regs->irs, JR_SIZE);
  81. if (!jr->irq)
  82. jr_disable_irq(sec_idx);
  83. }
  84. static int jr_init(uint8_t sec_idx)
  85. {
  86. struct jobring *jr = &jr0[sec_idx];
  87. memset(jr, 0, sizeof(struct jobring));
  88. jr->jq_id = DEFAULT_JR_ID;
  89. jr->irq = DEFAULT_IRQ;
  90. #ifdef CONFIG_FSL_CORENET
  91. jr->liodn = DEFAULT_JR_LIODN;
  92. #endif
  93. jr->size = JR_SIZE;
  94. jr->input_ring = (dma_addr_t *)memalign(ARCH_DMA_MINALIGN,
  95. JR_SIZE * sizeof(dma_addr_t));
  96. if (!jr->input_ring)
  97. return -1;
  98. jr->op_size = roundup(JR_SIZE * sizeof(struct op_ring),
  99. ARCH_DMA_MINALIGN);
  100. jr->output_ring =
  101. (struct op_ring *)memalign(ARCH_DMA_MINALIGN, jr->op_size);
  102. if (!jr->output_ring)
  103. return -1;
  104. memset(jr->input_ring, 0, JR_SIZE * sizeof(dma_addr_t));
  105. memset(jr->output_ring, 0, jr->op_size);
  106. start_jr0(sec_idx);
  107. jr_initregs(sec_idx);
  108. return 0;
  109. }
  110. static int jr_sw_cleanup(uint8_t sec_idx)
  111. {
  112. struct jobring *jr = &jr0[sec_idx];
  113. jr->head = 0;
  114. jr->tail = 0;
  115. jr->read_idx = 0;
  116. jr->write_idx = 0;
  117. memset(jr->info, 0, sizeof(jr->info));
  118. memset(jr->input_ring, 0, jr->size * sizeof(dma_addr_t));
  119. memset(jr->output_ring, 0, jr->size * sizeof(struct op_ring));
  120. return 0;
  121. }
  122. static int jr_hw_reset(uint8_t sec_idx)
  123. {
  124. struct jr_regs *regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx);
  125. uint32_t timeout = 100000;
  126. uint32_t jrint, jrcr;
  127. sec_out32(&regs->jrcr, JRCR_RESET);
  128. do {
  129. jrint = sec_in32(&regs->jrint);
  130. } while (((jrint & JRINT_ERR_HALT_MASK) ==
  131. JRINT_ERR_HALT_INPROGRESS) && --timeout);
  132. jrint = sec_in32(&regs->jrint);
  133. if (((jrint & JRINT_ERR_HALT_MASK) !=
  134. JRINT_ERR_HALT_INPROGRESS) && timeout == 0)
  135. return -1;
  136. timeout = 100000;
  137. sec_out32(&regs->jrcr, JRCR_RESET);
  138. do {
  139. jrcr = sec_in32(&regs->jrcr);
  140. } while ((jrcr & JRCR_RESET) && --timeout);
  141. if (timeout == 0)
  142. return -1;
  143. return 0;
  144. }
  145. /* -1 --- error, can't enqueue -- no space available */
  146. static int jr_enqueue(uint32_t *desc_addr,
  147. void (*callback)(uint32_t status, void *arg),
  148. void *arg, uint8_t sec_idx)
  149. {
  150. struct jr_regs *regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx);
  151. struct jobring *jr = &jr0[sec_idx];
  152. int head = jr->head;
  153. uint32_t desc_word;
  154. int length = desc_len(desc_addr);
  155. int i;
  156. #ifdef CONFIG_PHYS_64BIT
  157. uint32_t *addr_hi, *addr_lo;
  158. #endif
  159. /* The descriptor must be submitted to SEC block as per endianness
  160. * of the SEC Block.
  161. * So, if the endianness of Core and SEC block is different, each word
  162. * of the descriptor will be byte-swapped.
  163. */
  164. for (i = 0; i < length; i++) {
  165. desc_word = desc_addr[i];
  166. sec_out32((uint32_t *)&desc_addr[i], desc_word);
  167. }
  168. phys_addr_t desc_phys_addr = virt_to_phys(desc_addr);
  169. jr->info[head].desc_phys_addr = desc_phys_addr;
  170. jr->info[head].callback = (void *)callback;
  171. jr->info[head].arg = arg;
  172. jr->info[head].op_done = 0;
  173. unsigned long start = (unsigned long)&jr->info[head] &
  174. ~(ARCH_DMA_MINALIGN - 1);
  175. unsigned long end = ALIGN((unsigned long)&jr->info[head] +
  176. sizeof(struct jr_info), ARCH_DMA_MINALIGN);
  177. flush_dcache_range(start, end);
  178. #ifdef CONFIG_PHYS_64BIT
  179. /* Write the 64 bit Descriptor address on Input Ring.
  180. * The 32 bit hign and low part of the address will
  181. * depend on endianness of SEC block.
  182. */
  183. #ifdef CONFIG_SYS_FSL_SEC_LE
  184. addr_lo = (uint32_t *)(&jr->input_ring[head]);
  185. addr_hi = (uint32_t *)(&jr->input_ring[head]) + 1;
  186. #elif defined(CONFIG_SYS_FSL_SEC_BE)
  187. addr_hi = (uint32_t *)(&jr->input_ring[head]);
  188. addr_lo = (uint32_t *)(&jr->input_ring[head]) + 1;
  189. #endif /* ifdef CONFIG_SYS_FSL_SEC_LE */
  190. sec_out32(addr_hi, (uint32_t)(desc_phys_addr >> 32));
  191. sec_out32(addr_lo, (uint32_t)(desc_phys_addr));
  192. #else
  193. /* Write the 32 bit Descriptor address on Input Ring. */
  194. sec_out32(&jr->input_ring[head], desc_phys_addr);
  195. #endif /* ifdef CONFIG_PHYS_64BIT */
  196. start = (unsigned long)&jr->input_ring[head] & ~(ARCH_DMA_MINALIGN - 1);
  197. end = ALIGN((unsigned long)&jr->input_ring[head] +
  198. sizeof(dma_addr_t), ARCH_DMA_MINALIGN);
  199. flush_dcache_range(start, end);
  200. jr->head = (head + 1) & (jr->size - 1);
  201. /* Invalidate output ring */
  202. start = (unsigned long)jr->output_ring &
  203. ~(ARCH_DMA_MINALIGN - 1);
  204. end = ALIGN((unsigned long)jr->output_ring + jr->op_size,
  205. ARCH_DMA_MINALIGN);
  206. invalidate_dcache_range(start, end);
  207. sec_out32(&regs->irja, 1);
  208. return 0;
  209. }
  210. static int jr_dequeue(int sec_idx)
  211. {
  212. struct jr_regs *regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx);
  213. struct jobring *jr = &jr0[sec_idx];
  214. int head = jr->head;
  215. int tail = jr->tail;
  216. int idx, i, found;
  217. void (*callback)(uint32_t status, void *arg);
  218. void *arg = NULL;
  219. #ifdef CONFIG_PHYS_64BIT
  220. uint32_t *addr_hi, *addr_lo;
  221. #else
  222. uint32_t *addr;
  223. #endif
  224. while (sec_in32(&regs->orsf) && CIRC_CNT(jr->head, jr->tail,
  225. jr->size)) {
  226. found = 0;
  227. phys_addr_t op_desc;
  228. #ifdef CONFIG_PHYS_64BIT
  229. /* Read the 64 bit Descriptor address from Output Ring.
  230. * The 32 bit hign and low part of the address will
  231. * depend on endianness of SEC block.
  232. */
  233. #ifdef CONFIG_SYS_FSL_SEC_LE
  234. addr_lo = (uint32_t *)(&jr->output_ring[jr->tail].desc);
  235. addr_hi = (uint32_t *)(&jr->output_ring[jr->tail].desc) + 1;
  236. #elif defined(CONFIG_SYS_FSL_SEC_BE)
  237. addr_hi = (uint32_t *)(&jr->output_ring[jr->tail].desc);
  238. addr_lo = (uint32_t *)(&jr->output_ring[jr->tail].desc) + 1;
  239. #endif /* ifdef CONFIG_SYS_FSL_SEC_LE */
  240. op_desc = ((u64)sec_in32(addr_hi) << 32) |
  241. ((u64)sec_in32(addr_lo));
  242. #else
  243. /* Read the 32 bit Descriptor address from Output Ring. */
  244. addr = (uint32_t *)&jr->output_ring[jr->tail].desc;
  245. op_desc = sec_in32(addr);
  246. #endif /* ifdef CONFIG_PHYS_64BIT */
  247. uint32_t status = sec_in32(&jr->output_ring[jr->tail].status);
  248. for (i = 0; CIRC_CNT(head, tail + i, jr->size) >= 1; i++) {
  249. idx = (tail + i) & (jr->size - 1);
  250. if (op_desc == jr->info[idx].desc_phys_addr) {
  251. found = 1;
  252. break;
  253. }
  254. }
  255. /* Error condition if match not found */
  256. if (!found)
  257. return -1;
  258. jr->info[idx].op_done = 1;
  259. callback = (void *)jr->info[idx].callback;
  260. arg = jr->info[idx].arg;
  261. /* When the job on tail idx gets done, increment
  262. * tail till the point where job completed out of oredr has
  263. * been taken into account
  264. */
  265. if (idx == tail)
  266. do {
  267. tail = (tail + 1) & (jr->size - 1);
  268. } while (jr->info[tail].op_done);
  269. jr->tail = tail;
  270. jr->read_idx = (jr->read_idx + 1) & (jr->size - 1);
  271. sec_out32(&regs->orjr, 1);
  272. jr->info[idx].op_done = 0;
  273. callback(status, arg);
  274. }
  275. return 0;
  276. }
  277. static void desc_done(uint32_t status, void *arg)
  278. {
  279. struct result *x = arg;
  280. x->status = status;
  281. #ifndef CONFIG_SPL_BUILD
  282. caam_jr_strstatus(status);
  283. #endif
  284. x->done = 1;
  285. }
  286. static inline int run_descriptor_jr_idx(uint32_t *desc, uint8_t sec_idx)
  287. {
  288. unsigned long long timeval = get_ticks();
  289. unsigned long long timeout = usec2ticks(CONFIG_SEC_DEQ_TIMEOUT);
  290. struct result op;
  291. int ret = 0;
  292. memset(&op, 0, sizeof(op));
  293. ret = jr_enqueue(desc, desc_done, &op, sec_idx);
  294. if (ret) {
  295. debug("Error in SEC enq\n");
  296. ret = JQ_ENQ_ERR;
  297. goto out;
  298. }
  299. timeval = get_ticks();
  300. timeout = usec2ticks(CONFIG_SEC_DEQ_TIMEOUT);
  301. while (op.done != 1) {
  302. ret = jr_dequeue(sec_idx);
  303. if (ret) {
  304. debug("Error in SEC deq\n");
  305. ret = JQ_DEQ_ERR;
  306. goto out;
  307. }
  308. if ((get_ticks() - timeval) > timeout) {
  309. debug("SEC Dequeue timed out\n");
  310. ret = JQ_DEQ_TO_ERR;
  311. goto out;
  312. }
  313. }
  314. if (op.status) {
  315. debug("Error %x\n", op.status);
  316. ret = op.status;
  317. }
  318. out:
  319. return ret;
  320. }
  321. int run_descriptor_jr(uint32_t *desc)
  322. {
  323. return run_descriptor_jr_idx(desc, 0);
  324. }
  325. static inline int jr_reset_sec(uint8_t sec_idx)
  326. {
  327. if (jr_hw_reset(sec_idx) < 0)
  328. return -1;
  329. /* Clean up the jobring structure maintained by software */
  330. jr_sw_cleanup(sec_idx);
  331. return 0;
  332. }
  333. int jr_reset(void)
  334. {
  335. return jr_reset_sec(0);
  336. }
  337. static inline int sec_reset_idx(uint8_t sec_idx)
  338. {
  339. ccsr_sec_t *sec = (void *)SEC_ADDR(sec_idx);
  340. uint32_t mcfgr = sec_in32(&sec->mcfgr);
  341. uint32_t timeout = 100000;
  342. mcfgr |= MCFGR_SWRST;
  343. sec_out32(&sec->mcfgr, mcfgr);
  344. mcfgr |= MCFGR_DMA_RST;
  345. sec_out32(&sec->mcfgr, mcfgr);
  346. do {
  347. mcfgr = sec_in32(&sec->mcfgr);
  348. } while ((mcfgr & MCFGR_DMA_RST) == MCFGR_DMA_RST && --timeout);
  349. if (timeout == 0)
  350. return -1;
  351. timeout = 100000;
  352. do {
  353. mcfgr = sec_in32(&sec->mcfgr);
  354. } while ((mcfgr & MCFGR_SWRST) == MCFGR_SWRST && --timeout);
  355. if (timeout == 0)
  356. return -1;
  357. return 0;
  358. }
  359. int sec_reset(void)
  360. {
  361. return sec_reset_idx(0);
  362. }
  363. #ifndef CONFIG_SPL_BUILD
  364. static int instantiate_rng(uint8_t sec_idx)
  365. {
  366. u32 *desc;
  367. u32 rdsta_val;
  368. int ret = 0, sh_idx, size;
  369. ccsr_sec_t __iomem *sec = (ccsr_sec_t __iomem *)SEC_ADDR(sec_idx);
  370. struct rng4tst __iomem *rng =
  371. (struct rng4tst __iomem *)&sec->rng;
  372. desc = memalign(ARCH_DMA_MINALIGN, sizeof(uint32_t) * 6);
  373. if (!desc) {
  374. printf("cannot allocate RNG init descriptor memory\n");
  375. return -1;
  376. }
  377. for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) {
  378. /*
  379. * If the corresponding bit is set, this state handle
  380. * was initialized by somebody else, so it's left alone.
  381. */
  382. rdsta_val = sec_in32(&rng->rdsta) & RNG_STATE_HANDLE_MASK;
  383. if (rdsta_val & (1 << sh_idx))
  384. continue;
  385. inline_cnstr_jobdesc_rng_instantiation(desc, sh_idx);
  386. size = roundup(sizeof(uint32_t) * 6, ARCH_DMA_MINALIGN);
  387. flush_dcache_range((unsigned long)desc,
  388. (unsigned long)desc + size);
  389. ret = run_descriptor_jr_idx(desc, sec_idx);
  390. if (ret)
  391. printf("RNG: Instantiation failed with error 0x%x\n",
  392. ret);
  393. rdsta_val = sec_in32(&rng->rdsta) & RNG_STATE_HANDLE_MASK;
  394. if (!(rdsta_val & (1 << sh_idx))) {
  395. free(desc);
  396. return -1;
  397. }
  398. memset(desc, 0, sizeof(uint32_t) * 6);
  399. }
  400. free(desc);
  401. return ret;
  402. }
  403. static u8 get_rng_vid(uint8_t sec_idx)
  404. {
  405. ccsr_sec_t *sec = (void *)SEC_ADDR(sec_idx);
  406. u32 cha_vid = sec_in32(&sec->chavid_ls);
  407. return (cha_vid & SEC_CHAVID_RNG_LS_MASK) >> SEC_CHAVID_LS_RNG_SHIFT;
  408. }
  409. /*
  410. * By default, the TRNG runs for 200 clocks per sample;
  411. * 1200 clocks per sample generates better entropy.
  412. */
  413. static void kick_trng(int ent_delay, uint8_t sec_idx)
  414. {
  415. ccsr_sec_t __iomem *sec = (ccsr_sec_t __iomem *)SEC_ADDR(sec_idx);
  416. struct rng4tst __iomem *rng =
  417. (struct rng4tst __iomem *)&sec->rng;
  418. u32 val;
  419. /* put RNG4 into program mode */
  420. sec_setbits32(&rng->rtmctl, RTMCTL_PRGM);
  421. /* rtsdctl bits 0-15 contain "Entropy Delay, which defines the
  422. * length (in system clocks) of each Entropy sample taken
  423. * */
  424. val = sec_in32(&rng->rtsdctl);
  425. val = (val & ~RTSDCTL_ENT_DLY_MASK) |
  426. (ent_delay << RTSDCTL_ENT_DLY_SHIFT);
  427. sec_out32(&rng->rtsdctl, val);
  428. /* min. freq. count, equal to 1/4 of the entropy sample length */
  429. sec_out32(&rng->rtfreqmin, ent_delay >> 2);
  430. /* disable maximum frequency count */
  431. sec_out32(&rng->rtfreqmax, RTFRQMAX_DISABLE);
  432. /*
  433. * select raw sampling in both entropy shifter
  434. * and statistical checker
  435. */
  436. sec_setbits32(&rng->rtmctl, RTMCTL_SAMP_MODE_RAW_ES_SC);
  437. /* put RNG4 into run mode */
  438. sec_clrbits32(&rng->rtmctl, RTMCTL_PRGM);
  439. }
  440. static int rng_init(uint8_t sec_idx)
  441. {
  442. int ret, ent_delay = RTSDCTL_ENT_DLY_MIN;
  443. ccsr_sec_t __iomem *sec = (ccsr_sec_t __iomem *)SEC_ADDR(sec_idx);
  444. struct rng4tst __iomem *rng =
  445. (struct rng4tst __iomem *)&sec->rng;
  446. u32 inst_handles;
  447. do {
  448. inst_handles = sec_in32(&rng->rdsta) & RNG_STATE_HANDLE_MASK;
  449. /*
  450. * If either of the SH's were instantiated by somebody else
  451. * then it is assumed that the entropy
  452. * parameters are properly set and thus the function
  453. * setting these (kick_trng(...)) is skipped.
  454. * Also, if a handle was instantiated, do not change
  455. * the TRNG parameters.
  456. */
  457. if (!inst_handles) {
  458. kick_trng(ent_delay, sec_idx);
  459. ent_delay += 400;
  460. }
  461. /*
  462. * if instantiate_rng(...) fails, the loop will rerun
  463. * and the kick_trng(...) function will modfiy the
  464. * upper and lower limits of the entropy sampling
  465. * interval, leading to a sucessful initialization of
  466. * the RNG.
  467. */
  468. ret = instantiate_rng(sec_idx);
  469. } while ((ret == -1) && (ent_delay < RTSDCTL_ENT_DLY_MAX));
  470. if (ret) {
  471. printf("RNG: Failed to instantiate RNG\n");
  472. return ret;
  473. }
  474. /* Enable RDB bit so that RNG works faster */
  475. sec_setbits32(&sec->scfgr, SEC_SCFGR_RDBENABLE);
  476. return ret;
  477. }
  478. #endif
  479. int sec_init_idx(uint8_t sec_idx)
  480. {
  481. ccsr_sec_t *sec = (void *)SEC_ADDR(sec_idx);
  482. uint32_t mcr = sec_in32(&sec->mcfgr);
  483. uint32_t jrown_ns;
  484. int i;
  485. int ret = 0;
  486. #ifdef CONFIG_FSL_CORENET
  487. uint32_t liodnr;
  488. uint32_t liodn_ns;
  489. uint32_t liodn_s;
  490. #endif
  491. if (!(sec_idx < CONFIG_SYS_FSL_MAX_NUM_OF_SEC)) {
  492. printf("SEC initialization failed\n");
  493. return -1;
  494. }
  495. /*
  496. * Modifying CAAM Read/Write Attributes
  497. * For LS2080A
  498. * For AXI Write - Cacheable, Write Back, Write allocate
  499. * For AXI Read - Cacheable, Read allocate
  500. * Only For LS2080a, to solve CAAM coherency issues
  501. */
  502. #ifdef CONFIG_ARCH_LS2080A
  503. mcr = (mcr & ~MCFGR_AWCACHE_MASK) | (0xb << MCFGR_AWCACHE_SHIFT);
  504. mcr = (mcr & ~MCFGR_ARCACHE_MASK) | (0x6 << MCFGR_ARCACHE_SHIFT);
  505. #else
  506. mcr = (mcr & ~MCFGR_AWCACHE_MASK) | (0x2 << MCFGR_AWCACHE_SHIFT);
  507. #endif
  508. #ifdef CONFIG_PHYS_64BIT
  509. mcr |= (1 << MCFGR_PS_SHIFT);
  510. #endif
  511. sec_out32(&sec->mcfgr, mcr);
  512. #ifdef CONFIG_FSL_CORENET
  513. #ifdef CONFIG_SPL_BUILD
  514. /*
  515. * For SPL Build, Set the Liodns in SEC JR0 for
  516. * creating PAMU entries corresponding to these.
  517. * For normal build, these are set in set_liodns().
  518. */
  519. liodn_ns = CONFIG_SPL_JR0_LIODN_NS & JRNSLIODN_MASK;
  520. liodn_s = CONFIG_SPL_JR0_LIODN_S & JRSLIODN_MASK;
  521. liodnr = sec_in32(&sec->jrliodnr[0].ls) &
  522. ~(JRNSLIODN_MASK | JRSLIODN_MASK);
  523. liodnr = liodnr |
  524. (liodn_ns << JRNSLIODN_SHIFT) |
  525. (liodn_s << JRSLIODN_SHIFT);
  526. sec_out32(&sec->jrliodnr[0].ls, liodnr);
  527. #else
  528. liodnr = sec_in32(&sec->jrliodnr[0].ls);
  529. liodn_ns = (liodnr & JRNSLIODN_MASK) >> JRNSLIODN_SHIFT;
  530. liodn_s = (liodnr & JRSLIODN_MASK) >> JRSLIODN_SHIFT;
  531. #endif
  532. #endif
  533. /* Set ownership of job rings to non-TrustZone mode by default */
  534. for (i = 0; i < ARRAY_SIZE(sec->jrliodnr); i++) {
  535. jrown_ns = sec_in32(&sec->jrliodnr[i].ms);
  536. jrown_ns |= JROWN_NS | JRMID_NS;
  537. sec_out32(&sec->jrliodnr[i].ms, jrown_ns);
  538. }
  539. ret = jr_init(sec_idx);
  540. if (ret < 0) {
  541. printf("SEC initialization failed\n");
  542. return -1;
  543. }
  544. #ifdef CONFIG_FSL_CORENET
  545. ret = sec_config_pamu_table(liodn_ns, liodn_s);
  546. if (ret < 0)
  547. return -1;
  548. pamu_enable();
  549. #endif
  550. #ifndef CONFIG_SPL_BUILD
  551. if (get_rng_vid(sec_idx) >= 4) {
  552. if (rng_init(sec_idx) < 0) {
  553. printf("SEC%u: RNG instantiation failed\n", sec_idx);
  554. return -1;
  555. }
  556. printf("SEC%u: RNG instantiated\n", sec_idx);
  557. }
  558. #endif
  559. return ret;
  560. }
  561. int sec_init(void)
  562. {
  563. return sec_init_idx(0);
  564. }