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>
26 #define SETUP_PID 0x2D
27 #define INPUT_PID 0x69
28 #define OUTPUT_PID 0xE1
29 #define ERROR_PID 0x55
30 #define DATA0_PID 0xC3
31 #define DATA1_PID 0x4B
32 #define DATA2_PID 0x87
33 #define MDATA_PID 0x0F
36 #define STALL_PID 0x1E
39 #define PCI_CAPABILITY_ID_DEBUG_PORT 0x0A
40 #define USB_DEBUG_PORT_MAX_PACKET_SIZE 0x08
42 #define USB_DEBUG_PORT_IN_USE BIT10
43 #define USB_DEBUG_PORT_ENABLE BIT28
44 #define USB_DEBUG_PORT_OWNER BIT30
46 #define USB_PORT_LINE_STATUS_LS 0x400
47 #define USB_PORT_LINE_STATUS_MASK 0xC00
50 // Usb debug device descriptor, which is defined at
51 // USB2 Debug Device Specification.
53 typedef struct _USB_DEBUG_PORT_DESCRIPTOR
{
56 UINT8 DebugInEndpoint
;
57 UINT8 DebugOutEndpoint
;
58 }USB_DEBUG_PORT_DESCRIPTOR
;
60 USB_DEVICE_REQUEST mGetDebugDescriptor
= {
62 USB_REQ_GET_DESCRIPTOR
,
65 sizeof(USB_DEBUG_PORT_DESCRIPTOR
)
68 USB_DEVICE_REQUEST mSetDebugFeature
= {
76 USB_DEVICE_REQUEST mSetDebugAddress
= {
85 // Usb debug port register file, which is defined at
86 // EHCI Specification.
88 typedef struct _USB_DEBUG_PORT_REGISTER
{
99 }USB_DEBUG_PORT_REGISTER
;
103 // The internal data structure of DEBUG_PORT_HANDLE, which stores some
104 // important datum which are used across various phases.
106 typedef struct _USB_DEBUG_PORT_HANDLE
{
108 // The usb debug port memory BAR number in EHCI configuration space.
110 UINT8 DebugPortBarNumber
;
113 // The offset of usb debug port registers in EHCI memory range.
115 UINT16 DebugPortOffset
;
117 // The usb debug port memory BAR address.
119 UINTN UsbDebugPortMemoryBase
;
121 // The EHCI memory BAR address.
123 UINTN EhciMemoryBase
;
125 // The Bulk In endpoint toggle bit.
129 // The Bulk Out endpoint toggle bit.
133 // The available data length in the following data buffer.
137 // The data buffer. Maximum length is 8 bytes.
140 } USB_DEBUG_PORT_HANDLE
;
144 // The global variable which can be used after memory is ready.
146 USB_DEBUG_PORT_HANDLE mUsbDebugPortHandle
;
149 Calculate the usb debug port bar address.
151 @param DebugPortOffset Get usb debug port offset in the usb debug port memory space.
152 @param DebugPortBarNumbar Get the bar number at which usb debug port is located.
154 @retval RETURN_UNSUPPORTED The usb host controller does not supported usb debug port capability.
155 @retval RETURN_SUCCESS Get bar and offset successfully.
160 CalculateUsbDebugPortBar (
161 OUT UINT16
*DebugPortOffset
,
162 OUT UINT8
*DebugPortBarNumbar
170 // Enable Ehci Host Controller MMIO Space.
172 PciStatus
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_PRIMARY_STATUS_OFFSET
);
174 if ((PciStatus
& EFI_PCI_STATUS_CAPABILITY
) == 0) {
176 // The Pci Device Doesn't Support Capability Pointer.
178 return RETURN_UNSUPPORTED
;
182 // Get Pointer To Capability List
184 CapabilityPtr
= PciRead8(PcdGet32(PcdUsbEhciPciAddress
) + PCI_CAPBILITY_POINTER_OFFSET
);
187 // Find Capability ID 0xA, Which Is For Debug Port
189 while (CapabilityPtr
!= 0) {
190 CapabilityId
= PciRead8(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
);
191 if (CapabilityId
== PCI_CAPABILITY_ID_DEBUG_PORT
) {
194 CapabilityPtr
= PciRead8(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
+ 1);
198 // No Debug Port Capability Found
200 if (CapabilityPtr
== 0) {
201 return RETURN_UNSUPPORTED
;
205 // Get The Base Address Of Debug Port Register In Debug Port Capability Register
207 *DebugPortOffset
= (UINT16
)(PciRead16(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
+ 2) & 0x1FFF);
208 *DebugPortBarNumbar
= (UINT8
)((PciRead16(PcdGet32(PcdUsbEhciPciAddress
) + CapabilityPtr
+ 2) >> 13) - 1);
210 return RETURN_SUCCESS
;
214 Do a usb IN transaction by usb debug port.
216 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
217 @param Buffer Pointer to the buffer receiving data.
218 @param Length Number of bytes of the received data.
219 @param Token The token PID for each USB transaction.
220 @param Addr The usb device address for usb transaction.
221 @param Ep The endpoint for usb transaction.
222 @param DataToggle The toggle bit used at usb transaction.
224 @retval RETURN_SUCCESS The IN transaction is executed successfully.
225 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
226 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
232 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
233 IN OUT UINT8
*Buffer
,
243 if (Length
== NULL
) {
244 return RETURN_INVALID_PARAMETER
;
248 DebugPortRegister
->TokenPid
= Token
;
249 if (DataToggle
!= 0) {
250 DebugPortRegister
->SendPid
= DATA1_PID
;
252 DebugPortRegister
->SendPid
= DATA0_PID
;
255 DebugPortRegister
->UsbAddress
= (UINT8
)(Addr
& 0x7F);
256 DebugPortRegister
->UsbEndPoint
= (UINT8
)(Ep
& 0xF);
259 // Clearing W/R bit to indicate it's a READ operation
261 MmioAnd32((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)~BIT4
);
264 // Setting GO bit as well as clearing DONE bit
266 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)BIT5
);
269 // Wait for completing the request
271 while ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & (UINT32
)BIT16
) == 0);
274 // Check if the request is executed successfully or not.
276 if ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & BIT6
) {
277 return RETURN_DEVICE_ERROR
;
281 // Make sure the received data are not beyond the allowable maximum length - 8 byte
283 if (((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & 0xF) > USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
284 return RETURN_DEVICE_ERROR
;
287 *Length
= (UINT8
)(MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & 0xF);
288 ASSERT (*Length
<= 8);
289 for (Index
= 0; Index
< *Length
; Index
++) {
290 Buffer
[Index
] = DebugPortRegister
->DataBuffer
[Index
];
292 return RETURN_SUCCESS
;
296 Do a usb SETUP/OUT transaction by usb debug port.
298 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
299 @param Buffer Pointer to the buffer receiving data.
300 @param Length Number of bytes of the received data.
301 @param Token The token PID for each USB transaction.
302 @param Addr The usb device address for usb transaction.
303 @param Ep The endpoint for usb transaction.
304 @param DataToggle The toggle bit used at usb transaction.
306 @retval RETURN_SUCCESS The IN transaction is executed successfully.
307 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
308 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
314 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
326 return RETURN_INVALID_PARAMETER
;
329 DebugPortRegister
->TokenPid
= Token
;
330 if (DataToggle
!= 0) {
331 DebugPortRegister
->SendPid
= DATA1_PID
;
333 DebugPortRegister
->SendPid
= DATA0_PID
;
335 DebugPortRegister
->UsbAddress
= (UINT8
)(Addr
& 0x7F);
336 DebugPortRegister
->UsbEndPoint
= (UINT8
)(Ep
& 0xF);
339 // Fill in the data length and corresponding data.
341 MmioAnd32((UINTN
)&DebugPortRegister
->ControlStatus
, (UINT32
)~0xF);
342 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, Length
& 0xF);
343 for (Index
= 0; Index
< Length
; Index
++) {
344 DebugPortRegister
->DataBuffer
[Index
] = Buffer
[Index
];
348 // Setting W/R bit to indicate it's a WRITE operation
350 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, BIT4
);
352 // Setting GO bit as well as clearing DONE bit
354 MmioOr32((UINTN
)&DebugPortRegister
->ControlStatus
, BIT5
);
357 // Wait for completing the request
359 while ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
) & BIT16
) == 0);
362 // Check if the request is executed successfully or not.
364 if ((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & BIT6
) {
365 return RETURN_DEVICE_ERROR
;
369 // Make sure the sent data are not beyond the allowable maximum length - 8 byte
371 if (((MmioRead32((UINTN
)&DebugPortRegister
->ControlStatus
)) & 0xF) > USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
372 return RETURN_DEVICE_ERROR
;
375 return RETURN_SUCCESS
;
379 Do a usb control transfer by usb debug port.
381 @param DebugPortRegister Pointer to the base address of usb debug port register interface.
382 @param SetupPacket The token PID for each USB transaction.
383 @param Addr The usb device address for usb transaction.
384 @param Ep The endpoint for usb transaction.
385 @param Data Pointer to the buffer receiving data.
386 @param DataLength Number of bytes of the received data.
388 @retval RETURN_SUCCESS The IN transaction is executed successfully.
389 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.
390 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.
395 UsbDebugPortControlTransfer (
396 IN USB_DEBUG_PORT_REGISTER
*DebugPortRegister
,
397 IN USB_DEVICE_REQUEST
*SetupPacket
,
401 IN OUT UINT8
*DataLength
404 RETURN_STATUS Status
;
406 UINT8 ReturnStatus
[8];
411 Status
= UsbDebugPortOut(DebugPortRegister
, (UINT8
*)SetupPacket
, (UINT8
)sizeof(USB_DEVICE_REQUEST
), SETUP_PID
, Addr
, Ep
, 0);
412 if (RETURN_ERROR(Status
)) {
419 if (DataLength
!= 0) {
420 if ((SetupPacket
->RequestType
& BIT7
) != 0) {
422 // Get Data From Device
424 Status
= UsbDebugPortIn(DebugPortRegister
, Data
, DataLength
, INPUT_PID
, Addr
, Ep
, 1);
425 if (RETURN_ERROR(Status
)) {
430 // Send Data To Device
432 Status
= UsbDebugPortOut(DebugPortRegister
, Data
, *DataLength
, OUTPUT_PID
, Addr
, Ep
, 1);
433 if (RETURN_ERROR(Status
)) {
442 if ((SetupPacket
->RequestType
& BIT7
) != 0) {
444 // For READ operation, Data Toggle in Status Phase Should be 1.
446 Status
= UsbDebugPortOut(DebugPortRegister
, NULL
, 0, OUTPUT_PID
, Addr
, Ep
, 1);
449 // For WRITE operation, Data Toggle in Status Phase Should be 1.
451 Status
= UsbDebugPortIn(DebugPortRegister
, ReturnStatus
, &Temp
, INPUT_PID
, Addr
, Ep
, 1);
458 Check if it needs to re-initialize usb debug port hardware.
460 During different phases switch, such as SEC to PEI or PEI to DXE or DXE to SMM, we should check
461 whether the usb debug port hardware configuration is changed. Such case can be triggerred by
462 Pci bus resource allocation and so on.
464 @param Handle Debug port handle.
466 @retval TRUE The usb debug port hardware configuration is changed.
467 @retval FALSE The usb debug port hardware configuration is not changed.
472 NeedReinitializeHardware(
473 IN USB_DEBUG_PORT_HANDLE
*Handle
477 UINTN UsbDebugPortMemoryBase
;
478 UINTN EhciMemoryBase
;
480 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
484 EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
485 if (EhciMemoryBase
!= Handle
->EhciMemoryBase
) {
486 Handle
->EhciMemoryBase
= EhciMemoryBase
;
490 UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
->DebugPortBarNumber
* 4);
491 if (UsbDebugPortMemoryBase
!= Handle
->UsbDebugPortMemoryBase
) {
492 Handle
->UsbDebugPortMemoryBase
= UsbDebugPortMemoryBase
;
497 // Enable Ehci Memory Space Access
499 PciCmd
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_COMMAND_OFFSET
);
500 if (((PciCmd
& EFI_PCI_COMMAND_MEMORY_SPACE
) == 0) || ((PciCmd
& EFI_PCI_COMMAND_BUS_MASTER
) == 0)) {
505 // Check if the debug port is enabled and owned by myself.
507 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)(Handle
->UsbDebugPortMemoryBase
+ Handle
->DebugPortOffset
);
508 if ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) &
509 (USB_DEBUG_PORT_OWNER
| USB_DEBUG_PORT_ENABLE
| USB_DEBUG_PORT_IN_USE
)) == 0) {
516 Initialize usb debug port hardware.
518 1. reset ehci host controller.
519 2. set right port to debug port.
520 3. find a usb debug device is attached by getting debug device descriptor.
521 4. set address for the usb debug device.
522 5. configure the usb debug device to debug mode.
524 @param Handle Debug port handle.
526 @retval TRUE The usb debug port hardware configuration is changed.
527 @retval FALSE The usb debug port hardware configuration is not changed.
532 InitializeUsbDebugHardware (
533 IN USB_DEBUG_PORT_HANDLE
*Handle
536 RETURN_STATUS Status
;
537 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
538 USB_DEBUG_PORT_DESCRIPTOR UsbDebugPortDescriptor
;
544 UINT8 DebugPortNumber
;
547 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)(Handle
->UsbDebugPortMemoryBase
+ Handle
->DebugPortOffset
);
548 PciCmd
= PciRead16 (PcdGet32(PcdUsbEhciPciAddress
) + PCI_COMMAND_OFFSET
);
549 UsbHCSParam
= (UINT32
*)(Handle
->EhciMemoryBase
+ 0x04);
550 UsbCmd
= (UINT32
*)(Handle
->EhciMemoryBase
+ 0x20);
551 UsbStatus
= (UINT32
*)(Handle
->EhciMemoryBase
+ 0x24);
554 // initialize the data toggle used by bulk in/out endpoint.
556 Handle
->BulkInToggle
= 0;
557 Handle
->BulkOutToggle
= 0;
560 // Enable Ehci Memory Space Access
562 if (((PciCmd
& EFI_PCI_COMMAND_MEMORY_SPACE
) == 0) || ((PciCmd
& EFI_PCI_COMMAND_BUS_MASTER
) == 0)) {
563 PciCmd
|= EFI_PCI_COMMAND_MEMORY_SPACE
| EFI_PCI_COMMAND_BUS_MASTER
;
564 PciWrite16(PcdGet32(PcdUsbEhciPciAddress
) + PCI_COMMAND_OFFSET
, PciCmd
);
568 // If the host controller is not halted, then halt it.
570 if ((MmioRead32((UINTN
)UsbStatus
) & BIT12
) == 0) {
571 MmioAnd32((UINTN
)UsbCmd
, (UINT32
)~BIT0
);
572 while ((MmioRead32((UINTN
)UsbStatus
) & BIT12
) == 0);
575 // reset the host controller.
577 MmioOr32((UINTN
)UsbCmd
, BIT1
);
579 // ensure that the host controller is reset.
581 while (MmioRead32((UINTN
)UsbCmd
) & BIT1
);
584 // Start the host controller if it's not running
586 if (MmioRead32((UINTN
)UsbStatus
) & BIT12
) {
587 MmioOr32((UINTN
)UsbCmd
, BIT0
);
588 // ensure that the host controller is started (HALTED bit must be cleared)
589 while (MmioRead32((UINTN
)UsbStatus
) & BIT12
);
593 // First get the ownership of port 0.
595 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, USB_DEBUG_PORT_OWNER
);
597 MicroSecondDelay (200000);
600 // Find out which port is used as debug port.
602 DebugPortNumber
= (UINT8
)((MmioRead32((UINTN
)UsbHCSParam
) & 0x00F00000) >> 20);
604 // Should find a non low-speed device is connected
606 PortStatus
= (UINT32
*)(Handle
->EhciMemoryBase
+ 0x64 + (DebugPortNumber
- 1) * 4);
607 if (!(MmioRead32((UINTN
)PortStatus
) & BIT0
) || ((MmioRead32((UINTN
)PortStatus
) & USB_PORT_LINE_STATUS_MASK
) == USB_PORT_LINE_STATUS_LS
)) {
608 return RETURN_NOT_FOUND
;
612 // Reset the debug port
614 MmioOr32((UINTN
)PortStatus
, BIT8
);
615 MicroSecondDelay (200000);
616 MmioAnd32((UINTN
)PortStatus
, (UINT32
)~BIT8
);
617 while (MmioRead32((UINTN
)PortStatus
) & BIT8
);
620 // The port enabled bit should be set by HW.
622 if ((MmioRead32((UINTN
)PortStatus
) & BIT2
) == 0) {
623 return RETURN_DEVICE_ERROR
;
627 // Enable Usb Debug Port Capability
629 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, USB_DEBUG_PORT_ENABLE
| USB_DEBUG_PORT_IN_USE
);
632 // Start to communicate with Usb Debug Device to see if the attached device is usb debug device or not.
634 Length
= (UINT8
)sizeof (USB_DEBUG_PORT_DESCRIPTOR
);
637 // It's not a dedicated usb debug device, should use address 0 to get debug descriptor.
639 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mGetDebugDescriptor
, 0x0, 0x0, (UINT8
*)&UsbDebugPortDescriptor
, &Length
);
640 if (RETURN_ERROR(Status
)) {
642 // The device is not a usb debug device.
646 ASSERT (Length
== sizeof(USB_DEBUG_PORT_DESCRIPTOR
));
648 // set usb debug device address as 0x7F.
650 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mSetDebugAddress
, 0x0, 0x0, NULL
, NULL
);
651 if (RETURN_ERROR(Status
)) {
653 // The device can not work well.
659 // enable the usb debug feature.
661 Status
= UsbDebugPortControlTransfer (UsbDebugPortRegister
, &mSetDebugFeature
, 0x7F, 0x0, NULL
, NULL
);
667 Read data from debug device and save the datas in buffer.
669 Reads NumberOfBytes data bytes from a debug device into the buffer
670 specified by Buffer. The number of bytes actually read is returned.
671 If the return value is less than NumberOfBytes, then the rest operation failed.
672 If NumberOfBytes is zero, then return 0.
674 @param Handle Debug port handle.
675 @param Buffer Pointer to the data buffer to store the data read from the debug device.
676 @param NumberOfBytes Number of bytes which will be read.
677 @param Timeout Timeout value for reading from debug device. It unit is Microsecond.
679 @retval 0 Read data failed, no data is to be read.
680 @retval >0 Actual number of bytes read from debug device.
685 DebugPortReadBuffer (
686 IN DEBUG_PORT_HANDLE Handle
,
688 IN UINTN NumberOfBytes
,
692 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
693 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
694 RETURN_STATUS Status
;
701 if (NumberOfBytes
== 0 || Buffer
== NULL
) {
710 // If Handle is NULL, it means memory is ready for use.
711 // Use global variable to store handle value.
713 if (Handle
== NULL
) {
714 UsbDebugPortHandle
= &mUsbDebugPortHandle
;
716 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
719 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
720 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
721 if (RETURN_ERROR(Status
)) {
726 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)(UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
729 // First read data from buffer, then read debug port hw to get received data.
731 if (UsbDebugPortHandle
->DataCount
> 0) {
732 if (NumberOfBytes
<= UsbDebugPortHandle
->DataCount
) {
733 Total
= NumberOfBytes
;
735 Total
= UsbDebugPortHandle
->DataCount
;
738 for (Index
= 0; Index
< Total
; Index
++) {
739 Buffer
[Index
] = UsbDebugPortHandle
->Data
[Index
];
742 for (Index
= 0; Index
< UsbDebugPortHandle
->DataCount
- Total
; Index
++) {
743 if (Total
+ Index
>= 8) {
746 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortHandle
->Data
[Total
+ Index
];
748 UsbDebugPortHandle
->DataCount
= (UINT8
)(UsbDebugPortHandle
->DataCount
- (UINT8
)Total
);
752 // If Timeout is equal to 0, then it means it should always wait until all datum required are received.
755 Timeout
= 0xFFFFFFFF;
759 // Read remaining data by executing one or more usb debug transfer transactions at usb debug port hw.
761 while ((Total
< NumberOfBytes
) && (Timeout
!= 0)) {
762 Remaining
= NumberOfBytes
- Total
;
763 if (Remaining
>= USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
764 Status
= UsbDebugPortIn(UsbDebugPortRegister
, Buffer
+ Total
, &Received
, INPUT_PID
, 0x7f, 0x82, UsbDebugPortHandle
->BulkInToggle
);
766 if (RETURN_ERROR(Status
)) {
770 Status
= UsbDebugPortIn(UsbDebugPortRegister
, &UsbDebugPortHandle
->Data
[0], &Received
, INPUT_PID
, 0x7f, 0x82, UsbDebugPortHandle
->BulkInToggle
);
772 if (RETURN_ERROR(Status
)) {
776 UsbDebugPortHandle
->DataCount
= Received
;
778 if (Remaining
<= Received
) {
779 Length
= (UINT8
)Remaining
;
781 Length
= (UINT8
)Received
;
785 // Copy required data from the data buffer to user buffer.
787 for (Index
= 0; Index
< Length
; Index
++) {
788 (Buffer
+ Total
)[Index
] = UsbDebugPortHandle
->Data
[Index
];
789 UsbDebugPortHandle
->DataCount
--;
793 // reorder the data buffer to make available data arranged from the beginning of the data buffer.
795 for (Index
= 0; Index
< Received
- Length
; Index
++) {
796 if (Length
+ Index
>= 8) {
799 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortHandle
->Data
[Length
+ Index
];
802 // fixup the real received length in Buffer.
806 UsbDebugPortHandle
->BulkInToggle
^= 1;
816 Write data from buffer to debug device.
818 Writes NumberOfBytes data bytes from Buffer to the debug device.
819 The number of bytes actually written to the debug device is returned.
820 If the return value is less than NumberOfBytes, then the write operation failed.
821 If NumberOfBytes is zero, then return 0.
823 @param Handle Debug port handle.
824 @param Buffer Pointer to the data buffer to be written.
825 @param NumberOfBytes Number of bytes to written to the debug device.
827 @retval 0 NumberOfBytes is 0.
828 @retval >0 The number of bytes written to the debug device.
829 If this value is less than NumberOfBytes, then the read operation failed.
834 DebugPortWriteBuffer (
835 IN DEBUG_PORT_HANDLE Handle
,
837 IN UINTN NumberOfBytes
840 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
841 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
842 RETURN_STATUS Status
;
847 if (NumberOfBytes
== 0 || Buffer
== NULL
) {
855 // If Handle is NULL, it means memory is ready for use.
856 // Use global variable to store handle value.
858 if (Handle
== NULL
) {
859 UsbDebugPortHandle
= &mUsbDebugPortHandle
;
861 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
864 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
865 Status
= InitializeUsbDebugHardware (UsbDebugPortHandle
);
866 if (RETURN_ERROR(Status
)) {
871 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)(UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
873 while ((Total
< NumberOfBytes
)) {
874 if (NumberOfBytes
- Total
> USB_DEBUG_PORT_MAX_PACKET_SIZE
) {
875 Sent
= USB_DEBUG_PORT_MAX_PACKET_SIZE
;
877 Sent
= (UINT8
)(NumberOfBytes
- Total
);
880 Status
= UsbDebugPortOut(UsbDebugPortRegister
, Buffer
+ Total
, Sent
, OUTPUT_PID
, 0x7F, 0x01, UsbDebugPortHandle
->BulkOutToggle
);
882 if (RETURN_ERROR(Status
)) {
886 ReceivedPid
= (MmioRead8((UINTN
)&UsbDebugPortRegister
->ReceivedPid
));
888 // If received a NAK_PID on write transaction, it means the usb debug device is busy and can not handle this transaction.
889 // should send the packet again.
891 if (ReceivedPid
== NAK_PID
) {
894 UsbDebugPortHandle
->BulkOutToggle
^= 1;
902 Polls a debug device to see if there is any data waiting to be read.
904 Polls a debug device to see if there is any data waiting to be read.
905 If there is data waiting to be read from the debug device, then TRUE is returned.
906 If there is no data waiting to be read from the debug device, then FALSE is returned.
908 @param Handle Debug port handle.
910 @retval TRUE Data is waiting to be read from the debug device.
911 @retval FALSE There is no data waiting to be read from the serial device.
916 DebugPortPollBuffer (
917 IN DEBUG_PORT_HANDLE Handle
920 USB_DEBUG_PORT_HANDLE
*UsbDebugPortHandle
;
921 USB_DEBUG_PORT_REGISTER
*UsbDebugPortRegister
;
924 RETURN_STATUS Status
;
927 // If Handle is NULL, it means memory is ready for use.
928 // Use global variable to store handle value.
930 if (Handle
== NULL
) {
931 UsbDebugPortHandle
= &mUsbDebugPortHandle
;
933 UsbDebugPortHandle
= (USB_DEBUG_PORT_HANDLE
*)Handle
;
936 if (NeedReinitializeHardware(UsbDebugPortHandle
)) {
937 Status
= InitializeUsbDebugHardware(UsbDebugPortHandle
);
938 if (RETURN_ERROR(Status
)) {
944 // If the data buffer is not empty, then return TRUE directly.
945 // else initialize a usb read transaction and read data to the data buffer.
947 if (UsbDebugPortHandle
->DataCount
!= 0) {
951 UsbDebugPortRegister
= (USB_DEBUG_PORT_REGISTER
*)(UsbDebugPortHandle
->UsbDebugPortMemoryBase
+ UsbDebugPortHandle
->DebugPortOffset
);
953 UsbDebugPortRegister
->TokenPid
= INPUT_PID
;
954 if (UsbDebugPortHandle
->BulkInToggle
== 0) {
955 UsbDebugPortRegister
->SendPid
= DATA0_PID
;
957 UsbDebugPortRegister
->SendPid
= DATA1_PID
;
959 UsbDebugPortRegister
->UsbAddress
= 0x7F;
960 UsbDebugPortRegister
->UsbEndPoint
= 0x82 & 0x0F;
963 // Clearing W/R bit to indicate it's a READ operation
965 MmioAnd32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, (UINT32
)~BIT4
);
967 // Setting GO bit as well as clearing DONE bit
969 MmioOr32((UINTN
)&UsbDebugPortRegister
->ControlStatus
, (UINT32
)BIT5
);
972 // Wait for completing the request
974 while ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & (UINT32
)BIT16
) == 0);
976 if ((MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
)) & BIT6
) {
980 Length
= (UINT8
)(MmioRead32((UINTN
)&UsbDebugPortRegister
->ControlStatus
) & 0xF);
986 UsbDebugPortHandle
->BulkInToggle
^= 1;
992 for (Index
= 0; Index
< Length
; Index
++) {
993 UsbDebugPortHandle
->Data
[Index
] = UsbDebugPortRegister
->DataBuffer
[Index
];
995 UsbDebugPortHandle
->DataCount
= Length
;
1001 Initialize the debug port.
1003 If Function is not NULL, Debug Communication Libary will call this function
1004 by passing in the Context to be the first parameter. If needed, Debug Communication
1005 Library will create one debug port handle to be the second argument passing in
1006 calling the Function, otherwise it will pass NULL to be the second argument of
1009 If Function is NULL, and Context is not NULL, the Debug Communication Library could
1010 a) Return the same handle as passed in (as Context parameter).
1011 b) Ignore the input Context parameter and create new hanlde to be returned.
1013 If parameter Function is NULL and Context is NULL, Debug Communication Library could
1014 created a new handle if needed and return it, otherwise it will return NULL.
1016 @param[in] Context Context needed by callback function; it was optional.
1017 @param[in] Function Continue function called by Debug Communication library;
1020 @return The debug port handle created by Debug Communication Library if Function
1026 DebugPortInitialize (
1028 IN DEBUG_PORT_CONTINUE Function
1031 RETURN_STATUS Status
;
1032 USB_DEBUG_PORT_HANDLE Handle
;
1034 if (Function
== NULL
&& Context
!= NULL
) {
1035 return (DEBUG_PORT_HANDLE
*) Context
;
1038 ZeroMem(&Handle
, sizeof (USB_DEBUG_PORT_HANDLE
));
1040 Status
= CalculateUsbDebugPortBar(&Handle
.DebugPortOffset
, &Handle
.DebugPortBarNumber
);
1041 if (RETURN_ERROR (Status
)) {
1045 Handle
.EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
1047 if (Handle
.EhciMemoryBase
== 0) {
1049 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
1051 PciWrite32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
, PcdGet32(PcdUsbEhciMemorySpaceBase
));
1052 Handle
.EhciMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
);
1055 Handle
.UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4);
1057 if (Handle
.UsbDebugPortMemoryBase
== 0) {
1059 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero
1061 PciWrite32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4, PcdGet32(PcdUsbDebugPortMemorySpaceBase
));
1062 Handle
.UsbDebugPortMemoryBase
= 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress
) + PCI_BASE_ADDRESSREG_OFFSET
+ Handle
.DebugPortBarNumber
* 4);
1065 Status
= InitializeUsbDebugHardware (&Handle
);
1066 if (RETURN_ERROR(Status
)) {
1070 if (Function
!= NULL
) {
1071 Function (Context
, &Handle
);
1073 CopyMem(&mUsbDebugPortHandle
, &Handle
, sizeof (USB_DEBUG_PORT_HANDLE
));
1076 return (DEBUG_PORT_HANDLE
)(UINTN
)&mUsbDebugPortHandle
;