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