2 PEIM to produce gPeiUsb2HostControllerPpiGuid based on gPeiUsbControllerPpiGuid
3 which is used to enable recovery function from USB Drivers.
5 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
14 // Two arrays used to translate the EHCI port state (change)
15 // to the UEFI protocol's port state (change).
17 USB_PORT_STATE_MAP mUsbPortStateMap
[] = {
18 {PORTSC_CONN
, USB_PORT_STAT_CONNECTION
},
19 {PORTSC_ENABLED
, USB_PORT_STAT_ENABLE
},
20 {PORTSC_SUSPEND
, USB_PORT_STAT_SUSPEND
},
21 {PORTSC_OVERCUR
, USB_PORT_STAT_OVERCURRENT
},
22 {PORTSC_RESET
, USB_PORT_STAT_RESET
},
23 {PORTSC_POWER
, USB_PORT_STAT_POWER
},
24 {PORTSC_OWNER
, USB_PORT_STAT_OWNER
}
27 USB_PORT_STATE_MAP mUsbPortChangeMap
[] = {
28 {PORTSC_CONN_CHANGE
, USB_PORT_STAT_C_CONNECTION
},
29 {PORTSC_ENABLE_CHANGE
, USB_PORT_STAT_C_ENABLE
},
30 {PORTSC_OVERCUR_CHANGE
, USB_PORT_STAT_C_OVERCURRENT
}
34 Read Ehc Operation register.
36 @param Ehc The EHCI device.
37 @param Offset The operation register offset.
39 @retval the register content read.
44 IN PEI_USB2_HC_DEV
*Ehc
,
50 ASSERT (Ehc
->CapLen
!= 0);
52 Data
= MmioRead32 (Ehc
->UsbHostControllerBaseAddress
+ Ehc
->CapLen
+ Offset
);
58 Write the data to the EHCI operation register.
60 @param Ehc The EHCI device.
61 @param Offset EHCI operation register offset.
62 @param Data The data to write.
67 IN PEI_USB2_HC_DEV
*Ehc
,
73 ASSERT (Ehc
->CapLen
!= 0);
75 MmioWrite32(Ehc
->UsbHostControllerBaseAddress
+ Ehc
->CapLen
+ Offset
, Data
);
80 Set one bit of the operational register while keeping other bits.
82 @param Ehc The EHCI device.
83 @param Offset The offset of the operational register.
84 @param Bit The bit mask of the register to set.
89 IN PEI_USB2_HC_DEV
*Ehc
,
96 Data
= EhcReadOpReg (Ehc
, Offset
);
98 EhcWriteOpReg (Ehc
, Offset
, Data
);
102 Clear one bit of the operational register while keeping other bits.
104 @param Ehc The EHCI device.
105 @param Offset The offset of the operational register.
106 @param Bit The bit mask of the register to clear.
111 IN PEI_USB2_HC_DEV
*Ehc
,
118 Data
= EhcReadOpReg (Ehc
, Offset
);
120 EhcWriteOpReg (Ehc
, Offset
, Data
);
124 Wait the operation register's bit as specified by Bit
125 to become set (or clear).
127 @param Ehc The EHCI device.
128 @param Offset The offset of the operational register.
129 @param Bit The bit mask of the register to wait for.
130 @param WaitToSet Wait the bit to set or clear.
131 @param Timeout The time to wait before abort (in millisecond).
133 @retval EFI_SUCCESS The bit successfully changed by host controller.
134 @retval EFI_TIMEOUT The time out occurred.
139 IN PEI_USB2_HC_DEV
*Ehc
,
142 IN BOOLEAN WaitToSet
,
148 for (Index
= 0; Index
< Timeout
/ EHC_SYNC_POLL_INTERVAL
+ 1; Index
++) {
149 if (EHC_REG_BIT_IS_SET (Ehc
, Offset
, Bit
) == WaitToSet
) {
153 MicroSecondDelay (EHC_SYNC_POLL_INTERVAL
);
160 Read EHCI capability register.
162 @param Ehc The EHCI device.
163 @param Offset Capability register address.
165 @retval the register content read.
170 IN PEI_USB2_HC_DEV
*Ehc
,
176 Data
= MmioRead32(Ehc
->UsbHostControllerBaseAddress
+ Offset
);
182 Set door bell and wait it to be ACKed by host controller.
183 This function is used to synchronize with the hardware.
185 @param Ehc The EHCI device.
186 @param Timeout The time to wait before abort (in millisecond, ms).
188 @retval EFI_TIMEOUT Time out happened while waiting door bell to set.
189 @retval EFI_SUCCESS Synchronized with the hardware.
193 EhcSetAndWaitDoorBell (
194 IN PEI_USB2_HC_DEV
*Ehc
,
201 EhcSetOpRegBit (Ehc
, EHC_USBCMD_OFFSET
, USBCMD_IAAD
);
203 Status
= EhcWaitOpRegBit (Ehc
, EHC_USBSTS_OFFSET
, USBSTS_IAA
, TRUE
, Timeout
);
206 // ACK the IAA bit in USBSTS register. Make sure other
207 // interrupt bits are not ACKed. These bits are WC (Write Clean).
209 Data
= EhcReadOpReg (Ehc
, EHC_USBSTS_OFFSET
);
210 Data
&= ~USBSTS_INTACK_MASK
;
213 EhcWriteOpReg (Ehc
, EHC_USBSTS_OFFSET
, Data
);
219 Clear all the interrutp status bits, these bits
222 @param Ehc The EHCI device.
227 IN PEI_USB2_HC_DEV
*Ehc
230 EhcWriteOpReg (Ehc
, EHC_USBSTS_OFFSET
, USBSTS_INTACK_MASK
);
234 Enable the periodic schedule then wait EHC to
237 @param Ehc The EHCI device.
238 @param Timeout The time to wait before abort (in millisecond, ms).
240 @retval EFI_TIMEOUT Time out happened while enabling periodic schedule.
241 @retval EFI_SUCCESS The periodical schedule is enabled.
245 EhcEnablePeriodSchd (
246 IN PEI_USB2_HC_DEV
*Ehc
,
252 EhcSetOpRegBit (Ehc
, EHC_USBCMD_OFFSET
, USBCMD_ENABLE_PERIOD
);
254 Status
= EhcWaitOpRegBit (Ehc
, EHC_USBSTS_OFFSET
, USBSTS_PERIOD_ENABLED
, TRUE
, Timeout
);
259 Enable asynchrounous schedule.
261 @param Ehc The EHCI device.
262 @param Timeout Time to wait before abort.
264 @retval EFI_SUCCESS The EHCI asynchronous schedule is enabled.
265 @retval Others Failed to enable the asynchronous scheudle.
270 IN PEI_USB2_HC_DEV
*Ehc
,
276 EhcSetOpRegBit (Ehc
, EHC_USBCMD_OFFSET
, USBCMD_ENABLE_ASYNC
);
278 Status
= EhcWaitOpRegBit (Ehc
, EHC_USBSTS_OFFSET
, USBSTS_ASYNC_ENABLED
, TRUE
, Timeout
);
283 Check whether Ehc is halted.
285 @param Ehc The EHCI device.
287 @retval TRUE The controller is halted.
288 @retval FALSE The controller isn't halted.
293 IN PEI_USB2_HC_DEV
*Ehc
296 return EHC_REG_BIT_IS_SET (Ehc
, EHC_USBSTS_OFFSET
, USBSTS_HALT
);
300 Check whether system error occurred.
302 @param Ehc The EHCI device.
304 @retval TRUE System error happened.
305 @retval FALSE No system error.
310 IN PEI_USB2_HC_DEV
*Ehc
313 return EHC_REG_BIT_IS_SET (Ehc
, EHC_USBSTS_OFFSET
, USBSTS_SYS_ERROR
);
317 Reset the host controller.
319 @param Ehc The EHCI device.
320 @param Timeout Time to wait before abort (in millisecond, ms).
322 @retval EFI_TIMEOUT The transfer failed due to time out.
323 @retval Others Failed to reset the host.
328 IN PEI_USB2_HC_DEV
*Ehc
,
335 // Host can only be reset when it is halt. If not so, halt it
337 if (!EHC_REG_BIT_IS_SET (Ehc
, EHC_USBSTS_OFFSET
, USBSTS_HALT
)) {
338 Status
= EhcHaltHC (Ehc
, Timeout
);
340 if (EFI_ERROR (Status
)) {
345 EhcSetOpRegBit (Ehc
, EHC_USBCMD_OFFSET
, USBCMD_RESET
);
346 Status
= EhcWaitOpRegBit (Ehc
, EHC_USBCMD_OFFSET
, USBCMD_RESET
, FALSE
, Timeout
);
351 Halt the host controller.
353 @param Ehc The EHCI device.
354 @param Timeout Time to wait before abort.
356 @retval EFI_TIMEOUT Failed to halt the controller before Timeout.
357 @retval EFI_SUCCESS The EHCI is halt.
362 IN PEI_USB2_HC_DEV
*Ehc
,
368 EhcClearOpRegBit (Ehc
, EHC_USBCMD_OFFSET
, USBCMD_RUN
);
369 Status
= EhcWaitOpRegBit (Ehc
, EHC_USBSTS_OFFSET
, USBSTS_HALT
, TRUE
, Timeout
);
376 @param Ehc The EHCI device.
377 @param Timeout Time to wait before abort.
379 @retval EFI_SUCCESS The EHCI is running.
380 @retval Others Failed to set the EHCI to run.
385 IN PEI_USB2_HC_DEV
*Ehc
,
391 EhcSetOpRegBit (Ehc
, EHC_USBCMD_OFFSET
, USBCMD_RUN
);
392 Status
= EhcWaitOpRegBit (Ehc
, EHC_USBSTS_OFFSET
, USBSTS_HALT
, FALSE
, Timeout
);
397 Power On All EHCI Ports.
399 @param Ehc The EHCI device.
404 IN PEI_USB2_HC_DEV
*Ehc
411 PortNumber
= (UINT8
)(Ehc
->HcStructParams
& HCSP_NPORTS
);
412 for (Index
= 0; Index
< PortNumber
; Index
++) {
414 // Do not clear port status bits on initialization. Otherwise devices will
415 // not enumerate properly at startup.
417 RegVal
= EhcReadOpReg(Ehc
, EHC_PORT_STAT_OFFSET
+ 4 * Index
);
418 RegVal
&= ~PORTSC_CHANGE_MASK
;
419 RegVal
|= PORTSC_POWER
;
420 EhcWriteOpReg (Ehc
, EHC_PORT_STAT_OFFSET
+ 4 * Index
, RegVal
);
425 Initialize the HC hardware.
426 EHCI spec lists the five things to do to initialize the hardware.
427 1. Program CTRLDSSEGMENT.
428 2. Set USBINTR to enable interrupts.
429 3. Set periodic list base.
430 4. Set USBCMD, interrupt threshold, frame list size etc.
431 5. Write 1 to CONFIGFLAG to route all ports to EHCI.
433 @param Ehc The EHCI device.
435 @retval EFI_SUCCESS The EHCI has come out of halt state.
436 @retval EFI_TIMEOUT Time out happened.
441 IN PEI_USB2_HC_DEV
*Ehc
445 EFI_PHYSICAL_ADDRESS TempPtr
;
448 ASSERT (EhcIsHalt (Ehc
));
451 // Allocate the periodic frame and associated memeory
452 // management facilities if not already done.
454 if (Ehc
->PeriodFrame
!= NULL
) {
457 PageNumber
= sizeof(PEI_URB
)/PAGESIZE
+1;
458 Status
= PeiServicesAllocatePages (
463 Ehc
->Urb
= (PEI_URB
*) ((UINTN
) TempPtr
);
464 if (Ehc
->Urb
== NULL
) {
468 EhcPowerOnAllPorts (Ehc
);
469 MicroSecondDelay (EHC_ROOT_PORT_RECOVERY_STALL
);
471 Status
= EhcInitSched (Ehc
);
473 if (EFI_ERROR (Status
)) {
477 // 1. Program the CTRLDSSEGMENT register with the high 32 bit addr
479 EhcWriteOpReg (Ehc
, EHC_CTRLDSSEG_OFFSET
, Ehc
->High32bitAddr
);
482 // 2. Clear USBINTR to disable all the interrupt. UEFI works by polling
484 EhcWriteOpReg (Ehc
, EHC_USBINTR_OFFSET
, 0);
487 // 3. Program periodic frame list, already done in EhcInitSched
488 // 4. Start the Host Controller
490 EhcSetOpRegBit (Ehc
, EHC_USBCMD_OFFSET
, USBCMD_RUN
);
493 // 5. Set all ports routing to EHC
495 EhcSetOpRegBit (Ehc
, EHC_CONFIG_FLAG_OFFSET
, CONFIGFLAG_ROUTE_EHC
);
498 // Wait roothub port power stable
500 MicroSecondDelay (EHC_ROOT_PORT_RECOVERY_STALL
);
502 Status
= EhcEnablePeriodSchd (Ehc
, EHC_GENERIC_TIMEOUT
);
504 if (EFI_ERROR (Status
)) {
508 Status
= EhcEnableAsyncSchd (Ehc
, EHC_GENERIC_TIMEOUT
);
510 if (EFI_ERROR (Status
)) {
518 Submits bulk transfer to a bulk endpoint of a USB device.
520 @param PeiServices The pointer of EFI_PEI_SERVICES.
521 @param This The pointer of PEI_USB2_HOST_CONTROLLER_PPI.
522 @param DeviceAddress Target device address.
523 @param EndPointAddress Endpoint number and its direction in bit 7.
524 @param DeviceSpeed Device speed, Low speed device doesn't support
526 @param MaximumPacketLength Maximum packet size the endpoint is capable of
527 sending or receiving.
528 @param Data Array of pointers to the buffers of data to transmit
529 from or receive into.
530 @param DataLength The lenght of the data buffer.
531 @param DataToggle On input, the initial data toggle for the transfer;
532 On output, it is updated to to next data toggle to use of
533 the subsequent bulk transfer.
534 @param TimeOut Indicates the maximum time, in millisecond, which the
535 transfer is allowed to complete.
536 If Timeout is 0, then the caller must wait for the function
537 to be completed until EFI_SUCCESS or EFI_DEVICE_ERROR is returned.
538 @param Translator A pointr to the transaction translator data.
539 @param TransferResult A pointer to the detailed result information of the
542 @retval EFI_SUCCESS The transfer was completed successfully.
543 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resource.
544 @retval EFI_INVALID_PARAMETER Parameters are invalid.
545 @retval EFI_TIMEOUT The transfer failed due to timeout.
546 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
552 IN EFI_PEI_SERVICES
**PeiServices
,
553 IN PEI_USB2_HOST_CONTROLLER_PPI
*This
,
554 IN UINT8 DeviceAddress
,
555 IN UINT8 EndPointAddress
,
556 IN UINT8 DeviceSpeed
,
557 IN UINTN MaximumPacketLength
,
558 IN OUT VOID
*Data
[EFI_USB_MAX_BULK_BUFFER_NUM
],
559 IN OUT UINTN
*DataLength
,
560 IN OUT UINT8
*DataToggle
,
562 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
563 OUT UINT32
*TransferResult
566 PEI_USB2_HC_DEV
*Ehc
;
571 // Validate the parameters
573 if ((DataLength
== NULL
) || (*DataLength
== 0) ||
574 (Data
== NULL
) || (Data
[0] == NULL
) || (TransferResult
== NULL
)) {
575 return EFI_INVALID_PARAMETER
;
578 if ((*DataToggle
!= 0) && (*DataToggle
!= 1)) {
579 return EFI_INVALID_PARAMETER
;
582 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) ||
583 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
584 ((EFI_USB_SPEED_HIGH
== DeviceSpeed
) && (MaximumPacketLength
> 512))) {
585 return EFI_INVALID_PARAMETER
;
588 Ehc
=PEI_RECOVERY_USB_EHC_DEV_FROM_EHCI_THIS(This
);
589 *TransferResult
= EFI_USB_ERR_SYSTEM
;
590 Status
= EFI_DEVICE_ERROR
;
592 if (EhcIsHalt (Ehc
) || EhcIsSysError (Ehc
)) {
593 EhcAckAllInterrupt (Ehc
);
597 EhcAckAllInterrupt (Ehc
);
600 // Create a new URB, insert it into the asynchronous
601 // schedule list, then poll the execution status.
621 Status
= EFI_OUT_OF_RESOURCES
;
625 EhcLinkQhToAsync (Ehc
, Urb
->Qh
);
626 Status
= EhcExecTransfer (Ehc
, Urb
, TimeOut
);
627 EhcUnlinkQhFromAsync (Ehc
, Urb
->Qh
);
629 *TransferResult
= Urb
->Result
;
630 *DataLength
= Urb
->Completed
;
631 *DataToggle
= Urb
->DataToggle
;
633 if (*TransferResult
== EFI_USB_NOERROR
) {
634 Status
= EFI_SUCCESS
;
637 EhcAckAllInterrupt (Ehc
);
638 EhcFreeUrb (Ehc
, Urb
);
645 Retrieves the number of root hub ports.
647 @param[in] PeiServices The pointer to the PEI Services Table.
648 @param[in] This The pointer to this instance of the
649 PEI_USB2_HOST_CONTROLLER_PPI.
650 @param[out] PortNumber The pointer to the number of the root hub ports.
652 @retval EFI_SUCCESS The port number was retrieved successfully.
653 @retval EFI_INVALID_PARAMETER PortNumber is NULL.
658 EhcGetRootHubPortNumber (
659 IN EFI_PEI_SERVICES
**PeiServices
,
660 IN PEI_USB2_HOST_CONTROLLER_PPI
*This
,
661 OUT UINT8
*PortNumber
665 PEI_USB2_HC_DEV
*EhcDev
;
666 EhcDev
= PEI_RECOVERY_USB_EHC_DEV_FROM_EHCI_THIS (This
);
668 if (PortNumber
== NULL
) {
669 return EFI_INVALID_PARAMETER
;
672 *PortNumber
= (UINT8
)(EhcDev
->HcStructParams
& HCSP_NPORTS
);
678 Clears a feature for the specified root hub port.
680 @param PeiServices The pointer of EFI_PEI_SERVICES.
681 @param This The pointer of PEI_USB2_HOST_CONTROLLER_PPI.
682 @param PortNumber Specifies the root hub port whose feature
683 is requested to be cleared.
684 @param PortFeature Indicates the feature selector associated with the
685 feature clear request.
687 @retval EFI_SUCCESS The feature specified by PortFeature was cleared
688 for the USB root hub port specified by PortNumber.
689 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
694 EhcClearRootHubPortFeature (
695 IN EFI_PEI_SERVICES
**PeiServices
,
696 IN PEI_USB2_HOST_CONTROLLER_PPI
*This
,
698 IN EFI_USB_PORT_FEATURE PortFeature
701 PEI_USB2_HC_DEV
*Ehc
;
707 Ehc
= PEI_RECOVERY_USB_EHC_DEV_FROM_EHCI_THIS (This
);
708 Status
= EFI_SUCCESS
;
710 TotalPort
= (Ehc
->HcStructParams
& HCSP_NPORTS
);
712 if (PortNumber
>= TotalPort
) {
713 Status
= EFI_INVALID_PARAMETER
;
717 Offset
= EHC_PORT_STAT_OFFSET
+ (4 * PortNumber
);
718 State
= EhcReadOpReg (Ehc
, Offset
);
719 State
&= ~PORTSC_CHANGE_MASK
;
721 switch (PortFeature
) {
722 case EfiUsbPortEnable
:
724 // Clear PORT_ENABLE feature means disable port.
726 State
&= ~PORTSC_ENABLED
;
727 EhcWriteOpReg (Ehc
, Offset
, State
);
730 case EfiUsbPortSuspend
:
732 // A write of zero to this bit is ignored by the host
733 // controller. The host controller will unconditionally
734 // set this bit to a zero when:
735 // 1. software sets the Forct Port Resume bit to a zero from a one.
736 // 2. software sets the Port Reset bit to a one frome a zero.
738 State
&= ~PORSTSC_RESUME
;
739 EhcWriteOpReg (Ehc
, Offset
, State
);
742 case EfiUsbPortReset
:
744 // Clear PORT_RESET means clear the reset signal.
746 State
&= ~PORTSC_RESET
;
747 EhcWriteOpReg (Ehc
, Offset
, State
);
750 case EfiUsbPortOwner
:
752 // Clear port owner means this port owned by EHC
754 State
&= ~PORTSC_OWNER
;
755 EhcWriteOpReg (Ehc
, Offset
, State
);
758 case EfiUsbPortConnectChange
:
760 // Clear connect status change
762 State
|= PORTSC_CONN_CHANGE
;
763 EhcWriteOpReg (Ehc
, Offset
, State
);
766 case EfiUsbPortEnableChange
:
768 // Clear enable status change
770 State
|= PORTSC_ENABLE_CHANGE
;
771 EhcWriteOpReg (Ehc
, Offset
, State
);
774 case EfiUsbPortOverCurrentChange
:
776 // Clear PortOverCurrent change
778 State
|= PORTSC_OVERCUR_CHANGE
;
779 EhcWriteOpReg (Ehc
, Offset
, State
);
782 case EfiUsbPortPower
:
783 case EfiUsbPortSuspendChange
:
784 case EfiUsbPortResetChange
:
786 // Not supported or not related operation
791 Status
= EFI_INVALID_PARAMETER
;
800 Sets a feature for the specified root hub port.
802 @param PeiServices The pointer of EFI_PEI_SERVICES
803 @param This The pointer of PEI_USB2_HOST_CONTROLLER_PPI
804 @param PortNumber Root hub port to set.
805 @param PortFeature Feature to set.
807 @retval EFI_SUCCESS The feature specified by PortFeature was set.
808 @retval EFI_INVALID_PARAMETER PortNumber is invalid or PortFeature is invalid.
809 @retval EFI_TIMEOUT The time out occurred.
814 EhcSetRootHubPortFeature (
815 IN EFI_PEI_SERVICES
**PeiServices
,
816 IN PEI_USB2_HOST_CONTROLLER_PPI
*This
,
818 IN EFI_USB_PORT_FEATURE PortFeature
821 PEI_USB2_HC_DEV
*Ehc
;
827 Ehc
= PEI_RECOVERY_USB_EHC_DEV_FROM_EHCI_THIS (This
);
828 Status
= EFI_SUCCESS
;
830 TotalPort
= (Ehc
->HcStructParams
& HCSP_NPORTS
);
832 if (PortNumber
>= TotalPort
) {
833 Status
= EFI_INVALID_PARAMETER
;
837 Offset
= (UINT32
) (EHC_PORT_STAT_OFFSET
+ (4 * PortNumber
));
838 State
= EhcReadOpReg (Ehc
, Offset
);
841 // Mask off the port status change bits, these bits are
844 State
&= ~PORTSC_CHANGE_MASK
;
846 switch (PortFeature
) {
847 case EfiUsbPortEnable
:
849 // Sofeware can't set this bit, Port can only be enable by
850 // EHCI as a part of the reset and enable
852 State
|= PORTSC_ENABLED
;
853 EhcWriteOpReg (Ehc
, Offset
, State
);
856 case EfiUsbPortSuspend
:
857 State
|= PORTSC_SUSPEND
;
858 EhcWriteOpReg (Ehc
, Offset
, State
);
861 case EfiUsbPortReset
:
863 // Make sure Host Controller not halt before reset it
865 if (EhcIsHalt (Ehc
)) {
866 Status
= EhcRunHC (Ehc
, EHC_GENERIC_TIMEOUT
);
868 if (EFI_ERROR (Status
)) {
874 // Set one to PortReset bit must also set zero to PortEnable bit
876 State
|= PORTSC_RESET
;
877 State
&= ~PORTSC_ENABLED
;
878 EhcWriteOpReg (Ehc
, Offset
, State
);
881 case EfiUsbPortPower
:
883 // Not supported, ignore the operation
885 Status
= EFI_SUCCESS
;
888 case EfiUsbPortOwner
:
889 State
|= PORTSC_OWNER
;
890 EhcWriteOpReg (Ehc
, Offset
, State
);
894 Status
= EFI_INVALID_PARAMETER
;
902 Retrieves the current status of a USB root hub port.
904 @param PeiServices The pointer of EFI_PEI_SERVICES.
905 @param This The pointer of PEI_USB2_HOST_CONTROLLER_PPI.
906 @param PortNumber The root hub port to retrieve the state from.
907 @param PortStatus Variable to receive the port state.
909 @retval EFI_SUCCESS The status of the USB root hub port specified.
910 by PortNumber was returned in PortStatus.
911 @retval EFI_INVALID_PARAMETER PortNumber is invalid.
916 EhcGetRootHubPortStatus (
917 IN EFI_PEI_SERVICES
**PeiServices
,
918 IN PEI_USB2_HOST_CONTROLLER_PPI
*This
,
920 OUT EFI_USB_PORT_STATUS
*PortStatus
923 PEI_USB2_HC_DEV
*Ehc
;
931 if (PortStatus
== NULL
) {
932 return EFI_INVALID_PARAMETER
;
935 Ehc
= PEI_RECOVERY_USB_EHC_DEV_FROM_EHCI_THIS(This
);
936 Status
= EFI_SUCCESS
;
938 TotalPort
= (Ehc
->HcStructParams
& HCSP_NPORTS
);
940 if (PortNumber
>= TotalPort
) {
941 Status
= EFI_INVALID_PARAMETER
;
945 Offset
= (UINT32
) (EHC_PORT_STAT_OFFSET
+ (4 * PortNumber
));
946 PortStatus
->PortStatus
= 0;
947 PortStatus
->PortChangeStatus
= 0;
949 State
= EhcReadOpReg (Ehc
, Offset
);
952 // Identify device speed. If in K state, it is low speed.
953 // If the port is enabled after reset, the device is of
954 // high speed. The USB bus driver should retrieve the actual
955 // port speed after reset.
957 if (EHC_BIT_IS_SET (State
, PORTSC_LINESTATE_K
)) {
958 PortStatus
->PortStatus
|= USB_PORT_STAT_LOW_SPEED
;
960 } else if (EHC_BIT_IS_SET (State
, PORTSC_ENABLED
)) {
961 PortStatus
->PortStatus
|= USB_PORT_STAT_HIGH_SPEED
;
965 // Convert the EHCI port/port change state to UEFI status
967 MapSize
= sizeof (mUsbPortStateMap
) / sizeof (USB_PORT_STATE_MAP
);
969 for (Index
= 0; Index
< MapSize
; Index
++) {
970 if (EHC_BIT_IS_SET (State
, mUsbPortStateMap
[Index
].HwState
)) {
971 PortStatus
->PortStatus
= (UINT16
) (PortStatus
->PortStatus
| mUsbPortStateMap
[Index
].UefiState
);
975 MapSize
= sizeof (mUsbPortChangeMap
) / sizeof (USB_PORT_STATE_MAP
);
977 for (Index
= 0; Index
< MapSize
; Index
++) {
978 if (EHC_BIT_IS_SET (State
, mUsbPortChangeMap
[Index
].HwState
)) {
979 PortStatus
->PortChangeStatus
= (UINT16
) (PortStatus
->PortChangeStatus
| mUsbPortChangeMap
[Index
].UefiState
);
988 Submits control transfer to a target USB device.
990 @param PeiServices The pointer of EFI_PEI_SERVICES.
991 @param This The pointer of PEI_USB2_HOST_CONTROLLER_PPI.
992 @param DeviceAddress The target device address.
993 @param DeviceSpeed Target device speed.
994 @param MaximumPacketLength Maximum packet size the default control transfer
995 endpoint is capable of sending or receiving.
996 @param Request USB device request to send.
997 @param TransferDirection Specifies the data direction for the data stage.
998 @param Data Data buffer to be transmitted or received from USB device.
999 @param DataLength The size (in bytes) of the data buffer.
1000 @param TimeOut Indicates the maximum timeout, in millisecond.
1001 If Timeout is 0, then the caller must wait for the function
1002 to be completed until EFI_SUCCESS or EFI_DEVICE_ERROR is returned.
1003 @param Translator Transaction translator to be used by this device.
1004 @param TransferResult Return the result of this control transfer.
1006 @retval EFI_SUCCESS Transfer was completed successfully.
1007 @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources.
1008 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
1009 @retval EFI_TIMEOUT Transfer failed due to timeout.
1010 @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error.
1015 EhcControlTransfer (
1016 IN EFI_PEI_SERVICES
**PeiServices
,
1017 IN PEI_USB2_HOST_CONTROLLER_PPI
*This
,
1018 IN UINT8 DeviceAddress
,
1019 IN UINT8 DeviceSpeed
,
1020 IN UINTN MaximumPacketLength
,
1021 IN EFI_USB_DEVICE_REQUEST
*Request
,
1022 IN EFI_USB_DATA_DIRECTION TransferDirection
,
1024 IN OUT UINTN
*DataLength
,
1026 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Translator
,
1027 OUT UINT32
*TransferResult
1030 PEI_USB2_HC_DEV
*Ehc
;
1036 // Validate parameters
1038 if ((Request
== NULL
) || (TransferResult
== NULL
)) {
1039 return EFI_INVALID_PARAMETER
;
1042 if ((TransferDirection
!= EfiUsbDataIn
) &&
1043 (TransferDirection
!= EfiUsbDataOut
) &&
1044 (TransferDirection
!= EfiUsbNoData
)) {
1045 return EFI_INVALID_PARAMETER
;
1048 if ((TransferDirection
== EfiUsbNoData
) &&
1049 ((Data
!= NULL
) || (*DataLength
!= 0))) {
1050 return EFI_INVALID_PARAMETER
;
1053 if ((TransferDirection
!= EfiUsbNoData
) &&
1054 ((Data
== NULL
) || (*DataLength
== 0))) {
1055 return EFI_INVALID_PARAMETER
;
1058 if ((MaximumPacketLength
!= 8) && (MaximumPacketLength
!= 16) &&
1059 (MaximumPacketLength
!= 32) && (MaximumPacketLength
!= 64)) {
1060 return EFI_INVALID_PARAMETER
;
1064 if ((DeviceSpeed
== EFI_USB_SPEED_LOW
) ||
1065 ((DeviceSpeed
== EFI_USB_SPEED_FULL
) && (MaximumPacketLength
> 64)) ||
1066 ((EFI_USB_SPEED_HIGH
== DeviceSpeed
) && (MaximumPacketLength
> 512))) {
1067 return EFI_INVALID_PARAMETER
;
1070 Ehc
= PEI_RECOVERY_USB_EHC_DEV_FROM_EHCI_THIS (This
);
1072 Status
= EFI_DEVICE_ERROR
;
1073 *TransferResult
= EFI_USB_ERR_SYSTEM
;
1075 if (EhcIsHalt (Ehc
) || EhcIsSysError (Ehc
)) {
1076 EhcAckAllInterrupt (Ehc
);
1080 EhcAckAllInterrupt (Ehc
);
1083 // Create a new URB, insert it into the asynchronous
1084 // schedule list, then poll the execution status.
1087 // Encode the direction in address, although default control
1088 // endpoint is bidirectional. EhcCreateUrb expects this
1089 // combination of Ep addr and its direction.
1091 Endpoint
= (UINT8
) (0 | ((TransferDirection
== EfiUsbDataIn
) ? 0x80 : 0));
1092 Urb
= EhcCreateUrb (
1098 MaximumPacketLength
,
1110 Status
= EFI_OUT_OF_RESOURCES
;
1114 EhcLinkQhToAsync (Ehc
, Urb
->Qh
);
1115 Status
= EhcExecTransfer (Ehc
, Urb
, TimeOut
);
1116 EhcUnlinkQhFromAsync (Ehc
, Urb
->Qh
);
1119 // Get the status from URB. The result is updated in EhcCheckUrbResult
1120 // which is called by EhcExecTransfer
1122 *TransferResult
= Urb
->Result
;
1123 *DataLength
= Urb
->Completed
;
1125 if (*TransferResult
== EFI_USB_NOERROR
) {
1126 Status
= EFI_SUCCESS
;
1129 EhcAckAllInterrupt (Ehc
);
1130 EhcFreeUrb (Ehc
, Urb
);
1137 One notified function to stop the Host Controller at the end of PEI
1139 @param[in] PeiServices Pointer to PEI Services Table.
1140 @param[in] NotifyDescriptor Pointer to the descriptor for the Notification event that
1141 caused this function to execute.
1142 @param[in] Ppi Pointer to the PPI data associated with this function.
1144 @retval EFI_SUCCESS The function completes successfully
1150 IN EFI_PEI_SERVICES
**PeiServices
,
1151 IN EFI_PEI_NOTIFY_DESCRIPTOR
*NotifyDescriptor
,
1155 PEI_USB2_HC_DEV
*Ehc
;
1157 Ehc
= PEI_RECOVERY_USB_EHC_DEV_FROM_THIS_NOTIFY (NotifyDescriptor
);
1159 EhcHaltHC (Ehc
, EHC_GENERIC_TIMEOUT
);
1167 @param FileHandle Handle of the file being invoked.
1168 @param PeiServices Describes the list of possible PEI Services.
1170 @retval EFI_SUCCESS PPI successfully installed.
1176 IN EFI_PEI_FILE_HANDLE FileHandle
,
1177 IN CONST EFI_PEI_SERVICES
**PeiServices
1180 PEI_USB_CONTROLLER_PPI
*ChipSetUsbControllerPpi
;
1183 UINTN ControllerType
;
1186 PEI_USB2_HC_DEV
*EhcDev
;
1187 EFI_PHYSICAL_ADDRESS TempPtr
;
1190 // Shadow this PEIM to run from memory
1192 if (!EFI_ERROR (PeiServicesRegisterForShadow (FileHandle
))) {
1196 Status
= PeiServicesLocatePpi (
1197 &gPeiUsbControllerPpiGuid
,
1200 (VOID
**) &ChipSetUsbControllerPpi
1202 if (EFI_ERROR (Status
)) {
1203 return EFI_UNSUPPORTED
;
1208 Status
= ChipSetUsbControllerPpi
->GetUsbController (
1209 (EFI_PEI_SERVICES
**) PeiServices
,
1210 ChipSetUsbControllerPpi
,
1216 // When status is error, meant no controller is found
1218 if (EFI_ERROR (Status
)) {
1223 // This PEIM is for UHC type controller.
1225 if (ControllerType
!= PEI_EHCI_CONTROLLER
) {
1230 MemPages
= sizeof (PEI_USB2_HC_DEV
) / PAGESIZE
+ 1;
1231 Status
= PeiServicesAllocatePages (
1232 EfiBootServicesCode
,
1236 if (EFI_ERROR (Status
)) {
1237 return EFI_OUT_OF_RESOURCES
;
1240 ZeroMem((VOID
*)(UINTN
)TempPtr
, MemPages
*PAGESIZE
);
1241 EhcDev
= (PEI_USB2_HC_DEV
*) ((UINTN
) TempPtr
);
1243 EhcDev
->Signature
= USB2_HC_DEV_SIGNATURE
;
1245 IoMmuInit (&EhcDev
->IoMmu
);
1247 EhcDev
->UsbHostControllerBaseAddress
= (UINT32
) BaseAddress
;
1250 EhcDev
->HcStructParams
= EhcReadCapRegister (EhcDev
, EHC_HCSPARAMS_OFFSET
);
1251 EhcDev
->HcCapParams
= EhcReadCapRegister (EhcDev
, EHC_HCCPARAMS_OFFSET
);
1252 EhcDev
->CapLen
= EhcReadCapRegister (EhcDev
, EHC_CAPLENGTH_OFFSET
) & 0x0FF;
1254 // Initialize Uhc's hardware
1256 Status
= InitializeUsbHC (EhcDev
);
1257 if (EFI_ERROR (Status
)) {
1261 EhcDev
->Usb2HostControllerPpi
.ControlTransfer
= EhcControlTransfer
;
1262 EhcDev
->Usb2HostControllerPpi
.BulkTransfer
= EhcBulkTransfer
;
1263 EhcDev
->Usb2HostControllerPpi
.GetRootHubPortNumber
= EhcGetRootHubPortNumber
;
1264 EhcDev
->Usb2HostControllerPpi
.GetRootHubPortStatus
= EhcGetRootHubPortStatus
;
1265 EhcDev
->Usb2HostControllerPpi
.SetRootHubPortFeature
= EhcSetRootHubPortFeature
;
1266 EhcDev
->Usb2HostControllerPpi
.ClearRootHubPortFeature
= EhcClearRootHubPortFeature
;
1268 EhcDev
->PpiDescriptor
.Flags
= (EFI_PEI_PPI_DESCRIPTOR_PPI
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
);
1269 EhcDev
->PpiDescriptor
.Guid
= &gPeiUsb2HostControllerPpiGuid
;
1270 EhcDev
->PpiDescriptor
.Ppi
= &EhcDev
->Usb2HostControllerPpi
;
1272 Status
= PeiServicesInstallPpi (&EhcDev
->PpiDescriptor
);
1273 if (EFI_ERROR (Status
)) {
1278 EhcDev
->EndOfPeiNotifyList
.Flags
= (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK
| EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST
);
1279 EhcDev
->EndOfPeiNotifyList
.Guid
= &gEfiEndOfPeiSignalPpiGuid
;
1280 EhcDev
->EndOfPeiNotifyList
.Notify
= EhcEndOfPei
;
1282 PeiServicesNotifyPpi (&EhcDev
->EndOfPeiNotifyList
);
1291 @param EhcDev EHCI Device.
1293 @retval EFI_SUCCESS EHCI successfully initialized.
1294 @retval EFI_ABORTED EHCI was failed to be initialized.
1299 IN PEI_USB2_HC_DEV
*EhcDev
1305 EhcResetHC (EhcDev
, EHC_RESET_TIMEOUT
);
1307 Status
= EhcInitHC (EhcDev
);
1309 if (EFI_ERROR (Status
)) {