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