xref: /openbsd/sys/dev/mii/lxtphy.c (revision 471aeecf)
1 /*	$OpenBSD: lxtphy.c,v 1.23 2022/04/06 18:59:29 naddy Exp $	*/
2 /*	$NetBSD: lxtphy.c,v 1.19 2000/02/02 23:34:57 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.
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 Level One's LXT-970/971 ethernet 10/100 PHY
60  * datasheet from www.level1.com
61  */
62 
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/device.h>
66 #include <sys/socket.h>
67 #include <sys/errno.h>
68 
69 #include <net/if.h>
70 #include <net/if_var.h>
71 #include <net/if_media.h>
72 
73 #include <dev/mii/mii.h>
74 #include <dev/mii/miivar.h>
75 #include <dev/mii/miidevs.h>
76 
77 #include <dev/mii/lxtphyreg.h>
78 
79 int	lxtphymatch(struct device *, void *, void *);
80 void	lxtphyattach(struct device *, struct device *, void *);
81 
82 const struct cfattach lxtphy_ca = {
83 	sizeof(struct mii_softc), lxtphymatch, lxtphyattach, mii_phy_detach
84 };
85 
86 struct cfdriver lxtphy_cd = {
87 	NULL, "lxtphy", DV_DULL
88 };
89 
90 int	lxtphy_service(struct mii_softc *, struct mii_data *, int);
91 void	lxtphy_status(struct mii_softc *);
92 void	lxtphy_reset(struct mii_softc *);
93 
94 const struct mii_phy_funcs lxtphy_funcs = {
95 	lxtphy_service, lxtphy_status, lxtphy_reset,
96 };
97 
98 const struct mii_phy_funcs lxtphy971_funcs = {
99 	lxtphy_service, ukphy_status, lxtphy_reset,
100 };
101 
102 static const struct mii_phydesc lxtphys[] = {
103 	{ MII_OUI_xxLEVEL1,		MII_MODEL_xxLEVEL1_LXT970,
104 	  MII_STR_xxLEVEL1_LXT970 },
105 	{ MII_OUI_xxLEVEL1a,		MII_MODEL_xxLEVEL1a_LXT971,
106 	  MII_STR_xxLEVEL1a_LXT971 },
107 
108 	{ 0,			0,
109 	  NULL },
110 };
111 
112 int
lxtphymatch(struct device * parent,void * match,void * aux)113 lxtphymatch(struct device *parent, void *match, void *aux)
114 {
115 	struct mii_attach_args *ma = aux;
116 
117 	if (mii_phy_match(ma, lxtphys) != NULL)
118 		return (10);
119 
120 	return (0);
121 }
122 
123 void
lxtphyattach(struct device * parent,struct device * self,void * aux)124 lxtphyattach(struct device *parent, struct device *self, 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 	const struct mii_phydesc *mpd;
130 
131 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxLEVEL1 &&
132 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxLEVEL1_LXT970) {
133 		sc->mii_funcs = &lxtphy_funcs;
134 	}
135 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxLEVEL1a &&
136 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxLEVEL1a_LXT971) {
137 		sc->mii_funcs = &lxtphy971_funcs;
138 	}
139 
140 	mpd = mii_phy_match(ma, lxtphys);
141 	printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
142 
143 	sc->mii_inst = mii->mii_instance;
144 	sc->mii_phy = ma->mii_phyno;
145 	sc->mii_pdata = mii;
146 	sc->mii_flags = ma->mii_flags;
147 
148 	PHY_RESET(sc);
149 
150 	sc->mii_capabilities =
151 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
152 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
153 		mii_phy_add_media(sc);
154 }
155 
156 int
lxtphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)157 lxtphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
158 {
159 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
160 	int reg;
161 
162 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
163 		return (ENXIO);
164 
165 	switch (cmd) {
166 	case MII_POLLSTAT:
167 		/*
168 		 * If we're not polling our PHY instance, just return.
169 		 */
170 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
171 			return (0);
172 		break;
173 
174 	case MII_MEDIACHG:
175 		/*
176 		 * If the media indicates a different PHY instance,
177 		 * isolate ourselves.
178 		 */
179 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
180 			reg = PHY_READ(sc, MII_BMCR);
181 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
182 			return (0);
183 		}
184 
185 		/*
186 		 * If the interface is not up, don't do anything.
187 		 */
188 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
189 			break;
190 
191 		mii_phy_setmedia(sc);
192 		break;
193 
194 	case MII_TICK:
195 		/*
196 		 * If we're not currently selected, just return.
197 		 */
198 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
199 			return (0);
200 
201 		if (mii_phy_tick(sc) == EJUSTRETURN)
202 			return (0);
203 		break;
204 
205 	case MII_DOWN:
206 		mii_phy_down(sc);
207 		return (0);
208 	}
209 
210 	/* Update the media status. */
211 	mii_phy_status(sc);
212 
213 	/* Callback if something changed. */
214 	mii_phy_update(sc, cmd);
215 	return (0);
216 }
217 
218 void
lxtphy_status(struct mii_softc * sc)219 lxtphy_status(struct mii_softc *sc)
220 {
221 	struct mii_data *mii = sc->mii_pdata;
222 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
223 	int bmcr, bmsr, csr;
224 
225 	mii->mii_media_status = IFM_AVALID;
226 	mii->mii_media_active = IFM_ETHER;
227 
228 	/*
229 	 * Get link status from the CSR; we need to read the CSR
230 	 * for media type anyhow, and the link status in the CSR
231 	 * doesn't latch, so fewer register reads are required.
232 	 */
233 	csr = PHY_READ(sc, MII_LXTPHY_CSR);
234 	if (csr & CSR_LINK)
235 		mii->mii_media_status |= IFM_ACTIVE;
236 
237 	bmcr = PHY_READ(sc, MII_BMCR);
238 	if (bmcr & BMCR_ISO) {
239 		mii->mii_media_active |= IFM_NONE;
240 		mii->mii_media_status = 0;
241 		return;
242 	}
243 
244 	if (bmcr & BMCR_LOOP)
245 		mii->mii_media_active |= IFM_LOOP;
246 
247 	if (bmcr & BMCR_AUTOEN) {
248 		bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
249 		if ((bmsr & BMSR_ACOMP) == 0) {
250 			/* Erg, still trying, I guess... */
251 			mii->mii_media_active |= IFM_NONE;
252 			return;
253 		}
254 		if (csr & CSR_SPEED)
255 			mii->mii_media_active |= IFM_100_TX;
256 		else
257 			mii->mii_media_active |= IFM_10_T;
258 
259 		if (csr & CSR_DUPLEX)
260 			mii->mii_media_active |= IFM_FDX;
261 		else
262 			mii->mii_media_active |= IFM_HDX;
263 	} else
264 		mii->mii_media_active = ife->ifm_media;
265 }
266 
267 void
lxtphy_reset(struct mii_softc * sc)268 lxtphy_reset(struct mii_softc *sc)
269 {
270 	mii_phy_reset(sc);
271 	PHY_WRITE(sc, MII_LXTPHY_IER,
272 	    PHY_READ(sc, MII_LXTPHY_IER) & ~IER_INTEN);
273 }
274