smscphy.c revision 1.1 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 sc->mii_anegticks = MII_ANEGTICKS;
111
112 PHY_RESET(sc);
113
114 PHY_READ(sc, MII_BMSR, &sc->mii_capabilities);
115 sc->mii_capabilities &= ma->mii_capmask;
116 aprint_normal_dev(self, "");
117 if ((sc->mii_capabilities & BMSR_MEDIAMASK) == 0)
118 aprint_error("no media present");
119 else
120 mii_phy_add_media(sc);
121 aprint_normal("\n");
122 }
123
124 static void
125 smscphy_power(struct mii_softc *sc, bool power)
126 {
127 uint16_t bmcr, new;
128
129 PHY_READ(sc, MII_BMCR, &bmcr);
130 if (power)
131 new = bmcr & ~BMCR_PDOWN;
132 else
133 new = bmcr | BMCR_PDOWN;
134 if (bmcr != new)
135 PHY_WRITE(sc, MII_BMCR, new);
136 }
137
138 static int
139 smscphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd)
140 {
141 struct ifmedia_entry *ife;
142 uint16_t reg;
143
144 ife = mii->mii_media.ifm_cur;
145
146 switch (cmd) {
147 case MII_POLLSTAT:
148 break;
149
150 case MII_MEDIACHG:
151 /* Try to power up the PHY in case it's down */
152 if (IFM_SUBTYPE(ife->ifm_media) != IFM_NONE)
153 smscphy_power(sc, true);
154
155 switch (IFM_SUBTYPE(ife->ifm_media)) {
156 case IFM_AUTO:
157 smscphy_auto(sc, ife->ifm_media);
158 break;
159
160 default:
161 mii_phy_setmedia(sc);
162 if (IFM_SUBTYPE(ife->ifm_media) == IFM_NONE)
163 smscphy_power(sc, false);
164 break;
165 }
166 break;
167
168 case MII_TICK:
169 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
170 break;
171
172 PHY_READ(sc, MII_BMSR, ®);
173 PHY_READ(sc, MII_BMSR, ®);
174 if (reg & BMSR_LINK) {
175 sc->mii_ticks = 0;
176 break;
177 }
178
179 if (++sc->mii_ticks <= MII_ANEGTICKS)
180 break;
181
182 PHY_RESET(sc);
183 smscphy_auto(sc, ife->ifm_media);
184 break;
185 }
186
187 /* Update the media status. */
188 PHY_STATUS(sc);
189
190 /* Callback if something changed. */
191 mii_phy_update(sc, cmd);
192 return 0;
193 }
194
195 static void
196 smscphy_auto(struct mii_softc *sc, int media)
197 {
198 uint16_t anar;
199
200 sc->mii_ticks = 0;
201 anar = BMSR_MEDIA_TO_ANAR(sc->mii_capabilities) | ANAR_CSMA;
202 if ((media & IFM_FLOW) != 0)
203 anar |= ANAR_FC;
204 PHY_WRITE(sc, MII_ANAR, anar);
205 /* Apparently this helps. */
206 PHY_READ(sc, MII_ANAR, &anar);
207 PHY_WRITE(sc, MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
208 }
209
210 static void
211 smscphy_status(struct mii_softc *sc)
212 {
213 struct mii_data *mii = sc->mii_pdata;
214 uint16_t bmcr, bmsr, status;
215
216 mii->mii_media_status = IFM_AVALID;
217 mii->mii_media_active = IFM_ETHER;
218
219 PHY_READ(sc, MII_BMSR, &bmsr);
220 PHY_READ(sc, MII_BMSR, &bmsr);
221 if ((bmsr & BMSR_LINK) != 0)
222 mii->mii_media_status |= IFM_ACTIVE;
223
224 PHY_READ(sc, MII_BMCR, &bmcr);
225 if ((bmcr & BMCR_ISO) != 0) {
226 mii->mii_media_active |= IFM_NONE;
227 mii->mii_media_status = 0;
228 return;
229 }
230
231 if ((bmcr & BMCR_LOOP) != 0)
232 mii->mii_media_active |= IFM_LOOP;
233
234 if ((bmcr & BMCR_AUTOEN) != 0) {
235 if ((bmsr & BMSR_ACOMP) == 0) {
236 /* Erg, still trying, I guess... */
237 mii->mii_media_active |= IFM_NONE;
238 return;
239 }
240 }
241
242 PHY_READ(sc, SMSCPHY_SPCSR, &status);
243 if ((status & SPCSR_SPDIND_SPDMASK) == SPCSR_SPDIND_100)
244 mii->mii_media_active |= IFM_100_TX;
245 else
246 mii->mii_media_active |= IFM_10_T;
247 if (status & SPCSR_SPDIND_FDX)
248 mii->mii_media_active |= IFM_FDX | mii_phy_flowstatus(sc);
249 else
250 mii->mii_media_active |= IFM_HDX;
251 }
252