1139749Simp/*-
250983Swpaul * Copyright (c) 1997, 1998, 1999
350983Swpaul *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
450983Swpaul *
550983Swpaul * Redistribution and use in source and binary forms, with or without
650983Swpaul * modification, are permitted provided that the following conditions
750983Swpaul * are met:
850983Swpaul * 1. Redistributions of source code must retain the above copyright
950983Swpaul *    notice, this list of conditions and the following disclaimer.
1050983Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1150983Swpaul *    notice, this list of conditions and the following disclaimer in the
1250983Swpaul *    documentation and/or other materials provided with the distribution.
1350983Swpaul * 3. All advertising materials mentioning features or use of this software
1450983Swpaul *    must display the following acknowledgement:
1550983Swpaul *	This product includes software developed by Bill Paul.
1650983Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1750983Swpaul *    may be used to endorse or promote products derived from this software
1850983Swpaul *    without specific prior written permission.
1950983Swpaul *
2050983Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2150983Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2250983Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2350983Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2450983Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2550983Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2650983Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2750983Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2850983Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2950983Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3050983Swpaul * THE POSSIBILITY OF SUCH DAMAGE.
3150983Swpaul */
3250983Swpaul
33119418Sobrien#include <sys/cdefs.h>
34119418Sobrien__FBSDID("$FreeBSD$");
35119418Sobrien
3650983Swpaul/*
3750983Swpaul * driver for AMD AM79c873 PHYs
38164835Smarius * This driver also works for Davicom DM910{1,2} PHYs, which appear
39164835Smarius * to be AM79c873 workalikes.
4050983Swpaul */
4150983Swpaul
4250983Swpaul#include <sys/param.h>
4350983Swpaul#include <sys/systm.h>
4450983Swpaul#include <sys/kernel.h>
45129876Sphk#include <sys/module.h>
4650983Swpaul#include <sys/socket.h>
4750983Swpaul#include <sys/bus.h>
4850983Swpaul
4950983Swpaul#include <net/if.h>
5050983Swpaul#include <net/if_media.h>
5150983Swpaul
5250983Swpaul#include <dev/mii/mii.h>
5350983Swpaul#include <dev/mii/miivar.h>
54109514Sobrien#include "miidevs.h"
5550983Swpaul
5650983Swpaul#include <dev/mii/amphyreg.h>
5750983Swpaul
5850983Swpaul#include "miibus_if.h"
5950983Swpaul
60105135Salfredstatic int amphy_probe(device_t);
61105135Salfredstatic int amphy_attach(device_t);
6250983Swpaul
6350983Swpaulstatic device_method_t amphy_methods[] = {
6450983Swpaul	/* device interface */
6550983Swpaul	DEVMETHOD(device_probe,		amphy_probe),
6650983Swpaul	DEVMETHOD(device_attach,	amphy_attach),
6795722Sphk	DEVMETHOD(device_detach,	mii_phy_detach),
6850983Swpaul	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
69227908Smarius	DEVMETHOD_END
7050983Swpaul};
7150983Swpaul
7250983Swpaulstatic devclass_t amphy_devclass;
7350983Swpaul
7450983Swpaulstatic driver_t amphy_driver = {
7550983Swpaul	"amphy",
7650983Swpaul	amphy_methods,
7750983Swpaul	sizeof(struct mii_softc)
7850983Swpaul};
7950983Swpaul
8050983SwpaulDRIVER_MODULE(amphy, miibus, amphy_driver, amphy_devclass, 0, 0);
8150983Swpaul
8292739Salfredstatic int	amphy_service(struct mii_softc *, struct mii_data *, int);
8392739Salfredstatic void	amphy_status(struct mii_softc *);
8450983Swpaul
85164827Smariusstatic const struct mii_phydesc amphys[] = {
86221407Smarius	MII_PHY_DESC(xxDAVICOM, DM9102),
87164827Smarius	MII_PHY_DESC(xxDAVICOM, DM9101),
88221407Smarius	MII_PHY_DESC(yyDAVICOM, DM9101),
89164827Smarius	MII_PHY_END
90164827Smarius};
91164827Smarius
92221407Smariusstatic const struct mii_phy_funcs amphy_funcs = {
93221407Smarius	amphy_service,
94221407Smarius	amphy_status,
95221407Smarius	mii_phy_reset
96221407Smarius};
97221407Smarius
98105135Salfredstatic int
99150763Simpamphy_probe(device_t dev)
10050983Swpaul{
10150983Swpaul
102164827Smarius	return (mii_phy_dev_probe(dev, amphys, BUS_PROBE_DEFAULT));
10350983Swpaul}
10450983Swpaul
105105135Salfredstatic int
106150763Simpamphy_attach(device_t dev)
10750983Swpaul{
10850983Swpaul
109221407Smarius	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &amphy_funcs, 1);
110164835Smarius	return (0);
11150983Swpaul}
11250983Swpaul
11384145Sjlemonstatic int
114150763Simpamphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
11550983Swpaul{
11650983Swpaul
11750983Swpaul	switch (cmd) {
11850983Swpaul	case MII_POLLSTAT:
11950983Swpaul		break;
12050983Swpaul
12150983Swpaul	case MII_MEDIACHG:
12250983Swpaul		/*
12350983Swpaul		 * If the interface is not up, don't do anything.
12450983Swpaul		 */
12550983Swpaul		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
12650983Swpaul			break;
12750983Swpaul
128164835Smarius		mii_phy_setmedia(sc);
12950983Swpaul		break;
13050983Swpaul
13150983Swpaul	case MII_TICK:
13284145Sjlemon		if (mii_phy_tick(sc) == EJUSTRETURN)
13350983Swpaul			return (0);
13450983Swpaul		break;
13550983Swpaul	}
13650983Swpaul
13750983Swpaul	/* Update the media status. */
138221407Smarius	PHY_STATUS(sc);
13950983Swpaul
14050983Swpaul	/* Callback if something changed. */
14184145Sjlemon	mii_phy_update(sc, cmd);
14250983Swpaul	return (0);
14350983Swpaul}
14450983Swpaul
14584145Sjlemonstatic void
146150763Simpamphy_status(struct mii_softc *sc)
14750983Swpaul{
14850983Swpaul	struct mii_data *mii = sc->mii_pdata;
149164835Smarius	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
15050983Swpaul	int bmsr, bmcr, par, anlpar;
15150983Swpaul
15250983Swpaul	mii->mii_media_status = IFM_AVALID;
15350983Swpaul	mii->mii_media_active = IFM_ETHER;
15450983Swpaul
15550983Swpaul	bmsr = PHY_READ(sc, MII_BMSR) |
15650983Swpaul	    PHY_READ(sc, MII_BMSR);
15750983Swpaul	if (bmsr & BMSR_LINK)
15850983Swpaul		mii->mii_media_status |= IFM_ACTIVE;
15950983Swpaul
16050983Swpaul	bmcr = PHY_READ(sc, MII_BMCR);
16150983Swpaul	if (bmcr & BMCR_ISO) {
16250983Swpaul		mii->mii_media_active |= IFM_NONE;
16350983Swpaul		mii->mii_media_status = 0;
16450983Swpaul		return;
16550983Swpaul	}
16650983Swpaul
16750983Swpaul	if (bmcr & BMCR_LOOP)
16850983Swpaul		mii->mii_media_active |= IFM_LOOP;
16950983Swpaul
17050983Swpaul	if (bmcr & BMCR_AUTOEN) {
17150983Swpaul		/*
172175703Smarius		 * The PAR status bits are only valid if autonegotiation
17350983Swpaul		 * has completed (or it's disabled).
17450983Swpaul		 */
17550983Swpaul		if ((bmsr & BMSR_ACOMP) == 0) {
17650983Swpaul			/* Erg, still trying, I guess... */
17750983Swpaul			mii->mii_media_active |= IFM_NONE;
17850983Swpaul			return;
17950983Swpaul		}
18050983Swpaul
18150983Swpaul		if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
18250983Swpaul			anlpar = PHY_READ(sc, MII_ANAR) &
18350983Swpaul			    PHY_READ(sc, MII_ANLPAR);
184173665Syongari			if (anlpar & ANLPAR_TX_FD)
185173665Syongari				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
186173665Syongari			else if (anlpar & ANLPAR_T4)
187213384Smarius				mii->mii_media_active |= IFM_100_T4|IFM_HDX;
18850983Swpaul			else if (anlpar & ANLPAR_TX)
189213384Smarius				mii->mii_media_active |= IFM_100_TX|IFM_HDX;
19050983Swpaul			else if (anlpar & ANLPAR_10_FD)
19150983Swpaul				mii->mii_media_active |= IFM_10_T|IFM_FDX;
19250983Swpaul			else if (anlpar & ANLPAR_10)
193213384Smarius				mii->mii_media_active |= IFM_10_T|IFM_HDX;
19450983Swpaul			else
19550983Swpaul				mii->mii_media_active |= IFM_NONE;
19650983Swpaul			return;
19750983Swpaul		}
19850983Swpaul
19950983Swpaul		/*
20050983Swpaul		 * Link partner is not capable of autonegotiation.
20150983Swpaul		 */
20250983Swpaul		par = PHY_READ(sc, MII_AMPHY_DSCSR);
20350983Swpaul		if (par & DSCSR_100FDX)
20450983Swpaul			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
20550983Swpaul		else if (par & DSCSR_100HDX)
206213384Smarius			mii->mii_media_active |= IFM_100_TX|IFM_HDX;
20750983Swpaul		else if (par & DSCSR_10FDX)
20850983Swpaul			mii->mii_media_active |= IFM_10_T|IFM_HDX;
20950983Swpaul		else if (par & DSCSR_10HDX)
210213384Smarius			mii->mii_media_active |= IFM_10_T|IFM_HDX;
211221407Smarius		if ((mii->mii_media_active & IFM_FDX) != 0)
212221407Smarius			mii->mii_media_active |= mii_phy_flowstatus(sc);
21350983Swpaul	} else
214164835Smarius		mii->mii_media_active = ife->ifm_media;
21550983Swpaul}
216