cros_ec_lpc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * cros_ec_lpc - LPC access to the Chrome OS Embedded Controller
  3. *
  4. * Copyright (C) 2012-2015 Google, Inc
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * This driver uses the Chrome OS EC byte-level message-based protocol for
  16. * communicating the keyboard state (which keys are pressed) from a keyboard EC
  17. * to the AP over some bus (such as i2c, lpc, spi). The EC does debouncing,
  18. * but everything else (including deghosting) is done here. The main
  19. * motivation for this is to keep the EC firmware as simple as possible, since
  20. * it cannot be easily upgraded and EC flash/IRAM space is relatively
  21. * expensive.
  22. */
  23. #include <linux/acpi.h>
  24. #include <linux/dmi.h>
  25. #include <linux/delay.h>
  26. #include <linux/io.h>
  27. #include <linux/mfd/cros_ec.h>
  28. #include <linux/mfd/cros_ec_commands.h>
  29. #include <linux/mfd/cros_ec_lpc_reg.h>
  30. #include <linux/module.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/printk.h>
  33. #define DRV_NAME "cros_ec_lpcs"
  34. #define ACPI_DRV_NAME "GOOG0004"
  35. static int ec_response_timed_out(void)
  36. {
  37. unsigned long one_second = jiffies + HZ;
  38. u8 data;
  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. return 1;
  47. }
  48. static int cros_ec_pkt_xfer_lpc(struct cros_ec_device *ec,
  49. struct cros_ec_command *msg)
  50. {
  51. struct ec_host_request *request;
  52. struct ec_host_response response;
  53. u8 sum;
  54. int ret = 0;
  55. u8 *dout;
  56. ret = cros_ec_prepare_tx(ec, msg);
  57. /* Write buffer */
  58. cros_ec_lpc_write_bytes(EC_LPC_ADDR_HOST_PACKET, ret, ec->dout);
  59. request = (struct ec_host_request *)ec->dout;
  60. /* Here we go */
  61. sum = EC_COMMAND_PROTOCOL_3;
  62. cros_ec_lpc_write_bytes(EC_LPC_ADDR_HOST_CMD, 1, &sum);
  63. if (ec_response_timed_out()) {
  64. dev_warn(ec->dev, "EC responsed timed out\n");
  65. ret = -EIO;
  66. goto done;
  67. }
  68. /* Check result */
  69. msg->result = cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_DATA, 1, &sum);
  70. ret = cros_ec_check_result(ec, msg);
  71. if (ret)
  72. goto done;
  73. /* Read back response */
  74. dout = (u8 *)&response;
  75. sum = cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_PACKET, sizeof(response),
  76. dout);
  77. msg->result = response.result;
  78. if (response.data_len > msg->insize) {
  79. dev_err(ec->dev,
  80. "packet too long (%d bytes, expected %d)",
  81. response.data_len, msg->insize);
  82. ret = -EMSGSIZE;
  83. goto done;
  84. }
  85. /* Read response and process checksum */
  86. sum += cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_PACKET +
  87. sizeof(response), response.data_len,
  88. msg->data);
  89. if (sum) {
  90. dev_err(ec->dev,
  91. "bad packet checksum %02x\n",
  92. response.checksum);
  93. ret = -EBADMSG;
  94. goto done;
  95. }
  96. /* Return actual amount of data received */
  97. ret = response.data_len;
  98. done:
  99. return ret;
  100. }
  101. static int cros_ec_cmd_xfer_lpc(struct cros_ec_device *ec,
  102. struct cros_ec_command *msg)
  103. {
  104. struct ec_lpc_host_args args;
  105. u8 sum;
  106. int ret = 0;
  107. if (msg->outsize > EC_PROTO2_MAX_PARAM_SIZE ||
  108. msg->insize > EC_PROTO2_MAX_PARAM_SIZE) {
  109. dev_err(ec->dev,
  110. "invalid buffer sizes (out %d, in %d)\n",
  111. msg->outsize, msg->insize);
  112. return -EINVAL;
  113. }
  114. /* Now actually send the command to the EC and get the result */
  115. args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
  116. args.command_version = msg->version;
  117. args.data_size = msg->outsize;
  118. /* Initialize checksum */
  119. sum = msg->command + args.flags + args.command_version + args.data_size;
  120. /* Copy data and update checksum */
  121. sum += cros_ec_lpc_write_bytes(EC_LPC_ADDR_HOST_PARAM, msg->outsize,
  122. msg->data);
  123. /* Finalize checksum and write args */
  124. args.checksum = sum;
  125. cros_ec_lpc_write_bytes(EC_LPC_ADDR_HOST_ARGS, sizeof(args),
  126. (u8 *)&args);
  127. /* Here we go */
  128. sum = msg->command;
  129. cros_ec_lpc_write_bytes(EC_LPC_ADDR_HOST_CMD, 1, &sum);
  130. if (ec_response_timed_out()) {
  131. dev_warn(ec->dev, "EC responsed timed out\n");
  132. ret = -EIO;
  133. goto done;
  134. }
  135. /* Check result */
  136. msg->result = cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_DATA, 1, &sum);
  137. ret = cros_ec_check_result(ec, msg);
  138. if (ret)
  139. goto done;
  140. /* Read back args */
  141. cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_ARGS, sizeof(args),
  142. (u8 *)&args);
  143. if (args.data_size > msg->insize) {
  144. dev_err(ec->dev,
  145. "packet too long (%d bytes, expected %d)",
  146. args.data_size, msg->insize);
  147. ret = -ENOSPC;
  148. goto done;
  149. }
  150. /* Start calculating response checksum */
  151. sum = msg->command + args.flags + args.command_version + args.data_size;
  152. /* Read response and update checksum */
  153. sum += cros_ec_lpc_read_bytes(EC_LPC_ADDR_HOST_PARAM, args.data_size,
  154. msg->data);
  155. /* Verify checksum */
  156. if (args.checksum != sum) {
  157. dev_err(ec->dev,
  158. "bad packet checksum, expected %02x, got %02x\n",
  159. args.checksum, sum);
  160. ret = -EBADMSG;
  161. goto done;
  162. }
  163. /* Return actual amount of data received */
  164. ret = args.data_size;
  165. done:
  166. return ret;
  167. }
  168. /* Returns num bytes read, or negative on error. Doesn't need locking. */
  169. static int cros_ec_lpc_readmem(struct cros_ec_device *ec, unsigned int offset,
  170. unsigned int bytes, void *dest)
  171. {
  172. int i = offset;
  173. char *s = dest;
  174. int cnt = 0;
  175. if (offset >= EC_MEMMAP_SIZE - bytes)
  176. return -EINVAL;
  177. /* fixed length */
  178. if (bytes) {
  179. cros_ec_lpc_read_bytes(EC_LPC_ADDR_MEMMAP + offset, bytes, s);
  180. return bytes;
  181. }
  182. /* string */
  183. for (; i < EC_MEMMAP_SIZE; i++, s++) {
  184. cros_ec_lpc_read_bytes(EC_LPC_ADDR_MEMMAP + i, 1, s);
  185. cnt++;
  186. if (!*s)
  187. break;
  188. }
  189. return cnt;
  190. }
  191. static void cros_ec_lpc_acpi_notify(acpi_handle device, u32 value, void *data)
  192. {
  193. struct cros_ec_device *ec_dev = data;
  194. if (ec_dev->mkbp_event_supported &&
  195. cros_ec_get_next_event(ec_dev, NULL) > 0)
  196. blocking_notifier_call_chain(&ec_dev->event_notifier, 0,
  197. ec_dev);
  198. }
  199. static int cros_ec_lpc_probe(struct platform_device *pdev)
  200. {
  201. struct device *dev = &pdev->dev;
  202. struct acpi_device *adev;
  203. acpi_status status;
  204. struct cros_ec_device *ec_dev;
  205. u8 buf[2];
  206. int ret;
  207. if (!devm_request_region(dev, EC_LPC_ADDR_MEMMAP, EC_MEMMAP_SIZE,
  208. dev_name(dev))) {
  209. dev_err(dev, "couldn't reserve memmap region\n");
  210. return -EBUSY;
  211. }
  212. cros_ec_lpc_read_bytes(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID, 2, buf);
  213. if (buf[0] != 'E' || buf[1] != 'C') {
  214. dev_err(dev, "EC ID not detected\n");
  215. return -ENODEV;
  216. }
  217. if (!devm_request_region(dev, EC_HOST_CMD_REGION0,
  218. EC_HOST_CMD_REGION_SIZE, dev_name(dev))) {
  219. dev_err(dev, "couldn't reserve region0\n");
  220. return -EBUSY;
  221. }
  222. if (!devm_request_region(dev, EC_HOST_CMD_REGION1,
  223. EC_HOST_CMD_REGION_SIZE, dev_name(dev))) {
  224. dev_err(dev, "couldn't reserve region1\n");
  225. return -EBUSY;
  226. }
  227. ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
  228. if (!ec_dev)
  229. return -ENOMEM;
  230. platform_set_drvdata(pdev, ec_dev);
  231. ec_dev->dev = dev;
  232. ec_dev->phys_name = dev_name(dev);
  233. ec_dev->cmd_xfer = cros_ec_cmd_xfer_lpc;
  234. ec_dev->pkt_xfer = cros_ec_pkt_xfer_lpc;
  235. ec_dev->cmd_readmem = cros_ec_lpc_readmem;
  236. ec_dev->din_size = sizeof(struct ec_host_response) +
  237. sizeof(struct ec_response_get_protocol_info);
  238. ec_dev->dout_size = sizeof(struct ec_host_request);
  239. ret = cros_ec_register(ec_dev);
  240. if (ret) {
  241. dev_err(dev, "couldn't register ec_dev (%d)\n", ret);
  242. return ret;
  243. }
  244. /*
  245. * Connect a notify handler to process MKBP messages if we have a
  246. * companion ACPI device.
  247. */
  248. adev = ACPI_COMPANION(dev);
  249. if (adev) {
  250. status = acpi_install_notify_handler(adev->handle,
  251. ACPI_ALL_NOTIFY,
  252. cros_ec_lpc_acpi_notify,
  253. ec_dev);
  254. if (ACPI_FAILURE(status))
  255. dev_warn(dev, "Failed to register notifier %08x\n",
  256. status);
  257. }
  258. return 0;
  259. }
  260. static int cros_ec_lpc_remove(struct platform_device *pdev)
  261. {
  262. struct cros_ec_device *ec_dev;
  263. struct acpi_device *adev;
  264. adev = ACPI_COMPANION(&pdev->dev);
  265. if (adev)
  266. acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY,
  267. cros_ec_lpc_acpi_notify);
  268. ec_dev = platform_get_drvdata(pdev);
  269. cros_ec_remove(ec_dev);
  270. return 0;
  271. }
  272. static const struct acpi_device_id cros_ec_lpc_acpi_device_ids[] = {
  273. { ACPI_DRV_NAME, 0 },
  274. { }
  275. };
  276. MODULE_DEVICE_TABLE(acpi, cros_ec_lpc_acpi_device_ids);
  277. static const struct dmi_system_id cros_ec_lpc_dmi_table[] __initconst = {
  278. {
  279. /*
  280. * Today all Chromebooks/boxes ship with Google_* as version and
  281. * coreboot as bios vendor. No other systems with this
  282. * combination are known to date.
  283. */
  284. .matches = {
  285. DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
  286. DMI_MATCH(DMI_BIOS_VERSION, "Google_"),
  287. },
  288. },
  289. {
  290. /* x86-link, the Chromebook Pixel. */
  291. .matches = {
  292. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  293. DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
  294. },
  295. },
  296. {
  297. /* x86-samus, the Chromebook Pixel 2. */
  298. .matches = {
  299. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  300. DMI_MATCH(DMI_PRODUCT_NAME, "Samus"),
  301. },
  302. },
  303. {
  304. /* x86-peppy, the Acer C720 Chromebook. */
  305. .matches = {
  306. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  307. DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
  308. },
  309. },
  310. { /* sentinel */ }
  311. };
  312. MODULE_DEVICE_TABLE(dmi, cros_ec_lpc_dmi_table);
  313. #ifdef CONFIG_PM_SLEEP
  314. static int cros_ec_lpc_suspend(struct device *dev)
  315. {
  316. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  317. return cros_ec_suspend(ec_dev);
  318. }
  319. static int cros_ec_lpc_resume(struct device *dev)
  320. {
  321. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  322. return cros_ec_resume(ec_dev);
  323. }
  324. #endif
  325. const struct dev_pm_ops cros_ec_lpc_pm_ops = {
  326. SET_LATE_SYSTEM_SLEEP_PM_OPS(cros_ec_lpc_suspend, cros_ec_lpc_resume)
  327. };
  328. static struct platform_driver cros_ec_lpc_driver = {
  329. .driver = {
  330. .name = DRV_NAME,
  331. .acpi_match_table = cros_ec_lpc_acpi_device_ids,
  332. .pm = &cros_ec_lpc_pm_ops,
  333. },
  334. .probe = cros_ec_lpc_probe,
  335. .remove = cros_ec_lpc_remove,
  336. };
  337. static int __init cros_ec_lpc_init(void)
  338. {
  339. int ret;
  340. if (!dmi_check_system(cros_ec_lpc_dmi_table)) {
  341. pr_err(DRV_NAME ": unsupported system.\n");
  342. return -ENODEV;
  343. }
  344. cros_ec_lpc_reg_init();
  345. /* Register the driver */
  346. ret = platform_driver_register(&cros_ec_lpc_driver);
  347. if (ret) {
  348. pr_err(DRV_NAME ": can't register driver: %d\n", ret);
  349. cros_ec_lpc_reg_destroy();
  350. return ret;
  351. }
  352. return 0;
  353. }
  354. static void __exit cros_ec_lpc_exit(void)
  355. {
  356. platform_driver_unregister(&cros_ec_lpc_driver);
  357. cros_ec_lpc_reg_destroy();
  358. }
  359. module_init(cros_ec_lpc_init);
  360. module_exit(cros_ec_lpc_exit);
  361. MODULE_LICENSE("GPL");
  362. MODULE_DESCRIPTION("ChromeOS EC LPC driver");