]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/dev/mii/rdcphy.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / dev / mii / rdcphy.c
1 /*-
2  * Copyright (c) 2010, Pyun YongHyeon <yongari@FreeBSD.org>
3  * 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 unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 /*
32  * Driver for the RDC Semiconductor R6040 10/100 PHY.
33  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/socket.h>
40 #include <sys/bus.h>
41
42 #include <net/if.h>
43 #include <net/if_media.h>
44
45 #include <dev/mii/mii.h>
46 #include <dev/mii/miivar.h>
47 #include "miidevs.h"
48
49 #include <dev/mii/rdcphyreg.h>
50
51 #include "miibus_if.h"
52
53 static device_probe_t   rdcphy_probe;
54 static device_attach_t  rdcphy_attach;
55
56 struct rdcphy_softc {
57         struct mii_softc mii_sc;
58         int mii_link_tick;
59 #define RDCPHY_MANNEG_TICK      3
60 };
61
62 static device_method_t rdcphy_methods[] = {
63         /* device interface */
64         DEVMETHOD(device_probe,         rdcphy_probe),
65         DEVMETHOD(device_attach,        rdcphy_attach),
66         DEVMETHOD(device_detach,        mii_phy_detach),
67         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
68         DEVMETHOD_END
69 };
70
71 static devclass_t rdcphy_devclass;
72
73 static driver_t rdcphy_driver = {
74         "rdcphy",
75         rdcphy_methods,
76         sizeof(struct rdcphy_softc)
77 };
78
79 DRIVER_MODULE(rdcphy, miibus, rdcphy_driver, rdcphy_devclass, 0, 0);
80
81 static int      rdcphy_service(struct mii_softc *, struct mii_data *, int);
82 static void     rdcphy_status(struct mii_softc *);
83
84 static const struct mii_phydesc rdcphys[] = {
85         MII_PHY_DESC(RDC, R6040),
86         MII_PHY_END
87 };
88
89 static const struct mii_phy_funcs rdcphy_funcs = {
90         rdcphy_service,
91         rdcphy_status,
92         mii_phy_reset
93 };
94
95 static int
96 rdcphy_probe(device_t dev)
97 {
98
99         return (mii_phy_dev_probe(dev, rdcphys, BUS_PROBE_DEFAULT));
100 }
101
102 static int
103 rdcphy_attach(device_t dev)
104 {
105
106         mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &rdcphy_funcs, 1);
107         return (0);
108 }
109
110 static int
111 rdcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
112 {
113         struct rdcphy_softc *rsc;
114         struct ifmedia_entry *ife;
115
116         rsc = (struct rdcphy_softc *)sc;
117         ife = mii->mii_media.ifm_cur;
118
119         switch (cmd) {
120         case MII_POLLSTAT:
121                 break;
122
123         case MII_MEDIACHG:
124                 /*
125                  * If the interface is not up, don't do anything.
126                  */
127                 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
128                         break;
129
130                 mii_phy_setmedia(sc);
131                 switch (IFM_SUBTYPE(ife->ifm_media)) {
132                 case IFM_100_TX:
133                 case IFM_10_T:
134                         /*
135                          * Report fake lost link event to parent
136                          * driver.  This will stop MAC of parent
137                          * driver and make it possible to reconfigure
138                          * MAC after completion of link establishment.
139                          * Note, the parent MAC seems to require
140                          * restarting MAC when underlying any PHY
141                          * configuration was changed even if the
142                          * resolved speed/duplex was not changed at
143                          * all.
144                          */
145                         mii->mii_media_status = 0;
146                         mii->mii_media_active = IFM_ETHER | IFM_NONE;
147                         rsc->mii_link_tick = RDCPHY_MANNEG_TICK;
148                         /* Immediately report link down. */
149                         mii_phy_update(sc, MII_MEDIACHG);
150                         return (0);
151                 default:
152                         break;
153                 }
154                 break;
155
156         case MII_TICK:
157                 if (mii_phy_tick(sc) == EJUSTRETURN)
158                         return (0);
159                 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
160                         /*
161                          * It seems the PHY hardware does not correctly
162                          * report link status changes when manual link
163                          * configuration is in progress.  It is also
164                          * possible for the PHY to complete establishing
165                          * a link within one second such that mii(4)
166                          * did not notice the link change.  To workaround
167                          * the issue, emulate lost link event and wait
168                          * for 3 seconds when manual link configuration
169                          * is in progress.  3 seconds would be long
170                          * enough to absorb transient link flips.
171                          */
172                         if (rsc->mii_link_tick > 0) {
173                                 rsc->mii_link_tick--;
174                                 return (0);
175                         }
176                 }
177                 break;
178         }
179
180         /* Update the media status. */
181         PHY_STATUS(sc);
182
183         /* Callback if something changed. */
184         mii_phy_update(sc, cmd);
185         return (0);
186 }
187
188 static void
189 rdcphy_status(struct mii_softc *sc)
190 {
191         struct mii_data *mii;
192         struct ifmedia_entry *ife;
193         int bmsr, bmcr, physts;
194
195         mii = sc->mii_pdata;
196         ife = mii->mii_media.ifm_cur;
197
198         mii->mii_media_status = IFM_AVALID;
199         mii->mii_media_active = IFM_ETHER;
200
201         bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
202         physts = PHY_READ(sc, MII_RDCPHY_STATUS);
203
204         if ((physts & STATUS_LINK_UP) != 0)
205                 mii->mii_media_status |= IFM_ACTIVE;
206
207         bmcr = PHY_READ(sc, MII_BMCR);
208         if ((bmcr & BMCR_ISO) != 0) {
209                 mii->mii_media_active |= IFM_NONE;
210                 mii->mii_media_status = 0;
211                 return;
212         }
213
214         if ((bmcr & BMCR_LOOP) != 0)
215                 mii->mii_media_active |= IFM_LOOP;
216
217         if ((bmcr & BMCR_AUTOEN) != 0) {
218                 if ((bmsr & BMSR_ACOMP) == 0) {
219                         /* Erg, still trying, I guess... */
220                         mii->mii_media_active |= IFM_NONE;
221                         return;
222                 }
223         }
224
225         switch (physts & STATUS_SPEED_MASK) {
226         case STATUS_SPEED_100:
227                 mii->mii_media_active |= IFM_100_TX;
228                 break;
229         case STATUS_SPEED_10:
230                 mii->mii_media_active |= IFM_10_T;
231                 break;
232         default:
233                 mii->mii_media_active |= IFM_NONE;
234                 return;
235         }
236         if ((physts & STATUS_FULL_DUPLEX) != 0)
237                 mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
238         else
239                 mii->mii_media_active |= IFM_HDX;
240 }