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