etphy.c revision 1.3 1 1.3 christos /* $NetBSD: etphy.c,v 1.3 2019/02/24 17:22:21 christos 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.3 christos __KERNEL_RCSID(0, "$NetBSD: etphy.c,v 1.3 2019/02/24 17:22:21 christos 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.1 jnemeth int etphy_service(struct mii_softc *, struct mii_data *, int);
81 1.1 jnemeth void etphy_attach(device_t, device_t, void *);
82 1.1 jnemeth int etphy_match(device_t, cfdata_t, void *);
83 1.1 jnemeth void etphy_reset(struct mii_softc *);
84 1.1 jnemeth void etphy_status(struct mii_softc *);
85 1.1 jnemeth
86 1.1 jnemeth 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.3 christos MII_PHY_END,
93 1.1 jnemeth };
94 1.1 jnemeth
95 1.1 jnemeth CFATTACH_DECL_NEW(etphy, sizeof(struct mii_softc),
96 1.1 jnemeth etphy_match, etphy_attach, mii_phy_detach, mii_phy_activate);
97 1.1 jnemeth
98 1.1 jnemeth static const struct etphy_dsp {
99 1.1 jnemeth uint16_t index;
100 1.1 jnemeth uint16_t data;
101 1.1 jnemeth } etphy_dspcode[] = {
102 1.1 jnemeth { 0x880b, 0x0926 }, /* AfeIfCreg4B1000Msbs */
103 1.1 jnemeth { 0x880c, 0x0926 }, /* AfeIfCreg4B100Msbs */
104 1.1 jnemeth { 0x880d, 0x0926 }, /* AfeIfCreg4B10Msbs */
105 1.1 jnemeth
106 1.1 jnemeth { 0x880e, 0xb4d3 }, /* AfeIfCreg4B1000Lsbs */
107 1.1 jnemeth { 0x880f, 0xb4d3 }, /* AfeIfCreg4B100Lsbs */
108 1.1 jnemeth { 0x8810, 0xb4d3 }, /* AfeIfCreg4B10Lsbs */
109 1.1 jnemeth
110 1.1 jnemeth { 0x8805, 0xb03e }, /* AfeIfCreg3B1000Msbs */
111 1.1 jnemeth { 0x8806, 0xb03e }, /* AfeIfCreg3B100Msbs */
112 1.1 jnemeth { 0x8807, 0xff00 }, /* AfeIfCreg3B10Msbs */
113 1.1 jnemeth
114 1.1 jnemeth { 0x8808, 0xe090 }, /* AfeIfCreg3B1000Lsbs */
115 1.1 jnemeth { 0x8809, 0xe110 }, /* AfeIfCreg3B100Lsbs */
116 1.1 jnemeth { 0x880a, 0x0000 }, /* AfeIfCreg3B10Lsbs */
117 1.1 jnemeth
118 1.1 jnemeth { 0x300d, 1 }, /* DisableNorm */
119 1.1 jnemeth
120 1.1 jnemeth { 0x280c, 0x0180 }, /* LinkHoldEnd */
121 1.1 jnemeth
122 1.1 jnemeth { 0x1c21, 0x0002 }, /* AlphaM */
123 1.1 jnemeth
124 1.1 jnemeth { 0x3821, 6 }, /* FfeLkgTx0 */
125 1.1 jnemeth { 0x381d, 1 }, /* FfeLkg1g4 */
126 1.1 jnemeth { 0x381e, 1 }, /* FfeLkg1g5 */
127 1.1 jnemeth { 0x381f, 1 }, /* FfeLkg1g6 */
128 1.1 jnemeth { 0x3820, 1 }, /* FfeLkg1g7 */
129 1.1 jnemeth
130 1.1 jnemeth { 0x8402, 0x01f0 }, /* Btinact */
131 1.1 jnemeth { 0x800e, 20 }, /* LftrainTime */
132 1.1 jnemeth { 0x800f, 24 }, /* DvguardTime */
133 1.1 jnemeth { 0x8010, 46 } /* IdlguardTime */
134 1.1 jnemeth };
135 1.1 jnemeth
136 1.1 jnemeth int
137 1.1 jnemeth etphy_match(device_t parent, cfdata_t match, void *aux)
138 1.1 jnemeth {
139 1.1 jnemeth struct mii_attach_args *ma = aux;
140 1.1 jnemeth
141 1.1 jnemeth if (mii_phy_match(ma, etphys) != NULL)
142 1.1 jnemeth return 10;
143 1.1 jnemeth
144 1.1 jnemeth return 0;
145 1.1 jnemeth }
146 1.1 jnemeth
147 1.1 jnemeth void
148 1.1 jnemeth etphy_attach(device_t parent, device_t self, void *aux)
149 1.1 jnemeth {
150 1.1 jnemeth struct mii_softc *sc = device_private(self);
151 1.1 jnemeth struct mii_attach_args *ma = aux;
152 1.1 jnemeth struct mii_data *mii = ma->mii_data;
153 1.1 jnemeth const struct mii_phydesc *mpd;
154 1.1 jnemeth
155 1.1 jnemeth mpd = mii_phy_match(ma, etphys);
156 1.1 jnemeth aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
157 1.1 jnemeth
158 1.1 jnemeth sc->mii_dev = self;
159 1.1 jnemeth sc->mii_inst = mii->mii_instance;
160 1.1 jnemeth sc->mii_phy = ma->mii_phyno;
161 1.1 jnemeth sc->mii_funcs = &etphy_funcs;
162 1.1 jnemeth sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
163 1.1 jnemeth sc->mii_pdata = mii;
164 1.1 jnemeth sc->mii_flags = ma->mii_flags;
165 1.1 jnemeth
166 1.1 jnemeth sc->mii_flags |= MIIF_NOISOLATE | MIIF_NOLOOP;
167 1.1 jnemeth
168 1.1 jnemeth PHY_RESET(sc);
169 1.1 jnemeth
170 1.2 msaitoh PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
171 1.2 msaitoh sc->mii_capabilities &= ma->mii_capmask;
172 1.1 jnemeth if (sc->mii_capabilities & BMSR_EXTSTAT) {
173 1.2 msaitoh PHY_READ(sc, MII_EXTSR, &sc->mii_extcapabilities);
174 1.1 jnemeth /* No 1000baseT half-duplex support */
175 1.1 jnemeth sc->mii_extcapabilities &= ~EXTSR_1000THDX;
176 1.1 jnemeth }
177 1.1 jnemeth aprint_normal_dev(self, "");
178 1.1 jnemeth if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
179 1.1 jnemeth aprint_error("no media present");
180 1.1 jnemeth else
181 1.1 jnemeth mii_phy_add_media(sc);
182 1.1 jnemeth aprint_normal("\n");
183 1.1 jnemeth
184 1.1 jnemeth if (!pmf_device_register(self, NULL, mii_phy_resume))
185 1.1 jnemeth aprint_error_dev(self, "couldn't establish power handler\n");
186 1.1 jnemeth }
187 1.1 jnemeth
188 1.1 jnemeth int
189 1.1 jnemeth etphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
190 1.1 jnemeth {
191 1.1 jnemeth struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
192 1.2 msaitoh uint16_t bmcr;
193 1.1 jnemeth
194 1.1 jnemeth switch (cmd) {
195 1.1 jnemeth case MII_POLLSTAT:
196 1.1 jnemeth /*
197 1.1 jnemeth * If we're not polling our PHY instance, just return.
198 1.1 jnemeth */
199 1.1 jnemeth if (IFM_INST(ife->ifm_media) != sc->mii_inst)
200 1.1 jnemeth return 0;
201 1.1 jnemeth break;
202 1.1 jnemeth
203 1.1 jnemeth case MII_MEDIACHG:
204 1.1 jnemeth /*
205 1.1 jnemeth * If the media indicates a different PHY instance,
206 1.1 jnemeth * isolate ourselves.
207 1.1 jnemeth */
208 1.1 jnemeth if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
209 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
210 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
211 1.1 jnemeth return 0;
212 1.1 jnemeth }
213 1.1 jnemeth
214 1.1 jnemeth /*
215 1.1 jnemeth * If the interface is not up, don't do anything.
216 1.1 jnemeth */
217 1.1 jnemeth if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
218 1.1 jnemeth break;
219 1.1 jnemeth
220 1.1 jnemeth if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
221 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
222 1.2 msaitoh bmcr &= ~BMCR_AUTOEN;
223 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr);
224 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_PDOWN);
225 1.1 jnemeth }
226 1.1 jnemeth
227 1.1 jnemeth mii_phy_setmedia(sc);
228 1.1 jnemeth
229 1.1 jnemeth if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
230 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
231 1.2 msaitoh bmcr &= ~BMCR_PDOWN;
232 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr);
233 1.1 jnemeth
234 1.1 jnemeth if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) {
235 1.1 jnemeth PHY_WRITE(sc, MII_BMCR,
236 1.1 jnemeth bmcr | BMCR_AUTOEN | BMCR_STARTNEG);
237 1.1 jnemeth }
238 1.1 jnemeth }
239 1.1 jnemeth break;
240 1.1 jnemeth
241 1.1 jnemeth case MII_TICK:
242 1.1 jnemeth /*
243 1.1 jnemeth * If we're not currently selected, just return.
244 1.1 jnemeth */
245 1.1 jnemeth if (IFM_INST(ife->ifm_media) != sc->mii_inst)
246 1.1 jnemeth return 0;
247 1.1 jnemeth
248 1.1 jnemeth if (mii_phy_tick(sc) == EJUSTRETURN)
249 1.1 jnemeth return 0;
250 1.1 jnemeth break;
251 1.1 jnemeth }
252 1.1 jnemeth
253 1.1 jnemeth /* Update the media status. */
254 1.1 jnemeth mii_phy_status(sc);
255 1.1 jnemeth
256 1.1 jnemeth /* Callback if something changed. */
257 1.1 jnemeth mii_phy_update(sc, cmd);
258 1.1 jnemeth return 0;
259 1.1 jnemeth }
260 1.1 jnemeth
261 1.1 jnemeth void
262 1.1 jnemeth etphy_reset(struct mii_softc *sc)
263 1.1 jnemeth {
264 1.2 msaitoh uint16_t reg;
265 1.1 jnemeth int i;
266 1.1 jnemeth
267 1.1 jnemeth for (i = 0; i < 2; ++i) {
268 1.2 msaitoh PHY_READ(sc, MII_PHYIDR1, ®);
269 1.2 msaitoh PHY_READ(sc, MII_PHYIDR2, ®);
270 1.1 jnemeth
271 1.2 msaitoh PHY_READ(sc, ETPHY_CTRL, ®);
272 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL,
273 1.1 jnemeth ETPHY_CTRL_DIAG | ETPHY_CTRL_RSV1);
274 1.1 jnemeth
275 1.1 jnemeth PHY_WRITE(sc, ETPHY_INDEX, ETPHY_INDEX_MAGIC);
276 1.2 msaitoh PHY_READ(sc, ETPHY_DATA, ®);
277 1.1 jnemeth
278 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL, ETPHY_CTRL_RSV1);
279 1.1 jnemeth }
280 1.1 jnemeth
281 1.2 msaitoh PHY_READ(sc, MII_BMCR, ®);
282 1.2 msaitoh PHY_READ(sc, ETPHY_CTRL, ®);
283 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_PDOWN | BMCR_S1000);
284 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL,
285 1.1 jnemeth ETPHY_CTRL_DIAG | ETPHY_CTRL_RSV1 | ETPHY_CTRL_RSV0);
286 1.1 jnemeth
287 1.1 jnemeth #define N(arr) (int)(sizeof(arr) / sizeof(arr[0]))
288 1.1 jnemeth
289 1.1 jnemeth for (i = 0; i < N(etphy_dspcode); ++i) {
290 1.1 jnemeth const struct etphy_dsp *dsp = &etphy_dspcode[i];
291 1.1 jnemeth
292 1.1 jnemeth PHY_WRITE(sc, ETPHY_INDEX, dsp->index);
293 1.1 jnemeth PHY_WRITE(sc, ETPHY_DATA, dsp->data);
294 1.1 jnemeth
295 1.1 jnemeth PHY_WRITE(sc, ETPHY_INDEX, dsp->index);
296 1.2 msaitoh PHY_READ(sc, ETPHY_DATA, ®);
297 1.1 jnemeth }
298 1.1 jnemeth
299 1.1 jnemeth #undef N
300 1.1 jnemeth
301 1.2 msaitoh PHY_READ(sc, MII_BMCR, ®);
302 1.2 msaitoh PHY_READ(sc, ETPHY_CTRL, ®);
303 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_S1000);
304 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL, ETPHY_CTRL_RSV1);
305 1.1 jnemeth
306 1.1 jnemeth mii_phy_reset(sc);
307 1.1 jnemeth }
308 1.1 jnemeth
309 1.1 jnemeth void
310 1.1 jnemeth etphy_status(struct mii_softc *sc)
311 1.1 jnemeth {
312 1.1 jnemeth struct mii_data *mii = sc->mii_pdata;
313 1.2 msaitoh uint16_t bmsr, bmcr, sr;
314 1.1 jnemeth
315 1.1 jnemeth mii->mii_media_status = IFM_AVALID;
316 1.1 jnemeth mii->mii_media_active = IFM_ETHER;
317 1.1 jnemeth
318 1.2 msaitoh PHY_READ(sc, ETPHY_SR, &sr);
319 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
320 1.1 jnemeth
321 1.2 msaitoh PHY_READ(sc, MII_BMSR, &bmsr);
322 1.2 msaitoh PHY_READ(sc, MII_BMSR, &bmsr);
323 1.1 jnemeth if (bmsr & BMSR_LINK)
324 1.1 jnemeth mii->mii_media_status |= IFM_ACTIVE;
325 1.1 jnemeth
326 1.1 jnemeth if (bmcr & BMCR_AUTOEN) {
327 1.1 jnemeth if ((bmsr & BMSR_ACOMP) == 0) {
328 1.1 jnemeth mii->mii_media_active |= IFM_NONE;
329 1.1 jnemeth return;
330 1.1 jnemeth }
331 1.1 jnemeth }
332 1.1 jnemeth
333 1.1 jnemeth switch (sr & ETPHY_SR_SPD_MASK) {
334 1.1 jnemeth case ETPHY_SR_SPD_1000T:
335 1.1 jnemeth mii->mii_media_active |= IFM_1000_T;
336 1.1 jnemeth break;
337 1.1 jnemeth case ETPHY_SR_SPD_100TX:
338 1.1 jnemeth mii->mii_media_active |= IFM_100_TX;
339 1.1 jnemeth break;
340 1.1 jnemeth case ETPHY_SR_SPD_10T:
341 1.1 jnemeth mii->mii_media_active |= IFM_10_T;
342 1.1 jnemeth break;
343 1.1 jnemeth default:
344 1.1 jnemeth mii->mii_media_active |= IFM_NONE;
345 1.1 jnemeth return;
346 1.1 jnemeth }
347 1.1 jnemeth
348 1.1 jnemeth if (sr & ETPHY_SR_FDX)
349 1.1 jnemeth mii->mii_media_active |= IFM_FDX;
350 1.1 jnemeth else
351 1.1 jnemeth mii->mii_media_active |= IFM_HDX;
352 1.1 jnemeth }
353