1 | unsigned char KS8721_AutoNegotiate(Ks8721 *pKs)
|
2 | {
|
3 | unsigned int retryMax;
|
4 | unsigned int value;
|
5 | unsigned int value2;
|
6 | unsigned int phyAnar;
|
7 | unsigned int phyAnalpar;
|
8 | unsigned int retryCount= 0;
|
9 | unsigned char phyAddress;
|
10 | unsigned char rc = 1;
|
11 | int auto_neg_configure = 1;
|
12 |
|
13 | ASSERT(pKs, "-F- KS8721_AutoNegotiate\n\r");
|
14 | phyAddress = pKs->phyAddress;
|
15 | retryMax = pKs->retryMax;
|
16 |
|
17 | EMAC_EnableMdio();
|
18 |
|
19 | if (!EMAC_ReadPhy(phyAddress, KS8721_PHYID1, &value, retryMax))
|
20 | {
|
21 | TRACE_ERROR("Pb EMAC_ReadPhy Id1\n\r");
|
22 | rc = 0;
|
23 | goto AutoNegotiateExit;
|
24 | }
|
25 |
|
26 | if (!EMAC_ReadPhy(phyAddress, KS8721_PHYID2, &phyAnar, retryMax))
|
27 | {
|
28 | TRACE_ERROR("Pb EMAC_ReadPhy Id2\n\r");
|
29 | rc = 0;
|
30 | goto AutoNegotiateExit;
|
31 | }
|
32 | value2 = ((phyAnar>>10) & KS8721_LSB_MASK);
|
33 |
|
34 | if( ( value == KS8721_OUI_MSB ) && ( value2 == KS8721_OUI_LSB))
|
35 | {
|
36 | // TRACE_DEBUG("Vendor Number Model = 0x%X\n\r", ((phyAnar>>4)&0x3F));
|
37 | // TRACE_DEBUG("Model Revision Number = 0x%X\n\r", (phyAnar&0x7));
|
38 | }
|
39 | else
|
40 | {
|
41 | TRACE_ERROR("Problem OUI value -> no Micrel PHY found\n\r");
|
42 | }
|
43 |
|
44 | // Setup control register
|
45 | rc = EMAC_ReadPhy(phyAddress, KS8721_BMCR, &value, retryMax);
|
46 | if (rc == 0)
|
47 | {
|
48 | goto AutoNegotiateExit;
|
49 | }
|
50 |
|
51 | value &= ~KS8721_AUTONEG; // Remove autonegotiation enable
|
52 | value &= ~(KS8721_LOOPBACK|KS8721_POWER_DOWN);
|
53 | value |= KS8721_ISOLATE; // Electrically isolate PHY
|
54 | rc = EMAC_WritePhy(phyAddress, KS8721_BMCR, value, retryMax);
|
55 | if (rc == 0)
|
56 | {
|
57 | goto AutoNegotiateExit;
|
58 | }
|
59 |
|
60 | // Set the Auto_negotiation Advertisement Register
|
61 | // MII advertising for Next page
|
62 | // 100BaseTxFD and HD, 10BaseTFD and HD, IEEE 802.3
|
63 | phyAnar = KS8721_NP | KS8721_TX_FDX | KS8721_TX_HDX |
|
64 | KS8721_10_FDX | KS8721_10_HDX | KS8721_AN_IEEE_802_3;
|
65 | rc = EMAC_WritePhy(phyAddress, KS8721_ANAR, phyAnar, retryMax);
|
66 | if (rc == 0)
|
67 | {
|
68 | goto AutoNegotiateExit;
|
69 | }
|
70 |
|
71 | // Read & modify control register
|
72 | rc = EMAC_ReadPhy(phyAddress, KS8721_BMCR, &value, retryMax);
|
73 | if (rc == 0)
|
74 | {
|
75 | goto AutoNegotiateExit;
|
76 | }
|
77 |
|
78 | value |= KS8721_SPEED_SELECT | KS8721_AUTONEG | KS8721_DUPLEX_MODE;
|
79 | rc = EMAC_WritePhy(phyAddress, KS8721_BMCR, value, retryMax); //Set Autonegotiate enable; CONNECTION ABORTED
|
80 | if (rc == 0)
|
81 | {
|
82 | goto AutoNegotiateExit;
|
83 | }
|
84 |
|
85 | // Restart Auto_negotiation
|
86 | value |= KS8721_RESTART_AUTONEG; // Restart Auto negotiate
|
87 | value &= ~KS8721_ISOLATE;
|
88 | rc = EMAC_WritePhy(phyAddress, KS8721_BMCR, value, retryMax);
|
89 | if (rc == 0)
|
90 | {
|
91 | goto AutoNegotiateExit;
|
92 | }
|
93 | // Check AutoNegotiate complete
|
94 | while (1)
|
95 | {
|
96 | rc = EMAC_ReadPhy(phyAddress, KS8721_BMSR, &value, retryMax);
|
97 | if (rc == 0)
|
98 | {
|
99 | TRACE_ERROR("rc==0\n\r");
|
100 | goto AutoNegotiateExit;
|
101 | }
|
102 | //TRACE_DEBUG(" _BMSR: 0x%X\n\r", value);
|
103 | // Done successfully
|
104 | if (value & KS8721_AUTONEG_COMP)
|
105 | {
|
106 | TRACE_INFO("AutoNegotiate complete\n\r");
|
107 | // rc = EMAC_WritePhy(phyAddress, DM9161_BMCR, value, retryMax);
|
108 | // TRACE_DEBUG(" _BMCR: 0x%X\n\r", value);
|
109 | break;
|
110 | }
|
111 |
|
112 | // Timeout check
|
113 | if (retryMax)
|
114 | {
|
115 | if (++ retryCount >= retryMax)
|
116 | {
|
117 | KS8721_DumpRegisters(pKs);
|
118 | TRACE_INFO("no AutoNeg link partner found -> set 100Base HalfDuplex\n\r");
|
119 |
|
120 | // TRACE_FATAL("TimeOut\n\r");
|
121 | auto_neg_configure = 0;
|
122 | break;
|
123 | }
|
124 | }
|
125 | }
|