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