summaryrefslogtreecommitdiff
path: root/common/usb_tc_vpd_sm.c
blob: 33762257a0ecaf70fa9e2dd232c084dc8e2879ed (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
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
/* Copyright 2019 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.
 */

#include "common.h"
#include "console.h"
#include "system.h"
#include "task.h"
#include "tcpm.h"
#include "usb_pd.h"
#include "usb_tc_sm.h"
#include "usb_tc_vpd_sm.h"
#include "vpd_api.h"

/* USB Type-C VCONN Powered Device module */

#ifdef CONFIG_COMMON_RUNTIME
#define CPRINTF(format, args...) cprintf(CC_USB, format, ## args)
#define CPRINTS(format, args...) cprints(CC_USB, format, ## args)
#else /* CONFIG_COMMON_RUNTIME */
#define CPRINTF(format, args...)
#define CPRINTS(format, args...)
#endif

/* Type-C Layer Flags */
#define TC_FLAGS_VCONN_ON           BIT(0)

#undef PD_DEFAULT_STATE
/* Port default state at startup */
#define PD_DEFAULT_STATE(port) tc_unattached_snk

/**
 * This is the Type-C Port object that contains information needed to
 * implement a VCONN Powered Device.
 */
struct type_c tc[CONFIG_USB_PD_PORT_COUNT];

/* Type-C states */
DECLARE_STATE(tc, disabled, WITH_EXIT);
DECLARE_STATE(tc, unattached_snk, NOOP_EXIT);
DECLARE_STATE(tc, attach_wait_snk, NOOP_EXIT);
DECLARE_STATE(tc, attached_snk, WITH_EXIT);

/* Super States */
DECLARE_STATE(tc, host_rard, NOOP_EXIT);
DECLARE_STATE(tc, host_open, NOOP_EXIT);
DECLARE_STATE(tc, vbus_cc_iso, NOOP_EXIT);

void tc_state_init(int port)
{
	int res = 0;
	sm_state this_state;

	res = tc_restart_tcpc(port);

	CPRINTS("TCPC p%d init %s", port, res ? "failed" : "ready");
	this_state = res ? tc_disabled : PD_DEFAULT_STATE(port);

	/* Disable TCPC RX until connection is established */
	tcpm_set_rx_enable(port, 0);

	init_state(port, TC_OBJ(port), this_state);

	/* Disable pd state machines */
	tc[port].pd_enable = 0;
	tc[port].evt_timeout = 10*MSEC;
	tc[port].power_role = PD_PLUG_CABLE_VPD;
	tc[port].data_role = 0; /* Reserved for VPD */
	tc[port].flags = 0;
}

void tc_event_check(int port, int evt)
{
	/* Do Nothing */
}

/**
 * Disabled
 *
 * Super State Entries:
 *   Enable mcu communication
 *   Remove the terminations from Host CC
 */
static unsigned int tc_disabled(int port, enum signal sig)
{
	int ret = 0;

	ret = (*tc_disabled_sig[sig])(port);
	return SUPER(ret, sig, tc_host_open);
}

static unsigned int tc_disabled_entry(int port)
{
	tc[port].state_id = DISABLED;
	CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]);

	return 0;
}

static unsigned int tc_disabled_run(int port)
{
	task_wait_event(-1);

	return RUN_SUPER;
}

static unsigned int tc_disabled_exit(int port)
{
#ifndef CONFIG_USB_PD_TCPC
	if (tc_restart_tcpc(port) != 0) {
		CPRINTS("TCPC p%d restart failed!", port);
		return 0;
	}
#endif
	CPRINTS("TCPC p%d resumed!", port);
	set_state(port, TC_OBJ(port), tc_unattached_snk);

	return 0;
}

/**
 * Unattached.SNK
 *
 * Super State Entry:
 *   Enable mcu communication
 *   Place Ra on VCONN and Rd on Host CC
 */
static unsigned int tc_unattached_snk(int port, enum signal sig)
{
	int ret;

	ret = (*tc_unattached_snk_sig[sig])(port);
	return SUPER(ret, sig, tc_host_rard);
}

static unsigned int tc_unattached_snk_entry(int port)
{
	tc[port].state_id = UNATTACHED_SNK;
	CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]);

	return 0;
}

static unsigned int tc_unattached_snk_run(int port)
{
	int host_cc;

	/* Check Host CC for connection */
	vpd_host_get_cc(&host_cc);

	/*
	 * Transition to AttachWait.SNK when a Source connection is
	 * detected, as indicated by the SNK.Rp state on its Host-side
	 * port’s CC pin.
	 */
	if (cc_is_rp(host_cc)) {
		set_state(port, TC_OBJ(port), tc_attach_wait_snk);
		return 0;
	}

	return RUN_SUPER;
}

/**
 * AttachedWait.SNK
 *
 * Super State Entry:
 *   Enable mcu communication
 *   Place Ra on VCONN and Rd on Host CC
 */
static unsigned int tc_attach_wait_snk(int port, enum signal sig)
{
	int ret = 0;

	ret = (*tc_attach_wait_snk_sig[sig])(port);
	return SUPER(ret, sig, tc_host_rard);
}

static unsigned int tc_attach_wait_snk_entry(int port)
{
	tc[port].state_id = ATTACH_WAIT_SNK;
	CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]);
	tc[port].host_cc_state = PD_CC_UNSET;

	return 0;
}

static unsigned int tc_attach_wait_snk_run(int port)
{
	int host_new_cc_state;
	int host_cc;

	/* Check Host CC for connection */
	vpd_host_get_cc(&host_cc);

	if (cc_is_rp(host_cc))
		host_new_cc_state = PD_CC_DFP_ATTACHED;
	else
		host_new_cc_state = PD_CC_NONE;

	/* Debounce the Host CC state */
	if (tc[port].host_cc_state != host_new_cc_state) {
		tc[port].host_cc_state = host_new_cc_state;
		if (host_new_cc_state == PD_CC_DFP_ATTACHED)
			tc[port].cc_debounce = get_time().val +
							PD_T_CC_DEBOUNCE;
		else
			tc[port].cc_debounce = get_time().val +
							PD_T_PD_DEBOUNCE;

		return 0;
	}

	/* Wait for Host CC debounce */
	if (get_time().val < tc[port].cc_debounce)
		return 0;

	/*
	 * A VCONN-Powered USB Device shall transition to
	 * Attached.SNK after the state of the Host-side port’s CC pin is
	 * SNK.Rp for at least tCCDebounce and either host-side VCONN or
	 * VBUS is detected.
	 *
	 * Transition to Unattached.SNK when the state of both the CC1 and
	 * CC2 pins is SNK.Open for at least tPDDebounce.
	 */
	if (tc[port].host_cc_state == PD_CC_DFP_ATTACHED &&
			(vpd_is_vconn_present() || vpd_is_host_vbus_present()))
		set_state(port, TC_OBJ(port), tc_attached_snk);
	else if (tc[port].host_cc_state == PD_CC_NONE)
		set_state(port, TC_OBJ(port), tc_unattached_snk);

	return 0;
}

/**
 * Attached.SNK
 */
static unsigned int tc_attached_snk(int port, enum signal sig)
{
	int ret;

	ret = (*tc_attached_snk_sig[sig])(port);
	return SUPER(ret, sig, 0);
}

static unsigned int tc_attached_snk_entry(int port)
{
	tc[port].state_id = ATTACHED_SNK;
	CPRINTS("C%d: %s", port, tc_state_names[tc[port].state_id]);

	/* Enable PD */
	tc[port].pd_enable = 1;
	set_polarity(port, 0);

	return 0;
}

static unsigned int tc_attached_snk_run(int port)
{
	/* Has host vbus and vconn been removed */
	if (!vpd_is_host_vbus_present() && !vpd_is_vconn_present()) {
		set_state(port, TC_OBJ(port), tc_unattached_snk);
		return 0;
	}

	if (vpd_is_vconn_present()) {
		if (!(tc[port].flags & TC_FLAGS_VCONN_ON)) {
			/* VCONN detected. Remove RA */
			vpd_host_set_pull(TYPEC_CC_RD, 0);
			tc[port].flags |= TC_FLAGS_VCONN_ON;
		}
	}

	return 0;
}

static unsigned int tc_attached_snk_exit(int port)
{
	/* Disable PD */
	tc[port].pd_enable = 0;
	tc[port].flags &= ~TC_FLAGS_VCONN_ON;

	return 0;
}

/**
 * Super State HOST_RARD
 */
static unsigned int tc_host_rard(int port, enum signal sig)
{
	int ret;

	ret = (*tc_host_rard_sig[sig])(port);
	return SUPER(ret, sig, tc_vbus_cc_iso);
}

static unsigned int tc_host_rard_entry(int port)
{
	/* Place Ra on VCONN and Rd on Host CC */
	vpd_host_set_pull(TYPEC_CC_RA_RD, 0);

	return 0;
}

static unsigned int tc_host_rard_run(int port)
{
	return RUN_SUPER;
}

/**
 * Super State HOST_OPEN
 */
static unsigned int tc_host_open(int port, enum signal sig)
{
	int ret;

	ret = (*tc_host_open_sig[sig])(port);
	return SUPER(ret, sig, tc_vbus_cc_iso);
}

static unsigned int tc_host_open_entry(int port)
{
	/* Remove the terminations from Host CC */
	vpd_host_set_pull(TYPEC_CC_OPEN, 0);

	return 0;
}

static unsigned int tc_host_open_run(int port)
{
	return RUN_SUPER;
}

/**
 * Super State VBUS_CC_ISO
 */
static unsigned int tc_vbus_cc_iso(int port, enum signal sig)
{
	int ret;

	ret = (*tc_vbus_cc_iso_sig[sig])(port);
	return SUPER(ret, sig, 0);
}

static unsigned int tc_vbus_cc_iso_entry(int port)
{
	/* Enable mcu communication and cc */
	vpd_mcu_cc_en(1);

	return 0;
}

static unsigned int tc_vbus_cc_iso_run(int port)
{
	return 0;
}