sandbox_spi.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Simulate a SPI port
  3. *
  4. * Copyright (c) 2011-2013 The Chromium OS Authors.
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * Licensed under the GPL-2 or later.
  9. */
  10. #include <common.h>
  11. #include <malloc.h>
  12. #include <spi.h>
  13. #include <os.h>
  14. #include <asm/errno.h>
  15. #include <asm/spi.h>
  16. #include <asm/state.h>
  17. #ifndef CONFIG_SPI_IDLE_VAL
  18. # define CONFIG_SPI_IDLE_VAL 0xFF
  19. #endif
  20. struct sandbox_spi_slave {
  21. struct spi_slave slave;
  22. const struct sandbox_spi_emu_ops *ops;
  23. void *priv;
  24. };
  25. #define to_sandbox_spi_slave(s) container_of(s, struct sandbox_spi_slave, slave)
  26. const char *sandbox_spi_parse_spec(const char *arg, unsigned long *bus,
  27. unsigned long *cs)
  28. {
  29. char *endp;
  30. *bus = simple_strtoul(arg, &endp, 0);
  31. if (*endp != ':' || *bus >= CONFIG_SANDBOX_SPI_MAX_BUS)
  32. return NULL;
  33. *cs = simple_strtoul(endp + 1, &endp, 0);
  34. if (*endp != ':' || *cs >= CONFIG_SANDBOX_SPI_MAX_CS)
  35. return NULL;
  36. return endp + 1;
  37. }
  38. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  39. {
  40. return bus < CONFIG_SANDBOX_SPI_MAX_BUS &&
  41. cs < CONFIG_SANDBOX_SPI_MAX_CS;
  42. }
  43. void spi_cs_activate(struct spi_slave *slave)
  44. {
  45. struct sandbox_spi_slave *sss = to_sandbox_spi_slave(slave);
  46. debug("sandbox_spi: activating CS\n");
  47. if (sss->ops->cs_activate)
  48. sss->ops->cs_activate(sss->priv);
  49. }
  50. void spi_cs_deactivate(struct spi_slave *slave)
  51. {
  52. struct sandbox_spi_slave *sss = to_sandbox_spi_slave(slave);
  53. debug("sandbox_spi: deactivating CS\n");
  54. if (sss->ops->cs_deactivate)
  55. sss->ops->cs_deactivate(sss->priv);
  56. }
  57. void spi_init(void)
  58. {
  59. }
  60. void spi_set_speed(struct spi_slave *slave, uint hz)
  61. {
  62. }
  63. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  64. unsigned int max_hz, unsigned int mode)
  65. {
  66. struct sandbox_spi_slave *sss;
  67. struct sandbox_state *state = state_get_current();
  68. const char *spec;
  69. if (!spi_cs_is_valid(bus, cs)) {
  70. debug("sandbox_spi: Invalid SPI bus/cs\n");
  71. return NULL;
  72. }
  73. sss = spi_alloc_slave(struct sandbox_spi_slave, bus, cs);
  74. if (!sss) {
  75. debug("sandbox_spi: Out of memory\n");
  76. return NULL;
  77. }
  78. spec = state->spi[bus][cs].spec;
  79. sss->ops = state->spi[bus][cs].ops;
  80. if (!spec || !sss->ops || sss->ops->setup(&sss->priv, spec)) {
  81. free(sss);
  82. printf("sandbox_spi: unable to locate a slave client\n");
  83. return NULL;
  84. }
  85. return &sss->slave;
  86. }
  87. void spi_free_slave(struct spi_slave *slave)
  88. {
  89. struct sandbox_spi_slave *sss = to_sandbox_spi_slave(slave);
  90. debug("sandbox_spi: releasing slave\n");
  91. if (sss->ops->free)
  92. sss->ops->free(sss->priv);
  93. free(sss);
  94. }
  95. static int spi_bus_claim_cnt[CONFIG_SANDBOX_SPI_MAX_BUS];
  96. int spi_claim_bus(struct spi_slave *slave)
  97. {
  98. if (spi_bus_claim_cnt[slave->bus]++) {
  99. printf("sandbox_spi: error: bus already claimed: %d!\n",
  100. spi_bus_claim_cnt[slave->bus]);
  101. }
  102. return 0;
  103. }
  104. void spi_release_bus(struct spi_slave *slave)
  105. {
  106. if (--spi_bus_claim_cnt[slave->bus]) {
  107. printf("sandbox_spi: error: bus freed too often: %d!\n",
  108. spi_bus_claim_cnt[slave->bus]);
  109. }
  110. }
  111. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  112. void *din, unsigned long flags)
  113. {
  114. struct sandbox_spi_slave *sss = to_sandbox_spi_slave(slave);
  115. uint bytes = bitlen / 8, i;
  116. int ret = 0;
  117. u8 *tx = (void *)dout, *rx = din;
  118. if (bitlen == 0)
  119. goto done;
  120. /* we can only do 8 bit transfers */
  121. if (bitlen % 8) {
  122. printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
  123. bitlen);
  124. flags |= SPI_XFER_END;
  125. goto done;
  126. }
  127. if (flags & SPI_XFER_BEGIN)
  128. spi_cs_activate(slave);
  129. /* make sure rx/tx buffers are full so clients can assume */
  130. if (!tx) {
  131. debug("sandbox_spi: xfer: auto-allocating tx scratch buffer\n");
  132. tx = malloc(bytes);
  133. if (!tx) {
  134. debug("sandbox_spi: Out of memory\n");
  135. return -ENOMEM;
  136. }
  137. }
  138. if (!rx) {
  139. debug("sandbox_spi: xfer: auto-allocating rx scratch buffer\n");
  140. rx = malloc(bytes);
  141. if (!rx) {
  142. debug("sandbox_spi: Out of memory\n");
  143. return -ENOMEM;
  144. }
  145. }
  146. debug("sandbox_spi: xfer: bytes = %u\n tx:", bytes);
  147. for (i = 0; i < bytes; ++i)
  148. debug(" %u:%02x", i, tx[i]);
  149. debug("\n");
  150. ret = sss->ops->xfer(sss->priv, tx, rx, bytes);
  151. debug("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
  152. ret, ret ? "bad" : "good");
  153. for (i = 0; i < bytes; ++i)
  154. debug(" %u:%02x", i, rx[i]);
  155. debug("\n");
  156. if (tx != dout)
  157. free(tx);
  158. if (rx != din)
  159. free(rx);
  160. done:
  161. if (flags & SPI_XFER_END)
  162. spi_cs_deactivate(slave);
  163. return ret;
  164. }
  165. /**
  166. * Set up a new SPI slave for an fdt node
  167. *
  168. * @param blob Device tree blob
  169. * @param node SPI peripheral node to use
  170. * @return 0 if ok, -1 on error
  171. */
  172. struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
  173. int spi_node)
  174. {
  175. return NULL;
  176. }