1
2
3
4
5
6#include <linux/delay.h>
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/of.h>
10#include <linux/platform_data/cros_ec_commands.h>
11#include <linux/platform_data/cros_ec_proto.h>
12#include <linux/platform_device.h>
13#include <linux/slab.h>
14#include <linux/spi/spi.h>
15#include <uapi/linux/sched/types.h>
16
17#include "cros_ec.h"
18
19
20#define EC_MSG_HEADER 0xec
21
22
23
24
25
26
27
28
29
30
31#define EC_MSG_PREAMBLE_COUNT 32
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52#define EC_MSG_DEADLINE_MS 200
53
54
55
56
57
58
59
60
61#define EC_SPI_RECOVERY_TIME_NS (200 * 1000)
62
63
64
65
66
67
68
69
70
71
72
73
74struct cros_ec_spi {
75 struct spi_device *spi;
76 s64 last_transfer_ns;
77 unsigned int start_of_msg_delay;
78 unsigned int end_of_msg_delay;
79 struct kthread_worker *high_pri_worker;
80};
81
82typedef int (*cros_ec_xfer_fn_t) (struct cros_ec_device *ec_dev,
83 struct cros_ec_command *ec_msg);
84
85
86
87
88
89
90
91
92
93
94
95struct cros_ec_xfer_work_params {
96 struct kthread_work work;
97 cros_ec_xfer_fn_t fn;
98 struct cros_ec_device *ec_dev;
99 struct cros_ec_command *ec_msg;
100 int ret;
101};
102
103static void debug_packet(struct device *dev, const char *name, u8 *ptr,
104 int len)
105{
106#ifdef DEBUG
107 int i;
108
109 dev_dbg(dev, "%s: ", name);
110 for (i = 0; i < len; i++)
111 pr_cont(" %02x", ptr[i]);
112
113 pr_cont("\n");
114#endif
115}
116
117static int terminate_request(struct cros_ec_device *ec_dev)
118{
119 struct cros_ec_spi *ec_spi = ec_dev->priv;
120 struct spi_message msg;
121 struct spi_transfer trans;
122 int ret;
123
124
125
126
127
128 spi_message_init(&msg);
129 memset(&trans, 0, sizeof(trans));
130 trans.delay_usecs = ec_spi->end_of_msg_delay;
131 spi_message_add_tail(&trans, &msg);
132
133 ret = spi_sync_locked(ec_spi->spi, &msg);
134
135
136 ec_spi->last_transfer_ns = ktime_get_ns();
137 if (ret < 0) {
138 dev_err(ec_dev->dev,
139 "cs-deassert spi transfer failed: %d\n",
140 ret);
141 }
142
143 return ret;
144}
145
146
147
148
149
150
151static int receive_n_bytes(struct cros_ec_device *ec_dev, u8 *buf, int n)
152{
153 struct cros_ec_spi *ec_spi = ec_dev->priv;
154 struct spi_transfer trans;
155 struct spi_message msg;
156 int ret;
157
158 BUG_ON(buf - ec_dev->din + n > ec_dev->din_size);
159
160 memset(&trans, 0, sizeof(trans));
161 trans.cs_change = 1;
162 trans.rx_buf = buf;
163 trans.len = n;
164
165 spi_message_init(&msg);
166 spi_message_add_tail(&trans, &msg);
167 ret = spi_sync_locked(ec_spi->spi, &msg);
168 if (ret < 0)
169 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
170
171 return ret;
172}
173
174
175
176
177
178
179
180
181
182
183
184
185
186static int cros_ec_spi_receive_packet(struct cros_ec_device *ec_dev,
187 int need_len)
188{
189 struct ec_host_response *response;
190 u8 *ptr, *end;
191 int ret;
192 unsigned long deadline;
193 int todo;
194
195 BUG_ON(ec_dev->din_size < EC_MSG_PREAMBLE_COUNT);
196
197
198 deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
199 while (true) {
200 unsigned long start_jiffies = jiffies;
201
202 ret = receive_n_bytes(ec_dev,
203 ec_dev->din,
204 EC_MSG_PREAMBLE_COUNT);
205 if (ret < 0)
206 return ret;
207
208 ptr = ec_dev->din;
209 for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) {
210 if (*ptr == EC_SPI_FRAME_START) {
211 dev_dbg(ec_dev->dev, "msg found at %zd\n",
212 ptr - ec_dev->din);
213 break;
214 }
215 }
216 if (ptr != end)
217 break;
218
219
220
221
222
223
224 if (time_after(start_jiffies, deadline)) {
225 dev_warn(ec_dev->dev, "EC failed to respond in time\n");
226 return -ETIMEDOUT;
227 }
228 }
229
230
231
232
233
234 todo = end - ++ptr;
235 BUG_ON(todo < 0 || todo > ec_dev->din_size);
236 todo = min(todo, need_len);
237 memmove(ec_dev->din, ptr, todo);
238 ptr = ec_dev->din + todo;
239 dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n",
240 need_len, todo);
241 need_len -= todo;
242
243
244 if (todo < sizeof(*response)) {
245 ret = receive_n_bytes(ec_dev, ptr, sizeof(*response) - todo);
246 if (ret < 0)
247 return -EBADMSG;
248 ptr += (sizeof(*response) - todo);
249 todo = sizeof(*response);
250 }
251
252 response = (struct ec_host_response *)ec_dev->din;
253
254
255 if (response->data_len > ec_dev->din_size)
256 return -EMSGSIZE;
257
258
259 while (need_len > 0) {
260
261
262
263
264
265
266 todo = min(need_len, 256);
267 dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n",
268 todo, need_len, ptr - ec_dev->din);
269
270 ret = receive_n_bytes(ec_dev, ptr, todo);
271 if (ret < 0)
272 return ret;
273
274 ptr += todo;
275 need_len -= todo;
276 }
277
278 dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
279
280 return 0;
281}
282
283
284
285
286
287
288
289
290
291
292
293
294
295static int cros_ec_spi_receive_response(struct cros_ec_device *ec_dev,
296 int need_len)
297{
298 u8 *ptr, *end;
299 int ret;
300 unsigned long deadline;
301 int todo;
302
303 BUG_ON(ec_dev->din_size < EC_MSG_PREAMBLE_COUNT);
304
305
306 deadline = jiffies + msecs_to_jiffies(EC_MSG_DEADLINE_MS);
307 while (true) {
308 unsigned long start_jiffies = jiffies;
309
310 ret = receive_n_bytes(ec_dev,
311 ec_dev->din,
312 EC_MSG_PREAMBLE_COUNT);
313 if (ret < 0)
314 return ret;
315
316 ptr = ec_dev->din;
317 for (end = ptr + EC_MSG_PREAMBLE_COUNT; ptr != end; ptr++) {
318 if (*ptr == EC_SPI_FRAME_START) {
319 dev_dbg(ec_dev->dev, "msg found at %zd\n",
320 ptr - ec_dev->din);
321 break;
322 }
323 }
324 if (ptr != end)
325 break;
326
327
328
329
330
331
332 if (time_after(start_jiffies, deadline)) {
333 dev_warn(ec_dev->dev, "EC failed to respond in time\n");
334 return -ETIMEDOUT;
335 }
336 }
337
338
339
340
341
342 todo = end - ++ptr;
343 BUG_ON(todo < 0 || todo > ec_dev->din_size);
344 todo = min(todo, need_len);
345 memmove(ec_dev->din, ptr, todo);
346 ptr = ec_dev->din + todo;
347 dev_dbg(ec_dev->dev, "need %d, got %d bytes from preamble\n",
348 need_len, todo);
349 need_len -= todo;
350
351
352 while (need_len > 0) {
353
354
355
356
357
358
359 todo = min(need_len, 256);
360 dev_dbg(ec_dev->dev, "loop, todo=%d, need_len=%d, ptr=%zd\n",
361 todo, need_len, ptr - ec_dev->din);
362
363 ret = receive_n_bytes(ec_dev, ptr, todo);
364 if (ret < 0)
365 return ret;
366
367 debug_packet(ec_dev->dev, "interim", ptr, todo);
368 ptr += todo;
369 need_len -= todo;
370 }
371
372 dev_dbg(ec_dev->dev, "loop done, ptr=%zd\n", ptr - ec_dev->din);
373
374 return 0;
375}
376
377
378
379
380
381
382
383static int do_cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev,
384 struct cros_ec_command *ec_msg)
385{
386 struct ec_host_response *response;
387 struct cros_ec_spi *ec_spi = ec_dev->priv;
388 struct spi_transfer trans, trans_delay;
389 struct spi_message msg;
390 int i, len;
391 u8 *ptr;
392 u8 *rx_buf;
393 u8 sum;
394 u8 rx_byte;
395 int ret = 0, final_ret;
396 unsigned long delay;
397
398 len = cros_ec_prepare_tx(ec_dev, ec_msg);
399 dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
400
401
402 delay = ktime_get_ns() - ec_spi->last_transfer_ns;
403 if (delay < EC_SPI_RECOVERY_TIME_NS)
404 ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
405
406 rx_buf = kzalloc(len, GFP_KERNEL);
407 if (!rx_buf)
408 return -ENOMEM;
409
410 spi_bus_lock(ec_spi->spi->master);
411
412
413
414
415
416 spi_message_init(&msg);
417 if (ec_spi->start_of_msg_delay) {
418 memset(&trans_delay, 0, sizeof(trans_delay));
419 trans_delay.delay_usecs = ec_spi->start_of_msg_delay;
420 spi_message_add_tail(&trans_delay, &msg);
421 }
422
423
424 memset(&trans, 0, sizeof(trans));
425 trans.tx_buf = ec_dev->dout;
426 trans.rx_buf = rx_buf;
427 trans.len = len;
428 trans.cs_change = 1;
429 spi_message_add_tail(&trans, &msg);
430 ret = spi_sync_locked(ec_spi->spi, &msg);
431
432
433 if (!ret) {
434
435 for (i = 0; i < len; i++) {
436 rx_byte = rx_buf[i];
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452 if (rx_byte == EC_SPI_PAST_END ||
453 rx_byte == EC_SPI_RX_BAD_DATA ||
454 rx_byte == EC_SPI_NOT_READY) {
455 ret = -EAGAIN;
456 break;
457 }
458 }
459 }
460
461 if (!ret)
462 ret = cros_ec_spi_receive_packet(ec_dev,
463 ec_msg->insize + sizeof(*response));
464 else if (ret != -EAGAIN)
465 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
466
467 final_ret = terminate_request(ec_dev);
468
469 spi_bus_unlock(ec_spi->spi->master);
470
471 if (!ret)
472 ret = final_ret;
473 if (ret < 0)
474 goto exit;
475
476 ptr = ec_dev->din;
477
478
479 response = (struct ec_host_response *)ptr;
480 ec_msg->result = response->result;
481
482 ret = cros_ec_check_result(ec_dev, ec_msg);
483 if (ret)
484 goto exit;
485
486 len = response->data_len;
487 sum = 0;
488 if (len > ec_msg->insize) {
489 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
490 len, ec_msg->insize);
491 ret = -EMSGSIZE;
492 goto exit;
493 }
494
495 for (i = 0; i < sizeof(*response); i++)
496 sum += ptr[i];
497
498
499 memcpy(ec_msg->data, ptr + sizeof(*response), len);
500 for (i = 0; i < len; i++)
501 sum += ec_msg->data[i];
502
503 if (sum) {
504 dev_err(ec_dev->dev,
505 "bad packet checksum, calculated %x\n",
506 sum);
507 ret = -EBADMSG;
508 goto exit;
509 }
510
511 ret = len;
512exit:
513 kfree(rx_buf);
514 if (ec_msg->command == EC_CMD_REBOOT_EC)
515 msleep(EC_REBOOT_DELAY_MS);
516
517 return ret;
518}
519
520
521
522
523
524
525
526static int do_cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
527 struct cros_ec_command *ec_msg)
528{
529 struct cros_ec_spi *ec_spi = ec_dev->priv;
530 struct spi_transfer trans;
531 struct spi_message msg;
532 int i, len;
533 u8 *ptr;
534 u8 *rx_buf;
535 u8 rx_byte;
536 int sum;
537 int ret = 0, final_ret;
538 unsigned long delay;
539
540 len = cros_ec_prepare_tx(ec_dev, ec_msg);
541 dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
542
543
544 delay = ktime_get_ns() - ec_spi->last_transfer_ns;
545 if (delay < EC_SPI_RECOVERY_TIME_NS)
546 ndelay(EC_SPI_RECOVERY_TIME_NS - delay);
547
548 rx_buf = kzalloc(len, GFP_KERNEL);
549 if (!rx_buf)
550 return -ENOMEM;
551
552 spi_bus_lock(ec_spi->spi->master);
553
554
555 debug_packet(ec_dev->dev, "out", ec_dev->dout, len);
556 memset(&trans, 0, sizeof(trans));
557 trans.tx_buf = ec_dev->dout;
558 trans.rx_buf = rx_buf;
559 trans.len = len;
560 trans.cs_change = 1;
561 spi_message_init(&msg);
562 spi_message_add_tail(&trans, &msg);
563 ret = spi_sync_locked(ec_spi->spi, &msg);
564
565
566 if (!ret) {
567
568 for (i = 0; i < len; i++) {
569 rx_byte = rx_buf[i];
570
571 if (rx_byte == EC_SPI_PAST_END ||
572 rx_byte == EC_SPI_RX_BAD_DATA ||
573 rx_byte == EC_SPI_NOT_READY) {
574 ret = -EAGAIN;
575 break;
576 }
577 }
578 }
579
580 if (!ret)
581 ret = cros_ec_spi_receive_response(ec_dev,
582 ec_msg->insize + EC_MSG_TX_PROTO_BYTES);
583 else if (ret != -EAGAIN)
584 dev_err(ec_dev->dev, "spi transfer failed: %d\n", ret);
585
586 final_ret = terminate_request(ec_dev);
587
588 spi_bus_unlock(ec_spi->spi->master);
589
590 if (!ret)
591 ret = final_ret;
592 if (ret < 0)
593 goto exit;
594
595 ptr = ec_dev->din;
596
597
598 ec_msg->result = ptr[0];
599 ret = cros_ec_check_result(ec_dev, ec_msg);
600 if (ret)
601 goto exit;
602
603 len = ptr[1];
604 sum = ptr[0] + ptr[1];
605 if (len > ec_msg->insize) {
606 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
607 len, ec_msg->insize);
608 ret = -ENOSPC;
609 goto exit;
610 }
611
612
613 for (i = 0; i < len; i++) {
614 sum += ptr[i + 2];
615 if (ec_msg->insize)
616 ec_msg->data[i] = ptr[i + 2];
617 }
618 sum &= 0xff;
619
620 debug_packet(ec_dev->dev, "in", ptr, len + 3);
621
622 if (sum != ptr[len + 2]) {
623 dev_err(ec_dev->dev,
624 "bad packet checksum, expected %02x, got %02x\n",
625 sum, ptr[len + 2]);
626 ret = -EBADMSG;
627 goto exit;
628 }
629
630 ret = len;
631exit:
632 kfree(rx_buf);
633 if (ec_msg->command == EC_CMD_REBOOT_EC)
634 msleep(EC_REBOOT_DELAY_MS);
635
636 return ret;
637}
638
639static void cros_ec_xfer_high_pri_work(struct kthread_work *work)
640{
641 struct cros_ec_xfer_work_params *params;
642
643 params = container_of(work, struct cros_ec_xfer_work_params, work);
644 params->ret = params->fn(params->ec_dev, params->ec_msg);
645}
646
647static int cros_ec_xfer_high_pri(struct cros_ec_device *ec_dev,
648 struct cros_ec_command *ec_msg,
649 cros_ec_xfer_fn_t fn)
650{
651 struct cros_ec_spi *ec_spi = ec_dev->priv;
652 struct cros_ec_xfer_work_params params = {
653 .work = KTHREAD_WORK_INIT(params.work,
654 cros_ec_xfer_high_pri_work),
655 .ec_dev = ec_dev,
656 .ec_msg = ec_msg,
657 .fn = fn,
658 };
659
660
661
662
663
664
665
666
667
668
669 kthread_queue_work(ec_spi->high_pri_worker, ¶ms.work);
670 kthread_flush_work(¶ms.work);
671
672 return params.ret;
673}
674
675static int cros_ec_pkt_xfer_spi(struct cros_ec_device *ec_dev,
676 struct cros_ec_command *ec_msg)
677{
678 return cros_ec_xfer_high_pri(ec_dev, ec_msg, do_cros_ec_pkt_xfer_spi);
679}
680
681static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
682 struct cros_ec_command *ec_msg)
683{
684 return cros_ec_xfer_high_pri(ec_dev, ec_msg, do_cros_ec_cmd_xfer_spi);
685}
686
687static void cros_ec_spi_dt_probe(struct cros_ec_spi *ec_spi, struct device *dev)
688{
689 struct device_node *np = dev->of_node;
690 u32 val;
691 int ret;
692
693 ret = of_property_read_u32(np, "google,cros-ec-spi-pre-delay", &val);
694 if (!ret)
695 ec_spi->start_of_msg_delay = val;
696
697 ret = of_property_read_u32(np, "google,cros-ec-spi-msg-delay", &val);
698 if (!ret)
699 ec_spi->end_of_msg_delay = val;
700}
701
702static void cros_ec_spi_high_pri_release(void *worker)
703{
704 kthread_destroy_worker(worker);
705}
706
707static int cros_ec_spi_devm_high_pri_alloc(struct device *dev,
708 struct cros_ec_spi *ec_spi)
709{
710 struct sched_param sched_priority = {
711 .sched_priority = MAX_RT_PRIO / 2,
712 };
713 int err;
714
715 ec_spi->high_pri_worker =
716 kthread_create_worker(0, "cros_ec_spi_high_pri");
717
718 if (IS_ERR(ec_spi->high_pri_worker)) {
719 err = PTR_ERR(ec_spi->high_pri_worker);
720 dev_err(dev, "Can't create cros_ec high pri worker: %d\n", err);
721 return err;
722 }
723
724 err = devm_add_action_or_reset(dev, cros_ec_spi_high_pri_release,
725 ec_spi->high_pri_worker);
726 if (err)
727 return err;
728
729 err = sched_setscheduler_nocheck(ec_spi->high_pri_worker->task,
730 SCHED_FIFO, &sched_priority);
731 if (err)
732 dev_err(dev, "Can't set cros_ec high pri priority: %d\n", err);
733 return err;
734}
735
736static int cros_ec_spi_probe(struct spi_device *spi)
737{
738 struct device *dev = &spi->dev;
739 struct cros_ec_device *ec_dev;
740 struct cros_ec_spi *ec_spi;
741 int err;
742
743 spi->bits_per_word = 8;
744 spi->mode = SPI_MODE_0;
745 spi->rt = true;
746 err = spi_setup(spi);
747 if (err < 0)
748 return err;
749
750 ec_spi = devm_kzalloc(dev, sizeof(*ec_spi), GFP_KERNEL);
751 if (ec_spi == NULL)
752 return -ENOMEM;
753 ec_spi->spi = spi;
754 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
755 if (!ec_dev)
756 return -ENOMEM;
757
758
759 cros_ec_spi_dt_probe(ec_spi, dev);
760
761 spi_set_drvdata(spi, ec_dev);
762 ec_dev->dev = dev;
763 ec_dev->priv = ec_spi;
764 ec_dev->irq = spi->irq;
765 ec_dev->cmd_xfer = cros_ec_cmd_xfer_spi;
766 ec_dev->pkt_xfer = cros_ec_pkt_xfer_spi;
767 ec_dev->phys_name = dev_name(&ec_spi->spi->dev);
768 ec_dev->din_size = EC_MSG_PREAMBLE_COUNT +
769 sizeof(struct ec_host_response) +
770 sizeof(struct ec_response_get_protocol_info);
771 ec_dev->dout_size = sizeof(struct ec_host_request);
772
773 ec_spi->last_transfer_ns = ktime_get_ns();
774
775 err = cros_ec_spi_devm_high_pri_alloc(dev, ec_spi);
776 if (err)
777 return err;
778
779 err = cros_ec_register(ec_dev);
780 if (err) {
781 dev_err(dev, "cannot register EC\n");
782 return err;
783 }
784
785 device_init_wakeup(&spi->dev, true);
786
787 return 0;
788}
789
790static int cros_ec_spi_remove(struct spi_device *spi)
791{
792 struct cros_ec_device *ec_dev = spi_get_drvdata(spi);
793
794 return cros_ec_unregister(ec_dev);
795}
796
797#ifdef CONFIG_PM_SLEEP
798static int cros_ec_spi_suspend(struct device *dev)
799{
800 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
801
802 return cros_ec_suspend(ec_dev);
803}
804
805static int cros_ec_spi_resume(struct device *dev)
806{
807 struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
808
809 return cros_ec_resume(ec_dev);
810}
811#endif
812
813static SIMPLE_DEV_PM_OPS(cros_ec_spi_pm_ops, cros_ec_spi_suspend,
814 cros_ec_spi_resume);
815
816static const struct of_device_id cros_ec_spi_of_match[] = {
817 { .compatible = "google,cros-ec-spi", },
818 { },
819};
820MODULE_DEVICE_TABLE(of, cros_ec_spi_of_match);
821
822static const struct spi_device_id cros_ec_spi_id[] = {
823 { "cros-ec-spi", 0 },
824 { }
825};
826MODULE_DEVICE_TABLE(spi, cros_ec_spi_id);
827
828static struct spi_driver cros_ec_driver_spi = {
829 .driver = {
830 .name = "cros-ec-spi",
831 .of_match_table = cros_ec_spi_of_match,
832 .pm = &cros_ec_spi_pm_ops,
833 },
834 .probe = cros_ec_spi_probe,
835 .remove = cros_ec_spi_remove,
836 .id_table = cros_ec_spi_id,
837};
838
839module_spi_driver(cros_ec_driver_spi);
840
841MODULE_LICENSE("GPL v2");
842MODULE_DESCRIPTION("SPI interface for ChromeOS Embedded Controller");
843