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