xref: /freebsd/sys/dev/usb/net/ruephy.c (revision 5bf5ca772c6de2d53344a78cf461447cc322ccea)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>.
5  * 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 /*
34  * driver for RealTek RTL8150 internal PHY
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/socket.h>
43 #include <sys/bus.h>
44 
45 #include <net/if.h>
46 #include <net/if_arp.h>
47 #include <net/if_media.h>
48 
49 #include <dev/mii/mii.h>
50 #include <dev/mii/miivar.h>
51 #include "miidevs.h"
52 
53 #include <dev/usb/net/ruephyreg.h>
54 
55 #include "miibus_if.h"
56 
57 static int ruephy_probe(device_t);
58 static int ruephy_attach(device_t);
59 
60 static device_method_t ruephy_methods[] = {
61 	/* device interface */
62 	DEVMETHOD(device_probe,		ruephy_probe),
63 	DEVMETHOD(device_attach,	ruephy_attach),
64 	DEVMETHOD(device_detach,	mii_phy_detach),
65 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
66 	DEVMETHOD_END
67 };
68 
69 static devclass_t ruephy_devclass;
70 
71 static driver_t ruephy_driver = {
72 	.name = "ruephy",
73 	.methods = ruephy_methods,
74 	.size = sizeof(struct mii_softc)
75 };
76 
77 DRIVER_MODULE(ruephy, miibus, ruephy_driver, ruephy_devclass, 0, 0);
78 
79 static int ruephy_service(struct mii_softc *, struct mii_data *, int);
80 static void ruephy_reset(struct mii_softc *);
81 static void ruephy_status(struct mii_softc *);
82 
83 /*
84  * The RealTek RTL8150 internal PHY doesn't have vendor/device ID
85  * registers; rue(4) fakes up a return value of all zeros.
86  */
87 static const struct mii_phydesc ruephys[] = {
88 	{ 0, 0, "RealTek RTL8150 internal media interface" },
89 	MII_PHY_END
90 };
91 
92 static const struct mii_phy_funcs ruephy_funcs = {
93 	ruephy_service,
94 	ruephy_status,
95 	ruephy_reset
96 };
97 
98 static int
99 ruephy_probe(device_t dev)
100 {
101 
102 	if (strcmp(device_get_name(device_get_parent(device_get_parent(dev))),
103 	    "rue") == 0)
104 		return (mii_phy_dev_probe(dev, ruephys, BUS_PROBE_DEFAULT));
105 	return (ENXIO);
106 }
107 
108 static int
109 ruephy_attach(device_t dev)
110 {
111 
112 	mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE,
113 	    &ruephy_funcs, 1);
114 	return (0);
115 }
116 
117 static int
118 ruephy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
119 {
120 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
121 	int reg;
122 
123 	switch (cmd) {
124 	case MII_POLLSTAT:
125 		break;
126 
127 	case MII_MEDIACHG:
128 		mii_phy_setmedia(sc);
129 		break;
130 
131 	case MII_TICK:
132 		/*
133 		 * Only used for autonegotiation.
134 		 */
135 		if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
136 			break;
137 
138 		/*
139 		 * Check to see if we have link.  If we do, we don't
140 		 * need to restart the autonegotiation process.  Read
141 		 * the MSR twice in case it's latched.
142 		 */
143 		reg = PHY_READ(sc, RUEPHY_MII_MSR) |
144 		    PHY_READ(sc, RUEPHY_MII_MSR);
145 		if (reg & RUEPHY_MSR_LINK)
146 			break;
147 
148 		/* Only retry autonegotiation every mii_anegticks seconds. */
149 		if (sc->mii_ticks <= sc->mii_anegticks)
150 			break;
151 
152 		sc->mii_ticks = 0;
153 		PHY_RESET(sc);
154 		if (mii_phy_auto(sc) == EJUSTRETURN)
155 			return (0);
156 		break;
157 	}
158 
159 	/* Update the media status. */
160 	PHY_STATUS(sc);
161 
162 	/* Callback if something changed. */
163 	mii_phy_update(sc, cmd);
164 
165 	return (0);
166 }
167 
168 static void
169 ruephy_reset(struct mii_softc *sc)
170 {
171 
172 	mii_phy_reset(sc);
173 
174 	/*
175 	 * XXX RealTek RTL8150 PHY doesn't set the BMCR properly after
176 	 * XXX reset, which breaks autonegotiation.
177 	 */
178 	PHY_WRITE(sc, MII_BMCR, (BMCR_S100 | BMCR_AUTOEN | BMCR_FDX));
179 }
180 
181 static void
182 ruephy_status(struct mii_softc *phy)
183 {
184 	struct mii_data *mii = phy->mii_pdata;
185 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
186 	int bmsr, bmcr, msr;
187 
188 	mii->mii_media_status = IFM_AVALID;
189 	mii->mii_media_active = IFM_ETHER;
190 
191 	msr = PHY_READ(phy, RUEPHY_MII_MSR) | PHY_READ(phy, RUEPHY_MII_MSR);
192 	if (msr & RUEPHY_MSR_LINK)
193 		mii->mii_media_status |= IFM_ACTIVE;
194 
195 	bmcr = PHY_READ(phy, MII_BMCR);
196 	if (bmcr & BMCR_ISO) {
197 		mii->mii_media_active |= IFM_NONE;
198 		mii->mii_media_status = 0;
199 		return;
200 	}
201 
202 	bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
203 	if (bmcr & BMCR_AUTOEN) {
204 		if ((bmsr & BMSR_ACOMP) == 0) {
205 			/* Erg, still trying, I guess... */
206 			mii->mii_media_active |= IFM_NONE;
207 			return;
208 		}
209 
210 		if (msr & RUEPHY_MSR_SPEED100)
211 			mii->mii_media_active |= IFM_100_TX;
212 		else
213 			mii->mii_media_active |= IFM_10_T;
214 
215 		if (msr & RUEPHY_MSR_DUPLEX)
216 			mii->mii_media_active |=
217 			    IFM_FDX | mii_phy_flowstatus(phy);
218 		else
219 			mii->mii_media_active |= IFM_HDX;
220 	} else
221 		mii->mii_media_active = ife->ifm_media;
222 }
223