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 int cros_ec_uart_rx_bytes(struct serdev_device *serdev,
88 struct ec_host_response *host_response;
89 struct cros_ec_device *ec_dev = serdev_device_get_drvdata(serdev);
90 struct cros_ec_uart *ec_uart = ec_dev->priv;
91 struct response_info *resp = &ec_uart->response;
93 /* Check if bytes were sent out of band */
95 /* Discard all bytes */
96 dev_warn(ec_dev->dev, "Bytes received out of band, dropping them.\n");
101 * Check if incoming bytes + resp->size is greater than allocated
102 * buffer in din by cros_ec. This will ensure that if EC sends more
103 * bytes than max_size, waiting process will be notified with an error.
105 if (resp->size + count > resp->max_size) {
106 resp->status = -EMSGSIZE;
107 wake_up(&resp->wait_queue);
111 memcpy(resp->data + resp->size, data, count);
115 /* Read data_len if we received response header and if exp_len was not read before. */
116 if (resp->size >= sizeof(*host_response) && resp->exp_len == 0) {
117 host_response = (struct ec_host_response *)resp->data;
118 resp->exp_len = host_response->data_len + sizeof(*host_response);
121 /* If driver received response header and payload from EC, wake up the wait queue. */
122 if (resp->size >= sizeof(*host_response) && resp->size == resp->exp_len) {
124 wake_up(&resp->wait_queue);
130 static int cros_ec_uart_pkt_xfer(struct cros_ec_device *ec_dev,
131 struct cros_ec_command *ec_msg)
133 struct cros_ec_uart *ec_uart = ec_dev->priv;
134 struct serdev_device *serdev = ec_uart->serdev;
135 struct response_info *resp = &ec_uart->response;
136 struct ec_host_response *host_response;
141 len = cros_ec_prepare_tx(ec_dev, ec_msg);
142 dev_dbg(ec_dev->dev, "Prepared len=%d\n", len);
144 /* Setup for incoming response */
145 resp->data = ec_dev->din;
146 resp->max_size = ec_dev->din_size;
151 ret = serdev_device_write_buf(serdev, ec_dev->dout, len);
152 if (ret < 0 || ret < len) {
153 dev_err(ec_dev->dev, "Unable to write data\n");
159 ret = wait_event_timeout(resp->wait_queue, resp->status,
160 msecs_to_jiffies(EC_MSG_DEADLINE_MS));
162 dev_warn(ec_dev->dev, "Timed out waiting for response.\n");
167 if (resp->status < 0) {
169 dev_warn(ec_dev->dev, "Error response received: %d\n", ret);
173 host_response = (struct ec_host_response *)ec_dev->din;
174 ec_msg->result = host_response->result;
176 if (host_response->data_len > ec_msg->insize) {
177 dev_err(ec_dev->dev, "Resp too long (%d bytes, expected %d)\n",
178 host_response->data_len, ec_msg->insize);
183 /* Validate checksum */
185 for (i = 0; i < sizeof(*host_response) + host_response->data_len; i++)
186 sum += ec_dev->din[i];
189 dev_err(ec_dev->dev, "Bad packet checksum calculated %x\n", sum);
194 memcpy(ec_msg->data, ec_dev->din + sizeof(*host_response), host_response->data_len);
196 ret = host_response->data_len;
199 /* Invalidate response buffer to guard against out of band rx data */
202 if (ec_msg->command == EC_CMD_REBOOT_EC)
203 msleep(EC_REBOOT_DELAY_MS);
208 static int cros_ec_uart_resource(struct acpi_resource *ares, void *data)
210 struct cros_ec_uart *ec_uart = data;
211 struct acpi_resource_uart_serialbus *sb = &ares->data.uart_serial_bus;
213 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS &&
214 sb->type == ACPI_RESOURCE_SERIAL_TYPE_UART) {
215 ec_uart->baudrate = sb->default_baud_rate;
216 dev_dbg(&ec_uart->serdev->dev, "Baudrate %d\n", ec_uart->baudrate);
218 ec_uart->flowcontrol = sb->flow_control;
219 dev_dbg(&ec_uart->serdev->dev, "Flow control %d\n", ec_uart->flowcontrol);
225 static int cros_ec_uart_acpi_probe(struct cros_ec_uart *ec_uart)
228 LIST_HEAD(resources);
229 struct acpi_device *adev = ACPI_COMPANION(&ec_uart->serdev->dev);
231 ret = acpi_dev_get_resources(adev, &resources, cros_ec_uart_resource, ec_uart);
235 acpi_dev_free_resource_list(&resources);
237 /* Retrieve GpioInt and translate it to Linux IRQ number */
238 ret = acpi_dev_gpio_irq_get(adev, 0);
243 dev_dbg(&ec_uart->serdev->dev, "IRQ number %d\n", ec_uart->irq);
248 static const struct serdev_device_ops cros_ec_uart_client_ops = {
249 .receive_buf = cros_ec_uart_rx_bytes,
252 static int cros_ec_uart_probe(struct serdev_device *serdev)
254 struct device *dev = &serdev->dev;
255 struct cros_ec_device *ec_dev;
256 struct cros_ec_uart *ec_uart;
259 ec_uart = devm_kzalloc(dev, sizeof(*ec_uart), GFP_KERNEL);
263 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
267 ret = devm_serdev_device_open(dev, serdev);
269 dev_err(dev, "Unable to open UART device");
273 serdev_device_set_drvdata(serdev, ec_dev);
274 init_waitqueue_head(&ec_uart->response.wait_queue);
276 ec_uart->serdev = serdev;
278 ret = cros_ec_uart_acpi_probe(ec_uart);
280 dev_err(dev, "Failed to get ACPI info (%d)", ret);
284 ret = serdev_device_set_baudrate(serdev, ec_uart->baudrate);
286 dev_err(dev, "Failed to set up host baud rate (%d)", ret);
290 serdev_device_set_flow_control(serdev, ec_uart->flowcontrol);
292 /* Initialize ec_dev for cros_ec */
293 ec_dev->phys_name = dev_name(dev);
295 ec_dev->priv = ec_uart;
296 ec_dev->irq = ec_uart->irq;
297 ec_dev->cmd_xfer = NULL;
298 ec_dev->pkt_xfer = cros_ec_uart_pkt_xfer;
299 ec_dev->din_size = sizeof(struct ec_host_response) +
300 sizeof(struct ec_response_get_protocol_info);
301 ec_dev->dout_size = sizeof(struct ec_host_request);
303 serdev_device_set_client_ops(serdev, &cros_ec_uart_client_ops);
305 return cros_ec_register(ec_dev);
308 static void cros_ec_uart_remove(struct serdev_device *serdev)
310 struct cros_ec_device *ec_dev = serdev_device_get_drvdata(serdev);
312 cros_ec_unregister(ec_dev);
315 static int __maybe_unused cros_ec_uart_suspend(struct device *dev)
317 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
319 return cros_ec_suspend(ec_dev);
322 static int __maybe_unused cros_ec_uart_resume(struct device *dev)
324 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
326 return cros_ec_resume(ec_dev);
329 static SIMPLE_DEV_PM_OPS(cros_ec_uart_pm_ops, cros_ec_uart_suspend,
330 cros_ec_uart_resume);
332 static const struct of_device_id cros_ec_uart_of_match[] = {
333 { .compatible = "google,cros-ec-uart" },
336 MODULE_DEVICE_TABLE(of, cros_ec_uart_of_match);
339 static const struct acpi_device_id cros_ec_uart_acpi_id[] = {
344 MODULE_DEVICE_TABLE(acpi, cros_ec_uart_acpi_id);
347 static struct serdev_device_driver cros_ec_uart_driver = {
349 .name = "cros-ec-uart",
350 .acpi_match_table = ACPI_PTR(cros_ec_uart_acpi_id),
351 .of_match_table = cros_ec_uart_of_match,
352 .pm = &cros_ec_uart_pm_ops,
354 .probe = cros_ec_uart_probe,
355 .remove = cros_ec_uart_remove,
358 module_serdev_device_driver(cros_ec_uart_driver);
360 MODULE_LICENSE("GPL");
361 MODULE_DESCRIPTION("UART interface for ChromeOS Embedded Controller");