cros_ec_uart.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * UART interface for ChromeOS Embedded Controller
  4. *
  5. * Copyright 2020-2022 Google LLC.
  6. */
  7. #include <linux/acpi.h>
  8. #include <linux/delay.h>
  9. #include <linux/errno.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_data/cros_ec_proto.h>
  15. #include <linux/serdev.h>
  16. #include <linux/slab.h>
  17. #include <uapi/linux/sched/types.h>
  18. #include "cros_ec.h"
  19. /*
  20. * EC sends contiguous bytes of response packet on UART AP RX.
  21. * TTY driver in AP accumulates incoming bytes and calls the registered callback
  22. * function. Byte count can range from 1 to MAX bytes supported by EC.
  23. * This driver should wait for long time for all callbacks to be processed.
  24. * Considering the worst case scenario, wait for 500 msec. This timeout should
  25. * account for max latency and some additional guard time.
  26. * Best case: Entire packet is received in ~200 ms, wait queue will be released
  27. * and packet will be processed.
  28. * Worst case: TTY driver sends bytes in multiple callbacks. In this case this
  29. * driver will wait for ~1 sec beyond which it will timeout.
  30. * This timeout value should not exceed ~500 msec because in case if
  31. * EC_CMD_REBOOT_EC sent, high level driver should be able to intercept EC
  32. * in RO.
  33. */
  34. #define EC_MSG_DEADLINE_MS 500
  35. /**
  36. * struct response_info - Encapsulate EC response related
  37. * information for passing between function
  38. * cros_ec_uart_pkt_xfer() and cros_ec_uart_rx_bytes()
  39. * callback.
  40. * @data: Copy the data received from EC here.
  41. * @max_size: Max size allocated for the @data buffer. If the
  42. * received data exceeds this value, we log an error.
  43. * @size: Actual size of data received from EC. This is also
  44. * used to accumulate byte count with response is received
  45. * in dma chunks.
  46. * @exp_len: Expected bytes of response from EC including header.
  47. * @status: Re-init to 0 before sending a cmd. Updated to 1 when
  48. * a response is successfully received, or an error number
  49. * on failure.
  50. * @wait_queue: Wait queue EC response where the cros_ec sends request
  51. * to EC and waits
  52. */
  53. struct response_info {
  54. void *data;
  55. size_t max_size;
  56. size_t size;
  57. size_t exp_len;
  58. int status;
  59. wait_queue_head_t wait_queue;
  60. };
  61. /**
  62. * struct cros_ec_uart - information about a uart-connected EC
  63. *
  64. * @serdev: serdev uart device we are connected to.
  65. * @baudrate: UART baudrate of attached EC device.
  66. * @flowcontrol: UART flowcontrol of attached device.
  67. * @irq: Linux IRQ number of associated serial device.
  68. * @response: Response info passing between cros_ec_uart_pkt_xfer()
  69. * and cros_ec_uart_rx_bytes()
  70. */
  71. struct cros_ec_uart {
  72. struct serdev_device *serdev;
  73. u32 baudrate;
  74. u8 flowcontrol;
  75. u32 irq;
  76. struct response_info response;
  77. };
  78. static size_t cros_ec_uart_rx_bytes(struct serdev_device *serdev,
  79. const u8 *data, size_t count)
  80. {
  81. struct ec_host_response *host_response;
  82. struct cros_ec_device *ec_dev = serdev_device_get_drvdata(serdev);
  83. struct cros_ec_uart *ec_uart = ec_dev->priv;
  84. struct response_info *resp = &ec_uart->response;
  85. /* Check if bytes were sent out of band */
  86. if (!resp->data) {
  87. /* Discard all bytes */
  88. dev_warn(ec_dev->dev, "Bytes received out of band, dropping them.\n");
  89. return count;
  90. }
  91. /*
  92. * Check if incoming bytes + resp->size is greater than allocated
  93. * buffer in din by cros_ec. This will ensure that if EC sends more
  94. * bytes than max_size, waiting process will be notified with an error.
  95. */
  96. if (resp->size + count > resp->max_size) {
  97. resp->status = -EMSGSIZE;
  98. wake_up(&resp->wait_queue);
  99. return count;
  100. }
  101. memcpy(resp->data + resp->size, data, count);
  102. resp->size += count;
  103. /* Read data_len if we received response header and if exp_len was not read before. */
  104. if (resp->size >= sizeof(*host_response) && resp->exp_len == 0) {
  105. host_response = (struct ec_host_response *)resp->data;
  106. resp->exp_len = host_response->data_len + sizeof(*host_response);
  107. }
  108. /* If driver received response header and payload from EC, wake up the wait queue. */
  109. if (resp->size >= sizeof(*host_response) && resp->size == resp->exp_len) {
  110. resp->status = 1;
  111. wake_up(&resp->wait_queue);
  112. }
  113. return count;
  114. }
  115. static int cros_ec_uart_pkt_xfer(struct cros_ec_device *ec_dev,
  116. struct cros_ec_command *ec_msg)
  117. {
  118. struct cros_ec_uart *ec_uart = ec_dev->priv;
  119. struct serdev_device *serdev = ec_uart->serdev;
  120. struct response_info *resp = &ec_uart->response;
  121. struct ec_host_response *host_response;
  122. unsigned int len;
  123. int ret, i;
  124. u8 sum;
  125. len = cros_ec_prepare_tx(ec_dev, ec_msg);
  126. dev_dbg(ec_dev->dev, "Prepared len=%d\n", len);
  127. /* Setup for incoming response */
  128. resp->data = ec_dev->din;
  129. resp->max_size = ec_dev->din_size;
  130. resp->size = 0;
  131. resp->exp_len = 0;
  132. resp->status = 0;
  133. ret = serdev_device_write_buf(serdev, ec_dev->dout, len);
  134. if (ret < 0 || ret < len) {
  135. dev_err(ec_dev->dev, "Unable to write data\n");
  136. if (ret >= 0)
  137. ret = -EIO;
  138. goto exit;
  139. }
  140. ret = wait_event_timeout(resp->wait_queue, resp->status,
  141. msecs_to_jiffies(EC_MSG_DEADLINE_MS));
  142. if (ret == 0) {
  143. dev_warn(ec_dev->dev, "Timed out waiting for response.\n");
  144. ret = -ETIMEDOUT;
  145. goto exit;
  146. }
  147. if (resp->status < 0) {
  148. ret = resp->status;
  149. dev_warn(ec_dev->dev, "Error response received: %d\n", ret);
  150. goto exit;
  151. }
  152. host_response = (struct ec_host_response *)ec_dev->din;
  153. ec_msg->result = host_response->result;
  154. if (host_response->data_len > ec_msg->insize) {
  155. dev_err(ec_dev->dev, "Resp too long (%d bytes, expected %d)\n",
  156. host_response->data_len, ec_msg->insize);
  157. ret = -ENOSPC;
  158. goto exit;
  159. }
  160. /* Validate checksum */
  161. sum = 0;
  162. for (i = 0; i < sizeof(*host_response) + host_response->data_len; i++)
  163. sum += ec_dev->din[i];
  164. if (sum) {
  165. dev_err(ec_dev->dev, "Bad packet checksum calculated %x\n", sum);
  166. ret = -EBADMSG;
  167. goto exit;
  168. }
  169. memcpy(ec_msg->data, ec_dev->din + sizeof(*host_response), host_response->data_len);
  170. ret = host_response->data_len;
  171. exit:
  172. /* Invalidate response buffer to guard against out of band rx data */
  173. resp->data = NULL;
  174. if (ec_msg->command == EC_CMD_REBOOT_EC)
  175. msleep(EC_REBOOT_DELAY_MS);
  176. return ret;
  177. }
  178. static int cros_ec_uart_resource(struct acpi_resource *ares, void *data)
  179. {
  180. struct cros_ec_uart *ec_uart = data;
  181. struct acpi_resource_uart_serialbus *sb = &ares->data.uart_serial_bus;
  182. if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS &&
  183. sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
  184. ec_uart->baudrate = sb->default_baud_rate;
  185. dev_dbg(&ec_uart->serdev->dev, "Baudrate %d\n", ec_uart->baudrate);
  186. ec_uart->flowcontrol = sb->flow_control;
  187. dev_dbg(&ec_uart->serdev->dev, "Flow control %d\n", ec_uart->flowcontrol);
  188. }
  189. return 0;
  190. }
  191. static int cros_ec_uart_acpi_probe(struct cros_ec_uart *ec_uart)
  192. {
  193. int ret;
  194. LIST_HEAD(resources);
  195. struct acpi_device *adev = ACPI_COMPANION(&ec_uart->serdev->dev);
  196. ret = acpi_dev_get_resources(adev, &resources, cros_ec_uart_resource, ec_uart);
  197. if (ret < 0)
  198. return ret;
  199. acpi_dev_free_resource_list(&resources);
  200. /* Retrieve GpioInt and translate it to Linux IRQ number */
  201. ret = acpi_dev_gpio_irq_get(adev, 0);
  202. if (ret < 0)
  203. return ret;
  204. ec_uart->irq = ret;
  205. dev_dbg(&ec_uart->serdev->dev, "IRQ number %d\n", ec_uart->irq);
  206. return 0;
  207. }
  208. static const struct serdev_device_ops cros_ec_uart_client_ops = {
  209. .receive_buf = cros_ec_uart_rx_bytes,
  210. };
  211. static int cros_ec_uart_probe(struct serdev_device *serdev)
  212. {
  213. struct device *dev = &serdev->dev;
  214. struct cros_ec_device *ec_dev;
  215. struct cros_ec_uart *ec_uart;
  216. int ret;
  217. ec_uart = devm_kzalloc(dev, sizeof(*ec_uart), GFP_KERNEL);
  218. if (!ec_uart)
  219. return -ENOMEM;
  220. ec_dev = cros_ec_device_alloc(dev);
  221. if (!ec_dev)
  222. return -ENOMEM;
  223. serdev_device_set_drvdata(serdev, ec_dev);
  224. init_waitqueue_head(&ec_uart->response.wait_queue);
  225. ec_uart->serdev = serdev;
  226. ret = cros_ec_uart_acpi_probe(ec_uart);
  227. if (ret < 0) {
  228. dev_err(dev, "Failed to get ACPI info (%d)", ret);
  229. return ret;
  230. }
  231. /* Initialize ec_dev for cros_ec */
  232. ec_dev->phys_name = dev_name(dev);
  233. ec_dev->priv = ec_uart;
  234. ec_dev->irq = ec_uart->irq;
  235. ec_dev->cmd_xfer = NULL;
  236. ec_dev->pkt_xfer = cros_ec_uart_pkt_xfer;
  237. serdev_device_set_client_ops(serdev, &cros_ec_uart_client_ops);
  238. ret = devm_serdev_device_open(dev, serdev);
  239. if (ret) {
  240. dev_err(dev, "Unable to open UART device");
  241. return ret;
  242. }
  243. ret = serdev_device_set_baudrate(serdev, ec_uart->baudrate);
  244. if (ret < 0) {
  245. dev_err(dev, "Failed to set up host baud rate (%d)", ret);
  246. return ret;
  247. }
  248. serdev_device_set_flow_control(serdev, ec_uart->flowcontrol);
  249. return cros_ec_register(ec_dev);
  250. }
  251. static void cros_ec_uart_remove(struct serdev_device *serdev)
  252. {
  253. struct cros_ec_device *ec_dev = serdev_device_get_drvdata(serdev);
  254. cros_ec_unregister(ec_dev);
  255. };
  256. static int __maybe_unused cros_ec_uart_suspend(struct device *dev)
  257. {
  258. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  259. return cros_ec_suspend(ec_dev);
  260. }
  261. static int __maybe_unused cros_ec_uart_resume(struct device *dev)
  262. {
  263. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  264. return cros_ec_resume(ec_dev);
  265. }
  266. static SIMPLE_DEV_PM_OPS(cros_ec_uart_pm_ops, cros_ec_uart_suspend,
  267. cros_ec_uart_resume);
  268. static const struct of_device_id cros_ec_uart_of_match[] = {
  269. { .compatible = "google,cros-ec-uart" },
  270. {}
  271. };
  272. MODULE_DEVICE_TABLE(of, cros_ec_uart_of_match);
  273. #ifdef CONFIG_ACPI
  274. static const struct acpi_device_id cros_ec_uart_acpi_id[] = {
  275. { "GOOG0019", 0 },
  276. {}
  277. };
  278. MODULE_DEVICE_TABLE(acpi, cros_ec_uart_acpi_id);
  279. #endif
  280. static struct serdev_device_driver cros_ec_uart_driver = {
  281. .driver = {
  282. .name = "cros-ec-uart",
  283. .acpi_match_table = ACPI_PTR(cros_ec_uart_acpi_id),
  284. .of_match_table = cros_ec_uart_of_match,
  285. .pm = &cros_ec_uart_pm_ops,
  286. },
  287. .probe = cros_ec_uart_probe,
  288. .remove = cros_ec_uart_remove,
  289. };
  290. module_serdev_device_driver(cros_ec_uart_driver);
  291. MODULE_LICENSE("GPL");
  292. MODULE_DESCRIPTION("UART interface for ChromeOS Embedded Controller");
  293. MODULE_AUTHOR("Bhanu Prakash Maiya <bhanumaiya@chromium.org>");