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 SEEQ 80220 and 84220.
30 * (Originally developed for the internal PHY on the SMSC LAN91C111.)
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>
37 #include <sys/errno.h>
38 #include <sys/module.h>
40 #include <sys/malloc.h>
42 #include <machine/bus.h>
45 #include <net/if_media.h>
47 #include <dev/mii/mii.h>
48 #include <dev/mii/miivar.h>
51 #include "miibus_if.h"
53 static int smcphy_probe(device_t);
54 static int smcphy_attach(device_t);
56 static int smcphy_service(struct mii_softc *, struct mii_data *, int);
57 static void smcphy_reset(struct mii_softc *);
58 static void smcphy_auto(struct mii_softc *, int);
59 static void smcphy_status(struct mii_softc *);
61 static device_method_t smcphy_methods[] = {
62 /* device interface */
63 DEVMETHOD(device_probe, smcphy_probe),
64 DEVMETHOD(device_attach, smcphy_attach),
65 DEVMETHOD(device_detach, mii_phy_detach),
66 DEVMETHOD(device_shutdown, bus_generic_shutdown),
70 static devclass_t smcphy_devclass;
72 static driver_t smcphy_driver = {
75 sizeof(struct mii_softc)
78 DRIVER_MODULE(smcphy, miibus, smcphy_driver, smcphy_devclass, 0, 0);
80 static const struct mii_phydesc smcphys[] = {
81 MII_PHY_DESC(SEEQ, 80220),
82 MII_PHY_DESC(SEEQ, 84220),
86 static const struct mii_phy_funcs smcphy80220_funcs = {
92 static const struct mii_phy_funcs smcphy_funcs = {
99 smcphy_probe(device_t dev)
102 return (mii_phy_dev_probe(dev, smcphys, BUS_PROBE_DEFAULT));
106 smcphy_attach(device_t dev)
108 struct mii_softc *sc;
109 struct mii_attach_args *ma;
110 const struct mii_phy_funcs *mpf;
112 sc = device_get_softc(dev);
113 ma = device_get_ivars(dev);
114 if (MII_MODEL(ma->mii_id2) == MII_MODEL_SEEQ_80220)
115 mpf = &smcphy80220_funcs;
118 mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE, mpf, 1);
119 mii_phy_setmedia(sc);
125 smcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
127 struct ifmedia_entry *ife;
130 ife = mii->mii_media.ifm_cur;
137 switch (IFM_SUBTYPE(ife->ifm_media)) {
139 smcphy_auto(sc, ife->ifm_media);
143 mii_phy_setmedia(sc);
150 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
154 /* I have no idea why BMCR_ISO gets set. */
155 reg = PHY_READ(sc, MII_BMCR);
156 if (reg & BMCR_ISO) {
157 PHY_WRITE(sc, MII_BMCR, reg & ~BMCR_ISO);
160 reg = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
161 if (reg & BMSR_LINK) {
166 if (++sc->mii_ticks <= MII_ANEGTICKS) {
172 smcphy_auto(sc, ife->ifm_media);
176 /* Update the media status. */
179 /* Callback if something changed. */
180 mii_phy_update(sc, cmd);
185 smcphy_reset(struct mii_softc *sc)
190 PHY_WRITE(sc, MII_BMCR, BMCR_RESET);
192 for (timeout = 2; timeout > 0; timeout--) {
194 bmcr = PHY_READ(sc, MII_BMCR);
195 if ((bmcr & BMCR_RESET) == 0)
199 if (bmcr & BMCR_RESET)
200 device_printf(sc->mii_dev, "reset failed\n");
202 PHY_WRITE(sc, MII_BMCR, 0x3000);
204 /* Mask interrupts, we poll instead. */
205 PHY_WRITE(sc, 0x1e, 0xffc0);
209 smcphy_auto(struct mii_softc *sc, int media)
213 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA;
214 if ((media & IFM_FLOW) != 0 || (sc->mii_flags & MIIF_FORCEPAUSE) != 0)
216 PHY_WRITE(sc, MII_ANAR, anar);
217 /* Apparently this helps. */
218 anar = PHY_READ(sc, MII_ANAR);
219 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
223 smcphy_status(struct mii_softc *sc)
225 struct mii_data *mii;
226 uint32_t bmcr, bmsr, status;
229 mii->mii_media_status = IFM_AVALID;
230 mii->mii_media_active = IFM_ETHER;
232 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
233 if ((bmsr & BMSR_LINK) != 0)
234 mii->mii_media_status |= IFM_ACTIVE;
236 bmcr = PHY_READ(sc, MII_BMCR);
237 if ((bmcr & BMCR_ISO) != 0) {
238 mii->mii_media_active |= IFM_NONE;
239 mii->mii_media_status = 0;
243 if ((bmcr & BMCR_LOOP) != 0)
244 mii->mii_media_active |= IFM_LOOP;
246 if ((bmcr & BMCR_AUTOEN) != 0) {
247 if ((bmsr & BMSR_ACOMP) == 0) {
248 /* Erg, still trying, I guess... */
249 mii->mii_media_active |= IFM_NONE;
254 status = PHY_READ(sc, 0x12);
256 mii->mii_media_active |= IFM_100_TX;
258 mii->mii_media_active |= IFM_10_T;
260 mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
262 mii->mii_media_active |= IFM_HDX;