]> git.proxmox.com Git - mirror_edk2.git/blame - SourceLevelDebugPkg/Library/DebugCommunicationLibUsb/DebugCommunicationLibUsb.c
SourceLevelDebugPkg: Fix typos in comments
[mirror_edk2.git] / SourceLevelDebugPkg / Library / DebugCommunicationLibUsb / DebugCommunicationLibUsb.c
CommitLineData
18b144ea 1/** @file\r
2 Debug Port Library implementation based on usb debug port.\r
3\r
08021523 4 Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>\r
18b144ea 5 This program and the accompanying materials\r
6 are licensed and made available under the terms and conditions of the BSD License\r
7 which accompanies this distribution. The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <Base.h>\r
16#include <IndustryStandard/Pci.h>\r
17#include <IndustryStandard/Usb.h>\r
18#include <Library/IoLib.h>\r
19#include <Library/PciLib.h>\r
20#include <Library/PcdLib.h>\r
21#include <Library/TimerLib.h>\r
22#include <Library/DebugCommunicationLib.h>\r
23#include <Library/BaseMemoryLib.h>\r
24#include <Library/BaseLib.h>\r
e2104834 25#include <Library/DebugLib.h>\r
18b144ea 26\r
27#define SETUP_PID 0x2D\r
28#define INPUT_PID 0x69\r
29#define OUTPUT_PID 0xE1\r
30#define ERROR_PID 0x55\r
31#define DATA0_PID 0xC3\r
32#define DATA1_PID 0x4B\r
33#define DATA2_PID 0x87\r
34#define MDATA_PID 0x0F\r
35#define ACK_PID 0xD2\r
36#define NAK_PID 0x5A\r
37#define STALL_PID 0x1E\r
38#define NYET_PID 0x96\r
39\r
40#define PCI_CAPABILITY_ID_DEBUG_PORT 0x0A\r
41#define USB_DEBUG_PORT_MAX_PACKET_SIZE 0x08\r
42\r
43#define USB_DEBUG_PORT_IN_USE BIT10\r
44#define USB_DEBUG_PORT_ENABLE BIT28\r
45#define USB_DEBUG_PORT_OWNER BIT30\r
46\r
47#define USB_PORT_LINE_STATUS_LS 0x400\r
48#define USB_PORT_LINE_STATUS_MASK 0xC00\r
49\r
50//\r
51// Usb debug device descriptor, which is defined at\r
52// USB2 Debug Device Specification.\r
53//\r
54typedef struct _USB_DEBUG_PORT_DESCRIPTOR {\r
55 UINT8 Length;\r
56 UINT8 DescriptorType;\r
57 UINT8 DebugInEndpoint;\r
58 UINT8 DebugOutEndpoint;\r
59}USB_DEBUG_PORT_DESCRIPTOR;\r
60\r
d6211ea1 61USB_DEVICE_REQUEST mDebugCommunicationLibUsbGetDebugDescriptor = {\r
18b144ea 62 0x80,\r
63 USB_REQ_GET_DESCRIPTOR,\r
64 (UINT16)(0x0A << 8),\r
65 0x0000,\r
66 sizeof(USB_DEBUG_PORT_DESCRIPTOR)\r
67 };\r
68\r
d6211ea1 69USB_DEVICE_REQUEST mDebugCommunicationLibUsbSetDebugFeature = {\r
18b144ea 70 0x0,\r
71 USB_REQ_SET_FEATURE,\r
72 (UINT16)(0x06),\r
73 0x0000,\r
74 0x0\r
75 };\r
76\r
d6211ea1 77USB_DEVICE_REQUEST mDebugCommunicationLibUsbSetDebugAddress = {\r
18b144ea 78 0x0,\r
79 USB_REQ_SET_ADDRESS,\r
80 (UINT16)(0x7F),\r
81 0x0000,\r
82 0x0\r
83 };\r
84\r
85//\r
86// Usb debug port register file, which is defined at\r
87// EHCI Specification.\r
88//\r
89typedef struct _USB_DEBUG_PORT_REGISTER {\r
90 UINT32 ControlStatus;\r
91 UINT8 TokenPid;\r
92 UINT8 SendPid;\r
93 UINT8 ReceivedPid;\r
94 UINT8 Reserved1;\r
95 UINT8 DataBuffer[8];\r
96 UINT8 UsbEndPoint;\r
97 UINT8 UsbAddress;\r
98 UINT8 Reserved2;\r
99 UINT8 Reserved3;\r
100}USB_DEBUG_PORT_REGISTER;\r
101\r
93c0bdec 102//\r
103// The state machine of usb debug port\r
104//\r
105#define USBDBG_NO_DEV 0 // No device present at debug port\r
106#define USBDBG_NO_DBG_CAB 1 // The device attached is not usb debug cable\r
107#define USBDBG_DBG_CAB 2 // The device attached is usb debug cable\r
108#define USBDBG_INIT_DONE 4 // The usb debug cable device is initialized\r
109#define USBDBG_RESET 8 // The system is reset\r
110\r
18b144ea 111#pragma pack(1)\r
112//\r
113// The internal data structure of DEBUG_PORT_HANDLE, which stores some\r
114// important datum which are used across various phases.\r
115//\r
116typedef struct _USB_DEBUG_PORT_HANDLE{\r
117 //\r
118 // The usb debug port memory BAR number in EHCI configuration space.\r
119 //\r
120 UINT8 DebugPortBarNumber;\r
93c0bdec 121 UINT8 Initialized;\r
18b144ea 122 //\r
123 // The offset of usb debug port registers in EHCI memory range.\r
124 //\r
125 UINT16 DebugPortOffset;\r
126 //\r
127 // The usb debug port memory BAR address.\r
128 //\r
93c0bdec 129 UINT32 UsbDebugPortMemoryBase;\r
18b144ea 130 //\r
131 // The EHCI memory BAR address.\r
132 //\r
93c0bdec 133 UINT32 EhciMemoryBase;\r
18b144ea 134 //\r
135 // The Bulk In endpoint toggle bit.\r
136 //\r
137 UINT8 BulkInToggle;\r
138 //\r
139 // The Bulk Out endpoint toggle bit.\r
140 //\r
141 UINT8 BulkOutToggle;\r
142 //\r
143 // The available data length in the following data buffer.\r
144 //\r
145 UINT8 DataCount;\r
146 //\r
147 // The data buffer. Maximum length is 8 bytes.\r
148 //\r
149 UINT8 Data[8];\r
150} USB_DEBUG_PORT_HANDLE;\r
151#pragma pack()\r
152\r
153//\r
154// The global variable which can be used after memory is ready.\r
155//\r
d6211ea1 156USB_DEBUG_PORT_HANDLE mDebugCommunicationLibUsbDebugPortHandle;\r
18b144ea 157\r
158/**\r
159 Calculate the usb debug port bar address.\r
160\r
161 @param DebugPortOffset Get usb debug port offset in the usb debug port memory space.\r
162 @param DebugPortBarNumbar Get the bar number at which usb debug port is located.\r
163\r
164 @retval RETURN_UNSUPPORTED The usb host controller does not supported usb debug port capability.\r
165 @retval RETURN_SUCCESS Get bar and offset successfully.\r
166\r
167**/\r
168RETURN_STATUS\r
169EFIAPI\r
170CalculateUsbDebugPortBar (\r
171 OUT UINT16 *DebugPortOffset,\r
172 OUT UINT8 *DebugPortBarNumbar\r
173 )\r
174{\r
175 UINT16 PciStatus;\r
f98c2685 176 UINT16 VendorId;\r
177 UINT16 DeviceId;\r
178 UINT8 ProgInterface;\r
179 UINT8 SubClassCode;\r
180 UINT8 BaseCode;\r
18b144ea 181 UINT8 CapabilityPtr;\r
182 UINT8 CapabilityId;\r
183\r
f98c2685 184 VendorId = PciRead16 (PcdGet32(PcdUsbEhciPciAddress) + PCI_VENDOR_ID_OFFSET);\r
185 DeviceId = PciRead16 (PcdGet32(PcdUsbEhciPciAddress) + PCI_DEVICE_ID_OFFSET);\r
186 \r
187 if ((VendorId == 0xFFFF) || (DeviceId == 0xFFFF)) {\r
188 return RETURN_UNSUPPORTED;\r
189 }\r
190\r
191 ProgInterface = PciRead8 (PcdGet32(PcdUsbEhciPciAddress) + PCI_CLASSCODE_OFFSET);\r
192 SubClassCode = PciRead8 (PcdGet32(PcdUsbEhciPciAddress) + PCI_CLASSCODE_OFFSET + 1);\r
193 BaseCode = PciRead8 (PcdGet32(PcdUsbEhciPciAddress) + PCI_CLASSCODE_OFFSET + 2);\r
194 \r
195 if ((ProgInterface != PCI_IF_EHCI) || (SubClassCode != PCI_CLASS_SERIAL_USB) || (BaseCode != PCI_CLASS_SERIAL)) {\r
196 return RETURN_UNSUPPORTED;\r
197 }\r
198\r
18b144ea 199 //\r
200 // Enable Ehci Host Controller MMIO Space.\r
201 //\r
202 PciStatus = PciRead16 (PcdGet32(PcdUsbEhciPciAddress) + PCI_PRIMARY_STATUS_OFFSET);\r
203\r
204 if ((PciStatus & EFI_PCI_STATUS_CAPABILITY) == 0) {\r
205 //\r
206 // The Pci Device Doesn't Support Capability Pointer.\r
207 //\r
208 return RETURN_UNSUPPORTED;\r
209 }\r
210\r
211 //\r
212 // Get Pointer To Capability List\r
213 //\r
214 CapabilityPtr = PciRead8(PcdGet32(PcdUsbEhciPciAddress) + PCI_CAPBILITY_POINTER_OFFSET);\r
215\r
216 //\r
217 // Find Capability ID 0xA, Which Is For Debug Port\r
218 //\r
219 while (CapabilityPtr != 0) {\r
220 CapabilityId = PciRead8(PcdGet32(PcdUsbEhciPciAddress) + CapabilityPtr);\r
221 if (CapabilityId == PCI_CAPABILITY_ID_DEBUG_PORT) {\r
222 break;\r
223 }\r
224 CapabilityPtr = PciRead8(PcdGet32(PcdUsbEhciPciAddress) + CapabilityPtr + 1);\r
225 }\r
226\r
227 //\r
228 // No Debug Port Capability Found\r
229 //\r
230 if (CapabilityPtr == 0) {\r
231 return RETURN_UNSUPPORTED;\r
232 }\r
233\r
234 //\r
235 // Get The Base Address Of Debug Port Register In Debug Port Capability Register\r
236 //\r
237 *DebugPortOffset = (UINT16)(PciRead16(PcdGet32(PcdUsbEhciPciAddress) + CapabilityPtr + 2) & 0x1FFF);\r
238 *DebugPortBarNumbar = (UINT8)((PciRead16(PcdGet32(PcdUsbEhciPciAddress) + CapabilityPtr + 2) >> 13) - 1);\r
239\r
240 return RETURN_SUCCESS;\r
241}\r
242\r
243/**\r
244 Do a usb IN transaction by usb debug port.\r
245\r
246 @param DebugPortRegister Pointer to the base address of usb debug port register interface.\r
247 @param Buffer Pointer to the buffer receiving data.\r
248 @param Length Number of bytes of the received data.\r
249 @param Token The token PID for each USB transaction.\r
250 @param Addr The usb device address for usb transaction.\r
251 @param Ep The endpoint for usb transaction.\r
252 @param DataToggle The toggle bit used at usb transaction.\r
253\r
254 @retval RETURN_SUCCESS The IN transaction is executed successfully.\r
255 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.\r
256 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.\r
257\r
258**/\r
259RETURN_STATUS\r
260EFIAPI\r
261UsbDebugPortIn (\r
262 IN USB_DEBUG_PORT_REGISTER *DebugPortRegister,\r
263 IN OUT UINT8 *Buffer,\r
264 OUT UINT8 *Length,\r
265 IN UINT8 Token,\r
266 IN UINT8 Addr,\r
267 IN UINT8 Ep,\r
268 IN UINT8 DataToggle\r
269 )\r
270{\r
271 UINTN Index;\r
272\r
273 if (Length == NULL) {\r
274 return RETURN_INVALID_PARAMETER;\r
275 }\r
276 *Length = 0;\r
277\r
278 DebugPortRegister->TokenPid = Token;\r
279 if (DataToggle != 0) {\r
280 DebugPortRegister->SendPid = DATA1_PID;\r
281 } else {\r
282 DebugPortRegister->SendPid = DATA0_PID;\r
283 }\r
284\r
285 DebugPortRegister->UsbAddress = (UINT8)(Addr & 0x7F);\r
286 DebugPortRegister->UsbEndPoint = (UINT8)(Ep & 0xF);\r
287\r
288 //\r
289 // Clearing W/R bit to indicate it's a READ operation\r
290 //\r
291 MmioAnd32((UINTN)&DebugPortRegister->ControlStatus, (UINT32)~BIT4);\r
292\r
293 //\r
294 // Setting GO bit as well as clearing DONE bit\r
295 //\r
296 MmioOr32((UINTN)&DebugPortRegister->ControlStatus, (UINT32)BIT5);\r
297\r
298 //\r
299 // Wait for completing the request\r
300 //\r
93c0bdec 301 while ((MmioRead32((UINTN)&DebugPortRegister->ControlStatus) & (UINT32)BIT16) == 0) {\r
302 if ((MmioRead32((UINTN)&DebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE))\r
303 != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE)) {\r
304 return RETURN_DEVICE_ERROR;\r
305 }\r
306 }\r
307 \r
308 //\r
309 // Clearing DONE bit by writing 1\r
310 //\r
311 MmioOr32((UINTN)&DebugPortRegister->ControlStatus, BIT16);\r
18b144ea 312\r
313 //\r
314 // Check if the request is executed successfully or not.\r
315 //\r
316 if ((MmioRead32((UINTN)&DebugPortRegister->ControlStatus)) & BIT6) {\r
317 return RETURN_DEVICE_ERROR;\r
318 }\r
319\r
320 //\r
321 // Make sure the received data are not beyond the allowable maximum length - 8 byte\r
322 //\r
323 if (((MmioRead32((UINTN)&DebugPortRegister->ControlStatus)) & 0xF) > USB_DEBUG_PORT_MAX_PACKET_SIZE) {\r
324 return RETURN_DEVICE_ERROR;\r
325 }\r
326\r
327 *Length = (UINT8)(MmioRead32((UINTN)&DebugPortRegister->ControlStatus) & 0xF);\r
ec99fa8e 328 if (*Length > 8) {\r
329 return RETURN_DEVICE_ERROR;\r
330 }\r
331\r
18b144ea 332 for (Index = 0; Index < *Length; Index++) {\r
333 Buffer[Index] = DebugPortRegister->DataBuffer[Index];\r
334 }\r
335 return RETURN_SUCCESS;\r
336}\r
337\r
338/**\r
339 Do a usb SETUP/OUT transaction by usb debug port.\r
340\r
341 @param DebugPortRegister Pointer to the base address of usb debug port register interface.\r
342 @param Buffer Pointer to the buffer receiving data.\r
343 @param Length Number of bytes of the received data.\r
344 @param Token The token PID for each USB transaction.\r
345 @param Addr The usb device address for usb transaction.\r
346 @param Ep The endpoint for usb transaction.\r
347 @param DataToggle The toggle bit used at usb transaction.\r
348\r
349 @retval RETURN_SUCCESS The IN transaction is executed successfully.\r
350 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.\r
351 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.\r
352\r
353**/\r
354RETURN_STATUS\r
355EFIAPI\r
356UsbDebugPortOut (\r
357 IN USB_DEBUG_PORT_REGISTER *DebugPortRegister,\r
358 IN UINT8 *Buffer,\r
359 IN UINT8 Length,\r
360 IN UINT8 Token,\r
361 IN UINT8 Addr,\r
362 IN UINT8 Ep,\r
363 IN UINT8 DataToggle\r
364 )\r
365{\r
366 UINT8 Index;\r
367\r
368 if (Length > 8) {\r
369 return RETURN_INVALID_PARAMETER;\r
370 }\r
371\r
372 DebugPortRegister->TokenPid = Token;\r
373 if (DataToggle != 0) {\r
374 DebugPortRegister->SendPid = DATA1_PID;\r
375 } else {\r
376 DebugPortRegister->SendPid = DATA0_PID;\r
377 }\r
378 DebugPortRegister->UsbAddress = (UINT8)(Addr & 0x7F);\r
379 DebugPortRegister->UsbEndPoint = (UINT8)(Ep & 0xF);\r
380\r
381 //\r
382 // Fill in the data length and corresponding data.\r
383 //\r
384 MmioAnd32((UINTN)&DebugPortRegister->ControlStatus, (UINT32)~0xF);\r
385 MmioOr32((UINTN)&DebugPortRegister->ControlStatus, Length & 0xF);\r
386 for (Index = 0; Index < Length; Index++) {\r
387 DebugPortRegister->DataBuffer[Index] = Buffer[Index];\r
388 }\r
389\r
390 //\r
391 // Setting W/R bit to indicate it's a WRITE operation\r
392 //\r
393 MmioOr32((UINTN)&DebugPortRegister->ControlStatus, BIT4);\r
394 //\r
395 // Setting GO bit as well as clearing DONE bit\r
396 //\r
397 MmioOr32((UINTN)&DebugPortRegister->ControlStatus, BIT5);\r
398\r
399 //\r
400 // Wait for completing the request\r
401 //\r
93c0bdec 402 while ((MmioRead32((UINTN)&DebugPortRegister->ControlStatus) & BIT16) == 0) {\r
403 if ((MmioRead32((UINTN)&DebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE))\r
404 != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE)) {\r
405 return RETURN_DEVICE_ERROR;\r
406 }\r
407 }\r
408 \r
409 //\r
410 // Clearing DONE bit by writing 1\r
411 //\r
412 MmioOr32((UINTN)&DebugPortRegister->ControlStatus, BIT16);\r
18b144ea 413\r
414 //\r
415 // Check if the request is executed successfully or not.\r
416 //\r
417 if ((MmioRead32((UINTN)&DebugPortRegister->ControlStatus)) & BIT6) {\r
418 return RETURN_DEVICE_ERROR;\r
419 }\r
420\r
421 //\r
422 // Make sure the sent data are not beyond the allowable maximum length - 8 byte\r
423 //\r
424 if (((MmioRead32((UINTN)&DebugPortRegister->ControlStatus)) & 0xF) > USB_DEBUG_PORT_MAX_PACKET_SIZE) {\r
425 return RETURN_DEVICE_ERROR;\r
426 }\r
427\r
428 return RETURN_SUCCESS;\r
429}\r
430\r
431/**\r
432 Do a usb control transfer by usb debug port.\r
433\r
434 @param DebugPortRegister Pointer to the base address of usb debug port register interface.\r
435 @param SetupPacket The token PID for each USB transaction.\r
436 @param Addr The usb device address for usb transaction.\r
437 @param Ep The endpoint for usb transaction.\r
438 @param Data Pointer to the buffer receiving data.\r
439 @param DataLength Number of bytes of the received data.\r
440\r
441 @retval RETURN_SUCCESS The IN transaction is executed successfully.\r
442 @retval RETURN_INVALID_PARAMETER The parameters passed in are invalid.\r
443 @retval RETURN_DEVICE_ERROR The IN transaction comes across error.\r
444\r
445**/\r
446RETURN_STATUS\r
447EFIAPI\r
448UsbDebugPortControlTransfer (\r
449 IN USB_DEBUG_PORT_REGISTER *DebugPortRegister,\r
450 IN USB_DEVICE_REQUEST *SetupPacket,\r
451 IN UINT8 Addr,\r
452 IN UINT8 Ep,\r
453 OUT UINT8 *Data,\r
454 IN OUT UINT8 *DataLength\r
455 )\r
456{\r
457 RETURN_STATUS Status;\r
458 UINT8 Temp;\r
83283ef1 459 UINT8 ReturnStatus[8];\r
18b144ea 460\r
461 //\r
462 // Setup Phase\r
463 //\r
464 Status = UsbDebugPortOut(DebugPortRegister, (UINT8 *)SetupPacket, (UINT8)sizeof(USB_DEVICE_REQUEST), SETUP_PID, Addr, Ep, 0);\r
465 if (RETURN_ERROR(Status)) {\r
466 return Status;\r
467 }\r
468\r
469 //\r
470 // Data Phase\r
471 //\r
83283ef1 472 if (DataLength != 0) {\r
18b144ea 473 if ((SetupPacket->RequestType & BIT7) != 0) {\r
474 //\r
475 // Get Data From Device\r
476 //\r
477 Status = UsbDebugPortIn(DebugPortRegister, Data, DataLength, INPUT_PID, Addr, Ep, 1);\r
478 if (RETURN_ERROR(Status)) {\r
479 return Status;\r
480 }\r
481 } else {\r
482 //\r
483 // Send Data To Device\r
484 //\r
485 Status = UsbDebugPortOut(DebugPortRegister, Data, *DataLength, OUTPUT_PID, Addr, Ep, 1);\r
486 if (RETURN_ERROR(Status)) {\r
487 return Status;\r
488 }\r
489 }\r
490 }\r
491\r
492 //\r
493 // Status Phase\r
494 //\r
495 if ((SetupPacket->RequestType & BIT7) != 0) {\r
496 //\r
497 // For READ operation, Data Toggle in Status Phase Should be 1.\r
498 //\r
499 Status = UsbDebugPortOut(DebugPortRegister, NULL, 0, OUTPUT_PID, Addr, Ep, 1);\r
500 } else {\r
501 //\r
502 // For WRITE operation, Data Toggle in Status Phase Should be 1.\r
503 //\r
83283ef1 504 Status = UsbDebugPortIn(DebugPortRegister, ReturnStatus, &Temp, INPUT_PID, Addr, Ep, 1);\r
18b144ea 505 }\r
506\r
507 return Status;\r
508}\r
509\r
510/**\r
511 Check if it needs to re-initialize usb debug port hardware.\r
512\r
513 During different phases switch, such as SEC to PEI or PEI to DXE or DXE to SMM, we should check\r
74cdb367 514 whether the usb debug port hardware configuration is changed. Such case can be triggered by\r
18b144ea 515 Pci bus resource allocation and so on.\r
516\r
517 @param Handle Debug port handle.\r
518\r
519 @retval TRUE The usb debug port hardware configuration is changed.\r
520 @retval FALSE The usb debug port hardware configuration is not changed.\r
521\r
522**/\r
523BOOLEAN\r
524EFIAPI\r
525NeedReinitializeHardware(\r
526 IN USB_DEBUG_PORT_HANDLE *Handle\r
527 )\r
528{\r
529 UINT16 PciCmd;\r
93c0bdec 530 UINT32 UsbDebugPortMemoryBase;\r
531 UINT32 EhciMemoryBase;\r
18b144ea 532 BOOLEAN Status;\r
533 USB_DEBUG_PORT_REGISTER *UsbDebugPortRegister;\r
534\r
535 Status = FALSE;\r
536\r
537 EhciMemoryBase = 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET);\r
538 if (EhciMemoryBase != Handle->EhciMemoryBase) {\r
539 Handle->EhciMemoryBase = EhciMemoryBase;\r
540 Status = TRUE;\r
541 }\r
542\r
543 UsbDebugPortMemoryBase = 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET + Handle->DebugPortBarNumber * 4);\r
544 if (UsbDebugPortMemoryBase != Handle->UsbDebugPortMemoryBase) {\r
545 Handle->UsbDebugPortMemoryBase = UsbDebugPortMemoryBase;\r
546 Status = TRUE;\r
547 }\r
548\r
549 //\r
550 // Enable Ehci Memory Space Access\r
551 //\r
552 PciCmd = PciRead16 (PcdGet32(PcdUsbEhciPciAddress) + PCI_COMMAND_OFFSET);\r
553 if (((PciCmd & EFI_PCI_COMMAND_MEMORY_SPACE) == 0) || ((PciCmd & EFI_PCI_COMMAND_BUS_MASTER) == 0)) {\r
93c0bdec 554 PciCmd |= EFI_PCI_COMMAND_MEMORY_SPACE | EFI_PCI_COMMAND_BUS_MASTER;\r
555 PciWrite16(PcdGet32(PcdUsbEhciPciAddress) + PCI_COMMAND_OFFSET, PciCmd);\r
18b144ea 556 Status = TRUE;\r
557 }\r
558\r
559 //\r
93c0bdec 560 // 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
18b144ea 561 //\r
93c0bdec 562 UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(Handle->UsbDebugPortMemoryBase + Handle->DebugPortOffset);\r
563 if ((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_ENABLE | USB_DEBUG_PORT_IN_USE))\r
564 != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_ENABLE | USB_DEBUG_PORT_IN_USE)) {\r
565 Status = TRUE;\r
566 }\r
567\r
568 if (Handle->Initialized == USBDBG_RESET) {\r
569 Status = TRUE;\r
570 } else if (Handle->Initialized != USBDBG_INIT_DONE) {\r
18b144ea 571 Status = TRUE;\r
572 }\r
573 return Status;\r
574}\r
575\r
576/**\r
577 Initialize usb debug port hardware.\r
578\r
579 1. reset ehci host controller.\r
580 2. set right port to debug port.\r
581 3. find a usb debug device is attached by getting debug device descriptor.\r
582 4. set address for the usb debug device.\r
583 5. configure the usb debug device to debug mode.\r
584\r
585 @param Handle Debug port handle.\r
586\r
587 @retval TRUE The usb debug port hardware configuration is changed.\r
588 @retval FALSE The usb debug port hardware configuration is not changed.\r
589\r
590**/\r
591RETURN_STATUS\r
592EFIAPI\r
593InitializeUsbDebugHardware (\r
594 IN USB_DEBUG_PORT_HANDLE *Handle\r
595)\r
596{\r
597 RETURN_STATUS Status;\r
598 USB_DEBUG_PORT_REGISTER *UsbDebugPortRegister;\r
599 USB_DEBUG_PORT_DESCRIPTOR UsbDebugPortDescriptor;\r
18b144ea 600 UINT32 *PortStatus;\r
601 UINT32 *UsbCmd;\r
602 UINT32 *UsbStatus;\r
603 UINT32 *UsbHCSParam;\r
604 UINT8 DebugPortNumber;\r
605 UINT8 Length;\r
606\r
93c0bdec 607 UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(Handle->UsbDebugPortMemoryBase + Handle->DebugPortOffset);\r
93c0bdec 608 UsbHCSParam = (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x04);\r
609 UsbCmd = (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x20);\r
610 UsbStatus = (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x24);\r
18b144ea 611\r
612 //\r
93c0bdec 613 // Check if the debug port is enabled and owned by myself.\r
18b144ea 614 //\r
93c0bdec 615 if (((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE))\r
616 != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE)) || (Handle->Initialized == USBDBG_RESET)) {\r
93723244
RN
617 DEBUG ((\r
618 EFI_D_INFO,\r
619 "UsbDbg: Need to reset the host controller. ControlStatus = %08x\n",\r
620 MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus)\r
621 ));\r
93c0bdec 622 //\r
b422b62c 623 // If the host controller is halted, then reset and restart it.\r
93c0bdec 624 //\r
b422b62c 625 if ((MmioRead32((UINTN)UsbStatus) & BIT12) != 0) {\r
93723244 626 DEBUG ((EFI_D_INFO, "UsbDbg: Reset the host controller.\n"));\r
b422b62c 627 //\r
628 // reset the host controller.\r
629 //\r
630 MmioOr32((UINTN)UsbCmd, BIT1);\r
631 //\r
632 // ensure that the host controller is reset.\r
633 //\r
634 while ((MmioRead32((UINTN)UsbCmd) & BIT1) != 0);\r
18b144ea 635\r
93c0bdec 636 MmioOr32((UINTN)UsbCmd, BIT0);\r
637 // ensure that the host controller is started (HALTED bit must be cleared)\r
b422b62c 638 while ((MmioRead32((UINTN)UsbStatus) & BIT12) != 0);\r
93c0bdec 639 }\r
18b144ea 640\r
93c0bdec 641 //\r
642 // First get the ownership of port 0.\r
643 //\r
644 MmioOr32((UINTN)&UsbDebugPortRegister->ControlStatus, USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE);\r
18b144ea 645\r
93c0bdec 646 MicroSecondDelay (200000);\r
18b144ea 647 }\r
18b144ea 648 //\r
649 // Find out which port is used as debug port.\r
650 //\r
651 DebugPortNumber = (UINT8)((MmioRead32((UINTN)UsbHCSParam) & 0x00F00000) >> 20);\r
652 //\r
93c0bdec 653 // Should find a device is connected at debug port\r
18b144ea 654 //\r
93c0bdec 655 PortStatus = (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x64 + (DebugPortNumber - 1) * 4);\r
656 if (!(MmioRead32((UINTN)PortStatus) & BIT0)) {\r
657 Handle->Initialized = USBDBG_NO_DEV;\r
18b144ea 658 return RETURN_NOT_FOUND;\r
659 }\r
660\r
93723244
RN
661 if (Handle->Initialized != USBDBG_INIT_DONE ||\r
662 (MmioRead32 ((UINTN) &UsbDebugPortRegister->ControlStatus) & USB_DEBUG_PORT_ENABLE) == 0) {\r
663 DEBUG ((EFI_D_INFO, "UsbDbg: Reset the debug port.\n"));\r
93c0bdec 664 //\r
665 // Reset the debug port\r
666 //\r
667 MmioOr32((UINTN)PortStatus, BIT8);\r
668 MicroSecondDelay (500000);\r
669 MmioAnd32((UINTN)PortStatus, (UINT32)~BIT8);\r
670 while (MmioRead32((UINTN)PortStatus) & BIT8);\r
18b144ea 671\r
93c0bdec 672 //\r
673 // The port enabled bit should be set by HW.\r
674 //\r
675 if ((MmioRead32((UINTN)PortStatus) & BIT2) == 0) {\r
676 Handle->Initialized = USBDBG_NO_DBG_CAB;\r
677 return RETURN_DEVICE_ERROR;\r
678 }\r
18b144ea 679\r
93c0bdec 680 //\r
681 // Enable Usb Debug Port Capability\r
682 //\r
683 MmioOr32((UINTN)&UsbDebugPortRegister->ControlStatus, USB_DEBUG_PORT_ENABLE);\r
18b144ea 684\r
93c0bdec 685 //\r
686 // initialize the data toggle used by bulk in/out endpoint.\r
687 //\r
688 Handle->BulkInToggle = 0;\r
689 Handle->BulkOutToggle = 0;\r
18b144ea 690\r
18b144ea 691 //\r
93c0bdec 692 // set usb debug device address as 0x7F.\r
18b144ea 693 //\r
d6211ea1 694 Status = UsbDebugPortControlTransfer (UsbDebugPortRegister, &mDebugCommunicationLibUsbSetDebugAddress, 0x0, 0x0, NULL, NULL);\r
93c0bdec 695 if (RETURN_ERROR(Status)) {\r
696 //\r
697 // The device can not work well.\r
698 //\r
699 Handle->Initialized = USBDBG_NO_DBG_CAB;\r
700 return Status;\r
701 }\r
ec99fa8e 702\r
93c0bdec 703 //\r
704 // Start to communicate with Usb Debug Device to see if the attached device is usb debug device or not.\r
705 //\r
706 Length = (UINT8)sizeof (USB_DEBUG_PORT_DESCRIPTOR);\r
ec99fa8e 707\r
18b144ea 708 //\r
93c0bdec 709 // Get debug descriptor.\r
18b144ea 710 //\r
d6211ea1 711 Status = UsbDebugPortControlTransfer (UsbDebugPortRegister, &mDebugCommunicationLibUsbGetDebugDescriptor, 0x7F, 0x0, (UINT8*)&UsbDebugPortDescriptor, &Length);\r
93c0bdec 712 if (RETURN_ERROR(Status)) {\r
713 //\r
714 // The device is not a usb debug device.\r
715 //\r
716 Handle->Initialized = USBDBG_NO_DBG_CAB;\r
717 return Status;\r
718 }\r
719\r
720 if (Length != sizeof(USB_DEBUG_PORT_DESCRIPTOR)) {\r
721 Handle->Initialized = USBDBG_NO_DBG_CAB;\r
722 return RETURN_DEVICE_ERROR;\r
723 }\r
724\r
725 //\r
726 // enable the usb debug feature.\r
727 //\r
d6211ea1 728 Status = UsbDebugPortControlTransfer (UsbDebugPortRegister, &mDebugCommunicationLibUsbSetDebugFeature, 0x7F, 0x0, NULL, NULL);\r
93c0bdec 729 if (RETURN_ERROR(Status)) {\r
730 //\r
731 // The device can not work well.\r
732 //\r
733 Handle->Initialized = USBDBG_NO_DBG_CAB;\r
734 return Status;\r
735 }\r
736 \r
737 Handle->Initialized = USBDBG_DBG_CAB;\r
18b144ea 738 }\r
739\r
740 //\r
93c0bdec 741 // Set initialized flag\r
18b144ea 742 //\r
93c0bdec 743 Handle->Initialized = USBDBG_INIT_DONE;\r
18b144ea 744\r
93c0bdec 745 return RETURN_SUCCESS;\r
18b144ea 746}\r
747\r
748/**\r
749 Read data from debug device and save the datas in buffer.\r
750\r
751 Reads NumberOfBytes data bytes from a debug device into the buffer\r
752 specified by Buffer. The number of bytes actually read is returned.\r
753 If the return value is less than NumberOfBytes, then the rest operation failed.\r
754 If NumberOfBytes is zero, then return 0.\r
755\r
756 @param Handle Debug port handle.\r
757 @param Buffer Pointer to the data buffer to store the data read from the debug device.\r
758 @param NumberOfBytes Number of bytes which will be read.\r
759 @param Timeout Timeout value for reading from debug device. It unit is Microsecond.\r
760\r
761 @retval 0 Read data failed, no data is to be read.\r
762 @retval >0 Actual number of bytes read from debug device.\r
763\r
764**/\r
765UINTN\r
766EFIAPI\r
767DebugPortReadBuffer (\r
768 IN DEBUG_PORT_HANDLE Handle,\r
769 IN UINT8 *Buffer,\r
770 IN UINTN NumberOfBytes,\r
771 IN UINTN Timeout\r
772 )\r
773{\r
774 USB_DEBUG_PORT_HANDLE *UsbDebugPortHandle;\r
18b144ea 775 RETURN_STATUS Status;\r
18b144ea 776 UINT8 Index;\r
18b144ea 777\r
08021523 778 if (NumberOfBytes != 1 || Buffer == NULL || Timeout != 0) {\r
18b144ea 779 return 0;\r
780 }\r
781\r
18b144ea 782 //\r
783 // If Handle is NULL, it means memory is ready for use.\r
784 // Use global variable to store handle value.\r
785 //\r
786 if (Handle == NULL) {\r
d6211ea1 787 UsbDebugPortHandle = &mDebugCommunicationLibUsbDebugPortHandle;\r
18b144ea 788 } else {\r
789 UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;\r
790 }\r
791\r
792 if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r
793 Status = InitializeUsbDebugHardware (UsbDebugPortHandle);\r
794 if (RETURN_ERROR(Status)) {\r
795 return 0;\r
796 }\r
797 }\r
798\r
18b144ea 799 //\r
08021523 800 // Read data from buffer\r
18b144ea 801 //\r
08021523
JF
802 if (UsbDebugPortHandle->DataCount < 1) {\r
803 return 0;\r
804 } else {\r
805 *Buffer = UsbDebugPortHandle->Data[0];\r
806 for (Index = 0; Index < UsbDebugPortHandle->DataCount - 1; Index++) {\r
807 if ((Index + 1) >= USB_DEBUG_PORT_MAX_PACKET_SIZE) {\r
18b144ea 808 return 0;\r
809 }\r
08021523 810 UsbDebugPortHandle->Data[Index] = UsbDebugPortHandle->Data[Index + 1];\r
b422b62c 811 }\r
08021523
JF
812 UsbDebugPortHandle->DataCount = (UINT8)(UsbDebugPortHandle->DataCount - 1);\r
813 return 1;\r
18b144ea 814 }\r
18b144ea 815}\r
816\r
817/**\r
818 Write data from buffer to debug device.\r
819\r
820 Writes NumberOfBytes data bytes from Buffer to the debug device.\r
821 The number of bytes actually written to the debug device is returned.\r
822 If the return value is less than NumberOfBytes, then the write operation failed.\r
823 If NumberOfBytes is zero, then return 0.\r
824\r
825 @param Handle Debug port handle.\r
826 @param Buffer Pointer to the data buffer to be written.\r
827 @param NumberOfBytes Number of bytes to written to the debug device.\r
828\r
829 @retval 0 NumberOfBytes is 0.\r
830 @retval >0 The number of bytes written to the debug device.\r
831 If this value is less than NumberOfBytes, then the read operation failed.\r
832\r
833**/\r
834UINTN\r
835EFIAPI\r
836DebugPortWriteBuffer (\r
837 IN DEBUG_PORT_HANDLE Handle,\r
838 IN UINT8 *Buffer,\r
839 IN UINTN NumberOfBytes\r
840 )\r
841{\r
842 USB_DEBUG_PORT_HANDLE *UsbDebugPortHandle;\r
843 USB_DEBUG_PORT_REGISTER *UsbDebugPortRegister;\r
844 RETURN_STATUS Status;\r
845 UINT8 Sent;\r
846 UINTN Total;\r
847 UINT8 ReceivedPid;\r
848\r
849 if (NumberOfBytes == 0 || Buffer == NULL) {\r
850 return 0;\r
851 }\r
852\r
853 Sent = 0;\r
854 Total = 0;\r
855\r
856 //\r
857 // If Handle is NULL, it means memory is ready for use.\r
858 // Use global variable to store handle value.\r
859 //\r
860 if (Handle == NULL) {\r
d6211ea1 861 UsbDebugPortHandle = &mDebugCommunicationLibUsbDebugPortHandle;\r
18b144ea 862 } else {\r
863 UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;\r
864 }\r
865\r
866 if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r
867 Status = InitializeUsbDebugHardware (UsbDebugPortHandle);\r
868 if (RETURN_ERROR(Status)) {\r
869 return 0;\r
870 }\r
871 }\r
872\r
93c0bdec 873 UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);\r
18b144ea 874\r
875 while ((Total < NumberOfBytes)) {\r
876 if (NumberOfBytes - Total > USB_DEBUG_PORT_MAX_PACKET_SIZE) {\r
877 Sent = USB_DEBUG_PORT_MAX_PACKET_SIZE;\r
878 } else {\r
879 Sent = (UINT8)(NumberOfBytes - Total);\r
880 }\r
881\r
882 Status = UsbDebugPortOut(UsbDebugPortRegister, Buffer + Total, Sent, OUTPUT_PID, 0x7F, 0x01, UsbDebugPortHandle->BulkOutToggle);\r
883\r
884 if (RETURN_ERROR(Status)) {\r
885 return Total;\r
886 }\r
887\r
888 ReceivedPid = (MmioRead8((UINTN)&UsbDebugPortRegister->ReceivedPid));\r
889 //\r
890 // If received a NAK_PID on write transaction, it means the usb debug device is busy and can not handle this transaction.\r
891 // should send the packet again.\r
892 //\r
893 if (ReceivedPid == NAK_PID) {\r
894 Sent = 0;\r
895 } else {\r
896 UsbDebugPortHandle->BulkOutToggle ^= 1;\r
897 }\r
898 Total += Sent;\r
899 }\r
900 return Total;\r
901}\r
902\r
903/**\r
904 Polls a debug device to see if there is any data waiting to be read.\r
905\r
906 Polls a debug device to see if there is any data waiting to be read.\r
907 If there is data waiting to be read from the debug device, then TRUE is returned.\r
908 If there is no data waiting to be read from the debug device, then FALSE is returned.\r
909\r
910 @param Handle Debug port handle.\r
911\r
912 @retval TRUE Data is waiting to be read from the debug device.\r
913 @retval FALSE There is no data waiting to be read from the serial device.\r
914\r
915**/\r
916BOOLEAN\r
917EFIAPI\r
918DebugPortPollBuffer (\r
919 IN DEBUG_PORT_HANDLE Handle\r
920 )\r
921{\r
922 USB_DEBUG_PORT_HANDLE *UsbDebugPortHandle;\r
923 USB_DEBUG_PORT_REGISTER *UsbDebugPortRegister;\r
924 UINT8 Length;\r
925 UINT8 Index;\r
926 RETURN_STATUS Status;\r
927\r
928 //\r
929 // If Handle is NULL, it means memory is ready for use.\r
930 // Use global variable to store handle value.\r
931 //\r
932 if (Handle == NULL) {\r
d6211ea1 933 UsbDebugPortHandle = &mDebugCommunicationLibUsbDebugPortHandle;\r
18b144ea 934 } else {\r
935 UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;\r
936 }\r
937\r
938 if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r
939 Status = InitializeUsbDebugHardware(UsbDebugPortHandle);\r
940 if (RETURN_ERROR(Status)) {\r
941 return FALSE;\r
942 }\r
943 }\r
944\r
945 //\r
946 // If the data buffer is not empty, then return TRUE directly.\r
947 // else initialize a usb read transaction and read data to the data buffer.\r
948 //\r
949 if (UsbDebugPortHandle->DataCount != 0) {\r
950 return TRUE;\r
951 }\r
952\r
93c0bdec 953 UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);\r
18b144ea 954\r
955 UsbDebugPortRegister->TokenPid = INPUT_PID;\r
956 if (UsbDebugPortHandle->BulkInToggle == 0) {\r
957 UsbDebugPortRegister->SendPid = DATA0_PID;\r
958 } else {\r
959 UsbDebugPortRegister->SendPid = DATA1_PID;\r
960 }\r
961 UsbDebugPortRegister->UsbAddress = 0x7F;\r
962 UsbDebugPortRegister->UsbEndPoint = 0x82 & 0x0F;\r
963\r
964 //\r
965 // Clearing W/R bit to indicate it's a READ operation\r
966 //\r
967 MmioAnd32((UINTN)&UsbDebugPortRegister->ControlStatus, (UINT32)~BIT4);\r
968 //\r
969 // Setting GO bit as well as clearing DONE bit\r
970 //\r
971 MmioOr32((UINTN)&UsbDebugPortRegister->ControlStatus, (UINT32)BIT5);\r
972\r
973 //\r
974 // Wait for completing the request\r
975 //\r
93c0bdec 976 while ((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) & (UINT32)BIT16) == 0) {\r
977 if ((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE))\r
978 != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE)) {\r
979 return FALSE;\r
980 }\r
981 }\r
18b144ea 982\r
983 if ((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus)) & BIT6) {\r
984 return FALSE;\r
985 }\r
986\r
987 Length = (UINT8)(MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) & 0xF);\r
988\r
989 if (Length > 8) {\r
990 return FALSE;\r
991 }\r
992\r
993 UsbDebugPortHandle->BulkInToggle ^= 1;\r
994\r
995 if (Length == 0) {\r
996 return FALSE;\r
997 }\r
998\r
999 for (Index = 0; Index < Length; Index++) {\r
1000 UsbDebugPortHandle->Data[Index] = UsbDebugPortRegister->DataBuffer[Index];\r
1001 }\r
1002 UsbDebugPortHandle->DataCount = Length;\r
1003\r
1004 return TRUE;\r
1005}\r
1006\r
1007/**\r
1008 Initialize the debug port.\r
1009\r
74cdb367 1010 If Function is not NULL, Debug Communication Library will call this function\r
18b144ea 1011 by passing in the Context to be the first parameter. If needed, Debug Communication\r
1012 Library will create one debug port handle to be the second argument passing in\r
1013 calling the Function, otherwise it will pass NULL to be the second argument of\r
1014 Function.\r
1015\r
1016 If Function is NULL, and Context is not NULL, the Debug Communication Library could\r
1017 a) Return the same handle as passed in (as Context parameter).\r
f95e6f6b 1018 b) Ignore the input Context parameter and create new handle to be returned.\r
18b144ea 1019\r
1020 If parameter Function is NULL and Context is NULL, Debug Communication Library could\r
1021 created a new handle if needed and return it, otherwise it will return NULL.\r
1022\r
1023 @param[in] Context Context needed by callback function; it was optional.\r
1024 @param[in] Function Continue function called by Debug Communication library;\r
1025 it was optional.\r
1026\r
1027 @return The debug port handle created by Debug Communication Library if Function\r
1028 is not NULL.\r
1029\r
1030**/\r
1031DEBUG_PORT_HANDLE\r
1032EFIAPI\r
1033DebugPortInitialize (\r
1034 IN VOID *Context,\r
1035 IN DEBUG_PORT_CONTINUE Function\r
1036 )\r
1037{\r
1038 RETURN_STATUS Status;\r
1039 USB_DEBUG_PORT_HANDLE Handle;\r
d6211ea1
RN
1040\r
1041 //\r
b422b62c 1042 // Validate the PCD PcdDebugPortHandleBufferSize value \r
1043 //\r
1044 ASSERT (PcdGet16 (PcdDebugPortHandleBufferSize) == sizeof (USB_DEBUG_PORT_HANDLE));\r
1045\r
b422b62c 1046 if (Function == NULL && Context != NULL) {\r
1047 return (DEBUG_PORT_HANDLE *) Context;\r
1048 }\r
491ae245 1049 ZeroMem(&Handle, sizeof (USB_DEBUG_PORT_HANDLE));\r
18b144ea 1050\r
1051 Status = CalculateUsbDebugPortBar(&Handle.DebugPortOffset, &Handle.DebugPortBarNumber);\r
1052 if (RETURN_ERROR (Status)) {\r
93c0bdec 1053 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
e2104834 1054 goto Exit;\r
18b144ea 1055 }\r
1056\r
1057 Handle.EhciMemoryBase = 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET);\r
1058\r
1059 if (Handle.EhciMemoryBase == 0) {\r
1060 //\r
1061 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero\r
1062 //\r
1063 PciWrite32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET, PcdGet32(PcdUsbEhciMemorySpaceBase));\r
1064 Handle.EhciMemoryBase = 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET);\r
1065 }\r
1066\r
1067 Handle.UsbDebugPortMemoryBase = 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET + Handle.DebugPortBarNumber * 4);\r
1068\r
1069 if (Handle.UsbDebugPortMemoryBase == 0) {\r
1070 //\r
1071 // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero\r
1072 //\r
1073 PciWrite32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET + Handle.DebugPortBarNumber * 4, PcdGet32(PcdUsbDebugPortMemorySpaceBase));\r
1074 Handle.UsbDebugPortMemoryBase = 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET + Handle.DebugPortBarNumber * 4);\r
1075 }\r
1076\r
93c0bdec 1077 Handle.Initialized = USBDBG_RESET;\r
18b144ea 1078\r
93c0bdec 1079 if (NeedReinitializeHardware(&Handle)) {\r
1080 DEBUG ((EFI_D_ERROR, "UsbDbg: Start EHCI debug port initialization!\n"));\r
1081 Status = InitializeUsbDebugHardware (&Handle);\r
1082 if (RETURN_ERROR(Status)) {\r
b422b62c 1083 DEBUG ((EFI_D_ERROR, "UsbDbg: Failed, please check if USB debug cable is plugged into EHCI debug port correctly!\n"));\r
93c0bdec 1084 goto Exit;\r
1085 }\r
1086 }\r
e2104834 1087\r
1088Exit:\r
1089\r
18b144ea 1090 if (Function != NULL) {\r
1091 Function (Context, &Handle);\r
1092 } else {\r
d6211ea1 1093 CopyMem(&mDebugCommunicationLibUsbDebugPortHandle, &Handle, sizeof (USB_DEBUG_PORT_HANDLE));\r
18b144ea 1094 }\r
1095\r
d6211ea1 1096 return (DEBUG_PORT_HANDLE)(UINTN)&mDebugCommunicationLibUsbDebugPortHandle;\r
18b144ea 1097}\r
1098\r