fsl_sdramc.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_sdramc.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /* Component ID definition, used by tools. */
  39. #ifndef FSL_COMPONENT_ID
  40. #define FSL_COMPONENT_ID "platform.drivers.sdramc"
  41. #endif
  42. /*! @brief Define macros for SDRAM driver. */
  43. #define SDRAMC_ONEMILLSEC_NANOSECONDS (1000000U)
  44. #define SDRAMC_ONESECOND_MILLISECONDS (1000U)
  45. /*******************************************************************************
  46. * Prototypes
  47. ******************************************************************************/
  48. /*!
  49. * @brief Get instance number for SDRAMC module.
  50. *
  51. * @param base SDRAMC peripheral base address
  52. */
  53. static uint32_t SDRAMC_GetInstance(SDRAM_Type *base);
  54. /*******************************************************************************
  55. * Variables
  56. ******************************************************************************/
  57. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  58. /*! @brief Pointers to SDRAMC clocks for each instance. */
  59. static const clock_ip_name_t s_sdramClock[FSL_FEATURE_SOC_SDRAM_COUNT] = SDRAM_CLOCKS;
  60. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  61. /*! @brief Pointers to SDRAMC bases for each instance. */
  62. static SDRAM_Type *const s_sdramcBases[] = SDRAM_BASE_PTRS;
  63. /*******************************************************************************
  64. * Code
  65. ******************************************************************************/
  66. static uint32_t SDRAMC_GetInstance(SDRAM_Type *base)
  67. {
  68. uint32_t instance;
  69. /* Find the instance index from base address mappings. */
  70. for (instance = 0; instance < ARRAY_SIZE(s_sdramcBases); instance++)
  71. {
  72. if (s_sdramcBases[instance] == base)
  73. {
  74. break;
  75. }
  76. }
  77. assert(instance < ARRAY_SIZE(s_sdramcBases));
  78. return instance;
  79. }
  80. void SDRAMC_Init(SDRAM_Type *base, sdramc_config_t *configure)
  81. {
  82. assert(configure);
  83. assert(configure->refreshConfig);
  84. assert(configure->blockConfig);
  85. assert(configure->refreshConfig->busClock_Hz);
  86. sdramc_blockctl_config_t *bctlConfig = configure->blockConfig;
  87. sdramc_refresh_config_t *refreshConfig = configure->refreshConfig;
  88. uint32_t count;
  89. uint32_t index;
  90. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  91. /* Un-gate sdram controller clock. */
  92. CLOCK_EnableClock(s_sdramClock[SDRAMC_GetInstance(base)]);
  93. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  94. /* Initialize sdram Auto refresh timing. */
  95. count = refreshConfig->sdramRefreshRow * (refreshConfig->busClock_Hz / SDRAMC_ONESECOND_MILLISECONDS);
  96. count = (count / SDRAMC_ONEMILLSEC_NANOSECONDS) / 16 - 1;
  97. base->CTRL = SDRAM_CTRL_RC(count) | SDRAM_CTRL_RTIM(refreshConfig->refreshTime);
  98. for (index = 0; index < configure->numBlockConfig; index++)
  99. {
  100. /* Set the sdram block control. */
  101. base->BLOCK[index].AC = SDRAM_AC_PS(bctlConfig->portSize) | SDRAM_AC_CASL(bctlConfig->latency) |
  102. SDRAM_AC_CBM(bctlConfig->location) | (bctlConfig->address & SDRAM_AC_BA_MASK);
  103. base->BLOCK[index].CM = (bctlConfig->addressMask & SDRAM_CM_BAM_MASK) | SDRAM_CM_V_MASK;
  104. /* Increases to the next sdram block. */
  105. bctlConfig++;
  106. }
  107. }
  108. void SDRAMC_Deinit(SDRAM_Type *base)
  109. {
  110. /* Set the SDRAMC invalid, do not decode DRAM accesses. */
  111. SDRAMC_EnableOperateValid(base, kSDRAMC_Block0, false);
  112. SDRAMC_EnableOperateValid(base, kSDRAMC_Block1, false);
  113. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  114. /* Disable SDRAM clock. */
  115. CLOCK_DisableClock(s_sdramClock[SDRAMC_GetInstance(base)]);
  116. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  117. }
  118. void SDRAMC_SendCommand(SDRAM_Type *base, sdramc_block_selection_t block, sdramc_command_t command)
  119. {
  120. switch (command)
  121. {
  122. /* Initiate mrs command. */
  123. case kSDRAMC_ImrsCommand:
  124. base->BLOCK[block].AC |= SDRAM_AC_IMRS_MASK;
  125. break;
  126. /* Initiate precharge command. */
  127. case kSDRAMC_PrechargeCommand:
  128. base->BLOCK[block].AC |= SDRAM_AC_IP_MASK;
  129. break;
  130. /* Enable Auto refresh command. */
  131. case kSDRAMC_AutoRefreshEnableCommand:
  132. base->BLOCK[block].AC |= SDRAM_AC_RE_MASK;
  133. break;
  134. /* Disable Auto refresh command. */
  135. case kSDRAMC_AutoRefreshDisableCommand:
  136. base->BLOCK[block].AC &= ~SDRAM_AC_RE_MASK;
  137. break;
  138. /* Enter self-refresh command. */
  139. case kSDRAMC_SelfrefreshEnterCommand:
  140. base->CTRL |= SDRAM_CTRL_IS_MASK;
  141. break;
  142. /* Exit self-refresh command. */
  143. case kSDRAMC_SelfrefreshExitCommand:
  144. base->CTRL &= ~SDRAM_CTRL_IS_MASK;
  145. break;
  146. default:
  147. break;
  148. }
  149. }