cros_ec_i2c.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Chromium OS cros_ec driver - I2C 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 <dm.h>
  15. #include <i2c.h>
  16. #include <cros_ec.h>
  17. #include <log.h>
  18. #ifdef DEBUG_TRACE
  19. #define debug_trace(fmt, b...) debug(fmt, #b)
  20. #else
  21. #define debug_trace(fmt, b...)
  22. #endif
  23. /**
  24. * Request format for protocol v3
  25. * byte 0 0xda (EC_COMMAND_PROTOCOL_3)
  26. * byte 1-8 struct ec_host_request
  27. * byte 10- response data
  28. */
  29. struct ec_host_request_i2c {
  30. /* Always 0xda to backward compatible with v2 struct */
  31. uint8_t command_protocol;
  32. struct ec_host_request ec_request;
  33. } __packed;
  34. /*
  35. * Response format for protocol v3
  36. * byte 0 result code
  37. * byte 1 packet_length
  38. * byte 2-9 struct ec_host_response
  39. * byte 10- response data
  40. */
  41. struct ec_host_response_i2c {
  42. uint8_t result;
  43. uint8_t packet_length;
  44. struct ec_host_response ec_response;
  45. } __packed;
  46. static int cros_ec_i2c_packet(struct udevice *udev, int out_bytes, int in_bytes)
  47. {
  48. struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
  49. struct dm_i2c_chip *chip = dev_get_parent_platdata(udev);
  50. struct ec_host_request_i2c *ec_request_i2c =
  51. (struct ec_host_request_i2c *)dev->dout;
  52. struct ec_host_response_i2c *ec_response_i2c =
  53. (struct ec_host_response_i2c *)dev->din;
  54. struct i2c_msg i2c_msg[2];
  55. int ret;
  56. i2c_msg[0].addr = chip->chip_addr;
  57. i2c_msg[0].flags = 0;
  58. i2c_msg[1].addr = chip->chip_addr;
  59. i2c_msg[1].flags = I2C_M_RD;
  60. /* one extra byte, to indicate v3 */
  61. i2c_msg[0].len = out_bytes + 1;
  62. i2c_msg[0].buf = dev->dout;
  63. /* stitch on EC_COMMAND_PROTOCOL_3 */
  64. memmove(&ec_request_i2c->ec_request, dev->dout, out_bytes);
  65. ec_request_i2c->command_protocol = EC_COMMAND_PROTOCOL_3;
  66. /* two extra bytes for v3 */
  67. i2c_msg[1].len = in_bytes + 2;
  68. i2c_msg[1].buf = dev->din;
  69. ret = dm_i2c_xfer(udev, &i2c_msg[0], 2);
  70. if (ret) {
  71. printf("%s: Could not execute transfer: %d\n", __func__, ret);
  72. return ret;
  73. }
  74. /* When we send a v3 request to v2 ec, ec won't recognize the 0xda
  75. * (EC_COMMAND_PROTOCOL_3) and will return with status
  76. * EC_RES_INVALID_COMMAND with zero data length
  77. *
  78. * In case of invalid command for v3 protocol the data length
  79. * will be at least sizeof(struct ec_host_response)
  80. */
  81. if (ec_response_i2c->result == EC_RES_INVALID_COMMAND &&
  82. ec_response_i2c->packet_length == 0)
  83. return -EPROTONOSUPPORT;
  84. if (ec_response_i2c->packet_length < sizeof(struct ec_host_response)) {
  85. printf("%s: response of %u bytes too short; not a full hdr\n",
  86. __func__, ec_response_i2c->packet_length);
  87. return -EBADMSG;
  88. }
  89. /* drop result and packet_len */
  90. memmove(dev->din, &ec_response_i2c->ec_response, in_bytes);
  91. return in_bytes;
  92. }
  93. static int cros_ec_i2c_command(struct udevice *udev, uint8_t cmd,
  94. int cmd_version, const uint8_t *dout,
  95. int dout_len, uint8_t **dinp, int din_len)
  96. {
  97. struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
  98. struct dm_i2c_chip *chip = dev_get_parent_platdata(udev);
  99. struct i2c_msg i2c_msg[2];
  100. /* version8, cmd8, arglen8, out8[dout_len], csum8 */
  101. int out_bytes = dout_len + 4;
  102. /* response8, arglen8, in8[din_len], checksum8 */
  103. int in_bytes = din_len + 3;
  104. uint8_t *ptr;
  105. /* Receive input data, so that args will be dword aligned */
  106. uint8_t *in_ptr;
  107. int len, csum, ret;
  108. /*
  109. * Sanity-check I/O sizes given transaction overhead in internal
  110. * buffers.
  111. */
  112. if (out_bytes > sizeof(dev->dout)) {
  113. debug("%s: Cannot send %d bytes\n", __func__, dout_len);
  114. return -1;
  115. }
  116. if (in_bytes > sizeof(dev->din)) {
  117. debug("%s: Cannot receive %d bytes\n", __func__, din_len);
  118. return -1;
  119. }
  120. assert(dout_len >= 0);
  121. assert(dinp);
  122. i2c_msg[0].addr = chip->chip_addr;
  123. i2c_msg[0].len = out_bytes;
  124. i2c_msg[0].buf = dev->dout;
  125. i2c_msg[0].flags = 0;
  126. /*
  127. * Copy command and data into output buffer so we can do a single I2C
  128. * burst transaction.
  129. */
  130. ptr = dev->dout;
  131. /*
  132. * in_ptr starts of pointing to a dword-aligned input data buffer.
  133. * We decrement it back by the number of header bytes we expect to
  134. * receive, so that the first parameter of the resulting input data
  135. * will be dword aligned.
  136. */
  137. in_ptr = dev->din + sizeof(int64_t);
  138. if (dev->protocol_version != 2) {
  139. /* Something we don't support */
  140. debug("%s: Protocol version %d unsupported\n",
  141. __func__, dev->protocol_version);
  142. return -1;
  143. }
  144. *ptr++ = EC_CMD_VERSION0 + cmd_version;
  145. *ptr++ = cmd;
  146. *ptr++ = dout_len;
  147. in_ptr -= 2; /* Expect status, length bytes */
  148. memcpy(ptr, dout, dout_len);
  149. ptr += dout_len;
  150. *ptr++ = (uint8_t)
  151. cros_ec_calc_checksum(dev->dout, dout_len + 3);
  152. i2c_msg[1].addr = chip->chip_addr;
  153. i2c_msg[1].len = in_bytes;
  154. i2c_msg[1].buf = in_ptr;
  155. i2c_msg[1].flags = I2C_M_RD;
  156. /* Send output data */
  157. cros_ec_dump_data("out", -1, dev->dout, out_bytes);
  158. ret = dm_i2c_xfer(udev, &i2c_msg[0], 2);
  159. if (ret) {
  160. debug("%s: Could not execute transfer to %s\n", __func__,
  161. udev->name);
  162. ret = -1;
  163. }
  164. if (*in_ptr != EC_RES_SUCCESS) {
  165. debug("%s: Received bad result code %d\n", __func__, *in_ptr);
  166. return -(int)*in_ptr;
  167. }
  168. len = in_ptr[1];
  169. if (len + 3 > sizeof(dev->din)) {
  170. debug("%s: Received length %#02x too large\n",
  171. __func__, len);
  172. return -1;
  173. }
  174. csum = cros_ec_calc_checksum(in_ptr, 2 + len);
  175. if (csum != in_ptr[2 + len]) {
  176. debug("%s: Invalid checksum rx %#02x, calced %#02x\n",
  177. __func__, in_ptr[2 + din_len], csum);
  178. return -1;
  179. }
  180. din_len = min(din_len, len);
  181. cros_ec_dump_data("in", -1, in_ptr, din_len + 3);
  182. /* Return pointer to dword-aligned input data, if any */
  183. *dinp = dev->din + sizeof(int64_t);
  184. return din_len;
  185. }
  186. static int cros_ec_probe(struct udevice *dev)
  187. {
  188. return cros_ec_register(dev);
  189. }
  190. static struct dm_cros_ec_ops cros_ec_ops = {
  191. .command = cros_ec_i2c_command,
  192. .packet = cros_ec_i2c_packet,
  193. };
  194. static const struct udevice_id cros_ec_ids[] = {
  195. { .compatible = "google,cros-ec-i2c" },
  196. { }
  197. };
  198. U_BOOT_DRIVER(google_cros_ec_i2c) = {
  199. .name = "google_cros_ec_i2c",
  200. .id = UCLASS_CROS_EC,
  201. .of_match = cros_ec_ids,
  202. .probe = cros_ec_probe,
  203. .ops = &cros_ec_ops,
  204. };