fsl_sdramc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #ifndef _FSL_SDRAMC_H_
  35. #define _FSL_SDRAMC_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup sdramc
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*! @name Driver version */
  45. /*@{*/
  46. /*! @brief SDRAMC driver version 2.1.0. */
  47. #define FSL_SDRAMC_DRIVER_VERSION (MAKE_VERSION(2, 1, 0))
  48. /*@}*/
  49. /*! @brief SDRAM controller auto-refresh timing. */
  50. typedef enum _sdramc_refresh_time
  51. {
  52. kSDRAMC_RefreshThreeClocks = 0x0U, /*!< The refresh timing with three bus clocks. */
  53. kSDRAMC_RefreshSixClocks, /*!< The refresh timing with six bus clocks. */
  54. kSDRAMC_RefreshNineClocks /*!< The refresh timing with nine bus clocks. */
  55. } sdramc_refresh_time_t;
  56. /*!
  57. * @brief Setting latency for SDRAM controller timing specifications.
  58. *
  59. * The latency setting affects the following SDRAM timing specifications:
  60. * - trcd: SRAS assertion to SCAS assertion \n
  61. * - tcasl: SCAS assertion to data out \n
  62. * - tras: ACTV command to Precharge command \n
  63. * - trp: Precharge command to ACTV command \n
  64. * - trwl, trdl: Last data input to Precharge command \n
  65. * - tep: Last data out to Precharge command \n
  66. * The details of the latency setting and timing specifications are shown in the following table list. \n
  67. * latency trcd: tcasl tras trp trwl,trdl tep \n
  68. * 0 1 bus clock 1 bus clock 2 bus clocks 1 bus clock 1 bus clock 1 bus clock \n
  69. * 1 2 bus clock 2 bus clock 4 bus clocks 2 bus clock 1 bus clock 1 bus clock \n
  70. * 2 3 bus clock 3 bus clock 6 bus clocks 3 bus clock 1 bus clock 1 bus clock \n
  71. * 3 3 bus clock 3 bus clock 6 bus clocks 3 bus clock 1 bus clock 1 bus clock \n
  72. */
  73. typedef enum _sdramc_latency
  74. {
  75. kSDRAMC_LatencyZero = 0x0U, /*!< Latency 0. */
  76. kSDRAMC_LatencyOne, /*!< Latency 1. */
  77. kSDRAMC_LatencyTwo, /*!< Latency 2. */
  78. kSDRAMC_LatencyThree, /*!< Latency 3. */
  79. } sdramc_latency_t;
  80. /*! @brief SDRAM controller command bit location. */
  81. typedef enum _sdramc_command_bit_location
  82. {
  83. kSDRAMC_Commandbit17 = 0x0U, /*!< Command bit location is bit 17. */
  84. kSDRAMC_Commandbit18, /*!< Command bit location is bit 18. */
  85. kSDRAMC_Commandbit19, /*!< Command bit location is bit 19. */
  86. kSDRAMC_Commandbit20, /*!< Command bit location is bit 20. */
  87. kSDRAMC_Commandbit21, /*!< Command bit location is bit 21. */
  88. kSDRAMC_Commandbit22, /*!< Command bit location is bit 22. */
  89. kSDRAMC_Commandbit23, /*!< Command bit location is bit 23. */
  90. kSDRAMC_Commandbit24 /*!< Command bit location is bit 24. */
  91. } sdramc_command_bit_location_t;
  92. /*! @brief SDRAM controller command. */
  93. typedef enum _sdramc_command
  94. {
  95. kSDRAMC_ImrsCommand = 0x0U, /*!< Initiate MRS command. */
  96. kSDRAMC_PrechargeCommand, /*!< Initiate precharge command. */
  97. kSDRAMC_SelfrefreshEnterCommand, /*!< Enter self-refresh command. */
  98. kSDRAMC_SelfrefreshExitCommand, /*!< Exit self-refresh command. */
  99. kSDRAMC_AutoRefreshEnableCommand, /*!< Enable Auto refresh command. */
  100. kSDRAMC_AutoRefreshDisableCommand, /*!< Disable Auto refresh command. */
  101. } sdramc_command_t;
  102. /*! @brief SDRAM port size. */
  103. typedef enum _sdramc_port_size
  104. {
  105. kSDRAMC_PortSize32Bit = 0x0U, /*!< 32-Bit port size. */
  106. kSDRAMC_PortSize8Bit, /*!< 8-Bit port size. */
  107. kSDRAMC_PortSize16Bit /*!< 16-Bit port size. */
  108. } sdramc_port_size_t;
  109. /*! @brief SDRAM controller block selection. */
  110. typedef enum _sdramc_block_selection
  111. {
  112. kSDRAMC_Block0 = 0x0U, /*!< Select SDRAM block 0. */
  113. kSDRAMC_Block1, /*!< Select SDRAM block 1. */
  114. } sdramc_block_selection_t;
  115. /*! @brief SDRAM controller block control configuration structure. */
  116. typedef struct _sdramc_blockctl_config
  117. {
  118. sdramc_block_selection_t block; /*!< The block number. */
  119. sdramc_port_size_t portSize; /*!< The port size of the associated SDRAM block. */
  120. sdramc_command_bit_location_t location; /*!< The command bit location. */
  121. sdramc_latency_t latency; /*!< The latency for some timing specifications. */
  122. uint32_t address; /*!< The base address of the SDRAM block. */
  123. uint32_t addressMask; /*!< The base address mask of the SDRAM block. */
  124. } sdramc_blockctl_config_t;
  125. /*! @brief SDRAM controller refresh timing configuration structure. */
  126. typedef struct _sdramc_refresh_config
  127. {
  128. sdramc_refresh_time_t refreshTime; /*!< Trc:The number of bus clocks inserted
  129. between a REF and next ACTIVE command. */
  130. uint32_t sdramRefreshRow; /*!< The SDRAM refresh time each row: ns/row. */
  131. uint32_t busClock_Hz; /*!< The bus clock for SDRAMC. */
  132. } sdramc_refresh_config_t;
  133. /*!
  134. * @brief SDRAM controller configuration structure.
  135. *
  136. * Defines a configure structure and uses the SDRAMC_Configure() function to make necessary
  137. * initializations.
  138. */
  139. typedef struct _sdramc_config_t
  140. {
  141. sdramc_refresh_config_t *refreshConfig; /*!< Refresh timing configure structure pointer. */
  142. sdramc_blockctl_config_t *blockConfig; /*!< Block configure structure pointer. If both SDRAM
  143. blocks are used, use the two continuous blockConfig. */
  144. uint8_t numBlockConfig; /*!< SDRAM block numbers for configuration. */
  145. } sdramc_config_t;
  146. /*******************************************************************************
  147. * API
  148. ******************************************************************************/
  149. #if defined(__cplusplus)
  150. extern "C" {
  151. #endif
  152. /*!
  153. * @name SDRAM Controller Initialization and De-initialization
  154. * @{
  155. */
  156. /*!
  157. * @brief Initializes the SDRAM controller.
  158. * This function ungates the SDRAM controller clock and initializes the SDRAM controller.
  159. * This function must be called before calling any other SDRAM controller driver functions.
  160. * Example
  161. @code
  162. sdramc_refresh_config_t refreshConfig;
  163. sdramc_blockctl_config_t blockConfig;
  164. sdramc_config_t config;
  165. refreshConfig.refreshTime = kSDRAM_RefreshThreeClocks;
  166. refreshConfig.sdramRefreshRow = 15625;
  167. refreshConfig.busClock = 60000000;
  168. blockConfig.block = kSDRAMC_Block0;
  169. blockConfig.portSize = kSDRAMC_PortSize16Bit;
  170. blockConfig.location = kSDRAMC_Commandbit19;
  171. blockConfig.latency = kSDRAMC_RefreshThreeClocks;
  172. blockConfig.address = SDRAM_START_ADDRESS;
  173. blockConfig.addressMask = 0x7c0000;
  174. config.refreshConfig = &refreshConfig,
  175. config.blockConfig = &blockConfig,
  176. config.totalBlocks = 1;
  177. SDRAMC_Init(SDRAM, &config);
  178. @endcode
  179. *
  180. * @param base SDRAM controller peripheral base address.
  181. * @param configure The SDRAM configuration structure pointer.
  182. */
  183. void SDRAMC_Init(SDRAM_Type *base, sdramc_config_t *configure);
  184. /*!
  185. * @brief Deinitializes the SDRAM controller module and gates the clock.
  186. * This function gates the SDRAM controller clock. As a result, the SDRAM
  187. * controller module doesn't work after calling this function.
  188. *
  189. * @param base SDRAM controller peripheral base address.
  190. */
  191. void SDRAMC_Deinit(SDRAM_Type *base);
  192. /* @} */
  193. /*!
  194. * @name SDRAM Controller Basic Operation
  195. * @{
  196. */
  197. /*!
  198. * @brief Sends the SDRAM command.
  199. * This function sends commands to SDRAM. The commands are precharge command, initialization MRS command,
  200. * auto-refresh enable/disable command, and self-refresh enter/exit commands.
  201. * Note that the self-refresh enter/exit commands are all blocks setting and "block"
  202. * is ignored. Ensure to set the correct "block" when send other commands.
  203. *
  204. * @param base SDRAM controller peripheral base address.
  205. * @param block The block selection.
  206. * @param command The SDRAM command, see "sdramc_command_t".
  207. * kSDRAMC_ImrsCommand - Initialize MRS command \n
  208. * kSDRAMC_PrechargeCommand - Initialize precharge command \n
  209. * kSDRAMC_SelfrefreshEnterCommand - Enter self-refresh command \n
  210. * kSDRAMC_SelfrefreshExitCommand - Exit self-refresh command \n
  211. * kSDRAMC_AutoRefreshEnableCommand - Enable auto refresh command \n
  212. * kSDRAMC_AutoRefreshDisableCommand - Disable auto refresh command
  213. */
  214. void SDRAMC_SendCommand(SDRAM_Type *base, sdramc_block_selection_t block, sdramc_command_t command);
  215. /*!
  216. * @brief Enables/disables the write protection.
  217. *
  218. * @param base SDRAM peripheral base address.
  219. * @param block The block which is selected.
  220. * @param enable True enable write protection, false disable write protection.
  221. */
  222. static inline void SDRAMC_EnableWriteProtect(SDRAM_Type *base, sdramc_block_selection_t block, bool enable)
  223. {
  224. if (enable)
  225. {
  226. base->BLOCK[block].CM |= SDRAM_CM_WP_MASK;
  227. }
  228. else
  229. {
  230. base->BLOCK[block].CM &= ~SDRAM_CM_WP_MASK;
  231. }
  232. }
  233. /*!
  234. * @brief Enables/disables the valid operation.
  235. *
  236. * @param base SDRAM peripheral base address.
  237. * @param block The block which is selected.
  238. * @param enable True enable the valid operation; false disable the valid operation.
  239. */
  240. static inline void SDRAMC_EnableOperateValid(SDRAM_Type *base, sdramc_block_selection_t block, bool enable)
  241. {
  242. if (enable)
  243. {
  244. base->BLOCK[block].CM |= SDRAM_CM_V_MASK;
  245. }
  246. else
  247. {
  248. base->BLOCK[block].CM &= ~SDRAM_CM_V_MASK;
  249. }
  250. }
  251. /* @} */
  252. #if defined(__cplusplus)
  253. }
  254. #endif
  255. /*! @}*/
  256. #endif /* _FSL_SDRAMC_H_*/