cros_ec_lpc.c 4.7 KB

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