fsl_xrdc2.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /*
  2. * Copyright 2019-2020 NXP
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include "fsl_xrdc2.h"
  8. /*******************************************************************************
  9. * Definitions
  10. ******************************************************************************/
  11. /* Component ID definition, used by tools. */
  12. #ifndef FSL_COMPONENT_ID
  13. #define FSL_COMPONENT_ID "platform.drivers.xrdc2"
  14. #endif
  15. /* Definitions for access policy. */
  16. #define XRDC2_DXACP_WIDTH (3U)
  17. #define XRDC2_DXACP_MASK ((1UL << XRDC2_DXACP_WIDTH) - 1U)
  18. #define XRDC2_DXACP_0_7(domainId, dxacp) ((uint32_t)(dxacp) << (XRDC2_DXACP_WIDTH * (uint32_t)(domainId)))
  19. #define XRDC2_DXACP_8_15(domainId, dxacp) ((uint32_t)(dxacp) << (XRDC2_DXACP_WIDTH * ((uint32_t)(domainId)-8U)))
  20. #define XRDC2_DXACP_0_7_MASK(domainId) XRDC2_DXACP_0_7(domainId, XRDC2_DXACP_MASK)
  21. #define XRDC2_DXACP_8_15_MASK(domainId) XRDC2_DXACP_8_15(domainId, XRDC2_DXACP_MASK)
  22. /* Memory region alignment. */
  23. #define XRDC2_MRGD_ADDR_ALIGN_MASK (0x00000FFFU)
  24. /*******************************************************************************
  25. * Prototypes
  26. ******************************************************************************/
  27. static void XRDC2_MakeDXACP(const xrdc2_access_policy_t policy[FSL_FEATURE_XRDC2_DOMAIN_COUNT],
  28. uint32_t *w0,
  29. uint32_t *w1);
  30. /*******************************************************************************
  31. * Variables
  32. ******************************************************************************/
  33. /*******************************************************************************
  34. * Code
  35. ******************************************************************************/
  36. static void XRDC2_MakeDXACP(const xrdc2_access_policy_t policy[FSL_FEATURE_XRDC2_DOMAIN_COUNT],
  37. uint32_t *w0,
  38. uint32_t *w1)
  39. {
  40. uint32_t domain = (uint32_t)FSL_FEATURE_XRDC2_DOMAIN_COUNT;
  41. *w0 = 0U;
  42. *w1 = 0U;
  43. #if (FSL_FEATURE_XRDC2_DOMAIN_COUNT > 8)
  44. while (domain > 8U)
  45. {
  46. domain--;
  47. *w0 <<= XRDC2_DXACP_WIDTH;
  48. *w0 |= (uint32_t)policy[domain - 8U];
  49. }
  50. #endif
  51. while (domain > 0U)
  52. {
  53. domain--;
  54. *w1 <<= XRDC2_DXACP_WIDTH;
  55. *w1 |= (uint32_t)policy[domain];
  56. }
  57. }
  58. /*!
  59. * brief Initializes the XRDC2 module.
  60. *
  61. * This function enables the XRDC2 clock.
  62. *
  63. * param base XRDC2 peripheral base address.
  64. */
  65. void XRDC2_Init(XRDC2_Type *base)
  66. {
  67. }
  68. /*!
  69. * brief De-initializes the XRDC2 module.
  70. *
  71. * This function disables the XRDC2 clock.
  72. *
  73. * param base XRDC2 peripheral base address.
  74. */
  75. void XRDC2_Deinit(XRDC2_Type *base)
  76. {
  77. }
  78. /*!
  79. * brief Sets the XRDC2 global valid.
  80. *
  81. * This function sets the XRDC2 global valid or invalid. When the XRDC2 is global
  82. * invalid, all accesses from all bus masters to all slaves are allowed.
  83. *
  84. * param base XRDC2 peripheral base address.
  85. * param valid True to valid XRDC2.
  86. */
  87. void XRDC2_SetGlobalValid(XRDC2_Type *base, bool valid)
  88. {
  89. uint32_t mcr = base->MCR & ~(XRDC2_MCR_GVLDM_MASK | XRDC2_MCR_GVLDC_MASK);
  90. if (valid)
  91. {
  92. mcr |= XRDC2_MCR_GVLDM_MASK;
  93. base->MCR = mcr;
  94. /* Two dummy read to ensure the configuration takes effect. */
  95. (void)base->MCR;
  96. (void)base->MCR;
  97. mcr |= XRDC2_MCR_GVLDC_MASK;
  98. base->MCR = mcr;
  99. }
  100. else
  101. {
  102. base->MCR = mcr;
  103. }
  104. }
  105. /*!
  106. * brief Gets the default master domain assignment.
  107. *
  108. * This function sets the assignment as follows:
  109. *
  110. * code
  111. * config->lock = false;
  112. * config->privilegeAttr = kXRDC2_MasterPrivilege;
  113. * config->secureAttr = kXRDC2_MasterSecure;
  114. * config->domainId = 0U;
  115. * config->mask = 0U;
  116. * config->match = 0U;
  117. * endcode
  118. *
  119. * param assignment Pointer to the assignment structure.
  120. */
  121. void XRDC2_GetDefaultMasterDomainAssignment(xrdc2_master_domain_assignment_t *assignment)
  122. {
  123. assert(NULL != assignment);
  124. assignment->lock = false;
  125. assignment->privilegeAttr = kXRDC2_MasterPrivilege;
  126. assignment->secureAttr = kXRDC2_MasterSecure;
  127. assignment->domainId = 0U;
  128. assignment->mask = 0U;
  129. assignment->match = 0U;
  130. }
  131. /*!
  132. * brief Sets the processor bus master domain assignment.
  133. *
  134. * param base XRDC2 peripheral base address.
  135. * param master Which master to configure.
  136. * param assignIndex Which assignment register to set.
  137. * param assignment Pointer to the assignment structure.
  138. */
  139. void XRDC2_SetMasterDomainAssignment(XRDC2_Type *base,
  140. xrdc2_master_t master,
  141. uint8_t assignIndex,
  142. const xrdc2_master_domain_assignment_t *assignment)
  143. {
  144. assert(NULL != assignment);
  145. uint32_t w0;
  146. uint32_t w1;
  147. w0 = (((uint32_t)assignment->mask << XRDC2_MDAC_MDA_W0_MASK_SHIFT) |
  148. ((uint32_t)assignment->match << XRDC2_MDAC_MDA_W0_MATCH_SHIFT));
  149. w1 = ((uint32_t)assignment->domainId << XRDC2_MDAC_MDA_W1_DID_SHIFT) |
  150. (XRDC2_MDAC_MDA_W1_PA(assignment->privilegeAttr)) | (XRDC2_MDAC_MDA_W1_SA(assignment->secureAttr)) |
  151. XRDC2_MDAC_MDA_W1_VLD_MASK;
  152. if (assignment->lock)
  153. {
  154. w1 |= XRDC2_MDAC_MDA_W1_DL_MASK;
  155. }
  156. base->MDACI_MDAJ[master][assignIndex].MDAC_MDA_W0 = w0;
  157. base->MDACI_MDAJ[master][assignIndex].MDAC_MDA_W1 = w1;
  158. }
  159. /*!
  160. * brief Gets the default memory slot access configuration.
  161. *
  162. * This function sets the assignment as follows:
  163. *
  164. * code
  165. * config->lockMode = kXRDC2_AccessConfigLockDisabled;
  166. * config->policy[0] = kXRDC2_AccessPolicyNone;
  167. * config->policy[1] = kXRDC2_AccessPolicyNone;
  168. * ...
  169. * endcode
  170. *
  171. * param config Pointer to the configuration.
  172. */
  173. void XRDC2_GetMemSlotAccessDefaultConfig(xrdc2_mem_slot_access_config_t *config)
  174. {
  175. assert(NULL != config);
  176. uint8_t domain;
  177. config->lockMode = kXRDC2_AccessConfigLockDisabled;
  178. for (domain = 0; domain < (uint32_t)FSL_FEATURE_XRDC2_DOMAIN_COUNT; domain++)
  179. {
  180. config->policy[domain] = kXRDC2_AccessPolicyNone;
  181. }
  182. }
  183. /*!
  184. * brief Sets the memory slot access policy.
  185. *
  186. * param base XRDC2 peripheral base address.
  187. * param config Pointer to the access policy configuration structure.
  188. */
  189. void XRDC2_SetMemSlotAccessConfig(XRDC2_Type *base,
  190. xrdc2_mem_slot_t memSlot,
  191. const xrdc2_mem_slot_access_config_t *config)
  192. {
  193. assert(NULL != config);
  194. uint32_t w0 = 0;
  195. uint32_t w1 = 0;
  196. XRDC2_MakeDXACP(config->policy, &w0, &w1);
  197. w1 |= XRDC2_MSC_MSAC_W1_DL2(config->lockMode);
  198. w1 |= XRDC2_MSC_MSAC_W1_VLD_MASK;
  199. base->MSCI_MSAC_WK[(uint8_t)memSlot].MSC_MSAC_W0 = w0;
  200. base->MSCI_MSAC_WK[(uint8_t)memSlot].MSC_MSAC_W1 = w1;
  201. }
  202. /*!
  203. * brief Sets the memory slot descriptor as valid or invalid.
  204. *
  205. * param base XRDC2 peripheral base address.
  206. * param memSlot Which memory slot descriptor to set.
  207. * param valid True to set valid, false to set invalid.
  208. */
  209. void XRDC2_SetMemSlotAccessValid(XRDC2_Type *base, xrdc2_mem_slot_t memSlot, bool valid)
  210. {
  211. uint32_t reg = base->MSCI_MSAC_WK[(uint32_t)memSlot].MSC_MSAC_W1 & ~XRDC2_EAL_MASK;
  212. if (valid)
  213. {
  214. base->MSCI_MSAC_WK[(uint32_t)memSlot].MSC_MSAC_W1 = (reg | XRDC2_MSC_MSAC_W1_VLD_MASK);
  215. }
  216. else
  217. {
  218. base->MSCI_MSAC_WK[(uint32_t)memSlot].MSC_MSAC_W1 = (reg & ~XRDC2_MSC_MSAC_W1_VLD_MASK);
  219. }
  220. }
  221. /*!
  222. * brief Sets the memory slot descriptor lock mode.
  223. *
  224. * param base XRDC2 peripheral base address.
  225. * param memSlot Which memory slot descriptor to set.
  226. * param lockMode The lock mode to set.
  227. */
  228. void XRDC2_SetMemSlotAccessLockMode(XRDC2_Type *base, xrdc2_mem_slot_t memSlot, xrdc2_access_config_lock_t lockMode)
  229. {
  230. uint32_t reg = base->MSCI_MSAC_WK[(uint32_t)memSlot].MSC_MSAC_W1 & ~(XRDC2_EAL_MASK | XRDC2_MRC_MRGD_W6_DL2_MASK);
  231. base->MSCI_MSAC_WK[(uint32_t)memSlot].MSC_MSAC_W1 = (reg | XRDC2_MRC_MRGD_W6_DL2(lockMode));
  232. }
  233. /*!
  234. * brief Sets the memory slot access policy for specific domain.
  235. *
  236. * param base XRDC2 peripheral base address.
  237. * param memSlot The memory slot to operate.
  238. * param domainId The ID of the domain whose policy will be changed.
  239. * param policy The access policy to set.
  240. */
  241. void XRDC2_SetMemSlotDomainAccessPolicy(XRDC2_Type *base,
  242. xrdc2_mem_slot_t memSlot,
  243. uint8_t domainId,
  244. xrdc2_access_policy_t policy)
  245. {
  246. uint32_t reg;
  247. if (domainId < 8U)
  248. {
  249. reg = base->MSCI_MSAC_WK[(uint32_t)memSlot].MSC_MSAC_W0 & ~XRDC2_DXACP_0_7_MASK(domainId);
  250. reg |= XRDC2_DXACP_0_7(domainId, policy);
  251. base->MSCI_MSAC_WK[(uint32_t)memSlot].MSC_MSAC_W0 = reg;
  252. }
  253. else
  254. {
  255. reg = base->MSCI_MSAC_WK[(uint32_t)memSlot].MSC_MSAC_W1 & ~XRDC2_DXACP_8_15_MASK(domainId);
  256. reg |= XRDC2_DXACP_8_15(domainId, policy);
  257. reg &= ~XRDC2_EAL_MASK;
  258. base->MSCI_MSAC_WK[(uint32_t)memSlot].MSC_MSAC_W1 = reg;
  259. }
  260. }
  261. /*!
  262. * brief Enable or disable the memory slot exclusive access lock.
  263. *
  264. * The lock must be enabled first before use. Once disabled, it could not be
  265. * enabled until reset.
  266. *
  267. * param base XRDC2 peripheral base address.
  268. * param memSlot The memory slot to operate.
  269. * param enable True to enable, false to disable.
  270. */
  271. void XRDC2_EnableMemSlotExclAccessLock(XRDC2_Type *base, xrdc2_mem_slot_t memSlot, bool enable)
  272. {
  273. if (enable)
  274. {
  275. base->MSCI_MSAC_WK[(uint32_t)memSlot].MSC_MSAC_W1 = XRDC2_EAL_UNLOCKED;
  276. }
  277. else
  278. {
  279. base->MSCI_MSAC_WK[(uint32_t)memSlot].MSC_MSAC_W1 = XRDC2_EAL_DISABLE_UNTIL_RESET;
  280. }
  281. }
  282. /*!
  283. * @brief Get current memory slot exclusive access lock owner.
  284. *
  285. * @param base XRDC2 peripheral base address.
  286. * @param memSlot The memory slot to operate.
  287. * @return The domain ID of the lock owner.
  288. */
  289. uint8_t XRDC2_GetMemSlotExclAccessLockDomainOwner(XRDC2_Type *base, xrdc2_mem_slot_t memSlot)
  290. {
  291. return (uint8_t)((base->MSCI_MSAC_WK[(uint32_t)memSlot].MSC_MSAC_W0 & XRDC2_MSC_MSAC_W0_EALO_MASK) >>
  292. XRDC2_MSC_MSAC_W0_EALO_SHIFT);
  293. }
  294. /*!
  295. * brief Try to lock the memory slot exclusive access.
  296. *
  297. * param base XRDC2 peripheral base address.
  298. * param memSlot The memory slot to operate.
  299. * retval kStatus_Fail Failed to lock.
  300. * retval kStatus_Success Locked succussfully.
  301. */
  302. status_t XRDC2_TryLockMemSlotExclAccess(XRDC2_Type *base, xrdc2_mem_slot_t memSlot)
  303. {
  304. status_t status;
  305. uint8_t curDomainID;
  306. volatile uint32_t *lockReg = &(base->MSCI_MSAC_WK[(uint32_t)memSlot].MSC_MSAC_W1);
  307. curDomainID = XRDC2_GetCurrentMasterDomainId(base);
  308. *lockReg = XRDC2_EAL_LOCKED;
  309. if (curDomainID != XRDC2_GetMemSlotExclAccessLockDomainOwner(base, memSlot))
  310. {
  311. status = kStatus_Fail;
  312. }
  313. else
  314. {
  315. status = kStatus_Success;
  316. }
  317. return status;
  318. }
  319. /*!
  320. * brief Lock the memory slot exclusive access using blocking method.
  321. *
  322. * param base XRDC2 peripheral base address.
  323. * param memSlot The memory slot to operate.
  324. *
  325. * note This function must be called when the lock is not disabled.
  326. */
  327. void XRDC2_LockMemSlotExclAccess(XRDC2_Type *base, xrdc2_mem_slot_t memSlot)
  328. {
  329. uint8_t curDomainID;
  330. volatile uint32_t *lockReg = &(base->MSCI_MSAC_WK[(uint32_t)memSlot].MSC_MSAC_W1);
  331. curDomainID = XRDC2_GetCurrentMasterDomainId(base);
  332. while (true)
  333. {
  334. *lockReg = XRDC2_EAL_LOCKED;
  335. /* Locked and owner is current domain. */
  336. if (curDomainID == XRDC2_GetMemSlotExclAccessLockDomainOwner(base, memSlot))
  337. {
  338. break;
  339. }
  340. }
  341. return;
  342. }
  343. /*!
  344. * brief Gets the default memory access configuration.
  345. *
  346. * This function sets the assignment as follows:
  347. *
  348. * code
  349. * config->startAddr = 0U;
  350. * config->endAddr = 0xFFFFFFFFU;
  351. * config->lockMode = kXRDC2_AccessConfigLockDisabled;
  352. * config->policy[0] = kXRDC2_AccessPolicyNone;
  353. * config->policy[1] = kXRDC2_AccessPolicyNone;
  354. * ...
  355. * endcode
  356. *
  357. * param config Pointer to the configuration.
  358. */
  359. void XRDC2_GetMemAccessDefaultConfig(xrdc2_mem_access_config_t *config)
  360. {
  361. assert(NULL != config);
  362. uint8_t domain;
  363. config->startAddr = 0U;
  364. config->endAddr = 0xFFFFFFFFU;
  365. config->lockMode = kXRDC2_AccessConfigLockDisabled;
  366. for (domain = 0; domain < (uint32_t)FSL_FEATURE_XRDC2_DOMAIN_COUNT; domain++)
  367. {
  368. config->policy[domain] = kXRDC2_AccessPolicyNone;
  369. }
  370. }
  371. /*!
  372. * brief Sets the memory region access policy.
  373. *
  374. * param base XRDC2 peripheral base address.
  375. * param config Pointer to the access policy configuration structure.
  376. */
  377. void XRDC2_SetMemAccessConfig(XRDC2_Type *base, xrdc2_mem_t mem, const xrdc2_mem_access_config_t *config)
  378. {
  379. assert(NULL != config);
  380. /* Check the memory region alignment. */
  381. assert((config->startAddr & XRDC2_MRGD_ADDR_ALIGN_MASK) == 0U);
  382. assert(((config->endAddr + 1U) & XRDC2_MRGD_ADDR_ALIGN_MASK) == 0U);
  383. uint32_t w5 = 0;
  384. uint32_t w6 = 0;
  385. uint32_t mrc = XRDC2_GET_MRC((uint32_t)mem);
  386. uint32_t mrgd = XRDC2_GET_MRGD((uint32_t)mem);
  387. XRDC2_MakeDXACP(config->policy, &w5, &w6);
  388. w6 |= XRDC2_MRC_MRGD_W6_DL2(config->lockMode);
  389. w6 |= XRDC2_MRC_MRGD_W6_VLD_MASK;
  390. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W0 = config->startAddr;
  391. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W1 = 0U;
  392. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W2 = config->endAddr;
  393. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W3 = 0U;
  394. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W5 = w5;
  395. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W6 = w6;
  396. }
  397. /*!
  398. * brief Sets the memory region descriptor as valid or invalid.
  399. *
  400. * param base XRDC2 peripheral base address.
  401. * param mem Which memory region descriptor to set.
  402. * param valid True to set valid, false to set invalid.
  403. */
  404. void XRDC2_SetMemAccessValid(XRDC2_Type *base, xrdc2_mem_t mem, bool valid)
  405. {
  406. uint32_t mrc = XRDC2_GET_MRC((uint32_t)mem);
  407. uint32_t mrgd = XRDC2_GET_MRGD((uint32_t)mem);
  408. uint32_t reg = base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W6 & ~XRDC2_EAL_MASK;
  409. if (valid)
  410. {
  411. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W6 = (reg | XRDC2_MRC_MRGD_W6_VLD_MASK);
  412. }
  413. else
  414. {
  415. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W6 = (reg & ~XRDC2_MRC_MRGD_W6_VLD_MASK);
  416. }
  417. }
  418. /*!
  419. * brief Sets the memory descriptor lock mode.
  420. *
  421. * param base XRDC2 peripheral base address.
  422. * param mem Which memory descriptor to set.
  423. * param lockMode The lock mode to set.
  424. */
  425. void XRDC2_SetMemAccessLockMode(XRDC2_Type *base, xrdc2_mem_t mem, xrdc2_access_config_lock_t lockMode)
  426. {
  427. uint32_t mrc = XRDC2_GET_MRC((uint32_t)mem);
  428. uint32_t mrgd = XRDC2_GET_MRGD((uint32_t)mem);
  429. uint32_t reg = base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W6 & ~(XRDC2_EAL_MASK | XRDC2_MRC_MRGD_W6_DL2_MASK);
  430. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W6 = (reg | XRDC2_MRC_MRGD_W6_DL2(lockMode));
  431. }
  432. /*!
  433. * brief Sets the memory region access policy for specific domain.
  434. *
  435. * param base XRDC2 peripheral base address.
  436. * param mem The memory region to operate.
  437. * param domainId The ID of the domain whose policy will be changed.
  438. * param policy The access policy to set.
  439. */
  440. void XRDC2_SetMemDomainAccessPolicy(XRDC2_Type *base, xrdc2_mem_t mem, uint8_t domainId, xrdc2_access_policy_t policy)
  441. {
  442. uint32_t reg;
  443. uint32_t mrc = XRDC2_GET_MRC((uint32_t)mem);
  444. uint32_t mrgd = XRDC2_GET_MRGD((uint32_t)mem);
  445. if (domainId < 8U)
  446. {
  447. reg = base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W5 & ~XRDC2_DXACP_0_7_MASK(domainId);
  448. reg |= XRDC2_DXACP_0_7(domainId, policy);
  449. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W5 = reg;
  450. }
  451. else
  452. {
  453. reg = base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W6 & ~XRDC2_DXACP_8_15_MASK(domainId);
  454. reg |= XRDC2_DXACP_8_15(domainId, policy);
  455. reg &= ~XRDC2_EAL_MASK;
  456. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W6 = reg;
  457. }
  458. }
  459. /*!
  460. * brief Disable the memory region exclusive access lock.
  461. *
  462. * Once disabled, it could not be enabled until reset.
  463. *
  464. * param base XRDC2 peripheral base address.
  465. * param mem The memory region to operate.
  466. */
  467. void XRDC2_EnableMemExclAccessLock(XRDC2_Type *base, xrdc2_mem_t mem, bool enable)
  468. {
  469. uint32_t mrc = XRDC2_GET_MRC((uint32_t)mem);
  470. uint32_t mrgd = XRDC2_GET_MRGD((uint32_t)mem);
  471. if (enable)
  472. {
  473. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W6 = XRDC2_EAL_UNLOCKED;
  474. }
  475. else
  476. {
  477. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W6 = XRDC2_EAL_DISABLE_UNTIL_RESET;
  478. }
  479. }
  480. /*!
  481. * brief Get current memory region exclusive access lock owner.
  482. *
  483. * param base XRDC2 peripheral base address.
  484. * param mem The memory region to operate.
  485. * param The domain ID of the lock owner.
  486. */
  487. uint8_t XRDC2_GetMemExclAccessLockDomainOwner(XRDC2_Type *base, xrdc2_mem_t mem)
  488. {
  489. uint32_t mrc = XRDC2_GET_MRC((uint32_t)mem);
  490. uint32_t mrgd = XRDC2_GET_MRGD((uint32_t)mem);
  491. return (uint8_t)((base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W5 & XRDC2_MRC_MRGD_W5_EALO_MASK) >>
  492. XRDC2_MRC_MRGD_W5_EALO_SHIFT);
  493. }
  494. /*!
  495. * brief Try to lock the memory region exclusive access.
  496. *
  497. * param base XRDC2 peripheral base address.
  498. * param mem The memory region to operate.
  499. * retval kStatus_Fail Failed to lock.
  500. * retval kStatus_Success Locked succussfully.
  501. */
  502. status_t XRDC2_TryLockMemExclAccess(XRDC2_Type *base, xrdc2_mem_t mem)
  503. {
  504. status_t status;
  505. uint8_t curDomainID;
  506. uint32_t mrc = XRDC2_GET_MRC((uint32_t)mem);
  507. uint32_t mrgd = XRDC2_GET_MRGD((uint32_t)mem);
  508. volatile uint32_t *lockReg = &(base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W6);
  509. curDomainID = XRDC2_GetCurrentMasterDomainId(base);
  510. *lockReg = XRDC2_EAL_LOCKED;
  511. if (curDomainID != XRDC2_GetMemExclAccessLockDomainOwner(base, mem))
  512. {
  513. status = kStatus_Fail;
  514. }
  515. else
  516. {
  517. status = kStatus_Success;
  518. }
  519. return status;
  520. }
  521. /*!
  522. * brief Lock the memory region exclusive access using blocking method.
  523. *
  524. * param base XRDC2 peripheral base address.
  525. * param mem The memory region to operate.
  526. *
  527. * note This function must be called when the lock is not disabled.
  528. */
  529. void XRDC2_LockMemExclAccess(XRDC2_Type *base, xrdc2_mem_t mem)
  530. {
  531. uint8_t curDomainID;
  532. uint32_t mrc = XRDC2_GET_MRC((uint32_t)mem);
  533. uint32_t mrgd = XRDC2_GET_MRGD((uint32_t)mem);
  534. volatile uint32_t *lockReg = &(base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W6);
  535. curDomainID = XRDC2_GetCurrentMasterDomainId(base);
  536. while (true)
  537. {
  538. *lockReg = XRDC2_EAL_LOCKED;
  539. /* Locked and owner is current domain. */
  540. if (curDomainID == XRDC2_GetMemExclAccessLockDomainOwner(base, mem))
  541. {
  542. break;
  543. }
  544. }
  545. return;
  546. }
  547. /*!
  548. * brief Unlock the memory region exclusive access.
  549. *
  550. * param base XRDC2 peripheral base address.
  551. * param mem The memory region to operate.
  552. *
  553. * note This function must be called by the lock owner.
  554. */
  555. void XRDC2_UnlockMemExclAccess(XRDC2_Type *base, xrdc2_mem_t mem)
  556. {
  557. uint32_t mrc = XRDC2_GET_MRC((uint32_t)mem);
  558. uint32_t mrgd = XRDC2_GET_MRGD((uint32_t)mem);
  559. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W6 = XRDC2_EAL_UNLOCKED;
  560. }
  561. /*!
  562. * brief Force the memory region exclusive access lock release.
  563. *
  564. * The master does not own the lock could call this function to force release the lock.
  565. *
  566. * param base XRDC2 peripheral base address.
  567. * param mem The memory region to operate.
  568. */
  569. void XRDC2_ForceMemExclAccessLockRelease(XRDC2_Type *base, xrdc2_mem_t mem)
  570. {
  571. uint32_t mrc = XRDC2_GET_MRC((uint32_t)mem);
  572. uint32_t mrgd = XRDC2_GET_MRGD((uint32_t)mem);
  573. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W6 = XRDC2_EAL_FORCE_RELEASE_MAGIC_0;
  574. base->MRCI_MRGDJ[mrc][mrgd].MRC_MRGD_W6 = XRDC2_EAL_FORCE_RELEASE_MAGIC_1;
  575. }
  576. /*!
  577. * brief Gets the default peripheral access configuration.
  578. *
  579. * The default configuration is set as follows:
  580. * code
  581. * config->lockMode = kXRDC2_AccessConfigLockWritable;
  582. * config->policy[0] = kXRDC2_AccessPolicyNone;
  583. * config->policy[1] = kXRDC2_AccessPolicyNone;
  584. * ...
  585. * config->policy[15] = kXRDC2_AccessPolicyNone;
  586. * endcode
  587. *
  588. * param config Pointer to the configuration structure.
  589. */
  590. void XRDC2_GetPeriphAccessDefaultConfig(xrdc2_periph_access_config_t *config)
  591. {
  592. assert(NULL != config);
  593. uint8_t domain;
  594. config->lockMode = kXRDC2_AccessConfigLockDisabled;
  595. for (domain = 0; domain < (uint32_t)FSL_FEATURE_XRDC2_DOMAIN_COUNT; domain++)
  596. {
  597. config->policy[domain] = kXRDC2_AccessPolicyNone;
  598. }
  599. }
  600. /*!
  601. * brief Sets the peripheral access policy.
  602. *
  603. * param base XRDC2 peripheral base address.
  604. * param config Pointer to the access policy configuration structure.
  605. */
  606. void XRDC2_SetPeriphAccessConfig(XRDC2_Type *base, xrdc2_periph_t periph, const xrdc2_periph_access_config_t *config)
  607. {
  608. assert(NULL != config);
  609. uint32_t w0 = 0;
  610. uint32_t w1 = 0;
  611. uint32_t pac = XRDC2_GET_PAC((uint32_t)periph);
  612. uint32_t pdac = XRDC2_GET_PDAC((uint32_t)periph);
  613. XRDC2_MakeDXACP(config->policy, &w0, &w1);
  614. w1 |= XRDC2_PAC_PDAC_W1_DL2(config->lockMode);
  615. w1 |= XRDC2_PAC_PDAC_W1_VLD_MASK;
  616. base->PACI_PDACJ[pac][pdac].PAC_PDAC_W0 = w0;
  617. base->PACI_PDACJ[pac][pdac].PAC_PDAC_W1 = w1;
  618. }
  619. /*!
  620. * brief Sets the peripheral descriptor as valid or invalid.
  621. *
  622. * param base XRDC2 peripheral base address.
  623. * param periph Which peripheral descriptor to set.
  624. * param valid True to set valid, false to set invalid.
  625. */
  626. void XRDC2_SetPeriphAccessValid(XRDC2_Type *base, xrdc2_periph_t periph, bool valid)
  627. {
  628. uint32_t pac = XRDC2_GET_PAC((uint32_t)periph);
  629. uint32_t pdac = XRDC2_GET_PDAC((uint32_t)periph);
  630. uint32_t reg = base->PACI_PDACJ[pac][pdac].PAC_PDAC_W1 & ~XRDC2_EAL_MASK;
  631. if (valid)
  632. {
  633. base->PACI_PDACJ[pac][pdac].PAC_PDAC_W1 = (reg | XRDC2_PAC_PDAC_W1_VLD_MASK);
  634. }
  635. else
  636. {
  637. base->PACI_PDACJ[pac][pdac].PAC_PDAC_W1 = (reg & ~XRDC2_PAC_PDAC_W1_VLD_MASK);
  638. }
  639. }
  640. /*!
  641. * brief Sets the peripheral descriptor lock mode.
  642. *
  643. * param base XRDC2 peripheral base address.
  644. * param periph Which peripheral descriptor to set.
  645. * param lockMode The lock mode to set.
  646. */
  647. void XRDC2_SetPeriphAccessLockMode(XRDC2_Type *base, xrdc2_periph_t periph, xrdc2_access_config_lock_t lockMode)
  648. {
  649. uint32_t pac = XRDC2_GET_PAC((uint32_t)periph);
  650. uint32_t pdac = XRDC2_GET_PDAC((uint32_t)periph);
  651. uint32_t reg = base->PACI_PDACJ[pac][pdac].PAC_PDAC_W1 & ~(XRDC2_EAL_MASK | XRDC2_PAC_PDAC_W1_DL2_MASK);
  652. base->PACI_PDACJ[pac][pdac].PAC_PDAC_W1 = (reg | XRDC2_PAC_PDAC_W1_DL2(lockMode));
  653. }
  654. /*!
  655. * brief Sets the peripheral access policy for specific domain.
  656. *
  657. * param base XRDC2 peripheral base address.
  658. * param periph The peripheral to operate.
  659. * param domainId The ID of the domain whose policy will be changed.
  660. * param policy The access policy to set.
  661. */
  662. void XRDC2_SetPeriphDomainAccessPolicy(XRDC2_Type *base,
  663. xrdc2_periph_t periph,
  664. uint8_t domainId,
  665. xrdc2_access_policy_t policy)
  666. {
  667. uint32_t reg;
  668. uint32_t pac = XRDC2_GET_PAC((uint32_t)periph);
  669. uint32_t pdac = XRDC2_GET_PDAC((uint32_t)periph);
  670. if (domainId < 8U)
  671. {
  672. reg = base->PACI_PDACJ[pac][pdac].PAC_PDAC_W0 & ~XRDC2_DXACP_0_7_MASK(domainId);
  673. reg |= XRDC2_DXACP_0_7(domainId, policy);
  674. base->PACI_PDACJ[pac][pdac].PAC_PDAC_W0 = reg;
  675. }
  676. else
  677. {
  678. reg = base->PACI_PDACJ[pac][pdac].PAC_PDAC_W1 & ~XRDC2_DXACP_8_15_MASK(domainId);
  679. reg |= XRDC2_DXACP_8_15(domainId, policy);
  680. reg &= ~XRDC2_EAL_MASK;
  681. base->PACI_PDACJ[pac][pdac].PAC_PDAC_W1 = reg;
  682. }
  683. }
  684. /*!
  685. * brief Get current peripheral exclusive access lock owner.
  686. *
  687. * param base XRDC2 peripheral base address.
  688. * param periph The peripheral to operate.
  689. * param The domain ID of the lock owner.
  690. */
  691. uint8_t XRDC2_GetPeriphExclAccessLockDomainOwner(XRDC2_Type *base, xrdc2_periph_t periph)
  692. {
  693. uint32_t pac = XRDC2_GET_PAC((uint32_t)periph);
  694. uint32_t pdac = XRDC2_GET_PDAC((uint32_t)periph);
  695. return (uint8_t)((base->PACI_PDACJ[pac][pdac].PAC_PDAC_W0 & XRDC2_PAC_PDAC_W0_EALO_MASK) >>
  696. XRDC2_PAC_PDAC_W0_EALO_SHIFT);
  697. }
  698. /*!
  699. * brief Disable the peripheral exclusive access lock.
  700. *
  701. * Once disabled, it could not be enabled until reset.
  702. *
  703. * param base XRDC2 peripheral base address.
  704. * param periph The peripheral to operate.
  705. * param enable True to enable, false to disable.
  706. */
  707. void XRDC2_EnablePeriphExclAccessLock(XRDC2_Type *base, xrdc2_periph_t periph, bool enable)
  708. {
  709. uint32_t pac = XRDC2_GET_PAC((uint32_t)periph);
  710. uint32_t pdac = XRDC2_GET_PDAC((uint32_t)periph);
  711. if (enable)
  712. {
  713. base->PACI_PDACJ[pac][pdac].PAC_PDAC_W1 = XRDC2_EAL_UNLOCKED;
  714. }
  715. else
  716. {
  717. base->PACI_PDACJ[pac][pdac].PAC_PDAC_W1 = XRDC2_EAL_DISABLE_UNTIL_RESET;
  718. }
  719. }
  720. /*!
  721. * brief Try to lock the peripheral exclusive access.
  722. *
  723. * param base XRDC2 peripheral base address.
  724. * param periph The peripheral to operate.
  725. * retval kStatus_Fail Failed to lock.
  726. * retval kStatus_Success Locked succussfully.
  727. */
  728. status_t XRDC2_TryLockPeriphExclAccess(XRDC2_Type *base, xrdc2_periph_t periph)
  729. {
  730. status_t status;
  731. uint8_t curDomainID;
  732. uint32_t pac = XRDC2_GET_PAC((uint32_t)periph);
  733. uint32_t pdac = XRDC2_GET_PDAC((uint32_t)periph);
  734. volatile uint32_t *lockReg = &(base->PACI_PDACJ[pac][pdac].PAC_PDAC_W1);
  735. curDomainID = XRDC2_GetCurrentMasterDomainId(base);
  736. *lockReg = XRDC2_EAL_LOCKED;
  737. if (curDomainID != XRDC2_GetPeriphExclAccessLockDomainOwner(base, periph))
  738. {
  739. status = kStatus_Fail;
  740. }
  741. else
  742. {
  743. status = kStatus_Success;
  744. }
  745. return status;
  746. }
  747. /*!
  748. * brief Lock the peripheral exclusive access using blocking method.
  749. *
  750. * param base XRDC2 peripheral base address.
  751. * param periph The peripheral to operate.
  752. *
  753. * note This function must be called when the lock is not disabled.
  754. */
  755. void XRDC2_LockPeriphExclAccess(XRDC2_Type *base, xrdc2_periph_t periph)
  756. {
  757. uint8_t curDomainID;
  758. uint32_t pac = XRDC2_GET_PAC((uint32_t)periph);
  759. uint32_t pdac = XRDC2_GET_PDAC((uint32_t)periph);
  760. volatile uint32_t *lockReg = &(base->PACI_PDACJ[pac][pdac].PAC_PDAC_W1);
  761. curDomainID = XRDC2_GetCurrentMasterDomainId(base);
  762. while (true)
  763. {
  764. *lockReg = XRDC2_EAL_LOCKED;
  765. /* Locked and owner is current domain. */
  766. if (curDomainID == XRDC2_GetPeriphExclAccessLockDomainOwner(base, periph))
  767. {
  768. break;
  769. }
  770. }
  771. return;
  772. }
  773. /*!
  774. * brief Unlock the peripheral exclusive access.
  775. *
  776. * param base XRDC2 peripheral base address.
  777. * param periph The peripheral to operate.
  778. *
  779. * note This function must be called by the lock owner.
  780. */
  781. void XRDC2_UnlockPeriphExclAccess(XRDC2_Type *base, xrdc2_periph_t periph)
  782. {
  783. uint32_t pac = XRDC2_GET_PAC((uint32_t)periph);
  784. uint32_t pdac = XRDC2_GET_PDAC((uint32_t)periph);
  785. base->PACI_PDACJ[pac][pdac].PAC_PDAC_W1 = XRDC2_EAL_UNLOCKED;
  786. }
  787. /*!
  788. * brief Force the peripheral exclusive access lock release.
  789. *
  790. * The master does not own the lock could call this function to force release the lock.
  791. *
  792. * param base XRDC2 peripheral base address.
  793. * param periph The peripheral to operate.
  794. */
  795. void XRDC2_ForcePeriphExclAccessLockRelease(XRDC2_Type *base, xrdc2_periph_t periph)
  796. {
  797. uint32_t pac = XRDC2_GET_PAC((uint32_t)periph);
  798. uint32_t pdac = XRDC2_GET_PDAC((uint32_t)periph);
  799. base->PACI_PDACJ[pac][pdac].PAC_PDAC_W1 = XRDC2_EAL_FORCE_RELEASE_MAGIC_0;
  800. base->PACI_PDACJ[pac][pdac].PAC_PDAC_W1 = XRDC2_EAL_FORCE_RELEASE_MAGIC_1;
  801. }