acphy.c revision 331722
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: stable/11/sys/dev/mii/acphy.c 331722 2018-03-29 02:50:57Z eadler $");
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
81static int acphy_probe(device_t);
82static int acphy_attach(device_t);
83
84static 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
93static devclass_t acphy_devclass;
94
95static driver_t acphy_driver = {
96	"acphy",
97	acphy_methods,
98	sizeof(struct mii_softc)
99};
100
101DRIVER_MODULE(acphy, miibus, acphy_driver, acphy_devclass, 0, 0);
102
103static int	acphy_service(struct mii_softc *, struct mii_data *, int);
104static void	acphy_reset(struct mii_softc *);
105static void	acphy_status(struct mii_softc *);
106
107static const struct mii_phydesc acphys[] = {
108	MII_PHY_DESC(ALTIMA, AC101),
109	MII_PHY_DESC(ALTIMA, AC101L),
110	/* XXX This is reported to work, but it's not from any data sheet. */
111	MII_PHY_DESC(ALTIMA, ACXXX),
112	MII_PHY_END
113};
114
115static const struct mii_phy_funcs acphy_funcs = {
116	acphy_service,
117	acphy_status,
118	acphy_reset
119};
120
121static int
122acphy_probe(device_t dev)
123{
124
125	return (mii_phy_dev_probe(dev, acphys, BUS_PROBE_DEFAULT));
126}
127
128static int
129acphy_attach(device_t dev)
130{
131	struct mii_softc *sc;
132
133	sc = device_get_softc(dev);
134
135	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &acphy_funcs, 0);
136
137	PHY_RESET(sc);
138
139	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & sc->mii_capmask;
140	device_printf(dev, " ");
141
142#define	ADD(m)	ifmedia_add(&sc->mii_pdata->mii_media, (m), 0, NULL)
143	if ((PHY_READ(sc, MII_ACPHY_MCTL) & AC_MCTL_FX_SEL) != 0) {
144		sc->mii_flags |= MIIF_HAVEFIBER;
145		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, 0, sc->mii_inst));
146		printf("100baseFX, ");
147		ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_FX, IFM_FDX, sc->mii_inst));
148		printf("100baseFX-FDX, ");
149	}
150#undef ADD
151
152	mii_phy_add_media(sc);
153	printf("\n");
154
155	MIIBUS_MEDIAINIT(sc->mii_dev);
156	return (0);
157}
158
159static int
160acphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
161{
162	int reg;
163
164	switch (cmd) {
165	case MII_POLLSTAT:
166		break;
167
168	case MII_MEDIACHG:
169		/* Wake & deisolate up if necessary */
170		reg = PHY_READ(sc, MII_BMCR);
171		if (reg & (BMCR_ISO | BMCR_PDOWN))
172			PHY_WRITE(sc, MII_BMCR, reg & ~(BMCR_ISO | BMCR_PDOWN));
173
174		mii_phy_setmedia(sc);
175		break;
176
177	case MII_TICK:
178		/*
179		 * This PHY's autonegotiation doesn't need to be kicked.
180		 */
181		break;
182	}
183
184	/* Update the media status. */
185	PHY_STATUS(sc);
186
187	/* Callback if something changed. */
188	mii_phy_update(sc, cmd);
189	return (0);
190}
191
192static void
193acphy_status(struct mii_softc *sc)
194{
195	struct mii_data *mii = sc->mii_pdata;
196	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
197	int bmsr, bmcr, diag;
198
199	mii->mii_media_status = IFM_AVALID;
200	mii->mii_media_active = IFM_ETHER;
201
202	bmsr = PHY_READ(sc, MII_BMSR) |
203	    PHY_READ(sc, MII_BMSR);
204	if (bmsr & BMSR_LINK)
205		mii->mii_media_status |= IFM_ACTIVE;
206
207	bmcr = PHY_READ(sc, MII_BMCR);
208	if (bmcr & BMCR_ISO) {
209		mii->mii_media_active |= IFM_NONE;
210		mii->mii_media_status = 0;
211		return;
212	}
213
214	if (bmcr & BMCR_LOOP)
215		mii->mii_media_active |= IFM_LOOP;
216
217	if (bmcr & BMCR_AUTOEN) {
218		if ((bmsr & BMSR_ACOMP) == 0) {
219			/* Erg, still trying, I guess... */
220			mii->mii_media_active |= IFM_NONE;
221			return;
222		}
223		diag = PHY_READ(sc, MII_ACPHY_DIAG);
224		if (diag & AC_DIAG_SPEED)
225			mii->mii_media_active |= IFM_100_TX;
226		else
227			mii->mii_media_active |= IFM_10_T;
228
229		if (diag & AC_DIAG_DUPLEX)
230			mii->mii_media_active |=
231			    IFM_FDX | mii_phy_flowstatus(sc);
232		else
233			mii->mii_media_active |= IFM_HDX;
234	} else
235		mii->mii_media_active = ife->ifm_media;
236}
237
238static void
239acphy_reset(struct mii_softc *sc)
240{
241
242	mii_phy_reset(sc);
243	PHY_WRITE(sc, MII_ACPHY_INT, 0);
244}
245