Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
usb_pd.h
1/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6/* USB Power delivery module */
7
8#ifndef __USB_PD_H
9#define __USB_PD_H
10
11#ifdef __cplusplus
12extern "C" {
13#endif
14
15#include "config.h"
16#include "task.h"
17#include "../../../../src/system/driver/pd/tcpm_driver.h"
18#include "../../../../src/system/driver/pd/usb_pd_driver.h"
19
20/* Time units in microseconds */
21#define MSEC_US (1000ul)
22#define SECOND_US (1000000ul)
23#define MINUTE_US (60000000ul)
24#define HOUR_US (3600000000ul) /* Too big to fit in a signed int */
25
26/* PD Host command timeout */
27#define PD_HOST_COMMAND_TIMEOUT_US SECOND_US
28
29 enum pd_rx_errors
30 {
31 PD_RX_ERR_INVAL = -1, /* Invalid packet */
32 PD_RX_ERR_HARD_RESET = -2, /* Got a Hard-Reset packet */
33 PD_RX_ERR_CRC = -3, /* CRC mismatch */
34 PD_RX_ERR_ID = -4, /* Invalid ID number */
35 PD_RX_ERR_UNSUPPORTED_SOP = -5, /* Unsupported SOP */
36 PD_RX_ERR_CABLE_RESET = -6 /* Got a Cable-Reset packet */
37 };
38
39/* Events for USB PD task */
40#define PD_EVENT_TX (1 << 3) /* Outgoing packet event */
41#define PD_EVENT_CC (1 << 4) /* CC line change event */
42#define PD_EVENT_TCPC_RESET (1 << 5) /* TCPC has reset */
43#define PD_EVENT_UPDATE_DUAL_ROLE (1 << 6) /* DRP state has changed */
44#define PD_EVENT_DEVICE_ACCESSED (1 << 7)
45#define PD_EVENT_POWER_STATE_CHANGE (1 << 8)
46#define PD_EVENT_RX_HARD_RESET (1 << 11) /* Receive a Hard Reset. */
47#define PD_EVENT_RESET_ALLOWED (1 << 12) /* The sink can now reset the source, see pd_set_reset_allowed */
48
49/* --- PD data message helpers --- */
50#define PDO_MAX_OBJECTS 7
51#define PDO_MODES (PDO_MAX_OBJECTS - 1)
52
53/* PDO : Power Data Object */
54/*
55 * 1. The vSafe5V Fixed Supply Object shall always be the first object.
56 * 2. The remaining Fixed Supply Objects,
57 * if present, shall be sent in voltage order; lowest to highest.
58 * 3. The Battery Supply Objects,
59 * if present shall be sent in Minimum Voltage order; lowest to highest.
60 * 4. The Variable Supply (non battery) Objects,
61 * if present, shall be sent in Minimum Voltage order; lowest to highest.
62 * 5. (PD3.0) The Augmented PDO is defined to allow extension beyond the 4 PDOs
63 * above by examining bits <29:28> to determine the additional PDO function.
64 */
65#define PDO_TYPE_FIXED (0 << 30)
66#define PDO_TYPE_BATTERY (1 << 30)
67#define PDO_TYPE_VARIABLE (2 << 30)
68#define PDO_TYPE_AUGMENTED (3 << 30)
69#define PDO_TYPE_MASK (3 << 30)
70
71#define PDO_FIXED_DUAL_ROLE (1L << 29) /* Dual role device */
72#define PDO_FIXED_SUSPEND (1L << 28) /* USB Suspend supported */
73#define PDO_FIXED_EXTERNAL (1L << 27) /* Externally powered */
74#define PDO_FIXED_COMM_CAP (1L << 26) /* USB Communications Capable */
75#define PDO_FIXED_DATA_SWAP (1L << 25) /* Data role swap command supported */
76#define PDO_FIXED_PEAK_CURR () /* [21..20] Peak current */
77#define PDO_FIXED_VOLT(mv) (((mv) / 50L) << 10) /* Voltage in 50mV units */
78#define PDO_FIXED_CURR(ma) (((ma) / 10L) << 0) /* Max current in 10mA units */
79
80#define PDO_FIXED(mv, ma, flags) (PDO_FIXED_VOLT(mv) | PDO_FIXED_CURR(ma) | (flags))
81
82#define PDO_VAR_MAX_VOLT(mv) ((((mv) / 50L) & 0x3FF) << 20)
83#define PDO_VAR_MIN_VOLT(mv) ((((mv) / 50L) & 0x3FF) << 10)
84#define PDO_VAR_OP_CURR(ma) ((((ma) / 10L) & 0x3FF) << 0)
85
86#define PDO_VAR(min_mv, max_mv, op_ma) \
87 (PDO_VAR_MIN_VOLT(min_mv) | PDO_VAR_MAX_VOLT(max_mv) | PDO_VAR_OP_CURR(op_ma) | PDO_TYPE_VARIABLE)
88
89#define PDO_BATT_MAX_VOLT(mv) ((((mv) / 50L) & 0x3FF) << 20)
90#define PDO_BATT_MIN_VOLT(mv) ((((mv) / 50L) & 0x3FF) << 10)
91#define PDO_BATT_OP_POWER(mw) ((((mw) / 250L) & 0x3FF) << 0)
92
93#define PDO_BATT(min_mv, max_mv, op_mw) \
94 (PDO_BATT_MIN_VOLT(min_mv) | PDO_BATT_MAX_VOLT(max_mv) | PDO_BATT_OP_POWER(op_mw) | PDO_TYPE_BATTERY)
95
96/* RDO : Request Data Object */
97#define RDO_OBJ_POS(n) (((n) & 0x7) << 28)
98#define RDO_POS(rdo) (((rdo) >> 28) & 0x7)
99#define RDO_GIVE_BACK (1 << 27)
100#define RDO_CAP_MISMATCH (1 << 26)
101#define RDO_COMM_CAP (1 << 25)
102#define RDO_NO_SUSPEND (1 << 24)
103#define RDO_FIXED_VAR_OP_CURR(ma) ((((ma) / 10) & 0x3FF) << 10)
104#define RDO_FIXED_VAR_MAX_CURR(ma) ((((ma) / 10) & 0x3FF) << 0)
105
106#define RDO_BATT_OP_POWER(mw) ((((mw) / 250) & 0x3FF) << 10)
107#define RDO_BATT_MAX_POWER(mw) ((((mw) / 250) & 0x3FF) << 10)
108
109#define RDO_FIXED(n, op_ma, max_ma, flags) \
110 (RDO_OBJ_POS(n) | (flags) | RDO_FIXED_VAR_OP_CURR(op_ma) | RDO_FIXED_VAR_MAX_CURR(max_ma))
111
112#define RDO_BATT(n, op_mw, max_mw, flags) \
113 (RDO_OBJ_POS(n) | (flags) | RDO_BATT_OP_POWER(op_mw) | RDO_BATT_MAX_POWER(max_mw))
114
115/* BDO : BIST Data Object */
116#define BDO_MODE_RECV (0 << 28)
117#define BDO_MODE_TRANSMIT (1 << 28)
118#define BDO_MODE_COUNTERS (2 << 28)
119#define BDO_MODE_CARRIER0 (3 << 28)
120#define BDO_MODE_CARRIER1 (4 << 28)
121#define BDO_MODE_CARRIER2 (5 << 28)
122#define BDO_MODE_CARRIER3 (6 << 28)
123#define BDO_MODE_EYE (7 << 28)
124
125#define BDO(mode, cnt) ((mode) | ((cnt) & 0xFFFF))
126
127#define SVID_DISCOVERY_MAX 16
128
129/* Timers */
130#define PD_T_SINK_TX (18 * MSEC_US) /* between 16ms and 20 */
131#define PD_T_CHUNKING_NOT_SUPPORTED (45 * MSEC_US) /* between 40ms and 50ms */
132#define PD_T_CHUNK_SENDER_RSP (24 * MSEC_US) /* between 24ms and 30ms */
133#define PD_T_CHUNK_SENDER_REQ (24 * MSEC_US) /* between 24ms and 30ms */
134#define PD_T_HARD_RESET_COMPLETE (5 * MSEC_US) /* between 4ms and 5ms*/
135#define PD_T_HARD_RESET_RETRY (1 * MSEC_US) /* 1ms */
136#define PD_T_SEND_SOURCE_CAP (100 * MSEC_US) /* between 100ms and 200ms */
137#define PD_T_SINK_WAIT_CAP (600 * MSEC_US) /* between 310ms and 620ms */
138#define PD_T_SINK_TRANSITION (35 * MSEC_US) /* between 20ms and 35ms */
139#define PD_T_SRC_PS_RDY_MAX (400 * MSEC_US) /* source: latest PS_RDY after Accept, < tPSTransition */
140#define PD_T_SOURCE_ACTIVITY (45 * MSEC_US) /* between 40ms and 50ms */
141#define PD_T_SENDER_RESPONSE (30 * MSEC_US) /* between 24ms and 30ms */
142#define PD_T_PS_TRANSITION (500 * MSEC_US) /* between 450ms and 550ms */
143#define PD_T_PS_SOURCE_ON (480 * MSEC_US) /* between 390ms and 480ms */
144#define PD_T_PS_SOURCE_OFF (920 * MSEC_US) /* between 750ms and 920ms */
145#define PD_T_PS_HARD_RESET (25 * MSEC_US) /* between 25ms and 35ms */
146#define PD_T_ERROR_RECOVERY (240 * MSEC_US) /* min 240ms if sourcing VConn */
147#define PD_T_CC_DEBOUNCE (100 * MSEC_US) /* between 100ms and 200ms */
148/* DRP_SNK + DRP_SRC must be between 50ms and 100ms with 30%-70% duty cycle */
149#define PD_T_DRP_SNK (40 * MSEC_US) /* toggle time for sink DRP */
150#define PD_T_DRP_SRC (30 * MSEC_US) /* toggle time for source DRP */
151#define PD_T_DEBOUNCE (15 * MSEC_US) /* between 10ms and 20ms */
152#define PD_T_TRY_CC_DEBOUNCE (15 * MSEC_US) /* between 10ms and 20ms */
153#define PD_T_SINK_ADJ (55 * MSEC_US) /* between tPDDebounce and 60ms */
154#define PD_T_SRC_RECOVER (760 * MSEC_US) /* between 660ms and 1000ms */
155#define PD_T_SRC_RECOVER_MAX (1000 * MSEC_US) /* 1000ms */
156#define PD_T_SRC_TURN_ON (275 * MSEC_US) /* 275ms */
157#define PD_T_SAFE_0V (650 * MSEC_US) /* 650ms */
158#define PD_T_NO_RESPONSE (5500 * MSEC_US) /* between 4.5s and 5.5s */
159#define PD_T_BIST_TRANSMIT (50 * MSEC_US) /* 50ms (for task_wait arg) */
160#define PD_T_BIST_RECEIVE (60 * MSEC_US) /* 60ms (time to process bist) */
161#define PD_T_BIST_CONT_MODE (60 * MSEC_US) /* 30ms to 60ms */
162#define PD_T_VCONN_SOURCE_ON (100 * MSEC_US) /* 100ms */
163#define PD_T_DRP_TRY (125 * MSEC_US) /* between 75ms and 150ms */
164#define PD_T_TRY_TIMEOUT (550 * MSEC_US) /* between 550ms and 1100ms */
165#define PD_T_TRY_WAIT (600 * MSEC_US) /* Wait time for TryWait.SNK */
166#define PD_T_SINK_REQUEST (100 * MSEC_US) /* 100ms before next request */
167#define PD_T_PD_DEBOUNCE (15 * MSEC_US) /* between 10ms and 20ms */
168#define PD_T_CHUNK_SENDER_RESPONSE (25 * MSEC_US) /* 25ms */
169#define PD_T_CHUNK_SENDER_REQUEST (25 * MSEC_US) /* 25ms */
170#define PD_T_SWAP_SOURCE_START (25 * MSEC_US) /* Min of 20ms */
171#define PD_T_RP_VALUE_CHANGE (20 * MSEC_US) /* 20ms */
172#define PD_T_SRC_DISCONNECT (15 * MSEC_US) /* 15ms */
173#define PD_T_VCONN_STABLE (50 * MSEC_US) /* 50ms */
174#define PD_T_DISCOVER_IDENTITY (45 * MSEC_US) /* between 40ms and 50ms */
175#define PD_T_SYSJUMP (1000 * MSEC_US) /* 1s */
176#define PD_T_PR_SWAP_WAIT (100 * MSEC_US) /* tPRSwapWait 100ms */
177/* power role swap to source: the old source waits our PS_RDY tPSSourceOn (390ms min) after its own PS_RDY */
178#define PD_T_SWAP_NEW_SRC_MAX (380 * MSEC_US)
179
180/* number of edges and time window to detect CC line is not idle */
181#define PD_RX_TRANSITION_COUNT 3
182#define PD_RX_TRANSITION_WINDOW 20 /* between 12us and 20us */
183
184/* from USB Type-C Specification Table 5-1 */
185#define PD_T_AME (1 * SECOND_US) /* timeout from UFP attach to Alt Mode Entry */
186
187/* VDM Timers ( USB PD Spec Rev2.0 Table 6-30 )*/
188#define PD_T_VDM_BUSY (100 * MSEC_US) /* at least 100ms */
189#define PD_T_VDM_E_MODE (25 * MSEC_US) /* enter/exit the same max */
190#define PD_T_VDM_RCVR_RSP (15 * MSEC_US) /* max of 15ms */
191#define PD_T_VDM_SNDR_RSP (30 * MSEC_US) /* max of 30ms */
192#define PD_T_VDM_WAIT_MODE_E (100 * MSEC_US) /* enter/exit the same max */
193
194 enum pd_drp_next_states
195 {
196 DRP_TC_DEFAULT,
197 DRP_TC_UNATTACHED_SNK,
198 DRP_TC_ATTACHED_WAIT_SNK,
199 DRP_TC_UNATTACHED_SRC,
200 DRP_TC_ATTACHED_WAIT_SRC,
201 DRP_TC_DRP_AUTO_TOGGLE
202 };
203
204 /* function table for entered mode */
205 struct amode_fx
206 {
207 int (*status)(uint32_t* payload);
208 int (*config)(uint32_t* payload);
209 };
210
211 /* function table for alternate mode capable responders */
213 {
214 int (*identity)(uint32_t* payload);
215 int (*svids)(uint32_t* payload);
216 int (*modes)(uint32_t* payload);
217 int (*enter_mode)(uint32_t* payload);
218 int (*exit_mode)(uint32_t* payload);
219 struct amode_fx* amode;
220 };
221
223 {
224 uint16_t svid;
225 int mode_cnt;
226 uint32_t mode_vdo[PDO_MODES];
227 };
228
230 {
231 uint16_t svid;
232 int (*enter)(uint32_t mode_caps);
233 int (*status)(uint32_t* payload);
234 int (*config)(uint32_t* payload);
235 void (*post_config)();
236 int (*attention)(uint32_t* payload);
237 void (*exit)();
238 };
239
240 /* defined in <board>/usb_pd_policy.c */
241 /* All UFP_U should have */
242 extern const struct svdm_response svdm_rsp;
243 /* All DFP_U should have */
244 extern const struct svdm_amode_fx supported_modes[];
245 extern const int supported_modes_cnt;
246
247 /* DFP data needed to support alternate mode entry and exit */
249 {
250 const struct svdm_amode_fx* fx;
251 /* VDM object position */
252 int opos;
253 /* mode capabilities specific to SVID amode. */
254 struct svdm_svid_data* data;
255 };
256
257 enum hpd_event
258 {
259 hpd_none,
260 hpd_low,
261 hpd_high,
262 hpd_irq,
263 };
264
265/* DisplayPort flags */
266#define DP_FLAGS_DP_ON (1 << 0) /* Display port mode is on */
267#define DP_FLAGS_HPD_HI_PENDING (1 << 1) /* Pending HPD_HI */
268
269 /* supported alternate modes */
270 enum pd_alternate_modes
271 {
272 PD_AMODE_GOOGLE,
273 PD_AMODE_DISPLAYPORT,
274 /* not a real mode */
275 PD_AMODE_COUNT,
276 };
277
278 /* Policy structure for driving alternate mode */
280 {
281 /* index of svid currently being operated on */
282 int svid_idx;
283 /* count of svids discovered */
284 int svid_cnt;
285 /* SVDM identity info (Id, Cert Stat, 0-4 Typec specific) */
286 uint32_t identity[PDO_MAX_OBJECTS - 1];
287 /* supported svids & corresponding vdo mode data */
288 struct svdm_svid_data svids[SVID_DISCOVERY_MAX];
289 /* active modes */
290 struct svdm_amode_data amodes[PD_AMODE_COUNT];
291 /* Next index to insert DFP alternate mode into amodes */
292 int amode_idx;
293 };
294
295 /*
296 * VDO : Vendor Defined Message Object
297 * VDM object is minimum of VDM header + 6 additional data objects.
298 */
299
300#define VDO_MAX_SIZE 7
301
302#define VDM_VER10 0
303#define VDM_VER20 1
304
305/*
306 * VDM header
307 * ----------
308 * <31:16> :: SVID
309 * <15> :: VDM type ( 1b == structured, 0b == unstructured )
310 * <14:13> :: Structured VDM version (00b == Rev 2.0, 01b == Rev 3.0 )
311 * <12:11> :: reserved
312 * <10:8> :: object position (1-7 valid ... used for enter/exit mode only)
313 * <7:6> :: command type (SVDM only?)
314 * <5> :: reserved (SVDM), command type (UVDM)
315 * <4:0> :: command
316 */
317#define VDO(vid, type, custom) (((vid) << 16) | ((type) << 15) | ((custom) & 0x7FFF))
318
319#define VDO_SVDM_TYPE (1 << 15)
320#define VDO_SVDM_VERS(x) (x << 13)
321#define VDO_OPOS(x) (x << 8)
322#define VDO_CMDT(x) (x << 6)
323#define VDO_OPOS_MASK VDO_OPOS(0x7)
324#define VDO_CMDT_MASK VDO_CMDT(0x3)
325
326#define CMDT_INIT 0
327#define CMDT_RSP_ACK 1
328#define CMDT_RSP_NAK 2
329#define CMDT_RSP_BUSY 3
330
331/* reserved for SVDM ... for Google UVDM */
332#define VDO_SRC_INITIATOR (0 << 5)
333#define VDO_SRC_RESPONDER (1 << 5)
334
335#define CMD_DISCOVER_IDENT 1
336#define CMD_DISCOVER_SVID 2
337#define CMD_DISCOVER_MODES 3
338#define CMD_ENTER_MODE 4
339#define CMD_EXIT_MODE 5
340#define CMD_ATTENTION 6
341#define CMD_DP_STATUS 16
342#define CMD_DP_CONFIG 17
343
344#define VDO_CMD_VENDOR(x) (((10 + (x)) & 0x1f))
345
346/* ChromeOS specific commands */
347#define VDO_CMD_VERSION VDO_CMD_VENDOR(0)
348#define VDO_CMD_SEND_INFO VDO_CMD_VENDOR(1)
349#define VDO_CMD_READ_INFO VDO_CMD_VENDOR(2)
350#define VDO_CMD_REBOOT VDO_CMD_VENDOR(5)
351#define VDO_CMD_FLASH_ERASE VDO_CMD_VENDOR(6)
352#define VDO_CMD_FLASH_WRITE VDO_CMD_VENDOR(7)
353#define VDO_CMD_ERASE_SIG VDO_CMD_VENDOR(8)
354#define VDO_CMD_PING_ENABLE VDO_CMD_VENDOR(10)
355#define VDO_CMD_CURRENT VDO_CMD_VENDOR(11)
356#define VDO_CMD_FLIP VDO_CMD_VENDOR(12)
357#define VDO_CMD_GET_LOG VDO_CMD_VENDOR(13)
358#define VDO_CMD_CCD_EN VDO_CMD_VENDOR(14)
359
360#define PD_VDO_VID(vdo) ((vdo) >> 16)
361#define PD_VDO_SVDM(vdo) (((vdo) >> 15) & 1)
362#define PD_VDO_OPOS(vdo) (((vdo) >> 8) & 0x7)
363#define PD_VDO_CMD(vdo) ((vdo) & 0x1f)
364#define PD_VDO_CMDT(vdo) (((vdo) >> 6) & 0x3)
365
366/*
367 * SVDM Identity request -> response
368 *
369 * Request is simply properly formatted SVDM header
370 *
371 * Response is 4 data objects:
372 * [0] :: SVDM header
373 * [1] :: Identitiy header
374 * [2] :: Cert Stat VDO
375 * [3] :: (Product | Cable) VDO
376 * [4] :: AMA VDO
377 *
378 */
379#define VDO_INDEX_HDR 0
380#define VDO_INDEX_IDH 1
381#define VDO_INDEX_CSTAT 2
382#define VDO_INDEX_CABLE 3
383#define VDO_INDEX_PRODUCT 3
384#define VDO_INDEX_AMA 4
385#define VDO_I(name) VDO_INDEX_##name
386
387/*
388 * SVDM Identity Header
389 * --------------------
390 * <31> :: data capable as a USB host
391 * <30> :: data capable as a USB device
392 * <29:27> :: product type
393 * <26> :: modal operation supported (1b == yes)
394 * <25:16> :: SBZ
395 * <15:0> :: USB-IF assigned VID for this cable vendor
396 */
397#define IDH_PTYPE_UNDEF 0
398#define IDH_PTYPE_HUB 1
399#define IDH_PTYPE_PERIPH 2
400#define IDH_PTYPE_PCABLE 3
401#define IDH_PTYPE_ACABLE 4
402#define IDH_PTYPE_AMA 5
403
404#define VDO_IDH(usbh, usbd, ptype, is_modal, vid) \
405 ((usbh) << 31 | (usbd) << 30 | ((ptype) & 0x7) << 27 | (is_modal) << 26 | ((vid) & 0xffff))
406
407#define PD_IDH_PTYPE(vdo) (((vdo) >> 27) & 0x7)
408#define PD_IDH_VID(vdo) ((vdo) & 0xffff)
409
410/*
411 * Cert Stat VDO
412 * -------------
413 * <31:20> : SBZ
414 * <19:0> : USB-IF assigned TID for this cable
415 */
416#define VDO_CSTAT(tid) ((tid) & 0xfffff)
417#define PD_CSTAT_TID(vdo) ((vdo) & 0xfffff)
418
419/*
420 * Product VDO
421 * -----------
422 * <31:16> : USB Product ID
423 * <15:0> : USB bcdDevice
424 */
425#define VDO_PRODUCT(pid, bcd) (((pid) & 0xffff) << 16 | ((bcd) & 0xffff))
426#define PD_PRODUCT_PID(vdo) (((vdo) >> 16) & 0xffff)
427
428/*
429 * Message id starts from 0 to 7. If last_msg_id is initialized to 0,
430 * it will lead to repetitive message id with first received packet,
431 * so initialize it with an invalid value 0xff.
432 */
433#define INVALID_MSG_ID_COUNTER 0xff
434
435/*
436 * AMA VDO
437 * ---------
438 * <31:28> :: Cable HW version
439 * <27:24> :: Cable FW version
440 * <23:12> :: SBZ
441 * <11> :: SSTX1 Directionality support (0b == fixed, 1b == cfgable)
442 * <10> :: SSTX2 Directionality support
443 * <9> :: SSRX1 Directionality support
444 * <8> :: SSRX2 Directionality support
445 * <7:5> :: Vconn power
446 * <4> :: Vconn power required
447 * <3> :: Vbus power required
448 * <2:0> :: USB SS Signaling support
449 */
450#define VDO_AMA(hw, fw, tx1d, tx2d, rx1d, rx2d, vcpwr, vcr, vbr, usbss) \
451 (((hw) & 0x7) << 28 | ((fw) & 0x7) << 24 | (tx1d) << 11 | (tx2d) << 10 | (rx1d) << 9 | (rx2d) << 8 | \
452 ((vcpwr) & 0x3) << 5 | (vcr) << 4 | (vbr) << 3 | ((usbss) & 0x7))
453
454#define PD_VDO_AMA_VCONN_REQ(vdo) (((vdo) >> 4) & 1)
455#define PD_VDO_AMA_VBUS_REQ(vdo) (((vdo) >> 3) & 1)
456
457/*
458 * SVDM Discover SVIDs request -> response
459 *
460 * Request is properly formatted VDM Header with discover SVIDs command.
461 * Response is a set of SVIDs of all all supported SVIDs with all zero's to
462 * mark the end of SVIDs. If more than 12 SVIDs are supported command SHOULD be
463 * repeated.
464 */
465#define VDO_SVID(svid0, svid1) (((svid0) & 0xffff) << 16 | ((svid1) & 0xffff))
466#define PD_VDO_SVID_SVID0(vdo) ((vdo) >> 16)
467#define PD_VDO_SVID_SVID1(vdo) ((vdo) & 0xffff)
468
469/*
470 * DisplayPort modes capabilities
471 * -------------------------------
472 * <31:24> : SBZ
473 * <23:16> : UFP_D pin assignment supported
474 * <15:8> : DFP_D pin assignment supported
475 * <7> : USB 2.0 signaling (0b=yes, 1b=no)
476 * <6> : Plug | Receptacle (0b == plug, 1b == receptacle)
477 * <5:2> : xxx1: Supports DPv1.3, xx1x Supports USB Gen 2 signaling
478 * Other bits are reserved.
479 * <1:0> : signal direction ( 00b=rsv, 01b=sink, 10b=src 11b=both )
480 */
481#define VDO_MODE_DP(snkp, srcp, usb, gdr, sign, sdir) \
482 (((snkp) & 0xff) << 16 | ((srcp) & 0xff) << 8 | ((usb) & 1) << 7 | ((gdr) & 1) << 6 | ((sign) & 0xF) << 2 | \
483 ((sdir) & 0x3))
484#define PD_DP_PIN_CAPS(x) ((((x) >> 6) & 0x1) ? (((x) >> 16) & 0x3f) : (((x) >> 8) & 0x3f))
485
486#define MODE_DP_PIN_A 0x01
487#define MODE_DP_PIN_B 0x02
488#define MODE_DP_PIN_C 0x04
489#define MODE_DP_PIN_D 0x08
490#define MODE_DP_PIN_E 0x10
491#define MODE_DP_PIN_F 0x20
492
493/* Pin configs B/D/F support multi-function */
494#define MODE_DP_PIN_MF_MASK 0x2a
495/* Pin configs A/B support BR2 signaling levels */
496#define MODE_DP_PIN_BR2_MASK 0x3
497/* Pin configs C/D/E/F support DP signaling levels */
498#define MODE_DP_PIN_DP_MASK 0x3c
499
500#define MODE_DP_V13 0x1
501#define MODE_DP_GEN2 0x2
502
503#define MODE_DP_SNK 0x1
504#define MODE_DP_SRC 0x2
505#define MODE_DP_BOTH 0x3
506
507/*
508 * DisplayPort Status VDO
509 * ----------------------
510 * <31:9> : SBZ
511 * <8> : IRQ_HPD : 1 == irq arrived since last message otherwise 0.
512 * <7> : HPD state : 0 = HPD_LOW, 1 == HPD_HIGH
513 * <6> : Exit DP Alt mode: 0 == maintain, 1 == exit
514 * <5> : USB config : 0 == maintain current, 1 == switch to USB from DP
515 * <4> : Multi-function preference : 0 == no pref, 1 == MF preferred.
516 * <3> : enabled : is DPout on/off.
517 * <2> : power low : 0 == normal or LPM disabled, 1 == DP disabled for LPM
518 * <1:0> : connect status : 00b == no (DFP|UFP)_D is connected or disabled.
519 * 01b == DFP_D connected, 10b == UFP_D connected, 11b == both.
520 */
521#define VDO_DP_STATUS(irq, lvl, amode, usbc, mf, en, lp, conn) \
522 (((irq) & 1) << 8 | ((lvl) & 1) << 7 | ((amode) & 1) << 6 | ((usbc) & 1) << 5 | ((mf) & 1) << 4 | ((en) & 1) << 3 | \
523 ((lp) & 1) << 2 | ((conn & 0x3) << 0))
524
525#define PD_VDO_DPSTS_HPD_IRQ(x) (((x) >> 8) & 1)
526#define PD_VDO_DPSTS_HPD_LVL(x) (((x) >> 7) & 1)
527#define PD_VDO_DPSTS_MF_PREF(x) (((x) >> 4) & 1)
528
529/* Per DisplayPort Spec v1.3 Section 3.3 */
530#define HPD_USTREAM_DEBOUNCE_LVL (2 * MSEC_US)
531#define HPD_USTREAM_DEBOUNCE_IRQ (250)
532#define HPD_DSTREAM_DEBOUNCE_IRQ (500) /* between 500-1000us */
533
534/*
535 * DisplayPort Configure VDO
536 * -------------------------
537 * <31:24> : SBZ
538 * <23:16> : SBZ
539 * <15:8> : Pin assignment requested. Choose one from mode caps.
540 * <7:6> : SBZ
541 * <5:2> : signalling : 1h == DP v1.3, 2h == Gen 2
542 * Oh is only for USB, remaining values are reserved
543 * <1:0> : cfg : 00 == USB, 01 == DFP_D, 10 == UFP_D, 11 == reserved
544 */
545#define VDO_DP_CFG(pin, sig, cfg) (((pin) & 0xff) << 8 | ((sig) & 0xf) << 2 | ((cfg) & 0x3))
546
547#define PD_DP_CFG_DPON(x) (((x & 0x3) == 1) || ((x & 0x3) == 2))
548/*
549 * Get the pin assignment mask
550 * for backward compatibility, if it is null,
551 * get the former sink pin assignment we used to be in <23:16>.
552 */
553#define PD_DP_CFG_PIN(x) ((((x) >> 8) & 0xff) ? (((x) >> 8) & 0xff) : (((x) >> 16) & 0xff))
554/*
555 * ChromeOS specific PD device Hardware IDs. Used to identify unique
556 * products and used in VDO_INFO. Note this field is 10 bits.
557 */
558#define USB_PD_HW_DEV_ID_RESERVED 0
559#define USB_PD_HW_DEV_ID_ZINGER 1
560#define USB_PD_HW_DEV_ID_MINIMUFFIN 2
561#define USB_PD_HW_DEV_ID_DINGDONG 3
562#define USB_PD_HW_DEV_ID_HOHO 4
563#define USB_PD_HW_DEV_ID_HONEYBUNS 5
564
565/*
566 * ChromeOS specific VDO_CMD_READ_INFO responds with device info including:
567 * RW Hash: First 20 bytes of SHA-256 of RW (20 bytes)
568 * HW Device ID: unique descriptor for each ChromeOS model (2 bytes)
569 * top 6 bits are minor revision, bottom 10 bits are major
570 * SW Debug Version: Software version useful for debugging (15 bits)
571 * IS RW: True if currently in RW, False otherwise (1 bit)
572 */
573#define VDO_INFO(id, id_minor, ver, is_rw) \
574 ((id_minor) << 26 | ((id) & 0x3ff) << 16 | ((ver) & 0x7fff) << 1 | ((is_rw) & 1))
575#define VDO_INFO_HW_DEV_ID(x) ((x) >> 16)
576#define VDO_INFO_SW_DBG_VER(x) (((x) >> 1) & 0x7fff)
577#define VDO_INFO_IS_RW(x) ((x) & 1)
578
579#define HW_DEV_ID_MAJ(x) (x & 0x3ff)
580#define HW_DEV_ID_MIN(x) ((x) >> 10)
581
582/* USB-IF SIDs */
583#define USB_SID_PD 0xff00 /* power delivery */
584#define USB_SID_DISPLAYPORT 0xff01
585
586#define USB_GOOGLE_TYPEC_URL "http://www.google.com/chrome/devices/typec"
587/* USB Vendor ID assigned to Google Inc. */
588#define USB_VID_GOOGLE 0x18d1
589
590/* Other Vendor IDs */
591#define USB_VID_APPLE 0x05ac
592
593/* Timeout for message receive in microseconds */
594#define USB_PD_RX_TMOUT_US 1800
595
596 /* --- Protocol layer functions --- */
597
598 enum pd_states
599 {
600 PD_STATE_DISABLED,
601 PD_STATE_SUSPENDED,
602#ifdef CONFIG_USB_PD_DUAL_ROLE
603 PD_STATE_SNK_DISCONNECTED,
604 PD_STATE_SNK_DISCONNECTED_DEBOUNCE,
605 PD_STATE_SNK_HARD_RESET_RECOVER,
606 PD_STATE_SNK_DISCOVERY,
607 PD_STATE_SNK_REQUESTED,
608 PD_STATE_SNK_TRANSITION,
609 PD_STATE_SNK_READY,
610
611 PD_STATE_SNK_SWAP_INIT,
612 PD_STATE_SNK_SWAP_SNK_DISABLE,
613 PD_STATE_SNK_SWAP_SRC_DISABLE,
614 PD_STATE_SNK_SWAP_STANDBY,
615 PD_STATE_SNK_SWAP_COMPLETE,
616
617 PD_STATE_SRC_SWAP_INIT,
618 PD_STATE_SRC_SWAP_SNK_DISABLE,
619 PD_STATE_SRC_SWAP_SRC_DISABLE,
620 PD_STATE_SRC_SWAP_STANDBY,
621#endif /* CONFIG_USB_PD_DUAL_ROLE */
622
623 PD_STATE_SRC_DISCONNECTED,
624 PD_STATE_SRC_DISCONNECTED_DEBOUNCE,
625 PD_STATE_SRC_HARD_RESET_RECOVER,
626 PD_STATE_SRC_STARTUP,
627 PD_STATE_SRC_DISCOVERY,
628 PD_STATE_SRC_NEGOCIATE,
629 PD_STATE_SRC_ACCEPTED,
630 PD_STATE_SRC_POWERED,
631 PD_STATE_SRC_TRANSITION,
632 PD_STATE_SRC_READY,
633 PD_STATE_SRC_GET_SINK_CAP,
634 PD_STATE_DR_SWAP,
635
636#ifdef CONFIG_USB_PD_DUAL_ROLE
637#ifdef CONFIG_USBC_VCONN_SWAP
638 PD_STATE_VCONN_SWAP_SEND,
639 PD_STATE_VCONN_SWAP_INIT,
640 PD_STATE_VCONN_SWAP_READY,
641#endif /* CONFIG_USBC_VCONN_SWAP */
642#endif /* CONFIG_USB_PD_DUAL_ROLE */
643
644 PD_STATE_SOFT_RESET,
645 PD_STATE_HARD_RESET_SEND,
646 PD_STATE_HARD_RESET_EXECUTE,
647#ifdef CONFIG_COMMON_RUNTIME
648 PD_STATE_BIST_RX,
649 PD_STATE_BIST_TX,
650#endif
651
652#ifdef CONFIG_USB_PD_DUAL_ROLE_AUTO_TOGGLE
653 PD_STATE_DRP_AUTO_TOGGLE,
654#endif
655 PD_STATE_ENTER_USB, /* C39 */
656 /* Number of states. Not an actual state. */
657 PD_STATE_COUNT,
658 };
659
660#define PD_FLAGS_PING_ENABLED (1 << 0) /* SRC_READY pings enabled */
661#define PD_FLAGS_PARTNER_DR_POWER (1 << 1) /* port partner is dualrole power */
662#define PD_FLAGS_PARTNER_DR_DATA (1 << 2) /* port partner is dualrole data */
663#define PD_FLAGS_CHECK_IDENTITY (1 << 3) /* discover identity in READY */
664#define PD_FLAGS_SNK_CAP_RECVD (1 << 4) /* sink capabilities received */
665#define PD_FLAGS_TCPC_DRP_TOGGLE (1 << 5) /* TCPC-controlled DRP toggling */
666#define PD_FLAGS_EXPLICIT_CONTRACT (1 << 6) /* explicit pwr contract in place */
667#define PD_FLAGS_VBUS_NEVER_LOW (1 << 7) /* VBUS input has never been low */
668#define PD_FLAGS_PREVIOUS_PD_CONN (1 << 8) /* previously PD connected */
669#define PD_FLAGS_CHECK_PR_ROLE (1 << 9) /* check power role in READY */
670#define PD_FLAGS_CHECK_DR_ROLE (1 << 10) /* check data role in READY */
671#define PD_FLAGS_PARTNER_UNCONSTR (1 << 11) /* port partner unconstrained pwr */
672#define PD_FLAGS_VCONN_ON (1 << 12) /* vconn is being sourced */
673#define PD_FLAGS_TRY_SRC (1 << 13) /* Try.SRC states are active */
674#define PD_FLAGS_PARTNER_USB_COMM (1 << 14) /* port partner is USB comms */
675#define PD_FLAGS_UPDATE_SRC_CAPS (1 << 15) /* send new source capabilities */
676#define PD_FLAGS_TS_DTS_PARTNER (1 << 16) /* partner has rp/rp or rd/rd */
677
678/*
679 * These PD_FLAGS_LPM* flags track the software state (PD_LPM_FLAGS_REQUESTED)
680 * and hardware state (PD_LPM_FLAGS_ENGAGED) of the TCPC low power mode.
681 * PD_FLAGS_LPM_TRANSITION is set while the HW is transitioning into or out of
682 * low power (when PD_LPM_FLAGS_ENGAGED is changing).
683 */
684#ifdef CONFIG_USB_PD_TCPC_LOW_POWER
685#define PD_FLAGS_LPM_REQUESTED (1 << 17) /* Tracks SW LPM state */
686#define PD_FLAGS_LPM_ENGAGED (1 << 18) /* Tracks HW LPM state */
687#define PD_FLAGS_LPM_TRANSITION (1 << 19) /* Tracks HW LPM transition */
688#define PD_FLAGS_LPM_EXIT (1 << 19) /* Tracks HW LPM exit */
689#endif
690/*
691 * Tracks whether port negotiation may have stalled due to not starting reset
692 * timers in SNK_DISCOVERY
693 */
694#define PD_FLAGS_SNK_WAITING_BATT (1 << 21)
695/* Check vconn state in READY */
696#define PD_FLAGS_CHECK_VCONN_STATE (1 << 22)
697
698#ifdef CONFIG_USB_PD_DUAL_ROLE
699
700 /* Per-port battery backed RAM flags */
701#define PD_BBRMFLG_EXPLICIT_CONTRACT (1 << 0)
702#define PD_BBRMFLG_POWER_ROLE (1 << 1)
703#define PD_BBRMFLG_DATA_ROLE (1 << 2)
704#define PD_BBRMFLG_VCONN_ROLE (1 << 3)
705#define PD_BBRMFLG_DBGACC_ROLE (1 << 4)
706
707/* Initial value for CC debounce variable */
708#define PD_CC_UNSET -1
709
710 enum pd_dual_role_states
711 {
712 /* While disconnected, toggle between src and sink */
713 PD_DRP_TOGGLE_ON,
714 /* Stay in src until disconnect, then stay in sink forever */
715 PD_DRP_TOGGLE_OFF,
716 /* Stay in current power role, don't switch. No auto-toggle support */
717 PD_DRP_FREEZE,
718 /* Switch to sink */
719 PD_DRP_FORCE_SINK,
720 /* Switch to source */
721 PD_DRP_FORCE_SOURCE,
722 };
728 enum pd_dual_role_states pd_get_dual_role(void);
729
730#ifndef CONFIG_CHARGER
734 int board_get_battery_soc();
735#endif
736
737 void pd_set_dual_role_no_wakeup(enum pd_dual_role_states state);
738
745 void pd_set_dual_role(enum pd_dual_role_states state);
746
752 int pd_get_role();
753
754#endif
755
756 /* Control Message type */
757 enum pd_ctrl_msg_type
758 {
759 /* 0 Reserved */
760 PD_CTRL_GOOD_CRC = 1,
761 PD_CTRL_GOTO_MIN = 2,
762 PD_CTRL_ACCEPT = 3,
763 PD_CTRL_REJECT = 4,
764 PD_CTRL_PING = 5,
765 PD_CTRL_PS_RDY = 6,
766 PD_CTRL_GET_SOURCE_CAP = 7,
767 PD_CTRL_GET_SINK_CAP = 8,
768 PD_CTRL_DR_SWAP = 9,
769 PD_CTRL_PR_SWAP = 10,
770 PD_CTRL_VCONN_SWAP = 11,
771 PD_CTRL_WAIT = 12,
772 PD_CTRL_SOFT_RESET = 13,
773 /* 14-15 Reserved */
774
775 /* Used for REV 3.0 */
776 PD_CTRL_NOT_SUPPORTED = 16,
777 PD_CTRL_GET_SOURCE_CAP_EXT = 17,
778 PD_CTRL_GET_STATUS = 18,
779 PD_CTRL_FR_SWAP = 19,
780 PD_CTRL_GET_PPS_STATUS = 20,
781 PD_CTRL_GET_COUNTRY_CODES = 21,
782 /* 22-31 Reserved */
783 };
784
785/* Control message types which always mark the start of an AMS */
786#define PD_CTRL_AMS_START_MASK \
787 ((1 << PD_CTRL_GOTO_MIN) | (1 << PD_CTRL_GET_SOURCE_CAP) | (1 << PD_CTRL_GET_SINK_CAP) | (1 << PD_CTRL_DR_SWAP) | \
788 (1 << PD_CTRL_PR_SWAP) | (1 << PD_CTRL_VCONN_SWAP) | (1 << PD_CTRL_GET_SOURCE_CAP_EXT) | \
789 (1 << PD_CTRL_GET_STATUS) | (1 << PD_CTRL_FR_SWAP) | (1 << PD_CTRL_GET_PPS_STATUS) | \
790 (1 << PD_CTRL_GET_COUNTRY_CODES))
791
792/* Battery Status Data Object fields for REV 3.0 */
793#define BSDO_CAP_UNKNOWN 0xffff
794#define BSDO_CAP(n) (((n) & 0xffff) << 16)
795#define BSDO_INVALID (1 << 8)
796#define BSDO_PRESENT (1 << 9)
797#define BSDO_DISCHARGING (1 << 10)
798#define BSDO_IDLE (1 << 11)
799
800/* Get Battery Cap Message fields for REV 3.0 */
801#define BATT_CAP_REF(n) (((n) >> 16) & 0xff)
802
803 /* Extended message type for REV 3.0 */
804 enum pd_ext_msg_type
805 {
806 /* 0 Reserved */
807 PD_EXT_SOURCE_CAP = 1,
808 PD_EXT_STATUS = 2,
809 PD_EXT_GET_BATTERY_CAP = 3,
810 PD_EXT_GET_BATTERY_STATUS = 4,
811 PD_EXT_BATTERY_CAP = 5,
812 PD_EXT_GET_MANUFACTURER_INFO = 6,
813 PD_EXT_MANUFACTURER_INFO = 7,
814 PD_EXT_SECURITY_REQUEST = 8,
815 PD_EXT_SECURITY_RESPONSE = 9,
816 PD_EXT_FIRMWARE_UPDATE_REQUEST = 10,
817 PD_EXT_FIRMWARE_UPDATE_RESPONSE = 11,
818 PD_EXT_PPS_STATUS = 12,
819 PD_EXT_COUNTRY_INFO = 13,
820 PD_EXT_COUNTRY_CODES = 14,
821 /* 15-31 Reserved */
822 };
823
824 /* Data message type */
825 enum pd_data_msg_type
826 {
827 /* 0 Reserved */
828 PD_DATA_SOURCE_CAP = 1,
829 PD_DATA_REQUEST = 2,
830 PD_DATA_BIST = 3,
831 PD_DATA_SINK_CAP = 4,
832 /* 5-14 Reserved for REV 2.0 */
833 PD_DATA_BATTERY_STATUS = 5,
834 PD_DATA_ALERT = 6,
835 PD_DATA_GET_COUNTRY_INFO = 7,
836 /* 8-14 Reserved for REV 3.0 */
837 PD_DATA_ENTER_USB = 8,
838 PD_DATA_VENDOR_DEF = 15,
839 };
840
841 /*
842 * Cable plug. See 6.2.1.1.7 Cable Plug. Only applies to SOP' and SOP".
843 * Replaced by pd_power_role for SOP packets.
844 */
845 enum pd_cable_plug
846 {
847 PD_PLUG_FROM_DFP_UFP = 0,
848 PD_PLUG_FROM_CABLE = 1
849 };
850
851 enum cable_outlet
852 {
853 CABLE_PLUG = 0,
854 CABLE_RECEPTACLE = 1,
855 };
856
857/* Protocol revision */
858#define PD_REV10 0
859#define PD_REV20 1
860#define PD_REV30 2
861
862/* Power role */
863#define PD_ROLE_SINK 0
864#define PD_ROLE_SOURCE 1
865/* Data role */
866#define PD_ROLE_UFP 0
867#define PD_ROLE_DFP 1
868/* Vconn role */
869#define PD_ROLE_VCONN_OFF 0
870#define PD_ROLE_VCONN_ON 1
871
872/* chunk is a request or response in REV 3.0 */
873#define CHUNK_RESPONSE 0
874#define CHUNK_REQUEST 1
875
876#ifndef NULL
877#define NULL 0
878#endif
879
880/* collision avoidance Rp values in REV 3.0 */
881#define SINK_TX_OK TYPEC_RP_3A0
882#define SINK_TX_NG TYPEC_RP_1A5
883
884/* Port role at startup */
885#ifndef PD_ROLE_DEFAULT
886#ifdef CONFIG_USB_PD_DUAL_ROLE
887#define PD_ROLE_DEFAULT() PD_ROLE_SINK
888#else
889#define PD_ROLE_DEFAULT() PD_ROLE_SOURCE
890#endif
891#endif
892
893/* Port default state at startup */
894#ifdef CONFIG_USB_PD_DUAL_ROLE
895#define PD_DEFAULT_STATE() \
896 ((PD_ROLE_DEFAULT() == PD_ROLE_SOURCE) ? PD_STATE_SRC_DISCONNECTED : PD_STATE_SNK_DISCONNECTED)
897#else
898#define PD_DEFAULT_STATE() PD_STATE_SRC_DISCONNECTED
899#endif
900
901/* build extended message header */
902/* All extended messages are chunked, so set bit 15 */
903#define PD_EXT_HEADER(cnum, rchk, dsize) ((1 << 15) | ((cnum) << 11) | ((rchk) << 10) | (dsize))
904
905/* build message header */
906#define PD_HEADER(type, prole, drole, id, cnt, rev, ext) \
907 ((type) | ((rev) << 6) | ((drole) << 5) | ((prole) << 8) | ((id) << 9) | ((cnt) << 12) | ((ext) << 15))
908
909/* Used for processing pd header */
910#define PD_HEADER_EXT(header) (((header) >> 15) & 1)
911#define PD_HEADER_CNT(header) (((header) >> 12) & 7)
912/*
913 * NOTE: bit 4 was added in PD 3.0, and should be reserved and set to 0 in PD
914 * 2.0 messages
915 */
916#define PD_HEADER_TYPE(header) ((header) & 0x1F)
917#define PD_HEADER_ID(header) (((header) >> 9) & 7)
918#define PD_HEADER_PROLE(header) (((header) >> 8) & 1)
919#define PD_HEADER_REV(header) (((header) >> 6) & 3)
920#define PD_HEADER_DROLE(header) (((header) >> 5) & 1)
921
922/*
923 * The message header is a 16-bit value that's stored in a 32-bit data type.
924 * SOP* is encoded in bits 31 to 28 of the 32-bit data type.
925 * NOTE: This is not part of the PD spec.
926 */
927#define PD_HEADER_GET_SOP(header) (((header) >> 28) & 0xf)
928#define PD_HEADER_SOP(sop) (((sop) & 0xf) << 28)
929
930 enum pd_msg_type
931 {
932 PD_MSG_SOP,
933 PD_MSG_SOP_PRIME,
934 PD_MSG_SOP_PRIME_PRIME,
935 PD_MSG_SOP_DBG_PRIME,
936 PD_MSG_SOP_DBG_PRIME_PRIME,
937 PD_MSG_SOP_CBL_RST,
938 };
939
940/* Used for processing pd extended header */
941#define PD_EXT_HEADER_CHUNKED(header) (((header) >> 15) & 1)
942#define PD_EXT_HEADER_CHUNK_NUM(header) (((header) >> 11) & 0xf)
943#define PD_EXT_HEADER_REQ_CHUNK(header) (((header) >> 10) & 1)
944#define PD_EXT_HEADER_DATA_SIZE(header) ((header) & 0x1ff)
945
946/* K-codes for special symbols */
947#define PD_SYNC1 0x18
948#define PD_SYNC2 0x11
949#define PD_SYNC3 0x06
950#define PD_RST1 0x07
951#define PD_RST2 0x19
952#define PD_EOP 0x0D
953
954/* Minimum PD supply current (mA) */
955#define PD_MIN_MA 500
956
957/* Minimum PD voltage (mV) */
958#define PD_MIN_MV 5000
959
960/* No connect voltage threshold for sources based on Rp */
961#define PD_SRC_DEF_VNC_MV 1600
962#define PD_SRC_1_5_VNC_MV 1600
963#define PD_SRC_3_0_VNC_MV 2600
964
965/* Rd voltage threshold for sources based on Rp */
966#define PD_SRC_DEF_RD_THRESH_MV 200
967#define PD_SRC_1_5_RD_THRESH_MV 400
968#define PD_SRC_3_0_RD_THRESH_MV 800
969
970/* Voltage threshold to detect connection when presenting Rd */
971#define PD_SNK_VA_MV 250
972
973 /* --- Policy layer functions --- */
974
975 /* Request types for pd_build_request() */
976 enum pd_request_type
977 {
978 PD_REQUEST_VSAFE5V,
979 PD_REQUEST_MAX,
980 };
981
982#ifdef CONFIG_USB_PD_REV30
989 int pd_get_rev();
990
997 int pd_get_vdo_ver();
998#else
999#define pd_get_rev(n) PD_REV20
1000#define pd_get_vdo_ver(n) VDM_VER10
1001#endif
1012 int pd_build_request(uint32_t* rdo, uint32_t* ma, uint32_t* mv, enum pd_request_type req_type);
1013
1020 int pd_is_max_request_allowed(void);
1021
1026 int tcpm_enqueue_message();
1027 int tcpm_has_pending_message();
1028 int tcpm_dequeue_message(uint32_t* const payload, int* const header);
1029 void tcpm_clear_pending_messages();
1030
1031 int consume_sop_prime_repeat_msg(uint8_t msg_id);
1032 int consume_sop_prime_prime_repeat_msg(uint8_t msg_id);
1033 void reset_pd_cable();
1034
1042 void pd_process_source_cap_callback(int cnt, uint32_t* src_caps);
1043
1051 void pd_process_source_cap(int cnt, uint32_t* src_caps);
1052
1062 int pd_find_pdo_index(int max_mv, uint32_t* pdo);
1063
1069 int is_sink_ready();
1070
1075 int is_sink_negotiating();
1076
1081 int is_sink_hard_resetting();
1082
1089
1093 int is_power_role_swapping();
1094
1097 {
1099 uint32_t attaches;
1103 uint32_t contracts;
1106 uint32_t soft_resets_failed;
1109 uint32_t hard_resets_tx_failed;
1116 uint32_t src_caps_acked;
1121 uint32_t pr_swaps_rejected;
1124 uint32_t pr_swaps_to_sink;
1127 };
1128
1129 /* time of the last wake up of the PD state machine, in ms (debug) */
1130 uint32_t pd_get_last_run_ms(void);
1131
1134 {
1135 uint8_t power_role;
1136 uint8_t polarity;
1137 int is_explicit_contract;
1140 int is_vbus_never_low;
1143 uint32_t requested_ma;
1146 struct pd_debug_counters counters;
1147 };
1148
1150 void pd_get_contract_info(struct pd_contract_info* info);
1151
1157 void pd_set_reset_allowed(int allowed);
1159 int pd_is_reset_allowed();
1160
1166 const char* get_state_cstr();
1167
1175 void pd_extract_pdo_power(uint32_t pdo, uint32_t* ma, uint32_t* mv);
1176
1184 void pd_snk_give_back(uint32_t* const ma, uint32_t* const mv);
1185
1190 void pd_set_max_voltage(unsigned mv);
1191
1196 unsigned pd_get_max_voltage(void);
1197
1204 int pd_is_valid_input_voltage(int mv);
1205
1213 int pd_check_requested_voltage(uint32_t rdo);
1214
1222 int pd_board_check_request(uint32_t rdo, int pdo_cnt);
1223
1229 void pd_transition_voltage(int idx);
1230
1236 void pd_power_supply_reset();
1237
1244 int pd_set_power_supply_ready();
1245
1251 int pd_is_source_voltage_ready();
1252
1260 void pd_request_source_voltage(int mv);
1261
1269 void pd_set_external_voltage_limit(int mv);
1270
1278 void pd_set_input_current_limit(uint32_t max_ma, uint32_t supply_voltage);
1279
1285 void pd_update_contract();
1286
1287 /* Encode DTS status of port partner in current limit parameter */
1288 typedef uint32_t typec_current_t;
1289#define TYPEC_CURRENT_DTS_MASK (1 << 31)
1290#define TYPEC_CURRENT_ILIM_MASK (~TYPEC_CURRENT_DTS_MASK)
1291
1299 void typec_set_input_current_limit(typec_current_t max_ma, uint32_t supply_voltage);
1300
1307 void typec_set_source_current_limit(int rp);
1308
1314 int pd_board_checks(void);
1315
1321 void pd_vbus_low();
1322
1329 int pd_is_power_swap_succesful();
1330
1338 int pd_check_data_swap(int data_role);
1339
1347 int pd_check_vconn_swap();
1348
1356 void pd_check_pr_role(int pr_role, int flags);
1357
1362 void pd_request_pr_role_check();
1363
1371 void pd_check_dr_role(int dr_role, int flags);
1372
1379 void pd_execute_data_swap(int data_role);
1380
1386 void pd_get_info(uint32_t* info_data);
1387
1397 int pd_custom_vdm(int cnt, uint32_t* payload, uint32_t** rpayload);
1398
1408 int pd_svdm(int cnt, uint32_t* payload, uint32_t** rpayload);
1409
1418 uint32_t pd_dfp_enter_mode(uint16_t svid, int opos);
1419
1428 int pd_dfp_exit_mode(uint16_t svid, int opos);
1429
1435 void pd_dfp_pe_init();
1436
1443 uint16_t pd_get_identity_vid();
1444
1455 int pd_dev_store_rw_hash(uint16_t dev_id, uint32_t* rw_hash, uint32_t ec_current_image);
1456
1466 void pd_send_vdm(uint32_t vid, int cmd, const uint32_t* data, int count);
1467
1468 /* Power Data Objects for the source and the sink */
1469 extern const uint32_t pd_src_pdo[];
1470 extern const int pd_src_pdo_cnt;
1471 extern const uint32_t pd_snk_pdo[];
1472 extern const int pd_snk_pdo_cnt;
1473
1479#if defined(HAS_TASK_HOSTCMD) && !defined(TEST_BUILD)
1480 void pd_send_host_event(int mask);
1481#else
1482static inline void pd_send_host_event(int mask) {}
1483#endif
1484
1492 int pd_alt_mode(uint16_t svid);
1493
1500 void pd_send_hpd(enum hpd_event hpd);
1501
1505 extern const struct deferred_data pd_usb_billboard_deferred_data;
1506 /* --- Physical layer functions : chip specific --- */
1507
1508 /* Packet preparation/retrieval */
1509
1510 // REMOVED
1511
1512 /* TX/RX callbacks */
1513
1519 void pd_set_suspend(int enable);
1520
1528 int pd_is_port_enabled();
1529
1530 /* stop listening to the CC wire during transmissions */
1531 void pd_rx_disable_monitoring();
1532
1538 void pd_hw_release();
1539
1546 void pd_hw_init(int role);
1547
1551 void pd_init();
1552
1558 void pd_run_state_machine();
1559
1560 /* --- Protocol layer functions --- */
1561
1567 int pd_comm_is_enabled();
1568
1575 int pd_is_connected();
1576
1582 int pd_is_vbus_present();
1583
1589 void pd_execute_hard_reset();
1590
1597 void pd_transmit_complete(int status);
1598
1604 enum pd_data_role pd_get_data_role();
1605
1611 enum pd_power_role pd_get_power_role();
1612
1618 int pd_is_battery_capable(void);
1619
1625 int pd_is_try_source_capable(void);
1626
1632 void pd_request_vconn_swap();
1633
1640 enum pd_cc_states pd_get_task_cc_state();
1641
1650 uint8_t pd_get_task_state();
1651
1658 const char* pd_get_task_state_name();
1659
1666 int pd_get_vconn_state();
1667
1674 int pd_get_partner_dual_role_power();
1675
1682 int pd_get_partner_unconstr_power();
1683
1689 int pd_get_polarity();
1690
1696 int pd_get_partner_data_swap_capable();
1697
1698 int pd_is_disconnected();
1699
1705 void pd_request_power_swap();
1706
1713 void pd_try_vconn_src();
1714
1720 void pd_request_data_swap();
1721
1730 void pd_comm_enable(int enable);
1731
1740 void pd_ping_enable(int enable);
1741
1742 /* Issue PD soft reset */
1743 void pd_soft_reset(void);
1744
1750 void pd_set_new_power_request();
1751
1758 int pd_ts_dts_plugged();
1759
1760 /* ----- Logging ----- */
1761 static inline void pd_log_event(uint8_t type, uint8_t size_port, uint16_t data, void* payload) {}
1762 static inline int pd_vdm_get_log_entry(uint32_t* payload) { return 0; }
1763
1764#ifdef __cplusplus
1765}
1766#endif
1767
1768#endif /* __CROS_EC_USB_PD_H */
bool is_source_hard_resetting()
return true when we are a source in a hard reset: VBUS must be cut until the end of the recovery time
Definition: power_delivery.cpp:644
Definition: usb_pd.h:206
Details on the current contract, for debug.
Definition: usb_pd.h:1134
int is_pd_capable
the partner sent at least one PD message
Definition: usb_pd.h:1139
uint32_t requested_mv
last request sent (contract when an explicit contract is in place)
Definition: usb_pd.h:1142
int hard_reset_count
hard resets sent since the attach
Definition: usb_pd.h:1145
Debug counters of the PD negociation events, since boot.
Definition: usb_pd.h:1097
uint32_t hard_resets_received
hard resets received from the partner
Definition: usb_pd.h:1111
uint32_t src_contracts
contracts established as source (PS_RDY sent and acknowledged)
Definition: usb_pd.h:1118
uint32_t hard_resets_sent
hard resets sent, and the ones whose transmission never completed (assumed sent, as the spec says)
Definition: usb_pd.h:1108
uint32_t pr_swaps_to_source
power role swaps completed: we became the source, we became the sink
Definition: usb_pd.h:1123
uint32_t src_caps_received
source capabilities messages received (and answered with a request)
Definition: usb_pd.h:1101
uint32_t src_caps_sent
source capabilities sent while we are source, and the ones acknowledged by a GoodCRC
Definition: usb_pd.h:1115
uint32_t contracts
contracts established as sink (PS_RDY received)
Definition: usb_pd.h:1103
uint32_t attaches
source attached (CC debounced, VBUS present)
Definition: usb_pd.h:1099
uint32_t src_attaches
sink attached while we are source (CC debounced)
Definition: usb_pd.h:1113
uint32_t soft_resets_sent
soft resets sent, and the ones that were not acknowledged
Definition: usb_pd.h:1105
uint32_t pr_swaps_vbus_timeout
swaps to source abandoned because our VBUS did not come up in time
Definition: usb_pd.h:1126
uint32_t pr_swaps_accepted
power role swaps requested by the partner: accepted, rejected
Definition: usb_pd.h:1120
Definition: usb_pd.h:280
Definition: usb_pd.h:249
Definition: usb_pd.h:230
Definition: usb_pd.h:213
Definition: usb_pd.h:223