2 Debug Port Library implementation based on usb debug port.
4 Copyright (c) 2010 - 2017, 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 mDebugCommunicationLibUsbGetDebugDescriptor
= {
63 USB_REQ_GET_DESCRIPTOR
,
66 sizeof(USB_DEBUG_PORT_DESCRIPTOR
)
69 USB_DEVICE_REQUEST mDebugCommunicationLibUsbSetDebugFeature
= {
77 USB_DEVICE_REQUEST mDebugCommunicationLibUsbSetDebugAddress
= {
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 mDebugCommunicationLibUsbDebugPortHandle
;
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 triggered 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
;
604 UINT8 DebugPortNumber
;
607 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)((UINTN
)Handle
->UsbDebugPortMemoryBase
+ Handle
->DebugPortOffset
);
608 UsbHCSParam
= (UINT32
*)((UINTN
)Handle
->EhciMemoryBase
+ 0x04);
609 UsbCmd
= (UINT32
*)((UINTN
)Handle
->EhciMemoryBase
+ 0x20);
610 UsbStatus
= (UINT32
*)((UINTN
)Handle
->EhciMemoryBase
+ 0x24);
613 // Check if the debug port is enabled and owned by myself.
615 if (((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
))
616 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
)) || (Handle
->Initialized
== USBDBG_RESET
)) {
619 "UsbDbg: Need to reset the host controller. ControlStatus = %08x\n",
620 MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
)
623 // If the host controller is halted, then reset and restart it.
625 if ((MmioRead32((UINTN
)UsbStatus
) & BIT12
) != 0) {
626 DEBUG ((EFI_D_INFO
, "UsbDbg: Reset the host controller.\n"));
628 // reset the host controller.
630 MmioOr32((UINTN
)UsbCmd
, BIT1
);
632 // ensure that the host controller is reset.
634 while ((MmioRead32((UINTN
)UsbCmd
) & BIT1
) != 0);
636 MmioOr32((UINTN
)UsbCmd
, BIT0
);
637 // ensure that the host controller is started (HALTED bit must be cleared)
638 while ((MmioRead32((UINTN
)UsbStatus
) & BIT12
) != 0);
642 // First get the ownership of port 0.
644 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
);
646 MicroSecondDelay (200000);
649 // Find out which port is used as debug port.
651 DebugPortNumber
= (UINT8
)((MmioRead32((UINTN
)UsbHCSParam
) & 0x00F00000) >> 20);
653 // Should find a device is connected at debug port
655 PortStatus
= (UINT32
*)((UINTN
)Handle
->EhciMemoryBase
+ 0x64 + (DebugPortNumber
- 1) * 4);
656 if (!(MmioRead32((UINTN
)PortStatus
) & BIT0
)) {
657 Handle
->Initialized
= USBDBG_NO_DEV
;
658 return RETURN_NOT_FOUND
;
661 if (Handle
->Initialized
!= USBDBG_INIT_DONE
||
662 (MmioRead32 ((UINTN
) &UsbDebugPortRegister
->ControlStatus
) & USB_DEBUG_PORT_ENABLE
) == 0) {
663 DEBUG ((EFI_D_INFO
, "UsbDbg: Reset the debug port.\n"));
665 // Reset the debug port
667 MmioOr32((UINTN
)PortStatus
, BIT8
);
668 MicroSecondDelay (500000);
669 MmioAnd32((UINTN
)PortStatus
, (UINT32
)~BIT8
);
670 while (MmioRead32((UINTN
)PortStatus
) & BIT8
);
673 // The port enabled bit should be set by HW.
675 if ((MmioRead32((UINTN
)PortStatus
) & BIT2
) == 0) {
676 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
677 return RETURN_DEVICE_ERROR
;
681 // Enable Usb Debug Port Capability
683 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, USB_DEBUG_PORT_ENABLE
);
686 // initialize the data toggle used by bulk in/out endpoint.
688 Handle
->BulkInToggle
= 0;
689 Handle
->BulkOutToggle
= 0;
692 // set usb debug device address as 0x7F.
694 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mDebugCommunicationLibUsbSetDebugAddress
, 0x0, 0x0, NULL
, NULL
);
695 if (RETURN_ERROR(Status
)) {
697 // The device can not work well.
699 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
704 // Start to communicate with Usb Debug Device to see if the attached device is usb debug device or not.
706 Length
= (UINT8
)sizeof (USB_DEBUG_PORT_DESCRIPTOR
);
709 // Get debug descriptor.
711 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mDebugCommunicationLibUsbGetDebugDescriptor
, 0x7F, 0x0, (UINT8
*)&UsbDebugPortDescriptor
, &Length
);
712 if (RETURN_ERROR(Status
)) {
714 // The device is not a usb debug device.
716 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
720 if (Length
!= sizeof(USB_DEBUG_PORT_DESCRIPTOR
)) {
721 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
722 return RETURN_DEVICE_ERROR
;
726 // enable the usb debug feature.
728 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mDebugCommunicationLibUsbSetDebugFeature
, 0x7F, 0x0, NULL
, NULL
);
729 if (RETURN_ERROR(Status
)) {
731 // The device can not work well.
733 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
737 Handle
->Initialized
= USBDBG_DBG_CAB
;
741 // Set initialized flag
743 Handle
->Initialized
= USBDBG_INIT_DONE
;
745 return RETURN_SUCCESS
;
749 Read data from debug device and save the datas in buffer.
751 Reads NumberOfBytes data bytes from a debug device into the buffer
752 specified by Buffer. The number of bytes actually read is returned.
753 If the return value is less than NumberOfBytes, then the rest operation failed.
754 If NumberOfBytes is zero, then return 0.
756 @param Handle Debug port handle.
757 @param Buffer Pointer to the data buffer to store the data read from the debug device.
758 @param NumberOfBytes Number of bytes which will be read.
759 @param Timeout Timeout value for reading from debug device. It unit is Microsecond.
761 @retval 0 Read data failed, no data is to be read.
762 @retval >0 Actual number of bytes read from debug device.
767 DebugPortReadBuffer (
768 IN DEBUG_PORT_HANDLE Handle
,
770 IN UINTN NumberOfBytes
,
774 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
775 RETURN_STATUS Status
;
778 if (NumberOfBytes
!= 1 || Buffer
== NULL
|| Timeout
!= 0) {
783 // If Handle is NULL, it means memory is ready for use.
784 // Use global variable to store handle value.
786 if (Handle
== NULL
) {
787 UsbDebugPortHandle
= &mDebugCommunicationLibUsbDebugPortHandle
;
789 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
792 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
793 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
794 if (RETURN_ERROR(Status
)) {
800 // Read data from buffer
802 if (UsbDebugPortHandle
->DataCount
< 1) {
805 *Buffer
= UsbDebugPortHandle
->Data
[0];
806 for (Index
= 0; Index
< UsbDebugPortHandle
->DataCount
- 1; Index
++) {
807 if ((Index
+ 1) >= USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
810 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortHandle
->Data
[Index
+ 1];
812 UsbDebugPortHandle
->DataCount
= (UINT8
)(UsbDebugPortHandle
->DataCount
- 1);
818 Write data from buffer to debug device.
820 Writes NumberOfBytes data bytes from Buffer to the debug device.
821 The number of bytes actually written to the debug device is returned.
822 If the return value is less than NumberOfBytes, then the write operation failed.
823 If NumberOfBytes is zero, then return 0.
825 @param Handle Debug port handle.
826 @param Buffer Pointer to the data buffer to be written.
827 @param NumberOfBytes Number of bytes to written to the debug device.
829 @retval 0 NumberOfBytes is 0.
830 @retval >0 The number of bytes written to the debug device.
831 If this value is less than NumberOfBytes, then the read operation failed.
836 DebugPortWriteBuffer (
837 IN DEBUG_PORT_HANDLE Handle
,
839 IN UINTN NumberOfBytes
842 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
843 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
844 RETURN_STATUS Status
;
849 if (NumberOfBytes
== 0 || Buffer
== NULL
) {
857 // If Handle is NULL, it means memory is ready for use.
858 // Use global variable to store handle value.
860 if (Handle
== NULL
) {
861 UsbDebugPortHandle
= &mDebugCommunicationLibUsbDebugPortHandle
;
863 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
866 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
867 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
868 if (RETURN_ERROR(Status
)) {
873 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)((UINTN
)UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
875 while ((Total
< NumberOfBytes
)) {
876 if (NumberOfBytes
- Total
> USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
877 Sent
= USB_DEBUG_PORT_MAX_PACKET_SIZE
;
879 Sent
= (UINT8
)(NumberOfBytes
- Total
);
882 Status
= UsbDebugPortOut(UsbDebugPortRegister
, Buffer
+ Total
, Sent
, OUTPUT_PID
, 0x7F, 0x01, UsbDebugPortHandle
->BulkOutToggle
);
884 if (RETURN_ERROR(Status
)) {
888 ReceivedPid
= (MmioRead8((UINTN
)&UsbDebugPortRegister
->ReceivedPid
));
890 // If received a NAK_PID on write transaction, it means the usb debug device is busy and can not handle this transaction.
891 // should send the packet again.
893 if (ReceivedPid
== NAK_PID
) {
896 UsbDebugPortHandle
->BulkOutToggle
^= 1;
904 Polls a debug device to see if there is any data waiting to be read.
906 Polls a debug device to see if there is any data waiting to be read.
907 If there is data waiting to be read from the debug device, then TRUE is returned.
908 If there is no data waiting to be read from the debug device, then FALSE is returned.
910 @param Handle Debug port handle.
912 @retval TRUE Data is waiting to be read from the debug device.
913 @retval FALSE There is no data waiting to be read from the serial device.
918 DebugPortPollBuffer (
919 IN DEBUG_PORT_HANDLE Handle
922 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
923 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
926 RETURN_STATUS Status
;
929 // If Handle is NULL, it means memory is ready for use.
930 // Use global variable to store handle value.
932 if (Handle
== NULL
) {
933 UsbDebugPortHandle
= &mDebugCommunicationLibUsbDebugPortHandle
;
935 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
938 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
939 Status
= InitializeUsbDebugHardware(UsbDebugPortHandle
);
940 if (RETURN_ERROR(Status
)) {
946 // If the data buffer is not empty, then return TRUE directly.
947 // else initialize a usb read transaction and read data to the data buffer.
949 if (UsbDebugPortHandle
->DataCount
!= 0) {
953 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)((UINTN
)UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
955 UsbDebugPortRegister
->TokenPid
= INPUT_PID
;
956 if (UsbDebugPortHandle
->BulkInToggle
== 0) {
957 UsbDebugPortRegister
->SendPid
= DATA0_PID
;
959 UsbDebugPortRegister
->SendPid
= DATA1_PID
;
961 UsbDebugPortRegister
->UsbAddress
= 0x7F;
962 UsbDebugPortRegister
->UsbEndPoint
= 0x82 & 0x0F;
965 // Clearing W/R bit to indicate it's a READ operation
967 MmioAnd32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, (UINT32
)~BIT4
);
969 // Setting GO bit as well as clearing DONE bit
971 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, (UINT32
)BIT5
);
974 // Wait for completing the request
976 while ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (UINT32
)BIT16
) == 0) {
977 if ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
978 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
)) {
983 if ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
)) & BIT6
) {
987 Length
= (UINT8
)(MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & 0xF);
993 UsbDebugPortHandle
->BulkInToggle
^= 1;
999 for (Index
= 0; Index
< Length
; Index
++) {
1000 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortRegister
->DataBuffer
[Index
];
1002 UsbDebugPortHandle
->DataCount
= Length
;
1008 Initialize the debug port.
1010 If Function is not NULL, Debug Communication Library will call this function
1011 by passing in the Context to be the first parameter. If needed, Debug Communication
1012 Library will create one debug port handle to be the second argument passing in
1013 calling the Function, otherwise it will pass NULL to be the second argument of
1016 If Function is NULL, and Context is not NULL, the Debug Communication Library could
1017 a) Return the same handle as passed in (as Context parameter).
1018 b) Ignore the input Context parameter and create new handle to be returned.
1020 If parameter Function is NULL and Context is NULL, Debug Communication Library could
1021 created a new handle if needed and return it, otherwise it will return NULL.
1023 @param[in] Context Context needed by callback function; it was optional.
1024 @param[in] Function Continue function called by Debug Communication library;
1027 @return The debug port handle created by Debug Communication Library if Function
1033 DebugPortInitialize (
1035 IN DEBUG_PORT_CONTINUE Function
1038 RETURN_STATUS Status
;
1039 USB_DEBUG_PORT_HANDLE Handle
;
1042 // Validate the PCD PcdDebugPortHandleBufferSize value
1044 ASSERT (PcdGet16 (PcdDebugPortHandleBufferSize
) == sizeof (USB_DEBUG_PORT_HANDLE
));
1046 if (Function
== NULL
&& Context
!= NULL
) {
1047 return (DEBUG_PORT_HANDLE
*) Context
;
1049 ZeroMem(&Handle
, sizeof (USB_DEBUG_PORT_HANDLE
));
1051 Status
= CalculateUsbDebugPortBar(&Handle
.DebugPortOffset
, &Handle
.DebugPortBarNumber
);
1052 if (RETURN_ERROR (Status
)) {
1053 DEBUG ((EFI_D_ERROR
, "UsbDbg: the pci device pointed by PcdUsbEhciPciAddress is not EHCI host controller or does not support debug port capability!\n"));
1057 Handle
.EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
1059 if (Handle
.EhciMemoryBase
== 0) {
1061 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
1063 PciWrite32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
, PcdGet32(PcdUsbEhciMemorySpaceBase
));
1064 Handle
.EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
1067 Handle
.UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4);
1069 if (Handle
.UsbDebugPortMemoryBase
== 0) {
1071 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
1073 PciWrite32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4, PcdGet32(PcdUsbDebugPortMemorySpaceBase
));
1074 Handle
.UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4);
1077 Handle
.Initialized
= USBDBG_RESET
;
1079 if (NeedReinitializeHardware(&Handle
)) {
1080 DEBUG ((EFI_D_ERROR
, "UsbDbg: Start EHCI debug port initialization!\n"));
1081 Status
= InitializeUsbDebugHardware (&Handle
);
1082 if (RETURN_ERROR(Status
)) {
1083 DEBUG ((EFI_D_ERROR
, "UsbDbg: Failed, please check if USB debug cable is plugged into EHCI debug port correctly!\n"));
1090 if (Function
!= NULL
) {
1091 Function (Context
, &Handle
);
1093 CopyMem(&mDebugCommunicationLibUsbDebugPortHandle
, &Handle
, sizeof (USB_DEBUG_PORT_HANDLE
));
1096 return (DEBUG_PORT_HANDLE
)(UINTN
)&mDebugCommunicationLibUsbDebugPortHandle
;