cros_ec_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. * struct cros_ec_spi - information about a SPI-connected EC
  63. *
  64. * @spi: SPI device we are connected to
  65. * @last_transfer_ns: time that we last finished a transfer, or 0 if there
  66. * if no record
  67. * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that
  68. * is sent when we want to turn off CS at the end of a transaction.
  69. * @lock: mutex to ensure only one user of cros_ec_command_spi_xfer at a time
  70. */
  71. struct cros_ec_spi {
  72. struct spi_device *spi;
  73. s64 last_transfer_ns;
  74. unsigned int end_of_msg_delay;
  75. struct mutex lock;
  76. };
  77. static void debug_packet(struct device *dev, const char *name, u8 *ptr,
  78. int len)
  79. {
  80. #ifdef DEBUG
  81. int i;
  82. dev_dbg(dev, "%s: ", name);
  83. for (i = 0; i < len; i++)
  84. pr_cont(" %02x", ptr[i]);
  85. pr_cont("\n");
  86. #endif
  87. }
  88. /**
  89. * cros_ec_spi_receive_response - Receive a response from the EC.
  90. *
  91. * This function has two phases: reading the preamble bytes (since if we read
  92. * data from the EC before it is ready to send, we just get preamble) and
  93. * reading the actual message.
  94. *
  95. * The received data is placed into ec_dev->din.
  96. *
  97. * @ec_dev: ChromeOS EC device
  98. * @need_len: Number of message bytes we need to read
  99. */
  100. static int cros_ec_spi_receive_response(struct cros_ec_device *ec_dev,
  101. int need_len)
  102. {
  103. struct cros_ec_spi *ec_spi = ec_dev->priv;
  104. struct spi_transfer trans;
  105. struct spi_message msg;
  106. u8 *ptr, *end;
  107. int ret;
  108. unsigned long deadline;
  109. int todo;
  110. /* Receive data until we see the header byte */
  111. deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
  112. while (true) {
  113. unsigned long start_jiffies = jiffies;
  114. memset(&trans, 0, sizeof(trans));
  115. trans.cs_change = 1;
  116. trans.rx_buf = ptr = ec_dev->din;
  117. trans.len = EC_MSG_PREAMBLE_COUNT;
  118. spi_message_init(&msg);
  119. spi_message_add_tail(&trans, &msg);
  120. ret = spi_sync(ec_spi->spi, &msg);
  121. if (ret < 0) {
  122. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  123. return ret;
  124. }
  125. for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) {
  126. if (*ptr == EC_MSG_HEADER) {
  127. dev_dbg(ec_dev->dev, "msg found at %zd\n",
  128. ptr - ec_dev->din);
  129. break;
  130. }
  131. }
  132. if (ptr != end)
  133. break;
  134. /*
  135. * Use the time at the start of the loop as a timeout. This
  136. * gives us one last shot at getting the transfer and is useful
  137. * in case we got context switched out for a while.
  138. */
  139. if (time_after(start_jiffies, deadline)) {
  140. dev_warn(ec_dev->dev, "EC failed to respond in time\n");
  141. return -ETIMEDOUT;
  142. }
  143. }
  144. /*
  145. * ptr now points to the header byte. Copy any valid data to the
  146. * start of our buffer
  147. */
  148. todo = end - ++ptr;
  149. BUG_ON(todo < 0 || todo > ec_dev->din_size);
  150. todo = min(todo, need_len);
  151. memmove(ec_dev->din, ptr, todo);
  152. ptr = ec_dev->din + todo;
  153. dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n",
  154. need_len, todo);
  155. need_len -= todo;
  156. /* Receive data until we have it all */
  157. while (need_len > 0) {
  158. /*
  159. * We can't support transfers larger than the SPI FIFO size
  160. * unless we have DMA. We don't have DMA on the ISP SPI ports
  161. * for Exynos. We need a way of asking SPI driver for
  162. * maximum-supported transfer size.
  163. */
  164. todo = min(need_len, 256);
  165. dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n",
  166. todo, need_len, ptr - ec_dev->din);
  167. memset(&trans, 0, sizeof(trans));
  168. trans.cs_change = 1;
  169. trans.rx_buf = ptr;
  170. trans.len = todo;
  171. spi_message_init(&msg);
  172. spi_message_add_tail(&trans, &msg);
  173. /* send command to EC and read answer */
  174. BUG_ON((u8 *)trans.rx_buf - ec_dev->din + todo >
  175. ec_dev->din_size);
  176. ret = spi_sync(ec_spi->spi, &msg);
  177. if (ret < 0) {
  178. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  179. return ret;
  180. }
  181. debug_packet(ec_dev->dev, "interim", ptr, todo);
  182. ptr += todo;
  183. need_len -= todo;
  184. }
  185. dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
  186. return 0;
  187. }
  188. /**
  189. * cros_ec_command_spi_xfer - Transfer a message over SPI and receive the reply
  190. *
  191. * @ec_dev: ChromeOS EC device
  192. * @ec_msg: Message to transfer
  193. */
  194. static int cros_ec_command_spi_xfer(struct cros_ec_device *ec_dev,
  195. struct cros_ec_msg *ec_msg)
  196. {
  197. struct cros_ec_spi *ec_spi = ec_dev->priv;
  198. struct spi_transfer trans;
  199. struct spi_message msg;
  200. int i, len;
  201. u8 *ptr;
  202. int sum;
  203. int ret = 0, final_ret;
  204. struct timespec ts;
  205. /*
  206. * We have the shared ec_dev buffer plus we do lots of separate spi_sync
  207. * calls, so we need to make sure only one person is using this at a
  208. * time.
  209. */
  210. mutex_lock(&ec_spi->lock);
  211. len = cros_ec_prepare_tx(ec_dev, ec_msg);
  212. dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
  213. /* If it's too soon to do another transaction, wait */
  214. if (ec_spi->last_transfer_ns) {
  215. struct timespec ts;
  216. unsigned long delay; /* The delay completed so far */
  217. ktime_get_ts(&ts);
  218. delay = timespec_to_ns(&ts) - ec_spi->last_transfer_ns;
  219. if (delay < EC_SPI_RECOVERY_TIME_NS)
  220. ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
  221. }
  222. /* Transmit phase - send our message */
  223. debug_packet(ec_dev->dev, "out", ec_dev->dout, len);
  224. memset(&trans, 0, sizeof(trans));
  225. trans.tx_buf = ec_dev->dout;
  226. trans.len = len;
  227. trans.cs_change = 1;
  228. spi_message_init(&msg);
  229. spi_message_add_tail(&trans, &msg);
  230. ret = spi_sync(ec_spi->spi, &msg);
  231. /* Get the response */
  232. if (!ret) {
  233. ret = cros_ec_spi_receive_response(ec_dev,
  234. ec_msg->in_len + EC_MSG_TX_PROTO_BYTES);
  235. } else {
  236. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  237. }
  238. /* turn off CS */
  239. spi_message_init(&msg);
  240. if (ec_spi->end_of_msg_delay) {
  241. /*
  242. * Add delay for last transaction, to ensure the rising edge
  243. * doesn't come too soon after the end of the data.
  244. */
  245. memset(&trans, 0, sizeof(trans));
  246. trans.delay_usecs = ec_spi->end_of_msg_delay;
  247. spi_message_add_tail(&trans, &msg);
  248. }
  249. final_ret = spi_sync(ec_spi->spi, &msg);
  250. ktime_get_ts(&ts);
  251. ec_spi->last_transfer_ns = timespec_to_ns(&ts);
  252. if (!ret)
  253. ret = final_ret;
  254. if (ret < 0) {
  255. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  256. goto exit;
  257. }
  258. /* check response error code */
  259. ptr = ec_dev->din;
  260. if (ptr[0]) {
  261. dev_warn(ec_dev->dev, "command 0x%02x returned an error %d\n",
  262. ec_msg->cmd, ptr[0]);
  263. debug_packet(ec_dev->dev, "in_err", ptr, len);
  264. ret = -EINVAL;
  265. goto exit;
  266. }
  267. len = ptr[1];
  268. sum = ptr[0] + ptr[1];
  269. if (len > ec_msg->in_len) {
  270. dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
  271. len, ec_msg->in_len);
  272. ret = -ENOSPC;
  273. goto exit;
  274. }
  275. /* copy response packet payload and compute checksum */
  276. for (i = 0; i < len; i++) {
  277. sum += ptr[i + 2];
  278. if (ec_msg->in_len)
  279. ec_msg->in_buf[i] = ptr[i + 2];
  280. }
  281. sum &= 0xff;
  282. debug_packet(ec_dev->dev, "in", ptr, len + 3);
  283. if (sum != ptr[len + 2]) {
  284. dev_err(ec_dev->dev,
  285. "bad packet checksum, expected %02x, got %02x\n",
  286. sum, ptr[len + 2]);
  287. ret = -EBADMSG;
  288. goto exit;
  289. }
  290. ret = 0;
  291. exit:
  292. mutex_unlock(&ec_spi->lock);
  293. return ret;
  294. }
  295. static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
  296. {
  297. struct device_node *np = dev->of_node;
  298. u32 val;
  299. int ret;
  300. ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val);
  301. if (!ret)
  302. ec_spi->end_of_msg_delay = val;
  303. }
  304. static int cros_ec_spi_probe(struct spi_device *spi)
  305. {
  306. struct device *dev = &spi->dev;
  307. struct cros_ec_device *ec_dev;
  308. struct cros_ec_spi *ec_spi;
  309. int err;
  310. spi->bits_per_word = 8;
  311. spi->mode = SPI_MODE_0;
  312. err = spi_setup(spi);
  313. if (err < 0)
  314. return err;
  315. ec_spi = devm_kzalloc(dev, sizeof(*ec_spi), GFP_KERNEL);
  316. if (ec_spi == NULL)
  317. return -ENOMEM;
  318. ec_spi->spi = spi;
  319. mutex_init(&ec_spi->lock);
  320. ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
  321. if (!ec_dev)
  322. return -ENOMEM;
  323. /* Check for any DT properties */
  324. cros_ec_spi_dt_probe(ec_spi, dev);
  325. spi_set_drvdata(spi, ec_dev);
  326. ec_dev->name = "SPI";
  327. ec_dev->dev = dev;
  328. ec_dev->priv = ec_spi;
  329. ec_dev->irq = spi->irq;
  330. ec_dev->command_xfer = cros_ec_command_spi_xfer;
  331. ec_dev->ec_name = ec_spi->spi->modalias;
  332. ec_dev->phys_name = dev_name(&ec_spi->spi->dev);
  333. ec_dev->parent = &ec_spi->spi->dev;
  334. ec_dev->din_size = EC_MSG_BYTES + EC_MSG_PREAMBLE_COUNT;
  335. ec_dev->dout_size = EC_MSG_BYTES;
  336. err = cros_ec_register(ec_dev);
  337. if (err) {
  338. dev_err(dev, "cannot register EC\n");
  339. return err;
  340. }
  341. return 0;
  342. }
  343. static int cros_ec_spi_remove(struct spi_device *spi)
  344. {
  345. struct cros_ec_device *ec_dev;
  346. ec_dev = spi_get_drvdata(spi);
  347. cros_ec_remove(ec_dev);
  348. return 0;
  349. }
  350. #ifdef CONFIG_PM_SLEEP
  351. static int cros_ec_spi_suspend(struct device *dev)
  352. {
  353. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  354. return cros_ec_suspend(ec_dev);
  355. }
  356. static int cros_ec_spi_resume(struct device *dev)
  357. {
  358. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  359. return cros_ec_resume(ec_dev);
  360. }
  361. #endif
  362. static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops, cros_ec_spi_suspend,
  363. cros_ec_spi_resume);
  364. static const struct spi_device_id cros_ec_spi_id[] = {
  365. { "cros-ec-spi", 0 },
  366. { }
  367. };
  368. MODULE_DEVICE_TABLE(spi, cros_ec_spi_id);
  369. static struct spi_driver cros_ec_driver_spi = {
  370. .driver = {
  371. .name = "cros-ec-spi",
  372. .owner = THIS_MODULE,
  373. .pm = &cros_ec_spi_pm_ops,
  374. },
  375. .probe = cros_ec_spi_probe,
  376. .remove = cros_ec_spi_remove,
  377. .id_table = cros_ec_spi_id,
  378. };
  379. module_spi_driver(cros_ec_driver_spi);
  380. MODULE_LICENSE("GPL v2");
  381. MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)");