fsl_flexcomm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_common.h"
  9. #include "fsl_flexcomm.h"
  10. /*******************************************************************************
  11. * Definitions
  12. ******************************************************************************/
  13. /* Component ID definition, used by tools. */
  14. #ifndef FSL_COMPONENT_ID
  15. #define FSL_COMPONENT_ID "platform.drivers.flexcomm"
  16. #endif
  17. /*******************************************************************************
  18. * Prototypes
  19. ******************************************************************************/
  20. /*! @brief Set the FLEXCOMM mode . */
  21. static status_t FLEXCOMM_SetPeriph(FLEXCOMM_Type *base, FLEXCOMM_PERIPH_T periph, int lock);
  22. /*! @brief check whether flexcomm supports peripheral type */
  23. static bool FLEXCOMM_PeripheralIsPresent(FLEXCOMM_Type *base, FLEXCOMM_PERIPH_T periph);
  24. /*******************************************************************************
  25. * Variables
  26. ******************************************************************************/
  27. /*! @brief Pointers to real IRQ handlers installed by drivers for each instance. */
  28. static flexcomm_irq_handler_t s_flexcommIrqHandler[FSL_FEATURE_SOC_FLEXCOMM_COUNT];
  29. /*! @brief Pointers to handles for each instance to provide context to interrupt routines */
  30. static void *s_flexcommHandle[FSL_FEATURE_SOC_FLEXCOMM_COUNT];
  31. /*! @brief Array to map FLEXCOMM instance number to IRQ number. */
  32. IRQn_Type const kFlexcommIrqs[] = FLEXCOMM_IRQS;
  33. /*! @brief Array to map FLEXCOMM instance number to base address. */
  34. static const uint32_t s_flexcommBaseAddrs[FSL_FEATURE_SOC_FLEXCOMM_COUNT] = FLEXCOMM_BASE_ADDRS;
  35. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  36. /*! @brief IDs of clock for each FLEXCOMM module */
  37. static const clock_ip_name_t s_flexcommClocks[] = FLEXCOMM_CLOCKS;
  38. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  39. #if !(defined(FSL_FEATURE_FLEXCOMM_HAS_NO_RESET) && FSL_FEATURE_FLEXCOMM_HAS_NO_RESET)
  40. /*! @brief Pointers to FLEXCOMM resets for each instance. */
  41. static const reset_ip_name_t s_flexcommResets[] = FLEXCOMM_RSTS;
  42. #endif
  43. /*******************************************************************************
  44. * Code
  45. ******************************************************************************/
  46. /* check whether flexcomm supports peripheral type */
  47. static bool FLEXCOMM_PeripheralIsPresent(FLEXCOMM_Type *base, FLEXCOMM_PERIPH_T periph)
  48. {
  49. if (periph == FLEXCOMM_PERIPH_NONE)
  50. {
  51. return true;
  52. }
  53. else if (periph <= FLEXCOMM_PERIPH_I2S_TX)
  54. {
  55. return (base->PSELID & (uint32_t)(1 << ((uint32_t)periph + 3))) > (uint32_t)0 ? true : false;
  56. }
  57. else if (periph == FLEXCOMM_PERIPH_I2S_RX)
  58. {
  59. return (base->PSELID & (1 << 7)) > (uint32_t)0 ? true : false;
  60. }
  61. else
  62. {
  63. return false;
  64. }
  65. }
  66. /* Get the index corresponding to the FLEXCOMM */
  67. /*! brief Returns instance number for FLEXCOMM module with given base address. */
  68. uint32_t FLEXCOMM_GetInstance(void *base)
  69. {
  70. int i;
  71. for (i = 0; i < FSL_FEATURE_SOC_FLEXCOMM_COUNT; i++)
  72. {
  73. if ((uint32_t)base == s_flexcommBaseAddrs[i])
  74. {
  75. return i;
  76. }
  77. }
  78. assert(false);
  79. return 0;
  80. }
  81. /* Changes FLEXCOMM mode */
  82. static status_t FLEXCOMM_SetPeriph(FLEXCOMM_Type *base, FLEXCOMM_PERIPH_T periph, int lock)
  83. {
  84. /* Check whether peripheral type is present */
  85. if (!FLEXCOMM_PeripheralIsPresent(base, periph))
  86. {
  87. return kStatus_OutOfRange;
  88. }
  89. /* Flexcomm is locked to different peripheral type than expected */
  90. if ((base->PSELID & FLEXCOMM_PSELID_LOCK_MASK) && ((base->PSELID & FLEXCOMM_PSELID_PERSEL_MASK) != periph))
  91. {
  92. return kStatus_Fail;
  93. }
  94. /* Check if we are asked to lock */
  95. if (lock)
  96. {
  97. base->PSELID = (uint32_t)periph | FLEXCOMM_PSELID_LOCK_MASK;
  98. }
  99. else
  100. {
  101. base->PSELID = (uint32_t)periph;
  102. }
  103. return kStatus_Success;
  104. }
  105. /*! brief Initializes FLEXCOMM and selects peripheral mode according to the second parameter. */
  106. status_t FLEXCOMM_Init(void *base, FLEXCOMM_PERIPH_T periph)
  107. {
  108. int idx = FLEXCOMM_GetInstance(base);
  109. if (idx < 0)
  110. {
  111. return kStatus_InvalidArgument;
  112. }
  113. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  114. /* Enable the peripheral clock */
  115. CLOCK_EnableClock(s_flexcommClocks[idx]);
  116. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  117. #if !(defined(FSL_FEATURE_FLEXCOMM_HAS_NO_RESET) && FSL_FEATURE_FLEXCOMM_HAS_NO_RESET)
  118. /* Reset the FLEXCOMM module */
  119. RESET_PeripheralReset(s_flexcommResets[idx]);
  120. #endif
  121. /* Set the FLEXCOMM to given peripheral */
  122. return FLEXCOMM_SetPeriph((FLEXCOMM_Type *)base, periph, 0);
  123. }
  124. /*! brief Sets IRQ handler for given FLEXCOMM module. It is used by drivers register IRQ handler according to FLEXCOMM
  125. * mode */
  126. void FLEXCOMM_SetIRQHandler(void *base, flexcomm_irq_handler_t handler, void *handle)
  127. {
  128. uint32_t instance;
  129. /* Look up instance number */
  130. instance = FLEXCOMM_GetInstance(base);
  131. /* Clear handler first to avoid execution of the handler with wrong handle */
  132. s_flexcommIrqHandler[instance] = NULL;
  133. s_flexcommHandle[instance] = handle;
  134. s_flexcommIrqHandler[instance] = handler;
  135. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  136. exception return operation might vector to incorrect interrupt */
  137. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  138. __DSB();
  139. #endif
  140. }
  141. /* IRQ handler functions overloading weak symbols in the startup */
  142. #if defined(FLEXCOMM0)
  143. void FLEXCOMM0_DriverIRQHandler(void)
  144. {
  145. assert(s_flexcommIrqHandler[0]);
  146. s_flexcommIrqHandler[0]((void *)s_flexcommBaseAddrs[0], s_flexcommHandle[0]);
  147. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  148. exception return operation might vector to incorrect interrupt */
  149. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  150. __DSB();
  151. #endif
  152. }
  153. #endif
  154. #if defined(FLEXCOMM1)
  155. void FLEXCOMM1_DriverIRQHandler(void)
  156. {
  157. assert(s_flexcommIrqHandler[1]);
  158. s_flexcommIrqHandler[1]((void *)s_flexcommBaseAddrs[1], s_flexcommHandle[1]);
  159. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  160. exception return operation might vector to incorrect interrupt */
  161. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  162. __DSB();
  163. #endif
  164. }
  165. #endif
  166. #if defined(FLEXCOMM2)
  167. void FLEXCOMM2_DriverIRQHandler(void)
  168. {
  169. assert(s_flexcommIrqHandler[2]);
  170. s_flexcommIrqHandler[2]((void *)s_flexcommBaseAddrs[2], s_flexcommHandle[2]);
  171. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  172. exception return operation might vector to incorrect interrupt */
  173. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  174. __DSB();
  175. #endif
  176. }
  177. #endif
  178. #if defined(FLEXCOMM3)
  179. void FLEXCOMM3_DriverIRQHandler(void)
  180. {
  181. assert(s_flexcommIrqHandler[3]);
  182. s_flexcommIrqHandler[3]((void *)s_flexcommBaseAddrs[3], s_flexcommHandle[3]);
  183. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  184. exception return operation might vector to incorrect interrupt */
  185. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  186. __DSB();
  187. #endif
  188. }
  189. #endif
  190. #if defined(FLEXCOMM4)
  191. void FLEXCOMM4_DriverIRQHandler(void)
  192. {
  193. assert(s_flexcommIrqHandler[4]);
  194. s_flexcommIrqHandler[4]((void *)s_flexcommBaseAddrs[4], s_flexcommHandle[4]);
  195. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  196. exception return operation might vector to incorrect interrupt */
  197. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  198. __DSB();
  199. #endif
  200. }
  201. #endif
  202. #if defined(FLEXCOMM5)
  203. void FLEXCOMM5_DriverIRQHandler(void)
  204. {
  205. assert(s_flexcommIrqHandler[5]);
  206. s_flexcommIrqHandler[5]((void *)s_flexcommBaseAddrs[5], s_flexcommHandle[5]);
  207. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  208. exception return operation might vector to incorrect interrupt */
  209. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  210. __DSB();
  211. #endif
  212. }
  213. #endif
  214. #if defined(FLEXCOMM6)
  215. void FLEXCOMM6_DriverIRQHandler(void)
  216. {
  217. assert(s_flexcommIrqHandler[6]);
  218. s_flexcommIrqHandler[6]((void *)s_flexcommBaseAddrs[6], s_flexcommHandle[6]);
  219. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  220. exception return operation might vector to incorrect interrupt */
  221. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  222. __DSB();
  223. #endif
  224. }
  225. #endif
  226. #if defined(FLEXCOMM7)
  227. void FLEXCOMM7_DriverIRQHandler(void)
  228. {
  229. assert(s_flexcommIrqHandler[7]);
  230. s_flexcommIrqHandler[7]((void *)s_flexcommBaseAddrs[7], s_flexcommHandle[7]);
  231. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  232. exception return operation might vector to incorrect interrupt */
  233. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  234. __DSB();
  235. #endif
  236. }
  237. #endif
  238. #if defined(FLEXCOMM8)
  239. void FLEXCOMM8_DriverIRQHandler(void)
  240. {
  241. assert(s_flexcommIrqHandler[8]);
  242. s_flexcommIrqHandler[8]((void *)s_flexcommBaseAddrs[8], s_flexcommHandle[8]);
  243. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  244. exception return operation might vector to incorrect interrupt */
  245. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  246. __DSB();
  247. #endif
  248. }
  249. #endif
  250. #if defined(FLEXCOMM9)
  251. void FLEXCOMM9_DriverIRQHandler(void)
  252. {
  253. assert(s_flexcommIrqHandler[9]);
  254. s_flexcommIrqHandler[9]((void *)s_flexcommBaseAddrs[9], s_flexcommHandle[9]);
  255. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  256. exception return operation might vector to incorrect interrupt */
  257. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  258. __DSB();
  259. #endif
  260. }
  261. #endif
  262. #if defined(FLEXCOMM14)
  263. void FLEXCOMM14_DriverIRQHandler(void)
  264. {
  265. uint32_t instance;
  266. /* Look up instance number */
  267. instance = FLEXCOMM_GetInstance(FLEXCOMM14);
  268. assert(s_flexcommIrqHandler[instance]);
  269. s_flexcommIrqHandler[instance]((void *)s_flexcommBaseAddrs[instance], s_flexcommHandle[instance]);
  270. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  271. exception return operation might vector to incorrect interrupt */
  272. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  273. __DSB();
  274. #endif
  275. }
  276. #endif
  277. #if defined(FLEXCOMM15)
  278. void FLEXCOMM15_DriverIRQHandler(void)
  279. {
  280. uint32_t instance;
  281. /* Look up instance number */
  282. instance = FLEXCOMM_GetInstance(FLEXCOMM14);
  283. assert(s_flexcommIrqHandler[instance]);
  284. s_flexcommIrqHandler[instance]((void *)s_flexcommBaseAddrs[instance], s_flexcommHandle[instance]);
  285. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  286. exception return operation might vector to incorrect interrupt */
  287. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  288. __DSB();
  289. #endif
  290. }
  291. #endif