cros_ec_i2c.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <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. int cros_ec_i2c_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
  23. const uint8_t *dout, int dout_len,
  24. uint8_t **dinp, int din_len)
  25. {
  26. int old_bus = 0;
  27. /* version8, cmd8, arglen8, out8[dout_len], csum8 */
  28. int out_bytes = dout_len + 4;
  29. /* response8, arglen8, in8[din_len], checksum8 */
  30. int in_bytes = din_len + 3;
  31. uint8_t *ptr;
  32. /* Receive input data, so that args will be dword aligned */
  33. uint8_t *in_ptr;
  34. int ret;
  35. old_bus = i2c_get_bus_num();
  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->cmd_version_is_supported) {
  63. /* Send an old-style command */
  64. *ptr++ = cmd;
  65. out_bytes = dout_len + 1;
  66. in_bytes = din_len + 2;
  67. in_ptr--; /* Expect just a status byte */
  68. } else {
  69. *ptr++ = EC_CMD_VERSION0 + cmd_version;
  70. *ptr++ = cmd;
  71. *ptr++ = dout_len;
  72. in_ptr -= 2; /* Expect status, length bytes */
  73. }
  74. memcpy(ptr, dout, dout_len);
  75. ptr += dout_len;
  76. if (dev->cmd_version_is_supported)
  77. *ptr++ = (uint8_t)
  78. cros_ec_calc_checksum(dev->dout, dout_len + 3);
  79. /* Set to the proper i2c bus */
  80. if (i2c_set_bus_num(dev->bus_num)) {
  81. debug("%s: Cannot change to I2C bus %d\n", __func__,
  82. dev->bus_num);
  83. return -1;
  84. }
  85. /* Send output data */
  86. cros_ec_dump_data("out", -1, dev->dout, out_bytes);
  87. ret = i2c_write(dev->addr, 0, 0, dev->dout, out_bytes);
  88. if (ret) {
  89. debug("%s: Cannot complete I2C write to 0x%x\n",
  90. __func__, dev->addr);
  91. ret = -1;
  92. }
  93. if (!ret) {
  94. ret = i2c_read(dev->addr, 0, 0, in_ptr, in_bytes);
  95. if (ret) {
  96. debug("%s: Cannot complete I2C read from 0x%x\n",
  97. __func__, dev->addr);
  98. ret = -1;
  99. }
  100. }
  101. /* Return to original bus number */
  102. i2c_set_bus_num(old_bus);
  103. if (ret)
  104. return ret;
  105. if (*in_ptr != EC_RES_SUCCESS) {
  106. debug("%s: Received bad result code %d\n", __func__, *in_ptr);
  107. return -(int)*in_ptr;
  108. }
  109. if (dev->cmd_version_is_supported) {
  110. int len, csum;
  111. len = in_ptr[1];
  112. if (len + 3 > sizeof(dev->din)) {
  113. debug("%s: Received length %#02x too large\n",
  114. __func__, len);
  115. return -1;
  116. }
  117. csum = cros_ec_calc_checksum(in_ptr, 2 + len);
  118. if (csum != in_ptr[2 + len]) {
  119. debug("%s: Invalid checksum rx %#02x, calced %#02x\n",
  120. __func__, in_ptr[2 + din_len], csum);
  121. return -1;
  122. }
  123. din_len = min(din_len, len);
  124. cros_ec_dump_data("in", -1, in_ptr, din_len + 3);
  125. } else {
  126. cros_ec_dump_data("in (old)", -1, in_ptr, in_bytes);
  127. }
  128. /* Return pointer to dword-aligned input data, if any */
  129. *dinp = dev->din + sizeof(int64_t);
  130. return din_len;
  131. }
  132. int cros_ec_i2c_decode_fdt(struct cros_ec_dev *dev, const void *blob)
  133. {
  134. /* Decode interface-specific FDT params */
  135. dev->max_frequency = fdtdec_get_int(blob, dev->node,
  136. "i2c-max-frequency", 100000);
  137. dev->bus_num = i2c_get_bus_num_fdt(dev->parent_node);
  138. if (dev->bus_num == -1) {
  139. debug("%s: Failed to read bus number\n", __func__);
  140. return -1;
  141. }
  142. dev->addr = fdtdec_get_int(blob, dev->node, "reg", -1);
  143. if (dev->addr == -1) {
  144. debug("%s: Failed to read device address\n", __func__);
  145. return -1;
  146. }
  147. return 0;
  148. }
  149. /**
  150. * Initialize I2C protocol.
  151. *
  152. * @param dev CROS_EC device
  153. * @param blob Device tree blob
  154. * @return 0 if ok, -1 on error
  155. */
  156. int cros_ec_i2c_init(struct cros_ec_dev *dev, const void *blob)
  157. {
  158. i2c_init(dev->max_frequency, dev->addr);
  159. dev->cmd_version_is_supported = 0;
  160. return 0;
  161. }