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
) {
227 CapabilityPtr
= PciRead8 (PcdGet32 (PcdUsbEhciPciAddress
) + CapabilityPtr
+ 1);
231 // No Debug Port Capability Found
233 if (CapabilityPtr
== 0) {
234 return RETURN_UNSUPPORTED
;
238 // Get The Base Address Of Debug Port Register In Debug Port Capability Register
240 *DebugPortOffset
= (UINT16
)(PciRead16 (PcdGet32 (PcdUsbEhciPciAddress
) + CapabilityPtr
+ 2) & 0x1FFF);
241 *DebugPortBarNumbar
= (UINT8
)((PciRead16 (PcdGet32 (PcdUsbEhciPciAddress
) + CapabilityPtr
+ 2) >> 13) - 1);
243 return RETURN_SUCCESS
;
247 Do a usb IN transaction by usb debug port.
249 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
250 @param Buffer Pointer to the buffer receiving data.
251 @param Length Number of bytes of the received data.
252 @param Token The token PID for each USB transaction.
253 @param Addr The usb device address for usb transaction.
254 @param Ep The endpoint for usb transaction.
255 @param DataToggle The toggle bit used at usb transaction.
257 @retval RETURN_SUCCESS The IN transaction is executed successfully.
258 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
259 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
265 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
266 IN OUT UINT8
*Buffer
,
276 if (Length
== NULL
) {
277 return RETURN_INVALID_PARAMETER
;
282 DebugPortRegister
->TokenPid
= Token
;
283 if (DataToggle
!= 0) {
284 DebugPortRegister
->SendPid
= DATA1_PID
;
286 DebugPortRegister
->SendPid
= DATA0_PID
;
289 DebugPortRegister
->UsbAddress
= (UINT8
)(Addr
& 0x7F);
290 DebugPortRegister
->UsbEndPoint
= (UINT8
)(Ep
& 0xF);
293 // Clearing W/R bit to indicate it's a READ operation
295 MmioAnd32 ((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
) ~BIT4
);
298 // Setting GO bit as well as clearing DONE bit
300 MmioOr32 ((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)BIT5
);
303 // Wait for completing the request
305 while ((MmioRead32 ((UINTN
)&DebugPortRegister
->ControlStatus
) & (UINT32
)BIT16
) == 0) {
306 if ((MmioRead32 ((UINTN
)&DebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
307 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
309 return RETURN_DEVICE_ERROR
;
314 // Clearing DONE bit by writing 1
316 MmioOr32 ((UINTN
)&DebugPortRegister
->ControlStatus
, BIT16
);
319 // Check if the request is executed successfully or not.
321 if ((MmioRead32 ((UINTN
)&DebugPortRegister
->ControlStatus
)) & BIT6
) {
322 return RETURN_DEVICE_ERROR
;
326 // Make sure the received data are not beyond the allowable maximum length - 8 byte
328 if (((MmioRead32 ((UINTN
)&DebugPortRegister
->ControlStatus
)) & 0xF) > USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
329 return RETURN_DEVICE_ERROR
;
332 *Length
= (UINT8
)(MmioRead32 ((UINTN
)&DebugPortRegister
->ControlStatus
) & 0xF);
334 return RETURN_DEVICE_ERROR
;
337 for (Index
= 0; Index
< *Length
; Index
++) {
338 Buffer
[Index
] = DebugPortRegister
->DataBuffer
[Index
];
341 return RETURN_SUCCESS
;
345 Do a usb SETUP/OUT transaction by usb debug port.
347 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
348 @param Buffer Pointer to the buffer receiving data.
349 @param Length Number of bytes of the received data.
350 @param Token The token PID for each USB transaction.
351 @param Addr The usb device address for usb transaction.
352 @param Ep The endpoint for usb transaction.
353 @param DataToggle The toggle bit used at usb transaction.
355 @retval RETURN_SUCCESS The IN transaction is executed successfully.
356 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
357 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
363 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
375 return RETURN_INVALID_PARAMETER
;
378 DebugPortRegister
->TokenPid
= Token
;
379 if (DataToggle
!= 0) {
380 DebugPortRegister
->SendPid
= DATA1_PID
;
382 DebugPortRegister
->SendPid
= DATA0_PID
;
385 DebugPortRegister
->UsbAddress
= (UINT8
)(Addr
& 0x7F);
386 DebugPortRegister
->UsbEndPoint
= (UINT8
)(Ep
& 0xF);
389 // Fill in the data length and corresponding data.
391 MmioAnd32 ((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
) ~0xF);
392 MmioOr32 ((UINTN
)&DebugPortRegister
->ControlStatus
, Length
& 0xF);
393 for (Index
= 0; Index
< Length
; Index
++) {
394 DebugPortRegister
->DataBuffer
[Index
] = Buffer
[Index
];
398 // Setting W/R bit to indicate it's a WRITE operation
400 MmioOr32 ((UINTN
)&DebugPortRegister
->ControlStatus
, BIT4
);
402 // Setting GO bit as well as clearing DONE bit
404 MmioOr32 ((UINTN
)&DebugPortRegister
->ControlStatus
, BIT5
);
407 // Wait for completing the request
409 while ((MmioRead32 ((UINTN
)&DebugPortRegister
->ControlStatus
) & BIT16
) == 0) {
410 if ((MmioRead32 ((UINTN
)&DebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
411 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
413 return RETURN_DEVICE_ERROR
;
418 // Clearing DONE bit by writing 1
420 MmioOr32 ((UINTN
)&DebugPortRegister
->ControlStatus
, BIT16
);
423 // Check if the request is executed successfully or not.
425 if ((MmioRead32 ((UINTN
)&DebugPortRegister
->ControlStatus
)) & BIT6
) {
426 return RETURN_DEVICE_ERROR
;
430 // Make sure the sent data are not beyond the allowable maximum length - 8 byte
432 if (((MmioRead32 ((UINTN
)&DebugPortRegister
->ControlStatus
)) & 0xF) > USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
433 return RETURN_DEVICE_ERROR
;
436 return RETURN_SUCCESS
;
440 Do a usb control transfer by usb debug port.
442 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
443 @param SetupPacket The token PID for each USB transaction.
444 @param Addr The usb device address for usb transaction.
445 @param Ep The endpoint for usb transaction.
446 @param Data Pointer to the buffer receiving data.
447 @param DataLength Number of bytes of the received data.
449 @retval RETURN_SUCCESS The IN transaction is executed successfully.
450 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
451 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
456 UsbDebugPortControlTransfer (
457 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
458 IN USB_DEVICE_REQUEST
*SetupPacket
,
462 IN OUT UINT8
*DataLength
465 RETURN_STATUS Status
;
467 UINT8 ReturnStatus
[8];
472 Status
= UsbDebugPortOut (DebugPortRegister
, (UINT8
*)SetupPacket
, (UINT8
)sizeof (USB_DEVICE_REQUEST
), SETUP_PID
, Addr
, Ep
, 0);
473 if (RETURN_ERROR (Status
)) {
480 if (DataLength
!= 0) {
481 if ((SetupPacket
->RequestType
& BIT7
) != 0) {
483 // Get Data From Device
485 Status
= UsbDebugPortIn (DebugPortRegister
, Data
, DataLength
, INPUT_PID
, Addr
, Ep
, 1);
486 if (RETURN_ERROR (Status
)) {
491 // Send Data To Device
493 Status
= UsbDebugPortOut (DebugPortRegister
, Data
, *DataLength
, OUTPUT_PID
, Addr
, Ep
, 1);
494 if (RETURN_ERROR (Status
)) {
503 if ((SetupPacket
->RequestType
& BIT7
) != 0) {
505 // For READ operation, Data Toggle in Status Phase Should be 1.
507 Status
= UsbDebugPortOut (DebugPortRegister
, NULL
, 0, OUTPUT_PID
, Addr
, Ep
, 1);
510 // For WRITE operation, Data Toggle in Status Phase Should be 1.
512 Status
= UsbDebugPortIn (DebugPortRegister
, ReturnStatus
, &Temp
, INPUT_PID
, Addr
, Ep
, 1);
519 Check if it needs to re-initialize usb debug port hardware.
521 During different phases switch, such as SEC to PEI or PEI to DXE or DXE to SMM, we should check
522 whether the usb debug port hardware configuration is changed. Such case can be triggered by
523 Pci bus resource allocation and so on.
525 @param Handle Debug port handle.
527 @retval TRUE The usb debug port hardware configuration is changed.
528 @retval FALSE The usb debug port hardware configuration is not changed.
533 NeedReinitializeHardware (
534 IN USB_DEBUG_PORT_HANDLE
*Handle
538 UINT32 UsbDebugPortMemoryBase
;
539 UINT32 EhciMemoryBase
;
541 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
545 EhciMemoryBase
= 0xFFFFFC00 & PciRead32 (PcdGet32 (PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
546 if (EhciMemoryBase
!= Handle
->EhciMemoryBase
) {
547 Handle
->EhciMemoryBase
= EhciMemoryBase
;
551 UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32 (PcdGet32 (PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
->DebugPortBarNumber
* 4);
552 if (UsbDebugPortMemoryBase
!= Handle
->UsbDebugPortMemoryBase
) {
553 Handle
->UsbDebugPortMemoryBase
= UsbDebugPortMemoryBase
;
558 // Enable Ehci Memory Space Access
560 PciCmd
= PciRead16 (PcdGet32 (PcdUsbEhciPciAddress
) + PCI_COMMAND_OFFSET
);
561 if (((PciCmd
& EFI_PCI_COMMAND_MEMORY_SPACE
) == 0) || ((PciCmd
& EFI_PCI_COMMAND_BUS_MASTER
) == 0)) {
562 PciCmd
|= EFI_PCI_COMMAND_MEMORY_SPACE
| EFI_PCI_COMMAND_BUS_MASTER
;
563 PciWrite16 (PcdGet32 (PcdUsbEhciPciAddress
) + PCI_COMMAND_OFFSET
, PciCmd
);
568 // 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.
570 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)((UINTN
)Handle
->UsbDebugPortMemoryBase
+ Handle
->DebugPortOffset
);
571 if ((MmioRead32 ((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_ENABLE
| USB_DEBUG_PORT_IN_USE
))
572 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_ENABLE
| USB_DEBUG_PORT_IN_USE
))
577 if (Handle
->Initialized
== USBDBG_RESET
) {
579 } else if (Handle
->Initialized
!= USBDBG_INIT_DONE
) {
587 Initialize usb debug port hardware.
589 1. reset ehci host controller.
590 2. set right port to debug port.
591 3. find a usb debug device is attached by getting debug device descriptor.
592 4. set address for the usb debug device.
593 5. configure the usb debug device to debug mode.
595 @param Handle Debug port handle.
597 @retval TRUE The usb debug port hardware configuration is changed.
598 @retval FALSE The usb debug port hardware configuration is not changed.
603 InitializeUsbDebugHardware (
604 IN USB_DEBUG_PORT_HANDLE
*Handle
607 RETURN_STATUS Status
;
608 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
609 USB_DEBUG_PORT_DESCRIPTOR UsbDebugPortDescriptor
;
614 UINT8 DebugPortNumber
;
617 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)((UINTN
)Handle
->UsbDebugPortMemoryBase
+ Handle
->DebugPortOffset
);
618 UsbHCSParam
= (UINT32
*)((UINTN
)Handle
->EhciMemoryBase
+ 0x04);
619 UsbCmd
= (UINT32
*)((UINTN
)Handle
->EhciMemoryBase
+ 0x20);
620 UsbStatus
= (UINT32
*)((UINTN
)Handle
->EhciMemoryBase
+ 0x24);
623 // Check if the debug port is enabled and owned by myself.
625 if (((MmioRead32 ((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
))
626 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
)) || (Handle
->Initialized
== USBDBG_RESET
))
630 "UsbDbg: Need to reset the host controller. ControlStatus = %08x\n",
631 MmioRead32 ((UINTN
)&UsbDebugPortRegister
->ControlStatus
)
634 // If the host controller is halted, then reset and restart it.
636 if ((MmioRead32 ((UINTN
)UsbStatus
) & BIT12
) != 0) {
637 DEBUG ((DEBUG_INFO
, "UsbDbg: Reset the host controller.\n"));
639 // reset the host controller.
641 MmioOr32 ((UINTN
)UsbCmd
, BIT1
);
643 // ensure that the host controller is reset.
645 while ((MmioRead32 ((UINTN
)UsbCmd
) & BIT1
) != 0) {
648 MmioOr32 ((UINTN
)UsbCmd
, BIT0
);
649 // ensure that the host controller is started (HALTED bit must be cleared)
650 while ((MmioRead32 ((UINTN
)UsbStatus
) & BIT12
) != 0) {
655 // First get the ownership of port 0.
657 MmioOr32 ((UINTN
)&UsbDebugPortRegister
->ControlStatus
, USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
);
659 MicroSecondDelay (200000);
663 // Find out which port is used as debug port.
665 DebugPortNumber
= (UINT8
)((MmioRead32 ((UINTN
)UsbHCSParam
) & 0x00F00000) >> 20);
667 // Should find a device is connected at debug port
669 PortStatus
= (UINT32
*)((UINTN
)Handle
->EhciMemoryBase
+ 0x64 + (DebugPortNumber
- 1) * 4);
670 if (!(MmioRead32 ((UINTN
)PortStatus
) & BIT0
)) {
671 Handle
->Initialized
= USBDBG_NO_DEV
;
672 return RETURN_NOT_FOUND
;
675 if ((Handle
->Initialized
!= USBDBG_INIT_DONE
) ||
676 ((MmioRead32 ((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & USB_DEBUG_PORT_ENABLE
) == 0))
678 DEBUG ((DEBUG_INFO
, "UsbDbg: Reset the debug port.\n"));
680 // Reset the debug port
682 MmioOr32 ((UINTN
)PortStatus
, BIT8
);
683 MicroSecondDelay (500000);
684 MmioAnd32 ((UINTN
)PortStatus
, (UINT32
) ~BIT8
);
685 while (MmioRead32 ((UINTN
)PortStatus
) & BIT8
) {
689 // The port enabled bit should be set by HW.
691 if ((MmioRead32 ((UINTN
)PortStatus
) & BIT2
) == 0) {
692 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
693 return RETURN_DEVICE_ERROR
;
697 // Enable Usb Debug Port Capability
699 MmioOr32 ((UINTN
)&UsbDebugPortRegister
->ControlStatus
, USB_DEBUG_PORT_ENABLE
);
702 // initialize the data toggle used by bulk in/out endpoint.
704 Handle
->BulkInToggle
= 0;
705 Handle
->BulkOutToggle
= 0;
708 // set usb debug device address as 0x7F.
710 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mDebugCommunicationLibUsbSetDebugAddress
, 0x0, 0x0, NULL
, NULL
);
711 if (RETURN_ERROR (Status
)) {
713 // The device can not work well.
715 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
720 // Start to communicate with Usb Debug Device to see if the attached device is usb debug device or not.
722 Length
= (UINT8
)sizeof (USB_DEBUG_PORT_DESCRIPTOR
);
725 // Get debug descriptor.
727 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mDebugCommunicationLibUsbGetDebugDescriptor
, 0x7F, 0x0, (UINT8
*)&UsbDebugPortDescriptor
, &Length
);
728 if (RETURN_ERROR (Status
)) {
730 // The device is not a usb debug device.
732 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
736 if (Length
!= sizeof (USB_DEBUG_PORT_DESCRIPTOR
)) {
737 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
738 return RETURN_DEVICE_ERROR
;
742 // Determine the usb debug device endpoints.
744 Handle
->InEndpoint
= UsbDebugPortDescriptor
.DebugInEndpoint
;
745 Handle
->OutEndpoint
= UsbDebugPortDescriptor
.DebugOutEndpoint
;
748 // enable the usb debug feature.
750 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mDebugCommunicationLibUsbSetDebugFeature
, 0x7F, 0x0, NULL
, NULL
);
751 if (RETURN_ERROR (Status
)) {
753 // The device can not work well.
755 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
759 Handle
->Initialized
= USBDBG_DBG_CAB
;
763 // Set initialized flag
765 Handle
->Initialized
= USBDBG_INIT_DONE
;
767 return RETURN_SUCCESS
;
771 Read data from debug device and save the data in a buffer.
773 Reads NumberOfBytes data bytes from a debug device into the buffer
774 specified by Buffer. The number of bytes actually read is returned.
775 If the return value is less than NumberOfBytes, then the rest operation failed.
776 If NumberOfBytes is zero, then return 0.
778 @param Handle Debug port handle.
779 @param Buffer Pointer to the data buffer to store the data read from the debug device.
780 @param NumberOfBytes Number of bytes which will be read.
781 @param Timeout Timeout value for reading from debug device. It unit is Microsecond.
783 @retval 0 Read data failed, no data is to be read.
784 @retval >0 Actual number of bytes read from debug device.
789 DebugPortReadBuffer (
790 IN DEBUG_PORT_HANDLE Handle
,
792 IN UINTN NumberOfBytes
,
796 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
797 RETURN_STATUS Status
;
800 if ((NumberOfBytes
!= 1) || (Buffer
== NULL
) || (Timeout
!= 0)) {
805 // If Handle is NULL, it means memory is ready for use.
806 // Use global variable to store handle value.
808 if (Handle
== NULL
) {
809 UsbDebugPortHandle
= &mDebugCommunicationLibUsbDebugPortHandle
;
811 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
814 if (NeedReinitializeHardware (UsbDebugPortHandle
)) {
815 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
816 if (RETURN_ERROR (Status
)) {
822 // Read data from buffer
824 if (UsbDebugPortHandle
->DataCount
< 1) {
827 *Buffer
= UsbDebugPortHandle
->Data
[0];
828 for (Index
= 0; Index
< UsbDebugPortHandle
->DataCount
- 1; Index
++) {
829 if ((Index
+ 1) >= USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
833 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortHandle
->Data
[Index
+ 1];
836 UsbDebugPortHandle
->DataCount
= (UINT8
)(UsbDebugPortHandle
->DataCount
- 1);
842 Write data from buffer to debug device.
844 Writes NumberOfBytes data bytes from Buffer to the debug device.
845 The number of bytes actually written to the debug device is returned.
846 If the return value is less than NumberOfBytes, then the write operation failed.
847 If NumberOfBytes is zero, then return 0.
849 @param Handle Debug port handle.
850 @param Buffer Pointer to the data buffer to be written.
851 @param NumberOfBytes Number of bytes to written to the debug device.
853 @retval 0 NumberOfBytes is 0.
854 @retval >0 The number of bytes written to the debug device.
855 If this value is less than NumberOfBytes, then the read operation failed.
860 DebugPortWriteBuffer (
861 IN DEBUG_PORT_HANDLE Handle
,
863 IN UINTN NumberOfBytes
866 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
867 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
868 RETURN_STATUS Status
;
873 if ((NumberOfBytes
== 0) || (Buffer
== NULL
)) {
881 // If Handle is NULL, it means memory is ready for use.
882 // Use global variable to store handle value.
884 if (Handle
== NULL
) {
885 UsbDebugPortHandle
= &mDebugCommunicationLibUsbDebugPortHandle
;
887 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
890 if (NeedReinitializeHardware (UsbDebugPortHandle
)) {
891 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
892 if (RETURN_ERROR (Status
)) {
897 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)((UINTN
)UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
899 while ((Total
< NumberOfBytes
)) {
900 if (NumberOfBytes
- Total
> USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
901 Sent
= USB_DEBUG_PORT_MAX_PACKET_SIZE
;
903 Sent
= (UINT8
)(NumberOfBytes
- Total
);
906 Status
= UsbDebugPortOut (UsbDebugPortRegister
, Buffer
+ Total
, Sent
, OUTPUT_PID
, 0x7F, UsbDebugPortHandle
->OutEndpoint
, UsbDebugPortHandle
->BulkOutToggle
);
908 if (RETURN_ERROR (Status
)) {
912 ReceivedPid
= (MmioRead8 ((UINTN
)&UsbDebugPortRegister
->ReceivedPid
));
914 // If received a NAK_PID on write transaction, it means the usb debug device is busy and can not handle this transaction.
915 // should send the packet again.
917 if (ReceivedPid
== NAK_PID
) {
920 UsbDebugPortHandle
->BulkOutToggle
^= 1;
930 Polls a debug device to see if there is any data waiting to be read.
932 Polls a debug device to see if there is any data waiting to be read.
933 If there is data waiting to be read from the debug device, then TRUE is returned.
934 If there is no data waiting to be read from the debug device, then FALSE is returned.
936 @param Handle Debug port handle.
938 @retval TRUE Data is waiting to be read from the debug device.
939 @retval FALSE There is no data waiting to be read from the serial device.
944 DebugPortPollBuffer (
945 IN DEBUG_PORT_HANDLE Handle
948 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
949 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
952 RETURN_STATUS Status
;
955 // If Handle is NULL, it means memory is ready for use.
956 // Use global variable to store handle value.
958 if (Handle
== NULL
) {
959 UsbDebugPortHandle
= &mDebugCommunicationLibUsbDebugPortHandle
;
961 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
964 if (NeedReinitializeHardware (UsbDebugPortHandle
)) {
965 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
966 if (RETURN_ERROR (Status
)) {
972 // If the data buffer is not empty, then return TRUE directly.
973 // else initialize a usb read transaction and read data to the data buffer.
975 if (UsbDebugPortHandle
->DataCount
!= 0) {
979 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)((UINTN
)UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
981 UsbDebugPortRegister
->TokenPid
= INPUT_PID
;
982 if (UsbDebugPortHandle
->BulkInToggle
== 0) {
983 UsbDebugPortRegister
->SendPid
= DATA0_PID
;
985 UsbDebugPortRegister
->SendPid
= DATA1_PID
;
988 UsbDebugPortRegister
->UsbAddress
= 0x7F;
989 UsbDebugPortRegister
->UsbEndPoint
= UsbDebugPortHandle
->InEndpoint
& 0x0F;
992 // Clearing W/R bit to indicate it's a READ operation
994 MmioAnd32 ((UINTN
)&UsbDebugPortRegister
->ControlStatus
, (UINT32
) ~BIT4
);
996 // Setting GO bit as well as clearing DONE bit
998 MmioOr32 ((UINTN
)&UsbDebugPortRegister
->ControlStatus
, (UINT32
)BIT5
);
1001 // Wait for completing the request
1003 while ((MmioRead32 ((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (UINT32
)BIT16
) == 0) {
1004 if ((MmioRead32 ((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
1005 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
1011 if ((MmioRead32 ((UINTN
)&UsbDebugPortRegister
->ControlStatus
)) & BIT6
) {
1015 Length
= (UINT8
)(MmioRead32 ((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & 0xF);
1021 UsbDebugPortHandle
->BulkInToggle
^= 1;
1027 for (Index
= 0; Index
< Length
; Index
++) {
1028 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortRegister
->DataBuffer
[Index
];
1031 UsbDebugPortHandle
->DataCount
= Length
;
1037 Initialize the debug port.
1039 If Function is not NULL, Debug Communication Library will call this function
1040 by passing in the Context to be the first parameter. If needed, Debug Communication
1041 Library will create one debug port handle to be the second argument passing in
1042 calling the Function, otherwise it will pass NULL to be the second argument of
1045 If Function is NULL, and Context is not NULL, the Debug Communication Library could
1046 a) Return the same handle as passed in (as Context parameter).
1047 b) Ignore the input Context parameter and create new handle to be returned.
1049 If parameter Function is NULL and Context is NULL, Debug Communication Library could
1050 created a new handle if needed and return it, otherwise it will return NULL.
1052 @param[in] Context Context needed by callback function; it was optional.
1053 @param[in] Function Continue function called by Debug Communication library;
1056 @return The debug port handle created by Debug Communication Library if Function
1062 DebugPortInitialize (
1064 IN DEBUG_PORT_CONTINUE Function
1067 RETURN_STATUS Status
;
1068 USB_DEBUG_PORT_HANDLE Handle
;
1071 // Validate the PCD PcdDebugPortHandleBufferSize value
1073 ASSERT (PcdGet16 (PcdDebugPortHandleBufferSize
) == sizeof (USB_DEBUG_PORT_HANDLE
));
1075 if ((Function
== NULL
) && (Context
!= NULL
)) {
1076 return (DEBUG_PORT_HANDLE
*)Context
;
1079 ZeroMem (&Handle
, sizeof (USB_DEBUG_PORT_HANDLE
));
1081 Status
= CalculateUsbDebugPortBar (&Handle
.DebugPortOffset
, &Handle
.DebugPortBarNumber
);
1082 if (RETURN_ERROR (Status
)) {
1083 DEBUG ((DEBUG_ERROR
, "UsbDbg: the pci device pointed by PcdUsbEhciPciAddress is not EHCI host controller or does not support debug port capability!\n"));
1087 Handle
.EhciMemoryBase
= 0xFFFFFC00 & PciRead32 (PcdGet32 (PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
1089 if (Handle
.EhciMemoryBase
== 0) {
1091 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
1093 PciWrite32 (PcdGet32 (PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
, PcdGet32 (PcdUsbEhciMemorySpaceBase
));
1094 Handle
.EhciMemoryBase
= 0xFFFFFC00 & PciRead32 (PcdGet32 (PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
1097 Handle
.UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32 (PcdGet32 (PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4);
1099 if (Handle
.UsbDebugPortMemoryBase
== 0) {
1101 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
1103 PciWrite32 (PcdGet32 (PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4, PcdGet32 (PcdUsbDebugPortMemorySpaceBase
));
1104 Handle
.UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32 (PcdGet32 (PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4);
1107 Handle
.Initialized
= USBDBG_RESET
;
1109 if (NeedReinitializeHardware (&Handle
)) {
1110 DEBUG ((DEBUG_ERROR
, "UsbDbg: Start EHCI debug port initialization!\n"));
1111 Status
= InitializeUsbDebugHardware (&Handle
);
1112 if (RETURN_ERROR (Status
)) {
1113 DEBUG ((DEBUG_ERROR
, "UsbDbg: Failed, please check if USB debug cable is plugged into EHCI debug port correctly!\n"));
1120 if (Function
!= NULL
) {
1121 Function (Context
, &Handle
);
1123 CopyMem (&mDebugCommunicationLibUsbDebugPortHandle
, &Handle
, sizeof (USB_DEBUG_PORT_HANDLE
));
1126 return (DEBUG_PORT_HANDLE
)(UINTN
)&mDebugCommunicationLibUsbDebugPortHandle
;