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