smscphy.c revision 1.2 1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2006 Benno Rice. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following 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 ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 /* $FreeBSD: head/sys/dev/mii/smscphy.c 326255 2017-11-27 14:52:40Z pfg $ */
29
30 /*
31 * Driver for the SMSC LAN8710A
32 */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38 #include <sys/socket.h>
39 #include <sys/errno.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 <dev/mii/miidevs.h>
47
48 /* PHY special control/status register */
49 #define SMSCPHY_SPCSR 0x1f
50 #define SPCSR_SPDIND_10 0x0004
51 #define SPCSR_SPDIND_100 0x0008
52 #define SPCSR_SPDIND_SPDMASK 0x000c
53 #define SPCSR_SPDIND_FDX 0x0010
54
55 static int smscphy_match(device_t, cfdata_t, void *);
56 static void smscphy_attach(device_t, device_t, void *);
57
58 CFATTACH_DECL_NEW(smscphy, sizeof (struct mii_softc),
59 smscphy_match, smscphy_attach, mii_phy_detach, mii_phy_activate);
60
61 static void smscphy_power(struct mii_softc *, bool);
62 static int smscphy_service(struct mii_softc *, struct mii_data *, int);
63 static void smscphy_auto(struct mii_softc *, int);
64 static void smscphy_status(struct mii_softc *);
65
66 static const struct mii_phydesc smscphys[] = {
67 MII_PHY_DESC(SMSC, LAN8700),
68 MII_PHY_DESC(SMSC, LAN8710_LAN8720),
69 MII_PHY_END
70 };
71
72 static const struct mii_phy_funcs smscphy_funcs = {
73 smscphy_service,
74 smscphy_status,
75 mii_phy_reset
76 };
77
78 static int
79 smscphy_match(device_t dev, cfdata_t match, void *aux)
80 {
81 struct mii_attach_args *ma = aux;
82
83 if (mii_phy_match(ma, smscphys) != NULL)
84 return 10;
85
86 return 0;
87 }
88
89 static void
90 smscphy_attach(device_t parent, device_t self, void *aux)
91 {
92 struct mii_softc *sc = device_private(self);
93 struct mii_attach_args *ma = aux;
94 struct mii_data *mii = ma->mii_data;
95 const struct mii_phydesc *mpd;
96
97 mpd = mii_phy_match(ma, smscphys);
98 aprint_naive(": Media interface\n");
99 aprint_normal(": %s, rev. %d\n", mpd->mpd_name, MII_REV(ma->mii_id2));
100
101 sc->mii_dev = self;
102 sc->mii_inst = mii->mii_instance;
103 sc->mii_phy = ma->mii_phyno;
104 sc->mii_funcs = &smscphy_funcs;
105 sc->mii_mpd_oui = MII_OUI(ma->mii_id1, ma->mii_id2);
106 sc->mii_mpd_model = MII_MODEL(ma->mii_id2);
107 sc->mii_mpd_rev = MII_REV(ma->mii_id2);
108 sc->mii_pdata = mii;
109 sc->mii_flags = ma->mii_flags;
110
111 PHY_RESET(sc);
112
113 PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
114 sc->mii_capabilities &= ma->mii_capmask;
115
116 mii_phy_add_media(sc);
117 }
118
119 static void
120 smscphy_power(struct mii_softc *sc, bool power)
121 {
122 uint16_t bmcr, new;
123
124 PHY_READ(sc, MII_BMCR, &bmcr);
125 if (power)
126 new = bmcr & ~BMCR_PDOWN;
127 else
128 new = bmcr | BMCR_PDOWN;
129 if (bmcr != new)
130 PHY_WRITE(sc, MII_BMCR, new);
131 }
132
133 static int
134 smscphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
135 {
136 struct ifmedia_entry *ife;
137 uint16_t reg;
138
139 ife = mii->mii_media.ifm_cur;
140
141 switch (cmd) {
142 case MII_POLLSTAT:
143 break;
144
145 case MII_MEDIACHG:
146 /* Try to power up the PHY in case it's down */
147 if (IFM_SUBTYPE(ife->ifm_media) != IFM_NONE)
148 smscphy_power(sc, true);
149
150 switch (IFM_SUBTYPE(ife->ifm_media)) {
151 case IFM_AUTO:
152 smscphy_auto(sc, ife->ifm_media);
153 break;
154
155 default:
156 mii_phy_setmedia(sc);
157 if (IFM_SUBTYPE(ife->ifm_media) == IFM_NONE)
158 smscphy_power(sc, false);
159 break;
160 }
161 break;
162
163 case MII_TICK:
164 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
165 break;
166
167 PHY_READ(sc, MII_BMSR, ®);
168 PHY_READ(sc, MII_BMSR, ®);
169 if (reg & BMSR_LINK) {
170 sc->mii_ticks = 0;
171 break;
172 }
173
174 if (++sc->mii_ticks <= MII_ANEGTICKS)
175 break;
176
177 PHY_RESET(sc);
178 smscphy_auto(sc, ife->ifm_media);
179 break;
180 }
181
182 /* Update the media status. */
183 PHY_STATUS(sc);
184
185 /* Callback if something changed. */
186 mii_phy_update(sc, cmd);
187 return 0;
188 }
189
190 static void
191 smscphy_auto(struct mii_softc *sc, int media)
192 {
193 uint16_t anar;
194
195 sc->mii_ticks = 0;
196 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA;
197 if ((media & IFM_FLOW) != 0)
198 anar |= ANAR_FC;
199 PHY_WRITE(sc, MII_ANAR, anar);
200 /* Apparently this helps. */
201 PHY_READ(sc, MII_ANAR, &anar);
202 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
203 }
204
205 static void
206 smscphy_status(struct mii_softc *sc)
207 {
208 struct mii_data *mii = sc->mii_pdata;
209 uint16_t bmcr, bmsr, status;
210
211 mii->mii_media_status = IFM_AVALID;
212 mii->mii_media_active = IFM_ETHER;
213
214 PHY_READ(sc, MII_BMSR, &bmsr);
215 PHY_READ(sc, MII_BMSR, &bmsr);
216 if ((bmsr & BMSR_LINK) != 0)
217 mii->mii_media_status |= IFM_ACTIVE;
218
219 PHY_READ(sc, MII_BMCR, &bmcr);
220 if ((bmcr & BMCR_ISO) != 0) {
221 mii->mii_media_active |= IFM_NONE;
222 mii->mii_media_status = 0;
223 return;
224 }
225
226 if ((bmcr & BMCR_LOOP) != 0)
227 mii->mii_media_active |= IFM_LOOP;
228
229 if ((bmcr & BMCR_AUTOEN) != 0) {
230 if ((bmsr & BMSR_ACOMP) == 0) {
231 /* Erg, still trying, I guess... */
232 mii->mii_media_active |= IFM_NONE;
233 return;
234 }
235 }
236
237 PHY_READ(sc, SMSCPHY_SPCSR, &status);
238 if ((status & SPCSR_SPDIND_SPDMASK) == SPCSR_SPDIND_100)
239 mii->mii_media_active |= IFM_100_TX;
240 else
241 mii->mii_media_active |= IFM_10_T;
242 if (status & SPCSR_SPDIND_FDX)
243 mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
244 else
245 mii->mii_media_active |= IFM_HDX;
246 }
247