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