xref: /freebsd/sys/dev/mii/pnaphy.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
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/cdefs.h>
47 __FBSDID("$FreeBSD$");
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/socket.h>
53 #include <sys/errno.h>
54 #include <sys/module.h>
55 #include <sys/bus.h>
56 
57 #include <net/if.h>
58 #include <net/if_media.h>
59 
60 
61 #include <dev/mii/mii.h>
62 #include <dev/mii/miivar.h>
63 #include "miidevs.h"
64 
65 #include "miibus_if.h"
66 
67 static int pnaphy_probe(device_t);
68 static int pnaphy_attach(device_t);
69 
70 static device_method_t pnaphy_methods[] = {
71 	/* device interface */
72 	DEVMETHOD(device_probe,		pnaphy_probe),
73 	DEVMETHOD(device_attach,	pnaphy_attach),
74 	DEVMETHOD(device_detach,	mii_phy_detach),
75 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
76 	{ 0, 0 }
77 };
78 
79 static devclass_t pnaphy_devclass;
80 
81 static driver_t pnaphy_driver = {
82 	"pnaphy",
83 	pnaphy_methods,
84 	sizeof(struct mii_softc)
85 };
86 
87 DRIVER_MODULE(pnaphy, miibus, pnaphy_driver, pnaphy_devclass, 0, 0);
88 
89 static int	pnaphy_service(struct mii_softc *, struct mii_data *,int);
90 
91 static int
92 pnaphy_probe(dev)
93 	device_t		dev;
94 {
95 
96 	struct mii_attach_args	*ma;
97 
98 	ma = device_get_ivars(dev);
99 
100 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_AMD &&
101 	    MII_MODEL(ma->mii_id2) == MII_MODEL_AMD_79c978) {
102 		device_set_desc(dev, MII_STR_AMD_79c978);
103 		return(0);
104 	}
105 
106 	return(ENXIO);
107 }
108 
109 static int
110 pnaphy_attach(dev)
111 	device_t		dev;
112 {
113 	struct mii_softc *sc;
114 	struct mii_attach_args *ma;
115 	struct mii_data *mii;
116 	const char *sep = "";
117 
118 	sc = device_get_softc(dev);
119 	ma = device_get_ivars(dev);
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_phy = ma->mii_phyno;
126 	sc->mii_service = pnaphy_service;
127 	sc->mii_pdata = mii;
128 
129 	mii->mii_instance++;
130 
131 	sc->mii_flags |= MIIF_NOISOLATE;
132 
133 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
134 #define PRINT(s)	printf("%s%s", sep, s); sep = ", "
135 
136 	mii_phy_reset(sc);
137 
138 	sc->mii_capabilities =
139 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
140 	device_printf(dev, " ");
141 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
142 		printf("no media present");
143 	else {
144 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_HPNA_1, 0, sc->mii_inst), 0);
145 		PRINT("HomePNA");
146 	}
147 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
148 	    BMCR_ISO);
149 
150 	printf("\n");
151 
152 #undef ADD
153 #undef PRINT
154 
155 	MIIBUS_MEDIAINIT(sc->mii_dev);
156 
157 	return(0);
158 }
159 
160 static int
161 pnaphy_service(sc, mii, cmd)
162 	struct mii_softc *sc;
163 	struct mii_data *mii;
164 	int cmd;
165 {
166 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
167 	int reg;
168 
169 	switch (cmd) {
170 	case MII_POLLSTAT:
171 		/*
172 		 * If we're not polling our PHY instance, just return.
173 		 */
174 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
175 			return (0);
176 		break;
177 
178 	case MII_MEDIACHG:
179 		/*
180 		 * If the media indicates a different PHY instance,
181 		 * isolate ourselves.
182 		 */
183 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
184 			reg = PHY_READ(sc, MII_BMCR);
185 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
186 			return (0);
187 		}
188 
189 		/*
190 		 * If the interface is not up, don't do anything.
191 		 */
192 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
193 			break;
194 
195 		switch (IFM_SUBTYPE(ife->ifm_media)) {
196 		case IFM_AUTO:
197 		case IFM_10_T:
198 		case IFM_100_TX:
199 		case IFM_100_T4:
200 			return (EINVAL);
201 		default:
202 			/*
203 			 * BMCR data is stored in the ifmedia entry.
204 			 */
205 			PHY_WRITE(sc, MII_ANAR,
206 			    mii_anar(ife->ifm_media));
207 			PHY_WRITE(sc, MII_BMCR, ife->ifm_data);
208 		}
209 		break;
210 
211 	case MII_TICK:
212 		/*
213 		 * If we're not currently selected, just return.
214 		 */
215 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
216 			return (0);
217 		if (mii_phy_tick(sc) == EJUSTRETURN)
218 			return (0);
219 		break;
220 	}
221 
222 	/* Update the media status. */
223 	ukphy_status(sc);
224 	if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T)
225 		mii->mii_media_active = IFM_ETHER|IFM_HPNA_1;
226 
227 	/* Callback if something changed. */
228 	mii_phy_update(sc, cmd);
229 	return (0);
230 }
231