jr.c 16 KB

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