xref: /freebsd/sys/dev/dc/pnphy.c (revision cded07a8783dedbb0634ebde78764f935f26467d)
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 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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/lock.h>
49 #include <sys/module.h>
50 #include <sys/mutex.h>
51 #include <sys/bus.h>
52 
53 #include <net/if.h>
54 #include <net/if_arp.h>
55 #include <net/if_media.h>
56 
57 #include <dev/mii/mii.h>
58 #include <dev/mii/miivar.h>
59 #include "miidevs.h"
60 
61 #include <machine/bus.h>
62 #include <machine/resource.h>
63 #include <sys/bus.h>
64 
65 #include <dev/dc/if_dcreg.h>
66 
67 #include "miibus_if.h"
68 
69 #define DC_SETBIT(sc, reg, x)                           \
70         CSR_WRITE_4(sc, reg,                            \
71                 CSR_READ_4(sc, reg) | x)
72 
73 #define DC_CLRBIT(sc, reg, x)                           \
74         CSR_WRITE_4(sc, reg,                            \
75                 CSR_READ_4(sc, reg) & ~x)
76 
77 static int pnphy_probe(device_t);
78 static int pnphy_attach(device_t);
79 
80 static device_method_t pnphy_methods[] = {
81 	/* device interface */
82 	DEVMETHOD(device_probe,		pnphy_probe),
83 	DEVMETHOD(device_attach,	pnphy_attach),
84 	DEVMETHOD(device_detach,	mii_phy_detach),
85 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
86 	{ 0, 0 }
87 };
88 
89 static devclass_t pnphy_devclass;
90 
91 static driver_t pnphy_driver = {
92 	"pnphy",
93 	pnphy_methods,
94 	sizeof(struct mii_softc)
95 };
96 
97 DRIVER_MODULE(pnphy, miibus, pnphy_driver, pnphy_devclass, 0, 0);
98 
99 static int	pnphy_service(struct mii_softc *, struct mii_data *, int);
100 static void	pnphy_status(struct mii_softc *);
101 
102 static int
103 pnphy_probe(device_t dev)
104 {
105 	struct mii_attach_args *ma;
106 
107 	ma = device_get_ivars(dev);
108 
109 	/*
110 	 * The dc driver will report the 82c168 vendor and device
111 	 * ID to let us know that it wants us to attach.
112 	 */
113 	if (ma->mii_id1 != DC_VENDORID_LO ||
114 	    ma->mii_id2 != DC_DEVICEID_82C168)
115 		return (ENXIO);
116 
117 	device_set_desc(dev, "PNIC 82c168 media interface");
118 
119 	return (BUS_PROBE_DEFAULT);
120 }
121 
122 static int
123 pnphy_attach(device_t dev)
124 {
125 	struct mii_softc *sc;
126 	struct mii_attach_args *ma;
127 	struct mii_data *mii;
128 
129 	sc = device_get_softc(dev);
130 	ma = device_get_ivars(dev);
131 	sc->mii_dev = device_get_parent(dev);
132 	mii = ma->mii_data;
133 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
134 
135 	sc->mii_flags = miibus_get_flags(dev);
136 	sc->mii_inst = mii->mii_instance++;
137 	sc->mii_phy = ma->mii_phyno;
138 	sc->mii_service = pnphy_service;
139 	sc->mii_pdata = mii;
140 
141 	/*
142 	 * Apparently, we can neither isolate nor do loopback.
143 	 */
144 	sc->mii_flags |= MIIF_NOISOLATE | MIIF_NOLOOP;
145 
146 	sc->mii_capabilities =
147 	    BMSR_100TXFDX | BMSR_100TXHDX | BMSR_10TFDX | BMSR_10THDX;
148 	sc->mii_capabilities &= ma->mii_capmask;
149 	device_printf(dev, " ");
150 	mii_phy_add_media(sc);
151 	printf("\n");
152 
153 	MIIBUS_MEDIAINIT(sc->mii_dev);
154 	return (0);
155 }
156 
157 static int
158 pnphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
159 {
160 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
161 
162 	switch (cmd) {
163 	case MII_POLLSTAT:
164 		break;
165 
166 	case MII_MEDIACHG:
167 		/*
168 		 * If the interface is not up, don't do anything.
169 		 */
170 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
171 			break;
172 
173 		switch (IFM_SUBTYPE(ife->ifm_media)) {
174 		case IFM_AUTO:
175 			/* NWAY is busted on this chip */
176 		case IFM_100_T4:
177 			/*
178 			 * XXX Not supported as a manual setting right now.
179 			 */
180 			return (EINVAL);
181 		case IFM_100_TX:
182 			mii->mii_media_active = IFM_ETHER | IFM_100_TX;
183 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
184 				mii->mii_media_active |= IFM_FDX;
185 			MIIBUS_STATCHG(sc->mii_dev);
186 			return (0);
187 		case IFM_10_T:
188 			mii->mii_media_active = IFM_ETHER | IFM_10_T;
189 			if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
190 				mii->mii_media_active |= IFM_FDX;
191 			MIIBUS_STATCHG(sc->mii_dev);
192 			return (0);
193 		default:
194 			return (EINVAL);
195 		}
196 		break;
197 
198 	case MII_TICK:
199 		/*
200 		 * Is the interface even up?
201 		 */
202 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
203 			return (0);
204 
205 		break;
206 	}
207 
208 	/* Update the media status. */
209 	pnphy_status(sc);
210 
211 	/* Callback if something changed. */
212 	mii_phy_update(sc, cmd);
213 	return (0);
214 }
215 
216 static void
217 pnphy_status(struct mii_softc *sc)
218 {
219 	struct mii_data *mii = sc->mii_pdata;
220 	int reg;
221 	struct dc_softc		*dc_sc;
222 
223 	dc_sc = mii->mii_ifp->if_softc;
224 
225 	mii->mii_media_status = IFM_AVALID;
226 	mii->mii_media_active = IFM_ETHER;
227 
228 	reg = CSR_READ_4(dc_sc, DC_ISR);
229 
230 	if (!(reg & DC_ISR_LINKFAIL))
231 		mii->mii_media_status |= IFM_ACTIVE;
232 
233 	if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_SPEEDSEL)
234 		mii->mii_media_active |= IFM_10_T;
235 	else
236 		mii->mii_media_active |= IFM_100_TX;
237 	if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_FULLDUPLEX)
238 		mii->mii_media_active |= IFM_FDX;
239 	else
240 		mii->mii_media_active |= IFM_HDX;
241 }
242