jr.c 16 KB

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