1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#define LOG_CATEGORY UCLASS_CROS_EC
17
18#include <common.h>
19#include <command.h>
20#include <dm.h>
21#include <i2c.h>
22#include <cros_ec.h>
23#include <fdtdec.h>
24#include <malloc.h>
25#include <spi.h>
26#include <linux/errno.h>
27#include <asm/io.h>
28#include <asm-generic/gpio.h>
29#include <dm/device-internal.h>
30#include <dm/of_extra.h>
31#include <dm/uclass-internal.h>
32
33#ifdef DEBUG_TRACE
34#define debug_trace(fmt, b...) debug(fmt, #b)
35#else
36#define debug_trace(fmt, b...)
37#endif
38
39enum {
40
41 CROS_EC_CMD_TIMEOUT_MS = 5000,
42
43 CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
44};
45
46#define INVALID_HCMD 0xFF
47
48
49
50
51
52static const struct {
53 u8 set_cmd;
54 u8 clear_cmd;
55 u8 get_cmd;
56} event_map[] = {
57 [EC_HOST_EVENT_MAIN] = {
58 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR,
59 INVALID_HCMD,
60 },
61 [EC_HOST_EVENT_B] = {
62 INVALID_HCMD, EC_CMD_HOST_EVENT_CLEAR_B,
63 EC_CMD_HOST_EVENT_GET_B,
64 },
65 [EC_HOST_EVENT_SCI_MASK] = {
66 EC_CMD_HOST_EVENT_SET_SCI_MASK, INVALID_HCMD,
67 EC_CMD_HOST_EVENT_GET_SCI_MASK,
68 },
69 [EC_HOST_EVENT_SMI_MASK] = {
70 EC_CMD_HOST_EVENT_SET_SMI_MASK, INVALID_HCMD,
71 EC_CMD_HOST_EVENT_GET_SMI_MASK,
72 },
73 [EC_HOST_EVENT_ALWAYS_REPORT_MASK] = {
74 INVALID_HCMD, INVALID_HCMD, INVALID_HCMD,
75 },
76 [EC_HOST_EVENT_ACTIVE_WAKE_MASK] = {
77 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
78 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
79 },
80 [EC_HOST_EVENT_LAZY_WAKE_MASK_S0IX] = {
81 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
82 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
83 },
84 [EC_HOST_EVENT_LAZY_WAKE_MASK_S3] = {
85 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
86 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
87 },
88 [EC_HOST_EVENT_LAZY_WAKE_MASK_S5] = {
89 EC_CMD_HOST_EVENT_SET_WAKE_MASK, INVALID_HCMD,
90 EC_CMD_HOST_EVENT_GET_WAKE_MASK,
91 },
92};
93
94void cros_ec_dump_data(const char *name, int cmd, const uint8_t *data, int len)
95{
96#ifdef DEBUG
97 int i;
98
99 printf("%s: ", name);
100 if (cmd != -1)
101 printf("cmd=%#x: ", cmd);
102 for (i = 0; i < len; i++)
103 printf("%02x ", data[i]);
104 printf("\n");
105#endif
106}
107
108
109
110
111
112
113
114
115int cros_ec_calc_checksum(const uint8_t *data, int size)
116{
117 int csum, i;
118
119 for (i = csum = 0; i < size; i++)
120 csum += data[i];
121 return csum & 0xff;
122}
123
124
125
126
127
128
129
130
131
132
133
134
135
136static int create_proto3_request(struct cros_ec_dev *cdev,
137 int cmd, int cmd_version,
138 const void *dout, int dout_len)
139{
140 struct ec_host_request *rq = (struct ec_host_request *)cdev->dout;
141 int out_bytes = dout_len + sizeof(*rq);
142
143
144 if (out_bytes > (int)sizeof(cdev->dout)) {
145 debug("%s: Cannot send %d bytes\n", __func__, dout_len);
146 return -EC_RES_REQUEST_TRUNCATED;
147 }
148
149
150 rq->struct_version = EC_HOST_REQUEST_VERSION;
151 rq->checksum = 0;
152 rq->command = cmd;
153 rq->command_version = cmd_version;
154 rq->reserved = 0;
155 rq->data_len = dout_len;
156
157
158 memcpy(rq + 1, dout, dout_len);
159
160
161 rq->checksum = (uint8_t)(-cros_ec_calc_checksum(cdev->dout, out_bytes));
162
163 cros_ec_dump_data("out", cmd, cdev->dout, out_bytes);
164
165
166 return out_bytes;
167}
168
169
170
171
172
173
174
175
176static int prepare_proto3_response_buffer(struct cros_ec_dev *cdev, int din_len)
177{
178 int in_bytes = din_len + sizeof(struct ec_host_response);
179
180
181 if (in_bytes > (int)sizeof(cdev->din)) {
182 debug("%s: Cannot receive %d bytes\n", __func__, din_len);
183 return -EC_RES_RESPONSE_TOO_BIG;
184 }
185
186
187 return in_bytes;
188}
189
190
191
192
193
194
195
196
197
198
199
200
201
202static int handle_proto3_response(struct cros_ec_dev *dev,
203 uint8_t **dinp, int din_len)
204{
205 struct ec_host_response *rs = (struct ec_host_response *)dev->din;
206 int in_bytes;
207 int csum;
208
209 cros_ec_dump_data("in-header", -1, dev->din, sizeof(*rs));
210
211
212 if (rs->struct_version != EC_HOST_RESPONSE_VERSION) {
213 debug("%s: EC response version mismatch\n", __func__);
214 return -EC_RES_INVALID_RESPONSE;
215 }
216
217 if (rs->reserved) {
218 debug("%s: EC response reserved != 0\n", __func__);
219 return -EC_RES_INVALID_RESPONSE;
220 }
221
222 if (rs->data_len > din_len) {
223 debug("%s: EC returned too much data\n", __func__);
224 return -EC_RES_RESPONSE_TOO_BIG;
225 }
226
227 cros_ec_dump_data("in-data", -1, dev->din + sizeof(*rs), rs->data_len);
228
229
230 in_bytes = sizeof(*rs) + rs->data_len;
231
232
233 csum = cros_ec_calc_checksum(dev->din, in_bytes);
234 if (csum) {
235 debug("%s: EC response checksum invalid: 0x%02x\n", __func__,
236 csum);
237 return -EC_RES_INVALID_CHECKSUM;
238 }
239
240
241 if (rs->result)
242 return -(int)rs->result;
243
244
245 *dinp = (uint8_t *)(rs + 1);
246
247 return rs->data_len;
248}
249
250static int send_command_proto3(struct cros_ec_dev *cdev,
251 int cmd, int cmd_version,
252 const void *dout, int dout_len,
253 uint8_t **dinp, int din_len)
254{
255 struct dm_cros_ec_ops *ops;
256 int out_bytes, in_bytes;
257 int rv;
258
259
260 out_bytes = create_proto3_request(cdev, cmd, cmd_version,
261 dout, dout_len);
262 if (out_bytes < 0)
263 return out_bytes;
264
265
266 in_bytes = prepare_proto3_response_buffer(cdev, din_len);
267 if (in_bytes < 0)
268 return in_bytes;
269
270 ops = dm_cros_ec_get_ops(cdev->dev);
271 rv = ops->packet ? ops->packet(cdev->dev, out_bytes, in_bytes) :
272 -ENOSYS;
273 if (rv < 0)
274 return rv;
275
276
277 return handle_proto3_response(cdev, dinp, din_len);
278}
279
280static int send_command(struct cros_ec_dev *dev, uint cmd, int cmd_version,
281 const void *dout, int dout_len,
282 uint8_t **dinp, int din_len)
283{
284 struct dm_cros_ec_ops *ops;
285 int ret = -1;
286
287
288 if (dev->protocol_version == 3) {
289 return send_command_proto3(dev, cmd, cmd_version,
290 dout, dout_len, dinp, din_len);
291 }
292
293 ops = dm_cros_ec_get_ops(dev->dev);
294 ret = ops->command(dev->dev, cmd, cmd_version,
295 (const uint8_t *)dout, dout_len, dinp, din_len);
296
297 return ret;
298}
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316static int ec_command_inptr(struct udevice *dev, uint8_t cmd,
317 int cmd_version, const void *dout, int dout_len,
318 uint8_t **dinp, int din_len)
319{
320 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
321 uint8_t *din = NULL;
322 int len;
323
324 len = send_command(cdev, cmd, cmd_version, dout, dout_len, &din,
325 din_len);
326
327
328 if (len == -EC_RES_IN_PROGRESS) {
329 struct ec_response_get_comms_status *resp = NULL;
330 ulong start;
331
332
333 start = get_timer(0);
334 do {
335 int ret;
336
337 mdelay(50);
338 ret = send_command(cdev, EC_CMD_GET_COMMS_STATUS, 0,
339 NULL, 0,
340 (uint8_t **)&resp, sizeof(*resp));
341 if (ret < 0)
342 return ret;
343
344 if (get_timer(start) > CROS_EC_CMD_TIMEOUT_MS) {
345 debug("%s: Command %#02x timeout\n",
346 __func__, cmd);
347 return -EC_RES_TIMEOUT;
348 }
349 } while (resp->flags & EC_COMMS_STATUS_PROCESSING);
350
351
352
353 len = send_command(cdev, EC_CMD_RESEND_RESPONSE, 0, NULL, 0,
354 &din, din_len);
355 }
356
357 debug("%s: len=%d, din=%p\n", __func__, len, din);
358 if (dinp) {
359
360 assert(len <= 0 || !((uintptr_t)din & 7));
361 *dinp = din;
362 }
363
364 return len;
365}
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383static int ec_command(struct udevice *dev, uint cmd, int cmd_version,
384 const void *dout, int dout_len,
385 void *din, int din_len)
386{
387 uint8_t *in_buffer;
388 int len;
389
390 assert((din_len == 0) || din);
391 len = ec_command_inptr(dev, cmd, cmd_version, dout, dout_len,
392 &in_buffer, din_len);
393 if (len > 0) {
394
395
396
397
398 if (din && in_buffer) {
399 assert(len <= din_len);
400 memmove(din, in_buffer, len);
401 }
402 }
403 return len;
404}
405
406int cros_ec_scan_keyboard(struct udevice *dev, struct mbkp_keyscan *scan)
407{
408 if (ec_command(dev, EC_CMD_MKBP_STATE, 0, NULL, 0, scan,
409 sizeof(scan->data)) != sizeof(scan->data))
410 return -1;
411
412 return 0;
413}
414
415int cros_ec_read_id(struct udevice *dev, char *id, int maxlen)
416{
417 struct ec_response_get_version *r;
418 int ret;
419
420 ret = ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
421 (uint8_t **)&r, sizeof(*r));
422 if (ret != sizeof(*r)) {
423 log_err("Got rc %d, expected %u\n", ret, (uint)sizeof(*r));
424 return -1;
425 }
426
427 if (maxlen > (int)sizeof(r->version_string_ro))
428 maxlen = sizeof(r->version_string_ro);
429
430 switch (r->current_image) {
431 case EC_IMAGE_RO:
432 memcpy(id, r->version_string_ro, maxlen);
433 break;
434 case EC_IMAGE_RW:
435 memcpy(id, r->version_string_rw, maxlen);
436 break;
437 default:
438 log_err("Invalid EC image %d\n", r->current_image);
439 return -1;
440 }
441
442 id[maxlen - 1] = '\0';
443 return 0;
444}
445
446int cros_ec_read_version(struct udevice *dev,
447 struct ec_response_get_version **versionp)
448{
449 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
450 (uint8_t **)versionp, sizeof(**versionp))
451 != sizeof(**versionp))
452 return -1;
453
454 return 0;
455}
456
457int cros_ec_read_build_info(struct udevice *dev, char **strp)
458{
459 if (ec_command_inptr(dev, EC_CMD_GET_BUILD_INFO, 0, NULL, 0,
460 (uint8_t **)strp, EC_PROTO2_MAX_PARAM_SIZE) < 0)
461 return -1;
462
463 return 0;
464}
465
466int cros_ec_read_current_image(struct udevice *dev,
467 enum ec_current_image *image)
468{
469 struct ec_response_get_version *r;
470
471 if (ec_command_inptr(dev, EC_CMD_GET_VERSION, 0, NULL, 0,
472 (uint8_t **)&r, sizeof(*r)) != sizeof(*r))
473 return -1;
474
475 *image = r->current_image;
476 return 0;
477}
478
479static int cros_ec_wait_on_hash_done(struct udevice *dev,
480 struct ec_response_vboot_hash *hash)
481{
482 struct ec_params_vboot_hash p;
483 ulong start;
484
485 start = get_timer(0);
486 while (hash->status == EC_VBOOT_HASH_STATUS_BUSY) {
487 mdelay(50);
488
489 p.cmd = EC_VBOOT_HASH_GET;
490 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
491 hash, sizeof(*hash)) < 0)
492 return -1;
493
494 if (get_timer(start) > CROS_EC_CMD_HASH_TIMEOUT_MS) {
495 debug("%s: EC_VBOOT_HASH_GET timeout\n", __func__);
496 return -EC_RES_TIMEOUT;
497 }
498 }
499 return 0;
500}
501
502int cros_ec_read_hash(struct udevice *dev, uint hash_offset,
503 struct ec_response_vboot_hash *hash)
504{
505 struct ec_params_vboot_hash p;
506 int rv;
507
508 p.cmd = EC_VBOOT_HASH_GET;
509 p.offset = hash_offset;
510 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
511 hash, sizeof(*hash)) < 0)
512 return -1;
513
514
515 rv = cros_ec_wait_on_hash_done(dev, hash);
516 if (rv)
517 return rv;
518
519
520
521
522
523 if (hash->status == EC_VBOOT_HASH_STATUS_DONE && hash->size)
524 return 0;
525
526 debug("%s: No valid hash (status=%d size=%d). Compute one...\n",
527 __func__, hash->status, hash->size);
528
529 p.cmd = EC_VBOOT_HASH_START;
530 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
531 p.nonce_size = 0;
532 p.offset = hash_offset;
533
534 if (ec_command(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
535 hash, sizeof(*hash)) < 0)
536 return -1;
537
538 rv = cros_ec_wait_on_hash_done(dev, hash);
539 if (rv)
540 return rv;
541
542 debug("%s: hash done\n", __func__);
543
544 return 0;
545}
546
547static int cros_ec_invalidate_hash(struct udevice *dev)
548{
549 struct ec_params_vboot_hash p;
550 struct ec_response_vboot_hash *hash;
551
552
553
554
555
556 p.cmd = EC_VBOOT_HASH_RECALC;
557 p.hash_type = EC_VBOOT_HASH_TYPE_SHA256;
558 p.nonce_size = 0;
559 p.offset = 0;
560 p.size = 0;
561
562 debug("%s:\n", __func__);
563
564 if (ec_command_inptr(dev, EC_CMD_VBOOT_HASH, 0, &p, sizeof(p),
565 (uint8_t **)&hash, sizeof(*hash)) < 0)
566 return -1;
567
568
569 return 0;
570}
571
572int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)
573{
574 struct ec_params_reboot_ec p;
575
576 p.cmd = cmd;
577 p.flags = flags;
578
579 if (ec_command_inptr(dev, EC_CMD_REBOOT_EC, 0, &p, sizeof(p), NULL, 0)
580 < 0)
581 return -1;
582
583 if (!(flags & EC_REBOOT_FLAG_ON_AP_SHUTDOWN)) {
584
585
586
587
588
589
590
591
592
593
594
595 udelay(50000);
596 }
597
598 return 0;
599}
600
601int cros_ec_interrupt_pending(struct udevice *dev)
602{
603 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
604
605
606 if (!dm_gpio_is_valid(&cdev->ec_int))
607 return -ENOENT;
608
609 return dm_gpio_get_value(&cdev->ec_int);
610}
611
612int cros_ec_info(struct udevice *dev, struct ec_response_mkbp_info *info)
613{
614 if (ec_command(dev, EC_CMD_MKBP_INFO, 0, NULL, 0, info,
615 sizeof(*info)) != sizeof(*info))
616 return -1;
617
618 return 0;
619}
620
621int cros_ec_get_event_mask(struct udevice *dev, uint type, uint32_t *mask)
622{
623 struct ec_response_host_event_mask rsp;
624 int ret;
625
626 ret = ec_command(dev, type, 0, NULL, 0, &rsp, sizeof(rsp));
627 if (ret < 0)
628 return ret;
629 else if (ret != sizeof(rsp))
630 return -EINVAL;
631
632 *mask = rsp.mask;
633
634 return 0;
635}
636
637int cros_ec_set_event_mask(struct udevice *dev, uint type, uint32_t mask)
638{
639 struct ec_params_host_event_mask req;
640 int ret;
641
642 req.mask = mask;
643
644 ret = ec_command(dev, type, 0, &req, sizeof(req), NULL, 0);
645 if (ret < 0)
646 return ret;
647
648 return 0;
649}
650
651int cros_ec_get_host_events(struct udevice *dev, uint32_t *events_ptr)
652{
653 struct ec_response_host_event_mask *resp;
654
655
656
657
658
659 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_GET_B, 0, NULL, 0,
660 (uint8_t **)&resp, sizeof(*resp)) < (int)sizeof(*resp))
661 return -1;
662
663 if (resp->mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_INVALID))
664 return -1;
665
666 *events_ptr = resp->mask;
667 return 0;
668}
669
670int cros_ec_clear_host_events(struct udevice *dev, uint32_t events)
671{
672 struct ec_params_host_event_mask params;
673
674 params.mask = events;
675
676
677
678
679
680 if (ec_command_inptr(dev, EC_CMD_HOST_EVENT_CLEAR_B, 0,
681 ¶ms, sizeof(params), NULL, 0) < 0)
682 return -1;
683
684 return 0;
685}
686
687int cros_ec_flash_protect(struct udevice *dev, uint32_t set_mask,
688 uint32_t set_flags,
689 struct ec_response_flash_protect *resp)
690{
691 struct ec_params_flash_protect params;
692
693 params.mask = set_mask;
694 params.flags = set_flags;
695
696 if (ec_command(dev, EC_CMD_FLASH_PROTECT, EC_VER_FLASH_PROTECT,
697 ¶ms, sizeof(params),
698 resp, sizeof(*resp)) != sizeof(*resp))
699 return -1;
700
701 return 0;
702}
703
704int cros_ec_entering_mode(struct udevice *dev, int mode)
705{
706 int rc;
707
708 rc = ec_command(dev, EC_CMD_ENTERING_MODE, 0, &mode, sizeof(mode),
709 NULL, 0);
710 if (rc)
711 return -1;
712 return 0;
713}
714
715static int cros_ec_check_version(struct udevice *dev)
716{
717 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
718 struct ec_params_hello req;
719 struct ec_response_hello *resp;
720
721 struct dm_cros_ec_ops *ops;
722 int ret;
723
724 ops = dm_cros_ec_get_ops(dev);
725 if (ops->check_version) {
726 ret = ops->check_version(dev);
727 if (ret)
728 return ret;
729 }
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746 cdev->protocol_version = 3;
747 req.in_data = 0;
748 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
749 (uint8_t **)&resp, sizeof(*resp)) > 0)
750 return 0;
751
752
753 cdev->protocol_version = 2;
754 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
755 (uint8_t **)&resp, sizeof(*resp)) > 0)
756 return 0;
757
758
759
760
761
762
763
764 cdev->protocol_version = 0;
765 printf("%s: ERROR: old EC interface not supported\n", __func__);
766 return -1;
767}
768
769int cros_ec_test(struct udevice *dev)
770{
771 struct ec_params_hello req;
772 struct ec_response_hello *resp;
773
774 req.in_data = 0x12345678;
775 if (ec_command_inptr(dev, EC_CMD_HELLO, 0, &req, sizeof(req),
776 (uint8_t **)&resp, sizeof(*resp)) < sizeof(*resp)) {
777 printf("ec_command_inptr() returned error\n");
778 return -1;
779 }
780 if (resp->out_data != req.in_data + 0x01020304) {
781 printf("Received invalid handshake %x\n", resp->out_data);
782 return -1;
783 }
784
785 return 0;
786}
787
788int cros_ec_flash_offset(struct udevice *dev, enum ec_flash_region region,
789 uint32_t *offset, uint32_t *size)
790{
791 struct ec_params_flash_region_info p;
792 struct ec_response_flash_region_info *r;
793 int ret;
794
795 p.region = region;
796 ret = ec_command_inptr(dev, EC_CMD_FLASH_REGION_INFO,
797 EC_VER_FLASH_REGION_INFO,
798 &p, sizeof(p), (uint8_t **)&r, sizeof(*r));
799 if (ret != sizeof(*r))
800 return -1;
801
802 if (offset)
803 *offset = r->offset;
804 if (size)
805 *size = r->size;
806
807 return 0;
808}
809
810int cros_ec_flash_erase(struct udevice *dev, uint32_t offset, uint32_t size)
811{
812 struct ec_params_flash_erase p;
813
814 p.offset = offset;
815 p.size = size;
816 return ec_command_inptr(dev, EC_CMD_FLASH_ERASE, 0, &p, sizeof(p),
817 NULL, 0);
818}
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838static int cros_ec_flash_write_block(struct udevice *dev, const uint8_t *data,
839 uint32_t offset, uint32_t size)
840{
841 struct ec_params_flash_write *p;
842 int ret;
843
844 p = malloc(sizeof(*p) + size);
845 if (!p)
846 return -ENOMEM;
847
848 p->offset = offset;
849 p->size = size;
850 assert(data && p->size <= EC_FLASH_WRITE_VER0_SIZE);
851 memcpy(p + 1, data, p->size);
852
853 ret = ec_command_inptr(dev, EC_CMD_FLASH_WRITE, 0,
854 p, sizeof(*p) + size, NULL, 0) >= 0 ? 0 : -1;
855
856 free(p);
857
858 return ret;
859}
860
861
862
863
864static int cros_ec_flash_write_burst_size(struct udevice *dev)
865{
866 return EC_FLASH_WRITE_VER0_SIZE;
867}
868
869
870
871
872
873
874
875
876
877
878
879static int cros_ec_data_is_erased(const uint32_t *data, int size)
880{
881 assert(!(size & 3));
882 size /= sizeof(uint32_t);
883 for (; size > 0; size -= 4, data++)
884 if (*data != -1U)
885 return 0;
886
887 return 1;
888}
889
890
891
892
893
894
895
896
897
898int cros_ec_read_flashinfo(struct udevice *dev,
899 struct ec_response_flash_info *info)
900{
901 int ret;
902
903 ret = ec_command(dev, EC_CMD_FLASH_INFO, 0,
904 NULL, 0, info, sizeof(*info));
905 if (ret < 0)
906 return ret;
907
908 return ret < sizeof(*info) ? -1 : 0;
909}
910
911int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,
912 uint32_t offset, uint32_t size)
913{
914 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
915 uint32_t burst = cros_ec_flash_write_burst_size(dev);
916 uint32_t end, off;
917 int ret;
918
919 if (!burst)
920 return -EINVAL;
921
922
923
924
925
926 end = offset + size;
927 for (off = offset; off < end; off += burst, data += burst) {
928 uint32_t todo;
929
930
931 todo = min(end - off, burst);
932 if (cdev->optimise_flash_write &&
933 cros_ec_data_is_erased((uint32_t *)data, todo))
934 continue;
935
936 ret = cros_ec_flash_write_block(dev, data, off, todo);
937 if (ret)
938 return ret;
939 }
940
941 return 0;
942}
943
944
945
946
947
948
949
950
951int cros_ec_efs_verify(struct udevice *dev, enum ec_flash_region region)
952{
953 struct ec_params_efs_verify p;
954 int rv;
955
956 log_info("EFS: EC is verifying updated image...\n");
957 p.region = region;
958
959 rv = ec_command(dev, EC_CMD_EFS_VERIFY, 0, &p, sizeof(p), NULL, 0);
960 if (rv >= 0) {
961 log_info("EFS: Verification success\n");
962 return 0;
963 }
964 if (rv == -EC_RES_INVALID_COMMAND) {
965 log_info("EFS: EC doesn't support EFS_VERIFY command\n");
966 return 0;
967 }
968 log_info("EFS: Verification failed\n");
969
970 return rv;
971}
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988static int cros_ec_flash_read_block(struct udevice *dev, uint8_t *data,
989 uint32_t offset, uint32_t size)
990{
991 struct ec_params_flash_read p;
992
993 p.offset = offset;
994 p.size = size;
995
996 return ec_command(dev, EC_CMD_FLASH_READ, 0,
997 &p, sizeof(p), data, size) >= 0 ? 0 : -1;
998}
999
1000int cros_ec_flash_read(struct udevice *dev, uint8_t *data, uint32_t offset,
1001 uint32_t size)
1002{
1003 uint32_t burst = cros_ec_flash_write_burst_size(dev);
1004 uint32_t end, off;
1005 int ret;
1006
1007 end = offset + size;
1008 for (off = offset; off < end; off += burst, data += burst) {
1009 ret = cros_ec_flash_read_block(dev, data, off,
1010 min(end - off, burst));
1011 if (ret)
1012 return ret;
1013 }
1014
1015 return 0;
1016}
1017
1018int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,
1019 int image_size)
1020{
1021 uint32_t rw_offset, rw_size;
1022 int ret;
1023
1024 if (cros_ec_flash_offset(dev, EC_FLASH_REGION_ACTIVE, &rw_offset,
1025 &rw_size))
1026 return -1;
1027 if (image_size > (int)rw_size)
1028 return -1;
1029
1030
1031
1032
1033
1034 ret = cros_ec_invalidate_hash(dev);
1035 if (ret)
1036 return ret;
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046 ret = cros_ec_flash_erase(dev, rw_offset, rw_size);
1047 if (ret)
1048 return ret;
1049
1050
1051 ret = cros_ec_flash_write(dev, image, rw_offset, image_size);
1052 if (ret)
1053 return ret;
1054
1055 return 0;
1056}
1057
1058int cros_ec_read_nvdata(struct udevice *dev, uint8_t *block, int size)
1059{
1060 struct ec_params_vbnvcontext p;
1061 int len;
1062
1063 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
1064 return -EINVAL;
1065
1066 p.op = EC_VBNV_CONTEXT_OP_READ;
1067
1068 len = ec_command(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
1069 &p, sizeof(uint32_t) + size, block, size);
1070 if (len != size) {
1071 log_err("Expected %d bytes, got %d\n", size, len);
1072 return -EIO;
1073 }
1074
1075 return 0;
1076}
1077
1078int cros_ec_write_nvdata(struct udevice *dev, const uint8_t *block, int size)
1079{
1080 struct ec_params_vbnvcontext p;
1081 int len;
1082
1083 if (size != EC_VBNV_BLOCK_SIZE && size != EC_VBNV_BLOCK_SIZE_V2)
1084 return -EINVAL;
1085 p.op = EC_VBNV_CONTEXT_OP_WRITE;
1086 memcpy(p.block, block, size);
1087
1088 len = ec_command_inptr(dev, EC_CMD_VBNV_CONTEXT, EC_VER_VBNV_CONTEXT,
1089 &p, sizeof(uint32_t) + size, NULL, 0);
1090 if (len < 0)
1091 return -1;
1092
1093 return 0;
1094}
1095
1096int cros_ec_battery_cutoff(struct udevice *dev, uint8_t flags)
1097{
1098 struct ec_params_battery_cutoff p;
1099 int len;
1100
1101 p.flags = flags;
1102 len = ec_command(dev, EC_CMD_BATTERY_CUT_OFF, 1, &p, sizeof(p),
1103 NULL, 0);
1104
1105 if (len < 0)
1106 return -1;
1107 return 0;
1108}
1109
1110int cros_ec_set_ldo(struct udevice *dev, uint8_t index, uint8_t state)
1111{
1112 struct ec_params_ldo_set params;
1113
1114 params.index = index;
1115 params.state = state;
1116
1117 if (ec_command_inptr(dev, EC_CMD_LDO_SET, 0, ¶ms, sizeof(params),
1118 NULL, 0))
1119 return -1;
1120
1121 return 0;
1122}
1123
1124int cros_ec_get_ldo(struct udevice *dev, uint8_t index, uint8_t *state)
1125{
1126 struct ec_params_ldo_get params;
1127 struct ec_response_ldo_get *resp;
1128
1129 params.index = index;
1130
1131 if (ec_command_inptr(dev, EC_CMD_LDO_GET, 0, ¶ms, sizeof(params),
1132 (uint8_t **)&resp, sizeof(*resp)) !=
1133 sizeof(*resp))
1134 return -1;
1135
1136 *state = resp->state;
1137
1138 return 0;
1139}
1140
1141int cros_ec_register(struct udevice *dev)
1142{
1143 struct cros_ec_dev *cdev = dev_get_uclass_priv(dev);
1144 char id[MSG_BYTES];
1145
1146 cdev->dev = dev;
1147 gpio_request_by_name(dev, "ec-interrupt", 0, &cdev->ec_int,
1148 GPIOD_IS_IN);
1149 cdev->optimise_flash_write = dev_read_bool(dev, "optimise-flash-write");
1150
1151 if (cros_ec_check_version(dev)) {
1152 debug("%s: Could not detect CROS-EC version\n", __func__);
1153 return -CROS_EC_ERR_CHECK_VERSION;
1154 }
1155
1156 if (cros_ec_read_id(dev, id, sizeof(id))) {
1157 debug("%s: Could not read KBC ID\n", __func__);
1158 return -CROS_EC_ERR_READ_ID;
1159 }
1160
1161
1162 debug("Google Chrome EC v%d CROS-EC driver ready, id '%s'\n",
1163 cdev->protocol_version, id);
1164
1165 return 0;
1166}
1167
1168int cros_ec_decode_ec_flash(struct udevice *dev, struct fdt_cros_ec *config)
1169{
1170 ofnode flash_node, node;
1171
1172 flash_node = dev_read_subnode(dev, "flash");
1173 if (!ofnode_valid(flash_node)) {
1174 debug("Failed to find flash node\n");
1175 return -1;
1176 }
1177
1178 if (ofnode_read_fmap_entry(flash_node, &config->flash)) {
1179 debug("Failed to decode flash node in chrome-ec\n");
1180 return -1;
1181 }
1182
1183 config->flash_erase_value = ofnode_read_s32_default(flash_node,
1184 "erase-value", -1);
1185 ofnode_for_each_subnode(node, flash_node) {
1186 const char *name = ofnode_get_name(node);
1187 enum ec_flash_region region;
1188
1189 if (0 == strcmp(name, "ro")) {
1190 region = EC_FLASH_REGION_RO;
1191 } else if (0 == strcmp(name, "rw")) {
1192 region = EC_FLASH_REGION_ACTIVE;
1193 } else if (0 == strcmp(name, "wp-ro")) {
1194 region = EC_FLASH_REGION_WP_RO;
1195 } else {
1196 debug("Unknown EC flash region name '%s'\n", name);
1197 return -1;
1198 }
1199
1200 if (ofnode_read_fmap_entry(node, &config->region[region])) {
1201 debug("Failed to decode flash region in chrome-ec'\n");
1202 return -1;
1203 }
1204 }
1205
1206 return 0;
1207}
1208
1209int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,
1210 int nmsgs)
1211{
1212 union {
1213 struct ec_params_i2c_passthru p;
1214 uint8_t outbuf[EC_PROTO2_MAX_PARAM_SIZE];
1215 } params;
1216 union {
1217 struct ec_response_i2c_passthru r;
1218 uint8_t inbuf[EC_PROTO2_MAX_PARAM_SIZE];
1219 } response;
1220 struct ec_params_i2c_passthru *p = ¶ms.p;
1221 struct ec_response_i2c_passthru *r = &response.r;
1222 struct ec_params_i2c_passthru_msg *msg;
1223 uint8_t *pdata, *read_ptr = NULL;
1224 int read_len;
1225 int size;
1226 int rv;
1227 int i;
1228
1229 p->port = port;
1230
1231 p->num_msgs = nmsgs;
1232 size = sizeof(*p) + p->num_msgs * sizeof(*msg);
1233
1234
1235 pdata = (uint8_t *)p + size;
1236
1237 read_len = 0;
1238 for (i = 0, msg = p->msg; i < nmsgs; i++, msg++, in++) {
1239 bool is_read = in->flags & I2C_M_RD;
1240
1241 msg->addr_flags = in->addr;
1242 msg->len = in->len;
1243 if (is_read) {
1244 msg->addr_flags |= EC_I2C_FLAG_READ;
1245 read_len += in->len;
1246 read_ptr = in->buf;
1247 if (sizeof(*r) + read_len > sizeof(response)) {
1248 puts("Read length too big for buffer\n");
1249 return -1;
1250 }
1251 } else {
1252 if (pdata - (uint8_t *)p + in->len > sizeof(params)) {
1253 puts("Params too large for buffer\n");
1254 return -1;
1255 }
1256 memcpy(pdata, in->buf, in->len);
1257 pdata += in->len;
1258 }
1259 }
1260
1261 rv = ec_command(dev, EC_CMD_I2C_PASSTHRU, 0, p, pdata - (uint8_t *)p,
1262 r, sizeof(*r) + read_len);
1263 if (rv < 0)
1264 return rv;
1265
1266
1267 if (r->i2c_status & EC_I2C_STATUS_ERROR) {
1268 printf("Transfer failed with status=0x%x\n", r->i2c_status);
1269 return -1;
1270 }
1271
1272 if (rv < sizeof(*r) + read_len) {
1273 puts("Truncated read response\n");
1274 return -1;
1275 }
1276
1277
1278 if (read_len)
1279 memcpy(read_ptr, r->data, read_len);
1280
1281 return 0;
1282}
1283
1284int cros_ec_check_feature(struct udevice *dev, int feature)
1285{
1286 struct ec_response_get_features r;
1287 int rv;
1288
1289 rv = ec_command(dev, EC_CMD_GET_FEATURES, 0, &r, sizeof(r), NULL, 0);
1290 if (rv)
1291 return rv;
1292
1293 if (feature >= 8 * sizeof(r.flags))
1294 return -1;
1295
1296 return r.flags[feature / 32] & EC_FEATURE_MASK_0(feature);
1297}
1298
1299
1300
1301
1302
1303static int cros_ec_uhepi_cmd(struct udevice *dev, uint mask, uint action,
1304 uint64_t *value)
1305{
1306 int ret;
1307 struct ec_params_host_event req;
1308 struct ec_response_host_event rsp;
1309
1310 req.action = action;
1311 req.mask_type = mask;
1312 if (action != EC_HOST_EVENT_GET)
1313 req.value = *value;
1314 else
1315 *value = 0;
1316 ret = ec_command(dev, EC_CMD_HOST_EVENT, 0, &req, sizeof(req), &rsp,
1317 sizeof(rsp));
1318
1319 if (action != EC_HOST_EVENT_GET)
1320 return ret;
1321 if (ret == 0)
1322 *value = rsp.value;
1323
1324 return ret;
1325}
1326
1327static int cros_ec_handle_non_uhepi_cmd(struct udevice *dev, uint hcmd,
1328 uint action, uint64_t *value)
1329{
1330 int ret = -1;
1331 struct ec_params_host_event_mask req;
1332 struct ec_response_host_event_mask rsp;
1333
1334 if (hcmd == INVALID_HCMD)
1335 return ret;
1336
1337 if (action != EC_HOST_EVENT_GET)
1338 req.mask = (uint32_t)*value;
1339 else
1340 *value = 0;
1341
1342 ret = ec_command(dev, hcmd, 0, &req, sizeof(req), &rsp, sizeof(rsp));
1343 if (action != EC_HOST_EVENT_GET)
1344 return ret;
1345 if (ret == 0)
1346 *value = rsp.mask;
1347
1348 return ret;
1349}
1350
1351bool cros_ec_is_uhepi_supported(struct udevice *dev)
1352{
1353#define UHEPI_SUPPORTED 1
1354#define UHEPI_NOT_SUPPORTED 2
1355 static int uhepi_support;
1356
1357 if (!uhepi_support) {
1358 uhepi_support = cros_ec_check_feature(dev,
1359 EC_FEATURE_UNIFIED_WAKE_MASKS) > 0 ? UHEPI_SUPPORTED :
1360 UHEPI_NOT_SUPPORTED;
1361 log_debug("Chrome EC: UHEPI %s\n",
1362 uhepi_support == UHEPI_SUPPORTED ? "supported" :
1363 "not supported");
1364 }
1365 return uhepi_support == UHEPI_SUPPORTED;
1366}
1367
1368static int cros_ec_get_mask(struct udevice *dev, uint type)
1369{
1370 u64 value = 0;
1371
1372 if (cros_ec_is_uhepi_supported(dev)) {
1373 cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_GET, &value);
1374 } else {
1375 assert(type < ARRAY_SIZE(event_map));
1376 cros_ec_handle_non_uhepi_cmd(dev, event_map[type].get_cmd,
1377 EC_HOST_EVENT_GET, &value);
1378 }
1379 return value;
1380}
1381
1382static int cros_ec_clear_mask(struct udevice *dev, uint type, u64 mask)
1383{
1384 if (cros_ec_is_uhepi_supported(dev))
1385 return cros_ec_uhepi_cmd(dev, type, EC_HOST_EVENT_CLEAR, &mask);
1386
1387 assert(type < ARRAY_SIZE(event_map));
1388
1389 return cros_ec_handle_non_uhepi_cmd(dev, event_map[type].clear_cmd,
1390 EC_HOST_EVENT_CLEAR, &mask);
1391}
1392
1393uint64_t cros_ec_get_events_b(struct udevice *dev)
1394{
1395 return cros_ec_get_mask(dev, EC_HOST_EVENT_B);
1396}
1397
1398int cros_ec_clear_events_b(struct udevice *dev, uint64_t mask)
1399{
1400 log_debug("Chrome EC: clear events_b mask to 0x%016llx\n", mask);
1401
1402 return cros_ec_clear_mask(dev, EC_HOST_EVENT_B, mask);
1403}
1404
1405int cros_ec_read_limit_power(struct udevice *dev, int *limit_powerp)
1406{
1407 struct ec_params_charge_state p;
1408 struct ec_response_charge_state r;
1409 int ret;
1410
1411 p.cmd = CHARGE_STATE_CMD_GET_PARAM;
1412 p.get_param.param = CS_PARAM_LIMIT_POWER;
1413 ret = ec_command(dev, EC_CMD_CHARGE_STATE, 0, &p, sizeof(p),
1414 &r, sizeof(r));
1415
1416
1417
1418
1419
1420 if (ret == -EC_RES_INVALID_PARAM || ret == -EC_RES_INVALID_COMMAND) {
1421 log_warning("PARAM_LIMIT_POWER not supported by EC\n");
1422 return -ENOSYS;
1423 }
1424
1425 if (ret != sizeof(r.get_param))
1426 return -EINVAL;
1427
1428 *limit_powerp = r.get_param.value;
1429 return 0;
1430}
1431
1432int cros_ec_config_powerbtn(struct udevice *dev, uint32_t flags)
1433{
1434 struct ec_params_config_power_button params;
1435 int ret;
1436
1437 params.flags = flags;
1438 ret = ec_command(dev, EC_CMD_CONFIG_POWER_BUTTON, 0,
1439 ¶ms, sizeof(params), NULL, 0);
1440 if (ret < 0)
1441 return ret;
1442
1443 return 0;
1444}
1445
1446int cros_ec_get_lid_shutdown_mask(struct udevice *dev)
1447{
1448 u32 mask;
1449 int ret;
1450
1451 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1452 &mask);
1453 if (ret < 0)
1454 return ret;
1455
1456 return !!(mask & EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED));
1457}
1458
1459int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)
1460{
1461 u32 mask;
1462 int ret;
1463
1464 ret = cros_ec_get_event_mask(dev, EC_CMD_HOST_EVENT_GET_SMI_MASK,
1465 &mask);
1466 if (ret < 0)
1467 return ret;
1468
1469
1470 if (enable)
1471 mask |= EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1472 else
1473 mask &= ~EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED);
1474
1475 ret = cros_ec_set_event_mask(dev, EC_CMD_HOST_EVENT_SET_SMI_MASK, mask);
1476 if (ret < 0)
1477 return ret;
1478
1479 printf("EC: %sabled lid close event\n", enable ? "en" : "dis");
1480 return 0;
1481}
1482
1483UCLASS_DRIVER(cros_ec) = {
1484 .id = UCLASS_CROS_EC,
1485 .name = "cros-ec",
1486 .per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
1487 .post_bind = dm_scan_fdt_dev,
1488 .flags = DM_UC_FLAG_ALLOC_PRIV_DMA,
1489};
1490