xref: /netbsd-src/sys/dev/mii/amhphy.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: amhphy.c,v 1.16 2007/12/09 20:28:02 jmcneill Exp $	*/
2 
3 /*
4  * Copyright 2001 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed for the NetBSD Project by
20  *	Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Driver for the 10BASE-T portion of the AMD Am79c901 PHY.
40  */
41 
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: amhphy.c,v 1.16 2007/12/09 20:28:02 jmcneill Exp $");
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/device.h>
49 #include <sys/socket.h>
50 #include <sys/errno.h>
51 
52 #include <net/if.h>
53 #include <net/if_media.h>
54 
55 #include <dev/mii/mii.h>
56 #include <dev/mii/miivar.h>
57 #include <dev/mii/miidevs.h>
58 
59 #include <dev/mii/amhphyreg.h>
60 
61 static int	amhphymatch(struct device *, struct cfdata *, void *);
62 static void	amhphyattach(struct device *, struct device *, void *);
63 
64 CFATTACH_DECL(amhphy, sizeof(struct mii_softc),
65     amhphymatch, amhphyattach, mii_phy_detach, mii_phy_activate);
66 
67 static int	amhphy_service(struct mii_softc *, struct mii_data *, int);
68 static void	amhphy_status(struct mii_softc *);
69 
70 static const struct mii_phy_funcs amhphy_funcs = {
71 	amhphy_service, amhphy_status, mii_phy_reset,
72 };
73 
74 static const struct mii_phydesc amhphys[] = {
75 	{ MII_OUI_yyAMD,		MII_MODEL_yyAMD_79c901,
76 	  MII_STR_yyAMD_79c901 },
77 
78 	{ 0,				0,
79 	  NULL },
80 };
81 
82 static int
83 amhphymatch(struct device *parent, struct cfdata *match,
84     void *aux)
85 {
86 	struct mii_attach_args *ma = aux;
87 
88 	if (mii_phy_match(ma, amhphys) != NULL)
89 		return (10);
90 
91 	return (0);
92 }
93 
94 static void
95 amhphyattach(struct device *parent, struct device *self, void *aux)
96 {
97 	struct mii_softc *sc = device_private(self);
98 	struct mii_attach_args *ma = aux;
99 	struct mii_data *mii = ma->mii_data;
100 	const struct mii_phydesc *mpd;
101 
102 	mpd = mii_phy_match(ma, amhphys);
103 	aprint_naive(": Media interface\n");
104 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
105 
106 	sc->mii_inst = mii->mii_instance;
107 	sc->mii_phy = ma->mii_phyno;
108 	sc->mii_funcs = &amhphy_funcs;
109 	sc->mii_pdata = mii;
110 	sc->mii_flags = ma->mii_flags;
111 	sc->mii_anegticks = MII_ANEGTICKS;
112 
113 	PHY_RESET(sc);
114 
115 	sc->mii_capabilities =
116 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
117 	aprint_normal("%s: ", sc->mii_dev.dv_xname);
118 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
119 		aprint_error("no media present");
120 	else
121 		mii_phy_add_media(sc);
122 	aprint_normal("\n");
123 
124 	if (!pmf_device_register(self, NULL, mii_phy_resume))
125 		aprint_error_dev(self, "couldn't establish power handler\n");
126 }
127 
128 static int
129 amhphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
130 {
131 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
132 	int reg;
133 
134 	switch (cmd) {
135 	case MII_POLLSTAT:
136 		/*
137 		 * If we're not polling our PHY instance, just return.
138 		 */
139 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
140 			return (0);
141 		break;
142 
143 	case MII_MEDIACHG:
144 		/*
145 		 * If the media indicates a different PHY instance,
146 		 * isolate ourselves.
147 		 */
148 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
149 			reg = PHY_READ(sc, MII_BMCR);
150 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
151 			return (0);
152 		}
153 
154 		/*
155 		 * If the interface is not up, don't do anything.
156 		 */
157 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
158 			break;
159 
160 		mii_phy_setmedia(sc);
161 		break;
162 
163 	case MII_TICK:
164 		/*
165 		 * If we're not currently selected, just return.
166 		 */
167 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
168 			return (0);
169 
170 		if (mii_phy_tick(sc) == EJUSTRETURN)
171 			return (0);
172 		break;
173 
174 	case MII_DOWN:
175 		mii_phy_down(sc);
176 		return (0);
177 	}
178 
179 	/* Update the media status. */
180 	mii_phy_status(sc);
181 
182 	/* Callback if something changed. */
183 	mii_phy_update(sc, cmd);
184 	return (0);
185 }
186 
187 static void
188 amhphy_status(struct mii_softc *sc)
189 {
190 	struct mii_data *mii = sc->mii_pdata;
191 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
192 	int bmsr, bmcr, ssr;
193 
194 	mii->mii_media_status = IFM_AVALID;
195 	mii->mii_media_active = IFM_ETHER;
196 
197 	bmsr = PHY_READ(sc, MII_BMSR);
198 	ssr = PHY_READ(sc, MII_AMHPHY_SSR);
199 
200 	if (ssr & SSR_LS)
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 media status bits are only valid of 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 (ssr & SSR_S) {
225 			/*
226 			 * This should't really ever happen, since it's
227 			 * a 10BASE-T only PHY.  But the bit exists,
228 			 * according to the documentation, so we pay
229 			 * attention to it.
230 			 */
231 			mii->mii_media_active |= IFM_100_TX;
232 		} else
233 			mii->mii_media_active |= IFM_10_T;
234 		if (ssr & SSR_FD)
235 			mii->mii_media_active |= IFM_FDX;
236 	} else
237 		mii->mii_media_active = ife->ifm_media;
238 }
239