1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <common.h>
18#include <command.h>
19#include <dm.h>
20#include <i2c.h>
21#include <cros_ec.h>
22#include <fdtdec.h>
23#include <malloc.h>
24#include <spi.h>
25#include <linux/errno.h>
26#include <asm/io.h>
27#include <asm-generic/gpio.h>
28#include <dm/device-internal.h>
29#include <dm/of_extra.h>
30#include <dm/uclass-internal.h>
31
32#ifdef DEBUG_TRACE
33#define debug_trace(fmt, b...) debug(fmt, #b)
34#else
35#define debug_trace(fmt, b...)
36#endif
37
38enum {
39
40 CROS_EC_CMD_TIMEOUT_MS = 5000,
41
42 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
43};
44
45DECLARE_GLOBAL_DATA_PTR;
46
47void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
48{
49#ifdef DEBUG
50 int i;
51
52 printf("%s: ", name);
53 if (cmd != -1)
54 printf("cmd=%#x: ", cmd);
55 for (i = 0; i < len; i++)
56 printf("%02x ", data[i]);
57 printf("\n");
58#endif
59}
60
61
62
63
64
65
66
67
68int cros_ec_calc_checksum(const uint8_t *data, int size)
69{
70 int csum, i;
71
72 for (i = csum = 0; i < size; i++)
73 csum += data[i];
74 return csum & 0xff;
75}
76
77
78
79
80
81
82
83
84
85
86
87
88
89static int create_proto3_request(struct cros_ec_dev *dev,
90 int cmd, int cmd_version,
91 const void *dout, int dout_len)
92{
93 struct ec_host_request *rq = (struct ec_host_request *)dev->dout;
94 int out_bytes = dout_len + sizeof(*rq);
95
96
97 if (out_bytes > (int)sizeof(dev->dout)) {
98 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
99 return -EC_RES_REQUEST_TRUNCATED;
100 }
101
102
103 rq->struct_version = EC_HOST_REQUEST_VERSION;
104 rq->checksum = 0;
105 rq->command = cmd;
106 rq->command_version = cmd_version;
107 rq->reserved = 0;
108 rq->data_len = dout_len;
109
110
111 memcpy(rq + 1, dout, dout_len);
112
113
114 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(dev->dout, out_bytes));
115
116 cros_ec_dump_data("out", cmd, dev->dout, out_bytes);
117
118
119 return out_bytes;
120}
121
122
123
124
125
126
127
128
129static int prepare_proto3_response_buffer(struct cros_ec_dev *dev, int din_len)
130{
131 int in_bytes = din_len + sizeof(struct ec_host_response);
132
133
134 if (in_bytes > (int)sizeof(dev->din)) {
135 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
136 return -EC_RES_RESPONSE_TOO_BIG;
137 }
138
139
140 return in_bytes;
141}
142
143
144
145
146
147
148
149
150
151
152
153
154
155static int handle_proto3_response(struct cros_ec_dev *dev,
156 uint8_t **dinp, int din_len)
157{
158 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
159 int in_bytes;
160 int csum;
161
162 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
163
164
165 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
166 debug("%s: EC response version mismatch\n", __func__);
167 return -EC_RES_INVALID_RESPONSE;
168 }
169
170 if (rs->reserved) {
171 debug("%s: EC response reserved != 0\n", __func__);
172 return -EC_RES_INVALID_RESPONSE;
173 }
174
175 if (rs->data_len > din_len) {
176 debug("%s: EC returned too much data\n", __func__);
177 return -EC_RES_RESPONSE_TOO_BIG;
178 }
179
180 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
181
182
183 in_bytes = sizeof(*rs) + rs->data_len;
184
185
186 csum = cros_ec_calc_checksum(dev->din, in_bytes);
187 if (csum) {
188 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
189 csum);
190 return -EC_RES_INVALID_CHECKSUM;
191 }
192
193
194 if (rs->result)
195 return -(int)rs->result;
196
197
198 *dinp = (uint8_t *)(rs + 1);
199
200 return rs->data_len;
201}
202
203static int send_command_proto3(struct cros_ec_dev *dev,
204 int cmd, int cmd_version,
205 const void *dout, int dout_len,
206 uint8_t **dinp, int din_len)
207{
208 struct dm_cros_ec_ops *ops;
209 int out_bytes, in_bytes;
210 int rv;
211
212
213 out_bytes = create_proto3_request(dev, cmd, cmd_version,
214 dout, dout_len);
215 if (out_bytes < 0)
216 return out_bytes;
217
218
219 in_bytes = prepare_proto3_response_buffer(dev, din_len);
220 if (in_bytes < 0)
221 return in_bytes;
222
223 ops = dm_cros_ec_get_ops(dev->dev);
224 rv = ops->packet ? ops->packet(dev->dev, out_bytes, in_bytes) : -ENOSYS;
225 if (rv < 0)
226 return rv;
227
228
229 return handle_proto3_response(dev, dinp, din_len);
230}
231
232static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
233 const void *dout, int dout_len,
234 uint8_t **dinp, int din_len)
235{
236 struct dm_cros_ec_ops *ops;
237 int ret = -1;
238
239
240 if (dev->protocol_version == 3) {
241 return send_command_proto3(dev, cmd, cmd_version,
242 dout, dout_len, dinp, din_len);
243 }
244
245 ops = dm_cros_ec_get_ops(dev->dev);
246 ret = ops->command(dev->dev, cmd, cmd_version,
247 (const uint8_t *)dout, dout_len, dinp, din_len);
248
249 return ret;
250}
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268static int ec_command_inptr(struct cros_ec_dev *dev, uint8_t cmd,
269 int cmd_version, const void *dout, int dout_len, uint8_t **dinp,
270 int din_len)
271{
272 uint8_t *din = NULL;
273 int len;
274
275 len = send_command(dev, cmd, cmd_version, dout, dout_len,
276 &din, din_len);
277
278
279 if (len == -EC_RES_IN_PROGRESS) {
280 struct ec_response_get_comms_status *resp = NULL;
281 ulong start;
282
283
284 start = get_timer(0);
285 do {
286 int ret;
287
288 mdelay(50);
289 ret = send_command(dev, EC_CMD_GET_COMMS_STATUS, 0,
290 NULL, 0,
291 (uint8_t **)&resp, sizeof(*resp));
292 if (ret < 0)
293 return ret;
294
295 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
296 debug("%s: Command %#02x timeout\n",
297 __func__, cmd);
298 return -EC_RES_TIMEOUT;
299 }
300 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
301
302
303
304 len = send_command(dev, EC_CMD_RESEND_RESPONSE, 0,
305 NULL, 0, &din, din_len);
306 }
307
308 debug("%s: len=%d, din=%p\n", __func__, len, din);
309 if (dinp) {
310
311 assert(len <= 0 || !((uintptr_t)din & 7));
312 *dinp = din;
313 }
314
315 return len;
316}
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334static int ec_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
335 const void *dout, int dout_len,
336 void *din, int din_len)
337{
338 uint8_t *in_buffer;
339 int len;
340
341 assert((din_len == 0) || din);
342 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
343 &in_buffer, din_len);
344 if (len > 0) {
345
346
347
348
349 if (din && in_buffer) {
350 assert(len <= din_len);
351 memmove(din, in_buffer, len);
352 }
353 }
354 return len;
355}
356
357int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
358{
359 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
360
361 if (ec_command(cdev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
362 sizeof(scan->data)) != sizeof(scan->data))
363 return -1;
364
365 return 0;
366}
367
368int cros_ec_read_id(struct cros_ec_dev *dev, char *id, int maxlen)
369{
370 struct ec_response_get_version *r;
371
372 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
373 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
374 return -1;
375
376 if (maxlen > (int)sizeof(r->version_string_ro))
377 maxlen = sizeof(r->version_string_ro);
378
379 switch (r->current_image) {
380 case EC_IMAGE_RO:
381 memcpy(id, r->version_string_ro, maxlen);
382 break;
383 case EC_IMAGE_RW:
384 memcpy(id, r->version_string_rw, maxlen);
385 break;
386 default:
387 return -1;
388 }
389
390 id[maxlen - 1] = '\0';
391 return 0;
392}
393
394int cros_ec_read_version(struct cros_ec_dev *dev,
395 struct ec_response_get_version **versionp)
396{
397 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
398 (uint8_t **)versionp, sizeof(**versionp))
399 != sizeof(**versionp))
400 return -1;
401
402 return 0;
403}
404
405int cros_ec_read_build_info(struct cros_ec_dev *dev, char **strp)
406{
407 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
408 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
409 return -1;
410
411 return 0;
412}
413
414int cros_ec_read_current_image(struct cros_ec_dev *dev,
415 enum ec_current_image *image)
416{
417 struct ec_response_get_version *r;
418
419 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
420 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
421 return -1;
422
423 *image = r->current_image;
424 return 0;
425}
426
427static int cros_ec_wait_on_hash_done(struct cros_ec_dev *dev,
428 struct ec_response_vboot_hash *hash)
429{
430 struct ec_params_vboot_hash p;
431 ulong start;
432
433 start = get_timer(0);
434 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
435 mdelay(50);
436
437 p.cmd = EC_VBOOT_HASH_GET;
438 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
439 hash, sizeof(*hash)) < 0)
440 return -1;
441
442 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
443 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
444 return -EC_RES_TIMEOUT;
445 }
446 }
447 return 0;
448}
449
450
451int cros_ec_read_hash(struct cros_ec_dev *dev,
452 struct ec_response_vboot_hash *hash)
453{
454 struct ec_params_vboot_hash p;
455 int rv;
456
457 p.cmd = EC_VBOOT_HASH_GET;
458 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
459 hash, sizeof(*hash)) < 0)
460 return -1;
461
462
463 rv = cros_ec_wait_on_hash_done(dev, hash);
464 if (rv)
465 return rv;
466
467
468
469
470
471 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
472 return 0;
473
474 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
475 __func__, hash->status, hash->size);
476
477 p.cmd = EC_VBOOT_HASH_START;
478 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
479 p.nonce_size = 0;
480 p.offset = EC_VBOOT_HASH_OFFSET_RW;
481
482 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
483 hash, sizeof(*hash)) < 0)
484 return -1;
485
486 rv = cros_ec_wait_on_hash_done(dev, hash);
487 if (rv)
488 return rv;
489
490 debug("%s: hash done\n", __func__);
491
492 return 0;
493}
494
495static int cros_ec_invalidate_hash(struct cros_ec_dev *dev)
496{
497 struct ec_params_vboot_hash p;
498 struct ec_response_vboot_hash *hash;
499
500
501
502
503
504 p.cmd = EC_VBOOT_HASH_RECALC;
505 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
506 p.nonce_size = 0;
507 p.offset = 0;
508 p.size = 0;
509
510 debug("%s:\n", __func__);
511
512 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
513 (uint8_t **)&hash, sizeof(*hash)) < 0)
514 return -1;
515
516
517 return 0;
518}
519
520int cros_ec_reboot(struct cros_ec_dev *dev, enum ec_reboot_cmd cmd,
521 uint8_t flags)
522{
523 struct ec_params_reboot_ec p;
524
525 p.cmd = cmd;
526 p.flags = flags;
527
528 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
529 < 0)
530 return -1;
531
532 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
533
534
535
536
537
538
539
540
541
542
543
544 udelay(50000);
545 }
546
547 return 0;
548}
549
550int cros_ec_interrupt_pending(struct udevice *dev)
551{
552 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
553
554
555 if (!dm_gpio_is_valid(&cdev->ec_int))
556 return -ENOENT;
557
558 return dm_gpio_get_value(&cdev->ec_int);
559}
560
561int cros_ec_info(struct cros_ec_dev *dev, struct ec_response_mkbp_info *info)
562{
563 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
564 sizeof(*info)) != sizeof(*info))
565 return -1;
566
567 return 0;
568}
569
570int cros_ec_get_host_events(struct cros_ec_dev *dev, uint32_t *events_ptr)
571{
572 struct ec_response_host_event_mask *resp;
573
574
575
576
577
578 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
579 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
580 return -1;
581
582 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
583 return -1;
584
585 *events_ptr = resp->mask;
586 return 0;
587}
588
589int cros_ec_clear_host_events(struct cros_ec_dev *dev, uint32_t events)
590{
591 struct ec_params_host_event_mask params;
592
593 params.mask = events;
594
595
596
597
598
599 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
600 ¶ms, sizeof(params), NULL, 0) < 0)
601 return -1;
602
603 return 0;
604}
605
606int cros_ec_flash_protect(struct cros_ec_dev *dev,
607 uint32_t set_mask, uint32_t set_flags,
608 struct ec_response_flash_protect *resp)
609{
610 struct ec_params_flash_protect params;
611
612 params.mask = set_mask;
613 params.flags = set_flags;
614
615 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
616 ¶ms, sizeof(params),
617 resp, sizeof(*resp)) != sizeof(*resp))
618 return -1;
619
620 return 0;
621}
622
623static int cros_ec_check_version(struct cros_ec_dev *dev)
624{
625 struct ec_params_hello req;
626 struct ec_response_hello *resp;
627
628 struct dm_cros_ec_ops *ops;
629 int ret;
630
631 ops = dm_cros_ec_get_ops(dev->dev);
632 if (ops->check_version) {
633 ret = ops->check_version(dev->dev);
634 if (ret)
635 return ret;
636 }
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653 dev->protocol_version = 3;
654 req.in_data = 0;
655 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
656 (uint8_t **)&resp, sizeof(*resp)) > 0) {
657 return 0;
658 }
659
660
661 dev->protocol_version = 2;
662 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
663 (uint8_t **)&resp, sizeof(*resp)) > 0) {
664 return 0;
665 }
666
667
668
669
670
671
672
673 dev->protocol_version = 0;
674 printf("%s: ERROR: old EC interface not supported\n", __func__);
675 return -1;
676}
677
678int cros_ec_test(struct cros_ec_dev *dev)
679{
680 struct ec_params_hello req;
681 struct ec_response_hello *resp;
682
683 req.in_data = 0x12345678;
684 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
685 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
686 printf("ec_command_inptr() returned error\n");
687 return -1;
688 }
689 if (resp->out_data != req.in_data + 0x01020304) {
690 printf("Received invalid handshake %x\n", resp->out_data);
691 return -1;
692 }
693
694 return 0;
695}
696
697int cros_ec_flash_offset(struct cros_ec_dev *dev, enum ec_flash_region region,
698 uint32_t *offset, uint32_t *size)
699{
700 struct ec_params_flash_region_info p;
701 struct ec_response_flash_region_info *r;
702 int ret;
703
704 p.region = region;
705 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
706 EC_VER_FLASH_REGION_INFO,
707 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
708 if (ret != sizeof(*r))
709 return -1;
710
711 if (offset)
712 *offset = r->offset;
713 if (size)
714 *size = r->size;
715
716 return 0;
717}
718
719int cros_ec_flash_erase(struct cros_ec_dev *dev, uint32_t offset, uint32_t size)
720{
721 struct ec_params_flash_erase p;
722
723 p.offset = offset;
724 p.size = size;
725 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
726 NULL, 0);
727}
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747static int cros_ec_flash_write_block(struct cros_ec_dev *dev,
748 const uint8_t *data, uint32_t offset, uint32_t size)
749{
750 struct ec_params_flash_write *p;
751 int ret;
752
753 p = malloc(sizeof(*p) + size);
754 if (!p)
755 return -ENOMEM;
756
757 p->offset = offset;
758 p->size = size;
759 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
760 memcpy(p + 1, data, p->size);
761
762 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
763 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
764
765 free(p);
766
767 return ret;
768}
769
770
771
772
773static int cros_ec_flash_write_burst_size(struct cros_ec_dev *dev)
774{
775 return EC_FLASH_WRITE_VER0_SIZE;
776}
777
778
779
780
781
782
783
784
785
786
787
788static int cros_ec_data_is_erased(const uint32_t *data, int size)
789{
790 assert(!(size & 3));
791 size /= sizeof(uint32_t);
792 for (; size > 0; size -= 4, data++)
793 if (*data != -1U)
794 return 0;
795
796 return 1;
797}
798
799
800
801
802
803
804
805
806
807int cros_ec_read_flashinfo(struct cros_ec_dev *dev,
808 struct ec_response_flash_info *info)
809{
810 int ret;
811
812 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
813 NULL, 0, info, sizeof(*info));
814 if (ret < 0)
815 return ret;
816
817 return ret < sizeof(*info) ? -1 : 0;
818}
819
820int cros_ec_flash_write(struct cros_ec_dev *dev, const uint8_t *data,
821 uint32_t offset, uint32_t size)
822{
823 uint32_t burst = cros_ec_flash_write_burst_size(dev);
824 uint32_t end, off;
825 int ret;
826
827
828
829
830
831 end = offset + size;
832 for (off = offset; off < end; off += burst, data += burst) {
833 uint32_t todo;
834
835
836 todo = min(end - off, burst);
837 if (dev->optimise_flash_write &&
838 cros_ec_data_is_erased((uint32_t *)data, todo))
839 continue;
840
841 ret = cros_ec_flash_write_block(dev, data, off, todo);
842 if (ret)
843 return ret;
844 }
845
846 return 0;
847}
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864static int cros_ec_flash_read_block(struct cros_ec_dev *dev, uint8_t *data,
865 uint32_t offset, uint32_t size)
866{
867 struct ec_params_flash_read p;
868
869 p.offset = offset;
870 p.size = size;
871
872 return ec_command(dev, EC_CMD_FLASH_READ, 0,
873 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
874}
875
876int cros_ec_flash_read(struct cros_ec_dev *dev, uint8_t *data, uint32_t offset,
877 uint32_t size)
878{
879 uint32_t burst = cros_ec_flash_write_burst_size(dev);
880 uint32_t end, off;
881 int ret;
882
883 end = offset + size;
884 for (off = offset; off < end; off += burst, data += burst) {
885 ret = cros_ec_flash_read_block(dev, data, off,
886 min(end - off, burst));
887 if (ret)
888 return ret;
889 }
890
891 return 0;
892}
893
894int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
895 const uint8_t *image, int image_size)
896{
897 uint32_t rw_offset, rw_size;
898 int ret;
899
900 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_RW, &rw_offset, &rw_size))
901 return -1;
902 if (image_size > (int)rw_size)
903 return -1;
904
905
906
907
908
909 ret = cros_ec_invalidate_hash(dev);
910 if (ret)
911 return ret;
912
913
914
915
916
917
918
919
920
921 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
922 if (ret)
923 return ret;
924
925
926 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
927 if (ret)
928 return ret;
929
930 return 0;
931}
932
933int cros_ec_read_vbnvcontext(struct cros_ec_dev *dev, uint8_t *block)
934{
935 struct ec_params_vbnvcontext p;
936 int len;
937
938 p.op = EC_VBNV_CONTEXT_OP_READ;
939
940 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
941 &p, sizeof(p), block, EC_VBNV_BLOCK_SIZE);
942 if (len < EC_VBNV_BLOCK_SIZE)
943 return -1;
944
945 return 0;
946}
947
948int cros_ec_write_vbnvcontext(struct cros_ec_dev *dev, const uint8_t *block)
949{
950 struct ec_params_vbnvcontext p;
951 int len;
952
953 p.op = EC_VBNV_CONTEXT_OP_WRITE;
954 memcpy(p.block, block, sizeof(p.block));
955
956 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
957 &p, sizeof(p), NULL, 0);
958 if (len < 0)
959 return -1;
960
961 return 0;
962}
963
964int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
965{
966 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
967 struct ec_params_ldo_set params;
968
969 params.index = index;
970 params.state = state;
971
972 if (ec_command_inptr(cdev, EC_CMD_LDO_SET, 0, ¶ms, sizeof(params),
973 NULL, 0))
974 return -1;
975
976 return 0;
977}
978
979int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
980{
981 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
982 struct ec_params_ldo_get params;
983 struct ec_response_ldo_get *resp;
984
985 params.index = index;
986
987 if (ec_command_inptr(cdev, EC_CMD_LDO_GET, 0, ¶ms, sizeof(params),
988 (uint8_t **)&resp, sizeof(*resp)) !=
989 sizeof(*resp))
990 return -1;
991
992 *state = resp->state;
993
994 return 0;
995}
996
997int cros_ec_register(struct udevice *dev)
998{
999 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1000 char id[MSG_BYTES];
1001
1002 cdev->dev = dev;
1003 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1004 GPIOD_IS_IN);
1005 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
1006
1007 if (cros_ec_check_version(cdev)) {
1008 debug("%s: Could not detect CROS-EC version\n", __func__);
1009 return -CROS_EC_ERR_CHECK_VERSION;
1010 }
1011
1012 if (cros_ec_read_id(cdev, id, sizeof(id))) {
1013 debug("%s: Could not read KBC ID\n", __func__);
1014 return -CROS_EC_ERR_READ_ID;
1015 }
1016
1017
1018 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1019 cdev->protocol_version, id);
1020
1021 return 0;
1022}
1023
1024int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
1025{
1026 ofnode flash_node, node;
1027
1028 flash_node = dev_read_subnode(dev, "flash");
1029 if (!ofnode_valid(flash_node)) {
1030 debug("Failed to find flash node\n");
1031 return -1;
1032 }
1033
1034 if (of_read_fmap_entry(flash_node, "flash", &config->flash)) {
1035 debug("Failed to decode flash node in chrome-ec\n");
1036 return -1;
1037 }
1038
1039 config->flash_erase_value = ofnode_read_s32_default(flash_node,
1040 "erase-value", -1);
1041 ofnode_for_each_subnode(node, flash_node) {
1042 const char *name = ofnode_get_name(node);
1043 enum ec_flash_region region;
1044
1045 if (0 == strcmp(name, "ro")) {
1046 region = EC_FLASH_REGION_RO;
1047 } else if (0 == strcmp(name, "rw")) {
1048 region = EC_FLASH_REGION_RW;
1049 } else if (0 == strcmp(name, "wp-ro")) {
1050 region = EC_FLASH_REGION_WP_RO;
1051 } else {
1052 debug("Unknown EC flash region name '%s'\n", name);
1053 return -1;
1054 }
1055
1056 if (of_read_fmap_entry(node, "reg", &config->region[region])) {
1057 debug("Failed to decode flash region in chrome-ec'\n");
1058 return -1;
1059 }
1060 }
1061
1062 return 0;
1063}
1064
1065int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1066 int nmsgs)
1067{
1068 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1069 union {
1070 struct ec_params_i2c_passthru p;
1071 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1072 } params;
1073 union {
1074 struct ec_response_i2c_passthru r;
1075 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1076 } response;
1077 struct ec_params_i2c_passthru *p = ¶ms.p;
1078 struct ec_response_i2c_passthru *r = &response.r;
1079 struct ec_params_i2c_passthru_msg *msg;
1080 uint8_t *pdata, *read_ptr = NULL;
1081 int read_len;
1082 int size;
1083 int rv;
1084 int i;
1085
1086 p->port = port;
1087
1088 p->num_msgs = nmsgs;
1089 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1090
1091
1092 pdata = (uint8_t *)p + size;
1093
1094 read_len = 0;
1095 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1096 bool is_read = in->flags & I2C_M_RD;
1097
1098 msg->addr_flags = in->addr;
1099 msg->len = in->len;
1100 if (is_read) {
1101 msg->addr_flags |= EC_I2C_FLAG_READ;
1102 read_len += in->len;
1103 read_ptr = in->buf;
1104 if (sizeof(*r) + read_len > sizeof(response)) {
1105 puts("Read length too big for buffer\n");
1106 return -1;
1107 }
1108 } else {
1109 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1110 puts("Params too large for buffer\n");
1111 return -1;
1112 }
1113 memcpy(pdata, in->buf, in->len);
1114 pdata += in->len;
1115 }
1116 }
1117
1118 rv = ec_command(cdev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
1119 r, sizeof(*r) + read_len);
1120 if (rv < 0)
1121 return rv;
1122
1123
1124 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1125 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1126 return -1;
1127 }
1128
1129 if (rv < sizeof(*r) + read_len) {
1130 puts("Truncated read response\n");
1131 return -1;
1132 }
1133
1134
1135 if (read_len)
1136 memcpy(read_ptr, r->data, read_len);
1137
1138 return 0;
1139}
1140
1141UCLASS_DRIVER(cros_ec) = {
1142 .id = UCLASS_CROS_EC,
1143 .name = "cros_ec",
1144 .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
1145 .post_bind = dm_scan_fdt_dev,
1146};
1147