spi_mem.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2019, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef DRIVERS_SPI_MEM_H
  7. #define DRIVERS_SPI_MEM_H
  8. #include <errno.h>
  9. #include <stdbool.h>
  10. #include <stdint.h>
  11. #define SPI_MEM_BUSWIDTH_1_LINE 1U
  12. #define SPI_MEM_BUSWIDTH_2_LINE 2U
  13. #define SPI_MEM_BUSWIDTH_4_LINE 4U
  14. /*
  15. * enum spi_mem_data_dir - Describes the direction of a SPI memory data
  16. * transfer from the controller perspective.
  17. * @SPI_MEM_DATA_IN: data coming from the SPI memory.
  18. * @SPI_MEM_DATA_OUT: data sent to the SPI memory.
  19. */
  20. enum spi_mem_data_dir {
  21. SPI_MEM_DATA_IN,
  22. SPI_MEM_DATA_OUT,
  23. };
  24. /*
  25. * struct spi_mem_op - Describes a SPI memory operation.
  26. *
  27. * @cmd.buswidth: Number of IO lines used to transmit the command.
  28. * @cmd.opcode: Operation opcode.
  29. * @addr.nbytes: Number of address bytes to send. Can be zero if the operation
  30. * does not need to send an address.
  31. * @addr.buswidth: Number of IO lines used to transmit the address.
  32. * @addr.val: Address value. This value is always sent MSB first on the bus.
  33. * Note that only @addr.nbytes are taken into account in this
  34. * address value, so users should make sure the value fits in the
  35. * assigned number of bytes.
  36. * @dummy.nbytes: Number of dummy bytes to send after an opcode or address. Can
  37. * be zero if the operation does not require dummy bytes.
  38. * @dummy.buswidth: Number of IO lines used to transmit the dummy bytes.
  39. * @data.buswidth: Number of IO lines used to send/receive the data.
  40. * @data.dir: Direction of the transfer.
  41. * @data.nbytes: Number of data bytes to transfer.
  42. * @data.buf: Input or output data buffer depending on data::dir.
  43. */
  44. struct spi_mem_op {
  45. struct {
  46. uint8_t buswidth;
  47. uint8_t opcode;
  48. } cmd;
  49. struct {
  50. uint8_t nbytes;
  51. uint8_t buswidth;
  52. uint64_t val;
  53. } addr;
  54. struct {
  55. uint8_t nbytes;
  56. uint8_t buswidth;
  57. } dummy;
  58. struct {
  59. uint8_t buswidth;
  60. enum spi_mem_data_dir dir;
  61. unsigned int nbytes;
  62. void *buf;
  63. } data;
  64. };
  65. /* SPI mode flags */
  66. #define SPI_CPHA BIT(0) /* clock phase */
  67. #define SPI_CPOL BIT(1) /* clock polarity */
  68. #define SPI_CS_HIGH BIT(2) /* CS active high */
  69. #define SPI_LSB_FIRST BIT(3) /* per-word bits-on-wire */
  70. #define SPI_3WIRE BIT(4) /* SI/SO signals shared */
  71. #define SPI_PREAMBLE BIT(5) /* Skip preamble bytes */
  72. #define SPI_TX_DUAL BIT(6) /* transmit with 2 wires */
  73. #define SPI_TX_QUAD BIT(7) /* transmit with 4 wires */
  74. #define SPI_RX_DUAL BIT(8) /* receive with 2 wires */
  75. #define SPI_RX_QUAD BIT(9) /* receive with 4 wires */
  76. struct spi_bus_ops {
  77. /*
  78. * Claim the bus and prepare it for communication.
  79. *
  80. * @cs: The chip select.
  81. * Returns: 0 if the bus was claimed successfully, or a negative value
  82. * if it wasn't.
  83. */
  84. int (*claim_bus)(unsigned int cs);
  85. /*
  86. * Release the SPI bus.
  87. */
  88. void (*release_bus)(void);
  89. /*
  90. * Set transfer speed.
  91. *
  92. * @hz: The transfer speed in Hertz.
  93. * Returns: 0 on success, a negative error code otherwise.
  94. */
  95. int (*set_speed)(unsigned int hz);
  96. /*
  97. * Set the SPI mode/flags.
  98. *
  99. * @mode: Requested SPI mode (SPI_... flags).
  100. * Returns: 0 on success, a negative error code otherwise.
  101. */
  102. int (*set_mode)(unsigned int mode);
  103. /*
  104. * Execute a SPI memory operation.
  105. *
  106. * @op: The memory operation to execute.
  107. * Returns: 0 on success, a negative error code otherwise.
  108. */
  109. int (*exec_op)(const struct spi_mem_op *op);
  110. };
  111. int spi_mem_exec_op(const struct spi_mem_op *op);
  112. int spi_mem_init_slave(void *fdt, int bus_node,
  113. const struct spi_bus_ops *ops);
  114. #endif /* DRIVERS_SPI_MEM_H */