2 * Copyright (c) 1997, 1998, 1999
3 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
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
30 * THE POSSIBILITY OF SUCH DAMAGE.
32 * $FreeBSD: src/sys/dev/mii/dcphy.c,v 1.2.2.2 2000/10/14 00:44:40 wpaul Exp $
33 * $DragonFly: src/sys/dev/netif/mii_layer/dcphy.c,v 1.5 2004/09/18 19:32:59 dillon Exp $
35 * $FreeBSD: src/sys/dev/mii/dcphy.c,v 1.2.2.2 2000/10/14 00:44:40 wpaul Exp $
39 * Pseudo-driver for internal NWAY support on DEC 21143 and workalike
40 * controllers. Technically we're abusing the miibus code to handle
41 * media selection and NWAY support here since there is no MII
42 * interface. However the logical operations are roughly the same,
43 * and the alternative is to create a fake MII interface in the driver,
44 * which is harder to do.
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/socket.h>
51 #include <sys/errno.h>
52 #include <sys/module.h>
56 #include <net/if_arp.h>
57 #include <net/if_media.h>
63 #include <machine/clock.h>
64 #include <machine/bus_pio.h>
65 #include <machine/bus_memio.h>
66 #include <machine/bus.h>
67 #include <machine/resource.h>
70 #include <bus/pci/pcivar.h>
71 #include "../dc/if_dcreg.h"
73 #include "miibus_if.h"
75 #define DC_SETBIT(sc, reg, x) \
76 CSR_WRITE_4(sc, reg, \
77 CSR_READ_4(sc, reg) | x)
79 #define DC_CLRBIT(sc, reg, x) \
80 CSR_WRITE_4(sc, reg, \
81 CSR_READ_4(sc, reg) & ~x)
83 #define MIIF_AUTOTIMEOUT 0x0004
86 * This is the subsystem ID for the built-in 21143 ethernet
87 * in several Compaq Presario systems. Apparently these are
88 * 10Mbps only, so we need to treat them specially.
90 #define COMPAQ_PRESARIO_ID 0xb0bb0e11
92 static int dcphy_probe (device_t);
93 static int dcphy_attach (device_t);
94 static int dcphy_detach (device_t);
96 static device_method_t dcphy_methods[] = {
97 /* device interface */
98 DEVMETHOD(device_probe, dcphy_probe),
99 DEVMETHOD(device_attach, dcphy_attach),
100 DEVMETHOD(device_detach, dcphy_detach),
101 DEVMETHOD(device_shutdown, bus_generic_shutdown),
105 static devclass_t dcphy_devclass;
107 static driver_t dcphy_driver = {
110 sizeof(struct mii_softc)
113 DRIVER_MODULE(dcphy, miibus, dcphy_driver, dcphy_devclass, 0, 0);
115 int dcphy_service (struct mii_softc *, struct mii_data *, int);
116 void dcphy_status (struct mii_softc *);
117 static int dcphy_auto (struct mii_softc *, int);
118 static void dcphy_reset (struct mii_softc *);
120 static int dcphy_probe(dev)
123 struct mii_attach_args *ma;
125 ma = device_get_ivars(dev);
128 * The dc driver will report the 21143 vendor and device
129 * ID to let us know that it wants us to attach.
131 if (ma->mii_id1 != DC_VENDORID_DEC ||
132 ma->mii_id2 != DC_DEVICEID_21143)
135 device_set_desc(dev, "Intel 21143 NWAY media interface");
140 static int dcphy_attach(dev)
143 struct mii_softc *sc;
144 struct mii_attach_args *ma;
145 struct mii_data *mii;
146 struct dc_softc *dc_sc;
148 sc = device_get_softc(dev);
149 ma = device_get_ivars(dev);
151 sc->mii_dev = device_get_parent(dev);
152 mii = device_get_softc(sc->mii_dev);
153 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
155 sc->mii_inst = mii->mii_instance;
156 sc->mii_phy = ma->mii_phyno;
157 sc->mii_service = dcphy_service;
160 sc->mii_flags |= MIIF_NOISOLATE;
163 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
165 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
169 dc_sc = mii->mii_ifp->if_softc;
170 CSR_WRITE_4(dc_sc, DC_10BTSTAT, 0);
171 CSR_WRITE_4(dc_sc, DC_10BTCTRL, 0);
173 switch(pci_read_config(device_get_parent(sc->mii_dev),
175 case COMPAQ_PRESARIO_ID:
176 /* Example of how to only allow 10Mbps modes. */
177 sc->mii_capabilities = BMSR_ANEG|BMSR_10TFDX|BMSR_10THDX;
180 if (dc_sc->dc_pmode == DC_PMODE_SIA) {
181 sc->mii_capabilities =
182 BMSR_ANEG|BMSR_10TFDX|BMSR_10THDX;
184 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP,
185 sc->mii_inst), BMCR_LOOP|BMCR_S100);
187 sc->mii_capabilities =
188 BMSR_ANEG|BMSR_100TXFDX|BMSR_100TXHDX|
189 BMSR_10TFDX|BMSR_10THDX;
194 sc->mii_capabilities &= ma->mii_capmask;
195 device_printf(dev, " ");
196 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
197 printf("no media present");
199 mii_add_media(mii, sc->mii_capabilities, sc->mii_inst);
203 MIIBUS_MEDIAINIT(sc->mii_dev);
207 static int dcphy_detach(dev)
210 struct mii_softc *sc;
211 struct mii_data *mii;
213 sc = device_get_softc(dev);
214 mii = device_get_softc(device_get_parent(dev));
216 LIST_REMOVE(sc, mii_list);
222 dcphy_service(sc, mii, cmd)
223 struct mii_softc *sc;
224 struct mii_data *mii;
227 struct dc_softc *dc_sc;
228 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
232 dc_sc = mii->mii_ifp->if_softc;
237 * If we're not polling our PHY instance, just return.
239 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
246 * If the media indicates a different PHY instance,
249 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
254 * If the interface is not up, don't do anything.
256 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
260 mii->mii_media_active = IFM_NONE;
261 mode = CSR_READ_4(dc_sc, DC_NETCFG);
262 mode &= ~(DC_NETCFG_FULLDUPLEX|DC_NETCFG_PORTSEL|
263 DC_NETCFG_PCS|DC_NETCFG_SCRAMBLER|DC_NETCFG_SPEEDSEL);
265 switch (IFM_SUBTYPE(ife->ifm_media)) {
268 sc->mii_flags &= ~MIIF_DOINGAUTO;
269 (void) dcphy_auto(sc, 0);
273 * XXX Not supported as a manual setting right now.
278 DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
279 mode |= DC_NETCFG_PORTSEL|DC_NETCFG_PCS|
281 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
282 mode |= DC_NETCFG_FULLDUPLEX;
284 mode &= ~DC_NETCFG_FULLDUPLEX;
285 CSR_WRITE_4(dc_sc, DC_NETCFG, mode);
288 DC_CLRBIT(dc_sc, DC_SIARESET, DC_SIA_RESET);
289 DC_CLRBIT(dc_sc, DC_10BTCTRL, 0xFFFF);
290 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
291 DC_SETBIT(dc_sc, DC_10BTCTRL, 0x7F3D);
293 DC_SETBIT(dc_sc, DC_10BTCTRL, 0x7F3F);
294 DC_SETBIT(dc_sc, DC_SIARESET, DC_SIA_RESET);
295 DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
296 mode &= ~DC_NETCFG_PORTSEL;
297 mode |= DC_NETCFG_SPEEDSEL;
298 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
299 mode |= DC_NETCFG_FULLDUPLEX;
301 mode &= ~DC_NETCFG_FULLDUPLEX;
302 CSR_WRITE_4(dc_sc, DC_NETCFG, mode);
312 * If we're not currently selected, just return.
314 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
318 * Only used for autonegotiation.
320 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
324 * Is the interface even up?
326 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
329 reg = CSR_READ_4(dc_sc, DC_10BTSTAT) &
330 (DC_TSTAT_LS10|DC_TSTAT_LS100);
332 if (!(reg & DC_TSTAT_LS10) || !(reg & DC_TSTAT_LS100))
336 * Only retry autonegotiation every 5 seconds.
338 if (++sc->mii_ticks != 50)
342 /*if (DC_IS_INTEL(dc_sc))*/
343 sc->mii_flags &= ~MIIF_DOINGAUTO;
349 /* Update the media status. */
352 /* Callback if something changed. */
353 if (sc->mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
354 MIIBUS_STATCHG(sc->mii_dev);
355 sc->mii_active = mii->mii_media_active;
362 struct mii_softc *sc;
364 struct mii_data *mii = sc->mii_pdata;
365 int reg, anlpar, tstat = 0;
366 struct dc_softc *dc_sc;
368 dc_sc = mii->mii_ifp->if_softc;
370 mii->mii_media_status = IFM_AVALID;
371 mii->mii_media_active = IFM_ETHER;
373 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
376 reg = CSR_READ_4(dc_sc, DC_10BTSTAT) &
377 (DC_TSTAT_LS10|DC_TSTAT_LS100);
379 if (!(reg & DC_TSTAT_LS10) || !(reg & DC_TSTAT_LS100))
380 mii->mii_media_status |= IFM_ACTIVE;
382 if (CSR_READ_4(dc_sc, DC_10BTCTRL) & DC_TCTL_AUTONEGENBL) {
383 /* Erg, still trying, I guess... */
384 tstat = CSR_READ_4(dc_sc, DC_10BTSTAT);
385 if ((tstat & DC_TSTAT_ANEGSTAT) != DC_ASTAT_AUTONEGCMP) {
386 if ((DC_IS_MACRONIX(dc_sc) || DC_IS_PNICII(dc_sc)) &&
387 (tstat & DC_TSTAT_ANEGSTAT) == DC_ASTAT_DISABLE)
389 mii->mii_media_active |= IFM_NONE;
393 if (tstat & DC_TSTAT_LP_CAN_NWAY) {
394 anlpar = tstat >> 16;
395 if (anlpar & ANLPAR_T4 &&
396 sc->mii_capabilities & BMSR_100TXHDX)
397 mii->mii_media_active |= IFM_100_T4;
398 else if (anlpar & ANLPAR_TX_FD &&
399 sc->mii_capabilities & BMSR_100TXFDX)
400 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
401 else if (anlpar & ANLPAR_TX &&
402 sc->mii_capabilities & BMSR_100TXHDX)
403 mii->mii_media_active |= IFM_100_TX;
404 else if (anlpar & ANLPAR_10_FD)
405 mii->mii_media_active |= IFM_10_T|IFM_FDX;
406 else if (anlpar & ANLPAR_10)
407 mii->mii_media_active |= IFM_10_T;
409 mii->mii_media_active |= IFM_NONE;
410 if (DC_IS_INTEL(dc_sc))
411 DC_CLRBIT(dc_sc, DC_10BTCTRL,
412 DC_TCTL_AUTONEGENBL);
416 * If the other side doesn't support NWAY, then the
417 * best we can do is determine if we have a 10Mbps or
418 * 100Mbps link. There's no way to know if the link
419 * is full or half duplex, so we default to half duplex
420 * and hope that the user is clever enough to manually
421 * change the media settings if we're wrong.
423 if (!(reg & DC_TSTAT_LS100))
424 mii->mii_media_active |= IFM_100_TX;
425 else if (!(reg & DC_TSTAT_LS10))
426 mii->mii_media_active |= IFM_10_T;
428 mii->mii_media_active |= IFM_NONE;
429 if (DC_IS_INTEL(dc_sc))
430 DC_CLRBIT(dc_sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
436 if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_SPEEDSEL)
437 mii->mii_media_active |= IFM_10_T;
439 mii->mii_media_active |= IFM_100_TX;
440 if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_FULLDUPLEX)
441 mii->mii_media_active |= IFM_FDX;
447 dcphy_auto(mii, waitfor)
448 struct mii_softc *mii;
454 sc = mii->mii_pdata->mii_ifp->if_softc;
456 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0) {
457 DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
458 DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
459 DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
460 if (mii->mii_capabilities & BMSR_100TXHDX)
461 CSR_WRITE_4(sc, DC_10BTCTRL, 0x3FFFF);
463 CSR_WRITE_4(sc, DC_10BTCTRL, 0xFFFF);
464 DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
465 DC_SETBIT(sc, DC_10BTCTRL, DC_TCTL_AUTONEGENBL);
466 DC_SETBIT(sc, DC_10BTSTAT, DC_ASTAT_TXDISABLE);
470 /* Wait 500ms for it to complete. */
471 for (i = 0; i < 500; i++) {
472 if ((CSR_READ_4(sc, DC_10BTSTAT) & DC_TSTAT_ANEGSTAT)
473 == DC_ASTAT_AUTONEGCMP)
478 * Don't need to worry about clearing MIIF_DOINGAUTO.
479 * If that's set, a timeout is pending, and it will
486 * Just let it finish asynchronously. This is for the benefit of
487 * the tick handler driving autonegotiation. Don't want 500ms
488 * delays all the time while the system is running!
490 if ((mii->mii_flags & MIIF_DOINGAUTO) == 0)
491 mii->mii_flags |= MIIF_DOINGAUTO;
498 struct mii_softc *mii;
502 sc = mii->mii_pdata->mii_ifp->if_softc;
504 DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
506 DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);