/
3C90xMIIPHY.cpp
570 lines (437 loc) · 15.8 KB
/
3C90xMIIPHY.cpp
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
* Copyright (c) 1998-2004 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.1 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include "3C90x.h"
/**************************************************************************
*
* Low-level management interface methods, specific to the 3C90x.
*
**************************************************************************/
//---------------------------------------------------------------------------
// physicalMgmtWriteWord
//
// Write 'bits' number of bits to the MII management interface
// starting with the most significant bit in 'word'.
void
Apple3Com3C90x::physicalMgmtWriteWord( UInt32 word, UInt8 bits )
{
UInt16 reg;
for ( int shifts = 31; (shifts >= 0) && bits; shifts--, bits-- )
{
reg = kPhysicalMgmtDirWrite
| ( (word & (1 << shifts)) ? kPhysicalMgmtDataMask : 0 );
setPhysicalMgmt( reg );
IODelay( kMIIDelay );
reg |= kPhysicalMgmtClkMask;
setPhysicalMgmt( reg );
IODelay( kMIIDelay );
reg &= ~kPhysicalMgmtClkMask;
setPhysicalMgmt( reg );
IODelay( kMIIDelay );
}
}
//---------------------------------------------------------------------------
// physicalMgmtReadBit
//
// Read one bit from the MII management interface. Also used to
// tri-state the STA's MDIO pin.
UInt8 Apple3Com3C90x::physicalMgmtReadBit()
{
UInt16 reg;
reg = kPhysicalMgmtDirRead;
setPhysicalMgmt( reg );
IODelay( kMIIDelay );
reg = kPhysicalMgmtDirRead | kPhysicalMgmtClkMask;
setPhysicalMgmt( reg );
IODelay( kMIIDelay );
reg = kPhysicalMgmtDirRead;
setPhysicalMgmt( reg );
IODelay( kMIIDelay );
reg = getPhysicalMgmt();
return ( reg & kPhysicalMgmtDataMask ) ? 1 : 0;
}
//---------------------------------------------------------------------------
// miiReadWord
bool
Apple3Com3C90x::miiReadWord( PHYAddress phyAddr,
PHYRegAddr phyReg,
PHYWord * phyData )
{
UInt32 miiFrame;
PHYWord value = 0;
bool ret = true;
// Write preamble.
physicalMgmtWriteWord( kMIIFramePreamble, kMIIFrameSize );
// Prepare command frame.
miiFrame = kMIIFrameStart
| kMIIFrameOpcodeRead
| SetBitField( MIIFrame, RegAddr, phyReg )
| SetBitField( MIIFrame, PhyAddr, phyAddr );
// Write ST, OP, PHYAD, REGAD in the MII command frame.
physicalMgmtWriteWord( miiFrame, 14 );
// Hi-Z state
// Make sure the PHY generated a zero bit after the 2nd Hi-Z bit.
if ( physicalMgmtReadBit() ) ret = false;
// read 16-bit data.
for ( int i = 0; i < 16; i++ )
{
value = physicalMgmtReadBit() | (value << 1);
}
if ( phyData )
*phyData = value;
physicalMgmtReadBit(); // Hi-Z state
return ret;
}
//---------------------------------------------------------------------------
// miiWriteWord
void
Apple3Com3C90x::miiWriteWord( PHYAddress phyAddr,
PHYRegAddr phyReg,
PHYWord phyData )
{
UInt32 miiFrame;
// Write preamble.
physicalMgmtWriteWord( kMIIFramePreamble, kMIIFrameSize );
// Prepare command frame.
miiFrame = kMIIFrameStart
| kMIIFrameOpcodeWrite
| kMIIFrameTAWrite
| SetBitField( MIIFrame, RegAddr, phyReg )
| SetBitField( MIIFrame, PhyAddr, phyAddr )
| SetBitField( MIIFrame, Data, phyData );
// Write command frame.
physicalMgmtWriteWord( miiFrame, kMIIFrameSize );
// Hi-Z state
physicalMgmtReadBit();
}
/**************************************************************************
*
* Generic MII/PHY management interface methods. Hardware independent but
* rely on the low-level MII methods.
*
**************************************************************************/
//---------------------------------------------------------------------------
// phyProbe
//
// Return true if a PHY is detected at the given PHY address.
bool Apple3Com3C90x::phyProbe( PHYAddress phy )
{
if ( phy > kPHYAddrMax ) return false;
return( miiReadWord( phy, kMIIRegisterStatus ) );
}
//---------------------------------------------------------------------------
// phyReset
//
// Reset the PHY device. Returns true for a successful reset cycle.
bool Apple3Com3C90x::phyReset( PHYAddress phy )
{
bool success = false;
PHYWord control;
LOG_PHY("%s::%s\n", getName(), __FUNCTION__);
if ( miiReadWord( phy, kMIIRegisterControl, &control ) == false )
return false;
// Set the reset bit in the PHY Control register
miiWriteWord( phy, kMIIRegisterControl,
(control | kMIIControlResetMask) );
// Wait till reset is complete, kMIIControlReset bit
// will auto clear to zero.
for ( SInt32 timeout = kPHYResetTimeout;
timeout > 0;
timeout -= kPHYResetDelay )
{
if ( miiReadWord( phy, kMIIRegisterControl, &control ) == false )
break;
if ( (control & kMIIControlResetMask) == 0 )
{
/*
* Onboard implementation on Dell Optiplex GX1 needs this delay.
* Without it, the first PHY register read after a reset will be
* read as all zeroes.
*/
IOSleep(10);
success = true;
break;
}
IOSleep( kPHYResetDelay );
}
return success;
}
//---------------------------------------------------------------------------
// phyWaitForValidLink
//
// Wait and return true if a valid link was detected,
// false on time out.
bool Apple3Com3C90x::phyWaitForValidLink( PHYAddress phy )
{
PHYWord status;
LOG_PHY("%s::%s\n", getName(), __FUNCTION__);
for ( SInt32 timeout = kPHYLinkTimeout;
timeout > 0;
timeout -= kPHYLinkDelay )
{
if ( miiReadWord( phy, kMIIRegisterStatus, &status ) == false )
return false;
if ( status & kMIIStatusLinkValidMask )
return true;
IOSleep( kPHYLinkDelay );
}
return false;
}
//---------------------------------------------------------------------------
// phyWaitForNegotiation
//
// Wait and return true if Nway auto-negotiation is complete.
bool Apple3Com3C90x::phyWaitForNegotiation( PHYAddress phy )
{
PHYWord status;
LOG_PHY("%s::%s\n", getName(), __FUNCTION__);
for ( SInt32 timeout = kPHYLinkTimeout;
timeout > 0;
timeout -= kPHYLinkDelay )
{
if ( miiReadWord( phy, kMIIRegisterStatus, &status ) == false )
return false;
if ( status & kMIIStatusNegotiationCompleteMask )
return true;
IOSleep( kPHYLinkDelay );
}
return false;
}
//---------------------------------------------------------------------------
// phyGetSupportedLinks
//
// Return a mask of all MII links supported by the PHY.
UInt32 Apple3Com3C90x::phyGetSupportedLinks( PHYAddress phy )
{
PHYWord status;
UInt32 mask = 0;
if ( miiReadWord( phy, kMIIRegisterStatus, &status ) == true )
{
mask = GetBitField( MIIStatus, Links, status );
}
return mask;
}
//---------------------------------------------------------------------------
// phyGetBestNegotiatedLink
//
// Return the best mode negotiated by the station and the remote link partner.
MIILink Apple3Com3C90x::phyGetBestNegotiatedLink( PHYAddress phy,
MIILink phyLink,
MIILink reportedLink,
PHYWord * reportedStatus )
{
MIILink newLink = kMIILinkNone;
PHYWord status;
// Read status register.
miiReadWord( phy, kMIIRegisterStatus, &status );
// Minimize the amount of work if status is unchanged.
if ( reportedStatus )
{
if ( ( ( status ^ *reportedStatus ) &
( kMIIStatusNegotiationCompleteMask |
kMIIStatusLinkValidMask ) ) == 0 )
{
// LOG_PHY("%s: status unchanged: 0x%04x\n", getName(), status);
return reportedLink;
}
*reportedStatus = status; // Update with current status.
}
if ( phyLink & kMIILinkNway )
{
// PHY was configured with auto-negotiation enabled.
if (( status &
( kMIIStatusLinkValidMask | kMIIStatusNegotiationCompleteMask ))
== ( kMIIStatusLinkValidMask | kMIIStatusNegotiationCompleteMask ))
{
PHYWord local, remote;
if ( miiReadWord( phy, kMIIRegisterAdvertisement, &local ) &&
miiReadWord( phy, kMIIRegisterLinkPartner, &remote ) )
{
UInt32 commonLinks;
LOG_PHY("%s: local links: 0x%04x\n", getName(),
GetBitField( MIIAdvertisement, Links, local ));
LOG_PHY("%s: remote links: 0x%04x\n", getName(),
GetBitField( MIILinkPartner, Links, remote ));
commonLinks = GetBitField( MIIAdvertisement, Links, local ) &
GetBitField( MIILinkPartner, Links, remote );
// Ranked according to 802.3u priority.
if ( commonLinks & kMIILink100TX_FD )
newLink = kMIILink100TX_FD;
else if ( commonLinks & kMIILink100T4 )
newLink = kMIILink100T4;
else if ( commonLinks & kMIILink100TX )
newLink = kMIILink100TX;
else if ( commonLinks & kMIILink10BT_FD )
newLink = kMIILink10BT_FD;
else if ( commonLinks & kMIILink10BT )
newLink = kMIILink10BT;
}
}
}
else
{
// Auto-negotiation not enabled.
if ( status & kMIIStatusLinkValidMask )
newLink = phyLink;
}
// Preserve the kMIILinkNway flag.
newLink |= ( phyLink & kMIILinkNway );
return newLink;
}
//---------------------------------------------------------------------------
// phyGetID
//
// Return the 32-bit PHY ID.
UInt32 Apple3Com3C90x::phyGetIdentifier( PHYAddress phy )
{
UInt32 id = kPHYIDInvalid;
UInt16 id_hi;
UInt16 id_lo;
if ( miiReadWord( phy, kMIIRegisterID0, &id_hi ) &&
miiReadWord( phy, kMIIRegisterID1, &id_lo ) )
{
id = ((id_hi << 16) & 0xffff0000) | id_lo;
}
LOG_PHY("%s: PHY ID = %08lx\n", getName(), id);
return id;
}
//---------------------------------------------------------------------------
// phyForceLink
//
// Disable Nway and force the PHY to the given link.
// Returns true on success, false otherwise.
bool Apple3Com3C90x::phyForceMIILink( PHYAddress phy, MIILink link )
{
PHYWord reg;
UInt32 linkMask;
LOG_PHY("%s::%s(%lx)\n", getName(), __FUNCTION__, link);
// Refuse to handle Nway or invalid links.
if ( link & kMIILinkNway )
return false;
if ( ( link & kMIILinkMask ) == kMIILinkNone )
return false;
// Verify that the link is supported by the PHY.
linkMask = phyGetSupportedLinks( phy );
if ( ( link & linkMask ) == 0 )
{
LOG_PHY("%s::%s PHY does not support link %lx\n",
getName(), __FUNCTION__, link);
return false;
}
// Reset the PHY.
if ( phyReset( phy ) == false )
{
LOG_PHY("%s::%s PHY reset error\n", getName(), __FUNCTION__);
return false;
}
// Advertise a single supported link, though Nway is going to
// get turned off very shortly.
if ( miiReadWord( phy, kMIIRegisterAdvertisement, ® ) )
{
reg &= ~( kMIIAdvertisementProtocolMask |
kMIIAdvertisementLinksMask |
kMIIAdvertisementPauseCapableMask );
reg |= SetBitField( MIIAdvertisement, Links, link );
reg |= SetBitField( MIIAdvertisement, Protocol, 0x01 );
if (_flowControlEnabled)
reg |= SetBitField( MIIAdvertisement, PauseCapable, 0x01 );
miiWriteWord( phy, kMIIRegisterAdvertisement, reg );
phyWaitForNegotiation( phy );
}
// Disable Nway auto-negotiation and set the desired link.
if ( miiReadWord( phy, kMIIRegisterControl, ® ) == false )
{
LOG_PHY("%s::%s CONTROL read error\n", getName(), __FUNCTION__);
return false;
}
reg &= ~( kMIIControlSpeedSelectionMask |
kMIIControlFullDuplexMask |
kMIIControlIsolateMask |
kMIIControlAutoNegotiationMask );
if ( link >= kMIILink100TX )
reg |= kMIIControlSpeedSelectionMask; // 100 Mb/s
if ( link & ( kMIILink100TX_FD | kMIILink10BT_FD ) )
reg |= kMIIControlFullDuplexMask; // Full-Duplex
// Update the PHY Control register.
miiWriteWord( phy, kMIIRegisterControl, reg );
LOG_PHY("%s::%s CONTROL set to %04x\n", getName(), __FUNCTION__,
reg);
return true;
}
//---------------------------------------------------------------------------
// phyStartNegotiation
//
// Start Nway auto-negotiation.
bool
Apple3Com3C90x::phyStartNegotiation( PHYAddress phy )
{
PHYWord reg;
UInt32 linkMask;
LOG_PHY("%s::%s\n", getName(), __FUNCTION__);
// Reset PHY.
if ( phyReset( phy ) == false )
{
LOG_PHY("%s::%s PHY reset error\n", getName(), __FUNCTION__);
return false;
}
// Verify that the PHY supports auto-negotiation.
if ( miiReadWord( phy, kMIIRegisterStatus, ® ) == false )
{
LOG_PHY("%s::%s STATUS read error\n", getName(), __FUNCTION__);
return false;
}
if ( ( reg & kMIIStatusNegotiationAbilityMask ) == 0 )
{
LOG_PHY("%s::%s no Nway support\n", getName(), __FUNCTION__);
return false;
}
// Get local PHY capabilities.
linkMask = phyGetSupportedLinks( phy );
if ( linkMask == 0 )
{
LOG_PHY("%s::%s zero link support\n", getName(), __FUNCTION__);
return false;
}
// Make sure the PHY is advertising all the available links.
if ( miiReadWord( phy, kMIIRegisterAdvertisement, ® ) == false )
{
LOG_PHY("%s::%s: ANAR read error\n", getName(), __FUNCTION__);
return false;
}
reg &= ~( kMIIAdvertisementProtocolMask |
kMIIAdvertisementLinksMask |
kMIIAdvertisementPauseCapableMask );
reg |= SetBitField( MIIAdvertisement, Links, linkMask );
reg |= SetBitField( MIIAdvertisement, Protocol, 0x01 );
if (_flowControlEnabled)
reg |= SetBitField( MIIAdvertisement, PauseCapable, 0x01 );
miiWriteWord( phy, kMIIRegisterAdvertisement, reg );
// Set MII CONTROL to auto-negotiate and restart negotiation.
if ( miiReadWord( phy, kMIIRegisterControl, ® ) == false )
{
LOG_PHY("%s::%s CONTROL read error\n", getName(), __FUNCTION__);
return false;
}
reg |= ( kMIIControlAutoNegotiationMask |
kMIIControlRestartNegotiationMask );
miiWriteWord( phy, kMIIRegisterControl, reg );
return true;
}