summaryrefslogblamecommitdiffstats
path: root/private/ntos/ndis/dc21x4/mactophy.c
blob: e8ac6cde0432158cb7482f7dbf9838208638f992 (plain) (tree)
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701




























































































































































































































































































































































































































































































































































































































































































































                                                                                                        
/*+
 * file:        mactophy.c
 *
 * Copyright (C) 1992-1995 by
 * Digital Equipment Corporation, Maynard, Massachusetts.
 * All rights reserved.
 *
 * This software is furnished under a license and may be used and copied
 * only  in  accordance  of  the  terms  of  such  license  and with the
 * inclusion of the above copyright notice. This software or  any  other
 * copies thereof may not be provided or otherwise made available to any
 * other person.  No title to and  ownership of the  software is  hereby
 * transferred.
 *
 * The information in this software is  subject to change without notice
 * and  should  not  be  construed  as a commitment by digital equipment
 * corporation.
 *
 * Digital assumes no responsibility for the use  or  reliability of its
 * software on equipment which is not supplied by digital.
 *
 *
 * Abstract:    This file contains the code to access the MII PHY device
 *              (initialization, set the connections, and sensing of the
 *              link.
 *
 * Author:      Claudio Hazan
 *
 * Revision History:
 *
 *  15-Oct-95   ch   Creation
 *  20-Dec-95   phk  Cleanup, add support for DC21142
 *
-*/

#include <precomp.h>

#define LINK_PASS_MAX_LOOP 350
#define LINK_PASS_DELAY    (10*MILLISECOND)

#if MII_DBG
PUCHAR MiiMediumString[] = {
   "","","","","","","","","",
   "Mii10BaseT",    
   "Mii10BaseT_FD",
   "Mii10Base2",    
   "Mii10Base5",          
   "Mii100BaseTx",        
   "Mii100BaseTx_FD",      
   "Mii100BaseT4",         
   "Mii100BaseFx",         
   "Mii100BaseFx_FD"      
};
#endif

/*+
 *   DC21X4PhyInit
 *
 * Routine Description:
 *
 *   This routine initializes each PHY present in the SROM; Reset each Phy
 *   if there is a Reset sequence specified there, Initializes the Phys and
 *   updates the Capabilities tables from the values contained in the SROM
 *   with the values supported by the Phys.
 *
 * Arguments:
 *
 *    Adapter     - Pointer to the Data Structure
 *
 * Return Value:
 *
 *    True if any PHY was succesfully initialized.
 *
-*/
extern
BOOLEAN
DC21X4PhyInit(
    IN PDC21X4_ADAPTER Adapter
    )
{
   BOOLEAN  PhyPresent;
   BOOLEAN  ConnectionPresent=FALSE;
   USHORT   Capabilities;
   INT      PhyNumber;
   INT      Seq;

   ULONG Data;

#if MII_DBG
   DbgPrint("DC21X4PhyInit\n");
#endif
   // Reset all the phys using its reset sequence.

   if (!Adapter->PhyMediumInSrom) {
#if __DBG
      DbgPrint("No PHY Media in SROM\n");
#endif
      return FALSE;
   }

#if __DBG
   DbgPrint("PHY Reset\n");
#endif

   Adapter->Force10 = FALSE;

   for (PhyNumber=0; PhyNumber < MAX_PHY_TABLE; PhyNumber++) {

      if (Adapter->Phy[Adapter->PhyNumber].ResetSequenceLength != 0) {

           DC21X4WriteGepRegister(
               Adapter,
               (ULONG)Adapter->Phy[Adapter->PhyNumber].GeneralPurposeCtrl
               );

           for (Seq=0; Seq < Adapter->Phy[Adapter->PhyNumber].ResetSequenceLength; Seq++) {
#if MII_DBG
               DbgPrint("Sequence[%d]=%04x\n", Seq, Adapter->Phy[PhyNumber].ResetSequence[Seq]);
#endif
               DELAY (5);        // 5 microseconds
    
               DC21X4WriteGepRegister(
                   Adapter,
                   (ULONG)Adapter->Phy[Adapter->PhyNumber].ResetSequence[Seq]
                   );
           }
      }
  }

   Adapter->PhyNumber = 0;
   PhyPresent = MiiGenInit(Adapter);

   if (PhyPresent) {
#if __DBG
      DbgPrint("Phy: Initialization OK\n");
#endif

      Capabilities = MiiGenGetCapabilities(Adapter);

      // Convert the MediaType to the MiiMediaType values

      Adapter->MiiMediaType = 
           ConvertMediaTypeToMiiType[Adapter->MediaType & MEDIA_MASK] 
         | (Adapter->MediaType & CONTROL_MASK);

#if MII_DBG
      DbgPrint("Translate MediaType %x to MiiMediaType %x\n", 
            Adapter->MediaType,Adapter->MiiMediaType);
#endif

      Adapter->PhyNwayCapable = (  ((Adapter->MiiMediaType & MEDIA_NWAY) != 0)
                                && ((Capabilities & MiiPhyNwayCapable) != 0)
                                );

      // Merge the PHY Capabilities with the medium stored in SROM 

      for (PhyNumber=0; PhyNumber < MAX_PHY_TABLE; PhyNumber++) {

          Adapter->Phy[PhyNumber].MediaCapabilities &= Capabilities;


          Adapter->Phy[PhyNumber].MediaCapabilities |= (Capabilities & MiiPhyNwayCapable);
#if MII_DBG
          DbgPrint("Merged Capabilities (SROM&PHY)= %04x\n", Adapter->Phy[PhyNumber].MediaCapabilities);
#endif
          Adapter->MiiGen.Phys[PhyNumber]->PhyCapabilities = Adapter->Phy[PhyNumber].MediaCapabilities;
      }
   }
   if (PhyPresent) {
#if MII_DBG
      DbgPrint("Check if Connection is supported\n");
#endif
      // Now check the Connection.
      ConnectionPresent = MiiGenCheckConnection(
                                  Adapter,
                                  (USHORT)Adapter->MiiMediaType
                                  );

      if (!ConnectionPresent) {
          //Force the Phy to 10
          DC21X4SetPhyControl(
               Adapter,
               MiiGenAdminForce10
               );      
      } 
   }

   return (PhyPresent && ConnectionPresent);
}


/*+
 *   DC21X4SetPhyConnection
 *
 * Routine Description:
 *
 *   This function first resets the MAC (which could reset the PHY) and
 *   initialize CSR6 and CSR12 registers. Then sets the PHY to
 *   the required medium.
 *
 * Arguments:
 *
 *    Adapter     - Pointer to the Data Structure
 *
 * Return Value:
 *
 *    True if the requested Medium is supported by the Phy.
 *
-*/
extern
BOOLEAN
DC21X4SetPhyConnection(
    IN PDC21X4_ADAPTER Adapter
    )
{
   BOOLEAN  MediaSupported;

   // First set Mac Connection.
#if MII_DBG
   DbgPrint("DC21X4SetPhyConnection\n");
#endif
   SetMacConnection(Adapter);

   // Now set the PHY Connection.
   MediaSupported = MiiGenSetConnection(
                       Adapter,
                       Adapter->MiiMediaType,
                       Adapter->Phy[Adapter->PhyNumber].NwayAdvertisement
                       );
   return  MediaSupported;

}



/*+
 *   SetMacConnection
 *
 * Routine Description:
 *
 *   This function initialize the CSR6 and CSR12 registers according to the
 *   values present in the SROM for the Selected Medium.
 *
 * Arguments:
 *
 *    Adapter     - Pointer to the Data Structure
 *
 * Return Value:
 *
 *
-*/

void
SetMacConnection(
    IN PDC21X4_ADAPTER Adapter
    )
{

   USHORT MediaPositionMask;
   ULONG  OperationMode;

#if MII_DBG
   DbgPrint("SetMacConnection\n");
#endif
   // First of all find the media type.

   MediaPositionMask = MediaBitTable[(Adapter->MiiMediaType & MEDIA_MASK)];
#if MII_DBG
   DbgPrint(" MiiMediaType %04x   Bitmap %04x\n", 
         Adapter->MiiMediaType,MediaPositionMask);
#endif

   // If unknown media, just return.
   if (MediaPositionMask == 0) {
#if __DBG
      DbgPrint("UNKNOWN Phy Medium %x!!!\n",Adapter->MiiMediaType);
#endif
      return;
   }

   // select Port_Select MII  (disable HEARTBEAT in MII mode).
   OperationMode =  Adapter->OperationMode | (DC21X4_PORT_SELECT | DC21X4_HEARTBEAT_DISABLE);

   if (Adapter->Phy[Adapter->PhyNumber].MediaCapabilities & MediaPositionMask) {

      if (Adapter->Phy[Adapter->PhyNumber].FullDuplexBits & MediaPositionMask) {
         // Set FullDuplex bit
         OperationMode |= DC21X4_FULL_DUPLEX_MODE;
      }
      else {
         //Reset FullDuplex bit
         OperationMode &= ~DC21X4_FULL_DUPLEX_MODE;
      }
      if (Adapter->Phy[Adapter->PhyNumber].TxThresholdModeBits & MediaPositionMask) {
         // Set TTM bit
         OperationMode &= ~(Adapter->Threshold100Mbps);
         OperationMode |=  (DC21X4_TXM_THRESHOLD_MODE | Adapter->Threshold10Mbps);
      }
      else {
         // Reset TTM bit
         OperationMode &= ~(DC21X4_TXM_THRESHOLD_MODE | Adapter->Threshold10Mbps);
         OperationMode |= Adapter->Threshold100Mbps;
      }

#if __DBG
      DbgPrint("SetMacConnection:%s FullDuplex & %s TTM bits in CSR6\n",
             OperationMode & DC21X4_FULL_DUPLEX_MODE ? "Set" : "Reset",
             OperationMode & DC21X4_TXM_THRESHOLD_MODE ? "Set" : "Reset");
#endif

   }

   // Switch Medium: 

   DC21X4IndicateMediaStatus(
        Adapter,
        LinkFail
        );

   // if TTM or FDX setting has changed, stop the TXM and RCV process before modifying
   // these modes 

   if ((OperationMode & DC21X4_MODE_MASK) != (Adapter->OperationMode & DC21X4_MODE_MASK) ) { 
#if MII_DBG
       DbgPrint("FDX or TTM setting has changed: stop RCV and TXM\n");
#endif
       DC21X4StopReceiverAndTransmitter(Adapter);
   }

   switch (Adapter->DeviceId) {

      case DC21142_CFID:

          //Initialize the Sia Registers for MII operation
          // Reset Sia (CSR13)
          // Turn off the BNC transceiver (CSR15)
          // Disable Autonegotiation (CSR14)

          DC21X4_WRITE_PORT(                                                          
              DC21X4_SIA_MODE_0,
              DC21X4_RESET_SIA
              );

          Adapter->Gep_Sia2 = 
              (DC21142_SIA2_10BT & DC21142_SIA2_MASK) 
            | (Adapter->Gep_Sia2 & DC21142_GEP_MASK);

          DC21X4_WRITE_PORT(
              DC21X4_SIA_MODE_2,
              Adapter->Gep_Sia2
              );

          DC21X4_WRITE_PORT(
              DC21X4_SIA_MODE_1,
              0
              );

          break;
   }


   // Init the Command Register.

   Adapter->OperationMode = OperationMode;

#if __DBG
   DbgPrint("SetMacConnection: Write CSR6=%08x\n", OperationMode);
#endif
   DC21X4_WRITE_PORT(
            DC21X4_OPERATION_MODE,
            Adapter->OperationMode
            );
   DELAY(5);

   DC21X4InitializeGepRegisters(Adapter,TRUE);

}

/*+
 *   DC21X4MiiAutoDetect
 *
 * Routine Description:
 *
 *   This function contains the power up autosensing for PHYs.
 *
 * Arguments:
 *
 *    Adapter     - Pointer to the Data Structure
 *
 * Return Value:
 *
 *    Link status
 *
-*/
extern
BOOLEAN
DC21X4MiiAutoDetect(
    IN PDC21X4_ADAPTER Adapter
    )
{
   ULONG OperationMode;
   INT Loop;
   USHORT ConnectionStatus;
   USHORT TmpMedia;
   BOOLEAN Link;
#if MII_DBG
   ULONG Status;
#endif

   Link = MiiGenGetConnectionStatus(
                          Adapter,
                          &ConnectionStatus
                          );
   DC21X4IndicateMediaStatus(
        Adapter,
        Link ? MiiLinkPass : LinkFail
        );

   // If no link available while still negotiating, continue to
   // check the link up to 3s.

   if (!Link) {
#if MII_DBG
      Loop = 30;
#else
      Loop = LINK_PASS_MAX_LOOP;
#endif
      while (!Link && Loop--) {
#if MII_DBG
         DbgPrint("LinkPass=FALSE: wait up to 3 seconds %d\n", Loop);
#endif
         DELAY(LINK_PASS_DELAY);
         Link = MiiGenGetConnectionStatus(
                              Adapter,
                              &ConnectionStatus
                              );
      }
      DC21X4IndicateMediaStatus(
           Adapter,
           Link ? MiiLinkPass : LinkFail
           );
   }
   
#if MII_DBG
   DC21X4_READ_PORT(
      DC21X4_OPERATION_MODE,
      &Status
      );
   DbgPrint("CSR6= %08x, OperationMode=%08x\n", Status, Adapter->OperationMode);
#endif

   // In AutoSense mode, we must know which media has been chosen to set the MAC.
   // otherwise the MAC has been already set.

   if (Adapter->MiiMediaType & MEDIA_AUTOSENSE){
#if MII_DBG
      DbgPrint("AutoSense to check which Medium was chosen\n");
#endif
      Link = MiiGenGetConnection(
                            Adapter,
                            &TmpMedia
                            );
      DC21X4IndicateMediaStatus(
           Adapter,
           Link ? MiiLinkPass : LinkFail
           );

      if (Link){
#if MII_DBG
         DbgPrint("LinkPass= TRUE; Medium = %s\n",
              MiiMediumString[TmpMedia&MEDIA_MASK]);
#endif
         Adapter->MiiMediaType = (TmpMedia | MEDIA_AUTOSENSE);
         SetMacConnection(Adapter);
      }
   }

   if (  !Link 
      && (Adapter->MediaCapable)
      && (Adapter->MiiMediaType & MEDIA_AUTOSENSE)
      ) {

      //Switch back to the non_MII port

      SelectNonMiiPort(Adapter);

   }

   return Link;

}


/*+
 *   DC21X4DynamicMiiAutoSense
 *
 * Routine Description:
 *
 *   This function performs the PHY Dynamic Autosense
 *
 * Arguments:
 *
 *    Adapter     - Pointer to the Data Structure
 *
 * Return Value:
 *
 *    Link status
 *
-*/
extern
BOOLEAN
DC21X4MiiAutoSense(
   IN PDC21X4_ADAPTER Adapter
  )

{
   ULONG OperationMode;
   USHORT CurrentMedia;
   USHORT ConnectionStatus;
   BOOLEAN Link;
   BOOLEAN SwitchPort=FALSE;

#if MII_DBG
   DbgPrint("DC21X4MiiAutoSense\n");
#endif

   Link = MiiGenGetConnectionStatus(
                        Adapter,
                        &ConnectionStatus
                        );

#if MII_DBG
   DbgPrint("MiiMediaType = %04x, ConnectionStatus = %04x\n",
             Adapter->MiiMediaType, ConnectionStatus);
#endif

   if (Adapter->MiiMediaType & MEDIA_AUTOSENSE) {

      if (Link &&
          ((ConnectionStatus & MEDIA_STATUS_MASK) == MEDIA_LINK_PASS_WITH_PF)){

         // There was a link failure 
         // Check the current medium

         Link = MiiGenGetConnection(
                    Adapter,
                    &CurrentMedia
                    );

         if (  Link 
            && (  (Adapter->MiiMediaType != CurrentMedia) 
               || (Adapter->LinkStatus != MiiLinkPass)
               )
            ){

            //The link is up but the medium has changed
#if MII_DBG
            DbgPrint("Mii Medium has changed: New Mii Medium = %s \n", 
                MiiMediumString[CurrentMedia&MEDIA_MASK]);
#endif
            Adapter->MiiMediaType = (CurrentMedia | MEDIA_AUTOSENSE);
            SetMacConnection(Adapter);
         }
      }
   }

   // do not indicate the Mii Link down transition 
   // if the non_Mii link is up.
   // In any other case indicate the transition

   if (Link || (Adapter->LinkStatus != LinkPass)) {

      DC21X4IndicateMediaStatus(
          Adapter,
          Link ? MiiLinkPass : LinkFail
          );
   }

   if (  !Link 
      && (Adapter->MediaCapable) 
      && (Adapter->MiiMediaType & MEDIA_AUTOSENSE)
      ) {

      if ((ConnectionStatus & MEDIA_STATUS_MASK) == MEDIA_READ_REGISTER_FAILED){

          //PHY is not connected anymore

          Adapter->PhyPresent=FALSE;

          if (Adapter->MediaType & MEDIA_NWAY) {

                 //Enable Nway Negotiation
                 DC21X4EnableNway (Adapter);   

                 SwitchPort=TRUE;
          }
      }
      else if (Adapter->OperationMode & DC21X4_PORT_SELECT){

         // Mii link is down,current port is Mii,
         // at least one non Mii port is populated:
         //Switch Port_Select to enable the non MII ports
         SwitchPort = TRUE;
      }

      if (SwitchPort) {

         SelectNonMiiPort(Adapter);

      }

   }

   return Link;

}

/*+
 * SelectNonMiiPort
 *
 * Routine Description:
 *
 *   This function selects the DC21X4 non_MII port
 *
 * Arguments:
 *
 *    Adapter     - Pointer to the Data Structure
 *
 * Return Value:
 *
 *    None
 *
-*/
extern
VOID
SelectNonMiiPort(
   IN PDC21X4_ADAPTER Adapter
  )

{

  ULONG OperationMode;

  OperationMode = Adapter->OperationMode & ~(DC21X4_MEDIUM_MASK);
  OperationMode |= Adapter->Media[Adapter->SelectedMedium].Mode;

  if ((OperationMode & DC21X4_MODE_MASK) != (Adapter->OperationMode & DC21X4_MODE_MASK)) { 
      DC21X4StopReceiverAndTransmitter(Adapter);
  }

  Adapter->OperationMode = OperationMode;

  DC21X4_WRITE_PORT(
      DC21X4_OPERATION_MODE,
      Adapter->OperationMode
      );

  DC21X4InitializeMediaRegisters(Adapter,FALSE);

  switch (Adapter->SelectedMedium) {
       
      case Medium10Base2:
      case Medium10Base5:

          DC21X4IndicateMediaStatus(Adapter,LinkPass);
          break; 
  }

}


/*+
 *   DC21X4SetPhyControl
 *
 * Routine Description:
 *
 *   This function modifies the PHY Control register.
 *
 * Arguments:
 *
 *    Adapter     - Pointer to the Data Structure
 *    Control     - The Control to perform
 *
 * Return Value:
 *
 *    none
-*/
extern
VOID
DC21X4SetPhyControl(
   PDC21X4_ADAPTER Adapter,
   USHORT  AdminControl
    )
{

   MiiGenAdminControl(Adapter, AdminControl);
   return;

}