cros_ec_lpc.c 5.2 KB

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