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 usb debug device In endpoint.
139 // The usb debug device Out endpoint.
143 // The Bulk In endpoint toggle bit.
147 // The Bulk Out endpoint toggle bit.
151 // The available data length in the following data buffer.
155 // The data buffer. Maximum length is 8 bytes.
158 } USB_DEBUG_PORT_HANDLE
;
162 // The global variable which can be used after memory is ready.
164 USB_DEBUG_PORT_HANDLE mDebugCommunicationLibUsbDebugPortHandle
;
167 Calculate the usb debug port bar address.
169 @param DebugPortOffset Get usb debug port offset in the usb debug port memory space.
170 @param DebugPortBarNumbar Get the bar number at which usb debug port is located.
172 @retval RETURN_UNSUPPORTED The usb host controller does not supported usb debug port capability.
173 @retval RETURN_SUCCESS Get bar and offset successfully.
178 CalculateUsbDebugPortBar (
179 OUT UINT16
*DebugPortOffset
,
180 OUT UINT8
*DebugPortBarNumbar
192 VendorId
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_VENDOR_ID_OFFSET
);
193 DeviceId
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_DEVICE_ID_OFFSET
);
195 if ((VendorId
== 0xFFFF) || (DeviceId
== 0xFFFF)) {
196 return RETURN_UNSUPPORTED
;
199 ProgInterface
= PciRead8 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_CLASSCODE_OFFSET
);
200 SubClassCode
= PciRead8 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_CLASSCODE_OFFSET
+ 1);
201 BaseCode
= PciRead8 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_CLASSCODE_OFFSET
+ 2);
203 if ((ProgInterface
!= PCI_IF_EHCI
) || (SubClassCode
!= PCI_CLASS_SERIAL_USB
) || (BaseCode
!= PCI_CLASS_SERIAL
)) {
204 return RETURN_UNSUPPORTED
;
208 // Enable Ehci Host Controller MMIO Space.
210 PciStatus
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_PRIMARY_STATUS_OFFSET
);
212 if ((PciStatus
& EFI_PCI_STATUS_CAPABILITY
) == 0) {
214 // The Pci Device Doesn't Support Capability Pointer.
216 return RETURN_UNSUPPORTED
;
220 // Get Pointer To Capability List
222 CapabilityPtr
= PciRead8(PcdGet32(PcdUsbEhciPciAddress
) + PCI_CAPBILITY_POINTER_OFFSET
);
225 // Find Capability ID 0xA, Which Is For Debug Port
227 while (CapabilityPtr
!= 0) {
228 CapabilityId
= PciRead8(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
);
229 if (CapabilityId
== PCI_CAPABILITY_ID_DEBUG_PORT
) {
232 CapabilityPtr
= PciRead8(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
+ 1);
236 // No Debug Port Capability Found
238 if (CapabilityPtr
== 0) {
239 return RETURN_UNSUPPORTED
;
243 // Get The Base Address Of Debug Port Register In Debug Port Capability Register
245 *DebugPortOffset
= (UINT16
)(PciRead16(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
+ 2) & 0x1FFF);
246 *DebugPortBarNumbar
= (UINT8
)((PciRead16(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
+ 2) >> 13) - 1);
248 return RETURN_SUCCESS
;
252 Do a usb IN transaction by usb debug port.
254 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
255 @param Buffer Pointer to the buffer receiving data.
256 @param Length Number of bytes of the received data.
257 @param Token The token PID for each USB transaction.
258 @param Addr The usb device address for usb transaction.
259 @param Ep The endpoint for usb transaction.
260 @param DataToggle The toggle bit used at usb transaction.
262 @retval RETURN_SUCCESS The IN transaction is executed successfully.
263 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
264 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
270 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
271 IN OUT UINT8
*Buffer
,
281 if (Length
== NULL
) {
282 return RETURN_INVALID_PARAMETER
;
286 DebugPortRegister
->TokenPid
= Token
;
287 if (DataToggle
!= 0) {
288 DebugPortRegister
->SendPid
= DATA1_PID
;
290 DebugPortRegister
->SendPid
= DATA0_PID
;
293 DebugPortRegister
->UsbAddress
= (UINT8
)(Addr
& 0x7F);
294 DebugPortRegister
->UsbEndPoint
= (UINT8
)(Ep
& 0xF);
297 // Clearing W/R bit to indicate it's a READ operation
299 MmioAnd32((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)~BIT4
);
302 // Setting GO bit as well as clearing DONE bit
304 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)BIT5
);
307 // Wait for completing the request
309 while ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & (UINT32
)BIT16
) == 0) {
310 if ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
311 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
)) {
312 return RETURN_DEVICE_ERROR
;
317 // Clearing DONE bit by writing 1
319 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, BIT16
);
322 // Check if the request is executed successfully or not.
324 if ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & BIT6
) {
325 return RETURN_DEVICE_ERROR
;
329 // Make sure the received data are not beyond the allowable maximum length - 8 byte
331 if (((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & 0xF) > USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
332 return RETURN_DEVICE_ERROR
;
335 *Length
= (UINT8
)(MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & 0xF);
337 return RETURN_DEVICE_ERROR
;
340 for (Index
= 0; Index
< *Length
; Index
++) {
341 Buffer
[Index
] = DebugPortRegister
->DataBuffer
[Index
];
343 return RETURN_SUCCESS
;
347 Do a usb SETUP/OUT transaction by usb debug port.
349 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
350 @param Buffer Pointer to the buffer receiving data.
351 @param Length Number of bytes of the received data.
352 @param Token The token PID for each USB transaction.
353 @param Addr The usb device address for usb transaction.
354 @param Ep The endpoint for usb transaction.
355 @param DataToggle The toggle bit used at usb transaction.
357 @retval RETURN_SUCCESS The IN transaction is executed successfully.
358 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
359 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
365 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
377 return RETURN_INVALID_PARAMETER
;
380 DebugPortRegister
->TokenPid
= Token
;
381 if (DataToggle
!= 0) {
382 DebugPortRegister
->SendPid
= DATA1_PID
;
384 DebugPortRegister
->SendPid
= DATA0_PID
;
386 DebugPortRegister
->UsbAddress
= (UINT8
)(Addr
& 0x7F);
387 DebugPortRegister
->UsbEndPoint
= (UINT8
)(Ep
& 0xF);
390 // Fill in the data length and corresponding data.
392 MmioAnd32((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)~0xF);
393 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, Length
& 0xF);
394 for (Index
= 0; Index
< Length
; Index
++) {
395 DebugPortRegister
->DataBuffer
[Index
] = Buffer
[Index
];
399 // Setting W/R bit to indicate it's a WRITE operation
401 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, BIT4
);
403 // Setting GO bit as well as clearing DONE bit
405 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, BIT5
);
408 // Wait for completing the request
410 while ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & BIT16
) == 0) {
411 if ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
412 != (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
)) {
576 if (Handle
->Initialized
== USBDBG_RESET
) {
578 } else if (Handle
->Initialized
!= USBDBG_INIT_DONE
) {
585 Initialize usb debug port hardware.
587 1. reset ehci host controller.
588 2. set right port to debug port.
589 3. find a usb debug device is attached by getting debug device descriptor.
590 4. set address for the usb debug device.
591 5. configure the usb debug device to debug mode.
593 @param Handle Debug port handle.
595 @retval TRUE The usb debug port hardware configuration is changed.
596 @retval FALSE The usb debug port hardware configuration is not changed.
601 InitializeUsbDebugHardware (
602 IN USB_DEBUG_PORT_HANDLE
*Handle
605 RETURN_STATUS Status
;
606 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
607 USB_DEBUG_PORT_DESCRIPTOR UsbDebugPortDescriptor
;
612 UINT8 DebugPortNumber
;
615 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)((UINTN
)Handle
->UsbDebugPortMemoryBase
+ Handle
->DebugPortOffset
);
616 UsbHCSParam
= (UINT32
*)((UINTN
)Handle
->EhciMemoryBase
+ 0x04);
617 UsbCmd
= (UINT32
*)((UINTN
)Handle
->EhciMemoryBase
+ 0x20);
618 UsbStatus
= (UINT32
*)((UINTN
)Handle
->EhciMemoryBase
+ 0x24);
621 // Check if the debug port is enabled and owned by myself.
623 if (((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
))
624 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
)) || (Handle
->Initialized
== USBDBG_RESET
)) {
627 "UsbDbg: Need to reset the host controller. ControlStatus = %08x\n",
628 MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
)
631 // If the host controller is halted, then reset and restart it.
633 if ((MmioRead32((UINTN
)UsbStatus
) & BIT12
) != 0) {
634 DEBUG ((EFI_D_INFO
, "UsbDbg: Reset the host controller.\n"));
636 // reset the host controller.
638 MmioOr32((UINTN
)UsbCmd
, BIT1
);
640 // ensure that the host controller is reset.
642 while ((MmioRead32((UINTN
)UsbCmd
) & BIT1
) != 0);
644 MmioOr32((UINTN
)UsbCmd
, BIT0
);
645 // ensure that the host controller is started (HALTED bit must be cleared)
646 while ((MmioRead32((UINTN
)UsbStatus
) & BIT12
) != 0);
650 // First get the ownership of port 0.
652 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
);
654 MicroSecondDelay (200000);
657 // Find out which port is used as debug port.
659 DebugPortNumber
= (UINT8
)((MmioRead32((UINTN
)UsbHCSParam
) & 0x00F00000) >> 20);
661 // Should find a device is connected at debug port
663 PortStatus
= (UINT32
*)((UINTN
)Handle
->EhciMemoryBase
+ 0x64 + (DebugPortNumber
- 1) * 4);
664 if (!(MmioRead32((UINTN
)PortStatus
) & BIT0
)) {
665 Handle
->Initialized
= USBDBG_NO_DEV
;
666 return RETURN_NOT_FOUND
;
669 if (Handle
->Initialized
!= USBDBG_INIT_DONE
||
670 (MmioRead32 ((UINTN
) &UsbDebugPortRegister
->ControlStatus
) & USB_DEBUG_PORT_ENABLE
) == 0) {
671 DEBUG ((EFI_D_INFO
, "UsbDbg: Reset the debug port.\n"));
673 // Reset the debug port
675 MmioOr32((UINTN
)PortStatus
, BIT8
);
676 MicroSecondDelay (500000);
677 MmioAnd32((UINTN
)PortStatus
, (UINT32
)~BIT8
);
678 while (MmioRead32((UINTN
)PortStatus
) & BIT8
);
681 // The port enabled bit should be set by HW.
683 if ((MmioRead32((UINTN
)PortStatus
) & BIT2
) == 0) {
684 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
685 return RETURN_DEVICE_ERROR
;
689 // Enable Usb Debug Port Capability
691 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, USB_DEBUG_PORT_ENABLE
);
694 // initialize the data toggle used by bulk in/out endpoint.
696 Handle
->BulkInToggle
= 0;
697 Handle
->BulkOutToggle
= 0;
700 // set usb debug device address as 0x7F.
702 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mDebugCommunicationLibUsbSetDebugAddress
, 0x0, 0x0, NULL
, NULL
);
703 if (RETURN_ERROR(Status
)) {
705 // The device can not work well.
707 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
712 // Start to communicate with Usb Debug Device to see if the attached device is usb debug device or not.
714 Length
= (UINT8
)sizeof (USB_DEBUG_PORT_DESCRIPTOR
);
717 // Get debug descriptor.
719 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mDebugCommunicationLibUsbGetDebugDescriptor
, 0x7F, 0x0, (UINT8
*)&UsbDebugPortDescriptor
, &Length
);
720 if (RETURN_ERROR(Status
)) {
722 // The device is not a usb debug device.
724 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
728 if (Length
!= sizeof(USB_DEBUG_PORT_DESCRIPTOR
)) {
729 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
730 return RETURN_DEVICE_ERROR
;
734 // Determine the usb debug device endpoints.
736 Handle
->InEndpoint
= UsbDebugPortDescriptor
.DebugInEndpoint
;
737 Handle
->OutEndpoint
= UsbDebugPortDescriptor
.DebugOutEndpoint
;
740 // enable the usb debug feature.
742 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mDebugCommunicationLibUsbSetDebugFeature
, 0x7F, 0x0, NULL
, NULL
);
743 if (RETURN_ERROR(Status
)) {
745 // The device can not work well.
747 Handle
->Initialized
= USBDBG_NO_DBG_CAB
;
751 Handle
->Initialized
= USBDBG_DBG_CAB
;
755 // Set initialized flag
757 Handle
->Initialized
= USBDBG_INIT_DONE
;
759 return RETURN_SUCCESS
;
763 Read data from debug device and save the datas in buffer.
765 Reads NumberOfBytes data bytes from a debug device into the buffer
766 specified by Buffer. The number of bytes actually read is returned.
767 If the return value is less than NumberOfBytes, then the rest operation failed.
768 If NumberOfBytes is zero, then return 0.
770 @param Handle Debug port handle.
771 @param Buffer Pointer to the data buffer to store the data read from the debug device.
772 @param NumberOfBytes Number of bytes which will be read.
773 @param Timeout Timeout value for reading from debug device. It unit is Microsecond.
775 @retval 0 Read data failed, no data is to be read.
776 @retval >0 Actual number of bytes read from debug device.
781 DebugPortReadBuffer (
782 IN DEBUG_PORT_HANDLE Handle
,
784 IN UINTN NumberOfBytes
,
788 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
789 RETURN_STATUS Status
;
792 if (NumberOfBytes
!= 1 || Buffer
== NULL
|| Timeout
!= 0) {
797 // If Handle is NULL, it means memory is ready for use.
798 // Use global variable to store handle value.
800 if (Handle
== NULL
) {
801 UsbDebugPortHandle
= &mDebugCommunicationLibUsbDebugPortHandle
;
803 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
806 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
807 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
808 if (RETURN_ERROR(Status
)) {
814 // Read data from buffer
816 if (UsbDebugPortHandle
->DataCount
< 1) {
819 *Buffer
= UsbDebugPortHandle
->Data
[0];
820 for (Index
= 0; Index
< UsbDebugPortHandle
->DataCount
- 1; Index
++) {
821 if ((Index
+ 1) >= USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
824 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortHandle
->Data
[Index
+ 1];
826 UsbDebugPortHandle
->DataCount
= (UINT8
)(UsbDebugPortHandle
->DataCount
- 1);
832 Write data from buffer to debug device.
834 Writes NumberOfBytes data bytes from Buffer to the debug device.
835 The number of bytes actually written to the debug device is returned.
836 If the return value is less than NumberOfBytes, then the write operation failed.
837 If NumberOfBytes is zero, then return 0.
839 @param Handle Debug port handle.
840 @param Buffer Pointer to the data buffer to be written.
841 @param NumberOfBytes Number of bytes to written to the debug device.
843 @retval 0 NumberOfBytes is 0.
844 @retval >0 The number of bytes written to the debug device.
845 If this value is less than NumberOfBytes, then the read operation failed.
850 DebugPortWriteBuffer (
851 IN DEBUG_PORT_HANDLE Handle
,
853 IN UINTN NumberOfBytes
856 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
857 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
858 RETURN_STATUS Status
;
863 if (NumberOfBytes
== 0 || Buffer
== NULL
) {
871 // If Handle is NULL, it means memory is ready for use.
872 // Use global variable to store handle value.
874 if (Handle
== NULL
) {
875 UsbDebugPortHandle
= &mDebugCommunicationLibUsbDebugPortHandle
;
877 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
880 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
881 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
882 if (RETURN_ERROR(Status
)) {
887 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)((UINTN
)UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
889 while ((Total
< NumberOfBytes
)) {
890 if (NumberOfBytes
- Total
> USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
891 Sent
= USB_DEBUG_PORT_MAX_PACKET_SIZE
;
893 Sent
= (UINT8
)(NumberOfBytes
- Total
);
896 Status
= UsbDebugPortOut(UsbDebugPortRegister
, Buffer
+ Total
, Sent
, OUTPUT_PID
, 0x7F, UsbDebugPortHandle
->OutEndpoint
, UsbDebugPortHandle
->BulkOutToggle
);
898 if (RETURN_ERROR(Status
)) {
902 ReceivedPid
= (MmioRead8((UINTN
)&UsbDebugPortRegister
->ReceivedPid
));
904 // If received a NAK_PID on write transaction, it means the usb debug device is busy and can not handle this transaction.
905 // should send the packet again.
907 if (ReceivedPid
== NAK_PID
) {
910 UsbDebugPortHandle
->BulkOutToggle
^= 1;
918 Polls a debug device to see if there is any data waiting to be read.
920 Polls a debug device to see if there is any data waiting to be read.
921 If there is data waiting to be read from the debug device, then TRUE is returned.
922 If there is no data waiting to be read from the debug device, then FALSE is returned.
924 @param Handle Debug port handle.
926 @retval TRUE Data is waiting to be read from the debug device.
927 @retval FALSE There is no data waiting to be read from the serial device.
932 DebugPortPollBuffer (
933 IN DEBUG_PORT_HANDLE Handle
936 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
937 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
940 RETURN_STATUS Status
;
943 // If Handle is NULL, it means memory is ready for use.
944 // Use global variable to store handle value.
946 if (Handle
== NULL
) {
947 UsbDebugPortHandle
= &mDebugCommunicationLibUsbDebugPortHandle
;
949 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
952 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
953 Status
= InitializeUsbDebugHardware(UsbDebugPortHandle
);
954 if (RETURN_ERROR(Status
)) {
960 // If the data buffer is not empty, then return TRUE directly.
961 // else initialize a usb read transaction and read data to the data buffer.
963 if (UsbDebugPortHandle
->DataCount
!= 0) {
967 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)((UINTN
)UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
969 UsbDebugPortRegister
->TokenPid
= INPUT_PID
;
970 if (UsbDebugPortHandle
->BulkInToggle
== 0) {
971 UsbDebugPortRegister
->SendPid
= DATA0_PID
;
973 UsbDebugPortRegister
->SendPid
= DATA1_PID
;
975 UsbDebugPortRegister
->UsbAddress
= 0x7F;
976 UsbDebugPortRegister
->UsbEndPoint
= UsbDebugPortHandle
->InEndpoint
& 0x0F;
979 // Clearing W/R bit to indicate it's a READ operation
981 MmioAnd32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, (UINT32
)~BIT4
);
983 // Setting GO bit as well as clearing DONE bit
985 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, (UINT32
)BIT5
);
988 // Wait for completing the request
990 while ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (UINT32
)BIT16
) == 0) {
991 if ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
))
992 != (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_IN_USE
| USB_DEBUG_PORT_ENABLE
)) {
997 if ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
)) & BIT6
) {
1001 Length
= (UINT8
)(MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & 0xF);
1007 UsbDebugPortHandle
->BulkInToggle
^= 1;
1013 for (Index
= 0; Index
< Length
; Index
++) {
1014 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortRegister
->DataBuffer
[Index
];
1016 UsbDebugPortHandle
->DataCount
= Length
;
1022 Initialize the debug port.
1024 If Function is not NULL, Debug Communication Library will call this function
1025 by passing in the Context to be the first parameter. If needed, Debug Communication
1026 Library will create one debug port handle to be the second argument passing in
1027 calling the Function, otherwise it will pass NULL to be the second argument of
1030 If Function is NULL, and Context is not NULL, the Debug Communication Library could
1031 a) Return the same handle as passed in (as Context parameter).
1032 b) Ignore the input Context parameter and create new handle to be returned.
1034 If parameter Function is NULL and Context is NULL, Debug Communication Library could
1035 created a new handle if needed and return it, otherwise it will return NULL.
1037 @param[in] Context Context needed by callback function; it was optional.
1038 @param[in] Function Continue function called by Debug Communication library;
1041 @return The debug port handle created by Debug Communication Library if Function
1047 DebugPortInitialize (
1049 IN DEBUG_PORT_CONTINUE Function
1052 RETURN_STATUS Status
;
1053 USB_DEBUG_PORT_HANDLE Handle
;
1056 // Validate the PCD PcdDebugPortHandleBufferSize value
1058 ASSERT (PcdGet16 (PcdDebugPortHandleBufferSize
) == sizeof (USB_DEBUG_PORT_HANDLE
));
1060 if (Function
== NULL
&& Context
!= NULL
) {
1061 return (DEBUG_PORT_HANDLE
*) Context
;
1063 ZeroMem(&Handle
, sizeof (USB_DEBUG_PORT_HANDLE
));
1065 Status
= CalculateUsbDebugPortBar(&Handle
.DebugPortOffset
, &Handle
.DebugPortBarNumber
);
1066 if (RETURN_ERROR (Status
)) {
1067 DEBUG ((EFI_D_ERROR
, "UsbDbg: the pci device pointed by PcdUsbEhciPciAddress is not EHCI host controller or does not support debug port capability!\n"));
1071 Handle
.EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
1073 if (Handle
.EhciMemoryBase
== 0) {
1075 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
1077 PciWrite32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
, PcdGet32(PcdUsbEhciMemorySpaceBase
));
1078 Handle
.EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
1081 Handle
.UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4);
1083 if (Handle
.UsbDebugPortMemoryBase
== 0) {
1085 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
1087 PciWrite32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4, PcdGet32(PcdUsbDebugPortMemorySpaceBase
));
1088 Handle
.UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4);
1091 Handle
.Initialized
= USBDBG_RESET
;
1093 if (NeedReinitializeHardware(&Handle
)) {
1094 DEBUG ((EFI_D_ERROR
, "UsbDbg: Start EHCI debug port initialization!\n"));
1095 Status
= InitializeUsbDebugHardware (&Handle
);
1096 if (RETURN_ERROR(Status
)) {
1097 DEBUG ((EFI_D_ERROR
, "UsbDbg: Failed, please check if USB debug cable is plugged into EHCI debug port correctly!\n"));
1104 if (Function
!= NULL
) {
1105 Function (Context
, &Handle
);
1107 CopyMem(&mDebugCommunicationLibUsbDebugPortHandle
, &Handle
, sizeof (USB_DEBUG_PORT_HANDLE
));
1110 return (DEBUG_PORT_HANDLE
)(UINTN
)&mDebugCommunicationLibUsbDebugPortHandle
;