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