1 /* $OpenBSD: amphy.c,v 1.13 2005/05/27 08:04:15 brad Exp $ */
4 * Copyright (c) 1997, 1998, 1999
5 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
34 * $FreeBSD: src/sys/dev/mii/amphy.c,v 1.2.2.2 2002/11/08 21:53:49 semenu Exp $
38 * driver for AMD AM79c873 PHYs
39 * This driver also works for the Davicom DM9101 PHY, which appears to
40 * be an AM79c873 workalike.
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/socket.h>
50 #include <net/if_media.h>
58 #include "miibus_if.h"
60 static int amphy_probe (device_t);
61 static int amphy_attach (device_t);
63 static device_method_t amphy_methods[] = {
64 /* device interface */
65 DEVMETHOD(device_probe, amphy_probe),
66 DEVMETHOD(device_attach, amphy_attach),
67 DEVMETHOD(device_detach, ukphy_detach),
68 DEVMETHOD(device_shutdown, bus_generic_shutdown),
72 static const struct mii_phydesc amphys[] = {
73 MII_PHYDESC(xxAMD, 79C873),
74 MII_PHYDESC(xxDAVICOM, DM9101),
75 MII_PHYDESC(DAVICOM, DM9102),
76 MII_PHYDESC(DAVICOM, DM9601),
80 static devclass_t amphy_devclass;
82 static driver_t amphy_driver = {
85 sizeof(struct mii_softc)
88 DRIVER_MODULE(amphy, miibus, amphy_driver, amphy_devclass, NULL, NULL);
90 static int amphy_service(struct mii_softc *, struct mii_data *, int);
91 static void amphy_status(struct mii_softc *);
94 amphy_probe(device_t dev)
96 struct mii_attach_args *ma = device_get_ivars(dev);
97 const struct mii_phydesc *mpd;
99 mpd = mii_phy_match(ma, amphys);
101 device_set_desc(dev, mpd->mpd_name);
108 amphy_attach(device_t dev)
110 struct mii_softc *sc;
111 struct mii_attach_args *ma;
112 struct mii_data *mii;
114 sc = device_get_softc(dev);
115 ma = device_get_ivars(dev);
116 mii_softc_init(sc, ma);
117 sc->mii_dev = device_get_parent(dev);
118 mii = device_get_softc(sc->mii_dev);
119 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
121 sc->mii_inst = mii->mii_instance;
122 sc->mii_service = amphy_service;
125 sc->mii_flags |= MIIF_NOISOLATE;
128 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
130 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
133 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
139 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
140 device_printf(dev, " ");
141 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
142 kprintf("no media present");
144 mii_phy_add_media(sc);
147 MIIBUS_MEDIAINIT(sc->mii_dev);
152 amphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
154 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
160 * If we're not polling our PHY instance, just return.
162 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
168 * If the media indicates a different PHY instance,
171 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
172 reg = PHY_READ(sc, MII_BMCR);
173 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
178 * If the interface is not up, don't do anything.
180 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
183 mii_phy_set_media(sc);
188 * If we're not currently selected, just return.
190 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
193 if (mii_phy_tick(sc) == EJUSTRETURN)
198 /* Update the media status. */
201 /* Callback if something changed. */
202 mii_phy_update(sc, cmd);
207 amphy_status(struct mii_softc *sc)
209 struct mii_data *mii = sc->mii_pdata;
210 int bmsr, bmcr, par, anlpar;
212 mii->mii_media_status = IFM_AVALID;
213 mii->mii_media_active = IFM_ETHER;
215 bmsr = PHY_READ(sc, MII_BMSR) |
216 PHY_READ(sc, MII_BMSR);
217 if (bmsr & BMSR_LINK)
218 mii->mii_media_status |= IFM_ACTIVE;
220 bmcr = PHY_READ(sc, MII_BMCR);
221 if (bmcr & BMCR_ISO) {
222 mii->mii_media_active |= IFM_NONE;
223 mii->mii_media_status = 0;
227 if (bmcr & BMCR_LOOP)
228 mii->mii_media_active |= IFM_LOOP;
230 if (bmcr & BMCR_AUTOEN) {
232 * The PAR status bits are only valid of autonegotiation
233 * has completed (or it's disabled).
235 if ((bmsr & BMSR_ACOMP) == 0) {
236 /* Erg, still trying, I guess... */
237 mii->mii_media_active |= IFM_NONE;
241 if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
242 anlpar = PHY_READ(sc, MII_ANAR) &
243 PHY_READ(sc, MII_ANLPAR);
244 if (anlpar & ANLPAR_T4)
245 mii->mii_media_active |= IFM_100_T4;
246 else if (anlpar & ANLPAR_TX_FD)
247 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
248 else if (anlpar & ANLPAR_TX)
249 mii->mii_media_active |= IFM_100_TX;
250 else if (anlpar & ANLPAR_10_FD)
251 mii->mii_media_active |= IFM_10_T|IFM_FDX;
252 else if (anlpar & ANLPAR_10)
253 mii->mii_media_active |= IFM_10_T;
255 mii->mii_media_active |= IFM_NONE;
260 * Link partner is not capable of autonegotiation.
262 par = PHY_READ(sc, MII_AMPHY_DSCSR);
263 if (par & DSCSR_100FDX)
264 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
265 else if (par & DSCSR_100HDX)
266 mii->mii_media_active |= IFM_100_TX;
267 else if (par & DSCSR_10FDX)
268 mii->mii_media_active |= IFM_10_T|IFM_HDX;
269 else if (par & DSCSR_10HDX)
270 mii->mii_media_active |= IFM_10_T;
272 mii->mii_media_active = mii->mii_media.ifm_cur->ifm_media;