summaryrefslogtreecommitdiff
path: root/bsp/cvitek/drivers/libraries/eth/mii.h
blob: d75ba1260f584c4aa853c8db14d712324b46af43 (plain)
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
/*
* Copyright (C) Cvitek Co., Ltd. 2019-2020. All rights reserved.
*/

#ifndef __MII_H__
#define __MII_H__

/* Basic mode control register. */
#define CVI_BMCR_RESV           (0x003f)
#define CVI_BMCR_SPEED1000      (0x0040)
#define CVI_BMCR_CTST           (0x0080)
#define CVI_BMCR_FULLDPLX       (0x0100)
#define CVI_BMCR_ANRESTART      (0x0200)
#define CVI_BMCR_ISOLATE        (0x0400)
#define CVI_BMCR_PDOWN          (0x0800)
#define CVI_BMCR_ANENABLE       (0x1000)
#define CVI_BMCR_SPEED100       (0x2000)
#define CVI_BMCR_LOOPBACK       (0x4000)
#define CVI_BMCR_RESET          (0x8000)

/* Basic mode status register. */
#define CVI_BMSR_ERCAP          (0x0001)
#define CVI_BMSR_JCD            (0x0002)
#define CVI_BMSR_LSTATUS        (0x0004)
#define CVI_BMSR_ANEGCAPABLE    (0x0008)
#define CVI_BMSR_RFAULT         (0x0010)
#define CVI_BMSR_ANEGCOMPLETE   (0x0020)
#define CVI_BMSR_RESV           (0x00c0)
#define CVI_BMSR_ESTATEN        (0x0100)
#define CVI_BMSR_100HALF2       (0x0200)
#define CVI_BMSR_100FULL2       (0x0400)
#define CVI_BMSR_10HALF         (0x0800)
#define CVI_BMSR_10FULL         (0x1000)
#define CVI_BMSR_100HALF        (0x2000)
#define CVI_BMSR_100FULL        (0x4000)
#define CVI_BMSR_100BASE4       (0x8000)

/* Advertisement control register. */
#define CVI_ADVERTISE_CSMA            (0x0001)
#define CVI_ADVERTISE_SLCT            (0x001f)
#define CVI_ADVERTISE_10HALF          (0x0020)
#define CVI_ADVERTISE_1000XFULL       (0x0020)
#define CVI_ADVERTISE_10FULL          (0x0040)
#define CVI_ADVERTISE_1000XHALF       (0x0040)
#define CVI_ADVERTISE_100HALF         (0x0080)
#define CVI_ADVERTISE_1000XPAUSE      (0x0080)
#define CVI_ADVERTISE_100FULL         (0x0100)
#define CVI_ADVERTISE_1000XPSE_ASYM   (0x0100)
#define CVI_ADVERTISE_100BASE4        (0x0200)
#define CVI_ADVERTISE_PAUSE_CAP       (0x0400)
#define CVI_ADVERTISE_PAUSE_ASYM      (0x0800)
#define CVI_ADVERTISE_RESV            (0x1000)
#define CVI_ADVERTISE_RFAULT          (0x2000)
#define CVI_ADVERTISE_LPACK           (0x4000)
#define CVI_ADVERTISE_NPAGE           (0x8000)

/* Generic MII registers. */

#define CVI_MII_BMCR            (0x00)
#define CVI_MII_BMSR            (0x01)
#define CVI_MII_PHYSID1         (0x02)
#define CVI_MII_PHYSID2         (0x03)
#define CVI_MII_ADVERTISE       (0x04)
#define CVI_MII_LPA             (0x05)
#define CVI_MII_EXPANSION       (0x06)
#define CVI_MII_CTRL1000        (0x09)
#define CVI_MII_STAT1000        (0x0a)
#define CVI_MII_ESTATUS         (0x0f)
#define CVI_MII_DCOUNTER        (0x12)
#define CVI_MII_FCSCOUNTER      (0x13)
#define CVI_MII_NWAYTEST        (0x14)
#define CVI_MII_RERRCOUNTER     (0x15)
#define CVI_MII_SREVISION       (0x16)
#define CVI_MII_RESV1           (0x17)
#define CVI_MII_LBRERROR        (0x18)
#define CVI_MII_PHYADDR         (0x19)
#define CVI_MII_RESV2           (0x1a)
#define CVI_MII_TPISTATUS       (0x1b)
#define CVI_MII_NCONFIG         (0x1c)

#define CVI_ADVERTISE_FULL (CVI_ADVERTISE_100FULL | CVI_ADVERTISE_10FULL | \
            CVI_ADVERTISE_CSMA)
#define CVI_ADVERTISE_ALL (CVI_ADVERTISE_10HALF | CVI_ADVERTISE_10FULL | \
               CVI_ADVERTISE_100HALF | CVI_ADVERTISE_100FULL)

/* Expansion register for auto-negotiation. */
#define CVI_EXPANSION_NWAY            (0x0001)
#define CVI_EXPANSION_LCWP            (0x0002)
#define CVI_EXPANSION_ENABLENPAGE     (0x0004)
#define CVI_EXPANSION_NPCAPABLE       (0x0008)
#define CVI_EXPANSION_MFAULTS         (0x0010)
#define CVI_ESTATUS_1000_THALF        (0x1000)
#define CVI_ESTATUS_1000_TFULL        (0x2000)
#define CVI_ESTATUS_1000_XHALF        (0x4000)
#define CVI_ESTATUS_1000_XFULL        (0x8000)
#define CVI_EXPANSION_RESV            (0xffe0)

/* Link partner ability register. */
#define CVI_LPA_SLCT                 (0x001f)
#define CVI_LPA_10HALF               (0x0020)
#define CVI_LPA_1000XFULL            (0x0020)
#define CVI_LPA_10FULL               (0x0040)
#define CVI_LPA_1000XHALF            (0x0040)
#define CVI_LPA_100HALF              (0x0080)
#define CVI_LPA_1000XPAUSE           (0x0080)
#define CVI_LPA_100FULL              (0x0100)
#define CVI_LPA_1000XPAUSE_ASYM      (0x0100)
#define CVI_LPA_100BASE4             (0x0200)
#define CVI_LPA_PAUSE_CAP            (0x0400)
#define CVI_LPA_PAUSE_ASYM           (0x0800)
#define CVI_LPA_RESV                 (0x1000)
#define CVI_LPA_RFAULT               (0x2000)
#define CVI_LPA_LPACK                (0x4000)
#define CVI_LPA_NPAGE                (0x8000)

#define CVI_LPA_DUPLEX      (CVI_LPA_10FULL | CVI_LPA_100FULL)
#define CVI_LPA_100         (CVI_LPA_100FULL | CVI_LPA_100HALF | CVI_LPA_100BASE4)
/* N-way test register. */
#define CVI_NWAYTEST_RESV1      (0x00ff)
#define CVI_NWAYTEST_LOOPBACK   (0x0100)
#define CVI_NWAYTEST_RESV2      (0xfe00)

/* 1000BASE-T Control register */
#define CVI_ADVERTISE_1000FULL  0x0200
#define CVI_ADVERTISE_1000HALF  0x0100

/* 1000BASE-T Status register */
#define CVI_LPA_1000LOCALRXOK   0x2000
#define CVI_LPA_1000REMRXOK     0x1000
#define CVI_LPA_1000FULL        0x0800
#define CVI_LPA_1000HALF        0x0400

/* Flow control flags */
#define CVI_FLOW_CTRL_TX        0x01
#define CVI_FLOW_CTRL_RX        0x02

/**
 * mii_nway_result
 * @negotiated: value of MII ANAR and'd with ANLPAR
 *
 * Given a set of MII abilities, check each bit and returns the
 * currently supported media, in the priority order defined by
 * IEEE 802.3u.  We use LPA_xxx constants but note this is not the
 * value of LPA solely, as described above.
 *
 * The one exception to IEEE 802.3u is that 100baseT4 is placed
 * between 100T-full and 100T-half.  If your phy does not support
 * 100T4 this is fine. If your phy places 100T4 elsewhere in the
 * priority order, you will need to roll your own function.
 */
static inline unsigned int mii_nway_result (unsigned int negotiated)
{
    unsigned int ret;

    if (negotiated & CVI_LPA_100FULL)
        ret = CVI_LPA_100FULL;
    else if (negotiated & CVI_LPA_100BASE4)
        ret = CVI_LPA_100BASE4;
    else if (negotiated & CVI_LPA_100HALF)
        ret = CVI_LPA_100HALF;
    else if (negotiated & CVI_LPA_10FULL)
        ret = CVI_LPA_10FULL;
    else
        ret = CVI_LPA_10HALF;

    return ret;
}

/**
 * mii_duplex
 * @duplex_lock: Non-zero if duplex is locked at full
 * @negotiated: value of MII ANAR and'd with ANLPAR
 *
 * A small helper function for a common case.  Returns one
 * if the media is operating or locked at full duplex, and
 * returns zero otherwise.
 */
static inline unsigned int mii_duplex (unsigned int duplex_lock,
                       unsigned int negotiated)
{
    if (duplex_lock)
        return 1;
    if (mii_nway_result(negotiated) & CVI_LPA_DUPLEX)
        return 1;
    return 0;
}

#endif /* __LINUX_MII_H__ */