cros_ec_spi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #ifdef CONFIG_DM_CROS_EC
  21. int cros_ec_spi_packet(struct udevice *udev, int out_bytes, int in_bytes)
  22. {
  23. struct cros_ec_dev *dev = udev->uclass_priv;
  24. #else
  25. int cros_ec_spi_packet(struct cros_ec_dev *dev, int out_bytes, int in_bytes)
  26. {
  27. #endif
  28. struct spi_slave *slave = dev_get_parentdata(dev->dev);
  29. int rv;
  30. /* Do the transfer */
  31. if (spi_claim_bus(slave)) {
  32. debug("%s: Cannot claim SPI bus\n", __func__);
  33. return -1;
  34. }
  35. rv = spi_xfer(slave, max(out_bytes, in_bytes) * 8,
  36. dev->dout, dev->din,
  37. SPI_XFER_BEGIN | SPI_XFER_END);
  38. spi_release_bus(slave);
  39. if (rv) {
  40. debug("%s: Cannot complete SPI transfer\n", __func__);
  41. return -1;
  42. }
  43. return in_bytes;
  44. }
  45. /**
  46. * Send a command to a LPC CROS_EC device and return the reply.
  47. *
  48. * The device's internal input/output buffers are used.
  49. *
  50. * @param dev CROS_EC device
  51. * @param cmd Command to send (EC_CMD_...)
  52. * @param cmd_version Version of command to send (EC_VER_...)
  53. * @param dout Output data (may be NULL If dout_len=0)
  54. * @param dout_len Size of output data in bytes
  55. * @param dinp Returns pointer to response data. This will be
  56. * untouched unless we return a value > 0.
  57. * @param din_len Maximum size of response in bytes
  58. * @return number of bytes in response, or -1 on error
  59. */
  60. #ifdef CONFIG_DM_CROS_EC
  61. int cros_ec_spi_command(struct udevice *udev, uint8_t cmd, int cmd_version,
  62. const uint8_t *dout, int dout_len,
  63. uint8_t **dinp, int din_len)
  64. {
  65. struct cros_ec_dev *dev = udev->uclass_priv;
  66. #else
  67. int cros_ec_spi_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
  68. const uint8_t *dout, int dout_len,
  69. uint8_t **dinp, int din_len)
  70. {
  71. #endif
  72. struct spi_slave *slave = dev_get_parentdata(dev->dev);
  73. int in_bytes = din_len + 4; /* status, length, checksum, trailer */
  74. uint8_t *out;
  75. uint8_t *p;
  76. int csum, len;
  77. int rv;
  78. if (dev->protocol_version != 2) {
  79. debug("%s: Unsupported EC protcol version %d\n",
  80. __func__, dev->protocol_version);
  81. return -1;
  82. }
  83. /*
  84. * Sanity-check input size to make sure it plus transaction overhead
  85. * fits in the internal device buffer.
  86. */
  87. if (in_bytes > sizeof(dev->din)) {
  88. debug("%s: Cannot receive %d bytes\n", __func__, din_len);
  89. return -1;
  90. }
  91. /* We represent message length as a byte */
  92. if (dout_len > 0xff) {
  93. debug("%s: Cannot send %d bytes\n", __func__, dout_len);
  94. return -1;
  95. }
  96. /*
  97. * Clear input buffer so we don't get false hits for MSG_HEADER
  98. */
  99. memset(dev->din, '\0', in_bytes);
  100. if (spi_claim_bus(slave)) {
  101. debug("%s: Cannot claim SPI bus\n", __func__);
  102. return -1;
  103. }
  104. out = dev->dout;
  105. out[0] = EC_CMD_VERSION0 + cmd_version;
  106. out[1] = cmd;
  107. out[2] = (uint8_t)dout_len;
  108. memcpy(out + 3, dout, dout_len);
  109. csum = cros_ec_calc_checksum(out, 3)
  110. + cros_ec_calc_checksum(dout, dout_len);
  111. out[3 + dout_len] = (uint8_t)csum;
  112. /*
  113. * Send output data and receive input data starting such that the
  114. * message body will be dword aligned.
  115. */
  116. p = dev->din + sizeof(int64_t) - 2;
  117. len = dout_len + 4;
  118. cros_ec_dump_data("out", cmd, out, len);
  119. rv = spi_xfer(slave, max(len, in_bytes) * 8, out, p,
  120. SPI_XFER_BEGIN | SPI_XFER_END);
  121. spi_release_bus(slave);
  122. if (rv) {
  123. debug("%s: Cannot complete SPI transfer\n", __func__);
  124. return -1;
  125. }
  126. len = min(p[1], din_len);
  127. cros_ec_dump_data("in", -1, p, len + 3);
  128. /* Response code is first byte of message */
  129. if (p[0] != EC_RES_SUCCESS) {
  130. printf("%s: Returned status %d\n", __func__, p[0]);
  131. return -(int)(p[0]);
  132. }
  133. /* Check checksum */
  134. csum = cros_ec_calc_checksum(p, len + 2);
  135. if (csum != p[len + 2]) {
  136. debug("%s: Invalid checksum rx %#02x, calced %#02x\n", __func__,
  137. p[2 + len], csum);
  138. return -1;
  139. }
  140. /* Anything else is the response data */
  141. *dinp = p + 2;
  142. return len;
  143. }
  144. #ifndef CONFIG_DM_CROS_EC
  145. int cros_ec_spi_decode_fdt(struct cros_ec_dev *dev, const void *blob)
  146. {
  147. /* Decode interface-specific FDT params */
  148. dev->max_frequency = fdtdec_get_int(blob, dev->node,
  149. "spi-max-frequency", 500000);
  150. dev->cs = fdtdec_get_int(blob, dev->node, "reg", 0);
  151. return 0;
  152. }
  153. /**
  154. * Initialize SPI protocol.
  155. *
  156. * @param dev CROS_EC device
  157. * @param blob Device tree blob
  158. * @return 0 if ok, -1 on error
  159. */
  160. int cros_ec_spi_init(struct cros_ec_dev *dev, const void *blob)
  161. {
  162. int ret;
  163. ret = spi_setup_slave_fdt(blob, dev->node, dev->parent_node,
  164. &slave);
  165. if (ret) {
  166. debug("%s: Could not setup SPI slave\n", __func__);
  167. return ret;
  168. }
  169. return 0;
  170. }
  171. #endif
  172. #ifdef CONFIG_DM_CROS_EC
  173. int cros_ec_probe(struct udevice *dev)
  174. {
  175. struct spi_slave *slave = dev_get_parentdata(dev);
  176. int ret;
  177. /*
  178. * TODO(sjg@chromium.org)
  179. *
  180. * This is really horrible at present. It is an artifact of removing
  181. * the child_pre_probe() method for SPI. Everything here could go in
  182. * an automatic function, except that spi_get_bus_and_cs() wants to
  183. * set it up manually and call device_probe_child().
  184. *
  185. * The solution may be to re-enable the child_pre_probe() method for
  186. * SPI and have it do nothing if the child is already passed in via
  187. * device_probe_child().
  188. */
  189. slave->dev = dev;
  190. ret = spi_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, slave);
  191. if (ret)
  192. return ret;
  193. return cros_ec_register(dev);
  194. }
  195. struct dm_cros_ec_ops cros_ec_ops = {
  196. .packet = cros_ec_spi_packet,
  197. .command = cros_ec_spi_command,
  198. };
  199. static const struct udevice_id cros_ec_ids[] = {
  200. { .compatible = "google,cros-ec" },
  201. { }
  202. };
  203. U_BOOT_DRIVER(cros_ec_spi) = {
  204. .name = "cros_ec",
  205. .id = UCLASS_CROS_EC,
  206. .of_match = cros_ec_ids,
  207. .probe = cros_ec_probe,
  208. .ops = &cros_ec_ops,
  209. };
  210. #endif