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