cros_ec_spi.c 4.7 KB

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