1 /* $OpenBSD: exphy.c,v 1.24 2022/04/06 18:59:29 naddy Exp $ */
2 /* $NetBSD: exphy.c,v 1.23 2000/02/02 23:34:56 thorpej Exp $ */
3
4 /*-
5 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 * NASA Ames Research Center, and by Frank van der Linden.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
50 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
55 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 */
57
58 /*
59 * driver for 3Com internal PHYs
60 */
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/device.h>
65 #include <sys/socket.h>
66
67 #include <net/if.h>
68 #include <net/if_var.h>
69 #include <net/if_media.h>
70
71 #include <dev/mii/mii.h>
72 #include <dev/mii/miivar.h>
73 #include <dev/mii/miidevs.h>
74
75 int exphymatch(struct device *, void *, void *);
76 void exphyattach(struct device *, struct device *, void *);
77
78 const struct cfattach exphy_ca = {
79 sizeof(struct mii_softc), exphymatch, exphyattach, mii_phy_detach
80 };
81
82 struct cfdriver exphy_cd = {
83 NULL, "exphy", DV_DULL
84 };
85
86 int exphy_service(struct mii_softc *, struct mii_data *, int);
87 void exphy_reset(struct mii_softc *);
88
89 const struct mii_phy_funcs exphy_funcs = {
90 exphy_service, ukphy_status, exphy_reset,
91 };
92
93 int
exphymatch(struct device * parent,void * match,void * aux)94 exphymatch(struct device *parent, void *match, void *aux)
95 {
96 struct mii_attach_args *ma = aux;
97
98 /*
99 * Since 3com's PHY for some xl adapters is braindead and doesn't
100 * report the proper OUI/MODEL information, we have this stupid
101 * match function.
102 */
103 if ((strcmp(parent->dv_cfdata->cf_driver->cd_name, "xl") == 0) &&
104 ((MII_OUI(ma->mii_id1, ma->mii_id2) == 0 &&
105 MII_MODEL(ma->mii_id2) == 0) ||
106 (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_3COM &&
107 MII_MODEL(ma->mii_id2) == 0)))
108 return (10);
109
110 return (0);
111 }
112
113 void
exphyattach(struct device * parent,struct device * self,void * aux)114 exphyattach(struct device *parent, struct device *self, void *aux)
115 {
116 struct mii_softc *sc = (struct mii_softc *)self;
117 struct mii_attach_args *ma = aux;
118 struct mii_data *mii = ma->mii_data;
119
120 printf(": 3Com internal media interface\n");
121
122 sc->mii_inst = mii->mii_instance;
123 sc->mii_phy = ma->mii_phyno;
124 sc->mii_funcs = &exphy_funcs;
125 sc->mii_pdata = mii;
126 sc->mii_flags = ma->mii_flags;
127
128 sc->mii_flags |= MIIF_NOISOLATE;
129
130 /*
131 * The 3Com PHY can never be isolated, so never allow non-zero
132 * instances!
133 */
134 if (mii->mii_instance != 0) {
135 printf("%s: ignoring this PHY, non-zero instance\n",
136 sc->mii_dev.dv_xname);
137 return;
138 }
139
140 PHY_RESET(sc);
141
142 sc->mii_capabilities =
143 PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
144 if (sc->mii_capabilities & BMSR_MEDIAMASK)
145 mii_phy_add_media(sc);
146 }
147
148 int
exphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)149 exphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
150 {
151 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
152
153 if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
154 return (ENXIO);
155
156 /*
157 * We can't isolate the 3Com PHY, so it has to be the only one!
158 */
159 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
160 panic("exphy_service: can't isolate 3Com PHY");
161
162 switch (cmd) {
163 case MII_POLLSTAT:
164 break;
165
166 case MII_MEDIACHG:
167 /*
168 * If the interface is not up, don't do anything.
169 */
170 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
171 break;
172
173 mii_phy_setmedia(sc);
174 break;
175
176 case MII_TICK:
177 if (mii_phy_tick(sc) == EJUSTRETURN)
178 return (0);
179
180 break;
181
182 case MII_DOWN:
183 mii_phy_down(sc);
184 return (0);
185 }
186
187 /* Update the media status. */
188 mii_phy_status(sc);
189
190 /* Callback if something changed. */
191 mii_phy_update(sc, cmd);
192 return (0);
193 }
194
195 void
exphy_reset(struct mii_softc * sc)196 exphy_reset(struct mii_softc *sc)
197 {
198
199 mii_phy_reset(sc);
200
201 /*
202 * XXX 3Com PHY doesn't set the BMCR properly after
203 * XXX reset, which breaks autonegotiation.
204 */
205 PHY_WRITE(sc, MII_BMCR, BMCR_S100|BMCR_AUTOEN|BMCR_FDX);
206 }
207