cros_ec_i2c.c 3.8 KB

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