xref: /freebsd/sys/dev/dc/pnphy.c (revision 685dc743)
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 /*
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_var.h>
55 #include <net/if_arp.h>
56 #include <net/if_media.h>
57 
58 #include <dev/mii/mii.h>
59 #include <dev/mii/miivar.h>
60 #include "miidevs.h"
61 
62 #include <machine/bus.h>
63 #include <machine/resource.h>
64 
65 #include <dev/dc/if_dcreg.h>
66 
67 #include "miibus_if.h"
68 
69 static int pnphy_probe(device_t);
70 static int pnphy_attach(device_t);
71 
72 static device_method_t pnphy_methods[] = {
73 	/* device interface */
74 	DEVMETHOD(device_probe,		pnphy_probe),
75 	DEVMETHOD(device_attach,	pnphy_attach),
76 	DEVMETHOD(device_detach,	mii_phy_detach),
77 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
78 	DEVMETHOD_END
79 };
80 
81 static driver_t pnphy_driver = {
82 	"pnphy",
83 	pnphy_methods,
84 	sizeof(struct mii_softc)
85 };
86 
87 DRIVER_MODULE(pnphy, miibus, pnphy_driver, 0, 0);
88 
89 static int	pnphy_service(struct mii_softc *, struct mii_data *, int);
90 static void	pnphy_status(struct mii_softc *);
91 static void	pnphy_reset(struct mii_softc *);
92 
93 static const struct mii_phy_funcs pnphy_funcs = {
94 	pnphy_service,
95 	pnphy_status,
96 	pnphy_reset
97 };
98 
99 static int
pnphy_probe(device_t dev)100 pnphy_probe(device_t dev)
101 {
102 	struct mii_attach_args *ma;
103 
104 	ma = device_get_ivars(dev);
105 
106 	/*
107 	 * The dc driver will report the 82c168 vendor and device
108 	 * ID to let us know that it wants us to attach.
109 	 */
110 	if (ma->mii_id1 != DC_VENDORID_LO ||
111 	    ma->mii_id2 != DC_DEVICEID_82C168)
112 		return (ENXIO);
113 
114 	device_set_desc(dev, "PNIC 82c168 media interface");
115 
116 	return (BUS_PROBE_DEFAULT);
117 }
118 
119 static int
pnphy_attach(device_t dev)120 pnphy_attach(device_t dev)
121 {
122 	struct mii_softc *sc;
123 
124 	sc = device_get_softc(dev);
125 
126 	mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE,
127 	    &pnphy_funcs, 0);
128 
129 	sc->mii_capabilities =
130 	    BMSR_100TXFDX | BMSR_100TXHDX | BMSR_10TFDX | BMSR_10THDX;
131 	sc->mii_capabilities &= sc->mii_capmask;
132 	device_printf(dev, " ");
133 	mii_phy_add_media(sc);
134 	printf("\n");
135 
136 	MIIBUS_MEDIAINIT(sc->mii_dev);
137 	return (0);
138 }
139 
140 static int
pnphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)141 pnphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
142 {
143 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
144 
145 	switch (cmd) {
146 	case MII_POLLSTAT:
147 		break;
148 
149 	case MII_MEDIACHG:
150 		/*
151 		 * If the interface is not up, don't do anything.
152 		 */
153 		if ((if_getflags(mii->mii_ifp) & IFF_UP) == 0)
154 			break;
155 
156 		/*
157 		 * Note that auto-negotiation is broken on this chip.
158 		 */
159 		switch (IFM_SUBTYPE(ife->ifm_media)) {
160 		case IFM_100_TX:
161 			mii->mii_media_active = IFM_ETHER | IFM_100_TX;
162 			if ((ife->ifm_media & IFM_FDX) != 0)
163 				mii->mii_media_active |= IFM_FDX;
164 			MIIBUS_STATCHG(sc->mii_dev);
165 			return (0);
166 		case IFM_10_T:
167 			mii->mii_media_active = IFM_ETHER | IFM_10_T;
168 			if ((ife->ifm_media & IFM_FDX) != 0)
169 				mii->mii_media_active |= IFM_FDX;
170 			MIIBUS_STATCHG(sc->mii_dev);
171 			return (0);
172 		default:
173 			return (EINVAL);
174 		}
175 		break;
176 
177 	case MII_TICK:
178 		/*
179 		 * Is the interface even up?
180 		 */
181 		if ((if_getflags(mii->mii_ifp) & IFF_UP) == 0)
182 			return (0);
183 
184 		break;
185 	}
186 
187 	/* Update the media status. */
188 	PHY_STATUS(sc);
189 
190 	/* Callback if something changed. */
191 	mii_phy_update(sc, cmd);
192 	return (0);
193 }
194 
195 static void
pnphy_status(struct mii_softc * sc)196 pnphy_status(struct mii_softc *sc)
197 {
198 	struct mii_data *mii = sc->mii_pdata;
199 	int reg;
200 	struct dc_softc		*dc_sc;
201 
202 	dc_sc = if_getsoftc(mii->mii_ifp);
203 
204 	mii->mii_media_status = IFM_AVALID;
205 	mii->mii_media_active = IFM_ETHER;
206 
207 	reg = CSR_READ_4(dc_sc, DC_ISR);
208 	if (!(reg & DC_ISR_LINKFAIL))
209 		mii->mii_media_status |= IFM_ACTIVE;
210 	reg = CSR_READ_4(dc_sc, DC_NETCFG);
211 	if (reg & DC_NETCFG_SPEEDSEL)
212 		mii->mii_media_active |= IFM_10_T;
213 	else
214 		mii->mii_media_active |= IFM_100_TX;
215 	if (reg & DC_NETCFG_FULLDUPLEX)
216 		mii->mii_media_active |= IFM_FDX;
217 	else
218 		mii->mii_media_active |= IFM_HDX;
219 }
220 
221 static void
pnphy_reset(struct mii_softc * sc __unused)222 pnphy_reset(struct mii_softc *sc __unused)
223 {
224 
225 }
226