xref: /freebsd/sys/dev/mii/amphy.c (revision 0957b409)
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 devclass_t amphy_devclass;
75 
76 static driver_t amphy_driver = {
77 	"amphy",
78 	amphy_methods,
79 	sizeof(struct mii_softc)
80 };
81 
82 DRIVER_MODULE(amphy, miibus, amphy_driver, amphy_devclass, 0, 0);
83 
84 static int	amphy_service(struct mii_softc *, struct mii_data *, int);
85 static void	amphy_status(struct mii_softc *);
86 
87 static const struct mii_phydesc amphys[] = {
88 	MII_PHY_DESC(xxDAVICOM, DM9102),
89 	MII_PHY_DESC(xxDAVICOM, DM9101),
90 	MII_PHY_DESC(yyDAVICOM, DM9101),
91 	MII_PHY_END
92 };
93 
94 static const struct mii_phy_funcs amphy_funcs = {
95 	amphy_service,
96 	amphy_status,
97 	mii_phy_reset
98 };
99 
100 static int
101 amphy_probe(device_t dev)
102 {
103 
104 	return (mii_phy_dev_probe(dev, amphys, BUS_PROBE_DEFAULT));
105 }
106 
107 static int
108 amphy_attach(device_t dev)
109 {
110 
111 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &amphy_funcs, 1);
112 	return (0);
113 }
114 
115 static int
116 amphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
117 {
118 
119 	switch (cmd) {
120 	case MII_POLLSTAT:
121 		break;
122 
123 	case MII_MEDIACHG:
124 		mii_phy_setmedia(sc);
125 		break;
126 
127 	case MII_TICK:
128 		if (mii_phy_tick(sc) == EJUSTRETURN)
129 			return (0);
130 		break;
131 	}
132 
133 	/* Update the media status. */
134 	PHY_STATUS(sc);
135 
136 	/* Callback if something changed. */
137 	mii_phy_update(sc, cmd);
138 	return (0);
139 }
140 
141 static void
142 amphy_status(struct mii_softc *sc)
143 {
144 	struct mii_data *mii = sc->mii_pdata;
145 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
146 	int bmsr, bmcr, par, anlpar;
147 
148 	mii->mii_media_status = IFM_AVALID;
149 	mii->mii_media_active = IFM_ETHER;
150 
151 	bmsr = PHY_READ(sc, MII_BMSR) |
152 	    PHY_READ(sc, MII_BMSR);
153 	if (bmsr & BMSR_LINK)
154 		mii->mii_media_status |= IFM_ACTIVE;
155 
156 	bmcr = PHY_READ(sc, MII_BMCR);
157 	if (bmcr & BMCR_ISO) {
158 		mii->mii_media_active |= IFM_NONE;
159 		mii->mii_media_status = 0;
160 		return;
161 	}
162 
163 	if (bmcr & BMCR_LOOP)
164 		mii->mii_media_active |= IFM_LOOP;
165 
166 	if (bmcr & BMCR_AUTOEN) {
167 		/*
168 		 * The PAR status bits are only valid if autonegotiation
169 		 * has completed (or it's disabled).
170 		 */
171 		if ((bmsr & BMSR_ACOMP) == 0) {
172 			/* Erg, still trying, I guess... */
173 			mii->mii_media_active |= IFM_NONE;
174 			return;
175 		}
176 
177 		if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
178 			anlpar = PHY_READ(sc, MII_ANAR) &
179 			    PHY_READ(sc, MII_ANLPAR);
180 			if (anlpar & ANLPAR_TX_FD)
181 				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
182 			else if (anlpar & ANLPAR_T4)
183 				mii->mii_media_active |= IFM_100_T4|IFM_HDX;
184 			else if (anlpar & ANLPAR_TX)
185 				mii->mii_media_active |= IFM_100_TX|IFM_HDX;
186 			else if (anlpar & ANLPAR_10_FD)
187 				mii->mii_media_active |= IFM_10_T|IFM_FDX;
188 			else if (anlpar & ANLPAR_10)
189 				mii->mii_media_active |= IFM_10_T|IFM_HDX;
190 			else
191 				mii->mii_media_active |= IFM_NONE;
192 			return;
193 		}
194 
195 		/*
196 		 * Link partner is not capable of autonegotiation.
197 		 */
198 		par = PHY_READ(sc, MII_AMPHY_DSCSR);
199 		if (par & DSCSR_100FDX)
200 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
201 		else if (par & DSCSR_100HDX)
202 			mii->mii_media_active |= IFM_100_TX|IFM_HDX;
203 		else if (par & DSCSR_10FDX)
204 			mii->mii_media_active |= IFM_10_T|IFM_HDX;
205 		else if (par & DSCSR_10HDX)
206 			mii->mii_media_active |= IFM_10_T|IFM_HDX;
207 		if ((mii->mii_media_active & IFM_FDX) != 0)
208 			mii->mii_media_active |= mii_phy_flowstatus(sc);
209 	} else
210 		mii->mii_media_active = ife->ifm_media;
211 }
212