2 * Copyright (c) 2006 Benno Rice. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
29 * Driver for the internal PHY on the SMSC LAN91C111.
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/socket.h>
36 #include <sys/errno.h>
37 #include <sys/module.h>
39 #include <sys/malloc.h>
41 #include <machine/bus.h>
44 #include <net/if_media.h>
46 #include <dev/mii/mii.h>
47 #include <dev/mii/miivar.h>
50 #include "miibus_if.h"
52 static int smcphy_probe(device_t);
53 static int smcphy_attach(device_t);
55 static int smcphy_service(struct mii_softc *, struct mii_data *, int);
56 static int smcphy_reset(struct mii_softc *);
57 static void smcphy_auto(struct mii_softc *, int);
59 static device_method_t smcphy_methods[] = {
60 /* device interface */
61 DEVMETHOD(device_probe, smcphy_probe),
62 DEVMETHOD(device_attach, smcphy_attach),
63 DEVMETHOD(device_detach, mii_phy_detach),
64 DEVMETHOD(device_shutdown, bus_generic_shutdown),
69 static devclass_t smcphy_devclass;
71 static driver_t smcphy_driver = {
74 sizeof(struct mii_softc)
77 DRIVER_MODULE(smcphy, miibus, smcphy_driver, smcphy_devclass, 0, 0);
79 static const struct mii_phydesc smcphys[] = {
80 MII_PHY_DESC(SMSC, LAN83C183),
85 smcphy_probe(device_t dev)
88 return (mii_phy_dev_probe(dev, smcphys, BUS_PROBE_DEFAULT));
92 smcphy_attach(device_t dev)
95 struct mii_attach_args *ma;
98 sc = device_get_softc(dev);
99 ma = device_get_ivars(dev);
100 sc->mii_dev = device_get_parent(dev);
102 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
104 sc->mii_flags = miibus_get_flags(dev);
105 sc->mii_inst = mii->mii_instance++;
106 sc->mii_phy = ma->mii_phyno;
107 sc->mii_service = smcphy_service;
110 sc->mii_flags |= MIIF_NOISOLATE | MIIF_NOLOOP;
112 if (smcphy_reset(sc) != 0) {
113 device_printf(dev, "reset failed\n");
116 /* Mask interrupts, we poll instead. */
117 PHY_WRITE(sc, 0x1e, 0xffc0);
119 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
120 device_printf(dev, " ");
121 mii_phy_add_media(sc);
124 MIIBUS_MEDIAINIT(sc->mii_dev);
125 mii_phy_setmedia(sc);
131 smcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
133 struct ifmedia_entry *ife;
136 ife = mii->mii_media.ifm_cur;
144 * If the interface is not up, don't do anything.
146 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
149 switch (IFM_SUBTYPE(ife->ifm_media)) {
151 smcphy_auto(sc, ife->ifm_media);
155 mii_phy_setmedia(sc);
162 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) {
166 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
170 /* I have no idea why BMCR_ISO gets set. */
171 reg = PHY_READ(sc, MII_BMCR);
172 if (reg & BMCR_ISO) {
173 PHY_WRITE(sc, MII_BMCR, reg & ~BMCR_ISO);
176 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
177 if (reg & BMSR_LINK) {
182 if (++sc->mii_ticks <= MII_ANEGTICKS) {
187 if (smcphy_reset(sc) != 0) {
188 device_printf(sc->mii_dev, "reset failed\n");
190 smcphy_auto(sc, ife->ifm_media);
194 /* Update the media status. */
197 /* Callback if something changed. */
198 mii_phy_update(sc, cmd);
203 smcphy_reset(struct mii_softc *sc)
208 PHY_WRITE(sc, MII_BMCR, BMCR_RESET);
210 for (timeout = 2; timeout > 0; timeout--) {
212 bmcr = PHY_READ(sc, MII_BMCR);
213 if ((bmcr & BMCR_RESET) == 0)
217 if (bmcr & BMCR_RESET) {
221 PHY_WRITE(sc, MII_BMCR, 0x3000);
226 smcphy_auto(struct mii_softc *sc, int media)
230 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) |
232 if ((media & IFM_FLOW) != 0 || (sc->mii_flags & MIIF_FORCEPAUSE) != 0)
234 PHY_WRITE(sc, MII_ANAR, anar);
235 /* Apparently this helps. */
236 anar = PHY_READ(sc, MII_ANAR);
237 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);