xref: /dragonfly/sys/dev/netif/mii_layer/pnphy.c (revision 2e3ed54d)
1 /*
2  * Copyright (c) 1997, 1998, 1999
3  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/dev/mii/pnphy.c,v 1.1.2.1 2002/11/08 21:53:49 semenu Exp $
33  * $DragonFly: src/sys/dev/netif/mii_layer/pnphy.c,v 1.8 2005/10/24 16:55:40 dillon Exp $
34  */
35 
36 /*
37  * Pseudo-driver for media selection on the Lite-On PNIC 82c168
38  * chip. The NWAY support on this chip is horribly broken, so we
39  * only support manual mode selection. This is lame, but getting
40  * NWAY to work right is amazingly difficult.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/socket.h>
47 #include <sys/errno.h>
48 #include <sys/module.h>
49 #include <sys/bus.h>
50 
51 #include <net/if.h>
52 #include <net/if_arp.h>
53 #include <net/if_media.h>
54 
55 #include "mii.h"
56 #include "miivar.h"
57 #include "miidevs.h"
58 
59 #include <machine/clock.h>
60 #include <machine/bus_pio.h>
61 #include <machine/bus_memio.h>
62 #include <machine/bus.h>
63 #include <machine/resource.h>
64 #include <sys/bus.h>
65 
66 #include "../dc/if_dcreg.h"
67 
68 #include "miibus_if.h"
69 
70 #define DC_SETBIT(sc, reg, x)                           \
71         CSR_WRITE_4(sc, reg,                            \
72                 CSR_READ_4(sc, reg) | x)
73 
74 #define DC_CLRBIT(sc, reg, x)                           \
75         CSR_WRITE_4(sc, reg,                            \
76                 CSR_READ_4(sc, reg) & ~x)
77 
78 static int pnphy_probe		(device_t);
79 static int pnphy_attach		(device_t);
80 static int pnphy_detach		(device_t);
81 
82 static device_method_t pnphy_methods[] = {
83 	/* device interface */
84 	DEVMETHOD(device_probe,		pnphy_probe),
85 	DEVMETHOD(device_attach,	pnphy_attach),
86 	DEVMETHOD(device_detach,	pnphy_detach),
87 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
88 	{ 0, 0 }
89 };
90 
91 static devclass_t pnphy_devclass;
92 
93 static driver_t pnphy_driver = {
94 	"pnphy",
95 	pnphy_methods,
96 	sizeof(struct mii_softc)
97 };
98 
99 DRIVER_MODULE(pnphy, miibus, pnphy_driver, pnphy_devclass, 0, 0);
100 
101 int	pnphy_service (struct mii_softc *, struct mii_data *, int);
102 void	pnphy_status (struct mii_softc *);
103 
104 static int pnphy_probe(dev)
105 	device_t		dev;
106 {
107 	struct mii_attach_args *ma;
108 
109 	ma = device_get_ivars(dev);
110 
111 	/*
112 	 * The dc driver will report the 82c168 vendor and device
113 	 * ID to let us know that it wants us to attach.
114 	 */
115 	if (ma->mii_id1 != DC_VENDORID_LO ||
116 	    ma->mii_id2 != DC_DEVICEID_82C168)
117 		return(ENXIO);
118 
119 	device_set_desc(dev, "PNIC 82c168 media interface");
120 
121 	return (0);
122 }
123 
124 static int pnphy_attach(dev)
125 	device_t		dev;
126 {
127 	struct mii_softc *sc;
128 	struct mii_attach_args *ma;
129 	struct mii_data *mii;
130 
131 	sc = device_get_softc(dev);
132 	ma = device_get_ivars(dev);
133 	mii_softc_init(sc, ma);
134 	sc->mii_dev = device_get_parent(dev);
135 	mii = device_get_softc(sc->mii_dev);
136 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
137 
138 	sc->mii_inst = mii->mii_instance;
139 	sc->mii_service = pnphy_service;
140 	sc->mii_pdata = mii;
141 
142 	sc->mii_flags |= MIIF_NOISOLATE;
143 	mii->mii_instance++;
144 
145 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
146 
147 	sc->mii_capabilities =
148 	    BMSR_100TXFDX|BMSR_100TXHDX|BMSR_10TFDX|BMSR_10THDX;
149 	sc->mii_capabilities &= ma->mii_capmask;
150 	device_printf(dev, " ");
151 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
152 		printf("no media present");
153 	else
154 		mii_add_media(sc, sc->mii_capabilities);
155 	printf("\n");
156 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
157 	    BMCR_ISO);
158 
159 	ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
160 	    BMCR_LOOP|BMCR_S100);
161 
162 #undef ADD
163 
164 	MIIBUS_MEDIAINIT(sc->mii_dev);
165 	return(0);
166 }
167 
168 static int pnphy_detach(dev)
169 	device_t		dev;
170 {
171 	struct mii_softc *sc;
172 	struct mii_data *mii;
173 
174 	sc = device_get_softc(dev);
175 	mii = device_get_softc(device_get_parent(dev));
176 	sc->mii_dev = NULL;
177 	LIST_REMOVE(sc, mii_list);
178 
179 	return(0);
180 }
181 
182 int
183 pnphy_service(sc, mii, cmd)
184 	struct mii_softc *sc;
185 	struct mii_data *mii;
186 	int cmd;
187 {
188 	struct dc_softc		*dc_sc;
189 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
190 
191 	dc_sc = mii->mii_ifp->if_softc;
192 
193 	switch (cmd) {
194 	case MII_POLLSTAT:
195 		/*
196 		 * If we're not polling our PHY instance, just return.
197 		 */
198 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
199 			return (0);
200 		}
201 		break;
202 
203 	case MII_MEDIACHG:
204 		/*
205 		 * If the media indicates a different PHY instance,
206 		 * isolate ourselves.
207 		 */
208 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
209 			return (0);
210 
211 		/*
212 		 * If the interface is not up, don't do anything.
213 		 */
214 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
215 			break;
216 
217 		sc->mii_flags = 0;
218 
219 		switch (IFM_SUBTYPE(ife->ifm_media)) {
220 		case IFM_AUTO:
221 			/* NWAY is busted on this chip */
222 		case IFM_100_T4:
223 			/*
224 			 * XXX Not supported as a manual setting right now.
225 			 */
226 			return (EINVAL);
227 		case IFM_100_TX:
228 			mii->mii_media_active = IFM_ETHER|IFM_100_TX;
229 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
230 				mii->mii_media_active |= IFM_FDX;
231 			MIIBUS_STATCHG(sc->mii_dev);
232 			return(0);
233 			break;
234 		case IFM_10_T:
235 			mii->mii_media_active = IFM_ETHER|IFM_10_T;
236 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
237 				mii->mii_media_active |= IFM_FDX;
238 			MIIBUS_STATCHG(sc->mii_dev);
239 			return(0);
240 			break;
241 		default:
242 			return(EINVAL);
243 			break;
244 		}
245 		break;
246 
247 	case MII_TICK:
248 		/*
249 		 * If we're not currently selected, just return.
250 		 */
251 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
252 			return (0);
253 
254 		/*
255 		 * Only used for autonegotiation.
256 		 */
257 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
258 			return (0);
259 
260 		/*
261 		 * Is the interface even up?
262 		 */
263 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
264 			return (0);
265 
266 		return(0);
267 	}
268 
269 	/* Update the media status. */
270 	pnphy_status(sc);
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 
280 void
281 pnphy_status(sc)
282 	struct mii_softc *sc;
283 {
284 	struct mii_data *mii = sc->mii_pdata;
285 	int reg;
286 	struct dc_softc		*dc_sc;
287 
288 	dc_sc = mii->mii_ifp->if_softc;
289 
290 	mii->mii_media_status = IFM_AVALID;
291 	mii->mii_media_active = IFM_ETHER;
292 
293 	reg = CSR_READ_4(dc_sc, DC_ISR);
294 
295 	if (!(reg & DC_ISR_LINKFAIL))
296 		mii->mii_media_status |= IFM_ACTIVE;
297 
298 	if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_SPEEDSEL)
299 		mii->mii_media_active |= IFM_10_T;
300 	else
301 		mii->mii_media_active |= IFM_100_TX;
302 	if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_FULLDUPLEX)
303 		mii->mii_media_active |= IFM_FDX;
304 
305 	return;
306 }
307