xref: /freebsd/sys/dev/mii/lxtphy.c (revision 0957b409)
1 /*	OpenBSD: lxtphy.c,v 1.5 2000/08/26 20:04:17 nate Exp 	*/
2 /*	NetBSD: lxtphy.c,v 1.19 2000/02/02 23:34:57 thorpej Exp 	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6  *
7  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
8  * All rights reserved.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
12  * NASA Ames Research Center.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*-
37  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
52  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58  */
59 
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
62 
63 /*
64  * driver for Level One's LXT-970 ethernet 10/100 PHY
65  * datasheet from www.level1.com
66  */
67 
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/kernel.h>
71 #include <sys/socket.h>
72 #include <sys/errno.h>
73 #include <sys/module.h>
74 #include <sys/bus.h>
75 
76 #include <net/if.h>
77 #include <net/if_media.h>
78 
79 #include <dev/mii/mii.h>
80 #include <dev/mii/miivar.h>
81 #include "miidevs.h"
82 
83 #include <dev/mii/lxtphyreg.h>
84 
85 #include "miibus_if.h"
86 
87 static int lxtphy_probe(device_t);
88 static int lxtphy_attach(device_t);
89 
90 static device_method_t lxtphy_methods[] = {
91 	/* device interface */
92 	DEVMETHOD(device_probe,		lxtphy_probe),
93 	DEVMETHOD(device_attach,	lxtphy_attach),
94 	DEVMETHOD(device_detach,	mii_phy_detach),
95 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
96 	DEVMETHOD_END
97 };
98 
99 static devclass_t lxtphy_devclass;
100 
101 static driver_t lxtphy_driver = {
102 	"lxtphy",
103 	lxtphy_methods,
104 	sizeof(struct mii_softc)
105 };
106 
107 DRIVER_MODULE(lxtphy, miibus, lxtphy_driver, lxtphy_devclass, 0, 0);
108 
109 static int	lxtphy_service(struct mii_softc *, struct mii_data *, int);
110 static void	lxtphy_status(struct mii_softc *);
111 static void	lxtphy_reset(struct mii_softc *);
112 static void	lxtphy_set_tp(struct mii_softc *);
113 static void	lxtphy_set_fx(struct mii_softc *);
114 
115 static const struct mii_phydesc lxtphys[] = {
116 	MII_PHY_DESC(xxLEVEL1, LXT970),
117 	MII_PHY_END
118 };
119 
120 static const struct mii_phy_funcs lxtphy_funcs = {
121 	lxtphy_service,
122 	lxtphy_status,
123 	lxtphy_reset
124 };
125 
126 static int
127 lxtphy_probe(device_t dev)
128 {
129 
130 	return (mii_phy_dev_probe(dev, lxtphys, BUS_PROBE_DEFAULT));
131 }
132 
133 static int
134 lxtphy_attach(device_t dev)
135 {
136 	struct mii_softc *sc;
137 
138 	sc = device_get_softc(dev);
139 
140 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &lxtphy_funcs, 0);
141 
142 	PHY_RESET(sc);
143 
144 	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & sc->mii_capmask;
145 	device_printf(dev, " ");
146 
147 #define	ADD(m)	ifmedia_add(&sc->mii_pdata->mii_media, (m), 0, NULL)
148 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, 0, sc->mii_inst));
149 	printf("100baseFX, ");
150 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, IFM_FDX, sc->mii_inst));
151 	printf("100baseFX-FDX, ");
152 #undef ADD
153 
154 	mii_phy_add_media(sc);
155 	printf("\n");
156 
157 	MIIBUS_MEDIAINIT(sc->mii_dev);
158 	return (0);
159 }
160 
161 static int
162 lxtphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
163 {
164 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
165 
166 	switch (cmd) {
167 	case MII_POLLSTAT:
168 		break;
169 
170 	case MII_MEDIACHG:
171 		if (IFM_SUBTYPE(ife->ifm_media) == IFM_100_FX)
172 			lxtphy_set_fx(sc);
173 		else
174 			lxtphy_set_tp(sc);
175 
176 		mii_phy_setmedia(sc);
177 		break;
178 
179 	case MII_TICK:
180 		if (mii_phy_tick(sc) == EJUSTRETURN)
181 			return (0);
182 		break;
183 	}
184 
185 	/* Update the media status. */
186 	PHY_STATUS(sc);
187 
188 	/* Callback if something changed. */
189 	mii_phy_update(sc, cmd);
190 	return (0);
191 }
192 
193 static void
194 lxtphy_status(struct mii_softc *sc)
195 {
196 	struct mii_data *mii = sc->mii_pdata;
197 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
198 	int bmcr, bmsr, csr;
199 
200 	mii->mii_media_status = IFM_AVALID;
201 	mii->mii_media_active = IFM_ETHER;
202 
203 	/*
204 	 * Get link status from the CSR; we need to read the CSR
205 	 * for media type anyhow, and the link status in the CSR
206 	 * doens't latch, so fewer register reads are required.
207 	 */
208 	csr = PHY_READ(sc, MII_LXTPHY_CSR);
209 	if (csr & CSR_LINK)
210 		mii->mii_media_status |= IFM_ACTIVE;
211 
212 	bmcr = PHY_READ(sc, MII_BMCR);
213 	if (bmcr & BMCR_ISO) {
214 		mii->mii_media_active |= IFM_NONE;
215 		mii->mii_media_status = 0;
216 		return;
217 	}
218 
219 	if (bmcr & BMCR_LOOP)
220 		mii->mii_media_active |= IFM_LOOP;
221 
222 	if (bmcr & BMCR_AUTOEN) {
223 		bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
224 		if ((bmsr & BMSR_ACOMP) == 0) {
225 			/* Erg, still trying, I guess... */
226 			mii->mii_media_active |= IFM_NONE;
227 			return;
228 		}
229 		if (csr & CSR_SPEED)
230 			mii->mii_media_active |= IFM_100_TX;
231 		else
232 			mii->mii_media_active |= IFM_10_T;
233 		if (csr & CSR_DUPLEX)
234 			mii->mii_media_active |=
235 			    IFM_FDX | mii_phy_flowstatus(sc);
236 		else
237 			mii->mii_media_active |= IFM_HDX;
238 	} else
239 		mii->mii_media_active = ife->ifm_media;
240 }
241 
242 static void
243 lxtphy_reset(struct mii_softc *sc)
244 {
245 
246 	mii_phy_reset(sc);
247 	PHY_WRITE(sc, MII_LXTPHY_IER,
248 	    PHY_READ(sc, MII_LXTPHY_IER) & ~IER_INTEN);
249 }
250 
251 static void
252 lxtphy_set_tp(struct mii_softc *sc)
253 {
254 	int cfg;
255 
256 	cfg = PHY_READ(sc, MII_LXTPHY_CONFIG);
257 	cfg &= ~CONFIG_100BASEFX;
258 	PHY_WRITE(sc, MII_LXTPHY_CONFIG, cfg);
259 }
260 
261 static void
262 lxtphy_set_fx(struct mii_softc *sc)
263 {
264 	int cfg;
265 
266 	cfg = PHY_READ(sc, MII_LXTPHY_CONFIG);
267 	cfg |= CONFIG_100BASEFX;
268 	PHY_WRITE(sc, MII_LXTPHY_CONFIG, cfg);
269 }
270