sandbox_spi.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #define LOG_CATEGORY UCLASS_SPI
  11. #include <common.h>
  12. #include <dm.h>
  13. #include <malloc.h>
  14. #include <spi.h>
  15. #include <spi_flash.h>
  16. #include <os.h>
  17. #include <linux/errno.h>
  18. #include <asm/spi.h>
  19. #include <asm/state.h>
  20. #include <dm/device-internal.h>
  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. 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. ops = spi_emul_get_ops(emul);
  78. ret = ops->xfer(emul, bitlen, dout, din, flags);
  79. log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
  80. ret, ret ? "bad" : "good");
  81. if (din) {
  82. for (i = 0; i < bytes; ++i)
  83. log_content(" %u:%02x", i, ((u8 *)din)[i]);
  84. }
  85. log_content("\n");
  86. return ret;
  87. }
  88. static int sandbox_spi_set_speed(struct udevice *bus, uint speed)
  89. {
  90. return 0;
  91. }
  92. static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
  93. {
  94. return 0;
  95. }
  96. static int sandbox_cs_info(struct udevice *bus, uint cs,
  97. struct spi_cs_info *info)
  98. {
  99. /* Always allow activity on CS 0 */
  100. if (cs >= 1)
  101. return -ENODEV;
  102. return 0;
  103. }
  104. static const struct dm_spi_ops sandbox_spi_ops = {
  105. .xfer = sandbox_spi_xfer,
  106. .set_speed = sandbox_spi_set_speed,
  107. .set_mode = sandbox_spi_set_mode,
  108. .cs_info = sandbox_cs_info,
  109. };
  110. static const struct udevice_id sandbox_spi_ids[] = {
  111. { .compatible = "sandbox,spi" },
  112. { }
  113. };
  114. U_BOOT_DRIVER(spi_sandbox) = {
  115. .name = "spi_sandbox",
  116. .id = UCLASS_SPI,
  117. .of_match = sandbox_spi_ids,
  118. .ops = &sandbox_spi_ops,
  119. };