spi_mem.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (c) 2019-2022, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <inttypes.h>
  8. #include <stdint.h>
  9. #include <drivers/spi_mem.h>
  10. #include <lib/utils_def.h>
  11. #include <libfdt.h>
  12. #define SPI_MEM_DEFAULT_SPEED_HZ 100000U
  13. /*
  14. * struct spi_slave - Representation of a SPI slave.
  15. *
  16. * @max_hz: Maximum speed for this slave in Hertz.
  17. * @cs: ID of the chip select connected to the slave.
  18. * @mode: SPI mode to use for this slave (see SPI mode flags).
  19. * @ops: Ops defined by the bus.
  20. */
  21. struct spi_slave {
  22. unsigned int max_hz;
  23. unsigned int cs;
  24. unsigned int mode;
  25. const struct spi_bus_ops *ops;
  26. };
  27. static struct spi_slave spi_slave;
  28. static bool spi_mem_check_buswidth_req(uint8_t buswidth, bool tx)
  29. {
  30. switch (buswidth) {
  31. case 1U:
  32. return true;
  33. case 2U:
  34. if ((tx && (spi_slave.mode & (SPI_TX_DUAL | SPI_TX_QUAD)) !=
  35. 0U) ||
  36. (!tx && (spi_slave.mode & (SPI_RX_DUAL | SPI_RX_QUAD)) !=
  37. 0U)) {
  38. return true;
  39. }
  40. break;
  41. case 4U:
  42. if ((tx && (spi_slave.mode & SPI_TX_QUAD) != 0U) ||
  43. (!tx && (spi_slave.mode & SPI_RX_QUAD) != 0U)) {
  44. return true;
  45. }
  46. break;
  47. default:
  48. break;
  49. }
  50. return false;
  51. }
  52. static bool spi_mem_supports_op(const struct spi_mem_op *op)
  53. {
  54. if (!spi_mem_check_buswidth_req(op->cmd.buswidth, true)) {
  55. return false;
  56. }
  57. if ((op->addr.nbytes != 0U) &&
  58. !spi_mem_check_buswidth_req(op->addr.buswidth, true)) {
  59. return false;
  60. }
  61. if ((op->dummy.nbytes != 0U) &&
  62. !spi_mem_check_buswidth_req(op->dummy.buswidth, true)) {
  63. return false;
  64. }
  65. if ((op->data.nbytes != 0U) &&
  66. !spi_mem_check_buswidth_req(op->data.buswidth,
  67. op->data.dir == SPI_MEM_DATA_OUT)) {
  68. return false;
  69. }
  70. return true;
  71. }
  72. static int spi_mem_set_speed_mode(void)
  73. {
  74. const struct spi_bus_ops *ops = spi_slave.ops;
  75. int ret;
  76. ret = ops->set_speed(spi_slave.max_hz);
  77. if (ret != 0) {
  78. VERBOSE("Cannot set speed (err=%d)\n", ret);
  79. return ret;
  80. }
  81. ret = ops->set_mode(spi_slave.mode);
  82. if (ret != 0) {
  83. VERBOSE("Cannot set mode (err=%d)\n", ret);
  84. return ret;
  85. }
  86. return 0;
  87. }
  88. static int spi_mem_check_bus_ops(const struct spi_bus_ops *ops)
  89. {
  90. bool error = false;
  91. if (ops->claim_bus == NULL) {
  92. VERBOSE("Ops claim bus is not defined\n");
  93. error = true;
  94. }
  95. if (ops->release_bus == NULL) {
  96. VERBOSE("Ops release bus is not defined\n");
  97. error = true;
  98. }
  99. if (ops->exec_op == NULL) {
  100. VERBOSE("Ops exec op is not defined\n");
  101. error = true;
  102. }
  103. if (ops->set_speed == NULL) {
  104. VERBOSE("Ops set speed is not defined\n");
  105. error = true;
  106. }
  107. if (ops->set_mode == NULL) {
  108. VERBOSE("Ops set mode is not defined\n");
  109. error = true;
  110. }
  111. return error ? -EINVAL : 0;
  112. }
  113. /*
  114. * spi_mem_exec_op() - Execute a memory operation.
  115. * @op: The memory operation to execute.
  116. *
  117. * This function first checks that @op is supported and then tries to execute
  118. * it.
  119. *
  120. * Return: 0 in case of success, a negative error code otherwise.
  121. */
  122. int spi_mem_exec_op(const struct spi_mem_op *op)
  123. {
  124. const struct spi_bus_ops *ops = spi_slave.ops;
  125. int ret;
  126. VERBOSE("%s: cmd:%x mode:%d.%d.%d.%d addqr:%" PRIx64 " len:%x\n",
  127. __func__, op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,
  128. op->dummy.buswidth, op->data.buswidth,
  129. op->addr.val, op->data.nbytes);
  130. if (!spi_mem_supports_op(op)) {
  131. WARN("Error in spi_mem_support\n");
  132. return -ENOTSUP;
  133. }
  134. ret = ops->claim_bus(spi_slave.cs);
  135. if (ret != 0) {
  136. WARN("Error claim_bus\n");
  137. return ret;
  138. }
  139. ret = ops->exec_op(op);
  140. ops->release_bus();
  141. return ret;
  142. }
  143. /*
  144. * spi_mem_init_slave() - SPI slave device initialization.
  145. * @fdt: Pointer to the device tree blob.
  146. * @bus_node: Offset of the bus node.
  147. * @ops: The SPI bus ops defined.
  148. *
  149. * This function first checks that @ops are supported and then tries to find
  150. * a SPI slave device.
  151. *
  152. * Return: 0 in case of success, a negative error code otherwise.
  153. */
  154. int spi_mem_init_slave(void *fdt, int bus_node, const struct spi_bus_ops *ops)
  155. {
  156. int ret;
  157. int mode = 0;
  158. int nchips = 0;
  159. int bus_subnode = 0;
  160. const fdt32_t *cuint = NULL;
  161. ret = spi_mem_check_bus_ops(ops);
  162. if (ret != 0) {
  163. return ret;
  164. }
  165. fdt_for_each_subnode(bus_subnode, fdt, bus_node) {
  166. nchips++;
  167. }
  168. if (nchips != 1) {
  169. ERROR("Only one SPI device is currently supported\n");
  170. return -EINVAL;
  171. }
  172. fdt_for_each_subnode(bus_subnode, fdt, bus_node) {
  173. /* Get chip select */
  174. cuint = fdt_getprop(fdt, bus_subnode, "reg", NULL);
  175. if (cuint == NULL) {
  176. ERROR("Chip select not well defined\n");
  177. return -EINVAL;
  178. }
  179. spi_slave.cs = fdt32_to_cpu(*cuint);
  180. /* Get max slave frequency */
  181. spi_slave.max_hz = SPI_MEM_DEFAULT_SPEED_HZ;
  182. cuint = fdt_getprop(fdt, bus_subnode,
  183. "spi-max-frequency", NULL);
  184. if (cuint != NULL) {
  185. spi_slave.max_hz = fdt32_to_cpu(*cuint);
  186. }
  187. /* Get mode */
  188. if ((fdt_getprop(fdt, bus_subnode, "spi-cpol", NULL)) != NULL) {
  189. mode |= SPI_CPOL;
  190. }
  191. if ((fdt_getprop(fdt, bus_subnode, "spi-cpha", NULL)) != NULL) {
  192. mode |= SPI_CPHA;
  193. }
  194. if ((fdt_getprop(fdt, bus_subnode, "spi-cs-high", NULL)) !=
  195. NULL) {
  196. mode |= SPI_CS_HIGH;
  197. }
  198. if ((fdt_getprop(fdt, bus_subnode, "spi-3wire", NULL)) !=
  199. NULL) {
  200. mode |= SPI_3WIRE;
  201. }
  202. if ((fdt_getprop(fdt, bus_subnode, "spi-half-duplex", NULL)) !=
  203. NULL) {
  204. mode |= SPI_PREAMBLE;
  205. }
  206. /* Get dual/quad mode */
  207. cuint = fdt_getprop(fdt, bus_subnode, "spi-tx-bus-width", NULL);
  208. if (cuint != NULL) {
  209. switch (fdt32_to_cpu(*cuint)) {
  210. case 1U:
  211. break;
  212. case 2U:
  213. mode |= SPI_TX_DUAL;
  214. break;
  215. case 4U:
  216. mode |= SPI_TX_QUAD;
  217. break;
  218. default:
  219. WARN("spi-tx-bus-width %u not supported\n",
  220. fdt32_to_cpu(*cuint));
  221. return -EINVAL;
  222. }
  223. }
  224. cuint = fdt_getprop(fdt, bus_subnode, "spi-rx-bus-width", NULL);
  225. if (cuint != NULL) {
  226. switch (fdt32_to_cpu(*cuint)) {
  227. case 1U:
  228. break;
  229. case 2U:
  230. mode |= SPI_RX_DUAL;
  231. break;
  232. case 4U:
  233. mode |= SPI_RX_QUAD;
  234. break;
  235. default:
  236. WARN("spi-rx-bus-width %u not supported\n",
  237. fdt32_to_cpu(*cuint));
  238. return -EINVAL;
  239. }
  240. }
  241. spi_slave.mode = mode;
  242. spi_slave.ops = ops;
  243. }
  244. return spi_mem_set_speed_mode();
  245. }