sandbox_spi.c 3.5 KB

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