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
;
104 // The internal data structure of DEBUG_PORT_HANDLE, which stores some
105 // important datum which are used across various phases.
107 typedef struct _USB_DEBUG_PORT_HANDLE
{
109 // The usb debug port memory BAR number in EHCI configuration space.
111 UINT8 DebugPortBarNumber
;
114 // The offset of usb debug port registers in EHCI memory range.
116 UINT16 DebugPortOffset
;
118 // The usb debug port memory BAR address.
120 UINTN UsbDebugPortMemoryBase
;
122 // The EHCI memory BAR address.
124 UINTN EhciMemoryBase
;
126 // The Bulk In endpoint toggle bit.
130 // The Bulk Out endpoint toggle bit.
134 // The available data length in the following data buffer.
138 // The data buffer. Maximum length is 8 bytes.
141 } USB_DEBUG_PORT_HANDLE
;
145 // The global variable which can be used after memory is ready.
147 USB_DEBUG_PORT_HANDLE mUsbDebugPortHandle
;
150 Calculate the usb debug port bar address.
152 @param DebugPortOffset Get usb debug port offset in the usb debug port memory space.
153 @param DebugPortBarNumbar Get the bar number at which usb debug port is located.
155 @retval RETURN_UNSUPPORTED The usb host controller does not supported usb debug port capability.
156 @retval RETURN_SUCCESS Get bar and offset successfully.
161 CalculateUsbDebugPortBar (
162 OUT UINT16
*DebugPortOffset
,
163 OUT UINT8
*DebugPortBarNumbar
175 VendorId
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_VENDOR_ID_OFFSET
);
176 DeviceId
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_DEVICE_ID_OFFSET
);
178 if ((VendorId
== 0xFFFF) || (DeviceId
== 0xFFFF)) {
179 return RETURN_UNSUPPORTED
;
182 ProgInterface
= PciRead8 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_CLASSCODE_OFFSET
);
183 SubClassCode
= PciRead8 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_CLASSCODE_OFFSET
+ 1);
184 BaseCode
= PciRead8 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_CLASSCODE_OFFSET
+ 2);
186 if ((ProgInterface
!= PCI_IF_EHCI
) || (SubClassCode
!= PCI_CLASS_SERIAL_USB
) || (BaseCode
!= PCI_CLASS_SERIAL
)) {
187 return RETURN_UNSUPPORTED
;
191 // Enable Ehci Host Controller MMIO Space.
193 PciStatus
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_PRIMARY_STATUS_OFFSET
);
195 if ((PciStatus
& EFI_PCI_STATUS_CAPABILITY
) == 0) {
197 // The Pci Device Doesn't Support Capability Pointer.
199 return RETURN_UNSUPPORTED
;
203 // Get Pointer To Capability List
205 CapabilityPtr
= PciRead8(PcdGet32(PcdUsbEhciPciAddress
) + PCI_CAPBILITY_POINTER_OFFSET
);
208 // Find Capability ID 0xA, Which Is For Debug Port
210 while (CapabilityPtr
!= 0) {
211 CapabilityId
= PciRead8(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
);
212 if (CapabilityId
== PCI_CAPABILITY_ID_DEBUG_PORT
) {
215 CapabilityPtr
= PciRead8(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
+ 1);
219 // No Debug Port Capability Found
221 if (CapabilityPtr
== 0) {
222 return RETURN_UNSUPPORTED
;
226 // Get The Base Address Of Debug Port Register In Debug Port Capability Register
228 *DebugPortOffset
= (UINT16
)(PciRead16(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
+ 2) & 0x1FFF);
229 *DebugPortBarNumbar
= (UINT8
)((PciRead16(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
+ 2) >> 13) - 1);
231 return RETURN_SUCCESS
;
235 Do a usb IN transaction by usb debug port.
237 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
238 @param Buffer Pointer to the buffer receiving data.
239 @param Length Number of bytes of the received data.
240 @param Token The token PID for each USB transaction.
241 @param Addr The usb device address for usb transaction.
242 @param Ep The endpoint for usb transaction.
243 @param DataToggle The toggle bit used at usb transaction.
245 @retval RETURN_SUCCESS The IN transaction is executed successfully.
246 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
247 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
253 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
254 IN OUT UINT8
*Buffer
,
264 if (Length
== NULL
) {
265 return RETURN_INVALID_PARAMETER
;
269 DebugPortRegister
->TokenPid
= Token
;
270 if (DataToggle
!= 0) {
271 DebugPortRegister
->SendPid
= DATA1_PID
;
273 DebugPortRegister
->SendPid
= DATA0_PID
;
276 DebugPortRegister
->UsbAddress
= (UINT8
)(Addr
& 0x7F);
277 DebugPortRegister
->UsbEndPoint
= (UINT8
)(Ep
& 0xF);
280 // Clearing W/R bit to indicate it's a READ operation
282 MmioAnd32((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)~BIT4
);
285 // Setting GO bit as well as clearing DONE bit
287 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)BIT5
);
290 // Wait for completing the request
292 while ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & (UINT32
)BIT16
) == 0);
295 // Check if the request is executed successfully or not.
297 if ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & BIT6
) {
298 return RETURN_DEVICE_ERROR
;
302 // Make sure the received data are not beyond the allowable maximum length - 8 byte
304 if (((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & 0xF) > USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
305 return RETURN_DEVICE_ERROR
;
308 *Length
= (UINT8
)(MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & 0xF);
310 return RETURN_DEVICE_ERROR
;
313 for (Index
= 0; Index
< *Length
; Index
++) {
314 Buffer
[Index
] = DebugPortRegister
->DataBuffer
[Index
];
316 return RETURN_SUCCESS
;
320 Do a usb SETUP/OUT transaction by usb debug port.
322 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
323 @param Buffer Pointer to the buffer receiving data.
324 @param Length Number of bytes of the received data.
325 @param Token The token PID for each USB transaction.
326 @param Addr The usb device address for usb transaction.
327 @param Ep The endpoint for usb transaction.
328 @param DataToggle The toggle bit used at usb transaction.
330 @retval RETURN_SUCCESS The IN transaction is executed successfully.
331 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
332 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
338 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
350 return RETURN_INVALID_PARAMETER
;
353 DebugPortRegister
->TokenPid
= Token
;
354 if (DataToggle
!= 0) {
355 DebugPortRegister
->SendPid
= DATA1_PID
;
357 DebugPortRegister
->SendPid
= DATA0_PID
;
359 DebugPortRegister
->UsbAddress
= (UINT8
)(Addr
& 0x7F);
360 DebugPortRegister
->UsbEndPoint
= (UINT8
)(Ep
& 0xF);
363 // Fill in the data length and corresponding data.
365 MmioAnd32((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)~0xF);
366 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, Length
& 0xF);
367 for (Index
= 0; Index
< Length
; Index
++) {
368 DebugPortRegister
->DataBuffer
[Index
] = Buffer
[Index
];
372 // Setting W/R bit to indicate it's a WRITE operation
374 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, BIT4
);
376 // Setting GO bit as well as clearing DONE bit
378 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, BIT5
);
381 // Wait for completing the request
383 while ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & BIT16
) == 0);
386 // Check if the request is executed successfully or not.
388 if ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & BIT6
) {
389 return RETURN_DEVICE_ERROR
;
393 // Make sure the sent data are not beyond the allowable maximum length - 8 byte
395 if (((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & 0xF) > USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
396 return RETURN_DEVICE_ERROR
;
399 return RETURN_SUCCESS
;
403 Do a usb control transfer by usb debug port.
405 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
406 @param SetupPacket The token PID for each USB transaction.
407 @param Addr The usb device address for usb transaction.
408 @param Ep The endpoint for usb transaction.
409 @param Data Pointer to the buffer receiving data.
410 @param DataLength Number of bytes of the received data.
412 @retval RETURN_SUCCESS The IN transaction is executed successfully.
413 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
414 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
419 UsbDebugPortControlTransfer (
420 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
421 IN USB_DEVICE_REQUEST
*SetupPacket
,
425 IN OUT UINT8
*DataLength
428 RETURN_STATUS Status
;
430 UINT8 ReturnStatus
[8];
435 Status
= UsbDebugPortOut(DebugPortRegister
, (UINT8
*)SetupPacket
, (UINT8
)sizeof(USB_DEVICE_REQUEST
), SETUP_PID
, Addr
, Ep
, 0);
436 if (RETURN_ERROR(Status
)) {
443 if (DataLength
!= 0) {
444 if ((SetupPacket
->RequestType
& BIT7
) != 0) {
446 // Get Data From Device
448 Status
= UsbDebugPortIn(DebugPortRegister
, Data
, DataLength
, INPUT_PID
, Addr
, Ep
, 1);
449 if (RETURN_ERROR(Status
)) {
454 // Send Data To Device
456 Status
= UsbDebugPortOut(DebugPortRegister
, Data
, *DataLength
, OUTPUT_PID
, Addr
, Ep
, 1);
457 if (RETURN_ERROR(Status
)) {
466 if ((SetupPacket
->RequestType
& BIT7
) != 0) {
468 // For READ operation, Data Toggle in Status Phase Should be 1.
470 Status
= UsbDebugPortOut(DebugPortRegister
, NULL
, 0, OUTPUT_PID
, Addr
, Ep
, 1);
473 // For WRITE operation, Data Toggle in Status Phase Should be 1.
475 Status
= UsbDebugPortIn(DebugPortRegister
, ReturnStatus
, &Temp
, INPUT_PID
, Addr
, Ep
, 1);
482 Check if it needs to re-initialize usb debug port hardware.
484 During different phases switch, such as SEC to PEI or PEI to DXE or DXE to SMM, we should check
485 whether the usb debug port hardware configuration is changed. Such case can be triggerred by
486 Pci bus resource allocation and so on.
488 @param Handle Debug port handle.
490 @retval TRUE The usb debug port hardware configuration is changed.
491 @retval FALSE The usb debug port hardware configuration is not changed.
496 NeedReinitializeHardware(
497 IN USB_DEBUG_PORT_HANDLE
*Handle
501 UINTN UsbDebugPortMemoryBase
;
502 UINTN EhciMemoryBase
;
504 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
508 EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
509 if (EhciMemoryBase
!= Handle
->EhciMemoryBase
) {
510 Handle
->EhciMemoryBase
= EhciMemoryBase
;
514 UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
->DebugPortBarNumber
* 4);
515 if (UsbDebugPortMemoryBase
!= Handle
->UsbDebugPortMemoryBase
) {
516 Handle
->UsbDebugPortMemoryBase
= UsbDebugPortMemoryBase
;
521 // Enable Ehci Memory Space Access
523 PciCmd
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_COMMAND_OFFSET
);
524 if (((PciCmd
& EFI_PCI_COMMAND_MEMORY_SPACE
) == 0) || ((PciCmd
& EFI_PCI_COMMAND_BUS_MASTER
) == 0)) {
529 // Check if the debug port is enabled and owned by myself.
531 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)(Handle
->UsbDebugPortMemoryBase
+ Handle
->DebugPortOffset
);
532 if ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) &
533 (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_ENABLE
| USB_DEBUG_PORT_IN_USE
)) == 0) {
540 Initialize usb debug port hardware.
542 1. reset ehci host controller.
543 2. set right port to debug port.
544 3. find a usb debug device is attached by getting debug device descriptor.
545 4. set address for the usb debug device.
546 5. configure the usb debug device to debug mode.
548 @param Handle Debug port handle.
550 @retval TRUE The usb debug port hardware configuration is changed.
551 @retval FALSE The usb debug port hardware configuration is not changed.
556 InitializeUsbDebugHardware (
557 IN USB_DEBUG_PORT_HANDLE
*Handle
560 RETURN_STATUS Status
;
561 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
562 USB_DEBUG_PORT_DESCRIPTOR UsbDebugPortDescriptor
;
568 UINT8 DebugPortNumber
;
571 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)(Handle
->UsbDebugPortMemoryBase
+ Handle
->DebugPortOffset
);
572 PciCmd
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_COMMAND_OFFSET
);
573 UsbHCSParam
= (UINT32
*)(Handle
->EhciMemoryBase
+ 0x04);
574 UsbCmd
= (UINT32
*)(Handle
->EhciMemoryBase
+ 0x20);
575 UsbStatus
= (UINT32
*)(Handle
->EhciMemoryBase
+ 0x24);
578 // initialize the data toggle used by bulk in/out endpoint.
580 Handle
->BulkInToggle
= 0;
581 Handle
->BulkOutToggle
= 0;
584 // Enable Ehci Memory Space Access
586 if (((PciCmd
& EFI_PCI_COMMAND_MEMORY_SPACE
) == 0) || ((PciCmd
& EFI_PCI_COMMAND_BUS_MASTER
) == 0)) {
587 PciCmd
|= EFI_PCI_COMMAND_MEMORY_SPACE
| EFI_PCI_COMMAND_BUS_MASTER
;
588 PciWrite16(PcdGet32(PcdUsbEhciPciAddress
) + PCI_COMMAND_OFFSET
, PciCmd
);
592 // If the host controller is not halted, then halt it.
594 if ((MmioRead32((UINTN
)UsbStatus
) & BIT12
) == 0) {
595 MmioAnd32((UINTN
)UsbCmd
, (UINT32
)~BIT0
);
596 while ((MmioRead32((UINTN
)UsbStatus
) & BIT12
) == 0);
599 // reset the host controller.
601 MmioOr32((UINTN
)UsbCmd
, BIT1
);
603 // ensure that the host controller is reset.
605 while (MmioRead32((UINTN
)UsbCmd
) & BIT1
);
608 // Start the host controller if it's not running
610 if (MmioRead32((UINTN
)UsbStatus
) & BIT12
) {
611 MmioOr32((UINTN
)UsbCmd
, BIT0
);
612 // ensure that the host controller is started (HALTED bit must be cleared)
613 while (MmioRead32((UINTN
)UsbStatus
) & BIT12
);
617 // First get the ownership of port 0.
619 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, USB_DEBUG_PORT_OWNER
);
621 MicroSecondDelay (200000);
624 // Find out which port is used as debug port.
626 DebugPortNumber
= (UINT8
)((MmioRead32((UINTN
)UsbHCSParam
) & 0x00F00000) >> 20);
628 // Should find a non low-speed device is connected
630 PortStatus
= (UINT32
*)(Handle
->EhciMemoryBase
+ 0x64 + (DebugPortNumber
- 1) * 4);
631 if (!(MmioRead32((UINTN
)PortStatus
) & BIT0
) || ((MmioRead32((UINTN
)PortStatus
) & USB_PORT_LINE_STATUS_MASK
) == USB_PORT_LINE_STATUS_LS
)) {
632 return RETURN_NOT_FOUND
;
636 // Reset the debug port
638 MmioOr32((UINTN
)PortStatus
, BIT8
);
639 MicroSecondDelay (200000);
640 MmioAnd32((UINTN
)PortStatus
, (UINT32
)~BIT8
);
641 while (MmioRead32((UINTN
)PortStatus
) & BIT8
);
644 // The port enabled bit should be set by HW.
646 if ((MmioRead32((UINTN
)PortStatus
) & BIT2
) == 0) {
647 return RETURN_DEVICE_ERROR
;
651 // Enable Usb Debug Port Capability
653 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, USB_DEBUG_PORT_ENABLE
| USB_DEBUG_PORT_IN_USE
);
656 // Start to communicate with Usb Debug Device to see if the attached device is usb debug device or not.
658 Length
= (UINT8
)sizeof (USB_DEBUG_PORT_DESCRIPTOR
);
661 // It's not a dedicated usb debug device, should use address 0 to get debug descriptor.
663 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mGetDebugDescriptor
, 0x0, 0x0, (UINT8
*)&UsbDebugPortDescriptor
, &Length
);
664 if (RETURN_ERROR(Status
)) {
666 // The device is not a usb debug device.
671 if (Length
!= sizeof(USB_DEBUG_PORT_DESCRIPTOR
)) {
672 return RETURN_DEVICE_ERROR
;
676 // set usb debug device address as 0x7F.
678 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mSetDebugAddress
, 0x0, 0x0, NULL
, NULL
);
679 if (RETURN_ERROR(Status
)) {
681 // The device can not work well.
687 // enable the usb debug feature.
689 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mSetDebugFeature
, 0x7F, 0x0, NULL
, NULL
);
695 Read data from debug device and save the datas in buffer.
697 Reads NumberOfBytes data bytes from a debug device into the buffer
698 specified by Buffer. The number of bytes actually read is returned.
699 If the return value is less than NumberOfBytes, then the rest operation failed.
700 If NumberOfBytes is zero, then return 0.
702 @param Handle Debug port handle.
703 @param Buffer Pointer to the data buffer to store the data read from the debug device.
704 @param NumberOfBytes Number of bytes which will be read.
705 @param Timeout Timeout value for reading from debug device. It unit is Microsecond.
707 @retval 0 Read data failed, no data is to be read.
708 @retval >0 Actual number of bytes read from debug device.
713 DebugPortReadBuffer (
714 IN DEBUG_PORT_HANDLE Handle
,
716 IN UINTN NumberOfBytes
,
720 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
721 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
722 RETURN_STATUS Status
;
729 if (NumberOfBytes
== 0 || Buffer
== NULL
) {
738 // If Handle is NULL, it means memory is ready for use.
739 // Use global variable to store handle value.
741 if (Handle
== NULL
) {
742 UsbDebugPortHandle
= &mUsbDebugPortHandle
;
744 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
748 // Check if debug port is ready
750 if (!UsbDebugPortHandle
->Initialized
) {
754 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
755 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
756 if (RETURN_ERROR(Status
)) {
761 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)(UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
764 // First read data from buffer, then read debug port hw to get received data.
766 if (UsbDebugPortHandle
->DataCount
> 0) {
767 if (NumberOfBytes
<= UsbDebugPortHandle
->DataCount
) {
768 Total
= NumberOfBytes
;
770 Total
= UsbDebugPortHandle
->DataCount
;
773 for (Index
= 0; Index
< Total
; Index
++) {
774 Buffer
[Index
] = UsbDebugPortHandle
->Data
[Index
];
777 for (Index
= 0; Index
< UsbDebugPortHandle
->DataCount
- Total
; Index
++) {
778 if (Total
+ Index
>= 8) {
781 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortHandle
->Data
[Total
+ Index
];
783 UsbDebugPortHandle
->DataCount
= (UINT8
)(UsbDebugPortHandle
->DataCount
- (UINT8
)Total
);
787 // If Timeout is equal to 0, then it means it should always wait until all datum required are received.
790 Timeout
= 0xFFFFFFFF;
794 // Read remaining data by executing one or more usb debug transfer transactions at usb debug port hw.
796 while ((Total
< NumberOfBytes
) && (Timeout
!= 0)) {
797 Remaining
= NumberOfBytes
- Total
;
798 if (Remaining
>= USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
799 Status
= UsbDebugPortIn(UsbDebugPortRegister
, Buffer
+ Total
, &Received
, INPUT_PID
, 0x7f, 0x82, UsbDebugPortHandle
->BulkInToggle
);
801 if (RETURN_ERROR(Status
)) {
805 Status
= UsbDebugPortIn(UsbDebugPortRegister
, &UsbDebugPortHandle
->Data
[0], &Received
, INPUT_PID
, 0x7f, 0x82, UsbDebugPortHandle
->BulkInToggle
);
807 if (RETURN_ERROR(Status
)) {
811 UsbDebugPortHandle
->DataCount
= Received
;
813 if (Remaining
<= Received
) {
814 Length
= (UINT8
)Remaining
;
816 Length
= (UINT8
)Received
;
820 // Copy required data from the data buffer to user buffer.
822 for (Index
= 0; Index
< Length
; Index
++) {
823 (Buffer
+ Total
)[Index
] = UsbDebugPortHandle
->Data
[Index
];
824 UsbDebugPortHandle
->DataCount
--;
828 // reorder the data buffer to make available data arranged from the beginning of the data buffer.
830 for (Index
= 0; Index
< Received
- Length
; Index
++) {
831 if (Length
+ Index
>= 8) {
834 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortHandle
->Data
[Length
+ Index
];
837 // fixup the real received length in Buffer.
841 UsbDebugPortHandle
->BulkInToggle
^= 1;
851 Write data from buffer to debug device.
853 Writes NumberOfBytes data bytes from Buffer to the debug device.
854 The number of bytes actually written to the debug device is returned.
855 If the return value is less than NumberOfBytes, then the write operation failed.
856 If NumberOfBytes is zero, then return 0.
858 @param Handle Debug port handle.
859 @param Buffer Pointer to the data buffer to be written.
860 @param NumberOfBytes Number of bytes to written to the debug device.
862 @retval 0 NumberOfBytes is 0.
863 @retval >0 The number of bytes written to the debug device.
864 If this value is less than NumberOfBytes, then the read operation failed.
869 DebugPortWriteBuffer (
870 IN DEBUG_PORT_HANDLE Handle
,
872 IN UINTN NumberOfBytes
875 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
876 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
877 RETURN_STATUS Status
;
882 if (NumberOfBytes
== 0 || Buffer
== NULL
) {
890 // If Handle is NULL, it means memory is ready for use.
891 // Use global variable to store handle value.
893 if (Handle
== NULL
) {
894 UsbDebugPortHandle
= &mUsbDebugPortHandle
;
896 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
900 // Check if debug port is ready
902 if (!UsbDebugPortHandle
->Initialized
) {
906 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
907 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
908 if (RETURN_ERROR(Status
)) {
913 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)(UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
915 while ((Total
< NumberOfBytes
)) {
916 if (NumberOfBytes
- Total
> USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
917 Sent
= USB_DEBUG_PORT_MAX_PACKET_SIZE
;
919 Sent
= (UINT8
)(NumberOfBytes
- Total
);
922 Status
= UsbDebugPortOut(UsbDebugPortRegister
, Buffer
+ Total
, Sent
, OUTPUT_PID
, 0x7F, 0x01, UsbDebugPortHandle
->BulkOutToggle
);
924 if (RETURN_ERROR(Status
)) {
928 ReceivedPid
= (MmioRead8((UINTN
)&UsbDebugPortRegister
->ReceivedPid
));
930 // If received a NAK_PID on write transaction, it means the usb debug device is busy and can not handle this transaction.
931 // should send the packet again.
933 if (ReceivedPid
== NAK_PID
) {
936 UsbDebugPortHandle
->BulkOutToggle
^= 1;
944 Polls a debug device to see if there is any data waiting to be read.
946 Polls a debug device to see if there is any data waiting to be read.
947 If there is data waiting to be read from the debug device, then TRUE is returned.
948 If there is no data waiting to be read from the debug device, then FALSE is returned.
950 @param Handle Debug port handle.
952 @retval TRUE Data is waiting to be read from the debug device.
953 @retval FALSE There is no data waiting to be read from the serial device.
958 DebugPortPollBuffer (
959 IN DEBUG_PORT_HANDLE Handle
962 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
963 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
966 RETURN_STATUS Status
;
969 // If Handle is NULL, it means memory is ready for use.
970 // Use global variable to store handle value.
972 if (Handle
== NULL
) {
973 UsbDebugPortHandle
= &mUsbDebugPortHandle
;
975 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
979 // Check if debug port is ready
981 if (!UsbDebugPortHandle
->Initialized
) {
985 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
986 Status
= InitializeUsbDebugHardware(UsbDebugPortHandle
);
987 if (RETURN_ERROR(Status
)) {
993 // If the data buffer is not empty, then return TRUE directly.
994 // else initialize a usb read transaction and read data to the data buffer.
996 if (UsbDebugPortHandle
->DataCount
!= 0) {
1000 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)(UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
1002 UsbDebugPortRegister
->TokenPid
= INPUT_PID
;
1003 if (UsbDebugPortHandle
->BulkInToggle
== 0) {
1004 UsbDebugPortRegister
->SendPid
= DATA0_PID
;
1006 UsbDebugPortRegister
->SendPid
= DATA1_PID
;
1008 UsbDebugPortRegister
->UsbAddress
= 0x7F;
1009 UsbDebugPortRegister
->UsbEndPoint
= 0x82 & 0x0F;
1012 // Clearing W/R bit to indicate it's a READ operation
1014 MmioAnd32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, (UINT32
)~BIT4
);
1016 // Setting GO bit as well as clearing DONE bit
1018 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, (UINT32
)BIT5
);
1021 // Wait for completing the request
1023 while ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (UINT32
)BIT16
) == 0);
1025 if ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
)) & BIT6
) {
1029 Length
= (UINT8
)(MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & 0xF);
1035 UsbDebugPortHandle
->BulkInToggle
^= 1;
1041 for (Index
= 0; Index
< Length
; Index
++) {
1042 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortRegister
->DataBuffer
[Index
];
1044 UsbDebugPortHandle
->DataCount
= Length
;
1050 Initialize the debug port.
1052 If Function is not NULL, Debug Communication Libary will call this function
1053 by passing in the Context to be the first parameter. If needed, Debug Communication
1054 Library will create one debug port handle to be the second argument passing in
1055 calling the Function, otherwise it will pass NULL to be the second argument of
1058 If Function is NULL, and Context is not NULL, the Debug Communication Library could
1059 a) Return the same handle as passed in (as Context parameter).
1060 b) Ignore the input Context parameter and create new hanlde to be returned.
1062 If parameter Function is NULL and Context is NULL, Debug Communication Library could
1063 created a new handle if needed and return it, otherwise it will return NULL.
1065 @param[in] Context Context needed by callback function; it was optional.
1066 @param[in] Function Continue function called by Debug Communication library;
1069 @return The debug port handle created by Debug Communication Library if Function
1075 DebugPortInitialize (
1077 IN DEBUG_PORT_CONTINUE Function
1080 RETURN_STATUS Status
;
1081 USB_DEBUG_PORT_HANDLE Handle
;
1083 if (Function
== NULL
&& Context
!= NULL
) {
1084 return (DEBUG_PORT_HANDLE
*) Context
;
1087 ZeroMem(&Handle
, sizeof (USB_DEBUG_PORT_HANDLE
));
1089 Status
= CalculateUsbDebugPortBar(&Handle
.DebugPortOffset
, &Handle
.DebugPortBarNumber
);
1090 if (RETURN_ERROR (Status
)) {
1091 DEBUG ((EFI_D_ERROR
, "USB Debug Port: the pci device pointed by PcdUsbEhciPciAddress is not EHCI host controller or does not support debug port capability!\n"));
1095 Handle
.EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
1097 if (Handle
.EhciMemoryBase
== 0) {
1099 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
1101 PciWrite32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
, PcdGet32(PcdUsbEhciMemorySpaceBase
));
1102 Handle
.EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
1105 Handle
.UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4);
1107 if (Handle
.UsbDebugPortMemoryBase
== 0) {
1109 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
1111 PciWrite32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4, PcdGet32(PcdUsbDebugPortMemorySpaceBase
));
1112 Handle
.UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4);
1115 Status
= InitializeUsbDebugHardware (&Handle
);
1116 if (RETURN_ERROR(Status
)) {
1117 DEBUG ((EFI_D_ERROR
, "USB Debug Port: Initialization failed, please check if USB debug cable is plugged into EHCI debug port correctly!\n"));
1122 // Set debug port initialized successfully flag
1124 Handle
.Initialized
= TRUE
;
1128 if (Function
!= NULL
) {
1129 Function (Context
, &Handle
);
1131 CopyMem(&mUsbDebugPortHandle
, &Handle
, sizeof (USB_DEBUG_PORT_HANDLE
));
1134 return (DEBUG_PORT_HANDLE
)(UINTN
)&mUsbDebugPortHandle
;