1 /* 2 * Copyright (c) 2007 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Sepherosa Ziehau <sepherosa@gmail.com> 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 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * $DragonFly: src/sys/dev/netif/mii_layer/truephy.c,v 1.2 2007/10/23 14:28:42 sephe Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/bus.h> 39 #include <sys/kernel.h> 40 41 #include <net/if.h> 42 #include <net/if_media.h> 43 #include <net/if_arp.h> 44 #include <net/ethernet.h> 45 #include <net/vlan/if_vlan_var.h> 46 47 #include <dev/netif/mii_layer/mii.h> 48 #include <dev/netif/mii_layer/miivar.h> 49 #include <dev/netif/mii_layer/miidevs.h> 50 #include <dev/netif/mii_layer/truephyreg.h> 51 52 #include "miibus_if.h" 53 54 #define FRAMELEN(mtu) (ETHER_HDR_LEN + EVL_ENCAPLEN + (mtu) + ETHER_CRC_LEN) 55 56 static int truephy_service(struct mii_softc *, struct mii_data *, int); 57 static int truephy_attach(device_t); 58 static int truephy_probe(device_t); 59 static void truephy_reset(struct mii_softc *); 60 static void truephy_status(struct mii_softc *); 61 62 static device_method_t truephy_methods[] = { 63 /* device interface */ 64 DEVMETHOD(device_probe, truephy_probe), 65 DEVMETHOD(device_attach, truephy_attach), 66 DEVMETHOD(device_detach, ukphy_detach), 67 DEVMETHOD(device_shutdown, bus_generic_shutdown), 68 { 0, 0 } 69 }; 70 71 static const struct mii_phydesc truephys[] = { 72 MII_PHYDESC(AGERE, ET1011C), 73 MII_PHYDESC_NULL 74 }; 75 76 static devclass_t truephy_devclass; 77 78 static driver_t truephy_driver = { 79 "truephy", 80 truephy_methods, 81 sizeof(struct mii_softc) 82 }; 83 84 DRIVER_MODULE(truephy, miibus, truephy_driver, truephy_devclass, 0, 0); 85 86 static const struct truephy_dsp { 87 uint16_t index; 88 uint16_t data; 89 } truephy_dspcode[] = { 90 { 0x880b, 0x0926 }, /* AfeIfCreg4B1000Msbs */ 91 { 0x880c, 0x0926 }, /* AfeIfCreg4B100Msbs */ 92 { 0x880d, 0x0926 }, /* AfeIfCreg4B10Msbs */ 93 94 { 0x880e, 0xb4d3 }, /* AfeIfCreg4B1000Lsbs */ 95 { 0x880f, 0xb4d3 }, /* AfeIfCreg4B100Lsbs */ 96 { 0x8810, 0xb4d3 }, /* AfeIfCreg4B10Lsbs */ 97 98 { 0x8805, 0xb03e }, /* AfeIfCreg3B1000Msbs */ 99 { 0x8806, 0xb03e }, /* AfeIfCreg3B100Msbs */ 100 { 0x8807, 0xff00 }, /* AfeIfCreg3B10Msbs */ 101 102 { 0x8808, 0xe090 }, /* AfeIfCreg3B1000Lsbs */ 103 { 0x8809, 0xe110 }, /* AfeIfCreg3B100Lsbs */ 104 { 0x880a, 0x0000 }, /* AfeIfCreg3B10Lsbs */ 105 106 { 0x300d, 1 }, /* DisableNorm */ 107 108 { 0x280c, 0x0180 }, /* LinkHoldEnd */ 109 110 { 0x1c21, 0x0002 }, /* AlphaM */ 111 112 { 0x3821, 6 }, /* FfeLkgTx0 */ 113 { 0x381d, 1 }, /* FfeLkg1g4 */ 114 { 0x381e, 1 }, /* FfeLkg1g5 */ 115 { 0x381f, 1 }, /* FfeLkg1g6 */ 116 { 0x3820, 1 }, /* FfeLkg1g7 */ 117 118 { 0x8402, 0x01f0 }, /* Btinact */ 119 { 0x800e, 20 }, /* LftrainTime */ 120 { 0x800f, 24 }, /* DvguardTime */ 121 { 0x8010, 46 } /* IdlguardTime */ 122 }; 123 124 static int 125 truephy_probe(device_t dev) 126 { 127 struct mii_attach_args *ma = device_get_ivars(dev); 128 const struct mii_phydesc *mpd; 129 130 mpd = mii_phy_match(ma, truephys); 131 if (mpd != NULL) { 132 device_set_desc(dev, mpd->mpd_name); 133 return 0; 134 } 135 return ENXIO; 136 } 137 138 static int 139 truephy_attach(device_t dev) 140 { 141 struct mii_softc *sc; 142 struct mii_attach_args *ma; 143 struct mii_data *mii; 144 145 sc = device_get_softc(dev); 146 ma = device_get_ivars(dev); 147 148 mii_softc_init(sc, ma); 149 sc->mii_dev = device_get_parent(dev); 150 mii = device_get_softc(sc->mii_dev); 151 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list); 152 153 sc->mii_inst = mii->mii_instance; 154 sc->mii_service = truephy_service; 155 sc->mii_reset = truephy_reset; 156 sc->mii_pdata = mii; 157 158 sc->mii_flags |= MIIF_NOISOLATE | MIIF_NOLOOP; 159 160 mii->mii_instance++; 161 162 truephy_reset(sc); 163 164 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask; 165 if (sc->mii_capabilities & BMSR_EXTSTAT) { 166 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR); 167 /* No 1000baseT half-duplex support */ 168 sc->mii_extcapabilities &= ~EXTSR_1000THDX; 169 } 170 171 device_printf(dev, " "); 172 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 && 173 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0) 174 kprintf("no media present"); 175 else 176 mii_phy_add_media(sc); 177 kprintf("\n"); 178 179 MIIBUS_MEDIAINIT(sc->mii_dev); 180 return 0; 181 } 182 183 static int 184 truephy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 185 { 186 struct ifmedia_entry *ife = mii->mii_media.ifm_cur; 187 int bmcr; 188 189 switch (cmd) { 190 case MII_POLLSTAT: 191 /* 192 * If we're not polling our PHY instance, just return. 193 */ 194 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 195 return 0; 196 break; 197 198 case MII_MEDIACHG: 199 /* 200 * If the media indicates a different PHY instance, 201 * isolate ourselves. 202 */ 203 if (IFM_INST(ife->ifm_media) != sc->mii_inst) { 204 bmcr = PHY_READ(sc, MII_BMCR); 205 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO); 206 return 0; 207 } 208 209 /* 210 * If the interface is not up, don't do anything. 211 */ 212 if ((mii->mii_ifp->if_flags & IFF_UP) == 0) 213 break; 214 215 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) { 216 bmcr = PHY_READ(sc, MII_BMCR) & ~BMCR_AUTOEN; 217 PHY_WRITE(sc, MII_BMCR, bmcr); 218 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_PDOWN); 219 } 220 221 mii_phy_set_media(sc); 222 223 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) { 224 bmcr = PHY_READ(sc, MII_BMCR) & ~BMCR_PDOWN; 225 PHY_WRITE(sc, MII_BMCR, bmcr); 226 227 if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) { 228 PHY_WRITE(sc, MII_BMCR, 229 bmcr | BMCR_AUTOEN | BMCR_STARTNEG); 230 } 231 } 232 break; 233 234 case MII_TICK: 235 /* 236 * If we're not currently selected, just return. 237 */ 238 if (IFM_INST(ife->ifm_media) != sc->mii_inst) 239 return 0; 240 241 if (mii_phy_tick(sc) == EJUSTRETURN) 242 return 0; 243 break; 244 } 245 246 /* Update the media status. */ 247 truephy_status(sc); 248 249 /* Callback if something changed. */ 250 mii_phy_update(sc, cmd); 251 return 0; 252 } 253 254 static void 255 truephy_reset(struct mii_softc *sc) 256 { 257 int i; 258 259 for (i = 0; i < 2; ++i) { 260 PHY_READ(sc, MII_PHYIDR1); 261 PHY_READ(sc, MII_PHYIDR2); 262 263 PHY_READ(sc, TRUEPHY_CTRL); 264 PHY_WRITE(sc, TRUEPHY_CTRL, 265 TRUEPHY_CTRL_DIAG | TRUEPHY_CTRL_RSV1); 266 267 PHY_WRITE(sc, TRUEPHY_INDEX, TRUEPHY_INDEX_MAGIC); 268 PHY_READ(sc, TRUEPHY_DATA); 269 270 PHY_WRITE(sc, TRUEPHY_CTRL, TRUEPHY_CTRL_RSV1); 271 } 272 273 PHY_READ(sc, MII_BMCR); 274 PHY_READ(sc, TRUEPHY_CTRL); 275 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_PDOWN | BMCR_S1000); 276 PHY_WRITE(sc, TRUEPHY_CTRL, 277 TRUEPHY_CTRL_DIAG | TRUEPHY_CTRL_RSV1 | TRUEPHY_CTRL_RSV0); 278 279 #define N(arr) (int)(sizeof(arr) / sizeof(arr[0])) 280 281 for (i = 0; i < N(truephy_dspcode); ++i) { 282 const struct truephy_dsp *dsp = &truephy_dspcode[i]; 283 284 PHY_WRITE(sc, TRUEPHY_INDEX, dsp->index); 285 PHY_WRITE(sc, TRUEPHY_DATA, dsp->data); 286 287 PHY_WRITE(sc, TRUEPHY_INDEX, dsp->index); 288 PHY_READ(sc, TRUEPHY_DATA); 289 } 290 291 #undef N 292 293 PHY_READ(sc, MII_BMCR); 294 PHY_READ(sc, TRUEPHY_CTRL); 295 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_S1000); 296 PHY_WRITE(sc, TRUEPHY_CTRL, TRUEPHY_CTRL_RSV1); 297 298 mii_phy_reset(sc); 299 300 if (FRAMELEN(sc->mii_pdata->mii_ifp->if_mtu) > 2048) { 301 int conf; 302 303 conf = PHY_READ(sc, TRUEPHY_CONF); 304 conf &= ~TRUEPHY_CONF_TXFIFO_MASK; 305 conf |= TRUEPHY_CONF_TXFIFO_24; 306 PHY_WRITE(sc, TRUEPHY_CONF, conf); 307 } 308 } 309 310 static void 311 truephy_status(struct mii_softc *sc) 312 { 313 struct mii_data *mii = sc->mii_pdata; 314 int bmsr, bmcr, sr; 315 316 mii->mii_media_status = IFM_AVALID; 317 mii->mii_media_active = IFM_ETHER; 318 319 sr = PHY_READ(sc, TRUEPHY_SR); 320 bmcr = PHY_READ(sc, MII_BMCR); 321 322 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR); 323 if (bmsr & BMSR_LINK) 324 mii->mii_media_status |= IFM_ACTIVE; 325 326 if (bmcr & BMCR_AUTOEN) { 327 if ((bmsr & BMSR_ACOMP) == 0) { 328 mii->mii_media_active |= IFM_NONE; 329 return; 330 } 331 } 332 333 switch (sr & TRUEPHY_SR_SPD_MASK) { 334 case TRUEPHY_SR_SPD_1000T: 335 mii->mii_media_active |= IFM_1000_T; 336 break; 337 case TRUEPHY_SR_SPD_100TX: 338 mii->mii_media_active |= IFM_100_TX; 339 break; 340 case TRUEPHY_SR_SPD_10T: 341 mii->mii_media_active |= IFM_10_T; 342 break; 343 default: 344 mii->mii_media_active |= IFM_NONE; 345 break; 346 } 347 348 if (sr & TRUEPHY_SR_FDX) 349 mii->mii_media_active |= IFM_FDX; 350 } 351