cros_ec_i2c.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Chromium OS cros_ec driver - I2C interface
  3. *
  4. * Copyright (c) 2012 The Chromium OS Authors.
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * The Matrix Keyboard Protocol driver handles talking to the keyboard
  25. * controller chip. Mostly this is for keyboard functions, but some other
  26. * things have slipped in, so we provide generic services to talk to the
  27. * KBC.
  28. */
  29. #include <common.h>
  30. #include <i2c.h>
  31. #include <cros_ec.h>
  32. #ifdef DEBUG_TRACE
  33. #define debug_trace(fmt, b...) debug(fmt, #b)
  34. #else
  35. #define debug_trace(fmt, b...)
  36. #endif
  37. int cros_ec_i2c_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
  38. const uint8_t *dout, int dout_len,
  39. uint8_t **dinp, int din_len)
  40. {
  41. int old_bus = 0;
  42. /* version8, cmd8, arglen8, out8[dout_len], csum8 */
  43. int out_bytes = dout_len + 4;
  44. /* response8, arglen8, in8[din_len], checksum8 */
  45. int in_bytes = din_len + 3;
  46. uint8_t *ptr;
  47. /* Receive input data, so that args will be dword aligned */
  48. uint8_t *in_ptr;
  49. int ret;
  50. old_bus = i2c_get_bus_num();
  51. /*
  52. * Sanity-check I/O sizes given transaction overhead in internal
  53. * buffers.
  54. */
  55. if (out_bytes > sizeof(dev->dout)) {
  56. debug("%s: Cannot send %d bytes\n", __func__, dout_len);
  57. return -1;
  58. }
  59. if (in_bytes > sizeof(dev->din)) {
  60. debug("%s: Cannot receive %d bytes\n", __func__, din_len);
  61. return -1;
  62. }
  63. assert(dout_len >= 0);
  64. assert(dinp);
  65. /*
  66. * Copy command and data into output buffer so we can do a single I2C
  67. * burst transaction.
  68. */
  69. ptr = dev->dout;
  70. /*
  71. * in_ptr starts of pointing to a dword-aligned input data buffer.
  72. * We decrement it back by the number of header bytes we expect to
  73. * receive, so that the first parameter of the resulting input data
  74. * will be dword aligned.
  75. */
  76. in_ptr = dev->din + sizeof(int64_t);
  77. if (!dev->cmd_version_is_supported) {
  78. /* Send an old-style command */
  79. *ptr++ = cmd;
  80. out_bytes = dout_len + 1;
  81. in_bytes = din_len + 2;
  82. in_ptr--; /* Expect just a status byte */
  83. } else {
  84. *ptr++ = EC_CMD_VERSION0 + cmd_version;
  85. *ptr++ = cmd;
  86. *ptr++ = dout_len;
  87. in_ptr -= 2; /* Expect status, length bytes */
  88. }
  89. memcpy(ptr, dout, dout_len);
  90. ptr += dout_len;
  91. if (dev->cmd_version_is_supported)
  92. *ptr++ = (uint8_t)
  93. cros_ec_calc_checksum(dev->dout, dout_len + 3);
  94. /* Set to the proper i2c bus */
  95. if (i2c_set_bus_num(dev->bus_num)) {
  96. debug("%s: Cannot change to I2C bus %d\n", __func__,
  97. dev->bus_num);
  98. return -1;
  99. }
  100. /* Send output data */
  101. cros_ec_dump_data("out", -1, dev->dout, out_bytes);
  102. ret = i2c_write(dev->addr, 0, 0, dev->dout, out_bytes);
  103. if (ret) {
  104. debug("%s: Cannot complete I2C write to 0x%x\n",
  105. __func__, dev->addr);
  106. ret = -1;
  107. }
  108. if (!ret) {
  109. ret = i2c_read(dev->addr, 0, 0, in_ptr, in_bytes);
  110. if (ret) {
  111. debug("%s: Cannot complete I2C read from 0x%x\n",
  112. __func__, dev->addr);
  113. ret = -1;
  114. }
  115. }
  116. /* Return to original bus number */
  117. i2c_set_bus_num(old_bus);
  118. if (ret)
  119. return ret;
  120. if (*in_ptr != EC_RES_SUCCESS) {
  121. debug("%s: Received bad result code %d\n", __func__, *in_ptr);
  122. return -(int)*in_ptr;
  123. }
  124. if (dev->cmd_version_is_supported) {
  125. int len, csum;
  126. len = in_ptr[1];
  127. if (len + 3 > sizeof(dev->din)) {
  128. debug("%s: Received length %#02x too large\n",
  129. __func__, len);
  130. return -1;
  131. }
  132. csum = cros_ec_calc_checksum(in_ptr, 2 + len);
  133. if (csum != in_ptr[2 + len]) {
  134. debug("%s: Invalid checksum rx %#02x, calced %#02x\n",
  135. __func__, in_ptr[2 + din_len], csum);
  136. return -1;
  137. }
  138. din_len = min(din_len, len);
  139. cros_ec_dump_data("in", -1, in_ptr, din_len + 3);
  140. } else {
  141. cros_ec_dump_data("in (old)", -1, in_ptr, in_bytes);
  142. }
  143. /* Return pointer to dword-aligned input data, if any */
  144. *dinp = dev->din + sizeof(int64_t);
  145. return din_len;
  146. }
  147. int cros_ec_i2c_decode_fdt(struct cros_ec_dev *dev, const void *blob)
  148. {
  149. /* Decode interface-specific FDT params */
  150. dev->max_frequency = fdtdec_get_int(blob, dev->node,
  151. "i2c-max-frequency", 100000);
  152. dev->bus_num = i2c_get_bus_num_fdt(dev->parent_node);
  153. if (dev->bus_num == -1) {
  154. debug("%s: Failed to read bus number\n", __func__);
  155. return -1;
  156. }
  157. dev->addr = fdtdec_get_int(blob, dev->node, "reg", -1);
  158. if (dev->addr == -1) {
  159. debug("%s: Failed to read device address\n", __func__);
  160. return -1;
  161. }
  162. return 0;
  163. }
  164. /**
  165. * Initialize I2C protocol.
  166. *
  167. * @param dev CROS_EC device
  168. * @param blob Device tree blob
  169. * @return 0 if ok, -1 on error
  170. */
  171. int cros_ec_i2c_init(struct cros_ec_dev *dev, const void *blob)
  172. {
  173. i2c_init(dev->max_frequency, dev->addr);
  174. dev->cmd_version_is_supported = 0;
  175. return 0;
  176. }