xref: /freebsd/sys/dev/mii/rlphy.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /*
2  * Copyright (c) 1997, 1998, 1999
3  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34 
35 /*
36  * driver for RealTek 8139 internal PHYs
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/socket.h>
43 #include <sys/bus.h>
44 
45 #include <net/if.h>
46 #include <net/if_media.h>
47 
48 #include <dev/mii/mii.h>
49 #include <dev/mii/miivar.h>
50 
51 #include "miibus_if.h"
52 
53 #if !defined(lint)
54 static const char rcsid[] =
55    "$FreeBSD$";
56 #endif
57 
58 static int rlphy_probe		__P((device_t));
59 static int rlphy_attach		__P((device_t));
60 static int rlphy_detach		__P((device_t));
61 
62 static device_method_t rlphy_methods[] = {
63 	/* device interface */
64 	DEVMETHOD(device_probe,		rlphy_probe),
65 	DEVMETHOD(device_attach,	rlphy_attach),
66 	DEVMETHOD(device_detach,	rlphy_detach),
67 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
68 	{ 0, 0 }
69 };
70 
71 static devclass_t rlphy_devclass;
72 
73 static driver_t rlphy_driver = {
74 	"rlphy",
75 	rlphy_methods,
76 	sizeof(struct mii_softc)
77 };
78 
79 DRIVER_MODULE(rlphy, miibus, rlphy_driver, rlphy_devclass, 0, 0);
80 
81 int	rlphy_service __P((struct mii_softc *, struct mii_data *, int));
82 void	rlphy_reset __P((struct mii_softc *));
83 
84 static int rlphy_probe(dev)
85 	device_t		dev;
86 {
87 	struct mii_attach_args *ma;
88 	device_t		parent;
89 
90 	ma = device_get_ivars(dev);
91 	parent = device_get_parent(device_get_parent(dev));
92 
93 	/*
94 	 * RealTek PHY doesn't have vendor/device ID registers:
95 	 * the rl driver fakes up a return value of all zeros.
96 	 */
97 	if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 ||
98 	    MII_MODEL(ma->mii_id2) != 0)
99 		return (ENXIO);
100 
101 	/*
102 	 * Make sure the parent is an `rl'.
103 	 */
104 	if (strcmp(device_get_name(parent), "rl") != 0)
105 		return (ENXIO);
106 
107 	device_set_desc(dev, "RealTek internal media interface");
108 
109 	return (0);
110 }
111 
112 static int rlphy_attach(dev)
113 	device_t		dev;
114 {
115 	struct mii_softc	*sc;
116 	struct mii_attach_args	*ma;
117 	struct mii_data		*mii;
118 
119 	sc = device_get_softc(dev);
120 	ma = device_get_ivars(dev);
121 	sc->mii_dev = device_get_parent(dev);
122 	mii = device_get_softc(sc->mii_dev);
123 
124 	/*
125 	 * The RealTek PHY can never be isolated, so never allow non-zero
126 	 * instances!
127 	 */
128 	if (mii->mii_instance != 0) {
129 		device_printf(dev, "ignoring this PHY, non-zero instance\n");
130 		return(ENXIO);
131 	}
132 
133 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
134 
135 	sc->mii_inst = mii->mii_instance;
136 	sc->mii_phy = ma->mii_phyno;
137 	sc->mii_service = rlphy_service;
138 	sc->mii_pdata = mii;
139 	mii->mii_instance++;
140 
141 	sc->mii_flags |= MIIF_NOISOLATE;
142 
143 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
144 
145 #if 0 /* See above. */
146 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
147 	    BMCR_ISO);
148 #endif
149 
150 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
151 	    BMCR_LOOP|BMCR_S100);
152 
153 	rlphy_reset(sc);
154 
155 	sc->mii_capabilities =
156 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
157 	device_printf(dev, " ");
158 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
159 		printf("no media present");
160 	else
161 		mii_add_media(mii, sc->mii_capabilities,
162 		    sc->mii_inst);
163 	printf("\n");
164 #undef ADD
165 	MIIBUS_MEDIAINIT(sc->mii_dev);
166 	return(0);
167 }
168 
169 static int rlphy_detach(dev)
170 	device_t		dev;
171 {
172 	struct mii_softc	*sc;
173 	struct mii_data		*mii;
174 
175 	sc = device_get_softc(dev);
176 	mii = device_get_softc(device_get_softc(dev));
177 	sc->mii_dev = NULL;
178 	LIST_REMOVE(sc, mii_list);
179 
180 	return(0);
181 }
182 
183 int
184 rlphy_service(sc, mii, cmd)
185 	struct mii_softc *sc;
186 	struct mii_data *mii;
187 	int cmd;
188 {
189 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
190 
191 	/*
192 	 * We can't isolate the RealTek PHY, so it has to be the only one!
193 	 */
194 	if (IFM_INST(ife->ifm_media) != sc->mii_inst)
195 		panic("rlphy_service: can't isolate RealTek PHY");
196 
197 	switch (cmd) {
198 	case MII_POLLSTAT:
199 		break;
200 
201 	case MII_MEDIACHG:
202 		/*
203 		 * If the interface is not up, don't do anything.
204 		 */
205 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
206 			break;
207 
208 		switch (IFM_SUBTYPE(ife->ifm_media)) {
209 		case IFM_AUTO:
210 			/*
211 			 * If we're already in auto mode, just return.
212 			 */
213 			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
214 				return (0);
215 			(void) mii_phy_auto(sc, 0);
216 			break;
217 		case IFM_100_T4:
218 			/*
219 			 * XXX Not supported as a manual setting right now.
220 			 */
221 			return (EINVAL);
222 		default:
223 			/*
224 			 * BMCR data is stored in the ifmedia entry.
225 			 */
226 			PHY_WRITE(sc, MII_ANAR,
227 			    mii_anar(ife->ifm_media));
228 			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
229 		}
230 		break;
231 
232 	case MII_TICK:
233 		/*
234 		 * Only used for autonegotiation.
235 		 */
236 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
237 			return (0);
238 
239 		/*
240 		 * Is the interface even up?
241 		 */
242 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
243 			return (0);
244 
245 		/*
246 		 * Only retry autonegotiation every 5 seconds.
247 		 */
248 		if (++sc->mii_ticks != 5)
249 			return (0);
250 
251 		sc->mii_ticks = 0;
252 
253 		/*
254 		 * The RealTek PHY's autonegotiation doesn't need to be
255 		 * kicked; it continues in the background.
256 		 */
257 		break;
258 	}
259 
260 	/* Update the media status. */
261 	ukphy_status(sc);
262 
263 	/* Callback if something changed. */
264 	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
265 		MIIBUS_STATCHG(sc->mii_dev);
266 		sc->mii_active = mii->mii_media_active;
267 	}
268 	return (0);
269 }
270 
271 void
272 rlphy_reset(sc)
273 	struct mii_softc *sc;
274 {
275 
276 	mii_phy_reset(sc);
277 
278 	/*
279 	 * XXX RealTek PHY doesn't set the BMCR properly after
280 	 * XXX reset, which breaks autonegotiation.
281 	 */
282 	PHY_WRITE(sc, MII_BMCR, BMCR_S100|BMCR_AUTOEN|BMCR_FDX);
283 }
284