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