dwc2.h revision 1.9.18.1 1 /* $NetBSD: dwc2.h,v 1.9.18.1 2019/06/10 22:08:33 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nick Hudson
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _EXTERNAL_BSD_DWC2_DWC2_H_
33 #define _EXTERNAL_BSD_DWC2_DWC2_H_
34
35 #include <sys/param.h>
36
37 #include <sys/callout.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/workqueue.h>
41
42 #include <linux/list.h>
43 #include <linux/workqueue.h>
44
45 #include "opt_usb.h"
46 // #define VERBOSE_DEBUG
47 // #define DWC2_DUMP_FRREM
48 // #define CONFIG_USB_DWC2_TRACK_MISSED_SOFS
49
50 #define CONFIG_USB_DWC2_HOST 1
51 #define CONFIG_USB_DWC2_DUAL_ROLE 0
52 #define CONFIG_USB_DWC2_PERIPHERAL 0
53
54 typedef int irqreturn_t;
55 #define IRQ_NONE 0
56 #define IRQ_HANDLED 1
57
58 #define u8 uint8_t
59 #define u16 uint16_t
60 #define s16 int16_t
61 #define u32 uint32_t
62 #define u64 uint64_t
63
64 #define dma_addr_t bus_addr_t
65
66 #define DWC2_READ_4(hsotg, reg) \
67 bus_space_read_4((hsotg)->hsotg_sc->sc_iot, (hsotg)->hsotg_sc->sc_ioh, (reg))
68 #define DWC2_WRITE_4(hsotg, reg, data) \
69 bus_space_write_4((hsotg)->hsotg_sc->sc_iot, (hsotg)->hsotg_sc->sc_ioh, (reg), (data));
70
71 #ifdef DWC2_DEBUG
72 extern int dwc2debug;
73 #define WARN_ON(x) KASSERT(!(x))
74
75 #define dev_info(d,fmt,...) do { \
76 printf("%s: " fmt, device_xname(d), \
77 ## __VA_ARGS__); \
78 } while (0)
79 #define dev_warn(d,fmt,...) do { \
80 printf("%s: " fmt, device_xname(d), \
81 ## __VA_ARGS__); \
82 } while (0)
83 #define dev_err(d,fmt,...) do { \
84 printf("%s: " fmt, device_xname(d), \
85 ## __VA_ARGS__); \
86 } while (0)
87 #define dev_dbg(d,fmt,...) do { \
88 if (dwc2debug >= 1) { \
89 printf("%s: " fmt, device_xname(d), \
90 ## __VA_ARGS__); \
91 } \
92 } while (0)
93 #define dev_vdbg(d,fmt,...) do { \
94 if (dwc2debug >= 2) { \
95 printf("%s: " fmt, device_xname(d), \
96 ## __VA_ARGS__); \
97 } \
98 } while (0)
99 #else
100 #define WARN_ON(x)
101 #define dev_info(...) do { } while (0)
102 #define dev_warn(...) do { } while (0)
103 #define dev_err(...) do { } while (0)
104 #define dev_dbg(...) do { } while (0)
105 #define dev_vdbg(...) do { } while (0)
106 #endif
107
108 #define jiffies hardclock_ticks
109 #define msecs_to_jiffies mstohz
110
111 enum usb_otg_state {
112 OTG_STATE_RESERVED = 0,
113
114 OTG_STATE_A_HOST,
115 OTG_STATE_A_PERIPHERAL,
116 OTG_STATE_A_SUSPEND,
117 OTG_STATE_B_HOST,
118 OTG_STATE_B_PERIPHERAL,
119 };
120
121 #define usleep_range(l, u) do { DELAY(u); } while (0)
122
123 #define spinlock_t kmutex_t
124 #define spin_lock_init(lock) mutex_init(lock, MUTEX_DEFAULT, IPL_VM)
125 #define spin_lock(l) do { mutex_spin_enter(l); } while (0)
126 #define spin_unlock(l) do { mutex_spin_exit(l); } while (0)
127
128 #define spin_lock_irqsave(l, f) \
129 do { mutex_spin_enter(l); (void)(f); } while (0)
130
131 #define spin_unlock_irqrestore(l, f) \
132 do { mutex_spin_exit(l); (void)(f); } while (0)
133
134 #define IRQ_RETVAL(r) (r)
135
136 #define USB_ENDPOINT_XFER_CONTROL UE_CONTROL /* 0 */
137 #define USB_ENDPOINT_XFER_ISOC UE_ISOCHRONOUS /* 1 */
138 #define USB_ENDPOINT_XFER_BULK UE_BULK /* 2 */
139 #define USB_ENDPOINT_XFER_INT UE_INTERRUPT /* 3 */
140
141 #define USB_DIR_IN UE_DIR_IN
142 #define USB_DIR_OUT UE_DIR_OUT
143
144 #define USB_PORT_FEAT_CONNECTION UHF_PORT_CONNECTION
145 #define USB_PORT_FEAT_ENABLE UHF_PORT_ENABLE
146 #define USB_PORT_FEAT_SUSPEND UHF_PORT_SUSPEND
147 #define USB_PORT_FEAT_OVER_CURRENT UHF_PORT_OVER_CURRENT
148 #define USB_PORT_FEAT_RESET UHF_PORT_RESET
149 // #define USB_PORT_FEAT_L1 5 /* L1 suspend */
150 #define USB_PORT_FEAT_POWER UHF_PORT_POWER
151 #define USB_PORT_FEAT_LOWSPEED UHF_PORT_LOW_SPEED
152 #define USB_PORT_FEAT_C_CONNECTION UHF_C_PORT_CONNECTION
153 #define USB_PORT_FEAT_C_ENABLE UHF_C_PORT_ENABLE
154 #define USB_PORT_FEAT_C_SUSPEND UHF_C_PORT_SUSPEND
155 #define USB_PORT_FEAT_C_OVER_CURRENT UHF_C_PORT_OVER_CURRENT
156 #define USB_PORT_FEAT_C_RESET UHF_C_PORT_RESET
157 #define USB_PORT_FEAT_TEST UHF_PORT_TEST
158 #define USB_PORT_FEAT_INDICATOR UHF_PORT_INDICATOR
159 #define USB_PORT_FEAT_C_PORT_L1 UHF_C_PORT_L1
160
161 #define C_HUB_LOCAL_POWER UHF_C_HUB_LOCAL_POWER
162 #define C_HUB_OVER_CURRENT UHF_C_HUB_OVER_CURRENT
163
164 #define USB_REQ_GET_STATUS UR_GET_STATUS
165 #define USB_REQ_CLEAR_FEATURE UR_CLEAR_FEATURE
166 #define USB_REQ_SET_FEATURE UR_SET_FEATURE
167 #define USB_REQ_GET_DESCRIPTOR UR_GET_DESCRIPTOR
168
169 #define ClearHubFeature ((UT_WRITE_CLASS_DEVICE << 8) | USB_REQ_CLEAR_FEATURE)
170 #define ClearPortFeature ((UT_WRITE_CLASS_OTHER << 8) | USB_REQ_CLEAR_FEATURE)
171 #define GetHubDescriptor ((UT_READ_CLASS_DEVICE << 8) | USB_REQ_GET_DESCRIPTOR)
172 #define GetHubStatus ((UT_READ_CLASS_DEVICE << 8) | USB_REQ_GET_STATUS)
173 #define GetPortStatus ((UT_READ_CLASS_OTHER << 8) | USB_REQ_GET_STATUS)
174 #define SetHubFeature ((UT_WRITE_CLASS_DEVICE << 8) | USB_REQ_SET_FEATURE)
175 #define SetPortFeature ((UT_WRITE_CLASS_OTHER << 8) | USB_REQ_SET_FEATURE)
176
177 #define USB_PORT_STAT_CONNECTION UPS_CURRENT_CONNECT_STATUS
178 #define USB_PORT_STAT_ENABLE UPS_PORT_ENABLED
179 #define USB_PORT_STAT_SUSPEND UPS_SUSPEND
180 #define USB_PORT_STAT_OVERCURRENT UPS_OVERCURRENT_INDICATOR
181 #define USB_PORT_STAT_RESET UPS_RESET
182 #define USB_PORT_STAT_L1 UPS_PORT_L1
183 #define USB_PORT_STAT_POWER UPS_PORT_POWER
184 #define USB_PORT_STAT_LOW_SPEED UPS_LOW_SPEED
185 #define USB_PORT_STAT_HIGH_SPEED UPS_HIGH_SPEED
186 #define USB_PORT_STAT_TEST UPS_PORT_TEST
187 #define USB_PORT_STAT_INDICATOR UPS_PORT_INDICATOR
188
189 #define USB_PORT_STAT_C_CONNECTION UPS_C_CONNECT_STATUS
190 #define USB_PORT_STAT_C_ENABLE UPS_C_PORT_ENABLED
191 #define USB_PORT_STAT_C_SUSPEND UPS_C_SUSPEND
192 #define USB_PORT_STAT_C_OVERCURRENT UPS_C_OVERCURRENT_INDICATOR
193 #define USB_PORT_STAT_C_RESET UPS_C_PORT_RESET
194 #define USB_PORT_STAT_C_L1 UPS_C_PORT_L1
195
196 #define USB_DT_HUB UDESC_HUB
197
198 /* See USB 2.0 spec Table 11-13, offset 3 */
199 #define HUB_CHAR_LPSM UHD_PWR
200 #define HUB_CHAR_COMMON_LPSM UHD_PWR_GANGED
201 #define HUB_CHAR_INDV_PORT_LPSM UHD_PWR_INDIVIDUAL
202 #define HUB_CHAR_NO_LPSM UHD_PWR_NO_SWITCH
203
204 #define HUB_CHAR_COMPOUND UHD_COMPOUND
205
206 #define HUB_CHAR_OCPM UHD_OC
207 #define HUB_CHAR_COMMON_OCPM UHD_OC_GLOBAL
208 #define HUB_CHAR_INDV_PORT_OCPM UHD_OC_INDIVIDUAL
209 #define HUB_CHAR_NO_OCPM UHD_OC_NONE
210
211 #define HUB_CHAR_TTTT UHD_TT_THINK
212 #define HUB_CHAR_PORTIND UHD_PORT_IND
213
214 enum usb_dr_mode {
215 USB_DR_MODE_UNKNOWN,
216 USB_DR_MODE_HOST,
217 USB_DR_MODE_PERIPHERAL,
218 USB_DR_MODE_OTG,
219 };
220
221 struct usb_phy;
222 struct usb_hcd;
223
224 static inline int
225 usb_phy_set_suspend(struct usb_phy *x, int suspend)
226 {
227
228 return 0;
229 }
230
231 static inline void
232 usb_hcd_resume_root_hub(struct usb_hcd *hcd)
233 {
234
235 return;
236 }
237
238 static inline int
239 usb_disabled(void)
240 {
241
242 return 0;
243 }
244
245 static inline void
246 udelay(unsigned long usecs)
247 {
248
249 DELAY(usecs);
250 }
251
252 static inline void
253 ndelay(unsigned long nsecs)
254 {
255
256 DELAY(nsecs / 1000);
257 }
258
259 static inline void
260 msleep(unsigned int msec)
261 {
262 if (cold ||
263 ((hz < 1000) && (msec < (1000/hz))))
264 udelay(msec * 1000);
265 else
266 (void)kpause("mdelay", false, mstohz(msec), NULL);
267 }
268
269 #define EREMOTEIO EIO
270 #define ECOMM EIO
271 #define ENOTSUPP ENOTSUP
272
273 #define NS_TO_US(ns) ((ns + 500L) / 1000L)
274
275 #define USB_RESUME_TIMEOUT 40 /* ms */
276
277 #endif
278