cros_ec_lpc.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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/dmi.h>
  24. #include <linux/delay.h>
  25. #include <linux/io.h>
  26. #include <linux/mfd/cros_ec.h>
  27. #include <linux/mfd/cros_ec_commands.h>
  28. #include <linux/module.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/printk.h>
  31. #define DRV_NAME "cros_ec_lpc"
  32. static int ec_response_timed_out(void)
  33. {
  34. unsigned long one_second = jiffies + HZ;
  35. usleep_range(200, 300);
  36. do {
  37. if (!(inb(EC_LPC_ADDR_HOST_CMD) & EC_LPC_STATUS_BUSY_MASK))
  38. return 0;
  39. usleep_range(100, 200);
  40. } while (time_before(jiffies, one_second));
  41. return 1;
  42. }
  43. static int cros_ec_pkt_xfer_lpc(struct cros_ec_device *ec,
  44. struct cros_ec_command *msg)
  45. {
  46. struct ec_host_request *request;
  47. struct ec_host_response response;
  48. u8 sum = 0;
  49. int i;
  50. int ret = 0;
  51. u8 *dout;
  52. ret = cros_ec_prepare_tx(ec, msg);
  53. /* Write buffer */
  54. for (i = 0; i < ret; i++)
  55. outb(ec->dout[i], EC_LPC_ADDR_HOST_PACKET + i);
  56. request = (struct ec_host_request *)ec->dout;
  57. /* Here we go */
  58. outb(EC_COMMAND_PROTOCOL_3, EC_LPC_ADDR_HOST_CMD);
  59. if (ec_response_timed_out()) {
  60. dev_warn(ec->dev, "EC responsed timed out\n");
  61. ret = -EIO;
  62. goto done;
  63. }
  64. /* Check result */
  65. msg->result = inb(EC_LPC_ADDR_HOST_DATA);
  66. ret = cros_ec_check_result(ec, msg);
  67. if (ret)
  68. goto done;
  69. /* Read back response */
  70. dout = (u8 *)&response;
  71. for (i = 0; i < sizeof(response); i++) {
  72. dout[i] = inb(EC_LPC_ADDR_HOST_PACKET + i);
  73. sum += dout[i];
  74. }
  75. msg->result = response.result;
  76. if (response.data_len > msg->insize) {
  77. dev_err(ec->dev,
  78. "packet too long (%d bytes, expected %d)",
  79. response.data_len, msg->insize);
  80. ret = -EMSGSIZE;
  81. goto done;
  82. }
  83. /* Read response and process checksum */
  84. for (i = 0; i < response.data_len; i++) {
  85. msg->data[i] =
  86. inb(EC_LPC_ADDR_HOST_PACKET + sizeof(response) + i);
  87. sum += msg->data[i];
  88. }
  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. int csum;
  106. int i;
  107. int ret = 0;
  108. if (msg->outsize > EC_PROTO2_MAX_PARAM_SIZE ||
  109. msg->insize > EC_PROTO2_MAX_PARAM_SIZE) {
  110. dev_err(ec->dev,
  111. "invalid buffer sizes (out %d, in %d)\n",
  112. msg->outsize, msg->insize);
  113. return -EINVAL;
  114. }
  115. /* Now actually send the command to the EC and get the result */
  116. args.flags = EC_HOST_ARGS_FLAG_FROM_HOST;
  117. args.command_version = msg->version;
  118. args.data_size = msg->outsize;
  119. /* Initialize checksum */
  120. csum = msg->command + args.flags +
  121. args.command_version + args.data_size;
  122. /* Copy data and update checksum */
  123. for (i = 0; i < msg->outsize; i++) {
  124. outb(msg->data[i], EC_LPC_ADDR_HOST_PARAM + i);
  125. csum += msg->data[i];
  126. }
  127. /* Finalize checksum and write args */
  128. args.checksum = csum & 0xFF;
  129. outb(args.flags, EC_LPC_ADDR_HOST_ARGS);
  130. outb(args.command_version, EC_LPC_ADDR_HOST_ARGS + 1);
  131. outb(args.data_size, EC_LPC_ADDR_HOST_ARGS + 2);
  132. outb(args.checksum, EC_LPC_ADDR_HOST_ARGS + 3);
  133. /* Here we go */
  134. outb(msg->command, EC_LPC_ADDR_HOST_CMD);
  135. if (ec_response_timed_out()) {
  136. dev_warn(ec->dev, "EC responsed timed out\n");
  137. ret = -EIO;
  138. goto done;
  139. }
  140. /* Check result */
  141. msg->result = inb(EC_LPC_ADDR_HOST_DATA);
  142. ret = cros_ec_check_result(ec, msg);
  143. if (ret)
  144. goto done;
  145. /* Read back args */
  146. args.flags = inb(EC_LPC_ADDR_HOST_ARGS);
  147. args.command_version = inb(EC_LPC_ADDR_HOST_ARGS + 1);
  148. args.data_size = inb(EC_LPC_ADDR_HOST_ARGS + 2);
  149. args.checksum = inb(EC_LPC_ADDR_HOST_ARGS + 3);
  150. if (args.data_size > msg->insize) {
  151. dev_err(ec->dev,
  152. "packet too long (%d bytes, expected %d)",
  153. args.data_size, msg->insize);
  154. ret = -ENOSPC;
  155. goto done;
  156. }
  157. /* Start calculating response checksum */
  158. csum = msg->command + args.flags +
  159. args.command_version + args.data_size;
  160. /* Read response and update checksum */
  161. for (i = 0; i < args.data_size; i++) {
  162. msg->data[i] = inb(EC_LPC_ADDR_HOST_PARAM + i);
  163. csum += msg->data[i];
  164. }
  165. /* Verify checksum */
  166. if (args.checksum != (csum & 0xFF)) {
  167. dev_err(ec->dev,
  168. "bad packet checksum, expected %02x, got %02x\n",
  169. args.checksum, csum & 0xFF);
  170. ret = -EBADMSG;
  171. goto done;
  172. }
  173. /* Return actual amount of data received */
  174. ret = args.data_size;
  175. done:
  176. return ret;
  177. }
  178. /* Returns num bytes read, or negative on error. Doesn't need locking. */
  179. static int cros_ec_lpc_readmem(struct cros_ec_device *ec, unsigned int offset,
  180. unsigned int bytes, void *dest)
  181. {
  182. int i = offset;
  183. char *s = dest;
  184. int cnt = 0;
  185. if (offset >= EC_MEMMAP_SIZE - bytes)
  186. return -EINVAL;
  187. /* fixed length */
  188. if (bytes) {
  189. for (; cnt < bytes; i++, s++, cnt++)
  190. *s = inb(EC_LPC_ADDR_MEMMAP + i);
  191. return cnt;
  192. }
  193. /* string */
  194. for (; i < EC_MEMMAP_SIZE; i++, s++) {
  195. *s = inb(EC_LPC_ADDR_MEMMAP + i);
  196. cnt++;
  197. if (!*s)
  198. break;
  199. }
  200. return cnt;
  201. }
  202. static int cros_ec_lpc_probe(struct platform_device *pdev)
  203. {
  204. struct device *dev = &pdev->dev;
  205. struct cros_ec_device *ec_dev;
  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. if ((inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID) != 'E') ||
  213. (inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID + 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. return 0;
  245. }
  246. static int cros_ec_lpc_remove(struct platform_device *pdev)
  247. {
  248. struct cros_ec_device *ec_dev;
  249. ec_dev = platform_get_drvdata(pdev);
  250. cros_ec_remove(ec_dev);
  251. return 0;
  252. }
  253. static struct dmi_system_id cros_ec_lpc_dmi_table[] __initdata = {
  254. {
  255. /*
  256. * Today all Chromebooks/boxes ship with Google_* as version and
  257. * coreboot as bios vendor. No other systems with this
  258. * combination are known to date.
  259. */
  260. .matches = {
  261. DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
  262. DMI_MATCH(DMI_BIOS_VERSION, "Google_"),
  263. },
  264. },
  265. {
  266. /* x86-link, the Chromebook Pixel. */
  267. .matches = {
  268. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  269. DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
  270. },
  271. },
  272. {
  273. /* x86-samus, the Chromebook Pixel 2. */
  274. .matches = {
  275. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  276. DMI_MATCH(DMI_PRODUCT_NAME, "Samus"),
  277. },
  278. },
  279. {
  280. /* x86-peppy, the Acer C720 Chromebook. */
  281. .matches = {
  282. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  283. DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
  284. },
  285. },
  286. { /* sentinel */ }
  287. };
  288. MODULE_DEVICE_TABLE(dmi, cros_ec_lpc_dmi_table);
  289. static struct platform_driver cros_ec_lpc_driver = {
  290. .driver = {
  291. .name = DRV_NAME,
  292. },
  293. .probe = cros_ec_lpc_probe,
  294. .remove = cros_ec_lpc_remove,
  295. };
  296. static struct platform_device cros_ec_lpc_device = {
  297. .name = DRV_NAME
  298. };
  299. static int __init cros_ec_lpc_init(void)
  300. {
  301. int ret;
  302. if (!dmi_check_system(cros_ec_lpc_dmi_table)) {
  303. pr_err(DRV_NAME ": unsupported system.\n");
  304. return -ENODEV;
  305. }
  306. /* Register the driver */
  307. ret = platform_driver_register(&cros_ec_lpc_driver);
  308. if (ret) {
  309. pr_err(DRV_NAME ": can't register driver: %d\n", ret);
  310. return ret;
  311. }
  312. /* Register the device, and it'll get hooked up automatically */
  313. ret = platform_device_register(&cros_ec_lpc_device);
  314. if (ret) {
  315. pr_err(DRV_NAME ": can't register device: %d\n", ret);
  316. platform_driver_unregister(&cros_ec_lpc_driver);
  317. return ret;
  318. }
  319. return 0;
  320. }
  321. static void __exit cros_ec_lpc_exit(void)
  322. {
  323. platform_device_unregister(&cros_ec_lpc_device);
  324. platform_driver_unregister(&cros_ec_lpc_driver);
  325. }
  326. module_init(cros_ec_lpc_init);
  327. module_exit(cros_ec_lpc_exit);
  328. MODULE_LICENSE("GPL");
  329. MODULE_DESCRIPTION("ChromeOS EC LPC driver");