fsl_flexbus.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_flexbus.h"
  35. /*******************************************************************************
  36. * Prototypes
  37. ******************************************************************************/
  38. /*!
  39. * @brief Gets the instance from the base address
  40. *
  41. * @param base FLEXBUS peripheral base address
  42. *
  43. * @return The FLEXBUS instance
  44. */
  45. static uint32_t FLEXBUS_GetInstance(FB_Type *base);
  46. /*******************************************************************************
  47. * Variables
  48. ******************************************************************************/
  49. /*! @brief Pointers to FLEXBUS bases for each instance. */
  50. static FB_Type *const s_flexbusBases[] = FB_BASE_PTRS;
  51. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  52. /*! @brief Pointers to FLEXBUS clocks for each instance. */
  53. static const clock_ip_name_t s_flexbusClocks[] = FLEXBUS_CLOCKS;
  54. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  55. /*******************************************************************************
  56. * Code
  57. ******************************************************************************/
  58. static uint32_t FLEXBUS_GetInstance(FB_Type *base)
  59. {
  60. uint32_t instance;
  61. /* Find the instance index from base address mappings. */
  62. for (instance = 0; instance < ARRAY_SIZE(s_flexbusBases); instance++)
  63. {
  64. if (s_flexbusBases[instance] == base)
  65. {
  66. break;
  67. }
  68. }
  69. assert(instance < ARRAY_SIZE(s_flexbusBases));
  70. return instance;
  71. }
  72. void FLEXBUS_Init(FB_Type *base, const flexbus_config_t *config)
  73. {
  74. assert(config != NULL);
  75. assert(config->chip < FB_CSAR_COUNT);
  76. assert(config->waitStates <= 0x3FU);
  77. uint32_t chip = 0;
  78. uint32_t reg_value = 0;
  79. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  80. /* Ungate clock for FLEXBUS */
  81. CLOCK_EnableClock(s_flexbusClocks[FLEXBUS_GetInstance(base)]);
  82. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  83. /* Reset all the register to default state */
  84. for (chip = 0; chip < FB_CSAR_COUNT; chip++)
  85. {
  86. /* Reset CSMR register, all chips not valid (disabled) */
  87. base->CS[chip].CSMR = 0x0000U;
  88. /* Set default base address */
  89. base->CS[chip].CSAR &= (~FB_CSAR_BA_MASK);
  90. /* Reset FB_CSCRx register */
  91. base->CS[chip].CSCR = 0x0000U;
  92. }
  93. /* Set FB_CSPMCR register */
  94. /* FlexBus signal group 1 multiplex control */
  95. reg_value |= kFLEXBUS_MultiplexGroup1_FB_ALE << FB_CSPMCR_GROUP1_SHIFT;
  96. /* FlexBus signal group 2 multiplex control */
  97. reg_value |= kFLEXBUS_MultiplexGroup2_FB_CS4 << FB_CSPMCR_GROUP2_SHIFT;
  98. /* FlexBus signal group 3 multiplex control */
  99. reg_value |= kFLEXBUS_MultiplexGroup3_FB_CS5 << FB_CSPMCR_GROUP3_SHIFT;
  100. /* FlexBus signal group 4 multiplex control */
  101. reg_value |= kFLEXBUS_MultiplexGroup4_FB_TBST << FB_CSPMCR_GROUP4_SHIFT;
  102. /* FlexBus signal group 5 multiplex control */
  103. reg_value |= kFLEXBUS_MultiplexGroup5_FB_TA << FB_CSPMCR_GROUP5_SHIFT;
  104. /* Write to CSPMCR register */
  105. base->CSPMCR = reg_value;
  106. /* Update chip value */
  107. chip = config->chip;
  108. /* Base address */
  109. reg_value = config->chipBaseAddress;
  110. /* Write to CSAR register */
  111. base->CS[chip].CSAR = reg_value;
  112. /* Chip-select validation */
  113. reg_value = 0x1U << FB_CSMR_V_SHIFT;
  114. /* Write protect */
  115. reg_value |= (uint32_t)(config->writeProtect) << FB_CSMR_WP_SHIFT;
  116. /* Base address mask */
  117. reg_value |= config->chipBaseAddressMask << FB_CSMR_BAM_SHIFT;
  118. /* Write to CSMR register */
  119. base->CS[chip].CSMR = reg_value;
  120. /* Burst write */
  121. reg_value = (uint32_t)(config->burstWrite) << FB_CSCR_BSTW_SHIFT;
  122. /* Burst read */
  123. reg_value |= (uint32_t)(config->burstRead) << FB_CSCR_BSTR_SHIFT;
  124. /* Byte-enable mode */
  125. reg_value |= (uint32_t)(config->byteEnableMode) << FB_CSCR_BEM_SHIFT;
  126. /* Port size */
  127. reg_value |= (uint32_t)config->portSize << FB_CSCR_PS_SHIFT;
  128. /* The internal transfer acknowledge for accesses */
  129. reg_value |= (uint32_t)(config->autoAcknowledge) << FB_CSCR_AA_SHIFT;
  130. /* Byte-Lane shift */
  131. reg_value |= (uint32_t)config->byteLaneShift << FB_CSCR_BLS_SHIFT;
  132. /* The number of wait states */
  133. reg_value |= (uint32_t)config->waitStates << FB_CSCR_WS_SHIFT;
  134. /* Write address hold or deselect */
  135. reg_value |= (uint32_t)config->writeAddressHold << FB_CSCR_WRAH_SHIFT;
  136. /* Read address hold or deselect */
  137. reg_value |= (uint32_t)config->readAddressHold << FB_CSCR_RDAH_SHIFT;
  138. /* Address setup */
  139. reg_value |= (uint32_t)config->addressSetup << FB_CSCR_ASET_SHIFT;
  140. /* Extended transfer start/extended address latch */
  141. reg_value |= (uint32_t)(config->extendTransferAddress) << FB_CSCR_EXTS_SHIFT;
  142. /* Secondary wait state */
  143. reg_value |= (uint32_t)(config->secondaryWaitStates) << FB_CSCR_SWSEN_SHIFT;
  144. /* Write to CSCR register */
  145. base->CS[chip].CSCR = reg_value;
  146. /* FlexBus signal group 1 multiplex control */
  147. reg_value = (uint32_t)config->group1MultiplexControl << FB_CSPMCR_GROUP1_SHIFT;
  148. /* FlexBus signal group 2 multiplex control */
  149. reg_value |= (uint32_t)config->group2MultiplexControl << FB_CSPMCR_GROUP2_SHIFT;
  150. /* FlexBus signal group 3 multiplex control */
  151. reg_value |= (uint32_t)config->group3MultiplexControl << FB_CSPMCR_GROUP3_SHIFT;
  152. /* FlexBus signal group 4 multiplex control */
  153. reg_value |= (uint32_t)config->group4MultiplexControl << FB_CSPMCR_GROUP4_SHIFT;
  154. /* FlexBus signal group 5 multiplex control */
  155. reg_value |= (uint32_t)config->group5MultiplexControl << FB_CSPMCR_GROUP5_SHIFT;
  156. /* Write to CSPMCR register */
  157. base->CSPMCR = reg_value;
  158. /* Enable CSPMCR0[V] to make all chip select registers take effect. */
  159. if ( chip )
  160. {
  161. base->CS[0].CSMR = FB_CSMR_V_MASK;
  162. }
  163. }
  164. void FLEXBUS_Deinit(FB_Type *base)
  165. {
  166. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  167. /* Gate clock for FLEXBUS */
  168. CLOCK_DisableClock(s_flexbusClocks[FLEXBUS_GetInstance(base)]);
  169. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  170. }
  171. void FLEXBUS_GetDefaultConfig(flexbus_config_t *config)
  172. {
  173. config->chip = 0; /* Chip 0 FlexBus for validation */
  174. config->writeProtect = 0; /* Write accesses are allowed */
  175. config->burstWrite = 0; /* Burst-Write disable */
  176. config->burstRead = 0; /* Burst-Read disable */
  177. config->byteEnableMode = 0; /* Byte-Enable mode is asserted for data write only */
  178. config->autoAcknowledge = true; /* Auto-Acknowledge enable */
  179. config->extendTransferAddress = 0; /* Extend transfer start/extend address latch disable */
  180. config->secondaryWaitStates = 0; /* Secondary wait state disable */
  181. config->byteLaneShift = kFLEXBUS_NotShifted; /* Byte-Lane shift disable */
  182. config->writeAddressHold = kFLEXBUS_Hold1Cycle; /* Write address hold 1 cycles */
  183. config->readAddressHold = kFLEXBUS_Hold1Or0Cycles; /* Read address hold 0 cycles */
  184. config->addressSetup =
  185. kFLEXBUS_FirstRisingEdge; /* Assert ~FB_CSn on the first rising clock edge after the address is asserted */
  186. config->portSize = kFLEXBUS_1Byte; /* 1 byte port size of transfer */
  187. config->group1MultiplexControl = kFLEXBUS_MultiplexGroup1_FB_ALE; /* FB_ALE */
  188. config->group2MultiplexControl = kFLEXBUS_MultiplexGroup2_FB_CS4; /* FB_CS4 */
  189. config->group3MultiplexControl = kFLEXBUS_MultiplexGroup3_FB_CS5; /* FB_CS5 */
  190. config->group4MultiplexControl = kFLEXBUS_MultiplexGroup4_FB_TBST; /* FB_TBST */
  191. config->group5MultiplexControl = kFLEXBUS_MultiplexGroup5_FB_TA; /* FB_TA */
  192. }