sandbox_spi.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 <log.h>
  14. #include <malloc.h>
  15. #include <spi.h>
  16. #include <spi_flash.h>
  17. #include <os.h>
  18. #include <linux/errno.h>
  19. #include <asm/spi.h>
  20. #include <asm/state.h>
  21. #include <dm/acpi.h>
  22. #include <dm/device-internal.h>
  23. #ifndef CONFIG_SPI_IDLE_VAL
  24. # define CONFIG_SPI_IDLE_VAL 0xFF
  25. #endif
  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. __weak int sandbox_spi_get_emul(struct sandbox_state *state,
  39. struct udevice *bus, struct udevice *slave,
  40. struct udevice **emulp)
  41. {
  42. return -ENOENT;
  43. }
  44. static int sandbox_spi_xfer(struct udevice *slave, unsigned int bitlen,
  45. const void *dout, void *din, unsigned long flags)
  46. {
  47. struct udevice *bus = slave->parent;
  48. struct sandbox_state *state = state_get_current();
  49. struct dm_spi_emul_ops *ops;
  50. struct udevice *emul;
  51. uint bytes = bitlen / 8, i;
  52. int ret;
  53. uint busnum, cs;
  54. if (bitlen == 0)
  55. return 0;
  56. /* we can only do 8 bit transfers */
  57. if (bitlen % 8) {
  58. printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
  59. bitlen);
  60. return -EINVAL;
  61. }
  62. busnum = dev_seq(bus);
  63. cs = spi_chip_select(slave);
  64. if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS ||
  65. cs >= CONFIG_SANDBOX_SPI_MAX_CS) {
  66. printf("%s: busnum=%u, cs=%u: out of range\n", __func__,
  67. busnum, cs);
  68. return -ENOENT;
  69. }
  70. ret = sandbox_spi_get_emul(state, bus, slave, &emul);
  71. if (ret) {
  72. printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n",
  73. __func__, busnum, cs, ret);
  74. return -ENOENT;
  75. }
  76. ret = device_probe(emul);
  77. if (ret)
  78. return ret;
  79. ops = spi_emul_get_ops(emul);
  80. ret = ops->xfer(emul, bitlen, dout, din, flags);
  81. log_content("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
  82. ret, ret ? "bad" : "good");
  83. if (din) {
  84. for (i = 0; i < bytes; ++i)
  85. log_content(" %u:%02x", i, ((u8 *)din)[i]);
  86. }
  87. log_content("\n");
  88. return ret;
  89. }
  90. static int sandbox_spi_set_speed(struct udevice *bus, uint speed)
  91. {
  92. return 0;
  93. }
  94. static int sandbox_spi_set_mode(struct udevice *bus, uint mode)
  95. {
  96. return 0;
  97. }
  98. static int sandbox_cs_info(struct udevice *bus, uint cs,
  99. struct spi_cs_info *info)
  100. {
  101. /* Always allow activity on CS 0 */
  102. if (cs >= 1)
  103. return -EINVAL;
  104. return 0;
  105. }
  106. static int sandbox_spi_get_mmap(struct udevice *dev, ulong *map_basep,
  107. uint *map_sizep, uint *offsetp)
  108. {
  109. *map_basep = 0x1000;
  110. *map_sizep = 0x2000;
  111. *offsetp = 0x100;
  112. return 0;
  113. }
  114. static const struct dm_spi_ops sandbox_spi_ops = {
  115. .xfer = sandbox_spi_xfer,
  116. .set_speed = sandbox_spi_set_speed,
  117. .set_mode = sandbox_spi_set_mode,
  118. .cs_info = sandbox_cs_info,
  119. .get_mmap = sandbox_spi_get_mmap,
  120. };
  121. static const struct udevice_id sandbox_spi_ids[] = {
  122. { .compatible = "sandbox,spi" },
  123. { }
  124. };
  125. U_BOOT_DRIVER(sandbox_spi) = {
  126. .name = "sandbox_spi",
  127. .id = UCLASS_SPI,
  128. .of_match = sandbox_spi_ids,
  129. .ops = &sandbox_spi_ops,
  130. };