hc
2024-03-22 a0752693d998599af469473b8dc239ef973a012f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/** @file
  Miscellaneous services internal to USB debug port implementation.
 
  Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent
 
**/
 
#include "Usb3DebugPortLibInternal.h"
 
/**
  Verifies if the bit positions specified by a mask are set in a register.
 
  @param[in, out] Register    UNITN register
  @param[in]      BitMask     32-bit mask
 
  @return  BOOLEAN  - TRUE  if all bits specified by the mask are enabled.
                    - FALSE even if one of the bits specified by the mask
                            is not enabled.
**/
BOOLEAN
XhcIsBitSet(
  IN OUT  UINTN  Register,
  IN      UINT32 BitMask
  )
{
  if ((MmioRead32 (Register) & (BitMask)) != 0) {
    return TRUE;
  }
  return FALSE;
}
 
/**
  Sets bits as per the enabled bit positions in the mask.
 
  @param[in, out] Register    UINTN register
  @param[in]      BitMask     32-bit mask
**/
VOID
XhcSetR32Bit(
  IN OUT  UINTN  Register,
  IN      UINT32 BitMask
  )
{
  UINT32    RegisterValue;
 
  RegisterValue = MmioRead32 (Register);
  RegisterValue |= (UINT32)(BitMask);
  MmioWrite32 (Register, RegisterValue);
}
 
/**
  Clears bits as per the enabled bit positions in the mask.
 
  @param[in, out] Register    UINTN register
  @param[in]      BitMask     32-bit mask
**/
VOID
XhcClrR32Bit(
  IN OUT  UINTN  Register,
  IN      UINT32 BitMask
  )
{
  UINT32    RegisterValue;
 
  RegisterValue = MmioRead32 (Register);
  RegisterValue &= (UINT32)(~(BitMask));
  MmioWrite32 (Register, RegisterValue);
}
 
/**
  Initialize the USB debug port hardware.
 
  If no initialization is required, then return RETURN_SUCCESS.
  If the serial device was successfully initialized, then return RETURN_SUCCESS.
  If the serial device could not be initialized, then return RETURN_DEVICE_ERROR.
 
  @retval RETURN_SUCCESS        The serial device was initialized.
  @retval RETURN_DEVICE_ERROR   The serial device could not be initialized.
**/
RETURN_STATUS
EFIAPI
Usb3DebugPortInitialize (
  VOID
  )
{
  USB3Initialize ();
  return RETURN_SUCCESS;
}
 
/**
  Write data from buffer to USB debug port.
 
  Writes NumberOfBytes data bytes from Buffer to the serial device.
  The number of bytes actually written to the serial device is returned.
  If the return value is less than NumberOfBytes, then the write operation failed.
  If Buffer is NULL, then ASSERT().
  If NumberOfBytes is zero, then return 0.
 
  @param  Buffer           Pointer to the data buffer to be written.
  @param  NumberOfBytes    Number of bytes to written to the serial device.
 
  @retval 0                NumberOfBytes is 0.
  @retval >0               The number of bytes written to the serial device.
                           If this value is less than NumberOfBytes, then the read operation failed.
**/
UINTN
EFIAPI
Usb3DebugPortWrite (
  IN UINT8     *Buffer,
  IN UINTN     NumberOfBytes
  )
{
  Usb3DbgOut (Buffer, &NumberOfBytes);
  return NumberOfBytes;
}
 
/**
  Read data from USB debug port and save the datas in buffer.
 
  Reads NumberOfBytes data bytes from a serial device into the buffer
  specified by Buffer. The number of bytes actually read is returned.
  If the return value is less than NumberOfBytes, then the rest operation failed.
  If Buffer is NULL, then ASSERT().
  If NumberOfBytes is zero, then return 0.
 
  @param  Buffer           Pointer to the data buffer to store the data read from the serial device.
  @param  NumberOfBytes    Number of bytes which will be read.
 
  @retval 0                Read data failed, no data is to be read.
  @retval >0               Actual number of bytes read from serial device.
**/
UINTN
EFIAPI
Usb3DebugPortRead (
  OUT UINT8   *Buffer,
  IN  UINTN   NumberOfBytes
  )
{
  Usb3DbgIn (Buffer, &NumberOfBytes);
  return NumberOfBytes;
}
 
/**
  Polls a USB debug port to see if there is any data waiting to be read.
 
  Polls a serial device to see if there is any data waiting to be read.
  If there is data waiting to be read from the serial device, then TRUE is returned.
  If there is no data waiting to be read from the serial device, then FALSE is returned.
 
  @retval TRUE             Data is waiting to be read from the serial device.
  @retval FALSE            There is no data waiting to be read from the serial device.
**/
BOOLEAN
EFIAPI
Usb3DebugPortPoll (
  VOID
  )
{
  return FALSE;
}
 
/**
  Write the data to the XHCI debug register.
 
  @param  Xhc          The XHCI Instance.
  @param  Offset       The offset of the runtime register.
  @param  Data         The data to write.
 
**/
VOID
XhcWriteDebugReg (
  IN USB3_DEBUG_PORT_INSTANCE *Xhc,
  IN UINT32                   Offset,
  IN UINT32                   Data
  )
{
  EFI_PHYSICAL_ADDRESS  DebugCapabilityBase;
 
  DebugCapabilityBase = Xhc->DebugCapabilityBase;
 
  MmioWrite32 ((UINTN)(DebugCapabilityBase + Offset), Data);
 
  return;
}
 
/**
  Read XHCI debug register.
 
  @param  Xhc          The XHCI Instance.
  @param  Offset       The offset of the debug register.
 
  @return The register content read
 
**/
UINT32
XhcReadDebugReg (
  IN  USB3_DEBUG_PORT_INSTANCE *Xhc,
  IN  UINT32                   Offset
  )
{
  UINT32                  Data;
  EFI_PHYSICAL_ADDRESS    DebugCapabilityBase;
 
  DebugCapabilityBase = Xhc->DebugCapabilityBase;
 
  Data = MmioRead32 ((UINTN)(DebugCapabilityBase + Offset));
 
  return Data;
}
 
/**
  Set one bit of the debug register while keeping other bits.
 
  @param  Xhc          The XHCI Instance.
  @param  Offset       The offset of the debug register.
  @param  Bit          The bit mask of the register to set.
 
**/
VOID
XhcSetDebugRegBit (
  IN USB3_DEBUG_PORT_INSTANCE *Xhc,
  IN UINT32                   Offset,
  IN UINT32                   Bit
  )
{
  UINT32                  Data;
 
  Data  = XhcReadDebugReg (Xhc, Offset);
  Data |= Bit;
  XhcWriteDebugReg (Xhc, Offset, Data);
}
 
/**
  Clear one bit of the debug register while keeping other bits.
 
  @param  Xhc          The XHCI Instance.
  @param  Offset       The offset of the debug register.
  @param  Bit          The bit mask of the register to set.
 
**/
VOID
XhcClearDebugRegBit (
  IN USB3_DEBUG_PORT_INSTANCE *Xhc,
  IN UINT32                   Offset,
  IN UINT32                   Bit
  )
{
  UINT32                  Data;
 
  Data  = XhcReadDebugReg (Xhc, Offset);
  Data  &= ~Bit;
  XhcWriteDebugReg (Xhc, Offset, Data);
}
 
/**
  Write the data to the XHCI MMIO register.
 
  @param  Xhc          The XHCI Instance.
  @param  Offset       The offset of the runtime register.
  @param  Data         The data to write.
 
**/
VOID
XhcWriteMmioReg (
  IN USB3_DEBUG_PORT_INSTANCE *Xhc,
  IN UINT32                   Offset,
  IN UINT32                   Data
  )
{
  EFI_PHYSICAL_ADDRESS    MmioBase;
 
  MmioBase = Xhc->XhcMmioBase;
  MmioWrite32 ((UINTN)(MmioBase + Offset), Data);
 
  return;
}
 
/**
  Read XHCI MMIO register.
 
  @param  Xhc          The XHCI Instance.
  @param  Offset       The offset of the runtime register.
 
  @return The register content read
 
**/
UINT32
XhcReadMmioReg (
  IN  USB3_DEBUG_PORT_INSTANCE *Xhc,
  IN  UINT32                   Offset
  )
{
  UINT32                  Data;
  EFI_PHYSICAL_ADDRESS    MmioBase;
 
  MmioBase = Xhc->XhcMmioBase;
 
  Data = MmioRead32 ((UINTN)(MmioBase + Offset));
 
  return Data;
}
 
/**
  Set one bit of the operational register while keeping other bits.
 
  @param  Xhc          The XHCI Instance.
  @param  Offset       The offset of the runtime register.
  @param  Bit          The bit mask of the register to set.
 
**/
VOID
XhcSetMmioRegBit (
  IN USB3_DEBUG_PORT_INSTANCE   *Xhc,
  IN UINT32                     Offset,
  IN UINT32                     Bit
  )
{
  UINT32                  Data;
 
  Data  = XhcReadMmioReg (Xhc, Offset);
  Data |= Bit;
  XhcWriteMmioReg (Xhc, Offset, Data);
}
 
/**
  Clear one bit of the operational register while keeping other bits.
 
  @param  Xhc          The XHCI Instance.
  @param  Offset       The offset of the runtime register.
  @param  Bit          The bit mask of the register to set.
 
**/
VOID
XhcClearMmioRegBit (
  IN USB3_DEBUG_PORT_INSTANCE   *Xhc,
  IN UINT32                     Offset,
  IN UINT32                     Bit
  )
{
  UINT32                  Data;
 
  Data  = XhcReadMmioReg (Xhc, Offset);
  Data  &= ~Bit;
  XhcWriteMmioReg (Xhc, Offset, Data);
}
 
/**
  Wait the operation register's bit as specified by Bit
  to be set (or clear).
 
  @param  Xhc          The XHCI Instance.
  @param  Offset       The offset of the operational register.
  @param  Bit          The bit of the register to wait for.
  @param  WaitToSet    Wait the bit to set or clear.
  @param  Timeout      The time to wait before abort (in millisecond, ms).
 
  @retval EFI_SUCCESS  The bit successfully changed by host controller.
  @retval EFI_TIMEOUT  The time out occurred.
 
**/
EFI_STATUS
XhcWaitMmioRegBit (
  IN USB3_DEBUG_PORT_INSTANCE *Xhc,
  IN UINT32                   Offset,
  IN UINT32                   Bit,
  IN BOOLEAN                  WaitToSet,
  IN UINT32                   Timeout
  )
{
  UINT32                  Index;
  UINTN                   Loop;
 
  Loop   = (Timeout * XHC_1_MILLISECOND / XHC_POLL_DELAY) + 1;
 
  for (Index = 0; Index < Loop; Index++) {
    if (XHC_REG_BIT_IS_SET (Xhc, Offset, Bit) == WaitToSet) {
      return EFI_SUCCESS;
    }
 
    MicroSecondDelay (XHC_POLL_DELAY);
  }
 
  return EFI_TIMEOUT;
}