xref: /dflybsd-src/sys/dev/netif/mii_layer/pnaphy.c (revision 1f0f7b357f695364c2d426fad5891e5b0177d56a)
1 /*
2  * Copyright (c) 2000 Berkeley Software Design, Inc.
3  * Copyright (c) 1997, 1998, 1999, 2000
4  *	Bill Paul <wpaul@osd.bsdi.com>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Bill Paul.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
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
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD: src/sys/dev/mii/pnaphy.c,v 1.1.2.3 2002/11/08 21:53:49 semenu Exp $
34  * $DragonFly: src/sys/dev/netif/mii_layer/pnaphy.c,v 1.8 2005/10/24 16:55:40 dillon Exp $
35  */
36 
37 /*
38  * driver for homePNA PHYs
39  * This is really just a stub that allows us to identify homePNA-based
40  * transceicers and display the link status. MII-based homePNA PHYs
41  * only support one media type and no autonegotiation. If we were
42  * really clever, we could tweak some of the vendor-specific registers
43  * to optimize the link.
44  */
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/socket.h>
50 #include <sys/errno.h>
51 #include <sys/module.h>
52 #include <sys/bus.h>
53 
54 #include <net/if.h>
55 #include <net/if_media.h>
56 
57 #include <machine/clock.h>
58 
59 #include "mii.h"
60 #include "miivar.h"
61 #include "miidevs.h"
62 
63 #include "miibus_if.h"
64 
65 static int pnaphy_probe		(device_t);
66 static int pnaphy_attach	(device_t);
67 static int pnaphy_detach	(device_t);
68 
69 static device_method_t pnaphy_methods[] = {
70 	/* device interface */
71 	DEVMETHOD(device_probe,		pnaphy_probe),
72 	DEVMETHOD(device_attach,	pnaphy_attach),
73 	DEVMETHOD(device_detach,	pnaphy_detach),
74 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
75 	{ 0, 0 }
76 };
77 
78 static devclass_t pnaphy_devclass;
79 
80 static driver_t pnaphy_driver = {
81 	"pnaphy",
82 	pnaphy_methods,
83 	sizeof(struct mii_softc)
84 };
85 
86 DRIVER_MODULE(pnaphy, miibus, pnaphy_driver, pnaphy_devclass, 0, 0);
87 
88 int	pnaphy_service (struct mii_softc *, struct mii_data *, int);
89 
90 static int
91 pnaphy_probe(dev)
92 	device_t		dev;
93 {
94 
95 	struct mii_attach_args	*ma;
96 
97 	ma = device_get_ivars(dev);
98 
99 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_AMD &&
100 	    MII_MODEL(ma->mii_id2) == MII_MODEL_AMD_79c978) {
101 		device_set_desc(dev, MII_STR_AMD_79c978);
102 		return(0);
103 	}
104 
105 	return(ENXIO);
106 }
107 
108 static int
109 pnaphy_attach(dev)
110 	device_t		dev;
111 {
112 	struct mii_softc *sc;
113 	struct mii_attach_args *ma;
114 	struct mii_data *mii;
115 	const char *sep = "";
116 
117 	sc = device_get_softc(dev);
118 	ma = device_get_ivars(dev);
119 	mii_softc_init(sc, ma);
120 	sc->mii_dev = device_get_parent(dev);
121 	mii = device_get_softc(sc->mii_dev);
122 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
123 
124 	sc->mii_inst = mii->mii_instance;
125 	sc->mii_service = pnaphy_service;
126 	sc->mii_pdata = mii;
127 
128 	mii->mii_instance++;
129 
130 	sc->mii_flags |= MIIF_NOISOLATE;
131 
132 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
133 #define PRINT(s)	printf("%s%s", sep, s); sep = ", "
134 
135 	mii_phy_reset(sc);
136 
137 	sc->mii_capabilities =
138 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
139 	device_printf(dev, " ");
140 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
141 		printf("no media present");
142 	else {
143 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0, sc->mii_inst), 0);
144 		PRINT("HomePNA");
145 	}
146 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
147 	    BMCR_ISO);
148 
149 	printf("\n");
150 
151 #undef ADD
152 #undef PRINT
153 
154 	MIIBUS_MEDIAINIT(sc->mii_dev);
155 
156 	return(0);
157 }
158 
159 static int pnaphy_detach(dev)
160 	device_t		dev;
161 {
162 	struct mii_softc *sc;
163 	struct mii_data *mii;
164 
165 	sc = device_get_softc(dev);
166 	mii = device_get_softc(device_get_parent(dev));
167 	mii_phy_auto_stop(sc);
168 	sc->mii_dev = NULL;
169 	LIST_REMOVE(sc, mii_list);
170 
171 	return(0);
172 }
173 
174 int
175 pnaphy_service(sc, mii, cmd)
176 	struct mii_softc *sc;
177 	struct mii_data *mii;
178 	int cmd;
179 {
180 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
181 	int reg;
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 		switch (IFM_SUBTYPE(ife->ifm_media)) {
210 		case IFM_AUTO:
211 		case IFM_10_T:
212 		case IFM_100_TX:
213 		case IFM_100_T4:
214 			return (EINVAL);
215 		default:
216 			/*
217 			 * BMCR data is stored in the ifmedia entry.
218 			 */
219 			PHY_WRITE(sc, MII_ANAR,
220 			    mii_anar(ife->ifm_media));
221 			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
222 		}
223 		break;
224 
225 	case MII_TICK:
226 		/*
227 		 * If we're not currently selected, just return.
228 		 */
229 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
230 			return (0);
231 
232 		/*
233 		 * Only used for autonegotiation.
234 		 */
235 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
236 			return (0);
237 
238 		/*
239 		 * Is the interface even up?
240 		 */
241 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
242 			return (0);
243 
244 		/*
245 		 * Check to see if we have link.  If we do, we don't
246 		 * need to restart the autonegotiation process.  Read
247 		 * the BMSR twice in case it's latched.
248 		 */
249 		reg = PHY_READ(sc, MII_BMSR) |
250 		    PHY_READ(sc, MII_BMSR);
251 		if (reg & BMSR_LINK)
252 			return (0);
253 
254 		/*
255 		 * Only retry autonegotiation every 5 seconds.
256 		 */
257 		if (++sc->mii_ticks != 5)
258 			return (0);
259 
260 		sc->mii_ticks = 0;
261 		mii_phy_reset(sc);
262 		if (mii_phy_auto(sc, 0) == EJUSTRETURN)
263 			return (0);
264 		break;
265 	}
266 
267 	/* Update the media status. */
268 	ukphy_status(sc);
269 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T)
270 		mii->mii_media_active = IFM_ETHER | IFM_HPNA_1;
271 
272 	/* Callback if something changed. */
273 	if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
274 		MIIBUS_STATCHG(sc->mii_dev);
275 		sc->mii_active = mii->mii_media_active;
276 	}
277 	return (0);
278 }
279