cros_ec.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. *
  4. * Copyright (C) 2012 Google Inc.
  5. * Copyright (C) 2016 Free Software Foundation, Inc.
  6. *
  7. * This is based on depthcharge code.
  8. *
  9. * GRUB is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * GRUB is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <grub/mm.h>
  23. #include <grub/time.h>
  24. #include <grub/misc.h>
  25. #include <grub/arm/cros_ec.h>
  26. #include <grub/fdtbus.h>
  27. static const grub_uint64_t FRAMING_TIMEOUT_MS = 300;
  28. static const grub_uint8_t EC_FRAMING_BYTE = 0xec;
  29. #define EC_CMD_MKBP_STATE 0x60
  30. #define EC_CMD_VERSION0 0xdc
  31. static grub_uint64_t last_transfer;
  32. static void
  33. stop_bus (const struct grub_fdtbus_dev *spi)
  34. {
  35. spi->driver->stop (spi);
  36. last_transfer = grub_get_time_ms ();
  37. }
  38. static int
  39. wait_for_frame (const struct grub_fdtbus_dev *spi)
  40. {
  41. grub_uint64_t start = grub_get_time_ms ();
  42. grub_uint8_t byte;
  43. do
  44. {
  45. if (spi->driver->receive (spi, &byte, 1))
  46. return -1;
  47. if (byte != EC_FRAMING_BYTE &&
  48. grub_get_time_ms () - start > FRAMING_TIMEOUT_MS)
  49. {
  50. grub_dprintf ("cros", "Timeout waiting for framing byte.\n");
  51. return -1;
  52. }
  53. }
  54. while (byte != EC_FRAMING_BYTE);
  55. return 0;
  56. }
  57. /*
  58. * Calculate a simple 8-bit checksum of a data block
  59. *
  60. * @param data Data block to checksum
  61. * @param size Size of data block in bytes
  62. * @return checksum value (0 to 255)
  63. */
  64. static grub_uint8_t
  65. cros_ec_calc_checksum (const void *data, int size)
  66. {
  67. grub_uint8_t csum;
  68. const grub_uint8_t *bytes = data;
  69. int i;
  70. for (i = csum = 0; i < size; i++)
  71. csum += bytes[i];
  72. return csum & 0xff;
  73. }
  74. enum
  75. {
  76. /* response, arglen */
  77. CROS_EC_SPI_IN_HDR_SIZE = 2,
  78. /* version, cmd, arglen */
  79. CROS_EC_SPI_OUT_HDR_SIZE = 3
  80. };
  81. static grub_uint8_t busbuf[256];
  82. #define MSG_BYTES ((int)sizeof (busbuf))
  83. static int
  84. ec_command (const struct grub_fdtbus_dev *dev, int cmd, int cmd_version,
  85. const void *dout, int dout_len, void *din, int din_len)
  86. {
  87. const struct grub_fdtbus_dev *spi = dev->parent;
  88. grub_uint8_t *bytes;
  89. /* Header + data + checksum. */
  90. grub_uint32_t out_bytes = CROS_EC_SPI_OUT_HDR_SIZE + dout_len + 1;
  91. grub_uint32_t in_bytes = CROS_EC_SPI_IN_HDR_SIZE + din_len + 1;
  92. /*
  93. * Sanity-check I/O sizes given transaction overhead in internal
  94. * buffers.
  95. */
  96. if (out_bytes > MSG_BYTES)
  97. {
  98. grub_dprintf ("cros", "Cannot send %d bytes\n", dout_len);
  99. return -1;
  100. }
  101. if (in_bytes > MSG_BYTES)
  102. {
  103. grub_dprintf ("cros", "Cannot receive %d bytes\n", din_len);
  104. return -1;
  105. }
  106. /* Prepare the output. */
  107. bytes = busbuf;
  108. *bytes++ = EC_CMD_VERSION0 + cmd_version;
  109. *bytes++ = cmd;
  110. *bytes++ = dout_len;
  111. grub_memcpy (bytes, dout, dout_len);
  112. bytes += dout_len;
  113. *bytes++ = cros_ec_calc_checksum (busbuf,
  114. CROS_EC_SPI_OUT_HDR_SIZE + dout_len);
  115. /* Depthcharge uses 200 us here but GRUB timer resolution is only 1ms,
  116. decrease this when we increase timer resolution. */
  117. while (grub_get_time_ms () - last_transfer < 1)
  118. ;
  119. if (spi->driver->start (spi))
  120. return -1;
  121. /* Allow EC to ramp up clock after being awoken. */
  122. /* Depthcharge only waits 100 us here but GRUB timer resolution is only 1ms,
  123. decrease this when we increase timer resolution. */
  124. grub_millisleep (1);
  125. if (spi->driver->send (spi, busbuf, out_bytes))
  126. {
  127. stop_bus (spi);
  128. return -1;
  129. }
  130. /* Wait until the EC is ready. */
  131. if (wait_for_frame (spi))
  132. {
  133. stop_bus (spi);
  134. return -1;
  135. }
  136. /* Read the response code and the data length. */
  137. bytes = busbuf;
  138. if (spi->driver->receive (spi, bytes, 2))
  139. {
  140. stop_bus (spi);
  141. return -1;
  142. }
  143. grub_uint8_t result = *bytes++;
  144. grub_uint8_t length = *bytes++;
  145. /* Make sure there's enough room for the data. */
  146. if (CROS_EC_SPI_IN_HDR_SIZE + length + 1 > MSG_BYTES)
  147. {
  148. grub_dprintf ("cros", "Received length %#02x too large\n", length);
  149. stop_bus (spi);
  150. return -1;
  151. }
  152. /* Read the data and the checksum, and finish up. */
  153. if (spi->driver->receive (spi, bytes, length + 1))
  154. {
  155. stop_bus (spi);
  156. return -1;
  157. }
  158. bytes += length;
  159. int expected = *bytes++;
  160. stop_bus (spi);
  161. /* Check the integrity of the response. */
  162. if (result != 0)
  163. {
  164. grub_dprintf ("cros", "Received bad result code %d\n", result);
  165. return -result;
  166. }
  167. int csum = cros_ec_calc_checksum (busbuf,
  168. CROS_EC_SPI_IN_HDR_SIZE + length);
  169. if (csum != expected)
  170. {
  171. grub_dprintf ("cros", "Invalid checksum rx %#02x, calced %#02x\n",
  172. expected, csum);
  173. return -1;
  174. }
  175. /* If the caller wants the response, copy it out for them. */
  176. if (length < din_len)
  177. din_len = length;
  178. if (din)
  179. {
  180. grub_memcpy (din, (grub_uint8_t *) busbuf + CROS_EC_SPI_IN_HDR_SIZE, din_len);
  181. }
  182. return din_len;
  183. }
  184. int
  185. grub_cros_ec_scan_keyboard (const struct grub_fdtbus_dev *dev, struct grub_cros_ec_keyscan *scan)
  186. {
  187. if (ec_command (dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
  188. sizeof (*scan)) < (int) sizeof (*scan))
  189. return -1;
  190. return 0;
  191. }
  192. int
  193. grub_cros_ec_validate (const struct grub_fdtbus_dev *dev)
  194. {
  195. if (!grub_fdtbus_is_compatible("google,cros-ec-spi", dev))
  196. return 0;
  197. if (!dev->parent)
  198. return 0;
  199. if (!dev->parent->driver)
  200. return 0;
  201. if (!dev->parent->driver->send
  202. || !dev->parent->driver->receive)
  203. return 0;
  204. return 1;
  205. }