1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2010, Pyun YongHyeon <yongari@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 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. 16 * 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 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 /* 34 * Driver for the RDC Semiconductor R6040 10/100 PHY. 35 */ 36 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> 42 #include <sys/bus.h> 43 44 #include <net/if.h> 45 #include <net/if_media.h> 46 47 #include <dev/mii/mii.h> 48 #include <dev/mii/miivar.h> 49 #include "miidevs.h" 50 51 #include <dev/mii/rdcphyreg.h> 52 53 #include "miibus_if.h" 54 55 static device_probe_t rdcphy_probe; 56 static device_attach_t rdcphy_attach; 57 58 struct rdcphy_softc { 59 struct mii_softc mii_sc; 60 int mii_link_tick; 61 #define RDCPHY_MANNEG_TICK 3 62 }; 63 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), 70 DEVMETHOD_END 71 }; 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, 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_DESC(RDC, R6040_2), 87 MII_PHY_END 88 }; 89 90 static const struct mii_phy_funcs rdcphy_funcs = { 91 rdcphy_service, 92 rdcphy_status, 93 mii_phy_reset 94 }; 95 96 static int 97 rdcphy_probe(device_t dev) 98 { 99 100 return (mii_phy_dev_probe(dev, rdcphys, BUS_PROBE_DEFAULT)); 101 } 102 103 static int 104 rdcphy_attach(device_t dev) 105 { 106 107 mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &rdcphy_funcs, 1); 108 return (0); 109 } 110 111 static int 112 rdcphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 113 { 114 struct rdcphy_softc *rsc; 115 struct ifmedia_entry *ife; 116 117 rsc = (struct rdcphy_softc *)sc; 118 ife = mii->mii_media.ifm_cur; 119 120 switch (cmd) { 121 case MII_POLLSTAT: 122 break; 123 124 case MII_MEDIACHG: 125 mii_phy_setmedia(sc); 126 switch (IFM_SUBTYPE(ife->ifm_media)) { 127 case IFM_100_TX: 128 case IFM_10_T: 129 /* 130 * Report fake lost link event to parent 131 * driver. This will stop MAC of parent 132 * driver and make it possible to reconfigure 133 * MAC after completion of link establishment. 134 * Note, the parent MAC seems to require 135 * restarting MAC when underlying any PHY 136 * configuration was changed even if the 137 * resolved speed/duplex was not changed at 138 * all. 139 */ 140 mii->mii_media_status = 0; 141 mii->mii_media_active = IFM_ETHER | IFM_NONE; 142 rsc->mii_link_tick = RDCPHY_MANNEG_TICK; 143 /* Immediately report link down. */ 144 mii_phy_update(sc, MII_MEDIACHG); 145 return (0); 146 default: 147 break; 148 } 149 break; 150 151 case MII_TICK: 152 if (mii_phy_tick(sc) == EJUSTRETURN) 153 return (0); 154 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) { 155 /* 156 * It seems the PHY hardware does not correctly 157 * report link status changes when manual link 158 * configuration is in progress. It is also 159 * possible for the PHY to complete establishing 160 * a link within one second such that mii(4) 161 * did not notice the link change. To workaround 162 * the issue, emulate lost link event and wait 163 * for 3 seconds when manual link configuration 164 * is in progress. 3 seconds would be long 165 * enough to absorb transient link flips. 166 */ 167 if (rsc->mii_link_tick > 0) { 168 rsc->mii_link_tick--; 169 return (0); 170 } 171 } 172 break; 173 } 174 175 /* Update the media status. */ 176 PHY_STATUS(sc); 177 178 /* Callback if something changed. */ 179 mii_phy_update(sc, cmd); 180 return (0); 181 } 182 183 static void 184 rdcphy_status(struct mii_softc *sc) 185 { 186 struct mii_data *mii; 187 int bmsr, bmcr, physts; 188 189 mii = sc->mii_pdata; 190 191 mii->mii_media_status = IFM_AVALID; 192 mii->mii_media_active = IFM_ETHER; 193 194 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 195 physts = PHY_READ(sc, MII_RDCPHY_STATUS); 196 197 if ((physts & STATUS_LINK_UP) != 0) 198 mii->mii_media_status |= IFM_ACTIVE; 199 200 bmcr = PHY_READ(sc, MII_BMCR); 201 if ((bmcr & BMCR_ISO) != 0) { 202 mii->mii_media_active |= IFM_NONE; 203 mii->mii_media_status = 0; 204 return; 205 } 206 207 if ((bmcr & BMCR_LOOP) != 0) 208 mii->mii_media_active |= IFM_LOOP; 209 210 if ((bmcr & BMCR_AUTOEN) != 0) { 211 if ((bmsr & BMSR_ACOMP) == 0) { 212 /* Erg, still trying, I guess... */ 213 mii->mii_media_active |= IFM_NONE; 214 return; 215 } 216 } 217 218 switch (physts & STATUS_SPEED_MASK) { 219 case STATUS_SPEED_100: 220 mii->mii_media_active |= IFM_100_TX; 221 break; 222 case STATUS_SPEED_10: 223 mii->mii_media_active |= IFM_10_T; 224 break; 225 default: 226 mii->mii_media_active |= IFM_NONE; 227 return; 228 } 229 if ((physts & STATUS_FULL_DUPLEX) != 0) 230 mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc); 231 else 232 mii->mii_media_active |= IFM_HDX; 233 } 234