forked from FrameworkComputer/EmbeddedController
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusb_pe_drp_sm.c
8766 lines (7778 loc) · 244 KB
/
usb_pe_drp_sm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2019 The ChromiumOS Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "atomic.h"
#include "battery.h"
#include "battery_smart.h"
#include "builtin/assert.h"
#include "charge_manager.h"
#include "charge_state.h"
#include "common.h"
#include "console.h"
#include "dps.h"
#include "driver/tcpm/tcpm.h"
#include "ec_commands.h"
#include "hooks.h"
#include "host_command.h"
#include "power_button.h"
#include "stdbool.h"
#include "system.h"
#include "task.h"
#include "tcpm/tcpm.h"
#include "usb_charge.h"
#include "usb_common.h"
#include "usb_dp_alt_mode.h"
#include "usb_emsg.h"
#include "usb_mode.h"
#include "usb_mux.h"
#include "usb_pd.h"
#include "usb_pd_dpm_sm.h"
#include "usb_pd_policy.h"
#include "usb_pd_tcpm.h"
#include "usb_pd_timer.h"
#include "usb_pe_private.h"
#include "usb_pe_sm.h"
#include "usb_prl_sm.h"
#include "usb_sm.h"
#include "usb_tbt_alt_mode.h"
#include "usb_tc_sm.h"
#include "usbc_ppc.h"
#include "util.h"
/*
* USB Policy Engine Sink / Source module
*
* Based on Revision 3.0, Version 1.2 of
* the USB Power Delivery Specification.
*/
#ifdef CONFIG_COMMON_RUNTIME
#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ##args)
#define CPRINTS(format, args...) cprints(CC_USBPD, format, ##args)
#else
#define CPRINTF(format, args...)
#define CPRINTS(format, args...)
#endif
#define CPRINTF_LX(x, format, args...) \
do { \
if (pe_debug_level >= x) \
CPRINTF(format, ##args); \
} while (0)
#define CPRINTF_L1(format, args...) CPRINTF_LX(1, format, ##args)
#define CPRINTF_L2(format, args...) CPRINTF_LX(2, format, ##args)
#define CPRINTF_L3(format, args...) CPRINTF_LX(3, format, ##args)
#define CPRINTS_LX(x, format, args...) \
do { \
if (pe_debug_level >= x) \
CPRINTS(format, ##args); \
} while (0)
#define CPRINTS_L1(format, args...) CPRINTS_LX(1, format, ##args)
#define CPRINTS_L2(format, args...) CPRINTS_LX(2, format, ##args)
#define CPRINTS_L3(format, args...) CPRINTS_LX(3, format, ##args)
#define PE_SET_FN(port, _fn) \
atomic_or(ATOMIC_ELEM(pe[port].flags_a, (_fn)), ATOMIC_MASK(_fn))
#define PE_CLR_FN(port, _fn) \
atomic_clear_bits(ATOMIC_ELEM(pe[port].flags_a, (_fn)), \
ATOMIC_MASK(_fn))
#define PE_CHK_FN(port, _fn) \
(pe[port].flags_a[ATOMIC_ELEM(0, (_fn))] & ATOMIC_MASK(_fn))
#define PE_SET_FLAG(port, name) PE_SET_FN(port, (name##_FN))
#define PE_CLR_FLAG(port, name) PE_CLR_FN(port, (name##_FN))
#define PE_CHK_FLAG(port, name) PE_CHK_FN(port, (name##_FN))
/*
* TODO(b/229655319): support more than 32 bits
*/
#define PE_SET_MASK(port, mask) atomic_or(&pe[port].flags_a[0], (mask))
#define PE_CLR_MASK(port, mask) atomic_clear_bits(&pe[port].flags_a[0], (mask))
/*
* These macros SET, CLEAR, and CHECK, a DPM (Device Policy Manager)
* Request. The Requests are listed in usb_pe_sm.h.
*/
#define PE_SET_DPM_REQUEST(port, req) atomic_or(&pe[port].dpm_request, (req))
#define PE_CLR_DPM_REQUEST(port, req) \
atomic_clear_bits(&pe[port].dpm_request, (req))
#define PE_CHK_DPM_REQUEST(port, req) (pe[port].dpm_request & (req))
/* Message flags which should not persist on returning to ready state */
#define PE_MASK_READY_CLR \
(BIT(PE_FLAGS_LOCALLY_INITIATED_AMS_FN) | \
BIT(PE_FLAGS_MSG_DISCARDED_FN) | \
BIT(PE_FLAGS_VDM_REQUEST_TIMEOUT_FN) | \
BIT(PE_FLAGS_INTERRUPTIBLE_AMS_FN))
/*
* Combination to check whether a reply to a message was received. Our message
* should have sent (i.e. not been discarded) and a partner message is ready to
* process.
*
* When chunking is disabled (ex. for PD 2.0), these flags will set
* on the same run cycle. With chunking, received message will take an
* additional cycle to be flagged.
*/
#define PE_CHK_REPLY(port) \
(PE_CHK_FLAG(port, PE_FLAGS_MSG_RECEIVED) && \
!PE_CHK_FLAG(port, PE_FLAGS_MSG_DISCARDED))
/* 6.7.3 Hard Reset Counter */
#define N_HARD_RESET_COUNT 2
/* 6.7.4 Capabilities Counter */
#define N_CAPS_COUNT 25
/* 6.7.5 Discover Identity Counter */
/*
* NOTE: The Protocol Layer tries to send a message 3 time before giving up,
* so a Discover Identity SOP' message will be sent 3*6 = 18 times (slightly
* less than spec maximum of 20). This counter applies only to cable plug
* discovery.
*/
#define N_DISCOVER_IDENTITY_COUNT 6
/*
* It is permitted to send SOP' Discover Identity messages before a PD contract
* is in place. However, this is only beneficial if the cable powers up quickly
* solely from VCONN. Limit the number of retries without a contract to
* ensure we attempt some cable discovery after a contract is in place.
*/
#define N_DISCOVER_IDENTITY_PRECONTRACT_LIMIT 2
/*
* Once this limit of SOP' Discover Identity messages has been set, downgrade
* to PD 2.0 in case the cable is non-compliant about GoodCRC-ing higher
* revisions. This limit should be higher than the precontract limit.
*/
#define N_DISCOVER_IDENTITY_PD3_0_LIMIT 4
/*
* tDiscoverIdentity is only defined while an explicit contract is in place, so
* extend the interval between retries pre-contract.
*/
#define PE_T_DISCOVER_IDENTITY_NO_CONTRACT (200 * MSEC)
/*
* Only VCONN source can communicate with the cable plug. Hence, try VCONN swap
* 3 times before giving up.
*
* Note: This is not a part of power delivery specification
*/
#define N_VCONN_SWAP_COUNT 3
/*
* Counter to track how many times to attempt SRC to SNK PR swaps before giving
* up.
*
* Note: This is not a part of power delivery specification
*/
#define N_SNK_SRC_PR_SWAP_COUNT 5
/*
* ChromeOS policy:
* For PD2.0, We must be DFP before sending Discover Identity message
* to the port partner. Attempt to DR SWAP from UFP to DFP
* N_DR_SWAP_ATTEMPT_COUNT times before giving up on sending a
* Discover Identity message.
*/
#define N_DR_SWAP_ATTEMPT_COUNT 5
#define TIMER_DISABLED 0xffffffffffffffff /* Unreachable time in future */
/*
* The time that we allow the port partner to send any messages after an
* explicit contract is established. 200ms was chosen somewhat arbitrarily as
* it should be long enough for sources to decide to send a message if they were
* going to, but not so long that a "low power charger connected" notification
* would be shown in the chrome OS UI. Setting t0o large a delay can cause
* problems if the PD discovery time exceeds 1s (tAMETimeout)
*/
#define SRC_SNK_READY_HOLD_OFF_US (200 * MSEC)
/*
* Function pointer to a Structured Vendor Defined Message (SVDM) response
* function defined in the board's usb_pd_policy.c file.
*/
typedef int (*svdm_rsp_func)(int port, uint32_t *payload);
/* List of all Policy Engine level states */
enum usb_pe_state {
/* Super States */
PE_PRS_FRS_SHARED, /* pe-st0 */
PE_VDM_SEND_REQUEST, /* pe-st1 */
/* Normal States */
PE_SRC_STARTUP, /* pe-st2 */
PE_SRC_DISCOVERY, /* pe-st3 */
PE_SRC_SEND_CAPABILITIES, /* pe-st4 */
PE_SRC_NEGOTIATE_CAPABILITY, /* pe-st5 */
PE_SRC_TRANSITION_SUPPLY, /* pe-st6 */
PE_SRC_READY, /* pe-st7 */
PE_SRC_DISABLED, /* pe-st8 */
PE_SRC_CAPABILITY_RESPONSE, /* pe-st9 */
PE_SRC_HARD_RESET, /* pe-st10 */
PE_SRC_HARD_RESET_RECEIVED, /* pe-st11 */
PE_SRC_TRANSITION_TO_DEFAULT, /* pe-st12 */
PE_SNK_STARTUP, /* pe-st13 */
PE_SNK_DISCOVERY, /* pe-st14 */
PE_SNK_WAIT_FOR_CAPABILITIES, /* pe-st15 */
PE_SNK_EVALUATE_CAPABILITY, /* pe-st16 */
PE_SNK_SELECT_CAPABILITY, /* pe-st17 */
PE_SNK_READY, /* pe-st18 */
PE_SNK_HARD_RESET, /* pe-st19 */
PE_SNK_TRANSITION_TO_DEFAULT, /* pe-st20 */
PE_SNK_GIVE_SINK_CAP, /* pe-st21 */
PE_SNK_GET_SOURCE_CAP, /* pe-st22 */
PE_SNK_TRANSITION_SINK, /* pe-st23 */
PE_SEND_SOFT_RESET, /* pe-st24 */
PE_SOFT_RESET, /* pe-st25 */
PE_SEND_NOT_SUPPORTED, /* pe-st26 */
PE_SRC_PING, /* pe-st27 */
PE_DRS_EVALUATE_SWAP, /* pe-st28 */
PE_DRS_CHANGE, /* pe-st29 */
PE_DRS_SEND_SWAP, /* pe-st30 */
PE_PRS_SRC_SNK_EVALUATE_SWAP, /* pe-st31 */
PE_PRS_SRC_SNK_TRANSITION_TO_OFF, /* pe-st32 */
PE_PRS_SRC_SNK_ASSERT_RD, /* pe-st33 */
PE_PRS_SRC_SNK_WAIT_SOURCE_ON, /* pe-st34 */
PE_PRS_SRC_SNK_SEND_SWAP, /* pe-st35 */
PE_PRS_SNK_SRC_EVALUATE_SWAP, /* pe-st36 */
PE_PRS_SNK_SRC_TRANSITION_TO_OFF, /* pe-st37 */
PE_PRS_SNK_SRC_ASSERT_RP, /* pe-st38 */
PE_PRS_SNK_SRC_SOURCE_ON, /* pe-st39 */
PE_PRS_SNK_SRC_SEND_SWAP, /* pe-st40 */
PE_VCS_EVALUATE_SWAP, /* pe-st41 */
PE_VCS_SEND_SWAP, /* pe-st42 */
PE_VCS_WAIT_FOR_VCONN_SWAP, /* pe-st43 */
PE_VCS_TURN_ON_VCONN_SWAP, /* pe-st44 */
PE_VCS_TURN_OFF_VCONN_SWAP, /* pe-st45 */
PE_VCS_SEND_PS_RDY_SWAP, /* pe-st46 */
PE_VCS_CBL_SEND_SOFT_RESET, /* pe-st47 */
PE_VDM_IDENTITY_REQUEST_CBL, /* pe-st48 */
PE_INIT_PORT_VDM_IDENTITY_REQUEST, /* pe-st49 */
PE_INIT_VDM_SVIDS_REQUEST, /* pe-st50 */
PE_INIT_VDM_MODES_REQUEST, /* pe-st51 */
PE_VDM_REQUEST_DPM, /* pe-st52 */
PE_VDM_RESPONSE, /* pe-st53 */
PE_WAIT_FOR_ERROR_RECOVERY, /* pe-st54 */
PE_BIST_TX, /* pe-st55 */
PE_DEU_SEND_ENTER_USB, /* pe-st56 */
PE_DR_GET_SINK_CAP, /* pe-st57 */
PE_DR_SNK_GIVE_SOURCE_CAP, /* pe-st58 */
PE_DR_SRC_GET_SOURCE_CAP, /* pe-st59 */
/* PD3.0 only states below here*/
/* UFP Data Reset States */
PE_UDR_SEND_DATA_RESET, /* pe-st60 */
PE_UDR_DATA_RESET_RECEIVED, /* pe-st61 */
PE_UDR_TURN_OFF_VCONN, /* pe-st62 */
PE_UDR_SEND_PS_RDY, /* pe-st63 */
PE_UDR_WAIT_FOR_DATA_RESET_COMPLETE, /* pe-st64 */
/* DFP Data Reset States */
PE_DDR_SEND_DATA_RESET, /* pe-st65 */
PE_DDR_DATA_RESET_RECEIVED, /* pe-st66 */
PE_DDR_WAIT_FOR_VCONN_OFF, /* pe-st67 */
PE_DDR_PERFORM_DATA_RESET, /* pe-st68 */
PE_FRS_SNK_SRC_START_AMS, /* pe-st69 */
PE_GIVE_BATTERY_CAP, /* pe-st70 */
PE_GIVE_BATTERY_STATUS, /* pe-st71 */
PE_GIVE_STATUS, /* pe-st72 */
PE_SEND_ALERT, /* pe-st73 */
PE_ALERT_RECEIVED, /* pe-st74 */
PE_SRC_CHUNK_RECEIVED, /* pe-st75 */
PE_SNK_CHUNK_RECEIVED, /* pe-st76 */
PE_VCS_FORCE_VCONN, /* pe-st77 */
PE_GET_REVISION, /* pe-st78 */
/* EPR states */
PE_SNK_SEND_EPR_MODE_ENTRY,
PE_SNK_EPR_MODE_ENTRY_WAIT_FOR_RESPONSE,
PE_SNK_EPR_KEEP_ALIVE,
PE_SNK_SEND_EPR_MODE_EXIT,
PE_SNK_EPR_MODE_EXIT_RECEIVED,
};
/*
* The result of a previously sent DPM request; used by PE_VDM_SEND_REQUEST to
* indicate to child states when they need to handle a response.
*/
enum vdm_response_result {
/* The parent state is still waiting for a response. */
VDM_RESULT_WAITING,
/*
* The parent state parsed a message, but there is nothing for the child
* to handle, e.g. BUSY.
*/
VDM_RESULT_NO_ACTION,
/* The parent state processed an ACK response. */
VDM_RESULT_ACK,
/*
* The parent state processed a NAK-like response (NAK, Not Supported,
* or response timeout.
*/
VDM_RESULT_NAK,
};
/* Forward declare the full list of states. This is indexed by usb_pe_state */
static const struct usb_state pe_states[];
/*
* We will use DEBUG LABELS if we will be able to print (COMMON RUNTIME)
* and either CONFIG_USB_PD_DEBUG_LEVEL is not defined (no override) or
* we are overriding and the level is not DISABLED.
*
* If we can't print or the CONFIG_USB_PD_DEBUG_LEVEL is defined to be 0
* then the DEBUG LABELS will be removed from the build.
*/
#if defined(CONFIG_COMMON_RUNTIME) && (!defined(CONFIG_USB_PD_DEBUG_LEVEL) || \
(CONFIG_USB_PD_DEBUG_LEVEL > 0))
#define USB_PD_DEBUG_LABELS
#endif
/* List of human readable state names for console debugging */
__maybe_unused static __const_data const char *const pe_state_names[] = {
/* Super States */
#ifdef CONFIG_USB_PD_REV30
[PE_PRS_FRS_SHARED] = "SS:PE_PRS_FRS_SHARED",
#endif
[PE_VDM_SEND_REQUEST] = "SS:PE_VDM_Send_Request",
/* Normal States */
[PE_SRC_STARTUP] = "PE_SRC_Startup",
[PE_SRC_DISCOVERY] = "PE_SRC_Discovery",
[PE_SRC_SEND_CAPABILITIES] = "PE_SRC_Send_Capabilities",
[PE_SRC_NEGOTIATE_CAPABILITY] = "PE_SRC_Negotiate_Capability",
[PE_SRC_TRANSITION_SUPPLY] = "PE_SRC_Transition_Supply",
[PE_SRC_READY] = "PE_SRC_Ready",
[PE_SRC_DISABLED] = "PE_SRC_Disabled",
[PE_SRC_CAPABILITY_RESPONSE] = "PE_SRC_Capability_Response",
[PE_SRC_HARD_RESET] = "PE_SRC_Hard_Reset",
[PE_SRC_HARD_RESET_RECEIVED] = "PE_SRC_Hard_Reset_Received",
[PE_SRC_TRANSITION_TO_DEFAULT] = "PE_SRC_Transition_to_default",
[PE_SNK_STARTUP] = "PE_SNK_Startup",
[PE_SNK_DISCOVERY] = "PE_SNK_Discovery",
[PE_SNK_WAIT_FOR_CAPABILITIES] = "PE_SNK_Wait_for_Capabilities",
[PE_SNK_EVALUATE_CAPABILITY] = "PE_SNK_Evaluate_Capability",
[PE_SNK_SELECT_CAPABILITY] = "PE_SNK_Select_Capability",
[PE_SNK_READY] = "PE_SNK_Ready",
[PE_SNK_HARD_RESET] = "PE_SNK_Hard_Reset",
[PE_SNK_TRANSITION_TO_DEFAULT] = "PE_SNK_Transition_to_default",
[PE_SNK_GIVE_SINK_CAP] = "PE_SNK_Give_Sink_Cap",
[PE_SNK_GET_SOURCE_CAP] = "PE_SNK_Get_Source_Cap",
[PE_SNK_TRANSITION_SINK] = "PE_SNK_Transition_Sink",
[PE_SEND_SOFT_RESET] = "PE_Send_Soft_Reset",
[PE_SOFT_RESET] = "PE_Soft_Reset",
[PE_SEND_NOT_SUPPORTED] = "PE_Send_Not_Supported",
[PE_SRC_PING] = "PE_SRC_Ping",
[PE_DRS_EVALUATE_SWAP] = "PE_DRS_Evaluate_Swap",
[PE_DRS_CHANGE] = "PE_DRS_Change",
[PE_DRS_SEND_SWAP] = "PE_DRS_Send_Swap",
[PE_PRS_SRC_SNK_EVALUATE_SWAP] = "PE_PRS_SRC_SNK_Evaluate_Swap",
[PE_PRS_SRC_SNK_TRANSITION_TO_OFF] = "PE_PRS_SRC_SNK_Transition_To_Off",
[PE_PRS_SRC_SNK_ASSERT_RD] = "PE_PRS_SRC_SNK_Assert_Rd",
[PE_PRS_SRC_SNK_WAIT_SOURCE_ON] = "PE_PRS_SRC_SNK_Wait_Source_On",
[PE_PRS_SRC_SNK_SEND_SWAP] = "PE_PRS_SRC_SNK_Send_Swap",
[PE_PRS_SNK_SRC_EVALUATE_SWAP] = "PE_PRS_SNK_SRC_Evaluate_Swap",
[PE_PRS_SNK_SRC_TRANSITION_TO_OFF] = "PE_PRS_SNK_SRC_Transition_To_Off",
[PE_PRS_SNK_SRC_ASSERT_RP] = "PE_PRS_SNK_SRC_Assert_Rp",
[PE_PRS_SNK_SRC_SOURCE_ON] = "PE_PRS_SNK_SRC_Source_On",
[PE_PRS_SNK_SRC_SEND_SWAP] = "PE_PRS_SNK_SRC_Send_Swap",
#ifdef CONFIG_USBC_VCONN
[PE_VCS_EVALUATE_SWAP] = "PE_VCS_Evaluate_Swap",
[PE_VCS_SEND_SWAP] = "PE_VCS_Send_Swap",
[PE_VCS_WAIT_FOR_VCONN_SWAP] = "PE_VCS_Wait_For_Vconn_Swap",
[PE_VCS_TURN_ON_VCONN_SWAP] = "PE_VCS_Turn_On_Vconn_Swap",
[PE_VCS_TURN_OFF_VCONN_SWAP] = "PE_VCS_Turn_Off_Vconn_Swap",
[PE_VCS_SEND_PS_RDY_SWAP] = "PE_VCS_Send_Ps_Rdy_Swap",
[PE_VCS_CBL_SEND_SOFT_RESET] = "PE_VCS_CBL_Send_Soft_Reset",
#endif
[PE_VDM_IDENTITY_REQUEST_CBL] = "PE_VDM_Identity_Request_Cbl",
[PE_INIT_PORT_VDM_IDENTITY_REQUEST] =
"PE_INIT_PORT_VDM_Identity_Request",
[PE_INIT_VDM_SVIDS_REQUEST] = "PE_INIT_VDM_SVIDs_Request",
[PE_INIT_VDM_MODES_REQUEST] = "PE_INIT_VDM_Modes_Request",
[PE_VDM_REQUEST_DPM] = "PE_VDM_Request_DPM",
[PE_VDM_RESPONSE] = "PE_VDM_Response",
[PE_WAIT_FOR_ERROR_RECOVERY] = "PE_Wait_For_Error_Recovery",
[PE_BIST_TX] = "PE_Bist_TX",
[PE_DEU_SEND_ENTER_USB] = "PE_DEU_Send_Enter_USB",
[PE_DR_GET_SINK_CAP] = "PE_DR_Get_Sink_Cap",
[PE_DR_SNK_GIVE_SOURCE_CAP] = "PE_DR_SNK_Give_Source_Cap",
[PE_DR_SRC_GET_SOURCE_CAP] = "PE_DR_SRC_Get_Source_Cap",
/* PD3.0 only states below here*/
#ifdef CONFIG_USB_PD_REV30
[PE_FRS_SNK_SRC_START_AMS] = "PE_FRS_SNK_SRC_Start_Ams",
[PE_GET_REVISION] = "PE_Get_Revision",
#ifdef CONFIG_USB_PD_EXTENDED_MESSAGES
[PE_GIVE_BATTERY_CAP] = "PE_Give_Battery_Cap",
[PE_GIVE_BATTERY_STATUS] = "PE_Give_Battery_Status",
[PE_GIVE_STATUS] = "PE_Give_Status",
[PE_SEND_ALERT] = "PE_Send_Alert",
[PE_ALERT_RECEIVED] = "PE_Alert_Received",
#else
[PE_SRC_CHUNK_RECEIVED] = "PE_SRC_Chunk_Received",
[PE_SNK_CHUNK_RECEIVED] = "PE_SNK_Chunk_Received",
#endif
#ifdef CONFIG_USBC_VCONN
[PE_VCS_FORCE_VCONN] = "PE_VCS_Force_Vconn",
#endif
#ifdef CONFIG_USB_PD_DATA_RESET_MSG
[PE_UDR_SEND_DATA_RESET] = "PE_UDR_Send_Data_Reset",
[PE_UDR_DATA_RESET_RECEIVED] = "PE_UDR_Data_Reset_Received",
[PE_UDR_TURN_OFF_VCONN] = "PE_UDR_Turn_Off_VCONN",
[PE_UDR_SEND_PS_RDY] = "PE_UDR_Send_Ps_Rdy",
[PE_UDR_WAIT_FOR_DATA_RESET_COMPLETE] =
"PE_UDR_Wait_For_Data_Reset_Complete",
[PE_DDR_SEND_DATA_RESET] = "PE_DDR_Send_Data_Reset",
[PE_DDR_DATA_RESET_RECEIVED] = "PE_DDR_Data_Reset_Received",
[PE_DDR_WAIT_FOR_VCONN_OFF] = "PE_DDR_Wait_For_VCONN_Off",
[PE_DDR_PERFORM_DATA_RESET] = "PE_DDR_Perform_Data_Reset",
#endif /* CONFIG_USB_PD_DATA_RESET_MSG */
#ifdef CONFIG_USB_PD_EPR
[PE_SNK_SEND_EPR_MODE_ENTRY] = "PE_SNK_Send_EPR_Mode_Entry",
[PE_SNK_EPR_MODE_ENTRY_WAIT_FOR_RESPONSE] =
"PE_SNK_EPR_Mode_Entry_Wait_For_Response",
[PE_SNK_EPR_KEEP_ALIVE] = "PE_SNK_EPR_Keep_Alive",
[PE_SNK_SEND_EPR_MODE_EXIT] = "PE_SNK_Send_EPR_Mode_Exit",
[PE_SNK_EPR_MODE_EXIT_RECEIVED] = "PE_SNK_EPR_Mode_Exit_Received",
#endif
#endif /* CONFIG_USB_PD_REV30 */
};
#ifndef CONFIG_USBC_VCONN
GEN_NOT_SUPPORTED(PE_VCS_EVALUATE_SWAP);
#define PE_VCS_EVALUATE_SWAP PE_VCS_EVALUATE_SWAP_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_VCS_SEND_SWAP);
#define PE_VCS_SEND_SWAP PE_VCS_SEND_SWAP_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_VCS_WAIT_FOR_VCONN_SWAP);
#define PE_VCS_WAIT_FOR_VCONN_SWAP PE_VCS_WAIT_FOR_VCONN_SWAP_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_VCS_TURN_ON_VCONN_SWAP);
#define PE_VCS_TURN_ON_VCONN_SWAP PE_VCS_TURN_ON_VCONN_SWAP_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_VCS_TURN_OFF_VCONN_SWAP);
#define PE_VCS_TURN_OFF_VCONN_SWAP PE_VCS_TURN_OFF_VCONN_SWAP_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_VCS_SEND_PS_RDY_SWAP);
#define PE_VCS_SEND_PS_RDY_SWAP PE_VCS_SEND_PS_RDY_SWAP_NOT_SUPPORTED
#endif /* CONFIG_USBC_VCONN */
#ifndef CONFIG_USB_PD_REV30
GEN_NOT_SUPPORTED(PE_FRS_SNK_SRC_START_AMS);
#define PE_FRS_SNK_SRC_START_AMS PE_FRS_SNK_SRC_START_AMS_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_PRS_FRS_SHARED);
#define PE_PRS_FRS_SHARED PE_PRS_FRS_SHARED_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_SRC_CHUNK_RECEIVED);
#define PE_SRC_CHUNK_RECEIVED PE_SRC_CHUNK_RECEIVED_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_SNK_CHUNK_RECEIVED);
#define PE_SNK_CHUNK_RECEIVED PE_SNK_CHUNK_RECEIVED_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_GET_REVISION);
#define PE_GET_REVISION PE_GET_REVISION_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_ALERT_RECEIVED);
#define PE_ALERT_RECEIVED PE_ALERT_RECEIVED_NOT_SUPPORTED
#endif /* CONFIG_USB_PD_REV30 */
#if !defined(CONFIG_USBC_VCONN) || !defined(CONFIG_USB_PD_REV30)
GEN_NOT_SUPPORTED(PE_VCS_FORCE_VCONN);
#define PE_VCS_FORCE_VCONN PE_VCS_FORCE_VCONN_NOT_SUPPORTED
#endif
#ifndef CONFIG_USB_PD_EXTENDED_MESSAGES
GEN_NOT_SUPPORTED(PE_GIVE_BATTERY_CAP);
#define PE_GIVE_BATTERY_CAP PE_GIVE_BATTERY_CAP_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_GIVE_BATTERY_STATUS);
#define PE_GIVE_BATTERY_STATUS PE_GIVE_BATTERY_STATUS_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_GIVE_STATUS);
#define PE_GIVE_STATUS PE_GIVE_STATUS_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_SEND_ALERT);
#define PE_SEND_ALERT PE_SEND_ALERT_NOT_SUPPORTED
#endif /* CONFIG_USB_PD_EXTENDED_MESSAGES */
#ifdef CONFIG_USB_PD_EXTENDED_MESSAGES
GEN_NOT_SUPPORTED(PE_SRC_CHUNK_RECEIVED);
#define PE_SRC_CHUNK_RECEIVED PE_SRC_CHUNK_RECEIVED_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_SNK_CHUNK_RECEIVED);
#define PE_SNK_CHUNK_RECEIVED PE_SNK_CHUNK_RECEIVED_NOT_SUPPORTED
#endif /* CONFIG_USB_PD_EXTENDED_MESSAGES */
#ifndef CONFIG_USB_PD_DATA_RESET_MSG
GEN_NOT_SUPPORTED(PE_UDR_SEND_DATA_RESET);
#define PE_UDR_SEND_DATA_RESET PE_UDR_SEND_DATA_RESET_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_UDR_DATA_RESET_RECEIVED);
#define PE_UDR_DATA_RESET_RECEIVED PE_UDR_DATA_RESET_RECEIVED_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_UDR_TURN_OFF_VCONN);
#define PE_UDR_TURN_OFF_VCONN PE_UDR_TURN_OFF_VCONN_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_UDR_SEND_PS_RDY);
#define PE_UDR_SEND_PS_RDY PE_UDR_SEND_PS_RDY_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_UDR_WAIT_FOR_DATA_RESET_COMPLETE);
#define PE_UDR_WAIT_FOR_DATA_RESET_COMPLETE \
PE_UDR_WAIT_FOR_DATA_RESET_COMPLETE_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_DDR_SEND_DATA_RESET);
#define PE_DDR_SEND_DATA_RESET PE_DDR_SEND_DATA_RESET_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_DDR_DATA_RESET_RECEIVED);
#define PE_DDR_DATA_RESET_RECEIVED PE_DDR_DATA_RESET_RECEIVED_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_DDR_WAIT_FOR_VCONN_OFF);
#define PE_DDR_WAIT_FOR_VCONN_OFF PE_DDR_WAIT_FOR_VCONN_OFF_NOT_SUPPORTED
GEN_NOT_SUPPORTED(PE_DDR_PERFORM_DATA_RESET);
#define PE_DDR_PERFORM_DATA_RESET PE_DDR_PERFORM_DATA_RESET_NOT_SUPPORTED
#endif /* CONFIG_USB_PD_DATA_RESET_MSG */
static enum sm_local_state local_state[CONFIG_USB_PD_PORT_MAX_COUNT];
/*
* Common message send checking
*
* PE_MSG_SEND_PENDING: A message has been requested to be sent. It has
* not been GoodCRCed or Discarded.
* PE_MSG_SEND_COMPLETED: The message that was requested has been sent.
* This will only be returned one time and any other
* request for message send status will just return
* PE_MSG_SENT. This message actually includes both
* The COMPLETED and the SENT bit for easier checking.
* NOTE: PE_MSG_SEND_COMPLETED will only be returned
* a single time, directly after TX_COMPLETE.
* PE_MSG_SENT: The message that was requested to be sent has
* successfully been transferred to the partner.
* PE_MSG_DISCARDED: The message that was requested to be sent was
* discarded. The partner did not receive it.
* NOTE: PE_MSG_DISCARDED will only be returned
* one time and it is up to the caller to process
* what ever is needed to handle the Discard.
* PE_MSG_DPM_DISCARDED: The message that was requested to be sent was
* discarded and an active DRP_REQUEST was active.
* The DRP_REQUEST that was current will be moved
* back to the drp_requests so it can be performed
* later if needed.
* NOTE: PE_MSG_DPM_DISCARDED will only be returned
* one time and it is up to the caller to process
* what ever is needed to handle the Discard.
*/
enum pe_msg_check {
PE_MSG_SEND_PENDING = BIT(0),
PE_MSG_SENT = BIT(1),
PE_MSG_DISCARDED = BIT(2),
PE_MSG_SEND_COMPLETED = BIT(3) | PE_MSG_SENT,
PE_MSG_DPM_DISCARDED = BIT(4) | PE_MSG_DISCARDED,
};
static void pe_sender_response_msg_entry(const int port);
static enum pe_msg_check pe_sender_response_msg_run(const int port);
static void pe_sender_response_msg_exit(const int port);
/* Debug log level - higher number == more log */
#ifdef CONFIG_USB_PD_DEBUG_LEVEL
static const enum debug_level pe_debug_level = CONFIG_USB_PD_DEBUG_LEVEL;
#elif defined(CONFIG_USB_PD_INITIAL_DEBUG_LEVEL)
static enum debug_level pe_debug_level = CONFIG_USB_PD_INITIAL_DEBUG_LEVEL;
#else
static enum debug_level pe_debug_level = DEBUG_LEVEL_1;
#endif
/*
* Policy Engine State Machine Object
*/
static struct policy_engine {
/* state machine context */
struct sm_ctx ctx;
/* current port power role (SOURCE or SINK) */
enum pd_power_role power_role;
/* current port data role (DFP or UFP) */
enum pd_data_role data_role;
/* state machine flags */
ATOMIC_DEFINE(flags_a, PE_FLAGS_COUNT);
/* Device Policy Manager Request */
atomic_t dpm_request;
uint32_t dpm_curr_request;
/* last requested voltage PDO index */
int requested_idx;
/*
* Port events - PD_STATUS_EVENT_* values
* Set from PD task but may be cleared by host command
*/
atomic_t events;
/* port address where soft resets are sent */
enum tcpci_msg_type soft_reset_sop;
/* Current limit / voltage based on the last request message */
uint32_t curr_limit;
uint32_t supply_voltage;
/* PD_VDO_INVALID is used when there is an invalid VDO */
int32_t ama_vdo;
int32_t vpd_vdo;
/* Alternate mode discovery results */
struct pd_discovery discovery[DISCOVERY_TYPE_COUNT];
/* Partner type to send */
enum tcpci_msg_type tx_type;
/* VDM - used to send information to shared VDM Request state */
uint32_t vdm_cnt;
uint32_t vdm_data[VDO_HDR_SIZE + VDO_MAX_SIZE];
uint8_t vdm_ack_min_data_objects;
/* ADO - Used to store information about alert messages */
uint32_t ado;
mutex_t ado_lock;
/* Counters */
/*
* This counter is used to retry the Hard Reset whenever there is no
* response from the remote device.
*/
uint32_t hard_reset_counter;
/*
* This counter is used to count the number of Source_Capabilities
* Messages which have been sent by a Source at power up or after a
* Hard Reset.
*/
uint32_t caps_counter;
/*
* This counter maintains a count of Discover Identity Messages sent
* to a cable. If no GoodCRC messages are received after
* nDiscoverIdentityCount, the port shall not send any further
* SOP'/SOP'' messages.
*/
uint32_t discover_identity_counter;
/*
* For PD2.0, we need to be a DFP before sending a discovery identity
* message to our port partner. This counter keeps track of how
* many attempts to DR SWAP from UFP to DFP.
*/
uint32_t dr_swap_attempt_counter;
/*
* This counter tracks how many PR Swap messages are sent when the
* partner responds with a Wait message. Only used during SRC to SNK
* PR swaps
*/
uint8_t src_snk_pr_swap_counter;
/*
* This counter maintains a count of VCONN swap requests. If VCONN swap
* isn't successful after N_VCONN_SWAP_COUNT, the port calls
* dpm_vdm_naked().
*/
uint8_t vconn_swap_counter;
/* Last received source cap */
uint32_t src_caps[PDO_MAX_OBJECTS];
int src_cap_cnt; /* -1 on error retrieving source caps */
/* Last received sink cap */
uint32_t snk_caps[PDO_MAX_OBJECTS];
int snk_cap_cnt;
/* Last received Revision Message Data Object (RMDO) from the partner */
struct rmdo partner_rmdo;
} pe[CONFIG_USB_PD_PORT_MAX_COUNT];
test_export_static enum usb_pe_state get_state_pe(const int port);
test_export_static void set_state_pe(const int port,
const enum usb_pe_state new_state);
static void pe_set_dpm_curr_request(const int port, const int request);
/*
* The spec. revision is used to index into this array.
* PD 1.0 (VDO 1.0) - return VDM_VER10
* PD 2.0 (VDO 1.0) - return VDM_VER10
* PD 3.0 (VDO 2.0) - return VDM_VER20
*/
static const uint8_t vdo_ver[] = {
[PD_REV10] = VDM_VER10,
[PD_REV20] = VDM_VER10,
[PD_REV30] = VDM_VER20,
};
int pd_get_rev(int port, enum tcpci_msg_type type)
{
return prl_get_rev(port, type);
}
int pd_get_vdo_ver(int port, enum tcpci_msg_type type)
{
enum pd_rev_type rev = prl_get_rev(port, type);
if (rev < PD_REV30)
return vdo_ver[rev];
else
return VDM_VER20;
}
static void pe_set_ready_state(int port)
{
if (pe[port].power_role == PD_ROLE_SOURCE)
set_state_pe(port, PE_SRC_READY);
else
set_state_pe(port, PE_SNK_READY);
}
static void pe_set_hard_reset(int port)
{
if (pe[port].power_role == PD_ROLE_SOURCE)
set_state_pe(port, PE_SRC_HARD_RESET);
else
set_state_pe(port, PE_SNK_HARD_RESET);
}
static inline void send_data_msg(int port, enum tcpci_msg_type type,
enum pd_data_msg_type msg)
{
/* Clear any previous TX status before sending a new message */
PE_CLR_FLAG(port, PE_FLAGS_TX_COMPLETE);
prl_send_data_msg(port, type, msg);
}
static __maybe_unused inline void
send_ext_data_msg(int port, enum tcpci_msg_type type, enum pd_ext_msg_type msg)
{
/* Clear any previous TX status before sending a new message */
PE_CLR_FLAG(port, PE_FLAGS_TX_COMPLETE);
prl_send_ext_data_msg(port, type, msg);
}
static inline void send_ctrl_msg(int port, enum tcpci_msg_type type,
enum pd_ctrl_msg_type msg)
{
/* Clear any previous TX status before sending a new message */
PE_CLR_FLAG(port, PE_FLAGS_TX_COMPLETE);
prl_send_ctrl_msg(port, type, msg);
}
/* Set both the SOP' and SOP'' revisions to the given value */
static void set_cable_rev(int port, enum pd_rev_type rev)
{
prl_set_rev(port, TCPCI_MSG_SOP_PRIME, rev);
prl_set_rev(port, TCPCI_MSG_SOP_PRIME_PRIME, rev);
}
/* Initialize the cable revision based only on the partner SOP revision */
static void init_cable_rev(int port)
{
/*
* If port partner runs PD 2.0, cable communication must
* also be PD 2.0
*/
if (prl_get_rev(port, TCPCI_MSG_SOP) == PD_REV20) {
/*
* If the cable supports PD 3.0, but the port partner supports
* PD 2.0, redo the cable discover with PD 2.0
*/
if (prl_get_rev(port, TCPCI_MSG_SOP_PRIME) == PD_REV30 &&
pd_get_identity_discovery(port, TCPCI_MSG_SOP_PRIME) ==
PD_DISC_COMPLETE) {
pd_set_identity_discovery(port, TCPCI_MSG_SOP_PRIME,
PD_DISC_NEEDED);
}
set_cable_rev(port, PD_REV20);
}
}
/* Compile-time insurance to ensure this code does not call into prl directly */
#define prl_send_data_msg DO_NOT_USE
#define prl_send_ext_data_msg DO_NOT_USE
#define prl_send_ctrl_msg DO_NOT_USE
static void pe_init(int port)
{
memset(&pe[port].flags_a, 0, sizeof(pe[port].flags_a));
pe[port].dpm_request = 0;
pe[port].dpm_curr_request = 0;
pd_timer_disable_range(port, PE_TIMER_RANGE);
pe[port].data_role = pd_get_data_role(port);
pe[port].tx_type = TCPCI_MSG_INVALID;
pe[port].events = 0;
tc_pd_connection(port, 0);
if (pd_get_power_role(port) == PD_ROLE_SOURCE)
set_state_pe(port, PE_SRC_STARTUP);
else
set_state_pe(port, PE_SNK_STARTUP);
}
int pe_is_running(int port)
{
return local_state[port] == SM_RUN;
}
bool pe_in_frs_mode(int port)
{
return PE_CHK_FLAG(port, PE_FLAGS_FAST_ROLE_SWAP_PATH);
}
bool pe_in_local_ams(int port)
{
return !!PE_CHK_FLAG(port, PE_FLAGS_LOCALLY_INITIATED_AMS);
}
void pe_set_debug_level(enum debug_level debug_level)
{
#ifndef CONFIG_USB_PD_DEBUG_LEVEL
pe_debug_level = debug_level;
#endif
}
void pe_run(int port, int evt, int en)
{
switch (local_state[port]) {
case SM_PAUSED:
if (!en)
break;
__fallthrough;
case SM_INIT:
pe_init(port);
local_state[port] = SM_RUN;
__fallthrough;
case SM_RUN:
if (!en) {
local_state[port] = SM_PAUSED;
/*
* While we are paused, exit all states and wait until
* initialized again.
*/
set_state(port, &pe[port].ctx, NULL);
break;
}
/*
* 8.3.3.3.8 PE_SNK_Hard_Reset State
* The Policy Engine Shall transition to the PE_SNK_Hard_Reset
* state from any state when:
* - Hard Reset request from Device Policy Manager
*
* USB PD specification clearly states that we should go to
* PE_SNK_Hard_Reset from ANY state (including states in which
* port is source) when DPM requests that. This can lead to
* execute Hard Reset path for sink when actually our power
* role is source. In our implementation we will choose Hard
* Reset path depending on current power role.
*/
if (PE_CHK_DPM_REQUEST(port, DPM_REQUEST_HARD_RESET_SEND)) {
/*
* If a hard reset condition came up during FRS, we must
* go into ErrorRecovery. Performing a hard reset could
* leave us assuming our own FRS Vbus is coming from the
* partner and leave the port stuck as Attached.SNK
*/
if (pe_in_frs_mode(port)) {
PE_CLR_DPM_REQUEST(port,
DPM_REQUEST_HARD_RESET_SEND);
set_state_pe(port, PE_WAIT_FOR_ERROR_RECOVERY);
} else {
pe_set_dpm_curr_request(
port, DPM_REQUEST_HARD_RESET_SEND);
pe_set_hard_reset(port);
}
}
/*
* Check for Fast Role Swap signal
* This is not a typical pattern for adding state changes.
* I added this here because FRS SIGNALED can happen at any
* state once we are listening for the signal and we want to
* make sure to handle it immediately.
*/
if (IS_ENABLED(CONFIG_USB_PD_REV30) &&
PE_CHK_FLAG(port, PE_FLAGS_FAST_ROLE_SWAP_SIGNALED)) {
PE_CLR_FLAG(port, PE_FLAGS_FAST_ROLE_SWAP_SIGNALED);
set_state_pe(port, PE_FRS_SNK_SRC_START_AMS);
}
/* Run state machine */
run_state(port, &pe[port].ctx);
break;
}
}
int pe_is_explicit_contract(int port)
{
return PE_CHK_FLAG(port, PE_FLAGS_EXPLICIT_CONTRACT);
}
void pe_message_received(int port)
{
/* This should only be called from the PD task */
assert(port == TASK_ID_TO_PD_PORT(task_get_current()));
PE_SET_FLAG(port, PE_FLAGS_MSG_RECEIVED);
task_wake(PD_PORT_TO_TASK_ID(port));
}
void pe_hard_reset_sent(int port)
{
/* This should only be called from the PD task */
assert(port == TASK_ID_TO_PD_PORT(task_get_current()));
PE_CLR_FLAG(port, PE_FLAGS_HARD_RESET_PENDING);
}
void pe_got_hard_reset(int port)
{
/* This should only be called from the PD task */
assert(port == TASK_ID_TO_PD_PORT(task_get_current()));
/*
* If we're in the middle of an FRS, any error should cause us to follow
* the ErrorRecovery path
*/
if (pe_in_frs_mode(port)) {
set_state_pe(port, PE_WAIT_FOR_ERROR_RECOVERY);
return;
}
/*
* Transition from any state to the PE_SRC_Hard_Reset_Received or
* PE_SNK_Transition_to_default state when:
* 1) Hard Reset Signaling is detected.
*/
pe[port].power_role = pd_get_power_role(port);
/* Exit BIST Test mode, in case the TCPC entered it. */
tcpc_set_bist_test_mode(port, false);
if (pe[port].power_role == PD_ROLE_SOURCE)
set_state_pe(port, PE_SRC_HARD_RESET_RECEIVED);
else
set_state_pe(port, PE_SNK_TRANSITION_TO_DEFAULT);
}
#ifdef CONFIG_USB_PD_REV30
/*
* pd_got_frs_signal
*
* Called by the handler that detects the FRS signal in order to
* switch PE states to complete the FRS that the hardware has
* started.
*
* If the PE is not running, generate an error recovery to turn off
* Vbus and get the port back into a known state.
*/
void pd_got_frs_signal(int port)
{
if (pe_is_running(port))
PE_SET_FLAG(port, PE_FLAGS_FAST_ROLE_SWAP_SIGNALED);
else
pd_set_error_recovery(port);
task_wake(PD_PORT_TO_TASK_ID(port));
}
#endif /* CONFIG_USB_PD_REV30 */
/*
* PE_Set_FRS_Enable
*
* This function should be called every time an explicit contract
* is disabled, to disable FRS.
*
* Enabling an explicit contract is not enough to enable FRS, it
* also requires a Sink Capability power requirement from a Source
* that supports FRS so we can determine if this is something we
* can handle.
*/
static void pe_set_frs_enable(int port, int enable)
{
int current = PE_CHK_FLAG(port, PE_FLAGS_FAST_ROLE_SWAP_ENABLED);
/* This should only be called from the PD task */
if (!IS_ENABLED(TEST_BUILD))
assert(port == TASK_ID_TO_PD_PORT(task_get_current()));
if (!IS_ENABLED(CONFIG_USB_PD_FRS) || !IS_ENABLED(CONFIG_USB_PD_REV30))
return;
/* Request an FRS change, only if the state has changed */
if (!!current == !!enable)
return;
pd_set_frs_enable(port, enable);
if (enable) {
int curr_limit = *pd_get_snk_caps(port) &
PDO_FIXED_FRS_CURR_MASK;
typec_select_src_current_limit_rp(
port, curr_limit == PDO_FIXED_FRS_CURR_3A0_AT_5V ?