fsl_spifi.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #include "fsl_spifi.h"
  31. /*******************************************************************************
  32. * Definitions
  33. ******************************************************************************/
  34. /*******************************************************************************
  35. * Prototypes
  36. ******************************************************************************/
  37. /*!
  38. * @brief Get the SPIFI instance from peripheral base address.
  39. *
  40. * @param base SPIFI peripheral base address.
  41. * @return SPIFI instance.
  42. */
  43. uint32_t SPIFI_GetInstance(SPIFI_Type *base);
  44. /*******************************************************************************
  45. * Variables
  46. ******************************************************************************/
  47. /* Array of SPIFI peripheral base address. */
  48. static SPIFI_Type *const s_spifiBases[] = SPIFI_BASE_PTRS;
  49. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  50. /* Array of SPIFI clock name. */
  51. static const clock_ip_name_t s_spifiClock[] = SPIFI_CLOCKS;
  52. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  53. /*******************************************************************************
  54. * Code
  55. ******************************************************************************/
  56. uint32_t SPIFI_GetInstance(SPIFI_Type *base)
  57. {
  58. uint32_t instance;
  59. /* Find the instance index from base address mappings. */
  60. for (instance = 0; instance < ARRAY_SIZE(s_spifiBases); instance++)
  61. {
  62. if (s_spifiBases[instance] == base)
  63. {
  64. break;
  65. }
  66. }
  67. assert(instance < ARRAY_SIZE(s_spifiBases));
  68. return instance;
  69. }
  70. void SPIFI_GetDefaultConfig(spifi_config_t *config)
  71. {
  72. config->timeout = 0xFFFFU;
  73. config->csHighTime = 0xFU;
  74. config->disablePrefetch = false;
  75. config->disableCachePrefech = false;
  76. config->isFeedbackClock = true;
  77. config->spiMode = kSPIFI_SPISckLow;
  78. config->isReadFullClockCycle = false;
  79. config->dualMode = kSPIFI_QuadMode;
  80. }
  81. void SPIFI_Init(SPIFI_Type *base, const spifi_config_t *config)
  82. {
  83. assert(config);
  84. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  85. /* Enable the SAI clock */
  86. CLOCK_EnableClock(s_spifiClock[SPIFI_GetInstance(base)]);
  87. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  88. /* Reset the Command register */
  89. SPIFI_ResetCommand(base);
  90. /* Set time delay parameter */
  91. base->CTRL = SPIFI_CTRL_TIMEOUT(config->timeout) | SPIFI_CTRL_CSHIGH(config->csHighTime) |
  92. SPIFI_CTRL_D_PRFTCH_DIS(config->disablePrefetch) | SPIFI_CTRL_MODE3(config->spiMode) |
  93. SPIFI_CTRL_PRFTCH_DIS(config->disableCachePrefech) | SPIFI_CTRL_DUAL(config->dualMode) |
  94. SPIFI_CTRL_RFCLK(config->isReadFullClockCycle) | SPIFI_CTRL_FBCLK(config->isFeedbackClock);
  95. }
  96. void SPIFI_Deinit(SPIFI_Type *base)
  97. {
  98. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  99. /* Enable the SAI clock */
  100. CLOCK_DisableClock(s_spifiClock[SPIFI_GetInstance(base)]);
  101. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  102. }
  103. void SPIFI_SetCommand(SPIFI_Type *base, spifi_command_t *cmd)
  104. {
  105. /* Wait for the CMD and MCINT flag all be 0 */
  106. while (SPIFI_GetStatusFlag(base) & (SPIFI_STAT_MCINIT_MASK | SPIFI_STAT_CMD_MASK))
  107. {
  108. }
  109. base->CMD = SPIFI_CMD_DATALEN(cmd->dataLen) | SPIFI_CMD_POLL(cmd->isPollMode) | SPIFI_CMD_DOUT(cmd->direction) |
  110. SPIFI_CMD_INTLEN(cmd->intermediateBytes) | SPIFI_CMD_FIELDFORM(cmd->format) |
  111. SPIFI_CMD_FRAMEFORM(cmd->type) | SPIFI_CMD_OPCODE(cmd->opcode);
  112. /* Wait for the command written */
  113. while ((base->STAT & SPIFI_STAT_CMD_MASK) == 0U)
  114. {
  115. }
  116. }
  117. void SPIFI_SetMemoryCommand(SPIFI_Type *base, spifi_command_t *cmd)
  118. {
  119. /* Wait for the CMD and MCINT flag all be 0 */
  120. while (SPIFI_GetStatusFlag(base) & (SPIFI_STAT_MCINIT_MASK | SPIFI_STAT_CMD_MASK))
  121. {
  122. }
  123. base->MCMD = SPIFI_MCMD_POLL(0U) | SPIFI_MCMD_DOUT(0U) | SPIFI_MCMD_INTLEN(cmd->intermediateBytes) |
  124. SPIFI_MCMD_FIELDFORM(cmd->format) | SPIFI_MCMD_FRAMEFORM(cmd->type) | SPIFI_MCMD_OPCODE(cmd->opcode);
  125. }