xref: /freebsd/sys/dev/fxp/inphy.c (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001 Jonathan Lemon
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the author nor the names of any co-contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 /*
37  * driver for Intel 82553 and 82555 PHYs
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/socket.h>
45 #include <sys/bus.h>
46 
47 #include <net/if.h>
48 #include <net/if_var.h>
49 #include <net/if_media.h>
50 
51 #include <dev/mii/mii.h>
52 #include <dev/mii/miivar.h>
53 #include "miidevs.h"
54 
55 #include <dev/fxp/inphyreg.h>
56 
57 #include "miibus_if.h"
58 
59 static int 	inphy_probe(device_t dev);
60 static int 	inphy_attach(device_t dev);
61 
62 static device_method_t inphy_methods[] = {
63 	/* device interface */
64 	DEVMETHOD(device_probe,		inphy_probe),
65 	DEVMETHOD(device_attach,	inphy_attach),
66 	DEVMETHOD(device_detach,	mii_phy_detach),
67 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
68 	{ 0, 0 }
69 };
70 
71 static devclass_t inphy_devclass;
72 
73 static driver_t inphy_driver = {
74 	"inphy",
75 	inphy_methods,
76 	sizeof(struct mii_softc)
77 };
78 
79 DRIVER_MODULE(inphy, miibus, inphy_driver, inphy_devclass, 0, 0);
80 
81 static int	inphy_service(struct mii_softc *, struct mii_data *, int);
82 static void	inphy_status(struct mii_softc *);
83 static void	inphy_reset(struct mii_softc *);
84 
85 static const struct mii_phydesc inphys[] = {
86 	MII_PHY_DESC(xxINTEL, I82553),
87 	MII_PHY_DESC(yyINTEL, I82553),
88 	MII_PHY_DESC(yyINTEL, I82555),
89 	MII_PHY_DESC(yyINTEL, I82562EM),
90 	MII_PHY_DESC(yyINTEL, I82562ET),
91 	MII_PHY_END
92 };
93 
94 static const struct mii_phy_funcs inphy_funcs = {
95 	inphy_service,
96 	inphy_status,
97 	inphy_reset
98 };
99 
100 static int
101 inphy_probe(device_t dev)
102 {
103 
104 	return (mii_phy_dev_probe(dev, inphys, BUS_PROBE_DEFAULT));
105 }
106 
107 static int
108 inphy_attach(device_t dev)
109 {
110 
111 	mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &inphy_funcs, 1);
112 	return (0);
113 }
114 
115 static int
116 inphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
117 {
118 
119 	switch (cmd) {
120 	case MII_POLLSTAT:
121 		break;
122 
123 	case MII_MEDIACHG:
124 		/*
125 		 * If the interface is not up, don't do anything.
126 		 */
127 		if ((if_getflags(mii->mii_ifp) & IFF_UP) == 0)
128 			break;
129 
130 		mii_phy_setmedia(sc);
131 		break;
132 
133 	case MII_TICK:
134 		if (mii_phy_tick(sc) == EJUSTRETURN)
135 			return (0);
136 		break;
137 	}
138 
139 	/* Update the media status. */
140 	PHY_STATUS(sc);
141 
142 	/* Callback if something changed. */
143 	mii_phy_update(sc, cmd);
144 	return (0);
145 }
146 
147 static void
148 inphy_status(struct mii_softc *sc)
149 {
150 	struct mii_data *mii = sc->mii_pdata;
151 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
152 	int bmsr, bmcr, scr;
153 
154 	mii->mii_media_status = IFM_AVALID;
155 	mii->mii_media_active = IFM_ETHER;
156 
157 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
158 	if (bmsr & BMSR_LINK)
159 		mii->mii_media_status |= IFM_ACTIVE;
160 
161 	bmcr = PHY_READ(sc, MII_BMCR);
162 	if (bmcr & BMCR_ISO) {
163 		mii->mii_media_active |= IFM_NONE;
164 		mii->mii_media_status = 0;
165 		return;
166 	}
167 
168 	if (bmcr & BMCR_LOOP)
169 		mii->mii_media_active |= IFM_LOOP;
170 
171 	if (bmcr & BMCR_AUTOEN) {
172 		if ((bmsr & BMSR_ACOMP) == 0) {
173 			mii->mii_media_active |= IFM_NONE;
174 			return;
175 		}
176 
177 		scr = PHY_READ(sc, MII_INPHY_SCR);
178 		if (scr & SCR_S100)
179 			mii->mii_media_active |= IFM_100_TX;
180 		else
181 			mii->mii_media_active |= IFM_10_T;
182 		if (scr & SCR_FDX)
183 			mii->mii_media_active |=
184 			    IFM_FDX | mii_phy_flowstatus(sc);
185 		else
186 			mii->mii_media_active |= IFM_HDX;
187 	} else
188 		mii->mii_media_active = ife->ifm_media;
189 }
190 
191 static void
192 inphy_reset(struct mii_softc *sc)
193 {
194 
195 	mii_phy_reset(sc);
196 
197 	/* Ensure Bay flow control is disabled. */
198 	PHY_WRITE(sc, MII_INPHY_SCR,
199 	    PHY_READ(sc, MII_INPHY_SCR) & ~SCR_FLOWCTL);
200 }
201