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