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
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>
14#include <linux/of.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>
19
20#include "cros_ec.h"
21
22/*
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
35 * in RO.
36 */
37#define EC_MSG_DEADLINE_MS		500
38
39/**
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()
43 *			callback.
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
49 *			in dma chunks.
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
53 *			on failure.
54 * @wait_queue:	Wait queue EC response where the cros_ec sends request
55 *			to EC and waits
56 */
57struct response_info {
58	void *data;
59	size_t max_size;
60	size_t size;
61	size_t exp_len;
62	int status;
63	wait_queue_head_t wait_queue;
64};
65
66/**
67 * struct cros_ec_uart - information about a uart-connected EC
68 *
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()
75 */
76struct cros_ec_uart {
77	struct serdev_device *serdev;
78	u32 baudrate;
79	u8 flowcontrol;
80	u32 irq;
81	struct response_info response;
82};
83
84static int cros_ec_uart_rx_bytes(struct serdev_device *serdev,
85				 const u8 *data,
86				 size_t count)
87{
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;
92
93	/* Check if bytes were sent out of band */
94	if (!resp->data) {
95		/* Discard all bytes */
96		dev_warn(ec_dev->dev, "Bytes received out of band, dropping them.\n");
97		return count;
98	}
99
100	/*
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.
104	 */
105	if (resp->size + count > resp->max_size) {
106		resp->status = -EMSGSIZE;
107		wake_up(&resp->wait_queue);
108		return count;
109	}
110
111	memcpy(resp->data + resp->size, data, count);
112
113	resp->size += count;
114
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);
119	}
120
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) {
123		resp->status = 1;
124		wake_up(&resp->wait_queue);
125	}
126
127	return count;
128}
129
130static int cros_ec_uart_pkt_xfer(struct cros_ec_device *ec_dev,
131				 struct cros_ec_command *ec_msg)
132{
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;
137	unsigned int len;
138	int ret, i;
139	u8 sum;
140
141	len = cros_ec_prepare_tx(ec_dev, ec_msg);
142	dev_dbg(ec_dev->dev, "Prepared len=%d\n", len);
143
144	/* Setup for incoming response */
145	resp->data = ec_dev->din;
146	resp->max_size = ec_dev->din_size;
147	resp->size = 0;
148	resp->exp_len = 0;
149	resp->status = 0;
150
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");
154		if (ret >= 0)
155			ret = -EIO;
156		goto exit;
157	}
158
159	ret = wait_event_timeout(resp->wait_queue, resp->status,
160				 msecs_to_jiffies(EC_MSG_DEADLINE_MS));
161	if (ret == 0) {
162		dev_warn(ec_dev->dev, "Timed out waiting for response.\n");
163		ret = -ETIMEDOUT;
164		goto exit;
165	}
166
167	if (resp->status < 0) {
168		ret = resp->status;
169		dev_warn(ec_dev->dev, "Error response received: %d\n", ret);
170		goto exit;
171	}
172
173	host_response = (struct ec_host_response *)ec_dev->din;
174	ec_msg->result = host_response->result;
175
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);
179		ret = -ENOSPC;
180		goto exit;
181	}
182
183	/* Validate checksum */
184	sum = 0;
185	for (i = 0; i < sizeof(*host_response) + host_response->data_len; i++)
186		sum += ec_dev->din[i];
187
188	if (sum) {
189		dev_err(ec_dev->dev, "Bad packet checksum calculated %x\n", sum);
190		ret = -EBADMSG;
191		goto exit;
192	}
193
194	memcpy(ec_msg->data, ec_dev->din + sizeof(*host_response), host_response->data_len);
195
196	ret = host_response->data_len;
197
198exit:
199	/* Invalidate response buffer to guard against out of band rx data */
200	resp->data = NULL;
201
202	if (ec_msg->command == EC_CMD_REBOOT_EC)
203		msleep(EC_REBOOT_DELAY_MS);
204
205	return ret;
206}
207
208static int cros_ec_uart_resource(struct acpi_resource *ares, void *data)
209{
210	struct cros_ec_uart *ec_uart = data;
211	struct acpi_resource_uart_serialbus *sb = &ares->data.uart_serial_bus;
212
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);
217
218		ec_uart->flowcontrol = sb->flow_control;
219		dev_dbg(&ec_uart->serdev->dev, "Flow control %d\n", ec_uart->flowcontrol);
220	}
221
222	return 0;
223}
224
225static int cros_ec_uart_acpi_probe(struct cros_ec_uart *ec_uart)
226{
227	int ret;
228	LIST_HEAD(resources);
229	struct acpi_device *adev = ACPI_COMPANION(&ec_uart->serdev->dev);
230
231	ret = acpi_dev_get_resources(adev, &resources, cros_ec_uart_resource, ec_uart);
232	if (ret < 0)
233		return ret;
234
235	acpi_dev_free_resource_list(&resources);
236
237	/* Retrieve GpioInt and translate it to Linux IRQ number */
238	ret = acpi_dev_gpio_irq_get(adev, 0);
239	if (ret < 0)
240		return ret;
241
242	ec_uart->irq = ret;
243	dev_dbg(&ec_uart->serdev->dev, "IRQ number %d\n", ec_uart->irq);
244
245	return 0;
246}
247
248static const struct serdev_device_ops cros_ec_uart_client_ops = {
249	.receive_buf = cros_ec_uart_rx_bytes,
250};
251
252static int cros_ec_uart_probe(struct serdev_device *serdev)
253{
254	struct device *dev = &serdev->dev;
255	struct cros_ec_device *ec_dev;
256	struct cros_ec_uart *ec_uart;
257	int ret;
258
259	ec_uart = devm_kzalloc(dev, sizeof(*ec_uart), GFP_KERNEL);
260	if (!ec_uart)
261		return -ENOMEM;
262
263	ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
264	if (!ec_dev)
265		return -ENOMEM;
266
267	ret = devm_serdev_device_open(dev, serdev);
268	if (ret) {
269		dev_err(dev, "Unable to open UART device");
270		return ret;
271	}
272
273	serdev_device_set_drvdata(serdev, ec_dev);
274	init_waitqueue_head(&ec_uart->response.wait_queue);
275
276	ec_uart->serdev = serdev;
277
278	ret = cros_ec_uart_acpi_probe(ec_uart);
279	if (ret < 0) {
280		dev_err(dev, "Failed to get ACPI info (%d)", ret);
281		return ret;
282	}
283
284	ret = serdev_device_set_baudrate(serdev, ec_uart->baudrate);
285	if (ret < 0) {
286		dev_err(dev, "Failed to set up host baud rate (%d)", ret);
287		return ret;
288	}
289
290	serdev_device_set_flow_control(serdev, ec_uart->flowcontrol);
291
292	/* Initialize ec_dev for cros_ec  */
293	ec_dev->phys_name = dev_name(dev);
294	ec_dev->dev = 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);
302
303	serdev_device_set_client_ops(serdev, &cros_ec_uart_client_ops);
304
305	return cros_ec_register(ec_dev);
306}
307
308static void cros_ec_uart_remove(struct serdev_device *serdev)
309{
310	struct cros_ec_device *ec_dev = serdev_device_get_drvdata(serdev);
311
312	cros_ec_unregister(ec_dev);
313};
314
315static int __maybe_unused cros_ec_uart_suspend(struct device *dev)
316{
317	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
318
319	return cros_ec_suspend(ec_dev);
320}
321
322static int __maybe_unused cros_ec_uart_resume(struct device *dev)
323{
324	struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
325
326	return cros_ec_resume(ec_dev);
327}
328
329static SIMPLE_DEV_PM_OPS(cros_ec_uart_pm_ops, cros_ec_uart_suspend,
330			 cros_ec_uart_resume);
331
332static const struct of_device_id cros_ec_uart_of_match[] = {
333	{ .compatible = "google,cros-ec-uart" },
334	{}
335};
336MODULE_DEVICE_TABLE(of, cros_ec_uart_of_match);
337
338#ifdef CONFIG_ACPI
339static const struct acpi_device_id cros_ec_uart_acpi_id[] = {
340	{ "GOOG0019", 0 },
341	{}
342};
343
344MODULE_DEVICE_TABLE(acpi, cros_ec_uart_acpi_id);
345#endif
346
347static struct serdev_device_driver cros_ec_uart_driver = {
348	.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,
353	},
354	.probe		= cros_ec_uart_probe,
355	.remove		= cros_ec_uart_remove,
356};
357
358module_serdev_device_driver(cros_ec_uart_driver);
359
360MODULE_LICENSE("GPL");
361MODULE_DESCRIPTION("UART interface for ChromeOS Embedded Controller");
362MODULE_AUTHOR("Bhanu Prakash Maiya <bhanumaiya@chromium.org>");
363