xref: /openbsd-src/sys/dev/mii/exphy.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: exphy.c,v 1.12 2003/02/13 06:02:09 fgsch 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  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *	This product includes software developed by the NetBSD
23  *	Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40 
41 /*
42  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *	This product includes software developed by Manuel Bouyer.
55  * 4. The name of the author may not be used to endorse or promote products
56  *    derived from this software without specific prior written permission.
57  *
58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
62  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
63  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
67  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68  */
69 
70 /*
71  * driver for 3Com internal PHYs
72  */
73 
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/device.h>
78 #include <sys/malloc.h>
79 #include <sys/socket.h>
80 
81 #include <net/if.h>
82 #include <net/if_media.h>
83 
84 #include <dev/mii/mii.h>
85 #include <dev/mii/miivar.h>
86 #include <dev/mii/miidevs.h>
87 
88 int	exphymatch(struct device *, void *, void *);
89 void	exphyattach(struct device *, struct device *, void *);
90 
91 struct cfattach exphy_ca = {
92 	sizeof(struct mii_softc), exphymatch, exphyattach, mii_phy_detach,
93 	    mii_phy_activate
94 };
95 
96 struct cfdriver exphy_cd = {
97 	NULL, "exphy", DV_DULL
98 };
99 
100 int	exphy_service(struct mii_softc *, struct mii_data *, int);
101 void	exphy_reset(struct mii_softc *);
102 
103 int
104 exphymatch(parent, match, aux)
105 	struct device *parent;
106 	void *match;
107 	void *aux;
108 {
109 	struct mii_attach_args *ma = aux;
110 
111 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_BROADCOM &&
112 	    (MII_MODEL(ma->mii_id2) == MII_MODEL_BROADCOM_3C905B ||
113 	     MII_MODEL(ma->mii_id2) == MII_MODEL_BROADCOM_3C905C))
114 		return (10);
115 
116 	/*
117 	 * Since 3com's PHY for some xl adapters is braindead and doesn't
118 	 * report the proper OUI/MODEL information, we have this stupid
119 	 * match function.
120 	 */
121 	if ((strcmp(parent->dv_cfdata->cf_driver->cd_name, "xl") == 0) &&
122 	    ((MII_OUI(ma->mii_id1, ma->mii_id2) == 0 &&
123 	      MII_MODEL(ma->mii_id2) == 0) ||
124 	     (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_3COM &&
125 	      MII_MODEL(ma->mii_id2) == 0)))
126 		return (10);
127 
128 	return (0);
129 }
130 
131 void
132 exphyattach(parent, self, aux)
133 	struct device *parent, *self;
134 	void *aux;
135 {
136 	struct mii_softc *sc = (struct mii_softc *)self;
137 	struct mii_attach_args *ma = aux;
138 	struct mii_data *mii = ma->mii_data;
139 
140 	if ((MII_OUI(ma->mii_id1, ma->mii_id2) == 0 ||
141 	     MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_3COM) &&
142 	    MII_MODEL(ma->mii_id2) == 0)
143 		printf(": 3Com internal media interface\n");
144 	else if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_BROADCOM &&
145 	    MII_MODEL(ma->mii_id2) == MII_MODEL_BROADCOM_3C905B)
146 		printf(": %s, rev. %d\n", MII_STR_BROADCOM_3C905B,
147 		    MII_REV(ma->mii_id2));
148 	else if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_BROADCOM &&
149 	    MII_MODEL(ma->mii_id2) == MII_MODEL_BROADCOM_3C905C)
150 		printf(": %s, rev. %d\n", MII_STR_BROADCOM_3C905C,
151 		    MII_REV(ma->mii_id2));
152 	else
153 		printf(": unknown phy\n");
154 
155 	sc->mii_inst = mii->mii_instance;
156 	sc->mii_phy = ma->mii_phyno;
157 	sc->mii_service = exphy_service;
158 	sc->mii_status = ukphy_status;
159 	sc->mii_pdata = mii;
160 	sc->mii_flags = mii->mii_flags;
161 
162 	/*
163 	 * The 3Com PHY can never be isolated, so never allow non-zero
164 	 * instances!
165 	 */
166 	if (mii->mii_instance != 0) {
167 		printf("%s: ignoring this PHY, non-zero instance\n",
168 		    sc->mii_dev.dv_xname);
169 		return;
170 	}
171 	sc->mii_flags |= MIIF_NOISOLATE;
172 
173 	exphy_reset(sc);
174 
175 	sc->mii_capabilities =
176 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
177 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
178 		mii_phy_add_media(sc);
179 }
180 
181 int
182 exphy_service(sc, mii, cmd)
183 	struct mii_softc *sc;
184 	struct mii_data *mii;
185 	int cmd;
186 {
187 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
188 
189 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
190 		return (ENXIO);
191 
192 	/*
193 	 * We can't isolate the 3Com PHY, so it has to be the only one!
194 	 */
195 	if (IFM_INST(ife->ifm_media) != sc->mii_inst)
196 		panic("exphy_service: can't isolate 3Com PHY");
197 
198 	switch (cmd) {
199 	case MII_POLLSTAT:
200 		break;
201 
202 	case MII_MEDIACHG:
203 		/*
204 		 * If the interface is not up, don't do anything.
205 		 */
206 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
207 			break;
208 
209 		mii_phy_setmedia(sc);
210 		break;
211 
212 	case MII_TICK:
213 		/*
214 		 * Only used for autonegotiation.
215 		 */
216 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
217 			return (0);
218 
219 		if (mii_phy_tick(sc) == EJUSTRETURN)
220 			return (0);
221 
222 		break;
223 
224 	case MII_DOWN:
225 		mii_phy_down(sc);
226 		return (0);
227 	}
228 
229 	/* Update the media status. */
230 	mii_phy_status(sc);
231 
232 	/* Callback if something changed. */
233 	mii_phy_update(sc, cmd);
234 	return (0);
235 }
236 
237 void
238 exphy_reset(sc)
239 	struct mii_softc *sc;
240 {
241 
242 	mii_phy_reset(sc);
243 
244 	/*
245 	 * XXX 3Com PHY doesn't set the BMCR properly after
246 	 * XXX reset, which breaks autonegotiation.
247 	 */
248 	PHY_WRITE(sc, MII_BMCR, BMCR_S100|BMCR_AUTOEN|BMCR_FDX);
249 }
250