cros_ec_lpc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Chromium OS cros_ec driver - LPC interface
  4. *
  5. * Copyright (c) 2012 The Chromium OS Authors.
  6. */
  7. /*
  8. * The Matrix Keyboard Protocol driver handles talking to the keyboard
  9. * controller chip. Mostly this is for keyboard functions, but some other
  10. * things have slipped in, so we provide generic services to talk to the
  11. * KBC.
  12. */
  13. #include <common.h>
  14. #include <dm.h>
  15. #include <command.h>
  16. #include <cros_ec.h>
  17. #include <log.h>
  18. #include <asm/io.h>
  19. #ifdef DEBUG_TRACE
  20. #define debug_trace(fmt, b...) debug(fmt, ##b)
  21. #else
  22. #define debug_trace(fmt, b...)
  23. #endif
  24. /* Timeout waiting for a flash erase command to complete */
  25. static const int CROS_EC_CMD_TIMEOUT_MS = 5000;
  26. static int wait_for_sync(struct cros_ec_dev *dev)
  27. {
  28. unsigned long start;
  29. start = get_timer(0);
  30. while (inb(EC_LPC_ADDR_HOST_CMD) & EC_LPC_STATUS_BUSY_MASK) {
  31. if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
  32. debug("%s: Timeout waiting for CROS_EC sync\n",
  33. __func__);
  34. return -1;
  35. }
  36. }
  37. return 0;
  38. }
  39. int cros_ec_lpc_packet(struct udevice *udev, int out_bytes, int in_bytes)
  40. {
  41. struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
  42. uint8_t *d;
  43. int i;
  44. if (out_bytes > EC_LPC_HOST_PACKET_SIZE)
  45. return log_msg_ret("Cannot send that many bytes\n", -E2BIG);
  46. if (in_bytes > EC_LPC_HOST_PACKET_SIZE)
  47. return log_msg_ret("Cannot receive that many bytes\n", -E2BIG);
  48. if (wait_for_sync(dev))
  49. return log_msg_ret("Timeout waiting ready\n", -ETIMEDOUT);
  50. /* Write data */
  51. for (i = 0, d = (uint8_t *)dev->dout; i < out_bytes; i++, d++)
  52. outb(*d, EC_LPC_ADDR_HOST_PACKET + i);
  53. /* Start the command */
  54. outb(EC_COMMAND_PROTOCOL_3, EC_LPC_ADDR_HOST_CMD);
  55. if (wait_for_sync(dev))
  56. return log_msg_ret("Timeout waiting ready\n", -ETIMEDOUT);
  57. /* Read back args */
  58. for (i = 0, d = dev->din; i < in_bytes; i++, d++)
  59. *d = inb(EC_LPC_ADDR_HOST_PACKET + i);
  60. return in_bytes;
  61. }
  62. int cros_ec_lpc_command(struct udevice *udev, uint8_t cmd, int cmd_version,
  63. const uint8_t *dout, int dout_len,
  64. uint8_t **dinp, int din_len)
  65. {
  66. struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
  67. const int cmd_addr = EC_LPC_ADDR_HOST_CMD;
  68. const int data_addr = EC_LPC_ADDR_HOST_DATA;
  69. const int args_addr = EC_LPC_ADDR_HOST_ARGS;
  70. const int param_addr = EC_LPC_ADDR_HOST_PARAM;
  71. struct ec_lpc_host_args args;
  72. uint8_t *d;
  73. int csum;
  74. int i;
  75. if (dout_len > EC_PROTO2_MAX_PARAM_SIZE) {
  76. debug("%s: Cannot send %d bytes\n", __func__, dout_len);
  77. return -1;
  78. }
  79. /* Fill in args */
  80. args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
  81. args.command_version = cmd_version;
  82. args.data_size = dout_len;
  83. /* Calculate checksum */
  84. csum = cmd + args.flags + args.command_version + args.data_size;
  85. for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++)
  86. csum += *d;
  87. args.checksum = (uint8_t)csum;
  88. if (wait_for_sync(dev)) {
  89. debug("%s: Timeout waiting ready\n", __func__);
  90. return -1;
  91. }
  92. /* Write args */
  93. for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
  94. outb(*d, args_addr + i);
  95. /* Write data, if any */
  96. debug_trace("cmd: %02x, ver: %02x", cmd, cmd_version);
  97. for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++) {
  98. outb(*d, param_addr + i);
  99. debug_trace("%02x ", *d);
  100. }
  101. outb(cmd, cmd_addr);
  102. debug_trace("\n");
  103. if (wait_for_sync(dev)) {
  104. debug("%s: Timeout waiting for response\n", __func__);
  105. return -1;
  106. }
  107. /* Check result */
  108. i = inb(data_addr);
  109. if (i) {
  110. debug("%s: CROS_EC result code %d\n", __func__, i);
  111. return -i;
  112. }
  113. /* Read back args */
  114. for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
  115. *d = inb(args_addr + i);
  116. /*
  117. * If EC didn't modify args flags, then somehow we sent a new-style
  118. * command to an old EC, which means it would have read its params
  119. * from the wrong place.
  120. */
  121. if (!(args.flags & EC_HOST_ARGS_FLAG_TO_HOST)) {
  122. debug("%s: CROS_EC protocol mismatch\n", __func__);
  123. return -EC_RES_INVALID_RESPONSE;
  124. }
  125. if (args.data_size > din_len) {
  126. debug("%s: CROS_EC returned too much data %d > %d\n",
  127. __func__, args.data_size, din_len);
  128. return -EC_RES_INVALID_RESPONSE;
  129. }
  130. /* Read data, if any */
  131. for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++) {
  132. *d = inb(param_addr + i);
  133. debug_trace("%02x ", *d);
  134. }
  135. debug_trace("\n");
  136. /* Verify checksum */
  137. csum = cmd + args.flags + args.command_version + args.data_size;
  138. for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++)
  139. csum += *d;
  140. if (args.checksum != (uint8_t)csum) {
  141. debug("%s: CROS_EC response has invalid checksum\n", __func__);
  142. return -EC_RES_INVALID_CHECKSUM;
  143. }
  144. *dinp = dev->din;
  145. /* Return actual amount of data received */
  146. return args.data_size;
  147. }
  148. /**
  149. * Initialize LPC protocol.
  150. *
  151. * @param dev CROS_EC device
  152. * @param blob Device tree blob
  153. * @return 0 if ok, -1 on error
  154. */
  155. int cros_ec_lpc_init(struct cros_ec_dev *dev, const void *blob)
  156. {
  157. int byte, i;
  158. /* See if we can find an EC at the other end */
  159. byte = 0xff;
  160. byte &= inb(EC_LPC_ADDR_HOST_CMD);
  161. byte &= inb(EC_LPC_ADDR_HOST_DATA);
  162. for (i = 0; i < EC_PROTO2_MAX_PARAM_SIZE && (byte == 0xff); i++)
  163. byte &= inb(EC_LPC_ADDR_HOST_PARAM + i);
  164. if (byte == 0xff) {
  165. debug("%s: CROS_EC device not found on LPC bus\n",
  166. __func__);
  167. return -1;
  168. }
  169. return 0;
  170. }
  171. /*
  172. * Test if LPC command args are supported.
  173. *
  174. * The cheapest way to do this is by looking for the memory-mapped
  175. * flag. This is faster than sending a new-style 'hello' command and
  176. * seeing whether the EC sets the EC_HOST_ARGS_FLAG_FROM_HOST flag
  177. * in args when it responds.
  178. */
  179. static int cros_ec_lpc_check_version(struct udevice *dev)
  180. {
  181. if (inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID) == 'E' &&
  182. inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID + 1)
  183. == 'C' &&
  184. (inb(EC_LPC_ADDR_MEMMAP +
  185. EC_MEMMAP_HOST_CMD_FLAGS) &
  186. EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED)) {
  187. return 0;
  188. }
  189. printf("%s: ERROR: old EC interface not supported\n", __func__);
  190. return -1;
  191. }
  192. static int cros_ec_probe(struct udevice *dev)
  193. {
  194. return cros_ec_register(dev);
  195. }
  196. static struct dm_cros_ec_ops cros_ec_ops = {
  197. .packet = cros_ec_lpc_packet,
  198. .command = cros_ec_lpc_command,
  199. .check_version = cros_ec_lpc_check_version,
  200. };
  201. static const struct udevice_id cros_ec_ids[] = {
  202. { .compatible = "google,cros-ec-lpc" },
  203. { }
  204. };
  205. U_BOOT_DRIVER(google_cros_ec_lpc) = {
  206. .name = "google_cros_ec_lpc",
  207. .id = UCLASS_CROS_EC,
  208. .of_match = cros_ec_ids,
  209. .probe = cros_ec_probe,
  210. .ops = &cros_ec_ops,
  211. };