nrf_block_dev_qspi.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA 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. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_BLOCK_DEV_QSPI_H__
  41. #define NRF_BLOCK_DEV_QSPI_H__
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. #include "nrf_block_dev.h"
  46. #include "nrf_drv_qspi.h"
  47. #include "nrf_log_instance.h"
  48. /**@file
  49. *
  50. * @defgroup nrf_block_dev_qspi QSPI implementation
  51. * @ingroup nrf_block_dev
  52. * @{
  53. *
  54. */
  55. /**
  56. * @brief QSPI block device operations
  57. * */
  58. extern const nrf_block_dev_ops_t nrf_block_device_qspi_ops;
  59. /**
  60. * @brief QSPI block device internal erase unit buffer size
  61. * */
  62. #define NRF_BLOCK_DEV_QSPI_ERASE_UNIT_SIZE (4096)
  63. /**
  64. * @brief Internal Block device state
  65. */
  66. typedef enum {
  67. NRF_BLOCK_DEV_QSPI_STATE_DISABLED = 0, /**< QSPI block device state DISABLED */
  68. NRF_BLOCK_DEV_QSPI_STATE_IDLE, /**< QSPI block device state IDLE */
  69. NRF_BLOCK_DEV_QSPI_STATE_READ_EXEC, /**< QSPI block device state READ_EXEC */
  70. NRF_BLOCK_DEV_QSPI_STATE_EUNIT_LOAD, /**< QSPI block device state EUNIT_LOAD */
  71. NRF_BLOCK_DEV_QSPI_STATE_WRITE_ERASE, /**< QSPI block device state WRITE_ERASE */
  72. NRF_BLOCK_DEV_QSPI_STATE_WRITE_EXEC, /**< QSPI block device state WRITE_EXEC */
  73. } nrf_block_dev_qspi_state_t;
  74. /**
  75. * @brief Work structure of QSPI block device
  76. */
  77. typedef struct {
  78. volatile nrf_block_dev_qspi_state_t state; //!< QSPI block device state
  79. nrf_block_dev_geometry_t geometry; //!< Block device geometry
  80. nrf_block_dev_ev_handler ev_handler; //!< Block device event handler
  81. void const * p_context; //!< Context handle passed to event handler
  82. nrf_block_req_t req; //!< Block READ/WRITE request: original value
  83. nrf_block_req_t left_req; //!< Block READ/WRITE request: left value
  84. bool cache_flushing; //!< QSPI cache flush in progress flag
  85. bool writeback_mode; //!< QSPI write-back mode flag
  86. bool erase_required; //!< QSPI erase required flag
  87. uint32_t erase_unit_idx; //!< QSPI erase unit index
  88. uint32_t erase_unit_dirty_blocks; //!< QSPI erase unit dirty blocks mask
  89. uint8_t p_erase_unit_buff[NRF_BLOCK_DEV_QSPI_ERASE_UNIT_SIZE]; //!< QSPI erase unit buffer (fixed value)
  90. } nrf_block_dev_qspi_work_t;
  91. /**
  92. * @brief QSPI block device flags*/
  93. typedef enum {
  94. NRF_BLOCK_DEV_QSPI_FLAG_CACHE_WRITEBACK = (1u << 0) //!< Cache write-back mode enable flag
  95. } nrf_block_dev_qspi_flag_t;
  96. /** @brief Name of the module used for logger messaging.
  97. */
  98. #define NRF_BLOCK_DEV_QSPI_LOG_NAME block_dev_qspi
  99. /**
  100. * @brief QSPI block device config initializer (@ref nrf_block_dev_qspi_config_t)
  101. *
  102. * @param blk_size Block size
  103. * @param blk_flags Block device flags, @ref nrf_block_dev_qspi_flag_t
  104. * @param qspi_drv_config QPSI driver config
  105. * */
  106. #define NRF_BLOCK_DEV_QSPI_CONFIG(blk_size, blk_flags, qspi_drv_config) { \
  107. .block_size = (blk_size), \
  108. .flags = (blk_flags), \
  109. .qspi_config = qspi_drv_config \
  110. }
  111. /**
  112. * @brief QSPI block device config
  113. */
  114. typedef struct {
  115. uint32_t block_size; //!< Desired block size
  116. uint32_t flags; //!< QSPI block device flags
  117. nrf_drv_qspi_config_t qspi_config; //!< QSPI configuration
  118. } nrf_block_dev_qspi_config_t;
  119. /**
  120. * @brief QSPI block device
  121. * */
  122. typedef struct {
  123. nrf_block_dev_t block_dev; //!< Block device
  124. nrf_block_dev_info_strings_t info_strings; //!< Block device information strings
  125. nrf_block_dev_qspi_config_t qspi_bdev_config; //!< QSPI block device config
  126. nrf_block_dev_qspi_work_t * p_work; //!< QSPI block device work structure
  127. NRF_LOG_INSTANCE_PTR_DECLARE(p_log) //!< Pointer to instance of the logger object (Conditionally compiled).
  128. } nrf_block_dev_qspi_t;
  129. /**
  130. * @brief Defines a QSPI block device.
  131. *
  132. * @param name Instance name
  133. * @param config Configuration @ref nrf_block_dev_qspi_config_t
  134. * @param info Info strings @ref NFR_BLOCK_DEV_INFO_CONFIG
  135. * */
  136. #define NRF_BLOCK_DEV_QSPI_DEFINE(name, config, info) \
  137. static nrf_block_dev_qspi_work_t CONCAT_2(name, _work); \
  138. NRF_LOG_INSTANCE_REGISTER(NRF_BLOCK_DEV_QSPI_LOG_NAME, name, \
  139. NRF_BLOCK_DEV_QSPI_CONFIG_INFO_COLOR, \
  140. NRF_BLOCK_DEV_QSPI_CONFIG_DEBUG_COLOR, \
  141. NRF_BLOCK_DEV_QSPI_CONFIG_LOG_INIT_FILTER_LEVEL, \
  142. NRF_BLOCK_DEV_QSPI_CONFIG_LOG_ENABLED ? \
  143. NRF_BLOCK_DEV_QSPI_CONFIG_LOG_LEVEL : NRF_LOG_SEVERITY_NONE); \
  144. static const nrf_block_dev_qspi_t name = { \
  145. .block_dev = { .p_ops = &nrf_block_device_qspi_ops }, \
  146. .info_strings = BRACKET_EXTRACT(info), \
  147. .qspi_bdev_config = config, \
  148. .p_work = &CONCAT_2(name, _work), \
  149. NRF_LOG_INSTANCE_PTR_INIT(p_log, NRF_BLOCK_DEV_QSPI_LOG_NAME, name) \
  150. }
  151. /**
  152. * @brief Returns block device API handle from QSPI block device.
  153. *
  154. * @param[in] p_blk_qspi QSPI block device
  155. * @return Block device handle
  156. */
  157. static inline nrf_block_dev_t const *
  158. nrf_block_dev_qspi_ops_get(nrf_block_dev_qspi_t const * p_blk_qspi)
  159. {
  160. return &p_blk_qspi->block_dev;
  161. }
  162. /** @} */
  163. #ifdef __cplusplus
  164. }
  165. #endif
  166. #endif /* NRF_BLOCK_DEV_QSPI_H__ */