etphy.c revision 1.8 1 1.8 msaitoh /* $NetBSD: etphy.c,v 1.8 2020/02/28 05:13:19 msaitoh Exp $ */
2 1.1 jnemeth /* $OpenBSD: etphy.c,v 1.4 2008/04/02 20:12:58 brad Exp $ */
3 1.1 jnemeth
4 1.1 jnemeth /*
5 1.1 jnemeth * Copyright (c) 2007 The DragonFly Project. All rights reserved.
6 1.1 jnemeth *
7 1.1 jnemeth * This code is derived from software contributed to The DragonFly Project
8 1.1 jnemeth * by Sepherosa Ziehau <sepherosa (at) gmail.com>
9 1.1 jnemeth *
10 1.1 jnemeth * Redistribution and use in source and binary forms, with or without
11 1.1 jnemeth * modification, are permitted provided that the following conditions
12 1.1 jnemeth * are met:
13 1.1 jnemeth *
14 1.1 jnemeth * 1. Redistributions of source code must retain the above copyright
15 1.1 jnemeth * notice, this list of conditions and the following disclaimer.
16 1.1 jnemeth * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 jnemeth * notice, this list of conditions and the following disclaimer in
18 1.1 jnemeth * the documentation and/or other materials provided with the
19 1.1 jnemeth * distribution.
20 1.1 jnemeth * 3. Neither the name of The DragonFly Project nor the names of its
21 1.1 jnemeth * contributors may be used to endorse or promote products derived
22 1.1 jnemeth * from this software without specific, prior written permission.
23 1.1 jnemeth *
24 1.1 jnemeth * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 1.1 jnemeth * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 1.1 jnemeth * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 1.1 jnemeth * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 1.1 jnemeth * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 1.1 jnemeth * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 1.1 jnemeth * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 1.1 jnemeth * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 1.1 jnemeth * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 1.1 jnemeth * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34 1.1 jnemeth * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 jnemeth * SUCH DAMAGE.
36 1.1 jnemeth *
37 1.1 jnemeth * $DragonFly: src/sys/dev/netif/mii_layer/truephy.c,v 1.1 2007/10/12 14:12:42 sephe Exp $
38 1.1 jnemeth */
39 1.1 jnemeth
40 1.1 jnemeth #include <sys/cdefs.h>
41 1.8 msaitoh __KERNEL_RCSID(0, "$NetBSD: etphy.c,v 1.8 2020/02/28 05:13:19 msaitoh Exp $");
42 1.1 jnemeth
43 1.1 jnemeth #include <sys/param.h>
44 1.1 jnemeth #include <sys/systm.h>
45 1.1 jnemeth #include <sys/kernel.h>
46 1.1 jnemeth #include <sys/device.h>
47 1.1 jnemeth #include <sys/socket.h>
48 1.1 jnemeth
49 1.1 jnemeth #include <net/if.h>
50 1.1 jnemeth #include <net/if_media.h>
51 1.1 jnemeth
52 1.1 jnemeth #include <dev/mii/mii.h>
53 1.1 jnemeth #include <dev/mii/miivar.h>
54 1.1 jnemeth #include <dev/mii/miidevs.h>
55 1.1 jnemeth
56 1.1 jnemeth #define ETPHY_INDEX 0x10 /* XXX reserved in DS */
57 1.1 jnemeth #define ETPHY_INDEX_MAGIC 0x402
58 1.1 jnemeth #define ETPHY_DATA 0x11 /* XXX reserved in DS */
59 1.1 jnemeth
60 1.1 jnemeth #define ETPHY_CTRL 0x12
61 1.1 jnemeth #define ETPHY_CTRL_DIAG 0x0004
62 1.1 jnemeth #define ETPHY_CTRL_RSV1 0x0002 /* XXX reserved */
63 1.1 jnemeth #define ETPHY_CTRL_RSV0 0x0001 /* XXX reserved */
64 1.1 jnemeth
65 1.1 jnemeth #define ETPHY_CONF 0x16
66 1.1 jnemeth #define ETPHY_CONF_TXFIFO_MASK 0x3000
67 1.1 jnemeth #define ETPHY_CONF_TXFIFO_8 0x0000
68 1.1 jnemeth #define ETPHY_CONF_TXFIFO_16 0x1000
69 1.1 jnemeth #define ETPHY_CONF_TXFIFO_24 0x2000
70 1.1 jnemeth #define ETPHY_CONF_TXFIFO_32 0x3000
71 1.1 jnemeth
72 1.1 jnemeth #define ETPHY_SR 0x1a
73 1.1 jnemeth #define ETPHY_SR_SPD_MASK 0x0300
74 1.1 jnemeth #define ETPHY_SR_SPD_1000T 0x0200
75 1.1 jnemeth #define ETPHY_SR_SPD_100TX 0x0100
76 1.1 jnemeth #define ETPHY_SR_SPD_10T 0x0000
77 1.1 jnemeth #define ETPHY_SR_FDX 0x0080
78 1.1 jnemeth
79 1.1 jnemeth
80 1.5 msaitoh static int etphy_service(struct mii_softc *, struct mii_data *, int);
81 1.5 msaitoh static void etphy_attach(device_t, device_t, void *);
82 1.5 msaitoh static int etphy_match(device_t, cfdata_t, void *);
83 1.5 msaitoh static void etphy_reset(struct mii_softc *);
84 1.5 msaitoh static void etphy_status(struct mii_softc *);
85 1.1 jnemeth
86 1.5 msaitoh static const struct mii_phy_funcs etphy_funcs = {
87 1.1 jnemeth etphy_service, etphy_status, etphy_reset,
88 1.1 jnemeth };
89 1.1 jnemeth
90 1.1 jnemeth static const struct mii_phydesc etphys[] = {
91 1.3 christos MII_PHY_DESC(AGERE, ET1011),
92 1.6 msaitoh MII_PHY_DESC(AGERE, ET1011C),
93 1.3 christos MII_PHY_END,
94 1.1 jnemeth };
95 1.1 jnemeth
96 1.1 jnemeth CFATTACH_DECL_NEW(etphy, sizeof(struct mii_softc),
97 1.1 jnemeth etphy_match, etphy_attach, mii_phy_detach, mii_phy_activate);
98 1.1 jnemeth
99 1.1 jnemeth static const struct etphy_dsp {
100 1.1 jnemeth uint16_t index;
101 1.1 jnemeth uint16_t data;
102 1.1 jnemeth } etphy_dspcode[] = {
103 1.1 jnemeth { 0x880b, 0x0926 }, /* AfeIfCreg4B1000Msbs */
104 1.1 jnemeth { 0x880c, 0x0926 }, /* AfeIfCreg4B100Msbs */
105 1.1 jnemeth { 0x880d, 0x0926 }, /* AfeIfCreg4B10Msbs */
106 1.1 jnemeth
107 1.1 jnemeth { 0x880e, 0xb4d3 }, /* AfeIfCreg4B1000Lsbs */
108 1.1 jnemeth { 0x880f, 0xb4d3 }, /* AfeIfCreg4B100Lsbs */
109 1.1 jnemeth { 0x8810, 0xb4d3 }, /* AfeIfCreg4B10Lsbs */
110 1.1 jnemeth
111 1.1 jnemeth { 0x8805, 0xb03e }, /* AfeIfCreg3B1000Msbs */
112 1.1 jnemeth { 0x8806, 0xb03e }, /* AfeIfCreg3B100Msbs */
113 1.1 jnemeth { 0x8807, 0xff00 }, /* AfeIfCreg3B10Msbs */
114 1.1 jnemeth
115 1.1 jnemeth { 0x8808, 0xe090 }, /* AfeIfCreg3B1000Lsbs */
116 1.1 jnemeth { 0x8809, 0xe110 }, /* AfeIfCreg3B100Lsbs */
117 1.1 jnemeth { 0x880a, 0x0000 }, /* AfeIfCreg3B10Lsbs */
118 1.1 jnemeth
119 1.1 jnemeth { 0x300d, 1 }, /* DisableNorm */
120 1.1 jnemeth
121 1.1 jnemeth { 0x280c, 0x0180 }, /* LinkHoldEnd */
122 1.1 jnemeth
123 1.1 jnemeth { 0x1c21, 0x0002 }, /* AlphaM */
124 1.1 jnemeth
125 1.1 jnemeth { 0x3821, 6 }, /* FfeLkgTx0 */
126 1.1 jnemeth { 0x381d, 1 }, /* FfeLkg1g4 */
127 1.1 jnemeth { 0x381e, 1 }, /* FfeLkg1g5 */
128 1.1 jnemeth { 0x381f, 1 }, /* FfeLkg1g6 */
129 1.1 jnemeth { 0x3820, 1 }, /* FfeLkg1g7 */
130 1.1 jnemeth
131 1.1 jnemeth { 0x8402, 0x01f0 }, /* Btinact */
132 1.1 jnemeth { 0x800e, 20 }, /* LftrainTime */
133 1.1 jnemeth { 0x800f, 24 }, /* DvguardTime */
134 1.1 jnemeth { 0x8010, 46 } /* IdlguardTime */
135 1.1 jnemeth };
136 1.1 jnemeth
137 1.5 msaitoh static int
138 1.1 jnemeth etphy_match(device_t parent, cfdata_t match, void *aux)
139 1.1 jnemeth {
140 1.1 jnemeth struct mii_attach_args *ma = aux;
141 1.1 jnemeth
142 1.1 jnemeth if (mii_phy_match(ma, etphys) != NULL)
143 1.1 jnemeth return 10;
144 1.1 jnemeth
145 1.1 jnemeth return 0;
146 1.1 jnemeth }
147 1.1 jnemeth
148 1.5 msaitoh static void
149 1.1 jnemeth etphy_attach(device_t parent, device_t self, void *aux)
150 1.1 jnemeth {
151 1.1 jnemeth struct mii_softc *sc = device_private(self);
152 1.1 jnemeth struct mii_attach_args *ma = aux;
153 1.1 jnemeth struct mii_data *mii = ma->mii_data;
154 1.1 jnemeth const struct mii_phydesc *mpd;
155 1.1 jnemeth
156 1.1 jnemeth mpd = mii_phy_match(ma, etphys);
157 1.1 jnemeth aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
158 1.1 jnemeth
159 1.1 jnemeth sc->mii_dev = self;
160 1.1 jnemeth sc->mii_inst = mii->mii_instance;
161 1.1 jnemeth sc->mii_phy = ma->mii_phyno;
162 1.1 jnemeth sc->mii_funcs = &etphy_funcs;
163 1.1 jnemeth sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
164 1.1 jnemeth sc->mii_pdata = mii;
165 1.1 jnemeth sc->mii_flags = ma->mii_flags;
166 1.1 jnemeth
167 1.1 jnemeth sc->mii_flags |= MIIF_NOISOLATE | MIIF_NOLOOP;
168 1.1 jnemeth
169 1.1 jnemeth PHY_RESET(sc);
170 1.1 jnemeth
171 1.2 msaitoh PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
172 1.2 msaitoh sc->mii_capabilities &= ma->mii_capmask;
173 1.1 jnemeth if (sc->mii_capabilities & BMSR_EXTSTAT) {
174 1.2 msaitoh PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
175 1.1 jnemeth /* No 1000baseT half-duplex support */
176 1.1 jnemeth sc->mii_extcapabilities &= ~EXTSR_1000THDX;
177 1.1 jnemeth }
178 1.1 jnemeth
179 1.7 msaitoh mii_phy_add_media(sc);
180 1.1 jnemeth }
181 1.1 jnemeth
182 1.5 msaitoh static int
183 1.1 jnemeth etphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
184 1.1 jnemeth {
185 1.1 jnemeth struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
186 1.2 msaitoh uint16_t bmcr;
187 1.1 jnemeth
188 1.1 jnemeth switch (cmd) {
189 1.1 jnemeth case MII_POLLSTAT:
190 1.4 msaitoh /* If we're not polling our PHY instance, just return. */
191 1.1 jnemeth if (IFM_INST(ife->ifm_media) != sc->mii_inst)
192 1.1 jnemeth return 0;
193 1.1 jnemeth break;
194 1.1 jnemeth
195 1.1 jnemeth case MII_MEDIACHG:
196 1.1 jnemeth /*
197 1.1 jnemeth * If the media indicates a different PHY instance,
198 1.1 jnemeth * isolate ourselves.
199 1.1 jnemeth */
200 1.1 jnemeth if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
201 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
202 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
203 1.1 jnemeth return 0;
204 1.1 jnemeth }
205 1.1 jnemeth
206 1.4 msaitoh /* If the interface is not up, don't do anything. */
207 1.1 jnemeth if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
208 1.1 jnemeth break;
209 1.1 jnemeth
210 1.1 jnemeth if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
211 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
212 1.2 msaitoh bmcr &= ~BMCR_AUTOEN;
213 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr);
214 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_PDOWN);
215 1.8 msaitoh } else {
216 1.8 msaitoh /*
217 1.8 msaitoh * Issue reset before configuring autonego.
218 1.8 msaitoh * XXX Is this required?
219 1.8 msaitoh */
220 1.8 msaitoh PHY_RESET(sc);
221 1.1 jnemeth }
222 1.1 jnemeth
223 1.1 jnemeth mii_phy_setmedia(sc);
224 1.1 jnemeth
225 1.1 jnemeth if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
226 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
227 1.2 msaitoh bmcr &= ~BMCR_PDOWN;
228 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr);
229 1.1 jnemeth
230 1.1 jnemeth if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) {
231 1.1 jnemeth PHY_WRITE(sc, MII_BMCR,
232 1.5 msaitoh bmcr | BMCR_AUTOEN | BMCR_STARTNEG);
233 1.1 jnemeth }
234 1.1 jnemeth }
235 1.1 jnemeth break;
236 1.1 jnemeth
237 1.1 jnemeth case MII_TICK:
238 1.4 msaitoh /* If we're not currently selected, just return. */
239 1.1 jnemeth if (IFM_INST(ife->ifm_media) != sc->mii_inst)
240 1.1 jnemeth return 0;
241 1.1 jnemeth
242 1.1 jnemeth if (mii_phy_tick(sc) == EJUSTRETURN)
243 1.1 jnemeth return 0;
244 1.1 jnemeth break;
245 1.1 jnemeth }
246 1.1 jnemeth
247 1.1 jnemeth /* Update the media status. */
248 1.1 jnemeth mii_phy_status(sc);
249 1.1 jnemeth
250 1.1 jnemeth /* Callback if something changed. */
251 1.1 jnemeth mii_phy_update(sc, cmd);
252 1.1 jnemeth return 0;
253 1.1 jnemeth }
254 1.1 jnemeth
255 1.5 msaitoh static void
256 1.1 jnemeth etphy_reset(struct mii_softc *sc)
257 1.1 jnemeth {
258 1.2 msaitoh uint16_t reg;
259 1.1 jnemeth int i;
260 1.1 jnemeth
261 1.6 msaitoh if (sc->mii_mpd_model == MII_MODEL_AGERE_ET1011) {
262 1.6 msaitoh mii_phy_reset(sc);
263 1.6 msaitoh return;
264 1.6 msaitoh }
265 1.6 msaitoh
266 1.1 jnemeth for (i = 0; i < 2; ++i) {
267 1.2 msaitoh PHY_READ(sc, MII_PHYIDR1, ®);
268 1.2 msaitoh PHY_READ(sc, MII_PHYIDR2, ®);
269 1.1 jnemeth
270 1.2 msaitoh PHY_READ(sc, ETPHY_CTRL, ®);
271 1.5 msaitoh PHY_WRITE(sc, ETPHY_CTRL, ETPHY_CTRL_DIAG | ETPHY_CTRL_RSV1);
272 1.1 jnemeth
273 1.1 jnemeth PHY_WRITE(sc, ETPHY_INDEX, ETPHY_INDEX_MAGIC);
274 1.2 msaitoh PHY_READ(sc, ETPHY_DATA, ®);
275 1.1 jnemeth
276 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL, ETPHY_CTRL_RSV1);
277 1.1 jnemeth }
278 1.1 jnemeth
279 1.2 msaitoh PHY_READ(sc, MII_BMCR, ®);
280 1.2 msaitoh PHY_READ(sc, ETPHY_CTRL, ®);
281 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_PDOWN | BMCR_S1000);
282 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL,
283 1.1 jnemeth ETPHY_CTRL_DIAG | ETPHY_CTRL_RSV1 | ETPHY_CTRL_RSV0);
284 1.1 jnemeth
285 1.1 jnemeth #define N(arr) (int)(sizeof(arr) / sizeof(arr[0]))
286 1.1 jnemeth
287 1.1 jnemeth for (i = 0; i < N(etphy_dspcode); ++i) {
288 1.1 jnemeth const struct etphy_dsp *dsp = &etphy_dspcode[i];
289 1.1 jnemeth
290 1.1 jnemeth PHY_WRITE(sc, ETPHY_INDEX, dsp->index);
291 1.1 jnemeth PHY_WRITE(sc, ETPHY_DATA, dsp->data);
292 1.1 jnemeth
293 1.1 jnemeth PHY_WRITE(sc, ETPHY_INDEX, dsp->index);
294 1.2 msaitoh PHY_READ(sc, ETPHY_DATA, ®);
295 1.1 jnemeth }
296 1.1 jnemeth
297 1.1 jnemeth #undef N
298 1.1 jnemeth
299 1.2 msaitoh PHY_READ(sc, MII_BMCR, ®);
300 1.2 msaitoh PHY_READ(sc, ETPHY_CTRL, ®);
301 1.5 msaitoh PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_S1000);
302 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL, ETPHY_CTRL_RSV1);
303 1.1 jnemeth
304 1.1 jnemeth mii_phy_reset(sc);
305 1.1 jnemeth }
306 1.1 jnemeth
307 1.5 msaitoh static void
308 1.1 jnemeth etphy_status(struct mii_softc *sc)
309 1.1 jnemeth {
310 1.1 jnemeth struct mii_data *mii = sc->mii_pdata;
311 1.2 msaitoh uint16_t bmsr, bmcr, sr;
312 1.1 jnemeth
313 1.1 jnemeth mii->mii_media_status = IFM_AVALID;
314 1.1 jnemeth mii->mii_media_active = IFM_ETHER;
315 1.1 jnemeth
316 1.2 msaitoh PHY_READ(sc, ETPHY_SR, &sr);
317 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
318 1.1 jnemeth
319 1.2 msaitoh PHY_READ(sc, MII_BMSR, &bmsr);
320 1.2 msaitoh PHY_READ(sc, MII_BMSR, &bmsr);
321 1.1 jnemeth if (bmsr & BMSR_LINK)
322 1.1 jnemeth mii->mii_media_status |= IFM_ACTIVE;
323 1.1 jnemeth
324 1.1 jnemeth if (bmcr & BMCR_AUTOEN) {
325 1.1 jnemeth if ((bmsr & BMSR_ACOMP) == 0) {
326 1.1 jnemeth mii->mii_media_active |= IFM_NONE;
327 1.1 jnemeth return;
328 1.1 jnemeth }
329 1.1 jnemeth }
330 1.1 jnemeth
331 1.1 jnemeth switch (sr & ETPHY_SR_SPD_MASK) {
332 1.1 jnemeth case ETPHY_SR_SPD_1000T:
333 1.1 jnemeth mii->mii_media_active |= IFM_1000_T;
334 1.1 jnemeth break;
335 1.1 jnemeth case ETPHY_SR_SPD_100TX:
336 1.1 jnemeth mii->mii_media_active |= IFM_100_TX;
337 1.1 jnemeth break;
338 1.1 jnemeth case ETPHY_SR_SPD_10T:
339 1.1 jnemeth mii->mii_media_active |= IFM_10_T;
340 1.1 jnemeth break;
341 1.1 jnemeth default:
342 1.1 jnemeth mii->mii_media_active |= IFM_NONE;
343 1.1 jnemeth return;
344 1.1 jnemeth }
345 1.1 jnemeth
346 1.1 jnemeth if (sr & ETPHY_SR_FDX)
347 1.6 msaitoh mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
348 1.1 jnemeth else
349 1.1 jnemeth mii->mii_media_active |= IFM_HDX;
350 1.1 jnemeth }
351