xref: /freebsd/sys/dev/mii/acphy.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*-
34  * Copyright (c) 1997 Manuel Bouyer.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
46  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
48  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
49  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
51  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
52  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
53  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
54  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55  */
56 
57 #include <sys/cdefs.h>
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 	DEVMETHOD_END
91 };
92 
93 static driver_t acphy_driver = {
94 	"acphy",
95 	acphy_methods,
96 	sizeof(struct mii_softc)
97 };
98 
99 DRIVER_MODULE(acphy, miibus, acphy_driver, 0, 0);
100 
101 static int	acphy_service(struct mii_softc *, struct mii_data *, int);
102 static void	acphy_reset(struct mii_softc *);
103 static void	acphy_status(struct mii_softc *);
104 
105 static const struct mii_phydesc acphys[] = {
106 	MII_PHY_DESC(ALTIMA, AC101),
107 	MII_PHY_DESC(ALTIMA, AC101L),
108 	/* XXX This is reported to work, but it's not from any data sheet. */
109 	MII_PHY_DESC(ALTIMA, ACXXX),
110 	MII_PHY_END
111 };
112 
113 static const struct mii_phy_funcs acphy_funcs = {
114 	acphy_service,
115 	acphy_status,
116 	acphy_reset
117 };
118 
119 static int
120 acphy_probe(device_t dev)
121 {
122 
123 	return (mii_phy_dev_probe(dev, acphys, BUS_PROBE_DEFAULT));
124 }
125 
126 static int
127 acphy_attach(device_t dev)
128 {
129 	struct mii_softc *sc;
130 
131 	sc = device_get_softc(dev);
132 
133 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &acphy_funcs, 0);
134 
135 	PHY_RESET(sc);
136 
137 	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & sc->mii_capmask;
138 	device_printf(dev, " ");
139 
140 #define	ADD(m)	ifmedia_add(&sc->mii_pdata->mii_media, (m), 0, NULL)
141 	if ((PHY_READ(sc, MII_ACPHY_MCTL) & AC_MCTL_FX_SEL) != 0) {
142 		sc->mii_flags |= MIIF_HAVEFIBER;
143 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, 0, sc->mii_inst));
144 		printf("100baseFX, ");
145 		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, IFM_FDX, sc->mii_inst));
146 		printf("100baseFX-FDX, ");
147 	}
148 #undef ADD
149 
150 	mii_phy_add_media(sc);
151 	printf("\n");
152 
153 	MIIBUS_MEDIAINIT(sc->mii_dev);
154 	return (0);
155 }
156 
157 static int
158 acphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
159 {
160 	int reg;
161 
162 	switch (cmd) {
163 	case MII_POLLSTAT:
164 		break;
165 
166 	case MII_MEDIACHG:
167 		/* Wake & deisolate up if necessary */
168 		reg = PHY_READ(sc, MII_BMCR);
169 		if (reg & (BMCR_ISO | BMCR_PDOWN))
170 			PHY_WRITE(sc, MII_BMCR, reg & ~(BMCR_ISO | BMCR_PDOWN));
171 
172 		mii_phy_setmedia(sc);
173 		break;
174 
175 	case MII_TICK:
176 		/*
177 		 * This PHY's autonegotiation doesn't need to be kicked.
178 		 */
179 		break;
180 	}
181 
182 	/* Update the media status. */
183 	PHY_STATUS(sc);
184 
185 	/* Callback if something changed. */
186 	mii_phy_update(sc, cmd);
187 	return (0);
188 }
189 
190 static 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, diag;
196 
197 	mii->mii_media_status = IFM_AVALID;
198 	mii->mii_media_active = IFM_ETHER;
199 
200 	bmsr = PHY_READ(sc, MII_BMSR) |
201 	    PHY_READ(sc, MII_BMSR);
202 	if (bmsr & BMSR_LINK)
203 		mii->mii_media_status |= IFM_ACTIVE;
204 
205 	bmcr = PHY_READ(sc, MII_BMCR);
206 	if (bmcr & BMCR_ISO) {
207 		mii->mii_media_active |= IFM_NONE;
208 		mii->mii_media_status = 0;
209 		return;
210 	}
211 
212 	if (bmcr & BMCR_LOOP)
213 		mii->mii_media_active |= IFM_LOOP;
214 
215 	if (bmcr & BMCR_AUTOEN) {
216 		if ((bmsr & BMSR_ACOMP) == 0) {
217 			/* Erg, still trying, I guess... */
218 			mii->mii_media_active |= IFM_NONE;
219 			return;
220 		}
221 		diag = PHY_READ(sc, MII_ACPHY_DIAG);
222 		if (diag & AC_DIAG_SPEED)
223 			mii->mii_media_active |= IFM_100_TX;
224 		else
225 			mii->mii_media_active |= IFM_10_T;
226 
227 		if (diag & AC_DIAG_DUPLEX)
228 			mii->mii_media_active |=
229 			    IFM_FDX | mii_phy_flowstatus(sc);
230 		else
231 			mii->mii_media_active |= IFM_HDX;
232 	} else
233 		mii->mii_media_active = ife->ifm_media;
234 }
235 
236 static void
237 acphy_reset(struct mii_softc *sc)
238 {
239 
240 	mii_phy_reset(sc);
241 	PHY_WRITE(sc, MII_ACPHY_INT, 0);
242 }
243