xref: /openbsd/sys/dev/mii/ukphy.c (revision 09467b48)
1 /*	$OpenBSD: ukphy.c,v 1.24 2016/07/11 09:50:02 kettenis Exp $	*/
2 /*	$NetBSD: ukphy.c,v 1.9 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, and by Frank van der Linden.
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  *
46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
50  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
55  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  */
57 
58 /*
59  * driver for generic unknown PHYs
60  */
61 
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/device.h>
65 #include <sys/socket.h>
66 #include <sys/errno.h>
67 
68 #include <net/if.h>
69 #include <net/if_var.h>
70 #include <net/if_media.h>
71 
72 #include <dev/mii/mii.h>
73 #include <dev/mii/miivar.h>
74 
75 int	ukphymatch(struct device *, void *, void *);
76 void	ukphyattach(struct device *, struct device *, void *);
77 
78 struct cfattach ukphy_ca = {
79 	sizeof(struct mii_softc), ukphymatch, ukphyattach, mii_phy_detach
80 };
81 
82 struct cfdriver ukphy_cd = {
83 	NULL, "ukphy", DV_DULL
84 };
85 
86 int	ukphy_service(struct mii_softc *, struct mii_data *, int);
87 
88 const struct mii_phy_funcs ukphy_funcs = {
89 	ukphy_service, ukphy_status, mii_phy_reset,
90 };
91 
92 int
93 ukphymatch(struct device *parent, void *match, void *aux)
94 {
95 
96 	/*
97 	 * We know something is here, so always match at a low priority.
98 	 */
99 	return (1);
100 }
101 
102 void
103 ukphyattach(struct device *parent, struct device *self, void *aux)
104 {
105 	struct mii_softc *sc = (struct mii_softc *)self;
106 	struct mii_attach_args *ma = aux;
107 	struct mii_data *mii = ma->mii_data;
108 
109 	printf(": Generic IEEE 802.3u media interface, rev. %d:",
110 	    MII_REV(ma->mii_id2));
111 	printf(" OUI 0x%06x, model 0x%04x\n",
112 	    MII_OUI(ma->mii_id1, ma->mii_id2), MII_MODEL(ma->mii_id2));
113 
114 	sc->mii_inst = mii->mii_instance;
115 	sc->mii_phy = ma->mii_phyno;
116 	sc->mii_funcs = &ukphy_funcs;
117 	sc->mii_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
118 	sc->mii_model = MII_MODEL(ma->mii_id2);
119 	sc->mii_pdata = mii;
120 	sc->mii_flags = ma->mii_flags;
121 
122 	/*
123 	 * Don't do loopback on unknown PHYs.  It might confuse some of them.
124 	 */
125 	sc->mii_flags |= MIIF_NOLOOP;
126 
127 	PHY_RESET(sc);
128 
129 	sc->mii_capabilities =
130 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
131 	if (sc->mii_capabilities & BMSR_EXTSTAT)
132 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
133 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
134 	    (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
135 		printf("%s: no media present\n", sc->mii_dev.dv_xname);
136 	else
137 		mii_phy_add_media(sc);
138 }
139 
140 int
141 ukphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
142 {
143 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
144 	int reg;
145 
146 	if ((sc->mii_dev.dv_flags & DVF_ACTIVE) == 0)
147 		return (ENXIO);
148 
149 	switch (cmd) {
150 	case MII_POLLSTAT:
151 		/*
152 		 * If we're not polling our PHY instance, just return.
153 		 */
154 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
155 			return (0);
156 		break;
157 
158 	case MII_MEDIACHG:
159 		/*
160 		 * If the media indicates a different PHY instance,
161 		 * isolate ourselves.
162 		 */
163 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
164 			reg = PHY_READ(sc, MII_BMCR);
165 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
166 			return (0);
167 		}
168 
169 		/*
170 		 * If the interface is not up, don't do anything.
171 		 */
172 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
173 			break;
174 
175 		mii_phy_setmedia(sc);
176 		break;
177 
178 	case MII_TICK:
179 		/*
180 		 * If we're not currently selected, just return.
181 		 */
182 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
183 			return (0);
184 
185 		if (mii_phy_tick(sc) == EJUSTRETURN)
186 			return (0);
187 		break;
188 
189 	case MII_DOWN:
190 		mii_phy_down(sc);
191 		return (0);
192 	}
193 
194 	/* Update the media status. */
195 	mii_phy_status(sc);
196 
197 	/* Callback if something changed. */
198 	mii_phy_update(sc, cmd);
199 	return (0);
200 }
201