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
|
/*
* 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.
*/
/* Type-C port manager for Nuvoton NCT38XX. */
#include "common.h"
#include "console.h"
#include "ioexpander_nct38xx.h"
#include "nct38xx.h"
#include "tcpci.h"
#if !defined(CONFIG_USB_PD_TCPM_TCPCI)
#error "NCT38XX is using part of standard TCPCI control"
#error "Please upgrade your board configuration"
#endif
#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ## args)
#define CPRINTS(format, args...) cprints(CC_USBPD, format, ## args)
#define POLARITY_NORMAL 0
#define POLARITY_FLIPPED 1
#define POLARITY_NONE 3
static int cable_polarity[CONFIG_USB_PD_PORT_MAX_COUNT];
static unsigned char txBuf[33];
static unsigned char rxBuf[33];
/* Save the selected rp value */
static int selected_rp[CONFIG_USB_PD_PORT_MAX_COUNT];
static int nct38xx_tcpm_init(int port)
{
int rv = 0;
int reg;
cable_polarity[port] = POLARITY_NONE;
rv = tcpci_tcpm_init(port);
if (rv)
return rv;
/*
* Write to the CONTROL_OUT_EN register to enable:
* [6] - CONNDIREN : Connector direction indication output enable
* [2] - SNKEN : VBUS sink enable output enable
* [0] - SRCEN : VBUS source voltage enable output enable
*/
reg = NCT38XX_REG_CTRL_OUT_EN_SRCEN |
NCT38XX_REG_CTRL_OUT_EN_SNKEN |
NCT38XX_REG_CTRL_OUT_EN_CONNDIREN;
rv = tcpc_write(port, NCT38XX_REG_CTRL_OUT_EN, reg);
if (rv)
return rv;
/* Disable OVP */
rv = tcpc_read(port, TCPC_REG_FAULT_CTRL, ®);
if (rv)
return rv;
reg = reg | TCPC_REG_FAULT_CTRL_VBUS_OVP_FAULT_DIS;
rv = tcpc_write(port, TCPC_REG_FAULT_CTRL, reg);
if (rv)
return rv;
/* Enable VBus monitor and Disable FRS */
rv = tcpc_read(port, TCPC_REG_POWER_CTRL, ®);
if (rv)
return rv;
reg = reg & ~(TCPC_REG_POWER_CTRL_VBUS_VOL_MONITOR_DIS |
TCPC_REG_POWER_CTRL_FRS_ENABLE);
rv = tcpc_write(port, TCPC_REG_POWER_CTRL, reg);
if (rv)
return rv;
/* Set FRS direction for SNK detect, if FRS is enabled */
if (IS_ENABLED(CONFIG_USB_TYPEC_PD_FAST_ROLE_SWAP)) {
reg = TCPC_REG_DEV_CAP_2_SNK_FR_SWAP;
rv = tcpc_write(port, TCPC_REG_DEV_CAP_2, reg);
if (rv)
return rv;
reg = TCPC_REG_CONFIG_EXT_1_FR_SWAP_SNK_DIR;
rv = tcpc_write(port, TCPC_REG_CONFIG_EXT_1, reg);
if (rv)
return rv;
}
/* Start VBus monitor */
rv = tcpc_write(port, TCPC_REG_COMMAND,
TCPC_REG_COMMAND_ENABLE_VBUS_DETECT);
/*
* Enable the Vendor Define alert event only when the IO expander
* feature is defined
*/
if (IS_ENABLED(CONFIG_IO_EXPANDER_NCT38XX)) {
int mask;
rv |= tcpc_read16(port, TCPC_REG_ALERT_MASK, &mask);
mask |= TCPC_REG_ALERT_VENDOR_DEF;
rv |= tcpc_write16(port, TCPC_REG_ALERT_MASK, mask);
}
return rv;
}
static int tcpci_nct38xx_check_cable_polarity(int port)
{
int cc, rv;
/* Try to check the polarity */
rv = tcpc_read(port, TCPC_REG_CC_STATUS, &cc);
if (rv)
return rv;
if (TCPC_REG_CC_STATUS_TERM(cc)) {
/* TCPC is presenting RD (Sink mode) */
if ((TCPC_REG_CC_STATUS_CC1(cc) != TYPEC_CC_VOLT_OPEN) &&
(TCPC_REG_CC_STATUS_CC2(cc) == TYPEC_CC_VOLT_OPEN)) {
/* CC1 active && CC2 open */
cable_polarity[port] = POLARITY_NORMAL;
}
if ((TCPC_REG_CC_STATUS_CC1(cc) == TYPEC_CC_VOLT_OPEN) &&
(TCPC_REG_CC_STATUS_CC2(cc) != TYPEC_CC_VOLT_OPEN)) {
/* CC1 open && CC2 active */
cable_polarity[port] = POLARITY_FLIPPED;
}
} else {
/* TCPC is presenting RP (Source mode) */
if ((TCPC_REG_CC_STATUS_CC1(cc) == TYPEC_CC_VOLT_RD) &&
(TCPC_REG_CC_STATUS_CC2(cc) != TYPEC_CC_VOLT_RD)) {
/* CC1 active && CC2 open */
cable_polarity[port] = POLARITY_NORMAL;
}
if ((TCPC_REG_CC_STATUS_CC1(cc) != TYPEC_CC_VOLT_RD) &&
(TCPC_REG_CC_STATUS_CC2(cc) == TYPEC_CC_VOLT_RD)) {
/* CC1 open && CC2 active */
cable_polarity[port] = POLARITY_FLIPPED;
}
}
return rv;
}
/*
* TODO(crbug.com/951681): This code can be simplified once that bug is fixed.
*/
static int tcpci_nct38xx_set_cc(int port, int pull)
{
int rv;
if (cable_polarity[port] == POLARITY_NONE) {
rv = tcpci_nct38xx_check_cable_polarity(port);
if (rv)
return rv;
}
if (cable_polarity[port] == POLARITY_NORMAL) {
rv = tcpc_write(port, TCPC_REG_ROLE_CTRL,
TCPC_REG_ROLE_CTRL_SET(0, selected_rp[port],
pull, TYPEC_CC_OPEN));
} else if (cable_polarity[port] == POLARITY_FLIPPED) {
rv = tcpc_write(port, TCPC_REG_ROLE_CTRL,
TCPC_REG_ROLE_CTRL_SET(0, selected_rp[port],
TYPEC_CC_OPEN, pull));
} else {
return -1;
}
return rv;
}
static int tcpci_nct38xx_get_cc(int port, enum tcpc_cc_voltage_status *cc1,
enum tcpc_cc_voltage_status *cc2)
{
int rv;
int rc;
rv = tcpc_read(port, TCPC_REG_ROLE_CTRL, &rc);
if (rv)
return rv;
rv = tcpci_tcpm_get_cc(port, cc1, cc2);
if (rv)
return rv;
if (!TCPC_REG_ROLE_CTRL_DRP(rc)) {
if ((*cc1 != TYPEC_CC_VOLT_OPEN) &&
(TCPC_REG_ROLE_CTRL_CC1(rc) == TYPEC_CC_RD))
*cc1 |= 0x4;
if ((*cc2 != TYPEC_CC_VOLT_OPEN) &&
(TCPC_REG_ROLE_CTRL_CC2(rc) == TYPEC_CC_RD))
*cc2 |= 0x04;
}
return rv;
}
int tcpci_nct38xx_drp_toggle(int port)
{
cable_polarity[port] = POLARITY_NONE;
return tcpci_tcpc_drp_toggle(port);
}
int tcpci_nct38xx_set_polarity(int port, int polarity)
{
int rv, reg;
rv = tcpc_read(port, TCPC_REG_TCPC_CTRL, ®);
if (rv)
return rv;
reg = polarity ? (reg | TCPC_REG_TCPC_CTRL_SET(1)) :
(reg & ~TCPC_REG_TCPC_CTRL_SET(1));
return tcpc_write(port, TCPC_REG_TCPC_CTRL, reg);
}
int tcpci_nct38xx_transmit(int port, enum tcpm_transmit_type type,
uint16_t header, const uint32_t *data)
{
int rv, num_obj_byte;
num_obj_byte = PD_HEADER_CNT(header) * 4;
txBuf[0] = num_obj_byte + 2;
txBuf[1] = (unsigned char)(header & 0xFF);
txBuf[2] = (unsigned char)((header >> 8) & 0xFF);
if (num_obj_byte) {
uint32_t *buf_ptr;
buf_ptr = (uint32_t *)&txBuf[3];
memcpy(buf_ptr, data, num_obj_byte);
}
/* total write size = size header (1 byte) + message size */
rv = tcpc_write_block(port, TCPC_REG_TX_BYTE_CNT,
(const uint8_t *)txBuf, txBuf[0] + 1);
rv = tcpc_write(port, TCPC_REG_TRANSMIT,
TCPC_REG_TRANSMIT_SET_WITH_RETRY(type));
return rv;
}
static int tcpci_nct38xx_get_message_raw(int port, uint32_t *payload, int *head)
{
int rv, cnt, num_obj_byte;
rv = tcpc_read(port, TCPC_REG_RX_BYTE_CNT, &cnt);
if (rv != EC_SUCCESS || cnt < 3) {
rv = EC_ERROR_UNKNOWN;
goto clear;
}
rv = tcpc_read_block(port, TCPC_REG_RX_BYTE_CNT, rxBuf, cnt + 1);
if (rv != EC_SUCCESS)
goto clear;
*head = *(int *)&rxBuf[2];
num_obj_byte = PD_HEADER_CNT(*head) * 4;
if (num_obj_byte) {
uint32_t *buf_ptr;
buf_ptr = (uint32_t *)&rxBuf[4];
memcpy(payload, buf_ptr, num_obj_byte);
}
clear:
/* Read complete, clear RX status alert bit */
tcpc_write16(port, TCPC_REG_ALERT, TCPC_REG_ALERT_RX_STATUS);
return rv;
}
static void nct38xx_tcpc_alert(int port)
{
int alert, rv;
/*
* If IO expander feature is defined, read the ALERT register first to
* keep the status of Vendor Define bit. Otherwise, the status of ALERT
* register will be cleared after tcpci_tcpc_alert() is executed.
*/
if (IS_ENABLED(CONFIG_IO_EXPANDER_NCT38XX))
rv = tcpc_read16(port, TCPC_REG_ALERT, &alert);
/* Process normal TCPC ALERT event and clear status */
tcpci_tcpc_alert(port);
/*
* If IO expander feature is defined, check the Vendor Define bit to
* handle the IOEX IO's interrupt event
*/
if (IS_ENABLED(CONFIG_IO_EXPANDER_NCT38XX))
if (!rv && (alert & TCPC_REG_ALERT_VENDOR_DEF))
nct38xx_ioex_event_handler(port);
}
const struct tcpm_drv nct38xx_tcpm_drv = {
.init = &nct38xx_tcpm_init,
.release = &tcpci_tcpm_release,
.get_cc = &tcpci_nct38xx_get_cc,
#ifdef CONFIG_USB_PD_VBUS_DETECT_TCPC
.get_vbus_level = &tcpci_tcpm_get_vbus_level,
#endif
.select_rp_value = &tcpci_tcpm_select_rp_value,
.set_cc = &tcpci_nct38xx_set_cc,
.set_polarity = &tcpci_nct38xx_set_polarity,
.set_vconn = &tcpci_tcpm_set_vconn,
.set_msg_header = &tcpci_tcpm_set_msg_header,
.set_rx_enable = &tcpci_tcpm_set_rx_enable,
.get_message_raw = &tcpci_nct38xx_get_message_raw,
.transmit = &tcpci_nct38xx_transmit,
.tcpc_alert = &nct38xx_tcpc_alert,
#ifdef CONFIG_USB_PD_DISCHARGE_TCPC
.tcpc_discharge_vbus = &tcpci_tcpc_discharge_vbus,
#endif
.tcpc_connect_state_change =
&tcpci_tcpc_connect_state_change,
#ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE
.drp_toggle = &tcpci_nct38xx_drp_toggle,
#endif
#ifdef CONFIG_USBC_PPC
.set_snk_ctrl = &tcpci_tcpm_set_snk_ctrl,
.set_src_ctrl = &tcpci_tcpm_set_src_ctrl,
#endif
.get_chip_info = &tcpci_get_chip_info,
#ifdef CONFIG_USB_PD_TCPC_LOW_POWER
.enter_low_power_mode = &tcpci_enter_low_power_mode,
#endif
#ifdef CONFIG_USB_TYPEC_PD_FAST_ROLE_SWAP
.set_frs_enable = &tcpci_tcpc_fast_role_swap_enable,
#endif
};
|