mfd: cros_ec: Support multiple EC in a system
[deliverable/linux.git] / drivers / mfd / cros_ec_spi.c
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
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>
21 #include <linux/of.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/spi/spi.h>
25
26
27 /* The header byte, which follows the preamble */
28 #define EC_MSG_HEADER 0xec
29
30 /*
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).
38 */
39 #define EC_MSG_PREAMBLE_COUNT 32
40
41 /*
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
47 *
48 * We'll wait 4 times that to handle clock stretching and other
49 * paranoia.
50 *
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.
56 */
57 #define EC_MSG_DEADLINE_MS 100
58
59 /*
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
64 * safe.
65 */
66 #define EC_SPI_RECOVERY_TIME_NS (200 * 1000)
67
68 /**
69 * struct cros_ec_spi - information about a SPI-connected EC
70 *
71 * @spi: SPI device we are connected to
72 * @last_transfer_ns: time that we last finished a transfer, or 0 if there
73 * if no record
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.
76 */
77 struct cros_ec_spi {
78 struct spi_device *spi;
79 s64 last_transfer_ns;
80 unsigned int end_of_msg_delay;
81 };
82
83 static void debug_packet(struct device *dev, const char *name, u8 *ptr,
84 int len)
85 {
86 #ifdef DEBUG
87 int i;
88
89 dev_dbg(dev, "%s: ", name);
90 for (i = 0; i < len; i++)
91 pr_cont(" %02x", ptr[i]);
92
93 pr_cont("\n");
94 #endif
95 }
96
97 static int terminate_request(struct cros_ec_device *ec_dev)
98 {
99 struct cros_ec_spi *ec_spi = ec_dev->priv;
100 struct spi_message msg;
101 struct spi_transfer trans;
102 int ret;
103
104 /*
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.
107 */
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);
112
113 ret = spi_sync(ec_spi->spi, &msg);
114
115 /* Reset end-of-response timer */
116 ec_spi->last_transfer_ns = ktime_get_ns();
117 if (ret < 0) {
118 dev_err(ec_dev->dev,
119 "cs-deassert spi transfer failed: %d\n",
120 ret);
121 }
122
123 return ret;
124 }
125
126 /**
127 * receive_n_bytes - receive n bytes from the EC.
128 *
129 * Assumes buf is a pointer into the ec_dev->din buffer
130 */
131 static int receive_n_bytes(struct cros_ec_device *ec_dev, u8 *buf, int n)
132 {
133 struct cros_ec_spi *ec_spi = ec_dev->priv;
134 struct spi_transfer trans;
135 struct spi_message msg;
136 int ret;
137
138 BUG_ON(buf - ec_dev->din + n > ec_dev->din_size);
139
140 memset(&trans, 0, sizeof(trans));
141 trans.cs_change = 1;
142 trans.rx_buf = buf;
143 trans.len = n;
144
145 spi_message_init(&msg);
146 spi_message_add_tail(&trans, &msg);
147 ret = spi_sync(ec_spi->spi, &msg);
148 if (ret < 0)
149 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
150
151 return ret;
152 }
153
154 /**
155 * cros_ec_spi_receive_packet - Receive a packet from the EC.
156 *
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.
160 *
161 * The received data is placed into ec_dev->din.
162 *
163 * @ec_dev: ChromeOS EC device
164 * @need_len: Number of message bytes we need to read
165 */
166 static int cros_ec_spi_receive_packet(struct cros_ec_device *ec_dev,
167 int need_len)
168 {
169 struct ec_host_response *response;
170 u8 *ptr, *end;
171 int ret;
172 unsigned long deadline;
173 int todo;
174
175 BUG_ON(EC_MSG_PREAMBLE_COUNT > ec_dev->din_size);
176
177 /* Receive data until we see the header byte */
178 deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
179 while (true) {
180 unsigned long start_jiffies = jiffies;
181
182 ret = receive_n_bytes(ec_dev,
183 ec_dev->din,
184 EC_MSG_PREAMBLE_COUNT);
185 if (ret < 0)
186 return ret;
187
188 ptr = ec_dev->din;
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",
192 ptr - ec_dev->din);
193 break;
194 }
195 }
196 if (ptr != end)
197 break;
198
199 /*
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.
203 */
204 if (time_after(start_jiffies, deadline)) {
205 dev_warn(ec_dev->dev, "EC failed to respond in time\n");
206 return -ETIMEDOUT;
207 }
208 }
209
210 /*
211 * ptr now points to the header byte. Copy any valid data to the
212 * start of our buffer
213 */
214 todo = end - ++ptr;
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",
220 need_len, todo);
221 need_len -= todo;
222
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);
226 if (ret < 0)
227 return -EBADMSG;
228 ptr += (sizeof(*response) - todo);
229 todo = sizeof(*response);
230 }
231
232 response = (struct ec_host_response *)ec_dev->din;
233
234 /* Abort if data_len is too large. */
235 if (response->data_len > ec_dev->din_size)
236 return -EMSGSIZE;
237
238 /* Receive data until we have it all */
239 while (need_len > 0) {
240 /*
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.
245 */
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);
249
250 ret = receive_n_bytes(ec_dev, ptr, todo);
251 if (ret < 0)
252 return ret;
253
254 ptr += todo;
255 need_len -= todo;
256 }
257
258 dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
259
260 return 0;
261 }
262
263 /**
264 * cros_ec_spi_receive_response - Receive a response from the EC.
265 *
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.
269 *
270 * The received data is placed into ec_dev->din.
271 *
272 * @ec_dev: ChromeOS EC device
273 * @need_len: Number of message bytes we need to read
274 */
275 static int cros_ec_spi_receive_response(struct cros_ec_device *ec_dev,
276 int need_len)
277 {
278 u8 *ptr, *end;
279 int ret;
280 unsigned long deadline;
281 int todo;
282
283 BUG_ON(EC_MSG_PREAMBLE_COUNT > ec_dev->din_size);
284
285 /* Receive data until we see the header byte */
286 deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
287 while (true) {
288 unsigned long start_jiffies = jiffies;
289
290 ret = receive_n_bytes(ec_dev,
291 ec_dev->din,
292 EC_MSG_PREAMBLE_COUNT);
293 if (ret < 0)
294 return ret;
295
296 ptr = ec_dev->din;
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",
300 ptr - ec_dev->din);
301 break;
302 }
303 }
304 if (ptr != end)
305 break;
306
307 /*
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.
311 */
312 if (time_after(start_jiffies, deadline)) {
313 dev_warn(ec_dev->dev, "EC failed to respond in time\n");
314 return -ETIMEDOUT;
315 }
316 }
317
318 /*
319 * ptr now points to the header byte. Copy any valid data to the
320 * start of our buffer
321 */
322 todo = end - ++ptr;
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",
328 need_len, todo);
329 need_len -= todo;
330
331 /* Receive data until we have it all */
332 while (need_len > 0) {
333 /*
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.
338 */
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);
342
343 ret = receive_n_bytes(ec_dev, ptr, todo);
344 if (ret < 0)
345 return ret;
346
347 debug_packet(ec_dev->dev, "interim", ptr, todo);
348 ptr += todo;
349 need_len -= todo;
350 }
351
352 dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
353
354 return 0;
355 }
356
357 /**
358 * cros_ec_pkt_xfer_spi - Transfer a packet over SPI and receive the reply
359 *
360 * @ec_dev: ChromeOS EC device
361 * @ec_msg: Message to transfer
362 */
363 static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev,
364 struct cros_ec_command *ec_msg)
365 {
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;
371 int i, len;
372 u8 *ptr;
373 u8 *rx_buf;
374 u8 sum;
375 int ret = 0, final_ret;
376
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);
380
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 */
384
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);
388 }
389
390 rx_buf = kzalloc(len, GFP_KERNEL);
391 if (!rx_buf) {
392 ret = -ENOMEM;
393 goto exit;
394 }
395
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;
400 trans.len = len;
401 trans.cs_change = 1;
402 spi_message_init(&msg);
403 spi_message_add_tail(&trans, &msg);
404 ret = spi_sync(ec_spi->spi, &msg);
405
406 /* Get the response */
407 if (!ret) {
408 /* Verify that EC can process command */
409 for (i = 0; i < len; i++) {
410 switch (rx_buf[i]) {
411 case EC_SPI_PAST_END:
412 case EC_SPI_RX_BAD_DATA:
413 case EC_SPI_NOT_READY:
414 ret = -EAGAIN;
415 ec_msg->result = EC_RES_IN_PROGRESS;
416 default:
417 break;
418 }
419 if (ret)
420 break;
421 }
422 if (!ret)
423 ret = cros_ec_spi_receive_packet(ec_dev,
424 ec_msg->insize + sizeof(*response));
425 } else {
426 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
427 }
428
429 final_ret = terminate_request(ec_dev);
430 if (!ret)
431 ret = final_ret;
432 if (ret < 0)
433 goto exit;
434
435 ptr = ec_dev->din;
436
437 /* check response error code */
438 response = (struct ec_host_response *)ptr;
439 ec_msg->result = response->result;
440
441 ret = cros_ec_check_result(ec_dev, ec_msg);
442 if (ret)
443 goto exit;
444
445 len = response->data_len;
446 sum = 0;
447 if (len > ec_msg->insize) {
448 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
449 len, ec_msg->insize);
450 ret = -EMSGSIZE;
451 goto exit;
452 }
453
454 for (i = 0; i < sizeof(*response); i++)
455 sum += ptr[i];
456
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];
461
462 if (sum) {
463 dev_err(ec_dev->dev,
464 "bad packet checksum, calculated %x\n",
465 sum);
466 ret = -EBADMSG;
467 goto exit;
468 }
469
470 ret = len;
471 exit:
472 kfree(rx_buf);
473 if (ec_msg->command == EC_CMD_REBOOT_EC)
474 msleep(EC_REBOOT_DELAY_MS);
475
476 return ret;
477 }
478
479 /**
480 * cros_ec_cmd_xfer_spi - Transfer a message over SPI and receive the reply
481 *
482 * @ec_dev: ChromeOS EC device
483 * @ec_msg: Message to transfer
484 */
485 static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
486 struct cros_ec_command *ec_msg)
487 {
488 struct cros_ec_spi *ec_spi = ec_dev->priv;
489 struct spi_transfer trans;
490 struct spi_message msg;
491 int i, len;
492 u8 *ptr;
493 u8 *rx_buf;
494 int sum;
495 int ret = 0, final_ret;
496
497 len = cros_ec_prepare_tx(ec_dev, ec_msg);
498 dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
499
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 */
503
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);
507 }
508
509 rx_buf = kzalloc(len, GFP_KERNEL);
510 if (!rx_buf) {
511 ret = -ENOMEM;
512 goto exit;
513 }
514
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;
520 trans.len = len;
521 trans.cs_change = 1;
522 spi_message_init(&msg);
523 spi_message_add_tail(&trans, &msg);
524 ret = spi_sync(ec_spi->spi, &msg);
525
526 /* Get the response */
527 if (!ret) {
528 /* Verify that EC can process command */
529 for (i = 0; i < len; i++) {
530 switch (rx_buf[i]) {
531 case EC_SPI_PAST_END:
532 case EC_SPI_RX_BAD_DATA:
533 case EC_SPI_NOT_READY:
534 ret = -EAGAIN;
535 ec_msg->result = EC_RES_IN_PROGRESS;
536 default:
537 break;
538 }
539 if (ret)
540 break;
541 }
542 if (!ret)
543 ret = cros_ec_spi_receive_response(ec_dev,
544 ec_msg->insize + EC_MSG_TX_PROTO_BYTES);
545 } else {
546 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
547 }
548
549 final_ret = terminate_request(ec_dev);
550 if (!ret)
551 ret = final_ret;
552 if (ret < 0)
553 goto exit;
554
555 ptr = ec_dev->din;
556
557 /* check response error code */
558 ec_msg->result = ptr[0];
559 ret = cros_ec_check_result(ec_dev, ec_msg);
560 if (ret)
561 goto exit;
562
563 len = ptr[1];
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);
568 ret = -ENOSPC;
569 goto exit;
570 }
571
572 /* copy response packet payload and compute checksum */
573 for (i = 0; i < len; i++) {
574 sum += ptr[i + 2];
575 if (ec_msg->insize)
576 ec_msg->data[i] = ptr[i + 2];
577 }
578 sum &= 0xff;
579
580 debug_packet(ec_dev->dev, "in", ptr, len + 3);
581
582 if (sum != ptr[len + 2]) {
583 dev_err(ec_dev->dev,
584 "bad packet checksum, expected %02x, got %02x\n",
585 sum, ptr[len + 2]);
586 ret = -EBADMSG;
587 goto exit;
588 }
589
590 ret = len;
591 exit:
592 kfree(rx_buf);
593 if (ec_msg->command == EC_CMD_REBOOT_EC)
594 msleep(EC_REBOOT_DELAY_MS);
595
596 return ret;
597 }
598
599 static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
600 {
601 struct device_node *np = dev->of_node;
602 u32 val;
603 int ret;
604
605 ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val);
606 if (!ret)
607 ec_spi->end_of_msg_delay = val;
608 }
609
610 static int cros_ec_spi_probe(struct spi_device *spi)
611 {
612 struct device *dev = &spi->dev;
613 struct cros_ec_device *ec_dev;
614 struct cros_ec_spi *ec_spi;
615 int err;
616
617 spi->bits_per_word = 8;
618 spi->mode = SPI_MODE_0;
619 err = spi_setup(spi);
620 if (err < 0)
621 return err;
622
623 ec_spi = devm_kzalloc(dev, sizeof(*ec_spi), GFP_KERNEL);
624 if (ec_spi == NULL)
625 return -ENOMEM;
626 ec_spi->spi = spi;
627 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
628 if (!ec_dev)
629 return -ENOMEM;
630
631 /* Check for any DT properties */
632 cros_ec_spi_dt_probe(ec_spi, dev);
633
634 spi_set_drvdata(spi, ec_dev);
635 ec_dev->dev = 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);
645
646
647 err = cros_ec_register(ec_dev);
648 if (err) {
649 dev_err(dev, "cannot register EC\n");
650 return err;
651 }
652
653 device_init_wakeup(&spi->dev, true);
654
655 return 0;
656 }
657
658 static int cros_ec_spi_remove(struct spi_device *spi)
659 {
660 struct cros_ec_device *ec_dev;
661
662 ec_dev = spi_get_drvdata(spi);
663 cros_ec_remove(ec_dev);
664
665 return 0;
666 }
667
668 #ifdef CONFIG_PM_SLEEP
669 static int cros_ec_spi_suspend(struct device *dev)
670 {
671 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
672
673 return cros_ec_suspend(ec_dev);
674 }
675
676 static int cros_ec_spi_resume(struct device *dev)
677 {
678 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
679
680 return cros_ec_resume(ec_dev);
681 }
682 #endif
683
684 static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops, cros_ec_spi_suspend,
685 cros_ec_spi_resume);
686
687 static const struct spi_device_id cros_ec_spi_id[] = {
688 { "cros-ec-spi", 0 },
689 { }
690 };
691 MODULE_DEVICE_TABLE(spi, cros_ec_spi_id);
692
693 static struct spi_driver cros_ec_driver_spi = {
694 .driver = {
695 .name = "cros-ec-spi",
696 .owner = THIS_MODULE,
697 .pm = &cros_ec_spi_pm_ops,
698 },
699 .probe = cros_ec_spi_probe,
700 .remove = cros_ec_spi_remove,
701 .id_table = cros_ec_spi_id,
702 };
703
704 module_spi_driver(cros_ec_driver_spi);
705
706 MODULE_LICENSE("GPL v2");
707 MODULE_DESCRIPTION("ChromeOS EC multi function device (SPI)");
This page took 0.060523 seconds and 5 git commands to generate.