cros_ec_lpc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Chromium OS cros_ec driver - LPC interface
  3. *
  4. * Copyright (c) 2012 The Chromium OS Authors.
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /*
  24. * The Matrix Keyboard Protocol driver handles talking to the keyboard
  25. * controller chip. Mostly this is for keyboard functions, but some other
  26. * things have slipped in, so we provide generic services to talk to the
  27. * KBC.
  28. */
  29. #include <common.h>
  30. #include <command.h>
  31. #include <cros_ec.h>
  32. #include <asm/io.h>
  33. #ifdef DEBUG_TRACE
  34. #define debug_trace(fmt, b...) debug(fmt, ##b)
  35. #else
  36. #define debug_trace(fmt, b...)
  37. #endif
  38. static int wait_for_sync(struct cros_ec_dev *dev)
  39. {
  40. unsigned long start;
  41. start = get_timer(0);
  42. while (inb(EC_LPC_ADDR_HOST_CMD) & EC_LPC_STATUS_BUSY_MASK) {
  43. if (get_timer(start) > 1000) {
  44. debug("%s: Timeout waiting for CROS_EC sync\n",
  45. __func__);
  46. return -1;
  47. }
  48. }
  49. return 0;
  50. }
  51. /**
  52. * Send a command to a LPC CROS_EC device and return the reply.
  53. *
  54. * The device's internal input/output buffers are used.
  55. *
  56. * @param dev CROS_EC device
  57. * @param cmd Command to send (EC_CMD_...)
  58. * @param cmd_version Version of command to send (EC_VER_...)
  59. * @param dout Output data (may be NULL If dout_len=0)
  60. * @param dout_len Size of output data in bytes
  61. * @param dinp Place to put pointer to response data
  62. * @param din_len Maximum size of response in bytes
  63. * @return number of bytes in response, or -1 on error
  64. */
  65. static int old_lpc_command(struct cros_ec_dev *dev, uint8_t cmd,
  66. const uint8_t *dout, int dout_len,
  67. uint8_t **dinp, int din_len)
  68. {
  69. int ret, i;
  70. if (dout_len > EC_OLD_PARAM_SIZE) {
  71. debug("%s: Cannot send %d bytes\n", __func__, dout_len);
  72. return -1;
  73. }
  74. if (din_len > EC_OLD_PARAM_SIZE) {
  75. debug("%s: Cannot receive %d bytes\n", __func__, din_len);
  76. return -1;
  77. }
  78. if (wait_for_sync(dev)) {
  79. debug("%s: Timeout waiting ready\n", __func__);
  80. return -1;
  81. }
  82. debug_trace("cmd: %02x, ", cmd);
  83. for (i = 0; i < dout_len; i++) {
  84. debug_trace("%02x ", dout[i]);
  85. outb(dout[i], EC_LPC_ADDR_OLD_PARAM + i);
  86. }
  87. outb(cmd, EC_LPC_ADDR_HOST_CMD);
  88. debug_trace("\n");
  89. if (wait_for_sync(dev)) {
  90. debug("%s: Timeout waiting ready\n", __func__);
  91. return -1;
  92. }
  93. ret = inb(EC_LPC_ADDR_HOST_DATA);
  94. if (ret) {
  95. debug("%s: CROS_EC result code %d\n", __func__, ret);
  96. return -ret;
  97. }
  98. debug_trace("resp: %02x, ", ret);
  99. for (i = 0; i < din_len; i++) {
  100. dev->din[i] = inb(EC_LPC_ADDR_OLD_PARAM + i);
  101. debug_trace("%02x ", dev->din[i]);
  102. }
  103. debug_trace("\n");
  104. *dinp = dev->din;
  105. return din_len;
  106. }
  107. int cros_ec_lpc_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
  108. const uint8_t *dout, int dout_len,
  109. uint8_t **dinp, int din_len)
  110. {
  111. const int cmd_addr = EC_LPC_ADDR_HOST_CMD;
  112. const int data_addr = EC_LPC_ADDR_HOST_DATA;
  113. const int args_addr = EC_LPC_ADDR_HOST_ARGS;
  114. const int param_addr = EC_LPC_ADDR_HOST_PARAM;
  115. struct ec_lpc_host_args args;
  116. uint8_t *d;
  117. int csum;
  118. int i;
  119. /* Fall back to old-style command interface if args aren't supported */
  120. if (!dev->cmd_version_is_supported)
  121. return old_lpc_command(dev, cmd, dout, dout_len, dinp,
  122. din_len);
  123. if (dout_len > EC_HOST_PARAM_SIZE) {
  124. debug("%s: Cannot send %d bytes\n", __func__, dout_len);
  125. return -1;
  126. }
  127. /* Fill in args */
  128. args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
  129. args.command_version = cmd_version;
  130. args.data_size = dout_len;
  131. /* Calculate checksum */
  132. csum = cmd + args.flags + args.command_version + args.data_size;
  133. for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++)
  134. csum += *d;
  135. args.checksum = (uint8_t)csum;
  136. if (wait_for_sync(dev)) {
  137. debug("%s: Timeout waiting ready\n", __func__);
  138. return -1;
  139. }
  140. /* Write args */
  141. for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
  142. outb(*d, args_addr + i);
  143. /* Write data, if any */
  144. debug_trace("cmd: %02x, ver: %02x", cmd, cmd_version);
  145. for (i = 0, d = (uint8_t *)dout; i < dout_len; i++, d++) {
  146. outb(*d, param_addr + i);
  147. debug_trace("%02x ", *d);
  148. }
  149. outb(cmd, cmd_addr);
  150. debug_trace("\n");
  151. if (wait_for_sync(dev)) {
  152. debug("%s: Timeout waiting for response\n", __func__);
  153. return -1;
  154. }
  155. /* Check result */
  156. i = inb(data_addr);
  157. if (i) {
  158. debug("%s: CROS_EC result code %d\n", __func__, i);
  159. return -i;
  160. }
  161. /* Read back args */
  162. for (i = 0, d = (uint8_t *)&args; i < sizeof(args); i++, d++)
  163. *d = inb(args_addr + i);
  164. /*
  165. * If EC didn't modify args flags, then somehow we sent a new-style
  166. * command to an old EC, which means it would have read its params
  167. * from the wrong place.
  168. */
  169. if (!(args.flags & EC_HOST_ARGS_FLAG_TO_HOST)) {
  170. debug("%s: CROS_EC protocol mismatch\n", __func__);
  171. return -EC_RES_INVALID_RESPONSE;
  172. }
  173. if (args.data_size > din_len) {
  174. debug("%s: CROS_EC returned too much data %d > %d\n",
  175. __func__, args.data_size, din_len);
  176. return -EC_RES_INVALID_RESPONSE;
  177. }
  178. /* Read data, if any */
  179. for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++) {
  180. *d = inb(param_addr + i);
  181. debug_trace("%02x ", *d);
  182. }
  183. debug_trace("\n");
  184. /* Verify checksum */
  185. csum = cmd + args.flags + args.command_version + args.data_size;
  186. for (i = 0, d = (uint8_t *)dev->din; i < args.data_size; i++, d++)
  187. csum += *d;
  188. if (args.checksum != (uint8_t)csum) {
  189. debug("%s: CROS_EC response has invalid checksum\n", __func__);
  190. return -EC_RES_INVALID_CHECKSUM;
  191. }
  192. *dinp = dev->din;
  193. /* Return actual amount of data received */
  194. return args.data_size;
  195. }
  196. /**
  197. * Initialize LPC protocol.
  198. *
  199. * @param dev CROS_EC device
  200. * @param blob Device tree blob
  201. * @return 0 if ok, -1 on error
  202. */
  203. int cros_ec_lpc_init(struct cros_ec_dev *dev, const void *blob)
  204. {
  205. int byte, i;
  206. /* See if we can find an EC at the other end */
  207. byte = 0xff;
  208. byte &= inb(EC_LPC_ADDR_HOST_CMD);
  209. byte &= inb(EC_LPC_ADDR_HOST_DATA);
  210. for (i = 0; i < EC_HOST_PARAM_SIZE && (byte == 0xff); i++)
  211. byte &= inb(EC_LPC_ADDR_HOST_PARAM + i);
  212. if (byte == 0xff) {
  213. debug("%s: CROS_EC device not found on LPC bus\n",
  214. __func__);
  215. return -1;
  216. }
  217. return 0;
  218. }
  219. /*
  220. * Test if LPC command args are supported.
  221. *
  222. * The cheapest way to do this is by looking for the memory-mapped
  223. * flag. This is faster than sending a new-style 'hello' command and
  224. * seeing whether the EC sets the EC_HOST_ARGS_FLAG_FROM_HOST flag
  225. * in args when it responds.
  226. */
  227. int cros_ec_lpc_check_version(struct cros_ec_dev *dev)
  228. {
  229. if (inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID) == 'E' &&
  230. inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID + 1)
  231. == 'C' &&
  232. (inb(EC_LPC_ADDR_MEMMAP +
  233. EC_MEMMAP_HOST_CMD_FLAGS) &
  234. EC_HOST_CMD_FLAG_LPC_ARGS_SUPPORTED)) {
  235. dev->cmd_version_is_supported = 1;
  236. } else {
  237. /* We are going to use the old IO ports */
  238. dev->cmd_version_is_supported = 0;
  239. }
  240. debug("lpc: version %s\n", dev->cmd_version_is_supported ?
  241. "new" : "old");
  242. return 0;
  243. }