summaryrefslogtreecommitdiff
path: root/common/usbc/usb_mode.c
blob: b9dc4973bcacaf6b0cef57fe1c1537820b180525 (plain)
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
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/*
 * USB4 mode support
 * Refer USB Type-C Cable and Connector Specification Release 2.0 Section 5 and
 * USB Power Delivery Specification Revision 3.0, Version 2.0 Section 6.4.8
 */

#include <stdbool.h>
#include <stdint.h>
#include "compile_time_macros.h"
#include "console.h"
#include "tcpm/tcpm.h"
#include "usb_common.h"
#include "usb_mode.h"
#include "usb_mux.h"
#include "usb_pd.h"
#include "usb_pd_dpm.h"
#include "usb_pd_tcpm.h"
#include "usb_pe_sm.h"
#include "usbc_ppc.h"

#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

enum usb4_mode_status {
	USB4_MODE_FAILURE,
	USB4_MODE_SUCCESS,
};

enum usb4_states {
	USB4_START,
	USB4_ENTER_SOP,
	USB4_ENTER_SOP_PRIME,
	USB4_ENTER_SOP_PRIME_PRIME,
	USB4_ACTIVE,
	USB4_INACTIVE,
	USB4_STATE_COUNT,
};

/*
 * USB4 PD flow:
 *
 *                            Cable type
 *                                 |
 *            |-------- Passive ---|---- Active -----|
 *            |                                      |
 *      USB Highest Speed         Structured VDM version
 *            |                   (cable revision)-- <2.0---->|
 *    --------|--------|------|       |                       |
 *    |       |        |      |       >=2.0                   |
 *  >=Gen3   Gen2    Gen1  USB2.0     |                       |
 *    |       |        |      |   VDO version--- <1.3 ---> Modal op? -- N --|
 * Enter USB  |        |      |   (B21:23 of                  |             |
 * SOP  with  |        |      |    Discover ID SOP'-          y             |
 * Gen3 cable |        |    Skip   Active cable VDO1)         |             |
 * speed      |        |    USB4      |                    TBT SVID? -- N --|
 *            |        |    mode      >=1.3                   |             |
 *    Is modal op?     |    entry     |                       y             |
 *            |        |            Cable USB4  - N           |             |
 *            y        |            support?      |       Gen4 cable? - N - Skip
 *            |        |               |      Skip USB4       |             USB4
 *    Is TBT SVID? -N- Enter           |      mode entry      |             mode
 *            |       USB4 SOP         |                      |            entry
 *            y       with Gen2        y                      |
 *            |       cable speed      |                      |
 *            |                        |                      |
 *    Is Discover mode                 |                      |
 *    SOP' B25? - N - Enter      Enter USB4 mode              |
 *            |     USB4 SOP     (SOP, SOP', SOP'')           |
 *            |     with speed                                |
 *            y     from TBT mode                             |
 *            |     SOP' VDO                                  |
 *            |                           |<-- NAK -- Enter mode TBT SOP'<---|
 * |---->Enter TBT SOP'-------NAK------>| |                   |              |
 * |          |                         | |                  ACK             |
 * |         ACK                        | |                   |              |
 * |          |                         | |<-- NAK -- Enter mode TBT SOP''   |
 * |     Enter USB4 SOP                 | |                   |              |
 * |     with speed from         Exit TBT mode SOP           ACK             |
 * |     TBT mode SOP' VDO              | |                   |              |
 * |                                  ACK/NAK          Enter USB4 SOP        |
 * |                                    | |            with speed from       |
 * |                             Exit TBT mode SOP''   TBT mode SOP' VDO     |
 * |                                    | |                                  |
 * |                                  ACK/NAK                                |
 * |                                    | |                                  |
 * |                             Exit TBT mode SOP'                          |
 * |                                    | |                                  |
 * |                                   ACK/NAK                               |
 * |                                    | |                                  |
 * |---- N ----Retry done? -------------| |--------Retry done? ---- N -------|
 *                  |                                   |
 *                  y                                   y
 *                  |                                   |
 *           Skip USB4 mode entry                 Skip USB4 mode entry
 */

static enum usb4_states usb4_state[CONFIG_USB_PD_PORT_MAX_COUNT];

static void usb4_debug_prints(int port, enum usb4_mode_status usb4_status)
{
	CPRINTS("C%d: USB4: State:%d Status:%d", port, usb4_state[port],
		usb4_status);
}

bool enter_usb_entry_is_done(int port)
{
	return usb4_state[port] == USB4_ACTIVE ||
		usb4_state[port] == USB4_INACTIVE;
}

void usb4_exit_mode_request(int port)
{
	usb4_state[port] = USB4_START;
	usb_mux_set_safe_mode_exit(port);
	set_usb_mux_with_current_data_role(port);
}

void enter_usb_init(int port)
{
	usb4_state[port] = USB4_START;
}

void enter_usb_failed(int port)
{
	/*
	 * Since Enter USB sets the mux state to SAFE mode, fall back
	 * to USB mode on receiving a NAK.
	 */
	usb_mux_set(port, USB_PD_MUX_USB_ENABLED, USB_SWITCH_CONNECT,
		    polarity_rm_dts(pd_get_polarity(port)));

	usb4_debug_prints(port, USB4_MODE_FAILURE);
	usb4_state[port] = USB4_INACTIVE;
}

static bool enter_usb_response_valid(int port, enum tcpci_msg_type type)
{
	/*
	 * Check for an unexpected response.
	 */
	if (get_usb_pd_cable_type(port) == IDH_PTYPE_PCABLE &&
	     type != TCPCI_MSG_SOP) {
		enter_usb_failed(port);
		return false;
	}
	return true;
}

bool enter_usb_port_partner_is_capable(int port)
{
	const struct pd_discovery *disc =
			pd_get_am_discovery(port, TCPCI_MSG_SOP);

	if (usb4_state[port] == USB4_INACTIVE)
		return false;

	if (!PD_PRODUCT_IS_USB4(disc->identity.product_t1.raw_value))
		return false;

	return true;
}

bool enter_usb_cable_is_capable(int port)
{
	if (get_usb_pd_cable_type(port) == IDH_PTYPE_PCABLE) {
		if (get_usb4_cable_speed(port) < USB_R30_SS_U32_U40_GEN1)
			return false;
	} else if (get_usb_pd_cable_type(port) == IDH_PTYPE_ACABLE) {
		const struct pd_discovery *disc_sop_prime =
			pd_get_am_discovery(port, TCPCI_MSG_SOP_PRIME);

		if (pd_get_vdo_ver(port, TCPCI_MSG_SOP_PRIME) >= VDM_VER20 &&
		    disc_sop_prime->identity.product_t1.a_rev30.vdo_ver >=
							VDO_VERSION_1_3) {
			union active_cable_vdo2_rev30 a2_rev30 =
				disc_sop_prime->identity.product_t2.a2_rev30;
			/*
			 * For VDM version >= 2.0 and VD0 version is >= 1.3,
			 * do not enter USB4 mode if the cable isn't USB4
			 * capable.
			 */
			if (a2_rev30.usb_40_support == USB4_NOT_SUPPORTED)
				return false;
		/*
		 * For VDM version < 2.0 or VDO version < 1.3, do not enter USB4
		 * mode if the cable -
		 * doesn't support modal operation or
		 * doesn't support Intel SVID or
		 * doesn't have rounded support.
		 */
		} else {
			const struct pd_discovery *disc =
				pd_get_am_discovery(port, TCPCI_MSG_SOP);
			union tbt_mode_resp_cable cable_mode_resp = {
				.raw_value = pd_get_tbt_mode_vdo(port,
							TCPCI_MSG_SOP_PRIME) };

			if (!disc->identity.idh.modal_support ||
			   !pd_is_mode_discovered_for_svid(port,
					TCPCI_MSG_SOP_PRIME, USB_VID_INTEL) ||
			    cable_mode_resp.tbt_rounded !=
					TBT_GEN3_GEN4_ROUNDED_NON_ROUNDED)
				return false;
		}
	} else {
		/* Not Emark cable */
		return false;
	}

	return true;
}

void enter_usb_accepted(int port, enum tcpci_msg_type type)
{
	const struct pd_discovery *disc;

	if (!enter_usb_response_valid(port, type))
		return;

	switch (usb4_state[port]) {
	case USB4_ENTER_SOP_PRIME:
		disc = pd_get_am_discovery(port, TCPCI_MSG_SOP_PRIME);
		if (disc->identity.product_t1.a_rev20.sop_p_p)
			usb4_state[port] = USB4_ENTER_SOP_PRIME_PRIME;
		else
			usb4_state[port] = USB4_ENTER_SOP;
		break;
	case USB4_ENTER_SOP_PRIME_PRIME:
		usb4_state[port] = USB4_ENTER_SOP;
		break;
	case USB4_ENTER_SOP:
		/* Connect the SBU and USB lines to the connector */
		if (IS_ENABLED(CONFIG_USBC_PPC_SBU))
			ppc_set_sbu(port, 1);

		usb4_state[port] = USB4_ACTIVE;

		/* Set usb mux to USB4 mode */
		usb_mux_set(port, USB_PD_MUX_USB4_ENABLED, USB_SWITCH_CONNECT,
			    polarity_rm_dts(pd_get_polarity(port)));

		usb4_debug_prints(port, USB4_MODE_SUCCESS);
		break;
	case USB4_ACTIVE:
		break;
	default:
		enter_usb_failed(port);
	}
}

void enter_usb_rejected(int port, enum tcpci_msg_type type)
{
	if (!enter_usb_response_valid(port, type) ||
	    usb4_state[port] == USB4_ACTIVE)
		return;

	enter_usb_failed(port);
}

uint32_t enter_usb_setup_next_msg(int port, enum tcpci_msg_type *type)
{
	const struct pd_discovery *disc_sop_prime;

	switch (usb4_state[port]) {
	case USB4_START:
		disc_sop_prime = pd_get_am_discovery(port, TCPCI_MSG_SOP_PRIME);
		/*
		 * Ref: Tiger Lake Platform PD Controller Interface Requirements
		 * for Integrated USBC, section A.2.2: USB4 as DFP.
		 * Enter safe mode before sending Enter USB SOP/SOP'/SOP''
		 * TODO (b/156749387): Remove once data reset feature is in
		 * place.
		 */
		usb_mux_set_safe_mode(port);

		if (pd_get_vdo_ver(port, TCPCI_MSG_SOP_PRIME) < VDM_VER20 ||
		    disc_sop_prime->identity.product_t1.a_rev30.vdo_ver <
							VDO_VERSION_1_3 ||
		    get_usb_pd_cable_type(port) == IDH_PTYPE_PCABLE) {
			usb4_state[port] = USB4_ENTER_SOP;
		} else {
			usb4_state[port] = USB4_ENTER_SOP_PRIME;
			*type = TCPCI_MSG_SOP_PRIME;
		}
		break;
	case USB4_ENTER_SOP_PRIME:
		*type = TCPCI_MSG_SOP_PRIME;
		break;
	case USB4_ENTER_SOP_PRIME_PRIME:
		*type = TCPCI_MSG_SOP_PRIME_PRIME;
		break;
	case USB4_ENTER_SOP:
		*type = TCPCI_MSG_SOP;
		break;
	case USB4_ACTIVE:
		return -1;
	default:
		return 0;
	}
	return get_enter_usb_msg_payload(port);
}