aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>2012-07-05 18:15:38 -0400
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2012-07-05 18:15:38 -0400
commit933141509cefd64102a943d61d154c5c53bad889 (patch)
treed36112785cb0742ab3b6d6c91cfd4777978b3220
parent94ff447cf275170042a7a05b972b6da8b94754ca (diff)
parent6f17f74b3d8345663f5fcfd834145685110075bd (diff)
Merge tag 'dwc3-for-v3.6' of git://git.kernel.org/pub/scm/linux/kernel/git/balbi/usb into usb-next
usb: dwc3: patches for v3.6 merge window lots of changes for the dwc3 driver which will make the driver a lot more stable and some changes which are just cosmetic. The bulk of changes is related to mis-defined macros which were never used before, some fixes to error path which e.g. prevent the driver from initiating two transfer on ep0out in case of stall, some fixes to request dequeueing and the End Transfer Command. We have some changes to U1/U2 handling where we were enabling those transtions at the wrong spot (though we haven't seen a problem from that as of today). A few patches will make it easier to add support for newer releases of the core by adding definitions for new registers and changing the code to act accordingly when certain revisions are detected. There's also the usual comestic changes to make the driver easier to maintain and make it easier for new- comers to understand the driver. Also one patch fixing a double inclusion of a header.
-rw-r--r--drivers/usb/dwc3/core.c13
-rw-r--r--drivers/usb/dwc3/core.h107
-rw-r--r--drivers/usb/dwc3/dwc3-exynos.c1
-rw-r--r--drivers/usb/dwc3/ep0.c146
-rw-r--r--drivers/usb/dwc3/gadget.c363
-rw-r--r--drivers/usb/dwc3/gadget.h6
6 files changed, 407 insertions, 229 deletions
diff --git a/drivers/usb/dwc3/core.c b/drivers/usb/dwc3/core.c
index 1040bdb8dc88..c34452a7304f 100644
--- a/drivers/usb/dwc3/core.c
+++ b/drivers/usb/dwc3/core.c
@@ -148,6 +148,8 @@ static void dwc3_core_soft_reset(struct dwc3 *dwc)
148 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST; 148 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
149 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 149 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
150 150
151 mdelay(100);
152
151 /* After PHYs are stable we can take Core out of reset state */ 153 /* After PHYs are stable we can take Core out of reset state */
152 reg = dwc3_readl(dwc->regs, DWC3_GCTL); 154 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
153 reg &= ~DWC3_GCTL_CORESOFTRESET; 155 reg &= ~DWC3_GCTL_CORESOFTRESET;
@@ -255,7 +257,7 @@ static int __devinit dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
255 * 257 *
256 * Returns 0 on success otherwise negative errno. 258 * Returns 0 on success otherwise negative errno.
257 */ 259 */
258static int __devinit dwc3_event_buffers_setup(struct dwc3 *dwc) 260static int dwc3_event_buffers_setup(struct dwc3 *dwc)
259{ 261{
260 struct dwc3_event_buffer *evt; 262 struct dwc3_event_buffer *evt;
261 int n; 263 int n;
@@ -266,6 +268,8 @@ static int __devinit dwc3_event_buffers_setup(struct dwc3 *dwc)
266 evt->buf, (unsigned long long) evt->dma, 268 evt->buf, (unsigned long long) evt->dma,
267 evt->length); 269 evt->length);
268 270
271 evt->lpos = 0;
272
269 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 273 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
270 lower_32_bits(evt->dma)); 274 lower_32_bits(evt->dma));
271 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 275 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
@@ -285,6 +289,9 @@ static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
285 289
286 for (n = 0; n < dwc->num_event_buffers; n++) { 290 for (n = 0; n < dwc->num_event_buffers; n++) {
287 evt = dwc->ev_buffs[n]; 291 evt = dwc->ev_buffs[n];
292
293 evt->lpos = 0;
294
288 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0); 295 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
289 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0); 296 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
290 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0); 297 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), 0);
@@ -328,8 +335,6 @@ static int __devinit dwc3_core_init(struct dwc3 *dwc)
328 } 335 }
329 dwc->revision = reg; 336 dwc->revision = reg;
330 337
331 dwc3_core_soft_reset(dwc);
332
333 /* issue device SoftReset too */ 338 /* issue device SoftReset too */
334 timeout = jiffies + msecs_to_jiffies(500); 339 timeout = jiffies + msecs_to_jiffies(500);
335 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST); 340 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
@@ -347,6 +352,8 @@ static int __devinit dwc3_core_init(struct dwc3 *dwc)
347 cpu_relax(); 352 cpu_relax();
348 } while (true); 353 } while (true);
349 354
355 dwc3_core_soft_reset(dwc);
356
350 dwc3_cache_hwparams(dwc); 357 dwc3_cache_hwparams(dwc);
351 358
352 reg = dwc3_readl(dwc->regs, DWC3_GCTL); 359 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
diff --git a/drivers/usb/dwc3/core.h b/drivers/usb/dwc3/core.h
index f69c877add09..151eca876dfd 100644
--- a/drivers/usb/dwc3/core.h
+++ b/drivers/usb/dwc3/core.h
@@ -67,6 +67,7 @@
67#define DWC3_DEVICE_EVENT_CONNECT_DONE 2 67#define DWC3_DEVICE_EVENT_CONNECT_DONE 2
68#define DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE 3 68#define DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE 3
69#define DWC3_DEVICE_EVENT_WAKEUP 4 69#define DWC3_DEVICE_EVENT_WAKEUP 4
70#define DWC3_DEVICE_EVENT_HIBER_REQ 5
70#define DWC3_DEVICE_EVENT_EOPF 6 71#define DWC3_DEVICE_EVENT_EOPF 6
71#define DWC3_DEVICE_EVENT_SOF 7 72#define DWC3_DEVICE_EVENT_SOF 7
72#define DWC3_DEVICE_EVENT_ERRATIC_ERROR 9 73#define DWC3_DEVICE_EVENT_ERRATIC_ERROR 9
@@ -171,28 +172,36 @@
171#define DWC3_GCTL_PRTCAP_DEVICE 2 172#define DWC3_GCTL_PRTCAP_DEVICE 2
172#define DWC3_GCTL_PRTCAP_OTG 3 173#define DWC3_GCTL_PRTCAP_OTG 3
173 174
174#define DWC3_GCTL_CORESOFTRESET (1 << 11) 175#define DWC3_GCTL_CORESOFTRESET (1 << 11)
175#define DWC3_GCTL_SCALEDOWN(n) ((n) << 4) 176#define DWC3_GCTL_SCALEDOWN(n) ((n) << 4)
176#define DWC3_GCTL_SCALEDOWN_MASK DWC3_GCTL_SCALEDOWN(3) 177#define DWC3_GCTL_SCALEDOWN_MASK DWC3_GCTL_SCALEDOWN(3)
177#define DWC3_GCTL_DISSCRAMBLE (1 << 3) 178#define DWC3_GCTL_DISSCRAMBLE (1 << 3)
178#define DWC3_GCTL_DSBLCLKGTNG (1 << 0) 179#define DWC3_GCTL_GBLHIBERNATIONEN (1 << 1)
180#define DWC3_GCTL_DSBLCLKGTNG (1 << 0)
179 181
180/* Global USB2 PHY Configuration Register */ 182/* Global USB2 PHY Configuration Register */
181#define DWC3_GUSB2PHYCFG_PHYSOFTRST (1 << 31) 183#define DWC3_GUSB2PHYCFG_PHYSOFTRST (1 << 31)
182#define DWC3_GUSB2PHYCFG_SUSPHY (1 << 6) 184#define DWC3_GUSB2PHYCFG_SUSPHY (1 << 6)
183 185
184/* Global USB3 PIPE Control Register */ 186/* Global USB3 PIPE Control Register */
185#define DWC3_GUSB3PIPECTL_PHYSOFTRST (1 << 31) 187#define DWC3_GUSB3PIPECTL_PHYSOFTRST (1 << 31)
186#define DWC3_GUSB3PIPECTL_SUSPHY (1 << 17) 188#define DWC3_GUSB3PIPECTL_SUSPHY (1 << 17)
187 189
188/* Global TX Fifo Size Register */ 190/* Global TX Fifo Size Register */
189#define DWC3_GTXFIFOSIZ_TXFDEF(n) ((n) & 0xffff) 191#define DWC3_GTXFIFOSIZ_TXFDEF(n) ((n) & 0xffff)
190#define DWC3_GTXFIFOSIZ_TXFSTADDR(n) ((n) & 0xffff0000) 192#define DWC3_GTXFIFOSIZ_TXFSTADDR(n) ((n) & 0xffff0000)
191 193
192/* Global HWPARAMS1 Register */ 194/* Global HWPARAMS1 Register */
193#define DWC3_GHWPARAMS1_EN_PWROPT(n) (((n) & (3 << 24)) >> 24) 195#define DWC3_GHWPARAMS1_EN_PWROPT(n) (((n) & (3 << 24)) >> 24)
194#define DWC3_GHWPARAMS1_EN_PWROPT_NO 0 196#define DWC3_GHWPARAMS1_EN_PWROPT_NO 0
195#define DWC3_GHWPARAMS1_EN_PWROPT_CLK 1 197#define DWC3_GHWPARAMS1_EN_PWROPT_CLK 1
198#define DWC3_GHWPARAMS1_EN_PWROPT_HIB 2
199#define DWC3_GHWPARAMS1_PWROPT(n) ((n) << 24)
200#define DWC3_GHWPARAMS1_PWROPT_MASK DWC3_GHWPARAMS1_PWROPT(3)
201
202/* Global HWPARAMS4 Register */
203#define DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(n) (((n) & (0x0f << 13)) >> 13)
204#define DWC3_MAX_HIBER_SCRATCHBUFS 15
196 205
197/* Device Configuration Register */ 206/* Device Configuration Register */
198#define DWC3_DCFG_LPM_CAP (1 << 22) 207#define DWC3_DCFG_LPM_CAP (1 << 22)
@@ -206,24 +215,32 @@
206#define DWC3_DCFG_LOWSPEED (2 << 0) 215#define DWC3_DCFG_LOWSPEED (2 << 0)
207#define DWC3_DCFG_FULLSPEED1 (3 << 0) 216#define DWC3_DCFG_FULLSPEED1 (3 << 0)
208 217
218#define DWC3_DCFG_LPM_CAP (1 << 22)
219
209/* Device Control Register */ 220/* Device Control Register */
210#define DWC3_DCTL_RUN_STOP (1 << 31) 221#define DWC3_DCTL_RUN_STOP (1 << 31)
211#define DWC3_DCTL_CSFTRST (1 << 30) 222#define DWC3_DCTL_CSFTRST (1 << 30)
212#define DWC3_DCTL_LSFTRST (1 << 29) 223#define DWC3_DCTL_LSFTRST (1 << 29)
213 224
214#define DWC3_DCTL_HIRD_THRES_MASK (0x1f << 24) 225#define DWC3_DCTL_HIRD_THRES_MASK (0x1f << 24)
215#define DWC3_DCTL_HIRD_THRES(n) (((n) & DWC3_DCTL_HIRD_THRES_MASK) >> 24) 226#define DWC3_DCTL_HIRD_THRES(n) ((n) << 24)
216 227
217#define DWC3_DCTL_APPL1RES (1 << 23) 228#define DWC3_DCTL_APPL1RES (1 << 23)
218 229
219#define DWC3_DCTL_TRGTULST_MASK (0x0f << 17) 230/* These apply for core versions 1.87a and earlier */
220#define DWC3_DCTL_TRGTULST(n) ((n) << 17) 231#define DWC3_DCTL_TRGTULST_MASK (0x0f << 17)
221 232#define DWC3_DCTL_TRGTULST(n) ((n) << 17)
222#define DWC3_DCTL_TRGTULST_U2 (DWC3_DCTL_TRGTULST(2)) 233#define DWC3_DCTL_TRGTULST_U2 (DWC3_DCTL_TRGTULST(2))
223#define DWC3_DCTL_TRGTULST_U3 (DWC3_DCTL_TRGTULST(3)) 234#define DWC3_DCTL_TRGTULST_U3 (DWC3_DCTL_TRGTULST(3))
224#define DWC3_DCTL_TRGTULST_SS_DIS (DWC3_DCTL_TRGTULST(4)) 235#define DWC3_DCTL_TRGTULST_SS_DIS (DWC3_DCTL_TRGTULST(4))
225#define DWC3_DCTL_TRGTULST_RX_DET (DWC3_DCTL_TRGTULST(5)) 236#define DWC3_DCTL_TRGTULST_RX_DET (DWC3_DCTL_TRGTULST(5))
226#define DWC3_DCTL_TRGTULST_SS_INACT (DWC3_DCTL_TRGTULST(6)) 237#define DWC3_DCTL_TRGTULST_SS_INACT (DWC3_DCTL_TRGTULST(6))
238
239/* These apply for core versions 1.94a and later */
240#define DWC3_DCTL_KEEP_CONNECT (1 << 19)
241#define DWC3_DCTL_L1_HIBER_EN (1 << 18)
242#define DWC3_DCTL_CRS (1 << 17)
243#define DWC3_DCTL_CSS (1 << 16)
227 244
228#define DWC3_DCTL_INITU2ENA (1 << 12) 245#define DWC3_DCTL_INITU2ENA (1 << 12)
229#define DWC3_DCTL_ACCEPTU2ENA (1 << 11) 246#define DWC3_DCTL_ACCEPTU2ENA (1 << 11)
@@ -249,6 +266,7 @@
249#define DWC3_DEVTEN_ERRTICERREN (1 << 9) 266#define DWC3_DEVTEN_ERRTICERREN (1 << 9)
250#define DWC3_DEVTEN_SOFEN (1 << 7) 267#define DWC3_DEVTEN_SOFEN (1 << 7)
251#define DWC3_DEVTEN_EOPFEN (1 << 6) 268#define DWC3_DEVTEN_EOPFEN (1 << 6)
269#define DWC3_DEVTEN_HIBERNATIONREQEVTEN (1 << 5)
252#define DWC3_DEVTEN_WKUPEVTEN (1 << 4) 270#define DWC3_DEVTEN_WKUPEVTEN (1 << 4)
253#define DWC3_DEVTEN_ULSTCNGEN (1 << 3) 271#define DWC3_DEVTEN_ULSTCNGEN (1 << 3)
254#define DWC3_DEVTEN_CONNECTDONEEN (1 << 2) 272#define DWC3_DEVTEN_CONNECTDONEEN (1 << 2)
@@ -256,7 +274,15 @@
256#define DWC3_DEVTEN_DISCONNEVTEN (1 << 0) 274#define DWC3_DEVTEN_DISCONNEVTEN (1 << 0)
257 275
258/* Device Status Register */ 276/* Device Status Register */
277#define DWC3_DSTS_DCNRD (1 << 29)
278
279/* This applies for core versions 1.87a and earlier */
259#define DWC3_DSTS_PWRUPREQ (1 << 24) 280#define DWC3_DSTS_PWRUPREQ (1 << 24)
281
282/* These apply for core versions 1.94a and later */
283#define DWC3_DSTS_RSS (1 << 25)
284#define DWC3_DSTS_SSS (1 << 24)
285
260#define DWC3_DSTS_COREIDLE (1 << 23) 286#define DWC3_DSTS_COREIDLE (1 << 23)
261#define DWC3_DSTS_DEVCTRLHLT (1 << 22) 287#define DWC3_DSTS_DEVCTRLHLT (1 << 22)
262 288
@@ -265,7 +291,7 @@
265 291
266#define DWC3_DSTS_RXFIFOEMPTY (1 << 17) 292#define DWC3_DSTS_RXFIFOEMPTY (1 << 17)
267 293
268#define DWC3_DSTS_SOFFN_MASK (0x3ff << 3) 294#define DWC3_DSTS_SOFFN_MASK (0x3fff << 3)
269#define DWC3_DSTS_SOFFN(n) (((n) & DWC3_DSTS_SOFFN_MASK) >> 3) 295#define DWC3_DSTS_SOFFN(n) (((n) & DWC3_DSTS_SOFFN_MASK) >> 3)
270 296
271#define DWC3_DSTS_CONNECTSPD (7 << 0) 297#define DWC3_DSTS_CONNECTSPD (7 << 0)
@@ -280,6 +306,11 @@
280#define DWC3_DGCMD_SET_LMP 0x01 306#define DWC3_DGCMD_SET_LMP 0x01
281#define DWC3_DGCMD_SET_PERIODIC_PAR 0x02 307#define DWC3_DGCMD_SET_PERIODIC_PAR 0x02
282#define DWC3_DGCMD_XMIT_FUNCTION 0x03 308#define DWC3_DGCMD_XMIT_FUNCTION 0x03
309
310/* These apply for core versions 1.94a and later */
311#define DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO 0x04
312#define DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI 0x05
313
283#define DWC3_DGCMD_SELECTED_FIFO_FLUSH 0x09 314#define DWC3_DGCMD_SELECTED_FIFO_FLUSH 0x09
284#define DWC3_DGCMD_ALL_FIFO_FLUSH 0x0a 315#define DWC3_DGCMD_ALL_FIFO_FLUSH 0x0a
285#define DWC3_DGCMD_SET_ENDPOINT_NRDY 0x0c 316#define DWC3_DGCMD_SET_ENDPOINT_NRDY 0x0c
@@ -287,6 +318,15 @@
287 318
288#define DWC3_DGCMD_STATUS(n) (((n) >> 15) & 1) 319#define DWC3_DGCMD_STATUS(n) (((n) >> 15) & 1)
289#define DWC3_DGCMD_CMDACT (1 << 10) 320#define DWC3_DGCMD_CMDACT (1 << 10)
321#define DWC3_DGCMD_CMDIOC (1 << 8)
322
323/* Device Generic Command Parameter Register */
324#define DWC3_DGCMDPAR_FORCE_LINKPM_ACCEPT (1 << 0)
325#define DWC3_DGCMDPAR_FIFO_NUM(n) ((n) << 0)
326#define DWC3_DGCMDPAR_RX_FIFO (0 << 5)
327#define DWC3_DGCMDPAR_TX_FIFO (1 << 5)
328#define DWC3_DGCMDPAR_LOOPBACK_DIS (0 << 0)
329#define DWC3_DGCMDPAR_LOOPBACK_ENA (1 << 0)
290 330
291/* Device Endpoint Command Register */ 331/* Device Endpoint Command Register */
292#define DWC3_DEPCMD_PARAM_SHIFT 16 332#define DWC3_DEPCMD_PARAM_SHIFT 16
@@ -303,7 +343,10 @@
303#define DWC3_DEPCMD_STARTTRANSFER (0x06 << 0) 343#define DWC3_DEPCMD_STARTTRANSFER (0x06 << 0)
304#define DWC3_DEPCMD_CLEARSTALL (0x05 << 0) 344#define DWC3_DEPCMD_CLEARSTALL (0x05 << 0)
305#define DWC3_DEPCMD_SETSTALL (0x04 << 0) 345#define DWC3_DEPCMD_SETSTALL (0x04 << 0)
346/* This applies for core versions 1.90a and earlier */
306#define DWC3_DEPCMD_GETSEQNUMBER (0x03 << 0) 347#define DWC3_DEPCMD_GETSEQNUMBER (0x03 << 0)
348/* This applies for core versions 1.94a and later */
349#define DWC3_DEPCMD_GETEPSTATE (0x03 << 0)
307#define DWC3_DEPCMD_SETTRANSFRESOURCE (0x02 << 0) 350#define DWC3_DEPCMD_SETTRANSFRESOURCE (0x02 << 0)
308#define DWC3_DEPCMD_SETEPCONFIG (0x01 << 0) 351#define DWC3_DEPCMD_SETEPCONFIG (0x01 << 0)
309 352
@@ -361,7 +404,8 @@ struct dwc3_event_buffer {
361 * @current_trb: index of current used trb 404 * @current_trb: index of current used trb
362 * @number: endpoint number (1 - 15) 405 * @number: endpoint number (1 - 15)
363 * @type: set to bmAttributes & USB_ENDPOINT_XFERTYPE_MASK 406 * @type: set to bmAttributes & USB_ENDPOINT_XFERTYPE_MASK
364 * @res_trans_idx: Resource transfer index 407 * @resource_index: Resource transfer index
408 * @current_uf: Current uf received through last event parameter
365 * @interval: the intervall on which the ISOC transfer is started 409 * @interval: the intervall on which the ISOC transfer is started
366 * @name: a human readable name e.g. ep1out-bulk 410 * @name: a human readable name e.g. ep1out-bulk
367 * @direction: true for TX, false for RX 411 * @direction: true for TX, false for RX
@@ -385,6 +429,7 @@ struct dwc3_ep {
385#define DWC3_EP_WEDGE (1 << 2) 429#define DWC3_EP_WEDGE (1 << 2)
386#define DWC3_EP_BUSY (1 << 4) 430#define DWC3_EP_BUSY (1 << 4)
387#define DWC3_EP_PENDING_REQUEST (1 << 5) 431#define DWC3_EP_PENDING_REQUEST (1 << 5)
432#define DWC3_EP_MISSED_ISOC (1 << 6)
388 433
389 /* This last one is specific to EP0 */ 434 /* This last one is specific to EP0 */
390#define DWC3_EP0_DIR_IN (1 << 31) 435#define DWC3_EP0_DIR_IN (1 << 31)
@@ -393,7 +438,8 @@ struct dwc3_ep {
393 438
394 u8 number; 439 u8 number;
395 u8 type; 440 u8 type;
396 u8 res_trans_idx; 441 u8 resource_index;
442 u16 current_uf;
397 u32 interval; 443 u32 interval;
398 444
399 char name[20]; 445 char name[20];
@@ -437,6 +483,8 @@ enum dwc3_link_state {
437 DWC3_LINK_STATE_HRESET = 0x09, 483 DWC3_LINK_STATE_HRESET = 0x09,
438 DWC3_LINK_STATE_CMPLY = 0x0a, 484 DWC3_LINK_STATE_CMPLY = 0x0a,
439 DWC3_LINK_STATE_LPBK = 0x0b, 485 DWC3_LINK_STATE_LPBK = 0x0b,
486 DWC3_LINK_STATE_RESET = 0x0e,
487 DWC3_LINK_STATE_RESUME = 0x0f,
440 DWC3_LINK_STATE_MASK = 0x0f, 488 DWC3_LINK_STATE_MASK = 0x0f,
441}; 489};
442 490
@@ -450,11 +498,12 @@ enum dwc3_device_state {
450#define DWC3_TRB_SIZE_MASK (0x00ffffff) 498#define DWC3_TRB_SIZE_MASK (0x00ffffff)
451#define DWC3_TRB_SIZE_LENGTH(n) ((n) & DWC3_TRB_SIZE_MASK) 499#define DWC3_TRB_SIZE_LENGTH(n) ((n) & DWC3_TRB_SIZE_MASK)
452#define DWC3_TRB_SIZE_PCM1(n) (((n) & 0x03) << 24) 500#define DWC3_TRB_SIZE_PCM1(n) (((n) & 0x03) << 24)
453#define DWC3_TRB_SIZE_TRBSTS(n) (((n) & (0x0f << 28) >> 28)) 501#define DWC3_TRB_SIZE_TRBSTS(n) (((n) & (0x0f << 28)) >> 28)
454 502
455#define DWC3_TRBSTS_OK 0 503#define DWC3_TRBSTS_OK 0
456#define DWC3_TRBSTS_MISSED_ISOC 1 504#define DWC3_TRBSTS_MISSED_ISOC 1
457#define DWC3_TRBSTS_SETUP_PENDING 2 505#define DWC3_TRBSTS_SETUP_PENDING 2
506#define DWC3_TRB_STS_XFER_IN_PROG 4
458 507
459/* TRB Control */ 508/* TRB Control */
460#define DWC3_TRB_CTRL_HWO (1 << 0) 509#define DWC3_TRB_CTRL_HWO (1 << 0)
@@ -543,6 +592,14 @@ struct dwc3_request {
543 unsigned queued:1; 592 unsigned queued:1;
544}; 593};
545 594
595/*
596 * struct dwc3_scratchpad_array - hibernation scratchpad array
597 * (format defined by hw)
598 */
599struct dwc3_scratchpad_array {
600 __le64 dma_adr[DWC3_MAX_HIBER_SCRATCHBUFS];
601};
602
546/** 603/**
547 * struct dwc3 - representation of our controller 604 * struct dwc3 - representation of our controller
548 * @ctrl_req: usb control request which is used for ep0 605 * @ctrl_req: usb control request which is used for ep0
@@ -624,8 +681,10 @@ struct dwc3 {
624#define DWC3_REVISION_180A 0x5533180a 681#define DWC3_REVISION_180A 0x5533180a
625#define DWC3_REVISION_183A 0x5533183a 682#define DWC3_REVISION_183A 0x5533183a
626#define DWC3_REVISION_185A 0x5533185a 683#define DWC3_REVISION_185A 0x5533185a
684#define DWC3_REVISION_187A 0x5533187a
627#define DWC3_REVISION_188A 0x5533188a 685#define DWC3_REVISION_188A 0x5533188a
628#define DWC3_REVISION_190A 0x5533190a 686#define DWC3_REVISION_190A 0x5533190a
687#define DWC3_REVISION_194A 0x5533194a
629#define DWC3_REVISION_200A 0x5533200a 688#define DWC3_REVISION_200A 0x5533200a
630#define DWC3_REVISION_202A 0x5533202a 689#define DWC3_REVISION_202A 0x5533202a
631#define DWC3_REVISION_210A 0x5533210a 690#define DWC3_REVISION_210A 0x5533210a
diff --git a/drivers/usb/dwc3/dwc3-exynos.c b/drivers/usb/dwc3/dwc3-exynos.c
index d19030198086..b8f00389fa34 100644
--- a/drivers/usb/dwc3/dwc3-exynos.c
+++ b/drivers/usb/dwc3/dwc3-exynos.c
@@ -18,7 +18,6 @@
18#include <linux/platform_device.h> 18#include <linux/platform_device.h>
19#include <linux/platform_data/dwc3-exynos.h> 19#include <linux/platform_data/dwc3-exynos.h>
20#include <linux/dma-mapping.h> 20#include <linux/dma-mapping.h>
21#include <linux/module.h>
22#include <linux/clk.h> 21#include <linux/clk.h>
23 22
24#include "core.h" 23#include "core.h"
diff --git a/drivers/usb/dwc3/ep0.c b/drivers/usb/dwc3/ep0.c
index 9e8a3dce69fd..9b94886b66e5 100644
--- a/drivers/usb/dwc3/ep0.c
+++ b/drivers/usb/dwc3/ep0.c
@@ -54,7 +54,9 @@
54#include "gadget.h" 54#include "gadget.h"
55#include "io.h" 55#include "io.h"
56 56
57static void dwc3_ep0_do_control_status(struct dwc3 *dwc, u32 epnum); 57static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep);
58static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
59 struct dwc3_ep *dep, struct dwc3_request *req);
58 60
59static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state) 61static const char *dwc3_ep0_state_string(enum dwc3_ep0_state state)
60{ 62{
@@ -111,7 +113,7 @@ static int dwc3_ep0_start_trans(struct dwc3 *dwc, u8 epnum, dma_addr_t buf_dma,
111 } 113 }
112 114
113 dep->flags |= DWC3_EP_BUSY; 115 dep->flags |= DWC3_EP_BUSY;
114 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc, 116 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
115 dep->number); 117 dep->number);
116 118
117 dwc->ep0_next_event = DWC3_EP0_COMPLETE; 119 dwc->ep0_next_event = DWC3_EP0_COMPLETE;
@@ -150,16 +152,15 @@ static int __dwc3_gadget_ep0_queue(struct dwc3_ep *dep,
150 return 0; 152 return 0;
151 } 153 }
152 154
153 ret = dwc3_ep0_start_trans(dwc, direction, 155 __dwc3_ep0_do_control_data(dwc, dwc->eps[direction], req);
154 req->request.dma, req->request.length, 156
155 DWC3_TRBCTL_CONTROL_DATA);
156 dep->flags &= ~(DWC3_EP_PENDING_REQUEST | 157 dep->flags &= ~(DWC3_EP_PENDING_REQUEST |
157 DWC3_EP0_DIR_IN); 158 DWC3_EP0_DIR_IN);
158 } else if (dwc->delayed_status) { 159 } else if (dwc->delayed_status) {
159 dwc->delayed_status = false; 160 dwc->delayed_status = false;
160 161
161 if (dwc->ep0state == EP0_STATUS_PHASE) 162 if (dwc->ep0state == EP0_STATUS_PHASE)
162 dwc3_ep0_do_control_status(dwc, 1); 163 __dwc3_ep0_do_control_status(dwc, dwc->eps[1]);
163 else 164 else
164 dev_dbg(dwc->dev, "too early for delayed status\n"); 165 dev_dbg(dwc->dev, "too early for delayed status\n");
165 } 166 }
@@ -224,6 +225,16 @@ static void dwc3_ep0_stall_and_restart(struct dwc3 *dwc)
224 dwc3_ep0_out_start(dwc); 225 dwc3_ep0_out_start(dwc);
225} 226}
226 227
228int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value)
229{
230 struct dwc3_ep *dep = to_dwc3_ep(ep);
231 struct dwc3 *dwc = dep->dwc;
232
233 dwc3_ep0_stall_and_restart(dwc);
234
235 return 0;
236}
237
227void dwc3_ep0_out_start(struct dwc3 *dwc) 238void dwc3_ep0_out_start(struct dwc3 *dwc)
228{ 239{
229 int ret; 240 int ret;
@@ -463,6 +474,7 @@ static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
463{ 474{
464 u32 cfg; 475 u32 cfg;
465 int ret; 476 int ret;
477 u32 reg;
466 478
467 dwc->start_config_issued = false; 479 dwc->start_config_issued = false;
468 cfg = le16_to_cpu(ctrl->wValue); 480 cfg = le16_to_cpu(ctrl->wValue);
@@ -477,6 +489,14 @@ static int dwc3_ep0_set_config(struct dwc3 *dwc, struct usb_ctrlrequest *ctrl)
477 /* if the cfg matches and the cfg is non zero */ 489 /* if the cfg matches and the cfg is non zero */
478 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) { 490 if (cfg && (!ret || (ret == USB_GADGET_DELAYED_STATUS))) {
479 dwc->dev_state = DWC3_CONFIGURED_STATE; 491 dwc->dev_state = DWC3_CONFIGURED_STATE;
492 /*
493 * Enable transition to U1/U2 state when
494 * nothing is pending from application.
495 */
496 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
497 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
498 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
499
480 dwc->resize_fifos = true; 500 dwc->resize_fifos = true;
481 dev_dbg(dwc->dev, "resize fifos flag SET\n"); 501 dev_dbg(dwc->dev, "resize fifos flag SET\n");
482 } 502 }
@@ -514,8 +534,8 @@ static void dwc3_ep0_set_sel_cmpl(struct usb_ep *ep, struct usb_request *req)
514 534
515 dwc->u1sel = timing.u1sel; 535 dwc->u1sel = timing.u1sel;
516 dwc->u1pel = timing.u1pel; 536 dwc->u1pel = timing.u1pel;
517 dwc->u2sel = timing.u2sel; 537 dwc->u2sel = le16_to_cpu(timing.u2sel);
518 dwc->u2pel = timing.u2pel; 538 dwc->u2pel = le16_to_cpu(timing.u2pel);
519 539
520 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 540 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
521 if (reg & DWC3_DCTL_INITU2ENA) 541 if (reg & DWC3_DCTL_INITU2ENA)
@@ -640,11 +660,11 @@ static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
640 const struct dwc3_event_depevt *event) 660 const struct dwc3_event_depevt *event)
641{ 661{
642 struct usb_ctrlrequest *ctrl = dwc->ctrl_req; 662 struct usb_ctrlrequest *ctrl = dwc->ctrl_req;
643 int ret; 663 int ret = -EINVAL;
644 u32 len; 664 u32 len;
645 665
646 if (!dwc->gadget_driver) 666 if (!dwc->gadget_driver)
647 goto err; 667 goto out;
648 668
649 len = le16_to_cpu(ctrl->wLength); 669 len = le16_to_cpu(ctrl->wLength);
650 if (!len) { 670 if (!len) {
@@ -665,11 +685,9 @@ static void dwc3_ep0_inspect_setup(struct dwc3 *dwc,
665 if (ret == USB_GADGET_DELAYED_STATUS) 685 if (ret == USB_GADGET_DELAYED_STATUS)
666 dwc->delayed_status = true; 686 dwc->delayed_status = true;
667 687
668 if (ret >= 0) 688out:
669 return; 689 if (ret < 0)
670 690 dwc3_ep0_stall_and_restart(dwc);
671err:
672 dwc3_ep0_stall_and_restart(dwc);
673} 691}
674 692
675static void dwc3_ep0_complete_data(struct dwc3 *dwc, 693static void dwc3_ep0_complete_data(struct dwc3 *dwc,
@@ -723,7 +741,7 @@ static void dwc3_ep0_complete_data(struct dwc3 *dwc,
723 } 741 }
724} 742}
725 743
726static void dwc3_ep0_complete_req(struct dwc3 *dwc, 744static void dwc3_ep0_complete_status(struct dwc3 *dwc,
727 const struct dwc3_event_depevt *event) 745 const struct dwc3_event_depevt *event)
728{ 746{
729 struct dwc3_request *r; 747 struct dwc3_request *r;
@@ -745,6 +763,7 @@ static void dwc3_ep0_complete_req(struct dwc3 *dwc,
745 dev_dbg(dwc->dev, "Invalid Test #%d\n", 763 dev_dbg(dwc->dev, "Invalid Test #%d\n",
746 dwc->test_mode_nr); 764 dwc->test_mode_nr);
747 dwc3_ep0_stall_and_restart(dwc); 765 dwc3_ep0_stall_and_restart(dwc);
766 return;
748 } 767 }
749 } 768 }
750 769
@@ -758,7 +777,7 @@ static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
758 struct dwc3_ep *dep = dwc->eps[event->endpoint_number]; 777 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
759 778
760 dep->flags &= ~DWC3_EP_BUSY; 779 dep->flags &= ~DWC3_EP_BUSY;
761 dep->res_trans_idx = 0; 780 dep->resource_index = 0;
762 dwc->setup_packet_pending = false; 781 dwc->setup_packet_pending = false;
763 782
764 switch (dwc->ep0state) { 783 switch (dwc->ep0state) {
@@ -774,7 +793,7 @@ static void dwc3_ep0_xfer_complete(struct dwc3 *dwc,
774 793
775 case EP0_STATUS_PHASE: 794 case EP0_STATUS_PHASE:
776 dev_vdbg(dwc->dev, "Status Phase\n"); 795 dev_vdbg(dwc->dev, "Status Phase\n");
777 dwc3_ep0_complete_req(dwc, event); 796 dwc3_ep0_complete_status(dwc, event);
778 break; 797 break;
779 default: 798 default:
780 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state); 799 WARN(true, "UNKNOWN ep0state %d\n", dwc->ep0state);
@@ -787,68 +806,81 @@ static void dwc3_ep0_do_control_setup(struct dwc3 *dwc,
787 dwc3_ep0_out_start(dwc); 806 dwc3_ep0_out_start(dwc);
788} 807}
789 808
790static void dwc3_ep0_do_control_data(struct dwc3 *dwc, 809static void __dwc3_ep0_do_control_data(struct dwc3 *dwc,
791 const struct dwc3_event_depevt *event) 810 struct dwc3_ep *dep, struct dwc3_request *req)
792{ 811{
793 struct dwc3_ep *dep;
794 struct dwc3_request *req;
795 int ret; 812 int ret;
796 813
797 dep = dwc->eps[0]; 814 req->direction = !!dep->number;
798
799 if (list_empty(&dep->request_list)) {
800 dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n");
801 dep->flags |= DWC3_EP_PENDING_REQUEST;
802
803 if (event->endpoint_number)
804 dep->flags |= DWC3_EP0_DIR_IN;
805 return;
806 }
807
808 req = next_request(&dep->request_list);
809 req->direction = !!event->endpoint_number;
810 815
811 if (req->request.length == 0) { 816 if (req->request.length == 0) {
812 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number, 817 ret = dwc3_ep0_start_trans(dwc, dep->number,
813 dwc->ctrl_req_addr, 0, 818 dwc->ctrl_req_addr, 0,
814 DWC3_TRBCTL_CONTROL_DATA); 819 DWC3_TRBCTL_CONTROL_DATA);
815 } else if ((req->request.length % dep->endpoint.maxpacket) 820 } else if (!IS_ALIGNED(req->request.length, dep->endpoint.maxpacket)
816 && (event->endpoint_number == 0)) { 821 && (dep->number == 0)) {
822 u32 transfer_size;
823
817 ret = usb_gadget_map_request(&dwc->gadget, &req->request, 824 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
818 event->endpoint_number); 825 dep->number);
819 if (ret) { 826 if (ret) {
820 dev_dbg(dwc->dev, "failed to map request\n"); 827 dev_dbg(dwc->dev, "failed to map request\n");
821 return; 828 return;
822 } 829 }
823 830
824 WARN_ON(req->request.length > dep->endpoint.maxpacket); 831 WARN_ON(req->request.length > DWC3_EP0_BOUNCE_SIZE);
832
833 transfer_size = roundup(req->request.length,
834 (u32) dep->endpoint.maxpacket);
825 835
826 dwc->ep0_bounced = true; 836 dwc->ep0_bounced = true;
827 837
828 /* 838 /*
829 * REVISIT in case request length is bigger than EP0 839 * REVISIT in case request length is bigger than
830 * wMaxPacketSize, we will need two chained TRBs to handle 840 * DWC3_EP0_BOUNCE_SIZE we will need two chained
831 * the transfer. 841 * TRBs to handle the transfer.
832 */ 842 */
833 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number, 843 ret = dwc3_ep0_start_trans(dwc, dep->number,
834 dwc->ep0_bounce_addr, dep->endpoint.maxpacket, 844 dwc->ep0_bounce_addr, transfer_size,
835 DWC3_TRBCTL_CONTROL_DATA); 845 DWC3_TRBCTL_CONTROL_DATA);
836 } else { 846 } else {
837 ret = usb_gadget_map_request(&dwc->gadget, &req->request, 847 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
838 event->endpoint_number); 848 dep->number);
839 if (ret) { 849 if (ret) {
840 dev_dbg(dwc->dev, "failed to map request\n"); 850 dev_dbg(dwc->dev, "failed to map request\n");
841 return; 851 return;
842 } 852 }
843 853
844 ret = dwc3_ep0_start_trans(dwc, event->endpoint_number, 854 ret = dwc3_ep0_start_trans(dwc, dep->number, req->request.dma,
845 req->request.dma, req->request.length, 855 req->request.length, DWC3_TRBCTL_CONTROL_DATA);
846 DWC3_TRBCTL_CONTROL_DATA);
847 } 856 }
848 857
849 WARN_ON(ret < 0); 858 WARN_ON(ret < 0);
850} 859}
851 860
861static void dwc3_ep0_do_control_data(struct dwc3 *dwc,
862 const struct dwc3_event_depevt *event)
863{
864 struct dwc3_ep *dep;
865 struct dwc3_request *req;
866
867 dep = dwc->eps[0];
868
869 if (list_empty(&dep->request_list)) {
870 dev_vdbg(dwc->dev, "pending request for EP0 Data phase\n");
871 dep->flags |= DWC3_EP_PENDING_REQUEST;
872
873 if (event->endpoint_number)
874 dep->flags |= DWC3_EP0_DIR_IN;
875 return;
876 }
877
878 req = next_request(&dep->request_list);
879 dep = dwc->eps[event->endpoint_number];
880
881 __dwc3_ep0_do_control_data(dwc, dep, req);
882}
883
852static int dwc3_ep0_start_control_status(struct dwc3_ep *dep) 884static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
853{ 885{
854 struct dwc3 *dwc = dep->dwc; 886 struct dwc3 *dwc = dep->dwc;
@@ -861,10 +893,8 @@ static int dwc3_ep0_start_control_status(struct dwc3_ep *dep)
861 dwc->ctrl_req_addr, 0, type); 893 dwc->ctrl_req_addr, 0, type);
862} 894}
863 895
864static void dwc3_ep0_do_control_status(struct dwc3 *dwc, u32 epnum) 896static void __dwc3_ep0_do_control_status(struct dwc3 *dwc, struct dwc3_ep *dep)
865{ 897{
866 struct dwc3_ep *dep = dwc->eps[epnum];
867
868 if (dwc->resize_fifos) { 898 if (dwc->resize_fifos) {
869 dev_dbg(dwc->dev, "starting to resize fifos\n"); 899 dev_dbg(dwc->dev, "starting to resize fifos\n");
870 dwc3_gadget_resize_tx_fifos(dwc); 900 dwc3_gadget_resize_tx_fifos(dwc);
@@ -874,13 +904,21 @@ static void dwc3_ep0_do_control_status(struct dwc3 *dwc, u32 epnum)
874 WARN_ON(dwc3_ep0_start_control_status(dep)); 904 WARN_ON(dwc3_ep0_start_control_status(dep));
875} 905}
876 906
907static void dwc3_ep0_do_control_status(struct dwc3 *dwc,
908 const struct dwc3_event_depevt *event)
909{
910 struct dwc3_ep *dep = dwc->eps[event->endpoint_number];
911
912 __dwc3_ep0_do_control_status(dwc, dep);
913}
914
877static void dwc3_ep0_xfernotready(struct dwc3 *dwc, 915static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
878 const struct dwc3_event_depevt *event) 916 const struct dwc3_event_depevt *event)
879{ 917{
880 dwc->setup_packet_pending = true; 918 dwc->setup_packet_pending = true;
881 919
882 /* 920 /*
883 * This part is very tricky: If we has just handled 921 * This part is very tricky: If we have just handled
884 * XferNotReady(Setup) and we're now expecting a 922 * XferNotReady(Setup) and we're now expecting a
885 * XferComplete but, instead, we receive another 923 * XferComplete but, instead, we receive another
886 * XferNotReady(Setup), we should STALL and restart 924 * XferNotReady(Setup), we should STALL and restart
@@ -974,7 +1012,7 @@ static void dwc3_ep0_xfernotready(struct dwc3 *dwc,
974 return; 1012 return;
975 } 1013 }
976 1014
977 dwc3_ep0_do_control_status(dwc, event->endpoint_number); 1015 dwc3_ep0_do_control_status(dwc, event);
978 } 1016 }
979} 1017}
980 1018
diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c
index 4f3e8811daef..58fdfad96b4d 100644
--- a/drivers/usb/dwc3/gadget.c
+++ b/drivers/usb/dwc3/gadget.c
@@ -100,6 +100,23 @@ int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
100 int retries = 10000; 100 int retries = 10000;
101 u32 reg; 101 u32 reg;
102 102
103 /*
104 * Wait until device controller is ready. Only applies to 1.94a and
105 * later RTL.
106 */
107 if (dwc->revision >= DWC3_REVISION_194A) {
108 while (--retries) {
109 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
110 if (reg & DWC3_DSTS_DCNRD)
111 udelay(5);
112 else
113 break;
114 }
115
116 if (retries <= 0)
117 return -ETIMEDOUT;
118 }
119
103 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 120 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
104 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; 121 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
105 122
@@ -107,7 +124,15 @@ int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
107 reg |= DWC3_DCTL_ULSTCHNGREQ(state); 124 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
108 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 125 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
109 126
127 /*
128 * The following code is racy when called from dwc3_gadget_wakeup,
129 * and is not needed, at least on newer versions
130 */
131 if (dwc->revision >= DWC3_REVISION_194A)
132 return 0;
133
110 /* wait for a change in DSTS */ 134 /* wait for a change in DSTS */
135 retries = 10000;
111 while (--retries) { 136 while (--retries) {
112 reg = dwc3_readl(dwc->regs, DWC3_DSTS); 137 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
113 138
@@ -265,8 +290,8 @@ static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
265 return "Clear Stall"; 290 return "Clear Stall";
266 case DWC3_DEPCMD_SETSTALL: 291 case DWC3_DEPCMD_SETSTALL:
267 return "Set Stall"; 292 return "Set Stall";
268 case DWC3_DEPCMD_GETSEQNUMBER: 293 case DWC3_DEPCMD_GETEPSTATE:
269 return "Get Data Sequence Number"; 294 return "Get Endpoint State";
270 case DWC3_DEPCMD_SETTRANSFRESOURCE: 295 case DWC3_DEPCMD_SETTRANSFRESOURCE:
271 return "Set Endpoint Transfer Resource"; 296 return "Set Endpoint Transfer Resource";
272 case DWC3_DEPCMD_SETEPCONFIG: 297 case DWC3_DEPCMD_SETEPCONFIG:
@@ -530,9 +555,37 @@ static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
530{ 555{
531 struct dwc3_request *req; 556 struct dwc3_request *req;
532 557
533 if (!list_empty(&dep->req_queued)) 558 if (!list_empty(&dep->req_queued)) {
534 dwc3_stop_active_transfer(dwc, dep->number); 559 dwc3_stop_active_transfer(dwc, dep->number);
535 560
561 /*
562 * NOTICE: We are violating what the Databook says about the
563 * EndTransfer command. Ideally we would _always_ wait for the
564 * EndTransfer Command Completion IRQ, but that's causing too
565 * much trouble synchronizing between us and gadget driver.
566 *
567 * We have discussed this with the IP Provider and it was
568 * suggested to giveback all requests here, but give HW some
569 * extra time to synchronize with the interconnect. We're using
570 * an arbitraty 100us delay for that.
571 *
572 * Note also that a similar handling was tested by Synopsys
573 * (thanks a lot Paul) and nothing bad has come out of it.
574 * In short, what we're doing is:
575 *
576 * - Issue EndTransfer WITH CMDIOC bit set
577 * - Wait 100us
578 * - giveback all requests to gadget driver
579 */
580 udelay(100);
581
582 while (!list_empty(&dep->req_queued)) {
583 req = next_request(&dep->req_queued);
584
585 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
586 }
587 }
588
536 while (!list_empty(&dep->request_list)) { 589 while (!list_empty(&dep->request_list)) {
537 req = next_request(&dep->request_list); 590 req = next_request(&dep->request_list);
538 591
@@ -741,8 +794,7 @@ static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
741 case USB_ENDPOINT_XFER_ISOC: 794 case USB_ENDPOINT_XFER_ISOC:
742 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; 795 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
743 796
744 /* IOC every DWC3_TRB_NUM / 4 so we can refill */ 797 if (!req->request.no_interrupt)
745 if (!(cur_slot % (DWC3_TRB_NUM / 4)))
746 trb->ctrl |= DWC3_TRB_CTRL_IOC; 798 trb->ctrl |= DWC3_TRB_CTRL_IOC;
747 break; 799 break;
748 800
@@ -958,14 +1010,42 @@ static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
958 dep->flags |= DWC3_EP_BUSY; 1010 dep->flags |= DWC3_EP_BUSY;
959 1011
960 if (start_new) { 1012 if (start_new) {
961 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc, 1013 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
962 dep->number); 1014 dep->number);
963 WARN_ON_ONCE(!dep->res_trans_idx); 1015 WARN_ON_ONCE(!dep->resource_index);
964 } 1016 }
965 1017
966 return 0; 1018 return 0;
967} 1019}
968 1020
1021static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1022 struct dwc3_ep *dep, u32 cur_uf)
1023{
1024 u32 uf;
1025
1026 if (list_empty(&dep->request_list)) {
1027 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1028 dep->name);
1029 return;
1030 }
1031
1032 /* 4 micro frames in the future */
1033 uf = cur_uf + dep->interval * 4;
1034
1035 __dwc3_gadget_kick_transfer(dep, uf, 1);
1036}
1037
1038static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1039 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1040{
1041 u32 cur_uf, mask;
1042
1043 mask = ~(dep->interval - 1);
1044 cur_uf = event->parameters & mask;
1045
1046 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1047}
1048
969static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) 1049static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
970{ 1050{
971 struct dwc3 *dwc = dep->dwc; 1051 struct dwc3 *dwc = dep->dwc;
@@ -995,11 +1075,8 @@ static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
995 1075
996 list_add_tail(&req->list, &dep->request_list); 1076 list_add_tail(&req->list, &dep->request_list);
997 1077
998 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && (dep->flags & DWC3_EP_BUSY))
999 dep->flags |= DWC3_EP_PENDING_REQUEST;
1000
1001 /* 1078 /*
1002 * There are two special cases: 1079 * There are a few special cases:
1003 * 1080 *
1004 * 1. XferNotReady with empty list of requests. We need to kick the 1081 * 1. XferNotReady with empty list of requests. We need to kick the
1005 * transfer here in that situation, otherwise we will be NAKing 1082 * transfer here in that situation, otherwise we will be NAKing
@@ -1008,31 +1085,46 @@ static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1008 * able to receive the data until the next request is queued. 1085 * able to receive the data until the next request is queued.
1009 * The following code is handling exactly that. 1086 * The following code is handling exactly that.
1010 * 1087 *
1011 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1012 * kick the transfer here after queuing a request, otherwise the
1013 * core may not see the modified TRB(s).
1014 */ 1088 */
1015 if (dep->flags & DWC3_EP_PENDING_REQUEST) { 1089 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
1016 int ret; 1090 int ret;
1017 int start_trans = 1;
1018 u8 trans_idx = dep->res_trans_idx;
1019 1091
1020 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && 1092 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
1021 (dep->flags & DWC3_EP_BUSY)) { 1093 if (ret && ret != -EBUSY) {
1022 start_trans = 0; 1094 struct dwc3 *dwc = dep->dwc;
1023 WARN_ON_ONCE(!trans_idx); 1095
1024 } else { 1096 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1025 trans_idx = 0; 1097 dep->name);
1026 } 1098 }
1099 }
1027 1100
1028 ret = __dwc3_gadget_kick_transfer(dep, trans_idx, start_trans); 1101 /*
1102 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1103 * kick the transfer here after queuing a request, otherwise the
1104 * core may not see the modified TRB(s).
1105 */
1106 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1107 (dep->flags & DWC3_EP_BUSY)) {
1108 WARN_ON_ONCE(!dep->resource_index);
1109 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
1110 false);
1029 if (ret && ret != -EBUSY) { 1111 if (ret && ret != -EBUSY) {
1030 struct dwc3 *dwc = dep->dwc; 1112 struct dwc3 *dwc = dep->dwc;
1031 1113
1032 dev_dbg(dwc->dev, "%s: failed to kick transfers\n", 1114 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1033 dep->name); 1115 dep->name);
1034 } 1116 }
1035 }; 1117 }
1118
1119 /*
1120 * 3. Missed ISOC Handling. We need to start isoc transfer on the saved
1121 * uframe number.
1122 */
1123 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1124 (dep->flags & DWC3_EP_MISSED_ISOC)) {
1125 __dwc3_gadget_start_isoc(dwc, dep, dep->current_uf);
1126 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1127 }
1036 1128
1037 return 0; 1129 return 0;
1038} 1130}
@@ -1118,15 +1210,6 @@ int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1118 memset(&params, 0x00, sizeof(params)); 1210 memset(&params, 0x00, sizeof(params));
1119 1211
1120 if (value) { 1212 if (value) {
1121 if (dep->number == 0 || dep->number == 1) {
1122 /*
1123 * Whenever EP0 is stalled, we will restart
1124 * the state machine, thus moving back to
1125 * Setup Phase
1126 */
1127 dwc->ep0state = EP0_SETUP_PHASE;
1128 }
1129
1130 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, 1213 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1131 DWC3_DEPCMD_SETSTALL, &params); 1214 DWC3_DEPCMD_SETSTALL, &params);
1132 if (ret) 1215 if (ret)
@@ -1186,7 +1269,10 @@ static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1186 dep->flags |= DWC3_EP_WEDGE; 1269 dep->flags |= DWC3_EP_WEDGE;
1187 spin_unlock_irqrestore(&dwc->lock, flags); 1270 spin_unlock_irqrestore(&dwc->lock, flags);
1188 1271
1189 return dwc3_gadget_ep_set_halt(ep, 1); 1272 if (dep->number == 0 || dep->number == 1)
1273 return dwc3_gadget_ep0_set_halt(ep, 1);
1274 else
1275 return dwc3_gadget_ep_set_halt(ep, 1);
1190} 1276}
1191 1277
1192/* -------------------------------------------------------------------------- */ 1278/* -------------------------------------------------------------------------- */
@@ -1204,7 +1290,7 @@ static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1204 .free_request = dwc3_gadget_ep_free_request, 1290 .free_request = dwc3_gadget_ep_free_request,
1205 .queue = dwc3_gadget_ep0_queue, 1291 .queue = dwc3_gadget_ep0_queue,
1206 .dequeue = dwc3_gadget_ep_dequeue, 1292 .dequeue = dwc3_gadget_ep_dequeue,
1207 .set_halt = dwc3_gadget_ep_set_halt, 1293 .set_halt = dwc3_gadget_ep0_set_halt,
1208 .set_wedge = dwc3_gadget_ep_set_wedge, 1294 .set_wedge = dwc3_gadget_ep_set_wedge,
1209}; 1295};
1210 1296
@@ -1280,9 +1366,13 @@ static int dwc3_gadget_wakeup(struct usb_gadget *g)
1280 goto out; 1366 goto out;
1281 } 1367 }
1282 1368
1283 /* write zeroes to Link Change Request */ 1369 /* Recent versions do this automatically */
1284 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; 1370 if (dwc->revision < DWC3_REVISION_194A) {
1285 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 1371 /* write zeroes to Link Change Request */
1372 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1373 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1374 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1375 }
1286 1376
1287 /* poll until Link State changes to ON */ 1377 /* poll until Link State changes to ON */
1288 timeout = jiffies + msecs_to_jiffies(100); 1378 timeout = jiffies + msecs_to_jiffies(100);
@@ -1319,16 +1409,21 @@ static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1319 return 0; 1409 return 0;
1320} 1410}
1321 1411
1322static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on) 1412static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1323{ 1413{
1324 u32 reg; 1414 u32 reg;
1325 u32 timeout = 500; 1415 u32 timeout = 500;
1326 1416
1327 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 1417 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1328 if (is_on) { 1418 if (is_on) {
1329 reg &= ~DWC3_DCTL_TRGTULST_MASK; 1419 if (dwc->revision <= DWC3_REVISION_187A) {
1330 reg |= (DWC3_DCTL_RUN_STOP 1420 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1331 | DWC3_DCTL_TRGTULST_RX_DET); 1421 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1422 }
1423
1424 if (dwc->revision >= DWC3_REVISION_194A)
1425 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1426 reg |= DWC3_DCTL_RUN_STOP;
1332 } else { 1427 } else {
1333 reg &= ~DWC3_DCTL_RUN_STOP; 1428 reg &= ~DWC3_DCTL_RUN_STOP;
1334 } 1429 }
@@ -1346,7 +1441,7 @@ static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1346 } 1441 }
1347 timeout--; 1442 timeout--;
1348 if (!timeout) 1443 if (!timeout)
1349 break; 1444 return -ETIMEDOUT;
1350 udelay(1); 1445 udelay(1);
1351 } while (1); 1446 } while (1);
1352 1447
@@ -1354,20 +1449,23 @@ static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1354 dwc->gadget_driver 1449 dwc->gadget_driver
1355 ? dwc->gadget_driver->function : "no-function", 1450 ? dwc->gadget_driver->function : "no-function",
1356 is_on ? "connect" : "disconnect"); 1451 is_on ? "connect" : "disconnect");
1452
1453 return 0;
1357} 1454}
1358 1455
1359static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) 1456static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1360{ 1457{
1361 struct dwc3 *dwc = gadget_to_dwc(g); 1458 struct dwc3 *dwc = gadget_to_dwc(g);
1362 unsigned long flags; 1459 unsigned long flags;
1460 int ret;
1363 1461
1364 is_on = !!is_on; 1462 is_on = !!is_on;
1365 1463
1366 spin_lock_irqsave(&dwc->lock, flags); 1464 spin_lock_irqsave(&dwc->lock, flags);
1367 dwc3_gadget_run_stop(dwc, is_on); 1465 ret = dwc3_gadget_run_stop(dwc, is_on);
1368 spin_unlock_irqrestore(&dwc->lock, flags); 1466 spin_unlock_irqrestore(&dwc->lock, flags);
1369 1467
1370 return 0; 1468 return ret;
1371} 1469}
1372 1470
1373static int dwc3_gadget_start(struct usb_gadget *g, 1471static int dwc3_gadget_start(struct usb_gadget *g,
@@ -1468,6 +1566,7 @@ static int dwc3_gadget_stop(struct usb_gadget *g,
1468 1566
1469 return 0; 1567 return 0;
1470} 1568}
1569
1471static const struct usb_gadget_ops dwc3_gadget_ops = { 1570static const struct usb_gadget_ops dwc3_gadget_ops = {
1472 .get_frame = dwc3_gadget_get_frame, 1571 .get_frame = dwc3_gadget_get_frame,
1473 .wakeup = dwc3_gadget_wakeup, 1572 .wakeup = dwc3_gadget_wakeup,
@@ -1558,6 +1657,7 @@ static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1558 struct dwc3_trb *trb; 1657 struct dwc3_trb *trb;
1559 unsigned int count; 1658 unsigned int count;
1560 unsigned int s_pkt = 0; 1659 unsigned int s_pkt = 0;
1660 unsigned int trb_status;
1561 1661
1562 do { 1662 do {
1563 req = next_request(&dep->req_queued); 1663 req = next_request(&dep->req_queued);
@@ -1583,9 +1683,18 @@ static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1583 1683
1584 if (dep->direction) { 1684 if (dep->direction) {
1585 if (count) { 1685 if (count) {
1586 dev_err(dwc->dev, "incomplete IN transfer %s\n", 1686 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1587 dep->name); 1687 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1588 status = -ECONNRESET; 1688 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1689 dep->name);
1690 dep->current_uf = event->parameters &
1691 ~(dep->interval - 1);
1692 dep->flags |= DWC3_EP_MISSED_ISOC;
1693 } else {
1694 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1695 dep->name);
1696 status = -ECONNRESET;
1697 }
1589 } 1698 }
1590 } else { 1699 } else {
1591 if (count && (event->status & DEPEVT_STATUS_SHORT)) 1700 if (count && (event->status & DEPEVT_STATUS_SHORT))
@@ -1604,7 +1713,8 @@ static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1604 if (s_pkt) 1713 if (s_pkt)
1605 break; 1714 break;
1606 if ((event->status & DEPEVT_STATUS_LST) && 1715 if ((event->status & DEPEVT_STATUS_LST) &&
1607 (trb->ctrl & DWC3_TRB_CTRL_LST)) 1716 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1717 DWC3_TRB_CTRL_HWO)))
1608 break; 1718 break;
1609 if ((event->status & DEPEVT_STATUS_IOC) && 1719 if ((event->status & DEPEVT_STATUS_IOC) &&
1610 (trb->ctrl & DWC3_TRB_CTRL_IOC)) 1720 (trb->ctrl & DWC3_TRB_CTRL_IOC))
@@ -1657,65 +1767,6 @@ static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1657 } 1767 }
1658} 1768}
1659 1769
1660static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1661 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1662{
1663 u32 uf, mask;
1664
1665 if (list_empty(&dep->request_list)) {
1666 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1667 dep->name);
1668 return;
1669 }
1670
1671 mask = ~(dep->interval - 1);
1672 uf = event->parameters & mask;
1673 /* 4 micro frames in the future */
1674 uf += dep->interval * 4;
1675
1676 __dwc3_gadget_kick_transfer(dep, uf, 1);
1677}
1678
1679static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1680 const struct dwc3_event_depevt *event)
1681{
1682 struct dwc3 *dwc = dep->dwc;
1683 struct dwc3_event_depevt mod_ev = *event;
1684
1685 /*
1686 * We were asked to remove one request. It is possible that this
1687 * request and a few others were started together and have the same
1688 * transfer index. Since we stopped the complete endpoint we don't
1689 * know how many requests were already completed (and not yet)
1690 * reported and how could be done (later). We purge them all until
1691 * the end of the list.
1692 */
1693 mod_ev.status = DEPEVT_STATUS_LST;
1694 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1695 dep->flags &= ~DWC3_EP_BUSY;
1696 /* pending requests are ignored and are queued on XferNotReady */
1697}
1698
1699static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1700 const struct dwc3_event_depevt *event)
1701{
1702 u32 param = event->parameters;
1703 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1704
1705 switch (cmd_type) {
1706 case DWC3_DEPCMD_ENDTRANSFER:
1707 dwc3_process_ep_cmd_complete(dep, event);
1708 break;
1709 case DWC3_DEPCMD_STARTTRANSFER:
1710 dep->res_trans_idx = param & 0x7f;
1711 break;
1712 default:
1713 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1714 __func__, cmd_type);
1715 break;
1716 };
1717}
1718
1719static void dwc3_endpoint_interrupt(struct dwc3 *dwc, 1770static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1720 const struct dwc3_event_depevt *event) 1771 const struct dwc3_event_depevt *event)
1721{ 1772{
@@ -1724,6 +1775,9 @@ static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1724 1775
1725 dep = dwc->eps[epnum]; 1776 dep = dwc->eps[epnum];
1726 1777
1778 if (!(dep->flags & DWC3_EP_ENABLED))
1779 return;
1780
1727 dev_vdbg(dwc->dev, "%s: %s\n", dep->name, 1781 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1728 dwc3_ep_event_string(event->endpoint_event)); 1782 dwc3_ep_event_string(event->endpoint_event));
1729 1783
@@ -1734,7 +1788,7 @@ static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1734 1788
1735 switch (event->endpoint_event) { 1789 switch (event->endpoint_event) {
1736 case DWC3_DEPEVT_XFERCOMPLETE: 1790 case DWC3_DEPEVT_XFERCOMPLETE:
1737 dep->res_trans_idx = 0; 1791 dep->resource_index = 0;
1738 1792
1739 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { 1793 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1740 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n", 1794 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
@@ -1797,7 +1851,7 @@ static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1797 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name); 1851 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1798 break; 1852 break;
1799 case DWC3_DEPEVT_EPCMDCMPLT: 1853 case DWC3_DEPEVT_EPCMDCMPLT:
1800 dwc3_ep_cmd_compl(dep, event); 1854 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
1801 break; 1855 break;
1802 } 1856 }
1803} 1857}
@@ -1820,16 +1874,16 @@ static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1820 1874
1821 dep = dwc->eps[epnum]; 1875 dep = dwc->eps[epnum];
1822 1876
1823 WARN_ON(!dep->res_trans_idx); 1877 if (!dep->resource_index)
1824 if (dep->res_trans_idx) { 1878 return;
1825 cmd = DWC3_DEPCMD_ENDTRANSFER; 1879
1826 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC; 1880 cmd = DWC3_DEPCMD_ENDTRANSFER;
1827 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx); 1881 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1828 memset(&params, 0, sizeof(params)); 1882 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1829 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params); 1883 memset(&params, 0, sizeof(params));
1830 WARN_ON_ONCE(ret); 1884 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1831 dep->res_trans_idx = 0; 1885 WARN_ON_ONCE(ret);
1832 } 1886 dep->resource_index = 0;
1833} 1887}
1834 1888
1835static void dwc3_stop_active_transfers(struct dwc3 *dwc) 1889static void dwc3_stop_active_transfers(struct dwc3 *dwc)
@@ -1872,11 +1926,9 @@ static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1872 1926
1873static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) 1927static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1874{ 1928{
1929 int reg;
1930
1875 dev_vdbg(dwc->dev, "%s\n", __func__); 1931 dev_vdbg(dwc->dev, "%s\n", __func__);
1876#if 0
1877 XXX
1878 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1879 enable it before we can disable it.
1880 1932
1881 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 1933 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1882 reg &= ~DWC3_DCTL_INITU1ENA; 1934 reg &= ~DWC3_DCTL_INITU1ENA;
@@ -1884,9 +1936,7 @@ static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1884 1936
1885 reg &= ~DWC3_DCTL_INITU2ENA; 1937 reg &= ~DWC3_DCTL_INITU2ENA;
1886 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 1938 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1887#endif
1888 1939
1889 dwc3_stop_active_transfers(dwc);
1890 dwc3_disconnect_gadget(dwc); 1940 dwc3_disconnect_gadget(dwc);
1891 dwc->start_config_issued = false; 1941 dwc->start_config_issued = false;
1892 1942
@@ -1894,30 +1944,30 @@ static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1894 dwc->setup_packet_pending = false; 1944 dwc->setup_packet_pending = false;
1895} 1945}
1896 1946
1897static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on) 1947static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
1898{ 1948{
1899 u32 reg; 1949 u32 reg;
1900 1950
1901 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0)); 1951 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1902 1952
1903 if (on) 1953 if (suspend)
1904 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1905 else
1906 reg |= DWC3_GUSB3PIPECTL_SUSPHY; 1954 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1955 else
1956 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1907 1957
1908 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg); 1958 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1909} 1959}
1910 1960
1911static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on) 1961static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
1912{ 1962{
1913 u32 reg; 1963 u32 reg;
1914 1964
1915 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); 1965 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1916 1966
1917 if (on) 1967 if (suspend)
1918 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1919 else
1920 reg |= DWC3_GUSB2PHYCFG_SUSPHY; 1968 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1969 else
1970 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1921 1971
1922 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); 1972 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1923} 1973}
@@ -1962,16 +2012,18 @@ static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1962 /* after reset -> Default State */ 2012 /* after reset -> Default State */
1963 dwc->dev_state = DWC3_DEFAULT_STATE; 2013 dwc->dev_state = DWC3_DEFAULT_STATE;
1964 2014
1965 /* Enable PHYs */ 2015 /* Recent versions support automatic phy suspend and don't need this */
1966 dwc3_gadget_usb2_phy_power(dwc, true); 2016 if (dwc->revision < DWC3_REVISION_194A) {
1967 dwc3_gadget_usb3_phy_power(dwc, true); 2017 /* Resume PHYs */
2018 dwc3_gadget_usb2_phy_suspend(dwc, false);
2019 dwc3_gadget_usb3_phy_suspend(dwc, false);
2020 }
1968 2021
1969 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) 2022 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1970 dwc3_disconnect_gadget(dwc); 2023 dwc3_disconnect_gadget(dwc);
1971 2024
1972 reg = dwc3_readl(dwc->regs, DWC3_DCTL); 2025 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1973 reg &= ~DWC3_DCTL_TSTCTRL_MASK; 2026 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1974 reg &= ~(DWC3_DCTL_INITU1ENA | DWC3_DCTL_INITU2ENA);
1975 dwc3_writel(dwc->regs, DWC3_DCTL, reg); 2027 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1976 dwc->test_mode = false; 2028 dwc->test_mode = false;
1977 2029
@@ -2010,16 +2062,16 @@ static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2010 dwc3_writel(dwc->regs, DWC3_GCTL, reg); 2062 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2011} 2063}
2012 2064
2013static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed) 2065static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
2014{ 2066{
2015 switch (speed) { 2067 switch (speed) {
2016 case USB_SPEED_SUPER: 2068 case USB_SPEED_SUPER:
2017 dwc3_gadget_usb2_phy_power(dwc, false); 2069 dwc3_gadget_usb2_phy_suspend(dwc, true);
2018 break; 2070 break;
2019 case USB_SPEED_HIGH: 2071 case USB_SPEED_HIGH:
2020 case USB_SPEED_FULL: 2072 case USB_SPEED_FULL:
2021 case USB_SPEED_LOW: 2073 case USB_SPEED_LOW:
2022 dwc3_gadget_usb3_phy_power(dwc, false); 2074 dwc3_gadget_usb3_phy_suspend(dwc, true);
2023 break; 2075 break;
2024 } 2076 }
2025} 2077}
@@ -2082,8 +2134,11 @@ static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2082 break; 2134 break;
2083 } 2135 }
2084 2136
2085 /* Disable unneded PHY */ 2137 /* Recent versions support automatic phy suspend and don't need this */
2086 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed); 2138 if (dwc->revision < DWC3_REVISION_194A) {
2139 /* Suspend unneeded PHY */
2140 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2141 }
2087 2142
2088 dep = dwc->eps[0]; 2143 dep = dwc->eps[0];
2089 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL); 2144 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
@@ -2373,10 +2428,6 @@ int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2373 reg |= DWC3_DCFG_LPM_CAP; 2428 reg |= DWC3_DCFG_LPM_CAP;
2374 dwc3_writel(dwc->regs, DWC3_DCFG, reg); 2429 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2375 2430
2376 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2377 reg |= DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA;
2378 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2379
2380 /* Enable all but Start and End of Frame IRQs */ 2431 /* Enable all but Start and End of Frame IRQs */
2381 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN | 2432 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2382 DWC3_DEVTEN_EVNTOVERFLOWEN | 2433 DWC3_DEVTEN_EVNTOVERFLOWEN |
@@ -2389,6 +2440,24 @@ int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2389 DWC3_DEVTEN_DISCONNEVTEN); 2440 DWC3_DEVTEN_DISCONNEVTEN);
2390 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); 2441 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2391 2442
2443 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
2444 if (dwc->revision >= DWC3_REVISION_194A) {
2445 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2446 reg |= DWC3_DCFG_LPM_CAP;
2447 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2448
2449 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2450 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2451
2452 /* TODO: This should be configurable */
2453 reg |= DWC3_DCTL_HIRD_THRES(28);
2454
2455 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2456
2457 dwc3_gadget_usb2_phy_suspend(dwc, false);
2458 dwc3_gadget_usb3_phy_suspend(dwc, false);
2459 }
2460
2392 ret = device_register(&dwc->gadget.dev); 2461 ret = device_register(&dwc->gadget.dev);
2393 if (ret) { 2462 if (ret) {
2394 dev_err(dwc->dev, "failed to register gadget device\n"); 2463 dev_err(dwc->dev, "failed to register gadget device\n");
diff --git a/drivers/usb/dwc3/gadget.h b/drivers/usb/dwc3/gadget.h
index 95ef6a2f7764..99e6d7248820 100644
--- a/drivers/usb/dwc3/gadget.h
+++ b/drivers/usb/dwc3/gadget.h
@@ -66,7 +66,12 @@ struct dwc3;
66#define DWC3_DEPCFG_FIFO_NUMBER(n) ((n) << 17) 66#define DWC3_DEPCFG_FIFO_NUMBER(n) ((n) << 17)
67#define DWC3_DEPCFG_BURST_SIZE(n) ((n) << 22) 67#define DWC3_DEPCFG_BURST_SIZE(n) ((n) << 22)
68#define DWC3_DEPCFG_DATA_SEQ_NUM(n) ((n) << 26) 68#define DWC3_DEPCFG_DATA_SEQ_NUM(n) ((n) << 26)
69/* This applies for core versions earlier than 1.94a */
69#define DWC3_DEPCFG_IGN_SEQ_NUM (1 << 31) 70#define DWC3_DEPCFG_IGN_SEQ_NUM (1 << 31)
71/* These apply for core versions 1.94a and later */
72#define DWC3_DEPCFG_ACTION_INIT (0 << 30)
73#define DWC3_DEPCFG_ACTION_RESTORE (1 << 30)
74#define DWC3_DEPCFG_ACTION_MODIFY (2 << 30)
70 75
71/* DEPXFERCFG parameter 0 */ 76/* DEPXFERCFG parameter 0 */
72#define DWC3_DEPXFERCFG_NUM_XFER_RES(n) ((n) & 0xffff) 77#define DWC3_DEPXFERCFG_NUM_XFER_RES(n) ((n) & 0xffff)
@@ -106,6 +111,7 @@ int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state);
106void dwc3_ep0_interrupt(struct dwc3 *dwc, 111void dwc3_ep0_interrupt(struct dwc3 *dwc,
107 const struct dwc3_event_depevt *event); 112 const struct dwc3_event_depevt *event);
108void dwc3_ep0_out_start(struct dwc3 *dwc); 113void dwc3_ep0_out_start(struct dwc3 *dwc);
114int dwc3_gadget_ep0_set_halt(struct usb_ep *ep, int value);
109int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request, 115int dwc3_gadget_ep0_queue(struct usb_ep *ep, struct usb_request *request,
110 gfp_t gfp_flags); 116 gfp_t gfp_flags);
111int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value); 117int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value);