cros_ec_i2c.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Chromium OS cros_ec driver - I2C 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 <dm.h>
  16. #include <i2c.h>
  17. #include <cros_ec.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. static int cros_ec_i2c_command(struct udevice *udev, uint8_t cmd,
  24. int cmd_version, const uint8_t *dout,
  25. int dout_len, uint8_t **dinp, int din_len)
  26. {
  27. struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
  28. /* version8, cmd8, arglen8, out8[dout_len], csum8 */
  29. int out_bytes = dout_len + 4;
  30. /* response8, arglen8, in8[din_len], checksum8 */
  31. int in_bytes = din_len + 3;
  32. uint8_t *ptr;
  33. /* Receive input data, so that args will be dword aligned */
  34. uint8_t *in_ptr;
  35. int len, csum, ret;
  36. /*
  37. * Sanity-check I/O sizes given transaction overhead in internal
  38. * buffers.
  39. */
  40. if (out_bytes > sizeof(dev->dout)) {
  41. debug("%s: Cannot send %d bytes\n", __func__, dout_len);
  42. return -1;
  43. }
  44. if (in_bytes > sizeof(dev->din)) {
  45. debug("%s: Cannot receive %d bytes\n", __func__, din_len);
  46. return -1;
  47. }
  48. assert(dout_len >= 0);
  49. assert(dinp);
  50. /*
  51. * Copy command and data into output buffer so we can do a single I2C
  52. * burst transaction.
  53. */
  54. ptr = dev->dout;
  55. /*
  56. * in_ptr starts of pointing to a dword-aligned input data buffer.
  57. * We decrement it back by the number of header bytes we expect to
  58. * receive, so that the first parameter of the resulting input data
  59. * will be dword aligned.
  60. */
  61. in_ptr = dev->din + sizeof(int64_t);
  62. if (dev->protocol_version != 2) {
  63. /* Something we don't support */
  64. debug("%s: Protocol version %d unsupported\n",
  65. __func__, dev->protocol_version);
  66. return -1;
  67. }
  68. *ptr++ = EC_CMD_VERSION0 + cmd_version;
  69. *ptr++ = cmd;
  70. *ptr++ = dout_len;
  71. in_ptr -= 2; /* Expect status, length bytes */
  72. memcpy(ptr, dout, dout_len);
  73. ptr += dout_len;
  74. *ptr++ = (uint8_t)
  75. cros_ec_calc_checksum(dev->dout, dout_len + 3);
  76. /* Send output data */
  77. cros_ec_dump_data("out", -1, dev->dout, out_bytes);
  78. ret = dm_i2c_write(udev, 0, dev->dout, out_bytes);
  79. if (ret) {
  80. debug("%s: Cannot complete I2C write to %s\n", __func__,
  81. udev->name);
  82. ret = -1;
  83. }
  84. if (!ret) {
  85. ret = dm_i2c_read(udev, 0, in_ptr, in_bytes);
  86. if (ret) {
  87. debug("%s: Cannot complete I2C read from %s\n",
  88. __func__, udev->name);
  89. ret = -1;
  90. }
  91. }
  92. if (*in_ptr != EC_RES_SUCCESS) {
  93. debug("%s: Received bad result code %d\n", __func__, *in_ptr);
  94. return -(int)*in_ptr;
  95. }
  96. len = in_ptr[1];
  97. if (len + 3 > sizeof(dev->din)) {
  98. debug("%s: Received length %#02x too large\n",
  99. __func__, len);
  100. return -1;
  101. }
  102. csum = cros_ec_calc_checksum(in_ptr, 2 + len);
  103. if (csum != in_ptr[2 + len]) {
  104. debug("%s: Invalid checksum rx %#02x, calced %#02x\n",
  105. __func__, in_ptr[2 + din_len], csum);
  106. return -1;
  107. }
  108. din_len = min(din_len, len);
  109. cros_ec_dump_data("in", -1, in_ptr, din_len + 3);
  110. /* Return pointer to dword-aligned input data, if any */
  111. *dinp = dev->din + sizeof(int64_t);
  112. return din_len;
  113. }
  114. static int cros_ec_probe(struct udevice *dev)
  115. {
  116. return cros_ec_register(dev);
  117. }
  118. static struct dm_cros_ec_ops cros_ec_ops = {
  119. .command = cros_ec_i2c_command,
  120. };
  121. static const struct udevice_id cros_ec_ids[] = {
  122. { .compatible = "google,cros-ec-i2c" },
  123. { }
  124. };
  125. U_BOOT_DRIVER(cros_ec_i2c) = {
  126. .name = "cros_ec_i2c",
  127. .id = UCLASS_CROS_EC,
  128. .of_match = cros_ec_ids,
  129. .probe = cros_ec_probe,
  130. .ops = &cros_ec_ops,
  131. };