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