1 // SPDX-License-Identifier: GPL-2.0-only
3 * UART interface for ChromeOS Embedded Controller
5 * Copyright 2020-2022 Google LLC.
8 #include <linux/acpi.h>
9 #include <linux/delay.h>
10 #include <linux/errno.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/platform_data/cros_ec_proto.h>
16 #include <linux/serdev.h>
17 #include <linux/slab.h>
18 #include <uapi/linux/sched/types.h>
23 * EC sends contiguous bytes of response packet on UART AP RX.
24 * TTY driver in AP accumulates incoming bytes and calls the registered callback
25 * function. Byte count can range from 1 to MAX bytes supported by EC.
26 * This driver should wait for long time for all callbacks to be processed.
27 * Considering the worst case scenario, wait for 500 msec. This timeout should
28 * account for max latency and some additional guard time.
29 * Best case: Entire packet is received in ~200 ms, wait queue will be released
30 * and packet will be processed.
31 * Worst case: TTY driver sends bytes in multiple callbacks. In this case this
32 * driver will wait for ~1 sec beyond which it will timeout.
33 * This timeout value should not exceed ~500 msec because in case if
34 * EC_CMD_REBOOT_EC sent, high level driver should be able to intercept EC
37 #define EC_MSG_DEADLINE_MS 500
40 * struct response_info - Encapsulate EC response related
41 * information for passing between function
42 * cros_ec_uart_pkt_xfer() and cros_ec_uart_rx_bytes()
44 * @data: Copy the data received from EC here.
45 * @max_size: Max size allocated for the @data buffer. If the
46 * received data exceeds this value, we log an error.
47 * @size: Actual size of data received from EC. This is also
48 * used to accumulate byte count with response is received
50 * @exp_len: Expected bytes of response from EC including header.
51 * @status: Re-init to 0 before sending a cmd. Updated to 1 when
52 * a response is successfully received, or an error number
54 * @wait_queue: Wait queue EC response where the cros_ec sends request
57 struct response_info {
63 wait_queue_head_t wait_queue;
67 * struct cros_ec_uart - information about a uart-connected EC
69 * @serdev: serdev uart device we are connected to.
70 * @baudrate: UART baudrate of attached EC device.
71 * @flowcontrol: UART flowcontrol of attached device.
72 * @irq: Linux IRQ number of associated serial device.
73 * @response: Response info passing between cros_ec_uart_pkt_xfer()
74 * and cros_ec_uart_rx_bytes()
77 struct serdev_device *serdev;
81 struct response_info response;
84 static size_t cros_ec_uart_rx_bytes(struct serdev_device *serdev,
85 const u8 *data, size_t count)
87 struct ec_host_response *host_response;
88 struct cros_ec_device *ec_dev = serdev_device_get_drvdata(serdev);
89 struct cros_ec_uart *ec_uart = ec_dev->priv;
90 struct response_info *resp = &ec_uart->response;
92 /* Check if bytes were sent out of band */
94 /* Discard all bytes */
95 dev_warn(ec_dev->dev, "Bytes received out of band, dropping them.\n");
100 * Check if incoming bytes + resp->size is greater than allocated
101 * buffer in din by cros_ec. This will ensure that if EC sends more
102 * bytes than max_size, waiting process will be notified with an error.
104 if (resp->size + count > resp->max_size) {
105 resp->status = -EMSGSIZE;
106 wake_up(&resp->wait_queue);
110 memcpy(resp->data + resp->size, data, count);
114 /* Read data_len if we received response header and if exp_len was not read before. */
115 if (resp->size >= sizeof(*host_response) && resp->exp_len == 0) {
116 host_response = (struct ec_host_response *)resp->data;
117 resp->exp_len = host_response->data_len + sizeof(*host_response);
120 /* If driver received response header and payload from EC, wake up the wait queue. */
121 if (resp->size >= sizeof(*host_response) && resp->size == resp->exp_len) {
123 wake_up(&resp->wait_queue);
129 static int cros_ec_uart_pkt_xfer(struct cros_ec_device *ec_dev,
130 struct cros_ec_command *ec_msg)
132 struct cros_ec_uart *ec_uart = ec_dev->priv;
133 struct serdev_device *serdev = ec_uart->serdev;
134 struct response_info *resp = &ec_uart->response;
135 struct ec_host_response *host_response;
140 len = cros_ec_prepare_tx(ec_dev, ec_msg);
141 dev_dbg(ec_dev->dev, "Prepared len=%d\n", len);
143 /* Setup for incoming response */
144 resp->data = ec_dev->din;
145 resp->max_size = ec_dev->din_size;
150 ret = serdev_device_write_buf(serdev, ec_dev->dout, len);
151 if (ret < 0 || ret < len) {
152 dev_err(ec_dev->dev, "Unable to write data\n");
158 ret = wait_event_timeout(resp->wait_queue, resp->status,
159 msecs_to_jiffies(EC_MSG_DEADLINE_MS));
161 dev_warn(ec_dev->dev, "Timed out waiting for response.\n");
166 if (resp->status < 0) {
168 dev_warn(ec_dev->dev, "Error response received: %d\n", ret);
172 host_response = (struct ec_host_response *)ec_dev->din;
173 ec_msg->result = host_response->result;
175 if (host_response->data_len > ec_msg->insize) {
176 dev_err(ec_dev->dev, "Resp too long (%d bytes, expected %d)\n",
177 host_response->data_len, ec_msg->insize);
182 /* Validate checksum */
184 for (i = 0; i < sizeof(*host_response) + host_response->data_len; i++)
185 sum += ec_dev->din[i];
188 dev_err(ec_dev->dev, "Bad packet checksum calculated %x\n", sum);
193 memcpy(ec_msg->data, ec_dev->din + sizeof(*host_response), host_response->data_len);
195 ret = host_response->data_len;
198 /* Invalidate response buffer to guard against out of band rx data */
201 if (ec_msg->command == EC_CMD_REBOOT_EC)
202 msleep(EC_REBOOT_DELAY_MS);
207 static int cros_ec_uart_resource(struct acpi_resource *ares, void *data)
209 struct cros_ec_uart *ec_uart = data;
210 struct acpi_resource_uart_serialbus *sb = &ares->data.uart_serial_bus;
212 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS &&
213 sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
214 ec_uart->baudrate = sb->default_baud_rate;
215 dev_dbg(&ec_uart->serdev->dev, "Baudrate %d\n", ec_uart->baudrate);
217 ec_uart->flowcontrol = sb->flow_control;
218 dev_dbg(&ec_uart->serdev->dev, "Flow control %d\n", ec_uart->flowcontrol);
224 static int cros_ec_uart_acpi_probe(struct cros_ec_uart *ec_uart)
227 LIST_HEAD(resources);
228 struct acpi_device *adev = ACPI_COMPANION(&ec_uart->serdev->dev);
230 ret = acpi_dev_get_resources(adev, &resources, cros_ec_uart_resource, ec_uart);
234 acpi_dev_free_resource_list(&resources);
236 /* Retrieve GpioInt and translate it to Linux IRQ number */
237 ret = acpi_dev_gpio_irq_get(adev, 0);
242 dev_dbg(&ec_uart->serdev->dev, "IRQ number %d\n", ec_uart->irq);
247 static const struct serdev_device_ops cros_ec_uart_client_ops = {
248 .receive_buf = cros_ec_uart_rx_bytes,
251 static int cros_ec_uart_probe(struct serdev_device *serdev)
253 struct device *dev = &serdev->dev;
254 struct cros_ec_device *ec_dev;
255 struct cros_ec_uart *ec_uart;
258 ec_uart = devm_kzalloc(dev, sizeof(*ec_uart), GFP_KERNEL);
262 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
266 serdev_device_set_drvdata(serdev, ec_dev);
267 init_waitqueue_head(&ec_uart->response.wait_queue);
269 ec_uart->serdev = serdev;
271 ret = cros_ec_uart_acpi_probe(ec_uart);
273 dev_err(dev, "Failed to get ACPI info (%d)", ret);
277 /* Initialize ec_dev for cros_ec */
278 ec_dev->phys_name = dev_name(dev);
280 ec_dev->priv = ec_uart;
281 ec_dev->irq = ec_uart->irq;
282 ec_dev->cmd_xfer = NULL;
283 ec_dev->pkt_xfer = cros_ec_uart_pkt_xfer;
284 ec_dev->din_size = sizeof(struct ec_host_response) +
285 sizeof(struct ec_response_get_protocol_info);
286 ec_dev->dout_size = sizeof(struct ec_host_request) + sizeof(struct ec_params_rwsig_action);
288 serdev_device_set_client_ops(serdev, &cros_ec_uart_client_ops);
290 ret = devm_serdev_device_open(dev, serdev);
292 dev_err(dev, "Unable to open UART device");
296 ret = serdev_device_set_baudrate(serdev, ec_uart->baudrate);
298 dev_err(dev, "Failed to set up host baud rate (%d)", ret);
302 serdev_device_set_flow_control(serdev, ec_uart->flowcontrol);
304 return cros_ec_register(ec_dev);
307 static void cros_ec_uart_remove(struct serdev_device *serdev)
309 struct cros_ec_device *ec_dev = serdev_device_get_drvdata(serdev);
311 cros_ec_unregister(ec_dev);
314 static int __maybe_unused cros_ec_uart_suspend(struct device *dev)
316 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
318 return cros_ec_suspend(ec_dev);
321 static int __maybe_unused cros_ec_uart_resume(struct device *dev)
323 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
325 return cros_ec_resume(ec_dev);
328 static SIMPLE_DEV_PM_OPS(cros_ec_uart_pm_ops, cros_ec_uart_suspend,
329 cros_ec_uart_resume);
331 static const struct of_device_id cros_ec_uart_of_match[] = {
332 { .compatible = "google,cros-ec-uart" },
335 MODULE_DEVICE_TABLE(of, cros_ec_uart_of_match);
338 static const struct acpi_device_id cros_ec_uart_acpi_id[] = {
343 MODULE_DEVICE_TABLE(acpi, cros_ec_uart_acpi_id);
346 static struct serdev_device_driver cros_ec_uart_driver = {
348 .name = "cros-ec-uart",
349 .acpi_match_table = ACPI_PTR(cros_ec_uart_acpi_id),
350 .of_match_table = cros_ec_uart_of_match,
351 .pm = &cros_ec_uart_pm_ops,
353 .probe = cros_ec_uart_probe,
354 .remove = cros_ec_uart_remove,
357 module_serdev_device_driver(cros_ec_uart_driver);
359 MODULE_LICENSE("GPL");
360 MODULE_DESCRIPTION("UART interface for ChromeOS Embedded Controller");