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