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