xref: /openbsd-src/sys/dev/mii/sqphy.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: sqphy.c,v 1.17 2008/08/31 09:54:32 jsg Exp $	*/
2 /*	$NetBSD: sqphy.c,v 1.17 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  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by Manuel Bouyer.
48  * 4. The name of the author may not be used to endorse or promote products
49  *    derived from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  */
62 
63 /*
64  * driver for Seeq 80220/80221 and 80223 10/100 ethernet PHYs
65  */
66 
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/device.h>
71 #include <sys/socket.h>
72 #include <sys/errno.h>
73 
74 #include <net/if.h>
75 #include <net/if_media.h>
76 
77 #include <dev/mii/mii.h>
78 #include <dev/mii/miivar.h>
79 #include <dev/mii/miidevs.h>
80 
81 #include <dev/mii/sqphyreg.h>
82 
83 int	sqphymatch(struct device *, void *, void *);
84 void	sqphyattach(struct device *, struct device *, void *);
85 
86 struct cfattach sqphy_ca = {
87 	sizeof(struct mii_softc), sqphymatch, sqphyattach, mii_phy_detach,
88 	    mii_phy_activate
89 };
90 
91 struct cfdriver sqphy_cd = {
92 	NULL, "sqphy", DV_DULL
93 };
94 
95 int	sqphy_service(struct mii_softc *, struct mii_data *, int);
96 void	sqphy_status(struct mii_softc *);
97 
98 const struct mii_phy_funcs sqphy_funcs = {
99 	sqphy_service, sqphy_status, mii_phy_reset,
100 };
101 
102 static const struct mii_phydesc sqphys[] = {
103 	{ MII_OUI_xxSEEQ,		MII_MODEL_xxSEEQ_80220,
104 	  MII_STR_xxSEEQ_80220 },
105 	{ MII_OUI_xxSEEQ,		MII_MODEL_xxSEEQ_80225,
106 	  MII_STR_xxSEEQ_80225 },
107 	{ MII_OUI_xxSEEQ,		MII_MODEL_xxSEEQ_84220,
108 	  MII_STR_xxSEEQ_84220 },
109 
110 	{ 0,				0,
111 	  NULL },
112 };
113 
114 int
115 sqphymatch(struct device *parent, void *match, void *aux)
116 {
117 	struct mii_attach_args *ma = aux;
118 
119 	if (mii_phy_match(ma, sqphys) != NULL)
120 		return (10);
121 
122 	return (0);
123 }
124 
125 void
126 sqphyattach(struct device *parent, struct device *self, void *aux)
127 {
128 	struct mii_softc *sc = (struct mii_softc *)self;
129 	struct mii_attach_args *ma = aux;
130 	struct mii_data *mii = ma->mii_data;
131 	const struct mii_phydesc *mpd;
132 
133 	mpd = mii_phy_match(ma, sqphys);
134 	printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
135 
136 	sc->mii_inst = mii->mii_instance;
137 	sc->mii_phy = ma->mii_phyno;
138 	sc->mii_funcs = &sqphy_funcs;
139 	sc->mii_pdata = mii;
140 	sc->mii_flags = ma->mii_flags;
141 
142 	PHY_RESET(sc);
143 
144 	sc->mii_capabilities =
145 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
146 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
147 		mii_phy_add_media(sc);
148 }
149 
150 int
151 sqphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
152 {
153 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
154 	int reg;
155 
156 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
157 		return (ENXIO);
158 
159 	switch (cmd) {
160 	case MII_POLLSTAT:
161 		/*
162 		 * If we're not polling our PHY instance, just return.
163 		 */
164 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
165 			return (0);
166 		break;
167 
168 	case MII_MEDIACHG:
169 		/*
170 		 * If the media indicates a different PHY instance,
171 		 * isolate ourselves.
172 		 */
173 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
174 			reg = PHY_READ(sc, MII_BMCR);
175 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
176 			return (0);
177 		}
178 
179 		/*
180 		 * If the interface is not up, don't do anything.
181 		 */
182 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
183 			break;
184 
185 		mii_phy_setmedia(sc);
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 		if (mii_phy_tick(sc) == EJUSTRETURN)
196 			return (0);
197 		break;
198 
199 	case MII_DOWN:
200 		mii_phy_down(sc);
201 		return (0);
202 	}
203 
204 	/* Update the media status. */
205 	mii_phy_status(sc);
206 
207 	/* Callback if something changed. */
208 	mii_phy_update(sc, cmd);
209 	return (0);
210 }
211 
212 void
213 sqphy_status(struct mii_softc *sc)
214 {
215 	struct mii_data *mii = sc->mii_pdata;
216 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
217 	int bmsr, bmcr, status;
218 
219 	mii->mii_media_status = IFM_AVALID;
220 	mii->mii_media_active = IFM_ETHER;
221 
222 	bmsr = PHY_READ(sc, MII_BMSR) |
223 	    PHY_READ(sc, MII_BMSR);
224 	if (bmsr & BMSR_LINK)
225 		mii->mii_media_status |= IFM_ACTIVE;
226 
227 	bmcr = PHY_READ(sc, MII_BMCR);
228 	if (bmcr & BMCR_ISO) {
229 		mii->mii_media_active |= IFM_NONE;
230 		mii->mii_media_status = 0;
231 		return;
232 	}
233 
234 	if (bmcr & BMCR_LOOP)
235 		mii->mii_media_active |= IFM_LOOP;
236 
237 	if (bmcr & BMCR_AUTOEN) {
238 		if ((bmsr & BMSR_ACOMP) == 0) {
239 			/* Erg, still trying, I guess... */
240 			mii->mii_media_active |= IFM_NONE;
241 			return;
242 		}
243 		/*
244 		 * Note: don't get fancy here -- the 80225 only
245 		 * supports the SPD_DET and DPLX_DET bits in
246 		 * the STATUS register.
247 		 */
248 		status = PHY_READ(sc, MII_SQPHY_STATUS);
249 		if (status & STATUS_SPD_DET)
250 			mii->mii_media_active |= IFM_100_TX;
251 		else
252 			mii->mii_media_active |= IFM_10_T;
253 
254 		if (status & STATUS_DPLX_DET)
255 			mii->mii_media_active |= IFM_FDX;
256 		else
257 			mii->mii_media_active |= IFM_HDX;
258 	} else
259 		mii->mii_media_active = ife->ifm_media;
260 }
261