cros_ec_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * ChromeOS EC multi-function device (SPI)
  3. *
  4. * Copyright (C) 2012 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. #include <linux/delay.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/mfd/cros_ec.h>
  19. #include <linux/mfd/cros_ec_commands.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/spi/spi.h>
  24. /* The header byte, which follows the preamble */
  25. #define EC_MSG_HEADER 0xec
  26. /*
  27. * Number of EC preamble bytes we read at a time. Since it takes
  28. * about 400-500us for the EC to respond there is not a lot of
  29. * point in tuning this. If the EC could respond faster then
  30. * we could increase this so that might expect the preamble and
  31. * message to occur in a single transaction. However, the maximum
  32. * SPI transfer size is 256 bytes, so at 5MHz we need a response
  33. * time of perhaps <320us (200 bytes / 1600 bits).
  34. */
  35. #define EC_MSG_PREAMBLE_COUNT 32
  36. /*
  37. * Allow for a long time for the EC to respond. We support i2c
  38. * tunneling and support fairly long messages for the tunnel (249
  39. * bytes long at the moment). If we're talking to a 100 kHz device
  40. * on the other end and need to transfer ~256 bytes, then we need:
  41. * 10 us/bit * ~10 bits/byte * ~256 bytes = ~25ms
  42. *
  43. * We'll wait 4 times that to handle clock stretching and other
  44. * paranoia.
  45. *
  46. * It's pretty unlikely that we'll really see a 249 byte tunnel in
  47. * anything other than testing. If this was more common we might
  48. * consider having slow commands like this require a GET_STATUS
  49. * wait loop. The 'flash write' command would be another candidate
  50. * for this, clocking in at 2-3ms.
  51. */
  52. #define EC_MSG_DEADLINE_MS 100
  53. /*
  54. * Time between raising the SPI chip select (for the end of a
  55. * transaction) and dropping it again (for the next transaction).
  56. * If we go too fast, the EC will miss the transaction. We know that we
  57. * need at least 70 us with the 16 MHz STM32 EC, so go with 200 us to be
  58. * safe.
  59. */
  60. #define EC_SPI_RECOVERY_TIME_NS (200 * 1000)
  61. /*
  62. * The EC is unresponsive for a time after a reboot command. Add a
  63. * simple delay to make sure that the bus stays locked.
  64. */
  65. #define EC_REBOOT_DELAY_MS 50
  66. /**
  67. * struct cros_ec_spi - information about a SPI-connected EC
  68. *
  69. * @spi: SPI device we are connected to
  70. * @last_transfer_ns: time that we last finished a transfer, or 0 if there
  71. * if no record
  72. * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that
  73. * is sent when we want to turn off CS at the end of a transaction.
  74. */
  75. struct cros_ec_spi {
  76. struct spi_device *spi;
  77. s64 last_transfer_ns;
  78. unsigned int end_of_msg_delay;
  79. };
  80. static void debug_packet(struct device *dev, const char *name, u8 *ptr,
  81. int len)
  82. {
  83. #ifdef DEBUG
  84. int i;
  85. dev_dbg(dev, "%s: ", name);
  86. for (i = 0; i < len; i++)
  87. pr_cont(" %02x", ptr[i]);
  88. pr_cont("\n");
  89. #endif
  90. }
  91. /**
  92. * cros_ec_spi_receive_response - Receive a response from the EC.
  93. *
  94. * This function has two phases: reading the preamble bytes (since if we read
  95. * data from the EC before it is ready to send, we just get preamble) and
  96. * reading the actual message.
  97. *
  98. * The received data is placed into ec_dev->din.
  99. *
  100. * @ec_dev: ChromeOS EC device
  101. * @need_len: Number of message bytes we need to read
  102. */
  103. static int cros_ec_spi_receive_response(struct cros_ec_device *ec_dev,
  104. int need_len)
  105. {
  106. struct cros_ec_spi *ec_spi = ec_dev->priv;
  107. struct spi_transfer trans;
  108. struct spi_message msg;
  109. u8 *ptr, *end;
  110. int ret;
  111. unsigned long deadline;
  112. int todo;
  113. /* Receive data until we see the header byte */
  114. deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
  115. while (true) {
  116. unsigned long start_jiffies = jiffies;
  117. memset(&trans, 0, sizeof(trans));
  118. trans.cs_change = 1;
  119. trans.rx_buf = ptr = ec_dev->din;
  120. trans.len = EC_MSG_PREAMBLE_COUNT;
  121. spi_message_init(&msg);
  122. spi_message_add_tail(&trans, &msg);
  123. ret = spi_sync(ec_spi->spi, &msg);
  124. if (ret < 0) {
  125. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  126. return ret;
  127. }
  128. for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) {
  129. if (*ptr == EC_MSG_HEADER) {
  130. dev_dbg(ec_dev->dev, "msg found at %zd\n",
  131. ptr - ec_dev->din);
  132. break;
  133. }
  134. }
  135. if (ptr != end)
  136. break;
  137. /*
  138. * Use the time at the start of the loop as a timeout. This
  139. * gives us one last shot at getting the transfer and is useful
  140. * in case we got context switched out for a while.
  141. */
  142. if (time_after(start_jiffies, deadline)) {
  143. dev_warn(ec_dev->dev, "EC failed to respond in time\n");
  144. return -ETIMEDOUT;
  145. }
  146. }
  147. /*
  148. * ptr now points to the header byte. Copy any valid data to the
  149. * start of our buffer
  150. */
  151. todo = end - ++ptr;
  152. BUG_ON(todo < 0 || todo > ec_dev->din_size);
  153. todo = min(todo, need_len);
  154. memmove(ec_dev->din, ptr, todo);
  155. ptr = ec_dev->din + todo;
  156. dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n",
  157. need_len, todo);
  158. need_len -= todo;
  159. /* Receive data until we have it all */
  160. while (need_len > 0) {
  161. /*
  162. * We can't support transfers larger than the SPI FIFO size
  163. * unless we have DMA. We don't have DMA on the ISP SPI ports
  164. * for Exynos. We need a way of asking SPI driver for
  165. * maximum-supported transfer size.
  166. */
  167. todo = min(need_len, 256);
  168. dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n",
  169. todo, need_len, ptr - ec_dev->din);
  170. memset(&trans, 0, sizeof(trans));
  171. trans.cs_change = 1;
  172. trans.rx_buf = ptr;
  173. trans.len = todo;
  174. spi_message_init(&msg);
  175. spi_message_add_tail(&trans, &msg);
  176. /* send command to EC and read answer */
  177. BUG_ON((u8 *)trans.rx_buf - ec_dev->din + todo >
  178. ec_dev->din_size);
  179. ret = spi_sync(ec_spi->spi, &msg);
  180. if (ret < 0) {
  181. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  182. return ret;
  183. }
  184. debug_packet(ec_dev->dev, "interim", ptr, todo);
  185. ptr += todo;
  186. need_len -= todo;
  187. }
  188. dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
  189. return 0;
  190. }
  191. /**
  192. * cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply
  193. *
  194. * @ec_dev: ChromeOS EC device
  195. * @ec_msg: Message to transfer
  196. */
  197. static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
  198. struct cros_ec_command *ec_msg)
  199. {
  200. struct cros_ec_spi *ec_spi = ec_dev->priv;
  201. struct spi_transfer trans;
  202. struct spi_message msg;
  203. int i, len;
  204. u8 *ptr;
  205. int sum;
  206. int ret = 0, final_ret;
  207. len = cros_ec_prepare_tx(ec_dev, ec_msg);
  208. dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
  209. /* If it's too soon to do another transaction, wait */
  210. if (ec_spi->last_transfer_ns) {
  211. unsigned long delay; /* The delay completed so far */
  212. delay = ktime_get_ns() - ec_spi->last_transfer_ns;
  213. if (delay < EC_SPI_RECOVERY_TIME_NS)
  214. ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
  215. }
  216. /* Transmit phase - send our message */
  217. debug_packet(ec_dev->dev, "out", ec_dev->dout, len);
  218. memset(&trans, 0, sizeof(trans));
  219. trans.tx_buf = ec_dev->dout;
  220. trans.len = len;
  221. trans.cs_change = 1;
  222. spi_message_init(&msg);
  223. spi_message_add_tail(&trans, &msg);
  224. ret = spi_sync(ec_spi->spi, &msg);
  225. /* Get the response */
  226. if (!ret) {
  227. ret = cros_ec_spi_receive_response(ec_dev,
  228. ec_msg->insize + EC_MSG_TX_PROTO_BYTES);
  229. } else {
  230. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  231. }
  232. /*
  233. * Turn off CS, possibly adding a delay to ensure the rising edge
  234. * doesn't come too soon after the end of the data.
  235. */
  236. spi_message_init(&msg);
  237. memset(&trans, 0, sizeof(trans));
  238. trans.delay_usecs = ec_spi->end_of_msg_delay;
  239. spi_message_add_tail(&trans, &msg);
  240. final_ret = spi_sync(ec_spi->spi, &msg);
  241. ec_spi->last_transfer_ns = ktime_get_ns();
  242. if (!ret)
  243. ret = final_ret;
  244. if (ret < 0) {
  245. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  246. goto exit;
  247. }
  248. ptr = ec_dev->din;
  249. /* check response error code */
  250. ec_msg->result = ptr[0];
  251. ret = cros_ec_check_result(ec_dev, ec_msg);
  252. if (ret)
  253. goto exit;
  254. len = ptr[1];
  255. sum = ptr[0] + ptr[1];
  256. if (len > ec_msg->insize) {
  257. dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
  258. len, ec_msg->insize);
  259. ret = -ENOSPC;
  260. goto exit;
  261. }
  262. /* copy response packet payload and compute checksum */
  263. for (i = 0; i < len; i++) {
  264. sum += ptr[i + 2];
  265. if (ec_msg->insize)
  266. ec_msg->indata[i] = ptr[i + 2];
  267. }
  268. sum &= 0xff;
  269. debug_packet(ec_dev->dev, "in", ptr, len + 3);
  270. if (sum != ptr[len + 2]) {
  271. dev_err(ec_dev->dev,
  272. "bad packet checksum, expected %02x, got %02x\n",
  273. sum, ptr[len + 2]);
  274. ret = -EBADMSG;
  275. goto exit;
  276. }
  277. ret = len;
  278. exit:
  279. if (ec_msg->command == EC_CMD_REBOOT_EC)
  280. msleep(EC_REBOOT_DELAY_MS);
  281. return ret;
  282. }
  283. static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
  284. {
  285. struct device_node *np = dev->of_node;
  286. u32 val;
  287. int ret;
  288. ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val);
  289. if (!ret)
  290. ec_spi->end_of_msg_delay = val;
  291. }
  292. static int cros_ec_spi_probe(struct spi_device *spi)
  293. {
  294. struct device *dev = &spi->dev;
  295. struct cros_ec_device *ec_dev;
  296. struct cros_ec_spi *ec_spi;
  297. int err;
  298. spi->bits_per_word = 8;
  299. spi->mode = SPI_MODE_0;
  300. err = spi_setup(spi);
  301. if (err < 0)
  302. return err;
  303. ec_spi = devm_kzalloc(dev, sizeof(*ec_spi), GFP_KERNEL);
  304. if (ec_spi == NULL)
  305. return -ENOMEM;
  306. ec_spi->spi = spi;
  307. ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
  308. if (!ec_dev)
  309. return -ENOMEM;
  310. /* Check for any DT properties */
  311. cros_ec_spi_dt_probe(ec_spi, dev);
  312. spi_set_drvdata(spi, ec_dev);
  313. ec_dev->dev = dev;
  314. ec_dev->priv = ec_spi;
  315. ec_dev->irq = spi->irq;
  316. ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi;
  317. ec_dev->ec_name = ec_spi->spi->modalias;
  318. ec_dev->phys_name = dev_name(&ec_spi->spi->dev);
  319. ec_dev->parent = &ec_spi->spi->dev;
  320. ec_dev->din_size = EC_MSG_BYTES + EC_MSG_PREAMBLE_COUNT;
  321. ec_dev->dout_size = EC_MSG_BYTES;
  322. err = cros_ec_register(ec_dev);
  323. if (err) {
  324. dev_err(dev, "cannot register EC\n");
  325. return err;
  326. }
  327. device_init_wakeup(&spi->dev, true);
  328. return 0;
  329. }
  330. static int cros_ec_spi_remove(struct spi_device *spi)
  331. {
  332. struct cros_ec_device *ec_dev;
  333. ec_dev = spi_get_drvdata(spi);
  334. cros_ec_remove(ec_dev);
  335. return 0;
  336. }
  337. #ifdef CONFIG_PM_SLEEP
  338. static int cros_ec_spi_suspend(struct device *dev)
  339. {
  340. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  341. return cros_ec_suspend(ec_dev);
  342. }
  343. static int cros_ec_spi_resume(struct device *dev)
  344. {
  345. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  346. return cros_ec_resume(ec_dev);
  347. }
  348. #endif
  349. static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops, cros_ec_spi_suspend,
  350. cros_ec_spi_resume);
  351. static const struct spi_device_id cros_ec_spi_id[] = {
  352. { "cros-ec-spi", 0 },
  353. { }
  354. };
  355. MODULE_DEVICE_TABLE(spi, cros_ec_spi_id);
  356. static struct spi_driver cros_ec_driver_spi = {
  357. .driver = {
  358. .name = "cros-ec-spi",
  359. .owner = THIS_MODULE,
  360. .pm = &cros_ec_spi_pm_ops,
  361. },
  362. .probe = cros_ec_spi_probe,
  363. .remove = cros_ec_spi_remove,
  364. .id_table = cros_ec_spi_id,
  365. };
  366. module_spi_driver(cros_ec_driver_spi);
  367. MODULE_LICENSE("GPL v2");
  368. MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)");