cros_ec_spi.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Chromium OS cros_ec driver - SPI interface
  4. *
  5. * Copyright (c) 2012 The Chromium OS Authors.
  6. */
  7. /*
  8. * The Matrix Keyboard Protocol driver handles talking to the keyboard
  9. * controller chip. Mostly this is for keyboard functions, but some other
  10. * things have slipped in, so we provide generic services to talk to the
  11. * KBC.
  12. */
  13. #include <common.h>
  14. #include <cros_ec.h>
  15. #include <dm.h>
  16. #include <errno.h>
  17. #include <spi.h>
  18. int cros_ec_spi_packet(struct udevice *udev, int out_bytes, int in_bytes)
  19. {
  20. struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
  21. struct spi_slave *slave = dev_get_parent_priv(dev->dev);
  22. ulong start;
  23. uint8_t byte;
  24. int rv;
  25. /* Do the transfer */
  26. if (spi_claim_bus(slave)) {
  27. debug("%s: Cannot claim SPI bus\n", __func__);
  28. return -1;
  29. }
  30. rv = spi_xfer(slave, out_bytes * 8, dev->dout, NULL, SPI_XFER_BEGIN);
  31. if (rv)
  32. goto done;
  33. start = get_timer(0);
  34. while (1) {
  35. rv = spi_xfer(slave, 8, NULL, &byte, 0);
  36. if (byte == SPI_PREAMBLE_END_BYTE)
  37. break;
  38. if (rv)
  39. goto done;
  40. if (get_timer(start) > 100) {
  41. rv = -ETIMEDOUT;
  42. goto done;
  43. }
  44. }
  45. rv = spi_xfer(slave, in_bytes * 8, NULL, dev->din, 0);
  46. done:
  47. spi_xfer(slave, 0, NULL, NULL, SPI_XFER_END);
  48. spi_release_bus(slave);
  49. if (rv) {
  50. debug("%s: Cannot complete SPI transfer\n", __func__);
  51. return -1;
  52. }
  53. return in_bytes;
  54. }
  55. /**
  56. * Send a command to a LPC CROS_EC device and return the reply.
  57. *
  58. * The device's internal input/output buffers are used.
  59. *
  60. * @param dev CROS_EC device
  61. * @param cmd Command to send (EC_CMD_...)
  62. * @param cmd_version Version of command to send (EC_VER_...)
  63. * @param dout Output data (may be NULL If dout_len=0)
  64. * @param dout_len Size of output data in bytes
  65. * @param dinp Returns pointer to response data. This will be
  66. * untouched unless we return a value > 0.
  67. * @param din_len Maximum size of response in bytes
  68. * @return number of bytes in response, or -1 on error
  69. */
  70. int cros_ec_spi_command(struct udevice *udev, uint8_t cmd, int cmd_version,
  71. const uint8_t *dout, int dout_len,
  72. uint8_t **dinp, int din_len)
  73. {
  74. struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
  75. struct spi_slave *slave = dev_get_parent_priv(dev->dev);
  76. int in_bytes = din_len + 4; /* status, length, checksum, trailer */
  77. uint8_t *out;
  78. uint8_t *p;
  79. int csum, len;
  80. int rv;
  81. if (dev->protocol_version != 2) {
  82. debug("%s: Unsupported EC protcol version %d\n",
  83. __func__, dev->protocol_version);
  84. return -1;
  85. }
  86. /*
  87. * Sanity-check input size to make sure it plus transaction overhead
  88. * fits in the internal device buffer.
  89. */
  90. if (in_bytes > sizeof(dev->din)) {
  91. debug("%s: Cannot receive %d bytes\n", __func__, din_len);
  92. return -1;
  93. }
  94. /* We represent message length as a byte */
  95. if (dout_len > 0xff) {
  96. debug("%s: Cannot send %d bytes\n", __func__, dout_len);
  97. return -1;
  98. }
  99. /*
  100. * Clear input buffer so we don't get false hits for MSG_HEADER
  101. */
  102. memset(dev->din, '\0', in_bytes);
  103. if (spi_claim_bus(slave)) {
  104. debug("%s: Cannot claim SPI bus\n", __func__);
  105. return -1;
  106. }
  107. out = dev->dout;
  108. out[0] = EC_CMD_VERSION0 + cmd_version;
  109. out[1] = cmd;
  110. out[2] = (uint8_t)dout_len;
  111. memcpy(out + 3, dout, dout_len);
  112. csum = cros_ec_calc_checksum(out, 3)
  113. + cros_ec_calc_checksum(dout, dout_len);
  114. out[3 + dout_len] = (uint8_t)csum;
  115. /*
  116. * Send output data and receive input data starting such that the
  117. * message body will be dword aligned.
  118. */
  119. p = dev->din + sizeof(int64_t) - 2;
  120. len = dout_len + 4;
  121. cros_ec_dump_data("out", cmd, out, len);
  122. rv = spi_xfer(slave, max(len, in_bytes) * 8, out, p,
  123. SPI_XFER_BEGIN | SPI_XFER_END);
  124. spi_release_bus(slave);
  125. if (rv) {
  126. debug("%s: Cannot complete SPI transfer\n", __func__);
  127. return -1;
  128. }
  129. len = min((int)p[1], din_len);
  130. cros_ec_dump_data("in", -1, p, len + 3);
  131. /* Response code is first byte of message */
  132. if (p[0] != EC_RES_SUCCESS) {
  133. printf("%s: Returned status %d\n", __func__, p[0]);
  134. return -(int)(p[0]);
  135. }
  136. /* Check checksum */
  137. csum = cros_ec_calc_checksum(p, len + 2);
  138. if (csum != p[len + 2]) {
  139. debug("%s: Invalid checksum rx %#02x, calced %#02x\n", __func__,
  140. p[2 + len], csum);
  141. return -1;
  142. }
  143. /* Anything else is the response data */
  144. *dinp = p + 2;
  145. return len;
  146. }
  147. static int cros_ec_probe(struct udevice *dev)
  148. {
  149. return cros_ec_register(dev);
  150. }
  151. static struct dm_cros_ec_ops cros_ec_ops = {
  152. .packet = cros_ec_spi_packet,
  153. .command = cros_ec_spi_command,
  154. };
  155. static const struct udevice_id cros_ec_ids[] = {
  156. { .compatible = "google,cros-ec-spi" },
  157. { }
  158. };
  159. U_BOOT_DRIVER(cros_ec_spi) = {
  160. .name = "cros_ec_spi",
  161. .id = UCLASS_CROS_EC,
  162. .of_match = cros_ec_ids,
  163. .probe = cros_ec_probe,
  164. .ops = &cros_ec_ops,
  165. };