xref: /openbsd/sys/dev/mii/ukphy.c (revision c43900ff)
1*c43900ffSbrad /*	$OpenBSD: ukphy.c,v 1.13 2004/09/27 18:25:48 brad Exp $	*/
25d6e3d42Snate /*	$NetBSD: ukphy.c,v 1.9 2000/02/02 23:34:57 thorpej Exp $	*/
3bf11b014Sjason 
4bf11b014Sjason /*-
5b2e83a74Sjason  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
6bf11b014Sjason  * All rights reserved.
7bf11b014Sjason  *
8bf11b014Sjason  * This code is derived from software contributed to The NetBSD Foundation
9bf11b014Sjason  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10bf11b014Sjason  * NASA Ames Research Center, and by Frank van der Linden.
11bf11b014Sjason  *
12bf11b014Sjason  * Redistribution and use in source and binary forms, with or without
13bf11b014Sjason  * modification, are permitted provided that the following conditions
14bf11b014Sjason  * are met:
15bf11b014Sjason  * 1. Redistributions of source code must retain the above copyright
16bf11b014Sjason  *    notice, this list of conditions and the following disclaimer.
17bf11b014Sjason  * 2. Redistributions in binary form must reproduce the above copyright
18bf11b014Sjason  *    notice, this list of conditions and the following disclaimer in the
19bf11b014Sjason  *    documentation and/or other materials provided with the distribution.
20bf11b014Sjason  * 3. All advertising materials mentioning features or use of this software
21bf11b014Sjason  *    must display the following acknowledgement:
22bf11b014Sjason  *	This product includes software developed by the NetBSD
23bf11b014Sjason  *	Foundation, Inc. and its contributors.
24bf11b014Sjason  * 4. Neither the name of The NetBSD Foundation nor the names of its
25bf11b014Sjason  *    contributors may be used to endorse or promote products derived
26bf11b014Sjason  *    from this software without specific prior written permission.
27bf11b014Sjason  *
28bf11b014Sjason  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29bf11b014Sjason  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30bf11b014Sjason  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31bf11b014Sjason  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32bf11b014Sjason  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33bf11b014Sjason  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34bf11b014Sjason  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35bf11b014Sjason  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36bf11b014Sjason  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37bf11b014Sjason  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38bf11b014Sjason  * POSSIBILITY OF SUCH DAMAGE.
39bf11b014Sjason  */
40bf11b014Sjason 
41bf11b014Sjason /*
42bf11b014Sjason  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
43bf11b014Sjason  *
44bf11b014Sjason  * Redistribution and use in source and binary forms, with or without
45bf11b014Sjason  * modification, are permitted provided that the following conditions
46bf11b014Sjason  * are met:
47bf11b014Sjason  * 1. Redistributions of source code must retain the above copyright
48bf11b014Sjason  *    notice, this list of conditions and the following disclaimer.
49bf11b014Sjason  * 2. Redistributions in binary form must reproduce the above copyright
50bf11b014Sjason  *    notice, this list of conditions and the following disclaimer in the
51bf11b014Sjason  *    documentation and/or other materials provided with the distribution.
52bf11b014Sjason  * 3. All advertising materials mentioning features or use of this software
53bf11b014Sjason  *    must display the following acknowledgement:
54bf11b014Sjason  *	This product includes software developed by Manuel Bouyer.
55bf11b014Sjason  * 4. The name of the author may not be used to endorse or promote products
56bf11b014Sjason  *    derived from this software without specific prior written permission.
57bf11b014Sjason  *
58bf11b014Sjason  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
59bf11b014Sjason  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60bf11b014Sjason  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61bf11b014Sjason  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
62bf11b014Sjason  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
63bf11b014Sjason  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64bf11b014Sjason  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65bf11b014Sjason  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66bf11b014Sjason  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
67bf11b014Sjason  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68bf11b014Sjason  */
69bf11b014Sjason 
70bf11b014Sjason /*
71bf11b014Sjason  * driver for generic unknown PHYs
72bf11b014Sjason  */
73bf11b014Sjason 
74bf11b014Sjason #include <sys/param.h>
75bf11b014Sjason #include <sys/systm.h>
76bf11b014Sjason #include <sys/kernel.h>
77bf11b014Sjason #include <sys/device.h>
78bf11b014Sjason #include <sys/socket.h>
79b2e83a74Sjason #include <sys/errno.h>
80bf11b014Sjason 
81bf11b014Sjason #include <net/if.h>
82bf11b014Sjason #include <net/if_media.h>
83bf11b014Sjason 
84bf11b014Sjason #include <dev/mii/mii.h>
85bf11b014Sjason #include <dev/mii/miivar.h>
86bf11b014Sjason 
87c4071fd1Smillert int	ukphymatch(struct device *, void *, void *);
88c4071fd1Smillert void	ukphyattach(struct device *, struct device *, void *);
89bf11b014Sjason 
90bf11b014Sjason struct cfattach ukphy_ca = {
915d6e3d42Snate 	sizeof(struct mii_softc), ukphymatch, ukphyattach, mii_phy_detach,
925d6e3d42Snate 	    mii_phy_activate
93bf11b014Sjason };
94bf11b014Sjason 
95bf11b014Sjason struct cfdriver ukphy_cd = {
96bf11b014Sjason 	NULL, "ukphy", DV_DULL
97bf11b014Sjason };
98bf11b014Sjason 
99c4071fd1Smillert int	ukphy_service(struct mii_softc *, struct mii_data *, int);
100bf11b014Sjason 
10178a92591Sbrad const struct mii_phy_funcs ukphy_funcs = {
10278a92591Sbrad 	ukphy_service, ukphy_status, mii_phy_reset,
10378a92591Sbrad };
10478a92591Sbrad 
105bf11b014Sjason int
106*c43900ffSbrad ukphymatch(struct device *parent, void *match, void *aux)
107bf11b014Sjason {
108bf11b014Sjason 
109bf11b014Sjason 	/*
110bf11b014Sjason 	 * We know something is here, so always match at a low priority.
111bf11b014Sjason 	 */
112bf11b014Sjason 	return (1);
113bf11b014Sjason }
114bf11b014Sjason 
115bf11b014Sjason void
116*c43900ffSbrad ukphyattach(struct device *parent, struct device *self, void *aux)
117bf11b014Sjason {
118bf11b014Sjason 	struct mii_softc *sc = (struct mii_softc *)self;
119bf11b014Sjason 	struct mii_attach_args *ma = aux;
120bf11b014Sjason 	struct mii_data *mii = ma->mii_data;
121bf11b014Sjason 
122bf11b014Sjason 	printf(": Generic IEEE 802.3u media interface\n");
123bf11b014Sjason 	printf("%s: OUI 0x%06x, model 0x%04x, rev. %d\n",
124bf11b014Sjason 	    sc->mii_dev.dv_xname, MII_OUI(ma->mii_id1, ma->mii_id2),
125bf11b014Sjason 	    MII_MODEL(ma->mii_id2), MII_REV(ma->mii_id2));
126bf11b014Sjason 
127bf11b014Sjason 	sc->mii_inst = mii->mii_instance;
128bf11b014Sjason 	sc->mii_phy = ma->mii_phyno;
12978a92591Sbrad 	sc->mii_funcs = &ukphy_funcs;
130bf11b014Sjason 	sc->mii_pdata = mii;
1315d6e3d42Snate 	sc->mii_flags = mii->mii_flags;
1325d6e3d42Snate 
1335d6e3d42Snate 	/*
1345d6e3d42Snate 	 * Don't do loopback on unknown PHYs.  It might confuse some of them.
1355d6e3d42Snate 	 */
1365d6e3d42Snate 	sc->mii_flags |= MIIF_NOLOOP;
137bf11b014Sjason 
13878a92591Sbrad 	PHY_RESET(sc);
139bf11b014Sjason 
140bf11b014Sjason 	sc->mii_capabilities =
141bf11b014Sjason 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
142ebf88804Snate 	if (sc->mii_capabilities & BMSR_EXTSTAT)
143ebf88804Snate 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
144ebf88804Snate 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
145ebf88804Snate 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
146344b3c31Sjason 		printf("%s: no media present\n", sc->mii_dev.dv_xname);
1475d6e3d42Snate 	else
1485d6e3d42Snate 		mii_phy_add_media(sc);
149bf11b014Sjason }
150bf11b014Sjason 
151bf11b014Sjason int
152*c43900ffSbrad ukphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
153bf11b014Sjason {
154bf11b014Sjason 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
155bf11b014Sjason 	int reg;
156bf11b014Sjason 
1575d6e3d42Snate 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
1585d6e3d42Snate 		return (ENXIO);
1595d6e3d42Snate 
160bf11b014Sjason 	switch (cmd) {
161bf11b014Sjason 	case MII_POLLSTAT:
162bf11b014Sjason 		/*
163bf11b014Sjason 		 * If we're not polling our PHY instance, just return.
164bf11b014Sjason 		 */
165bf11b014Sjason 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
166bf11b014Sjason 			return (0);
167bf11b014Sjason 		break;
168bf11b014Sjason 
169bf11b014Sjason 	case MII_MEDIACHG:
170bf11b014Sjason 		/*
171bf11b014Sjason 		 * If the media indicates a different PHY instance,
172bf11b014Sjason 		 * isolate ourselves.
173bf11b014Sjason 		 */
174bf11b014Sjason 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
175bf11b014Sjason 			reg = PHY_READ(sc, MII_BMCR);
176bf11b014Sjason 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
177bf11b014Sjason 			return (0);
178bf11b014Sjason 		}
179bf11b014Sjason 
180bf11b014Sjason 		/*
181bf11b014Sjason 		 * If the interface is not up, don't do anything.
182bf11b014Sjason 		 */
183bf11b014Sjason 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
184bf11b014Sjason 			break;
185bf11b014Sjason 
1865c7e8fc0Sjason 		mii_phy_setmedia(sc);
187bf11b014Sjason 		break;
188bf11b014Sjason 
189bf11b014Sjason 	case MII_TICK:
190bf11b014Sjason 		/*
191bf11b014Sjason 		 * If we're not currently selected, just return.
192bf11b014Sjason 		 */
193bf11b014Sjason 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
194bf11b014Sjason 			return (0);
195bf11b014Sjason 
196bf11b014Sjason 		/*
197bf11b014Sjason 		 * Only used for autonegotiation.
198bf11b014Sjason 		 */
199bf11b014Sjason 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
200bf11b014Sjason 			return (0);
201bf11b014Sjason 
2025d6e3d42Snate 		if (mii_phy_tick(sc) == EJUSTRETURN)
203b2e83a74Sjason 			return (0);
204bf11b014Sjason 		break;
2055c7e8fc0Sjason 
2065c7e8fc0Sjason 	case MII_DOWN:
2075c7e8fc0Sjason 		mii_phy_down(sc);
2085c7e8fc0Sjason 		return (0);
209bf11b014Sjason 	}
210bf11b014Sjason 
211bf11b014Sjason 	/* Update the media status. */
2125d6e3d42Snate 	mii_phy_status(sc);
213bf11b014Sjason 
214bf11b014Sjason 	/* Callback if something changed. */
2155d6e3d42Snate 	mii_phy_update(sc, cmd);
216bf11b014Sjason 	return (0);
217bf11b014Sjason }
218