xref: /freebsd/sys/dev/mii/amphy.c (revision ecaeac805b044f715c98960a8fbf19fe2b76ae6b)
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 __FBSDID("$FreeBSD$");
37 
38 /*
39  * driver for AMD AM79c873 PHYs
40  * This driver also works for Davicom DM910{1,2} PHYs, which appear
41  * to be AM79c873 workalikes.
42  */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/module.h>
48 #include <sys/socket.h>
49 #include <sys/bus.h>
50 
51 #include <net/if.h>
52 #include <net/if_media.h>
53 
54 #include <dev/mii/mii.h>
55 #include <dev/mii/miivar.h>
56 #include "miidevs.h"
57 
58 #include <dev/mii/amphyreg.h>
59 
60 #include "miibus_if.h"
61 
62 static int amphy_probe(device_t);
63 static int amphy_attach(device_t);
64 
65 static device_method_t amphy_methods[] = {
66 	/* device interface */
67 	DEVMETHOD(device_probe,		amphy_probe),
68 	DEVMETHOD(device_attach,	amphy_attach),
69 	DEVMETHOD(device_detach,	mii_phy_detach),
70 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
71 	DEVMETHOD_END
72 };
73 
74 static driver_t amphy_driver = {
75 	"amphy",
76 	amphy_methods,
77 	sizeof(struct mii_softc)
78 };
79 
80 DRIVER_MODULE(amphy, miibus, amphy_driver, 0, 0);
81 
82 static int	amphy_service(struct mii_softc *, struct mii_data *, int);
83 static void	amphy_status(struct mii_softc *);
84 
85 static const struct mii_phydesc amphys[] = {
86 	MII_PHY_DESC(xxDAVICOM, DM9102),
87 	MII_PHY_DESC(xxDAVICOM, DM9101),
88 	MII_PHY_DESC(yyDAVICOM, DM9101),
89 	MII_PHY_END
90 };
91 
92 static const struct mii_phy_funcs amphy_funcs = {
93 	amphy_service,
94 	amphy_status,
95 	mii_phy_reset
96 };
97 
98 static int
99 amphy_probe(device_t dev)
100 {
101 
102 	return (mii_phy_dev_probe(dev, amphys, BUS_PROBE_DEFAULT));
103 }
104 
105 static int
106 amphy_attach(device_t dev)
107 {
108 
109 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &amphy_funcs, 1);
110 	return (0);
111 }
112 
113 static int
114 amphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
115 {
116 
117 	switch (cmd) {
118 	case MII_POLLSTAT:
119 		break;
120 
121 	case MII_MEDIACHG:
122 		mii_phy_setmedia(sc);
123 		break;
124 
125 	case MII_TICK:
126 		if (mii_phy_tick(sc) == EJUSTRETURN)
127 			return (0);
128 		break;
129 	}
130 
131 	/* Update the media status. */
132 	PHY_STATUS(sc);
133 
134 	/* Callback if something changed. */
135 	mii_phy_update(sc, cmd);
136 	return (0);
137 }
138 
139 static void
140 amphy_status(struct mii_softc *sc)
141 {
142 	struct mii_data *mii = sc->mii_pdata;
143 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
144 	int bmsr, bmcr, par, anlpar;
145 
146 	mii->mii_media_status = IFM_AVALID;
147 	mii->mii_media_active = IFM_ETHER;
148 
149 	bmsr = PHY_READ(sc, MII_BMSR) |
150 	    PHY_READ(sc, MII_BMSR);
151 	if (bmsr & BMSR_LINK)
152 		mii->mii_media_status |= IFM_ACTIVE;
153 
154 	bmcr = PHY_READ(sc, MII_BMCR);
155 	if (bmcr & BMCR_ISO) {
156 		mii->mii_media_active |= IFM_NONE;
157 		mii->mii_media_status = 0;
158 		return;
159 	}
160 
161 	if (bmcr & BMCR_LOOP)
162 		mii->mii_media_active |= IFM_LOOP;
163 
164 	if (bmcr & BMCR_AUTOEN) {
165 		/*
166 		 * The PAR status bits are only valid if autonegotiation
167 		 * has completed (or it's disabled).
168 		 */
169 		if ((bmsr & BMSR_ACOMP) == 0) {
170 			/* Erg, still trying, I guess... */
171 			mii->mii_media_active |= IFM_NONE;
172 			return;
173 		}
174 
175 		if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
176 			anlpar = PHY_READ(sc, MII_ANAR) &
177 			    PHY_READ(sc, MII_ANLPAR);
178 			if (anlpar & ANLPAR_TX_FD)
179 				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
180 			else if (anlpar & ANLPAR_T4)
181 				mii->mii_media_active |= IFM_100_T4|IFM_HDX;
182 			else if (anlpar & ANLPAR_TX)
183 				mii->mii_media_active |= IFM_100_TX|IFM_HDX;
184 			else if (anlpar & ANLPAR_10_FD)
185 				mii->mii_media_active |= IFM_10_T|IFM_FDX;
186 			else if (anlpar & ANLPAR_10)
187 				mii->mii_media_active |= IFM_10_T|IFM_HDX;
188 			else
189 				mii->mii_media_active |= IFM_NONE;
190 			return;
191 		}
192 
193 		/*
194 		 * Link partner is not capable of autonegotiation.
195 		 */
196 		par = PHY_READ(sc, MII_AMPHY_DSCSR);
197 		if (par & DSCSR_100FDX)
198 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
199 		else if (par & DSCSR_100HDX)
200 			mii->mii_media_active |= IFM_100_TX|IFM_HDX;
201 		else if (par & DSCSR_10FDX)
202 			mii->mii_media_active |= IFM_10_T|IFM_HDX;
203 		else if (par & DSCSR_10HDX)
204 			mii->mii_media_active |= IFM_10_T|IFM_HDX;
205 		if ((mii->mii_media_active & IFM_FDX) != 0)
206 			mii->mii_media_active |= mii_phy_flowstatus(sc);
207 	} else
208 		mii->mii_media_active = ife->ifm_media;
209 }
210