1/*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * Copyright (c) 1997, 1998, 1999
5 *	Bill Paul <wpaul@ee.columbia.edu>.  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, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36/*
37 * driver for AMD AM79c873 PHYs
38 * This driver also works for Davicom DM910{1,2} PHYs, which appear
39 * to be AM79c873 workalikes.
40 */
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/module.h>
46#include <sys/socket.h>
47#include <sys/bus.h>
48
49#include <net/if.h>
50#include <net/if_media.h>
51
52#include <dev/mii/mii.h>
53#include <dev/mii/miivar.h>
54#include "miidevs.h"
55
56#include <dev/mii/amphyreg.h>
57
58#include "miibus_if.h"
59
60static int amphy_probe(device_t);
61static int amphy_attach(device_t);
62
63static device_method_t amphy_methods[] = {
64	/* device interface */
65	DEVMETHOD(device_probe,		amphy_probe),
66	DEVMETHOD(device_attach,	amphy_attach),
67	DEVMETHOD(device_detach,	mii_phy_detach),
68	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
69	DEVMETHOD_END
70};
71
72static driver_t amphy_driver = {
73	"amphy",
74	amphy_methods,
75	sizeof(struct mii_softc)
76};
77
78DRIVER_MODULE(amphy, miibus, amphy_driver, 0, 0);
79
80static int	amphy_service(struct mii_softc *, struct mii_data *, int);
81static void	amphy_status(struct mii_softc *);
82
83static const struct mii_phydesc amphys[] = {
84	MII_PHY_DESC(xxDAVICOM, DM9102),
85	MII_PHY_DESC(xxDAVICOM, DM9101),
86	MII_PHY_DESC(yyDAVICOM, DM9101),
87	MII_PHY_END
88};
89
90static const struct mii_phy_funcs amphy_funcs = {
91	amphy_service,
92	amphy_status,
93	mii_phy_reset
94};
95
96static int
97amphy_probe(device_t dev)
98{
99
100	return (mii_phy_dev_probe(dev, amphys, BUS_PROBE_DEFAULT));
101}
102
103static int
104amphy_attach(device_t dev)
105{
106
107	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &amphy_funcs, 1);
108	return (0);
109}
110
111static int
112amphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
113{
114
115	switch (cmd) {
116	case MII_POLLSTAT:
117		break;
118
119	case MII_MEDIACHG:
120		mii_phy_setmedia(sc);
121		break;
122
123	case MII_TICK:
124		if (mii_phy_tick(sc) == EJUSTRETURN)
125			return (0);
126		break;
127	}
128
129	/* Update the media status. */
130	PHY_STATUS(sc);
131
132	/* Callback if something changed. */
133	mii_phy_update(sc, cmd);
134	return (0);
135}
136
137static void
138amphy_status(struct mii_softc *sc)
139{
140	struct mii_data *mii = sc->mii_pdata;
141	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
142	int bmsr, bmcr, par, anlpar;
143
144	mii->mii_media_status = IFM_AVALID;
145	mii->mii_media_active = IFM_ETHER;
146
147	bmsr = PHY_READ(sc, MII_BMSR) |
148	    PHY_READ(sc, MII_BMSR);
149	if (bmsr & BMSR_LINK)
150		mii->mii_media_status |= IFM_ACTIVE;
151
152	bmcr = PHY_READ(sc, MII_BMCR);
153	if (bmcr & BMCR_ISO) {
154		mii->mii_media_active |= IFM_NONE;
155		mii->mii_media_status = 0;
156		return;
157	}
158
159	if (bmcr & BMCR_LOOP)
160		mii->mii_media_active |= IFM_LOOP;
161
162	if (bmcr & BMCR_AUTOEN) {
163		/*
164		 * The PAR status bits are only valid if autonegotiation
165		 * has completed (or it's disabled).
166		 */
167		if ((bmsr & BMSR_ACOMP) == 0) {
168			/* Erg, still trying, I guess... */
169			mii->mii_media_active |= IFM_NONE;
170			return;
171		}
172
173		if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
174			anlpar = PHY_READ(sc, MII_ANAR) &
175			    PHY_READ(sc, MII_ANLPAR);
176			if (anlpar & ANLPAR_TX_FD)
177				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
178			else if (anlpar & ANLPAR_T4)
179				mii->mii_media_active |= IFM_100_T4|IFM_HDX;
180			else if (anlpar & ANLPAR_TX)
181				mii->mii_media_active |= IFM_100_TX|IFM_HDX;
182			else if (anlpar & ANLPAR_10_FD)
183				mii->mii_media_active |= IFM_10_T|IFM_FDX;
184			else if (anlpar & ANLPAR_10)
185				mii->mii_media_active |= IFM_10_T|IFM_HDX;
186			else
187				mii->mii_media_active |= IFM_NONE;
188			return;
189		}
190
191		/*
192		 * Link partner is not capable of autonegotiation.
193		 */
194		par = PHY_READ(sc, MII_AMPHY_DSCSR);
195		if (par & DSCSR_100FDX)
196			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
197		else if (par & DSCSR_100HDX)
198			mii->mii_media_active |= IFM_100_TX|IFM_HDX;
199		else if (par & DSCSR_10FDX)
200			mii->mii_media_active |= IFM_10_T|IFM_HDX;
201		else if (par & DSCSR_10HDX)
202			mii->mii_media_active |= IFM_10_T|IFM_HDX;
203		if ((mii->mii_media_active & IFM_FDX) != 0)
204			mii->mii_media_active |= mii_phy_flowstatus(sc);
205	} else
206		mii->mii_media_active = ife->ifm_media;
207}
208