cros_ec_lpc.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. switch (msg->result) {
  143. case EC_RES_SUCCESS:
  144. break;
  145. case EC_RES_IN_PROGRESS:
  146. ret = -EAGAIN;
  147. dev_dbg(ec->dev, "command 0x%02x in progress\n",
  148. msg->command);
  149. goto done;
  150. default:
  151. dev_dbg(ec->dev, "command 0x%02x returned %d\n",
  152. msg->command, msg->result);
  153. }
  154. /* Read back args */
  155. args.flags = inb(EC_LPC_ADDR_HOST_ARGS);
  156. args.command_version = inb(EC_LPC_ADDR_HOST_ARGS + 1);
  157. args.data_size = inb(EC_LPC_ADDR_HOST_ARGS + 2);
  158. args.checksum = inb(EC_LPC_ADDR_HOST_ARGS + 3);
  159. if (args.data_size > msg->insize) {
  160. dev_err(ec->dev,
  161. "packet too long (%d bytes, expected %d)",
  162. args.data_size, msg->insize);
  163. ret = -ENOSPC;
  164. goto done;
  165. }
  166. /* Start calculating response checksum */
  167. csum = msg->command + args.flags +
  168. args.command_version + args.data_size;
  169. /* Read response and update checksum */
  170. for (i = 0; i < args.data_size; i++) {
  171. msg->data[i] = inb(EC_LPC_ADDR_HOST_PARAM + i);
  172. csum += msg->data[i];
  173. }
  174. /* Verify checksum */
  175. if (args.checksum != (csum & 0xFF)) {
  176. dev_err(ec->dev,
  177. "bad packet checksum, expected %02x, got %02x\n",
  178. args.checksum, csum & 0xFF);
  179. ret = -EBADMSG;
  180. goto done;
  181. }
  182. /* Return actual amount of data received */
  183. ret = args.data_size;
  184. done:
  185. return ret;
  186. }
  187. /* Returns num bytes read, or negative on error. Doesn't need locking. */
  188. static int cros_ec_lpc_readmem(struct cros_ec_device *ec, unsigned int offset,
  189. unsigned int bytes, void *dest)
  190. {
  191. int i = offset;
  192. char *s = dest;
  193. int cnt = 0;
  194. if (offset >= EC_MEMMAP_SIZE - bytes)
  195. return -EINVAL;
  196. /* fixed length */
  197. if (bytes) {
  198. for (; cnt < bytes; i++, s++, cnt++)
  199. *s = inb(EC_LPC_ADDR_MEMMAP + i);
  200. return cnt;
  201. }
  202. /* string */
  203. for (; i < EC_MEMMAP_SIZE; i++, s++) {
  204. *s = inb(EC_LPC_ADDR_MEMMAP + i);
  205. cnt++;
  206. if (!*s)
  207. break;
  208. }
  209. return cnt;
  210. }
  211. static int cros_ec_lpc_probe(struct platform_device *pdev)
  212. {
  213. struct device *dev = &pdev->dev;
  214. struct cros_ec_device *ec_dev;
  215. int ret;
  216. if (!devm_request_region(dev, EC_LPC_ADDR_MEMMAP, EC_MEMMAP_SIZE,
  217. dev_name(dev))) {
  218. dev_err(dev, "couldn't reserve memmap region\n");
  219. return -EBUSY;
  220. }
  221. if ((inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID) != 'E') ||
  222. (inb(EC_LPC_ADDR_MEMMAP + EC_MEMMAP_ID + 1) != 'C')) {
  223. dev_err(dev, "EC ID not detected\n");
  224. return -ENODEV;
  225. }
  226. if (!devm_request_region(dev, EC_HOST_CMD_REGION0,
  227. EC_HOST_CMD_REGION_SIZE, dev_name(dev))) {
  228. dev_err(dev, "couldn't reserve region0\n");
  229. return -EBUSY;
  230. }
  231. if (!devm_request_region(dev, EC_HOST_CMD_REGION1,
  232. EC_HOST_CMD_REGION_SIZE, dev_name(dev))) {
  233. dev_err(dev, "couldn't reserve region1\n");
  234. return -EBUSY;
  235. }
  236. ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
  237. if (!ec_dev)
  238. return -ENOMEM;
  239. platform_set_drvdata(pdev, ec_dev);
  240. ec_dev->dev = dev;
  241. ec_dev->phys_name = dev_name(dev);
  242. ec_dev->cmd_xfer = cros_ec_cmd_xfer_lpc;
  243. ec_dev->pkt_xfer = cros_ec_pkt_xfer_lpc;
  244. ec_dev->cmd_readmem = cros_ec_lpc_readmem;
  245. ec_dev->din_size = sizeof(struct ec_host_response) +
  246. sizeof(struct ec_response_get_protocol_info);
  247. ec_dev->dout_size = sizeof(struct ec_host_request);
  248. ret = cros_ec_register(ec_dev);
  249. if (ret) {
  250. dev_err(dev, "couldn't register ec_dev (%d)\n", ret);
  251. return ret;
  252. }
  253. return 0;
  254. }
  255. static int cros_ec_lpc_remove(struct platform_device *pdev)
  256. {
  257. struct cros_ec_device *ec_dev;
  258. ec_dev = platform_get_drvdata(pdev);
  259. cros_ec_remove(ec_dev);
  260. return 0;
  261. }
  262. static struct dmi_system_id cros_ec_lpc_dmi_table[] __initdata = {
  263. {
  264. /*
  265. * Today all Chromebooks/boxes ship with Google_* as version and
  266. * coreboot as bios vendor. No other systems with this
  267. * combination are known to date.
  268. */
  269. .matches = {
  270. DMI_MATCH(DMI_BIOS_VENDOR, "coreboot"),
  271. DMI_MATCH(DMI_BIOS_VERSION, "Google_"),
  272. },
  273. },
  274. {
  275. /* x86-link, the Chromebook Pixel. */
  276. .matches = {
  277. DMI_MATCH(DMI_SYS_VENDOR, "GOOGLE"),
  278. DMI_MATCH(DMI_PRODUCT_NAME, "Link"),
  279. },
  280. },
  281. {
  282. /* x86-peppy, the Acer C720 Chromebook. */
  283. .matches = {
  284. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  285. DMI_MATCH(DMI_PRODUCT_NAME, "Peppy"),
  286. },
  287. },
  288. { /* sentinel */ }
  289. };
  290. MODULE_DEVICE_TABLE(dmi, cros_ec_lpc_dmi_table);
  291. static struct platform_driver cros_ec_lpc_driver = {
  292. .driver = {
  293. .name = DRV_NAME,
  294. },
  295. .probe = cros_ec_lpc_probe,
  296. .remove = cros_ec_lpc_remove,
  297. };
  298. static struct platform_device cros_ec_lpc_device = {
  299. .name = DRV_NAME
  300. };
  301. static int __init cros_ec_lpc_init(void)
  302. {
  303. int ret;
  304. if (!dmi_check_system(cros_ec_lpc_dmi_table)) {
  305. pr_err(DRV_NAME ": unsupported system.\n");
  306. return -ENODEV;
  307. }
  308. /* Register the driver */
  309. ret = platform_driver_register(&cros_ec_lpc_driver);
  310. if (ret) {
  311. pr_err(DRV_NAME ": can't register driver: %d\n", ret);
  312. return ret;
  313. }
  314. /* Register the device, and it'll get hooked up automatically */
  315. ret = platform_device_register(&cros_ec_lpc_device);
  316. if (ret) {
  317. pr_err(DRV_NAME ": can't register device: %d\n", ret);
  318. platform_driver_unregister(&cros_ec_lpc_driver);
  319. return ret;
  320. }
  321. return 0;
  322. }
  323. static void __exit cros_ec_lpc_exit(void)
  324. {
  325. platform_device_unregister(&cros_ec_lpc_device);
  326. platform_driver_unregister(&cros_ec_lpc_driver);
  327. }
  328. module_init(cros_ec_lpc_init);
  329. module_exit(cros_ec_lpc_exit);
  330. MODULE_LICENSE("GPL");
  331. MODULE_DESCRIPTION("ChromeOS EC LPC driver");