inphy.c revision 215905
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: head/sys/dev/mii/inphy.c 215905 2010-11-26 21:41:42Z marius $");
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_media.h>
47
48#include <dev/mii/mii.h>
49#include <dev/mii/miivar.h>
50#include "miidevs.h"
51
52#include <dev/mii/inphyreg.h>
53
54#include "miibus_if.h"
55
56static int 	inphy_probe(device_t dev);
57static int 	inphy_attach(device_t dev);
58
59static device_method_t inphy_methods[] = {
60	/* device interface */
61	DEVMETHOD(device_probe,		inphy_probe),
62	DEVMETHOD(device_attach,	inphy_attach),
63	DEVMETHOD(device_detach,	mii_phy_detach),
64	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
65	{ 0, 0 }
66};
67
68static devclass_t inphy_devclass;
69
70static driver_t inphy_driver = {
71	"inphy",
72	inphy_methods,
73	sizeof(struct mii_softc)
74};
75
76DRIVER_MODULE(inphy, miibus, inphy_driver, inphy_devclass, 0, 0);
77
78static int	inphy_service(struct mii_softc *, struct mii_data *, int);
79static void	inphy_status(struct mii_softc *);
80static void	inphy_reset(struct mii_softc *);
81
82static const struct mii_phydesc inphys[] = {
83	MII_PHY_DESC(INTEL, I82553C),
84	MII_PHY_DESC(INTEL, I82555),
85	MII_PHY_DESC(INTEL, I82562EM),
86	MII_PHY_DESC(INTEL, I82562ET),
87	MII_PHY_DESC(xxINTEL, I82553AB),
88	MII_PHY_END
89};
90
91static int
92inphy_probe(device_t dev)
93{
94
95	return (mii_phy_dev_probe(dev, inphys, BUS_PROBE_DEFAULT));
96}
97
98static int
99inphy_attach(device_t dev)
100{
101	struct mii_softc *sc;
102	struct mii_attach_args *ma;
103	struct mii_data *mii;
104
105	sc = device_get_softc(dev);
106	ma = device_get_ivars(dev);
107	sc->mii_dev = device_get_parent(dev);
108	mii = ma->mii_data;
109	LIST_INSERT_HEAD(&mii->mii_phys, sc, mii_list);
110
111	sc->mii_flags = miibus_get_flags(dev);
112	sc->mii_inst = mii->mii_instance++;
113	sc->mii_phy = ma->mii_phyno;
114	sc->mii_service = inphy_service;
115	sc->mii_pdata = mii;
116
117	sc->mii_flags |= MIIF_NOMANPAUSE;
118
119	ifmedia_add(&mii->mii_media,
120	    IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_LOOP, sc->mii_inst),
121	    MII_MEDIA_100_TX, NULL);
122
123	inphy_reset(sc);
124
125	sc->mii_capabilities = PHY_READ(sc, MII_BMSR) & ma->mii_capmask;
126	device_printf(dev, " ");
127	mii_phy_add_media(sc);
128	printf("\n");
129
130	MIIBUS_MEDIAINIT(sc->mii_dev);
131
132	return (0);
133}
134
135static int
136inphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
137{
138
139	switch (cmd) {
140	case MII_POLLSTAT:
141		break;
142
143	case MII_MEDIACHG:
144		/*
145		 * If the interface is not up, don't do anything.
146		 */
147		if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
148			break;
149
150		mii_phy_setmedia(sc);
151		break;
152
153	case MII_TICK:
154		if (mii_phy_tick(sc) == EJUSTRETURN)
155			return (0);
156		break;
157	}
158
159	/* Update the media status. */
160	inphy_status(sc);
161
162	/* Callback if something changed. */
163	mii_phy_update(sc, cmd);
164	return (0);
165}
166
167static void
168inphy_status(struct mii_softc *sc)
169{
170	struct mii_data *mii = sc->mii_pdata;
171	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
172	int bmsr, bmcr, scr;
173
174	mii->mii_media_status = IFM_AVALID;
175	mii->mii_media_active = IFM_ETHER;
176
177	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
178	if (bmsr & BMSR_LINK)
179		mii->mii_media_status |= IFM_ACTIVE;
180
181	bmcr = PHY_READ(sc, MII_BMCR);
182	if (bmcr & BMCR_ISO) {
183		mii->mii_media_active |= IFM_NONE;
184		mii->mii_media_status = 0;
185		return;
186	}
187
188	if (bmcr & BMCR_LOOP)
189		mii->mii_media_active |= IFM_LOOP;
190
191	if (bmcr & BMCR_AUTOEN) {
192		if ((bmsr & BMSR_ACOMP) == 0) {
193			mii->mii_media_active |= IFM_NONE;
194			return;
195		}
196
197		scr = PHY_READ(sc, MII_INPHY_SCR);
198		if (scr & SCR_S100)
199			mii->mii_media_active |= IFM_100_TX;
200		else
201			mii->mii_media_active |= IFM_10_T;
202		if (scr & SCR_FDX)
203			mii->mii_media_active |=
204			    IFM_FDX | mii_phy_flowstatus(sc);
205		else
206			mii->mii_media_active |= IFM_HDX;
207	} else
208		mii->mii_media_active = ife->ifm_media;
209}
210
211static void
212inphy_reset(struct mii_softc *sc)
213{
214
215	mii_phy_reset(sc);
216
217	/* Ensure Bay flow control is disabled. */
218	PHY_WRITE(sc, MII_INPHY_SCR,
219	    PHY_READ(sc, MII_INPHY_SCR) & ~SCR_FLOWCTL);
220}
221