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 */ 57 struct 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 */ 76 struct cros_ec_uart { 77 struct serdev_device *serdev; 78 u32 baudrate; 79 u8 flowcontrol; 80 u32 irq; 81 struct response_info response; 82 }; 83 84 static size_t cros_ec_uart_rx_bytes(struct serdev_device *serdev, 85 const u8 *data, 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 < 0 || ret < len) { 152 dev_err(ec_dev->dev, "Unable to write data\n"); 153 if (ret >= 0) 154 ret = -EIO; 155 goto exit; 156 } 157 158 ret = wait_event_timeout(resp->wait_queue, resp->status, 159 msecs_to_jiffies(EC_MSG_DEADLINE_MS)); 160 if (ret == 0) { 161 dev_warn(ec_dev->dev, "Timed out waiting for response.\n"); 162 ret = -ETIMEDOUT; 163 goto exit; 164 } 165 166 if (resp->status < 0) { 167 ret = resp->status; 168 dev_warn(ec_dev->dev, "Error response received: %d\n", ret); 169 goto exit; 170 } 171 172 host_response = (struct ec_host_response *)ec_dev->din; 173 ec_msg->result = host_response->result; 174 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); 178 ret = -ENOSPC; 179 goto exit; 180 } 181 182 /* Validate checksum */ 183 sum = 0; 184 for (i = 0; i < sizeof(*host_response) + host_response->data_len; i++) 185 sum += ec_dev->din[i]; 186 187 if (sum) { 188 dev_err(ec_dev->dev, "Bad packet checksum calculated %x\n", sum); 189 ret = -EBADMSG; 190 goto exit; 191 } 192 193 memcpy(ec_msg->data, ec_dev->din + sizeof(*host_response), host_response->data_len); 194 195 ret = host_response->data_len; 196 197 exit: 198 /* Invalidate response buffer to guard against out of band rx data */ 199 resp->data = NULL; 200 201 if (ec_msg->command == EC_CMD_REBOOT_EC) 202 msleep(EC_REBOOT_DELAY_MS); 203 204 return ret; 205 } 206 207 static int cros_ec_uart_resource(struct acpi_resource *ares, void *data) 208 { 209 struct cros_ec_uart *ec_uart = data; 210 struct acpi_resource_uart_serialbus *sb = &ares->data.uart_serial_bus; 211 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); 216 217 ec_uart->flowcontrol = sb->flow_control; 218 dev_dbg(&ec_uart->serdev->dev, "Flow control %d\n", ec_uart->flowcontrol); 219 } 220 221 return 0; 222 } 223 224 static int cros_ec_uart_acpi_probe(struct cros_ec_uart *ec_uart) 225 { 226 int ret; 227 LIST_HEAD(resources); 228 struct acpi_device *adev = ACPI_COMPANION(&ec_uart->serdev->dev); 229 230 ret = acpi_dev_get_resources(adev, &resources, cros_ec_uart_resource, ec_uart); 231 if (ret < 0) 232 return ret; 233 234 acpi_dev_free_resource_list(&resources); 235 236 /* Retrieve GpioInt and translate it to Linux IRQ number */ 237 ret = acpi_dev_gpio_irq_get(adev, 0); 238 if (ret < 0) 239 return ret; 240 241 ec_uart->irq = ret; 242 dev_dbg(&ec_uart->serdev->dev, "IRQ number %d\n", ec_uart->irq); 243 244 return 0; 245 } 246 247 static const struct serdev_device_ops cros_ec_uart_client_ops = { 248 .receive_buf = cros_ec_uart_rx_bytes, 249 }; 250 251 static int cros_ec_uart_probe(struct serdev_device *serdev) 252 { 253 struct device *dev = &serdev->dev; 254 struct cros_ec_device *ec_dev; 255 struct cros_ec_uart *ec_uart; 256 int ret; 257 258 ec_uart = devm_kzalloc(dev, sizeof(*ec_uart), GFP_KERNEL); 259 if (!ec_uart) 260 return -ENOMEM; 261 262 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL); 263 if (!ec_dev) 264 return -ENOMEM; 265 266 ret = devm_serdev_device_open(dev, serdev); 267 if (ret) { 268 dev_err(dev, "Unable to open UART device"); 269 return ret; 270 } 271 272 serdev_device_set_drvdata(serdev, ec_dev); 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 serdev_device_set_client_ops(serdev, &cros_ec_uart_client_ops); 303 304 return cros_ec_register(ec_dev); 305 } 306 307 static void cros_ec_uart_remove(struct serdev_device *serdev) 308 { 309 struct cros_ec_device *ec_dev = serdev_device_get_drvdata(serdev); 310 311 cros_ec_unregister(ec_dev); 312 }; 313 314 static int __maybe_unused cros_ec_uart_suspend(struct device *dev) 315 { 316 struct cros_ec_device *ec_dev = dev_get_drvdata(dev); 317 318 return cros_ec_suspend(ec_dev); 319 } 320 321 static int __maybe_unused cros_ec_uart_resume(struct device *dev) 322 { 323 struct cros_ec_device *ec_dev = dev_get_drvdata(dev); 324 325 return cros_ec_resume(ec_dev); 326 } 327 328 static SIMPLE_DEV_PM_OPS(cros_ec_uart_pm_ops, cros_ec_uart_suspend, 329 cros_ec_uart_resume); 330 331 static const struct of_device_id cros_ec_uart_of_match[] = { 332 { .compatible = "google,cros-ec-uart" }, 333 {} 334 }; 335 MODULE_DEVICE_TABLE(of, cros_ec_uart_of_match); 336 337 #ifdef CONFIG_ACPI 338 static const struct acpi_device_id cros_ec_uart_acpi_id[] = { 339 { "GOOG0019", 0 }, 340 {} 341 }; 342 343 MODULE_DEVICE_TABLE(acpi, cros_ec_uart_acpi_id); 344 #endif 345 346 static struct serdev_device_driver cros_ec_uart_driver = { 347 .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, 352 }, 353 .probe = cros_ec_uart_probe, 354 .remove = cros_ec_uart_remove, 355 }; 356 357 module_serdev_device_driver(cros_ec_uart_driver); 358 359 MODULE_LICENSE("GPL"); 360 MODULE_DESCRIPTION("UART interface for ChromeOS Embedded Controller"); 361 MODULE_AUTHOR("Bhanu Prakash Maiya <bhanumaiya@chromium.org>"); 362