xref: /netbsd-src/sys/dev/mii/acphy.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: acphy.c,v 1.20 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 Altima AC101 PHY.
40  */
41 
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: acphy.c,v 1.20 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/acphyreg.h>
60 
61 static int	acphymatch(struct device *, struct cfdata *, void *);
62 static void	acphyattach(struct device *, struct device *, void *);
63 
64 CFATTACH_DECL(acphy, sizeof(struct mii_softc),
65     acphymatch, acphyattach, mii_phy_detach, mii_phy_activate);
66 
67 static int	acphy_service(struct mii_softc *, struct mii_data *, int);
68 static void	acphy_status(struct mii_softc *);
69 
70 static const struct mii_phy_funcs acphy_funcs = {
71 	acphy_service, acphy_status, mii_phy_reset,
72 };
73 
74 static const struct mii_phydesc acphys[] = {
75 	{ MII_OUI_ALTIMA,		MII_MODEL_ALTIMA_AC101,
76 	  MII_STR_ALTIMA_AC101 },
77 	{ MII_OUI_ALTIMA,		MII_MODEL_ALTIMA_AC101L,
78 	  MII_STR_ALTIMA_AC101L },
79 	{ MII_OUI_ALTIMA,		MII_MODEL_ALTIMA_Am79C874,
80 	  MII_STR_ALTIMA_Am79C874 },
81 	{ MII_OUI_ALTIMA,		MII_MODEL_ALTIMA_Am79C875,
82 	  MII_STR_ALTIMA_Am79C875 },
83 
84 	/* XXX This is reported to work, but it's not from any data sheet. */
85 	{ MII_OUI_ALTIMA,		MII_MODEL_ALTIMA_ACXXX,
86 	  MII_STR_ALTIMA_ACXXX },
87 
88 	{ 0,				0,
89 	  NULL },
90 };
91 
92 static int
93 acphymatch(struct device *parent, struct cfdata *match,
94     void *aux)
95 {
96 	struct mii_attach_args *ma = aux;
97 
98 	if (mii_phy_match(ma, acphys) != NULL)
99 		return (10);
100 
101 	return (0);
102 }
103 
104 static void
105 acphyattach(struct device *parent, struct device *self, void *aux)
106 {
107 	struct mii_softc *sc = device_private(self);
108 	struct mii_attach_args *ma = aux;
109 	struct mii_data *mii = ma->mii_data;
110 	const struct mii_phydesc *mpd;
111 
112 	mpd = mii_phy_match(ma, acphys);
113 	aprint_naive(": Media interface\n");
114 	aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
115 
116 	sc->mii_inst = mii->mii_instance;
117 	sc->mii_phy = ma->mii_phyno;
118 	sc->mii_funcs = &acphy_funcs;
119 	sc->mii_pdata = mii;
120 	sc->mii_flags = ma->mii_flags;
121 	sc->mii_anegticks = MII_ANEGTICKS;
122 
123 	PHY_RESET(sc);
124 
125 	/*
126 	 * XXX Check MCR_FX_SEL to set MIIF_HAVE_FIBER?
127 	 */
128 
129 	sc->mii_capabilities =
130 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
131 	aprint_normal("%s: ", sc->mii_dev.dv_xname);
132 
133 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
134 	if (sc->mii_flags & MIIF_HAVEFIBER) {
135 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, 0, sc->mii_inst),
136 		    MII_MEDIA_100_TX);
137 		aprint_normal("100baseFX, ");
138 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, IFM_FDX, sc->mii_inst),
139 		    MII_MEDIA_100_TX);
140 		aprint_normal("100baseFX-FDX, ");
141 	}
142 #undef ADD
143 
144 	if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
145 		aprint_error("no media present");
146 	else
147 		mii_phy_add_media(sc);
148 	aprint_normal("\n");
149 
150 	if (!pmf_device_register(self, NULL, mii_phy_resume))
151 		aprint_error_dev(self, "couldn't establish power handler\n");
152 }
153 
154 static int
155 acphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
156 {
157 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
158 	int reg;
159 
160 	switch (cmd) {
161 	case MII_POLLSTAT:
162 		/*
163 		 * If we're not polling our PHY instance, just return.
164 		 */
165 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
166 			return (0);
167 		break;
168 
169 	case MII_MEDIACHG:
170 		/*
171 		 * If the media indicates a different PHY instance,
172 		 * isolate ourselves.
173 		 */
174 		if (IFM_INST(ife->ifm_media) != sc->mii_inst) {
175 			reg = PHY_READ(sc, MII_BMCR);
176 			PHY_WRITE(sc, MII_BMCR, reg | BMCR_ISO);
177 			return (0);
178 		}
179 
180 		/*
181 		 * If the interface is not up, don't do anything.
182 		 */
183 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
184 			break;
185 
186 		mii_phy_setmedia(sc);
187 		break;
188 
189 	case MII_TICK:
190 		/*
191 		 * If we're not currently selected, just return.
192 		 */
193 		if (IFM_INST(ife->ifm_media) != sc->mii_inst)
194 			return (0);
195 
196 		if (mii_phy_tick(sc) == EJUSTRETURN)
197 			return (0);
198 		break;
199 
200 	case MII_DOWN:
201 		mii_phy_down(sc);
202 		return (0);
203 	}
204 
205 	/* Update the media status. */
206 	mii_phy_status(sc);
207 
208 	/* Callback if something changed. */
209 	mii_phy_update(sc, cmd);
210 	return (0);
211 }
212 
213 static void
214 acphy_status(struct mii_softc *sc)
215 {
216 	struct mii_data *mii = sc->mii_pdata;
217 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
218 	int bmsr, bmcr, dr;
219 
220 	mii->mii_media_status = IFM_AVALID;
221 	mii->mii_media_active = IFM_ETHER;
222 
223 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
224 	dr = PHY_READ(sc, MII_ACPHY_DR);
225 
226 	if (bmsr & BMSR_LINK)
227 		mii->mii_media_status |= IFM_ACTIVE;
228 
229 	bmcr = PHY_READ(sc, MII_BMCR);
230 	if (bmcr & BMCR_ISO) {
231 		mii->mii_media_active |= IFM_NONE;
232 		mii->mii_media_status = 0;
233 		return;
234 	}
235 
236 	if (bmcr & BMCR_LOOP)
237 		mii->mii_media_active |= IFM_LOOP;
238 
239 	if (bmcr & BMCR_AUTOEN) {
240 		/*
241 		 * The media status bits are only valid if autonegotiation
242 		 * has completed (or it's disabled).
243 		 */
244 		if ((bmsr & BMSR_ACOMP) == 0) {
245 			/* Erg, still trying, I guess... */
246 			mii->mii_media_active |= IFM_NONE;
247 			return;
248 		}
249 
250 		if (dr & DR_SPEED)
251 			mii->mii_media_active |= IFM_100_TX;
252 		else
253 			mii->mii_media_active |= IFM_10_T;
254 		if (dr & DR_DPLX)
255 			mii->mii_media_active |= IFM_FDX;
256 	} else
257 		mii->mii_media_active = ife->ifm_media;
258 }
259