1 /*	$OpenBSD: amphy.c,v 1.22 2022/04/06 18:59:29 naddy Exp $	*/
2 
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  * $FreeBSD: src/sys/dev/mii/amphy.c,v 1.3 2000/04/19 14:57:29 phk Exp $
35  */
36 
37 /*
38  * driver for AMD AM79c873 PHYs
39  * This driver also works for the Davicom DM9101 PHY, which appears to
40  * be an AM79c873 workalike.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 #include <sys/socket.h>
47 
48 #include <net/if.h>
49 #include <net/if_var.h>
50 #include <net/if_media.h>
51 
52 #include <dev/mii/mii.h>
53 #include <dev/mii/miivar.h>
54 #include <dev/mii/miidevs.h>
55 
56 #include <dev/mii/amphyreg.h>
57 
58 int	amphymatch(struct device *, void *, void *);
59 void	amphyattach(struct device *, struct device *, void *);
60 
61 const struct cfattach amphy_ca = {
62 	sizeof(struct mii_softc), amphymatch, amphyattach, mii_phy_detach
63 };
64 
65 struct cfdriver amphy_cd = {
66 	NULL, "amphy", DV_DULL
67 };
68 
69 int	amphy_service(struct mii_softc *, struct mii_data *, int);
70 void	amphy_status(struct mii_softc *);
71 
72 const struct mii_phy_funcs amphy_funcs = {
73 	amphy_service, amphy_status, mii_phy_reset,
74 };
75 
76 static const struct mii_phydesc amphys[] = {
77 	{ MII_OUI_xxAMD,		MII_MODEL_xxAMD_79C873,
78 	  MII_STR_xxAMD_79C873 },
79 	{ MII_OUI_xxDAVICOM,		MII_MODEL_xxDAVICOM_DM9101,
80 	  MII_STR_xxDAVICOM_DM9101 },
81 	{ MII_OUI_DAVICOM,		MII_MODEL_DAVICOM_DM9102,
82 	  MII_STR_DAVICOM_DM9102 },
83 	{ MII_OUI_DAVICOM,		MII_MODEL_DAVICOM_DM9601,
84 	  MII_STR_DAVICOM_DM9601 },
85 	{ MII_OUI_xxALTIMA,		MII_MODEL_AMD_79C875phy,
86 	  MII_STR_AMD_79C875phy },
87 
88 	{ 0,				0,
89 	  NULL },
90 };
91 
92 int
amphymatch(struct device * parent,void * match,void * aux)93 amphymatch(struct device *parent, void *match, void *aux)
94 {
95 	struct mii_attach_args *ma = aux;
96 
97 	if (mii_phy_match(ma, amphys) != NULL)
98 		return (10);
99 
100 	return (0);
101 }
102 
103 void
amphyattach(struct device * parent,struct device * self,void * aux)104 amphyattach(struct device *parent, struct device *self, void *aux)
105 {
106 	struct mii_softc *sc = (struct mii_softc *)self;
107 	struct mii_attach_args *ma = aux;
108 	struct mii_data *mii = ma->mii_data;
109 	const struct mii_phydesc *mpd;
110 
111 	mpd = mii_phy_match(ma, amphys);
112 	printf(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
113 
114 	sc->mii_inst = mii->mii_instance;
115 	sc->mii_phy = ma->mii_phyno;
116 	sc->mii_funcs = &amphy_funcs;
117 	sc->mii_pdata = mii;
118 	sc->mii_flags = ma->mii_flags;
119 
120 	sc->mii_flags |= MIIF_NOISOLATE;
121 
122 	PHY_RESET(sc);
123 
124 	sc->mii_capabilities =
125 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
126 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
127 		mii_phy_add_media(sc);
128 }
129 
130 int
amphy_service(struct mii_softc * sc,struct mii_data * mii,int cmd)131 amphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
132 {
133 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
134 	int reg;
135 
136 	switch (cmd) {
137 	case MII_POLLSTAT:
138 		/*
139 		 * If we're not polling our PHY instance, just return.
140 		 */
141 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
142 			return (0);
143 		break;
144 
145 	case MII_MEDIACHG:
146 		/*
147 		 * If the media indicates a different PHY instance,
148 		 * isolate ourselves.
149 		 */
150 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
151 			reg = PHY_READ(sc, MII_BMCR);
152 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
153 			return (0);
154 		}
155 
156 		/*
157 		 * If the interface is not up, don't do anything.
158 		 */
159 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
160 			break;
161 
162 		mii_phy_setmedia(sc);
163 		break;
164 
165 	case MII_TICK:
166 		/*
167 		 * If we're not currently selected, just return.
168 		 */
169 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
170 			return (0);
171 
172 		if (mii_phy_tick(sc) == EJUSTRETURN)
173 			return (0);
174 		break;
175 	case MII_DOWN:
176 		mii_phy_down(sc);
177 		return (0);
178 	}
179 
180 	/* Update the media status. */
181 	mii_phy_status(sc);
182 
183 	/* Callback if something changed. */
184 	mii_phy_update(sc, cmd);
185 	return (0);
186 }
187 
188 void
amphy_status(struct mii_softc * sc)189 amphy_status(struct mii_softc *sc)
190 {
191 	struct mii_data *mii = sc->mii_pdata;
192 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
193 	int bmsr, bmcr, par, anlpar;
194 
195 	mii->mii_media_status = IFM_AVALID;
196 	mii->mii_media_active = IFM_ETHER;
197 
198 	bmsr = PHY_READ(sc, MII_BMSR) |
199 	    PHY_READ(sc, MII_BMSR);
200 	if (bmsr & BMSR_LINK)
201 		mii->mii_media_status |= IFM_ACTIVE;
202 
203 	bmcr = PHY_READ(sc, MII_BMCR);
204 	if (bmcr & BMCR_ISO) {
205 		mii->mii_media_active |= IFM_NONE;
206 		mii->mii_media_status = 0;
207 		return;
208 	}
209 
210 	if (bmcr & BMCR_LOOP)
211 		mii->mii_media_active |= IFM_LOOP;
212 
213 	if (bmcr & BMCR_AUTOEN) {
214 		/*
215 		 * The PAR status bits are only valid if autonegotiation
216 		 * has completed (or it's disabled).
217 		 */
218 		if ((bmsr & BMSR_ACOMP) == 0) {
219 			/* Erg, still trying, I guess... */
220 			mii->mii_media_active |= IFM_NONE;
221 			return;
222 		}
223 
224 		if (PHY_READ(sc, MII_ANER) & ANER_LPAN) {
225 			anlpar = PHY_READ(sc, MII_ANAR) &
226 			    PHY_READ(sc, MII_ANLPAR);
227 			if (anlpar & ANLPAR_TX_FD)
228 				mii->mii_media_active |= IFM_100_TX|IFM_FDX;
229 			else if (anlpar & ANLPAR_T4)
230 				mii->mii_media_active |= IFM_100_T4|IFM_HDX;
231 			else if (anlpar & ANLPAR_TX)
232 				mii->mii_media_active |= IFM_100_TX|IFM_HDX;
233 			else if (anlpar & ANLPAR_10_FD)
234 				mii->mii_media_active |= IFM_10_T|IFM_FDX;
235 			else if (anlpar & ANLPAR_10)
236 				mii->mii_media_active |= IFM_10_T|IFM_HDX;
237 			else
238 				mii->mii_media_active |= IFM_NONE;
239 			return;
240 		}
241 
242 		/*
243 		 * Link partner is not capable of autonegotiation.
244 		 */
245 		par = PHY_READ(sc, MII_AMPHY_DSCSR);
246 		if (par & DSCSR_100FDX)
247 			mii->mii_media_active |= IFM_100_TX|IFM_FDX;
248 		else if (par & DSCSR_100HDX)
249 			mii->mii_media_active |= IFM_100_TX|IFM_HDX;
250 		else if (par & DSCSR_10FDX)
251 			mii->mii_media_active |= IFM_10_T|IFM_FDX;
252 		else if (par & DSCSR_10HDX)
253 			mii->mii_media_active |= IFM_10_T|IFM_HDX;
254 	} else
255 		mii->mii_media_active = ife->ifm_media;
256 }
257