summaryrefslogtreecommitdiff
path: root/board/servo_v4p1/usb_tc_snk_sm.c
blob: f9a396643438cde073f92afd94d8e3725bcdb6cb (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
/* 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.
 */

#include "common.h"
#include "console.h"
#include "fusb302b.h"
#include "ioexpanders.h"
#include "system.h"
#include "task.h"
#include "usb_common.h"
#include "usb_pd.h"
#include "usb_sm.h"
#include "usb_tc_sm.h"

#define EVT_TIMEOUT_NEVER  (-1)
#define EVT_TIMEOUT_5MS    (5 * MSEC)

/*
 * USB Type-C Sink
 *   See Figure 4-13 in Release 1.4 of USB Type-C Spec.
 */
#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ## args)
#define CPRINTS(format, args...) cprints(CC_USBPD, format, ## args)

/* Type-C Layer Flags */

/* List of all TypeC-level states */
enum usb_tc_state {
	TC_UNATTACHED_SNK,
	TC_ATTACH_WAIT_SNK,
	TC_ATTACHED_SNK,
};
/* Forward declare the full list of states. This is indexed by usb_tc_state */
static const struct usb_state tc_states[];

/* TypeC Power strings */
static const char * const pwr2_5_str = "5V/0.5A";
static const char * const pwr7_5_str = "5V/1.5A";
static const char * const pwr15_str  = "5V/3A";

static struct type_c {
	/* state machine context */
	struct sm_ctx ctx;
	/* Port polarity */
	enum tcpc_cc_polarity polarity;
	/* event timeout */
	uint64_t evt_timeout;
	/* Time a port shall wait before it can determine it is attached */
	uint64_t cc_debounce;
	/*
	 * Time a Sink port shall wait before it can determine it is detached
	 * due to the potential for USB PD signaling on CC as described in
	 * the state definitions.
	 */
	uint64_t pd_debounce;
	/* The cc state */
	enum pd_cc_states cc_state;
	/* Generic timer */
	uint64_t timeout;
	/* Voltage on CC pin */
	enum tcpc_cc_voltage_status cc_voltage;
	/* Current CC1 value */
	int cc1;
	/* Current CC2 value */
	int cc2;
} tc;

/* Forward declare common, private functions */
static void set_state_tc(const enum usb_tc_state new_state);

static void restart_tc_sm(enum usb_tc_state start_state)
{
	int res;

	res = init_fusb302b(1);
	CPRINTS("FUSB302b init %s", res ? "failed" : "ready");

	/* State machine is disabled if init_fusb302b fails */
	if (!res)
		set_state_tc(start_state);

	/* Disable timeout. Task will wake on interrupt */
	tc.evt_timeout = EVT_TIMEOUT_NEVER;
}

/*
 * Private Functions
 */

/* Set the TypeC state machine to a new state. */
static void set_state_tc(const enum usb_tc_state new_state)
{
	set_state(0, &tc.ctx, &tc_states[new_state]);
}

static void print_alt_power(void)
{
	enum tcpc_cc_voltage_status cc;
	char const *pwr;

	cc = tc.polarity ? tc.cc2 : tc.cc1;
	if (cc == TYPEC_CC_VOLT_OPEN ||
		cc == TYPEC_CC_VOLT_RA || cc == TYPEC_CC_VOLT_RD) {
		/* Supply removed or not detected */
		return;
	}

	if (cc == TYPEC_CC_VOLT_RP_1_5)
		pwr = pwr7_5_str;
	else if (cc == TYPEC_CC_VOLT_RP_3_0)
		pwr = pwr15_str;
	else
		pwr = pwr2_5_str;

	CPRINTS("ALT: Switching to alternate supply @ %s", pwr);
}

static void sink_power_sub_states(void)
{
	enum tcpc_cc_voltage_status cc;
	enum tcpc_cc_voltage_status new_cc_voltage;

	cc = tc.polarity ? tc.cc2 : tc.cc1;

	if (cc == TYPEC_CC_VOLT_RP_DEF)
		new_cc_voltage = TYPEC_CC_VOLT_RP_DEF;
	else if (cc == TYPEC_CC_VOLT_RP_1_5)
		new_cc_voltage = TYPEC_CC_VOLT_RP_1_5;
	else if (cc == TYPEC_CC_VOLT_RP_3_0)
		new_cc_voltage = TYPEC_CC_VOLT_RP_3_0;
	else
		new_cc_voltage = TYPEC_CC_VOLT_OPEN;

	/* Debounce the cc state */
	if (new_cc_voltage != tc.cc_voltage) {
		tc.cc_voltage = new_cc_voltage;
		tc.cc_debounce = get_time().val + PD_T_RP_VALUE_CHANGE;
		return;
	}

	if (tc.cc_debounce == 0 || get_time().val < tc.cc_debounce)
		return;

	tc.cc_debounce = 0;
	print_alt_power();
}

/*
 * TYPE-C State Implementations
 */

/**
 * Unattached.SNK
 */
static void tc_unattached_snk_entry(int port)
{
	tc.evt_timeout = EVT_TIMEOUT_NEVER;
}

static void tc_unattached_snk_run(int port)
{
	/*
	 * The port shall transition to AttachWait.SNK when a Source
	 * connection is detected, as indicated by the SNK.Rp state
	 * on at least one of its CC pins.
	 */
	if (cc_is_rp(tc.cc1) || cc_is_rp(tc.cc2))
		set_state_tc(TC_ATTACH_WAIT_SNK);
}

/**
 * AttachWait.SNK
 */
static void tc_attach_wait_snk_entry(int port)
{
	tc.evt_timeout = EVT_TIMEOUT_5MS;
	tc.cc_state = PD_CC_UNSET;
}

static void tc_attach_wait_snk_run(int port)
{
	enum pd_cc_states new_cc_state;

	if (cc_is_rp(tc.cc1) && cc_is_rp(tc.cc2))
		new_cc_state = PD_CC_DFP_DEBUG_ACC;
	else if (cc_is_rp(tc.cc1) || cc_is_rp(tc.cc2))
		new_cc_state = PD_CC_DFP_ATTACHED;
	else
		new_cc_state = PD_CC_NONE;

	/* Debounce the cc state */
	if (new_cc_state != tc.cc_state) {
		tc.cc_debounce = get_time().val + PD_T_CC_DEBOUNCE;
		tc.pd_debounce = get_time().val + PD_T_PD_DEBOUNCE;
		tc.cc_state = new_cc_state;
		return;
	}

	/* Wait for CC debounce */
	if (get_time().val < tc.cc_debounce)
		return;

	/*
	 * The port shall transition to Attached.SNK after the state of only
	 * one of the CC1 or CC2 pins is SNK.Rp for at least tCCDebounce and
	 * VBUS is detected.
	 */
	if (is_vbus_present() && (new_cc_state == PD_CC_DFP_ATTACHED))
		set_state_tc(TC_ATTACHED_SNK);
	else
		set_state_tc(TC_UNATTACHED_SNK);
}

/**
 * Attached.SNK
 */
static void tc_attached_snk_entry(int port)
{
	print_alt_power();

	tc.evt_timeout = EVT_TIMEOUT_NEVER;
	tc.cc_debounce = 0;

	/* Switch over to alternate supply */
	en_pp5000_alt_3p3(1);
}

static void tc_attached_snk_run(int port)
{
	/* Detach detection */
	if (!is_vbus_present()) {
		set_state_tc(TC_UNATTACHED_SNK);
		return;
	}

	/* Run Sink Power Sub-State */
	sink_power_sub_states();
}

static void tc_attached_snk_exit(int port)
{
	/* Alternate charger removed. Switch back to host power */
	en_pp5000_alt_3p3(0);
}

/*
 * Type-C State
 *
 * TC_UNATTACHED_SNK
 * TC_ATTACH_WAIT_SNK
 * TC_TRY_WAIT_SNK
 * TC_ATTACHED_SNK
 */
static const struct usb_state tc_states[] = {
	[TC_UNATTACHED_SNK] = {
		.entry	= tc_unattached_snk_entry,
		.run	= tc_unattached_snk_run,
	},
	[TC_ATTACH_WAIT_SNK] = {
		.entry	= tc_attach_wait_snk_entry,
		.run	= tc_attach_wait_snk_run,
	},
	[TC_ATTACHED_SNK] = {
		.entry	= tc_attached_snk_entry,
		.run	= tc_attached_snk_run,
		.exit	= tc_attached_snk_exit,
	},
};

void snk_task(void *u)
{
	/* Unattached.SNK is the default starting state. */
	restart_tc_sm(TC_UNATTACHED_SNK);

	while (1) {
		/* wait for next event or timeout expiration */
		task_wait_event(tc.evt_timeout);

		/* Sample CC lines */
		get_cc(&tc.cc1, &tc.cc2);

		/* Detect polarity */
		tc.polarity = (tc.cc1 > tc.cc2) ? POLARITY_CC1 : POLARITY_CC2;

		/* Run TypeC state machine */
		run_state(0, &tc.ctx);
	}
}