2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2010, Pyun YongHyeon <yongari@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Driver for the RDC Semiconductor R6040 10/100 PHY.
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/socket.h>
45 #include <net/if_media.h>
47 #include <dev/mii/mii.h>
48 #include <dev/mii/miivar.h>
51 #include <dev/mii/rdcphyreg.h>
53 #include "miibus_if.h"
55 static device_probe_t rdcphy_probe;
56 static device_attach_t rdcphy_attach;
59 struct mii_softc mii_sc;
61 #define RDCPHY_MANNEG_TICK 3
64 static device_method_t rdcphy_methods[] = {
65 /* device interface */
66 DEVMETHOD(device_probe, rdcphy_probe),
67 DEVMETHOD(device_attach, rdcphy_attach),
68 DEVMETHOD(device_detach, mii_phy_detach),
69 DEVMETHOD(device_shutdown, bus_generic_shutdown),
73 static devclass_t rdcphy_devclass;
75 static driver_t rdcphy_driver = {
78 sizeof(struct rdcphy_softc)
81 DRIVER_MODULE(rdcphy, miibus, rdcphy_driver, rdcphy_devclass, 0, 0);
83 static int rdcphy_service(struct mii_softc *, struct mii_data *, int);
84 static void rdcphy_status(struct mii_softc *);
86 static const struct mii_phydesc rdcphys[] = {
87 MII_PHY_DESC(RDC, R6040),
91 static const struct mii_phy_funcs rdcphy_funcs = {
98 rdcphy_probe(device_t dev)
101 return (mii_phy_dev_probe(dev, rdcphys, BUS_PROBE_DEFAULT));
105 rdcphy_attach(device_t dev)
108 mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &rdcphy_funcs, 1);
113 rdcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
115 struct rdcphy_softc *rsc;
116 struct ifmedia_entry *ife;
118 rsc = (struct rdcphy_softc *)sc;
119 ife = mii->mii_media.ifm_cur;
126 mii_phy_setmedia(sc);
127 switch (IFM_SUBTYPE(ife->ifm_media)) {
131 * Report fake lost link event to parent
132 * driver. This will stop MAC of parent
133 * driver and make it possible to reconfigure
134 * MAC after completion of link establishment.
135 * Note, the parent MAC seems to require
136 * restarting MAC when underlying any PHY
137 * configuration was changed even if the
138 * resolved speed/duplex was not changed at
141 mii->mii_media_status = 0;
142 mii->mii_media_active = IFM_ETHER | IFM_NONE;
143 rsc->mii_link_tick = RDCPHY_MANNEG_TICK;
144 /* Immediately report link down. */
145 mii_phy_update(sc, MII_MEDIACHG);
153 if (mii_phy_tick(sc) == EJUSTRETURN)
155 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
157 * It seems the PHY hardware does not correctly
158 * report link status changes when manual link
159 * configuration is in progress. It is also
160 * possible for the PHY to complete establishing
161 * a link within one second such that mii(4)
162 * did not notice the link change. To workaround
163 * the issue, emulate lost link event and wait
164 * for 3 seconds when manual link configuration
165 * is in progress. 3 seconds would be long
166 * enough to absorb transient link flips.
168 if (rsc->mii_link_tick > 0) {
169 rsc->mii_link_tick--;
176 /* Update the media status. */
179 /* Callback if something changed. */
180 mii_phy_update(sc, cmd);
185 rdcphy_status(struct mii_softc *sc)
187 struct mii_data *mii;
188 int bmsr, bmcr, physts;
192 mii->mii_media_status = IFM_AVALID;
193 mii->mii_media_active = IFM_ETHER;
195 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
196 physts = PHY_READ(sc, MII_RDCPHY_STATUS);
198 if ((physts & STATUS_LINK_UP) != 0)
199 mii->mii_media_status |= IFM_ACTIVE;
201 bmcr = PHY_READ(sc, MII_BMCR);
202 if ((bmcr & BMCR_ISO) != 0) {
203 mii->mii_media_active |= IFM_NONE;
204 mii->mii_media_status = 0;
208 if ((bmcr & BMCR_LOOP) != 0)
209 mii->mii_media_active |= IFM_LOOP;
211 if ((bmcr & BMCR_AUTOEN) != 0) {
212 if ((bmsr & BMSR_ACOMP) == 0) {
213 /* Erg, still trying, I guess... */
214 mii->mii_media_active |= IFM_NONE;
219 switch (physts & STATUS_SPEED_MASK) {
220 case STATUS_SPEED_100:
221 mii->mii_media_active |= IFM_100_TX;
223 case STATUS_SPEED_10:
224 mii->mii_media_active |= IFM_10_T;
227 mii->mii_media_active |= IFM_NONE;
230 if ((physts & STATUS_FULL_DUPLEX) != 0)
231 mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
233 mii->mii_media_active |= IFM_HDX;