1 /******************************************************************************
3 * Copyright (C) 2015 Xilinx, Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
31 ******************************************************************************/
32 /****************************************************************************/
39 * MODIFICATION HISTORY:
41 * Ver Who Date Changes
42 * ----- ----- -------- -----------------------------------------------------
43 * 1.00a bss 01/22/15 First release
44 * 1.00a bss 03/18/15 Added XUsbPsu_Wait_Clear_Timeout and
45 * XUsbPsu_Wait_Set_Timeout functions
49 *****************************************************************************/
51 /***************************** Include Files ********************************/
55 /************************** Constant Definitions *****************************/
58 /**************************** Type Definitions *******************************/
61 /***************** Macros (Inline Functions) Definitions *********************/
64 /************************** Function Prototypes ******************************/
67 /************************** Variable Definitions *****************************/
69 /*****************************************************************************/
71 * Waits until a bit in a register is cleared or timeout occurs
73 * @param InstancePtr is a pointer to the XUsbPsu instance to be worked on.
74 * @param Offset is register offset.
75 * @param BitMask is bit mask of required bit to be checked.
76 * @param Timeout is the time to wait specified in micro seconds.
79 * - XST_SUCCESS when bit is cleared.
80 * - XST_FAILURE when timed out.
82 ******************************************************************************/
83 int XUsbPsu_Wait_Clear_Timeout(struct XUsbPsu *InstancePtr, u32 Offset,
84 u32 BitMask, u32 Timeout)
89 RegVal = XUsbPsu_ReadReg(InstancePtr, Offset);
90 if (!(RegVal & BitMask))
101 /*****************************************************************************/
103 * Waits until a bit in a register is set or timeout occurs
105 * @param InstancePtr is a pointer to the XUsbPsu instance to be worked on.
106 * @param Offset is register offset.
107 * @param BitMask is bit mask of required bit to be checked.
108 * @param Timeout is the time to wait specified in micro seconds.
111 * - XST_SUCCESS when bit is set.
112 * - XST_FAILURE when timed out.
114 ******************************************************************************/
115 int XUsbPsu_Wait_Set_Timeout(struct XUsbPsu *InstancePtr, u32 Offset,
116 u32 BitMask, u32 Timeout)
121 RegVal = XUsbPsu_ReadReg(InstancePtr, Offset);
122 if (RegVal & BitMask)
133 /*****************************************************************************/
135 * Sets mode of Core to USB Device/Host/OTG.
138 * @param InstancePtr is a pointer to the XUsbPsu instance to be worked on.
139 * @param Mode is mode to set
140 * - XUSBPSU_GCTL_PRTCAP_OTG
141 * - XUSBPSU_GCTL_PRTCAP_HOST
142 * - XUSBPSU_GCTL_PRTCAP_DEVICE
146 ******************************************************************************/
147 void XUsbPsu_SetMode(struct XUsbPsu *InstancePtr, u32 Mode)
151 Xil_AssertVoid(InstancePtr != NULL);
152 Xil_AssertVoid(Mode <= XUSBPSU_GCTL_PRTCAP_OTG &&
153 Mode >= XUSBPSU_GCTL_PRTCAP_HOST);
155 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_GCTL);
156 RegVal &= ~(XUSBPSU_GCTL_PRTCAPDIR(XUSBPSU_GCTL_PRTCAP_OTG));
157 RegVal |= XUSBPSU_GCTL_PRTCAPDIR(Mode);
158 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GCTL, RegVal);
161 /*****************************************************************************/
163 * Issues core PHY reset.
165 * @param InstancePtr is a pointer to the XUsbPsu instance to be worked on.
169 ******************************************************************************/
170 void XUsbPsu_PhyReset(struct XUsbPsu *InstancePtr)
174 Xil_AssertVoid(InstancePtr != NULL);
176 /* Before Resetting PHY, put Core in Reset */
177 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_GCTL);
178 RegVal |= XUSBPSU_GCTL_CORESOFTRESET;
179 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GCTL, RegVal);
181 /* Assert USB3 PHY reset */
182 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_GUSB3PIPECTL(0));
183 RegVal |= XUSBPSU_GUSB3PIPECTL_PHYSOFTRST;
184 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GUSB3PIPECTL(0), RegVal);
186 /* Assert USB2 PHY reset */
187 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_GUSB2PHYCFG(0));
188 RegVal |= XUSBPSU_GUSB2PHYCFG_PHYSOFTRST;
189 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GUSB2PHYCFG(0), RegVal);
191 usleep(XUSBPSU_PHY_TIMEOUT);
193 /* Clear USB3 PHY reset */
194 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_GUSB3PIPECTL(0));
195 RegVal &= ~XUSBPSU_GUSB3PIPECTL_PHYSOFTRST;
196 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GUSB3PIPECTL(0), RegVal);
198 /* Clear USB2 PHY reset */
199 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_GUSB2PHYCFG(0));
200 RegVal &= ~XUSBPSU_GUSB2PHYCFG_PHYSOFTRST;
201 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GUSB2PHYCFG(0), RegVal);
203 usleep(XUSBPSU_PHY_TIMEOUT);
205 /* After PHYs are stable we can take Core out of reset State */
206 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_GCTL);
207 RegVal &= ~XUSBPSU_GCTL_CORESOFTRESET;
208 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GCTL, RegVal);
211 /*****************************************************************************/
213 * Sets up Event buffers so that events are written by Core.
215 * @param InstancePtr is a pointer to the XUsbPsu instance to be worked on.
219 ******************************************************************************/
220 void XUsbPsu_EventBuffersSetup(struct XUsbPsu *InstancePtr)
222 struct XUsbPsu_EvtBuffer *Evt;
224 Xil_AssertVoid(InstancePtr != NULL);
226 Evt = &InstancePtr->Evt;
227 Evt->BuffAddr = (void *)InstancePtr->EventBuffer;
229 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GEVNTADRLO(0),
230 (UINTPTR)InstancePtr->EventBuffer);
231 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GEVNTADRHI(0),
232 ((UINTPTR)(InstancePtr->EventBuffer) >> 16) >> 16);
233 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GEVNTSIZ(0),
234 XUSBPSU_GEVNTSIZ_SIZE(sizeof(InstancePtr->EventBuffer)));
235 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GEVNTCOUNT(0), 0);
238 /*****************************************************************************/
240 * Resets Event buffer Registers to zero so that events are not written by Core.
242 * @param InstancePtr is a pointer to the XUsbPsu instance to be worked on.
246 ******************************************************************************/
247 void XUsbPsu_EventBuffersReset(struct XUsbPsu *InstancePtr)
250 Xil_AssertVoid(InstancePtr != NULL);
252 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GEVNTADRLO(0), 0);
253 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GEVNTADRHI(0), 0);
254 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GEVNTSIZ(0),
255 XUSBPSU_GEVNTSIZ_INTMASK | XUSBPSU_GEVNTSIZ_SIZE(0));
256 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GEVNTCOUNT(0), 0);
259 /*****************************************************************************/
261 * Reads data from Hardware Params Registers of Core.
263 * @param InstancePtr is a pointer to the XUsbPsu instance to be worked on.
264 * @param RegIndex is Register number to read
265 * - XUSBPSU_GHWPARAMS0
266 * - XUSBPSU_GHWPARAMS1
267 * - XUSBPSU_GHWPARAMS2
268 * - XUSBPSU_GHWPARAMS3
269 * - XUSBPSU_GHWPARAMS4
270 * - XUSBPSU_GHWPARAMS5
271 * - XUSBPSU_GHWPARAMS6
272 * - XUSBPSU_GHWPARAMS7
274 * @return One of the GHWPARAMS RegValister contents.
276 ******************************************************************************/
277 u32 XUsbPsu_ReadHwParams(struct XUsbPsu *InstancePtr, u8 RegIndex)
281 Xil_AssertNonvoid(InstancePtr != NULL);
282 Xil_AssertNonvoid(RegIndex >= XUSBPSU_GHWPARAMS0 &&
283 RegIndex <= XUSBPSU_GHWPARAMS7);
285 RegVal = XUsbPsu_ReadReg(InstancePtr, (XUSBPSU_GHWPARAMS0_OFFSET +
290 /*****************************************************************************/
294 * @param InstancePtr is a pointer to the XUsbPsu instance to be worked on.
297 * - XST_SUCCESS if initialization was successful
298 * - XST_FAILURE if initialization was not successful
300 ******************************************************************************/
301 int XUsbPsu_CoreInit(struct XUsbPsu *InstancePtr)
306 Xil_AssertNonvoid(InstancePtr != NULL);
308 /* issue device SoftReset too */
309 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_DCTL, XUSBPSU_DCTL_CSFTRST);
311 if (XUsbPsu_Wait_Clear_Timeout(InstancePtr, XUSBPSU_DCTL,
312 XUSBPSU_DCTL_CSFTRST, 500) == XST_FAILURE) {
313 /* timed out return failure */
317 XUsbPsu_PhyReset(InstancePtr);
319 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_GCTL);
320 RegVal &= ~XUSBPSU_GCTL_SCALEDOWN_MASK;
321 RegVal &= ~XUSBPSU_GCTL_DISSCRAMBLE;
323 Hwparams1 = XUsbPsu_ReadHwParams(InstancePtr, 1);
325 switch (XUSBPSU_GHWPARAMS1_EN_PWROPT(Hwparams1)) {
326 case XUSBPSU_GHWPARAMS1_EN_PWROPT_CLK:
327 RegVal &= ~XUSBPSU_GCTL_DSBLCLKGTNG;
329 case XUSBPSU_GHWPARAMS1_EN_PWROPT_HIB:
330 /* enable hibernation here */
336 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_GCTL, RegVal);
341 /*****************************************************************************/
343 * Enables an interrupt in Event Enable RegValister.
345 * @param InstancePtr is a pointer to the XUsbPsu instance to be worked on
346 * @param Mask is the OR of any Interrupt Enable Masks:
347 * - XUSBPSU_DEVTEN_VNDRDEVTSTRCVEDEN
348 * - XUSBPSU_DEVTEN_EVNTOVERFLOWEN
349 * - XUSBPSU_DEVTEN_CMDCMPLTEN
350 * - XUSBPSU_DEVTEN_ERRTICERREN
351 * - XUSBPSU_DEVTEN_SOFEN
352 * - XUSBPSU_DEVTEN_EOPFEN
353 * - XUSBPSU_DEVTEN_HIBERNATIONREQEVTEN
354 * - XUSBPSU_DEVTEN_WKUPEVTEN
355 * - XUSBPSU_DEVTEN_ULSTCNGEN
356 * - XUSBPSU_DEVTEN_CONNECTDONEEN
357 * - XUSBPSU_DEVTEN_USBRSTEN
358 * - XUSBPSU_DEVTEN_DISCONNEVTEN
362 ******************************************************************************/
363 void XUsbPsu_EnableIntr(struct XUsbPsu *InstancePtr, u32 Mask)
367 Xil_AssertVoid(InstancePtr != NULL);
369 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_DEVTEN);
372 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_DEVTEN, RegVal);
375 /*****************************************************************************/
377 * Disables an interrupt in Event Enable RegValister.
379 * @param InstancePtr is a pointer to the XUsbPsu instance to be worked on.
380 * @param Mask is the OR of Interrupt Enable Masks
381 * - XUSBPSU_DEVTEN_VNDRDEVTSTRCVEDEN
382 * - XUSBPSU_DEVTEN_EVNTOVERFLOWEN
383 * - XUSBPSU_DEVTEN_CMDCMPLTEN
384 * - XUSBPSU_DEVTEN_ERRTICERREN
385 * - XUSBPSU_DEVTEN_SOFEN
386 * - XUSBPSU_DEVTEN_EOPFEN
387 * - XUSBPSU_DEVTEN_HIBERNATIONREQEVTEN
388 * - XUSBPSU_DEVTEN_WKUPEVTEN
389 * - XUSBPSU_DEVTEN_ULSTCNGEN
390 * - XUSBPSU_DEVTEN_CONNECTDONEEN
391 * - XUSBPSU_DEVTEN_USBRSTEN
392 * - XUSBPSU_DEVTEN_DISCONNEVTEN
396 ******************************************************************************/
397 void XUsbPsu_DisableIntr(struct XUsbPsu *InstancePtr, u32 Mask)
401 Xil_AssertVoid(InstancePtr != NULL);
403 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_DEVTEN);
405 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_DEVTEN, RegVal);
408 /****************************************************************************/
411 * This function does the following:
412 * - initializes a specific XUsbPsu instance.
413 * - sets up Event Buffer for Core to write events.
414 * - Core Reset and PHY Reset.
415 * - Sets core in Device Mode.
416 * - Sets default speed as HIGH_SPEED.
417 * - Sets Device Address to 0.
418 * - Enables interrupts.
420 * @param InstancePtr is a pointer to the XUsbPsu instance.
421 * @param ConfigPtr points to the XUsbPsu device configuration structure.
422 * @param BaseAddress is the device base address in the virtual memory
423 * address space. If the address translation is not used then the
424 * physical address is passed.
425 * Unexpected errors may occur if the address mapping is changed
426 * after this function is invoked.
428 * @return XST_SUCCESS else XST_FAILURE
432 *****************************************************************************/
433 int XUsbPsu_CfgInitialize(struct XUsbPsu *InstancePtr,
434 XUsbPsu_Config *ConfigPtr, u32 BaseAddress)
440 Xil_AssertNonvoid(InstancePtr != NULL);
441 Xil_AssertNonvoid(ConfigPtr != NULL);
443 InstancePtr->ConfigPtr = ConfigPtr;
445 Ret = XUsbPsu_CoreInit(InstancePtr);
450 RegVal = XUsbPsu_ReadHwParams(InstancePtr, 3);
451 InstancePtr->NumInEps = XUSBPSU_NUM_IN_EPS(RegVal);
452 InstancePtr->NumOutEps = XUSBPSU_NUM_EPS(RegVal) - InstancePtr->NumInEps;
454 /* Map USB and Physical Endpoints */
455 XUsbPsu_InitializeEps(InstancePtr);
457 XUsbPsu_EventBuffersSetup(InstancePtr);
459 XUsbPsu_SetMode(InstancePtr, XUSBPSU_GCTL_PRTCAP_DEVICE);
461 XUsbPsu_SetSpeed(InstancePtr, XUSBPSU_DCFG_HIGHSPEED);
463 XUsbPsu_SetDeviceAddress(InstancePtr, 0);
468 /****************************************************************************/
471 * Starts the controller so that Host can detect this device.
473 * @param InstancePtr is a pointer to the XUsbPsu instance.
475 * @return XST_SUCCESS else XST_FAILURE
479 *****************************************************************************/
480 int XUsbPsu_Start(struct XUsbPsu *InstancePtr)
484 Xil_AssertNonvoid(InstancePtr != NULL);
486 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_DCTL);
488 RegVal |= XUSBPSU_DCTL_RUN_STOP;
490 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_DCTL, RegVal);
492 if (XUsbPsu_Wait_Clear_Timeout(InstancePtr, XUSBPSU_DSTS,
493 XUSBPSU_DSTS_DEVCTRLHLT, 500) == XST_FAILURE) {
500 /****************************************************************************/
503 * Stops the controller so that Device disconnects from Host.
505 * @param InstancePtr is a pointer to the XUsbPsu instance.
507 * @return XST_SUCCESS else XST_FAILURE
511 *****************************************************************************/
512 int XUsbPsu_Stop(struct XUsbPsu *InstancePtr)
516 Xil_AssertNonvoid(InstancePtr != NULL);
518 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_DCTL);
519 RegVal &= ~XUSBPSU_DCTL_RUN_STOP;
521 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_DCTL, RegVal);
523 if (XUsbPsu_Wait_Set_Timeout(InstancePtr, XUSBPSU_DSTS,
524 XUSBPSU_DSTS_DEVCTRLHLT, 500) == XST_FAILURE) {
531 /****************************************************************************/
533 * Enables USB2 Test Modes
535 * @param InstancePtr is a pointer to the XUsbPsu instance.
536 * @param Mode is Test mode to set.
538 * @return XST_SUCCESS else XST_FAILURE
542 ****************************************************************************/
543 int XUsbPsu_SetTestMode(struct XUsbPsu *InstancePtr, int Mode)
547 Xil_AssertNonvoid(InstancePtr != NULL);
548 Xil_AssertNonvoid(Mode >= TEST_J && Mode <= TEST_FORCE_ENABLE);
550 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_DCTL);
551 RegVal &= ~XUSBPSU_DCTL_TSTCTRL_MASK;
558 case TEST_FORCE_ENABLE:
565 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_DCTL, RegVal);
570 /****************************************************************************/
572 * Gets current State of USB Link
574 * @param InstancePtr is a pointer to the XUsbPsu instance.
580 ****************************************************************************/
581 u32 XUsbPsu_GetLinkState(struct XUsbPsu *InstancePtr)
585 Xil_AssertNonvoid(InstancePtr != NULL);
587 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_DSTS);
589 return XUSBPSU_DSTS_USBLNKST(RegVal);
592 /****************************************************************************/
594 * Sets USB Link to a particular State
596 * @param InstancePtr is a pointer to the XUsbPsu instance.
597 * @param State is State of Link to set.
599 * @return XST_SUCCESS else XST_FAILURE
603 ****************************************************************************/
604 int XUsbPsu_SetLinkState(struct XUsbPsu *InstancePtr, u8 State)
608 Xil_AssertNonvoid(InstancePtr != NULL);
610 /* Wait until device controller is ready. */
611 if (XUsbPsu_Wait_Clear_Timeout(InstancePtr, XUSBPSU_DSTS,
612 XUSBPSU_DSTS_DCNRD, 500) == XST_FAILURE) {
616 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_DCTL);
617 RegVal &= ~XUSBPSU_DCTL_ULSTCHNGREQ_MASK;
619 RegVal |= XUSBPSU_DCTL_ULSTCHNGREQ(State);
620 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_DCTL, RegVal);
625 /****************************************************************************/
627 * Sets speed of the Core for connecting to Host
629 * @param InstancePtr is a pointer to the XUsbPsu instance.
630 * @param Speed is required speed
631 * - XUSBPSU_DCFG_HIGHSPEED
632 * - XUSBPSU_DCFG_FULLSPEED2
633 * - XUSBPSU_DCFG_LOWSPEED
634 * - XUSBPSU_DCFG_FULLSPEED1
640 *****************************************************************************/
641 void XUsbPsu_SetSpeed(struct XUsbPsu *InstancePtr, u32 Speed)
645 Xil_AssertVoid(InstancePtr != NULL);
646 Xil_AssertVoid(Speed >= XUSBPSU_DCFG_HIGHSPEED &&
647 Speed <= XUSBPSU_DCFG_SUPERSPEED);
649 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_DCFG);
650 RegVal &= ~(XUSBPSU_DCFG_SPEED_MASK);
652 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_DCFG, RegVal);
655 /****************************************************************************/
657 * Sets Device Address of the Core
659 * @param InstancePtr is a pointer to the XUsbPsu instance.
660 * @param Addr is address to set.
662 * @return XST_SUCCESS else XST_FAILURE
666 *****************************************************************************/
667 int XUsbPsu_SetDeviceAddress(struct XUsbPsu *InstancePtr, u16 Addr)
671 Xil_AssertNonvoid(InstancePtr != NULL);
672 Xil_AssertNonvoid(Addr <= 127);
674 if (InstancePtr->State == XUSBPSU_STATE_CONFIGURED) {
678 RegVal = XUsbPsu_ReadReg(InstancePtr, XUSBPSU_DCFG);
679 RegVal &= ~(XUSBPSU_DCFG_DEVADDR_MASK);
680 RegVal |= XUSBPSU_DCFG_DEVADDR(Addr);
681 XUsbPsu_WriteReg(InstancePtr, XUSBPSU_DCFG, RegVal);
684 InstancePtr->State = XUSBPSU_STATE_ADDRESS;
686 InstancePtr->State = XUSBPSU_STATE_DEFAULT;