1 // SPDX-License-Identifier: GPL-2.0 2 // LPC interface for ChromeOS Embedded Controller 3 // 4 // Copyright (C) 2012-2015 Google, Inc 5 // 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 12 // expensive. 13 14 #include <linux/acpi.h> 15 #include <linux/dmi.h> 16 #include <linux/delay.h> 17 #include <linux/io.h> 18 #include <linux/interrupt.h> 19 #include <linux/mfd/cros_ec.h> 20 #include <linux/mfd/cros_ec_commands.h> 21 #include <linux/module.h> 22 #include <linux/platform_device.h> 23 #include <linux/printk.h> 24 #include <linux/suspend.h> 25 26 #include "cros_ec_lpc_reg.h" 27 28 #define DRV_NAME "cros_ec_lpcs" 29 #define ACPI_DRV_NAME "GOOG0004" 30 31 /* True if ACPI device is present */ 32 static bool cros_ec_lpc_acpi_device_found; 33 34 static int ec_response_timed_out(void) 35 { 36 unsigned long one_second = jiffies + HZ; 37 u8 data; 38 39 usleep_range(200, 300); 40 do { 41 if (!(cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_CMD, 1, &data) & 42 EC_LPC_STATUS_BUSY_MASK)) 43 return 0; 44 usleep_range(100, 200); 45 } while (time_before(jiffies, one_second)); 46 47 return 1; 48 } 49 50 static int cros_ec_pkt_xfer_lpc(struct cros_ec_device *ec, 51 struct cros_ec_command *msg) 52 { 53 struct ec_host_response response; 54 u8 sum; 55 int ret = 0; 56 u8 *dout; 57 58 ret = cros_ec_prepare_tx(ec, msg); 59 60 /* Write buffer */ 61 cros_ec_lpc_write_bytes(EC_LPC_ADDR_HOST_PACKET, ret, ec->dout); 62 63 /* Here we go */ 64 sum = EC_COMMAND_PROTOCOL_3; 65 cros_ec_lpc_write_bytes(EC_LPC_ADDR_HOST_CMD, 1, &sum); 66 67 if (ec_response_timed_out()) { 68 dev_warn(ec->dev, "EC responsed timed out\n"); 69 ret = -EIO; 70 goto done; 71 } 72 73 /* Check result */ 74 msg->result = cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_DATA, 1, &sum); 75 ret = cros_ec_check_result(ec, msg); 76 if (ret) 77 goto done; 78 79 /* Read back response */ 80 dout = (u8 *)&response; 81 sum = cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_PACKET, sizeof(response), 82 dout); 83 84 msg->result = response.result; 85 86 if (response.data_len > msg->insize) { 87 dev_err(ec->dev, 88 "packet too long (%d bytes, expected %d)", 89 response.data_len, msg->insize); 90 ret = -EMSGSIZE; 91 goto done; 92 } 93 94 /* Read response and process checksum */ 95 sum += cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_PACKET + 96 sizeof(response), response.data_len, 97 msg->data); 98 99 if (sum) { 100 dev_err(ec->dev, 101 "bad packet checksum %02x\n", 102 response.checksum); 103 ret = -EBADMSG; 104 goto done; 105 } 106 107 /* Return actual amount of data received */ 108 ret = response.data_len; 109 done: 110 return ret; 111 } 112 113 static int cros_ec_cmd_xfer_lpc(struct cros_ec_device *ec, 114 struct cros_ec_command *msg) 115 { 116 struct ec_lpc_host_args args; 117 u8 sum; 118 int ret = 0; 119 120 if (msg->outsize > EC_PROTO2_MAX_PARAM_SIZE || 121 msg->insize > EC_PROTO2_MAX_PARAM_SIZE) { 122 dev_err(ec->dev, 123 "invalid buffer sizes (out %d, in %d)\n", 124 msg->outsize, msg->insize); 125 return -EINVAL; 126 } 127 128 /* Now actually send the command to the EC and get the result */ 129 args.flags = EC_HOST_ARGS_FLAG_FROM_HOST; 130 args.command_version = msg->version; 131 args.data_size = msg->outsize; 132 133 /* Initialize checksum */ 134 sum = msg->command + args.flags + args.command_version + args.data_size; 135 136 /* Copy data and update checksum */ 137 sum += cros_ec_lpc_write_bytes(EC_LPC_ADDR_HOST_PARAM, msg->outsize, 138 msg->data); 139 140 /* Finalize checksum and write args */ 141 args.checksum = sum; 142 cros_ec_lpc_write_bytes(EC_LPC_ADDR_HOST_ARGS, sizeof(args), 143 (u8 *)&args); 144 145 /* Here we go */ 146 sum = msg->command; 147 cros_ec_lpc_write_bytes(EC_LPC_ADDR_HOST_CMD, 1, &sum); 148 149 if (ec_response_timed_out()) { 150 dev_warn(ec->dev, "EC responsed timed out\n"); 151 ret = -EIO; 152 goto done; 153 } 154 155 /* Check result */ 156 msg->result = cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_DATA, 1, &sum); 157 ret = cros_ec_check_result(ec, msg); 158 if (ret) 159 goto done; 160 161 /* Read back args */ 162 cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_ARGS, sizeof(args), 163 (u8 *)&args); 164 165 if (args.data_size > msg->insize) { 166 dev_err(ec->dev, 167 "packet too long (%d bytes, expected %d)", 168 args.data_size, msg->insize); 169 ret = -ENOSPC; 170 goto done; 171 } 172 173 /* Start calculating response checksum */ 174 sum = msg->command + args.flags + args.command_version + args.data_size; 175 176 /* Read response and update checksum */ 177 sum += cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_PARAM, args.data_size, 178 msg->data); 179 180 /* Verify checksum */ 181 if (args.checksum != sum) { 182 dev_err(ec->dev, 183 "bad packet checksum, expected %02x, got %02x\n", 184 args.checksum, sum); 185 ret = -EBADMSG; 186 goto done; 187 } 188 189 /* Return actual amount of data received */ 190 ret = args.data_size; 191 done: 192 return ret; 193 } 194 195 /* Returns num bytes read, or negative on error. Doesn't need locking. */ 196 static int cros_ec_lpc_readmem(struct cros_ec_device *ec, unsigned int offset, 197 unsigned int bytes, void *dest) 198 { 199 int i = offset; 200 char *s = dest; 201 int cnt = 0; 202 203 if (offset >= EC_MEMMAP_SIZE - bytes) 204 return -EINVAL; 205 206 /* fixed length */ 207 if (bytes) { 208 cros_ec_lpc_read_bytes(EC_LPC_ADDR_MEMMAP + offset, bytes, s); 209 return bytes; 210 } 211 212 /* string */ 213 for (; i < EC_MEMMAP_SIZE; i++, s++) { 214 cros_ec_lpc_read_bytes(EC_LPC_ADDR_MEMMAP + i, 1, s); 215 cnt++; 216 if (!*s) 217 break; 218 } 219 220 return cnt; 221 } 222 223 static void cros_ec_lpc_acpi_notify(acpi_handle device, u32 value, void *data) 224 { 225 struct cros_ec_device *ec_dev = data; 226 227 if (ec_dev->mkbp_event_supported && 228 cros_ec_get_next_event(ec_dev, NULL) > 0) 229 blocking_notifier_call_chain(&ec_dev->event_notifier, 0, 230 ec_dev); 231 232 if (value == ACPI_NOTIFY_DEVICE_WAKE) 233 pm_system_wakeup(); 234 } 235 236 static int cros_ec_lpc_probe(struct platform_device *pdev) 237 { 238 struct device *dev = &pdev->dev; 239 struct acpi_device *adev; 240 acpi_status status; 241 struct cros_ec_device *ec_dev; 242 u8 buf[2]; 243 int irq, ret; 244 245 if (!devm_request_region(dev, EC_LPC_ADDR_MEMMAP, EC_MEMMAP_SIZE, 246 dev_name(dev))) { 247 dev_err(dev, "couldn't reserve memmap region\n"); 248 return -EBUSY; 249 } 250 251 cros_ec_lpc_read_bytes(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID, 2, buf); 252 if (buf[0] != 'E' || buf[1] != 'C') { 253 dev_err(dev, "EC ID not detected\n"); 254 return -ENODEV; 255 } 256 257 if (!devm_request_region(dev, EC_HOST_CMD_REGION0, 258 EC_HOST_CMD_REGION_SIZE, dev_name(dev))) { 259 dev_err(dev, "couldn't reserve region0\n"); 260 return -EBUSY; 261 } 262 if (!devm_request_region(dev, EC_HOST_CMD_REGION1, 263 EC_HOST_CMD_REGION_SIZE, dev_name(dev))) { 264 dev_err(dev, "couldn't reserve region1\n"); 265 return -EBUSY; 266 } 267 268 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL); 269 if (!ec_dev) 270 return -ENOMEM; 271 272 platform_set_drvdata(pdev, ec_dev); 273 ec_dev->dev = dev; 274 ec_dev->phys_name = dev_name(dev); 275 ec_dev->cmd_xfer = cros_ec_cmd_xfer_lpc; 276 ec_dev->pkt_xfer = cros_ec_pkt_xfer_lpc; 277 ec_dev->cmd_readmem = cros_ec_lpc_readmem; 278 ec_dev->din_size = sizeof(struct ec_host_response) + 279 sizeof(struct ec_response_get_protocol_info); 280 ec_dev->dout_size = sizeof(struct ec_host_request); 281 282 /* 283 * Some boards do not have an IRQ allotted for cros_ec_lpc, 284 * which makes ENXIO an expected (and safe) scenario. 285 */ 286 irq = platform_get_irq(pdev, 0); 287 if (irq > 0) 288 ec_dev->irq = irq; 289 else if (irq != -ENXIO) { 290 dev_err(dev, "couldn't retrieve IRQ number (%d)\n", irq); 291 return irq; 292 } 293 294 ret = cros_ec_register(ec_dev); 295 if (ret) { 296 dev_err(dev, "couldn't register ec_dev (%d)\n", ret); 297 return ret; 298 } 299 300 /* 301 * Connect a notify handler to process MKBP messages if we have a 302 * companion ACPI device. 303 */ 304 adev = ACPI_COMPANION(dev); 305 if (adev) { 306 status = acpi_install_notify_handler(adev->handle, 307 ACPI_ALL_NOTIFY, 308 cros_ec_lpc_acpi_notify, 309 ec_dev); 310 if (ACPI_FAILURE(status)) 311 dev_warn(dev, "Failed to register notifier %08x\n", 312 status); 313 } 314 315 return 0; 316 } 317 318 static int cros_ec_lpc_remove(struct platform_device *pdev) 319 { 320 struct acpi_device *adev; 321 322 adev = ACPI_COMPANION(&pdev->dev); 323 if (adev) 324 acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY, 325 cros_ec_lpc_acpi_notify); 326 327 return 0; 328 } 329 330 static const struct acpi_device_id cros_ec_lpc_acpi_device_ids[] = { 331 { ACPI_DRV_NAME, 0 }, 332 { } 333 }; 334 MODULE_DEVICE_TABLE(acpi, cros_ec_lpc_acpi_device_ids); 335 336 static const struct dmi_system_id cros_ec_lpc_dmi_table[] __initconst = { 337 { 338 /* 339 * Today all Chromebooks/boxes ship with Google_* as version and 340 * coreboot as bios vendor. No other systems with this 341 * combination are known to date. 342 */ 343 .matches = { 344 DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"), 345 DMI_MATCH(DMI_BIOS_VERSION, "Google_"), 346 }, 347 }, 348 { 349 /* 350 * If the box is running custom coreboot firmware then the 351 * DMI BIOS version string will not be matched by "Google_", 352 * but the system vendor string will still be matched by 353 * "GOOGLE". 354 */ 355 .matches = { 356 DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"), 357 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"), 358 }, 359 }, 360 { 361 /* x86-link, the Chromebook Pixel. */ 362 .matches = { 363 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"), 364 DMI_MATCH(DMI_PRODUCT_NAME, "Link"), 365 }, 366 }, 367 { 368 /* x86-samus, the Chromebook Pixel 2. */ 369 .matches = { 370 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"), 371 DMI_MATCH(DMI_PRODUCT_NAME, "Samus"), 372 }, 373 }, 374 { 375 /* x86-peppy, the Acer C720 Chromebook. */ 376 .matches = { 377 DMI_MATCH(DMI_SYS_VENDOR, "Acer"), 378 DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"), 379 }, 380 }, 381 { 382 /* x86-glimmer, the Lenovo Thinkpad Yoga 11e. */ 383 .matches = { 384 DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"), 385 DMI_MATCH(DMI_PRODUCT_NAME, "Glimmer"), 386 }, 387 }, 388 { /* sentinel */ } 389 }; 390 MODULE_DEVICE_TABLE(dmi, cros_ec_lpc_dmi_table); 391 392 #ifdef CONFIG_PM_SLEEP 393 static int cros_ec_lpc_suspend(struct device *dev) 394 { 395 struct cros_ec_device *ec_dev = dev_get_drvdata(dev); 396 397 return cros_ec_suspend(ec_dev); 398 } 399 400 static int cros_ec_lpc_resume(struct device *dev) 401 { 402 struct cros_ec_device *ec_dev = dev_get_drvdata(dev); 403 404 return cros_ec_resume(ec_dev); 405 } 406 #endif 407 408 const struct dev_pm_ops cros_ec_lpc_pm_ops = { 409 SET_LATE_SYSTEM_SLEEP_PM_OPS(cros_ec_lpc_suspend, cros_ec_lpc_resume) 410 }; 411 412 static struct platform_driver cros_ec_lpc_driver = { 413 .driver = { 414 .name = DRV_NAME, 415 .acpi_match_table = cros_ec_lpc_acpi_device_ids, 416 .pm = &cros_ec_lpc_pm_ops, 417 }, 418 .probe = cros_ec_lpc_probe, 419 .remove = cros_ec_lpc_remove, 420 }; 421 422 static struct platform_device cros_ec_lpc_device = { 423 .name = DRV_NAME 424 }; 425 426 static acpi_status cros_ec_lpc_parse_device(acpi_handle handle, u32 level, 427 void *context, void **retval) 428 { 429 *(bool *)context = true; 430 return AE_CTRL_TERMINATE; 431 } 432 433 static int __init cros_ec_lpc_init(void) 434 { 435 int ret; 436 acpi_status status; 437 438 status = acpi_get_devices(ACPI_DRV_NAME, cros_ec_lpc_parse_device, 439 &cros_ec_lpc_acpi_device_found, NULL); 440 if (ACPI_FAILURE(status)) 441 pr_warn(DRV_NAME ": Looking for %s failed\n", ACPI_DRV_NAME); 442 443 if (!cros_ec_lpc_acpi_device_found && 444 !dmi_check_system(cros_ec_lpc_dmi_table)) { 445 pr_err(DRV_NAME ": unsupported system.\n"); 446 return -ENODEV; 447 } 448 449 cros_ec_lpc_reg_init(); 450 451 /* Register the driver */ 452 ret = platform_driver_register(&cros_ec_lpc_driver); 453 if (ret) { 454 pr_err(DRV_NAME ": can't register driver: %d\n", ret); 455 cros_ec_lpc_reg_destroy(); 456 return ret; 457 } 458 459 if (!cros_ec_lpc_acpi_device_found) { 460 /* Register the device, and it'll get hooked up automatically */ 461 ret = platform_device_register(&cros_ec_lpc_device); 462 if (ret) { 463 pr_err(DRV_NAME ": can't register device: %d\n", ret); 464 platform_driver_unregister(&cros_ec_lpc_driver); 465 cros_ec_lpc_reg_destroy(); 466 } 467 } 468 469 return ret; 470 } 471 472 static void __exit cros_ec_lpc_exit(void) 473 { 474 if (!cros_ec_lpc_acpi_device_found) 475 platform_device_unregister(&cros_ec_lpc_device); 476 platform_driver_unregister(&cros_ec_lpc_driver); 477 cros_ec_lpc_reg_destroy(); 478 } 479 480 module_init(cros_ec_lpc_init); 481 module_exit(cros_ec_lpc_exit); 482 483 MODULE_LICENSE("GPL"); 484 MODULE_DESCRIPTION("ChromeOS EC LPC driver"); 485