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
27 * $FreeBSD: src/sys/dev/mii/atphy.c,v 1.3 2008/10/25 06:39:17 yongari Exp $
31 * Driver for the Attansic/Atheros F1 10/100/1000 PHY.
34 #include <sys/param.h>
36 #include <sys/kernel.h>
39 #include <net/if_media.h>
41 #include <dev/netif/mii_layer/mii.h>
42 #include <dev/netif/mii_layer/miivar.h>
43 #include <dev/netif/mii_layer/miidevs.h>
44 #include <dev/netif/mii_layer/atphyreg.h>
46 #include "miibus_if.h"
48 static int atphy_probe(device_t);
49 static int atphy_attach(device_t);
50 static int atphy_service(struct mii_softc *, struct mii_data *, int);
51 static void atphy_status(struct mii_softc *);
52 static void atphy_reset(struct mii_softc *);
53 static uint16_t atphy_anar(struct ifmedia_entry *);
54 static void atphy_auto(struct mii_softc *);
56 static device_method_t atphy_methods[] = {
57 /* Device interface. */
58 DEVMETHOD(device_probe, atphy_probe),
59 DEVMETHOD(device_attach, atphy_attach),
60 DEVMETHOD(device_detach, ukphy_detach),
61 DEVMETHOD(device_shutdown, bus_generic_shutdown),
65 static const struct mii_phydesc atphys[] = {
66 MII_PHYDESC(ATHEROS, F1),
67 MII_PHYDESC(ATHEROS, F2),
71 static devclass_t atphy_devclass;
73 static driver_t atphy_driver = {
76 sizeof(struct mii_softc)
79 DRIVER_MODULE(atphy, miibus, atphy_driver, atphy_devclass, 0, 0);
82 atphy_probe(device_t dev)
84 struct mii_attach_args *ma = device_get_ivars(dev);
85 const struct mii_phydesc *mpd;
87 mpd = mii_phy_match(ma, atphys);
89 device_set_desc(dev, mpd->mpd_name);
96 atphy_attach(device_t dev)
99 struct mii_attach_args *ma;
100 struct mii_data *mii;
102 sc = device_get_softc(dev);
103 ma = device_get_ivars(dev);
105 mii_softc_init(sc, ma);
106 sc->mii_dev = device_get_parent(dev);
107 mii = device_get_softc(sc->mii_dev);
108 LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
110 sc->mii_inst = mii->mii_instance;
111 sc->mii_service = atphy_service;
113 sc->mii_anegticks = MII_ANEGTICKS_GIGE;
117 sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
118 if (sc->mii_capabilities & BMSR_EXTSTAT)
119 sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
121 device_printf(dev, " ");
122 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0 &&
123 (sc->mii_extcapabilities & EXTSR_MEDIAMASK) == 0)
124 kprintf("no media present");
126 mii_phy_add_media(sc);
131 MIIBUS_MEDIAINIT(sc->mii_dev);
136 atphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
138 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
139 uint16_t anar, bmcr, bmsr;
144 * If we're not polling our PHY instance, just return.
146 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
152 * If the media indicates a different PHY instance,
155 if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
156 bmcr = PHY_READ(sc, MII_BMCR);
157 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
162 * If the interface is not up, don't do anything.
164 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
167 if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO ||
168 IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T) {
174 switch (IFM_SUBTYPE(ife->ifm_media)) {
184 bmcr = PHY_READ(sc, MII_BMCR);
187 * Due to an unknown reason powering down PHY resulted
188 * in unexpected results such as inaccessbility of
189 * hardware of freshly rebooted system. Disable
190 * powering down PHY until I got more information for
191 * Attansic/Atheros PHY hardwares.
193 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_ISO);
200 anar = atphy_anar(ife);
201 if ((ife->ifm_media & IFM_GMASK) & IFM_FDX) {
207 if (sc->mii_extcapabilities & (EXTSR_1000TFDX | EXTSR_1000THDX))
208 PHY_WRITE(sc, MII_100T2CR, 0);
209 PHY_WRITE(sc, MII_ANAR, anar | ANAR_CSMA);
212 * Reset the PHY so all changes take effect.
214 PHY_WRITE(sc, MII_BMCR, bmcr | BMCR_RESET | BMCR_AUTOEN |
221 * If we're not currently selected, just return.
223 if (IFM_INST(ife->ifm_media) != sc->mii_inst)
227 * Is the interface even up?
229 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
233 * Only used for autonegotiation.
235 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO) {
242 * Read the status register twice; BMSR_LINK is latch-low.
244 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
245 if (bmsr & BMSR_LINK) {
250 /* Announce link loss right after it happens. */
251 if (sc->mii_ticks++ == 0)
253 if (sc->mii_ticks <= sc->mii_anegticks)
261 /* Update the media status. */
264 /* Callback if something changed. */
265 mii_phy_update(sc, cmd);
270 atphy_status(struct mii_softc *sc)
272 struct mii_data *mii = sc->mii_pdata;
273 uint32_t bmsr, bmcr, ssr;
275 mii->mii_media_status = IFM_AVALID;
276 mii->mii_media_active = IFM_ETHER;
278 bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
279 if (bmsr & BMSR_LINK)
280 mii->mii_media_status |= IFM_ACTIVE;
282 bmcr = PHY_READ(sc, MII_BMCR);
283 if (bmcr & BMCR_ISO) {
284 mii->mii_media_active |= IFM_NONE;
285 mii->mii_media_status = 0;
289 if (bmcr & BMCR_LOOP)
290 mii->mii_media_active |= IFM_LOOP;
292 ssr = PHY_READ(sc, ATPHY_SSR);
293 if ((ssr & ATPHY_SSR_SPD_DPLX_RESOLVED) == 0) {
294 /* Erg, still trying, I guess... */
295 mii->mii_media_active |= IFM_NONE;
299 switch (ssr & ATPHY_SSR_SPEED_MASK) {
300 case ATPHY_SSR_1000MBS:
301 mii->mii_media_active |= IFM_1000_T;
303 * atphy(4) got a valid link so reset mii_ticks.
304 * Resetting mii_ticks is needed in order to
305 * detect link loss after auto-negotiation.
310 case ATPHY_SSR_100MBS:
311 mii->mii_media_active |= IFM_100_TX;
315 case ATPHY_SSR_10MBS:
316 mii->mii_media_active |= IFM_10_T;
321 mii->mii_media_active |= IFM_NONE;
325 if (ssr & ATPHY_SSR_DUPLEX)
326 mii->mii_media_active |= IFM_FDX;
328 mii->mii_media_active |= IFM_HDX;
330 /* XXX Master/Slave, Flow-control */
334 atphy_reset(struct mii_softc *sc)
339 /* Take PHY out of power down mode. */
340 PHY_WRITE(sc, 29, 0x29);
341 PHY_WRITE(sc, 30, 0);
343 reg = PHY_READ(sc, ATPHY_SCR);
344 /* Enable automatic crossover. */
345 reg |= ATPHY_SCR_AUTO_X_MODE;
346 /* Disable power down. */
347 reg &= ~ATPHY_SCR_MAC_PDOWN;
348 /* Enable CRS on Tx. */
349 reg |= ATPHY_SCR_ASSERT_CRS_ON_TX;
350 /* Auto correction for reversed cable polarity. */
351 reg |= ATPHY_SCR_POLARITY_REVERSAL;
352 PHY_WRITE(sc, ATPHY_SCR, reg);
354 /* Workaround F1 bug to reset phy. */
357 for (i = 0; i < 1000; i++) {
359 if ((PHY_READ(sc, MII_BMCR) & BMCR_RESET) == 0)
365 atphy_anar(struct ifmedia_entry *ife)
369 switch (IFM_SUBTYPE(ife->ifm_media)) {
371 anar |= ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10;
389 if ((ife->ifm_media & IFM_GMASK) & IFM_FDX) {
390 if (IFM_SUBTYPE(ife->ifm_media) == IFM_100_TX)
399 atphy_auto(struct mii_softc *sc)
403 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities);
404 PHY_WRITE(sc, MII_ANAR, anar | (3 << 10) | ANAR_CSMA);
406 if (sc->mii_extcapabilities & (EXTSR_1000TFDX | EXTSR_1000THDX)) {
407 PHY_WRITE(sc, MII_100T2CR, GTCR_ADV_1000TFDX |
410 PHY_WRITE(sc, MII_BMCR, BMCR_RESET | BMCR_AUTOEN | BMCR_STARTNEG);