1 // SPDX-License-Identifier: GPL-2.0
2 // LPC interface for ChromeOS Embedded Controller
4 // Copyright (C) 2012-2015 Google, Inc
6 // This driver uses the ChromeOS EC byte-level message-based protocol for
7 // communicating the keyboard state (which keys are pressed) from a keyboard EC
8 // to the AP over some bus (such as i2c, lpc, spi). The EC does debouncing,
9 // but everything else (including deghosting) is done here. The main
10 // motivation for this is to keep the EC firmware as simple as possible, since
11 // it cannot be easily upgraded and EC flash/IRAM space is relatively
14 #include <linux/acpi.h>
15 #include <linux/dmi.h>
16 #include <linux/delay.h>
18 #include <linux/interrupt.h>
19 #include <linux/module.h>
20 #include <linux/platform_data/cros_ec_commands.h>
21 #include <linux/platform_data/cros_ec_proto.h>
22 #include <linux/platform_device.h>
23 #include <linux/printk.h>
24 #include <linux/reboot.h>
25 #include <linux/suspend.h>
28 #include "cros_ec_lpc_mec.h"
30 #define DRV_NAME "cros_ec_lpcs"
31 #define ACPI_DRV_NAME "GOOG0004"
33 /* True if ACPI device is present */
34 static bool cros_ec_lpc_acpi_device_found;
37 * struct lpc_driver_ops - LPC driver operations
38 * @read: Copy length bytes from EC address offset into buffer dest. Returns
39 * the 8-bit checksum of all bytes read.
40 * @write: Copy length bytes from buffer msg into EC address offset. Returns
41 * the 8-bit checksum of all bytes written.
43 struct lpc_driver_ops {
44 u8 (*read)(unsigned int offset, unsigned int length, u8 *dest);
45 u8 (*write)(unsigned int offset, unsigned int length, const u8 *msg);
48 static struct lpc_driver_ops cros_ec_lpc_ops = { };
51 * A generic instance of the read function of struct lpc_driver_ops, used for
54 static u8 cros_ec_lpc_read_bytes(unsigned int offset, unsigned int length,
60 for (i = 0; i < length; ++i) {
61 dest[i] = inb(offset + i);
65 /* Return checksum of all bytes read */
70 * A generic instance of the write function of struct lpc_driver_ops, used for
73 static u8 cros_ec_lpc_write_bytes(unsigned int offset, unsigned int length,
79 for (i = 0; i < length; ++i) {
80 outb(msg[i], offset + i);
84 /* Return checksum of all bytes written */
89 * An instance of the read function of struct lpc_driver_ops, used for the
90 * MEC variant of LPC EC.
92 static u8 cros_ec_lpc_mec_read_bytes(unsigned int offset, unsigned int length,
95 int in_range = cros_ec_lpc_mec_in_range(offset, length);
101 cros_ec_lpc_io_bytes_mec(MEC_IO_READ,
102 offset - EC_HOST_CMD_REGION0,
104 cros_ec_lpc_read_bytes(offset, length, dest);
108 * An instance of the write function of struct lpc_driver_ops, used for the
109 * MEC variant of LPC EC.
111 static u8 cros_ec_lpc_mec_write_bytes(unsigned int offset, unsigned int length,
114 int in_range = cros_ec_lpc_mec_in_range(offset, length);
120 cros_ec_lpc_io_bytes_mec(MEC_IO_WRITE,
121 offset - EC_HOST_CMD_REGION0,
123 cros_ec_lpc_write_bytes(offset, length, msg);
126 static int ec_response_timed_out(void)
128 unsigned long one_second = jiffies + HZ;
131 usleep_range(200, 300);
133 if (!(cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_CMD, 1, &data) &
134 EC_LPC_STATUS_BUSY_MASK))
136 usleep_range(100, 200);
137 } while (time_before(jiffies, one_second));
142 static int cros_ec_pkt_xfer_lpc(struct cros_ec_device *ec,
143 struct cros_ec_command *msg)
145 struct ec_host_response response;
150 ret = cros_ec_prepare_tx(ec, msg);
155 cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_PACKET, ret, ec->dout);
158 sum = EC_COMMAND_PROTOCOL_3;
159 cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_CMD, 1, &sum);
161 if (ec_response_timed_out()) {
162 dev_warn(ec->dev, "EC response timed out\n");
168 msg->result = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_DATA, 1, &sum);
169 ret = cros_ec_check_result(ec, msg);
173 /* Read back response */
174 dout = (u8 *)&response;
175 sum = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PACKET, sizeof(response),
178 msg->result = response.result;
180 if (response.data_len > msg->insize) {
182 "packet too long (%d bytes, expected %d)",
183 response.data_len, msg->insize);
188 /* Read response and process checksum */
189 sum += cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PACKET +
190 sizeof(response), response.data_len,
195 "bad packet checksum %02x\n",
201 /* Return actual amount of data received */
202 ret = response.data_len;
207 static int cros_ec_cmd_xfer_lpc(struct cros_ec_device *ec,
208 struct cros_ec_command *msg)
210 struct ec_lpc_host_args args;
214 if (msg->outsize > EC_PROTO2_MAX_PARAM_SIZE ||
215 msg->insize > EC_PROTO2_MAX_PARAM_SIZE) {
217 "invalid buffer sizes (out %d, in %d)\n",
218 msg->outsize, msg->insize);
222 /* Now actually send the command to the EC and get the result */
223 args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
224 args.command_version = msg->version;
225 args.data_size = msg->outsize;
227 /* Initialize checksum */
228 sum = msg->command + args.flags + args.command_version + args.data_size;
230 /* Copy data and update checksum */
231 sum += cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_PARAM, msg->outsize,
234 /* Finalize checksum and write args */
236 cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_ARGS, sizeof(args),
241 cros_ec_lpc_ops.write(EC_LPC_ADDR_HOST_CMD, 1, &sum);
243 if (ec_response_timed_out()) {
244 dev_warn(ec->dev, "EC response timed out\n");
250 msg->result = cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_DATA, 1, &sum);
251 ret = cros_ec_check_result(ec, msg);
256 cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_ARGS, sizeof(args), (u8 *)&args);
258 if (args.data_size > msg->insize) {
260 "packet too long (%d bytes, expected %d)",
261 args.data_size, msg->insize);
266 /* Start calculating response checksum */
267 sum = msg->command + args.flags + args.command_version + args.data_size;
269 /* Read response and update checksum */
270 sum += cros_ec_lpc_ops.read(EC_LPC_ADDR_HOST_PARAM, args.data_size,
273 /* Verify checksum */
274 if (args.checksum != sum) {
276 "bad packet checksum, expected %02x, got %02x\n",
282 /* Return actual amount of data received */
283 ret = args.data_size;
288 /* Returns num bytes read, or negative on error. Doesn't need locking. */
289 static int cros_ec_lpc_readmem(struct cros_ec_device *ec, unsigned int offset,
290 unsigned int bytes, void *dest)
296 if (offset >= EC_MEMMAP_SIZE - bytes)
301 cros_ec_lpc_ops.read(EC_LPC_ADDR_MEMMAP + offset, bytes, s);
306 for (; i < EC_MEMMAP_SIZE; i++, s++) {
307 cros_ec_lpc_ops.read(EC_LPC_ADDR_MEMMAP + i, 1, s);
316 static void cros_ec_lpc_acpi_notify(acpi_handle device, u32 value, void *data)
318 struct cros_ec_device *ec_dev = data;
319 bool ec_has_more_events;
322 ec_dev->last_event_time = cros_ec_get_time_ns();
324 if (value == ACPI_NOTIFY_CROS_EC_PANIC) {
325 dev_emerg(ec_dev->dev, "CrOS EC Panic Reported. Shutdown is imminent!");
326 blocking_notifier_call_chain(&ec_dev->panic_notifier, 0, ec_dev);
327 /* Begin orderly shutdown. Force shutdown after 1 second. */
328 hw_protection_shutdown("CrOS EC Panic", 1000);
329 /* Do not query for other events after a panic is reported */
333 if (ec_dev->mkbp_event_supported)
335 ret = cros_ec_get_next_event(ec_dev, NULL,
336 &ec_has_more_events);
338 blocking_notifier_call_chain(
339 &ec_dev->event_notifier, 0,
341 } while (ec_has_more_events);
343 if (value == ACPI_NOTIFY_DEVICE_WAKE)
347 static int cros_ec_lpc_probe(struct platform_device *pdev)
349 struct device *dev = &pdev->dev;
350 struct acpi_device *adev;
352 struct cros_ec_device *ec_dev;
357 * The Framework Laptop (and possibly other non-ChromeOS devices)
358 * only exposes the eight I/O ports that are required for the Microchip EC.
359 * Requesting a larger reservation will fail.
361 if (!devm_request_region(dev, EC_HOST_CMD_REGION0,
362 EC_HOST_CMD_MEC_REGION_SIZE, dev_name(dev))) {
363 dev_err(dev, "couldn't reserve MEC region\n");
367 cros_ec_lpc_mec_init(EC_HOST_CMD_REGION0,
368 EC_LPC_ADDR_MEMMAP + EC_MEMMAP_SIZE);
371 * Read the mapped ID twice, the first one is assuming the
372 * EC is a Microchip Embedded Controller (MEC) variant, if the
373 * protocol fails, fallback to the non MEC variant and try to
376 cros_ec_lpc_ops.read = cros_ec_lpc_mec_read_bytes;
377 cros_ec_lpc_ops.write = cros_ec_lpc_mec_write_bytes;
378 cros_ec_lpc_ops.read(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID, 2, buf);
379 if (buf[0] != 'E' || buf[1] != 'C') {
380 if (!devm_request_region(dev, EC_LPC_ADDR_MEMMAP, EC_MEMMAP_SIZE,
382 dev_err(dev, "couldn't reserve memmap region\n");
386 /* Re-assign read/write operations for the non MEC variant */
387 cros_ec_lpc_ops.read = cros_ec_lpc_read_bytes;
388 cros_ec_lpc_ops.write = cros_ec_lpc_write_bytes;
389 cros_ec_lpc_ops.read(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID, 2,
391 if (buf[0] != 'E' || buf[1] != 'C') {
392 dev_err(dev, "EC ID not detected\n");
396 /* Reserve the remaining I/O ports required by the non-MEC protocol. */
397 if (!devm_request_region(dev, EC_HOST_CMD_REGION0 + EC_HOST_CMD_MEC_REGION_SIZE,
398 EC_HOST_CMD_REGION_SIZE - EC_HOST_CMD_MEC_REGION_SIZE,
400 dev_err(dev, "couldn't reserve remainder of region0\n");
403 if (!devm_request_region(dev, EC_HOST_CMD_REGION1,
404 EC_HOST_CMD_REGION_SIZE, dev_name(dev))) {
405 dev_err(dev, "couldn't reserve region1\n");
410 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
414 platform_set_drvdata(pdev, ec_dev);
416 ec_dev->phys_name = dev_name(dev);
417 ec_dev->cmd_xfer = cros_ec_cmd_xfer_lpc;
418 ec_dev->pkt_xfer = cros_ec_pkt_xfer_lpc;
419 ec_dev->cmd_readmem = cros_ec_lpc_readmem;
420 ec_dev->din_size = sizeof(struct ec_host_response) +
421 sizeof(struct ec_response_get_protocol_info);
422 ec_dev->dout_size = sizeof(struct ec_host_request);
425 * Some boards do not have an IRQ allotted for cros_ec_lpc,
426 * which makes ENXIO an expected (and safe) scenario.
428 irq = platform_get_irq_optional(pdev, 0);
431 else if (irq != -ENXIO) {
432 dev_err(dev, "couldn't retrieve IRQ number (%d)\n", irq);
436 ret = cros_ec_register(ec_dev);
438 dev_err(dev, "couldn't register ec_dev (%d)\n", ret);
443 * Connect a notify handler to process MKBP messages if we have a
444 * companion ACPI device.
446 adev = ACPI_COMPANION(dev);
448 status = acpi_install_notify_handler(adev->handle,
450 cros_ec_lpc_acpi_notify,
452 if (ACPI_FAILURE(status))
453 dev_warn(dev, "Failed to register notifier %08x\n",
460 static int cros_ec_lpc_remove(struct platform_device *pdev)
462 struct cros_ec_device *ec_dev = platform_get_drvdata(pdev);
463 struct acpi_device *adev;
465 adev = ACPI_COMPANION(&pdev->dev);
467 acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
468 cros_ec_lpc_acpi_notify);
470 cros_ec_unregister(ec_dev);
475 static const struct acpi_device_id cros_ec_lpc_acpi_device_ids[] = {
476 { ACPI_DRV_NAME, 0 },
479 MODULE_DEVICE_TABLE(acpi, cros_ec_lpc_acpi_device_ids);
481 static const struct dmi_system_id cros_ec_lpc_dmi_table[] __initconst = {
484 * Today all Chromebooks/boxes ship with Google_* as version and
485 * coreboot as bios vendor. No other systems with this
486 * combination are known to date.
489 DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
490 DMI_MATCH(DMI_BIOS_VERSION, "Google_"),
495 * If the box is running custom coreboot firmware then the
496 * DMI BIOS version string will not be matched by "Google_",
497 * but the system vendor string will still be matched by
501 DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
502 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
506 /* x86-link, the Chromebook Pixel. */
508 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
509 DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
513 /* x86-samus, the Chromebook Pixel 2. */
515 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
516 DMI_MATCH(DMI_PRODUCT_NAME, "Samus"),
520 /* x86-peppy, the Acer C720 Chromebook. */
522 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
523 DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
527 /* x86-glimmer, the Lenovo Thinkpad Yoga 11e. */
529 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
530 DMI_MATCH(DMI_PRODUCT_NAME, "Glimmer"),
533 /* A small number of non-Chromebook/box machines also use the ChromeOS EC */
535 /* the Framework Laptop */
537 DMI_MATCH(DMI_SYS_VENDOR, "Framework"),
538 DMI_MATCH(DMI_PRODUCT_NAME, "Laptop"),
543 MODULE_DEVICE_TABLE(dmi, cros_ec_lpc_dmi_table);
545 #ifdef CONFIG_PM_SLEEP
546 static int cros_ec_lpc_suspend(struct device *dev)
548 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
550 return cros_ec_suspend(ec_dev);
553 static int cros_ec_lpc_resume(struct device *dev)
555 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
557 return cros_ec_resume(ec_dev);
561 static const struct dev_pm_ops cros_ec_lpc_pm_ops = {
562 SET_LATE_SYSTEM_SLEEP_PM_OPS(cros_ec_lpc_suspend, cros_ec_lpc_resume)
565 static struct platform_driver cros_ec_lpc_driver = {
568 .acpi_match_table = cros_ec_lpc_acpi_device_ids,
569 .pm = &cros_ec_lpc_pm_ops,
571 * ACPI child devices may probe before us, and they racily
572 * check our drvdata pointer. Force synchronous probe until
573 * those races are resolved.
575 .probe_type = PROBE_FORCE_SYNCHRONOUS,
577 .probe = cros_ec_lpc_probe,
578 .remove = cros_ec_lpc_remove,
581 static struct platform_device cros_ec_lpc_device = {
585 static acpi_status cros_ec_lpc_parse_device(acpi_handle handle, u32 level,
586 void *context, void **retval)
588 *(bool *)context = true;
589 return AE_CTRL_TERMINATE;
592 static int __init cros_ec_lpc_init(void)
597 status = acpi_get_devices(ACPI_DRV_NAME, cros_ec_lpc_parse_device,
598 &cros_ec_lpc_acpi_device_found, NULL);
599 if (ACPI_FAILURE(status))
600 pr_warn(DRV_NAME ": Looking for %s failed\n", ACPI_DRV_NAME);
602 if (!cros_ec_lpc_acpi_device_found &&
603 !dmi_check_system(cros_ec_lpc_dmi_table)) {
604 pr_err(DRV_NAME ": unsupported system.\n");
608 /* Register the driver */
609 ret = platform_driver_register(&cros_ec_lpc_driver);
611 pr_err(DRV_NAME ": can't register driver: %d\n", ret);
615 if (!cros_ec_lpc_acpi_device_found) {
616 /* Register the device, and it'll get hooked up automatically */
617 ret = platform_device_register(&cros_ec_lpc_device);
619 pr_err(DRV_NAME ": can't register device: %d\n", ret);
620 platform_driver_unregister(&cros_ec_lpc_driver);
627 static void __exit cros_ec_lpc_exit(void)
629 if (!cros_ec_lpc_acpi_device_found)
630 platform_device_unregister(&cros_ec_lpc_device);
631 platform_driver_unregister(&cros_ec_lpc_driver);
634 module_init(cros_ec_lpc_init);
635 module_exit(cros_ec_lpc_exit);
637 MODULE_LICENSE("GPL");
638 MODULE_DESCRIPTION("ChromeOS EC LPC driver");