-
Notifications
You must be signed in to change notification settings - Fork 62
/
usb_pd_protocol.c
5392 lines (4888 loc) · 148 KB
/
usb_pd_protocol.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 2014 The ChromiumOS Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* LCOV_EXCL_START - TCPMv1 is difficult to meaningfully test: b/304349098. */
#include "atomic.h"
#include "battery.h"
#include "battery_smart.h"
#include "board.h"
#include "builtin/assert.h"
#include "charge_manager.h"
#include "charge_state.h"
#include "chipset.h"
#include "common.h"
#include "console.h"
#include "cros_version.h"
#include "ec_commands.h"
#include "gpio.h"
#include "hooks.h"
#include "host_command.h"
#include "printf.h"
#include "registers.h"
#include "system.h"
#include "task.h"
#include "tcpm/tcpci.h"
#include "tcpm/tcpm.h"
#include "timer.h"
#include "typec_control.h"
#include "usb_charge.h"
#include "usb_common.h"
#include "usb_mux.h"
#include "usb_pd.h"
#include "usb_pd_flags.h"
#include "usb_pd_tcpc.h"
#include "usb_pd_tcpm.h"
#include "usbc_ocp.h"
#include "usbc_ppc.h"
#include "util.h"
#include "vboot.h"
/* Flags to clear on a disconnect */
#define PD_FLAGS_RESET_ON_DISCONNECT_MASK \
(PD_FLAGS_PARTNER_DR_POWER | PD_FLAGS_PARTNER_DR_DATA | \
PD_FLAGS_CHECK_IDENTITY | PD_FLAGS_SNK_CAP_RECVD | \
PD_FLAGS_TCPC_DRP_TOGGLE | PD_FLAGS_EXPLICIT_CONTRACT | \
PD_FLAGS_PREVIOUS_PD_CONN | PD_FLAGS_CHECK_PR_ROLE | \
PD_FLAGS_CHECK_DR_ROLE | PD_FLAGS_PARTNER_UNCONSTR | \
PD_FLAGS_VCONN_ON | PD_FLAGS_TRY_SRC | PD_FLAGS_PARTNER_USB_COMM | \
PD_FLAGS_UPDATE_SRC_CAPS | PD_FLAGS_TS_DTS_PARTNER | \
PD_FLAGS_SNK_WAITING_BATT | PD_FLAGS_CHECK_VCONN_STATE)
#ifdef CONFIG_COMMON_RUNTIME
#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ##args)
#define CPRINTS(format, args...) cprints(CC_USBPD, format, ##args)
static int tcpc_prints(const char *string, int port)
{
return CPRINTS("TCPC p%d %s", port, string);
}
BUILD_ASSERT(CONFIG_USB_PD_PORT_MAX_COUNT <= EC_USB_PD_MAX_PORTS);
/*
* Debug log level - higher number == more log
* Level 0: Log state transitions
* Level 1: Level 0, plus state name
* Level 2: Level 1, plus packet info
* Level 3: Level 2, plus ping packet and packet dump on error
*
* Note that higher log level causes timing changes and thus may affect
* performance.
*
* Can be limited to constant debug_level by CONFIG_USB_PD_DEBUG_LEVEL
*/
#ifdef CONFIG_USB_PD_DEBUG_LEVEL
static const int debug_level = CONFIG_USB_PD_DEBUG_LEVEL;
#else
static int debug_level;
#endif
/*
* PD communication enabled flag. When false, PD state machine still
* detects source/sink connection and disconnection, and will still
* provide VBUS, but never sends any PD communication.
*/
static uint8_t pd_comm_enabled[CONFIG_USB_PD_PORT_MAX_COUNT];
#else /* CONFIG_COMMON_RUNTIME */
#define CPRINTF(format, args...)
#define CPRINTS(format, args...)
#define tcpc_prints(string, port)
static const int debug_level;
#endif
#ifdef CONFIG_USB_PD_DUAL_ROLE
#define DUAL_ROLE_IF_ELSE(port, sink_clause, src_clause) \
(pd[port].power_role == PD_ROLE_SINK ? (sink_clause) : (src_clause))
#else
#define DUAL_ROLE_IF_ELSE(port, sink_clause, src_clause) (src_clause)
#endif
#define READY_RETURN_STATE(port) \
DUAL_ROLE_IF_ELSE(port, PD_STATE_SNK_READY, PD_STATE_SRC_READY)
/* Type C supply voltage (mV) */
#define TYPE_C_VOLTAGE 5000 /* mV */
/* PD counter definitions */
#define PD_MESSAGE_ID_COUNT 7
#define PD_HARD_RESET_COUNT 2
#define PD_CAPS_COUNT 50
#define PD_SNK_CAP_RETRIES 3
/*
* 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.
*/
#define SNK_READY_HOLD_OFF_US (200 * MSEC)
/*
* For the same purpose as SNK_READY_HOLD_OFF_US, but this delay can be longer
* since the concern over "low power charger" is not relevant when connected as
* a source and the additional delay avoids a race condition where the partner
* port sends a power role swap request close to when the VDM discover identity
* message gets sent.
*/
#define SRC_READY_HOLD_OFF_US (400 * MSEC)
enum ams_seq {
AMS_START,
AMS_RESPONSE,
};
enum vdm_states {
VDM_STATE_ERR_BUSY = -3,
VDM_STATE_ERR_SEND = -2,
VDM_STATE_ERR_TMOUT = -1,
VDM_STATE_DONE = 0,
/* Anything >0 represents an active state */
VDM_STATE_READY = 1,
VDM_STATE_BUSY = 2,
VDM_STATE_WAIT_RSP_BUSY = 3,
};
#ifdef CONFIG_USB_PD_DUAL_ROLE
/* Port dual-role state */
enum pd_dual_role_states drp_state[CONFIG_USB_PD_PORT_MAX_COUNT] = {
[0 ...(CONFIG_USB_PD_PORT_MAX_COUNT - 1)] =
CONFIG_USB_PD_INITIAL_DRP_STATE
};
/* Enable variable for Try.SRC states */
static bool pd_try_src_enable;
#endif
#ifdef CONFIG_USB_PD_REV30
/*
* The spec. revision is the argument for this macro.
* Rev 0 (PD 1.0) - return PD_CTRL_REJECT
* Rev 1 (PD 2.0) - return PD_CTRL_REJECT
* Rev 2 (PD 3.0) - return PD_CTRL_NOT_SUPPORTED
*
* Note: this should only be used in locations where responding on a lower
* revision with a Reject is valid (ex. a source refusing a PR_Swap). For
* other uses of Not_Supported, use PD_CTRL_NOT_SUPPORTED directly.
*/
#define NOT_SUPPORTED(r) (r < 2 ? PD_CTRL_REJECT : PD_CTRL_NOT_SUPPORTED)
#else
#define NOT_SUPPORTED(r) PD_CTRL_REJECT
#endif
#ifdef CONFIG_USB_PD_REV30
/*
* The spec. revision is used to index into this array.
* Rev 0 (VDO 1.0) - return SVDM_VER_1_0
* Rev 1 (VDO 1.0) - return SVDM_VER_1_0
* Rev 2 (VDO 2.0) - return SVDM_VER_2_0
*/
static const uint8_t vdo_ver[] = { SVDM_VER_1_0, SVDM_VER_1_0, SVDM_VER_2_0 };
#define VDO_VER(v) vdo_ver[v]
#else
#define VDO_VER(v) SVDM_VER_1_0
#endif
static struct pd_protocol {
/* 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;
/* 3-bit rolling message ID counter */
uint8_t msg_id;
/* port polarity */
enum tcpc_cc_polarity polarity;
/* PD state for port */
enum pd_states task_state;
/* PD state when we run state handler the last time */
enum pd_states last_state;
/* bool: request state change to SUSPENDED */
uint8_t req_suspend_state;
/* The state to go to after timeout */
enum pd_states timeout_state;
/* port flags, see PD_FLAGS_* */
uint32_t flags;
/* Timeout for the current state. Set to 0 for no timeout. */
uint64_t timeout;
/* Time for source recovery after hard reset */
uint64_t src_recover;
/* Time for CC debounce end */
uint64_t cc_debounce;
/* The cc state */
enum pd_cc_states cc_state;
/* status of last transmit */
uint8_t tx_status;
/* Last received */
uint8_t last_msg_id;
/* last requested voltage PDO index */
int requested_idx;
#ifdef CONFIG_USB_PD_DUAL_ROLE
/* Current limit / voltage based on the last request message */
uint32_t curr_limit;
uint32_t supply_voltage;
/* Signal charging update that affects the port */
int new_power_request;
/* Store previously requested voltage request */
int prev_request_mv;
/* Time for Try.SRC states */
uint64_t try_src_marker;
uint64_t try_timeout;
#endif
#ifdef CONFIG_USB_PD_TCPC_LOW_POWER
/* Time to enter low power mode */
uint64_t low_power_time;
/* Time to debounce exit low power mode */
uint64_t low_power_exit_time;
/* Tasks to notify after TCPC has been reset */
atomic_t tasks_waiting_on_reset;
/* Tasks preventing TCPC from entering low power mode */
atomic_t tasks_preventing_lpm;
#endif
#ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE
/*
* Timer for handling TOGGLE_OFF/FORCE_SINK mode when auto-toggle
* enabled. See drp_auto_toggle_next_state() for details.
*/
uint64_t drp_sink_time;
#endif
/*
* Time to ignore Vbus absence due to external IC debounce detection
* logic immediately after a power role swap.
*/
uint64_t vbus_debounce_time;
/* PD state for Vendor Defined Messages */
enum vdm_states vdm_state;
/* Timeout for the current vdm state. Set to 0 for no timeout. */
timestamp_t vdm_timeout;
/* next Vendor Defined Message to send */
uint32_t vdo_data[VDO_MAX_SIZE];
/* type of transmit message (SOP/SOP'/SOP'') */
enum tcpci_msg_type xmit_type;
uint8_t vdo_count;
/* VDO to retry if UFP responder replied busy. */
uint32_t vdo_retry;
/* Attached ChromeOS device id, RW hash, and current RO / RW image */
uint16_t dev_id;
uint32_t dev_rw_hash[PD_RW_HASH_SIZE / 4];
enum ec_image current_image;
#ifdef CONFIG_USB_PD_REV30
/* protocol revision */
uint8_t rev;
#endif
/*
* Some port partners are really chatty after an explicit contract is
* established. Therefore, we allow this time for the port partner to
* send any messages in order to avoid a collision of sending messages
* of our own.
*/
uint64_t ready_state_holdoff_timer;
/*
* PD 2.0 spec, section 6.5.11.1
* When we can give up on a HARD_RESET transmission.
*/
uint64_t hard_reset_complete_timer;
} pd[CONFIG_USB_PD_PORT_MAX_COUNT];
#ifdef CONFIG_USB_PD_TCPMV1_DEBUG
static const char *const pd_state_names[] = {
"DISABLED",
"SUSPENDED",
"SNK_DISCONNECTED",
"SNK_DISCONNECTED_DEBOUNCE",
"SNK_HARD_RESET_RECOVER",
"SNK_DISCOVERY",
"SNK_REQUESTED",
"SNK_TRANSITION",
"SNK_READY",
"SNK_SWAP_INIT",
"SNK_SWAP_SNK_DISABLE",
"SNK_SWAP_SRC_DISABLE",
"SNK_SWAP_STANDBY",
"SNK_SWAP_COMPLETE",
"SRC_DISCONNECTED",
"SRC_DISCONNECTED_DEBOUNCE",
"SRC_HARD_RESET_RECOVER",
"SRC_STARTUP",
"SRC_DISCOVERY",
"SRC_NEGOCIATE",
"SRC_ACCEPTED",
"SRC_POWERED",
"SRC_TRANSITION",
"SRC_READY",
"SRC_GET_SNK_CAP",
"DR_SWAP",
"SRC_SWAP_INIT",
"SRC_SWAP_SNK_DISABLE",
"SRC_SWAP_SRC_DISABLE",
"SRC_SWAP_STANDBY",
"VCONN_SWAP_SEND",
"VCONN_SWAP_INIT",
"VCONN_SWAP_READY",
"SOFT_RESET",
"HARD_RESET_SEND",
"HARD_RESET_EXECUTE",
"BIST_RX",
"BIST_TX",
"DRP_AUTO_TOGGLE",
};
BUILD_ASSERT(ARRAY_SIZE(pd_state_names) == PD_STATE_COUNT);
#endif
int pd_comm_is_enabled(int port)
{
#ifdef CONFIG_COMMON_RUNTIME
return pd_comm_enabled[port];
#else
return 1;
#endif
}
bool pd_alt_mode_capable(int port)
{
/*
* PD is alternate mode capable only if PD communication is enabled and
* the port is not suspended.
*/
return pd_comm_is_enabled(port) &&
!(pd[port].task_state == PD_STATE_SUSPENDED);
}
static inline void set_state_timeout(int port, uint64_t timeout,
enum pd_states timeout_state)
{
pd[port].timeout = timeout;
pd[port].timeout_state = timeout_state;
}
int pd_get_rev(int port, enum tcpci_msg_type type)
{
#ifdef CONFIG_USB_PD_REV30
/* TCPMv1 Only stores PD revision for SOP and SOP' types */
ASSERT(type < NUM_SOP_STAR_TYPES - 1);
return pd[port].rev;
#else
return PD_REV20;
#endif
}
int pd_get_vdo_ver(int port, enum tcpci_msg_type type)
{
#ifdef CONFIG_USB_PD_REV30
return vdo_ver[pd[port].rev];
#else
return SVDM_VER_1_0;
#endif
}
/* Return flag for pd state is connected */
int pd_is_connected(int port)
{
if (pd[port].task_state == PD_STATE_DISABLED)
return 0;
#ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE
if (pd[port].task_state == PD_STATE_DRP_AUTO_TOGGLE)
return 0;
#endif
return DUAL_ROLE_IF_ELSE(
port,
/* sink */
pd[port].task_state != PD_STATE_SNK_DISCONNECTED &&
pd[port].task_state !=
PD_STATE_SNK_DISCONNECTED_DEBOUNCE,
/* source */
pd[port].task_state != PD_STATE_SRC_DISCONNECTED &&
pd[port].task_state !=
PD_STATE_SRC_DISCONNECTED_DEBOUNCE);
}
/* Return true if partner port is known to be PD capable. */
bool pd_capable(int port)
{
return !!(pd[port].flags & PD_FLAGS_PREVIOUS_PD_CONN);
}
/*
* Return true if partner port is capable of communication over USB data
* lines.
*/
bool pd_get_partner_usb_comm_capable(int port)
{
return !!(pd[port].flags & PD_FLAGS_PARTNER_USB_COMM);
}
#ifdef CONFIG_USB_PD_DUAL_ROLE
void pd_vbus_low(int port)
{
pd[port].flags &= ~PD_FLAGS_VBUS_NEVER_LOW;
}
#endif
#ifdef CONFIG_USBC_VCONN
static void set_vconn(int port, int enable)
{
/*
* Disable PPC Vconn first then TCPC in case the voltage feeds back
* to TCPC and damages.
*/
if (IS_ENABLED(CONFIG_USBC_PPC_VCONN) && !enable)
ppc_set_vconn(port, 0);
/*
* Some TCPCs/PPC combinations can trigger OVP if the TCPC doesn't
* source VCONN. This happens if the TCPC will trip OVP with 5V, and the
* PPC doesn't isolate the TCPC from VCONN when sourcing. But, some PPCs
* which do isolate the TCPC can't handle 5V on its host-side CC pins,
* so the TCPC shouldn't source VCONN in those cases.
*
* In the first case, both TCPC and PPC will potentially source Vconn,
* but that should be okay since Vconn has "make before break"
* electrical requirements when swapping anyway.
*
* See b/72961003 and b/180973460
*/
tcpm_set_vconn(port, enable);
if (IS_ENABLED(CONFIG_USBC_PPC_VCONN) && enable)
ppc_set_vconn(port, 1);
}
#endif /* defined(CONFIG_USBC_VCONN) */
#ifdef CONFIG_USB_PD_REV30
/* Note: rp should be set to either SINK_TX_OK or SINK_TX_NG */
static void sink_can_xmit(int port, int rp)
{
tcpm_select_rp_value(port, rp);
tcpm_set_cc(port, TYPEC_CC_RP);
/* We must wait tSinkTx before sending a message */
if (rp == SINK_TX_NG)
crec_usleep(PD_T_SINK_TX);
}
#endif
#ifdef CONFIG_USB_PD_TCPC_LOW_POWER
/* 10 ms is enough time for any TCPC transaction to complete. */
#define PD_LPM_DEBOUNCE_US (10 * MSEC)
/* 25 ms on LPM exit to ensure TCPC is settled */
#define PD_LPM_EXIT_DEBOUNCE_US (25 * MSEC)
/* This is only called from the PD tasks that owns the port. */
static void handle_device_access(int port)
{
/* This should only be called from the PD task */
assert(port == TASK_ID_TO_PD_PORT(task_get_current()));
pd[port].low_power_time = get_time().val + PD_LPM_DEBOUNCE_US;
if (pd[port].flags & PD_FLAGS_LPM_ENGAGED) {
tcpc_prints("Exit Low Power Mode", port);
pd[port].flags &=
~(PD_FLAGS_LPM_ENGAGED | PD_FLAGS_LPM_REQUESTED);
pd[port].flags |= PD_FLAGS_LPM_EXIT;
pd[port].low_power_exit_time =
get_time().val + PD_LPM_EXIT_DEBOUNCE_US;
/*
* Wake to ensure we make another pass through the main task
* loop after clearing the flags.
*/
task_wake(PD_PORT_TO_TASK_ID(port));
}
}
static int pd_device_in_low_power(int port)
{
/*
* If we are actively waking the device up in the PD task, do not
* let TCPC operation wait or retry because we are in low power mode.
*/
if (port == TASK_ID_TO_PD_PORT(task_get_current()) &&
(pd[port].flags & PD_FLAGS_LPM_TRANSITION))
return 0;
return pd[port].flags & PD_FLAGS_LPM_ENGAGED;
}
static int reset_device_and_notify(int port)
{
int rv;
int task, waiting_tasks;
/* This should only be called from the PD task */
assert(port == TASK_ID_TO_PD_PORT(task_get_current()));
pd[port].flags |= PD_FLAGS_LPM_TRANSITION;
rv = tcpm_init(port);
pd[port].flags &= ~PD_FLAGS_LPM_TRANSITION;
if (rv == EC_SUCCESS)
tcpc_prints("init ready", port);
else
tcpc_prints("init failed!", port);
/*
* Before getting the other tasks that are waiting, clear the reset
* event from this PD task to prevent multiple reset/init events
* occurring.
*
* The double reset event happens when the higher priority PD interrupt
* task gets an interrupt during the above tcpm_init function. When that
* occurs, the higher priority task waits correctly for us to finish
* waking the TCPC, but it has also set PD_EVENT_TCPC_RESET again, which
* would result in a second, unnecessary init.
*/
atomic_clear_bits(task_get_event_bitmap(task_get_current()),
PD_EVENT_TCPC_RESET);
waiting_tasks = atomic_clear(&pd[port].tasks_waiting_on_reset);
/*
* Now that we are done waking up the device, handle device access
* manually because we ignored it while waking up device.
*/
handle_device_access(port);
/* Clear SW LPM state; the state machine will set it again if needed */
pd[port].flags &= ~PD_FLAGS_LPM_REQUESTED;
/* Wake up all waiting tasks. */
while (waiting_tasks) {
task = __fls(waiting_tasks);
waiting_tasks &= ~BIT(task);
task_set_event(task, TASK_EVENT_PD_AWAKE);
}
return rv;
}
static void pd_wait_for_wakeup(int port)
{
if (port == TASK_ID_TO_PD_PORT(task_get_current())) {
/* If we are in the PD task, we can directly reset */
reset_device_and_notify(port);
} else {
/* Otherwise, we need to wait for the TCPC reset to complete */
atomic_or(&pd[port].tasks_waiting_on_reset,
1 << task_get_current());
/*
* NOTE: We could be sending the PD task the reset event while
* it is already processing the reset event. If that occurs,
* then we will reset the TCPC multiple times, which is
* undesirable but most likely benign. Empirically, this doesn't
* happen much, but it if starts occurring, we can add a guard
* to prevent/reduce it.
*/
task_set_event(PD_PORT_TO_TASK_ID(port), PD_EVENT_TCPC_RESET);
task_wait_event_mask(TASK_EVENT_PD_AWAKE, -1);
}
}
void pd_wait_exit_low_power(int port)
{
if (pd_device_in_low_power(port))
pd_wait_for_wakeup(port);
}
/*
* This can be called from any task. If we are in the PD task, we can handle
* immediately. Otherwise, we need to notify the PD task via event.
*/
void pd_device_accessed(int port)
{
if (port == TASK_ID_TO_PD_PORT(task_get_current())) {
/* Ignore any access to device while it is waking up */
if (pd[port].flags & PD_FLAGS_LPM_TRANSITION)
return;
handle_device_access(port);
} else {
task_set_event(PD_PORT_TO_TASK_ID(port),
PD_EVENT_DEVICE_ACCESSED);
}
}
void pd_prevent_low_power_mode(int port, int prevent)
{
const int current_task_mask = (1 << task_get_current());
if (prevent)
atomic_or(&pd[port].tasks_preventing_lpm, current_task_mask);
else
atomic_clear_bits(&pd[port].tasks_preventing_lpm,
current_task_mask);
}
/* This is only called from the PD tasks that owns the port. */
static void exit_low_power_mode(int port)
{
if (pd[port].flags & PD_FLAGS_LPM_ENGAGED)
reset_device_and_notify(port);
else
pd[port].flags &= ~PD_FLAGS_LPM_REQUESTED;
}
#else /* !CONFIG_USB_PD_TCPC_LOW_POWER */
/* We don't need to notify anyone if low power mode isn't involved. */
static int reset_device_and_notify(int port)
{
const int rv = tcpm_init(port);
if (rv == EC_SUCCESS)
tcpc_prints("init ready", port);
else
tcpc_prints("init failed!", port);
return rv;
}
#endif /* CONFIG_USB_PD_TCPC_LOW_POWER */
/**
* Invalidate last message received at the port when the port gets disconnected
* or reset(soft/hard). This is used to identify and handle the duplicate
* messages.
*
* @param port USB PD TCPC port number
*/
static void invalidate_last_message_id(int port)
{
pd[port].last_msg_id = INVALID_MSG_ID_COUNTER;
}
static bool consume_sop_repeat_message(int port, uint8_t msg_id)
{
if (pd[port].last_msg_id != msg_id) {
pd[port].last_msg_id = msg_id;
return false;
}
CPRINTF("C%d Repeat msg_id %d\n", port, msg_id);
return true;
}
/**
* Identify and drop any duplicate messages received at the port.
*
* @param port USB PD TCPC port number
* @param msg_header Message Header containing the RX message ID
* @return True if the received message is a duplicate one, False otherwise.
*/
static bool consume_repeat_message(int port, uint32_t msg_header)
{
uint8_t msg_id = PD_HEADER_ID(msg_header);
/* If repeat message ignore, except softreset control request. */
if (PD_HEADER_TYPE(msg_header) == PD_CTRL_SOFT_RESET &&
PD_HEADER_CNT(msg_header) == 0) {
return false;
} else {
return consume_sop_repeat_message(port, msg_id);
}
}
/**
* Returns true if the port is currently in the try src state.
*/
static inline int is_try_src(int port)
{
return pd[port].flags & PD_FLAGS_TRY_SRC;
}
static inline void set_state(int port, enum pd_states next_state)
{
enum pd_states last_state = pd[port].task_state;
#if defined(CONFIG_LOW_POWER_IDLE) && !defined(CONFIG_USB_PD_TCPC_ON_CHIP)
int i;
#endif
int not_auto_toggling = 1;
set_state_timeout(port, 0, 0);
pd[port].task_state = next_state;
if (last_state == next_state)
return;
#if defined(CONFIG_USBC_PPC) && defined(CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE)
/* If we're entering DRP_AUTO_TOGGLE, there is no sink connected. */
if (next_state == PD_STATE_DRP_AUTO_TOGGLE) {
ppc_dev_is_connected(port, PPC_DEV_DISCONNECTED);
/* Disable Auto Discharge Disconnect */
tcpm_enable_auto_discharge_disconnect(port, 0);
if (IS_ENABLED(CONFIG_USBC_OCP)) {
usbc_ocp_snk_is_connected(port, false);
/*
* Clear the overcurrent event counter
* since we've detected a disconnect.
*/
usbc_ocp_clear_event_counter(port);
}
}
#endif /* CONFIG_USBC_PPC && CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE */
#ifdef CONFIG_USB_PD_DUAL_ROLE
#ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE
if (last_state != PD_STATE_DRP_AUTO_TOGGLE)
/* Clear flag to allow DRP auto toggle when possible */
pd[port].flags &= ~PD_FLAGS_TCPC_DRP_TOGGLE;
else
/* This is an auto toggle instead of disconnect */
not_auto_toggling = 0;
#endif
/* Ignore dual-role toggling between sink and source */
if ((last_state == PD_STATE_SNK_DISCONNECTED &&
next_state == PD_STATE_SRC_DISCONNECTED) ||
(last_state == PD_STATE_SRC_DISCONNECTED &&
next_state == PD_STATE_SNK_DISCONNECTED))
return;
if (next_state == PD_STATE_SRC_DISCONNECTED ||
next_state == PD_STATE_SNK_DISCONNECTED) {
#ifdef CONFIG_USBC_PPC
enum tcpc_cc_voltage_status cc1, cc2;
tcpm_get_cc(port, &cc1, &cc2);
/*
* Neither a debug accessory nor UFP attached.
* Tell the PPC module that there is no device connected.
*/
if (!cc_is_at_least_one_rd(cc1, cc2)) {
ppc_dev_is_connected(port, PPC_DEV_DISCONNECTED);
if (IS_ENABLED(CONFIG_USBC_OCP)) {
usbc_ocp_snk_is_connected(port, false);
/*
* Clear the overcurrent event counter
* since we've detected a disconnect.
*/
usbc_ocp_clear_event_counter(port);
}
}
#endif /* CONFIG_USBC_PPC */
/* Clear the holdoff timer since the port is disconnected. */
pd[port].ready_state_holdoff_timer = 0;
/*
* We should not clear any flags when transitioning back to the
* disconnected state from the debounce state as the two states
* here are really the same states in the state diagram.
*/
if (last_state != PD_STATE_SNK_DISCONNECTED_DEBOUNCE &&
last_state != PD_STATE_SRC_DISCONNECTED_DEBOUNCE) {
pd[port].flags &= ~PD_FLAGS_RESET_ON_DISCONNECT_MASK;
}
/* Clear the input current limit */
pd_set_input_current_limit(port, 0, 0);
#ifdef CONFIG_CHARGE_MANAGER
typec_set_input_current_limit(port, 0, 0);
charge_manager_set_ceil(port, CEIL_REQUESTOR_PD,
CHARGE_CEIL_NONE);
#endif
#ifdef CONFIG_BC12_DETECT_DATA_ROLE_TRIGGER
/*
* When data role set events are used to enable BC1.2, then CC
* detach events are used to notify BC1.2 that it can be powered
* down.
*/
usb_charger_task_set_event(port, USB_CHG_EVENT_CC_OPEN);
#endif /* CONFIG_BC12_DETECT_DATA_ROLE_TRIGGER */
#ifdef CONFIG_USBC_VCONN
set_vconn(port, 0);
#endif /* defined(CONFIG_USBC_VCONN) */
pd_update_saved_port_flags(port, PD_BBRMFLG_EXPLICIT_CONTRACT,
0);
#else /* CONFIG_USB_PD_DUAL_ROLE */
if (next_state == PD_STATE_SRC_DISCONNECTED) {
#ifdef CONFIG_USBC_VCONN
set_vconn(port, 0);
#endif /* CONFIG_USBC_VCONN */
#endif /* !CONFIG_USB_PD_DUAL_ROLE */
/* If we are source, make sure VBUS is off and restore RP */
if (pd[port].power_role == PD_ROLE_SOURCE) {
/* Restore non-active ports to CONFIG_USB_PD_PULLUP */
pd_power_supply_reset(port);
tcpm_set_cc(port, TYPEC_CC_RP);
}
#ifdef CONFIG_USB_PD_REV30
/* Adjust rev to highest level*/
pd[port].rev = PD_REV30;
#endif
pd[port].dev_id = 0;
#ifdef CONFIG_CHARGE_MANAGER
charge_manager_update_dualrole(port, CAP_UNKNOWN);
#endif
#ifdef CONFIG_USB_PD_ALT_MODE_DFP
if (pd_dfp_exit_mode(port, TCPCI_MSG_SOP, 0, 0))
usb_mux_set_safe_mode(port);
#endif
/*
* Indicate that the port is disconnected by setting role to
* DFP as SoCs have special signals when they are the UFP ports
* (e.g. OTG signals)
*/
pd_execute_data_swap(port, PD_ROLE_DFP);
#ifdef CONFIG_USBC_SS_MUX
usb_mux_set(port, USB_PD_MUX_NONE, USB_SWITCH_DISCONNECT,
pd[port].polarity);
#endif
/* Disable TCPC RX */
tcpm_set_rx_enable(port, 0);
/* Invalidate message IDs. */
invalidate_last_message_id(port);
if (not_auto_toggling)
/* Disable Auto Discharge Disconnect */
tcpm_enable_auto_discharge_disconnect(port, 0);
/* detect USB PD cc disconnect */
if (IS_ENABLED(CONFIG_COMMON_RUNTIME))
hook_notify(HOOK_USB_PD_DISCONNECT);
}
#ifdef CONFIG_USB_PD_REV30
/* Upon entering SRC_READY, it is safe for the sink to transmit */
if (next_state == PD_STATE_SRC_READY) {
if (pd[port].rev == PD_REV30 &&
pd[port].flags & PD_FLAGS_EXPLICIT_CONTRACT)
sink_can_xmit(port, SINK_TX_OK);
}
#endif
#if defined(CONFIG_LOW_POWER_IDLE) && !defined(CONFIG_USB_PD_TCPC_ON_CHIP)
/* If a PD device is attached then disable deep sleep */
for (i = 0; i < board_get_usb_pd_port_count(); i++) {
if (pd_capable(i))
break;
}
if (i == board_get_usb_pd_port_count())
enable_sleep(SLEEP_MASK_USB_PD);
else
disable_sleep(SLEEP_MASK_USB_PD);
#endif
#ifdef CONFIG_USB_PD_TCPMV1_DEBUG
if (debug_level > 0)
CPRINTF("C%d st%d %s\n", port, next_state,
pd_state_names[next_state]);
else
#endif
CPRINTF("C%d st%d\n", port, next_state);
}
/* increment message ID counter */
static void inc_id(int port)
{
pd[port].msg_id = (pd[port].msg_id + 1) & PD_MESSAGE_ID_COUNT;
}
void pd_transmit_complete(int port, int status)
{
if (status == TCPC_TX_COMPLETE_SUCCESS)
inc_id(port);
pd[port].tx_status = status;
task_set_event(PD_PORT_TO_TASK_ID(port), PD_EVENT_TX);
}
static int pd_transmit(int port, enum tcpci_msg_type type, uint16_t header,
const uint32_t *data, enum ams_seq ams)
{
int evt;
int res;
#ifdef CONFIG_USB_PD_REV30
int sink_ng = 0;
#endif
/* If comms are disabled, do not transmit, return error */
if (!pd_comm_is_enabled(port))
return -1;
/* Don't try to transmit anything until we have processed
* all RX messages.
*/
if (tcpm_has_pending_message(port))
return -1;
#ifdef CONFIG_USB_PD_REV30
/* Source-coordinated collision avoidance */
/*
* USB PD Rev 3.0, Version 2.0: Section 2.7.3.2
* Collision Avoidance - Protocol Layer
*
* In order to avoid message collisions due to asynchronous Messaging
* sent from the Sink, the Source sets Rp to SinkTxOk (3A) to indicate
* to the Sink that it is ok to initiate an AMS. When the Source wishes
* to initiate an AMS, it sets Rp to SinkTxNG (1.5A).
* When the Sink detects that Rp is set to SinkTxOk, it May initiate an
* AMS. When the Sink detects that Rp is set to SinkTxNG it Shall Not
* initiate an AMS and Shall only send Messages that are part of an AMS
* the Source has initiated.
* Note that this restriction applies to SOP* AMS’s i.e. for both Port
* to Port and Port to Cable Plug communications.
*
* This starts after an Explicit Contract is in place (see section 2.5.2
* SOP* Collision Avoidance).
*
* Note: a Sink can still send Hard Reset signaling at any time.
*/
if ((pd[port].rev == PD_REV30) && ams == AMS_START &&
(pd[port].flags & PD_FLAGS_EXPLICIT_CONTRACT)) {
if (pd[port].power_role == PD_ROLE_SOURCE) {
/*
* Inform Sink that it can't transmit. If a sink
* transmission is in progress and a collision occurs,
* a reset is generated. This should be rare because
* all extended messages are chunked. This effectively
* defaults to PD REV 2.0 collision avoidance.
*/
sink_can_xmit(port, SINK_TX_NG);
sink_ng = 1;
} else if (type != TCPCI_MSG_TX_HARD_RESET) {
enum tcpc_cc_voltage_status cc1, cc2;
tcpm_get_cc(port, &cc1, &cc2);
if (cc1 == TYPEC_CC_VOLT_RP_1_5 ||
cc2 == TYPEC_CC_VOLT_RP_1_5) {
/* Sink can't transmit now. */
/* Return failure, pd_task can retry later */
return -1;
}
}
}
#endif
tcpm_transmit(port, type, header, data);
/* Wait until TX is complete */
evt = task_wait_event_mask(PD_EVENT_TX, PD_T_TCPC_TX_TIMEOUT);
if (evt & TASK_EVENT_TIMER)
return -1;
/* TODO: give different error condition for failed vs discarded */
res = pd[port].tx_status == TCPC_TX_COMPLETE_SUCCESS ? 1 : -1;
#ifdef CONFIG_USB_PD_REV30
/* If the AMS transaction failed to start, reset CC to OK */
if (res < 0 && sink_ng)
sink_can_xmit(port, SINK_TX_OK);
#endif
return res;
}
static void pd_update_roles(int port)
{
/* Notify TCPC of role update */
tcpm_set_msg_header(port, pd[port].power_role, pd[port].data_role);
}
static int send_control(int port, int type)
{
int bit_len;
uint16_t header = PD_HEADER(type, pd[port].power_role,
pd[port].data_role, pd[port].msg_id, 0,
pd_get_rev(port, TCPCI_MSG_SOP), 0);
/*