cros_ec_spi.c 4.3 KB

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