/** @file\r
Debug Port Library implementation based on usb debug port.\r
\r
- Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
+ Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r
This program and the accompanying materials\r
are licensed and made available under the terms and conditions of the BSD License\r
which accompanies this distribution. The full text of the license may be found at\r
#include <Library/DebugCommunicationLib.h>\r
#include <Library/BaseMemoryLib.h>\r
#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
\r
#define SETUP_PID 0x2D\r
#define INPUT_PID 0x69\r
UINT8 DebugOutEndpoint;\r
}USB_DEBUG_PORT_DESCRIPTOR;\r
\r
-USB_DEVICE_REQUEST mGetDebugDescriptor = {\r
+USB_DEVICE_REQUEST mDebugCommunicationLibUsbGetDebugDescriptor = {\r
0x80,\r
USB_REQ_GET_DESCRIPTOR,\r
(UINT16)(0x0A << 8),\r
sizeof(USB_DEBUG_PORT_DESCRIPTOR)\r
};\r
\r
-USB_DEVICE_REQUEST mSetDebugFeature = {\r
+USB_DEVICE_REQUEST mDebugCommunicationLibUsbSetDebugFeature = {\r
0x0,\r
USB_REQ_SET_FEATURE,\r
(UINT16)(0x06),\r
0x0\r
};\r
\r
-USB_DEVICE_REQUEST mSetDebugAddress = {\r
+USB_DEVICE_REQUEST mDebugCommunicationLibUsbSetDebugAddress = {\r
0x0,\r
USB_REQ_SET_ADDRESS,\r
(UINT16)(0x7F),\r
UINT8 Reserved3;\r
}USB_DEBUG_PORT_REGISTER;\r
\r
+//\r
+// The state machine of usb debug port\r
+//\r
+#define USBDBG_NO_DEV 0 // No device present at debug port\r
+#define USBDBG_NO_DBG_CAB 1 // The device attached is not usb debug cable\r
+#define USBDBG_DBG_CAB 2 // The device attached is usb debug cable\r
+#define USBDBG_INIT_DONE 4 // The usb debug cable device is initialized\r
+#define USBDBG_RESET 8 // The system is reset\r
+\r
#pragma pack(1)\r
//\r
// The internal data structure of DEBUG_PORT_HANDLE, which stores some\r
// The usb debug port memory BAR number in EHCI configuration space.\r
//\r
UINT8 DebugPortBarNumber;\r
- UINT8 Reserved;\r
+ UINT8 Initialized;\r
//\r
// The offset of usb debug port registers in EHCI memory range.\r
//\r
//\r
// The usb debug port memory BAR address.\r
//\r
- UINTN UsbDebugPortMemoryBase;\r
+ UINT32 UsbDebugPortMemoryBase;\r
//\r
// The EHCI memory BAR address.\r
//\r
- UINTN EhciMemoryBase;\r
+ UINT32 EhciMemoryBase;\r
//\r
// The Bulk In endpoint toggle bit.\r
//\r
//\r
// The global variable which can be used after memory is ready.\r
//\r
-USB_DEBUG_PORT_HANDLE mUsbDebugPortHandle;\r
+USB_DEBUG_PORT_HANDLE mDebugCommunicationLibUsbDebugPortHandle;\r
\r
/**\r
Calculate the usb debug port bar address.\r
)\r
{\r
UINT16 PciStatus;\r
+ UINT16 VendorId;\r
+ UINT16 DeviceId;\r
+ UINT8 ProgInterface;\r
+ UINT8 SubClassCode;\r
+ UINT8 BaseCode;\r
UINT8 CapabilityPtr;\r
UINT8 CapabilityId;\r
\r
+ VendorId = PciRead16 (PcdGet32(PcdUsbEhciPciAddress) + PCI_VENDOR_ID_OFFSET);\r
+ DeviceId = PciRead16 (PcdGet32(PcdUsbEhciPciAddress) + PCI_DEVICE_ID_OFFSET);\r
+ \r
+ if ((VendorId == 0xFFFF) || (DeviceId == 0xFFFF)) {\r
+ return RETURN_UNSUPPORTED;\r
+ }\r
+\r
+ ProgInterface = PciRead8 (PcdGet32(PcdUsbEhciPciAddress) + PCI_CLASSCODE_OFFSET);\r
+ SubClassCode = PciRead8 (PcdGet32(PcdUsbEhciPciAddress) + PCI_CLASSCODE_OFFSET + 1);\r
+ BaseCode = PciRead8 (PcdGet32(PcdUsbEhciPciAddress) + PCI_CLASSCODE_OFFSET + 2);\r
+ \r
+ if ((ProgInterface != PCI_IF_EHCI) || (SubClassCode != PCI_CLASS_SERIAL_USB) || (BaseCode != PCI_CLASS_SERIAL)) {\r
+ return RETURN_UNSUPPORTED;\r
+ }\r
+\r
//\r
// Enable Ehci Host Controller MMIO Space.\r
//\r
//\r
// Wait for completing the request\r
//\r
- while ((MmioRead32((UINTN)&DebugPortRegister->ControlStatus) & (UINT32)BIT16) == 0);\r
+ while ((MmioRead32((UINTN)&DebugPortRegister->ControlStatus) & (UINT32)BIT16) == 0) {\r
+ if ((MmioRead32((UINTN)&DebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE))\r
+ != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE)) {\r
+ return RETURN_DEVICE_ERROR;\r
+ }\r
+ }\r
+ \r
+ //\r
+ // Clearing DONE bit by writing 1\r
+ //\r
+ MmioOr32((UINTN)&DebugPortRegister->ControlStatus, BIT16);\r
\r
//\r
// Check if the request is executed successfully or not.\r
}\r
\r
*Length = (UINT8)(MmioRead32((UINTN)&DebugPortRegister->ControlStatus) & 0xF);\r
+ if (*Length > 8) {\r
+ return RETURN_DEVICE_ERROR;\r
+ }\r
+\r
for (Index = 0; Index < *Length; Index++) {\r
Buffer[Index] = DebugPortRegister->DataBuffer[Index];\r
}\r
//\r
// Wait for completing the request\r
//\r
- while ((MmioRead32((UINTN)&DebugPortRegister->ControlStatus) & BIT16) == 0);\r
+ while ((MmioRead32((UINTN)&DebugPortRegister->ControlStatus) & BIT16) == 0) {\r
+ if ((MmioRead32((UINTN)&DebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE))\r
+ != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE)) {\r
+ return RETURN_DEVICE_ERROR;\r
+ }\r
+ }\r
+ \r
+ //\r
+ // Clearing DONE bit by writing 1\r
+ //\r
+ MmioOr32((UINTN)&DebugPortRegister->ControlStatus, BIT16);\r
\r
//\r
// Check if the request is executed successfully or not.\r
{\r
RETURN_STATUS Status;\r
UINT8 Temp;\r
+ UINT8 ReturnStatus[8];\r
\r
//\r
// Setup Phase\r
//\r
// Data Phase\r
//\r
- if (SetupPacket->Length != 0) {\r
+ if (DataLength != 0) {\r
if ((SetupPacket->RequestType & BIT7) != 0) {\r
//\r
// Get Data From Device\r
//\r
// For WRITE operation, Data Toggle in Status Phase Should be 1.\r
//\r
- Status = UsbDebugPortIn(DebugPortRegister, NULL, &Temp, INPUT_PID, Addr, Ep, 1);\r
+ Status = UsbDebugPortIn(DebugPortRegister, ReturnStatus, &Temp, INPUT_PID, Addr, Ep, 1);\r
}\r
\r
return Status;\r
)\r
{\r
UINT16 PciCmd;\r
- UINTN UsbDebugPortMemoryBase;\r
- UINTN EhciMemoryBase;\r
+ UINT32 UsbDebugPortMemoryBase;\r
+ UINT32 EhciMemoryBase;\r
BOOLEAN Status;\r
USB_DEBUG_PORT_REGISTER *UsbDebugPortRegister;\r
\r
//\r
PciCmd = PciRead16 (PcdGet32(PcdUsbEhciPciAddress) + PCI_COMMAND_OFFSET);\r
if (((PciCmd & EFI_PCI_COMMAND_MEMORY_SPACE) == 0) || ((PciCmd & EFI_PCI_COMMAND_BUS_MASTER) == 0)) {\r
+ PciCmd |= EFI_PCI_COMMAND_MEMORY_SPACE | EFI_PCI_COMMAND_BUS_MASTER;\r
+ PciWrite16(PcdGet32(PcdUsbEhciPciAddress) + PCI_COMMAND_OFFSET, PciCmd);\r
Status = TRUE;\r
}\r
\r
//\r
- // Check if the debug port is enabled and owned by myself.\r
+ // 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.\r
//\r
- UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(Handle->UsbDebugPortMemoryBase + Handle->DebugPortOffset);\r
- if ((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) &\r
- (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_ENABLE | USB_DEBUG_PORT_IN_USE)) == 0) {\r
+ UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(Handle->UsbDebugPortMemoryBase + Handle->DebugPortOffset);\r
+ if ((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_ENABLE | USB_DEBUG_PORT_IN_USE))\r
+ != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_ENABLE | USB_DEBUG_PORT_IN_USE)) {\r
+ Status = TRUE;\r
+ }\r
+\r
+ if (Handle->Initialized == USBDBG_RESET) {\r
+ Status = TRUE;\r
+ } else if (Handle->Initialized != USBDBG_INIT_DONE) {\r
Status = TRUE;\r
}\r
return Status;\r
RETURN_STATUS Status;\r
USB_DEBUG_PORT_REGISTER *UsbDebugPortRegister;\r
USB_DEBUG_PORT_DESCRIPTOR UsbDebugPortDescriptor;\r
- UINT16 PciCmd;\r
UINT32 *PortStatus;\r
UINT32 *UsbCmd;\r
UINT32 *UsbStatus;\r
UINT8 DebugPortNumber;\r
UINT8 Length;\r
\r
- UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(Handle->UsbDebugPortMemoryBase + Handle->DebugPortOffset);\r
- PciCmd = PciRead16 (PcdGet32(PcdUsbEhciPciAddress) + PCI_COMMAND_OFFSET);\r
- UsbHCSParam = (UINT32 *)(Handle->EhciMemoryBase + 0x04);\r
- UsbCmd = (UINT32 *)(Handle->EhciMemoryBase + 0x20);\r
- UsbStatus = (UINT32 *)(Handle->EhciMemoryBase + 0x24);\r
+ UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(Handle->UsbDebugPortMemoryBase + Handle->DebugPortOffset);\r
+ UsbHCSParam = (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x04);\r
+ UsbCmd = (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x20);\r
+ UsbStatus = (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x24);\r
\r
//\r
- // initialize the data toggle used by bulk in/out endpoint.\r
+ // Check if the debug port is enabled and owned by myself.\r
//\r
- Handle->BulkInToggle = 0;\r
- Handle->BulkOutToggle = 0;\r
+ if (((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE))\r
+ != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE)) || (Handle->Initialized == USBDBG_RESET)) {\r
+ DEBUG ((\r
+ EFI_D_INFO,\r
+ "UsbDbg: Need to reset the host controller. ControlStatus = %08x\n",\r
+ MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus)\r
+ ));\r
+ //\r
+ // If the host controller is halted, then reset and restart it.\r
+ //\r
+ if ((MmioRead32((UINTN)UsbStatus) & BIT12) != 0) {\r
+ DEBUG ((EFI_D_INFO, "UsbDbg: Reset the host controller.\n"));\r
+ //\r
+ // reset the host controller.\r
+ //\r
+ MmioOr32((UINTN)UsbCmd, BIT1);\r
+ //\r
+ // ensure that the host controller is reset.\r
+ //\r
+ while ((MmioRead32((UINTN)UsbCmd) & BIT1) != 0);\r
\r
- //\r
- // Enable Ehci Memory Space Access\r
- //\r
- if (((PciCmd & EFI_PCI_COMMAND_MEMORY_SPACE) == 0) || ((PciCmd & EFI_PCI_COMMAND_BUS_MASTER) == 0)) {\r
- PciCmd |= EFI_PCI_COMMAND_MEMORY_SPACE | EFI_PCI_COMMAND_BUS_MASTER;\r
- PciWrite16(PcdGet32(PcdUsbEhciPciAddress) + PCI_COMMAND_OFFSET, PciCmd);\r
- }\r
+ MmioOr32((UINTN)UsbCmd, BIT0);\r
+ // ensure that the host controller is started (HALTED bit must be cleared)\r
+ while ((MmioRead32((UINTN)UsbStatus) & BIT12) != 0);\r
+ }\r
\r
- //\r
- // If the host controller is not halted, then halt it.\r
- //\r
- if ((MmioRead32((UINTN)UsbStatus) & BIT12) == 0) {\r
- MmioAnd32((UINTN)UsbCmd, (UINT32)~BIT0);\r
- while ((MmioRead32((UINTN)UsbStatus) & BIT12) == 0);\r
- }\r
- //\r
- // reset the host controller.\r
- //\r
- MmioOr32((UINTN)UsbCmd, BIT1);\r
- //\r
- // ensure that the host controller is reset.\r
- //\r
- while (MmioRead32((UINTN)UsbCmd) & BIT1);\r
+ //\r
+ // First get the ownership of port 0.\r
+ //\r
+ MmioOr32((UINTN)&UsbDebugPortRegister->ControlStatus, USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE);\r
\r
- //\r
- // Start the host controller if it's not running\r
- //\r
- if (MmioRead32((UINTN)UsbStatus) & BIT12) {\r
- MmioOr32((UINTN)UsbCmd, BIT0);\r
- // ensure that the host controller is started (HALTED bit must be cleared)\r
- while (MmioRead32((UINTN)UsbStatus) & BIT12);\r
+ MicroSecondDelay (200000);\r
}\r
-\r
- //\r
- // First get the ownership of port 0.\r
- //\r
- MmioOr32((UINTN)&UsbDebugPortRegister->ControlStatus, USB_DEBUG_PORT_OWNER);\r
-\r
- MicroSecondDelay (200000);\r
-\r
//\r
// Find out which port is used as debug port.\r
//\r
DebugPortNumber = (UINT8)((MmioRead32((UINTN)UsbHCSParam) & 0x00F00000) >> 20);\r
//\r
- // Should find a non low-speed device is connected\r
+ // Should find a device is connected at debug port\r
//\r
- PortStatus = (UINT32 *)(Handle->EhciMemoryBase + 0x64 + (DebugPortNumber - 1) * 4);\r
- if (!(MmioRead32((UINTN)PortStatus) & BIT0) || ((MmioRead32((UINTN)PortStatus) & USB_PORT_LINE_STATUS_MASK) == USB_PORT_LINE_STATUS_LS)) {\r
+ PortStatus = (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x64 + (DebugPortNumber - 1) * 4);\r
+ if (!(MmioRead32((UINTN)PortStatus) & BIT0)) {\r
+ Handle->Initialized = USBDBG_NO_DEV;\r
return RETURN_NOT_FOUND;\r
}\r
\r
- //\r
- // Reset the debug port\r
- //\r
- MmioOr32((UINTN)PortStatus, BIT8);\r
- MicroSecondDelay (200000);\r
- MmioAnd32((UINTN)PortStatus, (UINT32)~BIT8);\r
- while (MmioRead32((UINTN)PortStatus) & BIT8);\r
+ if (Handle->Initialized != USBDBG_INIT_DONE ||\r
+ (MmioRead32 ((UINTN) &UsbDebugPortRegister->ControlStatus) & USB_DEBUG_PORT_ENABLE) == 0) {\r
+ DEBUG ((EFI_D_INFO, "UsbDbg: Reset the debug port.\n"));\r
+ //\r
+ // Reset the debug port\r
+ //\r
+ MmioOr32((UINTN)PortStatus, BIT8);\r
+ MicroSecondDelay (500000);\r
+ MmioAnd32((UINTN)PortStatus, (UINT32)~BIT8);\r
+ while (MmioRead32((UINTN)PortStatus) & BIT8);\r
\r
- //\r
- // The port enabled bit should be set by HW.\r
- //\r
- if ((MmioRead32((UINTN)PortStatus) & BIT2) == 0) {\r
- return RETURN_DEVICE_ERROR;\r
- }\r
+ //\r
+ // The port enabled bit should be set by HW.\r
+ //\r
+ if ((MmioRead32((UINTN)PortStatus) & BIT2) == 0) {\r
+ Handle->Initialized = USBDBG_NO_DBG_CAB;\r
+ return RETURN_DEVICE_ERROR;\r
+ }\r
\r
- //\r
- // Enable Usb Debug Port Capability\r
- //\r
- MmioOr32((UINTN)&UsbDebugPortRegister->ControlStatus, USB_DEBUG_PORT_ENABLE | USB_DEBUG_PORT_IN_USE);\r
+ //\r
+ // Enable Usb Debug Port Capability\r
+ //\r
+ MmioOr32((UINTN)&UsbDebugPortRegister->ControlStatus, USB_DEBUG_PORT_ENABLE);\r
\r
- //\r
- // Start to communicate with Usb Debug Device to see if the attached device is usb debug device or not.\r
- //\r
- Length = (UINT8)sizeof (USB_DEBUG_PORT_DESCRIPTOR);\r
+ //\r
+ // initialize the data toggle used by bulk in/out endpoint.\r
+ //\r
+ Handle->BulkInToggle = 0;\r
+ Handle->BulkOutToggle = 0;\r
\r
- //\r
- // It's not a dedicated usb debug device, should use address 0 to get debug descriptor.\r
- //\r
- Status = UsbDebugPortControlTransfer (UsbDebugPortRegister, &mGetDebugDescriptor, 0x0, 0x0, (UINT8*)&UsbDebugPortDescriptor, &Length);\r
- if (RETURN_ERROR(Status)) {\r
//\r
- // The device is not a usb debug device.\r
+ // set usb debug device address as 0x7F.\r
//\r
- return Status;\r
- }\r
+ Status = UsbDebugPortControlTransfer (UsbDebugPortRegister, &mDebugCommunicationLibUsbSetDebugAddress, 0x0, 0x0, NULL, NULL);\r
+ if (RETURN_ERROR(Status)) {\r
+ //\r
+ // The device can not work well.\r
+ //\r
+ Handle->Initialized = USBDBG_NO_DBG_CAB;\r
+ return Status;\r
+ }\r
\r
- //\r
- // set usb debug device address as 0x7F.\r
- //\r
- Status = UsbDebugPortControlTransfer (UsbDebugPortRegister, &mSetDebugAddress, 0x0, 0x0, (UINT8*)&UsbDebugPortDescriptor, &Length);\r
- if (RETURN_ERROR(Status)) {\r
//\r
- // The device can not work well.\r
+ // Start to communicate with Usb Debug Device to see if the attached device is usb debug device or not.\r
//\r
- return Status;\r
+ Length = (UINT8)sizeof (USB_DEBUG_PORT_DESCRIPTOR);\r
+\r
+ //\r
+ // Get debug descriptor.\r
+ //\r
+ Status = UsbDebugPortControlTransfer (UsbDebugPortRegister, &mDebugCommunicationLibUsbGetDebugDescriptor, 0x7F, 0x0, (UINT8*)&UsbDebugPortDescriptor, &Length);\r
+ if (RETURN_ERROR(Status)) {\r
+ //\r
+ // The device is not a usb debug device.\r
+ //\r
+ Handle->Initialized = USBDBG_NO_DBG_CAB;\r
+ return Status;\r
+ }\r
+\r
+ if (Length != sizeof(USB_DEBUG_PORT_DESCRIPTOR)) {\r
+ Handle->Initialized = USBDBG_NO_DBG_CAB;\r
+ return RETURN_DEVICE_ERROR;\r
+ }\r
+\r
+ //\r
+ // enable the usb debug feature.\r
+ //\r
+ Status = UsbDebugPortControlTransfer (UsbDebugPortRegister, &mDebugCommunicationLibUsbSetDebugFeature, 0x7F, 0x0, NULL, NULL);\r
+ if (RETURN_ERROR(Status)) {\r
+ //\r
+ // The device can not work well.\r
+ //\r
+ Handle->Initialized = USBDBG_NO_DBG_CAB;\r
+ return Status;\r
+ }\r
+ \r
+ Handle->Initialized = USBDBG_DBG_CAB;\r
}\r
\r
//\r
- // enable the usb debug feature.\r
+ // Set initialized flag\r
//\r
- Status = UsbDebugPortControlTransfer (UsbDebugPortRegister, &mSetDebugFeature, 0x7F, 0x0, NULL, NULL);\r
+ Handle->Initialized = USBDBG_INIT_DONE;\r
\r
- return Status;\r
+ return RETURN_SUCCESS;\r
}\r
\r
/**\r
)\r
{\r
USB_DEBUG_PORT_HANDLE *UsbDebugPortHandle;\r
- USB_DEBUG_PORT_REGISTER *UsbDebugPortRegister;\r
RETURN_STATUS Status;\r
- UINT8 Received;\r
- UINTN Total;\r
- UINTN Remaining;\r
UINT8 Index;\r
- UINT8 Length;\r
\r
- if (NumberOfBytes == 0 || Buffer == NULL) {\r
+ if (NumberOfBytes != 1 || Buffer == NULL || Timeout != 0) {\r
return 0;\r
}\r
\r
- Received = 0;\r
- Total = 0;\r
- Remaining = 0;\r
-\r
//\r
// If Handle is NULL, it means memory is ready for use.\r
// Use global variable to store handle value.\r
//\r
if (Handle == NULL) {\r
- UsbDebugPortHandle = &mUsbDebugPortHandle;\r
+ UsbDebugPortHandle = &mDebugCommunicationLibUsbDebugPortHandle;\r
} else {\r
UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;\r
}\r
}\r
}\r
\r
- UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);\r
-\r
//\r
- // First read data from buffer, then read debug port hw to get received data.\r
+ // Read data from buffer\r
//\r
- if (UsbDebugPortHandle->DataCount > 0) {\r
- if (NumberOfBytes <= UsbDebugPortHandle->DataCount) {\r
- Total = NumberOfBytes;\r
- } else {\r
- Total = UsbDebugPortHandle->DataCount;\r
- }\r
-\r
- for (Index = 0; Index < Total; Index++) {\r
- Buffer[Index] = UsbDebugPortHandle->Data[Index];\r
- }\r
-\r
- for (Index = 0; Index < UsbDebugPortHandle->DataCount - Total; Index++) {\r
- if (Total + Index >= 8) {\r
+ if (UsbDebugPortHandle->DataCount < 1) {\r
+ return 0;\r
+ } else {\r
+ *Buffer = UsbDebugPortHandle->Data[0];\r
+ for (Index = 0; Index < UsbDebugPortHandle->DataCount - 1; Index++) {\r
+ if ((Index + 1) >= USB_DEBUG_PORT_MAX_PACKET_SIZE) {\r
return 0;\r
}\r
- UsbDebugPortHandle->Data[Index] = UsbDebugPortHandle->Data[Total + Index];\r
+ UsbDebugPortHandle->Data[Index] = UsbDebugPortHandle->Data[Index + 1];\r
}\r
- UsbDebugPortHandle->DataCount = (UINT8)(UsbDebugPortHandle->DataCount - (UINT8)Total);\r
+ UsbDebugPortHandle->DataCount = (UINT8)(UsbDebugPortHandle->DataCount - 1);\r
+ return 1;\r
}\r
-\r
- //\r
- // If Timeout is equal to 0, then it means it should always wait until all datum required are received.\r
- //\r
- if (Timeout == 0) {\r
- Timeout = 0xFFFFFFFF;\r
- }\r
-\r
- //\r
- // Read remaining data by executing one or more usb debug transfer transactions at usb debug port hw.\r
- //\r
- while ((Total < NumberOfBytes) && (Timeout != 0)) {\r
- Remaining = NumberOfBytes - Total;\r
- if (Remaining >= USB_DEBUG_PORT_MAX_PACKET_SIZE) {\r
- Status = UsbDebugPortIn(UsbDebugPortRegister, Buffer + Total, &Received, INPUT_PID, 0x7f, 0x82, UsbDebugPortHandle->BulkInToggle);\r
-\r
- if (RETURN_ERROR(Status)) {\r
- return Total;\r
- }\r
- } else {\r
- Status = UsbDebugPortIn(UsbDebugPortRegister, &UsbDebugPortHandle->Data[0], &Received, INPUT_PID, 0x7f, 0x82, UsbDebugPortHandle->BulkInToggle);\r
-\r
- if (RETURN_ERROR(Status)) {\r
- return Total;\r
- }\r
-\r
- UsbDebugPortHandle->DataCount = Received;\r
-\r
- if (Remaining <= Received) {\r
- Length = (UINT8)Remaining;\r
- } else {\r
- Length = (UINT8)Received;\r
- }\r
-\r
- //\r
- // Copy required data from the data buffer to user buffer.\r
- //\r
- for (Index = 0; Index < Length; Index++) {\r
- (Buffer + Total)[Index] = UsbDebugPortHandle->Data[Index];\r
- UsbDebugPortHandle->DataCount--;\r
- }\r
-\r
- //\r
- // reorder the data buffer to make available data arranged from the beginning of the data buffer.\r
- //\r
- for (Index = 0; Index < Received - Length; Index++) {\r
- if (Length + Index >= 8) {\r
- return 0;\r
- }\r
- UsbDebugPortHandle->Data[Index] = UsbDebugPortHandle->Data[Length + Index];\r
- }\r
- //\r
- // fixup the real received length in Buffer.\r
- //\r
- Received = Length;\r
- }\r
- UsbDebugPortHandle->BulkInToggle ^= 1;\r
-\r
- Total += Received;\r
- Timeout -= 100;\r
- }\r
-\r
- return Total;\r
}\r
\r
/**\r
// Use global variable to store handle value.\r
//\r
if (Handle == NULL) {\r
- UsbDebugPortHandle = &mUsbDebugPortHandle;\r
+ UsbDebugPortHandle = &mDebugCommunicationLibUsbDebugPortHandle;\r
} else {\r
UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;\r
}\r
}\r
}\r
\r
- UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);\r
+ UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);\r
\r
while ((Total < NumberOfBytes)) {\r
if (NumberOfBytes - Total > USB_DEBUG_PORT_MAX_PACKET_SIZE) {\r
// Use global variable to store handle value.\r
//\r
if (Handle == NULL) {\r
- UsbDebugPortHandle = &mUsbDebugPortHandle;\r
+ UsbDebugPortHandle = &mDebugCommunicationLibUsbDebugPortHandle;\r
} else {\r
UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;\r
}\r
return TRUE;\r
}\r
\r
- UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);\r
+ UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);\r
\r
UsbDebugPortRegister->TokenPid = INPUT_PID;\r
if (UsbDebugPortHandle->BulkInToggle == 0) {\r
//\r
// Wait for completing the request\r
//\r
- while ((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) & (UINT32)BIT16) == 0);\r
+ while ((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) & (UINT32)BIT16) == 0) {\r
+ if ((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE))\r
+ != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE)) {\r
+ return FALSE;\r
+ }\r
+ }\r
\r
if ((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus)) & BIT6) {\r
return FALSE;\r
\r
If Function is NULL, and Context is not NULL, the Debug Communication Library could\r
a) Return the same handle as passed in (as Context parameter).\r
- b) Ignore the input Context parameter and create new hanlde to be returned.\r
+ b) Ignore the input Context parameter and create new handle to be returned.\r
\r
If parameter Function is NULL and Context is NULL, Debug Communication Library could\r
created a new handle if needed and return it, otherwise it will return NULL.\r
RETURN_STATUS Status;\r
USB_DEBUG_PORT_HANDLE Handle;\r
\r
+ //\r
+ // Validate the PCD PcdDebugPortHandleBufferSize value \r
+ //\r
+ ASSERT (PcdGet16 (PcdDebugPortHandleBufferSize) == sizeof (USB_DEBUG_PORT_HANDLE));\r
+\r
if (Function == NULL && Context != NULL) {\r
return (DEBUG_PORT_HANDLE *) Context;\r
}\r
-\r
ZeroMem(&Handle, sizeof (USB_DEBUG_PORT_HANDLE));\r
\r
Status = CalculateUsbDebugPortBar(&Handle.DebugPortOffset, &Handle.DebugPortBarNumber);\r
if (RETURN_ERROR (Status)) {\r
- return NULL;\r
+ DEBUG ((EFI_D_ERROR, "UsbDbg: the pci device pointed by PcdUsbEhciPciAddress is not EHCI host controller or does not support debug port capability!\n"));\r
+ goto Exit;\r
}\r
\r
Handle.EhciMemoryBase = 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET);\r
Handle.UsbDebugPortMemoryBase = 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET + Handle.DebugPortBarNumber * 4);\r
}\r
\r
- Status = InitializeUsbDebugHardware (&Handle);\r
- if (RETURN_ERROR(Status)) {\r
- return NULL;\r
+ Handle.Initialized = USBDBG_RESET;\r
+\r
+ if (NeedReinitializeHardware(&Handle)) {\r
+ DEBUG ((EFI_D_ERROR, "UsbDbg: Start EHCI debug port initialization!\n"));\r
+ Status = InitializeUsbDebugHardware (&Handle);\r
+ if (RETURN_ERROR(Status)) {\r
+ DEBUG ((EFI_D_ERROR, "UsbDbg: Failed, please check if USB debug cable is plugged into EHCI debug port correctly!\n"));\r
+ goto Exit;\r
+ }\r
}\r
\r
+Exit:\r
+\r
if (Function != NULL) {\r
Function (Context, &Handle);\r
} else {\r
- CopyMem(&mUsbDebugPortHandle, &Handle, sizeof (USB_DEBUG_PORT_HANDLE));\r
+ CopyMem(&mDebugCommunicationLibUsbDebugPortHandle, &Handle, sizeof (USB_DEBUG_PORT_HANDLE));\r
}\r
\r
- return (DEBUG_PORT_HANDLE)(UINTN)&mUsbDebugPortHandle;\r
+ return (DEBUG_PORT_HANDLE)(UINTN)&mDebugCommunicationLibUsbDebugPortHandle;\r
}\r
\r