fsl_flexbus.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.flexbus"
  38. #endif
  39. /*******************************************************************************
  40. * Prototypes
  41. ******************************************************************************/
  42. /*!
  43. * @brief Gets the instance from the base address
  44. *
  45. * @param base FLEXBUS peripheral base address
  46. *
  47. * @return The FLEXBUS instance
  48. */
  49. static uint32_t FLEXBUS_GetInstance(FB_Type *base);
  50. /*******************************************************************************
  51. * Variables
  52. ******************************************************************************/
  53. /*! @brief Pointers to FLEXBUS bases for each instance. */
  54. static FB_Type *const s_flexbusBases[] = FB_BASE_PTRS;
  55. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  56. /*! @brief Pointers to FLEXBUS clocks for each instance. */
  57. static const clock_ip_name_t s_flexbusClocks[] = FLEXBUS_CLOCKS;
  58. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  59. /*******************************************************************************
  60. * Code
  61. ******************************************************************************/
  62. static uint32_t FLEXBUS_GetInstance(FB_Type *base)
  63. {
  64. uint32_t instance;
  65. /* Find the instance index from base address mappings. */
  66. for (instance = 0; instance < ARRAY_SIZE(s_flexbusBases); instance++)
  67. {
  68. if (s_flexbusBases[instance] == base)
  69. {
  70. break;
  71. }
  72. }
  73. assert(instance < ARRAY_SIZE(s_flexbusBases));
  74. return instance;
  75. }
  76. void FLEXBUS_Init(FB_Type *base, const flexbus_config_t *config)
  77. {
  78. assert(config != NULL);
  79. assert(config->chip < FB_CSAR_COUNT);
  80. assert(config->waitStates <= 0x3FU);
  81. uint32_t chip = 0;
  82. uint32_t reg_value = 0;
  83. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  84. /* Ungate clock for FLEXBUS */
  85. CLOCK_EnableClock(s_flexbusClocks[FLEXBUS_GetInstance(base)]);
  86. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  87. /* Reset all the register to default state */
  88. for (chip = 0; chip < FB_CSAR_COUNT; chip++)
  89. {
  90. /* Reset CSMR register, all chips not valid (disabled) */
  91. base->CS[chip].CSMR = 0x0000U;
  92. /* Set default base address */
  93. base->CS[chip].CSAR &= (~FB_CSAR_BA_MASK);
  94. /* Reset FB_CSCRx register */
  95. base->CS[chip].CSCR = 0x0000U;
  96. }
  97. /* Set FB_CSPMCR register */
  98. /* FlexBus signal group 1 multiplex control */
  99. reg_value |= kFLEXBUS_MultiplexGroup1_FB_ALE << FB_CSPMCR_GROUP1_SHIFT;
  100. /* FlexBus signal group 2 multiplex control */
  101. reg_value |= kFLEXBUS_MultiplexGroup2_FB_CS4 << FB_CSPMCR_GROUP2_SHIFT;
  102. /* FlexBus signal group 3 multiplex control */
  103. reg_value |= kFLEXBUS_MultiplexGroup3_FB_CS5 << FB_CSPMCR_GROUP3_SHIFT;
  104. /* FlexBus signal group 4 multiplex control */
  105. reg_value |= kFLEXBUS_MultiplexGroup4_FB_TBST << FB_CSPMCR_GROUP4_SHIFT;
  106. /* FlexBus signal group 5 multiplex control */
  107. reg_value |= kFLEXBUS_MultiplexGroup5_FB_TA << FB_CSPMCR_GROUP5_SHIFT;
  108. /* Write to CSPMCR register */
  109. base->CSPMCR = reg_value;
  110. /* Update chip value */
  111. chip = config->chip;
  112. /* Base address */
  113. reg_value = config->chipBaseAddress;
  114. /* Write to CSAR register */
  115. base->CS[chip].CSAR = reg_value;
  116. /* Chip-select validation */
  117. reg_value = 0x1U << FB_CSMR_V_SHIFT;
  118. /* Write protect */
  119. reg_value |= (uint32_t)(config->writeProtect) << FB_CSMR_WP_SHIFT;
  120. /* Base address mask */
  121. reg_value |= config->chipBaseAddressMask << FB_CSMR_BAM_SHIFT;
  122. /* Write to CSMR register */
  123. base->CS[chip].CSMR = reg_value;
  124. /* Burst write */
  125. reg_value = (uint32_t)(config->burstWrite) << FB_CSCR_BSTW_SHIFT;
  126. /* Burst read */
  127. reg_value |= (uint32_t)(config->burstRead) << FB_CSCR_BSTR_SHIFT;
  128. /* Byte-enable mode */
  129. reg_value |= (uint32_t)(config->byteEnableMode) << FB_CSCR_BEM_SHIFT;
  130. /* Port size */
  131. reg_value |= (uint32_t)config->portSize << FB_CSCR_PS_SHIFT;
  132. /* The internal transfer acknowledge for accesses */
  133. reg_value |= (uint32_t)(config->autoAcknowledge) << FB_CSCR_AA_SHIFT;
  134. /* Byte-Lane shift */
  135. reg_value |= (uint32_t)config->byteLaneShift << FB_CSCR_BLS_SHIFT;
  136. /* The number of wait states */
  137. reg_value |= (uint32_t)config->waitStates << FB_CSCR_WS_SHIFT;
  138. /* Write address hold or deselect */
  139. reg_value |= (uint32_t)config->writeAddressHold << FB_CSCR_WRAH_SHIFT;
  140. /* Read address hold or deselect */
  141. reg_value |= (uint32_t)config->readAddressHold << FB_CSCR_RDAH_SHIFT;
  142. /* Address setup */
  143. reg_value |= (uint32_t)config->addressSetup << FB_CSCR_ASET_SHIFT;
  144. /* Extended transfer start/extended address latch */
  145. reg_value |= (uint32_t)(config->extendTransferAddress) << FB_CSCR_EXTS_SHIFT;
  146. /* Secondary wait state */
  147. reg_value |= (uint32_t)(config->secondaryWaitStates) << FB_CSCR_SWSEN_SHIFT;
  148. /* Write to CSCR register */
  149. base->CS[chip].CSCR = reg_value;
  150. /* FlexBus signal group 1 multiplex control */
  151. reg_value = (uint32_t)config->group1MultiplexControl << FB_CSPMCR_GROUP1_SHIFT;
  152. /* FlexBus signal group 2 multiplex control */
  153. reg_value |= (uint32_t)config->group2MultiplexControl << FB_CSPMCR_GROUP2_SHIFT;
  154. /* FlexBus signal group 3 multiplex control */
  155. reg_value |= (uint32_t)config->group3MultiplexControl << FB_CSPMCR_GROUP3_SHIFT;
  156. /* FlexBus signal group 4 multiplex control */
  157. reg_value |= (uint32_t)config->group4MultiplexControl << FB_CSPMCR_GROUP4_SHIFT;
  158. /* FlexBus signal group 5 multiplex control */
  159. reg_value |= (uint32_t)config->group5MultiplexControl << FB_CSPMCR_GROUP5_SHIFT;
  160. /* Write to CSPMCR register */
  161. base->CSPMCR = reg_value;
  162. /* Enable CSPMCR0[V] to make all chip select registers take effect. */
  163. if ( chip )
  164. {
  165. base->CS[0].CSMR = FB_CSMR_V_MASK;
  166. }
  167. }
  168. void FLEXBUS_Deinit(FB_Type *base)
  169. {
  170. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  171. /* Gate clock for FLEXBUS */
  172. CLOCK_DisableClock(s_flexbusClocks[FLEXBUS_GetInstance(base)]);
  173. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  174. }
  175. void FLEXBUS_GetDefaultConfig(flexbus_config_t *config)
  176. {
  177. config->chip = 0; /* Chip 0 FlexBus for validation */
  178. config->writeProtect = 0; /* Write accesses are allowed */
  179. config->burstWrite = 0; /* Burst-Write disable */
  180. config->burstRead = 0; /* Burst-Read disable */
  181. config->byteEnableMode = 0; /* Byte-Enable mode is asserted for data write only */
  182. config->autoAcknowledge = true; /* Auto-Acknowledge enable */
  183. config->extendTransferAddress = 0; /* Extend transfer start/extend address latch disable */
  184. config->secondaryWaitStates = 0; /* Secondary wait state disable */
  185. config->byteLaneShift = kFLEXBUS_NotShifted; /* Byte-Lane shift disable */
  186. config->writeAddressHold = kFLEXBUS_Hold1Cycle; /* Write address hold 1 cycles */
  187. config->readAddressHold = kFLEXBUS_Hold1Or0Cycles; /* Read address hold 0 cycles */
  188. config->addressSetup =
  189. kFLEXBUS_FirstRisingEdge; /* Assert ~FB_CSn on the first rising clock edge after the address is asserted */
  190. config->portSize = kFLEXBUS_1Byte; /* 1 byte port size of transfer */
  191. config->group1MultiplexControl = kFLEXBUS_MultiplexGroup1_FB_ALE; /* FB_ALE */
  192. config->group2MultiplexControl = kFLEXBUS_MultiplexGroup2_FB_CS4; /* FB_CS4 */
  193. config->group3MultiplexControl = kFLEXBUS_MultiplexGroup3_FB_CS5; /* FB_CS5 */
  194. config->group4MultiplexControl = kFLEXBUS_MultiplexGroup4_FB_TBST; /* FB_TBST */
  195. config->group5MultiplexControl = kFLEXBUS_MultiplexGroup5_FB_TA; /* FB_TA */
  196. }