1 /*
2 * Copyright (c) 2019-2025 Allwinner Technology Co., Ltd. ALL rights reserved.
3 *
4 * Allwinner is a trademark of Allwinner Technology Co.,Ltd., registered in
5 * the the People's Republic of China and other countries.
6 * All Allwinner Technology Co.,Ltd. trademarks are used with permission.
7 *
8 * DISCLAIMER
9 * THIRD PARTY LICENCES MAY BE REQUIRED TO IMPLEMENT THE SOLUTION/PRODUCT.
10 * IF YOU NEED TO INTEGRATE THIRD PARTY’S TECHNOLOGY (SONY, DTS, DOLBY, AVS OR MPEGLA, ETC.)
11 * IN ALLWINNERS’SDK OR PRODUCTS, YOU SHALL BE SOLELY RESPONSIBLE TO OBTAIN
12 * ALL APPROPRIATELY REQUIRED THIRD PARTY LICENCES.
13 * ALLWINNER SHALL HAVE NO WARRANTY, INDEMNITY OR OTHER OBLIGATIONS WITH RESPECT TO MATTERS
14 * COVERED UNDER ANY REQUIRED THIRD PARTY LICENSE.
15 * YOU ARE SOLELY RESPONSIBLE FOR YOUR USAGE OF THIRD PARTY’S TECHNOLOGY.
16 *
17 *
18 * THIS SOFTWARE IS PROVIDED BY ALLWINNER"AS IS" AND TO THE MAXIMUM EXTENT
19 * PERMITTED BY LAW, ALLWINNER EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND,
20 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION REGARDING
21 * THE TITLE, NON-INFRINGEMENT, ACCURACY, CONDITION, COMPLETENESS, PERFORMANCE
22 * OR MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 * IN NO EVENT SHALL ALLWINNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 
33 #ifndef __SUNXI_HAL_MII_H__
34 #define __SUNXI_HAL_MII_H__
35 
36 /* Generic MII registers. */
37 
38 #define MII_BMCR        0x00    /* Basic mode control register */
39 #define MII_BMSR        0x01    /* Basic mode status register  */
40 #define MII_PHYSID1     0x02    /* PHYS ID 1               */
41 #define MII_PHYSID2     0x03    /* PHYS ID 2               */
42 #define MII_ADVERTISE       0x04    /* Advertisement control reg   */
43 #define MII_LPA         0x05    /* Link partner ability reg    */
44 #define MII_EXPANSION       0x06    /* Expansion register          */
45 #define MII_CTRL1000        0x09    /* 1000BASE-T control          */
46 #define MII_STAT1000        0x0a    /* 1000BASE-T status           */
47 #define MII_ESTATUS     0x0f    /* Extended Status */
48 #define MII_DCOUNTER        0x12    /* Disconnect counter          */
49 #define MII_FCSCOUNTER      0x13    /* False carrier counter       */
50 #define MII_NWAYTEST        0x14    /* N-way auto-neg test reg     */
51 #define MII_RERRCOUNTER     0x15    /* Receive error counter       */
52 #define MII_SREVISION       0x16    /* Silicon revision        */
53 #define MII_RESV1       0x17    /* Reserved...             */
54 #define MII_LBRERROR        0x18    /* Lpback, rx, bypass error    */
55 #define MII_PHYADDR     0x19    /* PHY address             */
56 #define MII_RESV2       0x1a    /* Reserved...             */
57 #define MII_TPISTATUS       0x1b    /* TPI status for 10mbps       */
58 #define MII_NCONFIG     0x1c    /* Network interface config    */
59 
60 /* Basic mode control register. */
61 #define BMCR_RESV       0x003f  /* Unused...               */
62 #define BMCR_SPEED1000      0x0040  /* MSB of Speed (1000)         */
63 #define BMCR_CTST       0x0080  /* Collision test          */
64 #define BMCR_FULLDPLX       0x0100  /* Full duplex             */
65 #define BMCR_ANRESTART      0x0200  /* Auto negotiation restart    */
66 #define BMCR_ISOLATE        0x0400  /* Disconnect DP83840 from MII */
67 #define BMCR_PDOWN      0x0800  /* Powerdown the DP83840       */
68 #define BMCR_ANENABLE       0x1000  /* Enable auto negotiation     */
69 #define BMCR_SPEED100       0x2000  /* Select 100Mbps          */
70 #define BMCR_LOOPBACK       0x4000  /* TXD loopback bits           */
71 #define BMCR_RESET      0x8000  /* Reset the DP83840           */
72 
73 /* Basic mode status register. */
74 #define BMSR_ERCAP      0x0001  /* Ext-reg capability          */
75 #define BMSR_JCD        0x0002  /* Jabber detected         */
76 #define BMSR_LSTATUS        0x0004  /* Link status             */
77 #define BMSR_ANEGCAPABLE    0x0008  /* Able to do auto-negotiation */
78 #define BMSR_RFAULT     0x0010  /* Remote fault detected       */
79 #define BMSR_ANEGCOMPLETE   0x0020  /* Auto-negotiation complete   */
80 #define BMSR_RESV       0x00c0  /* Unused...               */
81 #define BMSR_ESTATEN        0x0100  /* Extended Status in R15 */
82 #define BMSR_100HALF2       0x0200  /* Can do 100BASE-T2 HDX */
83 #define BMSR_100FULL2       0x0400  /* Can do 100BASE-T2 FDX */
84 #define BMSR_10HALF     0x0800  /* Can do 10mbps, half-duplex  */
85 #define BMSR_10FULL     0x1000  /* Can do 10mbps, full-duplex  */
86 #define BMSR_100HALF        0x2000  /* Can do 100mbps, half-duplex */
87 #define BMSR_100FULL        0x4000  /* Can do 100mbps, full-duplex */
88 #define BMSR_100BASE4       0x8000  /* Can do 100mbps, 4k packets  */
89 
90 /* Advertisement control register. */
91 #define ADVERTISE_SLCT      0x001f  /* Selector bits           */
92 #define ADVERTISE_CSMA      0x0001  /* Only selector supported     */
93 #define ADVERTISE_10HALF    0x0020  /* Try for 10mbps half-duplex  */
94 #define ADVERTISE_1000XFULL 0x0020  /* Try for 1000BASE-X full-duplex */
95 #define ADVERTISE_10FULL    0x0040  /* Try for 10mbps full-duplex  */
96 #define ADVERTISE_1000XHALF 0x0040  /* Try for 1000BASE-X half-duplex */
97 #define ADVERTISE_100HALF   0x0080  /* Try for 100mbps half-duplex */
98 #define ADVERTISE_1000XPAUSE    0x0080  /* Try for 1000BASE-X pause    */
99 #define ADVERTISE_100FULL   0x0100  /* Try for 100mbps full-duplex */
100 #define ADVERTISE_1000XPSE_ASYM 0x0100  /* Try for 1000BASE-X asym pause */
101 #define ADVERTISE_100BASE4  0x0200  /* Try for 100mbps 4k packets  */
102 #define ADVERTISE_PAUSE_CAP 0x0400  /* Try for pause           */
103 #define ADVERTISE_PAUSE_ASYM    0x0800  /* Try for asymetric pause     */
104 #define ADVERTISE_RESV      0x1000  /* Unused...               */
105 #define ADVERTISE_RFAULT    0x2000  /* Say we can detect faults    */
106 #define ADVERTISE_LPACK     0x4000  /* Ack link partners response  */
107 #define ADVERTISE_NPAGE     0x8000  /* Next page bit           */
108 
109 #define ADVERTISE_FULL (ADVERTISE_100FULL | ADVERTISE_10FULL | \
110             ADVERTISE_CSMA)
111 #define ADVERTISE_ALL (ADVERTISE_10HALF | ADVERTISE_10FULL | \
112                ADVERTISE_100HALF | ADVERTISE_100FULL)
113 
114 /* Link partner ability register. */
115 #define LPA_SLCT        0x001f  /* Same as advertise selector  */
116 #define LPA_10HALF      0x0020  /* Can do 10mbps half-duplex   */
117 #define LPA_1000XFULL       0x0020  /* Can do 1000BASE-X full-duplex */
118 #define LPA_10FULL      0x0040  /* Can do 10mbps full-duplex   */
119 #define LPA_1000XHALF       0x0040  /* Can do 1000BASE-X half-duplex */
120 #define LPA_100HALF     0x0080  /* Can do 100mbps half-duplex  */
121 #define LPA_1000XPAUSE      0x0080  /* Can do 1000BASE-X pause     */
122 #define LPA_100FULL     0x0100  /* Can do 100mbps full-duplex  */
123 #define LPA_1000XPAUSE_ASYM 0x0100  /* Can do 1000BASE-X pause asym*/
124 #define LPA_100BASE4        0x0200  /* Can do 100mbps 4k packets   */
125 #define LPA_PAUSE_CAP       0x0400  /* Can pause               */
126 #define LPA_PAUSE_ASYM      0x0800  /* Can pause asymetrically     */
127 #define LPA_RESV        0x1000  /* Unused...               */
128 #define LPA_RFAULT      0x2000  /* Link partner faulted        */
129 #define LPA_LPACK       0x4000  /* Link partner acked us       */
130 #define LPA_NPAGE       0x8000  /* Next page bit           */
131 
132 #define LPA_DUPLEX      (LPA_10FULL | LPA_100FULL)
133 #define LPA_100         (LPA_100FULL | LPA_100HALF | LPA_100BASE4)
134 
135 /* Expansion register for auto-negotiation. */
136 #define EXPANSION_NWAY      0x0001  /* Can do N-way auto-nego      */
137 #define EXPANSION_LCWP      0x0002  /* Got new RX page code word   */
138 #define EXPANSION_ENABLENPAGE   0x0004  /* This enables npage words    */
139 #define EXPANSION_NPCAPABLE 0x0008  /* Link partner supports npage */
140 #define EXPANSION_MFAULTS   0x0010  /* Multiple faults detected    */
141 #define EXPANSION_RESV      0xffe0  /* Unused...               */
142 
143 #define ESTATUS_1000_XFULL  0x8000  /* Can do 1000BX Full */
144 #define ESTATUS_1000_XHALF  0x4000  /* Can do 1000BX Half */
145 #define ESTATUS_1000_TFULL  0x2000  /* Can do 1000BT Full */
146 #define ESTATUS_1000_THALF  0x1000  /* Can do 1000BT Half */
147 
148 /* N-way test register. */
149 #define NWAYTEST_RESV1      0x00ff  /* Unused...               */
150 #define NWAYTEST_LOOPBACK   0x0100  /* Enable loopback for N-way   */
151 #define NWAYTEST_RESV2      0xfe00  /* Unused...               */
152 
153 /* 1000BASE-T Control register */
154 #define ADVERTISE_1000FULL  0x0200  /* Advertise 1000BASE-T full duplex */
155 #define ADVERTISE_1000HALF  0x0100  /* Advertise 1000BASE-T half duplex */
156 
157 /* 1000BASE-T Status register */
158 #define LPA_1000LOCALRXOK   0x2000  /* Link partner local receiver status */
159 #define LPA_1000REMRXOK     0x1000  /* Link partner remote receiver status */
160 #define LPA_1000FULL        0x0800  /* Link partner 1000BASE-T full duplex */
161 #define LPA_1000HALF        0x0400  /* Link partner 1000BASE-T half duplex */
162 
163 /* Flow control flags */
164 #define FLOW_CTRL_TX        0x01
165 #define FLOW_CTRL_RX        0x02
166 
167 /**
168  * mii_nway_result
169  * @negotiated: value of MII ANAR and'd with ANLPAR
170  *
171  * Given a set of MII abilities, check each bit and returns the
172  * currently supported media, in the priority order defined by
173  * IEEE 802.3u.  We use LPA_xxx constants but note this is not the
174  * value of LPA solely, as described above.
175  *
176  * The one exception to IEEE 802.3u is that 100baseT4 is placed
177  * between 100T-full and 100T-half.  If your phy does not support
178  * 100T4 this is fine. If your phy places 100T4 elsewhere in the
179  * priority order, you will need to roll your own function.
180  */
mii_nway_result(unsigned int negotiated)181 static inline unsigned int mii_nway_result (unsigned int negotiated)
182 {
183     unsigned int ret;
184 
185     if (negotiated & LPA_100FULL)
186         ret = LPA_100FULL;
187     else if (negotiated & LPA_100BASE4)
188         ret = LPA_100BASE4;
189     else if (negotiated & LPA_100HALF)
190         ret = LPA_100HALF;
191     else if (negotiated & LPA_10FULL)
192         ret = LPA_10FULL;
193     else
194         ret = LPA_10HALF;
195 
196     return ret;
197 }
198 
199 /**
200  * mii_duplex
201  * @duplex_lock: Non-zero if duplex is locked at full
202  * @negotiated: value of MII ANAR and'd with ANLPAR
203  *
204  * A small helper function for a common case.  Returns one
205  * if the media is operating or locked at full duplex, and
206  * returns zero otherwise.
207  */
mii_duplex(unsigned int duplex_lock,unsigned int negotiated)208 static inline unsigned int mii_duplex (unsigned int duplex_lock,
209                        unsigned int negotiated)
210 {
211     if (duplex_lock)
212         return 1;
213     if (mii_nway_result(negotiated) & LPA_DUPLEX)
214         return 1;
215     return 0;
216 }
217 
218 #endif /* __SUNXI_HAL_MII_H__ */
219