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 * @start_of_msg_delay: used to set the delay_usecs on the spi_transfer that
75 * is sent when we want to turn on CS at the start of a transaction.
76 * @end_of_msg_delay: used to set the delay_usecs on the spi_transfer that
77 * is sent when we want to turn off CS at the end of a transaction.
80 struct spi_device
*spi
;
82 unsigned int start_of_msg_delay
;
83 unsigned int end_of_msg_delay
;
86 static void debug_packet(struct device
*dev
, const char *name
, u8
*ptr
,
92 dev_dbg(dev
, "%s: ", name
);
93 for (i
= 0; i
< len
; i
++)
94 pr_cont(" %02x", ptr
[i
]);
100 static int terminate_request(struct cros_ec_device
*ec_dev
)
102 struct cros_ec_spi
*ec_spi
= ec_dev
->priv
;
103 struct spi_message msg
;
104 struct spi_transfer trans
;
108 * Turn off CS, possibly adding a delay to ensure the rising edge
109 * doesn't come too soon after the end of the data.
111 spi_message_init(&msg
);
112 memset(&trans
, 0, sizeof(trans
));
113 trans
.delay_usecs
= ec_spi
->end_of_msg_delay
;
114 spi_message_add_tail(&trans
, &msg
);
116 ret
= spi_sync_locked(ec_spi
->spi
, &msg
);
118 /* Reset end-of-response timer */
119 ec_spi
->last_transfer_ns
= ktime_get_ns();
122 "cs-deassert spi transfer failed: %d\n",
130 * receive_n_bytes - receive n bytes from the EC.
132 * Assumes buf is a pointer into the ec_dev->din buffer
134 static int receive_n_bytes(struct cros_ec_device
*ec_dev
, u8
*buf
, int n
)
136 struct cros_ec_spi
*ec_spi
= ec_dev
->priv
;
137 struct spi_transfer trans
;
138 struct spi_message msg
;
141 BUG_ON(buf
- ec_dev
->din
+ n
> ec_dev
->din_size
);
143 memset(&trans
, 0, sizeof(trans
));
148 spi_message_init(&msg
);
149 spi_message_add_tail(&trans
, &msg
);
150 ret
= spi_sync_locked(ec_spi
->spi
, &msg
);
152 dev_err(ec_dev
->dev
, "spi transfer failed: %d\n", ret
);
158 * cros_ec_spi_receive_packet - Receive a packet from the EC.
160 * This function has two phases: reading the preamble bytes (since if we read
161 * data from the EC before it is ready to send, we just get preamble) and
162 * reading the actual message.
164 * The received data is placed into ec_dev->din.
166 * @ec_dev: ChromeOS EC device
167 * @need_len: Number of message bytes we need to read
169 static int cros_ec_spi_receive_packet(struct cros_ec_device
*ec_dev
,
172 struct ec_host_response
*response
;
175 unsigned long deadline
;
178 BUG_ON(ec_dev
->din_size
< EC_MSG_PREAMBLE_COUNT
);
180 /* Receive data until we see the header byte */
181 deadline
= jiffies
+ msecs_to_jiffies(EC_MSG_DEADLINE_MS
);
183 unsigned long start_jiffies
= jiffies
;
185 ret
= receive_n_bytes(ec_dev
,
187 EC_MSG_PREAMBLE_COUNT
);
192 for (end
= ptr
+ EC_MSG_PREAMBLE_COUNT
; ptr
!= end
; ptr
++) {
193 if (*ptr
== EC_SPI_FRAME_START
) {
194 dev_dbg(ec_dev
->dev
, "msg found at %zd\n",
203 * Use the time at the start of the loop as a timeout. This
204 * gives us one last shot at getting the transfer and is useful
205 * in case we got context switched out for a while.
207 if (time_after(start_jiffies
, deadline
)) {
208 dev_warn(ec_dev
->dev
, "EC failed to respond in time\n");
214 * ptr now points to the header byte. Copy any valid data to the
215 * start of our buffer
218 BUG_ON(todo
< 0 || todo
> ec_dev
->din_size
);
219 todo
= min(todo
, need_len
);
220 memmove(ec_dev
->din
, ptr
, todo
);
221 ptr
= ec_dev
->din
+ todo
;
222 dev_dbg(ec_dev
->dev
, "need %d, got %d bytes from preamble\n",
226 /* If the entire response struct wasn't read, get the rest of it. */
227 if (todo
< sizeof(*response
)) {
228 ret
= receive_n_bytes(ec_dev
, ptr
, sizeof(*response
) - todo
);
231 ptr
+= (sizeof(*response
) - todo
);
232 todo
= sizeof(*response
);
235 response
= (struct ec_host_response
*)ec_dev
->din
;
237 /* Abort if data_len is too large. */
238 if (response
->data_len
> ec_dev
->din_size
)
241 /* Receive data until we have it all */
242 while (need_len
> 0) {
244 * We can't support transfers larger than the SPI FIFO size
245 * unless we have DMA. We don't have DMA on the ISP SPI ports
246 * for Exynos. We need a way of asking SPI driver for
247 * maximum-supported transfer size.
249 todo
= min(need_len
, 256);
250 dev_dbg(ec_dev
->dev
, "loop, todo=%d, need_len=%d, ptr=%zd\n",
251 todo
, need_len
, ptr
- ec_dev
->din
);
253 ret
= receive_n_bytes(ec_dev
, ptr
, todo
);
261 dev_dbg(ec_dev
->dev
, "loop done, ptr=%zd\n", ptr
- ec_dev
->din
);
267 * cros_ec_spi_receive_response - Receive a response from the EC.
269 * This function has two phases: reading the preamble bytes (since if we read
270 * data from the EC before it is ready to send, we just get preamble) and
271 * reading the actual message.
273 * The received data is placed into ec_dev->din.
275 * @ec_dev: ChromeOS EC device
276 * @need_len: Number of message bytes we need to read
278 static int cros_ec_spi_receive_response(struct cros_ec_device
*ec_dev
,
283 unsigned long deadline
;
286 BUG_ON(ec_dev
->din_size
< EC_MSG_PREAMBLE_COUNT
);
288 /* Receive data until we see the header byte */
289 deadline
= jiffies
+ msecs_to_jiffies(EC_MSG_DEADLINE_MS
);
291 unsigned long start_jiffies
= jiffies
;
293 ret
= receive_n_bytes(ec_dev
,
295 EC_MSG_PREAMBLE_COUNT
);
300 for (end
= ptr
+ EC_MSG_PREAMBLE_COUNT
; ptr
!= end
; ptr
++) {
301 if (*ptr
== EC_SPI_FRAME_START
) {
302 dev_dbg(ec_dev
->dev
, "msg found at %zd\n",
311 * Use the time at the start of the loop as a timeout. This
312 * gives us one last shot at getting the transfer and is useful
313 * in case we got context switched out for a while.
315 if (time_after(start_jiffies
, deadline
)) {
316 dev_warn(ec_dev
->dev
, "EC failed to respond in time\n");
322 * ptr now points to the header byte. Copy any valid data to the
323 * start of our buffer
326 BUG_ON(todo
< 0 || todo
> ec_dev
->din_size
);
327 todo
= min(todo
, need_len
);
328 memmove(ec_dev
->din
, ptr
, todo
);
329 ptr
= ec_dev
->din
+ todo
;
330 dev_dbg(ec_dev
->dev
, "need %d, got %d bytes from preamble\n",
334 /* Receive data until we have it all */
335 while (need_len
> 0) {
337 * We can't support transfers larger than the SPI FIFO size
338 * unless we have DMA. We don't have DMA on the ISP SPI ports
339 * for Exynos. We need a way of asking SPI driver for
340 * maximum-supported transfer size.
342 todo
= min(need_len
, 256);
343 dev_dbg(ec_dev
->dev
, "loop, todo=%d, need_len=%d, ptr=%zd\n",
344 todo
, need_len
, ptr
- ec_dev
->din
);
346 ret
= receive_n_bytes(ec_dev
, ptr
, todo
);
350 debug_packet(ec_dev
->dev
, "interim", ptr
, todo
);
355 dev_dbg(ec_dev
->dev
, "loop done, ptr=%zd\n", ptr
- ec_dev
->din
);
361 * cros_ec_pkt_xfer_spi - Transfer a packet over SPI and receive the reply
363 * @ec_dev: ChromeOS EC device
364 * @ec_msg: Message to transfer
366 static int cros_ec_pkt_xfer_spi(struct cros_ec_device
*ec_dev
,
367 struct cros_ec_command
*ec_msg
)
369 struct ec_host_request
*request
;
370 struct ec_host_response
*response
;
371 struct cros_ec_spi
*ec_spi
= ec_dev
->priv
;
372 struct spi_transfer trans
, trans_delay
;
373 struct spi_message msg
;
378 int ret
= 0, final_ret
;
380 len
= cros_ec_prepare_tx(ec_dev
, ec_msg
);
381 request
= (struct ec_host_request
*)ec_dev
->dout
;
382 dev_dbg(ec_dev
->dev
, "prepared, len=%d\n", len
);
384 /* If it's too soon to do another transaction, wait */
385 if (ec_spi
->last_transfer_ns
) {
386 unsigned long delay
; /* The delay completed so far */
388 delay
= ktime_get_ns() - ec_spi
->last_transfer_ns
;
389 if (delay
< EC_SPI_RECOVERY_TIME_NS
)
390 ndelay(EC_SPI_RECOVERY_TIME_NS
- delay
);
393 rx_buf
= kzalloc(len
, GFP_KERNEL
);
397 spi_bus_lock(ec_spi
->spi
->master
);
400 * Leave a gap between CS assertion and clocking of data to allow the
403 spi_message_init(&msg
);
404 if (ec_spi
->start_of_msg_delay
) {
405 memset(&trans_delay
, 0, sizeof(trans_delay
));
406 trans_delay
.delay_usecs
= ec_spi
->start_of_msg_delay
;
407 spi_message_add_tail(&trans_delay
, &msg
);
410 /* Transmit phase - send our message */
411 memset(&trans
, 0, sizeof(trans
));
412 trans
.tx_buf
= ec_dev
->dout
;
413 trans
.rx_buf
= rx_buf
;
416 spi_message_add_tail(&trans
, &msg
);
417 ret
= spi_sync_locked(ec_spi
->spi
, &msg
);
419 /* Get the response */
421 /* Verify that EC can process command */
422 for (i
= 0; i
< len
; i
++) {
424 case EC_SPI_PAST_END
:
425 case EC_SPI_RX_BAD_DATA
:
426 case EC_SPI_NOT_READY
:
428 ec_msg
->result
= EC_RES_IN_PROGRESS
;
436 ret
= cros_ec_spi_receive_packet(ec_dev
,
437 ec_msg
->insize
+ sizeof(*response
));
439 dev_err(ec_dev
->dev
, "spi transfer failed: %d\n", ret
);
442 final_ret
= terminate_request(ec_dev
);
444 spi_bus_unlock(ec_spi
->spi
->master
);
453 /* check response error code */
454 response
= (struct ec_host_response
*)ptr
;
455 ec_msg
->result
= response
->result
;
457 ret
= cros_ec_check_result(ec_dev
, ec_msg
);
461 len
= response
->data_len
;
463 if (len
> ec_msg
->insize
) {
464 dev_err(ec_dev
->dev
, "packet too long (%d bytes, expected %d)",
465 len
, ec_msg
->insize
);
470 for (i
= 0; i
< sizeof(*response
); i
++)
473 /* copy response packet payload and compute checksum */
474 memcpy(ec_msg
->data
, ptr
+ sizeof(*response
), len
);
475 for (i
= 0; i
< len
; i
++)
476 sum
+= ec_msg
->data
[i
];
480 "bad packet checksum, calculated %x\n",
489 if (ec_msg
->command
== EC_CMD_REBOOT_EC
)
490 msleep(EC_REBOOT_DELAY_MS
);
496 * cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply
498 * @ec_dev: ChromeOS EC device
499 * @ec_msg: Message to transfer
501 static int cros_ec_cmd_xfer_spi(struct cros_ec_device
*ec_dev
,
502 struct cros_ec_command
*ec_msg
)
504 struct cros_ec_spi
*ec_spi
= ec_dev
->priv
;
505 struct spi_transfer trans
;
506 struct spi_message msg
;
511 int ret
= 0, final_ret
;
513 len
= cros_ec_prepare_tx(ec_dev
, ec_msg
);
514 dev_dbg(ec_dev
->dev
, "prepared, len=%d\n", len
);
516 /* If it's too soon to do another transaction, wait */
517 if (ec_spi
->last_transfer_ns
) {
518 unsigned long delay
; /* The delay completed so far */
520 delay
= ktime_get_ns() - ec_spi
->last_transfer_ns
;
521 if (delay
< EC_SPI_RECOVERY_TIME_NS
)
522 ndelay(EC_SPI_RECOVERY_TIME_NS
- delay
);
525 rx_buf
= kzalloc(len
, GFP_KERNEL
);
529 spi_bus_lock(ec_spi
->spi
->master
);
531 /* Transmit phase - send our message */
532 debug_packet(ec_dev
->dev
, "out", ec_dev
->dout
, len
);
533 memset(&trans
, 0, sizeof(trans
));
534 trans
.tx_buf
= ec_dev
->dout
;
535 trans
.rx_buf
= rx_buf
;
538 spi_message_init(&msg
);
539 spi_message_add_tail(&trans
, &msg
);
540 ret
= spi_sync_locked(ec_spi
->spi
, &msg
);
542 /* Get the response */
544 /* Verify that EC can process command */
545 for (i
= 0; i
< len
; i
++) {
547 case EC_SPI_PAST_END
:
548 case EC_SPI_RX_BAD_DATA
:
549 case EC_SPI_NOT_READY
:
551 ec_msg
->result
= EC_RES_IN_PROGRESS
;
559 ret
= cros_ec_spi_receive_response(ec_dev
,
560 ec_msg
->insize
+ EC_MSG_TX_PROTO_BYTES
);
562 dev_err(ec_dev
->dev
, "spi transfer failed: %d\n", ret
);
565 final_ret
= terminate_request(ec_dev
);
567 spi_bus_unlock(ec_spi
->spi
->master
);
576 /* check response error code */
577 ec_msg
->result
= ptr
[0];
578 ret
= cros_ec_check_result(ec_dev
, ec_msg
);
583 sum
= ptr
[0] + ptr
[1];
584 if (len
> ec_msg
->insize
) {
585 dev_err(ec_dev
->dev
, "packet too long (%d bytes, expected %d)",
586 len
, ec_msg
->insize
);
591 /* copy response packet payload and compute checksum */
592 for (i
= 0; i
< len
; i
++) {
595 ec_msg
->data
[i
] = ptr
[i
+ 2];
599 debug_packet(ec_dev
->dev
, "in", ptr
, len
+ 3);
601 if (sum
!= ptr
[len
+ 2]) {
603 "bad packet checksum, expected %02x, got %02x\n",
612 if (ec_msg
->command
== EC_CMD_REBOOT_EC
)
613 msleep(EC_REBOOT_DELAY_MS
);
618 static void cros_ec_spi_dt_probe(struct cros_ec_spi
*ec_spi
, struct device
*dev
)
620 struct device_node
*np
= dev
->of_node
;
624 ret
= of_property_read_u32(np
, "google,cros-ec-spi-pre-delay", &val
);
626 ec_spi
->start_of_msg_delay
= val
;
628 ret
= of_property_read_u32(np
, "google,cros-ec-spi-msg-delay", &val
);
630 ec_spi
->end_of_msg_delay
= val
;
633 static int cros_ec_spi_probe(struct spi_device
*spi
)
635 struct device
*dev
= &spi
->dev
;
636 struct cros_ec_device
*ec_dev
;
637 struct cros_ec_spi
*ec_spi
;
640 spi
->bits_per_word
= 8;
641 spi
->mode
= SPI_MODE_0
;
642 err
= spi_setup(spi
);
646 ec_spi
= devm_kzalloc(dev
, sizeof(*ec_spi
), GFP_KERNEL
);
650 ec_dev
= devm_kzalloc(dev
, sizeof(*ec_dev
), GFP_KERNEL
);
654 /* Check for any DT properties */
655 cros_ec_spi_dt_probe(ec_spi
, dev
);
657 spi_set_drvdata(spi
, ec_dev
);
659 ec_dev
->priv
= ec_spi
;
660 ec_dev
->irq
= spi
->irq
;
661 ec_dev
->cmd_xfer
= cros_ec_cmd_xfer_spi
;
662 ec_dev
->pkt_xfer
= cros_ec_pkt_xfer_spi
;
663 ec_dev
->phys_name
= dev_name(&ec_spi
->spi
->dev
);
664 ec_dev
->din_size
= EC_MSG_PREAMBLE_COUNT
+
665 sizeof(struct ec_host_response
) +
666 sizeof(struct ec_response_get_protocol_info
);
667 ec_dev
->dout_size
= sizeof(struct ec_host_request
);
670 err
= cros_ec_register(ec_dev
);
672 dev_err(dev
, "cannot register EC\n");
676 device_init_wakeup(&spi
->dev
, true);
681 static int cros_ec_spi_remove(struct spi_device
*spi
)
683 struct cros_ec_device
*ec_dev
;
685 ec_dev
= spi_get_drvdata(spi
);
686 cros_ec_remove(ec_dev
);
691 #ifdef CONFIG_PM_SLEEP
692 static int cros_ec_spi_suspend(struct device
*dev
)
694 struct cros_ec_device
*ec_dev
= dev_get_drvdata(dev
);
696 return cros_ec_suspend(ec_dev
);
699 static int cros_ec_spi_resume(struct device
*dev
)
701 struct cros_ec_device
*ec_dev
= dev_get_drvdata(dev
);
703 return cros_ec_resume(ec_dev
);
707 static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops
, cros_ec_spi_suspend
,
710 static const struct of_device_id cros_ec_spi_of_match
[] = {
711 { .compatible
= "google,cros-ec-spi", },
714 MODULE_DEVICE_TABLE(of
, cros_ec_spi_of_match
);
716 static const struct spi_device_id cros_ec_spi_id
[] = {
717 { "cros-ec-spi", 0 },
720 MODULE_DEVICE_TABLE(spi
, cros_ec_spi_id
);
722 static struct spi_driver cros_ec_driver_spi
= {
724 .name
= "cros-ec-spi",
725 .of_match_table
= of_match_ptr(cros_ec_spi_of_match
),
726 .pm
= &cros_ec_spi_pm_ops
,
728 .probe
= cros_ec_spi_probe
,
729 .remove
= cros_ec_spi_remove
,
730 .id_table
= cros_ec_spi_id
,
733 module_spi_driver(cros_ec_driver_spi
);
735 MODULE_LICENSE("GPL v2");
736 MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)");