xref: /openbsd/sys/dev/mii/icsphy.c (revision db3296cf)
1 /*	$OpenBSD: icsphy.c,v 1.11 2003/07/09 02:09:08 krw Exp $	*/
2 /*	$NetBSD: icsphy.c,v 1.17 2000/02/02 23:34:56 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  * 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 Integrated Circuit Systems' ICS1890 ethernet 10/100 PHY
72  * and its successor ICS1892
73  * datasheet from www.icst.com
74  */
75 
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/device.h>
80 #include <sys/malloc.h>
81 #include <sys/socket.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/icsphyreg.h>
91 
92 int	icsphymatch(struct device *, void *, void *);
93 void	icsphyattach(struct device *, struct device *, void *);
94 
95 struct cfattach icsphy_ca = {
96 	sizeof(struct mii_softc), icsphymatch, icsphyattach, mii_phy_detach,
97 	    mii_phy_activate
98 };
99 
100 struct cfdriver icsphy_cd = {
101 	NULL, "icsphy", DV_DULL
102 };
103 
104 int	icsphy_service(struct mii_softc *, struct mii_data *, int);
105 void	icsphy_reset(struct mii_softc *);
106 void	icsphy_status(struct mii_softc *);
107 
108 int
109 icsphymatch(parent, match, aux)
110 	struct device *parent;
111 	void *match;
112 	void *aux;
113 {
114 	struct mii_attach_args *ma = aux;
115 
116 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxICS &&
117 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxICS_1890)
118 		return (10);
119 
120         if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxICS &&
121 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxICS_1892)
122 		return (10);
123 
124         if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxICS &&
125 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxICS_1893)
126 		return (10);
127 
128 	return (0);
129 }
130 
131 void
132 icsphyattach(parent, self, aux)
133 	struct device *parent, *self;
134 	void *aux;
135 {
136 	struct mii_softc *sc = (struct mii_softc *)self;
137 	struct mii_attach_args *ma = aux;
138 	struct mii_data *mii = ma->mii_data;
139 	char *model = "?";
140 
141         if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxICS)
142 		switch (MII_MODEL(ma->mii_id2)) {
143 		case MII_MODEL_xxICS_1890:
144 			model = MII_STR_xxICS_1890;
145 			break;
146 		case MII_MODEL_xxICS_1892:
147 			model = MII_STR_xxICS_1892;
148 			break;
149 		case MII_MODEL_xxICS_1893:
150 			model = MII_STR_xxICS_1893;
151 			break;
152 		}
153 
154 	printf(": %s, rev. %d\n", model, MII_REV(ma->mii_id2));
155 
156 	sc->mii_inst = mii->mii_instance;
157 	sc->mii_phy = ma->mii_phyno;
158 	sc->mii_service = icsphy_service;
159 	sc->mii_status = icsphy_status;
160 	sc->mii_pdata = mii;
161 	sc->mii_flags = mii->mii_flags;
162 
163 	icsphy_reset(sc);
164 
165 	sc->mii_capabilities =
166 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
167 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
168 		mii_phy_add_media(sc);
169 }
170 
171 int
172 icsphy_service(sc, mii, cmd)
173 	struct mii_softc *sc;
174 	struct mii_data *mii;
175 	int cmd;
176 {
177 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
178 	int reg;
179 
180 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
181 		return (ENXIO);
182 
183 	switch (cmd) {
184 	case MII_POLLSTAT:
185 		/*
186 		 * If we're not polling our PHY instance, just return.
187 		 */
188 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
189 			return (0);
190 		break;
191 
192 	case MII_MEDIACHG:
193 		/*
194 		 * If the media indicates a different PHY instance,
195 		 * isolate ourselves.
196 		 */
197 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
198 			reg = PHY_READ(sc, MII_BMCR);
199 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
200 			return (0);
201 		}
202 
203 		/*
204 		 * If the interface is not up, don't do anything.
205 		 */
206 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
207 			break;
208 
209 		mii_phy_setmedia(sc);
210 		break;
211 
212 	case MII_TICK:
213 		/*
214 		 * If we're not currently selected, just return.
215 		 */
216 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
217 			return (0);
218 
219 		if (mii_phy_tick(sc) == EJUSTRETURN)
220 			return (0);
221 		break;
222 
223 	case MII_DOWN:
224 		mii_phy_down(sc);
225 		return (0);
226 	}
227 
228 	/* Update the media status. */
229 	mii_phy_status(sc);
230 
231 	/* Callback if something changed. */
232 	mii_phy_update(sc, cmd);
233 	return (0);
234 }
235 
236 void
237 icsphy_status(sc)
238 	struct mii_softc *sc;
239 {
240 	struct mii_data *mii = sc->mii_pdata;
241 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
242 	int bmcr, qpr;
243 
244 	mii->mii_media_status = IFM_AVALID;
245 	mii->mii_media_active = IFM_ETHER;
246 
247 	/*
248 	 * Don't get link from the BMSR.  It's available in the QPR,
249 	 * and we have to read it twice to unlatch it anyhow.  This
250 	 * gives us fewer register reads.
251 	 */
252 	qpr = PHY_READ(sc, MII_ICSPHY_QPR);		/* unlatch */
253 	qpr = PHY_READ(sc, MII_ICSPHY_QPR);		/* real value */
254 
255 	if (qpr & QPR_LINK)
256 		mii->mii_media_status |= IFM_ACTIVE;
257 
258 	bmcr = PHY_READ(sc, MII_BMCR);
259 	if (bmcr & BMCR_ISO) {
260 		mii->mii_media_active |= IFM_NONE;
261 		mii->mii_media_status = 0;
262 		return;
263 	}
264 
265 	if (bmcr & BMCR_LOOP)
266 		mii->mii_media_active |= IFM_LOOP;
267 
268 	if (bmcr & BMCR_AUTOEN) {
269 		if ((qpr & QPR_ACOMP) == 0) {
270 			/* Erg, still trying, I guess... */
271 			mii->mii_media_active |= IFM_NONE;
272 			return;
273 		}
274 		if (qpr & QPR_SPEED)
275 			mii->mii_media_active |= IFM_100_TX;
276 		else
277 			mii->mii_media_active |= IFM_10_T;
278 		if (qpr & QPR_FDX)
279 			mii->mii_media_active |= IFM_FDX;
280 	} else
281 		mii->mii_media_active = ife->ifm_media;
282 }
283 
284 void
285 icsphy_reset(sc)
286 	struct mii_softc *sc;
287 {
288 
289 	mii_phy_reset(sc);
290 	PHY_WRITE(sc, MII_ICSPHY_ECR2, ECR2_10TPROT|ECR2_Q10T);
291 
292 	/*
293 	 * XXX the ICS1892 doesn't set the BMCR properly after
294 	 * XXX reset, which breaks autonegotiation.
295 	 */
296 	PHY_WRITE(sc, MII_BMCR, BMCR_S100|BMCR_AUTOEN|BMCR_FDX);
297 
298 }
299