2 * ChromeOS EC multi-function device (SPI)
4 * Copyright (C) 2012 Google, Inc
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.
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.
16 #include <linux/delay.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/mfd/cros_ec.h>
20 #include <linux/mfd/cros_ec_commands.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/spi/spi.h>
27 /* The header byte, which follows the preamble */
28 #define EC_MSG_HEADER 0xec
31 * Number of EC preamble bytes we read at a time. Since it takes
32 * about 400-500us for the EC to respond there is not a lot of
33 * point in tuning this. If the EC could respond faster then
34 * we could increase this so that might expect the preamble and
35 * message to occur in a single transaction. However, the maximum
36 * SPI transfer size is 256 bytes, so at 5MHz we need a response
37 * time of perhaps <320us (200 bytes / 1600 bits).
39 #define EC_MSG_PREAMBLE_COUNT 32
42 * Allow for a long time for the EC to respond. We support i2c
43 * tunneling and support fairly long messages for the tunnel (249
44 * bytes long at the moment). If we're talking to a 100 kHz device
45 * on the other end and need to transfer ~256 bytes, then we need:
46 * 10 us/bit * ~10 bits/byte * ~256 bytes = ~25ms
48 * We'll wait 8 times that to handle clock stretching and other
49 * paranoia. Note that some battery gas gauge ICs claim to have a
50 * clock stretch of 144ms in rare situations. That's incentive for
51 * not directly passing i2c through, but it's too late for that for
54 * It's pretty unlikely that we'll really see a 249 byte tunnel in
55 * anything other than testing. If this was more common we might
56 * consider having slow commands like this require a GET_STATUS
57 * wait loop. The 'flash write' command would be another candidate
58 * for this, clocking in at 2-3ms.
60 #define EC_MSG_DEADLINE_MS 200
63 * Time between raising the SPI chip select (for the end of a
64 * transaction) and dropping it again (for the next transaction).
65 * If we go too fast, the EC will miss the transaction. We know that we
66 * need at least 70 us with the 16 MHz STM32 EC, so go with 200 us to be
69 #define EC_SPI_RECOVERY_TIME_NS (200 * 1000)
72 * struct cros_ec_spi - information about a SPI-connected EC
74 * @spi: SPI device we are connected to
75 * @last_transfer_ns: time that we last finished a transfer, or 0 if there
77 * @start_of_msg_delay: used to set the delay_usecs on the spi_transfer that
78 * is sent when we want to turn on CS at the start of a transaction.
79 * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that
80 * is sent when we want to turn off CS at the end of a transaction.
83 struct spi_device *spi;
85 unsigned int start_of_msg_delay;
86 unsigned int end_of_msg_delay;
89 static void debug_packet(struct device *dev, const char *name, u8 *ptr,
95 dev_dbg(dev, "%s: ", name);
96 for (i = 0; i < len; i++)
97 pr_cont(" %02x", ptr[i]);
103 static int terminate_request(struct cros_ec_device *ec_dev)
105 struct cros_ec_spi *ec_spi = ec_dev->priv;
106 struct spi_message msg;
107 struct spi_transfer trans;
111 * Turn off CS, possibly adding a delay to ensure the rising edge
112 * doesn't come too soon after the end of the data.
114 spi_message_init(&msg);
115 memset(&trans, 0, sizeof(trans));
116 trans.delay_usecs = ec_spi->end_of_msg_delay;
117 spi_message_add_tail(&trans, &msg);
119 ret = spi_sync_locked(ec_spi->spi, &msg);
121 /* Reset end-of-response timer */
122 ec_spi->last_transfer_ns = ktime_get_ns();
125 "cs-deassert spi transfer failed: %d\n",
133 * receive_n_bytes - receive n bytes from the EC.
135 * Assumes buf is a pointer into the ec_dev->din buffer
137 static int receive_n_bytes(struct cros_ec_device *ec_dev, u8 *buf, int n)
139 struct cros_ec_spi *ec_spi = ec_dev->priv;
140 struct spi_transfer trans;
141 struct spi_message msg;
144 BUG_ON(buf - ec_dev->din + n > ec_dev->din_size);
146 memset(&trans, 0, sizeof(trans));
151 spi_message_init(&msg);
152 spi_message_add_tail(&trans, &msg);
153 ret = spi_sync_locked(ec_spi->spi, &msg);
155 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
161 * cros_ec_spi_receive_packet - Receive a packet from the EC.
163 * This function has two phases: reading the preamble bytes (since if we read
164 * data from the EC before it is ready to send, we just get preamble) and
165 * reading the actual message.
167 * The received data is placed into ec_dev->din.
169 * @ec_dev: ChromeOS EC device
170 * @need_len: Number of message bytes we need to read
172 static int cros_ec_spi_receive_packet(struct cros_ec_device *ec_dev,
175 struct ec_host_response *response;
178 unsigned long deadline;
181 BUG_ON(ec_dev->din_size < EC_MSG_PREAMBLE_COUNT);
183 /* Receive data until we see the header byte */
184 deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
186 unsigned long start_jiffies = jiffies;
188 ret = receive_n_bytes(ec_dev,
190 EC_MSG_PREAMBLE_COUNT);
195 for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) {
196 if (*ptr == EC_SPI_FRAME_START) {
197 dev_dbg(ec_dev->dev, "msg found at %zd\n",
206 * Use the time at the start of the loop as a timeout. This
207 * gives us one last shot at getting the transfer and is useful
208 * in case we got context switched out for a while.
210 if (time_after(start_jiffies, deadline)) {
211 dev_warn(ec_dev->dev, "EC failed to respond in time\n");
217 * ptr now points to the header byte. Copy any valid data to the
218 * start of our buffer
221 BUG_ON(todo < 0 || todo > ec_dev->din_size);
222 todo = min(todo, need_len);
223 memmove(ec_dev->din, ptr, todo);
224 ptr = ec_dev->din + todo;
225 dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n",
229 /* If the entire response struct wasn't read, get the rest of it. */
230 if (todo < sizeof(*response)) {
231 ret = receive_n_bytes(ec_dev, ptr, sizeof(*response) - todo);
234 ptr += (sizeof(*response) - todo);
235 todo = sizeof(*response);
238 response = (struct ec_host_response *)ec_dev->din;
240 /* Abort if data_len is too large. */
241 if (response->data_len > ec_dev->din_size)
244 /* Receive data until we have it all */
245 while (need_len > 0) {
247 * We can't support transfers larger than the SPI FIFO size
248 * unless we have DMA. We don't have DMA on the ISP SPI ports
249 * for Exynos. We need a way of asking SPI driver for
250 * maximum-supported transfer size.
252 todo = min(need_len, 256);
253 dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n",
254 todo, need_len, ptr - ec_dev->din);
256 ret = receive_n_bytes(ec_dev, ptr, todo);
264 dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
270 * cros_ec_spi_receive_response - Receive a response from the EC.
272 * This function has two phases: reading the preamble bytes (since if we read
273 * data from the EC before it is ready to send, we just get preamble) and
274 * reading the actual message.
276 * The received data is placed into ec_dev->din.
278 * @ec_dev: ChromeOS EC device
279 * @need_len: Number of message bytes we need to read
281 static int cros_ec_spi_receive_response(struct cros_ec_device *ec_dev,
286 unsigned long deadline;
289 BUG_ON(ec_dev->din_size < EC_MSG_PREAMBLE_COUNT);
291 /* Receive data until we see the header byte */
292 deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
294 unsigned long start_jiffies = jiffies;
296 ret = receive_n_bytes(ec_dev,
298 EC_MSG_PREAMBLE_COUNT);
303 for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) {
304 if (*ptr == EC_SPI_FRAME_START) {
305 dev_dbg(ec_dev->dev, "msg found at %zd\n",
314 * Use the time at the start of the loop as a timeout. This
315 * gives us one last shot at getting the transfer and is useful
316 * in case we got context switched out for a while.
318 if (time_after(start_jiffies, deadline)) {
319 dev_warn(ec_dev->dev, "EC failed to respond in time\n");
325 * ptr now points to the header byte. Copy any valid data to the
326 * start of our buffer
329 BUG_ON(todo < 0 || todo > ec_dev->din_size);
330 todo = min(todo, need_len);
331 memmove(ec_dev->din, ptr, todo);
332 ptr = ec_dev->din + todo;
333 dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n",
337 /* Receive data until we have it all */
338 while (need_len > 0) {
340 * We can't support transfers larger than the SPI FIFO size
341 * unless we have DMA. We don't have DMA on the ISP SPI ports
342 * for Exynos. We need a way of asking SPI driver for
343 * maximum-supported transfer size.
345 todo = min(need_len, 256);
346 dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n",
347 todo, need_len, ptr - ec_dev->din);
349 ret = receive_n_bytes(ec_dev, ptr, todo);
353 debug_packet(ec_dev->dev, "interim", ptr, todo);
358 dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
364 * cros_ec_pkt_xfer_spi - Transfer a packet over SPI and receive the reply
366 * @ec_dev: ChromeOS EC device
367 * @ec_msg: Message to transfer
369 static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev,
370 struct cros_ec_command *ec_msg)
372 struct ec_host_response *response;
373 struct cros_ec_spi *ec_spi = ec_dev->priv;
374 struct spi_transfer trans, trans_delay;
375 struct spi_message msg;
380 int ret = 0, final_ret;
382 len = cros_ec_prepare_tx(ec_dev, ec_msg);
383 dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
385 /* If it's too soon to do another transaction, wait */
386 if (ec_spi->last_transfer_ns) {
387 unsigned long delay; /* The delay completed so far */
389 delay = ktime_get_ns() - ec_spi->last_transfer_ns;
390 if (delay < EC_SPI_RECOVERY_TIME_NS)
391 ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
394 rx_buf = kzalloc(len, GFP_KERNEL);
398 spi_bus_lock(ec_spi->spi->master);
401 * Leave a gap between CS assertion and clocking of data to allow the
404 spi_message_init(&msg);
405 if (ec_spi->start_of_msg_delay) {
406 memset(&trans_delay, 0, sizeof(trans_delay));
407 trans_delay.delay_usecs = ec_spi->start_of_msg_delay;
408 spi_message_add_tail(&trans_delay, &msg);
411 /* Transmit phase - send our message */
412 memset(&trans, 0, sizeof(trans));
413 trans.tx_buf = ec_dev->dout;
414 trans.rx_buf = rx_buf;
417 spi_message_add_tail(&trans, &msg);
418 ret = spi_sync_locked(ec_spi->spi, &msg);
420 /* Get the response */
422 /* Verify that EC can process command */
423 for (i = 0; i < len; i++) {
425 case EC_SPI_PAST_END:
426 case EC_SPI_RX_BAD_DATA:
427 case EC_SPI_NOT_READY:
429 ec_msg->result = EC_RES_IN_PROGRESS;
437 ret = cros_ec_spi_receive_packet(ec_dev,
438 ec_msg->insize + sizeof(*response));
440 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
443 final_ret = terminate_request(ec_dev);
445 spi_bus_unlock(ec_spi->spi->master);
454 /* check response error code */
455 response = (struct ec_host_response *)ptr;
456 ec_msg->result = response->result;
458 ret = cros_ec_check_result(ec_dev, ec_msg);
462 len = response->data_len;
464 if (len > ec_msg->insize) {
465 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
466 len, ec_msg->insize);
471 for (i = 0; i < sizeof(*response); i++)
474 /* copy response packet payload and compute checksum */
475 memcpy(ec_msg->data, ptr + sizeof(*response), len);
476 for (i = 0; i < len; i++)
477 sum += ec_msg->data[i];
481 "bad packet checksum, calculated %x\n",
490 if (ec_msg->command == EC_CMD_REBOOT_EC)
491 msleep(EC_REBOOT_DELAY_MS);
497 * cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply
499 * @ec_dev: ChromeOS EC device
500 * @ec_msg: Message to transfer
502 static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
503 struct cros_ec_command *ec_msg)
505 struct cros_ec_spi *ec_spi = ec_dev->priv;
506 struct spi_transfer trans;
507 struct spi_message msg;
512 int ret = 0, final_ret;
514 len = cros_ec_prepare_tx(ec_dev, ec_msg);
515 dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
517 /* If it's too soon to do another transaction, wait */
518 if (ec_spi->last_transfer_ns) {
519 unsigned long delay; /* The delay completed so far */
521 delay = ktime_get_ns() - ec_spi->last_transfer_ns;
522 if (delay < EC_SPI_RECOVERY_TIME_NS)
523 ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
526 rx_buf = kzalloc(len, GFP_KERNEL);
530 spi_bus_lock(ec_spi->spi->master);
532 /* Transmit phase - send our message */
533 debug_packet(ec_dev->dev, "out", ec_dev->dout, len);
534 memset(&trans, 0, sizeof(trans));
535 trans.tx_buf = ec_dev->dout;
536 trans.rx_buf = rx_buf;
539 spi_message_init(&msg);
540 spi_message_add_tail(&trans, &msg);
541 ret = spi_sync_locked(ec_spi->spi, &msg);
543 /* Get the response */
545 /* Verify that EC can process command */
546 for (i = 0; i < len; i++) {
548 case EC_SPI_PAST_END:
549 case EC_SPI_RX_BAD_DATA:
550 case EC_SPI_NOT_READY:
552 ec_msg->result = EC_RES_IN_PROGRESS;
560 ret = cros_ec_spi_receive_response(ec_dev,
561 ec_msg->insize + EC_MSG_TX_PROTO_BYTES);
563 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
566 final_ret = terminate_request(ec_dev);
568 spi_bus_unlock(ec_spi->spi->master);
577 /* check response error code */
578 ec_msg->result = ptr[0];
579 ret = cros_ec_check_result(ec_dev, ec_msg);
584 sum = ptr[0] + ptr[1];
585 if (len > ec_msg->insize) {
586 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
587 len, ec_msg->insize);
592 /* copy response packet payload and compute checksum */
593 for (i = 0; i < len; i++) {
596 ec_msg->data[i] = ptr[i + 2];
600 debug_packet(ec_dev->dev, "in", ptr, len + 3);
602 if (sum != ptr[len + 2]) {
604 "bad packet checksum, expected %02x, got %02x\n",
613 if (ec_msg->command == EC_CMD_REBOOT_EC)
614 msleep(EC_REBOOT_DELAY_MS);
619 static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
621 struct device_node *np = dev->of_node;
625 ret = of_property_read_u32(np, "google,cros-ec-spi-pre-delay", &val);
627 ec_spi->start_of_msg_delay = val;
629 ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val);
631 ec_spi->end_of_msg_delay = val;
634 static int cros_ec_spi_probe(struct spi_device *spi)
636 struct device *dev = &spi->dev;
637 struct cros_ec_device *ec_dev;
638 struct cros_ec_spi *ec_spi;
641 spi->bits_per_word = 8;
642 spi->mode = SPI_MODE_0;
643 err = spi_setup(spi);
647 ec_spi = devm_kzalloc(dev, sizeof(*ec_spi), GFP_KERNEL);
651 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
655 /* Check for any DT properties */
656 cros_ec_spi_dt_probe(ec_spi, dev);
658 spi_set_drvdata(spi, ec_dev);
660 ec_dev->priv = ec_spi;
661 ec_dev->irq = spi->irq;
662 ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi;
663 ec_dev->pkt_xfer = cros_ec_pkt_xfer_spi;
664 ec_dev->phys_name = dev_name(&ec_spi->spi->dev);
665 ec_dev->din_size = EC_MSG_PREAMBLE_COUNT +
666 sizeof(struct ec_host_response) +
667 sizeof(struct ec_response_get_protocol_info);
668 ec_dev->dout_size = sizeof(struct ec_host_request);
670 ec_spi->last_transfer_ns = ktime_get_ns();
672 err = cros_ec_register(ec_dev);
674 dev_err(dev, "cannot register EC\n");
678 device_init_wakeup(&spi->dev, true);
683 static int cros_ec_spi_remove(struct spi_device *spi)
685 struct cros_ec_device *ec_dev;
687 ec_dev = spi_get_drvdata(spi);
688 cros_ec_remove(ec_dev);
693 #ifdef CONFIG_PM_SLEEP
694 static int cros_ec_spi_suspend(struct device *dev)
696 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
698 return cros_ec_suspend(ec_dev);
701 static int cros_ec_spi_resume(struct device *dev)
703 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
705 return cros_ec_resume(ec_dev);
709 static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops, cros_ec_spi_suspend,
712 static const struct of_device_id cros_ec_spi_of_match[] = {
713 { .compatible = "google,cros-ec-spi", },
716 MODULE_DEVICE_TABLE(of, cros_ec_spi_of_match);
718 static const struct spi_device_id cros_ec_spi_id[] = {
719 { "cros-ec-spi", 0 },
722 MODULE_DEVICE_TABLE(spi, cros_ec_spi_id);
724 static struct spi_driver cros_ec_driver_spi = {
726 .name = "cros-ec-spi",
727 .of_match_table = of_match_ptr(cros_ec_spi_of_match),
728 .pm = &cros_ec_spi_pm_ops,
730 .probe = cros_ec_spi_probe,
731 .remove = cros_ec_spi_remove,
732 .id_table = cros_ec_spi_id,
735 module_spi_driver(cros_ec_driver_spi);
737 MODULE_LICENSE("GPL v2");
738 MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)");