xref: /freebsd/sys/dev/mii/mv88e151x.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2019 Rubicon Communications, LLC (Netgate)
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, and by Frank van der Linden.
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 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 /* Driver for the Marvell 88e151x gigabit ethernet PHY. */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/socket.h>
42 #include <sys/errno.h>
43 #include <sys/module.h>
44 #include <sys/bus.h>
45 
46 #include <net/if.h>
47 #include <net/if_media.h>
48 
49 #include <dev/mii/mii.h>
50 #include <dev/mii/miivar.h>
51 #include <dev/mii/mv88e151xreg.h>
52 #include "miidevs.h"
53 
54 #include "miibus_if.h"
55 
56 static int mv88e151x_probe(device_t);
57 static int mv88e151x_attach(device_t);
58 
59 static device_method_t mv88e151x_methods[] = {
60 	/* device interface */
61 	DEVMETHOD(device_probe,		mv88e151x_probe),
62 	DEVMETHOD(device_attach,	mv88e151x_attach),
63 	DEVMETHOD(device_detach,	mii_phy_detach),
64 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
65 	DEVMETHOD_END
66 };
67 
68 static driver_t mv88e151x_driver = {
69 	"mv88e151x",
70 	mv88e151x_methods,
71 	sizeof(struct mii_softc)
72 };
73 
74 DRIVER_MODULE(mv88e151x, miibus, mv88e151x_driver, 0, 0);
75 
76 static int mv88e151x_service(struct mii_softc *, struct mii_data *, int);
77 static void mv88e151x_status(struct mii_softc *);
78 
79 static const struct mii_phydesc mv88e151xphys[] = {
80 	MII_PHY_DESC(xxMARVELL, E1512),
81 	MII_PHY_END
82 };
83 
84 static const struct mii_phy_funcs mv88e151x_funcs = {
85 	mv88e151x_service,
86 	mv88e151x_status,
87 	mii_phy_reset
88 };
89 
90 static int
91 mv88e151x_probe(device_t dev)
92 {
93 
94 	return (mii_phy_dev_probe(dev, mv88e151xphys, BUS_PROBE_DEFAULT));
95 }
96 
97 static int
98 mv88e151x_attach(device_t dev)
99 {
100 	const struct mii_attach_args *ma;
101 	struct mii_softc *sc;
102 	uint32_t cop_cap, cop_extcap;
103 
104 	sc = device_get_softc(dev);
105 	ma = device_get_ivars(dev);
106 
107 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &mv88e151x_funcs, 0);
108 
109 	PHY_RESET(sc);
110 	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & sc->mii_capmask;
111 	cop_cap = sc->mii_capabilities;
112 	if (sc->mii_capabilities & BMSR_EXTSTAT) {
113 		sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
114 		cop_extcap = sc->mii_extcapabilities;
115 	}
116 	device_printf(dev, " ");
117 	if (MII_MODEL(ma->mii_id2) == MII_MODEL_xxMARVELL_E1512)
118 		sc->mii_capabilities &= ~BMSR_ANEG;
119 	mii_phy_add_media(sc);
120 	if (MII_MODEL(ma->mii_id2) == MII_MODEL_xxMARVELL_E1512) {
121 		/* Switch the fiber PHY and add the supported media. */
122 		PHY_WRITE(sc, MV88E151X_PAGE, MV88E151X_PAGE_FIBER);
123 		PHY_RESET(sc);
124 		sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & sc->mii_capmask;
125 		if (sc->mii_capabilities & BMSR_EXTSTAT)
126 			sc->mii_extcapabilities = PHY_READ(sc, MII_EXTSR);
127 		sc->mii_flags |= MIIF_NOISOLATE;
128 		printf(", ");
129 		mii_phy_add_media(sc);
130 
131 		/* Switch back to copper PHY. */
132 		PHY_WRITE(sc, MV88E151X_PAGE, MV88E151X_PAGE_COPPER);
133 		sc->mii_flags &= ~MIIF_NOISOLATE;
134 		sc->mii_capabilities = cop_cap;
135 		sc->mii_extcapabilities = cop_extcap;
136 	}
137 	printf("\n");
138 
139 	MIIBUS_MEDIAINIT(sc->mii_dev);
140 
141 	mii_phy_setmedia(sc);
142 
143 	/* Enable the fiber PHY auto negotiation. */
144 	if (MII_MODEL(ma->mii_id2) == MII_MODEL_xxMARVELL_E1512) {
145 		PHY_WRITE(sc, MV88E151X_PAGE, MV88E151X_PAGE_FIBER);
146 		PHY_WRITE(sc, MII_BMCR, BMCR_RESET | BMCR_AUTOEN |
147 		    BMCR_STARTNEG | BMCR_FDX | BMCR_S1000);
148 		PHY_WRITE(sc, MV88E151X_PAGE, MV88E151X_PAGE_COPPER);
149 	}
150 
151 	return (0);
152 }
153 
154 static int
155 mv88e151x_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
156 {
157 	switch (cmd) {
158 	case MII_POLLSTAT:
159 		break;
160 
161 	case MII_MEDIACHG:
162 		mii_phy_setmedia(sc);
163 		break;
164 
165 	case MII_TICK:
166 		if (mii_phy_tick(sc) == EJUSTRETURN)
167 			return (0);
168 		break;
169 	}
170 
171 	/* Update the media status. */
172 	PHY_STATUS(sc);
173 
174 	/* Callback if something changed. */
175 	mii_phy_update(sc, cmd);
176 	return (0);
177 }
178 
179 static void
180 mv88e151x_fiber_status(struct mii_softc *phy)
181 {
182 	struct mii_data *mii = phy->mii_pdata;
183 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
184 	int bmsr, bmcr, anlpar; //, gtcr, gtsr;
185 	uint32_t reg;
186 
187 	mii->mii_media_status = IFM_AVALID;
188 	mii->mii_media_active = IFM_ETHER;
189 
190 	/* Switch to the fiber PHY. */
191 	PHY_WRITE(phy, MV88E151X_PAGE, MV88E151X_PAGE_FIBER);
192 
193 	reg = PHY_READ(phy, MV88E151X_FIBER_STATUS);
194 	bmsr = PHY_READ(phy, MII_BMSR) | PHY_READ(phy, MII_BMSR);
195 	if (reg & MV88E151X_STATUS_LINK)
196 		mii->mii_media_status |= IFM_ACTIVE;
197 	bmcr = PHY_READ(phy, MII_BMCR);
198 	if (bmcr & BMCR_ISO) {
199 		mii->mii_media_active |= IFM_NONE;
200 		mii->mii_media_status = 0;
201 		PHY_WRITE(phy, MV88E151X_PAGE, MV88E151X_PAGE_COPPER);
202 		return;
203 	}
204 
205 	if (bmcr & BMCR_LOOP)
206 		mii->mii_media_active |= IFM_LOOP;
207 
208 	if (bmcr & BMCR_AUTOEN) {
209 		/*
210 		 * NWay autonegotiation takes the highest-order common
211 		 * bit of the ANAR and ANLPAR (i.e. best media advertised
212 		 * both by us and our link partner).
213 		 */
214 		if ((bmsr & BMSR_ACOMP) == 0) {
215 			/* Erg, still trying, I guess... */
216 			mii->mii_media_active |= IFM_NONE;
217 			PHY_WRITE(phy, MV88E151X_PAGE, MV88E151X_PAGE_COPPER);
218 			return;
219 		}
220 
221 		anlpar = PHY_READ(phy, MII_ANAR) & PHY_READ(phy, MII_ANLPAR);
222 		if (anlpar & ANLPAR_X_FD)
223 			mii->mii_media_active |= IFM_1000_SX | IFM_FDX;
224 		else if (anlpar & ANLPAR_X_HD)
225 			mii->mii_media_active |= IFM_1000_SX | IFM_HDX;
226 		else if (reg & MV88E151X_STATUS_LINK &&
227 		    reg & MV88E151X_STATUS_SYNC &&
228 		    (reg & MV88E151X_STATUS_ENERGY) == 0) {
229 			if ((reg & MV88E151X_STATUS_SPEED_MASK) ==
230 			    MV88E151X_STATUS_SPEED_1000)
231 				mii->mii_media_active |= IFM_1000_SX;
232 			else if ((reg & MV88E151X_STATUS_SPEED_MASK) ==
233 			    MV88E151X_STATUS_SPEED_100)
234 				mii->mii_media_active |= IFM_100_FX;
235 			else
236 				mii->mii_media_active |= IFM_NONE;
237 			if ((reg & MV88E151X_STATUS_SPEED_MASK) != 0 &&
238 			    (reg & MV88E151X_STATUS_FDX))
239 				mii->mii_media_active |= IFM_FDX;
240 		} else
241 			mii->mii_media_active |= IFM_NONE;
242 
243 		if ((mii->mii_media_active & IFM_NONE) == 0)
244 			phy->mii_flags |= MIIF_IS_1000X;
245 		if ((mii->mii_media_active & IFM_FDX) != 0)
246 			mii->mii_media_active |= mii_phy_flowstatus(phy);
247 	} else
248 		mii->mii_media_active = ife->ifm_media;
249 
250 	if ((mii->mii_media_status & IFM_ACTIVE) == 0)
251 		phy->mii_flags &= ~MIIF_IS_1000X;
252 
253 	/* Switch back to copper PHY. */
254 	PHY_WRITE(phy, MV88E151X_PAGE, MV88E151X_PAGE_COPPER);
255 }
256 
257 static void
258 mv88e151x_status(struct mii_softc *phy)
259 {
260 	struct mii_data *mii = phy->mii_pdata;
261 
262 	mv88e151x_fiber_status(phy);
263 	if (mii->mii_media_status & IFM_ACTIVE)
264 		return;
265 	ukphy_status(phy);
266 }
267