summaryrefslogtreecommitdiff
path: root/Silicon/Intel/KabylakeSiliconPkg/Pch/PchInit/Smm/PchLanSxSmm.c
blob: d05cb61bbc4e7d05b7a5c9b9e5df309afe040c35 (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
/** @file
  PCH LAN Sx handler implementation.

Copyright (c) 2017, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials are licensed and made available under
the terms and conditions of the BSD License that accompanies this distribution.
The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include <Library/TimerLib.h>
#include "PchInitSmm.h"

//
// Maximum loop time for GbE status check
//
#define GBE_MAX_LOOP_TIME  4000

/**
  Checks if Lan is Enabled or Disabled

  @retval  BOOLEAN    TRUE if device is enabled, FALSE otherwise.
**/
BOOLEAN
IsGbeEnabled (
  VOID
  )
{
  UINTN  GbePciBase;

  GbePciBase = MmPciBase (
                 DEFAULT_PCI_BUS_NUMBER_PCH,
                 PCI_DEVICE_NUMBER_PCH_LAN,
                 PCI_FUNCTION_NUMBER_PCH_LAN
                 );

  if (MmioRead32 (GbePciBase) != 0xFFFFFFFF) {
    return TRUE;
  }

  return FALSE;
}



/**
  Test for MDIO operation complete.

  @param [in]  GbeBar   GbE MMIO space

  @retval EFI_SUCCESS
  @retval EFI_TIMEOUT
**/
EFI_STATUS
GbeMdiWaitReady (
  UINT32          GbeBar
  )
{
  UINT32  Count;

  for (Count = 0; Count < GBE_MAX_LOOP_TIME; ++Count) {
    if (MmioRead32 (GbeBar + R_PCH_LAN_CSR_MDIC) & B_PCH_LAN_CSR_MDIC_RB) {
      return EFI_SUCCESS;
    }
    MicroSecondDelay (50);
  }
  return EFI_TIMEOUT;
}

/**
  Acquire MDIO software semaphore.

  1. Ensure that MBARA offset F00h [5] = 1b
  2. Poll MBARA offset F00h [5] up to 200ms

  @param [in] GbeBar   GbE MMIO space

  @retval EFI_SUCCESS
  @retval EFI_TIMEOUT
**/
EFI_STATUS
GbeAcquireMdio (
  IN      UINT32  GbeBar
  )
{
  UINT32  ExtCnfCtrl;
  UINT32  Count;

  MmioOr32 (GbeBar + R_PCH_LAN_CSR_EXTCNF_CTRL, B_PCH_LAN_CSR_EXTCNF_CTRL_SWFLAG);
  for (Count = 0; Count < GBE_MAX_LOOP_TIME; ++Count) {
    ExtCnfCtrl = MmioRead32 (GbeBar + R_PCH_LAN_CSR_EXTCNF_CTRL);
    if (ExtCnfCtrl & B_PCH_LAN_CSR_EXTCNF_CTRL_SWFLAG) {
      return EFI_SUCCESS;
    }
    MicroSecondDelay (50);
  }
  ASSERT (FALSE);
  return EFI_TIMEOUT;
}


/**
  Release MDIO software semaphore by clearing MBARA offset F00h [5]

  @param [in]  GbeBar   GbE MMIO space
**/
VOID
GbeReleaseMdio (
  IN      UINT32  GbeBar
  )
{
  ASSERT (MmioRead32 (GbeBar + R_PCH_LAN_CSR_EXTCNF_CTRL) & B_PCH_LAN_CSR_EXTCNF_CTRL_SWFLAG);
  MmioAnd32 (GbeBar + R_PCH_LAN_CSR_EXTCNF_CTRL, (UINT32) ~B_PCH_LAN_CSR_EXTCNF_CTRL_SWFLAG);
  ASSERT ((MmioRead32 (GbeBar + R_PCH_LAN_CSR_EXTCNF_CTRL) & B_PCH_LAN_CSR_EXTCNF_CTRL_SWFLAG) == 0);
}

/**
  Perform MDI write.

  @param [in]  GbeBar   GbE MMIO space
  @param [in]  Data     Value to write in lower 16bits, upper 16bit may contain opcode.

  @return EFI_STATUS
**/
EFI_STATUS
GbeMdiWrite (
  IN      UINT32  GbeBar,
  IN      UINT32  Data
  )
{
  MmioWrite32 (GbeBar + R_PCH_LAN_CSR_MDIC, Data | 0x04000000);
  return GbeMdiWaitReady (GbeBar);
}

/**
  Perform MDI read.

  @param [in]  GbeBar   GbE MMIO space
  @param [in]  Command  MDI command
  @param [out] Data     Value read

  @return EFI_STATUS
**/
EFI_STATUS
GbeMdiRead (
  IN      UINT32  GbeBar,
  IN      UINT32  Command,
  OUT     UINT16  *Data
  )
{
  EFI_STATUS  Status;
  MmioWrite32 (GbeBar + R_PCH_LAN_CSR_MDIC, Command | 0x08000000);
  Status = GbeMdiWaitReady (GbeBar);
  *Data = (UINT16) MmioRead32 (GbeBar + R_PCH_LAN_CSR_MDIC);
  return Status;
}

/**
  Configure WOL during Sx entry.

  @param [in]  GbeBar   GbE MMIO space
**/
VOID
GbeWolWorkaround (
  IN      UINT32  GbeBar
  )
{
  UINT32      RAL0;
  UINT32      RAH0;
  EFI_STATUS  Status;
  UINT16      Data16;

  //
  // System BIOS performs read from MBARA offset 5400h [31:0] and MBARA offset 5404h [31:0]
  //
  RAL0 = MmioRead32 (GbeBar + R_PCH_LAN_CSR_RAL);
  RAH0 = MmioRead32 (GbeBar + R_PCH_LAN_CSR_RAH);

  //
  // Set MBARA offset 20h = 0x043f6400
  //
  Status = GbeMdiWrite (GbeBar, 0x043f6400);

  //
  // If timeout is reached, force MAC to SMB and try again
  // Assert if failed on second attempt
  //
  if (Status == EFI_TIMEOUT) {
    //
    // Change Extended Device Control Register BIT 11 to 1 which
    // forces the interface between the MAC and the Phy to be on SMBus.
    // Cleared on the assertion of PCI reset.
    //
    MmioOr32 (GbeBar + R_PCH_LAN_CSR_CTRL_EXT, B_PCH_LAN_CSR_CTRL_EXT_FORCE_SMB);
    Status = GbeMdiWrite (GbeBar, 0x043f6400);
  }
  if (EFI_ERROR (Status)) return;

  //
  // Wait 4 mSec
  //
  MicroSecondDelay (4000);

  //
  // Set MBARA offset 20h = 0x4310010
  //
  Status = GbeMdiWrite (GbeBar, 0x04310010);
  if (EFI_ERROR (Status)) return;

  //
  // Set MBARA offset 20h = 0x4320000 or with
  // the least significant word of MBARA offset 5400
  //
  Status = GbeMdiWrite (GbeBar, (0x04320000 | (RAL0 & 0xFFFF)));
  if (EFI_ERROR (Status)) return;

  //
  // Set MBARA offset 20h = 0x4310011
  //
  Status = GbeMdiWrite (GbeBar, 0x04310011);
  if (EFI_ERROR (Status)) return;

  //
  // Set MBARA offset 20h = 0x4320000 or with
  // the most significant word of MBARA offset 5400
  //
  Status = GbeMdiWrite (GbeBar, (0x04320000 | (RAL0 >> 16)));
  if (EFI_ERROR (Status)) return;

  //
  // Set MBARA offset 20h = 0x4310012
  //
  Status = GbeMdiWrite (GbeBar, 0x04310012);
  if (EFI_ERROR (Status)) return;

  //
  // Set MBARA offset 20h = 0x4320000 or with
  // the least significant word of MBARA offset 5404
  //
  Status = GbeMdiWrite (GbeBar, (0x04320000 | (RAH0 & B_PCH_LAN_CSR_RAH_RAH)));
  if (EFI_ERROR (Status)) return;

  //
  // Set MBARA offset 20h = 0x4310013
  //
  Status = GbeMdiWrite (GbeBar, 0x04310013);
  if (EFI_ERROR (Status)) return;

  //
  // Set MBARA offset 20h = 0x4328000
  //
  Status = GbeMdiWrite (GbeBar, 0x04328000);
  if (EFI_ERROR (Status)) return;

  //
  // Set MBARA offset 20h = 0x4310001
  //
  Status = GbeMdiWrite (GbeBar, 0x04310001);
  if (EFI_ERROR (Status)) return;

  //
  // Set MBARA offset 20h = 0x8320000
  //
  Status = GbeMdiRead (GbeBar, 0x08320000, &Data16);
  if (EFI_ERROR (Status)) return;

  //
  // Set MBARA offset 20h = 0x4320000 OR TEMP[15:0] OR 1
  //
  Status = GbeMdiWrite (GbeBar, 0x04320000 | Data16 | BIT0);
  if (EFI_ERROR (Status)) return;

  //
  // Set MBARA offset 20h = 0x43f6460
  //
  Status = GbeMdiWrite (GbeBar, 0x043f6460);
  if (EFI_ERROR (Status)) return;

  //
  // Wait 4 mSec
  //
  MicroSecondDelay (4000);

  //
  // Set MBARA offset 20h = 0x4310042
  //
  Status = GbeMdiWrite (GbeBar, 0x04310042);
  if (EFI_ERROR (Status)) return;

  //
  // Set MBARA offset 20h = 0x43F6020
  //
  Status = GbeMdiWrite (GbeBar, 0x043F6020);
  if (EFI_ERROR (Status)) return;

  //
  // Wait 4 mSec
  //
  MicroSecondDelay (4000);

  //
  // Set MBARA offset 20h = 0x8310000
  // TEMP[15:0] = MBARA + 20[15:0]
  //
  Status = GbeMdiRead (GbeBar, 0x08310000, &Data16);
  if (EFI_ERROR (Status)) return;

  //
  // Set MBARA + 20h = 4310000h or with the TEMP[15:0] OR 10h
  //
  GbeMdiWrite (GbeBar, 0x04310000 | Data16 | BIT4);
}

/**
  Additional Internal GbE Controller special cases WOL Support.

  System BIOS is required perform additional steps upon S0 to S3,4,5 transition
  when ME is off and GbE device in D0. This is needed to enable LAN wake
  in particular when platform is shut-down from EFI.
**/
VOID
GbeSxWorkaround (
  VOID
  )
{
  UINTN       LanRegBase;
  UINT32      GbeBar;
  EFI_STATUS  Status;

  LanRegBase = MmPciBase (
                 DEFAULT_PCI_BUS_NUMBER_PCH,
                 PCI_DEVICE_NUMBER_PCH_LAN,
                 PCI_FUNCTION_NUMBER_PCH_LAN
                 );

  if (MmioRead16 (LanRegBase + PCI_VENDOR_ID_OFFSET) == 0xFFFF) {
    return;
  }

  //
  // Check if GbE device is in D0
  //
  if ((MmioRead16 (LanRegBase + R_PCH_LAN_PMCS) & B_PCH_LAN_PMCS_PS) != V_PCH_LAN_PMCS_PS0) {
    return;
  }

  ASSERT (mResvMmioSize >= (1 << N_PCH_LAN_MBARA_ALIGN));
  GbeBar = (UINT32) mResvMmioBaseAddr;
  if (GbeBar == 0) {
    ASSERT (FALSE);
    return;
  }

  //
  // Enable MMIO decode using reserved range.
  //
  MmioAnd16 (LanRegBase + PCI_COMMAND_OFFSET, (UINT16) ~EFI_PCI_COMMAND_MEMORY_SPACE);
  MmioWrite32 (LanRegBase + R_PCH_LAN_MBARA, GbeBar);
  MmioOr16 (LanRegBase + PCI_COMMAND_OFFSET, EFI_PCI_COMMAND_MEMORY_SPACE);

  //
  // If MBARA offset 5800h [0] = 1b then proceed with the w/a
  //
  if (MmioRead32 (GbeBar + R_PCH_LAN_CSR_WUC) & B_PCH_LAN_CSR_WUC_APME) {
    Status = GbeAcquireMdio (GbeBar);
    ASSERT_EFI_ERROR (Status);
    if (!EFI_ERROR (Status)) {
      GbeWolWorkaround (GbeBar);
      GbeReleaseMdio (GbeBar);
    }
  }

  //
  // Disable MMIO decode.
  //
  MmioAnd16 (LanRegBase + PCI_COMMAND_OFFSET, (UINT16) ~EFI_PCI_COMMAND_MEMORY_SPACE);
  MmioWrite32 (LanRegBase + R_PCH_LAN_MBARA, 0);
}

/**
  Enable platform wake from LAN when in DeepSx if platform supports it.
  Called upon Sx entry.
**/
VOID
GbeConfigureDeepSxWake (
  VOID
  )
{
  UINT32      PchPwrmBase;

  PchPwrmBaseGet (&PchPwrmBase);

  if ((MmioRead32 (PchPwrmBase + R_PCH_PWRM_DSX_CFG) & (UINT32) B_PCH_PWRM_DSX_CFG_LAN_WAKE_EN) != 0) {
    IoOr32 ((UINTN) (mAcpiBaseAddr + R_PCH_ACPI_GPE0_EN_127_96), (UINT32) B_PCH_ACPI_GPE0_EN_127_96_LAN_WAKE);
  }
}

/**
  GbE Sx entry handler
**/
VOID
PchLanSxCallback (
  VOID
  )
{
  if (IsGbeEnabled ()) {
    GbeSxWorkaround ();
    GbeConfigureDeepSxWake ();

  }
}