etphy.c revision 1.5 1 1.5 msaitoh /* $NetBSD: etphy.c,v 1.5 2019/08/16 15:17:31 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.5 msaitoh __KERNEL_RCSID(0, "$NetBSD: etphy.c,v 1.5 2019/08/16 15:17:31 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.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.5 msaitoh static 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.5 msaitoh static 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.5 msaitoh static 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.4 msaitoh /* If we're not polling our PHY instance, just return. */
197 1.1 jnemeth if (IFM_INST(ife->ifm_media) != sc->mii_inst)
198 1.1 jnemeth return 0;
199 1.1 jnemeth break;
200 1.1 jnemeth
201 1.1 jnemeth case MII_MEDIACHG:
202 1.1 jnemeth /*
203 1.1 jnemeth * If the media indicates a different PHY instance,
204 1.1 jnemeth * isolate ourselves.
205 1.1 jnemeth */
206 1.1 jnemeth if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
207 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
208 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
209 1.1 jnemeth return 0;
210 1.1 jnemeth }
211 1.1 jnemeth
212 1.4 msaitoh /* If the interface is not up, don't do anything. */
213 1.1 jnemeth if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
214 1.1 jnemeth break;
215 1.1 jnemeth
216 1.1 jnemeth if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
217 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
218 1.2 msaitoh bmcr &= ~BMCR_AUTOEN;
219 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr);
220 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_PDOWN);
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.1 jnemeth for (i = 0; i < 2; ++i) {
262 1.2 msaitoh PHY_READ(sc, MII_PHYIDR1, ®);
263 1.2 msaitoh PHY_READ(sc, MII_PHYIDR2, ®);
264 1.1 jnemeth
265 1.2 msaitoh PHY_READ(sc, ETPHY_CTRL, ®);
266 1.5 msaitoh PHY_WRITE(sc, ETPHY_CTRL, ETPHY_CTRL_DIAG | ETPHY_CTRL_RSV1);
267 1.1 jnemeth
268 1.1 jnemeth PHY_WRITE(sc, ETPHY_INDEX, ETPHY_INDEX_MAGIC);
269 1.2 msaitoh PHY_READ(sc, ETPHY_DATA, ®);
270 1.1 jnemeth
271 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL, ETPHY_CTRL_RSV1);
272 1.1 jnemeth }
273 1.1 jnemeth
274 1.2 msaitoh PHY_READ(sc, MII_BMCR, ®);
275 1.2 msaitoh PHY_READ(sc, ETPHY_CTRL, ®);
276 1.1 jnemeth PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_PDOWN | BMCR_S1000);
277 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL,
278 1.1 jnemeth ETPHY_CTRL_DIAG | ETPHY_CTRL_RSV1 | ETPHY_CTRL_RSV0);
279 1.1 jnemeth
280 1.1 jnemeth #define N(arr) (int)(sizeof(arr) / sizeof(arr[0]))
281 1.1 jnemeth
282 1.1 jnemeth for (i = 0; i < N(etphy_dspcode); ++i) {
283 1.1 jnemeth const struct etphy_dsp *dsp = &etphy_dspcode[i];
284 1.1 jnemeth
285 1.1 jnemeth PHY_WRITE(sc, ETPHY_INDEX, dsp->index);
286 1.1 jnemeth PHY_WRITE(sc, ETPHY_DATA, dsp->data);
287 1.1 jnemeth
288 1.1 jnemeth PHY_WRITE(sc, ETPHY_INDEX, dsp->index);
289 1.2 msaitoh PHY_READ(sc, ETPHY_DATA, ®);
290 1.1 jnemeth }
291 1.1 jnemeth
292 1.1 jnemeth #undef N
293 1.1 jnemeth
294 1.2 msaitoh PHY_READ(sc, MII_BMCR, ®);
295 1.2 msaitoh PHY_READ(sc, ETPHY_CTRL, ®);
296 1.5 msaitoh PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_S1000);
297 1.1 jnemeth PHY_WRITE(sc, ETPHY_CTRL, ETPHY_CTRL_RSV1);
298 1.1 jnemeth
299 1.1 jnemeth mii_phy_reset(sc);
300 1.1 jnemeth }
301 1.1 jnemeth
302 1.5 msaitoh static void
303 1.1 jnemeth etphy_status(struct mii_softc *sc)
304 1.1 jnemeth {
305 1.1 jnemeth struct mii_data *mii = sc->mii_pdata;
306 1.2 msaitoh uint16_t bmsr, bmcr, sr;
307 1.1 jnemeth
308 1.1 jnemeth mii->mii_media_status = IFM_AVALID;
309 1.1 jnemeth mii->mii_media_active = IFM_ETHER;
310 1.1 jnemeth
311 1.2 msaitoh PHY_READ(sc, ETPHY_SR, &sr);
312 1.2 msaitoh PHY_READ(sc, MII_BMCR, &bmcr);
313 1.1 jnemeth
314 1.2 msaitoh PHY_READ(sc, MII_BMSR, &bmsr);
315 1.2 msaitoh PHY_READ(sc, MII_BMSR, &bmsr);
316 1.1 jnemeth if (bmsr & BMSR_LINK)
317 1.1 jnemeth mii->mii_media_status |= IFM_ACTIVE;
318 1.1 jnemeth
319 1.1 jnemeth if (bmcr & BMCR_AUTOEN) {
320 1.1 jnemeth if ((bmsr & BMSR_ACOMP) == 0) {
321 1.1 jnemeth mii->mii_media_active |= IFM_NONE;
322 1.1 jnemeth return;
323 1.1 jnemeth }
324 1.1 jnemeth }
325 1.1 jnemeth
326 1.1 jnemeth switch (sr & ETPHY_SR_SPD_MASK) {
327 1.1 jnemeth case ETPHY_SR_SPD_1000T:
328 1.1 jnemeth mii->mii_media_active |= IFM_1000_T;
329 1.1 jnemeth break;
330 1.1 jnemeth case ETPHY_SR_SPD_100TX:
331 1.1 jnemeth mii->mii_media_active |= IFM_100_TX;
332 1.1 jnemeth break;
333 1.1 jnemeth case ETPHY_SR_SPD_10T:
334 1.1 jnemeth mii->mii_media_active |= IFM_10_T;
335 1.1 jnemeth break;
336 1.1 jnemeth default:
337 1.1 jnemeth mii->mii_media_active |= IFM_NONE;
338 1.1 jnemeth return;
339 1.1 jnemeth }
340 1.1 jnemeth
341 1.1 jnemeth if (sr & ETPHY_SR_FDX)
342 1.1 jnemeth mii->mii_media_active |= IFM_FDX;
343 1.1 jnemeth else
344 1.1 jnemeth mii->mii_media_active |= IFM_HDX;
345 1.1 jnemeth }
346