1 /*
2  * Copyright 2015-2017 Google, Inc
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 
15 #ifndef __LINUX_USB_PD_VDO_H
16 #define __LINUX_USB_PD_VDO_H
17 
18 #include "pd.h"
19 
20 /*
21  * VDO : Vendor Defined Message Object
22  * VDM object is minimum of VDM header + 6 additional data objects.
23  */
24 
25 #define VDO_MAX_OBJECTS		6
26 #define VDO_MAX_SIZE		(VDO_MAX_OBJECTS + 1)
27 
28 /*
29  * VDM header
30  * ----------
31  * <31:16>  :: SVID
32  * <15>     :: VDM type ( 1b == structured, 0b == unstructured )
33  * <14:13>  :: Structured VDM version (can only be 00 == 1.0 currently)
34  * <12:11>  :: reserved
35  * <10:8>   :: object position (1-7 valid ... used for enter/exit mode only)
36  * <7:6>    :: command type (SVDM only?)
37  * <5>      :: reserved (SVDM), command type (UVDM)
38  * <4:0>    :: command
39  */
40 #define VDO(vid, type, custom)				\
41 	(((vid) << 16) |				\
42 	 ((type) << 15) |				\
43 	 ((custom) & 0x7FFF))
44 
45 #define VDO_SVDM_TYPE		(1 << 15)
46 #define VDO_SVDM_VERS(x)	((x) << 13)
47 #define VDO_OPOS(x)		((x) << 8)
48 #define VDO_CMDT(x)		((x) << 6)
49 #define VDO_OPOS_MASK		VDO_OPOS(0x7)
50 #define VDO_CMDT_MASK		VDO_CMDT(0x3)
51 
52 #define CMDT_INIT		0
53 #define CMDT_RSP_ACK		1
54 #define CMDT_RSP_NAK		2
55 #define CMDT_RSP_BUSY		3
56 
57 /* reserved for SVDM ... for Google UVDM */
58 #define VDO_SRC_INITIATOR	(0 << 5)
59 #define VDO_SRC_RESPONDER	(1 << 5)
60 
61 #define CMD_DISCOVER_IDENT	1
62 #define CMD_DISCOVER_SVID	2
63 #define CMD_DISCOVER_MODES	3
64 #define CMD_ENTER_MODE		4
65 #define CMD_EXIT_MODE		5
66 #define CMD_ATTENTION		6
67 
68 #define VDO_CMD_VENDOR(x)    (((0x10 + (x)) & 0x1f))
69 
70 /* ChromeOS specific commands */
71 #define VDO_CMD_VERSION		VDO_CMD_VENDOR(0)
72 #define VDO_CMD_SEND_INFO	VDO_CMD_VENDOR(1)
73 #define VDO_CMD_READ_INFO	VDO_CMD_VENDOR(2)
74 #define VDO_CMD_REBOOT		VDO_CMD_VENDOR(5)
75 #define VDO_CMD_FLASH_ERASE	VDO_CMD_VENDOR(6)
76 #define VDO_CMD_FLASH_WRITE	VDO_CMD_VENDOR(7)
77 #define VDO_CMD_ERASE_SIG	VDO_CMD_VENDOR(8)
78 #define VDO_CMD_PING_ENABLE	VDO_CMD_VENDOR(10)
79 #define VDO_CMD_CURRENT		VDO_CMD_VENDOR(11)
80 #define VDO_CMD_FLIP		VDO_CMD_VENDOR(12)
81 #define VDO_CMD_GET_LOG		VDO_CMD_VENDOR(13)
82 #define VDO_CMD_CCD_EN		VDO_CMD_VENDOR(14)
83 
84 #define PD_VDO_VID(vdo)		((vdo) >> 16)
85 #define PD_VDO_SVDM(vdo)	(((vdo) >> 15) & 1)
86 #define PD_VDO_OPOS(vdo)	(((vdo) >> 8) & 0x7)
87 #define PD_VDO_CMD(vdo)		((vdo) & 0x1f)
88 #define PD_VDO_CMDT(vdo)	(((vdo) >> 6) & 0x3)
89 
90 /*
91  * SVDM Identity request -> response
92  *
93  * Request is simply properly formatted SVDM header
94  *
95  * Response is 4 data objects:
96  * [0] :: SVDM header
97  * [1] :: Identitiy header
98  * [2] :: Cert Stat VDO
99  * [3] :: (Product | Cable) VDO
100  * [4] :: AMA VDO
101  *
102  */
103 #define VDO_INDEX_HDR		0
104 #define VDO_INDEX_IDH		1
105 #define VDO_INDEX_CSTAT		2
106 #define VDO_INDEX_CABLE		3
107 #define VDO_INDEX_PRODUCT	3
108 #define VDO_INDEX_AMA		4
109 
110 /*
111  * SVDM Identity Header
112  * --------------------
113  * <31>     :: data capable as a USB host
114  * <30>     :: data capable as a USB device
115  * <29:27>  :: product type
116  * <26>     :: modal operation supported (1b == yes)
117  * <25:16>  :: Reserved, Shall be set to zero
118  * <15:0>   :: USB-IF assigned VID for this cable vendor
119  */
120 #define IDH_PTYPE_UNDEF		0
121 #define IDH_PTYPE_HUB		1
122 #define IDH_PTYPE_PERIPH	2
123 #define IDH_PTYPE_PCABLE	3
124 #define IDH_PTYPE_ACABLE	4
125 #define IDH_PTYPE_AMA		5
126 
127 #define VDO_IDH(usbh, usbd, ptype, is_modal, vid)		\
128 	((usbh) << 31 | (usbd) << 30 | ((ptype) & 0x7) << 27	\
129 	 | (is_modal) << 26 | ((vid) & 0xffff))
130 
131 #define PD_IDH_PTYPE(vdo)	(((vdo) >> 27) & 0x7)
132 #define PD_IDH_VID(vdo)		((vdo) & 0xffff)
133 #define PD_IDH_MODAL_SUPP(vdo)	((vdo) & (1 << 26))
134 
135 /*
136  * Cert Stat VDO
137  * -------------
138  * <31:0>  : USB-IF assigned XID for this cable
139  */
140 #define PD_CSTAT_XID(vdo)	(vdo)
141 
142 /*
143  * Product VDO
144  * -----------
145  * <31:16> : USB Product ID
146  * <15:0>  : USB bcdDevice
147  */
148 #define VDO_PRODUCT(pid, bcd)	(((pid) & 0xffff) << 16 | ((bcd) & 0xffff))
149 #define PD_PRODUCT_PID(vdo)	(((vdo) >> 16) & 0xffff)
150 
151 /*
152  * Cable VDO
153  * ---------
154  * <31:28> :: Cable HW version
155  * <27:24> :: Cable FW version
156  * <23:20> :: Reserved, Shall be set to zero
157  * <19:18> :: type-C to Type-A/B/C (00b == A, 01 == B, 10 == C)
158  * <17>    :: Type-C to Plug/Receptacle (0b == plug, 1b == receptacle)
159  * <16:13> :: cable latency (0001 == <10ns(~1m length))
160  * <12:11> :: cable termination type (11b == both ends active VCONN req)
161  * <10>    :: SSTX1 Directionality support (0b == fixed, 1b == cfgable)
162  * <9>     :: SSTX2 Directionality support
163  * <8>     :: SSRX1 Directionality support
164  * <7>     :: SSRX2 Directionality support
165  * <6:5>   :: Vbus current handling capability
166  * <4>     :: Vbus through cable (0b == no, 1b == yes)
167  * <3>     :: SOP" controller present? (0b == no, 1b == yes)
168  * <2:0>   :: USB SS Signaling support
169  */
170 #define CABLE_ATYPE		0
171 #define CABLE_BTYPE		1
172 #define CABLE_CTYPE		2
173 #define CABLE_PLUG		0
174 #define CABLE_RECEPTACLE	1
175 #define CABLE_CURR_1A5		0
176 #define CABLE_CURR_3A		1
177 #define CABLE_CURR_5A		2
178 #define CABLE_USBSS_U2_ONLY	0
179 #define CABLE_USBSS_U31_GEN1	1
180 #define CABLE_USBSS_U31_GEN2	2
181 #define VDO_CABLE(hw, fw, cbl, gdr, lat, term, tx1d, tx2d, rx1d, rx2d, cur,\
182 		  vps, sopp, usbss) \
183 	(((hw) & 0x7) << 28 | ((fw) & 0x7) << 24 | ((cbl) & 0x3) << 18	\
184 	 | (gdr) << 17 | ((lat) & 0x7) << 13 | ((term) & 0x3) << 11	\
185 	 | (tx1d) << 10 | (tx2d) << 9 | (rx1d) << 8 | (rx2d) << 7	\
186 	 | ((cur) & 0x3) << 5 | (vps) << 4 | (sopp) << 3		\
187 	 | ((usbss) & 0x7))
188 
189 /*
190  * AMA VDO
191  * ---------
192  * <31:28> :: Cable HW version
193  * <27:24> :: Cable FW version
194  * <23:12> :: Reserved, Shall be set to zero
195  * <11>    :: SSTX1 Directionality support (0b == fixed, 1b == cfgable)
196  * <10>    :: SSTX2 Directionality support
197  * <9>     :: SSRX1 Directionality support
198  * <8>     :: SSRX2 Directionality support
199  * <7:5>   :: Vconn power
200  * <4>     :: Vconn power required
201  * <3>     :: Vbus power required
202  * <2:0>   :: USB SS Signaling support
203  */
204 #define VDO_AMA(hw, fw, tx1d, tx2d, rx1d, rx2d, vcpwr, vcr, vbr, usbss) \
205 	(((hw) & 0x7) << 28 | ((fw) & 0x7) << 24			\
206 	 | (tx1d) << 11 | (tx2d) << 10 | (rx1d) << 9 | (rx2d) << 8	\
207 	 | ((vcpwr) & 0x7) << 5 | (vcr) << 4 | (vbr) << 3		\
208 	 | ((usbss) & 0x7))
209 
210 #define PD_VDO_AMA_VCONN_REQ(vdo)	(((vdo) >> 4) & 1)
211 #define PD_VDO_AMA_VBUS_REQ(vdo)	(((vdo) >> 3) & 1)
212 
213 #define AMA_VCONN_PWR_1W	0
214 #define AMA_VCONN_PWR_1W5	1
215 #define AMA_VCONN_PWR_2W	2
216 #define AMA_VCONN_PWR_3W	3
217 #define AMA_VCONN_PWR_4W	4
218 #define AMA_VCONN_PWR_5W	5
219 #define AMA_VCONN_PWR_6W	6
220 #define AMA_USBSS_U2_ONLY	0
221 #define AMA_USBSS_U31_GEN1	1
222 #define AMA_USBSS_U31_GEN2	2
223 #define AMA_USBSS_BBONLY	3
224 
225 /*
226  * SVDM Discover SVIDs request -> response
227  *
228  * Request is properly formatted VDM Header with discover SVIDs command.
229  * Response is a set of SVIDs of all all supported SVIDs with all zero's to
230  * mark the end of SVIDs.  If more than 12 SVIDs are supported command SHOULD be
231  * repeated.
232  */
233 #define VDO_SVID(svid0, svid1)	(((svid0) & 0xffff) << 16 | ((svid1) & 0xffff))
234 #define PD_VDO_SVID_SVID0(vdo)	((vdo) >> 16)
235 #define PD_VDO_SVID_SVID1(vdo)	((vdo) & 0xffff)
236 
237 /* USB-IF SIDs */
238 #define USB_SID_PD		0xff00 /* power delivery */
239 #define USB_SID_DISPLAYPORT	0xff01
240 #define USB_SID_MHL		0xff02	/* Mobile High-Definition Link */
241 
242 /* VDM command timeouts (in ms) */
243 
244 #define PD_T_VDM_UNSTRUCTURED	500
245 #define PD_T_VDM_BUSY		100
246 #define PD_T_VDM_WAIT_MODE_E	100
247 #define PD_T_VDM_SNDR_RSP	30
248 #define PD_T_VDM_E_MODE		25
249 #define PD_T_VDM_RCVR_RSP	15
250 
251 #endif /* __LINUX_USB_PD_VDO_H */
252