1 /*	$OpenBSD: exphy.c,v 1.18 2005/02/04 23:23:56 brad 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/socket.h>
79 
80 #include <net/if.h>
81 #include <net/if_media.h>
82 
83 #include <dev/mii/mii.h>
84 #include <dev/mii/miivar.h>
85 #include <dev/mii/miidevs.h>
86 
87 int	exphymatch(struct device *, void *, void *);
88 void	exphyattach(struct device *, struct device *, void *);
89 
90 struct cfattach exphy_ca = {
91 	sizeof(struct mii_softc), exphymatch, exphyattach, mii_phy_detach,
92 	    mii_phy_activate
93 };
94 
95 struct cfdriver exphy_cd = {
96 	NULL, "exphy", DV_DULL
97 };
98 
99 int	exphy_service(struct mii_softc *, struct mii_data *, int);
100 void	exphy_reset(struct mii_softc *);
101 
102 const struct mii_phy_funcs exphy_funcs = {
103 	exphy_service, ukphy_status, exphy_reset,
104 };
105 
106 int
exphymatch(struct device * parent,void * match,void * aux)107 exphymatch(struct device *parent, void *match, void *aux)
108 {
109 	struct mii_attach_args *ma = aux;
110 
111 	/*
112 	 * Since 3com's PHY for some xl adapters is braindead and doesn't
113 	 * report the proper OUI/MODEL information, we have this stupid
114 	 * match function.
115 	 */
116 	if ((strcmp(parent->dv_cfdata->cf_driver->cd_name, "xl") == 0) &&
117 	    ((MII_OUI(ma->mii_id1, ma->mii_id2) == 0 &&
118 	      MII_MODEL(ma->mii_id2) == 0) ||
119 	     (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_3COM &&
120 	      MII_MODEL(ma->mii_id2) == 0)))
121 		return (10);
122 
123 	return (0);
124 }
125 
126 void
exphyattach(struct device * parent,struct device * self,void * aux)127 exphyattach(struct device *parent, struct device *self, void *aux)
128 {
129 	struct mii_softc *sc = (struct mii_softc *)self;
130 	struct mii_attach_args *ma = aux;
131 	struct mii_data *mii = ma->mii_data;
132 
133 	printf(": 3Com internal media interface\n");
134 
135 	sc->mii_inst = mii->mii_instance;
136 	sc->mii_phy = ma->mii_phyno;
137 	sc->mii_funcs = &exphy_funcs;
138 	sc->mii_pdata = mii;
139 	sc->mii_flags = ma->mii_flags;
140 
141 	sc->mii_flags |= MIIF_NOISOLATE;
142 
143 	/*
144 	 * The 3Com PHY can never be isolated, so never allow non-zero
145 	 * instances!
146 	 */
147 	if (mii->mii_instance != 0) {
148 		printf("%s: ignoring this PHY, non-zero instance\n",
149 		    sc->mii_dev.dv_xname);
150 		return;
151 	}
152 
153 	PHY_RESET(sc);
154 
155 	sc->mii_capabilities =
156 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
157 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
158 		mii_phy_add_media(sc);
159 }
160 
161 int
exphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)162 exphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
163 {
164 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
165 
166 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
167 		return (ENXIO);
168 
169 	/*
170 	 * We can't isolate the 3Com PHY, so it has to be the only one!
171 	 */
172 	if (IFM_INST(ife->ifm_media) != sc->mii_inst)
173 		panic("exphy_service: can't isolate 3Com PHY");
174 
175 	switch (cmd) {
176 	case MII_POLLSTAT:
177 		break;
178 
179 	case MII_MEDIACHG:
180 		/*
181 		 * If the interface is not up, don't do anything.
182 		 */
183 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
184 			break;
185 
186 		mii_phy_setmedia(sc);
187 		break;
188 
189 	case MII_TICK:
190 		if (mii_phy_tick(sc) == EJUSTRETURN)
191 			return (0);
192 
193 		break;
194 
195 	case MII_DOWN:
196 		mii_phy_down(sc);
197 		return (0);
198 	}
199 
200 	/* Update the media status. */
201 	mii_phy_status(sc);
202 
203 	/* Callback if something changed. */
204 	mii_phy_update(sc, cmd);
205 	return (0);
206 }
207 
208 void
exphy_reset(struct mii_softc * sc)209 exphy_reset(struct mii_softc *sc)
210 {
211 
212 	mii_phy_reset(sc);
213 
214 	/*
215 	 * XXX 3Com PHY doesn't set the BMCR properly after
216 	 * XXX reset, which breaks autonegotiation.
217 	 */
218 	PHY_WRITE(sc, MII_BMCR, BMCR_S100|BMCR_AUTOEN|BMCR_FDX);
219 }
220