mailbox.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Mailbox interface for Wilco Embedded Controller
  4. *
  5. * Copyright 2018 Google LLC
  6. *
  7. * The Wilco EC is similar to a typical ChromeOS embedded controller.
  8. * It uses the same MEC based low-level communication and a similar
  9. * protocol, but with some important differences. The EC firmware does
  10. * not support the same mailbox commands so it is not registered as a
  11. * cros_ec device type.
  12. *
  13. * Most messages follow a standard format, but there are some exceptions
  14. * and an interface is provided to do direct/raw transactions that do not
  15. * make assumptions about byte placement.
  16. */
  17. #include <linux/delay.h>
  18. #include <linux/device.h>
  19. #include <linux/io.h>
  20. #include <linux/platform_data/wilco-ec.h>
  21. #include <linux/platform_device.h>
  22. #include "../cros_ec_lpc_mec.h"
  23. /* Version of mailbox interface */
  24. #define EC_MAILBOX_VERSION 0
  25. /* Command to start mailbox transaction */
  26. #define EC_MAILBOX_START_COMMAND 0xda
  27. /* Version of EC protocol */
  28. #define EC_MAILBOX_PROTO_VERSION 3
  29. /* Number of header bytes to be counted as data bytes */
  30. #define EC_MAILBOX_DATA_EXTRA 2
  31. /* Maximum timeout */
  32. #define EC_MAILBOX_TIMEOUT HZ
  33. /* EC response flags */
  34. #define EC_CMDR_DATA BIT(0) /* Data ready for host to read */
  35. #define EC_CMDR_PENDING BIT(1) /* Write pending to EC */
  36. #define EC_CMDR_BUSY BIT(2) /* EC is busy processing a command */
  37. #define EC_CMDR_CMD BIT(3) /* Last host write was a command */
  38. /**
  39. * wilco_ec_response_timed_out() - Wait for EC response.
  40. * @ec: EC device.
  41. *
  42. * Return: true if EC timed out, false if EC did not time out.
  43. */
  44. static bool wilco_ec_response_timed_out(struct wilco_ec_device *ec)
  45. {
  46. unsigned long timeout = jiffies + EC_MAILBOX_TIMEOUT;
  47. do {
  48. if (!(inb(ec->io_command->start) &
  49. (EC_CMDR_PENDING | EC_CMDR_BUSY)))
  50. return false;
  51. usleep_range(100, 200);
  52. } while (time_before(jiffies, timeout));
  53. return true;
  54. }
  55. /**
  56. * wilco_ec_checksum() - Compute 8-bit checksum over data range.
  57. * @data: Data to checksum.
  58. * @size: Number of bytes to checksum.
  59. *
  60. * Return: 8-bit checksum of provided data.
  61. */
  62. static u8 wilco_ec_checksum(const void *data, size_t size)
  63. {
  64. u8 *data_bytes = (u8 *)data;
  65. u8 checksum = 0;
  66. size_t i;
  67. for (i = 0; i < size; i++)
  68. checksum += data_bytes[i];
  69. return checksum;
  70. }
  71. /**
  72. * wilco_ec_prepare() - Prepare the request structure for the EC.
  73. * @msg: EC message with request information.
  74. * @rq: EC request structure to fill.
  75. */
  76. static void wilco_ec_prepare(struct wilco_ec_message *msg,
  77. struct wilco_ec_request *rq)
  78. {
  79. memset(rq, 0, sizeof(*rq));
  80. rq->struct_version = EC_MAILBOX_PROTO_VERSION;
  81. rq->mailbox_id = msg->type;
  82. rq->mailbox_version = EC_MAILBOX_VERSION;
  83. rq->data_size = msg->request_size;
  84. /* Checksum header and data */
  85. rq->checksum = wilco_ec_checksum(rq, sizeof(*rq));
  86. rq->checksum += wilco_ec_checksum(msg->request_data, msg->request_size);
  87. rq->checksum = -rq->checksum;
  88. }
  89. /**
  90. * wilco_ec_transfer() - Perform actual data transfer.
  91. * @ec: EC device.
  92. * @msg: EC message data for request and response.
  93. * @rq: Filled in request structure
  94. *
  95. * Context: ec->mailbox_lock should be held while using this function.
  96. * Return: number of bytes received or negative error code on failure.
  97. */
  98. static int wilco_ec_transfer(struct wilco_ec_device *ec,
  99. struct wilco_ec_message *msg,
  100. struct wilco_ec_request *rq)
  101. {
  102. struct wilco_ec_response *rs;
  103. int ret;
  104. u8 flag;
  105. /* Write request header, then data */
  106. ret = cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, 0, sizeof(*rq), (u8 *)rq);
  107. if (ret < 0)
  108. return ret;
  109. ret = cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE, sizeof(*rq), msg->request_size,
  110. msg->request_data);
  111. if (ret < 0)
  112. return ret;
  113. /* Start the command */
  114. outb(EC_MAILBOX_START_COMMAND, ec->io_command->start);
  115. /* For some commands (eg shutdown) the EC will not respond, that's OK */
  116. if (msg->flags & WILCO_EC_FLAG_NO_RESPONSE) {
  117. dev_dbg(ec->dev, "EC does not respond to this command\n");
  118. return 0;
  119. }
  120. /* Wait for it to complete */
  121. if (wilco_ec_response_timed_out(ec)) {
  122. dev_dbg(ec->dev, "response timed out\n");
  123. return -ETIMEDOUT;
  124. }
  125. /* Check result */
  126. flag = inb(ec->io_data->start);
  127. if (flag) {
  128. dev_dbg(ec->dev, "bad response: 0x%02x\n", flag);
  129. return -EIO;
  130. }
  131. /* Read back response */
  132. rs = ec->data_buffer;
  133. ret = cros_ec_lpc_io_bytes_mec(MEC_IO_READ, 0,
  134. sizeof(*rs) + EC_MAILBOX_DATA_SIZE,
  135. (u8 *)rs);
  136. if (ret < 0)
  137. return ret;
  138. if (ret) {
  139. dev_dbg(ec->dev, "bad packet checksum 0x%02x\n", rs->checksum);
  140. return -EBADMSG;
  141. }
  142. if (rs->result) {
  143. dev_dbg(ec->dev, "EC reported failure: 0x%02x\n", rs->result);
  144. return -EBADMSG;
  145. }
  146. if (rs->data_size != EC_MAILBOX_DATA_SIZE) {
  147. dev_dbg(ec->dev, "unexpected packet size (%u != %u)\n",
  148. rs->data_size, EC_MAILBOX_DATA_SIZE);
  149. return -EMSGSIZE;
  150. }
  151. if (rs->data_size < msg->response_size) {
  152. dev_dbg(ec->dev, "EC didn't return enough data (%u < %zu)\n",
  153. rs->data_size, msg->response_size);
  154. return -EMSGSIZE;
  155. }
  156. memcpy(msg->response_data, rs->data, msg->response_size);
  157. return rs->data_size;
  158. }
  159. /**
  160. * wilco_ec_mailbox() - Send EC request and receive EC response.
  161. * @ec: EC device.
  162. * @msg: EC message data for request and response.
  163. *
  164. * On entry msg->type, msg->request_size, and msg->request_data should all be
  165. * filled in. If desired, msg->flags can be set.
  166. *
  167. * If a response is expected, msg->response_size should be set, and
  168. * msg->response_data should point to a buffer with enough space. On exit
  169. * msg->response_data will be filled.
  170. *
  171. * Return: number of bytes received or negative error code on failure.
  172. */
  173. int wilco_ec_mailbox(struct wilco_ec_device *ec, struct wilco_ec_message *msg)
  174. {
  175. struct wilco_ec_request *rq;
  176. int ret;
  177. dev_dbg(ec->dev, "type=%04x flags=%02x rslen=%zu rqlen=%zu\n",
  178. msg->type, msg->flags, msg->response_size, msg->request_size);
  179. mutex_lock(&ec->mailbox_lock);
  180. /* Prepare request packet */
  181. rq = ec->data_buffer;
  182. wilco_ec_prepare(msg, rq);
  183. ret = wilco_ec_transfer(ec, msg, rq);
  184. mutex_unlock(&ec->mailbox_lock);
  185. return ret;
  186. }
  187. EXPORT_SYMBOL_GPL(wilco_ec_mailbox);