2 * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * Driver for the Attansic/Atheros F1 10/100/1000 PHY.
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>
43 #include <net/if_media.h>
45 #include <dev/mii/mii.h>
46 #include <dev/mii/miivar.h>
49 #include <dev/mii/atphyreg.h>
51 #include "miibus_if.h"
53 static int atphy_probe(device_t);
54 static int atphy_attach(device_t);
57 struct mii_softc mii_sc;
63 static device_method_t atphy_methods[] = {
64 /* Device interface. */
65 DEVMETHOD(device_probe, atphy_probe),
66 DEVMETHOD(device_attach, atphy_attach),
67 DEVMETHOD(device_detach, mii_phy_detach),
68 DEVMETHOD(device_shutdown, bus_generic_shutdown),
72 static devclass_t atphy_devclass;
73 static driver_t atphy_driver = {
76 sizeof(struct atphy_softc)
79 DRIVER_MODULE(atphy, miibus, atphy_driver, atphy_devclass, 0, 0);
81 static int atphy_service(struct mii_softc *, struct mii_data *, int);
82 static void atphy_status(struct mii_softc *);
83 static void atphy_reset(struct mii_softc *);
84 static uint16_t atphy_anar(struct ifmedia_entry *);
85 static int atphy_auto(struct mii_softc *);
87 static const struct mii_phydesc atphys[] = {
88 MII_PHY_DESC(ATHEROS, F1),
89 MII_PHY_DESC(ATHEROS, F2),
94 atphy_probe(device_t dev)
97 return (mii_phy_dev_probe(dev, atphys, BUS_PROBE_DEFAULT));
101 atphy_attach(device_t dev)
103 struct atphy_softc *asc;
104 struct mii_softc *sc;
105 struct mii_attach_args *ma;
106 struct mii_data *mii;
108 asc = device_get_softc(dev);
110 ma = device_get_ivars(dev);
111 sc->mii_dev = device_get_parent(dev);
112 mii = device_get_softc(sc->mii_dev);
113 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
115 sc->mii_inst = mii->mii_instance;
116 sc->mii_phy = ma->mii_phyno;
117 sc->mii_service = atphy_service;
119 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
123 asc->mii_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
124 asc->mii_model = MII_MODEL(ma->mii_id2);
125 asc->mii_rev = MII_REV(ma->mii_id2);
127 device_printf(dev, "OUI 0x%06x, model 0x%04x, rev. %d\n",
128 asc->mii_oui, asc->mii_model, asc->mii_rev);
132 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
133 if (sc->mii_capabilities & BMSR_EXTSTAT)
134 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
135 device_printf(dev, " ");
136 mii_phy_add_media(sc);
139 MIIBUS_MEDIAINIT(sc->mii_dev);
144 atphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
146 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
147 uint16_t anar, bmcr, bmsr;
152 * If we're not polling our PHY instance, just return.
154 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
160 * If the media indicates a different PHY instance,
163 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
164 bmcr = PHY_READ(sc, MII_BMCR);
165 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
170 * If the interface is not up, don't do anything.
172 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
175 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO ||
176 IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) {
182 switch (IFM_SUBTYPE(ife->ifm_media)) {
190 bmcr = PHY_READ(sc, MII_BMCR);
193 * Due to an unknown reason powering down PHY resulted
194 * in unexpected results such as inaccessbility of
195 * hardware of freshly rebooted system. Disable
196 * powering down PHY until I got more information for
197 * Attansic/Atheros PHY hardwares.
199 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
205 anar = atphy_anar(ife);
206 if (((ife->ifm_media & IFM_GMASK) & IFM_FDX) != 0) {
212 if ((sc->mii_extcapabilities & (EXTSR_1000TFDX |
213 EXTSR_1000THDX)) != 0)
214 PHY_WRITE(sc, MII_100T2CR, 0);
215 PHY_WRITE(sc, MII_ANAR, anar | ANAR_CSMA);
218 * Reset the PHY so all changes take effect.
220 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_RESET | BMCR_AUTOEN |
227 * If we're not currently selected, just return.
229 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
233 * Is the interface even up?
235 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
239 * Only used for autonegotiation.
241 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
248 * Read the status register twice; BMSR_LINK is latch-low.
250 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
251 if (bmsr & BMSR_LINK) {
256 /* Announce link loss right after it happens. */
257 if (sc->mii_ticks++ == 0)
259 if (sc->mii_ticks <= sc->mii_anegticks)
267 /* Update the media status. */
270 /* Callback if something changed. */
271 mii_phy_update(sc, cmd);
276 atphy_status(struct mii_softc *sc)
278 struct mii_data *mii = sc->mii_pdata;
279 uint32_t bmsr, bmcr, ssr;
281 mii->mii_media_status = IFM_AVALID;
282 mii->mii_media_active = IFM_ETHER;
284 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
285 if ((bmsr & BMSR_LINK) != 0)
286 mii->mii_media_status |= IFM_ACTIVE;
288 bmcr = PHY_READ(sc, MII_BMCR);
289 if ((bmcr & BMCR_ISO) != 0) {
290 mii->mii_media_active |= IFM_NONE;
291 mii->mii_media_status = 0;
295 if ((bmcr & BMCR_LOOP) != 0)
296 mii->mii_media_active |= IFM_LOOP;
298 ssr = PHY_READ(sc, ATPHY_SSR);
299 if ((ssr & ATPHY_SSR_SPD_DPLX_RESOLVED) == 0) {
300 /* Erg, still trying, I guess... */
301 mii->mii_media_active |= IFM_NONE;
305 switch (ssr & ATPHY_SSR_SPEED_MASK) {
306 case ATPHY_SSR_1000MBS:
307 mii->mii_media_active |= IFM_1000_T;
309 * atphy(4) got a valid link so reset mii_ticks.
310 * Resetting mii_ticks is needed in order to
311 * detect link loss after auto-negotiation.
315 case ATPHY_SSR_100MBS:
316 mii->mii_media_active |= IFM_100_TX;
319 case ATPHY_SSR_10MBS:
320 mii->mii_media_active |= IFM_10_T;
324 mii->mii_media_active |= IFM_NONE;
328 if ((ssr & ATPHY_SSR_DUPLEX) != 0)
329 mii->mii_media_active |= IFM_FDX;
331 mii->mii_media_active |= IFM_HDX;
333 /* XXX Master/Slave, Flow-control */
337 atphy_reset(struct mii_softc *sc)
339 struct atphy_softc *asc;
343 asc = (struct atphy_softc *)sc;
345 /* Take PHY out of power down mode. */
346 PHY_WRITE(sc, 29, 0x29);
347 PHY_WRITE(sc, 30, 0);
349 reg = PHY_READ(sc, ATPHY_SCR);
350 /* Enable automatic crossover. */
351 reg |= ATPHY_SCR_AUTO_X_MODE;
352 /* Disable power down. */
353 reg &= ~ATPHY_SCR_MAC_PDOWN;
354 /* Enable CRS on Tx. */
355 reg |= ATPHY_SCR_ASSERT_CRS_ON_TX;
356 /* Auto correction for reversed cable polarity. */
357 reg |= ATPHY_SCR_POLARITY_REVERSAL;
358 PHY_WRITE(sc, ATPHY_SCR, reg);
360 /* Workaround F1 bug to reset phy. */
363 for (i = 0; i < 1000; i++) {
365 if ((PHY_READ(sc, MII_BMCR) & BMCR_RESET) == 0)
371 atphy_anar(struct ifmedia_entry *ife)
376 switch (IFM_SUBTYPE(ife->ifm_media)) {
378 anar |= ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10;
392 if (((ife->ifm_media & IFM_GMASK) & IFM_FDX) != 0) {
393 if (IFM_SUBTYPE(ife->ifm_media) == IFM_100_TX)
403 atphy_auto(struct mii_softc *sc)
407 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities);
408 PHY_WRITE(sc, MII_ANAR, anar | (3 << 10) | ANAR_CSMA);
409 if ((sc->mii_extcapabilities & (EXTSR_1000TFDX | EXTSR_1000THDX)) != 0)
410 PHY_WRITE(sc, MII_100T2CR, GTCR_ADV_1000TFDX |
412 PHY_WRITE(sc, MII_BMCR, BMCR_RESET | BMCR_AUTOEN | BMCR_STARTNEG);
414 return (EJUSTRETURN);