xref: /openbsd/sys/dev/mii/ukphy.c (revision 471aeecf)
1*471aeecfSnaddy /*	$OpenBSD: ukphy.c,v 1.25 2022/04/06 18:59:29 naddy 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  *
21bf11b014Sjason  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22bf11b014Sjason  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23bf11b014Sjason  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24bf11b014Sjason  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25bf11b014Sjason  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26bf11b014Sjason  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27bf11b014Sjason  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28bf11b014Sjason  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29bf11b014Sjason  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30bf11b014Sjason  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31bf11b014Sjason  * POSSIBILITY OF SUCH DAMAGE.
32bf11b014Sjason  */
33bf11b014Sjason 
34bf11b014Sjason /*
35bf11b014Sjason  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
36bf11b014Sjason  *
37bf11b014Sjason  * Redistribution and use in source and binary forms, with or without
38bf11b014Sjason  * modification, are permitted provided that the following conditions
39bf11b014Sjason  * are met:
40bf11b014Sjason  * 1. Redistributions of source code must retain the above copyright
41bf11b014Sjason  *    notice, this list of conditions and the following disclaimer.
42bf11b014Sjason  * 2. Redistributions in binary form must reproduce the above copyright
43bf11b014Sjason  *    notice, this list of conditions and the following disclaimer in the
44bf11b014Sjason  *    documentation and/or other materials provided with the distribution.
45bf11b014Sjason  *
46bf11b014Sjason  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47bf11b014Sjason  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48bf11b014Sjason  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49bf11b014Sjason  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
50bf11b014Sjason  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51bf11b014Sjason  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52bf11b014Sjason  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53bf11b014Sjason  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54bf11b014Sjason  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
55bf11b014Sjason  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56bf11b014Sjason  */
57bf11b014Sjason 
58bf11b014Sjason /*
59bf11b014Sjason  * driver for generic unknown PHYs
60bf11b014Sjason  */
61bf11b014Sjason 
62bf11b014Sjason #include <sys/param.h>
63bf11b014Sjason #include <sys/systm.h>
64bf11b014Sjason #include <sys/device.h>
65bf11b014Sjason #include <sys/socket.h>
66b2e83a74Sjason #include <sys/errno.h>
67bf11b014Sjason 
68bf11b014Sjason #include <net/if.h>
690deb6685Smpi #include <net/if_var.h>
70bf11b014Sjason #include <net/if_media.h>
71bf11b014Sjason 
72bf11b014Sjason #include <dev/mii/mii.h>
73bf11b014Sjason #include <dev/mii/miivar.h>
74bf11b014Sjason 
75c4071fd1Smillert int	ukphymatch(struct device *, void *, void *);
76c4071fd1Smillert void	ukphyattach(struct device *, struct device *, void *);
77bf11b014Sjason 
78*471aeecfSnaddy const struct cfattach ukphy_ca = {
79fa9fb3edSderaadt 	sizeof(struct mii_softc), ukphymatch, ukphyattach, mii_phy_detach
80bf11b014Sjason };
81bf11b014Sjason 
82bf11b014Sjason struct cfdriver ukphy_cd = {
83bf11b014Sjason 	NULL, "ukphy", DV_DULL
84bf11b014Sjason };
85bf11b014Sjason 
86c4071fd1Smillert int	ukphy_service(struct mii_softc *, struct mii_data *, int);
87bf11b014Sjason 
8878a92591Sbrad const struct mii_phy_funcs ukphy_funcs = {
8978a92591Sbrad 	ukphy_service, ukphy_status, mii_phy_reset,
9078a92591Sbrad };
9178a92591Sbrad 
92bf11b014Sjason int
ukphymatch(struct device * parent,void * match,void * aux)93c43900ffSbrad ukphymatch(struct device *parent, void *match, void *aux)
94bf11b014Sjason {
95bf11b014Sjason 
96bf11b014Sjason 	/*
97bf11b014Sjason 	 * We know something is here, so always match at a low priority.
98bf11b014Sjason 	 */
99bf11b014Sjason 	return (1);
100bf11b014Sjason }
101bf11b014Sjason 
102bf11b014Sjason void
ukphyattach(struct device * parent,struct device * self,void * aux)103c43900ffSbrad ukphyattach(struct device *parent, struct device *self, void *aux)
104bf11b014Sjason {
105bf11b014Sjason 	struct mii_softc *sc = (struct mii_softc *)self;
106bf11b014Sjason 	struct mii_attach_args *ma = aux;
107bf11b014Sjason 	struct mii_data *mii = ma->mii_data;
108bf11b014Sjason 
10969feb0e2Sbrad 	printf(": Generic IEEE 802.3u media interface, rev. %d:",
11069feb0e2Sbrad 	    MII_REV(ma->mii_id2));
1119b9295b8Smaja 	printf(" OUI 0x%06x, model 0x%04x\n",
11269feb0e2Sbrad 	    MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2));
113bf11b014Sjason 
114bf11b014Sjason 	sc->mii_inst = mii->mii_instance;
115bf11b014Sjason 	sc->mii_phy = ma->mii_phyno;
11678a92591Sbrad 	sc->mii_funcs = &ukphy_funcs;
1172ff229f7Skettenis 	sc->mii_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
1182ff229f7Skettenis 	sc->mii_model = MII_MODEL(ma->mii_id2);
119bf11b014Sjason 	sc->mii_pdata = mii;
120d240c9bfSbrad 	sc->mii_flags = ma->mii_flags;
1215d6e3d42Snate 
1225d6e3d42Snate 	/*
1235d6e3d42Snate 	 * Don't do loopback on unknown PHYs.  It might confuse some of them.
1245d6e3d42Snate 	 */
1255d6e3d42Snate 	sc->mii_flags |= MIIF_NOLOOP;
126bf11b014Sjason 
12778a92591Sbrad 	PHY_RESET(sc);
128bf11b014Sjason 
129bf11b014Sjason 	sc->mii_capabilities =
130bf11b014Sjason 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
131ebf88804Snate 	if (sc->mii_capabilities & BMSR_EXTSTAT)
132ebf88804Snate 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
133ebf88804Snate 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
134ebf88804Snate 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
135344b3c31Sjason 		printf("%s: no media present\n", sc->mii_dev.dv_xname);
1365d6e3d42Snate 	else
1375d6e3d42Snate 		mii_phy_add_media(sc);
138bf11b014Sjason }
139bf11b014Sjason 
140bf11b014Sjason int
ukphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)141c43900ffSbrad ukphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
142bf11b014Sjason {
143bf11b014Sjason 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
144bf11b014Sjason 	int reg;
145bf11b014Sjason 
1465d6e3d42Snate 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
1475d6e3d42Snate 		return (ENXIO);
1485d6e3d42Snate 
149bf11b014Sjason 	switch (cmd) {
150bf11b014Sjason 	case MII_POLLSTAT:
151bf11b014Sjason 		/*
152bf11b014Sjason 		 * If we're not polling our PHY instance, just return.
153bf11b014Sjason 		 */
154bf11b014Sjason 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
155bf11b014Sjason 			return (0);
156bf11b014Sjason 		break;
157bf11b014Sjason 
158bf11b014Sjason 	case MII_MEDIACHG:
159bf11b014Sjason 		/*
160bf11b014Sjason 		 * If the media indicates a different PHY instance,
161bf11b014Sjason 		 * isolate ourselves.
162bf11b014Sjason 		 */
163bf11b014Sjason 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
164bf11b014Sjason 			reg = PHY_READ(sc, MII_BMCR);
165bf11b014Sjason 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
166bf11b014Sjason 			return (0);
167bf11b014Sjason 		}
168bf11b014Sjason 
169bf11b014Sjason 		/*
170bf11b014Sjason 		 * If the interface is not up, don't do anything.
171bf11b014Sjason 		 */
172bf11b014Sjason 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
173bf11b014Sjason 			break;
174bf11b014Sjason 
1755c7e8fc0Sjason 		mii_phy_setmedia(sc);
176bf11b014Sjason 		break;
177bf11b014Sjason 
178bf11b014Sjason 	case MII_TICK:
179bf11b014Sjason 		/*
180bf11b014Sjason 		 * If we're not currently selected, just return.
181bf11b014Sjason 		 */
182bf11b014Sjason 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
183bf11b014Sjason 			return (0);
184bf11b014Sjason 
1855d6e3d42Snate 		if (mii_phy_tick(sc) == EJUSTRETURN)
186b2e83a74Sjason 			return (0);
187bf11b014Sjason 		break;
1885c7e8fc0Sjason 
1895c7e8fc0Sjason 	case MII_DOWN:
1905c7e8fc0Sjason 		mii_phy_down(sc);
1915c7e8fc0Sjason 		return (0);
192bf11b014Sjason 	}
193bf11b014Sjason 
194bf11b014Sjason 	/* Update the media status. */
1955d6e3d42Snate 	mii_phy_status(sc);
196bf11b014Sjason 
197bf11b014Sjason 	/* Callback if something changed. */
1985d6e3d42Snate 	mii_phy_update(sc, cmd);
199bf11b014Sjason 	return (0);
200bf11b014Sjason }
201