hc
2024-03-26 e0728245c89800c2038c23308f2d88969d5b41c8
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
/** @file
 
  Copyright (c) 2020 Jared McNeill. All rights reserved.
  Copyright (c) 2020 Andrey Warkentin <andrey.warkentin@gmail.com>
 
  SPDX-License-Identifier: BSD-2-Clause-Patent
 
**/
 
#include <Uefi.h>
#include <Library/DebugLib.h>
#include <Library/UefiBootServicesTableLib.h>
 
#include "GenericPhy.h"
 
#define PHY_RESET_TIMEOUT       500
 
/**
  Perform a PHY register read.
 
  @param  Phy[in]      Pointer to GENERIC_PHY_PRIVATE_DATA.
  @param  PhyAddr[in]  PHY address.
  @param  Reg[in]      PHY register.
  @param  Data[out]    Pointer to register data read.
 
  @retval EFI_SUCCESS       Data read successfully.
  @retval EFI_DEVICE_ERROR  Failed to read data.
 
**/
STATIC
EFI_STATUS
GenericPhyRead (
  IN  GENERIC_PHY_PRIVATE_DATA    *Phy,
  IN  UINT8                       PhyAddr,
  IN  UINT8                       Reg,
  OUT UINT16                      *Data
  )
{
  return Phy->Read (Phy->PrivateData, PhyAddr, Reg, Data);
}
 
/**
  Perform a PHY register write.
 
  @param  Phy[in]      Pointer to GENERIC_PHY_PRIVATE_DATA.
  @param  PhyAddr[in]  PHY address.
  @param  Reg[in]      PHY register.
  @param  Data[in]     Pointer to register data to write.
 
  @retval EFI_SUCCESS  Data written successfully.
  @retval EFI_DEVICE_ERROR  Failed to write data.
 
**/
STATIC
EFI_STATUS
GenericPhyWrite (
  IN GENERIC_PHY_PRIVATE_DATA   *Phy,
  IN UINT8                      PhyAddr,
  IN UINT8                      Reg,
  IN UINT16                     Data
  )
{
  return Phy->Write (Phy->PrivateData, PhyAddr, Reg, Data);
}
 
/**
  Process a PHY link speed change (e.g. with MAC layer).
 
  @param  Phy[in]     Pointer to GENERIC_PHY_PRIVATE_DATA.
  @param  Speed[in]   Speed setting.
  @param  Duplex[in]  Duplex setting.
 
**/
STATIC
VOID
GenericPhyConfigure (
  IN GENERIC_PHY_PRIVATE_DATA   *Phy,
  IN GENERIC_PHY_SPEED          Speed,
  IN GENERIC_PHY_DUPLEX         Duplex
  )
{
  Phy->Configure (Phy->PrivateData, Speed, Duplex);
}
 
/**
  Detect address for the first PHY seen, probing all possible addresses.
 
  @param  Phy[in]  Pointer to GENERIC_PHY_PRIVATE_DATA.
 
  @retval EFI_SUCCESS       Found a PHY and programmed Phy->PhyAddr
  @retval EFI_DEVICE_ERROR  Error reading/writing a PHY register.
  @retval EFI_NOT_FOUND     No PHY detected.
 
**/
STATIC
EFI_STATUS
GenericPhyDetect (
  IN GENERIC_PHY_PRIVATE_DATA   *Phy
  )
{
  EFI_STATUS  Status;
  UINT8       PhyAddr;
  UINT16      Id1, Id2;
 
  for (PhyAddr = 0; PhyAddr < 32; PhyAddr++) {
    Status = GenericPhyRead (Phy, PhyAddr, GENERIC_PHY_PHYIDR1, &Id1);
    if (EFI_ERROR (Status)) {
      continue;
    }
    Status = GenericPhyRead (Phy, PhyAddr, GENERIC_PHY_PHYIDR2, &Id2);
    if (EFI_ERROR (Status)) {
      continue;
    }
    if (Id1 != 0xFFFF && Id2 != 0xFFFF) {
      Phy->PhyAddr = PhyAddr;
      DEBUG ((DEBUG_INFO,
        "%a: PHY detected at address 0x%02X (PHYIDR1=0x%04X, PHYIDR2=0x%04X)\n",
        __FUNCTION__, PhyAddr, Id1, Id2));
      return EFI_SUCCESS;
    }
  }
 
  return EFI_NOT_FOUND;
}
 
/**
  Start link auto-negotiation on a PHY.
 
  @param  Phy[in]  Pointer to GENERIC_PHY_PRIVATE_DATA.
 
  @retval EFI_SUCCESS       Auto-netogiation started.
  @retval EFI_DEVICE_ERROR  PHY register read/write error.
 
**/
STATIC
EFI_STATUS
GenericPhyAutoNegotiate (
  IN GENERIC_PHY_PRIVATE_DATA   *Phy
  )
{
  EFI_STATUS    Status;
  UINT16        Anar, Gbcr, Bmcr;
 
  Status = GenericPhyRead (Phy, Phy->PhyAddr, GENERIC_PHY_ANAR, &Anar);
  if (EFI_ERROR (Status)) {
    return Status;
  }
  Anar |= GENERIC_PHY_ANAR_100BASETX_FDX |
          GENERIC_PHY_ANAR_100BASETX |
          GENERIC_PHY_ANAR_10BASET_FDX |
          GENERIC_PHY_ANAR_10BASET;
  Status = GenericPhyWrite (Phy, Phy->PhyAddr, GENERIC_PHY_ANAR, Anar);
  if (EFI_ERROR (Status)) {
    return Status;
  }
 
  Status = GenericPhyRead (Phy, Phy->PhyAddr, GENERIC_PHY_GBCR, &Gbcr);
  if (EFI_ERROR (Status)) {
    return Status;
  }
  Gbcr |= GENERIC_PHY_GBCR_1000BASET_FDX |
          GENERIC_PHY_GBCR_1000BASET;
  Status = GenericPhyWrite (Phy, Phy->PhyAddr, GENERIC_PHY_GBCR, Gbcr);
  if (EFI_ERROR (Status)) {
    return Status;
  }
 
  Status = GenericPhyRead (Phy, Phy->PhyAddr, GENERIC_PHY_BMCR, &Bmcr);
  if (EFI_ERROR (Status)) {
    return Status;
  }
  Bmcr |= GENERIC_PHY_BMCR_ANE |
          GENERIC_PHY_BMCR_RESTART_AN;
  return GenericPhyWrite (Phy, Phy->PhyAddr, GENERIC_PHY_BMCR, Bmcr);
}
 
/**
  Initialize the first PHY detected, performing a reset and enabling
  auto-negotiation.
 
  @param  Phy[in]  Pointer to GENERIC_PHY_PRIVATE_DATA.
 
  @retval EFI_SUCCESS       Auto-negotiation started.
  @retval EFI_DEVICE_ERROR  PHY register read/write error.
  @retval EFI_TIMEOUT       PHY reset time-out.
  @retval EFI_NOT_FOUND     No PHY detected.
 
**/
EFI_STATUS
EFIAPI
GenericPhyInit (
  IN GENERIC_PHY_PRIVATE_DATA   *Phy
  )
{
  EFI_STATUS    Status;
 
  ASSERT (Phy->Read != NULL);
  ASSERT (Phy->Write != NULL);
  ASSERT (Phy->Configure != NULL);
 
  Status = GenericPhyDetect (Phy);
  if (EFI_ERROR (Status)) {
    return Status;
  }
 
  Status = GenericPhyReset (Phy);
  if (EFI_ERROR (Status)) {
    return Status;
  }
 
  return GenericPhyAutoNegotiate (Phy);
}
 
/**
  Perform a PHY reset.
 
  @param  Phy[in]  Pointer to GENERIC_PHY_PRIVATE_DATA.
 
  @retval EFI_SUCCESS       Auto-negotiation started.
  @retval EFI_DEVICE_ERROR  PHY register read/write error.
  @retval EFI_TIMEOUT       PHY reset time-out.
 
**/
EFI_STATUS
EFIAPI
GenericPhyReset (
  IN GENERIC_PHY_PRIVATE_DATA   *Phy
  )
{
  EFI_STATUS    Status;
  UINTN         Retry;
  UINT16        Data;
 
  // Start reset sequence
  Status = GenericPhyWrite (Phy, Phy->PhyAddr, GENERIC_PHY_BMCR,
             GENERIC_PHY_BMCR_RESET);
  if (EFI_ERROR (Status)) {
    return Status;
  }
 
  // Wait up to 500ms for it to complete
  for (Retry = PHY_RESET_TIMEOUT; Retry > 0; Retry--) {
    Status = GenericPhyRead (Phy, Phy->PhyAddr, GENERIC_PHY_BMCR, &Data);
    if (EFI_ERROR (Status)) {
      return Status;
    }
    if ((Data & GENERIC_PHY_BMCR_RESET) == 0) {
      break;
    }
    gBS->Stall (1000);
  }
  if (Retry == 0) {
    return EFI_TIMEOUT;
  }
 
  if (Phy->ResetAction != NULL) {
    Phy->ResetAction (Phy->PrivateData);
  }
 
  return EFI_SUCCESS;
}
 
/**
  Probe link status.
 
  @param  Phy[in]  Pointer to GENERIC_PHY_PRIVATE_DATA.
 
  @retval EFI_SUCCESS       Link is up and auto-negotiation is complete.
  @retval EFI_DEVICE_ERROR  PHY register read/write error,
 
**/
STATIC
EFI_STATUS
GenericPhyGetLinkStatus (
  IN GENERIC_PHY_PRIVATE_DATA   *Phy
  )
{
  EFI_STATUS  Status;
  UINT16      Bmsr;
 
  Status = GenericPhyRead (Phy, Phy->PhyAddr, GENERIC_PHY_BMSR, &Bmsr);
  if (EFI_ERROR (Status)) {
    return Status;
  }
 
  if ((Bmsr & GENERIC_PHY_BMSR_LINK_STATUS) == 0) {
   return EFI_TIMEOUT;
  }
 
  if ((Bmsr & GENERIC_PHY_BMSR_ANEG_COMPLETE) == 0) {
    return EFI_TIMEOUT;
  }
 
  return EFI_SUCCESS;
}
 
/**
  Return PHY link configuration.
 
  @param  Phy[in]      Pointer to GENERIC_PHY_PRIVATE_DATA.
  @param  Speed[out]   Pointer to store link speed.
  @param  Duplex[out]  Pointer to store link duplex setting.
 
  @retval EFI_SUCCESS       Link configuration settings read.
  @retval EFI_DEVICE_ERROR  PHY register read/write error,
 
**/
STATIC
EFI_STATUS
GenericPhyGetConfig (
  IN  GENERIC_PHY_PRIVATE_DATA  *Phy,
  OUT GENERIC_PHY_SPEED         *Speed,
  OUT GENERIC_PHY_DUPLEX        *Duplex
  )
{
  EFI_STATUS  Status;
  UINT16      Gbcr, Gbsr, Anlpar, Anar;
  UINT16      Gb, An;
 
  Status = GenericPhyRead (Phy, Phy->PhyAddr, GENERIC_PHY_GBCR, &Gbcr);
  if (EFI_ERROR (Status)) {
    return Status;
  }
  Status = GenericPhyRead (Phy, Phy->PhyAddr, GENERIC_PHY_GBSR, &Gbsr);
  if (EFI_ERROR (Status)) {
    return Status;
  }
  Status = GenericPhyRead (Phy, Phy->PhyAddr, GENERIC_PHY_ANLPAR, &Anlpar);
  if (EFI_ERROR (Status)) {
    return Status;
  }
  Status = GenericPhyRead (Phy, Phy->PhyAddr, GENERIC_PHY_ANAR, &Anar);
  if (EFI_ERROR (Status)) {
    return Status;
  }
 
  Gb = (Gbsr >> 2) & Gbcr;
  An = Anlpar & Anar;
 
  if ((Gb & (GENERIC_PHY_GBCR_1000BASET_FDX |
             GENERIC_PHY_GBCR_1000BASET)) != 0) {
    *Speed = PHY_SPEED_1000;
    *Duplex = (Gb & GENERIC_PHY_GBCR_1000BASET_FDX) ? PHY_DUPLEX_FULL
                                                    : PHY_DUPLEX_HALF;
  } else if ((An & (GENERIC_PHY_ANAR_100BASETX_FDX |
                    GENERIC_PHY_ANAR_100BASETX)) != 0) {
    *Speed = PHY_SPEED_100;
    *Duplex = (An & GENERIC_PHY_ANAR_100BASETX_FDX) ? PHY_DUPLEX_FULL
                                                    : PHY_DUPLEX_HALF;
  } else {
    *Speed = PHY_SPEED_10;
    *Duplex = (An & GENERIC_PHY_ANAR_10BASET_FDX) ? PHY_DUPLEX_FULL
                                                  : PHY_DUPLEX_HALF;
  }
 
  DEBUG ((DEBUG_INFO, "%a: Link speed %d Mbps, %a-duplex\n",
    __FUNCTION__, *Speed, *Duplex == PHY_DUPLEX_FULL ? "full" : "half"));
 
  return EFI_SUCCESS;
}
 
/**
  Update link status, propagating PHY link state into the MAC layer.
 
  @param  Phy[in]  Pointer to GENERIC_PHY_PRIVATE_DATA.
 
  @retval EFI_SUCCESS       Link is up.
  @retval EFI_DEVICE_ERROR  PHY register read/write error.
  @retval EFI_NOT_READY     Link is down.
 
**/
EFI_STATUS
EFIAPI
GenericPhyUpdateConfig (
  IN GENERIC_PHY_PRIVATE_DATA *Phy
  )
{
  EFI_STATUS          Status;
  GENERIC_PHY_SPEED   Speed;
  GENERIC_PHY_DUPLEX  Duplex;
  BOOLEAN             LinkUp;
 
  Status = GenericPhyGetLinkStatus (Phy);
  LinkUp = EFI_ERROR (Status) ? FALSE : TRUE;
 
  if (Phy->LinkUp != LinkUp) {
    if (LinkUp) {
      DEBUG ((DEBUG_VERBOSE, "%a: Link is up\n", __FUNCTION__));
 
      Status = GenericPhyGetConfig (Phy, &Speed, &Duplex);
      if (EFI_ERROR (Status)) {
        return Status;
      }
 
      GenericPhyConfigure (Phy, Speed, Duplex);
    } else {
      DEBUG ((DEBUG_VERBOSE, "%a: Link is down\n", __FUNCTION__));
    }
  }
 
  Phy->LinkUp = LinkUp;
 
  return LinkUp ? EFI_SUCCESS : EFI_NOT_READY;
}