xref: /freebsd/sys/dev/dc/pnphy.c (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
1 /*
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1997, 1998, 1999
5  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 /*
39  * Pseudo-driver for media selection on the Lite-On PNIC 82c168
40  * chip.  The NWAY support on this chip is horribly broken, so we
41  * only support manual mode selection.  This is lame, but getting
42  * NWAY to work right is amazingly difficult.
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/errno.h>
50 #include <sys/lock.h>
51 #include <sys/module.h>
52 #include <sys/mutex.h>
53 #include <sys/bus.h>
54 
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/if_arp.h>
58 #include <net/if_media.h>
59 
60 #include <dev/mii/mii.h>
61 #include <dev/mii/miivar.h>
62 #include "miidevs.h"
63 
64 #include <machine/bus.h>
65 #include <machine/resource.h>
66 
67 #include <dev/dc/if_dcreg.h>
68 
69 #include "miibus_if.h"
70 
71 static int pnphy_probe(device_t);
72 static int pnphy_attach(device_t);
73 
74 static device_method_t pnphy_methods[] = {
75 	/* device interface */
76 	DEVMETHOD(device_probe,		pnphy_probe),
77 	DEVMETHOD(device_attach,	pnphy_attach),
78 	DEVMETHOD(device_detach,	mii_phy_detach),
79 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
80 	DEVMETHOD_END
81 };
82 
83 static devclass_t pnphy_devclass;
84 
85 static driver_t pnphy_driver = {
86 	"pnphy",
87 	pnphy_methods,
88 	sizeof(struct mii_softc)
89 };
90 
91 DRIVER_MODULE(pnphy, miibus, pnphy_driver, pnphy_devclass, 0, 0);
92 
93 static int	pnphy_service(struct mii_softc *, struct mii_data *, int);
94 static void	pnphy_status(struct mii_softc *);
95 static void	pnphy_reset(struct mii_softc *);
96 
97 static const struct mii_phy_funcs pnphy_funcs = {
98 	pnphy_service,
99 	pnphy_status,
100 	pnphy_reset
101 };
102 
103 static int
104 pnphy_probe(device_t dev)
105 {
106 	struct mii_attach_args *ma;
107 
108 	ma = device_get_ivars(dev);
109 
110 	/*
111 	 * The dc driver will report the 82c168 vendor and device
112 	 * ID to let us know that it wants us to attach.
113 	 */
114 	if (ma->mii_id1 != DC_VENDORID_LO ||
115 	    ma->mii_id2 != DC_DEVICEID_82C168)
116 		return (ENXIO);
117 
118 	device_set_desc(dev, "PNIC 82c168 media interface");
119 
120 	return (BUS_PROBE_DEFAULT);
121 }
122 
123 static int
124 pnphy_attach(device_t dev)
125 {
126 	struct mii_softc *sc;
127 
128 	sc = device_get_softc(dev);
129 
130 	mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE,
131 	    &pnphy_funcs, 0);
132 
133 	sc->mii_capabilities =
134 	    BMSR_100TXFDX | BMSR_100TXHDX | BMSR_10TFDX | BMSR_10THDX;
135 	sc->mii_capabilities &= sc->mii_capmask;
136 	device_printf(dev, " ");
137 	mii_phy_add_media(sc);
138 	printf("\n");
139 
140 	MIIBUS_MEDIAINIT(sc->mii_dev);
141 	return (0);
142 }
143 
144 static int
145 pnphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
146 {
147 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
148 
149 	switch (cmd) {
150 	case MII_POLLSTAT:
151 		break;
152 
153 	case MII_MEDIACHG:
154 		/*
155 		 * If the interface is not up, don't do anything.
156 		 */
157 		if ((if_getflags(mii->mii_ifp) & IFF_UP) == 0)
158 			break;
159 
160 		/*
161 		 * Note that auto-negotiation is broken on this chip.
162 		 */
163 		switch (IFM_SUBTYPE(ife->ifm_media)) {
164 		case IFM_100_TX:
165 			mii->mii_media_active = IFM_ETHER | IFM_100_TX;
166 			if ((ife->ifm_media & IFM_FDX) != 0)
167 				mii->mii_media_active |= IFM_FDX;
168 			MIIBUS_STATCHG(sc->mii_dev);
169 			return (0);
170 		case IFM_10_T:
171 			mii->mii_media_active = IFM_ETHER | IFM_10_T;
172 			if ((ife->ifm_media & IFM_FDX) != 0)
173 				mii->mii_media_active |= IFM_FDX;
174 			MIIBUS_STATCHG(sc->mii_dev);
175 			return (0);
176 		default:
177 			return (EINVAL);
178 		}
179 		break;
180 
181 	case MII_TICK:
182 		/*
183 		 * Is the interface even up?
184 		 */
185 		if ((if_getflags(mii->mii_ifp) & IFF_UP) == 0)
186 			return (0);
187 
188 		break;
189 	}
190 
191 	/* Update the media status. */
192 	PHY_STATUS(sc);
193 
194 	/* Callback if something changed. */
195 	mii_phy_update(sc, cmd);
196 	return (0);
197 }
198 
199 static void
200 pnphy_status(struct mii_softc *sc)
201 {
202 	struct mii_data *mii = sc->mii_pdata;
203 	int reg;
204 	struct dc_softc		*dc_sc;
205 
206 	dc_sc = if_getsoftc(mii->mii_ifp);
207 
208 	mii->mii_media_status = IFM_AVALID;
209 	mii->mii_media_active = IFM_ETHER;
210 
211 	reg = CSR_READ_4(dc_sc, DC_ISR);
212 	if (!(reg & DC_ISR_LINKFAIL))
213 		mii->mii_media_status |= IFM_ACTIVE;
214 	reg = CSR_READ_4(dc_sc, DC_NETCFG);
215 	if (reg & DC_NETCFG_SPEEDSEL)
216 		mii->mii_media_active |= IFM_10_T;
217 	else
218 		mii->mii_media_active |= IFM_100_TX;
219 	if (reg & DC_NETCFG_FULLDUPLEX)
220 		mii->mii_media_active |= IFM_FDX;
221 	else
222 		mii->mii_media_active |= IFM_HDX;
223 }
224 
225 static void
226 pnphy_reset(struct mii_softc *sc __unused)
227 {
228 
229 }
230