cros_ec_spi.c 4.6 KB

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