cros_ec_lpc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <asm/io.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 wait_for_sync(struct cros_ec_dev *dev)
  24. {
  25. unsigned long start;
  26. start = get_timer(0);
  27. while (inb(EC_LPC_ADDR_HOST_CMD) & EC_LPC_STATUS_BUSY_MASK) {
  28. if (get_timer(start) > 1000) {
  29. debug("%s: Timeout waiting for CROS_EC sync\n",
  30. __func__);
  31. return -1;
  32. }
  33. }
  34. return 0;
  35. }
  36. int cros_ec_lpc_command(struct udevice *udev, uint8_t cmd, int cmd_version,
  37. const uint8_t *dout, int dout_len,
  38. uint8_t **dinp, int din_len)
  39. {
  40. struct cros_ec_dev *dev = dev_get_uclass_priv(udev);
  41. const int cmd_addr = EC_LPC_ADDR_HOST_CMD;
  42. const int data_addr = EC_LPC_ADDR_HOST_DATA;
  43. const int args_addr = EC_LPC_ADDR_HOST_ARGS;
  44. const int param_addr = EC_LPC_ADDR_HOST_PARAM;
  45. struct ec_lpc_host_args args;
  46. uint8_t *d;
  47. int csum;
  48. int i;
  49. if (dout_len > EC_PROTO2_MAX_PARAM_SIZE) {
  50. debug("%s: Cannot send %d bytes\n", __func__, dout_len);
  51. return -1;
  52. }
  53. /* Fill in args */
  54. args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
  55. args.command_version = cmd_version;
  56. args.data_size = dout_len;
  57. /* Calculate checksum */
  58. csum = cmd + args.flags + args.command_version + args.data_size;
  59. for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++)
  60. csum += *d;
  61. args.checksum = (uint8_t)csum;
  62. if (wait_for_sync(dev)) {
  63. debug("%s: Timeout waiting ready\n", __func__);
  64. return -1;
  65. }
  66. /* Write args */
  67. for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
  68. outb(*d, args_addr + i);
  69. /* Write data, if any */
  70. debug_trace("cmd: %02x, ver: %02x", cmd, cmd_version);
  71. for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++) {
  72. outb(*d, param_addr + i);
  73. debug_trace("%02x ", *d);
  74. }
  75. outb(cmd, cmd_addr);
  76. debug_trace("\n");
  77. if (wait_for_sync(dev)) {
  78. debug("%s: Timeout waiting for response\n", __func__);
  79. return -1;
  80. }
  81. /* Check result */
  82. i = inb(data_addr);
  83. if (i) {
  84. debug("%s: CROS_EC result code %d\n", __func__, i);
  85. return -i;
  86. }
  87. /* Read back args */
  88. for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
  89. *d = inb(args_addr + i);
  90. /*
  91. * If EC didn't modify args flags, then somehow we sent a new-style
  92. * command to an old EC, which means it would have read its params
  93. * from the wrong place.
  94. */
  95. if (!(args.flags & EC_HOST_ARGS_FLAG_TO_HOST)) {
  96. debug("%s: CROS_EC protocol mismatch\n", __func__);
  97. return -EC_RES_INVALID_RESPONSE;
  98. }
  99. if (args.data_size > din_len) {
  100. debug("%s: CROS_EC returned too much data %d > %d\n",
  101. __func__, args.data_size, din_len);
  102. return -EC_RES_INVALID_RESPONSE;
  103. }
  104. /* Read data, if any */
  105. for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++) {
  106. *d = inb(param_addr + i);
  107. debug_trace("%02x ", *d);
  108. }
  109. debug_trace("\n");
  110. /* Verify checksum */
  111. csum = cmd + args.flags + args.command_version + args.data_size;
  112. for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++)
  113. csum += *d;
  114. if (args.checksum != (uint8_t)csum) {
  115. debug("%s: CROS_EC response has invalid checksum\n", __func__);
  116. return -EC_RES_INVALID_CHECKSUM;
  117. }
  118. *dinp = dev->din;
  119. /* Return actual amount of data received */
  120. return args.data_size;
  121. }
  122. /**
  123. * Initialize LPC protocol.
  124. *
  125. * @param dev CROS_EC device
  126. * @param blob Device tree blob
  127. * @return 0 if ok, -1 on error
  128. */
  129. int cros_ec_lpc_init(struct cros_ec_dev *dev, const void *blob)
  130. {
  131. int byte, i;
  132. /* See if we can find an EC at the other end */
  133. byte = 0xff;
  134. byte &= inb(EC_LPC_ADDR_HOST_CMD);
  135. byte &= inb(EC_LPC_ADDR_HOST_DATA);
  136. for (i = 0; i < EC_PROTO2_MAX_PARAM_SIZE && (byte == 0xff); i++)
  137. byte &= inb(EC_LPC_ADDR_HOST_PARAM + i);
  138. if (byte == 0xff) {
  139. debug("%s: CROS_EC device not found on LPC bus\n",
  140. __func__);
  141. return -1;
  142. }
  143. return 0;
  144. }
  145. /*
  146. * Test if LPC command args are supported.
  147. *
  148. * The cheapest way to do this is by looking for the memory-mapped
  149. * flag. This is faster than sending a new-style 'hello' command and
  150. * seeing whether the EC sets the EC_HOST_ARGS_FLAG_FROM_HOST flag
  151. * in args when it responds.
  152. */
  153. static int cros_ec_lpc_check_version(struct udevice *dev)
  154. {
  155. if (inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID) == 'E' &&
  156. inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID + 1)
  157. == 'C' &&
  158. (inb(EC_LPC_ADDR_MEMMAP +
  159. EC_MEMMAP_HOST_CMD_FLAGS) &
  160. EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED)) {
  161. return 0;
  162. }
  163. printf("%s: ERROR: old EC interface not supported\n", __func__);
  164. return -1;
  165. }
  166. static int cros_ec_probe(struct udevice *dev)
  167. {
  168. return cros_ec_register(dev);
  169. }
  170. static struct dm_cros_ec_ops cros_ec_ops = {
  171. .command = cros_ec_lpc_command,
  172. .check_version = cros_ec_lpc_check_version,
  173. };
  174. static const struct udevice_id cros_ec_ids[] = {
  175. { .compatible = "google,cros-ec-lpc" },
  176. { }
  177. };
  178. U_BOOT_DRIVER(cros_ec_lpc) = {
  179. .name = "cros_ec_lpc",
  180. .id = UCLASS_CROS_EC,
  181. .of_match = cros_ec_ids,
  182. .probe = cros_ec_probe,
  183. .ops = &cros_ec_ops,
  184. };