1 /* $NetBSD: tlphy.c,v 1.18 1999/05/14 11:40:28 drochner 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.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1997 Manuel Bouyer. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
61 * Driver for Texas Instruments's ThunderLAN PHYs
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/socket.h>
68 #include <sys/errno.h>
69 #include <sys/module.h>
71 #include <sys/malloc.h>
73 #include <machine/bus.h>
76 #include <net/if_media.h>
78 #include <dev/mii/mii.h>
79 #include <dev/mii/miivar.h>
82 #include <dev/mii/tlphyreg.h>
84 #include "miibus_if.h"
87 struct mii_softc sc_mii; /* generic PHY */
91 static int tlphy_probe(device_t);
92 static int tlphy_attach(device_t);
94 static device_method_t tlphy_methods[] = {
95 /* device interface */
96 DEVMETHOD(device_probe, tlphy_probe),
97 DEVMETHOD(device_attach, tlphy_attach),
98 DEVMETHOD(device_detach, mii_phy_detach),
99 DEVMETHOD(device_shutdown, bus_generic_shutdown),
103 static devclass_t tlphy_devclass;
105 static driver_t tlphy_driver = {
108 sizeof(struct tlphy_softc)
111 DRIVER_MODULE(tlphy, miibus, tlphy_driver, tlphy_devclass, 0, 0);
113 static int tlphy_service(struct mii_softc *, struct mii_data *, int);
114 static int tlphy_auto(struct tlphy_softc *);
115 static void tlphy_acomp(struct tlphy_softc *);
116 static void tlphy_status(struct tlphy_softc *);
118 static const struct mii_phydesc tlphys[] = {
119 MII_PHY_DESC(xxTI, TLAN10T),
124 tlphy_probe(device_t dev)
127 if (strcmp(device_get_name(device_get_parent(device_get_parent(dev))),
130 return (mii_phy_dev_probe(dev, tlphys, BUS_PROBE_DEFAULT));
134 tlphy_attach(device_t dev)
137 struct tlphy_softc *sc;
138 struct mii_softc *other;
139 struct mii_attach_args *ma;
140 struct mii_data *mii;
141 const char *sep = "";
142 int capmask, devs, i;
144 sc = device_get_softc(dev);
145 ma = device_get_ivars(dev);
146 sc->sc_mii.mii_dev = device_get_parent(dev);
147 mii = device_get_softc(sc->sc_mii.mii_dev);
148 LIST_INSERT_HEAD(&mii->mii_phys, &sc->sc_mii, mii_list);
150 sc->sc_mii.mii_flags = miibus_get_flags(dev);
151 sc->sc_mii.mii_inst = mii->mii_instance;
152 sc->sc_mii.mii_phy = ma->mii_phyno;
153 sc->sc_mii.mii_service = tlphy_service;
154 sc->sc_mii.mii_pdata = mii;
156 sc->sc_mii.mii_flags |= MIIF_NOMANPAUSE;
159 * Note that if we're on a device that also supports 100baseTX,
160 * we are not going to want to use the built-in 10baseT port,
161 * since there will be another PHY on the MII wired up to the
164 capmask = BMSR_DEFCAPMASK;
165 if (mii->mii_instance &&
166 device_get_children(sc->sc_mii.mii_dev, &devlist, &devs) == 0) {
167 for (i = 0; i < devs; i++) {
168 if (devlist[i] != dev) {
169 other = device_get_softc(devlist[i]);
170 capmask &= ~other->mii_capabilities;
174 free(devlist, M_TEMP);
179 mii_phy_reset(&sc->sc_mii);
181 sc->sc_mii.mii_capabilities =
182 PHY_READ(&sc->sc_mii, MII_BMSR) & capmask;
184 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
186 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_LOOP, sc->sc_mii.mii_inst),
189 #define PRINT(s) printf("%s%s", sep, s); sep = ", "
191 if ((sc->sc_mii.mii_flags & (MIIF_MACPRIV0 | MIIF_MACPRIV1)) != 0 &&
192 (sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) != 0)
193 device_printf(dev, " ");
194 if ((sc->sc_mii.mii_flags & MIIF_MACPRIV0) != 0) {
195 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_2, 0, sc->sc_mii.mii_inst),
197 PRINT("10base2/BNC");
199 if ((sc->sc_mii.mii_flags & MIIF_MACPRIV1) != 0) {
200 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_5, 0, sc->sc_mii.mii_inst),
202 PRINT("10base5/AUI");
204 if ((sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) != 0) {
206 mii_phy_add_media(&sc->sc_mii);
208 if ((sc->sc_mii.mii_flags & (MIIF_MACPRIV0 | MIIF_MACPRIV1)) != 0 &&
209 (sc->sc_mii.mii_capabilities & BMSR_MEDIAMASK) != 0)
213 MIIBUS_MEDIAINIT(sc->sc_mii.mii_dev);
218 tlphy_service(struct mii_softc *self, struct mii_data *mii, int cmd)
220 struct tlphy_softc *sc = (struct tlphy_softc *)self;
221 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
224 if (sc->sc_need_acomp)
233 * If the interface is not up, don't do anything.
235 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
238 switch (IFM_SUBTYPE(ife->ifm_media)) {
241 * The ThunderLAN PHY doesn't self-configure after
242 * an autonegotiation cycle, so there's no such
243 * thing as "already in auto mode".
245 (void)tlphy_auto(sc);
249 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
250 PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, CTRL_AUISEL);
254 PHY_WRITE(&sc->sc_mii, MII_TLPHY_CTRL, 0);
256 mii_phy_setmedia(&sc->sc_mii);
262 * Is the interface even up?
264 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
268 * Only used for autonegotiation.
270 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
274 * Check to see if we have link. If we do, we don't
275 * need to restart the autonegotiation process. Read
276 * the BMSR twice in case it's latched.
278 * XXX WHAT ABOUT CHECKING LINK ON THE BNC/AUI?!
280 reg = PHY_READ(&sc->sc_mii, MII_BMSR) |
281 PHY_READ(&sc->sc_mii, MII_BMSR);
286 * Only retry autonegotiation every 5 seconds.
288 if (++sc->sc_mii.mii_ticks <= MII_ANEGTICKS)
291 sc->sc_mii.mii_ticks = 0;
292 mii_phy_reset(&sc->sc_mii);
293 (void)tlphy_auto(sc);
297 /* Update the media status. */
300 /* Callback if something changed. */
301 mii_phy_update(&sc->sc_mii, cmd);
306 tlphy_status(struct tlphy_softc *sc)
308 struct mii_data *mii = sc->sc_mii.mii_pdata;
309 int bmsr, bmcr, tlctrl;
311 mii->mii_media_status = IFM_AVALID;
312 mii->mii_media_active = IFM_ETHER;
314 bmcr = PHY_READ(&sc->sc_mii, MII_BMCR);
315 if (bmcr & BMCR_ISO) {
316 mii->mii_media_active |= IFM_NONE;
317 mii->mii_media_status = 0;
321 tlctrl = PHY_READ(&sc->sc_mii, MII_TLPHY_CTRL);
322 if (tlctrl & CTRL_AUISEL) {
323 mii->mii_media_status = 0;
324 mii->mii_media_active = mii->mii_media.ifm_cur->ifm_media;
328 bmsr = PHY_READ(&sc->sc_mii, MII_BMSR) |
329 PHY_READ(&sc->sc_mii, MII_BMSR);
330 if (bmsr & BMSR_LINK)
331 mii->mii_media_status |= IFM_ACTIVE;
333 if (bmcr & BMCR_LOOP)
334 mii->mii_media_active |= IFM_LOOP;
337 * Grr, braindead ThunderLAN PHY doesn't have any way to
338 * tell which media is actually active. (Note it also
339 * doesn't self-configure after autonegotiation.) We
340 * just have to report what's in the BMCR.
343 mii->mii_media_active |=
344 IFM_FDX | mii_phy_flowstatus(&sc->sc_mii);
346 mii->mii_media_active |= IFM_HDX;
347 mii->mii_media_active |= IFM_10_T;
351 tlphy_auto(struct tlphy_softc *sc)
355 switch ((error = mii_phy_auto(&sc->sc_mii))) {
358 * Just assume we're not in full-duplex mode.
359 * XXX Check link and try AUI/BNC?
361 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);
365 /* Flag that we need to program when it completes. */
366 sc->sc_need_acomp = 1;
377 tlphy_acomp(struct tlphy_softc *sc)
381 sc->sc_need_acomp = 0;
384 * Grr, braindead ThunderLAN PHY doesn't self-configure
385 * after autonegotiation. We have to do it ourselves
386 * based on the link partner status.
389 aner = PHY_READ(&sc->sc_mii, MII_ANER);
390 if (aner & ANER_LPAN) {
391 anlpar = PHY_READ(&sc->sc_mii, MII_ANLPAR) &
392 PHY_READ(&sc->sc_mii, MII_ANAR);
393 if (anlpar & ANAR_10_FD) {
394 PHY_WRITE(&sc->sc_mii, MII_BMCR, BMCR_FDX);
398 PHY_WRITE(&sc->sc_mii, MII_BMCR, 0);