cros_ec_spi.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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/platform_device.h>
  21. #include <linux/slab.h>
  22. #include <linux/spi/spi.h>
  23. /* The header byte, which follows the preamble */
  24. #define EC_MSG_HEADER 0xec
  25. /*
  26. * Number of EC preamble bytes we read at a time. Since it takes
  27. * about 400-500us for the EC to respond there is not a lot of
  28. * point in tuning this. If the EC could respond faster then
  29. * we could increase this so that might expect the preamble and
  30. * message to occur in a single transaction. However, the maximum
  31. * SPI transfer size is 256 bytes, so at 5MHz we need a response
  32. * time of perhaps <320us (200 bytes / 1600 bits).
  33. */
  34. #define EC_MSG_PREAMBLE_COUNT 32
  35. /*
  36. * We must get a response from the EC in 5ms. This is a very long
  37. * time, but the flash write command can take 2-3ms. The EC command
  38. * processing is currently not very fast (about 500us). We could
  39. * look at speeding this up and making the flash write command a
  40. * 'slow' command, requiring a GET_STATUS wait loop, like flash
  41. * erase.
  42. */
  43. #define EC_MSG_DEADLINE_MS 5
  44. /*
  45. * Time between raising the SPI chip select (for the end of a
  46. * transaction) and dropping it again (for the next transaction).
  47. * If we go too fast, the EC will miss the transaction. We know that we
  48. * need at least 70 us with the 16 MHz STM32 EC, so go with 200 us to be
  49. * safe.
  50. */
  51. #define EC_SPI_RECOVERY_TIME_NS (200 * 1000)
  52. /**
  53. * struct cros_ec_spi - information about a SPI-connected EC
  54. *
  55. * @spi: SPI device we are connected to
  56. * @last_transfer_ns: time that we last finished a transfer, or 0 if there
  57. * if no record
  58. */
  59. struct cros_ec_spi {
  60. struct spi_device *spi;
  61. s64 last_transfer_ns;
  62. };
  63. static void debug_packet(struct device *dev, const char *name, u8 *ptr,
  64. int len)
  65. {
  66. #ifdef DEBUG
  67. int i;
  68. dev_dbg(dev, "%s: ", name);
  69. for (i = 0; i < len; i++)
  70. pr_cont(" %02x", ptr[i]);
  71. pr_cont("\n");
  72. #endif
  73. }
  74. /**
  75. * cros_ec_spi_receive_response - Receive a response from the EC.
  76. *
  77. * This function has two phases: reading the preamble bytes (since if we read
  78. * data from the EC before it is ready to send, we just get preamble) and
  79. * reading the actual message.
  80. *
  81. * The received data is placed into ec_dev->din.
  82. *
  83. * @ec_dev: ChromeOS EC device
  84. * @need_len: Number of message bytes we need to read
  85. */
  86. static int cros_ec_spi_receive_response(struct cros_ec_device *ec_dev,
  87. int need_len)
  88. {
  89. struct cros_ec_spi *ec_spi = ec_dev->priv;
  90. struct spi_transfer trans;
  91. struct spi_message msg;
  92. u8 *ptr, *end;
  93. int ret;
  94. unsigned long deadline;
  95. int todo;
  96. /* Receive data until we see the header byte */
  97. deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
  98. do {
  99. memset(&trans, '\0', sizeof(trans));
  100. trans.cs_change = 1;
  101. trans.rx_buf = ptr = ec_dev->din;
  102. trans.len = EC_MSG_PREAMBLE_COUNT;
  103. spi_message_init(&msg);
  104. spi_message_add_tail(&trans, &msg);
  105. ret = spi_sync(ec_spi->spi, &msg);
  106. if (ret < 0) {
  107. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  108. return ret;
  109. }
  110. for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) {
  111. if (*ptr == EC_MSG_HEADER) {
  112. dev_dbg(ec_dev->dev, "msg found at %zd\n",
  113. ptr - ec_dev->din);
  114. break;
  115. }
  116. }
  117. if (time_after(jiffies, deadline)) {
  118. dev_warn(ec_dev->dev, "EC failed to respond in time\n");
  119. return -ETIMEDOUT;
  120. }
  121. } while (ptr == end);
  122. /*
  123. * ptr now points to the header byte. Copy any valid data to the
  124. * start of our buffer
  125. */
  126. todo = end - ++ptr;
  127. BUG_ON(todo < 0 || todo > ec_dev->din_size);
  128. todo = min(todo, need_len);
  129. memmove(ec_dev->din, ptr, todo);
  130. ptr = ec_dev->din + todo;
  131. dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n",
  132. need_len, todo);
  133. need_len -= todo;
  134. /* Receive data until we have it all */
  135. while (need_len > 0) {
  136. /*
  137. * We can't support transfers larger than the SPI FIFO size
  138. * unless we have DMA. We don't have DMA on the ISP SPI ports
  139. * for Exynos. We need a way of asking SPI driver for
  140. * maximum-supported transfer size.
  141. */
  142. todo = min(need_len, 256);
  143. dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n",
  144. todo, need_len, ptr - ec_dev->din);
  145. memset(&trans, '\0', sizeof(trans));
  146. trans.cs_change = 1;
  147. trans.rx_buf = ptr;
  148. trans.len = todo;
  149. spi_message_init(&msg);
  150. spi_message_add_tail(&trans, &msg);
  151. /* send command to EC and read answer */
  152. BUG_ON((u8 *)trans.rx_buf - ec_dev->din + todo >
  153. ec_dev->din_size);
  154. ret = spi_sync(ec_spi->spi, &msg);
  155. if (ret < 0) {
  156. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  157. return ret;
  158. }
  159. debug_packet(ec_dev->dev, "interim", ptr, todo);
  160. ptr += todo;
  161. need_len -= todo;
  162. }
  163. dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
  164. return 0;
  165. }
  166. /**
  167. * cros_ec_command_spi_xfer - Transfer a message over SPI and receive the reply
  168. *
  169. * @ec_dev: ChromeOS EC device
  170. * @ec_msg: Message to transfer
  171. */
  172. static int cros_ec_command_spi_xfer(struct cros_ec_device *ec_dev,
  173. struct cros_ec_msg *ec_msg)
  174. {
  175. struct cros_ec_spi *ec_spi = ec_dev->priv;
  176. struct spi_transfer trans;
  177. struct spi_message msg;
  178. int i, len;
  179. u8 *ptr;
  180. int sum;
  181. int ret = 0, final_ret;
  182. struct timespec ts;
  183. len = cros_ec_prepare_tx(ec_dev, ec_msg);
  184. dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
  185. /* If it's too soon to do another transaction, wait */
  186. if (ec_spi->last_transfer_ns) {
  187. struct timespec ts;
  188. unsigned long delay; /* The delay completed so far */
  189. ktime_get_ts(&ts);
  190. delay = timespec_to_ns(&ts) - ec_spi->last_transfer_ns;
  191. if (delay < EC_SPI_RECOVERY_TIME_NS)
  192. ndelay(delay);
  193. }
  194. /* Transmit phase - send our message */
  195. debug_packet(ec_dev->dev, "out", ec_dev->dout, len);
  196. memset(&trans, '\0', sizeof(trans));
  197. trans.tx_buf = ec_dev->dout;
  198. trans.len = len;
  199. trans.cs_change = 1;
  200. spi_message_init(&msg);
  201. spi_message_add_tail(&trans, &msg);
  202. ret = spi_sync(ec_spi->spi, &msg);
  203. /* Get the response */
  204. if (!ret) {
  205. ret = cros_ec_spi_receive_response(ec_dev,
  206. ec_msg->in_len + EC_MSG_TX_PROTO_BYTES);
  207. } else {
  208. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  209. }
  210. /* turn off CS */
  211. spi_message_init(&msg);
  212. final_ret = spi_sync(ec_spi->spi, &msg);
  213. ktime_get_ts(&ts);
  214. ec_spi->last_transfer_ns = timespec_to_ns(&ts);
  215. if (!ret)
  216. ret = final_ret;
  217. if (ret < 0) {
  218. dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
  219. return ret;
  220. }
  221. /* check response error code */
  222. ptr = ec_dev->din;
  223. if (ptr[0]) {
  224. dev_warn(ec_dev->dev, "command 0x%02x returned an error %d\n",
  225. ec_msg->cmd, ptr[0]);
  226. debug_packet(ec_dev->dev, "in_err", ptr, len);
  227. return -EINVAL;
  228. }
  229. len = ptr[1];
  230. sum = ptr[0] + ptr[1];
  231. if (len > ec_msg->in_len) {
  232. dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
  233. len, ec_msg->in_len);
  234. return -ENOSPC;
  235. }
  236. /* copy response packet payload and compute checksum */
  237. for (i = 0; i < len; i++) {
  238. sum += ptr[i + 2];
  239. if (ec_msg->in_len)
  240. ec_msg->in_buf[i] = ptr[i + 2];
  241. }
  242. sum &= 0xff;
  243. debug_packet(ec_dev->dev, "in", ptr, len + 3);
  244. if (sum != ptr[len + 2]) {
  245. dev_err(ec_dev->dev,
  246. "bad packet checksum, expected %02x, got %02x\n",
  247. sum, ptr[len + 2]);
  248. return -EBADMSG;
  249. }
  250. return 0;
  251. }
  252. static int cros_ec_probe_spi(struct spi_device *spi)
  253. {
  254. struct device *dev = &spi->dev;
  255. struct cros_ec_device *ec_dev;
  256. struct cros_ec_spi *ec_spi;
  257. int err;
  258. spi->bits_per_word = 8;
  259. spi->mode = SPI_MODE_0;
  260. err = spi_setup(spi);
  261. if (err < 0)
  262. return err;
  263. ec_spi = devm_kzalloc(dev, sizeof(*ec_spi), GFP_KERNEL);
  264. if (ec_spi == NULL)
  265. return -ENOMEM;
  266. ec_spi->spi = spi;
  267. ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
  268. if (!ec_dev)
  269. return -ENOMEM;
  270. spi_set_drvdata(spi, ec_dev);
  271. ec_dev->name = "SPI";
  272. ec_dev->dev = dev;
  273. ec_dev->priv = ec_spi;
  274. ec_dev->irq = spi->irq;
  275. ec_dev->command_xfer = cros_ec_command_spi_xfer;
  276. ec_dev->ec_name = ec_spi->spi->modalias;
  277. ec_dev->phys_name = dev_name(&ec_spi->spi->dev);
  278. ec_dev->parent = &ec_spi->spi->dev;
  279. ec_dev->din_size = EC_MSG_BYTES + EC_MSG_PREAMBLE_COUNT;
  280. ec_dev->dout_size = EC_MSG_BYTES;
  281. err = cros_ec_register(ec_dev);
  282. if (err) {
  283. dev_err(dev, "cannot register EC\n");
  284. return err;
  285. }
  286. return 0;
  287. }
  288. static int cros_ec_remove_spi(struct spi_device *spi)
  289. {
  290. struct cros_ec_device *ec_dev;
  291. ec_dev = spi_get_drvdata(spi);
  292. cros_ec_remove(ec_dev);
  293. return 0;
  294. }
  295. #ifdef CONFIG_PM_SLEEP
  296. static int cros_ec_spi_suspend(struct device *dev)
  297. {
  298. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  299. return cros_ec_suspend(ec_dev);
  300. }
  301. static int cros_ec_spi_resume(struct device *dev)
  302. {
  303. struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
  304. return cros_ec_resume(ec_dev);
  305. }
  306. #endif
  307. static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops, cros_ec_spi_suspend,
  308. cros_ec_spi_resume);
  309. static const struct spi_device_id cros_ec_spi_id[] = {
  310. { "cros-ec-spi", 0 },
  311. { }
  312. };
  313. MODULE_DEVICE_TABLE(spi, cros_ec_spi_id);
  314. static struct spi_driver cros_ec_driver_spi = {
  315. .driver = {
  316. .name = "cros-ec-spi",
  317. .owner = THIS_MODULE,
  318. .pm = &cros_ec_spi_pm_ops,
  319. },
  320. .probe = cros_ec_probe_spi,
  321. .remove = cros_ec_remove_spi,
  322. .id_table = cros_ec_spi_id,
  323. };
  324. module_spi_driver(cros_ec_driver_spi);
  325. MODULE_LICENSE("GPL");
  326. MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)");