2 Debug Port Library implementation based on usb debug port.
4 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <IndustryStandard/Pci.h>
11 #include <IndustryStandard/Usb.h>
12 #include <Library/IoLib.h>
13 #include <Library/PciLib.h>
14 #include <Library/PcdLib.h>
15 #include <Library/TimerLib.h>
16 #include <Library/DebugCommunicationLib.h>
17 #include <Library/BaseMemoryLib.h>
18 #include <Library/BaseLib.h>
19 #include <Library/DebugLib.h>
21 #define SETUP_PID 0x2D
22 #define INPUT_PID 0x69
23 #define OUTPUT_PID 0xE1
24 #define ERROR_PID 0x55
25 #define DATA0_PID 0xC3
26 #define DATA1_PID 0x4B
27 #define DATA2_PID 0x87
28 #define MDATA_PID 0x0F
31 #define STALL_PID 0x1E
34 #define PCI_CAPABILITY_ID_DEBUG_PORT 0x0A
35 #define USB_DEBUG_PORT_MAX_PACKET_SIZE 0x08
37 #define USB_DEBUG_PORT_IN_USE BIT10
38 #define USB_DEBUG_PORT_ENABLE BIT28
39 #define USB_DEBUG_PORT_OWNER BIT30
41 #define USB_PORT_LINE_STATUS_LS 0x400
42 #define USB_PORT_LINE_STATUS_MASK 0xC00
45 // Usb debug device descriptor, which is defined at
46 // USB2 Debug Device Specification.
48 typedef struct _USB_DEBUG_PORT_DESCRIPTOR
{
51 UINT8 DebugInEndpoint
;
52 UINT8 DebugOutEndpoint
;
53 }USB_DEBUG_PORT_DESCRIPTOR
;
55 USB_DEVICE_REQUEST mDebugCommunicationLibUsbGetDebugDescriptor
= {
57 USB_REQ_GET_DESCRIPTOR
,
60 sizeof(USB_DEBUG_PORT_DESCRIPTOR
)
63 USB_DEVICE_REQUEST mDebugCommunicationLibUsbSetDebugFeature
= {
71 USB_DEVICE_REQUEST mDebugCommunicationLibUsbSetDebugAddress
= {
80 // Usb debug port register file, which is defined at
81 // EHCI Specification.
83 typedef struct _USB_DEBUG_PORT_REGISTER
{
94 }USB_DEBUG_PORT_REGISTER
;
97 // The state machine of usb debug port
99 #define USBDBG_NO_DEV 0 // No device present at debug port
100 #define USBDBG_NO_DBG_CAB 1 // The device attached is not usb debug cable
101 #define USBDBG_DBG_CAB 2 // The device attached is usb debug cable
102 #define USBDBG_INIT_DONE 4 // The usb debug cable device is initialized
103 #define USBDBG_RESET 8 // The system is reset
107 // The internal data structure of DEBUG_PORT_HANDLE, which stores some
108 // important datum which are used across various phases.
110 typedef struct _USB_DEBUG_PORT_HANDLE
{
112 // The usb debug port memory BAR number in EHCI configuration space.
114 UINT8 DebugPortBarNumber
;
117 // The offset of usb debug port registers in EHCI memory range.
119 UINT16 DebugPortOffset
;
121 // The usb debug port memory BAR address.
123 UINT32 UsbDebugPortMemoryBase
;
125 // The EHCI memory BAR address.
127 UINT32 EhciMemoryBase
;
129 // The usb debug device In endpoint.
133 // The usb debug device Out endpoint.
137 // The Bulk In endpoint toggle bit.
141 // The Bulk Out endpoint toggle bit.
145 // The available data length in the following data buffer.
149 // The data buffer. Maximum length is 8 bytes.
152 } USB_DEBUG_PORT_HANDLE
;
156 // The global variable which can be used after memory is ready.
158 USB_DEBUG_PORT_HANDLE mDebugCommunicationLibUsbDebugPortHandle
;
161 Calculate the usb debug port bar address.
163 @param DebugPortOffset Get usb debug port offset in the usb debug port memory space.
164 @param DebugPortBarNumbar Get the bar number at which usb debug port is located.
166 @retval RETURN_UNSUPPORTED The usb host controller does not supported usb debug port capability.
167 @retval RETURN_SUCCESS Get bar and offset successfully.
172 CalculateUsbDebugPortBar (
173 OUT UINT16
*DebugPortOffset
,
174 OUT UINT8
*DebugPortBarNumbar
186 VendorId
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_VENDOR_ID_OFFSET
);
187 DeviceId
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_DEVICE_ID_OFFSET
);
189 if ((VendorId
== 0xFFFF) || (DeviceId
== 0xFFFF)) {
190 return RETURN_UNSUPPORTED
;
193 ProgInterface
= PciRead8 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_CLASSCODE_OFFSET
);
194 SubClassCode
= PciRead8 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_CLASSCODE_OFFSET
+ 1);
195 BaseCode
= PciRead8 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_CLASSCODE_OFFSET
+ 2);
197 if ((ProgInterface
!= PCI_IF_EHCI
) || (SubClassCode
!= PCI_CLASS_SERIAL_USB
) || (BaseCode
!= PCI_CLASS_SERIAL
)) {
198 return RETURN_UNSUPPORTED
;
202 // Enable Ehci Host Controller MMIO Space.
204 PciStatus
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_PRIMARY_STATUS_OFFSET
);
206 if ((PciStatus
& EFI_PCI_STATUS_CAPABILITY
) == 0) {
208 // The Pci Device Doesn't Support Capability Pointer.
210 return RETURN_UNSUPPORTED
;
214 // Get Pointer To Capability List
216 CapabilityPtr
= PciRead8(PcdGet32(PcdUsbEhciPciAddress
) + PCI_CAPBILITY_POINTER_OFFSET
);
219 // Find Capability ID 0xA, Which Is For Debug Port
221 while (CapabilityPtr
!= 0) {
222 CapabilityId
= PciRead8(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
);
223 if (CapabilityId
== PCI_CAPABILITY_ID_DEBUG_PORT
) {
226 CapabilityPtr
= PciRead8(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
+ 1);
230 // No Debug Port Capability Found
232 if (CapabilityPtr
== 0) {
233 return RETURN_UNSUPPORTED
;
237 // Get The Base Address Of Debug Port Register In Debug Port Capability Register
239 *DebugPortOffset
= (UINT16
)(PciRead16(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
+ 2) & 0x1FFF);
240 *DebugPortBarNumbar
= (UINT8
)((PciRead16(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
+ 2) >> 13) - 1);
242 return RETURN_SUCCESS
;
246 Do a usb IN transaction by usb debug port.
248 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
249 @param Buffer Pointer to the buffer receiving data.
250 @param Length Number of bytes of the received data.
251 @param Token The token PID for each USB transaction.
252 @param Addr The usb device address for usb transaction.
253 @param Ep The endpoint for usb transaction.
254 @param DataToggle The toggle bit used at usb transaction.
256 @retval RETURN_SUCCESS The IN transaction is executed successfully.
257 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
258 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
264 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
265 IN OUT UINT8
*Buffer
,
275 if (Length
== NULL
) {
276 return RETURN_INVALID_PARAMETER
;
280 DebugPortRegister
->TokenPid
= Token
;
281 if (DataToggle
!= 0) {
282 DebugPortRegister
->SendPid
= DATA1_PID
;
284 DebugPortRegister
->SendPid
= DATA0_PID
;
287 DebugPortRegister
->UsbAddress
= (UINT8
)(Addr
& 0x7F);
288 DebugPortRegister
->UsbEndPoint
= (UINT8
)(Ep
& 0xF);
291 // Clearing W/R bit to indicate it's a READ operation
293 MmioAnd32((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)~BIT4
);
296 // Setting GO bit as well as clearing DONE bit
298 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)BIT5
);
301 // Wait for completing the request
303 while ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & (UINT32
)BIT16
) == 0) {
304 if ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
305 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
)) {
306 return RETURN_DEVICE_ERROR
;
311 // Clearing DONE bit by writing 1
313 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, BIT16
);
316 // Check if the request is executed successfully or not.
318 if ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & BIT6
) {
319 return RETURN_DEVICE_ERROR
;
323 // Make sure the received data are not beyond the allowable maximum length - 8 byte
325 if (((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & 0xF) > USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
326 return RETURN_DEVICE_ERROR
;
329 *Length
= (UINT8
)(MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & 0xF);
331 return RETURN_DEVICE_ERROR
;
334 for (Index
= 0; Index
< *Length
; Index
++) {
335 Buffer
[Index
] = DebugPortRegister
->DataBuffer
[Index
];
337 return RETURN_SUCCESS
;
341 Do a usb SETUP/OUT transaction by usb debug port.
343 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
344 @param Buffer Pointer to the buffer receiving data.
345 @param Length Number of bytes of the received data.
346 @param Token The token PID for each USB transaction.
347 @param Addr The usb device address for usb transaction.
348 @param Ep The endpoint for usb transaction.
349 @param DataToggle The toggle bit used at usb transaction.
351 @retval RETURN_SUCCESS The IN transaction is executed successfully.
352 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
353 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
359 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
371 return RETURN_INVALID_PARAMETER
;
374 DebugPortRegister
->TokenPid
= Token
;
375 if (DataToggle
!= 0) {
376 DebugPortRegister
->SendPid
= DATA1_PID
;
378 DebugPortRegister
->SendPid
= DATA0_PID
;
380 DebugPortRegister
->UsbAddress
= (UINT8
)(Addr
& 0x7F);
381 DebugPortRegister
->UsbEndPoint
= (UINT8
)(Ep
& 0xF);
384 // Fill in the data length and corresponding data.
386 MmioAnd32((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)~0xF);
387 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, Length
& 0xF);
388 for (Index
= 0; Index
< Length
; Index
++) {
389 DebugPortRegister
->DataBuffer
[Index
] = Buffer
[Index
];
393 // Setting W/R bit to indicate it's a WRITE operation
395 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, BIT4
);
397 // Setting GO bit as well as clearing DONE bit
399 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, BIT5
);
402 // Wait for completing the request
404 while ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & BIT16
) == 0) {
405 if ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
406 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
)) {
407 return RETURN_DEVICE_ERROR
;
412 // Clearing DONE bit by writing 1
414 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, BIT16
);
417 // Check if the request is executed successfully or not.
419 if ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & BIT6
) {
420 return RETURN_DEVICE_ERROR
;
424 // Make sure the sent data are not beyond the allowable maximum length - 8 byte
426 if (((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & 0xF) > USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
427 return RETURN_DEVICE_ERROR
;
430 return RETURN_SUCCESS
;
434 Do a usb control transfer by usb debug port.
436 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
437 @param SetupPacket The token PID for each USB transaction.
438 @param Addr The usb device address for usb transaction.
439 @param Ep The endpoint for usb transaction.
440 @param Data Pointer to the buffer receiving data.
441 @param DataLength Number of bytes of the received data.
443 @retval RETURN_SUCCESS The IN transaction is executed successfully.
444 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
445 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
450 UsbDebugPortControlTransfer (
451 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
452 IN USB_DEVICE_REQUEST
*SetupPacket
,
456 IN OUT UINT8
*DataLength
459 RETURN_STATUS Status
;
461 UINT8 ReturnStatus
[8];
466 Status
= UsbDebugPortOut(DebugPortRegister
, (UINT8
*)SetupPacket
, (UINT8
)sizeof(USB_DEVICE_REQUEST
), SETUP_PID
, Addr
, Ep
, 0);
467 if (RETURN_ERROR(Status
)) {
474 if (DataLength
!= 0) {
475 if ((SetupPacket
->RequestType
& BIT7
) != 0) {
477 // Get Data From Device
479 Status
= UsbDebugPortIn(DebugPortRegister
, Data
, DataLength
, INPUT_PID
, Addr
, Ep
, 1);
480 if (RETURN_ERROR(Status
)) {
485 // Send Data To Device
487 Status
= UsbDebugPortOut(DebugPortRegister
, Data
, *DataLength
, OUTPUT_PID
, Addr
, Ep
, 1);
488 if (RETURN_ERROR(Status
)) {
497 if ((SetupPacket
->RequestType
& BIT7
) != 0) {
499 // For READ operation, Data Toggle in Status Phase Should be 1.
501 Status
= UsbDebugPortOut(DebugPortRegister
, NULL
, 0, OUTPUT_PID
, Addr
, Ep
, 1);
504 // For WRITE operation, Data Toggle in Status Phase Should be 1.
506 Status
= UsbDebugPortIn(DebugPortRegister
, ReturnStatus
, &Temp
, INPUT_PID
, Addr
, Ep
, 1);
513 Check if it needs to re-initialize usb debug port hardware.
515 During different phases switch, such as SEC to PEI or PEI to DXE or DXE to SMM, we should check
516 whether the usb debug port hardware configuration is changed. Such case can be triggered by
517 Pci bus resource allocation and so on.
519 @param Handle Debug port handle.
521 @retval TRUE The usb debug port hardware configuration is changed.
522 @retval FALSE The usb debug port hardware configuration is not changed.
527 NeedReinitializeHardware(
528 IN USB_DEBUG_PORT_HANDLE
*Handle
532 UINT32 UsbDebugPortMemoryBase
;
533 UINT32 EhciMemoryBase
;
535 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
539 EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
540 if (EhciMemoryBase
!= Handle
->EhciMemoryBase
) {
541 Handle
->EhciMemoryBase
= EhciMemoryBase
;
545 UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
->DebugPortBarNumber
* 4);
546 if (UsbDebugPortMemoryBase
!= Handle
->UsbDebugPortMemoryBase
) {
547 Handle
->UsbDebugPortMemoryBase
= UsbDebugPortMemoryBase
;
552 // Enable Ehci Memory Space Access
554 PciCmd
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_COMMAND_OFFSET
);
555 if (((PciCmd
& EFI_PCI_COMMAND_MEMORY_SPACE
) == 0) || ((PciCmd
& EFI_PCI_COMMAND_BUS_MASTER
) == 0)) {
556 PciCmd
|= EFI_PCI_COMMAND_MEMORY_SPACE
| EFI_PCI_COMMAND_BUS_MASTER
;
557 PciWrite16(PcdGet32(PcdUsbEhciPciAddress
) + PCI_COMMAND_OFFSET
, PciCmd
);
562 // 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.
564 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)((UINTN
)Handle
->UsbDebugPortMemoryBase
+ Handle
->DebugPortOffset
);
565 if ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_ENABLE
| USB_DEBUG_PORT_IN_USE
))
566 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_ENABLE
| USB_DEBUG_PORT_IN_USE
)) {
570 if (Handle
->Initialized
== USBDBG_RESET
) {
572 } else if (Handle
->Initialized
!= USBDBG_INIT_DONE
) {
579 Initialize usb debug port hardware.
581 1. reset ehci host controller.
582 2. set right port to debug port.
583 3. find a usb debug device is attached by getting debug device descriptor.
584 4. set address for the usb debug device.
585 5. configure the usb debug device to debug mode.
587 @param Handle Debug port handle.
589 @retval TRUE The usb debug port hardware configuration is changed.
590 @retval FALSE The usb debug port hardware configuration is not changed.
595 InitializeUsbDebugHardware (
596 IN USB_DEBUG_PORT_HANDLE
*Handle
599 RETURN_STATUS Status
;
600 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
601 USB_DEBUG_PORT_DESCRIPTOR UsbDebugPortDescriptor
;
606 UINT8 DebugPortNumber
;
609 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)((UINTN
)Handle
->UsbDebugPortMemoryBase
+ Handle
->DebugPortOffset
);
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
)) {
621 "UsbDbg: Need to reset the host controller. ControlStatus = %08x\n",
622 MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
)
625 // If the host controller is halted, then reset and restart it.
627 if ((MmioRead32((UINTN
)UsbStatus
) & BIT12
) != 0) {
628 DEBUG ((DEBUG_INFO
, "UsbDbg: Reset the host controller.\n"));
630 // reset the host controller.
632 MmioOr32((UINTN
)UsbCmd
, BIT1
);
634 // ensure that the host controller is reset.
636 while ((MmioRead32((UINTN
)UsbCmd
) & BIT1
) != 0);
638 MmioOr32((UINTN
)UsbCmd
, BIT0
);
639 // ensure that the host controller is started (HALTED bit must be cleared)
640 while ((MmioRead32((UINTN
)UsbStatus
) & BIT12
) != 0);
644 // First get the ownership of port 0.
646 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
);
648 MicroSecondDelay (200000);
651 // Find out which port is used as debug port.
653 DebugPortNumber
= (UINT8
)((MmioRead32((UINTN
)UsbHCSParam
) & 0x00F00000) >> 20);
655 // Should find a device is connected at debug port
657 PortStatus
= (UINT32
*)((UINTN
)Handle
->EhciMemoryBase
+ 0x64 + (DebugPortNumber
- 1) * 4);
658 if (!(MmioRead32((UINTN
)PortStatus
) & BIT0
)) {
659 Handle
->Initialized
= USBDBG_NO_DEV
;
660 return RETURN_NOT_FOUND
;
663 if (Handle
->Initialized
!= USBDBG_INIT_DONE
||
664 (MmioRead32 ((UINTN
) &UsbDebugPortRegister
->ControlStatus
) & USB_DEBUG_PORT_ENABLE
) == 0) {
665 DEBUG ((DEBUG_INFO
, "UsbDbg: Reset the debug port.\n"));
667 // Reset the debug port
669 MmioOr32((UINTN
)PortStatus
, BIT8
);
670 MicroSecondDelay (500000);
671 MmioAnd32((UINTN
)PortStatus
, (UINT32
)~BIT8
);
672 while (MmioRead32((UINTN
)PortStatus
) & BIT8
);
675 // The port enabled bit should be set by HW.
677 if ((MmioRead32((UINTN
)PortStatus
) & BIT2
) == 0) {
678 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
679 return RETURN_DEVICE_ERROR
;
683 // Enable Usb Debug Port Capability
685 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, USB_DEBUG_PORT_ENABLE
);
688 // initialize the data toggle used by bulk in/out endpoint.
690 Handle
->BulkInToggle
= 0;
691 Handle
->BulkOutToggle
= 0;
694 // set usb debug device address as 0x7F.
696 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mDebugCommunicationLibUsbSetDebugAddress
, 0x0, 0x0, NULL
, NULL
);
697 if (RETURN_ERROR(Status
)) {
699 // The device can not work well.
701 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
706 // Start to communicate with Usb Debug Device to see if the attached device is usb debug device or not.
708 Length
= (UINT8
)sizeof (USB_DEBUG_PORT_DESCRIPTOR
);
711 // Get debug descriptor.
713 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mDebugCommunicationLibUsbGetDebugDescriptor
, 0x7F, 0x0, (UINT8
*)&UsbDebugPortDescriptor
, &Length
);
714 if (RETURN_ERROR(Status
)) {
716 // The device is not a usb debug device.
718 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
722 if (Length
!= sizeof(USB_DEBUG_PORT_DESCRIPTOR
)) {
723 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
724 return RETURN_DEVICE_ERROR
;
728 // Determine the usb debug device endpoints.
730 Handle
->InEndpoint
= UsbDebugPortDescriptor
.DebugInEndpoint
;
731 Handle
->OutEndpoint
= UsbDebugPortDescriptor
.DebugOutEndpoint
;
734 // enable the usb debug feature.
736 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mDebugCommunicationLibUsbSetDebugFeature
, 0x7F, 0x0, NULL
, NULL
);
737 if (RETURN_ERROR(Status
)) {
739 // The device can not work well.
741 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
745 Handle
->Initialized
= USBDBG_DBG_CAB
;
749 // Set initialized flag
751 Handle
->Initialized
= USBDBG_INIT_DONE
;
753 return RETURN_SUCCESS
;
757 Read data from debug device and save the datas in buffer.
759 Reads NumberOfBytes data bytes from a debug device into the buffer
760 specified by Buffer. The number of bytes actually read is returned.
761 If the return value is less than NumberOfBytes, then the rest operation failed.
762 If NumberOfBytes is zero, then return 0.
764 @param Handle Debug port handle.
765 @param Buffer Pointer to the data buffer to store the data read from the debug device.
766 @param NumberOfBytes Number of bytes which will be read.
767 @param Timeout Timeout value for reading from debug device. It unit is Microsecond.
769 @retval 0 Read data failed, no data is to be read.
770 @retval >0 Actual number of bytes read from debug device.
775 DebugPortReadBuffer (
776 IN DEBUG_PORT_HANDLE Handle
,
778 IN UINTN NumberOfBytes
,
782 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
783 RETURN_STATUS Status
;
786 if (NumberOfBytes
!= 1 || Buffer
== NULL
|| Timeout
!= 0) {
791 // If Handle is NULL, it means memory is ready for use.
792 // Use global variable to store handle value.
794 if (Handle
== NULL
) {
795 UsbDebugPortHandle
= &mDebugCommunicationLibUsbDebugPortHandle
;
797 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
800 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
801 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
802 if (RETURN_ERROR(Status
)) {
808 // Read data from buffer
810 if (UsbDebugPortHandle
->DataCount
< 1) {
813 *Buffer
= UsbDebugPortHandle
->Data
[0];
814 for (Index
= 0; Index
< UsbDebugPortHandle
->DataCount
- 1; Index
++) {
815 if ((Index
+ 1) >= USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
818 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortHandle
->Data
[Index
+ 1];
820 UsbDebugPortHandle
->DataCount
= (UINT8
)(UsbDebugPortHandle
->DataCount
- 1);
826 Write data from buffer to debug device.
828 Writes NumberOfBytes data bytes from Buffer to the debug device.
829 The number of bytes actually written to the debug device is returned.
830 If the return value is less than NumberOfBytes, then the write operation failed.
831 If NumberOfBytes is zero, then return 0.
833 @param Handle Debug port handle.
834 @param Buffer Pointer to the data buffer to be written.
835 @param NumberOfBytes Number of bytes to written to the debug device.
837 @retval 0 NumberOfBytes is 0.
838 @retval >0 The number of bytes written to the debug device.
839 If this value is less than NumberOfBytes, then the read operation failed.
844 DebugPortWriteBuffer (
845 IN DEBUG_PORT_HANDLE Handle
,
847 IN UINTN NumberOfBytes
850 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
851 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
852 RETURN_STATUS Status
;
857 if (NumberOfBytes
== 0 || Buffer
== NULL
) {
865 // If Handle is NULL, it means memory is ready for use.
866 // Use global variable to store handle value.
868 if (Handle
== NULL
) {
869 UsbDebugPortHandle
= &mDebugCommunicationLibUsbDebugPortHandle
;
871 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
874 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
875 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
876 if (RETURN_ERROR(Status
)) {
881 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)((UINTN
)UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
883 while ((Total
< NumberOfBytes
)) {
884 if (NumberOfBytes
- Total
> USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
885 Sent
= USB_DEBUG_PORT_MAX_PACKET_SIZE
;
887 Sent
= (UINT8
)(NumberOfBytes
- Total
);
890 Status
= UsbDebugPortOut(UsbDebugPortRegister
, Buffer
+ Total
, Sent
, OUTPUT_PID
, 0x7F, UsbDebugPortHandle
->OutEndpoint
, UsbDebugPortHandle
->BulkOutToggle
);
892 if (RETURN_ERROR(Status
)) {
896 ReceivedPid
= (MmioRead8((UINTN
)&UsbDebugPortRegister
->ReceivedPid
));
898 // If received a NAK_PID on write transaction, it means the usb debug device is busy and can not handle this transaction.
899 // should send the packet again.
901 if (ReceivedPid
== NAK_PID
) {
904 UsbDebugPortHandle
->BulkOutToggle
^= 1;
912 Polls a debug device to see if there is any data waiting to be read.
914 Polls a debug device to see if there is any data waiting to be read.
915 If there is data waiting to be read from the debug device, then TRUE is returned.
916 If there is no data waiting to be read from the debug device, then FALSE is returned.
918 @param Handle Debug port handle.
920 @retval TRUE Data is waiting to be read from the debug device.
921 @retval FALSE There is no data waiting to be read from the serial device.
926 DebugPortPollBuffer (
927 IN DEBUG_PORT_HANDLE Handle
930 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
931 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
934 RETURN_STATUS Status
;
937 // If Handle is NULL, it means memory is ready for use.
938 // Use global variable to store handle value.
940 if (Handle
== NULL
) {
941 UsbDebugPortHandle
= &mDebugCommunicationLibUsbDebugPortHandle
;
943 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
946 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
947 Status
= InitializeUsbDebugHardware(UsbDebugPortHandle
);
948 if (RETURN_ERROR(Status
)) {
954 // If the data buffer is not empty, then return TRUE directly.
955 // else initialize a usb read transaction and read data to the data buffer.
957 if (UsbDebugPortHandle
->DataCount
!= 0) {
961 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)((UINTN
)UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
963 UsbDebugPortRegister
->TokenPid
= INPUT_PID
;
964 if (UsbDebugPortHandle
->BulkInToggle
== 0) {
965 UsbDebugPortRegister
->SendPid
= DATA0_PID
;
967 UsbDebugPortRegister
->SendPid
= DATA1_PID
;
969 UsbDebugPortRegister
->UsbAddress
= 0x7F;
970 UsbDebugPortRegister
->UsbEndPoint
= UsbDebugPortHandle
->InEndpoint
& 0x0F;
973 // Clearing W/R bit to indicate it's a READ operation
975 MmioAnd32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, (UINT32
)~BIT4
);
977 // Setting GO bit as well as clearing DONE bit
979 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, (UINT32
)BIT5
);
982 // Wait for completing the request
984 while ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (UINT32
)BIT16
) == 0) {
985 if ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
986 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
)) {
991 if ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
)) & BIT6
) {
995 Length
= (UINT8
)(MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & 0xF);
1001 UsbDebugPortHandle
->BulkInToggle
^= 1;
1007 for (Index
= 0; Index
< Length
; Index
++) {
1008 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortRegister
->DataBuffer
[Index
];
1010 UsbDebugPortHandle
->DataCount
= Length
;
1016 Initialize the debug port.
1018 If Function is not NULL, Debug Communication Library will call this function
1019 by passing in the Context to be the first parameter. If needed, Debug Communication
1020 Library will create one debug port handle to be the second argument passing in
1021 calling the Function, otherwise it will pass NULL to be the second argument of
1024 If Function is NULL, and Context is not NULL, the Debug Communication Library could
1025 a) Return the same handle as passed in (as Context parameter).
1026 b) Ignore the input Context parameter and create new handle to be returned.
1028 If parameter Function is NULL and Context is NULL, Debug Communication Library could
1029 created a new handle if needed and return it, otherwise it will return NULL.
1031 @param[in] Context Context needed by callback function; it was optional.
1032 @param[in] Function Continue function called by Debug Communication library;
1035 @return The debug port handle created by Debug Communication Library if Function
1041 DebugPortInitialize (
1043 IN DEBUG_PORT_CONTINUE Function
1046 RETURN_STATUS Status
;
1047 USB_DEBUG_PORT_HANDLE Handle
;
1050 // Validate the PCD PcdDebugPortHandleBufferSize value
1052 ASSERT (PcdGet16 (PcdDebugPortHandleBufferSize
) == sizeof (USB_DEBUG_PORT_HANDLE
));
1054 if (Function
== NULL
&& Context
!= NULL
) {
1055 return (DEBUG_PORT_HANDLE
*) Context
;
1057 ZeroMem(&Handle
, sizeof (USB_DEBUG_PORT_HANDLE
));
1059 Status
= CalculateUsbDebugPortBar(&Handle
.DebugPortOffset
, &Handle
.DebugPortBarNumber
);
1060 if (RETURN_ERROR (Status
)) {
1061 DEBUG ((DEBUG_ERROR
, "UsbDbg: the pci device pointed by PcdUsbEhciPciAddress is not EHCI host controller or does not support debug port capability!\n"));
1065 Handle
.EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
1067 if (Handle
.EhciMemoryBase
== 0) {
1069 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
1071 PciWrite32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
, PcdGet32(PcdUsbEhciMemorySpaceBase
));
1072 Handle
.EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
1075 Handle
.UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4);
1077 if (Handle
.UsbDebugPortMemoryBase
== 0) {
1079 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
1081 PciWrite32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4, PcdGet32(PcdUsbDebugPortMemorySpaceBase
));
1082 Handle
.UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4);
1085 Handle
.Initialized
= USBDBG_RESET
;
1087 if (NeedReinitializeHardware(&Handle
)) {
1088 DEBUG ((DEBUG_ERROR
, "UsbDbg: Start EHCI debug port initialization!\n"));
1089 Status
= InitializeUsbDebugHardware (&Handle
);
1090 if (RETURN_ERROR(Status
)) {
1091 DEBUG ((DEBUG_ERROR
, "UsbDbg: Failed, please check if USB debug cable is plugged into EHCI debug port correctly!\n"));
1098 if (Function
!= NULL
) {
1099 Function (Context
, &Handle
);
1101 CopyMem(&mDebugCommunicationLibUsbDebugPortHandle
, &Handle
, sizeof (USB_DEBUG_PORT_HANDLE
));
1104 return (DEBUG_PORT_HANDLE
)(UINTN
)&mDebugCommunicationLibUsbDebugPortHandle
;