xref: /openbsd-src/sys/dev/mii/icsphy.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: icsphy.c,v 1.7 2001/06/03 15:54:44 deraadt 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 __P((struct device *, void *, void *));
93 void	icsphyattach __P((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 __P((struct mii_softc *, struct mii_data *, int));
105 void	icsphy_reset __P((struct mii_softc *));
106 void	icsphy_status __P((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 	return (0);
125 }
126 
127 void
128 icsphyattach(parent, self, aux)
129 	struct device *parent, *self;
130 	void *aux;
131 {
132 	struct mii_softc *sc = (struct mii_softc *)self;
133 	struct mii_attach_args *ma = aux;
134 	struct mii_data *mii = ma->mii_data;
135 
136         if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxICS &&
137 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxICS_1890)
138 	       	printf(": %s, rev. %d\n", MII_STR_xxICS_1890,
139 	          MII_REV(ma->mii_id2));
140 
141         if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxICS &&
142 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxICS_1892)
143 	       	printf(": %s, rev. %d\n", MII_STR_xxICS_1892,
144 	          MII_REV(ma->mii_id2));
145 
146 	sc->mii_inst = mii->mii_instance;
147 	sc->mii_phy = ma->mii_phyno;
148 	sc->mii_service = icsphy_service;
149 	sc->mii_status = icsphy_status;
150 	sc->mii_pdata = mii;
151 	sc->mii_flags = mii->mii_flags;
152 
153 	icsphy_reset(sc);
154 
155 	sc->mii_capabilities =
156 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
157 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
158 		mii_phy_add_media(sc);
159 }
160 
161 int
162 icsphy_service(sc, mii, cmd)
163 	struct mii_softc *sc;
164 	struct mii_data *mii;
165 	int cmd;
166 {
167 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
168 	int reg;
169 
170 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
171 		return (ENXIO);
172 
173 	switch (cmd) {
174 	case MII_POLLSTAT:
175 		/*
176 		 * If we're not polling our PHY instance, just return.
177 		 */
178 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
179 			return (0);
180 		break;
181 
182 	case MII_MEDIACHG:
183 		/*
184 		 * If the media indicates a different PHY instance,
185 		 * isolate ourselves.
186 		 */
187 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
188 			reg = PHY_READ(sc, MII_BMCR);
189 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
190 			return (0);
191 		}
192 
193 		/*
194 		 * If the interface is not up, don't do anything.
195 		 */
196 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
197 			break;
198 
199 		mii_phy_setmedia(sc);
200 		break;
201 
202 	case MII_TICK:
203 		/*
204 		 * If we're not currently selected, just return.
205 		 */
206 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
207 			return (0);
208 
209 		if (mii_phy_tick(sc) == EJUSTRETURN)
210 			return (0);
211 		break;
212 
213 	case MII_DOWN:
214 		mii_phy_down(sc);
215 		return (0);
216 	}
217 
218 	/* Update the media status. */
219 	mii_phy_status(sc);
220 
221 	/* Callback if something changed. */
222 	mii_phy_update(sc, cmd);
223 	return (0);
224 }
225 
226 void
227 icsphy_status(sc)
228 	struct mii_softc *sc;
229 {
230 	struct mii_data *mii = sc->mii_pdata;
231 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
232 	int bmcr, qpr;
233 
234 	mii->mii_media_status = IFM_AVALID;
235 	mii->mii_media_active = IFM_ETHER;
236 
237 	/*
238 	 * Don't get link from the BMSR.  It's available in the QPR,
239 	 * and we have to read it twice to unlatch it anyhow.  This
240 	 * gives us fewer register reads.
241 	 */
242 	qpr = PHY_READ(sc, MII_ICSPHY_QPR);		/* unlatch */
243 	qpr = PHY_READ(sc, MII_ICSPHY_QPR);		/* real value */
244 
245 	if (qpr & QPR_LINK)
246 		mii->mii_media_status |= IFM_ACTIVE;
247 
248 	bmcr = PHY_READ(sc, MII_BMCR);
249 	if (bmcr & BMCR_ISO) {
250 		mii->mii_media_active |= IFM_NONE;
251 		mii->mii_media_status = 0;
252 		return;
253 	}
254 
255 	if (bmcr & BMCR_LOOP)
256 		mii->mii_media_active |= IFM_LOOP;
257 
258 	if (bmcr & BMCR_AUTOEN) {
259 		if ((qpr & QPR_ACOMP) == 0) {
260 			/* Erg, still trying, I guess... */
261 			mii->mii_media_active |= IFM_NONE;
262 			return;
263 		}
264 		if (qpr & QPR_SPEED)
265 			mii->mii_media_active |= IFM_100_TX;
266 		else
267 			mii->mii_media_active |= IFM_10_T;
268 		if (qpr & QPR_FDX)
269 			mii->mii_media_active |= IFM_FDX;
270 	} else
271 		mii->mii_media_active = ife->ifm_media;
272 }
273 
274 void
275 icsphy_reset(sc)
276 	struct mii_softc *sc;
277 {
278 
279 	mii_phy_reset(sc);
280 	PHY_WRITE(sc, MII_ICSPHY_ECR2, ECR2_10TPROT|ECR2_Q10T);
281 
282 	/*
283 	 * XXX the ICS1892 doesn't set the BMCR properly after
284 	 * XXX reset, which breaks autonegotiation.
285 	 */
286 	PHY_WRITE(sc, MII_BMCR, BMCR_S100|BMCR_AUTOEN|BMCR_FDX);
287 
288 }
289