sandbox_spi.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <dm.h>
  12. #include <malloc.h>
  13. #include <spi.h>
  14. #include <spi_flash.h>
  15. #include <os.h>
  16. #include <linux/errno.h>
  17. #include <asm/spi.h>
  18. #include <asm/state.h>
  19. #include <dm/device-internal.h>
  20. #ifndef CONFIG_SPI_IDLE_VAL
  21. # define CONFIG_SPI_IDLE_VAL 0xFF
  22. #endif
  23. const char *sandbox_spi_parse_spec(const char *arg, unsigned long *bus,
  24. unsigned long *cs)
  25. {
  26. char *endp;
  27. *bus = simple_strtoul(arg, &endp, 0);
  28. if (*endp != ':' || *bus >= CONFIG_SANDBOX_SPI_MAX_BUS)
  29. return NULL;
  30. *cs = simple_strtoul(endp + 1, &endp, 0);
  31. if (*endp != ':' || *cs >= CONFIG_SANDBOX_SPI_MAX_CS)
  32. return NULL;
  33. return endp + 1;
  34. }
  35. __weak int sandbox_spi_get_emul(struct sandbox_state *state,
  36. struct udevice *bus, struct udevice *slave,
  37. struct udevice **emulp)
  38. {
  39. return -ENOENT;
  40. }
  41. static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
  42. const void *dout, void *din, unsigned long flags)
  43. {
  44. struct udevice *bus = slave->parent;
  45. struct sandbox_state *state = state_get_current();
  46. struct dm_spi_emul_ops *ops;
  47. struct udevice *emul;
  48. uint bytes = bitlen / 8, i;
  49. int ret;
  50. u8 *tx = (void *)dout, *rx = din;
  51. uint busnum, cs;
  52. if (bitlen == 0)
  53. return 0;
  54. /* we can only do 8 bit transfers */
  55. if (bitlen % 8) {
  56. printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
  57. bitlen);
  58. return -EINVAL;
  59. }
  60. busnum = bus->seq;
  61. cs = spi_chip_select(slave);
  62. if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS ||
  63. cs >= CONFIG_SANDBOX_SPI_MAX_CS) {
  64. printf("%s: busnum=%u, cs=%u: out of range\n", __func__,
  65. busnum, cs);
  66. return -ENOENT;
  67. }
  68. ret = sandbox_spi_get_emul(state, bus, slave, &emul);
  69. if (ret) {
  70. printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n",
  71. __func__, busnum, cs, ret);
  72. return -ENOENT;
  73. }
  74. ret = device_probe(emul);
  75. if (ret)
  76. return ret;
  77. /* make sure rx/tx buffers are full so clients can assume */
  78. if (!tx) {
  79. debug("sandbox_spi: xfer: auto-allocating tx scratch buffer\n");
  80. tx = malloc(bytes);
  81. if (!tx) {
  82. debug("sandbox_spi: Out of memory\n");
  83. return -ENOMEM;
  84. }
  85. }
  86. if (!rx) {
  87. debug("sandbox_spi: xfer: auto-allocating rx scratch buffer\n");
  88. rx = malloc(bytes);
  89. if (!rx) {
  90. debug("sandbox_spi: Out of memory\n");
  91. return -ENOMEM;
  92. }
  93. }
  94. ops = spi_emul_get_ops(emul);
  95. ret = ops->xfer(emul, bitlen, dout, din, flags);
  96. debug("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
  97. ret, ret ? "bad" : "good");
  98. for (i = 0; i < bytes; ++i)
  99. debug(" %u:%02x", i, rx[i]);
  100. debug("\n");
  101. if (tx != dout)
  102. free(tx);
  103. if (rx != din)
  104. free(rx);
  105. return ret;
  106. }
  107. static int sandbox_spi_set_speed(struct udevice *bus, uint speed)
  108. {
  109. return 0;
  110. }
  111. static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
  112. {
  113. return 0;
  114. }
  115. static int sandbox_cs_info(struct udevice *bus, uint cs,
  116. struct spi_cs_info *info)
  117. {
  118. /* Always allow activity on CS 0 */
  119. if (cs >= 1)
  120. return -ENODEV;
  121. return 0;
  122. }
  123. static const struct dm_spi_ops sandbox_spi_ops = {
  124. .xfer = sandbox_spi_xfer,
  125. .set_speed = sandbox_spi_set_speed,
  126. .set_mode = sandbox_spi_set_mode,
  127. .cs_info = sandbox_cs_info,
  128. };
  129. static const struct udevice_id sandbox_spi_ids[] = {
  130. { .compatible = "sandbox,spi" },
  131. { }
  132. };
  133. U_BOOT_DRIVER(spi_sandbox) = {
  134. .name = "spi_sandbox",
  135. .id = UCLASS_SPI,
  136. .of_match = sandbox_spi_ids,
  137. .ops = &sandbox_spi_ops,
  138. };