xref: /openbsd/sys/dev/mii/lxtphy.c (revision 5c7e8fc0)
1 /*	$OpenBSD: lxtphy.c,v 1.4 1999/12/07 22:01:31 jason Exp $	*/
2 /*	$NetBSD: lxtphy.c,v 1.9.6.1 1999/04/23 15:41:43 perry 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.
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 Level One's LXT-970 ethernet 10/100 PHY
72  * datasheet from www.level1.com
73  */
74 
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/kernel.h>
78 #include <sys/device.h>
79 #include <sys/malloc.h>
80 #include <sys/socket.h>
81 #include <sys/errno.h>
82 
83 #include <net/if.h>
84 #include <net/if_media.h>
85 
86 #include <dev/mii/mii.h>
87 #include <dev/mii/miivar.h>
88 #include <dev/mii/miidevs.h>
89 
90 #include <dev/mii/lxtphyreg.h>
91 
92 int	lxtphymatch __P((struct device *, void *, void *));
93 void	lxtphyattach __P((struct device *, struct device *, void *));
94 
95 struct cfattach lxtphy_ca = {
96 	sizeof(struct mii_softc), lxtphymatch, lxtphyattach
97 };
98 
99 struct cfdriver lxtphy_cd = {
100 	NULL, "lxtphy", DV_DULL
101 };
102 
103 int	lxtphy_service __P((struct mii_softc *, struct mii_data *, int));
104 void	lxtphy_status __P((struct mii_softc *));
105 
106 int
107 lxtphymatch(parent, match, aux)
108 	struct device *parent;
109 	void *match;
110 	void *aux;
111 {
112 	struct mii_attach_args *ma = aux;
113 
114 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxLEVEL1 &&
115 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxLEVEL1_LXT970)
116 		return (10);
117 
118 	return (0);
119 }
120 
121 void
122 lxtphyattach(parent, self, aux)
123 	struct device *parent, *self;
124 	void *aux;
125 {
126 	struct mii_softc *sc = (struct mii_softc *)self;
127 	struct mii_attach_args *ma = aux;
128 	struct mii_data *mii = ma->mii_data;
129 
130 	printf(": %s, rev. %d\n", MII_STR_xxLEVEL1_LXT970,
131 	    MII_REV(ma->mii_id2));
132 
133 	sc->mii_inst = mii->mii_instance;
134 	sc->mii_phy = ma->mii_phyno;
135 	sc->mii_service = lxtphy_service;
136 	sc->mii_pdata = mii;
137 
138 	mii_phy_reset(sc);
139 
140 	sc->mii_capabilities =
141 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
142 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
143 		mii_add_media(sc);
144 }
145 
146 int
147 lxtphy_service(sc, mii, cmd)
148 	struct mii_softc *sc;
149 	struct mii_data *mii;
150 	int cmd;
151 {
152 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
153 	int reg;
154 
155 	switch (cmd) {
156 	case MII_POLLSTAT:
157 		/*
158 		 * If we're not polling our PHY instance, just return.
159 		 */
160 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
161 			return (0);
162 		break;
163 
164 	case MII_MEDIACHG:
165 		/*
166 		 * If the media indicates a different PHY instance,
167 		 * isolate ourselves.
168 		 */
169 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
170 			reg = PHY_READ(sc, MII_BMCR);
171 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
172 			return (0);
173 		}
174 
175 		/*
176 		 * If the interface is not up, don't do anything.
177 		 */
178 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
179 			break;
180 
181 		switch (IFM_SUBTYPE(ife->ifm_media)) {
182 		case IFM_AUTO:
183 			/*
184 			 * If we're already in auto mode, just return.
185 			 */
186 			if (PHY_READ(sc, MII_BMCR) & BMCR_AUTOEN)
187 				return (0);
188 			(void) mii_phy_auto(sc, 1);
189 			break;
190 		default:
191 			mii_phy_setmedia(sc);
192 		}
193 		break;
194 
195 	case MII_TICK:
196 		/*
197 		 * If we're not currently selected, just return.
198 		 */
199 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
200 			return (0);
201 
202 		/*
203 		 * Only used for autonegotiation.
204 		 */
205 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
206 			return (0);
207 
208 		/*
209 		 * Is the interface even up?
210 		 */
211 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
212 			return (0);
213 
214 		/*
215 		 * Check to see if we have link.  If we do, we don't
216 		 * need to restart the autonegotiation process.  Use
217 		 * the LXT CSR instead of the BMSR, since the CSR's
218 		 * link indication is dynamic, not latched, so only
219 		 * one register read is required.
220 		 */
221 		reg = PHY_READ(sc, MII_LXTPHY_CSR);
222 		if (reg & CSR_LINK)
223 			return (0);
224 
225 		/*
226 		 * Only retry autonegotiation every 5 seconds.
227 		 */
228 		if (++sc->mii_ticks != 5)
229 			return (0);
230 
231 		sc->mii_ticks = 0;
232 		mii_phy_reset(sc);
233 		if (mii_phy_auto(sc, 0) == EJUSTRETURN)
234 			return (0);
235 		break;
236 
237 	case MII_DOWN:
238 		mii_phy_down(sc);
239 		return (0);
240 	}
241 
242 	/* Update the media status. */
243 	lxtphy_status(sc);
244 
245 	/* Callback if something changed. */
246 	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
247 		(*mii->mii_statchg)(sc->mii_dev.dv_parent);
248 		sc->mii_active = mii->mii_media_active;
249 	}
250 	return (0);
251 }
252 
253 void
254 lxtphy_status(sc)
255 	struct mii_softc *sc;
256 {
257 	struct mii_data *mii = sc->mii_pdata;
258 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
259 	int bmcr, csr;
260 
261 	mii->mii_media_status = IFM_AVALID;
262 	mii->mii_media_active = IFM_ETHER;
263 
264 	/*
265 	 * Get link status from the CSR; we need to read the CSR
266 	 * for media type anyhow, and the link status in the CSR
267 	 * doens't latch, so fewer register reads are required.
268 	 */
269 	csr = PHY_READ(sc, MII_LXTPHY_CSR);
270 	if (csr & CSR_LINK)
271 		mii->mii_media_status |= IFM_ACTIVE;
272 
273 	bmcr = PHY_READ(sc, MII_BMCR);
274 	if (bmcr & BMCR_ISO) {
275 		mii->mii_media_active |= IFM_NONE;
276 		mii->mii_media_status = 0;
277 		return;
278 	}
279 
280 	if (bmcr & BMCR_LOOP)
281 		mii->mii_media_active |= IFM_LOOP;
282 
283 	if (bmcr & BMCR_AUTOEN) {
284 		if ((csr & CSR_ACOMP) == 0) {
285 			/* Erg, still trying, I guess... */
286 			mii->mii_media_active |= IFM_NONE;
287 			return;
288 		}
289 		if (csr & CSR_SPEED)
290 			mii->mii_media_active |= IFM_100_TX;
291 		else
292 			mii->mii_media_active |= IFM_10_T;
293 		if (csr & CSR_DUPLEX)
294 			mii->mii_media_active |= IFM_FDX;
295 	} else
296 		mii->mii_media_active = ife->ifm_media;
297 }
298