1 /* $NetBSD: acphy.c,v 1.16 2006/03/29 07:05:24 thorpej Exp $ */
4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
39 * $FreeBSD: src/sys/dev/mii/acphy.c,v 1.2.2.2 2002/10/21 21:20:19 semenu Exp $
40 * $DragonFly: src/sys/dev/netif/mii_layer/acphy.c,v 1.10 2006/12/22 23:26:20 swildner Exp $
44 * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by Manuel Bouyer.
57 * 4. The name of the author may not be used to endorse or promote products
58 * derived from this software without specific prior written permission.
60 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
61 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
62 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
63 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
64 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
65 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
66 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
67 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
69 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 * Driver for Altima AC101 10/100 PHY
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/kernel.h>
79 #include <sys/socket.h>
80 #include <sys/errno.h>
81 #include <sys/module.h>
85 #include <net/if_media.h>
93 #include "miibus_if.h"
95 static int acphy_probe (device_t);
96 static int acphy_attach (device_t);
98 static device_method_t acphy_methods[] = {
99 /* device interface */
100 DEVMETHOD(device_probe, acphy_probe),
101 DEVMETHOD(device_attach, acphy_attach),
102 DEVMETHOD(device_detach, ukphy_detach),
103 DEVMETHOD(device_shutdown, bus_generic_shutdown),
107 static const struct mii_phydesc acphys[] = {
108 MII_PHYDESC(xxALTIMA, AC101),
109 MII_PHYDESC(xxALTIMA, AC101L),
110 MII_PHYDESC(xxALTIMA, AC_UNKNOWN),
111 MII_PHYDESC(xxALTIMA, Am79C875),
115 static devclass_t acphy_devclass;
117 static driver_t acphy_driver = {
120 sizeof(struct mii_softc)
123 DRIVER_MODULE(acphy, miibus, acphy_driver, acphy_devclass, 0, 0);
125 static int acphy_service(struct mii_softc *, struct mii_data *, int);
126 static void acphy_reset(struct mii_softc *);
127 static void acphy_status(struct mii_softc *);
130 acphy_probe(device_t dev)
132 struct mii_attach_args *ma = device_get_ivars(dev);
133 const struct mii_phydesc *mpd;
135 mpd = mii_phy_match(ma, acphys);
137 device_set_desc(dev, mpd->mpd_name);
144 acphy_attach(device_t dev)
146 struct mii_softc *sc;
147 struct mii_attach_args *ma;
148 struct mii_data *mii;
150 sc = device_get_softc(dev);
151 ma = device_get_ivars(dev);
152 mii_softc_init(sc, ma);
153 sc->mii_dev = device_get_parent(dev);
154 mii = device_get_softc(sc->mii_dev);
155 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
157 sc->mii_inst = mii->mii_instance;
158 sc->mii_service = acphy_service;
159 sc->mii_reset = acphy_reset;
161 sc->mii_flags |= MIIF_NOISOLATE;
167 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
168 device_printf(dev, " ");
169 if (sc->mii_capabilities & BMSR_MEDIAMASK)
170 mii_phy_add_media(sc);
172 kprintf("no media present");
175 MIIBUS_MEDIAINIT(sc->mii_dev);
180 acphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
182 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
186 * If we're not selected, then do nothing, just isolate and power
187 * down, if changing media.
189 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
190 if (cmd == MII_MEDIACHG) {
191 reg = PHY_READ(sc, MII_BMCR);
192 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO | BMCR_PDOWN);
204 * If the interface is not up, don't do anything.
206 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
209 mii_phy_set_media(sc);
214 * Is the interface even up?
216 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
220 * Only used for autonegotiation.
222 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
226 * This PHY's autonegotiation doesn't need to be kicked.
231 /* Update the media status. */
234 /* Callback if something changed. */
235 mii_phy_update(sc, cmd);
240 acphy_status(struct mii_softc *sc)
242 struct mii_data *mii = sc->mii_pdata;
243 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
246 mii->mii_media_status = IFM_AVALID;
247 mii->mii_media_active = IFM_ETHER;
249 bmsr = PHY_READ(sc, MII_BMSR) |
250 PHY_READ(sc, MII_BMSR);
251 if (bmsr & BMSR_LINK)
252 mii->mii_media_status |= IFM_ACTIVE;
254 bmcr = PHY_READ(sc, MII_BMCR);
255 if (bmcr & BMCR_ISO) {
256 mii->mii_media_active |= IFM_NONE;
257 mii->mii_media_status = 0;
261 if (bmcr & BMCR_LOOP)
262 mii->mii_media_active |= IFM_LOOP;
264 if (bmcr & BMCR_AUTOEN) {
267 if ((bmsr & BMSR_ACOMP) == 0) {
268 /* Erg, still trying, I guess... */
269 mii->mii_media_active |= IFM_NONE;
273 diag = PHY_READ(sc, MII_ACPHY_DIAG);
274 if (diag & AC_DIAG_SPEED)
275 mii->mii_media_active |= IFM_100_TX;
277 mii->mii_media_active |= IFM_10_T;
279 if (diag & AC_DIAG_DUPLEX)
280 mii->mii_media_active |= IFM_FDX;
282 mii->mii_media_active = ife->ifm_media;
287 acphy_reset(struct mii_softc *sc)
290 PHY_WRITE(sc, MII_ACPHY_INT, 0);