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 4 times that to handle clock stretching and other
51 * It's pretty unlikely that we'll really see a 249 byte tunnel in
52 * anything other than testing. If this was more common we might
53 * consider having slow commands like this require a GET_STATUS
54 * wait loop. The 'flash write' command would be another candidate
55 * for this, clocking in at 2-3ms.
57 #define EC_MSG_DEADLINE_MS 100
60 * Time between raising the SPI chip select (for the end of a
61 * transaction) and dropping it again (for the next transaction).
62 * If we go too fast, the EC will miss the transaction. We know that we
63 * need at least 70 us with the 16 MHz STM32 EC, so go with 200 us to be
66 #define EC_SPI_RECOVERY_TIME_NS (200 * 1000)
69 * struct cros_ec_spi - information about a SPI-connected EC
71 * @spi: SPI device we are connected to
72 * @last_transfer_ns: time that we last finished a transfer, or 0 if there
74 * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that
75 * is sent when we want to turn off CS at the end of a transaction.
78 struct spi_device
*spi
;
80 unsigned int end_of_msg_delay
;
83 static void debug_packet(struct device
*dev
, const char *name
, u8
*ptr
,
89 dev_dbg(dev
, "%s: ", name
);
90 for (i
= 0; i
< len
; i
++)
91 pr_cont(" %02x", ptr
[i
]);
97 static int terminate_request(struct cros_ec_device
*ec_dev
)
99 struct cros_ec_spi
*ec_spi
= ec_dev
->priv
;
100 struct spi_message msg
;
101 struct spi_transfer trans
;
105 * Turn off CS, possibly adding a delay to ensure the rising edge
106 * doesn't come too soon after the end of the data.
108 spi_message_init(&msg
);
109 memset(&trans
, 0, sizeof(trans
));
110 trans
.delay_usecs
= ec_spi
->end_of_msg_delay
;
111 spi_message_add_tail(&trans
, &msg
);
113 ret
= spi_sync(ec_spi
->spi
, &msg
);
115 /* Reset end-of-response timer */
116 ec_spi
->last_transfer_ns
= ktime_get_ns();
119 "cs-deassert spi transfer failed: %d\n",
127 * receive_n_bytes - receive n bytes from the EC.
129 * Assumes buf is a pointer into the ec_dev->din buffer
131 static int receive_n_bytes(struct cros_ec_device
*ec_dev
, u8
*buf
, int n
)
133 struct cros_ec_spi
*ec_spi
= ec_dev
->priv
;
134 struct spi_transfer trans
;
135 struct spi_message msg
;
138 BUG_ON(buf
- ec_dev
->din
+ n
> ec_dev
->din_size
);
140 memset(&trans
, 0, sizeof(trans
));
145 spi_message_init(&msg
);
146 spi_message_add_tail(&trans
, &msg
);
147 ret
= spi_sync(ec_spi
->spi
, &msg
);
149 dev_err(ec_dev
->dev
, "spi transfer failed: %d\n", ret
);
155 * cros_ec_spi_receive_packet - Receive a packet from the EC.
157 * This function has two phases: reading the preamble bytes (since if we read
158 * data from the EC before it is ready to send, we just get preamble) and
159 * reading the actual message.
161 * The received data is placed into ec_dev->din.
163 * @ec_dev: ChromeOS EC device
164 * @need_len: Number of message bytes we need to read
166 static int cros_ec_spi_receive_packet(struct cros_ec_device
*ec_dev
,
169 struct ec_host_response
*response
;
172 unsigned long deadline
;
175 BUG_ON(EC_MSG_PREAMBLE_COUNT
> ec_dev
->din_size
);
177 /* Receive data until we see the header byte */
178 deadline
= jiffies
+ msecs_to_jiffies(EC_MSG_DEADLINE_MS
);
180 unsigned long start_jiffies
= jiffies
;
182 ret
= receive_n_bytes(ec_dev
,
184 EC_MSG_PREAMBLE_COUNT
);
189 for (end
= ptr
+ EC_MSG_PREAMBLE_COUNT
; ptr
!= end
; ptr
++) {
190 if (*ptr
== EC_SPI_FRAME_START
) {
191 dev_dbg(ec_dev
->dev
, "msg found at %zd\n",
200 * Use the time at the start of the loop as a timeout. This
201 * gives us one last shot at getting the transfer and is useful
202 * in case we got context switched out for a while.
204 if (time_after(start_jiffies
, deadline
)) {
205 dev_warn(ec_dev
->dev
, "EC failed to respond in time\n");
211 * ptr now points to the header byte. Copy any valid data to the
212 * start of our buffer
215 BUG_ON(todo
< 0 || todo
> ec_dev
->din_size
);
216 todo
= min(todo
, need_len
);
217 memmove(ec_dev
->din
, ptr
, todo
);
218 ptr
= ec_dev
->din
+ todo
;
219 dev_dbg(ec_dev
->dev
, "need %d, got %d bytes from preamble\n",
223 /* If the entire response struct wasn't read, get the rest of it. */
224 if (todo
< sizeof(*response
)) {
225 ret
= receive_n_bytes(ec_dev
, ptr
, sizeof(*response
) - todo
);
228 ptr
+= (sizeof(*response
) - todo
);
229 todo
= sizeof(*response
);
232 response
= (struct ec_host_response
*)ec_dev
->din
;
234 /* Abort if data_len is too large. */
235 if (response
->data_len
> ec_dev
->din_size
)
238 /* Receive data until we have it all */
239 while (need_len
> 0) {
241 * We can't support transfers larger than the SPI FIFO size
242 * unless we have DMA. We don't have DMA on the ISP SPI ports
243 * for Exynos. We need a way of asking SPI driver for
244 * maximum-supported transfer size.
246 todo
= min(need_len
, 256);
247 dev_dbg(ec_dev
->dev
, "loop, todo=%d, need_len=%d, ptr=%zd\n",
248 todo
, need_len
, ptr
- ec_dev
->din
);
250 ret
= receive_n_bytes(ec_dev
, ptr
, todo
);
258 dev_dbg(ec_dev
->dev
, "loop done, ptr=%zd\n", ptr
- ec_dev
->din
);
264 * cros_ec_spi_receive_response - Receive a response from the EC.
266 * This function has two phases: reading the preamble bytes (since if we read
267 * data from the EC before it is ready to send, we just get preamble) and
268 * reading the actual message.
270 * The received data is placed into ec_dev->din.
272 * @ec_dev: ChromeOS EC device
273 * @need_len: Number of message bytes we need to read
275 static int cros_ec_spi_receive_response(struct cros_ec_device
*ec_dev
,
280 unsigned long deadline
;
283 BUG_ON(EC_MSG_PREAMBLE_COUNT
> ec_dev
->din_size
);
285 /* Receive data until we see the header byte */
286 deadline
= jiffies
+ msecs_to_jiffies(EC_MSG_DEADLINE_MS
);
288 unsigned long start_jiffies
= jiffies
;
290 ret
= receive_n_bytes(ec_dev
,
292 EC_MSG_PREAMBLE_COUNT
);
297 for (end
= ptr
+ EC_MSG_PREAMBLE_COUNT
; ptr
!= end
; ptr
++) {
298 if (*ptr
== EC_SPI_FRAME_START
) {
299 dev_dbg(ec_dev
->dev
, "msg found at %zd\n",
308 * Use the time at the start of the loop as a timeout. This
309 * gives us one last shot at getting the transfer and is useful
310 * in case we got context switched out for a while.
312 if (time_after(start_jiffies
, deadline
)) {
313 dev_warn(ec_dev
->dev
, "EC failed to respond in time\n");
319 * ptr now points to the header byte. Copy any valid data to the
320 * start of our buffer
323 BUG_ON(todo
< 0 || todo
> ec_dev
->din_size
);
324 todo
= min(todo
, need_len
);
325 memmove(ec_dev
->din
, ptr
, todo
);
326 ptr
= ec_dev
->din
+ todo
;
327 dev_dbg(ec_dev
->dev
, "need %d, got %d bytes from preamble\n",
331 /* Receive data until we have it all */
332 while (need_len
> 0) {
334 * We can't support transfers larger than the SPI FIFO size
335 * unless we have DMA. We don't have DMA on the ISP SPI ports
336 * for Exynos. We need a way of asking SPI driver for
337 * maximum-supported transfer size.
339 todo
= min(need_len
, 256);
340 dev_dbg(ec_dev
->dev
, "loop, todo=%d, need_len=%d, ptr=%zd\n",
341 todo
, need_len
, ptr
- ec_dev
->din
);
343 ret
= receive_n_bytes(ec_dev
, ptr
, todo
);
347 debug_packet(ec_dev
->dev
, "interim", ptr
, todo
);
352 dev_dbg(ec_dev
->dev
, "loop done, ptr=%zd\n", ptr
- ec_dev
->din
);
358 * cros_ec_pkt_xfer_spi - Transfer a packet over SPI and receive the reply
360 * @ec_dev: ChromeOS EC device
361 * @ec_msg: Message to transfer
363 static int cros_ec_pkt_xfer_spi(struct cros_ec_device
*ec_dev
,
364 struct cros_ec_command
*ec_msg
)
366 struct ec_host_request
*request
;
367 struct ec_host_response
*response
;
368 struct cros_ec_spi
*ec_spi
= ec_dev
->priv
;
369 struct spi_transfer trans
;
370 struct spi_message msg
;
375 int ret
= 0, final_ret
;
377 len
= cros_ec_prepare_tx(ec_dev
, ec_msg
);
378 request
= (struct ec_host_request
*)ec_dev
->dout
;
379 dev_dbg(ec_dev
->dev
, "prepared, len=%d\n", len
);
381 /* If it's too soon to do another transaction, wait */
382 if (ec_spi
->last_transfer_ns
) {
383 unsigned long delay
; /* The delay completed so far */
385 delay
= ktime_get_ns() - ec_spi
->last_transfer_ns
;
386 if (delay
< EC_SPI_RECOVERY_TIME_NS
)
387 ndelay(EC_SPI_RECOVERY_TIME_NS
- delay
);
390 rx_buf
= kzalloc(len
, GFP_KERNEL
);
396 /* Transmit phase - send our message */
397 memset(&trans
, 0, sizeof(trans
));
398 trans
.tx_buf
= ec_dev
->dout
;
399 trans
.rx_buf
= rx_buf
;
402 spi_message_init(&msg
);
403 spi_message_add_tail(&trans
, &msg
);
404 ret
= spi_sync(ec_spi
->spi
, &msg
);
406 /* Get the response */
408 /* Verify that EC can process command */
409 for (i
= 0; i
< len
; i
++) {
411 case EC_SPI_PAST_END
:
412 case EC_SPI_RX_BAD_DATA
:
413 case EC_SPI_NOT_READY
:
415 ec_msg
->result
= EC_RES_IN_PROGRESS
;
423 ret
= cros_ec_spi_receive_packet(ec_dev
,
424 ec_msg
->insize
+ sizeof(*response
));
426 dev_err(ec_dev
->dev
, "spi transfer failed: %d\n", ret
);
429 final_ret
= terminate_request(ec_dev
);
437 /* check response error code */
438 response
= (struct ec_host_response
*)ptr
;
439 ec_msg
->result
= response
->result
;
441 ret
= cros_ec_check_result(ec_dev
, ec_msg
);
445 len
= response
->data_len
;
447 if (len
> ec_msg
->insize
) {
448 dev_err(ec_dev
->dev
, "packet too long (%d bytes, expected %d)",
449 len
, ec_msg
->insize
);
454 for (i
= 0; i
< sizeof(*response
); i
++)
457 /* copy response packet payload and compute checksum */
458 memcpy(ec_msg
->data
, ptr
+ sizeof(*response
), len
);
459 for (i
= 0; i
< len
; i
++)
460 sum
+= ec_msg
->data
[i
];
464 "bad packet checksum, calculated %x\n",
473 if (ec_msg
->command
== EC_CMD_REBOOT_EC
)
474 msleep(EC_REBOOT_DELAY_MS
);
480 * cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply
482 * @ec_dev: ChromeOS EC device
483 * @ec_msg: Message to transfer
485 static int cros_ec_cmd_xfer_spi(struct cros_ec_device
*ec_dev
,
486 struct cros_ec_command
*ec_msg
)
488 struct cros_ec_spi
*ec_spi
= ec_dev
->priv
;
489 struct spi_transfer trans
;
490 struct spi_message msg
;
495 int ret
= 0, final_ret
;
497 len
= cros_ec_prepare_tx(ec_dev
, ec_msg
);
498 dev_dbg(ec_dev
->dev
, "prepared, len=%d\n", len
);
500 /* If it's too soon to do another transaction, wait */
501 if (ec_spi
->last_transfer_ns
) {
502 unsigned long delay
; /* The delay completed so far */
504 delay
= ktime_get_ns() - ec_spi
->last_transfer_ns
;
505 if (delay
< EC_SPI_RECOVERY_TIME_NS
)
506 ndelay(EC_SPI_RECOVERY_TIME_NS
- delay
);
509 rx_buf
= kzalloc(len
, GFP_KERNEL
);
515 /* Transmit phase - send our message */
516 debug_packet(ec_dev
->dev
, "out", ec_dev
->dout
, len
);
517 memset(&trans
, 0, sizeof(trans
));
518 trans
.tx_buf
= ec_dev
->dout
;
519 trans
.rx_buf
= rx_buf
;
522 spi_message_init(&msg
);
523 spi_message_add_tail(&trans
, &msg
);
524 ret
= spi_sync(ec_spi
->spi
, &msg
);
526 /* Get the response */
528 /* Verify that EC can process command */
529 for (i
= 0; i
< len
; i
++) {
531 case EC_SPI_PAST_END
:
532 case EC_SPI_RX_BAD_DATA
:
533 case EC_SPI_NOT_READY
:
535 ec_msg
->result
= EC_RES_IN_PROGRESS
;
543 ret
= cros_ec_spi_receive_response(ec_dev
,
544 ec_msg
->insize
+ EC_MSG_TX_PROTO_BYTES
);
546 dev_err(ec_dev
->dev
, "spi transfer failed: %d\n", ret
);
549 final_ret
= terminate_request(ec_dev
);
557 /* check response error code */
558 ec_msg
->result
= ptr
[0];
559 ret
= cros_ec_check_result(ec_dev
, ec_msg
);
564 sum
= ptr
[0] + ptr
[1];
565 if (len
> ec_msg
->insize
) {
566 dev_err(ec_dev
->dev
, "packet too long (%d bytes, expected %d)",
567 len
, ec_msg
->insize
);
572 /* copy response packet payload and compute checksum */
573 for (i
= 0; i
< len
; i
++) {
576 ec_msg
->data
[i
] = ptr
[i
+ 2];
580 debug_packet(ec_dev
->dev
, "in", ptr
, len
+ 3);
582 if (sum
!= ptr
[len
+ 2]) {
584 "bad packet checksum, expected %02x, got %02x\n",
593 if (ec_msg
->command
== EC_CMD_REBOOT_EC
)
594 msleep(EC_REBOOT_DELAY_MS
);
599 static void cros_ec_spi_dt_probe(struct cros_ec_spi
*ec_spi
, struct device
*dev
)
601 struct device_node
*np
= dev
->of_node
;
605 ret
= of_property_read_u32(np
, "google,cros-ec-spi-msg-delay", &val
);
607 ec_spi
->end_of_msg_delay
= val
;
610 static int cros_ec_spi_probe(struct spi_device
*spi
)
612 struct device
*dev
= &spi
->dev
;
613 struct cros_ec_device
*ec_dev
;
614 struct cros_ec_spi
*ec_spi
;
617 spi
->bits_per_word
= 8;
618 spi
->mode
= SPI_MODE_0
;
619 err
= spi_setup(spi
);
623 ec_spi
= devm_kzalloc(dev
, sizeof(*ec_spi
), GFP_KERNEL
);
627 ec_dev
= devm_kzalloc(dev
, sizeof(*ec_dev
), GFP_KERNEL
);
631 /* Check for any DT properties */
632 cros_ec_spi_dt_probe(ec_spi
, dev
);
634 spi_set_drvdata(spi
, ec_dev
);
636 ec_dev
->priv
= ec_spi
;
637 ec_dev
->irq
= spi
->irq
;
638 ec_dev
->cmd_xfer
= cros_ec_cmd_xfer_spi
;
639 ec_dev
->pkt_xfer
= cros_ec_pkt_xfer_spi
;
640 ec_dev
->phys_name
= dev_name(&ec_spi
->spi
->dev
);
641 ec_dev
->din_size
= EC_MSG_PREAMBLE_COUNT
+
642 sizeof(struct ec_host_response
) +
643 sizeof(struct ec_response_get_protocol_info
);
644 ec_dev
->dout_size
= sizeof(struct ec_host_request
);
647 err
= cros_ec_register(ec_dev
);
649 dev_err(dev
, "cannot register EC\n");
653 device_init_wakeup(&spi
->dev
, true);
658 static int cros_ec_spi_remove(struct spi_device
*spi
)
660 struct cros_ec_device
*ec_dev
;
662 ec_dev
= spi_get_drvdata(spi
);
663 cros_ec_remove(ec_dev
);
668 #ifdef CONFIG_PM_SLEEP
669 static int cros_ec_spi_suspend(struct device
*dev
)
671 struct cros_ec_device
*ec_dev
= dev_get_drvdata(dev
);
673 return cros_ec_suspend(ec_dev
);
676 static int cros_ec_spi_resume(struct device
*dev
)
678 struct cros_ec_device
*ec_dev
= dev_get_drvdata(dev
);
680 return cros_ec_resume(ec_dev
);
684 static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops
, cros_ec_spi_suspend
,
687 static const struct spi_device_id cros_ec_spi_id
[] = {
688 { "cros-ec-spi", 0 },
691 MODULE_DEVICE_TABLE(spi
, cros_ec_spi_id
);
693 static struct spi_driver cros_ec_driver_spi
= {
695 .name
= "cros-ec-spi",
696 .owner
= THIS_MODULE
,
697 .pm
= &cros_ec_spi_pm_ops
,
699 .probe
= cros_ec_spi_probe
,
700 .remove
= cros_ec_spi_remove
,
701 .id_table
= cros_ec_spi_id
,
704 module_spi_driver(cros_ec_driver_spi
);
706 MODULE_LICENSE("GPL v2");
707 MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)");