cros_ec_i2c.c 6.1 KB

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