fsl_emc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_EMC_H_
  31. #define _FSL_EMC_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup emc
  35. * @{
  36. */
  37. /*******************************************************************************
  38. * Definitions
  39. ******************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. /*! @brief EMC driver version 2.0.0. */
  43. #define FSL_EMC_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  44. /*@}*/
  45. /*! @brief Define the chip numbers for dynamic and static memory devices. */
  46. #define EMC_STATIC_MEMDEV_NUM (4U)
  47. #define EMC_DYNAMIC_MEMDEV_NUM (4U)
  48. #define EMC_ADDRMAP_SHIFT EMC_DYNAMIC_DYNAMICCONFIG_AM0_SHIFT
  49. #define EMC_ADDRMAP_MASK (EMC_DYNAMIC_DYNAMICCONFIG_AM0_MASK |EMC_DYNAMIC_DYNAMICCONFIG_AM1_MASK)
  50. #define EMC_ADDRMAP(x) (((uint32_t)(((uint32_t)(x)) << EMC_ADDRMAP_SHIFT)) & EMC_ADDRMAP_MASK)
  51. #define EMC_HZ_ONEMHZ (1000000U)
  52. #define EMC_MILLISECS_ONESEC (1000U)
  53. #define EMC_SDRAM_MODE_CL_SHIFT (4U)
  54. #define EMC_SDRAM_MODE_CL_MASK (0x70U)
  55. /*!
  56. * @brief Define EMC memory width for static memory device.
  57. */
  58. typedef enum _emc_static_memwidth
  59. {
  60. kEMC_8BitWidth = 0x0U, /*!< 8 bit memory width. */
  61. kEMC_16BitWidth, /*!< 16 bit memory width. */
  62. kEMC_32BitWidth /*!< 32 bit memory width. */
  63. } emc_static_memwidth_t;
  64. /*!
  65. * @brief Define EMC static configuration.
  66. */
  67. typedef enum _emc_static_special_config
  68. {
  69. kEMC_AsynchronosPageEnable = 0x0008U,/*!< Enable the asynchronous page mode. page length four. */
  70. kEMC_ActiveHighChipSelect = 0x0040U, /*!< Chip select active high. */
  71. kEMC_ByteLaneStateAllLow = 0x0080U, /*!< Reads/writes the respective valuie bits in BLS3:0 are low. */
  72. kEMC_ExtWaitEnable = 0x0100U, /*!< Extended wait enable. */
  73. kEMC_BufferEnable = 0x80000U /*!< Buffer enable. */
  74. } emc_static_special_config_t;
  75. /*! @brief EMC dynamic memory device. */
  76. typedef enum _emc_dynamic_device
  77. {
  78. kEMC_Sdram = 0x0U, /*!< Dynamic memory device: SDRAM. */
  79. kEMC_Lpsdram, /*!< Dynamic memory device: Low-power SDRAM. */
  80. } emc_dynamic_device_t;
  81. /*! @brief EMC dynamic read strategy. */
  82. typedef enum _emc_dynamic_read
  83. {
  84. kEMC_NoDelay = 0x0U, /*!< No delay. */
  85. kEMC_Cmddelay, /*!< Command delayed strategy, using EMCCLKDELAY. */
  86. kEMC_CmdDelayPulseOneclk, /*!< Command delayed strategy pluse one clock cycle using EMCCLKDELAY. */
  87. kEMC_CmddelayPulsetwoclk, /*!< Command delayed strategy pulse two clock cycle using EMCCLKDELAY. */
  88. } emc_dynamic_read_t;
  89. /*! @brief EMC endian mode. */
  90. typedef enum _emc_endian_mode
  91. {
  92. kEMC_LittleEndian = 0x0U, /*!< Little endian mode. */
  93. kEMC_BigEndian, /*!< Big endian mode. */
  94. } emc_endian_mode_t;
  95. /*! @brief EMC Feedback clock input source select. */
  96. typedef enum _emc_fbclk_src
  97. {
  98. kEMC_IntloopbackEmcclk = 0U, /*!< Use the internal loop back from EMC_CLK output. */
  99. kEMC_EMCFbclkInput /*!< Use the external EMC_FBCLK input. */
  100. } emc_fbclk_src_t;
  101. /*! @brief EMC dynamic timing/delay configure structure. */
  102. typedef struct _emc_dynamic_timing_config
  103. {
  104. emc_dynamic_read_t readConfig; /* Dynamic read strategy. */
  105. uint32_t refreshPeriod_Nanosec; /*!< The refresh period in unit of nanosecond. */
  106. uint32_t tRp_Ns; /*!< Precharge command period in unit of nanosecond. */
  107. uint32_t tRas_Ns; /*!< Active to precharge command period in unit of nanosecond. */
  108. uint32_t tSrex_Ns; /*!< Self-refresh exit time in unit of nanosecond. */
  109. uint32_t tApr_Ns; /*!< Last data out to active command time in unit of nanosecond. */
  110. uint32_t tDal_Ns; /*!< Data-in to active command in unit of nanosecond. */
  111. uint32_t tWr_Ns; /*!< Write recovery time in unit of nanosecond. */
  112. uint32_t tRc_Ns; /*!< Active to active command period in unit of nanosecond. */
  113. uint32_t tRfc_Ns; /*!< Auto-refresh period and auto-refresh to active command period in unit of nanosecond. */
  114. uint32_t tXsr_Ns; /*!< Exit self-refresh to active command time in unit of nanosecond. */
  115. uint32_t tRrd_Ns; /*!< Active bank A to active bank B latency in unit of nanosecond. */
  116. uint8_t tMrd_Nclk; /*!< Load mode register to active command time in unit of EMCCLK cycles.*/
  117. } emc_dynamic_timing_config_t;
  118. /*!
  119. * @brief EMC dynamic memory controller independent chip configuration structure.
  120. * Please take refer to the address mapping table in the RM in EMC chapter when you
  121. * set the "devAddrMap". Choose the right Bit 14 Bit12 ~ Bit 7 group in the table
  122. * according to the bus width/banks/row/colum length for you device.
  123. * Set devAddrMap with the value make up with the seven bits (bit14 bit12 ~ bit 7)
  124. * and inset the bit 13 with 0.
  125. * for example, if the bit 14 and bit12 ~ bit7 is 1000001 is choosen according to the
  126. * 32bit high-performance bus width with 2 banks, 11 row lwngth, 8 column length.
  127. * Set devAddrMap with 0x81.
  128. */
  129. typedef struct _emc_dynamic_chip_config
  130. {
  131. uint8_t chipIndex; /*!< Chip Index, range from 0 ~ EMC_DYNAMIC_MEMDEV_NUM - 1. */
  132. emc_dynamic_device_t dynamicDevice; /*!< All chips shall use the same device setting. mixed use are not supported. */
  133. uint8_t rAS_Nclk; /*!< Active to read/write delay tRCD. */
  134. uint16_t sdramModeReg; /*!< Sdram mode register setting. */
  135. uint16_t sdramExtModeReg; /*!< Used for low-power sdram device. The extended mode register. */
  136. uint8_t devAddrMap; /*!< dynamic device address mapping, choose the address mapping for your specific device. */
  137. } emc_dynamic_chip_config_t;
  138. /*!
  139. * @brief EMC static memory controller independent chip configuration structure.
  140. */
  141. typedef struct _emc_static_chip_config
  142. {
  143. uint8_t chipIndex;
  144. emc_static_memwidth_t memWidth; /*!< Memory width. */
  145. uint32_t specailConfig; /*!< Static configuration,a logical OR of "emc_static_special_config_t". */
  146. uint32_t tWaitWriteEn_Ns;/*!< The delay form chip select to write enable in unit of nanosecond. */
  147. uint32_t tWaitOutEn_Ns; /*!< The delay from chip selcet to output enable in unit of nanosecond. */
  148. uint32_t tWaitReadNoPage_Ns;/*!< In No-page mode, the delay from chip select to read access in unit of nanosecond. */
  149. uint32_t tWaitReadPage_Ns; /*!< In page mode, the read after the first read wait states in unit of nanosecond. */
  150. uint32_t tWaitWrite_Ns; /*!< The delay from chip select to write access in unit of nanosecond. */
  151. uint32_t tWaitTurn_Ns; /*!< The Bus turn-around time in unit of nanosecond. */
  152. } emc_static_chip_config_t;
  153. /*!
  154. * @brief EMC module basic configuration structure.
  155. *
  156. * Defines the static memory controller configure structure and
  157. * uses the EMC_Init() function to make necessary initializations.
  158. *
  159. */
  160. typedef struct _emc_basic_config
  161. {
  162. emc_endian_mode_t endian; /*!< Endian mode . */
  163. emc_fbclk_src_t fbClkSrc; /*!< The feedback clock source. */
  164. uint8_t emcClkDiv; /*!< EMC_CLK = AHB_CLK / (emc_clkDiv + 1). */
  165. } emc_basic_config_t;
  166. /*******************************************************************************
  167. * API
  168. ******************************************************************************/
  169. #if defined(__cplusplus)
  170. extern "C" {
  171. #endif
  172. /*!
  173. * @name EMC Initialize and de-initialize opeartion
  174. * @{
  175. */
  176. /*!
  177. * @brief Initializes the basic for EMC.
  178. * This function ungates the EMC clock, initializes the emc system configure
  179. * and enable the EMC module. This function must be called in the first step to initialize
  180. * the external memory.
  181. *
  182. * @param base EMC peripheral base address.
  183. * @param config The EMC basic configuration.
  184. */
  185. void EMC_Init(EMC_Type *base, emc_basic_config_t *config);
  186. /*!
  187. * @brief Initializes the dynamic memory controller.
  188. * This function initializes the dynamic memory controller in external memory controller.
  189. * This function must be called after EMC_Init and before accessing the external dynamic memory.
  190. *
  191. * @param base EMC peripheral base address.
  192. * @param timing The timing and latency for dynamica memory controller setting. It shall
  193. * be used for all dynamica memory chips, threfore the worst timing value for all
  194. * used chips must be given.
  195. * @param configure The EMC dynamic memory controller chip independent configuration pointer.
  196. * This configuration pointer is actually pointer to a configration array. the array number
  197. * depends on the "totalChips".
  198. * @param totalChips The total dynamic memory chip numbers been used or the length of the
  199. * "emc_dynamic_chip_config_t" type memory.
  200. */
  201. void EMC_DynamicMemInit(EMC_Type *base, emc_dynamic_timing_config_t *timing,
  202. emc_dynamic_chip_config_t *config, uint32_t totalChips);
  203. /*!
  204. * @brief Initializes the static memory controller.
  205. * This function initializes the static memory controller in external memory controller.
  206. * This function must be called after EMC_Init and before accessing the external static memory.
  207. *
  208. * @param base EMC peripheral base address.
  209. * @param extWait_Ns The extended wait timeout or the read/write transfer time.
  210. * This is common for all static memory chips and set with NULL if not required.
  211. * @param configure The EMC static memory controller chip independent configuration pointer.
  212. * This configuration pointer is actually pointer to a configration array. the array number
  213. * depends on the "totalChips".
  214. * @param totalChips The total static memory chip numbers been used or the length of the
  215. * "emc_static_chip_config_t" type memory.
  216. */
  217. void EMC_StaticMemInit(EMC_Type *base, uint32_t *extWait_Ns, emc_static_chip_config_t *config, uint32_t totalChips);
  218. /*!
  219. * @brief Deinitializes the EMC module and gates the clock.
  220. * This function gates the EMC controller clock. As a result, the EMC
  221. * module doesn't work after calling this function.
  222. *
  223. * @param base EMC peripheral base address.
  224. */
  225. void EMC_Deinit(EMC_Type *base);
  226. /* @} */
  227. /*!
  228. * @name EMC Basic Operation
  229. * @{
  230. */
  231. /*!
  232. * @brief Enables/disables the EMC module.
  233. *
  234. * @param base EMC peripheral base address.
  235. * @param enable True enable EMC module, false disable.
  236. */
  237. static inline void EMC_Enable(EMC_Type *base, bool enable)
  238. {
  239. if (enable)
  240. {
  241. base->CONTROL |= EMC_CONTROL_E_MASK;
  242. }
  243. else
  244. {
  245. base->CONTROL &= ~EMC_CONTROL_E_MASK;
  246. }
  247. }
  248. /*!
  249. * @brief Enables/disables the EMC Dynaimc memory controller.
  250. *
  251. * @param base EMC peripheral base address.
  252. * @param enable True enable EMC dynamic memory controller, false disable.
  253. */
  254. static inline void EMC_EnableDynamicMemControl(EMC_Type *base, bool enable)
  255. {
  256. if (enable)
  257. {
  258. base->DYNAMICCONTROL |= (EMC_DYNAMICCONTROL_CE_MASK | EMC_DYNAMICCONTROL_CS_MASK);
  259. }
  260. else
  261. {
  262. base->DYNAMICCONTROL &= ~(EMC_DYNAMICCONTROL_CE_MASK | EMC_DYNAMICCONTROL_CS_MASK);
  263. }
  264. }
  265. /*!
  266. * @brief Enables/disables the EMC address mirror.
  267. * Enable the address mirror the EMC_CS1is mirrored to both EMC_CS0
  268. * and EMC_DYCS0 memory areas. Disable the address mirror enables
  269. * EMC_cS0 and EMC_DYCS0 memory to be accessed.
  270. *
  271. * @param base EMC peripheral base address.
  272. * @param enable True enable the address mirror, false disable the address mirror.
  273. */
  274. static inline void EMC_MirrorChipAddr(EMC_Type *base, bool enable)
  275. {
  276. if (enable)
  277. {
  278. base->CONTROL |= EMC_CONTROL_M_MASK;
  279. }
  280. else
  281. {
  282. base->CONTROL &= ~EMC_CONTROL_M_MASK;
  283. }
  284. }
  285. /*!
  286. * @brief Enter the self-refresh mode for dynamic memory controller.
  287. * This function provided self-refresh mode enter or exit for application.
  288. *
  289. * @param base EMC peripheral base address.
  290. * @param enable True enter the self-refresh mode, false to exit self-refresh
  291. * and enter the normal mode.
  292. */
  293. static inline void EMC_EnterSelfRefreshCommand(EMC_Type *base, bool enable)
  294. {
  295. if (enable)
  296. {
  297. base->DYNAMICCONTROL |= EMC_DYNAMICCONTROL_SR_MASK;
  298. }
  299. else
  300. {
  301. base->DYNAMICCONTROL &= ~EMC_DYNAMICCONTROL_SR_MASK;
  302. }
  303. }
  304. /*!
  305. * @brief Get the operating mode of the EMC.
  306. * This function can be used to get the operating mode of the EMC.
  307. *
  308. * @param base EMC peripheral base address.
  309. * @return The EMC in self-refresh mode if true, else in normal mode.
  310. */
  311. static inline bool EMC_IsInSelfrefreshMode(EMC_Type *base)
  312. {
  313. return ((base->STATUS & EMC_STATUS_SA_MASK) ? true : false);
  314. }
  315. /*!
  316. * @brief Enter/exit the low-power mode.
  317. *
  318. * @param base EMC peripheral base address.
  319. * @param enable True Enter the low-power mode, false exit low-power mode
  320. * and return to normal mode.
  321. */
  322. static inline void EMC_EnterLowPowerMode(EMC_Type *base, bool enable)
  323. {
  324. if (enable)
  325. {
  326. base->CONTROL |= EMC_CONTROL_L_MASK;
  327. }
  328. else
  329. {
  330. base->CONTROL &= ~ EMC_CONTROL_L_MASK;
  331. }
  332. }
  333. /* @} */
  334. #if defined(__cplusplus)
  335. }
  336. #endif
  337. /*! @}*/
  338. #endif /* _FSL_EMC_H_*/