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