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