cros_ec_spi.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Chromium OS cros_ec driver - SPI interface
  3. *
  4. * Copyright (c) 2012 The Chromium OS Authors.
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * The Matrix Keyboard Protocol driver handles talking to the keyboard
  25. * controller chip. Mostly this is for keyboard functions, but some other
  26. * things have slipped in, so we provide generic services to talk to the
  27. * KBC.
  28. */
  29. #include <common.h>
  30. #include <cros_ec.h>
  31. #include <spi.h>
  32. /**
  33. * Send a command to a LPC CROS_EC device and return the reply.
  34. *
  35. * The device's internal input/output buffers are used.
  36. *
  37. * @param dev CROS_EC device
  38. * @param cmd Command to send (EC_CMD_...)
  39. * @param cmd_version Version of command to send (EC_VER_...)
  40. * @param dout Output data (may be NULL If dout_len=0)
  41. * @param dout_len Size of output data in bytes
  42. * @param dinp Returns pointer to response data. This will be
  43. * untouched unless we return a value > 0.
  44. * @param din_len Maximum size of response in bytes
  45. * @return number of bytes in response, or -1 on error
  46. */
  47. int cros_ec_spi_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
  48. const uint8_t *dout, int dout_len,
  49. uint8_t **dinp, int din_len)
  50. {
  51. int in_bytes = din_len + 4; /* status, length, checksum, trailer */
  52. uint8_t *out;
  53. uint8_t *p;
  54. int csum, len;
  55. int rv;
  56. /*
  57. * Sanity-check input size to make sure it plus transaction overhead
  58. * fits in the internal device buffer.
  59. */
  60. if (in_bytes > sizeof(dev->din)) {
  61. debug("%s: Cannot receive %d bytes\n", __func__, din_len);
  62. return -1;
  63. }
  64. /* We represent message length as a byte */
  65. if (dout_len > 0xff) {
  66. debug("%s: Cannot send %d bytes\n", __func__, dout_len);
  67. return -1;
  68. }
  69. /*
  70. * Clear input buffer so we don't get false hits for MSG_HEADER
  71. */
  72. memset(dev->din, '\0', in_bytes);
  73. if (spi_claim_bus(dev->spi)) {
  74. debug("%s: Cannot claim SPI bus\n", __func__);
  75. return -1;
  76. }
  77. out = dev->dout;
  78. out[0] = cmd_version;
  79. out[1] = cmd;
  80. out[2] = (uint8_t)dout_len;
  81. memcpy(out + 3, dout, dout_len);
  82. csum = cros_ec_calc_checksum(out, 3)
  83. + cros_ec_calc_checksum(dout, dout_len);
  84. out[3 + dout_len] = (uint8_t)csum;
  85. /*
  86. * Send output data and receive input data starting such that the
  87. * message body will be dword aligned.
  88. */
  89. p = dev->din + sizeof(int64_t) - 2;
  90. len = dout_len + 4;
  91. cros_ec_dump_data("out", cmd, out, len);
  92. rv = spi_xfer(dev->spi, max(len, in_bytes) * 8, out, p,
  93. SPI_XFER_BEGIN | SPI_XFER_END);
  94. spi_release_bus(dev->spi);
  95. if (rv) {
  96. debug("%s: Cannot complete SPI transfer\n", __func__);
  97. return -1;
  98. }
  99. len = min(p[1], din_len);
  100. cros_ec_dump_data("in", -1, p, len + 3);
  101. /* Response code is first byte of message */
  102. if (p[0] != EC_RES_SUCCESS) {
  103. printf("%s: Returned status %d\n", __func__, p[0]);
  104. return -(int)(p[0]);
  105. }
  106. /* Check checksum */
  107. csum = cros_ec_calc_checksum(p, len + 2);
  108. if (csum != p[len + 2]) {
  109. debug("%s: Invalid checksum rx %#02x, calced %#02x\n", __func__,
  110. p[2 + len], csum);
  111. return -1;
  112. }
  113. /* Anything else is the response data */
  114. *dinp = p + 2;
  115. return len;
  116. }
  117. int cros_ec_spi_decode_fdt(struct cros_ec_dev *dev, const void *blob)
  118. {
  119. /* Decode interface-specific FDT params */
  120. dev->max_frequency = fdtdec_get_int(blob, dev->node,
  121. "spi-max-frequency", 500000);
  122. dev->cs = fdtdec_get_int(blob, dev->node, "reg", 0);
  123. return 0;
  124. }
  125. /**
  126. * Initialize SPI protocol.
  127. *
  128. * @param dev CROS_EC device
  129. * @param blob Device tree blob
  130. * @return 0 if ok, -1 on error
  131. */
  132. int cros_ec_spi_init(struct cros_ec_dev *dev, const void *blob)
  133. {
  134. dev->spi = spi_setup_slave_fdt(blob, dev->parent_node,
  135. dev->cs, dev->max_frequency, 0);
  136. if (!dev->spi) {
  137. debug("%s: Could not setup SPI slave\n", __func__);
  138. return -1;
  139. }
  140. return 0;
  141. }