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 $
35 * $DragonFly: src/sys/dev/netif/mii_layer/amphy.c,v 1.11 2006/12/22 23:26:20 swildner Exp $
39 * driver for AMD AM79c873 PHYs
40 * This driver also works for the Davicom DM9101 PHY, which appears to
41 * be an AM79c873 workalike.
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/socket.h>
51 #include <net/if_media.h>
59 #include "miibus_if.h"
61 static int amphy_probe (device_t
);
62 static int amphy_attach (device_t
);
64 static device_method_t amphy_methods
[] = {
65 /* device interface */
66 DEVMETHOD(device_probe
, amphy_probe
),
67 DEVMETHOD(device_attach
, amphy_attach
),
68 DEVMETHOD(device_detach
, ukphy_detach
),
69 DEVMETHOD(device_shutdown
, bus_generic_shutdown
),
73 static const struct mii_phydesc amphys
[] = {
74 MII_PHYDESC(xxAMD
, 79C873
),
75 MII_PHYDESC(xxDAVICOM
, DM9101
),
76 MII_PHYDESC(DAVICOM
, DM9102
),
77 MII_PHYDESC(DAVICOM
, DM9601
),
81 static devclass_t amphy_devclass
;
83 static driver_t amphy_driver
= {
86 sizeof(struct mii_softc
)
89 DRIVER_MODULE(amphy
, miibus
, amphy_driver
, amphy_devclass
, 0, 0);
91 static int amphy_service(struct mii_softc
*, struct mii_data
*, int);
92 static void amphy_status(struct mii_softc
*);
95 amphy_probe(device_t dev
)
97 struct mii_attach_args
*ma
= device_get_ivars(dev
);
98 const struct mii_phydesc
*mpd
;
100 mpd
= mii_phy_match(ma
, amphys
);
102 device_set_desc(dev
, mpd
->mpd_name
);
109 amphy_attach(device_t dev
)
111 struct mii_softc
*sc
;
112 struct mii_attach_args
*ma
;
113 struct mii_data
*mii
;
115 sc
= device_get_softc(dev
);
116 ma
= device_get_ivars(dev
);
117 mii_softc_init(sc
, ma
);
118 sc
->mii_dev
= device_get_parent(dev
);
119 mii
= device_get_softc(sc
->mii_dev
);
120 LIST_INSERT_HEAD(&mii
->mii_phys
, sc
, mii_list
);
122 sc
->mii_inst
= mii
->mii_instance
;
123 sc
->mii_service
= amphy_service
;
126 sc
->mii_flags
|= MIIF_NOISOLATE
;
129 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
131 ADD(IFM_MAKEWORD(IFM_ETHER
, IFM_NONE
, 0, sc
->mii_inst
),
134 ADD(IFM_MAKEWORD(IFM_ETHER
, IFM_100_TX
, IFM_LOOP
, sc
->mii_inst
),
140 sc
->mii_capabilities
= PHY_READ(sc
, MII_BMSR
) & ma
->mii_capmask
;
141 device_printf(dev
, " ");
142 if ((sc
->mii_capabilities
& BMSR_MEDIAMASK
) == 0)
143 kprintf("no media present");
145 mii_phy_add_media(sc
);
148 MIIBUS_MEDIAINIT(sc
->mii_dev
);
153 amphy_service(struct mii_softc
*sc
, struct mii_data
*mii
, int cmd
)
155 struct ifmedia_entry
*ife
= mii
->mii_media
.ifm_cur
;
161 * If we're not polling our PHY instance, just return.
163 if (IFM_INST(ife
->ifm_media
) != sc
->mii_inst
)
169 * If the media indicates a different PHY instance,
172 if (IFM_INST(ife
->ifm_media
) != sc
->mii_inst
) {
173 reg
= PHY_READ(sc
, MII_BMCR
);
174 PHY_WRITE(sc
, MII_BMCR
, reg
| BMCR_ISO
);
179 * If the interface is not up, don't do anything.
181 if ((mii
->mii_ifp
->if_flags
& IFF_UP
) == 0)
184 mii_phy_set_media(sc
);
189 * If we're not currently selected, just return.
191 if (IFM_INST(ife
->ifm_media
) != sc
->mii_inst
)
194 if (mii_phy_tick(sc
) == EJUSTRETURN
)
199 /* Update the media status. */
202 /* Callback if something changed. */
203 mii_phy_update(sc
, cmd
);
208 amphy_status(struct mii_softc
*sc
)
210 struct mii_data
*mii
= sc
->mii_pdata
;
211 int bmsr
, bmcr
, par
, anlpar
;
213 mii
->mii_media_status
= IFM_AVALID
;
214 mii
->mii_media_active
= IFM_ETHER
;
216 bmsr
= PHY_READ(sc
, MII_BMSR
) |
217 PHY_READ(sc
, MII_BMSR
);
218 if (bmsr
& BMSR_LINK
)
219 mii
->mii_media_status
|= IFM_ACTIVE
;
221 bmcr
= PHY_READ(sc
, MII_BMCR
);
222 if (bmcr
& BMCR_ISO
) {
223 mii
->mii_media_active
|= IFM_NONE
;
224 mii
->mii_media_status
= 0;
228 if (bmcr
& BMCR_LOOP
)
229 mii
->mii_media_active
|= IFM_LOOP
;
231 if (bmcr
& BMCR_AUTOEN
) {
233 * The PAR status bits are only valid of autonegotiation
234 * has completed (or it's disabled).
236 if ((bmsr
& BMSR_ACOMP
) == 0) {
237 /* Erg, still trying, I guess... */
238 mii
->mii_media_active
|= IFM_NONE
;
242 if (PHY_READ(sc
, MII_ANER
) & ANER_LPAN
) {
243 anlpar
= PHY_READ(sc
, MII_ANAR
) &
244 PHY_READ(sc
, MII_ANLPAR
);
245 if (anlpar
& ANLPAR_T4
)
246 mii
->mii_media_active
|= IFM_100_T4
;
247 else if (anlpar
& ANLPAR_TX_FD
)
248 mii
->mii_media_active
|= IFM_100_TX
|IFM_FDX
;
249 else if (anlpar
& ANLPAR_TX
)
250 mii
->mii_media_active
|= IFM_100_TX
;
251 else if (anlpar
& ANLPAR_10_FD
)
252 mii
->mii_media_active
|= IFM_10_T
|IFM_FDX
;
253 else if (anlpar
& ANLPAR_10
)
254 mii
->mii_media_active
|= IFM_10_T
;
256 mii
->mii_media_active
|= IFM_NONE
;
261 * Link partner is not capable of autonegotiation.
263 par
= PHY_READ(sc
, MII_AMPHY_DSCSR
);
264 if (par
& DSCSR_100FDX
)
265 mii
->mii_media_active
|= IFM_100_TX
|IFM_FDX
;
266 else if (par
& DSCSR_100HDX
)
267 mii
->mii_media_active
|= IFM_100_TX
;
268 else if (par
& DSCSR_10FDX
)
269 mii
->mii_media_active
|= IFM_10_T
|IFM_HDX
;
270 else if (par
& DSCSR_10HDX
)
271 mii
->mii_media_active
|= IFM_10_T
;
273 mii
->mii_media_active
= mii
->mii_media
.ifm_cur
->ifm_media
;