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.
36 * Pseudo-driver for media selection on the Lite-On PNIC 82c168
37 * chip. The NWAY support on this chip is horribly broken, so we
38 * only support manual mode selection. This is lame, but getting
39 * NWAY to work right is amazingly difficult.
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/socket.h>
46 #include <sys/errno.h>
48 #include <sys/module.h>
49 #include <sys/mutex.h>
53 #include <net/if_arp.h>
54 #include <net/if_media.h>
56 #include <dev/mii/mii.h>
57 #include <dev/mii/miivar.h>
58 #include <dev/mii/miidevs.h>
60 #include <machine/bus_pio.h>
61 #include <machine/bus_memio.h>
62 #include <machine/bus.h>
63 #include <machine/resource.h>
66 #include <pci/if_dcreg.h>
68 #include "miibus_if.h"
71 static const char rcsid[] =
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 static int pnphy_probe __P((device_t));
84 static int pnphy_attach __P((device_t));
85 static int pnphy_detach __P((device_t));
87 static device_method_t pnphy_methods[] = {
88 /* device interface */
89 DEVMETHOD(device_probe, pnphy_probe),
90 DEVMETHOD(device_attach, pnphy_attach),
91 DEVMETHOD(device_detach, pnphy_detach),
92 DEVMETHOD(device_shutdown, bus_generic_shutdown),
96 static devclass_t pnphy_devclass;
98 static driver_t pnphy_driver = {
101 sizeof(struct mii_softc)
104 DRIVER_MODULE(pnphy, miibus, pnphy_driver, pnphy_devclass, 0, 0);
106 static int pnphy_service __P((struct mii_softc *, struct mii_data *, int));
107 static void pnphy_status __P((struct mii_softc *));
109 static int pnphy_probe(dev)
112 struct mii_attach_args *ma;
114 ma = device_get_ivars(dev);
117 * The dc driver will report the 82c168 vendor and device
118 * ID to let us know that it wants us to attach.
120 if (ma->mii_id1 != DC_VENDORID_LO ||
121 ma->mii_id2 != DC_DEVICEID_82C168)
124 device_set_desc(dev, "PNIC 82c168 media interface");
129 static int pnphy_attach(dev)
132 struct mii_softc *sc;
133 struct mii_attach_args *ma;
134 struct mii_data *mii;
136 sc = device_get_softc(dev);
137 ma = device_get_ivars(dev);
138 sc->mii_dev = device_get_parent(dev);
139 mii = device_get_softc(sc->mii_dev);
140 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
142 sc->mii_inst = mii->mii_instance;
143 sc->mii_phy = ma->mii_phyno;
144 sc->mii_service = pnphy_service;
147 sc->mii_flags |= MIIF_NOISOLATE;
150 #define ADD(m, c) ifmedia_add(&mii->mii_media, (m), (c), NULL)
152 sc->mii_capabilities =
153 BMSR_100TXFDX|BMSR_100TXHDX|BMSR_10TFDX|BMSR_10THDX;
154 sc->mii_capabilities &= ma->mii_capmask;
155 device_printf(dev, " ");
156 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
157 printf("no media present");
159 mii_add_media(mii, sc->mii_capabilities, sc->mii_inst);
161 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, sc->mii_inst),
164 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
165 BMCR_LOOP|BMCR_S100);
169 MIIBUS_MEDIAINIT(sc->mii_dev);
173 static int pnphy_detach(dev)
176 struct mii_softc *sc;
177 struct mii_data *mii;
179 sc = device_get_softc(dev);
180 mii = device_get_softc(device_get_parent(dev));
182 LIST_REMOVE(sc, mii_list);
188 pnphy_service(sc, mii, cmd)
189 struct mii_softc *sc;
190 struct mii_data *mii;
193 struct dc_softc *dc_sc;
194 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
196 dc_sc = mii->mii_ifp->if_softc;
201 * If we're not polling our PHY instance, just return.
203 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
210 * If the media indicates a different PHY instance,
213 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
217 * If the interface is not up, don't do anything.
219 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
224 switch (IFM_SUBTYPE(ife->ifm_media)) {
226 /* NWAY is busted on this chip */
229 * XXX Not supported as a manual setting right now.
233 mii->mii_media_active = IFM_ETHER|IFM_100_TX;
234 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
235 mii->mii_media_active |= IFM_FDX;
236 MIIBUS_STATCHG(sc->mii_dev);
240 mii->mii_media_active = IFM_ETHER|IFM_10_T;
241 if ((ife->ifm_media & IFM_GMASK) == IFM_FDX)
242 mii->mii_media_active |= IFM_FDX;
243 MIIBUS_STATCHG(sc->mii_dev);
254 * If we're not currently selected, just return.
256 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
260 * Is the interface even up?
262 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
268 /* Update the media status. */
271 /* Callback if something changed. */
272 mii_phy_update(sc, cmd);
278 struct mii_softc *sc;
280 struct mii_data *mii = sc->mii_pdata;
282 struct dc_softc *dc_sc;
284 dc_sc = mii->mii_ifp->if_softc;
286 mii->mii_media_status = IFM_AVALID;
287 mii->mii_media_active = IFM_ETHER;
289 reg = CSR_READ_4(dc_sc, DC_ISR);
291 if (!(reg & DC_ISR_LINKFAIL))
292 mii->mii_media_status |= IFM_ACTIVE;
294 if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_SPEEDSEL)
295 mii->mii_media_active |= IFM_10_T;
297 mii->mii_media_active |= IFM_100_TX;
298 if (CSR_READ_4(dc_sc, DC_NETCFG) & DC_NETCFG_FULLDUPLEX)
299 mii->mii_media_active |= IFM_FDX;