]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/dev/mii/amphy.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / dev / mii / amphy.c
1 /*-
2  * Copyright (c) 1997, 1998, 1999
3  *      Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
19  *
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.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 /*
37  * driver for AMD AM79c873 PHYs
38  * This driver also works for Davicom DM910{1,2} PHYs, which appear
39  * to be AM79c873 workalikes.
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/module.h>
46 #include <sys/socket.h>
47 #include <sys/bus.h>
48
49 #include <net/if.h>
50 #include <net/if_media.h>
51
52 #include <dev/mii/mii.h>
53 #include <dev/mii/miivar.h>
54 #include "miidevs.h"
55
56 #include <dev/mii/amphyreg.h>
57
58 #include "miibus_if.h"
59
60 static int amphy_probe(device_t);
61 static int amphy_attach(device_t);
62
63 static device_method_t amphy_methods[] = {
64         /* device interface */
65         DEVMETHOD(device_probe,         amphy_probe),
66         DEVMETHOD(device_attach,        amphy_attach),
67         DEVMETHOD(device_detach,        mii_phy_detach),
68         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
69         { 0, 0 }
70 };
71
72 static devclass_t amphy_devclass;
73
74 static driver_t amphy_driver = {
75         "amphy",
76         amphy_methods,
77         sizeof(struct mii_softc)
78 };
79
80 DRIVER_MODULE(amphy, miibus, amphy_driver, amphy_devclass, 0, 0);
81
82 static int      amphy_service(struct mii_softc *, struct mii_data *, int);
83 static void     amphy_status(struct mii_softc *);
84
85 static const struct mii_phydesc amphys[] = {
86         MII_PHY_DESC(DAVICOM, DM9102),
87         MII_PHY_DESC(xxAMD, 79C873),
88         MII_PHY_DESC(xxDAVICOM, DM9101),
89         MII_PHY_END
90 };
91
92 static int
93 amphy_probe(device_t dev)
94 {
95
96         return (mii_phy_dev_probe(dev, amphys, BUS_PROBE_DEFAULT));
97 }
98
99 static int
100 amphy_attach(device_t dev)
101 {
102         struct mii_softc *sc;
103         struct mii_attach_args *ma;
104         struct mii_data *mii;
105
106         sc = device_get_softc(dev);
107         ma = device_get_ivars(dev);
108         sc->mii_dev = device_get_parent(dev);
109         mii = device_get_softc(sc->mii_dev);
110         LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
111
112         sc->mii_inst = mii->mii_instance;
113         sc->mii_phy = ma->mii_phyno;
114         sc->mii_service = amphy_service;
115         sc->mii_pdata = mii;
116
117         mii->mii_instance++;
118
119 #define ADD(m, c)       ifmedia_add(&mii->mii_media, (m), (c), NULL)
120
121 #if 0
122         ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
123             MII_MEDIA_100_TX);
124 #endif
125
126         mii_phy_reset(sc);
127
128         sc->mii_capabilities =
129             PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
130         device_printf(dev, " ");
131         mii_phy_add_media(sc);
132         printf("\n");
133 #undef ADD
134         MIIBUS_MEDIAINIT(sc->mii_dev);
135         return (0);
136 }
137
138 static int
139 amphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
140 {
141         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
142         int reg;
143
144         switch (cmd) {
145         case MII_POLLSTAT:
146                 /*
147                  * If we're not polling our PHY instance, just return.
148                  */
149                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
150                         return (0);
151                 break;
152
153         case MII_MEDIACHG:
154                 /*
155                  * If the media indicates a different PHY instance,
156                  * isolate ourselves.
157                  */
158                 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
159                         reg = PHY_READ(sc, MII_BMCR);
160                         PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
161                         return (0);
162                 }
163
164                 /*
165                  * If the interface is not up, don't do anything.
166                  */
167                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
168                         break;
169
170                 mii_phy_setmedia(sc);
171                 break;
172
173         case MII_TICK:
174                 /*
175                  * If we're not currently selected, just return.
176                  */
177                 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
178                         return (0);
179                 if (mii_phy_tick(sc) == EJUSTRETURN)
180                         return (0);
181                 break;
182         }
183
184         /* Update the media status. */
185         amphy_status(sc);
186
187         /* Callback if something changed. */
188         mii_phy_update(sc, cmd);
189         return (0);
190 }
191
192 static void
193 amphy_status(struct mii_softc *sc)
194 {
195         struct mii_data *mii = sc->mii_pdata;
196         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
197         int bmsr, bmcr, par, anlpar;
198
199         mii->mii_media_status = IFM_AVALID;
200         mii->mii_media_active = IFM_ETHER;
201
202         bmsr = PHY_READ(sc, MII_BMSR) |
203             PHY_READ(sc, MII_BMSR);
204         if (bmsr & BMSR_LINK)
205                 mii->mii_media_status |= IFM_ACTIVE;
206
207         bmcr = PHY_READ(sc, MII_BMCR);
208         if (bmcr & BMCR_ISO) {
209                 mii->mii_media_active |= IFM_NONE;
210                 mii->mii_media_status = 0;
211                 return;
212         }
213
214         if (bmcr & BMCR_LOOP)
215                 mii->mii_media_active |= IFM_LOOP;
216
217         if (bmcr & BMCR_AUTOEN) {
218                 /*
219                  * The PAR status bits are only valid if autonegotiation
220                  * has completed (or it's disabled).
221                  */
222                 if ((bmsr & BMSR_ACOMP) == 0) {
223                         /* Erg, still trying, I guess... */
224                         mii->mii_media_active |= IFM_NONE;
225                         return;
226                 }
227
228                 if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
229                         anlpar = PHY_READ(sc, MII_ANAR) &
230                             PHY_READ(sc, MII_ANLPAR);
231                         if (anlpar & ANLPAR_TX_FD)
232                                 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
233                         else if (anlpar & ANLPAR_T4)
234                                 mii->mii_media_active |= IFM_100_T4;
235                         else if (anlpar & ANLPAR_TX)
236                                 mii->mii_media_active |= IFM_100_TX;
237                         else if (anlpar & ANLPAR_10_FD)
238                                 mii->mii_media_active |= IFM_10_T|IFM_FDX;
239                         else if (anlpar & ANLPAR_10)
240                                 mii->mii_media_active |= IFM_10_T;
241                         else
242                                 mii->mii_media_active |= IFM_NONE;
243                         return;
244                 }
245
246                 /*
247                  * Link partner is not capable of autonegotiation.
248                  */
249                 par = PHY_READ(sc, MII_AMPHY_DSCSR);
250                 if (par & DSCSR_100FDX)
251                         mii->mii_media_active |= IFM_100_TX|IFM_FDX;
252                 else if (par & DSCSR_100HDX)
253                         mii->mii_media_active |= IFM_100_TX;
254                 else if (par & DSCSR_10FDX)
255                         mii->mii_media_active |= IFM_10_T|IFM_HDX;
256                 else if (par & DSCSR_10HDX)
257                         mii->mii_media_active |= IFM_10_T;
258         } else
259                 mii->mii_media_active = ife->ifm_media;
260 }