qspi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /****************************************************************************
  2. * include/nuttx/qspi/qspi.h
  3. *
  4. * Copyright(C) 2015 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. #ifndef __INCLUDE_NUTTX_QSPI_QSPI_H
  36. #define __INCLUDE_NUTTX_QSPI_QSPI_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <sys/types.h>
  42. #include <stdint.h>
  43. #include <stdbool.h>
  44. /****************************************************************************
  45. * Pre-processor Definitions
  46. ****************************************************************************/
  47. /* Access macros ************************************************************/
  48. /****************************************************************************
  49. * Name: QSPI_LOCK
  50. *
  51. * Description:
  52. * On QSPI buses where there are multiple devices, it will be necessary to
  53. * lock QSPI to have exclusive access to the buses for a sequence of
  54. * transfers. The bus should be locked before the chip is selected. After
  55. * locking the QSPI bus, the caller should then also call the setfrequency,
  56. * setbits, and setmode methods to make sure that the QSPI is properly
  57. * configured for the device. If the QSPI bus is being shared, then it
  58. * may have been left in an incompatible state.
  59. *
  60. * Input Parameters:
  61. * dev - Device-specific state data
  62. * lock - true: Lock qspi bus, false: unlock QSPI bus
  63. *
  64. * Returned Value:
  65. * None
  66. *
  67. ****************************************************************************/
  68. #define QSPI_LOCK(d,l) (d)->ops->lock(d,l)
  69. /****************************************************************************
  70. * Name: QSPI_SETFREQUENCY
  71. *
  72. * Description:
  73. * Set the QSPI frequency. Required.
  74. *
  75. * Input Parameters:
  76. * dev - Device-specific state data
  77. * frequency - The QSPI frequency requested
  78. *
  79. * Returned Value:
  80. * Returns the actual frequency selected
  81. *
  82. ****************************************************************************/
  83. #define QSPI_SETFREQUENCY(d,f) ((d)->ops->setfrequency(d,f))
  84. /****************************************************************************
  85. * Name: QSPI_SETMODE
  86. *
  87. * Description:
  88. * Set the QSPI mode. Optional. See enum qspi_mode_e for mode definitions.
  89. *
  90. * Input Parameters:
  91. * dev - Device-specific state data
  92. * mode - The QSPI mode requested
  93. *
  94. * Returned Value:
  95. * none
  96. *
  97. ****************************************************************************/
  98. #define QSPI_SETMODE(d,m) (d)->ops->setmode(d,m)
  99. /****************************************************************************
  100. * Name: QSPI_SETBITS
  101. *
  102. * Description:
  103. * Set the number if bits per word.
  104. *
  105. * Input Parameters:
  106. * dev - Device-specific state data
  107. * nbits - The number of bits requests.
  108. * If value is greater > 0 then it implies MSB first
  109. * If value is below < 0, then it implies LSB first with -nbits
  110. *
  111. * Returned Value:
  112. * none
  113. *
  114. ****************************************************************************/
  115. #define QSPI_SETBITS(d,b) (d)->ops->setbits(d,b)
  116. /****************************************************************************
  117. * Name: QSPI_COMMAND
  118. *
  119. * Description:
  120. * Perform one QSPI command transfer
  121. *
  122. * Input Parameters:
  123. * dev - Device-specific state data
  124. * cmdinfo - Describes the command transfer to be performed.
  125. *
  126. * Returned Value:
  127. * Zero (OK) on SUCCESS, a negated errno on value of failure
  128. *
  129. ****************************************************************************/
  130. #define QSPI_COMMAND(d,c) (d)->ops->command(d,c)
  131. /* QSPI Command Transfer Flags */
  132. #define QSPICMD_ADDRESS (1 << 0) /* Bit 0: Enable address transfer */
  133. #define QSPICMD_READDATA (1 << 1) /* Bit 1: Enable read data transfer */
  134. #define QSPICMD_WRITEDATA (1 << 2) /* Bit 2: Enable write data transfer */
  135. #define QSPICMD_IDUAL (1 << 3) /* Bit 3: Instruction on two lines */
  136. #define QSPICMD_IQUAD (1 << 4) /* Bit 4: Instruction on four lines */
  137. #define QSPICMD_ISADDRESS(f) (((f) & QSPICMD_ADDRESS) != 0)
  138. #define QSPICMD_ISDATA(f) (((f) & (QSPICMD_READDATA | QSPICMD_WRITEDATA)) != 0)
  139. #define QSPICMD_ISREAD(f) (((f) & QSPICMD_READDATA) != 0)
  140. #define QSPICMD_ISWRITE(f) (((f) & QSPICMD_WRITEDATA) != 0)
  141. #define QSPICMD_ISIDUAL(f) (((f) & QSPICMD_IDUAL) != 0)
  142. #define QSPICMD_ISIQUAD(f) (((f) & QSPICMD_IQUAD) != 0)
  143. /****************************************************************************
  144. * Name: QSPI_MEMORY
  145. *
  146. * Description:
  147. * Perform one QSPI memory transfer
  148. *
  149. * Input Parameters:
  150. * dev - Device-specific state data
  151. * meminfo - Describes the memory transfer to be performed.
  152. *
  153. * Returned Value:
  154. * Zero (OK) on SUCCESS, a negated errno on value of failure
  155. *
  156. ****************************************************************************/
  157. #define QSPI_MEMORY(d,m) (d)->ops->memory(d,m)
  158. /* QSPI Memory Transfer Flags */
  159. #define QSPIMEM_READ (0) /* Bit 2: 0=Memory read data transfer */
  160. #define QSPIMEM_WRITE (1 << 2) /* Bit 2: 1=Memory write data transfer */
  161. #define QSPIMEM_DUALIO (1 << 3) /* Bit 3: Use Dual I/O (READ only) */
  162. #define QSPIMEM_QUADIO (1 << 4) /* Bit 4: Use Quad I/O (READ only) */
  163. #define QSPIMEM_SCRAMBLE (1 << 5) /* Bit 5: Scramble data */
  164. #define QSPIMEM_RANDOM (1 << 6) /* Bit 6: Use random key in scrambler */
  165. #define QSPIMEM_IDUAL (1 << 7) /* Bit 7: Instruction on two lines */
  166. #define QSPIMEM_IQUAD (1 << 0) /* Bit 0: Instruction on four lines */
  167. #define QSPIMEM_ISREAD(f) (((f) & QSPIMEM_WRITE) == 0)
  168. #define QSPIMEM_ISWRITE(f) (((f) & QSPIMEM_WRITE) != 0)
  169. #define QSPIMEM_ISDUALIO(f) (((f) & QSPIMEM_DUALIO) != 0)
  170. #define QSPIMEM_ISQUADIO(f) (((f) & QSPIMEM_QUADIO) != 0)
  171. #define QSPIMEM_ISSCRAMBLE(f) (((f) & QSPIMEM_SCRAMBLE) != 0)
  172. #define QSPIMEM_ISIDUAL(f) (((f) & QSPIMEM_IDUAL) != 0)
  173. #define QSPIMEM_ISIQUAD(f) (((f) & QSPIMEM_IQUAD) != 0)
  174. #define QSPIMEM_ISRANDOM(f) \
  175. (((f) & (QSPIMEM_SCRAMBLE|QSPIMEM_RANDOM)) == \
  176. (QSPIMEM_SCRAMBLE|QSPIMEM_RANDOM))
  177. /****************************************************************************
  178. * Name: QSPI_ALLOC
  179. *
  180. * Description:
  181. * Allocate a buffer suitable for DMA data transfer
  182. *
  183. * Input Parameters:
  184. * dev - Device-specific state data
  185. * buflen - Buffer length to allocate in bytes
  186. *
  187. * Returned Value:
  188. * Address of the allocated memory on success; NULL is returned on any
  189. * failure.
  190. *
  191. ****************************************************************************/
  192. #define QSPI_ALLOC(d,b) (d)->ops->alloc(d,b)
  193. /****************************************************************************
  194. * Name: QSPI_FREE
  195. *
  196. * Description:
  197. * Free memory returned by QSPI_ALLOC
  198. *
  199. * Input Parameters:
  200. * dev - Device-specific state data
  201. * buffer - Buffer previously allocated via QSPI_ALLOC
  202. *
  203. * Returned Value:
  204. * None.
  205. *
  206. ****************************************************************************/
  207. #define QSPI_FREE(d,b) (d)->ops->free(d,b)
  208. /****************************************************************************
  209. * Public Types
  210. ****************************************************************************/
  211. /* Certain QSPI devices may required different clocking modes */
  212. enum qspi_mode_e
  213. {
  214. QSPIDEV_MODE0 = 0, /* CPOL=0 CHPHA=0 */
  215. QSPIDEV_MODE1, /* CPOL=0 CHPHA=1 */
  216. QSPIDEV_MODE2, /* CPOL=1 CHPHA=0 */
  217. QSPIDEV_MODE3 /* CPOL=1 CHPHA=1 */
  218. };
  219. /* This structure describes one command transfer */
  220. struct qspi_cmdinfo_s
  221. {
  222. uint8_t flags; /* See QSPICMD_* definitions */
  223. uint8_t addrlen; /* Address length in bytes (if QSPICMD_ADDRESS) */
  224. uint16_t cmd; /* Command */
  225. uint16_t buflen; /* Data buffer length in bytes (if QSPICMD_DATA) */
  226. uint32_t addr; /* Address (if QSPICMD_ADDRESS) */
  227. FAR void *buffer; /* Data buffer (if QSPICMD_DATA) */
  228. };
  229. /* This structure describes one memory transfer */
  230. struct qspi_meminfo_s
  231. {
  232. uint8_t flags; /* See QSPMEM_* definitions */
  233. uint8_t addrlen; /* Address length in bytes */
  234. uint8_t dummies; /* Number of dummy read cycles (READ only) */
  235. uint16_t buflen; /* Data buffer length in bytes */
  236. uint16_t cmd; /* Memory access command */
  237. uint32_t addr; /* Memory Address */
  238. uint32_t key; /* Scrambler key */
  239. FAR void *buffer; /* Data buffer */
  240. };
  241. /* The QSPI vtable */
  242. struct qspi_dev_s;
  243. struct qspi_ops_s
  244. {
  245. CODE int (*lock)(FAR struct qspi_dev_s *dev, bool lock);
  246. CODE uint32_t (*setfrequency)(FAR struct qspi_dev_s *dev,
  247. uint32_t frequency);
  248. CODE void (*setmode)(FAR struct qspi_dev_s *dev,
  249. enum qspi_mode_e mode);
  250. CODE void (*setbits)(FAR struct qspi_dev_s *dev, int nbits);
  251. CODE int (*command)(FAR struct qspi_dev_s *dev,
  252. FAR struct qspi_cmdinfo_s *cmdinfo);
  253. CODE int (*memory)(FAR struct qspi_dev_s *dev,
  254. FAR struct qspi_meminfo_s *meminfo);
  255. CODE FAR void *(*alloc)(FAR struct qspi_dev_s *dev, size_t buflen);
  256. CODE void (*free)(FAR struct qspi_dev_s *dev, FAR void *buffer);
  257. };
  258. /* QSPI private data. This structure only defines the initial fields of the
  259. * structure visible to the QSPI client. The specific implementation may
  260. * add additional, device specific fields
  261. */
  262. struct qspi_dev_s
  263. {
  264. FAR const struct qspi_ops_s *ops;
  265. };
  266. /****************************************************************************
  267. * Public Data
  268. ****************************************************************************/
  269. #undef EXTERN
  270. #if defined(__cplusplus)
  271. #define EXTERN extern "C"
  272. extern "C"
  273. {
  274. #else
  275. #define EXTERN extern
  276. #endif
  277. /****************************************************************************
  278. * Public Function Prototypes
  279. ****************************************************************************/
  280. #undef EXTERN
  281. #if defined(__cplusplus)
  282. }
  283. #endif
  284. #endif /* __INCLUDE_NUTTX_QSPI_QSPI_H */