]>
Commit | Line | Data |
---|---|---|
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 | |
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 | |
61 | USB_DEVICE_REQUEST mGetDebugDescriptor = {\r | |
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 | |
69 | USB_DEVICE_REQUEST mSetDebugFeature = {\r | |
70 | 0x0,\r | |
71 | USB_REQ_SET_FEATURE,\r | |
72 | (UINT16)(0x06),\r | |
73 | 0x0000,\r | |
74 | 0x0\r | |
75 | };\r | |
76 | \r | |
77 | USB_DEVICE_REQUEST mSetDebugAddress = {\r | |
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 | |
156 | USB_DEBUG_PORT_HANDLE mUsbDebugPortHandle;\r | |
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 | |
619 | //\r | |
620 | // If the host controller is not halted, then halt it.\r | |
621 | //\r | |
622 | if ((MmioRead32((UINTN)UsbStatus) & BIT12) == 0) {\r | |
623 | MmioAnd32((UINTN)UsbCmd, (UINT32)~BIT0);\r | |
624 | while ((MmioRead32((UINTN)UsbStatus) & BIT12) == 0);\r | |
625 | }\r | |
626 | //\r | |
627 | // reset the host controller.\r | |
628 | //\r | |
629 | MmioOr32((UINTN)UsbCmd, BIT1);\r | |
630 | //\r | |
631 | // ensure that the host controller is reset.\r | |
632 | //\r | |
633 | while (MmioRead32((UINTN)UsbCmd) & BIT1);\r | |
18b144ea | 634 | \r |
93c0bdec | 635 | //\r |
636 | // Start the host controller if it's not running\r | |
637 | //\r | |
638 | if (MmioRead32((UINTN)UsbStatus) & BIT12) {\r | |
639 | MmioOr32((UINTN)UsbCmd, BIT0);\r | |
640 | // ensure that the host controller is started (HALTED bit must be cleared)\r | |
641 | while (MmioRead32((UINTN)UsbStatus) & BIT12);\r | |
642 | }\r | |
18b144ea | 643 | \r |
93c0bdec | 644 | //\r |
645 | // First get the ownership of port 0.\r | |
646 | //\r | |
647 | MmioOr32((UINTN)&UsbDebugPortRegister->ControlStatus, USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE);\r | |
18b144ea | 648 | \r |
93c0bdec | 649 | MicroSecondDelay (200000);\r |
18b144ea | 650 | }\r |
18b144ea | 651 | //\r |
652 | // Find out which port is used as debug port.\r | |
653 | //\r | |
654 | DebugPortNumber = (UINT8)((MmioRead32((UINTN)UsbHCSParam) & 0x00F00000) >> 20);\r | |
655 | //\r | |
93c0bdec | 656 | // Should find a device is connected at debug port\r |
18b144ea | 657 | //\r |
93c0bdec | 658 | PortStatus = (UINT32 *)(UINTN)(Handle->EhciMemoryBase + 0x64 + (DebugPortNumber - 1) * 4);\r |
659 | if (!(MmioRead32((UINTN)PortStatus) & BIT0)) {\r | |
660 | Handle->Initialized = USBDBG_NO_DEV;\r | |
18b144ea | 661 | return RETURN_NOT_FOUND;\r |
662 | }\r | |
663 | \r | |
93c0bdec | 664 | if (Handle->Initialized != USBDBG_INIT_DONE) {\r |
665 | //\r | |
666 | // Reset the debug port\r | |
667 | //\r | |
668 | MmioOr32((UINTN)PortStatus, BIT8);\r | |
669 | MicroSecondDelay (500000);\r | |
670 | MmioAnd32((UINTN)PortStatus, (UINT32)~BIT8);\r | |
671 | while (MmioRead32((UINTN)PortStatus) & BIT8);\r | |
18b144ea | 672 | \r |
93c0bdec | 673 | //\r |
674 | // The port enabled bit should be set by HW.\r | |
675 | //\r | |
676 | if ((MmioRead32((UINTN)PortStatus) & BIT2) == 0) {\r | |
677 | Handle->Initialized = USBDBG_NO_DBG_CAB;\r | |
678 | return RETURN_DEVICE_ERROR;\r | |
679 | }\r | |
18b144ea | 680 | \r |
93c0bdec | 681 | //\r |
682 | // Enable Usb Debug Port Capability\r | |
683 | //\r | |
684 | MmioOr32((UINTN)&UsbDebugPortRegister->ControlStatus, USB_DEBUG_PORT_ENABLE);\r | |
18b144ea | 685 | \r |
93c0bdec | 686 | //\r |
687 | // initialize the data toggle used by bulk in/out endpoint.\r | |
688 | //\r | |
689 | Handle->BulkInToggle = 0;\r | |
690 | Handle->BulkOutToggle = 0;\r | |
18b144ea | 691 | \r |
18b144ea | 692 | //\r |
93c0bdec | 693 | // set usb debug device address as 0x7F.\r |
18b144ea | 694 | //\r |
93c0bdec | 695 | Status = UsbDebugPortControlTransfer (UsbDebugPortRegister, &mSetDebugAddress, 0x0, 0x0, NULL, NULL);\r |
696 | if (RETURN_ERROR(Status)) {\r | |
697 | //\r | |
698 | // The device can not work well.\r | |
699 | //\r | |
700 | Handle->Initialized = USBDBG_NO_DBG_CAB;\r | |
701 | return Status;\r | |
702 | }\r | |
ec99fa8e | 703 | \r |
93c0bdec | 704 | //\r |
705 | // Start to communicate with Usb Debug Device to see if the attached device is usb debug device or not.\r | |
706 | //\r | |
707 | Length = (UINT8)sizeof (USB_DEBUG_PORT_DESCRIPTOR);\r | |
ec99fa8e | 708 | \r |
18b144ea | 709 | //\r |
93c0bdec | 710 | // Get debug descriptor.\r |
18b144ea | 711 | //\r |
93c0bdec | 712 | Status = UsbDebugPortControlTransfer (UsbDebugPortRegister, &mGetDebugDescriptor, 0x7F, 0x0, (UINT8*)&UsbDebugPortDescriptor, &Length);\r |
713 | if (RETURN_ERROR(Status)) {\r | |
714 | //\r | |
715 | // The device is not a usb debug device.\r | |
716 | //\r | |
717 | Handle->Initialized = USBDBG_NO_DBG_CAB;\r | |
718 | return Status;\r | |
719 | }\r | |
720 | \r | |
721 | if (Length != sizeof(USB_DEBUG_PORT_DESCRIPTOR)) {\r | |
722 | Handle->Initialized = USBDBG_NO_DBG_CAB;\r | |
723 | return RETURN_DEVICE_ERROR;\r | |
724 | }\r | |
725 | \r | |
726 | //\r | |
727 | // enable the usb debug feature.\r | |
728 | //\r | |
729 | Status = UsbDebugPortControlTransfer (UsbDebugPortRegister, &mSetDebugFeature, 0x7F, 0x0, NULL, NULL);\r | |
730 | if (RETURN_ERROR(Status)) {\r | |
731 | //\r | |
732 | // The device can not work well.\r | |
733 | //\r | |
734 | Handle->Initialized = USBDBG_NO_DBG_CAB;\r | |
735 | return Status;\r | |
736 | }\r | |
737 | \r | |
738 | Handle->Initialized = USBDBG_DBG_CAB;\r | |
18b144ea | 739 | }\r |
740 | \r | |
741 | //\r | |
93c0bdec | 742 | // Set initialized flag\r |
18b144ea | 743 | //\r |
93c0bdec | 744 | Handle->Initialized = USBDBG_INIT_DONE;\r |
18b144ea | 745 | \r |
93c0bdec | 746 | return RETURN_SUCCESS;\r |
18b144ea | 747 | }\r |
748 | \r | |
749 | /**\r | |
750 | Read data from debug device and save the datas in buffer.\r | |
751 | \r | |
752 | Reads NumberOfBytes data bytes from a debug device into the buffer\r | |
753 | specified by Buffer. The number of bytes actually read is returned.\r | |
754 | If the return value is less than NumberOfBytes, then the rest operation failed.\r | |
755 | If NumberOfBytes is zero, then return 0.\r | |
756 | \r | |
757 | @param Handle Debug port handle.\r | |
758 | @param Buffer Pointer to the data buffer to store the data read from the debug device.\r | |
759 | @param NumberOfBytes Number of bytes which will be read.\r | |
760 | @param Timeout Timeout value for reading from debug device. It unit is Microsecond.\r | |
761 | \r | |
762 | @retval 0 Read data failed, no data is to be read.\r | |
763 | @retval >0 Actual number of bytes read from debug device.\r | |
764 | \r | |
765 | **/\r | |
766 | UINTN\r | |
767 | EFIAPI\r | |
768 | DebugPortReadBuffer (\r | |
769 | IN DEBUG_PORT_HANDLE Handle,\r | |
770 | IN UINT8 *Buffer,\r | |
771 | IN UINTN NumberOfBytes,\r | |
772 | IN UINTN Timeout\r | |
773 | )\r | |
774 | {\r | |
775 | USB_DEBUG_PORT_HANDLE *UsbDebugPortHandle;\r | |
776 | USB_DEBUG_PORT_REGISTER *UsbDebugPortRegister;\r | |
777 | RETURN_STATUS Status;\r | |
778 | UINT8 Received;\r | |
779 | UINTN Total;\r | |
780 | UINTN Remaining;\r | |
781 | UINT8 Index;\r | |
782 | UINT8 Length;\r | |
783 | \r | |
784 | if (NumberOfBytes == 0 || Buffer == NULL) {\r | |
785 | return 0;\r | |
786 | }\r | |
787 | \r | |
788 | Received = 0;\r | |
789 | Total = 0;\r | |
790 | Remaining = 0;\r | |
791 | \r | |
792 | //\r | |
793 | // If Handle is NULL, it means memory is ready for use.\r | |
794 | // Use global variable to store handle value.\r | |
795 | //\r | |
796 | if (Handle == NULL) {\r | |
797 | UsbDebugPortHandle = &mUsbDebugPortHandle;\r | |
798 | } else {\r | |
799 | UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;\r | |
800 | }\r | |
801 | \r | |
802 | if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r | |
803 | Status = InitializeUsbDebugHardware (UsbDebugPortHandle);\r | |
804 | if (RETURN_ERROR(Status)) {\r | |
805 | return 0;\r | |
806 | }\r | |
807 | }\r | |
808 | \r | |
93c0bdec | 809 | UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);\r |
18b144ea | 810 | \r |
811 | //\r | |
812 | // First read data from buffer, then read debug port hw to get received data.\r | |
813 | //\r | |
814 | if (UsbDebugPortHandle->DataCount > 0) {\r | |
815 | if (NumberOfBytes <= UsbDebugPortHandle->DataCount) {\r | |
816 | Total = NumberOfBytes;\r | |
817 | } else {\r | |
818 | Total = UsbDebugPortHandle->DataCount;\r | |
819 | }\r | |
820 | \r | |
821 | for (Index = 0; Index < Total; Index++) {\r | |
822 | Buffer[Index] = UsbDebugPortHandle->Data[Index];\r | |
823 | }\r | |
824 | \r | |
825 | for (Index = 0; Index < UsbDebugPortHandle->DataCount - Total; Index++) {\r | |
826 | if (Total + Index >= 8) {\r | |
827 | return 0;\r | |
828 | }\r | |
829 | UsbDebugPortHandle->Data[Index] = UsbDebugPortHandle->Data[Total + Index];\r | |
830 | }\r | |
831 | UsbDebugPortHandle->DataCount = (UINT8)(UsbDebugPortHandle->DataCount - (UINT8)Total);\r | |
832 | }\r | |
833 | \r | |
834 | //\r | |
835 | // If Timeout is equal to 0, then it means it should always wait until all datum required are received.\r | |
836 | //\r | |
837 | if (Timeout == 0) {\r | |
838 | Timeout = 0xFFFFFFFF;\r | |
839 | }\r | |
840 | \r | |
841 | //\r | |
842 | // Read remaining data by executing one or more usb debug transfer transactions at usb debug port hw.\r | |
843 | //\r | |
844 | while ((Total < NumberOfBytes) && (Timeout != 0)) {\r | |
845 | Remaining = NumberOfBytes - Total;\r | |
846 | if (Remaining >= USB_DEBUG_PORT_MAX_PACKET_SIZE) {\r | |
847 | Status = UsbDebugPortIn(UsbDebugPortRegister, Buffer + Total, &Received, INPUT_PID, 0x7f, 0x82, UsbDebugPortHandle->BulkInToggle);\r | |
848 | \r | |
849 | if (RETURN_ERROR(Status)) {\r | |
850 | return Total;\r | |
851 | }\r | |
852 | } else {\r | |
853 | Status = UsbDebugPortIn(UsbDebugPortRegister, &UsbDebugPortHandle->Data[0], &Received, INPUT_PID, 0x7f, 0x82, UsbDebugPortHandle->BulkInToggle);\r | |
854 | \r | |
855 | if (RETURN_ERROR(Status)) {\r | |
856 | return Total;\r | |
857 | }\r | |
858 | \r | |
859 | UsbDebugPortHandle->DataCount = Received;\r | |
860 | \r | |
861 | if (Remaining <= Received) {\r | |
862 | Length = (UINT8)Remaining;\r | |
863 | } else {\r | |
864 | Length = (UINT8)Received;\r | |
865 | }\r | |
866 | \r | |
867 | //\r | |
868 | // Copy required data from the data buffer to user buffer.\r | |
869 | //\r | |
870 | for (Index = 0; Index < Length; Index++) {\r | |
871 | (Buffer + Total)[Index] = UsbDebugPortHandle->Data[Index];\r | |
872 | UsbDebugPortHandle->DataCount--;\r | |
873 | }\r | |
874 | \r | |
875 | //\r | |
876 | // reorder the data buffer to make available data arranged from the beginning of the data buffer.\r | |
877 | //\r | |
878 | for (Index = 0; Index < Received - Length; Index++) {\r | |
879 | if (Length + Index >= 8) {\r | |
880 | return 0;\r | |
881 | }\r | |
882 | UsbDebugPortHandle->Data[Index] = UsbDebugPortHandle->Data[Length + Index];\r | |
883 | }\r | |
884 | //\r | |
885 | // fixup the real received length in Buffer.\r | |
886 | //\r | |
887 | Received = Length;\r | |
888 | }\r | |
889 | UsbDebugPortHandle->BulkInToggle ^= 1;\r | |
890 | \r | |
891 | Total += Received;\r | |
892 | Timeout -= 100;\r | |
893 | }\r | |
894 | \r | |
895 | return Total;\r | |
896 | }\r | |
897 | \r | |
898 | /**\r | |
899 | Write data from buffer to debug device.\r | |
900 | \r | |
901 | Writes NumberOfBytes data bytes from Buffer to the debug device.\r | |
902 | The number of bytes actually written to the debug device is returned.\r | |
903 | If the return value is less than NumberOfBytes, then the write operation failed.\r | |
904 | If NumberOfBytes is zero, then return 0.\r | |
905 | \r | |
906 | @param Handle Debug port handle.\r | |
907 | @param Buffer Pointer to the data buffer to be written.\r | |
908 | @param NumberOfBytes Number of bytes to written to the debug device.\r | |
909 | \r | |
910 | @retval 0 NumberOfBytes is 0.\r | |
911 | @retval >0 The number of bytes written to the debug device.\r | |
912 | If this value is less than NumberOfBytes, then the read operation failed.\r | |
913 | \r | |
914 | **/\r | |
915 | UINTN\r | |
916 | EFIAPI\r | |
917 | DebugPortWriteBuffer (\r | |
918 | IN DEBUG_PORT_HANDLE Handle,\r | |
919 | IN UINT8 *Buffer,\r | |
920 | IN UINTN NumberOfBytes\r | |
921 | )\r | |
922 | {\r | |
923 | USB_DEBUG_PORT_HANDLE *UsbDebugPortHandle;\r | |
924 | USB_DEBUG_PORT_REGISTER *UsbDebugPortRegister;\r | |
925 | RETURN_STATUS Status;\r | |
926 | UINT8 Sent;\r | |
927 | UINTN Total;\r | |
928 | UINT8 ReceivedPid;\r | |
929 | \r | |
930 | if (NumberOfBytes == 0 || Buffer == NULL) {\r | |
931 | return 0;\r | |
932 | }\r | |
933 | \r | |
934 | Sent = 0;\r | |
935 | Total = 0;\r | |
936 | \r | |
937 | //\r | |
938 | // If Handle is NULL, it means memory is ready for use.\r | |
939 | // Use global variable to store handle value.\r | |
940 | //\r | |
941 | if (Handle == NULL) {\r | |
942 | UsbDebugPortHandle = &mUsbDebugPortHandle;\r | |
943 | } else {\r | |
944 | UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;\r | |
945 | }\r | |
946 | \r | |
947 | if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r | |
948 | Status = InitializeUsbDebugHardware (UsbDebugPortHandle);\r | |
949 | if (RETURN_ERROR(Status)) {\r | |
950 | return 0;\r | |
951 | }\r | |
952 | }\r | |
953 | \r | |
93c0bdec | 954 | UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);\r |
18b144ea | 955 | \r |
956 | while ((Total < NumberOfBytes)) {\r | |
957 | if (NumberOfBytes - Total > USB_DEBUG_PORT_MAX_PACKET_SIZE) {\r | |
958 | Sent = USB_DEBUG_PORT_MAX_PACKET_SIZE;\r | |
959 | } else {\r | |
960 | Sent = (UINT8)(NumberOfBytes - Total);\r | |
961 | }\r | |
962 | \r | |
963 | Status = UsbDebugPortOut(UsbDebugPortRegister, Buffer + Total, Sent, OUTPUT_PID, 0x7F, 0x01, UsbDebugPortHandle->BulkOutToggle);\r | |
964 | \r | |
965 | if (RETURN_ERROR(Status)) {\r | |
966 | return Total;\r | |
967 | }\r | |
968 | \r | |
969 | ReceivedPid = (MmioRead8((UINTN)&UsbDebugPortRegister->ReceivedPid));\r | |
970 | //\r | |
971 | // If received a NAK_PID on write transaction, it means the usb debug device is busy and can not handle this transaction.\r | |
972 | // should send the packet again.\r | |
973 | //\r | |
974 | if (ReceivedPid == NAK_PID) {\r | |
975 | Sent = 0;\r | |
976 | } else {\r | |
977 | UsbDebugPortHandle->BulkOutToggle ^= 1;\r | |
978 | }\r | |
979 | Total += Sent;\r | |
980 | }\r | |
981 | return Total;\r | |
982 | }\r | |
983 | \r | |
984 | /**\r | |
985 | Polls a debug device to see if there is any data waiting to be read.\r | |
986 | \r | |
987 | Polls a debug device to see if there is any data waiting to be read.\r | |
988 | If there is data waiting to be read from the debug device, then TRUE is returned.\r | |
989 | If there is no data waiting to be read from the debug device, then FALSE is returned.\r | |
990 | \r | |
991 | @param Handle Debug port handle.\r | |
992 | \r | |
993 | @retval TRUE Data is waiting to be read from the debug device.\r | |
994 | @retval FALSE There is no data waiting to be read from the serial device.\r | |
995 | \r | |
996 | **/\r | |
997 | BOOLEAN\r | |
998 | EFIAPI\r | |
999 | DebugPortPollBuffer (\r | |
1000 | IN DEBUG_PORT_HANDLE Handle\r | |
1001 | )\r | |
1002 | {\r | |
1003 | USB_DEBUG_PORT_HANDLE *UsbDebugPortHandle;\r | |
1004 | USB_DEBUG_PORT_REGISTER *UsbDebugPortRegister;\r | |
1005 | UINT8 Length;\r | |
1006 | UINT8 Index;\r | |
1007 | RETURN_STATUS Status;\r | |
1008 | \r | |
1009 | //\r | |
1010 | // If Handle is NULL, it means memory is ready for use.\r | |
1011 | // Use global variable to store handle value.\r | |
1012 | //\r | |
1013 | if (Handle == NULL) {\r | |
1014 | UsbDebugPortHandle = &mUsbDebugPortHandle;\r | |
1015 | } else {\r | |
1016 | UsbDebugPortHandle = (USB_DEBUG_PORT_HANDLE *)Handle;\r | |
1017 | }\r | |
1018 | \r | |
1019 | if (NeedReinitializeHardware(UsbDebugPortHandle)) {\r | |
1020 | Status = InitializeUsbDebugHardware(UsbDebugPortHandle);\r | |
1021 | if (RETURN_ERROR(Status)) {\r | |
1022 | return FALSE;\r | |
1023 | }\r | |
1024 | }\r | |
1025 | \r | |
1026 | //\r | |
1027 | // If the data buffer is not empty, then return TRUE directly.\r | |
1028 | // else initialize a usb read transaction and read data to the data buffer.\r | |
1029 | //\r | |
1030 | if (UsbDebugPortHandle->DataCount != 0) {\r | |
1031 | return TRUE;\r | |
1032 | }\r | |
1033 | \r | |
93c0bdec | 1034 | UsbDebugPortRegister = (USB_DEBUG_PORT_REGISTER *)(UINTN)(UsbDebugPortHandle->UsbDebugPortMemoryBase + UsbDebugPortHandle->DebugPortOffset);\r |
18b144ea | 1035 | \r |
1036 | UsbDebugPortRegister->TokenPid = INPUT_PID;\r | |
1037 | if (UsbDebugPortHandle->BulkInToggle == 0) {\r | |
1038 | UsbDebugPortRegister->SendPid = DATA0_PID;\r | |
1039 | } else {\r | |
1040 | UsbDebugPortRegister->SendPid = DATA1_PID;\r | |
1041 | }\r | |
1042 | UsbDebugPortRegister->UsbAddress = 0x7F;\r | |
1043 | UsbDebugPortRegister->UsbEndPoint = 0x82 & 0x0F;\r | |
1044 | \r | |
1045 | //\r | |
1046 | // Clearing W/R bit to indicate it's a READ operation\r | |
1047 | //\r | |
1048 | MmioAnd32((UINTN)&UsbDebugPortRegister->ControlStatus, (UINT32)~BIT4);\r | |
1049 | //\r | |
1050 | // Setting GO bit as well as clearing DONE bit\r | |
1051 | //\r | |
1052 | MmioOr32((UINTN)&UsbDebugPortRegister->ControlStatus, (UINT32)BIT5);\r | |
1053 | \r | |
1054 | //\r | |
1055 | // Wait for completing the request\r | |
1056 | //\r | |
93c0bdec | 1057 | while ((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) & (UINT32)BIT16) == 0) {\r |
1058 | if ((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) & (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE))\r | |
1059 | != (USB_DEBUG_PORT_OWNER | USB_DEBUG_PORT_IN_USE | USB_DEBUG_PORT_ENABLE)) {\r | |
1060 | return FALSE;\r | |
1061 | }\r | |
1062 | }\r | |
18b144ea | 1063 | \r |
1064 | if ((MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus)) & BIT6) {\r | |
1065 | return FALSE;\r | |
1066 | }\r | |
1067 | \r | |
1068 | Length = (UINT8)(MmioRead32((UINTN)&UsbDebugPortRegister->ControlStatus) & 0xF);\r | |
1069 | \r | |
1070 | if (Length > 8) {\r | |
1071 | return FALSE;\r | |
1072 | }\r | |
1073 | \r | |
1074 | UsbDebugPortHandle->BulkInToggle ^= 1;\r | |
1075 | \r | |
1076 | if (Length == 0) {\r | |
1077 | return FALSE;\r | |
1078 | }\r | |
1079 | \r | |
1080 | for (Index = 0; Index < Length; Index++) {\r | |
1081 | UsbDebugPortHandle->Data[Index] = UsbDebugPortRegister->DataBuffer[Index];\r | |
1082 | }\r | |
1083 | UsbDebugPortHandle->DataCount = Length;\r | |
1084 | \r | |
1085 | return TRUE;\r | |
1086 | }\r | |
1087 | \r | |
1088 | /**\r | |
1089 | Initialize the debug port.\r | |
1090 | \r | |
1091 | If Function is not NULL, Debug Communication Libary will call this function\r | |
1092 | by passing in the Context to be the first parameter. If needed, Debug Communication\r | |
1093 | Library will create one debug port handle to be the second argument passing in\r | |
1094 | calling the Function, otherwise it will pass NULL to be the second argument of\r | |
1095 | Function.\r | |
1096 | \r | |
1097 | If Function is NULL, and Context is not NULL, the Debug Communication Library could\r | |
1098 | a) Return the same handle as passed in (as Context parameter).\r | |
1099 | b) Ignore the input Context parameter and create new hanlde to be returned.\r | |
1100 | \r | |
1101 | If parameter Function is NULL and Context is NULL, Debug Communication Library could\r | |
1102 | created a new handle if needed and return it, otherwise it will return NULL.\r | |
1103 | \r | |
1104 | @param[in] Context Context needed by callback function; it was optional.\r | |
1105 | @param[in] Function Continue function called by Debug Communication library;\r | |
1106 | it was optional.\r | |
1107 | \r | |
1108 | @return The debug port handle created by Debug Communication Library if Function\r | |
1109 | is not NULL.\r | |
1110 | \r | |
1111 | **/\r | |
1112 | DEBUG_PORT_HANDLE\r | |
1113 | EFIAPI\r | |
1114 | DebugPortInitialize (\r | |
1115 | IN VOID *Context,\r | |
1116 | IN DEBUG_PORT_CONTINUE Function\r | |
1117 | )\r | |
1118 | {\r | |
1119 | RETURN_STATUS Status;\r | |
1120 | USB_DEBUG_PORT_HANDLE Handle;\r | |
93c0bdec | 1121 | \r if (Function == NULL && Context != NULL) {\r |
18b144ea | 1122 | return (DEBUG_PORT_HANDLE *) Context;\r |
1123 | }\r | |
1124 | \r | |
1125 | ZeroMem(&Handle, sizeof (USB_DEBUG_PORT_HANDLE));\r | |
1126 | \r | |
1127 | Status = CalculateUsbDebugPortBar(&Handle.DebugPortOffset, &Handle.DebugPortBarNumber);\r | |
1128 | if (RETURN_ERROR (Status)) {\r | |
93c0bdec | 1129 | DEBUG ((EFI_D_ERROR, "UsbDbg: the pci device pointed by PcdUsbEhciPciAddress is not EHCI host controller or does not support debug port capability!\n"));\r |
e2104834 | 1130 | goto Exit;\r |
18b144ea | 1131 | }\r |
1132 | \r | |
1133 | Handle.EhciMemoryBase = 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET);\r | |
1134 | \r | |
1135 | if (Handle.EhciMemoryBase == 0) {\r | |
1136 | //\r | |
1137 | // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero\r | |
1138 | //\r | |
1139 | PciWrite32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET, PcdGet32(PcdUsbEhciMemorySpaceBase));\r | |
1140 | Handle.EhciMemoryBase = 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET);\r | |
1141 | }\r | |
1142 | \r | |
1143 | Handle.UsbDebugPortMemoryBase = 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET + Handle.DebugPortBarNumber * 4);\r | |
1144 | \r | |
1145 | if (Handle.UsbDebugPortMemoryBase == 0) {\r | |
1146 | //\r | |
1147 | // Usb Debug Port MMIO Space Is Not Enabled. Assumption here that DebugPortBase is zero\r | |
1148 | //\r | |
1149 | PciWrite32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET + Handle.DebugPortBarNumber * 4, PcdGet32(PcdUsbDebugPortMemorySpaceBase));\r | |
1150 | Handle.UsbDebugPortMemoryBase = 0xFFFFFC00 & PciRead32(PcdGet32(PcdUsbEhciPciAddress) + PCI_BASE_ADDRESSREG_OFFSET + Handle.DebugPortBarNumber * 4);\r | |
1151 | }\r | |
1152 | \r | |
93c0bdec | 1153 | Handle.Initialized = USBDBG_RESET;\r |
18b144ea | 1154 | \r |
93c0bdec | 1155 | if (NeedReinitializeHardware(&Handle)) {\r |
1156 | DEBUG ((EFI_D_ERROR, "UsbDbg: Start EHCI debug port initialization!\n"));\r | |
1157 | Status = InitializeUsbDebugHardware (&Handle);\r | |
1158 | if (RETURN_ERROR(Status)) {\r | |
1159 | DEBUG ((EFI_D_ERROR, "UsbDbg: Initialization failed, please check if USB debug cable is plugged into EHCI debug port correctly!\n"));\r | |
1160 | goto Exit;\r | |
1161 | }\r | |
1162 | }\r | |
e2104834 | 1163 | \r |
1164 | Exit:\r | |
1165 | \r | |
18b144ea | 1166 | if (Function != NULL) {\r |
1167 | Function (Context, &Handle);\r | |
1168 | } else {\r | |
1169 | CopyMem(&mUsbDebugPortHandle, &Handle, sizeof (USB_DEBUG_PORT_HANDLE));\r | |
1170 | }\r | |
1171 | \r | |
1172 | return (DEBUG_PORT_HANDLE)(UINTN)&mUsbDebugPortHandle;\r | |
1173 | }\r | |
1174 | \r |