xref: /openbsd/sys/dev/mii/xmphy.c (revision da5607f6)
1 /*	$OpenBSD: xmphy.c,v 1.26 2024/06/26 01:40:49 jsg Exp $	*/
2 
3 /*
4  * Copyright (c) 2000
5  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $FreeBSD: src/sys/dev/mii/xmphy.c,v 1.1 2000/04/22 01:58:18 wpaul Exp $
35  */
36 
37 /*
38  * driver for the XaQti XMAC II's internal PHY. This is sort of
39  * like a 10/100 PHY, except the only thing we're really autoselecting
40  * here is full/half duplex. Speed is always 1000mbps.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 #include <sys/socket.h>
47 #include <sys/errno.h>
48 
49 #include <net/if.h>
50 #include <net/if_var.h>
51 #include <net/if_media.h>
52 
53 #include <dev/mii/mii.h>
54 #include <dev/mii/miivar.h>
55 #include <dev/mii/miidevs.h>
56 
57 int xmphy_probe(struct device *, void *, void *);
58 void xmphy_attach(struct device *, struct device *, void *);
59 
60 const struct cfattach xmphy_ca = {
61 	sizeof(struct mii_softc), xmphy_probe, xmphy_attach, mii_phy_detach
62 };
63 
64 struct cfdriver xmphy_cd = {
65 	NULL, "xmphy", DV_DULL
66 };
67 
68 int	xmphy_service(struct mii_softc *, struct mii_data *, int);
69 void	xmphy_status(struct mii_softc *);
70 
71 int	xmphy_mii_phy_auto(struct mii_softc *);
72 
73 const struct mii_phy_funcs xmphy_funcs = {
74 	xmphy_service, xmphy_status, mii_phy_reset,
75 };
76 
77 static const struct mii_phydesc xmphys[] = {
78 	{ MII_OUI_xxXAQTI,	MII_MODEL_XAQTI_XMACII,
79 	  MII_STR_XAQTI_XMACII },
80 
81 	{ MII_OUI_JATO,		MII_MODEL_JATO_BASEX,
82 	  MII_STR_JATO_BASEX },
83 
84 	{ 0,			0,
85 	  NULL },
86 };
87 
88 int
xmphy_probe(struct device * parent,void * match,void * aux)89 xmphy_probe(struct device *parent, void *match, void *aux)
90 {
91 	struct mii_attach_args *ma = aux;
92 
93 	if (mii_phy_match(ma, xmphys) != NULL)
94 		return (10);
95 
96 	return (0);
97 }
98 
99 void
xmphy_attach(struct device * parent,struct device * self,void * aux)100 xmphy_attach(struct device *parent, struct device *self, void *aux)
101 {
102 	struct mii_softc *sc = (struct mii_softc *)self;
103 	struct mii_attach_args *ma = aux;
104 	struct mii_data *mii = ma->mii_data;
105 	const struct mii_phydesc *mpd;
106 
107 	mpd = mii_phy_match(ma, xmphys);
108 	printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
109 
110 	sc->mii_inst = mii->mii_instance;
111 	sc->mii_phy = ma->mii_phyno;
112 	sc->mii_funcs = &xmphy_funcs;
113 	sc->mii_pdata = mii;
114 	sc->mii_flags = ma->mii_flags;
115 	sc->mii_anegticks = MII_ANEGTICKS;
116 
117 	sc->mii_flags |= MIIF_NOISOLATE;
118 
119 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
120 
121 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
122 	    BMCR_ISO);
123 
124 	PHY_RESET(sc);
125 
126 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0, sc->mii_inst),
127 	    BMCR_FDX);
128 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, sc->mii_inst), 0);
129 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, sc->mii_inst), 0);
130 
131 #undef ADD
132 }
133 
134 int
xmphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)135 xmphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
136 {
137 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
138 	int reg;
139 
140 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
141 		return (ENXIO);
142 
143 	switch (cmd) {
144 	case MII_POLLSTAT:
145 		/*
146 		 * If we're not polling our PHY instance, just return.
147 		 */
148 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
149 			return (0);
150 		break;
151 
152 	case MII_MEDIACHG:
153 		/*
154 		 * If the media indicates a different PHY instance,
155 		 * isolate ourselves.
156 		 */
157 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
158 			reg = PHY_READ(sc, MII_BMCR);
159 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
160 			return (0);
161 		}
162 
163 		/*
164 		 * If the interface is not up, don't do anything.
165 		 */
166 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
167 			break;
168 
169 		switch (IFM_SUBTYPE(ife->ifm_media)) {
170 		case IFM_AUTO:
171 			(void) xmphy_mii_phy_auto(sc);
172 			break;
173 		case IFM_1000_SX:
174 			PHY_RESET(sc);
175 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX) {
176 				PHY_WRITE(sc, MII_ANAR, ANAR_10_FD);
177 				PHY_WRITE(sc, MII_BMCR, BMCR_FDX);
178 			} else {
179 				PHY_WRITE(sc, MII_ANAR, ANAR_10);
180 				PHY_WRITE(sc, MII_BMCR, 0);
181 			}
182 			break;
183 		default:
184 			return (EINVAL);
185 		}
186 		break;
187 
188 	case MII_TICK:
189 		/*
190 		 * If we're not currently selected, just return.
191 		 */
192 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
193 			return (0);
194 
195 		/*
196 		 * Is the interface even up?
197 		 */
198 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
199 			return (0);
200 
201 		/*
202 		 * Only used for autonegotiation.
203 		 */
204 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
205 			break;
206 
207                 /*
208                  * Check to see if we have link.  If we do, we don't
209                  * need to restart the autonegotiation process.  Read
210                  * the BMSR twice in case it's latched.
211                  */
212 		reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
213 		if (reg & BMSR_LINK) {
214 			sc->mii_ticks = 0;
215 			break;
216 		}
217 
218 		/*
219 		 * Only retry autonegotiation every mii_anegticks seconds.
220 		 */
221 		if (++sc->mii_ticks <= sc->mii_anegticks)
222 			break;
223 
224 		sc->mii_ticks = 0;
225 		PHY_RESET(sc);
226 
227 		xmphy_mii_phy_auto(sc);
228 		break;
229 	}
230 
231 	/* Update the media status. */
232 	mii_phy_status(sc);
233 
234 	/* Callback if something changed. */
235 	mii_phy_update(sc, cmd);
236 	return (0);
237 }
238 
239 void
xmphy_status(struct mii_softc * sc)240 xmphy_status(struct mii_softc *sc)
241 {
242 	struct mii_data *mii = sc->mii_pdata;
243 	int bmsr, bmcr, anlpar;
244 
245 	mii->mii_media_status = IFM_AVALID;
246 	mii->mii_media_active = IFM_ETHER;
247 
248 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
249 	if (bmsr & BMSR_LINK)
250 		mii->mii_media_status |= IFM_ACTIVE;
251 
252 	/* Do dummy read of extended status register. */
253 	bmcr = PHY_READ(sc, MII_EXTSR);
254 
255 	bmcr = PHY_READ(sc, MII_BMCR);
256 
257 	if (bmcr & BMCR_LOOP)
258 		mii->mii_media_active |= IFM_LOOP;
259 
260 
261 	if (bmcr & BMCR_AUTOEN) {
262 		if ((bmsr & BMSR_ACOMP) == 0) {
263 			if (bmsr & BMSR_LINK) {
264 				mii->mii_media_active |= IFM_1000_SX|IFM_HDX;
265 				return;
266 			}
267 			/* Erg, still trying, I guess... */
268 			mii->mii_media_active |= IFM_NONE;
269 			return;
270 		}
271 
272 		mii->mii_media_active |= IFM_1000_SX;
273 		anlpar = PHY_READ(sc, MII_ANAR) & PHY_READ(sc, MII_ANLPAR);
274 		if (anlpar & ANLPAR_10_FD)
275 			mii->mii_media_active |= IFM_FDX;
276 		else
277 			mii->mii_media_active |= IFM_HDX;
278 		return;
279 	}
280 
281 	mii->mii_media_active |= IFM_1000_SX;
282 	if (bmcr & BMCR_FDX)
283 		mii->mii_media_active |= IFM_FDX;
284 	else
285 		mii->mii_media_active |= IFM_HDX;
286 
287 	return;
288 }
289 
290 
291 int
xmphy_mii_phy_auto(struct mii_softc * sc)292 xmphy_mii_phy_auto(struct mii_softc *sc)
293 {
294 	int anar = 0;
295 
296 	anar = PHY_READ(sc, MII_ANAR);
297 	anar |= ANAR_10_FD|ANAR_10;
298 	PHY_WRITE(sc, MII_ANAR, anar);
299 	DELAY(1000);
300 	PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
301 
302 	return (EJUSTRETURN);
303 }
304