xref: /freebsd/sys/dev/mii/acphy.c (revision ad30f8e79bd1007cc2476e491bd21b4f5e389e0a)
1 /*-
2  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
7  * NASA Ames Research Center.
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  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /*-
32  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
44  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
45  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
46  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
47  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
52  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53  */
54 
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
57 
58 /*
59  * Driver for Altima AC101 10/100 PHY
60  */
61 
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/socket.h>
66 #include <sys/errno.h>
67 #include <sys/module.h>
68 #include <sys/bus.h>
69 
70 #include <net/if.h>
71 #include <net/if_media.h>
72 
73 #include <dev/mii/mii.h>
74 #include <dev/mii/miivar.h>
75 #include "miidevs.h"
76 
77 #include <dev/mii/acphyreg.h>
78 
79 #include "miibus_if.h"
80 
81 static int acphy_probe(device_t);
82 static int acphy_attach(device_t);
83 
84 static device_method_t acphy_methods[] = {
85 	/* device interface */
86 	DEVMETHOD(device_probe,		acphy_probe),
87 	DEVMETHOD(device_attach,	acphy_attach),
88 	DEVMETHOD(device_detach,	mii_phy_detach),
89 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
90 	{ 0, 0 }
91 };
92 
93 static devclass_t acphy_devclass;
94 
95 static driver_t acphy_driver = {
96 	"acphy",
97 	acphy_methods,
98 	sizeof(struct mii_softc)
99 };
100 
101 DRIVER_MODULE(acphy, miibus, acphy_driver, acphy_devclass, 0, 0);
102 
103 static int	acphy_service(struct mii_softc *, struct mii_data *, int);
104 static void	acphy_reset(struct mii_softc *);
105 static void	acphy_status(struct mii_softc *);
106 
107 static const struct mii_phydesc acphys[] = {
108 	MII_PHY_DESC(xxALTIMA, AC101),
109 	MII_PHY_DESC(xxALTIMA, AC101L),
110 	/* XXX This is reported to work, but it's not from any data sheet. */
111 	MII_PHY_DESC(xxALTIMA, ACXXX),
112 	MII_PHY_END
113 };
114 
115 static int
116 acphy_probe(device_t dev)
117 {
118 
119 	return (mii_phy_dev_probe(dev, acphys, BUS_PROBE_DEFAULT));
120 }
121 
122 static int
123 acphy_attach(device_t dev)
124 {
125 	struct mii_softc *sc;
126 	struct mii_attach_args *ma;
127 	struct mii_data *mii;
128 
129 	sc = device_get_softc(dev);
130 	ma = device_get_ivars(dev);
131 	sc->mii_dev = device_get_parent(dev);
132 	mii = ma->mii_data;
133 	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
134 
135 	sc->mii_flags = miibus_get_flags(dev);
136 	sc->mii_inst = mii->mii_instance++;
137 	sc->mii_phy = ma->mii_phyno;
138 	sc->mii_service = acphy_service;
139 	sc->mii_pdata = mii;
140 
141 	acphy_reset(sc);
142 
143 	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
144 	device_printf(dev, " ");
145 
146 #define	ADD(m, c)	ifmedia_add(&mii->mii_media, (m), (c), NULL)
147 	if ((PHY_READ(sc, MII_ACPHY_MCTL) & AC_MCTL_FX_SEL) != 0) {
148 		sc->mii_flags |= MIIF_HAVEFIBER;
149 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, 0, sc->mii_inst),
150 		    MII_MEDIA_100_TX);
151 		printf("100baseFX, ");
152 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, IFM_FDX, sc->mii_inst),
153 		    MII_MEDIA_100_TX_FDX);
154 		printf("100baseFX-FDX, ");
155 	}
156 #undef ADD
157 
158 	mii_phy_add_media(sc);
159 	printf("\n");
160 
161 	MIIBUS_MEDIAINIT(sc->mii_dev);
162 	return (0);
163 }
164 
165 static int
166 acphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
167 {
168 	int reg;
169 
170 	switch (cmd) {
171 	case MII_POLLSTAT:
172 		break;
173 
174 	case MII_MEDIACHG:
175 		/*
176 		 * If the interface is not up, don't do anything.
177 		 */
178 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
179 			break;
180 
181 		/* Wake & deisolate up if necessary */
182 		reg = PHY_READ(sc, MII_BMCR);
183 		if (reg & (BMCR_ISO | BMCR_PDOWN))
184 			PHY_WRITE(sc, MII_BMCR, reg & ~(BMCR_ISO | BMCR_PDOWN));
185 
186 		mii_phy_setmedia(sc);
187 		break;
188 
189 	case MII_TICK:
190 		/*
191 		 * Is the interface even up?
192 		 */
193 		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
194 			return (0);
195 
196 		/*
197 		 * This PHY's autonegotiation doesn't need to be kicked.
198 		 */
199 		break;
200 	}
201 
202 	/* Update the media status. */
203 	acphy_status(sc);
204 
205 	/* Callback if something changed. */
206 	mii_phy_update(sc, cmd);
207 	return (0);
208 }
209 
210 static void
211 acphy_status(struct mii_softc *sc)
212 {
213 	struct mii_data *mii = sc->mii_pdata;
214 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
215 	int bmsr, bmcr, diag;
216 
217 	mii->mii_media_status = IFM_AVALID;
218 	mii->mii_media_active = IFM_ETHER;
219 
220 	bmsr = PHY_READ(sc, MII_BMSR) |
221 	    PHY_READ(sc, MII_BMSR);
222 	if (bmsr & BMSR_LINK)
223 		mii->mii_media_status |= IFM_ACTIVE;
224 
225 	bmcr = PHY_READ(sc, MII_BMCR);
226 	if (bmcr & BMCR_ISO) {
227 		mii->mii_media_active |= IFM_NONE;
228 		mii->mii_media_status = 0;
229 		return;
230 	}
231 
232 	if (bmcr & BMCR_LOOP)
233 		mii->mii_media_active |= IFM_LOOP;
234 
235 	if (bmcr & BMCR_AUTOEN) {
236 		if ((bmsr & BMSR_ACOMP) == 0) {
237 			/* Erg, still trying, I guess... */
238 			mii->mii_media_active |= IFM_NONE;
239 			return;
240 		}
241 		diag = PHY_READ(sc, MII_ACPHY_DIAG);
242 		if (diag & AC_DIAG_SPEED)
243 			mii->mii_media_active |= IFM_100_TX;
244 		else
245 			mii->mii_media_active |= IFM_10_T;
246 
247 		if (diag & AC_DIAG_DUPLEX)
248 			mii->mii_media_active |= IFM_FDX;
249 		else
250 			mii->mii_media_active |= IFM_HDX;
251 	} else
252 		mii->mii_media_active = ife->ifm_media;
253 }
254 
255 static void
256 acphy_reset(struct mii_softc *sc)
257 {
258 
259 	mii_phy_reset(sc);
260 	PHY_WRITE(sc, MII_ACPHY_INT, 0);
261 }
262