2 Debug Port Library implementation based on usb debug port.
4 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include <IndustryStandard/Pci.h>
17 #include <IndustryStandard/Usb.h>
18 #include <Library/IoLib.h>
19 #include <Library/PciLib.h>
20 #include <Library/PcdLib.h>
21 #include <Library/TimerLib.h>
22 #include <Library/DebugCommunicationLib.h>
23 #include <Library/BaseMemoryLib.h>
24 #include <Library/BaseLib.h>
25 #include <Library/DebugLib.h>
27 #define SETUP_PID 0x2D
28 #define INPUT_PID 0x69
29 #define OUTPUT_PID 0xE1
30 #define ERROR_PID 0x55
31 #define DATA0_PID 0xC3
32 #define DATA1_PID 0x4B
33 #define DATA2_PID 0x87
34 #define MDATA_PID 0x0F
37 #define STALL_PID 0x1E
40 #define PCI_CAPABILITY_ID_DEBUG_PORT 0x0A
41 #define USB_DEBUG_PORT_MAX_PACKET_SIZE 0x08
43 #define USB_DEBUG_PORT_IN_USE BIT10
44 #define USB_DEBUG_PORT_ENABLE BIT28
45 #define USB_DEBUG_PORT_OWNER BIT30
47 #define USB_PORT_LINE_STATUS_LS 0x400
48 #define USB_PORT_LINE_STATUS_MASK 0xC00
51 // Usb debug device descriptor, which is defined at
52 // USB2 Debug Device Specification.
54 typedef struct _USB_DEBUG_PORT_DESCRIPTOR
{
57 UINT8 DebugInEndpoint
;
58 UINT8 DebugOutEndpoint
;
59 }USB_DEBUG_PORT_DESCRIPTOR
;
61 USB_DEVICE_REQUEST mGetDebugDescriptor
= {
63 USB_REQ_GET_DESCRIPTOR
,
66 sizeof(USB_DEBUG_PORT_DESCRIPTOR
)
69 USB_DEVICE_REQUEST mSetDebugFeature
= {
77 USB_DEVICE_REQUEST mSetDebugAddress
= {
86 // Usb debug port register file, which is defined at
87 // EHCI Specification.
89 typedef struct _USB_DEBUG_PORT_REGISTER
{
100 }USB_DEBUG_PORT_REGISTER
;
103 // The state machine of usb debug port
105 #define USBDBG_NO_DEV 0 // No device present at debug port
106 #define USBDBG_NO_DBG_CAB 1 // The device attached is not usb debug cable
107 #define USBDBG_DBG_CAB 2 // The device attached is usb debug cable
108 #define USBDBG_INIT_DONE 4 // The usb debug cable device is initialized
109 #define USBDBG_RESET 8 // The system is reset
113 // The internal data structure of DEBUG_PORT_HANDLE, which stores some
114 // important datum which are used across various phases.
116 typedef struct _USB_DEBUG_PORT_HANDLE
{
118 // The usb debug port memory BAR number in EHCI configuration space.
120 UINT8 DebugPortBarNumber
;
123 // The offset of usb debug port registers in EHCI memory range.
125 UINT16 DebugPortOffset
;
127 // The usb debug port memory BAR address.
129 UINT32 UsbDebugPortMemoryBase
;
131 // The EHCI memory BAR address.
133 UINT32 EhciMemoryBase
;
135 // The Bulk In endpoint toggle bit.
139 // The Bulk Out endpoint toggle bit.
143 // The available data length in the following data buffer.
147 // The data buffer. Maximum length is 8 bytes.
150 } USB_DEBUG_PORT_HANDLE
;
154 // The global variable which can be used after memory is ready.
156 USB_DEBUG_PORT_HANDLE mUsbDebugPortHandle
;
159 Calculate the usb debug port bar address.
161 @param DebugPortOffset Get usb debug port offset in the usb debug port memory space.
162 @param DebugPortBarNumbar Get the bar number at which usb debug port is located.
164 @retval RETURN_UNSUPPORTED The usb host controller does not supported usb debug port capability.
165 @retval RETURN_SUCCESS Get bar and offset successfully.
170 CalculateUsbDebugPortBar (
171 OUT UINT16
*DebugPortOffset
,
172 OUT UINT8
*DebugPortBarNumbar
184 VendorId
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_VENDOR_ID_OFFSET
);
185 DeviceId
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_DEVICE_ID_OFFSET
);
187 if ((VendorId
== 0xFFFF) || (DeviceId
== 0xFFFF)) {
188 return RETURN_UNSUPPORTED
;
191 ProgInterface
= PciRead8 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_CLASSCODE_OFFSET
);
192 SubClassCode
= PciRead8 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_CLASSCODE_OFFSET
+ 1);
193 BaseCode
= PciRead8 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_CLASSCODE_OFFSET
+ 2);
195 if ((ProgInterface
!= PCI_IF_EHCI
) || (SubClassCode
!= PCI_CLASS_SERIAL_USB
) || (BaseCode
!= PCI_CLASS_SERIAL
)) {
196 return RETURN_UNSUPPORTED
;
200 // Enable Ehci Host Controller MMIO Space.
202 PciStatus
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_PRIMARY_STATUS_OFFSET
);
204 if ((PciStatus
& EFI_PCI_STATUS_CAPABILITY
) == 0) {
206 // The Pci Device Doesn't Support Capability Pointer.
208 return RETURN_UNSUPPORTED
;
212 // Get Pointer To Capability List
214 CapabilityPtr
= PciRead8(PcdGet32(PcdUsbEhciPciAddress
) + PCI_CAPBILITY_POINTER_OFFSET
);
217 // Find Capability ID 0xA, Which Is For Debug Port
219 while (CapabilityPtr
!= 0) {
220 CapabilityId
= PciRead8(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
);
221 if (CapabilityId
== PCI_CAPABILITY_ID_DEBUG_PORT
) {
224 CapabilityPtr
= PciRead8(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
+ 1);
228 // No Debug Port Capability Found
230 if (CapabilityPtr
== 0) {
231 return RETURN_UNSUPPORTED
;
235 // Get The Base Address Of Debug Port Register In Debug Port Capability Register
237 *DebugPortOffset
= (UINT16
)(PciRead16(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
+ 2) & 0x1FFF);
238 *DebugPortBarNumbar
= (UINT8
)((PciRead16(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
+ 2) >> 13) - 1);
240 return RETURN_SUCCESS
;
244 Do a usb IN transaction by usb debug port.
246 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
247 @param Buffer Pointer to the buffer receiving data.
248 @param Length Number of bytes of the received data.
249 @param Token The token PID for each USB transaction.
250 @param Addr The usb device address for usb transaction.
251 @param Ep The endpoint for usb transaction.
252 @param DataToggle The toggle bit used at usb transaction.
254 @retval RETURN_SUCCESS The IN transaction is executed successfully.
255 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
256 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
262 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
263 IN OUT UINT8
*Buffer
,
273 if (Length
== NULL
) {
274 return RETURN_INVALID_PARAMETER
;
278 DebugPortRegister
->TokenPid
= Token
;
279 if (DataToggle
!= 0) {
280 DebugPortRegister
->SendPid
= DATA1_PID
;
282 DebugPortRegister
->SendPid
= DATA0_PID
;
285 DebugPortRegister
->UsbAddress
= (UINT8
)(Addr
& 0x7F);
286 DebugPortRegister
->UsbEndPoint
= (UINT8
)(Ep
& 0xF);
289 // Clearing W/R bit to indicate it's a READ operation
291 MmioAnd32((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)~BIT4
);
294 // Setting GO bit as well as clearing DONE bit
296 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)BIT5
);
299 // Wait for completing the request
301 while ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & (UINT32
)BIT16
) == 0) {
302 if ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
303 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
)) {
304 return RETURN_DEVICE_ERROR
;
309 // Clearing DONE bit by writing 1
311 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, BIT16
);
314 // Check if the request is executed successfully or not.
316 if ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & BIT6
) {
317 return RETURN_DEVICE_ERROR
;
321 // Make sure the received data are not beyond the allowable maximum length - 8 byte
323 if (((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & 0xF) > USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
324 return RETURN_DEVICE_ERROR
;
327 *Length
= (UINT8
)(MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & 0xF);
329 return RETURN_DEVICE_ERROR
;
332 for (Index
= 0; Index
< *Length
; Index
++) {
333 Buffer
[Index
] = DebugPortRegister
->DataBuffer
[Index
];
335 return RETURN_SUCCESS
;
339 Do a usb SETUP/OUT transaction by usb debug port.
341 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
342 @param Buffer Pointer to the buffer receiving data.
343 @param Length Number of bytes of the received data.
344 @param Token The token PID for each USB transaction.
345 @param Addr The usb device address for usb transaction.
346 @param Ep The endpoint for usb transaction.
347 @param DataToggle The toggle bit used at usb transaction.
349 @retval RETURN_SUCCESS The IN transaction is executed successfully.
350 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
351 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
357 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
369 return RETURN_INVALID_PARAMETER
;
372 DebugPortRegister
->TokenPid
= Token
;
373 if (DataToggle
!= 0) {
374 DebugPortRegister
->SendPid
= DATA1_PID
;
376 DebugPortRegister
->SendPid
= DATA0_PID
;
378 DebugPortRegister
->UsbAddress
= (UINT8
)(Addr
& 0x7F);
379 DebugPortRegister
->UsbEndPoint
= (UINT8
)(Ep
& 0xF);
382 // Fill in the data length and corresponding data.
384 MmioAnd32((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)~0xF);
385 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, Length
& 0xF);
386 for (Index
= 0; Index
< Length
; Index
++) {
387 DebugPortRegister
->DataBuffer
[Index
] = Buffer
[Index
];
391 // Setting W/R bit to indicate it's a WRITE operation
393 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, BIT4
);
395 // Setting GO bit as well as clearing DONE bit
397 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, BIT5
);
400 // Wait for completing the request
402 while ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & BIT16
) == 0) {
403 if ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
404 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
)) {
405 return RETURN_DEVICE_ERROR
;
410 // Clearing DONE bit by writing 1
412 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, BIT16
);
415 // Check if the request is executed successfully or not.
417 if ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & BIT6
) {
418 return RETURN_DEVICE_ERROR
;
422 // Make sure the sent data are not beyond the allowable maximum length - 8 byte
424 if (((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & 0xF) > USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
425 return RETURN_DEVICE_ERROR
;
428 return RETURN_SUCCESS
;
432 Do a usb control transfer by usb debug port.
434 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
435 @param SetupPacket The token PID for each USB transaction.
436 @param Addr The usb device address for usb transaction.
437 @param Ep The endpoint for usb transaction.
438 @param Data Pointer to the buffer receiving data.
439 @param DataLength Number of bytes of the received data.
441 @retval RETURN_SUCCESS The IN transaction is executed successfully.
442 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
443 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
448 UsbDebugPortControlTransfer (
449 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
450 IN USB_DEVICE_REQUEST
*SetupPacket
,
454 IN OUT UINT8
*DataLength
457 RETURN_STATUS Status
;
459 UINT8 ReturnStatus
[8];
464 Status
= UsbDebugPortOut(DebugPortRegister
, (UINT8
*)SetupPacket
, (UINT8
)sizeof(USB_DEVICE_REQUEST
), SETUP_PID
, Addr
, Ep
, 0);
465 if (RETURN_ERROR(Status
)) {
472 if (DataLength
!= 0) {
473 if ((SetupPacket
->RequestType
& BIT7
) != 0) {
475 // Get Data From Device
477 Status
= UsbDebugPortIn(DebugPortRegister
, Data
, DataLength
, INPUT_PID
, Addr
, Ep
, 1);
478 if (RETURN_ERROR(Status
)) {
483 // Send Data To Device
485 Status
= UsbDebugPortOut(DebugPortRegister
, Data
, *DataLength
, OUTPUT_PID
, Addr
, Ep
, 1);
486 if (RETURN_ERROR(Status
)) {
495 if ((SetupPacket
->RequestType
& BIT7
) != 0) {
497 // For READ operation, Data Toggle in Status Phase Should be 1.
499 Status
= UsbDebugPortOut(DebugPortRegister
, NULL
, 0, OUTPUT_PID
, Addr
, Ep
, 1);
502 // For WRITE operation, Data Toggle in Status Phase Should be 1.
504 Status
= UsbDebugPortIn(DebugPortRegister
, ReturnStatus
, &Temp
, INPUT_PID
, Addr
, Ep
, 1);
511 Check if it needs to re-initialize usb debug port hardware.
513 During different phases switch, such as SEC to PEI or PEI to DXE or DXE to SMM, we should check
514 whether the usb debug port hardware configuration is changed. Such case can be triggerred by
515 Pci bus resource allocation and so on.
517 @param Handle Debug port handle.
519 @retval TRUE The usb debug port hardware configuration is changed.
520 @retval FALSE The usb debug port hardware configuration is not changed.
525 NeedReinitializeHardware(
526 IN USB_DEBUG_PORT_HANDLE
*Handle
530 UINT32 UsbDebugPortMemoryBase
;
531 UINT32 EhciMemoryBase
;
533 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
537 EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
538 if (EhciMemoryBase
!= Handle
->EhciMemoryBase
) {
539 Handle
->EhciMemoryBase
= EhciMemoryBase
;
543 UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
->DebugPortBarNumber
* 4);
544 if (UsbDebugPortMemoryBase
!= Handle
->UsbDebugPortMemoryBase
) {
545 Handle
->UsbDebugPortMemoryBase
= UsbDebugPortMemoryBase
;
550 // Enable Ehci Memory Space Access
552 PciCmd
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_COMMAND_OFFSET
);
553 if (((PciCmd
& EFI_PCI_COMMAND_MEMORY_SPACE
) == 0) || ((PciCmd
& EFI_PCI_COMMAND_BUS_MASTER
) == 0)) {
554 PciCmd
|= EFI_PCI_COMMAND_MEMORY_SPACE
| EFI_PCI_COMMAND_BUS_MASTER
;
555 PciWrite16(PcdGet32(PcdUsbEhciPciAddress
) + PCI_COMMAND_OFFSET
, PciCmd
);
560 // If the owner and in_use bit is not set, it means system is doing cold/warm boot or EHCI host controller is reset by system software.
562 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)(UINTN
)(Handle
->UsbDebugPortMemoryBase
+ Handle
->DebugPortOffset
);
563 if ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_ENABLE
| USB_DEBUG_PORT_IN_USE
))
564 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_ENABLE
| USB_DEBUG_PORT_IN_USE
)) {
568 if (Handle
->Initialized
== USBDBG_RESET
) {
570 } else if (Handle
->Initialized
!= USBDBG_INIT_DONE
) {
577 Initialize usb debug port hardware.
579 1. reset ehci host controller.
580 2. set right port to debug port.
581 3. find a usb debug device is attached by getting debug device descriptor.
582 4. set address for the usb debug device.
583 5. configure the usb debug device to debug mode.
585 @param Handle Debug port handle.
587 @retval TRUE The usb debug port hardware configuration is changed.
588 @retval FALSE The usb debug port hardware configuration is not changed.
593 InitializeUsbDebugHardware (
594 IN USB_DEBUG_PORT_HANDLE
*Handle
597 RETURN_STATUS Status
;
598 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
599 USB_DEBUG_PORT_DESCRIPTOR UsbDebugPortDescriptor
;
605 UINT8 DebugPortNumber
;
608 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)(UINTN
)(Handle
->UsbDebugPortMemoryBase
+ Handle
->DebugPortOffset
);
609 PciCmd
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_COMMAND_OFFSET
);
610 UsbHCSParam
= (UINT32
*)(UINTN
)(Handle
->EhciMemoryBase
+ 0x04);
611 UsbCmd
= (UINT32
*)(UINTN
)(Handle
->EhciMemoryBase
+ 0x20);
612 UsbStatus
= (UINT32
*)(UINTN
)(Handle
->EhciMemoryBase
+ 0x24);
615 // Check if the debug port is enabled and owned by myself.
617 if (((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
))
618 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
)) || (Handle
->Initialized
== USBDBG_RESET
)) {
620 // If the host controller is not halted, then halt it.
622 if ((MmioRead32((UINTN
)UsbStatus
) & BIT12
) == 0) {
623 MmioAnd32((UINTN
)UsbCmd
, (UINT32
)~BIT0
);
624 while ((MmioRead32((UINTN
)UsbStatus
) & BIT12
) == 0);
627 // reset the host controller.
629 MmioOr32((UINTN
)UsbCmd
, BIT1
);
631 // ensure that the host controller is reset.
633 while (MmioRead32((UINTN
)UsbCmd
) & BIT1
);
636 // Start the host controller if it's not running
638 if (MmioRead32((UINTN
)UsbStatus
) & BIT12
) {
639 MmioOr32((UINTN
)UsbCmd
, BIT0
);
640 // ensure that the host controller is started (HALTED bit must be cleared)
641 while (MmioRead32((UINTN
)UsbStatus
) & BIT12
);
645 // First get the ownership of port 0.
647 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
);
649 MicroSecondDelay (200000);
652 // Find out which port is used as debug port.
654 DebugPortNumber
= (UINT8
)((MmioRead32((UINTN
)UsbHCSParam
) & 0x00F00000) >> 20);
656 // Should find a device is connected at debug port
658 PortStatus
= (UINT32
*)(UINTN
)(Handle
->EhciMemoryBase
+ 0x64 + (DebugPortNumber
- 1) * 4);
659 if (!(MmioRead32((UINTN
)PortStatus
) & BIT0
)) {
660 Handle
->Initialized
= USBDBG_NO_DEV
;
661 return RETURN_NOT_FOUND
;
664 if (Handle
->Initialized
!= USBDBG_INIT_DONE
) {
666 // Reset the debug port
668 MmioOr32((UINTN
)PortStatus
, BIT8
);
669 MicroSecondDelay (500000);
670 MmioAnd32((UINTN
)PortStatus
, (UINT32
)~BIT8
);
671 while (MmioRead32((UINTN
)PortStatus
) & BIT8
);
674 // The port enabled bit should be set by HW.
676 if ((MmioRead32((UINTN
)PortStatus
) & BIT2
) == 0) {
677 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
678 return RETURN_DEVICE_ERROR
;
682 // Enable Usb Debug Port Capability
684 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, USB_DEBUG_PORT_ENABLE
);
687 // initialize the data toggle used by bulk in/out endpoint.
689 Handle
->BulkInToggle
= 0;
690 Handle
->BulkOutToggle
= 0;
693 // set usb debug device address as 0x7F.
695 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mSetDebugAddress
, 0x0, 0x0, NULL
, NULL
);
696 if (RETURN_ERROR(Status
)) {
698 // The device can not work well.
700 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
705 // Start to communicate with Usb Debug Device to see if the attached device is usb debug device or not.
707 Length
= (UINT8
)sizeof (USB_DEBUG_PORT_DESCRIPTOR
);
710 // Get debug descriptor.
712 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mGetDebugDescriptor
, 0x7F, 0x0, (UINT8
*)&UsbDebugPortDescriptor
, &Length
);
713 if (RETURN_ERROR(Status
)) {
715 // The device is not a usb debug device.
717 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
721 if (Length
!= sizeof(USB_DEBUG_PORT_DESCRIPTOR
)) {
722 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
723 return RETURN_DEVICE_ERROR
;
727 // enable the usb debug feature.
729 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mSetDebugFeature
, 0x7F, 0x0, NULL
, NULL
);
730 if (RETURN_ERROR(Status
)) {
732 // The device can not work well.
734 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
738 Handle
->Initialized
= USBDBG_DBG_CAB
;
742 // Set initialized flag
744 Handle
->Initialized
= USBDBG_INIT_DONE
;
746 return RETURN_SUCCESS
;
750 Read data from debug device and save the datas in buffer.
752 Reads NumberOfBytes data bytes from a debug device into the buffer
753 specified by Buffer. The number of bytes actually read is returned.
754 If the return value is less than NumberOfBytes, then the rest operation failed.
755 If NumberOfBytes is zero, then return 0.
757 @param Handle Debug port handle.
758 @param Buffer Pointer to the data buffer to store the data read from the debug device.
759 @param NumberOfBytes Number of bytes which will be read.
760 @param Timeout Timeout value for reading from debug device. It unit is Microsecond.
762 @retval 0 Read data failed, no data is to be read.
763 @retval >0 Actual number of bytes read from debug device.
768 DebugPortReadBuffer (
769 IN DEBUG_PORT_HANDLE Handle
,
771 IN UINTN NumberOfBytes
,
775 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
776 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
777 RETURN_STATUS Status
;
784 if (NumberOfBytes
== 0 || Buffer
== NULL
) {
793 // If Handle is NULL, it means memory is ready for use.
794 // Use global variable to store handle value.
796 if (Handle
== NULL
) {
797 UsbDebugPortHandle
= &mUsbDebugPortHandle
;
799 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
802 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
803 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
804 if (RETURN_ERROR(Status
)) {
809 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)(UINTN
)(UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
812 // First read data from buffer, then read debug port hw to get received data.
814 if (UsbDebugPortHandle
->DataCount
> 0) {
815 if (NumberOfBytes
<= UsbDebugPortHandle
->DataCount
) {
816 Total
= NumberOfBytes
;
818 Total
= UsbDebugPortHandle
->DataCount
;
821 for (Index
= 0; Index
< Total
; Index
++) {
822 Buffer
[Index
] = UsbDebugPortHandle
->Data
[Index
];
825 for (Index
= 0; Index
< UsbDebugPortHandle
->DataCount
- Total
; Index
++) {
826 if (Total
+ Index
>= 8) {
829 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortHandle
->Data
[Total
+ Index
];
831 UsbDebugPortHandle
->DataCount
= (UINT8
)(UsbDebugPortHandle
->DataCount
- (UINT8
)Total
);
835 // If Timeout is equal to 0, then it means it should always wait until all datum required are received.
838 Timeout
= 0xFFFFFFFF;
842 // Read remaining data by executing one or more usb debug transfer transactions at usb debug port hw.
844 while ((Total
< NumberOfBytes
) && (Timeout
!= 0)) {
845 Remaining
= NumberOfBytes
- Total
;
846 if (Remaining
>= USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
847 Status
= UsbDebugPortIn(UsbDebugPortRegister
, Buffer
+ Total
, &Received
, INPUT_PID
, 0x7f, 0x82, UsbDebugPortHandle
->BulkInToggle
);
849 if (RETURN_ERROR(Status
)) {
853 Status
= UsbDebugPortIn(UsbDebugPortRegister
, &UsbDebugPortHandle
->Data
[0], &Received
, INPUT_PID
, 0x7f, 0x82, UsbDebugPortHandle
->BulkInToggle
);
855 if (RETURN_ERROR(Status
)) {
859 UsbDebugPortHandle
->DataCount
= Received
;
861 if (Remaining
<= Received
) {
862 Length
= (UINT8
)Remaining
;
864 Length
= (UINT8
)Received
;
868 // Copy required data from the data buffer to user buffer.
870 for (Index
= 0; Index
< Length
; Index
++) {
871 (Buffer
+ Total
)[Index
] = UsbDebugPortHandle
->Data
[Index
];
872 UsbDebugPortHandle
->DataCount
--;
876 // reorder the data buffer to make available data arranged from the beginning of the data buffer.
878 for (Index
= 0; Index
< Received
- Length
; Index
++) {
879 if (Length
+ Index
>= 8) {
882 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortHandle
->Data
[Length
+ Index
];
885 // fixup the real received length in Buffer.
889 UsbDebugPortHandle
->BulkInToggle
^= 1;
899 Write data from buffer to debug device.
901 Writes NumberOfBytes data bytes from Buffer to the debug device.
902 The number of bytes actually written to the debug device is returned.
903 If the return value is less than NumberOfBytes, then the write operation failed.
904 If NumberOfBytes is zero, then return 0.
906 @param Handle Debug port handle.
907 @param Buffer Pointer to the data buffer to be written.
908 @param NumberOfBytes Number of bytes to written to the debug device.
910 @retval 0 NumberOfBytes is 0.
911 @retval >0 The number of bytes written to the debug device.
912 If this value is less than NumberOfBytes, then the read operation failed.
917 DebugPortWriteBuffer (
918 IN DEBUG_PORT_HANDLE Handle
,
920 IN UINTN NumberOfBytes
923 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
924 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
925 RETURN_STATUS Status
;
930 if (NumberOfBytes
== 0 || Buffer
== NULL
) {
938 // If Handle is NULL, it means memory is ready for use.
939 // Use global variable to store handle value.
941 if (Handle
== NULL
) {
942 UsbDebugPortHandle
= &mUsbDebugPortHandle
;
944 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
947 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
948 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
949 if (RETURN_ERROR(Status
)) {
954 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)(UINTN
)(UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
956 while ((Total
< NumberOfBytes
)) {
957 if (NumberOfBytes
- Total
> USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
958 Sent
= USB_DEBUG_PORT_MAX_PACKET_SIZE
;
960 Sent
= (UINT8
)(NumberOfBytes
- Total
);
963 Status
= UsbDebugPortOut(UsbDebugPortRegister
, Buffer
+ Total
, Sent
, OUTPUT_PID
, 0x7F, 0x01, UsbDebugPortHandle
->BulkOutToggle
);
965 if (RETURN_ERROR(Status
)) {
969 ReceivedPid
= (MmioRead8((UINTN
)&UsbDebugPortRegister
->ReceivedPid
));
971 // If received a NAK_PID on write transaction, it means the usb debug device is busy and can not handle this transaction.
972 // should send the packet again.
974 if (ReceivedPid
== NAK_PID
) {
977 UsbDebugPortHandle
->BulkOutToggle
^= 1;
985 Polls a debug device to see if there is any data waiting to be read.
987 Polls a debug device to see if there is any data waiting to be read.
988 If there is data waiting to be read from the debug device, then TRUE is returned.
989 If there is no data waiting to be read from the debug device, then FALSE is returned.
991 @param Handle Debug port handle.
993 @retval TRUE Data is waiting to be read from the debug device.
994 @retval FALSE There is no data waiting to be read from the serial device.
999 DebugPortPollBuffer (
1000 IN DEBUG_PORT_HANDLE Handle
1003 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
1004 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
1007 RETURN_STATUS Status
;
1010 // If Handle is NULL, it means memory is ready for use.
1011 // Use global variable to store handle value.
1013 if (Handle
== NULL
) {
1014 UsbDebugPortHandle
= &mUsbDebugPortHandle
;
1016 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
1019 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
1020 Status
= InitializeUsbDebugHardware(UsbDebugPortHandle
);
1021 if (RETURN_ERROR(Status
)) {
1027 // If the data buffer is not empty, then return TRUE directly.
1028 // else initialize a usb read transaction and read data to the data buffer.
1030 if (UsbDebugPortHandle
->DataCount
!= 0) {
1034 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)(UINTN
)(UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
1036 UsbDebugPortRegister
->TokenPid
= INPUT_PID
;
1037 if (UsbDebugPortHandle
->BulkInToggle
== 0) {
1038 UsbDebugPortRegister
->SendPid
= DATA0_PID
;
1040 UsbDebugPortRegister
->SendPid
= DATA1_PID
;
1042 UsbDebugPortRegister
->UsbAddress
= 0x7F;
1043 UsbDebugPortRegister
->UsbEndPoint
= 0x82 & 0x0F;
1046 // Clearing W/R bit to indicate it's a READ operation
1048 MmioAnd32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, (UINT32
)~BIT4
);
1050 // Setting GO bit as well as clearing DONE bit
1052 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, (UINT32
)BIT5
);
1055 // Wait for completing the request
1057 while ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (UINT32
)BIT16
) == 0) {
1058 if ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
1059 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
)) {
1064 if ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
)) & BIT6
) {
1068 Length
= (UINT8
)(MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & 0xF);
1074 UsbDebugPortHandle
->BulkInToggle
^= 1;
1080 for (Index
= 0; Index
< Length
; Index
++) {
1081 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortRegister
->DataBuffer
[Index
];
1083 UsbDebugPortHandle
->DataCount
= Length
;
1089 Initialize the debug port.
1091 If Function is not NULL, Debug Communication Libary will call this function
1092 by passing in the Context to be the first parameter. If needed, Debug Communication
1093 Library will create one debug port handle to be the second argument passing in
1094 calling the Function, otherwise it will pass NULL to be the second argument of
1097 If Function is NULL, and Context is not NULL, the Debug Communication Library could
1098 a) Return the same handle as passed in (as Context parameter).
1099 b) Ignore the input Context parameter and create new hanlde to be returned.
1101 If parameter Function is NULL and Context is NULL, Debug Communication Library could
1102 created a new handle if needed and return it, otherwise it will return NULL.
1104 @param[in] Context Context needed by callback function; it was optional.
1105 @param[in] Function Continue function called by Debug Communication library;
1108 @return The debug port handle created by Debug Communication Library if Function
1114 DebugPortInitialize (
1116 IN DEBUG_PORT_CONTINUE Function
1119 RETURN_STATUS Status
;
1120 USB_DEBUG_PORT_HANDLE Handle
;
1121
if (Function
== NULL
&& Context
!= NULL
) {
1122 return (DEBUG_PORT_HANDLE
*) Context
;
1125 ZeroMem(&Handle
, sizeof (USB_DEBUG_PORT_HANDLE
));
1127 Status
= CalculateUsbDebugPortBar(&Handle
.DebugPortOffset
, &Handle
.DebugPortBarNumber
);
1128 if (RETURN_ERROR (Status
)) {
1129 DEBUG ((EFI_D_ERROR
, "UsbDbg: the pci device pointed by PcdUsbEhciPciAddress is not EHCI host controller or does not support debug port capability!\n"));
1133 Handle
.EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
1135 if (Handle
.EhciMemoryBase
== 0) {
1137 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
1139 PciWrite32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
, PcdGet32(PcdUsbEhciMemorySpaceBase
));
1140 Handle
.EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
1143 Handle
.UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4);
1145 if (Handle
.UsbDebugPortMemoryBase
== 0) {
1147 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
1149 PciWrite32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4, PcdGet32(PcdUsbDebugPortMemorySpaceBase
));
1150 Handle
.UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4);
1153 Handle
.Initialized
= USBDBG_RESET
;
1155 if (NeedReinitializeHardware(&Handle
)) {
1156 DEBUG ((EFI_D_ERROR
, "UsbDbg: Start EHCI debug port initialization!\n"));
1157 Status
= InitializeUsbDebugHardware (&Handle
);
1158 if (RETURN_ERROR(Status
)) {
1159 DEBUG ((EFI_D_ERROR
, "UsbDbg: Initialization failed, please check if USB debug cable is plugged into EHCI debug port correctly!\n"));
1166 if (Function
!= NULL
) {
1167 Function (Context
, &Handle
);
1169 CopyMem(&mUsbDebugPortHandle
, &Handle
, sizeof (USB_DEBUG_PORT_HANDLE
));
1172 return (DEBUG_PORT_HANDLE
)(UINTN
)&mUsbDebugPortHandle
;