summaryrefslogtreecommitdiff
path: root/ReferenceCode/Chipset/LynxPoint/PchInit/Dxe/PchUsb.c
blob: ddf41a835496c72ee714f1b3517cb24a43238993 (plain)
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/** @file
  Initializes PCH USB Controllers.

@copyright
  Copyright (c) 1999 - 2013 Intel Corporation. All rights reserved
  This software and associated documentation (if any) is furnished
  under a license and may only be used or copied in accordance
  with the terms of the license. Except as permitted by such
  license, no part of this software or documentation may be
  reproduced, stored in a retrieval system, or transmitted in any
  form or by any means without the express written consent of
  Intel Corporation.

  This file contains an 'Intel Peripheral Driver' and uniquely
  identified as "Intel Reference Module" and is
  licensed for Intel CPUs and chipsets under the terms of your
  license agreement with Intel or your vendor.  This file may
  be modified by the user, subject to additional terms of the
  license agreement
**/
#include "PchInit.h"

/**
  Lock USB registers before boot

  @param[in] PchPlatformPolicy    The PCH Platform Policy

  @retval None
**/
VOID
UsbInitBeforeBoot(
  IN  DXE_PCH_PLATFORM_POLICY_PROTOCOL  *PchPlatformPolicy
)
{
  EFI_STATUS                            Status;
  UINT32                                XhccCfg;
  UINTN                                 XhciPciMmBase;
  UINT32                                XhciMmioBase;
  UINT32                                Data32And;
  UINT32                                Data32Or;
  UINT32                                PchSeries;
  UINT16                                OrgCommandWord;
  BOOLEAN                               NeedGcdMemSpace;

  Data32And       = 0xFFFFFFFF;
  Data32Or        = 0x0;
  NeedGcdMemSpace = FALSE;

  if (PchPlatformPolicy->UsbConfig->Usb30Settings.Mode == PCH_XHCI_MODE_OFF) {
    return;
  }

  XhciPciMmBase   = MmPciAddress (
                      0,
                      PchPlatformPolicy->BusNumber,
                      PCI_DEVICE_NUMBER_PCH_XHCI,
                      PCI_FUNCTION_NUMBER_PCH_XHCI,
                      0
                      );
  XhciMmioBase = MmioRead32(XhciPciMmBase + R_PCH_XHCI_MEM_BASE) & ~(0x0F);
  if(XhciMmioBase == 0){
    ///
    /// Allocate GCD mem space
    ///
    XhciMmioBase = 0xFFFFFFFF;
    Status = gDS->AllocateMemorySpace (
                EfiGcdAllocateMaxAddressSearchBottomUp,
                    EfiGcdMemoryTypeMemoryMappedIo,
                    N_PCH_XHCI_MEM_ALIGN,
                    V_PCH_XHCI_MEM_LENGTH,
                    (EFI_PHYSICAL_ADDRESS *)&XhciMmioBase,
                    mImageHandle,
                    NULL
                    );
    ASSERT_EFI_ERROR (Status);
    if (EFI_ERROR (Status)) {
      return;
    }
    NeedGcdMemSpace = TRUE;
    MmioWrite32 (XhciPciMmBase + R_PCH_XHCI_MEM_BASE, XhciMmioBase);
  }
  PchSeries = GetPchSeries();

  ///
  ///Restore xHCI MMIO Enable
  ///
  OrgCommandWord = MmioRead16 (XhciPciMmBase + R_PCH_XHCI_COMMAND_REGISTER);
  MmioOr16 (
    XhciPciMmBase + R_PCH_XHCI_COMMAND_REGISTER,
    (UINT16) (B_PCH_XHCI_COMMAND_MSE | B_PCH_XHCI_COMMAND_BME)
    );
  PCH_INIT_COMMON_SCRIPT_MEM_WRITE (
    EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
    EfiBootScriptWidthUint16,
    (UINTN) (XhciPciMmBase + R_PCH_XHCI_COMMAND_REGISTER),
    1,
    (VOID *) (UINTN) (XhciPciMmBase + R_PCH_XHCI_COMMAND_REGISTER)
    );

  ///
  ///Restore xHCI BAR
  ///
  PCH_INIT_COMMON_SCRIPT_MEM_WRITE (
    EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
    EfiBootScriptWidthUint32,
    (UINTN) (XhciPciMmBase + R_PCH_XHCI_MEM_BASE),
    1,
    (VOID *) (UINTN) (XhciPciMmBase + R_PCH_XHCI_MEM_BASE)
    );

  if (PchSeries == PchH) {
    ///
    /// For LPT-H, Set xHCIBAR + 8144h[8, 7, 6] to 1b, 0b, 0b
    ///
    MmioAndThenOr32 (XhciMmioBase + 0x8144, (UINT32) ~(BIT7 | BIT6), (UINT32) (BIT8));
    PCH_INIT_COMMON_SCRIPT_MEM_WRITE (
      EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
      EfiBootScriptWidthUint32,
      (UINTN) (XhciMmioBase + 0x8144),
      1,
      (VOID *) (UINTN) (XhciMmioBase + 0x8144)
      );
  } else if (PchSeries == PchLp) {
    ///
    /// For LPT-LP, Set xHCIBAR + 8144h[8, 7, 6] to 1b, 1b, 1b
    ///
    MmioOr32 (XhciMmioBase + 0x8144, (UINT32) (BIT8 | BIT7 | BIT6));
    PCH_INIT_COMMON_SCRIPT_MEM_WRITE (
      EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
      EfiBootScriptWidthUint32,
      (UINTN) (XhciMmioBase + 0x8144),
      1,
      (VOID *) (UINTN) (XhciMmioBase + 0x8144)
      );
    ///
    /// For LPT-LP, Set xHCIBAR + 816Ch[19:0] to 000E0038h
    ///
    Data32And = (UINT32) ~(0x000FFFFF);
    Data32Or  = (UINT32) (0x000E0038);
    MmioAndThenOr32 (
     (XhciMmioBase + 0x816C),
      Data32And,
      Data32Or
      );
    PCH_INIT_COMMON_SCRIPT_MEM_WRITE (
      EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
      EfiBootScriptWidthUint32,
      (UINTN) (XhciMmioBase + 0x816C),
      1,
      (VOID *) (UINTN) (XhciMmioBase + 0x816C)
      );
    ///
    /// For LPT-LP, Set D20:F0:B0h[17,14,13] to 1b, 0b, 0b
    ///
    MmioAndThenOr32 (XhciPciMmBase + 0xB0, (UINT32) ~(BIT14 | BIT13),  (UINT32) (BIT17));
    PCH_INIT_COMMON_SCRIPT_MEM_WRITE (
      EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
      EfiBootScriptWidthUint32,
      (UINTN) (XhciPciMmBase + 0xB0),
      1,
      (VOID *) (UINTN) (XhciPciMmBase + 0xB0)
      );
  }

  ///
  /// Set D20:F0:50h[28:0] to 0FCE2E5F for LPT-LP
  /// Set D20:F0:50h[26:0] to 07886E9Fh for LPT-H B0 onward
  ///
  if (PchSeries == PchH) {
    Data32And = (UINT32)~(0x07FFFFFF);
    Data32Or  = (UINT32) (0x07886E9F);
  } else if (PchSeries == PchLp) {
    Data32And = (UINT32) ~(0x1FFFFFFF);
    Data32Or  = (UINT32) (0x0FCE2E5F);
  }
  MmioAndThenOr32 (
   (XhciPciMmBase + 0x50),
    Data32And,
    Data32Or
    );
  PCH_INIT_COMMON_SCRIPT_MEM_WRITE (
    EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
    EfiBootScriptWidthUint32,
    (UINTN) (XhciPciMmBase + 0x50),
    1,
    (VOID *) (UINTN) (XhciPciMmBase + 0x50)
    );

  if ((GetBootModeHob () == BOOT_ON_S4_RESUME) &&
      (PchPlatformPolicy->UsbConfig->UsbPrecondition == PCH_DEVICE_ENABLE)) {
    ///
    /// For LPT-LP, Set xHCIBAR + 80E0[24] to 1h
    ///
    MmioOr32 (XhciMmioBase + 0x80E0, (UINT32) (BIT24));

    ///
    /// For LPT-LP, Set xHCIBAR + 80E0[24] to 0h
    ///
    MmioAnd32 (XhciMmioBase + 0x80E0, (UINT32) ~(BIT24));
  }

  ///
  /// PCH BIOS Spec xHCI controller setup
  /// Note:
  /// D20:F0:40h is write once register.
  /// Unsupported Request Detected bit is write clear
  ///
  XhccCfg = MmioRead32 (XhciPciMmBase + R_PCH_XHCI_XHCC1);
  XhccCfg &= (UINT32) ~(B_PCH_XHCI_XHCC1_URD);
  ///
  /// PCH BIOS Spec Rev 0.5.5, Section 13.2.4 Locking xHCI Register Settings
  /// PCH BIOS Spec Locking xHCI Register settings
  /// After xHCI is initialized, BIOS should lock the xHCI configuration registers to RO.
  /// This prevent any unintended changes. There is also a lockdown feature for OverCurrent
  /// registers. BIOS should set these bits to lock down the settings prior to end of POST.
  /// 1. Set Access Control bit at D20:F0:40h[31] to 1b to lock xHCI register settings.
  /// 2. Set OC Configuration Done bit at D20:F0:44h[31] to lock overcurrent mappings from
  ///    further changes.
  ///
  MmioOr32 (XhciPciMmBase + 0x44, (UINT32) (BIT31));
  PCH_INIT_COMMON_SCRIPT_MEM_WRITE (
    EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
    EfiBootScriptWidthUint32,
    (UINTN) (XhciPciMmBase + 0x44),
    1,
    (VOID *) (UINTN) (XhciPciMmBase + 0x44)
    );
  XhccCfg |= (UINT32) (B_PCH_XHCI_XHCC1_ACCTRL);
  MmioWrite32 (XhciPciMmBase + R_PCH_XHCI_XHCC1, XhccCfg);
  PCH_INIT_COMMON_SCRIPT_MEM_WRITE (
    EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
    EfiBootScriptWidthUint32,
    (UINTN) (XhciPciMmBase + R_PCH_XHCI_XHCC1),
    1,
    &XhccCfg
    );

  ///
  ///restore xHCI original command byte
  ///
  MmioWrite16 ((XhciPciMmBase + R_PCH_XHCI_COMMAND_REGISTER), OrgCommandWord);
  PCH_INIT_COMMON_SCRIPT_MEM_WRITE (
    EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
    EfiBootScriptWidthUint16,
    (UINTN) (XhciPciMmBase + R_PCH_XHCI_COMMAND_REGISTER),
    1,
    (VOID *) (UINTN) (XhciPciMmBase + R_PCH_XHCI_COMMAND_REGISTER)
    );

  if (NeedGcdMemSpace) {
    MmioWrite32 (XhciPciMmBase + R_PCH_XHCI_MEM_BASE, 0);
    ///
    ///clear xHCI BAR
    ///
    PCH_INIT_COMMON_SCRIPT_MEM_WRITE (
      EFI_ACPI_S3_RESUME_SCRIPT_TABLE,
      EfiBootScriptWidthUint32,
      (UINTN) (XhciPciMmBase + R_PCH_XHCI_MEM_BASE),
      1,
      (VOID *) (UINTN) (XhciPciMmBase + R_PCH_XHCI_MEM_BASE)
      );
    ///
    /// release GCD Mem space
    ///
    gDS->FreeMemorySpace (
           XhciMmioBase,
           V_PCH_XHCI_MEM_LENGTH
           );
  }

}

/**
  Configures ports of the PCH USB3 (xHCI) controller
  just before OS boot.

  @param[in] PchPlatformPolicy    The PCH Platform Policy protocol instance

  @retval EFI_INVALID_PARAMETER   The parameter of PchPlatformPolicy is invalid
  @retval EFI_SUCCESS             The function completed successfully
**/
VOID
ConfigureXhciAtBoot (
  IN DXE_PCH_PLATFORM_POLICY_PROTOCOL *PchPlatformPolicy
  )
{
  UINTN   PciD20F0RegBase;
  UINT32  PortMask;

  DEBUG ((EFI_D_INFO, "ConfigureXhciAtBoot() Start\n"));

  PciD20F0RegBase = MmPciAddress (
                      0,
                      PchPlatformPolicy->BusNumber,
                      PCI_DEVICE_NUMBER_PCH_XHCI,
                      PCI_FUNCTION_NUMBER_PCH_XHCI,
                      0
                      );

  ///
  /// PCH BIOS Spec Rev 0.5.0
  /// When the BIOS does not have xHCI pre-boot software available:
  /// Section 13.1.1.2 xHCI Enabled mode
  /// BIOS should route the Ports to the EHCI controller and prior to OS boot
  /// it should route the ports to the xHCI controller.
  ///
  if ((PchPlatformPolicy->UsbConfig->Usb30Settings.Mode == PCH_XHCI_MODE_ON) &&
      (PchPlatformPolicy->UsbConfig->Usb30Settings.PreBootSupport == PCH_DEVICE_DISABLE)) {
    ///
    /// PCH BIOS Spec Rev 0.5.0 Section 13.2.6 Routing of switchable USB Ports to
    /// xHCI Controller
    /// Step 1
    /// Done in GetXhciPortsNumber()
    /// Step 2
    /// Program D20:F0:D8h[5:0] to the value of xHCI D20:F0:DCh[5:0]
    ///
    PortMask = MmioRead32 (PciD20F0RegBase + R_PCH_XHCI_USB3PRM);

    MmioAndThenOr32 (
      PciD20F0RegBase + R_PCH_XHCI_USB3PR,
      (UINT32)~B_PCH_XHCI_USB3PR_USB3SSEN,
      PortMask
      );
    ///
    /// Step 3
    /// Program D20:F0:D0h[14:0] to the value of xHCI D20:F0:D4h[15:0]
    ///
    PortMask = MmioRead32 (PciD20F0RegBase + R_PCH_XHCI_USB2PRM);

    MmioAndThenOr32 (
      PciD20F0RegBase + R_PCH_XHCI_USB2PR,
      (UINT32)~B_PCH_XHCI_USB2PR_USB2HCSEL,
      PortMask
      );
    ///
    /// Note: Registers USB3PR[5:0] and USB2PR[14:0] are located in SUS well so BIOS doesn't
    ///       need to restore them during S3 resume, but needs to restore corresponding mask
    ///       registers. For RapidStart resume from G3 state support, HC Switch driver will call
    ///       _OSC method to restore USB2PR and USB3PR.
  }
  
  DEBUG ((EFI_D_INFO, "ConfigureXhciAtBoot() End\n"));
}

/**
  Configures PCH USB controller

  @param[in] PchPlatformPolicy    The PCH Platform Policy protocol instance
  @param[in] RootComplexBar       RootComplexBar address of this PCH device
  @param[in, out] FuncDisableReg  Function Disable Register

  @retval EFI_INVALID_PARAMETER   The parameter of PchPlatformPolicy is invalid
  @retval EFI_SUCCESS             The function completed successfully
**/
EFI_STATUS
ConfigureUsb (
  IN      DXE_PCH_PLATFORM_POLICY_PROTOCOL    *PchPlatformPolicy,
  IN      UINT32                              RootComplexBar,
  IN OUT  UINT32                              *FuncDisableReg
  )
{
  EFI_STATUS            Status;
  UINT8                 BusNumber;
  PCH_USB_CONFIG        *UsbConfig;
  UINT32                UsbFuncDisable;
  EFI_PHYSICAL_ADDRESS  EhciMemBaseAddress;
  EFI_PHYSICAL_ADDRESS  XhciMemBaseAddress;

  DEBUG ((EFI_D_INFO, "ConfigureUsb() Start\n"));

  BusNumber          = PchPlatformPolicy->BusNumber;
  UsbConfig          = PchPlatformPolicy->UsbConfig;
  EhciMemBaseAddress = 0x0ffffffff;

  Status = gDS->AllocateMemorySpace (
                  EfiGcdAllocateMaxAddressSearchBottomUp,
                  EfiGcdMemoryTypeMemoryMappedIo,
                  N_PCH_EHCI_MEM_ALIGN,
                  V_PCH_EHCI_MEM_LENGTH,
                  &EhciMemBaseAddress,
                  mImageHandle,
                  NULL
                  );
  ASSERT_EFI_ERROR (Status);
  if (EFI_ERROR (Status)) {
    return Status;
  }

  XhciMemBaseAddress = 0x0ffffffff;

  Status = gDS->AllocateMemorySpace (
                  EfiGcdAllocateMaxAddressSearchBottomUp,
                  EfiGcdMemoryTypeMemoryMappedIo,
                  N_PCH_XHCI_MEM_ALIGN,
                  V_PCH_XHCI_MEM_LENGTH,
                  &XhciMemBaseAddress,
                  mImageHandle,
                  NULL
                  );
  ASSERT_EFI_ERROR (Status);
  if (EFI_ERROR (Status)) {

    gDS->FreeMemorySpace (
          EhciMemBaseAddress,
          V_PCH_EHCI_MEM_LENGTH
          );

    return Status;
  }

  UsbFuncDisable = *FuncDisableReg;

  Status = CommonUsbInit (
            UsbConfig,
            (UINT32) EhciMemBaseAddress,
            (UINT32) XhciMemBaseAddress,
            BusNumber,
            RootComplexBar,
            &UsbFuncDisable,
            PchPlatformPolicy->Revision
            );
  *FuncDisableReg = UsbFuncDisable;

  //
  // Free allocated resources
  //
  gDS->FreeMemorySpace (
        EhciMemBaseAddress,
        V_PCH_EHCI_MEM_LENGTH
        );

  gDS->FreeMemorySpace (
        XhciMemBaseAddress,
        V_PCH_XHCI_MEM_LENGTH
        );
  DEBUG ((EFI_D_INFO, "ConfigureUsb() End\n"));

  return EFI_SUCCESS;
}