xref: /openbsd-src/sys/dev/mii/acphy.c (revision 8500990981f885cbe5e6a4958549cacc238b5ae6)
1 /*	$OpenBSD: acphy.c,v 1.1 2003/08/12 19:42:37 mickey Exp $	*/
2 /*	$NetBSD: acphy.c,v 1.13 2003/04/29 01:49:33 thorpej Exp $	*/
3 
4 /*
5  * Copyright 2001 Wasabi Systems, Inc.
6  * All rights reserved.
7  *
8  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed for the NetBSD Project by
21  *	Wasabi Systems, Inc.
22  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
23  *    or promote products derived from this software without specific prior
24  *    written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Driver for the Altima AC101 PHY.
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/device.h>
47 #include <sys/socket.h>
48 #include <sys/errno.h>
49 
50 #include <net/if.h>
51 #include <net/if_media.h>
52 
53 #include <dev/mii/mii.h>
54 #include <dev/mii/miivar.h>
55 #include <dev/mii/miidevs.h>
56 
57 #include <dev/mii/acphyreg.h>
58 
59 int	acphymatch(struct device *, void *, void *);
60 void	acphyattach(struct device *, struct device *, void *);
61 
62 struct cfattach acphy_ca = {
63 	sizeof(struct mii_softc), acphymatch, acphyattach, mii_phy_detach,
64 	    mii_phy_activate
65 };
66 
67 struct cfdriver acphy_cd = {
68 	NULL, "acphy", DV_DULL
69 };
70 
71 int	acphy_service(struct mii_softc *, struct mii_data *, int);
72 void	acphy_status(struct mii_softc *);
73 
74 int
75 acphymatch(struct device *parent, void *match, void *aux)
76 {
77 	struct mii_attach_args *ma = aux;
78 
79 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxALTIMA &&
80 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxALTIMA_AC101)
81 		return (10);
82 	if (MII_OUI(ma->mii_id1, ma->mii_id2) == MII_OUI_xxALTIMA &&
83 	    MII_MODEL(ma->mii_id2) == MII_MODEL_xxALTIMA_AC101L)
84 		return (10);
85 
86 	return (0);
87 }
88 
89 void
90 acphyattach(struct device *parent, struct device *self, void *aux)
91 {
92 	struct mii_softc *sc = (struct mii_softc *)self;
93 	struct mii_attach_args *ma = aux;
94 	struct mii_data *mii = ma->mii_data;
95 
96 	printf(": %s, rev. %d\n", MII_STR_xxALTIMA_AC101, MII_REV(ma->mii_id2));
97 
98 	sc->mii_inst = mii->mii_instance;
99 	sc->mii_phy = ma->mii_phyno;
100 	sc->mii_service = acphy_service;
101 	sc->mii_status = acphy_status;
102 	sc->mii_pdata = mii;
103 	sc->mii_flags = ma->mii_flags;
104 	sc->mii_anegticks = 5;
105 
106 	mii_phy_reset(sc);
107 
108 	/*
109 	 * XXX Check MCR_FX_SEL to set MIIF_HAVE_FIBER?
110 	 */
111 
112 	sc->mii_capabilities =
113 	    PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
114 
115 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
116 	if (sc->mii_flags & MIIF_HAVEFIBER) {
117 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, 0, sc->mii_inst),
118 		    MII_MEDIA_100_TX);
119 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, IFM_FDX, sc->mii_inst),
120 		    MII_MEDIA_100_TX);
121 	}
122 #undef ADD
123 
124 	if (sc->mii_capabilities & BMSR_MEDIAMASK)
125 		mii_phy_add_media(sc);
126 }
127 
128 int
129 acphy_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 void
188 acphy_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, dr;
193 
194 	mii->mii_media_status = IFM_AVALID;
195 	mii->mii_media_active = IFM_ETHER;
196 
197 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
198 	dr = PHY_READ(sc, MII_ACPHY_DR);
199 
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 media 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 (dr & DR_SPEED)
225 			mii->mii_media_active |= IFM_100_TX;
226 		else
227 			mii->mii_media_active |= IFM_10_T;
228 		if (dr & DR_DPLX)
229 			mii->mii_media_active |= IFM_FDX;
230 	} else
231 		mii->mii_media_active = ife->ifm_media;
232 }
233