2 * Copyright (c) 2001-2003, Shunsuke Akiyama <akiyama@FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/dev/mii/ruephy.c,v 1.1.4.1 2003/07/30 13:57:35 akiyama Exp $
30 * driver for RealTek RTL8150 internal PHY
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/socket.h>
41 #include <net/if_media.h>
43 #include <dev/netif/mii_layer/mii.h>
44 #include <dev/netif/mii_layer/miivar.h>
45 #include <dev/netif/mii_layer/ruephyreg.h>
47 #include "miibus_if.h"
49 static int ruephy_probe(device_t);
50 static int ruephy_attach(device_t);
52 static device_method_t ruephy_methods[] = {
53 /* device interface */
54 DEVMETHOD(device_probe, ruephy_probe),
55 DEVMETHOD(device_attach, ruephy_attach),
56 DEVMETHOD(device_detach, ukphy_detach),
57 DEVMETHOD(device_shutdown, bus_generic_shutdown),
61 static devclass_t ruephy_devclass;
63 static driver_t ruephy_driver = {
66 sizeof(struct mii_softc)
69 DRIVER_MODULE(ruephy, miibus, ruephy_driver, ruephy_devclass, NULL, NULL);
71 static int ruephy_service(struct mii_softc *, struct mii_data *, int);
72 static void ruephy_reset(struct mii_softc *);
73 static void ruephy_status(struct mii_softc *);
76 ruephy_probe(device_t dev)
78 struct mii_attach_args *ma;
81 ma = device_get_ivars(dev);
82 parent = device_get_parent(device_get_parent(dev));
85 * RealTek RTL8150 PHY doesn't have vendor/device ID registers:
86 * the rue driver fakes up a return value of all zeros.
88 if (MII_OUI(ma->mii_id1, ma->mii_id2) != 0 ||
89 MII_MODEL(ma->mii_id2) != 0)
93 * Make sure the parent is an 'rue'.
95 if (strcmp(device_get_name(parent), "rue") != 0)
98 device_set_desc(dev, "RealTek RTL8150 internal media interface");
104 ruephy_attach(device_t dev)
106 struct mii_softc *sc;
107 struct mii_attach_args *ma;
108 struct mii_data *mii;
110 sc = device_get_softc(dev);
111 ma = device_get_ivars(dev);
112 mii_softc_init(sc, ma);
113 sc->mii_dev = device_get_parent(dev);
114 mii = device_get_softc(sc->mii_dev);
117 * The RealTek PHY can never be isolated, so never allow non-zero
120 if (mii->mii_instance != 0) {
121 device_printf(dev, "ignoring this PHY, non-zero instance\n");
125 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
127 sc->mii_inst = mii->mii_instance;
128 sc->mii_service = ruephy_service;
129 sc->mii_reset = ruephy_reset;
133 sc->mii_flags |= MIIF_NOISOLATE;
137 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
139 device_printf(dev, " ");
140 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
141 kprintf("no media present");
143 mii_phy_add_media(sc);
146 MIIBUS_MEDIAINIT(sc->mii_dev);
151 ruephy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
153 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
157 * We can't isolate the RealTek RTL8150 PHY,
158 * so it has to be the only one!
160 KASSERT(IFM_INST(ife->ifm_media) == sc->mii_inst,
161 ("ruephy_service: can't isolate RealTek RTL8150 PHY"));
169 * If the interface is not up, don't do anything.
171 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
174 if (IFM_SUBTYPE(ife->ifm_media) == IFM_100_T4) {
176 * XXX Not supported as a manual setting right now.
181 mii_phy_set_media(sc);
186 * Is the interface even up?
188 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
192 * Only used for autonegotiation.
194 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
198 * Check to see if we have link. If we do, we don't
199 * need to restart the autonegotiation process. Read
200 * the MSR twice in case it's latched.
202 reg = PHY_READ(sc, RUEPHY_MII_MSR) |
203 PHY_READ(sc, RUEPHY_MII_MSR);
204 if (reg & RUEPHY_MSR_LINK) {
210 * Only retry autonegotiation every 5 seconds.
212 if (++sc->mii_ticks != 5)
218 if (mii_phy_auto(sc, 0) == EJUSTRETURN)
223 /* Update the media status. */
226 /* Callback if something changed. */
227 mii_phy_update(sc, cmd);
232 ruephy_reset(struct mii_softc *sc)
238 * XXX RealTek RTL8150 PHY doesn't set the BMCR properly after
239 * XXX reset, which breaks autonegotiation.
241 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN);
245 ruephy_status(struct mii_softc *sc)
247 struct mii_data *mii = sc->mii_pdata;
250 mii->mii_media_status = IFM_AVALID;
251 mii->mii_media_active = IFM_ETHER;
253 msr = PHY_READ(sc, RUEPHY_MII_MSR) | PHY_READ(sc, RUEPHY_MII_MSR);
254 if (msr & RUEPHY_MSR_LINK)
255 mii->mii_media_status |= IFM_ACTIVE;
257 bmcr = PHY_READ(sc, MII_BMCR);
258 if (bmcr & BMCR_ISO) {
259 mii->mii_media_active |= IFM_NONE;
260 mii->mii_media_status = 0;
264 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
266 if (bmcr & BMCR_AUTOEN) {
267 if ((bmsr & BMSR_ACOMP) == 0) {
268 /* Erg, still trying, I guess... */
269 mii->mii_media_active |= IFM_NONE;
273 if (msr & RUEPHY_MSR_SPEED100)
274 mii->mii_media_active |= IFM_100_TX;
276 mii->mii_media_active |= IFM_10_T;
278 if (msr & RUEPHY_MSR_DUPLEX)
279 mii->mii_media_active |= IFM_FDX;
281 mii->mii_media_active = mii->mii_media.ifm_cur->ifm_media;