1 /* $OpenBSD: lxtphy.c,v 1.14 2005/02/19 06:00:04 brad Exp $ */
2 /* NetBSD: lxtphy.c,v 1.19 2000/02/02 23:34:57 thorpej Exp */
5 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 * NASA Ames Research Center.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
40 * $FreeBSD: src/sys/dev/mii/lxtphy.c,v 1.1.2.1 2001/06/08 19:58:33 semenu 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 Level One's LXT-970/LXT-971 ethernet 10/100 PHY
74 * datasheet from www.level1.com
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/socket.h>
81 #include <sys/errno.h>
82 #include <sys/module.h>
86 #include <net/if_media.h>
92 #include "lxtphyreg.h"
94 #include "miibus_if.h"
96 static int lxtphy_probe(device_t);
97 static int lxtphy_attach(device_t);
99 static int lxtphy_service(struct mii_softc *, struct mii_data *, int);
100 static void lxtphy_status(struct mii_softc *);
101 static void lxtphy_reset(struct mii_softc *);
102 static void lxtphy_set_tp(struct mii_softc *);
103 static void lxtphy_set_fx(struct mii_softc *);
105 static device_method_t lxtphy_methods[] = {
106 /* device interface */
107 DEVMETHOD(device_probe, lxtphy_probe),
108 DEVMETHOD(device_attach, lxtphy_attach),
109 DEVMETHOD(device_detach, ukphy_detach),
110 DEVMETHOD(device_shutdown, bus_generic_shutdown),
114 static const struct mii_phydesc lxtphys[] = {
115 MII_PHYDESC_ARG(xxLEVEL1, LXT970, lxtphy_status),
116 MII_PHYDESC_ARG(xxLEVEL1a, LXT971, ukphy_status),
120 static devclass_t lxtphy_devclass;
122 static driver_t lxtphy_driver = {
125 sizeof(struct mii_softc)
128 DRIVER_MODULE(lxtphy, miibus, lxtphy_driver, lxtphy_devclass, NULL, NULL);
131 lxtphy_probe(device_t dev)
133 struct mii_attach_args *ma = device_get_ivars(dev);
134 const struct mii_phydesc *mpd;
136 mpd = mii_phy_match(ma, lxtphys);
138 struct mii_softc *sc = device_get_softc(dev);
140 sc->mii_status = mpd->mpd_priv;
141 device_set_desc(dev, mpd->mpd_name);
148 lxtphy_attach(device_t dev)
150 struct mii_softc *sc;
151 struct mii_attach_args *ma;
152 struct mii_data *mii;
154 sc = device_get_softc(dev);
155 ma = device_get_ivars(dev);
156 mii_softc_init(sc, ma);
157 sc->mii_dev = device_get_parent(dev);
158 mii = device_get_softc(sc->mii_dev);
159 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
161 sc->mii_inst = mii->mii_instance;
162 sc->mii_service = lxtphy_service;
163 sc->mii_reset = lxtphy_reset;
165 sc->mii_flags |= MIIF_NOISOLATE;
171 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
172 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
176 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
178 device_printf(dev, " ");
179 if (sc->mii_capabilities & BMSR_MEDIAMASK)
180 mii_phy_add_media(sc);
182 kprintf("no media present");
185 MIIBUS_MEDIAINIT(sc->mii_dev);
190 lxtphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
192 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
198 * If we're not polling our PHY instance, just return.
200 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
206 * If the media indicates a different PHY instance,
209 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
210 reg = PHY_READ(sc, MII_BMCR);
211 PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
216 * If the interface is not up, don't do anything.
218 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
221 if (IFM_SUBTYPE(ife->ifm_media) == IFM_100_FX)
226 mii_phy_set_media(sc);
231 * If we're not currently selected, just return.
233 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
236 if (mii_phy_tick(sc) == EJUSTRETURN)
241 /* Update the media status. */
244 /* Callback if something changed. */
245 mii_phy_update(sc, cmd);
250 lxtphy_status(struct mii_softc *sc)
252 struct mii_data *mii = sc->mii_pdata;
253 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
256 mii->mii_media_status = IFM_AVALID;
257 mii->mii_media_active = IFM_ETHER;
260 * Get link status from the CSR; we need to read the CSR
261 * for media type anyhow, and the link status in the CSR
262 * doens't latch, so fewer register reads are required.
264 csr = PHY_READ(sc, MII_LXTPHY_CSR);
266 mii->mii_media_status |= IFM_ACTIVE;
268 bmcr = PHY_READ(sc, MII_BMCR);
269 if (bmcr & BMCR_ISO) {
270 mii->mii_media_active |= IFM_NONE;
271 mii->mii_media_status = 0;
275 if (bmcr & BMCR_LOOP)
276 mii->mii_media_active |= IFM_LOOP;
278 if (bmcr & BMCR_AUTOEN) {
279 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
280 if ((bmsr & BMSR_ACOMP) == 0) {
281 /* Erg, still trying, I guess... */
282 mii->mii_media_active |= IFM_NONE;
286 mii->mii_media_active |= IFM_100_TX;
288 mii->mii_media_active |= IFM_10_T;
289 if (csr & CSR_DUPLEX)
290 mii->mii_media_active |= IFM_FDX;
292 mii->mii_media_active = ife->ifm_media;
296 lxtphy_set_tp(struct mii_softc *sc)
300 cfg = PHY_READ(sc, MII_LXTPHY_CONFIG);
301 cfg &= ~CONFIG_100BASEFX;
302 PHY_WRITE(sc, MII_LXTPHY_CONFIG, cfg);
306 lxtphy_set_fx(struct mii_softc *sc)
310 cfg = PHY_READ(sc, MII_LXTPHY_CONFIG);
311 cfg |= CONFIG_100BASEFX;
312 PHY_WRITE(sc, MII_LXTPHY_CONFIG, cfg);
316 lxtphy_reset(struct mii_softc *sc)
320 PHY_WRITE(sc, MII_LXTPHY_IER,
321 PHY_READ(sc, MII_LXTPHY_IER) & ~IER_INTEN);