xref: /freebsd/sys/dev/mii/axphy.c (revision b0b1dbdd)
1 /*-
2  * Copyright (c) 2009, M. Warner Losh
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 unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * driver for internal phy in the AX88x9x chips.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/socket.h>
39 #include <sys/bus.h>
40 
41 #include <net/if.h>
42 #include <net/if_media.h>
43 
44 #include <dev/mii/mii.h>
45 #include <dev/mii/miivar.h>
46 #include "miidevs.h"
47 
48 #include "miibus_if.h"
49 
50 static int 	axphy_probe(device_t dev);
51 static int 	axphy_attach(device_t dev);
52 
53 static device_method_t axphy_methods[] = {
54 	/* device interface */
55 	DEVMETHOD(device_probe,		axphy_probe),
56 	DEVMETHOD(device_attach,	axphy_attach),
57 	DEVMETHOD(device_detach,	mii_phy_detach),
58 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
59 	DEVMETHOD_END
60 };
61 
62 static devclass_t axphy_devclass;
63 
64 static driver_t axphy_driver = {
65 	"axphy",
66 	axphy_methods,
67 	sizeof(struct mii_softc)
68 };
69 
70 DRIVER_MODULE(axphy, miibus, axphy_driver, axphy_devclass, 0, 0);
71 
72 static int	axphy_service(struct mii_softc *, struct mii_data *, int);
73 static void	axphy_status(struct mii_softc *);
74 
75 static const struct mii_phydesc axphys[] = {
76 	MII_PHY_DESC(xxASIX, AX88X9X),
77 	MII_PHY_END
78 };
79 
80 static const struct mii_phy_funcs axphy_funcs = {
81 	axphy_service,
82 	axphy_status,
83 	mii_phy_reset
84 };
85 
86 static int
87 axphy_probe(device_t dev)
88 {
89 
90 	return (mii_phy_dev_probe(dev, axphys, BUS_PROBE_DEFAULT));
91 }
92 
93 static int
94 axphy_attach(device_t dev)
95 {
96 	struct mii_softc *sc;
97 
98 	sc = device_get_softc(dev);
99 
100 	mii_phy_dev_attach(dev, MIIF_NOISOLATE | MIIF_NOMANPAUSE,
101 	    &axphy_funcs, 1);
102 	mii_phy_setmedia(sc);
103 
104 	return (0);
105 }
106 
107 static int
108 axphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
109 {
110 
111 	switch (cmd) {
112 	case MII_POLLSTAT:
113 		break;
114 
115 	case MII_MEDIACHG:
116 		mii_phy_setmedia(sc);
117 		break;
118 
119 	case MII_TICK:
120 		if (mii_phy_tick(sc) == EJUSTRETURN)
121 			return (0);
122 		break;
123 	}
124 
125 	/* Update the media status. */
126 	PHY_STATUS(sc);
127 
128 	/* Callback if something changed. */
129 	mii_phy_update(sc, cmd);
130 	return (0);
131 }
132 
133 static void
134 axphy_status(struct mii_softc *sc)
135 {
136 	struct mii_data *mii = sc->mii_pdata;
137 	struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
138 	int bmsr, bmcr;
139 
140 	mii->mii_media_status = IFM_AVALID;
141 	mii->mii_media_active = IFM_ETHER;
142 
143 	bmsr = PHY_READ(sc, MII_BMSR) | PHY_READ(sc, MII_BMSR);
144 	if (bmsr & BMSR_LINK)
145 		mii->mii_media_status |= IFM_ACTIVE;
146 
147 	bmcr = PHY_READ(sc, MII_BMCR);
148 	if (bmcr & BMCR_ISO) {
149 		mii->mii_media_active |= IFM_NONE;
150 		mii->mii_media_status = 0;
151 		return;
152 	}
153 
154 	if (bmcr & BMCR_LOOP)
155 		mii->mii_media_active |= IFM_LOOP;
156 
157 	if (bmcr & BMCR_AUTOEN) {
158 		if ((bmsr & BMSR_ACOMP) == 0) {
159 			mii->mii_media_active |= IFM_NONE;
160 			return;
161 		}
162 
163 #if 0
164 		scr = PHY_READ(sc, MII_AXPHY_SCR);
165 		if (scr & SCR_S100)
166 			mii->mii_media_active |= IFM_100_TX;
167 		else
168 			mii->mii_media_active |= IFM_10_T;
169 		if (scr & SCR_FDX)
170 			mii->mii_media_active |=
171 			    IFM_FDX | mii_phy_flowstatus(sc);
172 		else
173 			mii->mii_media_active |= IFM_HDX;
174 #endif
175 	} else
176 		mii->mii_media_active = ife->ifm_media;
177 }
178